CustomScpExplorer.cpp 394 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835
  1. //---------------------------------------------------------------------------
  2. #define NO_WIN32_LEAN_AND_MEAN
  3. #include <vcl.h>
  4. #pragma hdrstop
  5. #include <Common.h>
  6. #include "CustomScpExplorer.h"
  7. #include <algorithm>
  8. #include <Bookmarks.h>
  9. #include <Interface.h>
  10. #include <Exceptions.h>
  11. #include <CoreMain.h>
  12. #include <FileSystems.h>
  13. #include <TextsCore.h>
  14. #include <TextsWin.h>
  15. #include <HelpWin.h>
  16. #include <VCLCommon.h>
  17. #include <Progress.h>
  18. #include <SynchronizeProgress.h>
  19. #include <DragExt.h>
  20. #include <WinApi.h>
  21. #include "GUITools.h"
  22. #include "NonVisual.h"
  23. #include "Glyphs.h"
  24. #include "Tools.h"
  25. #include "WinConfiguration.h"
  26. #include "TerminalManager.h"
  27. #include "EditorManager.h"
  28. #include "ProgParams.h"
  29. #include "Setup.h"
  30. #include <Consts.hpp>
  31. #include <DateUtils.hpp>
  32. #include <TB2Common.hpp>
  33. #include <DirectoryMonitor.hpp>
  34. #include <System.IOUtils.hpp>
  35. //---------------------------------------------------------------------------
  36. #pragma package(smart_init)
  37. #pragma link "CustomDirView"
  38. #pragma link "CustomUnixDirView"
  39. #pragma link "IEListView"
  40. #pragma link "NortonLikeListView"
  41. #pragma link "UnixDirView"
  42. #pragma link "CustomDriveView"
  43. #pragma link "UnixDriveView"
  44. #pragma link "CustomDriveView"
  45. #pragma link "TB2Dock"
  46. #pragma link "TBXStatusBars"
  47. #pragma link "TB2Item"
  48. #pragma link "TB2Toolbar"
  49. #pragma link "ThemePageControl"
  50. #pragma link "PathLabel"
  51. #pragma resource "*.dfm"
  52. //---------------------------------------------------------------------------
  53. #define WM_COMPONENT_HIDE (WM_WINSCP_USER + 4)
  54. static const int SessionPanelCount = 3;
  55. //---------------------------------------------------------------------------
  56. class TMutexGuard
  57. {
  58. public:
  59. TMutexGuard(HANDLE AMutex, int Message = MUTEX_RELEASE_TIMEOUT,
  60. int Timeout = 5000)
  61. {
  62. FMutex = NULL;
  63. unsigned long WaitResult = WaitForSingleObject(AMutex, Timeout);
  64. if (WaitResult == WAIT_TIMEOUT)
  65. {
  66. throw Exception(LoadStr(MUTEX_RELEASE_TIMEOUT));
  67. }
  68. else
  69. {
  70. FMutex = AMutex;
  71. }
  72. }
  73. ~TMutexGuard()
  74. {
  75. if (FMutex != NULL)
  76. {
  77. ReleaseMutex(FMutex);
  78. }
  79. }
  80. private:
  81. HANDLE FMutex;
  82. };
  83. //---------------------------------------------------------------------------
  84. class TWindowLock
  85. {
  86. public:
  87. TWindowLock(TCustomScpExplorerForm * Form) :
  88. FForm(Form)
  89. {
  90. FForm->LockWindow();
  91. }
  92. ~TWindowLock()
  93. {
  94. FForm->UnlockWindow();
  95. }
  96. private:
  97. TCustomScpExplorerForm * FForm;
  98. };
  99. //---------------------------------------------------------------------------
  100. TAutoBatch::TAutoBatch(TCustomScpExplorerForm * Form) :
  101. FForm(Form)
  102. {
  103. FForm->BatchStart(FBatchStorage);
  104. }
  105. //---------------------------------------------------------------------------
  106. TAutoBatch::~TAutoBatch()
  107. {
  108. FForm->BatchEnd(FBatchStorage);
  109. }
  110. //---------------------------------------------------------------------------
  111. struct TTransferOperationParam
  112. {
  113. TTransferOperationParam();
  114. UnicodeString TargetDirectory;
  115. bool Temp;
  116. bool DragDrop;
  117. int Options;
  118. TAutoSwitch Queue;
  119. };
  120. //---------------------------------------------------------------------------
  121. TTransferOperationParam::TTransferOperationParam()
  122. {
  123. Temp = false;
  124. DragDrop = false;
  125. Options = 0;
  126. Queue = asAuto;
  127. }
  128. //---------------------------------------------------------------------------
  129. class TTransferPresetNoteData : public TObject
  130. {
  131. public:
  132. UnicodeString Message;
  133. };
  134. //---------------------------------------------------------------------------
  135. class TTerminalNoteData : public TObject
  136. {
  137. public:
  138. TManagedTerminal * Terminal;
  139. };
  140. //---------------------------------------------------------------------------
  141. __fastcall TCustomScpExplorerForm::TCustomScpExplorerForm(TComponent* Owner):
  142. FFormRestored(false),
  143. TForm(Owner)
  144. {
  145. AddStartupSequence(L"F");
  146. FInvalid = false;
  147. FCurrentSide = osRemote;
  148. FEverShown = false;
  149. FDocks = new TList();
  150. InitControls();
  151. DebugAssert(NonVisualDataModule && !NonVisualDataModule->ScpExplorer);
  152. // So that UpdateCustomCommandsToolbar called from RestoreParams works
  153. NonVisualDataModule->ScpExplorer = this;
  154. RestoreParams();
  155. ConfigurationChanged();
  156. RemoteDirView->Invalidate();
  157. FAutoOperation = false;
  158. FOnFileOperationFinished = NULL;
  159. FPrimaryOperation = foNone;
  160. FForceExecution = false;
  161. FIgnoreNextDialogChar = 0;
  162. FErrorList = NULL;
  163. FSynchronizeProgressForm = NULL;
  164. FProgressForm = NULL;
  165. FRefreshLocalDirectory = false;
  166. FRefreshRemoteDirectory = false;
  167. FDDExtMapFile = NULL;
  168. // CreateMutexW keeps failing with ERROR_NOACCESS
  169. FDDExtMutex = CreateMutexA(NULL, false, AnsiString(DRAG_EXT_MUTEX).c_str());
  170. DebugAssert(FDDExtMutex != NULL);
  171. FDDTargetControl = NULL;
  172. FDelayedDeletionTimer = NULL;
  173. FDelayedDeletionList = new TStringList();
  174. FDDFileList = NULL;
  175. FPendingTempSpaceWarn = false;
  176. FCapturedLog = NULL;
  177. FDragDropOperation = false;
  178. memset(&FHistoryMenu, 0, sizeof(FHistoryMenu));
  179. FAllowTransferPresetAutoSelect = true;
  180. FSessionChanging = false;
  181. FCopyParamDefault = L"";
  182. FSynchronizeController = NULL;
  183. FPendingQueueActionItem = NULL;
  184. FLockLevel = 0;
  185. FLockSuspendLevel = 0;
  186. FDisabledOnLockSuspend = false;
  187. FAlternativeDelete = false;
  188. FTrayIcon = new ::TTrayIcon(0);
  189. FTrayIcon->OnClick = TrayIconClick;
  190. FMaxQueueLength = 0;
  191. FLastContextPopupScreenPoint = TPoint(-1, -1);
  192. FTransferResumeList = NULL;
  193. FDeletedFiles = NULL;
  194. FMoveToQueue = false;
  195. StandaloneOperation = false;
  196. FOnFeedSynchronizeError = NULL;
  197. FOnSynchronizeAbort = NULL;
  198. FSynchronizeTerminal = NULL;
  199. FNeedSession = false;
  200. FDoNotIdleCurrentTerminal = 0;
  201. FIncrementalSearching = 0;
  202. FIncrementalSearchHaveNext = false;
  203. FQueueFileList.reset(new TQueueFileList());
  204. FProgressSide = osCurrent;
  205. FCalculateSizeOperation = NULL;
  206. FDownloadingFromClipboard = false;
  207. FClipboardFakeMonitorsPendingReset = false;
  208. FEditorManager = new TEditorManager();
  209. FEditorManager->OnFileChange = ExecutedFileChanged;
  210. FEditorManager->OnFileReload = ExecutedFileReload;
  211. FEditorManager->OnFileEarlyClosed = ExecutedFileEarlyClosed;
  212. FEditorManager->OnFileUploadComplete = ExecutedFileUploadComplete;
  213. FLocalEditors = new TList();
  214. FQueueStatus = NULL;
  215. FQueueStatusSection = new TCriticalSection();
  216. FQueueStatusInvalidated = false;
  217. FQueueItemInvalidated = false;
  218. FQueueStatusUpdating = false;
  219. FQueueActedItem = NULL;
  220. FQueueController = new TQueueController(QueueView3);
  221. FQueueScrollOnDragOver = new TListViewScrollOnDragOver(QueueView3, true);
  222. FUserActionTimer = new TTimer(this);
  223. FUserActionTimer->Enabled = false;
  224. FUserActionTimer->Interval = 10;
  225. FUserActionTimer->OnTimer = UserActionTimer;
  226. FNotes = new TStringList();
  227. FNoteTimer = new TTimer(this);
  228. FNoteTimer->Enabled = false;
  229. FNoteTimer->OnTimer = NoteTimer;
  230. FOnNoteClick = NULL;
  231. FOle32Library = LoadLibrary(L"Ole32.dll");
  232. FDragMoveCursor = FOle32Library != NULL ?
  233. LoadCursor(FOle32Library, MAKEINTRESOURCE(2)) : NULL;
  234. UseSystemSettings(this);
  235. TTBXStringList * TransferList = dynamic_cast<TTBXStringList*>(
  236. static_cast<TObject*>(GetComponent(fcTransferList)));
  237. DebugAssert(TransferList != NULL);
  238. FTransferListHoverIndex = -1;
  239. TransferList->OnChange = TransferListChange;
  240. TransferList->OnDrawItem = TransferListDrawItem;
  241. SetSubmenu(dynamic_cast<TTBXCustomItem *>(static_cast<TObject *>(GetComponent(fcColorMenu))), true);
  242. SetSubmenu(NonVisualDataModule->ColorMenuItem, true);
  243. UseDesktopFont(SessionsPageControl);
  244. UpdateSessionsPageControlHeight();
  245. UseDesktopFont(RemoteDirView);
  246. UseDesktopFont(RemoteDriveView);
  247. UseDesktopFont(QueueView3);
  248. UseDesktopFont(QueueFileList);
  249. UseDesktopFont(QueueLabel);
  250. UseDesktopFont(RemoteStatusBar);
  251. reinterpret_cast<TLabel*>(QueueSplitter)->OnDblClick = QueueSplitterDblClick;
  252. QueueSplitter->ShowHint = true;
  253. reinterpret_cast<TLabel*>(QueueFileListSplitter)->OnDblClick = QueueFileListSplitterDblClick;
  254. QueueFileListSplitter->ShowHint = true;
  255. RemotePanelSplitter->ShowHint = true;
  256. UpdateImages();
  257. FCustomCommandMenu = CreateTBXPopupMenu(this);
  258. FCustomCommandLocalFileList = NULL;
  259. FCustomCommandRemoteFileList = NULL;
  260. FSessionColors = new TPngImageList(this);
  261. FSessionColors->ColorDepth = cd32Bit;
  262. AddFixedSessionImages();
  263. SessionsPageControl->Images = FSessionColors;
  264. UpdateQueueLabel();
  265. CheckStoreTransition();
  266. CreateHiddenWindow();
  267. StartUpdates();
  268. }
  269. //---------------------------------------------------------------------------
  270. __fastcall TCustomScpExplorerForm::~TCustomScpExplorerForm()
  271. {
  272. FInvalid = true;
  273. // this has to be one of the very first things to do
  274. StopUpdateThread();
  275. // This is needed when shuting down Windows only. Otherwise it's already set NULL from Execute()
  276. TTerminalManager::Instance()->ScpExplorer = NULL;
  277. delete FCustomCommandLocalFileList;
  278. delete FCustomCommandRemoteFileList;
  279. delete FCustomCommandMenu;
  280. delete FTrayIcon;
  281. FTrayIcon = NULL;
  282. FEditorManager->CloseInternalEditors(ForceCloseInternalEditor);
  283. delete FEditorManager;
  284. ForceCloseLocalEditors();
  285. delete FLocalEditors;
  286. HideFileFindDialog();
  287. if (FDelayedDeletionTimer)
  288. {
  289. DoDelayedDeletion(NULL);
  290. SAFE_DESTROY(FDelayedDeletionTimer);
  291. }
  292. SAFE_DESTROY(FDelayedDeletionList);
  293. // sometimes we do not get DDEnd so the list is not released
  294. SAFE_DESTROY(FDDFileList);
  295. DebugAssert(FSynchronizeController == NULL);
  296. CloseHandle(FDDExtMutex);
  297. FDDExtMutex = NULL;
  298. FreeLibrary(FOle32Library);
  299. FOle32Library = NULL;
  300. FDragMoveCursor = NULL;
  301. DebugAssert(!FErrorList);
  302. if (FEverShown)
  303. {
  304. // when window is never shown (like when running command-line operation),
  305. // particularly window site is not restored correctly (BoundsRect value set
  306. // in RestoreForm gets lost during handle allocation), so we do not want
  307. // it to be stored
  308. StoreParams();
  309. }
  310. ManagedSession = NULL;
  311. Queue = NULL;
  312. DebugAssert(NonVisualDataModule && (NonVisualDataModule->ScpExplorer == this));
  313. NonVisualDataModule->ScpExplorer = NULL;
  314. delete FQueueScrollOnDragOver;
  315. FQueueScrollOnDragOver = NULL;
  316. delete FQueueController;
  317. FQueueController = NULL;
  318. delete FQueueStatusSection;
  319. FQueueStatusSection = NULL;
  320. delete FQueueStatus;
  321. FQueueStatus = NULL;
  322. delete FUserActionTimer;
  323. FUserActionTimer = NULL;
  324. delete FNoteTimer;
  325. delete FNotes;
  326. delete FNoteData;
  327. SAFE_DESTROY(FDocks);
  328. SessionsPageControl->Images = NULL;
  329. // only after clearing Terminal (after SessionListChanged => ... => AddSessionColor is called)
  330. SAFE_DESTROY(FSessionColors);
  331. SAFE_DESTROY(FSessionsDragDropFilesEx);
  332. SAFE_DESTROY(FHistoryMenu[0][0]);
  333. SAFE_DESTROY(FHistoryMenu[0][1]);
  334. SAFE_DESTROY(FHistoryMenu[1][0]);
  335. SAFE_DESTROY(FHistoryMenu[1][1]);
  336. if (FHiddenWindow != NULL)
  337. {
  338. DestroyWindow(FHiddenWindow);
  339. FHiddenWindow = NULL;
  340. }
  341. }
  342. //---------------------------------------------------------------------------
  343. void __fastcall TCustomScpExplorerForm::RefreshPanel(const UnicodeString & Session, const UnicodeString & Path)
  344. {
  345. std::unique_ptr<TSessionData> Data;
  346. if (!Session.IsEmpty())
  347. {
  348. bool DefaultsOnly;
  349. Data.reset(StoredSessions->ParseUrl(Session, NULL, DefaultsOnly));
  350. }
  351. TTerminalManager * Manager = TTerminalManager::Instance();
  352. for (int Index = 0; Index < Manager->Count; Index++)
  353. {
  354. TTerminal * Terminal = Manager->Sessions[Index];
  355. if (Session.IsEmpty() ||
  356. Manager->IsActiveTerminalForSite(Terminal, Data.get()))
  357. {
  358. if (Path.IsEmpty())
  359. {
  360. Terminal->ClearCaches();
  361. }
  362. else
  363. {
  364. Terminal->DirectoryModified(Path, true);
  365. }
  366. }
  367. }
  368. // We should flag a pending refresh for the background terminals or busy foreground terminals
  369. if (HasActiveTerminal() &&
  370. CanCommandLineFromAnotherInstance() &&
  371. (Session.IsEmpty() || Manager->IsActiveTerminalForSite(Terminal, Data.get())) &&
  372. (Path.IsEmpty() || UnixIsChildPath(Path, Terminal->CurrentDirectory)))
  373. {
  374. Terminal->ReloadDirectory();
  375. }
  376. }
  377. //---------------------------------------------------------------------------
  378. void __fastcall TCustomScpExplorerForm::WMCopyData(TMessage & Message)
  379. {
  380. PCOPYDATASTRUCT CopyData = reinterpret_cast<PCOPYDATASTRUCT>(Message.LParam);
  381. AppLogFmt(L"Received copy data message with size %d", (int(CopyData->cbData)));
  382. size_t MessageSize = sizeof(TCopyDataMessage);
  383. bool Result = DebugAlwaysTrue(CopyData->cbData == MessageSize);
  384. if (Result)
  385. {
  386. const TCopyDataMessage & Message = *reinterpret_cast<const TCopyDataMessage *>(CopyData->lpData);
  387. Result = (Message.Version == TCopyDataMessage::Version1);
  388. if (Result)
  389. {
  390. switch (Message.Command)
  391. {
  392. case TCopyDataMessage::CommandCanCommandLine:
  393. Result = CanCommandLineFromAnotherInstance();
  394. if (Result)
  395. {
  396. AppLog(L"Can process command-line from other instance");
  397. }
  398. else
  399. {
  400. AppLog(L"Cannot process command-line from other instance");
  401. }
  402. break;
  403. case TCopyDataMessage::CommandCommandLine:
  404. {
  405. UnicodeString CommandLine(Message.CommandLine);
  406. Result = CommandLineFromAnotherInstance(CommandLine);
  407. }
  408. break;
  409. case TCopyDataMessage::RefreshPanel:
  410. RefreshPanel(Message.Refresh.Session, Message.Refresh.Path);
  411. break;
  412. case TCopyDataMessage::MainWindowCheck:
  413. Result = true;
  414. break;
  415. default:
  416. Result = false;
  417. break;
  418. }
  419. }
  420. }
  421. Message.Result = Result ? 1 : 0;
  422. }
  423. //---------------------------------------------------------------------------
  424. void __fastcall TCustomScpExplorerForm::CreateHiddenWindow()
  425. {
  426. WNDCLASS WindowClass = {0};
  427. WindowClass.lpfnWndProc = DefWindowProc;
  428. WindowClass.hInstance = HInstance;
  429. WindowClass.lpszClassName = HIDDEN_WINDOW_NAME;
  430. FHiddenWindow = NULL;
  431. if (RegisterClass(&WindowClass))
  432. {
  433. FHiddenWindow = CreateWindow(HIDDEN_WINDOW_NAME, L"",
  434. WS_POPUP, 0, 0, 0, 0, 0, 0, HInstance, NULL);
  435. }
  436. }
  437. //---------------------------------------------------------------------------
  438. bool __fastcall TCustomScpExplorerForm::CanConsole()
  439. {
  440. return
  441. HasActiveTerminal() &&
  442. (Terminal->IsCapable[fcAnyCommand] || Terminal->IsCapable[fcSecondaryShell]);
  443. }
  444. //---------------------------------------------------------------------------
  445. bool __fastcall TCustomScpExplorerForm::CanCommandLineFromAnotherInstance()
  446. {
  447. bool Result = Visible && !NonVisualDataModule->Busy;
  448. return Result;
  449. }
  450. //---------------------------------------------------------------------------
  451. bool __fastcall TCustomScpExplorerForm::CommandLineFromAnotherInstance(
  452. const UnicodeString & CommandLine)
  453. {
  454. TProgramParams Params(CommandLine);
  455. bool Result = CanCommandLineFromAnotherInstance() && DebugAlwaysTrue(Params.ParamCount > 0);
  456. if (Result)
  457. {
  458. NonVisualDataModule->StartBusy();
  459. try
  460. {
  461. // this action is initiated from another process,
  462. // so it's likely that our window is not visible,
  463. // and user won't see what is going on
  464. Application->BringToFront();
  465. // reload sessions as we may be asked to open a session
  466. // just stored by another instance
  467. StoredSessions->Reload();
  468. UnicodeString SessionName = Params.Param[1];
  469. std::unique_ptr<TObjectList> DataList(new TObjectList());
  470. try
  471. {
  472. UnicodeString DownloadFile; // unused
  473. int Flags = GetCommandLineParseUrlFlags(&Params);
  474. GetLoginData(SessionName, &Params, DataList.get(), DownloadFile, true, this, Flags);
  475. }
  476. catch (EAbort &)
  477. {
  478. // Happens when opening session in PuTTY and in other situations,
  479. // in which we do not want to fail WM_COPYDATA, as that would cause master instance
  480. // to process the command-line on its own (or to pass it to yet another instance)
  481. }
  482. if (DataList->Count > 0)
  483. {
  484. TTerminalManager * Manager = TTerminalManager::Instance();
  485. Manager->ActiveSession = Manager->NewSessions(DataList.get());
  486. }
  487. }
  488. __finally
  489. {
  490. NonVisualDataModule->EndBusy();
  491. }
  492. }
  493. return Result;
  494. }
  495. //---------------------------------------------------------------------------
  496. TManagedTerminal * TCustomScpExplorerForm::GetTerminal()
  497. {
  498. TManagedTerminal * Result;
  499. if ((FManagedSession != NULL) && !FManagedSession->LocalBrowser)
  500. {
  501. Result = FManagedSession;
  502. }
  503. else
  504. {
  505. Result = NULL;
  506. }
  507. return Result;
  508. }
  509. //---------------------------------------------------------------------------
  510. void __fastcall TCustomScpExplorerForm::SetManagedSession(TManagedTerminal * value)
  511. {
  512. if (FManagedSession != value)
  513. {
  514. SessionChanging();
  515. DoSetManagedSession(value, false);
  516. }
  517. }
  518. //---------------------------------------------------------------------------
  519. void __fastcall TCustomScpExplorerForm::DoSetManagedSession(TManagedTerminal * value, bool Replace)
  520. {
  521. DebugAssert(!Replace || ((value != NULL) && !value->LocalBrowser));
  522. FManagedSession = value;
  523. {
  524. TValueRestorer<bool> AllowTransferPresetAutoSelectRestorer(FAllowTransferPresetAutoSelect);
  525. FAllowTransferPresetAutoSelect = false;
  526. TAutoFlag SessionChangingFlag(FSessionChanging);
  527. SessionChanged(Replace);
  528. }
  529. if (Terminal != NULL)
  530. {
  531. TransferPresetAutoSelect();
  532. }
  533. // Update app and tab titles, prevented above by FSessionChanging.
  534. UpdateControls();
  535. }
  536. //---------------------------------------------------------------------------
  537. void __fastcall TCustomScpExplorerForm::ReplaceTerminal(TManagedTerminal * value)
  538. {
  539. DoSetManagedSession(value, true);
  540. }
  541. //---------------------------------------------------------------------------
  542. void __fastcall TCustomScpExplorerForm::SessionChanging()
  543. {
  544. if (ManagedSession != NULL)
  545. {
  546. UpdateSession(ManagedSession);
  547. }
  548. }
  549. //---------------------------------------------------------------------------
  550. void __fastcall TCustomScpExplorerForm::SessionChanged(bool Replaced)
  551. {
  552. if (ManagedSession != NULL)
  553. {
  554. UpdateSessionColor(static_cast<TColor>(ManagedSession->StateData->Color));
  555. }
  556. if (!TTerminalManager::Instance()->Updating)
  557. {
  558. SessionListChanged();
  559. }
  560. if (Terminal != NULL)
  561. {
  562. TObject * State = NULL;
  563. if (WinConfiguration->PreservePanelState)
  564. {
  565. State = Terminal->RemoteExplorerState;
  566. DirView(osRemote)->AnnounceState(State);
  567. }
  568. try
  569. {
  570. if (Replaced)
  571. {
  572. RemoteDirView->ReplaceTerminal(Terminal);
  573. }
  574. else
  575. {
  576. RemoteDirView->Terminal = Terminal;
  577. }
  578. if (Terminal->Active)
  579. {
  580. Terminal->RefreshDirectory();
  581. }
  582. if (State != NULL)
  583. {
  584. DirView(osRemote)->RestoreState(State);
  585. }
  586. }
  587. __finally
  588. {
  589. DirView(osRemote)->AnnounceState(NULL);
  590. }
  591. }
  592. else
  593. {
  594. RemoteDirView->Terminal = NULL;
  595. }
  596. InitStatusBar();
  597. UpdateTransferList();
  598. // Update panels Enable state before refreshing the labels
  599. UpdateControls();
  600. }
  601. //---------------------------------------------------------------------------
  602. void __fastcall TCustomScpExplorerForm::SetQueue(TTerminalQueue * value)
  603. {
  604. if (Queue != value)
  605. {
  606. if (FQueue != NULL)
  607. {
  608. FQueue->OnListUpdate = NULL;
  609. FQueue->OnQueueItemUpdate = NULL;
  610. }
  611. FQueue = value;
  612. if (FQueue != NULL)
  613. {
  614. DebugAssert(FQueue->OnListUpdate == NULL);
  615. FQueue->OnListUpdate = QueueListUpdate;
  616. DebugAssert(FQueue->OnQueueItemUpdate == NULL);
  617. FQueue->OnQueueItemUpdate = QueueItemUpdate;
  618. }
  619. QueueChanged();
  620. }
  621. }
  622. //---------------------------------------------------------------------------
  623. void __fastcall TCustomScpExplorerForm::QueueView3Deletion(TObject * /*Sender*/,
  624. TListItem * Item)
  625. {
  626. if (FQueueActedItem == Item)
  627. {
  628. FQueueActedItem = NULL;
  629. if ((QueueView3->PopupMenu != NULL) &&
  630. (QueueView3->PopupMenu->PopupComponent == QueueView3))
  631. {
  632. // rather "trick", suggested by Jordan on jrsoftware.toolbar2000
  633. ReleaseCapture();
  634. }
  635. }
  636. if (Item->Data == FPendingQueueActionItem)
  637. {
  638. FPendingQueueActionItem = NULL;
  639. }
  640. }
  641. //---------------------------------------------------------------------------
  642. bool TCustomScpExplorerForm::IsAnythingQueued()
  643. {
  644. return (FQueueStatus != NULL) && (FQueueStatus->ActiveAndPendingPrimaryCount > 0);
  645. }
  646. //---------------------------------------------------------------------------
  647. void __fastcall TCustomScpExplorerForm::UpdateQueueStatus(bool QueueChanging)
  648. {
  649. {
  650. TGuard Guard(FQueueStatusSection);
  651. FQueueStatusInvalidated = false;
  652. if (FQueue != NULL)
  653. {
  654. FQueueStatus = FQueue->CreateStatus(FQueueStatus);
  655. }
  656. }
  657. if ((FQueueStatus != NULL) && (FQueueStatus->Count > FMaxQueueLength))
  658. {
  659. FMaxQueueLength = FQueueStatus->Count;
  660. Configuration->Usage->SetMax(L"MaxQueueLength", FMaxQueueLength);
  661. }
  662. {
  663. TAutoFlag Flag(FQueueStatusUpdating);
  664. FQueueController->UpdateQueueStatus(FQueueStatus);
  665. }
  666. UpdateQueueFileList();
  667. SetQueueProgress();
  668. UpdateQueueView();
  669. bool IsEmpty = !IsAnythingQueued();
  670. if (IsEmpty && (Terminal != NULL))
  671. {
  672. TOnceDoneOperation OnceDoneOperation =
  673. NonVisualDataModule->CurrentQueueOnceEmptyOperation();
  674. NonVisualDataModule->ResetQueueOnceEmptyOperation();
  675. if ((FQueue != NULL) && !WinConfiguration->EnableQueueByDefault && !QueueChanging)
  676. {
  677. FQueue->Enabled = false;
  678. }
  679. if ((OnceDoneOperation != odoIdle) && !NonVisualDataModule->Busy)
  680. {
  681. Terminal->CloseOnCompletion(OnceDoneOperation, LoadStr(CLOSED_ON_QUEUE_EMPTY));
  682. }
  683. }
  684. }
  685. //---------------------------------------------------------------------------
  686. UnicodeString __fastcall TCustomScpExplorerForm::GetQueueProgressTitle()
  687. {
  688. UnicodeString Result;
  689. if (FQueueStatus != NULL)
  690. {
  691. if (FQueueStatus->IsOnlyOneActiveAndNoPending())
  692. {
  693. TFileOperationProgressType * ProgressData =
  694. FQueueStatus->Items[FQueueStatus->DoneCount]->ProgressData;
  695. if ((ProgressData != NULL) && ProgressData->InProgress)
  696. {
  697. Result = TProgressForm::ProgressStr(NULL, ProgressData);
  698. }
  699. }
  700. else if (FQueueStatus->ActiveAndPendingPrimaryCount > 1)
  701. {
  702. Result = FMTLOAD(PROGRESS_IN_QUEUE, (FQueueStatus->ActiveAndPendingPrimaryCount));
  703. }
  704. }
  705. return Result;
  706. }
  707. //---------------------------------------------------------------------------
  708. void __fastcall TCustomScpExplorerForm::UpdateQueueView()
  709. {
  710. ComponentVisible[fcQueueView] =
  711. !IsLocalBrowserMode() &&
  712. ((WinConfiguration->QueueView.Show == qvShow) ||
  713. ((WinConfiguration->QueueView.Show == qvHideWhenEmpty) &&
  714. (FQueueStatus != NULL) && (FQueueStatus->Count > 0)));
  715. }
  716. //---------------------------------------------------------------------------
  717. void __fastcall TCustomScpExplorerForm::QueueChanged()
  718. {
  719. if (FQueueStatus != NULL)
  720. {
  721. // UpdateFileList implementation relies on the status being recreated when session changes
  722. delete FQueueStatus;
  723. FQueueStatus = NULL;
  724. }
  725. UpdateQueueStatus(true);
  726. }
  727. //---------------------------------------------------------------------------
  728. void __fastcall TCustomScpExplorerForm::QueueListUpdate(TTerminalQueue * Queue)
  729. {
  730. if (FQueue == Queue)
  731. {
  732. FQueueStatusInvalidated = true;
  733. }
  734. }
  735. //---------------------------------------------------------------------------
  736. void __fastcall TCustomScpExplorerForm::QueueItemUpdate(TTerminalQueue * Queue,
  737. TQueueItem * Item)
  738. {
  739. if (FQueue == Queue)
  740. {
  741. TGuard Guard(FQueueStatusSection);
  742. // this may be running in parallel with QueueChanged
  743. if (FQueueStatus != NULL)
  744. {
  745. TQueueItemProxy * QueueItem = FQueueStatus->FindByQueueItem(Item);
  746. if ((Item->Status == TQueueItem::qsDone) && (Terminal != NULL))
  747. {
  748. FRefreshLocalDirectory = (QueueItem == NULL) ||
  749. (!QueueItem->Info->ModifiedLocal.IsEmpty());
  750. FRefreshRemoteDirectory = (QueueItem == NULL) ||
  751. (!QueueItem->Info->ModifiedRemote.IsEmpty());
  752. }
  753. if (QueueItem != NULL)
  754. {
  755. QueueItem->UserData = (void*)true;
  756. FQueueItemInvalidated = true;
  757. }
  758. }
  759. }
  760. }
  761. //---------------------------------------------------------------------------
  762. bool __fastcall TCustomScpExplorerForm::IsQueueAutoPopup()
  763. {
  764. // during standalone editing, we have no way to see/control queue,
  765. // so we have to always popup prompts automatically
  766. return StandaloneOperation || GUIConfiguration->QueueAutoPopup;
  767. }
  768. //---------------------------------------------------------------------------
  769. void __fastcall TCustomScpExplorerForm::RefreshQueueItems()
  770. {
  771. if (FQueueStatus != NULL)
  772. {
  773. bool QueueAutoPopup = IsQueueAutoPopup();
  774. bool NeedRefresh = FQueueController->NeedRefresh();
  775. bool Refresh = FQueueItemInvalidated || NeedRefresh;
  776. FQueueItemInvalidated = false;
  777. int Limit = Refresh ? FQueueStatus->Count : FQueueStatus->DoneAndActiveCount;
  778. bool Updated = false;
  779. TQueueItemProxy * QueueItem;
  780. bool WasUserAction;
  781. for (int Index = 0; Index < Limit; Index++)
  782. {
  783. bool Update = false;
  784. QueueItem = FQueueStatus->Items[Index];
  785. WasUserAction = TQueueItem::IsUserActionStatus(QueueItem->Status);
  786. if (!NonVisualDataModule->Busy && QueueAutoPopup && WasUserAction &&
  787. (FPendingQueueActionItem == NULL))
  788. {
  789. FPendingQueueActionItem = QueueItem;
  790. FUserActionTimer->Enabled = true;
  791. }
  792. if ((bool)QueueItem->UserData)
  793. {
  794. QueueItem->UserData = (void*)false;
  795. QueueItem->Update();
  796. Updated = true;
  797. Update = true;
  798. }
  799. else if (FQueueController->QueueItemNeedsFrequentRefresh(QueueItem) || NeedRefresh)
  800. {
  801. Update = true;
  802. }
  803. if (Update)
  804. {
  805. FQueueController->RefreshQueueItem(QueueItem);
  806. }
  807. }
  808. if (Updated)
  809. {
  810. NonVisualDataModule->UpdateNonVisibleActions();
  811. SetQueueProgress();
  812. UpdateQueueFileList();
  813. }
  814. }
  815. }
  816. //---------------------------------------------------------------------------
  817. void __fastcall TCustomScpExplorerForm::SetTaskbarListProgressState(TBPFLAG Flags)
  818. {
  819. if (FTaskbarList != NULL)
  820. {
  821. // Could now use Application->MainFormHandle, now that we implement Application->OnGetMainFormHandle
  822. FTaskbarList->SetProgressState(GetMainForm()->Handle, Flags);
  823. }
  824. }
  825. //---------------------------------------------------------------------------
  826. void __fastcall TCustomScpExplorerForm::SetTaskbarListProgressValue(int Progress)
  827. {
  828. if (Progress >= 0)
  829. {
  830. if (FTaskbarList != NULL)
  831. {
  832. FTaskbarList->SetProgressValue(GetMainForm()->Handle, Progress, 100);
  833. }
  834. }
  835. else
  836. {
  837. SetTaskbarListProgressState(TBPF_INDETERMINATE);
  838. }
  839. }
  840. //---------------------------------------------------------------------------
  841. void __fastcall TCustomScpExplorerForm::SetTaskbarListProgressValue(TFileOperationProgressType * ProgressData)
  842. {
  843. int OverallProgress;
  844. // FProgressForm is null when this is called from SetQueueProgress
  845. if ((FProgressForm != NULL) && (FProgressForm->SynchronizeProgress != NULL))
  846. {
  847. OverallProgress = FProgressForm->SynchronizeProgress->Progress(ProgressData);
  848. }
  849. else if (!ProgressData->IsIndeterminate())
  850. {
  851. OverallProgress = ProgressData->OverallProgress();
  852. }
  853. else
  854. {
  855. OverallProgress = -1;
  856. }
  857. SetTaskbarListProgressValue(OverallProgress);
  858. }
  859. //---------------------------------------------------------------------------
  860. void __fastcall TCustomScpExplorerForm::SetQueueProgress()
  861. {
  862. TTerminalManager::Instance()->QueueStatusUpdated();
  863. if ((FTaskbarList != NULL) && (FProgressForm == NULL))
  864. {
  865. // Do not animate task bar button, when items are queued, but processing is disabled
  866. if ((FQueueStatus != NULL) && (FQueueStatus->ActivePrimaryCount > 0))
  867. {
  868. if (FQueueStatus->IsOnlyOneActiveAndNoPending())
  869. {
  870. TFileOperationProgressType * ProgressData = NULL;
  871. if (FQueueStatus->Items[FQueueStatus->DoneCount] != NULL)
  872. {
  873. ProgressData = FQueueStatus->Items[FQueueStatus->DoneCount]->ProgressData;
  874. }
  875. if ((ProgressData != NULL) &&
  876. ProgressData->InProgress)
  877. {
  878. SetTaskbarListProgressValue(ProgressData);
  879. }
  880. else
  881. {
  882. SetTaskbarListProgressState(TBPF_NOPROGRESS);
  883. }
  884. }
  885. else
  886. {
  887. SetTaskbarListProgressState(TBPF_INDETERMINATE);
  888. }
  889. }
  890. else
  891. {
  892. SetTaskbarListProgressState(TBPF_NOPROGRESS);
  893. }
  894. }
  895. UpdateQueueLabel();
  896. }
  897. //---------------------------------------------------------------------------
  898. void __fastcall TCustomScpExplorerForm::UpdateQueueLabel()
  899. {
  900. UnicodeString Caption = LoadStr(QUEUE_CAPTION);
  901. if (FQueueStatus != NULL)
  902. {
  903. int ActiveAndPendingPrimaryCount = FQueueStatus->ActiveAndPendingPrimaryCount;
  904. if (ActiveAndPendingPrimaryCount > 0)
  905. {
  906. Caption = FORMAT("%s (%d)", (Caption, ActiveAndPendingPrimaryCount));
  907. }
  908. }
  909. QueueLabel->Caption = Caption;
  910. }
  911. //---------------------------------------------------------------------------
  912. void TCustomScpExplorerForm::CheckStoreTransition()
  913. {
  914. if (WinConfiguration->StoreTransition == stInit)
  915. {
  916. if (IsUWP())
  917. {
  918. WinConfiguration->StoreTransition = stStoreFresh;
  919. AppLog(L"Fresh UWP/store installation");
  920. }
  921. else
  922. {
  923. WinConfiguration->StoreTransition = stStandard;
  924. AppLog(L"Fresh standard installation");
  925. }
  926. }
  927. else if ((WinConfiguration->StoreTransition == stStandard) && IsUWP())
  928. {
  929. WinConfiguration->StoreTransition = stStoreMigrated;
  930. WinConfiguration->FirstRun = WinConfiguration->FirstRun + L"=>" + StandardDatestamp();
  931. AppLog(L"Standard installation migrated to store installation");
  932. }
  933. if (WinConfiguration->StoreTransition == stStoreMigrated)
  934. {
  935. TTBXToolbar * Toolbar = new TTBXToolbar(MessageDock);
  936. Toolbar->Parent = MessageDock;
  937. Toolbar->FullSize = true;
  938. Toolbar->Color = Application->HintColor;
  939. TTBXLabelItem * LabelItem;
  940. LabelItem = new TTBXLabelItem(Toolbar);
  941. LabelItem->Margin = ScaleByTextHeight(MessageDock, 2); // Is this automatically rescaled?
  942. LabelItem->Caption = LoadStr(STORE_MIGRATION_THANKS);
  943. Toolbar->Items->Add(LabelItem);
  944. TTBXCustomItem * LinkItem = new TTBXItem(Toolbar);
  945. LinkItem->FontSettings->Color = LinkColor;
  946. LinkItem->Caption = LoadStr(STORE_MIGRATION_LEARN);
  947. LinkItem->OnClick = StoreTransitionLinkClick;
  948. Toolbar->Items->Add(LinkItem);
  949. AddMenuSeparator(Toolbar->Items);
  950. TTBCustomItem * CloseItem = new TTBXItem(Toolbar);
  951. CloseItem->Caption = LoadStr(MESSAGE_DISMISS);
  952. CloseItem->OnClick = StoreTransitionCloseClick;
  953. Toolbar->Items->Add(CloseItem);
  954. GiveTBItemPriority(CloseItem);
  955. }
  956. }
  957. //---------------------------------------------------------------------------
  958. void __fastcall TCustomScpExplorerForm::StoreTransitionLinkClick(TObject *)
  959. {
  960. ShowHelp(HELP_STORE_TRANSITION);
  961. }
  962. //---------------------------------------------------------------------------
  963. void __fastcall TCustomScpExplorerForm::StoreTransitionCloseClick(TObject * Sender)
  964. {
  965. TTBCustomItem * Item = DebugNotNull(dynamic_cast<TTBCustomItem *>(Sender));
  966. TTBXToolbar * Toolbar = DebugNotNull(dynamic_cast<TTBXToolbar *>(DebugNotNull(DebugNotNull(Item->Parent)->ParentComponent)));
  967. delete Toolbar;
  968. WinConfiguration->StoreTransition = stStoreAcknowledged;
  969. UpdateControls();
  970. }
  971. //---------------------------------------------------------------------------
  972. void __fastcall TCustomScpExplorerForm::UpdateTransferList()
  973. {
  974. TTBXStringList * TransferList = dynamic_cast<TTBXStringList*>(
  975. static_cast<TComponent*>(GetComponent(fcTransferList)));
  976. TTBXDropDownItem * TransferDropDown = dynamic_cast<TTBXDropDownItem*>(
  977. static_cast<TComponent*>(GetComponent(fcTransferDropDown)));
  978. TransferList->Strings->BeginUpdate();
  979. try
  980. {
  981. TransferList->Strings->Assign(GUIConfiguration->CopyParamList->NameList);
  982. TransferList->Strings->Insert(0, StripHotkey(LoadStr(COPY_PARAM_DEFAULT)));
  983. TransferList->ItemIndex = GUIConfiguration->CopyParamIndex + 1;
  984. if (FTransferDropDownHint.IsEmpty())
  985. {
  986. FTransferDropDownHint = TransferDropDown->Hint;
  987. }
  988. // this way we get name for "default" settings (COPY_PARAM_DEFAULT)
  989. UnicodeString Name = TransferList->Strings->Strings[TransferList->ItemIndex];
  990. TransferDropDown->Text = StripHotkey(Name);
  991. TransferDropDown->Hint = FORMAT(L"%s|%s:\n%s",
  992. (FTransferDropDownHint, StripHotkey(Name),
  993. GUIConfiguration->CurrentCopyParam.GetInfoStr(L"; ",
  994. FLAGMASK(HasActiveTerminal(), Terminal->UsableCopyParamAttrs(0).General))));
  995. // update the label, otherwise when it is updated only on the first draw
  996. // of the list, it is drawn "bold" for some reason
  997. FTransferListHoverIndex = TransferList->ItemIndex;
  998. UpdateTransferLabel();
  999. }
  1000. __finally
  1001. {
  1002. TransferList->Strings->EndUpdate();
  1003. }
  1004. }
  1005. //---------------------------------------------------------------------------
  1006. void __fastcall TCustomScpExplorerForm::UpdateCustomCommandsToolbar()
  1007. {
  1008. TTBXToolbar * Toolbar = dynamic_cast<TTBXToolbar *>(GetComponent(fcCustomCommandsBand));
  1009. DebugAssert(Toolbar != NULL);
  1010. NonVisualDataModule->UpdateCustomCommandsToolbar(Toolbar);
  1011. }
  1012. //---------------------------------------------------------------------------
  1013. void __fastcall TCustomScpExplorerForm::UpdateActions()
  1014. {
  1015. TForm::UpdateActions();
  1016. if (ComponentVisible[fcCustomCommandsBand])
  1017. {
  1018. UpdateCustomCommandsToolbar();
  1019. }
  1020. }
  1021. //---------------------------------------------------------------------------
  1022. void __fastcall TCustomScpExplorerForm::UpdateSessionsPageControlHeight()
  1023. {
  1024. SessionsPageControl->Height = SessionsPageControl->GetTabsHeight();
  1025. }
  1026. //---------------------------------------------------------------------------
  1027. void __fastcall TCustomScpExplorerForm::UpdateRowSelect(TCustomDirView * DirView)
  1028. {
  1029. if (DirView->RowSelect != WinConfiguration->FullRowSelect)
  1030. {
  1031. DirView->RowSelect = WinConfiguration->FullRowSelect;
  1032. // Without this, the panel turns gray and unused part of header turns black
  1033. DirView->Perform(CM_COLORCHANGED, 0, 0);
  1034. }
  1035. }
  1036. //---------------------------------------------------------------------------
  1037. void __fastcall TCustomScpExplorerForm::ConfigurationChanged()
  1038. {
  1039. Color = GetBtnFaceColor();
  1040. DebugAssert(Configuration && RemoteDirView);
  1041. RemoteDirView->DDAllowMove = !WinConfiguration->DDDisableMove;
  1042. RemoteDirView->DimmHiddenFiles = WinConfiguration->DimmHiddenFiles;
  1043. RemoteDirView->ShowHiddenFiles = WinConfiguration->ShowHiddenFiles;
  1044. RemoteDirView->FormatSizeBytes = WinConfiguration->FormatSizeBytes;
  1045. RemoteDirView->ShowInaccesibleDirectories = WinConfiguration->ShowInaccesibleDirectories;
  1046. RemoteDirView->NaturalOrderNumericalSorting = WinConfiguration->NaturalOrderNumericalSorting;
  1047. RemoteDirView->AlwaysSortDirectoriesByName = WinConfiguration->AlwaysSortDirectoriesByName;
  1048. UpdateRowSelect(RemoteDirView);
  1049. RemoteDriveView->DDAllowMove = !WinConfiguration->DDDisableMove;
  1050. RemoteDriveView->DimmHiddenDirs = WinConfiguration->DimmHiddenFiles;
  1051. RemoteDriveView->ShowHiddenDirs = WinConfiguration->ShowHiddenFiles;
  1052. RemoteDriveView->ShowInaccesibleDirectories = WinConfiguration->ShowInaccesibleDirectories;
  1053. RemoteDriveView->NaturalOrderNumericalSorting = WinConfiguration->NaturalOrderNumericalSorting;
  1054. UpdateSessionsPageControlHeight();
  1055. SetDockAllowDrag(!WinConfiguration->LockToolbars);
  1056. UpdateToolbarDisplayMode();
  1057. UpdateTransferList();
  1058. if (Terminal != NULL)
  1059. {
  1060. TransferPresetAutoSelect();
  1061. }
  1062. UpdateQueueView();
  1063. UpdateControls();
  1064. // this can be called even before constuctor finishes.
  1065. if (FEditorManager != NULL)
  1066. {
  1067. FEditorManager->ProcessFiles(FileConfigurationChanged, NULL);
  1068. }
  1069. // this can be called even before constuctor finishes.
  1070. if (FLocalEditors != NULL)
  1071. {
  1072. for (int Index = 0; Index < FLocalEditors->Count; Index++)
  1073. {
  1074. ReconfigureEditorForm(static_cast<TForm *>(FLocalEditors->Items[Index]));
  1075. }
  1076. }
  1077. if (ComponentVisible[fcCustomCommandsBand])
  1078. {
  1079. UpdateCustomCommandsToolbar();
  1080. }
  1081. // show only when keeping queue items forever,
  1082. // otherwise, it's enough to have it in the context menu
  1083. QueueDeleteAllDoneQueueToolbarItem->Visible =
  1084. WinConfiguration->QueueKeepDoneItems && (WinConfiguration->QueueKeepDoneItemsFor < 0);
  1085. if (FFileColorsCurrent != WinConfiguration->FileColors)
  1086. {
  1087. TFileColorData::LoadList(WinConfiguration->FileColors, FFileColors);
  1088. FFileColorsCurrent = WinConfiguration->FileColors;
  1089. FileColorsChanged();
  1090. }
  1091. }
  1092. //---------------------------------------------------------------------------
  1093. void __fastcall TCustomScpExplorerForm::DoFileColorsChanged(TCustomDirView * DirView)
  1094. {
  1095. DirView->OnGetItemColor = FFileColors.empty() ? TDirViewGetItemColorEvent(NULL) : TDirViewGetItemColorEvent(&DirViewGetItemColor);
  1096. DirView->Invalidate();
  1097. }
  1098. //---------------------------------------------------------------------------
  1099. void __fastcall TCustomScpExplorerForm::FileColorsChanged()
  1100. {
  1101. DoFileColorsChanged(RemoteDirView);
  1102. }
  1103. //---------------------------------------------------------------------------
  1104. void __fastcall TCustomScpExplorerForm::FileConfigurationChanged(
  1105. const UnicodeString FileName, TEditedFileData * /*Data*/, TObject * Token,
  1106. void * /*Arg*/)
  1107. {
  1108. if (Token != NULL)
  1109. {
  1110. TForm * Editor = dynamic_cast<TForm*>(Token);
  1111. DebugAssert(Editor != NULL);
  1112. ReconfigureEditorForm(Editor);
  1113. }
  1114. }
  1115. //---------------------------------------------------------------------------
  1116. void __fastcall TCustomScpExplorerForm::EnableDDTransferConfirmation(TObject * /*Sender*/)
  1117. {
  1118. WinConfiguration->DDTransferConfirmation = asOn;
  1119. }
  1120. //---------------------------------------------------------------------------
  1121. void __fastcall TCustomScpExplorerForm::CopyParamDialogAfter(
  1122. TTransferDirection /*Direction*/, bool /*Temp*/, const UnicodeString & /*TargetDirectory*/)
  1123. {
  1124. // noop
  1125. }
  1126. //---------------------------------------------------------------------------
  1127. bool TCustomScpExplorerForm::GetDoNotShowCopyDialogDefault(bool DragDrop)
  1128. {
  1129. return DragDrop && (WinConfiguration->DDTransferConfirmation == asAuto);
  1130. }
  1131. //---------------------------------------------------------------------------
  1132. void TCustomScpExplorerForm::HandleDoNotShowCopyDialogAgain(bool DragDrop, bool DoNotShowAgain)
  1133. {
  1134. if (DoNotShowAgain)
  1135. {
  1136. if (DragDrop)
  1137. {
  1138. if (WinConfiguration->DDTransferConfirmation == asAuto)
  1139. {
  1140. PopupTrayBalloon(
  1141. NULL, LoadStr(DD_TRANSFER_CONFIRM_OFF2), qtInformation, NULL, 0, EnableDDTransferConfirmation, NULL);
  1142. }
  1143. WinConfiguration->DDTransferConfirmation = asOff;
  1144. }
  1145. else
  1146. {
  1147. WinConfiguration->ConfirmTransferring = false;
  1148. }
  1149. }
  1150. else
  1151. {
  1152. // User explicitly unchecked "do not show again",
  1153. // so show him the dialog the next time
  1154. if (DragDrop && (WinConfiguration->DDTransferConfirmation == asAuto))
  1155. {
  1156. WinConfiguration->DDTransferConfirmation = asOn;
  1157. }
  1158. }
  1159. }
  1160. //---------------------------------------------------------------------------
  1161. bool __fastcall TCustomScpExplorerForm::CopyParamDialog(
  1162. TTransferDirection Direction, TTransferType Type, bool Temp,
  1163. TStrings * FileList, UnicodeString & TargetDirectory, TGUICopyParamType & CopyParam,
  1164. bool Confirm, bool DragDrop, int Options)
  1165. {
  1166. bool Result = true;
  1167. DebugAssert(HasActiveTerminal());
  1168. // these parameters are known in advance
  1169. int Params =
  1170. FLAGMASK(Type == ttMove, cpDelete);
  1171. bool ToTemp = (Temp && (Direction == tdToLocal));
  1172. if (Result && Confirm && WinConfiguration->ConfirmTransferring)
  1173. {
  1174. bool DisableNewerOnly =
  1175. (!Terminal->IsCapable[fcNewerOnlyUpload] && (Direction == tdToRemote)) ||
  1176. ToTemp;
  1177. Options |=
  1178. FLAGMASK(ToTemp, coTemp) |
  1179. FLAGMASK(!Terminal->IsCapable[fcBackgroundTransfers], coDisableQueue) |
  1180. coDoNotShowAgain;
  1181. TUsableCopyParamAttrs UsableCopyParamAttrs = Terminal->UsableCopyParamAttrs(Params);
  1182. int CopyParamAttrs = (Direction == tdToRemote ?
  1183. UsableCopyParamAttrs.Upload : UsableCopyParamAttrs.Download) |
  1184. FLAGMASK(DisableNewerOnly, cpaNoNewerOnly);
  1185. int OutputOptions = FLAGMASK(GetDoNotShowCopyDialogDefault(DragDrop), cooDoNotShowAgain);
  1186. std::unique_ptr<TSessionData> SessionData(SessionDataForCode());
  1187. FlashOnBackground(); // Particularly when called from ClipboardFakeCreated
  1188. Result = DoCopyDialog(Direction == tdToRemote, Type == ttMove,
  1189. FileList, TargetDirectory, &CopyParam, Options, CopyParamAttrs, SessionData.get(), &OutputOptions, -1);
  1190. if (Result)
  1191. {
  1192. HandleDoNotShowCopyDialogAgain(DragDrop, FLAGSET(OutputOptions, cooDoNotShowAgain));
  1193. CopyParamDialogAfter(Direction, Temp, TargetDirectory);
  1194. }
  1195. }
  1196. if (Result && CopyParam.Queue && !ToTemp && Terminal->IsCapable[fcBackgroundTransfers])
  1197. {
  1198. Configuration->Usage->Inc(L"TransfersOnBackground");
  1199. // these parameter are known only after transfer dialog
  1200. Params |=
  1201. (CopyParam.QueueNoConfirmation ? cpNoConfirmation : 0);
  1202. AddQueueItem(Queue, Direction, FileList, TargetDirectory, CopyParam, Params);
  1203. Result = false;
  1204. ClearTransferSourceSelection(Direction);
  1205. }
  1206. return Result;
  1207. }
  1208. //---------------------------------------------------------------------------
  1209. void TCustomScpExplorerForm::ClearOperationSelection(TOperationSide Side)
  1210. {
  1211. if (FOnFileOperationFinished != NULL)
  1212. {
  1213. FOnFileOperationFinished(Side, UnicodeString(), true);
  1214. }
  1215. else
  1216. {
  1217. if (HasDirView[Side])
  1218. {
  1219. DirView(Side)->SelectAll(smNone);
  1220. }
  1221. }
  1222. }
  1223. //---------------------------------------------------------------------------
  1224. void __fastcall TCustomScpExplorerForm::ClearTransferSourceSelection(TTransferDirection Direction)
  1225. {
  1226. TOperationSide Side = ((Direction == tdToRemote) ? osLocal : osRemote);
  1227. ClearOperationSelection(Side);
  1228. }
  1229. //---------------------------------------------------------------------------
  1230. void __fastcall TCustomScpExplorerForm::AddQueueItem(
  1231. TTerminalQueue * Queue, TTransferDirection Direction, TStrings * FileList,
  1232. const UnicodeString TargetDirectory, TGUICopyParamType & CopyParam,
  1233. int Params)
  1234. {
  1235. DebugAssert(Queue != NULL);
  1236. bool SingleFile = false;
  1237. if (FileList->Count == 1)
  1238. {
  1239. if (Direction == tdToRemote)
  1240. {
  1241. UnicodeString FileName = FileList->Strings[0];
  1242. SingleFile = FileExists(ApiPath(FileName));
  1243. }
  1244. else
  1245. {
  1246. TRemoteFile * File = static_cast<TRemoteFile *>(FileList->Objects[0]);
  1247. SingleFile = !File->IsDirectory;
  1248. }
  1249. }
  1250. TQueueItem * QueueItem;
  1251. if (Direction == tdToRemote)
  1252. {
  1253. CopyParam.IncludeFileMask.SetRoots(FileList, TargetDirectory);
  1254. QueueItem = new TUploadQueueItem(Terminal, FileList, TargetDirectory,
  1255. &CopyParam, Params, SingleFile, CopyParam.QueueParallel);
  1256. }
  1257. else
  1258. {
  1259. CopyParam.IncludeFileMask.SetRoots(TargetDirectory, FileList);
  1260. QueueItem = new TDownloadQueueItem(Terminal, FileList, TargetDirectory,
  1261. &CopyParam, Params, SingleFile, CopyParam.QueueParallel);
  1262. }
  1263. AddQueueItem(Queue, QueueItem, Terminal);
  1264. }
  1265. //---------------------------------------------------------------------------
  1266. void __fastcall TCustomScpExplorerForm::AddQueueItem(TTerminalQueue * Queue, TQueueItem * QueueItem, TManagedTerminal * Terminal)
  1267. {
  1268. if (Queue->IsEmpty)
  1269. {
  1270. Terminal->QueueOperationStart = Now();
  1271. }
  1272. Queue->AddItem(QueueItem);
  1273. }
  1274. //---------------------------------------------------------------------------
  1275. void __fastcall TCustomScpExplorerForm::RestoreFormParams()
  1276. {
  1277. }
  1278. //---------------------------------------------------------------------------
  1279. void __fastcall TCustomScpExplorerForm::InitControls()
  1280. {
  1281. DebugAssert(FDocks != NULL);
  1282. for (int Index = 0; Index < ComponentCount; Index++)
  1283. {
  1284. TTBDock * Dock = dynamic_cast<TTBDock *>(Components[Index]);
  1285. if ((Dock != NULL) && (Dock->Tag == 0))
  1286. {
  1287. FDocks->Add(Dock);
  1288. }
  1289. }
  1290. CollectItemsWithTextDisplayMode(this);
  1291. }
  1292. //---------------------------------------------------------------------------
  1293. void __fastcall TCustomScpExplorerForm::RestoreParams()
  1294. {
  1295. ConfigurationChanged();
  1296. QueuePanel->Height =
  1297. LoadDimension(WinConfiguration->QueueView.Height, WinConfiguration->QueueView.HeightPixelsPerInch, this);
  1298. LoadListViewStr(QueueView3, WinConfiguration->QueueView.Layout);
  1299. QueueDock->Visible = WinConfiguration->QueueView.ToolBar;
  1300. QueueLabel->Visible = WinConfiguration->QueueView.Label;
  1301. QueueFileList->Visible = WinConfiguration->QueueView.FileList;
  1302. QueueFileList->Height =
  1303. LoadDimension(WinConfiguration->QueueView.FileListHeight, WinConfiguration->QueueView.FileListHeightPixelsPerInch, this);
  1304. if (QueueFileList->Visible)
  1305. {
  1306. AdjustQueueLayout();
  1307. }
  1308. UpdateCustomCommandsToolbar();
  1309. }
  1310. //---------------------------------------------------------------------------
  1311. void __fastcall TCustomScpExplorerForm::StoreParams()
  1312. {
  1313. int APixelsPerInch = GetControlPixelsPerInch(this);
  1314. WinConfiguration->QueueView.Height = QueuePanel->Height;
  1315. WinConfiguration->QueueView.HeightPixelsPerInch = APixelsPerInch;
  1316. WinConfiguration->QueueView.Layout = GetListViewStr(QueueView3);
  1317. WinConfiguration->QueueView.ToolBar = QueueDock->Visible;
  1318. WinConfiguration->QueueView.Label = QueueLabel->Visible;
  1319. WinConfiguration->QueueView.FileList = QueueFileList->Visible;
  1320. WinConfiguration->QueueView.FileListHeight = QueueFileList->Height;
  1321. WinConfiguration->QueueView.FileListHeightPixelsPerInch = APixelsPerInch;
  1322. }
  1323. //---------------------------------------------------------------------------
  1324. void __fastcall TCustomScpExplorerForm::CreateParams(TCreateParams & Params)
  1325. {
  1326. if (!FFormRestored)
  1327. {
  1328. FFormRestored = true;
  1329. RestoreFormParams();
  1330. }
  1331. TForm::CreateParams(Params);
  1332. }
  1333. //---------------------------------------------------------------------------
  1334. void __fastcall TCustomScpExplorerForm::SetDockAllowDrag(bool value)
  1335. {
  1336. DebugAssert(FDocks != NULL);
  1337. for (int Index = 0; Index < FDocks->Count; Index++)
  1338. {
  1339. static_cast<TTBDock*>(FDocks->Items[Index])->AllowDrag = value;
  1340. }
  1341. }
  1342. //---------------------------------------------------------------------------
  1343. void __fastcall TCustomScpExplorerForm::LoadToolbarsLayoutStr(UnicodeString LayoutStr, UnicodeString ButtonsStr)
  1344. {
  1345. SetDockAllowDrag(true);
  1346. while (!ButtonsStr.IsEmpty())
  1347. {
  1348. UnicodeString S = CutToChar(ButtonsStr, L';', true);
  1349. UnicodeString Name = CutToChar(S, L'=', true);
  1350. for (int ToolbarIndex = 0; ToolbarIndex < ComponentCount; ToolbarIndex++)
  1351. {
  1352. TTBCustomToolbar * Toolbar = dynamic_cast<TTBCustomToolbar *>(Components[ToolbarIndex]);
  1353. if ((Toolbar != NULL) && SameText(Name, GetToolbarKey(Toolbar->Name)))
  1354. {
  1355. while (!S.IsEmpty())
  1356. {
  1357. UnicodeString S2 = CutToChar(S, L',', true);
  1358. UnicodeString Name = CutToChar(S2, L':', true);
  1359. for (int ItemIndex = 0; ItemIndex < Toolbar->Items->Count; ItemIndex++)
  1360. {
  1361. TTBCustomItem * Item = Toolbar->Items->Items[ItemIndex];
  1362. if (SameText(GetToolbarItemName(Item), Name))
  1363. {
  1364. // forward compatibility
  1365. UnicodeString S3 = CutToChar(S2, L':', true);
  1366. bool Visible = StrToIntDef(S3, 0);
  1367. Item->Visible = Visible;
  1368. }
  1369. }
  1370. }
  1371. }
  1372. }
  1373. }
  1374. ::LoadToolbarsLayoutStr(this, LayoutStr);
  1375. SetDockAllowDrag(!WinConfiguration->LockToolbars);
  1376. }
  1377. //---------------------------------------------------------------------------
  1378. UnicodeString __fastcall TCustomScpExplorerForm::GetToolbarsLayoutStr()
  1379. {
  1380. UnicodeString Result;
  1381. SetDockAllowDrag(true);
  1382. Result = ::GetToolbarsLayoutStr(this);
  1383. SetDockAllowDrag(!WinConfiguration->LockToolbars);
  1384. return Result;
  1385. }
  1386. //---------------------------------------------------------------------------
  1387. UnicodeString __fastcall TCustomScpExplorerForm::GetToolbarItemName(TTBCustomItem * Item)
  1388. {
  1389. UnicodeString Result;
  1390. if (Item->Action != NULL)
  1391. {
  1392. Result = Item->Action->Name;
  1393. Result = RemoveSuffix(Result, L"Action", true);
  1394. }
  1395. else
  1396. {
  1397. Result = Item->Name;
  1398. Result = RemoveSuffix(Result, L"SubmenuItem", true);
  1399. Result = RemoveSuffix(Result, L"Item", true);
  1400. }
  1401. return Result;
  1402. }
  1403. //---------------------------------------------------------------------------
  1404. UnicodeString __fastcall TCustomScpExplorerForm::GetToolbarsButtonsStr()
  1405. {
  1406. UnicodeString Result;
  1407. for (int ToolbarIndex = 0; ToolbarIndex < ComponentCount; ToolbarIndex++)
  1408. {
  1409. UnicodeString ToolbarStr;
  1410. TTBCustomToolbar * Toolbar = dynamic_cast<TTBCustomToolbar *>(Components[ToolbarIndex]);
  1411. if ((Toolbar != NULL) && (Toolbar != QueueToolbar))
  1412. {
  1413. for (int ItemIndex = 0; ItemIndex < Toolbar->Items->Count; ItemIndex++)
  1414. {
  1415. TTBCustomItem * Item = Toolbar->Items->Items[ItemIndex];
  1416. // Currently all buttons are visible by default, so we can safely remember all hidden buttons.
  1417. // Once we introduce any buttons that are hidden by default, we would have to remember their initial state
  1418. // and save the changes here only.
  1419. if (NonVisualDataModule->IsCustomizableToolbarItem(Item) && !Item->Visible)
  1420. {
  1421. UnicodeString Name = GetToolbarItemName(Item);
  1422. DebugAssert(Name.Pos(L"TBX") == 0);
  1423. AddToList(ToolbarStr, FORMAT(L"%s:0", (Name)), L",");
  1424. }
  1425. }
  1426. }
  1427. if (!ToolbarStr.IsEmpty())
  1428. {
  1429. AddToList(Result, FORMAT(L"%s=%s", (GetToolbarKey(Toolbar->Name), ToolbarStr)), L";");
  1430. }
  1431. }
  1432. return Result;
  1433. }
  1434. //---------------------------------------------------------------------------
  1435. void __fastcall TCustomScpExplorerForm::CreateProgressForm(TSynchronizeProgress * SynchronizeProgress)
  1436. {
  1437. DebugAssert(FProgressForm == NULL);
  1438. bool AllowSkip = HasActiveTerminal() ? Terminal->IsCapable[fcSkipTransfer] : false;
  1439. bool AllowMoveToQueue = (FTransferResumeList != NULL) || (FDeletedFiles != NULL);
  1440. FProgressForm = new TProgressForm(Application, AllowMoveToQueue, AllowSkip, SynchronizeProgress);
  1441. FProgressForm->DeleteLocalToRecycleBin =
  1442. (WinConfiguration->DeleteToRecycleBin != FAlternativeDelete);
  1443. FProgressForm->DeleteRemoteToRecycleBin =
  1444. HasActiveTerminal() &&
  1445. (Terminal->SessionData->DeleteToRecycleBin != FAlternativeDelete) &&
  1446. !Terminal->SessionData->RecycleBinPath.IsEmpty();
  1447. // As progress window has delayed show now, we need to lock ourselves, (at least) until then
  1448. LockWindow();
  1449. // Actually, do not know what hides the progress once the operation finishes
  1450. // (it is possibly the SetQueueProgress - and we should not rely on that)
  1451. SetTaskbarListProgressState(TBPF_NORMAL);
  1452. }
  1453. //---------------------------------------------------------------------------
  1454. void __fastcall TCustomScpExplorerForm::DestroyProgressForm()
  1455. {
  1456. SAFE_DESTROY(FProgressForm);
  1457. UnlockWindow();
  1458. SetQueueProgress();
  1459. }
  1460. //---------------------------------------------------------------------------
  1461. void __fastcall TCustomScpExplorerForm::FileOperationProgress(
  1462. TFileOperationProgressType & ProgressData)
  1463. {
  1464. // Download to temporary local directory
  1465. if (FPendingTempSpaceWarn && ProgressData.InProgress && ProgressData.TotalSizeSet)
  1466. {
  1467. bool Continue = true;
  1468. FPendingTempSpaceWarn = false;
  1469. DoWarnLackOfTempSpace(ProgressData.Directory, ProgressData.TotalSize, Continue);
  1470. if (!Continue)
  1471. {
  1472. Abort();
  1473. }
  1474. }
  1475. // operation is being executed and we still didn't show up progress form
  1476. if (ProgressData.InProgress && !FProgressForm)
  1477. {
  1478. CreateProgressForm(NULL);
  1479. DebugAssert(FProgressForm->OnceDoneOperation == odoIdle);
  1480. FProgressForm->OnceDoneOperation = ProgressData.InitialOnceDoneOperation;
  1481. }
  1482. // operation is finished (or terminated), so we hide progress form
  1483. else if (!ProgressData.InProgress && (FProgressForm != NULL) && (FProgressForm->SynchronizeProgress == NULL))
  1484. {
  1485. DestroyProgressForm();
  1486. if (ProgressData.Operation == foCalculateSize)
  1487. {
  1488. // When calculating size before transfer, the abort caused by
  1489. // cancel flag set due to "MoveToQueue" below,
  1490. // is not propagated back to us in ExecuteFileOperation,
  1491. // as it is silently swallowed.
  1492. // So we explicitly (re)throw it here.
  1493. if (FMoveToQueue)
  1494. {
  1495. Abort();
  1496. }
  1497. }
  1498. else
  1499. {
  1500. if ((ProgressData.Cancel == csContinue) &&
  1501. !FAutoOperation)
  1502. {
  1503. OperationComplete(ProgressData.StartTime);
  1504. }
  1505. }
  1506. }
  1507. if (FProgressForm &&
  1508. // Can happen only during synchronization, when ending one of the operations
  1509. ProgressData.InProgress)
  1510. {
  1511. FProgressForm->SetProgressData(ProgressData);
  1512. DebugAssert(ProgressData.InProgress);
  1513. SetTaskbarListProgressValue(&ProgressData);
  1514. if (FProgressForm->Cancel > csContinue)
  1515. {
  1516. ProgressData.SetCancelAtLeast(FProgressForm->Cancel);
  1517. if (FProgressForm->Cancel == csCancelFile)
  1518. {
  1519. FProgressForm->ClearCancel();
  1520. }
  1521. // cancel cancels even the move
  1522. FMoveToQueue = false;
  1523. }
  1524. else if (FProgressForm->MoveToQueue)
  1525. {
  1526. FMoveToQueue = true;
  1527. ProgressData.SetCancelAtLeast(csCancel);
  1528. }
  1529. if ((FTransferResumeList != NULL) &&
  1530. ProgressData.InProgress &&
  1531. ProgressData.IsTransfer() &&
  1532. !ProgressData.FullFileName.IsEmpty())
  1533. {
  1534. if ((FTransferResumeList->Count == 0) ||
  1535. (FTransferResumeList->Strings[FTransferResumeList->Count - 1] != ProgressData.FullFileName))
  1536. {
  1537. // note that we do not recognize directories from files here
  1538. FTransferResumeList->Add(ProgressData.FullFileName);
  1539. }
  1540. }
  1541. }
  1542. }
  1543. //---------------------------------------------------------------------------
  1544. void __fastcall TCustomScpExplorerForm::OperationComplete(
  1545. const TDateTime & StartTime)
  1546. {
  1547. if (GUIConfiguration->BeepOnFinish &&
  1548. (Now() - StartTime > GUIConfiguration->BeepOnFinishAfter))
  1549. {
  1550. UnicodeString BeepSound = GUIConfiguration->BeepSound;
  1551. DWORD Sound;
  1552. if (!ExtractFileExt(BeepSound).IsEmpty())
  1553. {
  1554. Sound = SND_FILENAME;
  1555. }
  1556. else
  1557. {
  1558. Sound = SND_ALIAS;
  1559. }
  1560. PlaySound(BeepSound.c_str(), NULL, Sound | SND_ASYNC);
  1561. }
  1562. }
  1563. //---------------------------------------------------------------------------
  1564. void __fastcall TCustomScpExplorerForm::OperationProgress(
  1565. TFileOperationProgressType & ProgressData)
  1566. {
  1567. // For example, when calling TFileOperationProgressType::Suspend in TTerminal::QueryReopen,
  1568. // we cannot recurse back to TTerminal::Idle as that may detect another error, calling "suspend" again.
  1569. TAutoNestingCounter Counter(FDoNotIdleCurrentTerminal);
  1570. FileOperationProgress(ProgressData);
  1571. }
  1572. //---------------------------------------------------------------------------
  1573. UnicodeString __fastcall TCustomScpExplorerForm::GetProgressTitle()
  1574. {
  1575. UnicodeString Result;
  1576. if (FProgressForm != NULL)
  1577. {
  1578. Result = FProgressForm->ProgressStr();
  1579. }
  1580. return Result;
  1581. }
  1582. //---------------------------------------------------------------------------
  1583. bool __fastcall TCustomScpExplorerForm::PanelOperation(TOperationSide /*Side*/,
  1584. bool DragDrop)
  1585. {
  1586. return (!DragDrop && (DropSourceControl == NULL)) ||
  1587. (DropSourceControl == DirView(osOther));
  1588. }
  1589. //---------------------------------------------------------------------------
  1590. TListItem * TCustomScpExplorerForm::VisualiseOperationFinished(TOperationSide Side, const UnicodeString & FileName, bool Unselect)
  1591. {
  1592. TCustomDirView * ADirView = DirView(Side);
  1593. UnicodeString FileNameOnly = ExtractFileName(FileName, !IsSideLocalBrowser(Side));
  1594. TListItem * Item = ADirView->FindFileItem(FileNameOnly);
  1595. // this can happen when local drive is unplugged in the middle of the operation
  1596. if (Item != NULL)
  1597. {
  1598. if (Unselect)
  1599. {
  1600. Item->Selected = false;
  1601. }
  1602. ADirView->MakeProgressVisible(Item);
  1603. }
  1604. return Item;
  1605. }
  1606. //---------------------------------------------------------------------------
  1607. void __fastcall TCustomScpExplorerForm::DoOperationFinished(
  1608. TFileOperation Operation, TOperationSide Side,
  1609. bool /*Temp*/, const UnicodeString & FileName, bool Success,
  1610. TOnceDoneOperation & OnceDoneOperation)
  1611. {
  1612. if (!FAutoOperation)
  1613. {
  1614. // no selection on "/upload", form serves only as event handler
  1615. // (it is not displayed)
  1616. if (PanelOperation(Side, FDragDropOperation) && Visible &&
  1617. ((Operation == FPrimaryOperation) ||
  1618. ((Operation != foCalculateSize) && (Operation != foGetProperties) && (Operation != foCalculateChecksum))))
  1619. {
  1620. TOperationSide DViewSide;
  1621. if ((FProgressSide != osCurrent) &&
  1622. IsLocalBrowserMode() && (Side == osLocal)) // Only to limit the impact
  1623. {
  1624. // assume the operation is over the focused panel
  1625. DViewSide = FProgressSide;
  1626. }
  1627. else
  1628. {
  1629. DViewSide = Side;
  1630. }
  1631. if (FOnFileOperationFinished != NULL)
  1632. {
  1633. FOnFileOperationFinished(DViewSide, FileName, Success);
  1634. }
  1635. else
  1636. {
  1637. VisualiseOperationFinished(DViewSide, FileName, Success);
  1638. }
  1639. }
  1640. if (TFileOperationProgressType::IsTransferOperation(Operation))
  1641. {
  1642. if (DebugAlwaysTrue(!IsLocalBrowserMode()))
  1643. {
  1644. if (Side == osLocal)
  1645. {
  1646. Configuration->Usage->Inc(L"UploadedFiles");
  1647. }
  1648. else
  1649. {
  1650. Configuration->Usage->Inc(L"DownloadedFiles");
  1651. }
  1652. }
  1653. }
  1654. }
  1655. if (Success && (FSynchronizeController != NULL))
  1656. {
  1657. DebugAssert(!IsSideLocalBrowser(osRemote));
  1658. if (Operation == foCopy)
  1659. {
  1660. DebugAssert(Side == osLocal);
  1661. FSynchronizeController->LogOperation(soUpload, FileName);
  1662. }
  1663. else if (Operation == foDelete)
  1664. {
  1665. DebugAssert(Side == osRemote);
  1666. FSynchronizeController->LogOperation(soDelete, FileName);
  1667. }
  1668. }
  1669. if (FProgressForm)
  1670. {
  1671. OnceDoneOperation = FProgressForm->OnceDoneOperation;
  1672. }
  1673. }
  1674. //---------------------------------------------------------------------------
  1675. void __fastcall TCustomScpExplorerForm::OperationFinished(
  1676. TFileOperation Operation, TOperationSide Side,
  1677. bool Temp, const UnicodeString & FileName, Boolean Success,
  1678. TOnceDoneOperation & OnceDoneOperation)
  1679. {
  1680. TAutoNestingCounter Counter(FDoNotIdleCurrentTerminal);
  1681. DoOperationFinished(Operation, Side, Temp, FileName, Success,
  1682. OnceDoneOperation);
  1683. }
  1684. //---------------------------------------------------------------------------
  1685. bool TCustomScpExplorerForm::IsLocalBrowserMode()
  1686. {
  1687. return false;
  1688. }
  1689. //---------------------------------------------------------------------------
  1690. bool TCustomScpExplorerForm::SupportsLocalBrowser()
  1691. {
  1692. return false;
  1693. }
  1694. //---------------------------------------------------------------------------
  1695. bool TCustomScpExplorerForm::IsSideLocalBrowser(TOperationSide)
  1696. {
  1697. return false;
  1698. }
  1699. //---------------------------------------------------------------------------
  1700. TCustomDirView * TCustomScpExplorerForm::GetCurrentLocalBrowser()
  1701. {
  1702. return NULL;
  1703. }
  1704. //---------------------------------------------------------------------------
  1705. TCustomDirView * __fastcall TCustomScpExplorerForm::DirView(TOperationSide Side)
  1706. {
  1707. DebugAssert(GetSide(Side) == osRemote);
  1708. DebugUsedParam(Side);
  1709. return RemoteDirView;
  1710. }
  1711. //---------------------------------------------------------------------------
  1712. TCustomDriveView * __fastcall TCustomScpExplorerForm::DriveView(TOperationSide Side)
  1713. {
  1714. DebugAssert(GetSide(Side) == osRemote);
  1715. DebugUsedParam(Side);
  1716. return RemoteDriveView;
  1717. }
  1718. //---------------------------------------------------------------------------
  1719. bool __fastcall TCustomScpExplorerForm::DirViewEnabled(TOperationSide Side)
  1720. {
  1721. DebugAssert(GetSide(Side) == osRemote);
  1722. DebugUsedParam(Side);
  1723. return HasActiveTerminal();
  1724. }
  1725. //---------------------------------------------------------------------------
  1726. bool __fastcall TCustomScpExplorerForm::GetEnableFocusedOperation(
  1727. TOperationSide Side, int FilesOnly)
  1728. {
  1729. return DirView(Side)->AnyFileSelected(true, (FilesOnly != 0), true);
  1730. }
  1731. //---------------------------------------------------------------------------
  1732. bool __fastcall TCustomScpExplorerForm::GetEnableSelectedOperation(
  1733. TOperationSide Side, int FilesOnly)
  1734. {
  1735. return DirView(Side)->AnyFileSelected(false, (FilesOnly != 0), true);
  1736. }
  1737. //---------------------------------------------------------------------------
  1738. struct THistoryItemData
  1739. {
  1740. short int Side;
  1741. short int Index;
  1742. };
  1743. //---------------------------------------------------------------------------
  1744. void __fastcall TCustomScpExplorerForm::HistoryGo(TOperationSide Side, int Index)
  1745. {
  1746. DirView(Side)->HistoryGo(Index);
  1747. }
  1748. //---------------------------------------------------------------------------
  1749. void __fastcall TCustomScpExplorerForm::HistoryItemClick(System::TObject* Sender)
  1750. {
  1751. TTBCustomItem * Item = dynamic_cast<TTBCustomItem *>(Sender);
  1752. THistoryItemData Data = *reinterpret_cast<THistoryItemData*>(&(Item->Tag));
  1753. HistoryGo((TOperationSide)Data.Side, Data.Index);
  1754. }
  1755. //---------------------------------------------------------------------------
  1756. void __fastcall TCustomScpExplorerForm::UpdateHistoryMenu(TOperationSide Side,
  1757. bool Back)
  1758. {
  1759. if (FHistoryMenu[Side == osLocal][Back] != NULL)
  1760. {
  1761. TCustomDirView * DView = DirView(Side);
  1762. TTBXPopupMenu * Menu = FHistoryMenu[Side == osLocal][Back];
  1763. int ICount = Back ? DView->BackCount : DView->ForwardCount;
  1764. if (ICount > 10)
  1765. {
  1766. ICount = 10;
  1767. }
  1768. Menu->Items->Clear();
  1769. THistoryItemData Data;
  1770. Data.Side = Side;
  1771. for (short int i = 1; i <= ICount; i++)
  1772. {
  1773. TTBCustomItem * Item = new TTBXItem(Menu);
  1774. Data.Index = static_cast<short int>(i * (Back ? -1 : 1));
  1775. Item->Caption = MinimizeName(DView->HistoryPath[Data.Index], 50, !IsSideLocalBrowser(Side));
  1776. Item->Hint = DView->HistoryPath[Data.Index];
  1777. DebugAssert(sizeof(int) == sizeof(THistoryItemData));
  1778. Item->Tag = *reinterpret_cast<int*>(&Data);
  1779. Item->OnClick = HistoryItemClick;
  1780. Menu->Items->Add(Item);
  1781. }
  1782. }
  1783. }
  1784. //---------------------------------------------------------------------------
  1785. TTBXPopupMenu * __fastcall TCustomScpExplorerForm::HistoryMenu(
  1786. TOperationSide Side, bool Back)
  1787. {
  1788. if (FHistoryMenu[Side == osLocal][Back] == NULL)
  1789. {
  1790. // workaround
  1791. // In Pascal the size of TTBXPopupMenu is 132, in C++ 136,
  1792. // operator new allocates memory in Pascal code, but calls inline
  1793. // contructor in C++, leading in problems, the function does
  1794. // both in Pascal code
  1795. FHistoryMenu[Side == osLocal][Back] = CreateTBXPopupMenu(this);
  1796. UpdateHistoryMenu(Side, Back);
  1797. }
  1798. return FHistoryMenu[Side == osLocal][Back];
  1799. }
  1800. //---------------------------------------------------------------------------
  1801. void __fastcall TCustomScpExplorerForm::DirViewHistoryChange(
  1802. TCustomDirView *Sender)
  1803. {
  1804. TOperationSide Side = (Sender == DirView(osOther) ? osOther : osLocal);
  1805. UpdateHistoryMenu(Side, true);
  1806. UpdateHistoryMenu(Side, false);
  1807. }
  1808. //---------------------------------------------------------------------------
  1809. bool __fastcall TCustomScpExplorerForm::CustomCommandRemoteAllowed()
  1810. {
  1811. // remote custom commands can be executed only if the server supports shell commands
  1812. // or have secondary shell
  1813. return HasActiveTerminal() && (Terminal->IsCapable[fcSecondaryShell] || Terminal->IsCapable[fcShellAnyCommand]);
  1814. }
  1815. //---------------------------------------------------------------------------
  1816. int __fastcall TCustomScpExplorerForm::CustomCommandState(
  1817. const TCustomCommandType & Command, bool OnFocused, TCustomCommandListType ListType)
  1818. {
  1819. // -1 = hidden, 0 = disabled, 1 = enabled
  1820. int Result;
  1821. TFileCustomCommand RemoteCustomCommand;
  1822. TLocalCustomCommand LocalCustomCommand;
  1823. TFileCustomCommand * NonInteractiveCustomCommand =
  1824. FLAGCLEAR(Command.Params, ccLocal) ? &RemoteCustomCommand : &LocalCustomCommand;
  1825. TInteractiveCustomCommand InteractiveCustomCommand(NonInteractiveCustomCommand);
  1826. UnicodeString Cmd = InteractiveCustomCommand.Complete(Command.Command, false);
  1827. if (FLAGCLEAR(Command.Params, ccLocal))
  1828. {
  1829. int AllowedState = CustomCommandRemoteAllowed() ? 1 : 0;
  1830. // custom command that does not operate with files can be executed anytime ...
  1831. if (!NonInteractiveCustomCommand->IsFileCommand(Cmd))
  1832. {
  1833. // ... but do not show such command in remote file menu
  1834. if ((ListType == ccltAll) || (ListType == ccltNonFile))
  1835. {
  1836. Result = AllowedState;
  1837. }
  1838. else
  1839. {
  1840. Result = -1;
  1841. }
  1842. }
  1843. else
  1844. {
  1845. if ((ListType == ccltAll) || ((ListType == ccltFile) && !IsSideLocalBrowser(FCurrentSide)))
  1846. {
  1847. Result = (DirView(FCurrentSide)->AnyFileSelected(OnFocused, false, true)) ? AllowedState : 0;
  1848. }
  1849. else
  1850. {
  1851. Result = -1;
  1852. }
  1853. }
  1854. }
  1855. else
  1856. {
  1857. // custom command that does not operate with files can be executed anytime
  1858. if (!NonInteractiveCustomCommand->IsFileCommand(Cmd))
  1859. {
  1860. Result = ((ListType == ccltAll) || (ListType == ccltNonFile)) ? 1 : -1;
  1861. }
  1862. else if (LocalCustomCommand.HasLocalFileName(Cmd))
  1863. {
  1864. if ((ListType == ccltAll) || (ListType == ccltFile))
  1865. {
  1866. // special case is "diff"-style command that can be executed over any side,
  1867. // if we have both sides
  1868. Result =
  1869. // Cannot have focus on both panels, so we have to call AnyFileSelected
  1870. // directly (instead of EnableSelectedOperation) to pass
  1871. // false to FocusedFileOnlyWhenFocused when panel is inactive.
  1872. ((HasDirView[osLocal] && !IsSideLocalBrowser(osRemote) &&
  1873. DirView(osLocal)->AnyFileSelected(false, false, (FCurrentSide == osLocal))) &&
  1874. DirView(osRemote)->AnyFileSelected(false, false, (FCurrentSide == osRemote))) ? 1 : 0;
  1875. }
  1876. else if (ListType == ccltBoth)
  1877. {
  1878. DebugAssert(!IsLocalBrowserMode());
  1879. Result = 1;
  1880. }
  1881. else
  1882. {
  1883. Result = -1;
  1884. }
  1885. }
  1886. else
  1887. {
  1888. if ((ListType == ccltAll) ||
  1889. ((ListType == ccltFile) && (!IsSideLocalBrowser(FCurrentSide) || FLAGCLEAR(Command.Params, ccRemoteFiles))))
  1890. {
  1891. Result = DirView(FCurrentSide)->AnyFileSelected(OnFocused, false, true) ? 1 : 0;
  1892. }
  1893. else
  1894. {
  1895. Result = -1;
  1896. }
  1897. }
  1898. if ((Result > 0) &&
  1899. LocalCustomCommand.IsSessionCommand(Cmd) &&
  1900. !HasActiveTerminal())
  1901. {
  1902. Result = 0;
  1903. }
  1904. }
  1905. return Result;
  1906. }
  1907. //---------------------------------------------------------------------------
  1908. void __fastcall TCustomScpExplorerForm::RemoteCustomCommand(
  1909. TStrings * FileList, const TCustomCommandType & ACommand,
  1910. const TCustomCommandData & Data, const UnicodeString & CommandCommand)
  1911. {
  1912. if (EnsureCommandSessionFallback(fcShellAnyCommand))
  1913. {
  1914. TRemoteCustomCommand RemoteCustomCommand(Data, Terminal->CurrentDirectory);
  1915. TWinInteractiveCustomCommand InteractiveCustomCommand(
  1916. &RemoteCustomCommand, ACommand.Name, ACommand.HomePage);
  1917. UnicodeString Command = InteractiveCustomCommand.Complete(CommandCommand, false);
  1918. Configuration->Usage->Inc(L"RemoteCustomCommandRuns2");
  1919. bool Capture =
  1920. FLAGSET(ACommand.Params, ccShowResults) ||
  1921. FLAGSET(ACommand.Params, ccCopyResults) ||
  1922. FLAGSET(ACommand.Params, ccShowResultsInMsgBox);
  1923. TCaptureOutputEvent OutputEvent = NULL;
  1924. DebugAssert(FCapturedLog == NULL);
  1925. if (Capture)
  1926. {
  1927. FCapturedLog = new TStringList();
  1928. OutputEvent = TerminalCaptureLog;
  1929. }
  1930. try
  1931. {
  1932. if (!RemoteCustomCommand.IsFileCommand(Command))
  1933. {
  1934. Terminal->AnyCommand(RemoteCustomCommand.Complete(Command, true),
  1935. OutputEvent);
  1936. }
  1937. else
  1938. {
  1939. Terminal->CustomCommandOnFiles(Command, ACommand.Params, FileList, OutputEvent);
  1940. }
  1941. if ((FCapturedLog != NULL) && (FCapturedLog->Count > 0))
  1942. {
  1943. if (FLAGSET(ACommand.Params, ccCopyResults))
  1944. {
  1945. CopyToClipboard(FCapturedLog);
  1946. }
  1947. if (FLAGSET(ACommand.Params, ccShowResults))
  1948. {
  1949. DoConsoleDialog(Terminal, L"", FCapturedLog);
  1950. }
  1951. else if (FLAGSET(ACommand.Params, ccShowResultsInMsgBox))
  1952. {
  1953. MessageDialog(FCapturedLog->Text, qtInformation, qaOK);
  1954. }
  1955. }
  1956. }
  1957. __finally
  1958. {
  1959. SAFE_DESTROY(FCapturedLog);
  1960. }
  1961. }
  1962. }
  1963. //---------------------------------------------------------------------------
  1964. void __fastcall TCustomScpExplorerForm::LocalCustomCommandPure(
  1965. TStrings * FileList, const TCustomCommandType & ACommand, const UnicodeString & Command, TStrings * ALocalFileList,
  1966. const TCustomCommandData & Data, bool LocalFileCommand, bool FileListCommand, UnicodeString * POutput)
  1967. {
  1968. DebugAssert(!IsLocalBrowserMode());
  1969. TStrings * LocalFileList = NULL;
  1970. TStrings * RemoteFileList = NULL;
  1971. TStrings * RemoteFileListFull = NULL;
  1972. try
  1973. {
  1974. if (LocalFileCommand)
  1975. {
  1976. if (ALocalFileList == NULL)
  1977. {
  1978. DebugAssert(HasDirView[osLocal]);
  1979. // Cannot have focus on both panels, so we have to call AnyFileSelected
  1980. // directly (instead of EnableSelectedOperation) to pass
  1981. // false to FocusedFileOnlyWhenFocused
  1982. DebugAssert(DirView(osLocal)->AnyFileSelected(false, false, false));
  1983. LocalFileList = DirView(osLocal)->CreateFileList(false, true, NULL);
  1984. }
  1985. else
  1986. {
  1987. LocalFileList = ALocalFileList;
  1988. }
  1989. if (FileListCommand)
  1990. {
  1991. if (LocalFileList->Count != 1)
  1992. {
  1993. throw Exception(LoadStr(CUSTOM_COMMAND_SELECTED_UNMATCH1));
  1994. }
  1995. }
  1996. else
  1997. {
  1998. if ((LocalFileList->Count != 1) &&
  1999. (FileList->Count != 1) &&
  2000. (LocalFileList->Count != FileList->Count))
  2001. {
  2002. throw Exception(LoadStr(CUSTOM_COMMAND_SELECTED_UNMATCH));
  2003. }
  2004. }
  2005. }
  2006. UnicodeString RootTempDir;
  2007. UnicodeString TempDir;
  2008. bool RemoteFiles = FLAGSET(ACommand.Params, ccRemoteFiles);
  2009. if (!RemoteFiles)
  2010. {
  2011. TemporarilyDownloadFiles(FileList, false, RootTempDir, TempDir, false, true, true);
  2012. }
  2013. try
  2014. {
  2015. TDateTimes RemoteFileTimes;
  2016. if (RemoteFiles)
  2017. {
  2018. RemoteFileList = FileList;
  2019. }
  2020. else
  2021. {
  2022. RemoteFileList = new TStringList();
  2023. TMakeLocalFileListParams MakeFileListParam;
  2024. MakeFileListParam.FileList = RemoteFileList;
  2025. MakeFileListParam.FileTimes = &RemoteFileTimes;
  2026. MakeFileListParam.IncludeDirs = FLAGSET(ACommand.Params, ccApplyToDirectories);
  2027. MakeFileListParam.Recursive =
  2028. FLAGSET(ACommand.Params, ccRecursive) && !FileListCommand;
  2029. ProcessLocalDirectory(TempDir, Terminal->MakeLocalFileList, &MakeFileListParam);
  2030. if (!MakeFileListParam.Recursive)
  2031. {
  2032. RemoteFileTimes.clear();
  2033. RemoteFileListFull = new TStringList();
  2034. MakeFileListParam.FileList = RemoteFileListFull;
  2035. MakeFileListParam.Recursive = true;
  2036. MakeFileListParam.IncludeDirs = false;
  2037. ProcessLocalDirectory(TempDir, Terminal->MakeLocalFileList, &MakeFileListParam);
  2038. }
  2039. else
  2040. {
  2041. RemoteFileListFull = RemoteFileList;
  2042. }
  2043. }
  2044. bool NonBlocking = FileListCommand && RemoteFiles && !POutput;
  2045. TFileOperationProgressType Progress(&OperationProgress, &OperationFinished);
  2046. if (!NonBlocking)
  2047. {
  2048. int Count;
  2049. if (FileListCommand)
  2050. {
  2051. Count = 1;
  2052. }
  2053. else if (LocalFileCommand)
  2054. {
  2055. Count = std::max(LocalFileList->Count, RemoteFileList->Count);
  2056. }
  2057. else
  2058. {
  2059. Count = RemoteFileList->Count;
  2060. }
  2061. Progress.Start(foCustomCommand, osRemote, Count);
  2062. DebugAssert(FProgressForm != NULL);
  2063. FProgressForm->ReadOnly = true;
  2064. }
  2065. try
  2066. {
  2067. if (FileListCommand)
  2068. {
  2069. UnicodeString LocalFile;
  2070. // MakeFileList does not delimit filenames
  2071. UnicodeString FileList = MakeFileList(RemoteFileList);
  2072. if (LocalFileCommand)
  2073. {
  2074. DebugAssert(LocalFileList->Count == 1);
  2075. LocalFile = LocalFileList->Strings[0];
  2076. }
  2077. TLocalCustomCommand CustomCommand(Data,
  2078. Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(), L"", LocalFile, FileList);
  2079. UnicodeString ShellCommand = CustomCommand.Complete(Command, true);
  2080. if (NonBlocking)
  2081. {
  2082. DebugAssert(!POutput);
  2083. ExecuteShellChecked(ShellCommand);
  2084. }
  2085. else
  2086. {
  2087. ExecuteProcessCheckedAndWait(ShellCommand, HelpKeyword, POutput);
  2088. }
  2089. }
  2090. else if (LocalFileCommand)
  2091. {
  2092. if (LocalFileList->Count == 1)
  2093. {
  2094. UnicodeString LocalFile = LocalFileList->Strings[0];
  2095. for (int Index = 0; Index < RemoteFileList->Count; Index++)
  2096. {
  2097. UnicodeString FileName = RemoteFileList->Strings[Index];
  2098. UnicodeString FileNameForProgress = RemoteFiles ? FileName : ExtractFileName(FileName);
  2099. Progress.SetFile(FileNameForProgress);
  2100. TLocalCustomCommand CustomCommand(Data,
  2101. Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(), FileName, LocalFile, L"");
  2102. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  2103. TOnceDoneOperation OnceDoneOperation;
  2104. // Do not unselect anything, as it won't work with two panels anyway
  2105. Progress.Finish(UnicodeString(), true, OnceDoneOperation);
  2106. }
  2107. }
  2108. else if (RemoteFileList->Count == 1)
  2109. {
  2110. UnicodeString FileName = RemoteFileList->Strings[0];
  2111. for (int Index = 0; Index < LocalFileList->Count; Index++)
  2112. {
  2113. UnicodeString LocalFileName = LocalFileList->Strings[Index];
  2114. UnicodeString LocalFileNameOnly = ExtractFileName(LocalFileName);
  2115. Progress.SetFile(LocalFileNameOnly);
  2116. TLocalCustomCommand CustomCommand(
  2117. Data, Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(),
  2118. FileName, LocalFileName, L"");
  2119. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  2120. TOnceDoneOperation OnceDoneOperation;
  2121. // Do not unselect anything, as it won't work with two panels anyway
  2122. Progress.Finish(UnicodeString(), true, OnceDoneOperation);
  2123. }
  2124. }
  2125. else
  2126. {
  2127. if (LocalFileList->Count != RemoteFileList->Count)
  2128. {
  2129. throw Exception(LoadStr(CUSTOM_COMMAND_PAIRS_DOWNLOAD_FAILED));
  2130. }
  2131. for (int Index = 0; Index < LocalFileList->Count; Index++)
  2132. {
  2133. UnicodeString FileName = RemoteFileList->Strings[Index];
  2134. UnicodeString LocalFileName = LocalFileList->Strings[Index];
  2135. UnicodeString LocalFileNameOnly = ExtractFileName(LocalFileName);
  2136. Progress.SetFile(LocalFileNameOnly); // sic
  2137. TLocalCustomCommand CustomCommand(
  2138. Data, Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(),
  2139. FileName, LocalFileName, L"");
  2140. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  2141. TOnceDoneOperation OnceDoneOperation;
  2142. // Do not unselect anything, as it won't work with two panels anyway
  2143. Progress.Finish(UnicodeString(), true, OnceDoneOperation);
  2144. }
  2145. }
  2146. }
  2147. else
  2148. {
  2149. for (int Index = 0; Index < RemoteFileList->Count; Index++)
  2150. {
  2151. UnicodeString FileName = RemoteFileList->Strings[Index];
  2152. UnicodeString FileNameForProgress = RemoteFiles ? FileName : ExtractFileName(FileName);
  2153. Progress.SetFile(FileNameForProgress);
  2154. TLocalCustomCommand CustomCommand(Data,
  2155. Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(),
  2156. FileName, L"", L"");
  2157. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  2158. TOnceDoneOperation OnceDoneOperation;
  2159. Progress.Finish(FileNameForProgress, true, OnceDoneOperation);
  2160. }
  2161. }
  2162. }
  2163. __finally
  2164. {
  2165. if (!NonBlocking)
  2166. {
  2167. Progress.Stop();
  2168. }
  2169. }
  2170. DebugAssert(!FAutoOperation);
  2171. if (!RemoteFiles)
  2172. {
  2173. TempDir = IncludeTrailingBackslash(TempDir);
  2174. for (int Index = 0; Index < RemoteFileListFull->Count; Index++)
  2175. {
  2176. UnicodeString FileName = RemoteFileListFull->Strings[Index];
  2177. if (DebugAlwaysTrue(SameText(TempDir, FileName.SubString(1, TempDir.Length()))) &&
  2178. // Skip directories, process only nested files.
  2179. // The check is redundant as FileAge fails for directories anyway.
  2180. !DirectoryExists(FileName))
  2181. {
  2182. UnicodeString RemoteDir =
  2183. UnixExtractFileDir(
  2184. UnixIncludeTrailingBackslash(Terminal->CurrentDirectory) +
  2185. ToUnixPath(FileName.SubString(TempDir.Length() + 1, FileName.Length() - TempDir.Length())));
  2186. TDateTime NewTime;
  2187. if (FileAge(FileName, NewTime) &&
  2188. (NewTime != RemoteFileTimes[Index]))
  2189. {
  2190. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  2191. TemporaryFileCopyParam(CopyParam);
  2192. CopyParam.FileMask = L"";
  2193. FAutoOperation = true;
  2194. std::unique_ptr<TStrings> TemporaryFilesList(new TStringList());
  2195. TemporaryFilesList->Add(FileName);
  2196. Terminal->CopyToRemote(TemporaryFilesList.get(), RemoteDir, &CopyParam, cpTemporary, NULL);
  2197. }
  2198. }
  2199. }
  2200. }
  2201. }
  2202. __finally
  2203. {
  2204. FAutoOperation = false;
  2205. if (!RootTempDir.IsEmpty() && DebugAlwaysTrue(!RemoteFiles))
  2206. {
  2207. RecursiveDeleteFile(ExcludeTrailingBackslash(RootTempDir), false);
  2208. }
  2209. }
  2210. }
  2211. __finally
  2212. {
  2213. if (RemoteFileList != FileList)
  2214. {
  2215. delete RemoteFileList;
  2216. }
  2217. if (LocalFileList != ALocalFileList)
  2218. {
  2219. delete LocalFileList;
  2220. }
  2221. if (RemoteFileListFull != RemoteFileList)
  2222. {
  2223. delete RemoteFileListFull;
  2224. }
  2225. }
  2226. }
  2227. //---------------------------------------------------------------------------
  2228. void __fastcall TCustomScpExplorerForm::LocalCustomCommandWithLocalFiles(
  2229. const TCustomCommandType & ACommand, const UnicodeString & Command, const TCustomCommandData & Data,
  2230. bool FileListCommand, UnicodeString * POutput)
  2231. {
  2232. std::unique_ptr<TStrings> SelectedFileList(DirView(osCurrent)->CreateFileList(false, true, NULL));
  2233. std::unique_ptr<TStrings> LocalFileList(new TStringList());
  2234. for (int Index = 0; Index < SelectedFileList->Count; Index++)
  2235. {
  2236. UnicodeString FileName = SelectedFileList->Strings[Index];
  2237. if (DirectoryExists(FileName))
  2238. {
  2239. if (FLAGSET(ACommand.Params, ccApplyToDirectories))
  2240. {
  2241. LocalFileList->Add(FileName);
  2242. }
  2243. if (FLAGSET(ACommand.Params, ccRecursive))
  2244. {
  2245. TMakeLocalFileListParams MakeFileListParam;
  2246. MakeFileListParam.FileList = LocalFileList.get();
  2247. MakeFileListParam.FileTimes = NULL;
  2248. MakeFileListParam.IncludeDirs = FLAGSET(ACommand.Params, ccApplyToDirectories);
  2249. MakeFileListParam.Recursive = true;
  2250. ProcessLocalDirectory(FileName, Terminal->MakeLocalFileList, &MakeFileListParam);
  2251. }
  2252. }
  2253. else
  2254. {
  2255. LocalFileList->Add(FileName);
  2256. }
  2257. }
  2258. UnicodeString RemotePath;
  2259. if (Terminal != NULL)
  2260. {
  2261. RemotePath = Terminal->CurrentDirectory;
  2262. }
  2263. if (FileListCommand)
  2264. {
  2265. UnicodeString FileList = MakeFileList(LocalFileList.get());
  2266. TLocalCustomCommand CustomCommand(
  2267. Data, RemotePath, DefaultDownloadTargetDirectory(),
  2268. L"", L"", FileList);
  2269. ExecuteProcessChecked(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  2270. }
  2271. else
  2272. {
  2273. TFileOperationProgressType Progress(&OperationProgress, &OperationFinished);
  2274. Progress.Start(foCustomCommand, osLocal, LocalFileList->Count);
  2275. DebugAssert(FProgressForm != NULL);
  2276. FProgressForm->ReadOnly = true;
  2277. try
  2278. {
  2279. for (int Index = 0; Index < LocalFileList->Count; Index++)
  2280. {
  2281. UnicodeString FileName = LocalFileList->Strings[Index];
  2282. Progress.SetFile(FileName);
  2283. TLocalCustomCommand CustomCommand(
  2284. Data, RemotePath, DefaultDownloadTargetDirectory(),
  2285. FileName, L"", L"");
  2286. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  2287. TOnceDoneOperation OnceDoneOperation;
  2288. Progress.Finish(FileName, true, OnceDoneOperation);
  2289. }
  2290. }
  2291. __finally
  2292. {
  2293. Progress.Stop();
  2294. }
  2295. }
  2296. }
  2297. //---------------------------------------------------------------------------
  2298. void __fastcall TCustomScpExplorerForm::LocalCustomCommand(TStrings * FileList,
  2299. const TCustomCommandType & ACommand, TStrings * ALocalFileList,
  2300. const TCustomCommandData & Data, const UnicodeString & CommandCommand)
  2301. {
  2302. UnicodeString RemotePath = (Terminal != NULL) ? Terminal->CurrentDirectory : UnicodeString();
  2303. TLocalCustomCommand LocalCustomCommand(Data, RemotePath, DefaultDownloadTargetDirectory());
  2304. TWinInteractiveCustomCommand InteractiveCustomCommand(
  2305. &LocalCustomCommand, ACommand.Name, ACommand.HomePage);
  2306. UnicodeString Command = InteractiveCustomCommand.Complete(CommandCommand, false);
  2307. bool FileListCommand = LocalCustomCommand.IsFileListCommand(Command);
  2308. bool LocalFileCommand = LocalCustomCommand.HasLocalFileName(Command);
  2309. Configuration->Usage->Inc(L"LocalCustomCommandRuns2");
  2310. std::unique_ptr<UnicodeString> POutput;
  2311. if (FLAGSET(ACommand.Params, ccShowResultsInMsgBox) ||
  2312. FLAGSET(ACommand.Params, ccCopyResults))
  2313. {
  2314. POutput.reset(new UnicodeString());
  2315. }
  2316. TValueRestorer<TOperationSide> ProgressSideRestorer(FProgressSide);
  2317. FProgressSide = FCurrentSide;
  2318. if (!LocalCustomCommand.IsFileCommand(Command))
  2319. {
  2320. ExecuteProcessChecked(LocalCustomCommand.Complete(Command, true), HelpKeyword, POutput.get());
  2321. }
  2322. // remote files?
  2323. else if (!IsSideLocalBrowser(FCurrentSide) || LocalFileCommand)
  2324. {
  2325. LocalCustomCommandPure(FileList, ACommand, Command, ALocalFileList, Data, LocalFileCommand, FileListCommand, POutput.get());
  2326. }
  2327. // local files
  2328. else
  2329. {
  2330. LocalCustomCommandWithLocalFiles(ACommand, Command, Data, FileListCommand, POutput.get());
  2331. }
  2332. if (POutput.get() != NULL)
  2333. {
  2334. // If the output is single-line, we do not want the trailing CRLF, as the CopyToClipboard(TStrings *) does
  2335. int P = POutput->Pos(sLineBreak);
  2336. if (P == POutput->Length() - static_cast<int>(strlen(sLineBreak)) + 1)
  2337. {
  2338. POutput->SetLength(P - 1);
  2339. }
  2340. // Copy even empty output to clipboard
  2341. if (FLAGSET(ACommand.Params, ccCopyResults))
  2342. {
  2343. CopyToClipboard(*POutput);
  2344. }
  2345. // But do not show an empty message box.
  2346. // This way the ShowResultsInMsgBox can be used to suppress a console window of commands with no output
  2347. if (FLAGSET(ACommand.Params, ccShowResultsInMsgBox) &&
  2348. !POutput->IsEmpty())
  2349. {
  2350. TClipboardHandler ClipboardHandler;
  2351. ClipboardHandler.Text = *POutput;
  2352. TMessageParams Params;
  2353. TQueryButtonAlias Aliases[1];
  2354. Aliases[0].Button = qaRetry;
  2355. Aliases[0].Alias = LoadStr(EDIT_COPY);
  2356. Aliases[0].OnSubmit = &ClipboardHandler.Copy;
  2357. Params.Aliases = Aliases;
  2358. Params.AliasesCount = LENOF(Aliases);
  2359. MessageDialog(*POutput, qtInformation, qaOK | qaRetry, HelpKeyword, &Params);
  2360. }
  2361. }
  2362. }
  2363. //---------------------------------------------------------------------------
  2364. void __fastcall TCustomScpExplorerForm::CustomCommand(TStrings * FileList,
  2365. const TCustomCommandType & ACommand, TStrings * ALocalFileList)
  2366. {
  2367. TCustomCommandData Data;
  2368. UnicodeString Site;
  2369. UnicodeString RemotePath;
  2370. if (HasActiveTerminal())
  2371. {
  2372. std::unique_ptr<TSessionData> SessionData(SessionDataForCode());
  2373. Data = TCustomCommandData(SessionData.get());
  2374. Site = Terminal->SessionData->SessionKey;
  2375. RemotePath = Terminal->CurrentDirectory;
  2376. }
  2377. UnicodeString HelpKeyword = ACommand.HomePage;
  2378. std::unique_ptr<TStrings> CustomCommandOptions(CloneStrings(WinConfiguration->CustomCommandOptions));
  2379. if (ACommand.AnyOptionWithFlag(TCustomCommandType::ofRun))
  2380. {
  2381. std::unique_ptr<TCustomCommand> CustomCommandForOptions;
  2382. if (FLAGCLEAR(ACommand.Params, ccLocal))
  2383. {
  2384. CustomCommandForOptions.reset(new TRemoteCustomCommand(Data, RemotePath));
  2385. }
  2386. else
  2387. {
  2388. CustomCommandForOptions.reset(new TLocalCustomCommand(Data, RemotePath, DefaultDownloadTargetDirectory()));
  2389. }
  2390. if (!DoCustomCommandOptionsDialog(
  2391. &ACommand, CustomCommandOptions.get(), NULL, TCustomCommandType::ofRun, CustomCommandForOptions.get(), Site, NULL))
  2392. {
  2393. Abort();
  2394. }
  2395. }
  2396. UnicodeString CommandCommand = ACommand.GetCommandWithExpandedOptions(CustomCommandOptions.get(), Site);
  2397. if (FLAGCLEAR(ACommand.Params, ccLocal))
  2398. {
  2399. RemoteCustomCommand(FileList, ACommand, Data, CommandCommand);
  2400. }
  2401. else
  2402. {
  2403. LocalCustomCommand(FileList, ACommand, ALocalFileList, Data, CommandCommand);
  2404. }
  2405. }
  2406. //---------------------------------------------------------------------------
  2407. void __fastcall TCustomScpExplorerForm::BothCustomCommand(
  2408. const TCustomCommandType & Command)
  2409. {
  2410. DebugAssert(FCustomCommandLocalFileList != NULL);
  2411. DebugAssert(FCustomCommandRemoteFileList != NULL);
  2412. DebugAssert(FCustomCommandLocalFileList->Count == FCustomCommandRemoteFileList->Count);
  2413. TStrings * LocalFileList = new TStringList();
  2414. TStrings * RemoteFileList = new TStringList();
  2415. try
  2416. {
  2417. for (int Index = 0; Index < FCustomCommandLocalFileList->Count; Index++)
  2418. {
  2419. LocalFileList->Clear();
  2420. LocalFileList->AddObject(
  2421. FCustomCommandLocalFileList->Strings[Index],
  2422. FCustomCommandLocalFileList->Objects[Index]);
  2423. RemoteFileList->Clear();
  2424. RemoteFileList->AddObject(
  2425. FCustomCommandRemoteFileList->Strings[Index],
  2426. FCustomCommandRemoteFileList->Objects[Index]);
  2427. CustomCommand(RemoteFileList, Command, LocalFileList);
  2428. }
  2429. }
  2430. __finally
  2431. {
  2432. delete LocalFileList;
  2433. delete RemoteFileList;
  2434. }
  2435. }
  2436. //---------------------------------------------------------------------------
  2437. void __fastcall TCustomScpExplorerForm::CustomCommandMenu(
  2438. TAction * Action, TStrings * LocalFileList, TStrings * RemoteFileList)
  2439. {
  2440. delete FCustomCommandLocalFileList;
  2441. delete FCustomCommandRemoteFileList;
  2442. // takeover ownership,
  2443. // the lists must survive the MenuPopup as OnClick occurs only after it exits
  2444. FCustomCommandLocalFileList = LocalFileList;
  2445. FCustomCommandRemoteFileList = RemoteFileList;
  2446. TButton * Button = dynamic_cast<TButton *>(Action->ActionComponent);
  2447. if (Button != NULL)
  2448. {
  2449. FCustomCommandMenu->Items->Clear();
  2450. NonVisualDataModule->CreateCustomCommandsMenu(FCustomCommandMenu->Items, false, false, ccltBoth, NULL);
  2451. MenuPopup(FCustomCommandMenu, Button);
  2452. }
  2453. else
  2454. {
  2455. NonVisualDataModule->CreateCustomCommandsMenu(Action, false, ccltBoth);
  2456. }
  2457. }
  2458. //---------------------------------------------------------------------------
  2459. void __fastcall TCustomScpExplorerForm::TerminalCaptureLog(
  2460. const UnicodeString & AddedLine, TCaptureOutputType OutputType)
  2461. {
  2462. DebugAssert(FCapturedLog != NULL);
  2463. if ((OutputType == cotOutput) || (OutputType == cotError))
  2464. {
  2465. FCapturedLog->Add(AddedLine);
  2466. }
  2467. }
  2468. //---------------------------------------------------------------------------
  2469. bool __fastcall TCustomScpExplorerForm::IsFileControl(TObject * Control,
  2470. TOperationSide Side)
  2471. {
  2472. return
  2473. (Side == osOther) &&
  2474. ((Control == DirView(osOther)) || (Control == DriveView(osOther)));
  2475. }
  2476. //---------------------------------------------------------------------------
  2477. void __fastcall TCustomScpExplorerForm::DirViewContextPopupDefaultItem(
  2478. TOperationSide Side, TTBXCustomItem * Item,
  2479. TResolvedDoubleClickAction DoubleClickAction1, TResolvedDoubleClickAction DoubleClickAction2)
  2480. {
  2481. TCustomDirView * DView = DirView(Side);
  2482. bool IsDefault = false;
  2483. if (DView->ItemFocused != NULL)
  2484. {
  2485. TTerminal * ATerminal = !IsSideLocalBrowser(Side) ? Terminal : NULL;
  2486. TResolvedDoubleClickAction DefaultAction =
  2487. WinConfiguration->ResolveDoubleClickAction(DView->ItemIsDirectory(DView->ItemFocused), ATerminal);
  2488. IsDefault = (DefaultAction == DoubleClickAction1) || (DefaultAction == DoubleClickAction2);
  2489. }
  2490. TTBItemOptions O = Item->Options;
  2491. if (IsDefault)
  2492. {
  2493. Item->Options = O << tboDefault;
  2494. }
  2495. else
  2496. {
  2497. Item->Options = O >> tboDefault;
  2498. }
  2499. }
  2500. //---------------------------------------------------------------------------
  2501. void __fastcall TCustomScpExplorerForm::DirViewContextPopup(
  2502. TOperationSide Side, Byte PopupComponent, const TPoint &MousePos)
  2503. {
  2504. TCustomDirView * DView = DirView(Side);
  2505. TListItem * Item = DView->ItemFocused;
  2506. if ((DView->GetItemAt(MousePos.x, MousePos.y) == Item) &&
  2507. EnableFocusedOperation[Side])
  2508. {
  2509. TPoint ClientPoint;
  2510. ClientPoint = ((MousePos.x < 0) && (MousePos.y < 0)) ?
  2511. TPoint(0, 0) : MousePos;
  2512. FLastContextPopupScreenPoint = DView->ClientToScreen(ClientPoint);
  2513. reinterpret_cast<TPopupMenu*>(GetComponent(PopupComponent))->Popup(
  2514. FLastContextPopupScreenPoint.x, FLastContextPopupScreenPoint.y);
  2515. }
  2516. }
  2517. //---------------------------------------------------------------------------
  2518. void __fastcall TCustomScpExplorerForm::RemoteDirViewContextPopup(
  2519. TObject * /*Sender*/, const TPoint &MousePos, bool &Handled)
  2520. {
  2521. DirViewContextPopupDefaultItem(osRemote, NonVisualDataModule->RemoteOpenMenuItem, rdcaChangeDir, rdcaOpen);
  2522. DirViewContextPopupDefaultItem(osRemote, NonVisualDataModule->RemoteEditMenuItem, rdcaEdit, rdcaNone);
  2523. DirViewContextPopupDefaultItem(osRemote, NonVisualDataModule->RemoteCopyMenuItem, rdcaCopy, rdcaNone);
  2524. DirViewContextPopup(osRemote, fcRemotePopup, MousePos);
  2525. Handled = true;
  2526. }
  2527. //---------------------------------------------------------------------------
  2528. void __fastcall TCustomScpExplorerForm::ReloadLocalDirectory(const UnicodeString Directory)
  2529. {
  2530. }
  2531. //---------------------------------------------------------------------------
  2532. void __fastcall TCustomScpExplorerForm::BatchStart(void *& /*Storage*/)
  2533. {
  2534. DebugAssert(FErrorList == NULL);
  2535. if (WinConfiguration->ContinueOnError)
  2536. {
  2537. FErrorList = new TStringList();
  2538. Configuration->Usage->Inc(L"ContinuationsOnError");
  2539. }
  2540. NonVisualDataModule->StartBusy();
  2541. }
  2542. //---------------------------------------------------------------------------
  2543. void __fastcall TCustomScpExplorerForm::BatchEnd(void * /*Storage*/)
  2544. {
  2545. NonVisualDataModule->EndBusy();
  2546. if (FErrorList)
  2547. {
  2548. HandleErrorList(FErrorList);
  2549. }
  2550. }
  2551. //---------------------------------------------------------------------------
  2552. void __fastcall TCustomScpExplorerForm::UpdateCopyParamCounters(
  2553. const TCopyParamType & CopyParam)
  2554. {
  2555. if (!CopyParam.IncludeFileMask.Masks.IsEmpty())
  2556. {
  2557. Configuration->Usage->Inc(L"FileMaskUses");
  2558. }
  2559. if (IsEffectiveFileNameMask(CopyParam.FileMask))
  2560. {
  2561. Configuration->Usage->Inc(L"OperationMaskUses");
  2562. }
  2563. }
  2564. //---------------------------------------------------------------------------
  2565. bool __fastcall TCustomScpExplorerForm::ExecuteCopyMoveFileOperation(
  2566. TFileOperation Operation, TOperationSide Side, TStrings * FileList, bool NoConfirmation, void * AParam)
  2567. {
  2568. DebugAssert(!IsLocalBrowserMode());
  2569. TTransferDirection Direction = (Side == osLocal ? tdToRemote : tdToLocal);
  2570. TTransferType Type = (Operation == foCopy ? ttCopy : ttMove);
  2571. TTransferOperationParam DefaultParam;
  2572. TTransferOperationParam & Param = (AParam != NULL) ? *static_cast<TTransferOperationParam *>(AParam) : DefaultParam;
  2573. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  2574. switch (Param.Queue)
  2575. {
  2576. case asOn:
  2577. CopyParam.Queue = true;
  2578. break;
  2579. case asOff:
  2580. CopyParam.Queue = false;
  2581. break;
  2582. case asAuto:
  2583. default:
  2584. // keep default
  2585. break;
  2586. }
  2587. bool Result =
  2588. CopyParamDialog(Direction, Type, Param.Temp, FileList, Param.TargetDirectory, CopyParam, !NoConfirmation, Param.DragDrop, Param.Options);
  2589. if (Result)
  2590. {
  2591. DebugAssert(Terminal);
  2592. bool SelectionRestored = false;
  2593. TCustomDirView * DView = NULL;
  2594. if (HasDirView[Side])
  2595. {
  2596. DView = DirView(Side);
  2597. DView->SaveSelection();
  2598. DView->SaveSelectedNames();
  2599. }
  2600. UpdateCopyParamCounters(CopyParam);
  2601. std::unique_ptr<TStringList> TransferResumeList(new TStringList());
  2602. DebugAssert(FTransferResumeList == NULL);
  2603. FTransferResumeList = Terminal->IsCapable[fcMoveToQueue] ? TransferResumeList.get() : NULL;
  2604. FMoveToQueue = false;
  2605. int Params = FLAGMASK(Operation == foMove, cpDelete);
  2606. try
  2607. {
  2608. TStrings * PermanentFileList;
  2609. std::unique_ptr<TStrings> PermanentFileListOwner;
  2610. try
  2611. {
  2612. if (Side == osLocal)
  2613. {
  2614. PermanentFileList = FileList;
  2615. Params |= FLAGMASK(Param.Temp, cpTemporary);
  2616. CopyParam.IncludeFileMask.SetRoots(FileList, Param.TargetDirectory);
  2617. Terminal->CopyToRemote(FileList, Param.TargetDirectory, &CopyParam, Params, NULL);
  2618. if (Operation == foMove)
  2619. {
  2620. ReloadLocalDirectory();
  2621. if (DView != NULL)
  2622. {
  2623. DView->RestoreSelection();
  2624. }
  2625. SelectionRestored = true;
  2626. }
  2627. }
  2628. else
  2629. {
  2630. // Clone the file list as it may refer to current directory files,
  2631. // which get destroyed, when the source directory is reloaded after foMove operation.
  2632. // We should actually clone the file list for whole ExecuteFileOperation to protect against reloads.
  2633. // But for a hotfix, we are not going to do such a big change.
  2634. PermanentFileListOwner.reset(TRemoteFileList::CloneStrings(FileList));
  2635. PermanentFileList = PermanentFileListOwner.get();
  2636. try
  2637. {
  2638. CopyParam.IncludeFileMask.SetRoots(Param.TargetDirectory, FileList);
  2639. Terminal->CopyToLocal(FileList, Param.TargetDirectory, &CopyParam, Params, NULL);
  2640. }
  2641. __finally
  2642. {
  2643. if (Operation == foMove)
  2644. {
  2645. if (DView != NULL)
  2646. {
  2647. DView->RestoreSelection();
  2648. }
  2649. SelectionRestored = true;
  2650. }
  2651. ReloadLocalDirectory(Param.TargetDirectory);
  2652. }
  2653. }
  2654. // When the transfer batch is aborted between individual file transfers, the Abort is not called.
  2655. if (FMoveToQueue)
  2656. {
  2657. Abort();
  2658. }
  2659. }
  2660. catch (EAbort &)
  2661. {
  2662. if (FMoveToQueue)
  2663. {
  2664. Params |= (CopyParam.QueueNoConfirmation ? cpNoConfirmation : 0);
  2665. DebugAssert(CopyParam.TransferSkipList == NULL);
  2666. DebugAssert(CopyParam.TransferResumeFile.IsEmpty());
  2667. if (TransferResumeList->Count > 0)
  2668. {
  2669. CopyParam.TransferResumeFile = TransferResumeList->Strings[TransferResumeList->Count - 1];
  2670. TransferResumeList->Delete(TransferResumeList->Count - 1);
  2671. }
  2672. CopyParam.TransferSkipList = TransferResumeList.release();
  2673. // not really needed, just to keep it consistent with TransferResumeList
  2674. FTransferResumeList = NULL;
  2675. FMoveToQueue = false;
  2676. Configuration->Usage->Inc("MovesToBackground");
  2677. AddQueueItem(Queue, Direction, PermanentFileList, Param.TargetDirectory, CopyParam, Params);
  2678. ClearTransferSourceSelection(Direction);
  2679. }
  2680. throw;
  2681. }
  2682. }
  2683. __finally
  2684. {
  2685. if (!SelectionRestored && (DView != NULL))
  2686. {
  2687. DView->DiscardSavedSelection();
  2688. }
  2689. FTransferResumeList = NULL;
  2690. }
  2691. }
  2692. return Result;
  2693. }
  2694. //---------------------------------------------------------------------------
  2695. bool __fastcall TCustomScpExplorerForm::ExecuteDeleteFileOperation(
  2696. TOperationSide Side, TStrings * FileList, void * Param)
  2697. {
  2698. DebugAssert(FileList->Count);
  2699. // We deliberately do not toggle alternative flag (Param), but use OR,
  2700. // because the Param is set only when command is invoked using Shift-Del/F8 keyboard
  2701. // shortcut of CurrentDeleteAlternativeAction
  2702. bool Alternative =
  2703. bool(Param) || UseAlternativeFunction();
  2704. bool Recycle;
  2705. if (IsSideLocalBrowser(Side))
  2706. {
  2707. Recycle = (WinConfiguration->DeleteToRecycleBin != Alternative);
  2708. }
  2709. else
  2710. {
  2711. Recycle =
  2712. (Terminal->SessionData->DeleteToRecycleBin != Alternative) &&
  2713. !Terminal->SessionData->RecycleBinPath.IsEmpty() &&
  2714. !Terminal->IsRecycledFile(FileList->Strings[0]);
  2715. }
  2716. bool Result = !(Recycle ? WinConfiguration->ConfirmRecycling : WinConfiguration->ConfirmDeleting);
  2717. if (!Result)
  2718. {
  2719. UnicodeString Query;
  2720. if (FileList->Count == 1)
  2721. {
  2722. if (IsSideLocalBrowser(Side))
  2723. {
  2724. Query = ExtractFileName(FileList->Strings[0]);
  2725. }
  2726. else
  2727. {
  2728. Query = UnixExtractFileName(FileList->Strings[0]);
  2729. }
  2730. Query = FMTLOAD((Recycle ? CONFIRM_RECYCLE_FILE : CONFIRM_DELETE_FILE), (Query));
  2731. }
  2732. else
  2733. {
  2734. Query = FMTLOAD((Recycle ? CONFIRM_RECYCLE_FILES : CONFIRM_DELETE_FILES), (FileList->Count));
  2735. }
  2736. TMessageParams Params(mpNeverAskAgainCheck);
  2737. Params.ImageName = L"Delete file";
  2738. unsigned int Answer =
  2739. MessageDialog(MainInstructions(Query), qtConfirmation, qaOK | qaCancel, HELP_DELETE_FILE, &Params);
  2740. if (Answer == qaNeverAskAgain)
  2741. {
  2742. Result = true;
  2743. if (Recycle)
  2744. {
  2745. WinConfiguration->ConfirmRecycling = false;
  2746. }
  2747. else
  2748. {
  2749. WinConfiguration->ConfirmDeleting = false;
  2750. }
  2751. }
  2752. else
  2753. {
  2754. Result = (Answer == qaOK);
  2755. }
  2756. }
  2757. if (Result)
  2758. {
  2759. DeleteFiles(Side, FileList, FLAGMASK(Alternative, dfAlternative));
  2760. }
  2761. return Result;
  2762. }
  2763. //---------------------------------------------------------------------------
  2764. bool __fastcall TCustomScpExplorerForm::ExecuteFileOperation(TFileOperation Operation,
  2765. TOperationSide Side, TStrings * FileList, bool NoConfirmation, void * Param)
  2766. {
  2767. TAutoBatch AutoBatch(this);
  2768. bool Result;
  2769. if (TFileOperationProgressType::IsTransferOperation(Operation))
  2770. {
  2771. Result = ExecuteCopyMoveFileOperation(Operation, Side, FileList, NoConfirmation, Param);
  2772. }
  2773. else if (Operation == foRename)
  2774. {
  2775. DebugAssert(DirView(Side)->ItemFocused);
  2776. DirView(Side)->ItemFocused->EditCaption();
  2777. Result = true;
  2778. }
  2779. else if (Operation == foDelete)
  2780. {
  2781. Result = ExecuteDeleteFileOperation(Side, FileList, Param);
  2782. }
  2783. else if (Operation == foSetProperties)
  2784. {
  2785. DirView(osRemote)->SaveSelectedNames();
  2786. Result = SetProperties(Side, FileList);
  2787. }
  2788. else if (Operation == foCustomCommand)
  2789. {
  2790. DebugAssert(Param);
  2791. DebugAssert(Side == osRemote); // Side carries no meaning for foCustomCommand
  2792. DirView(osRemote)->SaveSelectedNames();
  2793. const TCustomCommandType * Command = static_cast<const TCustomCommandType*>(Param);
  2794. CustomCommand(FileList, *Command, NULL);
  2795. Result = true;
  2796. }
  2797. else if ((Operation == foRemoteMove) || (Operation == foRemoteCopy))
  2798. {
  2799. DebugAssert(!IsSideLocalBrowser(Side));
  2800. Result = RemoteTransferFiles(FileList, NoConfirmation,
  2801. (Operation == foRemoteMove), reinterpret_cast<TManagedTerminal *>(Param));
  2802. }
  2803. else if (Operation == foLock)
  2804. {
  2805. DebugAssert(!IsSideLocalBrowser(Side));
  2806. LockFiles(FileList, true);
  2807. Result = true;
  2808. }
  2809. else if (Operation == foUnlock)
  2810. {
  2811. DebugAssert(!IsSideLocalBrowser(Side));
  2812. LockFiles(FileList, false);
  2813. Result = true;
  2814. }
  2815. else
  2816. {
  2817. DebugFail();
  2818. }
  2819. return Result;
  2820. }
  2821. //---------------------------------------------------------------------------
  2822. TOperationSide __fastcall TCustomScpExplorerForm::GetSide(TOperationSide Side)
  2823. {
  2824. if (Side == osCurrent)
  2825. {
  2826. Side = FCurrentSide;
  2827. }
  2828. return Side;
  2829. }
  2830. //---------------------------------------------------------------------------
  2831. bool __fastcall TCustomScpExplorerForm::ExecuteFileOperation(TFileOperation Operation,
  2832. TOperationSide Side, bool OnFocused, bool NoConfirmation, void * Param)
  2833. {
  2834. Side = GetSide(Side);
  2835. bool FullPath = IsSideLocalBrowser(Side);
  2836. TCustomDirView * ADirView = DirView(Side);
  2837. std::unique_ptr<TStringList> FileList(new TStringList());
  2838. ADirView->CreateFileList(OnFocused, FullPath, FileList.get());
  2839. bool ReloadProperties =
  2840. !IsSideLocalBrowser(Side) && (Operation == foSetProperties) &&
  2841. DebugAlwaysTrue(HasActiveTerminal()) && Terminal->IsCapable[fcLoadingAdditionalProperties];
  2842. bool Result = ExecuteFileOperation(Operation, Side, FileList.get(), NoConfirmation, Param);
  2843. if (Result && ReloadProperties)
  2844. {
  2845. std::unique_ptr<TStringList> FileList2(new TStringList());
  2846. FileList->CaseSensitive = true;
  2847. FileList->Sort();
  2848. for (int Index = 0; Index < ADirView->Items->Count; Index++)
  2849. {
  2850. TListItem * Item = ADirView->Items->Item[Index];
  2851. UnicodeString FileName = ADirView->ItemFileName(Item);
  2852. int Index2;
  2853. if (FileList->Find(FileName, Index2))
  2854. {
  2855. FileList2->AddObject(FileName, ADirView->ItemData(Item));
  2856. }
  2857. }
  2858. LoadFilesProperties(FileList2.get());
  2859. }
  2860. return Result;
  2861. }
  2862. //---------------------------------------------------------------------------
  2863. void __fastcall TCustomScpExplorerForm::ExecuteFileOperationCommand(
  2864. TFileOperation Operation, TOperationSide Side, bool OnFocused,
  2865. bool NoConfirmation, void * Param)
  2866. {
  2867. if (ExecuteFileOperation(Operation, Side, OnFocused, NoConfirmation, Param))
  2868. {
  2869. if (TFileOperationProgressType::IsTransferOperation(Operation))
  2870. {
  2871. DebugAssert(!IsLocalBrowserMode());
  2872. if (GetSide(Side) == osLocal)
  2873. {
  2874. Configuration->Usage->Inc(L"UploadsCommand");
  2875. }
  2876. else
  2877. {
  2878. Configuration->Usage->Inc(L"DownloadsCommand");
  2879. }
  2880. }
  2881. }
  2882. }
  2883. //---------------------------------------------------------------------------
  2884. void __fastcall TCustomScpExplorerForm::ExecuteCopyOperationCommand(
  2885. TOperationSide Side, bool OnFocused, unsigned int Flags)
  2886. {
  2887. TTransferOperationParam Param;
  2888. if ((WinConfiguration->Interface != ifCommander) ||
  2889. WinConfiguration->ScpCommander.ExplorerKeyboardShortcuts)
  2890. {
  2891. Flags &= ~cocShortCutHint;
  2892. }
  2893. TCustomDirView * DView = DirView(Side);
  2894. Param.Options =
  2895. FLAGMASK(FLAGSET(Flags, cocShortCutHint), coShortCutHint) |
  2896. FLAGMASK(SelectedAllFilesInDirView(DView), coAllFiles);
  2897. if (FLAGSET(Flags, cocQueue))
  2898. {
  2899. Param.Queue = asOn;
  2900. }
  2901. else if (FLAGSET(Flags, cocNonQueue))
  2902. {
  2903. Param.Queue = asOff;
  2904. }
  2905. ExecuteFileOperationCommand(foCopy, Side, OnFocused, false, &Param);
  2906. }
  2907. //---------------------------------------------------------------------------
  2908. void __fastcall TCustomScpExplorerForm::HandleErrorList(TStringList *& ErrorList)
  2909. {
  2910. int Count = ErrorList->Count;
  2911. try
  2912. {
  2913. if (ErrorList->Count)
  2914. {
  2915. UnicodeString Message = MainInstructions(FMTLOAD(ERROR_LIST_COUNT, (ErrorList->Count)));
  2916. if (MessageDialog(Message, qtError,
  2917. qaOK | qaCancel, HELP_NONE) == qaOK)
  2918. {
  2919. unsigned int Answer;
  2920. int Index = 0;
  2921. do
  2922. {
  2923. DebugAssert(Index >= 0 && Index < Count);
  2924. TQueryButtonAlias Aliases[2];
  2925. Aliases[0].Button = qaYes;
  2926. Aliases[0].Alias = LoadStr(PREV_BUTTON);
  2927. Aliases[1].Button = qaNo;
  2928. Aliases[1].Alias = LoadStr(NEXT_BUTTON);
  2929. TMessageParams Params;
  2930. Params.Aliases = Aliases;
  2931. Params.AliasesCount = LENOF(Aliases);
  2932. int No = Index + 1;
  2933. UnicodeString Message = ErrorList->Strings[Index];
  2934. UnicodeString AMainInstructions;
  2935. UnicodeString Details;
  2936. if (ExtractMainInstructions(Message, AMainInstructions))
  2937. {
  2938. Details = Message;
  2939. Message = AMainInstructions;
  2940. }
  2941. Message = MainInstructions(FMTLOAD(ERROR_LIST_NUMBER, (No, Count, Message))) + Details;
  2942. Answer = MoreMessageDialog(
  2943. Message,
  2944. dynamic_cast<TStrings *>(ErrorList->Objects[Index]), qtError,
  2945. (Index ? qaYes : 0) | (Index < Count - 1 ? qaNo : 0) |
  2946. qaOK, HELP_NONE, &Params);
  2947. if (Answer == qaNo)
  2948. {
  2949. Index++;
  2950. }
  2951. if (Answer == qaYes)
  2952. {
  2953. Index--;
  2954. }
  2955. }
  2956. while (Answer != qaOK);
  2957. }
  2958. }
  2959. }
  2960. __finally
  2961. {
  2962. TStrings * List = ErrorList;
  2963. ErrorList = NULL;
  2964. for (int i = 0; i < Count; i++)
  2965. {
  2966. delete List->Objects[i];
  2967. }
  2968. delete List;
  2969. }
  2970. }
  2971. //---------------------------------------------------------------------------
  2972. void __fastcall TCustomScpExplorerForm::ExecuteRemoteFile(
  2973. const UnicodeString & FullFileName, TRemoteFile * File, TExecuteFileBy ExecuteFileBy)
  2974. {
  2975. // needed for checking filemasks, as there's no directory object
  2976. // associated with the file object
  2977. File->FullFileName = FullFileName;
  2978. TFileMasks::TParams MaskParams;
  2979. MaskParams.Size = File->Size;
  2980. MaskParams.Modification = File->Modification;
  2981. ExecuteFile(osRemote, ExecuteFileBy, NULL, FullFileName, File, MaskParams);
  2982. }
  2983. //---------------------------------------------------------------------------
  2984. void __fastcall TCustomScpExplorerForm::EditNew(TOperationSide Side)
  2985. {
  2986. DebugAssert(!WinConfiguration->DisableOpenEdit);
  2987. Side = GetSide(Side);
  2988. TCustomDirView * DView = DirView(Side);
  2989. TListItem * FocusedItem = DView->ItemFocused;
  2990. UnicodeString Name;
  2991. if ((FocusedItem != NULL) && !DView->ItemIsDirectory(FocusedItem))
  2992. {
  2993. Name = DView->ItemFileName(FocusedItem);
  2994. }
  2995. else
  2996. {
  2997. Name = LoadStr(NEW_FILE);
  2998. }
  2999. UnicodeString Names = Name;
  3000. std::unique_ptr<TStrings> History(CloneStrings(CustomWinConfiguration->History[L"EditFile"]));
  3001. if (InputDialog(LoadStr(EDIT_FILE_CAPTION), LoadStr(EDIT_FILE_PROMPT), Names,
  3002. HELP_EDIT_NEW, History.get(), true, NULL, true, 400))
  3003. {
  3004. while (!Names.IsEmpty())
  3005. {
  3006. Name = CutToChar(Names, FileMasksDelimiters[1], false);
  3007. CustomWinConfiguration->History[L"EditFile"] = History.get();
  3008. UnicodeString TargetFileName;
  3009. UnicodeString LocalFileName;
  3010. UnicodeString RootTempDir;
  3011. UnicodeString TempDir;
  3012. UnicodeString RemoteDirectory;
  3013. bool ExistingFile = false;
  3014. if (!IsSideLocalBrowser(Side))
  3015. {
  3016. Name = AbsolutePath(Terminal->CurrentDirectory, Name);
  3017. std::unique_ptr<TRemoteFile> File(Terminal->TryReadFile(Name));
  3018. if ((File.get() != NULL) &&
  3019. !File->IsDirectory)
  3020. {
  3021. ExecuteRemoteFile(Name, File.get(), efDefaultEditor);
  3022. ExistingFile = true;
  3023. }
  3024. File.reset(NULL);
  3025. if (!ExistingFile)
  3026. {
  3027. RemoteDirectory = UnixExtractFilePath(Name);
  3028. TemporaryDirectoryForRemoteFiles(
  3029. RemoteDirectory, GUIConfiguration->CurrentCopyParam, false, TempDir, RootTempDir);
  3030. TargetFileName = UnixExtractFileName(Name);
  3031. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  3032. LocalFileName = TempDir +
  3033. // We probably do not want to trim the VMS version here
  3034. Terminal->ChangeFileName(&CopyParam, TargetFileName, osRemote, false);
  3035. }
  3036. }
  3037. else
  3038. {
  3039. if (ExtractFilePath(Name).IsEmpty())
  3040. {
  3041. LocalFileName = IncludeTrailingBackslash(DView->PathName) + Name;
  3042. }
  3043. else
  3044. {
  3045. LocalFileName = ExpandFileName(Name);
  3046. }
  3047. TargetFileName = ExtractFileName(Name);
  3048. }
  3049. if (!ExistingFile)
  3050. {
  3051. bool NewFile = !FileExists(ApiPath(LocalFileName));
  3052. if (NewFile)
  3053. {
  3054. int File = FileCreate(ApiPath(LocalFileName));
  3055. if (File < 0)
  3056. {
  3057. if (!RootTempDir.IsEmpty())
  3058. {
  3059. RecursiveDeleteFile(ExcludeTrailingBackslash(RootTempDir), false);
  3060. }
  3061. throw Exception(FMTLOAD(CREATE_FILE_ERROR, (LocalFileName)));
  3062. }
  3063. else
  3064. {
  3065. FileClose(File);
  3066. }
  3067. }
  3068. TExecuteFileBy ExecuteFileBy = efDefaultEditor;
  3069. const TEditorData * ExternalEditor = NULL;
  3070. TFileMasks::TParams MaskParams; // size not known
  3071. ExecuteFileNormalize(ExecuteFileBy, ExternalEditor, TargetFileName,
  3072. false, MaskParams);
  3073. CustomExecuteFile(Side, ExecuteFileBy, LocalFileName, TargetFileName,
  3074. ExternalEditor, RootTempDir, RemoteDirectory, NewFile, TDateTime());
  3075. }
  3076. }
  3077. }
  3078. }
  3079. //---------------------------------------------------------------------------
  3080. bool __fastcall TCustomScpExplorerForm::RemoteExecuteForceText(
  3081. TExecuteFileBy ExecuteFileBy, const TEditorData * ExternalEditor)
  3082. {
  3083. DebugAssert((ExecuteFileBy == efExternalEditor) ==
  3084. ((ExternalEditor != NULL) && (ExternalEditor->Editor == edExternal)));
  3085. DebugAssert(ExecuteFileBy != efDefaultEditor);
  3086. return
  3087. ((ExecuteFileBy == efInternalEditor)) ||
  3088. ((ExecuteFileBy == efExternalEditor) && ExternalEditor->ExternalEditorText);
  3089. }
  3090. //---------------------------------------------------------------------------
  3091. void __fastcall TCustomScpExplorerForm::CustomExecuteFile(TOperationSide Side,
  3092. TExecuteFileBy ExecuteFileBy, UnicodeString FileName, UnicodeString OriginalFileName,
  3093. const TEditorData * ExternalEditor, UnicodeString LocalRootDirectory,
  3094. UnicodeString RemoteDirectory, bool NewFile, const TDateTime & SourceTimestamp)
  3095. {
  3096. DebugAssert(!WinConfiguration->DisableOpenEdit);
  3097. DebugAssert((ExecuteFileBy == efExternalEditor) ==
  3098. ((ExternalEditor != NULL) && (ExternalEditor->Editor == edExternal)));
  3099. DebugAssert(ExecuteFileBy != efDefaultEditor);
  3100. Side = GetSide(Side);
  3101. std::unique_ptr<TEditedFileData> Data(new TEditedFileData);
  3102. if (!IsSideLocalBrowser(Side))
  3103. {
  3104. Data->Terminal = Terminal;
  3105. Data->Queue = Queue;
  3106. Data->SessionData = CloneCurrentSessionData();
  3107. Data->ForceText = RemoteExecuteForceText(ExecuteFileBy, ExternalEditor);
  3108. Data->RemoteDirectory = RemoteDirectory;
  3109. Data->SessionName = Terminal->SessionData->SessionName;
  3110. Data->LocalRootDirectory = LocalRootDirectory;
  3111. Data->OriginalFileName = OriginalFileName;
  3112. Data->Command = L""; // will be changed later for external editor
  3113. // Empty SourceTimestamp indicates that checking was disabled when the file was opened
  3114. if (WinConfiguration->EditorCheckNotModified)
  3115. {
  3116. Data->SourceTimestamp = SourceTimestamp;
  3117. }
  3118. }
  3119. if (ExecuteFileBy == efInternalEditor)
  3120. {
  3121. if (!IsSideLocalBrowser(Side))
  3122. {
  3123. UnicodeString RemotePath = UnixCombinePaths(RemoteDirectory, OriginalFileName);
  3124. UnicodeString Caption = RemotePath + TitleSeparator + Terminal->SessionData->SessionName;
  3125. TForm * Editor;
  3126. try
  3127. {
  3128. Editor =
  3129. ShowEditorForm(
  3130. FileName, this,
  3131. FEditorManager->FileChanged, FEditorManager->FileReload, FEditorManager->FileClosed,
  3132. SaveAllInternalEditors, AnyInternalEditorModified,
  3133. Caption, StandaloneOperation, SessionColor, Terminal->SessionData->InternalEditorEncoding, NewFile);
  3134. }
  3135. catch(...)
  3136. {
  3137. if (!LocalRootDirectory.IsEmpty())
  3138. {
  3139. RecursiveDeleteFile(ExcludeTrailingBackslash(LocalRootDirectory), false);
  3140. }
  3141. throw;
  3142. }
  3143. FEditorManager->AddFileInternal(FileName, Data.release(), Editor);
  3144. AppLogFmt(L"Opened internal editor with \"%s\"", (FileName));
  3145. }
  3146. else
  3147. {
  3148. DebugAssert(!StandaloneOperation);
  3149. TForm * Editor =
  3150. ShowEditorForm(FileName, this, NULL, NULL, LocalEditorClosed,
  3151. SaveAllInternalEditors, AnyInternalEditorModified,
  3152. L"", false, SessionColor, -1, NewFile);
  3153. FLocalEditors->Add(Editor);
  3154. }
  3155. }
  3156. else
  3157. {
  3158. HANDLE Process;
  3159. if (ExecuteFileBy == efExternalEditor)
  3160. {
  3161. UnicodeString Program, Params, Dir;
  3162. Data->Command = ExternalEditor->ExternalEditor;
  3163. ReformatFileNameCommand(Data->Command);
  3164. SplitCommand(Data->Command, Program, Params, Dir);
  3165. Params = ExpandFileNameCommand(Params, FileName);
  3166. Program = ExpandEnvironmentVariables(Program);
  3167. if (!ExecuteShell(Program, Params, Process))
  3168. {
  3169. throw EOSExtException(FMTLOAD(EDITOR_ERROR, (Program)));
  3170. }
  3171. }
  3172. else
  3173. {
  3174. DebugAssert(!IsSideLocalBrowser(Side));
  3175. if (!ExecuteShell(FileName, L"", Process))
  3176. {
  3177. throw EOSExtException(FMTLOAD(EXECUTE_FILE_ERROR, (FileName)));
  3178. }
  3179. }
  3180. if (IsSideLocalBrowser(Side) ||
  3181. ((ExecuteFileBy == efShell) &&
  3182. !WinConfiguration->Editor.SDIShellEditor) ||
  3183. ((ExecuteFileBy == efExternalEditor) &&
  3184. !ExternalEditor->SDIExternalEditor))
  3185. {
  3186. // no need for handle
  3187. if (Process != NULL)
  3188. {
  3189. DebugCheck(CloseHandle(Process));
  3190. }
  3191. Process = INVALID_HANDLE_VALUE;
  3192. }
  3193. else
  3194. {
  3195. if (Process == NULL)
  3196. {
  3197. throw ExtException(LoadStr(OPEN_FILE_NO_PROCESS2), L"", HELP_OPEN_FILE_NO_PROCESS);
  3198. }
  3199. }
  3200. if (!IsSideLocalBrowser(Side))
  3201. {
  3202. FEditorManager->AddFileExternal(FileName, Data.release(), Process);
  3203. AppLogFmt(L"Opened external editor with remote file \"%s\".", (FileName));
  3204. }
  3205. }
  3206. }
  3207. //---------------------------------------------------------------------------
  3208. void __fastcall TCustomScpExplorerForm::SaveInternalEditor(
  3209. const UnicodeString /*FileName*/, TEditedFileData * /*Data*/, TObject * Token,
  3210. void * /*Arg*/)
  3211. {
  3212. if (Token != NULL)
  3213. {
  3214. EditorFormFileSave(static_cast<TForm *>(Token));
  3215. }
  3216. }
  3217. //---------------------------------------------------------------------------
  3218. void __fastcall TCustomScpExplorerForm::SaveAllInternalEditors(TObject * /*Sender*/)
  3219. {
  3220. for (int Index = 0; Index < FLocalEditors->Count; Index++)
  3221. {
  3222. EditorFormFileSave(static_cast<TForm *>(FLocalEditors->Items[Index]));
  3223. }
  3224. FEditorManager->ProcessFiles(SaveInternalEditor, NULL);
  3225. }
  3226. //---------------------------------------------------------------------------
  3227. void __fastcall TCustomScpExplorerForm::InternalEditorModified(
  3228. const UnicodeString /*FileName*/, TEditedFileData * /*Data*/, TObject * Token,
  3229. void * Arg)
  3230. {
  3231. if ((Token != NULL) &&
  3232. IsEditorFormModified(static_cast<TForm *>(Token)))
  3233. {
  3234. *static_cast<bool *>(Arg) = true;
  3235. }
  3236. }
  3237. //---------------------------------------------------------------------------
  3238. void __fastcall TCustomScpExplorerForm::AnyInternalEditorModified(
  3239. TObject * /*Sender*/, bool & Modified)
  3240. {
  3241. for (int Index = 0; !Modified && (Index < FLocalEditors->Count); Index++)
  3242. {
  3243. if (IsEditorFormModified(static_cast<TForm *>(FLocalEditors->Items[Index])))
  3244. {
  3245. Modified = true;
  3246. }
  3247. }
  3248. if (!Modified)
  3249. {
  3250. FEditorManager->ProcessFiles(InternalEditorModified, &Modified);
  3251. }
  3252. }
  3253. //---------------------------------------------------------------------------
  3254. void __fastcall TCustomScpExplorerForm::LocalEditorClosed(TObject * Sender, bool /*Forced*/)
  3255. {
  3256. DebugCheck(FLocalEditors->Extract(Sender) >= 0);
  3257. }
  3258. //---------------------------------------------------------------------------
  3259. void __fastcall TCustomScpExplorerForm::TemporaryDirectoryForRemoteFiles(
  3260. const UnicodeString & RemoteDirectory, const TCopyParamType & CopyParam, bool Simple,
  3261. UnicodeString & Result, UnicodeString & RootDirectory)
  3262. {
  3263. if (!WinConfiguration->TemporaryDirectoryDeterministic || Simple)
  3264. {
  3265. RootDirectory = IncludeTrailingBackslash(WinConfiguration->TemporaryDir());
  3266. Result = RootDirectory;
  3267. }
  3268. else
  3269. {
  3270. RootDirectory = L"";
  3271. Result = WinConfiguration->ExpandedTemporaryDirectory();
  3272. Result = IncludeTrailingBackslash(Result);
  3273. }
  3274. if (WinConfiguration->TemporaryDirectoryAppendSession && !Simple)
  3275. {
  3276. Result = IncludeTrailingBackslash(Result + MakeValidFileName(Terminal->SessionData->SessionName));
  3277. }
  3278. if (WinConfiguration->TemporaryDirectoryAppendPath && !Simple)
  3279. {
  3280. UnicodeString ARemoteDirectory = RemoteDirectory;
  3281. if (!ARemoteDirectory.IsEmpty() && (ARemoteDirectory[1] == L'/'))
  3282. {
  3283. ARemoteDirectory.Delete(1, 1);
  3284. }
  3285. Result = IncludeTrailingBackslash(Result + CopyParam.ValidLocalPath(FromUnixPath(ARemoteDirectory)));
  3286. }
  3287. if (!ForceDirectories(ApiPath(Result)))
  3288. {
  3289. throw EOSExtException(FMTLOAD(CREATE_TEMP_DIR_ERROR, (Result)));
  3290. }
  3291. }
  3292. //---------------------------------------------------------------------------
  3293. void __fastcall TCustomScpExplorerForm::TemporarilyDownloadFiles(
  3294. TStrings * FileList, bool ForceText, UnicodeString & RootTempDir, UnicodeString & TempDir,
  3295. bool GetTargetNames, bool AutoOperation, bool SimpleTempDir)
  3296. {
  3297. DebugAssert(!IsLocalBrowserMode());
  3298. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  3299. if (ForceText)
  3300. {
  3301. CopyParam.TransferMode = tmAscii;
  3302. }
  3303. TemporaryFileCopyParam(CopyParam);
  3304. if (TempDir.IsEmpty())
  3305. {
  3306. TemporaryDirectoryForRemoteFiles(Terminal->CurrentDirectory, CopyParam, SimpleTempDir, TempDir, RootTempDir);
  3307. }
  3308. DebugAssert(!FAutoOperation);
  3309. FAutoOperation = AutoOperation;
  3310. Terminal->ExceptionOnFail = true;
  3311. try
  3312. {
  3313. try
  3314. {
  3315. // turn off confirmations, as for MDI editors we may possibly download
  3316. // the same file over
  3317. Terminal->CopyToLocal(
  3318. FileList, TempDir, &CopyParam, cpNoConfirmation | cpTemporary, NULL);
  3319. if (GetTargetNames)
  3320. {
  3321. for (int i = 0; i < FileList->Count; i++)
  3322. {
  3323. FileList->Strings[i] =
  3324. Terminal->ChangeFileName(&CopyParam, UnixExtractFileName(FileList->Strings[i]), osRemote, false);
  3325. }
  3326. }
  3327. }
  3328. catch(...)
  3329. {
  3330. if (!RootTempDir.IsEmpty())
  3331. {
  3332. RecursiveDeleteFile(ExcludeTrailingBackslash(RootTempDir), false);
  3333. }
  3334. throw;
  3335. }
  3336. }
  3337. __finally
  3338. {
  3339. FAutoOperation = false;
  3340. Terminal->ExceptionOnFail = false;
  3341. }
  3342. }
  3343. //---------------------------------------------------------------------------
  3344. void __fastcall TCustomScpExplorerForm::EditorAutoConfig()
  3345. {
  3346. // Do not waste time checking for default editor list the next time,
  3347. // or testing if default editor is not notepad.
  3348. bool TryNextTime = false;
  3349. UnicodeString UsageState;
  3350. if (!WinConfiguration->EditorList->IsDefaultList())
  3351. {
  3352. UsageState = "H";
  3353. }
  3354. else
  3355. {
  3356. UnicodeString Executable;
  3357. UnicodeString ExecutableDescription;
  3358. if (DetectSystemExternalEditor(false, Executable, ExecutableDescription, UsageState, TryNextTime))
  3359. {
  3360. UnicodeString Message =
  3361. FMTLOAD(EDITOR_AUTO_CONFIG2, (ExecutableDescription, ExecutableDescription));
  3362. unsigned int Answer =
  3363. MessageDialog(Message, qtConfirmation, qaOK | qaCancel, HELP_EDITOR_AUTO_CONFIG);
  3364. if (Answer != qaOK)
  3365. {
  3366. UsageState = "R";
  3367. }
  3368. else
  3369. {
  3370. UsageState = "A";
  3371. TEditorData EditorData;
  3372. EditorData.Editor = edExternal;
  3373. EditorData.ExternalEditor = FormatCommand(Executable, L"");
  3374. EditorData.ExternalEditorOptionsAutodetect();
  3375. TEditorList EditorList;
  3376. EditorList = *WinConfiguration->EditorList;
  3377. EditorList.Insert(0, new TEditorPreferences(EditorData));
  3378. WinConfiguration->EditorList = &EditorList;
  3379. }
  3380. }
  3381. }
  3382. WinConfiguration->OfferedEditorAutoConfig = !TryNextTime;
  3383. WinConfiguration->Usage->Set(L"EditorAutoConfig", UsageState);
  3384. }
  3385. //---------------------------------------------------------------------------
  3386. void __fastcall TCustomScpExplorerForm::ExecuteFileNormalize(
  3387. TExecuteFileBy & ExecuteFileBy, const TEditorData *& ExternalEditor,
  3388. const UnicodeString & FileName, bool Local, const TFileMasks::TParams & MaskParams)
  3389. {
  3390. if (ExecuteFileBy == efDefaultEditor)
  3391. {
  3392. if (!WinConfiguration->OfferedEditorAutoConfig)
  3393. {
  3394. EditorAutoConfig();
  3395. }
  3396. const TEditorPreferences * Editor =
  3397. WinConfiguration->DefaultEditorForFile(FileName, Local, MaskParams);
  3398. if ((Editor == NULL) || (Editor->Data->Editor == edInternal))
  3399. {
  3400. ExecuteFileBy = efInternalEditor;
  3401. ExternalEditor = NULL;
  3402. }
  3403. else if (Editor->Data->Editor == edOpen)
  3404. {
  3405. ExecuteFileBy = efShell;
  3406. ExternalEditor = NULL;
  3407. }
  3408. else
  3409. {
  3410. ExecuteFileBy = efExternalEditor;
  3411. ExternalEditor = Editor->Data;
  3412. }
  3413. }
  3414. }
  3415. //---------------------------------------------------------------------------
  3416. void __fastcall TCustomScpExplorerForm::ExecuteFile(TOperationSide Side,
  3417. TExecuteFileBy ExecuteFileBy, const TEditorData * ExternalEditor,
  3418. UnicodeString FullFileName, TObject * Object, const TFileMasks::TParams & MaskParams)
  3419. {
  3420. UnicodeString OriginalFileName;
  3421. UnicodeString LocalRootDirectory;
  3422. UnicodeString RemoteDirectory;
  3423. ExecuteFileNormalize(ExecuteFileBy, ExternalEditor, FullFileName,
  3424. IsSideLocalBrowser(Side), MaskParams);
  3425. UnicodeString Counter;
  3426. UnicodeString LocalFileName;
  3427. bool Handled = false;
  3428. if (!IsSideLocalBrowser(Side))
  3429. {
  3430. // We need to trim VMS version here, so that we use name without version
  3431. // when uploading back to create a new version of the file
  3432. OriginalFileName = Terminal->GetBaseFileName(UnixExtractFileName(FullFileName));
  3433. RemoteDirectory = UnixExtractFilePath(FullFileName);
  3434. TObject * Token = NULL;
  3435. UnicodeString LocalDirectory;
  3436. if (!FEditorManager->CanAddFile(RemoteDirectory, OriginalFileName,
  3437. Terminal->SessionData->SessionName, Token, LocalRootDirectory,
  3438. LocalDirectory))
  3439. {
  3440. if (Token != NULL)
  3441. {
  3442. TForm * Form = dynamic_cast<TForm *>(Token);
  3443. if (Form->WindowState == wsMinimized)
  3444. {
  3445. ShowWindow(Form->Handle, SW_RESTORE);
  3446. }
  3447. else
  3448. {
  3449. Form->SetFocus();
  3450. }
  3451. Handled = true;
  3452. }
  3453. else
  3454. {
  3455. throw Exception(FMTLOAD(ALREADY_EDITED_EXTERNALLY_OR_UPLOADED, (OriginalFileName)));
  3456. }
  3457. }
  3458. if (!Handled)
  3459. {
  3460. TStringList * FileList1 = new TStringList();
  3461. try
  3462. {
  3463. FileList1->AddObject(FullFileName, Object);
  3464. TemporarilyDownloadFiles(FileList1,
  3465. RemoteExecuteForceText(ExecuteFileBy, ExternalEditor),
  3466. LocalRootDirectory, LocalDirectory, true, true, false);
  3467. LocalFileName = LocalDirectory + FileList1->Strings[0];
  3468. }
  3469. __finally
  3470. {
  3471. delete FileList1;
  3472. }
  3473. switch (ExecuteFileBy)
  3474. {
  3475. case efShell:
  3476. Counter = "RemoteFilesExecuted";
  3477. break;
  3478. case efInternalEditor:
  3479. Counter = "RemoteFilesOpenedInInternalEditor";
  3480. break;
  3481. case efExternalEditor:
  3482. Counter = "RemoteFilesOpenedInExternalEditor";
  3483. break;
  3484. default:
  3485. DebugFail();
  3486. }
  3487. }
  3488. }
  3489. else
  3490. {
  3491. LocalFileName = FullFileName;
  3492. OriginalFileName = ExtractFileName(FullFileName);
  3493. switch (ExecuteFileBy)
  3494. {
  3495. case efShell:
  3496. Counter = "LocalFilesExecuted";
  3497. break;
  3498. case efInternalEditor:
  3499. Counter = "LocalFilesOpenedInInternalEditor";
  3500. break;
  3501. case efExternalEditor:
  3502. Counter = "LocalFilesOpenedInExternalEditor";
  3503. break;
  3504. default:
  3505. DebugFail();
  3506. }
  3507. }
  3508. if (!Handled)
  3509. {
  3510. Configuration->Usage->Inc(Counter);
  3511. CustomExecuteFile(Side, ExecuteFileBy, LocalFileName, OriginalFileName,
  3512. ExternalEditor, LocalRootDirectory, RemoteDirectory, false, MaskParams.Modification);
  3513. }
  3514. }
  3515. //---------------------------------------------------------------------------
  3516. void __fastcall TCustomScpExplorerForm::ExecuteFile(TOperationSide Side,
  3517. TExecuteFileBy ExecuteFileBy, const TEditorData * ExternalEditor,
  3518. bool AllSelected, bool OnFocused)
  3519. {
  3520. DebugAssert(!WinConfiguration->DisableOpenEdit);
  3521. DebugAssert((ExecuteFileBy == efExternalEditor) ==
  3522. ((ExternalEditor != NULL) && (ExternalEditor->Editor == edExternal)));
  3523. Side = GetSide(Side);
  3524. TCustomDirView * DView = DirView(Side);
  3525. TStrings * FileList =
  3526. AllSelected ?
  3527. DView->CreateFileList(OnFocused, IsSideLocalBrowser(Side)) :
  3528. DView->CreateFocusedFileList(IsSideLocalBrowser(Side));
  3529. try
  3530. {
  3531. DebugAssert(AllSelected || (FileList->Count == 1));
  3532. for (int i = 0; i < FileList->Count; i++)
  3533. {
  3534. UnicodeString ListFileName = FileList->Strings[i];
  3535. UnicodeString FileNameOnly =
  3536. !IsSideLocalBrowser(Side) ? UnixExtractFileName(ListFileName) : ExtractFileName(ListFileName);
  3537. TListItem * Item = DView->FindFileItem(FileNameOnly);
  3538. if (!DView->ItemIsDirectory(Item))
  3539. {
  3540. UnicodeString FullFileName;
  3541. if (!IsSideLocalBrowser(Side))
  3542. {
  3543. FullFileName = DirView(Side)->Path + ListFileName;
  3544. }
  3545. else
  3546. {
  3547. FullFileName = ListFileName;
  3548. }
  3549. TObject * Object = FileList->Objects[i];
  3550. TFileMasks::TParams MaskParams;
  3551. MaskParams.Size = DView->ItemFileSize(Item);
  3552. TDateTimePrecision Precision;
  3553. MaskParams.Modification = DView->ItemFileTime(Item, Precision);
  3554. std::unique_ptr<TRemoteFile> File;
  3555. if (!IsSideLocalBrowser(Side))
  3556. {
  3557. File.reset(Terminal->TryReadFile(FullFileName));
  3558. if (File.get() != NULL)
  3559. {
  3560. File->FullFileName = FullFileName;
  3561. Object = File.get();
  3562. MaskParams.Size = File->Size;
  3563. MaskParams.Modification = File->Modification;
  3564. }
  3565. }
  3566. ExecuteFile(Side, ExecuteFileBy, ExternalEditor, FullFileName, Object, MaskParams);
  3567. }
  3568. }
  3569. }
  3570. __finally
  3571. {
  3572. delete FileList;
  3573. }
  3574. }
  3575. //---------------------------------------------------------------------------
  3576. void __fastcall TCustomScpExplorerForm::TemporaryFileCopyParam(TCopyParamType & CopyParam)
  3577. {
  3578. CopyParam.FileNameCase = ncNoChange;
  3579. CopyParam.PreserveRights = false;
  3580. CopyParam.PreserveReadOnly = false;
  3581. CopyParam.ReplaceInvalidChars = true;
  3582. CopyParam.IncludeFileMask = TFileMasks();
  3583. CopyParam.NewerOnly = false;
  3584. CopyParam.FileMask = L"";
  3585. }
  3586. //---------------------------------------------------------------------------
  3587. bool TCustomScpExplorerForm::EditorCheckNotModified(const TEditedFileData * Data)
  3588. {
  3589. return WinConfiguration->EditorCheckNotModified && (Data->SourceTimestamp != TDateTime());
  3590. }
  3591. //---------------------------------------------------------------------------
  3592. void __fastcall TCustomScpExplorerForm::ExecutedFileChanged(
  3593. const UnicodeString & FileName, TEditedFileData * Data, HANDLE UploadCompleteEvent, bool & Retry)
  3594. {
  3595. TTerminalManager * Manager = TTerminalManager::Instance();
  3596. if (!IsActiveTerminal(Data->Terminal))
  3597. {
  3598. if (!NonVisualDataModule->Busy)
  3599. {
  3600. UnicodeString FileNameOnly = ExtractFileName(FileName);
  3601. UnicodeString EditInactiveSessionReopenAcceptedCounter = L"EditInactiveSessionReopenAccepted";
  3602. UnicodeString EditInactiveSessionReopenRejectedCounter = L"EditInactiveSessionReopenRejected";
  3603. if (Data->Terminal == NULL)
  3604. {
  3605. TManagedTerminal * SameSiteTerminal = Manager->FindActiveTerminalForSite(Data->SessionData);
  3606. if (SameSiteTerminal != NULL)
  3607. {
  3608. UnicodeString Message =
  3609. FMTLOAD(EDIT_SESSION_REATTACH,
  3610. (FileNameOnly, Data->SessionName, FileNameOnly));
  3611. if (MessageDialog(Message, qtConfirmation, qaOK | qaCancel) == qaOK)
  3612. {
  3613. Data->Terminal = SameSiteTerminal;
  3614. Data->Queue = Manager->FindQueueForTerminal(SameSiteTerminal);
  3615. Data->SessionName = SameSiteTerminal->SessionData->SessionName;
  3616. // We might also overwrite session data
  3617. Configuration->Usage->Inc(EditInactiveSessionReopenAcceptedCounter);
  3618. }
  3619. else
  3620. {
  3621. Configuration->Usage->Inc(EditInactiveSessionReopenRejectedCounter);
  3622. Abort();
  3623. }
  3624. }
  3625. }
  3626. // foreground session should reconnect itself
  3627. else if (Terminal != Data->Terminal)
  3628. {
  3629. UnicodeString Message =
  3630. MainInstructions(
  3631. FMTLOAD(EDIT_SESSION_RECONNECT, (Data->SessionName, FileNameOnly)));
  3632. if (MessageDialog(Message, qtConfirmation, qaOK | qaCancel) == qaOK)
  3633. {
  3634. Manager->SetActiveTerminalWithAutoReconnect(Data->Terminal);
  3635. Configuration->Usage->Inc(EditInactiveSessionReopenAcceptedCounter);
  3636. }
  3637. else
  3638. {
  3639. Configuration->Usage->Inc(EditInactiveSessionReopenRejectedCounter);
  3640. Abort();
  3641. }
  3642. }
  3643. }
  3644. if (!IsActiveTerminal(Data->Terminal))
  3645. {
  3646. Configuration->Usage->Inc(L"EditInactiveSession");
  3647. // Prevent this when not idle (!NonVisualDataModule->Busy)?
  3648. throw Exception(FMTLOAD(EDIT_SESSION_CLOSED2,
  3649. (ExtractFileName(FileName), Data->SessionName)));
  3650. }
  3651. }
  3652. if (EditorCheckNotModified(Data))
  3653. {
  3654. UnicodeString RemoteFilePath = UnixCombinePaths(Data->RemoteDirectory, Data->OriginalFileName);
  3655. std::unique_ptr<TRemoteFile> File(Terminal->TryReadFile(RemoteFilePath));
  3656. if (File.get() != NULL)
  3657. {
  3658. AppLogFmt(L"Edited remote file timestamp: %s, Original timestamp: %s", (StandardTimestamp(File->Modification), StandardTimestamp(Data->SourceTimestamp)));
  3659. if (File->Modification != Data->SourceTimestamp)
  3660. {
  3661. UnicodeString Message = MainInstructions(LoadStr(EDIT_CHANGED_EXTERNALLY));
  3662. if (MessageDialog(Message, qtConfirmation, qaOK | qaCancel) != qaOK)
  3663. {
  3664. Abort();
  3665. }
  3666. }
  3667. }
  3668. }
  3669. TStrings * FileList = new TStringList();
  3670. try
  3671. {
  3672. FileList->Add(FileName);
  3673. // Consider using the same settings (preset) as when the file was downloaded.
  3674. // More over this does not reflect the actual session that will do the upload.
  3675. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  3676. TemporaryFileCopyParam(CopyParam);
  3677. if (Data->ForceText)
  3678. {
  3679. CopyParam.TransferMode = tmAscii;
  3680. }
  3681. // so i do not need to worry if masking algorithm works in all cases
  3682. // ("" means "copy file name", no masking is actually done)
  3683. if (ExtractFileName(FileName) == Data->OriginalFileName)
  3684. {
  3685. CopyParam.FileMask = L"";
  3686. }
  3687. else
  3688. {
  3689. CopyParam.FileMask = DelimitFileNameMask(Data->OriginalFileName);
  3690. }
  3691. int Params = cpNoConfirmation | cpTemporary;
  3692. if (Data->Terminal->IsCapable[fcBackgroundTransfers])
  3693. {
  3694. DebugAssert(Data->Queue != NULL);
  3695. TQueueItem * QueueItem = new TUploadQueueItem(Data->Terminal, FileList,
  3696. Data->RemoteDirectory, &CopyParam, Params, true, false);
  3697. QueueItem->CompleteEvent = UploadCompleteEvent;
  3698. AddQueueItem(Data->Queue, QueueItem, Data->Terminal);
  3699. }
  3700. else
  3701. {
  3702. if (NonVisualDataModule->Busy)
  3703. {
  3704. Retry = true;
  3705. }
  3706. else
  3707. {
  3708. Data->Terminal->CopyToRemote(FileList, Data->RemoteDirectory, &CopyParam, Params, NULL);
  3709. SetEvent(UploadCompleteEvent);
  3710. }
  3711. }
  3712. }
  3713. __finally
  3714. {
  3715. delete FileList;
  3716. }
  3717. }
  3718. //---------------------------------------------------------------------------
  3719. void __fastcall TCustomScpExplorerForm::ExecutedFileReload(
  3720. const UnicodeString & FileName, TEditedFileData * Data)
  3721. {
  3722. // Sanity check, we should not be busy otherwise user would not be able to click Reload button.
  3723. DebugAssert(!NonVisualDataModule->Busy);
  3724. if (!IsActiveTerminal(Data->Terminal))
  3725. {
  3726. throw Exception(FMTLOAD(EDIT_SESSION_CLOSED_RELOAD,
  3727. (ExtractFileName(FileName), Data->SessionName)));
  3728. }
  3729. TManagedTerminal * PrevSession = TTerminalManager::Instance()->ActiveSession;
  3730. TTerminalManager::Instance()->ActiveSession = Data->Terminal;
  3731. NonVisualDataModule->StartBusy();
  3732. try
  3733. {
  3734. std::unique_ptr<TRemoteFile> File;
  3735. UnicodeString RemoteFileName =
  3736. UnixIncludeTrailingBackslash(Data->RemoteDirectory) + Data->OriginalFileName;
  3737. Terminal->ExceptionOnFail = true;
  3738. try
  3739. {
  3740. File.reset(Terminal->ReadFile(RemoteFileName));
  3741. if (!File->HaveFullFileName)
  3742. {
  3743. File->FullFileName = RemoteFileName;
  3744. }
  3745. }
  3746. __finally
  3747. {
  3748. Terminal->ExceptionOnFail = false;
  3749. }
  3750. std::unique_ptr<TStrings> FileList(new TStringList());
  3751. FileList->AddObject(RemoteFileName, File.get());
  3752. UnicodeString RootTempDir = Data->LocalRootDirectory;
  3753. UnicodeString TempDir = ExtractFilePath(FileName);
  3754. TemporarilyDownloadFiles(FileList.get(), Data->ForceText, RootTempDir, TempDir, true, true, false);
  3755. if (EditorCheckNotModified(Data))
  3756. {
  3757. Data->SourceTimestamp = File->Modification;
  3758. }
  3759. // sanity check, the target file name should be still the same
  3760. DebugAssert(ExtractFileName(FileName) == FileList->Strings[0]);
  3761. }
  3762. __finally
  3763. {
  3764. NonVisualDataModule->EndBusy();
  3765. // it actually may not exist anymore...
  3766. TTerminalManager::Instance()->ActiveSession = PrevSession;
  3767. }
  3768. }
  3769. //---------------------------------------------------------------------------
  3770. void __fastcall TCustomScpExplorerForm::ExecutedFileEarlyClosed(
  3771. const TEditedFileData * Data, bool & KeepOpen)
  3772. {
  3773. // Command is set for external editors only (not for "shell" open).
  3774. if (!Data->Command.IsEmpty())
  3775. {
  3776. bool AnyFound = false;
  3777. bool AnyMDI = false;
  3778. bool AnyNonMDI = false;
  3779. bool AnyDetect = false;
  3780. TEditorList * EditorList = new TEditorList();
  3781. try
  3782. {
  3783. *EditorList = *WinConfiguration->EditorList;
  3784. for (int i = 0; i < EditorList->Count; i++)
  3785. {
  3786. const TEditorPreferences * Editor = EditorList->Editors[i];
  3787. if ((Editor->Data->Editor == edExternal) &&
  3788. (Editor->Data->ExternalEditor == Data->Command))
  3789. {
  3790. AnyFound = true;
  3791. if (Editor->Data->SDIExternalEditor)
  3792. {
  3793. AnyNonMDI = true;
  3794. if (Editor->Data->DetectMDIExternalEditor)
  3795. {
  3796. AnyDetect = true;
  3797. }
  3798. }
  3799. else
  3800. {
  3801. AnyMDI = true;
  3802. }
  3803. }
  3804. }
  3805. bool EnableMDI = false;
  3806. bool DisableDetect = false;
  3807. if (AnyMDI)
  3808. {
  3809. KeepOpen = true;
  3810. if (AnyNonMDI)
  3811. {
  3812. // there is at least one instance of the editor with MDI support enabled,
  3813. // and one with disabled, enable it for all instances
  3814. EnableMDI = true;
  3815. }
  3816. }
  3817. else if (AnyFound && !AnyDetect)
  3818. {
  3819. // at least once instance found but all have MDI autodetection disabled
  3820. // => close the file (default action)
  3821. }
  3822. else
  3823. {
  3824. // no instance of the editor has MDI support enabled
  3825. TMessageParams Params;
  3826. if (AnyFound)
  3827. {
  3828. // there is at least one instance of the editor
  3829. Params.Params |= mpNeverAskAgainCheck;
  3830. }
  3831. unsigned int Answer = MessageDialog(FMTLOAD(EDITOR_EARLY_CLOSED2, (Data->OriginalFileName)), qtWarning,
  3832. qaYes | qaNo, HELP_EDITOR_EARLY_CLOSED, &Params);
  3833. switch (Answer)
  3834. {
  3835. case qaNeverAskAgain:
  3836. DisableDetect = true;
  3837. break;
  3838. case qaNo:
  3839. EnableMDI = true;
  3840. KeepOpen = true;
  3841. break;
  3842. }
  3843. }
  3844. if (AnyFound && (EnableMDI || DisableDetect))
  3845. {
  3846. bool Changed = false;
  3847. for (int i = 0; i < EditorList->Count; i++)
  3848. {
  3849. const TEditorPreferences * Editor = EditorList->Editors[i];
  3850. if ((Editor->Data->Editor == edExternal) &&
  3851. (Editor->Data->ExternalEditor == Data->Command) &&
  3852. ((EnableMDI && Editor->Data->SDIExternalEditor) ||
  3853. (DisableDetect && Editor->Data->DetectMDIExternalEditor)))
  3854. {
  3855. Changed = true;
  3856. TEditorPreferences * UpdatedEditor = new TEditorPreferences(*Editor);
  3857. if (EnableMDI)
  3858. {
  3859. UpdatedEditor->GetData()->SDIExternalEditor = false;
  3860. }
  3861. if (DisableDetect)
  3862. {
  3863. UpdatedEditor->GetData()->DetectMDIExternalEditor = false;
  3864. }
  3865. EditorList->Change(i, UpdatedEditor);
  3866. }
  3867. }
  3868. if (Changed)
  3869. {
  3870. WinConfiguration->EditorList = EditorList;
  3871. }
  3872. }
  3873. }
  3874. __finally
  3875. {
  3876. delete EditorList;
  3877. }
  3878. }
  3879. else
  3880. {
  3881. // "open" case
  3882. MessageDialog(FMTLOAD(APP_EARLY_CLOSED, (Data->OriginalFileName)), qtWarning,
  3883. qaOK, HELP_APP_EARLY_CLOSED);
  3884. }
  3885. }
  3886. //---------------------------------------------------------------------------
  3887. void __fastcall TCustomScpExplorerForm::ExecutedFileUploadComplete(TEditedFileData * Data, TObject * Sender)
  3888. {
  3889. if (EditorCheckNotModified(Data))
  3890. {
  3891. UnicodeString RemoteFilePath = UnixCombinePaths(Data->RemoteDirectory, Data->OriginalFileName);
  3892. std::unique_ptr<TRemoteFile> File(Terminal->TryReadFile(RemoteFilePath));
  3893. if (File.get() != NULL)
  3894. {
  3895. Data->SourceTimestamp = File->Modification;
  3896. AppLogFmt(L"Uploaded edited remote file timestamp: %s: ", (StandardTimestamp(Data->SourceTimestamp)));
  3897. }
  3898. }
  3899. EditorFormFileUploadComplete(DebugNotNull(dynamic_cast<TForm *>(Sender)));
  3900. }
  3901. //---------------------------------------------------------------------------
  3902. void __fastcall TCustomScpExplorerForm::RemoteDirViewEnter(TObject * /*Sender*/)
  3903. {
  3904. SideEnter(osRemote);
  3905. }
  3906. //---------------------------------------------------------------------------
  3907. void __fastcall TCustomScpExplorerForm::RemoteDriveViewEnter(TObject * /*Sender*/)
  3908. {
  3909. MakeNextInTabOrder(RemoteDirPanel, RemoteDrivePanel);
  3910. SideEnter(osRemote);
  3911. }
  3912. //---------------------------------------------------------------------------
  3913. void __fastcall TCustomScpExplorerForm::SideEnter(TOperationSide Side)
  3914. {
  3915. FCurrentSide = Side;
  3916. if (Visible)
  3917. {
  3918. UpdateControls();
  3919. }
  3920. }
  3921. //---------------------------------------------------------------------------
  3922. void __fastcall TCustomScpExplorerForm::FileDeleted(TOperationSide Side, const UnicodeString & FileName, bool Success)
  3923. {
  3924. VisualiseOperationFinished(Side, FileName, Success);
  3925. if (DebugAlwaysTrue(FDeletedFiles != NULL) && Success)
  3926. {
  3927. FDeletedFiles->Add(FileName);
  3928. }
  3929. }
  3930. //---------------------------------------------------------------------------
  3931. void __fastcall TCustomScpExplorerForm::DeleteFiles(TOperationSide Side,
  3932. TStrings * FileList, bool Alternative)
  3933. {
  3934. TCustomDirView * DView = DirView(Side);
  3935. DView->SaveSelection();
  3936. DView->SaveSelectedNames();
  3937. DebugAssert(!FAlternativeDelete);
  3938. FAlternativeDelete = Alternative;
  3939. try
  3940. {
  3941. if (!IsSideLocalBrowser(Side))
  3942. {
  3943. DebugAssert(Terminal != NULL);
  3944. // Clone the file list as it may refer to current directory files, which get destroyed, when the directory is reloaded after the operation
  3945. std::unique_ptr<TStrings> PermanentFileList(TRemoteFileList::CloneStrings(FileList));
  3946. std::unique_ptr<TStringList> DeletedFiles(new TStringList());
  3947. DeletedFiles->CaseSensitive = DebugAlwaysTrue(!IsSideLocalBrowser(Side));
  3948. int Params = FLAGMASK(Alternative, dfAlternative);
  3949. try
  3950. {
  3951. TValueRestorer<TStrings *> DeletedFilesRestorer(FDeletedFiles);
  3952. DebugAssert(FDeletedFiles == NULL);
  3953. FDeletedFiles = DeletedFiles.get();
  3954. TValueRestorer<TFileOperationFinishedEvent> OnFileOperationFinishedRestorer(FOnFileOperationFinished);
  3955. FOnFileOperationFinished = FileDeleted;
  3956. FMoveToQueue = false;
  3957. Terminal->DeleteFiles(FileList, Params);
  3958. // Probably not needed for deleting, just for consistency with transfer code
  3959. if (FMoveToQueue)
  3960. {
  3961. Abort();
  3962. }
  3963. }
  3964. catch (EAbort &)
  3965. {
  3966. if (FMoveToQueue)
  3967. {
  3968. DeletedFiles->Sorted = true;
  3969. for (int Index = 0; Index < PermanentFileList->Count; Index++)
  3970. {
  3971. if (DeletedFiles->IndexOf(PermanentFileList->Strings[Index]) >= 0)
  3972. {
  3973. // We should always be deleting the first item => what can be used to optimize this code, if needed
  3974. DebugAssert(Index == 0);
  3975. PermanentFileList->Delete(Index);
  3976. Index--;
  3977. }
  3978. }
  3979. Configuration->Usage->Inc("MovesToBackgroundDelete");
  3980. TQueueItem * QueueItem = new TDeleteQueueItem(Terminal, PermanentFileList.get(), Params);
  3981. AddQueueItem(Queue, QueueItem, Terminal);
  3982. ClearOperationSelection(osRemote);
  3983. }
  3984. }
  3985. }
  3986. else
  3987. {
  3988. if (IsLocalBrowserMode())
  3989. {
  3990. Configuration->Usage->Inc(L"LocalLocalDeletes");
  3991. }
  3992. TValueRestorer<TOperationSide> ProgressSideRestorer(FProgressSide);
  3993. FProgressSide = FCurrentSide;
  3994. try
  3995. {
  3996. TTerminalManager::Instance()->LocalTerminal->DeleteLocalFiles(FileList, FLAGMASK(Alternative, dfAlternative));
  3997. }
  3998. __finally
  3999. {
  4000. ReloadLocalDirectory();
  4001. }
  4002. }
  4003. FAlternativeDelete = false;
  4004. }
  4005. catch(...)
  4006. {
  4007. FAlternativeDelete = false;
  4008. DView->DiscardSavedSelection();
  4009. throw;
  4010. }
  4011. DView->RestoreSelection();
  4012. }
  4013. //---------------------------------------------------------------------------
  4014. void __fastcall TCustomScpExplorerForm::LockFiles(TStrings * FileList, bool Lock)
  4015. {
  4016. DebugAssert(Terminal);
  4017. DebugAssert(!IsLocalBrowserMode());
  4018. RemoteDirView->SaveSelection();
  4019. RemoteDirView->SaveSelectedNames();
  4020. try
  4021. {
  4022. if (Lock)
  4023. {
  4024. Terminal->LockFiles(FileList);
  4025. }
  4026. else
  4027. {
  4028. Terminal->UnlockFiles(FileList);
  4029. }
  4030. }
  4031. catch(...)
  4032. {
  4033. RemoteDirView->DiscardSavedSelection();
  4034. throw;
  4035. }
  4036. RemoteDirView->RestoreSelection();
  4037. }
  4038. //---------------------------------------------------------------------------
  4039. bool __fastcall TCustomScpExplorerForm::RemoteTransferDialog(TManagedTerminal *& Session,
  4040. TStrings * FileList, UnicodeString & Target, UnicodeString & FileMask, bool & DirectCopy,
  4041. bool NoConfirmation, bool Move)
  4042. {
  4043. DebugAssert(Terminal != NULL);
  4044. DebugAssert(Terminal == TTerminalManager::Instance()->ActiveTerminal);
  4045. DebugAssert(!IsLocalBrowserMode());
  4046. if (Session == NULL)
  4047. {
  4048. Session = Terminal;
  4049. }
  4050. if (Session == Terminal)
  4051. {
  4052. if (RemoteDriveView->DropTarget != NULL)
  4053. {
  4054. Target = RemoteDriveView->NodePathName(RemoteDriveView->DropTarget);
  4055. }
  4056. else if (RemoteDirView->DropTarget != NULL)
  4057. {
  4058. DebugAssert(RemoteDirView->ItemIsDirectory(RemoteDirView->DropTarget));
  4059. Target = RemoteDirView->ItemFullFileName(RemoteDirView->DropTarget);
  4060. }
  4061. else
  4062. {
  4063. Target = RemoteDirView->Path;
  4064. }
  4065. }
  4066. else
  4067. {
  4068. Target = GetSessionPath(Session, osRemote);
  4069. }
  4070. Target = UnixIncludeTrailingBackslash(Target);
  4071. if (FileList->Count == 1)
  4072. {
  4073. FileMask = DelimitFileNameMask(UnixExtractFileName(FileList->Strings[0]));
  4074. }
  4075. else
  4076. {
  4077. FileMask = AnyMask;
  4078. }
  4079. DirectCopy = Terminal->IsCapable[fcRemoteCopy] || Terminal->IsCapable[fcSecondaryShell];
  4080. bool Result = true;
  4081. if (!NoConfirmation)
  4082. {
  4083. bool Multi = (FileList->Count > 1);
  4084. if (Move)
  4085. {
  4086. Result = DoRemoteMoveDialog(Multi, Target, FileMask);
  4087. }
  4088. else
  4089. {
  4090. std::unique_ptr<TStrings> Sessions(new TStringList);
  4091. std::unique_ptr<TStrings> Directories(new TStringList);
  4092. TStrings * SessionList = TTerminalManager::Instance()->SessionList;
  4093. for (int Index = 0; Index < SessionList->Count; Index++)
  4094. {
  4095. TManagedTerminal * ASession = DebugNotNull(dynamic_cast<TManagedTerminal *>(SessionList->Objects[Index]));
  4096. if (IsActiveTerminal(ASession) && DebugAlwaysTrue(!ASession->LocalBrowser))
  4097. {
  4098. Sessions->AddObject(SessionList->Strings[Index], ASession);
  4099. Directories->Add(GetSessionPath(ASession, osRemote));
  4100. }
  4101. }
  4102. TDirectRemoteCopy AllowDirectCopy;
  4103. if (Terminal->IsCapable[fcRemoteCopy] || Terminal->CommandSessionOpened)
  4104. {
  4105. DebugAssert(DirectCopy);
  4106. AllowDirectCopy = drcAllow;
  4107. }
  4108. else if (Terminal->IsCapable[fcSecondaryShell])
  4109. {
  4110. DebugAssert(DirectCopy);
  4111. AllowDirectCopy = drcConfirmCommandSession;
  4112. }
  4113. else
  4114. {
  4115. DebugAssert(!DirectCopy);
  4116. AllowDirectCopy = drcDisallow;
  4117. }
  4118. void * ASession = Session;
  4119. Result = DoRemoteCopyDialog(
  4120. Sessions.get(), Directories.get(), AllowDirectCopy, Multi, ASession, Target, FileMask, DirectCopy, Terminal);
  4121. Session = static_cast<TManagedTerminal *>(ASession);
  4122. }
  4123. }
  4124. return Result;
  4125. }
  4126. //---------------------------------------------------------------------------
  4127. bool __fastcall TCustomScpExplorerForm::RemoteTransferFiles(
  4128. TStrings * FileList, bool NoConfirmation, bool Move, TManagedTerminal * Session)
  4129. {
  4130. DebugAssert(!IsLocalBrowserMode());
  4131. bool DirectCopy;
  4132. UnicodeString Target, FileMask;
  4133. bool Result = RemoteTransferDialog(Session, FileList, Target, FileMask, DirectCopy, NoConfirmation, Move);
  4134. if (Result)
  4135. {
  4136. if (!Move && !DirectCopy)
  4137. {
  4138. Configuration->Usage->Inc("RemoteCopyTemp");
  4139. UnicodeString RootTempDir;
  4140. UnicodeString TempDir;
  4141. TemporarilyDownloadFiles(FileList, false, RootTempDir, TempDir, false, false, true);
  4142. TStrings * TemporaryFilesList = new TStringList();
  4143. try
  4144. {
  4145. TMakeLocalFileListParams MakeFileListParam;
  4146. MakeFileListParam.FileList = TemporaryFilesList;
  4147. MakeFileListParam.FileTimes = NULL;
  4148. MakeFileListParam.IncludeDirs = true;
  4149. MakeFileListParam.Recursive = false;
  4150. ProcessLocalDirectory(TempDir, Terminal->MakeLocalFileList, &MakeFileListParam);
  4151. TTerminalManager::Instance()->ActiveSession = Session;
  4152. if (TemporaryFilesList->Count > 0)
  4153. {
  4154. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  4155. CopyParam.FileMask = FileMask;
  4156. DebugAssert(!FAutoOperation);
  4157. FAutoOperation = true;
  4158. Terminal->CopyToRemote(TemporaryFilesList, Target, &CopyParam, cpTemporary, NULL);
  4159. }
  4160. }
  4161. __finally
  4162. {
  4163. delete TemporaryFilesList;
  4164. FAutoOperation = false;
  4165. if (!RootTempDir.IsEmpty())
  4166. {
  4167. RecursiveDeleteFile(ExcludeTrailingBackslash(RootTempDir), false);
  4168. }
  4169. }
  4170. }
  4171. else
  4172. {
  4173. RemoteDirView->SaveSelection();
  4174. RemoteDirView->SaveSelectedNames();
  4175. try
  4176. {
  4177. if (Move)
  4178. {
  4179. Configuration->Usage->Inc("RemoteMove");
  4180. Terminal->MoveFiles(FileList, Target, FileMask, false);
  4181. }
  4182. else
  4183. {
  4184. Configuration->Usage->Inc("RemoteCopyDirect");
  4185. DebugAssert(DirectCopy);
  4186. DebugAssert(Session == Terminal);
  4187. if (Terminal->IsCapable[fcRemoteCopy] ||
  4188. Terminal->CommandSessionOpened ||
  4189. CommandSessionFallback())
  4190. {
  4191. Terminal->CopyFiles(FileList, Target, FileMask);
  4192. }
  4193. }
  4194. }
  4195. catch(...)
  4196. {
  4197. RemoteDirView->DiscardSavedSelection();
  4198. throw;
  4199. }
  4200. RemoteDirView->RestoreSelection();
  4201. }
  4202. }
  4203. return Result;
  4204. }
  4205. //---------------------------------------------------------------------------
  4206. void __fastcall TCustomScpExplorerForm::CreateRemoteDirectory(
  4207. const UnicodeString & Path, TRemoteProperties & Properties)
  4208. {
  4209. DebugAssert(!IsLocalBrowserMode());
  4210. Properties.Valid = Properties.Valid << vpEncrypt;
  4211. Properties.Encrypt = GUIConfiguration->CurrentCopyParam.EncryptNewFiles;
  4212. RemoteDirView->CreateDirectoryEx(Path, &Properties);
  4213. }
  4214. //---------------------------------------------------------------------------
  4215. void __fastcall TCustomScpExplorerForm::CreateDirectory(TOperationSide Side)
  4216. {
  4217. Side = GetSide(Side);
  4218. TRemoteProperties Properties = GUIConfiguration->NewDirectoryProperties;
  4219. TRemoteProperties * AProperties = (!IsSideLocalBrowser(Side) ? &Properties : NULL);
  4220. UnicodeString Name = LoadStr(NEW_FOLDER);
  4221. int AllowedChanges =
  4222. FLAGMASK(!IsSideLocalBrowser(Side) && Terminal->IsCapable[fcModeChanging], cpMode);
  4223. bool SaveSettings = false;
  4224. if (DoCreateDirectoryDialog(Name, AProperties, AllowedChanges, SaveSettings))
  4225. {
  4226. TWindowLock Lock(this);
  4227. if (!IsSideLocalBrowser(Side))
  4228. {
  4229. if (SaveSettings)
  4230. {
  4231. GUIConfiguration->NewDirectoryProperties = Properties;
  4232. }
  4233. CreateRemoteDirectory(Name, Properties);
  4234. }
  4235. else
  4236. {
  4237. DirView(Side)->CreateDirectory(Name);
  4238. }
  4239. }
  4240. }
  4241. //---------------------------------------------------------------------------
  4242. void __fastcall TCustomScpExplorerForm::HomeDirectory(TOperationSide Side)
  4243. {
  4244. TWindowLock Lock(this);
  4245. DirView(Side)->ExecuteHomeDirectory();
  4246. }
  4247. //---------------------------------------------------------------------------
  4248. void __fastcall TCustomScpExplorerForm::OpenDirectory(TOperationSide Side)
  4249. {
  4250. DoOpenDirectoryDialog(odBrowse, Side);
  4251. }
  4252. //---------------------------------------------------------------------------
  4253. bool __fastcall TCustomScpExplorerForm::OpenBookmark(TOperationSide Side, TBookmark * Bookmark)
  4254. {
  4255. TOperationSide BookmarkSide = IsLocalBrowserMode() ? osLocal : Side;
  4256. UnicodeString Path = Bookmark->GetSideDirectory(BookmarkSide);
  4257. bool Result = !Path.IsEmpty();
  4258. if (Result)
  4259. {
  4260. TryOpenDirectory(Side, Path);
  4261. }
  4262. return Result;
  4263. }
  4264. //---------------------------------------------------------------------------
  4265. void __fastcall TCustomScpExplorerForm::CalculateSize(
  4266. TStrings * FileList, __int64 & Size, TCalculateSizeStats & Stats,
  4267. bool & Close)
  4268. {
  4269. // terminal can be already closed (e.g. dropped connection)
  4270. if (Terminal != NULL)
  4271. {
  4272. try
  4273. {
  4274. TCalculateSizeParams Params;
  4275. Params.Stats = &Stats;
  4276. Terminal->CalculateFilesSize(FileList, Size, Params);
  4277. }
  4278. catch(...)
  4279. {
  4280. if (!Terminal->Active)
  4281. {
  4282. Close = true;
  4283. }
  4284. throw;
  4285. }
  4286. }
  4287. }
  4288. //---------------------------------------------------------------------------
  4289. void __fastcall TCustomScpExplorerForm::CalculateChecksum(const UnicodeString & Alg,
  4290. TStrings * FileList, TCalculatedChecksumEvent OnCalculatedChecksum,
  4291. bool & Close)
  4292. {
  4293. if ((Terminal != NULL) && // terminal can be already closed (e.g. dropped connection)
  4294. EnsureCommandSessionFallback(fcCalculatingChecksum))
  4295. {
  4296. Configuration->Usage->Inc(L"ChecksumCalculated");
  4297. try
  4298. {
  4299. Terminal->CalculateFilesChecksum(Alg, FileList, OnCalculatedChecksum);
  4300. }
  4301. catch(...)
  4302. {
  4303. if (!Terminal->Active)
  4304. {
  4305. Close = true;
  4306. }
  4307. throw;
  4308. }
  4309. }
  4310. }
  4311. //---------------------------------------------------------------------------
  4312. void TCustomScpExplorerForm::LoadFilesProperties(TStrings * FileList)
  4313. {
  4314. if (Terminal->LoadFilesProperties(FileList))
  4315. {
  4316. RemoteDirView->Invalidate();
  4317. }
  4318. }
  4319. //---------------------------------------------------------------------------
  4320. bool __fastcall TCustomScpExplorerForm::SetProperties(TOperationSide Side, TStrings * FileList)
  4321. {
  4322. bool Result;
  4323. if (!IsSideLocalBrowser(Side))
  4324. {
  4325. TRemoteTokenList * GroupList = NULL;
  4326. TRemoteTokenList * UserList = NULL;
  4327. try
  4328. {
  4329. TRemoteProperties CurrentProperties;
  4330. LoadFilesProperties(FileList);
  4331. bool CapableGroupChanging = Terminal->IsCapable[fcGroupChanging];
  4332. bool CapableOwnerChanging = Terminal->IsCapable[fcOwnerChanging];
  4333. if (CapableGroupChanging || CapableOwnerChanging)
  4334. {
  4335. if (CapableGroupChanging)
  4336. {
  4337. GroupList = Terminal->Groups->Duplicate();
  4338. }
  4339. if (CapableOwnerChanging)
  4340. {
  4341. UserList = Terminal->Users->Duplicate();
  4342. }
  4343. TRemoteDirectory * Files = Terminal->Files;
  4344. int Count = Files->Count;
  4345. if (Count > 100)
  4346. {
  4347. Count = 100;
  4348. }
  4349. for (int Index = 0; Index < Count; Index++)
  4350. {
  4351. TRemoteFile * File = Files->Files[Index];
  4352. if (CapableGroupChanging)
  4353. {
  4354. GroupList->AddUnique(File->Group);
  4355. }
  4356. if (CapableOwnerChanging)
  4357. {
  4358. UserList->AddUnique(File->Owner);
  4359. }
  4360. }
  4361. // if we haven't collected tokens for all files in current directory,
  4362. // make sure we collect them at least for all selected files.
  4363. // (note that so far the files in FileList has to be from current direcotry)
  4364. if (Count < Files->Count)
  4365. {
  4366. for (int Index = 0; Index < FileList->Count; Index++)
  4367. {
  4368. TRemoteFile * File = (TRemoteFile *)(FileList->Objects[Index]);
  4369. if (CapableGroupChanging)
  4370. {
  4371. GroupList->AddUnique(File->Group);
  4372. }
  4373. if (CapableOwnerChanging)
  4374. {
  4375. UserList->AddUnique(File->Owner);
  4376. }
  4377. }
  4378. }
  4379. }
  4380. CurrentProperties = TRemoteProperties::CommonProperties(FileList);
  4381. bool CapableAclChanging = Terminal->IsCapable[fcAclChangingFiles];
  4382. for (int Index = 0; (Index < FileList->Count) && CapableAclChanging; Index++)
  4383. {
  4384. if (dynamic_cast<TRemoteFile *>(FileList->Objects[Index])->IsDirectory)
  4385. {
  4386. CapableAclChanging = false;
  4387. CurrentProperties.Valid = CurrentProperties.Valid >> vpRights;
  4388. }
  4389. }
  4390. int Flags = 0;
  4391. if (Terminal->IsCapable[fcModeChanging]) Flags |= cpMode;
  4392. if (CapableAclChanging) Flags |= cpAcl;
  4393. if (CapableOwnerChanging) Flags |= cpOwner;
  4394. if (CapableGroupChanging) Flags |= cpGroup;
  4395. TCalculateChecksumEvent CalculateChecksumEvent = NULL;
  4396. if (Terminal->IsCapable[fcCalculatingChecksum] ||
  4397. (Terminal->IsCapable[fcSecondaryShell] && !Terminal->IsEncryptingFiles()))
  4398. {
  4399. CalculateChecksumEvent = CalculateChecksum;
  4400. }
  4401. std::unique_ptr<TStrings> ChecksumAlgs(new TStringList());
  4402. Terminal->GetSupportedChecksumAlgs(ChecksumAlgs.get());
  4403. TRemoteProperties NewProperties = CurrentProperties;
  4404. Result =
  4405. DoPropertiesDialog(FileList, RemoteDirView->PathName,
  4406. GroupList, UserList, ChecksumAlgs.get(), &NewProperties, Flags,
  4407. Terminal->IsCapable[fcGroupOwnerChangingByID],
  4408. CalculateSize, CalculateChecksumEvent);
  4409. if (Result)
  4410. {
  4411. NewProperties = TRemoteProperties::ChangedProperties(CurrentProperties, NewProperties);
  4412. Terminal->ChangeFilesProperties(FileList, &NewProperties);
  4413. }
  4414. }
  4415. __finally
  4416. {
  4417. delete GroupList;
  4418. delete UserList;
  4419. }
  4420. }
  4421. else
  4422. {
  4423. DirView(Side)->DisplayPropertiesMenu();
  4424. Result = true;
  4425. }
  4426. return Result;
  4427. }
  4428. //---------------------------------------------------------------------------
  4429. void __fastcall TCustomScpExplorerForm::KeyProcessed(Word & Key, TShiftState Shift)
  4430. {
  4431. if (Shift * AllKeyShiftStates() == (TShiftState() << ssAlt))
  4432. {
  4433. FIgnoreNextDialogChar = Key;
  4434. }
  4435. Key = 0;
  4436. }
  4437. //---------------------------------------------------------------------------
  4438. void __fastcall TCustomScpExplorerForm::CheckCustomCommandShortCut(
  4439. TCustomCommandList * List, Word & Key, Classes::TShiftState Shift, TShortCut KeyShortCut)
  4440. {
  4441. const TCustomCommandType * Command = List->Find(KeyShortCut);
  4442. if (Command != NULL)
  4443. {
  4444. KeyProcessed(Key, Shift);
  4445. if (CustomCommandState(*Command, false, ccltAll) > 0)
  4446. {
  4447. TAutoFlag DontCopyCommandToClipboardFlag(DontCopyCommandToClipboard);
  4448. ExecuteFileOperationCommand(foCustomCommand, osRemote,
  4449. false, false, const_cast<TCustomCommandType *>(Command));
  4450. }
  4451. }
  4452. }
  4453. //---------------------------------------------------------------------------
  4454. void __fastcall TCustomScpExplorerForm::KeyDown(Word & Key, Classes::TShiftState Shift)
  4455. {
  4456. if (!NonVisualDataModule->Busy)
  4457. {
  4458. if (QueueView3->Focused() && (QueueView3->OnKeyDown != NULL))
  4459. {
  4460. QueueView3->OnKeyDown(QueueView3, Key, Shift);
  4461. }
  4462. if ((Key != 0) && !DirView(osCurrent)->IsEditing())
  4463. {
  4464. TShortCut KeyShortCut = ShortCut(Key, Shift);
  4465. for (int Index = 0; Index < NonVisualDataModule->ExplorerActions->ActionCount; Index++)
  4466. {
  4467. // Note that handling shortcuts on panel (current control) popup menu has precendence over this.
  4468. // But so far it's not a problem, as it does what we want.
  4469. TAction * Action = (TAction *)NonVisualDataModule->ExplorerActions->Actions[Index];
  4470. if (((Action->ShortCut == KeyShortCut) ||
  4471. (Action->SecondaryShortCuts->IndexOfShortCut(KeyShortCut) >= 0)) &&
  4472. AllowedAction(Action, aaShortCut))
  4473. {
  4474. // Has to be called before the action as the dialog char is already in queue.
  4475. // So when the action consumes message queue, we already need to have the
  4476. // FIgnoreNextDialogChar set
  4477. KeyProcessed(Key, Shift);
  4478. // Reset reference to previous component (when menu/toolbar was clicked).
  4479. // Needed to detect that action was invoked by keyboard shortcut
  4480. // in TNonVisualDataModule::ExplorerActionsExecute
  4481. Action->ActionComponent = NULL;
  4482. Action->Execute();
  4483. return;
  4484. }
  4485. }
  4486. for (int i = 0; i < TTerminalManager::Instance()->Count; i++)
  4487. {
  4488. if (NonVisualDataModule->OpenSessionShortCut(i) == KeyShortCut)
  4489. {
  4490. KeyProcessed(Key, Shift);
  4491. TTerminalManager::Instance()->ActiveSessionIndex = i;
  4492. return;
  4493. }
  4494. }
  4495. if (Key == VK_TAB && Shift.Contains(ssCtrl))
  4496. {
  4497. KeyProcessed(Key, Shift);
  4498. TTerminalManager::Instance()->CycleTerminals(!Shift.Contains(ssShift));
  4499. }
  4500. TShortCut CustomShortCut = NormalizeCustomShortCut(KeyShortCut);
  4501. if (IsCustomShortCut(CustomShortCut))
  4502. {
  4503. CheckCustomCommandShortCut(WinConfiguration->CustomCommandList, Key, Shift, CustomShortCut);
  4504. CheckCustomCommandShortCut(WinConfiguration->ExtensionList, Key, Shift, CustomShortCut);
  4505. if (WinConfiguration->SharedBookmarks != NULL)
  4506. {
  4507. TBookmark * Bookmark = WinConfiguration->SharedBookmarks->FindByShortCut(CustomShortCut);
  4508. if ((Bookmark != NULL) &&
  4509. OpenBookmark(FCurrentSide, Bookmark))
  4510. {
  4511. KeyProcessed(Key, Shift);
  4512. }
  4513. }
  4514. }
  4515. }
  4516. }
  4517. TForm::KeyDown(Key, Shift);
  4518. }
  4519. //---------------------------------------------------------------------------
  4520. void __fastcall TCustomScpExplorerForm::InitStatusBar()
  4521. {
  4522. bool OpenedTerminal = false;
  4523. UnicodeString ProtocolName;
  4524. UnicodeString ProtocolNameHint;
  4525. UnicodeString SecurityProtocolName;
  4526. UnicodeString DurationHint;
  4527. // See the comment in TScpCommanderForm::UpdateControls()
  4528. TTerminal * ActiveTerminal = TTerminalManager::Instance()->ActiveTerminal;
  4529. if (ActiveTerminal != NULL)
  4530. {
  4531. // Active is not enough here
  4532. if (ActiveTerminal->Status == ssOpened)
  4533. {
  4534. const TSessionInfo & SessionInfo = ActiveTerminal->GetSessionInfo();
  4535. const TFileSystemInfo & FileSystemInfo = ActiveTerminal->GetFileSystemInfo();
  4536. if (FileSystemInfo.ProtocolName.IsEmpty())
  4537. {
  4538. ProtocolName = SessionInfo.ProtocolName;
  4539. }
  4540. else
  4541. {
  4542. ProtocolName = FileSystemInfo.ProtocolName;
  4543. }
  4544. ProtocolNameHint = LoadStr(STATUS_PROTOCOL_HINT);
  4545. SecurityProtocolName = SessionInfo.SecurityProtocolName;
  4546. DurationHint = LoadStr(STATUS_DURATION_HINT);
  4547. OpenedTerminal = true;
  4548. }
  4549. else if (ActiveTerminal->Status > ssClosed)
  4550. {
  4551. ProtocolName = LoadStr(STATUS_CONNECTING);
  4552. }
  4553. else
  4554. {
  4555. ProtocolName = LoadStr(STATUS_NOT_CONNECTED2);
  4556. }
  4557. }
  4558. TTBXStatusBar * SessionStatusBar = (TTBXStatusBar *)GetComponent(fcStatusBar);
  4559. int Offset = SessionStatusBar->Panels->Count - SessionPanelCount;
  4560. TTBXStatusPanel * ProtocolStatusPanel = SessionStatusBar->Panels->Items[Offset + 0];
  4561. ProtocolStatusPanel->ViewPriority = (ActiveTerminal != NULL) ? 100 : 0;
  4562. if (ProtocolStatusPanel->Caption != ProtocolName)
  4563. {
  4564. ProtocolStatusPanel->Caption = ProtocolName;
  4565. std::unique_ptr<TCanvas> Canvas(CreateControlCanvas(SessionStatusBar));
  4566. ProtocolStatusPanel->Size = Canvas->TextWidth(ProtocolName) + ScaleByTextHeight(SessionStatusBar, 32);
  4567. }
  4568. ProtocolStatusPanel->Hint = ProtocolNameHint;
  4569. TTBXStatusPanel * SecurityStatusPanel = SessionStatusBar->Panels->Items[Offset + 1];
  4570. SecurityStatusPanel->ViewPriority = OpenedTerminal ? 98 : 0;
  4571. bool SecurityEnabled = !SecurityProtocolName.IsEmpty();
  4572. SecurityStatusPanel->Enabled = SecurityEnabled;
  4573. UnicodeString SecurityProtocolHint;
  4574. if (SecurityEnabled)
  4575. {
  4576. SecurityProtocolHint = FMTLOAD(STATUS_SECURE, (SecurityProtocolName));
  4577. }
  4578. else
  4579. {
  4580. SecurityProtocolHint = LoadStr(STATUS_INSECURE);
  4581. }
  4582. SecurityStatusPanel->Hint = SecurityProtocolHint;
  4583. TTBXStatusPanel * DurationStatusPanel = SessionStatusBar->Panels->Items[Offset + 2];
  4584. DurationStatusPanel->ViewPriority = OpenedTerminal ? 99 : 0;
  4585. DurationStatusPanel->Hint = DurationHint;
  4586. UpdateStatusBar();
  4587. }
  4588. //---------------------------------------------------------------------------
  4589. void __fastcall TCustomScpExplorerForm::UpdateStatusBar()
  4590. {
  4591. TTBXStatusBar * SessionStatusBar = (TTBXStatusBar *)GetComponent(fcStatusBar);
  4592. DebugAssert(SessionStatusBar != NULL);
  4593. TTBXStatusPanel * NoteStatusPanel = SessionStatusBar->Panels->Items[0];
  4594. if (!FNote.IsEmpty())
  4595. {
  4596. NoteStatusPanel->Caption = FNote;
  4597. }
  4598. else
  4599. {
  4600. UpdateStatusPanelText(NoteStatusPanel);
  4601. }
  4602. NoteStatusPanel->Hint = FNoteHints;
  4603. UnicodeString LoginTime;
  4604. // See the comment in TScpCommanderForm::UpdateControls()
  4605. TTerminal * ActiveTerminal = TTerminalManager::Instance()->ActiveTerminal;
  4606. // Active is not enough here
  4607. if ((ActiveTerminal != NULL) && (ActiveTerminal->Status == ssOpened))
  4608. {
  4609. const TSessionInfo & SessionInfo = ActiveTerminal->GetSessionInfo();
  4610. LoginTime = FormatDateTimeSpan(Configuration->TimeFormat, Now() - SessionInfo.LoginTime);
  4611. }
  4612. TTBXStatusPanel * DurationStatusPanel = SessionStatusBar->Panels->Items[SessionStatusBar->Panels->Count - 1];
  4613. DurationStatusPanel->Caption = LoginTime;
  4614. }
  4615. //---------------------------------------------------------------------------
  4616. void __fastcall TCustomScpExplorerForm::UpdateStatusPanelText(TTBXStatusPanel * Panel)
  4617. {
  4618. Panel->Caption = L"";
  4619. }
  4620. //---------------------------------------------------------------------------
  4621. void __fastcall TCustomScpExplorerForm::Idle()
  4622. {
  4623. if (FShowing ||
  4624. // Particularly for
  4625. // - Standalone editing
  4626. // - Detecting closed connection and automatically reconnect it while waiting for changes
  4627. // while "keeping remote directory up to date".
  4628. StandaloneOperation)
  4629. {
  4630. FEditorManager->Check();
  4631. // make sure that Idle is called before update queue, as it may invoke QueueEvent
  4632. // that needs to know if queue view is visible (and it may be closed after queue update)
  4633. TTerminalManager::Instance()->Idle(FDoNotIdleCurrentTerminal > 0);
  4634. }
  4635. if (FShowing)
  4636. {
  4637. if (!NonVisualDataModule->Busy &&
  4638. // Menu is opened or drag&drop is going on
  4639. (Mouse->Capture == NULL))
  4640. {
  4641. if (FRefreshRemoteDirectory)
  4642. {
  4643. if ((Terminal != NULL) && (Terminal->Status == ssOpened))
  4644. {
  4645. Terminal->RefreshDirectory();
  4646. }
  4647. FRefreshRemoteDirectory = false;
  4648. }
  4649. if (FRefreshLocalDirectory)
  4650. {
  4651. ReloadLocalDirectory();
  4652. FRefreshLocalDirectory = false;
  4653. }
  4654. if (WinConfiguration->RefreshRemotePanel)
  4655. {
  4656. if ((Terminal != NULL) && (Terminal->Status == ssOpened) &&
  4657. (Now() - Terminal->DirectoryLoaded > WinConfiguration->RefreshRemotePanelInterval))
  4658. {
  4659. RemoteDirView->ReloadDirectory();
  4660. }
  4661. }
  4662. }
  4663. // TB2 shows accels on Alt key down, but hides it only after menu modal loop finishes.
  4664. // But if Alt is used to switch apps or for keyboard shortcut, the menu modal loop does not happen and accelerators stay displayed.
  4665. // It's difficult to fix this in TB2, as there's no "up" equivalent to CM_DIALOGKEY, which shows the accels.
  4666. // And the Alt key can be released even when other window or application is active, so handling the key up in explorer window won't do either.
  4667. TTBCustomToolbar * MenuToolbar = DebugNotNull(dynamic_cast<TTBCustomToolbar *>(GetComponent(fcMenu)));
  4668. MenuToolbar->Idle();
  4669. }
  4670. if (FShowing || StandaloneOperation)
  4671. {
  4672. if (FQueueStatusInvalidated)
  4673. {
  4674. UpdateQueueStatus(false);
  4675. }
  4676. RefreshQueueItems();
  4677. }
  4678. if (FShowing)
  4679. {
  4680. UpdateStatusBar();
  4681. }
  4682. if (FClipboardFakeMonitorsPendingReset && !FDownloadingFromClipboard)
  4683. {
  4684. FClipboardFakeMonitorsPendingReset = false;
  4685. FClipboardFakeMonitors.reset(NULL);
  4686. }
  4687. FIgnoreNextDialogChar = 0;
  4688. }
  4689. //---------------------------------------------------------------------------
  4690. void __fastcall TCustomScpExplorerForm::UserActionTimer(TObject * /*Sender*/)
  4691. {
  4692. try
  4693. {
  4694. FUserActionTimer->Enabled = false;
  4695. if (IsQueueAutoPopup() && (FPendingQueueActionItem != NULL))
  4696. {
  4697. if (TQueueItem::IsUserActionStatus(FPendingQueueActionItem->Status))
  4698. {
  4699. FPendingQueueActionItem->ProcessUserAction();
  4700. }
  4701. }
  4702. }
  4703. __finally
  4704. {
  4705. FPendingQueueActionItem = NULL;
  4706. }
  4707. }
  4708. //---------------------------------------------------------------------------
  4709. void __fastcall TCustomScpExplorerForm::ApplicationMinimize(TObject * /*Sender*/)
  4710. {
  4711. if (WinConfiguration->MinimizeToTray)
  4712. {
  4713. UpdateTrayIcon();
  4714. FTrayIcon->Visible = true;
  4715. if (Visible)
  4716. {
  4717. ShowWindow(Handle, SW_HIDE);
  4718. }
  4719. }
  4720. }
  4721. //---------------------------------------------------------------------------
  4722. void __fastcall TCustomScpExplorerForm::ApplicationRestore(TObject * /*Sender*/)
  4723. {
  4724. // WORKAROUND
  4725. // When restoring maximized window from minimization,
  4726. // rarely some controls do not align properly.
  4727. // Two instances seen (both for Commander):
  4728. // - When restoring, window is temporarily narrower (not maximized),
  4729. // causing toolbars on TopDock to wrap and dock to expand horizontally.
  4730. // Once maximized already, top dock shinks back, but the session PageControl,
  4731. // do not align up, leaving space between TopDock and PageControl.
  4732. // - Similar issue seen with LocalDirView not aligning down to status bar.
  4733. for (int Index = 0; Index < ControlCount; Index++)
  4734. {
  4735. RealignControl(Controls[Index]);
  4736. }
  4737. if (FTrayIcon->Visible)
  4738. {
  4739. FTrayIcon->Visible = false;
  4740. if (Visible)
  4741. {
  4742. ShowWindow(Handle, SW_SHOW);
  4743. }
  4744. }
  4745. if (FNeedSession && DebugAlwaysTrue(Terminal == NULL))
  4746. {
  4747. FNeedSession = false;
  4748. NonVisualDataModule->StartBusy();
  4749. try
  4750. {
  4751. NeedSession(true);
  4752. }
  4753. __finally
  4754. {
  4755. NonVisualDataModule->EndBusy();
  4756. }
  4757. }
  4758. }
  4759. //---------------------------------------------------------------------------
  4760. void __fastcall TCustomScpExplorerForm::UpdateTrayIcon()
  4761. {
  4762. FTrayIcon->Hint = GetMainForm()->Caption;
  4763. }
  4764. //---------------------------------------------------------------------------
  4765. void __fastcall TCustomScpExplorerForm::ApplicationTitleChanged()
  4766. {
  4767. UpdateTrayIcon();
  4768. }
  4769. //---------------------------------------------------------------------------
  4770. void __fastcall TCustomScpExplorerForm::RestoreApp()
  4771. {
  4772. // workaround
  4773. // TApplication.WndProc sets TApplication.FAppIconic to false,
  4774. // on WM_ACTIVATEAPP, what renders TApplication.Restore no-op function.
  4775. // But WM_ACTIVATEAPP message can be received even when
  4776. // the main window is minimized to the tray and internal editor window is focused
  4777. // (after another application was previously active)
  4778. if (::IsIconic(Handle))
  4779. {
  4780. if (!IsAppIconic())
  4781. {
  4782. SetAppIconic(true);
  4783. }
  4784. }
  4785. ::ApplicationRestore();
  4786. Application->BringToFront();
  4787. }
  4788. //---------------------------------------------------------------------------
  4789. void __fastcall TCustomScpExplorerForm::TrayIconClick(TObject * /*Sender*/)
  4790. {
  4791. RestoreApp();
  4792. }
  4793. //---------------------------------------------------------------------------
  4794. void __fastcall TCustomScpExplorerForm::NewSession(const UnicodeString & SessionUrl)
  4795. {
  4796. if (OpenInNewWindow())
  4797. {
  4798. ExecuteNewInstance(SessionUrl);
  4799. }
  4800. else
  4801. {
  4802. TTerminalManager::Instance()->NewSession(SessionUrl);
  4803. }
  4804. }
  4805. //---------------------------------------------------------------------------
  4806. void TCustomScpExplorerForm::NewTab(TOperationSide, bool)
  4807. {
  4808. NewSession();
  4809. }
  4810. //---------------------------------------------------------------------------
  4811. UnicodeString __fastcall TCustomScpExplorerForm::SaveHiddenDuplicateSession(TSessionData * SessionData)
  4812. {
  4813. UnicodeString SessionName = StoredSessions->HiddenPrefix + ManagedSession->SessionData->SessionName;
  4814. StoredSessions->NewSession(SessionName, SessionData);
  4815. // modified only, explicit
  4816. StoredSessions->Save(false, true);
  4817. // encode session name because of slashes in hierarchical sessions
  4818. return EncodeUrlString(SessionName);
  4819. }
  4820. //---------------------------------------------------------------------------
  4821. UnicodeString __fastcall TCustomScpExplorerForm::CreateHiddenDuplicateSession()
  4822. {
  4823. // current working directories become defaults here, what is not right
  4824. std::unique_ptr<TSessionData> SessionData(CloneCurrentSessionData());
  4825. return SaveHiddenDuplicateSession(SessionData.get());
  4826. }
  4827. //---------------------------------------------------------------------------
  4828. void __fastcall TCustomScpExplorerForm::DuplicateTab()
  4829. {
  4830. if (OpenInNewWindow())
  4831. {
  4832. ExecuteNewInstance(CreateHiddenDuplicateSession());
  4833. }
  4834. else
  4835. {
  4836. // current working directories become defaults here, what is not right
  4837. std::unique_ptr<TSessionData> SessionData(CloneCurrentSessionData());
  4838. TTerminalManager * Manager = TTerminalManager::Instance();
  4839. TManagedTerminal * ASession = Manager->NewManagedTerminal(SessionData.get());
  4840. DebugAssert(ASession->LocalBrowser == ManagedSession->LocalBrowser);
  4841. // We definitelly want these
  4842. ASession->Disconnected = ManagedSession->Disconnected;
  4843. // Not sure about these two
  4844. ASession->Permanent = ManagedSession->Permanent;
  4845. ASession->DisconnectedTemporarily = ManagedSession->DisconnectedTemporarily;
  4846. Manager->ActiveSession = ASession;
  4847. }
  4848. }
  4849. //---------------------------------------------------------------------------
  4850. void __fastcall TCustomScpExplorerForm::RenameTab()
  4851. {
  4852. UnicodeString Name = ManagedSession->SessionData->SessionName;
  4853. if (InputDialog(LoadStr(RENAME_TAB_TITLE), LoadStr(RENAME_TAB_PROMPT), Name, HELP_TAB_RENAME) &&
  4854. // When submitting the default name, do not fix it
  4855. (Name != ManagedSession->SessionData->SessionName))
  4856. {
  4857. // Checks for a slash only, so it's not that big deal that we do the check after submitting the dialog only.
  4858. TSessionData::ValidateName(Name);
  4859. // This is inconsistent with how color (for example) is handled.
  4860. ManagedSession->SessionData->Name = Name;
  4861. UpdateControls();
  4862. // Add/Remove distinguishing paths from sessions of the same name.
  4863. SessionListChanged();
  4864. }
  4865. }
  4866. //---------------------------------------------------------------------------
  4867. bool __fastcall TCustomScpExplorerForm::CanCloseQueue(TTerminalQueue * Queue)
  4868. {
  4869. DebugAssert(Queue != NULL);
  4870. bool Result = Queue->IsEmpty;
  4871. if (!Result)
  4872. {
  4873. SetFocus();
  4874. Result = (MessageDialog(LoadStr(PENDING_QUEUE_ITEMS2), qtWarning, qaOK | qaCancel, HELP_NONE) == qaOK);
  4875. }
  4876. return Result;
  4877. }
  4878. //---------------------------------------------------------------------------
  4879. bool __fastcall TCustomScpExplorerForm::CanCloseQueue()
  4880. {
  4881. return CanCloseQueue(FQueue);
  4882. }
  4883. //---------------------------------------------------------------------------
  4884. void __fastcall TCustomScpExplorerForm::CloseTab()
  4885. {
  4886. if (CanCloseQueue())
  4887. {
  4888. TTerminalManager::Instance()->FreeActiveTerminal();
  4889. }
  4890. }
  4891. //---------------------------------------------------------------------------
  4892. void __fastcall TCustomScpExplorerForm::DisconnectSession()
  4893. {
  4894. if (CanCloseQueue())
  4895. {
  4896. TTerminalManager::Instance()->DisconnectActiveTerminal();
  4897. }
  4898. }
  4899. //---------------------------------------------------------------------------
  4900. void __fastcall TCustomScpExplorerForm::TerminalDisconnected()
  4901. {
  4902. DetachTerminal(Terminal);
  4903. RemoteDirView->Terminal = Terminal;
  4904. UpdateRemotePathComboBox(false);
  4905. InitStatusBar();
  4906. UpdateControls();
  4907. }
  4908. //---------------------------------------------------------------------------
  4909. void __fastcall TCustomScpExplorerForm::TerminalConnecting()
  4910. {
  4911. InitStatusBar();
  4912. }
  4913. //---------------------------------------------------------------------------
  4914. void __fastcall TCustomScpExplorerForm::ReconnectSession()
  4915. {
  4916. if (DebugAlwaysTrue(!Terminal->Active))
  4917. {
  4918. TTerminalManager::Instance()->ReconnectActiveTerminal();
  4919. }
  4920. }
  4921. //---------------------------------------------------------------------------
  4922. void __fastcall TCustomScpExplorerForm::OpenStoredSession(TSessionData * Data)
  4923. {
  4924. if (OpenInNewWindow())
  4925. {
  4926. // encode session name because of slashes in hierarchical sessions
  4927. ExecuteNewInstance(EncodeUrlString(Data->Name));
  4928. }
  4929. else
  4930. {
  4931. TTerminalManager * Manager = TTerminalManager::Instance();
  4932. TManagedTerminal * Terminal = Manager->NewManagedTerminal(Data);
  4933. Manager->ActiveSession = Terminal;
  4934. }
  4935. }
  4936. //---------------------------------------------------------------------------
  4937. void TCustomScpExplorerForm::DoOpenFolderOrWorkspace(
  4938. const UnicodeString & Name, bool ConnectFirstTerminal, bool CheckMaxSessions)
  4939. {
  4940. TTerminalManager * Manager = TTerminalManager::Instance();
  4941. std::unique_ptr<TObjectList> DataList(new TObjectList());
  4942. // This asks for a master password. Maybe we should postpone asking until a connection is actually opened.
  4943. StoredSessions->GetFolderOrWorkspace(Name, DataList.get());
  4944. if (!CheckMaxSessions || (DataList->Count <= WinConfiguration->MaxSessions))
  4945. {
  4946. TManagedTerminal * FirstSession = Manager->NewSessions(DataList.get());
  4947. // FirstSession can be null, if none of the workspace sites exist anymore
  4948. // or if all workspace sessions are not suported by the interface
  4949. // (when trying to open all local-local workspace in "explorer" interface)
  4950. if (FirstSession != NULL)
  4951. {
  4952. if (!ConnectFirstTerminal)
  4953. {
  4954. FirstSession->Disconnected = true;
  4955. FirstSession->DisconnectedTemporarily = true;
  4956. }
  4957. Manager->ActiveSession = FirstSession;
  4958. }
  4959. }
  4960. }
  4961. //---------------------------------------------------------------------------
  4962. void __fastcall TCustomScpExplorerForm::OpenFolderOrWorkspace(const UnicodeString & Name)
  4963. {
  4964. if (OpenInNewWindow())
  4965. {
  4966. ExecuteNewInstance(Name);
  4967. }
  4968. else
  4969. {
  4970. DoOpenFolderOrWorkspace(Name, true, false);
  4971. }
  4972. }
  4973. //---------------------------------------------------------------------------
  4974. void __fastcall TCustomScpExplorerForm::FormCloseQuery(TObject * /*Sender*/,
  4975. bool &CanClose)
  4976. {
  4977. // See the comment in CloseApp()
  4978. if (NonVisualDataModule->Busy)
  4979. {
  4980. CanClose = false;
  4981. }
  4982. else
  4983. {
  4984. TTerminalManager * Manager = TTerminalManager::Instance();
  4985. int ActiveSessions = 0;
  4986. for (int Index = 0; Index < Manager->Count; Index++)
  4987. {
  4988. if (Manager->Sessions[Index]->Active)
  4989. {
  4990. ActiveSessions++;
  4991. }
  4992. }
  4993. // Confirm, when there's at least one active remote session or
  4994. // when there are multiple (inactive or local) sessions and auto workspace saving is not enabled.
  4995. if (((ActiveSessions > 0) || ((Manager->Count > 1) && !WinConfiguration->AutoSaveWorkspace)) &&
  4996. WinConfiguration->ConfirmClosingSession)
  4997. {
  4998. unsigned int Result;
  4999. TMessageParams Params(mpNeverAskAgainCheck);
  5000. UnicodeString Message;
  5001. int Answers = qaOK | qaCancel;
  5002. // The current session is the only active session
  5003. if ((ActiveSessions == 1) && (Terminal != NULL) && Terminal->Active)
  5004. {
  5005. if (!WinConfiguration->AutoSaveWorkspace)
  5006. {
  5007. Message = LoadStr(CLOSE_SESSION_WORKSPACE);
  5008. }
  5009. else
  5010. {
  5011. Message = LoadStr(CLOSE_SESSION);
  5012. }
  5013. Message = FORMAT(Message, (Terminal->SessionData->SessionName));
  5014. }
  5015. // Multiple active sessions or one active session, but it's not the current one
  5016. else if (ActiveSessions > 0)
  5017. {
  5018. if (!WinConfiguration->AutoSaveWorkspace)
  5019. {
  5020. Message = LoadStr(CLOSE_SESSIONS_WORKSPACE3);
  5021. }
  5022. else
  5023. {
  5024. Message = LoadStr(CLOSE_SESSIONS);
  5025. }
  5026. }
  5027. // Multiple tabs, but all are inactive or local, and saving of worspace is not enabled
  5028. else
  5029. {
  5030. DebugAlwaysTrue(Manager->Count > 1);
  5031. DebugAlwaysTrue(!WinConfiguration->AutoSaveWorkspace);
  5032. Message = LoadStr(CLOSE_WORKSPACE);
  5033. }
  5034. UnicodeString Note;
  5035. if (WinConfiguration->AutoSaveWorkspace)
  5036. {
  5037. Note = FMTLOAD(AUTO_WORKSPACE, (WorkspaceName()));
  5038. }
  5039. else
  5040. {
  5041. Note = LoadStr(AUTO_WORKSPACE_ENABLE);
  5042. Answers = qaYes | qaNo | qaCancel;
  5043. }
  5044. Message = FORMAT("%s\n\n%s", (MainInstructions(Message), Note));
  5045. SetFocus();
  5046. Result = MessageDialog(Message, qtConfirmation,
  5047. Answers, HELP_CLOSE_SESSION_WORKSPACE, &Params);
  5048. if (Result == qaNeverAskAgain)
  5049. {
  5050. WinConfiguration->ConfirmClosingSession = false;
  5051. }
  5052. if (Result == qaNo)
  5053. {
  5054. CanClose = SaveWorkspace(true);
  5055. // note that the workspace will be saved redundatly again from FormClose
  5056. }
  5057. else
  5058. {
  5059. CanClose =
  5060. (Result == qaOK) ||
  5061. (Result == qaYes) || // CLOSE_SESSIONS_WORKSPACE variant
  5062. (Result == qaNeverAskAgain);
  5063. }
  5064. }
  5065. if (CanClose && HasActiveTerminal())
  5066. {
  5067. CanClose = CanCloseQueue();
  5068. }
  5069. }
  5070. if (CanClose)
  5071. {
  5072. CanClose =
  5073. FEditorManager->CloseInternalEditors(CloseInternalEditor) &&
  5074. FEditorManager->CloseExternalFilesWithoutProcess();
  5075. if (CanClose)
  5076. {
  5077. while (CanClose && (FLocalEditors->Count > 0))
  5078. {
  5079. int PrevCount = FLocalEditors->Count;
  5080. static_cast<TForm *>(FLocalEditors->Items[0])->Close();
  5081. CanClose = (FLocalEditors->Count < PrevCount);
  5082. }
  5083. if (CanClose)
  5084. {
  5085. CanClose = FEditorManager->Empty(true);
  5086. if (!CanClose)
  5087. {
  5088. SetFocus();
  5089. CanClose =
  5090. (MessageDialog(
  5091. LoadStr(PENDING_EDITORS), qtWarning, qaIgnore | qaCancel, HELP_NONE) == qaIgnore);
  5092. }
  5093. }
  5094. }
  5095. }
  5096. }
  5097. //---------------------------------------------------------------------------
  5098. void __fastcall TCustomScpExplorerForm::CloseInternalEditor(TObject * Sender)
  5099. {
  5100. TForm * Form = dynamic_cast<TForm *>(Sender);
  5101. DebugAssert(Form != NULL);
  5102. Form->Close();
  5103. }
  5104. //---------------------------------------------------------------------------
  5105. void __fastcall TCustomScpExplorerForm::ForceCloseInternalEditor(TObject * Sender)
  5106. {
  5107. TForm * Form = dynamic_cast<TForm *>(Sender);
  5108. delete Form;
  5109. }
  5110. //---------------------------------------------------------------------------
  5111. void __fastcall TCustomScpExplorerForm::ForceCloseLocalEditors()
  5112. {
  5113. while (FLocalEditors->Count > 0)
  5114. {
  5115. delete static_cast<TForm *>(FLocalEditors->Items[0]);
  5116. }
  5117. }
  5118. //---------------------------------------------------------------------------
  5119. void __fastcall TCustomScpExplorerForm::RemoteDirViewDisplayProperties(
  5120. TObject *Sender)
  5121. {
  5122. TStrings *FileList = ((TUnixDirView*)Sender)->CreateFileList(True, False, NULL);
  5123. try
  5124. {
  5125. SetProperties(osRemote, FileList);
  5126. }
  5127. __finally
  5128. {
  5129. delete FileList;
  5130. }
  5131. }
  5132. //---------------------------------------------------------------------------
  5133. void __fastcall TCustomScpExplorerForm::ComponentShowing(Byte Component, bool value)
  5134. {
  5135. if (value)
  5136. {
  5137. if (Component == fcCustomCommandsBand)
  5138. {
  5139. UpdateCustomCommandsToolbar();
  5140. }
  5141. if (Component == fcQueueFileList)
  5142. {
  5143. DebugAssert(!QueueFileListSplitter->Visible);
  5144. AdjustQueueLayout();
  5145. UpdateQueueFileList();
  5146. }
  5147. }
  5148. }
  5149. //---------------------------------------------------------------------------
  5150. void __fastcall TCustomScpExplorerForm::AdjustQueueLayout()
  5151. {
  5152. int MinHeightLimit =
  5153. GetStaticQueuePanelComponentsHeight() +
  5154. QueueFileListSplitter->Height +
  5155. QueueFileList->Height +
  5156. GetMinQueueViewHeight();
  5157. if (QueuePanel->ClientHeight < MinHeightLimit)
  5158. {
  5159. QueuePanel->ClientHeight = MinHeightLimit;
  5160. }
  5161. }
  5162. //---------------------------------------------------------------------------
  5163. void __fastcall TCustomScpExplorerForm::SetComponentVisible(Byte Component, Boolean value)
  5164. {
  5165. TControl * Control = GetComponent(Component);
  5166. DebugAssert(Control);
  5167. bool Changed = (Control->Visible != value);
  5168. if (Changed)
  5169. {
  5170. ComponentShowing(Component, value);
  5171. TWinControl * WinControl = dynamic_cast<TWinControl*>(Control);
  5172. bool WasFocused = (WinControl != NULL) && (ActiveControl != NULL) &&
  5173. ((ActiveControl == WinControl) || (ActiveControl->Parent == WinControl));
  5174. if (value)
  5175. {
  5176. int RemainingHeight = Control->Parent->ClientHeight;
  5177. int RemainingWidth = Control->Parent->ClientWidth;
  5178. for (int i = 0; i < Control->Parent->ControlCount; i++)
  5179. {
  5180. TControl * ChildControl = Control->Parent->Controls[i];
  5181. if (ChildControl->Visible)
  5182. {
  5183. switch (ChildControl->Align)
  5184. {
  5185. case alTop:
  5186. case alBottom:
  5187. RemainingHeight -= ChildControl->Height;
  5188. break;
  5189. case alLeft:
  5190. case alRight:
  5191. RemainingWidth -= ChildControl->Width;
  5192. break;
  5193. }
  5194. }
  5195. }
  5196. int Reserve = ScaleByTextHeight(this, 32);
  5197. // queue in explorer, trees in commander
  5198. if (Control->Height > RemainingHeight - Reserve)
  5199. {
  5200. Control->Height = RemainingHeight / 2;
  5201. }
  5202. if (Control->Width > RemainingWidth - Reserve)
  5203. {
  5204. Control->Width = RemainingWidth / 2;
  5205. }
  5206. }
  5207. Control->Visible = value;
  5208. if (WasFocused && Visible)
  5209. {
  5210. DirView(osCurrent)->SetFocus();
  5211. }
  5212. FixControlsPlacement();
  5213. }
  5214. }
  5215. //---------------------------------------------------------------------------
  5216. bool __fastcall TCustomScpExplorerForm::GetComponentVisible(Byte Component)
  5217. {
  5218. TControl * Control = GetComponent(Component);
  5219. if (Control == NULL)
  5220. {
  5221. return false;
  5222. }
  5223. else
  5224. {
  5225. return Control->Visible;
  5226. }
  5227. }
  5228. //---------------------------------------------------------------------------
  5229. bool __fastcall TCustomScpExplorerForm::IsComponentPossible(Byte Component)
  5230. {
  5231. bool Result;
  5232. if ((Component == fcExplorerUpdatesBand) || (Component == fcCommanderUpdatesBand))
  5233. {
  5234. Result = !IsUWP();
  5235. }
  5236. else
  5237. {
  5238. Result = true;
  5239. }
  5240. return Result;
  5241. }
  5242. //---------------------------------------------------------------------------
  5243. void __fastcall TCustomScpExplorerForm::MakeFocusedItemVisible(TCustomDirView * DirView)
  5244. {
  5245. if (DirView->ItemFocused != NULL)
  5246. {
  5247. DirView->ItemFocused->MakeVisible(false);
  5248. }
  5249. }
  5250. //---------------------------------------------------------------------------
  5251. void __fastcall TCustomScpExplorerForm::FixControlsPlacement()
  5252. {
  5253. MakeFocusedItemVisible(RemoteDirView);
  5254. QueueSplitter->Visible = QueuePanel->Visible;
  5255. QueueFileListSplitter->Visible = QueueFileList->Visible;
  5256. RemotePanelSplitter->Visible = RemoteDrivePanel->Visible;
  5257. TControl * TopControlsOrder[] =
  5258. { TopDock, MessageDock, SessionsPageControl };
  5259. SetVerticalControlsOrder(TopControlsOrder, LENOF(TopControlsOrder));
  5260. TControl * QueueControlsOrder[] =
  5261. { QueueDock, QueueView3, QueueFileListSplitter, QueueFileList };
  5262. SetVerticalControlsOrder(QueueControlsOrder, LENOF(QueueControlsOrder));
  5263. }
  5264. //---------------------------------------------------------------------------
  5265. TControl * __fastcall TCustomScpExplorerForm::GetComponent(Byte Component)
  5266. {
  5267. switch (Component) {
  5268. case fcStatusBar: return RemoteStatusBar;
  5269. case fcRemotePopup: return reinterpret_cast<TControl *>(NonVisualDataModule->RemoteFilePopup);
  5270. case fcQueueView: return QueuePanel;
  5271. case fcQueueToolbar: return QueueDock;
  5272. case fcRemoteTree: return RemoteDrivePanel;
  5273. case fcSessionsTabs: return SessionsPageControl;
  5274. case fcQueueFileList: return QueueFileList;
  5275. default: return NULL;
  5276. }
  5277. }
  5278. //---------------------------------------------------------------------------
  5279. void __fastcall TCustomScpExplorerForm::DirViewColumnRightClick(
  5280. TObject *Sender, TListColumn *Column, TPoint &Point)
  5281. {
  5282. DebugAssert(NonVisualDataModule && Column && Sender);
  5283. NonVisualDataModule->ListColumn = Column;
  5284. TPopupMenu * DirViewColumnMenu;
  5285. if (Sender == DirView(osOther))
  5286. {
  5287. DirViewColumnMenu = NonVisualDataModule->RemoteDirViewColumnPopup;
  5288. NonVisualDataModule->RemoteSortByExtColumnPopupItem->Visible = (Column->Index == uvName);
  5289. bool SizeColumn = (Column->Index == uvSize);
  5290. NonVisualDataModule->RemoteFormatSizeBytesPopupItem->Visible = SizeColumn;
  5291. NonVisualDataModule->RemoteCalculateDirectorySizesPopupItem->Visible = SizeColumn;
  5292. }
  5293. else
  5294. {
  5295. DirViewColumnMenu = NonVisualDataModule->LocalDirViewColumnPopup;
  5296. NonVisualDataModule->LocalSortByExtColumnPopupItem->Visible = (Column->Index == dvName);
  5297. bool SizeColumn = (Column->Index == dvSize);
  5298. NonVisualDataModule->LocalFormatSizeBytesPopupItem->Visible = SizeColumn;
  5299. NonVisualDataModule->LocalCalculateDirectorySizesPopupItem->Visible = SizeColumn;
  5300. }
  5301. TCustomListView * ListView = DebugNotNull(dynamic_cast<TCustomListView *>(Sender));
  5302. TPoint ScreenPoint = ListView->ClientToScreen(Point);
  5303. ScreenPoint.x -= GetScrollPos(ListView->Handle, SB_HORZ);
  5304. DirViewColumnMenu->Popup(ScreenPoint.x, ScreenPoint.y);
  5305. }
  5306. //---------------------------------------------------------------------------
  5307. void __fastcall TCustomScpExplorerForm::DirViewExecFile(
  5308. TObject *Sender, TListItem *Item, bool &AllowExec)
  5309. {
  5310. DoDirViewExecFile(Sender, Item, AllowExec);
  5311. }
  5312. //---------------------------------------------------------------------------
  5313. void __fastcall TCustomScpExplorerForm::DoDirViewExecFile(TObject * Sender,
  5314. TListItem * Item, bool & AllowExec)
  5315. {
  5316. DebugAssert(Sender && Item && Configuration);
  5317. DebugAssert(AllowExec);
  5318. TCustomDirView * ADirView = (TCustomDirView *)Sender;
  5319. TOperationSide Side = ((ADirView == DirView(osRemote)) ? osRemote : osLocal);
  5320. bool Remote = !IsSideLocalBrowser(Side);
  5321. DebugAssert(Item == ADirView->ItemFocused);
  5322. bool IsDirectory = ADirView->ItemIsDirectory(Item);
  5323. TResolvedDoubleClickAction Action;
  5324. if (FForceExecution)
  5325. {
  5326. Action = IsDirectory ? rdcaChangeDir : rdcaOpen;
  5327. }
  5328. else
  5329. {
  5330. TTerminal * ATerminal = !IsSideLocalBrowser(Side) ? Terminal : NULL;
  5331. Action = WinConfiguration->ResolveDoubleClickAction(IsDirectory, ATerminal);
  5332. }
  5333. if (Action == rdcaCopy)
  5334. {
  5335. // To counter the lock in DirViewBusy (called from TCustomDirView.DoExecute)
  5336. UnlockWindow();
  5337. try
  5338. {
  5339. if (IsLocalBrowserMode())
  5340. {
  5341. LocalLocalCopy(foCopy, Side, true, !WinConfiguration->CopyOnDoubleClickConfirmation, false, 0);
  5342. }
  5343. else
  5344. {
  5345. ExecuteFileOperation(foCopy, Side, true, !WinConfiguration->CopyOnDoubleClickConfirmation);
  5346. }
  5347. }
  5348. __finally
  5349. {
  5350. LockWindow();
  5351. }
  5352. AllowExec = false;
  5353. }
  5354. else if (Action == rdcaEdit)
  5355. {
  5356. if (!Remote || !WinConfiguration->DisableOpenEdit)
  5357. {
  5358. ExecuteFile(Side, efDefaultEditor);
  5359. AllowExec = false;
  5360. }
  5361. }
  5362. else if (Action == rdcaOpen)
  5363. {
  5364. if (DebugAlwaysTrue(!IsDirectory) && Remote && !WinConfiguration->DisableOpenEdit)
  5365. {
  5366. ExecuteFile(osRemote, efShell);
  5367. AllowExec = false;
  5368. }
  5369. }
  5370. }
  5371. //---------------------------------------------------------------------------
  5372. bool __fastcall TCustomScpExplorerForm::GetHasDirView(TOperationSide Side)
  5373. {
  5374. return ((Side == osRemote) || (Side == osCurrent));
  5375. }
  5376. //---------------------------------------------------------------------------
  5377. void __fastcall TCustomScpExplorerForm::CompareDirectories()
  5378. {
  5379. DebugFail();
  5380. }
  5381. //---------------------------------------------------------------------------
  5382. void __fastcall TCustomScpExplorerForm::SynchronizeDirectories()
  5383. {
  5384. DebugFail();
  5385. }
  5386. //---------------------------------------------------------------------------
  5387. bool __fastcall TCustomScpExplorerForm::DoSynchronizeDirectories(
  5388. UnicodeString & LocalDirectory, UnicodeString & RemoteDirectory, int UseDefaults)
  5389. {
  5390. TSynchronizeParamType Params;
  5391. Params.LocalDirectory = LocalDirectory;
  5392. Params.RemoteDirectory = RemoteDirectory;
  5393. int UnusedParams =
  5394. (GUIConfiguration->SynchronizeParams &
  5395. (TTerminal::spPreviewChanges | TTerminal::spTimestamp | TTerminal::spNotByTime | TTerminal::spBySize |
  5396. TTerminal::spCaseSensitive));
  5397. Params.Params = GUIConfiguration->SynchronizeParams & ~UnusedParams;
  5398. Params.Options = GUIConfiguration->SynchronizeOptions;
  5399. bool SaveSettings = false;
  5400. TSynchronizeController Controller(&DoSynchronize, &DoSynchronizeInvalid,
  5401. &DoSynchronizeTooManyDirectories);
  5402. DebugAssert(FSynchronizeController == NULL);
  5403. FSynchronizeController = &Controller;
  5404. bool Result;
  5405. try
  5406. {
  5407. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  5408. int CopyParamAttrs = Terminal->UsableCopyParamAttrs(0).Upload;
  5409. int Options =
  5410. FLAGMASK(SynchronizeAllowSelectedOnly(), soAllowSelectedOnly);
  5411. DebugAssert(FOnFeedSynchronizeError == NULL);
  5412. DebugAssert(FOnSynchronizeAbort == NULL);
  5413. DebugAssert(FSynchronizeTerminal == NULL);
  5414. FSynchronizeTerminal = Terminal;
  5415. Result = DoSynchronizeDialog(Params, &CopyParam, Controller.StartStop,
  5416. SaveSettings, Options, CopyParamAttrs, GetSynchronizeOptions, SynchronizeSessionLog,
  5417. FOnFeedSynchronizeError, FOnSynchronizeAbort, SynchronizeInNewWindow, UseDefaults);
  5418. if (Result)
  5419. {
  5420. if (SaveSettings)
  5421. {
  5422. GUIConfiguration->SynchronizeParams = Params.Params | UnusedParams;
  5423. GUIConfiguration->SynchronizeOptions = Params.Options;
  5424. }
  5425. else
  5426. {
  5427. if (FLAGSET(GUIConfiguration->SynchronizeOptions, soSynchronizeAsk) &&
  5428. FLAGCLEAR(Params.Options, soSynchronizeAsk) &&
  5429. FLAGSET(Params.Options, soSynchronize))
  5430. {
  5431. GUIConfiguration->SynchronizeOptions =
  5432. (GUIConfiguration->SynchronizeOptions & ~soSynchronizeAsk) |
  5433. soSynchronize;
  5434. }
  5435. }
  5436. LocalDirectory = Params.LocalDirectory;
  5437. RemoteDirectory = Params.RemoteDirectory;
  5438. }
  5439. }
  5440. __finally
  5441. {
  5442. FSynchronizeController = NULL;
  5443. DebugAssert(FOnFeedSynchronizeError == NULL);
  5444. FOnFeedSynchronizeError = NULL;
  5445. DebugAssert(FOnSynchronizeAbort == NULL);
  5446. FOnSynchronizeAbort = NULL;
  5447. FSynchronizeTerminal = NULL;
  5448. }
  5449. return Result;
  5450. }
  5451. //---------------------------------------------------------------------------
  5452. void __fastcall TCustomScpExplorerForm::DoSynchronize(
  5453. TSynchronizeController * /*Sender*/, const UnicodeString LocalDirectory,
  5454. const UnicodeString RemoteDirectory, const TCopyParamType & CopyParam,
  5455. const TSynchronizeParamType & Params, TSynchronizeChecklist ** Checklist,
  5456. TSynchronizeOptions * Options, bool Full)
  5457. {
  5458. // Keep-up-to-date mode
  5459. if (Terminal->Status == ssOpened)
  5460. {
  5461. try
  5462. {
  5463. int PParams = Params.Params;
  5464. if (!Full)
  5465. {
  5466. PParams |= TTerminal::spNoRecurse | TTerminal::spUseCache | TTerminal::spDelayProgress;
  5467. if (FLAGSET(Params.Options, soRecurse))
  5468. {
  5469. PParams |= TTerminal::spSubDirs;
  5470. }
  5471. }
  5472. else
  5473. {
  5474. // if keepuptodate is non-recursive,
  5475. // full sync before has to be non-recursive as well
  5476. if (FLAGCLEAR(Params.Options, soRecurse))
  5477. {
  5478. PParams |= TTerminal::spNoRecurse;
  5479. }
  5480. }
  5481. Synchronize(LocalDirectory, RemoteDirectory, smRemote, CopyParam,
  5482. PParams, Checklist, Options);
  5483. }
  5484. catch (Exception & E)
  5485. {
  5486. if (!Terminal->Active)
  5487. {
  5488. ShowExtendedExceptionEx(Terminal, &E);
  5489. // Do not abort the "keep up to date" when the session was succesfully reconnected.
  5490. if (!Terminal->Active)
  5491. {
  5492. throw;
  5493. }
  5494. }
  5495. else
  5496. {
  5497. if (FLAGSET(Params.Options, soContinueOnError) &&
  5498. DebugAlwaysTrue(FOnFeedSynchronizeError != NULL))
  5499. {
  5500. // noop, already logged in MoreMessageDialog
  5501. }
  5502. else
  5503. {
  5504. // We get mostly EAbort here, so this is noop.
  5505. // Exception is an error when listing directories while looking for differences.
  5506. ShowExtendedExceptionEx(Terminal, &E);
  5507. throw;
  5508. }
  5509. }
  5510. }
  5511. }
  5512. }
  5513. //---------------------------------------------------------------------------
  5514. void __fastcall TCustomScpExplorerForm::DoSynchronizeInvalid(
  5515. TSynchronizeController * /*Sender*/, const UnicodeString Directory,
  5516. const UnicodeString ErrorStr)
  5517. {
  5518. if (!Directory.IsEmpty())
  5519. {
  5520. SimpleErrorDialog(FMTLOAD(WATCH_ERROR_DIRECTORY, (Directory)), ErrorStr);
  5521. }
  5522. else
  5523. {
  5524. SimpleErrorDialog(LoadStr(WATCH_ERROR_GENERAL), ErrorStr);
  5525. }
  5526. }
  5527. //---------------------------------------------------------------------------
  5528. void __fastcall TCustomScpExplorerForm::DoSynchronizeTooManyDirectories(
  5529. TSynchronizeController * /*Sender*/, int & MaxDirectories)
  5530. {
  5531. if (MaxDirectories < GUIConfiguration->MaxWatchDirectories)
  5532. {
  5533. MaxDirectories = GUIConfiguration->MaxWatchDirectories;
  5534. }
  5535. else
  5536. {
  5537. TMessageParams Params(mpNeverAskAgainCheck);
  5538. unsigned int Result = MessageDialog(
  5539. FMTLOAD(TOO_MANY_WATCH_DIRECTORIES, (MaxDirectories, MaxDirectories)),
  5540. qtConfirmation, qaYes | qaNo, HELP_TOO_MANY_WATCH_DIRECTORIES, &Params);
  5541. if ((Result == qaYes) || (Result == qaNeverAskAgain))
  5542. {
  5543. MaxDirectories *= 2;
  5544. if (Result == qaNeverAskAgain)
  5545. {
  5546. GUIConfiguration->MaxWatchDirectories = MaxDirectories;
  5547. }
  5548. }
  5549. else
  5550. {
  5551. Abort();
  5552. }
  5553. }
  5554. }
  5555. //---------------------------------------------------------------------------
  5556. void __fastcall TCustomScpExplorerForm::Synchronize(const UnicodeString LocalDirectory,
  5557. const UnicodeString RemoteDirectory, TSynchronizeMode Mode,
  5558. const TCopyParamType & CopyParam, int Params, TSynchronizeChecklist ** Checklist,
  5559. TSynchronizeOptions * Options)
  5560. {
  5561. // Keep-up-to-date mode
  5562. DebugAssert(!FAutoOperation);
  5563. void * BatchStorage;
  5564. BatchStart(BatchStorage);
  5565. FAutoOperation = true;
  5566. bool AnyOperation = false;
  5567. TDateTime StartTime = Now();
  5568. TSynchronizeChecklist * AChecklist = NULL;
  5569. try
  5570. {
  5571. FSynchronizeProgressForm = new TSynchronizeProgressForm(Application, true, -1);
  5572. if (FLAGCLEAR(Params, TTerminal::spDelayProgress))
  5573. {
  5574. FSynchronizeProgressForm->Start();
  5575. }
  5576. AChecklist = Terminal->SynchronizeCollect(LocalDirectory, RemoteDirectory,
  5577. static_cast<TTerminal::TSynchronizeMode>(Mode),
  5578. &CopyParam, Params | TTerminal::spNoConfirmation, TerminalSynchronizeDirectory,
  5579. Options);
  5580. SetTaskbarListProgressState(TBPF_NOPROGRESS);
  5581. SAFE_DESTROY(FSynchronizeProgressForm);
  5582. AnyOperation = (AChecklist->CheckedCount > 0);
  5583. if (AnyOperation)
  5584. {
  5585. TSynchronizeProgress SynchronizeProgress(AChecklist);
  5586. CreateProgressForm(&SynchronizeProgress);
  5587. try
  5588. {
  5589. Terminal->SynchronizeApply(
  5590. AChecklist, &CopyParam, Params | TTerminal::spNoConfirmation, TerminalSynchronizeDirectory,
  5591. SynchronizeProcessedItem, NULL, NULL, NULL);
  5592. }
  5593. __finally
  5594. {
  5595. DestroyProgressForm();
  5596. }
  5597. }
  5598. }
  5599. __finally
  5600. {
  5601. if (Checklist == NULL)
  5602. {
  5603. delete AChecklist;
  5604. }
  5605. else
  5606. {
  5607. *Checklist = AChecklist;
  5608. }
  5609. FAutoOperation = false;
  5610. SAFE_DESTROY(FSynchronizeProgressForm);
  5611. BatchEnd(BatchStorage);
  5612. ReloadLocalDirectory();
  5613. if (AnyOperation)
  5614. {
  5615. OperationComplete(StartTime);
  5616. }
  5617. }
  5618. }
  5619. //---------------------------------------------------------------------------
  5620. bool __fastcall TCustomScpExplorerForm::SynchronizeAllowSelectedOnly()
  5621. {
  5622. // can be called from command line
  5623. return Visible &&
  5624. ((DirView(osRemote)->SelCount > 0) ||
  5625. (HasDirView[osLocal] && (DirView(osLocal)->SelCount > 0)));
  5626. }
  5627. //---------------------------------------------------------------------------
  5628. void __fastcall TCustomScpExplorerForm::SynchronizeSessionLog(const UnicodeString & Message)
  5629. {
  5630. LogSynchronizeEvent(Terminal, Message);
  5631. }
  5632. //---------------------------------------------------------------------------
  5633. void __fastcall TCustomScpExplorerForm::GetSynchronizeOptions(
  5634. int Params, TSynchronizeOptions & Options)
  5635. {
  5636. DebugAssert(!IsLocalBrowserMode());
  5637. if (FLAGSET(Params, TTerminal::spSelectedOnly) && SynchronizeAllowSelectedOnly())
  5638. {
  5639. Options.Filter = new TStringList();
  5640. Options.Filter->CaseSensitive = false;
  5641. Options.Filter->Duplicates = Types::dupAccept;
  5642. if (DirView(osRemote)->SelCount > 0)
  5643. {
  5644. DirView(osRemote)->CreateFileList(false, false, Options.Filter);
  5645. }
  5646. if (HasDirView[osLocal] && (DirView(osLocal)->SelCount > 0))
  5647. {
  5648. DirView(osLocal)->CreateFileList(false, false, Options.Filter);
  5649. }
  5650. Options.Filter->Sort();
  5651. }
  5652. }
  5653. //---------------------------------------------------------------------------
  5654. UnicodeString __fastcall TCustomScpExplorerForm::SerializeCopyParamForCommandLine(const TCopyParamType * CopyParams)
  5655. {
  5656. TCopyParamType Defaults;
  5657. std::unique_ptr<THierarchicalStorage> ConfigStorage(Configuration->CreateConfigStorage());
  5658. ConfigStorage->AccessMode = smRead;
  5659. if (ConfigStorage->OpenSubKey(Configuration->ConfigurationSubKey, false))
  5660. {
  5661. GUIConfiguration->LoadCopyParam(ConfigStorage.get(), &Defaults);
  5662. }
  5663. ConfigStorage.reset(NULL);
  5664. std::unique_ptr<TStringList> Options(new TStringList());
  5665. std::unique_ptr<TOptionsStorage> OptionsStorage(new TOptionsStorage(Options.get(), true));
  5666. CopyParams->Save(OptionsStorage.get(), &Defaults);
  5667. UnicodeString Result;
  5668. if (Options->Count > 0)
  5669. {
  5670. Result =
  5671. FORMAT(L" %s%s", (TProgramParams::FormatSwitch(RAWTRANSFERSETTINGS_SWITCH), StringsToParams(Options.get())));
  5672. }
  5673. return Result;
  5674. }
  5675. //---------------------------------------------------------------------------
  5676. void __fastcall TCustomScpExplorerForm::SynchronizeInNewWindow(
  5677. const TSynchronizeParamType & Params, const TCopyParamType * CopyParams)
  5678. {
  5679. UnicodeString SessionName = CreateHiddenDuplicateSession();
  5680. UnicodeString AdditionalParams =
  5681. FORMAT(L"%s%s %s // \"%s\" \"%s\" %d %d", (
  5682. TProgramParams::FormatSwitch(DEFAULTS_SWITCH),
  5683. SerializeCopyParamForCommandLine(CopyParams),
  5684. TProgramParams::FormatSwitch(KEEP_UP_TO_DATE_SWITCH),
  5685. Params.LocalDirectory, Params.RemoteDirectory, Params.Params, Params.Options));
  5686. ExecuteNewInstance(SessionName, AdditionalParams);
  5687. }
  5688. //---------------------------------------------------------------------------
  5689. struct TSynchronizeParams
  5690. {
  5691. UnicodeString LocalDirectory;
  5692. UnicodeString RemoteDirectory;
  5693. TSynchronizeMode Mode;
  5694. TSynchronizeChecklist * Checklist;
  5695. int Params;
  5696. TCopyParamType * CopyParam;
  5697. TDateTime CollectElapsed;
  5698. TDateTime * StartTime;
  5699. TProcessedSynchronizationChecklistItem OnProcessedItem;
  5700. };
  5701. //---------------------------------------------------------------------------
  5702. void __fastcall TCustomScpExplorerForm::FullSynchronize(
  5703. TSynchronizeParams & Params, TProcessedSynchronizationChecklistItem OnProcessedItem,
  5704. TUpdatedSynchronizationChecklistItems OnUpdatedSynchronizationChecklistItems)
  5705. {
  5706. DebugAssert(!FAutoOperation);
  5707. void * BatchStorage;
  5708. BatchStart(BatchStorage);
  5709. FAutoOperation = true;
  5710. TFileOperationStatistics Statistics;
  5711. TDateTime Start = Now();
  5712. try
  5713. {
  5714. Params.OnProcessedItem = OnProcessedItem;
  5715. TSynchronizeProgress SynchronizeProgress(Params.Checklist);
  5716. CreateProgressForm(&SynchronizeProgress);
  5717. Terminal->SynchronizeApply(
  5718. Params.Checklist, Params.CopyParam, Params.Params | TTerminal::spNoConfirmation,
  5719. TerminalSynchronizeDirectory, SynchronizeProcessedItem, OnUpdatedSynchronizationChecklistItems, &Params,
  5720. &Statistics);
  5721. }
  5722. __finally
  5723. {
  5724. Params.OnProcessedItem = NULL;
  5725. FAutoOperation = false;
  5726. DestroyProgressForm();
  5727. BatchEnd(BatchStorage);
  5728. ReloadLocalDirectory();
  5729. }
  5730. if (WinConfiguration->SynchronizeSummary)
  5731. {
  5732. UnicodeString Message = MainInstructions(LoadStr(SYNCHRONIZE_COMPLETE)) + L"\n";
  5733. // The statistics should be 0 anyway in this case
  5734. if (FLAGCLEAR(Params.Params, TTerminal::spTimestamp))
  5735. {
  5736. Message += L"\n";
  5737. if (Statistics.FilesUploaded > 0)
  5738. {
  5739. Message += FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 1), (FormatNumber(Statistics.FilesUploaded), FormatBytes(Statistics.TotalUploaded))) + L"\n";
  5740. }
  5741. if (Statistics.FilesDownloaded > 0)
  5742. {
  5743. Message += FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 2), (FormatNumber(Statistics.FilesDownloaded), FormatBytes(Statistics.TotalDownloaded))) + L"\n";
  5744. }
  5745. if (Statistics.FilesDeletedLocal > 0)
  5746. {
  5747. Message += FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 3), (FormatNumber(Statistics.FilesDeletedLocal))) + L"\n";
  5748. }
  5749. if (Statistics.FilesDeletedRemote > 0)
  5750. {
  5751. Message += FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 4), (FormatNumber(Statistics.FilesDeletedRemote))) + L"\n";
  5752. }
  5753. }
  5754. TDateTime Elapsed = (Now() - Start);
  5755. Message +=
  5756. L"\n" +
  5757. FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 5), (FormatDateTimeSpan(Configuration->TimeFormat, Params.CollectElapsed))) + L"\n" +
  5758. FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 6), (FormatDateTimeSpan(Configuration->TimeFormat, Elapsed)));
  5759. TMessageParams Params(mpNeverAskAgainCheck);
  5760. unsigned int Result = MessageDialog(Message, qtInformation, qaOK, HELP_NONE, &Params);
  5761. if (Result == qaNeverAskAgain)
  5762. {
  5763. WinConfiguration->SynchronizeSummary = false;
  5764. }
  5765. }
  5766. }
  5767. //---------------------------------------------------------------------------
  5768. void __fastcall TCustomScpExplorerForm::SynchronizeProcessedItem(void * Token, const TSynchronizeChecklist::TItem * ChecklistItem)
  5769. {
  5770. if (DebugAlwaysTrue(FProgressForm != NULL) && DebugAlwaysTrue(FProgressForm->SynchronizeProgress != NULL))
  5771. {
  5772. FProgressForm->SynchronizeProgress->ItemProcessed(ChecklistItem);
  5773. }
  5774. // Not set in keep-up-to-date mode - Synchronize() method
  5775. if (Token != NULL)
  5776. {
  5777. TSynchronizeParams & Params = *static_cast<TSynchronizeParams *>(Token);
  5778. if (Params.OnProcessedItem != NULL)
  5779. {
  5780. Params.OnProcessedItem(NULL, ChecklistItem);
  5781. }
  5782. }
  5783. }
  5784. //---------------------------------------------------------------------------
  5785. void __fastcall TCustomScpExplorerForm::DoFullSynchronize(
  5786. void * Token, TProcessedSynchronizationChecklistItem OnProcessedItem,
  5787. TUpdatedSynchronizationChecklistItems OnUpdatedSynchronizationChecklistItems)
  5788. {
  5789. TSynchronizeParams & Params = *static_cast<TSynchronizeParams *>(Token);
  5790. *Params.StartTime = Now();
  5791. FullSynchronize(Params, OnProcessedItem, OnUpdatedSynchronizationChecklistItems);
  5792. }
  5793. //---------------------------------------------------------------------------
  5794. void __fastcall TCustomScpExplorerForm::DoSynchronizeChecklistCalculateSize(
  5795. TSynchronizeChecklist * Checklist, const TSynchronizeChecklist::TItemList & Items, void * Token)
  5796. {
  5797. // terminal can be already closed (e.g. dropped connection)
  5798. if (Terminal != NULL)
  5799. {
  5800. TSynchronizeParams & Params = *static_cast<TSynchronizeParams *>(Token);
  5801. Terminal->SynchronizeChecklistCalculateSize(Checklist, Items, Params.CopyParam);
  5802. }
  5803. }
  5804. //---------------------------------------------------------------------------
  5805. void __fastcall TCustomScpExplorerForm::DoSynchronizeMove(
  5806. TOperationSide Side, const UnicodeString & FileName, const UnicodeString & NewFileName, TRemoteFile * RemoteFile)
  5807. {
  5808. DebugAssert(!IsLocalBrowserMode());
  5809. TAutoBatch AutoBatch(this);
  5810. TAutoFlag AutoOperationFlag(FAutoOperation);
  5811. if (Side == osRemote)
  5812. {
  5813. std::unique_ptr<TStrings> FileList(new TStringList());
  5814. FileList->AddObject(FileName, RemoteFile);
  5815. UnicodeString Target = UnixExtractFileDir(NewFileName);
  5816. UnicodeString FileMask = DelimitFileNameMask(UnixExtractFileName(NewFileName));
  5817. RemoteDirView->SaveSelection();
  5818. RemoteDirView->SaveSelectedNames();
  5819. try
  5820. {
  5821. Terminal->MoveFiles(FileList.get(), Target, FileMask, false);
  5822. }
  5823. catch(...)
  5824. {
  5825. RemoteDirView->DiscardSavedSelection();
  5826. throw;
  5827. }
  5828. RemoteDirView->RestoreSelection();
  5829. }
  5830. else if (DebugAlwaysTrue(Side == osLocal))
  5831. {
  5832. if (!MoveFile(FileName.c_str(), NewFileName.c_str()))
  5833. {
  5834. throw EOSExtException(FMTLOAD(RENAME_FILE_ERROR, (FileName, NewFileName)));
  5835. }
  5836. UnicodeString Directory = ExtractFileDir(FileName);
  5837. ReloadLocalDirectory(Directory);
  5838. UnicodeString NewDirectory = ExtractFileDir(NewFileName);
  5839. if (!SamePaths(Directory, NewDirectory))
  5840. {
  5841. ReloadLocalDirectory(NewDirectory);
  5842. }
  5843. }
  5844. }
  5845. //---------------------------------------------------------------------------
  5846. void __fastcall TCustomScpExplorerForm::DoSynchronizeBrowse(TOperationSide Side, TSynchronizeChecklist::TAction Action, const TSynchronizeChecklist::TItem * Item)
  5847. {
  5848. UnicodeString LocalPath = ExcludeTrailingBackslash(Item->Local.Directory);
  5849. if (Side == osLocal)
  5850. {
  5851. if (Action == TSynchronizeChecklist::saDownloadNew)
  5852. {
  5853. OpenFolderInExplorer(LocalPath);
  5854. }
  5855. else
  5856. {
  5857. OpenFileInExplorer(TPath::Combine(LocalPath, Item->GetFileName()));
  5858. }
  5859. }
  5860. else if (DebugAlwaysTrue(Side == osRemote))
  5861. {
  5862. // Similar to CreateHiddenDuplicateSession, except that it modifies the initial directories
  5863. std::unique_ptr<TSessionData> SessionData(CloneCurrentSessionData());
  5864. SessionData->RemoteDirectory = UnixExcludeTrailingBackslash(Item->Remote.Directory);
  5865. if (!LocalPath.IsEmpty())
  5866. {
  5867. SessionData->LocalDirectory = LocalPath;
  5868. }
  5869. UnicodeString SessionName = SaveHiddenDuplicateSession(SessionData.get());
  5870. ExecuteNewInstance(SessionName, FORMAT(L"%s=\"%s\"", (TProgramParams::FormatSwitch(BROWSE_SWITCH), Item->GetFileName())));
  5871. }
  5872. }
  5873. //---------------------------------------------------------------------------
  5874. void __fastcall TCustomScpExplorerForm::FullSynchronizeInNewWindow(
  5875. TSynchronizeMode Mode, int Params, const UnicodeString & LocalDirectory, const UnicodeString & RemoteDirectory,
  5876. const TCopyParamType * CopyParams)
  5877. {
  5878. UnicodeString SessionName = CreateHiddenDuplicateSession();
  5879. UnicodeString AdditionalParams =
  5880. FORMAT(L"%s%s %s // \"%s\" \"%s\" %d %d", (
  5881. TProgramParams::FormatSwitch(DEFAULTS_SWITCH),
  5882. SerializeCopyParamForCommandLine(CopyParams),
  5883. TProgramParams::FormatSwitch(SYNCHRONIZE_SWITCH),
  5884. LocalDirectory, RemoteDirectory, Mode, Params));
  5885. ExecuteNewInstance(SessionName, AdditionalParams);
  5886. }
  5887. //---------------------------------------------------------------------------
  5888. int __fastcall TCustomScpExplorerForm::DoFullSynchronizeDirectories(
  5889. UnicodeString & LocalDirectory, UnicodeString & RemoteDirectory,
  5890. TSynchronizeMode & Mode, int Params, bool & SaveMode, int UseDefaults)
  5891. {
  5892. int Result;
  5893. bool SaveSettings = false;
  5894. int Options =
  5895. FLAGMASK(!Terminal->IsCapable[fcTimestampChanging], fsoDisableTimestamp) |
  5896. FLAGMASK(SynchronizeAllowSelectedOnly(), fsoAllowSelectedOnly);
  5897. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  5898. TUsableCopyParamAttrs CopyParamAttrs = Terminal->UsableCopyParamAttrs(0);
  5899. bool Continue =
  5900. (UseDefaults == 0) ||
  5901. DoFullSynchronizeDialog(Mode, Params, LocalDirectory, RemoteDirectory,
  5902. &CopyParam, SaveSettings, SaveMode, Options, CopyParamAttrs, FullSynchronizeInNewWindow, UseDefaults);
  5903. if (Continue)
  5904. {
  5905. Configuration->Usage->Inc(L"Synchronizations");
  5906. CopyParam.IncludeFileMask.SetRoots(LocalDirectory, RemoteDirectory);
  5907. UpdateCopyParamCounters(CopyParam);
  5908. TSynchronizeOptions SynchronizeOptions;
  5909. GetSynchronizeOptions(Params, SynchronizeOptions);
  5910. if (SaveSettings)
  5911. {
  5912. GUIConfiguration->SynchronizeParams = Params;
  5913. }
  5914. else
  5915. {
  5916. SaveMode = false;
  5917. }
  5918. TDateTime StartTime = Now();
  5919. TSynchronizeChecklist * Checklist = NULL;
  5920. try
  5921. {
  5922. DebugAssert(!FAutoOperation);
  5923. FAutoOperation = true;
  5924. try
  5925. {
  5926. UnicodeString SessionKey = Terminal->SessionData->SessionKey;
  5927. std::unique_ptr<TStrings> DataList(Configuration->LoadDirectoryStatisticsCache(SessionKey, RemoteDirectory, CopyParam));
  5928. int Files = -1;
  5929. if (DataList->Count >= 1)
  5930. {
  5931. Files = StrToIntDef(DataList->Strings[0], Files);
  5932. }
  5933. else
  5934. {
  5935. DataList->Add(UnicodeString());
  5936. }
  5937. FSynchronizeProgressForm = new TSynchronizeProgressForm(Application, true, Files);
  5938. FSynchronizeProgressForm->Start();
  5939. Checklist = Terminal->SynchronizeCollect(LocalDirectory, RemoteDirectory,
  5940. static_cast<TTerminal::TSynchronizeMode>(Mode),
  5941. &CopyParam, Params | TTerminal::spNoConfirmation, TerminalSynchronizeDirectory,
  5942. &SynchronizeOptions);
  5943. if (Terminal->SessionData->CacheDirectories)
  5944. {
  5945. DataList->Strings[0] = IntToStr(SynchronizeOptions.Files);
  5946. Configuration->SaveDirectoryStatisticsCache(SessionKey, RemoteDirectory, CopyParam, DataList.get());
  5947. }
  5948. }
  5949. __finally
  5950. {
  5951. FAutoOperation = false;
  5952. SetTaskbarListProgressState(TBPF_NOPROGRESS);
  5953. SAFE_DESTROY(FSynchronizeProgressForm);
  5954. }
  5955. TSynchronizeParams SynchronizeParams;
  5956. SynchronizeParams.LocalDirectory = LocalDirectory;
  5957. SynchronizeParams.RemoteDirectory = RemoteDirectory;
  5958. SynchronizeParams.Mode = Mode;
  5959. SynchronizeParams.CopyParam = &CopyParam;
  5960. SynchronizeParams.Params = Params;
  5961. SynchronizeParams.Checklist = Checklist;
  5962. SynchronizeParams.CollectElapsed = (Now() - StartTime);
  5963. SynchronizeParams.StartTime = &StartTime;
  5964. SynchronizeParams.OnProcessedItem = NULL;
  5965. Result = Checklist->Count;
  5966. if (Checklist->Count > 0)
  5967. {
  5968. if (FLAGSET(Params, TTerminal::spPreviewChanges))
  5969. {
  5970. if (!DoSynchronizeChecklistDialog(
  5971. Checklist, Mode, Params, LocalDirectory, RemoteDirectory, CustomCommandMenu, DoFullSynchronize,
  5972. DoSynchronizeChecklistCalculateSize, DoSynchronizeMove, DoSynchronizeBrowse, &SynchronizeParams))
  5973. {
  5974. Result = -1;
  5975. }
  5976. }
  5977. else
  5978. {
  5979. FullSynchronize(SynchronizeParams, NULL, NULL);
  5980. }
  5981. }
  5982. else
  5983. {
  5984. UnicodeString Message = MainInstructions(LoadStr(COMPARE_NO_DIFFERENCES));
  5985. MessageDialog(Message, qtInformation, qaOK, HELP_SYNCHRONIZE_NO_DIFFERENCES);
  5986. }
  5987. }
  5988. __finally
  5989. {
  5990. delete Checklist;
  5991. }
  5992. OperationComplete(StartTime);
  5993. }
  5994. else
  5995. {
  5996. Result = -1;
  5997. }
  5998. return Result;
  5999. }
  6000. //---------------------------------------------------------------------------
  6001. void __fastcall TCustomScpExplorerForm::TerminalSynchronizeDirectory(
  6002. const UnicodeString & LocalDirectory, const UnicodeString & RemoteDirectory,
  6003. bool & Continue, bool Collect, const TSynchronizeOptions * Options)
  6004. {
  6005. if (Collect)
  6006. {
  6007. DebugAssert(FSynchronizeProgressForm != NULL);
  6008. if (!FSynchronizeProgressForm->Started)
  6009. {
  6010. FSynchronizeProgressForm->Start();
  6011. }
  6012. int CompareProgress = FSynchronizeProgressForm->SetData(LocalDirectory, RemoteDirectory, Options->Files, Continue);
  6013. SetTaskbarListProgressValue(CompareProgress);
  6014. }
  6015. else
  6016. {
  6017. DebugAssert(FSynchronizeProgressForm == NULL);
  6018. DebugAssert(FProgressForm != NULL);
  6019. }
  6020. }
  6021. //---------------------------------------------------------------------------
  6022. void __fastcall TCustomScpExplorerForm::StandaloneEdit(const UnicodeString & FileName)
  6023. {
  6024. UnicodeString FullFileName = AbsolutePath(Terminal->CurrentDirectory, FileName);
  6025. std::unique_ptr<TRemoteFile> File(Terminal->ReadFile(FullFileName));
  6026. if (File.get() != NULL)
  6027. {
  6028. ExecuteRemoteFile(FullFileName, File.get(), efInternalEditor);
  6029. Application->ShowMainForm = false;
  6030. Application->Run();
  6031. }
  6032. }
  6033. //---------------------------------------------------------------------------
  6034. void __fastcall TCustomScpExplorerForm::ExploreLocalDirectory(TOperationSide)
  6035. {
  6036. DebugFail();
  6037. }
  6038. //---------------------------------------------------------------------------
  6039. TSessionData * __fastcall TCustomScpExplorerForm::CloneCurrentSessionData()
  6040. {
  6041. std::unique_ptr<TSessionData> SessionData(new TSessionData(L""));
  6042. SessionData->Assign(ManagedSession->SessionData);
  6043. UpdateSessionData(SessionData.get());
  6044. DebugAssert(SessionData->IsLocalBrowser == IsLocalBrowserMode());
  6045. if (IsActiveTerminal(ManagedSession))
  6046. {
  6047. ManagedSession->UpdateSessionCredentials(SessionData.get());
  6048. }
  6049. if (!ManagedSession->SessionData->HasSessionName())
  6050. {
  6051. // Particularly for "Workspace/XXXX" name, we need to reset it, as it would become user-visible
  6052. // once IsWorkspace is cleared
  6053. SessionData->Name = UnicodeString();
  6054. }
  6055. // Most uses of this method ends with saving the session, and we do not want to store the IsWorkspace flag.
  6056. // Particularly, with SaveHiddenDuplicateSession, it won't work as TSessionData::ParseUrl explicitly ignored workspace sessions.
  6057. SessionData->IsWorkspace = false;
  6058. return SessionData.release();
  6059. }
  6060. //---------------------------------------------------------------------------
  6061. void __fastcall TCustomScpExplorerForm::SaveCurrentSession()
  6062. {
  6063. TSessionData * SessionData = CloneCurrentSessionData();
  6064. try
  6065. {
  6066. TSessionData * EditingSessionData = StoredSessions->FindSame(SessionData);
  6067. DoSaveSession(SessionData, EditingSessionData, true, NULL);
  6068. }
  6069. __finally
  6070. {
  6071. delete SessionData;
  6072. }
  6073. }
  6074. //---------------------------------------------------------------------------
  6075. TObjectList * __fastcall TCustomScpExplorerForm::DoCollectWorkspace()
  6076. {
  6077. TTerminalManager * Manager = TTerminalManager::Instance();
  6078. std::unique_ptr<TObjectList> DataList(new TObjectList());
  6079. if (DebugAlwaysTrue(ManagedSession != NULL))
  6080. {
  6081. // Update ManagedSession->StateData
  6082. UpdateSession(ManagedSession);
  6083. }
  6084. Manager->SaveWorkspace(DataList.get());
  6085. return DataList.release();
  6086. }
  6087. //---------------------------------------------------------------------------
  6088. void __fastcall TCustomScpExplorerForm::DoSaveWorkspace(
  6089. const UnicodeString & Name, TObjectList * DataList, bool SavePasswords, bool Explicit)
  6090. {
  6091. WinConfiguration->LastWorkspace = Name;
  6092. if (!SavePasswords)
  6093. {
  6094. for (int Index = 0; Index < DataList->Count; Index++)
  6095. {
  6096. TSessionData * SessionData = dynamic_cast<TSessionData *>(DataList->Items[Index]);
  6097. if (SessionData->Link.IsEmpty())
  6098. {
  6099. SessionData->ClearSessionPasswords();
  6100. }
  6101. }
  6102. }
  6103. StoredSessions->NewWorkspace(Name, DataList);
  6104. // modified only
  6105. StoredSessions->Save(false, Explicit);
  6106. }
  6107. //---------------------------------------------------------------------------
  6108. UnicodeString __fastcall TCustomScpExplorerForm::WorkspaceName()
  6109. {
  6110. return
  6111. DefaultStr(WinConfiguration->LastWorkspace,
  6112. DefaultStr(WinConfiguration->AutoWorkspace, LoadStr(NEW_WORKSPACE)));
  6113. }
  6114. //---------------------------------------------------------------------------
  6115. bool __fastcall TCustomScpExplorerForm::SaveWorkspace(bool EnableAutoSave)
  6116. {
  6117. std::unique_ptr<TObjectList> DataList(DoCollectWorkspace());
  6118. bool AnyNonStoredSessionWithPassword = false;
  6119. bool AnyNonStoredNonWorkspaceSessionWithPassword = false;
  6120. bool AllNonStoredSessionsAnonymous = true;
  6121. TTerminalManager * Manager = TTerminalManager::Instance();
  6122. for (int Index = 0; Index < DataList->Count; Index++)
  6123. {
  6124. TSessionData * SessionData = dynamic_cast<TSessionData *>(DataList->Items[Index]);
  6125. if (SessionData->Link.IsEmpty())
  6126. {
  6127. if (SessionData->HasAnySessionPassword())
  6128. {
  6129. AnyNonStoredSessionWithPassword = true;
  6130. if (!Manager->Sessions[Index]->SessionData->IsWorkspace)
  6131. {
  6132. AnyNonStoredNonWorkspaceSessionWithPassword = true;
  6133. }
  6134. }
  6135. if (!SameText(SessionData->UserName, AnonymousUserName))
  6136. {
  6137. AllNonStoredSessionsAnonymous = false;
  6138. }
  6139. }
  6140. }
  6141. bool SavePasswords;
  6142. bool * PSavePasswords;
  6143. bool NotRecommendedSavingPasswords =
  6144. !CustomWinConfiguration->UseMasterPassword &&
  6145. !AllNonStoredSessionsAnonymous;
  6146. if (Configuration->DisablePasswordStoring ||
  6147. !AnyNonStoredSessionWithPassword)
  6148. {
  6149. PSavePasswords = NULL;
  6150. SavePasswords = false;
  6151. }
  6152. else
  6153. {
  6154. PSavePasswords = &SavePasswords;
  6155. SavePasswords =
  6156. !AnyNonStoredNonWorkspaceSessionWithPassword ||
  6157. !NotRecommendedSavingPasswords;
  6158. }
  6159. UnicodeString Name = WorkspaceName();
  6160. bool CreateShortcut = false;
  6161. bool Result =
  6162. DoSaveWorkspaceDialog(
  6163. Name, PSavePasswords, NotRecommendedSavingPasswords, CreateShortcut,
  6164. EnableAutoSave);
  6165. if (Result)
  6166. {
  6167. DoSaveWorkspace(Name, DataList.get(), SavePasswords, true);
  6168. if (CreateShortcut)
  6169. {
  6170. TOperationVisualizer Visualizer;
  6171. UnicodeString AdditionalParams = TProgramParams::FormatSwitch(DESKTOP_SWITCH);
  6172. CreateDesktopSessionShortCut(Name, L"", AdditionalParams, -1, WORKSPACE_ICON);
  6173. }
  6174. if (EnableAutoSave)
  6175. {
  6176. WinConfiguration->AutoSaveWorkspace = true;
  6177. WinConfiguration->AutoWorkspace = Name;
  6178. if (PSavePasswords != NULL)
  6179. {
  6180. WinConfiguration->AutoSaveWorkspacePasswords = SavePasswords;
  6181. }
  6182. }
  6183. }
  6184. return Result;
  6185. }
  6186. //---------------------------------------------------------------------------
  6187. void __fastcall TCustomScpExplorerForm::UpdateSession(TManagedTerminal * Session)
  6188. {
  6189. DebugAssert(IsLocalBrowserMode() == Session->LocalBrowser);
  6190. DebugAssert(Session == ManagedSession);
  6191. if (!IsLocalBrowserMode())
  6192. {
  6193. SAFE_DESTROY(Session->RemoteExplorerState);
  6194. if (WinConfiguration->PreservePanelState)
  6195. {
  6196. Session->RemoteExplorerState = RemoteDirView->SaveState();
  6197. }
  6198. }
  6199. UpdateSessionData(Session->StateData);
  6200. }
  6201. //---------------------------------------------------------------------------
  6202. void __fastcall TCustomScpExplorerForm::UpdateSessionData(TSessionData * Data)
  6203. {
  6204. // Keep in sync with TSessionData::CopyStateData
  6205. DebugAssert(Data != NULL);
  6206. // This is inconsistent with how color (for example) is handled.
  6207. Data->Name = ManagedSession->SessionData->Name;
  6208. if (!IsLocalBrowserMode())
  6209. {
  6210. // cannot use RemoteDirView->Path, because it is empty if connection
  6211. // was already closed
  6212. // also only peek, we may not be connected at all atm
  6213. // so make sure we do not try retrieving current directory from the server
  6214. // (particularly with FTP)
  6215. UnicodeString ACurrentDirectory = Terminal->PeekCurrentDirectory();
  6216. if (!ACurrentDirectory.IsEmpty())
  6217. {
  6218. Data->RemoteDirectory = ACurrentDirectory;
  6219. }
  6220. }
  6221. Data->Color = SessionColor;
  6222. }
  6223. //---------------------------------------------------------------------------
  6224. void __fastcall TCustomScpExplorerForm::ToolBarResize(TObject *Sender)
  6225. {
  6226. TTBXToolbar * Toolbar = dynamic_cast<TTBXToolbar*>(Sender);
  6227. DebugAssert(Toolbar != NULL);
  6228. for (int i = 0; i < Toolbar->Items->Count; i++)
  6229. {
  6230. TTBXCustomDropDownItem * DropDownItem;
  6231. DropDownItem = dynamic_cast<TTBXCustomDropDownItem *>(Toolbar->Items->Items[i]);
  6232. if (DropDownItem != NULL)
  6233. {
  6234. ToolbarItemResize(DropDownItem,
  6235. Toolbar->Width - (Toolbar->View->BaseSize.x - DropDownItem->EditWidth) -
  6236. Toolbar->NonClientWidth);
  6237. break;
  6238. }
  6239. }
  6240. }
  6241. //---------------------------------------------------------------------------
  6242. void __fastcall TCustomScpExplorerForm::ToolbarItemResize(TTBXCustomDropDownItem * Item, int Width)
  6243. {
  6244. Item->EditWidth = Width;
  6245. }
  6246. //---------------------------------------------------------------------------
  6247. void __fastcall TCustomScpExplorerForm::ToolbarGetBaseSize(
  6248. TTBCustomToolbar * Toolbar, TPoint & ASize)
  6249. {
  6250. for (int i = 0; i < Toolbar->Items->Count; i++)
  6251. {
  6252. TTBXCustomDropDownItem * DropDownItem;
  6253. DropDownItem = dynamic_cast<TTBXCustomDropDownItem *>(Toolbar->Items->Items[i]);
  6254. if (DropDownItem != NULL)
  6255. {
  6256. ASize.x -= DropDownItem->EditWidth;
  6257. ASize.x += ScaleByTextHeight(this, 50) /* minimal combo width */;
  6258. }
  6259. }
  6260. }
  6261. //---------------------------------------------------------------------------
  6262. void __fastcall TCustomScpExplorerForm::DoWarnLackOfTempSpace(
  6263. const UnicodeString Path, __int64 RequiredSpace, bool & Continue)
  6264. {
  6265. if (WinConfiguration->DDWarnLackOfTempSpace)
  6266. {
  6267. UnicodeString ADrive = ExtractFileDrive(ExpandFileName(Path));
  6268. if (!ADrive.IsEmpty())
  6269. {
  6270. __int64 FreeSpace = DiskFree((Byte)(ADrive[1]-'A'+1));
  6271. DebugAssert(RequiredSpace >= 0);
  6272. __int64 RequiredWithReserve;
  6273. RequiredWithReserve = (__int64)(RequiredSpace * WinConfiguration->DDWarnLackOfTempSpaceRatio);
  6274. if (FreeSpace < RequiredWithReserve)
  6275. {
  6276. unsigned int Result;
  6277. TMessageParams Params(mpNeverAskAgainCheck);
  6278. Result = MessageDialog(FMTLOAD(DD_WARN_LACK_OF_TEMP_SPACE, (Path,
  6279. FormatBytes(FreeSpace), FormatBytes(RequiredSpace))),
  6280. qtWarning, qaYes | qaNo, HELP_DD_WARN_LACK_OF_TEMP_SPACE, &Params);
  6281. if (Result == qaNeverAskAgain)
  6282. {
  6283. WinConfiguration->DDWarnLackOfTempSpace = false;
  6284. }
  6285. Continue = (Result == qaYes || Result == qaNeverAskAgain);
  6286. }
  6287. }
  6288. }
  6289. }
  6290. //---------------------------------------------------------------------------
  6291. void __fastcall TCustomScpExplorerForm::AddBookmark(TOperationSide Side)
  6292. {
  6293. DoOpenDirectoryDialog(odAddBookmark, Side);
  6294. }
  6295. //---------------------------------------------------------------------------
  6296. TStrings * __fastcall TCustomScpExplorerForm::CreateVisitedDirectories(TOperationSide Side)
  6297. {
  6298. // we should better use TCustomDirView::FCaseSensitive, but it is private
  6299. TStringList * VisitedDirectories = CreateSortedStringList(!IsSideLocalBrowser(Side));
  6300. try
  6301. {
  6302. TCustomDirView * DView = DirView(Side);
  6303. for (int Index = -DView->BackCount; Index <= DView->ForwardCount; Index++)
  6304. {
  6305. VisitedDirectories->Add(DView->HistoryPath[Index]);
  6306. }
  6307. }
  6308. catch (...)
  6309. {
  6310. delete VisitedDirectories;
  6311. throw;
  6312. }
  6313. return VisitedDirectories;
  6314. }
  6315. //---------------------------------------------------------------------------
  6316. void __fastcall TCustomScpExplorerForm::DoOpenDirectoryDialog(
  6317. TOpenDirectoryMode Mode, TOperationSide Side)
  6318. {
  6319. bool Continue = true;
  6320. if (Mode == odAddBookmark)
  6321. {
  6322. TMessageParams Params(mpNeverAskAgainCheck);
  6323. Params.NeverAskAgainTitle = LoadStr(ADD_BOOKMARK_SHARED);
  6324. Params.NeverAskAgainCheckedInitially = WinConfiguration->UseSharedBookmarks;
  6325. UnicodeString Message = MainInstructions(FMTLOAD(ADD_BOOKMARK_CONFIRM, (DirView(Side)->PathName)));
  6326. unsigned int Answer =
  6327. MessageDialog(Message,
  6328. qtConfirmation, qaOK | qaCancel, HELP_ADD_BOOKMARK_CONFIRM, &Params);
  6329. if (Answer == qaNeverAskAgain)
  6330. {
  6331. Continue = true;
  6332. WinConfiguration->UseSharedBookmarks = true;
  6333. }
  6334. else if (Answer == qaOK)
  6335. {
  6336. Continue = true;
  6337. WinConfiguration->UseSharedBookmarks = false;
  6338. }
  6339. else
  6340. {
  6341. Continue = false;
  6342. }
  6343. }
  6344. if (Continue)
  6345. {
  6346. TStrings * VisitedDirectories = CreateVisitedDirectories(Side);
  6347. try
  6348. {
  6349. UnicodeString Name = DirView(Side)->PathName;
  6350. bool Repeat;
  6351. do
  6352. {
  6353. Repeat = false;
  6354. TOperationSide BookmarkSide = IsSideLocalBrowser(Side) ? osLocal : osRemote;
  6355. if (::DoOpenDirectoryDialog(Mode, BookmarkSide, Name, VisitedDirectories, Terminal,
  6356. // do not allow switching to location profiles,
  6357. // if we are not connected
  6358. HasDirView[osLocal] && (Terminal != NULL)))
  6359. {
  6360. TWindowLock Lock(this);
  6361. if (!TryOpenDirectory(Side, Name))
  6362. {
  6363. Repeat = true;
  6364. Mode = odBrowse;
  6365. }
  6366. }
  6367. }
  6368. while (Repeat);
  6369. }
  6370. __finally
  6371. {
  6372. delete VisitedDirectories;
  6373. }
  6374. }
  6375. }
  6376. //---------------------------------------------------------------------------
  6377. bool __fastcall TCustomScpExplorerForm::CommandSessionFallback()
  6378. {
  6379. DebugAssert(!Terminal->CommandSessionOpened);
  6380. return TTerminalManager::Instance()->ConnectTerminal(Terminal->CommandSession);
  6381. }
  6382. //---------------------------------------------------------------------------
  6383. bool __fastcall TCustomScpExplorerForm::EnsureCommandSessionFallback(TFSCapability Capability)
  6384. {
  6385. bool Result = Terminal->IsCapable[Capability] ||
  6386. Terminal->CommandSessionOpened;
  6387. if (!Result)
  6388. {
  6389. DebugAssert(Terminal->IsCapable[fcSecondaryShell]);
  6390. if (!GUIConfiguration->ConfirmCommandSession)
  6391. {
  6392. Result = true;
  6393. }
  6394. else
  6395. {
  6396. TMessageParams Params(mpNeverAskAgainCheck);
  6397. const TFileSystemInfo & FileSystemInfo = Terminal->GetFileSystemInfo();
  6398. unsigned int Answer = MessageDialog(FMTLOAD(PERFORM_ON_COMMAND_SESSION2,
  6399. (FileSystemInfo.ProtocolName, FileSystemInfo.ProtocolName)), qtConfirmation,
  6400. qaOK | qaCancel, HELP_PERFORM_ON_COMMAND_SESSION, &Params);
  6401. if (Answer == qaNeverAskAgain)
  6402. {
  6403. GUIConfiguration->ConfirmCommandSession = false;
  6404. Result = true;
  6405. }
  6406. else if (Answer == qaOK)
  6407. {
  6408. Result = true;
  6409. }
  6410. }
  6411. if (Result)
  6412. {
  6413. Result = CommandSessionFallback();
  6414. }
  6415. }
  6416. return Result;
  6417. }
  6418. //---------------------------------------------------------------------------
  6419. void __fastcall TCustomScpExplorerForm::OpenConsole(UnicodeString Command)
  6420. {
  6421. if (EnsureCommandSessionFallback(fcAnyCommand))
  6422. {
  6423. DoConsoleDialog(Terminal, Command);
  6424. }
  6425. }
  6426. //---------------------------------------------------------------------------
  6427. void __fastcall TCustomScpExplorerForm::FileControlDDDragEnter(
  6428. TObject *Sender, _di_IDataObject /*DataObj*/, int /*grfKeyState*/,
  6429. const TPoint & /*Point*/, int & /*dwEffect*/, bool & Accept)
  6430. {
  6431. if (!IsSideLocalBrowser(osRemote) &&
  6432. IsFileControl(DropSourceControl, osRemote) &&
  6433. (FDDExtMapFile != NULL))
  6434. {
  6435. Accept = true;
  6436. }
  6437. FDDTargetControl = dynamic_cast<TControl*>(Sender);
  6438. DebugAssert(FDDTargetControl != NULL);
  6439. }
  6440. //---------------------------------------------------------------------------
  6441. void __fastcall TCustomScpExplorerForm::SessionsDDDragEnter(
  6442. _di_IDataObject DataObj, int KeyState,
  6443. const TPoint & Point, int & Effect, bool & Accept)
  6444. {
  6445. FileControlDDDragEnter(SessionsPageControl, DataObj, KeyState, Point, Effect, Accept);
  6446. }
  6447. //---------------------------------------------------------------------------
  6448. void __fastcall TCustomScpExplorerForm::QueueDDDragEnter(
  6449. _di_IDataObject DataObj, int KeyState,
  6450. const TPoint & Point, int & Effect, bool & Accept)
  6451. {
  6452. FileControlDDDragEnter(QueueView3, DataObj, KeyState, Point, Effect, Accept);
  6453. }
  6454. //---------------------------------------------------------------------------
  6455. void __fastcall TCustomScpExplorerForm::FileControlDDDragLeave(
  6456. TObject * Sender)
  6457. {
  6458. DebugUsedParam(Sender);
  6459. DebugAssert(FDDTargetControl == Sender);
  6460. FDDTargetControl = NULL;
  6461. }
  6462. //---------------------------------------------------------------------------
  6463. void __fastcall TCustomScpExplorerForm::SessionsDDDragLeave()
  6464. {
  6465. FileControlDDDragLeave(SessionsPageControl);
  6466. }
  6467. //---------------------------------------------------------------------------
  6468. void __fastcall TCustomScpExplorerForm::QueueDDDragLeave()
  6469. {
  6470. FileControlDDDragLeave(QueueView3);
  6471. }
  6472. //---------------------------------------------------------------------------
  6473. void __fastcall TCustomScpExplorerForm::AddEditLink(TOperationSide Side, bool Add)
  6474. {
  6475. DebugAssert(!IsSideLocalBrowser(Side));
  6476. DebugUsedParam(Side);
  6477. bool Edit = false;
  6478. TRemoteFile * File = NULL;
  6479. UnicodeString FileName;
  6480. UnicodeString PointTo;
  6481. bool SymbolicLink = true;
  6482. if (RemoteDirView->ItemFocused)
  6483. {
  6484. DebugAssert(RemoteDirView->ItemFocused->Data);
  6485. File = (TRemoteFile *)RemoteDirView->ItemFocused->Data;
  6486. Edit = !Add && File->IsSymLink && Terminal->SessionData->ResolveSymlinks;
  6487. if (Edit)
  6488. {
  6489. FileName = File->FileName;
  6490. PointTo = File->LinkTo;
  6491. }
  6492. else if (!File->IsParentDirectory)
  6493. {
  6494. PointTo = File->FileName;
  6495. }
  6496. }
  6497. if (DoSymlinkDialog(FileName, PointTo, osRemote, SymbolicLink, Edit,
  6498. Terminal->IsCapable[fcHardLink]))
  6499. {
  6500. Configuration->Usage->Inc(L"RemoteLinksCreated");
  6501. if (Edit)
  6502. {
  6503. DebugAssert(File->FileName == FileName);
  6504. int Params = dfNoRecursive;
  6505. Terminal->ExceptionOnFail = true;
  6506. try
  6507. {
  6508. Terminal->DeleteFile(L"", File, &Params);
  6509. }
  6510. __finally
  6511. {
  6512. Terminal->ExceptionOnFail = false;
  6513. }
  6514. }
  6515. Terminal->CreateLink(FileName, PointTo, SymbolicLink);
  6516. }
  6517. }
  6518. //---------------------------------------------------------------------------
  6519. bool __fastcall TCustomScpExplorerForm::CanAddEditLink(TOperationSide Side)
  6520. {
  6521. return
  6522. (IsSideLocalBrowser(Side) ||
  6523. (HasActiveTerminal() &&
  6524. Terminal->ResolvingSymlinks &&
  6525. Terminal->IsCapable[fcSymbolicLink]));
  6526. }
  6527. //---------------------------------------------------------------------------
  6528. bool __fastcall TCustomScpExplorerForm::LinkFocused()
  6529. {
  6530. return
  6531. !IsSideLocalBrowser(FCurrentSide) &&
  6532. (RemoteDirView->ItemFocused != NULL) &&
  6533. ((TRemoteFile *)RemoteDirView->ItemFocused->Data)->IsSymLink &&
  6534. Terminal->SessionData->ResolveSymlinks;
  6535. }
  6536. //---------------------------------------------------------------------------
  6537. void __fastcall TCustomScpExplorerForm::ExecuteCurrentFile()
  6538. {
  6539. DebugAssert(!WinConfiguration->DisableOpenEdit);
  6540. FForceExecution = true;
  6541. try
  6542. {
  6543. DirView(osCurrent)->ExecuteCurrentFile();
  6544. }
  6545. __finally
  6546. {
  6547. FForceExecution = false;
  6548. }
  6549. }
  6550. //---------------------------------------------------------------------------
  6551. void __fastcall TCustomScpExplorerForm::ExecuteCurrentFileWith(bool OnFocused)
  6552. {
  6553. TEditorData ExternalEditor;
  6554. ExternalEditor.Editor = edExternal;
  6555. bool Remember = false;
  6556. if (DoEditorPreferencesDialog(&ExternalEditor, Remember, epmAdHoc, !IsSideLocalBrowser(osCurrent)))
  6557. {
  6558. if (Remember)
  6559. {
  6560. TEditorList * EditorList = new TEditorList;
  6561. try
  6562. {
  6563. *EditorList = *WinConfiguration->EditorList;
  6564. bool Found = false;
  6565. int i = 0;
  6566. while (!Found && (i < EditorList->Count))
  6567. {
  6568. const TEditorPreferences * Editor = EditorList->Editors[i];
  6569. if ((Editor->Data->Editor == edExternal) &&
  6570. (Editor->Data->ExternalEditor == ExternalEditor.ExternalEditor))
  6571. {
  6572. Found = true;
  6573. }
  6574. i++;
  6575. }
  6576. if (!Found)
  6577. {
  6578. EditorList->Add(new TEditorPreferences(ExternalEditor));
  6579. WinConfiguration->EditorList = EditorList;
  6580. }
  6581. }
  6582. __finally
  6583. {
  6584. delete EditorList;
  6585. }
  6586. }
  6587. ExecuteFile(osCurrent, efExternalEditor, &ExternalEditor, true, OnFocused);
  6588. }
  6589. }
  6590. //---------------------------------------------------------------------------
  6591. void __fastcall TCustomScpExplorerForm::DetachTerminal(TObject * ATerminal)
  6592. {
  6593. FEditorManager->ProcessFiles(FileTerminalRemoved, ATerminal);
  6594. if (FFileFindTerminal == ATerminal)
  6595. {
  6596. FFileFindTerminal = NULL;
  6597. HideFileFindDialog();
  6598. }
  6599. if (FClipboardTerminal == ATerminal)
  6600. {
  6601. ClipboardClear(); // implies ClipboardStop
  6602. }
  6603. if ((FOnSynchronizeAbort != NULL) &&
  6604. (ATerminal == FManagedSession))
  6605. {
  6606. FOnSynchronizeAbort(NULL);
  6607. }
  6608. }
  6609. //---------------------------------------------------------------------------
  6610. void __fastcall TCustomScpExplorerForm::TerminalRemoved(TObject * Sender)
  6611. {
  6612. DetachTerminal(Sender);
  6613. }
  6614. //---------------------------------------------------------------------------
  6615. void __fastcall TCustomScpExplorerForm::FileTerminalRemoved(const UnicodeString FileName,
  6616. TEditedFileData * Data, TObject * /*Token*/, void * Arg)
  6617. {
  6618. TTerminal * Terminal = static_cast<TTerminal *>(Arg);
  6619. DebugAssert(Terminal != NULL);
  6620. if (Data->Terminal == Terminal)
  6621. {
  6622. Data->Terminal = NULL;
  6623. }
  6624. }
  6625. //---------------------------------------------------------------------------
  6626. TManagedTerminal * TCustomScpExplorerForm::GetReplacementForLastSession()
  6627. {
  6628. return NULL;
  6629. }
  6630. //---------------------------------------------------------------------------
  6631. void __fastcall TCustomScpExplorerForm::LastTerminalClosed()
  6632. {
  6633. UpdateControls();
  6634. SessionColor = TColor(0);
  6635. UpdateRemotePathComboBox(false);
  6636. NeedSession(false);
  6637. }
  6638. //---------------------------------------------------------------------------
  6639. void __fastcall TCustomScpExplorerForm::NeedSession(bool Startup)
  6640. {
  6641. try
  6642. {
  6643. // Cache, as the login dialog can change its value.
  6644. // See also DoShow. Also see TTerminalManager::NewSession.
  6645. bool ShowLogin = WinConfiguration->ShowLoginWhenNoSession;
  6646. try
  6647. {
  6648. if (ShowLogin)
  6649. {
  6650. bool ReloadSessions = !Startup;
  6651. TTerminalManager::Instance()->NewSession(L"", ReloadSessions, this, true);
  6652. }
  6653. }
  6654. __finally
  6655. {
  6656. // Do not terminate, if we are only starting up and we are not showing Login dialog
  6657. // (so there was no chance for the user to open any session yet)
  6658. if ((ShowLogin || !Startup) &&
  6659. !WinConfiguration->KeepOpenWhenNoSession &&
  6660. // this rules out the implicit Commander's local-local browser
  6661. ((Terminal == NULL) || (!Terminal->Active && !Terminal->Permanent)))
  6662. {
  6663. TerminateApplication();
  6664. }
  6665. }
  6666. }
  6667. catch (EAbort &)
  6668. {
  6669. // swallow
  6670. // The TTerminalManager does not expect the OnLastTerminalClose to throw without trying to connect.
  6671. // Also when called from TWinControl.UpdateShowing => CMShowingChanged, the showing is aborted on exception
  6672. }
  6673. }
  6674. //---------------------------------------------------------------------------
  6675. void __fastcall TCustomScpExplorerForm::SessionListChanged(bool ForceTruncationUpdate)
  6676. {
  6677. TTerminalManager * Manager = TTerminalManager::Instance();
  6678. DebugAssert(!Manager->Updating);
  6679. TAutoFlag UpdatingSessionTabsFlag(FUpdatingSessionTabs);
  6680. int ActiveSessionIndex = Manager->ActiveSessionIndex;
  6681. Configuration->Usage->SetMax(L"MaxOpenedSessions", Manager->Count);
  6682. SendMessage(SessionsPageControl->Handle, WM_SETREDRAW, 0, 0);
  6683. try
  6684. {
  6685. int PrevTabsWidth = SessionsPageControl->TotalTabsWidth();
  6686. while (SessionsPageControl->PageCount > Manager->Count + 1)
  6687. {
  6688. delete SessionsPageControl->Pages[SessionsPageControl->PageCount - 1];
  6689. }
  6690. for (int Index = 0; Index <= Manager->Count; Index++)
  6691. {
  6692. TThemeTabSheet * TabSheet;
  6693. if (Index >= SessionsPageControl->PageCount)
  6694. {
  6695. TabSheet = new TThemeTabSheet(SessionsPageControl);
  6696. TabSheet->PageControl = SessionsPageControl;
  6697. }
  6698. else
  6699. {
  6700. TabSheet = SessionsPageControl->Pages[Index];
  6701. }
  6702. bool IsSessionTab = (Index < Manager->Count);
  6703. if (IsSessionTab)
  6704. {
  6705. TTerminal * Terminal = Manager->Sessions[Index];
  6706. TabSheet->Tag = reinterpret_cast<int>(Terminal);
  6707. TabSheet->CaptionTruncation = tttEllipsis;
  6708. UpdateSessionTab(TabSheet);
  6709. }
  6710. else
  6711. {
  6712. TabSheet->Tag = 0; // not really needed
  6713. TabSheet->Shadowed = false;
  6714. TabSheet->Button = SupportsLocalBrowser() ? ttbDropDown : ttbNone;
  6715. TabSheet->CaptionTruncation = tttNoText;
  6716. // 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)
  6717. UpdateNewTabTab();
  6718. }
  6719. }
  6720. // Prevent flicker on the right side where scrolling buttons would be, unless really needed
  6721. if (ForceTruncationUpdate || (SessionsPageControl->TotalTabsWidth() != PrevTabsWidth))
  6722. {
  6723. SessionsPageControl->UpdateTabsCaptionTruncation();
  6724. }
  6725. }
  6726. __finally
  6727. {
  6728. SendMessage(SessionsPageControl->Handle, WM_SETREDRAW, 1, 0);
  6729. }
  6730. SessionsPageControl->ActivePageIndex = ActiveSessionIndex;
  6731. }
  6732. //---------------------------------------------------------------------------
  6733. UnicodeString TCustomScpExplorerForm::GetNewTabTabCaption()
  6734. {
  6735. return StripHotkey(StripTrailingPunctuation(NonVisualDataModule->NewTabAction->Caption));
  6736. }
  6737. //---------------------------------------------------------------------------
  6738. void __fastcall TCustomScpExplorerForm::UpdateNewTabTab()
  6739. {
  6740. TThemeTabSheet * TabSheet = SessionsPageControl->Pages[SessionsPageControl->PageCount - 1];
  6741. UnicodeString TabCaption;
  6742. if (WinConfiguration->SelectiveToolbarText)
  6743. {
  6744. TabCaption = GetNewTabTabCaption();
  6745. }
  6746. TabSheet->BaseCaption = TabCaption;
  6747. TabSheet->ImageIndex = GetNewTabTabImageIndex(osCurrent);
  6748. }
  6749. //---------------------------------------------------------------------------
  6750. TManagedTerminal * __fastcall TCustomScpExplorerForm::GetSessionTabSession(TTabSheet * TabSheet)
  6751. {
  6752. return reinterpret_cast<TManagedTerminal *>(TabSheet->Tag);
  6753. }
  6754. //---------------------------------------------------------------------------
  6755. UnicodeString TCustomScpExplorerForm::GetLocalBrowserSessionTitle(TManagedTerminal *)
  6756. {
  6757. DebugFail();
  6758. return UnicodeString();
  6759. }
  6760. //---------------------------------------------------------------------------
  6761. void TCustomScpExplorerForm::UpdateSessionTab(TThemeTabSheet * TabSheet)
  6762. {
  6763. if (DebugAlwaysTrue(TabSheet != NULL))
  6764. {
  6765. TManagedTerminal * ASession = GetSessionTabSession(TabSheet);
  6766. if (DebugAlwaysTrue(ASession != NULL))
  6767. {
  6768. TColor Color = (ASession == Terminal) ? FSessionColor : ASession->StateData->Color;
  6769. if (ASession->LocalBrowser)
  6770. {
  6771. TabSheet->ImageIndex = FLocalBrowserTabImageIndex;
  6772. }
  6773. else
  6774. {
  6775. TabSheet->ImageIndex = AddSessionColor(Color);
  6776. }
  6777. TTerminalManager * Manager = TTerminalManager::Instance();
  6778. TabSheet->Shadowed = !ASession->Active && !ASession->LocalBrowser;
  6779. TabSheet->Button = CanCloseSession(ASession) ? ttbClose : ttbNone;
  6780. TabSheet->BaseCaption = Manager->GetSessionTitle(ASession, true);
  6781. }
  6782. }
  6783. }
  6784. //---------------------------------------------------------------------------
  6785. bool TCustomScpExplorerForm::CanCloseSession(TManagedTerminal * Session)
  6786. {
  6787. return !Session->LocalBrowser || (TTerminalManager::Instance()->Count > 1);
  6788. }
  6789. //---------------------------------------------------------------------------
  6790. bool __fastcall TCustomScpExplorerForm::SessionTabSwitched()
  6791. {
  6792. DebugAssert(SessionsPageControl->ActivePage != NULL);
  6793. TManagedTerminal * Session = GetSessionTabSession(SessionsPageControl->ActivePage);
  6794. bool Result = (Session != NULL);
  6795. if (Result)
  6796. {
  6797. TTerminalManager::Instance()->ActiveSession = Session;
  6798. }
  6799. else
  6800. {
  6801. try
  6802. {
  6803. NewTab();
  6804. }
  6805. __finally
  6806. {
  6807. SessionListChanged();
  6808. }
  6809. FSessionsPageControlNewTabTime = Now();
  6810. }
  6811. return Result;
  6812. }
  6813. //---------------------------------------------------------------------------
  6814. void __fastcall TCustomScpExplorerForm::SessionsPageControlChange(TObject * /*Sender*/)
  6815. {
  6816. TWindowLock Lock(this);
  6817. SessionTabSwitched();
  6818. }
  6819. //---------------------------------------------------------------------------
  6820. void __fastcall TCustomScpExplorerForm::TransferListChange(TObject * Sender)
  6821. {
  6822. TTBXStringList * TransferList = dynamic_cast<TTBXStringList *>(Sender);
  6823. DebugAssert(TransferList != NULL);
  6824. UnicodeString Name;
  6825. if (TransferList->ItemIndex <= 0)
  6826. {
  6827. Name = L"";
  6828. }
  6829. else
  6830. {
  6831. Name = GUIConfiguration->CopyParamList->Names[TransferList->ItemIndex - 1];
  6832. }
  6833. if (FCopyParamAutoSelected.IsEmpty())
  6834. {
  6835. // if previous preset was not autoselected, make new preset the "default"
  6836. FCopyParamDefault = Name;
  6837. }
  6838. GUIConfiguration->CopyParamCurrent = Name;
  6839. }
  6840. //---------------------------------------------------------------------------
  6841. void __fastcall TCustomScpExplorerForm::UpdateTransferLabel()
  6842. {
  6843. // sanity check
  6844. bool ExistingPreset =
  6845. (FTransferListHoverIndex >= 0) &&
  6846. (FTransferListHoverIndex < 1 + GUIConfiguration->CopyParamList->Count);
  6847. DebugAssert(ExistingPreset);
  6848. if (ExistingPreset)
  6849. {
  6850. HDC DC = GetDC(0);
  6851. TCanvas * Canvas = new TCanvas();
  6852. try
  6853. {
  6854. Canvas->Handle = DC;
  6855. Canvas->Font = GetToolbarFont(this);
  6856. UnicodeString Name;
  6857. if (FTransferListHoverIndex == 0)
  6858. {
  6859. Name = L"";
  6860. }
  6861. else
  6862. {
  6863. Name = GUIConfiguration->CopyParamList->Names[FTransferListHoverIndex - 1];
  6864. }
  6865. TTBXLabelItem * TransferLabel = dynamic_cast<TTBXLabelItem*>(
  6866. static_cast<TComponent*>(GetComponent(fcTransferLabel)));
  6867. TTBXStringList * TransferList = dynamic_cast<TTBXStringList*>(
  6868. static_cast<TObject*>(GetComponent(fcTransferList)));
  6869. UnicodeString InfoStr =
  6870. GUIConfiguration->CopyParamPreset[Name].
  6871. GetInfoStr(L"; ",
  6872. FLAGMASK(HasActiveTerminal(), Terminal->UsableCopyParamAttrs(0).General));
  6873. int MaxWidth = TransferList->MinWidth - (2 * TransferLabel->Margin) - ScaleByTextHeight(this, 10);
  6874. if (Canvas->TextExtent(InfoStr).cx > MaxWidth)
  6875. {
  6876. while (Canvas->TextExtent(InfoStr + Ellipsis).cx > MaxWidth)
  6877. {
  6878. InfoStr.SetLength(InfoStr.Length() - 1);
  6879. }
  6880. InfoStr += Ellipsis;
  6881. }
  6882. // UpdateCaption does not cause invalidation of whole submenu, while
  6883. // setting Caption property does.
  6884. // also it probably does not resize the label, even if necessary
  6885. // (we do not want that anyway)
  6886. TransferLabel->UpdateCaption(InfoStr);
  6887. }
  6888. __finally
  6889. {
  6890. Canvas->Handle = NULL;
  6891. ReleaseDC(0, DC);
  6892. delete Canvas;
  6893. }
  6894. }
  6895. }
  6896. //---------------------------------------------------------------------------
  6897. void __fastcall TCustomScpExplorerForm::TransferListDrawItem(
  6898. TTBXCustomList * /*Sender*/, TCanvas * /*ACanvas*/, const TRect & /*ARect*/,
  6899. int /*AIndex*/, int AHoverIndex, bool & /*DrawDefault*/)
  6900. {
  6901. if (FTransferListHoverIndex != AHoverIndex)
  6902. {
  6903. FTransferListHoverIndex = AHoverIndex;
  6904. UpdateTransferLabel();
  6905. }
  6906. }
  6907. //---------------------------------------------------------------------------
  6908. void __fastcall TCustomScpExplorerForm::WMAppCommand(TMessage & Message)
  6909. {
  6910. int Command = GET_APPCOMMAND_LPARAM(Message.LParam);
  6911. TShiftState Shift = KeyDataToShiftState(GET_KEYSTATE_LPARAM(Message.LParam));
  6912. if ((Shift * (TShiftState() << ssShift << ssAlt << ssCtrl)).Empty())
  6913. {
  6914. if (Command == APPCOMMAND_BROWSER_FAVORITES)
  6915. {
  6916. if (!NonVisualDataModule->Busy)
  6917. {
  6918. OpenDirectory(GetSide(osCurrent));
  6919. }
  6920. Message.Result = 1;
  6921. }
  6922. else
  6923. {
  6924. TForm::Dispatch(&Message);
  6925. }
  6926. }
  6927. else
  6928. {
  6929. TForm::Dispatch(&Message);
  6930. }
  6931. }
  6932. //---------------------------------------------------------------------------
  6933. void __fastcall TCustomScpExplorerForm::CMDialogChar(TMessage & AMessage)
  6934. {
  6935. TCMDialogChar & Message = reinterpret_cast<TCMDialogChar &>(AMessage);
  6936. if ((FIgnoreNextDialogChar != 0) &&
  6937. (toupper(Message.CharCode) == toupper(FIgnoreNextDialogChar)))
  6938. {
  6939. Message.Result = 1;
  6940. }
  6941. else
  6942. {
  6943. TForm::Dispatch(&Message);
  6944. }
  6945. FIgnoreNextDialogChar = 0;
  6946. }
  6947. //---------------------------------------------------------------------------
  6948. void __fastcall TCustomScpExplorerForm::WMSysCommand(TMessage & Message)
  6949. {
  6950. // The four low-order bits are used internally by Windows
  6951. unsigned int Cmd = (Message.WParam & 0xFFF0);
  6952. // SC_RESTORE, SC_MAXIMIZE, SC_MINIMIZE - buttons on windows title
  6953. // SC_DEFAULT - double click on windows title (does not work, at least on WinXP)
  6954. // 61730 - restore thru double click - undocumented
  6955. // 61490 - maximize thru double click - undocumented
  6956. if ((Cmd == SC_RESTORE) || (Cmd == SC_MAXIMIZE) ||
  6957. (Cmd == SC_MINIMIZE) || (Cmd == SC_DEFAULT))
  6958. {
  6959. SysResizing(Cmd);
  6960. }
  6961. TForm::Dispatch(&Message);
  6962. }
  6963. //---------------------------------------------------------------------------
  6964. void __fastcall TCustomScpExplorerForm::WMQueryEndSession(TMessage & Message)
  6965. {
  6966. // We were actually never able to make ENDSESSION_CRITICAL happen.
  6967. // Also there no point returning TRUE as we are not able to
  6968. // handle the abrupt termination caused by subsequent WM_ENDSESSION cleanly.
  6969. // Hence the process termination might be safer :)
  6970. if ((Message.LParam != ENDSESSION_CRITICAL) &&
  6971. (((FQueue != NULL) && !FQueue->IsEmpty) || (FProgressForm != NULL)))
  6972. {
  6973. Message.Result = FALSE;
  6974. }
  6975. else
  6976. {
  6977. Message.Result = TRUE;
  6978. }
  6979. // Do not call default handling as that triggers OnCloseQuery,
  6980. // where our implementation will popup confirmation dialogs, what we do not want,
  6981. // as per Vista guidelines:
  6982. // https://learn.microsoft.com/en-us/windows/win32/shutdown/shutting-down
  6983. }
  6984. //---------------------------------------------------------------------------
  6985. void __fastcall TCustomScpExplorerForm::WMEndSession(TWMEndSession & Message)
  6986. {
  6987. if (Message.EndSession && IsApplicationMinimized())
  6988. {
  6989. // WORKAROUND
  6990. // TApplication.WndProc() calls Application.Terminate() before Halt(),
  6991. // when it receives WM_ENDSESSION,
  6992. // but that sometimes (particularly when application is minimized) cause crashes.
  6993. // Crash popups message that blocks log off.
  6994. // Obviously application cannot shutdown cleanly after WM_ENDSESSION,
  6995. // so we call ExitProcess() immediately, not even trying to cleanup.
  6996. // It still causes beep, so there's likely some popup, but it does not block
  6997. // log off.
  6998. ExitProcess(0);
  6999. }
  7000. TForm::Dispatch(&Message);
  7001. }
  7002. #ifdef _DEBUG
  7003. //---------------------------------------------------------------------------
  7004. void __fastcall TCustomScpExplorerForm::WMWindowPosChanged(TWMWindowPosMsg & Message)
  7005. {
  7006. TForm::Dispatch(&Message);
  7007. }
  7008. #endif
  7009. //---------------------------------------------------------------------------
  7010. void __fastcall TCustomScpExplorerForm::SysResizing(unsigned int /*Cmd*/)
  7011. {
  7012. }
  7013. //---------------------------------------------------------------------------
  7014. void __fastcall TCustomScpExplorerForm::DoShow()
  7015. {
  7016. // Called from TCustomScpExplorerForm::CMShowingChanged
  7017. // only now are the controls resized finally, so the size constraints
  7018. // will not conflict with possibly very small window size
  7019. RestoreFormParams();
  7020. // This indicates that the form was rescaled after parameters were loaded and
  7021. // as most probably the parameters were actually saved with the current form scaling
  7022. // (windows was the last time likely closed on the same monitor, where it is starting now),
  7023. // rescaling happened twice already. Reload the parameters, as now they likely won't need any scaling
  7024. // and no rounding problems will occur.
  7025. // See also RestoreForm.
  7026. // (we should be safe to call RestoreParams unconditionally)
  7027. if (PixelsPerInch != Screen->PixelsPerInch)
  7028. {
  7029. RestoreParams();
  7030. }
  7031. FixControlsPlacement();
  7032. if (Position == poDefaultPosOnly)
  7033. {
  7034. CutFormToDesktop(this);
  7035. }
  7036. TForm::DoShow();
  7037. FSessionsDragDropFilesEx->DragDropControl = SessionsPageControl;
  7038. FQueueDragDropFilesEx->DragDropControl = QueueView3;
  7039. // This was previously in NeedSession, where other related code is.
  7040. // But that is called from CMShowingChanged, when the window is already visible and the local-local window state
  7041. // unpleasantly shows briefly.
  7042. if ((ManagedSession == NULL) &&
  7043. !WinConfiguration->ShowLoginWhenNoSession &&
  7044. WinConfiguration->AutoSaveWorkspace && !WinConfiguration->AutoWorkspace.IsEmpty() &&
  7045. // This detects if workspace was saved the last time the main widow was closed
  7046. SameText(WinConfiguration->LastStoredSession, WinConfiguration->AutoWorkspace))
  7047. {
  7048. DoOpenFolderOrWorkspace(WinConfiguration->AutoWorkspace, false, true);
  7049. Configuration->Usage->Inc(L"OpenedWorkspacesAuto");
  7050. }
  7051. // Before main window displays
  7052. if (ManagedSession == NULL)
  7053. {
  7054. StartingWithoutSession();
  7055. }
  7056. UpdatePixelsPerInchMainWindowCounter();
  7057. FShowing = true;
  7058. }
  7059. //---------------------------------------------------------------------------
  7060. void __fastcall TCustomScpExplorerForm::UpdatePixelsPerInchMainWindowCounter()
  7061. {
  7062. Configuration->Usage->Set(L"PixelsPerInchMainWindow", PixelsPerInch);
  7063. }
  7064. //---------------------------------------------------------------------------
  7065. void __fastcall TCustomScpExplorerForm::StartingWithoutSession()
  7066. {
  7067. SessionListChanged();
  7068. InitStatusBar();
  7069. UpdateControls();
  7070. }
  7071. //---------------------------------------------------------------------------
  7072. void __fastcall TCustomScpExplorerForm::PopupTrayBalloon(TTerminal * Terminal,
  7073. const UnicodeString & Str, TQueryType Type, Exception * E, unsigned int Seconds,
  7074. TNotifyEvent OnBalloonClick, TObject * UserData)
  7075. {
  7076. bool Do;
  7077. UnicodeString Message;
  7078. if (E == NULL)
  7079. {
  7080. Message = Str;
  7081. Do = true;
  7082. }
  7083. else
  7084. {
  7085. Do = ExceptionMessage(E, Message);
  7086. }
  7087. if (Do && WinConfiguration->BalloonNotifications)
  7088. {
  7089. UnicodeString Title;
  7090. if ((Terminal == NULL) && (Type == qtInformation) && ExtractMainInstructions(Message, Title))
  7091. {
  7092. Message = TrimLeft(Message);
  7093. }
  7094. else
  7095. {
  7096. Message = UnformatMessage(Message);
  7097. const ResourceString * Captions[] = { &_SMsgDlgConfirm, &_SMsgDlgWarning,
  7098. &_SMsgDlgError, &_SMsgDlgInformation, NULL };
  7099. Title = LoadResourceString(Captions[Type]);
  7100. if (Terminal != NULL)
  7101. {
  7102. TManagedTerminal * Session = DebugNotNull(dynamic_cast<TManagedTerminal *>(Terminal));
  7103. Title = TTerminalManager::Instance()->GetSessionTitle(Session, true) + TitleSeparator + Title;
  7104. }
  7105. }
  7106. if (Seconds == 0)
  7107. {
  7108. Seconds = WinConfiguration->NotificationsTimeout;
  7109. }
  7110. FTrayIcon->PopupBalloon(Title, Message, Type, Seconds * MSecsPerSec, OnBalloonClick, UserData);
  7111. }
  7112. }
  7113. //---------------------------------------------------------------------------
  7114. unsigned int __fastcall TCustomScpExplorerForm::MoreMessageDialog(const UnicodeString Message,
  7115. TStrings * MoreMessages, TQueryType Type, unsigned int Answers,
  7116. UnicodeString HelpKeyword, const TMessageParams * Params,
  7117. TTerminal * Terminal)
  7118. {
  7119. if (((WinConfiguration->ContinueOnError && (FErrorList != NULL)) ||
  7120. (FOnFeedSynchronizeError != NULL)) &&
  7121. (Params != NULL) && (Params->Params & mpAllowContinueOnError) )
  7122. {
  7123. if (FOnFeedSynchronizeError != NULL)
  7124. {
  7125. FOnFeedSynchronizeError(Message, MoreMessages, Type, HelpKeyword);
  7126. }
  7127. else
  7128. {
  7129. DebugAssert(FErrorList != NULL);
  7130. TStringList * MoreMessagesCopy = NULL;
  7131. if (MoreMessages)
  7132. {
  7133. MoreMessagesCopy = new TStringList();
  7134. MoreMessagesCopy->Assign(MoreMessages);
  7135. }
  7136. FErrorList->AddObject(Message, MoreMessagesCopy);
  7137. }
  7138. PopupTrayBalloon(Terminal, Message, Type);
  7139. return ContinueAnswer(Answers);
  7140. }
  7141. else
  7142. {
  7143. bool UseBalloon = FTrayIcon->Visible;
  7144. if (UseBalloon)
  7145. {
  7146. PopupTrayBalloon(Terminal, Message, Type);
  7147. }
  7148. unsigned int Result;
  7149. try
  7150. {
  7151. Result = ::MoreMessageDialog(Message, MoreMessages, Type, Answers, HelpKeyword, Params);
  7152. }
  7153. __finally
  7154. {
  7155. // cancel only balloon we popped up, otherwise we may cancel notification
  7156. // balloon that was there before the message dialog
  7157. // (such as "dd confirmation opt in balloon)
  7158. if (UseBalloon)
  7159. {
  7160. FTrayIcon->CancelBalloon();
  7161. }
  7162. }
  7163. return Result;
  7164. }
  7165. }
  7166. //---------------------------------------------------------------------------
  7167. void __fastcall TCustomScpExplorerForm::ShowExtendedException(
  7168. TTerminal * Terminal, Exception * E)
  7169. {
  7170. if (FTrayIcon->Visible)
  7171. {
  7172. PopupTrayBalloon(Terminal, L"", qtError, E);
  7173. }
  7174. // particularly prevent opening new session from jump list,
  7175. // while exception is shown
  7176. NonVisualDataModule->StartBusy();
  7177. try
  7178. {
  7179. ShowExtendedExceptionEx(Terminal, E);
  7180. }
  7181. __finally
  7182. {
  7183. NonVisualDataModule->EndBusy();
  7184. FTrayIcon->CancelBalloon();
  7185. }
  7186. }
  7187. //---------------------------------------------------------------------------
  7188. void __fastcall TCustomScpExplorerForm::SessionReady()
  7189. {
  7190. InitStatusBar();
  7191. // cannot rely on active page being page for active terminal,
  7192. // as it can happen that active page is the "new session" page
  7193. // (e.g. when reconnecting active terminal, while login dialog
  7194. // invoked from "new session" page is modal)
  7195. int ActiveSessionIndex = TTerminalManager::Instance()->ActiveSessionIndex;
  7196. UpdateSessionTab(SessionsPageControl->Pages[ActiveSessionIndex]);
  7197. }
  7198. //---------------------------------------------------------------------------
  7199. void __fastcall TCustomScpExplorerForm::InactiveTerminalException(
  7200. TTerminal * Terminal, Exception * E)
  7201. {
  7202. Notify(Terminal, L"", qtError, false, NULL, NULL, E);
  7203. if (!Terminal->Active)
  7204. {
  7205. int Index = TTerminalManager::Instance()->IndexOf(Terminal);
  7206. if (DebugAlwaysTrue((Index >= 0) && (Index < SessionsPageControl->PageCount)))
  7207. {
  7208. UpdateSessionTab(SessionsPageControl->Pages[Index]);
  7209. }
  7210. }
  7211. }
  7212. //---------------------------------------------------------------------------
  7213. void __fastcall TCustomScpExplorerForm::Notify(TTerminal * Terminal,
  7214. UnicodeString Message, TQueryType Type,
  7215. bool Important, TNotifyEvent OnClick, TObject * UserData, Exception * E)
  7216. {
  7217. if ((E == NULL) ||
  7218. ExceptionMessage(E, Message))
  7219. {
  7220. unsigned int Seconds = WinConfiguration->NotificationsTimeout;
  7221. if (Important)
  7222. {
  7223. Seconds *= 5;
  7224. }
  7225. UnicodeString NoteMessage(UnformatMessage(Message));
  7226. if (Terminal != NULL)
  7227. {
  7228. TManagedTerminal * Session = DebugNotNull(dynamic_cast<TManagedTerminal *>(Terminal));
  7229. NoteMessage = FORMAT(L"%s: %s",
  7230. (TTerminalManager::Instance()->GetSessionTitle(Session, true), NoteMessage));
  7231. }
  7232. if (WinConfiguration->BalloonNotifications)
  7233. {
  7234. AddNote(NoteMessage);
  7235. PopupTrayBalloon(Terminal, Message, Type, NULL, Seconds, OnClick, UserData);
  7236. }
  7237. else
  7238. {
  7239. FlashOnBackground();
  7240. PostNote(NoteMessage, Seconds, OnClick, UserData);
  7241. }
  7242. }
  7243. }
  7244. //---------------------------------------------------------------------------
  7245. void __fastcall TCustomScpExplorerForm::QueueEmptyNoteClicked(TObject * Sender)
  7246. {
  7247. RestoreApp();
  7248. TTerminalNoteData * TerminalNoteData = dynamic_cast<TTerminalNoteData *>(Sender);
  7249. if (DebugAlwaysTrue(TerminalNoteData != NULL) &&
  7250. !NonVisualDataModule->Busy)
  7251. {
  7252. TManagedTerminal * Terminal = TerminalNoteData->Terminal;
  7253. TTerminalManager::Instance()->ActiveSession = Terminal;
  7254. if (!ComponentVisible[fcQueueView])
  7255. {
  7256. ToggleQueueVisibility();
  7257. GoToQueue();
  7258. }
  7259. }
  7260. }
  7261. //---------------------------------------------------------------------------
  7262. void __fastcall TCustomScpExplorerForm::QueueEvent(TManagedTerminal * ATerminal,
  7263. TTerminalQueue * /*Queue*/, TQueueEvent Event)
  7264. {
  7265. UnicodeString Message;
  7266. TNotifyEvent OnClick = NULL;
  7267. TObject * UserData = NULL;
  7268. bool QueueInvisible = !ComponentVisible[fcQueueView] || IsApplicationMinimized();
  7269. switch (Event)
  7270. {
  7271. case qeEmptyButMonitored:
  7272. if ((ATerminal != Terminal) || QueueInvisible)
  7273. {
  7274. Message = LoadStr(BALLOON_QUEUE_EMPTY);
  7275. OnClick = QueueEmptyNoteClicked;
  7276. TTerminalNoteData * TerminalNoteData = new TTerminalNoteData();
  7277. TerminalNoteData->Terminal = ATerminal;
  7278. UserData = TerminalNoteData;
  7279. }
  7280. break;
  7281. case qeEmpty:
  7282. OperationComplete(ATerminal->QueueOperationStart);
  7283. break;
  7284. case qePendingUserAction:
  7285. if ((ATerminal != Terminal) ||
  7286. (QueueInvisible && !IsQueueAutoPopup()))
  7287. {
  7288. Message = LoadStr(BALLOON_QUEUE_USER_ACTION);
  7289. }
  7290. break;
  7291. default:
  7292. DebugFail();
  7293. }
  7294. if (!Message.IsEmpty())
  7295. {
  7296. Notify(ATerminal, Message, qtInformation, false, OnClick, UserData);
  7297. }
  7298. }
  7299. //---------------------------------------------------------------------------
  7300. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDCreateDragFileList(
  7301. TObject * /*Sender*/, TFileList * FileList, bool & Created)
  7302. {
  7303. if (FDDExtMapFile != NULL)
  7304. {
  7305. CloseHandle(FDDExtMapFile);
  7306. FDDExtMapFile = NULL;
  7307. }
  7308. if (WinConfiguration->DDFakeFile)
  7309. {
  7310. DDFakeFileInitDrag(FileList, Created);
  7311. }
  7312. }
  7313. //---------------------------------------------------------------------------
  7314. void __fastcall TCustomScpExplorerForm::DDFakeCreated(TObject * /*Sender*/, const UnicodeString FileName)
  7315. {
  7316. if (DebugAlwaysTrue(!FDragFakeDirectory.IsEmpty()) &&
  7317. SameText(ExtractFileName(FileName), ExtractFileName(FDragFakeDirectory)))
  7318. {
  7319. FFakeFileDropTarget = FileName;
  7320. }
  7321. }
  7322. //---------------------------------------------------------------------------
  7323. UnicodeString __fastcall TCustomScpExplorerForm::CreateFakeTransferDirectory()
  7324. {
  7325. UnicodeString Result = ExcludeTrailingBackslash(WinConfiguration->TemporaryDir());
  7326. if (!ForceDirectories(ApiPath(Result)))
  7327. {
  7328. throw Exception(FMTLOAD(CREATE_TEMP_DIR_ERROR, (Result)));
  7329. }
  7330. FileSetAttr(ApiPath(Result), faHidden);
  7331. return Result;
  7332. }
  7333. //---------------------------------------------------------------------------
  7334. void __fastcall TCustomScpExplorerForm::DDFakeFileInitDrag(TFileList * FileList,
  7335. bool & Created)
  7336. {
  7337. FFakeFileDropTarget = UnicodeString();
  7338. FDragFakeDirectory = CreateFakeTransferDirectory();
  7339. FileList->AddItem(NULL, FDragFakeDirectory);
  7340. Created = true;
  7341. if (!WinConfiguration->IsDDExtRunning() || WinConfiguration->IsDDExtBroken())
  7342. {
  7343. FDragFakeMonitors = StartCreationDirectoryMonitorsOnEachDrive(FILE_NOTIFY_CHANGE_DIR_NAME, DDFakeCreated);
  7344. }
  7345. FDDExtMapFile = CreateFileMappingA((HANDLE)0xFFFFFFFF, NULL, PAGE_READWRITE,
  7346. 0, sizeof(TDragExtCommStruct), AnsiString(DRAG_EXT_MAPPING).c_str());
  7347. {
  7348. TMutexGuard Guard(FDDExtMutex, DRAGEXT_MUTEX_RELEASE_TIMEOUT);
  7349. TDragExtCommStruct* CommStruct;
  7350. CommStruct = static_cast<TDragExtCommStruct*>(MapViewOfFile(FDDExtMapFile,
  7351. FILE_MAP_ALL_ACCESS, 0, 0, 0));
  7352. DebugAssert(CommStruct != NULL);
  7353. CommStruct->Version = TDragExtCommStruct::CurrentVersion;
  7354. CommStruct->Dragging = true;
  7355. wcsncpy(CommStruct->DropDest, FDragFakeDirectory.c_str(),
  7356. LENOF(CommStruct->DropDest));
  7357. NULL_TERMINATE(CommStruct->DropDest);
  7358. UnmapViewOfFile(CommStruct);
  7359. }
  7360. }
  7361. //---------------------------------------------------------------------------
  7362. bool __fastcall TCustomScpExplorerForm::RemoteFileControlFileOperation(
  7363. TObject * Sender, TFileOperation Operation, bool NoConfirmation, void * Param)
  7364. {
  7365. bool Result;
  7366. DebugAssert(!IsLocalBrowserMode());
  7367. if (Sender == RemoteDirView)
  7368. {
  7369. Result = ExecuteFileOperation(Operation, osRemote, true, NoConfirmation, Param);
  7370. }
  7371. else
  7372. {
  7373. DebugAssert(Sender == RemoteDriveView);
  7374. TStrings * FileList = RemoteDriveView->DragFileList();
  7375. try
  7376. {
  7377. Result = ExecuteFileOperation(Operation, osRemote, FileList, NoConfirmation, Param);
  7378. }
  7379. __finally
  7380. {
  7381. delete FileList;
  7382. }
  7383. }
  7384. if (FDDTargetControl == RemoteDriveView)
  7385. {
  7386. RemoteDriveView->UpdateDropTarget();
  7387. }
  7388. // foRemoteMove happens when file/dir is dragged within the remote tree view
  7389. // or from tree view to dir view.
  7390. // foMove happens when file/dir is dragged from remote tree view outside of
  7391. // application via dragex
  7392. if (((Operation == foRemoteMove) || (Operation == foMove)) &&
  7393. (DropSourceControl == RemoteDriveView))
  7394. {
  7395. RemoteDriveView->UpdateDropSource();
  7396. }
  7397. return Result;
  7398. }
  7399. //---------------------------------------------------------------------------
  7400. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDEnd(TObject * Sender)
  7401. {
  7402. DebugAssert(!IsLocalBrowserMode());
  7403. // This also handles drops of remote files to queue.
  7404. // Drops of local files (uploads) are handled in QueueDDProcessDropped.
  7405. SAFE_DESTROY(FDDFileList);
  7406. if (!FFakeFileDropTarget.IsEmpty())
  7407. {
  7408. RemoveDir(ApiPath(FFakeFileDropTarget));
  7409. }
  7410. if ((FDDExtMapFile != NULL) || (FDDTargetControl == QueueView3))
  7411. {
  7412. try
  7413. {
  7414. TDragResult DDResult = (Sender == RemoteDirView) ?
  7415. RemoteDirView->LastDDResult : RemoteDriveView->LastDDResult;
  7416. // Focus is moved to the target application,
  7417. // but as we are going to present the UI, we need to steal the focus back.
  7418. // This most likely won't work though (windows does not allow application
  7419. // to steal focus most of the time)
  7420. Application->BringToFront();
  7421. // On older version of Windows we never got drMove here, see also comment below.
  7422. // On Windows 10, we get the "move".
  7423. if ((DDResult == drCopy) || (DDResult == drMove) || (DDResult == drInvalid))
  7424. {
  7425. UnicodeString TargetDirectory;
  7426. TFileOperation Operation;
  7427. // drInvalid may mean drMove, see comment below
  7428. switch (DDResult)
  7429. {
  7430. case drCopy:
  7431. Operation = foCopy;
  7432. break;
  7433. case drMove:
  7434. Operation = foMove;
  7435. break;
  7436. default:
  7437. DebugFail();
  7438. case drInvalid:
  7439. // prefer "copy" for safety
  7440. Operation = FLAGSET(FLastDropEffect, DROPEFFECT_MOVE) ? foMove : foCopy;
  7441. break;
  7442. }
  7443. TTransferOperationParam Param;
  7444. UnicodeString CounterName;
  7445. bool ForceQueue;
  7446. if (!DDGetTarget(Param.TargetDirectory, ForceQueue, CounterName))
  7447. {
  7448. // we get drInvalid both if d&d was intercepted by ddext,
  7449. // and when users drops on no-drop location.
  7450. // we tell the difference by existence of response from ddext,
  7451. // so we ignore absence of response in this case
  7452. if (DDResult != drInvalid)
  7453. {
  7454. // here we know that the extension is installed,
  7455. // as it is checked as soon as drag&drop starts from
  7456. // RemoteFileControlDDCreateDragFileList
  7457. Configuration->Usage->Inc(L"DownloadsDragDropExternalExtTargetUnknown");
  7458. if (!WinConfiguration->DDExtInstalled || WinConfiguration->IsDDExtBroken())
  7459. {
  7460. throw ExtException(NULL, LoadStr(DD_TARGET_UNKNOWN), HELP_DD_TARGET_UNKNOWN);
  7461. }
  7462. else
  7463. {
  7464. throw ExtException(NULL, LoadStr(DRAGEXT_TARGET_UNKNOWN2), HELP_DRAGEXT_TARGET_UNKNOWN);
  7465. }
  7466. }
  7467. }
  7468. else
  7469. {
  7470. // download using fake file
  7471. Param.Temp = false;
  7472. Param.DragDrop = true;
  7473. if (ForceQueue)
  7474. {
  7475. Param.Queue = asOn;
  7476. }
  7477. if (Sender == RemoteDirView)
  7478. {
  7479. Param.Options = FLAGMASK(SelectedAllFilesInDirView(RemoteDirView), coAllFiles);
  7480. }
  7481. if (RemoteFileControlFileOperation(Sender, Operation,
  7482. (WinConfiguration->DDTransferConfirmation == asOff), &Param))
  7483. {
  7484. Configuration->Usage->Inc(CounterName);
  7485. }
  7486. }
  7487. }
  7488. }
  7489. __finally
  7490. {
  7491. CloseHandle(FDDExtMapFile);
  7492. FDDExtMapFile = NULL;
  7493. delete FDragFakeMonitors;
  7494. FDragFakeMonitors = NULL;
  7495. RemoveDir(ApiPath(FDragFakeDirectory));
  7496. FDragFakeDirectory = L"";
  7497. FFakeFileDropTarget = UnicodeString();
  7498. }
  7499. }
  7500. if (FDragDropSshTerminate.get() != NULL)
  7501. {
  7502. FDragDropSshTerminate->Rethrow();
  7503. }
  7504. }
  7505. //---------------------------------------------------------------------------
  7506. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDGiveFeedback(
  7507. TObject * /*Sender*/, int dwEffect, HRESULT & /*Result*/)
  7508. {
  7509. // Remember drop effect so we know (when user drops files), if we copy or move
  7510. FLastDropEffect = dwEffect;
  7511. }
  7512. //---------------------------------------------------------------------------
  7513. bool __fastcall TCustomScpExplorerForm::DDGetTarget(
  7514. UnicodeString & Directory, bool & ForceQueue, UnicodeString & CounterName)
  7515. {
  7516. bool Result;
  7517. if (FDDTargetControl == QueueView3)
  7518. {
  7519. Directory = DefaultDownloadTargetDirectory();
  7520. Result = true;
  7521. CounterName = L"DownloadsDragDropQueue";
  7522. ForceQueue = true;
  7523. }
  7524. else if (!FFakeFileDropTarget.IsEmpty())
  7525. {
  7526. Directory = ExcludeTrailingBackslash(ExtractFilePath(FFakeFileDropTarget));
  7527. Result = true;
  7528. ForceQueue = false;
  7529. CounterName = L"DownloadsDragDropFakeFile";
  7530. }
  7531. else
  7532. {
  7533. ForceQueue = false;
  7534. Enabled = false;
  7535. try
  7536. {
  7537. int Timer = 0;
  7538. Result = false;
  7539. while (!Result && (Timer < WinConfiguration->DDExtTimeout))
  7540. {
  7541. {
  7542. TMutexGuard Guard(FDDExtMutex, DRAGEXT_MUTEX_RELEASE_TIMEOUT);
  7543. TDragExtCommStruct* CommStruct;
  7544. CommStruct = static_cast<TDragExtCommStruct*>(MapViewOfFile(FDDExtMapFile,
  7545. FILE_MAP_ALL_ACCESS, 0, 0, 0));
  7546. DebugAssert(CommStruct != NULL);
  7547. Result = !CommStruct->Dragging;
  7548. if (Result)
  7549. {
  7550. Directory = ExtractFilePath(CommStruct->DropDest);
  7551. CounterName = L"DownloadsDragDropExternalExt";
  7552. }
  7553. UnmapViewOfFile(CommStruct);
  7554. }
  7555. if (!Result)
  7556. {
  7557. Sleep(50);
  7558. Timer += 50;
  7559. Application->ProcessMessages();
  7560. }
  7561. }
  7562. }
  7563. __finally
  7564. {
  7565. Enabled = true;
  7566. }
  7567. }
  7568. return Result;
  7569. }
  7570. //---------------------------------------------------------------------------
  7571. void __fastcall TCustomScpExplorerForm::AddDelayedDirectoryDeletion(
  7572. const UnicodeString TempDir, int SecDelay)
  7573. {
  7574. TDateTime Alarm = IncSecond(Now(), SecDelay);
  7575. FDelayedDeletionList->AddObject(TempDir, reinterpret_cast<TObject*>(Alarm.FileDate()));
  7576. if (FDelayedDeletionTimer == NULL)
  7577. {
  7578. DebugAssert(HandleAllocated());
  7579. FDelayedDeletionTimer = new TTimer(this);
  7580. FDelayedDeletionTimer->Interval = 10000;
  7581. FDelayedDeletionTimer->OnTimer = DoDelayedDeletion;
  7582. }
  7583. else
  7584. {
  7585. FDelayedDeletionTimer->Enabled = true;
  7586. }
  7587. }
  7588. //---------------------------------------------------------------------------
  7589. void __fastcall TCustomScpExplorerForm::DoDelayedDeletion(TObject * Sender)
  7590. {
  7591. DebugAssert(FDelayedDeletionList != NULL);
  7592. TDateTime N = Now();
  7593. TDateTime Alert;
  7594. UnicodeString Directory;
  7595. for (int Index = FDelayedDeletionList->Count-1; Index >= 0; Index--)
  7596. {
  7597. Alert = FileDateToDateTime(reinterpret_cast<int>(FDelayedDeletionList->Objects[Index]));
  7598. if ((N >= Alert) || (Sender == NULL))
  7599. {
  7600. Directory = FDelayedDeletionList->Strings[Index];
  7601. if (DeleteDirectory(ExcludeTrailingBackslash(Directory)))
  7602. {
  7603. FDelayedDeletionList->Delete(Index);
  7604. }
  7605. }
  7606. }
  7607. if (FDelayedDeletionList->Count == 0)
  7608. {
  7609. FDelayedDeletionTimer->Enabled = false;
  7610. }
  7611. }
  7612. //---------------------------------------------------------------------------
  7613. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDTargetDrop()
  7614. {
  7615. DebugAssert(!IsLocalBrowserMode());
  7616. if (IsFileControl(FDDTargetControl, osRemote) ||
  7617. (FDDTargetControl == SessionsPageControl))
  7618. {
  7619. TManagedTerminal * TargetTerminal = NULL;
  7620. TFileOperation Operation = foNone;
  7621. if (FDDTargetControl == SessionsPageControl)
  7622. {
  7623. TPoint Point = SessionsPageControl->ScreenToClient(Mouse->CursorPos);
  7624. int Index = SessionsPageControl->IndexOfTabAt(Point.X, Point.Y);
  7625. // do not allow dropping on local-local, disconnected and the "+" tab (see also SessionsDDProcessDropped)
  7626. TargetTerminal = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  7627. if (IsActiveTerminal(TargetTerminal) && DebugAlwaysTrue(!TargetTerminal->LocalBrowser))
  7628. {
  7629. if ((FLastDropEffect == DROPEFFECT_MOVE) &&
  7630. (TargetTerminal == TTerminalManager::Instance()->ActiveTerminal))
  7631. {
  7632. Operation = foRemoteMove;
  7633. }
  7634. else
  7635. {
  7636. Operation = foRemoteCopy;
  7637. }
  7638. }
  7639. }
  7640. else
  7641. {
  7642. if (FLastDropEffect == DROPEFFECT_MOVE)
  7643. {
  7644. Operation = foRemoteMove;
  7645. }
  7646. else if (FLastDropEffect == DROPEFFECT_COPY)
  7647. {
  7648. Operation = foRemoteCopy;
  7649. }
  7650. }
  7651. if (Operation != foNone)
  7652. {
  7653. RemoteFileControlFileOperation(DropSourceControl,
  7654. Operation, (WinConfiguration->DDTransferConfirmation == asOff), TargetTerminal);
  7655. }
  7656. // abort drag&drop
  7657. Abort();
  7658. }
  7659. else if ((FDDExtMapFile == NULL) && (FLastDropEffect != DROPEFFECT_NONE) &&
  7660. // Drops of remote files to queue are handled in RemoteFileControlDDEnd
  7661. (FDDTargetControl != QueueView3))
  7662. {
  7663. DebugAssert(!FDragTempDir.IsEmpty());
  7664. TTransferType Type;
  7665. UnicodeString TempDir = FDragTempDir;
  7666. // We clear FDragTempDir before calling
  7667. // just in case it fail (raises exception)
  7668. FDragTempDir = L"";
  7669. Type = (FLastDropEffect & DROPEFFECT_MOVE ? ttMove : Type = ttCopy);
  7670. TGUICopyParamType CopyParams = GUIConfiguration->CurrentCopyParam;
  7671. // empty directory parameter means temp directory -> don't display it!
  7672. UnicodeString TargetDir = L"";
  7673. int Options = 0;
  7674. if (!CopyParamDialog(tdToLocal, Type, true, FDDFileList,
  7675. TargetDir, CopyParams, (WinConfiguration->DDTransferConfirmation != asOff), true, Options))
  7676. {
  7677. Abort();
  7678. }
  7679. // TargetDir is set when dropped on local file control
  7680. // (this was workaround for legacy dirview event handling, now it should be
  7681. // made prettier)
  7682. if (TargetDir.IsEmpty())
  7683. {
  7684. TargetDir = TempDir;
  7685. if (ForceDirectories(ApiPath(TargetDir)))
  7686. {
  7687. DebugAssert(Terminal && !TargetDir.IsEmpty());
  7688. FPendingTempSpaceWarn = true;
  7689. try
  7690. {
  7691. FDragDropOperation = true;
  7692. int Params = cpTemporary |
  7693. (Type == ttMove ? cpDelete : 0);
  7694. DDDownload(FDDFileList, TargetDir, &CopyParams, Params);
  7695. Configuration->Usage->Inc(L"DownloadsDragDropExternalTemp");
  7696. }
  7697. __finally
  7698. {
  7699. FDragDropOperation = false;
  7700. FPendingTempSpaceWarn = false;
  7701. AddDelayedDirectoryDeletion(TargetDir, WinConfiguration->DDDeleteDelay);
  7702. }
  7703. }
  7704. else
  7705. {
  7706. throw Exception(FMTLOAD(CREATE_TEMP_DIR_ERROR, (TargetDir)));
  7707. }
  7708. }
  7709. }
  7710. }
  7711. //---------------------------------------------------------------------------
  7712. void __fastcall TCustomScpExplorerForm::DDDownload(
  7713. TStrings * FilesToCopy, const UnicodeString & TargetDir, TCopyParamType * CopyParam, int Params)
  7714. {
  7715. DebugAssert(!IsLocalBrowserMode());
  7716. TAutoBatch AutoBatch(this);
  7717. CopyParam->IncludeFileMask.SetRoots(TargetDir, FilesToCopy);
  7718. UpdateCopyParamCounters(*CopyParam);
  7719. Terminal->CopyToLocal(FilesToCopy, TargetDir, CopyParam, Params, NULL);
  7720. if (FLAGSET(Params, cpDelete) && (DropSourceControl == RemoteDriveView))
  7721. {
  7722. RemoteDriveView->UpdateDropSource();
  7723. }
  7724. }
  7725. //---------------------------------------------------------------------------
  7726. class TFakeDataObjectFilesEx : public TDataObjectFilesEx
  7727. {
  7728. public:
  7729. __fastcall TFakeDataObjectFilesEx(TFileList * AFileList, bool RenderPIDL,
  7730. bool RenderFilename) : TDataObjectFilesEx(AFileList, RenderPIDL, RenderFilename, true)
  7731. {
  7732. }
  7733. virtual bool __fastcall AllowData(const tagFORMATETC & FormatEtc)
  7734. {
  7735. return (FormatEtc.cfFormat == CF_HDROP) ? false :
  7736. TDataObjectFilesEx::AllowData(FormatEtc);
  7737. }
  7738. };
  7739. //---------------------------------------------------------------------------
  7740. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDCreateDataObject(
  7741. TObject * Sender, TDataObject *& DataObject)
  7742. {
  7743. if (FDDExtMapFile != NULL)
  7744. {
  7745. TFileList * FileList = DragDropFiles(Sender)->FileList;
  7746. if (!FileList->RenderPIDLs() || !FileList->RenderNames())
  7747. {
  7748. Abort();
  7749. }
  7750. if (FileList->Count > 0)
  7751. {
  7752. TDataObjectFilesEx * FilesObject = new TFakeDataObjectFilesEx(FileList, true, true);
  7753. if (!FilesObject->IsValid(true, true))
  7754. {
  7755. FilesObject->_Release();
  7756. }
  7757. else
  7758. {
  7759. DataObject = FilesObject;
  7760. }
  7761. }
  7762. }
  7763. }
  7764. //---------------------------------------------------------------------------
  7765. void __fastcall TCustomScpExplorerForm::GoToCommandLine()
  7766. {
  7767. DebugFail();
  7768. }
  7769. //---------------------------------------------------------------------------
  7770. void __fastcall TCustomScpExplorerForm::GoToTree()
  7771. {
  7772. ComponentVisible[fcRemoteTree] = true;
  7773. DriveView(osRemote)->SetFocus();
  7774. }
  7775. //---------------------------------------------------------------------------
  7776. TStrings * __fastcall TCustomScpExplorerForm::PanelExport(TOperationSide Side,
  7777. TPanelExport Export)
  7778. {
  7779. TCustomDirView * DirView = this->DirView(Side);
  7780. std::unique_ptr<TStrings> ExportData(new TStringList());
  7781. switch (Export)
  7782. {
  7783. case pePath:
  7784. ExportData->Add(DirView->PathName);
  7785. break;
  7786. case peFileList:
  7787. case peFullFileList:
  7788. {
  7789. bool FullPath = (Export == peFullFileList);
  7790. DirView->CreateFileList(false, FullPath, ExportData.get());
  7791. for (int Index = 0; Index < ExportData->Count; Index++)
  7792. {
  7793. if (ExportData->Strings[Index].Pos(L" ") > 0)
  7794. {
  7795. ExportData->Strings[Index] = FORMAT(L"\"%s\"", (ExportData->Strings[Index]));
  7796. }
  7797. }
  7798. }
  7799. break;
  7800. default:
  7801. DebugFail();
  7802. }
  7803. return ExportData.release();
  7804. }
  7805. //---------------------------------------------------------------------------
  7806. void __fastcall TCustomScpExplorerForm::PanelExport(TOperationSide Side,
  7807. TPanelExport Export, TPanelExportDestination Destination)
  7808. {
  7809. std::unique_ptr<TStrings> ExportData(PanelExport(Side, Export));
  7810. PanelExportStore(Side, Export, Destination, ExportData.get());
  7811. }
  7812. //---------------------------------------------------------------------------
  7813. void __fastcall TCustomScpExplorerForm::PanelExportStore(TOperationSide /*Side*/,
  7814. TPanelExport /*Export*/, TPanelExportDestination Destination,
  7815. TStrings * ExportData)
  7816. {
  7817. if (Destination == pedClipboard)
  7818. {
  7819. TInstantOperationVisualizer Visualizer;
  7820. CopyToClipboard(ExportData);
  7821. }
  7822. else
  7823. {
  7824. DebugFail();
  7825. }
  7826. }
  7827. //---------------------------------------------------------------------------
  7828. void __fastcall TCustomScpExplorerForm::Filter(TOperationSide Side)
  7829. {
  7830. TCustomDirView * DirView = this->DirView(Side);
  7831. UnicodeString Mask = DirView->Mask;
  7832. if (DoFilterMaskDialog(DirView, Mask))
  7833. {
  7834. DirView->Mask = TFileMasks::NormalizeMask(Mask);
  7835. Configuration->Usage->Inc(L"Filters");
  7836. }
  7837. }
  7838. //---------------------------------------------------------------------------
  7839. TQueueOperation __fastcall TCustomScpExplorerForm::DefaultQueueOperation()
  7840. {
  7841. return FQueueController->DefaultOperation();
  7842. }
  7843. //---------------------------------------------------------------------------
  7844. bool __fastcall TCustomScpExplorerForm::AllowQueueOperation(
  7845. TQueueOperation Operation, void ** Param)
  7846. {
  7847. switch (Operation)
  7848. {
  7849. case qoPreferences:
  7850. return true;
  7851. case qoGoTo:
  7852. return ComponentVisible[fcQueueView] && QueueView3->Enabled;
  7853. case qoOnceEmpty:
  7854. return IsAnythingQueued();
  7855. default:
  7856. return FQueueController->AllowOperation(Operation, Param);
  7857. }
  7858. }
  7859. //---------------------------------------------------------------------------
  7860. void __fastcall TCustomScpExplorerForm::GoToQueue()
  7861. {
  7862. if (DebugAlwaysTrue(QueueView3->Visible))
  7863. {
  7864. QueueView3->SetFocus();
  7865. }
  7866. }
  7867. //---------------------------------------------------------------------------
  7868. void __fastcall TCustomScpExplorerForm::ExecuteQueueOperation(
  7869. TQueueOperation Operation, void * Param)
  7870. {
  7871. if (Operation == qoGoTo)
  7872. {
  7873. GoToQueue();
  7874. }
  7875. else if (Operation == qoPreferences)
  7876. {
  7877. PreferencesDialog(pmQueue);
  7878. }
  7879. else
  7880. {
  7881. FQueueController->ExecuteOperation(Operation, Param);
  7882. }
  7883. }
  7884. //---------------------------------------------------------------------------
  7885. bool __fastcall TCustomScpExplorerForm::GetQueueEnabled()
  7886. {
  7887. return (Queue != NULL) && Queue->Enabled;
  7888. }
  7889. //---------------------------------------------------------------------------
  7890. void __fastcall TCustomScpExplorerForm::ToggleQueueEnabled()
  7891. {
  7892. DebugAssert(Queue != NULL);
  7893. if (Queue != NULL)
  7894. {
  7895. Queue->Enabled = !Queue->Enabled;
  7896. }
  7897. }
  7898. //---------------------------------------------------------------------------
  7899. void __fastcall TCustomScpExplorerForm::QueueView3ContextPopup(
  7900. TObject * /*Sender*/, TPoint & /*MousePos*/, bool & /*Handled*/)
  7901. {
  7902. FQueueActedItem = QueueView3->ItemFocused;
  7903. }
  7904. //---------------------------------------------------------------------------
  7905. /*virtual*/ int __fastcall TCustomScpExplorerForm::GetStaticComponentsHeight()
  7906. {
  7907. return TopDock->Height + (MessageDock->Visible ? MessageDock->Height : 0) + QueueSplitter->Height;
  7908. }
  7909. //---------------------------------------------------------------------------
  7910. int __fastcall TCustomScpExplorerForm::GetStaticQueuePanelComponentsHeight()
  7911. {
  7912. return
  7913. (QueueFileListSplitter->Visible ? QueueFileListSplitter->Height : 0) +
  7914. (QueueDock->Visible ? QueueDock->Height : 0) +
  7915. (QueueLabel->Visible ? QueueLabel->Height : 0);
  7916. }
  7917. //---------------------------------------------------------------------------
  7918. int __fastcall TCustomScpExplorerForm::GetMinQueueViewHeight()
  7919. {
  7920. return ScaleByTextHeight(this, 48);
  7921. }
  7922. //---------------------------------------------------------------------------
  7923. void __fastcall TCustomScpExplorerForm::QueueSplitterCanResize(
  7924. TObject * /*Sender*/, int & NewSize, bool & Accept)
  7925. {
  7926. // when queue is hidden by double-clicking splitter, stray attempt to
  7927. // resize the panel with strange value arrives, make sure it is ignored
  7928. if (ComponentVisible[fcQueueView])
  7929. {
  7930. int MaxHeightLimit = ClientHeight - GetStaticComponentsHeight() - RemotePanel->Constraints->MinHeight;
  7931. if (NewSize > MaxHeightLimit)
  7932. {
  7933. NewSize = MaxHeightLimit;
  7934. }
  7935. int MinHeightLimit =
  7936. GetStaticQueuePanelComponentsHeight() +
  7937. (QueueFileList->Visible ? QueueFileList->Height : 0) +
  7938. GetMinQueueViewHeight();
  7939. if (NewSize < MinHeightLimit)
  7940. {
  7941. NewSize = MinHeightLimit;
  7942. }
  7943. }
  7944. else
  7945. {
  7946. Accept = false;
  7947. }
  7948. }
  7949. //---------------------------------------------------------------------------
  7950. void __fastcall TCustomScpExplorerForm::QueueFileListSplitterCanResize(TObject *, int & NewSize, bool &)
  7951. {
  7952. int TotalHeight = GetStaticQueuePanelComponentsHeight() + NewSize;
  7953. int QueueViewHeight = QueuePanel->ClientHeight - TotalHeight;
  7954. if (QueueViewHeight < GetMinQueueViewHeight())
  7955. {
  7956. NewSize -= (GetMinQueueViewHeight() - QueueViewHeight);
  7957. }
  7958. }
  7959. //---------------------------------------------------------------------------
  7960. void __fastcall TCustomScpExplorerForm::QueueView3StartDrag(TObject * /*Sender*/,
  7961. TDragObject *& /*DragObject*/)
  7962. {
  7963. FQueueActedItem = QueueView3->ItemFocused;
  7964. FQueueScrollOnDragOver->StartDrag();
  7965. }
  7966. //---------------------------------------------------------------------------
  7967. void __fastcall TCustomScpExplorerForm::QueueView3EndDrag(
  7968. TObject *, TObject * DebugUsedArg(Target), int DebugUsedArg(X), int DebugUsedArg(Y))
  7969. {
  7970. FQueueScrollOnDragOver->EndDrag();
  7971. }
  7972. //---------------------------------------------------------------------------
  7973. void __fastcall TCustomScpExplorerForm::QueueView3DragOver(TObject * /*Sender*/,
  7974. TObject * Source, int X, int Y, TDragState /*State*/, bool & Accept)
  7975. {
  7976. Accept = true;
  7977. if (Source == QueueView3)
  7978. {
  7979. TListItem * DropTarget = QueueView3->GetItemAt(X, Y);
  7980. Accept = (DropTarget != NULL) && (FQueueActedItem != NULL);
  7981. if (Accept)
  7982. {
  7983. TQueueItemProxy * QueueItem;
  7984. TQueueItemProxy * DestQueueItem;
  7985. QueueItem = static_cast<TQueueItemProxy *>(FQueueActedItem->Data);
  7986. DestQueueItem = static_cast<TQueueItemProxy *>(DropTarget->Data);
  7987. Accept = (QueueItem != DestQueueItem) &&
  7988. (QueueItem->Status == TQueueItem::qsPending) &&
  7989. (DestQueueItem->Status == TQueueItem::qsPending);
  7990. }
  7991. FQueueScrollOnDragOver->DragOver(TPoint(X, Y));
  7992. }
  7993. }
  7994. //---------------------------------------------------------------------------
  7995. void __fastcall TCustomScpExplorerForm::QueueView3DragDrop(TObject * /*Sender*/,
  7996. TObject * /*Source*/, int /*X*/, int /*Y*/)
  7997. {
  7998. if ((FQueueActedItem != NULL) && (QueueView3->DropTarget != NULL))
  7999. {
  8000. TQueueItemProxy * QueueItem;
  8001. TQueueItemProxy * DestQueueItem;
  8002. QueueItem = static_cast<TQueueItemProxy *>(FQueueActedItem->Data);
  8003. DestQueueItem = static_cast<TQueueItemProxy *>(QueueView3->DropTarget->Data);
  8004. QueueItem->Move(DestQueueItem);
  8005. }
  8006. }
  8007. //---------------------------------------------------------------------------
  8008. void __fastcall TCustomScpExplorerForm::QueueView3Enter(TObject * /*Sender*/)
  8009. {
  8010. if ((QueueView3->ItemFocused == NULL) &&
  8011. (QueueView3->Items->Count > 0))
  8012. {
  8013. QueueView3->ItemFocused = QueueView3->Items->Item[0];
  8014. }
  8015. QueueLabelUpdateStatus();
  8016. }
  8017. //---------------------------------------------------------------------------
  8018. void __fastcall TCustomScpExplorerForm::QueueView3Exit(TObject * /*Sender*/)
  8019. {
  8020. QueueLabelUpdateStatus();
  8021. }
  8022. //---------------------------------------------------------------------------
  8023. void __fastcall TCustomScpExplorerForm::QueueLabelUpdateStatus()
  8024. {
  8025. QueueLabel->UpdateStatus();
  8026. }
  8027. //---------------------------------------------------------------------------
  8028. void __fastcall TCustomScpExplorerForm::QueueView3SelectItem(
  8029. TObject * /*Sender*/, TListItem * /*Item*/, bool Selected)
  8030. {
  8031. if (Selected)
  8032. {
  8033. NonVisualDataModule->UpdateNonVisibleActions();
  8034. }
  8035. }
  8036. //---------------------------------------------------------------------------
  8037. TDragDropFilesEx * __fastcall TCustomScpExplorerForm::DragDropFiles(TObject * Sender)
  8038. {
  8039. TDragDropFilesEx * Result = NULL;
  8040. if (Sender == SessionsPageControl)
  8041. {
  8042. Result = FSessionsDragDropFilesEx;
  8043. }
  8044. else if (Sender == QueueView3)
  8045. {
  8046. Result = FQueueDragDropFilesEx;
  8047. }
  8048. else
  8049. {
  8050. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  8051. if (DirView != NULL)
  8052. {
  8053. Result = DirView->DragDropFilesEx;
  8054. }
  8055. else
  8056. {
  8057. TCustomDriveView * DriveView = dynamic_cast<TCustomDriveView *>(Sender);
  8058. if (DriveView != NULL)
  8059. {
  8060. Result = DriveView->DragDropFilesEx;
  8061. }
  8062. }
  8063. }
  8064. DebugAssert(Result != NULL);
  8065. return Result;
  8066. }
  8067. //---------------------------------------------------------------------------
  8068. bool __fastcall TCustomScpExplorerForm::SelectedAllFilesInDirView(TCustomDirView * DView)
  8069. {
  8070. return (DView->SelCount == DView->FilesCount);
  8071. }
  8072. //---------------------------------------------------------------------------
  8073. bool __fastcall TCustomScpExplorerForm::DraggingAllFilesFromDirView(TOperationSide Side, TStrings * FileList)
  8074. {
  8075. return HasDirView[Side] && (DropSourceControl == DirView(Side)) && (FileList->Count == DirView(Side)->FilesCount);
  8076. }
  8077. //---------------------------------------------------------------------------
  8078. void __fastcall TCustomScpExplorerForm::RemoteFileControlDragDropFileOperation(
  8079. TObject * Sender, int Effect, UnicodeString TargetPath, bool ForceQueue, bool DragDrop)
  8080. {
  8081. DebugAssert(!IsLocalBrowserMode());
  8082. TFileOperation Operation;
  8083. switch (Effect)
  8084. {
  8085. case DROPEFFECT_MOVE:
  8086. Operation = foMove;
  8087. break;
  8088. case DROPEFFECT_COPY:
  8089. // occures on WinXP (reported by user)
  8090. default:
  8091. Operation = foCopy;
  8092. break;
  8093. };
  8094. TDragDropFilesEx * DragDropFilesEx = DragDropFiles(Sender);
  8095. // see a comment in TUnixDirView::PerformItemDragDropOperation
  8096. if (DragDropFilesEx->FileList->Count > 0)
  8097. {
  8098. TStrings * FileList = new TStringList();
  8099. try
  8100. {
  8101. for (int Index = 0; Index < DragDropFilesEx->FileList->Count; Index++)
  8102. {
  8103. FileList->Add(DragDropFilesEx->FileList->Items[Index]->Name);
  8104. }
  8105. FDragDropOperation = true;
  8106. TTransferOperationParam Param;
  8107. Param.TargetDirectory = TargetPath;
  8108. // upload, no temp dirs
  8109. Param.Temp = false;
  8110. Param.DragDrop = DragDrop;
  8111. Param.Options =
  8112. FLAGMASK(DraggingAllFilesFromDirView(osLocal, FileList), coAllFiles);
  8113. if (ForceQueue)
  8114. {
  8115. Param.Queue = asOn;
  8116. }
  8117. bool NoConfirmation = DragDrop ? (WinConfiguration->DDTransferConfirmation == asOff) : false;
  8118. if (ExecuteFileOperation(Operation, osLocal, FileList, NoConfirmation, &Param))
  8119. {
  8120. if (IsFileControl(DropSourceControl, osLocal))
  8121. {
  8122. Configuration->Usage->Inc(L"UploadsDragDropInternal");
  8123. }
  8124. else
  8125. {
  8126. Configuration->Usage->Inc(L"UploadsDragDropExternal");
  8127. }
  8128. }
  8129. }
  8130. __finally
  8131. {
  8132. FDragDropOperation = false;
  8133. delete FileList;
  8134. }
  8135. }
  8136. }
  8137. //---------------------------------------------------------------------------
  8138. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDFileOperation(
  8139. TObject * Sender, int Effect, UnicodeString /*SourcePath*/,
  8140. UnicodeString TargetPath, bool Paste, bool & /*DoOperation*/)
  8141. {
  8142. RemoteFileControlDragDropFileOperation(Sender, Effect, TargetPath, false, !Paste);
  8143. }
  8144. //---------------------------------------------------------------------------
  8145. void __fastcall TCustomScpExplorerForm::RemoteFileContolDDChooseEffect(
  8146. TObject * Sender, int grfKeyState, int & dwEffect)
  8147. {
  8148. DebugAssert(!IsLocalBrowserMode());
  8149. // if any drop effect is allowed at all (e.g. no drop to self and drop to parent)
  8150. if ((dwEffect != DROPEFFECT_NONE) &&
  8151. IsFileControl(DropSourceControl, osRemote))
  8152. {
  8153. // do not allow drop on remote panel (on free space, still allow drop on directories)
  8154. if ((Sender == RemoteDirView) && (DropSourceControl == RemoteDirView) &&
  8155. (RemoteDirView->DropTarget == NULL))
  8156. {
  8157. dwEffect = DROPEFFECT_NONE;
  8158. }
  8159. else
  8160. {
  8161. if (dwEffect == DROPEFFECT_COPY)
  8162. {
  8163. bool MoveCapable = Terminal->IsCapable[fcRemoteMove];
  8164. // currently we support copying always (at least via temporary directory);
  8165. // remove associated checks once this all proves stable and working
  8166. bool CopyCapable = true;
  8167. // if we do not support neither of operations, there's no discussion
  8168. if (!MoveCapable && !CopyCapable)
  8169. {
  8170. dwEffect = DROPEFFECT_NONE;
  8171. }
  8172. else
  8173. {
  8174. // The default effect inside remote panel is move,
  8175. // unless we do not support it, but support copy
  8176. if (FLAGCLEAR(grfKeyState, MK_CONTROL))
  8177. {
  8178. dwEffect = MoveCapable ? DROPEFFECT_MOVE : DROPEFFECT_COPY;
  8179. }
  8180. else
  8181. {
  8182. // with ctrl-down, we want copy unless it is not supported
  8183. dwEffect = CopyCapable ? DROPEFFECT_COPY : DROPEFFECT_NONE;
  8184. }
  8185. }
  8186. }
  8187. }
  8188. }
  8189. }
  8190. //---------------------------------------------------------------------------
  8191. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDDragFileName(
  8192. TObject * Sender, TRemoteFile * File, UnicodeString & FileName)
  8193. {
  8194. DebugAssert(!IsLocalBrowserMode());
  8195. if (FDDTotalSize >= 0)
  8196. {
  8197. if (File->IsDirectory)
  8198. {
  8199. FDDTotalSize = -1;
  8200. }
  8201. else
  8202. {
  8203. FDDTotalSize += File->Size;
  8204. }
  8205. }
  8206. DebugAssert(!FDragTempDir.IsEmpty());
  8207. // TODO: this is quite ineffective
  8208. // TODO: what if invalid character replacement is disabled?
  8209. FileName = FDragTempDir + GUIConfiguration->CurrentCopyParam.ValidLocalFileName(File->FileName);
  8210. UnicodeString TransferFileName;
  8211. if (Sender == RemoteDriveView)
  8212. {
  8213. TransferFileName = UnixExcludeTrailingBackslash(File->FullFileName);
  8214. }
  8215. else
  8216. {
  8217. TransferFileName = File->FileName;
  8218. }
  8219. FDDFileList->AddObject(TransferFileName, File);
  8220. }
  8221. //---------------------------------------------------------------------------
  8222. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDDragDetect(
  8223. TObject * /*Sender*/, int /*grfKeyState*/, const TPoint & /*DetectStart*/,
  8224. const TPoint & /*Point*/, TDragDetectStatus /*DragStatus*/)
  8225. {
  8226. // sometimes we do not get DDEnd so the list is not released
  8227. SAFE_DESTROY(FDDFileList);
  8228. FDDFileList = new TStringList();
  8229. FDragTempDir = WinConfiguration->TemporaryDir();
  8230. FDDTotalSize = 0;
  8231. FDragDropSshTerminate.reset(NULL);
  8232. }
  8233. //---------------------------------------------------------------------------
  8234. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDQueryContinueDrag(
  8235. TObject * /*Sender*/, BOOL /*FEscapePressed*/, int /*grfKeyState*/,
  8236. HRESULT & Result)
  8237. {
  8238. if (Result == DRAGDROP_S_DROP)
  8239. {
  8240. try
  8241. {
  8242. GlobalDragImageList->HideDragImage();
  8243. try
  8244. {
  8245. RemoteFileControlDDTargetDrop();
  8246. }
  8247. catch(ESshTerminate & E)
  8248. {
  8249. FDragDropSshTerminate.reset(E.Clone());
  8250. }
  8251. }
  8252. catch (Exception &E)
  8253. {
  8254. // If downloading fails we need to cancel drag&drop, otherwise
  8255. // Explorer shows error
  8256. // But by the way exception probably never reach this point as
  8257. // it's catched on way.
  8258. // Fatal exceptions get here (like when opening a secondary shell extension for file duplication fails).
  8259. Result = DRAGDROP_S_CANCEL;
  8260. ShowExtendedException(Terminal, &E);
  8261. }
  8262. }
  8263. }
  8264. //---------------------------------------------------------------------------
  8265. void __fastcall TCustomScpExplorerForm::DirViewMatchMask(
  8266. TObject * /*Sender*/, UnicodeString FileName, bool Directory, __int64 Size,
  8267. TDateTime Modification, UnicodeString Masks, bool & Matches, bool AllowImplicitMatches)
  8268. {
  8269. TFileMasks::TParams MaskParams;
  8270. MaskParams.Size = Size;
  8271. MaskParams.Modification = Modification;
  8272. // this does not re-parse the mask if it is the same as the last time
  8273. FDirViewMatchMask = Masks;
  8274. bool ImplicitMatch;
  8275. Matches =
  8276. // Local and RecurseInclude parameters have no effect as there's no path.
  8277. // This should use potential MatchesFileName overload with ImplicitMatch argument.
  8278. FDirViewMatchMask.Matches(FileName, false, Directory, &MaskParams, true, ImplicitMatch) &&
  8279. (AllowImplicitMatches || !ImplicitMatch);
  8280. }
  8281. //---------------------------------------------------------------------------
  8282. void __fastcall TCustomScpExplorerForm::DirViewGetOverlay(
  8283. TObject * Sender, TListItem * Item, WORD & Indexes)
  8284. {
  8285. TCustomDirView * DirView = reinterpret_cast<TCustomDirView *>(Sender);
  8286. UnicodeString Ext;
  8287. if (dynamic_cast<TUnixDirView *>(DirView) != NULL)
  8288. {
  8289. Ext = UnixExtractFileExt(DirView->ItemFileName(Item));
  8290. }
  8291. else
  8292. {
  8293. Ext = ExtractFileExt(DirView->ItemFileName(Item));
  8294. }
  8295. if (SameText(Ext, Configuration->PartialExt))
  8296. {
  8297. Indexes |= oiPartial;
  8298. }
  8299. }
  8300. //---------------------------------------------------------------------------
  8301. bool __fastcall TCustomScpExplorerForm::CanPasteToDirViewFromClipBoard()
  8302. {
  8303. return
  8304. DirViewEnabled(osCurrent) &&
  8305. DirView(osCurrent)->CanPasteFromClipBoard();
  8306. }
  8307. //---------------------------------------------------------------------------
  8308. bool __fastcall TCustomScpExplorerForm::CanPasteFromClipBoard()
  8309. {
  8310. bool Result = false;
  8311. if (CanPasteToDirViewFromClipBoard())
  8312. {
  8313. Result = true;
  8314. }
  8315. else
  8316. {
  8317. UnicodeString ClipboardText;
  8318. if (NonEmptyTextFromClipboard(ClipboardText) &&
  8319. (ClipboardText.Pos(L"\n") == 0)) // it's already trimmed
  8320. {
  8321. if (StoredSessions->IsUrl(ClipboardText))
  8322. {
  8323. Result = true;
  8324. }
  8325. else
  8326. {
  8327. Result = DirViewEnabled(osCurrent);
  8328. }
  8329. }
  8330. }
  8331. return Result;
  8332. }
  8333. //---------------------------------------------------------------------------
  8334. void __fastcall TCustomScpExplorerForm::PasteFromClipBoard()
  8335. {
  8336. // Pasting to any local panel is handled in TScpCommanderForm::PasteFromClipBoard()
  8337. if (DoesClipboardContainOurFiles())
  8338. {
  8339. if (DebugAlwaysTrue(CanPasteToDirViewFromClipBoard()))
  8340. {
  8341. DebugAssert(!IsLocalBrowserMode());
  8342. TTerminalManager * Manager = TTerminalManager::Instance();
  8343. TTerminal * TargetTerminal = Manager->ActiveTerminal;
  8344. Manager->ActiveSession = FClipboardTerminal;
  8345. ExecuteFileOperation(foRemoteCopy, osRemote, FClipboardFileList.get(), false, TargetTerminal);
  8346. }
  8347. }
  8348. else if (CanPasteToDirViewFromClipBoard())
  8349. {
  8350. DirView(osCurrent)->PasteFromClipBoard();
  8351. }
  8352. else
  8353. {
  8354. UnicodeString ClipboardText;
  8355. if (NonEmptyTextFromClipboard(ClipboardText))
  8356. {
  8357. if (StoredSessions->IsUrl(ClipboardText))
  8358. {
  8359. NewSession(ClipboardText);
  8360. }
  8361. else
  8362. {
  8363. DirView(osCurrent)->Path = ClipboardText;
  8364. }
  8365. }
  8366. }
  8367. }
  8368. //---------------------------------------------------------------------------
  8369. void __fastcall TCustomScpExplorerForm::FileListFromClipboard()
  8370. {
  8371. // TBD
  8372. }
  8373. //---------------------------------------------------------------------------
  8374. void __fastcall TCustomScpExplorerForm::SelectAll(TOperationSide Side, TSelectMode Mode)
  8375. {
  8376. TCustomDirView * ADirView = DirView(Side);
  8377. ADirView->SelectAll(Mode);
  8378. ADirView->SetFocus();
  8379. }
  8380. //---------------------------------------------------------------------------
  8381. void __fastcall TCustomScpExplorerForm::SelectByMask(TOperationSide Side, bool Select)
  8382. {
  8383. TCustomDirView * ADirView = DirView(Side);
  8384. TFileFilter Filter;
  8385. DefaultFileFilter(Filter);
  8386. if (DoSelectMaskDialog(ADirView, Select, Filter))
  8387. {
  8388. Configuration->Usage->Inc(L"MaskSelections");
  8389. ADirView->SelectFiles(Filter, Select);
  8390. ADirView->SetFocus();
  8391. }
  8392. }
  8393. //---------------------------------------------------------------------------
  8394. void __fastcall TCustomScpExplorerForm::RestoreSelectedNames(TOperationSide Side)
  8395. {
  8396. TCustomDirView * ADirView = DirView(Side);
  8397. ADirView->RestoreSelectedNames();
  8398. ADirView->SetFocus();
  8399. }
  8400. //---------------------------------------------------------------------------
  8401. void __fastcall TCustomScpExplorerForm::SelectSameExt(bool Select)
  8402. {
  8403. TCustomDirView * CurrentDirView = DirView(osCurrent);
  8404. if (DebugAlwaysTrue(CurrentDirView->ItemFocused != NULL))
  8405. {
  8406. UnicodeString FileName = CurrentDirView->ItemFileName(CurrentDirView->ItemFocused);
  8407. UnicodeString Ext;
  8408. if (!IsSideLocalBrowser(osCurrent))
  8409. {
  8410. Ext = UnixExtractFileExt(FileName);
  8411. }
  8412. else
  8413. {
  8414. Ext = ExtractFileExt(FileName);
  8415. }
  8416. if (Ext.IsEmpty())
  8417. {
  8418. Ext = L".";
  8419. }
  8420. TFileFilter Filter;
  8421. Filter.Masks = FORMAT(L"*%s", (Ext));
  8422. Filter.Directories = false;
  8423. CurrentDirView->SelectFiles(Filter, Select);
  8424. }
  8425. }
  8426. //---------------------------------------------------------------------------
  8427. UnicodeString __fastcall TCustomScpExplorerForm::FileStatusBarText(
  8428. const TStatusFileInfo & FileInfo, TOperationSide Side)
  8429. {
  8430. UnicodeString Result;
  8431. if (!FIncrementalSearch.IsEmpty() && (Side == GetSide(osCurrent)))
  8432. {
  8433. Result = FormatIncrementalSearchStatus(FIncrementalSearch, FIncrementalSearchHaveNext);
  8434. }
  8435. else if (!IsSideLocalBrowser(Side) && ((Terminal == NULL) || Terminal->Disconnected))
  8436. {
  8437. // noop
  8438. }
  8439. else
  8440. {
  8441. Result =
  8442. FMTLOAD(FILE_INFO_FORMAT,
  8443. (FormatBytes(FileInfo.SelectedSize),
  8444. FormatBytes(FileInfo.FilesSize),
  8445. FormatNumber(FileInfo.SelectedCount),
  8446. FormatNumber(FileInfo.FilesCount)));
  8447. }
  8448. return Result;
  8449. }
  8450. //---------------------------------------------------------------------------
  8451. void __fastcall TCustomScpExplorerForm::FileStatusBarPanelClick(
  8452. TTBXStatusPanel * Panel, TOperationSide Side)
  8453. {
  8454. if (Panel->Index == 1)
  8455. {
  8456. ToggleShowHiddenFiles();
  8457. }
  8458. else if (Panel->Index == 2)
  8459. {
  8460. Filter(Side);
  8461. }
  8462. }
  8463. //---------------------------------------------------------------------------
  8464. void __fastcall TCustomScpExplorerForm::UpdateFileStatusBar(
  8465. TTBXStatusBar * StatusBar, const TStatusFileInfo & FileInfo, TOperationSide Side)
  8466. {
  8467. DebugAssert(!StatusBar->SimplePanel);
  8468. StatusBar->Panels->Items[0]->Caption = FileStatusBarText(FileInfo, Side);
  8469. UpdateFileStatusExtendedPanels(StatusBar, FileInfo);
  8470. }
  8471. //---------------------------------------------------------------------------
  8472. void __fastcall TCustomScpExplorerForm::UpdateFileStatusExtendedPanels(
  8473. TTBXStatusBar * StatusBar, const TStatusFileInfo & FileInfo)
  8474. {
  8475. DebugAssert(StatusBar->Panels->Count >= 3);
  8476. TTBXStatusPanel * HiddenFilesPanel = StatusBar->Panels->Items[1];
  8477. if (FileInfo.HiddenCount > 0)
  8478. {
  8479. HiddenFilesPanel->Caption = FMTLOAD(FILE_INFO_HIDDEN2, (FormatNumber(FileInfo.HiddenCount)));
  8480. HiddenFilesPanel->ViewPriority = 90; // <100 allows hiding panel when it does not fit
  8481. }
  8482. else
  8483. {
  8484. HiddenFilesPanel->ViewPriority = 0;
  8485. // not really necessary, just to cleanup no-longer-valid data
  8486. HiddenFilesPanel->Caption = L"";
  8487. }
  8488. TTBXStatusPanel * FilteredFilesPanel = StatusBar->Panels->Items[2];
  8489. if (FileInfo.FilteredCount > 0)
  8490. {
  8491. FilteredFilesPanel->Caption = FMTLOAD(FILE_INFO_FILTERED2, (FormatNumber(FileInfo.FilteredCount)));
  8492. FilteredFilesPanel->ViewPriority = 90; // <100 allows hiding panel when it does not fit
  8493. }
  8494. else
  8495. {
  8496. FilteredFilesPanel->ViewPriority = 0;
  8497. // not really necessary, just to cleanup no-longer-valid data
  8498. FilteredFilesPanel->Caption = L"";
  8499. }
  8500. }
  8501. //---------------------------------------------------------------------------
  8502. void __fastcall TCustomScpExplorerForm::RemoteStatusBarClick(
  8503. TObject * /*Sender*/)
  8504. {
  8505. if (DirView(osOther)->Enabled)
  8506. {
  8507. DirView(osOther)->SetFocus();
  8508. }
  8509. }
  8510. //---------------------------------------------------------------------------
  8511. void __fastcall TCustomScpExplorerForm::ToggleQueueVisibility()
  8512. {
  8513. TQueueViewConfiguration Config = WinConfiguration->QueueView;
  8514. switch (Config.Show)
  8515. {
  8516. case qvShow:
  8517. if ((FQueueStatus != NULL) && (FQueueStatus->Count > 0))
  8518. {
  8519. Config.Show = qvHide;
  8520. }
  8521. else
  8522. {
  8523. Config.Show = Config.LastHideShow;
  8524. }
  8525. break;
  8526. case qvHideWhenEmpty:
  8527. if (ComponentVisible[fcQueueView])
  8528. {
  8529. Config.Show = qvHide;
  8530. }
  8531. else
  8532. {
  8533. Config.LastHideShow = Config.Show;
  8534. Config.Show = qvShow;
  8535. }
  8536. break;
  8537. case qvHide:
  8538. Config.LastHideShow = Config.Show;
  8539. Config.Show = qvShow;
  8540. break;
  8541. }
  8542. WinConfiguration->QueueView = Config;
  8543. }
  8544. //---------------------------------------------------------------------------
  8545. UnicodeString __fastcall TCustomScpExplorerForm::PathForCaption()
  8546. {
  8547. UnicodeString Result;
  8548. if (Terminal != NULL)
  8549. {
  8550. switch (WinConfiguration->PathInCaption)
  8551. {
  8552. case picShort:
  8553. Result = ExtractShortName(Terminal->CurrentDirectory, true);
  8554. break;
  8555. case picFull:
  8556. Result = Terminal->CurrentDirectory;
  8557. break;
  8558. }
  8559. }
  8560. return Result;
  8561. }
  8562. //---------------------------------------------------------------------------
  8563. TColor __fastcall TCustomScpExplorerForm::PanelColor()
  8564. {
  8565. TColor Result = GetWindowColor(FSessionColor);
  8566. return Result;
  8567. }
  8568. //---------------------------------------------------------------------------
  8569. TColor __fastcall TCustomScpExplorerForm::DisabledPanelColor()
  8570. {
  8571. TColor Result = (WinConfiguration->UseDarkTheme() ? static_cast<TColor>(RGB(0x40, 0x40, 0x40)) : clBtnFace);
  8572. return Result;
  8573. }
  8574. //---------------------------------------------------------------------------
  8575. void __fastcall TCustomScpExplorerForm::UpdateControls()
  8576. {
  8577. if (!FSessionChanging)
  8578. {
  8579. TTerminalManager::Instance()->UpdateAppTitle();
  8580. }
  8581. // WORKAROUND: Disabling list view when it is not showing yet does not set its
  8582. // background to gray on Windows 7 (works on Windows 10).
  8583. // See also EnableControl
  8584. if (Showing)
  8585. {
  8586. if (ManagedSession != NULL)
  8587. {
  8588. // Update path when it changes
  8589. if ((SessionsPageControl->ActivePage != NULL) &&
  8590. (GetSessionTabSession(SessionsPageControl->ActivePage) == ManagedSession))
  8591. {
  8592. UpdateSessionTab(SessionsPageControl->ActivePage);
  8593. }
  8594. }
  8595. NonVisualDataModule->ReconnectSessionAction->Update();
  8596. ReconnectToolbar->Visible = NonVisualDataModule->ReconnectSessionAction->Visible;
  8597. // ReconnectSessionAction is hidden when disabled, so enabling it actualy resizes the toolbar
  8598. CenterReconnectToolbar();
  8599. bool HasTerminal = HasActiveTerminal();
  8600. RemoteDirView->Enabled = HasTerminal;
  8601. RemoteDirView->Color = HasTerminal ? PanelColor() : DisabledPanelColor();
  8602. RemoteDirView->Font->Color = GetWindowTextColor(RemoteDirView->Color);
  8603. RemoteDriveView->Enabled = RemoteDirView->Enabled;
  8604. RemoteDriveView->Color = RemoteDirView->Color;
  8605. RemoteDriveView->Font->Color = RemoteDirView->Font->Color;
  8606. QueueView3->Enabled = HasTerminal && Terminal->IsCapable[fcBackgroundTransfers];
  8607. QueueView3->Color = QueueView3->Enabled ? GetWindowColor() : DisabledPanelColor();
  8608. QueueView3->Font->Color = GetWindowTextColor(QueueView3->Color);
  8609. QueueFileList->Enabled = QueueView3->Enabled;
  8610. QueueFileList->Color = QueueView3->Color;
  8611. QueueFileList->Font->Color = QueueView3->Font->Color;
  8612. QueueLabelUpdateStatus();
  8613. RemoteDirView->DarkMode = WinConfiguration->UseDarkTheme();
  8614. RemoteDriveView->DarkMode = RemoteDirView->DarkMode;
  8615. if (FImmersiveDarkMode != WinConfiguration->UseDarkTheme())
  8616. {
  8617. UpdateDarkMode();
  8618. }
  8619. reinterpret_cast<TTBCustomItem *>(GetComponent(fcRemotePathComboBox))->Enabled = HasTerminal || IsLocalBrowserMode();
  8620. MessageDock->Visible = (MessageDock->ToolbarCount > 0);
  8621. }
  8622. }
  8623. //---------------------------------------------------------------------------
  8624. void __fastcall TCustomScpExplorerForm::DoDirViewLoaded(TCustomDirView * /*Sender*/)
  8625. {
  8626. UpdateControls();
  8627. }
  8628. //---------------------------------------------------------------------------
  8629. void __fastcall TCustomScpExplorerForm::DirViewLoaded(
  8630. TObject * Sender)
  8631. {
  8632. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  8633. DebugAssert(DirView != NULL);
  8634. DoDirViewLoaded(DirView);
  8635. TransferPresetAutoSelect();
  8636. }
  8637. //---------------------------------------------------------------------------
  8638. void __fastcall TCustomScpExplorerForm::StartUpdates()
  8639. {
  8640. TUpdatesConfiguration Updates = WinConfiguration->Updates;
  8641. // first run after installation
  8642. if (double(Updates.LastCheck) == 0)
  8643. {
  8644. AppLog(L"First run, scheduling updates check for the next run");
  8645. // make sure next time there will be an update (if enabled)
  8646. Updates.LastCheck = TDateTime(1);
  8647. WinConfiguration->Updates = Updates;
  8648. }
  8649. else
  8650. {
  8651. TDateTime Period;
  8652. if (IsUWP() && !IsOfficialPackage())
  8653. {
  8654. Period = DefaultUpdatesPeriod;
  8655. AppLog(L"Thirdparty UWP package, using default updates check period");
  8656. }
  8657. else
  8658. {
  8659. Period = Updates.Period;
  8660. }
  8661. AppLogFmt(L"Updates check period: %.2f", (double(Period)));
  8662. if (double(Period) > 0)
  8663. {
  8664. TDateTime Interval = Now() - Updates.LastCheck;
  8665. AppLogFmt(L"Interval since the last updates check: %.2f", (double(Interval)));
  8666. if (Interval >= Period)
  8667. {
  8668. StartUpdateThread(UpdatesChecked);
  8669. }
  8670. }
  8671. }
  8672. }
  8673. //---------------------------------------------------------------------------
  8674. void __fastcall TCustomScpExplorerForm::UpdatesChecked()
  8675. {
  8676. AppLog(L"Updates check done");
  8677. UnicodeString Message;
  8678. bool New;
  8679. TQueryType Type;
  8680. GetUpdatesMessage(Message, New, Type, false);
  8681. if (!Message.IsEmpty())
  8682. {
  8683. AppLogFmt(L"Updates check message: %s", (Message));
  8684. if (New)
  8685. {
  8686. AppLog(L"New version detected");
  8687. Message = FMTLOAD(NEW_VERSION_CLICK, (Message));
  8688. }
  8689. if (!New && (Type != qtWarning))
  8690. {
  8691. AppLog(L"Posting non-critical updates note");
  8692. PostNote(UnformatMessage(Message), 0, UpdatesNoteClicked, NULL);
  8693. }
  8694. else
  8695. {
  8696. AppLog(L"Posting new version or critical message notification");
  8697. Notify(NULL, Message, Type, true, UpdatesNoteClicked);
  8698. }
  8699. if (New)
  8700. {
  8701. Configuration->Usage->Inc(L"UpdateNotifications");
  8702. }
  8703. }
  8704. }
  8705. //---------------------------------------------------------------------------
  8706. void __fastcall TCustomScpExplorerForm::UpdatesNoteClicked(TObject * /*Sender*/)
  8707. {
  8708. RestoreApp();
  8709. bool CanDisplay = !NonVisualDataModule->Busy;
  8710. if (!CanDisplay && (Screen->ActiveForm != NULL))
  8711. {
  8712. CanDisplay = (Screen->ActiveForm->Perform(WM_CAN_DISPLAY_UPDATES, 0, 0) != 0);
  8713. }
  8714. if (CanDisplay)
  8715. {
  8716. Configuration->Usage->Inc(L"UpdateNotificationsClicked");
  8717. // Probably not needed as CheckForUpdates is blocking since we started using neon for HTTP implementation.
  8718. NonVisualDataModule->StartBusy();
  8719. try
  8720. {
  8721. CheckForUpdates(true);
  8722. }
  8723. __finally
  8724. {
  8725. NonVisualDataModule->EndBusy();
  8726. }
  8727. }
  8728. else
  8729. {
  8730. Configuration->Usage->Inc(L"UpdateNotificationsBusy");
  8731. }
  8732. }
  8733. //---------------------------------------------------------------------------
  8734. void __fastcall TCustomScpExplorerForm::GetTransferPresetAutoSelectData(
  8735. TCopyParamRuleData & Data)
  8736. {
  8737. DebugAssert(Terminal != NULL);
  8738. Data.HostName = Terminal->SessionData->HostNameExpanded;
  8739. Data.UserName = Terminal->SessionData->UserNameExpanded;
  8740. Data.RemoteDirectory = RemoteDirView->PathName;
  8741. }
  8742. //---------------------------------------------------------------------------
  8743. void __fastcall TCustomScpExplorerForm::TransferPresetAutoSelect()
  8744. {
  8745. // Terminal can be null when we are changing local directory implicitly,
  8746. // because it has been deleted, while no session is connected
  8747. // (Login dialog is open)
  8748. if (FAllowTransferPresetAutoSelect && (Terminal != NULL))
  8749. {
  8750. DebugAssert(!IsLocalBrowserMode());
  8751. TCopyParamRuleData Data;
  8752. GetTransferPresetAutoSelectData(Data);
  8753. int CopyParamIndex = GUIConfiguration->CopyParamList->Find(Data);
  8754. UnicodeString CopyParamCurrent = GUIConfiguration->CopyParamCurrent;
  8755. if (CopyParamIndex < 0)
  8756. {
  8757. // there is no preset that matches autoselection
  8758. // set preset that we consider "default"
  8759. FCopyParamAutoSelected = L""; // forget last autoselected preset
  8760. GUIConfiguration->CopyParamCurrent = FCopyParamDefault;
  8761. }
  8762. else
  8763. {
  8764. // there is preset matching autoselection
  8765. UnicodeString CopyParamName = GUIConfiguration->CopyParamList->Names[CopyParamIndex];
  8766. if (CopyParamName == FCopyParamAutoSelected)
  8767. {
  8768. // autoselected the same preset as the last time
  8769. // make no change (i.e. preserve custom user preset, if any)
  8770. }
  8771. else
  8772. {
  8773. // autoselected the different preset then the last time (or there
  8774. // was default preset set)
  8775. FCopyParamAutoSelected = CopyParamName; // remember autoselection
  8776. GUIConfiguration->CopyParamCurrent = CopyParamName;
  8777. }
  8778. }
  8779. if (GUIConfiguration->CopyParamCurrent != CopyParamCurrent)
  8780. {
  8781. if (CopyParamIndex >= 0)
  8782. {
  8783. Configuration->Usage->Inc(L"CopyParamAutoSelects");
  8784. }
  8785. TTransferPresetNoteData * Data = new TTransferPresetNoteData;
  8786. try
  8787. {
  8788. int Fmt =
  8789. (CopyParamIndex < 0) ?
  8790. (GUIConfiguration->CopyParamIndex < 0 ? COPY_PARAM_DEFAULT_NORM : COPY_PARAM_DEFAULT_CUSTOM) :
  8791. COPY_PARAM_AUTOSELECTED;
  8792. UnicodeString Message = FMTLOAD(Fmt, (StripHotkey(GUIConfiguration->CopyParamCurrent)));
  8793. Data->Message = MainInstructions(Message);
  8794. int CopyParamAttrs = Terminal->UsableCopyParamAttrs(0).General;
  8795. UnicodeString Info = GUIConfiguration->CurrentCopyParam.GetInfoStr(L"\n",
  8796. CopyParamAttrs);
  8797. if (CopyParamIndex >= 0)
  8798. {
  8799. DebugAssert(GUIConfiguration->CopyParamList->Rules[CopyParamIndex] != NULL);
  8800. Info = FORMAT(L"%s\n \n%s", (Info,
  8801. FMTLOAD(COPY_PARAM_RULE,
  8802. (GUIConfiguration->CopyParamList->Rules[CopyParamIndex]->GetInfoStr(L"\n")))));
  8803. }
  8804. Data->Message += L"\n\n" + Info;
  8805. if (WinConfiguration->CopyParamAutoSelectNotice)
  8806. {
  8807. TransferPresetNoteMessage(Data, true);
  8808. }
  8809. else
  8810. {
  8811. PostNote(Message, 0, TransferPresetNoteClicked, Data);
  8812. Data = NULL; // ownership passed
  8813. }
  8814. }
  8815. __finally
  8816. {
  8817. delete Data;
  8818. }
  8819. }
  8820. }
  8821. }
  8822. //---------------------------------------------------------------------------
  8823. void __fastcall TCustomScpExplorerForm::TransferPresetNoteMessage(
  8824. TTransferPresetNoteData * NoteData, bool AllowNeverAskAgain)
  8825. {
  8826. DebugAssert(NoteData != NULL);
  8827. DebugAssert(!IsLocalBrowserMode());
  8828. TMessageParams Params(AllowNeverAskAgain ? mpNeverAskAgainCheck : 0);
  8829. TQueryButtonAlias Aliases[1];
  8830. Aliases[0].Button = qaIgnore; // "ignore" is after "ok"
  8831. Aliases[0].Alias = LoadStr(CONFIGURE_BUTTON);
  8832. Params.Aliases = Aliases;
  8833. Params.AliasesCount = LENOF(Aliases);
  8834. unsigned int Result =
  8835. MoreMessageDialog(NoteData->Message, NULL, qtInformation,
  8836. qaOK | qaIgnore, HELP_COPY_PARAM_AUTOSELECTED, &Params);
  8837. switch (Result)
  8838. {
  8839. case qaNeverAskAgain:
  8840. DebugAssert(AllowNeverAskAgain);
  8841. WinConfiguration->CopyParamAutoSelectNotice = false;
  8842. break;
  8843. case qaIgnore:
  8844. PreferencesDialog(pmPresets);
  8845. break;
  8846. }
  8847. }
  8848. //---------------------------------------------------------------------------
  8849. void __fastcall TCustomScpExplorerForm::TransferPresetNoteClicked(TObject * Sender)
  8850. {
  8851. // as of now useless, as this is used for notes only, never for balloons, ...
  8852. RestoreApp();
  8853. // .. and we should never be busy here
  8854. if (DebugAlwaysTrue(!NonVisualDataModule->Busy))
  8855. {
  8856. TransferPresetNoteMessage(DebugNotNull(dynamic_cast<TTransferPresetNoteData *>(Sender)), false);
  8857. }
  8858. }
  8859. //---------------------------------------------------------------------------
  8860. void __fastcall TCustomScpExplorerForm::PreferencesDialog(
  8861. TPreferencesMode APreferencesMode)
  8862. {
  8863. std::unique_ptr<TPreferencesDialogData> PreferencesData;
  8864. TCopyParamRuleData Data;
  8865. if (Terminal != NULL)
  8866. {
  8867. PreferencesData.reset(new TPreferencesDialogData());
  8868. GetTransferPresetAutoSelectData(Data);
  8869. PreferencesData->CopyParamRuleData = &Data;
  8870. }
  8871. DoPreferencesDialog(APreferencesMode, PreferencesData.get());
  8872. }
  8873. //---------------------------------------------------------------------------
  8874. void __fastcall TCustomScpExplorerForm::AdHocCustomCommandValidate(
  8875. const TCustomCommandType & Command)
  8876. {
  8877. if (CustomCommandState(Command, FEditingFocusedAdHocCommand, ccltAll) <= 0)
  8878. {
  8879. throw Exception(FMTLOAD(CUSTOM_COMMAND_IMPOSSIBLE2, (Command.Command)));
  8880. }
  8881. }
  8882. //---------------------------------------------------------------------------
  8883. void __fastcall TCustomScpExplorerForm::AdHocCustomCommand(bool OnFocused)
  8884. {
  8885. bool RemoteAllowed = CustomCommandRemoteAllowed();
  8886. TCustomCommandType Command;
  8887. // make sure we use local custom command when remote are not supported
  8888. if (RemoteAllowed || FLAGSET(FLastCustomCommand.Params, ccLocal))
  8889. {
  8890. Command = FLastCustomCommand;
  8891. }
  8892. else
  8893. {
  8894. Command.Params = Command.Params | ccLocal;
  8895. }
  8896. Command.Name = LoadStr(CUSTOM_COMMAND_AD_HOC_NAME);
  8897. FEditingFocusedAdHocCommand = OnFocused;
  8898. bool LocalSide = IsSideLocalBrowser(FCurrentSide);
  8899. int Options =
  8900. FLAGMASK((!RemoteAllowed || LocalSide), ccoDisableRemote) |
  8901. FLAGMASK(LocalSide, ccoDisableRemoteFiles);
  8902. if (DoCustomCommandDialog(Command, WinConfiguration->CustomCommandList,
  8903. ccmAdHoc, Options, AdHocCustomCommandValidate, NULL))
  8904. {
  8905. FLastCustomCommand = Command;
  8906. UpdateCustomCommandsToolbar();
  8907. ExecuteFileOperation(foCustomCommand, osRemote, OnFocused, false, &Command);
  8908. }
  8909. }
  8910. //---------------------------------------------------------------------------
  8911. void __fastcall TCustomScpExplorerForm::LastCustomCommand(bool OnFocused)
  8912. {
  8913. DebugAssert(!FLastCustomCommand.Command.IsEmpty());
  8914. int State = CustomCommandState(FLastCustomCommand, OnFocused, ccltAll);
  8915. DebugAssert(State > 0);
  8916. if (State <= 0)
  8917. {
  8918. throw Exception(FMTLOAD(CUSTOM_COMMAND_IMPOSSIBLE2, (FLastCustomCommand.Command)));
  8919. }
  8920. ExecuteFileOperation(foCustomCommand, osRemote, OnFocused, false, &FLastCustomCommand);
  8921. }
  8922. //---------------------------------------------------------------------------
  8923. bool __fastcall TCustomScpExplorerForm::GetLastCustomCommand(bool OnFocused,
  8924. TCustomCommandType & Command, int & State)
  8925. {
  8926. bool Result = !FLastCustomCommand.Command.IsEmpty();
  8927. if (Result)
  8928. {
  8929. Command = FLastCustomCommand;
  8930. State = CustomCommandState(FLastCustomCommand, OnFocused, ccltAll);
  8931. }
  8932. return Result;
  8933. }
  8934. //---------------------------------------------------------------------------
  8935. void __fastcall TCustomScpExplorerForm::BeforeAction()
  8936. {
  8937. ResetIncrementalSearch();
  8938. }
  8939. //---------------------------------------------------------------------------
  8940. void __fastcall TCustomScpExplorerForm::PostComponentHide(Byte Component)
  8941. {
  8942. DebugAssert(ComponentVisible[Component]);
  8943. PostMessage(Handle, WM_COMPONENT_HIDE, Component, 0);
  8944. }
  8945. //---------------------------------------------------------------------------
  8946. void __fastcall TCustomScpExplorerForm::QueueSplitterDblClick(TObject * /*Sender*/)
  8947. {
  8948. // when queue panel is resized here directly, the status bar is stretched
  8949. // over whole space the panel occupied
  8950. PostComponentHide(fcQueueView);
  8951. }
  8952. //---------------------------------------------------------------------------
  8953. void __fastcall TCustomScpExplorerForm::QueueFileListSplitterDblClick(TObject *)
  8954. {
  8955. ComponentVisible[fcQueueFileList] = false;
  8956. }
  8957. //---------------------------------------------------------------------------
  8958. void __fastcall TCustomScpExplorerForm::ThemeChanged()
  8959. {
  8960. // We hoped this will refresh scrollbar colors, but it does not have any effect here.
  8961. RefreshColorMode();
  8962. ResetSysDarkTheme();
  8963. ConfigurationChanged();
  8964. ConfigureInterface();
  8965. // Should be called for all controls
  8966. RemoteDirView->Perform(WM_THEMECHANGED, 0, 0);
  8967. }
  8968. //---------------------------------------------------------------------------
  8969. void __fastcall TCustomScpExplorerForm::WMSettingChange(TMessage & Message)
  8970. {
  8971. // Do not handle, when shutting down anyway (maybe also when not setup completely yet?)
  8972. if (!FInvalid &&
  8973. (Message.LParam != 0) &&
  8974. (wcscmp(reinterpret_cast<LPCWCH>(Message.LParam), L"ImmersiveColorSet") == 0))
  8975. {
  8976. ThemeChanged();
  8977. }
  8978. TForm::Dispatch(&Message);
  8979. }
  8980. //---------------------------------------------------------------------------
  8981. void __fastcall TCustomScpExplorerForm::Dispatch(void * Message)
  8982. {
  8983. TMessage * M = static_cast<TMessage*>(Message);
  8984. switch (M->Msg)
  8985. {
  8986. case CM_DIALOGCHAR:
  8987. CMDialogChar(*M);
  8988. break;
  8989. case WM_APPCOMMAND:
  8990. WMAppCommand(*M);
  8991. break;
  8992. case WM_SYSCOMMAND:
  8993. WMSysCommand(*M);
  8994. break;
  8995. case WM_QUERYENDSESSION:
  8996. WMQueryEndSession(*M);
  8997. break;
  8998. case WM_ENDSESSION:
  8999. WMEndSession(*reinterpret_cast<TWMEndSession *>(M));
  9000. break;
  9001. #ifdef _DEBUG
  9002. case WM_WINDOWPOSCHANGED:
  9003. WMWindowPosChanged(*reinterpret_cast<TWMWindowPosMsg *>(M));
  9004. break;
  9005. case CM_CUSTOMSTYLECHANGED:
  9006. TForm::Dispatch(Message);
  9007. break;
  9008. #endif
  9009. case WM_COMPONENT_HIDE:
  9010. {
  9011. Byte Component = static_cast<Byte>(M->WParam);
  9012. // sanity check
  9013. if (ComponentVisible[Component])
  9014. {
  9015. // special treatment
  9016. if (Component == fcQueueView)
  9017. {
  9018. ToggleQueueVisibility();
  9019. DebugAssert(!ComponentVisible[fcQueueView]);
  9020. }
  9021. else
  9022. {
  9023. ComponentVisible[Component] = false;
  9024. }
  9025. }
  9026. }
  9027. break;
  9028. case WM_COPYDATA:
  9029. WMCopyData(*M);
  9030. break;
  9031. case WM_MANAGES_CAPTION:
  9032. // caption managed in TTerminalManager::UpdateAppTitle()
  9033. M->Result = 1;
  9034. break;
  9035. case WM_WANTS_MOUSEWHEEL:
  9036. M->Result = 1;
  9037. break;
  9038. case CM_SHOWINGCHANGED:
  9039. CMShowingChanged(*M);
  9040. break;
  9041. case WM_CLOSE:
  9042. WMClose(*M);
  9043. break;
  9044. case WM_DPICHANGED:
  9045. WMDpiChanged(*M);
  9046. break;
  9047. case CM_DPICHANGED:
  9048. CMDpiChanged(*M);
  9049. break;
  9050. case WM_SETTINGCHANGE:
  9051. WMSettingChange(*M);
  9052. break;
  9053. case CM_DIALOGKEY:
  9054. CMDialogKey(*reinterpret_cast<TWMKeyDown *>(M));
  9055. break;
  9056. default:
  9057. TForm::Dispatch(Message);
  9058. break;
  9059. }
  9060. }
  9061. //---------------------------------------------------------------------------
  9062. void __fastcall TCustomScpExplorerForm::UpdateImages()
  9063. {
  9064. // noop
  9065. }
  9066. //---------------------------------------------------------------------------
  9067. void __fastcall TCustomScpExplorerForm::CMDpiChanged(TMessage & Message)
  9068. {
  9069. TForm::Dispatch(&Message);
  9070. FSessionColors->Clear();
  9071. AddFixedSessionImages();
  9072. RegenerateSessionColorsImageList(FSessionColors, FSessionColorMaskImageIndex);
  9073. UpdateImages();
  9074. }
  9075. //---------------------------------------------------------------------------
  9076. void __fastcall TCustomScpExplorerForm::WMDpiChanged(TMessage & Message)
  9077. {
  9078. TForm::Dispatch(&Message);
  9079. Configuration->Usage->Inc(L"PixelsPerInchChanged");
  9080. UpdatePixelsPerInchMainWindowCounter();
  9081. }
  9082. //---------------------------------------------------------------------------
  9083. void __fastcall TCustomScpExplorerForm::WMClose(TMessage & Message)
  9084. {
  9085. // Cannot close window while we are busy.
  9086. // We cannot test this in FormCloseQuery as that is called also from
  9087. // Close(), which is called by CloseApplicationAction. So we can be busy
  9088. // there already even, when it is legitimate to close the application.
  9089. // Possibly a better place to handle this would be WMSysCommand.
  9090. if (NonVisualDataModule->Busy)
  9091. {
  9092. Message.Result = 1;
  9093. }
  9094. else
  9095. {
  9096. TForm::Dispatch(&Message);
  9097. }
  9098. }
  9099. //---------------------------------------------------------------------------
  9100. void __fastcall TCustomScpExplorerForm::CMShowingChanged(TMessage & Message)
  9101. {
  9102. if (!FShowingChanged)
  9103. {
  9104. AddStartupSequence(L"D");
  9105. FShowingChanged = true;
  9106. }
  9107. // Ignoring implicit local-local browser of Commander, opened from DoShow
  9108. bool NoSession = (ManagedSession == NULL);
  9109. // This calls TCustomScpExplorerForm::DoShow
  9110. TForm::Dispatch(&Message);
  9111. // Now the window is visible (TForm::Dispatch is what usually takes long, like when loading a "local" network directory)
  9112. if (Showing && !FStarted)
  9113. {
  9114. FStarted = true;
  9115. InterfaceStarted();
  9116. }
  9117. if (Showing && NoSession)
  9118. {
  9119. // When we are starting minimized (i.e. from an installer),
  9120. // postpone showing Login dialog until we get restored.
  9121. // Otherwise the Login dialog (and Authentication window) show restored
  9122. // over invisible (minimized) main window.
  9123. if (WindowState == wsMinimized)
  9124. {
  9125. FNeedSession = true;
  9126. }
  9127. else
  9128. {
  9129. // This happens before application ever goes idle, so the toolbars would
  9130. // stay enabled (initial state) until the Login dialog is dismissed.
  9131. UpdateActions();
  9132. NonVisualDataModule->StartBusy();
  9133. try
  9134. {
  9135. // Need to process WM_ACTIVATEAPP before showing the Login dialog,
  9136. // otherwise the dialog does not receive focus.
  9137. // With Commander interface the ProcessMessages is called already
  9138. // by TDriveView, but with Explorer interface, we need to call it explicily
  9139. Application->ProcessMessages();
  9140. // do not reload sessions, they have been loaded just now (optimization)
  9141. NeedSession(true);
  9142. }
  9143. __finally
  9144. {
  9145. NonVisualDataModule->EndBusy();
  9146. }
  9147. }
  9148. }
  9149. }
  9150. //---------------------------------------------------------------------------
  9151. void __fastcall TCustomScpExplorerForm::CenterReconnectToolbar()
  9152. {
  9153. ReconnectToolbar->Left = (ReconnectToolbar->Parent->ClientWidth - ReconnectToolbar->Width) / 2;
  9154. ReconnectToolbar->Top = (ReconnectToolbar->Parent->ClientHeight - ReconnectToolbar->Height) / 2;
  9155. }
  9156. //---------------------------------------------------------------------------
  9157. void __fastcall TCustomScpExplorerForm::FormConstrainedResize(
  9158. TObject * /*Sender*/, int & MinWidth, int & MinHeight, int & MaxWidth,
  9159. int & MaxHeight)
  9160. {
  9161. // workaround for bug in TWinControl.CalcConstraints
  9162. // Check for empty rect (restore from iconic state) is done there only after
  9163. // call to AdjustClientRect, which enlarges the rect (for forms).
  9164. TRect R = GetClientRect();
  9165. // when restoring from iconic state, place no restrictions
  9166. if (IsRectEmpty(R))
  9167. {
  9168. MinWidth = 0;
  9169. MinHeight = 0;
  9170. MaxWidth = 32000;
  9171. MaxHeight = 32000;
  9172. }
  9173. }
  9174. //---------------------------------------------------------------------------
  9175. void __fastcall TCustomScpExplorerForm::GetSpaceAvailable(const UnicodeString Path,
  9176. TSpaceAvailable & ASpaceAvailable, bool & Close)
  9177. {
  9178. // terminal can be already closed (e.g. dropped connection)
  9179. if ((Terminal != NULL) && Terminal->IsCapable[fcCheckingSpaceAvailable])
  9180. {
  9181. Configuration->Usage->Inc(L"SpaceAvailableChecks");
  9182. try
  9183. {
  9184. Terminal->SpaceAvailable(Path, ASpaceAvailable);
  9185. }
  9186. catch(...)
  9187. {
  9188. if (!Terminal->Active)
  9189. {
  9190. Close = true;
  9191. }
  9192. throw;
  9193. }
  9194. }
  9195. }
  9196. //---------------------------------------------------------------------------
  9197. void __fastcall TCustomScpExplorerForm::FileSystemInfo()
  9198. {
  9199. const TSessionInfo & SessionInfo = Terminal->GetSessionInfo();
  9200. const TFileSystemInfo & FileSystemInfo = Terminal->GetFileSystemInfo(true);
  9201. TGetSpaceAvailable OnGetSpaceAvailable = NULL;
  9202. if (Terminal->IsCapable[fcCheckingSpaceAvailable])
  9203. {
  9204. OnGetSpaceAvailable = GetSpaceAvailable;
  9205. }
  9206. DoFileSystemInfoDialog(SessionInfo, FileSystemInfo, Terminal->CurrentDirectory,
  9207. OnGetSpaceAvailable);
  9208. }
  9209. //---------------------------------------------------------------------------
  9210. TSessionData * __fastcall TCustomScpExplorerForm::SessionDataForCode()
  9211. {
  9212. std::unique_ptr<TSessionData> Data(CloneCurrentSessionData());
  9213. Terminal->FillSessionDataForCode(Data.get());
  9214. return Data.release();
  9215. }
  9216. //---------------------------------------------------------------------------
  9217. void __fastcall TCustomScpExplorerForm::GenerateUrl(TStrings * Paths)
  9218. {
  9219. std::unique_ptr<TSessionData> Data(SessionDataForCode());
  9220. DoGenerateUrlDialog(Data.get(), Paths);
  9221. }
  9222. //---------------------------------------------------------------------------
  9223. void __fastcall TCustomScpExplorerForm::SessionGenerateUrl()
  9224. {
  9225. GenerateUrl(NULL);
  9226. }
  9227. //---------------------------------------------------------------------------
  9228. void __fastcall TCustomScpExplorerForm::FileGenerateUrl()
  9229. {
  9230. std::unique_ptr<TStrings> Paths(new TStringList());
  9231. DirView(osCurrent)->CreateFileList(false, true, Paths.get());
  9232. GenerateUrl(Paths.get());
  9233. }
  9234. //---------------------------------------------------------------------------
  9235. void __fastcall TCustomScpExplorerForm::UpdateSessionColor(TColor value)
  9236. {
  9237. FSessionColor = value;
  9238. TColor C = (value != 0 ? value : Vcl::Graphics::clNone);
  9239. TTBXColorItem * ColorItem = dynamic_cast<TTBXColorItem *>(
  9240. static_cast<TObject *>(GetComponent(fcColorMenu)));
  9241. DebugAssert(ColorItem != NULL);
  9242. ColorItem->Color = C;
  9243. NonVisualDataModule->ColorMenuItem->Color = C;
  9244. UpdateControls();
  9245. }
  9246. //---------------------------------------------------------------------------
  9247. void __fastcall TCustomScpExplorerForm::SetSessionColor(TColor value)
  9248. {
  9249. if (value != FSessionColor)
  9250. {
  9251. UpdateSessionColor(value);
  9252. // Is null when called from LastTerminalClosed
  9253. if (Terminal != NULL)
  9254. {
  9255. SessionsPageControl->ActivePage->ImageIndex = AddSessionColor(value);
  9256. }
  9257. }
  9258. }
  9259. //---------------------------------------------------------------------------
  9260. bool __fastcall TCustomScpExplorerForm::CancelNote(bool Force)
  9261. {
  9262. bool Result = FNoteTimer->Enabled;
  9263. if (Result)
  9264. {
  9265. // cannot cancel note too early
  9266. bool NotEarly =
  9267. (Now() - FNoteShown >
  9268. EncodeTimeVerbose(0, 0, (unsigned short)(WinConfiguration->NotificationsStickTime), 0));
  9269. if (Force || NotEarly)
  9270. {
  9271. FNoteTimer->Enabled = false;
  9272. FNote = L"";
  9273. SAFE_DESTROY(FNoteData);
  9274. FOnNoteClick = NULL;
  9275. FNoteHints = FNotes->Text;
  9276. FNoteHints.Delete(FNoteHints.Length() - 1, 2);
  9277. UpdateStatusBar();
  9278. }
  9279. }
  9280. return Result;
  9281. }
  9282. //---------------------------------------------------------------------------
  9283. void __fastcall TCustomScpExplorerForm::NoteTimer(TObject * /*Sender*/)
  9284. {
  9285. DebugAssert(FNoteTimer->Enabled);
  9286. CancelNote(true);
  9287. }
  9288. //---------------------------------------------------------------------------
  9289. void __fastcall TCustomScpExplorerForm::UpdateNoteHints()
  9290. {
  9291. FNoteHints = FNotes->Text.TrimRight();
  9292. }
  9293. //---------------------------------------------------------------------------
  9294. void __fastcall TCustomScpExplorerForm::AddNote(UnicodeString Note, bool UpdateNow)
  9295. {
  9296. int P = Note.Pos(L"\n");
  9297. if (P > 0)
  9298. {
  9299. Note.SetLength(P - 1);
  9300. }
  9301. FNotes->Add(FORMAT(L"[%s] %s",
  9302. (FormatDateTime(Configuration->TimeFormat, Now()), Note)));
  9303. while (FNotes->Count > 10)
  9304. {
  9305. FNotes->Delete(0);
  9306. }
  9307. if (UpdateNow)
  9308. {
  9309. UpdateNoteHints();
  9310. UpdateStatusBar();
  9311. }
  9312. }
  9313. //---------------------------------------------------------------------------
  9314. void __fastcall TCustomScpExplorerForm::PostNote(UnicodeString Note,
  9315. unsigned int Seconds, TNotifyEvent OnNoteClick, TObject * NoteData)
  9316. {
  9317. int P = Note.Pos(L"\n");
  9318. if (P > 0)
  9319. {
  9320. Note.SetLength(P - 1);
  9321. }
  9322. UpdateNoteHints();
  9323. FNote = Note;
  9324. SAFE_DESTROY(FNoteData);
  9325. FNoteData = NoteData;
  9326. FOnNoteClick = OnNoteClick;
  9327. AddNote(Note, false);
  9328. UpdateStatusBar();
  9329. FNoteShown = Now();
  9330. FNoteTimer->Enabled = false;
  9331. if (Seconds == 0)
  9332. {
  9333. Seconds = WinConfiguration->NotificationsTimeout;
  9334. }
  9335. FNoteTimer->Interval = Seconds * MSecsPerSec;
  9336. FNoteTimer->Enabled = true;
  9337. }
  9338. //---------------------------------------------------------------------------
  9339. void __fastcall TCustomScpExplorerForm::ReadDirectoryCancelled()
  9340. {
  9341. PostNote(LoadStr(DIRECTORY_READING_CANCELLED), 0, NULL, NULL);
  9342. }
  9343. //---------------------------------------------------------------------------
  9344. void __fastcall TCustomScpExplorerForm::SynchronizeBrowsingChanged()
  9345. {
  9346. if (NonVisualDataModule->SynchronizeBrowsingAction2->Checked)
  9347. {
  9348. Configuration->Usage->Inc(L"SynchronizeBrowsingEnabled");
  9349. }
  9350. PostNote(FORMAT(LoadStrPart(SYNC_DIR_BROWSE_TOGGLE, 1),
  9351. (LoadStrPart(SYNC_DIR_BROWSE_TOGGLE,
  9352. (NonVisualDataModule->SynchronizeBrowsingAction2->Checked ? 2 : 3)))),
  9353. 0, NULL, NULL);
  9354. }
  9355. //---------------------------------------------------------------------------
  9356. void __fastcall TCustomScpExplorerForm::ToggleShowHiddenFiles()
  9357. {
  9358. bool NewShowHiddenFiles = !WinConfiguration->ShowHiddenFiles;
  9359. UnicodeString Note =
  9360. FORMAT(LoadStrPart(SHOW_HIDDEN_FILES_TOGGLE, 1), (LoadStrPart(SHOW_HIDDEN_FILES_TOGGLE, (NewShowHiddenFiles ? 2 : 3))));
  9361. PostNote(Note, 0, NULL, NULL);
  9362. GetComponent(fcStatusBar)->Repaint(); // toggling ShowHiddenFiles takes time, force repaint beforehand
  9363. WinConfiguration->ShowHiddenFiles = NewShowHiddenFiles;
  9364. }
  9365. //---------------------------------------------------------------------------
  9366. void __fastcall TCustomScpExplorerForm::SetFormatSizeBytes(TFormatBytesStyle Style)
  9367. {
  9368. WinConfiguration->FormatSizeBytes = Style;
  9369. }
  9370. //---------------------------------------------------------------------------
  9371. void __fastcall TCustomScpExplorerForm::ToggleAutoReadDirectoryAfterOp()
  9372. {
  9373. Configuration->AutoReadDirectoryAfterOp = !Configuration->AutoReadDirectoryAfterOp;
  9374. PostNote(FORMAT(LoadStrPart(AUTO_READ_DIRECTORY_TOGGLE, 1),
  9375. (LoadStrPart(AUTO_READ_DIRECTORY_TOGGLE,
  9376. (Configuration->AutoReadDirectoryAfterOp ? 2 : 3)))), 0, NULL, NULL);
  9377. }
  9378. //---------------------------------------------------------------------------
  9379. void __fastcall TCustomScpExplorerForm::StatusBarPanelDblClick(
  9380. TTBXCustomStatusBar * Sender, TTBXStatusPanel * Panel)
  9381. {
  9382. if (Panel->Index == 0)
  9383. {
  9384. if (FOnNoteClick != NULL)
  9385. {
  9386. // prevent the user data from being freed by possible call
  9387. // to CancelNote or PostNote during call to OnNoteClick
  9388. std::unique_ptr<TObject> NoteData(FNoteData);
  9389. TNotifyEvent OnNoteClick = FOnNoteClick;
  9390. FNoteData = NULL;
  9391. // need to cancel the note as we are going to delete its user data
  9392. CancelNote(true);
  9393. OnNoteClick(NoteData.get());
  9394. }
  9395. }
  9396. if (HasActiveTerminal() &&
  9397. (Panel->Index >= Sender->Panels->Count - SessionPanelCount))
  9398. {
  9399. FileSystemInfo();
  9400. }
  9401. }
  9402. //---------------------------------------------------------------------------
  9403. void __fastcall TCustomScpExplorerForm::LockWindow(bool Force)
  9404. {
  9405. // workaround:
  9406. // 1) for unknown reason, disabling window, while minimized,
  9407. // prevents it from restoring, even if it was enabled again meanwhile
  9408. // 2) when disabling the main window, while another has focus
  9409. // minimize is no longer possible
  9410. // ("keep up to date" dialog - though does not seem to be the case with "find file" -
  9411. // so it has probably something to do with the semi-modalness of the "keep up to date" dialog)
  9412. // Shouldn't we use IsApplicationMinimized() here?
  9413. if ((FLockSuspendLevel == 0) && !IsIconic(Application->Handle) && (Force || (Screen->ActiveForm == this)))
  9414. {
  9415. Enabled = false;
  9416. }
  9417. FLockLevel++;
  9418. }
  9419. //---------------------------------------------------------------------------
  9420. void __fastcall TCustomScpExplorerForm::UnlockWindow()
  9421. {
  9422. DebugAssert(FLockLevel > 0);
  9423. FLockLevel--;
  9424. if (FLockLevel == 0)
  9425. {
  9426. DebugAssert(FLockSuspendLevel == 0);
  9427. Enabled = true;
  9428. }
  9429. }
  9430. //---------------------------------------------------------------------------
  9431. void __fastcall TCustomScpExplorerForm::SuspendWindowLock()
  9432. {
  9433. // We need to make sure that window is enabled when the last modal window closes
  9434. // otherwise focus is not restored correctly.
  9435. // So we re-enable the window when modal window opens and
  9436. // disable it back after is closes
  9437. if (FLockLevel > 0)
  9438. {
  9439. // while we have nesting counter, we know that we never be called
  9440. // recursivelly as Application->OnModalBegin is called only
  9441. // for the top-level modal window
  9442. if (DebugAlwaysTrue(FLockSuspendLevel == 0))
  9443. {
  9444. // won't be disabled when conditions in LockWindow() were not satisfied
  9445. FDisabledOnLockSuspend = !Enabled;
  9446. // When minimized to tray (or actually when set to SW_HIDE),
  9447. // setting Enabled makes the window focusable even when there's
  9448. // modal window over it
  9449. if (!FTrayIcon->Visible)
  9450. {
  9451. Enabled = true;
  9452. }
  9453. }
  9454. FLockSuspendLevel++;
  9455. }
  9456. }
  9457. //---------------------------------------------------------------------------
  9458. void __fastcall TCustomScpExplorerForm::ResumeWindowLock()
  9459. {
  9460. if (FLockSuspendLevel > 0)
  9461. {
  9462. DebugAssert(FLockLevel > 0);
  9463. FLockSuspendLevel--;
  9464. // see comment in SuspendWindowLock
  9465. if (DebugAlwaysTrue(FLockSuspendLevel == 0))
  9466. {
  9467. // Note that window can be enabled here, when we were minized to tray when
  9468. // was SuspendWindowLock() called.
  9469. // We should possibly do the same check as in LockWindow(),
  9470. // if it is ever possible that the consitions change between
  9471. // SuspendWindowLock() and ResumeWindowLock()
  9472. Enabled = !FDisabledOnLockSuspend;
  9473. }
  9474. }
  9475. }
  9476. //---------------------------------------------------------------------------
  9477. void __fastcall TCustomScpExplorerForm::UpdateRemotePathComboBox(bool TextOnly)
  9478. {
  9479. TTBXComboBoxItem * RemotePathComboBox =
  9480. reinterpret_cast<TTBXComboBoxItem *>(GetComponent(fcRemotePathComboBox));
  9481. if (!TextOnly)
  9482. {
  9483. TStrings * Items = RemotePathComboBox->Strings;
  9484. Items->BeginUpdate();
  9485. try
  9486. {
  9487. Items->Clear();
  9488. if ((Terminal != NULL) && !RemoteDirView->Path.IsEmpty())
  9489. {
  9490. UnicodeString APath = UnixExcludeTrailingBackslash(RemoteDirView->Path);
  9491. while (!IsUnixRootPath(APath))
  9492. {
  9493. int P = APath.LastDelimiter(L'/');
  9494. DebugAssert(P >= 0);
  9495. Items->Insert(0, APath.SubString(P + 1, APath.Length() - P));
  9496. APath.SetLength(P - 1);
  9497. }
  9498. Items->Insert(0, Customunixdirview_SUnixDefaultRootName);
  9499. }
  9500. }
  9501. __finally
  9502. {
  9503. Items->EndUpdate();
  9504. }
  9505. }
  9506. RemotePathComboBox->ItemIndex = RemotePathComboBox->Strings->Count - 1;
  9507. // Setting ItemIndex to -1 does not reset its text
  9508. if (RemotePathComboBox->Strings->Count == 0)
  9509. {
  9510. RemotePathComboBox->Text = L"";
  9511. }
  9512. }
  9513. //---------------------------------------------------------------------------
  9514. void __fastcall TCustomScpExplorerForm::DoRemotePathComboBoxAdjustImageIndex(
  9515. TTBXComboBoxItem * Sender, const UnicodeString DebugUsedArg(AText), int AIndex, int & ImageIndex)
  9516. {
  9517. if (AIndex < 0)
  9518. {
  9519. AIndex = Sender->ItemIndex;
  9520. }
  9521. ImageIndex = (AIndex < Sender->Strings->Count - 1 ? StdDirIcon : StdDirSelIcon);
  9522. }
  9523. //---------------------------------------------------------------------------
  9524. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxAdjustImageIndex(
  9525. TTBXComboBoxItem * Sender, const UnicodeString AText, int AIndex,
  9526. int & ImageIndex)
  9527. {
  9528. DoRemotePathComboBoxAdjustImageIndex(Sender, AText, AIndex, ImageIndex);
  9529. }
  9530. //---------------------------------------------------------------------------
  9531. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxDrawItem(
  9532. TTBXCustomList * /*Sender*/, TCanvas * /*ACanvas*/, TRect & ARect, int AIndex,
  9533. int /*AHoverIndex*/, bool & /*DrawDefault*/)
  9534. {
  9535. if (!IsLocalBrowserMode())
  9536. {
  9537. ARect.Left += (10 * AIndex);
  9538. }
  9539. }
  9540. //---------------------------------------------------------------------------
  9541. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxMeasureWidth(
  9542. TTBXCustomList * /*Sender*/, TCanvas * /*ACanvas*/, int AIndex, int &AWidth)
  9543. {
  9544. AWidth += (10 * AIndex);
  9545. }
  9546. //---------------------------------------------------------------------------
  9547. void __fastcall TCustomScpExplorerForm::DoRemotePathComboBoxItemClick(TObject * Sender)
  9548. {
  9549. TTBXComboBoxItem * RemotePathComboBox = dynamic_cast<TTBXComboBoxItem*>(Sender);
  9550. UnicodeString APath = UnixExcludeTrailingBackslash(RemoteDirView->Path);
  9551. int Index = RemotePathComboBox->ItemIndex;
  9552. while (Index < RemotePathComboBox->Strings->Count - 1)
  9553. {
  9554. APath = UnixExtractFileDir(APath);
  9555. Index++;
  9556. }
  9557. // VanDyke style paths
  9558. if (APath.IsEmpty())
  9559. {
  9560. DebugAssert(RemotePathComboBox->ItemIndex == 0);
  9561. APath = ROOTDIRECTORY;
  9562. }
  9563. if (RemoteDirView->Path != APath)
  9564. {
  9565. RemoteDirView->Path = APath;
  9566. }
  9567. }
  9568. //---------------------------------------------------------------------------
  9569. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxItemClick(TObject * Sender)
  9570. {
  9571. DoRemotePathComboBoxItemClick(Sender);
  9572. }
  9573. //---------------------------------------------------------------------------
  9574. void __fastcall TCustomScpExplorerForm::DoRemotePathComboBoxCancel(TObject * Sender)
  9575. {
  9576. DebugAssert(Sender == GetComponent(fcRemotePathComboBox));
  9577. DebugUsedParam(Sender);
  9578. UpdateRemotePathComboBox(true);
  9579. }
  9580. //---------------------------------------------------------------------------
  9581. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxCancel(TObject * Sender)
  9582. {
  9583. DoRemotePathComboBoxCancel(Sender);
  9584. }
  9585. //---------------------------------------------------------------------------
  9586. void __fastcall TCustomScpExplorerForm::DirViewEditing(
  9587. TObject * Sender, TListItem * Item, bool & /*AllowEdit*/)
  9588. {
  9589. ResetIncrementalSearch();
  9590. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  9591. DebugAssert(DirView != NULL);
  9592. if (!WinConfiguration->RenameWholeName && !DirView->ItemIsDirectory(Item))
  9593. {
  9594. HWND Edit = ListView_GetEditControl(DirView->Handle);
  9595. // OnEditing is called also from TCustomListView::CanEdit
  9596. if (Edit != NULL)
  9597. {
  9598. EditSelectBaseName(Edit);
  9599. }
  9600. }
  9601. }
  9602. //---------------------------------------------------------------------------
  9603. //---------------------------------------------------------------------------
  9604. TDragDropFilesEx * __fastcall TCustomScpExplorerForm::CreateDragDropFilesEx()
  9605. {
  9606. TDragDropFilesEx * Result = new TDragDropFilesEx(this);
  9607. Result->AutoDetectDnD = false;
  9608. Result->NeedValid = TFileExMustDnDSet() << nvFilename;
  9609. Result->RenderDataOn = rdoEnterAndDropSync;
  9610. Result->TargetEffects = TDropEffectSet() << deCopy << deMove;
  9611. return Result;
  9612. }
  9613. //---------------------------------------------------------------------------
  9614. void __fastcall TCustomScpExplorerForm::UpdateDarkMode()
  9615. {
  9616. if (IsWin10Build(2004))
  9617. {
  9618. FImmersiveDarkMode = WinConfiguration->UseDarkTheme();
  9619. BOOL DarkMode = FImmersiveDarkMode ? TRUE : FALSE;
  9620. const int DWMWA_USE_IMMERSIVE_DARK_MODE = 20;
  9621. DwmSetWindowAttribute(Handle, DWMWA_USE_IMMERSIVE_DARK_MODE, &DarkMode, sizeof(DarkMode));
  9622. }
  9623. }
  9624. //---------------------------------------------------------------------------
  9625. void __fastcall TCustomScpExplorerForm::CreateWnd()
  9626. {
  9627. TForm::CreateWnd();
  9628. // win32-darkmode calls AllowDarkModeForWindow(this, true) here, but it does not seem to have any effect
  9629. UpdateDarkMode();
  9630. if (FSessionsDragDropFilesEx == NULL)
  9631. {
  9632. FSessionsDragDropFilesEx = CreateDragDropFilesEx();
  9633. FSessionsDragDropFilesEx->OnDragOver = SessionsDDDragOver;
  9634. FSessionsDragDropFilesEx->OnProcessDropped = SessionsDDProcessDropped;
  9635. FSessionsDragDropFilesEx->OnDragEnter = SessionsDDDragEnter;
  9636. FSessionsDragDropFilesEx->OnDragLeave = SessionsDDDragLeave;
  9637. }
  9638. if (FQueueDragDropFilesEx == NULL)
  9639. {
  9640. FQueueDragDropFilesEx = CreateDragDropFilesEx();
  9641. // No need to set OnDragOver as we do not have any restrictions
  9642. FQueueDragDropFilesEx->OnProcessDropped = QueueDDProcessDropped;
  9643. FQueueDragDropFilesEx->OnDragEnter = QueueDDDragEnter;
  9644. FQueueDragDropFilesEx->OnDragLeave = QueueDDDragLeave;
  9645. }
  9646. }
  9647. //---------------------------------------------------------------------------
  9648. void __fastcall TCustomScpExplorerForm::DestroyWnd()
  9649. {
  9650. TForm::DestroyWnd();
  9651. FSessionsDragDropFilesEx->DragDropControl = NULL;
  9652. FQueueDragDropFilesEx->DragDropControl = NULL;
  9653. }
  9654. //---------------------------------------------------------------------------
  9655. void __fastcall TCustomScpExplorerForm::FormShow(TObject * /*Sender*/)
  9656. {
  9657. SideEnter(FCurrentSide);
  9658. FEverShown = true;
  9659. }
  9660. //---------------------------------------------------------------------------
  9661. void __fastcall TCustomScpExplorerForm::DoFindFiles(
  9662. TTerminal * ATerminal, UnicodeString Directory, const TFileMasks & FileMask,
  9663. TFileFoundEvent OnFileFound, TFindingFileEvent OnFindingFile)
  9664. {
  9665. if (!NonVisualDataModule->Busy)
  9666. {
  9667. TTerminalManager::Instance()->ActiveSession = DebugNotNull(dynamic_cast<TManagedTerminal *>(ATerminal));
  9668. Configuration->Usage->Inc(L"FileFinds");
  9669. LockWindow(true);
  9670. NonVisualDataModule->StartBusy();
  9671. WinConfiguration->LockedInterface = true;
  9672. try
  9673. {
  9674. Terminal->FilesFind(Directory, FileMask, OnFileFound, OnFindingFile);
  9675. }
  9676. __finally
  9677. {
  9678. WinConfiguration->LockedInterface = false;
  9679. NonVisualDataModule->EndBusy();
  9680. UnlockWindow();
  9681. }
  9682. }
  9683. }
  9684. //---------------------------------------------------------------------------
  9685. void __fastcall TCustomScpExplorerForm::DoFocusRemotePath(TTerminal * ATerminal, const UnicodeString & Path)
  9686. {
  9687. if (!NonVisualDataModule->Busy)
  9688. {
  9689. TTerminalManager::Instance()->ActiveSession = DebugNotNull(dynamic_cast<TManagedTerminal *>(ATerminal));
  9690. SetFocus();
  9691. RemoteDirView->Path = UnixExtractFilePath(Path);
  9692. UnicodeString FileName = UnixExtractFileName(Path);
  9693. TListItem * Item = RemoteDirView->FindFileItem(FileName);
  9694. // If not found, it can because we have loaded directory from cache and the file is new, try again after reload.
  9695. // (tbd: we should not even try this, if we have not loaded the directory from the cache)
  9696. if (Item == NULL)
  9697. {
  9698. ReloadDirectory(osRemote);
  9699. Item = RemoteDirView->FindFileItem(FileName);
  9700. }
  9701. if (Item != NULL)
  9702. {
  9703. RemoteDirView->ItemFocused = Item;
  9704. RemoteDirView->ItemFocused->MakeVisible(false);
  9705. RemoteDirView->SetFocus();
  9706. }
  9707. }
  9708. }
  9709. //---------------------------------------------------------------------------
  9710. bool __fastcall TCustomScpExplorerForm::CanOperateOnFoundFiles(TTerminal * ATerminal)
  9711. {
  9712. bool Result = !NonVisualDataModule->Busy;
  9713. if (Result)
  9714. {
  9715. TTerminalManager::Instance()->ActiveSession = DebugNotNull(dynamic_cast<TManagedTerminal *>(ATerminal));
  9716. }
  9717. return Result;
  9718. }
  9719. //---------------------------------------------------------------------------
  9720. void __fastcall TCustomScpExplorerForm::DoOperationOnFoundFiles(
  9721. TFileOperation Operation, TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  9722. {
  9723. if (CanOperateOnFoundFiles(ATerminal))
  9724. {
  9725. TValueRestorer<TFileOperationFinishedEvent> OnFileOperationFinishedRestorer(FOnFileOperationFinished);
  9726. FOnFileOperationFinished = OnFileOperationFinished;
  9727. ExecuteFileOperation(Operation, osRemote, FileList, false, NULL);
  9728. }
  9729. }
  9730. //---------------------------------------------------------------------------
  9731. void __fastcall TCustomScpExplorerForm::DoDeleteFoundFiles(
  9732. TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  9733. {
  9734. DoOperationOnFoundFiles(foDelete, ATerminal, FileList, OnFileOperationFinished);
  9735. }
  9736. //---------------------------------------------------------------------------
  9737. void __fastcall TCustomScpExplorerForm::DoDownloadFoundFiles(
  9738. TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  9739. {
  9740. DoOperationOnFoundFiles(foCopy, ATerminal, FileList, OnFileOperationFinished);
  9741. }
  9742. //---------------------------------------------------------------------------
  9743. void __fastcall TCustomScpExplorerForm::DoEditFoundFiles(
  9744. TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  9745. {
  9746. if (CanOperateOnFoundFiles(ATerminal))
  9747. {
  9748. for (int Index = 0; Index < FileList->Count; Index++)
  9749. {
  9750. UnicodeString FileName = FileList->Strings[Index];
  9751. TRemoteFile * File = static_cast<TRemoteFile *>(FileList->Objects[Index]);
  9752. ExecuteRemoteFile(FileName, File, efDefaultEditor);
  9753. OnFileOperationFinished(osRemote, FileName, true);
  9754. }
  9755. }
  9756. }
  9757. //---------------------------------------------------------------------------
  9758. void __fastcall TCustomScpExplorerForm::RemoteFindFiles()
  9759. {
  9760. FFileFindTerminal = Terminal;
  9761. ShowFileFindDialog(
  9762. Terminal, RemoteDirView->Path, DoFindFiles, DoFocusRemotePath, DoDeleteFoundFiles,
  9763. DoDownloadFoundFiles, DoEditFoundFiles);
  9764. }
  9765. //---------------------------------------------------------------------------
  9766. void __fastcall TCustomScpExplorerForm::UpdateTaskbarList(ITaskbarList3 * TaskbarList)
  9767. {
  9768. FTaskbarList = TaskbarList;
  9769. }
  9770. //---------------------------------------------------------------------------
  9771. void __fastcall TCustomScpExplorerForm::SessionsPageControlMouseDown(
  9772. TObject * /*Sender*/, TMouseButton Button, TShiftState /*Shift*/, int X, int Y)
  9773. {
  9774. int Index = SessionsPageControl->IndexOfTabAt(X, Y);
  9775. if (Index >= 0)
  9776. {
  9777. if (Button == mbLeft)
  9778. {
  9779. // WORKAROUND: "Mouse down" is raised only after tab is switched.
  9780. // If switching tab (switching session) takes long enough for user
  9781. // to actually release the button, "mouse down" is still raised,
  9782. // but we do not get "mouse up" event, so dragging is not cancelled,
  9783. // prevent that by not beginning dragging in the first place.
  9784. int Key = ((GetSystemMetrics(SM_SWAPBUTTON) == 0) ? VK_LBUTTON : VK_RBUTTON);
  9785. if (IsKeyPressed(Key))
  9786. {
  9787. // when user clicks the "+", we get mouse down only after the session
  9788. // is closed, when new session tab is already on X:Y, so dragging
  9789. // starts, prevent that
  9790. if (MilliSecondsBetween(Now(), FSessionsPageControlNewTabTime) > 500)
  9791. {
  9792. TTerminal * Terminal = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  9793. if (Terminal != NULL)
  9794. {
  9795. SessionsPageControl->BeginDrag(false);
  9796. }
  9797. }
  9798. }
  9799. }
  9800. else if (Button == mbMiddle)
  9801. {
  9802. // ignore middle-click for "New session tab" and the last local-local tab
  9803. TManagedTerminal * Session = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  9804. if ((Session != NULL) && CanCloseSession(Session))
  9805. {
  9806. CloseSessionTab(Index);
  9807. }
  9808. }
  9809. }
  9810. }
  9811. //---------------------------------------------------------------------------
  9812. void __fastcall TCustomScpExplorerForm::SessionsPageControlDragDrop(
  9813. TObject * /*Sender*/, TObject * /*Source*/, int X, int Y)
  9814. {
  9815. int Index = SessionsPageControl->IndexOfTabAt(X, Y);
  9816. // do not allow dropping on the "+" tab
  9817. TTerminal * TargetTerminal = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  9818. if ((TargetTerminal != NULL) &&
  9819. (SessionsPageControl->ActivePage->PageIndex != Index))
  9820. {
  9821. Configuration->Usage->Inc(L"SessionTabMoves");
  9822. // this is almost redundant as we would recreate tabs in SessionListChanged,
  9823. // but we want to actually prevent that to avoid flicker
  9824. SessionsPageControl->ActivePage->PageIndex = Index;
  9825. TTerminal * Terminal = GetSessionTabSession(SessionsPageControl->ActivePage);
  9826. TTerminalManager::Instance()->Move(Terminal, TargetTerminal);
  9827. }
  9828. }
  9829. //---------------------------------------------------------------------------
  9830. void __fastcall TCustomScpExplorerForm::SessionsPageControlDragOver(
  9831. TObject * Sender, TObject * Source, int X, int Y,
  9832. TDragState /*State*/, bool & Accept)
  9833. {
  9834. Accept = (Sender == Source);
  9835. if (Accept)
  9836. {
  9837. int Index = SessionsPageControl->IndexOfTabAt(X, Y);
  9838. TTerminal * Terminal = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  9839. // do not allow dragging to the "+" tab
  9840. Accept = (Terminal != NULL);
  9841. }
  9842. }
  9843. //---------------------------------------------------------------------------
  9844. void __fastcall TCustomScpExplorerForm::SessionsDDDragOver(
  9845. int DebugUsedArg(KeyState), const TPoint & Point, int & Effect, int DebugUsedArg(PreferredEffect))
  9846. {
  9847. int Index = SessionsPageControl->IndexOfTabAt(Point.X, Point.Y);
  9848. if (Index < 0)
  9849. {
  9850. Effect = DROPEFFECT_NONE;
  9851. }
  9852. else
  9853. {
  9854. TManagedTerminal * TargetSession = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  9855. // do not allow dropping on the "+" tab or on disconnected or local-local tabs
  9856. if (!IsActiveTerminal(TargetSession) || DebugAlwaysFalse(TargetSession->LocalBrowser))
  9857. {
  9858. Effect = DROPEFFECT_NONE;
  9859. }
  9860. else if ((TargetSession != Terminal) && (Effect == DROPEFFECT_MOVE))
  9861. {
  9862. Effect = DROPEFFECT_COPY;
  9863. }
  9864. }
  9865. }
  9866. //---------------------------------------------------------------------------
  9867. void __fastcall TCustomScpExplorerForm::SessionsDDProcessDropped(
  9868. TObject * /*Sender*/, int /*KeyState*/, const TPoint & Point, int Effect)
  9869. {
  9870. int Index = SessionsPageControl->IndexOfTabAt(Point.X, Point.Y);
  9871. // do not allow dropping on local-local, disconnected and the "+" tab
  9872. // (though we do not seem to get here in that case anyway, contrary to RemoteFileControlDDTargetDrop)
  9873. TManagedTerminal * TargetTerminal = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  9874. if (DebugAlwaysTrue(IsActiveTerminal(TargetTerminal) && !TargetTerminal->LocalBrowser))
  9875. {
  9876. DebugAssert(!IsFileControl(DropSourceControl, osRemote));
  9877. if (!IsFileControl(DropSourceControl, osRemote))
  9878. {
  9879. TTerminalManager::Instance()->ActiveSession = TargetTerminal;
  9880. RemoteFileControlDragDropFileOperation(SessionsPageControl, Effect,
  9881. // Why don't we use Terminal->CurrentDirectory directly?
  9882. TTerminalManager::Instance()->ActiveTerminal->CurrentDirectory,
  9883. // do not force queue, drag drop
  9884. false, true);
  9885. }
  9886. }
  9887. }
  9888. //---------------------------------------------------------------------------
  9889. void __fastcall TCustomScpExplorerForm::QueueDDProcessDropped(
  9890. TObject * /*Sender*/, int /*KeyState*/, const TPoint & /*Point*/, int Effect)
  9891. {
  9892. // Downloads are handled in RemoteFileControlDDEnd
  9893. if (!IsFileControl(DropSourceControl, osRemote))
  9894. {
  9895. RemoteFileControlDragDropFileOperation(QueueView3, Effect,
  9896. Terminal->CurrentDirectory,
  9897. // force queue, drag drop
  9898. true, true);
  9899. }
  9900. }
  9901. //---------------------------------------------------------------------------
  9902. void __fastcall TCustomScpExplorerForm::FormClose(TObject * /*Sender*/, TCloseAction & /*Action*/)
  9903. {
  9904. FShowing = false;
  9905. // Do not save empty workspace
  9906. if (WinConfiguration->AutoSaveWorkspace && (ManagedSession != NULL))
  9907. {
  9908. std::unique_ptr<TObjectList> DataList(DoCollectWorkspace());
  9909. UnicodeString Name = WorkspaceName();
  9910. DoSaveWorkspace(
  9911. Name, DataList.get(),
  9912. !Configuration->DisablePasswordStoring &&
  9913. WinConfiguration->AutoSaveWorkspacePasswords, false);
  9914. WinConfiguration->LastStoredSession = Name;
  9915. }
  9916. }
  9917. //---------------------------------------------------------------------------
  9918. void __fastcall TCustomScpExplorerForm::RemoteDirViewRead(TObject * /*Sender*/)
  9919. {
  9920. TManagedTerminal * ManagedTerminal =
  9921. dynamic_cast<TManagedTerminal *>(RemoteDirView->Terminal);
  9922. if (ManagedTerminal != NULL)
  9923. {
  9924. ManagedTerminal->DirectoryLoaded = Now();
  9925. }
  9926. }
  9927. //---------------------------------------------------------------------------
  9928. void __fastcall TCustomScpExplorerForm::DirViewSelectItem(TObject * Sender,
  9929. TListItem * /*Item*/, bool /*Selected*/)
  9930. {
  9931. TCustomDirView * DirView = reinterpret_cast<TCustomDirView *>(Sender);
  9932. switch (DirView->LastSelectMethod)
  9933. {
  9934. case smKeyboard:
  9935. Configuration->Usage->Inc(L"KeyboardSelections");
  9936. break;
  9937. case smMouse:
  9938. Configuration->Usage->Inc(L"MouseSelections");
  9939. break;
  9940. }
  9941. }
  9942. //---------------------------------------------------------------------------
  9943. int __fastcall TCustomScpExplorerForm::AddSessionColor(TColor Color)
  9944. {
  9945. if (Color != 0)
  9946. {
  9947. return GetSessionColorImage(FSessionColors, Color, FSessionColorMaskImageIndex);
  9948. }
  9949. else
  9950. {
  9951. return FSessionTabImageIndex;
  9952. }
  9953. }
  9954. //---------------------------------------------------------------------------
  9955. int __fastcall TCustomScpExplorerForm::AddFixedSessionImage(int GlyphsSourceIndex)
  9956. {
  9957. TPngImageCollectionItem * Item =
  9958. GlyphsModule->ExplorerImages->PngImages->Items[GlyphsSourceIndex];
  9959. FSessionColors->AddPng(Item->PngImage);
  9960. return FSessionColors->Count - 1;
  9961. }
  9962. //---------------------------------------------------------------------------
  9963. void __fastcall TCustomScpExplorerForm::AddFixedSessionImages()
  9964. {
  9965. FSessionColors->SetSize(GlyphsModule->ExplorerImages->Width, GlyphsModule->ExplorerImages->Height);
  9966. FNewRemoteTabTabImageIndex = AddFixedSessionImage(NonVisualDataModule->NewRemoteTabAction->ImageIndex);
  9967. FNewLocalTabTabImageIndex = AddFixedSessionImage(NonVisualDataModule->NewLocalTabAction->ImageIndex);
  9968. FSessionTabImageIndex = AddFixedSessionImage(SiteImageIndex);
  9969. FSessionColorMaskImageIndex = AddFixedSessionImage(SiteColorMaskImageIndex);
  9970. FLocalBrowserTabImageIndex = AddFixedSessionImage(LocalBrowserImageIndex);
  9971. }
  9972. //---------------------------------------------------------------------------
  9973. int TCustomScpExplorerForm::GetNewTabActionImageIndex()
  9974. {
  9975. return NonVisualDataModule->NewRemoteTabAction->ImageIndex;
  9976. }
  9977. //---------------------------------------------------------------------------
  9978. int TCustomScpExplorerForm::GetNewTabTabImageIndex(TOperationSide Side)
  9979. {
  9980. switch (Side)
  9981. {
  9982. case osLocal:
  9983. return FNewLocalTabTabImageIndex;
  9984. case osRemote:
  9985. case osCurrent:
  9986. return FNewRemoteTabTabImageIndex;
  9987. default:
  9988. DebugFail();
  9989. return -1;
  9990. }
  9991. }
  9992. //---------------------------------------------------------------------------
  9993. void __fastcall TCustomScpExplorerForm::CollectItemsWithTextDisplayMode(TWinControl * Control)
  9994. {
  9995. for (int Index = 0; Index < Control->ControlCount; Index++)
  9996. {
  9997. TControl * AControl = Control->Controls[Index];
  9998. TWinControl * WinControl = dynamic_cast<TWinControl *>(AControl);
  9999. if (WinControl != NULL)
  10000. {
  10001. CollectItemsWithTextDisplayMode(WinControl);
  10002. }
  10003. TTBCustomToolbar * Toolbar = dynamic_cast<TTBCustomToolbar *>(AControl);
  10004. if (Toolbar != NULL)
  10005. {
  10006. // we care for top-level items only
  10007. TTBCustomItem * Items = Toolbar->Items;
  10008. for (int ItemIndex = 0; ItemIndex < Items->Count; ItemIndex++)
  10009. {
  10010. TTBCustomItem * Item = Items->Items[ItemIndex];
  10011. if (((Item->DisplayMode == nbdmImageAndText) ||
  10012. (dynamic_cast<TTBXLabelItem *>(Item) != NULL)) &&
  10013. EligibleForImageDisplayMode(Item))
  10014. {
  10015. FItemsWithTextDisplayMode.insert(Item);
  10016. }
  10017. }
  10018. }
  10019. }
  10020. }
  10021. //---------------------------------------------------------------------------
  10022. bool __fastcall TCustomScpExplorerForm::EligibleForImageDisplayMode(TTBCustomItem * /*Item*/)
  10023. {
  10024. return true;
  10025. }
  10026. //---------------------------------------------------------------------------
  10027. bool __fastcall TCustomScpExplorerForm::UpdateToolbarDisplayMode()
  10028. {
  10029. bool SelectiveToolbarText = WinConfiguration->SelectiveToolbarText;
  10030. TTBItemDisplayMode DisplayMode = (SelectiveToolbarText ? nbdmImageAndText : nbdmDefault);
  10031. typedef std::set<TTBCustomItem *> TItemsWithTextDisplayMode;
  10032. TItemsWithTextDisplayMode::iterator i = FItemsWithTextDisplayMode.begin();
  10033. bool Result = true;
  10034. while (Result && (i != FItemsWithTextDisplayMode.end()))
  10035. {
  10036. TTBCustomItem * Item = *i;
  10037. TTBXLabelItem * Label = dynamic_cast<TTBXLabelItem *>(Item);
  10038. if (Label != NULL)
  10039. {
  10040. // optimization
  10041. if (Label->Visible == SelectiveToolbarText)
  10042. {
  10043. Result = false;
  10044. }
  10045. else
  10046. {
  10047. Label->Visible = SelectiveToolbarText;
  10048. }
  10049. }
  10050. else
  10051. {
  10052. // optimization
  10053. if (Item->DisplayMode == DisplayMode)
  10054. {
  10055. Result = false;
  10056. }
  10057. else
  10058. {
  10059. Item->DisplayMode = DisplayMode;
  10060. }
  10061. }
  10062. ++i;
  10063. }
  10064. UpdateNewTabTab();
  10065. return Result;
  10066. }
  10067. //---------------------------------------------------------------------------
  10068. void __fastcall TCustomScpExplorerForm::DisplaySystemContextMenu()
  10069. {
  10070. DebugFail();
  10071. }
  10072. //---------------------------------------------------------------------------
  10073. bool __fastcall TCustomScpExplorerForm::IsBusy()
  10074. {
  10075. // Among other, a lock level is non zero, while directory is being loaded,
  10076. // even when it happens as a result of dirview navigation,
  10077. // i.e. when TNonVisualDataModule is NOT FBusy.
  10078. // That's why the TNonVisualDataModule::GetBusy calls this method.
  10079. // Among other this prevents a panel auto update to occur while
  10080. // directory is changing.
  10081. return (FLockLevel > 0) || DirView(osCurrent)->IsEditing();
  10082. }
  10083. //---------------------------------------------------------------------------
  10084. Boolean __fastcall TCustomScpExplorerForm::AllowedAction(TAction * Action, TActionAllowed Allowed)
  10085. {
  10086. // While the window is disabled, we still seem to process menu shortcuts at least,
  10087. // so stop it at least here.
  10088. // See also TCustomScpExplorerForm::RemoteDirViewBusy
  10089. bool Result;
  10090. if (Allowed == aaUpdate)
  10091. {
  10092. Result = true;
  10093. }
  10094. else
  10095. {
  10096. Result = !NonVisualDataModule->Busy;
  10097. if (Result && (Allowed == aaShortCut))
  10098. {
  10099. // Otherwise F5 for local-local will be swallowed by disabled local<>remote actions in TCustomScpExplorerForm::KeyDown
  10100. Action->Update();
  10101. Result = Action->Enabled;
  10102. }
  10103. }
  10104. return Result;
  10105. }
  10106. //---------------------------------------------------------------------------
  10107. void __fastcall TCustomScpExplorerForm::EditMenuItemPopup(TTBCustomItem * Sender, bool FromLink)
  10108. {
  10109. NonVisualDataModule->EditMenuItemPopup(Sender, FromLink);
  10110. }
  10111. //---------------------------------------------------------------------------
  10112. void __fastcall TCustomScpExplorerForm::DirViewBusy(TObject * /*Sender*/, int Busy, bool & State)
  10113. {
  10114. // This is somewhat redundant to LockWindow() call from
  10115. // TTerminalManager::TerminalReadDirectoryProgress.
  10116. // But disabling window is known not to block keyboard shorcuts
  10117. // (see TCustomScpExplorerForm::AllowedAction), this hopefully works.
  10118. if (Busy > 0)
  10119. {
  10120. if (NonVisualDataModule->Busy)
  10121. {
  10122. State = false;
  10123. }
  10124. else
  10125. {
  10126. NonVisualDataModule->StartBusy();
  10127. // Will be countered for TCustomDirView.DoExecute in DoDirViewExecFile
  10128. LockWindow();
  10129. }
  10130. }
  10131. else if (Busy < 0)
  10132. {
  10133. UnlockWindow();
  10134. NonVisualDataModule->EndBusy();
  10135. }
  10136. else
  10137. {
  10138. State = NonVisualDataModule->Busy;
  10139. }
  10140. }
  10141. //---------------------------------------------------------------------------
  10142. void __fastcall TCustomScpExplorerForm::SessionsPageControlContextPopup(TObject * /*Sender*/, TPoint & MousePos, bool & Handled)
  10143. {
  10144. int Index = SessionsPageControl->IndexOfTabAt(MousePos.X, MousePos.Y);
  10145. if (Index >= 0)
  10146. {
  10147. TPopupMenu * PopupMenu = NULL;
  10148. TManagedTerminal * Session = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  10149. if (Session != NULL)
  10150. {
  10151. SessionsPageControl->ActivePageIndex = Index;
  10152. if (DebugAlwaysTrue(SessionTabSwitched()))
  10153. {
  10154. // copied from TControl.WMContextMenu
  10155. SendCancelMode(SessionsPageControl);
  10156. // explicit popup instead of using PopupMenu property
  10157. // to avoid menu to popup somewhere within SessionTabSwitched above,
  10158. // while connecting yet not-connected session and hence
  10159. // allowing an access to commands over not-completely connected session
  10160. PopupMenu = Session->LocalBrowser ? NonVisualDataModule->LocalBrowserPopup : NonVisualDataModule->SessionsPopup;
  10161. }
  10162. }
  10163. else
  10164. {
  10165. if (SupportsLocalBrowser())
  10166. {
  10167. PopupMenu = NonVisualDataModule->NewTabPopup;
  10168. }
  10169. }
  10170. if (PopupMenu != NULL)
  10171. {
  10172. TPoint Point = SessionsPageControl->ClientToScreen(MousePos);
  10173. PopupMenu->PopupComponent = SessionsPageControl;
  10174. PopupMenu->Popup(Point.x, Point.y);
  10175. }
  10176. }
  10177. Handled = true;
  10178. }
  10179. //---------------------------------------------------------------------------
  10180. bool __fastcall TCustomScpExplorerForm::CanChangePassword()
  10181. {
  10182. return HasActiveTerminal() && Terminal->IsCapable[fcChangePassword];
  10183. }
  10184. //---------------------------------------------------------------------------
  10185. void __fastcall TCustomScpExplorerForm::ChangePassword()
  10186. {
  10187. Configuration->Usage->Inc(L"PasswordChanges");
  10188. std::unique_ptr<TSessionData> ChangePasswordData(Terminal->SessionData->Clone());
  10189. ChangePasswordData->DisableAuthentationsExceptPassword();
  10190. ChangePasswordData->ChangePassword = true;
  10191. std::unique_ptr<TTerminal> ChangePasswordSession(
  10192. Terminal->CreateSecondarySession(L"ChangePassword", ChangePasswordData.get()));
  10193. ChangePasswordSession->AutoReadDirectory = false;
  10194. if (TTerminalManager::Instance()->ConnectTerminal(ChangePasswordSession.get()))
  10195. {
  10196. MessageDialog(MainInstructions(LoadStr(PASSWORD_CHANGED)), qtInformation, qaOK, HELP_CHANGE_PASSWORD);
  10197. }
  10198. else
  10199. {
  10200. Configuration->Usage->Inc(L"PasswordChangesFailed");
  10201. }
  10202. }
  10203. //---------------------------------------------------------------------------
  10204. bool __fastcall TCustomScpExplorerForm::CanPrivateKeyUpload()
  10205. {
  10206. // No nice way to assert SSH2
  10207. return HasActiveTerminal() && (Terminal->FSProtocol == cfsSFTP);
  10208. }
  10209. //---------------------------------------------------------------------------
  10210. void __fastcall TCustomScpExplorerForm::PrivateKeyUpload()
  10211. {
  10212. TOperationVisualizer OperationVisualizer;
  10213. UnicodeString FileName = Terminal->SessionData->PublicKeyFile;
  10214. TTerminalManager::Instance()->UploadPublicKey(Terminal, NULL, FileName);
  10215. }
  10216. //---------------------------------------------------------------------------
  10217. void __fastcall TCustomScpExplorerForm::ChangeScale(int M, int D)
  10218. {
  10219. TForm::ChangeScale(M, D);
  10220. int APixelsPerInch = GetControlPixelsPerInch(this);
  10221. HANDLE ResourceModule = GUIConfiguration->ChangeToDefaultResourceModule();
  10222. try
  10223. {
  10224. GlyphsModule->SetPixelsPerInch(APixelsPerInch);
  10225. }
  10226. __finally
  10227. {
  10228. GUIConfiguration->ChangeResourceModule(ResourceModule);
  10229. }
  10230. }
  10231. //---------------------------------------------------------------------------
  10232. void __fastcall TCustomScpExplorerForm::DockContextPopup(TObject * Sender, TPoint & MousePos, bool & /*Handled*/)
  10233. {
  10234. NonVisualDataModule->ControlContextPopup(Sender, MousePos);
  10235. }
  10236. //---------------------------------------------------------------------
  10237. class TPublicControl : public TControl
  10238. {
  10239. friend class TCustomScpExplorerForm;
  10240. };
  10241. //---------------------------------------------------------------------------
  10242. void __fastcall TCustomScpExplorerForm::CopyPopup(TControl * DestControl, TControl * SourceControl)
  10243. {
  10244. static_cast<TPublicControl *>(DestControl)->PopupMenu = static_cast<TPublicControl *>(SourceControl)->PopupMenu;
  10245. static_cast<TPublicControl *>(DestControl)->OnContextPopup = static_cast<TPublicControl *>(SourceControl)->OnContextPopup;
  10246. }
  10247. //---------------------------------------------------------------------------
  10248. void __fastcall TCustomScpExplorerForm::DoBookmarkClick(TOperationSide Side, TObject * Sender)
  10249. {
  10250. TBookmark * Bookmark = DebugNotNull(reinterpret_cast<TBookmark *>(dynamic_cast<TTBCustomItem *>(Sender)->Tag));
  10251. OpenBookmark(Side, Bookmark);
  10252. }
  10253. //---------------------------------------------------------------------------
  10254. void __fastcall TCustomScpExplorerForm::LocalBookmarkClick(TObject * Sender)
  10255. {
  10256. DoBookmarkClick(osLocal, Sender);
  10257. }
  10258. //---------------------------------------------------------------------------
  10259. void __fastcall TCustomScpExplorerForm::RemoteBookmarkClick(TObject * Sender)
  10260. {
  10261. DoBookmarkClick(osRemote, Sender);
  10262. }
  10263. //---------------------------------------------------------------------------
  10264. void __fastcall TCustomScpExplorerForm::CreateOpenDirMenuList(
  10265. TTBCustomItem * Menu, TOperationSide Side, TBookmarkList * BookmarkList)
  10266. {
  10267. if (BookmarkList != NULL)
  10268. {
  10269. TNotifyEvent OnBookmarkClick = (Side == osLocal) ? &LocalBookmarkClick : &RemoteBookmarkClick;
  10270. if (IsLocalBrowserMode())
  10271. {
  10272. Side = osLocal;
  10273. }
  10274. if (!WinConfiguration->UseLocationProfiles)
  10275. {
  10276. std::unique_ptr<TStringList> Directories(new TStringList());
  10277. Directories->CaseSensitive = (Side == osRemote);
  10278. for (int Index = 0; Index < BookmarkList->Count; Index++)
  10279. {
  10280. TBookmark * Bookmark = BookmarkList->Bookmarks[Index];
  10281. UnicodeString Directory = Bookmark->GetSideDirectory(Side);
  10282. if (!Directory.IsEmpty() && (Directories->IndexOf(Directory) < 0))
  10283. {
  10284. std::unique_ptr<TTBCustomItem> Item(new TTBXItem(Owner));
  10285. Item->Caption = EscapeHotkey(Directory);
  10286. Item->ShortCut = Bookmark->ShortCut;
  10287. Item->OnClick = OnBookmarkClick;
  10288. Item->Tag = reinterpret_cast<int>(Bookmark);
  10289. Directories->Add(Directory);
  10290. Menu->Add(Item.release());
  10291. }
  10292. }
  10293. }
  10294. else
  10295. {
  10296. std::unique_ptr<TStrings> Folders(CreateSortedStringList());
  10297. for (int Index = 0; Index < BookmarkList->Count; Index++)
  10298. {
  10299. TBookmark * Bookmark = BookmarkList->Bookmarks[Index];
  10300. if (!Bookmark->Node.IsEmpty())
  10301. {
  10302. Folders->Add(Bookmark->Node);
  10303. }
  10304. }
  10305. for (int Index = 0; Index < Folders->Count; Index++)
  10306. {
  10307. std::unique_ptr<TTBCustomItem> Item(new TTBXSubmenuItem(Owner));
  10308. Item->Caption = Folders->Strings[Index];
  10309. Item->ImageIndex = NonVisualDataModule->RemoteChangePathAction2->ImageIndex;
  10310. Folders->Objects[Index] = Item.get();
  10311. Menu->Add(Item.release());
  10312. }
  10313. for (int Index = 0; Index < BookmarkList->Count; Index++)
  10314. {
  10315. TBookmark * Bookmark = BookmarkList->Bookmarks[Index];
  10316. TTBCustomItem * Parent;
  10317. if (!Bookmark->Node.IsEmpty())
  10318. {
  10319. DebugAssert(Folders->IndexOf(Bookmark->Node) >= 0);
  10320. Parent = dynamic_cast<TTBCustomItem *>(Folders->Objects[Folders->IndexOf(Bookmark->Node)]);
  10321. }
  10322. else
  10323. {
  10324. Parent = Menu;
  10325. }
  10326. std::unique_ptr<TTBCustomItem> Item(new TTBXItem(Owner));
  10327. Item->Caption = Bookmark->Name;
  10328. Item->ShortCut = Bookmark->ShortCut;
  10329. Item->OnClick = OnBookmarkClick;
  10330. Item->Tag = reinterpret_cast<int>(Bookmark);
  10331. if (((Bookmark->Name == Bookmark->Local) || (Bookmark->Name == Bookmark->Remote)) &&
  10332. (Bookmark->Local.IsEmpty() || Bookmark->Remote.IsEmpty()))
  10333. {
  10334. // No hint for location profiles that are actually mere "bookmarks"
  10335. }
  10336. else
  10337. {
  10338. UnicodeString Hint = FORMAT(LoadStrPart(LOCATION_PROFILE_HINT, 1), (Bookmark->Name));
  10339. UnicodeString LongHint;
  10340. if (!Bookmark->Local.IsEmpty())
  10341. {
  10342. AddToList(LongHint, FORMAT(LoadStrPart(LOCATION_PROFILE_HINT, 2), (Bookmark->Local)), L"\n");
  10343. }
  10344. if (!Bookmark->Remote.IsEmpty())
  10345. {
  10346. AddToList(LongHint, FORMAT(LoadStrPart(LOCATION_PROFILE_HINT, 3), (Bookmark->Remote)), L"\n");
  10347. }
  10348. AddToList(Hint, LongHint, L"|");
  10349. Item->Hint = Hint;
  10350. }
  10351. Parent->Add(Item.release());
  10352. }
  10353. }
  10354. AddMenuSeparator(Menu);
  10355. }
  10356. }
  10357. //---------------------------------------------------------------------------
  10358. void __fastcall TCustomScpExplorerForm::CreateOpenDirMenu(TTBCustomItem * Menu, TOperationSide Side)
  10359. {
  10360. Menu->Clear();
  10361. std::unique_ptr<TTBCustomItem> Item;
  10362. Item.reset(new TTBXItem(Owner));
  10363. Item->Action = (Side == osLocal) ? NonVisualDataModule->LocalOpenDirAction : NonVisualDataModule->RemoteOpenDirAction;
  10364. Item->Options = Item->Options << tboDefault;
  10365. Menu->Add(Item.release());
  10366. AddMenuSeparator(Menu);
  10367. if (Terminal != NULL)
  10368. {
  10369. CreateOpenDirMenuList(Menu, Side, WinConfiguration->Bookmarks[Terminal->SessionData->SessionKey]);
  10370. }
  10371. CreateOpenDirMenuList(Menu, Side, WinConfiguration->SharedBookmarks);
  10372. Item.reset(new TTBXItem(Owner));
  10373. Item->Action = (Side == osLocal) ? NonVisualDataModule->LocalAddBookmarkAction2 : NonVisualDataModule->RemoteAddBookmarkAction2;
  10374. Menu->Add(Item.release());
  10375. }
  10376. //---------------------------------------------------------------------------
  10377. bool __fastcall TCustomScpExplorerForm::TryOpenDirectory(TOperationSide Side, const UnicodeString & Path)
  10378. {
  10379. bool Result = true;
  10380. bool Remote = !IsSideLocalBrowser(Side);
  10381. try
  10382. {
  10383. UnicodeString APath = Path;
  10384. if (Remote)
  10385. {
  10386. Terminal->ExceptionOnFail = true;
  10387. }
  10388. else
  10389. {
  10390. APath = ExpandEnvironmentVariables(Path);
  10391. }
  10392. try
  10393. {
  10394. DirView(Side)->Path = APath;
  10395. }
  10396. __finally
  10397. {
  10398. if (Remote)
  10399. {
  10400. Terminal->ExceptionOnFail = false;
  10401. }
  10402. }
  10403. }
  10404. catch (Exception & E)
  10405. {
  10406. if (!Remote || Terminal->Active)
  10407. {
  10408. ShowExtendedExceptionEx(Terminal, &E);
  10409. Result = false;
  10410. }
  10411. else
  10412. {
  10413. throw;
  10414. }
  10415. }
  10416. return Result;
  10417. }
  10418. //---------------------------------------------------------------------------
  10419. void __fastcall TCustomScpExplorerForm::ReloadDirectory(TOperationSide Side)
  10420. {
  10421. // Make sure there some feedback even when loading tiny local folders
  10422. TInstantOperationVisualizer Visualizer;
  10423. DirView(Side)->ReloadDirectory();
  10424. }
  10425. //---------------------------------------------------------------------------
  10426. void __fastcall TCustomScpExplorerForm::CloseSessionTab(int Index)
  10427. {
  10428. if (Index == SessionsPageControl->TabIndex)
  10429. {
  10430. CloseTab();
  10431. }
  10432. else
  10433. {
  10434. TTabSheet * TabSheet = SessionsPageControl->Pages[Index];
  10435. TTerminal * Terminal = GetSessionTabSession(TabSheet);
  10436. TTerminalManager * Manager = TTerminalManager::Instance();
  10437. if (!Terminal->Active)
  10438. {
  10439. Manager->FreeTerminal(Terminal);
  10440. }
  10441. else
  10442. {
  10443. TTerminalQueue * Queue = Manager->FindQueueForTerminal(Terminal);
  10444. if (CanCloseQueue(Queue))
  10445. {
  10446. Manager->FreeTerminal(Terminal);
  10447. }
  10448. }
  10449. }
  10450. }
  10451. //---------------------------------------------------------------------------
  10452. void __fastcall TCustomScpExplorerForm::SessionsPageControlTabButtonClick(TPageControl *, int Index)
  10453. {
  10454. TManagedTerminal * Session = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  10455. if (Session == NULL)
  10456. {
  10457. TRect ButtonRect = SessionsPageControl->TabButtonRect(Index);
  10458. ButtonRect = TRect(SessionsPageControl->ClientToScreen(ButtonRect.TopLeft()), SessionsPageControl->ClientToScreen(ButtonRect.BottomRight()));
  10459. //TPoint P = TPoint(ButtonRect.Left, ButtonRect.Bottom);
  10460. //P = SessionsPageControl->ClientToScreen(P);
  10461. MenuPopup(NonVisualDataModule->NewTabPopup, ButtonRect, SessionsPageControl);
  10462. }
  10463. else
  10464. {
  10465. CloseSessionTab(Index);
  10466. }
  10467. }
  10468. //---------------------------------------------------------------------------
  10469. void __fastcall TCustomScpExplorerForm::CopyFilesToClipboard(TOperationSide Side, bool OnFocused)
  10470. {
  10471. if (DebugAlwaysTrue(!IsSideLocalBrowser(Side)))
  10472. {
  10473. TInstantOperationVisualizer Visualizer;
  10474. // To trigger ClipboardDataObjectRelease (in case the clipboard already contains our data)
  10475. // before we create a new data object
  10476. ClipboardClear();
  10477. DebugAssert(FClipboardFakeDirectory.IsEmpty());
  10478. DebugAssert(FClipboardFakeMonitors.get() == NULL);
  10479. FClipboardFakeDirectory = CreateFakeTransferDirectory();
  10480. // Intermediate variable to WORKAROUND a crash in compiler
  10481. TObjectList * ClipboardFakeMonitors =
  10482. StartCreationDirectoryMonitorsOnEachDrive(FILE_NOTIFY_CHANGE_DIR_NAME, ClipboardFakeCreated);
  10483. FClipboardFakeMonitors.reset(ClipboardFakeMonitors);
  10484. if (FClipboardDragDropFilesEx.get() == NULL)
  10485. {
  10486. FClipboardDragDropFilesEx.reset(new TDragDropFilesEx(this));
  10487. FClipboardDragDropFilesEx->OnDataObjectRelease = ClipboardDataObjectRelease;
  10488. }
  10489. FClipboardDragDropFilesEx->FileList->Clear();
  10490. FClipboardDragDropFilesEx->FileList->AddItem(NULL, FClipboardFakeDirectory);
  10491. FClipboardDragDropFilesEx->CopyToClipboard();
  10492. FClipboardTerminal = Terminal;
  10493. // Need full paths, as cwd can be different once files are pasted
  10494. FClipboardFileList.reset(TRemoteFileList::CloneStrings(RemoteDirView->CreateFileList(OnFocused, true)));
  10495. for (int Index = 0; Index < FClipboardFileList->Count; Index++)
  10496. {
  10497. FClipboardFileList->Strings[Index] = UnixExcludeTrailingBackslash(FClipboardFileList->Strings[Index]);
  10498. }
  10499. }
  10500. }
  10501. //---------------------------------------------------------------------------
  10502. void __fastcall TCustomScpExplorerForm::ClipboardClear()
  10503. {
  10504. if (OpenClipboard(0))
  10505. {
  10506. EmptyClipboard(); // Calls ClipboardDataObjectRelease, if clipboard contains our data
  10507. CloseClipboard();
  10508. }
  10509. }
  10510. //---------------------------------------------------------------------------
  10511. bool __fastcall TCustomScpExplorerForm::DoesClipboardContainOurFiles()
  10512. {
  10513. return !FClipboardFakeDirectory.IsEmpty();
  10514. }
  10515. //---------------------------------------------------------------------------
  10516. void __fastcall TCustomScpExplorerForm::ClipboardStop()
  10517. {
  10518. RemoveDir(ApiPath(FClipboardFakeDirectory));
  10519. FClipboardFakeDirectory = UnicodeString();
  10520. FClipboardTerminal = NULL;
  10521. if (FDownloadingFromClipboard)
  10522. {
  10523. // We are called by the monitor, so attempt to release it would deadlock
  10524. FClipboardFakeMonitorsPendingReset = true;
  10525. }
  10526. else
  10527. {
  10528. FClipboardFakeMonitors.reset(NULL);
  10529. }
  10530. }
  10531. //---------------------------------------------------------------------------
  10532. void __fastcall TCustomScpExplorerForm::ClipboardDataObjectRelease(TObject * /*Sender*/)
  10533. {
  10534. if (DebugAlwaysTrue(!FClipboardFakeDirectory.IsEmpty()))
  10535. {
  10536. ClipboardStop();
  10537. }
  10538. }
  10539. //---------------------------------------------------------------------------
  10540. void __fastcall TCustomScpExplorerForm::ClipboardDownload(const UnicodeString & TargetDirectory, bool NoConfirmation, bool DragDrop)
  10541. {
  10542. TTerminalManager * Manager = TTerminalManager::Instance();
  10543. Manager->ActiveSession = FClipboardTerminal;
  10544. TTransferOperationParam Params;
  10545. Params.TargetDirectory = TargetDirectory;
  10546. Params.DragDrop = DragDrop;
  10547. ExecuteFileOperation(foCopy, osRemote, FClipboardFileList.get(), NoConfirmation, &Params);
  10548. }
  10549. //---------------------------------------------------------------------------
  10550. void __fastcall TCustomScpExplorerForm::ClipboardFakeCreated(TObject * /*Sender*/, const UnicodeString FileName)
  10551. {
  10552. // It can actually rarelly happen that some random file is created, while we are shutting down the monitor
  10553. // (as it pumps a Windows message queue while being shutted down)
  10554. if (DebugAlwaysTrue(!FClipboardFakeDirectory.IsEmpty()) &&
  10555. SameText(ExtractFileName(FileName), ExtractFileName(FClipboardFakeDirectory)))
  10556. {
  10557. // Can fail as it can be e.g. locked by AV, so we retry that later
  10558. bool Removed = RemoveDir(ApiPath(FileName));
  10559. try
  10560. {
  10561. if (!NonVisualDataModule->Busy)
  10562. {
  10563. DebugAssert(!FClipboardFakeMonitorsPendingReset);
  10564. FClipboardFakeMonitorsPendingReset = false;
  10565. bool NoConfirmation = (WinConfiguration->DDTransferConfirmation == asOff);
  10566. TAutoFlag Flag(FDownloadingFromClipboard);
  10567. ClipboardDownload(ExtractFilePath(FileName), NoConfirmation, true);
  10568. }
  10569. }
  10570. __finally
  10571. {
  10572. if (!Removed)
  10573. {
  10574. RemoveDir(ApiPath(FileName));
  10575. }
  10576. }
  10577. }
  10578. }
  10579. //---------------------------------------------------------------------------
  10580. void __fastcall TCustomScpExplorerForm::DirViewGetItemColor(
  10581. TObject * Sender, UnicodeString FileName, bool Directory, __int64 Size, TDateTime Modification, TColor & Color)
  10582. {
  10583. TFileMasks::TParams MaskParams;
  10584. MaskParams.Size = Size;
  10585. MaskParams.Modification = Modification;
  10586. TCustomDirView * DirView = DebugNotNull(dynamic_cast<TCustomDirView *>(Sender));
  10587. bool Local = (dynamic_cast<TUnixDirView *>(DirView) == NULL);
  10588. // TUnixDirView::ItemFullFileName returns trailing backslash for directories, what filemask cannot deal with
  10589. if (!Local && Directory)
  10590. {
  10591. FileName = UnixExcludeTrailingBackslash(FileName);
  10592. }
  10593. for (TFileColorData::TList::const_iterator Iter = FFileColors.begin(); Iter != FFileColors.end(); Iter++)
  10594. {
  10595. bool ImplicitMatch;
  10596. if (Iter->FileMask.Matches(FileName, Local, Directory, &MaskParams, false, ImplicitMatch) &&
  10597. !ImplicitMatch)
  10598. {
  10599. Color = Iter->Color;
  10600. break;
  10601. }
  10602. }
  10603. }
  10604. //---------------------------------------------------------------------------
  10605. void __fastcall TCustomScpExplorerForm::DirViewKeyDown(TObject * Sender, WORD & Key, TShiftState)
  10606. {
  10607. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  10608. if (!DirView->IsEditing())
  10609. {
  10610. // Handled here, instead of RemoteDirViewKeyPress (as on Login dialog),
  10611. // as VK_BACK is handled by TCustomDirView.KeyDown and we can swallow it here,
  10612. // while searching
  10613. if (Key == VK_BACK)
  10614. {
  10615. if (!FIncrementalSearch.IsEmpty())
  10616. {
  10617. if (FIncrementalSearch.Length() == 1)
  10618. {
  10619. ResetIncrementalSearch();
  10620. }
  10621. else
  10622. {
  10623. UnicodeString NewText = FIncrementalSearch.SubString(1, FIncrementalSearch.Length() - 1);
  10624. IncrementalSearch(NewText, false, false);
  10625. }
  10626. Key = 0;
  10627. }
  10628. }
  10629. }
  10630. }
  10631. //---------------------------------------------------------------------------
  10632. void __fastcall TCustomScpExplorerForm::DirViewKeyPress(TObject * Sender, wchar_t & Key)
  10633. {
  10634. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  10635. if (!DirView->IsEditing() && (WinConfiguration->PanelSearch != isOff))
  10636. {
  10637. // Filter control sequences.
  10638. // When not searching yet, prefer use of the space for toggling file selection
  10639. // (so we cannot incrementally search for a string starting with a space).
  10640. if ((Key > VK_SPACE) ||
  10641. ((Key == VK_SPACE) && (GetKeyState(VK_CONTROL) >= 0) && !FIncrementalSearch.IsEmpty()))
  10642. {
  10643. IncrementalSearch(FIncrementalSearch + Key, false, false);
  10644. Key = 0;
  10645. }
  10646. }
  10647. }
  10648. //---------------------------------------------------------------------------
  10649. void __fastcall TCustomScpExplorerForm::ResetIncrementalSearch()
  10650. {
  10651. if (!FIncrementalSearch.IsEmpty())
  10652. {
  10653. FIncrementalSearch = L"";
  10654. DirView(osCurrent)->UpdateStatusBar();
  10655. }
  10656. }
  10657. //---------------------------------------------------------------------------
  10658. void __fastcall TCustomScpExplorerForm::IncrementalSearch(const UnicodeString & Text, bool SkipCurrent, bool Reverse)
  10659. {
  10660. TListItem * Item = SearchFile(Text, SkipCurrent, Reverse);
  10661. if (Item == NULL)
  10662. {
  10663. MessageBeep(MB_ICONHAND);
  10664. }
  10665. else
  10666. {
  10667. TCustomDirView * ADirView = DirView(osCurrent);
  10668. {
  10669. TAutoNestingCounter Guard(FIncrementalSearching);
  10670. ADirView->FocusItem(Item);
  10671. if (ADirView->NortonLike == nlOff)
  10672. {
  10673. if ((ADirView->Selected != Item) ||
  10674. (ADirView->SelCount > 1))
  10675. {
  10676. ADirView->ClearSelection();
  10677. ADirView->Selected = Item;
  10678. }
  10679. }
  10680. }
  10681. FIncrementalSearch = Text;
  10682. Item->MakeVisible(false);
  10683. TListItem * NextItem = SearchFile(Text, true, Reverse);
  10684. FIncrementalSearchHaveNext = (NextItem != NULL) && (NextItem != Item);
  10685. ADirView->UpdateStatusBar();
  10686. }
  10687. }
  10688. //---------------------------------------------------------------------------
  10689. TListItem * __fastcall TCustomScpExplorerForm::GetNextFile(TListItem * Item, bool Reverse)
  10690. {
  10691. int Index = Item->Index;
  10692. if (!Reverse)
  10693. {
  10694. Index++;
  10695. if (Index >= Item->Owner->Count)
  10696. {
  10697. Index = 0;
  10698. }
  10699. }
  10700. else
  10701. {
  10702. Index--;
  10703. if (Index < 0)
  10704. {
  10705. Index = Item->Owner->Count - 1;
  10706. }
  10707. }
  10708. return Item->Owner->Item[Index];
  10709. }
  10710. //---------------------------------------------------------------------------
  10711. TListItem * __fastcall TCustomScpExplorerForm::SearchFile(const UnicodeString & Text, bool SkipCurrent, bool Reverse)
  10712. {
  10713. TCustomDirView * ADirView = DirView(osCurrent);
  10714. if (ADirView->Items->Count == 0)
  10715. {
  10716. return NULL;
  10717. }
  10718. else
  10719. {
  10720. TListItem * CurrentItem = (ADirView->ItemFocused != NULL) ? ADirView->ItemFocused : ADirView->Items->Item[0];
  10721. TListItem * Item = CurrentItem;
  10722. if (SkipCurrent)
  10723. {
  10724. Item = DebugNotNull(GetNextFile(Item, Reverse));
  10725. }
  10726. while (true)
  10727. {
  10728. bool Matches = false;
  10729. switch (WinConfiguration->PanelSearch)
  10730. {
  10731. case isNameStartOnly:
  10732. Matches = ContainsTextSemiCaseSensitive(ADirView->ItemFileName(Item).SubString(1, Text.Length()), Text);
  10733. break;
  10734. case isName:
  10735. Matches = ContainsTextSemiCaseSensitive(ADirView->ItemFileName(Item), Text);
  10736. break;
  10737. case isAll:
  10738. int ColCount = (ADirView->ViewStyle == vsReport) ? ADirView->ColProperties->Count : 1;
  10739. int Index = 0;
  10740. while ((Index < ColCount) && !Matches)
  10741. {
  10742. bool Visible = (ADirView->ViewStyle == vsReport) ? ADirView->ColProperties->Visible[Index] : true;
  10743. if (Visible)
  10744. {
  10745. Matches = ContainsTextSemiCaseSensitive(ADirView->GetColumnText(Item, Index), Text);
  10746. }
  10747. Index++;
  10748. }
  10749. break;
  10750. }
  10751. if (Matches)
  10752. {
  10753. return Item;
  10754. }
  10755. Item = GetNextFile(Item, Reverse);
  10756. if (Item == CurrentItem)
  10757. {
  10758. return NULL;
  10759. }
  10760. }
  10761. }
  10762. }
  10763. //---------------------------------------------------------------------------
  10764. void __fastcall TCustomScpExplorerForm::DirViewExit(TObject *)
  10765. {
  10766. ResetIncrementalSearch();
  10767. }
  10768. //---------------------------------------------------------------------------
  10769. void __fastcall TCustomScpExplorerForm::CMDialogKey(TWMKeyDown & Message)
  10770. {
  10771. if (Message.CharCode == VK_TAB)
  10772. {
  10773. if (!FIncrementalSearch.IsEmpty())
  10774. {
  10775. TShiftState Shift = KeyDataToShiftState(Message.KeyData);
  10776. bool Reverse = Shift.Contains(ssShift);
  10777. IncrementalSearch(FIncrementalSearch, true, Reverse);
  10778. Message.Result = 1;
  10779. return;
  10780. }
  10781. }
  10782. else if (Message.CharCode == VK_ESCAPE)
  10783. {
  10784. if (!FIncrementalSearch.IsEmpty())
  10785. {
  10786. ResetIncrementalSearch();
  10787. Message.Result = 1;
  10788. return;
  10789. }
  10790. }
  10791. TForm::Dispatch(&Message);
  10792. }
  10793. //---------------------------------------------------------------------------
  10794. void __fastcall TCustomScpExplorerForm::Deactivate()
  10795. {
  10796. TForm::Deactivate();
  10797. // E.g. when switching to an internal editor window
  10798. ResetIncrementalSearch();
  10799. }
  10800. //---------------------------------------------------------------------------
  10801. void __fastcall TCustomScpExplorerForm::ApplicationEventsDeactivate(TObject *)
  10802. {
  10803. // When switching to another application
  10804. ResetIncrementalSearch();
  10805. }
  10806. //---------------------------------------------------------------------------
  10807. void __fastcall TCustomScpExplorerForm::ApplicationEventsModalBegin(TObject *)
  10808. {
  10809. ResetIncrementalSearch();
  10810. }
  10811. //---------------------------------------------------------------------------
  10812. void __fastcall TCustomScpExplorerForm::DirViewChangeFocus(TObject *, TListItem *)
  10813. {
  10814. if (FIncrementalSearching <= 0)
  10815. {
  10816. ResetIncrementalSearch();
  10817. }
  10818. }
  10819. //---------------------------------------------------------------------------
  10820. void __fastcall TCustomScpExplorerForm::RemoteStatusBarMouseDown(TObject *, TMouseButton, TShiftState, int, int)
  10821. {
  10822. CountClicksForWindowPrint(this);
  10823. }
  10824. //---------------------------------------------------------------------------
  10825. void __fastcall TCustomScpExplorerForm::RemoteDirViewResize(TObject *)
  10826. {
  10827. CenterReconnectToolbar();
  10828. }
  10829. //---------------------------------------------------------------------------
  10830. void __fastcall TCustomScpExplorerForm::BrowseFile()
  10831. {
  10832. if (RemoteDirView->ItemFocused != NULL)
  10833. {
  10834. RemoteDirView->ItemFocused->Selected = true;
  10835. }
  10836. }
  10837. //---------------------------------------------------------------------------
  10838. void __fastcall TCustomScpExplorerForm::UpdateQueueFileList()
  10839. {
  10840. if (!FQueueStatusUpdating)
  10841. {
  10842. bool Refresh;
  10843. if ((FQueueStatus != NULL) && QueueFileList->Visible)
  10844. {
  10845. Refresh = FQueueStatus->UpdateFileList(FQueueController->GetFocusedPrimaryItem(), FQueueFileList.get());
  10846. }
  10847. else
  10848. {
  10849. if (FQueueFileList->GetCount() > 0)
  10850. {
  10851. FQueueFileList->Clear();
  10852. Refresh = true;
  10853. }
  10854. }
  10855. int Count = FQueueFileList->GetCount();
  10856. if (QueueFileList->Items->Count != Count)
  10857. {
  10858. DebugAssert(Refresh);
  10859. QueueFileList->Items->Count = Count;
  10860. }
  10861. if (Refresh)
  10862. {
  10863. QueueFileList->Invalidate();
  10864. }
  10865. DebugAssert(QueueFileList->Items->Count == FQueueFileList->GetCount());
  10866. }
  10867. }
  10868. //---------------------------------------------------------------------------
  10869. void __fastcall TCustomScpExplorerForm::QueueFileListData(TObject *, TListItem * Item)
  10870. {
  10871. Item->Caption = FQueueFileList->GetFileName(Item->Index);
  10872. }
  10873. //---------------------------------------------------------------------------
  10874. void __fastcall TCustomScpExplorerForm::QueueView3Change(TObject *, TListItem *, TItemChange)
  10875. {
  10876. // Should be improved, once we do not refresh the list when switching between details of the same batch.
  10877. // See TSynchronizeChecklistDialog::UpdateTimer
  10878. if (QueueFileList->Items->Count > 0)
  10879. {
  10880. QueueFileList->Items->Item[0]->MakeVisible(false);
  10881. }
  10882. UpdateQueueFileList();
  10883. }
  10884. //---------------------------------------------------------------------------
  10885. void __fastcall TCustomScpExplorerForm::QueueLabelGetStatus(TCustomPathLabel *, bool & Active)
  10886. {
  10887. Active = QueueView3->Focused() || QueueFileList->Focused();
  10888. }
  10889. //---------------------------------------------------------------------------
  10890. void __fastcall TCustomScpExplorerForm::QueueFileListEnterExit(TObject *)
  10891. {
  10892. QueueLabelUpdateStatus();
  10893. }
  10894. //---------------------------------------------------------------------------
  10895. void __fastcall TCustomScpExplorerForm::QueueFileListCustomDrawItem(
  10896. TCustomListView * Sender, TListItem * Item, TCustomDrawState, bool & DebugUsedArg(DefaultDraw))
  10897. {
  10898. int State = FQueueFileList->GetState(Item->Index);
  10899. if (State == qfsProcessed)
  10900. {
  10901. Sender->Canvas->Font->Color = clGrayText;
  10902. }
  10903. }
  10904. //---------------------------------------------------------------------------
  10905. void __fastcall TCustomScpExplorerForm::QueueFileListColumnAutoSize()
  10906. {
  10907. // We do not really need whole width, so we always preemptivelly keep free space for the vertical scrollbar
  10908. QueueFileList->Column[0]->Width =
  10909. QueueFileList->ClientWidth - GetSystemMetricsForControl(QueueFileList, SM_CXVSCROLL) - ScaleByTextHeight(QueueFileList, 8);
  10910. }
  10911. //---------------------------------------------------------------------------
  10912. void __fastcall TCustomScpExplorerForm::QueueFileListResize(TObject *)
  10913. {
  10914. QueueFileListColumnAutoSize();
  10915. }
  10916. //---------------------------------------------------------------------------
  10917. void __fastcall TCustomScpExplorerForm::CloseApp()
  10918. {
  10919. // Called from TNonVisualDataModule::ExplorerActionsExecute, which sets busy flag.
  10920. // FormCloseQuery must check busy state, so that the application cannot be closed while busy by Alt+F4 and X button.
  10921. // So we clear the flag here.
  10922. DebugAssert(NonVisualDataModule->Busy);
  10923. NonVisualDataModule->EndBusy();
  10924. try
  10925. {
  10926. Close();
  10927. }
  10928. __finally
  10929. {
  10930. NonVisualDataModule->StartBusy();
  10931. }
  10932. }
  10933. //---------------------------------------------------------------------------
  10934. bool __fastcall TCustomScpExplorerForm::IsActiveTerminal(TTerminal * Terminal)
  10935. {
  10936. return (Terminal != NULL) && Terminal->Active;
  10937. }
  10938. //---------------------------------------------------------------------------
  10939. bool __fastcall TCustomScpExplorerForm::HasManagedSession()
  10940. {
  10941. return (ManagedSession != NULL);
  10942. }
  10943. //---------------------------------------------------------------------------
  10944. bool __fastcall TCustomScpExplorerForm::HasActiveTerminal()
  10945. {
  10946. return IsActiveTerminal(Terminal);
  10947. }
  10948. //---------------------------------------------------------------------------
  10949. void TCustomScpExplorerForm::LocalLocalCopy(
  10950. TFileOperation, TOperationSide, bool DebugUsedArg(OnFocused), bool DebugUsedArg(NoConfirmation),
  10951. bool DebugUsedArg(DragDrop), unsigned int DebugUsedArg(Flags))
  10952. {
  10953. DebugFail();
  10954. }
  10955. //---------------------------------------------------------------------------
  10956. void TCustomScpExplorerForm::LocalLocalCopyCommand(TFileOperation Operation, TOperationSide Side, bool OnFocused, unsigned int Flags)
  10957. {
  10958. LocalLocalCopy(Operation, Side, OnFocused, !WinConfiguration->ConfirmTransferring, false, Flags);
  10959. }
  10960. //---------------------------------------------------------------------------
  10961. void __fastcall TCustomScpExplorerForm::SessionsPageControlResize(TObject *)
  10962. {
  10963. // 1) Changing tab list triggers the OnResize.
  10964. // 2) Is called in TForm constructor, when we are not ready to call SessionListChanged (particularly when starting with colored session)
  10965. if (!FUpdatingSessionTabs && (FSessionColors != NULL))
  10966. {
  10967. // UpdateTabsCaptionTruncation should be enough
  10968. SessionListChanged(true);
  10969. }
  10970. }
  10971. //---------------------------------------------------------------------------
  10972. UnicodeString TCustomScpExplorerForm::GetSessionPath(TManagedTerminal * ASession, TOperationSide Side)
  10973. {
  10974. if (ASession == ManagedSession)
  10975. {
  10976. UpdateSession(ASession);
  10977. }
  10978. TSessionData * Data = ASession->StateData;
  10979. UnicodeString Result;
  10980. if (Side == osLocal)
  10981. {
  10982. Result = Data->LocalDirectory;
  10983. }
  10984. else if (DebugAlwaysTrue(Side == osOther))
  10985. {
  10986. Result = ASession->LocalBrowser ? Data->OtherLocalDirectory : Data->RemoteDirectory;
  10987. }
  10988. return Result;
  10989. }
  10990. //---------------------------------------------------------------------------
  10991. UnicodeString TCustomScpExplorerForm::GetTabHintSessionDetails(TManagedTerminal * ASession)
  10992. {
  10993. UnicodeString Title = TTerminalManager::Instance()->GetSessionTitle(ASession, false);
  10994. UnicodeString Result = ASession->SessionData->DefaultSessionName;
  10995. if (Title == Result)
  10996. {
  10997. Result = EmptyStr;
  10998. }
  10999. else
  11000. {
  11001. Result += L"\n";
  11002. }
  11003. return Result;
  11004. }
  11005. //---------------------------------------------------------------------------
  11006. UnicodeString TCustomScpExplorerForm::GetTabHintDetails(TManagedTerminal * ASession)
  11007. {
  11008. UnicodeString Result = GetTabHintSessionDetails(ASession);
  11009. if (!ASession->Active)
  11010. {
  11011. Result += LoadStr(STATUS_NOT_CONNECTED2);
  11012. }
  11013. else
  11014. {
  11015. Result += GetSessionPath(ASession, osRemote);
  11016. }
  11017. return Result;
  11018. }
  11019. //---------------------------------------------------------------------------
  11020. UnicodeString TCustomScpExplorerForm::GetNewTabHintDetails()
  11021. {
  11022. return LoadStr(NEW_REMOTE_TAB_HINT);
  11023. }
  11024. //---------------------------------------------------------------------------
  11025. void __fastcall TCustomScpExplorerForm::SessionsPageControlTabHint(TPageControl *, int Index, UnicodeString & Hint)
  11026. {
  11027. if (Index >= 0)
  11028. {
  11029. TThemeTabSheet * TabSheet = SessionsPageControl->Pages[Index];
  11030. TManagedTerminal * TabSession = GetSessionTabSession(TabSheet);
  11031. if (TabSession == NULL)
  11032. {
  11033. UnicodeString NewTabTabCaption = GetNewTabTabCaption();
  11034. if (NewTabTabCaption != TabSheet->Caption)
  11035. {
  11036. Hint = NewTabTabCaption + L"|" + GetNewTabHintDetails();
  11037. }
  11038. }
  11039. else
  11040. {
  11041. TTerminalManager * Manager = TTerminalManager::Instance();
  11042. UnicodeString TabCaption = Manager->GetSessionTitle(TabSession, true);
  11043. Hint = TabCaption + L"|" + GetTabHintDetails(TabSession);
  11044. }
  11045. }
  11046. }
  11047. //---------------------------------------------------------------------------
  11048. typedef std::vector<TListItem *> TListItemsVector;
  11049. struct TCalculateSizeOperation
  11050. {
  11051. TCalculateSizeStats Stats;
  11052. TListItemsVector ListItems;
  11053. size_t Index;
  11054. };
  11055. //---------------------------------------------------------------------------
  11056. void TCustomScpExplorerForm::CalculateDirectorySizes(TOperationSide Side)
  11057. {
  11058. TCalculateSizeOperation CalculateSizeOperation;
  11059. Side = GetSide(Side);
  11060. TCustomDirView * ADirView = DirView(Side);
  11061. bool LocalSide = IsSideLocalBrowser(Side);
  11062. std::unique_ptr<TStrings> AllFileList(ADirView->CreateFileList(false, LocalSide, NULL, true));
  11063. std::unique_ptr<TStrings> FileList(new TStringList());
  11064. for (int Index = 0; Index < AllFileList->Count; Index++)
  11065. {
  11066. TListItem * Item = DebugNotNull(dynamic_cast<TListItem *>(AllFileList->Objects[Index]));
  11067. if (ADirView->ItemIsDirectory(Item))
  11068. {
  11069. FileList->AddObject(AllFileList->Strings[Index], ADirView->ItemData(Item));
  11070. CalculateSizeOperation.ListItems.push_back(Item);
  11071. }
  11072. }
  11073. CalculateSizeOperation.Index = 0;
  11074. __int64 Size = 0;
  11075. TValueRestorer<TCalculateSizeOperation *> DirectorySizeOperationRestorer(FCalculateSizeOperation);
  11076. FCalculateSizeOperation = &CalculateSizeOperation;
  11077. TValueRestorer<TFileOperationFinishedEvent> OnFileOperationFinishedRestorer(FOnFileOperationFinished);
  11078. FOnFileOperationFinished = DirectorySizeCalculated;
  11079. TValueRestorer<TFileOperation> PrimaryOperationRestorer(FPrimaryOperation);
  11080. FPrimaryOperation = foCalculateSize;
  11081. TCalculatedSizes CalculatedSizes;
  11082. CalculateSizeOperation.Stats.CalculatedSizes = &CalculatedSizes;
  11083. if (LocalSide)
  11084. {
  11085. TValueRestorer<TOperationSide> ProgressSideRestorer(FProgressSide);
  11086. FProgressSide = Side;
  11087. ManagedSession->CalculateLocalFilesSize(FileList.get(), Size, NULL, true, NULL, &CalculatedSizes);
  11088. }
  11089. else
  11090. {
  11091. TCalculateSizeParams Params;
  11092. Params.Stats = &CalculateSizeOperation.Stats;
  11093. Params.UseCache = true;
  11094. Terminal->CalculateFilesSize(FileList.get(), Size, Params);
  11095. }
  11096. }
  11097. //---------------------------------------------------------------------------
  11098. void __fastcall TCustomScpExplorerForm::DirectorySizeCalculated(TOperationSide Side, const UnicodeString & FileName, bool Success)
  11099. {
  11100. DebugAlwaysTrue(FCalculateSizeOperation != NULL);
  11101. TListItem * Item = VisualiseOperationFinished(Side, FileName, false);
  11102. TCustomDirView * ADirView = dynamic_cast<TCustomDirView *>(Item->Owner->Owner);
  11103. // optimization
  11104. bool GotExpectedItem =
  11105. (FCalculateSizeOperation->Index < FCalculateSizeOperation->ListItems.size()) &&
  11106. (FCalculateSizeOperation->ListItems[FCalculateSizeOperation->Index] == Item);
  11107. int Index = -1;
  11108. if (DebugAlwaysTrue(GotExpectedItem))
  11109. {
  11110. Index = FCalculateSizeOperation->Index;
  11111. FCalculateSizeOperation->Index++;
  11112. }
  11113. else
  11114. {
  11115. TListItemsVector::iterator I =
  11116. std::find(FCalculateSizeOperation->ListItems.begin(), FCalculateSizeOperation->ListItems.end(), Item);
  11117. if (DebugAlwaysTrue(I != FCalculateSizeOperation->ListItems.end()))
  11118. {
  11119. Index = I - FCalculateSizeOperation->ListItems.begin();
  11120. }
  11121. }
  11122. if (DebugAlwaysTrue(Index >= 0) && Success)
  11123. {
  11124. __int64 Size = (*FCalculateSizeOperation->Stats.CalculatedSizes)[Index];
  11125. ADirView->SetItemCalculatedSize(Item, Size);
  11126. }
  11127. }
  11128. //---------------------------------------------------------------------------
  11129. void __fastcall TCustomScpExplorerForm::MessageDockRequestDock(TObject *, TTBCustomDockableWindow *, bool & Accept)
  11130. {
  11131. Accept = false;
  11132. }
  11133. //---------------------------------------------------------------------------