CustomScpExplorer.cpp 394 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838
  1. //---------------------------------------------------------------------------
  2. #define NO_WIN32_LEAN_AND_MEAN
  3. #include <vcl.h>
  4. #pragma hdrstop
  5. #include <Common.h>
  6. #include "CustomScpExplorer.h"
  7. #include <algorithm>
  8. #include <Bookmarks.h>
  9. #include <Interface.h>
  10. #include <Exceptions.h>
  11. #include <CoreMain.h>
  12. #include <FileSystems.h>
  13. #include <TextsCore.h>
  14. #include <TextsWin.h>
  15. #include <HelpWin.h>
  16. #include <VCLCommon.h>
  17. #include <Progress.h>
  18. #include <SynchronizeProgress.h>
  19. #include <DragExt.h>
  20. #include <WinApi.h>
  21. #include "GUITools.h"
  22. #include "NonVisual.h"
  23. #include "Glyphs.h"
  24. #include "Tools.h"
  25. #include "WinConfiguration.h"
  26. #include "TerminalManager.h"
  27. #include "EditorManager.h"
  28. #include "ProgParams.h"
  29. #include "Setup.h"
  30. #include <Consts.hpp>
  31. #include <DateUtils.hpp>
  32. #include <TB2Common.hpp>
  33. #include <DirectoryMonitor.hpp>
  34. #include <System.IOUtils.hpp>
  35. //---------------------------------------------------------------------------
  36. #pragma package(smart_init)
  37. #pragma link "CustomDirView"
  38. #pragma link "CustomUnixDirView"
  39. #pragma link "IEListView"
  40. #pragma link "NortonLikeListView"
  41. #pragma link "UnixDirView"
  42. #pragma link "CustomDriveView"
  43. #pragma link "UnixDriveView"
  44. #pragma link "CustomDriveView"
  45. #pragma link "TB2Dock"
  46. #pragma link "TBXStatusBars"
  47. #pragma link "TB2Item"
  48. #pragma link "TB2Toolbar"
  49. #pragma link "ThemePageControl"
  50. #pragma link "PathLabel"
  51. #pragma resource "*.dfm"
  52. //---------------------------------------------------------------------------
  53. #define WM_COMPONENT_HIDE (WM_WINSCP_USER + 4)
  54. static const int SessionPanelCount = 3;
  55. //---------------------------------------------------------------------------
  56. class TMutexGuard
  57. {
  58. public:
  59. TMutexGuard(HANDLE AMutex, int Message = MUTEX_RELEASE_TIMEOUT,
  60. int Timeout = 5000)
  61. {
  62. FMutex = NULL;
  63. unsigned long WaitResult = WaitForSingleObject(AMutex, Timeout);
  64. if (WaitResult == WAIT_TIMEOUT)
  65. {
  66. throw Exception(LoadStr(MUTEX_RELEASE_TIMEOUT));
  67. }
  68. else
  69. {
  70. FMutex = AMutex;
  71. }
  72. }
  73. ~TMutexGuard()
  74. {
  75. if (FMutex != NULL)
  76. {
  77. ReleaseMutex(FMutex);
  78. }
  79. }
  80. private:
  81. HANDLE FMutex;
  82. };
  83. //---------------------------------------------------------------------------
  84. class TWindowLock
  85. {
  86. public:
  87. TWindowLock(TCustomScpExplorerForm * Form) :
  88. FForm(Form)
  89. {
  90. FForm->LockWindow();
  91. }
  92. ~TWindowLock()
  93. {
  94. FForm->UnlockWindow();
  95. }
  96. private:
  97. TCustomScpExplorerForm * FForm;
  98. };
  99. //---------------------------------------------------------------------------
  100. TAutoBatch::TAutoBatch(TCustomScpExplorerForm * Form) :
  101. FForm(Form)
  102. {
  103. FForm->BatchStart(FBatchStorage);
  104. }
  105. //---------------------------------------------------------------------------
  106. TAutoBatch::~TAutoBatch()
  107. {
  108. FForm->BatchEnd(FBatchStorage);
  109. }
  110. //---------------------------------------------------------------------------
  111. struct TTransferOperationParam
  112. {
  113. TTransferOperationParam();
  114. UnicodeString TargetDirectory;
  115. bool Temp;
  116. bool DragDrop;
  117. int Options;
  118. TAutoSwitch Queue;
  119. };
  120. //---------------------------------------------------------------------------
  121. TTransferOperationParam::TTransferOperationParam()
  122. {
  123. Temp = false;
  124. DragDrop = false;
  125. Options = 0;
  126. Queue = asAuto;
  127. }
  128. //---------------------------------------------------------------------------
  129. class TTransferPresetNoteData : public TObject
  130. {
  131. public:
  132. UnicodeString Message;
  133. };
  134. //---------------------------------------------------------------------------
  135. class TTerminalNoteData : public TObject
  136. {
  137. public:
  138. TManagedTerminal * Terminal;
  139. };
  140. //---------------------------------------------------------------------------
  141. __fastcall TCustomScpExplorerForm::TCustomScpExplorerForm(TComponent* Owner):
  142. FFormRestored(false),
  143. TForm(Owner)
  144. {
  145. AddStartupSequence(L"F");
  146. FInvalid = false;
  147. FCurrentSide = osRemote;
  148. FEverShown = false;
  149. FDocks = new TList();
  150. InitControls();
  151. DebugAssert(NonVisualDataModule && !NonVisualDataModule->ScpExplorer);
  152. // So that UpdateCustomCommandsToolbar called from RestoreParams works
  153. NonVisualDataModule->ScpExplorer = this;
  154. RestoreParams();
  155. ConfigurationChanged();
  156. RemoteDirView->Invalidate();
  157. FAutoOperation = false;
  158. FOnFileOperationFinished = NULL;
  159. FPrimaryOperation = foNone;
  160. FForceExecution = false;
  161. FIgnoreNextDialogChar = 0;
  162. FErrorList = NULL;
  163. FSynchronizeProgressForm = NULL;
  164. FProgressForm = NULL;
  165. FRefreshLocalDirectory = false;
  166. FRefreshRemoteDirectory = false;
  167. FDDExtMapFile = NULL;
  168. // CreateMutexW keeps failing with ERROR_NOACCESS
  169. FDDExtMutex = CreateMutexA(NULL, false, AnsiString(DRAG_EXT_MUTEX).c_str());
  170. DebugAssert(FDDExtMutex != NULL);
  171. FDDTargetControl = NULL;
  172. FDelayedDeletionTimer = NULL;
  173. FDelayedDeletionList = new TStringList();
  174. FDDFileList = NULL;
  175. FPendingTempSpaceWarn = false;
  176. FCapturedLog = NULL;
  177. FDragDropOperation = false;
  178. memset(&FHistoryMenu, 0, sizeof(FHistoryMenu));
  179. FAllowTransferPresetAutoSelect = true;
  180. FSessionChanging = false;
  181. FCopyParamDefault = L"";
  182. FSynchronizeController = NULL;
  183. FPendingQueueActionItem = NULL;
  184. FLockLevel = 0;
  185. FLockSuspendLevel = 0;
  186. FDisabledOnLockSuspend = false;
  187. FAlternativeDelete = false;
  188. FTrayIcon = new ::TTrayIcon(0);
  189. FTrayIcon->OnClick = TrayIconClick;
  190. FMaxQueueLength = 0;
  191. FLastContextPopupScreenPoint = TPoint(-1, -1);
  192. FTransferResumeList = NULL;
  193. FDeletedFiles = NULL;
  194. FMoveToQueue = false;
  195. StandaloneOperation = false;
  196. FOnFeedSynchronizeError = NULL;
  197. FOnSynchronizeAbort = NULL;
  198. FSynchronizeTerminal = NULL;
  199. FNeedSession = false;
  200. FDoNotIdleCurrentTerminal = 0;
  201. FIncrementalSearching = 0;
  202. FIncrementalSearchHaveNext = false;
  203. FQueueFileList.reset(new TQueueFileList());
  204. FProgressSide = osCurrent;
  205. FCalculateSizeOperation = NULL;
  206. FDownloadingFromClipboard = false;
  207. FClipboardFakeMonitorsPendingReset = false;
  208. FEditorManager = new TEditorManager();
  209. FEditorManager->OnFileChange = ExecutedFileChanged;
  210. FEditorManager->OnFileReload = ExecutedFileReload;
  211. FEditorManager->OnFileEarlyClosed = ExecutedFileEarlyClosed;
  212. FEditorManager->OnFileUploadComplete = ExecutedFileUploadComplete;
  213. FLocalEditors = new TList();
  214. FQueueStatus = NULL;
  215. FQueueStatusSection = new TCriticalSection();
  216. FQueueStatusInvalidated = false;
  217. FQueueItemInvalidated = false;
  218. FQueueStatusUpdating = false;
  219. FQueueActedItem = NULL;
  220. FQueueController = new TQueueController(QueueView3);
  221. FQueueScrollOnDragOver = new TListViewScrollOnDragOver(QueueView3, true);
  222. FUserActionTimer = new TTimer(this);
  223. FUserActionTimer->Enabled = false;
  224. FUserActionTimer->Interval = 10;
  225. FUserActionTimer->OnTimer = UserActionTimer;
  226. FNotes = new TStringList();
  227. FNoteTimer = new TTimer(this);
  228. FNoteTimer->Enabled = false;
  229. FNoteTimer->OnTimer = NoteTimer;
  230. FOnNoteClick = NULL;
  231. FOle32Library = LoadLibrary(L"Ole32.dll");
  232. FDragMoveCursor = FOle32Library != NULL ?
  233. LoadCursor(FOle32Library, MAKEINTRESOURCE(2)) : NULL;
  234. UseSystemSettings(this);
  235. TTBXStringList * TransferList = dynamic_cast<TTBXStringList*>(
  236. static_cast<TObject*>(GetComponent(fcTransferList)));
  237. DebugAssert(TransferList != NULL);
  238. FTransferListHoverIndex = -1;
  239. TransferList->OnChange = TransferListChange;
  240. TransferList->OnDrawItem = TransferListDrawItem;
  241. SetSubmenu(dynamic_cast<TTBXCustomItem *>(static_cast<TObject *>(GetComponent(fcColorMenu))), true);
  242. SetSubmenu(NonVisualDataModule->ColorMenuItem, true);
  243. UseDesktopFont(SessionsPageControl);
  244. UpdateSessionsPageControlHeight();
  245. UseDesktopFont(RemoteDirView);
  246. UseDesktopFont(RemoteDriveView);
  247. UseDesktopFont(QueueView3);
  248. UseDesktopFont(QueueFileList);
  249. UseDesktopFont(QueueLabel);
  250. UseDesktopFont(RemoteStatusBar);
  251. reinterpret_cast<TLabel*>(QueueSplitter)->OnDblClick = QueueSplitterDblClick;
  252. QueueSplitter->ShowHint = true;
  253. reinterpret_cast<TLabel*>(QueueFileListSplitter)->OnDblClick = QueueFileListSplitterDblClick;
  254. QueueFileListSplitter->ShowHint = true;
  255. RemotePanelSplitter->ShowHint = true;
  256. UpdateImages();
  257. FCustomCommandMenu = CreateTBXPopupMenu(this);
  258. FCustomCommandLocalFileList = NULL;
  259. FCustomCommandRemoteFileList = NULL;
  260. FSessionColors = new TPngImageList(this);
  261. FSessionColors->ColorDepth = cd32Bit;
  262. AddFixedSessionImages();
  263. SessionsPageControl->Images = FSessionColors;
  264. UpdateQueueLabel();
  265. CheckStoreTransition();
  266. CreateHiddenWindow();
  267. StartUpdates();
  268. }
  269. //---------------------------------------------------------------------------
  270. __fastcall TCustomScpExplorerForm::~TCustomScpExplorerForm()
  271. {
  272. FInvalid = true;
  273. // this has to be one of the very first things to do
  274. StopUpdateThread();
  275. // This is needed when shuting down Windows only. Otherwise it's already set NULL from Execute()
  276. TTerminalManager::Instance()->ScpExplorer = NULL;
  277. delete FCustomCommandLocalFileList;
  278. delete FCustomCommandRemoteFileList;
  279. delete FCustomCommandMenu;
  280. delete FTrayIcon;
  281. FTrayIcon = NULL;
  282. FEditorManager->CloseInternalEditors(ForceCloseInternalEditor);
  283. delete FEditorManager;
  284. ForceCloseLocalEditors();
  285. delete FLocalEditors;
  286. HideFileFindDialog();
  287. if (FDelayedDeletionTimer)
  288. {
  289. DoDelayedDeletion(NULL);
  290. SAFE_DESTROY(FDelayedDeletionTimer);
  291. }
  292. SAFE_DESTROY(FDelayedDeletionList);
  293. // sometimes we do not get DDEnd so the list is not released
  294. SAFE_DESTROY(FDDFileList);
  295. DebugAssert(FSynchronizeController == NULL);
  296. CloseHandle(FDDExtMutex);
  297. FDDExtMutex = NULL;
  298. FreeLibrary(FOle32Library);
  299. FOle32Library = NULL;
  300. FDragMoveCursor = NULL;
  301. DebugAssert(!FErrorList);
  302. if (FEverShown)
  303. {
  304. // when window is never shown (like when running command-line operation),
  305. // particularly window site is not restored correctly (BoundsRect value set
  306. // in RestoreForm gets lost during handle allocation), so we do not want
  307. // it to be stored
  308. StoreParams();
  309. }
  310. ManagedSession = NULL;
  311. Queue = NULL;
  312. DebugAssert(NonVisualDataModule && (NonVisualDataModule->ScpExplorer == this));
  313. NonVisualDataModule->ScpExplorer = NULL;
  314. delete FQueueScrollOnDragOver;
  315. FQueueScrollOnDragOver = NULL;
  316. delete FQueueController;
  317. FQueueController = NULL;
  318. delete FQueueStatusSection;
  319. FQueueStatusSection = NULL;
  320. delete FQueueStatus;
  321. FQueueStatus = NULL;
  322. delete FUserActionTimer;
  323. FUserActionTimer = NULL;
  324. delete FNoteTimer;
  325. delete FNotes;
  326. delete FNoteData;
  327. SAFE_DESTROY(FDocks);
  328. SessionsPageControl->Images = NULL;
  329. // only after clearing Terminal (after SessionListChanged => ... => AddSessionColor is called)
  330. SAFE_DESTROY(FSessionColors);
  331. SAFE_DESTROY(FSessionsDragDropFilesEx);
  332. SAFE_DESTROY(FHistoryMenu[0][0]);
  333. SAFE_DESTROY(FHistoryMenu[0][1]);
  334. SAFE_DESTROY(FHistoryMenu[1][0]);
  335. SAFE_DESTROY(FHistoryMenu[1][1]);
  336. if (FHiddenWindow != NULL)
  337. {
  338. DestroyWindow(FHiddenWindow);
  339. FHiddenWindow = NULL;
  340. }
  341. }
  342. //---------------------------------------------------------------------------
  343. void __fastcall TCustomScpExplorerForm::RefreshPanel(const UnicodeString & Session, const UnicodeString & Path)
  344. {
  345. std::unique_ptr<TSessionData> Data;
  346. if (!Session.IsEmpty())
  347. {
  348. bool DefaultsOnly;
  349. Data.reset(StoredSessions->ParseUrl(Session, NULL, DefaultsOnly));
  350. }
  351. TTerminalManager * Manager = TTerminalManager::Instance();
  352. for (int Index = 0; Index < Manager->Count; Index++)
  353. {
  354. TTerminal * Terminal = Manager->Sessions[Index];
  355. if (Session.IsEmpty() ||
  356. Manager->IsActiveTerminalForSite(Terminal, Data.get()))
  357. {
  358. if (Path.IsEmpty())
  359. {
  360. Terminal->ClearCaches();
  361. }
  362. else
  363. {
  364. Terminal->DirectoryModified(Path, true);
  365. }
  366. }
  367. }
  368. // We should flag a pending refresh for the background terminals or busy foreground terminals
  369. if (HasActiveTerminal() &&
  370. CanCommandLineFromAnotherInstance() &&
  371. (Session.IsEmpty() || Manager->IsActiveTerminalForSite(Terminal, Data.get())) &&
  372. (Path.IsEmpty() || UnixIsChildPath(Path, Terminal->CurrentDirectory)))
  373. {
  374. Terminal->ReloadDirectory();
  375. }
  376. }
  377. //---------------------------------------------------------------------------
  378. void __fastcall TCustomScpExplorerForm::WMCopyData(TMessage & Message)
  379. {
  380. PCOPYDATASTRUCT CopyData = reinterpret_cast<PCOPYDATASTRUCT>(Message.LParam);
  381. AppLogFmt(L"Received copy data message with size %d", (int(CopyData->cbData)));
  382. size_t MessageSize = sizeof(TCopyDataMessage);
  383. bool Result = DebugAlwaysTrue(CopyData->cbData == MessageSize);
  384. if (Result)
  385. {
  386. const TCopyDataMessage & Message = *reinterpret_cast<const TCopyDataMessage *>(CopyData->lpData);
  387. Result = (Message.Version == TCopyDataMessage::Version1);
  388. if (Result)
  389. {
  390. switch (Message.Command)
  391. {
  392. case TCopyDataMessage::CommandCanCommandLine:
  393. Result = CanCommandLineFromAnotherInstance();
  394. if (Result)
  395. {
  396. AppLog(L"Can process command-line from other instance");
  397. }
  398. else
  399. {
  400. AppLog(L"Cannot process command-line from other instance");
  401. }
  402. break;
  403. case TCopyDataMessage::CommandCommandLine:
  404. {
  405. UnicodeString CommandLine(Message.CommandLine);
  406. Result = CommandLineFromAnotherInstance(CommandLine);
  407. }
  408. break;
  409. case TCopyDataMessage::RefreshPanel:
  410. RefreshPanel(Message.Refresh.Session, Message.Refresh.Path);
  411. break;
  412. case TCopyDataMessage::MainWindowCheck:
  413. Result = true;
  414. break;
  415. default:
  416. Result = false;
  417. break;
  418. }
  419. }
  420. }
  421. Message.Result = Result ? 1 : 0;
  422. }
  423. //---------------------------------------------------------------------------
  424. void __fastcall TCustomScpExplorerForm::CreateHiddenWindow()
  425. {
  426. WNDCLASS WindowClass = {0};
  427. WindowClass.lpfnWndProc = DefWindowProc;
  428. WindowClass.hInstance = HInstance;
  429. WindowClass.lpszClassName = HIDDEN_WINDOW_NAME;
  430. FHiddenWindow = NULL;
  431. if (RegisterClass(&WindowClass))
  432. {
  433. FHiddenWindow = CreateWindow(HIDDEN_WINDOW_NAME, L"",
  434. WS_POPUP, 0, 0, 0, 0, 0, 0, HInstance, NULL);
  435. }
  436. }
  437. //---------------------------------------------------------------------------
  438. bool __fastcall TCustomScpExplorerForm::CanConsole()
  439. {
  440. return
  441. HasActiveTerminal() &&
  442. (Terminal->IsCapable[fcAnyCommand] || Terminal->IsCapable[fcSecondaryShell]);
  443. }
  444. //---------------------------------------------------------------------------
  445. bool __fastcall TCustomScpExplorerForm::CanCommandLineFromAnotherInstance()
  446. {
  447. bool Result = Visible && !NonVisualDataModule->Busy;
  448. return Result;
  449. }
  450. //---------------------------------------------------------------------------
  451. bool __fastcall TCustomScpExplorerForm::CommandLineFromAnotherInstance(
  452. const UnicodeString & CommandLine)
  453. {
  454. TProgramParams Params(CommandLine);
  455. bool Result = CanCommandLineFromAnotherInstance() && DebugAlwaysTrue(Params.ParamCount > 0);
  456. if (Result)
  457. {
  458. NonVisualDataModule->StartBusy();
  459. try
  460. {
  461. // this action is initiated from another process,
  462. // so it's likely that our window is not visible,
  463. // and user won't see what is going on
  464. Application->BringToFront();
  465. // reload sessions as we may be asked to open a session
  466. // just stored by another instance
  467. StoredSessions->Reload();
  468. UnicodeString SessionName = Params.Param[1];
  469. std::unique_ptr<TObjectList> DataList(new TObjectList());
  470. try
  471. {
  472. UnicodeString DownloadFile; // unused
  473. int Flags = GetCommandLineParseUrlFlags(&Params);
  474. GetLoginData(SessionName, &Params, DataList.get(), DownloadFile, true, this, Flags);
  475. }
  476. catch (EAbort &)
  477. {
  478. // Happens when opening session in PuTTY and in other situations,
  479. // in which we do not want to fail WM_COPYDATA, as that would cause master instance
  480. // to process the command-line on its own (or to pass it to yet another instance)
  481. }
  482. if (DataList->Count > 0)
  483. {
  484. TTerminalManager * Manager = TTerminalManager::Instance();
  485. Manager->ActiveSession = Manager->NewSessions(DataList.get());
  486. }
  487. }
  488. __finally
  489. {
  490. NonVisualDataModule->EndBusy();
  491. }
  492. }
  493. return Result;
  494. }
  495. //---------------------------------------------------------------------------
  496. TManagedTerminal * TCustomScpExplorerForm::GetTerminal()
  497. {
  498. TManagedTerminal * Result;
  499. if ((FManagedSession != NULL) && !FManagedSession->LocalBrowser)
  500. {
  501. Result = FManagedSession;
  502. }
  503. else
  504. {
  505. Result = NULL;
  506. }
  507. return Result;
  508. }
  509. //---------------------------------------------------------------------------
  510. void __fastcall TCustomScpExplorerForm::SetManagedSession(TManagedTerminal * value)
  511. {
  512. if (FManagedSession != value)
  513. {
  514. SessionChanging();
  515. DoSetManagedSession(value, false);
  516. }
  517. }
  518. //---------------------------------------------------------------------------
  519. void __fastcall TCustomScpExplorerForm::DoSetManagedSession(TManagedTerminal * value, bool Replace)
  520. {
  521. DebugAssert(!Replace || ((value != NULL) && !value->LocalBrowser));
  522. FManagedSession = value;
  523. {
  524. TValueRestorer<bool> AllowTransferPresetAutoSelectRestorer(FAllowTransferPresetAutoSelect);
  525. FAllowTransferPresetAutoSelect = false;
  526. TAutoFlag SessionChangingFlag(FSessionChanging);
  527. SessionChanged(Replace);
  528. }
  529. if (Terminal != NULL)
  530. {
  531. TransferPresetAutoSelect();
  532. }
  533. // Update app and tab titles, prevented above by FSessionChanging.
  534. UpdateControls();
  535. }
  536. //---------------------------------------------------------------------------
  537. void __fastcall TCustomScpExplorerForm::ReplaceTerminal(TManagedTerminal * value)
  538. {
  539. DoSetManagedSession(value, true);
  540. }
  541. //---------------------------------------------------------------------------
  542. void __fastcall TCustomScpExplorerForm::SessionChanging()
  543. {
  544. if (ManagedSession != NULL)
  545. {
  546. UpdateSession(ManagedSession);
  547. }
  548. }
  549. //---------------------------------------------------------------------------
  550. void __fastcall TCustomScpExplorerForm::SessionChanged(bool Replaced)
  551. {
  552. if (ManagedSession != NULL)
  553. {
  554. UpdateSessionColor(static_cast<TColor>(ManagedSession->StateData->Color));
  555. }
  556. if (!TTerminalManager::Instance()->Updating)
  557. {
  558. SessionListChanged();
  559. }
  560. if (Terminal != NULL)
  561. {
  562. TObject * State = NULL;
  563. if (WinConfiguration->PreservePanelState)
  564. {
  565. State = Terminal->RemoteExplorerState;
  566. DirView(osRemote)->AnnounceState(State);
  567. }
  568. try
  569. {
  570. if (Replaced)
  571. {
  572. RemoteDirView->ReplaceTerminal(Terminal);
  573. }
  574. else
  575. {
  576. RemoteDirView->Terminal = Terminal;
  577. }
  578. if (Terminal->Active)
  579. {
  580. Terminal->RefreshDirectory();
  581. }
  582. if (State != NULL)
  583. {
  584. DirView(osRemote)->RestoreState(State);
  585. }
  586. }
  587. __finally
  588. {
  589. DirView(osRemote)->AnnounceState(NULL);
  590. }
  591. }
  592. else
  593. {
  594. RemoteDirView->Terminal = NULL;
  595. }
  596. InitStatusBar();
  597. UpdateTransferList();
  598. // Update panels Enable state before refreshing the labels
  599. UpdateControls();
  600. }
  601. //---------------------------------------------------------------------------
  602. void __fastcall TCustomScpExplorerForm::SetQueue(TTerminalQueue * value)
  603. {
  604. if (Queue != value)
  605. {
  606. if (FQueue != NULL)
  607. {
  608. FQueue->OnListUpdate = NULL;
  609. FQueue->OnQueueItemUpdate = NULL;
  610. }
  611. FQueue = value;
  612. if (FQueue != NULL)
  613. {
  614. DebugAssert(FQueue->OnListUpdate == NULL);
  615. FQueue->OnListUpdate = QueueListUpdate;
  616. DebugAssert(FQueue->OnQueueItemUpdate == NULL);
  617. FQueue->OnQueueItemUpdate = QueueItemUpdate;
  618. }
  619. QueueChanged();
  620. }
  621. }
  622. //---------------------------------------------------------------------------
  623. void __fastcall TCustomScpExplorerForm::QueueView3Deletion(TObject * /*Sender*/,
  624. TListItem * Item)
  625. {
  626. if (FQueueActedItem == Item)
  627. {
  628. FQueueActedItem = NULL;
  629. if ((QueueView3->PopupMenu != NULL) &&
  630. (QueueView3->PopupMenu->PopupComponent == QueueView3))
  631. {
  632. // rather "trick", suggested by Jordan on jrsoftware.toolbar2000
  633. ReleaseCapture();
  634. }
  635. }
  636. if (Item->Data == FPendingQueueActionItem)
  637. {
  638. FPendingQueueActionItem = NULL;
  639. }
  640. }
  641. //---------------------------------------------------------------------------
  642. bool TCustomScpExplorerForm::IsAnythingQueued()
  643. {
  644. return (FQueueStatus != NULL) && (FQueueStatus->ActiveAndPendingPrimaryCount > 0);
  645. }
  646. //---------------------------------------------------------------------------
  647. void __fastcall TCustomScpExplorerForm::UpdateQueueStatus(bool QueueChanging)
  648. {
  649. {
  650. TGuard Guard(FQueueStatusSection);
  651. FQueueStatusInvalidated = false;
  652. if (FQueue != NULL)
  653. {
  654. FQueueStatus = FQueue->CreateStatus(FQueueStatus);
  655. }
  656. }
  657. if ((FQueueStatus != NULL) && (FQueueStatus->Count > FMaxQueueLength))
  658. {
  659. FMaxQueueLength = FQueueStatus->Count;
  660. Configuration->Usage->SetMax(L"MaxQueueLength", FMaxQueueLength);
  661. }
  662. {
  663. TAutoFlag Flag(FQueueStatusUpdating);
  664. FQueueController->UpdateQueueStatus(FQueueStatus);
  665. }
  666. UpdateQueueFileList();
  667. SetQueueProgress();
  668. UpdateQueueView();
  669. bool IsEmpty = !IsAnythingQueued();
  670. if (IsEmpty && (Terminal != NULL))
  671. {
  672. TOnceDoneOperation OnceDoneOperation =
  673. NonVisualDataModule->CurrentQueueOnceEmptyOperation();
  674. NonVisualDataModule->ResetQueueOnceEmptyOperation();
  675. if ((FQueue != NULL) && !WinConfiguration->EnableQueueByDefault && !QueueChanging)
  676. {
  677. FQueue->Enabled = false;
  678. }
  679. if ((OnceDoneOperation != odoIdle) && !NonVisualDataModule->Busy)
  680. {
  681. Terminal->CloseOnCompletion(OnceDoneOperation, LoadStr(CLOSED_ON_QUEUE_EMPTY));
  682. }
  683. }
  684. }
  685. //---------------------------------------------------------------------------
  686. UnicodeString __fastcall TCustomScpExplorerForm::GetQueueProgressTitle()
  687. {
  688. UnicodeString Result;
  689. if (FQueueStatus != NULL)
  690. {
  691. if (FQueueStatus->IsOnlyOneActiveAndNoPending())
  692. {
  693. TFileOperationProgressType * ProgressData =
  694. FQueueStatus->Items[FQueueStatus->DoneCount]->ProgressData;
  695. if ((ProgressData != NULL) && ProgressData->InProgress)
  696. {
  697. Result = TProgressForm::ProgressStr(NULL, ProgressData);
  698. }
  699. }
  700. else if (FQueueStatus->ActiveAndPendingPrimaryCount > 1)
  701. {
  702. Result = FMTLOAD(PROGRESS_IN_QUEUE, (FQueueStatus->ActiveAndPendingPrimaryCount));
  703. }
  704. }
  705. return Result;
  706. }
  707. //---------------------------------------------------------------------------
  708. void __fastcall TCustomScpExplorerForm::UpdateQueueView()
  709. {
  710. ComponentVisible[fcQueueView] =
  711. !IsLocalBrowserMode() &&
  712. ((WinConfiguration->QueueView.Show == qvShow) ||
  713. ((WinConfiguration->QueueView.Show == qvHideWhenEmpty) &&
  714. (FQueueStatus != NULL) && (FQueueStatus->Count > 0)));
  715. }
  716. //---------------------------------------------------------------------------
  717. void __fastcall TCustomScpExplorerForm::QueueChanged()
  718. {
  719. if (FQueueStatus != NULL)
  720. {
  721. // UpdateFileList implementation relies on the status being recreated when session changes
  722. delete FQueueStatus;
  723. FQueueStatus = NULL;
  724. }
  725. UpdateQueueStatus(true);
  726. }
  727. //---------------------------------------------------------------------------
  728. void __fastcall TCustomScpExplorerForm::QueueListUpdate(TTerminalQueue * Queue)
  729. {
  730. if (FQueue == Queue)
  731. {
  732. FQueueStatusInvalidated = true;
  733. }
  734. }
  735. //---------------------------------------------------------------------------
  736. void __fastcall TCustomScpExplorerForm::QueueItemUpdate(TTerminalQueue * Queue,
  737. TQueueItem * Item)
  738. {
  739. if (FQueue == Queue)
  740. {
  741. TGuard Guard(FQueueStatusSection);
  742. // this may be running in parallel with QueueChanged
  743. if (FQueueStatus != NULL)
  744. {
  745. TQueueItemProxy * QueueItem = FQueueStatus->FindByQueueItem(Item);
  746. if ((Item->Status == TQueueItem::qsDone) && (Terminal != NULL))
  747. {
  748. FRefreshLocalDirectory = (QueueItem == NULL) ||
  749. (!QueueItem->Info->ModifiedLocal.IsEmpty());
  750. FRefreshRemoteDirectory = (QueueItem == NULL) ||
  751. (!QueueItem->Info->ModifiedRemote.IsEmpty());
  752. }
  753. if (QueueItem != NULL)
  754. {
  755. QueueItem->UserData = (void*)true;
  756. FQueueItemInvalidated = true;
  757. }
  758. }
  759. }
  760. }
  761. //---------------------------------------------------------------------------
  762. bool __fastcall TCustomScpExplorerForm::IsQueueAutoPopup()
  763. {
  764. // during standalone editing, we have no way to see/control queue,
  765. // so we have to always popup prompts automatically
  766. return StandaloneOperation || GUIConfiguration->QueueAutoPopup;
  767. }
  768. //---------------------------------------------------------------------------
  769. void __fastcall TCustomScpExplorerForm::RefreshQueueItems()
  770. {
  771. if (FQueueStatus != NULL)
  772. {
  773. bool QueueAutoPopup = IsQueueAutoPopup();
  774. bool NeedRefresh = FQueueController->NeedRefresh();
  775. bool Refresh = FQueueItemInvalidated || NeedRefresh;
  776. FQueueItemInvalidated = false;
  777. int Limit = Refresh ? FQueueStatus->Count : FQueueStatus->DoneAndActiveCount;
  778. bool Updated = false;
  779. TQueueItemProxy * QueueItem;
  780. bool WasUserAction;
  781. for (int Index = 0; Index < Limit; Index++)
  782. {
  783. bool Update = false;
  784. QueueItem = FQueueStatus->Items[Index];
  785. WasUserAction = TQueueItem::IsUserActionStatus(QueueItem->Status);
  786. if (!NonVisualDataModule->Busy && QueueAutoPopup && WasUserAction &&
  787. (FPendingQueueActionItem == NULL))
  788. {
  789. FPendingQueueActionItem = QueueItem;
  790. FUserActionTimer->Enabled = true;
  791. }
  792. if ((bool)QueueItem->UserData)
  793. {
  794. QueueItem->UserData = (void*)false;
  795. QueueItem->Update();
  796. Updated = true;
  797. Update = true;
  798. }
  799. else if (FQueueController->QueueItemNeedsFrequentRefresh(QueueItem) || NeedRefresh)
  800. {
  801. Update = true;
  802. }
  803. if (Update)
  804. {
  805. FQueueController->RefreshQueueItem(QueueItem);
  806. }
  807. }
  808. if (Updated)
  809. {
  810. NonVisualDataModule->UpdateNonVisibleActions();
  811. SetQueueProgress();
  812. UpdateQueueFileList();
  813. }
  814. }
  815. }
  816. //---------------------------------------------------------------------------
  817. void __fastcall TCustomScpExplorerForm::SetTaskbarListProgressState(TBPFLAG Flags)
  818. {
  819. if (FTaskbarList != NULL)
  820. {
  821. // Could now use Application->MainFormHandle, now that we implement Application->OnGetMainFormHandle
  822. FTaskbarList->SetProgressState(GetMainForm()->Handle, Flags);
  823. }
  824. }
  825. //---------------------------------------------------------------------------
  826. void __fastcall TCustomScpExplorerForm::SetTaskbarListProgressValue(int Progress)
  827. {
  828. if (Progress >= 0)
  829. {
  830. if (FTaskbarList != NULL)
  831. {
  832. FTaskbarList->SetProgressValue(GetMainForm()->Handle, Progress, 100);
  833. }
  834. }
  835. else
  836. {
  837. SetTaskbarListProgressState(TBPF_INDETERMINATE);
  838. }
  839. }
  840. //---------------------------------------------------------------------------
  841. void __fastcall TCustomScpExplorerForm::SetTaskbarListProgressValue(TFileOperationProgressType * ProgressData)
  842. {
  843. int OverallProgress;
  844. // FProgressForm is null when this is called from SetQueueProgress
  845. if ((FProgressForm != NULL) && (FProgressForm->SynchronizeProgress != NULL))
  846. {
  847. OverallProgress = FProgressForm->SynchronizeProgress->Progress(ProgressData);
  848. }
  849. else if (!ProgressData->IsIndeterminate())
  850. {
  851. OverallProgress = ProgressData->OverallProgress();
  852. }
  853. else
  854. {
  855. OverallProgress = -1;
  856. }
  857. SetTaskbarListProgressValue(OverallProgress);
  858. }
  859. //---------------------------------------------------------------------------
  860. void __fastcall TCustomScpExplorerForm::SetQueueProgress()
  861. {
  862. TTerminalManager::Instance()->QueueStatusUpdated();
  863. if ((FTaskbarList != NULL) && (FProgressForm == NULL))
  864. {
  865. // Do not animate task bar button, when items are queued, but processing is disabled
  866. if ((FQueueStatus != NULL) && (FQueueStatus->ActivePrimaryCount > 0))
  867. {
  868. if (FQueueStatus->IsOnlyOneActiveAndNoPending())
  869. {
  870. TFileOperationProgressType * ProgressData = NULL;
  871. if (FQueueStatus->Items[FQueueStatus->DoneCount] != NULL)
  872. {
  873. ProgressData = FQueueStatus->Items[FQueueStatus->DoneCount]->ProgressData;
  874. }
  875. if ((ProgressData != NULL) &&
  876. ProgressData->InProgress)
  877. {
  878. SetTaskbarListProgressValue(ProgressData);
  879. }
  880. else
  881. {
  882. SetTaskbarListProgressState(TBPF_NOPROGRESS);
  883. }
  884. }
  885. else
  886. {
  887. SetTaskbarListProgressState(TBPF_INDETERMINATE);
  888. }
  889. }
  890. else
  891. {
  892. SetTaskbarListProgressState(TBPF_NOPROGRESS);
  893. }
  894. }
  895. UpdateQueueLabel();
  896. }
  897. //---------------------------------------------------------------------------
  898. void __fastcall TCustomScpExplorerForm::UpdateQueueLabel()
  899. {
  900. UnicodeString Caption = LoadStr(QUEUE_CAPTION);
  901. if (FQueueStatus != NULL)
  902. {
  903. int ActiveAndPendingPrimaryCount = FQueueStatus->ActiveAndPendingPrimaryCount;
  904. if (ActiveAndPendingPrimaryCount > 0)
  905. {
  906. Caption = FORMAT("%s (%d)", (Caption, ActiveAndPendingPrimaryCount));
  907. }
  908. }
  909. QueueLabel->Caption = Caption;
  910. }
  911. //---------------------------------------------------------------------------
  912. void TCustomScpExplorerForm::CheckStoreTransition()
  913. {
  914. if (WinConfiguration->StoreTransition == stInit)
  915. {
  916. if (IsUWP())
  917. {
  918. WinConfiguration->StoreTransition = stStoreFresh;
  919. AppLog(L"Fresh UWP/store installation");
  920. }
  921. else
  922. {
  923. WinConfiguration->StoreTransition = stStandard;
  924. AppLog(L"Fresh standard installation");
  925. }
  926. }
  927. else if ((WinConfiguration->StoreTransition == stStandard) && IsUWP())
  928. {
  929. WinConfiguration->StoreTransition = stStoreMigrated;
  930. WinConfiguration->FirstRun = WinConfiguration->FirstRun + L"=>" + StandardDatestamp();
  931. AppLog(L"Standard installation migrated to store installation");
  932. }
  933. if (WinConfiguration->StoreTransition == stStoreMigrated)
  934. {
  935. TTBXToolbar * Toolbar = new TTBXToolbar(MessageDock);
  936. Toolbar->Parent = MessageDock;
  937. Toolbar->FullSize = true;
  938. Toolbar->Color = Application->HintColor;
  939. TTBXLabelItem * LabelItem;
  940. LabelItem = new TTBXLabelItem(Toolbar);
  941. LabelItem->Margin = ScaleByTextHeight(MessageDock, 2); // Is this automatically rescaled?
  942. LabelItem->Caption = LoadStr(STORE_MIGRATION_THANKS);
  943. Toolbar->Items->Add(LabelItem);
  944. TTBXCustomItem * LinkItem = new TTBXItem(Toolbar);
  945. LinkItem->FontSettings->Color = LinkColor;
  946. LinkItem->Caption = LoadStr(STORE_MIGRATION_LEARN);
  947. LinkItem->OnClick = StoreTransitionLinkClick;
  948. Toolbar->Items->Add(LinkItem);
  949. AddMenuSeparator(Toolbar->Items);
  950. TTBCustomItem * CloseItem = new TTBXItem(Toolbar);
  951. CloseItem->Caption = LoadStr(MESSAGE_DISMISS);
  952. CloseItem->OnClick = StoreTransitionCloseClick;
  953. Toolbar->Items->Add(CloseItem);
  954. GiveTBItemPriority(CloseItem);
  955. }
  956. }
  957. //---------------------------------------------------------------------------
  958. void __fastcall TCustomScpExplorerForm::StoreTransitionLinkClick(TObject *)
  959. {
  960. ShowHelp(HELP_STORE_TRANSITION);
  961. }
  962. //---------------------------------------------------------------------------
  963. void __fastcall TCustomScpExplorerForm::StoreTransitionCloseClick(TObject * Sender)
  964. {
  965. TTBCustomItem * Item = DebugNotNull(dynamic_cast<TTBCustomItem *>(Sender));
  966. TTBXToolbar * Toolbar = DebugNotNull(dynamic_cast<TTBXToolbar *>(DebugNotNull(DebugNotNull(Item->Parent)->ParentComponent)));
  967. delete Toolbar;
  968. WinConfiguration->StoreTransition = stStoreAcknowledged;
  969. UpdateControls();
  970. }
  971. //---------------------------------------------------------------------------
  972. void __fastcall TCustomScpExplorerForm::UpdateTransferList()
  973. {
  974. TTBXStringList * TransferList = dynamic_cast<TTBXStringList*>(
  975. static_cast<TComponent*>(GetComponent(fcTransferList)));
  976. TTBXDropDownItem * TransferDropDown = dynamic_cast<TTBXDropDownItem*>(
  977. static_cast<TComponent*>(GetComponent(fcTransferDropDown)));
  978. TransferList->Strings->BeginUpdate();
  979. try
  980. {
  981. TransferList->Strings->Assign(GUIConfiguration->CopyParamList->NameList);
  982. TransferList->Strings->Insert(0, StripHotkey(LoadStr(COPY_PARAM_DEFAULT)));
  983. TransferList->ItemIndex = GUIConfiguration->CopyParamIndex + 1;
  984. if (FTransferDropDownHint.IsEmpty())
  985. {
  986. FTransferDropDownHint = TransferDropDown->Hint;
  987. }
  988. // this way we get name for "default" settings (COPY_PARAM_DEFAULT)
  989. UnicodeString Name = TransferList->Strings->Strings[TransferList->ItemIndex];
  990. TransferDropDown->Text = StripHotkey(Name);
  991. TransferDropDown->Hint = FORMAT(L"%s|%s:\n%s",
  992. (FTransferDropDownHint, StripHotkey(Name),
  993. GUIConfiguration->CurrentCopyParam.GetInfoStr(L"; ",
  994. FLAGMASK(HasActiveTerminal(), Terminal->UsableCopyParamAttrs(0).General))));
  995. // update the label, otherwise when it is updated only on the first draw
  996. // of the list, it is drawn "bold" for some reason
  997. FTransferListHoverIndex = TransferList->ItemIndex;
  998. UpdateTransferLabel();
  999. }
  1000. __finally
  1001. {
  1002. TransferList->Strings->EndUpdate();
  1003. }
  1004. }
  1005. //---------------------------------------------------------------------------
  1006. void __fastcall TCustomScpExplorerForm::UpdateCustomCommandsToolbar()
  1007. {
  1008. TTBXToolbar * Toolbar = dynamic_cast<TTBXToolbar *>(GetComponent(fcCustomCommandsBand));
  1009. DebugAssert(Toolbar != NULL);
  1010. NonVisualDataModule->UpdateCustomCommandsToolbar(Toolbar);
  1011. }
  1012. //---------------------------------------------------------------------------
  1013. void __fastcall TCustomScpExplorerForm::UpdateActions()
  1014. {
  1015. TForm::UpdateActions();
  1016. if (ComponentVisible[fcCustomCommandsBand])
  1017. {
  1018. UpdateCustomCommandsToolbar();
  1019. }
  1020. }
  1021. //---------------------------------------------------------------------------
  1022. void __fastcall TCustomScpExplorerForm::UpdateSessionsPageControlHeight()
  1023. {
  1024. SessionsPageControl->Height = SessionsPageControl->GetTabsHeight();
  1025. }
  1026. //---------------------------------------------------------------------------
  1027. void __fastcall TCustomScpExplorerForm::UpdateRowSelect(TCustomDirView * DirView)
  1028. {
  1029. if (DirView->RowSelect != WinConfiguration->FullRowSelect)
  1030. {
  1031. DirView->RowSelect = WinConfiguration->FullRowSelect;
  1032. // Without this, the panel turns gray and unused part of header turns black
  1033. DirView->Perform(CM_COLORCHANGED, 0, 0);
  1034. }
  1035. }
  1036. //---------------------------------------------------------------------------
  1037. void __fastcall TCustomScpExplorerForm::ConfigurationChanged()
  1038. {
  1039. Color = GetBtnFaceColor();
  1040. DebugAssert(Configuration && RemoteDirView);
  1041. RemoteDirView->DDAllowMove = !WinConfiguration->DDDisableMove;
  1042. RemoteDirView->DimmHiddenFiles = WinConfiguration->DimmHiddenFiles;
  1043. RemoteDirView->ShowHiddenFiles = WinConfiguration->ShowHiddenFiles;
  1044. RemoteDirView->FormatSizeBytes = WinConfiguration->FormatSizeBytes;
  1045. RemoteDirView->ShowInaccesibleDirectories = WinConfiguration->ShowInaccesibleDirectories;
  1046. RemoteDirView->NaturalOrderNumericalSorting = WinConfiguration->NaturalOrderNumericalSorting;
  1047. RemoteDirView->AlwaysSortDirectoriesByName = WinConfiguration->AlwaysSortDirectoriesByName;
  1048. UpdateRowSelect(RemoteDirView);
  1049. RemoteDriveView->DDAllowMove = !WinConfiguration->DDDisableMove;
  1050. RemoteDriveView->DimmHiddenDirs = WinConfiguration->DimmHiddenFiles;
  1051. RemoteDriveView->ShowHiddenDirs = WinConfiguration->ShowHiddenFiles;
  1052. RemoteDriveView->ShowInaccesibleDirectories = WinConfiguration->ShowInaccesibleDirectories;
  1053. RemoteDriveView->NaturalOrderNumericalSorting = WinConfiguration->NaturalOrderNumericalSorting;
  1054. UpdateSessionsPageControlHeight();
  1055. SetDockAllowDrag(!WinConfiguration->LockToolbars);
  1056. UpdateToolbarDisplayMode();
  1057. UpdateTransferList();
  1058. if (Terminal != NULL)
  1059. {
  1060. TransferPresetAutoSelect();
  1061. }
  1062. UpdateQueueView();
  1063. UpdateControls();
  1064. // this can be called even before constuctor finishes.
  1065. if (FEditorManager != NULL)
  1066. {
  1067. FEditorManager->ProcessFiles(FileConfigurationChanged, NULL);
  1068. }
  1069. // this can be called even before constuctor finishes.
  1070. if (FLocalEditors != NULL)
  1071. {
  1072. for (int Index = 0; Index < FLocalEditors->Count; Index++)
  1073. {
  1074. ReconfigureEditorForm(static_cast<TForm *>(FLocalEditors->Items[Index]));
  1075. }
  1076. }
  1077. if (ComponentVisible[fcCustomCommandsBand])
  1078. {
  1079. UpdateCustomCommandsToolbar();
  1080. }
  1081. // show only when keeping queue items forever,
  1082. // otherwise, it's enough to have it in the context menu
  1083. QueueDeleteAllDoneQueueToolbarItem->Visible =
  1084. WinConfiguration->QueueKeepDoneItems && (WinConfiguration->QueueKeepDoneItemsFor < 0);
  1085. if (FFileColorsCurrent != WinConfiguration->FileColors)
  1086. {
  1087. TFileColorData::LoadList(WinConfiguration->FileColors, FFileColors);
  1088. FFileColorsCurrent = WinConfiguration->FileColors;
  1089. FileColorsChanged();
  1090. }
  1091. }
  1092. //---------------------------------------------------------------------------
  1093. void __fastcall TCustomScpExplorerForm::DoFileColorsChanged(TCustomDirView * DirView)
  1094. {
  1095. DirView->OnGetItemColor = FFileColors.empty() ? TDirViewGetItemColorEvent(NULL) : TDirViewGetItemColorEvent(&DirViewGetItemColor);
  1096. DirView->Invalidate();
  1097. }
  1098. //---------------------------------------------------------------------------
  1099. void __fastcall TCustomScpExplorerForm::FileColorsChanged()
  1100. {
  1101. DoFileColorsChanged(RemoteDirView);
  1102. }
  1103. //---------------------------------------------------------------------------
  1104. void __fastcall TCustomScpExplorerForm::FileConfigurationChanged(
  1105. const UnicodeString FileName, TEditedFileData * /*Data*/, TObject * Token,
  1106. void * /*Arg*/)
  1107. {
  1108. if (Token != NULL)
  1109. {
  1110. TForm * Editor = dynamic_cast<TForm*>(Token);
  1111. DebugAssert(Editor != NULL);
  1112. ReconfigureEditorForm(Editor);
  1113. }
  1114. }
  1115. //---------------------------------------------------------------------------
  1116. void __fastcall TCustomScpExplorerForm::EnableDDTransferConfirmation(TObject * /*Sender*/)
  1117. {
  1118. WinConfiguration->DDTransferConfirmation = asOn;
  1119. }
  1120. //---------------------------------------------------------------------------
  1121. void __fastcall TCustomScpExplorerForm::CopyParamDialogAfter(
  1122. TTransferDirection /*Direction*/, bool /*Temp*/, const UnicodeString & /*TargetDirectory*/)
  1123. {
  1124. // noop
  1125. }
  1126. //---------------------------------------------------------------------------
  1127. bool TCustomScpExplorerForm::GetDoNotShowCopyDialogDefault(bool DragDrop)
  1128. {
  1129. return DragDrop && (WinConfiguration->DDTransferConfirmation == asAuto);
  1130. }
  1131. //---------------------------------------------------------------------------
  1132. void TCustomScpExplorerForm::HandleDoNotShowCopyDialogAgain(bool DragDrop, bool DoNotShowAgain)
  1133. {
  1134. if (DoNotShowAgain)
  1135. {
  1136. if (DragDrop)
  1137. {
  1138. if (WinConfiguration->DDTransferConfirmation == asAuto)
  1139. {
  1140. PopupTrayBalloon(
  1141. NULL, LoadStr(DD_TRANSFER_CONFIRM_OFF2), qtInformation, NULL, 0, EnableDDTransferConfirmation, NULL);
  1142. }
  1143. WinConfiguration->DDTransferConfirmation = asOff;
  1144. }
  1145. else
  1146. {
  1147. WinConfiguration->ConfirmTransferring = false;
  1148. }
  1149. }
  1150. else
  1151. {
  1152. // User explicitly unchecked "do not show again",
  1153. // so show him the dialog the next time
  1154. if (DragDrop && (WinConfiguration->DDTransferConfirmation == asAuto))
  1155. {
  1156. WinConfiguration->DDTransferConfirmation = asOn;
  1157. }
  1158. }
  1159. }
  1160. //---------------------------------------------------------------------------
  1161. bool __fastcall TCustomScpExplorerForm::CopyParamDialog(
  1162. TTransferDirection Direction, TTransferType Type, bool Temp,
  1163. TStrings * FileList, UnicodeString & TargetDirectory, TGUICopyParamType & CopyParam,
  1164. bool Confirm, bool DragDrop, int Options)
  1165. {
  1166. bool Result = true;
  1167. DebugAssert(HasActiveTerminal());
  1168. // these parameters are known in advance
  1169. int Params =
  1170. FLAGMASK(Type == ttMove, cpDelete);
  1171. bool ToTemp = (Temp && (Direction == tdToLocal));
  1172. if (Result && Confirm && WinConfiguration->ConfirmTransferring)
  1173. {
  1174. bool DisableNewerOnly =
  1175. (!Terminal->IsCapable[fcNewerOnlyUpload] && (Direction == tdToRemote)) ||
  1176. ToTemp;
  1177. Options |=
  1178. FLAGMASK(ToTemp, coTemp) |
  1179. FLAGMASK(!Terminal->IsCapable[fcBackgroundTransfers], coDisableQueue) |
  1180. coDoNotShowAgain;
  1181. TUsableCopyParamAttrs UsableCopyParamAttrs = Terminal->UsableCopyParamAttrs(Params);
  1182. int CopyParamAttrs = (Direction == tdToRemote ?
  1183. UsableCopyParamAttrs.Upload : UsableCopyParamAttrs.Download) |
  1184. FLAGMASK(DisableNewerOnly, cpaNoNewerOnly);
  1185. int OutputOptions = FLAGMASK(GetDoNotShowCopyDialogDefault(DragDrop), cooDoNotShowAgain);
  1186. std::unique_ptr<TSessionData> SessionData(SessionDataForCode());
  1187. FlashOnBackground(); // Particularly when called from ClipboardFakeCreated
  1188. Result = DoCopyDialog(Direction == tdToRemote, Type == ttMove,
  1189. FileList, TargetDirectory, &CopyParam, Options, CopyParamAttrs, SessionData.get(), &OutputOptions, -1);
  1190. if (Result)
  1191. {
  1192. HandleDoNotShowCopyDialogAgain(DragDrop, FLAGSET(OutputOptions, cooDoNotShowAgain));
  1193. CopyParamDialogAfter(Direction, Temp, TargetDirectory);
  1194. }
  1195. }
  1196. if (Result && CopyParam.Queue && !ToTemp && Terminal->IsCapable[fcBackgroundTransfers])
  1197. {
  1198. Configuration->Usage->Inc(L"TransfersOnBackground");
  1199. // these parameter are known only after transfer dialog
  1200. Params |=
  1201. (CopyParam.QueueNoConfirmation ? cpNoConfirmation : 0);
  1202. AddQueueItem(Queue, Direction, FileList, TargetDirectory, CopyParam, Params);
  1203. Result = false;
  1204. ClearTransferSourceSelection(Direction);
  1205. }
  1206. return Result;
  1207. }
  1208. //---------------------------------------------------------------------------
  1209. void TCustomScpExplorerForm::ClearOperationSelection(TOperationSide Side)
  1210. {
  1211. if (FOnFileOperationFinished != NULL)
  1212. {
  1213. FOnFileOperationFinished(Side, UnicodeString(), true);
  1214. }
  1215. else
  1216. {
  1217. if (HasDirView[Side])
  1218. {
  1219. DirView(Side)->SelectAll(smNone);
  1220. }
  1221. }
  1222. }
  1223. //---------------------------------------------------------------------------
  1224. void __fastcall TCustomScpExplorerForm::ClearTransferSourceSelection(TTransferDirection Direction)
  1225. {
  1226. TOperationSide Side = ((Direction == tdToRemote) ? osLocal : osRemote);
  1227. ClearOperationSelection(Side);
  1228. }
  1229. //---------------------------------------------------------------------------
  1230. void __fastcall TCustomScpExplorerForm::AddQueueItem(
  1231. TTerminalQueue * Queue, TTransferDirection Direction, TStrings * FileList,
  1232. const UnicodeString TargetDirectory, TGUICopyParamType & CopyParam,
  1233. int Params)
  1234. {
  1235. DebugAssert(Queue != NULL);
  1236. bool SingleFile = false;
  1237. if (FileList->Count == 1)
  1238. {
  1239. if (Direction == tdToRemote)
  1240. {
  1241. UnicodeString FileName = FileList->Strings[0];
  1242. SingleFile = FileExists(ApiPath(FileName));
  1243. }
  1244. else
  1245. {
  1246. TRemoteFile * File = static_cast<TRemoteFile *>(FileList->Objects[0]);
  1247. SingleFile = !File->IsDirectory;
  1248. }
  1249. }
  1250. TQueueItem * QueueItem;
  1251. if (Direction == tdToRemote)
  1252. {
  1253. CopyParam.IncludeFileMask.SetRoots(FileList, TargetDirectory);
  1254. QueueItem = new TUploadQueueItem(Terminal, FileList, TargetDirectory,
  1255. &CopyParam, Params, SingleFile, CopyParam.QueueParallel);
  1256. }
  1257. else
  1258. {
  1259. CopyParam.IncludeFileMask.SetRoots(TargetDirectory, FileList);
  1260. QueueItem = new TDownloadQueueItem(Terminal, FileList, TargetDirectory,
  1261. &CopyParam, Params, SingleFile, CopyParam.QueueParallel);
  1262. }
  1263. AddQueueItem(Queue, QueueItem, Terminal);
  1264. }
  1265. //---------------------------------------------------------------------------
  1266. void __fastcall TCustomScpExplorerForm::AddQueueItem(TTerminalQueue * Queue, TQueueItem * QueueItem, TManagedTerminal * Terminal)
  1267. {
  1268. if (Queue->IsEmpty)
  1269. {
  1270. Terminal->QueueOperationStart = Now();
  1271. }
  1272. Queue->AddItem(QueueItem);
  1273. }
  1274. //---------------------------------------------------------------------------
  1275. void __fastcall TCustomScpExplorerForm::RestoreFormParams()
  1276. {
  1277. }
  1278. //---------------------------------------------------------------------------
  1279. void __fastcall TCustomScpExplorerForm::InitControls()
  1280. {
  1281. DebugAssert(FDocks != NULL);
  1282. for (int Index = 0; Index < ComponentCount; Index++)
  1283. {
  1284. TTBDock * Dock = dynamic_cast<TTBDock *>(Components[Index]);
  1285. if ((Dock != NULL) && (Dock->Tag == 0))
  1286. {
  1287. FDocks->Add(Dock);
  1288. }
  1289. }
  1290. CollectItemsWithTextDisplayMode(this);
  1291. }
  1292. //---------------------------------------------------------------------------
  1293. void __fastcall TCustomScpExplorerForm::RestoreParams()
  1294. {
  1295. ConfigurationChanged();
  1296. QueuePanel->Height =
  1297. LoadDimension(WinConfiguration->QueueView.Height, WinConfiguration->QueueView.HeightPixelsPerInch, this);
  1298. LoadListViewStr(QueueView3, WinConfiguration->QueueView.Layout);
  1299. QueueDock->Visible = WinConfiguration->QueueView.ToolBar;
  1300. QueueLabel->Visible = WinConfiguration->QueueView.Label;
  1301. QueueFileList->Visible = WinConfiguration->QueueView.FileList;
  1302. QueueFileList->Height =
  1303. LoadDimension(WinConfiguration->QueueView.FileListHeight, WinConfiguration->QueueView.FileListHeightPixelsPerInch, this);
  1304. if (QueueFileList->Visible)
  1305. {
  1306. AdjustQueueLayout();
  1307. }
  1308. UpdateCustomCommandsToolbar();
  1309. }
  1310. //---------------------------------------------------------------------------
  1311. void __fastcall TCustomScpExplorerForm::StoreParams()
  1312. {
  1313. int APixelsPerInch = GetControlPixelsPerInch(this);
  1314. WinConfiguration->QueueView.Height = QueuePanel->Height;
  1315. WinConfiguration->QueueView.HeightPixelsPerInch = APixelsPerInch;
  1316. WinConfiguration->QueueView.Layout = GetListViewStr(QueueView3);
  1317. WinConfiguration->QueueView.ToolBar = QueueDock->Visible;
  1318. WinConfiguration->QueueView.Label = QueueLabel->Visible;
  1319. WinConfiguration->QueueView.FileList = QueueFileList->Visible;
  1320. WinConfiguration->QueueView.FileListHeight = QueueFileList->Height;
  1321. WinConfiguration->QueueView.FileListHeightPixelsPerInch = APixelsPerInch;
  1322. }
  1323. //---------------------------------------------------------------------------
  1324. void __fastcall TCustomScpExplorerForm::CreateParams(TCreateParams & Params)
  1325. {
  1326. if (!FFormRestored)
  1327. {
  1328. FFormRestored = true;
  1329. RestoreFormParams();
  1330. }
  1331. TForm::CreateParams(Params);
  1332. }
  1333. //---------------------------------------------------------------------------
  1334. void __fastcall TCustomScpExplorerForm::SetDockAllowDrag(bool value)
  1335. {
  1336. DebugAssert(FDocks != NULL);
  1337. for (int Index = 0; Index < FDocks->Count; Index++)
  1338. {
  1339. static_cast<TTBDock*>(FDocks->Items[Index])->AllowDrag = value;
  1340. }
  1341. }
  1342. //---------------------------------------------------------------------------
  1343. void __fastcall TCustomScpExplorerForm::LoadToolbarsLayoutStr(UnicodeString LayoutStr, UnicodeString ButtonsStr)
  1344. {
  1345. SetDockAllowDrag(true);
  1346. while (!ButtonsStr.IsEmpty())
  1347. {
  1348. UnicodeString S = CutToChar(ButtonsStr, L';', true);
  1349. UnicodeString Name = CutToChar(S, L'=', true);
  1350. for (int ToolbarIndex = 0; ToolbarIndex < ComponentCount; ToolbarIndex++)
  1351. {
  1352. TTBCustomToolbar * Toolbar = dynamic_cast<TTBCustomToolbar *>(Components[ToolbarIndex]);
  1353. if ((Toolbar != NULL) && SameText(Name, GetToolbarKey(Toolbar->Name)))
  1354. {
  1355. while (!S.IsEmpty())
  1356. {
  1357. UnicodeString S2 = CutToChar(S, L',', true);
  1358. UnicodeString Name = CutToChar(S2, L':', true);
  1359. for (int ItemIndex = 0; ItemIndex < Toolbar->Items->Count; ItemIndex++)
  1360. {
  1361. TTBCustomItem * Item = Toolbar->Items->Items[ItemIndex];
  1362. if (SameText(GetToolbarItemName(Item), Name))
  1363. {
  1364. // forward compatibility
  1365. UnicodeString S3 = CutToChar(S2, L':', true);
  1366. bool Visible = StrToIntDef(S3, 0);
  1367. Item->Visible = Visible;
  1368. }
  1369. }
  1370. }
  1371. }
  1372. }
  1373. }
  1374. ::LoadToolbarsLayoutStr(this, LayoutStr);
  1375. SetDockAllowDrag(!WinConfiguration->LockToolbars);
  1376. }
  1377. //---------------------------------------------------------------------------
  1378. UnicodeString __fastcall TCustomScpExplorerForm::GetToolbarsLayoutStr()
  1379. {
  1380. UnicodeString Result;
  1381. SetDockAllowDrag(true);
  1382. Result = ::GetToolbarsLayoutStr(this);
  1383. SetDockAllowDrag(!WinConfiguration->LockToolbars);
  1384. return Result;
  1385. }
  1386. //---------------------------------------------------------------------------
  1387. UnicodeString __fastcall TCustomScpExplorerForm::GetToolbarItemName(TTBCustomItem * Item)
  1388. {
  1389. UnicodeString Result;
  1390. if (Item->Action != NULL)
  1391. {
  1392. Result = Item->Action->Name;
  1393. Result = RemoveSuffix(Result, L"Action", true);
  1394. }
  1395. else
  1396. {
  1397. Result = Item->Name;
  1398. Result = RemoveSuffix(Result, L"SubmenuItem", true);
  1399. Result = RemoveSuffix(Result, L"Item", true);
  1400. }
  1401. return Result;
  1402. }
  1403. //---------------------------------------------------------------------------
  1404. UnicodeString __fastcall TCustomScpExplorerForm::GetToolbarsButtonsStr()
  1405. {
  1406. UnicodeString Result;
  1407. for (int ToolbarIndex = 0; ToolbarIndex < ComponentCount; ToolbarIndex++)
  1408. {
  1409. UnicodeString ToolbarStr;
  1410. TTBCustomToolbar * Toolbar = dynamic_cast<TTBCustomToolbar *>(Components[ToolbarIndex]);
  1411. if ((Toolbar != NULL) && (Toolbar != QueueToolbar))
  1412. {
  1413. for (int ItemIndex = 0; ItemIndex < Toolbar->Items->Count; ItemIndex++)
  1414. {
  1415. TTBCustomItem * Item = Toolbar->Items->Items[ItemIndex];
  1416. // Currently all buttons are visible by default, so we can safely remember all hidden buttons.
  1417. // Once we introduce any buttons that are hidden by default, we would have to remember their initial state
  1418. // and save the changes here only.
  1419. if (NonVisualDataModule->IsCustomizableToolbarItem(Item) && !Item->Visible)
  1420. {
  1421. UnicodeString Name = GetToolbarItemName(Item);
  1422. DebugAssert(Name.Pos(L"TBX") == 0);
  1423. AddToList(ToolbarStr, FORMAT(L"%s:0", (Name)), L",");
  1424. }
  1425. }
  1426. }
  1427. if (!ToolbarStr.IsEmpty())
  1428. {
  1429. AddToList(Result, FORMAT(L"%s=%s", (GetToolbarKey(Toolbar->Name), ToolbarStr)), L";");
  1430. }
  1431. }
  1432. return Result;
  1433. }
  1434. //---------------------------------------------------------------------------
  1435. void __fastcall TCustomScpExplorerForm::CreateProgressForm(TSynchronizeProgress * SynchronizeProgress)
  1436. {
  1437. DebugAssert(FProgressForm == NULL);
  1438. bool AllowSkip = HasActiveTerminal() ? Terminal->IsCapable[fcSkipTransfer] : false;
  1439. bool AllowMoveToQueue = (FTransferResumeList != NULL) || (FDeletedFiles != NULL);
  1440. FProgressForm = new TProgressForm(Application, AllowMoveToQueue, AllowSkip, SynchronizeProgress);
  1441. FProgressForm->DeleteLocalToRecycleBin =
  1442. (WinConfiguration->DeleteToRecycleBin != FAlternativeDelete);
  1443. FProgressForm->DeleteRemoteToRecycleBin =
  1444. HasActiveTerminal() &&
  1445. (Terminal->SessionData->DeleteToRecycleBin != FAlternativeDelete) &&
  1446. !Terminal->SessionData->RecycleBinPath.IsEmpty();
  1447. // As progress window has delayed show now, we need to lock ourselves, (at least) until then
  1448. LockWindow();
  1449. // Actually, do not know what hides the progress once the operation finishes
  1450. // (it is possibly the SetQueueProgress - and we should not rely on that)
  1451. SetTaskbarListProgressState(TBPF_NORMAL);
  1452. }
  1453. //---------------------------------------------------------------------------
  1454. void __fastcall TCustomScpExplorerForm::DestroyProgressForm()
  1455. {
  1456. SAFE_DESTROY(FProgressForm);
  1457. UnlockWindow();
  1458. SetQueueProgress();
  1459. }
  1460. //---------------------------------------------------------------------------
  1461. void __fastcall TCustomScpExplorerForm::FileOperationProgress(
  1462. TFileOperationProgressType & ProgressData)
  1463. {
  1464. // Download to temporary local directory
  1465. if (FPendingTempSpaceWarn && ProgressData.InProgress && ProgressData.TotalSizeSet)
  1466. {
  1467. bool Continue = true;
  1468. FPendingTempSpaceWarn = false;
  1469. DoWarnLackOfTempSpace(ProgressData.Directory, ProgressData.TotalSize, Continue);
  1470. if (!Continue)
  1471. {
  1472. Abort();
  1473. }
  1474. }
  1475. // operation is being executed and we still didn't show up progress form
  1476. if (ProgressData.InProgress && !FProgressForm)
  1477. {
  1478. CreateProgressForm(NULL);
  1479. DebugAssert(FProgressForm->OnceDoneOperation == odoIdle);
  1480. FProgressForm->OnceDoneOperation = ProgressData.InitialOnceDoneOperation;
  1481. }
  1482. // operation is finished (or terminated), so we hide progress form
  1483. else if (!ProgressData.InProgress && (FProgressForm != NULL) && (FProgressForm->SynchronizeProgress == NULL))
  1484. {
  1485. DestroyProgressForm();
  1486. if (ProgressData.Operation == foCalculateSize)
  1487. {
  1488. // When calculating size before transfer, the abort caused by
  1489. // cancel flag set due to "MoveToQueue" below,
  1490. // is not propagated back to us in ExecuteFileOperation,
  1491. // as it is silently swallowed.
  1492. // So we explicitly (re)throw it here.
  1493. if (FMoveToQueue)
  1494. {
  1495. Abort();
  1496. }
  1497. }
  1498. else
  1499. {
  1500. if ((ProgressData.Cancel == csContinue) &&
  1501. !FAutoOperation)
  1502. {
  1503. OperationComplete(ProgressData.StartTime);
  1504. }
  1505. }
  1506. }
  1507. if (FProgressForm &&
  1508. // Can happen only during synchronization, when ending one of the operations
  1509. ProgressData.InProgress)
  1510. {
  1511. FProgressForm->SetProgressData(ProgressData);
  1512. DebugAssert(ProgressData.InProgress);
  1513. SetTaskbarListProgressValue(&ProgressData);
  1514. if (FProgressForm->Cancel > csContinue)
  1515. {
  1516. ProgressData.SetCancelAtLeast(FProgressForm->Cancel);
  1517. if (FProgressForm->Cancel == csCancelFile)
  1518. {
  1519. FProgressForm->ClearCancel();
  1520. }
  1521. // cancel cancels even the move
  1522. FMoveToQueue = false;
  1523. }
  1524. else if (FProgressForm->MoveToQueue)
  1525. {
  1526. FMoveToQueue = true;
  1527. ProgressData.SetCancelAtLeast(csCancel);
  1528. }
  1529. if ((FTransferResumeList != NULL) &&
  1530. ProgressData.InProgress &&
  1531. ProgressData.IsTransfer() &&
  1532. !ProgressData.FullFileName.IsEmpty())
  1533. {
  1534. if ((FTransferResumeList->Count == 0) ||
  1535. (FTransferResumeList->Strings[FTransferResumeList->Count - 1] != ProgressData.FullFileName))
  1536. {
  1537. // note that we do not recognize directories from files here
  1538. FTransferResumeList->Add(ProgressData.FullFileName);
  1539. }
  1540. }
  1541. }
  1542. }
  1543. //---------------------------------------------------------------------------
  1544. void __fastcall TCustomScpExplorerForm::OperationComplete(
  1545. const TDateTime & StartTime)
  1546. {
  1547. if (GUIConfiguration->BeepOnFinish &&
  1548. (Now() - StartTime > GUIConfiguration->BeepOnFinishAfter))
  1549. {
  1550. UnicodeString BeepSound = GUIConfiguration->BeepSound;
  1551. DWORD Sound;
  1552. if (!ExtractFileExt(BeepSound).IsEmpty())
  1553. {
  1554. Sound = SND_FILENAME;
  1555. }
  1556. else
  1557. {
  1558. Sound = SND_ALIAS;
  1559. }
  1560. PlaySound(BeepSound.c_str(), NULL, Sound | SND_ASYNC);
  1561. }
  1562. }
  1563. //---------------------------------------------------------------------------
  1564. void __fastcall TCustomScpExplorerForm::OperationProgress(
  1565. TFileOperationProgressType & ProgressData)
  1566. {
  1567. // For example, when calling TFileOperationProgressType::Suspend in TTerminal::QueryReopen,
  1568. // we cannot recurse back to TTerminal::Idle as that may detect another error, calling "suspend" again.
  1569. TAutoNestingCounter Counter(FDoNotIdleCurrentTerminal);
  1570. FileOperationProgress(ProgressData);
  1571. }
  1572. //---------------------------------------------------------------------------
  1573. UnicodeString __fastcall TCustomScpExplorerForm::GetProgressTitle()
  1574. {
  1575. UnicodeString Result;
  1576. if (FProgressForm != NULL)
  1577. {
  1578. Result = FProgressForm->ProgressStr();
  1579. }
  1580. return Result;
  1581. }
  1582. //---------------------------------------------------------------------------
  1583. bool __fastcall TCustomScpExplorerForm::PanelOperation(TOperationSide /*Side*/,
  1584. bool DragDrop)
  1585. {
  1586. return (!DragDrop && (DropSourceControl == NULL)) ||
  1587. (DropSourceControl == DirView(osOther));
  1588. }
  1589. //---------------------------------------------------------------------------
  1590. TListItem * TCustomScpExplorerForm::VisualiseOperationFinished(TOperationSide Side, const UnicodeString & FileName, bool Unselect)
  1591. {
  1592. TCustomDirView * ADirView = DirView(Side);
  1593. UnicodeString FileNameOnly = ExtractFileName(FileName, !IsSideLocalBrowser(Side));
  1594. TListItem * Item = ADirView->FindFileItem(FileNameOnly);
  1595. // this can happen when local drive is unplugged in the middle of the operation
  1596. if (Item != NULL)
  1597. {
  1598. if (Unselect)
  1599. {
  1600. Item->Selected = false;
  1601. }
  1602. ADirView->MakeProgressVisible(Item);
  1603. }
  1604. return Item;
  1605. }
  1606. //---------------------------------------------------------------------------
  1607. void __fastcall TCustomScpExplorerForm::DoOperationFinished(
  1608. TFileOperation Operation, TOperationSide Side,
  1609. bool /*Temp*/, const UnicodeString & FileName, bool Success,
  1610. TOnceDoneOperation & OnceDoneOperation)
  1611. {
  1612. if (!FAutoOperation)
  1613. {
  1614. // no selection on "/upload", form serves only as event handler
  1615. // (it is not displayed)
  1616. if (PanelOperation(Side, FDragDropOperation) && Visible &&
  1617. ((Operation == FPrimaryOperation) ||
  1618. ((Operation != foCalculateSize) && (Operation != foGetProperties) && (Operation != foCalculateChecksum))))
  1619. {
  1620. TOperationSide DViewSide;
  1621. if ((FProgressSide != osCurrent) &&
  1622. IsLocalBrowserMode() && (Side == osLocal)) // Only to limit the impact
  1623. {
  1624. // assume the operation is over the focused panel
  1625. DViewSide = FProgressSide;
  1626. }
  1627. else
  1628. {
  1629. DViewSide = Side;
  1630. }
  1631. if (FOnFileOperationFinished != NULL)
  1632. {
  1633. FOnFileOperationFinished(DViewSide, FileName, Success);
  1634. }
  1635. else
  1636. {
  1637. VisualiseOperationFinished(DViewSide, FileName, Success);
  1638. }
  1639. }
  1640. if (TFileOperationProgressType::IsTransferOperation(Operation))
  1641. {
  1642. if (DebugAlwaysTrue(!IsLocalBrowserMode()))
  1643. {
  1644. if (Side == osLocal)
  1645. {
  1646. Configuration->Usage->Inc(L"UploadedFiles");
  1647. }
  1648. else
  1649. {
  1650. Configuration->Usage->Inc(L"DownloadedFiles");
  1651. }
  1652. }
  1653. }
  1654. }
  1655. if (Success && (FSynchronizeController != NULL))
  1656. {
  1657. DebugAssert(!IsSideLocalBrowser(osRemote));
  1658. if (Operation == foCopy)
  1659. {
  1660. DebugAssert(Side == osLocal);
  1661. FSynchronizeController->LogOperation(soUpload, FileName);
  1662. }
  1663. else if (Operation == foDelete)
  1664. {
  1665. DebugAssert(Side == osRemote);
  1666. FSynchronizeController->LogOperation(soDelete, FileName);
  1667. }
  1668. }
  1669. if (FProgressForm)
  1670. {
  1671. OnceDoneOperation = FProgressForm->OnceDoneOperation;
  1672. }
  1673. }
  1674. //---------------------------------------------------------------------------
  1675. void __fastcall TCustomScpExplorerForm::OperationFinished(
  1676. TFileOperation Operation, TOperationSide Side,
  1677. bool Temp, const UnicodeString & FileName, Boolean Success,
  1678. TOnceDoneOperation & OnceDoneOperation)
  1679. {
  1680. TAutoNestingCounter Counter(FDoNotIdleCurrentTerminal);
  1681. DoOperationFinished(Operation, Side, Temp, FileName, Success,
  1682. OnceDoneOperation);
  1683. }
  1684. //---------------------------------------------------------------------------
  1685. bool TCustomScpExplorerForm::IsLocalBrowserMode()
  1686. {
  1687. return false;
  1688. }
  1689. //---------------------------------------------------------------------------
  1690. bool TCustomScpExplorerForm::SupportsLocalBrowser()
  1691. {
  1692. return false;
  1693. }
  1694. //---------------------------------------------------------------------------
  1695. bool TCustomScpExplorerForm::IsSideLocalBrowser(TOperationSide)
  1696. {
  1697. return false;
  1698. }
  1699. //---------------------------------------------------------------------------
  1700. TCustomDirView * TCustomScpExplorerForm::GetCurrentLocalBrowser()
  1701. {
  1702. return NULL;
  1703. }
  1704. //---------------------------------------------------------------------------
  1705. TCustomDirView * __fastcall TCustomScpExplorerForm::DirView(TOperationSide Side)
  1706. {
  1707. DebugAssert(GetSide(Side) == osRemote);
  1708. DebugUsedParam(Side);
  1709. return RemoteDirView;
  1710. }
  1711. //---------------------------------------------------------------------------
  1712. TCustomDriveView * __fastcall TCustomScpExplorerForm::DriveView(TOperationSide Side)
  1713. {
  1714. DebugAssert(GetSide(Side) == osRemote);
  1715. DebugUsedParam(Side);
  1716. return RemoteDriveView;
  1717. }
  1718. //---------------------------------------------------------------------------
  1719. bool __fastcall TCustomScpExplorerForm::DirViewEnabled(TOperationSide Side)
  1720. {
  1721. DebugAssert(GetSide(Side) == osRemote);
  1722. DebugUsedParam(Side);
  1723. return HasActiveTerminal();
  1724. }
  1725. //---------------------------------------------------------------------------
  1726. bool __fastcall TCustomScpExplorerForm::GetEnableFocusedOperation(
  1727. TOperationSide Side, int FilesOnly)
  1728. {
  1729. return DirView(Side)->AnyFileSelected(true, (FilesOnly != 0), true);
  1730. }
  1731. //---------------------------------------------------------------------------
  1732. bool __fastcall TCustomScpExplorerForm::GetEnableSelectedOperation(
  1733. TOperationSide Side, int FilesOnly)
  1734. {
  1735. return DirView(Side)->AnyFileSelected(false, (FilesOnly != 0), true);
  1736. }
  1737. //---------------------------------------------------------------------------
  1738. struct THistoryItemData
  1739. {
  1740. short int Side;
  1741. short int Index;
  1742. };
  1743. //---------------------------------------------------------------------------
  1744. void __fastcall TCustomScpExplorerForm::HistoryGo(TOperationSide Side, int Index)
  1745. {
  1746. DirView(Side)->HistoryGo(Index);
  1747. }
  1748. //---------------------------------------------------------------------------
  1749. void __fastcall TCustomScpExplorerForm::HistoryItemClick(System::TObject* Sender)
  1750. {
  1751. TTBCustomItem * Item = dynamic_cast<TTBCustomItem *>(Sender);
  1752. THistoryItemData Data = *reinterpret_cast<THistoryItemData*>(&(Item->Tag));
  1753. HistoryGo((TOperationSide)Data.Side, Data.Index);
  1754. }
  1755. //---------------------------------------------------------------------------
  1756. void __fastcall TCustomScpExplorerForm::UpdateHistoryMenu(TOperationSide Side,
  1757. bool Back)
  1758. {
  1759. if (FHistoryMenu[Side == osLocal][Back] != NULL)
  1760. {
  1761. TCustomDirView * DView = DirView(Side);
  1762. TTBXPopupMenu * Menu = FHistoryMenu[Side == osLocal][Back];
  1763. int ICount = Back ? DView->BackCount : DView->ForwardCount;
  1764. if (ICount > 10)
  1765. {
  1766. ICount = 10;
  1767. }
  1768. Menu->Items->Clear();
  1769. THistoryItemData Data;
  1770. Data.Side = Side;
  1771. for (short int i = 1; i <= ICount; i++)
  1772. {
  1773. TTBCustomItem * Item = new TTBXItem(Menu);
  1774. Data.Index = static_cast<short int>(i * (Back ? -1 : 1));
  1775. Item->Caption = MinimizeName(DView->HistoryPath[Data.Index], 50, !IsSideLocalBrowser(Side));
  1776. Item->Hint = DView->HistoryPath[Data.Index];
  1777. DebugAssert(sizeof(int) == sizeof(THistoryItemData));
  1778. Item->Tag = *reinterpret_cast<int*>(&Data);
  1779. Item->OnClick = HistoryItemClick;
  1780. Menu->Items->Add(Item);
  1781. }
  1782. }
  1783. }
  1784. //---------------------------------------------------------------------------
  1785. TTBXPopupMenu * __fastcall TCustomScpExplorerForm::HistoryMenu(
  1786. TOperationSide Side, bool Back)
  1787. {
  1788. if (FHistoryMenu[Side == osLocal][Back] == NULL)
  1789. {
  1790. // workaround
  1791. // In Pascal the size of TTBXPopupMenu is 132, in C++ 136,
  1792. // operator new allocates memory in Pascal code, but calls inline
  1793. // contructor in C++, leading in problems, the function does
  1794. // both in Pascal code
  1795. FHistoryMenu[Side == osLocal][Back] = CreateTBXPopupMenu(this);
  1796. UpdateHistoryMenu(Side, Back);
  1797. }
  1798. return FHistoryMenu[Side == osLocal][Back];
  1799. }
  1800. //---------------------------------------------------------------------------
  1801. void __fastcall TCustomScpExplorerForm::DirViewHistoryChange(
  1802. TCustomDirView *Sender)
  1803. {
  1804. TOperationSide Side = (Sender == DirView(osOther) ? osOther : osLocal);
  1805. UpdateHistoryMenu(Side, true);
  1806. UpdateHistoryMenu(Side, false);
  1807. }
  1808. //---------------------------------------------------------------------------
  1809. bool __fastcall TCustomScpExplorerForm::CustomCommandRemoteAllowed()
  1810. {
  1811. // remote custom commands can be executed only if the server supports shell commands
  1812. // or have secondary shell
  1813. return HasActiveTerminal() && (Terminal->IsCapable[fcSecondaryShell] || Terminal->IsCapable[fcShellAnyCommand]);
  1814. }
  1815. //---------------------------------------------------------------------------
  1816. int __fastcall TCustomScpExplorerForm::CustomCommandState(
  1817. const TCustomCommandType & Command, bool OnFocused, TCustomCommandListType ListType)
  1818. {
  1819. // -1 = hidden, 0 = disabled, 1 = enabled
  1820. int Result;
  1821. TFileCustomCommand RemoteCustomCommand;
  1822. TLocalCustomCommand LocalCustomCommand;
  1823. TFileCustomCommand * NonInteractiveCustomCommand =
  1824. FLAGCLEAR(Command.Params, ccLocal) ? &RemoteCustomCommand : &LocalCustomCommand;
  1825. TInteractiveCustomCommand InteractiveCustomCommand(NonInteractiveCustomCommand);
  1826. UnicodeString Cmd = InteractiveCustomCommand.Complete(Command.Command, false);
  1827. if (FLAGCLEAR(Command.Params, ccLocal))
  1828. {
  1829. int AllowedState = CustomCommandRemoteAllowed() ? 1 : 0;
  1830. // custom command that does not operate with files can be executed anytime ...
  1831. if (!NonInteractiveCustomCommand->IsFileCommand(Cmd))
  1832. {
  1833. // ... but do not show such command in remote file menu
  1834. if ((ListType == ccltAll) || (ListType == ccltNonFile))
  1835. {
  1836. Result = AllowedState;
  1837. }
  1838. else
  1839. {
  1840. Result = -1;
  1841. }
  1842. }
  1843. else
  1844. {
  1845. if ((ListType == ccltAll) || ((ListType == ccltFile) && !IsSideLocalBrowser(FCurrentSide)))
  1846. {
  1847. Result = (DirView(FCurrentSide)->AnyFileSelected(OnFocused, false, true)) ? AllowedState : 0;
  1848. }
  1849. else
  1850. {
  1851. Result = -1;
  1852. }
  1853. }
  1854. }
  1855. else
  1856. {
  1857. // custom command that does not operate with files can be executed anytime
  1858. if (!NonInteractiveCustomCommand->IsFileCommand(Cmd))
  1859. {
  1860. Result = ((ListType == ccltAll) || (ListType == ccltNonFile)) ? 1 : -1;
  1861. }
  1862. else if (LocalCustomCommand.HasLocalFileName(Cmd))
  1863. {
  1864. if ((ListType == ccltAll) || (ListType == ccltFile))
  1865. {
  1866. // special case is "diff"-style command that can be executed over any side,
  1867. // if we have both sides
  1868. Result =
  1869. // Cannot have focus on both panels, so we have to call AnyFileSelected
  1870. // directly (instead of EnableSelectedOperation) to pass
  1871. // false to FocusedFileOnlyWhenFocused when panel is inactive.
  1872. ((HasDirView[osLocal] && !IsSideLocalBrowser(osRemote) &&
  1873. DirView(osLocal)->AnyFileSelected(false, false, (FCurrentSide == osLocal))) &&
  1874. DirView(osRemote)->AnyFileSelected(false, false, (FCurrentSide == osRemote))) ? 1 : 0;
  1875. }
  1876. else if (ListType == ccltBoth)
  1877. {
  1878. DebugAssert(!IsLocalBrowserMode());
  1879. Result = 1;
  1880. }
  1881. else
  1882. {
  1883. Result = -1;
  1884. }
  1885. }
  1886. else
  1887. {
  1888. if ((ListType == ccltAll) ||
  1889. ((ListType == ccltFile) && (!IsSideLocalBrowser(FCurrentSide) || FLAGCLEAR(Command.Params, ccRemoteFiles))))
  1890. {
  1891. Result = DirView(FCurrentSide)->AnyFileSelected(OnFocused, false, true) ? 1 : 0;
  1892. }
  1893. else
  1894. {
  1895. Result = -1;
  1896. }
  1897. }
  1898. if ((Result > 0) &&
  1899. LocalCustomCommand.IsSessionCommand(Cmd) &&
  1900. !HasActiveTerminal())
  1901. {
  1902. Result = 0;
  1903. }
  1904. }
  1905. return Result;
  1906. }
  1907. //---------------------------------------------------------------------------
  1908. void __fastcall TCustomScpExplorerForm::RemoteCustomCommand(
  1909. TStrings * FileList, const TCustomCommandType & ACommand,
  1910. const TCustomCommandData & Data, const UnicodeString & CommandCommand)
  1911. {
  1912. if (EnsureCommandSessionFallback(fcShellAnyCommand))
  1913. {
  1914. TRemoteCustomCommand RemoteCustomCommand(Data, Terminal->CurrentDirectory);
  1915. TWinInteractiveCustomCommand InteractiveCustomCommand(
  1916. &RemoteCustomCommand, ACommand.Name, ACommand.HomePage);
  1917. UnicodeString Command = InteractiveCustomCommand.Complete(CommandCommand, false);
  1918. Configuration->Usage->Inc(L"RemoteCustomCommandRuns2");
  1919. bool Capture =
  1920. FLAGSET(ACommand.Params, ccShowResults) ||
  1921. FLAGSET(ACommand.Params, ccCopyResults) ||
  1922. FLAGSET(ACommand.Params, ccShowResultsInMsgBox);
  1923. TCaptureOutputEvent OutputEvent = NULL;
  1924. DebugAssert(FCapturedLog == NULL);
  1925. if (Capture)
  1926. {
  1927. FCapturedLog = new TStringList();
  1928. OutputEvent = TerminalCaptureLog;
  1929. }
  1930. try
  1931. {
  1932. if (!RemoteCustomCommand.IsFileCommand(Command))
  1933. {
  1934. Terminal->AnyCommand(RemoteCustomCommand.Complete(Command, true),
  1935. OutputEvent);
  1936. }
  1937. else
  1938. {
  1939. Terminal->CustomCommandOnFiles(Command, ACommand.Params, FileList, OutputEvent);
  1940. }
  1941. if ((FCapturedLog != NULL) && (FCapturedLog->Count > 0))
  1942. {
  1943. if (FLAGSET(ACommand.Params, ccCopyResults))
  1944. {
  1945. CopyToClipboard(FCapturedLog);
  1946. }
  1947. if (FLAGSET(ACommand.Params, ccShowResults))
  1948. {
  1949. DoConsoleDialog(Terminal, L"", FCapturedLog);
  1950. }
  1951. else if (FLAGSET(ACommand.Params, ccShowResultsInMsgBox))
  1952. {
  1953. MessageDialog(FCapturedLog->Text, qtInformation, qaOK);
  1954. }
  1955. }
  1956. }
  1957. __finally
  1958. {
  1959. SAFE_DESTROY(FCapturedLog);
  1960. }
  1961. }
  1962. }
  1963. //---------------------------------------------------------------------------
  1964. void __fastcall TCustomScpExplorerForm::LocalCustomCommandPure(
  1965. TStrings * FileList, const TCustomCommandType & ACommand, const UnicodeString & Command, TStrings * ALocalFileList,
  1966. const TCustomCommandData & Data, bool LocalFileCommand, bool FileListCommand, UnicodeString * POutput)
  1967. {
  1968. DebugAssert(!IsLocalBrowserMode());
  1969. TStrings * LocalFileList = NULL;
  1970. TStrings * RemoteFileList = NULL;
  1971. TStrings * RemoteFileListFull = NULL;
  1972. try
  1973. {
  1974. if (LocalFileCommand)
  1975. {
  1976. if (ALocalFileList == NULL)
  1977. {
  1978. DebugAssert(HasDirView[osLocal]);
  1979. // Cannot have focus on both panels, so we have to call AnyFileSelected
  1980. // directly (instead of EnableSelectedOperation) to pass
  1981. // false to FocusedFileOnlyWhenFocused
  1982. DebugAssert(DirView(osLocal)->AnyFileSelected(false, false, false));
  1983. LocalFileList = DirView(osLocal)->CreateFileList(false, true, NULL);
  1984. }
  1985. else
  1986. {
  1987. LocalFileList = ALocalFileList;
  1988. }
  1989. if (FileListCommand)
  1990. {
  1991. if (LocalFileList->Count != 1)
  1992. {
  1993. throw Exception(LoadStr(CUSTOM_COMMAND_SELECTED_UNMATCH1));
  1994. }
  1995. }
  1996. else
  1997. {
  1998. if ((LocalFileList->Count != 1) &&
  1999. (FileList->Count != 1) &&
  2000. (LocalFileList->Count != FileList->Count))
  2001. {
  2002. throw Exception(LoadStr(CUSTOM_COMMAND_SELECTED_UNMATCH));
  2003. }
  2004. }
  2005. }
  2006. UnicodeString RootTempDir;
  2007. UnicodeString TempDir;
  2008. bool RemoteFiles = FLAGSET(ACommand.Params, ccRemoteFiles);
  2009. if (!RemoteFiles)
  2010. {
  2011. TemporarilyDownloadFiles(FileList, false, RootTempDir, TempDir, false, true, true);
  2012. }
  2013. try
  2014. {
  2015. TDateTimes RemoteFileTimes;
  2016. if (RemoteFiles)
  2017. {
  2018. RemoteFileList = FileList;
  2019. }
  2020. else
  2021. {
  2022. RemoteFileList = new TStringList();
  2023. TMakeLocalFileListParams MakeFileListParam;
  2024. MakeFileListParam.FileList = RemoteFileList;
  2025. MakeFileListParam.FileTimes = &RemoteFileTimes;
  2026. MakeFileListParam.IncludeDirs = FLAGSET(ACommand.Params, ccApplyToDirectories);
  2027. MakeFileListParam.Recursive =
  2028. FLAGSET(ACommand.Params, ccRecursive) && !FileListCommand;
  2029. ProcessLocalDirectory(TempDir, Terminal->MakeLocalFileList, &MakeFileListParam);
  2030. if (!MakeFileListParam.Recursive)
  2031. {
  2032. RemoteFileTimes.clear();
  2033. RemoteFileListFull = new TStringList();
  2034. MakeFileListParam.FileList = RemoteFileListFull;
  2035. MakeFileListParam.Recursive = true;
  2036. MakeFileListParam.IncludeDirs = false;
  2037. ProcessLocalDirectory(TempDir, Terminal->MakeLocalFileList, &MakeFileListParam);
  2038. }
  2039. else
  2040. {
  2041. RemoteFileListFull = RemoteFileList;
  2042. }
  2043. }
  2044. bool NonBlocking = FileListCommand && RemoteFiles && !POutput;
  2045. TFileOperationProgressType Progress(&OperationProgress, &OperationFinished);
  2046. if (!NonBlocking)
  2047. {
  2048. int Count;
  2049. if (FileListCommand)
  2050. {
  2051. Count = 1;
  2052. }
  2053. else if (LocalFileCommand)
  2054. {
  2055. Count = std::max(LocalFileList->Count, RemoteFileList->Count);
  2056. }
  2057. else
  2058. {
  2059. Count = RemoteFileList->Count;
  2060. }
  2061. Progress.Start(foCustomCommand, osRemote, Count);
  2062. DebugAssert(FProgressForm != NULL);
  2063. FProgressForm->ReadOnly = true;
  2064. }
  2065. try
  2066. {
  2067. if (FileListCommand)
  2068. {
  2069. UnicodeString LocalFile;
  2070. // MakeFileList does not delimit filenames
  2071. UnicodeString FileList = MakeFileList(RemoteFileList);
  2072. if (LocalFileCommand)
  2073. {
  2074. DebugAssert(LocalFileList->Count == 1);
  2075. LocalFile = LocalFileList->Strings[0];
  2076. }
  2077. TLocalCustomCommand CustomCommand(Data,
  2078. Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(), L"", LocalFile, FileList);
  2079. UnicodeString ShellCommand = CustomCommand.Complete(Command, true);
  2080. if (NonBlocking)
  2081. {
  2082. DebugAssert(!POutput);
  2083. ExecuteShellChecked(ShellCommand);
  2084. }
  2085. else
  2086. {
  2087. ExecuteProcessCheckedAndWait(ShellCommand, HelpKeyword, POutput);
  2088. }
  2089. }
  2090. else if (LocalFileCommand)
  2091. {
  2092. if (LocalFileList->Count == 1)
  2093. {
  2094. UnicodeString LocalFile = LocalFileList->Strings[0];
  2095. for (int Index = 0; Index < RemoteFileList->Count; Index++)
  2096. {
  2097. UnicodeString FileName = RemoteFileList->Strings[Index];
  2098. UnicodeString FileNameForProgress = RemoteFiles ? FileName : ExtractFileName(FileName);
  2099. Progress.SetFile(FileNameForProgress);
  2100. TLocalCustomCommand CustomCommand(Data,
  2101. Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(), FileName, LocalFile, L"");
  2102. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  2103. TOnceDoneOperation OnceDoneOperation;
  2104. // Do not unselect anything, as it won't work with two panels anyway
  2105. Progress.Finish(UnicodeString(), true, OnceDoneOperation);
  2106. }
  2107. }
  2108. else if (RemoteFileList->Count == 1)
  2109. {
  2110. UnicodeString FileName = RemoteFileList->Strings[0];
  2111. for (int Index = 0; Index < LocalFileList->Count; Index++)
  2112. {
  2113. UnicodeString LocalFileName = LocalFileList->Strings[Index];
  2114. UnicodeString LocalFileNameOnly = ExtractFileName(LocalFileName);
  2115. Progress.SetFile(LocalFileNameOnly);
  2116. TLocalCustomCommand CustomCommand(
  2117. Data, Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(),
  2118. FileName, LocalFileName, L"");
  2119. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  2120. TOnceDoneOperation OnceDoneOperation;
  2121. // Do not unselect anything, as it won't work with two panels anyway
  2122. Progress.Finish(UnicodeString(), true, OnceDoneOperation);
  2123. }
  2124. }
  2125. else
  2126. {
  2127. if (LocalFileList->Count != RemoteFileList->Count)
  2128. {
  2129. throw Exception(LoadStr(CUSTOM_COMMAND_PAIRS_DOWNLOAD_FAILED));
  2130. }
  2131. for (int Index = 0; Index < LocalFileList->Count; Index++)
  2132. {
  2133. UnicodeString FileName = RemoteFileList->Strings[Index];
  2134. UnicodeString LocalFileName = LocalFileList->Strings[Index];
  2135. UnicodeString LocalFileNameOnly = ExtractFileName(LocalFileName);
  2136. Progress.SetFile(LocalFileNameOnly); // sic
  2137. TLocalCustomCommand CustomCommand(
  2138. Data, Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(),
  2139. FileName, LocalFileName, L"");
  2140. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  2141. TOnceDoneOperation OnceDoneOperation;
  2142. // Do not unselect anything, as it won't work with two panels anyway
  2143. Progress.Finish(UnicodeString(), true, OnceDoneOperation);
  2144. }
  2145. }
  2146. }
  2147. else
  2148. {
  2149. for (int Index = 0; Index < RemoteFileList->Count; Index++)
  2150. {
  2151. UnicodeString FileName = RemoteFileList->Strings[Index];
  2152. UnicodeString FileNameForProgress = RemoteFiles ? FileName : ExtractFileName(FileName);
  2153. Progress.SetFile(FileNameForProgress);
  2154. TLocalCustomCommand CustomCommand(Data,
  2155. Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(),
  2156. FileName, L"", L"");
  2157. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  2158. TOnceDoneOperation OnceDoneOperation;
  2159. Progress.Finish(FileNameForProgress, true, OnceDoneOperation);
  2160. }
  2161. }
  2162. }
  2163. __finally
  2164. {
  2165. if (!NonBlocking)
  2166. {
  2167. Progress.Stop();
  2168. }
  2169. }
  2170. DebugAssert(!FAutoOperation);
  2171. if (!RemoteFiles)
  2172. {
  2173. TempDir = IncludeTrailingBackslash(TempDir);
  2174. for (int Index = 0; Index < RemoteFileListFull->Count; Index++)
  2175. {
  2176. UnicodeString FileName = RemoteFileListFull->Strings[Index];
  2177. if (DebugAlwaysTrue(SameText(TempDir, FileName.SubString(1, TempDir.Length()))) &&
  2178. // Skip directories, process only nested files.
  2179. // The check is redundant as FileAge fails for directories anyway.
  2180. !DirectoryExists(FileName))
  2181. {
  2182. UnicodeString RemoteDir =
  2183. UnixExtractFileDir(
  2184. UnixIncludeTrailingBackslash(Terminal->CurrentDirectory) +
  2185. ToUnixPath(FileName.SubString(TempDir.Length() + 1, FileName.Length() - TempDir.Length())));
  2186. TDateTime NewTime;
  2187. if (FileAge(FileName, NewTime) &&
  2188. (NewTime != RemoteFileTimes[Index]))
  2189. {
  2190. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  2191. TemporaryFileCopyParam(CopyParam);
  2192. CopyParam.FileMask = L"";
  2193. FAutoOperation = true;
  2194. std::unique_ptr<TStrings> TemporaryFilesList(new TStringList());
  2195. TemporaryFilesList->Add(FileName);
  2196. Terminal->CopyToRemote(TemporaryFilesList.get(), RemoteDir, &CopyParam, cpTemporary, NULL);
  2197. }
  2198. }
  2199. }
  2200. }
  2201. }
  2202. __finally
  2203. {
  2204. FAutoOperation = false;
  2205. if (!RootTempDir.IsEmpty() && DebugAlwaysTrue(!RemoteFiles))
  2206. {
  2207. RecursiveDeleteFile(ExcludeTrailingBackslash(RootTempDir), false);
  2208. }
  2209. }
  2210. }
  2211. __finally
  2212. {
  2213. if (RemoteFileList != FileList)
  2214. {
  2215. delete RemoteFileList;
  2216. }
  2217. if (LocalFileList != ALocalFileList)
  2218. {
  2219. delete LocalFileList;
  2220. }
  2221. if (RemoteFileListFull != RemoteFileList)
  2222. {
  2223. delete RemoteFileListFull;
  2224. }
  2225. }
  2226. }
  2227. //---------------------------------------------------------------------------
  2228. void __fastcall TCustomScpExplorerForm::LocalCustomCommandWithLocalFiles(
  2229. const TCustomCommandType & ACommand, const UnicodeString & Command, const TCustomCommandData & Data,
  2230. bool FileListCommand, UnicodeString * POutput)
  2231. {
  2232. std::unique_ptr<TStrings> SelectedFileList(DirView(osCurrent)->CreateFileList(false, true, NULL));
  2233. std::unique_ptr<TStrings> LocalFileList(new TStringList());
  2234. for (int Index = 0; Index < SelectedFileList->Count; Index++)
  2235. {
  2236. UnicodeString FileName = SelectedFileList->Strings[Index];
  2237. if (DirectoryExists(FileName))
  2238. {
  2239. if (FLAGSET(ACommand.Params, ccApplyToDirectories))
  2240. {
  2241. LocalFileList->Add(FileName);
  2242. }
  2243. if (FLAGSET(ACommand.Params, ccRecursive))
  2244. {
  2245. TMakeLocalFileListParams MakeFileListParam;
  2246. MakeFileListParam.FileList = LocalFileList.get();
  2247. MakeFileListParam.FileTimes = NULL;
  2248. MakeFileListParam.IncludeDirs = FLAGSET(ACommand.Params, ccApplyToDirectories);
  2249. MakeFileListParam.Recursive = true;
  2250. ProcessLocalDirectory(FileName, Terminal->MakeLocalFileList, &MakeFileListParam);
  2251. }
  2252. }
  2253. else
  2254. {
  2255. LocalFileList->Add(FileName);
  2256. }
  2257. }
  2258. UnicodeString RemotePath;
  2259. if (Terminal != NULL)
  2260. {
  2261. RemotePath = Terminal->CurrentDirectory;
  2262. }
  2263. if (FileListCommand)
  2264. {
  2265. UnicodeString FileList = MakeFileList(LocalFileList.get());
  2266. TLocalCustomCommand CustomCommand(
  2267. Data, RemotePath, DefaultDownloadTargetDirectory(),
  2268. L"", L"", FileList);
  2269. ExecuteProcessChecked(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  2270. }
  2271. else
  2272. {
  2273. TFileOperationProgressType Progress(&OperationProgress, &OperationFinished);
  2274. Progress.Start(foCustomCommand, osLocal, LocalFileList->Count);
  2275. DebugAssert(FProgressForm != NULL);
  2276. FProgressForm->ReadOnly = true;
  2277. try
  2278. {
  2279. for (int Index = 0; Index < LocalFileList->Count; Index++)
  2280. {
  2281. UnicodeString FileName = LocalFileList->Strings[Index];
  2282. Progress.SetFile(FileName);
  2283. TLocalCustomCommand CustomCommand(
  2284. Data, RemotePath, DefaultDownloadTargetDirectory(),
  2285. FileName, L"", L"");
  2286. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  2287. TOnceDoneOperation OnceDoneOperation;
  2288. Progress.Finish(FileName, true, OnceDoneOperation);
  2289. }
  2290. }
  2291. __finally
  2292. {
  2293. Progress.Stop();
  2294. }
  2295. }
  2296. }
  2297. //---------------------------------------------------------------------------
  2298. void __fastcall TCustomScpExplorerForm::LocalCustomCommand(TStrings * FileList,
  2299. const TCustomCommandType & ACommand, TStrings * ALocalFileList,
  2300. const TCustomCommandData & Data, const UnicodeString & CommandCommand)
  2301. {
  2302. UnicodeString RemotePath = (Terminal != NULL) ? Terminal->CurrentDirectory : UnicodeString();
  2303. TLocalCustomCommand LocalCustomCommand(Data, RemotePath, DefaultDownloadTargetDirectory());
  2304. TWinInteractiveCustomCommand InteractiveCustomCommand(
  2305. &LocalCustomCommand, ACommand.Name, ACommand.HomePage);
  2306. UnicodeString Command = InteractiveCustomCommand.Complete(CommandCommand, false);
  2307. bool FileListCommand = LocalCustomCommand.IsFileListCommand(Command);
  2308. bool LocalFileCommand = LocalCustomCommand.HasLocalFileName(Command);
  2309. Configuration->Usage->Inc(L"LocalCustomCommandRuns2");
  2310. std::unique_ptr<UnicodeString> POutput;
  2311. if (FLAGSET(ACommand.Params, ccShowResultsInMsgBox) ||
  2312. FLAGSET(ACommand.Params, ccCopyResults))
  2313. {
  2314. POutput.reset(new UnicodeString());
  2315. }
  2316. TValueRestorer<TOperationSide> ProgressSideRestorer(FProgressSide);
  2317. FProgressSide = FCurrentSide;
  2318. if (!LocalCustomCommand.IsFileCommand(Command))
  2319. {
  2320. ExecuteProcessChecked(LocalCustomCommand.Complete(Command, true), HelpKeyword, POutput.get());
  2321. }
  2322. // remote files?
  2323. else if (!IsSideLocalBrowser(FCurrentSide) || LocalFileCommand)
  2324. {
  2325. LocalCustomCommandPure(FileList, ACommand, Command, ALocalFileList, Data, LocalFileCommand, FileListCommand, POutput.get());
  2326. }
  2327. // local files
  2328. else
  2329. {
  2330. LocalCustomCommandWithLocalFiles(ACommand, Command, Data, FileListCommand, POutput.get());
  2331. }
  2332. if (POutput.get() != NULL)
  2333. {
  2334. // If the output is single-line, we do not want the trailing CRLF, as the CopyToClipboard(TStrings *) does
  2335. int P = POutput->Pos(sLineBreak);
  2336. if (P == POutput->Length() - static_cast<int>(strlen(sLineBreak)) + 1)
  2337. {
  2338. POutput->SetLength(P - 1);
  2339. }
  2340. // Copy even empty output to clipboard
  2341. if (FLAGSET(ACommand.Params, ccCopyResults))
  2342. {
  2343. CopyToClipboard(*POutput);
  2344. }
  2345. // But do not show an empty message box.
  2346. // This way the ShowResultsInMsgBox can be used to suppress a console window of commands with no output
  2347. if (FLAGSET(ACommand.Params, ccShowResultsInMsgBox) &&
  2348. !POutput->IsEmpty())
  2349. {
  2350. TClipboardHandler ClipboardHandler;
  2351. ClipboardHandler.Text = *POutput;
  2352. TMessageParams Params;
  2353. TQueryButtonAlias Aliases[1];
  2354. Aliases[0].Button = qaRetry;
  2355. Aliases[0].Alias = LoadStr(EDIT_COPY);
  2356. Aliases[0].OnSubmit = &ClipboardHandler.Copy;
  2357. Params.Aliases = Aliases;
  2358. Params.AliasesCount = LENOF(Aliases);
  2359. MessageDialog(*POutput, qtInformation, qaOK | qaRetry, HelpKeyword, &Params);
  2360. }
  2361. }
  2362. }
  2363. //---------------------------------------------------------------------------
  2364. void __fastcall TCustomScpExplorerForm::CustomCommand(TStrings * FileList,
  2365. const TCustomCommandType & ACommand, TStrings * ALocalFileList)
  2366. {
  2367. TCustomCommandData Data;
  2368. UnicodeString Site;
  2369. UnicodeString RemotePath;
  2370. if (HasActiveTerminal())
  2371. {
  2372. std::unique_ptr<TSessionData> SessionData(SessionDataForCode());
  2373. Data = TCustomCommandData(SessionData.get());
  2374. Site = Terminal->SessionData->SessionKey;
  2375. RemotePath = Terminal->CurrentDirectory;
  2376. }
  2377. UnicodeString HelpKeyword = ACommand.HomePage;
  2378. std::unique_ptr<TStrings> CustomCommandOptions(CloneStrings(WinConfiguration->CustomCommandOptions));
  2379. if (ACommand.AnyOptionWithFlag(TCustomCommandType::ofRun))
  2380. {
  2381. std::unique_ptr<TCustomCommand> CustomCommandForOptions;
  2382. if (FLAGCLEAR(ACommand.Params, ccLocal))
  2383. {
  2384. CustomCommandForOptions.reset(new TRemoteCustomCommand(Data, RemotePath));
  2385. }
  2386. else
  2387. {
  2388. CustomCommandForOptions.reset(new TLocalCustomCommand(Data, RemotePath, DefaultDownloadTargetDirectory()));
  2389. }
  2390. if (!DoCustomCommandOptionsDialog(
  2391. &ACommand, CustomCommandOptions.get(), NULL, TCustomCommandType::ofRun, CustomCommandForOptions.get(), Site, NULL))
  2392. {
  2393. Abort();
  2394. }
  2395. }
  2396. UnicodeString CommandCommand = ACommand.GetCommandWithExpandedOptions(CustomCommandOptions.get(), Site);
  2397. if (FLAGCLEAR(ACommand.Params, ccLocal))
  2398. {
  2399. RemoteCustomCommand(FileList, ACommand, Data, CommandCommand);
  2400. }
  2401. else
  2402. {
  2403. LocalCustomCommand(FileList, ACommand, ALocalFileList, Data, CommandCommand);
  2404. }
  2405. }
  2406. //---------------------------------------------------------------------------
  2407. void __fastcall TCustomScpExplorerForm::BothCustomCommand(
  2408. const TCustomCommandType & Command)
  2409. {
  2410. DebugAssert(FCustomCommandLocalFileList != NULL);
  2411. DebugAssert(FCustomCommandRemoteFileList != NULL);
  2412. DebugAssert(FCustomCommandLocalFileList->Count == FCustomCommandRemoteFileList->Count);
  2413. TStrings * LocalFileList = new TStringList();
  2414. TStrings * RemoteFileList = new TStringList();
  2415. try
  2416. {
  2417. for (int Index = 0; Index < FCustomCommandLocalFileList->Count; Index++)
  2418. {
  2419. LocalFileList->Clear();
  2420. LocalFileList->AddObject(
  2421. FCustomCommandLocalFileList->Strings[Index],
  2422. FCustomCommandLocalFileList->Objects[Index]);
  2423. RemoteFileList->Clear();
  2424. RemoteFileList->AddObject(
  2425. FCustomCommandRemoteFileList->Strings[Index],
  2426. FCustomCommandRemoteFileList->Objects[Index]);
  2427. CustomCommand(RemoteFileList, Command, LocalFileList);
  2428. }
  2429. }
  2430. __finally
  2431. {
  2432. delete LocalFileList;
  2433. delete RemoteFileList;
  2434. }
  2435. }
  2436. //---------------------------------------------------------------------------
  2437. void __fastcall TCustomScpExplorerForm::CustomCommandMenu(
  2438. TAction * Action, TStrings * LocalFileList, TStrings * RemoteFileList)
  2439. {
  2440. delete FCustomCommandLocalFileList;
  2441. delete FCustomCommandRemoteFileList;
  2442. // takeover ownership,
  2443. // the lists must survive the MenuPopup as OnClick occurs only after it exits
  2444. FCustomCommandLocalFileList = LocalFileList;
  2445. FCustomCommandRemoteFileList = RemoteFileList;
  2446. TButton * Button = dynamic_cast<TButton *>(Action->ActionComponent);
  2447. if (Button != NULL)
  2448. {
  2449. FCustomCommandMenu->Items->Clear();
  2450. NonVisualDataModule->CreateCustomCommandsMenu(FCustomCommandMenu->Items, false, false, ccltBoth, NULL);
  2451. MenuPopup(FCustomCommandMenu, Button);
  2452. }
  2453. else
  2454. {
  2455. NonVisualDataModule->CreateCustomCommandsMenu(Action, false, ccltBoth);
  2456. }
  2457. }
  2458. //---------------------------------------------------------------------------
  2459. void __fastcall TCustomScpExplorerForm::TerminalCaptureLog(
  2460. const UnicodeString & AddedLine, TCaptureOutputType OutputType)
  2461. {
  2462. DebugAssert(FCapturedLog != NULL);
  2463. if ((OutputType == cotOutput) || (OutputType == cotError))
  2464. {
  2465. FCapturedLog->Add(AddedLine);
  2466. }
  2467. }
  2468. //---------------------------------------------------------------------------
  2469. bool __fastcall TCustomScpExplorerForm::IsFileControl(TObject * Control,
  2470. TOperationSide Side)
  2471. {
  2472. return
  2473. (Side == osOther) &&
  2474. ((Control == DirView(osOther)) || (Control == DriveView(osOther)));
  2475. }
  2476. //---------------------------------------------------------------------------
  2477. void __fastcall TCustomScpExplorerForm::DirViewContextPopupDefaultItem(
  2478. TOperationSide Side, TTBXCustomItem * Item,
  2479. TResolvedDoubleClickAction DoubleClickAction1, TResolvedDoubleClickAction DoubleClickAction2)
  2480. {
  2481. TCustomDirView * DView = DirView(Side);
  2482. bool IsDefault = false;
  2483. if (DView->ItemFocused != NULL)
  2484. {
  2485. TTerminal * ATerminal = !IsSideLocalBrowser(Side) ? Terminal : NULL;
  2486. TResolvedDoubleClickAction DefaultAction =
  2487. WinConfiguration->ResolveDoubleClickAction(DView->ItemIsDirectory(DView->ItemFocused), ATerminal);
  2488. IsDefault = (DefaultAction == DoubleClickAction1) || (DefaultAction == DoubleClickAction2);
  2489. }
  2490. TTBItemOptions O = Item->Options;
  2491. if (IsDefault)
  2492. {
  2493. Item->Options = O << tboDefault;
  2494. }
  2495. else
  2496. {
  2497. Item->Options = O >> tboDefault;
  2498. }
  2499. }
  2500. //---------------------------------------------------------------------------
  2501. void __fastcall TCustomScpExplorerForm::DirViewContextPopup(
  2502. TOperationSide Side, Byte PopupComponent, const TPoint &MousePos)
  2503. {
  2504. TCustomDirView * DView = DirView(Side);
  2505. TListItem * Item = DView->ItemFocused;
  2506. if ((DView->GetItemAt(MousePos.x, MousePos.y) == Item) &&
  2507. EnableFocusedOperation[Side])
  2508. {
  2509. TPoint ClientPoint;
  2510. ClientPoint = ((MousePos.x < 0) && (MousePos.y < 0)) ?
  2511. TPoint(0, 0) : MousePos;
  2512. FLastContextPopupScreenPoint = DView->ClientToScreen(ClientPoint);
  2513. reinterpret_cast<TPopupMenu*>(GetComponent(PopupComponent))->Popup(
  2514. FLastContextPopupScreenPoint.x, FLastContextPopupScreenPoint.y);
  2515. }
  2516. }
  2517. //---------------------------------------------------------------------------
  2518. void __fastcall TCustomScpExplorerForm::RemoteDirViewContextPopup(
  2519. TObject * /*Sender*/, const TPoint &MousePos, bool &Handled)
  2520. {
  2521. DirViewContextPopupDefaultItem(osRemote, NonVisualDataModule->RemoteOpenMenuItem, rdcaChangeDir, rdcaOpen);
  2522. DirViewContextPopupDefaultItem(osRemote, NonVisualDataModule->RemoteEditMenuItem, rdcaEdit, rdcaNone);
  2523. DirViewContextPopupDefaultItem(osRemote, NonVisualDataModule->RemoteCopyMenuItem, rdcaCopy, rdcaNone);
  2524. DirViewContextPopup(osRemote, fcRemotePopup, MousePos);
  2525. Handled = true;
  2526. }
  2527. //---------------------------------------------------------------------------
  2528. void __fastcall TCustomScpExplorerForm::ReloadLocalDirectory(const UnicodeString Directory)
  2529. {
  2530. }
  2531. //---------------------------------------------------------------------------
  2532. void __fastcall TCustomScpExplorerForm::BatchStart(void *& /*Storage*/)
  2533. {
  2534. DebugAssert(FErrorList == NULL);
  2535. if (WinConfiguration->ContinueOnError)
  2536. {
  2537. FErrorList = new TStringList();
  2538. Configuration->Usage->Inc(L"ContinuationsOnError");
  2539. }
  2540. NonVisualDataModule->StartBusy();
  2541. }
  2542. //---------------------------------------------------------------------------
  2543. void __fastcall TCustomScpExplorerForm::BatchEnd(void * /*Storage*/)
  2544. {
  2545. NonVisualDataModule->EndBusy();
  2546. if (FErrorList)
  2547. {
  2548. HandleErrorList(FErrorList);
  2549. }
  2550. }
  2551. //---------------------------------------------------------------------------
  2552. void __fastcall TCustomScpExplorerForm::UpdateCopyParamCounters(
  2553. const TCopyParamType & CopyParam)
  2554. {
  2555. if (!CopyParam.IncludeFileMask.Masks.IsEmpty())
  2556. {
  2557. Configuration->Usage->Inc(L"FileMaskUses");
  2558. }
  2559. if (IsEffectiveFileNameMask(CopyParam.FileMask))
  2560. {
  2561. Configuration->Usage->Inc(L"OperationMaskUses");
  2562. }
  2563. }
  2564. //---------------------------------------------------------------------------
  2565. bool __fastcall TCustomScpExplorerForm::ExecuteCopyMoveFileOperation(
  2566. TFileOperation Operation, TOperationSide Side, TStrings * FileList, bool NoConfirmation, void * AParam)
  2567. {
  2568. DebugAssert(!IsLocalBrowserMode());
  2569. TTransferDirection Direction = (Side == osLocal ? tdToRemote : tdToLocal);
  2570. TTransferType Type = (Operation == foCopy ? ttCopy : ttMove);
  2571. TTransferOperationParam DefaultParam;
  2572. TTransferOperationParam & Param = (AParam != NULL) ? *static_cast<TTransferOperationParam *>(AParam) : DefaultParam;
  2573. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  2574. switch (Param.Queue)
  2575. {
  2576. case asOn:
  2577. CopyParam.Queue = true;
  2578. break;
  2579. case asOff:
  2580. CopyParam.Queue = false;
  2581. break;
  2582. case asAuto:
  2583. default:
  2584. // keep default
  2585. break;
  2586. }
  2587. bool Result =
  2588. CopyParamDialog(Direction, Type, Param.Temp, FileList, Param.TargetDirectory, CopyParam, !NoConfirmation, Param.DragDrop, Param.Options);
  2589. if (Result)
  2590. {
  2591. DebugAssert(Terminal);
  2592. bool SelectionRestored = false;
  2593. TCustomDirView * DView = NULL;
  2594. if (HasDirView[Side])
  2595. {
  2596. DView = DirView(Side);
  2597. DView->SaveSelection();
  2598. DView->SaveSelectedNames();
  2599. }
  2600. UpdateCopyParamCounters(CopyParam);
  2601. std::unique_ptr<TStringList> TransferResumeList(new TStringList());
  2602. DebugAssert(FTransferResumeList == NULL);
  2603. FTransferResumeList = Terminal->IsCapable[fcMoveToQueue] ? TransferResumeList.get() : NULL;
  2604. FMoveToQueue = false;
  2605. int Params = FLAGMASK(Operation == foMove, cpDelete);
  2606. try
  2607. {
  2608. TStrings * PermanentFileList;
  2609. std::unique_ptr<TStrings> PermanentFileListOwner;
  2610. try
  2611. {
  2612. if (Side == osLocal)
  2613. {
  2614. PermanentFileList = FileList;
  2615. Params |= FLAGMASK(Param.Temp, cpTemporary);
  2616. CopyParam.IncludeFileMask.SetRoots(FileList, Param.TargetDirectory);
  2617. Terminal->CopyToRemote(FileList, Param.TargetDirectory, &CopyParam, Params, NULL);
  2618. if (Operation == foMove)
  2619. {
  2620. ReloadLocalDirectory();
  2621. if (DView != NULL)
  2622. {
  2623. DView->RestoreSelection();
  2624. }
  2625. SelectionRestored = true;
  2626. }
  2627. }
  2628. else
  2629. {
  2630. // Clone the file list as it may refer to current directory files,
  2631. // which get destroyed, when the source directory is reloaded after foMove operation.
  2632. // We should actually clone the file list for whole ExecuteFileOperation to protect against reloads.
  2633. // But for a hotfix, we are not going to do such a big change.
  2634. PermanentFileListOwner.reset(TRemoteFileList::CloneStrings(FileList));
  2635. PermanentFileList = PermanentFileListOwner.get();
  2636. try
  2637. {
  2638. CopyParam.IncludeFileMask.SetRoots(Param.TargetDirectory, FileList);
  2639. Terminal->CopyToLocal(FileList, Param.TargetDirectory, &CopyParam, Params, NULL);
  2640. }
  2641. __finally
  2642. {
  2643. if (Operation == foMove)
  2644. {
  2645. if (DView != NULL)
  2646. {
  2647. DView->RestoreSelection();
  2648. }
  2649. SelectionRestored = true;
  2650. }
  2651. ReloadLocalDirectory(Param.TargetDirectory);
  2652. }
  2653. }
  2654. // When the transfer batch is aborted between individual file transfers, the Abort is not called.
  2655. if (FMoveToQueue)
  2656. {
  2657. Abort();
  2658. }
  2659. }
  2660. catch (EAbort &)
  2661. {
  2662. if (FMoveToQueue)
  2663. {
  2664. Params |= (CopyParam.QueueNoConfirmation ? cpNoConfirmation : 0);
  2665. DebugAssert(CopyParam.TransferSkipList == NULL);
  2666. DebugAssert(CopyParam.TransferResumeFile.IsEmpty());
  2667. if (TransferResumeList->Count > 0)
  2668. {
  2669. CopyParam.TransferResumeFile = TransferResumeList->Strings[TransferResumeList->Count - 1];
  2670. TransferResumeList->Delete(TransferResumeList->Count - 1);
  2671. }
  2672. CopyParam.TransferSkipList = TransferResumeList.release();
  2673. // not really needed, just to keep it consistent with TransferResumeList
  2674. FTransferResumeList = NULL;
  2675. FMoveToQueue = false;
  2676. Configuration->Usage->Inc("MovesToBackground");
  2677. AddQueueItem(Queue, Direction, PermanentFileList, Param.TargetDirectory, CopyParam, Params);
  2678. ClearTransferSourceSelection(Direction);
  2679. }
  2680. throw;
  2681. }
  2682. }
  2683. __finally
  2684. {
  2685. if (!SelectionRestored && (DView != NULL))
  2686. {
  2687. DView->DiscardSavedSelection();
  2688. }
  2689. FTransferResumeList = NULL;
  2690. }
  2691. }
  2692. return Result;
  2693. }
  2694. //---------------------------------------------------------------------------
  2695. bool __fastcall TCustomScpExplorerForm::ExecuteDeleteFileOperation(
  2696. TOperationSide Side, TStrings * FileList, void * Param)
  2697. {
  2698. DebugAssert(FileList->Count);
  2699. // We deliberately do not toggle alternative flag (Param), but use OR,
  2700. // because the Param is set only when command is invoked using Shift-Del/F8 keyboard
  2701. // shortcut of CurrentDeleteAlternativeAction
  2702. bool Alternative =
  2703. bool(Param) || UseAlternativeFunction();
  2704. bool Recycle;
  2705. if (IsSideLocalBrowser(Side))
  2706. {
  2707. Recycle = (WinConfiguration->DeleteToRecycleBin != Alternative);
  2708. }
  2709. else
  2710. {
  2711. Recycle =
  2712. (Terminal->SessionData->DeleteToRecycleBin != Alternative) &&
  2713. !Terminal->SessionData->RecycleBinPath.IsEmpty() &&
  2714. !Terminal->IsRecycledFile(FileList->Strings[0]);
  2715. }
  2716. bool Result = !(Recycle ? WinConfiguration->ConfirmRecycling : WinConfiguration->ConfirmDeleting);
  2717. if (!Result)
  2718. {
  2719. UnicodeString Query;
  2720. if (FileList->Count == 1)
  2721. {
  2722. if (IsSideLocalBrowser(Side))
  2723. {
  2724. Query = ExtractFileName(FileList->Strings[0]);
  2725. }
  2726. else
  2727. {
  2728. Query = UnixExtractFileName(FileList->Strings[0]);
  2729. }
  2730. Query = FMTLOAD((Recycle ? CONFIRM_RECYCLE_FILE : CONFIRM_DELETE_FILE), (Query));
  2731. }
  2732. else
  2733. {
  2734. Query = FMTLOAD((Recycle ? CONFIRM_RECYCLE_FILES : CONFIRM_DELETE_FILES), (FileList->Count));
  2735. }
  2736. TMessageParams Params(mpNeverAskAgainCheck);
  2737. Params.ImageName = L"Delete file";
  2738. unsigned int Answer =
  2739. MessageDialog(MainInstructions(Query), qtConfirmation, qaOK | qaCancel, HELP_DELETE_FILE, &Params);
  2740. if (Answer == qaNeverAskAgain)
  2741. {
  2742. Result = true;
  2743. if (Recycle)
  2744. {
  2745. WinConfiguration->ConfirmRecycling = false;
  2746. }
  2747. else
  2748. {
  2749. WinConfiguration->ConfirmDeleting = false;
  2750. }
  2751. }
  2752. else
  2753. {
  2754. Result = (Answer == qaOK);
  2755. }
  2756. }
  2757. if (Result)
  2758. {
  2759. DeleteFiles(Side, FileList, FLAGMASK(Alternative, dfAlternative));
  2760. }
  2761. return Result;
  2762. }
  2763. //---------------------------------------------------------------------------
  2764. bool __fastcall TCustomScpExplorerForm::ExecuteFileOperation(TFileOperation Operation,
  2765. TOperationSide Side, TStrings * FileList, bool NoConfirmation, void * Param)
  2766. {
  2767. TAutoBatch AutoBatch(this);
  2768. bool Result;
  2769. if (TFileOperationProgressType::IsTransferOperation(Operation))
  2770. {
  2771. Result = ExecuteCopyMoveFileOperation(Operation, Side, FileList, NoConfirmation, Param);
  2772. }
  2773. else if (Operation == foRename)
  2774. {
  2775. DebugAssert(DirView(Side)->ItemFocused);
  2776. DirView(Side)->ItemFocused->EditCaption();
  2777. Result = true;
  2778. }
  2779. else if (Operation == foDelete)
  2780. {
  2781. Result = ExecuteDeleteFileOperation(Side, FileList, Param);
  2782. }
  2783. else if (Operation == foSetProperties)
  2784. {
  2785. DirView(osRemote)->SaveSelectedNames();
  2786. Result = SetProperties(Side, FileList);
  2787. }
  2788. else if (Operation == foCustomCommand)
  2789. {
  2790. DebugAssert(Param);
  2791. DebugAssert(Side == osRemote); // Side carries no meaning for foCustomCommand
  2792. DirView(osRemote)->SaveSelectedNames();
  2793. const TCustomCommandType * Command = static_cast<const TCustomCommandType*>(Param);
  2794. CustomCommand(FileList, *Command, NULL);
  2795. Result = true;
  2796. }
  2797. else if ((Operation == foRemoteMove) || (Operation == foRemoteCopy))
  2798. {
  2799. DebugAssert(!IsSideLocalBrowser(Side));
  2800. Result = RemoteTransferFiles(FileList, NoConfirmation,
  2801. (Operation == foRemoteMove), reinterpret_cast<TManagedTerminal *>(Param));
  2802. }
  2803. else if (Operation == foLock)
  2804. {
  2805. DebugAssert(!IsSideLocalBrowser(Side));
  2806. LockFiles(FileList, true);
  2807. Result = true;
  2808. }
  2809. else if (Operation == foUnlock)
  2810. {
  2811. DebugAssert(!IsSideLocalBrowser(Side));
  2812. LockFiles(FileList, false);
  2813. Result = true;
  2814. }
  2815. else
  2816. {
  2817. DebugFail();
  2818. }
  2819. return Result;
  2820. }
  2821. //---------------------------------------------------------------------------
  2822. TOperationSide __fastcall TCustomScpExplorerForm::GetSide(TOperationSide Side)
  2823. {
  2824. if (Side == osCurrent)
  2825. {
  2826. Side = FCurrentSide;
  2827. }
  2828. return Side;
  2829. }
  2830. //---------------------------------------------------------------------------
  2831. bool __fastcall TCustomScpExplorerForm::ExecuteFileOperation(TFileOperation Operation,
  2832. TOperationSide Side, bool OnFocused, bool NoConfirmation, void * Param)
  2833. {
  2834. Side = GetSide(Side);
  2835. bool FullPath = IsSideLocalBrowser(Side);
  2836. TCustomDirView * ADirView = DirView(Side);
  2837. std::unique_ptr<TStringList> FileList(new TStringList());
  2838. ADirView->CreateFileList(OnFocused, FullPath, FileList.get());
  2839. bool ReloadProperties =
  2840. !IsSideLocalBrowser(Side) && (Operation == foSetProperties) &&
  2841. DebugAlwaysTrue(HasActiveTerminal()) && Terminal->IsCapable[fcLoadingAdditionalProperties];
  2842. bool Result = ExecuteFileOperation(Operation, Side, FileList.get(), NoConfirmation, Param);
  2843. if (Result && ReloadProperties)
  2844. {
  2845. std::unique_ptr<TStringList> FileList2(new TStringList());
  2846. FileList->CaseSensitive = true;
  2847. FileList->Sort();
  2848. for (int Index = 0; Index < ADirView->Items->Count; Index++)
  2849. {
  2850. TListItem * Item = ADirView->Items->Item[Index];
  2851. UnicodeString FileName = ADirView->ItemFileName(Item);
  2852. int Index2;
  2853. if (FileList->Find(FileName, Index2))
  2854. {
  2855. FileList2->AddObject(FileName, ADirView->ItemData(Item));
  2856. }
  2857. }
  2858. LoadFilesProperties(FileList2.get());
  2859. }
  2860. return Result;
  2861. }
  2862. //---------------------------------------------------------------------------
  2863. void __fastcall TCustomScpExplorerForm::ExecuteFileOperationCommand(
  2864. TFileOperation Operation, TOperationSide Side, bool OnFocused,
  2865. bool NoConfirmation, void * Param)
  2866. {
  2867. if (ExecuteFileOperation(Operation, Side, OnFocused, NoConfirmation, Param))
  2868. {
  2869. if (TFileOperationProgressType::IsTransferOperation(Operation))
  2870. {
  2871. DebugAssert(!IsLocalBrowserMode());
  2872. if (GetSide(Side) == osLocal)
  2873. {
  2874. Configuration->Usage->Inc(L"UploadsCommand");
  2875. }
  2876. else
  2877. {
  2878. Configuration->Usage->Inc(L"DownloadsCommand");
  2879. }
  2880. }
  2881. }
  2882. }
  2883. //---------------------------------------------------------------------------
  2884. void __fastcall TCustomScpExplorerForm::ExecuteCopyOperationCommand(
  2885. TOperationSide Side, bool OnFocused, unsigned int Flags)
  2886. {
  2887. TTransferOperationParam Param;
  2888. if ((WinConfiguration->Interface != ifCommander) ||
  2889. WinConfiguration->ScpCommander.ExplorerKeyboardShortcuts)
  2890. {
  2891. Flags &= ~cocShortCutHint;
  2892. }
  2893. TCustomDirView * DView = DirView(Side);
  2894. Param.Options =
  2895. FLAGMASK(FLAGSET(Flags, cocShortCutHint), coShortCutHint) |
  2896. FLAGMASK(SelectedAllFilesInDirView(DView), coAllFiles);
  2897. if (FLAGSET(Flags, cocQueue))
  2898. {
  2899. Param.Queue = asOn;
  2900. }
  2901. else if (FLAGSET(Flags, cocNonQueue))
  2902. {
  2903. Param.Queue = asOff;
  2904. }
  2905. ExecuteFileOperationCommand(foCopy, Side, OnFocused, false, &Param);
  2906. }
  2907. //---------------------------------------------------------------------------
  2908. void __fastcall TCustomScpExplorerForm::HandleErrorList(TStringList *& ErrorList)
  2909. {
  2910. int Count = ErrorList->Count;
  2911. try
  2912. {
  2913. if (ErrorList->Count)
  2914. {
  2915. UnicodeString Message = MainInstructions(FMTLOAD(ERROR_LIST_COUNT, (ErrorList->Count)));
  2916. if (MessageDialog(Message, qtError,
  2917. qaOK | qaCancel, HELP_NONE) == qaOK)
  2918. {
  2919. unsigned int Answer;
  2920. int Index = 0;
  2921. do
  2922. {
  2923. DebugAssert(Index >= 0 && Index < Count);
  2924. TQueryButtonAlias Aliases[2];
  2925. Aliases[0].Button = qaYes;
  2926. Aliases[0].Alias = LoadStr(PREV_BUTTON);
  2927. Aliases[1].Button = qaNo;
  2928. Aliases[1].Alias = LoadStr(NEXT_BUTTON);
  2929. TMessageParams Params;
  2930. Params.Aliases = Aliases;
  2931. Params.AliasesCount = LENOF(Aliases);
  2932. int No = Index + 1;
  2933. UnicodeString Message = ErrorList->Strings[Index];
  2934. UnicodeString AMainInstructions;
  2935. UnicodeString Details;
  2936. if (ExtractMainInstructions(Message, AMainInstructions))
  2937. {
  2938. Details = Message;
  2939. Message = AMainInstructions;
  2940. }
  2941. Message = MainInstructions(FMTLOAD(ERROR_LIST_NUMBER, (No, Count, Message))) + Details;
  2942. Answer = MoreMessageDialog(
  2943. Message,
  2944. dynamic_cast<TStrings *>(ErrorList->Objects[Index]), qtError,
  2945. (Index ? qaYes : 0) | (Index < Count - 1 ? qaNo : 0) |
  2946. qaOK, HELP_NONE, &Params);
  2947. if (Answer == qaNo)
  2948. {
  2949. Index++;
  2950. }
  2951. if (Answer == qaYes)
  2952. {
  2953. Index--;
  2954. }
  2955. }
  2956. while (Answer != qaOK);
  2957. }
  2958. }
  2959. }
  2960. __finally
  2961. {
  2962. TStrings * List = ErrorList;
  2963. ErrorList = NULL;
  2964. for (int i = 0; i < Count; i++)
  2965. {
  2966. delete List->Objects[i];
  2967. }
  2968. delete List;
  2969. }
  2970. }
  2971. //---------------------------------------------------------------------------
  2972. void __fastcall TCustomScpExplorerForm::ExecuteRemoteFile(
  2973. const UnicodeString & FullFileName, TRemoteFile * File, TExecuteFileBy ExecuteFileBy)
  2974. {
  2975. // needed for checking filemasks, as there's no directory object
  2976. // associated with the file object
  2977. File->FullFileName = FullFileName;
  2978. TFileMasks::TParams MaskParams;
  2979. MaskParams.Size = File->Size;
  2980. MaskParams.Modification = File->Modification;
  2981. ExecuteFile(osRemote, ExecuteFileBy, NULL, FullFileName, File, MaskParams);
  2982. }
  2983. //---------------------------------------------------------------------------
  2984. void __fastcall TCustomScpExplorerForm::EditNew(TOperationSide Side)
  2985. {
  2986. DebugAssert(!WinConfiguration->DisableOpenEdit);
  2987. Side = GetSide(Side);
  2988. TCustomDirView * DView = DirView(Side);
  2989. TListItem * FocusedItem = DView->ItemFocused;
  2990. UnicodeString Name;
  2991. if ((FocusedItem != NULL) && !DView->ItemIsDirectory(FocusedItem))
  2992. {
  2993. Name = DView->ItemFileName(FocusedItem);
  2994. }
  2995. else
  2996. {
  2997. Name = LoadStr(NEW_FILE);
  2998. }
  2999. UnicodeString Names = Name;
  3000. std::unique_ptr<TStrings> History(CloneStrings(CustomWinConfiguration->History[L"EditFile"]));
  3001. if (InputDialog(LoadStr(EDIT_FILE_CAPTION), LoadStr(EDIT_FILE_PROMPT), Names,
  3002. HELP_EDIT_NEW, History.get(), true, NULL, true, 400))
  3003. {
  3004. while (!Names.IsEmpty())
  3005. {
  3006. Name = CutToChar(Names, FileMasksDelimiters[1], false);
  3007. CustomWinConfiguration->History[L"EditFile"] = History.get();
  3008. UnicodeString TargetFileName;
  3009. UnicodeString LocalFileName;
  3010. UnicodeString RootTempDir;
  3011. UnicodeString TempDir;
  3012. UnicodeString RemoteDirectory;
  3013. bool ExistingFile = false;
  3014. if (!IsSideLocalBrowser(Side))
  3015. {
  3016. Name = AbsolutePath(Terminal->CurrentDirectory, Name);
  3017. std::unique_ptr<TRemoteFile> File(Terminal->TryReadFile(Name));
  3018. if ((File.get() != NULL) &&
  3019. !File->IsDirectory)
  3020. {
  3021. ExecuteRemoteFile(Name, File.get(), efDefaultEditor);
  3022. ExistingFile = true;
  3023. }
  3024. File.reset(NULL);
  3025. if (!ExistingFile)
  3026. {
  3027. RemoteDirectory = UnixExtractFilePath(Name);
  3028. TemporaryDirectoryForRemoteFiles(
  3029. RemoteDirectory, GUIConfiguration->CurrentCopyParam, false, TempDir, RootTempDir);
  3030. TargetFileName = UnixExtractFileName(Name);
  3031. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  3032. LocalFileName = TempDir +
  3033. // We probably do not want to trim the VMS version here
  3034. Terminal->ChangeFileName(&CopyParam, TargetFileName, osRemote, false);
  3035. }
  3036. }
  3037. else
  3038. {
  3039. if (ExtractFilePath(Name).IsEmpty())
  3040. {
  3041. LocalFileName = IncludeTrailingBackslash(DView->PathName) + Name;
  3042. }
  3043. else
  3044. {
  3045. LocalFileName = ExpandFileName(Name);
  3046. }
  3047. TargetFileName = ExtractFileName(Name);
  3048. }
  3049. if (!ExistingFile)
  3050. {
  3051. bool NewFile = !FileExists(ApiPath(LocalFileName));
  3052. if (NewFile)
  3053. {
  3054. int File = FileCreate(ApiPath(LocalFileName));
  3055. if (File < 0)
  3056. {
  3057. if (!RootTempDir.IsEmpty())
  3058. {
  3059. RecursiveDeleteFile(ExcludeTrailingBackslash(RootTempDir), false);
  3060. }
  3061. throw Exception(FMTLOAD(CREATE_FILE_ERROR, (LocalFileName)));
  3062. }
  3063. else
  3064. {
  3065. FileClose(File);
  3066. }
  3067. }
  3068. TExecuteFileBy ExecuteFileBy = efDefaultEditor;
  3069. const TEditorData * ExternalEditor = NULL;
  3070. TFileMasks::TParams MaskParams; // size not known
  3071. ExecuteFileNormalize(ExecuteFileBy, ExternalEditor, TargetFileName,
  3072. false, MaskParams);
  3073. CustomExecuteFile(Side, ExecuteFileBy, LocalFileName, TargetFileName,
  3074. ExternalEditor, RootTempDir, RemoteDirectory, NewFile, TDateTime());
  3075. }
  3076. }
  3077. }
  3078. }
  3079. //---------------------------------------------------------------------------
  3080. bool __fastcall TCustomScpExplorerForm::RemoteExecuteForceText(
  3081. TExecuteFileBy ExecuteFileBy, const TEditorData * ExternalEditor)
  3082. {
  3083. DebugAssert((ExecuteFileBy == efExternalEditor) ==
  3084. ((ExternalEditor != NULL) && (ExternalEditor->Editor == edExternal)));
  3085. DebugAssert(ExecuteFileBy != efDefaultEditor);
  3086. return
  3087. ((ExecuteFileBy == efInternalEditor)) ||
  3088. ((ExecuteFileBy == efExternalEditor) && ExternalEditor->ExternalEditorText);
  3089. }
  3090. //---------------------------------------------------------------------------
  3091. void __fastcall TCustomScpExplorerForm::CustomExecuteFile(TOperationSide Side,
  3092. TExecuteFileBy ExecuteFileBy, UnicodeString FileName, UnicodeString OriginalFileName,
  3093. const TEditorData * ExternalEditor, UnicodeString LocalRootDirectory,
  3094. UnicodeString RemoteDirectory, bool NewFile, const TDateTime & SourceTimestamp)
  3095. {
  3096. DebugAssert(!WinConfiguration->DisableOpenEdit);
  3097. DebugAssert((ExecuteFileBy == efExternalEditor) ==
  3098. ((ExternalEditor != NULL) && (ExternalEditor->Editor == edExternal)));
  3099. DebugAssert(ExecuteFileBy != efDefaultEditor);
  3100. Side = GetSide(Side);
  3101. std::unique_ptr<TEditedFileData> Data(new TEditedFileData);
  3102. if (!IsSideLocalBrowser(Side))
  3103. {
  3104. Data->Terminal = Terminal;
  3105. Data->Queue = Queue;
  3106. Data->SessionData = CloneCurrentSessionData();
  3107. Data->ForceText = RemoteExecuteForceText(ExecuteFileBy, ExternalEditor);
  3108. Data->RemoteDirectory = RemoteDirectory;
  3109. Data->SessionName = Terminal->SessionData->SessionName;
  3110. Data->LocalRootDirectory = LocalRootDirectory;
  3111. Data->OriginalFileName = OriginalFileName;
  3112. Data->Command = L""; // will be changed later for external editor
  3113. // Empty SourceTimestamp indicates that checking was disabled when the file was opened
  3114. if (WinConfiguration->EditorCheckNotModified)
  3115. {
  3116. Data->SourceTimestamp = SourceTimestamp;
  3117. }
  3118. }
  3119. if (ExecuteFileBy == efInternalEditor)
  3120. {
  3121. if (!IsSideLocalBrowser(Side))
  3122. {
  3123. UnicodeString RemotePath = UnixCombinePaths(RemoteDirectory, OriginalFileName);
  3124. UnicodeString Caption = RemotePath + TitleSeparator + Terminal->SessionData->SessionName;
  3125. TForm * Editor;
  3126. try
  3127. {
  3128. Editor =
  3129. ShowEditorForm(
  3130. FileName, this,
  3131. FEditorManager->FileChanged, FEditorManager->FileReload, FEditorManager->FileClosed,
  3132. SaveAllInternalEditors, AnyInternalEditorModified,
  3133. Caption, StandaloneOperation, SessionColor, Terminal->SessionData->InternalEditorEncoding, NewFile);
  3134. }
  3135. catch(...)
  3136. {
  3137. if (!LocalRootDirectory.IsEmpty())
  3138. {
  3139. RecursiveDeleteFile(ExcludeTrailingBackslash(LocalRootDirectory), false);
  3140. }
  3141. throw;
  3142. }
  3143. FEditorManager->AddFileInternal(FileName, Data.release(), Editor);
  3144. AppLogFmt(L"Opened internal editor with \"%s\"", (FileName));
  3145. }
  3146. else
  3147. {
  3148. DebugAssert(!StandaloneOperation);
  3149. TForm * Editor =
  3150. ShowEditorForm(FileName, this, NULL, NULL, LocalEditorClosed,
  3151. SaveAllInternalEditors, AnyInternalEditorModified,
  3152. L"", false, SessionColor, -1, NewFile);
  3153. FLocalEditors->Add(Editor);
  3154. }
  3155. }
  3156. else
  3157. {
  3158. HANDLE Process;
  3159. if (ExecuteFileBy == efExternalEditor)
  3160. {
  3161. UnicodeString Program, Params, Dir;
  3162. Data->Command = ExternalEditor->ExternalEditor;
  3163. ReformatFileNameCommand(Data->Command);
  3164. SplitCommand(Data->Command, Program, Params, Dir);
  3165. Params = ExpandFileNameCommand(Params, FileName);
  3166. Program = ExpandEnvironmentVariables(Program);
  3167. if (!ExecuteShell(Program, Params, Process))
  3168. {
  3169. throw EOSExtException(FMTLOAD(EDITOR_ERROR, (Program)));
  3170. }
  3171. }
  3172. else
  3173. {
  3174. DebugAssert(!IsSideLocalBrowser(Side));
  3175. if (!ExecuteShell(FileName, L"", Process))
  3176. {
  3177. throw EOSExtException(FMTLOAD(EXECUTE_FILE_ERROR, (FileName)));
  3178. }
  3179. }
  3180. if (IsSideLocalBrowser(Side) ||
  3181. ((ExecuteFileBy == efShell) &&
  3182. !WinConfiguration->Editor.SDIShellEditor) ||
  3183. ((ExecuteFileBy == efExternalEditor) &&
  3184. !ExternalEditor->SDIExternalEditor))
  3185. {
  3186. // no need for handle
  3187. if (Process != NULL)
  3188. {
  3189. DebugCheck(CloseHandle(Process));
  3190. }
  3191. Process = INVALID_HANDLE_VALUE;
  3192. }
  3193. else
  3194. {
  3195. if (Process == NULL)
  3196. {
  3197. throw ExtException(LoadStr(OPEN_FILE_NO_PROCESS2), L"", HELP_OPEN_FILE_NO_PROCESS);
  3198. }
  3199. }
  3200. if (!IsSideLocalBrowser(Side))
  3201. {
  3202. FEditorManager->AddFileExternal(FileName, Data.release(), Process);
  3203. AppLogFmt(L"Opened external editor with remote file \"%s\".", (FileName));
  3204. }
  3205. }
  3206. }
  3207. //---------------------------------------------------------------------------
  3208. void __fastcall TCustomScpExplorerForm::SaveInternalEditor(
  3209. const UnicodeString /*FileName*/, TEditedFileData * /*Data*/, TObject * Token,
  3210. void * /*Arg*/)
  3211. {
  3212. if (Token != NULL)
  3213. {
  3214. EditorFormFileSave(static_cast<TForm *>(Token));
  3215. }
  3216. }
  3217. //---------------------------------------------------------------------------
  3218. void __fastcall TCustomScpExplorerForm::SaveAllInternalEditors(TObject * /*Sender*/)
  3219. {
  3220. for (int Index = 0; Index < FLocalEditors->Count; Index++)
  3221. {
  3222. EditorFormFileSave(static_cast<TForm *>(FLocalEditors->Items[Index]));
  3223. }
  3224. FEditorManager->ProcessFiles(SaveInternalEditor, NULL);
  3225. }
  3226. //---------------------------------------------------------------------------
  3227. void __fastcall TCustomScpExplorerForm::InternalEditorModified(
  3228. const UnicodeString /*FileName*/, TEditedFileData * /*Data*/, TObject * Token,
  3229. void * Arg)
  3230. {
  3231. if ((Token != NULL) &&
  3232. IsEditorFormModified(static_cast<TForm *>(Token)))
  3233. {
  3234. *static_cast<bool *>(Arg) = true;
  3235. }
  3236. }
  3237. //---------------------------------------------------------------------------
  3238. void __fastcall TCustomScpExplorerForm::AnyInternalEditorModified(
  3239. TObject * /*Sender*/, bool & Modified)
  3240. {
  3241. for (int Index = 0; !Modified && (Index < FLocalEditors->Count); Index++)
  3242. {
  3243. if (IsEditorFormModified(static_cast<TForm *>(FLocalEditors->Items[Index])))
  3244. {
  3245. Modified = true;
  3246. }
  3247. }
  3248. if (!Modified)
  3249. {
  3250. FEditorManager->ProcessFiles(InternalEditorModified, &Modified);
  3251. }
  3252. }
  3253. //---------------------------------------------------------------------------
  3254. void __fastcall TCustomScpExplorerForm::LocalEditorClosed(TObject * Sender, bool /*Forced*/)
  3255. {
  3256. DebugCheck(FLocalEditors->Extract(Sender) >= 0);
  3257. }
  3258. //---------------------------------------------------------------------------
  3259. void __fastcall TCustomScpExplorerForm::TemporaryDirectoryForRemoteFiles(
  3260. const UnicodeString & RemoteDirectory, const TCopyParamType & CopyParam, bool Simple,
  3261. UnicodeString & Result, UnicodeString & RootDirectory)
  3262. {
  3263. if (!WinConfiguration->TemporaryDirectoryDeterministic || Simple)
  3264. {
  3265. RootDirectory = IncludeTrailingBackslash(WinConfiguration->TemporaryDir());
  3266. Result = RootDirectory;
  3267. }
  3268. else
  3269. {
  3270. RootDirectory = L"";
  3271. Result = WinConfiguration->ExpandedTemporaryDirectory();
  3272. Result = IncludeTrailingBackslash(Result);
  3273. }
  3274. if (WinConfiguration->TemporaryDirectoryAppendSession && !Simple)
  3275. {
  3276. Result = IncludeTrailingBackslash(Result + MakeValidFileName(Terminal->SessionData->SessionName));
  3277. }
  3278. if (WinConfiguration->TemporaryDirectoryAppendPath && !Simple)
  3279. {
  3280. UnicodeString ARemoteDirectory = RemoteDirectory;
  3281. if (!ARemoteDirectory.IsEmpty() && (ARemoteDirectory[1] == L'/'))
  3282. {
  3283. ARemoteDirectory.Delete(1, 1);
  3284. }
  3285. Result = IncludeTrailingBackslash(Result + CopyParam.ValidLocalPath(FromUnixPath(ARemoteDirectory)));
  3286. }
  3287. if (!ForceDirectories(ApiPath(Result)))
  3288. {
  3289. throw EOSExtException(FMTLOAD(CREATE_TEMP_DIR_ERROR, (Result)));
  3290. }
  3291. }
  3292. //---------------------------------------------------------------------------
  3293. void __fastcall TCustomScpExplorerForm::TemporarilyDownloadFiles(
  3294. TStrings * FileList, bool ForceText, UnicodeString & RootTempDir, UnicodeString & TempDir,
  3295. bool GetTargetNames, bool AutoOperation, bool SimpleTempDir)
  3296. {
  3297. DebugAssert(!IsLocalBrowserMode());
  3298. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  3299. if (ForceText)
  3300. {
  3301. CopyParam.TransferMode = tmAscii;
  3302. }
  3303. TemporaryFileCopyParam(CopyParam);
  3304. if (TempDir.IsEmpty())
  3305. {
  3306. TemporaryDirectoryForRemoteFiles(Terminal->CurrentDirectory, CopyParam, SimpleTempDir, TempDir, RootTempDir);
  3307. }
  3308. DebugAssert(!FAutoOperation);
  3309. FAutoOperation = AutoOperation;
  3310. Terminal->ExceptionOnFail = true;
  3311. try
  3312. {
  3313. try
  3314. {
  3315. // turn off confirmations, as for MDI editors we may possibly download
  3316. // the same file over
  3317. Terminal->CopyToLocal(
  3318. FileList, TempDir, &CopyParam, cpNoConfirmation | cpTemporary, NULL);
  3319. if (GetTargetNames)
  3320. {
  3321. for (int i = 0; i < FileList->Count; i++)
  3322. {
  3323. FileList->Strings[i] =
  3324. Terminal->ChangeFileName(&CopyParam, UnixExtractFileName(FileList->Strings[i]), osRemote, false);
  3325. }
  3326. }
  3327. }
  3328. catch(...)
  3329. {
  3330. if (!RootTempDir.IsEmpty())
  3331. {
  3332. RecursiveDeleteFile(ExcludeTrailingBackslash(RootTempDir), false);
  3333. }
  3334. throw;
  3335. }
  3336. }
  3337. __finally
  3338. {
  3339. FAutoOperation = false;
  3340. Terminal->ExceptionOnFail = false;
  3341. }
  3342. }
  3343. //---------------------------------------------------------------------------
  3344. void __fastcall TCustomScpExplorerForm::EditorAutoConfig()
  3345. {
  3346. // Do not waste time checking for default editor list the next time,
  3347. // or testing if default editor is not notepad.
  3348. bool TryNextTime = false;
  3349. UnicodeString UsageState;
  3350. if (!WinConfiguration->EditorList->IsDefaultList())
  3351. {
  3352. UsageState = "H";
  3353. }
  3354. else
  3355. {
  3356. UnicodeString Executable;
  3357. UnicodeString ExecutableDescription;
  3358. if (DetectSystemExternalEditor(false, Executable, ExecutableDescription, UsageState, TryNextTime))
  3359. {
  3360. UnicodeString Message =
  3361. FMTLOAD(EDITOR_AUTO_CONFIG2, (ExecutableDescription, ExecutableDescription));
  3362. unsigned int Answer =
  3363. MessageDialog(Message, qtConfirmation, qaOK | qaCancel, HELP_EDITOR_AUTO_CONFIG);
  3364. if (Answer != qaOK)
  3365. {
  3366. UsageState = "R";
  3367. }
  3368. else
  3369. {
  3370. UsageState = "A";
  3371. TEditorData EditorData;
  3372. EditorData.Editor = edExternal;
  3373. EditorData.ExternalEditor = FormatCommand(Executable, L"");
  3374. EditorData.ExternalEditorOptionsAutodetect();
  3375. TEditorList EditorList;
  3376. EditorList = *WinConfiguration->EditorList;
  3377. EditorList.Insert(0, new TEditorPreferences(EditorData));
  3378. WinConfiguration->EditorList = &EditorList;
  3379. }
  3380. }
  3381. }
  3382. WinConfiguration->OfferedEditorAutoConfig = !TryNextTime;
  3383. WinConfiguration->Usage->Set(L"EditorAutoConfig", UsageState);
  3384. }
  3385. //---------------------------------------------------------------------------
  3386. void __fastcall TCustomScpExplorerForm::ExecuteFileNormalize(
  3387. TExecuteFileBy & ExecuteFileBy, const TEditorData *& ExternalEditor,
  3388. const UnicodeString & FileName, bool Local, const TFileMasks::TParams & MaskParams)
  3389. {
  3390. if (ExecuteFileBy == efDefaultEditor)
  3391. {
  3392. if (!WinConfiguration->OfferedEditorAutoConfig)
  3393. {
  3394. EditorAutoConfig();
  3395. }
  3396. const TEditorPreferences * Editor =
  3397. WinConfiguration->DefaultEditorForFile(FileName, Local, MaskParams);
  3398. if ((Editor == NULL) || (Editor->Data->Editor == edInternal))
  3399. {
  3400. ExecuteFileBy = efInternalEditor;
  3401. ExternalEditor = NULL;
  3402. }
  3403. else if (Editor->Data->Editor == edOpen)
  3404. {
  3405. ExecuteFileBy = efShell;
  3406. ExternalEditor = NULL;
  3407. }
  3408. else
  3409. {
  3410. ExecuteFileBy = efExternalEditor;
  3411. ExternalEditor = Editor->Data;
  3412. }
  3413. }
  3414. }
  3415. //---------------------------------------------------------------------------
  3416. void __fastcall TCustomScpExplorerForm::ExecuteFile(TOperationSide Side,
  3417. TExecuteFileBy ExecuteFileBy, const TEditorData * ExternalEditor,
  3418. UnicodeString FullFileName, TObject * Object, const TFileMasks::TParams & MaskParams)
  3419. {
  3420. UnicodeString OriginalFileName;
  3421. UnicodeString LocalRootDirectory;
  3422. UnicodeString RemoteDirectory;
  3423. ExecuteFileNormalize(ExecuteFileBy, ExternalEditor, FullFileName,
  3424. IsSideLocalBrowser(Side), MaskParams);
  3425. UnicodeString Counter;
  3426. UnicodeString LocalFileName;
  3427. bool Handled = false;
  3428. if (!IsSideLocalBrowser(Side))
  3429. {
  3430. // We need to trim VMS version here, so that we use name without version
  3431. // when uploading back to create a new version of the file
  3432. OriginalFileName = Terminal->GetBaseFileName(UnixExtractFileName(FullFileName));
  3433. RemoteDirectory = UnixExtractFilePath(FullFileName);
  3434. TObject * Token = NULL;
  3435. UnicodeString LocalDirectory;
  3436. if (!FEditorManager->CanAddFile(RemoteDirectory, OriginalFileName,
  3437. Terminal->SessionData->SessionName, Token, LocalRootDirectory,
  3438. LocalDirectory))
  3439. {
  3440. if (Token != NULL)
  3441. {
  3442. TForm * Form = dynamic_cast<TForm *>(Token);
  3443. if (Form->WindowState == wsMinimized)
  3444. {
  3445. ShowWindow(Form->Handle, SW_RESTORE);
  3446. }
  3447. else
  3448. {
  3449. Form->SetFocus();
  3450. }
  3451. Handled = true;
  3452. }
  3453. else
  3454. {
  3455. throw Exception(FMTLOAD(ALREADY_EDITED_EXTERNALLY_OR_UPLOADED, (OriginalFileName)));
  3456. }
  3457. }
  3458. if (!Handled)
  3459. {
  3460. TStringList * FileList1 = new TStringList();
  3461. try
  3462. {
  3463. FileList1->AddObject(FullFileName, Object);
  3464. TemporarilyDownloadFiles(FileList1,
  3465. RemoteExecuteForceText(ExecuteFileBy, ExternalEditor),
  3466. LocalRootDirectory, LocalDirectory, true, true, false);
  3467. LocalFileName = LocalDirectory + FileList1->Strings[0];
  3468. }
  3469. __finally
  3470. {
  3471. delete FileList1;
  3472. }
  3473. switch (ExecuteFileBy)
  3474. {
  3475. case efShell:
  3476. Counter = "RemoteFilesExecuted";
  3477. break;
  3478. case efInternalEditor:
  3479. Counter = "RemoteFilesOpenedInInternalEditor";
  3480. break;
  3481. case efExternalEditor:
  3482. Counter = "RemoteFilesOpenedInExternalEditor";
  3483. break;
  3484. default:
  3485. DebugFail();
  3486. }
  3487. }
  3488. }
  3489. else
  3490. {
  3491. LocalFileName = FullFileName;
  3492. OriginalFileName = ExtractFileName(FullFileName);
  3493. switch (ExecuteFileBy)
  3494. {
  3495. case efShell:
  3496. Counter = "LocalFilesExecuted";
  3497. break;
  3498. case efInternalEditor:
  3499. Counter = "LocalFilesOpenedInInternalEditor";
  3500. break;
  3501. case efExternalEditor:
  3502. Counter = "LocalFilesOpenedInExternalEditor";
  3503. break;
  3504. default:
  3505. DebugFail();
  3506. }
  3507. }
  3508. if (!Handled)
  3509. {
  3510. Configuration->Usage->Inc(Counter);
  3511. CustomExecuteFile(Side, ExecuteFileBy, LocalFileName, OriginalFileName,
  3512. ExternalEditor, LocalRootDirectory, RemoteDirectory, false, MaskParams.Modification);
  3513. }
  3514. }
  3515. //---------------------------------------------------------------------------
  3516. void __fastcall TCustomScpExplorerForm::ExecuteFile(TOperationSide Side,
  3517. TExecuteFileBy ExecuteFileBy, const TEditorData * ExternalEditor,
  3518. bool AllSelected, bool OnFocused)
  3519. {
  3520. DebugAssert(!WinConfiguration->DisableOpenEdit);
  3521. DebugAssert((ExecuteFileBy == efExternalEditor) ==
  3522. ((ExternalEditor != NULL) && (ExternalEditor->Editor == edExternal)));
  3523. Side = GetSide(Side);
  3524. TCustomDirView * DView = DirView(Side);
  3525. TStrings * FileList =
  3526. AllSelected ?
  3527. DView->CreateFileList(OnFocused, IsSideLocalBrowser(Side)) :
  3528. DView->CreateFocusedFileList(IsSideLocalBrowser(Side));
  3529. try
  3530. {
  3531. DebugAssert(AllSelected || (FileList->Count == 1));
  3532. for (int i = 0; i < FileList->Count; i++)
  3533. {
  3534. UnicodeString ListFileName = FileList->Strings[i];
  3535. UnicodeString FileNameOnly =
  3536. !IsSideLocalBrowser(Side) ? UnixExtractFileName(ListFileName) : ExtractFileName(ListFileName);
  3537. TListItem * Item = DView->FindFileItem(FileNameOnly);
  3538. if (!DView->ItemIsDirectory(Item))
  3539. {
  3540. UnicodeString FullFileName;
  3541. if (!IsSideLocalBrowser(Side))
  3542. {
  3543. FullFileName = DirView(Side)->Path + ListFileName;
  3544. }
  3545. else
  3546. {
  3547. FullFileName = ListFileName;
  3548. }
  3549. TObject * Object = FileList->Objects[i];
  3550. TFileMasks::TParams MaskParams;
  3551. MaskParams.Size = DView->ItemFileSize(Item);
  3552. TDateTimePrecision Precision;
  3553. MaskParams.Modification = DView->ItemFileTime(Item, Precision);
  3554. std::unique_ptr<TRemoteFile> File;
  3555. if (!IsSideLocalBrowser(Side))
  3556. {
  3557. File.reset(Terminal->TryReadFile(FullFileName));
  3558. if (File.get() != NULL)
  3559. {
  3560. File->FullFileName = FullFileName;
  3561. Object = File.get();
  3562. MaskParams.Size = File->Size;
  3563. MaskParams.Modification = File->Modification;
  3564. }
  3565. }
  3566. ExecuteFile(Side, ExecuteFileBy, ExternalEditor, FullFileName, Object, MaskParams);
  3567. }
  3568. }
  3569. }
  3570. __finally
  3571. {
  3572. delete FileList;
  3573. }
  3574. }
  3575. //---------------------------------------------------------------------------
  3576. void __fastcall TCustomScpExplorerForm::TemporaryFileCopyParam(TCopyParamType & CopyParam)
  3577. {
  3578. CopyParam.FileNameCase = ncNoChange;
  3579. CopyParam.PreserveRights = false;
  3580. CopyParam.PreserveReadOnly = false;
  3581. CopyParam.ReplaceInvalidChars = true;
  3582. CopyParam.IncludeFileMask = TFileMasks();
  3583. CopyParam.NewerOnly = false;
  3584. CopyParam.FileMask = L"";
  3585. }
  3586. //---------------------------------------------------------------------------
  3587. bool TCustomScpExplorerForm::EditorCheckNotModified(const TEditedFileData * Data)
  3588. {
  3589. return WinConfiguration->EditorCheckNotModified && (Data->SourceTimestamp != TDateTime());
  3590. }
  3591. //---------------------------------------------------------------------------
  3592. void __fastcall TCustomScpExplorerForm::ExecutedFileChanged(
  3593. const UnicodeString & FileName, TEditedFileData * Data, HANDLE UploadCompleteEvent, bool & Retry)
  3594. {
  3595. TTerminalManager * Manager = TTerminalManager::Instance();
  3596. if (!IsActiveTerminal(Data->Terminal))
  3597. {
  3598. if (!NonVisualDataModule->Busy)
  3599. {
  3600. UnicodeString FileNameOnly = ExtractFileName(FileName);
  3601. UnicodeString EditInactiveSessionReopenAcceptedCounter = L"EditInactiveSessionReopenAccepted";
  3602. UnicodeString EditInactiveSessionReopenRejectedCounter = L"EditInactiveSessionReopenRejected";
  3603. if (Data->Terminal == NULL)
  3604. {
  3605. TManagedTerminal * SameSiteTerminal = Manager->FindActiveTerminalForSite(Data->SessionData);
  3606. if (SameSiteTerminal != NULL)
  3607. {
  3608. UnicodeString Message =
  3609. FMTLOAD(EDIT_SESSION_REATTACH,
  3610. (FileNameOnly, Data->SessionName, FileNameOnly));
  3611. if (MessageDialog(Message, qtConfirmation, qaOK | qaCancel) == qaOK)
  3612. {
  3613. Data->Terminal = SameSiteTerminal;
  3614. Data->Queue = Manager->FindQueueForTerminal(SameSiteTerminal);
  3615. Data->SessionName = SameSiteTerminal->SessionData->SessionName;
  3616. // We might also overwrite session data
  3617. Configuration->Usage->Inc(EditInactiveSessionReopenAcceptedCounter);
  3618. }
  3619. else
  3620. {
  3621. Configuration->Usage->Inc(EditInactiveSessionReopenRejectedCounter);
  3622. Abort();
  3623. }
  3624. }
  3625. }
  3626. // foreground session should reconnect itself
  3627. else if (Terminal != Data->Terminal)
  3628. {
  3629. UnicodeString Message =
  3630. MainInstructions(
  3631. FMTLOAD(EDIT_SESSION_RECONNECT, (Data->SessionName, FileNameOnly)));
  3632. if (MessageDialog(Message, qtConfirmation, qaOK | qaCancel) == qaOK)
  3633. {
  3634. Manager->SetActiveTerminalWithAutoReconnect(Data->Terminal);
  3635. Configuration->Usage->Inc(EditInactiveSessionReopenAcceptedCounter);
  3636. }
  3637. else
  3638. {
  3639. Configuration->Usage->Inc(EditInactiveSessionReopenRejectedCounter);
  3640. Abort();
  3641. }
  3642. }
  3643. }
  3644. if (!IsActiveTerminal(Data->Terminal))
  3645. {
  3646. Configuration->Usage->Inc(L"EditInactiveSession");
  3647. // Prevent this when not idle (!NonVisualDataModule->Busy)?
  3648. throw Exception(FMTLOAD(EDIT_SESSION_CLOSED2,
  3649. (ExtractFileName(FileName), Data->SessionName)));
  3650. }
  3651. }
  3652. if (EditorCheckNotModified(Data))
  3653. {
  3654. UnicodeString RemoteFilePath = UnixCombinePaths(Data->RemoteDirectory, Data->OriginalFileName);
  3655. std::unique_ptr<TRemoteFile> File(Terminal->TryReadFile(RemoteFilePath));
  3656. if (File.get() != NULL)
  3657. {
  3658. AppLogFmt(L"Edited remote file timestamp: %s, Original timestamp: %s", (StandardTimestamp(File->Modification), StandardTimestamp(Data->SourceTimestamp)));
  3659. if (File->Modification != Data->SourceTimestamp)
  3660. {
  3661. UnicodeString Message = MainInstructions(LoadStr(EDIT_CHANGED_EXTERNALLY));
  3662. if (MessageDialog(Message, qtConfirmation, qaOK | qaCancel) != qaOK)
  3663. {
  3664. Abort();
  3665. }
  3666. }
  3667. }
  3668. }
  3669. TStrings * FileList = new TStringList();
  3670. try
  3671. {
  3672. FileList->Add(FileName);
  3673. // Consider using the same settings (preset) as when the file was downloaded.
  3674. // More over this does not reflect the actual session that will do the upload.
  3675. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  3676. TemporaryFileCopyParam(CopyParam);
  3677. if (Data->ForceText)
  3678. {
  3679. CopyParam.TransferMode = tmAscii;
  3680. }
  3681. // so i do not need to worry if masking algorithm works in all cases
  3682. // ("" means "copy file name", no masking is actually done)
  3683. if (ExtractFileName(FileName) == Data->OriginalFileName)
  3684. {
  3685. CopyParam.FileMask = L"";
  3686. }
  3687. else
  3688. {
  3689. CopyParam.FileMask = DelimitFileNameMask(Data->OriginalFileName);
  3690. }
  3691. int Params = cpNoConfirmation | cpTemporary;
  3692. if (Data->Terminal->IsCapable[fcBackgroundTransfers])
  3693. {
  3694. DebugAssert(Data->Queue != NULL);
  3695. TQueueItem * QueueItem = new TUploadQueueItem(Data->Terminal, FileList,
  3696. Data->RemoteDirectory, &CopyParam, Params, true, false);
  3697. QueueItem->CompleteEvent = UploadCompleteEvent;
  3698. AddQueueItem(Data->Queue, QueueItem, Data->Terminal);
  3699. }
  3700. else
  3701. {
  3702. if (NonVisualDataModule->Busy)
  3703. {
  3704. Retry = true;
  3705. }
  3706. else
  3707. {
  3708. Data->Terminal->CopyToRemote(FileList, Data->RemoteDirectory, &CopyParam, Params, NULL);
  3709. SetEvent(UploadCompleteEvent);
  3710. }
  3711. }
  3712. }
  3713. __finally
  3714. {
  3715. delete FileList;
  3716. }
  3717. }
  3718. //---------------------------------------------------------------------------
  3719. void __fastcall TCustomScpExplorerForm::ExecutedFileReload(
  3720. const UnicodeString & FileName, TEditedFileData * Data)
  3721. {
  3722. // Sanity check, we should not be busy otherwise user would not be able to click Reload button.
  3723. DebugAssert(!NonVisualDataModule->Busy);
  3724. if (!IsActiveTerminal(Data->Terminal))
  3725. {
  3726. throw Exception(FMTLOAD(EDIT_SESSION_CLOSED_RELOAD,
  3727. (ExtractFileName(FileName), Data->SessionName)));
  3728. }
  3729. TManagedTerminal * PrevSession = TTerminalManager::Instance()->ActiveSession;
  3730. TTerminalManager::Instance()->ActiveSession = Data->Terminal;
  3731. NonVisualDataModule->StartBusy();
  3732. try
  3733. {
  3734. std::unique_ptr<TRemoteFile> File;
  3735. UnicodeString RemoteFileName =
  3736. UnixIncludeTrailingBackslash(Data->RemoteDirectory) + Data->OriginalFileName;
  3737. Terminal->ExceptionOnFail = true;
  3738. try
  3739. {
  3740. File.reset(Terminal->ReadFile(RemoteFileName));
  3741. if (!File->HaveFullFileName)
  3742. {
  3743. File->FullFileName = RemoteFileName;
  3744. }
  3745. }
  3746. __finally
  3747. {
  3748. Terminal->ExceptionOnFail = false;
  3749. }
  3750. std::unique_ptr<TStrings> FileList(new TStringList());
  3751. FileList->AddObject(RemoteFileName, File.get());
  3752. UnicodeString RootTempDir = Data->LocalRootDirectory;
  3753. UnicodeString TempDir = ExtractFilePath(FileName);
  3754. TemporarilyDownloadFiles(FileList.get(), Data->ForceText, RootTempDir, TempDir, true, true, false);
  3755. if (EditorCheckNotModified(Data))
  3756. {
  3757. Data->SourceTimestamp = File->Modification;
  3758. }
  3759. // sanity check, the target file name should be still the same
  3760. DebugAssert(ExtractFileName(FileName) == FileList->Strings[0]);
  3761. }
  3762. __finally
  3763. {
  3764. NonVisualDataModule->EndBusy();
  3765. // it actually may not exist anymore...
  3766. TTerminalManager::Instance()->ActiveSession = PrevSession;
  3767. }
  3768. }
  3769. //---------------------------------------------------------------------------
  3770. void __fastcall TCustomScpExplorerForm::ExecutedFileEarlyClosed(
  3771. const TEditedFileData * Data, bool & KeepOpen)
  3772. {
  3773. // Command is set for external editors only (not for "shell" open).
  3774. if (!Data->Command.IsEmpty())
  3775. {
  3776. bool AnyFound = false;
  3777. bool AnyMDI = false;
  3778. bool AnyNonMDI = false;
  3779. bool AnyDetect = false;
  3780. TEditorList * EditorList = new TEditorList();
  3781. try
  3782. {
  3783. *EditorList = *WinConfiguration->EditorList;
  3784. for (int i = 0; i < EditorList->Count; i++)
  3785. {
  3786. const TEditorPreferences * Editor = EditorList->Editors[i];
  3787. if ((Editor->Data->Editor == edExternal) &&
  3788. (Editor->Data->ExternalEditor == Data->Command))
  3789. {
  3790. AnyFound = true;
  3791. if (Editor->Data->SDIExternalEditor)
  3792. {
  3793. AnyNonMDI = true;
  3794. if (Editor->Data->DetectMDIExternalEditor)
  3795. {
  3796. AnyDetect = true;
  3797. }
  3798. }
  3799. else
  3800. {
  3801. AnyMDI = true;
  3802. }
  3803. }
  3804. }
  3805. bool EnableMDI = false;
  3806. bool DisableDetect = false;
  3807. if (AnyMDI)
  3808. {
  3809. KeepOpen = true;
  3810. if (AnyNonMDI)
  3811. {
  3812. // there is at least one instance of the editor with MDI support enabled,
  3813. // and one with disabled, enable it for all instances
  3814. EnableMDI = true;
  3815. }
  3816. }
  3817. else if (AnyFound && !AnyDetect)
  3818. {
  3819. // at least once instance found but all have MDI autodetection disabled
  3820. // => close the file (default action)
  3821. }
  3822. else
  3823. {
  3824. // no instance of the editor has MDI support enabled
  3825. TMessageParams Params;
  3826. if (AnyFound)
  3827. {
  3828. // there is at least one instance of the editor
  3829. Params.Params |= mpNeverAskAgainCheck;
  3830. }
  3831. unsigned int Answer = MessageDialog(FMTLOAD(EDITOR_EARLY_CLOSED2, (Data->OriginalFileName)), qtWarning,
  3832. qaYes | qaNo, HELP_EDITOR_EARLY_CLOSED, &Params);
  3833. switch (Answer)
  3834. {
  3835. case qaNeverAskAgain:
  3836. DisableDetect = true;
  3837. break;
  3838. case qaNo:
  3839. EnableMDI = true;
  3840. KeepOpen = true;
  3841. break;
  3842. }
  3843. }
  3844. if (AnyFound && (EnableMDI || DisableDetect))
  3845. {
  3846. bool Changed = false;
  3847. for (int i = 0; i < EditorList->Count; i++)
  3848. {
  3849. const TEditorPreferences * Editor = EditorList->Editors[i];
  3850. if ((Editor->Data->Editor == edExternal) &&
  3851. (Editor->Data->ExternalEditor == Data->Command) &&
  3852. ((EnableMDI && Editor->Data->SDIExternalEditor) ||
  3853. (DisableDetect && Editor->Data->DetectMDIExternalEditor)))
  3854. {
  3855. Changed = true;
  3856. TEditorPreferences * UpdatedEditor = new TEditorPreferences(*Editor);
  3857. if (EnableMDI)
  3858. {
  3859. UpdatedEditor->GetData()->SDIExternalEditor = false;
  3860. }
  3861. if (DisableDetect)
  3862. {
  3863. UpdatedEditor->GetData()->DetectMDIExternalEditor = false;
  3864. }
  3865. EditorList->Change(i, UpdatedEditor);
  3866. }
  3867. }
  3868. if (Changed)
  3869. {
  3870. WinConfiguration->EditorList = EditorList;
  3871. }
  3872. }
  3873. }
  3874. __finally
  3875. {
  3876. delete EditorList;
  3877. }
  3878. }
  3879. else
  3880. {
  3881. // "open" case
  3882. MessageDialog(FMTLOAD(APP_EARLY_CLOSED, (Data->OriginalFileName)), qtWarning,
  3883. qaOK, HELP_APP_EARLY_CLOSED);
  3884. }
  3885. }
  3886. //---------------------------------------------------------------------------
  3887. void __fastcall TCustomScpExplorerForm::ExecutedFileUploadComplete(TEditedFileData * Data, TObject * Sender, bool Failed)
  3888. {
  3889. if (!Failed && EditorCheckNotModified(Data))
  3890. {
  3891. UnicodeString RemoteFilePath = UnixCombinePaths(Data->RemoteDirectory, Data->OriginalFileName);
  3892. std::unique_ptr<TRemoteFile> File(Terminal->TryReadFile(RemoteFilePath));
  3893. if (File.get() != NULL)
  3894. {
  3895. Data->SourceTimestamp = File->Modification;
  3896. AppLogFmt(L"Uploaded edited remote file timestamp: %s: ", (StandardTimestamp(Data->SourceTimestamp)));
  3897. }
  3898. }
  3899. if (Sender != NULL)
  3900. {
  3901. EditorFormFileUploadComplete(DebugNotNull(dynamic_cast<TForm *>(Sender)));
  3902. }
  3903. }
  3904. //---------------------------------------------------------------------------
  3905. void __fastcall TCustomScpExplorerForm::RemoteDirViewEnter(TObject * /*Sender*/)
  3906. {
  3907. SideEnter(osRemote);
  3908. }
  3909. //---------------------------------------------------------------------------
  3910. void __fastcall TCustomScpExplorerForm::RemoteDriveViewEnter(TObject * /*Sender*/)
  3911. {
  3912. MakeNextInTabOrder(RemoteDirPanel, RemoteDrivePanel);
  3913. SideEnter(osRemote);
  3914. }
  3915. //---------------------------------------------------------------------------
  3916. void __fastcall TCustomScpExplorerForm::SideEnter(TOperationSide Side)
  3917. {
  3918. FCurrentSide = Side;
  3919. if (Visible)
  3920. {
  3921. UpdateControls();
  3922. }
  3923. }
  3924. //---------------------------------------------------------------------------
  3925. void __fastcall TCustomScpExplorerForm::FileDeleted(TOperationSide Side, const UnicodeString & FileName, bool Success)
  3926. {
  3927. VisualiseOperationFinished(Side, FileName, Success);
  3928. if (DebugAlwaysTrue(FDeletedFiles != NULL) && Success)
  3929. {
  3930. FDeletedFiles->Add(FileName);
  3931. }
  3932. }
  3933. //---------------------------------------------------------------------------
  3934. void __fastcall TCustomScpExplorerForm::DeleteFiles(TOperationSide Side,
  3935. TStrings * FileList, bool Alternative)
  3936. {
  3937. TCustomDirView * DView = DirView(Side);
  3938. DView->SaveSelection();
  3939. DView->SaveSelectedNames();
  3940. DebugAssert(!FAlternativeDelete);
  3941. FAlternativeDelete = Alternative;
  3942. try
  3943. {
  3944. if (!IsSideLocalBrowser(Side))
  3945. {
  3946. DebugAssert(Terminal != NULL);
  3947. // Clone the file list as it may refer to current directory files, which get destroyed, when the directory is reloaded after the operation
  3948. std::unique_ptr<TStrings> PermanentFileList(TRemoteFileList::CloneStrings(FileList));
  3949. std::unique_ptr<TStringList> DeletedFiles(new TStringList());
  3950. DeletedFiles->CaseSensitive = DebugAlwaysTrue(!IsSideLocalBrowser(Side));
  3951. int Params = FLAGMASK(Alternative, dfAlternative);
  3952. try
  3953. {
  3954. TValueRestorer<TStrings *> DeletedFilesRestorer(FDeletedFiles);
  3955. DebugAssert(FDeletedFiles == NULL);
  3956. FDeletedFiles = DeletedFiles.get();
  3957. TValueRestorer<TFileOperationFinishedEvent> OnFileOperationFinishedRestorer(FOnFileOperationFinished);
  3958. FOnFileOperationFinished = FileDeleted;
  3959. FMoveToQueue = false;
  3960. Terminal->DeleteFiles(FileList, Params);
  3961. // Probably not needed for deleting, just for consistency with transfer code
  3962. if (FMoveToQueue)
  3963. {
  3964. Abort();
  3965. }
  3966. }
  3967. catch (EAbort &)
  3968. {
  3969. if (FMoveToQueue)
  3970. {
  3971. DeletedFiles->Sorted = true;
  3972. for (int Index = 0; Index < PermanentFileList->Count; Index++)
  3973. {
  3974. if (DeletedFiles->IndexOf(PermanentFileList->Strings[Index]) >= 0)
  3975. {
  3976. // We should always be deleting the first item => what can be used to optimize this code, if needed
  3977. DebugAssert(Index == 0);
  3978. PermanentFileList->Delete(Index);
  3979. Index--;
  3980. }
  3981. }
  3982. Configuration->Usage->Inc("MovesToBackgroundDelete");
  3983. TQueueItem * QueueItem = new TDeleteQueueItem(Terminal, PermanentFileList.get(), Params);
  3984. AddQueueItem(Queue, QueueItem, Terminal);
  3985. ClearOperationSelection(osRemote);
  3986. }
  3987. }
  3988. }
  3989. else
  3990. {
  3991. if (IsLocalBrowserMode())
  3992. {
  3993. Configuration->Usage->Inc(L"LocalLocalDeletes");
  3994. }
  3995. TValueRestorer<TOperationSide> ProgressSideRestorer(FProgressSide);
  3996. FProgressSide = FCurrentSide;
  3997. try
  3998. {
  3999. TTerminalManager::Instance()->LocalTerminal->DeleteLocalFiles(FileList, FLAGMASK(Alternative, dfAlternative));
  4000. }
  4001. __finally
  4002. {
  4003. ReloadLocalDirectory();
  4004. }
  4005. }
  4006. FAlternativeDelete = false;
  4007. }
  4008. catch(...)
  4009. {
  4010. FAlternativeDelete = false;
  4011. DView->DiscardSavedSelection();
  4012. throw;
  4013. }
  4014. DView->RestoreSelection();
  4015. }
  4016. //---------------------------------------------------------------------------
  4017. void __fastcall TCustomScpExplorerForm::LockFiles(TStrings * FileList, bool Lock)
  4018. {
  4019. DebugAssert(Terminal);
  4020. DebugAssert(!IsLocalBrowserMode());
  4021. RemoteDirView->SaveSelection();
  4022. RemoteDirView->SaveSelectedNames();
  4023. try
  4024. {
  4025. if (Lock)
  4026. {
  4027. Terminal->LockFiles(FileList);
  4028. }
  4029. else
  4030. {
  4031. Terminal->UnlockFiles(FileList);
  4032. }
  4033. }
  4034. catch(...)
  4035. {
  4036. RemoteDirView->DiscardSavedSelection();
  4037. throw;
  4038. }
  4039. RemoteDirView->RestoreSelection();
  4040. }
  4041. //---------------------------------------------------------------------------
  4042. bool __fastcall TCustomScpExplorerForm::RemoteTransferDialog(TManagedTerminal *& Session,
  4043. TStrings * FileList, UnicodeString & Target, UnicodeString & FileMask, bool & DirectCopy,
  4044. bool NoConfirmation, bool Move)
  4045. {
  4046. DebugAssert(Terminal != NULL);
  4047. DebugAssert(Terminal == TTerminalManager::Instance()->ActiveTerminal);
  4048. DebugAssert(!IsLocalBrowserMode());
  4049. if (Session == NULL)
  4050. {
  4051. Session = Terminal;
  4052. }
  4053. if (Session == Terminal)
  4054. {
  4055. if (RemoteDriveView->DropTarget != NULL)
  4056. {
  4057. Target = RemoteDriveView->NodePathName(RemoteDriveView->DropTarget);
  4058. }
  4059. else if (RemoteDirView->DropTarget != NULL)
  4060. {
  4061. DebugAssert(RemoteDirView->ItemIsDirectory(RemoteDirView->DropTarget));
  4062. Target = RemoteDirView->ItemFullFileName(RemoteDirView->DropTarget);
  4063. }
  4064. else
  4065. {
  4066. Target = RemoteDirView->Path;
  4067. }
  4068. }
  4069. else
  4070. {
  4071. Target = GetSessionPath(Session, osRemote);
  4072. }
  4073. Target = UnixIncludeTrailingBackslash(Target);
  4074. if (FileList->Count == 1)
  4075. {
  4076. FileMask = DelimitFileNameMask(UnixExtractFileName(FileList->Strings[0]));
  4077. }
  4078. else
  4079. {
  4080. FileMask = AnyMask;
  4081. }
  4082. DirectCopy = Terminal->IsCapable[fcRemoteCopy] || Terminal->IsCapable[fcSecondaryShell];
  4083. bool Result = true;
  4084. if (!NoConfirmation)
  4085. {
  4086. bool Multi = (FileList->Count > 1);
  4087. if (Move)
  4088. {
  4089. Result = DoRemoteMoveDialog(Multi, Target, FileMask);
  4090. }
  4091. else
  4092. {
  4093. std::unique_ptr<TStrings> Sessions(new TStringList);
  4094. std::unique_ptr<TStrings> Directories(new TStringList);
  4095. TStrings * SessionList = TTerminalManager::Instance()->SessionList;
  4096. for (int Index = 0; Index < SessionList->Count; Index++)
  4097. {
  4098. TManagedTerminal * ASession = DebugNotNull(dynamic_cast<TManagedTerminal *>(SessionList->Objects[Index]));
  4099. if (IsActiveTerminal(ASession) && DebugAlwaysTrue(!ASession->LocalBrowser))
  4100. {
  4101. Sessions->AddObject(SessionList->Strings[Index], ASession);
  4102. Directories->Add(GetSessionPath(ASession, osRemote));
  4103. }
  4104. }
  4105. TDirectRemoteCopy AllowDirectCopy;
  4106. if (Terminal->IsCapable[fcRemoteCopy] || Terminal->CommandSessionOpened)
  4107. {
  4108. DebugAssert(DirectCopy);
  4109. AllowDirectCopy = drcAllow;
  4110. }
  4111. else if (Terminal->IsCapable[fcSecondaryShell])
  4112. {
  4113. DebugAssert(DirectCopy);
  4114. AllowDirectCopy = drcConfirmCommandSession;
  4115. }
  4116. else
  4117. {
  4118. DebugAssert(!DirectCopy);
  4119. AllowDirectCopy = drcDisallow;
  4120. }
  4121. void * ASession = Session;
  4122. Result = DoRemoteCopyDialog(
  4123. Sessions.get(), Directories.get(), AllowDirectCopy, Multi, ASession, Target, FileMask, DirectCopy, Terminal);
  4124. Session = static_cast<TManagedTerminal *>(ASession);
  4125. }
  4126. }
  4127. return Result;
  4128. }
  4129. //---------------------------------------------------------------------------
  4130. bool __fastcall TCustomScpExplorerForm::RemoteTransferFiles(
  4131. TStrings * FileList, bool NoConfirmation, bool Move, TManagedTerminal * Session)
  4132. {
  4133. DebugAssert(!IsLocalBrowserMode());
  4134. bool DirectCopy;
  4135. UnicodeString Target, FileMask;
  4136. bool Result = RemoteTransferDialog(Session, FileList, Target, FileMask, DirectCopy, NoConfirmation, Move);
  4137. if (Result)
  4138. {
  4139. if (!Move && !DirectCopy)
  4140. {
  4141. Configuration->Usage->Inc("RemoteCopyTemp");
  4142. UnicodeString RootTempDir;
  4143. UnicodeString TempDir;
  4144. TemporarilyDownloadFiles(FileList, false, RootTempDir, TempDir, false, false, true);
  4145. TStrings * TemporaryFilesList = new TStringList();
  4146. try
  4147. {
  4148. TMakeLocalFileListParams MakeFileListParam;
  4149. MakeFileListParam.FileList = TemporaryFilesList;
  4150. MakeFileListParam.FileTimes = NULL;
  4151. MakeFileListParam.IncludeDirs = true;
  4152. MakeFileListParam.Recursive = false;
  4153. ProcessLocalDirectory(TempDir, Terminal->MakeLocalFileList, &MakeFileListParam);
  4154. TTerminalManager::Instance()->ActiveSession = Session;
  4155. if (TemporaryFilesList->Count > 0)
  4156. {
  4157. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  4158. CopyParam.FileMask = FileMask;
  4159. DebugAssert(!FAutoOperation);
  4160. FAutoOperation = true;
  4161. Terminal->CopyToRemote(TemporaryFilesList, Target, &CopyParam, cpTemporary, NULL);
  4162. }
  4163. }
  4164. __finally
  4165. {
  4166. delete TemporaryFilesList;
  4167. FAutoOperation = false;
  4168. if (!RootTempDir.IsEmpty())
  4169. {
  4170. RecursiveDeleteFile(ExcludeTrailingBackslash(RootTempDir), false);
  4171. }
  4172. }
  4173. }
  4174. else
  4175. {
  4176. RemoteDirView->SaveSelection();
  4177. RemoteDirView->SaveSelectedNames();
  4178. try
  4179. {
  4180. if (Move)
  4181. {
  4182. Configuration->Usage->Inc("RemoteMove");
  4183. Terminal->MoveFiles(FileList, Target, FileMask, false);
  4184. }
  4185. else
  4186. {
  4187. Configuration->Usage->Inc("RemoteCopyDirect");
  4188. DebugAssert(DirectCopy);
  4189. DebugAssert(Session == Terminal);
  4190. if (Terminal->IsCapable[fcRemoteCopy] ||
  4191. Terminal->CommandSessionOpened ||
  4192. CommandSessionFallback())
  4193. {
  4194. Terminal->CopyFiles(FileList, Target, FileMask);
  4195. }
  4196. }
  4197. }
  4198. catch(...)
  4199. {
  4200. RemoteDirView->DiscardSavedSelection();
  4201. throw;
  4202. }
  4203. RemoteDirView->RestoreSelection();
  4204. }
  4205. }
  4206. return Result;
  4207. }
  4208. //---------------------------------------------------------------------------
  4209. void __fastcall TCustomScpExplorerForm::CreateRemoteDirectory(
  4210. const UnicodeString & Path, TRemoteProperties & Properties)
  4211. {
  4212. DebugAssert(!IsLocalBrowserMode());
  4213. Properties.Valid = Properties.Valid << vpEncrypt;
  4214. Properties.Encrypt = GUIConfiguration->CurrentCopyParam.EncryptNewFiles;
  4215. RemoteDirView->CreateDirectoryEx(Path, &Properties);
  4216. }
  4217. //---------------------------------------------------------------------------
  4218. void __fastcall TCustomScpExplorerForm::CreateDirectory(TOperationSide Side)
  4219. {
  4220. Side = GetSide(Side);
  4221. TRemoteProperties Properties = GUIConfiguration->NewDirectoryProperties;
  4222. TRemoteProperties * AProperties = (!IsSideLocalBrowser(Side) ? &Properties : NULL);
  4223. UnicodeString Name = LoadStr(NEW_FOLDER);
  4224. int AllowedChanges =
  4225. FLAGMASK(!IsSideLocalBrowser(Side) && Terminal->IsCapable[fcModeChanging], cpMode);
  4226. bool SaveSettings = false;
  4227. if (DoCreateDirectoryDialog(Name, AProperties, AllowedChanges, SaveSettings))
  4228. {
  4229. TWindowLock Lock(this);
  4230. if (!IsSideLocalBrowser(Side))
  4231. {
  4232. if (SaveSettings)
  4233. {
  4234. GUIConfiguration->NewDirectoryProperties = Properties;
  4235. }
  4236. CreateRemoteDirectory(Name, Properties);
  4237. }
  4238. else
  4239. {
  4240. DirView(Side)->CreateDirectory(Name);
  4241. }
  4242. }
  4243. }
  4244. //---------------------------------------------------------------------------
  4245. void __fastcall TCustomScpExplorerForm::HomeDirectory(TOperationSide Side)
  4246. {
  4247. TWindowLock Lock(this);
  4248. DirView(Side)->ExecuteHomeDirectory();
  4249. }
  4250. //---------------------------------------------------------------------------
  4251. void __fastcall TCustomScpExplorerForm::OpenDirectory(TOperationSide Side)
  4252. {
  4253. DoOpenDirectoryDialog(odBrowse, Side);
  4254. }
  4255. //---------------------------------------------------------------------------
  4256. bool __fastcall TCustomScpExplorerForm::OpenBookmark(TOperationSide Side, TBookmark * Bookmark)
  4257. {
  4258. TOperationSide BookmarkSide = IsLocalBrowserMode() ? osLocal : Side;
  4259. UnicodeString Path = Bookmark->GetSideDirectory(BookmarkSide);
  4260. bool Result = !Path.IsEmpty();
  4261. if (Result)
  4262. {
  4263. TryOpenDirectory(Side, Path);
  4264. }
  4265. return Result;
  4266. }
  4267. //---------------------------------------------------------------------------
  4268. void __fastcall TCustomScpExplorerForm::CalculateSize(
  4269. TStrings * FileList, __int64 & Size, TCalculateSizeStats & Stats,
  4270. bool & Close)
  4271. {
  4272. // terminal can be already closed (e.g. dropped connection)
  4273. if (Terminal != NULL)
  4274. {
  4275. try
  4276. {
  4277. TCalculateSizeParams Params;
  4278. Params.Stats = &Stats;
  4279. Terminal->CalculateFilesSize(FileList, Size, Params);
  4280. }
  4281. catch(...)
  4282. {
  4283. if (!Terminal->Active)
  4284. {
  4285. Close = true;
  4286. }
  4287. throw;
  4288. }
  4289. }
  4290. }
  4291. //---------------------------------------------------------------------------
  4292. void __fastcall TCustomScpExplorerForm::CalculateChecksum(const UnicodeString & Alg,
  4293. TStrings * FileList, TCalculatedChecksumEvent OnCalculatedChecksum,
  4294. bool & Close)
  4295. {
  4296. if ((Terminal != NULL) && // terminal can be already closed (e.g. dropped connection)
  4297. EnsureCommandSessionFallback(fcCalculatingChecksum))
  4298. {
  4299. Configuration->Usage->Inc(L"ChecksumCalculated");
  4300. try
  4301. {
  4302. Terminal->CalculateFilesChecksum(Alg, FileList, OnCalculatedChecksum);
  4303. }
  4304. catch(...)
  4305. {
  4306. if (!Terminal->Active)
  4307. {
  4308. Close = true;
  4309. }
  4310. throw;
  4311. }
  4312. }
  4313. }
  4314. //---------------------------------------------------------------------------
  4315. void TCustomScpExplorerForm::LoadFilesProperties(TStrings * FileList)
  4316. {
  4317. if (Terminal->LoadFilesProperties(FileList))
  4318. {
  4319. RemoteDirView->Invalidate();
  4320. }
  4321. }
  4322. //---------------------------------------------------------------------------
  4323. bool __fastcall TCustomScpExplorerForm::SetProperties(TOperationSide Side, TStrings * FileList)
  4324. {
  4325. bool Result;
  4326. if (!IsSideLocalBrowser(Side))
  4327. {
  4328. TRemoteTokenList * GroupList = NULL;
  4329. TRemoteTokenList * UserList = NULL;
  4330. try
  4331. {
  4332. TRemoteProperties CurrentProperties;
  4333. LoadFilesProperties(FileList);
  4334. bool CapableGroupChanging = Terminal->IsCapable[fcGroupChanging];
  4335. bool CapableOwnerChanging = Terminal->IsCapable[fcOwnerChanging];
  4336. if (CapableGroupChanging || CapableOwnerChanging)
  4337. {
  4338. if (CapableGroupChanging)
  4339. {
  4340. GroupList = Terminal->Groups->Duplicate();
  4341. }
  4342. if (CapableOwnerChanging)
  4343. {
  4344. UserList = Terminal->Users->Duplicate();
  4345. }
  4346. TRemoteDirectory * Files = Terminal->Files;
  4347. int Count = Files->Count;
  4348. if (Count > 100)
  4349. {
  4350. Count = 100;
  4351. }
  4352. for (int Index = 0; Index < Count; Index++)
  4353. {
  4354. TRemoteFile * File = Files->Files[Index];
  4355. if (CapableGroupChanging)
  4356. {
  4357. GroupList->AddUnique(File->Group);
  4358. }
  4359. if (CapableOwnerChanging)
  4360. {
  4361. UserList->AddUnique(File->Owner);
  4362. }
  4363. }
  4364. // if we haven't collected tokens for all files in current directory,
  4365. // make sure we collect them at least for all selected files.
  4366. // (note that so far the files in FileList has to be from current direcotry)
  4367. if (Count < Files->Count)
  4368. {
  4369. for (int Index = 0; Index < FileList->Count; Index++)
  4370. {
  4371. TRemoteFile * File = (TRemoteFile *)(FileList->Objects[Index]);
  4372. if (CapableGroupChanging)
  4373. {
  4374. GroupList->AddUnique(File->Group);
  4375. }
  4376. if (CapableOwnerChanging)
  4377. {
  4378. UserList->AddUnique(File->Owner);
  4379. }
  4380. }
  4381. }
  4382. }
  4383. CurrentProperties = TRemoteProperties::CommonProperties(FileList);
  4384. bool CapableAclChanging = Terminal->IsCapable[fcAclChangingFiles];
  4385. for (int Index = 0; (Index < FileList->Count) && CapableAclChanging; Index++)
  4386. {
  4387. if (dynamic_cast<TRemoteFile *>(FileList->Objects[Index])->IsDirectory)
  4388. {
  4389. CapableAclChanging = false;
  4390. CurrentProperties.Valid = CurrentProperties.Valid >> vpRights;
  4391. }
  4392. }
  4393. int Flags = 0;
  4394. if (Terminal->IsCapable[fcModeChanging]) Flags |= cpMode;
  4395. if (CapableAclChanging) Flags |= cpAcl;
  4396. if (CapableOwnerChanging) Flags |= cpOwner;
  4397. if (CapableGroupChanging) Flags |= cpGroup;
  4398. TCalculateChecksumEvent CalculateChecksumEvent = NULL;
  4399. if (Terminal->IsCapable[fcCalculatingChecksum] ||
  4400. (Terminal->IsCapable[fcSecondaryShell] && !Terminal->IsEncryptingFiles()))
  4401. {
  4402. CalculateChecksumEvent = CalculateChecksum;
  4403. }
  4404. std::unique_ptr<TStrings> ChecksumAlgs(new TStringList());
  4405. Terminal->GetSupportedChecksumAlgs(ChecksumAlgs.get());
  4406. TRemoteProperties NewProperties = CurrentProperties;
  4407. Result =
  4408. DoPropertiesDialog(FileList, RemoteDirView->PathName,
  4409. GroupList, UserList, ChecksumAlgs.get(), &NewProperties, Flags,
  4410. Terminal->IsCapable[fcGroupOwnerChangingByID],
  4411. CalculateSize, CalculateChecksumEvent);
  4412. if (Result)
  4413. {
  4414. NewProperties = TRemoteProperties::ChangedProperties(CurrentProperties, NewProperties);
  4415. Terminal->ChangeFilesProperties(FileList, &NewProperties);
  4416. }
  4417. }
  4418. __finally
  4419. {
  4420. delete GroupList;
  4421. delete UserList;
  4422. }
  4423. }
  4424. else
  4425. {
  4426. DirView(Side)->DisplayPropertiesMenu();
  4427. Result = true;
  4428. }
  4429. return Result;
  4430. }
  4431. //---------------------------------------------------------------------------
  4432. void __fastcall TCustomScpExplorerForm::KeyProcessed(Word & Key, TShiftState Shift)
  4433. {
  4434. if (Shift * AllKeyShiftStates() == (TShiftState() << ssAlt))
  4435. {
  4436. FIgnoreNextDialogChar = Key;
  4437. }
  4438. Key = 0;
  4439. }
  4440. //---------------------------------------------------------------------------
  4441. void __fastcall TCustomScpExplorerForm::CheckCustomCommandShortCut(
  4442. TCustomCommandList * List, Word & Key, Classes::TShiftState Shift, TShortCut KeyShortCut)
  4443. {
  4444. const TCustomCommandType * Command = List->Find(KeyShortCut);
  4445. if (Command != NULL)
  4446. {
  4447. KeyProcessed(Key, Shift);
  4448. if (CustomCommandState(*Command, false, ccltAll) > 0)
  4449. {
  4450. TAutoFlag DontCopyCommandToClipboardFlag(DontCopyCommandToClipboard);
  4451. ExecuteFileOperationCommand(foCustomCommand, osRemote,
  4452. false, false, const_cast<TCustomCommandType *>(Command));
  4453. }
  4454. }
  4455. }
  4456. //---------------------------------------------------------------------------
  4457. void __fastcall TCustomScpExplorerForm::KeyDown(Word & Key, Classes::TShiftState Shift)
  4458. {
  4459. if (!NonVisualDataModule->Busy)
  4460. {
  4461. if (QueueView3->Focused() && (QueueView3->OnKeyDown != NULL))
  4462. {
  4463. QueueView3->OnKeyDown(QueueView3, Key, Shift);
  4464. }
  4465. if ((Key != 0) && !DirView(osCurrent)->IsEditing())
  4466. {
  4467. TShortCut KeyShortCut = ShortCut(Key, Shift);
  4468. for (int Index = 0; Index < NonVisualDataModule->ExplorerActions->ActionCount; Index++)
  4469. {
  4470. // Note that handling shortcuts on panel (current control) popup menu has precendence over this.
  4471. // But so far it's not a problem, as it does what we want.
  4472. TAction * Action = (TAction *)NonVisualDataModule->ExplorerActions->Actions[Index];
  4473. if (((Action->ShortCut == KeyShortCut) ||
  4474. (Action->SecondaryShortCuts->IndexOfShortCut(KeyShortCut) >= 0)) &&
  4475. AllowedAction(Action, aaShortCut))
  4476. {
  4477. // Has to be called before the action as the dialog char is already in queue.
  4478. // So when the action consumes message queue, we already need to have the
  4479. // FIgnoreNextDialogChar set
  4480. KeyProcessed(Key, Shift);
  4481. // Reset reference to previous component (when menu/toolbar was clicked).
  4482. // Needed to detect that action was invoked by keyboard shortcut
  4483. // in TNonVisualDataModule::ExplorerActionsExecute
  4484. Action->ActionComponent = NULL;
  4485. Action->Execute();
  4486. return;
  4487. }
  4488. }
  4489. for (int i = 0; i < TTerminalManager::Instance()->Count; i++)
  4490. {
  4491. if (NonVisualDataModule->OpenSessionShortCut(i) == KeyShortCut)
  4492. {
  4493. KeyProcessed(Key, Shift);
  4494. TTerminalManager::Instance()->ActiveSessionIndex = i;
  4495. return;
  4496. }
  4497. }
  4498. if (Key == VK_TAB && Shift.Contains(ssCtrl))
  4499. {
  4500. KeyProcessed(Key, Shift);
  4501. TTerminalManager::Instance()->CycleTerminals(!Shift.Contains(ssShift));
  4502. }
  4503. TShortCut CustomShortCut = NormalizeCustomShortCut(KeyShortCut);
  4504. if (IsCustomShortCut(CustomShortCut))
  4505. {
  4506. CheckCustomCommandShortCut(WinConfiguration->CustomCommandList, Key, Shift, CustomShortCut);
  4507. CheckCustomCommandShortCut(WinConfiguration->ExtensionList, Key, Shift, CustomShortCut);
  4508. if (WinConfiguration->SharedBookmarks != NULL)
  4509. {
  4510. TBookmark * Bookmark = WinConfiguration->SharedBookmarks->FindByShortCut(CustomShortCut);
  4511. if ((Bookmark != NULL) &&
  4512. OpenBookmark(FCurrentSide, Bookmark))
  4513. {
  4514. KeyProcessed(Key, Shift);
  4515. }
  4516. }
  4517. }
  4518. }
  4519. }
  4520. TForm::KeyDown(Key, Shift);
  4521. }
  4522. //---------------------------------------------------------------------------
  4523. void __fastcall TCustomScpExplorerForm::InitStatusBar()
  4524. {
  4525. bool OpenedTerminal = false;
  4526. UnicodeString ProtocolName;
  4527. UnicodeString ProtocolNameHint;
  4528. UnicodeString SecurityProtocolName;
  4529. UnicodeString DurationHint;
  4530. // See the comment in TScpCommanderForm::UpdateControls()
  4531. TTerminal * ActiveTerminal = TTerminalManager::Instance()->ActiveTerminal;
  4532. if (ActiveTerminal != NULL)
  4533. {
  4534. // Active is not enough here
  4535. if (ActiveTerminal->Status == ssOpened)
  4536. {
  4537. const TSessionInfo & SessionInfo = ActiveTerminal->GetSessionInfo();
  4538. const TFileSystemInfo & FileSystemInfo = ActiveTerminal->GetFileSystemInfo();
  4539. if (FileSystemInfo.ProtocolName.IsEmpty())
  4540. {
  4541. ProtocolName = SessionInfo.ProtocolName;
  4542. }
  4543. else
  4544. {
  4545. ProtocolName = FileSystemInfo.ProtocolName;
  4546. }
  4547. ProtocolNameHint = LoadStr(STATUS_PROTOCOL_HINT);
  4548. SecurityProtocolName = SessionInfo.SecurityProtocolName;
  4549. DurationHint = LoadStr(STATUS_DURATION_HINT);
  4550. OpenedTerminal = true;
  4551. }
  4552. else if (ActiveTerminal->Status > ssClosed)
  4553. {
  4554. ProtocolName = LoadStr(STATUS_CONNECTING);
  4555. }
  4556. else
  4557. {
  4558. ProtocolName = LoadStr(STATUS_NOT_CONNECTED2);
  4559. }
  4560. }
  4561. TTBXStatusBar * SessionStatusBar = (TTBXStatusBar *)GetComponent(fcStatusBar);
  4562. int Offset = SessionStatusBar->Panels->Count - SessionPanelCount;
  4563. TTBXStatusPanel * ProtocolStatusPanel = SessionStatusBar->Panels->Items[Offset + 0];
  4564. ProtocolStatusPanel->ViewPriority = (ActiveTerminal != NULL) ? 100 : 0;
  4565. if (ProtocolStatusPanel->Caption != ProtocolName)
  4566. {
  4567. ProtocolStatusPanel->Caption = ProtocolName;
  4568. std::unique_ptr<TCanvas> Canvas(CreateControlCanvas(SessionStatusBar));
  4569. ProtocolStatusPanel->Size = Canvas->TextWidth(ProtocolName) + ScaleByTextHeight(SessionStatusBar, 32);
  4570. }
  4571. ProtocolStatusPanel->Hint = ProtocolNameHint;
  4572. TTBXStatusPanel * SecurityStatusPanel = SessionStatusBar->Panels->Items[Offset + 1];
  4573. SecurityStatusPanel->ViewPriority = OpenedTerminal ? 98 : 0;
  4574. bool SecurityEnabled = !SecurityProtocolName.IsEmpty();
  4575. SecurityStatusPanel->Enabled = SecurityEnabled;
  4576. UnicodeString SecurityProtocolHint;
  4577. if (SecurityEnabled)
  4578. {
  4579. SecurityProtocolHint = FMTLOAD(STATUS_SECURE, (SecurityProtocolName));
  4580. }
  4581. else
  4582. {
  4583. SecurityProtocolHint = LoadStr(STATUS_INSECURE);
  4584. }
  4585. SecurityStatusPanel->Hint = SecurityProtocolHint;
  4586. TTBXStatusPanel * DurationStatusPanel = SessionStatusBar->Panels->Items[Offset + 2];
  4587. DurationStatusPanel->ViewPriority = OpenedTerminal ? 99 : 0;
  4588. DurationStatusPanel->Hint = DurationHint;
  4589. UpdateStatusBar();
  4590. }
  4591. //---------------------------------------------------------------------------
  4592. void __fastcall TCustomScpExplorerForm::UpdateStatusBar()
  4593. {
  4594. TTBXStatusBar * SessionStatusBar = (TTBXStatusBar *)GetComponent(fcStatusBar);
  4595. DebugAssert(SessionStatusBar != NULL);
  4596. TTBXStatusPanel * NoteStatusPanel = SessionStatusBar->Panels->Items[0];
  4597. if (!FNote.IsEmpty())
  4598. {
  4599. NoteStatusPanel->Caption = FNote;
  4600. }
  4601. else
  4602. {
  4603. UpdateStatusPanelText(NoteStatusPanel);
  4604. }
  4605. NoteStatusPanel->Hint = FNoteHints;
  4606. UnicodeString LoginTime;
  4607. // See the comment in TScpCommanderForm::UpdateControls()
  4608. TTerminal * ActiveTerminal = TTerminalManager::Instance()->ActiveTerminal;
  4609. // Active is not enough here
  4610. if ((ActiveTerminal != NULL) && (ActiveTerminal->Status == ssOpened))
  4611. {
  4612. const TSessionInfo & SessionInfo = ActiveTerminal->GetSessionInfo();
  4613. LoginTime = FormatDateTimeSpan(Configuration->TimeFormat, Now() - SessionInfo.LoginTime);
  4614. }
  4615. TTBXStatusPanel * DurationStatusPanel = SessionStatusBar->Panels->Items[SessionStatusBar->Panels->Count - 1];
  4616. DurationStatusPanel->Caption = LoginTime;
  4617. }
  4618. //---------------------------------------------------------------------------
  4619. void __fastcall TCustomScpExplorerForm::UpdateStatusPanelText(TTBXStatusPanel * Panel)
  4620. {
  4621. Panel->Caption = L"";
  4622. }
  4623. //---------------------------------------------------------------------------
  4624. void __fastcall TCustomScpExplorerForm::Idle()
  4625. {
  4626. if (FShowing ||
  4627. // Particularly for
  4628. // - Standalone editing
  4629. // - Detecting closed connection and automatically reconnect it while waiting for changes
  4630. // while "keeping remote directory up to date".
  4631. StandaloneOperation)
  4632. {
  4633. FEditorManager->Check();
  4634. // make sure that Idle is called before update queue, as it may invoke QueueEvent
  4635. // that needs to know if queue view is visible (and it may be closed after queue update)
  4636. TTerminalManager::Instance()->Idle(FDoNotIdleCurrentTerminal > 0);
  4637. }
  4638. if (FShowing)
  4639. {
  4640. if (!NonVisualDataModule->Busy &&
  4641. // Menu is opened or drag&drop is going on
  4642. (Mouse->Capture == NULL))
  4643. {
  4644. if (FRefreshRemoteDirectory)
  4645. {
  4646. if ((Terminal != NULL) && (Terminal->Status == ssOpened))
  4647. {
  4648. Terminal->RefreshDirectory();
  4649. }
  4650. FRefreshRemoteDirectory = false;
  4651. }
  4652. if (FRefreshLocalDirectory)
  4653. {
  4654. ReloadLocalDirectory();
  4655. FRefreshLocalDirectory = false;
  4656. }
  4657. if (WinConfiguration->RefreshRemotePanel)
  4658. {
  4659. if ((Terminal != NULL) && (Terminal->Status == ssOpened) &&
  4660. (Now() - Terminal->DirectoryLoaded > WinConfiguration->RefreshRemotePanelInterval))
  4661. {
  4662. RemoteDirView->ReloadDirectory();
  4663. }
  4664. }
  4665. }
  4666. // TB2 shows accels on Alt key down, but hides it only after menu modal loop finishes.
  4667. // But if Alt is used to switch apps or for keyboard shortcut, the menu modal loop does not happen and accelerators stay displayed.
  4668. // It's difficult to fix this in TB2, as there's no "up" equivalent to CM_DIALOGKEY, which shows the accels.
  4669. // 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.
  4670. TTBCustomToolbar * MenuToolbar = DebugNotNull(dynamic_cast<TTBCustomToolbar *>(GetComponent(fcMenu)));
  4671. MenuToolbar->Idle();
  4672. }
  4673. if (FShowing || StandaloneOperation)
  4674. {
  4675. if (FQueueStatusInvalidated)
  4676. {
  4677. UpdateQueueStatus(false);
  4678. }
  4679. RefreshQueueItems();
  4680. }
  4681. if (FShowing)
  4682. {
  4683. UpdateStatusBar();
  4684. }
  4685. if (FClipboardFakeMonitorsPendingReset && !FDownloadingFromClipboard)
  4686. {
  4687. FClipboardFakeMonitorsPendingReset = false;
  4688. FClipboardFakeMonitors.reset(NULL);
  4689. }
  4690. FIgnoreNextDialogChar = 0;
  4691. }
  4692. //---------------------------------------------------------------------------
  4693. void __fastcall TCustomScpExplorerForm::UserActionTimer(TObject * /*Sender*/)
  4694. {
  4695. try
  4696. {
  4697. FUserActionTimer->Enabled = false;
  4698. if (IsQueueAutoPopup() && (FPendingQueueActionItem != NULL))
  4699. {
  4700. if (TQueueItem::IsUserActionStatus(FPendingQueueActionItem->Status))
  4701. {
  4702. FPendingQueueActionItem->ProcessUserAction();
  4703. }
  4704. }
  4705. }
  4706. __finally
  4707. {
  4708. FPendingQueueActionItem = NULL;
  4709. }
  4710. }
  4711. //---------------------------------------------------------------------------
  4712. void __fastcall TCustomScpExplorerForm::ApplicationMinimize(TObject * /*Sender*/)
  4713. {
  4714. if (WinConfiguration->MinimizeToTray)
  4715. {
  4716. UpdateTrayIcon();
  4717. FTrayIcon->Visible = true;
  4718. if (Visible)
  4719. {
  4720. ShowWindow(Handle, SW_HIDE);
  4721. }
  4722. }
  4723. }
  4724. //---------------------------------------------------------------------------
  4725. void __fastcall TCustomScpExplorerForm::ApplicationRestore(TObject * /*Sender*/)
  4726. {
  4727. // WORKAROUND
  4728. // When restoring maximized window from minimization,
  4729. // rarely some controls do not align properly.
  4730. // Two instances seen (both for Commander):
  4731. // - When restoring, window is temporarily narrower (not maximized),
  4732. // causing toolbars on TopDock to wrap and dock to expand horizontally.
  4733. // Once maximized already, top dock shinks back, but the session PageControl,
  4734. // do not align up, leaving space between TopDock and PageControl.
  4735. // - Similar issue seen with LocalDirView not aligning down to status bar.
  4736. for (int Index = 0; Index < ControlCount; Index++)
  4737. {
  4738. RealignControl(Controls[Index]);
  4739. }
  4740. if (FTrayIcon->Visible)
  4741. {
  4742. FTrayIcon->Visible = false;
  4743. if (Visible)
  4744. {
  4745. ShowWindow(Handle, SW_SHOW);
  4746. }
  4747. }
  4748. if (FNeedSession && DebugAlwaysTrue(Terminal == NULL))
  4749. {
  4750. FNeedSession = false;
  4751. NonVisualDataModule->StartBusy();
  4752. try
  4753. {
  4754. NeedSession(true);
  4755. }
  4756. __finally
  4757. {
  4758. NonVisualDataModule->EndBusy();
  4759. }
  4760. }
  4761. }
  4762. //---------------------------------------------------------------------------
  4763. void __fastcall TCustomScpExplorerForm::UpdateTrayIcon()
  4764. {
  4765. FTrayIcon->Hint = GetMainForm()->Caption;
  4766. }
  4767. //---------------------------------------------------------------------------
  4768. void __fastcall TCustomScpExplorerForm::ApplicationTitleChanged()
  4769. {
  4770. UpdateTrayIcon();
  4771. }
  4772. //---------------------------------------------------------------------------
  4773. void __fastcall TCustomScpExplorerForm::RestoreApp()
  4774. {
  4775. // workaround
  4776. // TApplication.WndProc sets TApplication.FAppIconic to false,
  4777. // on WM_ACTIVATEAPP, what renders TApplication.Restore no-op function.
  4778. // But WM_ACTIVATEAPP message can be received even when
  4779. // the main window is minimized to the tray and internal editor window is focused
  4780. // (after another application was previously active)
  4781. if (::IsIconic(Handle))
  4782. {
  4783. if (!IsAppIconic())
  4784. {
  4785. SetAppIconic(true);
  4786. }
  4787. }
  4788. ::ApplicationRestore();
  4789. Application->BringToFront();
  4790. }
  4791. //---------------------------------------------------------------------------
  4792. void __fastcall TCustomScpExplorerForm::TrayIconClick(TObject * /*Sender*/)
  4793. {
  4794. RestoreApp();
  4795. }
  4796. //---------------------------------------------------------------------------
  4797. void __fastcall TCustomScpExplorerForm::NewSession(const UnicodeString & SessionUrl)
  4798. {
  4799. if (OpenInNewWindow())
  4800. {
  4801. ExecuteNewInstance(SessionUrl);
  4802. }
  4803. else
  4804. {
  4805. TTerminalManager::Instance()->NewSession(SessionUrl);
  4806. }
  4807. }
  4808. //---------------------------------------------------------------------------
  4809. void TCustomScpExplorerForm::NewTab(TOperationSide, bool)
  4810. {
  4811. NewSession();
  4812. }
  4813. //---------------------------------------------------------------------------
  4814. UnicodeString __fastcall TCustomScpExplorerForm::SaveHiddenDuplicateSession(TSessionData * SessionData)
  4815. {
  4816. UnicodeString SessionName = StoredSessions->HiddenPrefix + ManagedSession->SessionData->SessionName;
  4817. StoredSessions->NewSession(SessionName, SessionData);
  4818. // modified only, explicit
  4819. StoredSessions->Save(false, true);
  4820. // encode session name because of slashes in hierarchical sessions
  4821. return EncodeUrlString(SessionName);
  4822. }
  4823. //---------------------------------------------------------------------------
  4824. UnicodeString __fastcall TCustomScpExplorerForm::CreateHiddenDuplicateSession()
  4825. {
  4826. // current working directories become defaults here, what is not right
  4827. std::unique_ptr<TSessionData> SessionData(CloneCurrentSessionData());
  4828. return SaveHiddenDuplicateSession(SessionData.get());
  4829. }
  4830. //---------------------------------------------------------------------------
  4831. void __fastcall TCustomScpExplorerForm::DuplicateTab()
  4832. {
  4833. if (OpenInNewWindow())
  4834. {
  4835. ExecuteNewInstance(CreateHiddenDuplicateSession());
  4836. }
  4837. else
  4838. {
  4839. // current working directories become defaults here, what is not right
  4840. std::unique_ptr<TSessionData> SessionData(CloneCurrentSessionData());
  4841. TTerminalManager * Manager = TTerminalManager::Instance();
  4842. TManagedTerminal * ASession = Manager->NewManagedTerminal(SessionData.get());
  4843. DebugAssert(ASession->LocalBrowser == ManagedSession->LocalBrowser);
  4844. // We definitelly want these
  4845. ASession->Disconnected = ManagedSession->Disconnected;
  4846. // Not sure about these two
  4847. ASession->Permanent = ManagedSession->Permanent;
  4848. ASession->DisconnectedTemporarily = ManagedSession->DisconnectedTemporarily;
  4849. Manager->ActiveSession = ASession;
  4850. }
  4851. }
  4852. //---------------------------------------------------------------------------
  4853. void __fastcall TCustomScpExplorerForm::RenameTab()
  4854. {
  4855. UnicodeString Name = ManagedSession->SessionData->SessionName;
  4856. if (InputDialog(LoadStr(RENAME_TAB_TITLE), LoadStr(RENAME_TAB_PROMPT), Name, HELP_TAB_RENAME) &&
  4857. // When submitting the default name, do not fix it
  4858. (Name != ManagedSession->SessionData->SessionName))
  4859. {
  4860. // Checks for a slash only, so it's not that big deal that we do the check after submitting the dialog only.
  4861. TSessionData::ValidateName(Name);
  4862. // This is inconsistent with how color (for example) is handled.
  4863. ManagedSession->SessionData->Name = Name;
  4864. UpdateControls();
  4865. // Add/Remove distinguishing paths from sessions of the same name.
  4866. SessionListChanged();
  4867. }
  4868. }
  4869. //---------------------------------------------------------------------------
  4870. bool __fastcall TCustomScpExplorerForm::CanCloseQueue(TTerminalQueue * Queue)
  4871. {
  4872. DebugAssert(Queue != NULL);
  4873. bool Result = Queue->IsEmpty;
  4874. if (!Result)
  4875. {
  4876. SetFocus();
  4877. Result = (MessageDialog(LoadStr(PENDING_QUEUE_ITEMS2), qtWarning, qaOK | qaCancel, HELP_NONE) == qaOK);
  4878. }
  4879. return Result;
  4880. }
  4881. //---------------------------------------------------------------------------
  4882. bool __fastcall TCustomScpExplorerForm::CanCloseQueue()
  4883. {
  4884. return CanCloseQueue(FQueue);
  4885. }
  4886. //---------------------------------------------------------------------------
  4887. void __fastcall TCustomScpExplorerForm::CloseTab()
  4888. {
  4889. if (CanCloseQueue())
  4890. {
  4891. TTerminalManager::Instance()->FreeActiveTerminal();
  4892. }
  4893. }
  4894. //---------------------------------------------------------------------------
  4895. void __fastcall TCustomScpExplorerForm::DisconnectSession()
  4896. {
  4897. if (CanCloseQueue())
  4898. {
  4899. TTerminalManager::Instance()->DisconnectActiveTerminal();
  4900. }
  4901. }
  4902. //---------------------------------------------------------------------------
  4903. void __fastcall TCustomScpExplorerForm::TerminalDisconnected()
  4904. {
  4905. DetachTerminal(Terminal);
  4906. RemoteDirView->Terminal = Terminal;
  4907. UpdateRemotePathComboBox(false);
  4908. InitStatusBar();
  4909. UpdateControls();
  4910. }
  4911. //---------------------------------------------------------------------------
  4912. void __fastcall TCustomScpExplorerForm::TerminalConnecting()
  4913. {
  4914. InitStatusBar();
  4915. }
  4916. //---------------------------------------------------------------------------
  4917. void __fastcall TCustomScpExplorerForm::ReconnectSession()
  4918. {
  4919. if (DebugAlwaysTrue(!Terminal->Active))
  4920. {
  4921. TTerminalManager::Instance()->ReconnectActiveTerminal();
  4922. }
  4923. }
  4924. //---------------------------------------------------------------------------
  4925. void __fastcall TCustomScpExplorerForm::OpenStoredSession(TSessionData * Data)
  4926. {
  4927. if (OpenInNewWindow())
  4928. {
  4929. // encode session name because of slashes in hierarchical sessions
  4930. ExecuteNewInstance(EncodeUrlString(Data->Name));
  4931. }
  4932. else
  4933. {
  4934. TTerminalManager * Manager = TTerminalManager::Instance();
  4935. TManagedTerminal * Terminal = Manager->NewManagedTerminal(Data);
  4936. Manager->ActiveSession = Terminal;
  4937. }
  4938. }
  4939. //---------------------------------------------------------------------------
  4940. void TCustomScpExplorerForm::DoOpenFolderOrWorkspace(
  4941. const UnicodeString & Name, bool ConnectFirstTerminal, bool CheckMaxSessions)
  4942. {
  4943. TTerminalManager * Manager = TTerminalManager::Instance();
  4944. std::unique_ptr<TObjectList> DataList(new TObjectList());
  4945. // This asks for a master password. Maybe we should postpone asking until a connection is actually opened.
  4946. StoredSessions->GetFolderOrWorkspace(Name, DataList.get());
  4947. if (!CheckMaxSessions || (DataList->Count <= WinConfiguration->MaxSessions))
  4948. {
  4949. TManagedTerminal * FirstSession = Manager->NewSessions(DataList.get());
  4950. // FirstSession can be null, if none of the workspace sites exist anymore
  4951. // or if all workspace sessions are not suported by the interface
  4952. // (when trying to open all local-local workspace in "explorer" interface)
  4953. if (FirstSession != NULL)
  4954. {
  4955. if (!ConnectFirstTerminal)
  4956. {
  4957. FirstSession->Disconnected = true;
  4958. FirstSession->DisconnectedTemporarily = true;
  4959. }
  4960. Manager->ActiveSession = FirstSession;
  4961. }
  4962. }
  4963. }
  4964. //---------------------------------------------------------------------------
  4965. void __fastcall TCustomScpExplorerForm::OpenFolderOrWorkspace(const UnicodeString & Name)
  4966. {
  4967. if (OpenInNewWindow())
  4968. {
  4969. ExecuteNewInstance(Name);
  4970. }
  4971. else
  4972. {
  4973. DoOpenFolderOrWorkspace(Name, true, false);
  4974. }
  4975. }
  4976. //---------------------------------------------------------------------------
  4977. void __fastcall TCustomScpExplorerForm::FormCloseQuery(TObject * /*Sender*/,
  4978. bool &CanClose)
  4979. {
  4980. // See the comment in CloseApp()
  4981. if (NonVisualDataModule->Busy)
  4982. {
  4983. CanClose = false;
  4984. }
  4985. else
  4986. {
  4987. TTerminalManager * Manager = TTerminalManager::Instance();
  4988. int ActiveSessions = 0;
  4989. for (int Index = 0; Index < Manager->Count; Index++)
  4990. {
  4991. if (Manager->Sessions[Index]->Active)
  4992. {
  4993. ActiveSessions++;
  4994. }
  4995. }
  4996. // Confirm, when there's at least one active remote session or
  4997. // when there are multiple (inactive or local) sessions and auto workspace saving is not enabled.
  4998. if (((ActiveSessions > 0) || ((Manager->Count > 1) && !WinConfiguration->AutoSaveWorkspace)) &&
  4999. WinConfiguration->ConfirmClosingSession)
  5000. {
  5001. unsigned int Result;
  5002. TMessageParams Params(mpNeverAskAgainCheck);
  5003. UnicodeString Message;
  5004. int Answers = qaOK | qaCancel;
  5005. // The current session is the only active session
  5006. if ((ActiveSessions == 1) && (Terminal != NULL) && Terminal->Active)
  5007. {
  5008. if (!WinConfiguration->AutoSaveWorkspace)
  5009. {
  5010. Message = LoadStr(CLOSE_SESSION_WORKSPACE);
  5011. }
  5012. else
  5013. {
  5014. Message = LoadStr(CLOSE_SESSION);
  5015. }
  5016. Message = FORMAT(Message, (Terminal->SessionData->SessionName));
  5017. }
  5018. // Multiple active sessions or one active session, but it's not the current one
  5019. else if (ActiveSessions > 0)
  5020. {
  5021. if (!WinConfiguration->AutoSaveWorkspace)
  5022. {
  5023. Message = LoadStr(CLOSE_SESSIONS_WORKSPACE3);
  5024. }
  5025. else
  5026. {
  5027. Message = LoadStr(CLOSE_SESSIONS);
  5028. }
  5029. }
  5030. // Multiple tabs, but all are inactive or local, and saving of worspace is not enabled
  5031. else
  5032. {
  5033. DebugAlwaysTrue(Manager->Count > 1);
  5034. DebugAlwaysTrue(!WinConfiguration->AutoSaveWorkspace);
  5035. Message = LoadStr(CLOSE_WORKSPACE);
  5036. }
  5037. UnicodeString Note;
  5038. if (WinConfiguration->AutoSaveWorkspace)
  5039. {
  5040. Note = FMTLOAD(AUTO_WORKSPACE, (WorkspaceName()));
  5041. }
  5042. else
  5043. {
  5044. Note = LoadStr(AUTO_WORKSPACE_ENABLE);
  5045. Answers = qaYes | qaNo | qaCancel;
  5046. }
  5047. Message = FORMAT("%s\n\n%s", (MainInstructions(Message), Note));
  5048. SetFocus();
  5049. Result = MessageDialog(Message, qtConfirmation,
  5050. Answers, HELP_CLOSE_SESSION_WORKSPACE, &Params);
  5051. if (Result == qaNeverAskAgain)
  5052. {
  5053. WinConfiguration->ConfirmClosingSession = false;
  5054. }
  5055. if (Result == qaNo)
  5056. {
  5057. CanClose = SaveWorkspace(true);
  5058. // note that the workspace will be saved redundatly again from FormClose
  5059. }
  5060. else
  5061. {
  5062. CanClose =
  5063. (Result == qaOK) ||
  5064. (Result == qaYes) || // CLOSE_SESSIONS_WORKSPACE variant
  5065. (Result == qaNeverAskAgain);
  5066. }
  5067. }
  5068. if (CanClose && HasActiveTerminal())
  5069. {
  5070. CanClose = CanCloseQueue();
  5071. }
  5072. }
  5073. if (CanClose)
  5074. {
  5075. CanClose =
  5076. FEditorManager->CloseInternalEditors(CloseInternalEditor) &&
  5077. FEditorManager->CloseExternalFilesWithoutProcess();
  5078. if (CanClose)
  5079. {
  5080. while (CanClose && (FLocalEditors->Count > 0))
  5081. {
  5082. int PrevCount = FLocalEditors->Count;
  5083. static_cast<TForm *>(FLocalEditors->Items[0])->Close();
  5084. CanClose = (FLocalEditors->Count < PrevCount);
  5085. }
  5086. if (CanClose)
  5087. {
  5088. CanClose = FEditorManager->Empty(true);
  5089. if (!CanClose)
  5090. {
  5091. SetFocus();
  5092. CanClose =
  5093. (MessageDialog(
  5094. LoadStr(PENDING_EDITORS), qtWarning, qaIgnore | qaCancel, HELP_NONE) == qaIgnore);
  5095. }
  5096. }
  5097. }
  5098. }
  5099. }
  5100. //---------------------------------------------------------------------------
  5101. void __fastcall TCustomScpExplorerForm::CloseInternalEditor(TObject * Sender)
  5102. {
  5103. TForm * Form = dynamic_cast<TForm *>(Sender);
  5104. DebugAssert(Form != NULL);
  5105. Form->Close();
  5106. }
  5107. //---------------------------------------------------------------------------
  5108. void __fastcall TCustomScpExplorerForm::ForceCloseInternalEditor(TObject * Sender)
  5109. {
  5110. TForm * Form = dynamic_cast<TForm *>(Sender);
  5111. delete Form;
  5112. }
  5113. //---------------------------------------------------------------------------
  5114. void __fastcall TCustomScpExplorerForm::ForceCloseLocalEditors()
  5115. {
  5116. while (FLocalEditors->Count > 0)
  5117. {
  5118. delete static_cast<TForm *>(FLocalEditors->Items[0]);
  5119. }
  5120. }
  5121. //---------------------------------------------------------------------------
  5122. void __fastcall TCustomScpExplorerForm::RemoteDirViewDisplayProperties(
  5123. TObject *Sender)
  5124. {
  5125. TStrings *FileList = ((TUnixDirView*)Sender)->CreateFileList(True, False, NULL);
  5126. try
  5127. {
  5128. SetProperties(osRemote, FileList);
  5129. }
  5130. __finally
  5131. {
  5132. delete FileList;
  5133. }
  5134. }
  5135. //---------------------------------------------------------------------------
  5136. void __fastcall TCustomScpExplorerForm::ComponentShowing(Byte Component, bool value)
  5137. {
  5138. if (value)
  5139. {
  5140. if (Component == fcCustomCommandsBand)
  5141. {
  5142. UpdateCustomCommandsToolbar();
  5143. }
  5144. if (Component == fcQueueFileList)
  5145. {
  5146. DebugAssert(!QueueFileListSplitter->Visible);
  5147. AdjustQueueLayout();
  5148. UpdateQueueFileList();
  5149. }
  5150. }
  5151. }
  5152. //---------------------------------------------------------------------------
  5153. void __fastcall TCustomScpExplorerForm::AdjustQueueLayout()
  5154. {
  5155. int MinHeightLimit =
  5156. GetStaticQueuePanelComponentsHeight() +
  5157. QueueFileListSplitter->Height +
  5158. QueueFileList->Height +
  5159. GetMinQueueViewHeight();
  5160. if (QueuePanel->ClientHeight < MinHeightLimit)
  5161. {
  5162. QueuePanel->ClientHeight = MinHeightLimit;
  5163. }
  5164. }
  5165. //---------------------------------------------------------------------------
  5166. void __fastcall TCustomScpExplorerForm::SetComponentVisible(Byte Component, Boolean value)
  5167. {
  5168. TControl * Control = GetComponent(Component);
  5169. DebugAssert(Control);
  5170. bool Changed = (Control->Visible != value);
  5171. if (Changed)
  5172. {
  5173. ComponentShowing(Component, value);
  5174. TWinControl * WinControl = dynamic_cast<TWinControl*>(Control);
  5175. bool WasFocused = (WinControl != NULL) && (ActiveControl != NULL) &&
  5176. ((ActiveControl == WinControl) || (ActiveControl->Parent == WinControl));
  5177. if (value)
  5178. {
  5179. int RemainingHeight = Control->Parent->ClientHeight;
  5180. int RemainingWidth = Control->Parent->ClientWidth;
  5181. for (int i = 0; i < Control->Parent->ControlCount; i++)
  5182. {
  5183. TControl * ChildControl = Control->Parent->Controls[i];
  5184. if (ChildControl->Visible)
  5185. {
  5186. switch (ChildControl->Align)
  5187. {
  5188. case alTop:
  5189. case alBottom:
  5190. RemainingHeight -= ChildControl->Height;
  5191. break;
  5192. case alLeft:
  5193. case alRight:
  5194. RemainingWidth -= ChildControl->Width;
  5195. break;
  5196. }
  5197. }
  5198. }
  5199. int Reserve = ScaleByTextHeight(this, 32);
  5200. // queue in explorer, trees in commander
  5201. if (Control->Height > RemainingHeight - Reserve)
  5202. {
  5203. Control->Height = RemainingHeight / 2;
  5204. }
  5205. if (Control->Width > RemainingWidth - Reserve)
  5206. {
  5207. Control->Width = RemainingWidth / 2;
  5208. }
  5209. }
  5210. Control->Visible = value;
  5211. if (WasFocused && Visible)
  5212. {
  5213. DirView(osCurrent)->SetFocus();
  5214. }
  5215. FixControlsPlacement();
  5216. }
  5217. }
  5218. //---------------------------------------------------------------------------
  5219. bool __fastcall TCustomScpExplorerForm::GetComponentVisible(Byte Component)
  5220. {
  5221. TControl * Control = GetComponent(Component);
  5222. if (Control == NULL)
  5223. {
  5224. return false;
  5225. }
  5226. else
  5227. {
  5228. return Control->Visible;
  5229. }
  5230. }
  5231. //---------------------------------------------------------------------------
  5232. bool __fastcall TCustomScpExplorerForm::IsComponentPossible(Byte Component)
  5233. {
  5234. bool Result;
  5235. if ((Component == fcExplorerUpdatesBand) || (Component == fcCommanderUpdatesBand))
  5236. {
  5237. Result = !IsUWP();
  5238. }
  5239. else
  5240. {
  5241. Result = true;
  5242. }
  5243. return Result;
  5244. }
  5245. //---------------------------------------------------------------------------
  5246. void __fastcall TCustomScpExplorerForm::MakeFocusedItemVisible(TCustomDirView * DirView)
  5247. {
  5248. if (DirView->ItemFocused != NULL)
  5249. {
  5250. DirView->ItemFocused->MakeVisible(false);
  5251. }
  5252. }
  5253. //---------------------------------------------------------------------------
  5254. void __fastcall TCustomScpExplorerForm::FixControlsPlacement()
  5255. {
  5256. MakeFocusedItemVisible(RemoteDirView);
  5257. QueueSplitter->Visible = QueuePanel->Visible;
  5258. QueueFileListSplitter->Visible = QueueFileList->Visible;
  5259. RemotePanelSplitter->Visible = RemoteDrivePanel->Visible;
  5260. TControl * TopControlsOrder[] =
  5261. { TopDock, MessageDock, SessionsPageControl };
  5262. SetVerticalControlsOrder(TopControlsOrder, LENOF(TopControlsOrder));
  5263. TControl * QueueControlsOrder[] =
  5264. { QueueDock, QueueView3, QueueFileListSplitter, QueueFileList };
  5265. SetVerticalControlsOrder(QueueControlsOrder, LENOF(QueueControlsOrder));
  5266. }
  5267. //---------------------------------------------------------------------------
  5268. TControl * __fastcall TCustomScpExplorerForm::GetComponent(Byte Component)
  5269. {
  5270. switch (Component) {
  5271. case fcStatusBar: return RemoteStatusBar;
  5272. case fcRemotePopup: return reinterpret_cast<TControl *>(NonVisualDataModule->RemoteFilePopup);
  5273. case fcQueueView: return QueuePanel;
  5274. case fcQueueToolbar: return QueueDock;
  5275. case fcRemoteTree: return RemoteDrivePanel;
  5276. case fcSessionsTabs: return SessionsPageControl;
  5277. case fcQueueFileList: return QueueFileList;
  5278. default: return NULL;
  5279. }
  5280. }
  5281. //---------------------------------------------------------------------------
  5282. void __fastcall TCustomScpExplorerForm::DirViewColumnRightClick(
  5283. TObject *Sender, TListColumn *Column, TPoint &Point)
  5284. {
  5285. DebugAssert(NonVisualDataModule && Column && Sender);
  5286. NonVisualDataModule->ListColumn = Column;
  5287. TPopupMenu * DirViewColumnMenu;
  5288. if (Sender == DirView(osOther))
  5289. {
  5290. DirViewColumnMenu = NonVisualDataModule->RemoteDirViewColumnPopup;
  5291. NonVisualDataModule->RemoteSortByExtColumnPopupItem->Visible = (Column->Index == uvName);
  5292. bool SizeColumn = (Column->Index == uvSize);
  5293. NonVisualDataModule->RemoteFormatSizeBytesPopupItem->Visible = SizeColumn;
  5294. NonVisualDataModule->RemoteCalculateDirectorySizesPopupItem->Visible = SizeColumn;
  5295. }
  5296. else
  5297. {
  5298. DirViewColumnMenu = NonVisualDataModule->LocalDirViewColumnPopup;
  5299. NonVisualDataModule->LocalSortByExtColumnPopupItem->Visible = (Column->Index == dvName);
  5300. bool SizeColumn = (Column->Index == dvSize);
  5301. NonVisualDataModule->LocalFormatSizeBytesPopupItem->Visible = SizeColumn;
  5302. NonVisualDataModule->LocalCalculateDirectorySizesPopupItem->Visible = SizeColumn;
  5303. }
  5304. TCustomListView * ListView = DebugNotNull(dynamic_cast<TCustomListView *>(Sender));
  5305. TPoint ScreenPoint = ListView->ClientToScreen(Point);
  5306. ScreenPoint.x -= GetScrollPos(ListView->Handle, SB_HORZ);
  5307. DirViewColumnMenu->Popup(ScreenPoint.x, ScreenPoint.y);
  5308. }
  5309. //---------------------------------------------------------------------------
  5310. void __fastcall TCustomScpExplorerForm::DirViewExecFile(
  5311. TObject *Sender, TListItem *Item, bool &AllowExec)
  5312. {
  5313. DoDirViewExecFile(Sender, Item, AllowExec);
  5314. }
  5315. //---------------------------------------------------------------------------
  5316. void __fastcall TCustomScpExplorerForm::DoDirViewExecFile(TObject * Sender,
  5317. TListItem * Item, bool & AllowExec)
  5318. {
  5319. DebugAssert(Sender && Item && Configuration);
  5320. DebugAssert(AllowExec);
  5321. TCustomDirView * ADirView = (TCustomDirView *)Sender;
  5322. TOperationSide Side = ((ADirView == DirView(osRemote)) ? osRemote : osLocal);
  5323. bool Remote = !IsSideLocalBrowser(Side);
  5324. DebugAssert(Item == ADirView->ItemFocused);
  5325. bool IsDirectory = ADirView->ItemIsDirectory(Item);
  5326. TResolvedDoubleClickAction Action;
  5327. if (FForceExecution)
  5328. {
  5329. Action = IsDirectory ? rdcaChangeDir : rdcaOpen;
  5330. }
  5331. else
  5332. {
  5333. TTerminal * ATerminal = !IsSideLocalBrowser(Side) ? Terminal : NULL;
  5334. Action = WinConfiguration->ResolveDoubleClickAction(IsDirectory, ATerminal);
  5335. }
  5336. if (Action == rdcaCopy)
  5337. {
  5338. // To counter the lock in DirViewBusy (called from TCustomDirView.DoExecute)
  5339. UnlockWindow();
  5340. try
  5341. {
  5342. if (IsLocalBrowserMode())
  5343. {
  5344. LocalLocalCopy(foCopy, Side, true, !WinConfiguration->CopyOnDoubleClickConfirmation, false, 0);
  5345. }
  5346. else
  5347. {
  5348. ExecuteFileOperation(foCopy, Side, true, !WinConfiguration->CopyOnDoubleClickConfirmation);
  5349. }
  5350. }
  5351. __finally
  5352. {
  5353. LockWindow();
  5354. }
  5355. AllowExec = false;
  5356. }
  5357. else if (Action == rdcaEdit)
  5358. {
  5359. if (!Remote || !WinConfiguration->DisableOpenEdit)
  5360. {
  5361. ExecuteFile(Side, efDefaultEditor);
  5362. AllowExec = false;
  5363. }
  5364. }
  5365. else if (Action == rdcaOpen)
  5366. {
  5367. if (DebugAlwaysTrue(!IsDirectory) && Remote && !WinConfiguration->DisableOpenEdit)
  5368. {
  5369. ExecuteFile(osRemote, efShell);
  5370. AllowExec = false;
  5371. }
  5372. }
  5373. }
  5374. //---------------------------------------------------------------------------
  5375. bool __fastcall TCustomScpExplorerForm::GetHasDirView(TOperationSide Side)
  5376. {
  5377. return ((Side == osRemote) || (Side == osCurrent));
  5378. }
  5379. //---------------------------------------------------------------------------
  5380. void __fastcall TCustomScpExplorerForm::CompareDirectories()
  5381. {
  5382. DebugFail();
  5383. }
  5384. //---------------------------------------------------------------------------
  5385. void __fastcall TCustomScpExplorerForm::SynchronizeDirectories()
  5386. {
  5387. DebugFail();
  5388. }
  5389. //---------------------------------------------------------------------------
  5390. bool __fastcall TCustomScpExplorerForm::DoSynchronizeDirectories(
  5391. UnicodeString & LocalDirectory, UnicodeString & RemoteDirectory, int UseDefaults)
  5392. {
  5393. TSynchronizeParamType Params;
  5394. Params.LocalDirectory = LocalDirectory;
  5395. Params.RemoteDirectory = RemoteDirectory;
  5396. int UnusedParams =
  5397. (GUIConfiguration->SynchronizeParams &
  5398. (TTerminal::spPreviewChanges | TTerminal::spTimestamp | TTerminal::spNotByTime | TTerminal::spBySize |
  5399. TTerminal::spCaseSensitive));
  5400. Params.Params = GUIConfiguration->SynchronizeParams & ~UnusedParams;
  5401. Params.Options = GUIConfiguration->SynchronizeOptions;
  5402. bool SaveSettings = false;
  5403. TSynchronizeController Controller(&DoSynchronize, &DoSynchronizeInvalid,
  5404. &DoSynchronizeTooManyDirectories);
  5405. DebugAssert(FSynchronizeController == NULL);
  5406. FSynchronizeController = &Controller;
  5407. bool Result;
  5408. try
  5409. {
  5410. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  5411. int CopyParamAttrs = Terminal->UsableCopyParamAttrs(0).Upload;
  5412. int Options =
  5413. FLAGMASK(SynchronizeAllowSelectedOnly(), soAllowSelectedOnly);
  5414. DebugAssert(FOnFeedSynchronizeError == NULL);
  5415. DebugAssert(FOnSynchronizeAbort == NULL);
  5416. DebugAssert(FSynchronizeTerminal == NULL);
  5417. FSynchronizeTerminal = Terminal;
  5418. Result = DoSynchronizeDialog(Params, &CopyParam, Controller.StartStop,
  5419. SaveSettings, Options, CopyParamAttrs, GetSynchronizeOptions, SynchronizeSessionLog,
  5420. FOnFeedSynchronizeError, FOnSynchronizeAbort, SynchronizeInNewWindow, UseDefaults);
  5421. if (Result)
  5422. {
  5423. if (SaveSettings)
  5424. {
  5425. GUIConfiguration->SynchronizeParams = Params.Params | UnusedParams;
  5426. GUIConfiguration->SynchronizeOptions = Params.Options;
  5427. }
  5428. else
  5429. {
  5430. if (FLAGSET(GUIConfiguration->SynchronizeOptions, soSynchronizeAsk) &&
  5431. FLAGCLEAR(Params.Options, soSynchronizeAsk) &&
  5432. FLAGSET(Params.Options, soSynchronize))
  5433. {
  5434. GUIConfiguration->SynchronizeOptions =
  5435. (GUIConfiguration->SynchronizeOptions & ~soSynchronizeAsk) |
  5436. soSynchronize;
  5437. }
  5438. }
  5439. LocalDirectory = Params.LocalDirectory;
  5440. RemoteDirectory = Params.RemoteDirectory;
  5441. }
  5442. }
  5443. __finally
  5444. {
  5445. FSynchronizeController = NULL;
  5446. DebugAssert(FOnFeedSynchronizeError == NULL);
  5447. FOnFeedSynchronizeError = NULL;
  5448. DebugAssert(FOnSynchronizeAbort == NULL);
  5449. FOnSynchronizeAbort = NULL;
  5450. FSynchronizeTerminal = NULL;
  5451. }
  5452. return Result;
  5453. }
  5454. //---------------------------------------------------------------------------
  5455. void __fastcall TCustomScpExplorerForm::DoSynchronize(
  5456. TSynchronizeController * /*Sender*/, const UnicodeString LocalDirectory,
  5457. const UnicodeString RemoteDirectory, const TCopyParamType & CopyParam,
  5458. const TSynchronizeParamType & Params, TSynchronizeChecklist ** Checklist,
  5459. TSynchronizeOptions * Options, bool Full)
  5460. {
  5461. // Keep-up-to-date mode
  5462. if (Terminal->Status == ssOpened)
  5463. {
  5464. try
  5465. {
  5466. int PParams = Params.Params;
  5467. if (!Full)
  5468. {
  5469. PParams |= TTerminal::spNoRecurse | TTerminal::spUseCache | TTerminal::spDelayProgress;
  5470. if (FLAGSET(Params.Options, soRecurse))
  5471. {
  5472. PParams |= TTerminal::spSubDirs;
  5473. }
  5474. }
  5475. else
  5476. {
  5477. // if keepuptodate is non-recursive,
  5478. // full sync before has to be non-recursive as well
  5479. if (FLAGCLEAR(Params.Options, soRecurse))
  5480. {
  5481. PParams |= TTerminal::spNoRecurse;
  5482. }
  5483. }
  5484. Synchronize(LocalDirectory, RemoteDirectory, smRemote, CopyParam,
  5485. PParams, Checklist, Options);
  5486. }
  5487. catch (Exception & E)
  5488. {
  5489. if (!Terminal->Active)
  5490. {
  5491. ShowExtendedExceptionEx(Terminal, &E);
  5492. // Do not abort the "keep up to date" when the session was succesfully reconnected.
  5493. if (!Terminal->Active)
  5494. {
  5495. throw;
  5496. }
  5497. }
  5498. else
  5499. {
  5500. if (FLAGSET(Params.Options, soContinueOnError) &&
  5501. DebugAlwaysTrue(FOnFeedSynchronizeError != NULL))
  5502. {
  5503. // noop, already logged in MoreMessageDialog
  5504. }
  5505. else
  5506. {
  5507. // We get mostly EAbort here, so this is noop.
  5508. // Exception is an error when listing directories while looking for differences.
  5509. ShowExtendedExceptionEx(Terminal, &E);
  5510. throw;
  5511. }
  5512. }
  5513. }
  5514. }
  5515. }
  5516. //---------------------------------------------------------------------------
  5517. void __fastcall TCustomScpExplorerForm::DoSynchronizeInvalid(
  5518. TSynchronizeController * /*Sender*/, const UnicodeString Directory,
  5519. const UnicodeString ErrorStr)
  5520. {
  5521. if (!Directory.IsEmpty())
  5522. {
  5523. SimpleErrorDialog(FMTLOAD(WATCH_ERROR_DIRECTORY, (Directory)), ErrorStr);
  5524. }
  5525. else
  5526. {
  5527. SimpleErrorDialog(LoadStr(WATCH_ERROR_GENERAL), ErrorStr);
  5528. }
  5529. }
  5530. //---------------------------------------------------------------------------
  5531. void __fastcall TCustomScpExplorerForm::DoSynchronizeTooManyDirectories(
  5532. TSynchronizeController * /*Sender*/, int & MaxDirectories)
  5533. {
  5534. if (MaxDirectories < GUIConfiguration->MaxWatchDirectories)
  5535. {
  5536. MaxDirectories = GUIConfiguration->MaxWatchDirectories;
  5537. }
  5538. else
  5539. {
  5540. TMessageParams Params(mpNeverAskAgainCheck);
  5541. unsigned int Result = MessageDialog(
  5542. FMTLOAD(TOO_MANY_WATCH_DIRECTORIES, (MaxDirectories, MaxDirectories)),
  5543. qtConfirmation, qaYes | qaNo, HELP_TOO_MANY_WATCH_DIRECTORIES, &Params);
  5544. if ((Result == qaYes) || (Result == qaNeverAskAgain))
  5545. {
  5546. MaxDirectories *= 2;
  5547. if (Result == qaNeverAskAgain)
  5548. {
  5549. GUIConfiguration->MaxWatchDirectories = MaxDirectories;
  5550. }
  5551. }
  5552. else
  5553. {
  5554. Abort();
  5555. }
  5556. }
  5557. }
  5558. //---------------------------------------------------------------------------
  5559. void __fastcall TCustomScpExplorerForm::Synchronize(const UnicodeString LocalDirectory,
  5560. const UnicodeString RemoteDirectory, TSynchronizeMode Mode,
  5561. const TCopyParamType & CopyParam, int Params, TSynchronizeChecklist ** Checklist,
  5562. TSynchronizeOptions * Options)
  5563. {
  5564. // Keep-up-to-date mode
  5565. DebugAssert(!FAutoOperation);
  5566. void * BatchStorage;
  5567. BatchStart(BatchStorage);
  5568. FAutoOperation = true;
  5569. bool AnyOperation = false;
  5570. TDateTime StartTime = Now();
  5571. TSynchronizeChecklist * AChecklist = NULL;
  5572. try
  5573. {
  5574. FSynchronizeProgressForm = new TSynchronizeProgressForm(Application, true, -1);
  5575. if (FLAGCLEAR(Params, TTerminal::spDelayProgress))
  5576. {
  5577. FSynchronizeProgressForm->Start();
  5578. }
  5579. AChecklist = Terminal->SynchronizeCollect(LocalDirectory, RemoteDirectory,
  5580. static_cast<TTerminal::TSynchronizeMode>(Mode),
  5581. &CopyParam, Params | TTerminal::spNoConfirmation, TerminalSynchronizeDirectory,
  5582. Options);
  5583. SetTaskbarListProgressState(TBPF_NOPROGRESS);
  5584. SAFE_DESTROY(FSynchronizeProgressForm);
  5585. AnyOperation = (AChecklist->CheckedCount > 0);
  5586. if (AnyOperation)
  5587. {
  5588. TSynchronizeProgress SynchronizeProgress(AChecklist);
  5589. CreateProgressForm(&SynchronizeProgress);
  5590. try
  5591. {
  5592. Terminal->SynchronizeApply(
  5593. AChecklist, &CopyParam, Params | TTerminal::spNoConfirmation, TerminalSynchronizeDirectory,
  5594. SynchronizeProcessedItem, NULL, NULL, NULL);
  5595. }
  5596. __finally
  5597. {
  5598. DestroyProgressForm();
  5599. }
  5600. }
  5601. }
  5602. __finally
  5603. {
  5604. if (Checklist == NULL)
  5605. {
  5606. delete AChecklist;
  5607. }
  5608. else
  5609. {
  5610. *Checklist = AChecklist;
  5611. }
  5612. FAutoOperation = false;
  5613. SAFE_DESTROY(FSynchronizeProgressForm);
  5614. BatchEnd(BatchStorage);
  5615. ReloadLocalDirectory();
  5616. if (AnyOperation)
  5617. {
  5618. OperationComplete(StartTime);
  5619. }
  5620. }
  5621. }
  5622. //---------------------------------------------------------------------------
  5623. bool __fastcall TCustomScpExplorerForm::SynchronizeAllowSelectedOnly()
  5624. {
  5625. // can be called from command line
  5626. return Visible &&
  5627. ((DirView(osRemote)->SelCount > 0) ||
  5628. (HasDirView[osLocal] && (DirView(osLocal)->SelCount > 0)));
  5629. }
  5630. //---------------------------------------------------------------------------
  5631. void __fastcall TCustomScpExplorerForm::SynchronizeSessionLog(const UnicodeString & Message)
  5632. {
  5633. LogSynchronizeEvent(Terminal, Message);
  5634. }
  5635. //---------------------------------------------------------------------------
  5636. void __fastcall TCustomScpExplorerForm::GetSynchronizeOptions(
  5637. int Params, TSynchronizeOptions & Options)
  5638. {
  5639. DebugAssert(!IsLocalBrowserMode());
  5640. if (FLAGSET(Params, TTerminal::spSelectedOnly) && SynchronizeAllowSelectedOnly())
  5641. {
  5642. Options.Filter = new TStringList();
  5643. Options.Filter->CaseSensitive = false;
  5644. Options.Filter->Duplicates = Types::dupAccept;
  5645. if (DirView(osRemote)->SelCount > 0)
  5646. {
  5647. DirView(osRemote)->CreateFileList(false, false, Options.Filter);
  5648. }
  5649. if (HasDirView[osLocal] && (DirView(osLocal)->SelCount > 0))
  5650. {
  5651. DirView(osLocal)->CreateFileList(false, false, Options.Filter);
  5652. }
  5653. Options.Filter->Sort();
  5654. }
  5655. }
  5656. //---------------------------------------------------------------------------
  5657. UnicodeString __fastcall TCustomScpExplorerForm::SerializeCopyParamForCommandLine(const TCopyParamType * CopyParams)
  5658. {
  5659. TCopyParamType Defaults;
  5660. std::unique_ptr<THierarchicalStorage> ConfigStorage(Configuration->CreateConfigStorage());
  5661. ConfigStorage->AccessMode = smRead;
  5662. if (ConfigStorage->OpenSubKey(Configuration->ConfigurationSubKey, false))
  5663. {
  5664. GUIConfiguration->LoadCopyParam(ConfigStorage.get(), &Defaults);
  5665. }
  5666. ConfigStorage.reset(NULL);
  5667. std::unique_ptr<TStringList> Options(new TStringList());
  5668. std::unique_ptr<TOptionsStorage> OptionsStorage(new TOptionsStorage(Options.get(), true));
  5669. CopyParams->Save(OptionsStorage.get(), &Defaults);
  5670. UnicodeString Result;
  5671. if (Options->Count > 0)
  5672. {
  5673. Result =
  5674. FORMAT(L" %s%s", (TProgramParams::FormatSwitch(RAWTRANSFERSETTINGS_SWITCH), StringsToParams(Options.get())));
  5675. }
  5676. return Result;
  5677. }
  5678. //---------------------------------------------------------------------------
  5679. void __fastcall TCustomScpExplorerForm::SynchronizeInNewWindow(
  5680. const TSynchronizeParamType & Params, const TCopyParamType * CopyParams)
  5681. {
  5682. UnicodeString SessionName = CreateHiddenDuplicateSession();
  5683. UnicodeString AdditionalParams =
  5684. FORMAT(L"%s%s %s // \"%s\" \"%s\" %d %d", (
  5685. TProgramParams::FormatSwitch(DEFAULTS_SWITCH),
  5686. SerializeCopyParamForCommandLine(CopyParams),
  5687. TProgramParams::FormatSwitch(KEEP_UP_TO_DATE_SWITCH),
  5688. Params.LocalDirectory, Params.RemoteDirectory, Params.Params, Params.Options));
  5689. ExecuteNewInstance(SessionName, AdditionalParams);
  5690. }
  5691. //---------------------------------------------------------------------------
  5692. struct TSynchronizeParams
  5693. {
  5694. UnicodeString LocalDirectory;
  5695. UnicodeString RemoteDirectory;
  5696. TSynchronizeMode Mode;
  5697. TSynchronizeChecklist * Checklist;
  5698. int Params;
  5699. TCopyParamType * CopyParam;
  5700. TDateTime CollectElapsed;
  5701. TDateTime * StartTime;
  5702. TProcessedSynchronizationChecklistItem OnProcessedItem;
  5703. };
  5704. //---------------------------------------------------------------------------
  5705. void __fastcall TCustomScpExplorerForm::FullSynchronize(
  5706. TSynchronizeParams & Params, TProcessedSynchronizationChecklistItem OnProcessedItem,
  5707. TUpdatedSynchronizationChecklistItems OnUpdatedSynchronizationChecklistItems)
  5708. {
  5709. DebugAssert(!FAutoOperation);
  5710. void * BatchStorage;
  5711. BatchStart(BatchStorage);
  5712. FAutoOperation = true;
  5713. TFileOperationStatistics Statistics;
  5714. TDateTime Start = Now();
  5715. try
  5716. {
  5717. Params.OnProcessedItem = OnProcessedItem;
  5718. TSynchronizeProgress SynchronizeProgress(Params.Checklist);
  5719. CreateProgressForm(&SynchronizeProgress);
  5720. Terminal->SynchronizeApply(
  5721. Params.Checklist, Params.CopyParam, Params.Params | TTerminal::spNoConfirmation,
  5722. TerminalSynchronizeDirectory, SynchronizeProcessedItem, OnUpdatedSynchronizationChecklistItems, &Params,
  5723. &Statistics);
  5724. }
  5725. __finally
  5726. {
  5727. Params.OnProcessedItem = NULL;
  5728. FAutoOperation = false;
  5729. DestroyProgressForm();
  5730. BatchEnd(BatchStorage);
  5731. ReloadLocalDirectory();
  5732. }
  5733. if (WinConfiguration->SynchronizeSummary)
  5734. {
  5735. UnicodeString Message = MainInstructions(LoadStr(SYNCHRONIZE_COMPLETE)) + L"\n";
  5736. // The statistics should be 0 anyway in this case
  5737. if (FLAGCLEAR(Params.Params, TTerminal::spTimestamp))
  5738. {
  5739. Message += L"\n";
  5740. if (Statistics.FilesUploaded > 0)
  5741. {
  5742. Message += FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 1), (FormatNumber(Statistics.FilesUploaded), FormatBytes(Statistics.TotalUploaded))) + L"\n";
  5743. }
  5744. if (Statistics.FilesDownloaded > 0)
  5745. {
  5746. Message += FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 2), (FormatNumber(Statistics.FilesDownloaded), FormatBytes(Statistics.TotalDownloaded))) + L"\n";
  5747. }
  5748. if (Statistics.FilesDeletedLocal > 0)
  5749. {
  5750. Message += FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 3), (FormatNumber(Statistics.FilesDeletedLocal))) + L"\n";
  5751. }
  5752. if (Statistics.FilesDeletedRemote > 0)
  5753. {
  5754. Message += FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 4), (FormatNumber(Statistics.FilesDeletedRemote))) + L"\n";
  5755. }
  5756. }
  5757. TDateTime Elapsed = (Now() - Start);
  5758. Message +=
  5759. L"\n" +
  5760. FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 5), (FormatDateTimeSpan(Configuration->TimeFormat, Params.CollectElapsed))) + L"\n" +
  5761. FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 6), (FormatDateTimeSpan(Configuration->TimeFormat, Elapsed)));
  5762. TMessageParams Params(mpNeverAskAgainCheck);
  5763. unsigned int Result = MessageDialog(Message, qtInformation, qaOK, HELP_NONE, &Params);
  5764. if (Result == qaNeverAskAgain)
  5765. {
  5766. WinConfiguration->SynchronizeSummary = false;
  5767. }
  5768. }
  5769. }
  5770. //---------------------------------------------------------------------------
  5771. void __fastcall TCustomScpExplorerForm::SynchronizeProcessedItem(void * Token, const TSynchronizeChecklist::TItem * ChecklistItem)
  5772. {
  5773. if (DebugAlwaysTrue(FProgressForm != NULL) && DebugAlwaysTrue(FProgressForm->SynchronizeProgress != NULL))
  5774. {
  5775. FProgressForm->SynchronizeProgress->ItemProcessed(ChecklistItem);
  5776. }
  5777. // Not set in keep-up-to-date mode - Synchronize() method
  5778. if (Token != NULL)
  5779. {
  5780. TSynchronizeParams & Params = *static_cast<TSynchronizeParams *>(Token);
  5781. if (Params.OnProcessedItem != NULL)
  5782. {
  5783. Params.OnProcessedItem(NULL, ChecklistItem);
  5784. }
  5785. }
  5786. }
  5787. //---------------------------------------------------------------------------
  5788. void __fastcall TCustomScpExplorerForm::DoFullSynchronize(
  5789. void * Token, TProcessedSynchronizationChecklistItem OnProcessedItem,
  5790. TUpdatedSynchronizationChecklistItems OnUpdatedSynchronizationChecklistItems)
  5791. {
  5792. TSynchronizeParams & Params = *static_cast<TSynchronizeParams *>(Token);
  5793. *Params.StartTime = Now();
  5794. FullSynchronize(Params, OnProcessedItem, OnUpdatedSynchronizationChecklistItems);
  5795. }
  5796. //---------------------------------------------------------------------------
  5797. void __fastcall TCustomScpExplorerForm::DoSynchronizeChecklistCalculateSize(
  5798. TSynchronizeChecklist * Checklist, const TSynchronizeChecklist::TItemList & Items, void * Token)
  5799. {
  5800. // terminal can be already closed (e.g. dropped connection)
  5801. if (Terminal != NULL)
  5802. {
  5803. TSynchronizeParams & Params = *static_cast<TSynchronizeParams *>(Token);
  5804. Terminal->SynchronizeChecklistCalculateSize(Checklist, Items, Params.CopyParam);
  5805. }
  5806. }
  5807. //---------------------------------------------------------------------------
  5808. void __fastcall TCustomScpExplorerForm::DoSynchronizeMove(
  5809. TOperationSide Side, const UnicodeString & FileName, const UnicodeString & NewFileName, TRemoteFile * RemoteFile)
  5810. {
  5811. DebugAssert(!IsLocalBrowserMode());
  5812. TAutoBatch AutoBatch(this);
  5813. TAutoFlag AutoOperationFlag(FAutoOperation);
  5814. if (Side == osRemote)
  5815. {
  5816. std::unique_ptr<TStrings> FileList(new TStringList());
  5817. FileList->AddObject(FileName, RemoteFile);
  5818. UnicodeString Target = UnixExtractFileDir(NewFileName);
  5819. UnicodeString FileMask = DelimitFileNameMask(UnixExtractFileName(NewFileName));
  5820. RemoteDirView->SaveSelection();
  5821. RemoteDirView->SaveSelectedNames();
  5822. try
  5823. {
  5824. Terminal->MoveFiles(FileList.get(), Target, FileMask, false);
  5825. }
  5826. catch(...)
  5827. {
  5828. RemoteDirView->DiscardSavedSelection();
  5829. throw;
  5830. }
  5831. RemoteDirView->RestoreSelection();
  5832. }
  5833. else if (DebugAlwaysTrue(Side == osLocal))
  5834. {
  5835. if (!MoveFile(FileName.c_str(), NewFileName.c_str()))
  5836. {
  5837. throw EOSExtException(FMTLOAD(RENAME_FILE_ERROR, (FileName, NewFileName)));
  5838. }
  5839. UnicodeString Directory = ExtractFileDir(FileName);
  5840. ReloadLocalDirectory(Directory);
  5841. UnicodeString NewDirectory = ExtractFileDir(NewFileName);
  5842. if (!SamePaths(Directory, NewDirectory))
  5843. {
  5844. ReloadLocalDirectory(NewDirectory);
  5845. }
  5846. }
  5847. }
  5848. //---------------------------------------------------------------------------
  5849. void __fastcall TCustomScpExplorerForm::DoSynchronizeBrowse(TOperationSide Side, TSynchronizeChecklist::TAction Action, const TSynchronizeChecklist::TItem * Item)
  5850. {
  5851. UnicodeString LocalPath = ExcludeTrailingBackslash(Item->Local.Directory);
  5852. if (Side == osLocal)
  5853. {
  5854. if (Action == TSynchronizeChecklist::saDownloadNew)
  5855. {
  5856. OpenFolderInExplorer(LocalPath);
  5857. }
  5858. else
  5859. {
  5860. OpenFileInExplorer(TPath::Combine(LocalPath, Item->GetFileName()));
  5861. }
  5862. }
  5863. else if (DebugAlwaysTrue(Side == osRemote))
  5864. {
  5865. // Similar to CreateHiddenDuplicateSession, except that it modifies the initial directories
  5866. std::unique_ptr<TSessionData> SessionData(CloneCurrentSessionData());
  5867. SessionData->RemoteDirectory = UnixExcludeTrailingBackslash(Item->Remote.Directory);
  5868. if (!LocalPath.IsEmpty())
  5869. {
  5870. SessionData->LocalDirectory = LocalPath;
  5871. }
  5872. UnicodeString SessionName = SaveHiddenDuplicateSession(SessionData.get());
  5873. ExecuteNewInstance(SessionName, FORMAT(L"%s=\"%s\"", (TProgramParams::FormatSwitch(BROWSE_SWITCH), Item->GetFileName())));
  5874. }
  5875. }
  5876. //---------------------------------------------------------------------------
  5877. void __fastcall TCustomScpExplorerForm::FullSynchronizeInNewWindow(
  5878. TSynchronizeMode Mode, int Params, const UnicodeString & LocalDirectory, const UnicodeString & RemoteDirectory,
  5879. const TCopyParamType * CopyParams)
  5880. {
  5881. UnicodeString SessionName = CreateHiddenDuplicateSession();
  5882. UnicodeString AdditionalParams =
  5883. FORMAT(L"%s%s %s // \"%s\" \"%s\" %d %d", (
  5884. TProgramParams::FormatSwitch(DEFAULTS_SWITCH),
  5885. SerializeCopyParamForCommandLine(CopyParams),
  5886. TProgramParams::FormatSwitch(SYNCHRONIZE_SWITCH),
  5887. LocalDirectory, RemoteDirectory, Mode, Params));
  5888. ExecuteNewInstance(SessionName, AdditionalParams);
  5889. }
  5890. //---------------------------------------------------------------------------
  5891. int __fastcall TCustomScpExplorerForm::DoFullSynchronizeDirectories(
  5892. UnicodeString & LocalDirectory, UnicodeString & RemoteDirectory,
  5893. TSynchronizeMode & Mode, int Params, bool & SaveMode, int UseDefaults)
  5894. {
  5895. int Result;
  5896. bool SaveSettings = false;
  5897. int Options =
  5898. FLAGMASK(!Terminal->IsCapable[fcTimestampChanging], fsoDisableTimestamp) |
  5899. FLAGMASK(SynchronizeAllowSelectedOnly(), fsoAllowSelectedOnly);
  5900. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  5901. TUsableCopyParamAttrs CopyParamAttrs = Terminal->UsableCopyParamAttrs(0);
  5902. bool Continue =
  5903. (UseDefaults == 0) ||
  5904. DoFullSynchronizeDialog(Mode, Params, LocalDirectory, RemoteDirectory,
  5905. &CopyParam, SaveSettings, SaveMode, Options, CopyParamAttrs, FullSynchronizeInNewWindow, UseDefaults);
  5906. if (Continue)
  5907. {
  5908. Configuration->Usage->Inc(L"Synchronizations");
  5909. CopyParam.IncludeFileMask.SetRoots(LocalDirectory, RemoteDirectory);
  5910. UpdateCopyParamCounters(CopyParam);
  5911. TSynchronizeOptions SynchronizeOptions;
  5912. GetSynchronizeOptions(Params, SynchronizeOptions);
  5913. if (SaveSettings)
  5914. {
  5915. GUIConfiguration->SynchronizeParams = Params;
  5916. }
  5917. else
  5918. {
  5919. SaveMode = false;
  5920. }
  5921. TDateTime StartTime = Now();
  5922. TSynchronizeChecklist * Checklist = NULL;
  5923. try
  5924. {
  5925. DebugAssert(!FAutoOperation);
  5926. FAutoOperation = true;
  5927. try
  5928. {
  5929. UnicodeString SessionKey = Terminal->SessionData->SessionKey;
  5930. std::unique_ptr<TStrings> DataList(Configuration->LoadDirectoryStatisticsCache(SessionKey, RemoteDirectory, CopyParam));
  5931. int Files = -1;
  5932. if (DataList->Count >= 1)
  5933. {
  5934. Files = StrToIntDef(DataList->Strings[0], Files);
  5935. }
  5936. else
  5937. {
  5938. DataList->Add(UnicodeString());
  5939. }
  5940. FSynchronizeProgressForm = new TSynchronizeProgressForm(Application, true, Files);
  5941. FSynchronizeProgressForm->Start();
  5942. Checklist = Terminal->SynchronizeCollect(LocalDirectory, RemoteDirectory,
  5943. static_cast<TTerminal::TSynchronizeMode>(Mode),
  5944. &CopyParam, Params | TTerminal::spNoConfirmation, TerminalSynchronizeDirectory,
  5945. &SynchronizeOptions);
  5946. if (Terminal->SessionData->CacheDirectories)
  5947. {
  5948. DataList->Strings[0] = IntToStr(SynchronizeOptions.Files);
  5949. Configuration->SaveDirectoryStatisticsCache(SessionKey, RemoteDirectory, CopyParam, DataList.get());
  5950. }
  5951. }
  5952. __finally
  5953. {
  5954. FAutoOperation = false;
  5955. SetTaskbarListProgressState(TBPF_NOPROGRESS);
  5956. SAFE_DESTROY(FSynchronizeProgressForm);
  5957. }
  5958. TSynchronizeParams SynchronizeParams;
  5959. SynchronizeParams.LocalDirectory = LocalDirectory;
  5960. SynchronizeParams.RemoteDirectory = RemoteDirectory;
  5961. SynchronizeParams.Mode = Mode;
  5962. SynchronizeParams.CopyParam = &CopyParam;
  5963. SynchronizeParams.Params = Params;
  5964. SynchronizeParams.Checklist = Checklist;
  5965. SynchronizeParams.CollectElapsed = (Now() - StartTime);
  5966. SynchronizeParams.StartTime = &StartTime;
  5967. SynchronizeParams.OnProcessedItem = NULL;
  5968. Result = Checklist->Count;
  5969. if (Checklist->Count > 0)
  5970. {
  5971. if (FLAGSET(Params, TTerminal::spPreviewChanges))
  5972. {
  5973. if (!DoSynchronizeChecklistDialog(
  5974. Checklist, Mode, Params, LocalDirectory, RemoteDirectory, CustomCommandMenu, DoFullSynchronize,
  5975. DoSynchronizeChecklistCalculateSize, DoSynchronizeMove, DoSynchronizeBrowse, &SynchronizeParams))
  5976. {
  5977. Result = -1;
  5978. }
  5979. }
  5980. else
  5981. {
  5982. FullSynchronize(SynchronizeParams, NULL, NULL);
  5983. }
  5984. }
  5985. else
  5986. {
  5987. UnicodeString Message = MainInstructions(LoadStr(COMPARE_NO_DIFFERENCES));
  5988. MessageDialog(Message, qtInformation, qaOK, HELP_SYNCHRONIZE_NO_DIFFERENCES);
  5989. }
  5990. }
  5991. __finally
  5992. {
  5993. delete Checklist;
  5994. }
  5995. OperationComplete(StartTime);
  5996. }
  5997. else
  5998. {
  5999. Result = -1;
  6000. }
  6001. return Result;
  6002. }
  6003. //---------------------------------------------------------------------------
  6004. void __fastcall TCustomScpExplorerForm::TerminalSynchronizeDirectory(
  6005. const UnicodeString & LocalDirectory, const UnicodeString & RemoteDirectory,
  6006. bool & Continue, bool Collect, const TSynchronizeOptions * Options)
  6007. {
  6008. if (Collect)
  6009. {
  6010. DebugAssert(FSynchronizeProgressForm != NULL);
  6011. if (!FSynchronizeProgressForm->Started)
  6012. {
  6013. FSynchronizeProgressForm->Start();
  6014. }
  6015. int CompareProgress = FSynchronizeProgressForm->SetData(LocalDirectory, RemoteDirectory, Options->Files, Continue);
  6016. SetTaskbarListProgressValue(CompareProgress);
  6017. }
  6018. else
  6019. {
  6020. DebugAssert(FSynchronizeProgressForm == NULL);
  6021. DebugAssert(FProgressForm != NULL);
  6022. }
  6023. }
  6024. //---------------------------------------------------------------------------
  6025. void __fastcall TCustomScpExplorerForm::StandaloneEdit(const UnicodeString & FileName)
  6026. {
  6027. UnicodeString FullFileName = AbsolutePath(Terminal->CurrentDirectory, FileName);
  6028. std::unique_ptr<TRemoteFile> File(Terminal->ReadFile(FullFileName));
  6029. if (File.get() != NULL)
  6030. {
  6031. ExecuteRemoteFile(FullFileName, File.get(), efInternalEditor);
  6032. Application->ShowMainForm = false;
  6033. Application->Run();
  6034. }
  6035. }
  6036. //---------------------------------------------------------------------------
  6037. void __fastcall TCustomScpExplorerForm::ExploreLocalDirectory(TOperationSide)
  6038. {
  6039. DebugFail();
  6040. }
  6041. //---------------------------------------------------------------------------
  6042. TSessionData * __fastcall TCustomScpExplorerForm::CloneCurrentSessionData()
  6043. {
  6044. std::unique_ptr<TSessionData> SessionData(new TSessionData(L""));
  6045. SessionData->Assign(ManagedSession->SessionData);
  6046. UpdateSessionData(SessionData.get());
  6047. DebugAssert(SessionData->IsLocalBrowser == IsLocalBrowserMode());
  6048. if (IsActiveTerminal(ManagedSession))
  6049. {
  6050. ManagedSession->UpdateSessionCredentials(SessionData.get());
  6051. }
  6052. if (!ManagedSession->SessionData->HasSessionName())
  6053. {
  6054. // Particularly for "Workspace/XXXX" name, we need to reset it, as it would become user-visible
  6055. // once IsWorkspace is cleared
  6056. SessionData->Name = UnicodeString();
  6057. }
  6058. // Most uses of this method ends with saving the session, and we do not want to store the IsWorkspace flag.
  6059. // Particularly, with SaveHiddenDuplicateSession, it won't work as TSessionData::ParseUrl explicitly ignored workspace sessions.
  6060. SessionData->IsWorkspace = false;
  6061. return SessionData.release();
  6062. }
  6063. //---------------------------------------------------------------------------
  6064. void __fastcall TCustomScpExplorerForm::SaveCurrentSession()
  6065. {
  6066. TSessionData * SessionData = CloneCurrentSessionData();
  6067. try
  6068. {
  6069. TSessionData * EditingSessionData = StoredSessions->FindSame(SessionData);
  6070. DoSaveSession(SessionData, EditingSessionData, true, NULL);
  6071. }
  6072. __finally
  6073. {
  6074. delete SessionData;
  6075. }
  6076. }
  6077. //---------------------------------------------------------------------------
  6078. TObjectList * __fastcall TCustomScpExplorerForm::DoCollectWorkspace()
  6079. {
  6080. TTerminalManager * Manager = TTerminalManager::Instance();
  6081. std::unique_ptr<TObjectList> DataList(new TObjectList());
  6082. if (DebugAlwaysTrue(ManagedSession != NULL))
  6083. {
  6084. // Update ManagedSession->StateData
  6085. UpdateSession(ManagedSession);
  6086. }
  6087. Manager->SaveWorkspace(DataList.get());
  6088. return DataList.release();
  6089. }
  6090. //---------------------------------------------------------------------------
  6091. void __fastcall TCustomScpExplorerForm::DoSaveWorkspace(
  6092. const UnicodeString & Name, TObjectList * DataList, bool SavePasswords, bool Explicit)
  6093. {
  6094. WinConfiguration->LastWorkspace = Name;
  6095. if (!SavePasswords)
  6096. {
  6097. for (int Index = 0; Index < DataList->Count; Index++)
  6098. {
  6099. TSessionData * SessionData = dynamic_cast<TSessionData *>(DataList->Items[Index]);
  6100. if (SessionData->Link.IsEmpty())
  6101. {
  6102. SessionData->ClearSessionPasswords();
  6103. }
  6104. }
  6105. }
  6106. StoredSessions->NewWorkspace(Name, DataList);
  6107. // modified only
  6108. StoredSessions->Save(false, Explicit);
  6109. }
  6110. //---------------------------------------------------------------------------
  6111. UnicodeString __fastcall TCustomScpExplorerForm::WorkspaceName()
  6112. {
  6113. return
  6114. DefaultStr(WinConfiguration->LastWorkspace,
  6115. DefaultStr(WinConfiguration->AutoWorkspace, LoadStr(NEW_WORKSPACE)));
  6116. }
  6117. //---------------------------------------------------------------------------
  6118. bool __fastcall TCustomScpExplorerForm::SaveWorkspace(bool EnableAutoSave)
  6119. {
  6120. std::unique_ptr<TObjectList> DataList(DoCollectWorkspace());
  6121. bool AnyNonStoredSessionWithPassword = false;
  6122. bool AnyNonStoredNonWorkspaceSessionWithPassword = false;
  6123. bool AllNonStoredSessionsAnonymous = true;
  6124. TTerminalManager * Manager = TTerminalManager::Instance();
  6125. for (int Index = 0; Index < DataList->Count; Index++)
  6126. {
  6127. TSessionData * SessionData = dynamic_cast<TSessionData *>(DataList->Items[Index]);
  6128. if (SessionData->Link.IsEmpty())
  6129. {
  6130. if (SessionData->HasAnySessionPassword())
  6131. {
  6132. AnyNonStoredSessionWithPassword = true;
  6133. if (!Manager->Sessions[Index]->SessionData->IsWorkspace)
  6134. {
  6135. AnyNonStoredNonWorkspaceSessionWithPassword = true;
  6136. }
  6137. }
  6138. if (!SameText(SessionData->UserName, AnonymousUserName))
  6139. {
  6140. AllNonStoredSessionsAnonymous = false;
  6141. }
  6142. }
  6143. }
  6144. bool SavePasswords;
  6145. bool * PSavePasswords;
  6146. bool NotRecommendedSavingPasswords =
  6147. !CustomWinConfiguration->UseMasterPassword &&
  6148. !AllNonStoredSessionsAnonymous;
  6149. if (Configuration->DisablePasswordStoring ||
  6150. !AnyNonStoredSessionWithPassword)
  6151. {
  6152. PSavePasswords = NULL;
  6153. SavePasswords = false;
  6154. }
  6155. else
  6156. {
  6157. PSavePasswords = &SavePasswords;
  6158. SavePasswords =
  6159. !AnyNonStoredNonWorkspaceSessionWithPassword ||
  6160. !NotRecommendedSavingPasswords;
  6161. }
  6162. UnicodeString Name = WorkspaceName();
  6163. bool CreateShortcut = false;
  6164. bool Result =
  6165. DoSaveWorkspaceDialog(
  6166. Name, PSavePasswords, NotRecommendedSavingPasswords, CreateShortcut,
  6167. EnableAutoSave);
  6168. if (Result)
  6169. {
  6170. DoSaveWorkspace(Name, DataList.get(), SavePasswords, true);
  6171. if (CreateShortcut)
  6172. {
  6173. TOperationVisualizer Visualizer;
  6174. UnicodeString AdditionalParams = TProgramParams::FormatSwitch(DESKTOP_SWITCH);
  6175. CreateDesktopSessionShortCut(Name, L"", AdditionalParams, -1, WORKSPACE_ICON);
  6176. }
  6177. if (EnableAutoSave)
  6178. {
  6179. WinConfiguration->AutoSaveWorkspace = true;
  6180. WinConfiguration->AutoWorkspace = Name;
  6181. if (PSavePasswords != NULL)
  6182. {
  6183. WinConfiguration->AutoSaveWorkspacePasswords = SavePasswords;
  6184. }
  6185. }
  6186. }
  6187. return Result;
  6188. }
  6189. //---------------------------------------------------------------------------
  6190. void __fastcall TCustomScpExplorerForm::UpdateSession(TManagedTerminal * Session)
  6191. {
  6192. DebugAssert(IsLocalBrowserMode() == Session->LocalBrowser);
  6193. DebugAssert(Session == ManagedSession);
  6194. if (!IsLocalBrowserMode())
  6195. {
  6196. SAFE_DESTROY(Session->RemoteExplorerState);
  6197. if (WinConfiguration->PreservePanelState)
  6198. {
  6199. Session->RemoteExplorerState = RemoteDirView->SaveState();
  6200. }
  6201. }
  6202. UpdateSessionData(Session->StateData);
  6203. }
  6204. //---------------------------------------------------------------------------
  6205. void __fastcall TCustomScpExplorerForm::UpdateSessionData(TSessionData * Data)
  6206. {
  6207. // Keep in sync with TSessionData::CopyStateData
  6208. DebugAssert(Data != NULL);
  6209. // This is inconsistent with how color (for example) is handled.
  6210. Data->Name = ManagedSession->SessionData->Name;
  6211. if (!IsLocalBrowserMode())
  6212. {
  6213. // cannot use RemoteDirView->Path, because it is empty if connection
  6214. // was already closed
  6215. // also only peek, we may not be connected at all atm
  6216. // so make sure we do not try retrieving current directory from the server
  6217. // (particularly with FTP)
  6218. UnicodeString ACurrentDirectory = Terminal->PeekCurrentDirectory();
  6219. if (!ACurrentDirectory.IsEmpty())
  6220. {
  6221. Data->RemoteDirectory = ACurrentDirectory;
  6222. }
  6223. }
  6224. Data->Color = SessionColor;
  6225. }
  6226. //---------------------------------------------------------------------------
  6227. void __fastcall TCustomScpExplorerForm::ToolBarResize(TObject *Sender)
  6228. {
  6229. TTBXToolbar * Toolbar = dynamic_cast<TTBXToolbar*>(Sender);
  6230. DebugAssert(Toolbar != NULL);
  6231. for (int i = 0; i < Toolbar->Items->Count; i++)
  6232. {
  6233. TTBXCustomDropDownItem * DropDownItem;
  6234. DropDownItem = dynamic_cast<TTBXCustomDropDownItem *>(Toolbar->Items->Items[i]);
  6235. if (DropDownItem != NULL)
  6236. {
  6237. ToolbarItemResize(DropDownItem,
  6238. Toolbar->Width - (Toolbar->View->BaseSize.x - DropDownItem->EditWidth) -
  6239. Toolbar->NonClientWidth);
  6240. break;
  6241. }
  6242. }
  6243. }
  6244. //---------------------------------------------------------------------------
  6245. void __fastcall TCustomScpExplorerForm::ToolbarItemResize(TTBXCustomDropDownItem * Item, int Width)
  6246. {
  6247. Item->EditWidth = Width;
  6248. }
  6249. //---------------------------------------------------------------------------
  6250. void __fastcall TCustomScpExplorerForm::ToolbarGetBaseSize(
  6251. TTBCustomToolbar * Toolbar, TPoint & ASize)
  6252. {
  6253. for (int i = 0; i < Toolbar->Items->Count; i++)
  6254. {
  6255. TTBXCustomDropDownItem * DropDownItem;
  6256. DropDownItem = dynamic_cast<TTBXCustomDropDownItem *>(Toolbar->Items->Items[i]);
  6257. if (DropDownItem != NULL)
  6258. {
  6259. ASize.x -= DropDownItem->EditWidth;
  6260. ASize.x += ScaleByTextHeight(this, 50) /* minimal combo width */;
  6261. }
  6262. }
  6263. }
  6264. //---------------------------------------------------------------------------
  6265. void __fastcall TCustomScpExplorerForm::DoWarnLackOfTempSpace(
  6266. const UnicodeString Path, __int64 RequiredSpace, bool & Continue)
  6267. {
  6268. if (WinConfiguration->DDWarnLackOfTempSpace)
  6269. {
  6270. UnicodeString ADrive = ExtractFileDrive(ExpandFileName(Path));
  6271. if (!ADrive.IsEmpty())
  6272. {
  6273. __int64 FreeSpace = DiskFree((Byte)(ADrive[1]-'A'+1));
  6274. DebugAssert(RequiredSpace >= 0);
  6275. __int64 RequiredWithReserve;
  6276. RequiredWithReserve = (__int64)(RequiredSpace * WinConfiguration->DDWarnLackOfTempSpaceRatio);
  6277. if (FreeSpace < RequiredWithReserve)
  6278. {
  6279. unsigned int Result;
  6280. TMessageParams Params(mpNeverAskAgainCheck);
  6281. Result = MessageDialog(FMTLOAD(DD_WARN_LACK_OF_TEMP_SPACE, (Path,
  6282. FormatBytes(FreeSpace), FormatBytes(RequiredSpace))),
  6283. qtWarning, qaYes | qaNo, HELP_DD_WARN_LACK_OF_TEMP_SPACE, &Params);
  6284. if (Result == qaNeverAskAgain)
  6285. {
  6286. WinConfiguration->DDWarnLackOfTempSpace = false;
  6287. }
  6288. Continue = (Result == qaYes || Result == qaNeverAskAgain);
  6289. }
  6290. }
  6291. }
  6292. }
  6293. //---------------------------------------------------------------------------
  6294. void __fastcall TCustomScpExplorerForm::AddBookmark(TOperationSide Side)
  6295. {
  6296. DoOpenDirectoryDialog(odAddBookmark, Side);
  6297. }
  6298. //---------------------------------------------------------------------------
  6299. TStrings * __fastcall TCustomScpExplorerForm::CreateVisitedDirectories(TOperationSide Side)
  6300. {
  6301. // we should better use TCustomDirView::FCaseSensitive, but it is private
  6302. TStringList * VisitedDirectories = CreateSortedStringList(!IsSideLocalBrowser(Side));
  6303. try
  6304. {
  6305. TCustomDirView * DView = DirView(Side);
  6306. for (int Index = -DView->BackCount; Index <= DView->ForwardCount; Index++)
  6307. {
  6308. VisitedDirectories->Add(DView->HistoryPath[Index]);
  6309. }
  6310. }
  6311. catch (...)
  6312. {
  6313. delete VisitedDirectories;
  6314. throw;
  6315. }
  6316. return VisitedDirectories;
  6317. }
  6318. //---------------------------------------------------------------------------
  6319. void __fastcall TCustomScpExplorerForm::DoOpenDirectoryDialog(
  6320. TOpenDirectoryMode Mode, TOperationSide Side)
  6321. {
  6322. bool Continue = true;
  6323. if (Mode == odAddBookmark)
  6324. {
  6325. TMessageParams Params(mpNeverAskAgainCheck);
  6326. Params.NeverAskAgainTitle = LoadStr(ADD_BOOKMARK_SHARED);
  6327. Params.NeverAskAgainCheckedInitially = WinConfiguration->UseSharedBookmarks;
  6328. UnicodeString Message = MainInstructions(FMTLOAD(ADD_BOOKMARK_CONFIRM, (DirView(Side)->PathName)));
  6329. unsigned int Answer =
  6330. MessageDialog(Message,
  6331. qtConfirmation, qaOK | qaCancel, HELP_ADD_BOOKMARK_CONFIRM, &Params);
  6332. if (Answer == qaNeverAskAgain)
  6333. {
  6334. Continue = true;
  6335. WinConfiguration->UseSharedBookmarks = true;
  6336. }
  6337. else if (Answer == qaOK)
  6338. {
  6339. Continue = true;
  6340. WinConfiguration->UseSharedBookmarks = false;
  6341. }
  6342. else
  6343. {
  6344. Continue = false;
  6345. }
  6346. }
  6347. if (Continue)
  6348. {
  6349. TStrings * VisitedDirectories = CreateVisitedDirectories(Side);
  6350. try
  6351. {
  6352. UnicodeString Name = DirView(Side)->PathName;
  6353. bool Repeat;
  6354. do
  6355. {
  6356. Repeat = false;
  6357. TOperationSide BookmarkSide = IsSideLocalBrowser(Side) ? osLocal : osRemote;
  6358. if (::DoOpenDirectoryDialog(Mode, BookmarkSide, Name, VisitedDirectories, Terminal,
  6359. // do not allow switching to location profiles,
  6360. // if we are not connected
  6361. HasDirView[osLocal] && (Terminal != NULL)))
  6362. {
  6363. TWindowLock Lock(this);
  6364. if (!TryOpenDirectory(Side, Name))
  6365. {
  6366. Repeat = true;
  6367. Mode = odBrowse;
  6368. }
  6369. }
  6370. }
  6371. while (Repeat);
  6372. }
  6373. __finally
  6374. {
  6375. delete VisitedDirectories;
  6376. }
  6377. }
  6378. }
  6379. //---------------------------------------------------------------------------
  6380. bool __fastcall TCustomScpExplorerForm::CommandSessionFallback()
  6381. {
  6382. DebugAssert(!Terminal->CommandSessionOpened);
  6383. return TTerminalManager::Instance()->ConnectTerminal(Terminal->CommandSession);
  6384. }
  6385. //---------------------------------------------------------------------------
  6386. bool __fastcall TCustomScpExplorerForm::EnsureCommandSessionFallback(TFSCapability Capability)
  6387. {
  6388. bool Result = Terminal->IsCapable[Capability] ||
  6389. Terminal->CommandSessionOpened;
  6390. if (!Result)
  6391. {
  6392. DebugAssert(Terminal->IsCapable[fcSecondaryShell]);
  6393. if (!GUIConfiguration->ConfirmCommandSession)
  6394. {
  6395. Result = true;
  6396. }
  6397. else
  6398. {
  6399. TMessageParams Params(mpNeverAskAgainCheck);
  6400. const TFileSystemInfo & FileSystemInfo = Terminal->GetFileSystemInfo();
  6401. unsigned int Answer = MessageDialog(FMTLOAD(PERFORM_ON_COMMAND_SESSION2,
  6402. (FileSystemInfo.ProtocolName, FileSystemInfo.ProtocolName)), qtConfirmation,
  6403. qaOK | qaCancel, HELP_PERFORM_ON_COMMAND_SESSION, &Params);
  6404. if (Answer == qaNeverAskAgain)
  6405. {
  6406. GUIConfiguration->ConfirmCommandSession = false;
  6407. Result = true;
  6408. }
  6409. else if (Answer == qaOK)
  6410. {
  6411. Result = true;
  6412. }
  6413. }
  6414. if (Result)
  6415. {
  6416. Result = CommandSessionFallback();
  6417. }
  6418. }
  6419. return Result;
  6420. }
  6421. //---------------------------------------------------------------------------
  6422. void __fastcall TCustomScpExplorerForm::OpenConsole(UnicodeString Command)
  6423. {
  6424. if (EnsureCommandSessionFallback(fcAnyCommand))
  6425. {
  6426. DoConsoleDialog(Terminal, Command);
  6427. }
  6428. }
  6429. //---------------------------------------------------------------------------
  6430. void __fastcall TCustomScpExplorerForm::FileControlDDDragEnter(
  6431. TObject *Sender, _di_IDataObject /*DataObj*/, int /*grfKeyState*/,
  6432. const TPoint & /*Point*/, int & /*dwEffect*/, bool & Accept)
  6433. {
  6434. if (!IsSideLocalBrowser(osRemote) &&
  6435. IsFileControl(DropSourceControl, osRemote) &&
  6436. (FDDExtMapFile != NULL))
  6437. {
  6438. Accept = true;
  6439. }
  6440. FDDTargetControl = dynamic_cast<TControl*>(Sender);
  6441. DebugAssert(FDDTargetControl != NULL);
  6442. }
  6443. //---------------------------------------------------------------------------
  6444. void __fastcall TCustomScpExplorerForm::SessionsDDDragEnter(
  6445. _di_IDataObject DataObj, int KeyState,
  6446. const TPoint & Point, int & Effect, bool & Accept)
  6447. {
  6448. FileControlDDDragEnter(SessionsPageControl, DataObj, KeyState, Point, Effect, Accept);
  6449. }
  6450. //---------------------------------------------------------------------------
  6451. void __fastcall TCustomScpExplorerForm::QueueDDDragEnter(
  6452. _di_IDataObject DataObj, int KeyState,
  6453. const TPoint & Point, int & Effect, bool & Accept)
  6454. {
  6455. FileControlDDDragEnter(QueueView3, DataObj, KeyState, Point, Effect, Accept);
  6456. }
  6457. //---------------------------------------------------------------------------
  6458. void __fastcall TCustomScpExplorerForm::FileControlDDDragLeave(
  6459. TObject * Sender)
  6460. {
  6461. DebugUsedParam(Sender);
  6462. DebugAssert(FDDTargetControl == Sender);
  6463. FDDTargetControl = NULL;
  6464. }
  6465. //---------------------------------------------------------------------------
  6466. void __fastcall TCustomScpExplorerForm::SessionsDDDragLeave()
  6467. {
  6468. FileControlDDDragLeave(SessionsPageControl);
  6469. }
  6470. //---------------------------------------------------------------------------
  6471. void __fastcall TCustomScpExplorerForm::QueueDDDragLeave()
  6472. {
  6473. FileControlDDDragLeave(QueueView3);
  6474. }
  6475. //---------------------------------------------------------------------------
  6476. void __fastcall TCustomScpExplorerForm::AddEditLink(TOperationSide Side, bool Add)
  6477. {
  6478. DebugAssert(!IsSideLocalBrowser(Side));
  6479. DebugUsedParam(Side);
  6480. bool Edit = false;
  6481. TRemoteFile * File = NULL;
  6482. UnicodeString FileName;
  6483. UnicodeString PointTo;
  6484. bool SymbolicLink = true;
  6485. if (RemoteDirView->ItemFocused)
  6486. {
  6487. DebugAssert(RemoteDirView->ItemFocused->Data);
  6488. File = (TRemoteFile *)RemoteDirView->ItemFocused->Data;
  6489. Edit = !Add && File->IsSymLink && Terminal->SessionData->ResolveSymlinks;
  6490. if (Edit)
  6491. {
  6492. FileName = File->FileName;
  6493. PointTo = File->LinkTo;
  6494. }
  6495. else if (!File->IsParentDirectory)
  6496. {
  6497. PointTo = File->FileName;
  6498. }
  6499. }
  6500. if (DoSymlinkDialog(FileName, PointTo, osRemote, SymbolicLink, Edit,
  6501. Terminal->IsCapable[fcHardLink]))
  6502. {
  6503. Configuration->Usage->Inc(L"RemoteLinksCreated");
  6504. if (Edit)
  6505. {
  6506. DebugAssert(File->FileName == FileName);
  6507. int Params = dfNoRecursive;
  6508. Terminal->ExceptionOnFail = true;
  6509. try
  6510. {
  6511. Terminal->DeleteFile(L"", File, &Params);
  6512. }
  6513. __finally
  6514. {
  6515. Terminal->ExceptionOnFail = false;
  6516. }
  6517. }
  6518. Terminal->CreateLink(FileName, PointTo, SymbolicLink);
  6519. }
  6520. }
  6521. //---------------------------------------------------------------------------
  6522. bool __fastcall TCustomScpExplorerForm::CanAddEditLink(TOperationSide Side)
  6523. {
  6524. return
  6525. (IsSideLocalBrowser(Side) ||
  6526. (HasActiveTerminal() &&
  6527. Terminal->ResolvingSymlinks &&
  6528. Terminal->IsCapable[fcSymbolicLink]));
  6529. }
  6530. //---------------------------------------------------------------------------
  6531. bool __fastcall TCustomScpExplorerForm::LinkFocused()
  6532. {
  6533. return
  6534. !IsSideLocalBrowser(FCurrentSide) &&
  6535. (RemoteDirView->ItemFocused != NULL) &&
  6536. ((TRemoteFile *)RemoteDirView->ItemFocused->Data)->IsSymLink &&
  6537. Terminal->SessionData->ResolveSymlinks;
  6538. }
  6539. //---------------------------------------------------------------------------
  6540. void __fastcall TCustomScpExplorerForm::ExecuteCurrentFile()
  6541. {
  6542. DebugAssert(!WinConfiguration->DisableOpenEdit);
  6543. FForceExecution = true;
  6544. try
  6545. {
  6546. DirView(osCurrent)->ExecuteCurrentFile();
  6547. }
  6548. __finally
  6549. {
  6550. FForceExecution = false;
  6551. }
  6552. }
  6553. //---------------------------------------------------------------------------
  6554. void __fastcall TCustomScpExplorerForm::ExecuteCurrentFileWith(bool OnFocused)
  6555. {
  6556. TEditorData ExternalEditor;
  6557. ExternalEditor.Editor = edExternal;
  6558. bool Remember = false;
  6559. if (DoEditorPreferencesDialog(&ExternalEditor, Remember, epmAdHoc, !IsSideLocalBrowser(osCurrent)))
  6560. {
  6561. if (Remember)
  6562. {
  6563. TEditorList * EditorList = new TEditorList;
  6564. try
  6565. {
  6566. *EditorList = *WinConfiguration->EditorList;
  6567. bool Found = false;
  6568. int i = 0;
  6569. while (!Found && (i < EditorList->Count))
  6570. {
  6571. const TEditorPreferences * Editor = EditorList->Editors[i];
  6572. if ((Editor->Data->Editor == edExternal) &&
  6573. (Editor->Data->ExternalEditor == ExternalEditor.ExternalEditor))
  6574. {
  6575. Found = true;
  6576. }
  6577. i++;
  6578. }
  6579. if (!Found)
  6580. {
  6581. EditorList->Add(new TEditorPreferences(ExternalEditor));
  6582. WinConfiguration->EditorList = EditorList;
  6583. }
  6584. }
  6585. __finally
  6586. {
  6587. delete EditorList;
  6588. }
  6589. }
  6590. ExecuteFile(osCurrent, efExternalEditor, &ExternalEditor, true, OnFocused);
  6591. }
  6592. }
  6593. //---------------------------------------------------------------------------
  6594. void __fastcall TCustomScpExplorerForm::DetachTerminal(TObject * ATerminal)
  6595. {
  6596. FEditorManager->ProcessFiles(FileTerminalRemoved, ATerminal);
  6597. if (FFileFindTerminal == ATerminal)
  6598. {
  6599. FFileFindTerminal = NULL;
  6600. HideFileFindDialog();
  6601. }
  6602. if (FClipboardTerminal == ATerminal)
  6603. {
  6604. ClipboardClear(); // implies ClipboardStop
  6605. }
  6606. if ((FOnSynchronizeAbort != NULL) &&
  6607. (ATerminal == FManagedSession))
  6608. {
  6609. FOnSynchronizeAbort(NULL);
  6610. }
  6611. }
  6612. //---------------------------------------------------------------------------
  6613. void __fastcall TCustomScpExplorerForm::TerminalRemoved(TObject * Sender)
  6614. {
  6615. DetachTerminal(Sender);
  6616. }
  6617. //---------------------------------------------------------------------------
  6618. void __fastcall TCustomScpExplorerForm::FileTerminalRemoved(const UnicodeString FileName,
  6619. TEditedFileData * Data, TObject * /*Token*/, void * Arg)
  6620. {
  6621. TTerminal * Terminal = static_cast<TTerminal *>(Arg);
  6622. DebugAssert(Terminal != NULL);
  6623. if (Data->Terminal == Terminal)
  6624. {
  6625. Data->Terminal = NULL;
  6626. }
  6627. }
  6628. //---------------------------------------------------------------------------
  6629. TManagedTerminal * TCustomScpExplorerForm::GetReplacementForLastSession()
  6630. {
  6631. return NULL;
  6632. }
  6633. //---------------------------------------------------------------------------
  6634. void __fastcall TCustomScpExplorerForm::LastTerminalClosed()
  6635. {
  6636. UpdateControls();
  6637. SessionColor = TColor(0);
  6638. UpdateRemotePathComboBox(false);
  6639. NeedSession(false);
  6640. }
  6641. //---------------------------------------------------------------------------
  6642. void __fastcall TCustomScpExplorerForm::NeedSession(bool Startup)
  6643. {
  6644. try
  6645. {
  6646. // Cache, as the login dialog can change its value.
  6647. // See also DoShow. Also see TTerminalManager::NewSession.
  6648. bool ShowLogin = WinConfiguration->ShowLoginWhenNoSession;
  6649. try
  6650. {
  6651. if (ShowLogin)
  6652. {
  6653. bool ReloadSessions = !Startup;
  6654. TTerminalManager::Instance()->NewSession(L"", ReloadSessions, this, true);
  6655. }
  6656. }
  6657. __finally
  6658. {
  6659. // Do not terminate, if we are only starting up and we are not showing Login dialog
  6660. // (so there was no chance for the user to open any session yet)
  6661. if ((ShowLogin || !Startup) &&
  6662. !WinConfiguration->KeepOpenWhenNoSession &&
  6663. // this rules out the implicit Commander's local-local browser
  6664. ((Terminal == NULL) || (!Terminal->Active && !Terminal->Permanent)))
  6665. {
  6666. TerminateApplication();
  6667. }
  6668. }
  6669. }
  6670. catch (EAbort &)
  6671. {
  6672. // swallow
  6673. // The TTerminalManager does not expect the OnLastTerminalClose to throw without trying to connect.
  6674. // Also when called from TWinControl.UpdateShowing => CMShowingChanged, the showing is aborted on exception
  6675. }
  6676. }
  6677. //---------------------------------------------------------------------------
  6678. void __fastcall TCustomScpExplorerForm::SessionListChanged(bool ForceTruncationUpdate)
  6679. {
  6680. TTerminalManager * Manager = TTerminalManager::Instance();
  6681. DebugAssert(!Manager->Updating);
  6682. TAutoFlag UpdatingSessionTabsFlag(FUpdatingSessionTabs);
  6683. int ActiveSessionIndex = Manager->ActiveSessionIndex;
  6684. Configuration->Usage->SetMax(L"MaxOpenedSessions", Manager->Count);
  6685. SendMessage(SessionsPageControl->Handle, WM_SETREDRAW, 0, 0);
  6686. try
  6687. {
  6688. int PrevTabsWidth = SessionsPageControl->TotalTabsWidth();
  6689. while (SessionsPageControl->PageCount > Manager->Count + 1)
  6690. {
  6691. delete SessionsPageControl->Pages[SessionsPageControl->PageCount - 1];
  6692. }
  6693. for (int Index = 0; Index <= Manager->Count; Index++)
  6694. {
  6695. TThemeTabSheet * TabSheet;
  6696. if (Index >= SessionsPageControl->PageCount)
  6697. {
  6698. TabSheet = new TThemeTabSheet(SessionsPageControl);
  6699. TabSheet->PageControl = SessionsPageControl;
  6700. }
  6701. else
  6702. {
  6703. TabSheet = SessionsPageControl->Pages[Index];
  6704. }
  6705. bool IsSessionTab = (Index < Manager->Count);
  6706. if (IsSessionTab)
  6707. {
  6708. TTerminal * Terminal = Manager->Sessions[Index];
  6709. TabSheet->Tag = reinterpret_cast<int>(Terminal);
  6710. TabSheet->CaptionTruncation = tttEllipsis;
  6711. UpdateSessionTab(TabSheet);
  6712. }
  6713. else
  6714. {
  6715. TabSheet->Tag = 0; // not really needed
  6716. TabSheet->Shadowed = false;
  6717. TabSheet->Button = SupportsLocalBrowser() ? ttbDropDown : ttbNone;
  6718. TabSheet->CaptionTruncation = tttNoText;
  6719. // 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)
  6720. UpdateNewTabTab();
  6721. }
  6722. }
  6723. // Prevent flicker on the right side where scrolling buttons would be, unless really needed
  6724. if (ForceTruncationUpdate || (SessionsPageControl->TotalTabsWidth() != PrevTabsWidth))
  6725. {
  6726. SessionsPageControl->UpdateTabsCaptionTruncation();
  6727. }
  6728. }
  6729. __finally
  6730. {
  6731. SendMessage(SessionsPageControl->Handle, WM_SETREDRAW, 1, 0);
  6732. }
  6733. SessionsPageControl->ActivePageIndex = ActiveSessionIndex;
  6734. }
  6735. //---------------------------------------------------------------------------
  6736. UnicodeString TCustomScpExplorerForm::GetNewTabTabCaption()
  6737. {
  6738. return StripHotkey(StripTrailingPunctuation(NonVisualDataModule->NewTabAction->Caption));
  6739. }
  6740. //---------------------------------------------------------------------------
  6741. void __fastcall TCustomScpExplorerForm::UpdateNewTabTab()
  6742. {
  6743. TThemeTabSheet * TabSheet = SessionsPageControl->Pages[SessionsPageControl->PageCount - 1];
  6744. UnicodeString TabCaption;
  6745. if (WinConfiguration->SelectiveToolbarText)
  6746. {
  6747. TabCaption = GetNewTabTabCaption();
  6748. }
  6749. TabSheet->BaseCaption = TabCaption;
  6750. TabSheet->ImageIndex = GetNewTabTabImageIndex(osCurrent);
  6751. }
  6752. //---------------------------------------------------------------------------
  6753. TManagedTerminal * __fastcall TCustomScpExplorerForm::GetSessionTabSession(TTabSheet * TabSheet)
  6754. {
  6755. return reinterpret_cast<TManagedTerminal *>(TabSheet->Tag);
  6756. }
  6757. //---------------------------------------------------------------------------
  6758. UnicodeString TCustomScpExplorerForm::GetLocalBrowserSessionTitle(TManagedTerminal *)
  6759. {
  6760. DebugFail();
  6761. return UnicodeString();
  6762. }
  6763. //---------------------------------------------------------------------------
  6764. void TCustomScpExplorerForm::UpdateSessionTab(TThemeTabSheet * TabSheet)
  6765. {
  6766. if (DebugAlwaysTrue(TabSheet != NULL))
  6767. {
  6768. TManagedTerminal * ASession = GetSessionTabSession(TabSheet);
  6769. if (DebugAlwaysTrue(ASession != NULL))
  6770. {
  6771. TColor Color = (ASession == Terminal) ? FSessionColor : ASession->StateData->Color;
  6772. if (ASession->LocalBrowser)
  6773. {
  6774. TabSheet->ImageIndex = FLocalBrowserTabImageIndex;
  6775. }
  6776. else
  6777. {
  6778. TabSheet->ImageIndex = AddSessionColor(Color);
  6779. }
  6780. TTerminalManager * Manager = TTerminalManager::Instance();
  6781. TabSheet->Shadowed = !ASession->Active && !ASession->LocalBrowser;
  6782. TabSheet->Button = CanCloseSession(ASession) ? ttbClose : ttbNone;
  6783. TabSheet->BaseCaption = Manager->GetSessionTitle(ASession, true);
  6784. }
  6785. }
  6786. }
  6787. //---------------------------------------------------------------------------
  6788. bool TCustomScpExplorerForm::CanCloseSession(TManagedTerminal * Session)
  6789. {
  6790. return !Session->LocalBrowser || (TTerminalManager::Instance()->Count > 1);
  6791. }
  6792. //---------------------------------------------------------------------------
  6793. bool __fastcall TCustomScpExplorerForm::SessionTabSwitched()
  6794. {
  6795. DebugAssert(SessionsPageControl->ActivePage != NULL);
  6796. TManagedTerminal * Session = GetSessionTabSession(SessionsPageControl->ActivePage);
  6797. bool Result = (Session != NULL);
  6798. if (Result)
  6799. {
  6800. TTerminalManager::Instance()->ActiveSession = Session;
  6801. }
  6802. else
  6803. {
  6804. try
  6805. {
  6806. NewTab();
  6807. }
  6808. __finally
  6809. {
  6810. SessionListChanged();
  6811. }
  6812. FSessionsPageControlNewTabTime = Now();
  6813. }
  6814. return Result;
  6815. }
  6816. //---------------------------------------------------------------------------
  6817. void __fastcall TCustomScpExplorerForm::SessionsPageControlChange(TObject * /*Sender*/)
  6818. {
  6819. TWindowLock Lock(this);
  6820. SessionTabSwitched();
  6821. }
  6822. //---------------------------------------------------------------------------
  6823. void __fastcall TCustomScpExplorerForm::TransferListChange(TObject * Sender)
  6824. {
  6825. TTBXStringList * TransferList = dynamic_cast<TTBXStringList *>(Sender);
  6826. DebugAssert(TransferList != NULL);
  6827. UnicodeString Name;
  6828. if (TransferList->ItemIndex <= 0)
  6829. {
  6830. Name = L"";
  6831. }
  6832. else
  6833. {
  6834. Name = GUIConfiguration->CopyParamList->Names[TransferList->ItemIndex - 1];
  6835. }
  6836. if (FCopyParamAutoSelected.IsEmpty())
  6837. {
  6838. // if previous preset was not autoselected, make new preset the "default"
  6839. FCopyParamDefault = Name;
  6840. }
  6841. GUIConfiguration->CopyParamCurrent = Name;
  6842. }
  6843. //---------------------------------------------------------------------------
  6844. void __fastcall TCustomScpExplorerForm::UpdateTransferLabel()
  6845. {
  6846. // sanity check
  6847. bool ExistingPreset =
  6848. (FTransferListHoverIndex >= 0) &&
  6849. (FTransferListHoverIndex < 1 + GUIConfiguration->CopyParamList->Count);
  6850. DebugAssert(ExistingPreset);
  6851. if (ExistingPreset)
  6852. {
  6853. HDC DC = GetDC(0);
  6854. TCanvas * Canvas = new TCanvas();
  6855. try
  6856. {
  6857. Canvas->Handle = DC;
  6858. Canvas->Font = GetToolbarFont(this);
  6859. UnicodeString Name;
  6860. if (FTransferListHoverIndex == 0)
  6861. {
  6862. Name = L"";
  6863. }
  6864. else
  6865. {
  6866. Name = GUIConfiguration->CopyParamList->Names[FTransferListHoverIndex - 1];
  6867. }
  6868. TTBXLabelItem * TransferLabel = dynamic_cast<TTBXLabelItem*>(
  6869. static_cast<TComponent*>(GetComponent(fcTransferLabel)));
  6870. TTBXStringList * TransferList = dynamic_cast<TTBXStringList*>(
  6871. static_cast<TObject*>(GetComponent(fcTransferList)));
  6872. UnicodeString InfoStr =
  6873. GUIConfiguration->CopyParamPreset[Name].
  6874. GetInfoStr(L"; ",
  6875. FLAGMASK(HasActiveTerminal(), Terminal->UsableCopyParamAttrs(0).General));
  6876. int MaxWidth = TransferList->MinWidth - (2 * TransferLabel->Margin) - ScaleByTextHeight(this, 10);
  6877. if (Canvas->TextExtent(InfoStr).cx > MaxWidth)
  6878. {
  6879. while (Canvas->TextExtent(InfoStr + Ellipsis).cx > MaxWidth)
  6880. {
  6881. InfoStr.SetLength(InfoStr.Length() - 1);
  6882. }
  6883. InfoStr += Ellipsis;
  6884. }
  6885. // UpdateCaption does not cause invalidation of whole submenu, while
  6886. // setting Caption property does.
  6887. // also it probably does not resize the label, even if necessary
  6888. // (we do not want that anyway)
  6889. TransferLabel->UpdateCaption(InfoStr);
  6890. }
  6891. __finally
  6892. {
  6893. Canvas->Handle = NULL;
  6894. ReleaseDC(0, DC);
  6895. delete Canvas;
  6896. }
  6897. }
  6898. }
  6899. //---------------------------------------------------------------------------
  6900. void __fastcall TCustomScpExplorerForm::TransferListDrawItem(
  6901. TTBXCustomList * /*Sender*/, TCanvas * /*ACanvas*/, const TRect & /*ARect*/,
  6902. int /*AIndex*/, int AHoverIndex, bool & /*DrawDefault*/)
  6903. {
  6904. if (FTransferListHoverIndex != AHoverIndex)
  6905. {
  6906. FTransferListHoverIndex = AHoverIndex;
  6907. UpdateTransferLabel();
  6908. }
  6909. }
  6910. //---------------------------------------------------------------------------
  6911. void __fastcall TCustomScpExplorerForm::WMAppCommand(TMessage & Message)
  6912. {
  6913. int Command = GET_APPCOMMAND_LPARAM(Message.LParam);
  6914. TShiftState Shift = KeyDataToShiftState(GET_KEYSTATE_LPARAM(Message.LParam));
  6915. if ((Shift * (TShiftState() << ssShift << ssAlt << ssCtrl)).Empty())
  6916. {
  6917. if (Command == APPCOMMAND_BROWSER_FAVORITES)
  6918. {
  6919. if (!NonVisualDataModule->Busy)
  6920. {
  6921. OpenDirectory(GetSide(osCurrent));
  6922. }
  6923. Message.Result = 1;
  6924. }
  6925. else
  6926. {
  6927. TForm::Dispatch(&Message);
  6928. }
  6929. }
  6930. else
  6931. {
  6932. TForm::Dispatch(&Message);
  6933. }
  6934. }
  6935. //---------------------------------------------------------------------------
  6936. void __fastcall TCustomScpExplorerForm::CMDialogChar(TMessage & AMessage)
  6937. {
  6938. TCMDialogChar & Message = reinterpret_cast<TCMDialogChar &>(AMessage);
  6939. if ((FIgnoreNextDialogChar != 0) &&
  6940. (toupper(Message.CharCode) == toupper(FIgnoreNextDialogChar)))
  6941. {
  6942. Message.Result = 1;
  6943. }
  6944. else
  6945. {
  6946. TForm::Dispatch(&Message);
  6947. }
  6948. FIgnoreNextDialogChar = 0;
  6949. }
  6950. //---------------------------------------------------------------------------
  6951. void __fastcall TCustomScpExplorerForm::WMSysCommand(TMessage & Message)
  6952. {
  6953. // The four low-order bits are used internally by Windows
  6954. unsigned int Cmd = (Message.WParam & 0xFFF0);
  6955. // SC_RESTORE, SC_MAXIMIZE, SC_MINIMIZE - buttons on windows title
  6956. // SC_DEFAULT - double click on windows title (does not work, at least on WinXP)
  6957. // 61730 - restore thru double click - undocumented
  6958. // 61490 - maximize thru double click - undocumented
  6959. if ((Cmd == SC_RESTORE) || (Cmd == SC_MAXIMIZE) ||
  6960. (Cmd == SC_MINIMIZE) || (Cmd == SC_DEFAULT))
  6961. {
  6962. SysResizing(Cmd);
  6963. }
  6964. TForm::Dispatch(&Message);
  6965. }
  6966. //---------------------------------------------------------------------------
  6967. void __fastcall TCustomScpExplorerForm::WMQueryEndSession(TMessage & Message)
  6968. {
  6969. // We were actually never able to make ENDSESSION_CRITICAL happen.
  6970. // Also there no point returning TRUE as we are not able to
  6971. // handle the abrupt termination caused by subsequent WM_ENDSESSION cleanly.
  6972. // Hence the process termination might be safer :)
  6973. if ((Message.LParam != ENDSESSION_CRITICAL) &&
  6974. (((FQueue != NULL) && !FQueue->IsEmpty) || (FProgressForm != NULL)))
  6975. {
  6976. Message.Result = FALSE;
  6977. }
  6978. else
  6979. {
  6980. Message.Result = TRUE;
  6981. }
  6982. // Do not call default handling as that triggers OnCloseQuery,
  6983. // where our implementation will popup confirmation dialogs, what we do not want,
  6984. // as per Vista guidelines:
  6985. // https://learn.microsoft.com/en-us/windows/win32/shutdown/shutting-down
  6986. }
  6987. //---------------------------------------------------------------------------
  6988. void __fastcall TCustomScpExplorerForm::WMEndSession(TWMEndSession & Message)
  6989. {
  6990. if (Message.EndSession && IsApplicationMinimized())
  6991. {
  6992. // WORKAROUND
  6993. // TApplication.WndProc() calls Application.Terminate() before Halt(),
  6994. // when it receives WM_ENDSESSION,
  6995. // but that sometimes (particularly when application is minimized) cause crashes.
  6996. // Crash popups message that blocks log off.
  6997. // Obviously application cannot shutdown cleanly after WM_ENDSESSION,
  6998. // so we call ExitProcess() immediately, not even trying to cleanup.
  6999. // It still causes beep, so there's likely some popup, but it does not block
  7000. // log off.
  7001. ExitProcess(0);
  7002. }
  7003. TForm::Dispatch(&Message);
  7004. }
  7005. #ifdef _DEBUG
  7006. //---------------------------------------------------------------------------
  7007. void __fastcall TCustomScpExplorerForm::WMWindowPosChanged(TWMWindowPosMsg & Message)
  7008. {
  7009. TForm::Dispatch(&Message);
  7010. }
  7011. #endif
  7012. //---------------------------------------------------------------------------
  7013. void __fastcall TCustomScpExplorerForm::SysResizing(unsigned int /*Cmd*/)
  7014. {
  7015. }
  7016. //---------------------------------------------------------------------------
  7017. void __fastcall TCustomScpExplorerForm::DoShow()
  7018. {
  7019. // Called from TCustomScpExplorerForm::CMShowingChanged
  7020. // only now are the controls resized finally, so the size constraints
  7021. // will not conflict with possibly very small window size
  7022. RestoreFormParams();
  7023. // This indicates that the form was rescaled after parameters were loaded and
  7024. // as most probably the parameters were actually saved with the current form scaling
  7025. // (windows was the last time likely closed on the same monitor, where it is starting now),
  7026. // rescaling happened twice already. Reload the parameters, as now they likely won't need any scaling
  7027. // and no rounding problems will occur.
  7028. // See also RestoreForm.
  7029. // (we should be safe to call RestoreParams unconditionally)
  7030. if (PixelsPerInch != Screen->PixelsPerInch)
  7031. {
  7032. RestoreParams();
  7033. }
  7034. FixControlsPlacement();
  7035. if (Position == poDefaultPosOnly)
  7036. {
  7037. CutFormToDesktop(this);
  7038. }
  7039. TForm::DoShow();
  7040. FSessionsDragDropFilesEx->DragDropControl = SessionsPageControl;
  7041. FQueueDragDropFilesEx->DragDropControl = QueueView3;
  7042. // This was previously in NeedSession, where other related code is.
  7043. // But that is called from CMShowingChanged, when the window is already visible and the local-local window state
  7044. // unpleasantly shows briefly.
  7045. if ((ManagedSession == NULL) &&
  7046. !WinConfiguration->ShowLoginWhenNoSession &&
  7047. WinConfiguration->AutoSaveWorkspace && !WinConfiguration->AutoWorkspace.IsEmpty() &&
  7048. // This detects if workspace was saved the last time the main widow was closed
  7049. SameText(WinConfiguration->LastStoredSession, WinConfiguration->AutoWorkspace))
  7050. {
  7051. DoOpenFolderOrWorkspace(WinConfiguration->AutoWorkspace, false, true);
  7052. Configuration->Usage->Inc(L"OpenedWorkspacesAuto");
  7053. }
  7054. // Before main window displays
  7055. if (ManagedSession == NULL)
  7056. {
  7057. StartingWithoutSession();
  7058. }
  7059. UpdatePixelsPerInchMainWindowCounter();
  7060. FShowing = true;
  7061. }
  7062. //---------------------------------------------------------------------------
  7063. void __fastcall TCustomScpExplorerForm::UpdatePixelsPerInchMainWindowCounter()
  7064. {
  7065. Configuration->Usage->Set(L"PixelsPerInchMainWindow", PixelsPerInch);
  7066. }
  7067. //---------------------------------------------------------------------------
  7068. void __fastcall TCustomScpExplorerForm::StartingWithoutSession()
  7069. {
  7070. SessionListChanged();
  7071. InitStatusBar();
  7072. UpdateControls();
  7073. }
  7074. //---------------------------------------------------------------------------
  7075. void __fastcall TCustomScpExplorerForm::PopupTrayBalloon(TTerminal * Terminal,
  7076. const UnicodeString & Str, TQueryType Type, Exception * E, unsigned int Seconds,
  7077. TNotifyEvent OnBalloonClick, TObject * UserData)
  7078. {
  7079. bool Do;
  7080. UnicodeString Message;
  7081. if (E == NULL)
  7082. {
  7083. Message = Str;
  7084. Do = true;
  7085. }
  7086. else
  7087. {
  7088. Do = ExceptionMessage(E, Message);
  7089. }
  7090. if (Do && WinConfiguration->BalloonNotifications)
  7091. {
  7092. UnicodeString Title;
  7093. if ((Terminal == NULL) && (Type == qtInformation) && ExtractMainInstructions(Message, Title))
  7094. {
  7095. Message = TrimLeft(Message);
  7096. }
  7097. else
  7098. {
  7099. Message = UnformatMessage(Message);
  7100. const ResourceString * Captions[] = { &_SMsgDlgConfirm, &_SMsgDlgWarning,
  7101. &_SMsgDlgError, &_SMsgDlgInformation, NULL };
  7102. Title = LoadResourceString(Captions[Type]);
  7103. if (Terminal != NULL)
  7104. {
  7105. TManagedTerminal * Session = DebugNotNull(dynamic_cast<TManagedTerminal *>(Terminal));
  7106. Title = TTerminalManager::Instance()->GetSessionTitle(Session, true) + TitleSeparator + Title;
  7107. }
  7108. }
  7109. if (Seconds == 0)
  7110. {
  7111. Seconds = WinConfiguration->NotificationsTimeout;
  7112. }
  7113. FTrayIcon->PopupBalloon(Title, Message, Type, Seconds * MSecsPerSec, OnBalloonClick, UserData);
  7114. }
  7115. }
  7116. //---------------------------------------------------------------------------
  7117. unsigned int __fastcall TCustomScpExplorerForm::MoreMessageDialog(const UnicodeString Message,
  7118. TStrings * MoreMessages, TQueryType Type, unsigned int Answers,
  7119. UnicodeString HelpKeyword, const TMessageParams * Params,
  7120. TTerminal * Terminal)
  7121. {
  7122. if (((WinConfiguration->ContinueOnError && (FErrorList != NULL)) ||
  7123. (FOnFeedSynchronizeError != NULL)) &&
  7124. (Params != NULL) && (Params->Params & mpAllowContinueOnError) )
  7125. {
  7126. if (FOnFeedSynchronizeError != NULL)
  7127. {
  7128. FOnFeedSynchronizeError(Message, MoreMessages, Type, HelpKeyword);
  7129. }
  7130. else
  7131. {
  7132. DebugAssert(FErrorList != NULL);
  7133. TStringList * MoreMessagesCopy = NULL;
  7134. if (MoreMessages)
  7135. {
  7136. MoreMessagesCopy = new TStringList();
  7137. MoreMessagesCopy->Assign(MoreMessages);
  7138. }
  7139. FErrorList->AddObject(Message, MoreMessagesCopy);
  7140. }
  7141. PopupTrayBalloon(Terminal, Message, Type);
  7142. return ContinueAnswer(Answers);
  7143. }
  7144. else
  7145. {
  7146. bool UseBalloon = FTrayIcon->Visible;
  7147. if (UseBalloon)
  7148. {
  7149. PopupTrayBalloon(Terminal, Message, Type);
  7150. }
  7151. unsigned int Result;
  7152. try
  7153. {
  7154. Result = ::MoreMessageDialog(Message, MoreMessages, Type, Answers, HelpKeyword, Params);
  7155. }
  7156. __finally
  7157. {
  7158. // cancel only balloon we popped up, otherwise we may cancel notification
  7159. // balloon that was there before the message dialog
  7160. // (such as "dd confirmation opt in balloon)
  7161. if (UseBalloon)
  7162. {
  7163. FTrayIcon->CancelBalloon();
  7164. }
  7165. }
  7166. return Result;
  7167. }
  7168. }
  7169. //---------------------------------------------------------------------------
  7170. void __fastcall TCustomScpExplorerForm::ShowExtendedException(
  7171. TTerminal * Terminal, Exception * E)
  7172. {
  7173. if (FTrayIcon->Visible)
  7174. {
  7175. PopupTrayBalloon(Terminal, L"", qtError, E);
  7176. }
  7177. // particularly prevent opening new session from jump list,
  7178. // while exception is shown
  7179. NonVisualDataModule->StartBusy();
  7180. try
  7181. {
  7182. ShowExtendedExceptionEx(Terminal, E);
  7183. }
  7184. __finally
  7185. {
  7186. NonVisualDataModule->EndBusy();
  7187. FTrayIcon->CancelBalloon();
  7188. }
  7189. }
  7190. //---------------------------------------------------------------------------
  7191. void __fastcall TCustomScpExplorerForm::SessionReady()
  7192. {
  7193. InitStatusBar();
  7194. // cannot rely on active page being page for active terminal,
  7195. // as it can happen that active page is the "new session" page
  7196. // (e.g. when reconnecting active terminal, while login dialog
  7197. // invoked from "new session" page is modal)
  7198. int ActiveSessionIndex = TTerminalManager::Instance()->ActiveSessionIndex;
  7199. UpdateSessionTab(SessionsPageControl->Pages[ActiveSessionIndex]);
  7200. }
  7201. //---------------------------------------------------------------------------
  7202. void __fastcall TCustomScpExplorerForm::InactiveTerminalException(
  7203. TTerminal * Terminal, Exception * E)
  7204. {
  7205. Notify(Terminal, L"", qtError, false, NULL, NULL, E);
  7206. if (!Terminal->Active)
  7207. {
  7208. int Index = TTerminalManager::Instance()->IndexOf(Terminal);
  7209. if (DebugAlwaysTrue((Index >= 0) && (Index < SessionsPageControl->PageCount)))
  7210. {
  7211. UpdateSessionTab(SessionsPageControl->Pages[Index]);
  7212. }
  7213. }
  7214. }
  7215. //---------------------------------------------------------------------------
  7216. void __fastcall TCustomScpExplorerForm::Notify(TTerminal * Terminal,
  7217. UnicodeString Message, TQueryType Type,
  7218. bool Important, TNotifyEvent OnClick, TObject * UserData, Exception * E)
  7219. {
  7220. if ((E == NULL) ||
  7221. ExceptionMessage(E, Message))
  7222. {
  7223. unsigned int Seconds = WinConfiguration->NotificationsTimeout;
  7224. if (Important)
  7225. {
  7226. Seconds *= 5;
  7227. }
  7228. UnicodeString NoteMessage(UnformatMessage(Message));
  7229. if (Terminal != NULL)
  7230. {
  7231. TManagedTerminal * Session = DebugNotNull(dynamic_cast<TManagedTerminal *>(Terminal));
  7232. NoteMessage = FORMAT(L"%s: %s",
  7233. (TTerminalManager::Instance()->GetSessionTitle(Session, true), NoteMessage));
  7234. }
  7235. if (WinConfiguration->BalloonNotifications)
  7236. {
  7237. AddNote(NoteMessage);
  7238. PopupTrayBalloon(Terminal, Message, Type, NULL, Seconds, OnClick, UserData);
  7239. }
  7240. else
  7241. {
  7242. FlashOnBackground();
  7243. PostNote(NoteMessage, Seconds, OnClick, UserData);
  7244. }
  7245. }
  7246. }
  7247. //---------------------------------------------------------------------------
  7248. void __fastcall TCustomScpExplorerForm::QueueEmptyNoteClicked(TObject * Sender)
  7249. {
  7250. RestoreApp();
  7251. TTerminalNoteData * TerminalNoteData = dynamic_cast<TTerminalNoteData *>(Sender);
  7252. if (DebugAlwaysTrue(TerminalNoteData != NULL) &&
  7253. !NonVisualDataModule->Busy)
  7254. {
  7255. TManagedTerminal * Terminal = TerminalNoteData->Terminal;
  7256. TTerminalManager::Instance()->ActiveSession = Terminal;
  7257. if (!ComponentVisible[fcQueueView])
  7258. {
  7259. ToggleQueueVisibility();
  7260. GoToQueue();
  7261. }
  7262. }
  7263. }
  7264. //---------------------------------------------------------------------------
  7265. void __fastcall TCustomScpExplorerForm::QueueEvent(TManagedTerminal * ATerminal,
  7266. TTerminalQueue * /*Queue*/, TQueueEvent Event)
  7267. {
  7268. UnicodeString Message;
  7269. TNotifyEvent OnClick = NULL;
  7270. TObject * UserData = NULL;
  7271. bool QueueInvisible = !ComponentVisible[fcQueueView] || IsApplicationMinimized();
  7272. switch (Event)
  7273. {
  7274. case qeEmptyButMonitored:
  7275. if ((ATerminal != Terminal) || QueueInvisible)
  7276. {
  7277. Message = LoadStr(BALLOON_QUEUE_EMPTY);
  7278. OnClick = QueueEmptyNoteClicked;
  7279. TTerminalNoteData * TerminalNoteData = new TTerminalNoteData();
  7280. TerminalNoteData->Terminal = ATerminal;
  7281. UserData = TerminalNoteData;
  7282. }
  7283. break;
  7284. case qeEmpty:
  7285. OperationComplete(ATerminal->QueueOperationStart);
  7286. break;
  7287. case qePendingUserAction:
  7288. if ((ATerminal != Terminal) ||
  7289. (QueueInvisible && !IsQueueAutoPopup()))
  7290. {
  7291. Message = LoadStr(BALLOON_QUEUE_USER_ACTION);
  7292. }
  7293. break;
  7294. default:
  7295. DebugFail();
  7296. }
  7297. if (!Message.IsEmpty())
  7298. {
  7299. Notify(ATerminal, Message, qtInformation, false, OnClick, UserData);
  7300. }
  7301. }
  7302. //---------------------------------------------------------------------------
  7303. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDCreateDragFileList(
  7304. TObject * /*Sender*/, TFileList * FileList, bool & Created)
  7305. {
  7306. if (FDDExtMapFile != NULL)
  7307. {
  7308. CloseHandle(FDDExtMapFile);
  7309. FDDExtMapFile = NULL;
  7310. }
  7311. if (WinConfiguration->DDFakeFile)
  7312. {
  7313. DDFakeFileInitDrag(FileList, Created);
  7314. }
  7315. }
  7316. //---------------------------------------------------------------------------
  7317. void __fastcall TCustomScpExplorerForm::DDFakeCreated(TObject * /*Sender*/, const UnicodeString FileName)
  7318. {
  7319. if (DebugAlwaysTrue(!FDragFakeDirectory.IsEmpty()) &&
  7320. SameText(ExtractFileName(FileName), ExtractFileName(FDragFakeDirectory)))
  7321. {
  7322. FFakeFileDropTarget = FileName;
  7323. }
  7324. }
  7325. //---------------------------------------------------------------------------
  7326. UnicodeString __fastcall TCustomScpExplorerForm::CreateFakeTransferDirectory()
  7327. {
  7328. UnicodeString Result = ExcludeTrailingBackslash(WinConfiguration->TemporaryDir());
  7329. if (!ForceDirectories(ApiPath(Result)))
  7330. {
  7331. throw Exception(FMTLOAD(CREATE_TEMP_DIR_ERROR, (Result)));
  7332. }
  7333. FileSetAttr(ApiPath(Result), faHidden);
  7334. return Result;
  7335. }
  7336. //---------------------------------------------------------------------------
  7337. void __fastcall TCustomScpExplorerForm::DDFakeFileInitDrag(TFileList * FileList,
  7338. bool & Created)
  7339. {
  7340. FFakeFileDropTarget = UnicodeString();
  7341. FDragFakeDirectory = CreateFakeTransferDirectory();
  7342. FileList->AddItem(NULL, FDragFakeDirectory);
  7343. Created = true;
  7344. if (!WinConfiguration->IsDDExtRunning() || WinConfiguration->IsDDExtBroken())
  7345. {
  7346. FDragFakeMonitors = StartCreationDirectoryMonitorsOnEachDrive(FILE_NOTIFY_CHANGE_DIR_NAME, DDFakeCreated);
  7347. }
  7348. FDDExtMapFile = CreateFileMappingA((HANDLE)0xFFFFFFFF, NULL, PAGE_READWRITE,
  7349. 0, sizeof(TDragExtCommStruct), AnsiString(DRAG_EXT_MAPPING).c_str());
  7350. {
  7351. TMutexGuard Guard(FDDExtMutex, DRAGEXT_MUTEX_RELEASE_TIMEOUT);
  7352. TDragExtCommStruct* CommStruct;
  7353. CommStruct = static_cast<TDragExtCommStruct*>(MapViewOfFile(FDDExtMapFile,
  7354. FILE_MAP_ALL_ACCESS, 0, 0, 0));
  7355. DebugAssert(CommStruct != NULL);
  7356. CommStruct->Version = TDragExtCommStruct::CurrentVersion;
  7357. CommStruct->Dragging = true;
  7358. wcsncpy(CommStruct->DropDest, FDragFakeDirectory.c_str(),
  7359. LENOF(CommStruct->DropDest));
  7360. NULL_TERMINATE(CommStruct->DropDest);
  7361. UnmapViewOfFile(CommStruct);
  7362. }
  7363. }
  7364. //---------------------------------------------------------------------------
  7365. bool __fastcall TCustomScpExplorerForm::RemoteFileControlFileOperation(
  7366. TObject * Sender, TFileOperation Operation, bool NoConfirmation, void * Param)
  7367. {
  7368. bool Result;
  7369. DebugAssert(!IsLocalBrowserMode());
  7370. if (Sender == RemoteDirView)
  7371. {
  7372. Result = ExecuteFileOperation(Operation, osRemote, true, NoConfirmation, Param);
  7373. }
  7374. else
  7375. {
  7376. DebugAssert(Sender == RemoteDriveView);
  7377. TStrings * FileList = RemoteDriveView->DragFileList();
  7378. try
  7379. {
  7380. Result = ExecuteFileOperation(Operation, osRemote, FileList, NoConfirmation, Param);
  7381. }
  7382. __finally
  7383. {
  7384. delete FileList;
  7385. }
  7386. }
  7387. if (FDDTargetControl == RemoteDriveView)
  7388. {
  7389. RemoteDriveView->UpdateDropTarget();
  7390. }
  7391. // foRemoteMove happens when file/dir is dragged within the remote tree view
  7392. // or from tree view to dir view.
  7393. // foMove happens when file/dir is dragged from remote tree view outside of
  7394. // application via dragex
  7395. if (((Operation == foRemoteMove) || (Operation == foMove)) &&
  7396. (DropSourceControl == RemoteDriveView))
  7397. {
  7398. RemoteDriveView->UpdateDropSource();
  7399. }
  7400. return Result;
  7401. }
  7402. //---------------------------------------------------------------------------
  7403. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDEnd(TObject * Sender)
  7404. {
  7405. DebugAssert(!IsLocalBrowserMode());
  7406. // This also handles drops of remote files to queue.
  7407. // Drops of local files (uploads) are handled in QueueDDProcessDropped.
  7408. SAFE_DESTROY(FDDFileList);
  7409. if (!FFakeFileDropTarget.IsEmpty())
  7410. {
  7411. RemoveDir(ApiPath(FFakeFileDropTarget));
  7412. }
  7413. if ((FDDExtMapFile != NULL) || (FDDTargetControl == QueueView3))
  7414. {
  7415. try
  7416. {
  7417. TDragResult DDResult = (Sender == RemoteDirView) ?
  7418. RemoteDirView->LastDDResult : RemoteDriveView->LastDDResult;
  7419. // Focus is moved to the target application,
  7420. // but as we are going to present the UI, we need to steal the focus back.
  7421. // This most likely won't work though (windows does not allow application
  7422. // to steal focus most of the time)
  7423. Application->BringToFront();
  7424. // On older version of Windows we never got drMove here, see also comment below.
  7425. // On Windows 10, we get the "move".
  7426. if ((DDResult == drCopy) || (DDResult == drMove) || (DDResult == drInvalid))
  7427. {
  7428. UnicodeString TargetDirectory;
  7429. TFileOperation Operation;
  7430. // drInvalid may mean drMove, see comment below
  7431. switch (DDResult)
  7432. {
  7433. case drCopy:
  7434. Operation = foCopy;
  7435. break;
  7436. case drMove:
  7437. Operation = foMove;
  7438. break;
  7439. default:
  7440. DebugFail();
  7441. case drInvalid:
  7442. // prefer "copy" for safety
  7443. Operation = FLAGSET(FLastDropEffect, DROPEFFECT_MOVE) ? foMove : foCopy;
  7444. break;
  7445. }
  7446. TTransferOperationParam Param;
  7447. UnicodeString CounterName;
  7448. bool ForceQueue;
  7449. if (!DDGetTarget(Param.TargetDirectory, ForceQueue, CounterName))
  7450. {
  7451. // we get drInvalid both if d&d was intercepted by ddext,
  7452. // and when users drops on no-drop location.
  7453. // we tell the difference by existence of response from ddext,
  7454. // so we ignore absence of response in this case
  7455. if (DDResult != drInvalid)
  7456. {
  7457. // here we know that the extension is installed,
  7458. // as it is checked as soon as drag&drop starts from
  7459. // RemoteFileControlDDCreateDragFileList
  7460. Configuration->Usage->Inc(L"DownloadsDragDropExternalExtTargetUnknown");
  7461. if (!WinConfiguration->DDExtInstalled || WinConfiguration->IsDDExtBroken())
  7462. {
  7463. throw ExtException(NULL, LoadStr(DD_TARGET_UNKNOWN), HELP_DD_TARGET_UNKNOWN);
  7464. }
  7465. else
  7466. {
  7467. throw ExtException(NULL, LoadStr(DRAGEXT_TARGET_UNKNOWN2), HELP_DRAGEXT_TARGET_UNKNOWN);
  7468. }
  7469. }
  7470. }
  7471. else
  7472. {
  7473. // download using fake file
  7474. Param.Temp = false;
  7475. Param.DragDrop = true;
  7476. if (ForceQueue)
  7477. {
  7478. Param.Queue = asOn;
  7479. }
  7480. if (Sender == RemoteDirView)
  7481. {
  7482. Param.Options = FLAGMASK(SelectedAllFilesInDirView(RemoteDirView), coAllFiles);
  7483. }
  7484. if (RemoteFileControlFileOperation(Sender, Operation,
  7485. (WinConfiguration->DDTransferConfirmation == asOff), &Param))
  7486. {
  7487. Configuration->Usage->Inc(CounterName);
  7488. }
  7489. }
  7490. }
  7491. }
  7492. __finally
  7493. {
  7494. CloseHandle(FDDExtMapFile);
  7495. FDDExtMapFile = NULL;
  7496. delete FDragFakeMonitors;
  7497. FDragFakeMonitors = NULL;
  7498. RemoveDir(ApiPath(FDragFakeDirectory));
  7499. FDragFakeDirectory = L"";
  7500. FFakeFileDropTarget = UnicodeString();
  7501. }
  7502. }
  7503. if (FDragDropSshTerminate.get() != NULL)
  7504. {
  7505. FDragDropSshTerminate->Rethrow();
  7506. }
  7507. }
  7508. //---------------------------------------------------------------------------
  7509. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDGiveFeedback(
  7510. TObject * /*Sender*/, int dwEffect, HRESULT & /*Result*/)
  7511. {
  7512. // Remember drop effect so we know (when user drops files), if we copy or move
  7513. FLastDropEffect = dwEffect;
  7514. }
  7515. //---------------------------------------------------------------------------
  7516. bool __fastcall TCustomScpExplorerForm::DDGetTarget(
  7517. UnicodeString & Directory, bool & ForceQueue, UnicodeString & CounterName)
  7518. {
  7519. bool Result;
  7520. if (FDDTargetControl == QueueView3)
  7521. {
  7522. Directory = DefaultDownloadTargetDirectory();
  7523. Result = true;
  7524. CounterName = L"DownloadsDragDropQueue";
  7525. ForceQueue = true;
  7526. }
  7527. else if (!FFakeFileDropTarget.IsEmpty())
  7528. {
  7529. Directory = ExcludeTrailingBackslash(ExtractFilePath(FFakeFileDropTarget));
  7530. Result = true;
  7531. ForceQueue = false;
  7532. CounterName = L"DownloadsDragDropFakeFile";
  7533. }
  7534. else
  7535. {
  7536. ForceQueue = false;
  7537. Enabled = false;
  7538. try
  7539. {
  7540. int Timer = 0;
  7541. Result = false;
  7542. while (!Result && (Timer < WinConfiguration->DDExtTimeout))
  7543. {
  7544. {
  7545. TMutexGuard Guard(FDDExtMutex, DRAGEXT_MUTEX_RELEASE_TIMEOUT);
  7546. TDragExtCommStruct* CommStruct;
  7547. CommStruct = static_cast<TDragExtCommStruct*>(MapViewOfFile(FDDExtMapFile,
  7548. FILE_MAP_ALL_ACCESS, 0, 0, 0));
  7549. DebugAssert(CommStruct != NULL);
  7550. Result = !CommStruct->Dragging;
  7551. if (Result)
  7552. {
  7553. Directory = ExtractFilePath(CommStruct->DropDest);
  7554. CounterName = L"DownloadsDragDropExternalExt";
  7555. }
  7556. UnmapViewOfFile(CommStruct);
  7557. }
  7558. if (!Result)
  7559. {
  7560. Sleep(50);
  7561. Timer += 50;
  7562. Application->ProcessMessages();
  7563. }
  7564. }
  7565. }
  7566. __finally
  7567. {
  7568. Enabled = true;
  7569. }
  7570. }
  7571. return Result;
  7572. }
  7573. //---------------------------------------------------------------------------
  7574. void __fastcall TCustomScpExplorerForm::AddDelayedDirectoryDeletion(
  7575. const UnicodeString TempDir, int SecDelay)
  7576. {
  7577. TDateTime Alarm = IncSecond(Now(), SecDelay);
  7578. FDelayedDeletionList->AddObject(TempDir, reinterpret_cast<TObject*>(Alarm.FileDate()));
  7579. if (FDelayedDeletionTimer == NULL)
  7580. {
  7581. DebugAssert(HandleAllocated());
  7582. FDelayedDeletionTimer = new TTimer(this);
  7583. FDelayedDeletionTimer->Interval = 10000;
  7584. FDelayedDeletionTimer->OnTimer = DoDelayedDeletion;
  7585. }
  7586. else
  7587. {
  7588. FDelayedDeletionTimer->Enabled = true;
  7589. }
  7590. }
  7591. //---------------------------------------------------------------------------
  7592. void __fastcall TCustomScpExplorerForm::DoDelayedDeletion(TObject * Sender)
  7593. {
  7594. DebugAssert(FDelayedDeletionList != NULL);
  7595. TDateTime N = Now();
  7596. TDateTime Alert;
  7597. UnicodeString Directory;
  7598. for (int Index = FDelayedDeletionList->Count-1; Index >= 0; Index--)
  7599. {
  7600. Alert = FileDateToDateTime(reinterpret_cast<int>(FDelayedDeletionList->Objects[Index]));
  7601. if ((N >= Alert) || (Sender == NULL))
  7602. {
  7603. Directory = FDelayedDeletionList->Strings[Index];
  7604. if (DeleteDirectory(ExcludeTrailingBackslash(Directory)))
  7605. {
  7606. FDelayedDeletionList->Delete(Index);
  7607. }
  7608. }
  7609. }
  7610. if (FDelayedDeletionList->Count == 0)
  7611. {
  7612. FDelayedDeletionTimer->Enabled = false;
  7613. }
  7614. }
  7615. //---------------------------------------------------------------------------
  7616. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDTargetDrop()
  7617. {
  7618. DebugAssert(!IsLocalBrowserMode());
  7619. if (IsFileControl(FDDTargetControl, osRemote) ||
  7620. (FDDTargetControl == SessionsPageControl))
  7621. {
  7622. TManagedTerminal * TargetTerminal = NULL;
  7623. TFileOperation Operation = foNone;
  7624. if (FDDTargetControl == SessionsPageControl)
  7625. {
  7626. TPoint Point = SessionsPageControl->ScreenToClient(Mouse->CursorPos);
  7627. int Index = SessionsPageControl->IndexOfTabAt(Point.X, Point.Y);
  7628. // do not allow dropping on local-local, disconnected and the "+" tab (see also SessionsDDProcessDropped)
  7629. TargetTerminal = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  7630. if (IsActiveTerminal(TargetTerminal) && DebugAlwaysTrue(!TargetTerminal->LocalBrowser))
  7631. {
  7632. if ((FLastDropEffect == DROPEFFECT_MOVE) &&
  7633. (TargetTerminal == TTerminalManager::Instance()->ActiveTerminal))
  7634. {
  7635. Operation = foRemoteMove;
  7636. }
  7637. else
  7638. {
  7639. Operation = foRemoteCopy;
  7640. }
  7641. }
  7642. }
  7643. else
  7644. {
  7645. if (FLastDropEffect == DROPEFFECT_MOVE)
  7646. {
  7647. Operation = foRemoteMove;
  7648. }
  7649. else if (FLastDropEffect == DROPEFFECT_COPY)
  7650. {
  7651. Operation = foRemoteCopy;
  7652. }
  7653. }
  7654. if (Operation != foNone)
  7655. {
  7656. RemoteFileControlFileOperation(DropSourceControl,
  7657. Operation, (WinConfiguration->DDTransferConfirmation == asOff), TargetTerminal);
  7658. }
  7659. // abort drag&drop
  7660. Abort();
  7661. }
  7662. else if ((FDDExtMapFile == NULL) && (FLastDropEffect != DROPEFFECT_NONE) &&
  7663. // Drops of remote files to queue are handled in RemoteFileControlDDEnd
  7664. (FDDTargetControl != QueueView3))
  7665. {
  7666. DebugAssert(!FDragTempDir.IsEmpty());
  7667. TTransferType Type;
  7668. UnicodeString TempDir = FDragTempDir;
  7669. // We clear FDragTempDir before calling
  7670. // just in case it fail (raises exception)
  7671. FDragTempDir = L"";
  7672. Type = (FLastDropEffect & DROPEFFECT_MOVE ? ttMove : Type = ttCopy);
  7673. TGUICopyParamType CopyParams = GUIConfiguration->CurrentCopyParam;
  7674. // empty directory parameter means temp directory -> don't display it!
  7675. UnicodeString TargetDir = L"";
  7676. int Options = 0;
  7677. if (!CopyParamDialog(tdToLocal, Type, true, FDDFileList,
  7678. TargetDir, CopyParams, (WinConfiguration->DDTransferConfirmation != asOff), true, Options))
  7679. {
  7680. Abort();
  7681. }
  7682. // TargetDir is set when dropped on local file control
  7683. // (this was workaround for legacy dirview event handling, now it should be
  7684. // made prettier)
  7685. if (TargetDir.IsEmpty())
  7686. {
  7687. TargetDir = TempDir;
  7688. if (ForceDirectories(ApiPath(TargetDir)))
  7689. {
  7690. DebugAssert(Terminal && !TargetDir.IsEmpty());
  7691. FPendingTempSpaceWarn = true;
  7692. try
  7693. {
  7694. FDragDropOperation = true;
  7695. int Params = cpTemporary |
  7696. (Type == ttMove ? cpDelete : 0);
  7697. DDDownload(FDDFileList, TargetDir, &CopyParams, Params);
  7698. Configuration->Usage->Inc(L"DownloadsDragDropExternalTemp");
  7699. }
  7700. __finally
  7701. {
  7702. FDragDropOperation = false;
  7703. FPendingTempSpaceWarn = false;
  7704. AddDelayedDirectoryDeletion(TargetDir, WinConfiguration->DDDeleteDelay);
  7705. }
  7706. }
  7707. else
  7708. {
  7709. throw Exception(FMTLOAD(CREATE_TEMP_DIR_ERROR, (TargetDir)));
  7710. }
  7711. }
  7712. }
  7713. }
  7714. //---------------------------------------------------------------------------
  7715. void __fastcall TCustomScpExplorerForm::DDDownload(
  7716. TStrings * FilesToCopy, const UnicodeString & TargetDir, TCopyParamType * CopyParam, int Params)
  7717. {
  7718. DebugAssert(!IsLocalBrowserMode());
  7719. TAutoBatch AutoBatch(this);
  7720. CopyParam->IncludeFileMask.SetRoots(TargetDir, FilesToCopy);
  7721. UpdateCopyParamCounters(*CopyParam);
  7722. Terminal->CopyToLocal(FilesToCopy, TargetDir, CopyParam, Params, NULL);
  7723. if (FLAGSET(Params, cpDelete) && (DropSourceControl == RemoteDriveView))
  7724. {
  7725. RemoteDriveView->UpdateDropSource();
  7726. }
  7727. }
  7728. //---------------------------------------------------------------------------
  7729. class TFakeDataObjectFilesEx : public TDataObjectFilesEx
  7730. {
  7731. public:
  7732. __fastcall TFakeDataObjectFilesEx(TFileList * AFileList, bool RenderPIDL,
  7733. bool RenderFilename) : TDataObjectFilesEx(AFileList, RenderPIDL, RenderFilename, true)
  7734. {
  7735. }
  7736. virtual bool __fastcall AllowData(const tagFORMATETC & FormatEtc)
  7737. {
  7738. return (FormatEtc.cfFormat == CF_HDROP) ? false :
  7739. TDataObjectFilesEx::AllowData(FormatEtc);
  7740. }
  7741. };
  7742. //---------------------------------------------------------------------------
  7743. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDCreateDataObject(
  7744. TObject * Sender, TDataObject *& DataObject)
  7745. {
  7746. if (FDDExtMapFile != NULL)
  7747. {
  7748. TFileList * FileList = DragDropFiles(Sender)->FileList;
  7749. if (!FileList->RenderPIDLs() || !FileList->RenderNames())
  7750. {
  7751. Abort();
  7752. }
  7753. if (FileList->Count > 0)
  7754. {
  7755. TDataObjectFilesEx * FilesObject = new TFakeDataObjectFilesEx(FileList, true, true);
  7756. if (!FilesObject->IsValid(true, true))
  7757. {
  7758. FilesObject->_Release();
  7759. }
  7760. else
  7761. {
  7762. DataObject = FilesObject;
  7763. }
  7764. }
  7765. }
  7766. }
  7767. //---------------------------------------------------------------------------
  7768. void __fastcall TCustomScpExplorerForm::GoToCommandLine()
  7769. {
  7770. DebugFail();
  7771. }
  7772. //---------------------------------------------------------------------------
  7773. void __fastcall TCustomScpExplorerForm::GoToTree()
  7774. {
  7775. ComponentVisible[fcRemoteTree] = true;
  7776. DriveView(osRemote)->SetFocus();
  7777. }
  7778. //---------------------------------------------------------------------------
  7779. TStrings * __fastcall TCustomScpExplorerForm::PanelExport(TOperationSide Side,
  7780. TPanelExport Export)
  7781. {
  7782. TCustomDirView * DirView = this->DirView(Side);
  7783. std::unique_ptr<TStrings> ExportData(new TStringList());
  7784. switch (Export)
  7785. {
  7786. case pePath:
  7787. ExportData->Add(DirView->PathName);
  7788. break;
  7789. case peFileList:
  7790. case peFullFileList:
  7791. {
  7792. bool FullPath = (Export == peFullFileList);
  7793. DirView->CreateFileList(false, FullPath, ExportData.get());
  7794. for (int Index = 0; Index < ExportData->Count; Index++)
  7795. {
  7796. if (ExportData->Strings[Index].Pos(L" ") > 0)
  7797. {
  7798. ExportData->Strings[Index] = FORMAT(L"\"%s\"", (ExportData->Strings[Index]));
  7799. }
  7800. }
  7801. }
  7802. break;
  7803. default:
  7804. DebugFail();
  7805. }
  7806. return ExportData.release();
  7807. }
  7808. //---------------------------------------------------------------------------
  7809. void __fastcall TCustomScpExplorerForm::PanelExport(TOperationSide Side,
  7810. TPanelExport Export, TPanelExportDestination Destination)
  7811. {
  7812. std::unique_ptr<TStrings> ExportData(PanelExport(Side, Export));
  7813. PanelExportStore(Side, Export, Destination, ExportData.get());
  7814. }
  7815. //---------------------------------------------------------------------------
  7816. void __fastcall TCustomScpExplorerForm::PanelExportStore(TOperationSide /*Side*/,
  7817. TPanelExport /*Export*/, TPanelExportDestination Destination,
  7818. TStrings * ExportData)
  7819. {
  7820. if (Destination == pedClipboard)
  7821. {
  7822. TInstantOperationVisualizer Visualizer;
  7823. CopyToClipboard(ExportData);
  7824. }
  7825. else
  7826. {
  7827. DebugFail();
  7828. }
  7829. }
  7830. //---------------------------------------------------------------------------
  7831. void __fastcall TCustomScpExplorerForm::Filter(TOperationSide Side)
  7832. {
  7833. TCustomDirView * DirView = this->DirView(Side);
  7834. UnicodeString Mask = DirView->Mask;
  7835. if (DoFilterMaskDialog(DirView, Mask))
  7836. {
  7837. DirView->Mask = TFileMasks::NormalizeMask(Mask);
  7838. Configuration->Usage->Inc(L"Filters");
  7839. }
  7840. }
  7841. //---------------------------------------------------------------------------
  7842. TQueueOperation __fastcall TCustomScpExplorerForm::DefaultQueueOperation()
  7843. {
  7844. return FQueueController->DefaultOperation();
  7845. }
  7846. //---------------------------------------------------------------------------
  7847. bool __fastcall TCustomScpExplorerForm::AllowQueueOperation(
  7848. TQueueOperation Operation, void ** Param)
  7849. {
  7850. switch (Operation)
  7851. {
  7852. case qoPreferences:
  7853. return true;
  7854. case qoGoTo:
  7855. return ComponentVisible[fcQueueView] && QueueView3->Enabled;
  7856. case qoOnceEmpty:
  7857. return IsAnythingQueued();
  7858. default:
  7859. return FQueueController->AllowOperation(Operation, Param);
  7860. }
  7861. }
  7862. //---------------------------------------------------------------------------
  7863. void __fastcall TCustomScpExplorerForm::GoToQueue()
  7864. {
  7865. if (DebugAlwaysTrue(QueueView3->Visible))
  7866. {
  7867. QueueView3->SetFocus();
  7868. }
  7869. }
  7870. //---------------------------------------------------------------------------
  7871. void __fastcall TCustomScpExplorerForm::ExecuteQueueOperation(
  7872. TQueueOperation Operation, void * Param)
  7873. {
  7874. if (Operation == qoGoTo)
  7875. {
  7876. GoToQueue();
  7877. }
  7878. else if (Operation == qoPreferences)
  7879. {
  7880. PreferencesDialog(pmQueue);
  7881. }
  7882. else
  7883. {
  7884. FQueueController->ExecuteOperation(Operation, Param);
  7885. }
  7886. }
  7887. //---------------------------------------------------------------------------
  7888. bool __fastcall TCustomScpExplorerForm::GetQueueEnabled()
  7889. {
  7890. return (Queue != NULL) && Queue->Enabled;
  7891. }
  7892. //---------------------------------------------------------------------------
  7893. void __fastcall TCustomScpExplorerForm::ToggleQueueEnabled()
  7894. {
  7895. DebugAssert(Queue != NULL);
  7896. if (Queue != NULL)
  7897. {
  7898. Queue->Enabled = !Queue->Enabled;
  7899. }
  7900. }
  7901. //---------------------------------------------------------------------------
  7902. void __fastcall TCustomScpExplorerForm::QueueView3ContextPopup(
  7903. TObject * /*Sender*/, TPoint & /*MousePos*/, bool & /*Handled*/)
  7904. {
  7905. FQueueActedItem = QueueView3->ItemFocused;
  7906. }
  7907. //---------------------------------------------------------------------------
  7908. /*virtual*/ int __fastcall TCustomScpExplorerForm::GetStaticComponentsHeight()
  7909. {
  7910. return TopDock->Height + (MessageDock->Visible ? MessageDock->Height : 0) + QueueSplitter->Height;
  7911. }
  7912. //---------------------------------------------------------------------------
  7913. int __fastcall TCustomScpExplorerForm::GetStaticQueuePanelComponentsHeight()
  7914. {
  7915. return
  7916. (QueueFileListSplitter->Visible ? QueueFileListSplitter->Height : 0) +
  7917. (QueueDock->Visible ? QueueDock->Height : 0) +
  7918. (QueueLabel->Visible ? QueueLabel->Height : 0);
  7919. }
  7920. //---------------------------------------------------------------------------
  7921. int __fastcall TCustomScpExplorerForm::GetMinQueueViewHeight()
  7922. {
  7923. return ScaleByTextHeight(this, 48);
  7924. }
  7925. //---------------------------------------------------------------------------
  7926. void __fastcall TCustomScpExplorerForm::QueueSplitterCanResize(
  7927. TObject * /*Sender*/, int & NewSize, bool & Accept)
  7928. {
  7929. // when queue is hidden by double-clicking splitter, stray attempt to
  7930. // resize the panel with strange value arrives, make sure it is ignored
  7931. if (ComponentVisible[fcQueueView])
  7932. {
  7933. int MaxHeightLimit = ClientHeight - GetStaticComponentsHeight() - RemotePanel->Constraints->MinHeight;
  7934. if (NewSize > MaxHeightLimit)
  7935. {
  7936. NewSize = MaxHeightLimit;
  7937. }
  7938. int MinHeightLimit =
  7939. GetStaticQueuePanelComponentsHeight() +
  7940. (QueueFileList->Visible ? QueueFileList->Height : 0) +
  7941. GetMinQueueViewHeight();
  7942. if (NewSize < MinHeightLimit)
  7943. {
  7944. NewSize = MinHeightLimit;
  7945. }
  7946. }
  7947. else
  7948. {
  7949. Accept = false;
  7950. }
  7951. }
  7952. //---------------------------------------------------------------------------
  7953. void __fastcall TCustomScpExplorerForm::QueueFileListSplitterCanResize(TObject *, int & NewSize, bool &)
  7954. {
  7955. int TotalHeight = GetStaticQueuePanelComponentsHeight() + NewSize;
  7956. int QueueViewHeight = QueuePanel->ClientHeight - TotalHeight;
  7957. if (QueueViewHeight < GetMinQueueViewHeight())
  7958. {
  7959. NewSize -= (GetMinQueueViewHeight() - QueueViewHeight);
  7960. }
  7961. }
  7962. //---------------------------------------------------------------------------
  7963. void __fastcall TCustomScpExplorerForm::QueueView3StartDrag(TObject * /*Sender*/,
  7964. TDragObject *& /*DragObject*/)
  7965. {
  7966. FQueueActedItem = QueueView3->ItemFocused;
  7967. FQueueScrollOnDragOver->StartDrag();
  7968. }
  7969. //---------------------------------------------------------------------------
  7970. void __fastcall TCustomScpExplorerForm::QueueView3EndDrag(
  7971. TObject *, TObject * DebugUsedArg(Target), int DebugUsedArg(X), int DebugUsedArg(Y))
  7972. {
  7973. FQueueScrollOnDragOver->EndDrag();
  7974. }
  7975. //---------------------------------------------------------------------------
  7976. void __fastcall TCustomScpExplorerForm::QueueView3DragOver(TObject * /*Sender*/,
  7977. TObject * Source, int X, int Y, TDragState /*State*/, bool & Accept)
  7978. {
  7979. Accept = true;
  7980. if (Source == QueueView3)
  7981. {
  7982. TListItem * DropTarget = QueueView3->GetItemAt(X, Y);
  7983. Accept = (DropTarget != NULL) && (FQueueActedItem != NULL);
  7984. if (Accept)
  7985. {
  7986. TQueueItemProxy * QueueItem;
  7987. TQueueItemProxy * DestQueueItem;
  7988. QueueItem = static_cast<TQueueItemProxy *>(FQueueActedItem->Data);
  7989. DestQueueItem = static_cast<TQueueItemProxy *>(DropTarget->Data);
  7990. Accept = (QueueItem != DestQueueItem) &&
  7991. (QueueItem->Status == TQueueItem::qsPending) &&
  7992. (DestQueueItem->Status == TQueueItem::qsPending);
  7993. }
  7994. FQueueScrollOnDragOver->DragOver(TPoint(X, Y));
  7995. }
  7996. }
  7997. //---------------------------------------------------------------------------
  7998. void __fastcall TCustomScpExplorerForm::QueueView3DragDrop(TObject * /*Sender*/,
  7999. TObject * /*Source*/, int /*X*/, int /*Y*/)
  8000. {
  8001. if ((FQueueActedItem != NULL) && (QueueView3->DropTarget != NULL))
  8002. {
  8003. TQueueItemProxy * QueueItem;
  8004. TQueueItemProxy * DestQueueItem;
  8005. QueueItem = static_cast<TQueueItemProxy *>(FQueueActedItem->Data);
  8006. DestQueueItem = static_cast<TQueueItemProxy *>(QueueView3->DropTarget->Data);
  8007. QueueItem->Move(DestQueueItem);
  8008. }
  8009. }
  8010. //---------------------------------------------------------------------------
  8011. void __fastcall TCustomScpExplorerForm::QueueView3Enter(TObject * /*Sender*/)
  8012. {
  8013. if ((QueueView3->ItemFocused == NULL) &&
  8014. (QueueView3->Items->Count > 0))
  8015. {
  8016. QueueView3->ItemFocused = QueueView3->Items->Item[0];
  8017. }
  8018. QueueLabelUpdateStatus();
  8019. }
  8020. //---------------------------------------------------------------------------
  8021. void __fastcall TCustomScpExplorerForm::QueueView3Exit(TObject * /*Sender*/)
  8022. {
  8023. QueueLabelUpdateStatus();
  8024. }
  8025. //---------------------------------------------------------------------------
  8026. void __fastcall TCustomScpExplorerForm::QueueLabelUpdateStatus()
  8027. {
  8028. QueueLabel->UpdateStatus();
  8029. }
  8030. //---------------------------------------------------------------------------
  8031. void __fastcall TCustomScpExplorerForm::QueueView3SelectItem(
  8032. TObject * /*Sender*/, TListItem * /*Item*/, bool Selected)
  8033. {
  8034. if (Selected)
  8035. {
  8036. NonVisualDataModule->UpdateNonVisibleActions();
  8037. }
  8038. }
  8039. //---------------------------------------------------------------------------
  8040. TDragDropFilesEx * __fastcall TCustomScpExplorerForm::DragDropFiles(TObject * Sender)
  8041. {
  8042. TDragDropFilesEx * Result = NULL;
  8043. if (Sender == SessionsPageControl)
  8044. {
  8045. Result = FSessionsDragDropFilesEx;
  8046. }
  8047. else if (Sender == QueueView3)
  8048. {
  8049. Result = FQueueDragDropFilesEx;
  8050. }
  8051. else
  8052. {
  8053. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  8054. if (DirView != NULL)
  8055. {
  8056. Result = DirView->DragDropFilesEx;
  8057. }
  8058. else
  8059. {
  8060. TCustomDriveView * DriveView = dynamic_cast<TCustomDriveView *>(Sender);
  8061. if (DriveView != NULL)
  8062. {
  8063. Result = DriveView->DragDropFilesEx;
  8064. }
  8065. }
  8066. }
  8067. DebugAssert(Result != NULL);
  8068. return Result;
  8069. }
  8070. //---------------------------------------------------------------------------
  8071. bool __fastcall TCustomScpExplorerForm::SelectedAllFilesInDirView(TCustomDirView * DView)
  8072. {
  8073. return (DView->SelCount == DView->FilesCount);
  8074. }
  8075. //---------------------------------------------------------------------------
  8076. bool __fastcall TCustomScpExplorerForm::DraggingAllFilesFromDirView(TOperationSide Side, TStrings * FileList)
  8077. {
  8078. return HasDirView[Side] && (DropSourceControl == DirView(Side)) && (FileList->Count == DirView(Side)->FilesCount);
  8079. }
  8080. //---------------------------------------------------------------------------
  8081. void __fastcall TCustomScpExplorerForm::RemoteFileControlDragDropFileOperation(
  8082. TObject * Sender, int Effect, UnicodeString TargetPath, bool ForceQueue, bool DragDrop)
  8083. {
  8084. DebugAssert(!IsLocalBrowserMode());
  8085. TFileOperation Operation;
  8086. switch (Effect)
  8087. {
  8088. case DROPEFFECT_MOVE:
  8089. Operation = foMove;
  8090. break;
  8091. case DROPEFFECT_COPY:
  8092. // occures on WinXP (reported by user)
  8093. default:
  8094. Operation = foCopy;
  8095. break;
  8096. };
  8097. TDragDropFilesEx * DragDropFilesEx = DragDropFiles(Sender);
  8098. // see a comment in TUnixDirView::PerformItemDragDropOperation
  8099. if (DragDropFilesEx->FileList->Count > 0)
  8100. {
  8101. TStrings * FileList = new TStringList();
  8102. try
  8103. {
  8104. for (int Index = 0; Index < DragDropFilesEx->FileList->Count; Index++)
  8105. {
  8106. FileList->Add(DragDropFilesEx->FileList->Items[Index]->Name);
  8107. }
  8108. FDragDropOperation = true;
  8109. TTransferOperationParam Param;
  8110. Param.TargetDirectory = TargetPath;
  8111. // upload, no temp dirs
  8112. Param.Temp = false;
  8113. Param.DragDrop = DragDrop;
  8114. Param.Options =
  8115. FLAGMASK(DraggingAllFilesFromDirView(osLocal, FileList), coAllFiles);
  8116. if (ForceQueue)
  8117. {
  8118. Param.Queue = asOn;
  8119. }
  8120. bool NoConfirmation = DragDrop ? (WinConfiguration->DDTransferConfirmation == asOff) : false;
  8121. if (ExecuteFileOperation(Operation, osLocal, FileList, NoConfirmation, &Param))
  8122. {
  8123. if (IsFileControl(DropSourceControl, osLocal))
  8124. {
  8125. Configuration->Usage->Inc(L"UploadsDragDropInternal");
  8126. }
  8127. else
  8128. {
  8129. Configuration->Usage->Inc(L"UploadsDragDropExternal");
  8130. }
  8131. }
  8132. }
  8133. __finally
  8134. {
  8135. FDragDropOperation = false;
  8136. delete FileList;
  8137. }
  8138. }
  8139. }
  8140. //---------------------------------------------------------------------------
  8141. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDFileOperation(
  8142. TObject * Sender, int Effect, UnicodeString /*SourcePath*/,
  8143. UnicodeString TargetPath, bool Paste, bool & /*DoOperation*/)
  8144. {
  8145. RemoteFileControlDragDropFileOperation(Sender, Effect, TargetPath, false, !Paste);
  8146. }
  8147. //---------------------------------------------------------------------------
  8148. void __fastcall TCustomScpExplorerForm::RemoteFileContolDDChooseEffect(
  8149. TObject * Sender, int grfKeyState, int & dwEffect)
  8150. {
  8151. DebugAssert(!IsLocalBrowserMode());
  8152. // if any drop effect is allowed at all (e.g. no drop to self and drop to parent)
  8153. if ((dwEffect != DROPEFFECT_NONE) &&
  8154. IsFileControl(DropSourceControl, osRemote))
  8155. {
  8156. // do not allow drop on remote panel (on free space, still allow drop on directories)
  8157. if ((Sender == RemoteDirView) && (DropSourceControl == RemoteDirView) &&
  8158. (RemoteDirView->DropTarget == NULL))
  8159. {
  8160. dwEffect = DROPEFFECT_NONE;
  8161. }
  8162. else
  8163. {
  8164. if (dwEffect == DROPEFFECT_COPY)
  8165. {
  8166. bool MoveCapable = Terminal->IsCapable[fcRemoteMove];
  8167. // currently we support copying always (at least via temporary directory);
  8168. // remove associated checks once this all proves stable and working
  8169. bool CopyCapable = true;
  8170. // if we do not support neither of operations, there's no discussion
  8171. if (!MoveCapable && !CopyCapable)
  8172. {
  8173. dwEffect = DROPEFFECT_NONE;
  8174. }
  8175. else
  8176. {
  8177. // The default effect inside remote panel is move,
  8178. // unless we do not support it, but support copy
  8179. if (FLAGCLEAR(grfKeyState, MK_CONTROL))
  8180. {
  8181. dwEffect = MoveCapable ? DROPEFFECT_MOVE : DROPEFFECT_COPY;
  8182. }
  8183. else
  8184. {
  8185. // with ctrl-down, we want copy unless it is not supported
  8186. dwEffect = CopyCapable ? DROPEFFECT_COPY : DROPEFFECT_NONE;
  8187. }
  8188. }
  8189. }
  8190. }
  8191. }
  8192. }
  8193. //---------------------------------------------------------------------------
  8194. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDDragFileName(
  8195. TObject * Sender, TRemoteFile * File, UnicodeString & FileName)
  8196. {
  8197. DebugAssert(!IsLocalBrowserMode());
  8198. if (FDDTotalSize >= 0)
  8199. {
  8200. if (File->IsDirectory)
  8201. {
  8202. FDDTotalSize = -1;
  8203. }
  8204. else
  8205. {
  8206. FDDTotalSize += File->Size;
  8207. }
  8208. }
  8209. DebugAssert(!FDragTempDir.IsEmpty());
  8210. // TODO: this is quite ineffective
  8211. // TODO: what if invalid character replacement is disabled?
  8212. FileName = FDragTempDir + GUIConfiguration->CurrentCopyParam.ValidLocalFileName(File->FileName);
  8213. UnicodeString TransferFileName;
  8214. if (Sender == RemoteDriveView)
  8215. {
  8216. TransferFileName = UnixExcludeTrailingBackslash(File->FullFileName);
  8217. }
  8218. else
  8219. {
  8220. TransferFileName = File->FileName;
  8221. }
  8222. FDDFileList->AddObject(TransferFileName, File);
  8223. }
  8224. //---------------------------------------------------------------------------
  8225. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDDragDetect(
  8226. TObject * /*Sender*/, int /*grfKeyState*/, const TPoint & /*DetectStart*/,
  8227. const TPoint & /*Point*/, TDragDetectStatus /*DragStatus*/)
  8228. {
  8229. // sometimes we do not get DDEnd so the list is not released
  8230. SAFE_DESTROY(FDDFileList);
  8231. FDDFileList = new TStringList();
  8232. FDragTempDir = WinConfiguration->TemporaryDir();
  8233. FDDTotalSize = 0;
  8234. FDragDropSshTerminate.reset(NULL);
  8235. }
  8236. //---------------------------------------------------------------------------
  8237. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDQueryContinueDrag(
  8238. TObject * /*Sender*/, BOOL /*FEscapePressed*/, int /*grfKeyState*/,
  8239. HRESULT & Result)
  8240. {
  8241. if (Result == DRAGDROP_S_DROP)
  8242. {
  8243. try
  8244. {
  8245. GlobalDragImageList->HideDragImage();
  8246. try
  8247. {
  8248. RemoteFileControlDDTargetDrop();
  8249. }
  8250. catch(ESshTerminate & E)
  8251. {
  8252. FDragDropSshTerminate.reset(E.Clone());
  8253. }
  8254. }
  8255. catch (Exception &E)
  8256. {
  8257. // If downloading fails we need to cancel drag&drop, otherwise
  8258. // Explorer shows error
  8259. // But by the way exception probably never reach this point as
  8260. // it's catched on way.
  8261. // Fatal exceptions get here (like when opening a secondary shell extension for file duplication fails).
  8262. Result = DRAGDROP_S_CANCEL;
  8263. ShowExtendedException(Terminal, &E);
  8264. }
  8265. }
  8266. }
  8267. //---------------------------------------------------------------------------
  8268. void __fastcall TCustomScpExplorerForm::DirViewMatchMask(
  8269. TObject * /*Sender*/, UnicodeString FileName, bool Directory, __int64 Size,
  8270. TDateTime Modification, UnicodeString Masks, bool & Matches, bool AllowImplicitMatches)
  8271. {
  8272. TFileMasks::TParams MaskParams;
  8273. MaskParams.Size = Size;
  8274. MaskParams.Modification = Modification;
  8275. // this does not re-parse the mask if it is the same as the last time
  8276. FDirViewMatchMask = Masks;
  8277. bool ImplicitMatch;
  8278. Matches =
  8279. // Local and RecurseInclude parameters have no effect as there's no path.
  8280. // This should use potential MatchesFileName overload with ImplicitMatch argument.
  8281. FDirViewMatchMask.Matches(FileName, false, Directory, &MaskParams, true, ImplicitMatch) &&
  8282. (AllowImplicitMatches || !ImplicitMatch);
  8283. }
  8284. //---------------------------------------------------------------------------
  8285. void __fastcall TCustomScpExplorerForm::DirViewGetOverlay(
  8286. TObject * Sender, TListItem * Item, WORD & Indexes)
  8287. {
  8288. TCustomDirView * DirView = reinterpret_cast<TCustomDirView *>(Sender);
  8289. UnicodeString Ext;
  8290. if (dynamic_cast<TUnixDirView *>(DirView) != NULL)
  8291. {
  8292. Ext = UnixExtractFileExt(DirView->ItemFileName(Item));
  8293. }
  8294. else
  8295. {
  8296. Ext = ExtractFileExt(DirView->ItemFileName(Item));
  8297. }
  8298. if (SameText(Ext, Configuration->PartialExt))
  8299. {
  8300. Indexes |= oiPartial;
  8301. }
  8302. }
  8303. //---------------------------------------------------------------------------
  8304. bool __fastcall TCustomScpExplorerForm::CanPasteToDirViewFromClipBoard()
  8305. {
  8306. return
  8307. DirViewEnabled(osCurrent) &&
  8308. DirView(osCurrent)->CanPasteFromClipBoard();
  8309. }
  8310. //---------------------------------------------------------------------------
  8311. bool __fastcall TCustomScpExplorerForm::CanPasteFromClipBoard()
  8312. {
  8313. bool Result = false;
  8314. if (CanPasteToDirViewFromClipBoard())
  8315. {
  8316. Result = true;
  8317. }
  8318. else
  8319. {
  8320. UnicodeString ClipboardText;
  8321. if (NonEmptyTextFromClipboard(ClipboardText) &&
  8322. (ClipboardText.Pos(L"\n") == 0)) // it's already trimmed
  8323. {
  8324. if (StoredSessions->IsUrl(ClipboardText))
  8325. {
  8326. Result = true;
  8327. }
  8328. else
  8329. {
  8330. Result = DirViewEnabled(osCurrent);
  8331. }
  8332. }
  8333. }
  8334. return Result;
  8335. }
  8336. //---------------------------------------------------------------------------
  8337. void __fastcall TCustomScpExplorerForm::PasteFromClipBoard()
  8338. {
  8339. // Pasting to any local panel is handled in TScpCommanderForm::PasteFromClipBoard()
  8340. if (DoesClipboardContainOurFiles())
  8341. {
  8342. if (DebugAlwaysTrue(CanPasteToDirViewFromClipBoard()))
  8343. {
  8344. DebugAssert(!IsLocalBrowserMode());
  8345. TTerminalManager * Manager = TTerminalManager::Instance();
  8346. TTerminal * TargetTerminal = Manager->ActiveTerminal;
  8347. Manager->ActiveSession = FClipboardTerminal;
  8348. ExecuteFileOperation(foRemoteCopy, osRemote, FClipboardFileList.get(), false, TargetTerminal);
  8349. }
  8350. }
  8351. else if (CanPasteToDirViewFromClipBoard())
  8352. {
  8353. DirView(osCurrent)->PasteFromClipBoard();
  8354. }
  8355. else
  8356. {
  8357. UnicodeString ClipboardText;
  8358. if (NonEmptyTextFromClipboard(ClipboardText))
  8359. {
  8360. if (StoredSessions->IsUrl(ClipboardText))
  8361. {
  8362. NewSession(ClipboardText);
  8363. }
  8364. else
  8365. {
  8366. DirView(osCurrent)->Path = ClipboardText;
  8367. }
  8368. }
  8369. }
  8370. }
  8371. //---------------------------------------------------------------------------
  8372. void __fastcall TCustomScpExplorerForm::FileListFromClipboard()
  8373. {
  8374. // TBD
  8375. }
  8376. //---------------------------------------------------------------------------
  8377. void __fastcall TCustomScpExplorerForm::SelectAll(TOperationSide Side, TSelectMode Mode)
  8378. {
  8379. TCustomDirView * ADirView = DirView(Side);
  8380. ADirView->SelectAll(Mode);
  8381. ADirView->SetFocus();
  8382. }
  8383. //---------------------------------------------------------------------------
  8384. void __fastcall TCustomScpExplorerForm::SelectByMask(TOperationSide Side, bool Select)
  8385. {
  8386. TCustomDirView * ADirView = DirView(Side);
  8387. TFileFilter Filter;
  8388. DefaultFileFilter(Filter);
  8389. if (DoSelectMaskDialog(ADirView, Select, Filter))
  8390. {
  8391. Configuration->Usage->Inc(L"MaskSelections");
  8392. ADirView->SelectFiles(Filter, Select);
  8393. ADirView->SetFocus();
  8394. }
  8395. }
  8396. //---------------------------------------------------------------------------
  8397. void __fastcall TCustomScpExplorerForm::RestoreSelectedNames(TOperationSide Side)
  8398. {
  8399. TCustomDirView * ADirView = DirView(Side);
  8400. ADirView->RestoreSelectedNames();
  8401. ADirView->SetFocus();
  8402. }
  8403. //---------------------------------------------------------------------------
  8404. void __fastcall TCustomScpExplorerForm::SelectSameExt(bool Select)
  8405. {
  8406. TCustomDirView * CurrentDirView = DirView(osCurrent);
  8407. if (DebugAlwaysTrue(CurrentDirView->ItemFocused != NULL))
  8408. {
  8409. UnicodeString FileName = CurrentDirView->ItemFileName(CurrentDirView->ItemFocused);
  8410. UnicodeString Ext;
  8411. if (!IsSideLocalBrowser(osCurrent))
  8412. {
  8413. Ext = UnixExtractFileExt(FileName);
  8414. }
  8415. else
  8416. {
  8417. Ext = ExtractFileExt(FileName);
  8418. }
  8419. if (Ext.IsEmpty())
  8420. {
  8421. Ext = L".";
  8422. }
  8423. TFileFilter Filter;
  8424. Filter.Masks = FORMAT(L"*%s", (Ext));
  8425. Filter.Directories = false;
  8426. CurrentDirView->SelectFiles(Filter, Select);
  8427. }
  8428. }
  8429. //---------------------------------------------------------------------------
  8430. UnicodeString __fastcall TCustomScpExplorerForm::FileStatusBarText(
  8431. const TStatusFileInfo & FileInfo, TOperationSide Side)
  8432. {
  8433. UnicodeString Result;
  8434. if (!FIncrementalSearch.IsEmpty() && (Side == GetSide(osCurrent)))
  8435. {
  8436. Result = FormatIncrementalSearchStatus(FIncrementalSearch, FIncrementalSearchHaveNext);
  8437. }
  8438. else if (!IsSideLocalBrowser(Side) && ((Terminal == NULL) || Terminal->Disconnected))
  8439. {
  8440. // noop
  8441. }
  8442. else
  8443. {
  8444. Result =
  8445. FMTLOAD(FILE_INFO_FORMAT,
  8446. (FormatBytes(FileInfo.SelectedSize),
  8447. FormatBytes(FileInfo.FilesSize),
  8448. FormatNumber(FileInfo.SelectedCount),
  8449. FormatNumber(FileInfo.FilesCount)));
  8450. }
  8451. return Result;
  8452. }
  8453. //---------------------------------------------------------------------------
  8454. void __fastcall TCustomScpExplorerForm::FileStatusBarPanelClick(
  8455. TTBXStatusPanel * Panel, TOperationSide Side)
  8456. {
  8457. if (Panel->Index == 1)
  8458. {
  8459. ToggleShowHiddenFiles();
  8460. }
  8461. else if (Panel->Index == 2)
  8462. {
  8463. Filter(Side);
  8464. }
  8465. }
  8466. //---------------------------------------------------------------------------
  8467. void __fastcall TCustomScpExplorerForm::UpdateFileStatusBar(
  8468. TTBXStatusBar * StatusBar, const TStatusFileInfo & FileInfo, TOperationSide Side)
  8469. {
  8470. DebugAssert(!StatusBar->SimplePanel);
  8471. StatusBar->Panels->Items[0]->Caption = FileStatusBarText(FileInfo, Side);
  8472. UpdateFileStatusExtendedPanels(StatusBar, FileInfo);
  8473. }
  8474. //---------------------------------------------------------------------------
  8475. void __fastcall TCustomScpExplorerForm::UpdateFileStatusExtendedPanels(
  8476. TTBXStatusBar * StatusBar, const TStatusFileInfo & FileInfo)
  8477. {
  8478. DebugAssert(StatusBar->Panels->Count >= 3);
  8479. TTBXStatusPanel * HiddenFilesPanel = StatusBar->Panels->Items[1];
  8480. if (FileInfo.HiddenCount > 0)
  8481. {
  8482. HiddenFilesPanel->Caption = FMTLOAD(FILE_INFO_HIDDEN2, (FormatNumber(FileInfo.HiddenCount)));
  8483. HiddenFilesPanel->ViewPriority = 90; // <100 allows hiding panel when it does not fit
  8484. }
  8485. else
  8486. {
  8487. HiddenFilesPanel->ViewPriority = 0;
  8488. // not really necessary, just to cleanup no-longer-valid data
  8489. HiddenFilesPanel->Caption = L"";
  8490. }
  8491. TTBXStatusPanel * FilteredFilesPanel = StatusBar->Panels->Items[2];
  8492. if (FileInfo.FilteredCount > 0)
  8493. {
  8494. FilteredFilesPanel->Caption = FMTLOAD(FILE_INFO_FILTERED2, (FormatNumber(FileInfo.FilteredCount)));
  8495. FilteredFilesPanel->ViewPriority = 90; // <100 allows hiding panel when it does not fit
  8496. }
  8497. else
  8498. {
  8499. FilteredFilesPanel->ViewPriority = 0;
  8500. // not really necessary, just to cleanup no-longer-valid data
  8501. FilteredFilesPanel->Caption = L"";
  8502. }
  8503. }
  8504. //---------------------------------------------------------------------------
  8505. void __fastcall TCustomScpExplorerForm::RemoteStatusBarClick(
  8506. TObject * /*Sender*/)
  8507. {
  8508. if (DirView(osOther)->Enabled)
  8509. {
  8510. DirView(osOther)->SetFocus();
  8511. }
  8512. }
  8513. //---------------------------------------------------------------------------
  8514. void __fastcall TCustomScpExplorerForm::ToggleQueueVisibility()
  8515. {
  8516. TQueueViewConfiguration Config = WinConfiguration->QueueView;
  8517. switch (Config.Show)
  8518. {
  8519. case qvShow:
  8520. if ((FQueueStatus != NULL) && (FQueueStatus->Count > 0))
  8521. {
  8522. Config.Show = qvHide;
  8523. }
  8524. else
  8525. {
  8526. Config.Show = Config.LastHideShow;
  8527. }
  8528. break;
  8529. case qvHideWhenEmpty:
  8530. if (ComponentVisible[fcQueueView])
  8531. {
  8532. Config.Show = qvHide;
  8533. }
  8534. else
  8535. {
  8536. Config.LastHideShow = Config.Show;
  8537. Config.Show = qvShow;
  8538. }
  8539. break;
  8540. case qvHide:
  8541. Config.LastHideShow = Config.Show;
  8542. Config.Show = qvShow;
  8543. break;
  8544. }
  8545. WinConfiguration->QueueView = Config;
  8546. }
  8547. //---------------------------------------------------------------------------
  8548. UnicodeString __fastcall TCustomScpExplorerForm::PathForCaption()
  8549. {
  8550. UnicodeString Result;
  8551. if (Terminal != NULL)
  8552. {
  8553. switch (WinConfiguration->PathInCaption)
  8554. {
  8555. case picShort:
  8556. Result = ExtractShortName(Terminal->CurrentDirectory, true);
  8557. break;
  8558. case picFull:
  8559. Result = Terminal->CurrentDirectory;
  8560. break;
  8561. }
  8562. }
  8563. return Result;
  8564. }
  8565. //---------------------------------------------------------------------------
  8566. TColor __fastcall TCustomScpExplorerForm::PanelColor()
  8567. {
  8568. TColor Result = GetWindowColor(FSessionColor);
  8569. return Result;
  8570. }
  8571. //---------------------------------------------------------------------------
  8572. TColor __fastcall TCustomScpExplorerForm::DisabledPanelColor()
  8573. {
  8574. TColor Result = (WinConfiguration->UseDarkTheme() ? static_cast<TColor>(RGB(0x40, 0x40, 0x40)) : clBtnFace);
  8575. return Result;
  8576. }
  8577. //---------------------------------------------------------------------------
  8578. void __fastcall TCustomScpExplorerForm::UpdateControls()
  8579. {
  8580. if (!FSessionChanging)
  8581. {
  8582. TTerminalManager::Instance()->UpdateAppTitle();
  8583. }
  8584. // WORKAROUND: Disabling list view when it is not showing yet does not set its
  8585. // background to gray on Windows 7 (works on Windows 10).
  8586. // See also EnableControl
  8587. if (Showing)
  8588. {
  8589. if (ManagedSession != NULL)
  8590. {
  8591. // Update path when it changes
  8592. if ((SessionsPageControl->ActivePage != NULL) &&
  8593. (GetSessionTabSession(SessionsPageControl->ActivePage) == ManagedSession))
  8594. {
  8595. UpdateSessionTab(SessionsPageControl->ActivePage);
  8596. }
  8597. }
  8598. NonVisualDataModule->ReconnectSessionAction->Update();
  8599. ReconnectToolbar->Visible = NonVisualDataModule->ReconnectSessionAction->Visible;
  8600. // ReconnectSessionAction is hidden when disabled, so enabling it actualy resizes the toolbar
  8601. CenterReconnectToolbar();
  8602. bool HasTerminal = HasActiveTerminal();
  8603. RemoteDirView->Enabled = HasTerminal;
  8604. RemoteDirView->Color = HasTerminal ? PanelColor() : DisabledPanelColor();
  8605. RemoteDirView->Font->Color = GetWindowTextColor(RemoteDirView->Color);
  8606. RemoteDriveView->Enabled = RemoteDirView->Enabled;
  8607. RemoteDriveView->Color = RemoteDirView->Color;
  8608. RemoteDriveView->Font->Color = RemoteDirView->Font->Color;
  8609. QueueView3->Enabled = HasTerminal && Terminal->IsCapable[fcBackgroundTransfers];
  8610. QueueView3->Color = QueueView3->Enabled ? GetWindowColor() : DisabledPanelColor();
  8611. QueueView3->Font->Color = GetWindowTextColor(QueueView3->Color);
  8612. QueueFileList->Enabled = QueueView3->Enabled;
  8613. QueueFileList->Color = QueueView3->Color;
  8614. QueueFileList->Font->Color = QueueView3->Font->Color;
  8615. QueueLabelUpdateStatus();
  8616. RemoteDirView->DarkMode = WinConfiguration->UseDarkTheme();
  8617. RemoteDriveView->DarkMode = RemoteDirView->DarkMode;
  8618. if (FImmersiveDarkMode != WinConfiguration->UseDarkTheme())
  8619. {
  8620. UpdateDarkMode();
  8621. }
  8622. reinterpret_cast<TTBCustomItem *>(GetComponent(fcRemotePathComboBox))->Enabled = HasTerminal || IsLocalBrowserMode();
  8623. MessageDock->Visible = (MessageDock->ToolbarCount > 0);
  8624. }
  8625. }
  8626. //---------------------------------------------------------------------------
  8627. void __fastcall TCustomScpExplorerForm::DoDirViewLoaded(TCustomDirView * /*Sender*/)
  8628. {
  8629. UpdateControls();
  8630. }
  8631. //---------------------------------------------------------------------------
  8632. void __fastcall TCustomScpExplorerForm::DirViewLoaded(
  8633. TObject * Sender)
  8634. {
  8635. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  8636. DebugAssert(DirView != NULL);
  8637. DoDirViewLoaded(DirView);
  8638. TransferPresetAutoSelect();
  8639. }
  8640. //---------------------------------------------------------------------------
  8641. void __fastcall TCustomScpExplorerForm::StartUpdates()
  8642. {
  8643. TUpdatesConfiguration Updates = WinConfiguration->Updates;
  8644. // first run after installation
  8645. if (double(Updates.LastCheck) == 0)
  8646. {
  8647. AppLog(L"First run, scheduling updates check for the next run");
  8648. // make sure next time there will be an update (if enabled)
  8649. Updates.LastCheck = TDateTime(1);
  8650. WinConfiguration->Updates = Updates;
  8651. }
  8652. else
  8653. {
  8654. TDateTime Period;
  8655. if (IsUWP() && !IsOfficialPackage())
  8656. {
  8657. Period = DefaultUpdatesPeriod;
  8658. AppLog(L"Thirdparty UWP package, using default updates check period");
  8659. }
  8660. else
  8661. {
  8662. Period = Updates.Period;
  8663. }
  8664. AppLogFmt(L"Updates check period: %.2f", (double(Period)));
  8665. if (double(Period) > 0)
  8666. {
  8667. TDateTime Interval = Now() - Updates.LastCheck;
  8668. AppLogFmt(L"Interval since the last updates check: %.2f", (double(Interval)));
  8669. if (Interval >= Period)
  8670. {
  8671. StartUpdateThread(UpdatesChecked);
  8672. }
  8673. }
  8674. }
  8675. }
  8676. //---------------------------------------------------------------------------
  8677. void __fastcall TCustomScpExplorerForm::UpdatesChecked()
  8678. {
  8679. AppLog(L"Updates check done");
  8680. UnicodeString Message;
  8681. bool New;
  8682. TQueryType Type;
  8683. GetUpdatesMessage(Message, New, Type, false);
  8684. if (!Message.IsEmpty())
  8685. {
  8686. AppLogFmt(L"Updates check message: %s", (Message));
  8687. if (New)
  8688. {
  8689. AppLog(L"New version detected");
  8690. Message = FMTLOAD(NEW_VERSION_CLICK, (Message));
  8691. }
  8692. if (!New && (Type != qtWarning))
  8693. {
  8694. AppLog(L"Posting non-critical updates note");
  8695. PostNote(UnformatMessage(Message), 0, UpdatesNoteClicked, NULL);
  8696. }
  8697. else
  8698. {
  8699. AppLog(L"Posting new version or critical message notification");
  8700. Notify(NULL, Message, Type, true, UpdatesNoteClicked);
  8701. }
  8702. if (New)
  8703. {
  8704. Configuration->Usage->Inc(L"UpdateNotifications");
  8705. }
  8706. }
  8707. }
  8708. //---------------------------------------------------------------------------
  8709. void __fastcall TCustomScpExplorerForm::UpdatesNoteClicked(TObject * /*Sender*/)
  8710. {
  8711. RestoreApp();
  8712. bool CanDisplay = !NonVisualDataModule->Busy;
  8713. if (!CanDisplay && (Screen->ActiveForm != NULL))
  8714. {
  8715. CanDisplay = (Screen->ActiveForm->Perform(WM_CAN_DISPLAY_UPDATES, 0, 0) != 0);
  8716. }
  8717. if (CanDisplay)
  8718. {
  8719. Configuration->Usage->Inc(L"UpdateNotificationsClicked");
  8720. // Probably not needed as CheckForUpdates is blocking since we started using neon for HTTP implementation.
  8721. NonVisualDataModule->StartBusy();
  8722. try
  8723. {
  8724. CheckForUpdates(true);
  8725. }
  8726. __finally
  8727. {
  8728. NonVisualDataModule->EndBusy();
  8729. }
  8730. }
  8731. else
  8732. {
  8733. Configuration->Usage->Inc(L"UpdateNotificationsBusy");
  8734. }
  8735. }
  8736. //---------------------------------------------------------------------------
  8737. void __fastcall TCustomScpExplorerForm::GetTransferPresetAutoSelectData(
  8738. TCopyParamRuleData & Data)
  8739. {
  8740. DebugAssert(Terminal != NULL);
  8741. Data.HostName = Terminal->SessionData->HostNameExpanded;
  8742. Data.UserName = Terminal->SessionData->UserNameExpanded;
  8743. Data.RemoteDirectory = RemoteDirView->PathName;
  8744. }
  8745. //---------------------------------------------------------------------------
  8746. void __fastcall TCustomScpExplorerForm::TransferPresetAutoSelect()
  8747. {
  8748. // Terminal can be null when we are changing local directory implicitly,
  8749. // because it has been deleted, while no session is connected
  8750. // (Login dialog is open)
  8751. if (FAllowTransferPresetAutoSelect && (Terminal != NULL))
  8752. {
  8753. DebugAssert(!IsLocalBrowserMode());
  8754. TCopyParamRuleData Data;
  8755. GetTransferPresetAutoSelectData(Data);
  8756. int CopyParamIndex = GUIConfiguration->CopyParamList->Find(Data);
  8757. UnicodeString CopyParamCurrent = GUIConfiguration->CopyParamCurrent;
  8758. if (CopyParamIndex < 0)
  8759. {
  8760. // there is no preset that matches autoselection
  8761. // set preset that we consider "default"
  8762. FCopyParamAutoSelected = L""; // forget last autoselected preset
  8763. GUIConfiguration->CopyParamCurrent = FCopyParamDefault;
  8764. }
  8765. else
  8766. {
  8767. // there is preset matching autoselection
  8768. UnicodeString CopyParamName = GUIConfiguration->CopyParamList->Names[CopyParamIndex];
  8769. if (CopyParamName == FCopyParamAutoSelected)
  8770. {
  8771. // autoselected the same preset as the last time
  8772. // make no change (i.e. preserve custom user preset, if any)
  8773. }
  8774. else
  8775. {
  8776. // autoselected the different preset then the last time (or there
  8777. // was default preset set)
  8778. FCopyParamAutoSelected = CopyParamName; // remember autoselection
  8779. GUIConfiguration->CopyParamCurrent = CopyParamName;
  8780. }
  8781. }
  8782. if (GUIConfiguration->CopyParamCurrent != CopyParamCurrent)
  8783. {
  8784. if (CopyParamIndex >= 0)
  8785. {
  8786. Configuration->Usage->Inc(L"CopyParamAutoSelects");
  8787. }
  8788. TTransferPresetNoteData * Data = new TTransferPresetNoteData;
  8789. try
  8790. {
  8791. int Fmt =
  8792. (CopyParamIndex < 0) ?
  8793. (GUIConfiguration->CopyParamIndex < 0 ? COPY_PARAM_DEFAULT_NORM : COPY_PARAM_DEFAULT_CUSTOM) :
  8794. COPY_PARAM_AUTOSELECTED;
  8795. UnicodeString Message = FMTLOAD(Fmt, (StripHotkey(GUIConfiguration->CopyParamCurrent)));
  8796. Data->Message = MainInstructions(Message);
  8797. int CopyParamAttrs = Terminal->UsableCopyParamAttrs(0).General;
  8798. UnicodeString Info = GUIConfiguration->CurrentCopyParam.GetInfoStr(L"\n",
  8799. CopyParamAttrs);
  8800. if (CopyParamIndex >= 0)
  8801. {
  8802. DebugAssert(GUIConfiguration->CopyParamList->Rules[CopyParamIndex] != NULL);
  8803. Info = FORMAT(L"%s\n \n%s", (Info,
  8804. FMTLOAD(COPY_PARAM_RULE,
  8805. (GUIConfiguration->CopyParamList->Rules[CopyParamIndex]->GetInfoStr(L"\n")))));
  8806. }
  8807. Data->Message += L"\n\n" + Info;
  8808. if (WinConfiguration->CopyParamAutoSelectNotice)
  8809. {
  8810. TransferPresetNoteMessage(Data, true);
  8811. }
  8812. else
  8813. {
  8814. PostNote(Message, 0, TransferPresetNoteClicked, Data);
  8815. Data = NULL; // ownership passed
  8816. }
  8817. }
  8818. __finally
  8819. {
  8820. delete Data;
  8821. }
  8822. }
  8823. }
  8824. }
  8825. //---------------------------------------------------------------------------
  8826. void __fastcall TCustomScpExplorerForm::TransferPresetNoteMessage(
  8827. TTransferPresetNoteData * NoteData, bool AllowNeverAskAgain)
  8828. {
  8829. DebugAssert(NoteData != NULL);
  8830. DebugAssert(!IsLocalBrowserMode());
  8831. TMessageParams Params(AllowNeverAskAgain ? mpNeverAskAgainCheck : 0);
  8832. TQueryButtonAlias Aliases[1];
  8833. Aliases[0].Button = qaIgnore; // "ignore" is after "ok"
  8834. Aliases[0].Alias = LoadStr(CONFIGURE_BUTTON);
  8835. Params.Aliases = Aliases;
  8836. Params.AliasesCount = LENOF(Aliases);
  8837. unsigned int Result =
  8838. MoreMessageDialog(NoteData->Message, NULL, qtInformation,
  8839. qaOK | qaIgnore, HELP_COPY_PARAM_AUTOSELECTED, &Params);
  8840. switch (Result)
  8841. {
  8842. case qaNeverAskAgain:
  8843. DebugAssert(AllowNeverAskAgain);
  8844. WinConfiguration->CopyParamAutoSelectNotice = false;
  8845. break;
  8846. case qaIgnore:
  8847. PreferencesDialog(pmPresets);
  8848. break;
  8849. }
  8850. }
  8851. //---------------------------------------------------------------------------
  8852. void __fastcall TCustomScpExplorerForm::TransferPresetNoteClicked(TObject * Sender)
  8853. {
  8854. // as of now useless, as this is used for notes only, never for balloons, ...
  8855. RestoreApp();
  8856. // .. and we should never be busy here
  8857. if (DebugAlwaysTrue(!NonVisualDataModule->Busy))
  8858. {
  8859. TransferPresetNoteMessage(DebugNotNull(dynamic_cast<TTransferPresetNoteData *>(Sender)), false);
  8860. }
  8861. }
  8862. //---------------------------------------------------------------------------
  8863. void __fastcall TCustomScpExplorerForm::PreferencesDialog(
  8864. TPreferencesMode APreferencesMode)
  8865. {
  8866. std::unique_ptr<TPreferencesDialogData> PreferencesData;
  8867. TCopyParamRuleData Data;
  8868. if (Terminal != NULL)
  8869. {
  8870. PreferencesData.reset(new TPreferencesDialogData());
  8871. GetTransferPresetAutoSelectData(Data);
  8872. PreferencesData->CopyParamRuleData = &Data;
  8873. }
  8874. DoPreferencesDialog(APreferencesMode, PreferencesData.get());
  8875. }
  8876. //---------------------------------------------------------------------------
  8877. void __fastcall TCustomScpExplorerForm::AdHocCustomCommandValidate(
  8878. const TCustomCommandType & Command)
  8879. {
  8880. if (CustomCommandState(Command, FEditingFocusedAdHocCommand, ccltAll) <= 0)
  8881. {
  8882. throw Exception(FMTLOAD(CUSTOM_COMMAND_IMPOSSIBLE2, (Command.Command)));
  8883. }
  8884. }
  8885. //---------------------------------------------------------------------------
  8886. void __fastcall TCustomScpExplorerForm::AdHocCustomCommand(bool OnFocused)
  8887. {
  8888. bool RemoteAllowed = CustomCommandRemoteAllowed();
  8889. TCustomCommandType Command;
  8890. // make sure we use local custom command when remote are not supported
  8891. if (RemoteAllowed || FLAGSET(FLastCustomCommand.Params, ccLocal))
  8892. {
  8893. Command = FLastCustomCommand;
  8894. }
  8895. else
  8896. {
  8897. Command.Params = Command.Params | ccLocal;
  8898. }
  8899. Command.Name = LoadStr(CUSTOM_COMMAND_AD_HOC_NAME);
  8900. FEditingFocusedAdHocCommand = OnFocused;
  8901. bool LocalSide = IsSideLocalBrowser(FCurrentSide);
  8902. int Options =
  8903. FLAGMASK((!RemoteAllowed || LocalSide), ccoDisableRemote) |
  8904. FLAGMASK(LocalSide, ccoDisableRemoteFiles);
  8905. if (DoCustomCommandDialog(Command, WinConfiguration->CustomCommandList,
  8906. ccmAdHoc, Options, AdHocCustomCommandValidate, NULL))
  8907. {
  8908. FLastCustomCommand = Command;
  8909. UpdateCustomCommandsToolbar();
  8910. ExecuteFileOperation(foCustomCommand, osRemote, OnFocused, false, &Command);
  8911. }
  8912. }
  8913. //---------------------------------------------------------------------------
  8914. void __fastcall TCustomScpExplorerForm::LastCustomCommand(bool OnFocused)
  8915. {
  8916. DebugAssert(!FLastCustomCommand.Command.IsEmpty());
  8917. int State = CustomCommandState(FLastCustomCommand, OnFocused, ccltAll);
  8918. DebugAssert(State > 0);
  8919. if (State <= 0)
  8920. {
  8921. throw Exception(FMTLOAD(CUSTOM_COMMAND_IMPOSSIBLE2, (FLastCustomCommand.Command)));
  8922. }
  8923. ExecuteFileOperation(foCustomCommand, osRemote, OnFocused, false, &FLastCustomCommand);
  8924. }
  8925. //---------------------------------------------------------------------------
  8926. bool __fastcall TCustomScpExplorerForm::GetLastCustomCommand(bool OnFocused,
  8927. TCustomCommandType & Command, int & State)
  8928. {
  8929. bool Result = !FLastCustomCommand.Command.IsEmpty();
  8930. if (Result)
  8931. {
  8932. Command = FLastCustomCommand;
  8933. State = CustomCommandState(FLastCustomCommand, OnFocused, ccltAll);
  8934. }
  8935. return Result;
  8936. }
  8937. //---------------------------------------------------------------------------
  8938. void __fastcall TCustomScpExplorerForm::BeforeAction()
  8939. {
  8940. ResetIncrementalSearch();
  8941. }
  8942. //---------------------------------------------------------------------------
  8943. void __fastcall TCustomScpExplorerForm::PostComponentHide(Byte Component)
  8944. {
  8945. DebugAssert(ComponentVisible[Component]);
  8946. PostMessage(Handle, WM_COMPONENT_HIDE, Component, 0);
  8947. }
  8948. //---------------------------------------------------------------------------
  8949. void __fastcall TCustomScpExplorerForm::QueueSplitterDblClick(TObject * /*Sender*/)
  8950. {
  8951. // when queue panel is resized here directly, the status bar is stretched
  8952. // over whole space the panel occupied
  8953. PostComponentHide(fcQueueView);
  8954. }
  8955. //---------------------------------------------------------------------------
  8956. void __fastcall TCustomScpExplorerForm::QueueFileListSplitterDblClick(TObject *)
  8957. {
  8958. ComponentVisible[fcQueueFileList] = false;
  8959. }
  8960. //---------------------------------------------------------------------------
  8961. void __fastcall TCustomScpExplorerForm::ThemeChanged()
  8962. {
  8963. // We hoped this will refresh scrollbar colors, but it does not have any effect here.
  8964. RefreshColorMode();
  8965. ResetSysDarkTheme();
  8966. ConfigurationChanged();
  8967. ConfigureInterface();
  8968. // Should be called for all controls
  8969. RemoteDirView->Perform(WM_THEMECHANGED, 0, 0);
  8970. }
  8971. //---------------------------------------------------------------------------
  8972. void __fastcall TCustomScpExplorerForm::WMSettingChange(TMessage & Message)
  8973. {
  8974. // Do not handle, when shutting down anyway (maybe also when not setup completely yet?)
  8975. if (!FInvalid &&
  8976. (Message.LParam != 0) &&
  8977. (wcscmp(reinterpret_cast<LPCWCH>(Message.LParam), L"ImmersiveColorSet") == 0))
  8978. {
  8979. ThemeChanged();
  8980. }
  8981. TForm::Dispatch(&Message);
  8982. }
  8983. //---------------------------------------------------------------------------
  8984. void __fastcall TCustomScpExplorerForm::Dispatch(void * Message)
  8985. {
  8986. TMessage * M = static_cast<TMessage*>(Message);
  8987. switch (M->Msg)
  8988. {
  8989. case CM_DIALOGCHAR:
  8990. CMDialogChar(*M);
  8991. break;
  8992. case WM_APPCOMMAND:
  8993. WMAppCommand(*M);
  8994. break;
  8995. case WM_SYSCOMMAND:
  8996. WMSysCommand(*M);
  8997. break;
  8998. case WM_QUERYENDSESSION:
  8999. WMQueryEndSession(*M);
  9000. break;
  9001. case WM_ENDSESSION:
  9002. WMEndSession(*reinterpret_cast<TWMEndSession *>(M));
  9003. break;
  9004. #ifdef _DEBUG
  9005. case WM_WINDOWPOSCHANGED:
  9006. WMWindowPosChanged(*reinterpret_cast<TWMWindowPosMsg *>(M));
  9007. break;
  9008. case CM_CUSTOMSTYLECHANGED:
  9009. TForm::Dispatch(Message);
  9010. break;
  9011. #endif
  9012. case WM_COMPONENT_HIDE:
  9013. {
  9014. Byte Component = static_cast<Byte>(M->WParam);
  9015. // sanity check
  9016. if (ComponentVisible[Component])
  9017. {
  9018. // special treatment
  9019. if (Component == fcQueueView)
  9020. {
  9021. ToggleQueueVisibility();
  9022. DebugAssert(!ComponentVisible[fcQueueView]);
  9023. }
  9024. else
  9025. {
  9026. ComponentVisible[Component] = false;
  9027. }
  9028. }
  9029. }
  9030. break;
  9031. case WM_COPYDATA:
  9032. WMCopyData(*M);
  9033. break;
  9034. case WM_MANAGES_CAPTION:
  9035. // caption managed in TTerminalManager::UpdateAppTitle()
  9036. M->Result = 1;
  9037. break;
  9038. case WM_WANTS_MOUSEWHEEL:
  9039. M->Result = 1;
  9040. break;
  9041. case CM_SHOWINGCHANGED:
  9042. CMShowingChanged(*M);
  9043. break;
  9044. case WM_CLOSE:
  9045. WMClose(*M);
  9046. break;
  9047. case WM_DPICHANGED:
  9048. WMDpiChanged(*M);
  9049. break;
  9050. case CM_DPICHANGED:
  9051. CMDpiChanged(*M);
  9052. break;
  9053. case WM_SETTINGCHANGE:
  9054. WMSettingChange(*M);
  9055. break;
  9056. case CM_DIALOGKEY:
  9057. CMDialogKey(*reinterpret_cast<TWMKeyDown *>(M));
  9058. break;
  9059. default:
  9060. TForm::Dispatch(Message);
  9061. break;
  9062. }
  9063. }
  9064. //---------------------------------------------------------------------------
  9065. void __fastcall TCustomScpExplorerForm::UpdateImages()
  9066. {
  9067. // noop
  9068. }
  9069. //---------------------------------------------------------------------------
  9070. void __fastcall TCustomScpExplorerForm::CMDpiChanged(TMessage & Message)
  9071. {
  9072. TForm::Dispatch(&Message);
  9073. FSessionColors->Clear();
  9074. AddFixedSessionImages();
  9075. RegenerateSessionColorsImageList(FSessionColors, FSessionColorMaskImageIndex);
  9076. UpdateImages();
  9077. }
  9078. //---------------------------------------------------------------------------
  9079. void __fastcall TCustomScpExplorerForm::WMDpiChanged(TMessage & Message)
  9080. {
  9081. TForm::Dispatch(&Message);
  9082. Configuration->Usage->Inc(L"PixelsPerInchChanged");
  9083. UpdatePixelsPerInchMainWindowCounter();
  9084. }
  9085. //---------------------------------------------------------------------------
  9086. void __fastcall TCustomScpExplorerForm::WMClose(TMessage & Message)
  9087. {
  9088. // Cannot close window while we are busy.
  9089. // We cannot test this in FormCloseQuery as that is called also from
  9090. // Close(), which is called by CloseApplicationAction. So we can be busy
  9091. // there already even, when it is legitimate to close the application.
  9092. // Possibly a better place to handle this would be WMSysCommand.
  9093. if (NonVisualDataModule->Busy)
  9094. {
  9095. Message.Result = 1;
  9096. }
  9097. else
  9098. {
  9099. TForm::Dispatch(&Message);
  9100. }
  9101. }
  9102. //---------------------------------------------------------------------------
  9103. void __fastcall TCustomScpExplorerForm::CMShowingChanged(TMessage & Message)
  9104. {
  9105. if (!FShowingChanged)
  9106. {
  9107. AddStartupSequence(L"D");
  9108. FShowingChanged = true;
  9109. }
  9110. // Ignoring implicit local-local browser of Commander, opened from DoShow
  9111. bool NoSession = (ManagedSession == NULL);
  9112. // This calls TCustomScpExplorerForm::DoShow
  9113. TForm::Dispatch(&Message);
  9114. // Now the window is visible (TForm::Dispatch is what usually takes long, like when loading a "local" network directory)
  9115. if (Showing && !FStarted)
  9116. {
  9117. FStarted = true;
  9118. InterfaceStarted();
  9119. }
  9120. if (Showing && NoSession)
  9121. {
  9122. // When we are starting minimized (i.e. from an installer),
  9123. // postpone showing Login dialog until we get restored.
  9124. // Otherwise the Login dialog (and Authentication window) show restored
  9125. // over invisible (minimized) main window.
  9126. if (WindowState == wsMinimized)
  9127. {
  9128. FNeedSession = true;
  9129. }
  9130. else
  9131. {
  9132. // This happens before application ever goes idle, so the toolbars would
  9133. // stay enabled (initial state) until the Login dialog is dismissed.
  9134. UpdateActions();
  9135. NonVisualDataModule->StartBusy();
  9136. try
  9137. {
  9138. // Need to process WM_ACTIVATEAPP before showing the Login dialog,
  9139. // otherwise the dialog does not receive focus.
  9140. // With Commander interface the ProcessMessages is called already
  9141. // by TDriveView, but with Explorer interface, we need to call it explicily
  9142. Application->ProcessMessages();
  9143. // do not reload sessions, they have been loaded just now (optimization)
  9144. NeedSession(true);
  9145. }
  9146. __finally
  9147. {
  9148. NonVisualDataModule->EndBusy();
  9149. }
  9150. }
  9151. }
  9152. }
  9153. //---------------------------------------------------------------------------
  9154. void __fastcall TCustomScpExplorerForm::CenterReconnectToolbar()
  9155. {
  9156. ReconnectToolbar->Left = (ReconnectToolbar->Parent->ClientWidth - ReconnectToolbar->Width) / 2;
  9157. ReconnectToolbar->Top = (ReconnectToolbar->Parent->ClientHeight - ReconnectToolbar->Height) / 2;
  9158. }
  9159. //---------------------------------------------------------------------------
  9160. void __fastcall TCustomScpExplorerForm::FormConstrainedResize(
  9161. TObject * /*Sender*/, int & MinWidth, int & MinHeight, int & MaxWidth,
  9162. int & MaxHeight)
  9163. {
  9164. // workaround for bug in TWinControl.CalcConstraints
  9165. // Check for empty rect (restore from iconic state) is done there only after
  9166. // call to AdjustClientRect, which enlarges the rect (for forms).
  9167. TRect R = GetClientRect();
  9168. // when restoring from iconic state, place no restrictions
  9169. if (IsRectEmpty(R))
  9170. {
  9171. MinWidth = 0;
  9172. MinHeight = 0;
  9173. MaxWidth = 32000;
  9174. MaxHeight = 32000;
  9175. }
  9176. }
  9177. //---------------------------------------------------------------------------
  9178. void __fastcall TCustomScpExplorerForm::GetSpaceAvailable(const UnicodeString Path,
  9179. TSpaceAvailable & ASpaceAvailable, bool & Close)
  9180. {
  9181. // terminal can be already closed (e.g. dropped connection)
  9182. if ((Terminal != NULL) && Terminal->IsCapable[fcCheckingSpaceAvailable])
  9183. {
  9184. Configuration->Usage->Inc(L"SpaceAvailableChecks");
  9185. try
  9186. {
  9187. Terminal->SpaceAvailable(Path, ASpaceAvailable);
  9188. }
  9189. catch(...)
  9190. {
  9191. if (!Terminal->Active)
  9192. {
  9193. Close = true;
  9194. }
  9195. throw;
  9196. }
  9197. }
  9198. }
  9199. //---------------------------------------------------------------------------
  9200. void __fastcall TCustomScpExplorerForm::FileSystemInfo()
  9201. {
  9202. const TSessionInfo & SessionInfo = Terminal->GetSessionInfo();
  9203. const TFileSystemInfo & FileSystemInfo = Terminal->GetFileSystemInfo(true);
  9204. TGetSpaceAvailable OnGetSpaceAvailable = NULL;
  9205. if (Terminal->IsCapable[fcCheckingSpaceAvailable])
  9206. {
  9207. OnGetSpaceAvailable = GetSpaceAvailable;
  9208. }
  9209. DoFileSystemInfoDialog(SessionInfo, FileSystemInfo, Terminal->CurrentDirectory,
  9210. OnGetSpaceAvailable);
  9211. }
  9212. //---------------------------------------------------------------------------
  9213. TSessionData * __fastcall TCustomScpExplorerForm::SessionDataForCode()
  9214. {
  9215. std::unique_ptr<TSessionData> Data(CloneCurrentSessionData());
  9216. Terminal->FillSessionDataForCode(Data.get());
  9217. return Data.release();
  9218. }
  9219. //---------------------------------------------------------------------------
  9220. void __fastcall TCustomScpExplorerForm::GenerateUrl(TStrings * Paths)
  9221. {
  9222. std::unique_ptr<TSessionData> Data(SessionDataForCode());
  9223. DoGenerateUrlDialog(Data.get(), Paths);
  9224. }
  9225. //---------------------------------------------------------------------------
  9226. void __fastcall TCustomScpExplorerForm::SessionGenerateUrl()
  9227. {
  9228. GenerateUrl(NULL);
  9229. }
  9230. //---------------------------------------------------------------------------
  9231. void __fastcall TCustomScpExplorerForm::FileGenerateUrl()
  9232. {
  9233. std::unique_ptr<TStrings> Paths(new TStringList());
  9234. DirView(osCurrent)->CreateFileList(false, true, Paths.get());
  9235. GenerateUrl(Paths.get());
  9236. }
  9237. //---------------------------------------------------------------------------
  9238. void __fastcall TCustomScpExplorerForm::UpdateSessionColor(TColor value)
  9239. {
  9240. FSessionColor = value;
  9241. TColor C = (value != 0 ? value : Vcl::Graphics::clNone);
  9242. TTBXColorItem * ColorItem = dynamic_cast<TTBXColorItem *>(
  9243. static_cast<TObject *>(GetComponent(fcColorMenu)));
  9244. DebugAssert(ColorItem != NULL);
  9245. ColorItem->Color = C;
  9246. NonVisualDataModule->ColorMenuItem->Color = C;
  9247. UpdateControls();
  9248. }
  9249. //---------------------------------------------------------------------------
  9250. void __fastcall TCustomScpExplorerForm::SetSessionColor(TColor value)
  9251. {
  9252. if (value != FSessionColor)
  9253. {
  9254. UpdateSessionColor(value);
  9255. // Is null when called from LastTerminalClosed
  9256. if (Terminal != NULL)
  9257. {
  9258. SessionsPageControl->ActivePage->ImageIndex = AddSessionColor(value);
  9259. }
  9260. }
  9261. }
  9262. //---------------------------------------------------------------------------
  9263. bool __fastcall TCustomScpExplorerForm::CancelNote(bool Force)
  9264. {
  9265. bool Result = FNoteTimer->Enabled;
  9266. if (Result)
  9267. {
  9268. // cannot cancel note too early
  9269. bool NotEarly =
  9270. (Now() - FNoteShown >
  9271. EncodeTimeVerbose(0, 0, (unsigned short)(WinConfiguration->NotificationsStickTime), 0));
  9272. if (Force || NotEarly)
  9273. {
  9274. FNoteTimer->Enabled = false;
  9275. FNote = L"";
  9276. SAFE_DESTROY(FNoteData);
  9277. FOnNoteClick = NULL;
  9278. FNoteHints = FNotes->Text;
  9279. FNoteHints.Delete(FNoteHints.Length() - 1, 2);
  9280. UpdateStatusBar();
  9281. }
  9282. }
  9283. return Result;
  9284. }
  9285. //---------------------------------------------------------------------------
  9286. void __fastcall TCustomScpExplorerForm::NoteTimer(TObject * /*Sender*/)
  9287. {
  9288. DebugAssert(FNoteTimer->Enabled);
  9289. CancelNote(true);
  9290. }
  9291. //---------------------------------------------------------------------------
  9292. void __fastcall TCustomScpExplorerForm::UpdateNoteHints()
  9293. {
  9294. FNoteHints = FNotes->Text.TrimRight();
  9295. }
  9296. //---------------------------------------------------------------------------
  9297. void __fastcall TCustomScpExplorerForm::AddNote(UnicodeString Note, bool UpdateNow)
  9298. {
  9299. int P = Note.Pos(L"\n");
  9300. if (P > 0)
  9301. {
  9302. Note.SetLength(P - 1);
  9303. }
  9304. FNotes->Add(FORMAT(L"[%s] %s",
  9305. (FormatDateTime(Configuration->TimeFormat, Now()), Note)));
  9306. while (FNotes->Count > 10)
  9307. {
  9308. FNotes->Delete(0);
  9309. }
  9310. if (UpdateNow)
  9311. {
  9312. UpdateNoteHints();
  9313. UpdateStatusBar();
  9314. }
  9315. }
  9316. //---------------------------------------------------------------------------
  9317. void __fastcall TCustomScpExplorerForm::PostNote(UnicodeString Note,
  9318. unsigned int Seconds, TNotifyEvent OnNoteClick, TObject * NoteData)
  9319. {
  9320. int P = Note.Pos(L"\n");
  9321. if (P > 0)
  9322. {
  9323. Note.SetLength(P - 1);
  9324. }
  9325. UpdateNoteHints();
  9326. FNote = Note;
  9327. SAFE_DESTROY(FNoteData);
  9328. FNoteData = NoteData;
  9329. FOnNoteClick = OnNoteClick;
  9330. AddNote(Note, false);
  9331. UpdateStatusBar();
  9332. FNoteShown = Now();
  9333. FNoteTimer->Enabled = false;
  9334. if (Seconds == 0)
  9335. {
  9336. Seconds = WinConfiguration->NotificationsTimeout;
  9337. }
  9338. FNoteTimer->Interval = Seconds * MSecsPerSec;
  9339. FNoteTimer->Enabled = true;
  9340. }
  9341. //---------------------------------------------------------------------------
  9342. void __fastcall TCustomScpExplorerForm::ReadDirectoryCancelled()
  9343. {
  9344. PostNote(LoadStr(DIRECTORY_READING_CANCELLED), 0, NULL, NULL);
  9345. }
  9346. //---------------------------------------------------------------------------
  9347. void __fastcall TCustomScpExplorerForm::SynchronizeBrowsingChanged()
  9348. {
  9349. if (NonVisualDataModule->SynchronizeBrowsingAction2->Checked)
  9350. {
  9351. Configuration->Usage->Inc(L"SynchronizeBrowsingEnabled");
  9352. }
  9353. PostNote(FORMAT(LoadStrPart(SYNC_DIR_BROWSE_TOGGLE, 1),
  9354. (LoadStrPart(SYNC_DIR_BROWSE_TOGGLE,
  9355. (NonVisualDataModule->SynchronizeBrowsingAction2->Checked ? 2 : 3)))),
  9356. 0, NULL, NULL);
  9357. }
  9358. //---------------------------------------------------------------------------
  9359. void __fastcall TCustomScpExplorerForm::ToggleShowHiddenFiles()
  9360. {
  9361. bool NewShowHiddenFiles = !WinConfiguration->ShowHiddenFiles;
  9362. UnicodeString Note =
  9363. FORMAT(LoadStrPart(SHOW_HIDDEN_FILES_TOGGLE, 1), (LoadStrPart(SHOW_HIDDEN_FILES_TOGGLE, (NewShowHiddenFiles ? 2 : 3))));
  9364. PostNote(Note, 0, NULL, NULL);
  9365. GetComponent(fcStatusBar)->Repaint(); // toggling ShowHiddenFiles takes time, force repaint beforehand
  9366. WinConfiguration->ShowHiddenFiles = NewShowHiddenFiles;
  9367. }
  9368. //---------------------------------------------------------------------------
  9369. void __fastcall TCustomScpExplorerForm::SetFormatSizeBytes(TFormatBytesStyle Style)
  9370. {
  9371. WinConfiguration->FormatSizeBytes = Style;
  9372. }
  9373. //---------------------------------------------------------------------------
  9374. void __fastcall TCustomScpExplorerForm::ToggleAutoReadDirectoryAfterOp()
  9375. {
  9376. Configuration->AutoReadDirectoryAfterOp = !Configuration->AutoReadDirectoryAfterOp;
  9377. PostNote(FORMAT(LoadStrPart(AUTO_READ_DIRECTORY_TOGGLE, 1),
  9378. (LoadStrPart(AUTO_READ_DIRECTORY_TOGGLE,
  9379. (Configuration->AutoReadDirectoryAfterOp ? 2 : 3)))), 0, NULL, NULL);
  9380. }
  9381. //---------------------------------------------------------------------------
  9382. void __fastcall TCustomScpExplorerForm::StatusBarPanelDblClick(
  9383. TTBXCustomStatusBar * Sender, TTBXStatusPanel * Panel)
  9384. {
  9385. if (Panel->Index == 0)
  9386. {
  9387. if (FOnNoteClick != NULL)
  9388. {
  9389. // prevent the user data from being freed by possible call
  9390. // to CancelNote or PostNote during call to OnNoteClick
  9391. std::unique_ptr<TObject> NoteData(FNoteData);
  9392. TNotifyEvent OnNoteClick = FOnNoteClick;
  9393. FNoteData = NULL;
  9394. // need to cancel the note as we are going to delete its user data
  9395. CancelNote(true);
  9396. OnNoteClick(NoteData.get());
  9397. }
  9398. }
  9399. if (HasActiveTerminal() &&
  9400. (Panel->Index >= Sender->Panels->Count - SessionPanelCount))
  9401. {
  9402. FileSystemInfo();
  9403. }
  9404. }
  9405. //---------------------------------------------------------------------------
  9406. void __fastcall TCustomScpExplorerForm::LockWindow(bool Force)
  9407. {
  9408. // workaround:
  9409. // 1) for unknown reason, disabling window, while minimized,
  9410. // prevents it from restoring, even if it was enabled again meanwhile
  9411. // 2) when disabling the main window, while another has focus
  9412. // minimize is no longer possible
  9413. // ("keep up to date" dialog - though does not seem to be the case with "find file" -
  9414. // so it has probably something to do with the semi-modalness of the "keep up to date" dialog)
  9415. // Shouldn't we use IsApplicationMinimized() here?
  9416. if ((FLockSuspendLevel == 0) && !IsIconic(Application->Handle) && (Force || (Screen->ActiveForm == this)))
  9417. {
  9418. Enabled = false;
  9419. }
  9420. FLockLevel++;
  9421. }
  9422. //---------------------------------------------------------------------------
  9423. void __fastcall TCustomScpExplorerForm::UnlockWindow()
  9424. {
  9425. DebugAssert(FLockLevel > 0);
  9426. FLockLevel--;
  9427. if (FLockLevel == 0)
  9428. {
  9429. DebugAssert(FLockSuspendLevel == 0);
  9430. Enabled = true;
  9431. }
  9432. }
  9433. //---------------------------------------------------------------------------
  9434. void __fastcall TCustomScpExplorerForm::SuspendWindowLock()
  9435. {
  9436. // We need to make sure that window is enabled when the last modal window closes
  9437. // otherwise focus is not restored correctly.
  9438. // So we re-enable the window when modal window opens and
  9439. // disable it back after is closes
  9440. if (FLockLevel > 0)
  9441. {
  9442. // while we have nesting counter, we know that we never be called
  9443. // recursivelly as Application->OnModalBegin is called only
  9444. // for the top-level modal window
  9445. if (DebugAlwaysTrue(FLockSuspendLevel == 0))
  9446. {
  9447. // won't be disabled when conditions in LockWindow() were not satisfied
  9448. FDisabledOnLockSuspend = !Enabled;
  9449. // When minimized to tray (or actually when set to SW_HIDE),
  9450. // setting Enabled makes the window focusable even when there's
  9451. // modal window over it
  9452. if (!FTrayIcon->Visible)
  9453. {
  9454. Enabled = true;
  9455. }
  9456. }
  9457. FLockSuspendLevel++;
  9458. }
  9459. }
  9460. //---------------------------------------------------------------------------
  9461. void __fastcall TCustomScpExplorerForm::ResumeWindowLock()
  9462. {
  9463. if (FLockSuspendLevel > 0)
  9464. {
  9465. DebugAssert(FLockLevel > 0);
  9466. FLockSuspendLevel--;
  9467. // see comment in SuspendWindowLock
  9468. if (DebugAlwaysTrue(FLockSuspendLevel == 0))
  9469. {
  9470. // Note that window can be enabled here, when we were minized to tray when
  9471. // was SuspendWindowLock() called.
  9472. // We should possibly do the same check as in LockWindow(),
  9473. // if it is ever possible that the consitions change between
  9474. // SuspendWindowLock() and ResumeWindowLock()
  9475. Enabled = !FDisabledOnLockSuspend;
  9476. }
  9477. }
  9478. }
  9479. //---------------------------------------------------------------------------
  9480. void __fastcall TCustomScpExplorerForm::UpdateRemotePathComboBox(bool TextOnly)
  9481. {
  9482. TTBXComboBoxItem * RemotePathComboBox =
  9483. reinterpret_cast<TTBXComboBoxItem *>(GetComponent(fcRemotePathComboBox));
  9484. if (!TextOnly)
  9485. {
  9486. TStrings * Items = RemotePathComboBox->Strings;
  9487. Items->BeginUpdate();
  9488. try
  9489. {
  9490. Items->Clear();
  9491. if ((Terminal != NULL) && !RemoteDirView->Path.IsEmpty())
  9492. {
  9493. UnicodeString APath = UnixExcludeTrailingBackslash(RemoteDirView->Path);
  9494. while (!IsUnixRootPath(APath))
  9495. {
  9496. int P = APath.LastDelimiter(L'/');
  9497. DebugAssert(P >= 0);
  9498. Items->Insert(0, APath.SubString(P + 1, APath.Length() - P));
  9499. APath.SetLength(P - 1);
  9500. }
  9501. Items->Insert(0, Customunixdirview_SUnixDefaultRootName);
  9502. }
  9503. }
  9504. __finally
  9505. {
  9506. Items->EndUpdate();
  9507. }
  9508. }
  9509. RemotePathComboBox->ItemIndex = RemotePathComboBox->Strings->Count - 1;
  9510. // Setting ItemIndex to -1 does not reset its text
  9511. if (RemotePathComboBox->Strings->Count == 0)
  9512. {
  9513. RemotePathComboBox->Text = L"";
  9514. }
  9515. }
  9516. //---------------------------------------------------------------------------
  9517. void __fastcall TCustomScpExplorerForm::DoRemotePathComboBoxAdjustImageIndex(
  9518. TTBXComboBoxItem * Sender, const UnicodeString DebugUsedArg(AText), int AIndex, int & ImageIndex)
  9519. {
  9520. if (AIndex < 0)
  9521. {
  9522. AIndex = Sender->ItemIndex;
  9523. }
  9524. ImageIndex = (AIndex < Sender->Strings->Count - 1 ? StdDirIcon : StdDirSelIcon);
  9525. }
  9526. //---------------------------------------------------------------------------
  9527. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxAdjustImageIndex(
  9528. TTBXComboBoxItem * Sender, const UnicodeString AText, int AIndex,
  9529. int & ImageIndex)
  9530. {
  9531. DoRemotePathComboBoxAdjustImageIndex(Sender, AText, AIndex, ImageIndex);
  9532. }
  9533. //---------------------------------------------------------------------------
  9534. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxDrawItem(
  9535. TTBXCustomList * /*Sender*/, TCanvas * /*ACanvas*/, TRect & ARect, int AIndex,
  9536. int /*AHoverIndex*/, bool & /*DrawDefault*/)
  9537. {
  9538. if (!IsLocalBrowserMode())
  9539. {
  9540. ARect.Left += (10 * AIndex);
  9541. }
  9542. }
  9543. //---------------------------------------------------------------------------
  9544. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxMeasureWidth(
  9545. TTBXCustomList * /*Sender*/, TCanvas * /*ACanvas*/, int AIndex, int &AWidth)
  9546. {
  9547. AWidth += (10 * AIndex);
  9548. }
  9549. //---------------------------------------------------------------------------
  9550. void __fastcall TCustomScpExplorerForm::DoRemotePathComboBoxItemClick(TObject * Sender)
  9551. {
  9552. TTBXComboBoxItem * RemotePathComboBox = dynamic_cast<TTBXComboBoxItem*>(Sender);
  9553. UnicodeString APath = UnixExcludeTrailingBackslash(RemoteDirView->Path);
  9554. int Index = RemotePathComboBox->ItemIndex;
  9555. while (Index < RemotePathComboBox->Strings->Count - 1)
  9556. {
  9557. APath = UnixExtractFileDir(APath);
  9558. Index++;
  9559. }
  9560. // VanDyke style paths
  9561. if (APath.IsEmpty())
  9562. {
  9563. DebugAssert(RemotePathComboBox->ItemIndex == 0);
  9564. APath = ROOTDIRECTORY;
  9565. }
  9566. if (RemoteDirView->Path != APath)
  9567. {
  9568. RemoteDirView->Path = APath;
  9569. }
  9570. }
  9571. //---------------------------------------------------------------------------
  9572. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxItemClick(TObject * Sender)
  9573. {
  9574. DoRemotePathComboBoxItemClick(Sender);
  9575. }
  9576. //---------------------------------------------------------------------------
  9577. void __fastcall TCustomScpExplorerForm::DoRemotePathComboBoxCancel(TObject * Sender)
  9578. {
  9579. DebugAssert(Sender == GetComponent(fcRemotePathComboBox));
  9580. DebugUsedParam(Sender);
  9581. UpdateRemotePathComboBox(true);
  9582. }
  9583. //---------------------------------------------------------------------------
  9584. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxCancel(TObject * Sender)
  9585. {
  9586. DoRemotePathComboBoxCancel(Sender);
  9587. }
  9588. //---------------------------------------------------------------------------
  9589. void __fastcall TCustomScpExplorerForm::DirViewEditing(
  9590. TObject * Sender, TListItem * Item, bool & /*AllowEdit*/)
  9591. {
  9592. ResetIncrementalSearch();
  9593. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  9594. DebugAssert(DirView != NULL);
  9595. if (!WinConfiguration->RenameWholeName && !DirView->ItemIsDirectory(Item))
  9596. {
  9597. HWND Edit = ListView_GetEditControl(DirView->Handle);
  9598. // OnEditing is called also from TCustomListView::CanEdit
  9599. if (Edit != NULL)
  9600. {
  9601. EditSelectBaseName(Edit);
  9602. }
  9603. }
  9604. }
  9605. //---------------------------------------------------------------------------
  9606. //---------------------------------------------------------------------------
  9607. TDragDropFilesEx * __fastcall TCustomScpExplorerForm::CreateDragDropFilesEx()
  9608. {
  9609. TDragDropFilesEx * Result = new TDragDropFilesEx(this);
  9610. Result->AutoDetectDnD = false;
  9611. Result->NeedValid = TFileExMustDnDSet() << nvFilename;
  9612. Result->RenderDataOn = rdoEnterAndDropSync;
  9613. Result->TargetEffects = TDropEffectSet() << deCopy << deMove;
  9614. return Result;
  9615. }
  9616. //---------------------------------------------------------------------------
  9617. void __fastcall TCustomScpExplorerForm::UpdateDarkMode()
  9618. {
  9619. if (IsWin10Build(2004))
  9620. {
  9621. FImmersiveDarkMode = WinConfiguration->UseDarkTheme();
  9622. BOOL DarkMode = FImmersiveDarkMode ? TRUE : FALSE;
  9623. const int DWMWA_USE_IMMERSIVE_DARK_MODE = 20;
  9624. DwmSetWindowAttribute(Handle, DWMWA_USE_IMMERSIVE_DARK_MODE, &DarkMode, sizeof(DarkMode));
  9625. }
  9626. }
  9627. //---------------------------------------------------------------------------
  9628. void __fastcall TCustomScpExplorerForm::CreateWnd()
  9629. {
  9630. TForm::CreateWnd();
  9631. // win32-darkmode calls AllowDarkModeForWindow(this, true) here, but it does not seem to have any effect
  9632. UpdateDarkMode();
  9633. if (FSessionsDragDropFilesEx == NULL)
  9634. {
  9635. FSessionsDragDropFilesEx = CreateDragDropFilesEx();
  9636. FSessionsDragDropFilesEx->OnDragOver = SessionsDDDragOver;
  9637. FSessionsDragDropFilesEx->OnProcessDropped = SessionsDDProcessDropped;
  9638. FSessionsDragDropFilesEx->OnDragEnter = SessionsDDDragEnter;
  9639. FSessionsDragDropFilesEx->OnDragLeave = SessionsDDDragLeave;
  9640. }
  9641. if (FQueueDragDropFilesEx == NULL)
  9642. {
  9643. FQueueDragDropFilesEx = CreateDragDropFilesEx();
  9644. // No need to set OnDragOver as we do not have any restrictions
  9645. FQueueDragDropFilesEx->OnProcessDropped = QueueDDProcessDropped;
  9646. FQueueDragDropFilesEx->OnDragEnter = QueueDDDragEnter;
  9647. FQueueDragDropFilesEx->OnDragLeave = QueueDDDragLeave;
  9648. }
  9649. }
  9650. //---------------------------------------------------------------------------
  9651. void __fastcall TCustomScpExplorerForm::DestroyWnd()
  9652. {
  9653. TForm::DestroyWnd();
  9654. FSessionsDragDropFilesEx->DragDropControl = NULL;
  9655. FQueueDragDropFilesEx->DragDropControl = NULL;
  9656. }
  9657. //---------------------------------------------------------------------------
  9658. void __fastcall TCustomScpExplorerForm::FormShow(TObject * /*Sender*/)
  9659. {
  9660. SideEnter(FCurrentSide);
  9661. FEverShown = true;
  9662. }
  9663. //---------------------------------------------------------------------------
  9664. void __fastcall TCustomScpExplorerForm::DoFindFiles(
  9665. TTerminal * ATerminal, UnicodeString Directory, const TFileMasks & FileMask,
  9666. TFileFoundEvent OnFileFound, TFindingFileEvent OnFindingFile)
  9667. {
  9668. if (!NonVisualDataModule->Busy)
  9669. {
  9670. TTerminalManager::Instance()->ActiveSession = DebugNotNull(dynamic_cast<TManagedTerminal *>(ATerminal));
  9671. Configuration->Usage->Inc(L"FileFinds");
  9672. LockWindow(true);
  9673. NonVisualDataModule->StartBusy();
  9674. WinConfiguration->LockedInterface = true;
  9675. try
  9676. {
  9677. Terminal->FilesFind(Directory, FileMask, OnFileFound, OnFindingFile);
  9678. }
  9679. __finally
  9680. {
  9681. WinConfiguration->LockedInterface = false;
  9682. NonVisualDataModule->EndBusy();
  9683. UnlockWindow();
  9684. }
  9685. }
  9686. }
  9687. //---------------------------------------------------------------------------
  9688. void __fastcall TCustomScpExplorerForm::DoFocusRemotePath(TTerminal * ATerminal, const UnicodeString & Path)
  9689. {
  9690. if (!NonVisualDataModule->Busy)
  9691. {
  9692. TTerminalManager::Instance()->ActiveSession = DebugNotNull(dynamic_cast<TManagedTerminal *>(ATerminal));
  9693. SetFocus();
  9694. RemoteDirView->Path = UnixExtractFilePath(Path);
  9695. UnicodeString FileName = UnixExtractFileName(Path);
  9696. TListItem * Item = RemoteDirView->FindFileItem(FileName);
  9697. // If not found, it can because we have loaded directory from cache and the file is new, try again after reload.
  9698. // (tbd: we should not even try this, if we have not loaded the directory from the cache)
  9699. if (Item == NULL)
  9700. {
  9701. ReloadDirectory(osRemote);
  9702. Item = RemoteDirView->FindFileItem(FileName);
  9703. }
  9704. if (Item != NULL)
  9705. {
  9706. RemoteDirView->ItemFocused = Item;
  9707. RemoteDirView->ItemFocused->MakeVisible(false);
  9708. RemoteDirView->SetFocus();
  9709. }
  9710. }
  9711. }
  9712. //---------------------------------------------------------------------------
  9713. bool __fastcall TCustomScpExplorerForm::CanOperateOnFoundFiles(TTerminal * ATerminal)
  9714. {
  9715. bool Result = !NonVisualDataModule->Busy;
  9716. if (Result)
  9717. {
  9718. TTerminalManager::Instance()->ActiveSession = DebugNotNull(dynamic_cast<TManagedTerminal *>(ATerminal));
  9719. }
  9720. return Result;
  9721. }
  9722. //---------------------------------------------------------------------------
  9723. void __fastcall TCustomScpExplorerForm::DoOperationOnFoundFiles(
  9724. TFileOperation Operation, TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  9725. {
  9726. if (CanOperateOnFoundFiles(ATerminal))
  9727. {
  9728. TValueRestorer<TFileOperationFinishedEvent> OnFileOperationFinishedRestorer(FOnFileOperationFinished);
  9729. FOnFileOperationFinished = OnFileOperationFinished;
  9730. ExecuteFileOperation(Operation, osRemote, FileList, false, NULL);
  9731. }
  9732. }
  9733. //---------------------------------------------------------------------------
  9734. void __fastcall TCustomScpExplorerForm::DoDeleteFoundFiles(
  9735. TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  9736. {
  9737. DoOperationOnFoundFiles(foDelete, ATerminal, FileList, OnFileOperationFinished);
  9738. }
  9739. //---------------------------------------------------------------------------
  9740. void __fastcall TCustomScpExplorerForm::DoDownloadFoundFiles(
  9741. TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  9742. {
  9743. DoOperationOnFoundFiles(foCopy, ATerminal, FileList, OnFileOperationFinished);
  9744. }
  9745. //---------------------------------------------------------------------------
  9746. void __fastcall TCustomScpExplorerForm::DoEditFoundFiles(
  9747. TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  9748. {
  9749. if (CanOperateOnFoundFiles(ATerminal))
  9750. {
  9751. for (int Index = 0; Index < FileList->Count; Index++)
  9752. {
  9753. UnicodeString FileName = FileList->Strings[Index];
  9754. TRemoteFile * File = static_cast<TRemoteFile *>(FileList->Objects[Index]);
  9755. ExecuteRemoteFile(FileName, File, efDefaultEditor);
  9756. OnFileOperationFinished(osRemote, FileName, true);
  9757. }
  9758. }
  9759. }
  9760. //---------------------------------------------------------------------------
  9761. void __fastcall TCustomScpExplorerForm::RemoteFindFiles()
  9762. {
  9763. FFileFindTerminal = Terminal;
  9764. ShowFileFindDialog(
  9765. Terminal, RemoteDirView->Path, DoFindFiles, DoFocusRemotePath, DoDeleteFoundFiles,
  9766. DoDownloadFoundFiles, DoEditFoundFiles);
  9767. }
  9768. //---------------------------------------------------------------------------
  9769. void __fastcall TCustomScpExplorerForm::UpdateTaskbarList(ITaskbarList3 * TaskbarList)
  9770. {
  9771. FTaskbarList = TaskbarList;
  9772. }
  9773. //---------------------------------------------------------------------------
  9774. void __fastcall TCustomScpExplorerForm::SessionsPageControlMouseDown(
  9775. TObject * /*Sender*/, TMouseButton Button, TShiftState /*Shift*/, int X, int Y)
  9776. {
  9777. int Index = SessionsPageControl->IndexOfTabAt(X, Y);
  9778. if (Index >= 0)
  9779. {
  9780. if (Button == mbLeft)
  9781. {
  9782. // WORKAROUND: "Mouse down" is raised only after tab is switched.
  9783. // If switching tab (switching session) takes long enough for user
  9784. // to actually release the button, "mouse down" is still raised,
  9785. // but we do not get "mouse up" event, so dragging is not cancelled,
  9786. // prevent that by not beginning dragging in the first place.
  9787. int Key = ((GetSystemMetrics(SM_SWAPBUTTON) == 0) ? VK_LBUTTON : VK_RBUTTON);
  9788. if (IsKeyPressed(Key))
  9789. {
  9790. // when user clicks the "+", we get mouse down only after the session
  9791. // is closed, when new session tab is already on X:Y, so dragging
  9792. // starts, prevent that
  9793. if (MilliSecondsBetween(Now(), FSessionsPageControlNewTabTime) > 500)
  9794. {
  9795. TTerminal * Terminal = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  9796. if (Terminal != NULL)
  9797. {
  9798. SessionsPageControl->BeginDrag(false);
  9799. }
  9800. }
  9801. }
  9802. }
  9803. else if (Button == mbMiddle)
  9804. {
  9805. // ignore middle-click for "New session tab" and the last local-local tab
  9806. TManagedTerminal * Session = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  9807. if ((Session != NULL) && CanCloseSession(Session))
  9808. {
  9809. CloseSessionTab(Index);
  9810. }
  9811. }
  9812. }
  9813. }
  9814. //---------------------------------------------------------------------------
  9815. void __fastcall TCustomScpExplorerForm::SessionsPageControlDragDrop(
  9816. TObject * /*Sender*/, TObject * /*Source*/, int X, int Y)
  9817. {
  9818. int Index = SessionsPageControl->IndexOfTabAt(X, Y);
  9819. // do not allow dropping on the "+" tab
  9820. TTerminal * TargetTerminal = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  9821. if ((TargetTerminal != NULL) &&
  9822. (SessionsPageControl->ActivePage->PageIndex != Index))
  9823. {
  9824. Configuration->Usage->Inc(L"SessionTabMoves");
  9825. // this is almost redundant as we would recreate tabs in SessionListChanged,
  9826. // but we want to actually prevent that to avoid flicker
  9827. SessionsPageControl->ActivePage->PageIndex = Index;
  9828. TTerminal * Terminal = GetSessionTabSession(SessionsPageControl->ActivePage);
  9829. TTerminalManager::Instance()->Move(Terminal, TargetTerminal);
  9830. }
  9831. }
  9832. //---------------------------------------------------------------------------
  9833. void __fastcall TCustomScpExplorerForm::SessionsPageControlDragOver(
  9834. TObject * Sender, TObject * Source, int X, int Y,
  9835. TDragState /*State*/, bool & Accept)
  9836. {
  9837. Accept = (Sender == Source);
  9838. if (Accept)
  9839. {
  9840. int Index = SessionsPageControl->IndexOfTabAt(X, Y);
  9841. TTerminal * Terminal = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  9842. // do not allow dragging to the "+" tab
  9843. Accept = (Terminal != NULL);
  9844. }
  9845. }
  9846. //---------------------------------------------------------------------------
  9847. void __fastcall TCustomScpExplorerForm::SessionsDDDragOver(
  9848. int DebugUsedArg(KeyState), const TPoint & Point, int & Effect, int DebugUsedArg(PreferredEffect))
  9849. {
  9850. int Index = SessionsPageControl->IndexOfTabAt(Point.X, Point.Y);
  9851. if (Index < 0)
  9852. {
  9853. Effect = DROPEFFECT_NONE;
  9854. }
  9855. else
  9856. {
  9857. TManagedTerminal * TargetSession = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  9858. // do not allow dropping on the "+" tab or on disconnected or local-local tabs
  9859. if (!IsActiveTerminal(TargetSession) || DebugAlwaysFalse(TargetSession->LocalBrowser))
  9860. {
  9861. Effect = DROPEFFECT_NONE;
  9862. }
  9863. else if ((TargetSession != Terminal) && (Effect == DROPEFFECT_MOVE))
  9864. {
  9865. Effect = DROPEFFECT_COPY;
  9866. }
  9867. }
  9868. }
  9869. //---------------------------------------------------------------------------
  9870. void __fastcall TCustomScpExplorerForm::SessionsDDProcessDropped(
  9871. TObject * /*Sender*/, int /*KeyState*/, const TPoint & Point, int Effect)
  9872. {
  9873. int Index = SessionsPageControl->IndexOfTabAt(Point.X, Point.Y);
  9874. // do not allow dropping on local-local, disconnected and the "+" tab
  9875. // (though we do not seem to get here in that case anyway, contrary to RemoteFileControlDDTargetDrop)
  9876. TManagedTerminal * TargetTerminal = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  9877. if (DebugAlwaysTrue(IsActiveTerminal(TargetTerminal) && !TargetTerminal->LocalBrowser))
  9878. {
  9879. DebugAssert(!IsFileControl(DropSourceControl, osRemote));
  9880. if (!IsFileControl(DropSourceControl, osRemote))
  9881. {
  9882. TTerminalManager::Instance()->ActiveSession = TargetTerminal;
  9883. RemoteFileControlDragDropFileOperation(SessionsPageControl, Effect,
  9884. // Why don't we use Terminal->CurrentDirectory directly?
  9885. TTerminalManager::Instance()->ActiveTerminal->CurrentDirectory,
  9886. // do not force queue, drag drop
  9887. false, true);
  9888. }
  9889. }
  9890. }
  9891. //---------------------------------------------------------------------------
  9892. void __fastcall TCustomScpExplorerForm::QueueDDProcessDropped(
  9893. TObject * /*Sender*/, int /*KeyState*/, const TPoint & /*Point*/, int Effect)
  9894. {
  9895. // Downloads are handled in RemoteFileControlDDEnd
  9896. if (!IsFileControl(DropSourceControl, osRemote))
  9897. {
  9898. RemoteFileControlDragDropFileOperation(QueueView3, Effect,
  9899. Terminal->CurrentDirectory,
  9900. // force queue, drag drop
  9901. true, true);
  9902. }
  9903. }
  9904. //---------------------------------------------------------------------------
  9905. void __fastcall TCustomScpExplorerForm::FormClose(TObject * /*Sender*/, TCloseAction & /*Action*/)
  9906. {
  9907. FShowing = false;
  9908. // Do not save empty workspace
  9909. if (WinConfiguration->AutoSaveWorkspace && (ManagedSession != NULL))
  9910. {
  9911. std::unique_ptr<TObjectList> DataList(DoCollectWorkspace());
  9912. UnicodeString Name = WorkspaceName();
  9913. DoSaveWorkspace(
  9914. Name, DataList.get(),
  9915. !Configuration->DisablePasswordStoring &&
  9916. WinConfiguration->AutoSaveWorkspacePasswords, false);
  9917. WinConfiguration->LastStoredSession = Name;
  9918. }
  9919. }
  9920. //---------------------------------------------------------------------------
  9921. void __fastcall TCustomScpExplorerForm::RemoteDirViewRead(TObject * /*Sender*/)
  9922. {
  9923. TManagedTerminal * ManagedTerminal =
  9924. dynamic_cast<TManagedTerminal *>(RemoteDirView->Terminal);
  9925. if (ManagedTerminal != NULL)
  9926. {
  9927. ManagedTerminal->DirectoryLoaded = Now();
  9928. }
  9929. }
  9930. //---------------------------------------------------------------------------
  9931. void __fastcall TCustomScpExplorerForm::DirViewSelectItem(TObject * Sender,
  9932. TListItem * /*Item*/, bool /*Selected*/)
  9933. {
  9934. TCustomDirView * DirView = reinterpret_cast<TCustomDirView *>(Sender);
  9935. switch (DirView->LastSelectMethod)
  9936. {
  9937. case smKeyboard:
  9938. Configuration->Usage->Inc(L"KeyboardSelections");
  9939. break;
  9940. case smMouse:
  9941. Configuration->Usage->Inc(L"MouseSelections");
  9942. break;
  9943. }
  9944. }
  9945. //---------------------------------------------------------------------------
  9946. int __fastcall TCustomScpExplorerForm::AddSessionColor(TColor Color)
  9947. {
  9948. if (Color != 0)
  9949. {
  9950. return GetSessionColorImage(FSessionColors, Color, FSessionColorMaskImageIndex);
  9951. }
  9952. else
  9953. {
  9954. return FSessionTabImageIndex;
  9955. }
  9956. }
  9957. //---------------------------------------------------------------------------
  9958. int __fastcall TCustomScpExplorerForm::AddFixedSessionImage(int GlyphsSourceIndex)
  9959. {
  9960. TPngImageCollectionItem * Item =
  9961. GlyphsModule->ExplorerImages->PngImages->Items[GlyphsSourceIndex];
  9962. FSessionColors->AddPng(Item->PngImage);
  9963. return FSessionColors->Count - 1;
  9964. }
  9965. //---------------------------------------------------------------------------
  9966. void __fastcall TCustomScpExplorerForm::AddFixedSessionImages()
  9967. {
  9968. FSessionColors->SetSize(GlyphsModule->ExplorerImages->Width, GlyphsModule->ExplorerImages->Height);
  9969. FNewRemoteTabTabImageIndex = AddFixedSessionImage(NonVisualDataModule->NewRemoteTabAction->ImageIndex);
  9970. FNewLocalTabTabImageIndex = AddFixedSessionImage(NonVisualDataModule->NewLocalTabAction->ImageIndex);
  9971. FSessionTabImageIndex = AddFixedSessionImage(SiteImageIndex);
  9972. FSessionColorMaskImageIndex = AddFixedSessionImage(SiteColorMaskImageIndex);
  9973. FLocalBrowserTabImageIndex = AddFixedSessionImage(LocalBrowserImageIndex);
  9974. }
  9975. //---------------------------------------------------------------------------
  9976. int TCustomScpExplorerForm::GetNewTabActionImageIndex()
  9977. {
  9978. return NonVisualDataModule->NewRemoteTabAction->ImageIndex;
  9979. }
  9980. //---------------------------------------------------------------------------
  9981. int TCustomScpExplorerForm::GetNewTabTabImageIndex(TOperationSide Side)
  9982. {
  9983. switch (Side)
  9984. {
  9985. case osLocal:
  9986. return FNewLocalTabTabImageIndex;
  9987. case osRemote:
  9988. case osCurrent:
  9989. return FNewRemoteTabTabImageIndex;
  9990. default:
  9991. DebugFail();
  9992. return -1;
  9993. }
  9994. }
  9995. //---------------------------------------------------------------------------
  9996. void __fastcall TCustomScpExplorerForm::CollectItemsWithTextDisplayMode(TWinControl * Control)
  9997. {
  9998. for (int Index = 0; Index < Control->ControlCount; Index++)
  9999. {
  10000. TControl * AControl = Control->Controls[Index];
  10001. TWinControl * WinControl = dynamic_cast<TWinControl *>(AControl);
  10002. if (WinControl != NULL)
  10003. {
  10004. CollectItemsWithTextDisplayMode(WinControl);
  10005. }
  10006. TTBCustomToolbar * Toolbar = dynamic_cast<TTBCustomToolbar *>(AControl);
  10007. if (Toolbar != NULL)
  10008. {
  10009. // we care for top-level items only
  10010. TTBCustomItem * Items = Toolbar->Items;
  10011. for (int ItemIndex = 0; ItemIndex < Items->Count; ItemIndex++)
  10012. {
  10013. TTBCustomItem * Item = Items->Items[ItemIndex];
  10014. if (((Item->DisplayMode == nbdmImageAndText) ||
  10015. (dynamic_cast<TTBXLabelItem *>(Item) != NULL)) &&
  10016. EligibleForImageDisplayMode(Item))
  10017. {
  10018. FItemsWithTextDisplayMode.insert(Item);
  10019. }
  10020. }
  10021. }
  10022. }
  10023. }
  10024. //---------------------------------------------------------------------------
  10025. bool __fastcall TCustomScpExplorerForm::EligibleForImageDisplayMode(TTBCustomItem * /*Item*/)
  10026. {
  10027. return true;
  10028. }
  10029. //---------------------------------------------------------------------------
  10030. bool __fastcall TCustomScpExplorerForm::UpdateToolbarDisplayMode()
  10031. {
  10032. bool SelectiveToolbarText = WinConfiguration->SelectiveToolbarText;
  10033. TTBItemDisplayMode DisplayMode = (SelectiveToolbarText ? nbdmImageAndText : nbdmDefault);
  10034. typedef std::set<TTBCustomItem *> TItemsWithTextDisplayMode;
  10035. TItemsWithTextDisplayMode::iterator i = FItemsWithTextDisplayMode.begin();
  10036. bool Result = true;
  10037. while (Result && (i != FItemsWithTextDisplayMode.end()))
  10038. {
  10039. TTBCustomItem * Item = *i;
  10040. TTBXLabelItem * Label = dynamic_cast<TTBXLabelItem *>(Item);
  10041. if (Label != NULL)
  10042. {
  10043. // optimization
  10044. if (Label->Visible == SelectiveToolbarText)
  10045. {
  10046. Result = false;
  10047. }
  10048. else
  10049. {
  10050. Label->Visible = SelectiveToolbarText;
  10051. }
  10052. }
  10053. else
  10054. {
  10055. // optimization
  10056. if (Item->DisplayMode == DisplayMode)
  10057. {
  10058. Result = false;
  10059. }
  10060. else
  10061. {
  10062. Item->DisplayMode = DisplayMode;
  10063. }
  10064. }
  10065. ++i;
  10066. }
  10067. UpdateNewTabTab();
  10068. return Result;
  10069. }
  10070. //---------------------------------------------------------------------------
  10071. void __fastcall TCustomScpExplorerForm::DisplaySystemContextMenu()
  10072. {
  10073. DebugFail();
  10074. }
  10075. //---------------------------------------------------------------------------
  10076. bool __fastcall TCustomScpExplorerForm::IsBusy()
  10077. {
  10078. // Among other, a lock level is non zero, while directory is being loaded,
  10079. // even when it happens as a result of dirview navigation,
  10080. // i.e. when TNonVisualDataModule is NOT FBusy.
  10081. // That's why the TNonVisualDataModule::GetBusy calls this method.
  10082. // Among other this prevents a panel auto update to occur while
  10083. // directory is changing.
  10084. return (FLockLevel > 0) || DirView(osCurrent)->IsEditing();
  10085. }
  10086. //---------------------------------------------------------------------------
  10087. Boolean __fastcall TCustomScpExplorerForm::AllowedAction(TAction * Action, TActionAllowed Allowed)
  10088. {
  10089. // While the window is disabled, we still seem to process menu shortcuts at least,
  10090. // so stop it at least here.
  10091. // See also TCustomScpExplorerForm::RemoteDirViewBusy
  10092. bool Result;
  10093. if (Allowed == aaUpdate)
  10094. {
  10095. Result = true;
  10096. }
  10097. else
  10098. {
  10099. Result = !NonVisualDataModule->Busy;
  10100. if (Result && (Allowed == aaShortCut))
  10101. {
  10102. // Otherwise F5 for local-local will be swallowed by disabled local<>remote actions in TCustomScpExplorerForm::KeyDown
  10103. Action->Update();
  10104. Result = Action->Enabled;
  10105. }
  10106. }
  10107. return Result;
  10108. }
  10109. //---------------------------------------------------------------------------
  10110. void __fastcall TCustomScpExplorerForm::EditMenuItemPopup(TTBCustomItem * Sender, bool FromLink)
  10111. {
  10112. NonVisualDataModule->EditMenuItemPopup(Sender, FromLink);
  10113. }
  10114. //---------------------------------------------------------------------------
  10115. void __fastcall TCustomScpExplorerForm::DirViewBusy(TObject * /*Sender*/, int Busy, bool & State)
  10116. {
  10117. // This is somewhat redundant to LockWindow() call from
  10118. // TTerminalManager::TerminalReadDirectoryProgress.
  10119. // But disabling window is known not to block keyboard shorcuts
  10120. // (see TCustomScpExplorerForm::AllowedAction), this hopefully works.
  10121. if (Busy > 0)
  10122. {
  10123. if (NonVisualDataModule->Busy)
  10124. {
  10125. State = false;
  10126. }
  10127. else
  10128. {
  10129. NonVisualDataModule->StartBusy();
  10130. // Will be countered for TCustomDirView.DoExecute in DoDirViewExecFile
  10131. LockWindow();
  10132. }
  10133. }
  10134. else if (Busy < 0)
  10135. {
  10136. UnlockWindow();
  10137. NonVisualDataModule->EndBusy();
  10138. }
  10139. else
  10140. {
  10141. State = NonVisualDataModule->Busy;
  10142. }
  10143. }
  10144. //---------------------------------------------------------------------------
  10145. void __fastcall TCustomScpExplorerForm::SessionsPageControlContextPopup(TObject * /*Sender*/, TPoint & MousePos, bool & Handled)
  10146. {
  10147. int Index = SessionsPageControl->IndexOfTabAt(MousePos.X, MousePos.Y);
  10148. if (Index >= 0)
  10149. {
  10150. TPopupMenu * PopupMenu = NULL;
  10151. TManagedTerminal * Session = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  10152. if (Session != NULL)
  10153. {
  10154. SessionsPageControl->ActivePageIndex = Index;
  10155. if (DebugAlwaysTrue(SessionTabSwitched()))
  10156. {
  10157. // copied from TControl.WMContextMenu
  10158. SendCancelMode(SessionsPageControl);
  10159. // explicit popup instead of using PopupMenu property
  10160. // to avoid menu to popup somewhere within SessionTabSwitched above,
  10161. // while connecting yet not-connected session and hence
  10162. // allowing an access to commands over not-completely connected session
  10163. PopupMenu = Session->LocalBrowser ? NonVisualDataModule->LocalBrowserPopup : NonVisualDataModule->SessionsPopup;
  10164. }
  10165. }
  10166. else
  10167. {
  10168. if (SupportsLocalBrowser())
  10169. {
  10170. PopupMenu = NonVisualDataModule->NewTabPopup;
  10171. }
  10172. }
  10173. if (PopupMenu != NULL)
  10174. {
  10175. TPoint Point = SessionsPageControl->ClientToScreen(MousePos);
  10176. PopupMenu->PopupComponent = SessionsPageControl;
  10177. PopupMenu->Popup(Point.x, Point.y);
  10178. }
  10179. }
  10180. Handled = true;
  10181. }
  10182. //---------------------------------------------------------------------------
  10183. bool __fastcall TCustomScpExplorerForm::CanChangePassword()
  10184. {
  10185. return HasActiveTerminal() && Terminal->IsCapable[fcChangePassword];
  10186. }
  10187. //---------------------------------------------------------------------------
  10188. void __fastcall TCustomScpExplorerForm::ChangePassword()
  10189. {
  10190. Configuration->Usage->Inc(L"PasswordChanges");
  10191. std::unique_ptr<TSessionData> ChangePasswordData(Terminal->SessionData->Clone());
  10192. ChangePasswordData->DisableAuthentationsExceptPassword();
  10193. ChangePasswordData->ChangePassword = true;
  10194. std::unique_ptr<TTerminal> ChangePasswordSession(
  10195. Terminal->CreateSecondarySession(L"ChangePassword", ChangePasswordData.get()));
  10196. ChangePasswordSession->AutoReadDirectory = false;
  10197. if (TTerminalManager::Instance()->ConnectTerminal(ChangePasswordSession.get()))
  10198. {
  10199. MessageDialog(MainInstructions(LoadStr(PASSWORD_CHANGED)), qtInformation, qaOK, HELP_CHANGE_PASSWORD);
  10200. }
  10201. else
  10202. {
  10203. Configuration->Usage->Inc(L"PasswordChangesFailed");
  10204. }
  10205. }
  10206. //---------------------------------------------------------------------------
  10207. bool __fastcall TCustomScpExplorerForm::CanPrivateKeyUpload()
  10208. {
  10209. // No nice way to assert SSH2
  10210. return HasActiveTerminal() && (Terminal->FSProtocol == cfsSFTP);
  10211. }
  10212. //---------------------------------------------------------------------------
  10213. void __fastcall TCustomScpExplorerForm::PrivateKeyUpload()
  10214. {
  10215. TOperationVisualizer OperationVisualizer;
  10216. UnicodeString FileName = Terminal->SessionData->PublicKeyFile;
  10217. TTerminalManager::Instance()->UploadPublicKey(Terminal, NULL, FileName);
  10218. }
  10219. //---------------------------------------------------------------------------
  10220. void __fastcall TCustomScpExplorerForm::ChangeScale(int M, int D)
  10221. {
  10222. TForm::ChangeScale(M, D);
  10223. int APixelsPerInch = GetControlPixelsPerInch(this);
  10224. HANDLE ResourceModule = GUIConfiguration->ChangeToDefaultResourceModule();
  10225. try
  10226. {
  10227. GlyphsModule->SetPixelsPerInch(APixelsPerInch);
  10228. }
  10229. __finally
  10230. {
  10231. GUIConfiguration->ChangeResourceModule(ResourceModule);
  10232. }
  10233. }
  10234. //---------------------------------------------------------------------------
  10235. void __fastcall TCustomScpExplorerForm::DockContextPopup(TObject * Sender, TPoint & MousePos, bool & /*Handled*/)
  10236. {
  10237. NonVisualDataModule->ControlContextPopup(Sender, MousePos);
  10238. }
  10239. //---------------------------------------------------------------------
  10240. class TPublicControl : public TControl
  10241. {
  10242. friend class TCustomScpExplorerForm;
  10243. };
  10244. //---------------------------------------------------------------------------
  10245. void __fastcall TCustomScpExplorerForm::CopyPopup(TControl * DestControl, TControl * SourceControl)
  10246. {
  10247. static_cast<TPublicControl *>(DestControl)->PopupMenu = static_cast<TPublicControl *>(SourceControl)->PopupMenu;
  10248. static_cast<TPublicControl *>(DestControl)->OnContextPopup = static_cast<TPublicControl *>(SourceControl)->OnContextPopup;
  10249. }
  10250. //---------------------------------------------------------------------------
  10251. void __fastcall TCustomScpExplorerForm::DoBookmarkClick(TOperationSide Side, TObject * Sender)
  10252. {
  10253. TBookmark * Bookmark = DebugNotNull(reinterpret_cast<TBookmark *>(dynamic_cast<TTBCustomItem *>(Sender)->Tag));
  10254. OpenBookmark(Side, Bookmark);
  10255. }
  10256. //---------------------------------------------------------------------------
  10257. void __fastcall TCustomScpExplorerForm::LocalBookmarkClick(TObject * Sender)
  10258. {
  10259. DoBookmarkClick(osLocal, Sender);
  10260. }
  10261. //---------------------------------------------------------------------------
  10262. void __fastcall TCustomScpExplorerForm::RemoteBookmarkClick(TObject * Sender)
  10263. {
  10264. DoBookmarkClick(osRemote, Sender);
  10265. }
  10266. //---------------------------------------------------------------------------
  10267. void __fastcall TCustomScpExplorerForm::CreateOpenDirMenuList(
  10268. TTBCustomItem * Menu, TOperationSide Side, TBookmarkList * BookmarkList)
  10269. {
  10270. if (BookmarkList != NULL)
  10271. {
  10272. TNotifyEvent OnBookmarkClick = (Side == osLocal) ? &LocalBookmarkClick : &RemoteBookmarkClick;
  10273. if (IsLocalBrowserMode())
  10274. {
  10275. Side = osLocal;
  10276. }
  10277. if (!WinConfiguration->UseLocationProfiles)
  10278. {
  10279. std::unique_ptr<TStringList> Directories(new TStringList());
  10280. Directories->CaseSensitive = (Side == osRemote);
  10281. for (int Index = 0; Index < BookmarkList->Count; Index++)
  10282. {
  10283. TBookmark * Bookmark = BookmarkList->Bookmarks[Index];
  10284. UnicodeString Directory = Bookmark->GetSideDirectory(Side);
  10285. if (!Directory.IsEmpty() && (Directories->IndexOf(Directory) < 0))
  10286. {
  10287. std::unique_ptr<TTBCustomItem> Item(new TTBXItem(Owner));
  10288. Item->Caption = EscapeHotkey(Directory);
  10289. Item->ShortCut = Bookmark->ShortCut;
  10290. Item->OnClick = OnBookmarkClick;
  10291. Item->Tag = reinterpret_cast<int>(Bookmark);
  10292. Directories->Add(Directory);
  10293. Menu->Add(Item.release());
  10294. }
  10295. }
  10296. }
  10297. else
  10298. {
  10299. std::unique_ptr<TStrings> Folders(CreateSortedStringList());
  10300. for (int Index = 0; Index < BookmarkList->Count; Index++)
  10301. {
  10302. TBookmark * Bookmark = BookmarkList->Bookmarks[Index];
  10303. if (!Bookmark->Node.IsEmpty())
  10304. {
  10305. Folders->Add(Bookmark->Node);
  10306. }
  10307. }
  10308. for (int Index = 0; Index < Folders->Count; Index++)
  10309. {
  10310. std::unique_ptr<TTBCustomItem> Item(new TTBXSubmenuItem(Owner));
  10311. Item->Caption = Folders->Strings[Index];
  10312. Item->ImageIndex = NonVisualDataModule->RemoteChangePathAction2->ImageIndex;
  10313. Folders->Objects[Index] = Item.get();
  10314. Menu->Add(Item.release());
  10315. }
  10316. for (int Index = 0; Index < BookmarkList->Count; Index++)
  10317. {
  10318. TBookmark * Bookmark = BookmarkList->Bookmarks[Index];
  10319. TTBCustomItem * Parent;
  10320. if (!Bookmark->Node.IsEmpty())
  10321. {
  10322. DebugAssert(Folders->IndexOf(Bookmark->Node) >= 0);
  10323. Parent = dynamic_cast<TTBCustomItem *>(Folders->Objects[Folders->IndexOf(Bookmark->Node)]);
  10324. }
  10325. else
  10326. {
  10327. Parent = Menu;
  10328. }
  10329. std::unique_ptr<TTBCustomItem> Item(new TTBXItem(Owner));
  10330. Item->Caption = Bookmark->Name;
  10331. Item->ShortCut = Bookmark->ShortCut;
  10332. Item->OnClick = OnBookmarkClick;
  10333. Item->Tag = reinterpret_cast<int>(Bookmark);
  10334. if (((Bookmark->Name == Bookmark->Local) || (Bookmark->Name == Bookmark->Remote)) &&
  10335. (Bookmark->Local.IsEmpty() || Bookmark->Remote.IsEmpty()))
  10336. {
  10337. // No hint for location profiles that are actually mere "bookmarks"
  10338. }
  10339. else
  10340. {
  10341. UnicodeString Hint = FORMAT(LoadStrPart(LOCATION_PROFILE_HINT, 1), (Bookmark->Name));
  10342. UnicodeString LongHint;
  10343. if (!Bookmark->Local.IsEmpty())
  10344. {
  10345. AddToList(LongHint, FORMAT(LoadStrPart(LOCATION_PROFILE_HINT, 2), (Bookmark->Local)), L"\n");
  10346. }
  10347. if (!Bookmark->Remote.IsEmpty())
  10348. {
  10349. AddToList(LongHint, FORMAT(LoadStrPart(LOCATION_PROFILE_HINT, 3), (Bookmark->Remote)), L"\n");
  10350. }
  10351. AddToList(Hint, LongHint, L"|");
  10352. Item->Hint = Hint;
  10353. }
  10354. Parent->Add(Item.release());
  10355. }
  10356. }
  10357. AddMenuSeparator(Menu);
  10358. }
  10359. }
  10360. //---------------------------------------------------------------------------
  10361. void __fastcall TCustomScpExplorerForm::CreateOpenDirMenu(TTBCustomItem * Menu, TOperationSide Side)
  10362. {
  10363. Menu->Clear();
  10364. std::unique_ptr<TTBCustomItem> Item;
  10365. Item.reset(new TTBXItem(Owner));
  10366. Item->Action = (Side == osLocal) ? NonVisualDataModule->LocalOpenDirAction : NonVisualDataModule->RemoteOpenDirAction;
  10367. Item->Options = Item->Options << tboDefault;
  10368. Menu->Add(Item.release());
  10369. AddMenuSeparator(Menu);
  10370. if (Terminal != NULL)
  10371. {
  10372. CreateOpenDirMenuList(Menu, Side, WinConfiguration->Bookmarks[Terminal->SessionData->SessionKey]);
  10373. }
  10374. CreateOpenDirMenuList(Menu, Side, WinConfiguration->SharedBookmarks);
  10375. Item.reset(new TTBXItem(Owner));
  10376. Item->Action = (Side == osLocal) ? NonVisualDataModule->LocalAddBookmarkAction2 : NonVisualDataModule->RemoteAddBookmarkAction2;
  10377. Menu->Add(Item.release());
  10378. }
  10379. //---------------------------------------------------------------------------
  10380. bool __fastcall TCustomScpExplorerForm::TryOpenDirectory(TOperationSide Side, const UnicodeString & Path)
  10381. {
  10382. bool Result = true;
  10383. bool Remote = !IsSideLocalBrowser(Side);
  10384. try
  10385. {
  10386. UnicodeString APath = Path;
  10387. if (Remote)
  10388. {
  10389. Terminal->ExceptionOnFail = true;
  10390. }
  10391. else
  10392. {
  10393. APath = ExpandEnvironmentVariables(Path);
  10394. }
  10395. try
  10396. {
  10397. DirView(Side)->Path = APath;
  10398. }
  10399. __finally
  10400. {
  10401. if (Remote)
  10402. {
  10403. Terminal->ExceptionOnFail = false;
  10404. }
  10405. }
  10406. }
  10407. catch (Exception & E)
  10408. {
  10409. if (!Remote || Terminal->Active)
  10410. {
  10411. ShowExtendedExceptionEx(Terminal, &E);
  10412. Result = false;
  10413. }
  10414. else
  10415. {
  10416. throw;
  10417. }
  10418. }
  10419. return Result;
  10420. }
  10421. //---------------------------------------------------------------------------
  10422. void __fastcall TCustomScpExplorerForm::ReloadDirectory(TOperationSide Side)
  10423. {
  10424. // Make sure there some feedback even when loading tiny local folders
  10425. TInstantOperationVisualizer Visualizer;
  10426. DirView(Side)->ReloadDirectory();
  10427. }
  10428. //---------------------------------------------------------------------------
  10429. void __fastcall TCustomScpExplorerForm::CloseSessionTab(int Index)
  10430. {
  10431. if (Index == SessionsPageControl->TabIndex)
  10432. {
  10433. CloseTab();
  10434. }
  10435. else
  10436. {
  10437. TTabSheet * TabSheet = SessionsPageControl->Pages[Index];
  10438. TTerminal * Terminal = GetSessionTabSession(TabSheet);
  10439. TTerminalManager * Manager = TTerminalManager::Instance();
  10440. if (!Terminal->Active)
  10441. {
  10442. Manager->FreeTerminal(Terminal);
  10443. }
  10444. else
  10445. {
  10446. TTerminalQueue * Queue = Manager->FindQueueForTerminal(Terminal);
  10447. if (CanCloseQueue(Queue))
  10448. {
  10449. Manager->FreeTerminal(Terminal);
  10450. }
  10451. }
  10452. }
  10453. }
  10454. //---------------------------------------------------------------------------
  10455. void __fastcall TCustomScpExplorerForm::SessionsPageControlTabButtonClick(TPageControl *, int Index)
  10456. {
  10457. TManagedTerminal * Session = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  10458. if (Session == NULL)
  10459. {
  10460. TRect ButtonRect = SessionsPageControl->TabButtonRect(Index);
  10461. ButtonRect = TRect(SessionsPageControl->ClientToScreen(ButtonRect.TopLeft()), SessionsPageControl->ClientToScreen(ButtonRect.BottomRight()));
  10462. //TPoint P = TPoint(ButtonRect.Left, ButtonRect.Bottom);
  10463. //P = SessionsPageControl->ClientToScreen(P);
  10464. MenuPopup(NonVisualDataModule->NewTabPopup, ButtonRect, SessionsPageControl);
  10465. }
  10466. else
  10467. {
  10468. CloseSessionTab(Index);
  10469. }
  10470. }
  10471. //---------------------------------------------------------------------------
  10472. void __fastcall TCustomScpExplorerForm::CopyFilesToClipboard(TOperationSide Side, bool OnFocused)
  10473. {
  10474. if (DebugAlwaysTrue(!IsSideLocalBrowser(Side)))
  10475. {
  10476. TInstantOperationVisualizer Visualizer;
  10477. // To trigger ClipboardDataObjectRelease (in case the clipboard already contains our data)
  10478. // before we create a new data object
  10479. ClipboardClear();
  10480. DebugAssert(FClipboardFakeDirectory.IsEmpty());
  10481. DebugAssert(FClipboardFakeMonitors.get() == NULL);
  10482. FClipboardFakeDirectory = CreateFakeTransferDirectory();
  10483. // Intermediate variable to WORKAROUND a crash in compiler
  10484. TObjectList * ClipboardFakeMonitors =
  10485. StartCreationDirectoryMonitorsOnEachDrive(FILE_NOTIFY_CHANGE_DIR_NAME, ClipboardFakeCreated);
  10486. FClipboardFakeMonitors.reset(ClipboardFakeMonitors);
  10487. if (FClipboardDragDropFilesEx.get() == NULL)
  10488. {
  10489. FClipboardDragDropFilesEx.reset(new TDragDropFilesEx(this));
  10490. FClipboardDragDropFilesEx->OnDataObjectRelease = ClipboardDataObjectRelease;
  10491. }
  10492. FClipboardDragDropFilesEx->FileList->Clear();
  10493. FClipboardDragDropFilesEx->FileList->AddItem(NULL, FClipboardFakeDirectory);
  10494. FClipboardDragDropFilesEx->CopyToClipboard();
  10495. FClipboardTerminal = Terminal;
  10496. // Need full paths, as cwd can be different once files are pasted
  10497. FClipboardFileList.reset(TRemoteFileList::CloneStrings(RemoteDirView->CreateFileList(OnFocused, true)));
  10498. for (int Index = 0; Index < FClipboardFileList->Count; Index++)
  10499. {
  10500. FClipboardFileList->Strings[Index] = UnixExcludeTrailingBackslash(FClipboardFileList->Strings[Index]);
  10501. }
  10502. }
  10503. }
  10504. //---------------------------------------------------------------------------
  10505. void __fastcall TCustomScpExplorerForm::ClipboardClear()
  10506. {
  10507. if (OpenClipboard(0))
  10508. {
  10509. EmptyClipboard(); // Calls ClipboardDataObjectRelease, if clipboard contains our data
  10510. CloseClipboard();
  10511. }
  10512. }
  10513. //---------------------------------------------------------------------------
  10514. bool __fastcall TCustomScpExplorerForm::DoesClipboardContainOurFiles()
  10515. {
  10516. return !FClipboardFakeDirectory.IsEmpty();
  10517. }
  10518. //---------------------------------------------------------------------------
  10519. void __fastcall TCustomScpExplorerForm::ClipboardStop()
  10520. {
  10521. RemoveDir(ApiPath(FClipboardFakeDirectory));
  10522. FClipboardFakeDirectory = UnicodeString();
  10523. FClipboardTerminal = NULL;
  10524. if (FDownloadingFromClipboard)
  10525. {
  10526. // We are called by the monitor, so attempt to release it would deadlock
  10527. FClipboardFakeMonitorsPendingReset = true;
  10528. }
  10529. else
  10530. {
  10531. FClipboardFakeMonitors.reset(NULL);
  10532. }
  10533. }
  10534. //---------------------------------------------------------------------------
  10535. void __fastcall TCustomScpExplorerForm::ClipboardDataObjectRelease(TObject * /*Sender*/)
  10536. {
  10537. if (DebugAlwaysTrue(!FClipboardFakeDirectory.IsEmpty()))
  10538. {
  10539. ClipboardStop();
  10540. }
  10541. }
  10542. //---------------------------------------------------------------------------
  10543. void __fastcall TCustomScpExplorerForm::ClipboardDownload(const UnicodeString & TargetDirectory, bool NoConfirmation, bool DragDrop)
  10544. {
  10545. TTerminalManager * Manager = TTerminalManager::Instance();
  10546. Manager->ActiveSession = FClipboardTerminal;
  10547. TTransferOperationParam Params;
  10548. Params.TargetDirectory = TargetDirectory;
  10549. Params.DragDrop = DragDrop;
  10550. ExecuteFileOperation(foCopy, osRemote, FClipboardFileList.get(), NoConfirmation, &Params);
  10551. }
  10552. //---------------------------------------------------------------------------
  10553. void __fastcall TCustomScpExplorerForm::ClipboardFakeCreated(TObject * /*Sender*/, const UnicodeString FileName)
  10554. {
  10555. // It can actually rarelly happen that some random file is created, while we are shutting down the monitor
  10556. // (as it pumps a Windows message queue while being shutted down)
  10557. if (DebugAlwaysTrue(!FClipboardFakeDirectory.IsEmpty()) &&
  10558. SameText(ExtractFileName(FileName), ExtractFileName(FClipboardFakeDirectory)))
  10559. {
  10560. // Can fail as it can be e.g. locked by AV, so we retry that later
  10561. bool Removed = RemoveDir(ApiPath(FileName));
  10562. try
  10563. {
  10564. if (!NonVisualDataModule->Busy)
  10565. {
  10566. DebugAssert(!FClipboardFakeMonitorsPendingReset);
  10567. FClipboardFakeMonitorsPendingReset = false;
  10568. bool NoConfirmation = (WinConfiguration->DDTransferConfirmation == asOff);
  10569. TAutoFlag Flag(FDownloadingFromClipboard);
  10570. ClipboardDownload(ExtractFilePath(FileName), NoConfirmation, true);
  10571. }
  10572. }
  10573. __finally
  10574. {
  10575. if (!Removed)
  10576. {
  10577. RemoveDir(ApiPath(FileName));
  10578. }
  10579. }
  10580. }
  10581. }
  10582. //---------------------------------------------------------------------------
  10583. void __fastcall TCustomScpExplorerForm::DirViewGetItemColor(
  10584. TObject * Sender, UnicodeString FileName, bool Directory, __int64 Size, TDateTime Modification, TColor & Color)
  10585. {
  10586. TFileMasks::TParams MaskParams;
  10587. MaskParams.Size = Size;
  10588. MaskParams.Modification = Modification;
  10589. TCustomDirView * DirView = DebugNotNull(dynamic_cast<TCustomDirView *>(Sender));
  10590. bool Local = (dynamic_cast<TUnixDirView *>(DirView) == NULL);
  10591. // TUnixDirView::ItemFullFileName returns trailing backslash for directories, what filemask cannot deal with
  10592. if (!Local && Directory)
  10593. {
  10594. FileName = UnixExcludeTrailingBackslash(FileName);
  10595. }
  10596. for (TFileColorData::TList::const_iterator Iter = FFileColors.begin(); Iter != FFileColors.end(); Iter++)
  10597. {
  10598. bool ImplicitMatch;
  10599. if (Iter->FileMask.Matches(FileName, Local, Directory, &MaskParams, false, ImplicitMatch) &&
  10600. !ImplicitMatch)
  10601. {
  10602. Color = Iter->Color;
  10603. break;
  10604. }
  10605. }
  10606. }
  10607. //---------------------------------------------------------------------------
  10608. void __fastcall TCustomScpExplorerForm::DirViewKeyDown(TObject * Sender, WORD & Key, TShiftState)
  10609. {
  10610. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  10611. if (!DirView->IsEditing())
  10612. {
  10613. // Handled here, instead of RemoteDirViewKeyPress (as on Login dialog),
  10614. // as VK_BACK is handled by TCustomDirView.KeyDown and we can swallow it here,
  10615. // while searching
  10616. if (Key == VK_BACK)
  10617. {
  10618. if (!FIncrementalSearch.IsEmpty())
  10619. {
  10620. if (FIncrementalSearch.Length() == 1)
  10621. {
  10622. ResetIncrementalSearch();
  10623. }
  10624. else
  10625. {
  10626. UnicodeString NewText = FIncrementalSearch.SubString(1, FIncrementalSearch.Length() - 1);
  10627. IncrementalSearch(NewText, false, false);
  10628. }
  10629. Key = 0;
  10630. }
  10631. }
  10632. }
  10633. }
  10634. //---------------------------------------------------------------------------
  10635. void __fastcall TCustomScpExplorerForm::DirViewKeyPress(TObject * Sender, wchar_t & Key)
  10636. {
  10637. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  10638. if (!DirView->IsEditing() && (WinConfiguration->PanelSearch != isOff))
  10639. {
  10640. // Filter control sequences.
  10641. // When not searching yet, prefer use of the space for toggling file selection
  10642. // (so we cannot incrementally search for a string starting with a space).
  10643. if ((Key > VK_SPACE) ||
  10644. ((Key == VK_SPACE) && (GetKeyState(VK_CONTROL) >= 0) && !FIncrementalSearch.IsEmpty()))
  10645. {
  10646. IncrementalSearch(FIncrementalSearch + Key, false, false);
  10647. Key = 0;
  10648. }
  10649. }
  10650. }
  10651. //---------------------------------------------------------------------------
  10652. void __fastcall TCustomScpExplorerForm::ResetIncrementalSearch()
  10653. {
  10654. if (!FIncrementalSearch.IsEmpty())
  10655. {
  10656. FIncrementalSearch = L"";
  10657. DirView(osCurrent)->UpdateStatusBar();
  10658. }
  10659. }
  10660. //---------------------------------------------------------------------------
  10661. void __fastcall TCustomScpExplorerForm::IncrementalSearch(const UnicodeString & Text, bool SkipCurrent, bool Reverse)
  10662. {
  10663. TListItem * Item = SearchFile(Text, SkipCurrent, Reverse);
  10664. if (Item == NULL)
  10665. {
  10666. MessageBeep(MB_ICONHAND);
  10667. }
  10668. else
  10669. {
  10670. TCustomDirView * ADirView = DirView(osCurrent);
  10671. {
  10672. TAutoNestingCounter Guard(FIncrementalSearching);
  10673. ADirView->FocusItem(Item);
  10674. if (ADirView->NortonLike == nlOff)
  10675. {
  10676. if ((ADirView->Selected != Item) ||
  10677. (ADirView->SelCount > 1))
  10678. {
  10679. ADirView->ClearSelection();
  10680. ADirView->Selected = Item;
  10681. }
  10682. }
  10683. }
  10684. FIncrementalSearch = Text;
  10685. Item->MakeVisible(false);
  10686. TListItem * NextItem = SearchFile(Text, true, Reverse);
  10687. FIncrementalSearchHaveNext = (NextItem != NULL) && (NextItem != Item);
  10688. ADirView->UpdateStatusBar();
  10689. }
  10690. }
  10691. //---------------------------------------------------------------------------
  10692. TListItem * __fastcall TCustomScpExplorerForm::GetNextFile(TListItem * Item, bool Reverse)
  10693. {
  10694. int Index = Item->Index;
  10695. if (!Reverse)
  10696. {
  10697. Index++;
  10698. if (Index >= Item->Owner->Count)
  10699. {
  10700. Index = 0;
  10701. }
  10702. }
  10703. else
  10704. {
  10705. Index--;
  10706. if (Index < 0)
  10707. {
  10708. Index = Item->Owner->Count - 1;
  10709. }
  10710. }
  10711. return Item->Owner->Item[Index];
  10712. }
  10713. //---------------------------------------------------------------------------
  10714. TListItem * __fastcall TCustomScpExplorerForm::SearchFile(const UnicodeString & Text, bool SkipCurrent, bool Reverse)
  10715. {
  10716. TCustomDirView * ADirView = DirView(osCurrent);
  10717. if (ADirView->Items->Count == 0)
  10718. {
  10719. return NULL;
  10720. }
  10721. else
  10722. {
  10723. TListItem * CurrentItem = (ADirView->ItemFocused != NULL) ? ADirView->ItemFocused : ADirView->Items->Item[0];
  10724. TListItem * Item = CurrentItem;
  10725. if (SkipCurrent)
  10726. {
  10727. Item = DebugNotNull(GetNextFile(Item, Reverse));
  10728. }
  10729. while (true)
  10730. {
  10731. bool Matches = false;
  10732. switch (WinConfiguration->PanelSearch)
  10733. {
  10734. case isNameStartOnly:
  10735. Matches = ContainsTextSemiCaseSensitive(ADirView->ItemFileName(Item).SubString(1, Text.Length()), Text);
  10736. break;
  10737. case isName:
  10738. Matches = ContainsTextSemiCaseSensitive(ADirView->ItemFileName(Item), Text);
  10739. break;
  10740. case isAll:
  10741. int ColCount = (ADirView->ViewStyle == vsReport) ? ADirView->ColProperties->Count : 1;
  10742. int Index = 0;
  10743. while ((Index < ColCount) && !Matches)
  10744. {
  10745. bool Visible = (ADirView->ViewStyle == vsReport) ? ADirView->ColProperties->Visible[Index] : true;
  10746. if (Visible)
  10747. {
  10748. Matches = ContainsTextSemiCaseSensitive(ADirView->GetColumnText(Item, Index), Text);
  10749. }
  10750. Index++;
  10751. }
  10752. break;
  10753. }
  10754. if (Matches)
  10755. {
  10756. return Item;
  10757. }
  10758. Item = GetNextFile(Item, Reverse);
  10759. if (Item == CurrentItem)
  10760. {
  10761. return NULL;
  10762. }
  10763. }
  10764. }
  10765. }
  10766. //---------------------------------------------------------------------------
  10767. void __fastcall TCustomScpExplorerForm::DirViewExit(TObject *)
  10768. {
  10769. ResetIncrementalSearch();
  10770. }
  10771. //---------------------------------------------------------------------------
  10772. void __fastcall TCustomScpExplorerForm::CMDialogKey(TWMKeyDown & Message)
  10773. {
  10774. if (Message.CharCode == VK_TAB)
  10775. {
  10776. if (!FIncrementalSearch.IsEmpty())
  10777. {
  10778. TShiftState Shift = KeyDataToShiftState(Message.KeyData);
  10779. bool Reverse = Shift.Contains(ssShift);
  10780. IncrementalSearch(FIncrementalSearch, true, Reverse);
  10781. Message.Result = 1;
  10782. return;
  10783. }
  10784. }
  10785. else if (Message.CharCode == VK_ESCAPE)
  10786. {
  10787. if (!FIncrementalSearch.IsEmpty())
  10788. {
  10789. ResetIncrementalSearch();
  10790. Message.Result = 1;
  10791. return;
  10792. }
  10793. }
  10794. TForm::Dispatch(&Message);
  10795. }
  10796. //---------------------------------------------------------------------------
  10797. void __fastcall TCustomScpExplorerForm::Deactivate()
  10798. {
  10799. TForm::Deactivate();
  10800. // E.g. when switching to an internal editor window
  10801. ResetIncrementalSearch();
  10802. }
  10803. //---------------------------------------------------------------------------
  10804. void __fastcall TCustomScpExplorerForm::ApplicationEventsDeactivate(TObject *)
  10805. {
  10806. // When switching to another application
  10807. ResetIncrementalSearch();
  10808. }
  10809. //---------------------------------------------------------------------------
  10810. void __fastcall TCustomScpExplorerForm::ApplicationEventsModalBegin(TObject *)
  10811. {
  10812. ResetIncrementalSearch();
  10813. }
  10814. //---------------------------------------------------------------------------
  10815. void __fastcall TCustomScpExplorerForm::DirViewChangeFocus(TObject *, TListItem *)
  10816. {
  10817. if (FIncrementalSearching <= 0)
  10818. {
  10819. ResetIncrementalSearch();
  10820. }
  10821. }
  10822. //---------------------------------------------------------------------------
  10823. void __fastcall TCustomScpExplorerForm::RemoteStatusBarMouseDown(TObject *, TMouseButton, TShiftState, int, int)
  10824. {
  10825. CountClicksForWindowPrint(this);
  10826. }
  10827. //---------------------------------------------------------------------------
  10828. void __fastcall TCustomScpExplorerForm::RemoteDirViewResize(TObject *)
  10829. {
  10830. CenterReconnectToolbar();
  10831. }
  10832. //---------------------------------------------------------------------------
  10833. void __fastcall TCustomScpExplorerForm::BrowseFile()
  10834. {
  10835. if (RemoteDirView->ItemFocused != NULL)
  10836. {
  10837. RemoteDirView->ItemFocused->Selected = true;
  10838. }
  10839. }
  10840. //---------------------------------------------------------------------------
  10841. void __fastcall TCustomScpExplorerForm::UpdateQueueFileList()
  10842. {
  10843. if (!FQueueStatusUpdating)
  10844. {
  10845. bool Refresh;
  10846. if ((FQueueStatus != NULL) && QueueFileList->Visible)
  10847. {
  10848. Refresh = FQueueStatus->UpdateFileList(FQueueController->GetFocusedPrimaryItem(), FQueueFileList.get());
  10849. }
  10850. else
  10851. {
  10852. if (FQueueFileList->GetCount() > 0)
  10853. {
  10854. FQueueFileList->Clear();
  10855. Refresh = true;
  10856. }
  10857. }
  10858. int Count = FQueueFileList->GetCount();
  10859. if (QueueFileList->Items->Count != Count)
  10860. {
  10861. DebugAssert(Refresh);
  10862. QueueFileList->Items->Count = Count;
  10863. }
  10864. if (Refresh)
  10865. {
  10866. QueueFileList->Invalidate();
  10867. }
  10868. DebugAssert(QueueFileList->Items->Count == FQueueFileList->GetCount());
  10869. }
  10870. }
  10871. //---------------------------------------------------------------------------
  10872. void __fastcall TCustomScpExplorerForm::QueueFileListData(TObject *, TListItem * Item)
  10873. {
  10874. Item->Caption = FQueueFileList->GetFileName(Item->Index);
  10875. }
  10876. //---------------------------------------------------------------------------
  10877. void __fastcall TCustomScpExplorerForm::QueueView3Change(TObject *, TListItem *, TItemChange)
  10878. {
  10879. // Should be improved, once we do not refresh the list when switching between details of the same batch.
  10880. // See TSynchronizeChecklistDialog::UpdateTimer
  10881. if (QueueFileList->Items->Count > 0)
  10882. {
  10883. QueueFileList->Items->Item[0]->MakeVisible(false);
  10884. }
  10885. UpdateQueueFileList();
  10886. }
  10887. //---------------------------------------------------------------------------
  10888. void __fastcall TCustomScpExplorerForm::QueueLabelGetStatus(TCustomPathLabel *, bool & Active)
  10889. {
  10890. Active = QueueView3->Focused() || QueueFileList->Focused();
  10891. }
  10892. //---------------------------------------------------------------------------
  10893. void __fastcall TCustomScpExplorerForm::QueueFileListEnterExit(TObject *)
  10894. {
  10895. QueueLabelUpdateStatus();
  10896. }
  10897. //---------------------------------------------------------------------------
  10898. void __fastcall TCustomScpExplorerForm::QueueFileListCustomDrawItem(
  10899. TCustomListView * Sender, TListItem * Item, TCustomDrawState, bool & DebugUsedArg(DefaultDraw))
  10900. {
  10901. int State = FQueueFileList->GetState(Item->Index);
  10902. if (State == qfsProcessed)
  10903. {
  10904. Sender->Canvas->Font->Color = clGrayText;
  10905. }
  10906. }
  10907. //---------------------------------------------------------------------------
  10908. void __fastcall TCustomScpExplorerForm::QueueFileListColumnAutoSize()
  10909. {
  10910. // We do not really need whole width, so we always preemptivelly keep free space for the vertical scrollbar
  10911. QueueFileList->Column[0]->Width =
  10912. QueueFileList->ClientWidth - GetSystemMetricsForControl(QueueFileList, SM_CXVSCROLL) - ScaleByTextHeight(QueueFileList, 8);
  10913. }
  10914. //---------------------------------------------------------------------------
  10915. void __fastcall TCustomScpExplorerForm::QueueFileListResize(TObject *)
  10916. {
  10917. QueueFileListColumnAutoSize();
  10918. }
  10919. //---------------------------------------------------------------------------
  10920. void __fastcall TCustomScpExplorerForm::CloseApp()
  10921. {
  10922. // Called from TNonVisualDataModule::ExplorerActionsExecute, which sets busy flag.
  10923. // FormCloseQuery must check busy state, so that the application cannot be closed while busy by Alt+F4 and X button.
  10924. // So we clear the flag here.
  10925. DebugAssert(NonVisualDataModule->Busy);
  10926. NonVisualDataModule->EndBusy();
  10927. try
  10928. {
  10929. Close();
  10930. }
  10931. __finally
  10932. {
  10933. NonVisualDataModule->StartBusy();
  10934. }
  10935. }
  10936. //---------------------------------------------------------------------------
  10937. bool __fastcall TCustomScpExplorerForm::IsActiveTerminal(TTerminal * Terminal)
  10938. {
  10939. return (Terminal != NULL) && Terminal->Active;
  10940. }
  10941. //---------------------------------------------------------------------------
  10942. bool __fastcall TCustomScpExplorerForm::HasManagedSession()
  10943. {
  10944. return (ManagedSession != NULL);
  10945. }
  10946. //---------------------------------------------------------------------------
  10947. bool __fastcall TCustomScpExplorerForm::HasActiveTerminal()
  10948. {
  10949. return IsActiveTerminal(Terminal);
  10950. }
  10951. //---------------------------------------------------------------------------
  10952. void TCustomScpExplorerForm::LocalLocalCopy(
  10953. TFileOperation, TOperationSide, bool DebugUsedArg(OnFocused), bool DebugUsedArg(NoConfirmation),
  10954. bool DebugUsedArg(DragDrop), unsigned int DebugUsedArg(Flags))
  10955. {
  10956. DebugFail();
  10957. }
  10958. //---------------------------------------------------------------------------
  10959. void TCustomScpExplorerForm::LocalLocalCopyCommand(TFileOperation Operation, TOperationSide Side, bool OnFocused, unsigned int Flags)
  10960. {
  10961. LocalLocalCopy(Operation, Side, OnFocused, !WinConfiguration->ConfirmTransferring, false, Flags);
  10962. }
  10963. //---------------------------------------------------------------------------
  10964. void __fastcall TCustomScpExplorerForm::SessionsPageControlResize(TObject *)
  10965. {
  10966. // 1) Changing tab list triggers the OnResize.
  10967. // 2) Is called in TForm constructor, when we are not ready to call SessionListChanged (particularly when starting with colored session)
  10968. if (!FUpdatingSessionTabs && (FSessionColors != NULL))
  10969. {
  10970. // UpdateTabsCaptionTruncation should be enough
  10971. SessionListChanged(true);
  10972. }
  10973. }
  10974. //---------------------------------------------------------------------------
  10975. UnicodeString TCustomScpExplorerForm::GetSessionPath(TManagedTerminal * ASession, TOperationSide Side)
  10976. {
  10977. if (ASession == ManagedSession)
  10978. {
  10979. UpdateSession(ASession);
  10980. }
  10981. TSessionData * Data = ASession->StateData;
  10982. UnicodeString Result;
  10983. if (Side == osLocal)
  10984. {
  10985. Result = Data->LocalDirectory;
  10986. }
  10987. else if (DebugAlwaysTrue(Side == osOther))
  10988. {
  10989. Result = ASession->LocalBrowser ? Data->OtherLocalDirectory : Data->RemoteDirectory;
  10990. }
  10991. return Result;
  10992. }
  10993. //---------------------------------------------------------------------------
  10994. UnicodeString TCustomScpExplorerForm::GetTabHintSessionDetails(TManagedTerminal * ASession)
  10995. {
  10996. UnicodeString Title = TTerminalManager::Instance()->GetSessionTitle(ASession, false);
  10997. UnicodeString Result = ASession->SessionData->DefaultSessionName;
  10998. if (Title == Result)
  10999. {
  11000. Result = EmptyStr;
  11001. }
  11002. else
  11003. {
  11004. Result += L"\n";
  11005. }
  11006. return Result;
  11007. }
  11008. //---------------------------------------------------------------------------
  11009. UnicodeString TCustomScpExplorerForm::GetTabHintDetails(TManagedTerminal * ASession)
  11010. {
  11011. UnicodeString Result = GetTabHintSessionDetails(ASession);
  11012. if (!ASession->Active)
  11013. {
  11014. Result += LoadStr(STATUS_NOT_CONNECTED2);
  11015. }
  11016. else
  11017. {
  11018. Result += GetSessionPath(ASession, osRemote);
  11019. }
  11020. return Result;
  11021. }
  11022. //---------------------------------------------------------------------------
  11023. UnicodeString TCustomScpExplorerForm::GetNewTabHintDetails()
  11024. {
  11025. return LoadStr(NEW_REMOTE_TAB_HINT);
  11026. }
  11027. //---------------------------------------------------------------------------
  11028. void __fastcall TCustomScpExplorerForm::SessionsPageControlTabHint(TPageControl *, int Index, UnicodeString & Hint)
  11029. {
  11030. if (Index >= 0)
  11031. {
  11032. TThemeTabSheet * TabSheet = SessionsPageControl->Pages[Index];
  11033. TManagedTerminal * TabSession = GetSessionTabSession(TabSheet);
  11034. if (TabSession == NULL)
  11035. {
  11036. UnicodeString NewTabTabCaption = GetNewTabTabCaption();
  11037. if (NewTabTabCaption != TabSheet->Caption)
  11038. {
  11039. Hint = NewTabTabCaption + L"|" + GetNewTabHintDetails();
  11040. }
  11041. }
  11042. else
  11043. {
  11044. TTerminalManager * Manager = TTerminalManager::Instance();
  11045. UnicodeString TabCaption = Manager->GetSessionTitle(TabSession, true);
  11046. Hint = TabCaption + L"|" + GetTabHintDetails(TabSession);
  11047. }
  11048. }
  11049. }
  11050. //---------------------------------------------------------------------------
  11051. typedef std::vector<TListItem *> TListItemsVector;
  11052. struct TCalculateSizeOperation
  11053. {
  11054. TCalculateSizeStats Stats;
  11055. TListItemsVector ListItems;
  11056. size_t Index;
  11057. };
  11058. //---------------------------------------------------------------------------
  11059. void TCustomScpExplorerForm::CalculateDirectorySizes(TOperationSide Side)
  11060. {
  11061. TCalculateSizeOperation CalculateSizeOperation;
  11062. Side = GetSide(Side);
  11063. TCustomDirView * ADirView = DirView(Side);
  11064. bool LocalSide = IsSideLocalBrowser(Side);
  11065. std::unique_ptr<TStrings> AllFileList(ADirView->CreateFileList(false, LocalSide, NULL, true));
  11066. std::unique_ptr<TStrings> FileList(new TStringList());
  11067. for (int Index = 0; Index < AllFileList->Count; Index++)
  11068. {
  11069. TListItem * Item = DebugNotNull(dynamic_cast<TListItem *>(AllFileList->Objects[Index]));
  11070. if (ADirView->ItemIsDirectory(Item))
  11071. {
  11072. FileList->AddObject(AllFileList->Strings[Index], ADirView->ItemData(Item));
  11073. CalculateSizeOperation.ListItems.push_back(Item);
  11074. }
  11075. }
  11076. CalculateSizeOperation.Index = 0;
  11077. __int64 Size = 0;
  11078. TValueRestorer<TCalculateSizeOperation *> DirectorySizeOperationRestorer(FCalculateSizeOperation);
  11079. FCalculateSizeOperation = &CalculateSizeOperation;
  11080. TValueRestorer<TFileOperationFinishedEvent> OnFileOperationFinishedRestorer(FOnFileOperationFinished);
  11081. FOnFileOperationFinished = DirectorySizeCalculated;
  11082. TValueRestorer<TFileOperation> PrimaryOperationRestorer(FPrimaryOperation);
  11083. FPrimaryOperation = foCalculateSize;
  11084. TCalculatedSizes CalculatedSizes;
  11085. CalculateSizeOperation.Stats.CalculatedSizes = &CalculatedSizes;
  11086. if (LocalSide)
  11087. {
  11088. TValueRestorer<TOperationSide> ProgressSideRestorer(FProgressSide);
  11089. FProgressSide = Side;
  11090. ManagedSession->CalculateLocalFilesSize(FileList.get(), Size, NULL, true, NULL, &CalculatedSizes);
  11091. }
  11092. else
  11093. {
  11094. TCalculateSizeParams Params;
  11095. Params.Stats = &CalculateSizeOperation.Stats;
  11096. Params.UseCache = true;
  11097. Terminal->CalculateFilesSize(FileList.get(), Size, Params);
  11098. }
  11099. }
  11100. //---------------------------------------------------------------------------
  11101. void __fastcall TCustomScpExplorerForm::DirectorySizeCalculated(TOperationSide Side, const UnicodeString & FileName, bool Success)
  11102. {
  11103. DebugAlwaysTrue(FCalculateSizeOperation != NULL);
  11104. TListItem * Item = VisualiseOperationFinished(Side, FileName, false);
  11105. TCustomDirView * ADirView = dynamic_cast<TCustomDirView *>(Item->Owner->Owner);
  11106. // optimization
  11107. bool GotExpectedItem =
  11108. (FCalculateSizeOperation->Index < FCalculateSizeOperation->ListItems.size()) &&
  11109. (FCalculateSizeOperation->ListItems[FCalculateSizeOperation->Index] == Item);
  11110. int Index = -1;
  11111. if (DebugAlwaysTrue(GotExpectedItem))
  11112. {
  11113. Index = FCalculateSizeOperation->Index;
  11114. FCalculateSizeOperation->Index++;
  11115. }
  11116. else
  11117. {
  11118. TListItemsVector::iterator I =
  11119. std::find(FCalculateSizeOperation->ListItems.begin(), FCalculateSizeOperation->ListItems.end(), Item);
  11120. if (DebugAlwaysTrue(I != FCalculateSizeOperation->ListItems.end()))
  11121. {
  11122. Index = I - FCalculateSizeOperation->ListItems.begin();
  11123. }
  11124. }
  11125. if (DebugAlwaysTrue(Index >= 0) && Success)
  11126. {
  11127. __int64 Size = (*FCalculateSizeOperation->Stats.CalculatedSizes)[Index];
  11128. ADirView->SetItemCalculatedSize(Item, Size);
  11129. }
  11130. }
  11131. //---------------------------------------------------------------------------
  11132. void __fastcall TCustomScpExplorerForm::MessageDockRequestDock(TObject *, TTBCustomDockableWindow *, bool & Accept)
  11133. {
  11134. Accept = false;
  11135. }
  11136. //---------------------------------------------------------------------------