CustomScpExplorer.cpp 397 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931
  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. #define WM_PASTE_FILES (WM_WINSCP_USER + 14)
  55. static const int SessionPanelCount = 3;
  56. //---------------------------------------------------------------------------
  57. class TMutexGuard
  58. {
  59. public:
  60. TMutexGuard(HANDLE AMutex, int Message = MUTEX_RELEASE_TIMEOUT,
  61. int Timeout = 5000)
  62. {
  63. FMutex = NULL;
  64. unsigned long WaitResult = WaitForSingleObject(AMutex, Timeout);
  65. if (WaitResult == WAIT_TIMEOUT)
  66. {
  67. throw Exception(LoadStr(MUTEX_RELEASE_TIMEOUT));
  68. }
  69. else
  70. {
  71. FMutex = AMutex;
  72. }
  73. }
  74. ~TMutexGuard()
  75. {
  76. if (FMutex != NULL)
  77. {
  78. ReleaseMutex(FMutex);
  79. }
  80. }
  81. private:
  82. HANDLE FMutex;
  83. };
  84. //---------------------------------------------------------------------------
  85. class TWindowLock
  86. {
  87. public:
  88. TWindowLock(TCustomScpExplorerForm * Form) :
  89. FForm(Form)
  90. {
  91. FForm->LockWindow();
  92. }
  93. ~TWindowLock()
  94. {
  95. FForm->UnlockWindow();
  96. }
  97. private:
  98. TCustomScpExplorerForm * FForm;
  99. };
  100. //---------------------------------------------------------------------------
  101. TAutoBatch::TAutoBatch(TCustomScpExplorerForm * Form) :
  102. FForm(Form)
  103. {
  104. FForm->BatchStart(FBatchStorage);
  105. }
  106. //---------------------------------------------------------------------------
  107. TAutoBatch::~TAutoBatch()
  108. {
  109. FForm->BatchEnd(FBatchStorage);
  110. }
  111. //---------------------------------------------------------------------------
  112. struct TTransferOperationParam
  113. {
  114. TTransferOperationParam();
  115. UnicodeString TargetDirectory;
  116. bool Temp;
  117. bool DragDrop;
  118. int Options;
  119. TAutoSwitch Queue;
  120. };
  121. //---------------------------------------------------------------------------
  122. TTransferOperationParam::TTransferOperationParam()
  123. {
  124. Temp = false;
  125. DragDrop = false;
  126. Options = 0;
  127. Queue = asAuto;
  128. }
  129. //---------------------------------------------------------------------------
  130. class TTransferPresetNoteData : public TObject
  131. {
  132. public:
  133. UnicodeString Message;
  134. };
  135. //---------------------------------------------------------------------------
  136. class TTerminalNoteData : public TObject
  137. {
  138. public:
  139. TManagedTerminal * Terminal;
  140. };
  141. //---------------------------------------------------------------------------
  142. __fastcall TCustomScpExplorerForm::TCustomScpExplorerForm(TComponent* Owner):
  143. FFormRestored(false),
  144. TForm(Owner)
  145. {
  146. AddStartupSequence(L"F");
  147. FInvalid = false;
  148. FCurrentSide = osRemote;
  149. FEverShown = false;
  150. FDocks = new TList();
  151. InitControls();
  152. DebugAssert(NonVisualDataModule && !NonVisualDataModule->ScpExplorer);
  153. // So that UpdateCustomCommandsToolbar called from RestoreParams works
  154. NonVisualDataModule->ScpExplorer = this;
  155. RestoreParams();
  156. ConfigurationChanged();
  157. RemoteDirView->Invalidate();
  158. FAutoOperation = false;
  159. FOnFileOperationFinished = NULL;
  160. FPrimaryOperation = foNone;
  161. FForceExecution = false;
  162. FIgnoreNextDialogChar = 0;
  163. FErrorList = NULL;
  164. FSynchronizeProgressForm = NULL;
  165. FProgressForm = NULL;
  166. FRefreshLocalDirectory = false;
  167. FRefreshRemoteDirectory = false;
  168. FDDExtMapFile = NULL;
  169. // CreateMutexW keeps failing with ERROR_NOACCESS
  170. FDDExtMutex = CreateMutexA(NULL, false, AnsiString(DRAG_EXT_MUTEX).c_str());
  171. DebugAssert(FDDExtMutex != NULL);
  172. FDDTargetControl = NULL;
  173. FDelayedDeletionTimer = NULL;
  174. FDelayedDeletionList = new TStringList();
  175. FDDFileList = NULL;
  176. FPendingTempSpaceWarn = false;
  177. FCapturedLog = NULL;
  178. FDragDropOperation = false;
  179. memset(&FHistoryMenu, 0, sizeof(FHistoryMenu));
  180. FAllowTransferPresetAutoSelect = true;
  181. FSessionChanging = false;
  182. FCopyParamDefault = L"";
  183. FSynchronizeController = NULL;
  184. FPendingQueueActionItem = NULL;
  185. FLockLevel = 0;
  186. FLockSuspendLevel = 0;
  187. FDisabledOnLockSuspend = false;
  188. FAlternativeDelete = false;
  189. FTrayIcon = new ::TTrayIcon(0);
  190. FTrayIcon->OnClick = TrayIconClick;
  191. FMaxQueueLength = 0;
  192. FLastContextPopupScreenPoint = TPoint(-1, -1);
  193. FTransferResumeList = NULL;
  194. FDeletedFiles = NULL;
  195. FMoveToQueue = false;
  196. StandaloneOperation = false;
  197. FOnFeedSynchronizeError = NULL;
  198. FOnSynchronizeAbort = NULL;
  199. FSynchronizeTerminal = NULL;
  200. FNeedSession = false;
  201. FDoNotIdleCurrentTerminal = 0;
  202. FIncrementalSearching = 0;
  203. FIncrementalSearchHaveNext = false;
  204. FQueueFileList.reset(new TQueueFileList());
  205. FProgressSide = osCurrent;
  206. FCalculateSizeOperation = NULL;
  207. FDownloadingFromClipboard = false;
  208. FClipboardFakeMonitorsPendingReset = false;
  209. FEditorManager = new TEditorManager();
  210. FEditorManager->OnFileChange = ExecutedFileChanged;
  211. FEditorManager->OnFileReload = ExecutedFileReload;
  212. FEditorManager->OnFileEarlyClosed = ExecutedFileEarlyClosed;
  213. FEditorManager->OnFileUploadComplete = ExecutedFileUploadComplete;
  214. FLocalEditors = new TList();
  215. FQueueStatus = NULL;
  216. FQueueStatusSection = new TCriticalSection();
  217. FQueueStatusInvalidated = false;
  218. FQueueItemInvalidated = false;
  219. FQueueStatusUpdating = false;
  220. FQueueActedItem = NULL;
  221. FQueueController = new TQueueController(QueueView3);
  222. FQueueScrollOnDragOver = new TListViewScrollOnDragOver(QueueView3, true);
  223. FUserActionTimer = new TTimer(this);
  224. FUserActionTimer->Enabled = false;
  225. FUserActionTimer->Interval = 10;
  226. FUserActionTimer->OnTimer = UserActionTimer;
  227. FNotes = new TStringList();
  228. FNoteTimer = new TTimer(this);
  229. FNoteTimer->Enabled = false;
  230. FNoteTimer->OnTimer = NoteTimer;
  231. FOnNoteClick = NULL;
  232. FOle32Library = LoadLibrary(L"Ole32.dll");
  233. FDragMoveCursor = FOle32Library != NULL ?
  234. LoadCursor(FOle32Library, MAKEINTRESOURCE(2)) : NULL;
  235. UseSystemSettings(this);
  236. TTBXStringList * TransferList = dynamic_cast<TTBXStringList*>(
  237. static_cast<TObject*>(GetComponent(fcTransferList)));
  238. DebugAssert(TransferList != NULL);
  239. FTransferListHoverIndex = -1;
  240. TransferList->OnChange = TransferListChange;
  241. TransferList->OnDrawItem = TransferListDrawItem;
  242. SetSubmenu(dynamic_cast<TTBXCustomItem *>(static_cast<TObject *>(GetComponent(fcColorMenu))), true);
  243. SetSubmenu(NonVisualDataModule->ColorMenuItem, true);
  244. UseDesktopFont(SessionsPageControl);
  245. UpdateSessionsPageControlHeight();
  246. UseDesktopFont(RemoteDirView);
  247. UseDesktopFont(RemoteDriveView);
  248. UseDesktopFont(QueueView3);
  249. UseDesktopFont(QueueFileList);
  250. UseDesktopFont(QueueLabel);
  251. UseDesktopFont(RemoteStatusBar);
  252. reinterpret_cast<TLabel*>(QueueSplitter)->OnDblClick = QueueSplitterDblClick;
  253. QueueSplitter->ShowHint = true;
  254. reinterpret_cast<TLabel*>(QueueFileListSplitter)->OnDblClick = QueueFileListSplitterDblClick;
  255. QueueFileListSplitter->ShowHint = true;
  256. RemotePanelSplitter->ShowHint = true;
  257. UpdateImages();
  258. FCustomCommandMenu = CreateTBXPopupMenu(this);
  259. FCustomCommandLocalFileList = NULL;
  260. FCustomCommandRemoteFileList = NULL;
  261. FSessionColors = new TPngImageList(this);
  262. FSessionColors->ColorDepth = cd32Bit;
  263. AddFixedSessionImages();
  264. SessionsPageControl->Images = FSessionColors;
  265. UpdateQueueLabel();
  266. CheckStoreTransition();
  267. CreateHiddenWindow();
  268. StartUpdates();
  269. // Start loading drive data, so that they are ready once we need them for StartCreationDirectoryMonitorsOnEachDrive
  270. // (necessary for "explorer" only, as "commander" loads it for its drive menu)
  271. DriveInfo->NeedData();
  272. }
  273. //---------------------------------------------------------------------------
  274. __fastcall TCustomScpExplorerForm::~TCustomScpExplorerForm()
  275. {
  276. FInvalid = true;
  277. if (FClipboardTerminal != NULL)
  278. {
  279. ClipboardClear(); // implies ClipboardStop
  280. }
  281. // this has to be one of the very first things to do
  282. StopUpdateThread();
  283. // This is needed when shuting down Windows only. Otherwise it's already set NULL from Execute()
  284. TTerminalManager::Instance()->ScpExplorer = NULL;
  285. delete FCustomCommandLocalFileList;
  286. delete FCustomCommandRemoteFileList;
  287. delete FCustomCommandMenu;
  288. delete FTrayIcon;
  289. FTrayIcon = NULL;
  290. FEditorManager->CloseInternalEditors(ForceCloseInternalEditor);
  291. delete FEditorManager;
  292. ForceCloseLocalEditors();
  293. delete FLocalEditors;
  294. HideFileFindDialog();
  295. if (FDelayedDeletionTimer)
  296. {
  297. DoDelayedDeletion(NULL);
  298. SAFE_DESTROY(FDelayedDeletionTimer);
  299. }
  300. SAFE_DESTROY(FDelayedDeletionList);
  301. // sometimes we do not get DDEnd so the list is not released
  302. SAFE_DESTROY(FDDFileList);
  303. DebugAssert(FSynchronizeController == NULL);
  304. CloseHandle(FDDExtMutex);
  305. FDDExtMutex = NULL;
  306. FreeLibrary(FOle32Library);
  307. FOle32Library = NULL;
  308. FDragMoveCursor = NULL;
  309. DebugAssert(!FErrorList);
  310. if (FEverShown)
  311. {
  312. // when window is never shown (like when running command-line operation),
  313. // particularly window site is not restored correctly (BoundsRect value set
  314. // in RestoreForm gets lost during handle allocation), so we do not want
  315. // it to be stored
  316. StoreParams();
  317. }
  318. ManagedSession = NULL;
  319. Queue = NULL;
  320. DebugAssert(NonVisualDataModule && (NonVisualDataModule->ScpExplorer == this));
  321. NonVisualDataModule->ScpExplorer = NULL;
  322. delete FQueueScrollOnDragOver;
  323. FQueueScrollOnDragOver = NULL;
  324. delete FQueueController;
  325. FQueueController = NULL;
  326. delete FQueueStatusSection;
  327. FQueueStatusSection = NULL;
  328. delete FQueueStatus;
  329. FQueueStatus = NULL;
  330. delete FUserActionTimer;
  331. FUserActionTimer = NULL;
  332. delete FNoteTimer;
  333. delete FNotes;
  334. delete FNoteData;
  335. SAFE_DESTROY(FDocks);
  336. SessionsPageControl->Images = NULL;
  337. // only after clearing Terminal (after SessionListChanged => ... => AddSessionColor is called)
  338. SAFE_DESTROY(FSessionColors);
  339. SAFE_DESTROY(FSessionsDragDropFilesEx);
  340. SAFE_DESTROY(FHistoryMenu[0][0]);
  341. SAFE_DESTROY(FHistoryMenu[0][1]);
  342. SAFE_DESTROY(FHistoryMenu[1][0]);
  343. SAFE_DESTROY(FHistoryMenu[1][1]);
  344. if (FHiddenWindow != NULL)
  345. {
  346. DestroyWindow(FHiddenWindow);
  347. FHiddenWindow = NULL;
  348. }
  349. }
  350. //---------------------------------------------------------------------------
  351. void __fastcall TCustomScpExplorerForm::RefreshPanel(const UnicodeString & Session, const UnicodeString & Path)
  352. {
  353. std::unique_ptr<TSessionData> Data;
  354. if (!Session.IsEmpty())
  355. {
  356. bool DefaultsOnly;
  357. Data.reset(StoredSessions->ParseUrl(Session, NULL, DefaultsOnly));
  358. }
  359. TTerminalManager * Manager = TTerminalManager::Instance();
  360. for (int Index = 0; Index < Manager->Count; Index++)
  361. {
  362. TTerminal * Terminal = Manager->Sessions[Index];
  363. if (Session.IsEmpty() ||
  364. Manager->IsActiveTerminalForSite(Terminal, Data.get()))
  365. {
  366. if (Path.IsEmpty())
  367. {
  368. Terminal->ClearCaches();
  369. }
  370. else
  371. {
  372. Terminal->DirectoryModified(Path, true);
  373. }
  374. }
  375. }
  376. // We should flag a pending refresh for the background terminals or busy foreground terminals
  377. if (HasActiveTerminal() &&
  378. CanCommandLineFromAnotherInstance() &&
  379. (Session.IsEmpty() || Manager->IsActiveTerminalForSite(Terminal, Data.get())) &&
  380. (Path.IsEmpty() || UnixIsChildPath(Path, Terminal->CurrentDirectory)))
  381. {
  382. Terminal->ReloadDirectory();
  383. }
  384. }
  385. //---------------------------------------------------------------------------
  386. void __fastcall TCustomScpExplorerForm::WMCopyData(TMessage & Message)
  387. {
  388. PCOPYDATASTRUCT CopyData = reinterpret_cast<PCOPYDATASTRUCT>(Message.LParam);
  389. AppLogFmt(L"Received copy data message with size %d", (int(CopyData->cbData)));
  390. size_t MessageSize = sizeof(TCopyDataMessage);
  391. bool Result = DebugAlwaysTrue(CopyData->cbData == MessageSize);
  392. if (Result)
  393. {
  394. const TCopyDataMessage & Message = *reinterpret_cast<const TCopyDataMessage *>(CopyData->lpData);
  395. Result = (Message.Version == TCopyDataMessage::Version1);
  396. if (Result)
  397. {
  398. switch (Message.Command)
  399. {
  400. case TCopyDataMessage::CommandCanCommandLine:
  401. Result = CanCommandLineFromAnotherInstance();
  402. if (Result)
  403. {
  404. AppLog(L"Can process command-line from other instance");
  405. }
  406. else
  407. {
  408. AppLog(L"Cannot process command-line from other instance");
  409. }
  410. break;
  411. case TCopyDataMessage::CommandCommandLine:
  412. {
  413. UnicodeString CommandLine(Message.CommandLine);
  414. Result = CommandLineFromAnotherInstance(CommandLine);
  415. }
  416. break;
  417. case TCopyDataMessage::RefreshPanel:
  418. RefreshPanel(Message.Refresh.Session, Message.Refresh.Path);
  419. break;
  420. case TCopyDataMessage::MainWindowCheck:
  421. Result = true;
  422. break;
  423. default:
  424. Result = false;
  425. break;
  426. }
  427. }
  428. }
  429. Message.Result = Result ? 1 : 0;
  430. }
  431. //---------------------------------------------------------------------------
  432. void __fastcall TCustomScpExplorerForm::CreateHiddenWindow()
  433. {
  434. WNDCLASS WindowClass = {0};
  435. WindowClass.lpfnWndProc = DefWindowProc;
  436. WindowClass.hInstance = HInstance;
  437. WindowClass.lpszClassName = HIDDEN_WINDOW_NAME;
  438. FHiddenWindow = NULL;
  439. if (RegisterClass(&WindowClass))
  440. {
  441. FHiddenWindow = CreateWindow(HIDDEN_WINDOW_NAME, L"",
  442. WS_POPUP, 0, 0, 0, 0, 0, 0, HInstance, NULL);
  443. }
  444. }
  445. //---------------------------------------------------------------------------
  446. bool __fastcall TCustomScpExplorerForm::CanConsole()
  447. {
  448. return
  449. HasActiveTerminal() &&
  450. (Terminal->IsCapable[fcAnyCommand] || Terminal->IsCapable[fcSecondaryShell]);
  451. }
  452. //---------------------------------------------------------------------------
  453. bool __fastcall TCustomScpExplorerForm::CanCommandLineFromAnotherInstance()
  454. {
  455. bool Result = Visible && !NonVisualDataModule->Busy;
  456. return Result;
  457. }
  458. //---------------------------------------------------------------------------
  459. bool __fastcall TCustomScpExplorerForm::CommandLineFromAnotherInstance(
  460. const UnicodeString & CommandLine)
  461. {
  462. TProgramParams Params(CommandLine);
  463. bool Result = CanCommandLineFromAnotherInstance() && DebugAlwaysTrue(Params.ParamCount > 0);
  464. if (Result)
  465. {
  466. NonVisualDataModule->StartBusy();
  467. try
  468. {
  469. // this action is initiated from another process,
  470. // so it's likely that our window is not visible,
  471. // and user won't see what is going on
  472. Application->BringToFront();
  473. // reload sessions as we may be asked to open a session
  474. // just stored by another instance
  475. StoredSessions->Reload();
  476. UnicodeString SessionName = Params.Param[1];
  477. std::unique_ptr<TObjectList> DataList(new TObjectList());
  478. try
  479. {
  480. UnicodeString DownloadFile; // unused
  481. int Flags = GetCommandLineParseUrlFlags(&Params);
  482. GetLoginData(SessionName, &Params, DataList.get(), DownloadFile, true, this, Flags);
  483. }
  484. catch (EAbort &)
  485. {
  486. // Happens when opening session in PuTTY and in other situations,
  487. // in which we do not want to fail WM_COPYDATA, as that would cause master instance
  488. // to process the command-line on its own (or to pass it to yet another instance)
  489. }
  490. if (DataList->Count > 0)
  491. {
  492. TTerminalManager * Manager = TTerminalManager::Instance();
  493. Manager->ActiveSession = Manager->NewSessions(DataList.get());
  494. }
  495. }
  496. __finally
  497. {
  498. NonVisualDataModule->EndBusy();
  499. }
  500. }
  501. return Result;
  502. }
  503. //---------------------------------------------------------------------------
  504. TManagedTerminal * TCustomScpExplorerForm::GetTerminal()
  505. {
  506. TManagedTerminal * Result;
  507. if ((FManagedSession != NULL) && !FManagedSession->LocalBrowser)
  508. {
  509. Result = FManagedSession;
  510. }
  511. else
  512. {
  513. Result = NULL;
  514. }
  515. return Result;
  516. }
  517. //---------------------------------------------------------------------------
  518. void __fastcall TCustomScpExplorerForm::SetManagedSession(TManagedTerminal * value)
  519. {
  520. if (FManagedSession != value)
  521. {
  522. SessionChanging();
  523. DoSetManagedSession(value, false);
  524. }
  525. }
  526. //---------------------------------------------------------------------------
  527. void __fastcall TCustomScpExplorerForm::DoSetManagedSession(TManagedTerminal * value, bool Replace)
  528. {
  529. DebugAssert(!Replace || ((value != NULL) && !value->LocalBrowser));
  530. FManagedSession = value;
  531. {
  532. TValueRestorer<bool> AllowTransferPresetAutoSelectRestorer(FAllowTransferPresetAutoSelect);
  533. FAllowTransferPresetAutoSelect = false;
  534. TAutoFlag SessionChangingFlag(FSessionChanging);
  535. SessionChanged(Replace);
  536. }
  537. if (Terminal != NULL)
  538. {
  539. TransferPresetAutoSelect();
  540. }
  541. // Update app and tab titles, prevented above by FSessionChanging.
  542. UpdateControls();
  543. }
  544. //---------------------------------------------------------------------------
  545. void __fastcall TCustomScpExplorerForm::ReplaceTerminal(TManagedTerminal * value)
  546. {
  547. DoSetManagedSession(value, true);
  548. }
  549. //---------------------------------------------------------------------------
  550. void __fastcall TCustomScpExplorerForm::SessionChanging()
  551. {
  552. if (ManagedSession != NULL)
  553. {
  554. UpdateSession(ManagedSession);
  555. }
  556. }
  557. //---------------------------------------------------------------------------
  558. void __fastcall TCustomScpExplorerForm::SessionChanged(bool Replaced)
  559. {
  560. if (ManagedSession != NULL)
  561. {
  562. UpdateSessionColor(static_cast<TColor>(ManagedSession->StateData->Color));
  563. }
  564. if (!TTerminalManager::Instance()->Updating)
  565. {
  566. SessionListChanged();
  567. }
  568. if (Terminal != NULL)
  569. {
  570. TObject * State = NULL;
  571. if (WinConfiguration->PreservePanelState)
  572. {
  573. State = Terminal->RemoteExplorerState;
  574. DirView(osRemote)->AnnounceState(State);
  575. }
  576. try
  577. {
  578. if (Replaced)
  579. {
  580. RemoteDirView->ReplaceTerminal(Terminal);
  581. }
  582. else
  583. {
  584. RemoteDirView->Terminal = Terminal;
  585. }
  586. if (Terminal->Active)
  587. {
  588. Terminal->RefreshDirectory();
  589. }
  590. if (State != NULL)
  591. {
  592. DirView(osRemote)->RestoreState(State);
  593. }
  594. }
  595. __finally
  596. {
  597. DirView(osRemote)->AnnounceState(NULL);
  598. }
  599. }
  600. else
  601. {
  602. RemoteDirView->Terminal = NULL;
  603. }
  604. InitStatusBar();
  605. UpdateTransferList();
  606. // Update panels Enable state before refreshing the labels
  607. UpdateControls();
  608. }
  609. //---------------------------------------------------------------------------
  610. void __fastcall TCustomScpExplorerForm::SetQueue(TTerminalQueue * value)
  611. {
  612. if (Queue != value)
  613. {
  614. if (FQueue != NULL)
  615. {
  616. FQueue->OnListUpdate = NULL;
  617. FQueue->OnQueueItemUpdate = NULL;
  618. }
  619. FQueue = value;
  620. if (FQueue != NULL)
  621. {
  622. DebugAssert(FQueue->OnListUpdate == NULL);
  623. FQueue->OnListUpdate = QueueListUpdate;
  624. DebugAssert(FQueue->OnQueueItemUpdate == NULL);
  625. FQueue->OnQueueItemUpdate = QueueItemUpdate;
  626. }
  627. QueueChanged();
  628. }
  629. }
  630. //---------------------------------------------------------------------------
  631. void __fastcall TCustomScpExplorerForm::QueueView3Deletion(TObject * /*Sender*/,
  632. TListItem * Item)
  633. {
  634. if (FQueueActedItem == Item)
  635. {
  636. FQueueActedItem = NULL;
  637. if ((QueueView3->PopupMenu != NULL) &&
  638. (QueueView3->PopupMenu->PopupComponent == QueueView3))
  639. {
  640. // rather "trick", suggested by Jordan on jrsoftware.toolbar2000
  641. ReleaseCapture();
  642. }
  643. }
  644. if (Item->Data == FPendingQueueActionItem)
  645. {
  646. FPendingQueueActionItem = NULL;
  647. }
  648. }
  649. //---------------------------------------------------------------------------
  650. bool TCustomScpExplorerForm::IsAnythingQueued()
  651. {
  652. return (FQueueStatus != NULL) && (FQueueStatus->ActiveAndPendingPrimaryCount > 0);
  653. }
  654. //---------------------------------------------------------------------------
  655. void __fastcall TCustomScpExplorerForm::UpdateQueueStatus(bool QueueChanging)
  656. {
  657. {
  658. TGuard Guard(FQueueStatusSection);
  659. FQueueStatusInvalidated = false;
  660. if (FQueue != NULL)
  661. {
  662. FQueueStatus = FQueue->CreateStatus(FQueueStatus);
  663. }
  664. }
  665. if ((FQueueStatus != NULL) && (FQueueStatus->Count > FMaxQueueLength))
  666. {
  667. FMaxQueueLength = FQueueStatus->Count;
  668. Configuration->Usage->SetMax(L"MaxQueueLength", FMaxQueueLength);
  669. }
  670. {
  671. TAutoFlag Flag(FQueueStatusUpdating);
  672. FQueueController->UpdateQueueStatus(FQueueStatus);
  673. }
  674. UpdateQueueFileList();
  675. SetQueueProgress();
  676. UpdateQueueView();
  677. bool IsEmpty = !IsAnythingQueued();
  678. if (IsEmpty && (Terminal != NULL))
  679. {
  680. TOnceDoneOperation OnceDoneOperation =
  681. NonVisualDataModule->CurrentQueueOnceEmptyOperation();
  682. NonVisualDataModule->ResetQueueOnceEmptyOperation();
  683. if ((FQueue != NULL) && !WinConfiguration->EnableQueueByDefault && !QueueChanging)
  684. {
  685. FQueue->Enabled = false;
  686. }
  687. if ((OnceDoneOperation != odoIdle) && !NonVisualDataModule->Busy)
  688. {
  689. Terminal->CloseOnCompletion(OnceDoneOperation, LoadStr(CLOSED_ON_QUEUE_EMPTY));
  690. }
  691. }
  692. }
  693. //---------------------------------------------------------------------------
  694. UnicodeString __fastcall TCustomScpExplorerForm::GetQueueProgressTitle()
  695. {
  696. UnicodeString Result;
  697. if (FQueueStatus != NULL)
  698. {
  699. if (FQueueStatus->IsOnlyOneActiveAndNoPending())
  700. {
  701. TFileOperationProgressType * ProgressData =
  702. FQueueStatus->Items[FQueueStatus->DoneCount]->ProgressData;
  703. if ((ProgressData != NULL) && ProgressData->InProgress)
  704. {
  705. Result = TProgressForm::ProgressStr(NULL, ProgressData);
  706. }
  707. }
  708. else if (FQueueStatus->ActiveAndPendingPrimaryCount > 1)
  709. {
  710. Result = FMTLOAD(PROGRESS_IN_QUEUE, (FQueueStatus->ActiveAndPendingPrimaryCount));
  711. }
  712. }
  713. return Result;
  714. }
  715. //---------------------------------------------------------------------------
  716. void __fastcall TCustomScpExplorerForm::UpdateQueueView()
  717. {
  718. ComponentVisible[fcQueueView] =
  719. !IsLocalBrowserMode() &&
  720. ((WinConfiguration->QueueView.Show == qvShow) ||
  721. ((WinConfiguration->QueueView.Show == qvHideWhenEmpty) &&
  722. (FQueueStatus != NULL) && (FQueueStatus->Count > 0)));
  723. }
  724. //---------------------------------------------------------------------------
  725. void __fastcall TCustomScpExplorerForm::QueueChanged()
  726. {
  727. if (FQueueStatus != NULL)
  728. {
  729. // UpdateFileList implementation relies on the status being recreated when session changes
  730. delete FQueueStatus;
  731. FQueueStatus = NULL;
  732. }
  733. UpdateQueueStatus(true);
  734. }
  735. //---------------------------------------------------------------------------
  736. void __fastcall TCustomScpExplorerForm::QueueListUpdate(TTerminalQueue * Queue)
  737. {
  738. if (FQueue == Queue)
  739. {
  740. FQueueStatusInvalidated = true;
  741. }
  742. }
  743. //---------------------------------------------------------------------------
  744. void __fastcall TCustomScpExplorerForm::QueueItemUpdate(TTerminalQueue * Queue,
  745. TQueueItem * Item)
  746. {
  747. if (FQueue == Queue)
  748. {
  749. TGuard Guard(FQueueStatusSection);
  750. // this may be running in parallel with QueueChanged
  751. if (FQueueStatus != NULL)
  752. {
  753. TQueueItemProxy * QueueItem = FQueueStatus->FindByQueueItem(Item);
  754. if ((Item->Status == TQueueItem::qsDone) && (Terminal != NULL))
  755. {
  756. FRefreshLocalDirectory = (QueueItem == NULL) ||
  757. (!QueueItem->Info->ModifiedLocal.IsEmpty());
  758. FRefreshRemoteDirectory = (QueueItem == NULL) ||
  759. (!QueueItem->Info->ModifiedRemote.IsEmpty());
  760. }
  761. if (QueueItem != NULL)
  762. {
  763. QueueItem->UserData = (void*)true;
  764. FQueueItemInvalidated = true;
  765. }
  766. }
  767. }
  768. }
  769. //---------------------------------------------------------------------------
  770. bool __fastcall TCustomScpExplorerForm::IsQueueAutoPopup()
  771. {
  772. // during standalone editing, we have no way to see/control queue,
  773. // so we have to always popup prompts automatically
  774. return StandaloneOperation || GUIConfiguration->QueueAutoPopup;
  775. }
  776. //---------------------------------------------------------------------------
  777. void __fastcall TCustomScpExplorerForm::RefreshQueueItems()
  778. {
  779. if (FQueueStatus != NULL)
  780. {
  781. bool QueueAutoPopup = IsQueueAutoPopup();
  782. bool NeedRefresh = FQueueController->NeedRefresh();
  783. bool Refresh = FQueueItemInvalidated || NeedRefresh;
  784. FQueueItemInvalidated = false;
  785. int Limit = Refresh ? FQueueStatus->Count : FQueueStatus->DoneAndActiveCount;
  786. bool Updated = false;
  787. TQueueItemProxy * QueueItem;
  788. bool WasUserAction;
  789. for (int Index = 0; Index < Limit; Index++)
  790. {
  791. bool Update = false;
  792. QueueItem = FQueueStatus->Items[Index];
  793. WasUserAction = TQueueItem::IsUserActionStatus(QueueItem->Status);
  794. if (!NonVisualDataModule->Busy && QueueAutoPopup && WasUserAction &&
  795. (FPendingQueueActionItem == NULL))
  796. {
  797. FPendingQueueActionItem = QueueItem;
  798. FUserActionTimer->Enabled = true;
  799. }
  800. if ((bool)QueueItem->UserData)
  801. {
  802. QueueItem->UserData = (void*)false;
  803. QueueItem->Update();
  804. Updated = true;
  805. Update = true;
  806. }
  807. else if (FQueueController->QueueItemNeedsFrequentRefresh(QueueItem) || NeedRefresh)
  808. {
  809. Update = true;
  810. }
  811. if (Update)
  812. {
  813. FQueueController->RefreshQueueItem(QueueItem);
  814. }
  815. }
  816. if (Updated)
  817. {
  818. NonVisualDataModule->UpdateNonVisibleActions();
  819. SetQueueProgress();
  820. UpdateQueueFileList();
  821. }
  822. }
  823. }
  824. //---------------------------------------------------------------------------
  825. void __fastcall TCustomScpExplorerForm::SetTaskbarListProgressState(TBPFLAG Flags)
  826. {
  827. if (FTaskbarList != NULL)
  828. {
  829. // Could now use Application->MainFormHandle, now that we implement Application->OnGetMainFormHandle
  830. FTaskbarList->SetProgressState(GetMainForm()->Handle, Flags);
  831. }
  832. }
  833. //---------------------------------------------------------------------------
  834. void __fastcall TCustomScpExplorerForm::SetTaskbarListProgressValue(int Progress)
  835. {
  836. if (Progress >= 0)
  837. {
  838. if (FTaskbarList != NULL)
  839. {
  840. FTaskbarList->SetProgressValue(GetMainForm()->Handle, Progress, 100);
  841. }
  842. }
  843. else
  844. {
  845. SetTaskbarListProgressState(TBPF_INDETERMINATE);
  846. }
  847. }
  848. //---------------------------------------------------------------------------
  849. void __fastcall TCustomScpExplorerForm::SetTaskbarListProgressValue(TFileOperationProgressType * ProgressData)
  850. {
  851. int OverallProgress;
  852. // FProgressForm is null when this is called from SetQueueProgress
  853. if ((FProgressForm != NULL) && (FProgressForm->SynchronizeProgress != NULL))
  854. {
  855. OverallProgress = FProgressForm->SynchronizeProgress->Progress(ProgressData);
  856. }
  857. else if (!ProgressData->IsIndeterminate())
  858. {
  859. OverallProgress = ProgressData->OverallProgress();
  860. }
  861. else
  862. {
  863. OverallProgress = -1;
  864. }
  865. SetTaskbarListProgressValue(OverallProgress);
  866. }
  867. //---------------------------------------------------------------------------
  868. void __fastcall TCustomScpExplorerForm::SetQueueProgress()
  869. {
  870. TTerminalManager::Instance()->QueueStatusUpdated();
  871. if ((FTaskbarList != NULL) && (FProgressForm == NULL))
  872. {
  873. // Do not animate task bar button, when items are queued, but processing is disabled
  874. if ((FQueueStatus != NULL) && (FQueueStatus->ActivePrimaryCount > 0))
  875. {
  876. if (FQueueStatus->IsOnlyOneActiveAndNoPending())
  877. {
  878. TFileOperationProgressType * ProgressData = NULL;
  879. if (FQueueStatus->Items[FQueueStatus->DoneCount] != NULL)
  880. {
  881. ProgressData = FQueueStatus->Items[FQueueStatus->DoneCount]->ProgressData;
  882. }
  883. if ((ProgressData != NULL) &&
  884. ProgressData->InProgress)
  885. {
  886. SetTaskbarListProgressValue(ProgressData);
  887. }
  888. else
  889. {
  890. SetTaskbarListProgressState(TBPF_NOPROGRESS);
  891. }
  892. }
  893. else
  894. {
  895. SetTaskbarListProgressState(TBPF_INDETERMINATE);
  896. }
  897. }
  898. else
  899. {
  900. SetTaskbarListProgressState(TBPF_NOPROGRESS);
  901. }
  902. }
  903. UpdateQueueLabel();
  904. }
  905. //---------------------------------------------------------------------------
  906. void __fastcall TCustomScpExplorerForm::UpdateQueueLabel()
  907. {
  908. UnicodeString Caption = LoadStr(QUEUE_CAPTION);
  909. if (FQueueStatus != NULL)
  910. {
  911. int ActiveAndPendingPrimaryCount = FQueueStatus->ActiveAndPendingPrimaryCount;
  912. if (ActiveAndPendingPrimaryCount > 0)
  913. {
  914. Caption = FORMAT("%s (%d)", (Caption, ActiveAndPendingPrimaryCount));
  915. }
  916. }
  917. QueueLabel->Caption = Caption;
  918. }
  919. //---------------------------------------------------------------------------
  920. void TCustomScpExplorerForm::CheckStoreTransition()
  921. {
  922. if (WinConfiguration->StoreTransition == stInit)
  923. {
  924. if (IsUWP())
  925. {
  926. WinConfiguration->StoreTransition = stStoreFresh;
  927. AppLog(L"Fresh UWP/store installation");
  928. }
  929. else
  930. {
  931. WinConfiguration->StoreTransition = stStandard;
  932. AppLog(L"Fresh standard installation");
  933. }
  934. }
  935. else if ((WinConfiguration->StoreTransition == stStandard) && IsUWP())
  936. {
  937. WinConfiguration->StoreTransition = stStoreMigrated;
  938. WinConfiguration->FirstRun = WinConfiguration->FirstRun + L"=>" + StandardDatestamp();
  939. AppLog(L"Standard installation migrated to store installation");
  940. }
  941. if (WinConfiguration->StoreTransition == stStoreMigrated)
  942. {
  943. TTBXToolbar * Toolbar = new TTBXToolbar(MessageDock);
  944. Toolbar->Parent = MessageDock;
  945. Toolbar->FullSize = true;
  946. Toolbar->Color = Application->HintColor;
  947. TTBXLabelItem * LabelItem;
  948. LabelItem = new TTBXLabelItem(Toolbar);
  949. // Convert to explicit RGB "black" color, to make sure it is contrasting even in Dark theme
  950. // (TTBXOfficeXPTheme.GetStandardColor would convert clWindow to "white" in dark theme)
  951. LabelItem->FontSettings->Color = static_cast<TColor>(GetSysColor(clWindow));
  952. LabelItem->Margin = ScaleByTextHeight(MessageDock, 2); // Is this automatically rescaled?
  953. LabelItem->Caption = LoadStr(STORE_MIGRATION_THANKS);
  954. Toolbar->Items->Add(LabelItem);
  955. TTBXCustomItem * LinkItem = new TTBXItem(Toolbar);
  956. LinkItem->FontSettings->Color = LinkColor;
  957. LinkItem->Caption = LoadStr(STORE_MIGRATION_LEARN);
  958. LinkItem->OnClick = StoreTransitionLinkClick;
  959. Toolbar->Items->Add(LinkItem);
  960. AddMenuSeparator(Toolbar->Items);
  961. TTBXCustomItem * CloseItem = new TTBXItem(Toolbar);
  962. CloseItem->FontSettings->Color = static_cast<TColor>(GetSysColor(clWindow));
  963. CloseItem->Caption = LoadStr(MESSAGE_DISMISS);
  964. CloseItem->OnClick = StoreTransitionCloseClick;
  965. Toolbar->Items->Add(CloseItem);
  966. GiveTBItemPriority(CloseItem);
  967. }
  968. }
  969. //---------------------------------------------------------------------------
  970. void __fastcall TCustomScpExplorerForm::StoreTransitionLinkClick(TObject *)
  971. {
  972. ShowHelp(HELP_STORE_TRANSITION);
  973. }
  974. //---------------------------------------------------------------------------
  975. void __fastcall TCustomScpExplorerForm::StoreTransitionCloseClick(TObject * Sender)
  976. {
  977. TTBCustomItem * Item = DebugNotNull(dynamic_cast<TTBCustomItem *>(Sender));
  978. TTBXToolbar * Toolbar = DebugNotNull(dynamic_cast<TTBXToolbar *>(DebugNotNull(DebugNotNull(Item->Parent)->ParentComponent)));
  979. delete Toolbar;
  980. WinConfiguration->StoreTransition = stStoreAcknowledged;
  981. UpdateControls();
  982. }
  983. //---------------------------------------------------------------------------
  984. void __fastcall TCustomScpExplorerForm::UpdateTransferList()
  985. {
  986. TTBXStringList * TransferList = dynamic_cast<TTBXStringList*>(
  987. static_cast<TComponent*>(GetComponent(fcTransferList)));
  988. TTBXDropDownItem * TransferDropDown = dynamic_cast<TTBXDropDownItem*>(
  989. static_cast<TComponent*>(GetComponent(fcTransferDropDown)));
  990. TransferList->Strings->BeginUpdate();
  991. try
  992. {
  993. TransferList->Strings->Assign(GUIConfiguration->CopyParamList->NameList);
  994. TransferList->Strings->Insert(0, StripHotkey(LoadStr(COPY_PARAM_DEFAULT)));
  995. TransferList->ItemIndex = GUIConfiguration->CopyParamIndex + 1;
  996. if (FTransferDropDownHint.IsEmpty())
  997. {
  998. FTransferDropDownHint = TransferDropDown->Hint;
  999. }
  1000. // this way we get name for "default" settings (COPY_PARAM_DEFAULT)
  1001. UnicodeString Name = TransferList->Strings->Strings[TransferList->ItemIndex];
  1002. TransferDropDown->Text = StripHotkey(Name);
  1003. TransferDropDown->Hint = FORMAT(L"%s|%s:\n%s",
  1004. (FTransferDropDownHint, StripHotkey(Name),
  1005. GUIConfiguration->CurrentCopyParam.GetInfoStr(L"; ",
  1006. FLAGMASK(HasActiveTerminal(), Terminal->UsableCopyParamAttrs(0).General))));
  1007. // update the label, otherwise when it is updated only on the first draw
  1008. // of the list, it is drawn "bold" for some reason
  1009. FTransferListHoverIndex = TransferList->ItemIndex;
  1010. UpdateTransferLabel();
  1011. }
  1012. __finally
  1013. {
  1014. TransferList->Strings->EndUpdate();
  1015. }
  1016. }
  1017. //---------------------------------------------------------------------------
  1018. void __fastcall TCustomScpExplorerForm::UpdateCustomCommandsToolbar()
  1019. {
  1020. TTBXToolbar * Toolbar = dynamic_cast<TTBXToolbar *>(GetComponent(fcCustomCommandsBand));
  1021. DebugAssert(Toolbar != NULL);
  1022. NonVisualDataModule->UpdateCustomCommandsToolbar(Toolbar);
  1023. }
  1024. //---------------------------------------------------------------------------
  1025. void __fastcall TCustomScpExplorerForm::UpdateActions()
  1026. {
  1027. TForm::UpdateActions();
  1028. if (ComponentVisible[fcCustomCommandsBand])
  1029. {
  1030. UpdateCustomCommandsToolbar();
  1031. }
  1032. }
  1033. //---------------------------------------------------------------------------
  1034. void __fastcall TCustomScpExplorerForm::UpdateSessionsPageControlHeight()
  1035. {
  1036. SessionsPageControl->Height = SessionsPageControl->GetTabsHeight();
  1037. }
  1038. //---------------------------------------------------------------------------
  1039. void __fastcall TCustomScpExplorerForm::UpdateRowSelect(TCustomDirView * DirView)
  1040. {
  1041. if (DirView->RowSelect != WinConfiguration->FullRowSelect)
  1042. {
  1043. DirView->RowSelect = WinConfiguration->FullRowSelect;
  1044. // Without this, the panel turns gray and unused part of header turns black
  1045. DirView->Perform(CM_COLORCHANGED, 0, 0);
  1046. }
  1047. }
  1048. //---------------------------------------------------------------------------
  1049. void __fastcall TCustomScpExplorerForm::ConfigurationChanged()
  1050. {
  1051. Color = GetBtnFaceColor();
  1052. DebugAssert(Configuration && RemoteDirView);
  1053. RemoteDirView->DDAllowMove = !WinConfiguration->DDDisableMove;
  1054. RemoteDirView->DimmHiddenFiles = WinConfiguration->DimmHiddenFiles;
  1055. RemoteDirView->ShowHiddenFiles = WinConfiguration->ShowHiddenFiles;
  1056. RemoteDirView->FormatSizeBytes = WinConfiguration->FormatSizeBytes;
  1057. RemoteDirView->ShowInaccesibleDirectories = WinConfiguration->ShowInaccesibleDirectories;
  1058. RemoteDirView->NaturalOrderNumericalSorting = WinConfiguration->NaturalOrderNumericalSorting;
  1059. RemoteDirView->AlwaysSortDirectoriesByName = WinConfiguration->AlwaysSortDirectoriesByName;
  1060. UpdateRowSelect(RemoteDirView);
  1061. RemoteDriveView->DDAllowMove = !WinConfiguration->DDDisableMove;
  1062. RemoteDriveView->DimmHiddenDirs = WinConfiguration->DimmHiddenFiles;
  1063. RemoteDriveView->ShowHiddenDirs = WinConfiguration->ShowHiddenFiles;
  1064. RemoteDriveView->ShowInaccesibleDirectories = WinConfiguration->ShowInaccesibleDirectories;
  1065. RemoteDriveView->NaturalOrderNumericalSorting = WinConfiguration->NaturalOrderNumericalSorting;
  1066. UpdateSessionsPageControlHeight();
  1067. SetDockAllowDrag(!WinConfiguration->LockToolbars);
  1068. UpdateToolbarDisplayMode();
  1069. UpdateTransferList();
  1070. if (Terminal != NULL)
  1071. {
  1072. TransferPresetAutoSelect();
  1073. }
  1074. UpdateQueueView();
  1075. UpdateControls();
  1076. // this can be called even before constuctor finishes.
  1077. if (FEditorManager != NULL)
  1078. {
  1079. FEditorManager->ProcessFiles(FileConfigurationChanged, NULL);
  1080. }
  1081. // this can be called even before constuctor finishes.
  1082. if (FLocalEditors != NULL)
  1083. {
  1084. for (int Index = 0; Index < FLocalEditors->Count; Index++)
  1085. {
  1086. ReconfigureEditorForm(static_cast<TForm *>(FLocalEditors->Items[Index]));
  1087. }
  1088. }
  1089. if (ComponentVisible[fcCustomCommandsBand])
  1090. {
  1091. UpdateCustomCommandsToolbar();
  1092. }
  1093. // show only when keeping queue items forever,
  1094. // otherwise, it's enough to have it in the context menu
  1095. QueueDeleteAllDoneQueueToolbarItem->Visible =
  1096. WinConfiguration->QueueKeepDoneItems && (WinConfiguration->QueueKeepDoneItemsFor < 0);
  1097. if (FFileColorsCurrent != WinConfiguration->FileColors)
  1098. {
  1099. TFileColorData::LoadList(WinConfiguration->FileColors, FFileColors);
  1100. FFileColorsCurrent = WinConfiguration->FileColors;
  1101. FileColorsChanged();
  1102. }
  1103. }
  1104. //---------------------------------------------------------------------------
  1105. void __fastcall TCustomScpExplorerForm::DoFileColorsChanged(TCustomDirView * DirView)
  1106. {
  1107. DirView->OnGetItemColor = FFileColors.empty() ? TDirViewGetItemColorEvent(NULL) : TDirViewGetItemColorEvent(&DirViewGetItemColor);
  1108. DirView->Invalidate();
  1109. }
  1110. //---------------------------------------------------------------------------
  1111. void __fastcall TCustomScpExplorerForm::FileColorsChanged()
  1112. {
  1113. DoFileColorsChanged(RemoteDirView);
  1114. }
  1115. //---------------------------------------------------------------------------
  1116. void __fastcall TCustomScpExplorerForm::FileConfigurationChanged(
  1117. const UnicodeString FileName, TEditedFileData * /*Data*/, TObject * Token,
  1118. void * /*Arg*/)
  1119. {
  1120. if (Token != NULL)
  1121. {
  1122. TForm * Editor = dynamic_cast<TForm*>(Token);
  1123. DebugAssert(Editor != NULL);
  1124. ReconfigureEditorForm(Editor);
  1125. }
  1126. }
  1127. //---------------------------------------------------------------------------
  1128. void __fastcall TCustomScpExplorerForm::EnableDDTransferConfirmation(TObject * /*Sender*/)
  1129. {
  1130. WinConfiguration->DDTransferConfirmation = asOn;
  1131. }
  1132. //---------------------------------------------------------------------------
  1133. void __fastcall TCustomScpExplorerForm::CopyParamDialogAfter(
  1134. TTransferDirection /*Direction*/, bool /*Temp*/, const UnicodeString & /*TargetDirectory*/)
  1135. {
  1136. // noop
  1137. }
  1138. //---------------------------------------------------------------------------
  1139. bool TCustomScpExplorerForm::GetDoNotShowCopyDialogDefault(bool DragDrop)
  1140. {
  1141. return DragDrop && (WinConfiguration->DDTransferConfirmation == asAuto);
  1142. }
  1143. //---------------------------------------------------------------------------
  1144. void TCustomScpExplorerForm::HandleDoNotShowCopyDialogAgain(bool DragDrop, bool DoNotShowAgain)
  1145. {
  1146. if (DoNotShowAgain)
  1147. {
  1148. if (DragDrop)
  1149. {
  1150. if (WinConfiguration->DDTransferConfirmation == asAuto)
  1151. {
  1152. PopupTrayBalloon(
  1153. NULL, LoadStr(DD_TRANSFER_CONFIRM_OFF2), qtInformation, NULL, 0, EnableDDTransferConfirmation, NULL);
  1154. }
  1155. WinConfiguration->DDTransferConfirmation = asOff;
  1156. }
  1157. else
  1158. {
  1159. WinConfiguration->ConfirmTransferring = false;
  1160. }
  1161. }
  1162. else
  1163. {
  1164. // User explicitly unchecked "do not show again",
  1165. // so show him the dialog the next time
  1166. if (DragDrop && (WinConfiguration->DDTransferConfirmation == asAuto))
  1167. {
  1168. WinConfiguration->DDTransferConfirmation = asOn;
  1169. }
  1170. }
  1171. }
  1172. //---------------------------------------------------------------------------
  1173. bool __fastcall TCustomScpExplorerForm::CopyParamDialog(
  1174. TTransferDirection Direction, TTransferType Type, bool Temp,
  1175. TStrings * FileList, UnicodeString & TargetDirectory, TGUICopyParamType & CopyParam,
  1176. bool Confirm, bool DragDrop, int Options)
  1177. {
  1178. bool Result = true;
  1179. DebugAssert(HasActiveTerminal());
  1180. // these parameters are known in advance
  1181. int Params =
  1182. FLAGMASK(Type == ttMove, cpDelete);
  1183. bool ToTemp = (Temp && (Direction == tdToLocal));
  1184. if (Result && Confirm && WinConfiguration->ConfirmTransferring)
  1185. {
  1186. bool DisableNewerOnly =
  1187. (!Terminal->IsCapable[fcNewerOnlyUpload] && (Direction == tdToRemote)) ||
  1188. ToTemp;
  1189. Options |=
  1190. FLAGMASK(ToTemp, coTemp) |
  1191. FLAGMASK(!Terminal->IsCapable[fcBackgroundTransfers], coDisableQueue) |
  1192. coDoNotShowAgain;
  1193. TUsableCopyParamAttrs UsableCopyParamAttrs = Terminal->UsableCopyParamAttrs(Params);
  1194. int CopyParamAttrs = (Direction == tdToRemote ?
  1195. UsableCopyParamAttrs.Upload : UsableCopyParamAttrs.Download) |
  1196. FLAGMASK(DisableNewerOnly, cpaNoNewerOnly);
  1197. int OutputOptions = FLAGMASK(GetDoNotShowCopyDialogDefault(DragDrop), cooDoNotShowAgain);
  1198. std::unique_ptr<TSessionData> SessionData(SessionDataForCode());
  1199. FlashOnBackground(); // Particularly when called from ClipboardFakeCreated
  1200. Result = DoCopyDialog(Direction == tdToRemote, Type == ttMove,
  1201. FileList, TargetDirectory, &CopyParam, Options, CopyParamAttrs, SessionData.get(), &OutputOptions, -1);
  1202. if (Result)
  1203. {
  1204. HandleDoNotShowCopyDialogAgain(DragDrop, FLAGSET(OutputOptions, cooDoNotShowAgain));
  1205. CopyParamDialogAfter(Direction, Temp, TargetDirectory);
  1206. }
  1207. }
  1208. if (Result && CopyParam.Queue && !ToTemp && Terminal->IsCapable[fcBackgroundTransfers])
  1209. {
  1210. Configuration->Usage->Inc(L"TransfersOnBackground");
  1211. // these parameter are known only after transfer dialog
  1212. Params |=
  1213. (CopyParam.QueueNoConfirmation ? cpNoConfirmation : 0);
  1214. AddQueueItem(Queue, Direction, FileList, TargetDirectory, CopyParam, Params);
  1215. Result = false;
  1216. ClearTransferSourceSelection(Direction);
  1217. }
  1218. return Result;
  1219. }
  1220. //---------------------------------------------------------------------------
  1221. void TCustomScpExplorerForm::ClearOperationSelection(TOperationSide Side)
  1222. {
  1223. if (FOnFileOperationFinished != NULL)
  1224. {
  1225. FOnFileOperationFinished(Side, UnicodeString(), true);
  1226. }
  1227. else
  1228. {
  1229. if (HasDirView[Side])
  1230. {
  1231. DirView(Side)->SelectAll(smNone);
  1232. }
  1233. }
  1234. }
  1235. //---------------------------------------------------------------------------
  1236. void __fastcall TCustomScpExplorerForm::ClearTransferSourceSelection(TTransferDirection Direction)
  1237. {
  1238. TOperationSide Side = ((Direction == tdToRemote) ? osLocal : osRemote);
  1239. ClearOperationSelection(Side);
  1240. }
  1241. //---------------------------------------------------------------------------
  1242. void __fastcall TCustomScpExplorerForm::AddQueueItem(
  1243. TTerminalQueue * Queue, TTransferDirection Direction, TStrings * FileList,
  1244. const UnicodeString TargetDirectory, TGUICopyParamType & CopyParam,
  1245. int Params)
  1246. {
  1247. DebugAssert(Queue != NULL);
  1248. bool SingleFile = false;
  1249. if (FileList->Count == 1)
  1250. {
  1251. if (Direction == tdToRemote)
  1252. {
  1253. UnicodeString FileName = FileList->Strings[0];
  1254. SingleFile = FileExists(ApiPath(FileName));
  1255. }
  1256. else
  1257. {
  1258. TRemoteFile * File = static_cast<TRemoteFile *>(FileList->Objects[0]);
  1259. SingleFile = !File->IsDirectory;
  1260. }
  1261. }
  1262. TQueueItem * QueueItem;
  1263. if (Direction == tdToRemote)
  1264. {
  1265. CopyParam.IncludeFileMask.SetRoots(FileList, TargetDirectory);
  1266. QueueItem = new TUploadQueueItem(Terminal, FileList, TargetDirectory,
  1267. &CopyParam, Params, SingleFile, CopyParam.QueueParallel);
  1268. }
  1269. else
  1270. {
  1271. CopyParam.IncludeFileMask.SetRoots(TargetDirectory, FileList);
  1272. QueueItem = new TDownloadQueueItem(Terminal, FileList, TargetDirectory,
  1273. &CopyParam, Params, SingleFile, CopyParam.QueueParallel);
  1274. }
  1275. AddQueueItem(Queue, QueueItem, Terminal);
  1276. }
  1277. //---------------------------------------------------------------------------
  1278. void __fastcall TCustomScpExplorerForm::AddQueueItem(TTerminalQueue * Queue, TQueueItem * QueueItem, TManagedTerminal * Terminal)
  1279. {
  1280. if (Queue->IsEmpty)
  1281. {
  1282. Terminal->QueueOperationStart = Now();
  1283. }
  1284. Queue->AddItem(QueueItem);
  1285. }
  1286. //---------------------------------------------------------------------------
  1287. void __fastcall TCustomScpExplorerForm::RestoreFormParams()
  1288. {
  1289. }
  1290. //---------------------------------------------------------------------------
  1291. void __fastcall TCustomScpExplorerForm::InitControls()
  1292. {
  1293. DebugAssert(FDocks != NULL);
  1294. for (int Index = 0; Index < ComponentCount; Index++)
  1295. {
  1296. TTBDock * Dock = dynamic_cast<TTBDock *>(Components[Index]);
  1297. if ((Dock != NULL) && (Dock->Tag == 0))
  1298. {
  1299. FDocks->Add(Dock);
  1300. }
  1301. }
  1302. CollectItemsWithTextDisplayMode(this);
  1303. }
  1304. //---------------------------------------------------------------------------
  1305. void __fastcall TCustomScpExplorerForm::RestoreParams()
  1306. {
  1307. ConfigurationChanged();
  1308. QueuePanel->Height =
  1309. LoadDimension(WinConfiguration->QueueView.Height, WinConfiguration->QueueView.HeightPixelsPerInch, this);
  1310. LoadListViewStr(QueueView3, WinConfiguration->QueueView.Layout);
  1311. QueueDock->Visible = WinConfiguration->QueueView.ToolBar;
  1312. QueueLabel->Visible = WinConfiguration->QueueView.Label;
  1313. QueueFileList->Visible = WinConfiguration->QueueView.FileList;
  1314. QueueFileList->Height =
  1315. LoadDimension(WinConfiguration->QueueView.FileListHeight, WinConfiguration->QueueView.FileListHeightPixelsPerInch, this);
  1316. if (QueueFileList->Visible)
  1317. {
  1318. AdjustQueueLayout();
  1319. }
  1320. UpdateCustomCommandsToolbar();
  1321. }
  1322. //---------------------------------------------------------------------------
  1323. void __fastcall TCustomScpExplorerForm::StoreParams()
  1324. {
  1325. int APixelsPerInch = GetControlPixelsPerInch(this);
  1326. WinConfiguration->QueueView.Height = QueuePanel->Height;
  1327. WinConfiguration->QueueView.HeightPixelsPerInch = APixelsPerInch;
  1328. WinConfiguration->QueueView.Layout = GetListViewStr(QueueView3);
  1329. WinConfiguration->QueueView.ToolBar = QueueDock->Visible;
  1330. WinConfiguration->QueueView.Label = QueueLabel->Visible;
  1331. WinConfiguration->QueueView.FileList = QueueFileList->Visible;
  1332. WinConfiguration->QueueView.FileListHeight = QueueFileList->Height;
  1333. WinConfiguration->QueueView.FileListHeightPixelsPerInch = APixelsPerInch;
  1334. }
  1335. //---------------------------------------------------------------------------
  1336. void __fastcall TCustomScpExplorerForm::CreateParams(TCreateParams & Params)
  1337. {
  1338. if (!FFormRestored)
  1339. {
  1340. FFormRestored = true;
  1341. RestoreFormParams();
  1342. }
  1343. TForm::CreateParams(Params);
  1344. }
  1345. //---------------------------------------------------------------------------
  1346. void __fastcall TCustomScpExplorerForm::SetDockAllowDrag(bool value)
  1347. {
  1348. DebugAssert(FDocks != NULL);
  1349. for (int Index = 0; Index < FDocks->Count; Index++)
  1350. {
  1351. static_cast<TTBDock*>(FDocks->Items[Index])->AllowDrag = value;
  1352. }
  1353. }
  1354. //---------------------------------------------------------------------------
  1355. void __fastcall TCustomScpExplorerForm::LoadToolbarsLayoutStr(UnicodeString LayoutStr, UnicodeString ButtonsStr)
  1356. {
  1357. SetDockAllowDrag(true);
  1358. while (!ButtonsStr.IsEmpty())
  1359. {
  1360. UnicodeString S = CutToChar(ButtonsStr, L';', true);
  1361. UnicodeString Name = CutToChar(S, L'=', true);
  1362. for (int ToolbarIndex = 0; ToolbarIndex < ComponentCount; ToolbarIndex++)
  1363. {
  1364. TTBCustomToolbar * Toolbar = dynamic_cast<TTBCustomToolbar *>(Components[ToolbarIndex]);
  1365. if ((Toolbar != NULL) && SameText(Name, GetToolbarKey(Toolbar->Name)))
  1366. {
  1367. while (!S.IsEmpty())
  1368. {
  1369. UnicodeString S2 = CutToChar(S, L',', true);
  1370. UnicodeString Name = CutToChar(S2, L':', true);
  1371. for (int ItemIndex = 0; ItemIndex < Toolbar->Items->Count; ItemIndex++)
  1372. {
  1373. TTBCustomItem * Item = Toolbar->Items->Items[ItemIndex];
  1374. if (SameText(GetToolbarItemName(Item), Name))
  1375. {
  1376. // forward compatibility
  1377. UnicodeString S3 = CutToChar(S2, L':', true);
  1378. bool Visible = StrToIntDef(S3, 0);
  1379. Item->Visible = Visible;
  1380. }
  1381. }
  1382. }
  1383. }
  1384. }
  1385. }
  1386. ::LoadToolbarsLayoutStr(this, LayoutStr);
  1387. SetDockAllowDrag(!WinConfiguration->LockToolbars);
  1388. }
  1389. //---------------------------------------------------------------------------
  1390. UnicodeString __fastcall TCustomScpExplorerForm::GetToolbarsLayoutStr()
  1391. {
  1392. UnicodeString Result;
  1393. SetDockAllowDrag(true);
  1394. Result = ::GetToolbarsLayoutStr(this);
  1395. SetDockAllowDrag(!WinConfiguration->LockToolbars);
  1396. return Result;
  1397. }
  1398. //---------------------------------------------------------------------------
  1399. UnicodeString __fastcall TCustomScpExplorerForm::GetToolbarItemName(TTBCustomItem * Item)
  1400. {
  1401. UnicodeString Result;
  1402. if (Item->Action != NULL)
  1403. {
  1404. Result = Item->Action->Name;
  1405. Result = RemoveSuffix(Result, L"Action", true);
  1406. }
  1407. else
  1408. {
  1409. Result = Item->Name;
  1410. Result = RemoveSuffix(Result, L"SubmenuItem", true);
  1411. Result = RemoveSuffix(Result, L"Item", true);
  1412. }
  1413. return Result;
  1414. }
  1415. //---------------------------------------------------------------------------
  1416. UnicodeString __fastcall TCustomScpExplorerForm::GetToolbarsButtonsStr()
  1417. {
  1418. UnicodeString Result;
  1419. for (int ToolbarIndex = 0; ToolbarIndex < ComponentCount; ToolbarIndex++)
  1420. {
  1421. UnicodeString ToolbarStr;
  1422. TTBCustomToolbar * Toolbar = dynamic_cast<TTBCustomToolbar *>(Components[ToolbarIndex]);
  1423. if ((Toolbar != NULL) && (Toolbar != QueueToolbar))
  1424. {
  1425. for (int ItemIndex = 0; ItemIndex < Toolbar->Items->Count; ItemIndex++)
  1426. {
  1427. TTBCustomItem * Item = Toolbar->Items->Items[ItemIndex];
  1428. // Currently all buttons are visible by default, so we can safely remember all hidden buttons.
  1429. // Once we introduce any buttons that are hidden by default, we would have to remember their initial state
  1430. // and save the changes here only.
  1431. if (NonVisualDataModule->IsCustomizableToolbarItem(Item) && !Item->Visible)
  1432. {
  1433. UnicodeString Name = GetToolbarItemName(Item);
  1434. DebugAssert(Name.Pos(L"TBX") == 0);
  1435. AddToList(ToolbarStr, FORMAT(L"%s:0", (Name)), L",");
  1436. }
  1437. }
  1438. }
  1439. if (!ToolbarStr.IsEmpty())
  1440. {
  1441. AddToList(Result, FORMAT(L"%s=%s", (GetToolbarKey(Toolbar->Name), ToolbarStr)), L";");
  1442. }
  1443. }
  1444. return Result;
  1445. }
  1446. //---------------------------------------------------------------------------
  1447. void __fastcall TCustomScpExplorerForm::CreateProgressForm(TSynchronizeProgress * SynchronizeProgress)
  1448. {
  1449. DebugAssert(FProgressForm == NULL);
  1450. bool AllowSkip = HasActiveTerminal() ? Terminal->IsCapable[fcSkipTransfer] : false;
  1451. bool AllowMoveToQueue = (FTransferResumeList != NULL) || (FDeletedFiles != NULL);
  1452. FProgressForm = new TProgressForm(Application, AllowMoveToQueue, AllowSkip, SynchronizeProgress);
  1453. FProgressForm->DeleteLocalToRecycleBin =
  1454. (WinConfiguration->DeleteToRecycleBin != FAlternativeDelete);
  1455. FProgressForm->DeleteRemoteToRecycleBin =
  1456. HasActiveTerminal() &&
  1457. (Terminal->SessionData->DeleteToRecycleBin != FAlternativeDelete) &&
  1458. !Terminal->SessionData->RecycleBinPath.IsEmpty();
  1459. // As progress window has delayed show now, we need to lock ourselves, (at least) until then
  1460. LockWindow();
  1461. // Actually, do not know what hides the progress once the operation finishes
  1462. // (it is possibly the SetQueueProgress - and we should not rely on that)
  1463. SetTaskbarListProgressState(TBPF_NORMAL);
  1464. }
  1465. //---------------------------------------------------------------------------
  1466. void __fastcall TCustomScpExplorerForm::DestroyProgressForm()
  1467. {
  1468. SAFE_DESTROY(FProgressForm);
  1469. UnlockWindow();
  1470. SetQueueProgress();
  1471. }
  1472. //---------------------------------------------------------------------------
  1473. void __fastcall TCustomScpExplorerForm::FileOperationProgress(
  1474. TFileOperationProgressType & ProgressData)
  1475. {
  1476. // Download to temporary local directory
  1477. if (FPendingTempSpaceWarn && ProgressData.InProgress && ProgressData.TotalSizeSet)
  1478. {
  1479. bool Continue = true;
  1480. FPendingTempSpaceWarn = false;
  1481. DoWarnLackOfTempSpace(ProgressData.Directory, ProgressData.TotalSize, Continue);
  1482. if (!Continue)
  1483. {
  1484. Abort();
  1485. }
  1486. }
  1487. // operation is being executed and we still didn't show up progress form
  1488. if (ProgressData.InProgress && !FProgressForm)
  1489. {
  1490. CreateProgressForm(NULL);
  1491. DebugAssert(FProgressForm->OnceDoneOperation == odoIdle);
  1492. FProgressForm->OnceDoneOperation = ProgressData.InitialOnceDoneOperation;
  1493. }
  1494. // operation is finished (or terminated), so we hide progress form
  1495. else if (!ProgressData.InProgress && (FProgressForm != NULL) && (FProgressForm->SynchronizeProgress == NULL))
  1496. {
  1497. DestroyProgressForm();
  1498. if (ProgressData.Operation == foCalculateSize)
  1499. {
  1500. // When calculating size before transfer, the abort caused by
  1501. // cancel flag set due to "MoveToQueue" below,
  1502. // is not propagated back to us in ExecuteFileOperation,
  1503. // as it is silently swallowed.
  1504. // So we explicitly (re)throw it here.
  1505. if (FMoveToQueue)
  1506. {
  1507. Abort();
  1508. }
  1509. }
  1510. else
  1511. {
  1512. if ((ProgressData.Cancel == csContinue) &&
  1513. !FAutoOperation)
  1514. {
  1515. OperationComplete(ProgressData.StartTime);
  1516. }
  1517. }
  1518. }
  1519. if (FProgressForm &&
  1520. // Can happen only during synchronization, when ending one of the operations
  1521. ProgressData.InProgress)
  1522. {
  1523. FProgressForm->SetProgressData(ProgressData);
  1524. DebugAssert(ProgressData.InProgress);
  1525. SetTaskbarListProgressValue(&ProgressData);
  1526. if (FProgressForm->Cancel > csContinue)
  1527. {
  1528. ProgressData.SetCancelAtLeast(FProgressForm->Cancel);
  1529. if (FProgressForm->Cancel == csCancelFile)
  1530. {
  1531. FProgressForm->ClearCancel();
  1532. }
  1533. // cancel cancels even the move
  1534. FMoveToQueue = false;
  1535. }
  1536. else if (FProgressForm->MoveToQueue)
  1537. {
  1538. FMoveToQueue = true;
  1539. ProgressData.SetCancelAtLeast(csCancel);
  1540. }
  1541. if ((FTransferResumeList != NULL) &&
  1542. ProgressData.InProgress &&
  1543. ProgressData.IsTransfer() &&
  1544. !ProgressData.FullFileName.IsEmpty())
  1545. {
  1546. if ((FTransferResumeList->Count == 0) ||
  1547. (FTransferResumeList->Strings[FTransferResumeList->Count - 1] != ProgressData.FullFileName))
  1548. {
  1549. // note that we do not recognize directories from files here
  1550. FTransferResumeList->Add(ProgressData.FullFileName);
  1551. }
  1552. }
  1553. }
  1554. }
  1555. //---------------------------------------------------------------------------
  1556. void __fastcall TCustomScpExplorerForm::OperationComplete(
  1557. const TDateTime & StartTime)
  1558. {
  1559. if (GUIConfiguration->BeepOnFinish &&
  1560. (Now() - StartTime > GUIConfiguration->BeepOnFinishAfter))
  1561. {
  1562. UnicodeString BeepSound = GUIConfiguration->BeepSound;
  1563. DWORD Sound;
  1564. if (!ExtractFileExt(BeepSound).IsEmpty())
  1565. {
  1566. Sound = SND_FILENAME;
  1567. }
  1568. else
  1569. {
  1570. Sound = SND_ALIAS;
  1571. }
  1572. PlaySound(BeepSound.c_str(), NULL, Sound | SND_ASYNC);
  1573. }
  1574. }
  1575. //---------------------------------------------------------------------------
  1576. void __fastcall TCustomScpExplorerForm::OperationProgress(
  1577. TFileOperationProgressType & ProgressData)
  1578. {
  1579. // For example, when calling TFileOperationProgressType::Suspend in TTerminal::QueryReopen,
  1580. // we cannot recurse back to TTerminal::Idle as that may detect another error, calling "suspend" again.
  1581. TAutoNestingCounter Counter(FDoNotIdleCurrentTerminal);
  1582. FileOperationProgress(ProgressData);
  1583. }
  1584. //---------------------------------------------------------------------------
  1585. UnicodeString __fastcall TCustomScpExplorerForm::GetProgressTitle()
  1586. {
  1587. UnicodeString Result;
  1588. if (FProgressForm != NULL)
  1589. {
  1590. Result = FProgressForm->ProgressStr();
  1591. }
  1592. return Result;
  1593. }
  1594. //---------------------------------------------------------------------------
  1595. bool __fastcall TCustomScpExplorerForm::PanelOperation(TOperationSide /*Side*/,
  1596. bool DragDrop)
  1597. {
  1598. return (!DragDrop && (DropSourceControl == NULL)) ||
  1599. (DropSourceControl == DirView(osOther));
  1600. }
  1601. //---------------------------------------------------------------------------
  1602. TListItem * TCustomScpExplorerForm::VisualiseOperationFinished(TOperationSide Side, const UnicodeString & FileName, bool Unselect)
  1603. {
  1604. TCustomDirView * ADirView = DirView(Side);
  1605. UnicodeString FileNameOnly = ExtractFileName(FileName, !IsSideLocalBrowser(Side));
  1606. TListItem * Item = ADirView->FindFileItem(FileNameOnly);
  1607. // this can happen when local drive is unplugged in the middle of the operation
  1608. if (Item != NULL)
  1609. {
  1610. if (Unselect)
  1611. {
  1612. Item->Selected = false;
  1613. }
  1614. ADirView->MakeProgressVisible(Item);
  1615. }
  1616. return Item;
  1617. }
  1618. //---------------------------------------------------------------------------
  1619. void __fastcall TCustomScpExplorerForm::DoOperationFinished(
  1620. TFileOperation Operation, TOperationSide Side,
  1621. bool /*Temp*/, const UnicodeString & FileName, bool Success,
  1622. TOnceDoneOperation & OnceDoneOperation)
  1623. {
  1624. if (!FAutoOperation)
  1625. {
  1626. // no selection on "/upload", form serves only as event handler
  1627. // (it is not displayed)
  1628. if (PanelOperation(Side, FDragDropOperation) && Visible &&
  1629. ((Operation == FPrimaryOperation) ||
  1630. ((Operation != foCalculateSize) && (Operation != foGetProperties) && (Operation != foCalculateChecksum))))
  1631. {
  1632. TOperationSide DViewSide;
  1633. if ((FProgressSide != osCurrent) &&
  1634. IsLocalBrowserMode() && (Side == osLocal)) // Only to limit the impact
  1635. {
  1636. // assume the operation is over the focused panel
  1637. DViewSide = FProgressSide;
  1638. }
  1639. else
  1640. {
  1641. DViewSide = Side;
  1642. }
  1643. if (FOnFileOperationFinished != NULL)
  1644. {
  1645. FOnFileOperationFinished(DViewSide, FileName, Success);
  1646. }
  1647. else
  1648. {
  1649. VisualiseOperationFinished(DViewSide, FileName, Success);
  1650. }
  1651. }
  1652. if (TFileOperationProgressType::IsTransferOperation(Operation))
  1653. {
  1654. if (DebugAlwaysTrue(!IsLocalBrowserMode()))
  1655. {
  1656. if (Side == osLocal)
  1657. {
  1658. Configuration->Usage->Inc(L"UploadedFiles");
  1659. }
  1660. else
  1661. {
  1662. Configuration->Usage->Inc(L"DownloadedFiles");
  1663. }
  1664. }
  1665. }
  1666. }
  1667. if (Success && (FSynchronizeController != NULL))
  1668. {
  1669. DebugAssert(!IsSideLocalBrowser(osRemote));
  1670. if (Operation == foCopy)
  1671. {
  1672. DebugAssert(Side == osLocal);
  1673. FSynchronizeController->LogOperation(soUpload, FileName);
  1674. }
  1675. else if (Operation == foDelete)
  1676. {
  1677. DebugAssert(Side == osRemote);
  1678. FSynchronizeController->LogOperation(soDelete, FileName);
  1679. }
  1680. }
  1681. if (FProgressForm)
  1682. {
  1683. OnceDoneOperation = FProgressForm->OnceDoneOperation;
  1684. }
  1685. }
  1686. //---------------------------------------------------------------------------
  1687. void __fastcall TCustomScpExplorerForm::OperationFinished(
  1688. TFileOperation Operation, TOperationSide Side,
  1689. bool Temp, const UnicodeString & FileName, Boolean Success,
  1690. TOnceDoneOperation & OnceDoneOperation)
  1691. {
  1692. TAutoNestingCounter Counter(FDoNotIdleCurrentTerminal);
  1693. DoOperationFinished(Operation, Side, Temp, FileName, Success,
  1694. OnceDoneOperation);
  1695. }
  1696. //---------------------------------------------------------------------------
  1697. bool TCustomScpExplorerForm::IsLocalBrowserMode()
  1698. {
  1699. return false;
  1700. }
  1701. //---------------------------------------------------------------------------
  1702. bool TCustomScpExplorerForm::SupportsLocalBrowser()
  1703. {
  1704. return false;
  1705. }
  1706. //---------------------------------------------------------------------------
  1707. bool TCustomScpExplorerForm::IsSideLocalBrowser(TOperationSide)
  1708. {
  1709. return false;
  1710. }
  1711. //---------------------------------------------------------------------------
  1712. TCustomDirView * TCustomScpExplorerForm::GetCurrentLocalBrowser()
  1713. {
  1714. return NULL;
  1715. }
  1716. //---------------------------------------------------------------------------
  1717. TCustomDirView * __fastcall TCustomScpExplorerForm::DirView(TOperationSide Side)
  1718. {
  1719. DebugAssert(GetSide(Side) == osRemote);
  1720. DebugUsedParam(Side);
  1721. return RemoteDirView;
  1722. }
  1723. //---------------------------------------------------------------------------
  1724. TCustomDriveView * __fastcall TCustomScpExplorerForm::DriveView(TOperationSide Side)
  1725. {
  1726. DebugAssert(GetSide(Side) == osRemote);
  1727. DebugUsedParam(Side);
  1728. return RemoteDriveView;
  1729. }
  1730. //---------------------------------------------------------------------------
  1731. bool __fastcall TCustomScpExplorerForm::DirViewEnabled(TOperationSide Side)
  1732. {
  1733. DebugAssert(GetSide(Side) == osRemote);
  1734. DebugUsedParam(Side);
  1735. return HasActiveTerminal();
  1736. }
  1737. //---------------------------------------------------------------------------
  1738. bool __fastcall TCustomScpExplorerForm::GetEnableFocusedOperation(
  1739. TOperationSide Side, int FilesOnly)
  1740. {
  1741. return DirView(Side)->AnyFileSelected(true, (FilesOnly != 0), true);
  1742. }
  1743. //---------------------------------------------------------------------------
  1744. bool __fastcall TCustomScpExplorerForm::GetEnableSelectedOperation(
  1745. TOperationSide Side, int FilesOnly)
  1746. {
  1747. return DirView(Side)->AnyFileSelected(false, (FilesOnly != 0), true);
  1748. }
  1749. //---------------------------------------------------------------------------
  1750. struct THistoryItemData
  1751. {
  1752. short int Side;
  1753. short int Index;
  1754. };
  1755. //---------------------------------------------------------------------------
  1756. void __fastcall TCustomScpExplorerForm::HistoryGo(TOperationSide Side, int Index)
  1757. {
  1758. DirView(Side)->HistoryGo(Index);
  1759. }
  1760. //---------------------------------------------------------------------------
  1761. void __fastcall TCustomScpExplorerForm::HistoryItemClick(System::TObject* Sender)
  1762. {
  1763. TTBCustomItem * Item = dynamic_cast<TTBCustomItem *>(Sender);
  1764. THistoryItemData Data = *reinterpret_cast<THistoryItemData*>(&(Item->Tag));
  1765. HistoryGo((TOperationSide)Data.Side, Data.Index);
  1766. }
  1767. //---------------------------------------------------------------------------
  1768. void __fastcall TCustomScpExplorerForm::UpdateHistoryMenu(TOperationSide Side,
  1769. bool Back)
  1770. {
  1771. if (FHistoryMenu[Side == osLocal][Back] != NULL)
  1772. {
  1773. TCustomDirView * DView = DirView(Side);
  1774. TTBXPopupMenu * Menu = FHistoryMenu[Side == osLocal][Back];
  1775. int ICount = Back ? DView->BackCount : DView->ForwardCount;
  1776. if (ICount > 10)
  1777. {
  1778. ICount = 10;
  1779. }
  1780. Menu->Items->Clear();
  1781. THistoryItemData Data;
  1782. Data.Side = Side;
  1783. for (short int i = 1; i <= ICount; i++)
  1784. {
  1785. TTBCustomItem * Item = new TTBXItem(Menu);
  1786. Data.Index = static_cast<short int>(i * (Back ? -1 : 1));
  1787. Item->Caption = MinimizeName(DView->HistoryPath[Data.Index], 50, !IsSideLocalBrowser(Side));
  1788. Item->Hint = DView->HistoryPath[Data.Index];
  1789. DebugAssert(sizeof(int) == sizeof(THistoryItemData));
  1790. Item->Tag = *reinterpret_cast<int*>(&Data);
  1791. Item->OnClick = HistoryItemClick;
  1792. Menu->Items->Add(Item);
  1793. }
  1794. }
  1795. }
  1796. //---------------------------------------------------------------------------
  1797. TTBXPopupMenu * __fastcall TCustomScpExplorerForm::HistoryMenu(
  1798. TOperationSide Side, bool Back)
  1799. {
  1800. if (FHistoryMenu[Side == osLocal][Back] == NULL)
  1801. {
  1802. // workaround
  1803. // In Pascal the size of TTBXPopupMenu is 132, in C++ 136,
  1804. // operator new allocates memory in Pascal code, but calls inline
  1805. // contructor in C++, leading in problems, the function does
  1806. // both in Pascal code
  1807. FHistoryMenu[Side == osLocal][Back] = CreateTBXPopupMenu(this);
  1808. UpdateHistoryMenu(Side, Back);
  1809. }
  1810. return FHistoryMenu[Side == osLocal][Back];
  1811. }
  1812. //---------------------------------------------------------------------------
  1813. void __fastcall TCustomScpExplorerForm::DirViewHistoryChange(
  1814. TCustomDirView *Sender)
  1815. {
  1816. TOperationSide Side = (Sender == DirView(osOther) ? osOther : osLocal);
  1817. UpdateHistoryMenu(Side, true);
  1818. UpdateHistoryMenu(Side, false);
  1819. }
  1820. //---------------------------------------------------------------------------
  1821. bool __fastcall TCustomScpExplorerForm::CustomCommandRemoteAllowed()
  1822. {
  1823. // remote custom commands can be executed only if the server supports shell commands
  1824. // or have secondary shell
  1825. return HasActiveTerminal() && (Terminal->IsCapable[fcSecondaryShell] || Terminal->IsCapable[fcShellAnyCommand]);
  1826. }
  1827. //---------------------------------------------------------------------------
  1828. int __fastcall TCustomScpExplorerForm::CustomCommandState(
  1829. const TCustomCommandType & Command, bool OnFocused, TCustomCommandListType ListType)
  1830. {
  1831. // -1 = hidden, 0 = disabled, 1 = enabled
  1832. int Result;
  1833. TFileCustomCommand RemoteCustomCommand;
  1834. TLocalCustomCommand LocalCustomCommand;
  1835. TFileCustomCommand * NonInteractiveCustomCommand =
  1836. FLAGCLEAR(Command.Params, ccLocal) ? &RemoteCustomCommand : &LocalCustomCommand;
  1837. TInteractiveCustomCommand InteractiveCustomCommand(NonInteractiveCustomCommand);
  1838. UnicodeString Cmd = InteractiveCustomCommand.Complete(Command.Command, false);
  1839. if (FLAGCLEAR(Command.Params, ccLocal))
  1840. {
  1841. int AllowedState = CustomCommandRemoteAllowed() ? 1 : 0;
  1842. // custom command that does not operate with files can be executed anytime ...
  1843. if (!NonInteractiveCustomCommand->IsFileCommand(Cmd))
  1844. {
  1845. // ... but do not show such command in remote file menu
  1846. if ((ListType == ccltAll) || (ListType == ccltNonFile))
  1847. {
  1848. Result = AllowedState;
  1849. }
  1850. else
  1851. {
  1852. Result = -1;
  1853. }
  1854. }
  1855. else
  1856. {
  1857. if ((ListType == ccltAll) || ((ListType == ccltFile) && !IsSideLocalBrowser(FCurrentSide)))
  1858. {
  1859. Result = (DirView(FCurrentSide)->AnyFileSelected(OnFocused, false, true)) ? AllowedState : 0;
  1860. }
  1861. else
  1862. {
  1863. Result = -1;
  1864. }
  1865. }
  1866. }
  1867. else
  1868. {
  1869. // custom command that does not operate with files can be executed anytime
  1870. if (!NonInteractiveCustomCommand->IsFileCommand(Cmd))
  1871. {
  1872. Result = ((ListType == ccltAll) || (ListType == ccltNonFile)) ? 1 : -1;
  1873. }
  1874. else if (LocalCustomCommand.HasLocalFileName(Cmd))
  1875. {
  1876. if ((ListType == ccltAll) || (ListType == ccltFile))
  1877. {
  1878. // special case is "diff"-style command that can be executed over any side,
  1879. // if we have both sides
  1880. Result =
  1881. // Cannot have focus on both panels, so we have to call AnyFileSelected
  1882. // directly (instead of EnableSelectedOperation) to pass
  1883. // false to FocusedFileOnlyWhenFocused when panel is inactive.
  1884. ((HasDirView[osLocal] && !IsSideLocalBrowser(osRemote) &&
  1885. DirView(osLocal)->AnyFileSelected(false, false, (FCurrentSide == osLocal))) &&
  1886. DirView(osRemote)->AnyFileSelected(false, false, (FCurrentSide == osRemote))) ? 1 : 0;
  1887. }
  1888. else if (ListType == ccltBoth)
  1889. {
  1890. DebugAssert(!IsLocalBrowserMode());
  1891. Result = 1;
  1892. }
  1893. else
  1894. {
  1895. Result = -1;
  1896. }
  1897. }
  1898. else
  1899. {
  1900. if ((ListType == ccltAll) ||
  1901. ((ListType == ccltFile) && (!IsSideLocalBrowser(FCurrentSide) || FLAGCLEAR(Command.Params, ccRemoteFiles))))
  1902. {
  1903. Result = DirView(FCurrentSide)->AnyFileSelected(OnFocused, false, true) ? 1 : 0;
  1904. }
  1905. else
  1906. {
  1907. Result = -1;
  1908. }
  1909. }
  1910. if ((Result > 0) &&
  1911. LocalCustomCommand.IsSessionCommand(Cmd) &&
  1912. !HasActiveTerminal())
  1913. {
  1914. Result = 0;
  1915. }
  1916. }
  1917. return Result;
  1918. }
  1919. //---------------------------------------------------------------------------
  1920. void __fastcall TCustomScpExplorerForm::RemoteCustomCommand(
  1921. TStrings * FileList, const TCustomCommandType & ACommand,
  1922. const TCustomCommandData & Data, const UnicodeString & CommandCommand)
  1923. {
  1924. if (EnsureCommandSessionFallback(fcShellAnyCommand))
  1925. {
  1926. TRemoteCustomCommand RemoteCustomCommand(Data, Terminal->CurrentDirectory);
  1927. TWinInteractiveCustomCommand InteractiveCustomCommand(
  1928. &RemoteCustomCommand, ACommand.Name, ACommand.HomePage);
  1929. UnicodeString Command = InteractiveCustomCommand.Complete(CommandCommand, false);
  1930. Configuration->Usage->Inc(L"RemoteCustomCommandRuns2");
  1931. bool Capture =
  1932. FLAGSET(ACommand.Params, ccShowResults) ||
  1933. FLAGSET(ACommand.Params, ccCopyResults) ||
  1934. FLAGSET(ACommand.Params, ccShowResultsInMsgBox);
  1935. TCaptureOutputEvent OutputEvent = NULL;
  1936. DebugAssert(FCapturedLog == NULL);
  1937. if (Capture)
  1938. {
  1939. FCapturedLog = new TStringList();
  1940. OutputEvent = TerminalCaptureLog;
  1941. }
  1942. try
  1943. {
  1944. if (!RemoteCustomCommand.IsFileCommand(Command))
  1945. {
  1946. Terminal->AnyCommand(RemoteCustomCommand.Complete(Command, true),
  1947. OutputEvent);
  1948. }
  1949. else
  1950. {
  1951. Terminal->CustomCommandOnFiles(Command, ACommand.Params, FileList, OutputEvent);
  1952. }
  1953. if ((FCapturedLog != NULL) && (FCapturedLog->Count > 0))
  1954. {
  1955. if (FLAGSET(ACommand.Params, ccCopyResults))
  1956. {
  1957. CopyToClipboard(FCapturedLog);
  1958. }
  1959. if (FLAGSET(ACommand.Params, ccShowResults))
  1960. {
  1961. DoConsoleDialog(Terminal, L"", FCapturedLog);
  1962. }
  1963. else if (FLAGSET(ACommand.Params, ccShowResultsInMsgBox))
  1964. {
  1965. MessageDialog(FCapturedLog->Text, qtInformation, qaOK);
  1966. }
  1967. }
  1968. }
  1969. __finally
  1970. {
  1971. SAFE_DESTROY(FCapturedLog);
  1972. }
  1973. }
  1974. }
  1975. //---------------------------------------------------------------------------
  1976. void __fastcall TCustomScpExplorerForm::LocalCustomCommandPure(
  1977. TStrings * FileList, const TCustomCommandType & ACommand, const UnicodeString & Command, TStrings * ALocalFileList,
  1978. const TCustomCommandData & Data, bool LocalFileCommand, bool FileListCommand, UnicodeString * POutput)
  1979. {
  1980. DebugAssert(!IsLocalBrowserMode());
  1981. TStrings * LocalFileList = NULL;
  1982. TStrings * RemoteFileList = NULL;
  1983. TStrings * RemoteFileListFull = NULL;
  1984. try
  1985. {
  1986. if (LocalFileCommand)
  1987. {
  1988. if (ALocalFileList == NULL)
  1989. {
  1990. DebugAssert(HasDirView[osLocal]);
  1991. // Cannot have focus on both panels, so we have to call AnyFileSelected
  1992. // directly (instead of EnableSelectedOperation) to pass
  1993. // false to FocusedFileOnlyWhenFocused
  1994. DebugAssert(DirView(osLocal)->AnyFileSelected(false, false, false));
  1995. LocalFileList = DirView(osLocal)->CreateFileList(false, true, NULL);
  1996. }
  1997. else
  1998. {
  1999. LocalFileList = ALocalFileList;
  2000. }
  2001. if (FileListCommand)
  2002. {
  2003. if (LocalFileList->Count != 1)
  2004. {
  2005. throw Exception(LoadStr(CUSTOM_COMMAND_SELECTED_UNMATCH1));
  2006. }
  2007. }
  2008. else
  2009. {
  2010. if ((LocalFileList->Count != 1) &&
  2011. (FileList->Count != 1) &&
  2012. (LocalFileList->Count != FileList->Count))
  2013. {
  2014. throw Exception(LoadStr(CUSTOM_COMMAND_SELECTED_UNMATCH));
  2015. }
  2016. }
  2017. }
  2018. UnicodeString RootTempDir;
  2019. UnicodeString TempDir;
  2020. bool RemoteFiles = FLAGSET(ACommand.Params, ccRemoteFiles);
  2021. if (!RemoteFiles)
  2022. {
  2023. TemporarilyDownloadFiles(FileList, false, RootTempDir, TempDir, false, true, true);
  2024. }
  2025. try
  2026. {
  2027. TDateTimes RemoteFileTimes;
  2028. if (RemoteFiles)
  2029. {
  2030. RemoteFileList = FileList;
  2031. }
  2032. else
  2033. {
  2034. RemoteFileList = new TStringList();
  2035. TMakeLocalFileListParams MakeFileListParam;
  2036. MakeFileListParam.FileList = RemoteFileList;
  2037. MakeFileListParam.FileTimes = &RemoteFileTimes;
  2038. MakeFileListParam.IncludeDirs = FLAGSET(ACommand.Params, ccApplyToDirectories);
  2039. MakeFileListParam.Recursive =
  2040. FLAGSET(ACommand.Params, ccRecursive) && !FileListCommand;
  2041. ProcessLocalDirectory(TempDir, Terminal->MakeLocalFileList, &MakeFileListParam);
  2042. if (!MakeFileListParam.Recursive)
  2043. {
  2044. RemoteFileTimes.clear();
  2045. RemoteFileListFull = new TStringList();
  2046. MakeFileListParam.FileList = RemoteFileListFull;
  2047. MakeFileListParam.Recursive = true;
  2048. MakeFileListParam.IncludeDirs = false;
  2049. ProcessLocalDirectory(TempDir, Terminal->MakeLocalFileList, &MakeFileListParam);
  2050. }
  2051. else
  2052. {
  2053. RemoteFileListFull = RemoteFileList;
  2054. }
  2055. }
  2056. bool NonBlocking = FileListCommand && RemoteFiles && !POutput;
  2057. TFileOperationProgressType Progress(&OperationProgress, &OperationFinished);
  2058. if (!NonBlocking)
  2059. {
  2060. int Count;
  2061. if (FileListCommand)
  2062. {
  2063. Count = 1;
  2064. }
  2065. else if (LocalFileCommand)
  2066. {
  2067. Count = std::max(LocalFileList->Count, RemoteFileList->Count);
  2068. }
  2069. else
  2070. {
  2071. Count = RemoteFileList->Count;
  2072. }
  2073. Progress.Start(foCustomCommand, osRemote, Count);
  2074. DebugAssert(FProgressForm != NULL);
  2075. FProgressForm->ReadOnly = true;
  2076. }
  2077. try
  2078. {
  2079. if (FileListCommand)
  2080. {
  2081. UnicodeString LocalFile;
  2082. // MakeFileList does not delimit filenames
  2083. UnicodeString FileList = MakeFileList(RemoteFileList);
  2084. if (LocalFileCommand)
  2085. {
  2086. DebugAssert(LocalFileList->Count == 1);
  2087. LocalFile = LocalFileList->Strings[0];
  2088. }
  2089. TLocalCustomCommand CustomCommand(Data,
  2090. Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(), L"", LocalFile, FileList);
  2091. UnicodeString ShellCommand = CustomCommand.Complete(Command, true);
  2092. if (NonBlocking)
  2093. {
  2094. DebugAssert(!POutput);
  2095. ExecuteShellChecked(ShellCommand);
  2096. }
  2097. else
  2098. {
  2099. ExecuteProcessCheckedAndWait(ShellCommand, HelpKeyword, POutput);
  2100. }
  2101. }
  2102. else if (LocalFileCommand)
  2103. {
  2104. if (LocalFileList->Count == 1)
  2105. {
  2106. UnicodeString LocalFile = LocalFileList->Strings[0];
  2107. for (int Index = 0; Index < RemoteFileList->Count; Index++)
  2108. {
  2109. UnicodeString FileName = RemoteFileList->Strings[Index];
  2110. UnicodeString FileNameForProgress = RemoteFiles ? FileName : ExtractFileName(FileName);
  2111. Progress.SetFile(FileNameForProgress);
  2112. TLocalCustomCommand CustomCommand(Data,
  2113. Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(), FileName, LocalFile, L"");
  2114. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  2115. TOnceDoneOperation OnceDoneOperation;
  2116. // Do not unselect anything, as it won't work with two panels anyway
  2117. Progress.Finish(UnicodeString(), true, OnceDoneOperation);
  2118. }
  2119. }
  2120. else if (RemoteFileList->Count == 1)
  2121. {
  2122. UnicodeString FileName = RemoteFileList->Strings[0];
  2123. for (int Index = 0; Index < LocalFileList->Count; Index++)
  2124. {
  2125. UnicodeString LocalFileName = LocalFileList->Strings[Index];
  2126. UnicodeString LocalFileNameOnly = ExtractFileName(LocalFileName);
  2127. Progress.SetFile(LocalFileNameOnly);
  2128. TLocalCustomCommand CustomCommand(
  2129. Data, Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(),
  2130. FileName, LocalFileName, L"");
  2131. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  2132. TOnceDoneOperation OnceDoneOperation;
  2133. // Do not unselect anything, as it won't work with two panels anyway
  2134. Progress.Finish(UnicodeString(), true, OnceDoneOperation);
  2135. }
  2136. }
  2137. else
  2138. {
  2139. if (LocalFileList->Count != RemoteFileList->Count)
  2140. {
  2141. throw Exception(LoadStr(CUSTOM_COMMAND_PAIRS_DOWNLOAD_FAILED));
  2142. }
  2143. for (int Index = 0; Index < LocalFileList->Count; Index++)
  2144. {
  2145. UnicodeString FileName = RemoteFileList->Strings[Index];
  2146. UnicodeString LocalFileName = LocalFileList->Strings[Index];
  2147. UnicodeString LocalFileNameOnly = ExtractFileName(LocalFileName);
  2148. Progress.SetFile(LocalFileNameOnly); // sic
  2149. TLocalCustomCommand CustomCommand(
  2150. Data, Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(),
  2151. FileName, LocalFileName, L"");
  2152. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  2153. TOnceDoneOperation OnceDoneOperation;
  2154. // Do not unselect anything, as it won't work with two panels anyway
  2155. Progress.Finish(UnicodeString(), true, OnceDoneOperation);
  2156. }
  2157. }
  2158. }
  2159. else
  2160. {
  2161. for (int Index = 0; Index < RemoteFileList->Count; Index++)
  2162. {
  2163. UnicodeString FileName = RemoteFileList->Strings[Index];
  2164. UnicodeString FileNameForProgress = RemoteFiles ? FileName : ExtractFileName(FileName);
  2165. Progress.SetFile(FileNameForProgress);
  2166. TLocalCustomCommand CustomCommand(Data,
  2167. Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(),
  2168. FileName, L"", L"");
  2169. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  2170. TOnceDoneOperation OnceDoneOperation;
  2171. Progress.Finish(FileNameForProgress, true, OnceDoneOperation);
  2172. }
  2173. }
  2174. }
  2175. __finally
  2176. {
  2177. if (!NonBlocking)
  2178. {
  2179. Progress.Stop();
  2180. }
  2181. }
  2182. DebugAssert(!FAutoOperation);
  2183. if (!RemoteFiles)
  2184. {
  2185. TempDir = IncludeTrailingBackslash(TempDir);
  2186. for (int Index = 0; Index < RemoteFileListFull->Count; Index++)
  2187. {
  2188. UnicodeString FileName = RemoteFileListFull->Strings[Index];
  2189. if (DebugAlwaysTrue(SameText(TempDir, FileName.SubString(1, TempDir.Length()))) &&
  2190. // Skip directories, process only nested files.
  2191. // The check is redundant as FileAge fails for directories anyway.
  2192. !DirectoryExists(FileName))
  2193. {
  2194. UnicodeString RemoteDir =
  2195. UnixExtractFileDir(
  2196. UnixIncludeTrailingBackslash(Terminal->CurrentDirectory) +
  2197. ToUnixPath(FileName.SubString(TempDir.Length() + 1, FileName.Length() - TempDir.Length())));
  2198. TDateTime NewTime;
  2199. if (FileAge(FileName, NewTime) &&
  2200. (NewTime != RemoteFileTimes[Index]))
  2201. {
  2202. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  2203. TemporaryFileCopyParam(CopyParam);
  2204. CopyParam.FileMask = L"";
  2205. FAutoOperation = true;
  2206. std::unique_ptr<TStrings> TemporaryFilesList(new TStringList());
  2207. TemporaryFilesList->Add(FileName);
  2208. Terminal->CopyToRemote(TemporaryFilesList.get(), RemoteDir, &CopyParam, cpTemporary, NULL);
  2209. }
  2210. }
  2211. }
  2212. }
  2213. }
  2214. __finally
  2215. {
  2216. FAutoOperation = false;
  2217. if (!RootTempDir.IsEmpty() && DebugAlwaysTrue(!RemoteFiles))
  2218. {
  2219. RecursiveDeleteFile(ExcludeTrailingBackslash(RootTempDir), false);
  2220. }
  2221. }
  2222. }
  2223. __finally
  2224. {
  2225. if (RemoteFileList != FileList)
  2226. {
  2227. delete RemoteFileList;
  2228. }
  2229. if (LocalFileList != ALocalFileList)
  2230. {
  2231. delete LocalFileList;
  2232. }
  2233. if (RemoteFileListFull != RemoteFileList)
  2234. {
  2235. delete RemoteFileListFull;
  2236. }
  2237. }
  2238. }
  2239. //---------------------------------------------------------------------------
  2240. void __fastcall TCustomScpExplorerForm::LocalCustomCommandWithLocalFiles(
  2241. const TCustomCommandType & ACommand, const UnicodeString & Command, const TCustomCommandData & Data,
  2242. bool FileListCommand, UnicodeString * POutput)
  2243. {
  2244. std::unique_ptr<TStrings> SelectedFileList(DirView(osCurrent)->CreateFileList(false, true, NULL));
  2245. std::unique_ptr<TStrings> LocalFileList(new TStringList());
  2246. for (int Index = 0; Index < SelectedFileList->Count; Index++)
  2247. {
  2248. UnicodeString FileName = SelectedFileList->Strings[Index];
  2249. if (DirectoryExists(FileName))
  2250. {
  2251. if (FLAGSET(ACommand.Params, ccApplyToDirectories))
  2252. {
  2253. LocalFileList->Add(FileName);
  2254. }
  2255. if (FLAGSET(ACommand.Params, ccRecursive))
  2256. {
  2257. TMakeLocalFileListParams MakeFileListParam;
  2258. MakeFileListParam.FileList = LocalFileList.get();
  2259. MakeFileListParam.FileTimes = NULL;
  2260. MakeFileListParam.IncludeDirs = FLAGSET(ACommand.Params, ccApplyToDirectories);
  2261. MakeFileListParam.Recursive = true;
  2262. ProcessLocalDirectory(FileName, Terminal->MakeLocalFileList, &MakeFileListParam);
  2263. }
  2264. }
  2265. else
  2266. {
  2267. LocalFileList->Add(FileName);
  2268. }
  2269. }
  2270. UnicodeString RemotePath;
  2271. if (Terminal != NULL)
  2272. {
  2273. RemotePath = Terminal->CurrentDirectory;
  2274. }
  2275. if (FileListCommand)
  2276. {
  2277. UnicodeString FileList = MakeFileList(LocalFileList.get());
  2278. TLocalCustomCommand CustomCommand(
  2279. Data, RemotePath, DefaultDownloadTargetDirectory(),
  2280. L"", L"", FileList);
  2281. ExecuteProcessChecked(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  2282. }
  2283. else
  2284. {
  2285. TFileOperationProgressType Progress(&OperationProgress, &OperationFinished);
  2286. Progress.Start(foCustomCommand, osLocal, LocalFileList->Count);
  2287. DebugAssert(FProgressForm != NULL);
  2288. FProgressForm->ReadOnly = true;
  2289. try
  2290. {
  2291. for (int Index = 0; Index < LocalFileList->Count; Index++)
  2292. {
  2293. UnicodeString FileName = LocalFileList->Strings[Index];
  2294. Progress.SetFile(FileName);
  2295. TLocalCustomCommand CustomCommand(
  2296. Data, RemotePath, DefaultDownloadTargetDirectory(),
  2297. FileName, L"", L"");
  2298. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  2299. TOnceDoneOperation OnceDoneOperation;
  2300. Progress.Finish(FileName, true, OnceDoneOperation);
  2301. }
  2302. }
  2303. __finally
  2304. {
  2305. Progress.Stop();
  2306. }
  2307. }
  2308. }
  2309. //---------------------------------------------------------------------------
  2310. void __fastcall TCustomScpExplorerForm::LocalCustomCommand(TStrings * FileList,
  2311. const TCustomCommandType & ACommand, TStrings * ALocalFileList,
  2312. const TCustomCommandData & Data, const UnicodeString & CommandCommand)
  2313. {
  2314. UnicodeString RemotePath = (Terminal != NULL) ? Terminal->CurrentDirectory : UnicodeString();
  2315. TLocalCustomCommand LocalCustomCommand(Data, RemotePath, DefaultDownloadTargetDirectory());
  2316. TWinInteractiveCustomCommand InteractiveCustomCommand(
  2317. &LocalCustomCommand, ACommand.Name, ACommand.HomePage);
  2318. UnicodeString Command = InteractiveCustomCommand.Complete(CommandCommand, false);
  2319. bool FileListCommand = LocalCustomCommand.IsFileListCommand(Command);
  2320. bool LocalFileCommand = LocalCustomCommand.HasLocalFileName(Command);
  2321. Configuration->Usage->Inc(L"LocalCustomCommandRuns2");
  2322. std::unique_ptr<UnicodeString> POutput;
  2323. if (FLAGSET(ACommand.Params, ccShowResultsInMsgBox) ||
  2324. FLAGSET(ACommand.Params, ccCopyResults))
  2325. {
  2326. POutput.reset(new UnicodeString());
  2327. }
  2328. TValueRestorer<TOperationSide> ProgressSideRestorer(FProgressSide);
  2329. FProgressSide = FCurrentSide;
  2330. if (!LocalCustomCommand.IsFileCommand(Command))
  2331. {
  2332. ExecuteProcessChecked(LocalCustomCommand.Complete(Command, true), HelpKeyword, POutput.get());
  2333. }
  2334. // remote files?
  2335. else if (!IsSideLocalBrowser(FCurrentSide) || LocalFileCommand)
  2336. {
  2337. LocalCustomCommandPure(FileList, ACommand, Command, ALocalFileList, Data, LocalFileCommand, FileListCommand, POutput.get());
  2338. }
  2339. // local files
  2340. else
  2341. {
  2342. LocalCustomCommandWithLocalFiles(ACommand, Command, Data, FileListCommand, POutput.get());
  2343. }
  2344. if (POutput.get() != NULL)
  2345. {
  2346. // If the output is single-line, we do not want the trailing CRLF, as the CopyToClipboard(TStrings *) does
  2347. int P = POutput->Pos(sLineBreak);
  2348. if (P == POutput->Length() - static_cast<int>(strlen(sLineBreak)) + 1)
  2349. {
  2350. POutput->SetLength(P - 1);
  2351. }
  2352. // Copy even empty output to clipboard
  2353. if (FLAGSET(ACommand.Params, ccCopyResults))
  2354. {
  2355. CopyToClipboard(*POutput);
  2356. }
  2357. // But do not show an empty message box.
  2358. // This way the ShowResultsInMsgBox can be used to suppress a console window of commands with no output
  2359. if (FLAGSET(ACommand.Params, ccShowResultsInMsgBox) &&
  2360. !POutput->IsEmpty())
  2361. {
  2362. TClipboardHandler ClipboardHandler;
  2363. ClipboardHandler.Text = *POutput;
  2364. TMessageParams Params;
  2365. TQueryButtonAlias Aliases[1];
  2366. Aliases[0].Button = qaRetry;
  2367. Aliases[0].Alias = LoadStr(EDIT_COPY);
  2368. Aliases[0].OnSubmit = &ClipboardHandler.Copy;
  2369. Params.Aliases = Aliases;
  2370. Params.AliasesCount = LENOF(Aliases);
  2371. MessageDialog(*POutput, qtInformation, qaOK | qaRetry, HelpKeyword, &Params);
  2372. }
  2373. }
  2374. }
  2375. //---------------------------------------------------------------------------
  2376. void __fastcall TCustomScpExplorerForm::CustomCommand(TStrings * FileList,
  2377. const TCustomCommandType & ACommand, TStrings * ALocalFileList)
  2378. {
  2379. TCustomCommandData Data;
  2380. UnicodeString Site;
  2381. UnicodeString RemotePath;
  2382. if (HasActiveTerminal())
  2383. {
  2384. std::unique_ptr<TSessionData> SessionData(SessionDataForCode());
  2385. Data = TCustomCommandData(SessionData.get());
  2386. Site = Terminal->SessionData->SessionKey;
  2387. RemotePath = Terminal->CurrentDirectory;
  2388. }
  2389. UnicodeString HelpKeyword = ACommand.HomePage;
  2390. std::unique_ptr<TStrings> CustomCommandOptions(CloneStrings(WinConfiguration->CustomCommandOptions));
  2391. if (ACommand.AnyOptionWithFlag(TCustomCommandType::ofRun))
  2392. {
  2393. std::unique_ptr<TCustomCommand> CustomCommandForOptions;
  2394. if (FLAGCLEAR(ACommand.Params, ccLocal))
  2395. {
  2396. CustomCommandForOptions.reset(new TRemoteCustomCommand(Data, RemotePath));
  2397. }
  2398. else
  2399. {
  2400. CustomCommandForOptions.reset(new TLocalCustomCommand(Data, RemotePath, DefaultDownloadTargetDirectory()));
  2401. }
  2402. if (!DoCustomCommandOptionsDialog(
  2403. &ACommand, CustomCommandOptions.get(), NULL, TCustomCommandType::ofRun, CustomCommandForOptions.get(), Site, NULL))
  2404. {
  2405. Abort();
  2406. }
  2407. }
  2408. UnicodeString CommandCommand = ACommand.GetCommandWithExpandedOptions(CustomCommandOptions.get(), Site);
  2409. if (FLAGCLEAR(ACommand.Params, ccLocal))
  2410. {
  2411. RemoteCustomCommand(FileList, ACommand, Data, CommandCommand);
  2412. }
  2413. else
  2414. {
  2415. LocalCustomCommand(FileList, ACommand, ALocalFileList, Data, CommandCommand);
  2416. }
  2417. }
  2418. //---------------------------------------------------------------------------
  2419. void __fastcall TCustomScpExplorerForm::BothCustomCommand(
  2420. const TCustomCommandType & Command)
  2421. {
  2422. DebugAssert(FCustomCommandLocalFileList != NULL);
  2423. DebugAssert(FCustomCommandRemoteFileList != NULL);
  2424. DebugAssert(FCustomCommandLocalFileList->Count == FCustomCommandRemoteFileList->Count);
  2425. TStrings * LocalFileList = new TStringList();
  2426. TStrings * RemoteFileList = new TStringList();
  2427. try
  2428. {
  2429. for (int Index = 0; Index < FCustomCommandLocalFileList->Count; Index++)
  2430. {
  2431. LocalFileList->Clear();
  2432. LocalFileList->AddObject(
  2433. FCustomCommandLocalFileList->Strings[Index],
  2434. FCustomCommandLocalFileList->Objects[Index]);
  2435. RemoteFileList->Clear();
  2436. RemoteFileList->AddObject(
  2437. FCustomCommandRemoteFileList->Strings[Index],
  2438. FCustomCommandRemoteFileList->Objects[Index]);
  2439. CustomCommand(RemoteFileList, Command, LocalFileList);
  2440. }
  2441. }
  2442. __finally
  2443. {
  2444. delete LocalFileList;
  2445. delete RemoteFileList;
  2446. }
  2447. }
  2448. //---------------------------------------------------------------------------
  2449. void __fastcall TCustomScpExplorerForm::CustomCommandMenu(
  2450. TAction * Action, TStrings * LocalFileList, TStrings * RemoteFileList)
  2451. {
  2452. delete FCustomCommandLocalFileList;
  2453. delete FCustomCommandRemoteFileList;
  2454. // takeover ownership,
  2455. // the lists must survive the MenuPopup as OnClick occurs only after it exits
  2456. FCustomCommandLocalFileList = LocalFileList;
  2457. FCustomCommandRemoteFileList = RemoteFileList;
  2458. TButton * Button = dynamic_cast<TButton *>(Action->ActionComponent);
  2459. if (Button != NULL)
  2460. {
  2461. FCustomCommandMenu->Items->Clear();
  2462. NonVisualDataModule->CreateCustomCommandsMenu(FCustomCommandMenu->Items, false, false, ccltBoth, NULL);
  2463. MenuPopup(FCustomCommandMenu, Button);
  2464. }
  2465. else
  2466. {
  2467. NonVisualDataModule->CreateCustomCommandsMenu(Action, false, ccltBoth);
  2468. }
  2469. }
  2470. //---------------------------------------------------------------------------
  2471. void __fastcall TCustomScpExplorerForm::TerminalCaptureLog(
  2472. const UnicodeString & AddedLine, TCaptureOutputType OutputType)
  2473. {
  2474. DebugAssert(FCapturedLog != NULL);
  2475. if ((OutputType == cotOutput) || (OutputType == cotError))
  2476. {
  2477. FCapturedLog->Add(AddedLine);
  2478. }
  2479. }
  2480. //---------------------------------------------------------------------------
  2481. bool __fastcall TCustomScpExplorerForm::IsFileControl(TObject * Control,
  2482. TOperationSide Side)
  2483. {
  2484. return
  2485. (Side == osOther) &&
  2486. ((Control == DirView(osOther)) || (Control == DriveView(osOther)));
  2487. }
  2488. //---------------------------------------------------------------------------
  2489. void __fastcall TCustomScpExplorerForm::DirViewContextPopupDefaultItem(
  2490. TOperationSide Side, TTBXCustomItem * Item,
  2491. TResolvedDoubleClickAction DoubleClickAction1, TResolvedDoubleClickAction DoubleClickAction2)
  2492. {
  2493. TCustomDirView * DView = DirView(Side);
  2494. bool IsDefault = false;
  2495. if (DView->ItemFocused != NULL)
  2496. {
  2497. TTerminal * ATerminal = !IsSideLocalBrowser(Side) ? Terminal : NULL;
  2498. TResolvedDoubleClickAction DefaultAction =
  2499. WinConfiguration->ResolveDoubleClickAction(DView->ItemIsDirectory(DView->ItemFocused), ATerminal);
  2500. IsDefault = (DefaultAction == DoubleClickAction1) || (DefaultAction == DoubleClickAction2);
  2501. }
  2502. TTBItemOptions O = Item->Options;
  2503. if (IsDefault)
  2504. {
  2505. Item->Options = O << tboDefault;
  2506. }
  2507. else
  2508. {
  2509. Item->Options = O >> tboDefault;
  2510. }
  2511. }
  2512. //---------------------------------------------------------------------------
  2513. void __fastcall TCustomScpExplorerForm::DirViewContextPopup(
  2514. TOperationSide Side, Byte PopupComponent, const TPoint &MousePos)
  2515. {
  2516. TCustomDirView * DView = DirView(Side);
  2517. TListItem * Item = DView->ItemFocused;
  2518. if ((DView->GetItemAt(MousePos.x, MousePos.y) == Item) &&
  2519. EnableFocusedOperation[Side])
  2520. {
  2521. TPoint ClientPoint;
  2522. ClientPoint = ((MousePos.x < 0) && (MousePos.y < 0)) ?
  2523. TPoint(0, 0) : MousePos;
  2524. FLastContextPopupScreenPoint = DView->ClientToScreen(ClientPoint);
  2525. reinterpret_cast<TPopupMenu*>(GetComponent(PopupComponent))->Popup(
  2526. FLastContextPopupScreenPoint.x, FLastContextPopupScreenPoint.y);
  2527. }
  2528. }
  2529. //---------------------------------------------------------------------------
  2530. void __fastcall TCustomScpExplorerForm::RemoteDirViewContextPopup(
  2531. TObject * /*Sender*/, const TPoint &MousePos, bool &Handled)
  2532. {
  2533. DirViewContextPopupDefaultItem(osRemote, NonVisualDataModule->RemoteOpenMenuItem, rdcaChangeDir, rdcaOpen);
  2534. DirViewContextPopupDefaultItem(osRemote, NonVisualDataModule->RemoteEditMenuItem, rdcaEdit, rdcaNone);
  2535. DirViewContextPopupDefaultItem(osRemote, NonVisualDataModule->RemoteCopyMenuItem, rdcaCopy, rdcaNone);
  2536. DirViewContextPopup(osRemote, fcRemotePopup, MousePos);
  2537. Handled = true;
  2538. }
  2539. //---------------------------------------------------------------------------
  2540. void __fastcall TCustomScpExplorerForm::ReloadLocalDirectory(const UnicodeString Directory)
  2541. {
  2542. }
  2543. //---------------------------------------------------------------------------
  2544. void __fastcall TCustomScpExplorerForm::BatchStart(void *& /*Storage*/)
  2545. {
  2546. DebugAssert(FErrorList == NULL);
  2547. if (WinConfiguration->ContinueOnError)
  2548. {
  2549. FErrorList = new TStringList();
  2550. Configuration->Usage->Inc(L"ContinuationsOnError");
  2551. }
  2552. NonVisualDataModule->StartBusy();
  2553. }
  2554. //---------------------------------------------------------------------------
  2555. void __fastcall TCustomScpExplorerForm::BatchEnd(void * /*Storage*/)
  2556. {
  2557. NonVisualDataModule->EndBusy();
  2558. if (FErrorList)
  2559. {
  2560. HandleErrorList(FErrorList);
  2561. }
  2562. }
  2563. //---------------------------------------------------------------------------
  2564. void __fastcall TCustomScpExplorerForm::UpdateCopyParamCounters(
  2565. const TCopyParamType & CopyParam)
  2566. {
  2567. if (!CopyParam.IncludeFileMask.Masks.IsEmpty())
  2568. {
  2569. Configuration->Usage->Inc(L"FileMaskUses");
  2570. }
  2571. if (IsEffectiveFileNameMask(CopyParam.FileMask))
  2572. {
  2573. Configuration->Usage->Inc(L"OperationMaskUses");
  2574. }
  2575. }
  2576. //---------------------------------------------------------------------------
  2577. bool __fastcall TCustomScpExplorerForm::ExecuteCopyMoveFileOperation(
  2578. TFileOperation Operation, TOperationSide Side, TStrings * FileList, bool NoConfirmation, void * AParam)
  2579. {
  2580. DebugAssert(!IsLocalBrowserMode());
  2581. TTransferDirection Direction = (Side == osLocal ? tdToRemote : tdToLocal);
  2582. TTransferType Type = (Operation == foCopy ? ttCopy : ttMove);
  2583. TTransferOperationParam DefaultParam;
  2584. TTransferOperationParam & Param = (AParam != NULL) ? *static_cast<TTransferOperationParam *>(AParam) : DefaultParam;
  2585. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  2586. switch (Param.Queue)
  2587. {
  2588. case asOn:
  2589. CopyParam.Queue = true;
  2590. break;
  2591. case asOff:
  2592. CopyParam.Queue = false;
  2593. break;
  2594. case asAuto:
  2595. default:
  2596. // keep default
  2597. break;
  2598. }
  2599. bool Result =
  2600. CopyParamDialog(Direction, Type, Param.Temp, FileList, Param.TargetDirectory, CopyParam, !NoConfirmation, Param.DragDrop, Param.Options);
  2601. if (Result)
  2602. {
  2603. DebugAssert(Terminal);
  2604. bool SelectionRestored = false;
  2605. TCustomDirView * DView = NULL;
  2606. if (HasDirView[Side])
  2607. {
  2608. DView = DirView(Side);
  2609. DView->SaveSelection();
  2610. DView->SaveSelectedNames();
  2611. }
  2612. UpdateCopyParamCounters(CopyParam);
  2613. std::unique_ptr<TStringList> TransferResumeList(new TStringList());
  2614. DebugAssert(FTransferResumeList == NULL);
  2615. FTransferResumeList = Terminal->IsCapable[fcMoveToQueue] ? TransferResumeList.get() : NULL;
  2616. FMoveToQueue = false;
  2617. int Params = FLAGMASK(Operation == foMove, cpDelete);
  2618. try
  2619. {
  2620. TStrings * PermanentFileList;
  2621. std::unique_ptr<TStrings> PermanentFileListOwner;
  2622. try
  2623. {
  2624. if (Side == osLocal)
  2625. {
  2626. PermanentFileList = FileList;
  2627. Params |= FLAGMASK(Param.Temp, cpTemporary);
  2628. CopyParam.IncludeFileMask.SetRoots(FileList, Param.TargetDirectory);
  2629. Terminal->CopyToRemote(FileList, Param.TargetDirectory, &CopyParam, Params, NULL);
  2630. if (Operation == foMove)
  2631. {
  2632. ReloadLocalDirectory();
  2633. if (DView != NULL)
  2634. {
  2635. DView->RestoreSelection();
  2636. }
  2637. SelectionRestored = true;
  2638. }
  2639. }
  2640. else
  2641. {
  2642. // Clone the file list as it may refer to current directory files,
  2643. // which get destroyed, when the source directory is reloaded after foMove operation.
  2644. // We should actually clone the file list for whole ExecuteFileOperation to protect against reloads.
  2645. // But for a hotfix, we are not going to do such a big change.
  2646. PermanentFileListOwner.reset(TRemoteFileList::CloneStrings(FileList));
  2647. PermanentFileList = PermanentFileListOwner.get();
  2648. try
  2649. {
  2650. CopyParam.IncludeFileMask.SetRoots(Param.TargetDirectory, FileList);
  2651. Terminal->CopyToLocal(FileList, Param.TargetDirectory, &CopyParam, Params, NULL);
  2652. }
  2653. __finally
  2654. {
  2655. if (Operation == foMove)
  2656. {
  2657. if (DView != NULL)
  2658. {
  2659. DView->RestoreSelection();
  2660. }
  2661. SelectionRestored = true;
  2662. }
  2663. ReloadLocalDirectory(Param.TargetDirectory);
  2664. }
  2665. }
  2666. // When the transfer batch is aborted between individual file transfers, the Abort is not called.
  2667. if (FMoveToQueue)
  2668. {
  2669. Abort();
  2670. }
  2671. }
  2672. catch (EAbort &)
  2673. {
  2674. if (FMoveToQueue)
  2675. {
  2676. Params |= (CopyParam.QueueNoConfirmation ? cpNoConfirmation : 0);
  2677. DebugAssert(CopyParam.TransferSkipList == NULL);
  2678. DebugAssert(CopyParam.TransferResumeFile.IsEmpty());
  2679. if (TransferResumeList->Count > 0)
  2680. {
  2681. CopyParam.TransferResumeFile = TransferResumeList->Strings[TransferResumeList->Count - 1];
  2682. TransferResumeList->Delete(TransferResumeList->Count - 1);
  2683. }
  2684. CopyParam.TransferSkipList = TransferResumeList.release();
  2685. // not really needed, just to keep it consistent with TransferResumeList
  2686. FTransferResumeList = NULL;
  2687. FMoveToQueue = false;
  2688. Configuration->Usage->Inc("MovesToBackground");
  2689. AddQueueItem(Queue, Direction, PermanentFileList, Param.TargetDirectory, CopyParam, Params);
  2690. ClearTransferSourceSelection(Direction);
  2691. }
  2692. throw;
  2693. }
  2694. }
  2695. __finally
  2696. {
  2697. if (!SelectionRestored && (DView != NULL))
  2698. {
  2699. DView->DiscardSavedSelection();
  2700. }
  2701. FTransferResumeList = NULL;
  2702. }
  2703. }
  2704. return Result;
  2705. }
  2706. //---------------------------------------------------------------------------
  2707. bool __fastcall TCustomScpExplorerForm::ExecuteDeleteFileOperation(
  2708. TOperationSide Side, TStrings * FileList, void * Param)
  2709. {
  2710. DebugAssert(FileList->Count);
  2711. // We deliberately do not toggle alternative flag (Param), but use OR,
  2712. // because the Param is set only when command is invoked using Shift-Del/F8 keyboard
  2713. // shortcut of CurrentDeleteAlternativeAction
  2714. bool Alternative =
  2715. bool(Param) || UseAlternativeFunction();
  2716. bool Recycle;
  2717. if (IsSideLocalBrowser(Side))
  2718. {
  2719. Recycle = (WinConfiguration->DeleteToRecycleBin != Alternative);
  2720. }
  2721. else
  2722. {
  2723. Recycle =
  2724. (Terminal->SessionData->DeleteToRecycleBin != Alternative) &&
  2725. !Terminal->SessionData->RecycleBinPath.IsEmpty() &&
  2726. !Terminal->IsRecycledFile(FileList->Strings[0]);
  2727. }
  2728. bool Result = !(Recycle ? WinConfiguration->ConfirmRecycling : WinConfiguration->ConfirmDeleting);
  2729. if (!Result)
  2730. {
  2731. UnicodeString Query;
  2732. if (FileList->Count == 1)
  2733. {
  2734. if (IsSideLocalBrowser(Side))
  2735. {
  2736. Query = ExtractFileName(FileList->Strings[0]);
  2737. }
  2738. else
  2739. {
  2740. Query = UnixExtractFileName(FileList->Strings[0]);
  2741. }
  2742. Query = FMTLOAD((Recycle ? CONFIRM_RECYCLE_FILE : CONFIRM_DELETE_FILE), (Query));
  2743. }
  2744. else
  2745. {
  2746. Query = FMTLOAD((Recycle ? CONFIRM_RECYCLE_FILES : CONFIRM_DELETE_FILES), (FileList->Count));
  2747. }
  2748. TMessageParams Params(mpNeverAskAgainCheck);
  2749. Params.ImageName = L"Delete file";
  2750. unsigned int Answer =
  2751. MessageDialog(MainInstructions(Query), qtConfirmation, qaOK | qaCancel, HELP_DELETE_FILE, &Params);
  2752. if (Answer == qaNeverAskAgain)
  2753. {
  2754. Result = true;
  2755. if (Recycle)
  2756. {
  2757. WinConfiguration->ConfirmRecycling = false;
  2758. }
  2759. else
  2760. {
  2761. WinConfiguration->ConfirmDeleting = false;
  2762. }
  2763. }
  2764. else
  2765. {
  2766. Result = (Answer == qaOK);
  2767. }
  2768. }
  2769. if (Result)
  2770. {
  2771. DeleteFiles(Side, FileList, FLAGMASK(Alternative, dfAlternative));
  2772. }
  2773. return Result;
  2774. }
  2775. //---------------------------------------------------------------------------
  2776. bool __fastcall TCustomScpExplorerForm::ExecuteFileOperation(TFileOperation Operation,
  2777. TOperationSide Side, TStrings * FileList, bool NoConfirmation, void * Param)
  2778. {
  2779. TAutoBatch AutoBatch(this);
  2780. bool Result;
  2781. if (TFileOperationProgressType::IsTransferOperation(Operation))
  2782. {
  2783. Result = ExecuteCopyMoveFileOperation(Operation, Side, FileList, NoConfirmation, Param);
  2784. }
  2785. else if (Operation == foRename)
  2786. {
  2787. DebugAssert(DirView(Side)->ItemFocused);
  2788. DirView(Side)->ItemFocused->EditCaption();
  2789. Result = true;
  2790. }
  2791. else if (Operation == foDelete)
  2792. {
  2793. Result = ExecuteDeleteFileOperation(Side, FileList, Param);
  2794. }
  2795. else if (Operation == foSetProperties)
  2796. {
  2797. DirView(osRemote)->SaveSelectedNames();
  2798. Result = SetProperties(Side, FileList);
  2799. }
  2800. else if (Operation == foCustomCommand)
  2801. {
  2802. DebugAssert(Param);
  2803. DebugAssert(Side == osRemote); // Side carries no meaning for foCustomCommand
  2804. DirView(osRemote)->SaveSelectedNames();
  2805. const TCustomCommandType * Command = static_cast<const TCustomCommandType*>(Param);
  2806. CustomCommand(FileList, *Command, NULL);
  2807. Result = true;
  2808. }
  2809. else if ((Operation == foRemoteMove) || (Operation == foRemoteCopy))
  2810. {
  2811. DebugAssert(!IsSideLocalBrowser(Side));
  2812. Result = RemoteTransferFiles(FileList, NoConfirmation,
  2813. (Operation == foRemoteMove), reinterpret_cast<TManagedTerminal *>(Param));
  2814. }
  2815. else if (Operation == foLock)
  2816. {
  2817. DebugAssert(!IsSideLocalBrowser(Side));
  2818. LockFiles(FileList, true);
  2819. Result = true;
  2820. }
  2821. else if (Operation == foUnlock)
  2822. {
  2823. DebugAssert(!IsSideLocalBrowser(Side));
  2824. LockFiles(FileList, false);
  2825. Result = true;
  2826. }
  2827. else
  2828. {
  2829. DebugFail();
  2830. }
  2831. return Result;
  2832. }
  2833. //---------------------------------------------------------------------------
  2834. TOperationSide __fastcall TCustomScpExplorerForm::GetSide(TOperationSide Side)
  2835. {
  2836. if (Side == osCurrent)
  2837. {
  2838. Side = FCurrentSide;
  2839. }
  2840. return Side;
  2841. }
  2842. //---------------------------------------------------------------------------
  2843. bool __fastcall TCustomScpExplorerForm::ExecuteFileOperation(TFileOperation Operation,
  2844. TOperationSide Side, bool OnFocused, bool NoConfirmation, void * Param)
  2845. {
  2846. Side = GetSide(Side);
  2847. bool FullPath = IsSideLocalBrowser(Side);
  2848. TCustomDirView * ADirView = DirView(Side);
  2849. std::unique_ptr<TStringList> FileList(new TStringList());
  2850. ADirView->CreateFileList(OnFocused, FullPath, FileList.get());
  2851. bool ReloadProperties =
  2852. !IsSideLocalBrowser(Side) && (Operation == foSetProperties) &&
  2853. DebugAlwaysTrue(HasActiveTerminal()) && Terminal->IsCapable[fcLoadingAdditionalProperties];
  2854. bool Result = ExecuteFileOperation(Operation, Side, FileList.get(), NoConfirmation, Param);
  2855. if (Result && ReloadProperties)
  2856. {
  2857. std::unique_ptr<TStringList> FileList2(new TStringList());
  2858. FileList->CaseSensitive = true;
  2859. FileList->Sort();
  2860. for (int Index = 0; Index < ADirView->Items->Count; Index++)
  2861. {
  2862. TListItem * Item = ADirView->Items->Item[Index];
  2863. UnicodeString FileName = ADirView->ItemFileName(Item);
  2864. int Index2;
  2865. if (FileList->Find(FileName, Index2))
  2866. {
  2867. FileList2->AddObject(FileName, ADirView->ItemData(Item));
  2868. }
  2869. }
  2870. LoadFilesProperties(FileList2.get());
  2871. }
  2872. return Result;
  2873. }
  2874. //---------------------------------------------------------------------------
  2875. void __fastcall TCustomScpExplorerForm::ExecuteFileOperationCommand(
  2876. TFileOperation Operation, TOperationSide Side, bool OnFocused,
  2877. bool NoConfirmation, void * Param)
  2878. {
  2879. if (ExecuteFileOperation(Operation, Side, OnFocused, NoConfirmation, Param))
  2880. {
  2881. if (TFileOperationProgressType::IsTransferOperation(Operation))
  2882. {
  2883. DebugAssert(!IsLocalBrowserMode());
  2884. if (GetSide(Side) == osLocal)
  2885. {
  2886. Configuration->Usage->Inc(L"UploadsCommand");
  2887. }
  2888. else
  2889. {
  2890. Configuration->Usage->Inc(L"DownloadsCommand");
  2891. }
  2892. }
  2893. }
  2894. }
  2895. //---------------------------------------------------------------------------
  2896. void __fastcall TCustomScpExplorerForm::ExecuteCopyOperationCommand(
  2897. TOperationSide Side, bool OnFocused, unsigned int Flags)
  2898. {
  2899. TTransferOperationParam Param;
  2900. if ((WinConfiguration->Interface != ifCommander) ||
  2901. WinConfiguration->ScpCommander.ExplorerKeyboardShortcuts)
  2902. {
  2903. Flags &= ~cocShortCutHint;
  2904. }
  2905. TCustomDirView * DView = DirView(Side);
  2906. Param.Options =
  2907. FLAGMASK(FLAGSET(Flags, cocShortCutHint), coShortCutHint) |
  2908. FLAGMASK(SelectedAllFilesInDirView(DView), coAllFiles);
  2909. if (FLAGSET(Flags, cocQueue))
  2910. {
  2911. Param.Queue = asOn;
  2912. }
  2913. else if (FLAGSET(Flags, cocNonQueue))
  2914. {
  2915. Param.Queue = asOff;
  2916. }
  2917. ExecuteFileOperationCommand(foCopy, Side, OnFocused, false, &Param);
  2918. }
  2919. //---------------------------------------------------------------------------
  2920. void __fastcall TCustomScpExplorerForm::HandleErrorList(TStringList *& ErrorList)
  2921. {
  2922. int Count = ErrorList->Count;
  2923. try
  2924. {
  2925. if (ErrorList->Count)
  2926. {
  2927. UnicodeString Message = MainInstructions(FMTLOAD(ERROR_LIST_COUNT, (ErrorList->Count)));
  2928. if (MessageDialog(Message, qtError,
  2929. qaOK | qaCancel, HELP_NONE) == qaOK)
  2930. {
  2931. unsigned int Answer;
  2932. int Index = 0;
  2933. do
  2934. {
  2935. DebugAssert(Index >= 0 && Index < Count);
  2936. TQueryButtonAlias Aliases[2];
  2937. Aliases[0].Button = qaYes;
  2938. Aliases[0].Alias = LoadStr(PREV_BUTTON);
  2939. Aliases[1].Button = qaNo;
  2940. Aliases[1].Alias = LoadStr(NEXT_BUTTON);
  2941. TMessageParams Params;
  2942. Params.Aliases = Aliases;
  2943. Params.AliasesCount = LENOF(Aliases);
  2944. int No = Index + 1;
  2945. UnicodeString Message = ErrorList->Strings[Index];
  2946. UnicodeString AMainInstructions;
  2947. UnicodeString Details;
  2948. if (ExtractMainInstructions(Message, AMainInstructions))
  2949. {
  2950. Details = Message;
  2951. Message = AMainInstructions;
  2952. }
  2953. Message = MainInstructions(FMTLOAD(ERROR_LIST_NUMBER, (No, Count, Message))) + Details;
  2954. Answer = MoreMessageDialog(
  2955. Message,
  2956. dynamic_cast<TStrings *>(ErrorList->Objects[Index]), qtError,
  2957. (Index ? qaYes : 0) | (Index < Count - 1 ? qaNo : 0) |
  2958. qaOK, HELP_NONE, &Params);
  2959. if (Answer == qaNo)
  2960. {
  2961. Index++;
  2962. }
  2963. if (Answer == qaYes)
  2964. {
  2965. Index--;
  2966. }
  2967. }
  2968. while (Answer != qaOK);
  2969. }
  2970. }
  2971. }
  2972. __finally
  2973. {
  2974. TStrings * List = ErrorList;
  2975. ErrorList = NULL;
  2976. for (int i = 0; i < Count; i++)
  2977. {
  2978. delete List->Objects[i];
  2979. }
  2980. delete List;
  2981. }
  2982. }
  2983. //---------------------------------------------------------------------------
  2984. void __fastcall TCustomScpExplorerForm::ExecuteRemoteFile(
  2985. const UnicodeString & FullFileName, TRemoteFile * File, TExecuteFileBy ExecuteFileBy)
  2986. {
  2987. // needed for checking filemasks, as there's no directory object
  2988. // associated with the file object
  2989. File->FullFileName = FullFileName;
  2990. TFileMasks::TParams MaskParams;
  2991. MaskParams.Size = File->Size;
  2992. MaskParams.Modification = File->Modification;
  2993. ExecuteFile(osRemote, ExecuteFileBy, NULL, FullFileName, File, MaskParams);
  2994. }
  2995. //---------------------------------------------------------------------------
  2996. void __fastcall TCustomScpExplorerForm::EditNew(TOperationSide Side)
  2997. {
  2998. DebugAssert(!WinConfiguration->DisableOpenEdit);
  2999. Side = GetSide(Side);
  3000. TCustomDirView * DView = DirView(Side);
  3001. TListItem * FocusedItem = DView->ItemFocused;
  3002. UnicodeString Name;
  3003. if ((FocusedItem != NULL) && !DView->ItemIsDirectory(FocusedItem))
  3004. {
  3005. Name = DView->ItemFileName(FocusedItem);
  3006. }
  3007. else
  3008. {
  3009. Name = LoadStr(NEW_FILE);
  3010. }
  3011. UnicodeString Names = Name;
  3012. std::unique_ptr<TStrings> History(CloneStrings(CustomWinConfiguration->History[L"EditFile"]));
  3013. if (InputDialog(LoadStr(EDIT_FILE_CAPTION), LoadStr(EDIT_FILE_PROMPT), Names,
  3014. HELP_EDIT_NEW, History.get(), true, NULL, true, 400))
  3015. {
  3016. while (!Names.IsEmpty())
  3017. {
  3018. Name = CutToChar(Names, FileMasksDelimiters[1], false);
  3019. CustomWinConfiguration->History[L"EditFile"] = History.get();
  3020. UnicodeString TargetFileName;
  3021. UnicodeString LocalFileName;
  3022. UnicodeString RootTempDir;
  3023. UnicodeString TempDir;
  3024. UnicodeString RemoteDirectory;
  3025. bool ExistingFile = false;
  3026. if (!IsSideLocalBrowser(Side))
  3027. {
  3028. Name = AbsolutePath(Terminal->CurrentDirectory, Name);
  3029. std::unique_ptr<TRemoteFile> File(Terminal->TryReadFile(Name));
  3030. if ((File.get() != NULL) &&
  3031. !File->IsDirectory)
  3032. {
  3033. ExecuteRemoteFile(Name, File.get(), efDefaultEditor);
  3034. ExistingFile = true;
  3035. }
  3036. File.reset(NULL);
  3037. if (!ExistingFile)
  3038. {
  3039. RemoteDirectory = UnixExtractFilePath(Name);
  3040. TemporaryDirectoryForRemoteFiles(
  3041. RemoteDirectory, GUIConfiguration->CurrentCopyParam, false, TempDir, RootTempDir);
  3042. TargetFileName = UnixExtractFileName(Name);
  3043. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  3044. LocalFileName = TempDir +
  3045. // We probably do not want to trim the VMS version here
  3046. Terminal->ChangeFileName(&CopyParam, TargetFileName, osRemote, false);
  3047. }
  3048. }
  3049. else
  3050. {
  3051. if (ExtractFilePath(Name).IsEmpty())
  3052. {
  3053. LocalFileName = IncludeTrailingBackslash(DView->PathName) + Name;
  3054. }
  3055. else
  3056. {
  3057. LocalFileName = ExpandFileName(Name);
  3058. }
  3059. TargetFileName = ExtractFileName(Name);
  3060. }
  3061. if (!ExistingFile)
  3062. {
  3063. bool NewFile = !FileExists(ApiPath(LocalFileName));
  3064. if (NewFile)
  3065. {
  3066. int File = FileCreate(ApiPath(LocalFileName));
  3067. if (File < 0)
  3068. {
  3069. if (!RootTempDir.IsEmpty())
  3070. {
  3071. RecursiveDeleteFile(ExcludeTrailingBackslash(RootTempDir), false);
  3072. }
  3073. throw Exception(FMTLOAD(CREATE_FILE_ERROR, (LocalFileName)));
  3074. }
  3075. else
  3076. {
  3077. FileClose(File);
  3078. }
  3079. }
  3080. TExecuteFileBy ExecuteFileBy = efDefaultEditor;
  3081. const TEditorData * ExternalEditor = NULL;
  3082. TFileMasks::TParams MaskParams; // size not known
  3083. ExecuteFileNormalize(ExecuteFileBy, ExternalEditor, TargetFileName,
  3084. false, MaskParams);
  3085. CustomExecuteFile(Side, ExecuteFileBy, LocalFileName, TargetFileName,
  3086. ExternalEditor, RootTempDir, RemoteDirectory, NewFile, TDateTime());
  3087. }
  3088. }
  3089. }
  3090. }
  3091. //---------------------------------------------------------------------------
  3092. bool __fastcall TCustomScpExplorerForm::RemoteExecuteForceText(
  3093. TExecuteFileBy ExecuteFileBy, const TEditorData * ExternalEditor)
  3094. {
  3095. DebugAssert((ExecuteFileBy == efExternalEditor) ==
  3096. ((ExternalEditor != NULL) && (ExternalEditor->Editor == edExternal)));
  3097. DebugAssert(ExecuteFileBy != efDefaultEditor);
  3098. return
  3099. ((ExecuteFileBy == efInternalEditor)) ||
  3100. ((ExecuteFileBy == efExternalEditor) && ExternalEditor->ExternalEditorText);
  3101. }
  3102. //---------------------------------------------------------------------------
  3103. void __fastcall TCustomScpExplorerForm::CustomExecuteFile(TOperationSide Side,
  3104. TExecuteFileBy ExecuteFileBy, UnicodeString FileName, UnicodeString OriginalFileName,
  3105. const TEditorData * ExternalEditor, UnicodeString LocalRootDirectory,
  3106. UnicodeString RemoteDirectory, bool NewFile, const TDateTime & SourceTimestamp)
  3107. {
  3108. DebugAssert(!WinConfiguration->DisableOpenEdit);
  3109. DebugAssert((ExecuteFileBy == efExternalEditor) ==
  3110. ((ExternalEditor != NULL) && (ExternalEditor->Editor == edExternal)));
  3111. DebugAssert(ExecuteFileBy != efDefaultEditor);
  3112. Side = GetSide(Side);
  3113. std::unique_ptr<TEditedFileData> Data(new TEditedFileData);
  3114. if (!IsSideLocalBrowser(Side))
  3115. {
  3116. Data->Terminal = Terminal;
  3117. Data->Queue = Queue;
  3118. Data->SessionData = CloneCurrentSessionData();
  3119. Data->ForceText = RemoteExecuteForceText(ExecuteFileBy, ExternalEditor);
  3120. Data->RemoteDirectory = RemoteDirectory;
  3121. Data->SessionName = Terminal->SessionData->SessionName;
  3122. Data->LocalRootDirectory = LocalRootDirectory;
  3123. Data->OriginalFileName = OriginalFileName;
  3124. Data->Command = L""; // will be changed later for external editor
  3125. // Empty SourceTimestamp indicates that checking was disabled when the file was opened
  3126. if (WinConfiguration->EditorCheckNotModified)
  3127. {
  3128. Data->SourceTimestamp = SourceTimestamp;
  3129. }
  3130. }
  3131. if (ExecuteFileBy == efInternalEditor)
  3132. {
  3133. if (!IsSideLocalBrowser(Side))
  3134. {
  3135. UnicodeString RemotePath = UnixCombinePaths(RemoteDirectory, OriginalFileName);
  3136. UnicodeString Caption = RemotePath + TitleSeparator + Terminal->SessionData->SessionName;
  3137. TForm * Editor;
  3138. try
  3139. {
  3140. Editor =
  3141. ShowEditorForm(
  3142. FileName, this,
  3143. FEditorManager->FileChanged, FEditorManager->FileReload, FEditorManager->FileClosed,
  3144. SaveAllInternalEditors, AnyInternalEditorModified,
  3145. Caption, StandaloneOperation, SessionColor, Terminal->SessionData->InternalEditorEncoding, NewFile);
  3146. }
  3147. catch(...)
  3148. {
  3149. if (!LocalRootDirectory.IsEmpty())
  3150. {
  3151. RecursiveDeleteFile(ExcludeTrailingBackslash(LocalRootDirectory), false);
  3152. }
  3153. throw;
  3154. }
  3155. FEditorManager->AddFileInternal(FileName, Data.release(), Editor);
  3156. AppLogFmt(L"Opened internal editor with \"%s\"", (FileName));
  3157. }
  3158. else
  3159. {
  3160. DebugAssert(!StandaloneOperation);
  3161. TForm * Editor =
  3162. ShowEditorForm(FileName, this, NULL, NULL, LocalEditorClosed,
  3163. SaveAllInternalEditors, AnyInternalEditorModified,
  3164. L"", false, SessionColor, -1, NewFile);
  3165. FLocalEditors->Add(Editor);
  3166. }
  3167. }
  3168. else
  3169. {
  3170. HANDLE Process;
  3171. if (ExecuteFileBy == efExternalEditor)
  3172. {
  3173. UnicodeString Program, Params, Dir;
  3174. Data->Command = ExternalEditor->ExternalEditor;
  3175. ReformatFileNameCommand(Data->Command);
  3176. SplitCommand(Data->Command, Program, Params, Dir);
  3177. Params = ExpandFileNameCommand(Params, FileName);
  3178. Program = ExpandEnvironmentVariables(Program);
  3179. if (!ExecuteShell(Program, Params, Process))
  3180. {
  3181. throw EOSExtException(FMTLOAD(EDITOR_ERROR, (Program)));
  3182. }
  3183. }
  3184. else
  3185. {
  3186. DebugAssert(!IsSideLocalBrowser(Side));
  3187. if (!ExecuteShell(FileName, L"", Process))
  3188. {
  3189. throw EOSExtException(FMTLOAD(EXECUTE_FILE_ERROR, (FileName)));
  3190. }
  3191. }
  3192. if (IsSideLocalBrowser(Side) ||
  3193. ((ExecuteFileBy == efShell) &&
  3194. !WinConfiguration->Editor.SDIShellEditor) ||
  3195. ((ExecuteFileBy == efExternalEditor) &&
  3196. !ExternalEditor->SDIExternalEditor))
  3197. {
  3198. // no need for handle
  3199. if (Process != NULL)
  3200. {
  3201. DebugCheck(CloseHandle(Process));
  3202. }
  3203. Process = INVALID_HANDLE_VALUE;
  3204. }
  3205. else
  3206. {
  3207. if (Process == NULL)
  3208. {
  3209. throw ExtException(LoadStr(OPEN_FILE_NO_PROCESS2), L"", HELP_OPEN_FILE_NO_PROCESS);
  3210. }
  3211. }
  3212. if (!IsSideLocalBrowser(Side))
  3213. {
  3214. FEditorManager->AddFileExternal(FileName, Data.release(), Process);
  3215. AppLogFmt(L"Opened external editor with remote file \"%s\".", (FileName));
  3216. }
  3217. }
  3218. }
  3219. //---------------------------------------------------------------------------
  3220. void __fastcall TCustomScpExplorerForm::SaveInternalEditor(
  3221. const UnicodeString /*FileName*/, TEditedFileData * /*Data*/, TObject * Token,
  3222. void * /*Arg*/)
  3223. {
  3224. if (Token != NULL)
  3225. {
  3226. EditorFormFileSave(static_cast<TForm *>(Token));
  3227. }
  3228. }
  3229. //---------------------------------------------------------------------------
  3230. void __fastcall TCustomScpExplorerForm::SaveAllInternalEditors(TObject * /*Sender*/)
  3231. {
  3232. for (int Index = 0; Index < FLocalEditors->Count; Index++)
  3233. {
  3234. EditorFormFileSave(static_cast<TForm *>(FLocalEditors->Items[Index]));
  3235. }
  3236. FEditorManager->ProcessFiles(SaveInternalEditor, NULL);
  3237. }
  3238. //---------------------------------------------------------------------------
  3239. void __fastcall TCustomScpExplorerForm::InternalEditorModified(
  3240. const UnicodeString /*FileName*/, TEditedFileData * /*Data*/, TObject * Token,
  3241. void * Arg)
  3242. {
  3243. if ((Token != NULL) &&
  3244. IsEditorFormModified(static_cast<TForm *>(Token)))
  3245. {
  3246. *static_cast<bool *>(Arg) = true;
  3247. }
  3248. }
  3249. //---------------------------------------------------------------------------
  3250. void __fastcall TCustomScpExplorerForm::AnyInternalEditorModified(
  3251. TObject * /*Sender*/, bool & Modified)
  3252. {
  3253. for (int Index = 0; !Modified && (Index < FLocalEditors->Count); Index++)
  3254. {
  3255. if (IsEditorFormModified(static_cast<TForm *>(FLocalEditors->Items[Index])))
  3256. {
  3257. Modified = true;
  3258. }
  3259. }
  3260. if (!Modified)
  3261. {
  3262. FEditorManager->ProcessFiles(InternalEditorModified, &Modified);
  3263. }
  3264. }
  3265. //---------------------------------------------------------------------------
  3266. void __fastcall TCustomScpExplorerForm::LocalEditorClosed(TObject * Sender, bool /*Forced*/)
  3267. {
  3268. DebugCheck(FLocalEditors->Extract(Sender) >= 0);
  3269. }
  3270. //---------------------------------------------------------------------------
  3271. void __fastcall TCustomScpExplorerForm::TemporaryDirectoryForRemoteFiles(
  3272. const UnicodeString & RemoteDirectory, const TCopyParamType & CopyParam, bool Simple,
  3273. UnicodeString & Result, UnicodeString & RootDirectory)
  3274. {
  3275. if (!WinConfiguration->TemporaryDirectoryDeterministic || Simple)
  3276. {
  3277. RootDirectory = IncludeTrailingBackslash(WinConfiguration->TemporaryDir());
  3278. Result = RootDirectory;
  3279. }
  3280. else
  3281. {
  3282. RootDirectory = L"";
  3283. Result = WinConfiguration->ExpandedTemporaryDirectory();
  3284. Result = IncludeTrailingBackslash(Result);
  3285. }
  3286. if (WinConfiguration->TemporaryDirectoryAppendSession && !Simple)
  3287. {
  3288. Result = IncludeTrailingBackslash(Result + MakeValidFileName(Terminal->SessionData->SessionName));
  3289. }
  3290. if (WinConfiguration->TemporaryDirectoryAppendPath && !Simple)
  3291. {
  3292. UnicodeString ARemoteDirectory = RemoteDirectory;
  3293. if (!ARemoteDirectory.IsEmpty() && (ARemoteDirectory[1] == L'/'))
  3294. {
  3295. ARemoteDirectory.Delete(1, 1);
  3296. }
  3297. Result = IncludeTrailingBackslash(Result + CopyParam.ValidLocalPath(FromUnixPath(ARemoteDirectory)));
  3298. }
  3299. if (!ForceDirectories(ApiPath(Result)))
  3300. {
  3301. throw EOSExtException(FMTLOAD(CREATE_TEMP_DIR_ERROR, (Result)));
  3302. }
  3303. }
  3304. //---------------------------------------------------------------------------
  3305. void __fastcall TCustomScpExplorerForm::TemporarilyDownloadFiles(
  3306. TStrings * FileList, bool ForceText, UnicodeString & RootTempDir, UnicodeString & TempDir,
  3307. bool GetTargetNames, bool AutoOperation, bool SimpleTempDir)
  3308. {
  3309. DebugAssert(!IsLocalBrowserMode());
  3310. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  3311. if (ForceText)
  3312. {
  3313. CopyParam.TransferMode = tmAscii;
  3314. }
  3315. TemporaryFileCopyParam(CopyParam);
  3316. if (TempDir.IsEmpty())
  3317. {
  3318. TemporaryDirectoryForRemoteFiles(Terminal->CurrentDirectory, CopyParam, SimpleTempDir, TempDir, RootTempDir);
  3319. }
  3320. DebugAssert(!FAutoOperation);
  3321. FAutoOperation = AutoOperation;
  3322. Terminal->ExceptionOnFail = true;
  3323. try
  3324. {
  3325. try
  3326. {
  3327. // turn off confirmations, as for MDI editors we may possibly download
  3328. // the same file over
  3329. Terminal->CopyToLocal(
  3330. FileList, TempDir, &CopyParam, cpNoConfirmation | cpTemporary, NULL);
  3331. if (GetTargetNames)
  3332. {
  3333. for (int i = 0; i < FileList->Count; i++)
  3334. {
  3335. FileList->Strings[i] =
  3336. Terminal->ChangeFileName(&CopyParam, UnixExtractFileName(FileList->Strings[i]), osRemote, false);
  3337. }
  3338. }
  3339. }
  3340. catch(...)
  3341. {
  3342. if (!RootTempDir.IsEmpty())
  3343. {
  3344. RecursiveDeleteFile(ExcludeTrailingBackslash(RootTempDir), false);
  3345. }
  3346. throw;
  3347. }
  3348. }
  3349. __finally
  3350. {
  3351. FAutoOperation = false;
  3352. Terminal->ExceptionOnFail = false;
  3353. }
  3354. }
  3355. //---------------------------------------------------------------------------
  3356. void __fastcall TCustomScpExplorerForm::EditorAutoConfig()
  3357. {
  3358. // Do not waste time checking for default editor list the next time,
  3359. // or testing if default editor is not notepad.
  3360. bool TryNextTime = false;
  3361. UnicodeString UsageState;
  3362. if (!WinConfiguration->EditorList->IsDefaultList())
  3363. {
  3364. UsageState = "H";
  3365. }
  3366. else
  3367. {
  3368. UnicodeString Executable;
  3369. UnicodeString ExecutableDescription;
  3370. if (DetectSystemExternalEditor(false, Executable, ExecutableDescription, UsageState, TryNextTime))
  3371. {
  3372. UnicodeString Message =
  3373. FMTLOAD(EDITOR_AUTO_CONFIG2, (ExecutableDescription, ExecutableDescription));
  3374. unsigned int Answer =
  3375. MessageDialog(Message, qtConfirmation, qaOK | qaCancel, HELP_EDITOR_AUTO_CONFIG);
  3376. if (Answer != qaOK)
  3377. {
  3378. UsageState = "R";
  3379. }
  3380. else
  3381. {
  3382. UsageState = "A";
  3383. TEditorData EditorData;
  3384. EditorData.Editor = edExternal;
  3385. EditorData.ExternalEditor = FormatCommand(Executable, L"");
  3386. EditorData.ExternalEditorOptionsAutodetect();
  3387. TEditorList EditorList;
  3388. EditorList = *WinConfiguration->EditorList;
  3389. EditorList.Insert(0, new TEditorPreferences(EditorData));
  3390. WinConfiguration->EditorList = &EditorList;
  3391. }
  3392. }
  3393. }
  3394. WinConfiguration->OfferedEditorAutoConfig = !TryNextTime;
  3395. WinConfiguration->Usage->Set(L"EditorAutoConfig", UsageState);
  3396. }
  3397. //---------------------------------------------------------------------------
  3398. void __fastcall TCustomScpExplorerForm::ExecuteFileNormalize(
  3399. TExecuteFileBy & ExecuteFileBy, const TEditorData *& ExternalEditor,
  3400. const UnicodeString & FileName, bool Local, const TFileMasks::TParams & MaskParams)
  3401. {
  3402. if (ExecuteFileBy == efDefaultEditor)
  3403. {
  3404. if (!WinConfiguration->OfferedEditorAutoConfig)
  3405. {
  3406. EditorAutoConfig();
  3407. }
  3408. const TEditorPreferences * Editor =
  3409. WinConfiguration->DefaultEditorForFile(FileName, Local, MaskParams);
  3410. if ((Editor == NULL) || (Editor->Data->Editor == edInternal))
  3411. {
  3412. ExecuteFileBy = efInternalEditor;
  3413. ExternalEditor = NULL;
  3414. }
  3415. else if (Editor->Data->Editor == edOpen)
  3416. {
  3417. ExecuteFileBy = efShell;
  3418. ExternalEditor = NULL;
  3419. }
  3420. else
  3421. {
  3422. ExecuteFileBy = efExternalEditor;
  3423. ExternalEditor = Editor->Data;
  3424. }
  3425. }
  3426. }
  3427. //---------------------------------------------------------------------------
  3428. void __fastcall TCustomScpExplorerForm::ExecuteFile(TOperationSide Side,
  3429. TExecuteFileBy ExecuteFileBy, const TEditorData * ExternalEditor,
  3430. UnicodeString FullFileName, TObject * Object, const TFileMasks::TParams & MaskParams)
  3431. {
  3432. UnicodeString OriginalFileName;
  3433. UnicodeString LocalRootDirectory;
  3434. UnicodeString RemoteDirectory;
  3435. ExecuteFileNormalize(ExecuteFileBy, ExternalEditor, FullFileName,
  3436. IsSideLocalBrowser(Side), MaskParams);
  3437. UnicodeString Counter;
  3438. UnicodeString LocalFileName;
  3439. bool Handled = false;
  3440. if (!IsSideLocalBrowser(Side))
  3441. {
  3442. // We need to trim VMS version here, so that we use name without version
  3443. // when uploading back to create a new version of the file
  3444. OriginalFileName = Terminal->GetBaseFileName(UnixExtractFileName(FullFileName));
  3445. RemoteDirectory = UnixExtractFilePath(FullFileName);
  3446. TObject * Token = NULL;
  3447. UnicodeString LocalDirectory;
  3448. if (!FEditorManager->CanAddFile(RemoteDirectory, OriginalFileName,
  3449. Terminal->SessionData->SessionName, Token, LocalRootDirectory,
  3450. LocalDirectory))
  3451. {
  3452. if (Token != NULL)
  3453. {
  3454. TForm * Form = dynamic_cast<TForm *>(Token);
  3455. if (Form->WindowState == wsMinimized)
  3456. {
  3457. ShowWindow(Form->Handle, SW_RESTORE);
  3458. }
  3459. else
  3460. {
  3461. Form->SetFocus();
  3462. }
  3463. Handled = true;
  3464. }
  3465. else
  3466. {
  3467. throw Exception(FMTLOAD(ALREADY_EDITED_EXTERNALLY_OR_UPLOADED, (OriginalFileName)));
  3468. }
  3469. }
  3470. if (!Handled)
  3471. {
  3472. TStringList * FileList1 = new TStringList();
  3473. try
  3474. {
  3475. FileList1->AddObject(FullFileName, Object);
  3476. TemporarilyDownloadFiles(FileList1,
  3477. RemoteExecuteForceText(ExecuteFileBy, ExternalEditor),
  3478. LocalRootDirectory, LocalDirectory, true, true, false);
  3479. LocalFileName = LocalDirectory + FileList1->Strings[0];
  3480. }
  3481. __finally
  3482. {
  3483. delete FileList1;
  3484. }
  3485. switch (ExecuteFileBy)
  3486. {
  3487. case efShell:
  3488. Counter = "RemoteFilesExecuted";
  3489. break;
  3490. case efInternalEditor:
  3491. Counter = "RemoteFilesOpenedInInternalEditor";
  3492. break;
  3493. case efExternalEditor:
  3494. Counter = "RemoteFilesOpenedInExternalEditor";
  3495. break;
  3496. default:
  3497. DebugFail();
  3498. }
  3499. }
  3500. }
  3501. else
  3502. {
  3503. LocalFileName = FullFileName;
  3504. OriginalFileName = ExtractFileName(FullFileName);
  3505. switch (ExecuteFileBy)
  3506. {
  3507. case efShell:
  3508. Counter = "LocalFilesExecuted";
  3509. break;
  3510. case efInternalEditor:
  3511. Counter = "LocalFilesOpenedInInternalEditor";
  3512. break;
  3513. case efExternalEditor:
  3514. Counter = "LocalFilesOpenedInExternalEditor";
  3515. break;
  3516. default:
  3517. DebugFail();
  3518. }
  3519. }
  3520. if (!Handled)
  3521. {
  3522. Configuration->Usage->Inc(Counter);
  3523. CustomExecuteFile(Side, ExecuteFileBy, LocalFileName, OriginalFileName,
  3524. ExternalEditor, LocalRootDirectory, RemoteDirectory, false, MaskParams.Modification);
  3525. }
  3526. }
  3527. //---------------------------------------------------------------------------
  3528. void __fastcall TCustomScpExplorerForm::ExecuteFile(TOperationSide Side,
  3529. TExecuteFileBy ExecuteFileBy, const TEditorData * ExternalEditor,
  3530. bool AllSelected, bool OnFocused)
  3531. {
  3532. DebugAssert(!WinConfiguration->DisableOpenEdit);
  3533. DebugAssert((ExecuteFileBy == efExternalEditor) ==
  3534. ((ExternalEditor != NULL) && (ExternalEditor->Editor == edExternal)));
  3535. Side = GetSide(Side);
  3536. TCustomDirView * DView = DirView(Side);
  3537. TStrings * FileList =
  3538. AllSelected ?
  3539. DView->CreateFileList(OnFocused, IsSideLocalBrowser(Side)) :
  3540. DView->CreateFocusedFileList(IsSideLocalBrowser(Side));
  3541. try
  3542. {
  3543. DebugAssert(AllSelected || (FileList->Count == 1));
  3544. for (int i = 0; i < FileList->Count; i++)
  3545. {
  3546. UnicodeString ListFileName = FileList->Strings[i];
  3547. UnicodeString FileNameOnly =
  3548. !IsSideLocalBrowser(Side) ? UnixExtractFileName(ListFileName) : ExtractFileName(ListFileName);
  3549. TListItem * Item = DView->FindFileItem(FileNameOnly);
  3550. if (!DView->ItemIsDirectory(Item))
  3551. {
  3552. UnicodeString FullFileName;
  3553. if (!IsSideLocalBrowser(Side))
  3554. {
  3555. FullFileName = DirView(Side)->Path + ListFileName;
  3556. }
  3557. else
  3558. {
  3559. FullFileName = ListFileName;
  3560. }
  3561. TObject * Object = FileList->Objects[i];
  3562. TFileMasks::TParams MaskParams;
  3563. MaskParams.Size = DView->ItemFileSize(Item);
  3564. TDateTimePrecision Precision;
  3565. MaskParams.Modification = DView->ItemFileTime(Item, Precision);
  3566. std::unique_ptr<TRemoteFile> File;
  3567. if (!IsSideLocalBrowser(Side))
  3568. {
  3569. File.reset(Terminal->TryReadFile(FullFileName));
  3570. if (File.get() != NULL)
  3571. {
  3572. File->FullFileName = FullFileName;
  3573. Object = File.get();
  3574. MaskParams.Size = File->Size;
  3575. MaskParams.Modification = File->Modification;
  3576. }
  3577. }
  3578. ExecuteFile(Side, ExecuteFileBy, ExternalEditor, FullFileName, Object, MaskParams);
  3579. }
  3580. }
  3581. }
  3582. __finally
  3583. {
  3584. delete FileList;
  3585. }
  3586. }
  3587. //---------------------------------------------------------------------------
  3588. void __fastcall TCustomScpExplorerForm::TemporaryFileCopyParam(TCopyParamType & CopyParam)
  3589. {
  3590. CopyParam.FileNameCase = ncNoChange;
  3591. CopyParam.PreserveRights = false;
  3592. CopyParam.PreserveReadOnly = false;
  3593. CopyParam.ReplaceInvalidChars = true;
  3594. CopyParam.IncludeFileMask = TFileMasks();
  3595. CopyParam.NewerOnly = false;
  3596. CopyParam.FileMask = L"";
  3597. }
  3598. //---------------------------------------------------------------------------
  3599. bool TCustomScpExplorerForm::EditorCheckNotModified(const TEditedFileData * Data)
  3600. {
  3601. return WinConfiguration->EditorCheckNotModified && (Data->SourceTimestamp != TDateTime());
  3602. }
  3603. //---------------------------------------------------------------------------
  3604. class TEditorUploadQueueItem : public TUploadQueueItem
  3605. {
  3606. public:
  3607. __fastcall TEditorUploadQueueItem(
  3608. TTerminal * Terminal, TStrings * FilesToCopy, const UnicodeString & TargetDir,
  3609. const TCopyParamType * CopyParam, int Params) :
  3610. TUploadQueueItem(Terminal, FilesToCopy, TargetDir, CopyParam, Params, true, false)
  3611. {
  3612. }
  3613. protected:
  3614. virtual void __fastcall DoTransferExecute(TTerminal * Terminal, TParallelOperation * ParallelOperation)
  3615. {
  3616. TUploadQueueItem::DoTransferExecute(Terminal, ParallelOperation);
  3617. TTerminalManager::Instance()->ScpExplorer->EditedFileUploaded(Terminal, CompleteEvent);
  3618. }
  3619. };
  3620. //---------------------------------------------------------------------------
  3621. void TCustomScpExplorerForm::EditedFileUploaded(TTerminal * ATerminal, HANDLE UploadCompleteEvent)
  3622. {
  3623. if (WinConfiguration->EditorCheckNotModified) // optimization
  3624. {
  3625. UnicodeString RemoteFilePath;
  3626. {
  3627. TGuard Guard(FEditorManager->Section);
  3628. TEditedFileData * Data = FEditorManager->FindByUploadCompleteEvent(UploadCompleteEvent);
  3629. if (DebugAlwaysTrue(Data != NULL) &&
  3630. EditorCheckNotModified(Data))
  3631. {
  3632. RemoteFilePath = UnixCombinePaths(Data->RemoteDirectory, Data->OriginalFileName);
  3633. }
  3634. }
  3635. if (!RemoteFilePath.IsEmpty())
  3636. {
  3637. std::unique_ptr<TRemoteFile> File(ATerminal->TryReadFile(RemoteFilePath));
  3638. if (File.get() != NULL)
  3639. {
  3640. TGuard Guard(FEditorManager->Section);
  3641. TEditedFileData * Data = FEditorManager->FindByUploadCompleteEvent(UploadCompleteEvent);
  3642. if (DebugAlwaysTrue(Data != NULL))
  3643. {
  3644. Data->SourceTimestamp = File->Modification;
  3645. AppLogFmt(L"Uploaded edited remote file timestamp: %s: ", (StandardTimestamp(Data->SourceTimestamp)));
  3646. }
  3647. }
  3648. }
  3649. }
  3650. }
  3651. //---------------------------------------------------------------------------
  3652. void __fastcall TCustomScpExplorerForm::ExecutedFileChanged(
  3653. const UnicodeString & FileName, TEditedFileData * Data, HANDLE UploadCompleteEvent, bool & Retry)
  3654. {
  3655. TTerminalManager * Manager = TTerminalManager::Instance();
  3656. if ((Manager->ActiveTerminal == Data->Terminal) &&
  3657. Manager->ScheduleTerminalReconnnect(Data->Terminal))
  3658. {
  3659. AppLog(L"Scheduled session reconnect, will retry the upload later");
  3660. Retry = true;
  3661. }
  3662. else if (!IsActiveTerminal(Data->Terminal))
  3663. {
  3664. if (!NonVisualDataModule->Busy)
  3665. {
  3666. UnicodeString FileNameOnly = ExtractFileName(FileName);
  3667. UnicodeString EditInactiveSessionReopenAcceptedCounter = L"EditInactiveSessionReopenAccepted";
  3668. UnicodeString EditInactiveSessionReopenRejectedCounter = L"EditInactiveSessionReopenRejected";
  3669. if (Data->Terminal == NULL)
  3670. {
  3671. TManagedTerminal * SameSiteTerminal = Manager->FindActiveTerminalForSite(Data->SessionData);
  3672. if (SameSiteTerminal != NULL)
  3673. {
  3674. UnicodeString Message =
  3675. FMTLOAD(EDIT_SESSION_REATTACH,
  3676. (FileNameOnly, Data->SessionName, FileNameOnly));
  3677. if (MessageDialog(Message, qtConfirmation, qaOK | qaCancel) == qaOK)
  3678. {
  3679. Data->Terminal = SameSiteTerminal;
  3680. Data->Queue = Manager->FindQueueForTerminal(SameSiteTerminal);
  3681. Data->SessionName = SameSiteTerminal->SessionData->SessionName;
  3682. // We might also overwrite session data
  3683. Configuration->Usage->Inc(EditInactiveSessionReopenAcceptedCounter);
  3684. }
  3685. else
  3686. {
  3687. Configuration->Usage->Inc(EditInactiveSessionReopenRejectedCounter);
  3688. Abort();
  3689. }
  3690. }
  3691. }
  3692. // Foreground session should reconnect itself (except for those disconnected explicitly by the user)
  3693. else if (DebugAlwaysTrue(Terminal->Disconnected || (Terminal != Data->Terminal)))
  3694. {
  3695. UnicodeString Message =
  3696. MainInstructions(
  3697. FMTLOAD(EDIT_SESSION_RECONNECT, (Data->SessionName, FileNameOnly)));
  3698. if (MessageDialog(Message, qtConfirmation, qaOK | qaCancel) == qaOK)
  3699. {
  3700. if (Terminal->Disconnected)
  3701. {
  3702. Manager->ActiveSession = Terminal;
  3703. ReconnectSession();
  3704. DebugAssert(Terminal == Data->Terminal);
  3705. Data->Queue = Manager->FindQueueForTerminal(Terminal);
  3706. }
  3707. else
  3708. {
  3709. Manager->SetActiveTerminalWithAutoReconnect(Data->Terminal);
  3710. }
  3711. Configuration->Usage->Inc(EditInactiveSessionReopenAcceptedCounter);
  3712. }
  3713. else
  3714. {
  3715. Configuration->Usage->Inc(EditInactiveSessionReopenRejectedCounter);
  3716. Abort();
  3717. }
  3718. }
  3719. }
  3720. if (!IsActiveTerminal(Data->Terminal))
  3721. {
  3722. Configuration->Usage->Inc(L"EditInactiveSession");
  3723. // Prevent this when not idle (!NonVisualDataModule->Busy)?
  3724. throw Exception(FMTLOAD(EDIT_SESSION_CLOSED2,
  3725. (ExtractFileName(FileName), Data->SessionName)));
  3726. }
  3727. }
  3728. if (!Retry && EditorCheckNotModified(Data))
  3729. {
  3730. if (NonVisualDataModule->Busy)
  3731. {
  3732. Retry = true;
  3733. }
  3734. else
  3735. {
  3736. UnicodeString RemoteFilePath = UnixCombinePaths(Data->RemoteDirectory, Data->OriginalFileName);
  3737. std::unique_ptr<TRemoteFile> File(Terminal->TryReadFile(RemoteFilePath));
  3738. if (File.get() != NULL)
  3739. {
  3740. AppLogFmt(L"Edited remote file timestamp: %s, Original timestamp: %s", (StandardTimestamp(File->Modification), StandardTimestamp(Data->SourceTimestamp)));
  3741. if (File->Modification != Data->SourceTimestamp)
  3742. {
  3743. UnicodeString Message = MainInstructions(LoadStr(EDIT_CHANGED_EXTERNALLY));
  3744. if (MessageDialog(Message, qtConfirmation, qaOK | qaCancel) != qaOK)
  3745. {
  3746. Abort();
  3747. }
  3748. }
  3749. }
  3750. }
  3751. }
  3752. if (!Retry)
  3753. {
  3754. TStrings * FileList = new TStringList();
  3755. try
  3756. {
  3757. FileList->Add(FileName);
  3758. // Consider using the same settings (preset) as when the file was downloaded.
  3759. // More over this does not reflect the actual session that will do the upload.
  3760. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  3761. TemporaryFileCopyParam(CopyParam);
  3762. if (Data->ForceText)
  3763. {
  3764. CopyParam.TransferMode = tmAscii;
  3765. }
  3766. // so i do not need to worry if masking algorithm works in all cases
  3767. // ("" means "copy file name", no masking is actually done)
  3768. if (ExtractFileName(FileName) == Data->OriginalFileName)
  3769. {
  3770. CopyParam.FileMask = L"";
  3771. }
  3772. else
  3773. {
  3774. CopyParam.FileMask = DelimitFileNameMask(Data->OriginalFileName);
  3775. }
  3776. int Params = cpNoConfirmation | cpTemporary;
  3777. if (Data->Terminal->IsCapable[fcBackgroundTransfers])
  3778. {
  3779. DebugAssert(Data->Queue != NULL);
  3780. TQueueItem * QueueItem =
  3781. new TEditorUploadQueueItem(Data->Terminal, FileList, Data->RemoteDirectory, &CopyParam, Params);
  3782. QueueItem->CompleteEvent = UploadCompleteEvent;
  3783. AddQueueItem(Data->Queue, QueueItem, Data->Terminal);
  3784. }
  3785. else
  3786. {
  3787. if (NonVisualDataModule->Busy)
  3788. {
  3789. Retry = true;
  3790. }
  3791. else
  3792. {
  3793. Data->Terminal->CopyToRemote(FileList, Data->RemoteDirectory, &CopyParam, Params, NULL);
  3794. SetEvent(UploadCompleteEvent);
  3795. }
  3796. }
  3797. }
  3798. __finally
  3799. {
  3800. delete FileList;
  3801. }
  3802. }
  3803. }
  3804. //---------------------------------------------------------------------------
  3805. void __fastcall TCustomScpExplorerForm::ExecutedFileReload(
  3806. const UnicodeString & FileName, TEditedFileData * Data)
  3807. {
  3808. // Sanity check, we should not be busy otherwise user would not be able to click Reload button.
  3809. DebugAssert(!NonVisualDataModule->Busy);
  3810. if (!IsActiveTerminal(Data->Terminal))
  3811. {
  3812. throw Exception(FMTLOAD(EDIT_SESSION_CLOSED_RELOAD,
  3813. (ExtractFileName(FileName), Data->SessionName)));
  3814. }
  3815. TManagedTerminal * PrevSession = TTerminalManager::Instance()->ActiveSession;
  3816. TTerminalManager::Instance()->ActiveSession = Data->Terminal;
  3817. NonVisualDataModule->StartBusy();
  3818. try
  3819. {
  3820. std::unique_ptr<TRemoteFile> File;
  3821. UnicodeString RemoteFileName =
  3822. UnixIncludeTrailingBackslash(Data->RemoteDirectory) + Data->OriginalFileName;
  3823. Terminal->ExceptionOnFail = true;
  3824. try
  3825. {
  3826. File.reset(Terminal->ReadFile(RemoteFileName));
  3827. if (!File->HaveFullFileName)
  3828. {
  3829. File->FullFileName = RemoteFileName;
  3830. }
  3831. }
  3832. __finally
  3833. {
  3834. Terminal->ExceptionOnFail = false;
  3835. }
  3836. std::unique_ptr<TStrings> FileList(new TStringList());
  3837. FileList->AddObject(RemoteFileName, File.get());
  3838. UnicodeString RootTempDir = Data->LocalRootDirectory;
  3839. UnicodeString TempDir = ExtractFilePath(FileName);
  3840. TemporarilyDownloadFiles(FileList.get(), Data->ForceText, RootTempDir, TempDir, true, true, false);
  3841. if (EditorCheckNotModified(Data))
  3842. {
  3843. Data->SourceTimestamp = File->Modification;
  3844. }
  3845. // sanity check, the target file name should be still the same
  3846. DebugAssert(ExtractFileName(FileName) == FileList->Strings[0]);
  3847. }
  3848. __finally
  3849. {
  3850. NonVisualDataModule->EndBusy();
  3851. // it actually may not exist anymore...
  3852. TTerminalManager::Instance()->ActiveSession = PrevSession;
  3853. }
  3854. }
  3855. //---------------------------------------------------------------------------
  3856. void __fastcall TCustomScpExplorerForm::ExecutedFileEarlyClosed(
  3857. const TEditedFileData * Data, bool & KeepOpen)
  3858. {
  3859. // Command is set for external editors only (not for "shell" open).
  3860. if (!Data->Command.IsEmpty())
  3861. {
  3862. bool AnyFound = false;
  3863. bool AnyMDI = false;
  3864. bool AnyNonMDI = false;
  3865. bool AnyDetect = false;
  3866. TEditorList * EditorList = new TEditorList();
  3867. try
  3868. {
  3869. *EditorList = *WinConfiguration->EditorList;
  3870. for (int i = 0; i < EditorList->Count; i++)
  3871. {
  3872. const TEditorPreferences * Editor = EditorList->Editors[i];
  3873. if ((Editor->Data->Editor == edExternal) &&
  3874. (Editor->Data->ExternalEditor == Data->Command))
  3875. {
  3876. AnyFound = true;
  3877. if (Editor->Data->SDIExternalEditor)
  3878. {
  3879. AnyNonMDI = true;
  3880. if (Editor->Data->DetectMDIExternalEditor)
  3881. {
  3882. AnyDetect = true;
  3883. }
  3884. }
  3885. else
  3886. {
  3887. AnyMDI = true;
  3888. }
  3889. }
  3890. }
  3891. bool EnableMDI = false;
  3892. bool DisableDetect = false;
  3893. if (AnyMDI)
  3894. {
  3895. KeepOpen = true;
  3896. if (AnyNonMDI)
  3897. {
  3898. // there is at least one instance of the editor with MDI support enabled,
  3899. // and one with disabled, enable it for all instances
  3900. EnableMDI = true;
  3901. }
  3902. }
  3903. else if (AnyFound && !AnyDetect)
  3904. {
  3905. // at least once instance found but all have MDI autodetection disabled
  3906. // => close the file (default action)
  3907. }
  3908. else
  3909. {
  3910. // no instance of the editor has MDI support enabled
  3911. TMessageParams Params;
  3912. if (AnyFound)
  3913. {
  3914. // there is at least one instance of the editor
  3915. Params.Params |= mpNeverAskAgainCheck;
  3916. }
  3917. unsigned int Answer = MessageDialog(FMTLOAD(EDITOR_EARLY_CLOSED2, (Data->OriginalFileName)), qtWarning,
  3918. qaYes | qaNo, HELP_EDITOR_EARLY_CLOSED, &Params);
  3919. switch (Answer)
  3920. {
  3921. case qaNeverAskAgain:
  3922. DisableDetect = true;
  3923. break;
  3924. case qaNo:
  3925. EnableMDI = true;
  3926. KeepOpen = true;
  3927. break;
  3928. }
  3929. }
  3930. if (AnyFound && (EnableMDI || DisableDetect))
  3931. {
  3932. bool Changed = false;
  3933. for (int i = 0; i < EditorList->Count; i++)
  3934. {
  3935. const TEditorPreferences * Editor = EditorList->Editors[i];
  3936. if ((Editor->Data->Editor == edExternal) &&
  3937. (Editor->Data->ExternalEditor == Data->Command) &&
  3938. ((EnableMDI && Editor->Data->SDIExternalEditor) ||
  3939. (DisableDetect && Editor->Data->DetectMDIExternalEditor)))
  3940. {
  3941. Changed = true;
  3942. TEditorPreferences * UpdatedEditor = new TEditorPreferences(*Editor);
  3943. if (EnableMDI)
  3944. {
  3945. UpdatedEditor->GetData()->SDIExternalEditor = false;
  3946. }
  3947. if (DisableDetect)
  3948. {
  3949. UpdatedEditor->GetData()->DetectMDIExternalEditor = false;
  3950. }
  3951. EditorList->Change(i, UpdatedEditor);
  3952. }
  3953. }
  3954. if (Changed)
  3955. {
  3956. WinConfiguration->EditorList = EditorList;
  3957. }
  3958. }
  3959. }
  3960. __finally
  3961. {
  3962. delete EditorList;
  3963. }
  3964. }
  3965. else
  3966. {
  3967. // "open" case
  3968. MessageDialog(FMTLOAD(APP_EARLY_CLOSED, (Data->OriginalFileName)), qtWarning,
  3969. qaOK, HELP_APP_EARLY_CLOSED);
  3970. }
  3971. }
  3972. //---------------------------------------------------------------------------
  3973. void __fastcall TCustomScpExplorerForm::ExecutedFileUploadComplete(TObject * Sender)
  3974. {
  3975. EditorFormFileUploadComplete(DebugNotNull(dynamic_cast<TForm *>(Sender)));
  3976. }
  3977. //---------------------------------------------------------------------------
  3978. void __fastcall TCustomScpExplorerForm::RemoteDirViewEnter(TObject * /*Sender*/)
  3979. {
  3980. SideEnter(osRemote);
  3981. }
  3982. //---------------------------------------------------------------------------
  3983. void __fastcall TCustomScpExplorerForm::RemoteDriveViewEnter(TObject * /*Sender*/)
  3984. {
  3985. MakeNextInTabOrder(RemoteDirPanel, RemoteDrivePanel);
  3986. SideEnter(osRemote);
  3987. }
  3988. //---------------------------------------------------------------------------
  3989. void __fastcall TCustomScpExplorerForm::SideEnter(TOperationSide Side)
  3990. {
  3991. FCurrentSide = Side;
  3992. if (Visible)
  3993. {
  3994. UpdateControls();
  3995. }
  3996. }
  3997. //---------------------------------------------------------------------------
  3998. void __fastcall TCustomScpExplorerForm::FileDeleted(TOperationSide Side, const UnicodeString & FileName, bool Success)
  3999. {
  4000. VisualiseOperationFinished(Side, FileName, Success);
  4001. if (DebugAlwaysTrue(FDeletedFiles != NULL) && Success)
  4002. {
  4003. FDeletedFiles->Add(FileName);
  4004. }
  4005. }
  4006. //---------------------------------------------------------------------------
  4007. void __fastcall TCustomScpExplorerForm::DeleteFiles(TOperationSide Side,
  4008. TStrings * FileList, bool Alternative)
  4009. {
  4010. TCustomDirView * DView = DirView(Side);
  4011. DView->SaveSelection();
  4012. DView->SaveSelectedNames();
  4013. DebugAssert(!FAlternativeDelete);
  4014. FAlternativeDelete = Alternative;
  4015. try
  4016. {
  4017. if (!IsSideLocalBrowser(Side))
  4018. {
  4019. DebugAssert(Terminal != NULL);
  4020. // Clone the file list as it may refer to current directory files, which get destroyed, when the directory is reloaded after the operation
  4021. std::unique_ptr<TStrings> PermanentFileList(TRemoteFileList::CloneStrings(FileList));
  4022. std::unique_ptr<TStringList> DeletedFiles(new TStringList());
  4023. DeletedFiles->CaseSensitive = DebugAlwaysTrue(!IsSideLocalBrowser(Side));
  4024. int Params = FLAGMASK(Alternative, dfAlternative);
  4025. try
  4026. {
  4027. TValueRestorer<TStrings *> DeletedFilesRestorer(FDeletedFiles);
  4028. DebugAssert(FDeletedFiles == NULL);
  4029. FDeletedFiles = DeletedFiles.get();
  4030. TValueRestorer<TFileOperationFinishedEvent> OnFileOperationFinishedRestorer(FOnFileOperationFinished);
  4031. FOnFileOperationFinished = FileDeleted;
  4032. FMoveToQueue = false;
  4033. Terminal->DeleteFiles(FileList, Params);
  4034. // Probably not needed for deleting, just for consistency with transfer code
  4035. if (FMoveToQueue)
  4036. {
  4037. Abort();
  4038. }
  4039. }
  4040. catch (EAbort &)
  4041. {
  4042. if (FMoveToQueue)
  4043. {
  4044. DeletedFiles->Sorted = true;
  4045. for (int Index = 0; Index < PermanentFileList->Count; Index++)
  4046. {
  4047. if (DeletedFiles->IndexOf(PermanentFileList->Strings[Index]) >= 0)
  4048. {
  4049. // We should always be deleting the first item => what can be used to optimize this code, if needed
  4050. DebugAssert(Index == 0);
  4051. PermanentFileList->Delete(Index);
  4052. Index--;
  4053. }
  4054. }
  4055. Configuration->Usage->Inc("MovesToBackgroundDelete");
  4056. TQueueItem * QueueItem = new TDeleteQueueItem(Terminal, PermanentFileList.get(), Params);
  4057. AddQueueItem(Queue, QueueItem, Terminal);
  4058. ClearOperationSelection(osRemote);
  4059. }
  4060. }
  4061. }
  4062. else
  4063. {
  4064. if (IsLocalBrowserMode())
  4065. {
  4066. Configuration->Usage->Inc(L"LocalLocalDeletes");
  4067. }
  4068. TValueRestorer<TOperationSide> ProgressSideRestorer(FProgressSide);
  4069. FProgressSide = FCurrentSide;
  4070. try
  4071. {
  4072. TTerminalManager::Instance()->LocalTerminal->DeleteLocalFiles(FileList, FLAGMASK(Alternative, dfAlternative));
  4073. }
  4074. __finally
  4075. {
  4076. ReloadLocalDirectory();
  4077. }
  4078. }
  4079. FAlternativeDelete = false;
  4080. }
  4081. catch(...)
  4082. {
  4083. FAlternativeDelete = false;
  4084. DView->DiscardSavedSelection();
  4085. throw;
  4086. }
  4087. DView->RestoreSelection();
  4088. }
  4089. //---------------------------------------------------------------------------
  4090. void __fastcall TCustomScpExplorerForm::LockFiles(TStrings * FileList, bool Lock)
  4091. {
  4092. DebugAssert(Terminal);
  4093. DebugAssert(!IsLocalBrowserMode());
  4094. RemoteDirView->SaveSelection();
  4095. RemoteDirView->SaveSelectedNames();
  4096. try
  4097. {
  4098. if (Lock)
  4099. {
  4100. Terminal->LockFiles(FileList);
  4101. }
  4102. else
  4103. {
  4104. Terminal->UnlockFiles(FileList);
  4105. }
  4106. }
  4107. catch(...)
  4108. {
  4109. RemoteDirView->DiscardSavedSelection();
  4110. throw;
  4111. }
  4112. RemoteDirView->RestoreSelection();
  4113. }
  4114. //---------------------------------------------------------------------------
  4115. bool __fastcall TCustomScpExplorerForm::RemoteTransferDialog(TManagedTerminal *& Session,
  4116. TStrings * FileList, UnicodeString & Target, UnicodeString & FileMask, bool & DirectCopy,
  4117. bool NoConfirmation, bool Move)
  4118. {
  4119. DebugAssert(Terminal != NULL);
  4120. DebugAssert(Terminal == TTerminalManager::Instance()->ActiveTerminal);
  4121. DebugAssert(!IsLocalBrowserMode());
  4122. if (Session == NULL)
  4123. {
  4124. Session = Terminal;
  4125. }
  4126. if (Session == Terminal)
  4127. {
  4128. if (RemoteDriveView->DropTarget != NULL)
  4129. {
  4130. Target = RemoteDriveView->NodePathName(RemoteDriveView->DropTarget);
  4131. }
  4132. else if (RemoteDirView->DropTarget != NULL)
  4133. {
  4134. DebugAssert(RemoteDirView->ItemIsDirectory(RemoteDirView->DropTarget));
  4135. Target = RemoteDirView->ItemFullFileName(RemoteDirView->DropTarget);
  4136. }
  4137. else
  4138. {
  4139. Target = RemoteDirView->Path;
  4140. }
  4141. }
  4142. else
  4143. {
  4144. Target = GetSessionPath(Session, osRemote);
  4145. }
  4146. Target = UnixIncludeTrailingBackslash(Target);
  4147. if (FileList->Count == 1)
  4148. {
  4149. FileMask = DelimitFileNameMask(UnixExtractFileName(FileList->Strings[0]));
  4150. }
  4151. else
  4152. {
  4153. FileMask = AnyMask;
  4154. }
  4155. DirectCopy = Terminal->IsCapable[fcRemoteCopy] || Terminal->IsCapable[fcSecondaryShell];
  4156. bool Result = true;
  4157. if (!NoConfirmation)
  4158. {
  4159. bool Multi = (FileList->Count > 1);
  4160. if (Move)
  4161. {
  4162. Result = DoRemoteMoveDialog(Multi, Target, FileMask);
  4163. }
  4164. else
  4165. {
  4166. std::unique_ptr<TStrings> Sessions(new TStringList);
  4167. std::unique_ptr<TStrings> Directories(new TStringList);
  4168. TStrings * SessionList = TTerminalManager::Instance()->SessionList;
  4169. for (int Index = 0; Index < SessionList->Count; Index++)
  4170. {
  4171. TManagedTerminal * ASession = DebugNotNull(dynamic_cast<TManagedTerminal *>(SessionList->Objects[Index]));
  4172. if (IsActiveTerminal(ASession) && DebugAlwaysTrue(!ASession->LocalBrowser))
  4173. {
  4174. Sessions->AddObject(SessionList->Strings[Index], ASession);
  4175. Directories->Add(GetSessionPath(ASession, osRemote));
  4176. }
  4177. }
  4178. TDirectRemoteCopy AllowDirectCopy;
  4179. if (Terminal->IsCapable[fcRemoteCopy] || Terminal->CommandSessionOpened)
  4180. {
  4181. DebugAssert(DirectCopy);
  4182. AllowDirectCopy = drcAllow;
  4183. }
  4184. else if (Terminal->IsCapable[fcSecondaryShell])
  4185. {
  4186. DebugAssert(DirectCopy);
  4187. AllowDirectCopy = drcConfirmCommandSession;
  4188. }
  4189. else
  4190. {
  4191. DebugAssert(!DirectCopy);
  4192. AllowDirectCopy = drcDisallow;
  4193. }
  4194. void * ASession = Session;
  4195. Result = DoRemoteCopyDialog(
  4196. Sessions.get(), Directories.get(), AllowDirectCopy, Multi, ASession, Target, FileMask, DirectCopy, Terminal);
  4197. Session = static_cast<TManagedTerminal *>(ASession);
  4198. }
  4199. }
  4200. return Result;
  4201. }
  4202. //---------------------------------------------------------------------------
  4203. bool __fastcall TCustomScpExplorerForm::RemoteTransferFiles(
  4204. TStrings * FileList, bool NoConfirmation, bool Move, TManagedTerminal * Session)
  4205. {
  4206. DebugAssert(!IsLocalBrowserMode());
  4207. bool DirectCopy;
  4208. UnicodeString Target, FileMask;
  4209. bool Result = RemoteTransferDialog(Session, FileList, Target, FileMask, DirectCopy, NoConfirmation, Move);
  4210. if (Result)
  4211. {
  4212. if (!Move && !DirectCopy)
  4213. {
  4214. Configuration->Usage->Inc("RemoteCopyTemp");
  4215. UnicodeString RootTempDir;
  4216. UnicodeString TempDir;
  4217. TemporarilyDownloadFiles(FileList, false, RootTempDir, TempDir, false, false, true);
  4218. TStrings * TemporaryFilesList = new TStringList();
  4219. try
  4220. {
  4221. TMakeLocalFileListParams MakeFileListParam;
  4222. MakeFileListParam.FileList = TemporaryFilesList;
  4223. MakeFileListParam.FileTimes = NULL;
  4224. MakeFileListParam.IncludeDirs = true;
  4225. MakeFileListParam.Recursive = false;
  4226. ProcessLocalDirectory(TempDir, Terminal->MakeLocalFileList, &MakeFileListParam);
  4227. TTerminalManager::Instance()->ActiveSession = Session;
  4228. if (TemporaryFilesList->Count > 0)
  4229. {
  4230. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  4231. CopyParam.FileMask = FileMask;
  4232. DebugAssert(!FAutoOperation);
  4233. FAutoOperation = true;
  4234. Terminal->CopyToRemote(TemporaryFilesList, Target, &CopyParam, cpTemporary, NULL);
  4235. }
  4236. }
  4237. __finally
  4238. {
  4239. delete TemporaryFilesList;
  4240. FAutoOperation = false;
  4241. if (!RootTempDir.IsEmpty())
  4242. {
  4243. RecursiveDeleteFile(ExcludeTrailingBackslash(RootTempDir), false);
  4244. }
  4245. }
  4246. }
  4247. else
  4248. {
  4249. RemoteDirView->SaveSelection();
  4250. RemoteDirView->SaveSelectedNames();
  4251. try
  4252. {
  4253. if (Move)
  4254. {
  4255. Configuration->Usage->Inc("RemoteMove");
  4256. Terminal->MoveFiles(FileList, Target, FileMask, false);
  4257. }
  4258. else
  4259. {
  4260. Configuration->Usage->Inc("RemoteCopyDirect");
  4261. DebugAssert(DirectCopy);
  4262. DebugAssert(Session == Terminal);
  4263. if (Terminal->IsCapable[fcRemoteCopy] ||
  4264. Terminal->CommandSessionOpened ||
  4265. CommandSessionFallback())
  4266. {
  4267. Terminal->CopyFiles(FileList, Target, FileMask);
  4268. }
  4269. }
  4270. }
  4271. catch(...)
  4272. {
  4273. RemoteDirView->DiscardSavedSelection();
  4274. throw;
  4275. }
  4276. RemoteDirView->RestoreSelection();
  4277. }
  4278. }
  4279. return Result;
  4280. }
  4281. //---------------------------------------------------------------------------
  4282. void __fastcall TCustomScpExplorerForm::CreateRemoteDirectory(
  4283. const UnicodeString & Path, TRemoteProperties & Properties)
  4284. {
  4285. DebugAssert(!IsLocalBrowserMode());
  4286. Properties.Valid = Properties.Valid << vpEncrypt;
  4287. Properties.Encrypt = GUIConfiguration->CurrentCopyParam.EncryptNewFiles;
  4288. RemoteDirView->CreateDirectoryEx(Path, &Properties);
  4289. }
  4290. //---------------------------------------------------------------------------
  4291. void __fastcall TCustomScpExplorerForm::CreateDirectory(TOperationSide Side)
  4292. {
  4293. Side = GetSide(Side);
  4294. TRemoteProperties Properties = GUIConfiguration->NewDirectoryProperties;
  4295. TRemoteProperties * AProperties = (!IsSideLocalBrowser(Side) ? &Properties : NULL);
  4296. UnicodeString Name = LoadStr(NEW_FOLDER);
  4297. int AllowedChanges =
  4298. FLAGMASK(!IsSideLocalBrowser(Side) && Terminal->IsCapable[fcModeChanging], cpMode);
  4299. bool SaveSettings = false;
  4300. if (DoCreateDirectoryDialog(Name, AProperties, AllowedChanges, SaveSettings))
  4301. {
  4302. TWindowLock Lock(this);
  4303. if (!IsSideLocalBrowser(Side))
  4304. {
  4305. if (SaveSettings)
  4306. {
  4307. GUIConfiguration->NewDirectoryProperties = Properties;
  4308. }
  4309. CreateRemoteDirectory(Name, Properties);
  4310. }
  4311. else
  4312. {
  4313. DirView(Side)->CreateDirectory(Name);
  4314. }
  4315. }
  4316. }
  4317. //---------------------------------------------------------------------------
  4318. void __fastcall TCustomScpExplorerForm::HomeDirectory(TOperationSide Side)
  4319. {
  4320. TWindowLock Lock(this);
  4321. DirView(Side)->ExecuteHomeDirectory();
  4322. }
  4323. //---------------------------------------------------------------------------
  4324. void __fastcall TCustomScpExplorerForm::OpenDirectory(TOperationSide Side)
  4325. {
  4326. DoOpenDirectoryDialog(odBrowse, Side);
  4327. }
  4328. //---------------------------------------------------------------------------
  4329. bool __fastcall TCustomScpExplorerForm::OpenBookmark(TOperationSide Side, TBookmark * Bookmark)
  4330. {
  4331. TOperationSide BookmarkSide = IsLocalBrowserMode() ? osLocal : Side;
  4332. UnicodeString Path = Bookmark->GetSideDirectory(BookmarkSide);
  4333. bool Result = !Path.IsEmpty();
  4334. if (Result)
  4335. {
  4336. TryOpenDirectory(Side, Path);
  4337. }
  4338. return Result;
  4339. }
  4340. //---------------------------------------------------------------------------
  4341. void __fastcall TCustomScpExplorerForm::CalculateSize(
  4342. TStrings * FileList, __int64 & Size, TCalculateSizeStats & Stats,
  4343. bool & Close)
  4344. {
  4345. // terminal can be already closed (e.g. dropped connection)
  4346. if (Terminal != NULL)
  4347. {
  4348. try
  4349. {
  4350. TCalculateSizeParams Params;
  4351. Params.Stats = &Stats;
  4352. Terminal->CalculateFilesSize(FileList, Size, Params);
  4353. }
  4354. catch(...)
  4355. {
  4356. if (!Terminal->Active)
  4357. {
  4358. Close = true;
  4359. }
  4360. throw;
  4361. }
  4362. }
  4363. }
  4364. //---------------------------------------------------------------------------
  4365. void __fastcall TCustomScpExplorerForm::CalculateChecksum(const UnicodeString & Alg,
  4366. TStrings * FileList, TCalculatedChecksumEvent OnCalculatedChecksum,
  4367. bool & Close)
  4368. {
  4369. if ((Terminal != NULL) && // terminal can be already closed (e.g. dropped connection)
  4370. EnsureCommandSessionFallback(fcCalculatingChecksum))
  4371. {
  4372. Configuration->Usage->Inc(L"ChecksumCalculated");
  4373. try
  4374. {
  4375. Terminal->CalculateFilesChecksum(Alg, FileList, OnCalculatedChecksum);
  4376. }
  4377. catch(...)
  4378. {
  4379. if (!Terminal->Active)
  4380. {
  4381. Close = true;
  4382. }
  4383. throw;
  4384. }
  4385. }
  4386. }
  4387. //---------------------------------------------------------------------------
  4388. void TCustomScpExplorerForm::LoadFilesProperties(TStrings * FileList)
  4389. {
  4390. if (Terminal->LoadFilesProperties(FileList))
  4391. {
  4392. RemoteDirView->Invalidate();
  4393. }
  4394. }
  4395. //---------------------------------------------------------------------------
  4396. bool __fastcall TCustomScpExplorerForm::SetProperties(TOperationSide Side, TStrings * FileList)
  4397. {
  4398. bool Result;
  4399. if (!IsSideLocalBrowser(Side))
  4400. {
  4401. TRemoteTokenList * GroupList = NULL;
  4402. TRemoteTokenList * UserList = NULL;
  4403. try
  4404. {
  4405. TRemoteProperties CurrentProperties;
  4406. LoadFilesProperties(FileList);
  4407. bool CapableGroupChanging = Terminal->IsCapable[fcGroupChanging];
  4408. bool CapableOwnerChanging = Terminal->IsCapable[fcOwnerChanging];
  4409. if (CapableGroupChanging || CapableOwnerChanging)
  4410. {
  4411. if (CapableGroupChanging)
  4412. {
  4413. GroupList = Terminal->Groups->Duplicate();
  4414. }
  4415. if (CapableOwnerChanging)
  4416. {
  4417. UserList = Terminal->Users->Duplicate();
  4418. }
  4419. TRemoteDirectory * Files = Terminal->Files;
  4420. int Count = Files->Count;
  4421. if (Count > 100)
  4422. {
  4423. Count = 100;
  4424. }
  4425. for (int Index = 0; Index < Count; Index++)
  4426. {
  4427. TRemoteFile * File = Files->Files[Index];
  4428. if (CapableGroupChanging)
  4429. {
  4430. GroupList->AddUnique(File->Group);
  4431. }
  4432. if (CapableOwnerChanging)
  4433. {
  4434. UserList->AddUnique(File->Owner);
  4435. }
  4436. }
  4437. // if we haven't collected tokens for all files in current directory,
  4438. // make sure we collect them at least for all selected files.
  4439. // (note that so far the files in FileList has to be from current direcotry)
  4440. if (Count < Files->Count)
  4441. {
  4442. for (int Index = 0; Index < FileList->Count; Index++)
  4443. {
  4444. TRemoteFile * File = (TRemoteFile *)(FileList->Objects[Index]);
  4445. if (CapableGroupChanging)
  4446. {
  4447. GroupList->AddUnique(File->Group);
  4448. }
  4449. if (CapableOwnerChanging)
  4450. {
  4451. UserList->AddUnique(File->Owner);
  4452. }
  4453. }
  4454. }
  4455. }
  4456. CurrentProperties = TRemoteProperties::CommonProperties(FileList);
  4457. bool CapableAclChanging = Terminal->IsCapable[fcAclChangingFiles];
  4458. for (int Index = 0; (Index < FileList->Count) && CapableAclChanging; Index++)
  4459. {
  4460. if (dynamic_cast<TRemoteFile *>(FileList->Objects[Index])->IsDirectory)
  4461. {
  4462. CapableAclChanging = false;
  4463. CurrentProperties.Valid = CurrentProperties.Valid >> vpRights;
  4464. }
  4465. }
  4466. int Flags = 0;
  4467. if (Terminal->IsCapable[fcModeChanging]) Flags |= cpMode;
  4468. if (CapableAclChanging) Flags |= cpAcl;
  4469. if (CapableOwnerChanging) Flags |= cpOwner;
  4470. if (CapableGroupChanging) Flags |= cpGroup;
  4471. TCalculateChecksumEvent CalculateChecksumEvent = NULL;
  4472. if (Terminal->IsCapable[fcCalculatingChecksum] ||
  4473. (Terminal->IsCapable[fcSecondaryShell] && !Terminal->IsEncryptingFiles()))
  4474. {
  4475. CalculateChecksumEvent = CalculateChecksum;
  4476. }
  4477. std::unique_ptr<TStrings> ChecksumAlgs(new TStringList());
  4478. Terminal->GetSupportedChecksumAlgs(ChecksumAlgs.get());
  4479. TRemoteProperties NewProperties = CurrentProperties;
  4480. Result =
  4481. DoPropertiesDialog(FileList, RemoteDirView->PathName,
  4482. GroupList, UserList, ChecksumAlgs.get(), &NewProperties, Flags,
  4483. Terminal->IsCapable[fcGroupOwnerChangingByID],
  4484. CalculateSize, CalculateChecksumEvent);
  4485. if (Result)
  4486. {
  4487. NewProperties = TRemoteProperties::ChangedProperties(CurrentProperties, NewProperties);
  4488. Terminal->ChangeFilesProperties(FileList, &NewProperties);
  4489. }
  4490. }
  4491. __finally
  4492. {
  4493. delete GroupList;
  4494. delete UserList;
  4495. }
  4496. }
  4497. else
  4498. {
  4499. DirView(Side)->DisplayPropertiesMenu();
  4500. Result = true;
  4501. }
  4502. return Result;
  4503. }
  4504. //---------------------------------------------------------------------------
  4505. void __fastcall TCustomScpExplorerForm::KeyProcessed(Word & Key, TShiftState Shift)
  4506. {
  4507. if (Shift * AllKeyShiftStates() == (TShiftState() << ssAlt))
  4508. {
  4509. FIgnoreNextDialogChar = Key;
  4510. }
  4511. Key = 0;
  4512. }
  4513. //---------------------------------------------------------------------------
  4514. void __fastcall TCustomScpExplorerForm::CheckCustomCommandShortCut(
  4515. TCustomCommandList * List, Word & Key, Classes::TShiftState Shift, TShortCut KeyShortCut)
  4516. {
  4517. const TCustomCommandType * Command = List->Find(KeyShortCut);
  4518. if (Command != NULL)
  4519. {
  4520. KeyProcessed(Key, Shift);
  4521. if (CustomCommandState(*Command, false, ccltAll) > 0)
  4522. {
  4523. TAutoFlag DontCopyCommandToClipboardFlag(DontCopyCommandToClipboard);
  4524. ExecuteFileOperationCommand(foCustomCommand, osRemote,
  4525. false, false, const_cast<TCustomCommandType *>(Command));
  4526. }
  4527. }
  4528. }
  4529. //---------------------------------------------------------------------------
  4530. void __fastcall TCustomScpExplorerForm::KeyDown(Word & Key, Classes::TShiftState Shift)
  4531. {
  4532. if (!NonVisualDataModule->Busy)
  4533. {
  4534. if (QueueView3->Focused() && (QueueView3->OnKeyDown != NULL))
  4535. {
  4536. QueueView3->OnKeyDown(QueueView3, Key, Shift);
  4537. }
  4538. if ((Key != 0) && !DirView(osCurrent)->IsEditing())
  4539. {
  4540. TShortCut KeyShortCut = ShortCut(Key, Shift);
  4541. for (int Index = 0; Index < NonVisualDataModule->ExplorerActions->ActionCount; Index++)
  4542. {
  4543. // Note that handling shortcuts on panel (current control) popup menu has precendence over this.
  4544. // But so far it's not a problem, as it does what we want.
  4545. TAction * Action = (TAction *)NonVisualDataModule->ExplorerActions->Actions[Index];
  4546. if (((Action->ShortCut == KeyShortCut) ||
  4547. (Action->SecondaryShortCuts->IndexOfShortCut(KeyShortCut) >= 0)) &&
  4548. AllowedAction(Action, aaShortCut))
  4549. {
  4550. // Has to be called before the action as the dialog char is already in queue.
  4551. // So when the action consumes message queue, we already need to have the
  4552. // FIgnoreNextDialogChar set
  4553. KeyProcessed(Key, Shift);
  4554. // Reset reference to previous component (when menu/toolbar was clicked).
  4555. // Needed to detect that action was invoked by keyboard shortcut
  4556. // in TNonVisualDataModule::ExplorerActionsExecute
  4557. Action->ActionComponent = NULL;
  4558. Action->Execute();
  4559. return;
  4560. }
  4561. }
  4562. for (int i = 0; i < TTerminalManager::Instance()->Count; i++)
  4563. {
  4564. if (NonVisualDataModule->OpenSessionShortCut(i) == KeyShortCut)
  4565. {
  4566. KeyProcessed(Key, Shift);
  4567. TTerminalManager::Instance()->ActiveSessionIndex = i;
  4568. return;
  4569. }
  4570. }
  4571. if (Key == VK_TAB && Shift.Contains(ssCtrl))
  4572. {
  4573. KeyProcessed(Key, Shift);
  4574. TTerminalManager::Instance()->CycleTerminals(!Shift.Contains(ssShift));
  4575. }
  4576. TShortCut CustomShortCut = NormalizeCustomShortCut(KeyShortCut);
  4577. if (IsCustomShortCut(CustomShortCut))
  4578. {
  4579. CheckCustomCommandShortCut(WinConfiguration->CustomCommandList, Key, Shift, CustomShortCut);
  4580. CheckCustomCommandShortCut(WinConfiguration->ExtensionList, Key, Shift, CustomShortCut);
  4581. if (WinConfiguration->SharedBookmarks != NULL)
  4582. {
  4583. TBookmark * Bookmark = WinConfiguration->SharedBookmarks->FindByShortCut(CustomShortCut);
  4584. if ((Bookmark != NULL) &&
  4585. OpenBookmark(FCurrentSide, Bookmark))
  4586. {
  4587. KeyProcessed(Key, Shift);
  4588. }
  4589. }
  4590. }
  4591. }
  4592. }
  4593. TForm::KeyDown(Key, Shift);
  4594. }
  4595. //---------------------------------------------------------------------------
  4596. void __fastcall TCustomScpExplorerForm::InitStatusBar()
  4597. {
  4598. bool OpenedTerminal = false;
  4599. UnicodeString ProtocolName;
  4600. UnicodeString ProtocolNameHint;
  4601. UnicodeString SecurityProtocolName;
  4602. UnicodeString DurationHint;
  4603. // See the comment in TScpCommanderForm::UpdateControls()
  4604. TTerminal * ActiveTerminal = TTerminalManager::Instance()->ActiveTerminal;
  4605. if (ActiveTerminal != NULL)
  4606. {
  4607. // Active is not enough here
  4608. if (ActiveTerminal->Status == ssOpened)
  4609. {
  4610. const TSessionInfo & SessionInfo = ActiveTerminal->GetSessionInfo();
  4611. const TFileSystemInfo & FileSystemInfo = ActiveTerminal->GetFileSystemInfo();
  4612. if (FileSystemInfo.ProtocolName.IsEmpty())
  4613. {
  4614. ProtocolName = SessionInfo.ProtocolName;
  4615. }
  4616. else
  4617. {
  4618. ProtocolName = FileSystemInfo.ProtocolName;
  4619. }
  4620. ProtocolNameHint = LoadStr(STATUS_PROTOCOL_HINT);
  4621. SecurityProtocolName = SessionInfo.SecurityProtocolName;
  4622. DurationHint = LoadStr(STATUS_DURATION_HINT);
  4623. OpenedTerminal = true;
  4624. }
  4625. else if (ActiveTerminal->Status > ssClosed)
  4626. {
  4627. ProtocolName = LoadStr(STATUS_CONNECTING);
  4628. }
  4629. else
  4630. {
  4631. ProtocolName = LoadStr(STATUS_NOT_CONNECTED2);
  4632. }
  4633. }
  4634. TTBXStatusBar * SessionStatusBar = (TTBXStatusBar *)GetComponent(fcStatusBar);
  4635. int Offset = SessionStatusBar->Panels->Count - SessionPanelCount;
  4636. TTBXStatusPanel * ProtocolStatusPanel = SessionStatusBar->Panels->Items[Offset + 0];
  4637. ProtocolStatusPanel->ViewPriority = (ActiveTerminal != NULL) ? 100 : 0;
  4638. if (ProtocolStatusPanel->Caption != ProtocolName)
  4639. {
  4640. ProtocolStatusPanel->Caption = ProtocolName;
  4641. std::unique_ptr<TCanvas> Canvas(CreateControlCanvas(SessionStatusBar));
  4642. ProtocolStatusPanel->Size = Canvas->TextWidth(ProtocolName) + ScaleByTextHeight(SessionStatusBar, 32);
  4643. }
  4644. ProtocolStatusPanel->Hint = ProtocolNameHint;
  4645. TTBXStatusPanel * SecurityStatusPanel = SessionStatusBar->Panels->Items[Offset + 1];
  4646. SecurityStatusPanel->ViewPriority = OpenedTerminal ? 98 : 0;
  4647. bool SecurityEnabled = !SecurityProtocolName.IsEmpty();
  4648. SecurityStatusPanel->Enabled = SecurityEnabled;
  4649. UnicodeString SecurityProtocolHint;
  4650. if (SecurityEnabled)
  4651. {
  4652. SecurityProtocolHint = FMTLOAD(STATUS_SECURE, (SecurityProtocolName));
  4653. }
  4654. else
  4655. {
  4656. SecurityProtocolHint = LoadStr(STATUS_INSECURE);
  4657. }
  4658. SecurityStatusPanel->Hint = SecurityProtocolHint;
  4659. TTBXStatusPanel * DurationStatusPanel = SessionStatusBar->Panels->Items[Offset + 2];
  4660. DurationStatusPanel->ViewPriority = OpenedTerminal ? 99 : 0;
  4661. DurationStatusPanel->Hint = DurationHint;
  4662. UpdateStatusBar();
  4663. }
  4664. //---------------------------------------------------------------------------
  4665. void __fastcall TCustomScpExplorerForm::UpdateStatusBar()
  4666. {
  4667. TTBXStatusBar * SessionStatusBar = (TTBXStatusBar *)GetComponent(fcStatusBar);
  4668. DebugAssert(SessionStatusBar != NULL);
  4669. TTBXStatusPanel * NoteStatusPanel = SessionStatusBar->Panels->Items[0];
  4670. if (!FNote.IsEmpty())
  4671. {
  4672. NoteStatusPanel->Caption = FNote;
  4673. }
  4674. else
  4675. {
  4676. UpdateStatusPanelText(NoteStatusPanel);
  4677. }
  4678. NoteStatusPanel->Hint = FNoteHints;
  4679. UnicodeString LoginTime;
  4680. // See the comment in TScpCommanderForm::UpdateControls()
  4681. TTerminal * ActiveTerminal = TTerminalManager::Instance()->ActiveTerminal;
  4682. // Active is not enough here
  4683. if ((ActiveTerminal != NULL) && (ActiveTerminal->Status == ssOpened))
  4684. {
  4685. const TSessionInfo & SessionInfo = ActiveTerminal->GetSessionInfo();
  4686. LoginTime = FormatDateTimeSpan(Configuration->TimeFormat, Now() - SessionInfo.LoginTime);
  4687. }
  4688. TTBXStatusPanel * DurationStatusPanel = SessionStatusBar->Panels->Items[SessionStatusBar->Panels->Count - 1];
  4689. DurationStatusPanel->Caption = LoginTime;
  4690. }
  4691. //---------------------------------------------------------------------------
  4692. void __fastcall TCustomScpExplorerForm::UpdateStatusPanelText(TTBXStatusPanel * Panel)
  4693. {
  4694. Panel->Caption = L"";
  4695. }
  4696. //---------------------------------------------------------------------------
  4697. void __fastcall TCustomScpExplorerForm::Idle()
  4698. {
  4699. if (FShowing ||
  4700. // Particularly for
  4701. // - Standalone editing
  4702. // - Detecting closed connection and automatically reconnect it while waiting for changes
  4703. // while "keeping remote directory up to date".
  4704. StandaloneOperation)
  4705. {
  4706. FEditorManager->Check();
  4707. // make sure that Idle is called before update queue, as it may invoke QueueEvent
  4708. // that needs to know if queue view is visible (and it may be closed after queue update)
  4709. TTerminalManager::Instance()->Idle(FDoNotIdleCurrentTerminal > 0);
  4710. }
  4711. if (FShowing)
  4712. {
  4713. if (!NonVisualDataModule->Busy &&
  4714. // Menu is opened or drag&drop is going on
  4715. (Mouse->Capture == NULL))
  4716. {
  4717. if (FRefreshRemoteDirectory)
  4718. {
  4719. if ((Terminal != NULL) && (Terminal->Status == ssOpened))
  4720. {
  4721. Terminal->RefreshDirectory();
  4722. }
  4723. FRefreshRemoteDirectory = false;
  4724. }
  4725. if (FRefreshLocalDirectory)
  4726. {
  4727. ReloadLocalDirectory();
  4728. FRefreshLocalDirectory = false;
  4729. }
  4730. if (WinConfiguration->RefreshRemotePanel)
  4731. {
  4732. if ((Terminal != NULL) && (Terminal->Status == ssOpened) &&
  4733. (Now() - Terminal->DirectoryLoaded > WinConfiguration->RefreshRemotePanelInterval))
  4734. {
  4735. RemoteDirView->ReloadDirectory();
  4736. }
  4737. }
  4738. }
  4739. // TB2 shows accels on Alt key down, but hides it only after menu modal loop finishes.
  4740. // But if Alt is used to switch apps or for keyboard shortcut, the menu modal loop does not happen and accelerators stay displayed.
  4741. // It's difficult to fix this in TB2, as there's no "up" equivalent to CM_DIALOGKEY, which shows the accels.
  4742. // 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.
  4743. TTBCustomToolbar * MenuToolbar = DebugNotNull(dynamic_cast<TTBCustomToolbar *>(GetComponent(fcMenu)));
  4744. MenuToolbar->Idle();
  4745. }
  4746. if (FShowing || StandaloneOperation)
  4747. {
  4748. if (FQueueStatusInvalidated)
  4749. {
  4750. UpdateQueueStatus(false);
  4751. }
  4752. RefreshQueueItems();
  4753. }
  4754. if (FShowing)
  4755. {
  4756. UpdateStatusBar();
  4757. }
  4758. if (FClipboardFakeMonitorsPendingReset && !FDownloadingFromClipboard)
  4759. {
  4760. FClipboardFakeMonitorsPendingReset = false;
  4761. FClipboardFakeMonitors.reset(NULL);
  4762. }
  4763. FIgnoreNextDialogChar = 0;
  4764. }
  4765. //---------------------------------------------------------------------------
  4766. void __fastcall TCustomScpExplorerForm::UserActionTimer(TObject * /*Sender*/)
  4767. {
  4768. try
  4769. {
  4770. FUserActionTimer->Enabled = false;
  4771. if (IsQueueAutoPopup() && (FPendingQueueActionItem != NULL))
  4772. {
  4773. if (TQueueItem::IsUserActionStatus(FPendingQueueActionItem->Status))
  4774. {
  4775. FPendingQueueActionItem->ProcessUserAction();
  4776. }
  4777. }
  4778. }
  4779. __finally
  4780. {
  4781. FPendingQueueActionItem = NULL;
  4782. }
  4783. }
  4784. //---------------------------------------------------------------------------
  4785. void __fastcall TCustomScpExplorerForm::ApplicationMinimize(TObject * /*Sender*/)
  4786. {
  4787. if (WinConfiguration->MinimizeToTray)
  4788. {
  4789. UpdateTrayIcon();
  4790. FTrayIcon->Visible = true;
  4791. if (Visible)
  4792. {
  4793. ShowWindow(Handle, SW_HIDE);
  4794. }
  4795. }
  4796. }
  4797. //---------------------------------------------------------------------------
  4798. void __fastcall TCustomScpExplorerForm::ApplicationRestore(TObject * /*Sender*/)
  4799. {
  4800. // WORKAROUND
  4801. // When restoring maximized window from minimization,
  4802. // rarely some controls do not align properly.
  4803. // Two instances seen (both for Commander):
  4804. // - When restoring, window is temporarily narrower (not maximized),
  4805. // causing toolbars on TopDock to wrap and dock to expand horizontally.
  4806. // Once maximized already, top dock shinks back, but the session PageControl,
  4807. // do not align up, leaving space between TopDock and PageControl.
  4808. // - Similar issue seen with LocalDirView not aligning down to status bar.
  4809. for (int Index = 0; Index < ControlCount; Index++)
  4810. {
  4811. RealignControl(Controls[Index]);
  4812. }
  4813. if (FTrayIcon->Visible)
  4814. {
  4815. FTrayIcon->Visible = false;
  4816. if (Visible)
  4817. {
  4818. ShowWindow(Handle, SW_SHOW);
  4819. }
  4820. }
  4821. if (FNeedSession && DebugAlwaysTrue(Terminal == NULL))
  4822. {
  4823. FNeedSession = false;
  4824. NonVisualDataModule->StartBusy();
  4825. try
  4826. {
  4827. NeedSession(true);
  4828. }
  4829. __finally
  4830. {
  4831. NonVisualDataModule->EndBusy();
  4832. }
  4833. }
  4834. }
  4835. //---------------------------------------------------------------------------
  4836. void __fastcall TCustomScpExplorerForm::UpdateTrayIcon()
  4837. {
  4838. FTrayIcon->Hint = GetMainForm()->Caption;
  4839. }
  4840. //---------------------------------------------------------------------------
  4841. void __fastcall TCustomScpExplorerForm::ApplicationTitleChanged()
  4842. {
  4843. UpdateTrayIcon();
  4844. }
  4845. //---------------------------------------------------------------------------
  4846. void __fastcall TCustomScpExplorerForm::RestoreApp()
  4847. {
  4848. // workaround
  4849. // TApplication.WndProc sets TApplication.FAppIconic to false,
  4850. // on WM_ACTIVATEAPP, what renders TApplication.Restore no-op function.
  4851. // But WM_ACTIVATEAPP message can be received even when
  4852. // the main window is minimized to the tray and internal editor window is focused
  4853. // (after another application was previously active)
  4854. if (::IsIconic(Handle))
  4855. {
  4856. if (!IsAppIconic())
  4857. {
  4858. SetAppIconic(true);
  4859. }
  4860. }
  4861. ::ApplicationRestore();
  4862. Application->BringToFront();
  4863. }
  4864. //---------------------------------------------------------------------------
  4865. void __fastcall TCustomScpExplorerForm::TrayIconClick(TObject * /*Sender*/)
  4866. {
  4867. RestoreApp();
  4868. }
  4869. //---------------------------------------------------------------------------
  4870. void __fastcall TCustomScpExplorerForm::NewSession(const UnicodeString & SessionUrl)
  4871. {
  4872. if (OpenInNewWindow())
  4873. {
  4874. ExecuteNewInstance(SessionUrl);
  4875. }
  4876. else
  4877. {
  4878. TTerminalManager::Instance()->NewSession(SessionUrl);
  4879. }
  4880. }
  4881. //---------------------------------------------------------------------------
  4882. void TCustomScpExplorerForm::NewTab(TOperationSide, bool)
  4883. {
  4884. NewSession();
  4885. }
  4886. //---------------------------------------------------------------------------
  4887. UnicodeString __fastcall TCustomScpExplorerForm::SaveHiddenDuplicateSession(TSessionData * SessionData)
  4888. {
  4889. UnicodeString SessionName = StoredSessions->HiddenPrefix + ManagedSession->SessionData->SessionName;
  4890. StoredSessions->NewSession(SessionName, SessionData);
  4891. // modified only, explicit
  4892. StoredSessions->Save(false, true);
  4893. // encode session name because of slashes in hierarchical sessions
  4894. return EncodeUrlString(SessionName);
  4895. }
  4896. //---------------------------------------------------------------------------
  4897. UnicodeString __fastcall TCustomScpExplorerForm::CreateHiddenDuplicateSession()
  4898. {
  4899. // current working directories become defaults here, what is not right
  4900. std::unique_ptr<TSessionData> SessionData(CloneCurrentSessionData());
  4901. return SaveHiddenDuplicateSession(SessionData.get());
  4902. }
  4903. //---------------------------------------------------------------------------
  4904. void __fastcall TCustomScpExplorerForm::DuplicateTab()
  4905. {
  4906. if (OpenInNewWindow())
  4907. {
  4908. ExecuteNewInstance(CreateHiddenDuplicateSession());
  4909. }
  4910. else
  4911. {
  4912. // current working directories become defaults here, what is not right
  4913. std::unique_ptr<TSessionData> SessionData(CloneCurrentSessionData());
  4914. TTerminalManager * Manager = TTerminalManager::Instance();
  4915. TManagedTerminal * ASession = Manager->NewManagedTerminal(SessionData.get());
  4916. DebugAssert(ASession->LocalBrowser == ManagedSession->LocalBrowser);
  4917. // We definitelly want these
  4918. ASession->Disconnected = ManagedSession->Disconnected;
  4919. // Not sure about these two
  4920. ASession->Permanent = ManagedSession->Permanent;
  4921. ASession->DisconnectedTemporarily = ManagedSession->DisconnectedTemporarily;
  4922. Manager->ActiveSession = ASession;
  4923. }
  4924. }
  4925. //---------------------------------------------------------------------------
  4926. void __fastcall TCustomScpExplorerForm::RenameTab()
  4927. {
  4928. UnicodeString Name = ManagedSession->SessionData->SessionName;
  4929. if (InputDialog(LoadStr(RENAME_TAB_TITLE), LoadStr(RENAME_TAB_PROMPT), Name, HELP_TAB_RENAME) &&
  4930. // When submitting the default name, do not fix it
  4931. (Name != ManagedSession->SessionData->SessionName))
  4932. {
  4933. // Checks for a slash only, so it's not that big deal that we do the check after submitting the dialog only.
  4934. TSessionData::ValidateName(Name);
  4935. // This is inconsistent with how color (for example) is handled.
  4936. ManagedSession->SessionData->Name = Name;
  4937. UpdateControls();
  4938. // Add/Remove distinguishing paths from sessions of the same name.
  4939. SessionListChanged();
  4940. }
  4941. }
  4942. //---------------------------------------------------------------------------
  4943. bool __fastcall TCustomScpExplorerForm::CanCloseQueue(TTerminalQueue * Queue)
  4944. {
  4945. DebugAssert(Queue != NULL);
  4946. bool Result = Queue->IsEmpty;
  4947. if (!Result)
  4948. {
  4949. SetFocus();
  4950. Result = (MessageDialog(LoadStr(PENDING_QUEUE_ITEMS2), qtWarning, qaOK | qaCancel, HELP_NONE) == qaOK);
  4951. }
  4952. return Result;
  4953. }
  4954. //---------------------------------------------------------------------------
  4955. bool __fastcall TCustomScpExplorerForm::CanCloseQueue()
  4956. {
  4957. return CanCloseQueue(FQueue);
  4958. }
  4959. //---------------------------------------------------------------------------
  4960. void __fastcall TCustomScpExplorerForm::CloseTab()
  4961. {
  4962. if (CanCloseQueue())
  4963. {
  4964. TTerminalManager::Instance()->FreeActiveTerminal();
  4965. }
  4966. }
  4967. //---------------------------------------------------------------------------
  4968. void __fastcall TCustomScpExplorerForm::DisconnectSession()
  4969. {
  4970. if (CanCloseQueue())
  4971. {
  4972. TTerminalManager::Instance()->DisconnectActiveTerminal();
  4973. }
  4974. }
  4975. //---------------------------------------------------------------------------
  4976. void __fastcall TCustomScpExplorerForm::TerminalDisconnected()
  4977. {
  4978. DetachTerminal(Terminal);
  4979. RemoteDirView->Terminal = Terminal;
  4980. UpdateRemotePathComboBox(false);
  4981. InitStatusBar();
  4982. UpdateControls();
  4983. }
  4984. //---------------------------------------------------------------------------
  4985. void __fastcall TCustomScpExplorerForm::TerminalConnecting()
  4986. {
  4987. InitStatusBar();
  4988. }
  4989. //---------------------------------------------------------------------------
  4990. void __fastcall TCustomScpExplorerForm::ReconnectSession()
  4991. {
  4992. if (DebugAlwaysTrue(!Terminal->Active))
  4993. {
  4994. TTerminalManager::Instance()->ReconnectActiveTerminal();
  4995. }
  4996. }
  4997. //---------------------------------------------------------------------------
  4998. void __fastcall TCustomScpExplorerForm::OpenStoredSession(TSessionData * Data)
  4999. {
  5000. if (OpenInNewWindow())
  5001. {
  5002. // encode session name because of slashes in hierarchical sessions
  5003. ExecuteNewInstance(EncodeUrlString(Data->Name));
  5004. }
  5005. else
  5006. {
  5007. TTerminalManager * Manager = TTerminalManager::Instance();
  5008. TManagedTerminal * Terminal = Manager->NewManagedTerminal(Data);
  5009. Manager->ActiveSession = Terminal;
  5010. }
  5011. }
  5012. //---------------------------------------------------------------------------
  5013. void TCustomScpExplorerForm::DoOpenFolderOrWorkspace(
  5014. const UnicodeString & Name, bool ConnectFirstTerminal, bool CheckMaxSessions)
  5015. {
  5016. TTerminalManager * Manager = TTerminalManager::Instance();
  5017. std::unique_ptr<TObjectList> DataList(new TObjectList());
  5018. // This asks for a master password. Maybe we should postpone asking until a connection is actually opened.
  5019. StoredSessions->GetFolderOrWorkspace(Name, DataList.get());
  5020. if (!CheckMaxSessions || (DataList->Count <= WinConfiguration->MaxSessions))
  5021. {
  5022. TManagedTerminal * FirstSession = Manager->NewSessions(DataList.get());
  5023. // FirstSession can be null, if none of the workspace sites exist anymore
  5024. // or if all workspace sessions are not suported by the interface
  5025. // (when trying to open all local-local workspace in "explorer" interface)
  5026. if (FirstSession != NULL)
  5027. {
  5028. if (!ConnectFirstTerminal)
  5029. {
  5030. FirstSession->Disconnected = true;
  5031. FirstSession->DisconnectedTemporarily = true;
  5032. }
  5033. Manager->ActiveSession = FirstSession;
  5034. }
  5035. }
  5036. }
  5037. //---------------------------------------------------------------------------
  5038. void __fastcall TCustomScpExplorerForm::OpenFolderOrWorkspace(const UnicodeString & Name)
  5039. {
  5040. if (OpenInNewWindow())
  5041. {
  5042. ExecuteNewInstance(Name);
  5043. }
  5044. else
  5045. {
  5046. DoOpenFolderOrWorkspace(Name, true, false);
  5047. }
  5048. }
  5049. //---------------------------------------------------------------------------
  5050. void __fastcall TCustomScpExplorerForm::FormCloseQuery(TObject * /*Sender*/,
  5051. bool &CanClose)
  5052. {
  5053. // See the comment in CloseApp()
  5054. if (NonVisualDataModule->Busy)
  5055. {
  5056. CanClose = false;
  5057. }
  5058. else
  5059. {
  5060. TTerminalManager * Manager = TTerminalManager::Instance();
  5061. int ActiveSessions = 0;
  5062. for (int Index = 0; Index < Manager->Count; Index++)
  5063. {
  5064. if (Manager->Sessions[Index]->Active)
  5065. {
  5066. ActiveSessions++;
  5067. }
  5068. }
  5069. // Confirm, when there's at least one active remote session or
  5070. // when there are multiple (inactive or local) sessions and auto workspace saving is not enabled.
  5071. if (((ActiveSessions > 0) || ((Manager->Count > 1) && !WinConfiguration->AutoSaveWorkspace)) &&
  5072. WinConfiguration->ConfirmClosingSession)
  5073. {
  5074. unsigned int Result;
  5075. TMessageParams Params(mpNeverAskAgainCheck);
  5076. UnicodeString Message;
  5077. int Answers = qaOK | qaCancel;
  5078. // The current session is the only active session
  5079. if ((ActiveSessions == 1) && (Terminal != NULL) && Terminal->Active)
  5080. {
  5081. if (!WinConfiguration->AutoSaveWorkspace)
  5082. {
  5083. Message = LoadStr(CLOSE_SESSION_WORKSPACE);
  5084. }
  5085. else
  5086. {
  5087. Message = LoadStr(CLOSE_SESSION);
  5088. }
  5089. Message = FORMAT(Message, (Terminal->SessionData->SessionName));
  5090. }
  5091. // Multiple active sessions or one active session, but it's not the current one
  5092. else if (ActiveSessions > 0)
  5093. {
  5094. if (!WinConfiguration->AutoSaveWorkspace)
  5095. {
  5096. Message = LoadStr(CLOSE_SESSIONS_WORKSPACE3);
  5097. }
  5098. else
  5099. {
  5100. Message = LoadStr(CLOSE_SESSIONS);
  5101. }
  5102. }
  5103. // Multiple tabs, but all are inactive or local, and saving of worspace is not enabled
  5104. else
  5105. {
  5106. DebugAlwaysTrue(Manager->Count > 1);
  5107. DebugAlwaysTrue(!WinConfiguration->AutoSaveWorkspace);
  5108. Message = LoadStr(CLOSE_WORKSPACE);
  5109. }
  5110. UnicodeString Note;
  5111. if (WinConfiguration->AutoSaveWorkspace)
  5112. {
  5113. Note = FMTLOAD(AUTO_WORKSPACE, (WorkspaceName()));
  5114. }
  5115. else
  5116. {
  5117. Note = LoadStr(AUTO_WORKSPACE_ENABLE);
  5118. Answers = qaYes | qaNo | qaCancel;
  5119. }
  5120. Message = FORMAT("%s\n\n%s", (MainInstructions(Message), Note));
  5121. SetFocus();
  5122. Result = MessageDialog(Message, qtConfirmation,
  5123. Answers, HELP_CLOSE_SESSION_WORKSPACE, &Params);
  5124. if (Result == qaNeverAskAgain)
  5125. {
  5126. WinConfiguration->ConfirmClosingSession = false;
  5127. }
  5128. if (Result == qaNo)
  5129. {
  5130. CanClose = SaveWorkspace(true);
  5131. // note that the workspace will be saved redundatly again from FormClose
  5132. }
  5133. else
  5134. {
  5135. CanClose =
  5136. (Result == qaOK) ||
  5137. (Result == qaYes) || // CLOSE_SESSIONS_WORKSPACE variant
  5138. (Result == qaNeverAskAgain);
  5139. }
  5140. }
  5141. if (CanClose && HasActiveTerminal())
  5142. {
  5143. CanClose = CanCloseQueue();
  5144. }
  5145. }
  5146. if (CanClose)
  5147. {
  5148. CanClose =
  5149. FEditorManager->CloseInternalEditors(CloseInternalEditor) &&
  5150. FEditorManager->CloseExternalFilesWithoutProcess();
  5151. if (CanClose)
  5152. {
  5153. while (CanClose && (FLocalEditors->Count > 0))
  5154. {
  5155. int PrevCount = FLocalEditors->Count;
  5156. static_cast<TForm *>(FLocalEditors->Items[0])->Close();
  5157. CanClose = (FLocalEditors->Count < PrevCount);
  5158. }
  5159. if (CanClose)
  5160. {
  5161. CanClose = FEditorManager->Empty(true);
  5162. if (!CanClose)
  5163. {
  5164. SetFocus();
  5165. CanClose =
  5166. (MessageDialog(
  5167. LoadStr(PENDING_EDITORS), qtWarning, qaIgnore | qaCancel, HELP_NONE) == qaIgnore);
  5168. }
  5169. }
  5170. }
  5171. }
  5172. }
  5173. //---------------------------------------------------------------------------
  5174. void __fastcall TCustomScpExplorerForm::CloseInternalEditor(TObject * Sender)
  5175. {
  5176. TForm * Form = dynamic_cast<TForm *>(Sender);
  5177. DebugAssert(Form != NULL);
  5178. Form->Close();
  5179. }
  5180. //---------------------------------------------------------------------------
  5181. void __fastcall TCustomScpExplorerForm::ForceCloseInternalEditor(TObject * Sender)
  5182. {
  5183. TForm * Form = dynamic_cast<TForm *>(Sender);
  5184. delete Form;
  5185. }
  5186. //---------------------------------------------------------------------------
  5187. void __fastcall TCustomScpExplorerForm::ForceCloseLocalEditors()
  5188. {
  5189. while (FLocalEditors->Count > 0)
  5190. {
  5191. delete static_cast<TForm *>(FLocalEditors->Items[0]);
  5192. }
  5193. }
  5194. //---------------------------------------------------------------------------
  5195. void __fastcall TCustomScpExplorerForm::RemoteDirViewDisplayProperties(
  5196. TObject *Sender)
  5197. {
  5198. TStrings *FileList = ((TUnixDirView*)Sender)->CreateFileList(True, False, NULL);
  5199. try
  5200. {
  5201. SetProperties(osRemote, FileList);
  5202. }
  5203. __finally
  5204. {
  5205. delete FileList;
  5206. }
  5207. }
  5208. //---------------------------------------------------------------------------
  5209. void __fastcall TCustomScpExplorerForm::ComponentShowing(Byte Component, bool value)
  5210. {
  5211. if (value)
  5212. {
  5213. if (Component == fcCustomCommandsBand)
  5214. {
  5215. UpdateCustomCommandsToolbar();
  5216. }
  5217. if (Component == fcQueueFileList)
  5218. {
  5219. DebugAssert(!QueueFileListSplitter->Visible);
  5220. AdjustQueueLayout();
  5221. UpdateQueueFileList();
  5222. }
  5223. }
  5224. }
  5225. //---------------------------------------------------------------------------
  5226. void __fastcall TCustomScpExplorerForm::AdjustQueueLayout()
  5227. {
  5228. int MinHeightLimit =
  5229. GetStaticQueuePanelComponentsHeight() +
  5230. QueueFileListSplitter->Height +
  5231. QueueFileList->Height +
  5232. GetMinQueueViewHeight();
  5233. if (QueuePanel->ClientHeight < MinHeightLimit)
  5234. {
  5235. QueuePanel->ClientHeight = MinHeightLimit;
  5236. }
  5237. }
  5238. //---------------------------------------------------------------------------
  5239. void __fastcall TCustomScpExplorerForm::SetComponentVisible(Byte Component, Boolean value)
  5240. {
  5241. TControl * Control = GetComponent(Component);
  5242. DebugAssert(Control);
  5243. bool Changed = (Control->Visible != value);
  5244. if (Changed)
  5245. {
  5246. ComponentShowing(Component, value);
  5247. TWinControl * WinControl = dynamic_cast<TWinControl*>(Control);
  5248. bool WasFocused = (WinControl != NULL) && (ActiveControl != NULL) &&
  5249. ((ActiveControl == WinControl) || (ActiveControl->Parent == WinControl));
  5250. if (value)
  5251. {
  5252. int RemainingHeight = Control->Parent->ClientHeight;
  5253. int RemainingWidth = Control->Parent->ClientWidth;
  5254. for (int i = 0; i < Control->Parent->ControlCount; i++)
  5255. {
  5256. TControl * ChildControl = Control->Parent->Controls[i];
  5257. if (ChildControl->Visible)
  5258. {
  5259. switch (ChildControl->Align)
  5260. {
  5261. case alTop:
  5262. case alBottom:
  5263. RemainingHeight -= ChildControl->Height;
  5264. break;
  5265. case alLeft:
  5266. case alRight:
  5267. RemainingWidth -= ChildControl->Width;
  5268. break;
  5269. }
  5270. }
  5271. }
  5272. int Reserve = ScaleByTextHeight(this, 32);
  5273. // queue in explorer, trees in commander
  5274. if (Control->Height > RemainingHeight - Reserve)
  5275. {
  5276. Control->Height = RemainingHeight / 2;
  5277. }
  5278. if (Control->Width > RemainingWidth - Reserve)
  5279. {
  5280. Control->Width = RemainingWidth / 2;
  5281. }
  5282. }
  5283. Control->Visible = value;
  5284. if (WasFocused && Visible)
  5285. {
  5286. DirView(osCurrent)->SetFocus();
  5287. }
  5288. FixControlsPlacement();
  5289. }
  5290. }
  5291. //---------------------------------------------------------------------------
  5292. bool __fastcall TCustomScpExplorerForm::GetComponentVisible(Byte Component)
  5293. {
  5294. TControl * Control = GetComponent(Component);
  5295. if (Control == NULL)
  5296. {
  5297. return false;
  5298. }
  5299. else
  5300. {
  5301. return Control->Visible;
  5302. }
  5303. }
  5304. //---------------------------------------------------------------------------
  5305. bool __fastcall TCustomScpExplorerForm::IsComponentPossible(Byte Component)
  5306. {
  5307. bool Result;
  5308. if ((Component == fcExplorerUpdatesBand) || (Component == fcCommanderUpdatesBand))
  5309. {
  5310. Result = !IsUWP();
  5311. }
  5312. else
  5313. {
  5314. Result = true;
  5315. }
  5316. return Result;
  5317. }
  5318. //---------------------------------------------------------------------------
  5319. void __fastcall TCustomScpExplorerForm::MakeFocusedItemVisible(TCustomDirView * DirView)
  5320. {
  5321. if (DirView->ItemFocused != NULL)
  5322. {
  5323. DirView->ItemFocused->MakeVisible(false);
  5324. }
  5325. }
  5326. //---------------------------------------------------------------------------
  5327. void __fastcall TCustomScpExplorerForm::FixControlsPlacement()
  5328. {
  5329. MakeFocusedItemVisible(RemoteDirView);
  5330. QueueSplitter->Visible = QueuePanel->Visible;
  5331. QueueFileListSplitter->Visible = QueueFileList->Visible;
  5332. RemotePanelSplitter->Visible = RemoteDrivePanel->Visible;
  5333. TControl * TopControlsOrder[] =
  5334. { TopDock, MessageDock, SessionsPageControl };
  5335. SetVerticalControlsOrder(TopControlsOrder, LENOF(TopControlsOrder));
  5336. TControl * QueueControlsOrder[] =
  5337. { QueueDock, QueueView3, QueueFileListSplitter, QueueFileList };
  5338. SetVerticalControlsOrder(QueueControlsOrder, LENOF(QueueControlsOrder));
  5339. }
  5340. //---------------------------------------------------------------------------
  5341. TControl * __fastcall TCustomScpExplorerForm::GetComponent(Byte Component)
  5342. {
  5343. switch (Component) {
  5344. case fcStatusBar: return RemoteStatusBar;
  5345. case fcRemotePopup: return reinterpret_cast<TControl *>(NonVisualDataModule->RemoteFilePopup);
  5346. case fcQueueView: return QueuePanel;
  5347. case fcQueueToolbar: return QueueDock;
  5348. case fcRemoteTree: return RemoteDrivePanel;
  5349. case fcSessionsTabs: return SessionsPageControl;
  5350. case fcQueueFileList: return QueueFileList;
  5351. default: return NULL;
  5352. }
  5353. }
  5354. //---------------------------------------------------------------------------
  5355. void __fastcall TCustomScpExplorerForm::DirViewColumnRightClick(
  5356. TObject *Sender, TListColumn *Column, TPoint &Point)
  5357. {
  5358. DebugAssert(NonVisualDataModule && Column && Sender);
  5359. NonVisualDataModule->ListColumn = Column;
  5360. TPopupMenu * DirViewColumnMenu;
  5361. if (Sender == DirView(osOther))
  5362. {
  5363. DirViewColumnMenu = NonVisualDataModule->RemoteDirViewColumnPopup;
  5364. NonVisualDataModule->RemoteSortByExtColumnPopupItem->Visible = (Column->Index == uvName);
  5365. bool SizeColumn = (Column->Index == uvSize);
  5366. NonVisualDataModule->RemoteFormatSizeBytesPopupItem->Visible = SizeColumn;
  5367. NonVisualDataModule->RemoteCalculateDirectorySizesPopupItem->Visible = SizeColumn;
  5368. }
  5369. else
  5370. {
  5371. DirViewColumnMenu = NonVisualDataModule->LocalDirViewColumnPopup;
  5372. NonVisualDataModule->LocalSortByExtColumnPopupItem->Visible = (Column->Index == dvName);
  5373. bool SizeColumn = (Column->Index == dvSize);
  5374. NonVisualDataModule->LocalFormatSizeBytesPopupItem->Visible = SizeColumn;
  5375. NonVisualDataModule->LocalCalculateDirectorySizesPopupItem->Visible = SizeColumn;
  5376. }
  5377. TCustomListView * ListView = DebugNotNull(dynamic_cast<TCustomListView *>(Sender));
  5378. TPoint ScreenPoint = ListView->ClientToScreen(Point);
  5379. ScreenPoint.x -= GetScrollPos(ListView->Handle, SB_HORZ);
  5380. DirViewColumnMenu->Popup(ScreenPoint.x, ScreenPoint.y);
  5381. }
  5382. //---------------------------------------------------------------------------
  5383. void __fastcall TCustomScpExplorerForm::DirViewExecFile(
  5384. TObject *Sender, TListItem *Item, bool &AllowExec)
  5385. {
  5386. DoDirViewExecFile(Sender, Item, AllowExec);
  5387. }
  5388. //---------------------------------------------------------------------------
  5389. void __fastcall TCustomScpExplorerForm::DoDirViewExecFile(TObject * Sender,
  5390. TListItem * Item, bool & AllowExec)
  5391. {
  5392. DebugAssert(Sender && Item && Configuration);
  5393. DebugAssert(AllowExec);
  5394. TCustomDirView * ADirView = (TCustomDirView *)Sender;
  5395. TOperationSide Side = ((ADirView == DirView(osRemote)) ? osRemote : osLocal);
  5396. bool Remote = !IsSideLocalBrowser(Side);
  5397. DebugAssert(Item == ADirView->ItemFocused);
  5398. bool IsDirectory = ADirView->ItemIsDirectory(Item);
  5399. TResolvedDoubleClickAction Action;
  5400. if (FForceExecution)
  5401. {
  5402. Action = IsDirectory ? rdcaChangeDir : rdcaOpen;
  5403. }
  5404. else
  5405. {
  5406. TTerminal * ATerminal = !IsSideLocalBrowser(Side) ? Terminal : NULL;
  5407. Action = WinConfiguration->ResolveDoubleClickAction(IsDirectory, ATerminal);
  5408. }
  5409. if (Action == rdcaCopy)
  5410. {
  5411. // To counter the lock in DirViewBusy (called from TCustomDirView.DoExecute)
  5412. UnlockWindow();
  5413. try
  5414. {
  5415. if (IsLocalBrowserMode())
  5416. {
  5417. LocalLocalCopy(foCopy, Side, true, !WinConfiguration->CopyOnDoubleClickConfirmation, false, 0);
  5418. }
  5419. else
  5420. {
  5421. ExecuteFileOperation(foCopy, Side, true, !WinConfiguration->CopyOnDoubleClickConfirmation);
  5422. }
  5423. }
  5424. __finally
  5425. {
  5426. LockWindow();
  5427. }
  5428. AllowExec = false;
  5429. }
  5430. else if (Action == rdcaEdit)
  5431. {
  5432. if (!Remote || !WinConfiguration->DisableOpenEdit)
  5433. {
  5434. ExecuteFile(Side, efDefaultEditor);
  5435. AllowExec = false;
  5436. }
  5437. }
  5438. else if (Action == rdcaOpen)
  5439. {
  5440. if (DebugAlwaysTrue(!IsDirectory) && Remote && !WinConfiguration->DisableOpenEdit)
  5441. {
  5442. ExecuteFile(osRemote, efShell);
  5443. AllowExec = false;
  5444. }
  5445. }
  5446. }
  5447. //---------------------------------------------------------------------------
  5448. bool __fastcall TCustomScpExplorerForm::GetHasDirView(TOperationSide Side)
  5449. {
  5450. return ((Side == osRemote) || (Side == osCurrent));
  5451. }
  5452. //---------------------------------------------------------------------------
  5453. void __fastcall TCustomScpExplorerForm::CompareDirectories()
  5454. {
  5455. DebugFail();
  5456. }
  5457. //---------------------------------------------------------------------------
  5458. void __fastcall TCustomScpExplorerForm::SynchronizeDirectories()
  5459. {
  5460. DebugFail();
  5461. }
  5462. //---------------------------------------------------------------------------
  5463. bool __fastcall TCustomScpExplorerForm::DoSynchronizeDirectories(
  5464. UnicodeString & LocalDirectory, UnicodeString & RemoteDirectory, int UseDefaults)
  5465. {
  5466. TSynchronizeParamType Params;
  5467. Params.LocalDirectory = LocalDirectory;
  5468. Params.RemoteDirectory = RemoteDirectory;
  5469. int UnusedParams =
  5470. (GUIConfiguration->SynchronizeParams &
  5471. (TTerminal::spPreviewChanges | TTerminal::spTimestamp | TTerminal::spNotByTime | TTerminal::spBySize |
  5472. TTerminal::spCaseSensitive));
  5473. Params.Params = GUIConfiguration->SynchronizeParams & ~UnusedParams;
  5474. Params.Options = GUIConfiguration->SynchronizeOptions;
  5475. bool SaveSettings = false;
  5476. TSynchronizeController Controller(&DoSynchronize, &DoSynchronizeInvalid,
  5477. &DoSynchronizeTooManyDirectories);
  5478. DebugAssert(FSynchronizeController == NULL);
  5479. FSynchronizeController = &Controller;
  5480. bool Result;
  5481. try
  5482. {
  5483. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  5484. int CopyParamAttrs = Terminal->UsableCopyParamAttrs(0).Upload;
  5485. int Options =
  5486. FLAGMASK(SynchronizeAllowSelectedOnly(), soAllowSelectedOnly);
  5487. DebugAssert(FOnFeedSynchronizeError == NULL);
  5488. DebugAssert(FOnSynchronizeAbort == NULL);
  5489. DebugAssert(FSynchronizeTerminal == NULL);
  5490. FSynchronizeTerminal = Terminal;
  5491. Result = DoSynchronizeDialog(Params, &CopyParam, Controller.StartStop,
  5492. SaveSettings, Options, CopyParamAttrs, GetSynchronizeOptions, SynchronizeSessionLog,
  5493. FOnFeedSynchronizeError, FOnSynchronizeAbort, SynchronizeInNewWindow, UseDefaults);
  5494. if (Result)
  5495. {
  5496. if (SaveSettings)
  5497. {
  5498. GUIConfiguration->SynchronizeParams = Params.Params | UnusedParams;
  5499. GUIConfiguration->SynchronizeOptions = Params.Options;
  5500. }
  5501. else
  5502. {
  5503. if (FLAGSET(GUIConfiguration->SynchronizeOptions, soSynchronizeAsk) &&
  5504. FLAGCLEAR(Params.Options, soSynchronizeAsk) &&
  5505. FLAGSET(Params.Options, soSynchronize))
  5506. {
  5507. GUIConfiguration->SynchronizeOptions =
  5508. (GUIConfiguration->SynchronizeOptions & ~soSynchronizeAsk) |
  5509. soSynchronize;
  5510. }
  5511. }
  5512. LocalDirectory = Params.LocalDirectory;
  5513. RemoteDirectory = Params.RemoteDirectory;
  5514. }
  5515. }
  5516. __finally
  5517. {
  5518. FSynchronizeController = NULL;
  5519. DebugAssert(FOnFeedSynchronizeError == NULL);
  5520. FOnFeedSynchronizeError = NULL;
  5521. DebugAssert(FOnSynchronizeAbort == NULL);
  5522. FOnSynchronizeAbort = NULL;
  5523. FSynchronizeTerminal = NULL;
  5524. }
  5525. return Result;
  5526. }
  5527. //---------------------------------------------------------------------------
  5528. void __fastcall TCustomScpExplorerForm::DoSynchronize(
  5529. TSynchronizeController * /*Sender*/, const UnicodeString LocalDirectory,
  5530. const UnicodeString RemoteDirectory, const TCopyParamType & CopyParam,
  5531. const TSynchronizeParamType & Params, TSynchronizeChecklist ** Checklist,
  5532. TSynchronizeOptions * Options, bool Full)
  5533. {
  5534. // Keep-up-to-date mode
  5535. if (Terminal->Status == ssOpened)
  5536. {
  5537. try
  5538. {
  5539. int PParams = Params.Params;
  5540. if (!Full)
  5541. {
  5542. PParams |= TTerminal::spNoRecurse | TTerminal::spUseCache | TTerminal::spDelayProgress;
  5543. if (FLAGSET(Params.Options, soRecurse))
  5544. {
  5545. PParams |= TTerminal::spSubDirs;
  5546. }
  5547. }
  5548. else
  5549. {
  5550. // if keepuptodate is non-recursive,
  5551. // full sync before has to be non-recursive as well
  5552. if (FLAGCLEAR(Params.Options, soRecurse))
  5553. {
  5554. PParams |= TTerminal::spNoRecurse;
  5555. }
  5556. }
  5557. Synchronize(LocalDirectory, RemoteDirectory, smRemote, CopyParam,
  5558. PParams, Checklist, Options);
  5559. }
  5560. catch (Exception & E)
  5561. {
  5562. if (!Terminal->Active)
  5563. {
  5564. ShowExtendedExceptionEx(Terminal, &E);
  5565. // Do not abort the "keep up to date" when the session was succesfully reconnected.
  5566. if (!Terminal->Active)
  5567. {
  5568. throw;
  5569. }
  5570. }
  5571. else
  5572. {
  5573. if (FLAGSET(Params.Options, soContinueOnError) &&
  5574. DebugAlwaysTrue(FOnFeedSynchronizeError != NULL))
  5575. {
  5576. // noop, already logged in MoreMessageDialog
  5577. }
  5578. else
  5579. {
  5580. // We get mostly EAbort here, so this is noop.
  5581. // Exception is an error when listing directories while looking for differences.
  5582. ShowExtendedExceptionEx(Terminal, &E);
  5583. throw;
  5584. }
  5585. }
  5586. }
  5587. }
  5588. }
  5589. //---------------------------------------------------------------------------
  5590. void __fastcall TCustomScpExplorerForm::DoSynchronizeInvalid(
  5591. TSynchronizeController * /*Sender*/, const UnicodeString Directory,
  5592. const UnicodeString ErrorStr)
  5593. {
  5594. if (!Directory.IsEmpty())
  5595. {
  5596. SimpleErrorDialog(FMTLOAD(WATCH_ERROR_DIRECTORY, (Directory)), ErrorStr);
  5597. }
  5598. else
  5599. {
  5600. SimpleErrorDialog(LoadStr(WATCH_ERROR_GENERAL), ErrorStr);
  5601. }
  5602. }
  5603. //---------------------------------------------------------------------------
  5604. void __fastcall TCustomScpExplorerForm::DoSynchronizeTooManyDirectories(
  5605. TSynchronizeController * /*Sender*/, int & MaxDirectories)
  5606. {
  5607. if (MaxDirectories < GUIConfiguration->MaxWatchDirectories)
  5608. {
  5609. MaxDirectories = GUIConfiguration->MaxWatchDirectories;
  5610. }
  5611. else
  5612. {
  5613. TMessageParams Params(mpNeverAskAgainCheck);
  5614. unsigned int Result = MessageDialog(
  5615. FMTLOAD(TOO_MANY_WATCH_DIRECTORIES, (MaxDirectories, MaxDirectories)),
  5616. qtConfirmation, qaYes | qaNo, HELP_TOO_MANY_WATCH_DIRECTORIES, &Params);
  5617. if ((Result == qaYes) || (Result == qaNeverAskAgain))
  5618. {
  5619. MaxDirectories *= 2;
  5620. if (Result == qaNeverAskAgain)
  5621. {
  5622. GUIConfiguration->MaxWatchDirectories = MaxDirectories;
  5623. }
  5624. }
  5625. else
  5626. {
  5627. Abort();
  5628. }
  5629. }
  5630. }
  5631. //---------------------------------------------------------------------------
  5632. void __fastcall TCustomScpExplorerForm::Synchronize(const UnicodeString LocalDirectory,
  5633. const UnicodeString RemoteDirectory, TSynchronizeMode Mode,
  5634. const TCopyParamType & CopyParam, int Params, TSynchronizeChecklist ** Checklist,
  5635. TSynchronizeOptions * Options)
  5636. {
  5637. // Keep-up-to-date mode
  5638. DebugAssert(!FAutoOperation);
  5639. void * BatchStorage;
  5640. BatchStart(BatchStorage);
  5641. FAutoOperation = true;
  5642. bool AnyOperation = false;
  5643. TDateTime StartTime = Now();
  5644. TSynchronizeChecklist * AChecklist = NULL;
  5645. try
  5646. {
  5647. FSynchronizeProgressForm = new TSynchronizeProgressForm(Application, true, -1);
  5648. if (FLAGCLEAR(Params, TTerminal::spDelayProgress))
  5649. {
  5650. FSynchronizeProgressForm->Start();
  5651. }
  5652. AChecklist = Terminal->SynchronizeCollect(LocalDirectory, RemoteDirectory,
  5653. static_cast<TTerminal::TSynchronizeMode>(Mode),
  5654. &CopyParam, Params | TTerminal::spNoConfirmation, TerminalSynchronizeDirectory,
  5655. Options);
  5656. SetTaskbarListProgressState(TBPF_NOPROGRESS);
  5657. SAFE_DESTROY(FSynchronizeProgressForm);
  5658. AnyOperation = (AChecklist->CheckedCount > 0);
  5659. if (AnyOperation)
  5660. {
  5661. TSynchronizeProgress SynchronizeProgress(AChecklist);
  5662. CreateProgressForm(&SynchronizeProgress);
  5663. try
  5664. {
  5665. Terminal->SynchronizeApply(
  5666. AChecklist, &CopyParam, Params | TTerminal::spNoConfirmation, TerminalSynchronizeDirectory,
  5667. SynchronizeProcessedItem, NULL, NULL, NULL);
  5668. }
  5669. __finally
  5670. {
  5671. DestroyProgressForm();
  5672. }
  5673. }
  5674. }
  5675. __finally
  5676. {
  5677. if (Checklist == NULL)
  5678. {
  5679. delete AChecklist;
  5680. }
  5681. else
  5682. {
  5683. *Checklist = AChecklist;
  5684. }
  5685. FAutoOperation = false;
  5686. SAFE_DESTROY(FSynchronizeProgressForm);
  5687. BatchEnd(BatchStorage);
  5688. ReloadLocalDirectory();
  5689. if (AnyOperation)
  5690. {
  5691. OperationComplete(StartTime);
  5692. }
  5693. }
  5694. }
  5695. //---------------------------------------------------------------------------
  5696. bool __fastcall TCustomScpExplorerForm::SynchronizeAllowSelectedOnly()
  5697. {
  5698. // can be called from command line
  5699. return Visible &&
  5700. ((DirView(osRemote)->SelCount > 0) ||
  5701. (HasDirView[osLocal] && (DirView(osLocal)->SelCount > 0)));
  5702. }
  5703. //---------------------------------------------------------------------------
  5704. void __fastcall TCustomScpExplorerForm::SynchronizeSessionLog(const UnicodeString & Message)
  5705. {
  5706. LogSynchronizeEvent(Terminal, Message);
  5707. }
  5708. //---------------------------------------------------------------------------
  5709. void __fastcall TCustomScpExplorerForm::GetSynchronizeOptions(
  5710. int Params, TSynchronizeOptions & Options)
  5711. {
  5712. DebugAssert(!IsLocalBrowserMode());
  5713. if (FLAGSET(Params, TTerminal::spSelectedOnly) && SynchronizeAllowSelectedOnly())
  5714. {
  5715. Options.Filter = new TStringList();
  5716. Options.Filter->CaseSensitive = false;
  5717. Options.Filter->Duplicates = Types::dupAccept;
  5718. if (DirView(osRemote)->SelCount > 0)
  5719. {
  5720. DirView(osRemote)->CreateFileList(false, false, Options.Filter);
  5721. }
  5722. if (HasDirView[osLocal] && (DirView(osLocal)->SelCount > 0))
  5723. {
  5724. DirView(osLocal)->CreateFileList(false, false, Options.Filter);
  5725. }
  5726. Options.Filter->Sort();
  5727. }
  5728. }
  5729. //---------------------------------------------------------------------------
  5730. UnicodeString __fastcall TCustomScpExplorerForm::SerializeCopyParamForCommandLine(const TCopyParamType * CopyParams)
  5731. {
  5732. TCopyParamType Defaults;
  5733. std::unique_ptr<THierarchicalStorage> ConfigStorage(Configuration->CreateConfigStorage());
  5734. ConfigStorage->AccessMode = smRead;
  5735. if (ConfigStorage->OpenSubKey(Configuration->ConfigurationSubKey, false))
  5736. {
  5737. GUIConfiguration->LoadCopyParam(ConfigStorage.get(), &Defaults);
  5738. }
  5739. ConfigStorage.reset(NULL);
  5740. std::unique_ptr<TStringList> Options(new TStringList());
  5741. std::unique_ptr<TOptionsStorage> OptionsStorage(new TOptionsStorage(Options.get(), true));
  5742. CopyParams->Save(OptionsStorage.get(), &Defaults);
  5743. UnicodeString Result;
  5744. if (Options->Count > 0)
  5745. {
  5746. Result =
  5747. FORMAT(L" %s%s", (TProgramParams::FormatSwitch(RAWTRANSFERSETTINGS_SWITCH), StringsToParams(Options.get())));
  5748. }
  5749. return Result;
  5750. }
  5751. //---------------------------------------------------------------------------
  5752. void __fastcall TCustomScpExplorerForm::SynchronizeInNewWindow(
  5753. const TSynchronizeParamType & Params, const TCopyParamType * CopyParams)
  5754. {
  5755. UnicodeString SessionName = CreateHiddenDuplicateSession();
  5756. UnicodeString AdditionalParams =
  5757. FORMAT(L"%s%s %s // \"%s\" \"%s\" %d %d", (
  5758. TProgramParams::FormatSwitch(DEFAULTS_SWITCH),
  5759. SerializeCopyParamForCommandLine(CopyParams),
  5760. TProgramParams::FormatSwitch(KEEP_UP_TO_DATE_SWITCH),
  5761. Params.LocalDirectory, Params.RemoteDirectory, Params.Params, Params.Options));
  5762. ExecuteNewInstance(SessionName, AdditionalParams);
  5763. }
  5764. //---------------------------------------------------------------------------
  5765. struct TSynchronizeParams
  5766. {
  5767. UnicodeString LocalDirectory;
  5768. UnicodeString RemoteDirectory;
  5769. TSynchronizeMode Mode;
  5770. TSynchronizeChecklist * Checklist;
  5771. int Params;
  5772. TCopyParamType * CopyParam;
  5773. TDateTime CollectElapsed;
  5774. TDateTime * StartTime;
  5775. TProcessedSynchronizationChecklistItem OnProcessedItem;
  5776. };
  5777. //---------------------------------------------------------------------------
  5778. void __fastcall TCustomScpExplorerForm::FullSynchronize(
  5779. TSynchronizeParams & Params, TProcessedSynchronizationChecklistItem OnProcessedItem,
  5780. TUpdatedSynchronizationChecklistItems OnUpdatedSynchronizationChecklistItems)
  5781. {
  5782. DebugAssert(!FAutoOperation);
  5783. void * BatchStorage;
  5784. BatchStart(BatchStorage);
  5785. FAutoOperation = true;
  5786. TFileOperationStatistics Statistics;
  5787. TDateTime Start = Now();
  5788. try
  5789. {
  5790. Params.OnProcessedItem = OnProcessedItem;
  5791. TSynchronizeProgress SynchronizeProgress(Params.Checklist);
  5792. CreateProgressForm(&SynchronizeProgress);
  5793. Terminal->SynchronizeApply(
  5794. Params.Checklist, Params.CopyParam, Params.Params | TTerminal::spNoConfirmation,
  5795. TerminalSynchronizeDirectory, SynchronizeProcessedItem, OnUpdatedSynchronizationChecklistItems, &Params,
  5796. &Statistics);
  5797. }
  5798. __finally
  5799. {
  5800. Params.OnProcessedItem = NULL;
  5801. FAutoOperation = false;
  5802. DestroyProgressForm();
  5803. BatchEnd(BatchStorage);
  5804. ReloadLocalDirectory();
  5805. }
  5806. if (WinConfiguration->SynchronizeSummary)
  5807. {
  5808. UnicodeString Message = MainInstructions(LoadStr(SYNCHRONIZE_COMPLETE)) + L"\n";
  5809. // The statistics should be 0 anyway in this case
  5810. if (FLAGCLEAR(Params.Params, TTerminal::spTimestamp))
  5811. {
  5812. Message += L"\n";
  5813. if (Statistics.FilesUploaded > 0)
  5814. {
  5815. Message += FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 1), (FormatNumber(Statistics.FilesUploaded), FormatBytes(Statistics.TotalUploaded))) + L"\n";
  5816. }
  5817. if (Statistics.FilesDownloaded > 0)
  5818. {
  5819. Message += FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 2), (FormatNumber(Statistics.FilesDownloaded), FormatBytes(Statistics.TotalDownloaded))) + L"\n";
  5820. }
  5821. if (Statistics.FilesDeletedLocal > 0)
  5822. {
  5823. Message += FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 3), (FormatNumber(Statistics.FilesDeletedLocal))) + L"\n";
  5824. }
  5825. if (Statistics.FilesDeletedRemote > 0)
  5826. {
  5827. Message += FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 4), (FormatNumber(Statistics.FilesDeletedRemote))) + L"\n";
  5828. }
  5829. }
  5830. TDateTime Elapsed = (Now() - Start);
  5831. Message +=
  5832. L"\n" +
  5833. FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 5), (FormatDateTimeSpan(Configuration->TimeFormat, Params.CollectElapsed))) + L"\n" +
  5834. FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 6), (FormatDateTimeSpan(Configuration->TimeFormat, Elapsed)));
  5835. TMessageParams Params(mpNeverAskAgainCheck);
  5836. unsigned int Result = MessageDialog(Message, qtInformation, qaOK, HELP_NONE, &Params);
  5837. if (Result == qaNeverAskAgain)
  5838. {
  5839. WinConfiguration->SynchronizeSummary = false;
  5840. }
  5841. }
  5842. }
  5843. //---------------------------------------------------------------------------
  5844. void __fastcall TCustomScpExplorerForm::SynchronizeProcessedItem(void * Token, const TSynchronizeChecklist::TItem * ChecklistItem)
  5845. {
  5846. if (DebugAlwaysTrue(FProgressForm != NULL) && DebugAlwaysTrue(FProgressForm->SynchronizeProgress != NULL))
  5847. {
  5848. FProgressForm->SynchronizeProgress->ItemProcessed(ChecklistItem);
  5849. }
  5850. // Not set in keep-up-to-date mode - Synchronize() method
  5851. if (Token != NULL)
  5852. {
  5853. TSynchronizeParams & Params = *static_cast<TSynchronizeParams *>(Token);
  5854. if (Params.OnProcessedItem != NULL)
  5855. {
  5856. Params.OnProcessedItem(NULL, ChecklistItem);
  5857. }
  5858. }
  5859. }
  5860. //---------------------------------------------------------------------------
  5861. void __fastcall TCustomScpExplorerForm::DoFullSynchronize(
  5862. void * Token, TProcessedSynchronizationChecklistItem OnProcessedItem,
  5863. TUpdatedSynchronizationChecklistItems OnUpdatedSynchronizationChecklistItems)
  5864. {
  5865. TSynchronizeParams & Params = *static_cast<TSynchronizeParams *>(Token);
  5866. *Params.StartTime = Now();
  5867. FullSynchronize(Params, OnProcessedItem, OnUpdatedSynchronizationChecklistItems);
  5868. }
  5869. //---------------------------------------------------------------------------
  5870. void __fastcall TCustomScpExplorerForm::DoSynchronizeChecklistCalculateSize(
  5871. TSynchronizeChecklist * Checklist, const TSynchronizeChecklist::TItemList & Items, void * Token)
  5872. {
  5873. // terminal can be already closed (e.g. dropped connection)
  5874. if (Terminal != NULL)
  5875. {
  5876. TSynchronizeParams & Params = *static_cast<TSynchronizeParams *>(Token);
  5877. Terminal->SynchronizeChecklistCalculateSize(Checklist, Items, Params.CopyParam);
  5878. }
  5879. }
  5880. //---------------------------------------------------------------------------
  5881. void __fastcall TCustomScpExplorerForm::DoSynchronizeMove(
  5882. TOperationSide Side, const UnicodeString & FileName, const UnicodeString & NewFileName, TRemoteFile * RemoteFile)
  5883. {
  5884. DebugAssert(!IsLocalBrowserMode());
  5885. TAutoBatch AutoBatch(this);
  5886. TAutoFlag AutoOperationFlag(FAutoOperation);
  5887. if (Side == osRemote)
  5888. {
  5889. std::unique_ptr<TStrings> FileList(new TStringList());
  5890. FileList->AddObject(FileName, RemoteFile);
  5891. UnicodeString Target = UnixExtractFileDir(NewFileName);
  5892. UnicodeString FileMask = DelimitFileNameMask(UnixExtractFileName(NewFileName));
  5893. RemoteDirView->SaveSelection();
  5894. RemoteDirView->SaveSelectedNames();
  5895. try
  5896. {
  5897. Terminal->MoveFiles(FileList.get(), Target, FileMask, false);
  5898. }
  5899. catch(...)
  5900. {
  5901. RemoteDirView->DiscardSavedSelection();
  5902. throw;
  5903. }
  5904. RemoteDirView->RestoreSelection();
  5905. }
  5906. else if (DebugAlwaysTrue(Side == osLocal))
  5907. {
  5908. if (!MoveFile(FileName.c_str(), NewFileName.c_str()))
  5909. {
  5910. throw EOSExtException(FMTLOAD(RENAME_FILE_ERROR, (FileName, NewFileName)));
  5911. }
  5912. UnicodeString Directory = ExtractFileDir(FileName);
  5913. ReloadLocalDirectory(Directory);
  5914. UnicodeString NewDirectory = ExtractFileDir(NewFileName);
  5915. if (!SamePaths(Directory, NewDirectory))
  5916. {
  5917. ReloadLocalDirectory(NewDirectory);
  5918. }
  5919. }
  5920. }
  5921. //---------------------------------------------------------------------------
  5922. void __fastcall TCustomScpExplorerForm::DoSynchronizeBrowse(TOperationSide Side, TSynchronizeChecklist::TAction Action, const TSynchronizeChecklist::TItem * Item)
  5923. {
  5924. UnicodeString LocalPath = ExcludeTrailingBackslash(Item->Local.Directory);
  5925. if (Side == osLocal)
  5926. {
  5927. if (Action == TSynchronizeChecklist::saDownloadNew)
  5928. {
  5929. OpenFolderInExplorer(LocalPath);
  5930. }
  5931. else
  5932. {
  5933. OpenFileInExplorer(TPath::Combine(LocalPath, Item->GetFileName()));
  5934. }
  5935. }
  5936. else if (DebugAlwaysTrue(Side == osRemote))
  5937. {
  5938. // Similar to CreateHiddenDuplicateSession, except that it modifies the initial directories
  5939. std::unique_ptr<TSessionData> SessionData(CloneCurrentSessionData());
  5940. SessionData->RemoteDirectory = UnixExcludeTrailingBackslash(Item->Remote.Directory);
  5941. if (!LocalPath.IsEmpty())
  5942. {
  5943. SessionData->LocalDirectory = LocalPath;
  5944. }
  5945. UnicodeString SessionName = SaveHiddenDuplicateSession(SessionData.get());
  5946. ExecuteNewInstance(SessionName, FORMAT(L"%s=\"%s\"", (TProgramParams::FormatSwitch(BROWSE_SWITCH), Item->GetFileName())));
  5947. }
  5948. }
  5949. //---------------------------------------------------------------------------
  5950. void __fastcall TCustomScpExplorerForm::FullSynchronizeInNewWindow(
  5951. TSynchronizeMode Mode, int Params, const UnicodeString & LocalDirectory, const UnicodeString & RemoteDirectory,
  5952. const TCopyParamType * CopyParams)
  5953. {
  5954. UnicodeString SessionName = CreateHiddenDuplicateSession();
  5955. UnicodeString AdditionalParams =
  5956. FORMAT(L"%s%s %s // \"%s\" \"%s\" %d %d", (
  5957. TProgramParams::FormatSwitch(DEFAULTS_SWITCH),
  5958. SerializeCopyParamForCommandLine(CopyParams),
  5959. TProgramParams::FormatSwitch(SYNCHRONIZE_SWITCH),
  5960. LocalDirectory, RemoteDirectory, Mode, Params));
  5961. ExecuteNewInstance(SessionName, AdditionalParams);
  5962. }
  5963. //---------------------------------------------------------------------------
  5964. int __fastcall TCustomScpExplorerForm::DoFullSynchronizeDirectories(
  5965. UnicodeString & LocalDirectory, UnicodeString & RemoteDirectory,
  5966. TSynchronizeMode & Mode, int Params, bool & SaveMode, int UseDefaults)
  5967. {
  5968. int Result;
  5969. bool SaveSettings = false;
  5970. int Options =
  5971. FLAGMASK(!Terminal->IsCapable[fcTimestampChanging], fsoDisableTimestamp) |
  5972. FLAGMASK(SynchronizeAllowSelectedOnly(), fsoAllowSelectedOnly);
  5973. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  5974. TUsableCopyParamAttrs CopyParamAttrs = Terminal->UsableCopyParamAttrs(0);
  5975. bool Continue =
  5976. (UseDefaults == 0) ||
  5977. DoFullSynchronizeDialog(Mode, Params, LocalDirectory, RemoteDirectory,
  5978. &CopyParam, SaveSettings, SaveMode, Options, CopyParamAttrs, FullSynchronizeInNewWindow, UseDefaults);
  5979. if (Continue)
  5980. {
  5981. Configuration->Usage->Inc(L"Synchronizations");
  5982. CopyParam.IncludeFileMask.SetRoots(LocalDirectory, RemoteDirectory);
  5983. UpdateCopyParamCounters(CopyParam);
  5984. TSynchronizeOptions SynchronizeOptions;
  5985. GetSynchronizeOptions(Params, SynchronizeOptions);
  5986. if (SaveSettings)
  5987. {
  5988. GUIConfiguration->SynchronizeParams = Params;
  5989. }
  5990. else
  5991. {
  5992. SaveMode = false;
  5993. }
  5994. TDateTime StartTime = Now();
  5995. TSynchronizeChecklist * Checklist = NULL;
  5996. try
  5997. {
  5998. DebugAssert(!FAutoOperation);
  5999. FAutoOperation = true;
  6000. try
  6001. {
  6002. UnicodeString SessionKey = Terminal->SessionData->SessionKey;
  6003. std::unique_ptr<TStrings> DataList(Configuration->LoadDirectoryStatisticsCache(SessionKey, RemoteDirectory, CopyParam));
  6004. int Files = -1;
  6005. if (DataList->Count >= 1)
  6006. {
  6007. Files = StrToIntDef(DataList->Strings[0], Files);
  6008. }
  6009. else
  6010. {
  6011. DataList->Add(UnicodeString());
  6012. }
  6013. FSynchronizeProgressForm = new TSynchronizeProgressForm(Application, true, Files);
  6014. FSynchronizeProgressForm->Start();
  6015. Checklist = Terminal->SynchronizeCollect(LocalDirectory, RemoteDirectory,
  6016. static_cast<TTerminal::TSynchronizeMode>(Mode),
  6017. &CopyParam, Params | TTerminal::spNoConfirmation, TerminalSynchronizeDirectory,
  6018. &SynchronizeOptions);
  6019. if (Terminal->SessionData->CacheDirectories)
  6020. {
  6021. DataList->Strings[0] = IntToStr(SynchronizeOptions.Files);
  6022. Configuration->SaveDirectoryStatisticsCache(SessionKey, RemoteDirectory, CopyParam, DataList.get());
  6023. }
  6024. }
  6025. __finally
  6026. {
  6027. FAutoOperation = false;
  6028. SetTaskbarListProgressState(TBPF_NOPROGRESS);
  6029. SAFE_DESTROY(FSynchronizeProgressForm);
  6030. }
  6031. TSynchronizeParams SynchronizeParams;
  6032. SynchronizeParams.LocalDirectory = LocalDirectory;
  6033. SynchronizeParams.RemoteDirectory = RemoteDirectory;
  6034. SynchronizeParams.Mode = Mode;
  6035. SynchronizeParams.CopyParam = &CopyParam;
  6036. SynchronizeParams.Params = Params;
  6037. SynchronizeParams.Checklist = Checklist;
  6038. SynchronizeParams.CollectElapsed = (Now() - StartTime);
  6039. SynchronizeParams.StartTime = &StartTime;
  6040. SynchronizeParams.OnProcessedItem = NULL;
  6041. Result = Checklist->Count;
  6042. if (Checklist->Count > 0)
  6043. {
  6044. if (FLAGSET(Params, TTerminal::spPreviewChanges))
  6045. {
  6046. if (!DoSynchronizeChecklistDialog(
  6047. Checklist, Mode, Params, LocalDirectory, RemoteDirectory, CustomCommandMenu, DoFullSynchronize,
  6048. DoSynchronizeChecklistCalculateSize, DoSynchronizeMove, DoSynchronizeBrowse, &SynchronizeParams))
  6049. {
  6050. Result = -1;
  6051. }
  6052. }
  6053. else
  6054. {
  6055. FullSynchronize(SynchronizeParams, NULL, NULL);
  6056. }
  6057. }
  6058. else
  6059. {
  6060. UnicodeString Message = MainInstructions(LoadStr(COMPARE_NO_DIFFERENCES));
  6061. MessageDialog(Message, qtInformation, qaOK, HELP_SYNCHRONIZE_NO_DIFFERENCES);
  6062. }
  6063. }
  6064. __finally
  6065. {
  6066. delete Checklist;
  6067. }
  6068. OperationComplete(StartTime);
  6069. }
  6070. else
  6071. {
  6072. Result = -1;
  6073. }
  6074. return Result;
  6075. }
  6076. //---------------------------------------------------------------------------
  6077. void __fastcall TCustomScpExplorerForm::TerminalSynchronizeDirectory(
  6078. const UnicodeString & LocalDirectory, const UnicodeString & RemoteDirectory,
  6079. bool & Continue, bool Collect, const TSynchronizeOptions * Options)
  6080. {
  6081. if (Collect)
  6082. {
  6083. DebugAssert(FSynchronizeProgressForm != NULL);
  6084. if (!FSynchronizeProgressForm->Started)
  6085. {
  6086. FSynchronizeProgressForm->Start();
  6087. }
  6088. int CompareProgress = FSynchronizeProgressForm->SetData(LocalDirectory, RemoteDirectory, Options->Files, Continue);
  6089. SetTaskbarListProgressValue(CompareProgress);
  6090. }
  6091. else
  6092. {
  6093. DebugAssert(FSynchronizeProgressForm == NULL);
  6094. DebugAssert(FProgressForm != NULL);
  6095. }
  6096. }
  6097. //---------------------------------------------------------------------------
  6098. void __fastcall TCustomScpExplorerForm::StandaloneEdit(const UnicodeString & FileName)
  6099. {
  6100. UnicodeString FullFileName = AbsolutePath(Terminal->CurrentDirectory, FileName);
  6101. std::unique_ptr<TRemoteFile> File(Terminal->ReadFile(FullFileName));
  6102. if (File.get() != NULL)
  6103. {
  6104. ExecuteRemoteFile(FullFileName, File.get(), efInternalEditor);
  6105. Application->ShowMainForm = false;
  6106. Application->Run();
  6107. }
  6108. }
  6109. //---------------------------------------------------------------------------
  6110. void __fastcall TCustomScpExplorerForm::ExploreLocalDirectory(TOperationSide)
  6111. {
  6112. DebugFail();
  6113. }
  6114. //---------------------------------------------------------------------------
  6115. TSessionData * __fastcall TCustomScpExplorerForm::CloneCurrentSessionData()
  6116. {
  6117. std::unique_ptr<TSessionData> SessionData(new TSessionData(L""));
  6118. SessionData->Assign(ManagedSession->SessionData);
  6119. UpdateSessionData(SessionData.get());
  6120. DebugAssert(SessionData->IsLocalBrowser == IsLocalBrowserMode());
  6121. if (IsActiveTerminal(ManagedSession))
  6122. {
  6123. ManagedSession->UpdateSessionCredentials(SessionData.get());
  6124. }
  6125. if (!ManagedSession->SessionData->HasSessionName())
  6126. {
  6127. // Particularly for "Workspace/XXXX" name, we need to reset it, as it would become user-visible
  6128. // once IsWorkspace is cleared
  6129. SessionData->Name = UnicodeString();
  6130. }
  6131. // Most uses of this method ends with saving the session, and we do not want to store the IsWorkspace flag.
  6132. // Particularly, with SaveHiddenDuplicateSession, it won't work as TSessionData::ParseUrl explicitly ignored workspace sessions.
  6133. SessionData->IsWorkspace = false;
  6134. return SessionData.release();
  6135. }
  6136. //---------------------------------------------------------------------------
  6137. void __fastcall TCustomScpExplorerForm::SaveCurrentSession()
  6138. {
  6139. TSessionData * SessionData = CloneCurrentSessionData();
  6140. try
  6141. {
  6142. TSessionData * EditingSessionData = StoredSessions->FindSame(SessionData);
  6143. DoSaveSession(SessionData, EditingSessionData, true, NULL);
  6144. }
  6145. __finally
  6146. {
  6147. delete SessionData;
  6148. }
  6149. }
  6150. //---------------------------------------------------------------------------
  6151. TObjectList * __fastcall TCustomScpExplorerForm::DoCollectWorkspace()
  6152. {
  6153. TTerminalManager * Manager = TTerminalManager::Instance();
  6154. std::unique_ptr<TObjectList> DataList(new TObjectList());
  6155. if (DebugAlwaysTrue(ManagedSession != NULL))
  6156. {
  6157. // Update ManagedSession->StateData
  6158. UpdateSession(ManagedSession);
  6159. }
  6160. Manager->SaveWorkspace(DataList.get());
  6161. return DataList.release();
  6162. }
  6163. //---------------------------------------------------------------------------
  6164. void __fastcall TCustomScpExplorerForm::DoSaveWorkspace(
  6165. const UnicodeString & Name, TObjectList * DataList, bool SavePasswords, bool Explicit)
  6166. {
  6167. WinConfiguration->LastWorkspace = Name;
  6168. if (!SavePasswords)
  6169. {
  6170. for (int Index = 0; Index < DataList->Count; Index++)
  6171. {
  6172. TSessionData * SessionData = dynamic_cast<TSessionData *>(DataList->Items[Index]);
  6173. if (SessionData->Link.IsEmpty())
  6174. {
  6175. SessionData->ClearSessionPasswords();
  6176. }
  6177. }
  6178. }
  6179. StoredSessions->NewWorkspace(Name, DataList);
  6180. // modified only
  6181. StoredSessions->Save(false, Explicit);
  6182. }
  6183. //---------------------------------------------------------------------------
  6184. UnicodeString __fastcall TCustomScpExplorerForm::WorkspaceName()
  6185. {
  6186. return
  6187. DefaultStr(WinConfiguration->LastWorkspace,
  6188. DefaultStr(WinConfiguration->AutoWorkspace, LoadStr(NEW_WORKSPACE)));
  6189. }
  6190. //---------------------------------------------------------------------------
  6191. bool __fastcall TCustomScpExplorerForm::SaveWorkspace(bool EnableAutoSave)
  6192. {
  6193. std::unique_ptr<TObjectList> DataList(DoCollectWorkspace());
  6194. bool AnyNonStoredSessionWithPassword = false;
  6195. bool AnyNonStoredNonWorkspaceSessionWithPassword = false;
  6196. bool AllNonStoredSessionsAnonymous = true;
  6197. TTerminalManager * Manager = TTerminalManager::Instance();
  6198. for (int Index = 0; Index < DataList->Count; Index++)
  6199. {
  6200. TSessionData * SessionData = dynamic_cast<TSessionData *>(DataList->Items[Index]);
  6201. if (SessionData->Link.IsEmpty())
  6202. {
  6203. if (SessionData->HasAnySessionPassword())
  6204. {
  6205. AnyNonStoredSessionWithPassword = true;
  6206. if (!Manager->Sessions[Index]->SessionData->IsWorkspace)
  6207. {
  6208. AnyNonStoredNonWorkspaceSessionWithPassword = true;
  6209. }
  6210. }
  6211. if (!SameText(SessionData->UserName, AnonymousUserName))
  6212. {
  6213. AllNonStoredSessionsAnonymous = false;
  6214. }
  6215. }
  6216. }
  6217. bool SavePasswords;
  6218. bool * PSavePasswords;
  6219. bool NotRecommendedSavingPasswords =
  6220. !CustomWinConfiguration->UseMasterPassword &&
  6221. !AllNonStoredSessionsAnonymous;
  6222. if (Configuration->DisablePasswordStoring ||
  6223. !AnyNonStoredSessionWithPassword)
  6224. {
  6225. PSavePasswords = NULL;
  6226. SavePasswords = false;
  6227. }
  6228. else
  6229. {
  6230. PSavePasswords = &SavePasswords;
  6231. SavePasswords =
  6232. !AnyNonStoredNonWorkspaceSessionWithPassword ||
  6233. !NotRecommendedSavingPasswords;
  6234. }
  6235. UnicodeString Name = WorkspaceName();
  6236. bool CreateShortcut = false;
  6237. bool Result =
  6238. DoSaveWorkspaceDialog(
  6239. Name, PSavePasswords, NotRecommendedSavingPasswords, CreateShortcut,
  6240. EnableAutoSave);
  6241. if (Result)
  6242. {
  6243. DoSaveWorkspace(Name, DataList.get(), SavePasswords, true);
  6244. if (CreateShortcut)
  6245. {
  6246. TOperationVisualizer Visualizer;
  6247. UnicodeString AdditionalParams = TProgramParams::FormatSwitch(DESKTOP_SWITCH);
  6248. CreateDesktopSessionShortCut(Name, L"", AdditionalParams, -1, WORKSPACE_ICON);
  6249. }
  6250. if (EnableAutoSave)
  6251. {
  6252. WinConfiguration->AutoSaveWorkspace = true;
  6253. WinConfiguration->AutoWorkspace = Name;
  6254. if (PSavePasswords != NULL)
  6255. {
  6256. WinConfiguration->AutoSaveWorkspacePasswords = SavePasswords;
  6257. }
  6258. }
  6259. }
  6260. return Result;
  6261. }
  6262. //---------------------------------------------------------------------------
  6263. void __fastcall TCustomScpExplorerForm::UpdateSession(TManagedTerminal * Session)
  6264. {
  6265. DebugAssert(IsLocalBrowserMode() == Session->LocalBrowser);
  6266. DebugAssert(Session == ManagedSession);
  6267. if (!IsLocalBrowserMode())
  6268. {
  6269. SAFE_DESTROY(Session->RemoteExplorerState);
  6270. if (WinConfiguration->PreservePanelState)
  6271. {
  6272. Session->RemoteExplorerState = RemoteDirView->SaveState();
  6273. }
  6274. }
  6275. UpdateSessionData(Session->StateData);
  6276. }
  6277. //---------------------------------------------------------------------------
  6278. void __fastcall TCustomScpExplorerForm::UpdateSessionData(TSessionData * Data)
  6279. {
  6280. // Keep in sync with TSessionData::CopyStateData
  6281. DebugAssert(Data != NULL);
  6282. // This is inconsistent with how color (for example) is handled.
  6283. Data->Name = ManagedSession->SessionData->Name;
  6284. if (!IsLocalBrowserMode())
  6285. {
  6286. // cannot use RemoteDirView->Path, because it is empty if connection
  6287. // was already closed
  6288. // also only peek, we may not be connected at all atm
  6289. // so make sure we do not try retrieving current directory from the server
  6290. // (particularly with FTP)
  6291. UnicodeString ACurrentDirectory = Terminal->PeekCurrentDirectory();
  6292. if (!ACurrentDirectory.IsEmpty())
  6293. {
  6294. Data->RemoteDirectory = ACurrentDirectory;
  6295. }
  6296. }
  6297. Data->Color = SessionColor;
  6298. }
  6299. //---------------------------------------------------------------------------
  6300. void __fastcall TCustomScpExplorerForm::ToolBarResize(TObject *Sender)
  6301. {
  6302. TTBXToolbar * Toolbar = dynamic_cast<TTBXToolbar*>(Sender);
  6303. DebugAssert(Toolbar != NULL);
  6304. for (int i = 0; i < Toolbar->Items->Count; i++)
  6305. {
  6306. TTBXCustomDropDownItem * DropDownItem;
  6307. DropDownItem = dynamic_cast<TTBXCustomDropDownItem *>(Toolbar->Items->Items[i]);
  6308. if (DropDownItem != NULL)
  6309. {
  6310. ToolbarItemResize(DropDownItem,
  6311. Toolbar->Width - (Toolbar->View->BaseSize.x - DropDownItem->EditWidth) -
  6312. Toolbar->NonClientWidth);
  6313. break;
  6314. }
  6315. }
  6316. }
  6317. //---------------------------------------------------------------------------
  6318. void __fastcall TCustomScpExplorerForm::ToolbarItemResize(TTBXCustomDropDownItem * Item, int Width)
  6319. {
  6320. Item->EditWidth = Width;
  6321. }
  6322. //---------------------------------------------------------------------------
  6323. void __fastcall TCustomScpExplorerForm::ToolbarGetBaseSize(
  6324. TTBCustomToolbar * Toolbar, TPoint & ASize)
  6325. {
  6326. for (int i = 0; i < Toolbar->Items->Count; i++)
  6327. {
  6328. TTBXCustomDropDownItem * DropDownItem;
  6329. DropDownItem = dynamic_cast<TTBXCustomDropDownItem *>(Toolbar->Items->Items[i]);
  6330. if (DropDownItem != NULL)
  6331. {
  6332. ASize.x -= DropDownItem->EditWidth;
  6333. ASize.x += ScaleByTextHeight(this, 50) /* minimal combo width */;
  6334. }
  6335. }
  6336. }
  6337. //---------------------------------------------------------------------------
  6338. void __fastcall TCustomScpExplorerForm::DoWarnLackOfTempSpace(
  6339. const UnicodeString Path, __int64 RequiredSpace, bool & Continue)
  6340. {
  6341. if (WinConfiguration->DDWarnLackOfTempSpace)
  6342. {
  6343. UnicodeString ADrive = ExtractFileDrive(ExpandFileName(Path));
  6344. if (!ADrive.IsEmpty())
  6345. {
  6346. __int64 FreeSpace = DiskFree((Byte)(ADrive[1]-'A'+1));
  6347. DebugAssert(RequiredSpace >= 0);
  6348. __int64 RequiredWithReserve;
  6349. RequiredWithReserve = (__int64)(RequiredSpace * WinConfiguration->DDWarnLackOfTempSpaceRatio);
  6350. if (FreeSpace < RequiredWithReserve)
  6351. {
  6352. unsigned int Result;
  6353. TMessageParams Params(mpNeverAskAgainCheck);
  6354. Result = MessageDialog(FMTLOAD(DD_WARN_LACK_OF_TEMP_SPACE, (Path,
  6355. FormatBytes(FreeSpace), FormatBytes(RequiredSpace))),
  6356. qtWarning, qaYes | qaNo, HELP_DD_WARN_LACK_OF_TEMP_SPACE, &Params);
  6357. if (Result == qaNeverAskAgain)
  6358. {
  6359. WinConfiguration->DDWarnLackOfTempSpace = false;
  6360. }
  6361. Continue = (Result == qaYes || Result == qaNeverAskAgain);
  6362. }
  6363. }
  6364. }
  6365. }
  6366. //---------------------------------------------------------------------------
  6367. void __fastcall TCustomScpExplorerForm::AddBookmark(TOperationSide Side)
  6368. {
  6369. DoOpenDirectoryDialog(odAddBookmark, Side);
  6370. }
  6371. //---------------------------------------------------------------------------
  6372. TStrings * __fastcall TCustomScpExplorerForm::CreateVisitedDirectories(TOperationSide Side)
  6373. {
  6374. // we should better use TCustomDirView::FCaseSensitive, but it is private
  6375. TStringList * VisitedDirectories = CreateSortedStringList(!IsSideLocalBrowser(Side));
  6376. try
  6377. {
  6378. TCustomDirView * DView = DirView(Side);
  6379. for (int Index = -DView->BackCount; Index <= DView->ForwardCount; Index++)
  6380. {
  6381. VisitedDirectories->Add(DView->HistoryPath[Index]);
  6382. }
  6383. }
  6384. catch (...)
  6385. {
  6386. delete VisitedDirectories;
  6387. throw;
  6388. }
  6389. return VisitedDirectories;
  6390. }
  6391. //---------------------------------------------------------------------------
  6392. void __fastcall TCustomScpExplorerForm::DoOpenDirectoryDialog(
  6393. TOpenDirectoryMode Mode, TOperationSide Side)
  6394. {
  6395. bool Continue = true;
  6396. if (Mode == odAddBookmark)
  6397. {
  6398. TMessageParams Params(mpNeverAskAgainCheck);
  6399. Params.NeverAskAgainTitle = LoadStr(ADD_BOOKMARK_SHARED);
  6400. Params.NeverAskAgainCheckedInitially = WinConfiguration->UseSharedBookmarks;
  6401. UnicodeString Message = MainInstructions(FMTLOAD(ADD_BOOKMARK_CONFIRM, (DirView(Side)->PathName)));
  6402. unsigned int Answer =
  6403. MessageDialog(Message,
  6404. qtConfirmation, qaOK | qaCancel, HELP_ADD_BOOKMARK_CONFIRM, &Params);
  6405. if (Answer == qaNeverAskAgain)
  6406. {
  6407. Continue = true;
  6408. WinConfiguration->UseSharedBookmarks = true;
  6409. }
  6410. else if (Answer == qaOK)
  6411. {
  6412. Continue = true;
  6413. WinConfiguration->UseSharedBookmarks = false;
  6414. }
  6415. else
  6416. {
  6417. Continue = false;
  6418. }
  6419. }
  6420. if (Continue)
  6421. {
  6422. TStrings * VisitedDirectories = CreateVisitedDirectories(Side);
  6423. try
  6424. {
  6425. UnicodeString Name = DirView(Side)->PathName;
  6426. bool Repeat;
  6427. do
  6428. {
  6429. Repeat = false;
  6430. TOperationSide BookmarkSide = IsSideLocalBrowser(Side) ? osLocal : osRemote;
  6431. if (::DoOpenDirectoryDialog(Mode, BookmarkSide, Name, VisitedDirectories, Terminal,
  6432. // do not allow switching to location profiles,
  6433. // if we are not connected
  6434. HasDirView[osLocal] && (Terminal != NULL)))
  6435. {
  6436. TWindowLock Lock(this);
  6437. if (!TryOpenDirectory(Side, Name))
  6438. {
  6439. Repeat = true;
  6440. Mode = odBrowse;
  6441. }
  6442. }
  6443. }
  6444. while (Repeat);
  6445. }
  6446. __finally
  6447. {
  6448. delete VisitedDirectories;
  6449. }
  6450. }
  6451. }
  6452. //---------------------------------------------------------------------------
  6453. bool __fastcall TCustomScpExplorerForm::CommandSessionFallback()
  6454. {
  6455. DebugAssert(!Terminal->CommandSessionOpened);
  6456. return TTerminalManager::Instance()->ConnectTerminal(Terminal->CommandSession);
  6457. }
  6458. //---------------------------------------------------------------------------
  6459. bool __fastcall TCustomScpExplorerForm::EnsureCommandSessionFallback(TFSCapability Capability)
  6460. {
  6461. bool Result = Terminal->IsCapable[Capability] ||
  6462. Terminal->CommandSessionOpened;
  6463. if (!Result)
  6464. {
  6465. DebugAssert(Terminal->IsCapable[fcSecondaryShell]);
  6466. if (!GUIConfiguration->ConfirmCommandSession)
  6467. {
  6468. Result = true;
  6469. }
  6470. else
  6471. {
  6472. TMessageParams Params(mpNeverAskAgainCheck);
  6473. const TFileSystemInfo & FileSystemInfo = Terminal->GetFileSystemInfo();
  6474. unsigned int Answer = MessageDialog(FMTLOAD(PERFORM_ON_COMMAND_SESSION2,
  6475. (FileSystemInfo.ProtocolName, FileSystemInfo.ProtocolName)), qtConfirmation,
  6476. qaOK | qaCancel, HELP_PERFORM_ON_COMMAND_SESSION, &Params);
  6477. if (Answer == qaNeverAskAgain)
  6478. {
  6479. GUIConfiguration->ConfirmCommandSession = false;
  6480. Result = true;
  6481. }
  6482. else if (Answer == qaOK)
  6483. {
  6484. Result = true;
  6485. }
  6486. }
  6487. if (Result)
  6488. {
  6489. Result = CommandSessionFallback();
  6490. }
  6491. }
  6492. return Result;
  6493. }
  6494. //---------------------------------------------------------------------------
  6495. void __fastcall TCustomScpExplorerForm::OpenConsole(UnicodeString Command)
  6496. {
  6497. if (EnsureCommandSessionFallback(fcAnyCommand))
  6498. {
  6499. DoConsoleDialog(Terminal, Command);
  6500. }
  6501. }
  6502. //---------------------------------------------------------------------------
  6503. void __fastcall TCustomScpExplorerForm::FileControlDDDragEnter(
  6504. TObject *Sender, _di_IDataObject /*DataObj*/, int /*grfKeyState*/,
  6505. const TPoint & /*Point*/, int & /*dwEffect*/, bool & Accept)
  6506. {
  6507. if (!IsSideLocalBrowser(osRemote) &&
  6508. IsFileControl(DropSourceControl, osRemote) &&
  6509. (FDDExtMapFile != NULL))
  6510. {
  6511. Accept = true;
  6512. }
  6513. FDDTargetControl = dynamic_cast<TControl*>(Sender);
  6514. DebugAssert(FDDTargetControl != NULL);
  6515. }
  6516. //---------------------------------------------------------------------------
  6517. void __fastcall TCustomScpExplorerForm::SessionsDDDragEnter(
  6518. _di_IDataObject DataObj, int KeyState,
  6519. const TPoint & Point, int & Effect, bool & Accept)
  6520. {
  6521. FileControlDDDragEnter(SessionsPageControl, DataObj, KeyState, Point, Effect, Accept);
  6522. }
  6523. //---------------------------------------------------------------------------
  6524. void __fastcall TCustomScpExplorerForm::QueueDDDragEnter(
  6525. _di_IDataObject DataObj, int KeyState,
  6526. const TPoint & Point, int & Effect, bool & Accept)
  6527. {
  6528. FileControlDDDragEnter(QueueView3, DataObj, KeyState, Point, Effect, Accept);
  6529. }
  6530. //---------------------------------------------------------------------------
  6531. void __fastcall TCustomScpExplorerForm::FileControlDDDragLeave(
  6532. TObject * Sender)
  6533. {
  6534. DebugUsedParam(Sender);
  6535. DebugAssert(FDDTargetControl == Sender);
  6536. FDDTargetControl = NULL;
  6537. }
  6538. //---------------------------------------------------------------------------
  6539. void __fastcall TCustomScpExplorerForm::SessionsDDDragLeave()
  6540. {
  6541. FileControlDDDragLeave(SessionsPageControl);
  6542. }
  6543. //---------------------------------------------------------------------------
  6544. void __fastcall TCustomScpExplorerForm::QueueDDDragLeave()
  6545. {
  6546. FileControlDDDragLeave(QueueView3);
  6547. }
  6548. //---------------------------------------------------------------------------
  6549. void __fastcall TCustomScpExplorerForm::AddEditLink(TOperationSide Side, bool Add)
  6550. {
  6551. DebugAssert(!IsSideLocalBrowser(Side));
  6552. DebugUsedParam(Side);
  6553. bool Edit = false;
  6554. TRemoteFile * File = NULL;
  6555. UnicodeString FileName;
  6556. UnicodeString PointTo;
  6557. bool SymbolicLink = true;
  6558. if (RemoteDirView->ItemFocused)
  6559. {
  6560. DebugAssert(RemoteDirView->ItemFocused->Data);
  6561. File = (TRemoteFile *)RemoteDirView->ItemFocused->Data;
  6562. Edit = !Add && File->IsSymLink && Terminal->SessionData->ResolveSymlinks;
  6563. if (Edit)
  6564. {
  6565. FileName = File->FileName;
  6566. PointTo = File->LinkTo;
  6567. }
  6568. else if (!File->IsParentDirectory)
  6569. {
  6570. PointTo = File->FileName;
  6571. }
  6572. }
  6573. if (DoSymlinkDialog(FileName, PointTo, osRemote, SymbolicLink, Edit,
  6574. Terminal->IsCapable[fcHardLink]))
  6575. {
  6576. Configuration->Usage->Inc(L"RemoteLinksCreated");
  6577. if (Edit)
  6578. {
  6579. DebugAssert(File->FileName == FileName);
  6580. int Params = dfNoRecursive;
  6581. Terminal->ExceptionOnFail = true;
  6582. try
  6583. {
  6584. Terminal->DeleteFile(L"", File, &Params);
  6585. }
  6586. __finally
  6587. {
  6588. Terminal->ExceptionOnFail = false;
  6589. }
  6590. }
  6591. Terminal->CreateLink(FileName, PointTo, SymbolicLink);
  6592. }
  6593. }
  6594. //---------------------------------------------------------------------------
  6595. bool __fastcall TCustomScpExplorerForm::CanAddEditLink(TOperationSide Side)
  6596. {
  6597. return
  6598. (IsSideLocalBrowser(Side) ||
  6599. (HasActiveTerminal() &&
  6600. Terminal->ResolvingSymlinks &&
  6601. Terminal->IsCapable[fcSymbolicLink]));
  6602. }
  6603. //---------------------------------------------------------------------------
  6604. bool __fastcall TCustomScpExplorerForm::LinkFocused()
  6605. {
  6606. return
  6607. !IsSideLocalBrowser(FCurrentSide) &&
  6608. (RemoteDirView->ItemFocused != NULL) &&
  6609. ((TRemoteFile *)RemoteDirView->ItemFocused->Data)->IsSymLink &&
  6610. Terminal->SessionData->ResolveSymlinks;
  6611. }
  6612. //---------------------------------------------------------------------------
  6613. void __fastcall TCustomScpExplorerForm::ExecuteCurrentFile()
  6614. {
  6615. DebugAssert(!WinConfiguration->DisableOpenEdit);
  6616. FForceExecution = true;
  6617. try
  6618. {
  6619. DirView(osCurrent)->ExecuteCurrentFile();
  6620. }
  6621. __finally
  6622. {
  6623. FForceExecution = false;
  6624. }
  6625. }
  6626. //---------------------------------------------------------------------------
  6627. void __fastcall TCustomScpExplorerForm::ExecuteCurrentFileWith(bool OnFocused)
  6628. {
  6629. TEditorData ExternalEditor;
  6630. ExternalEditor.Editor = edExternal;
  6631. bool Remember = false;
  6632. if (DoEditorPreferencesDialog(&ExternalEditor, Remember, epmAdHoc, !IsSideLocalBrowser(osCurrent)))
  6633. {
  6634. if (Remember)
  6635. {
  6636. TEditorList * EditorList = new TEditorList;
  6637. try
  6638. {
  6639. *EditorList = *WinConfiguration->EditorList;
  6640. bool Found = false;
  6641. int i = 0;
  6642. while (!Found && (i < EditorList->Count))
  6643. {
  6644. const TEditorPreferences * Editor = EditorList->Editors[i];
  6645. if ((Editor->Data->Editor == edExternal) &&
  6646. (Editor->Data->ExternalEditor == ExternalEditor.ExternalEditor))
  6647. {
  6648. Found = true;
  6649. }
  6650. i++;
  6651. }
  6652. if (!Found)
  6653. {
  6654. EditorList->Add(new TEditorPreferences(ExternalEditor));
  6655. WinConfiguration->EditorList = EditorList;
  6656. }
  6657. }
  6658. __finally
  6659. {
  6660. delete EditorList;
  6661. }
  6662. }
  6663. ExecuteFile(osCurrent, efExternalEditor, &ExternalEditor, true, OnFocused);
  6664. }
  6665. }
  6666. //---------------------------------------------------------------------------
  6667. void __fastcall TCustomScpExplorerForm::DetachTerminal(TObject * ATerminal)
  6668. {
  6669. if (FFileFindTerminal == ATerminal)
  6670. {
  6671. FFileFindTerminal = NULL;
  6672. HideFileFindDialog();
  6673. }
  6674. if (FClipboardTerminal == ATerminal)
  6675. {
  6676. ClipboardClear(); // implies ClipboardStop
  6677. }
  6678. if ((FOnSynchronizeAbort != NULL) &&
  6679. (ATerminal == FManagedSession))
  6680. {
  6681. FOnSynchronizeAbort(NULL);
  6682. }
  6683. }
  6684. //---------------------------------------------------------------------------
  6685. void __fastcall TCustomScpExplorerForm::TerminalRemoved(TObject * Sender)
  6686. {
  6687. FEditorManager->ProcessFiles(FileTerminalRemoved, Sender);
  6688. DetachTerminal(Sender);
  6689. }
  6690. //---------------------------------------------------------------------------
  6691. void __fastcall TCustomScpExplorerForm::FileTerminalRemoved(const UnicodeString FileName,
  6692. TEditedFileData * Data, TObject * /*Token*/, void * Arg)
  6693. {
  6694. TTerminal * Terminal = static_cast<TTerminal *>(Arg);
  6695. DebugAssert(Terminal != NULL);
  6696. if (Data->Terminal == Terminal)
  6697. {
  6698. Data->Terminal = NULL;
  6699. }
  6700. }
  6701. //---------------------------------------------------------------------------
  6702. TManagedTerminal * TCustomScpExplorerForm::GetReplacementForLastSession()
  6703. {
  6704. return NULL;
  6705. }
  6706. //---------------------------------------------------------------------------
  6707. void __fastcall TCustomScpExplorerForm::LastTerminalClosed()
  6708. {
  6709. UpdateControls();
  6710. SessionColor = TColor(0);
  6711. UpdateRemotePathComboBox(false);
  6712. NeedSession(false);
  6713. }
  6714. //---------------------------------------------------------------------------
  6715. void __fastcall TCustomScpExplorerForm::NeedSession(bool Startup)
  6716. {
  6717. try
  6718. {
  6719. // Cache, as the login dialog can change its value.
  6720. // See also DoShow. Also see TTerminalManager::NewSession.
  6721. bool ShowLogin = WinConfiguration->ShowLoginWhenNoSession;
  6722. try
  6723. {
  6724. if (ShowLogin)
  6725. {
  6726. bool ReloadSessions = !Startup;
  6727. TTerminalManager::Instance()->NewSession(L"", ReloadSessions, this, true);
  6728. }
  6729. }
  6730. __finally
  6731. {
  6732. // Do not terminate, if we are only starting up and we are not showing Login dialog
  6733. // (so there was no chance for the user to open any session yet)
  6734. if ((ShowLogin || !Startup) &&
  6735. !WinConfiguration->KeepOpenWhenNoSession &&
  6736. // this rules out the implicit Commander's local-local browser
  6737. ((Terminal == NULL) || (!Terminal->Active && !Terminal->Permanent)))
  6738. {
  6739. TerminateApplication();
  6740. }
  6741. }
  6742. }
  6743. catch (EAbort &)
  6744. {
  6745. // swallow
  6746. // The TTerminalManager does not expect the OnLastTerminalClose to throw without trying to connect.
  6747. // Also when called from TWinControl.UpdateShowing => CMShowingChanged, the showing is aborted on exception
  6748. }
  6749. }
  6750. //---------------------------------------------------------------------------
  6751. void __fastcall TCustomScpExplorerForm::SessionListChanged(bool ForceTruncationUpdate)
  6752. {
  6753. TTerminalManager * Manager = TTerminalManager::Instance();
  6754. DebugAssert(!Manager->Updating);
  6755. TAutoFlag UpdatingSessionTabsFlag(FUpdatingSessionTabs);
  6756. int ActiveSessionIndex = Manager->ActiveSessionIndex;
  6757. Configuration->Usage->SetMax(L"MaxOpenedSessions", Manager->Count);
  6758. SendMessage(SessionsPageControl->Handle, WM_SETREDRAW, 0, 0);
  6759. try
  6760. {
  6761. int PrevTabsWidth = SessionsPageControl->TotalTabsWidth();
  6762. while (SessionsPageControl->PageCount > Manager->Count + 1)
  6763. {
  6764. delete SessionsPageControl->Pages[SessionsPageControl->PageCount - 1];
  6765. }
  6766. for (int Index = 0; Index <= Manager->Count; Index++)
  6767. {
  6768. TThemeTabSheet * TabSheet;
  6769. if (Index >= SessionsPageControl->PageCount)
  6770. {
  6771. TabSheet = new TThemeTabSheet(SessionsPageControl);
  6772. TabSheet->PageControl = SessionsPageControl;
  6773. }
  6774. else
  6775. {
  6776. TabSheet = SessionsPageControl->Pages[Index];
  6777. }
  6778. bool IsSessionTab = (Index < Manager->Count);
  6779. if (IsSessionTab)
  6780. {
  6781. TTerminal * Terminal = Manager->Sessions[Index];
  6782. TabSheet->Tag = reinterpret_cast<int>(Terminal);
  6783. TabSheet->CaptionTruncation = tttEllipsis;
  6784. UpdateSessionTab(TabSheet);
  6785. }
  6786. else
  6787. {
  6788. TabSheet->Tag = 0; // not really needed
  6789. TabSheet->Shadowed = false;
  6790. TabSheet->Button = SupportsLocalBrowser() ? ttbDropDown : ttbNone;
  6791. TabSheet->CaptionTruncation = tttNoText;
  6792. // 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)
  6793. UpdateNewTabTab();
  6794. }
  6795. }
  6796. // Prevent flicker on the right side where scrolling buttons would be, unless really needed
  6797. if (ForceTruncationUpdate || (SessionsPageControl->TotalTabsWidth() != PrevTabsWidth))
  6798. {
  6799. SessionsPageControl->UpdateTabsCaptionTruncation();
  6800. }
  6801. }
  6802. __finally
  6803. {
  6804. SendMessage(SessionsPageControl->Handle, WM_SETREDRAW, 1, 0);
  6805. }
  6806. SessionsPageControl->ActivePageIndex = ActiveSessionIndex;
  6807. }
  6808. //---------------------------------------------------------------------------
  6809. UnicodeString TCustomScpExplorerForm::GetNewTabTabCaption()
  6810. {
  6811. return StripHotkey(StripTrailingPunctuation(NonVisualDataModule->NewTabAction->Caption));
  6812. }
  6813. //---------------------------------------------------------------------------
  6814. void __fastcall TCustomScpExplorerForm::UpdateNewTabTab()
  6815. {
  6816. TThemeTabSheet * TabSheet = SessionsPageControl->Pages[SessionsPageControl->PageCount - 1];
  6817. UnicodeString TabCaption;
  6818. if (WinConfiguration->SelectiveToolbarText)
  6819. {
  6820. TabCaption = GetNewTabTabCaption();
  6821. }
  6822. TabSheet->BaseCaption = TabCaption;
  6823. TabSheet->ImageIndex = GetNewTabTabImageIndex(osCurrent);
  6824. }
  6825. //---------------------------------------------------------------------------
  6826. TManagedTerminal * __fastcall TCustomScpExplorerForm::GetSessionTabSession(TTabSheet * TabSheet)
  6827. {
  6828. return reinterpret_cast<TManagedTerminal *>(TabSheet->Tag);
  6829. }
  6830. //---------------------------------------------------------------------------
  6831. UnicodeString TCustomScpExplorerForm::GetLocalBrowserSessionTitle(TManagedTerminal *)
  6832. {
  6833. DebugFail();
  6834. return UnicodeString();
  6835. }
  6836. //---------------------------------------------------------------------------
  6837. void TCustomScpExplorerForm::UpdateSessionTab(TThemeTabSheet * TabSheet)
  6838. {
  6839. if (DebugAlwaysTrue(TabSheet != NULL))
  6840. {
  6841. TManagedTerminal * ASession = GetSessionTabSession(TabSheet);
  6842. if (DebugAlwaysTrue(ASession != NULL))
  6843. {
  6844. TColor Color = (ASession == Terminal) ? FSessionColor : ASession->StateData->Color;
  6845. if (ASession->LocalBrowser)
  6846. {
  6847. TabSheet->ImageIndex = FLocalBrowserTabImageIndex;
  6848. }
  6849. else
  6850. {
  6851. TabSheet->ImageIndex = AddSessionColor(Color);
  6852. }
  6853. TTerminalManager * Manager = TTerminalManager::Instance();
  6854. TabSheet->Shadowed = !ASession->Active && !ASession->LocalBrowser;
  6855. TabSheet->Button = CanCloseSession(ASession) ? ttbClose : ttbNone;
  6856. TabSheet->BaseCaption = Manager->GetSessionTitle(ASession, true);
  6857. }
  6858. }
  6859. }
  6860. //---------------------------------------------------------------------------
  6861. bool TCustomScpExplorerForm::CanCloseSession(TManagedTerminal * Session)
  6862. {
  6863. return !Session->LocalBrowser || (TTerminalManager::Instance()->Count > 1);
  6864. }
  6865. //---------------------------------------------------------------------------
  6866. bool __fastcall TCustomScpExplorerForm::SessionTabSwitched()
  6867. {
  6868. DebugAssert(SessionsPageControl->ActivePage != NULL);
  6869. TManagedTerminal * Session = GetSessionTabSession(SessionsPageControl->ActivePage);
  6870. bool Result = (Session != NULL);
  6871. if (Result)
  6872. {
  6873. TTerminalManager::Instance()->ActiveSession = Session;
  6874. }
  6875. else
  6876. {
  6877. try
  6878. {
  6879. NewTab();
  6880. }
  6881. __finally
  6882. {
  6883. SessionListChanged();
  6884. }
  6885. FSessionsPageControlNewTabTime = Now();
  6886. }
  6887. return Result;
  6888. }
  6889. //---------------------------------------------------------------------------
  6890. void __fastcall TCustomScpExplorerForm::SessionsPageControlChange(TObject * /*Sender*/)
  6891. {
  6892. TWindowLock Lock(this);
  6893. SessionTabSwitched();
  6894. }
  6895. //---------------------------------------------------------------------------
  6896. void __fastcall TCustomScpExplorerForm::TransferListChange(TObject * Sender)
  6897. {
  6898. TTBXStringList * TransferList = dynamic_cast<TTBXStringList *>(Sender);
  6899. DebugAssert(TransferList != NULL);
  6900. UnicodeString Name;
  6901. if (TransferList->ItemIndex <= 0)
  6902. {
  6903. Name = L"";
  6904. }
  6905. else
  6906. {
  6907. Name = GUIConfiguration->CopyParamList->Names[TransferList->ItemIndex - 1];
  6908. }
  6909. if (FCopyParamAutoSelected.IsEmpty())
  6910. {
  6911. // if previous preset was not autoselected, make new preset the "default"
  6912. FCopyParamDefault = Name;
  6913. }
  6914. GUIConfiguration->CopyParamCurrent = Name;
  6915. }
  6916. //---------------------------------------------------------------------------
  6917. void __fastcall TCustomScpExplorerForm::UpdateTransferLabel()
  6918. {
  6919. // sanity check
  6920. bool ExistingPreset =
  6921. (FTransferListHoverIndex >= 0) &&
  6922. (FTransferListHoverIndex < 1 + GUIConfiguration->CopyParamList->Count);
  6923. DebugAssert(ExistingPreset);
  6924. if (ExistingPreset)
  6925. {
  6926. HDC DC = GetDC(0);
  6927. TCanvas * Canvas = new TCanvas();
  6928. try
  6929. {
  6930. Canvas->Handle = DC;
  6931. Canvas->Font = GetToolbarFont(this);
  6932. UnicodeString Name;
  6933. if (FTransferListHoverIndex == 0)
  6934. {
  6935. Name = L"";
  6936. }
  6937. else
  6938. {
  6939. Name = GUIConfiguration->CopyParamList->Names[FTransferListHoverIndex - 1];
  6940. }
  6941. TTBXLabelItem * TransferLabel = dynamic_cast<TTBXLabelItem*>(
  6942. static_cast<TComponent*>(GetComponent(fcTransferLabel)));
  6943. TTBXStringList * TransferList = dynamic_cast<TTBXStringList*>(
  6944. static_cast<TObject*>(GetComponent(fcTransferList)));
  6945. UnicodeString InfoStr =
  6946. GUIConfiguration->CopyParamPreset[Name].
  6947. GetInfoStr(L"; ",
  6948. FLAGMASK(HasActiveTerminal(), Terminal->UsableCopyParamAttrs(0).General));
  6949. int MaxWidth = TransferList->MinWidth - (2 * TransferLabel->Margin) - ScaleByTextHeight(this, 10);
  6950. if (Canvas->TextExtent(InfoStr).cx > MaxWidth)
  6951. {
  6952. while (Canvas->TextExtent(InfoStr + Ellipsis).cx > MaxWidth)
  6953. {
  6954. InfoStr.SetLength(InfoStr.Length() - 1);
  6955. }
  6956. InfoStr += Ellipsis;
  6957. }
  6958. // UpdateCaption does not cause invalidation of whole submenu, while
  6959. // setting Caption property does.
  6960. // also it probably does not resize the label, even if necessary
  6961. // (we do not want that anyway)
  6962. TransferLabel->UpdateCaption(InfoStr);
  6963. }
  6964. __finally
  6965. {
  6966. Canvas->Handle = NULL;
  6967. ReleaseDC(0, DC);
  6968. delete Canvas;
  6969. }
  6970. }
  6971. }
  6972. //---------------------------------------------------------------------------
  6973. void __fastcall TCustomScpExplorerForm::TransferListDrawItem(
  6974. TTBXCustomList * /*Sender*/, TCanvas * /*ACanvas*/, const TRect & /*ARect*/,
  6975. int /*AIndex*/, int AHoverIndex, bool & /*DrawDefault*/)
  6976. {
  6977. if (FTransferListHoverIndex != AHoverIndex)
  6978. {
  6979. FTransferListHoverIndex = AHoverIndex;
  6980. UpdateTransferLabel();
  6981. }
  6982. }
  6983. //---------------------------------------------------------------------------
  6984. void __fastcall TCustomScpExplorerForm::WMAppCommand(TMessage & Message)
  6985. {
  6986. int Command = GET_APPCOMMAND_LPARAM(Message.LParam);
  6987. TShiftState Shift = KeyDataToShiftState(GET_KEYSTATE_LPARAM(Message.LParam));
  6988. if ((Shift * (TShiftState() << ssShift << ssAlt << ssCtrl)).Empty())
  6989. {
  6990. if (Command == APPCOMMAND_BROWSER_FAVORITES)
  6991. {
  6992. if (!NonVisualDataModule->Busy)
  6993. {
  6994. OpenDirectory(GetSide(osCurrent));
  6995. }
  6996. Message.Result = 1;
  6997. }
  6998. else
  6999. {
  7000. TForm::Dispatch(&Message);
  7001. }
  7002. }
  7003. else
  7004. {
  7005. TForm::Dispatch(&Message);
  7006. }
  7007. }
  7008. //---------------------------------------------------------------------------
  7009. void __fastcall TCustomScpExplorerForm::CMDialogChar(TMessage & AMessage)
  7010. {
  7011. TCMDialogChar & Message = reinterpret_cast<TCMDialogChar &>(AMessage);
  7012. if ((FIgnoreNextDialogChar != 0) &&
  7013. (toupper(Message.CharCode) == toupper(FIgnoreNextDialogChar)))
  7014. {
  7015. Message.Result = 1;
  7016. }
  7017. else
  7018. {
  7019. TForm::Dispatch(&Message);
  7020. }
  7021. FIgnoreNextDialogChar = 0;
  7022. }
  7023. //---------------------------------------------------------------------------
  7024. void __fastcall TCustomScpExplorerForm::WMSysCommand(TMessage & Message)
  7025. {
  7026. // The four low-order bits are used internally by Windows
  7027. unsigned int Cmd = (Message.WParam & 0xFFF0);
  7028. // SC_RESTORE, SC_MAXIMIZE, SC_MINIMIZE - buttons on windows title
  7029. // SC_DEFAULT - double click on windows title (does not work, at least on WinXP)
  7030. // 61730 - restore thru double click - undocumented
  7031. // 61490 - maximize thru double click - undocumented
  7032. if ((Cmd == SC_RESTORE) || (Cmd == SC_MAXIMIZE) ||
  7033. (Cmd == SC_MINIMIZE) || (Cmd == SC_DEFAULT))
  7034. {
  7035. SysResizing(Cmd);
  7036. }
  7037. TForm::Dispatch(&Message);
  7038. }
  7039. //---------------------------------------------------------------------------
  7040. void __fastcall TCustomScpExplorerForm::WMQueryEndSession(TMessage & Message)
  7041. {
  7042. // We were actually never able to make ENDSESSION_CRITICAL happen.
  7043. // Also there no point returning TRUE as we are not able to
  7044. // handle the abrupt termination caused by subsequent WM_ENDSESSION cleanly.
  7045. // Hence the process termination might be safer :)
  7046. if ((Message.LParam != ENDSESSION_CRITICAL) &&
  7047. (((FQueue != NULL) && !FQueue->IsEmpty) || (FProgressForm != NULL)))
  7048. {
  7049. Message.Result = FALSE;
  7050. }
  7051. else
  7052. {
  7053. Message.Result = TRUE;
  7054. }
  7055. // Do not call default handling as that triggers OnCloseQuery,
  7056. // where our implementation will popup confirmation dialogs, what we do not want,
  7057. // as per Vista guidelines:
  7058. // https://learn.microsoft.com/en-us/windows/win32/shutdown/shutting-down
  7059. }
  7060. //---------------------------------------------------------------------------
  7061. void __fastcall TCustomScpExplorerForm::WMEndSession(TWMEndSession & Message)
  7062. {
  7063. if (Message.EndSession && IsApplicationMinimized())
  7064. {
  7065. // WORKAROUND
  7066. // TApplication.WndProc() calls Application.Terminate() before Halt(),
  7067. // when it receives WM_ENDSESSION,
  7068. // but that sometimes (particularly when application is minimized) cause crashes.
  7069. // Crash popups message that blocks log off.
  7070. // Obviously application cannot shutdown cleanly after WM_ENDSESSION,
  7071. // so we call ExitProcess() immediately, not even trying to cleanup.
  7072. // It still causes beep, so there's likely some popup, but it does not block
  7073. // log off.
  7074. ExitProcess(0);
  7075. }
  7076. TForm::Dispatch(&Message);
  7077. }
  7078. #ifdef _DEBUG
  7079. //---------------------------------------------------------------------------
  7080. void __fastcall TCustomScpExplorerForm::WMWindowPosChanged(TWMWindowPosMsg & Message)
  7081. {
  7082. TForm::Dispatch(&Message);
  7083. }
  7084. #endif
  7085. //---------------------------------------------------------------------------
  7086. void __fastcall TCustomScpExplorerForm::SysResizing(unsigned int /*Cmd*/)
  7087. {
  7088. }
  7089. //---------------------------------------------------------------------------
  7090. void __fastcall TCustomScpExplorerForm::DoShow()
  7091. {
  7092. // Called from TCustomScpExplorerForm::CMShowingChanged
  7093. // only now are the controls resized finally, so the size constraints
  7094. // will not conflict with possibly very small window size
  7095. RestoreFormParams();
  7096. // This indicates that the form was rescaled after parameters were loaded and
  7097. // as most probably the parameters were actually saved with the current form scaling
  7098. // (windows was the last time likely closed on the same monitor, where it is starting now),
  7099. // rescaling happened twice already. Reload the parameters, as now they likely won't need any scaling
  7100. // and no rounding problems will occur.
  7101. // See also RestoreForm.
  7102. // (we should be safe to call RestoreParams unconditionally)
  7103. if (PixelsPerInch != Screen->PixelsPerInch)
  7104. {
  7105. RestoreParams();
  7106. }
  7107. FixControlsPlacement();
  7108. if (Position == poDefaultPosOnly)
  7109. {
  7110. CutFormToDesktop(this);
  7111. }
  7112. TForm::DoShow();
  7113. FSessionsDragDropFilesEx->DragDropControl = SessionsPageControl;
  7114. FQueueDragDropFilesEx->DragDropControl = QueueView3;
  7115. // This was previously in NeedSession, where other related code is.
  7116. // But that is called from CMShowingChanged, when the window is already visible and the local-local window state
  7117. // unpleasantly shows briefly.
  7118. if ((ManagedSession == NULL) &&
  7119. !WinConfiguration->ShowLoginWhenNoSession &&
  7120. WinConfiguration->AutoSaveWorkspace && !WinConfiguration->AutoWorkspace.IsEmpty() &&
  7121. // This detects if workspace was saved the last time the main widow was closed
  7122. SameText(WinConfiguration->LastStoredSession, WinConfiguration->AutoWorkspace))
  7123. {
  7124. DoOpenFolderOrWorkspace(WinConfiguration->AutoWorkspace, false, true);
  7125. Configuration->Usage->Inc(L"OpenedWorkspacesAuto");
  7126. }
  7127. // Before main window displays
  7128. if (ManagedSession == NULL)
  7129. {
  7130. StartingWithoutSession();
  7131. }
  7132. UpdatePixelsPerInchMainWindowCounter();
  7133. FShowing = true;
  7134. }
  7135. //---------------------------------------------------------------------------
  7136. void __fastcall TCustomScpExplorerForm::UpdatePixelsPerInchMainWindowCounter()
  7137. {
  7138. Configuration->Usage->Set(L"PixelsPerInchMainWindow", PixelsPerInch);
  7139. }
  7140. //---------------------------------------------------------------------------
  7141. void __fastcall TCustomScpExplorerForm::StartingWithoutSession()
  7142. {
  7143. SessionListChanged();
  7144. InitStatusBar();
  7145. UpdateControls();
  7146. }
  7147. //---------------------------------------------------------------------------
  7148. void __fastcall TCustomScpExplorerForm::PopupTrayBalloon(TTerminal * Terminal,
  7149. const UnicodeString & Str, TQueryType Type, Exception * E, unsigned int Seconds,
  7150. TNotifyEvent OnBalloonClick, TObject * UserData)
  7151. {
  7152. bool Do;
  7153. UnicodeString Message;
  7154. if (E == NULL)
  7155. {
  7156. Message = Str;
  7157. Do = true;
  7158. }
  7159. else
  7160. {
  7161. Do = ExceptionMessage(E, Message);
  7162. }
  7163. if (Do && WinConfiguration->BalloonNotifications)
  7164. {
  7165. UnicodeString Title;
  7166. if ((Terminal == NULL) && (Type == qtInformation) && ExtractMainInstructions(Message, Title))
  7167. {
  7168. Message = TrimLeft(Message);
  7169. }
  7170. else
  7171. {
  7172. Message = UnformatMessage(Message);
  7173. const ResourceString * Captions[] = { &_SMsgDlgConfirm, &_SMsgDlgWarning,
  7174. &_SMsgDlgError, &_SMsgDlgInformation, NULL };
  7175. Title = LoadResourceString(Captions[Type]);
  7176. if (Terminal != NULL)
  7177. {
  7178. TManagedTerminal * Session = DebugNotNull(dynamic_cast<TManagedTerminal *>(Terminal));
  7179. Title = TTerminalManager::Instance()->GetSessionTitle(Session, true) + TitleSeparator + Title;
  7180. }
  7181. }
  7182. if (Seconds == 0)
  7183. {
  7184. Seconds = WinConfiguration->NotificationsTimeout;
  7185. }
  7186. FTrayIcon->PopupBalloon(Title, Message, Type, Seconds * MSecsPerSec, OnBalloonClick, UserData);
  7187. }
  7188. }
  7189. //---------------------------------------------------------------------------
  7190. unsigned int __fastcall TCustomScpExplorerForm::MoreMessageDialog(const UnicodeString Message,
  7191. TStrings * MoreMessages, TQueryType Type, unsigned int Answers,
  7192. UnicodeString HelpKeyword, const TMessageParams * Params,
  7193. TTerminal * Terminal)
  7194. {
  7195. if (((WinConfiguration->ContinueOnError && (FErrorList != NULL)) ||
  7196. (FOnFeedSynchronizeError != NULL)) &&
  7197. (Params != NULL) && (Params->Params & mpAllowContinueOnError) )
  7198. {
  7199. if (FOnFeedSynchronizeError != NULL)
  7200. {
  7201. FOnFeedSynchronizeError(Message, MoreMessages, Type, HelpKeyword);
  7202. }
  7203. else
  7204. {
  7205. DebugAssert(FErrorList != NULL);
  7206. TStringList * MoreMessagesCopy = NULL;
  7207. if (MoreMessages)
  7208. {
  7209. MoreMessagesCopy = new TStringList();
  7210. MoreMessagesCopy->Assign(MoreMessages);
  7211. }
  7212. FErrorList->AddObject(Message, MoreMessagesCopy);
  7213. }
  7214. PopupTrayBalloon(Terminal, Message, Type);
  7215. return ContinueAnswer(Answers);
  7216. }
  7217. else
  7218. {
  7219. bool UseBalloon = FTrayIcon->Visible;
  7220. if (UseBalloon)
  7221. {
  7222. PopupTrayBalloon(Terminal, Message, Type);
  7223. }
  7224. unsigned int Result;
  7225. try
  7226. {
  7227. Result = ::MoreMessageDialog(Message, MoreMessages, Type, Answers, HelpKeyword, Params);
  7228. }
  7229. __finally
  7230. {
  7231. // cancel only balloon we popped up, otherwise we may cancel notification
  7232. // balloon that was there before the message dialog
  7233. // (such as "dd confirmation opt in balloon)
  7234. if (UseBalloon)
  7235. {
  7236. FTrayIcon->CancelBalloon();
  7237. }
  7238. }
  7239. return Result;
  7240. }
  7241. }
  7242. //---------------------------------------------------------------------------
  7243. void __fastcall TCustomScpExplorerForm::ShowExtendedException(
  7244. TTerminal * Terminal, Exception * E)
  7245. {
  7246. if (FTrayIcon->Visible)
  7247. {
  7248. PopupTrayBalloon(Terminal, L"", qtError, E);
  7249. }
  7250. // particularly prevent opening new session from jump list,
  7251. // while exception is shown
  7252. NonVisualDataModule->StartBusy();
  7253. try
  7254. {
  7255. ShowExtendedExceptionEx(Terminal, E);
  7256. }
  7257. __finally
  7258. {
  7259. NonVisualDataModule->EndBusy();
  7260. FTrayIcon->CancelBalloon();
  7261. }
  7262. }
  7263. //---------------------------------------------------------------------------
  7264. void __fastcall TCustomScpExplorerForm::SessionReady()
  7265. {
  7266. InitStatusBar();
  7267. // cannot rely on active page being page for active terminal,
  7268. // as it can happen that active page is the "new session" page
  7269. // (e.g. when reconnecting active terminal, while login dialog
  7270. // invoked from "new session" page is modal)
  7271. int ActiveSessionIndex = TTerminalManager::Instance()->ActiveSessionIndex;
  7272. UpdateSessionTab(SessionsPageControl->Pages[ActiveSessionIndex]);
  7273. }
  7274. //---------------------------------------------------------------------------
  7275. void __fastcall TCustomScpExplorerForm::InactiveTerminalException(
  7276. TTerminal * Terminal, Exception * E)
  7277. {
  7278. Notify(Terminal, L"", qtError, false, NULL, NULL, E);
  7279. if (!Terminal->Active)
  7280. {
  7281. int Index = TTerminalManager::Instance()->IndexOf(Terminal);
  7282. if (DebugAlwaysTrue((Index >= 0) && (Index < SessionsPageControl->PageCount)))
  7283. {
  7284. UpdateSessionTab(SessionsPageControl->Pages[Index]);
  7285. }
  7286. }
  7287. }
  7288. //---------------------------------------------------------------------------
  7289. void __fastcall TCustomScpExplorerForm::Notify(TTerminal * Terminal,
  7290. UnicodeString Message, TQueryType Type,
  7291. bool Important, TNotifyEvent OnClick, TObject * UserData, Exception * E)
  7292. {
  7293. if ((E == NULL) ||
  7294. ExceptionMessage(E, Message))
  7295. {
  7296. unsigned int Seconds = WinConfiguration->NotificationsTimeout;
  7297. if (Important)
  7298. {
  7299. Seconds *= 5;
  7300. }
  7301. UnicodeString NoteMessage(UnformatMessage(Message));
  7302. if (Terminal != NULL)
  7303. {
  7304. TManagedTerminal * Session = DebugNotNull(dynamic_cast<TManagedTerminal *>(Terminal));
  7305. NoteMessage = FORMAT(L"%s: %s",
  7306. (TTerminalManager::Instance()->GetSessionTitle(Session, true), NoteMessage));
  7307. }
  7308. if (WinConfiguration->BalloonNotifications)
  7309. {
  7310. AddNote(NoteMessage);
  7311. PopupTrayBalloon(Terminal, Message, Type, NULL, Seconds, OnClick, UserData);
  7312. }
  7313. else
  7314. {
  7315. FlashOnBackground();
  7316. PostNote(NoteMessage, Seconds, OnClick, UserData);
  7317. }
  7318. }
  7319. }
  7320. //---------------------------------------------------------------------------
  7321. void __fastcall TCustomScpExplorerForm::QueueEmptyNoteClicked(TObject * Sender)
  7322. {
  7323. RestoreApp();
  7324. TTerminalNoteData * TerminalNoteData = dynamic_cast<TTerminalNoteData *>(Sender);
  7325. if (DebugAlwaysTrue(TerminalNoteData != NULL) &&
  7326. !NonVisualDataModule->Busy)
  7327. {
  7328. TManagedTerminal * Terminal = TerminalNoteData->Terminal;
  7329. TTerminalManager::Instance()->ActiveSession = Terminal;
  7330. if (!ComponentVisible[fcQueueView])
  7331. {
  7332. ToggleQueueVisibility();
  7333. GoToQueue();
  7334. }
  7335. }
  7336. }
  7337. //---------------------------------------------------------------------------
  7338. void __fastcall TCustomScpExplorerForm::QueueEvent(TManagedTerminal * ATerminal,
  7339. TTerminalQueue * /*Queue*/, TQueueEvent Event)
  7340. {
  7341. UnicodeString Message;
  7342. TNotifyEvent OnClick = NULL;
  7343. TObject * UserData = NULL;
  7344. bool QueueInvisible = !ComponentVisible[fcQueueView] || IsApplicationMinimized();
  7345. switch (Event)
  7346. {
  7347. case qeEmptyButMonitored:
  7348. if ((ATerminal != Terminal) || QueueInvisible)
  7349. {
  7350. Message = LoadStr(BALLOON_QUEUE_EMPTY);
  7351. OnClick = QueueEmptyNoteClicked;
  7352. TTerminalNoteData * TerminalNoteData = new TTerminalNoteData();
  7353. TerminalNoteData->Terminal = ATerminal;
  7354. UserData = TerminalNoteData;
  7355. }
  7356. break;
  7357. case qeEmpty:
  7358. OperationComplete(ATerminal->QueueOperationStart);
  7359. break;
  7360. case qePendingUserAction:
  7361. if ((ATerminal != Terminal) ||
  7362. (QueueInvisible && !IsQueueAutoPopup()))
  7363. {
  7364. Message = LoadStr(BALLOON_QUEUE_USER_ACTION);
  7365. }
  7366. break;
  7367. default:
  7368. DebugFail();
  7369. }
  7370. if (!Message.IsEmpty())
  7371. {
  7372. Notify(ATerminal, Message, qtInformation, false, OnClick, UserData);
  7373. }
  7374. }
  7375. //---------------------------------------------------------------------------
  7376. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDCreateDragFileList(
  7377. TObject * /*Sender*/, TFileList * FileList, bool & Created)
  7378. {
  7379. if (FDDExtMapFile != NULL)
  7380. {
  7381. CloseHandle(FDDExtMapFile);
  7382. FDDExtMapFile = NULL;
  7383. }
  7384. if (WinConfiguration->DDFakeFile)
  7385. {
  7386. DDFakeFileInitDrag(FileList, Created);
  7387. }
  7388. }
  7389. //---------------------------------------------------------------------------
  7390. void __fastcall TCustomScpExplorerForm::DDFakeCreated(TObject * /*Sender*/, const UnicodeString FileName)
  7391. {
  7392. if (DebugAlwaysTrue(!FDragFakeDirectory.IsEmpty()) &&
  7393. SameText(ExtractFileName(FileName), ExtractFileName(FDragFakeDirectory)))
  7394. {
  7395. FFakeFileDropTarget = FileName;
  7396. }
  7397. }
  7398. //---------------------------------------------------------------------------
  7399. UnicodeString __fastcall TCustomScpExplorerForm::CreateFakeTransferDirectory()
  7400. {
  7401. UnicodeString Result = ExcludeTrailingBackslash(WinConfiguration->TemporaryDir());
  7402. if (!ForceDirectories(ApiPath(Result)))
  7403. {
  7404. throw Exception(FMTLOAD(CREATE_TEMP_DIR_ERROR, (Result)));
  7405. }
  7406. FileSetAttr(ApiPath(Result), faHidden);
  7407. return Result;
  7408. }
  7409. //---------------------------------------------------------------------------
  7410. void __fastcall TCustomScpExplorerForm::DDFakeFileInitDrag(TFileList * FileList,
  7411. bool & Created)
  7412. {
  7413. FFakeFileDropTarget = UnicodeString();
  7414. FDragFakeDirectory = CreateFakeTransferDirectory();
  7415. FileList->AddItem(NULL, FDragFakeDirectory);
  7416. Created = true;
  7417. if (!WinConfiguration->IsDDExtRunning() || WinConfiguration->IsDDExtBroken())
  7418. {
  7419. FDragFakeMonitors = StartCreationDirectoryMonitorsOnEachDrive(FILE_NOTIFY_CHANGE_DIR_NAME, DDFakeCreated);
  7420. }
  7421. FDDExtMapFile = CreateFileMappingA((HANDLE)0xFFFFFFFF, NULL, PAGE_READWRITE,
  7422. 0, sizeof(TDragExtCommStruct), AnsiString(DRAG_EXT_MAPPING).c_str());
  7423. {
  7424. TMutexGuard Guard(FDDExtMutex, DRAGEXT_MUTEX_RELEASE_TIMEOUT);
  7425. TDragExtCommStruct* CommStruct;
  7426. CommStruct = static_cast<TDragExtCommStruct*>(MapViewOfFile(FDDExtMapFile,
  7427. FILE_MAP_ALL_ACCESS, 0, 0, 0));
  7428. DebugAssert(CommStruct != NULL);
  7429. CommStruct->Version = TDragExtCommStruct::CurrentVersion;
  7430. CommStruct->Dragging = true;
  7431. wcsncpy(CommStruct->DropDest, FDragFakeDirectory.c_str(),
  7432. LENOF(CommStruct->DropDest));
  7433. NULL_TERMINATE(CommStruct->DropDest);
  7434. UnmapViewOfFile(CommStruct);
  7435. }
  7436. }
  7437. //---------------------------------------------------------------------------
  7438. bool __fastcall TCustomScpExplorerForm::RemoteFileControlFileOperation(
  7439. TObject * Sender, TFileOperation Operation, bool NoConfirmation, void * Param)
  7440. {
  7441. bool Result;
  7442. DebugAssert(!IsLocalBrowserMode());
  7443. if (Sender == RemoteDirView)
  7444. {
  7445. Result = ExecuteFileOperation(Operation, osRemote, true, NoConfirmation, Param);
  7446. }
  7447. else
  7448. {
  7449. DebugAssert(Sender == RemoteDriveView);
  7450. TStrings * FileList = RemoteDriveView->DragFileList();
  7451. try
  7452. {
  7453. Result = ExecuteFileOperation(Operation, osRemote, FileList, NoConfirmation, Param);
  7454. }
  7455. __finally
  7456. {
  7457. delete FileList;
  7458. }
  7459. }
  7460. if (FDDTargetControl == RemoteDriveView)
  7461. {
  7462. RemoteDriveView->UpdateDropTarget();
  7463. }
  7464. // foRemoteMove happens when file/dir is dragged within the remote tree view
  7465. // or from tree view to dir view.
  7466. // foMove happens when file/dir is dragged from remote tree view outside of
  7467. // application via dragex
  7468. if (((Operation == foRemoteMove) || (Operation == foMove)) &&
  7469. (DropSourceControl == RemoteDriveView))
  7470. {
  7471. RemoteDriveView->UpdateDropSource();
  7472. }
  7473. return Result;
  7474. }
  7475. //---------------------------------------------------------------------------
  7476. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDEnd(TObject * Sender)
  7477. {
  7478. DebugAssert(!IsLocalBrowserMode());
  7479. // This also handles drops of remote files to queue.
  7480. // Drops of local files (uploads) are handled in QueueDDProcessDropped.
  7481. SAFE_DESTROY(FDDFileList);
  7482. if (!FFakeFileDropTarget.IsEmpty())
  7483. {
  7484. RemoveDir(ApiPath(FFakeFileDropTarget));
  7485. }
  7486. if ((FDDExtMapFile != NULL) || (FDDTargetControl == QueueView3))
  7487. {
  7488. try
  7489. {
  7490. TDragResult DDResult = (Sender == RemoteDirView) ?
  7491. RemoteDirView->LastDDResult : RemoteDriveView->LastDDResult;
  7492. // Focus is moved to the target application,
  7493. // but as we are going to present the UI, we need to steal the focus back.
  7494. // This most likely won't work though (windows does not allow application
  7495. // to steal focus most of the time)
  7496. Application->BringToFront();
  7497. // On older version of Windows we never got drMove here, see also comment below.
  7498. // On Windows 10, we get the "move".
  7499. if ((DDResult == drCopy) || (DDResult == drMove) || (DDResult == drInvalid))
  7500. {
  7501. UnicodeString TargetDirectory;
  7502. TFileOperation Operation;
  7503. // drInvalid may mean drMove, see comment below
  7504. switch (DDResult)
  7505. {
  7506. case drCopy:
  7507. Operation = foCopy;
  7508. break;
  7509. case drMove:
  7510. Operation = foMove;
  7511. break;
  7512. default:
  7513. DebugFail();
  7514. case drInvalid:
  7515. // prefer "copy" for safety
  7516. Operation = FLAGSET(FLastDropEffect, DROPEFFECT_MOVE) ? foMove : foCopy;
  7517. break;
  7518. }
  7519. TTransferOperationParam Param;
  7520. UnicodeString CounterName;
  7521. bool ForceQueue;
  7522. if (!DDGetTarget(Param.TargetDirectory, ForceQueue, CounterName))
  7523. {
  7524. // we get drInvalid both if d&d was intercepted by ddext,
  7525. // and when users drops on no-drop location.
  7526. // we tell the difference by existence of response from ddext,
  7527. // so we ignore absence of response in this case
  7528. if (DDResult != drInvalid)
  7529. {
  7530. // here we know that the extension is installed,
  7531. // as it is checked as soon as drag&drop starts from
  7532. // RemoteFileControlDDCreateDragFileList
  7533. Configuration->Usage->Inc(L"DownloadsDragDropExternalExtTargetUnknown");
  7534. if (!WinConfiguration->DDExtInstalled || WinConfiguration->IsDDExtBroken())
  7535. {
  7536. throw ExtException(NULL, LoadStr(DD_TARGET_UNKNOWN), HELP_DD_TARGET_UNKNOWN);
  7537. }
  7538. else
  7539. {
  7540. throw ExtException(NULL, LoadStr(DRAGEXT_TARGET_UNKNOWN2), HELP_DRAGEXT_TARGET_UNKNOWN);
  7541. }
  7542. }
  7543. }
  7544. else
  7545. {
  7546. // download using fake file
  7547. Param.Temp = false;
  7548. Param.DragDrop = true;
  7549. if (ForceQueue)
  7550. {
  7551. Param.Queue = asOn;
  7552. }
  7553. if (Sender == RemoteDirView)
  7554. {
  7555. Param.Options = FLAGMASK(SelectedAllFilesInDirView(RemoteDirView), coAllFiles);
  7556. }
  7557. if (RemoteFileControlFileOperation(Sender, Operation,
  7558. (WinConfiguration->DDTransferConfirmation == asOff), &Param))
  7559. {
  7560. Configuration->Usage->Inc(CounterName);
  7561. }
  7562. }
  7563. }
  7564. }
  7565. __finally
  7566. {
  7567. CloseHandle(FDDExtMapFile);
  7568. FDDExtMapFile = NULL;
  7569. delete FDragFakeMonitors;
  7570. FDragFakeMonitors = NULL;
  7571. RemoveDir(ApiPath(FDragFakeDirectory));
  7572. FDragFakeDirectory = L"";
  7573. FFakeFileDropTarget = UnicodeString();
  7574. }
  7575. }
  7576. if (FDragDropSshTerminate.get() != NULL)
  7577. {
  7578. FDragDropSshTerminate->Rethrow();
  7579. }
  7580. }
  7581. //---------------------------------------------------------------------------
  7582. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDGiveFeedback(
  7583. TObject * /*Sender*/, int dwEffect, HRESULT & /*Result*/)
  7584. {
  7585. // Remember drop effect so we know (when user drops files), if we copy or move
  7586. FLastDropEffect = dwEffect;
  7587. }
  7588. //---------------------------------------------------------------------------
  7589. bool __fastcall TCustomScpExplorerForm::DDGetTarget(
  7590. UnicodeString & Directory, bool & ForceQueue, UnicodeString & CounterName)
  7591. {
  7592. bool Result;
  7593. if (FDDTargetControl == QueueView3)
  7594. {
  7595. Directory = DefaultDownloadTargetDirectory();
  7596. Result = true;
  7597. CounterName = L"DownloadsDragDropQueue";
  7598. ForceQueue = true;
  7599. }
  7600. else if (!FFakeFileDropTarget.IsEmpty())
  7601. {
  7602. Directory = ExcludeTrailingBackslash(ExtractFilePath(FFakeFileDropTarget));
  7603. Result = true;
  7604. ForceQueue = false;
  7605. CounterName = L"DownloadsDragDropFakeFile";
  7606. }
  7607. else
  7608. {
  7609. ForceQueue = false;
  7610. Enabled = false;
  7611. try
  7612. {
  7613. int Timer = 0;
  7614. Result = false;
  7615. while (!Result && (Timer < WinConfiguration->DDExtTimeout))
  7616. {
  7617. {
  7618. TMutexGuard Guard(FDDExtMutex, DRAGEXT_MUTEX_RELEASE_TIMEOUT);
  7619. TDragExtCommStruct* CommStruct;
  7620. CommStruct = static_cast<TDragExtCommStruct*>(MapViewOfFile(FDDExtMapFile,
  7621. FILE_MAP_ALL_ACCESS, 0, 0, 0));
  7622. DebugAssert(CommStruct != NULL);
  7623. Result = !CommStruct->Dragging;
  7624. if (Result)
  7625. {
  7626. Directory = ExtractFilePath(CommStruct->DropDest);
  7627. CounterName = L"DownloadsDragDropExternalExt";
  7628. }
  7629. UnmapViewOfFile(CommStruct);
  7630. }
  7631. if (!Result)
  7632. {
  7633. Sleep(50);
  7634. Timer += 50;
  7635. Application->ProcessMessages();
  7636. }
  7637. }
  7638. }
  7639. __finally
  7640. {
  7641. Enabled = true;
  7642. }
  7643. }
  7644. return Result;
  7645. }
  7646. //---------------------------------------------------------------------------
  7647. void __fastcall TCustomScpExplorerForm::AddDelayedDirectoryDeletion(
  7648. const UnicodeString TempDir, int SecDelay)
  7649. {
  7650. TDateTime Alarm = IncSecond(Now(), SecDelay);
  7651. FDelayedDeletionList->AddObject(TempDir, reinterpret_cast<TObject*>(Alarm.FileDate()));
  7652. if (FDelayedDeletionTimer == NULL)
  7653. {
  7654. DebugAssert(HandleAllocated());
  7655. FDelayedDeletionTimer = new TTimer(this);
  7656. FDelayedDeletionTimer->Interval = 10000;
  7657. FDelayedDeletionTimer->OnTimer = DoDelayedDeletion;
  7658. }
  7659. else
  7660. {
  7661. FDelayedDeletionTimer->Enabled = true;
  7662. }
  7663. }
  7664. //---------------------------------------------------------------------------
  7665. void __fastcall TCustomScpExplorerForm::DoDelayedDeletion(TObject * Sender)
  7666. {
  7667. DebugAssert(FDelayedDeletionList != NULL);
  7668. TDateTime N = Now();
  7669. TDateTime Alert;
  7670. UnicodeString Directory;
  7671. for (int Index = FDelayedDeletionList->Count-1; Index >= 0; Index--)
  7672. {
  7673. Alert = FileDateToDateTime(reinterpret_cast<int>(FDelayedDeletionList->Objects[Index]));
  7674. if ((N >= Alert) || (Sender == NULL))
  7675. {
  7676. Directory = FDelayedDeletionList->Strings[Index];
  7677. if (DeleteDirectory(ExcludeTrailingBackslash(Directory)))
  7678. {
  7679. FDelayedDeletionList->Delete(Index);
  7680. }
  7681. }
  7682. }
  7683. if (FDelayedDeletionList->Count == 0)
  7684. {
  7685. FDelayedDeletionTimer->Enabled = false;
  7686. }
  7687. }
  7688. //---------------------------------------------------------------------------
  7689. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDTargetDrop()
  7690. {
  7691. DebugAssert(!IsLocalBrowserMode());
  7692. if (IsFileControl(FDDTargetControl, osRemote) ||
  7693. (FDDTargetControl == SessionsPageControl))
  7694. {
  7695. TManagedTerminal * TargetTerminal = NULL;
  7696. TFileOperation Operation = foNone;
  7697. if (FDDTargetControl == SessionsPageControl)
  7698. {
  7699. TPoint Point = SessionsPageControl->ScreenToClient(Mouse->CursorPos);
  7700. int Index = SessionsPageControl->IndexOfTabAt(Point.X, Point.Y);
  7701. // do not allow dropping on local-local, disconnected and the "+" tab (see also SessionsDDProcessDropped)
  7702. TargetTerminal = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  7703. if (IsActiveTerminal(TargetTerminal) && DebugAlwaysTrue(!TargetTerminal->LocalBrowser))
  7704. {
  7705. if ((FLastDropEffect == DROPEFFECT_MOVE) &&
  7706. (TargetTerminal == TTerminalManager::Instance()->ActiveTerminal))
  7707. {
  7708. Operation = foRemoteMove;
  7709. }
  7710. else
  7711. {
  7712. Operation = foRemoteCopy;
  7713. }
  7714. }
  7715. }
  7716. else
  7717. {
  7718. if (FLastDropEffect == DROPEFFECT_MOVE)
  7719. {
  7720. Operation = foRemoteMove;
  7721. }
  7722. else if (FLastDropEffect == DROPEFFECT_COPY)
  7723. {
  7724. Operation = foRemoteCopy;
  7725. }
  7726. }
  7727. if (Operation != foNone)
  7728. {
  7729. RemoteFileControlFileOperation(DropSourceControl,
  7730. Operation, (WinConfiguration->DDTransferConfirmation == asOff), TargetTerminal);
  7731. }
  7732. // abort drag&drop
  7733. Abort();
  7734. }
  7735. else if ((FDDExtMapFile == NULL) && (FLastDropEffect != DROPEFFECT_NONE) &&
  7736. // Drops of remote files to queue are handled in RemoteFileControlDDEnd
  7737. (FDDTargetControl != QueueView3))
  7738. {
  7739. DebugAssert(!FDragTempDir.IsEmpty());
  7740. TTransferType Type;
  7741. UnicodeString TempDir = FDragTempDir;
  7742. // We clear FDragTempDir before calling
  7743. // just in case it fail (raises exception)
  7744. FDragTempDir = L"";
  7745. Type = (FLastDropEffect & DROPEFFECT_MOVE ? ttMove : Type = ttCopy);
  7746. TGUICopyParamType CopyParams = GUIConfiguration->CurrentCopyParam;
  7747. // empty directory parameter means temp directory -> don't display it!
  7748. UnicodeString TargetDir = L"";
  7749. int Options = 0;
  7750. if (!CopyParamDialog(tdToLocal, Type, true, FDDFileList,
  7751. TargetDir, CopyParams, (WinConfiguration->DDTransferConfirmation != asOff), true, Options))
  7752. {
  7753. Abort();
  7754. }
  7755. // TargetDir is set when dropped on local file control
  7756. // (this was workaround for legacy dirview event handling, now it should be
  7757. // made prettier)
  7758. if (TargetDir.IsEmpty())
  7759. {
  7760. TargetDir = TempDir;
  7761. if (ForceDirectories(ApiPath(TargetDir)))
  7762. {
  7763. DebugAssert(Terminal && !TargetDir.IsEmpty());
  7764. FPendingTempSpaceWarn = true;
  7765. try
  7766. {
  7767. FDragDropOperation = true;
  7768. int Params = cpTemporary |
  7769. (Type == ttMove ? cpDelete : 0);
  7770. DDDownload(FDDFileList, TargetDir, &CopyParams, Params);
  7771. Configuration->Usage->Inc(L"DownloadsDragDropExternalTemp");
  7772. }
  7773. __finally
  7774. {
  7775. FDragDropOperation = false;
  7776. FPendingTempSpaceWarn = false;
  7777. AddDelayedDirectoryDeletion(TargetDir, WinConfiguration->DDDeleteDelay);
  7778. }
  7779. }
  7780. else
  7781. {
  7782. throw Exception(FMTLOAD(CREATE_TEMP_DIR_ERROR, (TargetDir)));
  7783. }
  7784. }
  7785. }
  7786. }
  7787. //---------------------------------------------------------------------------
  7788. void __fastcall TCustomScpExplorerForm::DDDownload(
  7789. TStrings * FilesToCopy, const UnicodeString & TargetDir, TCopyParamType * CopyParam, int Params)
  7790. {
  7791. DebugAssert(!IsLocalBrowserMode());
  7792. TAutoBatch AutoBatch(this);
  7793. CopyParam->IncludeFileMask.SetRoots(TargetDir, FilesToCopy);
  7794. UpdateCopyParamCounters(*CopyParam);
  7795. Terminal->CopyToLocal(FilesToCopy, TargetDir, CopyParam, Params, NULL);
  7796. if (FLAGSET(Params, cpDelete) && (DropSourceControl == RemoteDriveView))
  7797. {
  7798. RemoteDriveView->UpdateDropSource();
  7799. }
  7800. }
  7801. //---------------------------------------------------------------------------
  7802. class TFakeDataObjectFilesEx : public TDataObjectFilesEx
  7803. {
  7804. public:
  7805. __fastcall TFakeDataObjectFilesEx(TFileList * AFileList, bool RenderPIDL,
  7806. bool RenderFilename) : TDataObjectFilesEx(AFileList, RenderPIDL, RenderFilename, true)
  7807. {
  7808. }
  7809. virtual bool __fastcall AllowData(const tagFORMATETC & FormatEtc)
  7810. {
  7811. return (FormatEtc.cfFormat == CF_HDROP) ? false :
  7812. TDataObjectFilesEx::AllowData(FormatEtc);
  7813. }
  7814. };
  7815. //---------------------------------------------------------------------------
  7816. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDCreateDataObject(
  7817. TObject * Sender, TDataObject *& DataObject)
  7818. {
  7819. if (FDDExtMapFile != NULL)
  7820. {
  7821. TFileList * FileList = DragDropFiles(Sender)->FileList;
  7822. if (!FileList->RenderPIDLs() || !FileList->RenderNames())
  7823. {
  7824. Abort();
  7825. }
  7826. if (FileList->Count > 0)
  7827. {
  7828. TDataObjectFilesEx * FilesObject = new TFakeDataObjectFilesEx(FileList, true, true);
  7829. if (!FilesObject->IsValid(true, true))
  7830. {
  7831. FilesObject->_Release();
  7832. }
  7833. else
  7834. {
  7835. DataObject = FilesObject;
  7836. }
  7837. }
  7838. }
  7839. }
  7840. //---------------------------------------------------------------------------
  7841. void __fastcall TCustomScpExplorerForm::GoToCommandLine()
  7842. {
  7843. DebugFail();
  7844. }
  7845. //---------------------------------------------------------------------------
  7846. void __fastcall TCustomScpExplorerForm::GoToTree()
  7847. {
  7848. ComponentVisible[fcRemoteTree] = true;
  7849. DriveView(osRemote)->SetFocus();
  7850. }
  7851. //---------------------------------------------------------------------------
  7852. TStrings * __fastcall TCustomScpExplorerForm::PanelExport(TOperationSide Side,
  7853. TPanelExport Export)
  7854. {
  7855. TCustomDirView * DirView = this->DirView(Side);
  7856. std::unique_ptr<TStrings> ExportData(new TStringList());
  7857. switch (Export)
  7858. {
  7859. case pePath:
  7860. ExportData->Add(DirView->PathName);
  7861. break;
  7862. case peFileList:
  7863. case peFullFileList:
  7864. {
  7865. bool FullPath = (Export == peFullFileList);
  7866. DirView->CreateFileList(false, FullPath, ExportData.get());
  7867. for (int Index = 0; Index < ExportData->Count; Index++)
  7868. {
  7869. if (ExportData->Strings[Index].Pos(L" ") > 0)
  7870. {
  7871. ExportData->Strings[Index] = FORMAT(L"\"%s\"", (ExportData->Strings[Index]));
  7872. }
  7873. }
  7874. }
  7875. break;
  7876. default:
  7877. DebugFail();
  7878. }
  7879. return ExportData.release();
  7880. }
  7881. //---------------------------------------------------------------------------
  7882. void __fastcall TCustomScpExplorerForm::PanelExport(TOperationSide Side,
  7883. TPanelExport Export, TPanelExportDestination Destination)
  7884. {
  7885. std::unique_ptr<TStrings> ExportData(PanelExport(Side, Export));
  7886. PanelExportStore(Side, Export, Destination, ExportData.get());
  7887. }
  7888. //---------------------------------------------------------------------------
  7889. void __fastcall TCustomScpExplorerForm::PanelExportStore(TOperationSide /*Side*/,
  7890. TPanelExport /*Export*/, TPanelExportDestination Destination,
  7891. TStrings * ExportData)
  7892. {
  7893. if (Destination == pedClipboard)
  7894. {
  7895. TInstantOperationVisualizer Visualizer;
  7896. CopyToClipboard(ExportData);
  7897. }
  7898. else
  7899. {
  7900. DebugFail();
  7901. }
  7902. }
  7903. //---------------------------------------------------------------------------
  7904. void __fastcall TCustomScpExplorerForm::Filter(TOperationSide Side)
  7905. {
  7906. TCustomDirView * DirView = this->DirView(Side);
  7907. UnicodeString Mask = DirView->Mask;
  7908. if (DoFilterMaskDialog(DirView, Mask))
  7909. {
  7910. DirView->Mask = TFileMasks::NormalizeMask(Mask);
  7911. Configuration->Usage->Inc(L"Filters");
  7912. }
  7913. }
  7914. //---------------------------------------------------------------------------
  7915. TQueueOperation __fastcall TCustomScpExplorerForm::DefaultQueueOperation()
  7916. {
  7917. return FQueueController->DefaultOperation();
  7918. }
  7919. //---------------------------------------------------------------------------
  7920. bool __fastcall TCustomScpExplorerForm::AllowQueueOperation(
  7921. TQueueOperation Operation, void ** Param)
  7922. {
  7923. switch (Operation)
  7924. {
  7925. case qoPreferences:
  7926. return true;
  7927. case qoGoTo:
  7928. return ComponentVisible[fcQueueView] && QueueView3->Enabled;
  7929. case qoOnceEmpty:
  7930. return IsAnythingQueued();
  7931. default:
  7932. return FQueueController->AllowOperation(Operation, Param);
  7933. }
  7934. }
  7935. //---------------------------------------------------------------------------
  7936. void __fastcall TCustomScpExplorerForm::GoToQueue()
  7937. {
  7938. if (DebugAlwaysTrue(QueueView3->Visible))
  7939. {
  7940. QueueView3->SetFocus();
  7941. }
  7942. }
  7943. //---------------------------------------------------------------------------
  7944. void __fastcall TCustomScpExplorerForm::ExecuteQueueOperation(
  7945. TQueueOperation Operation, void * Param)
  7946. {
  7947. if (Operation == qoGoTo)
  7948. {
  7949. GoToQueue();
  7950. }
  7951. else if (Operation == qoPreferences)
  7952. {
  7953. PreferencesDialog(pmQueue);
  7954. }
  7955. else
  7956. {
  7957. FQueueController->ExecuteOperation(Operation, Param);
  7958. }
  7959. }
  7960. //---------------------------------------------------------------------------
  7961. bool __fastcall TCustomScpExplorerForm::GetQueueEnabled()
  7962. {
  7963. return (Queue != NULL) && Queue->Enabled;
  7964. }
  7965. //---------------------------------------------------------------------------
  7966. void __fastcall TCustomScpExplorerForm::ToggleQueueEnabled()
  7967. {
  7968. DebugAssert(Queue != NULL);
  7969. if (Queue != NULL)
  7970. {
  7971. Queue->Enabled = !Queue->Enabled;
  7972. }
  7973. }
  7974. //---------------------------------------------------------------------------
  7975. void __fastcall TCustomScpExplorerForm::QueueView3ContextPopup(
  7976. TObject * /*Sender*/, TPoint & /*MousePos*/, bool & /*Handled*/)
  7977. {
  7978. FQueueActedItem = QueueView3->ItemFocused;
  7979. }
  7980. //---------------------------------------------------------------------------
  7981. /*virtual*/ int __fastcall TCustomScpExplorerForm::GetStaticComponentsHeight()
  7982. {
  7983. return TopDock->Height + (MessageDock->Visible ? MessageDock->Height : 0) + QueueSplitter->Height;
  7984. }
  7985. //---------------------------------------------------------------------------
  7986. int __fastcall TCustomScpExplorerForm::GetStaticQueuePanelComponentsHeight()
  7987. {
  7988. return
  7989. (QueueFileListSplitter->Visible ? QueueFileListSplitter->Height : 0) +
  7990. (QueueDock->Visible ? QueueDock->Height : 0) +
  7991. (QueueLabel->Visible ? QueueLabel->Height : 0);
  7992. }
  7993. //---------------------------------------------------------------------------
  7994. int __fastcall TCustomScpExplorerForm::GetMinQueueViewHeight()
  7995. {
  7996. return ScaleByTextHeight(this, 48);
  7997. }
  7998. //---------------------------------------------------------------------------
  7999. void __fastcall TCustomScpExplorerForm::QueueSplitterCanResize(
  8000. TObject * /*Sender*/, int & NewSize, bool & Accept)
  8001. {
  8002. // when queue is hidden by double-clicking splitter, stray attempt to
  8003. // resize the panel with strange value arrives, make sure it is ignored
  8004. if (ComponentVisible[fcQueueView])
  8005. {
  8006. int MaxHeightLimit = ClientHeight - GetStaticComponentsHeight() - RemotePanel->Constraints->MinHeight;
  8007. if (NewSize > MaxHeightLimit)
  8008. {
  8009. NewSize = MaxHeightLimit;
  8010. }
  8011. int MinHeightLimit =
  8012. GetStaticQueuePanelComponentsHeight() +
  8013. (QueueFileList->Visible ? QueueFileList->Height : 0) +
  8014. GetMinQueueViewHeight();
  8015. if (NewSize < MinHeightLimit)
  8016. {
  8017. NewSize = MinHeightLimit;
  8018. }
  8019. }
  8020. else
  8021. {
  8022. Accept = false;
  8023. }
  8024. }
  8025. //---------------------------------------------------------------------------
  8026. void __fastcall TCustomScpExplorerForm::QueueFileListSplitterCanResize(TObject *, int & NewSize, bool &)
  8027. {
  8028. int TotalHeight = GetStaticQueuePanelComponentsHeight() + NewSize;
  8029. int QueueViewHeight = QueuePanel->ClientHeight - TotalHeight;
  8030. if (QueueViewHeight < GetMinQueueViewHeight())
  8031. {
  8032. NewSize -= (GetMinQueueViewHeight() - QueueViewHeight);
  8033. }
  8034. }
  8035. //---------------------------------------------------------------------------
  8036. void __fastcall TCustomScpExplorerForm::QueueView3StartDrag(TObject * /*Sender*/,
  8037. TDragObject *& /*DragObject*/)
  8038. {
  8039. FQueueActedItem = QueueView3->ItemFocused;
  8040. FQueueScrollOnDragOver->StartDrag();
  8041. }
  8042. //---------------------------------------------------------------------------
  8043. void __fastcall TCustomScpExplorerForm::QueueView3EndDrag(
  8044. TObject *, TObject * DebugUsedArg(Target), int DebugUsedArg(X), int DebugUsedArg(Y))
  8045. {
  8046. FQueueScrollOnDragOver->EndDrag();
  8047. }
  8048. //---------------------------------------------------------------------------
  8049. void __fastcall TCustomScpExplorerForm::QueueView3DragOver(TObject * /*Sender*/,
  8050. TObject * Source, int X, int Y, TDragState /*State*/, bool & Accept)
  8051. {
  8052. Accept = true;
  8053. if (Source == QueueView3)
  8054. {
  8055. TListItem * DropTarget = QueueView3->GetItemAt(X, Y);
  8056. Accept = (DropTarget != NULL) && (FQueueActedItem != NULL);
  8057. if (Accept)
  8058. {
  8059. TQueueItemProxy * QueueItem;
  8060. TQueueItemProxy * DestQueueItem;
  8061. QueueItem = static_cast<TQueueItemProxy *>(FQueueActedItem->Data);
  8062. DestQueueItem = static_cast<TQueueItemProxy *>(DropTarget->Data);
  8063. Accept = (QueueItem != DestQueueItem) &&
  8064. (QueueItem->Status == TQueueItem::qsPending) &&
  8065. (DestQueueItem->Status == TQueueItem::qsPending);
  8066. }
  8067. FQueueScrollOnDragOver->DragOver(TPoint(X, Y));
  8068. }
  8069. }
  8070. //---------------------------------------------------------------------------
  8071. void __fastcall TCustomScpExplorerForm::QueueView3DragDrop(TObject * /*Sender*/,
  8072. TObject * /*Source*/, int /*X*/, int /*Y*/)
  8073. {
  8074. if ((FQueueActedItem != NULL) && (QueueView3->DropTarget != NULL))
  8075. {
  8076. TQueueItemProxy * QueueItem;
  8077. TQueueItemProxy * DestQueueItem;
  8078. QueueItem = static_cast<TQueueItemProxy *>(FQueueActedItem->Data);
  8079. DestQueueItem = static_cast<TQueueItemProxy *>(QueueView3->DropTarget->Data);
  8080. QueueItem->Move(DestQueueItem);
  8081. }
  8082. }
  8083. //---------------------------------------------------------------------------
  8084. void __fastcall TCustomScpExplorerForm::QueueView3Enter(TObject * /*Sender*/)
  8085. {
  8086. if ((QueueView3->ItemFocused == NULL) &&
  8087. (QueueView3->Items->Count > 0))
  8088. {
  8089. QueueView3->ItemFocused = QueueView3->Items->Item[0];
  8090. }
  8091. QueueLabelUpdateStatus();
  8092. }
  8093. //---------------------------------------------------------------------------
  8094. void __fastcall TCustomScpExplorerForm::QueueView3Exit(TObject * /*Sender*/)
  8095. {
  8096. QueueLabelUpdateStatus();
  8097. }
  8098. //---------------------------------------------------------------------------
  8099. void __fastcall TCustomScpExplorerForm::QueueLabelUpdateStatus()
  8100. {
  8101. QueueLabel->UpdateStatus();
  8102. }
  8103. //---------------------------------------------------------------------------
  8104. void __fastcall TCustomScpExplorerForm::QueueView3SelectItem(
  8105. TObject * /*Sender*/, TListItem * /*Item*/, bool Selected)
  8106. {
  8107. if (Selected)
  8108. {
  8109. NonVisualDataModule->UpdateNonVisibleActions();
  8110. }
  8111. }
  8112. //---------------------------------------------------------------------------
  8113. TDragDropFilesEx * __fastcall TCustomScpExplorerForm::DragDropFiles(TObject * Sender)
  8114. {
  8115. TDragDropFilesEx * Result = NULL;
  8116. if (Sender == SessionsPageControl)
  8117. {
  8118. Result = FSessionsDragDropFilesEx;
  8119. }
  8120. else if (Sender == QueueView3)
  8121. {
  8122. Result = FQueueDragDropFilesEx;
  8123. }
  8124. else
  8125. {
  8126. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  8127. if (DirView != NULL)
  8128. {
  8129. Result = DirView->DragDropFilesEx;
  8130. }
  8131. else
  8132. {
  8133. TCustomDriveView * DriveView = dynamic_cast<TCustomDriveView *>(Sender);
  8134. if (DriveView != NULL)
  8135. {
  8136. Result = DriveView->DragDropFilesEx;
  8137. }
  8138. }
  8139. }
  8140. DebugAssert(Result != NULL);
  8141. return Result;
  8142. }
  8143. //---------------------------------------------------------------------------
  8144. bool __fastcall TCustomScpExplorerForm::SelectedAllFilesInDirView(TCustomDirView * DView)
  8145. {
  8146. return (DView->SelCount == DView->FilesCount);
  8147. }
  8148. //---------------------------------------------------------------------------
  8149. bool __fastcall TCustomScpExplorerForm::DraggingAllFilesFromDirView(TOperationSide Side, TStrings * FileList)
  8150. {
  8151. return HasDirView[Side] && (DropSourceControl == DirView(Side)) && (FileList->Count == DirView(Side)->FilesCount);
  8152. }
  8153. //---------------------------------------------------------------------------
  8154. void __fastcall TCustomScpExplorerForm::RemoteFileControlDragDropFileOperation(
  8155. TObject * Sender, int Effect, UnicodeString TargetPath, bool ForceQueue, bool DragDrop)
  8156. {
  8157. DebugAssert(!IsLocalBrowserMode());
  8158. TFileOperation Operation;
  8159. switch (Effect)
  8160. {
  8161. case DROPEFFECT_MOVE:
  8162. Operation = foMove;
  8163. break;
  8164. case DROPEFFECT_COPY:
  8165. // occures on WinXP (reported by user)
  8166. default:
  8167. Operation = foCopy;
  8168. break;
  8169. };
  8170. TDragDropFilesEx * DragDropFilesEx = DragDropFiles(Sender);
  8171. // see a comment in TUnixDirView::PerformItemDragDropOperation
  8172. if (DragDropFilesEx->FileList->Count > 0)
  8173. {
  8174. TStrings * FileList = new TStringList();
  8175. try
  8176. {
  8177. for (int Index = 0; Index < DragDropFilesEx->FileList->Count; Index++)
  8178. {
  8179. FileList->Add(DragDropFilesEx->FileList->Items[Index]->Name);
  8180. }
  8181. FDragDropOperation = true;
  8182. TTransferOperationParam Param;
  8183. Param.TargetDirectory = TargetPath;
  8184. // upload, no temp dirs
  8185. Param.Temp = false;
  8186. Param.DragDrop = DragDrop;
  8187. Param.Options =
  8188. FLAGMASK(DraggingAllFilesFromDirView(osLocal, FileList), coAllFiles);
  8189. if (ForceQueue)
  8190. {
  8191. Param.Queue = asOn;
  8192. }
  8193. bool NoConfirmation = DragDrop ? (WinConfiguration->DDTransferConfirmation == asOff) : false;
  8194. if (ExecuteFileOperation(Operation, osLocal, FileList, NoConfirmation, &Param))
  8195. {
  8196. if (IsFileControl(DropSourceControl, osLocal))
  8197. {
  8198. Configuration->Usage->Inc(L"UploadsDragDropInternal");
  8199. }
  8200. else
  8201. {
  8202. Configuration->Usage->Inc(L"UploadsDragDropExternal");
  8203. }
  8204. }
  8205. }
  8206. __finally
  8207. {
  8208. FDragDropOperation = false;
  8209. delete FileList;
  8210. }
  8211. }
  8212. }
  8213. //---------------------------------------------------------------------------
  8214. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDFileOperation(
  8215. TObject * Sender, int Effect, UnicodeString /*SourcePath*/,
  8216. UnicodeString TargetPath, bool Paste, bool & /*DoOperation*/)
  8217. {
  8218. RemoteFileControlDragDropFileOperation(Sender, Effect, TargetPath, false, !Paste);
  8219. }
  8220. //---------------------------------------------------------------------------
  8221. void __fastcall TCustomScpExplorerForm::RemoteFileContolDDChooseEffect(
  8222. TObject * Sender, int grfKeyState, int & dwEffect)
  8223. {
  8224. DebugAssert(!IsLocalBrowserMode());
  8225. // if any drop effect is allowed at all (e.g. no drop to self and drop to parent)
  8226. if ((dwEffect != DROPEFFECT_NONE) &&
  8227. IsFileControl(DropSourceControl, osRemote))
  8228. {
  8229. // do not allow drop on remote panel (on free space, still allow drop on directories)
  8230. if ((Sender == RemoteDirView) && (DropSourceControl == RemoteDirView) &&
  8231. (RemoteDirView->DropTarget == NULL))
  8232. {
  8233. dwEffect = DROPEFFECT_NONE;
  8234. }
  8235. else
  8236. {
  8237. if (dwEffect == DROPEFFECT_COPY)
  8238. {
  8239. bool MoveCapable = Terminal->IsCapable[fcRemoteMove];
  8240. // currently we support copying always (at least via temporary directory);
  8241. // remove associated checks once this all proves stable and working
  8242. bool CopyCapable = true;
  8243. // if we do not support neither of operations, there's no discussion
  8244. if (!MoveCapable && !CopyCapable)
  8245. {
  8246. dwEffect = DROPEFFECT_NONE;
  8247. }
  8248. else
  8249. {
  8250. // The default effect inside remote panel is move,
  8251. // unless we do not support it, but support copy
  8252. if (FLAGCLEAR(grfKeyState, MK_CONTROL))
  8253. {
  8254. dwEffect = MoveCapable ? DROPEFFECT_MOVE : DROPEFFECT_COPY;
  8255. }
  8256. else
  8257. {
  8258. // with ctrl-down, we want copy unless it is not supported
  8259. dwEffect = CopyCapable ? DROPEFFECT_COPY : DROPEFFECT_NONE;
  8260. }
  8261. }
  8262. }
  8263. }
  8264. }
  8265. }
  8266. //---------------------------------------------------------------------------
  8267. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDDragFileName(
  8268. TObject * Sender, TRemoteFile * File, UnicodeString & FileName)
  8269. {
  8270. DebugAssert(!IsLocalBrowserMode());
  8271. if (FDDTotalSize >= 0)
  8272. {
  8273. if (File->IsDirectory)
  8274. {
  8275. FDDTotalSize = -1;
  8276. }
  8277. else
  8278. {
  8279. FDDTotalSize += File->Size;
  8280. }
  8281. }
  8282. DebugAssert(!FDragTempDir.IsEmpty());
  8283. // TODO: this is quite ineffective
  8284. // TODO: what if invalid character replacement is disabled?
  8285. FileName = FDragTempDir + GUIConfiguration->CurrentCopyParam.ValidLocalFileName(File->FileName);
  8286. UnicodeString TransferFileName;
  8287. if (Sender == RemoteDriveView)
  8288. {
  8289. TransferFileName = UnixExcludeTrailingBackslash(File->FullFileName);
  8290. }
  8291. else
  8292. {
  8293. TransferFileName = File->FileName;
  8294. }
  8295. FDDFileList->AddObject(TransferFileName, File);
  8296. }
  8297. //---------------------------------------------------------------------------
  8298. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDDragDetect(
  8299. TObject * /*Sender*/, int /*grfKeyState*/, const TPoint & /*DetectStart*/,
  8300. const TPoint & /*Point*/, TDragDetectStatus /*DragStatus*/)
  8301. {
  8302. // sometimes we do not get DDEnd so the list is not released
  8303. SAFE_DESTROY(FDDFileList);
  8304. FDDFileList = new TStringList();
  8305. FDragTempDir = WinConfiguration->TemporaryDir();
  8306. FDDTotalSize = 0;
  8307. FDragDropSshTerminate.reset(NULL);
  8308. }
  8309. //---------------------------------------------------------------------------
  8310. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDQueryContinueDrag(
  8311. TObject * /*Sender*/, BOOL /*FEscapePressed*/, int /*grfKeyState*/,
  8312. HRESULT & Result)
  8313. {
  8314. if (Result == DRAGDROP_S_DROP)
  8315. {
  8316. try
  8317. {
  8318. GlobalDragImageList->HideDragImage();
  8319. try
  8320. {
  8321. RemoteFileControlDDTargetDrop();
  8322. }
  8323. catch(ESshTerminate & E)
  8324. {
  8325. FDragDropSshTerminate.reset(E.Clone());
  8326. }
  8327. }
  8328. catch (Exception &E)
  8329. {
  8330. // If downloading fails we need to cancel drag&drop, otherwise
  8331. // Explorer shows error
  8332. // But by the way exception probably never reach this point as
  8333. // it's catched on way.
  8334. // Fatal exceptions get here (like when opening a secondary shell extension for file duplication fails).
  8335. Result = DRAGDROP_S_CANCEL;
  8336. ShowExtendedException(Terminal, &E);
  8337. }
  8338. }
  8339. }
  8340. //---------------------------------------------------------------------------
  8341. void __fastcall TCustomScpExplorerForm::DirViewMatchMask(
  8342. TObject * /*Sender*/, UnicodeString FileName, bool Directory, __int64 Size,
  8343. TDateTime Modification, UnicodeString Masks, bool & Matches, bool AllowImplicitMatches)
  8344. {
  8345. TFileMasks::TParams MaskParams;
  8346. MaskParams.Size = Size;
  8347. MaskParams.Modification = Modification;
  8348. // this does not re-parse the mask if it is the same as the last time
  8349. FDirViewMatchMask = Masks;
  8350. bool ImplicitMatch;
  8351. Matches =
  8352. // Local and RecurseInclude parameters have no effect as there's no path.
  8353. // This should use potential MatchesFileName overload with ImplicitMatch argument.
  8354. FDirViewMatchMask.Matches(FileName, false, Directory, &MaskParams, true, ImplicitMatch) &&
  8355. (AllowImplicitMatches || !ImplicitMatch);
  8356. }
  8357. //---------------------------------------------------------------------------
  8358. void __fastcall TCustomScpExplorerForm::DirViewGetOverlay(
  8359. TObject * Sender, TListItem * Item, WORD & Indexes)
  8360. {
  8361. TCustomDirView * DirView = reinterpret_cast<TCustomDirView *>(Sender);
  8362. UnicodeString Ext;
  8363. if (dynamic_cast<TUnixDirView *>(DirView) != NULL)
  8364. {
  8365. Ext = UnixExtractFileExt(DirView->ItemFileName(Item));
  8366. }
  8367. else
  8368. {
  8369. Ext = ExtractFileExt(DirView->ItemFileName(Item));
  8370. }
  8371. if (SameText(Ext, Configuration->PartialExt))
  8372. {
  8373. Indexes |= oiPartial;
  8374. }
  8375. }
  8376. //---------------------------------------------------------------------------
  8377. bool __fastcall TCustomScpExplorerForm::CanPasteToDirViewFromClipBoard()
  8378. {
  8379. return
  8380. DirViewEnabled(osCurrent) &&
  8381. DirView(osCurrent)->CanPasteFromClipBoard();
  8382. }
  8383. //---------------------------------------------------------------------------
  8384. bool __fastcall TCustomScpExplorerForm::CanPasteFromClipBoard()
  8385. {
  8386. bool Result = false;
  8387. if (CanPasteToDirViewFromClipBoard())
  8388. {
  8389. Result = true;
  8390. }
  8391. else
  8392. {
  8393. UnicodeString ClipboardText;
  8394. if (NonEmptyTextFromClipboard(ClipboardText) &&
  8395. (ClipboardText.Pos(L"\n") == 0)) // it's already trimmed
  8396. {
  8397. if (StoredSessions->IsUrl(ClipboardText))
  8398. {
  8399. Result = true;
  8400. }
  8401. else
  8402. {
  8403. Result = DirViewEnabled(osCurrent);
  8404. }
  8405. }
  8406. }
  8407. return Result;
  8408. }
  8409. //---------------------------------------------------------------------------
  8410. void __fastcall TCustomScpExplorerForm::PasteFromClipBoard()
  8411. {
  8412. // Pasting to any local panel is handled in TScpCommanderForm::PasteFromClipBoard()
  8413. if (DoesClipboardContainOurFiles())
  8414. {
  8415. if (DebugAlwaysTrue(CanPasteToDirViewFromClipBoard()))
  8416. {
  8417. DebugAssert(!IsLocalBrowserMode());
  8418. TTerminalManager * Manager = TTerminalManager::Instance();
  8419. TTerminal * TargetTerminal = Manager->ActiveTerminal;
  8420. Manager->ActiveSession = FClipboardTerminal;
  8421. ExecuteFileOperation(foRemoteCopy, osRemote, FClipboardFileList.get(), false, TargetTerminal);
  8422. }
  8423. }
  8424. else if (CanPasteToDirViewFromClipBoard())
  8425. {
  8426. DirView(osCurrent)->PasteFromClipBoard();
  8427. }
  8428. else
  8429. {
  8430. UnicodeString ClipboardText;
  8431. if (NonEmptyTextFromClipboard(ClipboardText))
  8432. {
  8433. if (StoredSessions->IsUrl(ClipboardText))
  8434. {
  8435. NewSession(ClipboardText);
  8436. }
  8437. else
  8438. {
  8439. DirView(osCurrent)->Path = ClipboardText;
  8440. }
  8441. }
  8442. }
  8443. }
  8444. //---------------------------------------------------------------------------
  8445. void __fastcall TCustomScpExplorerForm::FileListFromClipboard()
  8446. {
  8447. // TBD
  8448. }
  8449. //---------------------------------------------------------------------------
  8450. void __fastcall TCustomScpExplorerForm::SelectAll(TOperationSide Side, TSelectMode Mode)
  8451. {
  8452. TCustomDirView * ADirView = DirView(Side);
  8453. ADirView->SelectAll(Mode);
  8454. ADirView->SetFocus();
  8455. }
  8456. //---------------------------------------------------------------------------
  8457. void __fastcall TCustomScpExplorerForm::SelectByMask(TOperationSide Side, bool Select)
  8458. {
  8459. TCustomDirView * ADirView = DirView(Side);
  8460. TFileFilter Filter;
  8461. DefaultFileFilter(Filter);
  8462. if (DoSelectMaskDialog(ADirView, Select, Filter))
  8463. {
  8464. Configuration->Usage->Inc(L"MaskSelections");
  8465. ADirView->SelectFiles(Filter, Select);
  8466. ADirView->SetFocus();
  8467. }
  8468. }
  8469. //---------------------------------------------------------------------------
  8470. void __fastcall TCustomScpExplorerForm::RestoreSelectedNames(TOperationSide Side)
  8471. {
  8472. TCustomDirView * ADirView = DirView(Side);
  8473. ADirView->RestoreSelectedNames();
  8474. ADirView->SetFocus();
  8475. }
  8476. //---------------------------------------------------------------------------
  8477. void __fastcall TCustomScpExplorerForm::SelectSameExt(bool Select)
  8478. {
  8479. TCustomDirView * CurrentDirView = DirView(osCurrent);
  8480. if (DebugAlwaysTrue(CurrentDirView->ItemFocused != NULL))
  8481. {
  8482. UnicodeString FileName = CurrentDirView->ItemFileName(CurrentDirView->ItemFocused);
  8483. UnicodeString Ext;
  8484. if (!IsSideLocalBrowser(osCurrent))
  8485. {
  8486. Ext = UnixExtractFileExt(FileName);
  8487. }
  8488. else
  8489. {
  8490. Ext = ExtractFileExt(FileName);
  8491. }
  8492. if (Ext.IsEmpty())
  8493. {
  8494. Ext = L".";
  8495. }
  8496. TFileFilter Filter;
  8497. Filter.Masks = FORMAT(L"*%s", (Ext));
  8498. Filter.Directories = false;
  8499. CurrentDirView->SelectFiles(Filter, Select);
  8500. }
  8501. }
  8502. //---------------------------------------------------------------------------
  8503. UnicodeString __fastcall TCustomScpExplorerForm::FileStatusBarText(
  8504. const TStatusFileInfo & FileInfo, TOperationSide Side)
  8505. {
  8506. UnicodeString Result;
  8507. if (!FIncrementalSearch.IsEmpty() && (Side == GetSide(osCurrent)))
  8508. {
  8509. Result = FormatIncrementalSearchStatus(FIncrementalSearch, FIncrementalSearchHaveNext);
  8510. }
  8511. else if (!IsSideLocalBrowser(Side) && ((Terminal == NULL) || Terminal->Disconnected))
  8512. {
  8513. // noop
  8514. }
  8515. else
  8516. {
  8517. Result =
  8518. FMTLOAD(FILE_INFO_FORMAT,
  8519. (FormatBytes(FileInfo.SelectedSize),
  8520. FormatBytes(FileInfo.FilesSize),
  8521. FormatNumber(FileInfo.SelectedCount),
  8522. FormatNumber(FileInfo.FilesCount)));
  8523. }
  8524. return Result;
  8525. }
  8526. //---------------------------------------------------------------------------
  8527. void __fastcall TCustomScpExplorerForm::FileStatusBarPanelClick(
  8528. TTBXStatusPanel * Panel, TOperationSide Side)
  8529. {
  8530. if (Panel->Index == 1)
  8531. {
  8532. ToggleShowHiddenFiles();
  8533. }
  8534. else if (Panel->Index == 2)
  8535. {
  8536. Filter(Side);
  8537. }
  8538. }
  8539. //---------------------------------------------------------------------------
  8540. void __fastcall TCustomScpExplorerForm::UpdateFileStatusBar(
  8541. TTBXStatusBar * StatusBar, const TStatusFileInfo & FileInfo, TOperationSide Side)
  8542. {
  8543. DebugAssert(!StatusBar->SimplePanel);
  8544. StatusBar->Panels->Items[0]->Caption = FileStatusBarText(FileInfo, Side);
  8545. UpdateFileStatusExtendedPanels(StatusBar, FileInfo);
  8546. }
  8547. //---------------------------------------------------------------------------
  8548. void __fastcall TCustomScpExplorerForm::UpdateFileStatusExtendedPanels(
  8549. TTBXStatusBar * StatusBar, const TStatusFileInfo & FileInfo)
  8550. {
  8551. DebugAssert(StatusBar->Panels->Count >= 3);
  8552. TTBXStatusPanel * HiddenFilesPanel = StatusBar->Panels->Items[1];
  8553. if (FileInfo.HiddenCount > 0)
  8554. {
  8555. HiddenFilesPanel->Caption = FMTLOAD(FILE_INFO_HIDDEN2, (FormatNumber(FileInfo.HiddenCount)));
  8556. HiddenFilesPanel->ViewPriority = 90; // <100 allows hiding panel when it does not fit
  8557. }
  8558. else
  8559. {
  8560. HiddenFilesPanel->ViewPriority = 0;
  8561. // not really necessary, just to cleanup no-longer-valid data
  8562. HiddenFilesPanel->Caption = L"";
  8563. }
  8564. TTBXStatusPanel * FilteredFilesPanel = StatusBar->Panels->Items[2];
  8565. if (FileInfo.FilteredCount > 0)
  8566. {
  8567. FilteredFilesPanel->Caption = FMTLOAD(FILE_INFO_FILTERED2, (FormatNumber(FileInfo.FilteredCount)));
  8568. FilteredFilesPanel->ViewPriority = 90; // <100 allows hiding panel when it does not fit
  8569. }
  8570. else
  8571. {
  8572. FilteredFilesPanel->ViewPriority = 0;
  8573. // not really necessary, just to cleanup no-longer-valid data
  8574. FilteredFilesPanel->Caption = L"";
  8575. }
  8576. }
  8577. //---------------------------------------------------------------------------
  8578. void __fastcall TCustomScpExplorerForm::RemoteStatusBarClick(
  8579. TObject * /*Sender*/)
  8580. {
  8581. if (DirView(osOther)->Enabled)
  8582. {
  8583. DirView(osOther)->SetFocus();
  8584. }
  8585. }
  8586. //---------------------------------------------------------------------------
  8587. void __fastcall TCustomScpExplorerForm::ToggleQueueVisibility()
  8588. {
  8589. TQueueViewConfiguration Config = WinConfiguration->QueueView;
  8590. switch (Config.Show)
  8591. {
  8592. case qvShow:
  8593. if ((FQueueStatus != NULL) && (FQueueStatus->Count > 0))
  8594. {
  8595. Config.Show = qvHide;
  8596. }
  8597. else
  8598. {
  8599. Config.Show = Config.LastHideShow;
  8600. }
  8601. break;
  8602. case qvHideWhenEmpty:
  8603. if (ComponentVisible[fcQueueView])
  8604. {
  8605. Config.Show = qvHide;
  8606. }
  8607. else
  8608. {
  8609. Config.LastHideShow = Config.Show;
  8610. Config.Show = qvShow;
  8611. }
  8612. break;
  8613. case qvHide:
  8614. Config.LastHideShow = Config.Show;
  8615. Config.Show = qvShow;
  8616. break;
  8617. }
  8618. WinConfiguration->QueueView = Config;
  8619. }
  8620. //---------------------------------------------------------------------------
  8621. UnicodeString __fastcall TCustomScpExplorerForm::PathForCaption()
  8622. {
  8623. UnicodeString Result;
  8624. if (Terminal != NULL)
  8625. {
  8626. switch (WinConfiguration->PathInCaption)
  8627. {
  8628. case picShort:
  8629. Result = ExtractShortName(Terminal->CurrentDirectory, true);
  8630. break;
  8631. case picFull:
  8632. Result = Terminal->CurrentDirectory;
  8633. break;
  8634. }
  8635. }
  8636. return Result;
  8637. }
  8638. //---------------------------------------------------------------------------
  8639. TColor __fastcall TCustomScpExplorerForm::PanelColor()
  8640. {
  8641. TColor Result = GetWindowColor(FSessionColor);
  8642. return Result;
  8643. }
  8644. //---------------------------------------------------------------------------
  8645. TColor __fastcall TCustomScpExplorerForm::DisabledPanelColor()
  8646. {
  8647. TColor Result = (WinConfiguration->UseDarkTheme() ? static_cast<TColor>(RGB(0x40, 0x40, 0x40)) : clBtnFace);
  8648. return Result;
  8649. }
  8650. //---------------------------------------------------------------------------
  8651. void __fastcall TCustomScpExplorerForm::UpdateControls()
  8652. {
  8653. if (!FSessionChanging)
  8654. {
  8655. TTerminalManager::Instance()->UpdateAppTitle();
  8656. }
  8657. // WORKAROUND: Disabling list view when it is not showing yet does not set its
  8658. // background to gray on Windows 7 (works on Windows 10).
  8659. // See also EnableControl
  8660. if (Showing)
  8661. {
  8662. if (ManagedSession != NULL)
  8663. {
  8664. // Update path when it changes
  8665. if ((SessionsPageControl->ActivePage != NULL) &&
  8666. (GetSessionTabSession(SessionsPageControl->ActivePage) == ManagedSession))
  8667. {
  8668. UpdateSessionTab(SessionsPageControl->ActivePage);
  8669. }
  8670. }
  8671. NonVisualDataModule->ReconnectSessionAction->Update();
  8672. ReconnectToolbar->Visible = NonVisualDataModule->ReconnectSessionAction->Visible;
  8673. // ReconnectSessionAction is hidden when disabled, so enabling it actualy resizes the toolbar
  8674. CenterReconnectToolbar();
  8675. bool HasTerminal = HasActiveTerminal();
  8676. RemoteDirView->Enabled = HasTerminal;
  8677. RemoteDirView->Color = HasTerminal ? PanelColor() : DisabledPanelColor();
  8678. RemoteDirView->Font->Color = GetWindowTextColor(RemoteDirView->Color);
  8679. RemoteDriveView->Enabled = RemoteDirView->Enabled;
  8680. RemoteDriveView->Color = RemoteDirView->Color;
  8681. RemoteDriveView->Font->Color = RemoteDirView->Font->Color;
  8682. QueueView3->Enabled = HasTerminal && Terminal->IsCapable[fcBackgroundTransfers];
  8683. QueueView3->Color = QueueView3->Enabled ? GetWindowColor() : DisabledPanelColor();
  8684. QueueView3->Font->Color = GetWindowTextColor(QueueView3->Color);
  8685. QueueFileList->Enabled = QueueView3->Enabled;
  8686. QueueFileList->Color = QueueView3->Color;
  8687. QueueFileList->Font->Color = QueueView3->Font->Color;
  8688. QueueLabelUpdateStatus();
  8689. RemoteDirView->DarkMode = WinConfiguration->UseDarkTheme();
  8690. RemoteDriveView->DarkMode = RemoteDirView->DarkMode;
  8691. if (FImmersiveDarkMode != WinConfiguration->UseDarkTheme())
  8692. {
  8693. UpdateDarkMode();
  8694. }
  8695. reinterpret_cast<TTBCustomItem *>(GetComponent(fcRemotePathComboBox))->Enabled = HasTerminal || IsLocalBrowserMode();
  8696. MessageDock->Visible = (MessageDock->ToolbarCount > 0);
  8697. }
  8698. }
  8699. //---------------------------------------------------------------------------
  8700. void __fastcall TCustomScpExplorerForm::DoDirViewLoaded(TCustomDirView * /*Sender*/)
  8701. {
  8702. UpdateControls();
  8703. }
  8704. //---------------------------------------------------------------------------
  8705. void __fastcall TCustomScpExplorerForm::DirViewLoaded(
  8706. TObject * Sender)
  8707. {
  8708. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  8709. DebugAssert(DirView != NULL);
  8710. DoDirViewLoaded(DirView);
  8711. TransferPresetAutoSelect();
  8712. }
  8713. //---------------------------------------------------------------------------
  8714. void __fastcall TCustomScpExplorerForm::StartUpdates()
  8715. {
  8716. TUpdatesConfiguration Updates = WinConfiguration->Updates;
  8717. // first run after installation
  8718. if (double(Updates.LastCheck) == 0)
  8719. {
  8720. AppLog(L"First run, scheduling updates check for the next run");
  8721. // make sure next time there will be an update (if enabled)
  8722. Updates.LastCheck = TDateTime(1);
  8723. WinConfiguration->Updates = Updates;
  8724. }
  8725. else
  8726. {
  8727. TDateTime Period;
  8728. if (IsUWP() && !IsOfficialPackage())
  8729. {
  8730. Period = DefaultUpdatesPeriod;
  8731. AppLog(L"Thirdparty UWP package, using default updates check period");
  8732. }
  8733. else
  8734. {
  8735. Period = Updates.Period;
  8736. }
  8737. AppLogFmt(L"Updates check period: %.2f", (double(Period)));
  8738. if (double(Period) > 0)
  8739. {
  8740. TDateTime Interval = Now() - Updates.LastCheck;
  8741. AppLogFmt(L"Interval since the last updates check: %.2f", (double(Interval)));
  8742. if (Interval >= Period)
  8743. {
  8744. StartUpdateThread(UpdatesChecked);
  8745. }
  8746. }
  8747. }
  8748. }
  8749. //---------------------------------------------------------------------------
  8750. void __fastcall TCustomScpExplorerForm::UpdatesChecked()
  8751. {
  8752. AppLog(L"Updates check done");
  8753. UnicodeString Message;
  8754. bool New;
  8755. TQueryType Type;
  8756. GetUpdatesMessage(Message, New, Type, false);
  8757. if (!Message.IsEmpty())
  8758. {
  8759. AppLogFmt(L"Updates check message: %s", (Message));
  8760. if (New)
  8761. {
  8762. AppLog(L"New version detected");
  8763. Message = FMTLOAD(NEW_VERSION_CLICK, (Message));
  8764. }
  8765. if (!New && (Type != qtWarning))
  8766. {
  8767. AppLog(L"Posting non-critical updates note");
  8768. PostNote(UnformatMessage(Message), 0, UpdatesNoteClicked, NULL);
  8769. }
  8770. else
  8771. {
  8772. AppLog(L"Posting new version or critical message notification");
  8773. Notify(NULL, Message, Type, true, UpdatesNoteClicked);
  8774. }
  8775. if (New)
  8776. {
  8777. Configuration->Usage->Inc(L"UpdateNotifications");
  8778. }
  8779. }
  8780. }
  8781. //---------------------------------------------------------------------------
  8782. void __fastcall TCustomScpExplorerForm::UpdatesNoteClicked(TObject * /*Sender*/)
  8783. {
  8784. RestoreApp();
  8785. bool CanDisplay = !NonVisualDataModule->Busy;
  8786. if (!CanDisplay && (Screen->ActiveForm != NULL))
  8787. {
  8788. CanDisplay = (Screen->ActiveForm->Perform(WM_CAN_DISPLAY_UPDATES, 0, 0) != 0);
  8789. }
  8790. if (CanDisplay)
  8791. {
  8792. Configuration->Usage->Inc(L"UpdateNotificationsClicked");
  8793. // Probably not needed as CheckForUpdates is blocking since we started using neon for HTTP implementation.
  8794. NonVisualDataModule->StartBusy();
  8795. try
  8796. {
  8797. CheckForUpdates(true);
  8798. }
  8799. __finally
  8800. {
  8801. NonVisualDataModule->EndBusy();
  8802. }
  8803. }
  8804. else
  8805. {
  8806. Configuration->Usage->Inc(L"UpdateNotificationsBusy");
  8807. }
  8808. }
  8809. //---------------------------------------------------------------------------
  8810. void __fastcall TCustomScpExplorerForm::GetTransferPresetAutoSelectData(
  8811. TCopyParamRuleData & Data)
  8812. {
  8813. DebugAssert(Terminal != NULL);
  8814. Data.HostName = Terminal->SessionData->HostNameExpanded;
  8815. Data.UserName = Terminal->SessionData->UserNameExpanded;
  8816. Data.RemoteDirectory = RemoteDirView->PathName;
  8817. }
  8818. //---------------------------------------------------------------------------
  8819. void __fastcall TCustomScpExplorerForm::TransferPresetAutoSelect()
  8820. {
  8821. // Terminal can be null when we are changing local directory implicitly,
  8822. // because it has been deleted, while no session is connected
  8823. // (Login dialog is open)
  8824. if (FAllowTransferPresetAutoSelect && (Terminal != NULL))
  8825. {
  8826. DebugAssert(!IsLocalBrowserMode());
  8827. TCopyParamRuleData Data;
  8828. GetTransferPresetAutoSelectData(Data);
  8829. int CopyParamIndex = GUIConfiguration->CopyParamList->Find(Data);
  8830. UnicodeString CopyParamCurrent = GUIConfiguration->CopyParamCurrent;
  8831. if (CopyParamIndex < 0)
  8832. {
  8833. // there is no preset that matches autoselection
  8834. // set preset that we consider "default"
  8835. FCopyParamAutoSelected = L""; // forget last autoselected preset
  8836. GUIConfiguration->CopyParamCurrent = FCopyParamDefault;
  8837. }
  8838. else
  8839. {
  8840. // there is preset matching autoselection
  8841. UnicodeString CopyParamName = GUIConfiguration->CopyParamList->Names[CopyParamIndex];
  8842. if (CopyParamName == FCopyParamAutoSelected)
  8843. {
  8844. // autoselected the same preset as the last time
  8845. // make no change (i.e. preserve custom user preset, if any)
  8846. }
  8847. else
  8848. {
  8849. // autoselected the different preset then the last time (or there
  8850. // was default preset set)
  8851. FCopyParamAutoSelected = CopyParamName; // remember autoselection
  8852. GUIConfiguration->CopyParamCurrent = CopyParamName;
  8853. }
  8854. }
  8855. if (GUIConfiguration->CopyParamCurrent != CopyParamCurrent)
  8856. {
  8857. if (CopyParamIndex >= 0)
  8858. {
  8859. Configuration->Usage->Inc(L"CopyParamAutoSelects");
  8860. }
  8861. TTransferPresetNoteData * Data = new TTransferPresetNoteData;
  8862. try
  8863. {
  8864. int Fmt =
  8865. (CopyParamIndex < 0) ?
  8866. (GUIConfiguration->CopyParamIndex < 0 ? COPY_PARAM_DEFAULT_NORM : COPY_PARAM_DEFAULT_CUSTOM) :
  8867. COPY_PARAM_AUTOSELECTED;
  8868. UnicodeString Message = FMTLOAD(Fmt, (StripHotkey(GUIConfiguration->CopyParamCurrent)));
  8869. Data->Message = MainInstructions(Message);
  8870. int CopyParamAttrs = Terminal->UsableCopyParamAttrs(0).General;
  8871. UnicodeString Info = GUIConfiguration->CurrentCopyParam.GetInfoStr(L"\n",
  8872. CopyParamAttrs);
  8873. if (CopyParamIndex >= 0)
  8874. {
  8875. DebugAssert(GUIConfiguration->CopyParamList->Rules[CopyParamIndex] != NULL);
  8876. Info = FORMAT(L"%s\n \n%s", (Info,
  8877. FMTLOAD(COPY_PARAM_RULE,
  8878. (GUIConfiguration->CopyParamList->Rules[CopyParamIndex]->GetInfoStr(L"\n")))));
  8879. }
  8880. Data->Message += L"\n\n" + Info;
  8881. if (WinConfiguration->CopyParamAutoSelectNotice)
  8882. {
  8883. TransferPresetNoteMessage(Data, true);
  8884. }
  8885. else
  8886. {
  8887. PostNote(Message, 0, TransferPresetNoteClicked, Data);
  8888. Data = NULL; // ownership passed
  8889. }
  8890. }
  8891. __finally
  8892. {
  8893. delete Data;
  8894. }
  8895. }
  8896. }
  8897. }
  8898. //---------------------------------------------------------------------------
  8899. void __fastcall TCustomScpExplorerForm::TransferPresetNoteMessage(
  8900. TTransferPresetNoteData * NoteData, bool AllowNeverAskAgain)
  8901. {
  8902. DebugAssert(NoteData != NULL);
  8903. DebugAssert(!IsLocalBrowserMode());
  8904. TMessageParams Params(AllowNeverAskAgain ? mpNeverAskAgainCheck : 0);
  8905. TQueryButtonAlias Aliases[1];
  8906. Aliases[0].Button = qaIgnore; // "ignore" is after "ok"
  8907. Aliases[0].Alias = LoadStr(CONFIGURE_BUTTON);
  8908. Params.Aliases = Aliases;
  8909. Params.AliasesCount = LENOF(Aliases);
  8910. unsigned int Result =
  8911. MoreMessageDialog(NoteData->Message, NULL, qtInformation,
  8912. qaOK | qaIgnore, HELP_COPY_PARAM_AUTOSELECTED, &Params);
  8913. switch (Result)
  8914. {
  8915. case qaNeverAskAgain:
  8916. DebugAssert(AllowNeverAskAgain);
  8917. WinConfiguration->CopyParamAutoSelectNotice = false;
  8918. break;
  8919. case qaIgnore:
  8920. PreferencesDialog(pmPresets);
  8921. break;
  8922. }
  8923. }
  8924. //---------------------------------------------------------------------------
  8925. void __fastcall TCustomScpExplorerForm::TransferPresetNoteClicked(TObject * Sender)
  8926. {
  8927. // as of now useless, as this is used for notes only, never for balloons, ...
  8928. RestoreApp();
  8929. // .. and we should never be busy here
  8930. if (DebugAlwaysTrue(!NonVisualDataModule->Busy))
  8931. {
  8932. TransferPresetNoteMessage(DebugNotNull(dynamic_cast<TTransferPresetNoteData *>(Sender)), false);
  8933. }
  8934. }
  8935. //---------------------------------------------------------------------------
  8936. void __fastcall TCustomScpExplorerForm::PreferencesDialog(
  8937. TPreferencesMode APreferencesMode)
  8938. {
  8939. std::unique_ptr<TPreferencesDialogData> PreferencesData;
  8940. TCopyParamRuleData Data;
  8941. if (Terminal != NULL)
  8942. {
  8943. PreferencesData.reset(new TPreferencesDialogData());
  8944. GetTransferPresetAutoSelectData(Data);
  8945. PreferencesData->CopyParamRuleData = &Data;
  8946. }
  8947. DoPreferencesDialog(APreferencesMode, PreferencesData.get());
  8948. }
  8949. //---------------------------------------------------------------------------
  8950. void __fastcall TCustomScpExplorerForm::AdHocCustomCommandValidate(
  8951. const TCustomCommandType & Command)
  8952. {
  8953. if (CustomCommandState(Command, FEditingFocusedAdHocCommand, ccltAll) <= 0)
  8954. {
  8955. throw Exception(FMTLOAD(CUSTOM_COMMAND_IMPOSSIBLE2, (Command.Command)));
  8956. }
  8957. }
  8958. //---------------------------------------------------------------------------
  8959. void __fastcall TCustomScpExplorerForm::AdHocCustomCommand(bool OnFocused)
  8960. {
  8961. bool RemoteAllowed = CustomCommandRemoteAllowed();
  8962. TCustomCommandType Command;
  8963. // make sure we use local custom command when remote are not supported
  8964. if (RemoteAllowed || FLAGSET(FLastCustomCommand.Params, ccLocal))
  8965. {
  8966. Command = FLastCustomCommand;
  8967. }
  8968. else
  8969. {
  8970. Command.Params = Command.Params | ccLocal;
  8971. }
  8972. Command.Name = LoadStr(CUSTOM_COMMAND_AD_HOC_NAME);
  8973. FEditingFocusedAdHocCommand = OnFocused;
  8974. bool LocalSide = IsSideLocalBrowser(FCurrentSide);
  8975. int Options =
  8976. FLAGMASK((!RemoteAllowed || LocalSide), ccoDisableRemote) |
  8977. FLAGMASK(LocalSide, ccoDisableRemoteFiles);
  8978. if (DoCustomCommandDialog(Command, WinConfiguration->CustomCommandList,
  8979. ccmAdHoc, Options, AdHocCustomCommandValidate, NULL))
  8980. {
  8981. FLastCustomCommand = Command;
  8982. UpdateCustomCommandsToolbar();
  8983. ExecuteFileOperation(foCustomCommand, osRemote, OnFocused, false, &Command);
  8984. }
  8985. }
  8986. //---------------------------------------------------------------------------
  8987. void __fastcall TCustomScpExplorerForm::LastCustomCommand(bool OnFocused)
  8988. {
  8989. DebugAssert(!FLastCustomCommand.Command.IsEmpty());
  8990. int State = CustomCommandState(FLastCustomCommand, OnFocused, ccltAll);
  8991. DebugAssert(State > 0);
  8992. if (State <= 0)
  8993. {
  8994. throw Exception(FMTLOAD(CUSTOM_COMMAND_IMPOSSIBLE2, (FLastCustomCommand.Command)));
  8995. }
  8996. ExecuteFileOperation(foCustomCommand, osRemote, OnFocused, false, &FLastCustomCommand);
  8997. }
  8998. //---------------------------------------------------------------------------
  8999. bool __fastcall TCustomScpExplorerForm::GetLastCustomCommand(bool OnFocused,
  9000. TCustomCommandType & Command, int & State)
  9001. {
  9002. bool Result = !FLastCustomCommand.Command.IsEmpty();
  9003. if (Result)
  9004. {
  9005. Command = FLastCustomCommand;
  9006. State = CustomCommandState(FLastCustomCommand, OnFocused, ccltAll);
  9007. }
  9008. return Result;
  9009. }
  9010. //---------------------------------------------------------------------------
  9011. void __fastcall TCustomScpExplorerForm::BeforeAction()
  9012. {
  9013. ResetIncrementalSearch();
  9014. }
  9015. //---------------------------------------------------------------------------
  9016. void __fastcall TCustomScpExplorerForm::PostComponentHide(Byte Component)
  9017. {
  9018. DebugAssert(ComponentVisible[Component]);
  9019. PostMessage(Handle, WM_COMPONENT_HIDE, Component, 0);
  9020. }
  9021. //---------------------------------------------------------------------------
  9022. void __fastcall TCustomScpExplorerForm::QueueSplitterDblClick(TObject * /*Sender*/)
  9023. {
  9024. // when queue panel is resized here directly, the status bar is stretched
  9025. // over whole space the panel occupied
  9026. PostComponentHide(fcQueueView);
  9027. }
  9028. //---------------------------------------------------------------------------
  9029. void __fastcall TCustomScpExplorerForm::QueueFileListSplitterDblClick(TObject *)
  9030. {
  9031. ComponentVisible[fcQueueFileList] = false;
  9032. }
  9033. //---------------------------------------------------------------------------
  9034. void __fastcall TCustomScpExplorerForm::ThemeChanged()
  9035. {
  9036. // We hoped this will refresh scrollbar colors, but it does not have any effect here.
  9037. RefreshColorMode();
  9038. ResetSysDarkTheme();
  9039. ConfigurationChanged();
  9040. ConfigureInterface();
  9041. // Should be called for all controls
  9042. RemoteDirView->Perform(WM_THEMECHANGED, 0, 0);
  9043. }
  9044. //---------------------------------------------------------------------------
  9045. void __fastcall TCustomScpExplorerForm::WMSettingChange(TMessage & Message)
  9046. {
  9047. // Do not handle, when shutting down anyway (maybe also when not setup completely yet?)
  9048. if (!FInvalid &&
  9049. (Message.LParam != 0) &&
  9050. (wcscmp(reinterpret_cast<LPCWCH>(Message.LParam), L"ImmersiveColorSet") == 0))
  9051. {
  9052. ThemeChanged();
  9053. }
  9054. TForm::Dispatch(&Message);
  9055. }
  9056. //---------------------------------------------------------------------------
  9057. void __fastcall TCustomScpExplorerForm::Dispatch(void * Message)
  9058. {
  9059. TMessage * M = static_cast<TMessage*>(Message);
  9060. switch (M->Msg)
  9061. {
  9062. case CM_DIALOGCHAR:
  9063. CMDialogChar(*M);
  9064. break;
  9065. case WM_APPCOMMAND:
  9066. WMAppCommand(*M);
  9067. break;
  9068. case WM_SYSCOMMAND:
  9069. WMSysCommand(*M);
  9070. break;
  9071. case WM_QUERYENDSESSION:
  9072. WMQueryEndSession(*M);
  9073. break;
  9074. case WM_ENDSESSION:
  9075. WMEndSession(*reinterpret_cast<TWMEndSession *>(M));
  9076. break;
  9077. #ifdef _DEBUG
  9078. case WM_WINDOWPOSCHANGED:
  9079. WMWindowPosChanged(*reinterpret_cast<TWMWindowPosMsg *>(M));
  9080. break;
  9081. case CM_CUSTOMSTYLECHANGED:
  9082. TForm::Dispatch(Message);
  9083. break;
  9084. #endif
  9085. case WM_COMPONENT_HIDE:
  9086. {
  9087. Byte Component = static_cast<Byte>(M->WParam);
  9088. // sanity check
  9089. if (ComponentVisible[Component])
  9090. {
  9091. // special treatment
  9092. if (Component == fcQueueView)
  9093. {
  9094. ToggleQueueVisibility();
  9095. DebugAssert(!ComponentVisible[fcQueueView]);
  9096. }
  9097. else
  9098. {
  9099. ComponentVisible[Component] = false;
  9100. }
  9101. }
  9102. }
  9103. break;
  9104. case WM_COPYDATA:
  9105. WMCopyData(*M);
  9106. break;
  9107. case WM_MANAGES_CAPTION:
  9108. // caption managed in TTerminalManager::UpdateAppTitle()
  9109. M->Result = 1;
  9110. break;
  9111. case WM_WANTS_MOUSEWHEEL:
  9112. M->Result = 1;
  9113. break;
  9114. case CM_SHOWINGCHANGED:
  9115. CMShowingChanged(*M);
  9116. break;
  9117. case WM_CLOSE:
  9118. WMClose(*M);
  9119. break;
  9120. case WM_DPICHANGED:
  9121. WMDpiChanged(*M);
  9122. break;
  9123. case CM_DPICHANGED:
  9124. CMDpiChanged(*M);
  9125. break;
  9126. case WM_SETTINGCHANGE:
  9127. WMSettingChange(*M);
  9128. break;
  9129. case CM_DIALOGKEY:
  9130. CMDialogKey(*reinterpret_cast<TWMKeyDown *>(M));
  9131. break;
  9132. case WM_PASTE_FILES:
  9133. PasteFiles();
  9134. break;
  9135. default:
  9136. TForm::Dispatch(Message);
  9137. break;
  9138. }
  9139. }
  9140. //---------------------------------------------------------------------------
  9141. void __fastcall TCustomScpExplorerForm::UpdateImages()
  9142. {
  9143. // noop
  9144. }
  9145. //---------------------------------------------------------------------------
  9146. void __fastcall TCustomScpExplorerForm::CMDpiChanged(TMessage & Message)
  9147. {
  9148. TForm::Dispatch(&Message);
  9149. FSessionColors->Clear();
  9150. AddFixedSessionImages();
  9151. RegenerateSessionColorsImageList(FSessionColors, FSessionColorMaskImageIndex);
  9152. UpdateImages();
  9153. }
  9154. //---------------------------------------------------------------------------
  9155. void __fastcall TCustomScpExplorerForm::WMDpiChanged(TMessage & Message)
  9156. {
  9157. TForm::Dispatch(&Message);
  9158. Configuration->Usage->Inc(L"PixelsPerInchChanged");
  9159. UpdatePixelsPerInchMainWindowCounter();
  9160. }
  9161. //---------------------------------------------------------------------------
  9162. void __fastcall TCustomScpExplorerForm::WMClose(TMessage & Message)
  9163. {
  9164. // Cannot close window while we are busy.
  9165. // We cannot test this in FormCloseQuery as that is called also from
  9166. // Close(), which is called by CloseApplicationAction. So we can be busy
  9167. // there already even, when it is legitimate to close the application.
  9168. // Possibly a better place to handle this would be WMSysCommand.
  9169. if (NonVisualDataModule->Busy)
  9170. {
  9171. Message.Result = 1;
  9172. }
  9173. else
  9174. {
  9175. TForm::Dispatch(&Message);
  9176. }
  9177. }
  9178. //---------------------------------------------------------------------------
  9179. void __fastcall TCustomScpExplorerForm::CMShowingChanged(TMessage & Message)
  9180. {
  9181. if (!FShowingChanged)
  9182. {
  9183. AddStartupSequence(L"D");
  9184. FShowingChanged = true;
  9185. }
  9186. // Ignoring implicit local-local browser of Commander, opened from DoShow
  9187. bool NoSession = (ManagedSession == NULL);
  9188. // This calls TCustomScpExplorerForm::DoShow
  9189. TForm::Dispatch(&Message);
  9190. // Now the window is visible (TForm::Dispatch is what usually takes long, like when loading a "local" network directory)
  9191. if (Showing && !FStarted)
  9192. {
  9193. FStarted = true;
  9194. InterfaceStarted();
  9195. }
  9196. if (Showing && NoSession)
  9197. {
  9198. // When we are starting minimized (i.e. from an installer),
  9199. // postpone showing Login dialog until we get restored.
  9200. // Otherwise the Login dialog (and Authentication window) show restored
  9201. // over invisible (minimized) main window.
  9202. if (WindowState == wsMinimized)
  9203. {
  9204. FNeedSession = true;
  9205. }
  9206. else
  9207. {
  9208. // This happens before application ever goes idle, so the toolbars would
  9209. // stay enabled (initial state) until the Login dialog is dismissed.
  9210. UpdateActions();
  9211. NonVisualDataModule->StartBusy();
  9212. try
  9213. {
  9214. // Need to process WM_ACTIVATEAPP before showing the Login dialog,
  9215. // otherwise the dialog does not receive focus.
  9216. // With Commander interface the ProcessMessages is called already
  9217. // by TDriveView, but with Explorer interface, we need to call it explicily
  9218. Application->ProcessMessages();
  9219. // do not reload sessions, they have been loaded just now (optimization)
  9220. NeedSession(true);
  9221. }
  9222. __finally
  9223. {
  9224. NonVisualDataModule->EndBusy();
  9225. }
  9226. }
  9227. }
  9228. }
  9229. //---------------------------------------------------------------------------
  9230. void __fastcall TCustomScpExplorerForm::CenterReconnectToolbar()
  9231. {
  9232. ReconnectToolbar->Left = (ReconnectToolbar->Parent->ClientWidth - ReconnectToolbar->Width) / 2;
  9233. ReconnectToolbar->Top = (ReconnectToolbar->Parent->ClientHeight - ReconnectToolbar->Height) / 2;
  9234. }
  9235. //---------------------------------------------------------------------------
  9236. void __fastcall TCustomScpExplorerForm::FormConstrainedResize(
  9237. TObject * /*Sender*/, int & MinWidth, int & MinHeight, int & MaxWidth,
  9238. int & MaxHeight)
  9239. {
  9240. // workaround for bug in TWinControl.CalcConstraints
  9241. // Check for empty rect (restore from iconic state) is done there only after
  9242. // call to AdjustClientRect, which enlarges the rect (for forms).
  9243. TRect R = GetClientRect();
  9244. // when restoring from iconic state, place no restrictions
  9245. if (IsRectEmpty(R))
  9246. {
  9247. MinWidth = 0;
  9248. MinHeight = 0;
  9249. MaxWidth = 32000;
  9250. MaxHeight = 32000;
  9251. }
  9252. }
  9253. //---------------------------------------------------------------------------
  9254. void __fastcall TCustomScpExplorerForm::GetSpaceAvailable(const UnicodeString Path,
  9255. TSpaceAvailable & ASpaceAvailable, bool & Close)
  9256. {
  9257. // terminal can be already closed (e.g. dropped connection)
  9258. if ((Terminal != NULL) && Terminal->IsCapable[fcCheckingSpaceAvailable])
  9259. {
  9260. Configuration->Usage->Inc(L"SpaceAvailableChecks");
  9261. try
  9262. {
  9263. Terminal->SpaceAvailable(Path, ASpaceAvailable);
  9264. }
  9265. catch(...)
  9266. {
  9267. if (!Terminal->Active)
  9268. {
  9269. Close = true;
  9270. }
  9271. throw;
  9272. }
  9273. }
  9274. }
  9275. //---------------------------------------------------------------------------
  9276. void __fastcall TCustomScpExplorerForm::FileSystemInfo()
  9277. {
  9278. const TSessionInfo & SessionInfo = Terminal->GetSessionInfo();
  9279. const TFileSystemInfo & FileSystemInfo = Terminal->GetFileSystemInfo(true);
  9280. TGetSpaceAvailable OnGetSpaceAvailable = NULL;
  9281. if (Terminal->IsCapable[fcCheckingSpaceAvailable])
  9282. {
  9283. OnGetSpaceAvailable = GetSpaceAvailable;
  9284. }
  9285. DoFileSystemInfoDialog(SessionInfo, FileSystemInfo, Terminal->CurrentDirectory,
  9286. OnGetSpaceAvailable);
  9287. }
  9288. //---------------------------------------------------------------------------
  9289. TSessionData * __fastcall TCustomScpExplorerForm::SessionDataForCode()
  9290. {
  9291. std::unique_ptr<TSessionData> Data(CloneCurrentSessionData());
  9292. Terminal->FillSessionDataForCode(Data.get());
  9293. return Data.release();
  9294. }
  9295. //---------------------------------------------------------------------------
  9296. void __fastcall TCustomScpExplorerForm::GenerateUrl(TStrings * Paths)
  9297. {
  9298. std::unique_ptr<TSessionData> Data(SessionDataForCode());
  9299. DoGenerateUrlDialog(Data.get(), Paths);
  9300. }
  9301. //---------------------------------------------------------------------------
  9302. void __fastcall TCustomScpExplorerForm::SessionGenerateUrl()
  9303. {
  9304. GenerateUrl(NULL);
  9305. }
  9306. //---------------------------------------------------------------------------
  9307. void __fastcall TCustomScpExplorerForm::FileGenerateUrl()
  9308. {
  9309. std::unique_ptr<TStrings> Paths(new TStringList());
  9310. DirView(osCurrent)->CreateFileList(false, true, Paths.get());
  9311. GenerateUrl(Paths.get());
  9312. }
  9313. //---------------------------------------------------------------------------
  9314. void __fastcall TCustomScpExplorerForm::UpdateSessionColor(TColor value)
  9315. {
  9316. FSessionColor = value;
  9317. TColor C = (value != 0 ? value : Vcl::Graphics::clNone);
  9318. TTBXColorItem * ColorItem = dynamic_cast<TTBXColorItem *>(
  9319. static_cast<TObject *>(GetComponent(fcColorMenu)));
  9320. DebugAssert(ColorItem != NULL);
  9321. ColorItem->Color = C;
  9322. NonVisualDataModule->ColorMenuItem->Color = C;
  9323. UpdateControls();
  9324. }
  9325. //---------------------------------------------------------------------------
  9326. void __fastcall TCustomScpExplorerForm::SetSessionColor(TColor value)
  9327. {
  9328. if (value != FSessionColor)
  9329. {
  9330. UpdateSessionColor(value);
  9331. // Is null when called from LastTerminalClosed
  9332. if (Terminal != NULL)
  9333. {
  9334. SessionsPageControl->ActivePage->ImageIndex = AddSessionColor(value);
  9335. }
  9336. }
  9337. }
  9338. //---------------------------------------------------------------------------
  9339. bool __fastcall TCustomScpExplorerForm::CancelNote(bool Force)
  9340. {
  9341. bool Result = FNoteTimer->Enabled;
  9342. if (Result)
  9343. {
  9344. // cannot cancel note too early
  9345. bool NotEarly =
  9346. (Now() - FNoteShown >
  9347. EncodeTimeVerbose(0, 0, (unsigned short)(WinConfiguration->NotificationsStickTime), 0));
  9348. if (Force || NotEarly)
  9349. {
  9350. FNoteTimer->Enabled = false;
  9351. FNote = L"";
  9352. SAFE_DESTROY(FNoteData);
  9353. FOnNoteClick = NULL;
  9354. FNoteHints = FNotes->Text;
  9355. FNoteHints.Delete(FNoteHints.Length() - 1, 2);
  9356. UpdateStatusBar();
  9357. }
  9358. }
  9359. return Result;
  9360. }
  9361. //---------------------------------------------------------------------------
  9362. void __fastcall TCustomScpExplorerForm::NoteTimer(TObject * /*Sender*/)
  9363. {
  9364. DebugAssert(FNoteTimer->Enabled);
  9365. CancelNote(true);
  9366. }
  9367. //---------------------------------------------------------------------------
  9368. void __fastcall TCustomScpExplorerForm::UpdateNoteHints()
  9369. {
  9370. FNoteHints = FNotes->Text.TrimRight();
  9371. }
  9372. //---------------------------------------------------------------------------
  9373. void __fastcall TCustomScpExplorerForm::AddNote(UnicodeString Note, bool UpdateNow)
  9374. {
  9375. int P = Note.Pos(L"\n");
  9376. if (P > 0)
  9377. {
  9378. Note.SetLength(P - 1);
  9379. }
  9380. FNotes->Add(FORMAT(L"[%s] %s",
  9381. (FormatDateTime(Configuration->TimeFormat, Now()), Note)));
  9382. while (FNotes->Count > 10)
  9383. {
  9384. FNotes->Delete(0);
  9385. }
  9386. if (UpdateNow)
  9387. {
  9388. UpdateNoteHints();
  9389. UpdateStatusBar();
  9390. }
  9391. }
  9392. //---------------------------------------------------------------------------
  9393. void __fastcall TCustomScpExplorerForm::PostNote(UnicodeString Note,
  9394. unsigned int Seconds, TNotifyEvent OnNoteClick, TObject * NoteData)
  9395. {
  9396. int P = Note.Pos(L"\n");
  9397. if (P > 0)
  9398. {
  9399. Note.SetLength(P - 1);
  9400. }
  9401. UpdateNoteHints();
  9402. FNote = Note;
  9403. SAFE_DESTROY(FNoteData);
  9404. FNoteData = NoteData;
  9405. FOnNoteClick = OnNoteClick;
  9406. AddNote(Note, false);
  9407. UpdateStatusBar();
  9408. FNoteShown = Now();
  9409. FNoteTimer->Enabled = false;
  9410. if (Seconds == 0)
  9411. {
  9412. Seconds = WinConfiguration->NotificationsTimeout;
  9413. }
  9414. FNoteTimer->Interval = Seconds * MSecsPerSec;
  9415. FNoteTimer->Enabled = true;
  9416. }
  9417. //---------------------------------------------------------------------------
  9418. void __fastcall TCustomScpExplorerForm::ReadDirectoryCancelled()
  9419. {
  9420. PostNote(LoadStr(DIRECTORY_READING_CANCELLED), 0, NULL, NULL);
  9421. }
  9422. //---------------------------------------------------------------------------
  9423. void __fastcall TCustomScpExplorerForm::SynchronizeBrowsingChanged()
  9424. {
  9425. if (NonVisualDataModule->SynchronizeBrowsingAction2->Checked)
  9426. {
  9427. Configuration->Usage->Inc(L"SynchronizeBrowsingEnabled");
  9428. }
  9429. PostNote(FORMAT(LoadStrPart(SYNC_DIR_BROWSE_TOGGLE, 1),
  9430. (LoadStrPart(SYNC_DIR_BROWSE_TOGGLE,
  9431. (NonVisualDataModule->SynchronizeBrowsingAction2->Checked ? 2 : 3)))),
  9432. 0, NULL, NULL);
  9433. }
  9434. //---------------------------------------------------------------------------
  9435. void __fastcall TCustomScpExplorerForm::ToggleShowHiddenFiles()
  9436. {
  9437. bool NewShowHiddenFiles = !WinConfiguration->ShowHiddenFiles;
  9438. UnicodeString Note =
  9439. FORMAT(LoadStrPart(SHOW_HIDDEN_FILES_TOGGLE, 1), (LoadStrPart(SHOW_HIDDEN_FILES_TOGGLE, (NewShowHiddenFiles ? 2 : 3))));
  9440. PostNote(Note, 0, NULL, NULL);
  9441. GetComponent(fcStatusBar)->Repaint(); // toggling ShowHiddenFiles takes time, force repaint beforehand
  9442. WinConfiguration->ShowHiddenFiles = NewShowHiddenFiles;
  9443. }
  9444. //---------------------------------------------------------------------------
  9445. void __fastcall TCustomScpExplorerForm::SetFormatSizeBytes(TFormatBytesStyle Style)
  9446. {
  9447. WinConfiguration->FormatSizeBytes = Style;
  9448. }
  9449. //---------------------------------------------------------------------------
  9450. void __fastcall TCustomScpExplorerForm::ToggleAutoReadDirectoryAfterOp()
  9451. {
  9452. Configuration->AutoReadDirectoryAfterOp = !Configuration->AutoReadDirectoryAfterOp;
  9453. PostNote(FORMAT(LoadStrPart(AUTO_READ_DIRECTORY_TOGGLE, 1),
  9454. (LoadStrPart(AUTO_READ_DIRECTORY_TOGGLE,
  9455. (Configuration->AutoReadDirectoryAfterOp ? 2 : 3)))), 0, NULL, NULL);
  9456. }
  9457. //---------------------------------------------------------------------------
  9458. void __fastcall TCustomScpExplorerForm::StatusBarPanelDblClick(
  9459. TTBXCustomStatusBar * Sender, TTBXStatusPanel * Panel)
  9460. {
  9461. if (Panel->Index == 0)
  9462. {
  9463. if (FOnNoteClick != NULL)
  9464. {
  9465. // prevent the user data from being freed by possible call
  9466. // to CancelNote or PostNote during call to OnNoteClick
  9467. std::unique_ptr<TObject> NoteData(FNoteData);
  9468. TNotifyEvent OnNoteClick = FOnNoteClick;
  9469. FNoteData = NULL;
  9470. // need to cancel the note as we are going to delete its user data
  9471. CancelNote(true);
  9472. OnNoteClick(NoteData.get());
  9473. }
  9474. }
  9475. if (HasActiveTerminal() &&
  9476. (Panel->Index >= Sender->Panels->Count - SessionPanelCount))
  9477. {
  9478. FileSystemInfo();
  9479. }
  9480. }
  9481. //---------------------------------------------------------------------------
  9482. void __fastcall TCustomScpExplorerForm::LockWindow(bool Force)
  9483. {
  9484. // workaround:
  9485. // 1) for unknown reason, disabling window, while minimized,
  9486. // prevents it from restoring, even if it was enabled again meanwhile
  9487. // 2) when disabling the main window, while another has focus
  9488. // minimize is no longer possible
  9489. // ("keep up to date" dialog - though does not seem to be the case with "find file" -
  9490. // so it has probably something to do with the semi-modalness of the "keep up to date" dialog)
  9491. // Shouldn't we use IsApplicationMinimized() here?
  9492. if ((FLockSuspendLevel == 0) && !IsIconic(Application->Handle) && (Force || (Screen->ActiveForm == this)))
  9493. {
  9494. Enabled = false;
  9495. }
  9496. FLockLevel++;
  9497. }
  9498. //---------------------------------------------------------------------------
  9499. void __fastcall TCustomScpExplorerForm::UnlockWindow()
  9500. {
  9501. DebugAssert(FLockLevel > 0);
  9502. FLockLevel--;
  9503. if (FLockLevel == 0)
  9504. {
  9505. DebugAssert(FLockSuspendLevel == 0);
  9506. Enabled = true;
  9507. }
  9508. }
  9509. //---------------------------------------------------------------------------
  9510. void __fastcall TCustomScpExplorerForm::SuspendWindowLock()
  9511. {
  9512. // We need to make sure that window is enabled when the last modal window closes
  9513. // otherwise focus is not restored correctly.
  9514. // So we re-enable the window when modal window opens and
  9515. // disable it back after is closes
  9516. if (FLockLevel > 0)
  9517. {
  9518. // while we have nesting counter, we know that we never be called
  9519. // recursivelly as Application->OnModalBegin is called only
  9520. // for the top-level modal window
  9521. if (DebugAlwaysTrue(FLockSuspendLevel == 0))
  9522. {
  9523. // won't be disabled when conditions in LockWindow() were not satisfied
  9524. FDisabledOnLockSuspend = !Enabled;
  9525. // When minimized to tray (or actually when set to SW_HIDE),
  9526. // setting Enabled makes the window focusable even when there's
  9527. // modal window over it
  9528. if (!FTrayIcon->Visible)
  9529. {
  9530. Enabled = true;
  9531. }
  9532. }
  9533. FLockSuspendLevel++;
  9534. }
  9535. }
  9536. //---------------------------------------------------------------------------
  9537. void __fastcall TCustomScpExplorerForm::ResumeWindowLock()
  9538. {
  9539. if (FLockSuspendLevel > 0)
  9540. {
  9541. DebugAssert(FLockLevel > 0);
  9542. FLockSuspendLevel--;
  9543. // see comment in SuspendWindowLock
  9544. if (DebugAlwaysTrue(FLockSuspendLevel == 0))
  9545. {
  9546. // Note that window can be enabled here, when we were minized to tray when
  9547. // was SuspendWindowLock() called.
  9548. // We should possibly do the same check as in LockWindow(),
  9549. // if it is ever possible that the consitions change between
  9550. // SuspendWindowLock() and ResumeWindowLock()
  9551. Enabled = !FDisabledOnLockSuspend;
  9552. }
  9553. }
  9554. }
  9555. //---------------------------------------------------------------------------
  9556. void __fastcall TCustomScpExplorerForm::UpdateRemotePathComboBox(bool TextOnly)
  9557. {
  9558. TTBXComboBoxItem * RemotePathComboBox =
  9559. reinterpret_cast<TTBXComboBoxItem *>(GetComponent(fcRemotePathComboBox));
  9560. if (!TextOnly)
  9561. {
  9562. TStrings * Items = RemotePathComboBox->Strings;
  9563. Items->BeginUpdate();
  9564. try
  9565. {
  9566. Items->Clear();
  9567. if ((Terminal != NULL) && !RemoteDirView->Path.IsEmpty())
  9568. {
  9569. UnicodeString APath = UnixExcludeTrailingBackslash(RemoteDirView->Path);
  9570. while (!IsUnixRootPath(APath))
  9571. {
  9572. int P = APath.LastDelimiter(L'/');
  9573. DebugAssert(P >= 0);
  9574. Items->Insert(0, APath.SubString(P + 1, APath.Length() - P));
  9575. APath.SetLength(P - 1);
  9576. }
  9577. Items->Insert(0, Customunixdirview_SUnixDefaultRootName);
  9578. }
  9579. }
  9580. __finally
  9581. {
  9582. Items->EndUpdate();
  9583. }
  9584. }
  9585. RemotePathComboBox->ItemIndex = RemotePathComboBox->Strings->Count - 1;
  9586. // Setting ItemIndex to -1 does not reset its text
  9587. if (RemotePathComboBox->Strings->Count == 0)
  9588. {
  9589. RemotePathComboBox->Text = L"";
  9590. }
  9591. }
  9592. //---------------------------------------------------------------------------
  9593. void __fastcall TCustomScpExplorerForm::DoRemotePathComboBoxAdjustImageIndex(
  9594. TTBXComboBoxItem * Sender, const UnicodeString DebugUsedArg(AText), int AIndex, int & ImageIndex)
  9595. {
  9596. if (AIndex < 0)
  9597. {
  9598. AIndex = Sender->ItemIndex;
  9599. }
  9600. ImageIndex = (AIndex < Sender->Strings->Count - 1 ? StdDirIcon : StdDirSelIcon);
  9601. }
  9602. //---------------------------------------------------------------------------
  9603. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxAdjustImageIndex(
  9604. TTBXComboBoxItem * Sender, const UnicodeString AText, int AIndex,
  9605. int & ImageIndex)
  9606. {
  9607. DoRemotePathComboBoxAdjustImageIndex(Sender, AText, AIndex, ImageIndex);
  9608. }
  9609. //---------------------------------------------------------------------------
  9610. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxDrawItem(
  9611. TTBXCustomList * /*Sender*/, TCanvas * /*ACanvas*/, TRect & ARect, int AIndex,
  9612. int /*AHoverIndex*/, bool & /*DrawDefault*/)
  9613. {
  9614. if (!IsLocalBrowserMode())
  9615. {
  9616. ARect.Left += (10 * AIndex);
  9617. }
  9618. }
  9619. //---------------------------------------------------------------------------
  9620. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxMeasureWidth(
  9621. TTBXCustomList * /*Sender*/, TCanvas * /*ACanvas*/, int AIndex, int &AWidth)
  9622. {
  9623. AWidth += (10 * AIndex);
  9624. }
  9625. //---------------------------------------------------------------------------
  9626. void __fastcall TCustomScpExplorerForm::DoRemotePathComboBoxItemClick(TObject * Sender)
  9627. {
  9628. TTBXComboBoxItem * RemotePathComboBox = dynamic_cast<TTBXComboBoxItem*>(Sender);
  9629. UnicodeString APath = UnixExcludeTrailingBackslash(RemoteDirView->Path);
  9630. int Index = RemotePathComboBox->ItemIndex;
  9631. while (Index < RemotePathComboBox->Strings->Count - 1)
  9632. {
  9633. APath = UnixExtractFileDir(APath);
  9634. Index++;
  9635. }
  9636. // VanDyke style paths
  9637. if (APath.IsEmpty())
  9638. {
  9639. DebugAssert(RemotePathComboBox->ItemIndex == 0);
  9640. APath = ROOTDIRECTORY;
  9641. }
  9642. if (RemoteDirView->Path != APath)
  9643. {
  9644. RemoteDirView->Path = APath;
  9645. }
  9646. }
  9647. //---------------------------------------------------------------------------
  9648. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxItemClick(TObject * Sender)
  9649. {
  9650. DoRemotePathComboBoxItemClick(Sender);
  9651. }
  9652. //---------------------------------------------------------------------------
  9653. void __fastcall TCustomScpExplorerForm::DoRemotePathComboBoxCancel(TObject * Sender)
  9654. {
  9655. DebugAssert(Sender == GetComponent(fcRemotePathComboBox));
  9656. DebugUsedParam(Sender);
  9657. UpdateRemotePathComboBox(true);
  9658. }
  9659. //---------------------------------------------------------------------------
  9660. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxCancel(TObject * Sender)
  9661. {
  9662. DoRemotePathComboBoxCancel(Sender);
  9663. }
  9664. //---------------------------------------------------------------------------
  9665. void __fastcall TCustomScpExplorerForm::DirViewEditing(
  9666. TObject * Sender, TListItem * Item, bool & /*AllowEdit*/)
  9667. {
  9668. ResetIncrementalSearch();
  9669. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  9670. DebugAssert(DirView != NULL);
  9671. if (!WinConfiguration->RenameWholeName && !DirView->ItemIsDirectory(Item))
  9672. {
  9673. HWND Edit = ListView_GetEditControl(DirView->Handle);
  9674. // OnEditing is called also from TCustomListView::CanEdit
  9675. if (Edit != NULL)
  9676. {
  9677. EditSelectBaseName(Edit);
  9678. }
  9679. }
  9680. }
  9681. //---------------------------------------------------------------------------
  9682. //---------------------------------------------------------------------------
  9683. TDragDropFilesEx * __fastcall TCustomScpExplorerForm::CreateDragDropFilesEx()
  9684. {
  9685. TDragDropFilesEx * Result = new TDragDropFilesEx(this);
  9686. Result->AutoDetectDnD = false;
  9687. Result->NeedValid = TFileExMustDnDSet() << nvFilename;
  9688. Result->RenderDataOn = rdoEnterAndDropSync;
  9689. Result->TargetEffects = TDropEffectSet() << deCopy << deMove;
  9690. return Result;
  9691. }
  9692. //---------------------------------------------------------------------------
  9693. void __fastcall TCustomScpExplorerForm::UpdateDarkMode()
  9694. {
  9695. if (IsWin10Build(2004))
  9696. {
  9697. FImmersiveDarkMode = WinConfiguration->UseDarkTheme();
  9698. BOOL DarkMode = FImmersiveDarkMode ? TRUE : FALSE;
  9699. const int DWMWA_USE_IMMERSIVE_DARK_MODE = 20;
  9700. DwmSetWindowAttribute(Handle, DWMWA_USE_IMMERSIVE_DARK_MODE, &DarkMode, sizeof(DarkMode));
  9701. }
  9702. }
  9703. //---------------------------------------------------------------------------
  9704. void __fastcall TCustomScpExplorerForm::CreateWnd()
  9705. {
  9706. TForm::CreateWnd();
  9707. // win32-darkmode calls AllowDarkModeForWindow(this, true) here, but it does not seem to have any effect
  9708. UpdateDarkMode();
  9709. if (FSessionsDragDropFilesEx == NULL)
  9710. {
  9711. FSessionsDragDropFilesEx = CreateDragDropFilesEx();
  9712. FSessionsDragDropFilesEx->OnDragOver = SessionsDDDragOver;
  9713. FSessionsDragDropFilesEx->OnProcessDropped = SessionsDDProcessDropped;
  9714. FSessionsDragDropFilesEx->OnDragEnter = SessionsDDDragEnter;
  9715. FSessionsDragDropFilesEx->OnDragLeave = SessionsDDDragLeave;
  9716. }
  9717. if (FQueueDragDropFilesEx == NULL)
  9718. {
  9719. FQueueDragDropFilesEx = CreateDragDropFilesEx();
  9720. // No need to set OnDragOver as we do not have any restrictions
  9721. FQueueDragDropFilesEx->OnProcessDropped = QueueDDProcessDropped;
  9722. FQueueDragDropFilesEx->OnDragEnter = QueueDDDragEnter;
  9723. FQueueDragDropFilesEx->OnDragLeave = QueueDDDragLeave;
  9724. }
  9725. }
  9726. //---------------------------------------------------------------------------
  9727. void __fastcall TCustomScpExplorerForm::DestroyWnd()
  9728. {
  9729. TForm::DestroyWnd();
  9730. FSessionsDragDropFilesEx->DragDropControl = NULL;
  9731. FQueueDragDropFilesEx->DragDropControl = NULL;
  9732. }
  9733. //---------------------------------------------------------------------------
  9734. void __fastcall TCustomScpExplorerForm::FormShow(TObject * /*Sender*/)
  9735. {
  9736. SideEnter(FCurrentSide);
  9737. FEverShown = true;
  9738. }
  9739. //---------------------------------------------------------------------------
  9740. void __fastcall TCustomScpExplorerForm::DoFindFiles(
  9741. TTerminal * ATerminal, UnicodeString Directory, const TFileMasks & FileMask,
  9742. TFileFoundEvent OnFileFound, TFindingFileEvent OnFindingFile)
  9743. {
  9744. if (!NonVisualDataModule->Busy)
  9745. {
  9746. TTerminalManager::Instance()->ActiveSession = DebugNotNull(dynamic_cast<TManagedTerminal *>(ATerminal));
  9747. Configuration->Usage->Inc(L"FileFinds");
  9748. LockWindow(true);
  9749. NonVisualDataModule->StartBusy();
  9750. WinConfiguration->LockedInterface = true;
  9751. try
  9752. {
  9753. Terminal->FilesFind(Directory, FileMask, OnFileFound, OnFindingFile);
  9754. }
  9755. __finally
  9756. {
  9757. WinConfiguration->LockedInterface = false;
  9758. NonVisualDataModule->EndBusy();
  9759. UnlockWindow();
  9760. }
  9761. }
  9762. }
  9763. //---------------------------------------------------------------------------
  9764. void __fastcall TCustomScpExplorerForm::DoFocusRemotePath(TTerminal * ATerminal, const UnicodeString & Path)
  9765. {
  9766. if (!NonVisualDataModule->Busy)
  9767. {
  9768. TTerminalManager::Instance()->ActiveSession = DebugNotNull(dynamic_cast<TManagedTerminal *>(ATerminal));
  9769. SetFocus();
  9770. RemoteDirView->Path = UnixExtractFilePath(Path);
  9771. UnicodeString FileName = UnixExtractFileName(Path);
  9772. TListItem * Item = RemoteDirView->FindFileItem(FileName);
  9773. // If not found, it can because we have loaded directory from cache and the file is new, try again after reload.
  9774. // (tbd: we should not even try this, if we have not loaded the directory from the cache)
  9775. if (Item == NULL)
  9776. {
  9777. ReloadDirectory(osRemote);
  9778. Item = RemoteDirView->FindFileItem(FileName);
  9779. }
  9780. if (Item != NULL)
  9781. {
  9782. RemoteDirView->ItemFocused = Item;
  9783. RemoteDirView->ItemFocused->MakeVisible(false);
  9784. RemoteDirView->SetFocus();
  9785. }
  9786. }
  9787. }
  9788. //---------------------------------------------------------------------------
  9789. bool __fastcall TCustomScpExplorerForm::CanOperateOnFoundFiles(TTerminal * ATerminal)
  9790. {
  9791. bool Result = !NonVisualDataModule->Busy;
  9792. if (Result)
  9793. {
  9794. TTerminalManager::Instance()->ActiveSession = DebugNotNull(dynamic_cast<TManagedTerminal *>(ATerminal));
  9795. }
  9796. return Result;
  9797. }
  9798. //---------------------------------------------------------------------------
  9799. void __fastcall TCustomScpExplorerForm::DoOperationOnFoundFiles(
  9800. TFileOperation Operation, TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  9801. {
  9802. if (CanOperateOnFoundFiles(ATerminal))
  9803. {
  9804. TValueRestorer<TFileOperationFinishedEvent> OnFileOperationFinishedRestorer(FOnFileOperationFinished);
  9805. FOnFileOperationFinished = OnFileOperationFinished;
  9806. ExecuteFileOperation(Operation, osRemote, FileList, false, NULL);
  9807. }
  9808. }
  9809. //---------------------------------------------------------------------------
  9810. void __fastcall TCustomScpExplorerForm::DoDeleteFoundFiles(
  9811. TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  9812. {
  9813. DoOperationOnFoundFiles(foDelete, ATerminal, FileList, OnFileOperationFinished);
  9814. }
  9815. //---------------------------------------------------------------------------
  9816. void __fastcall TCustomScpExplorerForm::DoDownloadFoundFiles(
  9817. TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  9818. {
  9819. DoOperationOnFoundFiles(foCopy, ATerminal, FileList, OnFileOperationFinished);
  9820. }
  9821. //---------------------------------------------------------------------------
  9822. void __fastcall TCustomScpExplorerForm::DoEditFoundFiles(
  9823. TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  9824. {
  9825. if (CanOperateOnFoundFiles(ATerminal))
  9826. {
  9827. for (int Index = 0; Index < FileList->Count; Index++)
  9828. {
  9829. UnicodeString FileName = FileList->Strings[Index];
  9830. TRemoteFile * File = static_cast<TRemoteFile *>(FileList->Objects[Index]);
  9831. ExecuteRemoteFile(FileName, File, efDefaultEditor);
  9832. OnFileOperationFinished(osRemote, FileName, true);
  9833. }
  9834. }
  9835. }
  9836. //---------------------------------------------------------------------------
  9837. void __fastcall TCustomScpExplorerForm::RemoteFindFiles()
  9838. {
  9839. FFileFindTerminal = Terminal;
  9840. ShowFileFindDialog(
  9841. Terminal, RemoteDirView->Path, DoFindFiles, DoFocusRemotePath, DoDeleteFoundFiles,
  9842. DoDownloadFoundFiles, DoEditFoundFiles);
  9843. }
  9844. //---------------------------------------------------------------------------
  9845. void __fastcall TCustomScpExplorerForm::UpdateTaskbarList(ITaskbarList3 * TaskbarList)
  9846. {
  9847. FTaskbarList = TaskbarList;
  9848. }
  9849. //---------------------------------------------------------------------------
  9850. void __fastcall TCustomScpExplorerForm::SessionsPageControlMouseDown(
  9851. TObject * /*Sender*/, TMouseButton Button, TShiftState /*Shift*/, int X, int Y)
  9852. {
  9853. int Index = SessionsPageControl->IndexOfTabAt(X, Y);
  9854. if (Index >= 0)
  9855. {
  9856. if (Button == mbLeft)
  9857. {
  9858. // WORKAROUND: "Mouse down" is raised only after tab is switched.
  9859. // If switching tab (switching session) takes long enough for user
  9860. // to actually release the button, "mouse down" is still raised,
  9861. // but we do not get "mouse up" event, so dragging is not cancelled,
  9862. // prevent that by not beginning dragging in the first place.
  9863. int Key = ((GetSystemMetrics(SM_SWAPBUTTON) == 0) ? VK_LBUTTON : VK_RBUTTON);
  9864. if (IsKeyPressed(Key))
  9865. {
  9866. // when user clicks the "+", we get mouse down only after the session
  9867. // is closed, when new session tab is already on X:Y, so dragging
  9868. // starts, prevent that
  9869. if (MilliSecondsBetween(Now(), FSessionsPageControlNewTabTime) > 500)
  9870. {
  9871. TTerminal * Terminal = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  9872. if (Terminal != NULL)
  9873. {
  9874. SessionsPageControl->BeginDrag(false);
  9875. }
  9876. }
  9877. }
  9878. }
  9879. else if (Button == mbMiddle)
  9880. {
  9881. // ignore middle-click for "New session tab" and the last local-local tab
  9882. TManagedTerminal * Session = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  9883. if ((Session != NULL) && CanCloseSession(Session))
  9884. {
  9885. CloseSessionTab(Index);
  9886. }
  9887. }
  9888. }
  9889. }
  9890. //---------------------------------------------------------------------------
  9891. void __fastcall TCustomScpExplorerForm::SessionsPageControlDragDrop(
  9892. TObject * /*Sender*/, TObject * /*Source*/, int X, int Y)
  9893. {
  9894. int Index = SessionsPageControl->IndexOfTabAt(X, Y);
  9895. // do not allow dropping on the "+" tab
  9896. TTerminal * TargetTerminal = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  9897. if ((TargetTerminal != NULL) &&
  9898. (SessionsPageControl->ActivePage->PageIndex != Index))
  9899. {
  9900. Configuration->Usage->Inc(L"SessionTabMoves");
  9901. // this is almost redundant as we would recreate tabs in SessionListChanged,
  9902. // but we want to actually prevent that to avoid flicker
  9903. SessionsPageControl->ActivePage->PageIndex = Index;
  9904. TTerminal * Terminal = GetSessionTabSession(SessionsPageControl->ActivePage);
  9905. TTerminalManager::Instance()->Move(Terminal, TargetTerminal);
  9906. }
  9907. }
  9908. //---------------------------------------------------------------------------
  9909. void __fastcall TCustomScpExplorerForm::SessionsPageControlDragOver(
  9910. TObject * Sender, TObject * Source, int X, int Y,
  9911. TDragState /*State*/, bool & Accept)
  9912. {
  9913. Accept = (Sender == Source);
  9914. if (Accept)
  9915. {
  9916. int Index = SessionsPageControl->IndexOfTabAt(X, Y);
  9917. TTerminal * Terminal = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  9918. // do not allow dragging to the "+" tab
  9919. Accept = (Terminal != NULL);
  9920. }
  9921. }
  9922. //---------------------------------------------------------------------------
  9923. void __fastcall TCustomScpExplorerForm::SessionsDDDragOver(
  9924. int DebugUsedArg(KeyState), const TPoint & Point, int & Effect, int DebugUsedArg(PreferredEffect))
  9925. {
  9926. int Index = SessionsPageControl->IndexOfTabAt(Point.X, Point.Y);
  9927. if (Index < 0)
  9928. {
  9929. Effect = DROPEFFECT_NONE;
  9930. }
  9931. else
  9932. {
  9933. TManagedTerminal * TargetSession = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  9934. // do not allow dropping on the "+" tab or on disconnected or local-local tabs
  9935. if (!IsActiveTerminal(TargetSession) || DebugAlwaysFalse(TargetSession->LocalBrowser))
  9936. {
  9937. Effect = DROPEFFECT_NONE;
  9938. }
  9939. else if ((TargetSession != Terminal) && (Effect == DROPEFFECT_MOVE))
  9940. {
  9941. Effect = DROPEFFECT_COPY;
  9942. }
  9943. }
  9944. }
  9945. //---------------------------------------------------------------------------
  9946. void __fastcall TCustomScpExplorerForm::SessionsDDProcessDropped(
  9947. TObject * /*Sender*/, int /*KeyState*/, const TPoint & Point, int Effect)
  9948. {
  9949. int Index = SessionsPageControl->IndexOfTabAt(Point.X, Point.Y);
  9950. // do not allow dropping on local-local, disconnected and the "+" tab
  9951. // (though we do not seem to get here in that case anyway, contrary to RemoteFileControlDDTargetDrop)
  9952. TManagedTerminal * TargetTerminal = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  9953. if (DebugAlwaysTrue(IsActiveTerminal(TargetTerminal) && !TargetTerminal->LocalBrowser))
  9954. {
  9955. DebugAssert(!IsFileControl(DropSourceControl, osRemote));
  9956. if (!IsFileControl(DropSourceControl, osRemote))
  9957. {
  9958. TTerminalManager::Instance()->ActiveSession = TargetTerminal;
  9959. RemoteFileControlDragDropFileOperation(SessionsPageControl, Effect,
  9960. // Why don't we use Terminal->CurrentDirectory directly?
  9961. TTerminalManager::Instance()->ActiveTerminal->CurrentDirectory,
  9962. // do not force queue, drag drop
  9963. false, true);
  9964. }
  9965. }
  9966. }
  9967. //---------------------------------------------------------------------------
  9968. void __fastcall TCustomScpExplorerForm::QueueDDProcessDropped(
  9969. TObject * /*Sender*/, int /*KeyState*/, const TPoint & /*Point*/, int Effect)
  9970. {
  9971. // Downloads are handled in RemoteFileControlDDEnd
  9972. if (!IsFileControl(DropSourceControl, osRemote))
  9973. {
  9974. RemoteFileControlDragDropFileOperation(QueueView3, Effect,
  9975. Terminal->CurrentDirectory,
  9976. // force queue, drag drop
  9977. true, true);
  9978. }
  9979. }
  9980. //---------------------------------------------------------------------------
  9981. void __fastcall TCustomScpExplorerForm::FormClose(TObject * /*Sender*/, TCloseAction & /*Action*/)
  9982. {
  9983. FShowing = false;
  9984. // Do not save empty workspace
  9985. if (WinConfiguration->AutoSaveWorkspace && (ManagedSession != NULL))
  9986. {
  9987. std::unique_ptr<TObjectList> DataList(DoCollectWorkspace());
  9988. UnicodeString Name = WorkspaceName();
  9989. DoSaveWorkspace(
  9990. Name, DataList.get(),
  9991. !Configuration->DisablePasswordStoring &&
  9992. WinConfiguration->AutoSaveWorkspacePasswords, false);
  9993. WinConfiguration->LastStoredSession = Name;
  9994. }
  9995. }
  9996. //---------------------------------------------------------------------------
  9997. void __fastcall TCustomScpExplorerForm::RemoteDirViewRead(TObject * /*Sender*/)
  9998. {
  9999. TManagedTerminal * ManagedTerminal =
  10000. dynamic_cast<TManagedTerminal *>(RemoteDirView->Terminal);
  10001. if (ManagedTerminal != NULL)
  10002. {
  10003. ManagedTerminal->DirectoryLoaded = Now();
  10004. }
  10005. }
  10006. //---------------------------------------------------------------------------
  10007. void __fastcall TCustomScpExplorerForm::DirViewSelectItem(TObject * Sender,
  10008. TListItem * /*Item*/, bool /*Selected*/)
  10009. {
  10010. TCustomDirView * DirView = reinterpret_cast<TCustomDirView *>(Sender);
  10011. switch (DirView->LastSelectMethod)
  10012. {
  10013. case smKeyboard:
  10014. Configuration->Usage->Inc(L"KeyboardSelections");
  10015. break;
  10016. case smMouse:
  10017. Configuration->Usage->Inc(L"MouseSelections");
  10018. break;
  10019. }
  10020. }
  10021. //---------------------------------------------------------------------------
  10022. int __fastcall TCustomScpExplorerForm::AddSessionColor(TColor Color)
  10023. {
  10024. if (Color != 0)
  10025. {
  10026. return GetSessionColorImage(FSessionColors, Color, FSessionColorMaskImageIndex);
  10027. }
  10028. else
  10029. {
  10030. return FSessionTabImageIndex;
  10031. }
  10032. }
  10033. //---------------------------------------------------------------------------
  10034. int __fastcall TCustomScpExplorerForm::AddFixedSessionImage(int GlyphsSourceIndex)
  10035. {
  10036. TPngImageCollectionItem * Item =
  10037. GlyphsModule->ExplorerImages->PngImages->Items[GlyphsSourceIndex];
  10038. FSessionColors->AddPng(Item->PngImage);
  10039. return FSessionColors->Count - 1;
  10040. }
  10041. //---------------------------------------------------------------------------
  10042. void __fastcall TCustomScpExplorerForm::AddFixedSessionImages()
  10043. {
  10044. FSessionColors->SetSize(GlyphsModule->ExplorerImages->Width, GlyphsModule->ExplorerImages->Height);
  10045. FNewRemoteTabTabImageIndex = AddFixedSessionImage(NonVisualDataModule->NewRemoteTabAction->ImageIndex);
  10046. FNewLocalTabTabImageIndex = AddFixedSessionImage(NonVisualDataModule->NewLocalTabAction->ImageIndex);
  10047. FSessionTabImageIndex = AddFixedSessionImage(SiteImageIndex);
  10048. FSessionColorMaskImageIndex = AddFixedSessionImage(SiteColorMaskImageIndex);
  10049. FLocalBrowserTabImageIndex = AddFixedSessionImage(LocalBrowserImageIndex);
  10050. }
  10051. //---------------------------------------------------------------------------
  10052. int TCustomScpExplorerForm::GetNewTabActionImageIndex()
  10053. {
  10054. return NonVisualDataModule->NewRemoteTabAction->ImageIndex;
  10055. }
  10056. //---------------------------------------------------------------------------
  10057. int TCustomScpExplorerForm::GetNewTabTabImageIndex(TOperationSide Side)
  10058. {
  10059. switch (Side)
  10060. {
  10061. case osLocal:
  10062. return FNewLocalTabTabImageIndex;
  10063. case osRemote:
  10064. case osCurrent:
  10065. return FNewRemoteTabTabImageIndex;
  10066. default:
  10067. DebugFail();
  10068. return -1;
  10069. }
  10070. }
  10071. //---------------------------------------------------------------------------
  10072. void __fastcall TCustomScpExplorerForm::CollectItemsWithTextDisplayMode(TWinControl * Control)
  10073. {
  10074. for (int Index = 0; Index < Control->ControlCount; Index++)
  10075. {
  10076. TControl * AControl = Control->Controls[Index];
  10077. TWinControl * WinControl = dynamic_cast<TWinControl *>(AControl);
  10078. if (WinControl != NULL)
  10079. {
  10080. CollectItemsWithTextDisplayMode(WinControl);
  10081. }
  10082. TTBCustomToolbar * Toolbar = dynamic_cast<TTBCustomToolbar *>(AControl);
  10083. if (Toolbar != NULL)
  10084. {
  10085. // we care for top-level items only
  10086. TTBCustomItem * Items = Toolbar->Items;
  10087. for (int ItemIndex = 0; ItemIndex < Items->Count; ItemIndex++)
  10088. {
  10089. TTBCustomItem * Item = Items->Items[ItemIndex];
  10090. if (((Item->DisplayMode == nbdmImageAndText) ||
  10091. (dynamic_cast<TTBXLabelItem *>(Item) != NULL)) &&
  10092. EligibleForImageDisplayMode(Item))
  10093. {
  10094. FItemsWithTextDisplayMode.insert(Item);
  10095. }
  10096. }
  10097. }
  10098. }
  10099. }
  10100. //---------------------------------------------------------------------------
  10101. bool __fastcall TCustomScpExplorerForm::EligibleForImageDisplayMode(TTBCustomItem * /*Item*/)
  10102. {
  10103. return true;
  10104. }
  10105. //---------------------------------------------------------------------------
  10106. bool __fastcall TCustomScpExplorerForm::UpdateToolbarDisplayMode()
  10107. {
  10108. bool SelectiveToolbarText = WinConfiguration->SelectiveToolbarText;
  10109. TTBItemDisplayMode DisplayMode = (SelectiveToolbarText ? nbdmImageAndText : nbdmDefault);
  10110. typedef std::set<TTBCustomItem *> TItemsWithTextDisplayMode;
  10111. TItemsWithTextDisplayMode::iterator i = FItemsWithTextDisplayMode.begin();
  10112. bool Result = true;
  10113. while (Result && (i != FItemsWithTextDisplayMode.end()))
  10114. {
  10115. TTBCustomItem * Item = *i;
  10116. TTBXLabelItem * Label = dynamic_cast<TTBXLabelItem *>(Item);
  10117. if (Label != NULL)
  10118. {
  10119. // optimization
  10120. if (Label->Visible == SelectiveToolbarText)
  10121. {
  10122. Result = false;
  10123. }
  10124. else
  10125. {
  10126. Label->Visible = SelectiveToolbarText;
  10127. }
  10128. }
  10129. else
  10130. {
  10131. // optimization
  10132. if (Item->DisplayMode == DisplayMode)
  10133. {
  10134. Result = false;
  10135. }
  10136. else
  10137. {
  10138. Item->DisplayMode = DisplayMode;
  10139. }
  10140. }
  10141. ++i;
  10142. }
  10143. UpdateNewTabTab();
  10144. return Result;
  10145. }
  10146. //---------------------------------------------------------------------------
  10147. void __fastcall TCustomScpExplorerForm::DisplaySystemContextMenu()
  10148. {
  10149. DebugFail();
  10150. }
  10151. //---------------------------------------------------------------------------
  10152. bool __fastcall TCustomScpExplorerForm::IsBusy()
  10153. {
  10154. // Among other, a lock level is non zero, while directory is being loaded,
  10155. // even when it happens as a result of dirview navigation,
  10156. // i.e. when TNonVisualDataModule is NOT FBusy.
  10157. // That's why the TNonVisualDataModule::GetBusy calls this method.
  10158. // Among other this prevents a panel auto update to occur while
  10159. // directory is changing.
  10160. return (FLockLevel > 0) || DirView(osCurrent)->IsEditing();
  10161. }
  10162. //---------------------------------------------------------------------------
  10163. Boolean __fastcall TCustomScpExplorerForm::AllowedAction(TAction * Action, TActionAllowed Allowed)
  10164. {
  10165. // While the window is disabled, we still seem to process menu shortcuts at least,
  10166. // so stop it at least here.
  10167. // See also TCustomScpExplorerForm::RemoteDirViewBusy
  10168. bool Result;
  10169. if (Allowed == aaUpdate)
  10170. {
  10171. Result = true;
  10172. }
  10173. else
  10174. {
  10175. Result = !NonVisualDataModule->Busy;
  10176. if (Result && (Allowed == aaShortCut))
  10177. {
  10178. // Otherwise F5 for local-local will be swallowed by disabled local<>remote actions in TCustomScpExplorerForm::KeyDown
  10179. Action->Update();
  10180. Result = Action->Enabled;
  10181. }
  10182. }
  10183. return Result;
  10184. }
  10185. //---------------------------------------------------------------------------
  10186. void __fastcall TCustomScpExplorerForm::EditMenuItemPopup(TTBCustomItem * Sender, bool FromLink)
  10187. {
  10188. NonVisualDataModule->EditMenuItemPopup(Sender, FromLink);
  10189. }
  10190. //---------------------------------------------------------------------------
  10191. void __fastcall TCustomScpExplorerForm::DirViewBusy(TObject * /*Sender*/, int Busy, bool & State)
  10192. {
  10193. // This is somewhat redundant to LockWindow() call from
  10194. // TTerminalManager::TerminalReadDirectoryProgress.
  10195. // But disabling window is known not to block keyboard shorcuts
  10196. // (see TCustomScpExplorerForm::AllowedAction), this hopefully works.
  10197. if (Busy > 0)
  10198. {
  10199. if (NonVisualDataModule->Busy)
  10200. {
  10201. State = false;
  10202. }
  10203. else
  10204. {
  10205. NonVisualDataModule->StartBusy();
  10206. // Will be countered for TCustomDirView.DoExecute in DoDirViewExecFile
  10207. LockWindow();
  10208. }
  10209. }
  10210. else if (Busy < 0)
  10211. {
  10212. UnlockWindow();
  10213. NonVisualDataModule->EndBusy();
  10214. }
  10215. else
  10216. {
  10217. State = NonVisualDataModule->Busy;
  10218. }
  10219. }
  10220. //---------------------------------------------------------------------------
  10221. void __fastcall TCustomScpExplorerForm::SessionsPageControlContextPopup(TObject * /*Sender*/, TPoint & MousePos, bool & Handled)
  10222. {
  10223. int Index = SessionsPageControl->IndexOfTabAt(MousePos.X, MousePos.Y);
  10224. if (Index >= 0)
  10225. {
  10226. TPopupMenu * PopupMenu = NULL;
  10227. TManagedTerminal * Session = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  10228. if (Session != NULL)
  10229. {
  10230. SessionsPageControl->ActivePageIndex = Index;
  10231. if (DebugAlwaysTrue(SessionTabSwitched()))
  10232. {
  10233. // copied from TControl.WMContextMenu
  10234. SendCancelMode(SessionsPageControl);
  10235. // explicit popup instead of using PopupMenu property
  10236. // to avoid menu to popup somewhere within SessionTabSwitched above,
  10237. // while connecting yet not-connected session and hence
  10238. // allowing an access to commands over not-completely connected session
  10239. PopupMenu = Session->LocalBrowser ? NonVisualDataModule->LocalBrowserPopup : NonVisualDataModule->SessionsPopup;
  10240. }
  10241. }
  10242. else
  10243. {
  10244. if (SupportsLocalBrowser())
  10245. {
  10246. PopupMenu = NonVisualDataModule->NewTabPopup;
  10247. }
  10248. }
  10249. if (PopupMenu != NULL)
  10250. {
  10251. TPoint Point = SessionsPageControl->ClientToScreen(MousePos);
  10252. PopupMenu->PopupComponent = SessionsPageControl;
  10253. PopupMenu->Popup(Point.x, Point.y);
  10254. }
  10255. }
  10256. Handled = true;
  10257. }
  10258. //---------------------------------------------------------------------------
  10259. bool __fastcall TCustomScpExplorerForm::CanChangePassword()
  10260. {
  10261. return HasActiveTerminal() && Terminal->IsCapable[fcChangePassword];
  10262. }
  10263. //---------------------------------------------------------------------------
  10264. void __fastcall TCustomScpExplorerForm::ChangePassword()
  10265. {
  10266. Configuration->Usage->Inc(L"PasswordChanges");
  10267. std::unique_ptr<TSessionData> ChangePasswordData(Terminal->SessionData->Clone());
  10268. ChangePasswordData->DisableAuthentationsExceptPassword();
  10269. ChangePasswordData->ChangePassword = true;
  10270. std::unique_ptr<TTerminal> ChangePasswordSession(
  10271. Terminal->CreateSecondarySession(L"ChangePassword", ChangePasswordData.get()));
  10272. ChangePasswordSession->AutoReadDirectory = false;
  10273. if (TTerminalManager::Instance()->ConnectTerminal(ChangePasswordSession.get()))
  10274. {
  10275. MessageDialog(MainInstructions(LoadStr(PASSWORD_CHANGED)), qtInformation, qaOK, HELP_CHANGE_PASSWORD);
  10276. }
  10277. else
  10278. {
  10279. Configuration->Usage->Inc(L"PasswordChangesFailed");
  10280. }
  10281. }
  10282. //---------------------------------------------------------------------------
  10283. bool __fastcall TCustomScpExplorerForm::CanPrivateKeyUpload()
  10284. {
  10285. // No nice way to assert SSH2
  10286. return HasActiveTerminal() && (Terminal->FSProtocol == cfsSFTP);
  10287. }
  10288. //---------------------------------------------------------------------------
  10289. void __fastcall TCustomScpExplorerForm::PrivateKeyUpload()
  10290. {
  10291. TOperationVisualizer OperationVisualizer;
  10292. UnicodeString FileName = Terminal->SessionData->PublicKeyFile;
  10293. TTerminalManager::Instance()->UploadPublicKey(Terminal, NULL, FileName);
  10294. }
  10295. //---------------------------------------------------------------------------
  10296. void __fastcall TCustomScpExplorerForm::ChangeScale(int M, int D)
  10297. {
  10298. TForm::ChangeScale(M, D);
  10299. int APixelsPerInch = GetControlPixelsPerInch(this);
  10300. HANDLE ResourceModule = GUIConfiguration->ChangeToDefaultResourceModule();
  10301. try
  10302. {
  10303. GlyphsModule->SetPixelsPerInch(APixelsPerInch);
  10304. }
  10305. __finally
  10306. {
  10307. GUIConfiguration->ChangeResourceModule(ResourceModule);
  10308. }
  10309. }
  10310. //---------------------------------------------------------------------------
  10311. void __fastcall TCustomScpExplorerForm::DockContextPopup(TObject * Sender, TPoint & MousePos, bool & /*Handled*/)
  10312. {
  10313. NonVisualDataModule->ControlContextPopup(Sender, MousePos);
  10314. }
  10315. //---------------------------------------------------------------------
  10316. class TPublicControl : public TControl
  10317. {
  10318. friend class TCustomScpExplorerForm;
  10319. };
  10320. //---------------------------------------------------------------------------
  10321. void __fastcall TCustomScpExplorerForm::CopyPopup(TControl * DestControl, TControl * SourceControl)
  10322. {
  10323. static_cast<TPublicControl *>(DestControl)->PopupMenu = static_cast<TPublicControl *>(SourceControl)->PopupMenu;
  10324. static_cast<TPublicControl *>(DestControl)->OnContextPopup = static_cast<TPublicControl *>(SourceControl)->OnContextPopup;
  10325. }
  10326. //---------------------------------------------------------------------------
  10327. void __fastcall TCustomScpExplorerForm::DoBookmarkClick(TOperationSide Side, TObject * Sender)
  10328. {
  10329. TBookmark * Bookmark = DebugNotNull(reinterpret_cast<TBookmark *>(dynamic_cast<TTBCustomItem *>(Sender)->Tag));
  10330. OpenBookmark(Side, Bookmark);
  10331. }
  10332. //---------------------------------------------------------------------------
  10333. void __fastcall TCustomScpExplorerForm::LocalBookmarkClick(TObject * Sender)
  10334. {
  10335. DoBookmarkClick(osLocal, Sender);
  10336. }
  10337. //---------------------------------------------------------------------------
  10338. void __fastcall TCustomScpExplorerForm::RemoteBookmarkClick(TObject * Sender)
  10339. {
  10340. DoBookmarkClick(osRemote, Sender);
  10341. }
  10342. //---------------------------------------------------------------------------
  10343. void __fastcall TCustomScpExplorerForm::CreateOpenDirMenuList(
  10344. TTBCustomItem * Menu, TOperationSide Side, TBookmarkList * BookmarkList)
  10345. {
  10346. if (BookmarkList != NULL)
  10347. {
  10348. TNotifyEvent OnBookmarkClick = (Side == osLocal) ? &LocalBookmarkClick : &RemoteBookmarkClick;
  10349. if (IsLocalBrowserMode())
  10350. {
  10351. Side = osLocal;
  10352. }
  10353. if (!WinConfiguration->UseLocationProfiles)
  10354. {
  10355. std::unique_ptr<TStringList> Directories(new TStringList());
  10356. Directories->CaseSensitive = (Side == osRemote);
  10357. for (int Index = 0; Index < BookmarkList->Count; Index++)
  10358. {
  10359. TBookmark * Bookmark = BookmarkList->Bookmarks[Index];
  10360. UnicodeString Directory = Bookmark->GetSideDirectory(Side);
  10361. if (!Directory.IsEmpty() && (Directories->IndexOf(Directory) < 0))
  10362. {
  10363. std::unique_ptr<TTBCustomItem> Item(new TTBXItem(Owner));
  10364. Item->Caption = EscapeHotkey(Directory);
  10365. Item->ShortCut = Bookmark->ShortCut;
  10366. Item->OnClick = OnBookmarkClick;
  10367. Item->Tag = reinterpret_cast<int>(Bookmark);
  10368. Directories->Add(Directory);
  10369. Menu->Add(Item.release());
  10370. }
  10371. }
  10372. }
  10373. else
  10374. {
  10375. std::unique_ptr<TStrings> Folders(CreateSortedStringList());
  10376. for (int Index = 0; Index < BookmarkList->Count; Index++)
  10377. {
  10378. TBookmark * Bookmark = BookmarkList->Bookmarks[Index];
  10379. if (!Bookmark->Node.IsEmpty())
  10380. {
  10381. Folders->Add(Bookmark->Node);
  10382. }
  10383. }
  10384. for (int Index = 0; Index < Folders->Count; Index++)
  10385. {
  10386. std::unique_ptr<TTBCustomItem> Item(new TTBXSubmenuItem(Owner));
  10387. Item->Caption = Folders->Strings[Index];
  10388. Item->ImageIndex = NonVisualDataModule->RemoteChangePathAction2->ImageIndex;
  10389. Folders->Objects[Index] = Item.get();
  10390. Menu->Add(Item.release());
  10391. }
  10392. for (int Index = 0; Index < BookmarkList->Count; Index++)
  10393. {
  10394. TBookmark * Bookmark = BookmarkList->Bookmarks[Index];
  10395. TTBCustomItem * Parent;
  10396. if (!Bookmark->Node.IsEmpty())
  10397. {
  10398. DebugAssert(Folders->IndexOf(Bookmark->Node) >= 0);
  10399. Parent = dynamic_cast<TTBCustomItem *>(Folders->Objects[Folders->IndexOf(Bookmark->Node)]);
  10400. }
  10401. else
  10402. {
  10403. Parent = Menu;
  10404. }
  10405. std::unique_ptr<TTBCustomItem> Item(new TTBXItem(Owner));
  10406. Item->Caption = Bookmark->Name;
  10407. Item->ShortCut = Bookmark->ShortCut;
  10408. Item->OnClick = OnBookmarkClick;
  10409. Item->Tag = reinterpret_cast<int>(Bookmark);
  10410. if (((Bookmark->Name == Bookmark->Local) || (Bookmark->Name == Bookmark->Remote)) &&
  10411. (Bookmark->Local.IsEmpty() || Bookmark->Remote.IsEmpty()))
  10412. {
  10413. // No hint for location profiles that are actually mere "bookmarks"
  10414. }
  10415. else
  10416. {
  10417. UnicodeString Hint = FORMAT(LoadStrPart(LOCATION_PROFILE_HINT, 1), (Bookmark->Name));
  10418. UnicodeString LongHint;
  10419. if (!Bookmark->Local.IsEmpty())
  10420. {
  10421. AddToList(LongHint, FORMAT(LoadStrPart(LOCATION_PROFILE_HINT, 2), (Bookmark->Local)), L"\n");
  10422. }
  10423. if (!Bookmark->Remote.IsEmpty())
  10424. {
  10425. AddToList(LongHint, FORMAT(LoadStrPart(LOCATION_PROFILE_HINT, 3), (Bookmark->Remote)), L"\n");
  10426. }
  10427. AddToList(Hint, LongHint, L"|");
  10428. Item->Hint = Hint;
  10429. }
  10430. Parent->Add(Item.release());
  10431. }
  10432. }
  10433. AddMenuSeparator(Menu);
  10434. }
  10435. }
  10436. //---------------------------------------------------------------------------
  10437. void __fastcall TCustomScpExplorerForm::CreateOpenDirMenu(TTBCustomItem * Menu, TOperationSide Side)
  10438. {
  10439. Menu->Clear();
  10440. std::unique_ptr<TTBCustomItem> Item;
  10441. Item.reset(new TTBXItem(Owner));
  10442. Item->Action = (Side == osLocal) ? NonVisualDataModule->LocalOpenDirAction : NonVisualDataModule->RemoteOpenDirAction;
  10443. Item->Options = Item->Options << tboDefault;
  10444. Menu->Add(Item.release());
  10445. AddMenuSeparator(Menu);
  10446. if (Terminal != NULL)
  10447. {
  10448. CreateOpenDirMenuList(Menu, Side, WinConfiguration->Bookmarks[Terminal->SessionData->SessionKey]);
  10449. }
  10450. CreateOpenDirMenuList(Menu, Side, WinConfiguration->SharedBookmarks);
  10451. Item.reset(new TTBXItem(Owner));
  10452. Item->Action = (Side == osLocal) ? NonVisualDataModule->LocalAddBookmarkAction2 : NonVisualDataModule->RemoteAddBookmarkAction2;
  10453. Menu->Add(Item.release());
  10454. }
  10455. //---------------------------------------------------------------------------
  10456. bool __fastcall TCustomScpExplorerForm::TryOpenDirectory(TOperationSide Side, const UnicodeString & Path)
  10457. {
  10458. bool Result = true;
  10459. bool Remote = !IsSideLocalBrowser(Side);
  10460. try
  10461. {
  10462. UnicodeString APath = Path;
  10463. if (Remote)
  10464. {
  10465. Terminal->ExceptionOnFail = true;
  10466. }
  10467. else
  10468. {
  10469. APath = ExpandEnvironmentVariables(Path);
  10470. }
  10471. try
  10472. {
  10473. DirView(Side)->Path = APath;
  10474. }
  10475. __finally
  10476. {
  10477. if (Remote)
  10478. {
  10479. Terminal->ExceptionOnFail = false;
  10480. }
  10481. }
  10482. }
  10483. catch (Exception & E)
  10484. {
  10485. if (!Remote || Terminal->Active)
  10486. {
  10487. ShowExtendedExceptionEx(Terminal, &E);
  10488. Result = false;
  10489. }
  10490. else
  10491. {
  10492. throw;
  10493. }
  10494. }
  10495. return Result;
  10496. }
  10497. //---------------------------------------------------------------------------
  10498. void __fastcall TCustomScpExplorerForm::ReloadDirectory(TOperationSide Side)
  10499. {
  10500. // Make sure there some feedback even when loading tiny local folders
  10501. TInstantOperationVisualizer Visualizer;
  10502. DirView(Side)->ReloadDirectory();
  10503. }
  10504. //---------------------------------------------------------------------------
  10505. void __fastcall TCustomScpExplorerForm::CloseSessionTab(int Index)
  10506. {
  10507. if (Index == SessionsPageControl->TabIndex)
  10508. {
  10509. CloseTab();
  10510. }
  10511. else
  10512. {
  10513. TTabSheet * TabSheet = SessionsPageControl->Pages[Index];
  10514. TTerminal * Terminal = GetSessionTabSession(TabSheet);
  10515. TTerminalManager * Manager = TTerminalManager::Instance();
  10516. if (!Terminal->Active)
  10517. {
  10518. Manager->FreeTerminal(Terminal);
  10519. }
  10520. else
  10521. {
  10522. TTerminalQueue * Queue = Manager->FindQueueForTerminal(Terminal);
  10523. if (CanCloseQueue(Queue))
  10524. {
  10525. Manager->FreeTerminal(Terminal);
  10526. }
  10527. }
  10528. }
  10529. }
  10530. //---------------------------------------------------------------------------
  10531. void __fastcall TCustomScpExplorerForm::SessionsPageControlTabButtonClick(TPageControl *, int Index)
  10532. {
  10533. TManagedTerminal * Session = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  10534. if (Session == NULL)
  10535. {
  10536. TRect ButtonRect = SessionsPageControl->TabButtonRect(Index);
  10537. ButtonRect = TRect(SessionsPageControl->ClientToScreen(ButtonRect.TopLeft()), SessionsPageControl->ClientToScreen(ButtonRect.BottomRight()));
  10538. //TPoint P = TPoint(ButtonRect.Left, ButtonRect.Bottom);
  10539. //P = SessionsPageControl->ClientToScreen(P);
  10540. MenuPopup(NonVisualDataModule->NewTabPopup, ButtonRect, SessionsPageControl);
  10541. }
  10542. else
  10543. {
  10544. CloseSessionTab(Index);
  10545. }
  10546. }
  10547. //---------------------------------------------------------------------------
  10548. void __fastcall TCustomScpExplorerForm::CopyFilesToClipboard(TOperationSide Side, bool OnFocused)
  10549. {
  10550. if (DebugAlwaysTrue(!IsSideLocalBrowser(Side)))
  10551. {
  10552. TInstantOperationVisualizer Visualizer;
  10553. // To trigger ClipboardDataObjectRelease (in case the clipboard already contains our data)
  10554. // before we create a new data object
  10555. ClipboardClear();
  10556. DebugAssert(FClipboardFakeDirectory.IsEmpty());
  10557. DebugAssert(FClipboardFakeMonitors.get() == NULL);
  10558. FClipboardFakeDirectory = CreateFakeTransferDirectory();
  10559. // Intermediate variable to WORKAROUND a crash in compiler
  10560. TObjectList * ClipboardFakeMonitors =
  10561. StartCreationDirectoryMonitorsOnEachDrive(FILE_NOTIFY_CHANGE_DIR_NAME, ClipboardFakeCreated);
  10562. FClipboardFakeMonitors.reset(ClipboardFakeMonitors);
  10563. if (FClipboardDragDropFilesEx.get() == NULL)
  10564. {
  10565. FClipboardDragDropFilesEx.reset(new TDragDropFilesEx(this));
  10566. FClipboardDragDropFilesEx->OnDataObjectRelease = ClipboardDataObjectRelease;
  10567. }
  10568. FClipboardDragDropFilesEx->FileList->Clear();
  10569. FClipboardDragDropFilesEx->FileList->AddItem(NULL, FClipboardFakeDirectory);
  10570. FClipboardDragDropFilesEx->CopyToClipboard();
  10571. FClipboardTerminal = Terminal;
  10572. // Need full paths, as cwd can be different once files are pasted
  10573. FClipboardFileList.reset(TRemoteFileList::CloneStrings(RemoteDirView->CreateFileList(OnFocused, true)));
  10574. for (int Index = 0; Index < FClipboardFileList->Count; Index++)
  10575. {
  10576. FClipboardFileList->Strings[Index] = UnixExcludeTrailingBackslash(FClipboardFileList->Strings[Index]);
  10577. }
  10578. }
  10579. }
  10580. //---------------------------------------------------------------------------
  10581. void __fastcall TCustomScpExplorerForm::ClipboardClear()
  10582. {
  10583. if (OpenClipboard(0))
  10584. {
  10585. EmptyClipboard(); // Calls ClipboardDataObjectRelease, if clipboard contains our data
  10586. CloseClipboard();
  10587. }
  10588. }
  10589. //---------------------------------------------------------------------------
  10590. bool __fastcall TCustomScpExplorerForm::DoesClipboardContainOurFiles()
  10591. {
  10592. return !FClipboardFakeDirectory.IsEmpty();
  10593. }
  10594. //---------------------------------------------------------------------------
  10595. void __fastcall TCustomScpExplorerForm::ClipboardStop()
  10596. {
  10597. RemoveDir(ApiPath(FClipboardFakeDirectory));
  10598. FClipboardFakeDirectory = UnicodeString();
  10599. FClipboardTerminal = NULL;
  10600. if (FDownloadingFromClipboard)
  10601. {
  10602. // We are called by the monitor, so attempt to release it would deadlock
  10603. FClipboardFakeMonitorsPendingReset = true;
  10604. }
  10605. else
  10606. {
  10607. FClipboardFakeMonitors.reset(NULL);
  10608. }
  10609. }
  10610. //---------------------------------------------------------------------------
  10611. void __fastcall TCustomScpExplorerForm::ClipboardDataObjectRelease(TObject * /*Sender*/)
  10612. {
  10613. if (DebugAlwaysTrue(!FClipboardFakeDirectory.IsEmpty()))
  10614. {
  10615. ClipboardStop();
  10616. }
  10617. }
  10618. //---------------------------------------------------------------------------
  10619. void __fastcall TCustomScpExplorerForm::ClipboardDownload(const UnicodeString & TargetDirectory, bool NoConfirmation, bool DragDrop)
  10620. {
  10621. TTerminalManager * Manager = TTerminalManager::Instance();
  10622. Manager->ActiveSession = FClipboardTerminal;
  10623. TTransferOperationParam Params;
  10624. Params.TargetDirectory = TargetDirectory;
  10625. Params.DragDrop = DragDrop;
  10626. ExecuteFileOperation(foCopy, osRemote, FClipboardFileList.get(), NoConfirmation, &Params);
  10627. }
  10628. //---------------------------------------------------------------------------
  10629. void TCustomScpExplorerForm::PasteFiles()
  10630. {
  10631. // Guard against possible race conditions
  10632. if (!FClipboardPasteTarget.IsEmpty())
  10633. {
  10634. // ClipboardFakeCreated might be called while we are here, so make sure we finish operating on the same path we started with
  10635. UnicodeString Target = FClipboardPasteTarget;
  10636. FClipboardPasteTarget = EmptyStr;
  10637. // Can fail as it can be e.g. locked by AV, so we retry that later
  10638. bool Removed = RemoveDir(ApiPath(Target));
  10639. try
  10640. {
  10641. if (!NonVisualDataModule->Busy)
  10642. {
  10643. DebugAssert(!FClipboardFakeMonitorsPendingReset);
  10644. FClipboardFakeMonitorsPendingReset = false;
  10645. bool NoConfirmation = (WinConfiguration->DDTransferConfirmation == asOff);
  10646. TAutoFlag Flag(FDownloadingFromClipboard);
  10647. ClipboardDownload(ExtractFilePath(Target), NoConfirmation, true);
  10648. }
  10649. }
  10650. __finally
  10651. {
  10652. if (!Removed)
  10653. {
  10654. RemoveDir(ApiPath(Target));
  10655. }
  10656. }
  10657. }
  10658. }
  10659. //---------------------------------------------------------------------------
  10660. void __fastcall TCustomScpExplorerForm::ClipboardFakeCreated(TObject * /*Sender*/, const UnicodeString FileName)
  10661. {
  10662. // It can actually rarelly happen that some random file is created, while we are shutting down the monitor
  10663. // (as it pumps a Windows message queue while being shutted down)
  10664. if (DebugAlwaysTrue(!FClipboardFakeDirectory.IsEmpty()) &&
  10665. SameText(ExtractFileName(FileName), ExtractFileName(FClipboardFakeDirectory)))
  10666. {
  10667. AppLogFmt(L"Fake clipboard directory pasted to \"%s\"", (FileName));
  10668. FClipboardPasteTarget = FileName;
  10669. // Particularly when pasting to external HDD, the fake directory is strangely recreated if removed here in the monitor callback.
  10670. // Postponing removing (or actually whole pasting operation) seems to help.
  10671. PostMessage(Handle, WM_PASTE_FILES, 0, 0);
  10672. }
  10673. }
  10674. //---------------------------------------------------------------------------
  10675. void __fastcall TCustomScpExplorerForm::DirViewGetItemColor(
  10676. TObject * Sender, UnicodeString FileName, bool Directory, __int64 Size, TDateTime Modification, TColor & Color)
  10677. {
  10678. TFileMasks::TParams MaskParams;
  10679. MaskParams.Size = Size;
  10680. MaskParams.Modification = Modification;
  10681. TCustomDirView * DirView = DebugNotNull(dynamic_cast<TCustomDirView *>(Sender));
  10682. bool Local = (dynamic_cast<TUnixDirView *>(DirView) == NULL);
  10683. // TUnixDirView::ItemFullFileName returns trailing backslash for directories, what filemask cannot deal with
  10684. if (!Local && Directory)
  10685. {
  10686. FileName = UnixExcludeTrailingBackslash(FileName);
  10687. }
  10688. for (TFileColorData::TList::const_iterator Iter = FFileColors.begin(); Iter != FFileColors.end(); Iter++)
  10689. {
  10690. bool ImplicitMatch;
  10691. if (Iter->FileMask.Matches(FileName, Local, Directory, &MaskParams, false, ImplicitMatch) &&
  10692. !ImplicitMatch)
  10693. {
  10694. Color = Iter->Color;
  10695. break;
  10696. }
  10697. }
  10698. }
  10699. //---------------------------------------------------------------------------
  10700. void __fastcall TCustomScpExplorerForm::DirViewKeyDown(TObject * Sender, WORD & Key, TShiftState)
  10701. {
  10702. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  10703. if (!DirView->IsEditing())
  10704. {
  10705. // Handled here, instead of RemoteDirViewKeyPress (as on Login dialog),
  10706. // as VK_BACK is handled by TCustomDirView.KeyDown and we can swallow it here,
  10707. // while searching
  10708. if (Key == VK_BACK)
  10709. {
  10710. if (!FIncrementalSearch.IsEmpty())
  10711. {
  10712. if (FIncrementalSearch.Length() == 1)
  10713. {
  10714. ResetIncrementalSearch();
  10715. }
  10716. else
  10717. {
  10718. UnicodeString NewText = FIncrementalSearch.SubString(1, FIncrementalSearch.Length() - 1);
  10719. IncrementalSearch(NewText, false, false);
  10720. }
  10721. Key = 0;
  10722. }
  10723. }
  10724. }
  10725. }
  10726. //---------------------------------------------------------------------------
  10727. void __fastcall TCustomScpExplorerForm::DirViewKeyPress(TObject * Sender, wchar_t & Key)
  10728. {
  10729. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  10730. if (!DirView->IsEditing() && (WinConfiguration->PanelSearch != isOff))
  10731. {
  10732. // Filter control sequences.
  10733. // When not searching yet, prefer use of the space for toggling file selection
  10734. // (so we cannot incrementally search for a string starting with a space).
  10735. if ((Key > VK_SPACE) ||
  10736. ((Key == VK_SPACE) && (GetKeyState(VK_CONTROL) >= 0) && !FIncrementalSearch.IsEmpty()))
  10737. {
  10738. IncrementalSearch(FIncrementalSearch + Key, false, false);
  10739. Key = 0;
  10740. }
  10741. }
  10742. }
  10743. //---------------------------------------------------------------------------
  10744. void __fastcall TCustomScpExplorerForm::ResetIncrementalSearch()
  10745. {
  10746. if (!FIncrementalSearch.IsEmpty())
  10747. {
  10748. FIncrementalSearch = L"";
  10749. DirView(osCurrent)->UpdateStatusBar();
  10750. }
  10751. }
  10752. //---------------------------------------------------------------------------
  10753. void __fastcall TCustomScpExplorerForm::IncrementalSearch(const UnicodeString & Text, bool SkipCurrent, bool Reverse)
  10754. {
  10755. TListItem * Item = SearchFile(Text, SkipCurrent, Reverse);
  10756. if (Item == NULL)
  10757. {
  10758. MessageBeep(MB_ICONHAND);
  10759. }
  10760. else
  10761. {
  10762. TCustomDirView * ADirView = DirView(osCurrent);
  10763. {
  10764. TAutoNestingCounter Guard(FIncrementalSearching);
  10765. ADirView->FocusItem(Item);
  10766. if (ADirView->NortonLike == nlOff)
  10767. {
  10768. if ((ADirView->Selected != Item) ||
  10769. (ADirView->SelCount > 1))
  10770. {
  10771. ADirView->ClearSelection();
  10772. ADirView->Selected = Item;
  10773. }
  10774. }
  10775. }
  10776. FIncrementalSearch = Text;
  10777. Item->MakeVisible(false);
  10778. TListItem * NextItem = SearchFile(Text, true, Reverse);
  10779. FIncrementalSearchHaveNext = (NextItem != NULL) && (NextItem != Item);
  10780. ADirView->UpdateStatusBar();
  10781. }
  10782. }
  10783. //---------------------------------------------------------------------------
  10784. TListItem * __fastcall TCustomScpExplorerForm::GetNextFile(TListItem * Item, bool Reverse)
  10785. {
  10786. int Index = Item->Index;
  10787. if (!Reverse)
  10788. {
  10789. Index++;
  10790. if (Index >= Item->Owner->Count)
  10791. {
  10792. Index = 0;
  10793. }
  10794. }
  10795. else
  10796. {
  10797. Index--;
  10798. if (Index < 0)
  10799. {
  10800. Index = Item->Owner->Count - 1;
  10801. }
  10802. }
  10803. return Item->Owner->Item[Index];
  10804. }
  10805. //---------------------------------------------------------------------------
  10806. TListItem * __fastcall TCustomScpExplorerForm::SearchFile(const UnicodeString & Text, bool SkipCurrent, bool Reverse)
  10807. {
  10808. TCustomDirView * ADirView = DirView(osCurrent);
  10809. if (ADirView->Items->Count == 0)
  10810. {
  10811. return NULL;
  10812. }
  10813. else
  10814. {
  10815. TListItem * CurrentItem = (ADirView->ItemFocused != NULL) ? ADirView->ItemFocused : ADirView->Items->Item[0];
  10816. TListItem * Item = CurrentItem;
  10817. if (SkipCurrent)
  10818. {
  10819. Item = DebugNotNull(GetNextFile(Item, Reverse));
  10820. }
  10821. while (true)
  10822. {
  10823. bool Matches = false;
  10824. switch (WinConfiguration->PanelSearch)
  10825. {
  10826. case isNameStartOnly:
  10827. Matches = ContainsTextSemiCaseSensitive(ADirView->ItemFileName(Item).SubString(1, Text.Length()), Text);
  10828. break;
  10829. case isName:
  10830. Matches = ContainsTextSemiCaseSensitive(ADirView->ItemFileName(Item), Text);
  10831. break;
  10832. case isAll:
  10833. int ColCount = (ADirView->ViewStyle == vsReport) ? ADirView->ColProperties->Count : 1;
  10834. int Index = 0;
  10835. while ((Index < ColCount) && !Matches)
  10836. {
  10837. bool Visible = (ADirView->ViewStyle == vsReport) ? ADirView->ColProperties->Visible[Index] : true;
  10838. if (Visible)
  10839. {
  10840. Matches = ContainsTextSemiCaseSensitive(ADirView->GetColumnText(Item, Index), Text);
  10841. }
  10842. Index++;
  10843. }
  10844. break;
  10845. }
  10846. if (Matches)
  10847. {
  10848. return Item;
  10849. }
  10850. Item = GetNextFile(Item, Reverse);
  10851. if (Item == CurrentItem)
  10852. {
  10853. return NULL;
  10854. }
  10855. }
  10856. }
  10857. }
  10858. //---------------------------------------------------------------------------
  10859. void __fastcall TCustomScpExplorerForm::DirViewExit(TObject *)
  10860. {
  10861. ResetIncrementalSearch();
  10862. }
  10863. //---------------------------------------------------------------------------
  10864. void __fastcall TCustomScpExplorerForm::CMDialogKey(TWMKeyDown & Message)
  10865. {
  10866. if (Message.CharCode == VK_TAB)
  10867. {
  10868. if (!FIncrementalSearch.IsEmpty())
  10869. {
  10870. TShiftState Shift = KeyDataToShiftState(Message.KeyData);
  10871. bool Reverse = Shift.Contains(ssShift);
  10872. IncrementalSearch(FIncrementalSearch, true, Reverse);
  10873. Message.Result = 1;
  10874. return;
  10875. }
  10876. }
  10877. else if (Message.CharCode == VK_ESCAPE)
  10878. {
  10879. if (!FIncrementalSearch.IsEmpty())
  10880. {
  10881. ResetIncrementalSearch();
  10882. Message.Result = 1;
  10883. return;
  10884. }
  10885. }
  10886. TForm::Dispatch(&Message);
  10887. }
  10888. //---------------------------------------------------------------------------
  10889. void __fastcall TCustomScpExplorerForm::Deactivate()
  10890. {
  10891. TForm::Deactivate();
  10892. // E.g. when switching to an internal editor window
  10893. ResetIncrementalSearch();
  10894. }
  10895. //---------------------------------------------------------------------------
  10896. void __fastcall TCustomScpExplorerForm::ApplicationEventsDeactivate(TObject *)
  10897. {
  10898. // When switching to another application
  10899. ResetIncrementalSearch();
  10900. }
  10901. //---------------------------------------------------------------------------
  10902. void __fastcall TCustomScpExplorerForm::ApplicationEventsModalBegin(TObject *)
  10903. {
  10904. ResetIncrementalSearch();
  10905. }
  10906. //---------------------------------------------------------------------------
  10907. void __fastcall TCustomScpExplorerForm::DirViewChangeFocus(TObject *, TListItem *)
  10908. {
  10909. if (FIncrementalSearching <= 0)
  10910. {
  10911. ResetIncrementalSearch();
  10912. }
  10913. }
  10914. //---------------------------------------------------------------------------
  10915. void __fastcall TCustomScpExplorerForm::RemoteStatusBarMouseDown(TObject *, TMouseButton, TShiftState, int, int)
  10916. {
  10917. CountClicksForWindowPrint(this);
  10918. }
  10919. //---------------------------------------------------------------------------
  10920. void __fastcall TCustomScpExplorerForm::RemoteDirViewResize(TObject *)
  10921. {
  10922. CenterReconnectToolbar();
  10923. }
  10924. //---------------------------------------------------------------------------
  10925. void __fastcall TCustomScpExplorerForm::BrowseFile()
  10926. {
  10927. if (RemoteDirView->ItemFocused != NULL)
  10928. {
  10929. RemoteDirView->ItemFocused->Selected = true;
  10930. }
  10931. }
  10932. //---------------------------------------------------------------------------
  10933. void __fastcall TCustomScpExplorerForm::UpdateQueueFileList()
  10934. {
  10935. if (!FQueueStatusUpdating)
  10936. {
  10937. bool Refresh;
  10938. if ((FQueueStatus != NULL) && QueueFileList->Visible)
  10939. {
  10940. Refresh = FQueueStatus->UpdateFileList(FQueueController->GetFocusedPrimaryItem(), FQueueFileList.get());
  10941. }
  10942. else
  10943. {
  10944. if (FQueueFileList->GetCount() > 0)
  10945. {
  10946. FQueueFileList->Clear();
  10947. Refresh = true;
  10948. }
  10949. }
  10950. int Count = FQueueFileList->GetCount();
  10951. if (QueueFileList->Items->Count != Count)
  10952. {
  10953. DebugAssert(Refresh);
  10954. QueueFileList->Items->Count = Count;
  10955. }
  10956. if (Refresh)
  10957. {
  10958. QueueFileList->Invalidate();
  10959. }
  10960. DebugAssert(QueueFileList->Items->Count == FQueueFileList->GetCount());
  10961. }
  10962. }
  10963. //---------------------------------------------------------------------------
  10964. void __fastcall TCustomScpExplorerForm::QueueFileListData(TObject *, TListItem * Item)
  10965. {
  10966. Item->Caption = FQueueFileList->GetFileName(Item->Index);
  10967. }
  10968. //---------------------------------------------------------------------------
  10969. void __fastcall TCustomScpExplorerForm::QueueView3Change(TObject *, TListItem *, TItemChange)
  10970. {
  10971. // Should be improved, once we do not refresh the list when switching between details of the same batch.
  10972. // See TSynchronizeChecklistDialog::UpdateTimer
  10973. if (QueueFileList->Items->Count > 0)
  10974. {
  10975. QueueFileList->Items->Item[0]->MakeVisible(false);
  10976. }
  10977. UpdateQueueFileList();
  10978. }
  10979. //---------------------------------------------------------------------------
  10980. void __fastcall TCustomScpExplorerForm::QueueLabelGetStatus(TCustomPathLabel *, bool & Active)
  10981. {
  10982. Active = QueueView3->Focused() || QueueFileList->Focused();
  10983. }
  10984. //---------------------------------------------------------------------------
  10985. void __fastcall TCustomScpExplorerForm::QueueFileListEnterExit(TObject *)
  10986. {
  10987. QueueLabelUpdateStatus();
  10988. }
  10989. //---------------------------------------------------------------------------
  10990. void __fastcall TCustomScpExplorerForm::QueueFileListCustomDrawItem(
  10991. TCustomListView * Sender, TListItem * Item, TCustomDrawState, bool & DebugUsedArg(DefaultDraw))
  10992. {
  10993. int State = FQueueFileList->GetState(Item->Index);
  10994. if (State == qfsProcessed)
  10995. {
  10996. Sender->Canvas->Font->Color = clGrayText;
  10997. }
  10998. }
  10999. //---------------------------------------------------------------------------
  11000. void __fastcall TCustomScpExplorerForm::QueueFileListColumnAutoSize()
  11001. {
  11002. // We do not really need whole width, so we always preemptivelly keep free space for the vertical scrollbar
  11003. QueueFileList->Column[0]->Width =
  11004. QueueFileList->ClientWidth - GetSystemMetricsForControl(QueueFileList, SM_CXVSCROLL) - ScaleByTextHeight(QueueFileList, 8);
  11005. }
  11006. //---------------------------------------------------------------------------
  11007. void __fastcall TCustomScpExplorerForm::QueueFileListResize(TObject *)
  11008. {
  11009. QueueFileListColumnAutoSize();
  11010. }
  11011. //---------------------------------------------------------------------------
  11012. void __fastcall TCustomScpExplorerForm::CloseApp()
  11013. {
  11014. // Called from TNonVisualDataModule::ExplorerActionsExecute, which sets busy flag.
  11015. // FormCloseQuery must check busy state, so that the application cannot be closed while busy by Alt+F4 and X button.
  11016. // So we clear the flag here.
  11017. DebugAssert(NonVisualDataModule->Busy);
  11018. NonVisualDataModule->EndBusy();
  11019. try
  11020. {
  11021. Close();
  11022. }
  11023. __finally
  11024. {
  11025. NonVisualDataModule->StartBusy();
  11026. }
  11027. }
  11028. //---------------------------------------------------------------------------
  11029. bool __fastcall TCustomScpExplorerForm::IsActiveTerminal(TTerminal * Terminal)
  11030. {
  11031. return (Terminal != NULL) && Terminal->Active;
  11032. }
  11033. //---------------------------------------------------------------------------
  11034. bool __fastcall TCustomScpExplorerForm::HasManagedSession()
  11035. {
  11036. return (ManagedSession != NULL);
  11037. }
  11038. //---------------------------------------------------------------------------
  11039. bool __fastcall TCustomScpExplorerForm::HasActiveTerminal()
  11040. {
  11041. return IsActiveTerminal(Terminal);
  11042. }
  11043. //---------------------------------------------------------------------------
  11044. void TCustomScpExplorerForm::LocalLocalCopy(
  11045. TFileOperation, TOperationSide, bool DebugUsedArg(OnFocused), bool DebugUsedArg(NoConfirmation),
  11046. bool DebugUsedArg(DragDrop), unsigned int DebugUsedArg(Flags))
  11047. {
  11048. DebugFail();
  11049. }
  11050. //---------------------------------------------------------------------------
  11051. void TCustomScpExplorerForm::LocalLocalCopyCommand(TFileOperation Operation, TOperationSide Side, bool OnFocused, unsigned int Flags)
  11052. {
  11053. LocalLocalCopy(Operation, Side, OnFocused, !WinConfiguration->ConfirmTransferring, false, Flags);
  11054. }
  11055. //---------------------------------------------------------------------------
  11056. void __fastcall TCustomScpExplorerForm::SessionsPageControlResize(TObject *)
  11057. {
  11058. // 1) Changing tab list triggers the OnResize.
  11059. // 2) Is called in TForm constructor, when we are not ready to call SessionListChanged (particularly when starting with colored session)
  11060. if (!FUpdatingSessionTabs && (FSessionColors != NULL))
  11061. {
  11062. // UpdateTabsCaptionTruncation should be enough
  11063. SessionListChanged(true);
  11064. }
  11065. }
  11066. //---------------------------------------------------------------------------
  11067. UnicodeString TCustomScpExplorerForm::GetSessionPath(TManagedTerminal * ASession, TOperationSide Side)
  11068. {
  11069. if (ASession == ManagedSession)
  11070. {
  11071. UpdateSession(ASession);
  11072. }
  11073. TSessionData * Data = ASession->StateData;
  11074. UnicodeString Result;
  11075. if (Side == osLocal)
  11076. {
  11077. Result = Data->LocalDirectory;
  11078. }
  11079. else if (DebugAlwaysTrue(Side == osOther))
  11080. {
  11081. Result = ASession->LocalBrowser ? Data->OtherLocalDirectory : Data->RemoteDirectory;
  11082. }
  11083. return Result;
  11084. }
  11085. //---------------------------------------------------------------------------
  11086. UnicodeString TCustomScpExplorerForm::GetTabHintSessionDetails(TManagedTerminal * ASession)
  11087. {
  11088. UnicodeString Title = TTerminalManager::Instance()->GetSessionTitle(ASession, false);
  11089. UnicodeString Result = ASession->SessionData->DefaultSessionName;
  11090. if (Title == Result)
  11091. {
  11092. Result = EmptyStr;
  11093. }
  11094. else
  11095. {
  11096. Result += L"\n";
  11097. }
  11098. return Result;
  11099. }
  11100. //---------------------------------------------------------------------------
  11101. UnicodeString TCustomScpExplorerForm::GetTabHintDetails(TManagedTerminal * ASession)
  11102. {
  11103. UnicodeString Result = GetTabHintSessionDetails(ASession);
  11104. if (!ASession->Active)
  11105. {
  11106. Result += LoadStr(STATUS_NOT_CONNECTED2);
  11107. }
  11108. else
  11109. {
  11110. Result += GetSessionPath(ASession, osRemote);
  11111. }
  11112. return Result;
  11113. }
  11114. //---------------------------------------------------------------------------
  11115. UnicodeString TCustomScpExplorerForm::GetNewTabHintDetails()
  11116. {
  11117. return LoadStr(NEW_REMOTE_TAB_HINT);
  11118. }
  11119. //---------------------------------------------------------------------------
  11120. void __fastcall TCustomScpExplorerForm::SessionsPageControlTabHint(TPageControl *, int Index, UnicodeString & Hint)
  11121. {
  11122. if (Index >= 0)
  11123. {
  11124. TThemeTabSheet * TabSheet = SessionsPageControl->Pages[Index];
  11125. TManagedTerminal * TabSession = GetSessionTabSession(TabSheet);
  11126. if (TabSession == NULL)
  11127. {
  11128. UnicodeString NewTabTabCaption = GetNewTabTabCaption();
  11129. if (NewTabTabCaption != TabSheet->Caption)
  11130. {
  11131. Hint = NewTabTabCaption + L"|" + GetNewTabHintDetails();
  11132. }
  11133. }
  11134. else
  11135. {
  11136. TTerminalManager * Manager = TTerminalManager::Instance();
  11137. UnicodeString TabCaption = Manager->GetSessionTitle(TabSession, true);
  11138. Hint = TabCaption + L"|" + GetTabHintDetails(TabSession);
  11139. }
  11140. }
  11141. }
  11142. //---------------------------------------------------------------------------
  11143. typedef std::vector<TListItem *> TListItemsVector;
  11144. struct TCalculateSizeOperation
  11145. {
  11146. TCalculateSizeStats Stats;
  11147. TListItemsVector ListItems;
  11148. size_t Index;
  11149. };
  11150. //---------------------------------------------------------------------------
  11151. void TCustomScpExplorerForm::CalculateDirectorySizes(TOperationSide Side)
  11152. {
  11153. TCalculateSizeOperation CalculateSizeOperation;
  11154. Side = GetSide(Side);
  11155. TCustomDirView * ADirView = DirView(Side);
  11156. bool LocalSide = IsSideLocalBrowser(Side);
  11157. std::unique_ptr<TStrings> AllFileList(ADirView->CreateFileList(false, LocalSide, NULL, true));
  11158. std::unique_ptr<TStrings> FileList(new TStringList());
  11159. for (int Index = 0; Index < AllFileList->Count; Index++)
  11160. {
  11161. TListItem * Item = DebugNotNull(dynamic_cast<TListItem *>(AllFileList->Objects[Index]));
  11162. if (ADirView->ItemIsDirectory(Item))
  11163. {
  11164. FileList->AddObject(AllFileList->Strings[Index], ADirView->ItemData(Item));
  11165. CalculateSizeOperation.ListItems.push_back(Item);
  11166. }
  11167. }
  11168. CalculateSizeOperation.Index = 0;
  11169. __int64 Size = 0;
  11170. TValueRestorer<TCalculateSizeOperation *> DirectorySizeOperationRestorer(FCalculateSizeOperation);
  11171. FCalculateSizeOperation = &CalculateSizeOperation;
  11172. TValueRestorer<TFileOperationFinishedEvent> OnFileOperationFinishedRestorer(FOnFileOperationFinished);
  11173. FOnFileOperationFinished = DirectorySizeCalculated;
  11174. TValueRestorer<TFileOperation> PrimaryOperationRestorer(FPrimaryOperation);
  11175. FPrimaryOperation = foCalculateSize;
  11176. TCalculatedSizes CalculatedSizes;
  11177. CalculateSizeOperation.Stats.CalculatedSizes = &CalculatedSizes;
  11178. if (LocalSide)
  11179. {
  11180. TValueRestorer<TOperationSide> ProgressSideRestorer(FProgressSide);
  11181. FProgressSide = Side;
  11182. ManagedSession->CalculateLocalFilesSize(FileList.get(), Size, NULL, true, NULL, &CalculatedSizes);
  11183. }
  11184. else
  11185. {
  11186. TCalculateSizeParams Params;
  11187. Params.Stats = &CalculateSizeOperation.Stats;
  11188. Params.UseCache = true;
  11189. Terminal->CalculateFilesSize(FileList.get(), Size, Params);
  11190. }
  11191. }
  11192. //---------------------------------------------------------------------------
  11193. void __fastcall TCustomScpExplorerForm::DirectorySizeCalculated(TOperationSide Side, const UnicodeString & FileName, bool Success)
  11194. {
  11195. DebugAlwaysTrue(FCalculateSizeOperation != NULL);
  11196. TListItem * Item = VisualiseOperationFinished(Side, FileName, false);
  11197. TCustomDirView * ADirView = dynamic_cast<TCustomDirView *>(Item->Owner->Owner);
  11198. // optimization
  11199. bool GotExpectedItem =
  11200. (FCalculateSizeOperation->Index < FCalculateSizeOperation->ListItems.size()) &&
  11201. (FCalculateSizeOperation->ListItems[FCalculateSizeOperation->Index] == Item);
  11202. int Index = -1;
  11203. if (DebugAlwaysTrue(GotExpectedItem))
  11204. {
  11205. Index = FCalculateSizeOperation->Index;
  11206. FCalculateSizeOperation->Index++;
  11207. }
  11208. else
  11209. {
  11210. TListItemsVector::iterator I =
  11211. std::find(FCalculateSizeOperation->ListItems.begin(), FCalculateSizeOperation->ListItems.end(), Item);
  11212. if (DebugAlwaysTrue(I != FCalculateSizeOperation->ListItems.end()))
  11213. {
  11214. Index = I - FCalculateSizeOperation->ListItems.begin();
  11215. }
  11216. }
  11217. if (DebugAlwaysTrue(Index >= 0) && Success)
  11218. {
  11219. __int64 Size = (*FCalculateSizeOperation->Stats.CalculatedSizes)[Index];
  11220. ADirView->SetItemCalculatedSize(Item, Size);
  11221. }
  11222. }
  11223. //---------------------------------------------------------------------------
  11224. void __fastcall TCustomScpExplorerForm::MessageDockRequestDock(TObject *, TTBCustomDockableWindow *, bool & Accept)
  11225. {
  11226. Accept = false;
  11227. }
  11228. //---------------------------------------------------------------------------