CustomScpExplorer.cpp 388 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721
  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. FUserActionTimer = new TTimer(this);
  222. FUserActionTimer->Enabled = false;
  223. FUserActionTimer->Interval = 10;
  224. FUserActionTimer->OnTimer = UserActionTimer;
  225. FNotes = new TStringList();
  226. FNoteTimer = new TTimer(this);
  227. FNoteTimer->Enabled = false;
  228. FNoteTimer->OnTimer = NoteTimer;
  229. FOnNoteClick = NULL;
  230. FOle32Library = LoadLibrary(L"Ole32.dll");
  231. FDragMoveCursor = FOle32Library != NULL ?
  232. LoadCursor(FOle32Library, MAKEINTRESOURCE(2)) : NULL;
  233. UseSystemSettings(this);
  234. TTBXStringList * TransferList = dynamic_cast<TTBXStringList*>(
  235. static_cast<TObject*>(GetComponent(fcTransferList)));
  236. DebugAssert(TransferList != NULL);
  237. FTransferListHoverIndex = -1;
  238. TransferList->OnChange = TransferListChange;
  239. TransferList->OnDrawItem = TransferListDrawItem;
  240. SetSubmenu(dynamic_cast<TTBXCustomItem *>(static_cast<TObject *>(GetComponent(fcColorMenu))), true);
  241. SetSubmenu(NonVisualDataModule->ColorMenuItem, true);
  242. UseDesktopFont(SessionsPageControl);
  243. UpdateSessionsPageControlHeight();
  244. UseDesktopFont(RemoteDirView);
  245. UseDesktopFont(RemoteDriveView);
  246. UseDesktopFont(QueueView3);
  247. UseDesktopFont(QueueFileList);
  248. UseDesktopFont(QueueLabel);
  249. UseDesktopFont(RemoteStatusBar);
  250. reinterpret_cast<TLabel*>(QueueSplitter)->OnDblClick = QueueSplitterDblClick;
  251. QueueSplitter->ShowHint = true;
  252. reinterpret_cast<TLabel*>(QueueFileListSplitter)->OnDblClick = QueueFileListSplitterDblClick;
  253. QueueFileListSplitter->ShowHint = true;
  254. RemotePanelSplitter->ShowHint = true;
  255. UpdateImages();
  256. FCustomCommandMenu = CreateTBXPopupMenu(this);
  257. FCustomCommandLocalFileList = NULL;
  258. FCustomCommandRemoteFileList = NULL;
  259. FSessionColors = new TPngImageList(this);
  260. FSessionColors->ColorDepth = cd32Bit;
  261. AddFixedSessionImages();
  262. SessionsPageControl->Images = FSessionColors;
  263. UpdateQueueLabel();
  264. CheckStoreTransition();
  265. CreateHiddenWindow();
  266. StartUpdates();
  267. }
  268. //---------------------------------------------------------------------------
  269. __fastcall TCustomScpExplorerForm::~TCustomScpExplorerForm()
  270. {
  271. FInvalid = true;
  272. // this has to be one of the very first things to do
  273. StopUpdateThread();
  274. // This is needed when shuting down Windows only. Otherwise it's already set NULL from Execute()
  275. TTerminalManager::Instance()->ScpExplorer = NULL;
  276. delete FCustomCommandLocalFileList;
  277. delete FCustomCommandRemoteFileList;
  278. delete FCustomCommandMenu;
  279. delete FTrayIcon;
  280. FTrayIcon = NULL;
  281. FEditorManager->CloseInternalEditors(ForceCloseInternalEditor);
  282. delete FEditorManager;
  283. ForceCloseLocalEditors();
  284. delete FLocalEditors;
  285. HideFileFindDialog();
  286. if (FDelayedDeletionTimer)
  287. {
  288. DoDelayedDeletion(NULL);
  289. SAFE_DESTROY(FDelayedDeletionTimer);
  290. }
  291. SAFE_DESTROY(FDelayedDeletionList);
  292. // sometimes we do not get DDEnd so the list is not released
  293. SAFE_DESTROY(FDDFileList);
  294. DebugAssert(FSynchronizeController == NULL);
  295. CloseHandle(FDDExtMutex);
  296. FDDExtMutex = NULL;
  297. FreeLibrary(FOle32Library);
  298. FOle32Library = NULL;
  299. FDragMoveCursor = NULL;
  300. DebugAssert(!FErrorList);
  301. if (FEverShown)
  302. {
  303. // when window is never shown (like when running command-line operation),
  304. // particularly window site is not restored correctly (BoundsRect value set
  305. // in RestoreForm gets lost during handle allocation), so we do not want
  306. // it to be stored
  307. StoreParams();
  308. }
  309. ManagedSession = NULL;
  310. Queue = NULL;
  311. DebugAssert(NonVisualDataModule && (NonVisualDataModule->ScpExplorer == this));
  312. NonVisualDataModule->ScpExplorer = NULL;
  313. delete FQueueController;
  314. FQueueController = NULL;
  315. delete FQueueStatusSection;
  316. FQueueStatusSection = NULL;
  317. delete FQueueStatus;
  318. FQueueStatus = NULL;
  319. delete FUserActionTimer;
  320. FUserActionTimer = NULL;
  321. delete FNoteTimer;
  322. delete FNotes;
  323. delete FNoteData;
  324. SAFE_DESTROY(FDocks);
  325. SessionsPageControl->Images = NULL;
  326. // only after clearing Terminal (after SessionListChanged => ... => AddSessionColor is called)
  327. SAFE_DESTROY(FSessionColors);
  328. SAFE_DESTROY(FSessionsDragDropFilesEx);
  329. SAFE_DESTROY(FHistoryMenu[0][0]);
  330. SAFE_DESTROY(FHistoryMenu[0][1]);
  331. SAFE_DESTROY(FHistoryMenu[1][0]);
  332. SAFE_DESTROY(FHistoryMenu[1][1]);
  333. if (FHiddenWindow != NULL)
  334. {
  335. DestroyWindow(FHiddenWindow);
  336. FHiddenWindow = NULL;
  337. }
  338. }
  339. //---------------------------------------------------------------------------
  340. void __fastcall TCustomScpExplorerForm::RefreshPanel(const UnicodeString & Session, const UnicodeString & Path)
  341. {
  342. std::unique_ptr<TSessionData> Data;
  343. if (!Session.IsEmpty())
  344. {
  345. bool DefaultsOnly;
  346. Data.reset(StoredSessions->ParseUrl(Session, NULL, DefaultsOnly));
  347. }
  348. TTerminalManager * Manager = TTerminalManager::Instance();
  349. for (int Index = 0; Index < Manager->Count; Index++)
  350. {
  351. TTerminal * Terminal = Manager->Sessions[Index];
  352. if (Session.IsEmpty() ||
  353. Manager->IsActiveTerminalForSite(Terminal, Data.get()))
  354. {
  355. if (Path.IsEmpty())
  356. {
  357. Terminal->ClearCaches();
  358. }
  359. else
  360. {
  361. Terminal->DirectoryModified(Path, true);
  362. }
  363. }
  364. }
  365. // We should flag a pending refresh for the background terminals or busy foreground terminals
  366. if (HasActiveTerminal() &&
  367. CanCommandLineFromAnotherInstance() &&
  368. (Session.IsEmpty() || Manager->IsActiveTerminalForSite(Terminal, Data.get())) &&
  369. (Path.IsEmpty() || UnixIsChildPath(Path, Terminal->CurrentDirectory)))
  370. {
  371. Terminal->ReloadDirectory();
  372. }
  373. }
  374. //---------------------------------------------------------------------------
  375. void __fastcall TCustomScpExplorerForm::WMCopyData(TMessage & Message)
  376. {
  377. PCOPYDATASTRUCT CopyData = reinterpret_cast<PCOPYDATASTRUCT>(Message.LParam);
  378. size_t MessageSize = sizeof(TCopyDataMessage);
  379. bool Result = DebugAlwaysTrue(CopyData->cbData == MessageSize);
  380. if (Result)
  381. {
  382. const TCopyDataMessage & Message = *reinterpret_cast<const TCopyDataMessage *>(CopyData->lpData);
  383. Result = (Message.Version == TCopyDataMessage::Version1);
  384. if (Result)
  385. {
  386. switch (Message.Command)
  387. {
  388. case TCopyDataMessage::CommandCanCommandLine:
  389. Result = CanCommandLineFromAnotherInstance();
  390. break;
  391. case TCopyDataMessage::CommandCommandLine:
  392. {
  393. UnicodeString CommandLine(Message.CommandLine);
  394. Result = CommandLineFromAnotherInstance(CommandLine);
  395. }
  396. break;
  397. case TCopyDataMessage::RefreshPanel:
  398. RefreshPanel(Message.Refresh.Session, Message.Refresh.Path);
  399. break;
  400. case TCopyDataMessage::MainWindowCheck:
  401. Result = true;
  402. break;
  403. default:
  404. Result = false;
  405. break;
  406. }
  407. }
  408. }
  409. Message.Result = Result ? 1 : 0;
  410. }
  411. //---------------------------------------------------------------------------
  412. void __fastcall TCustomScpExplorerForm::CreateHiddenWindow()
  413. {
  414. WNDCLASS WindowClass = {0};
  415. WindowClass.lpfnWndProc = DefWindowProc;
  416. WindowClass.hInstance = HInstance;
  417. WindowClass.lpszClassName = HIDDEN_WINDOW_NAME;
  418. FHiddenWindow = NULL;
  419. if (RegisterClass(&WindowClass))
  420. {
  421. FHiddenWindow = CreateWindow(HIDDEN_WINDOW_NAME, L"",
  422. WS_POPUP, 0, 0, 0, 0, 0, 0, HInstance, NULL);
  423. }
  424. }
  425. //---------------------------------------------------------------------------
  426. bool __fastcall TCustomScpExplorerForm::CanConsole()
  427. {
  428. return
  429. HasActiveTerminal() &&
  430. (Terminal->IsCapable[fcAnyCommand] || Terminal->IsCapable[fcSecondaryShell]);
  431. }
  432. //---------------------------------------------------------------------------
  433. bool __fastcall TCustomScpExplorerForm::CanCommandLineFromAnotherInstance()
  434. {
  435. bool Result = Visible && !NonVisualDataModule->Busy;
  436. return Result;
  437. }
  438. //---------------------------------------------------------------------------
  439. bool __fastcall TCustomScpExplorerForm::CommandLineFromAnotherInstance(
  440. const UnicodeString & CommandLine)
  441. {
  442. TProgramParams Params(CommandLine);
  443. bool Result = CanCommandLineFromAnotherInstance() && DebugAlwaysTrue(Params.ParamCount > 0);
  444. if (Result)
  445. {
  446. NonVisualDataModule->StartBusy();
  447. try
  448. {
  449. // this action is initiated from another process,
  450. // so it's likely that our window is not visible,
  451. // and user won't see what is going on
  452. Application->BringToFront();
  453. // reload sessions as we may be asked to open a session
  454. // just stored by another instance
  455. StoredSessions->Reload();
  456. UnicodeString SessionName = Params.Param[1];
  457. std::unique_ptr<TObjectList> DataList(new TObjectList());
  458. try
  459. {
  460. UnicodeString DownloadFile; // unused
  461. int Flags = GetCommandLineParseUrlFlags(&Params);
  462. GetLoginData(SessionName, &Params, DataList.get(), DownloadFile, true, this, Flags);
  463. }
  464. catch (EAbort &)
  465. {
  466. // Happens when opening session in PuTTY and in other situations,
  467. // in which we do not want to fail WM_COPYDATA, as that would cause master instance
  468. // to process the command-line on its own (or to pass it to yet another instance)
  469. }
  470. if (DataList->Count > 0)
  471. {
  472. TTerminalManager * Manager = TTerminalManager::Instance();
  473. Manager->ActiveSession = Manager->NewSessions(DataList.get());
  474. }
  475. }
  476. __finally
  477. {
  478. NonVisualDataModule->EndBusy();
  479. }
  480. }
  481. return Result;
  482. }
  483. //---------------------------------------------------------------------------
  484. TManagedTerminal * TCustomScpExplorerForm::GetTerminal()
  485. {
  486. TManagedTerminal * Result;
  487. if ((FManagedSession != NULL) && !FManagedSession->LocalBrowser)
  488. {
  489. Result = FManagedSession;
  490. }
  491. else
  492. {
  493. Result = NULL;
  494. }
  495. return Result;
  496. }
  497. //---------------------------------------------------------------------------
  498. void __fastcall TCustomScpExplorerForm::SetManagedSession(TManagedTerminal * value)
  499. {
  500. if (FManagedSession != value)
  501. {
  502. SessionChanging();
  503. DoSetManagedSession(value, false);
  504. }
  505. }
  506. //---------------------------------------------------------------------------
  507. void __fastcall TCustomScpExplorerForm::DoSetManagedSession(TManagedTerminal * value, bool Replace)
  508. {
  509. DebugAssert(!Replace || ((value != NULL) && !value->LocalBrowser));
  510. FManagedSession = value;
  511. {
  512. TValueRestorer<bool> AllowTransferPresetAutoSelectRestorer(FAllowTransferPresetAutoSelect);
  513. FAllowTransferPresetAutoSelect = false;
  514. TAutoFlag SessionChangingFlag(FSessionChanging);
  515. SessionChanged(Replace);
  516. }
  517. if (Terminal != NULL)
  518. {
  519. TransferPresetAutoSelect();
  520. }
  521. // Update app and tab titles, prevented above by FSessionChanging.
  522. UpdateControls();
  523. }
  524. //---------------------------------------------------------------------------
  525. void __fastcall TCustomScpExplorerForm::ReplaceTerminal(TManagedTerminal * value)
  526. {
  527. DoSetManagedSession(value, true);
  528. }
  529. //---------------------------------------------------------------------------
  530. void __fastcall TCustomScpExplorerForm::SessionChanging()
  531. {
  532. if (ManagedSession != NULL)
  533. {
  534. UpdateSession(ManagedSession);
  535. }
  536. }
  537. //---------------------------------------------------------------------------
  538. void __fastcall TCustomScpExplorerForm::SessionChanged(bool Replaced)
  539. {
  540. if (ManagedSession != NULL)
  541. {
  542. UpdateSessionColor(static_cast<TColor>(ManagedSession->StateData->Color));
  543. }
  544. if (!TTerminalManager::Instance()->Updating)
  545. {
  546. SessionListChanged();
  547. }
  548. if (Terminal != NULL)
  549. {
  550. TObject * State = NULL;
  551. if (WinConfiguration->PreservePanelState)
  552. {
  553. State = Terminal->RemoteExplorerState;
  554. DirView(osRemote)->AnnounceState(State);
  555. }
  556. try
  557. {
  558. if (Replaced)
  559. {
  560. RemoteDirView->ReplaceTerminal(Terminal);
  561. }
  562. else
  563. {
  564. RemoteDirView->Terminal = Terminal;
  565. }
  566. if (Terminal->Active)
  567. {
  568. Terminal->RefreshDirectory();
  569. }
  570. if (State != NULL)
  571. {
  572. DirView(osRemote)->RestoreState(State);
  573. }
  574. }
  575. __finally
  576. {
  577. DirView(osRemote)->AnnounceState(NULL);
  578. }
  579. }
  580. else
  581. {
  582. RemoteDirView->Terminal = NULL;
  583. }
  584. InitStatusBar();
  585. UpdateTransferList();
  586. // Update panels Enable state before refreshing the labels
  587. UpdateControls();
  588. }
  589. //---------------------------------------------------------------------------
  590. void __fastcall TCustomScpExplorerForm::SetQueue(TTerminalQueue * value)
  591. {
  592. if (Queue != value)
  593. {
  594. if (FQueue != NULL)
  595. {
  596. FQueue->OnListUpdate = NULL;
  597. FQueue->OnQueueItemUpdate = NULL;
  598. }
  599. FQueue = value;
  600. if (FQueue != NULL)
  601. {
  602. DebugAssert(FQueue->OnListUpdate == NULL);
  603. FQueue->OnListUpdate = QueueListUpdate;
  604. DebugAssert(FQueue->OnQueueItemUpdate == NULL);
  605. FQueue->OnQueueItemUpdate = QueueItemUpdate;
  606. }
  607. QueueChanged();
  608. }
  609. }
  610. //---------------------------------------------------------------------------
  611. void __fastcall TCustomScpExplorerForm::QueueView3Deletion(TObject * /*Sender*/,
  612. TListItem * Item)
  613. {
  614. if (FQueueActedItem == Item)
  615. {
  616. FQueueActedItem = NULL;
  617. if ((QueueView3->PopupMenu != NULL) &&
  618. (QueueView3->PopupMenu->PopupComponent == QueueView3))
  619. {
  620. // rather "trick", suggested by Jordan on jrsoftware.toolbar2000
  621. ReleaseCapture();
  622. }
  623. }
  624. if (Item->Data == FPendingQueueActionItem)
  625. {
  626. FPendingQueueActionItem = NULL;
  627. }
  628. }
  629. //---------------------------------------------------------------------------
  630. bool TCustomScpExplorerForm::IsAnythingQueued()
  631. {
  632. return (FQueueStatus != NULL) && (FQueueStatus->ActiveAndPendingPrimaryCount > 0);
  633. }
  634. //---------------------------------------------------------------------------
  635. void __fastcall TCustomScpExplorerForm::UpdateQueueStatus(bool QueueChanging)
  636. {
  637. {
  638. TGuard Guard(FQueueStatusSection);
  639. FQueueStatusInvalidated = false;
  640. if (FQueue != NULL)
  641. {
  642. FQueueStatus = FQueue->CreateStatus(FQueueStatus);
  643. }
  644. }
  645. if ((FQueueStatus != NULL) && (FQueueStatus->Count > FMaxQueueLength))
  646. {
  647. FMaxQueueLength = FQueueStatus->Count;
  648. Configuration->Usage->SetMax(L"MaxQueueLength", FMaxQueueLength);
  649. }
  650. {
  651. TAutoFlag Flag(FQueueStatusUpdating);
  652. FQueueController->UpdateQueueStatus(FQueueStatus);
  653. }
  654. UpdateQueueFileList();
  655. SetQueueProgress();
  656. UpdateQueueView();
  657. bool IsEmpty = !IsAnythingQueued();
  658. if (IsEmpty && (Terminal != NULL))
  659. {
  660. TOnceDoneOperation OnceDoneOperation =
  661. NonVisualDataModule->CurrentQueueOnceEmptyOperation();
  662. NonVisualDataModule->ResetQueueOnceEmptyOperation();
  663. if ((FQueue != NULL) && !WinConfiguration->EnableQueueByDefault && !QueueChanging)
  664. {
  665. FQueue->Enabled = false;
  666. }
  667. if ((OnceDoneOperation != odoIdle) && !NonVisualDataModule->Busy)
  668. {
  669. Terminal->CloseOnCompletion(OnceDoneOperation, LoadStr(CLOSED_ON_QUEUE_EMPTY));
  670. }
  671. }
  672. }
  673. //---------------------------------------------------------------------------
  674. UnicodeString __fastcall TCustomScpExplorerForm::GetQueueProgressTitle()
  675. {
  676. UnicodeString Result;
  677. if (FQueueStatus != NULL)
  678. {
  679. if (FQueueStatus->IsOnlyOneActiveAndNoPending())
  680. {
  681. TFileOperationProgressType * ProgressData =
  682. FQueueStatus->Items[FQueueStatus->DoneCount]->ProgressData;
  683. if ((ProgressData != NULL) && ProgressData->InProgress)
  684. {
  685. Result = TProgressForm::ProgressStr(NULL, ProgressData);
  686. }
  687. }
  688. else if (FQueueStatus->ActiveAndPendingPrimaryCount > 1)
  689. {
  690. Result = FMTLOAD(PROGRESS_IN_QUEUE, (FQueueStatus->ActiveAndPendingPrimaryCount));
  691. }
  692. }
  693. return Result;
  694. }
  695. //---------------------------------------------------------------------------
  696. void __fastcall TCustomScpExplorerForm::UpdateQueueView()
  697. {
  698. ComponentVisible[fcQueueView] =
  699. !IsLocalBrowserMode() &&
  700. ((WinConfiguration->QueueView.Show == qvShow) ||
  701. ((WinConfiguration->QueueView.Show == qvHideWhenEmpty) &&
  702. (FQueueStatus != NULL) && (FQueueStatus->Count > 0)));
  703. }
  704. //---------------------------------------------------------------------------
  705. void __fastcall TCustomScpExplorerForm::QueueChanged()
  706. {
  707. if (FQueueStatus != NULL)
  708. {
  709. // UpdateFileList implementation relies on the status being recreated when session changes
  710. delete FQueueStatus;
  711. FQueueStatus = NULL;
  712. }
  713. UpdateQueueStatus(true);
  714. }
  715. //---------------------------------------------------------------------------
  716. void __fastcall TCustomScpExplorerForm::QueueListUpdate(TTerminalQueue * Queue)
  717. {
  718. if (FQueue == Queue)
  719. {
  720. FQueueStatusInvalidated = true;
  721. }
  722. }
  723. //---------------------------------------------------------------------------
  724. void __fastcall TCustomScpExplorerForm::QueueItemUpdate(TTerminalQueue * Queue,
  725. TQueueItem * Item)
  726. {
  727. if (FQueue == Queue)
  728. {
  729. TGuard Guard(FQueueStatusSection);
  730. // this may be running in parallel with QueueChanged
  731. if (FQueueStatus != NULL)
  732. {
  733. TQueueItemProxy * QueueItem = FQueueStatus->FindByQueueItem(Item);
  734. if ((Item->Status == TQueueItem::qsDone) && (Terminal != NULL))
  735. {
  736. FRefreshLocalDirectory = (QueueItem == NULL) ||
  737. (!QueueItem->Info->ModifiedLocal.IsEmpty());
  738. FRefreshRemoteDirectory = (QueueItem == NULL) ||
  739. (!QueueItem->Info->ModifiedRemote.IsEmpty());
  740. }
  741. if (QueueItem != NULL)
  742. {
  743. QueueItem->UserData = (void*)true;
  744. FQueueItemInvalidated = true;
  745. }
  746. }
  747. }
  748. }
  749. //---------------------------------------------------------------------------
  750. bool __fastcall TCustomScpExplorerForm::IsQueueAutoPopup()
  751. {
  752. // during standalone editing, we have no way to see/control queue,
  753. // so we have to always popup prompts automatically
  754. return StandaloneOperation || GUIConfiguration->QueueAutoPopup;
  755. }
  756. //---------------------------------------------------------------------------
  757. void __fastcall TCustomScpExplorerForm::RefreshQueueItems()
  758. {
  759. if (FQueueStatus != NULL)
  760. {
  761. bool QueueAutoPopup = IsQueueAutoPopup();
  762. bool NeedRefresh = FQueueController->NeedRefresh();
  763. bool Refresh = FQueueItemInvalidated || NeedRefresh;
  764. FQueueItemInvalidated = false;
  765. int Limit = Refresh ? FQueueStatus->Count : FQueueStatus->DoneAndActiveCount;
  766. bool Updated = false;
  767. TQueueItemProxy * QueueItem;
  768. bool WasUserAction;
  769. for (int Index = 0; Index < Limit; Index++)
  770. {
  771. bool Update = false;
  772. QueueItem = FQueueStatus->Items[Index];
  773. WasUserAction = TQueueItem::IsUserActionStatus(QueueItem->Status);
  774. if (!NonVisualDataModule->Busy && QueueAutoPopup && WasUserAction &&
  775. (FPendingQueueActionItem == NULL))
  776. {
  777. FPendingQueueActionItem = QueueItem;
  778. FUserActionTimer->Enabled = true;
  779. }
  780. if ((bool)QueueItem->UserData)
  781. {
  782. QueueItem->UserData = (void*)false;
  783. QueueItem->Update();
  784. Updated = true;
  785. Update = true;
  786. }
  787. else if (FQueueController->QueueItemNeedsFrequentRefresh(QueueItem) || NeedRefresh)
  788. {
  789. Update = true;
  790. }
  791. if (Update)
  792. {
  793. FQueueController->RefreshQueueItem(QueueItem);
  794. }
  795. }
  796. if (Updated)
  797. {
  798. NonVisualDataModule->UpdateNonVisibleActions();
  799. SetQueueProgress();
  800. UpdateQueueFileList();
  801. }
  802. }
  803. }
  804. //---------------------------------------------------------------------------
  805. void __fastcall TCustomScpExplorerForm::SetTaskbarListProgressState(TBPFLAG Flags)
  806. {
  807. if (FTaskbarList != NULL)
  808. {
  809. // Could now use Application->MainFormHandle, now that we implement Application->OnGetMainFormHandle
  810. FTaskbarList->SetProgressState(GetMainForm()->Handle, Flags);
  811. }
  812. }
  813. //---------------------------------------------------------------------------
  814. void __fastcall TCustomScpExplorerForm::SetTaskbarListProgressValue(int Progress)
  815. {
  816. if (Progress >= 0)
  817. {
  818. if (FTaskbarList != NULL)
  819. {
  820. FTaskbarList->SetProgressValue(GetMainForm()->Handle, Progress, 100);
  821. }
  822. }
  823. else
  824. {
  825. SetTaskbarListProgressState(TBPF_INDETERMINATE);
  826. }
  827. }
  828. //---------------------------------------------------------------------------
  829. void __fastcall TCustomScpExplorerForm::SetTaskbarListProgressValue(TFileOperationProgressType * ProgressData)
  830. {
  831. int OverallProgress;
  832. // FProgressForm is null when this is called from SetQueueProgress
  833. if ((FProgressForm != NULL) && (FProgressForm->SynchronizeProgress != NULL))
  834. {
  835. OverallProgress = FProgressForm->SynchronizeProgress->Progress(ProgressData);
  836. }
  837. else if (!ProgressData->IsIndeterminate())
  838. {
  839. OverallProgress = ProgressData->OverallProgress();
  840. }
  841. else
  842. {
  843. OverallProgress = -1;
  844. }
  845. SetTaskbarListProgressValue(OverallProgress);
  846. }
  847. //---------------------------------------------------------------------------
  848. void __fastcall TCustomScpExplorerForm::SetQueueProgress()
  849. {
  850. TTerminalManager::Instance()->QueueStatusUpdated();
  851. if ((FTaskbarList != NULL) && (FProgressForm == NULL))
  852. {
  853. if ((FQueueStatus != NULL) && (FQueueStatus->ActiveAndPendingPrimaryCount > 0))
  854. {
  855. if (FQueueStatus->IsOnlyOneActiveAndNoPending())
  856. {
  857. TFileOperationProgressType * ProgressData = NULL;
  858. if (FQueueStatus->Items[FQueueStatus->DoneCount] != NULL)
  859. {
  860. ProgressData = FQueueStatus->Items[FQueueStatus->DoneCount]->ProgressData;
  861. }
  862. if ((ProgressData != NULL) &&
  863. ProgressData->InProgress)
  864. {
  865. SetTaskbarListProgressValue(ProgressData);
  866. }
  867. else
  868. {
  869. SetTaskbarListProgressState(TBPF_NOPROGRESS);
  870. }
  871. }
  872. else
  873. {
  874. SetTaskbarListProgressState(TBPF_INDETERMINATE);
  875. }
  876. }
  877. else
  878. {
  879. SetTaskbarListProgressState(TBPF_NOPROGRESS);
  880. }
  881. }
  882. UpdateQueueLabel();
  883. }
  884. //---------------------------------------------------------------------------
  885. void __fastcall TCustomScpExplorerForm::UpdateQueueLabel()
  886. {
  887. UnicodeString Caption = LoadStr(QUEUE_CAPTION);
  888. if (FQueueStatus != NULL)
  889. {
  890. int ActiveAndPendingPrimaryCount = FQueueStatus->ActiveAndPendingPrimaryCount;
  891. if (ActiveAndPendingPrimaryCount > 0)
  892. {
  893. Caption = FORMAT("%s (%d)", (Caption, ActiveAndPendingPrimaryCount));
  894. }
  895. }
  896. QueueLabel->Caption = Caption;
  897. }
  898. //---------------------------------------------------------------------------
  899. void TCustomScpExplorerForm::CheckStoreTransition()
  900. {
  901. if (WinConfiguration->StoreTransition == stInit)
  902. {
  903. if (IsUWP())
  904. {
  905. WinConfiguration->StoreTransition = stStoreFresh;
  906. AppLog(L"Fresh UWP/store installation");
  907. }
  908. else
  909. {
  910. WinConfiguration->StoreTransition = stStandard;
  911. AppLog(L"Fresh standard installation");
  912. }
  913. }
  914. else if ((WinConfiguration->StoreTransition == stStandard) && IsUWP())
  915. {
  916. WinConfiguration->StoreTransition = stStoreMigrated;
  917. WinConfiguration->FirstRun = WinConfiguration->FirstRun + L"=>" + StandardDatestamp();
  918. AppLog(L"Standard installation migrated to store installation");
  919. }
  920. if (WinConfiguration->StoreTransition == stStoreMigrated)
  921. {
  922. TTBXToolbar * Toolbar = new TTBXToolbar(MessageDock);
  923. Toolbar->Parent = MessageDock;
  924. Toolbar->FullSize = true;
  925. Toolbar->Color = Application->HintColor;
  926. TTBXLabelItem * LabelItem;
  927. LabelItem = new TTBXLabelItem(Toolbar);
  928. LabelItem->Margin = ScaleByTextHeight(MessageDock, 2); // Is this automatically rescaled?
  929. LabelItem->Caption = LoadStr(STORE_MIGRATION_THANKS);
  930. Toolbar->Items->Add(LabelItem);
  931. TTBXCustomItem * LinkItem = new TTBXItem(Toolbar);
  932. LinkItem->FontSettings->Color = LinkColor;
  933. LinkItem->Caption = LoadStr(STORE_MIGRATION_LEARN);
  934. LinkItem->OnClick = StoreTransitionLinkClick;
  935. Toolbar->Items->Add(LinkItem);
  936. AddMenuSeparator(Toolbar->Items);
  937. TTBCustomItem * CloseItem = new TTBXItem(Toolbar);
  938. CloseItem->Caption = LoadStr(MESSAGE_DISMISS);
  939. CloseItem->OnClick = StoreTransitionCloseClick;
  940. Toolbar->Items->Add(CloseItem);
  941. GiveTBItemPriority(CloseItem);
  942. }
  943. }
  944. //---------------------------------------------------------------------------
  945. void __fastcall TCustomScpExplorerForm::StoreTransitionLinkClick(TObject *)
  946. {
  947. ShowHelp(HELP_STORE_TRANSITION);
  948. }
  949. //---------------------------------------------------------------------------
  950. void __fastcall TCustomScpExplorerForm::StoreTransitionCloseClick(TObject * Sender)
  951. {
  952. TTBCustomItem * Item = DebugNotNull(dynamic_cast<TTBCustomItem *>(Sender));
  953. TTBXToolbar * Toolbar = DebugNotNull(dynamic_cast<TTBXToolbar *>(DebugNotNull(DebugNotNull(Item->Parent)->ParentComponent)));
  954. delete Toolbar;
  955. WinConfiguration->StoreTransition = stStoreAcknowledged;
  956. UpdateControls();
  957. }
  958. //---------------------------------------------------------------------------
  959. void __fastcall TCustomScpExplorerForm::UpdateTransferList()
  960. {
  961. TTBXStringList * TransferList = dynamic_cast<TTBXStringList*>(
  962. static_cast<TComponent*>(GetComponent(fcTransferList)));
  963. TTBXDropDownItem * TransferDropDown = dynamic_cast<TTBXDropDownItem*>(
  964. static_cast<TComponent*>(GetComponent(fcTransferDropDown)));
  965. TransferList->Strings->BeginUpdate();
  966. try
  967. {
  968. TransferList->Strings->Assign(GUIConfiguration->CopyParamList->NameList);
  969. TransferList->Strings->Insert(0, StripHotkey(LoadStr(COPY_PARAM_DEFAULT)));
  970. TransferList->ItemIndex = GUIConfiguration->CopyParamIndex + 1;
  971. if (FTransferDropDownHint.IsEmpty())
  972. {
  973. FTransferDropDownHint = TransferDropDown->Hint;
  974. }
  975. // this way we get name for "default" settings (COPY_PARAM_DEFAULT)
  976. UnicodeString Name = TransferList->Strings->Strings[TransferList->ItemIndex];
  977. TransferDropDown->Text = StripHotkey(Name);
  978. TransferDropDown->Hint = FORMAT(L"%s|%s:\n%s",
  979. (FTransferDropDownHint, StripHotkey(Name),
  980. GUIConfiguration->CurrentCopyParam.GetInfoStr(L"; ",
  981. FLAGMASK(HasActiveTerminal(), Terminal->UsableCopyParamAttrs(0).General))));
  982. // update the label, otherwise when it is updated only on the first draw
  983. // of the list, it is drawn "bold" for some reason
  984. FTransferListHoverIndex = TransferList->ItemIndex;
  985. UpdateTransferLabel();
  986. }
  987. __finally
  988. {
  989. TransferList->Strings->EndUpdate();
  990. }
  991. }
  992. //---------------------------------------------------------------------------
  993. void __fastcall TCustomScpExplorerForm::UpdateCustomCommandsToolbar()
  994. {
  995. TTBXToolbar * Toolbar = dynamic_cast<TTBXToolbar *>(GetComponent(fcCustomCommandsBand));
  996. DebugAssert(Toolbar != NULL);
  997. NonVisualDataModule->UpdateCustomCommandsToolbar(Toolbar);
  998. }
  999. //---------------------------------------------------------------------------
  1000. void __fastcall TCustomScpExplorerForm::UpdateActions()
  1001. {
  1002. TForm::UpdateActions();
  1003. if (ComponentVisible[fcCustomCommandsBand])
  1004. {
  1005. UpdateCustomCommandsToolbar();
  1006. }
  1007. }
  1008. //---------------------------------------------------------------------------
  1009. void __fastcall TCustomScpExplorerForm::UpdateSessionsPageControlHeight()
  1010. {
  1011. SessionsPageControl->Height = SessionsPageControl->GetTabsHeight();
  1012. }
  1013. //---------------------------------------------------------------------------
  1014. void __fastcall TCustomScpExplorerForm::UpdateRowSelect(TCustomDirView * DirView)
  1015. {
  1016. if (DirView->RowSelect != WinConfiguration->FullRowSelect)
  1017. {
  1018. DirView->RowSelect = WinConfiguration->FullRowSelect;
  1019. // Without this, the panel turns gray and unused part of header turns black
  1020. DirView->Perform(CM_COLORCHANGED, 0, 0);
  1021. }
  1022. }
  1023. //---------------------------------------------------------------------------
  1024. void __fastcall TCustomScpExplorerForm::ConfigurationChanged()
  1025. {
  1026. Color = GetBtnFaceColor();
  1027. DebugAssert(Configuration && RemoteDirView);
  1028. RemoteDirView->DDAllowMove = !WinConfiguration->DDDisableMove;
  1029. RemoteDirView->DimmHiddenFiles = WinConfiguration->DimmHiddenFiles;
  1030. RemoteDirView->ShowHiddenFiles = WinConfiguration->ShowHiddenFiles;
  1031. RemoteDirView->FormatSizeBytes = WinConfiguration->FormatSizeBytes;
  1032. RemoteDirView->ShowInaccesibleDirectories = WinConfiguration->ShowInaccesibleDirectories;
  1033. RemoteDirView->NaturalOrderNumericalSorting = WinConfiguration->NaturalOrderNumericalSorting;
  1034. UpdateRowSelect(RemoteDirView);
  1035. RemoteDriveView->DDAllowMove = !WinConfiguration->DDDisableMove;
  1036. RemoteDriveView->DimmHiddenDirs = WinConfiguration->DimmHiddenFiles;
  1037. RemoteDriveView->ShowHiddenDirs = WinConfiguration->ShowHiddenFiles;
  1038. RemoteDriveView->ShowInaccesibleDirectories = WinConfiguration->ShowInaccesibleDirectories;
  1039. RemoteDriveView->NaturalOrderNumericalSorting = WinConfiguration->NaturalOrderNumericalSorting;
  1040. UpdateSessionsPageControlHeight();
  1041. SetDockAllowDrag(!WinConfiguration->LockToolbars);
  1042. UpdateToolbarDisplayMode();
  1043. UpdateTransferList();
  1044. if (Terminal != NULL)
  1045. {
  1046. TransferPresetAutoSelect();
  1047. }
  1048. UpdateQueueView();
  1049. UpdateControls();
  1050. // this can be called even before constuctor finishes.
  1051. if (FEditorManager != NULL)
  1052. {
  1053. FEditorManager->ProcessFiles(FileConfigurationChanged, NULL);
  1054. }
  1055. // this can be called even before constuctor finishes.
  1056. if (FLocalEditors != NULL)
  1057. {
  1058. for (int Index = 0; Index < FLocalEditors->Count; Index++)
  1059. {
  1060. ReconfigureEditorForm(static_cast<TForm *>(FLocalEditors->Items[Index]));
  1061. }
  1062. }
  1063. if (ComponentVisible[fcCustomCommandsBand])
  1064. {
  1065. UpdateCustomCommandsToolbar();
  1066. }
  1067. // show only when keeping queue items forever,
  1068. // otherwise, it's enough to have it in the context menu
  1069. QueueDeleteAllDoneQueueToolbarItem->Visible =
  1070. WinConfiguration->QueueKeepDoneItems && (WinConfiguration->QueueKeepDoneItemsFor < 0);
  1071. if (FFileColorsCurrent != WinConfiguration->FileColors)
  1072. {
  1073. TFileColorData::LoadList(WinConfiguration->FileColors, FFileColors);
  1074. FFileColorsCurrent = WinConfiguration->FileColors;
  1075. FileColorsChanged();
  1076. }
  1077. }
  1078. //---------------------------------------------------------------------------
  1079. void __fastcall TCustomScpExplorerForm::DoFileColorsChanged(TCustomDirView * DirView)
  1080. {
  1081. DirView->OnGetItemColor = FFileColors.empty() ? TDirViewGetItemColorEvent(NULL) : TDirViewGetItemColorEvent(&DirViewGetItemColor);
  1082. DirView->Invalidate();
  1083. }
  1084. //---------------------------------------------------------------------------
  1085. void __fastcall TCustomScpExplorerForm::FileColorsChanged()
  1086. {
  1087. DoFileColorsChanged(RemoteDirView);
  1088. }
  1089. //---------------------------------------------------------------------------
  1090. void __fastcall TCustomScpExplorerForm::FileConfigurationChanged(
  1091. const UnicodeString FileName, TEditedFileData * /*Data*/, TObject * Token,
  1092. void * /*Arg*/)
  1093. {
  1094. if (Token != NULL)
  1095. {
  1096. TForm * Editor = dynamic_cast<TForm*>(Token);
  1097. DebugAssert(Editor != NULL);
  1098. ReconfigureEditorForm(Editor);
  1099. }
  1100. }
  1101. //---------------------------------------------------------------------------
  1102. void __fastcall TCustomScpExplorerForm::EnableDDTransferConfirmation(TObject * /*Sender*/)
  1103. {
  1104. WinConfiguration->DDTransferConfirmation = asOn;
  1105. }
  1106. //---------------------------------------------------------------------------
  1107. void __fastcall TCustomScpExplorerForm::CopyParamDialogAfter(
  1108. TTransferDirection /*Direction*/, bool /*Temp*/, const UnicodeString & /*TargetDirectory*/)
  1109. {
  1110. // noop
  1111. }
  1112. //---------------------------------------------------------------------------
  1113. bool TCustomScpExplorerForm::GetDoNotShowCopyDialogDefault(bool DragDrop)
  1114. {
  1115. return DragDrop && (WinConfiguration->DDTransferConfirmation == asAuto);
  1116. }
  1117. //---------------------------------------------------------------------------
  1118. void TCustomScpExplorerForm::HandleDoNotShowCopyDialogAgain(bool DragDrop, bool DoNotShowAgain)
  1119. {
  1120. if (DoNotShowAgain)
  1121. {
  1122. if (DragDrop)
  1123. {
  1124. if (WinConfiguration->DDTransferConfirmation == asAuto)
  1125. {
  1126. PopupTrayBalloon(
  1127. NULL, LoadStr(DD_TRANSFER_CONFIRM_OFF2), qtInformation, NULL, 0, EnableDDTransferConfirmation, NULL);
  1128. }
  1129. WinConfiguration->DDTransferConfirmation = asOff;
  1130. }
  1131. else
  1132. {
  1133. WinConfiguration->ConfirmTransferring = false;
  1134. }
  1135. }
  1136. else
  1137. {
  1138. // User explicitly unchecked "do not show again",
  1139. // so show him the dialog the next time
  1140. if (DragDrop && (WinConfiguration->DDTransferConfirmation == asAuto))
  1141. {
  1142. WinConfiguration->DDTransferConfirmation = asOn;
  1143. }
  1144. }
  1145. }
  1146. //---------------------------------------------------------------------------
  1147. bool __fastcall TCustomScpExplorerForm::CopyParamDialog(
  1148. TTransferDirection Direction, TTransferType Type, bool Temp,
  1149. TStrings * FileList, UnicodeString & TargetDirectory, TGUICopyParamType & CopyParam,
  1150. bool Confirm, bool DragDrop, int Options)
  1151. {
  1152. bool Result = true;
  1153. DebugAssert(HasActiveTerminal());
  1154. // these parameters are known in advance
  1155. int Params =
  1156. FLAGMASK(Type == ttMove, cpDelete);
  1157. bool ToTemp = (Temp && (Direction == tdToLocal));
  1158. if (Result && Confirm && WinConfiguration->ConfirmTransferring)
  1159. {
  1160. bool DisableNewerOnly =
  1161. (!Terminal->IsCapable[fcNewerOnlyUpload] && (Direction == tdToRemote)) ||
  1162. ToTemp;
  1163. Options |=
  1164. FLAGMASK(ToTemp, coTemp) |
  1165. FLAGMASK(!Terminal->IsCapable[fcBackgroundTransfers], coDisableQueue) |
  1166. coDoNotShowAgain;
  1167. TUsableCopyParamAttrs UsableCopyParamAttrs = Terminal->UsableCopyParamAttrs(Params);
  1168. int CopyParamAttrs = (Direction == tdToRemote ?
  1169. UsableCopyParamAttrs.Upload : UsableCopyParamAttrs.Download) |
  1170. FLAGMASK(DisableNewerOnly, cpaNoNewerOnly);
  1171. int OutputOptions = FLAGMASK(GetDoNotShowCopyDialogDefault(DragDrop), cooDoNotShowAgain);
  1172. std::unique_ptr<TSessionData> SessionData(SessionDataForCode());
  1173. FlashOnBackground(); // Particularly when called from ClipboardFakeCreated
  1174. Result = DoCopyDialog(Direction == tdToRemote, Type == ttMove,
  1175. FileList, TargetDirectory, &CopyParam, Options, CopyParamAttrs, SessionData.get(), &OutputOptions, -1);
  1176. if (Result)
  1177. {
  1178. HandleDoNotShowCopyDialogAgain(DragDrop, FLAGSET(OutputOptions, cooDoNotShowAgain));
  1179. CopyParamDialogAfter(Direction, Temp, TargetDirectory);
  1180. }
  1181. }
  1182. if (Result && CopyParam.Queue && !ToTemp && Terminal->IsCapable[fcBackgroundTransfers])
  1183. {
  1184. Configuration->Usage->Inc(L"TransfersOnBackground");
  1185. // these parameter are known only after transfer dialog
  1186. Params |=
  1187. (CopyParam.QueueNoConfirmation ? cpNoConfirmation : 0);
  1188. AddQueueItem(Queue, Direction, FileList, TargetDirectory, CopyParam, Params);
  1189. Result = false;
  1190. ClearTransferSourceSelection(Direction);
  1191. }
  1192. return Result;
  1193. }
  1194. //---------------------------------------------------------------------------
  1195. void TCustomScpExplorerForm::ClearOperationSelection(TOperationSide Side)
  1196. {
  1197. if (FOnFileOperationFinished != NULL)
  1198. {
  1199. FOnFileOperationFinished(Side, UnicodeString(), true);
  1200. }
  1201. else
  1202. {
  1203. if (HasDirView[Side])
  1204. {
  1205. DirView(Side)->SelectAll(smNone);
  1206. }
  1207. }
  1208. }
  1209. //---------------------------------------------------------------------------
  1210. void __fastcall TCustomScpExplorerForm::ClearTransferSourceSelection(TTransferDirection Direction)
  1211. {
  1212. TOperationSide Side = ((Direction == tdToRemote) ? osLocal : osRemote);
  1213. ClearOperationSelection(Side);
  1214. }
  1215. //---------------------------------------------------------------------------
  1216. void __fastcall TCustomScpExplorerForm::AddQueueItem(
  1217. TTerminalQueue * Queue, TTransferDirection Direction, TStrings * FileList,
  1218. const UnicodeString TargetDirectory, TGUICopyParamType & CopyParam,
  1219. int Params)
  1220. {
  1221. DebugAssert(Queue != NULL);
  1222. bool SingleFile = false;
  1223. if (FileList->Count == 1)
  1224. {
  1225. if (Direction == tdToRemote)
  1226. {
  1227. UnicodeString FileName = FileList->Strings[0];
  1228. SingleFile = FileExists(ApiPath(FileName));
  1229. }
  1230. else
  1231. {
  1232. TRemoteFile * File = static_cast<TRemoteFile *>(FileList->Objects[0]);
  1233. SingleFile = !File->IsDirectory;
  1234. }
  1235. }
  1236. TQueueItem * QueueItem;
  1237. if (Direction == tdToRemote)
  1238. {
  1239. CopyParam.IncludeFileMask.SetRoots(FileList, TargetDirectory);
  1240. QueueItem = new TUploadQueueItem(Terminal, FileList, TargetDirectory,
  1241. &CopyParam, Params, SingleFile, CopyParam.QueueParallel);
  1242. }
  1243. else
  1244. {
  1245. CopyParam.IncludeFileMask.SetRoots(TargetDirectory, FileList);
  1246. QueueItem = new TDownloadQueueItem(Terminal, FileList, TargetDirectory,
  1247. &CopyParam, Params, SingleFile, CopyParam.QueueParallel);
  1248. }
  1249. AddQueueItem(Queue, QueueItem, Terminal);
  1250. }
  1251. //---------------------------------------------------------------------------
  1252. void __fastcall TCustomScpExplorerForm::AddQueueItem(TTerminalQueue * Queue, TQueueItem * QueueItem, TManagedTerminal * Terminal)
  1253. {
  1254. if (Queue->IsEmpty)
  1255. {
  1256. Terminal->QueueOperationStart = Now();
  1257. }
  1258. Queue->AddItem(QueueItem);
  1259. }
  1260. //---------------------------------------------------------------------------
  1261. void __fastcall TCustomScpExplorerForm::RestoreFormParams()
  1262. {
  1263. }
  1264. //---------------------------------------------------------------------------
  1265. void __fastcall TCustomScpExplorerForm::InitControls()
  1266. {
  1267. DebugAssert(FDocks != NULL);
  1268. for (int Index = 0; Index < ComponentCount; Index++)
  1269. {
  1270. TTBDock * Dock = dynamic_cast<TTBDock *>(Components[Index]);
  1271. if ((Dock != NULL) && (Dock->Tag == 0))
  1272. {
  1273. FDocks->Add(Dock);
  1274. }
  1275. }
  1276. CollectItemsWithTextDisplayMode(this);
  1277. }
  1278. //---------------------------------------------------------------------------
  1279. void __fastcall TCustomScpExplorerForm::RestoreParams()
  1280. {
  1281. ConfigurationChanged();
  1282. QueuePanel->Height =
  1283. LoadDimension(WinConfiguration->QueueView.Height, WinConfiguration->QueueView.HeightPixelsPerInch, this);
  1284. LoadListViewStr(QueueView3, WinConfiguration->QueueView.Layout);
  1285. QueueDock->Visible = WinConfiguration->QueueView.ToolBar;
  1286. QueueLabel->Visible = WinConfiguration->QueueView.Label;
  1287. QueueFileList->Visible = WinConfiguration->QueueView.FileList;
  1288. QueueFileList->Height =
  1289. LoadDimension(WinConfiguration->QueueView.FileListHeight, WinConfiguration->QueueView.FileListHeightPixelsPerInch, this);
  1290. if (QueueFileList->Visible)
  1291. {
  1292. AdjustQueueLayout();
  1293. }
  1294. UpdateCustomCommandsToolbar();
  1295. }
  1296. //---------------------------------------------------------------------------
  1297. void __fastcall TCustomScpExplorerForm::StoreParams()
  1298. {
  1299. int APixelsPerInch = GetControlPixelsPerInch(this);
  1300. WinConfiguration->QueueView.Height = QueuePanel->Height;
  1301. WinConfiguration->QueueView.HeightPixelsPerInch = APixelsPerInch;
  1302. WinConfiguration->QueueView.Layout = GetListViewStr(QueueView3);
  1303. WinConfiguration->QueueView.ToolBar = QueueDock->Visible;
  1304. WinConfiguration->QueueView.Label = QueueLabel->Visible;
  1305. WinConfiguration->QueueView.FileList = QueueFileList->Visible;
  1306. WinConfiguration->QueueView.FileListHeight = QueueFileList->Height;
  1307. WinConfiguration->QueueView.FileListHeightPixelsPerInch = APixelsPerInch;
  1308. }
  1309. //---------------------------------------------------------------------------
  1310. void __fastcall TCustomScpExplorerForm::CreateParams(TCreateParams & Params)
  1311. {
  1312. if (!FFormRestored)
  1313. {
  1314. FFormRestored = true;
  1315. RestoreFormParams();
  1316. }
  1317. TForm::CreateParams(Params);
  1318. }
  1319. //---------------------------------------------------------------------------
  1320. void __fastcall TCustomScpExplorerForm::SetDockAllowDrag(bool value)
  1321. {
  1322. DebugAssert(FDocks != NULL);
  1323. for (int Index = 0; Index < FDocks->Count; Index++)
  1324. {
  1325. static_cast<TTBDock*>(FDocks->Items[Index])->AllowDrag = value;
  1326. }
  1327. }
  1328. //---------------------------------------------------------------------------
  1329. void __fastcall TCustomScpExplorerForm::LoadToolbarsLayoutStr(UnicodeString LayoutStr, UnicodeString ButtonsStr)
  1330. {
  1331. SetDockAllowDrag(true);
  1332. while (!ButtonsStr.IsEmpty())
  1333. {
  1334. UnicodeString S = CutToChar(ButtonsStr, L';', true);
  1335. UnicodeString Name = CutToChar(S, L'=', true);
  1336. for (int ToolbarIndex = 0; ToolbarIndex < ComponentCount; ToolbarIndex++)
  1337. {
  1338. TTBCustomToolbar * Toolbar = dynamic_cast<TTBCustomToolbar *>(Components[ToolbarIndex]);
  1339. if ((Toolbar != NULL) && SameText(Name, GetToolbarKey(Toolbar->Name)))
  1340. {
  1341. while (!S.IsEmpty())
  1342. {
  1343. UnicodeString S2 = CutToChar(S, L',', true);
  1344. UnicodeString Name = CutToChar(S2, L':', true);
  1345. for (int ItemIndex = 0; ItemIndex < Toolbar->Items->Count; ItemIndex++)
  1346. {
  1347. TTBCustomItem * Item = Toolbar->Items->Items[ItemIndex];
  1348. if (SameText(GetToolbarItemName(Item), Name))
  1349. {
  1350. // forward compatibility
  1351. UnicodeString S3 = CutToChar(S2, L':', true);
  1352. bool Visible = StrToIntDef(S3, 0);
  1353. Item->Visible = Visible;
  1354. }
  1355. }
  1356. }
  1357. }
  1358. }
  1359. }
  1360. ::LoadToolbarsLayoutStr(this, LayoutStr);
  1361. SetDockAllowDrag(!WinConfiguration->LockToolbars);
  1362. }
  1363. //---------------------------------------------------------------------------
  1364. UnicodeString __fastcall TCustomScpExplorerForm::GetToolbarsLayoutStr()
  1365. {
  1366. UnicodeString Result;
  1367. SetDockAllowDrag(true);
  1368. Result = ::GetToolbarsLayoutStr(this);
  1369. SetDockAllowDrag(!WinConfiguration->LockToolbars);
  1370. return Result;
  1371. }
  1372. //---------------------------------------------------------------------------
  1373. UnicodeString __fastcall TCustomScpExplorerForm::GetToolbarItemName(TTBCustomItem * Item)
  1374. {
  1375. UnicodeString Result;
  1376. if (Item->Action != NULL)
  1377. {
  1378. Result = Item->Action->Name;
  1379. Result = RemoveSuffix(Result, L"Action", true);
  1380. }
  1381. else
  1382. {
  1383. Result = Item->Name;
  1384. Result = RemoveSuffix(Result, L"SubmenuItem", true);
  1385. Result = RemoveSuffix(Result, L"Item", true);
  1386. }
  1387. return Result;
  1388. }
  1389. //---------------------------------------------------------------------------
  1390. UnicodeString __fastcall TCustomScpExplorerForm::GetToolbarsButtonsStr()
  1391. {
  1392. UnicodeString Result;
  1393. for (int ToolbarIndex = 0; ToolbarIndex < ComponentCount; ToolbarIndex++)
  1394. {
  1395. UnicodeString ToolbarStr;
  1396. TTBCustomToolbar * Toolbar = dynamic_cast<TTBCustomToolbar *>(Components[ToolbarIndex]);
  1397. if ((Toolbar != NULL) && (Toolbar != QueueToolbar))
  1398. {
  1399. for (int ItemIndex = 0; ItemIndex < Toolbar->Items->Count; ItemIndex++)
  1400. {
  1401. TTBCustomItem * Item = Toolbar->Items->Items[ItemIndex];
  1402. // Currently all buttons are visible by default, so we can safely remember all hidden buttons.
  1403. // Once we introduce any buttons that are hidden by default, we would have to remember their initial state
  1404. // and save the changes here only.
  1405. if (NonVisualDataModule->IsCustomizableToolbarItem(Item) && !Item->Visible)
  1406. {
  1407. UnicodeString Name = GetToolbarItemName(Item);
  1408. DebugAssert(Name.Pos(L"TBX") == 0);
  1409. AddToList(ToolbarStr, FORMAT(L"%s:0", (Name)), L",");
  1410. }
  1411. }
  1412. }
  1413. if (!ToolbarStr.IsEmpty())
  1414. {
  1415. AddToList(Result, FORMAT(L"%s=%s", (GetToolbarKey(Toolbar->Name), ToolbarStr)), L";");
  1416. }
  1417. }
  1418. return Result;
  1419. }
  1420. //---------------------------------------------------------------------------
  1421. void __fastcall TCustomScpExplorerForm::CreateProgressForm(TSynchronizeProgress * SynchronizeProgress)
  1422. {
  1423. DebugAssert(FProgressForm == NULL);
  1424. bool AllowSkip = HasActiveTerminal() ? Terminal->IsCapable[fcSkipTransfer] : false;
  1425. bool AllowMoveToQueue = (FTransferResumeList != NULL) || (FDeletedFiles != NULL);
  1426. FProgressForm = new TProgressForm(Application, AllowMoveToQueue, AllowSkip, SynchronizeProgress);
  1427. FProgressForm->DeleteLocalToRecycleBin =
  1428. (WinConfiguration->DeleteToRecycleBin != FAlternativeDelete);
  1429. FProgressForm->DeleteRemoteToRecycleBin =
  1430. HasActiveTerminal() &&
  1431. (Terminal->SessionData->DeleteToRecycleBin != FAlternativeDelete) &&
  1432. !Terminal->SessionData->RecycleBinPath.IsEmpty();
  1433. // As progress window has delayed show now, we need to lock ourselves, (at least) until then
  1434. LockWindow();
  1435. // Actually, do not know what hides the progress once the operation finishes
  1436. // (it is possibly the SetQueueProgress - and we should not rely on that)
  1437. SetTaskbarListProgressState(TBPF_NORMAL);
  1438. }
  1439. //---------------------------------------------------------------------------
  1440. void __fastcall TCustomScpExplorerForm::DestroyProgressForm()
  1441. {
  1442. SAFE_DESTROY(FProgressForm);
  1443. UnlockWindow();
  1444. SetQueueProgress();
  1445. }
  1446. //---------------------------------------------------------------------------
  1447. void __fastcall TCustomScpExplorerForm::FileOperationProgress(
  1448. TFileOperationProgressType & ProgressData)
  1449. {
  1450. // Download to temporary local directory
  1451. if (FPendingTempSpaceWarn && ProgressData.InProgress && ProgressData.TotalSizeSet)
  1452. {
  1453. bool Continue = true;
  1454. FPendingTempSpaceWarn = false;
  1455. DoWarnLackOfTempSpace(ProgressData.Directory, ProgressData.TotalSize, Continue);
  1456. if (!Continue)
  1457. {
  1458. Abort();
  1459. }
  1460. }
  1461. // operation is being executed and we still didn't show up progress form
  1462. if (ProgressData.InProgress && !FProgressForm)
  1463. {
  1464. CreateProgressForm(NULL);
  1465. DebugAssert(FProgressForm->OnceDoneOperation == odoIdle);
  1466. FProgressForm->OnceDoneOperation = ProgressData.InitialOnceDoneOperation;
  1467. }
  1468. // operation is finished (or terminated), so we hide progress form
  1469. else if (!ProgressData.InProgress && (FProgressForm != NULL) && (FProgressForm->SynchronizeProgress == NULL))
  1470. {
  1471. DestroyProgressForm();
  1472. if (ProgressData.Operation == foCalculateSize)
  1473. {
  1474. // When calculating size before transfer, the abort caused by
  1475. // cancel flag set due to "MoveToQueue" below,
  1476. // is not propagated back to us in ExecuteFileOperation,
  1477. // as it is silently swallowed.
  1478. // So we explicitly (re)throw it here.
  1479. if (FMoveToQueue)
  1480. {
  1481. Abort();
  1482. }
  1483. }
  1484. else
  1485. {
  1486. if ((ProgressData.Cancel == csContinue) &&
  1487. !FAutoOperation)
  1488. {
  1489. OperationComplete(ProgressData.StartTime);
  1490. }
  1491. }
  1492. }
  1493. if (FProgressForm &&
  1494. // Can happen only during synchronization, when ending one of the operations
  1495. ProgressData.InProgress)
  1496. {
  1497. FProgressForm->SetProgressData(ProgressData);
  1498. DebugAssert(ProgressData.InProgress);
  1499. SetTaskbarListProgressValue(&ProgressData);
  1500. if (FProgressForm->Cancel > csContinue)
  1501. {
  1502. ProgressData.SetCancelAtLeast(FProgressForm->Cancel);
  1503. if (FProgressForm->Cancel == csCancelFile)
  1504. {
  1505. FProgressForm->ClearCancel();
  1506. }
  1507. // cancel cancels even the move
  1508. FMoveToQueue = false;
  1509. }
  1510. else if (FProgressForm->MoveToQueue)
  1511. {
  1512. FMoveToQueue = true;
  1513. ProgressData.SetCancelAtLeast(csCancel);
  1514. }
  1515. if ((FTransferResumeList != NULL) &&
  1516. ProgressData.InProgress &&
  1517. ProgressData.IsTransfer() &&
  1518. !ProgressData.FullFileName.IsEmpty())
  1519. {
  1520. if ((FTransferResumeList->Count == 0) ||
  1521. (FTransferResumeList->Strings[FTransferResumeList->Count - 1] != ProgressData.FullFileName))
  1522. {
  1523. // note that we do not recognize directories from files here
  1524. FTransferResumeList->Add(ProgressData.FullFileName);
  1525. }
  1526. }
  1527. }
  1528. }
  1529. //---------------------------------------------------------------------------
  1530. void __fastcall TCustomScpExplorerForm::OperationComplete(
  1531. const TDateTime & StartTime)
  1532. {
  1533. if (GUIConfiguration->BeepOnFinish &&
  1534. (Now() - StartTime > GUIConfiguration->BeepOnFinishAfter))
  1535. {
  1536. UnicodeString BeepSound = GUIConfiguration->BeepSound;
  1537. DWORD Sound;
  1538. if (!ExtractFileExt(BeepSound).IsEmpty())
  1539. {
  1540. Sound = SND_FILENAME;
  1541. }
  1542. else
  1543. {
  1544. Sound = SND_ALIAS;
  1545. }
  1546. PlaySound(BeepSound.c_str(), NULL, Sound | SND_ASYNC);
  1547. }
  1548. }
  1549. //---------------------------------------------------------------------------
  1550. void __fastcall TCustomScpExplorerForm::OperationProgress(
  1551. TFileOperationProgressType & ProgressData)
  1552. {
  1553. // For example, when calling TFileOperationProgressType::Suspend in TTerminal::QueryReopen,
  1554. // we cannot recurse back to TTerminal::Idle as that may detect another error, calling "suspend" again.
  1555. TAutoNestingCounter Counter(FDoNotIdleCurrentTerminal);
  1556. FileOperationProgress(ProgressData);
  1557. }
  1558. //---------------------------------------------------------------------------
  1559. UnicodeString __fastcall TCustomScpExplorerForm::GetProgressTitle()
  1560. {
  1561. UnicodeString Result;
  1562. if (FProgressForm != NULL)
  1563. {
  1564. Result = FProgressForm->ProgressStr();
  1565. }
  1566. return Result;
  1567. }
  1568. //---------------------------------------------------------------------------
  1569. bool __fastcall TCustomScpExplorerForm::PanelOperation(TOperationSide /*Side*/,
  1570. bool DragDrop)
  1571. {
  1572. return (!DragDrop && (DropSourceControl == NULL)) ||
  1573. (DropSourceControl == DirView(osOther));
  1574. }
  1575. //---------------------------------------------------------------------------
  1576. TListItem * TCustomScpExplorerForm::VisualiseOperationFinished(TOperationSide Side, const UnicodeString & FileName, bool Unselect)
  1577. {
  1578. TCustomDirView * ADirView = DirView(Side);
  1579. UnicodeString FileNameOnly = ExtractFileName(FileName, !IsSideLocalBrowser(Side));
  1580. TListItem * Item = ADirView->FindFileItem(FileNameOnly);
  1581. // this can happen when local drive is unplugged in the middle of the operation
  1582. if (Item != NULL)
  1583. {
  1584. if (Unselect)
  1585. {
  1586. Item->Selected = false;
  1587. }
  1588. ADirView->MakeProgressVisible(Item);
  1589. }
  1590. return Item;
  1591. }
  1592. //---------------------------------------------------------------------------
  1593. void __fastcall TCustomScpExplorerForm::DoOperationFinished(
  1594. TFileOperation Operation, TOperationSide Side,
  1595. bool /*Temp*/, const UnicodeString & FileName, bool Success,
  1596. TOnceDoneOperation & OnceDoneOperation)
  1597. {
  1598. if (!FAutoOperation)
  1599. {
  1600. // no selection on "/upload", form serves only as event handler
  1601. // (it is not displayed)
  1602. if (PanelOperation(Side, FDragDropOperation) && Visible &&
  1603. ((Operation == FPrimaryOperation) ||
  1604. ((Operation != foCalculateSize) && (Operation != foGetProperties) && (Operation != foCalculateChecksum))))
  1605. {
  1606. TOperationSide DViewSide;
  1607. if ((FProgressSide != osCurrent) &&
  1608. IsLocalBrowserMode() && (Side == osLocal)) // Only to limit the impact
  1609. {
  1610. // assume the operation is over the focused panel
  1611. DViewSide = FProgressSide;
  1612. }
  1613. else
  1614. {
  1615. DViewSide = Side;
  1616. }
  1617. if (FOnFileOperationFinished != NULL)
  1618. {
  1619. FOnFileOperationFinished(DViewSide, FileName, Success);
  1620. }
  1621. else
  1622. {
  1623. VisualiseOperationFinished(DViewSide, FileName, Success);
  1624. }
  1625. }
  1626. if (TFileOperationProgressType::IsTransferOperation(Operation))
  1627. {
  1628. if (DebugAlwaysTrue(!IsLocalBrowserMode()))
  1629. {
  1630. if (Side == osLocal)
  1631. {
  1632. Configuration->Usage->Inc(L"UploadedFiles");
  1633. }
  1634. else
  1635. {
  1636. Configuration->Usage->Inc(L"DownloadedFiles");
  1637. }
  1638. }
  1639. }
  1640. }
  1641. if (Success && (FSynchronizeController != NULL))
  1642. {
  1643. DebugAssert(!IsSideLocalBrowser(osRemote));
  1644. if (Operation == foCopy)
  1645. {
  1646. DebugAssert(Side == osLocal);
  1647. FSynchronizeController->LogOperation(soUpload, FileName);
  1648. }
  1649. else if (Operation == foDelete)
  1650. {
  1651. DebugAssert(Side == osRemote);
  1652. FSynchronizeController->LogOperation(soDelete, FileName);
  1653. }
  1654. }
  1655. if (FProgressForm)
  1656. {
  1657. OnceDoneOperation = FProgressForm->OnceDoneOperation;
  1658. }
  1659. }
  1660. //---------------------------------------------------------------------------
  1661. void __fastcall TCustomScpExplorerForm::OperationFinished(
  1662. TFileOperation Operation, TOperationSide Side,
  1663. bool Temp, const UnicodeString & FileName, Boolean Success,
  1664. TOnceDoneOperation & OnceDoneOperation)
  1665. {
  1666. TAutoNestingCounter Counter(FDoNotIdleCurrentTerminal);
  1667. DoOperationFinished(Operation, Side, Temp, FileName, Success,
  1668. OnceDoneOperation);
  1669. }
  1670. //---------------------------------------------------------------------------
  1671. bool TCustomScpExplorerForm::IsLocalBrowserMode()
  1672. {
  1673. return false;
  1674. }
  1675. //---------------------------------------------------------------------------
  1676. bool TCustomScpExplorerForm::SupportsLocalBrowser()
  1677. {
  1678. return false;
  1679. }
  1680. //---------------------------------------------------------------------------
  1681. bool TCustomScpExplorerForm::IsSideLocalBrowser(TOperationSide)
  1682. {
  1683. return false;
  1684. }
  1685. //---------------------------------------------------------------------------
  1686. TCustomDirView * TCustomScpExplorerForm::GetCurrentLocalBrowser()
  1687. {
  1688. return NULL;
  1689. }
  1690. //---------------------------------------------------------------------------
  1691. TCustomDirView * __fastcall TCustomScpExplorerForm::DirView(TOperationSide Side)
  1692. {
  1693. DebugAssert(GetSide(Side) == osRemote);
  1694. DebugUsedParam(Side);
  1695. return RemoteDirView;
  1696. }
  1697. //---------------------------------------------------------------------------
  1698. TCustomDriveView * __fastcall TCustomScpExplorerForm::DriveView(TOperationSide Side)
  1699. {
  1700. DebugAssert(GetSide(Side) == osRemote);
  1701. DebugUsedParam(Side);
  1702. return RemoteDriveView;
  1703. }
  1704. //---------------------------------------------------------------------------
  1705. bool __fastcall TCustomScpExplorerForm::DirViewEnabled(TOperationSide Side)
  1706. {
  1707. DebugAssert(GetSide(Side) == osRemote);
  1708. DebugUsedParam(Side);
  1709. return HasActiveTerminal();
  1710. }
  1711. //---------------------------------------------------------------------------
  1712. bool __fastcall TCustomScpExplorerForm::GetEnableFocusedOperation(
  1713. TOperationSide Side, int FilesOnly)
  1714. {
  1715. return DirView(Side)->AnyFileSelected(true, (FilesOnly != 0), true);
  1716. }
  1717. //---------------------------------------------------------------------------
  1718. bool __fastcall TCustomScpExplorerForm::GetEnableSelectedOperation(
  1719. TOperationSide Side, int FilesOnly)
  1720. {
  1721. return DirView(Side)->AnyFileSelected(false, (FilesOnly != 0), true);
  1722. }
  1723. //---------------------------------------------------------------------------
  1724. struct THistoryItemData
  1725. {
  1726. short int Side;
  1727. short int Index;
  1728. };
  1729. //---------------------------------------------------------------------------
  1730. void __fastcall TCustomScpExplorerForm::HistoryGo(TOperationSide Side, int Index)
  1731. {
  1732. DirView(Side)->HistoryGo(Index);
  1733. }
  1734. //---------------------------------------------------------------------------
  1735. void __fastcall TCustomScpExplorerForm::HistoryItemClick(System::TObject* Sender)
  1736. {
  1737. TTBCustomItem * Item = dynamic_cast<TTBCustomItem *>(Sender);
  1738. THistoryItemData Data = *reinterpret_cast<THistoryItemData*>(&(Item->Tag));
  1739. HistoryGo((TOperationSide)Data.Side, Data.Index);
  1740. }
  1741. //---------------------------------------------------------------------------
  1742. void __fastcall TCustomScpExplorerForm::UpdateHistoryMenu(TOperationSide Side,
  1743. bool Back)
  1744. {
  1745. if (FHistoryMenu[Side == osLocal][Back] != NULL)
  1746. {
  1747. TCustomDirView * DView = DirView(Side);
  1748. TTBXPopupMenu * Menu = FHistoryMenu[Side == osLocal][Back];
  1749. int ICount = Back ? DView->BackCount : DView->ForwardCount;
  1750. if (ICount > 10)
  1751. {
  1752. ICount = 10;
  1753. }
  1754. Menu->Items->Clear();
  1755. THistoryItemData Data;
  1756. Data.Side = Side;
  1757. for (short int i = 1; i <= ICount; i++)
  1758. {
  1759. TTBCustomItem * Item = new TTBXItem(Menu);
  1760. Data.Index = static_cast<short int>(i * (Back ? -1 : 1));
  1761. Item->Caption = MinimizeName(DView->HistoryPath[Data.Index], 50, !IsSideLocalBrowser(Side));
  1762. Item->Hint = DView->HistoryPath[Data.Index];
  1763. DebugAssert(sizeof(int) == sizeof(THistoryItemData));
  1764. Item->Tag = *reinterpret_cast<int*>(&Data);
  1765. Item->OnClick = HistoryItemClick;
  1766. Menu->Items->Add(Item);
  1767. }
  1768. }
  1769. }
  1770. //---------------------------------------------------------------------------
  1771. TTBXPopupMenu * __fastcall TCustomScpExplorerForm::HistoryMenu(
  1772. TOperationSide Side, bool Back)
  1773. {
  1774. if (FHistoryMenu[Side == osLocal][Back] == NULL)
  1775. {
  1776. // workaround
  1777. // In Pascal the size of TTBXPopupMenu is 132, in C++ 136,
  1778. // operator new allocates memory in Pascal code, but calls inline
  1779. // contructor in C++, leading in problems, the function does
  1780. // both in Pascal code
  1781. FHistoryMenu[Side == osLocal][Back] = CreateTBXPopupMenu(this);
  1782. UpdateHistoryMenu(Side, Back);
  1783. }
  1784. return FHistoryMenu[Side == osLocal][Back];
  1785. }
  1786. //---------------------------------------------------------------------------
  1787. void __fastcall TCustomScpExplorerForm::DirViewHistoryChange(
  1788. TCustomDirView *Sender)
  1789. {
  1790. TOperationSide Side = (Sender == DirView(osOther) ? osOther : osLocal);
  1791. UpdateHistoryMenu(Side, true);
  1792. UpdateHistoryMenu(Side, false);
  1793. }
  1794. //---------------------------------------------------------------------------
  1795. bool __fastcall TCustomScpExplorerForm::CustomCommandRemoteAllowed()
  1796. {
  1797. // remote custom commands can be executed only if the server supports shell commands
  1798. // or have secondary shell
  1799. return HasActiveTerminal() && (Terminal->IsCapable[fcSecondaryShell] || Terminal->IsCapable[fcShellAnyCommand]);
  1800. }
  1801. //---------------------------------------------------------------------------
  1802. int __fastcall TCustomScpExplorerForm::CustomCommandState(
  1803. const TCustomCommandType & Command, bool OnFocused, TCustomCommandListType ListType)
  1804. {
  1805. // -1 = hidden, 0 = disabled, 1 = enabled
  1806. int Result;
  1807. TFileCustomCommand RemoteCustomCommand;
  1808. TLocalCustomCommand LocalCustomCommand;
  1809. TFileCustomCommand * NonInteractiveCustomCommand =
  1810. FLAGCLEAR(Command.Params, ccLocal) ? &RemoteCustomCommand : &LocalCustomCommand;
  1811. TInteractiveCustomCommand InteractiveCustomCommand(NonInteractiveCustomCommand);
  1812. UnicodeString Cmd = InteractiveCustomCommand.Complete(Command.Command, false);
  1813. if (FLAGCLEAR(Command.Params, ccLocal))
  1814. {
  1815. int AllowedState = CustomCommandRemoteAllowed() ? 1 : 0;
  1816. // custom command that does not operate with files can be executed anytime ...
  1817. if (!NonInteractiveCustomCommand->IsFileCommand(Cmd))
  1818. {
  1819. // ... but do not show such command in remote file menu
  1820. if ((ListType == ccltAll) || (ListType == ccltNonFile))
  1821. {
  1822. Result = AllowedState;
  1823. }
  1824. else
  1825. {
  1826. Result = -1;
  1827. }
  1828. }
  1829. else
  1830. {
  1831. if ((ListType == ccltAll) || ((ListType == ccltFile) && !IsSideLocalBrowser(FCurrentSide)))
  1832. {
  1833. Result = (DirView(FCurrentSide)->AnyFileSelected(OnFocused, false, true)) ? AllowedState : 0;
  1834. }
  1835. else
  1836. {
  1837. Result = -1;
  1838. }
  1839. }
  1840. }
  1841. else
  1842. {
  1843. // custom command that does not operate with files can be executed anytime
  1844. if (!NonInteractiveCustomCommand->IsFileCommand(Cmd))
  1845. {
  1846. Result = ((ListType == ccltAll) || (ListType == ccltNonFile)) ? 1 : -1;
  1847. }
  1848. else if (LocalCustomCommand.HasLocalFileName(Cmd))
  1849. {
  1850. if ((ListType == ccltAll) || (ListType == ccltFile))
  1851. {
  1852. // special case is "diff"-style command that can be executed over any side,
  1853. // if we have both sides
  1854. Result =
  1855. // Cannot have focus on both panels, so we have to call AnyFileSelected
  1856. // directly (instead of EnableSelectedOperation) to pass
  1857. // false to FocusedFileOnlyWhenFocused when panel is inactive.
  1858. ((HasDirView[osLocal] && !IsSideLocalBrowser(osRemote) &&
  1859. DirView(osLocal)->AnyFileSelected(false, false, (FCurrentSide == osLocal))) &&
  1860. DirView(osRemote)->AnyFileSelected(false, false, (FCurrentSide == osRemote))) ? 1 : 0;
  1861. }
  1862. else if (ListType == ccltBoth)
  1863. {
  1864. DebugAssert(!IsLocalBrowserMode());
  1865. Result = 1;
  1866. }
  1867. else
  1868. {
  1869. Result = -1;
  1870. }
  1871. }
  1872. else
  1873. {
  1874. if ((ListType == ccltAll) ||
  1875. ((ListType == ccltFile) && (!IsSideLocalBrowser(FCurrentSide) || FLAGCLEAR(Command.Params, ccRemoteFiles))))
  1876. {
  1877. Result = DirView(FCurrentSide)->AnyFileSelected(OnFocused, false, true) ? 1 : 0;
  1878. }
  1879. else
  1880. {
  1881. Result = -1;
  1882. }
  1883. }
  1884. if ((Result > 0) &&
  1885. LocalCustomCommand.IsSessionCommand(Cmd) &&
  1886. !HasActiveTerminal())
  1887. {
  1888. Result = 0;
  1889. }
  1890. }
  1891. return Result;
  1892. }
  1893. //---------------------------------------------------------------------------
  1894. void __fastcall TCustomScpExplorerForm::RemoteCustomCommand(
  1895. TStrings * FileList, const TCustomCommandType & ACommand,
  1896. const TCustomCommandData & Data, const UnicodeString & CommandCommand)
  1897. {
  1898. if (EnsureCommandSessionFallback(fcShellAnyCommand))
  1899. {
  1900. TRemoteCustomCommand RemoteCustomCommand(Data, Terminal->CurrentDirectory);
  1901. TWinInteractiveCustomCommand InteractiveCustomCommand(
  1902. &RemoteCustomCommand, ACommand.Name, ACommand.HomePage);
  1903. UnicodeString Command = InteractiveCustomCommand.Complete(CommandCommand, false);
  1904. Configuration->Usage->Inc(L"RemoteCustomCommandRuns2");
  1905. bool Capture =
  1906. FLAGSET(ACommand.Params, ccShowResults) ||
  1907. FLAGSET(ACommand.Params, ccCopyResults) ||
  1908. FLAGSET(ACommand.Params, ccShowResultsInMsgBox);
  1909. TCaptureOutputEvent OutputEvent = NULL;
  1910. DebugAssert(FCapturedLog == NULL);
  1911. if (Capture)
  1912. {
  1913. FCapturedLog = new TStringList();
  1914. OutputEvent = TerminalCaptureLog;
  1915. }
  1916. try
  1917. {
  1918. if (!RemoteCustomCommand.IsFileCommand(Command))
  1919. {
  1920. Terminal->AnyCommand(RemoteCustomCommand.Complete(Command, true),
  1921. OutputEvent);
  1922. }
  1923. else
  1924. {
  1925. Terminal->CustomCommandOnFiles(Command, ACommand.Params, FileList, OutputEvent);
  1926. }
  1927. if ((FCapturedLog != NULL) && (FCapturedLog->Count > 0))
  1928. {
  1929. if (FLAGSET(ACommand.Params, ccCopyResults))
  1930. {
  1931. CopyToClipboard(FCapturedLog);
  1932. }
  1933. if (FLAGSET(ACommand.Params, ccShowResults))
  1934. {
  1935. DoConsoleDialog(Terminal, L"", FCapturedLog);
  1936. }
  1937. else if (FLAGSET(ACommand.Params, ccShowResultsInMsgBox))
  1938. {
  1939. MessageDialog(FCapturedLog->Text, qtInformation, qaOK);
  1940. }
  1941. }
  1942. }
  1943. __finally
  1944. {
  1945. SAFE_DESTROY(FCapturedLog);
  1946. }
  1947. }
  1948. }
  1949. //---------------------------------------------------------------------------
  1950. void __fastcall TCustomScpExplorerForm::LocalCustomCommandPure(
  1951. TStrings * FileList, const TCustomCommandType & ACommand, const UnicodeString & Command, TStrings * ALocalFileList,
  1952. const TCustomCommandData & Data, bool LocalFileCommand, bool FileListCommand, UnicodeString * POutput)
  1953. {
  1954. DebugAssert(!IsLocalBrowserMode());
  1955. TStrings * LocalFileList = NULL;
  1956. TStrings * RemoteFileList = NULL;
  1957. TStrings * RemoteFileListFull = NULL;
  1958. try
  1959. {
  1960. if (LocalFileCommand)
  1961. {
  1962. if (ALocalFileList == NULL)
  1963. {
  1964. DebugAssert(HasDirView[osLocal]);
  1965. // Cannot have focus on both panels, so we have to call AnyFileSelected
  1966. // directly (instead of EnableSelectedOperation) to pass
  1967. // false to FocusedFileOnlyWhenFocused
  1968. DebugAssert(DirView(osLocal)->AnyFileSelected(false, false, false));
  1969. LocalFileList = DirView(osLocal)->CreateFileList(false, true, NULL);
  1970. }
  1971. else
  1972. {
  1973. LocalFileList = ALocalFileList;
  1974. }
  1975. if (FileListCommand)
  1976. {
  1977. if (LocalFileList->Count != 1)
  1978. {
  1979. throw Exception(LoadStr(CUSTOM_COMMAND_SELECTED_UNMATCH1));
  1980. }
  1981. }
  1982. else
  1983. {
  1984. if ((LocalFileList->Count != 1) &&
  1985. (FileList->Count != 1) &&
  1986. (LocalFileList->Count != FileList->Count))
  1987. {
  1988. throw Exception(LoadStr(CUSTOM_COMMAND_SELECTED_UNMATCH));
  1989. }
  1990. }
  1991. }
  1992. UnicodeString RootTempDir;
  1993. UnicodeString TempDir;
  1994. bool RemoteFiles = FLAGSET(ACommand.Params, ccRemoteFiles);
  1995. if (!RemoteFiles)
  1996. {
  1997. TemporarilyDownloadFiles(FileList, false, RootTempDir, TempDir, false, true);
  1998. }
  1999. try
  2000. {
  2001. TDateTimes RemoteFileTimes;
  2002. if (RemoteFiles)
  2003. {
  2004. RemoteFileList = FileList;
  2005. }
  2006. else
  2007. {
  2008. RemoteFileList = new TStringList();
  2009. TMakeLocalFileListParams MakeFileListParam;
  2010. MakeFileListParam.FileList = RemoteFileList;
  2011. MakeFileListParam.FileTimes = &RemoteFileTimes;
  2012. MakeFileListParam.IncludeDirs = FLAGSET(ACommand.Params, ccApplyToDirectories);
  2013. MakeFileListParam.Recursive =
  2014. FLAGSET(ACommand.Params, ccRecursive) && !FileListCommand;
  2015. ProcessLocalDirectory(TempDir, Terminal->MakeLocalFileList, &MakeFileListParam);
  2016. if (!MakeFileListParam.Recursive)
  2017. {
  2018. RemoteFileTimes.clear();
  2019. RemoteFileListFull = new TStringList();
  2020. MakeFileListParam.FileList = RemoteFileListFull;
  2021. MakeFileListParam.Recursive = true;
  2022. MakeFileListParam.IncludeDirs = false;
  2023. ProcessLocalDirectory(TempDir, Terminal->MakeLocalFileList, &MakeFileListParam);
  2024. }
  2025. else
  2026. {
  2027. RemoteFileListFull = RemoteFileList;
  2028. }
  2029. }
  2030. bool NonBlocking = FileListCommand && RemoteFiles && !POutput;
  2031. TFileOperationProgressType Progress(&OperationProgress, &OperationFinished);
  2032. if (!NonBlocking)
  2033. {
  2034. int Count;
  2035. if (FileListCommand)
  2036. {
  2037. Count = 1;
  2038. }
  2039. else if (LocalFileCommand)
  2040. {
  2041. Count = std::max(LocalFileList->Count, RemoteFileList->Count);
  2042. }
  2043. else
  2044. {
  2045. Count = RemoteFileList->Count;
  2046. }
  2047. Progress.Start(foCustomCommand, osRemote, Count);
  2048. DebugAssert(FProgressForm != NULL);
  2049. FProgressForm->ReadOnly = true;
  2050. }
  2051. try
  2052. {
  2053. if (FileListCommand)
  2054. {
  2055. UnicodeString LocalFile;
  2056. // MakeFileList does not delimit filenames
  2057. UnicodeString FileList = MakeFileList(RemoteFileList);
  2058. if (LocalFileCommand)
  2059. {
  2060. DebugAssert(LocalFileList->Count == 1);
  2061. LocalFile = LocalFileList->Strings[0];
  2062. }
  2063. TLocalCustomCommand CustomCommand(Data,
  2064. Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(), L"", LocalFile, FileList);
  2065. UnicodeString ShellCommand = CustomCommand.Complete(Command, true);
  2066. if (NonBlocking)
  2067. {
  2068. DebugAssert(!POutput);
  2069. ExecuteShellChecked(ShellCommand);
  2070. }
  2071. else
  2072. {
  2073. ExecuteProcessCheckedAndWait(ShellCommand, HelpKeyword, POutput);
  2074. }
  2075. }
  2076. else if (LocalFileCommand)
  2077. {
  2078. if (LocalFileList->Count == 1)
  2079. {
  2080. UnicodeString LocalFile = LocalFileList->Strings[0];
  2081. for (int Index = 0; Index < RemoteFileList->Count; Index++)
  2082. {
  2083. UnicodeString FileName = RemoteFileList->Strings[Index];
  2084. UnicodeString FileNameForProgress = RemoteFiles ? FileName : ExtractFileName(FileName);
  2085. Progress.SetFile(FileNameForProgress);
  2086. TLocalCustomCommand CustomCommand(Data,
  2087. Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(), FileName, LocalFile, L"");
  2088. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  2089. TOnceDoneOperation OnceDoneOperation;
  2090. // Do not unselect anything, as it won't work with two panels anyway
  2091. Progress.Finish(UnicodeString(), true, OnceDoneOperation);
  2092. }
  2093. }
  2094. else if (RemoteFileList->Count == 1)
  2095. {
  2096. UnicodeString FileName = RemoteFileList->Strings[0];
  2097. for (int Index = 0; Index < LocalFileList->Count; Index++)
  2098. {
  2099. UnicodeString LocalFileName = LocalFileList->Strings[Index];
  2100. UnicodeString LocalFileNameOnly = ExtractFileName(LocalFileName);
  2101. Progress.SetFile(LocalFileNameOnly);
  2102. TLocalCustomCommand CustomCommand(
  2103. Data, Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(),
  2104. FileName, LocalFileName, L"");
  2105. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  2106. TOnceDoneOperation OnceDoneOperation;
  2107. // Do not unselect anything, as it won't work with two panels anyway
  2108. Progress.Finish(UnicodeString(), true, OnceDoneOperation);
  2109. }
  2110. }
  2111. else
  2112. {
  2113. if (LocalFileList->Count != RemoteFileList->Count)
  2114. {
  2115. throw Exception(LoadStr(CUSTOM_COMMAND_PAIRS_DOWNLOAD_FAILED));
  2116. }
  2117. for (int Index = 0; Index < LocalFileList->Count; Index++)
  2118. {
  2119. UnicodeString FileName = RemoteFileList->Strings[Index];
  2120. UnicodeString LocalFileName = LocalFileList->Strings[Index];
  2121. UnicodeString LocalFileNameOnly = ExtractFileName(LocalFileName);
  2122. Progress.SetFile(LocalFileNameOnly); // sic
  2123. TLocalCustomCommand CustomCommand(
  2124. Data, Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(),
  2125. FileName, LocalFileName, L"");
  2126. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  2127. TOnceDoneOperation OnceDoneOperation;
  2128. // Do not unselect anything, as it won't work with two panels anyway
  2129. Progress.Finish(UnicodeString(), true, OnceDoneOperation);
  2130. }
  2131. }
  2132. }
  2133. else
  2134. {
  2135. for (int Index = 0; Index < RemoteFileList->Count; Index++)
  2136. {
  2137. UnicodeString FileName = RemoteFileList->Strings[Index];
  2138. UnicodeString FileNameForProgress = RemoteFiles ? FileName : ExtractFileName(FileName);
  2139. Progress.SetFile(FileNameForProgress);
  2140. TLocalCustomCommand CustomCommand(Data,
  2141. Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(),
  2142. FileName, L"", L"");
  2143. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  2144. TOnceDoneOperation OnceDoneOperation;
  2145. Progress.Finish(FileNameForProgress, true, OnceDoneOperation);
  2146. }
  2147. }
  2148. }
  2149. __finally
  2150. {
  2151. if (!NonBlocking)
  2152. {
  2153. Progress.Stop();
  2154. }
  2155. }
  2156. DebugAssert(!FAutoOperation);
  2157. if (!RemoteFiles)
  2158. {
  2159. TempDir = IncludeTrailingBackslash(TempDir);
  2160. for (int Index = 0; Index < RemoteFileListFull->Count; Index++)
  2161. {
  2162. UnicodeString FileName = RemoteFileListFull->Strings[Index];
  2163. if (DebugAlwaysTrue(SameText(TempDir, FileName.SubString(1, TempDir.Length()))) &&
  2164. // Skip directories, process only nested files.
  2165. // The check is redundant as FileAge fails for directories anyway.
  2166. !DirectoryExists(FileName))
  2167. {
  2168. UnicodeString RemoteDir =
  2169. UnixExtractFileDir(
  2170. UnixIncludeTrailingBackslash(Terminal->CurrentDirectory) +
  2171. ToUnixPath(FileName.SubString(TempDir.Length() + 1, FileName.Length() - TempDir.Length())));
  2172. TDateTime NewTime;
  2173. if (FileAge(FileName, NewTime) &&
  2174. (NewTime != RemoteFileTimes[Index]))
  2175. {
  2176. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  2177. TemporaryFileCopyParam(CopyParam);
  2178. CopyParam.FileMask = L"";
  2179. FAutoOperation = true;
  2180. std::unique_ptr<TStrings> TemporaryFilesList(new TStringList());
  2181. TemporaryFilesList->Add(FileName);
  2182. Terminal->CopyToRemote(TemporaryFilesList.get(), RemoteDir, &CopyParam, cpTemporary, NULL);
  2183. }
  2184. }
  2185. }
  2186. }
  2187. }
  2188. __finally
  2189. {
  2190. FAutoOperation = false;
  2191. if (!RootTempDir.IsEmpty() && DebugAlwaysTrue(!RemoteFiles))
  2192. {
  2193. RecursiveDeleteFile(ExcludeTrailingBackslash(RootTempDir), false);
  2194. }
  2195. }
  2196. }
  2197. __finally
  2198. {
  2199. if (RemoteFileList != FileList)
  2200. {
  2201. delete RemoteFileList;
  2202. }
  2203. if (LocalFileList != ALocalFileList)
  2204. {
  2205. delete LocalFileList;
  2206. }
  2207. if (RemoteFileListFull != RemoteFileList)
  2208. {
  2209. delete RemoteFileListFull;
  2210. }
  2211. }
  2212. }
  2213. //---------------------------------------------------------------------------
  2214. void __fastcall TCustomScpExplorerForm::LocalCustomCommandWithLocalFiles(
  2215. const TCustomCommandType & ACommand, const UnicodeString & Command, const TCustomCommandData & Data,
  2216. bool FileListCommand, UnicodeString * POutput)
  2217. {
  2218. std::unique_ptr<TStrings> SelectedFileList(DirView(osCurrent)->CreateFileList(false, true, NULL));
  2219. std::unique_ptr<TStrings> LocalFileList(new TStringList());
  2220. for (int Index = 0; Index < SelectedFileList->Count; Index++)
  2221. {
  2222. UnicodeString FileName = SelectedFileList->Strings[Index];
  2223. if (DirectoryExists(FileName))
  2224. {
  2225. if (FLAGSET(ACommand.Params, ccApplyToDirectories))
  2226. {
  2227. LocalFileList->Add(FileName);
  2228. }
  2229. if (FLAGSET(ACommand.Params, ccRecursive))
  2230. {
  2231. TMakeLocalFileListParams MakeFileListParam;
  2232. MakeFileListParam.FileList = LocalFileList.get();
  2233. MakeFileListParam.FileTimes = NULL;
  2234. MakeFileListParam.IncludeDirs = FLAGSET(ACommand.Params, ccApplyToDirectories);
  2235. MakeFileListParam.Recursive = true;
  2236. ProcessLocalDirectory(FileName, Terminal->MakeLocalFileList, &MakeFileListParam);
  2237. }
  2238. }
  2239. else
  2240. {
  2241. LocalFileList->Add(FileName);
  2242. }
  2243. }
  2244. UnicodeString RemotePath;
  2245. if (Terminal != NULL)
  2246. {
  2247. RemotePath = Terminal->CurrentDirectory;
  2248. }
  2249. if (FileListCommand)
  2250. {
  2251. UnicodeString FileList = MakeFileList(LocalFileList.get());
  2252. TLocalCustomCommand CustomCommand(
  2253. Data, RemotePath, DefaultDownloadTargetDirectory(),
  2254. L"", L"", FileList);
  2255. ExecuteProcessChecked(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  2256. }
  2257. else
  2258. {
  2259. TFileOperationProgressType Progress(&OperationProgress, &OperationFinished);
  2260. Progress.Start(foCustomCommand, osLocal, LocalFileList->Count);
  2261. DebugAssert(FProgressForm != NULL);
  2262. FProgressForm->ReadOnly = true;
  2263. try
  2264. {
  2265. for (int Index = 0; Index < LocalFileList->Count; Index++)
  2266. {
  2267. UnicodeString FileName = LocalFileList->Strings[Index];
  2268. Progress.SetFile(FileName);
  2269. TLocalCustomCommand CustomCommand(
  2270. Data, RemotePath, DefaultDownloadTargetDirectory(),
  2271. FileName, L"", L"");
  2272. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  2273. TOnceDoneOperation OnceDoneOperation;
  2274. Progress.Finish(FileName, true, OnceDoneOperation);
  2275. }
  2276. }
  2277. __finally
  2278. {
  2279. Progress.Stop();
  2280. }
  2281. }
  2282. }
  2283. //---------------------------------------------------------------------------
  2284. void __fastcall TCustomScpExplorerForm::LocalCustomCommand(TStrings * FileList,
  2285. const TCustomCommandType & ACommand, TStrings * ALocalFileList,
  2286. const TCustomCommandData & Data, const UnicodeString & CommandCommand)
  2287. {
  2288. UnicodeString RemotePath = (Terminal != NULL) ? Terminal->CurrentDirectory : UnicodeString();
  2289. TLocalCustomCommand LocalCustomCommand(Data, RemotePath, DefaultDownloadTargetDirectory());
  2290. TWinInteractiveCustomCommand InteractiveCustomCommand(
  2291. &LocalCustomCommand, ACommand.Name, ACommand.HomePage);
  2292. UnicodeString Command = InteractiveCustomCommand.Complete(CommandCommand, false);
  2293. bool FileListCommand = LocalCustomCommand.IsFileListCommand(Command);
  2294. bool LocalFileCommand = LocalCustomCommand.HasLocalFileName(Command);
  2295. Configuration->Usage->Inc(L"LocalCustomCommandRuns2");
  2296. std::unique_ptr<UnicodeString> POutput;
  2297. if (FLAGSET(ACommand.Params, ccShowResultsInMsgBox) ||
  2298. FLAGSET(ACommand.Params, ccCopyResults))
  2299. {
  2300. POutput.reset(new UnicodeString());
  2301. }
  2302. TValueRestorer<TOperationSide> ProgressSideRestorer(FProgressSide);
  2303. FProgressSide = FCurrentSide;
  2304. if (!LocalCustomCommand.IsFileCommand(Command))
  2305. {
  2306. ExecuteProcessChecked(LocalCustomCommand.Complete(Command, true), HelpKeyword, POutput.get());
  2307. }
  2308. // remote files?
  2309. else if (!IsSideLocalBrowser(FCurrentSide) || LocalFileCommand)
  2310. {
  2311. LocalCustomCommandPure(FileList, ACommand, Command, ALocalFileList, Data, LocalFileCommand, FileListCommand, POutput.get());
  2312. }
  2313. // local files
  2314. else
  2315. {
  2316. LocalCustomCommandWithLocalFiles(ACommand, Command, Data, FileListCommand, POutput.get());
  2317. }
  2318. if (POutput.get() != NULL)
  2319. {
  2320. // If the output is single-line, we do not want the trailing CRLF, as the CopyToClipboard(TStrings *) does
  2321. int P = POutput->Pos(sLineBreak);
  2322. if (P == POutput->Length() - static_cast<int>(strlen(sLineBreak)) + 1)
  2323. {
  2324. POutput->SetLength(P - 1);
  2325. }
  2326. // Copy even empty output to clipboard
  2327. if (FLAGSET(ACommand.Params, ccCopyResults))
  2328. {
  2329. CopyToClipboard(*POutput);
  2330. }
  2331. // But do not show an empty message box.
  2332. // This way the ShowResultsInMsgBox can be used to suppress a console window of commands with no output
  2333. if (FLAGSET(ACommand.Params, ccShowResultsInMsgBox) &&
  2334. !POutput->IsEmpty())
  2335. {
  2336. TClipboardHandler ClipboardHandler;
  2337. ClipboardHandler.Text = *POutput;
  2338. TMessageParams Params;
  2339. TQueryButtonAlias Aliases[1];
  2340. Aliases[0].Button = qaRetry;
  2341. Aliases[0].Alias = LoadStr(EDIT_COPY);
  2342. Aliases[0].OnSubmit = &ClipboardHandler.Copy;
  2343. Params.Aliases = Aliases;
  2344. Params.AliasesCount = LENOF(Aliases);
  2345. MessageDialog(*POutput, qtInformation, qaOK | qaRetry, HelpKeyword, &Params);
  2346. }
  2347. }
  2348. }
  2349. //---------------------------------------------------------------------------
  2350. void __fastcall TCustomScpExplorerForm::CustomCommand(TStrings * FileList,
  2351. const TCustomCommandType & ACommand, TStrings * ALocalFileList)
  2352. {
  2353. TCustomCommandData Data;
  2354. UnicodeString Site;
  2355. UnicodeString RemotePath;
  2356. if (HasActiveTerminal())
  2357. {
  2358. std::unique_ptr<TSessionData> SessionData(SessionDataForCode());
  2359. Data = TCustomCommandData(SessionData.get());
  2360. Site = Terminal->SessionData->SessionKey;
  2361. RemotePath = Terminal->CurrentDirectory;
  2362. }
  2363. UnicodeString HelpKeyword = ACommand.HomePage;
  2364. std::unique_ptr<TStrings> CustomCommandOptions(CloneStrings(WinConfiguration->CustomCommandOptions));
  2365. if (ACommand.AnyOptionWithFlag(TCustomCommandType::ofRun))
  2366. {
  2367. std::unique_ptr<TCustomCommand> CustomCommandForOptions;
  2368. if (FLAGCLEAR(ACommand.Params, ccLocal))
  2369. {
  2370. CustomCommandForOptions.reset(new TRemoteCustomCommand(Data, RemotePath));
  2371. }
  2372. else
  2373. {
  2374. CustomCommandForOptions.reset(new TLocalCustomCommand(Data, RemotePath, DefaultDownloadTargetDirectory()));
  2375. }
  2376. if (!DoCustomCommandOptionsDialog(
  2377. &ACommand, CustomCommandOptions.get(), NULL, TCustomCommandType::ofRun, CustomCommandForOptions.get(), Site, NULL))
  2378. {
  2379. Abort();
  2380. }
  2381. }
  2382. UnicodeString CommandCommand = ACommand.GetCommandWithExpandedOptions(CustomCommandOptions.get(), Site);
  2383. if (FLAGCLEAR(ACommand.Params, ccLocal))
  2384. {
  2385. RemoteCustomCommand(FileList, ACommand, Data, CommandCommand);
  2386. }
  2387. else
  2388. {
  2389. LocalCustomCommand(FileList, ACommand, ALocalFileList, Data, CommandCommand);
  2390. }
  2391. }
  2392. //---------------------------------------------------------------------------
  2393. void __fastcall TCustomScpExplorerForm::BothCustomCommand(
  2394. const TCustomCommandType & Command)
  2395. {
  2396. DebugAssert(FCustomCommandLocalFileList != NULL);
  2397. DebugAssert(FCustomCommandRemoteFileList != NULL);
  2398. DebugAssert(FCustomCommandLocalFileList->Count == FCustomCommandRemoteFileList->Count);
  2399. TStrings * LocalFileList = new TStringList();
  2400. TStrings * RemoteFileList = new TStringList();
  2401. try
  2402. {
  2403. for (int Index = 0; Index < FCustomCommandLocalFileList->Count; Index++)
  2404. {
  2405. LocalFileList->Clear();
  2406. LocalFileList->AddObject(
  2407. FCustomCommandLocalFileList->Strings[Index],
  2408. FCustomCommandLocalFileList->Objects[Index]);
  2409. RemoteFileList->Clear();
  2410. RemoteFileList->AddObject(
  2411. FCustomCommandRemoteFileList->Strings[Index],
  2412. FCustomCommandRemoteFileList->Objects[Index]);
  2413. CustomCommand(RemoteFileList, Command, LocalFileList);
  2414. }
  2415. }
  2416. __finally
  2417. {
  2418. delete LocalFileList;
  2419. delete RemoteFileList;
  2420. }
  2421. }
  2422. //---------------------------------------------------------------------------
  2423. void __fastcall TCustomScpExplorerForm::CustomCommandMenu(
  2424. TAction * Action, TStrings * LocalFileList, TStrings * RemoteFileList)
  2425. {
  2426. delete FCustomCommandLocalFileList;
  2427. delete FCustomCommandRemoteFileList;
  2428. // takeover ownership,
  2429. // the lists must survive the MenuPopup as OnClick occurs only after it exits
  2430. FCustomCommandLocalFileList = LocalFileList;
  2431. FCustomCommandRemoteFileList = RemoteFileList;
  2432. TButton * Button = dynamic_cast<TButton *>(Action->ActionComponent);
  2433. if (Button != NULL)
  2434. {
  2435. FCustomCommandMenu->Items->Clear();
  2436. NonVisualDataModule->CreateCustomCommandsMenu(FCustomCommandMenu->Items, false, false, ccltBoth, NULL);
  2437. MenuPopup(FCustomCommandMenu, Button);
  2438. }
  2439. else
  2440. {
  2441. NonVisualDataModule->CreateCustomCommandsMenu(Action, false, ccltBoth);
  2442. }
  2443. }
  2444. //---------------------------------------------------------------------------
  2445. void __fastcall TCustomScpExplorerForm::TerminalCaptureLog(
  2446. const UnicodeString & AddedLine, TCaptureOutputType OutputType)
  2447. {
  2448. DebugAssert(FCapturedLog != NULL);
  2449. if ((OutputType == cotOutput) || (OutputType == cotError))
  2450. {
  2451. FCapturedLog->Add(AddedLine);
  2452. }
  2453. }
  2454. //---------------------------------------------------------------------------
  2455. bool __fastcall TCustomScpExplorerForm::IsFileControl(TObject * Control,
  2456. TOperationSide Side)
  2457. {
  2458. return
  2459. (Side == osOther) &&
  2460. ((Control == DirView(osOther)) || (Control == DriveView(osOther)));
  2461. }
  2462. //---------------------------------------------------------------------------
  2463. void __fastcall TCustomScpExplorerForm::DirViewContextPopupDefaultItem(
  2464. TOperationSide Side, TTBXCustomItem * Item,
  2465. TResolvedDoubleClickAction DoubleClickAction1, TResolvedDoubleClickAction DoubleClickAction2)
  2466. {
  2467. TCustomDirView * DView = DirView(Side);
  2468. bool IsDefault = false;
  2469. if (DView->ItemFocused != NULL)
  2470. {
  2471. TTerminal * ATerminal = !IsSideLocalBrowser(Side) ? Terminal : NULL;
  2472. TResolvedDoubleClickAction DefaultAction =
  2473. WinConfiguration->ResolveDoubleClickAction(DView->ItemIsDirectory(DView->ItemFocused), ATerminal);
  2474. IsDefault = (DefaultAction == DoubleClickAction1) || (DefaultAction == DoubleClickAction2);
  2475. }
  2476. TTBItemOptions O = Item->Options;
  2477. if (IsDefault)
  2478. {
  2479. Item->Options = O << tboDefault;
  2480. }
  2481. else
  2482. {
  2483. Item->Options = O >> tboDefault;
  2484. }
  2485. }
  2486. //---------------------------------------------------------------------------
  2487. void __fastcall TCustomScpExplorerForm::DirViewContextPopup(
  2488. TOperationSide Side, Byte PopupComponent, const TPoint &MousePos)
  2489. {
  2490. TCustomDirView * DView = DirView(Side);
  2491. TListItem * Item = DView->ItemFocused;
  2492. if ((DView->GetItemAt(MousePos.x, MousePos.y) == Item) &&
  2493. EnableFocusedOperation[Side])
  2494. {
  2495. TPoint ClientPoint;
  2496. ClientPoint = ((MousePos.x < 0) && (MousePos.y < 0)) ?
  2497. TPoint(0, 0) : MousePos;
  2498. FLastContextPopupScreenPoint = DView->ClientToScreen(ClientPoint);
  2499. reinterpret_cast<TPopupMenu*>(GetComponent(PopupComponent))->Popup(
  2500. FLastContextPopupScreenPoint.x, FLastContextPopupScreenPoint.y);
  2501. }
  2502. }
  2503. //---------------------------------------------------------------------------
  2504. void __fastcall TCustomScpExplorerForm::RemoteDirViewContextPopup(
  2505. TObject * /*Sender*/, const TPoint &MousePos, bool &Handled)
  2506. {
  2507. DirViewContextPopupDefaultItem(osRemote, NonVisualDataModule->RemoteOpenMenuItem, rdcaChangeDir, rdcaOpen);
  2508. DirViewContextPopupDefaultItem(osRemote, NonVisualDataModule->RemoteEditMenuItem, rdcaEdit, rdcaNone);
  2509. DirViewContextPopupDefaultItem(osRemote, NonVisualDataModule->RemoteCopyMenuItem, rdcaCopy, rdcaNone);
  2510. DirViewContextPopup(osRemote, fcRemotePopup, MousePos);
  2511. Handled = true;
  2512. }
  2513. //---------------------------------------------------------------------------
  2514. void __fastcall TCustomScpExplorerForm::ReloadLocalDirectory(const UnicodeString Directory)
  2515. {
  2516. }
  2517. //---------------------------------------------------------------------------
  2518. void __fastcall TCustomScpExplorerForm::BatchStart(void *& /*Storage*/)
  2519. {
  2520. DebugAssert(FErrorList == NULL);
  2521. if (WinConfiguration->ContinueOnError)
  2522. {
  2523. FErrorList = new TStringList();
  2524. Configuration->Usage->Inc(L"ContinuationsOnError");
  2525. }
  2526. NonVisualDataModule->StartBusy();
  2527. }
  2528. //---------------------------------------------------------------------------
  2529. void __fastcall TCustomScpExplorerForm::BatchEnd(void * /*Storage*/)
  2530. {
  2531. NonVisualDataModule->EndBusy();
  2532. if (FErrorList)
  2533. {
  2534. HandleErrorList(FErrorList);
  2535. }
  2536. }
  2537. //---------------------------------------------------------------------------
  2538. void __fastcall TCustomScpExplorerForm::UpdateCopyParamCounters(
  2539. const TCopyParamType & CopyParam)
  2540. {
  2541. if (!CopyParam.IncludeFileMask.Masks.IsEmpty())
  2542. {
  2543. Configuration->Usage->Inc(L"FileMaskUses");
  2544. }
  2545. if (IsEffectiveFileNameMask(CopyParam.FileMask))
  2546. {
  2547. Configuration->Usage->Inc(L"OperationMaskUses");
  2548. }
  2549. }
  2550. //---------------------------------------------------------------------------
  2551. bool __fastcall TCustomScpExplorerForm::ExecuteCopyMoveFileOperation(
  2552. TFileOperation Operation, TOperationSide Side, TStrings * FileList, bool NoConfirmation, void * AParam)
  2553. {
  2554. DebugAssert(!IsLocalBrowserMode());
  2555. TTransferDirection Direction = (Side == osLocal ? tdToRemote : tdToLocal);
  2556. TTransferType Type = (Operation == foCopy ? ttCopy : ttMove);
  2557. TTransferOperationParam DefaultParam;
  2558. TTransferOperationParam & Param = (AParam != NULL) ? *static_cast<TTransferOperationParam *>(AParam) : DefaultParam;
  2559. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  2560. switch (Param.Queue)
  2561. {
  2562. case asOn:
  2563. CopyParam.Queue = true;
  2564. break;
  2565. case asOff:
  2566. CopyParam.Queue = false;
  2567. break;
  2568. case asAuto:
  2569. default:
  2570. // keep default
  2571. break;
  2572. }
  2573. bool Result =
  2574. CopyParamDialog(Direction, Type, Param.Temp, FileList, Param.TargetDirectory, CopyParam, !NoConfirmation, Param.DragDrop, Param.Options);
  2575. if (Result)
  2576. {
  2577. DebugAssert(Terminal);
  2578. bool SelectionRestored = false;
  2579. TCustomDirView * DView = NULL;
  2580. if (HasDirView[Side])
  2581. {
  2582. DView = DirView(Side);
  2583. DView->SaveSelection();
  2584. DView->SaveSelectedNames();
  2585. }
  2586. UpdateCopyParamCounters(CopyParam);
  2587. std::unique_ptr<TStringList> TransferResumeList(new TStringList());
  2588. DebugAssert(FTransferResumeList == NULL);
  2589. FTransferResumeList = Terminal->IsCapable[fcMoveToQueue] ? TransferResumeList.get() : NULL;
  2590. FMoveToQueue = false;
  2591. int Params = FLAGMASK(Operation == foMove, cpDelete);
  2592. try
  2593. {
  2594. TStrings * PermanentFileList;
  2595. std::unique_ptr<TStrings> PermanentFileListOwner;
  2596. try
  2597. {
  2598. if (Side == osLocal)
  2599. {
  2600. PermanentFileList = FileList;
  2601. Params |= FLAGMASK(Param.Temp, cpTemporary);
  2602. CopyParam.IncludeFileMask.SetRoots(FileList, Param.TargetDirectory);
  2603. Terminal->CopyToRemote(FileList, Param.TargetDirectory, &CopyParam, Params, NULL);
  2604. if (Operation == foMove)
  2605. {
  2606. ReloadLocalDirectory();
  2607. if (DView != NULL)
  2608. {
  2609. DView->RestoreSelection();
  2610. }
  2611. SelectionRestored = true;
  2612. }
  2613. }
  2614. else
  2615. {
  2616. // Clone the file list as it may refer to current directory files,
  2617. // which get destroyed, when the source directory is reloaded after foMove operation.
  2618. // We should actually clone the file list for whole ExecuteFileOperation to protect against reloads.
  2619. // But for a hotfix, we are not going to do such a big change.
  2620. PermanentFileListOwner.reset(TRemoteFileList::CloneStrings(FileList));
  2621. PermanentFileList = PermanentFileListOwner.get();
  2622. try
  2623. {
  2624. CopyParam.IncludeFileMask.SetRoots(Param.TargetDirectory, FileList);
  2625. Terminal->CopyToLocal(FileList, Param.TargetDirectory, &CopyParam, Params, NULL);
  2626. }
  2627. __finally
  2628. {
  2629. if (Operation == foMove)
  2630. {
  2631. if (DView != NULL)
  2632. {
  2633. DView->RestoreSelection();
  2634. }
  2635. SelectionRestored = true;
  2636. }
  2637. ReloadLocalDirectory(Param.TargetDirectory);
  2638. }
  2639. }
  2640. // When the transfer batch is aborted between individual file transfers, the Abort is not called.
  2641. if (FMoveToQueue)
  2642. {
  2643. Abort();
  2644. }
  2645. }
  2646. catch (EAbort &)
  2647. {
  2648. if (FMoveToQueue)
  2649. {
  2650. Params |= (CopyParam.QueueNoConfirmation ? cpNoConfirmation : 0);
  2651. DebugAssert(CopyParam.TransferSkipList == NULL);
  2652. DebugAssert(CopyParam.TransferResumeFile.IsEmpty());
  2653. if (TransferResumeList->Count > 0)
  2654. {
  2655. CopyParam.TransferResumeFile = TransferResumeList->Strings[TransferResumeList->Count - 1];
  2656. TransferResumeList->Delete(TransferResumeList->Count - 1);
  2657. }
  2658. CopyParam.TransferSkipList = TransferResumeList.release();
  2659. // not really needed, just to keep it consistent with TransferResumeList
  2660. FTransferResumeList = NULL;
  2661. FMoveToQueue = false;
  2662. Configuration->Usage->Inc("MovesToBackground");
  2663. AddQueueItem(Queue, Direction, PermanentFileList, Param.TargetDirectory, CopyParam, Params);
  2664. ClearTransferSourceSelection(Direction);
  2665. }
  2666. throw;
  2667. }
  2668. }
  2669. __finally
  2670. {
  2671. if (!SelectionRestored && (DView != NULL))
  2672. {
  2673. DView->DiscardSavedSelection();
  2674. }
  2675. FTransferResumeList = NULL;
  2676. }
  2677. }
  2678. return Result;
  2679. }
  2680. //---------------------------------------------------------------------------
  2681. bool __fastcall TCustomScpExplorerForm::ExecuteDeleteFileOperation(
  2682. TOperationSide Side, TStrings * FileList, void * Param)
  2683. {
  2684. DebugAssert(FileList->Count);
  2685. // We deliberately do not toggle alternative flag (Param), but use OR,
  2686. // because the Param is set only when command is invoked using Shift-Del/F8 keyboard
  2687. // shortcut of CurrentDeleteAlternativeAction
  2688. bool Alternative =
  2689. bool(Param) || UseAlternativeFunction();
  2690. bool Recycle;
  2691. if (IsSideLocalBrowser(Side))
  2692. {
  2693. Recycle = (WinConfiguration->DeleteToRecycleBin != Alternative);
  2694. }
  2695. else
  2696. {
  2697. Recycle =
  2698. (Terminal->SessionData->DeleteToRecycleBin != Alternative) &&
  2699. !Terminal->SessionData->RecycleBinPath.IsEmpty() &&
  2700. !Terminal->IsRecycledFile(FileList->Strings[0]);
  2701. }
  2702. bool Result = !(Recycle ? WinConfiguration->ConfirmRecycling : WinConfiguration->ConfirmDeleting);
  2703. if (!Result)
  2704. {
  2705. UnicodeString Query;
  2706. if (FileList->Count == 1)
  2707. {
  2708. if (IsSideLocalBrowser(Side))
  2709. {
  2710. Query = ExtractFileName(FileList->Strings[0]);
  2711. }
  2712. else
  2713. {
  2714. Query = UnixExtractFileName(FileList->Strings[0]);
  2715. }
  2716. Query = FMTLOAD((Recycle ? CONFIRM_RECYCLE_FILE : CONFIRM_DELETE_FILE), (Query));
  2717. }
  2718. else
  2719. {
  2720. Query = FMTLOAD((Recycle ? CONFIRM_RECYCLE_FILES : CONFIRM_DELETE_FILES), (FileList->Count));
  2721. }
  2722. TMessageParams Params(mpNeverAskAgainCheck);
  2723. Params.ImageName = L"Delete file";
  2724. unsigned int Answer =
  2725. MessageDialog(MainInstructions(Query), qtConfirmation, qaOK | qaCancel, HELP_DELETE_FILE, &Params);
  2726. if (Answer == qaNeverAskAgain)
  2727. {
  2728. Result = true;
  2729. if (Recycle)
  2730. {
  2731. WinConfiguration->ConfirmRecycling = false;
  2732. }
  2733. else
  2734. {
  2735. WinConfiguration->ConfirmDeleting = false;
  2736. }
  2737. }
  2738. else
  2739. {
  2740. Result = (Answer == qaOK);
  2741. }
  2742. }
  2743. if (Result)
  2744. {
  2745. DeleteFiles(Side, FileList, FLAGMASK(Alternative, dfAlternative));
  2746. }
  2747. return Result;
  2748. }
  2749. //---------------------------------------------------------------------------
  2750. bool __fastcall TCustomScpExplorerForm::ExecuteFileOperation(TFileOperation Operation,
  2751. TOperationSide Side, TStrings * FileList, bool NoConfirmation, void * Param)
  2752. {
  2753. TAutoBatch AutoBatch(this);
  2754. bool Result;
  2755. if (TFileOperationProgressType::IsTransferOperation(Operation))
  2756. {
  2757. Result = ExecuteCopyMoveFileOperation(Operation, Side, FileList, NoConfirmation, Param);
  2758. }
  2759. else if (Operation == foRename)
  2760. {
  2761. DebugAssert(DirView(Side)->ItemFocused);
  2762. DirView(Side)->ItemFocused->EditCaption();
  2763. Result = true;
  2764. }
  2765. else if (Operation == foDelete)
  2766. {
  2767. Result = ExecuteDeleteFileOperation(Side, FileList, Param);
  2768. }
  2769. else if (Operation == foSetProperties)
  2770. {
  2771. DirView(osRemote)->SaveSelectedNames();
  2772. Result = SetProperties(Side, FileList);
  2773. }
  2774. else if (Operation == foCustomCommand)
  2775. {
  2776. DebugAssert(Param);
  2777. DebugAssert(Side == osRemote); // Side carries no meaning for foCustomCommand
  2778. DirView(osRemote)->SaveSelectedNames();
  2779. const TCustomCommandType * Command = static_cast<const TCustomCommandType*>(Param);
  2780. CustomCommand(FileList, *Command, NULL);
  2781. Result = true;
  2782. }
  2783. else if ((Operation == foRemoteMove) || (Operation == foRemoteCopy))
  2784. {
  2785. DebugAssert(!IsSideLocalBrowser(Side));
  2786. Result = RemoteTransferFiles(FileList, NoConfirmation,
  2787. (Operation == foRemoteMove), reinterpret_cast<TManagedTerminal *>(Param));
  2788. }
  2789. else if (Operation == foLock)
  2790. {
  2791. DebugAssert(!IsSideLocalBrowser(Side));
  2792. LockFiles(FileList, true);
  2793. Result = true;
  2794. }
  2795. else if (Operation == foUnlock)
  2796. {
  2797. DebugAssert(!IsSideLocalBrowser(Side));
  2798. LockFiles(FileList, false);
  2799. Result = true;
  2800. }
  2801. else
  2802. {
  2803. DebugFail();
  2804. }
  2805. return Result;
  2806. }
  2807. //---------------------------------------------------------------------------
  2808. TOperationSide __fastcall TCustomScpExplorerForm::GetSide(TOperationSide Side)
  2809. {
  2810. if (Side == osCurrent)
  2811. {
  2812. Side = FCurrentSide;
  2813. }
  2814. return Side;
  2815. }
  2816. //---------------------------------------------------------------------------
  2817. bool __fastcall TCustomScpExplorerForm::ExecuteFileOperation(TFileOperation Operation,
  2818. TOperationSide Side, bool OnFocused, bool NoConfirmation, void * Param)
  2819. {
  2820. Side = GetSide(Side);
  2821. bool FullPath = IsSideLocalBrowser(Side);
  2822. TCustomDirView * ADirView = DirView(Side);
  2823. std::unique_ptr<TStringList> FileList(new TStringList());
  2824. ADirView->CreateFileList(OnFocused, FullPath, FileList.get());
  2825. bool ReloadProperties =
  2826. !IsSideLocalBrowser(Side) && (Operation == foSetProperties) &&
  2827. DebugAlwaysTrue(HasActiveTerminal()) && Terminal->IsCapable[fcLoadingAdditionalProperties];
  2828. bool Result = ExecuteFileOperation(Operation, Side, FileList.get(), NoConfirmation, Param);
  2829. if (Result && ReloadProperties)
  2830. {
  2831. std::unique_ptr<TStringList> FileList2(new TStringList());
  2832. FileList->CaseSensitive = true;
  2833. FileList->Sort();
  2834. for (int Index = 0; Index < ADirView->Items->Count; Index++)
  2835. {
  2836. TListItem * Item = ADirView->Items->Item[Index];
  2837. UnicodeString FileName = ADirView->ItemFileName(Item);
  2838. int Index2;
  2839. if (FileList->Find(FileName, Index2))
  2840. {
  2841. FileList2->AddObject(FileName, ADirView->ItemData(Item));
  2842. }
  2843. }
  2844. LoadFilesProperties(FileList2.get());
  2845. }
  2846. return Result;
  2847. }
  2848. //---------------------------------------------------------------------------
  2849. void __fastcall TCustomScpExplorerForm::ExecuteFileOperationCommand(
  2850. TFileOperation Operation, TOperationSide Side, bool OnFocused,
  2851. bool NoConfirmation, void * Param)
  2852. {
  2853. if (ExecuteFileOperation(Operation, Side, OnFocused, NoConfirmation, Param))
  2854. {
  2855. if (TFileOperationProgressType::IsTransferOperation(Operation))
  2856. {
  2857. DebugAssert(!IsLocalBrowserMode());
  2858. if (GetSide(Side) == osLocal)
  2859. {
  2860. Configuration->Usage->Inc(L"UploadsCommand");
  2861. }
  2862. else
  2863. {
  2864. Configuration->Usage->Inc(L"DownloadsCommand");
  2865. }
  2866. }
  2867. }
  2868. }
  2869. //---------------------------------------------------------------------------
  2870. void __fastcall TCustomScpExplorerForm::ExecuteCopyOperationCommand(
  2871. TOperationSide Side, bool OnFocused, unsigned int Flags)
  2872. {
  2873. TTransferOperationParam Param;
  2874. if ((WinConfiguration->Interface != ifCommander) ||
  2875. WinConfiguration->ScpCommander.ExplorerKeyboardShortcuts)
  2876. {
  2877. Flags &= ~cocShortCutHint;
  2878. }
  2879. TCustomDirView * DView = DirView(Side);
  2880. Param.Options =
  2881. FLAGMASK(FLAGSET(Flags, cocShortCutHint), coShortCutHint) |
  2882. FLAGMASK(SelectedAllFilesInDirView(DView), coAllFiles);
  2883. if (FLAGSET(Flags, cocQueue))
  2884. {
  2885. Param.Queue = asOn;
  2886. }
  2887. else if (FLAGSET(Flags, cocNonQueue))
  2888. {
  2889. Param.Queue = asOff;
  2890. }
  2891. ExecuteFileOperationCommand(foCopy, Side, OnFocused, false, &Param);
  2892. }
  2893. //---------------------------------------------------------------------------
  2894. void __fastcall TCustomScpExplorerForm::HandleErrorList(TStringList *& ErrorList)
  2895. {
  2896. int Count = ErrorList->Count;
  2897. try
  2898. {
  2899. if (ErrorList->Count)
  2900. {
  2901. UnicodeString Message = MainInstructions(FMTLOAD(ERROR_LIST_COUNT, (ErrorList->Count)));
  2902. if (MessageDialog(Message, qtError,
  2903. qaOK | qaCancel, HELP_NONE) == qaOK)
  2904. {
  2905. unsigned int Answer;
  2906. int Index = 0;
  2907. do
  2908. {
  2909. DebugAssert(Index >= 0 && Index < Count);
  2910. TQueryButtonAlias Aliases[2];
  2911. Aliases[0].Button = qaYes;
  2912. Aliases[0].Alias = LoadStr(PREV_BUTTON);
  2913. Aliases[1].Button = qaNo;
  2914. Aliases[1].Alias = LoadStr(NEXT_BUTTON);
  2915. TMessageParams Params;
  2916. Params.Aliases = Aliases;
  2917. Params.AliasesCount = LENOF(Aliases);
  2918. int No = Index + 1;
  2919. UnicodeString Message = ErrorList->Strings[Index];
  2920. UnicodeString AMainInstructions;
  2921. UnicodeString Details;
  2922. if (ExtractMainInstructions(Message, AMainInstructions))
  2923. {
  2924. Details = Message;
  2925. Message = AMainInstructions;
  2926. }
  2927. Message = MainInstructions(FMTLOAD(ERROR_LIST_NUMBER, (No, Count, Message))) + Details;
  2928. Answer = MoreMessageDialog(
  2929. Message,
  2930. dynamic_cast<TStrings *>(ErrorList->Objects[Index]), qtError,
  2931. (Index ? qaYes : 0) | (Index < Count - 1 ? qaNo : 0) |
  2932. qaOK, HELP_NONE, &Params);
  2933. if (Answer == qaNo)
  2934. {
  2935. Index++;
  2936. }
  2937. if (Answer == qaYes)
  2938. {
  2939. Index--;
  2940. }
  2941. }
  2942. while (Answer != qaOK);
  2943. }
  2944. }
  2945. }
  2946. __finally
  2947. {
  2948. TStrings * List = ErrorList;
  2949. ErrorList = NULL;
  2950. for (int i = 0; i < Count; i++)
  2951. {
  2952. delete List->Objects[i];
  2953. }
  2954. delete List;
  2955. }
  2956. }
  2957. //---------------------------------------------------------------------------
  2958. void __fastcall TCustomScpExplorerForm::ExecuteRemoteFile(
  2959. const UnicodeString & FullFileName, TRemoteFile * File, TExecuteFileBy ExecuteFileBy)
  2960. {
  2961. // needed for checking filemasks, as there's no directory object
  2962. // associated with the file object
  2963. File->FullFileName = FullFileName;
  2964. TFileMasks::TParams MaskParams;
  2965. MaskParams.Size = File->Size;
  2966. MaskParams.Modification = File->Modification;
  2967. ExecuteFile(osRemote, ExecuteFileBy, NULL, FullFileName, File, MaskParams);
  2968. }
  2969. //---------------------------------------------------------------------------
  2970. void __fastcall TCustomScpExplorerForm::EditNew(TOperationSide Side)
  2971. {
  2972. DebugAssert(!WinConfiguration->DisableOpenEdit);
  2973. Side = GetSide(Side);
  2974. TCustomDirView * DView = DirView(Side);
  2975. TListItem * FocusedItem = DView->ItemFocused;
  2976. UnicodeString Name;
  2977. if ((FocusedItem != NULL) && !DView->ItemIsDirectory(FocusedItem))
  2978. {
  2979. Name = DView->ItemFileName(FocusedItem);
  2980. }
  2981. else
  2982. {
  2983. Name = LoadStr(NEW_FILE);
  2984. }
  2985. UnicodeString Names = Name;
  2986. std::unique_ptr<TStrings> History(CloneStrings(CustomWinConfiguration->History[L"EditFile"]));
  2987. if (InputDialog(LoadStr(EDIT_FILE_CAPTION), LoadStr(EDIT_FILE_PROMPT), Names,
  2988. HELP_EDIT_NEW, History.get(), true, NULL, true, 400))
  2989. {
  2990. while (!Names.IsEmpty())
  2991. {
  2992. Name = CutToChar(Names, FileMasksDelimiters[1], false);
  2993. CustomWinConfiguration->History[L"EditFile"] = History.get();
  2994. UnicodeString TargetFileName;
  2995. UnicodeString LocalFileName;
  2996. UnicodeString RootTempDir;
  2997. UnicodeString TempDir;
  2998. UnicodeString RemoteDirectory;
  2999. bool ExistingFile = false;
  3000. if (!IsSideLocalBrowser(Side))
  3001. {
  3002. Name = AbsolutePath(Terminal->CurrentDirectory, Name);
  3003. TRemoteFile * File = NULL;
  3004. if (Terminal->FileExists(Name, &File) &&
  3005. !File->IsDirectory)
  3006. {
  3007. try
  3008. {
  3009. ExecuteRemoteFile(Name, File, efDefaultEditor);
  3010. ExistingFile = true;
  3011. }
  3012. __finally
  3013. {
  3014. delete File;
  3015. }
  3016. }
  3017. if (!ExistingFile)
  3018. {
  3019. RemoteDirectory = UnixExtractFilePath(Name);
  3020. TemporaryDirectoryForRemoteFiles(
  3021. RemoteDirectory, GUIConfiguration->CurrentCopyParam, TempDir, RootTempDir);
  3022. TargetFileName = UnixExtractFileName(Name);
  3023. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  3024. LocalFileName = TempDir +
  3025. // We probably do not want to trim the VMS version here
  3026. Terminal->ChangeFileName(&CopyParam, TargetFileName, osRemote, false);
  3027. }
  3028. }
  3029. else
  3030. {
  3031. if (ExtractFilePath(Name).IsEmpty())
  3032. {
  3033. LocalFileName = IncludeTrailingBackslash(DView->PathName) + Name;
  3034. }
  3035. else
  3036. {
  3037. LocalFileName = ExpandFileName(Name);
  3038. }
  3039. TargetFileName = ExtractFileName(Name);
  3040. }
  3041. if (!ExistingFile)
  3042. {
  3043. bool NewFile = !FileExists(ApiPath(LocalFileName));
  3044. if (NewFile)
  3045. {
  3046. int File = FileCreate(ApiPath(LocalFileName));
  3047. if (File < 0)
  3048. {
  3049. if (!RootTempDir.IsEmpty())
  3050. {
  3051. RecursiveDeleteFile(ExcludeTrailingBackslash(RootTempDir), false);
  3052. }
  3053. throw Exception(FMTLOAD(CREATE_FILE_ERROR, (LocalFileName)));
  3054. }
  3055. else
  3056. {
  3057. FileClose(File);
  3058. }
  3059. }
  3060. TExecuteFileBy ExecuteFileBy = efDefaultEditor;
  3061. const TEditorData * ExternalEditor = NULL;
  3062. TFileMasks::TParams MaskParams; // size not known
  3063. ExecuteFileNormalize(ExecuteFileBy, ExternalEditor, TargetFileName,
  3064. false, MaskParams);
  3065. CustomExecuteFile(Side, ExecuteFileBy, LocalFileName, TargetFileName,
  3066. ExternalEditor, RootTempDir, RemoteDirectory, NewFile);
  3067. }
  3068. }
  3069. }
  3070. }
  3071. //---------------------------------------------------------------------------
  3072. bool __fastcall TCustomScpExplorerForm::RemoteExecuteForceText(
  3073. TExecuteFileBy ExecuteFileBy, const TEditorData * ExternalEditor)
  3074. {
  3075. DebugAssert((ExecuteFileBy == efExternalEditor) ==
  3076. ((ExternalEditor != NULL) && (ExternalEditor->Editor == edExternal)));
  3077. DebugAssert(ExecuteFileBy != efDefaultEditor);
  3078. return
  3079. ((ExecuteFileBy == efInternalEditor)) ||
  3080. ((ExecuteFileBy == efExternalEditor) && ExternalEditor->ExternalEditorText);
  3081. }
  3082. //---------------------------------------------------------------------------
  3083. void __fastcall TCustomScpExplorerForm::CustomExecuteFile(TOperationSide Side,
  3084. TExecuteFileBy ExecuteFileBy, UnicodeString FileName, UnicodeString OriginalFileName,
  3085. const TEditorData * ExternalEditor, UnicodeString LocalRootDirectory,
  3086. UnicodeString RemoteDirectory, bool NewFile)
  3087. {
  3088. DebugAssert(!WinConfiguration->DisableOpenEdit);
  3089. DebugAssert((ExecuteFileBy == efExternalEditor) ==
  3090. ((ExternalEditor != NULL) && (ExternalEditor->Editor == edExternal)));
  3091. DebugAssert(ExecuteFileBy != efDefaultEditor);
  3092. Side = GetSide(Side);
  3093. std::unique_ptr<TEditedFileData> Data(new TEditedFileData);
  3094. if (!IsSideLocalBrowser(Side))
  3095. {
  3096. Data->Terminal = Terminal;
  3097. Data->Queue = Queue;
  3098. Data->SessionData = CloneCurrentSessionData();
  3099. Data->ForceText = RemoteExecuteForceText(ExecuteFileBy, ExternalEditor);
  3100. Data->RemoteDirectory = RemoteDirectory;
  3101. Data->SessionName = Terminal->SessionData->SessionName;
  3102. Data->LocalRootDirectory = LocalRootDirectory;
  3103. Data->OriginalFileName = OriginalFileName;
  3104. Data->Command = L""; // will be changed later for external editor
  3105. }
  3106. if (ExecuteFileBy == efInternalEditor)
  3107. {
  3108. if (!IsSideLocalBrowser(Side))
  3109. {
  3110. UnicodeString RemotePath = UnixCombinePaths(RemoteDirectory, OriginalFileName);
  3111. UnicodeString Caption = RemotePath + TitleSeparator + Terminal->SessionData->SessionName;
  3112. TForm * Editor;
  3113. try
  3114. {
  3115. Editor =
  3116. ShowEditorForm(
  3117. FileName, this,
  3118. FEditorManager->FileChanged, FEditorManager->FileReload, FEditorManager->FileClosed,
  3119. SaveAllInternalEditors, AnyInternalEditorModified,
  3120. Caption, StandaloneOperation, SessionColor, Terminal->SessionData->InternalEditorEncoding, NewFile);
  3121. }
  3122. catch(...)
  3123. {
  3124. if (!LocalRootDirectory.IsEmpty())
  3125. {
  3126. RecursiveDeleteFile(ExcludeTrailingBackslash(LocalRootDirectory), false);
  3127. }
  3128. throw;
  3129. }
  3130. FEditorManager->AddFileInternal(FileName, Data.release(), Editor);
  3131. }
  3132. else
  3133. {
  3134. DebugAssert(!StandaloneOperation);
  3135. TForm * Editor =
  3136. ShowEditorForm(FileName, this, NULL, NULL, LocalEditorClosed,
  3137. SaveAllInternalEditors, AnyInternalEditorModified,
  3138. L"", false, SessionColor, -1, NewFile);
  3139. FLocalEditors->Add(Editor);
  3140. }
  3141. }
  3142. else
  3143. {
  3144. HANDLE Process;
  3145. if (ExecuteFileBy == efExternalEditor)
  3146. {
  3147. UnicodeString Program, Params, Dir;
  3148. Data->Command = ExternalEditor->ExternalEditor;
  3149. ReformatFileNameCommand(Data->Command);
  3150. SplitCommand(Data->Command, Program, Params, Dir);
  3151. Params = ExpandFileNameCommand(Params, FileName);
  3152. Program = ExpandEnvironmentVariables(Program);
  3153. if (!ExecuteShell(Program, Params, Process))
  3154. {
  3155. throw EOSExtException(FMTLOAD(EDITOR_ERROR, (Program)));
  3156. }
  3157. }
  3158. else
  3159. {
  3160. DebugAssert(!IsSideLocalBrowser(Side));
  3161. if (!ExecuteShell(FileName, L"", Process))
  3162. {
  3163. throw EOSExtException(FMTLOAD(EXECUTE_FILE_ERROR, (FileName)));
  3164. }
  3165. }
  3166. if (IsSideLocalBrowser(Side) ||
  3167. ((ExecuteFileBy == efShell) &&
  3168. !WinConfiguration->Editor.SDIShellEditor) ||
  3169. ((ExecuteFileBy == efExternalEditor) &&
  3170. !ExternalEditor->SDIExternalEditor))
  3171. {
  3172. // no need for handle
  3173. if (Process != NULL)
  3174. {
  3175. DebugCheck(CloseHandle(Process));
  3176. }
  3177. Process = INVALID_HANDLE_VALUE;
  3178. }
  3179. else
  3180. {
  3181. if (Process == NULL)
  3182. {
  3183. throw ExtException(LoadStr(OPEN_FILE_NO_PROCESS2), L"", HELP_OPEN_FILE_NO_PROCESS);
  3184. }
  3185. }
  3186. if (!IsSideLocalBrowser(Side))
  3187. {
  3188. FEditorManager->AddFileExternal(FileName, Data.release(), Process);
  3189. }
  3190. }
  3191. }
  3192. //---------------------------------------------------------------------------
  3193. void __fastcall TCustomScpExplorerForm::SaveInternalEditor(
  3194. const UnicodeString /*FileName*/, TEditedFileData * /*Data*/, TObject * Token,
  3195. void * /*Arg*/)
  3196. {
  3197. if (Token != NULL)
  3198. {
  3199. EditorFormFileSave(static_cast<TForm *>(Token));
  3200. }
  3201. }
  3202. //---------------------------------------------------------------------------
  3203. void __fastcall TCustomScpExplorerForm::SaveAllInternalEditors(TObject * /*Sender*/)
  3204. {
  3205. for (int Index = 0; Index < FLocalEditors->Count; Index++)
  3206. {
  3207. EditorFormFileSave(static_cast<TForm *>(FLocalEditors->Items[Index]));
  3208. }
  3209. FEditorManager->ProcessFiles(SaveInternalEditor, NULL);
  3210. }
  3211. //---------------------------------------------------------------------------
  3212. void __fastcall TCustomScpExplorerForm::InternalEditorModified(
  3213. const UnicodeString /*FileName*/, TEditedFileData * /*Data*/, TObject * Token,
  3214. void * Arg)
  3215. {
  3216. if ((Token != NULL) &&
  3217. IsEditorFormModified(static_cast<TForm *>(Token)))
  3218. {
  3219. *static_cast<bool *>(Arg) = true;
  3220. }
  3221. }
  3222. //---------------------------------------------------------------------------
  3223. void __fastcall TCustomScpExplorerForm::AnyInternalEditorModified(
  3224. TObject * /*Sender*/, bool & Modified)
  3225. {
  3226. for (int Index = 0; !Modified && (Index < FLocalEditors->Count); Index++)
  3227. {
  3228. if (IsEditorFormModified(static_cast<TForm *>(FLocalEditors->Items[Index])))
  3229. {
  3230. Modified = true;
  3231. }
  3232. }
  3233. if (!Modified)
  3234. {
  3235. FEditorManager->ProcessFiles(InternalEditorModified, &Modified);
  3236. }
  3237. }
  3238. //---------------------------------------------------------------------------
  3239. void __fastcall TCustomScpExplorerForm::LocalEditorClosed(TObject * Sender, bool /*Forced*/)
  3240. {
  3241. DebugCheck(FLocalEditors->Extract(Sender) >= 0);
  3242. }
  3243. //---------------------------------------------------------------------------
  3244. void __fastcall TCustomScpExplorerForm::TemporaryDirectoryForRemoteFiles(
  3245. UnicodeString RemoteDirectory, TCopyParamType CopyParam,
  3246. UnicodeString & Result, UnicodeString & RootDirectory)
  3247. {
  3248. if (!WinConfiguration->TemporaryDirectoryDeterministic)
  3249. {
  3250. RootDirectory = IncludeTrailingBackslash(WinConfiguration->TemporaryDir());
  3251. Result = RootDirectory;
  3252. }
  3253. else
  3254. {
  3255. RootDirectory = L"";
  3256. Result = WinConfiguration->ExpandedTemporaryDirectory();
  3257. Result = IncludeTrailingBackslash(Result);
  3258. }
  3259. if (WinConfiguration->TemporaryDirectoryAppendSession)
  3260. {
  3261. Result = IncludeTrailingBackslash(Result + MakeValidFileName(Terminal->SessionData->SessionName));
  3262. }
  3263. if (WinConfiguration->TemporaryDirectoryAppendPath)
  3264. {
  3265. if (!RemoteDirectory.IsEmpty() && (RemoteDirectory[1] == L'/'))
  3266. {
  3267. RemoteDirectory.Delete(1, 1);
  3268. }
  3269. Result = IncludeTrailingBackslash(Result + CopyParam.ValidLocalPath(FromUnixPath(RemoteDirectory)));
  3270. }
  3271. if (!ForceDirectories(ApiPath(Result)))
  3272. {
  3273. throw EOSExtException(FMTLOAD(CREATE_TEMP_DIR_ERROR, (Result)));
  3274. }
  3275. }
  3276. //---------------------------------------------------------------------------
  3277. void __fastcall TCustomScpExplorerForm::TemporarilyDownloadFiles(
  3278. TStrings * FileList, bool ForceText, UnicodeString & RootTempDir, UnicodeString & TempDir,
  3279. bool GetTargetNames, bool AutoOperation)
  3280. {
  3281. DebugAssert(!IsLocalBrowserMode());
  3282. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  3283. if (ForceText)
  3284. {
  3285. CopyParam.TransferMode = tmAscii;
  3286. }
  3287. TemporaryFileCopyParam(CopyParam);
  3288. if (TempDir.IsEmpty())
  3289. {
  3290. TemporaryDirectoryForRemoteFiles(Terminal->CurrentDirectory, CopyParam, TempDir, RootTempDir);
  3291. }
  3292. DebugAssert(!FAutoOperation);
  3293. FAutoOperation = AutoOperation;
  3294. Terminal->ExceptionOnFail = true;
  3295. try
  3296. {
  3297. try
  3298. {
  3299. // turn off confirmations, as for MDI editors we may possibly download
  3300. // the same file over
  3301. Terminal->CopyToLocal(
  3302. FileList, TempDir, &CopyParam, cpNoConfirmation | cpTemporary, NULL);
  3303. if (GetTargetNames)
  3304. {
  3305. for (int i = 0; i < FileList->Count; i++)
  3306. {
  3307. FileList->Strings[i] =
  3308. Terminal->ChangeFileName(&CopyParam, UnixExtractFileName(FileList->Strings[i]), osRemote, false);
  3309. }
  3310. }
  3311. }
  3312. catch(...)
  3313. {
  3314. if (!RootTempDir.IsEmpty())
  3315. {
  3316. RecursiveDeleteFile(ExcludeTrailingBackslash(RootTempDir), false);
  3317. }
  3318. throw;
  3319. }
  3320. }
  3321. __finally
  3322. {
  3323. FAutoOperation = false;
  3324. Terminal->ExceptionOnFail = false;
  3325. }
  3326. }
  3327. //---------------------------------------------------------------------------
  3328. void __fastcall TCustomScpExplorerForm::EditorAutoConfig()
  3329. {
  3330. // Do not waste time checking for default editor list the next time,
  3331. // or testing if default editor is not notepad.
  3332. bool TryNextTime = false;
  3333. UnicodeString UsageState;
  3334. if (!WinConfiguration->EditorList->IsDefaultList())
  3335. {
  3336. UsageState = "H";
  3337. }
  3338. else
  3339. {
  3340. UnicodeString Executable;
  3341. UnicodeString ExecutableDescription;
  3342. if (DetectSystemExternalEditor(false, Executable, ExecutableDescription, UsageState, TryNextTime))
  3343. {
  3344. UnicodeString Message =
  3345. FMTLOAD(EDITOR_AUTO_CONFIG2, (ExecutableDescription, ExecutableDescription));
  3346. unsigned int Answer =
  3347. MessageDialog(Message, qtConfirmation, qaOK | qaCancel, HELP_EDITOR_AUTO_CONFIG);
  3348. if (Answer != qaOK)
  3349. {
  3350. UsageState = "R";
  3351. }
  3352. else
  3353. {
  3354. UsageState = "A";
  3355. TEditorData EditorData;
  3356. EditorData.Editor = edExternal;
  3357. EditorData.ExternalEditor = FormatCommand(Executable, L"");
  3358. EditorData.ExternalEditorOptionsAutodetect();
  3359. TEditorList EditorList;
  3360. EditorList = *WinConfiguration->EditorList;
  3361. EditorList.Insert(0, new TEditorPreferences(EditorData));
  3362. WinConfiguration->EditorList = &EditorList;
  3363. }
  3364. }
  3365. }
  3366. WinConfiguration->OfferedEditorAutoConfig = !TryNextTime;
  3367. WinConfiguration->Usage->Set(L"EditorAutoConfig", UsageState);
  3368. }
  3369. //---------------------------------------------------------------------------
  3370. void __fastcall TCustomScpExplorerForm::ExecuteFileNormalize(
  3371. TExecuteFileBy & ExecuteFileBy, const TEditorData *& ExternalEditor,
  3372. const UnicodeString & FileName, bool Local, const TFileMasks::TParams & MaskParams)
  3373. {
  3374. if (ExecuteFileBy == efDefaultEditor)
  3375. {
  3376. if (!WinConfiguration->OfferedEditorAutoConfig)
  3377. {
  3378. EditorAutoConfig();
  3379. }
  3380. const TEditorPreferences * Editor =
  3381. WinConfiguration->DefaultEditorForFile(FileName, Local, MaskParams);
  3382. if ((Editor == NULL) || (Editor->Data->Editor == edInternal))
  3383. {
  3384. ExecuteFileBy = efInternalEditor;
  3385. ExternalEditor = NULL;
  3386. }
  3387. else if (Editor->Data->Editor == edOpen)
  3388. {
  3389. ExecuteFileBy = efShell;
  3390. ExternalEditor = NULL;
  3391. }
  3392. else
  3393. {
  3394. ExecuteFileBy = efExternalEditor;
  3395. ExternalEditor = Editor->Data;
  3396. }
  3397. }
  3398. }
  3399. //---------------------------------------------------------------------------
  3400. void __fastcall TCustomScpExplorerForm::ExecuteFile(TOperationSide Side,
  3401. TExecuteFileBy ExecuteFileBy, const TEditorData * ExternalEditor,
  3402. UnicodeString FullFileName, TObject * Object, const TFileMasks::TParams & MaskParams)
  3403. {
  3404. UnicodeString OriginalFileName;
  3405. UnicodeString LocalRootDirectory;
  3406. UnicodeString RemoteDirectory;
  3407. ExecuteFileNormalize(ExecuteFileBy, ExternalEditor, FullFileName,
  3408. IsSideLocalBrowser(Side), MaskParams);
  3409. UnicodeString Counter;
  3410. UnicodeString LocalFileName;
  3411. bool Handled = false;
  3412. if (!IsSideLocalBrowser(Side))
  3413. {
  3414. // We need to trim VMS version here, so that we use name without version
  3415. // when uploading back to create a new version of the file
  3416. OriginalFileName = Terminal->GetBaseFileName(UnixExtractFileName(FullFileName));
  3417. RemoteDirectory = UnixExtractFilePath(FullFileName);
  3418. TObject * Token = NULL;
  3419. UnicodeString LocalDirectory;
  3420. if (!FEditorManager->CanAddFile(RemoteDirectory, OriginalFileName,
  3421. Terminal->SessionData->SessionName, Token, LocalRootDirectory,
  3422. LocalDirectory))
  3423. {
  3424. if (Token != NULL)
  3425. {
  3426. TForm * Form = dynamic_cast<TForm *>(Token);
  3427. if (Form->WindowState == wsMinimized)
  3428. {
  3429. ShowWindow(Form->Handle, SW_RESTORE);
  3430. }
  3431. else
  3432. {
  3433. Form->SetFocus();
  3434. }
  3435. Handled = true;
  3436. }
  3437. else
  3438. {
  3439. throw Exception(FMTLOAD(ALREADY_EDITED_EXTERNALLY_OR_UPLOADED, (OriginalFileName)));
  3440. }
  3441. }
  3442. if (!Handled)
  3443. {
  3444. TStringList * FileList1 = new TStringList();
  3445. try
  3446. {
  3447. FileList1->AddObject(FullFileName, Object);
  3448. TemporarilyDownloadFiles(FileList1,
  3449. RemoteExecuteForceText(ExecuteFileBy, ExternalEditor),
  3450. LocalRootDirectory, LocalDirectory, true, true);
  3451. LocalFileName = LocalDirectory + FileList1->Strings[0];
  3452. }
  3453. __finally
  3454. {
  3455. delete FileList1;
  3456. }
  3457. switch (ExecuteFileBy)
  3458. {
  3459. case efShell:
  3460. Counter = "RemoteFilesExecuted";
  3461. break;
  3462. case efInternalEditor:
  3463. Counter = "RemoteFilesOpenedInInternalEditor";
  3464. break;
  3465. case efExternalEditor:
  3466. Counter = "RemoteFilesOpenedInExternalEditor";
  3467. break;
  3468. default:
  3469. DebugFail();
  3470. }
  3471. }
  3472. }
  3473. else
  3474. {
  3475. LocalFileName = FullFileName;
  3476. OriginalFileName = ExtractFileName(FullFileName);
  3477. switch (ExecuteFileBy)
  3478. {
  3479. case efShell:
  3480. Counter = "LocalFilesExecuted";
  3481. break;
  3482. case efInternalEditor:
  3483. Counter = "LocalFilesOpenedInInternalEditor";
  3484. break;
  3485. case efExternalEditor:
  3486. Counter = "LocalFilesOpenedInExternalEditor";
  3487. break;
  3488. default:
  3489. DebugFail();
  3490. }
  3491. }
  3492. if (!Handled)
  3493. {
  3494. Configuration->Usage->Inc(Counter);
  3495. CustomExecuteFile(Side, ExecuteFileBy, LocalFileName, OriginalFileName,
  3496. ExternalEditor, LocalRootDirectory, RemoteDirectory, false);
  3497. }
  3498. }
  3499. //---------------------------------------------------------------------------
  3500. void __fastcall TCustomScpExplorerForm::ExecuteFile(TOperationSide Side,
  3501. TExecuteFileBy ExecuteFileBy, const TEditorData * ExternalEditor,
  3502. bool AllSelected, bool OnFocused)
  3503. {
  3504. DebugAssert(!WinConfiguration->DisableOpenEdit);
  3505. DebugAssert((ExecuteFileBy == efExternalEditor) ==
  3506. ((ExternalEditor != NULL) && (ExternalEditor->Editor == edExternal)));
  3507. Side = GetSide(Side);
  3508. TCustomDirView * DView = DirView(Side);
  3509. TStrings * FileList =
  3510. AllSelected ?
  3511. DView->CreateFileList(OnFocused, IsSideLocalBrowser(Side)) :
  3512. DView->CreateFocusedFileList(IsSideLocalBrowser(Side));
  3513. try
  3514. {
  3515. DebugAssert(AllSelected || (FileList->Count == 1));
  3516. for (int i = 0; i < FileList->Count; i++)
  3517. {
  3518. UnicodeString ListFileName = FileList->Strings[i];
  3519. UnicodeString FileNameOnly =
  3520. !IsSideLocalBrowser(Side) ? UnixExtractFileName(ListFileName) : ExtractFileName(ListFileName);
  3521. TListItem * Item = DView->FindFileItem(FileNameOnly);
  3522. if (!DView->ItemIsDirectory(Item))
  3523. {
  3524. UnicodeString FullFileName;
  3525. if (!IsSideLocalBrowser(Side))
  3526. {
  3527. FullFileName = DirView(Side)->Path + ListFileName;
  3528. }
  3529. else
  3530. {
  3531. FullFileName = ListFileName;
  3532. }
  3533. TFileMasks::TParams MaskParams;
  3534. MaskParams.Size = DView->ItemFileSize(Item);
  3535. TDateTimePrecision Precision;
  3536. MaskParams.Modification = DView->ItemFileTime(Item, Precision);
  3537. ExecuteFile(Side, ExecuteFileBy, ExternalEditor, FullFileName,
  3538. FileList->Objects[i], MaskParams);
  3539. }
  3540. }
  3541. }
  3542. __finally
  3543. {
  3544. delete FileList;
  3545. }
  3546. }
  3547. //---------------------------------------------------------------------------
  3548. void __fastcall TCustomScpExplorerForm::TemporaryFileCopyParam(TCopyParamType & CopyParam)
  3549. {
  3550. CopyParam.FileNameCase = ncNoChange;
  3551. CopyParam.PreserveRights = false;
  3552. CopyParam.PreserveReadOnly = false;
  3553. CopyParam.ReplaceInvalidChars = true;
  3554. CopyParam.IncludeFileMask = TFileMasks();
  3555. CopyParam.NewerOnly = false;
  3556. CopyParam.FileMask = L"";
  3557. }
  3558. //---------------------------------------------------------------------------
  3559. void __fastcall TCustomScpExplorerForm::ExecutedFileChanged(
  3560. const UnicodeString & FileName, TEditedFileData * Data, HANDLE UploadCompleteEvent, bool & Retry)
  3561. {
  3562. TTerminalManager * Manager = TTerminalManager::Instance();
  3563. if (!IsActiveTerminal(Data->Terminal))
  3564. {
  3565. if (!NonVisualDataModule->Busy)
  3566. {
  3567. UnicodeString FileNameOnly = ExtractFileName(FileName);
  3568. UnicodeString EditInactiveSessionReopenAcceptedCounter = L"EditInactiveSessionReopenAccepted";
  3569. UnicodeString EditInactiveSessionReopenRejectedCounter = L"EditInactiveSessionReopenRejected";
  3570. if (Data->Terminal == NULL)
  3571. {
  3572. TTerminal * SameSiteTerminal = Manager->FindActiveTerminalForSite(Data->SessionData);
  3573. if (SameSiteTerminal != NULL)
  3574. {
  3575. UnicodeString Message =
  3576. FMTLOAD(EDIT_SESSION_REATTACH,
  3577. (FileNameOnly, Data->SessionName, FileNameOnly));
  3578. if (MessageDialog(Message, qtConfirmation, qaOK | qaCancel) == qaOK)
  3579. {
  3580. Data->Terminal = Terminal;
  3581. Data->Queue = Manager->FindQueueForTerminal(Terminal);
  3582. Data->SessionName = Terminal->SessionData->SessionName;
  3583. // We might also overwrite session data
  3584. Configuration->Usage->Inc(EditInactiveSessionReopenAcceptedCounter);
  3585. }
  3586. else
  3587. {
  3588. Configuration->Usage->Inc(EditInactiveSessionReopenRejectedCounter);
  3589. Abort();
  3590. }
  3591. }
  3592. }
  3593. // foreground session should reconnect itself
  3594. else if (Terminal != Data->Terminal)
  3595. {
  3596. UnicodeString Message =
  3597. MainInstructions(
  3598. FMTLOAD(EDIT_SESSION_RECONNECT, (Data->SessionName, FileNameOnly)));
  3599. if (MessageDialog(Message, qtConfirmation, qaOK | qaCancel) == qaOK)
  3600. {
  3601. Manager->SetActiveTerminalWithAutoReconnect(Data->Terminal);
  3602. Configuration->Usage->Inc(EditInactiveSessionReopenAcceptedCounter);
  3603. }
  3604. else
  3605. {
  3606. Configuration->Usage->Inc(EditInactiveSessionReopenRejectedCounter);
  3607. Abort();
  3608. }
  3609. }
  3610. }
  3611. if (!IsActiveTerminal(Data->Terminal))
  3612. {
  3613. Configuration->Usage->Inc(L"EditInactiveSession");
  3614. // Prevent this when not idle (!NonVisualDataModule->Busy)?
  3615. throw Exception(FMTLOAD(EDIT_SESSION_CLOSED2,
  3616. (ExtractFileName(FileName), Data->SessionName)));
  3617. }
  3618. }
  3619. TStrings * FileList = new TStringList();
  3620. try
  3621. {
  3622. FileList->Add(FileName);
  3623. // consider using the same settings (preset) as when the file was downloaded
  3624. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  3625. TemporaryFileCopyParam(CopyParam);
  3626. if (Data->ForceText)
  3627. {
  3628. CopyParam.TransferMode = tmAscii;
  3629. }
  3630. // so i do not need to worry if masking algorithm works in all cases
  3631. // ("" means "copy file name", no masking is actually done)
  3632. if (ExtractFileName(FileName) == Data->OriginalFileName)
  3633. {
  3634. CopyParam.FileMask = L"";
  3635. }
  3636. else
  3637. {
  3638. CopyParam.FileMask = DelimitFileNameMask(Data->OriginalFileName);
  3639. }
  3640. int Params = cpNoConfirmation | cpTemporary;
  3641. if (Terminal->IsCapable[fcBackgroundTransfers])
  3642. {
  3643. DebugAssert(Data->Queue != NULL);
  3644. TQueueItem * QueueItem = new TUploadQueueItem(Data->Terminal, FileList,
  3645. Data->RemoteDirectory, &CopyParam, Params, true, false);
  3646. QueueItem->CompleteEvent = UploadCompleteEvent;
  3647. AddQueueItem(Data->Queue, QueueItem, Data->Terminal);
  3648. }
  3649. else
  3650. {
  3651. if (NonVisualDataModule->Busy)
  3652. {
  3653. Retry = true;
  3654. }
  3655. else
  3656. {
  3657. Terminal->CopyToRemote(FileList, Data->RemoteDirectory, &CopyParam, Params, NULL);
  3658. SetEvent(UploadCompleteEvent);
  3659. }
  3660. }
  3661. }
  3662. __finally
  3663. {
  3664. delete FileList;
  3665. }
  3666. }
  3667. //---------------------------------------------------------------------------
  3668. void __fastcall TCustomScpExplorerForm::ExecutedFileReload(
  3669. const UnicodeString FileName, const TEditedFileData * Data)
  3670. {
  3671. // Sanity check, we should not be busy otherwise user would not be able to click Reload button.
  3672. DebugAssert(!NonVisualDataModule->Busy);
  3673. if (!IsActiveTerminal(Data->Terminal))
  3674. {
  3675. throw Exception(FMTLOAD(EDIT_SESSION_CLOSED_RELOAD,
  3676. (ExtractFileName(FileName), Data->SessionName)));
  3677. }
  3678. TManagedTerminal * PrevSession = TTerminalManager::Instance()->ActiveSession;
  3679. TTerminalManager::Instance()->ActiveSession = Data->Terminal;
  3680. NonVisualDataModule->StartBusy();
  3681. try
  3682. {
  3683. std::unique_ptr<TRemoteFile> File;
  3684. UnicodeString RemoteFileName =
  3685. UnixIncludeTrailingBackslash(Data->RemoteDirectory) + Data->OriginalFileName;
  3686. Terminal->ExceptionOnFail = true;
  3687. try
  3688. {
  3689. TRemoteFile * AFile = NULL;
  3690. Terminal->ReadFile(RemoteFileName, AFile);
  3691. File.reset(AFile);
  3692. if (!File->HaveFullFileName)
  3693. {
  3694. File->FullFileName = RemoteFileName;
  3695. }
  3696. }
  3697. __finally
  3698. {
  3699. Terminal->ExceptionOnFail = false;
  3700. }
  3701. std::unique_ptr<TStrings> FileList(new TStringList());
  3702. FileList->AddObject(RemoteFileName, File.get());
  3703. UnicodeString RootTempDir = Data->LocalRootDirectory;
  3704. UnicodeString TempDir = ExtractFilePath(FileName);
  3705. TemporarilyDownloadFiles(FileList.get(), Data->ForceText, RootTempDir, TempDir, true, true);
  3706. // sanity check, the target file name should be still the same
  3707. DebugAssert(ExtractFileName(FileName) == FileList->Strings[0]);
  3708. }
  3709. __finally
  3710. {
  3711. NonVisualDataModule->EndBusy();
  3712. // it actually may not exist anymore...
  3713. TTerminalManager::Instance()->ActiveSession = PrevSession;
  3714. }
  3715. }
  3716. //---------------------------------------------------------------------------
  3717. void __fastcall TCustomScpExplorerForm::ExecutedFileEarlyClosed(
  3718. const TEditedFileData * Data, bool & KeepOpen)
  3719. {
  3720. // Command is set for external editors only (not for "shell" open).
  3721. if (!Data->Command.IsEmpty())
  3722. {
  3723. bool AnyFound = false;
  3724. bool AnyMDI = false;
  3725. bool AnyNonMDI = false;
  3726. bool AnyDetect = false;
  3727. TEditorList * EditorList = new TEditorList();
  3728. try
  3729. {
  3730. *EditorList = *WinConfiguration->EditorList;
  3731. for (int i = 0; i < EditorList->Count; i++)
  3732. {
  3733. const TEditorPreferences * Editor = EditorList->Editors[i];
  3734. if ((Editor->Data->Editor == edExternal) &&
  3735. (Editor->Data->ExternalEditor == Data->Command))
  3736. {
  3737. AnyFound = true;
  3738. if (Editor->Data->SDIExternalEditor)
  3739. {
  3740. AnyNonMDI = true;
  3741. if (Editor->Data->DetectMDIExternalEditor)
  3742. {
  3743. AnyDetect = true;
  3744. }
  3745. }
  3746. else
  3747. {
  3748. AnyMDI = true;
  3749. }
  3750. }
  3751. }
  3752. bool EnableMDI = false;
  3753. bool DisableDetect = false;
  3754. if (AnyMDI)
  3755. {
  3756. KeepOpen = true;
  3757. if (AnyNonMDI)
  3758. {
  3759. // there is at least one instance of the editor with MDI support enabled,
  3760. // and one with disabled, enable it for all instances
  3761. EnableMDI = true;
  3762. }
  3763. }
  3764. else if (AnyFound && !AnyDetect)
  3765. {
  3766. // at least once instance found but all have MDI autodetection disabled
  3767. // => close the file (default action)
  3768. }
  3769. else
  3770. {
  3771. // no instance of the editor has MDI support enabled
  3772. TMessageParams Params;
  3773. if (AnyFound)
  3774. {
  3775. // there is at least one instance of the editor
  3776. Params.Params |= mpNeverAskAgainCheck;
  3777. }
  3778. unsigned int Answer = MessageDialog(FMTLOAD(EDITOR_EARLY_CLOSED2, (Data->OriginalFileName)), qtWarning,
  3779. qaYes | qaNo, HELP_EDITOR_EARLY_CLOSED, &Params);
  3780. switch (Answer)
  3781. {
  3782. case qaNeverAskAgain:
  3783. DisableDetect = true;
  3784. break;
  3785. case qaNo:
  3786. EnableMDI = true;
  3787. KeepOpen = true;
  3788. break;
  3789. }
  3790. }
  3791. if (AnyFound && (EnableMDI || DisableDetect))
  3792. {
  3793. bool Changed = false;
  3794. for (int i = 0; i < EditorList->Count; i++)
  3795. {
  3796. const TEditorPreferences * Editor = EditorList->Editors[i];
  3797. if ((Editor->Data->Editor == edExternal) &&
  3798. (Editor->Data->ExternalEditor == Data->Command) &&
  3799. ((EnableMDI && Editor->Data->SDIExternalEditor) ||
  3800. (DisableDetect && Editor->Data->DetectMDIExternalEditor)))
  3801. {
  3802. Changed = true;
  3803. TEditorPreferences * UpdatedEditor = new TEditorPreferences(*Editor);
  3804. if (EnableMDI)
  3805. {
  3806. UpdatedEditor->GetData()->SDIExternalEditor = false;
  3807. }
  3808. if (DisableDetect)
  3809. {
  3810. UpdatedEditor->GetData()->DetectMDIExternalEditor = false;
  3811. }
  3812. EditorList->Change(i, UpdatedEditor);
  3813. }
  3814. }
  3815. if (Changed)
  3816. {
  3817. WinConfiguration->EditorList = EditorList;
  3818. }
  3819. }
  3820. }
  3821. __finally
  3822. {
  3823. delete EditorList;
  3824. }
  3825. }
  3826. else
  3827. {
  3828. // "open" case
  3829. MessageDialog(FMTLOAD(APP_EARLY_CLOSED, (Data->OriginalFileName)), qtWarning,
  3830. qaOK, HELP_APP_EARLY_CLOSED);
  3831. }
  3832. }
  3833. //---------------------------------------------------------------------------
  3834. void __fastcall TCustomScpExplorerForm::ExecutedFileUploadComplete(TObject * Sender)
  3835. {
  3836. EditorFormFileUploadComplete(DebugNotNull(dynamic_cast<TForm *>(Sender)));
  3837. }
  3838. //---------------------------------------------------------------------------
  3839. void __fastcall TCustomScpExplorerForm::RemoteDirViewEnter(TObject * /*Sender*/)
  3840. {
  3841. SideEnter(osRemote);
  3842. }
  3843. //---------------------------------------------------------------------------
  3844. void __fastcall TCustomScpExplorerForm::RemoteDriveViewEnter(TObject * /*Sender*/)
  3845. {
  3846. MakeNextInTabOrder(RemoteDirPanel, RemoteDrivePanel);
  3847. SideEnter(osRemote);
  3848. }
  3849. //---------------------------------------------------------------------------
  3850. void __fastcall TCustomScpExplorerForm::SideEnter(TOperationSide Side)
  3851. {
  3852. FCurrentSide = Side;
  3853. if (Visible)
  3854. {
  3855. UpdateControls();
  3856. }
  3857. }
  3858. //---------------------------------------------------------------------------
  3859. void __fastcall TCustomScpExplorerForm::FileDeleted(TOperationSide Side, const UnicodeString & FileName, bool Success)
  3860. {
  3861. VisualiseOperationFinished(Side, FileName, Success);
  3862. if (DebugAlwaysTrue(FDeletedFiles != NULL) && Success)
  3863. {
  3864. FDeletedFiles->Add(FileName);
  3865. }
  3866. }
  3867. //---------------------------------------------------------------------------
  3868. void __fastcall TCustomScpExplorerForm::DeleteFiles(TOperationSide Side,
  3869. TStrings * FileList, bool Alternative)
  3870. {
  3871. TCustomDirView * DView = DirView(Side);
  3872. DView->SaveSelection();
  3873. DView->SaveSelectedNames();
  3874. DebugAssert(!FAlternativeDelete);
  3875. FAlternativeDelete = Alternative;
  3876. try
  3877. {
  3878. if (!IsSideLocalBrowser(Side))
  3879. {
  3880. DebugAssert(Terminal != NULL);
  3881. // Clone the file list as it may refer to current directory files, which get destroyed, when the directory is reloaded after the operation
  3882. std::unique_ptr<TStrings> PermanentFileList(TRemoteFileList::CloneStrings(FileList));
  3883. std::unique_ptr<TStringList> DeletedFiles(new TStringList());
  3884. DeletedFiles->CaseSensitive = DebugAlwaysTrue(!IsSideLocalBrowser(Side));
  3885. int Params = FLAGMASK(Alternative, dfAlternative);
  3886. try
  3887. {
  3888. TValueRestorer<TStrings *> DeletedFilesRestorer(FDeletedFiles);
  3889. DebugAssert(FDeletedFiles == NULL);
  3890. FDeletedFiles = DeletedFiles.get();
  3891. TValueRestorer<TFileOperationFinishedEvent> OnFileOperationFinishedRestorer(FOnFileOperationFinished);
  3892. FOnFileOperationFinished = FileDeleted;
  3893. FMoveToQueue = false;
  3894. Terminal->DeleteFiles(FileList, Params);
  3895. // Probably not needed for deleting, just for consistency with transfer code
  3896. if (FMoveToQueue)
  3897. {
  3898. Abort();
  3899. }
  3900. }
  3901. catch (EAbort &)
  3902. {
  3903. if (FMoveToQueue)
  3904. {
  3905. DeletedFiles->Sorted = true;
  3906. for (int Index = 0; Index < PermanentFileList->Count; Index++)
  3907. {
  3908. if (DeletedFiles->IndexOf(PermanentFileList->Strings[Index]) >= 0)
  3909. {
  3910. // We should always be deleting the first item => what can be used to optimize this code, if needed
  3911. DebugAssert(Index == 0);
  3912. PermanentFileList->Delete(Index);
  3913. Index--;
  3914. }
  3915. }
  3916. Configuration->Usage->Inc("MovesToBackgroundDelete");
  3917. TQueueItem * QueueItem = new TDeleteQueueItem(Terminal, PermanentFileList.get(), Params);
  3918. AddQueueItem(Queue, QueueItem, Terminal);
  3919. ClearOperationSelection(osRemote);
  3920. }
  3921. }
  3922. }
  3923. else
  3924. {
  3925. if (IsLocalBrowserMode())
  3926. {
  3927. Configuration->Usage->Inc(L"LocalLocalDeletes");
  3928. }
  3929. TValueRestorer<TOperationSide> ProgressSideRestorer(FProgressSide);
  3930. FProgressSide = FCurrentSide;
  3931. try
  3932. {
  3933. TTerminalManager::Instance()->LocalTerminal->DeleteLocalFiles(FileList, FLAGMASK(Alternative, dfAlternative));
  3934. }
  3935. __finally
  3936. {
  3937. ReloadLocalDirectory();
  3938. }
  3939. }
  3940. FAlternativeDelete = false;
  3941. }
  3942. catch(...)
  3943. {
  3944. FAlternativeDelete = false;
  3945. DView->DiscardSavedSelection();
  3946. throw;
  3947. }
  3948. DView->RestoreSelection();
  3949. }
  3950. //---------------------------------------------------------------------------
  3951. void __fastcall TCustomScpExplorerForm::LockFiles(TStrings * FileList, bool Lock)
  3952. {
  3953. DebugAssert(Terminal);
  3954. DebugAssert(!IsLocalBrowserMode());
  3955. RemoteDirView->SaveSelection();
  3956. RemoteDirView->SaveSelectedNames();
  3957. try
  3958. {
  3959. if (Lock)
  3960. {
  3961. Terminal->LockFiles(FileList);
  3962. }
  3963. else
  3964. {
  3965. Terminal->UnlockFiles(FileList);
  3966. }
  3967. }
  3968. catch(...)
  3969. {
  3970. RemoteDirView->DiscardSavedSelection();
  3971. throw;
  3972. }
  3973. RemoteDirView->RestoreSelection();
  3974. }
  3975. //---------------------------------------------------------------------------
  3976. bool __fastcall TCustomScpExplorerForm::RemoteTransferDialog(TManagedTerminal *& Session,
  3977. TStrings * FileList, UnicodeString & Target, UnicodeString & FileMask, bool & DirectCopy,
  3978. bool NoConfirmation, bool Move)
  3979. {
  3980. DebugAssert(Terminal != NULL);
  3981. DebugAssert(Terminal == TTerminalManager::Instance()->ActiveTerminal);
  3982. DebugAssert(!IsLocalBrowserMode());
  3983. if (Session == NULL)
  3984. {
  3985. Session = Terminal;
  3986. }
  3987. if (Session == Terminal)
  3988. {
  3989. if (RemoteDriveView->DropTarget != NULL)
  3990. {
  3991. Target = RemoteDriveView->NodePathName(RemoteDriveView->DropTarget);
  3992. }
  3993. else if (RemoteDirView->DropTarget != NULL)
  3994. {
  3995. DebugAssert(RemoteDirView->ItemIsDirectory(RemoteDirView->DropTarget));
  3996. Target = RemoteDirView->ItemFullFileName(RemoteDirView->DropTarget);
  3997. }
  3998. else
  3999. {
  4000. Target = RemoteDirView->Path;
  4001. }
  4002. }
  4003. else
  4004. {
  4005. Target = GetSessionPath(Session, osRemote);
  4006. }
  4007. Target = UnixIncludeTrailingBackslash(Target);
  4008. if (FileList->Count == 1)
  4009. {
  4010. FileMask = DelimitFileNameMask(UnixExtractFileName(FileList->Strings[0]));
  4011. }
  4012. else
  4013. {
  4014. FileMask = AnyMask;
  4015. }
  4016. DirectCopy = Terminal->IsCapable[fcRemoteCopy] || Terminal->IsCapable[fcSecondaryShell];
  4017. bool Result = true;
  4018. if (!NoConfirmation)
  4019. {
  4020. bool Multi = (FileList->Count > 1);
  4021. if (Move)
  4022. {
  4023. Result = DoRemoteMoveDialog(Multi, Target, FileMask);
  4024. }
  4025. else
  4026. {
  4027. std::unique_ptr<TStrings> Sessions(new TStringList);
  4028. std::unique_ptr<TStrings> Directories(new TStringList);
  4029. TStrings * SessionList = TTerminalManager::Instance()->SessionList;
  4030. for (int Index = 0; Index < SessionList->Count; Index++)
  4031. {
  4032. TManagedTerminal * ASession = DebugNotNull(dynamic_cast<TManagedTerminal *>(SessionList->Objects[Index]));
  4033. if (IsActiveTerminal(ASession) && DebugAlwaysTrue(!ASession->LocalBrowser))
  4034. {
  4035. Sessions->AddObject(SessionList->Strings[Index], ASession);
  4036. Directories->Add(GetSessionPath(ASession, osRemote));
  4037. }
  4038. }
  4039. TDirectRemoteCopy AllowDirectCopy;
  4040. if (Terminal->IsCapable[fcRemoteCopy] || Terminal->CommandSessionOpened)
  4041. {
  4042. DebugAssert(DirectCopy);
  4043. AllowDirectCopy = drcAllow;
  4044. }
  4045. else if (Terminal->IsCapable[fcSecondaryShell])
  4046. {
  4047. DebugAssert(DirectCopy);
  4048. AllowDirectCopy = drcConfirmCommandSession;
  4049. }
  4050. else
  4051. {
  4052. DebugAssert(!DirectCopy);
  4053. AllowDirectCopy = drcDisallow;
  4054. }
  4055. void * ASession = Session;
  4056. Result = DoRemoteCopyDialog(
  4057. Sessions.get(), Directories.get(), AllowDirectCopy, Multi, ASession, Target, FileMask, DirectCopy, Terminal);
  4058. Session = static_cast<TManagedTerminal *>(ASession);
  4059. }
  4060. }
  4061. return Result;
  4062. }
  4063. //---------------------------------------------------------------------------
  4064. bool __fastcall TCustomScpExplorerForm::RemoteTransferFiles(
  4065. TStrings * FileList, bool NoConfirmation, bool Move, TManagedTerminal * Session)
  4066. {
  4067. DebugAssert(!IsLocalBrowserMode());
  4068. bool DirectCopy;
  4069. UnicodeString Target, FileMask;
  4070. bool Result = RemoteTransferDialog(Session, FileList, Target, FileMask, DirectCopy, NoConfirmation, Move);
  4071. if (Result)
  4072. {
  4073. if (!Move && !DirectCopy)
  4074. {
  4075. Configuration->Usage->Inc("RemoteCopyTemp");
  4076. UnicodeString RootTempDir;
  4077. UnicodeString TempDir;
  4078. TemporarilyDownloadFiles(FileList, false, RootTempDir, TempDir, false, false);
  4079. TStrings * TemporaryFilesList = new TStringList();
  4080. try
  4081. {
  4082. TMakeLocalFileListParams MakeFileListParam;
  4083. MakeFileListParam.FileList = TemporaryFilesList;
  4084. MakeFileListParam.FileTimes = NULL;
  4085. MakeFileListParam.IncludeDirs = true;
  4086. MakeFileListParam.Recursive = false;
  4087. ProcessLocalDirectory(TempDir, Terminal->MakeLocalFileList, &MakeFileListParam);
  4088. TTerminalManager::Instance()->ActiveSession = Session;
  4089. if (TemporaryFilesList->Count > 0)
  4090. {
  4091. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  4092. CopyParam.FileMask = FileMask;
  4093. DebugAssert(!FAutoOperation);
  4094. FAutoOperation = true;
  4095. Terminal->CopyToRemote(TemporaryFilesList, Target, &CopyParam, cpTemporary, NULL);
  4096. }
  4097. }
  4098. __finally
  4099. {
  4100. delete TemporaryFilesList;
  4101. FAutoOperation = false;
  4102. if (!RootTempDir.IsEmpty())
  4103. {
  4104. RecursiveDeleteFile(ExcludeTrailingBackslash(RootTempDir), false);
  4105. }
  4106. }
  4107. }
  4108. else
  4109. {
  4110. RemoteDirView->SaveSelection();
  4111. RemoteDirView->SaveSelectedNames();
  4112. try
  4113. {
  4114. if (Move)
  4115. {
  4116. Configuration->Usage->Inc("RemoteMove");
  4117. Terminal->MoveFiles(FileList, Target, FileMask);
  4118. }
  4119. else
  4120. {
  4121. Configuration->Usage->Inc("RemoteCopyDirect");
  4122. DebugAssert(DirectCopy);
  4123. DebugAssert(Session == Terminal);
  4124. if (Terminal->IsCapable[fcRemoteCopy] ||
  4125. Terminal->CommandSessionOpened ||
  4126. CommandSessionFallback())
  4127. {
  4128. Terminal->CopyFiles(FileList, Target, FileMask);
  4129. }
  4130. }
  4131. }
  4132. catch(...)
  4133. {
  4134. RemoteDirView->DiscardSavedSelection();
  4135. throw;
  4136. }
  4137. RemoteDirView->RestoreSelection();
  4138. }
  4139. }
  4140. return Result;
  4141. }
  4142. //---------------------------------------------------------------------------
  4143. void __fastcall TCustomScpExplorerForm::CreateRemoteDirectory(
  4144. const UnicodeString & Path, TRemoteProperties & Properties)
  4145. {
  4146. DebugAssert(!IsLocalBrowserMode());
  4147. Properties.Valid = Properties.Valid << vpEncrypt;
  4148. Properties.Encrypt = GUIConfiguration->CurrentCopyParam.EncryptNewFiles;
  4149. RemoteDirView->CreateDirectoryEx(Path, &Properties);
  4150. }
  4151. //---------------------------------------------------------------------------
  4152. void __fastcall TCustomScpExplorerForm::CreateDirectory(TOperationSide Side)
  4153. {
  4154. Side = GetSide(Side);
  4155. TRemoteProperties Properties = GUIConfiguration->NewDirectoryProperties;
  4156. TRemoteProperties * AProperties = (!IsSideLocalBrowser(Side) ? &Properties : NULL);
  4157. UnicodeString Name = LoadStr(NEW_FOLDER);
  4158. int AllowedChanges =
  4159. FLAGMASK(!IsSideLocalBrowser(Side) && Terminal->IsCapable[fcModeChanging], cpMode);
  4160. bool SaveSettings = false;
  4161. if (DoCreateDirectoryDialog(Name, AProperties, AllowedChanges, SaveSettings))
  4162. {
  4163. TWindowLock Lock(this);
  4164. if (!IsSideLocalBrowser(Side))
  4165. {
  4166. if (SaveSettings)
  4167. {
  4168. GUIConfiguration->NewDirectoryProperties = Properties;
  4169. }
  4170. CreateRemoteDirectory(Name, Properties);
  4171. }
  4172. else
  4173. {
  4174. DirView(Side)->CreateDirectory(Name);
  4175. }
  4176. }
  4177. }
  4178. //---------------------------------------------------------------------------
  4179. void __fastcall TCustomScpExplorerForm::HomeDirectory(TOperationSide Side)
  4180. {
  4181. TWindowLock Lock(this);
  4182. DirView(Side)->ExecuteHomeDirectory();
  4183. }
  4184. //---------------------------------------------------------------------------
  4185. void __fastcall TCustomScpExplorerForm::OpenDirectory(TOperationSide Side)
  4186. {
  4187. DoOpenDirectoryDialog(odBrowse, Side);
  4188. }
  4189. //---------------------------------------------------------------------------
  4190. bool __fastcall TCustomScpExplorerForm::OpenBookmark(TOperationSide Side, TBookmark * Bookmark)
  4191. {
  4192. TOperationSide BookmarkSide = IsLocalBrowserMode() ? osLocal : Side;
  4193. UnicodeString Path = Bookmark->GetSideDirectory(BookmarkSide);
  4194. bool Result = !Path.IsEmpty();
  4195. if (Result)
  4196. {
  4197. TryOpenDirectory(Side, Path);
  4198. }
  4199. return Result;
  4200. }
  4201. //---------------------------------------------------------------------------
  4202. void __fastcall TCustomScpExplorerForm::CalculateSize(
  4203. TStrings * FileList, __int64 & Size, TCalculateSizeStats & Stats,
  4204. bool & Close)
  4205. {
  4206. // terminal can be already closed (e.g. dropped connection)
  4207. if (Terminal != NULL)
  4208. {
  4209. try
  4210. {
  4211. TCalculateSizeParams Params;
  4212. Params.Stats = &Stats;
  4213. Terminal->CalculateFilesSize(FileList, Size, Params);
  4214. }
  4215. catch(...)
  4216. {
  4217. if (!Terminal->Active)
  4218. {
  4219. Close = true;
  4220. }
  4221. throw;
  4222. }
  4223. }
  4224. }
  4225. //---------------------------------------------------------------------------
  4226. void __fastcall TCustomScpExplorerForm::CalculateChecksum(const UnicodeString & Alg,
  4227. TStrings * FileList, TCalculatedChecksumEvent OnCalculatedChecksum,
  4228. bool & Close)
  4229. {
  4230. if ((Terminal != NULL) && // terminal can be already closed (e.g. dropped connection)
  4231. EnsureCommandSessionFallback(fcCalculatingChecksum))
  4232. {
  4233. Configuration->Usage->Inc(L"ChecksumCalculated");
  4234. try
  4235. {
  4236. Terminal->CalculateFilesChecksum(Alg, FileList, OnCalculatedChecksum);
  4237. }
  4238. catch(...)
  4239. {
  4240. if (!Terminal->Active)
  4241. {
  4242. Close = true;
  4243. }
  4244. throw;
  4245. }
  4246. }
  4247. }
  4248. //---------------------------------------------------------------------------
  4249. void TCustomScpExplorerForm::LoadFilesProperties(TStrings * FileList)
  4250. {
  4251. if (Terminal->LoadFilesProperties(FileList))
  4252. {
  4253. RemoteDirView->Invalidate();
  4254. }
  4255. }
  4256. //---------------------------------------------------------------------------
  4257. bool __fastcall TCustomScpExplorerForm::SetProperties(TOperationSide Side, TStrings * FileList)
  4258. {
  4259. bool Result;
  4260. if (!IsSideLocalBrowser(Side))
  4261. {
  4262. TRemoteTokenList * GroupList = NULL;
  4263. TRemoteTokenList * UserList = NULL;
  4264. try
  4265. {
  4266. TRemoteProperties CurrentProperties;
  4267. LoadFilesProperties(FileList);
  4268. bool CapableGroupChanging = Terminal->IsCapable[fcGroupChanging];
  4269. bool CapableOwnerChanging = Terminal->IsCapable[fcOwnerChanging];
  4270. if (CapableGroupChanging || CapableOwnerChanging)
  4271. {
  4272. if (CapableGroupChanging)
  4273. {
  4274. GroupList = Terminal->Groups->Duplicate();
  4275. }
  4276. if (CapableOwnerChanging)
  4277. {
  4278. UserList = Terminal->Users->Duplicate();
  4279. }
  4280. TRemoteDirectory * Files = Terminal->Files;
  4281. int Count = Files->Count;
  4282. if (Count > 100)
  4283. {
  4284. Count = 100;
  4285. }
  4286. for (int Index = 0; Index < Count; Index++)
  4287. {
  4288. TRemoteFile * File = Files->Files[Index];
  4289. if (CapableGroupChanging)
  4290. {
  4291. GroupList->AddUnique(File->Group);
  4292. }
  4293. if (CapableOwnerChanging)
  4294. {
  4295. UserList->AddUnique(File->Owner);
  4296. }
  4297. }
  4298. // if we haven't collected tokens for all files in current directory,
  4299. // make sure we collect them at least for all selected files.
  4300. // (note that so far the files in FileList has to be from current direcotry)
  4301. if (Count < Files->Count)
  4302. {
  4303. for (int Index = 0; Index < FileList->Count; Index++)
  4304. {
  4305. TRemoteFile * File = (TRemoteFile *)(FileList->Objects[Index]);
  4306. if (CapableGroupChanging)
  4307. {
  4308. GroupList->AddUnique(File->Group);
  4309. }
  4310. if (CapableOwnerChanging)
  4311. {
  4312. UserList->AddUnique(File->Owner);
  4313. }
  4314. }
  4315. }
  4316. }
  4317. CurrentProperties = TRemoteProperties::CommonProperties(FileList);
  4318. bool CapableAclChanging = Terminal->IsCapable[fcAclChangingFiles];
  4319. for (int Index = 0; (Index < FileList->Count) && CapableAclChanging; Index++)
  4320. {
  4321. if (dynamic_cast<TRemoteFile *>(FileList->Objects[Index])->IsDirectory)
  4322. {
  4323. CapableAclChanging = false;
  4324. CurrentProperties.Valid = CurrentProperties.Valid >> vpRights;
  4325. }
  4326. }
  4327. int Flags = 0;
  4328. if (Terminal->IsCapable[fcModeChanging]) Flags |= cpMode;
  4329. if (CapableAclChanging) Flags |= cpAcl;
  4330. if (CapableOwnerChanging) Flags |= cpOwner;
  4331. if (CapableGroupChanging) Flags |= cpGroup;
  4332. TCalculateChecksumEvent CalculateChecksumEvent = NULL;
  4333. if (Terminal->IsCapable[fcCalculatingChecksum] ||
  4334. (Terminal->IsCapable[fcSecondaryShell] && !Terminal->IsEncryptingFiles()))
  4335. {
  4336. CalculateChecksumEvent = CalculateChecksum;
  4337. }
  4338. std::unique_ptr<TStrings> ChecksumAlgs(new TStringList());
  4339. Terminal->GetSupportedChecksumAlgs(ChecksumAlgs.get());
  4340. TRemoteProperties NewProperties = CurrentProperties;
  4341. Result =
  4342. DoPropertiesDialog(FileList, RemoteDirView->PathName,
  4343. GroupList, UserList, ChecksumAlgs.get(), &NewProperties, Flags,
  4344. Terminal->IsCapable[fcGroupOwnerChangingByID],
  4345. CalculateSize, CalculateChecksumEvent);
  4346. if (Result)
  4347. {
  4348. NewProperties = TRemoteProperties::ChangedProperties(CurrentProperties, NewProperties);
  4349. Terminal->ChangeFilesProperties(FileList, &NewProperties);
  4350. }
  4351. }
  4352. __finally
  4353. {
  4354. delete GroupList;
  4355. delete UserList;
  4356. }
  4357. }
  4358. else
  4359. {
  4360. DirView(Side)->DisplayPropertiesMenu();
  4361. Result = true;
  4362. }
  4363. return Result;
  4364. }
  4365. //---------------------------------------------------------------------------
  4366. void __fastcall TCustomScpExplorerForm::KeyProcessed(Word & Key, TShiftState Shift)
  4367. {
  4368. if (Shift * AllKeyShiftStates() == (TShiftState() << ssAlt))
  4369. {
  4370. FIgnoreNextDialogChar = Key;
  4371. }
  4372. Key = 0;
  4373. }
  4374. //---------------------------------------------------------------------------
  4375. void __fastcall TCustomScpExplorerForm::CheckCustomCommandShortCut(
  4376. TCustomCommandList * List, Word & Key, Classes::TShiftState Shift, TShortCut KeyShortCut)
  4377. {
  4378. const TCustomCommandType * Command = List->Find(KeyShortCut);
  4379. if (Command != NULL)
  4380. {
  4381. KeyProcessed(Key, Shift);
  4382. if (CustomCommandState(*Command, false, ccltAll) > 0)
  4383. {
  4384. TAutoFlag DontCopyCommandToClipboardFlag(DontCopyCommandToClipboard);
  4385. ExecuteFileOperationCommand(foCustomCommand, osRemote,
  4386. false, false, const_cast<TCustomCommandType *>(Command));
  4387. }
  4388. }
  4389. }
  4390. //---------------------------------------------------------------------------
  4391. void __fastcall TCustomScpExplorerForm::KeyDown(Word & Key, Classes::TShiftState Shift)
  4392. {
  4393. if (!NonVisualDataModule->Busy)
  4394. {
  4395. if (QueueView3->Focused() && (QueueView3->OnKeyDown != NULL))
  4396. {
  4397. QueueView3->OnKeyDown(QueueView3, Key, Shift);
  4398. }
  4399. if ((Key != 0) && !DirView(osCurrent)->IsEditing())
  4400. {
  4401. TShortCut KeyShortCut = ShortCut(Key, Shift);
  4402. for (int Index = 0; Index < NonVisualDataModule->ExplorerActions->ActionCount; Index++)
  4403. {
  4404. // Note that handling shortcuts on panel (current control) popup menu has precendence over this.
  4405. // But so far it's not a problem, as it does what we want.
  4406. TAction * Action = (TAction *)NonVisualDataModule->ExplorerActions->Actions[Index];
  4407. if (((Action->ShortCut == KeyShortCut) ||
  4408. (Action->SecondaryShortCuts->IndexOfShortCut(KeyShortCut) >= 0)) &&
  4409. AllowedAction(Action, aaShortCut))
  4410. {
  4411. // Has to be called before the action as the dialog char is already in queue.
  4412. // So when the action consumes message queue, we already need to have the
  4413. // FIgnoreNextDialogChar set
  4414. KeyProcessed(Key, Shift);
  4415. // Reset reference to previous component (when menu/toolbar was clicked).
  4416. // Needed to detect that action was invoked by keyboard shortcut
  4417. // in TNonVisualDataModule::ExplorerActionsExecute
  4418. Action->ActionComponent = NULL;
  4419. Action->Execute();
  4420. return;
  4421. }
  4422. }
  4423. for (int i = 0; i < TTerminalManager::Instance()->Count; i++)
  4424. {
  4425. if (NonVisualDataModule->OpenSessionShortCut(i) == KeyShortCut)
  4426. {
  4427. KeyProcessed(Key, Shift);
  4428. TTerminalManager::Instance()->ActiveSessionIndex = i;
  4429. return;
  4430. }
  4431. }
  4432. if (Key == VK_TAB && Shift.Contains(ssCtrl))
  4433. {
  4434. KeyProcessed(Key, Shift);
  4435. TTerminalManager::Instance()->CycleTerminals(!Shift.Contains(ssShift));
  4436. }
  4437. TShortCut CustomShortCut = NormalizeCustomShortCut(KeyShortCut);
  4438. if (IsCustomShortCut(CustomShortCut))
  4439. {
  4440. CheckCustomCommandShortCut(WinConfiguration->CustomCommandList, Key, Shift, CustomShortCut);
  4441. CheckCustomCommandShortCut(WinConfiguration->ExtensionList, Key, Shift, CustomShortCut);
  4442. if (WinConfiguration->SharedBookmarks != NULL)
  4443. {
  4444. TBookmark * Bookmark = WinConfiguration->SharedBookmarks->FindByShortCut(CustomShortCut);
  4445. if ((Bookmark != NULL) &&
  4446. OpenBookmark(FCurrentSide, Bookmark))
  4447. {
  4448. KeyProcessed(Key, Shift);
  4449. }
  4450. }
  4451. }
  4452. }
  4453. }
  4454. TForm::KeyDown(Key, Shift);
  4455. }
  4456. //---------------------------------------------------------------------------
  4457. void __fastcall TCustomScpExplorerForm::InitStatusBar()
  4458. {
  4459. bool OpenedTerminal = false;
  4460. UnicodeString ProtocolName;
  4461. UnicodeString ProtocolNameHint;
  4462. UnicodeString SecurityProtocolName;
  4463. UnicodeString DurationHint;
  4464. // See the comment in TScpCommanderForm::UpdateControls()
  4465. TTerminal * ActiveTerminal = TTerminalManager::Instance()->ActiveTerminal;
  4466. if (ActiveTerminal != NULL)
  4467. {
  4468. // Active is not enough here
  4469. if (ActiveTerminal->Status == ssOpened)
  4470. {
  4471. const TSessionInfo & SessionInfo = ActiveTerminal->GetSessionInfo();
  4472. const TFileSystemInfo & FileSystemInfo = ActiveTerminal->GetFileSystemInfo();
  4473. if (FileSystemInfo.ProtocolName.IsEmpty())
  4474. {
  4475. ProtocolName = SessionInfo.ProtocolName;
  4476. }
  4477. else
  4478. {
  4479. ProtocolName = FileSystemInfo.ProtocolName;
  4480. }
  4481. ProtocolNameHint = LoadStr(STATUS_PROTOCOL_HINT);
  4482. SecurityProtocolName = SessionInfo.SecurityProtocolName;
  4483. DurationHint = LoadStr(STATUS_DURATION_HINT);
  4484. OpenedTerminal = true;
  4485. }
  4486. else if (ActiveTerminal->Status > ssClosed)
  4487. {
  4488. ProtocolName = LoadStr(STATUS_CONNECTING);
  4489. }
  4490. else
  4491. {
  4492. ProtocolName = LoadStr(STATUS_NOT_CONNECTED2);
  4493. }
  4494. }
  4495. TTBXStatusBar * SessionStatusBar = (TTBXStatusBar *)GetComponent(fcStatusBar);
  4496. int Offset = SessionStatusBar->Panels->Count - SessionPanelCount;
  4497. TTBXStatusPanel * ProtocolStatusPanel = SessionStatusBar->Panels->Items[Offset + 0];
  4498. ProtocolStatusPanel->ViewPriority = (ActiveTerminal != NULL) ? 100 : 0;
  4499. if (ProtocolStatusPanel->Caption != ProtocolName)
  4500. {
  4501. ProtocolStatusPanel->Caption = ProtocolName;
  4502. std::unique_ptr<TCanvas> Canvas(CreateControlCanvas(SessionStatusBar));
  4503. ProtocolStatusPanel->Size = Canvas->TextWidth(ProtocolName) + ScaleByTextHeight(SessionStatusBar, 32);
  4504. }
  4505. ProtocolStatusPanel->Hint = ProtocolNameHint;
  4506. TTBXStatusPanel * SecurityStatusPanel = SessionStatusBar->Panels->Items[Offset + 1];
  4507. SecurityStatusPanel->ViewPriority = OpenedTerminal ? 98 : 0;
  4508. bool SecurityEnabled = !SecurityProtocolName.IsEmpty();
  4509. SecurityStatusPanel->Enabled = SecurityEnabled;
  4510. UnicodeString SecurityProtocolHint;
  4511. if (SecurityEnabled)
  4512. {
  4513. SecurityProtocolHint = FMTLOAD(STATUS_SECURE, (SecurityProtocolName));
  4514. }
  4515. else
  4516. {
  4517. SecurityProtocolHint = LoadStr(STATUS_INSECURE);
  4518. }
  4519. SecurityStatusPanel->Hint = SecurityProtocolHint;
  4520. TTBXStatusPanel * DurationStatusPanel = SessionStatusBar->Panels->Items[Offset + 2];
  4521. DurationStatusPanel->ViewPriority = OpenedTerminal ? 99 : 0;
  4522. DurationStatusPanel->Hint = DurationHint;
  4523. UpdateStatusBar();
  4524. }
  4525. //---------------------------------------------------------------------------
  4526. void __fastcall TCustomScpExplorerForm::UpdateStatusBar()
  4527. {
  4528. TTBXStatusBar * SessionStatusBar = (TTBXStatusBar *)GetComponent(fcStatusBar);
  4529. DebugAssert(SessionStatusBar != NULL);
  4530. TTBXStatusPanel * NoteStatusPanel = SessionStatusBar->Panels->Items[0];
  4531. if (!FNote.IsEmpty())
  4532. {
  4533. NoteStatusPanel->Caption = FNote;
  4534. }
  4535. else
  4536. {
  4537. UpdateStatusPanelText(NoteStatusPanel);
  4538. }
  4539. NoteStatusPanel->Hint = FNoteHints;
  4540. UnicodeString LoginTime;
  4541. // See the comment in TScpCommanderForm::UpdateControls()
  4542. TTerminal * ActiveTerminal = TTerminalManager::Instance()->ActiveTerminal;
  4543. // Active is not enough here
  4544. if ((ActiveTerminal != NULL) && (ActiveTerminal->Status == ssOpened))
  4545. {
  4546. const TSessionInfo & SessionInfo = ActiveTerminal->GetSessionInfo();
  4547. LoginTime = FormatDateTimeSpan(Configuration->TimeFormat, Now() - SessionInfo.LoginTime);
  4548. }
  4549. TTBXStatusPanel * DurationStatusPanel = SessionStatusBar->Panels->Items[SessionStatusBar->Panels->Count - 1];
  4550. DurationStatusPanel->Caption = LoginTime;
  4551. }
  4552. //---------------------------------------------------------------------------
  4553. void __fastcall TCustomScpExplorerForm::UpdateStatusPanelText(TTBXStatusPanel * Panel)
  4554. {
  4555. Panel->Caption = L"";
  4556. }
  4557. //---------------------------------------------------------------------------
  4558. void __fastcall TCustomScpExplorerForm::Idle()
  4559. {
  4560. if (FShowing ||
  4561. // Particularly for
  4562. // - Standalone editing
  4563. // - Detecting closed connection and automatically reconnect it while waiting for changes
  4564. // while "keeping remote directory up to date".
  4565. StandaloneOperation)
  4566. {
  4567. FEditorManager->Check();
  4568. // make sure that Idle is called before update queue, as it may invoke QueueEvent
  4569. // that needs to know if queue view is visible (and it may be closed after queue update)
  4570. TTerminalManager::Instance()->Idle(FDoNotIdleCurrentTerminal > 0);
  4571. }
  4572. if (FShowing)
  4573. {
  4574. if (!NonVisualDataModule->Busy &&
  4575. // Menu is opened or drag&drop is going on
  4576. (Mouse->Capture == NULL))
  4577. {
  4578. if (FRefreshRemoteDirectory)
  4579. {
  4580. if ((Terminal != NULL) && (Terminal->Status == ssOpened))
  4581. {
  4582. Terminal->RefreshDirectory();
  4583. }
  4584. FRefreshRemoteDirectory = false;
  4585. }
  4586. if (FRefreshLocalDirectory)
  4587. {
  4588. ReloadLocalDirectory();
  4589. FRefreshLocalDirectory = false;
  4590. }
  4591. if (WinConfiguration->RefreshRemotePanel)
  4592. {
  4593. if ((Terminal != NULL) && (Terminal->Status == ssOpened) &&
  4594. (Now() - Terminal->DirectoryLoaded > WinConfiguration->RefreshRemotePanelInterval))
  4595. {
  4596. RemoteDirView->ReloadDirectory();
  4597. }
  4598. }
  4599. }
  4600. }
  4601. if (FShowing || StandaloneOperation)
  4602. {
  4603. if (FQueueStatusInvalidated)
  4604. {
  4605. UpdateQueueStatus(false);
  4606. }
  4607. RefreshQueueItems();
  4608. }
  4609. if (FShowing)
  4610. {
  4611. UpdateStatusBar();
  4612. }
  4613. if (FClipboardFakeMonitorsPendingReset && !FDownloadingFromClipboard)
  4614. {
  4615. FClipboardFakeMonitorsPendingReset = false;
  4616. FClipboardFakeMonitors.reset(NULL);
  4617. }
  4618. FIgnoreNextDialogChar = 0;
  4619. }
  4620. //---------------------------------------------------------------------------
  4621. void __fastcall TCustomScpExplorerForm::UserActionTimer(TObject * /*Sender*/)
  4622. {
  4623. try
  4624. {
  4625. FUserActionTimer->Enabled = false;
  4626. if (IsQueueAutoPopup() && (FPendingQueueActionItem != NULL))
  4627. {
  4628. if (TQueueItem::IsUserActionStatus(FPendingQueueActionItem->Status))
  4629. {
  4630. FPendingQueueActionItem->ProcessUserAction();
  4631. }
  4632. }
  4633. }
  4634. __finally
  4635. {
  4636. FPendingQueueActionItem = NULL;
  4637. }
  4638. }
  4639. //---------------------------------------------------------------------------
  4640. void __fastcall TCustomScpExplorerForm::ApplicationMinimize(TObject * /*Sender*/)
  4641. {
  4642. if (WinConfiguration->MinimizeToTray)
  4643. {
  4644. UpdateTrayIcon();
  4645. FTrayIcon->Visible = true;
  4646. if (Visible)
  4647. {
  4648. ShowWindow(Handle, SW_HIDE);
  4649. }
  4650. }
  4651. }
  4652. //---------------------------------------------------------------------------
  4653. void __fastcall TCustomScpExplorerForm::ApplicationRestore(TObject * /*Sender*/)
  4654. {
  4655. // WORKAROUND
  4656. // When restoring maximized window from minimization,
  4657. // rarely some controls do not align properly.
  4658. // Two instances seen (both for Commander):
  4659. // - When restoring, window is temporarily narrower (not maximized),
  4660. // causing toolbars on TopDock to wrap and dock to expand horizontally.
  4661. // Once maximized already, top dock shinks back, but the session PageControl,
  4662. // do not align up, leaving space between TopDock and PageControl.
  4663. // - Similar issue seen with LocalDirView not aligning down to status bar.
  4664. for (int Index = 0; Index < ControlCount; Index++)
  4665. {
  4666. RealignControl(Controls[Index]);
  4667. }
  4668. if (FTrayIcon->Visible)
  4669. {
  4670. FTrayIcon->Visible = false;
  4671. if (Visible)
  4672. {
  4673. ShowWindow(Handle, SW_SHOW);
  4674. }
  4675. }
  4676. if (FNeedSession && DebugAlwaysTrue(Terminal == NULL))
  4677. {
  4678. FNeedSession = false;
  4679. NonVisualDataModule->StartBusy();
  4680. try
  4681. {
  4682. NeedSession(true);
  4683. }
  4684. __finally
  4685. {
  4686. NonVisualDataModule->EndBusy();
  4687. }
  4688. }
  4689. }
  4690. //---------------------------------------------------------------------------
  4691. void __fastcall TCustomScpExplorerForm::UpdateTrayIcon()
  4692. {
  4693. FTrayIcon->Hint = GetMainForm()->Caption;
  4694. }
  4695. //---------------------------------------------------------------------------
  4696. void __fastcall TCustomScpExplorerForm::ApplicationTitleChanged()
  4697. {
  4698. UpdateTrayIcon();
  4699. }
  4700. //---------------------------------------------------------------------------
  4701. void __fastcall TCustomScpExplorerForm::RestoreApp()
  4702. {
  4703. // workaround
  4704. // TApplication.WndProc sets TApplication.FAppIconic to false,
  4705. // on WM_ACTIVATEAPP, what renders TApplication.Restore no-op function.
  4706. // But WM_ACTIVATEAPP message can be received even when
  4707. // the main window is minimized to the tray and internal editor window is focused
  4708. // (after another application was previously active)
  4709. if (::IsIconic(Handle))
  4710. {
  4711. if (!IsAppIconic())
  4712. {
  4713. SetAppIconic(true);
  4714. }
  4715. }
  4716. ::ApplicationRestore();
  4717. Application->BringToFront();
  4718. }
  4719. //---------------------------------------------------------------------------
  4720. void __fastcall TCustomScpExplorerForm::TrayIconClick(TObject * /*Sender*/)
  4721. {
  4722. RestoreApp();
  4723. }
  4724. //---------------------------------------------------------------------------
  4725. void __fastcall TCustomScpExplorerForm::NewSession(const UnicodeString & SessionUrl)
  4726. {
  4727. if (OpenInNewWindow())
  4728. {
  4729. ExecuteNewInstance(SessionUrl);
  4730. }
  4731. else
  4732. {
  4733. TTerminalManager::Instance()->NewSession(SessionUrl);
  4734. }
  4735. }
  4736. //---------------------------------------------------------------------------
  4737. void TCustomScpExplorerForm::NewTab(TOperationSide)
  4738. {
  4739. NewSession();
  4740. }
  4741. //---------------------------------------------------------------------------
  4742. UnicodeString __fastcall TCustomScpExplorerForm::SaveHiddenDuplicateSession(TSessionData * SessionData)
  4743. {
  4744. UnicodeString SessionName = StoredSessions->HiddenPrefix + ManagedSession->SessionData->SessionName;
  4745. StoredSessions->NewSession(SessionName, SessionData);
  4746. // modified only, explicit
  4747. StoredSessions->Save(false, true);
  4748. // encode session name because of slashes in hierarchical sessions
  4749. return EncodeUrlString(SessionName);
  4750. }
  4751. //---------------------------------------------------------------------------
  4752. UnicodeString __fastcall TCustomScpExplorerForm::CreateHiddenDuplicateSession()
  4753. {
  4754. // current working directories become defaults here, what is not right
  4755. std::unique_ptr<TSessionData> SessionData(CloneCurrentSessionData());
  4756. return SaveHiddenDuplicateSession(SessionData.get());
  4757. }
  4758. //---------------------------------------------------------------------------
  4759. void __fastcall TCustomScpExplorerForm::DuplicateSession()
  4760. {
  4761. if (OpenInNewWindow())
  4762. {
  4763. ExecuteNewInstance(CreateHiddenDuplicateSession());
  4764. }
  4765. else
  4766. {
  4767. // current working directories become defaults here, what is not right
  4768. std::unique_ptr<TSessionData> SessionData(CloneCurrentSessionData());
  4769. TTerminalManager * Manager = TTerminalManager::Instance();
  4770. TManagedTerminal * ASession = Manager->NewManagedTerminal(SessionData.get());
  4771. DebugAssert(ASession->LocalBrowser == ManagedSession->LocalBrowser);
  4772. // We definitelly want these
  4773. ASession->Disconnected = ManagedSession->Disconnected;
  4774. // Not sure about these two
  4775. ASession->Permanent = ManagedSession->Permanent;
  4776. ASession->DisconnectedTemporarily = ManagedSession->DisconnectedTemporarily;
  4777. Manager->ActiveSession = ASession;
  4778. }
  4779. }
  4780. //---------------------------------------------------------------------------
  4781. void __fastcall TCustomScpExplorerForm::RenameSession()
  4782. {
  4783. UnicodeString Name = ManagedSession->SessionData->SessionName;
  4784. if (InputDialog(LoadStr(RENAME_SESSION_TITLE), LoadStr(RENAME_SESSION_PROMPT), Name, HELP_SESSION_RENAME) &&
  4785. // When submitting the default name, do not fix it
  4786. (Name != ManagedSession->SessionData->SessionName))
  4787. {
  4788. // Checks for a slash only, so it's not that big deal that we do the check after submitting the dialog only.
  4789. TSessionData::ValidateName(Name);
  4790. // This is inconsistent with how color (for example) is handled.
  4791. ManagedSession->SessionData->Name = Name;
  4792. UpdateControls();
  4793. // Add/Remove distinguishing paths from sessions of the same name.
  4794. SessionListChanged();
  4795. }
  4796. }
  4797. //---------------------------------------------------------------------------
  4798. bool __fastcall TCustomScpExplorerForm::CanCloseQueue(TTerminalQueue * Queue)
  4799. {
  4800. DebugAssert(Queue != NULL);
  4801. bool Result = Queue->IsEmpty;
  4802. if (!Result)
  4803. {
  4804. SetFocus();
  4805. Result = (MessageDialog(LoadStr(PENDING_QUEUE_ITEMS2), qtWarning, qaOK | qaCancel, HELP_NONE) == qaOK);
  4806. }
  4807. return Result;
  4808. }
  4809. //---------------------------------------------------------------------------
  4810. bool __fastcall TCustomScpExplorerForm::CanCloseQueue()
  4811. {
  4812. return CanCloseQueue(FQueue);
  4813. }
  4814. //---------------------------------------------------------------------------
  4815. void __fastcall TCustomScpExplorerForm::CloseSession()
  4816. {
  4817. if (CanCloseQueue())
  4818. {
  4819. TTerminalManager::Instance()->FreeActiveTerminal();
  4820. }
  4821. }
  4822. //---------------------------------------------------------------------------
  4823. void __fastcall TCustomScpExplorerForm::DisconnectSession()
  4824. {
  4825. if (CanCloseQueue())
  4826. {
  4827. TTerminalManager::Instance()->DisconnectActiveTerminal();
  4828. }
  4829. }
  4830. //---------------------------------------------------------------------------
  4831. void __fastcall TCustomScpExplorerForm::TerminalDisconnected()
  4832. {
  4833. DetachTerminal(Terminal);
  4834. RemoteDirView->Terminal = Terminal;
  4835. UpdateRemotePathComboBox(false);
  4836. InitStatusBar();
  4837. UpdateControls();
  4838. }
  4839. //---------------------------------------------------------------------------
  4840. void __fastcall TCustomScpExplorerForm::TerminalConnecting()
  4841. {
  4842. InitStatusBar();
  4843. }
  4844. //---------------------------------------------------------------------------
  4845. void __fastcall TCustomScpExplorerForm::ReconnectSession()
  4846. {
  4847. if (DebugAlwaysTrue(!Terminal->Active))
  4848. {
  4849. TTerminalManager::Instance()->ReconnectActiveTerminal();
  4850. }
  4851. }
  4852. //---------------------------------------------------------------------------
  4853. void __fastcall TCustomScpExplorerForm::OpenStoredSession(TSessionData * Data)
  4854. {
  4855. if (OpenInNewWindow())
  4856. {
  4857. // encode session name because of slashes in hierarchical sessions
  4858. ExecuteNewInstance(EncodeUrlString(Data->Name));
  4859. }
  4860. else
  4861. {
  4862. TTerminalManager * Manager = TTerminalManager::Instance();
  4863. TManagedTerminal * Terminal = Manager->NewManagedTerminal(Data);
  4864. Manager->ActiveSession = Terminal;
  4865. }
  4866. }
  4867. //---------------------------------------------------------------------------
  4868. void TCustomScpExplorerForm::DoOpenFolderOrWorkspace(
  4869. const UnicodeString & Name, bool ConnectFirstTerminal, bool CheckMaxSessions)
  4870. {
  4871. TTerminalManager * Manager = TTerminalManager::Instance();
  4872. std::unique_ptr<TObjectList> DataList(new TObjectList());
  4873. // This asks for a master password. Maybe we should postpone asking until a connection is actually opened.
  4874. StoredSessions->GetFolderOrWorkspace(Name, DataList.get());
  4875. if (!CheckMaxSessions || (DataList->Count <= WinConfiguration->MaxSessions))
  4876. {
  4877. TManagedTerminal * FirstSession = Manager->NewSessions(DataList.get());
  4878. // FirstSession can be null, if some of the
  4879. if (!ConnectFirstTerminal && (FirstSession != NULL))
  4880. {
  4881. FirstSession->Disconnected = true;
  4882. FirstSession->DisconnectedTemporarily = true;
  4883. }
  4884. Manager->ActiveSession = FirstSession;
  4885. }
  4886. }
  4887. //---------------------------------------------------------------------------
  4888. void __fastcall TCustomScpExplorerForm::OpenFolderOrWorkspace(const UnicodeString & Name)
  4889. {
  4890. if (OpenInNewWindow())
  4891. {
  4892. ExecuteNewInstance(Name);
  4893. }
  4894. else
  4895. {
  4896. DoOpenFolderOrWorkspace(Name, true, false);
  4897. }
  4898. }
  4899. //---------------------------------------------------------------------------
  4900. void __fastcall TCustomScpExplorerForm::FormCloseQuery(TObject * /*Sender*/,
  4901. bool &CanClose)
  4902. {
  4903. // See the comment in CloseApp()
  4904. if (NonVisualDataModule->Busy)
  4905. {
  4906. CanClose = false;
  4907. }
  4908. else
  4909. {
  4910. TTerminalManager * Manager = TTerminalManager::Instance();
  4911. int ActiveSessions = 0;
  4912. for (int Index = 0; Index < Manager->Count; Index++)
  4913. {
  4914. if (Manager->Sessions[Index]->Active)
  4915. {
  4916. ActiveSessions++;
  4917. }
  4918. }
  4919. // Confirm, when there's at least one active remote session or
  4920. // when there are multiple (inactive or local) sessions and auto workspace saving is not enabled.
  4921. if (((ActiveSessions > 0) || ((Manager->Count > 1) && !WinConfiguration->AutoSaveWorkspace)) &&
  4922. WinConfiguration->ConfirmClosingSession)
  4923. {
  4924. unsigned int Result;
  4925. TMessageParams Params(mpNeverAskAgainCheck);
  4926. UnicodeString Message;
  4927. int Answers = qaOK | qaCancel;
  4928. // The current session is the only active session
  4929. if ((ActiveSessions == 1) && (Terminal != NULL) && Terminal->Active)
  4930. {
  4931. if (!WinConfiguration->AutoSaveWorkspace)
  4932. {
  4933. Message = LoadStr(CLOSE_SESSION_WORKSPACE);
  4934. }
  4935. else
  4936. {
  4937. Message = LoadStr(CLOSE_SESSION);
  4938. }
  4939. Message = FORMAT(Message, (Terminal->SessionData->SessionName));
  4940. }
  4941. // Multiple active sessions or one active session, but it's not the current one
  4942. else if (ActiveSessions > 0)
  4943. {
  4944. if (!WinConfiguration->AutoSaveWorkspace)
  4945. {
  4946. Message = LoadStr(CLOSE_SESSIONS_WORKSPACE3);
  4947. }
  4948. else
  4949. {
  4950. Message = LoadStr(CLOSE_SESSIONS);
  4951. }
  4952. }
  4953. // Multiple tabs, but all are inactive or local, and saving of worspace is not enabled
  4954. else
  4955. {
  4956. DebugAlwaysTrue(Manager->Count > 1);
  4957. DebugAlwaysTrue(!WinConfiguration->AutoSaveWorkspace);
  4958. Message = LoadStr(CLOSE_WORKSPACE);
  4959. }
  4960. UnicodeString Note;
  4961. if (WinConfiguration->AutoSaveWorkspace)
  4962. {
  4963. Note = FMTLOAD(AUTO_WORKSPACE, (WorkspaceName()));
  4964. }
  4965. else
  4966. {
  4967. Note = LoadStr(AUTO_WORKSPACE_ENABLE);
  4968. Answers = qaYes | qaNo | qaCancel;
  4969. }
  4970. Message = FORMAT("%s\n\n%s", (MainInstructions(Message), Note));
  4971. SetFocus();
  4972. Result = MessageDialog(Message, qtConfirmation,
  4973. Answers, HELP_CLOSE_SESSION_WORKSPACE, &Params);
  4974. if (Result == qaNeverAskAgain)
  4975. {
  4976. WinConfiguration->ConfirmClosingSession = false;
  4977. }
  4978. if (Result == qaNo)
  4979. {
  4980. CanClose = SaveWorkspace(true);
  4981. // note that the workspace will be saved redundatly again from FormClose
  4982. }
  4983. else
  4984. {
  4985. CanClose =
  4986. (Result == qaOK) ||
  4987. (Result == qaYes) || // CLOSE_SESSIONS_WORKSPACE variant
  4988. (Result == qaNeverAskAgain);
  4989. }
  4990. }
  4991. if (CanClose && HasActiveTerminal())
  4992. {
  4993. CanClose = CanCloseQueue();
  4994. }
  4995. }
  4996. if (CanClose)
  4997. {
  4998. CanClose =
  4999. FEditorManager->CloseInternalEditors(CloseInternalEditor) &&
  5000. FEditorManager->CloseExternalFilesWithoutProcess();
  5001. if (CanClose)
  5002. {
  5003. while (CanClose && (FLocalEditors->Count > 0))
  5004. {
  5005. int PrevCount = FLocalEditors->Count;
  5006. static_cast<TForm *>(FLocalEditors->Items[0])->Close();
  5007. CanClose = (FLocalEditors->Count < PrevCount);
  5008. }
  5009. if (CanClose)
  5010. {
  5011. CanClose = FEditorManager->Empty(true);
  5012. if (!CanClose)
  5013. {
  5014. SetFocus();
  5015. CanClose =
  5016. (MessageDialog(
  5017. LoadStr(PENDING_EDITORS), qtWarning, qaIgnore | qaCancel, HELP_NONE) == qaIgnore);
  5018. }
  5019. }
  5020. }
  5021. }
  5022. }
  5023. //---------------------------------------------------------------------------
  5024. void __fastcall TCustomScpExplorerForm::CloseInternalEditor(TObject * Sender)
  5025. {
  5026. TForm * Form = dynamic_cast<TForm *>(Sender);
  5027. DebugAssert(Form != NULL);
  5028. Form->Close();
  5029. }
  5030. //---------------------------------------------------------------------------
  5031. void __fastcall TCustomScpExplorerForm::ForceCloseInternalEditor(TObject * Sender)
  5032. {
  5033. TForm * Form = dynamic_cast<TForm *>(Sender);
  5034. delete Form;
  5035. }
  5036. //---------------------------------------------------------------------------
  5037. void __fastcall TCustomScpExplorerForm::ForceCloseLocalEditors()
  5038. {
  5039. while (FLocalEditors->Count > 0)
  5040. {
  5041. delete static_cast<TForm *>(FLocalEditors->Items[0]);
  5042. }
  5043. }
  5044. //---------------------------------------------------------------------------
  5045. void __fastcall TCustomScpExplorerForm::RemoteDirViewDisplayProperties(
  5046. TObject *Sender)
  5047. {
  5048. TStrings *FileList = ((TUnixDirView*)Sender)->CreateFileList(True, False, NULL);
  5049. try
  5050. {
  5051. SetProperties(osRemote, FileList);
  5052. }
  5053. __finally
  5054. {
  5055. delete FileList;
  5056. }
  5057. }
  5058. //---------------------------------------------------------------------------
  5059. void __fastcall TCustomScpExplorerForm::ComponentShowing(Byte Component, bool value)
  5060. {
  5061. if (value)
  5062. {
  5063. if (Component == fcCustomCommandsBand)
  5064. {
  5065. UpdateCustomCommandsToolbar();
  5066. }
  5067. if (Component == fcQueueFileList)
  5068. {
  5069. DebugAssert(!QueueFileListSplitter->Visible);
  5070. AdjustQueueLayout();
  5071. UpdateQueueFileList();
  5072. }
  5073. }
  5074. }
  5075. //---------------------------------------------------------------------------
  5076. void __fastcall TCustomScpExplorerForm::AdjustQueueLayout()
  5077. {
  5078. int MinHeightLimit =
  5079. GetStaticQueuePanelComponentsHeight() +
  5080. QueueFileListSplitter->Height +
  5081. QueueFileList->Height +
  5082. GetMinQueueViewHeight();
  5083. if (QueuePanel->ClientHeight < MinHeightLimit)
  5084. {
  5085. QueuePanel->ClientHeight = MinHeightLimit;
  5086. }
  5087. }
  5088. //---------------------------------------------------------------------------
  5089. void __fastcall TCustomScpExplorerForm::SetComponentVisible(Byte Component, Boolean value)
  5090. {
  5091. TControl * Control = GetComponent(Component);
  5092. DebugAssert(Control);
  5093. bool Changed = (Control->Visible != value);
  5094. if (Changed)
  5095. {
  5096. ComponentShowing(Component, value);
  5097. TWinControl * WinControl = dynamic_cast<TWinControl*>(Control);
  5098. bool WasFocused = (WinControl != NULL) && (ActiveControl != NULL) &&
  5099. ((ActiveControl == WinControl) || (ActiveControl->Parent == WinControl));
  5100. if (value)
  5101. {
  5102. int RemainingHeight = Control->Parent->ClientHeight;
  5103. int RemainingWidth = Control->Parent->ClientWidth;
  5104. for (int i = 0; i < Control->Parent->ControlCount; i++)
  5105. {
  5106. TControl * ChildControl = Control->Parent->Controls[i];
  5107. if (ChildControl->Visible)
  5108. {
  5109. switch (ChildControl->Align)
  5110. {
  5111. case alTop:
  5112. case alBottom:
  5113. RemainingHeight -= ChildControl->Height;
  5114. break;
  5115. case alLeft:
  5116. case alRight:
  5117. RemainingWidth -= ChildControl->Width;
  5118. break;
  5119. }
  5120. }
  5121. }
  5122. int Reserve = ScaleByTextHeight(this, 32);
  5123. // queue in explorer, trees in commander
  5124. if (Control->Height > RemainingHeight - Reserve)
  5125. {
  5126. Control->Height = RemainingHeight / 2;
  5127. }
  5128. if (Control->Width > RemainingWidth - Reserve)
  5129. {
  5130. Control->Width = RemainingWidth / 2;
  5131. }
  5132. }
  5133. Control->Visible = value;
  5134. if (WasFocused && Visible)
  5135. {
  5136. DirView(osCurrent)->SetFocus();
  5137. }
  5138. FixControlsPlacement();
  5139. }
  5140. }
  5141. //---------------------------------------------------------------------------
  5142. bool __fastcall TCustomScpExplorerForm::GetComponentVisible(Byte Component)
  5143. {
  5144. TControl * Control = GetComponent(Component);
  5145. if (Control == NULL)
  5146. {
  5147. return false;
  5148. }
  5149. else
  5150. {
  5151. return Control->Visible;
  5152. }
  5153. }
  5154. //---------------------------------------------------------------------------
  5155. bool __fastcall TCustomScpExplorerForm::IsComponentPossible(Byte Component)
  5156. {
  5157. bool Result;
  5158. if ((Component == fcExplorerUpdatesBand) || (Component == fcCommanderUpdatesBand))
  5159. {
  5160. Result = !IsUWP();
  5161. }
  5162. else
  5163. {
  5164. Result = true;
  5165. }
  5166. return Result;
  5167. }
  5168. //---------------------------------------------------------------------------
  5169. void __fastcall TCustomScpExplorerForm::MakeFocusedItemVisible(TCustomDirView * DirView)
  5170. {
  5171. if (DirView->ItemFocused != NULL)
  5172. {
  5173. DirView->ItemFocused->MakeVisible(false);
  5174. }
  5175. }
  5176. //---------------------------------------------------------------------------
  5177. void __fastcall TCustomScpExplorerForm::FixControlsPlacement()
  5178. {
  5179. MakeFocusedItemVisible(RemoteDirView);
  5180. QueueSplitter->Visible = QueuePanel->Visible;
  5181. QueueFileListSplitter->Visible = QueueFileList->Visible;
  5182. RemotePanelSplitter->Visible = RemoteDrivePanel->Visible;
  5183. TControl * TopControlsOrder[] =
  5184. { TopDock, MessageDock, SessionsPageControl };
  5185. SetVerticalControlsOrder(TopControlsOrder, LENOF(TopControlsOrder));
  5186. TControl * QueueControlsOrder[] =
  5187. { QueueDock, QueueView3, QueueFileListSplitter, QueueFileList };
  5188. SetVerticalControlsOrder(QueueControlsOrder, LENOF(QueueControlsOrder));
  5189. }
  5190. //---------------------------------------------------------------------------
  5191. TControl * __fastcall TCustomScpExplorerForm::GetComponent(Byte Component)
  5192. {
  5193. switch (Component) {
  5194. case fcStatusBar: return RemoteStatusBar;
  5195. case fcRemotePopup: return reinterpret_cast<TControl *>(NonVisualDataModule->RemoteFilePopup);
  5196. case fcQueueView: return QueuePanel;
  5197. case fcQueueToolbar: return QueueDock;
  5198. case fcRemoteTree: return RemoteDrivePanel;
  5199. case fcSessionsTabs: return SessionsPageControl;
  5200. case fcQueueFileList: return QueueFileList;
  5201. default: return NULL;
  5202. }
  5203. }
  5204. //---------------------------------------------------------------------------
  5205. void __fastcall TCustomScpExplorerForm::DirViewColumnRightClick(
  5206. TObject *Sender, TListColumn *Column, TPoint &Point)
  5207. {
  5208. DebugAssert(NonVisualDataModule && Column && Sender);
  5209. NonVisualDataModule->ListColumn = Column;
  5210. TPopupMenu * DirViewColumnMenu;
  5211. if (Sender == DirView(osOther))
  5212. {
  5213. DirViewColumnMenu = NonVisualDataModule->RemoteDirViewColumnPopup;
  5214. NonVisualDataModule->RemoteSortByExtColumnPopupItem->Visible =
  5215. (Column->Index == uvName);
  5216. NonVisualDataModule->RemoteFormatSizeBytesPopupItem->Visible =
  5217. (Column->Index == uvSize);
  5218. }
  5219. else
  5220. {
  5221. DirViewColumnMenu = NonVisualDataModule->LocalDirViewColumnPopup;
  5222. NonVisualDataModule->LocalSortByExtColumnPopupItem->Visible =
  5223. (Column->Index == dvName);
  5224. NonVisualDataModule->LocalFormatSizeBytesPopupItem->Visible =
  5225. (Column->Index == dvSize);
  5226. }
  5227. TCustomListView * ListView = DebugNotNull(dynamic_cast<TCustomListView *>(Sender));
  5228. TPoint ScreenPoint = ListView->ClientToScreen(Point);
  5229. ScreenPoint.x -= GetScrollPos(ListView->Handle, SB_HORZ);
  5230. DirViewColumnMenu->Popup(ScreenPoint.x, ScreenPoint.y);
  5231. }
  5232. //---------------------------------------------------------------------------
  5233. void __fastcall TCustomScpExplorerForm::DirViewExecFile(
  5234. TObject *Sender, TListItem *Item, bool &AllowExec)
  5235. {
  5236. DoDirViewExecFile(Sender, Item, AllowExec);
  5237. }
  5238. //---------------------------------------------------------------------------
  5239. void __fastcall TCustomScpExplorerForm::DoDirViewExecFile(TObject * Sender,
  5240. TListItem * Item, bool & AllowExec)
  5241. {
  5242. DebugAssert(Sender && Item && Configuration);
  5243. DebugAssert(AllowExec);
  5244. TCustomDirView * ADirView = (TCustomDirView *)Sender;
  5245. TOperationSide Side = ((ADirView == DirView(osRemote)) ? osRemote : osLocal);
  5246. bool Remote = !IsSideLocalBrowser(Side);
  5247. DebugAssert(Item == ADirView->ItemFocused);
  5248. bool IsDirectory = ADirView->ItemIsDirectory(Item);
  5249. TResolvedDoubleClickAction Action;
  5250. if (FForceExecution)
  5251. {
  5252. Action = IsDirectory ? rdcaChangeDir : rdcaOpen;
  5253. }
  5254. else
  5255. {
  5256. TTerminal * ATerminal = !IsSideLocalBrowser(Side) ? Terminal : NULL;
  5257. Action = WinConfiguration->ResolveDoubleClickAction(IsDirectory, ATerminal);
  5258. }
  5259. if (Action == rdcaCopy)
  5260. {
  5261. // To counter the lock in DirViewBusy (called from TCustomDirView.DoExecute)
  5262. UnlockWindow();
  5263. try
  5264. {
  5265. if (IsLocalBrowserMode())
  5266. {
  5267. LocalLocalCopy(foCopy, Side, true, !WinConfiguration->CopyOnDoubleClickConfirmation, false, 0);
  5268. }
  5269. else
  5270. {
  5271. ExecuteFileOperation(foCopy, Side, true, !WinConfiguration->CopyOnDoubleClickConfirmation);
  5272. }
  5273. }
  5274. __finally
  5275. {
  5276. LockWindow();
  5277. }
  5278. AllowExec = false;
  5279. }
  5280. else if (Action == rdcaEdit)
  5281. {
  5282. if (!Remote || !WinConfiguration->DisableOpenEdit)
  5283. {
  5284. ExecuteFile(Side, efDefaultEditor);
  5285. AllowExec = false;
  5286. }
  5287. }
  5288. else if (Action == rdcaOpen)
  5289. {
  5290. if (DebugAlwaysTrue(!IsDirectory) && Remote && !WinConfiguration->DisableOpenEdit)
  5291. {
  5292. ExecuteFile(osRemote, efShell);
  5293. AllowExec = false;
  5294. }
  5295. }
  5296. }
  5297. //---------------------------------------------------------------------------
  5298. bool __fastcall TCustomScpExplorerForm::GetHasDirView(TOperationSide Side)
  5299. {
  5300. return ((Side == osRemote) || (Side == osCurrent));
  5301. }
  5302. //---------------------------------------------------------------------------
  5303. void __fastcall TCustomScpExplorerForm::CompareDirectories()
  5304. {
  5305. DebugFail();
  5306. }
  5307. //---------------------------------------------------------------------------
  5308. void __fastcall TCustomScpExplorerForm::SynchronizeDirectories()
  5309. {
  5310. DebugFail();
  5311. }
  5312. //---------------------------------------------------------------------------
  5313. bool __fastcall TCustomScpExplorerForm::DoSynchronizeDirectories(
  5314. UnicodeString & LocalDirectory, UnicodeString & RemoteDirectory, int UseDefaults)
  5315. {
  5316. TSynchronizeParamType Params;
  5317. Params.LocalDirectory = LocalDirectory;
  5318. Params.RemoteDirectory = RemoteDirectory;
  5319. int UnusedParams =
  5320. (GUIConfiguration->SynchronizeParams &
  5321. (TTerminal::spPreviewChanges | TTerminal::spTimestamp | TTerminal::spNotByTime | TTerminal::spBySize |
  5322. TTerminal::spCaseSensitive));
  5323. Params.Params = GUIConfiguration->SynchronizeParams & ~UnusedParams;
  5324. Params.Options = GUIConfiguration->SynchronizeOptions;
  5325. bool SaveSettings = false;
  5326. TSynchronizeController Controller(&DoSynchronize, &DoSynchronizeInvalid,
  5327. &DoSynchronizeTooManyDirectories);
  5328. DebugAssert(FSynchronizeController == NULL);
  5329. FSynchronizeController = &Controller;
  5330. bool Result;
  5331. try
  5332. {
  5333. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  5334. int CopyParamAttrs = Terminal->UsableCopyParamAttrs(0).Upload;
  5335. int Options =
  5336. FLAGMASK(SynchronizeAllowSelectedOnly(), soAllowSelectedOnly);
  5337. DebugAssert(FOnFeedSynchronizeError == NULL);
  5338. DebugAssert(FOnSynchronizeAbort == NULL);
  5339. DebugAssert(FSynchronizeTerminal == NULL);
  5340. FSynchronizeTerminal = Terminal;
  5341. Result = DoSynchronizeDialog(Params, &CopyParam, Controller.StartStop,
  5342. SaveSettings, Options, CopyParamAttrs, GetSynchronizeOptions, SynchronizeSessionLog,
  5343. FOnFeedSynchronizeError, FOnSynchronizeAbort, SynchronizeInNewWindow, UseDefaults);
  5344. if (Result)
  5345. {
  5346. if (SaveSettings)
  5347. {
  5348. GUIConfiguration->SynchronizeParams = Params.Params | UnusedParams;
  5349. GUIConfiguration->SynchronizeOptions = Params.Options;
  5350. }
  5351. else
  5352. {
  5353. if (FLAGSET(GUIConfiguration->SynchronizeOptions, soSynchronizeAsk) &&
  5354. FLAGCLEAR(Params.Options, soSynchronizeAsk) &&
  5355. FLAGSET(Params.Options, soSynchronize))
  5356. {
  5357. GUIConfiguration->SynchronizeOptions =
  5358. (GUIConfiguration->SynchronizeOptions & ~soSynchronizeAsk) |
  5359. soSynchronize;
  5360. }
  5361. }
  5362. LocalDirectory = Params.LocalDirectory;
  5363. RemoteDirectory = Params.RemoteDirectory;
  5364. }
  5365. }
  5366. __finally
  5367. {
  5368. FSynchronizeController = NULL;
  5369. DebugAssert(FOnFeedSynchronizeError == NULL);
  5370. FOnFeedSynchronizeError = NULL;
  5371. DebugAssert(FOnSynchronizeAbort == NULL);
  5372. FOnSynchronizeAbort = NULL;
  5373. FSynchronizeTerminal = NULL;
  5374. }
  5375. return Result;
  5376. }
  5377. //---------------------------------------------------------------------------
  5378. void __fastcall TCustomScpExplorerForm::DoSynchronize(
  5379. TSynchronizeController * /*Sender*/, const UnicodeString LocalDirectory,
  5380. const UnicodeString RemoteDirectory, const TCopyParamType & CopyParam,
  5381. const TSynchronizeParamType & Params, TSynchronizeChecklist ** Checklist,
  5382. TSynchronizeOptions * Options, bool Full)
  5383. {
  5384. // Keep-up-to-date mode
  5385. if (Terminal->Status == ssOpened)
  5386. {
  5387. try
  5388. {
  5389. int PParams = Params.Params;
  5390. if (!Full)
  5391. {
  5392. PParams |= TTerminal::spNoRecurse | TTerminal::spUseCache | TTerminal::spDelayProgress;
  5393. if (FLAGSET(Params.Options, soRecurse))
  5394. {
  5395. PParams |= TTerminal::spSubDirs;
  5396. }
  5397. }
  5398. else
  5399. {
  5400. // if keepuptodate is non-recursive,
  5401. // full sync before has to be non-recursive as well
  5402. if (FLAGCLEAR(Params.Options, soRecurse))
  5403. {
  5404. PParams |= TTerminal::spNoRecurse;
  5405. }
  5406. }
  5407. Synchronize(LocalDirectory, RemoteDirectory, smRemote, CopyParam,
  5408. PParams, Checklist, Options);
  5409. }
  5410. catch (Exception & E)
  5411. {
  5412. if (!Terminal->Active)
  5413. {
  5414. ShowExtendedExceptionEx(Terminal, &E);
  5415. // Do not abort the "keep up to date" when the session was succesfully reconnected.
  5416. if (!Terminal->Active)
  5417. {
  5418. throw;
  5419. }
  5420. }
  5421. else
  5422. {
  5423. if (FLAGSET(Params.Options, soContinueOnError) &&
  5424. DebugAlwaysTrue(FOnFeedSynchronizeError != NULL))
  5425. {
  5426. // noop, already logged in MoreMessageDialog
  5427. }
  5428. else
  5429. {
  5430. // We get mostly EAbort here, so this is noop.
  5431. // Exception is an error when listing directories while looking for differences.
  5432. ShowExtendedExceptionEx(Terminal, &E);
  5433. throw;
  5434. }
  5435. }
  5436. }
  5437. }
  5438. }
  5439. //---------------------------------------------------------------------------
  5440. void __fastcall TCustomScpExplorerForm::DoSynchronizeInvalid(
  5441. TSynchronizeController * /*Sender*/, const UnicodeString Directory,
  5442. const UnicodeString ErrorStr)
  5443. {
  5444. if (!Directory.IsEmpty())
  5445. {
  5446. SimpleErrorDialog(FMTLOAD(WATCH_ERROR_DIRECTORY, (Directory)), ErrorStr);
  5447. }
  5448. else
  5449. {
  5450. SimpleErrorDialog(LoadStr(WATCH_ERROR_GENERAL), ErrorStr);
  5451. }
  5452. }
  5453. //---------------------------------------------------------------------------
  5454. void __fastcall TCustomScpExplorerForm::DoSynchronizeTooManyDirectories(
  5455. TSynchronizeController * /*Sender*/, int & MaxDirectories)
  5456. {
  5457. if (MaxDirectories < GUIConfiguration->MaxWatchDirectories)
  5458. {
  5459. MaxDirectories = GUIConfiguration->MaxWatchDirectories;
  5460. }
  5461. else
  5462. {
  5463. TMessageParams Params(mpNeverAskAgainCheck);
  5464. unsigned int Result = MessageDialog(
  5465. FMTLOAD(TOO_MANY_WATCH_DIRECTORIES, (MaxDirectories, MaxDirectories)),
  5466. qtConfirmation, qaYes | qaNo, HELP_TOO_MANY_WATCH_DIRECTORIES, &Params);
  5467. if ((Result == qaYes) || (Result == qaNeverAskAgain))
  5468. {
  5469. MaxDirectories *= 2;
  5470. if (Result == qaNeverAskAgain)
  5471. {
  5472. GUIConfiguration->MaxWatchDirectories = MaxDirectories;
  5473. }
  5474. }
  5475. else
  5476. {
  5477. Abort();
  5478. }
  5479. }
  5480. }
  5481. //---------------------------------------------------------------------------
  5482. void __fastcall TCustomScpExplorerForm::Synchronize(const UnicodeString LocalDirectory,
  5483. const UnicodeString RemoteDirectory, TSynchronizeMode Mode,
  5484. const TCopyParamType & CopyParam, int Params, TSynchronizeChecklist ** Checklist,
  5485. TSynchronizeOptions * Options)
  5486. {
  5487. // Keep-up-to-date mode
  5488. DebugAssert(!FAutoOperation);
  5489. void * BatchStorage;
  5490. BatchStart(BatchStorage);
  5491. FAutoOperation = true;
  5492. bool AnyOperation = false;
  5493. TDateTime StartTime = Now();
  5494. TSynchronizeChecklist * AChecklist = NULL;
  5495. try
  5496. {
  5497. FSynchronizeProgressForm = new TSynchronizeProgressForm(Application, true, -1);
  5498. if (FLAGCLEAR(Params, TTerminal::spDelayProgress))
  5499. {
  5500. FSynchronizeProgressForm->Start();
  5501. }
  5502. AChecklist = Terminal->SynchronizeCollect(LocalDirectory, RemoteDirectory,
  5503. static_cast<TTerminal::TSynchronizeMode>(Mode),
  5504. &CopyParam, Params | TTerminal::spNoConfirmation, TerminalSynchronizeDirectory,
  5505. Options);
  5506. SetTaskbarListProgressState(TBPF_NOPROGRESS);
  5507. SAFE_DESTROY(FSynchronizeProgressForm);
  5508. AnyOperation = (AChecklist->CheckedCount > 0);
  5509. if (AnyOperation)
  5510. {
  5511. TSynchronizeProgress SynchronizeProgress(AChecklist);
  5512. CreateProgressForm(&SynchronizeProgress);
  5513. try
  5514. {
  5515. Terminal->SynchronizeApply(
  5516. AChecklist, &CopyParam, Params | TTerminal::spNoConfirmation, TerminalSynchronizeDirectory,
  5517. SynchronizeProcessedItem, NULL, NULL, NULL);
  5518. }
  5519. __finally
  5520. {
  5521. DestroyProgressForm();
  5522. }
  5523. }
  5524. }
  5525. __finally
  5526. {
  5527. if (Checklist == NULL)
  5528. {
  5529. delete AChecklist;
  5530. }
  5531. else
  5532. {
  5533. *Checklist = AChecklist;
  5534. }
  5535. FAutoOperation = false;
  5536. SAFE_DESTROY(FSynchronizeProgressForm);
  5537. BatchEnd(BatchStorage);
  5538. ReloadLocalDirectory();
  5539. if (AnyOperation)
  5540. {
  5541. OperationComplete(StartTime);
  5542. }
  5543. }
  5544. }
  5545. //---------------------------------------------------------------------------
  5546. bool __fastcall TCustomScpExplorerForm::SynchronizeAllowSelectedOnly()
  5547. {
  5548. // can be called from command line
  5549. return Visible &&
  5550. ((DirView(osRemote)->SelCount > 0) ||
  5551. (HasDirView[osLocal] && (DirView(osLocal)->SelCount > 0)));
  5552. }
  5553. //---------------------------------------------------------------------------
  5554. void __fastcall TCustomScpExplorerForm::SynchronizeSessionLog(const UnicodeString & Message)
  5555. {
  5556. LogSynchronizeEvent(Terminal, Message);
  5557. }
  5558. //---------------------------------------------------------------------------
  5559. void __fastcall TCustomScpExplorerForm::GetSynchronizeOptions(
  5560. int Params, TSynchronizeOptions & Options)
  5561. {
  5562. DebugAssert(!IsLocalBrowserMode());
  5563. if (FLAGSET(Params, TTerminal::spSelectedOnly) && SynchronizeAllowSelectedOnly())
  5564. {
  5565. Options.Filter = new TStringList();
  5566. Options.Filter->CaseSensitive = false;
  5567. Options.Filter->Duplicates = Types::dupAccept;
  5568. if (DirView(osRemote)->SelCount > 0)
  5569. {
  5570. DirView(osRemote)->CreateFileList(false, false, Options.Filter);
  5571. }
  5572. if (HasDirView[osLocal] && (DirView(osLocal)->SelCount > 0))
  5573. {
  5574. DirView(osLocal)->CreateFileList(false, false, Options.Filter);
  5575. }
  5576. Options.Filter->Sort();
  5577. }
  5578. }
  5579. //---------------------------------------------------------------------------
  5580. UnicodeString __fastcall TCustomScpExplorerForm::SerializeCopyParamForCommandLine(const TCopyParamType * CopyParams)
  5581. {
  5582. TCopyParamType Defaults;
  5583. std::unique_ptr<THierarchicalStorage> ConfigStorage(Configuration->CreateConfigStorage());
  5584. ConfigStorage->AccessMode = smRead;
  5585. if (ConfigStorage->OpenSubKey(Configuration->ConfigurationSubKey, false))
  5586. {
  5587. GUIConfiguration->LoadCopyParam(ConfigStorage.get(), &Defaults);
  5588. }
  5589. ConfigStorage.reset(NULL);
  5590. std::unique_ptr<TStringList> Options(new TStringList());
  5591. std::unique_ptr<TOptionsStorage> OptionsStorage(new TOptionsStorage(Options.get(), true));
  5592. CopyParams->Save(OptionsStorage.get(), &Defaults);
  5593. UnicodeString Result;
  5594. if (Options->Count > 0)
  5595. {
  5596. Result =
  5597. FORMAT(L" %s%s", (TProgramParams::FormatSwitch(RAWTRANSFERSETTINGS_SWITCH), StringsToParams(Options.get())));
  5598. }
  5599. return Result;
  5600. }
  5601. //---------------------------------------------------------------------------
  5602. void __fastcall TCustomScpExplorerForm::SynchronizeInNewWindow(
  5603. const TSynchronizeParamType & Params, const TCopyParamType * CopyParams)
  5604. {
  5605. UnicodeString SessionName = CreateHiddenDuplicateSession();
  5606. UnicodeString AdditionalParams =
  5607. FORMAT(L"%s%s %s // \"%s\" \"%s\" %d %d", (
  5608. TProgramParams::FormatSwitch(DEFAULTS_SWITCH),
  5609. SerializeCopyParamForCommandLine(CopyParams),
  5610. TProgramParams::FormatSwitch(KEEP_UP_TO_DATE_SWITCH),
  5611. Params.LocalDirectory, Params.RemoteDirectory, Params.Params, Params.Options));
  5612. ExecuteNewInstance(SessionName, AdditionalParams);
  5613. }
  5614. //---------------------------------------------------------------------------
  5615. struct TSynchronizeParams
  5616. {
  5617. UnicodeString LocalDirectory;
  5618. UnicodeString RemoteDirectory;
  5619. TSynchronizeMode Mode;
  5620. TSynchronizeChecklist * Checklist;
  5621. int Params;
  5622. TCopyParamType * CopyParam;
  5623. TDateTime CollectElapsed;
  5624. TDateTime * StartTime;
  5625. TProcessedSynchronizationChecklistItem OnProcessedItem;
  5626. };
  5627. //---------------------------------------------------------------------------
  5628. void __fastcall TCustomScpExplorerForm::FullSynchronize(
  5629. TSynchronizeParams & Params, TProcessedSynchronizationChecklistItem OnProcessedItem,
  5630. TUpdatedSynchronizationChecklistItems OnUpdatedSynchronizationChecklistItems)
  5631. {
  5632. DebugAssert(!FAutoOperation);
  5633. void * BatchStorage;
  5634. BatchStart(BatchStorage);
  5635. FAutoOperation = true;
  5636. TFileOperationStatistics Statistics;
  5637. TDateTime Start = Now();
  5638. try
  5639. {
  5640. Params.OnProcessedItem = OnProcessedItem;
  5641. TSynchronizeProgress SynchronizeProgress(Params.Checklist);
  5642. CreateProgressForm(&SynchronizeProgress);
  5643. Terminal->SynchronizeApply(
  5644. Params.Checklist, Params.CopyParam, Params.Params | TTerminal::spNoConfirmation,
  5645. TerminalSynchronizeDirectory, SynchronizeProcessedItem, OnUpdatedSynchronizationChecklistItems, &Params,
  5646. &Statistics);
  5647. }
  5648. __finally
  5649. {
  5650. Params.OnProcessedItem = NULL;
  5651. FAutoOperation = false;
  5652. DestroyProgressForm();
  5653. BatchEnd(BatchStorage);
  5654. ReloadLocalDirectory();
  5655. }
  5656. if (WinConfiguration->SynchronizeSummary)
  5657. {
  5658. UnicodeString Message = MainInstructions(LoadStr(SYNCHRONIZE_COMPLETE)) + L"\n";
  5659. // The statistics should be 0 anyway in this case
  5660. if (FLAGCLEAR(Params.Params, TTerminal::spTimestamp))
  5661. {
  5662. Message += L"\n";
  5663. if (Statistics.FilesUploaded > 0)
  5664. {
  5665. Message += FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 1), (FormatNumber(Statistics.FilesUploaded), FormatBytes(Statistics.TotalUploaded))) + L"\n";
  5666. }
  5667. if (Statistics.FilesDownloaded > 0)
  5668. {
  5669. Message += FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 2), (FormatNumber(Statistics.FilesDownloaded), FormatBytes(Statistics.TotalDownloaded))) + L"\n";
  5670. }
  5671. if (Statistics.FilesDeletedLocal > 0)
  5672. {
  5673. Message += FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 3), (FormatNumber(Statistics.FilesDeletedLocal))) + L"\n";
  5674. }
  5675. if (Statistics.FilesDeletedRemote > 0)
  5676. {
  5677. Message += FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 4), (FormatNumber(Statistics.FilesDeletedRemote))) + L"\n";
  5678. }
  5679. }
  5680. TDateTime Elapsed = (Now() - Start);
  5681. Message +=
  5682. L"\n" +
  5683. FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 5), (FormatDateTimeSpan(Configuration->TimeFormat, Params.CollectElapsed))) + L"\n" +
  5684. FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 6), (FormatDateTimeSpan(Configuration->TimeFormat, Elapsed)));
  5685. TMessageParams Params(mpNeverAskAgainCheck);
  5686. unsigned int Result = MessageDialog(Message, qtInformation, qaOK, HELP_NONE, &Params);
  5687. if (Result == qaNeverAskAgain)
  5688. {
  5689. WinConfiguration->SynchronizeSummary = false;
  5690. }
  5691. }
  5692. }
  5693. //---------------------------------------------------------------------------
  5694. void __fastcall TCustomScpExplorerForm::SynchronizeProcessedItem(void * Token, const TSynchronizeChecklist::TItem * ChecklistItem)
  5695. {
  5696. if (DebugAlwaysTrue(FProgressForm != NULL) && DebugAlwaysTrue(FProgressForm->SynchronizeProgress != NULL))
  5697. {
  5698. FProgressForm->SynchronizeProgress->ItemProcessed(ChecklistItem);
  5699. }
  5700. // Not set in keep-up-to-date mode - Synchronize() method
  5701. if (Token != NULL)
  5702. {
  5703. TSynchronizeParams & Params = *static_cast<TSynchronizeParams *>(Token);
  5704. if (Params.OnProcessedItem != NULL)
  5705. {
  5706. Params.OnProcessedItem(NULL, ChecklistItem);
  5707. }
  5708. }
  5709. }
  5710. //---------------------------------------------------------------------------
  5711. void __fastcall TCustomScpExplorerForm::DoFullSynchronize(
  5712. void * Token, TProcessedSynchronizationChecklistItem OnProcessedItem,
  5713. TUpdatedSynchronizationChecklistItems OnUpdatedSynchronizationChecklistItems)
  5714. {
  5715. TSynchronizeParams & Params = *static_cast<TSynchronizeParams *>(Token);
  5716. *Params.StartTime = Now();
  5717. FullSynchronize(Params, OnProcessedItem, OnUpdatedSynchronizationChecklistItems);
  5718. }
  5719. //---------------------------------------------------------------------------
  5720. void __fastcall TCustomScpExplorerForm::DoSynchronizeChecklistCalculateSize(
  5721. TSynchronizeChecklist * Checklist, const TSynchronizeChecklist::TItemList & Items, void * Token)
  5722. {
  5723. // terminal can be already closed (e.g. dropped connection)
  5724. if (Terminal != NULL)
  5725. {
  5726. TSynchronizeParams & Params = *static_cast<TSynchronizeParams *>(Token);
  5727. Terminal->SynchronizeChecklistCalculateSize(Checklist, Items, Params.CopyParam);
  5728. }
  5729. }
  5730. //---------------------------------------------------------------------------
  5731. void __fastcall TCustomScpExplorerForm::DoSynchronizeMove(
  5732. TOperationSide Side, const UnicodeString & FileName, const UnicodeString & NewFileName, TRemoteFile * RemoteFile)
  5733. {
  5734. DebugAssert(!IsLocalBrowserMode());
  5735. TAutoBatch AutoBatch(this);
  5736. TAutoFlag AutoOperationFlag(FAutoOperation);
  5737. if (Side == osRemote)
  5738. {
  5739. std::unique_ptr<TStrings> FileList(new TStringList());
  5740. FileList->AddObject(FileName, RemoteFile);
  5741. UnicodeString Target = UnixExtractFileDir(NewFileName);
  5742. UnicodeString FileMask = DelimitFileNameMask(UnixExtractFileName(NewFileName));
  5743. RemoteDirView->SaveSelection();
  5744. RemoteDirView->SaveSelectedNames();
  5745. try
  5746. {
  5747. Terminal->MoveFiles(FileList.get(), Target, FileMask);
  5748. }
  5749. catch(...)
  5750. {
  5751. RemoteDirView->DiscardSavedSelection();
  5752. throw;
  5753. }
  5754. RemoteDirView->RestoreSelection();
  5755. }
  5756. else if (DebugAlwaysTrue(Side == osLocal))
  5757. {
  5758. if (!MoveFile(FileName.c_str(), NewFileName.c_str()))
  5759. {
  5760. throw EOSExtException(FMTLOAD(RENAME_FILE_ERROR, (FileName, NewFileName)));
  5761. }
  5762. UnicodeString Directory = ExtractFileDir(FileName);
  5763. ReloadLocalDirectory(Directory);
  5764. UnicodeString NewDirectory = ExtractFileDir(NewFileName);
  5765. if (!SamePaths(Directory, NewDirectory))
  5766. {
  5767. ReloadLocalDirectory(NewDirectory);
  5768. }
  5769. }
  5770. }
  5771. //---------------------------------------------------------------------------
  5772. void __fastcall TCustomScpExplorerForm::DoSynchronizeBrowse(TOperationSide Side, TSynchronizeChecklist::TAction Action, const TSynchronizeChecklist::TItem * Item)
  5773. {
  5774. UnicodeString LocalPath = ExcludeTrailingBackslash(Item->Local.Directory);
  5775. if (Side == osLocal)
  5776. {
  5777. if (Action == TSynchronizeChecklist::saDownloadNew)
  5778. {
  5779. OpenFolderInExplorer(LocalPath);
  5780. }
  5781. else
  5782. {
  5783. OpenFileInExplorer(TPath::Combine(LocalPath, Item->GetFileName()));
  5784. }
  5785. }
  5786. else if (DebugAlwaysTrue(Side == osRemote))
  5787. {
  5788. // Similar to CreateHiddenDuplicateSession, except that it modifies the initial directories
  5789. std::unique_ptr<TSessionData> SessionData(CloneCurrentSessionData());
  5790. SessionData->RemoteDirectory = UnixExcludeTrailingBackslash(Item->Remote.Directory);
  5791. if (!LocalPath.IsEmpty())
  5792. {
  5793. SessionData->LocalDirectory = LocalPath;
  5794. }
  5795. UnicodeString SessionName = SaveHiddenDuplicateSession(SessionData.get());
  5796. ExecuteNewInstance(SessionName, FORMAT(L"%s=\"%s\"", (TProgramParams::FormatSwitch(BROWSE_SWITCH), Item->GetFileName())));
  5797. }
  5798. }
  5799. //---------------------------------------------------------------------------
  5800. void __fastcall TCustomScpExplorerForm::FullSynchronizeInNewWindow(
  5801. TSynchronizeMode Mode, int Params, const UnicodeString & LocalDirectory, const UnicodeString & RemoteDirectory,
  5802. const TCopyParamType * CopyParams)
  5803. {
  5804. UnicodeString SessionName = CreateHiddenDuplicateSession();
  5805. UnicodeString AdditionalParams =
  5806. FORMAT(L"%s%s %s // \"%s\" \"%s\" %d %d", (
  5807. TProgramParams::FormatSwitch(DEFAULTS_SWITCH),
  5808. SerializeCopyParamForCommandLine(CopyParams),
  5809. TProgramParams::FormatSwitch(SYNCHRONIZE_SWITCH),
  5810. LocalDirectory, RemoteDirectory, Mode, Params));
  5811. ExecuteNewInstance(SessionName, AdditionalParams);
  5812. }
  5813. //---------------------------------------------------------------------------
  5814. int __fastcall TCustomScpExplorerForm::DoFullSynchronizeDirectories(
  5815. UnicodeString & LocalDirectory, UnicodeString & RemoteDirectory,
  5816. TSynchronizeMode & Mode, int Params, bool & SaveMode, int UseDefaults)
  5817. {
  5818. int Result;
  5819. bool SaveSettings = false;
  5820. int Options =
  5821. FLAGMASK(!Terminal->IsCapable[fcTimestampChanging], fsoDisableTimestamp) |
  5822. FLAGMASK(SynchronizeAllowSelectedOnly(), fsoAllowSelectedOnly);
  5823. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  5824. TUsableCopyParamAttrs CopyParamAttrs = Terminal->UsableCopyParamAttrs(0);
  5825. bool Continue =
  5826. (UseDefaults == 0) ||
  5827. DoFullSynchronizeDialog(Mode, Params, LocalDirectory, RemoteDirectory,
  5828. &CopyParam, SaveSettings, SaveMode, Options, CopyParamAttrs, FullSynchronizeInNewWindow, UseDefaults);
  5829. if (Continue)
  5830. {
  5831. Configuration->Usage->Inc(L"Synchronizations");
  5832. CopyParam.IncludeFileMask.SetRoots(LocalDirectory, RemoteDirectory);
  5833. UpdateCopyParamCounters(CopyParam);
  5834. TSynchronizeOptions SynchronizeOptions;
  5835. GetSynchronizeOptions(Params, SynchronizeOptions);
  5836. if (SaveSettings)
  5837. {
  5838. GUIConfiguration->SynchronizeParams = Params;
  5839. }
  5840. else
  5841. {
  5842. SaveMode = false;
  5843. }
  5844. TDateTime StartTime = Now();
  5845. TSynchronizeChecklist * Checklist = NULL;
  5846. try
  5847. {
  5848. DebugAssert(!FAutoOperation);
  5849. FAutoOperation = true;
  5850. try
  5851. {
  5852. UnicodeString SessionKey = Terminal->SessionData->SessionKey;
  5853. std::unique_ptr<TStrings> DataList(Configuration->LoadDirectoryStatisticsCache(SessionKey, RemoteDirectory, CopyParam));
  5854. int Files = -1;
  5855. if (DataList->Count >= 1)
  5856. {
  5857. Files = StrToIntDef(DataList->Strings[0], Files);
  5858. }
  5859. else
  5860. {
  5861. DataList->Add(UnicodeString());
  5862. }
  5863. FSynchronizeProgressForm = new TSynchronizeProgressForm(Application, true, Files);
  5864. FSynchronizeProgressForm->Start();
  5865. Checklist = Terminal->SynchronizeCollect(LocalDirectory, RemoteDirectory,
  5866. static_cast<TTerminal::TSynchronizeMode>(Mode),
  5867. &CopyParam, Params | TTerminal::spNoConfirmation, TerminalSynchronizeDirectory,
  5868. &SynchronizeOptions);
  5869. if (Terminal->SessionData->CacheDirectories)
  5870. {
  5871. DataList->Strings[0] = IntToStr(SynchronizeOptions.Files);
  5872. Configuration->SaveDirectoryStatisticsCache(SessionKey, RemoteDirectory, CopyParam, DataList.get());
  5873. }
  5874. }
  5875. __finally
  5876. {
  5877. FAutoOperation = false;
  5878. SetTaskbarListProgressState(TBPF_NOPROGRESS);
  5879. SAFE_DESTROY(FSynchronizeProgressForm);
  5880. }
  5881. TSynchronizeParams SynchronizeParams;
  5882. SynchronizeParams.LocalDirectory = LocalDirectory;
  5883. SynchronizeParams.RemoteDirectory = RemoteDirectory;
  5884. SynchronizeParams.Mode = Mode;
  5885. SynchronizeParams.CopyParam = &CopyParam;
  5886. SynchronizeParams.Params = Params;
  5887. SynchronizeParams.Checklist = Checklist;
  5888. SynchronizeParams.CollectElapsed = (Now() - StartTime);
  5889. SynchronizeParams.StartTime = &StartTime;
  5890. SynchronizeParams.OnProcessedItem = NULL;
  5891. Result = Checklist->Count;
  5892. if (Checklist->Count > 0)
  5893. {
  5894. if (FLAGSET(Params, TTerminal::spPreviewChanges))
  5895. {
  5896. if (!DoSynchronizeChecklistDialog(
  5897. Checklist, Mode, Params, LocalDirectory, RemoteDirectory, CustomCommandMenu, DoFullSynchronize,
  5898. DoSynchronizeChecklistCalculateSize, DoSynchronizeMove, DoSynchronizeBrowse, &SynchronizeParams))
  5899. {
  5900. Result = -1;
  5901. }
  5902. }
  5903. else
  5904. {
  5905. FullSynchronize(SynchronizeParams, NULL, NULL);
  5906. }
  5907. }
  5908. else
  5909. {
  5910. UnicodeString Message = MainInstructions(LoadStr(COMPARE_NO_DIFFERENCES));
  5911. MessageDialog(Message, qtInformation, qaOK, HELP_SYNCHRONIZE_NO_DIFFERENCES);
  5912. }
  5913. }
  5914. __finally
  5915. {
  5916. delete Checklist;
  5917. }
  5918. OperationComplete(StartTime);
  5919. }
  5920. else
  5921. {
  5922. Result = -1;
  5923. }
  5924. return Result;
  5925. }
  5926. //---------------------------------------------------------------------------
  5927. void __fastcall TCustomScpExplorerForm::TerminalSynchronizeDirectory(
  5928. const UnicodeString & LocalDirectory, const UnicodeString & RemoteDirectory,
  5929. bool & Continue, bool Collect, const TSynchronizeOptions * Options)
  5930. {
  5931. if (Collect)
  5932. {
  5933. DebugAssert(FSynchronizeProgressForm != NULL);
  5934. if (!FSynchronizeProgressForm->Started)
  5935. {
  5936. FSynchronizeProgressForm->Start();
  5937. }
  5938. int CompareProgress = FSynchronizeProgressForm->SetData(LocalDirectory, RemoteDirectory, Options->Files, Continue);
  5939. SetTaskbarListProgressValue(CompareProgress);
  5940. }
  5941. else
  5942. {
  5943. DebugAssert(FSynchronizeProgressForm == NULL);
  5944. DebugAssert(FProgressForm != NULL);
  5945. }
  5946. }
  5947. //---------------------------------------------------------------------------
  5948. void __fastcall TCustomScpExplorerForm::StandaloneEdit(const UnicodeString & FileName)
  5949. {
  5950. UnicodeString FullFileName = AbsolutePath(Terminal->CurrentDirectory, FileName);
  5951. TRemoteFile * File = NULL;
  5952. Terminal->ReadFile(FullFileName, File);
  5953. if (File != NULL)
  5954. {
  5955. std::unique_ptr<TRemoteFile> FileOwner(File);
  5956. ExecuteRemoteFile(FullFileName, File, efInternalEditor);
  5957. Application->ShowMainForm = false;
  5958. Application->Run();
  5959. }
  5960. }
  5961. //---------------------------------------------------------------------------
  5962. void __fastcall TCustomScpExplorerForm::ExploreLocalDirectory(TOperationSide)
  5963. {
  5964. DebugFail();
  5965. }
  5966. //---------------------------------------------------------------------------
  5967. TSessionData * __fastcall TCustomScpExplorerForm::CloneCurrentSessionData()
  5968. {
  5969. std::unique_ptr<TSessionData> SessionData(new TSessionData(L""));
  5970. SessionData->Assign(ManagedSession->SessionData);
  5971. UpdateSessionData(SessionData.get());
  5972. DebugAssert(SessionData->IsLocalBrowser == IsLocalBrowserMode());
  5973. if (IsActiveTerminal(ManagedSession))
  5974. {
  5975. ManagedSession->UpdateSessionCredentials(SessionData.get());
  5976. }
  5977. if (!ManagedSession->SessionData->HasSessionName())
  5978. {
  5979. // Particularly for "Workspace/XXXX" name, we need to reset it, as it would become user-visible
  5980. // once IsWorkspace is cleared
  5981. SessionData->Name = UnicodeString();
  5982. }
  5983. // Most uses of this method ends with saving the session, and we do not want to store the IsWorkspace flag.
  5984. // Particularly, with SaveHiddenDuplicateSession, it won't work as TSessionData::ParseUrl explicitly ignored workspace sessions.
  5985. SessionData->IsWorkspace = false;
  5986. return SessionData.release();
  5987. }
  5988. //---------------------------------------------------------------------------
  5989. void __fastcall TCustomScpExplorerForm::SaveCurrentSession()
  5990. {
  5991. TSessionData * SessionData = CloneCurrentSessionData();
  5992. try
  5993. {
  5994. TSessionData * EditingSessionData = StoredSessions->FindSame(SessionData);
  5995. DoSaveSession(SessionData, EditingSessionData, true, NULL);
  5996. }
  5997. __finally
  5998. {
  5999. delete SessionData;
  6000. }
  6001. }
  6002. //---------------------------------------------------------------------------
  6003. TObjectList * __fastcall TCustomScpExplorerForm::DoCollectWorkspace()
  6004. {
  6005. TTerminalManager * Manager = TTerminalManager::Instance();
  6006. std::unique_ptr<TObjectList> DataList(new TObjectList());
  6007. if (DebugAlwaysTrue(ManagedSession != NULL))
  6008. {
  6009. // Update ManagedSession->StateData
  6010. UpdateSession(ManagedSession);
  6011. }
  6012. Manager->SaveWorkspace(DataList.get());
  6013. return DataList.release();
  6014. }
  6015. //---------------------------------------------------------------------------
  6016. void __fastcall TCustomScpExplorerForm::DoSaveWorkspace(
  6017. const UnicodeString & Name, TObjectList * DataList, bool SavePasswords, bool Explicit)
  6018. {
  6019. WinConfiguration->LastWorkspace = Name;
  6020. if (!SavePasswords)
  6021. {
  6022. for (int Index = 0; Index < DataList->Count; Index++)
  6023. {
  6024. TSessionData * SessionData = dynamic_cast<TSessionData *>(DataList->Items[Index]);
  6025. if (SessionData->Link.IsEmpty())
  6026. {
  6027. SessionData->ClearSessionPasswords();
  6028. }
  6029. }
  6030. }
  6031. StoredSessions->NewWorkspace(Name, DataList);
  6032. // modified only
  6033. StoredSessions->Save(false, Explicit);
  6034. }
  6035. //---------------------------------------------------------------------------
  6036. UnicodeString __fastcall TCustomScpExplorerForm::WorkspaceName()
  6037. {
  6038. return
  6039. DefaultStr(WinConfiguration->LastWorkspace,
  6040. DefaultStr(WinConfiguration->AutoWorkspace, LoadStr(NEW_WORKSPACE)));
  6041. }
  6042. //---------------------------------------------------------------------------
  6043. bool __fastcall TCustomScpExplorerForm::SaveWorkspace(bool EnableAutoSave)
  6044. {
  6045. std::unique_ptr<TObjectList> DataList(DoCollectWorkspace());
  6046. bool AnyNonStoredSessionWithPassword = false;
  6047. bool AnyNonStoredNonWorkspaceSessionWithPassword = false;
  6048. bool AllNonStoredSessionsAnonymous = true;
  6049. TTerminalManager * Manager = TTerminalManager::Instance();
  6050. for (int Index = 0; Index < DataList->Count; Index++)
  6051. {
  6052. TSessionData * SessionData = dynamic_cast<TSessionData *>(DataList->Items[Index]);
  6053. if (SessionData->Link.IsEmpty())
  6054. {
  6055. if (SessionData->HasAnySessionPassword())
  6056. {
  6057. AnyNonStoredSessionWithPassword = true;
  6058. if (!Manager->Sessions[Index]->SessionData->IsWorkspace)
  6059. {
  6060. AnyNonStoredNonWorkspaceSessionWithPassword = true;
  6061. }
  6062. }
  6063. if (!SameText(SessionData->UserName, AnonymousUserName))
  6064. {
  6065. AllNonStoredSessionsAnonymous = false;
  6066. }
  6067. }
  6068. }
  6069. bool SavePasswords;
  6070. bool * PSavePasswords;
  6071. bool NotRecommendedSavingPasswords =
  6072. !CustomWinConfiguration->UseMasterPassword &&
  6073. !AllNonStoredSessionsAnonymous;
  6074. if (Configuration->DisablePasswordStoring ||
  6075. !AnyNonStoredSessionWithPassword)
  6076. {
  6077. PSavePasswords = NULL;
  6078. SavePasswords = false;
  6079. }
  6080. else
  6081. {
  6082. PSavePasswords = &SavePasswords;
  6083. SavePasswords =
  6084. !AnyNonStoredNonWorkspaceSessionWithPassword ||
  6085. !NotRecommendedSavingPasswords;
  6086. }
  6087. UnicodeString Name = WorkspaceName();
  6088. bool CreateShortcut = false;
  6089. bool Result =
  6090. DoSaveWorkspaceDialog(
  6091. Name, PSavePasswords, NotRecommendedSavingPasswords, CreateShortcut,
  6092. EnableAutoSave);
  6093. if (Result)
  6094. {
  6095. DoSaveWorkspace(Name, DataList.get(), SavePasswords, true);
  6096. if (CreateShortcut)
  6097. {
  6098. TOperationVisualizer Visualizer;
  6099. UnicodeString AdditionalParams = TProgramParams::FormatSwitch(DESKTOP_SWITCH);
  6100. CreateDesktopSessionShortCut(Name, L"", AdditionalParams, -1, WORKSPACE_ICON);
  6101. }
  6102. if (EnableAutoSave)
  6103. {
  6104. WinConfiguration->AutoSaveWorkspace = true;
  6105. WinConfiguration->AutoWorkspace = Name;
  6106. if (PSavePasswords != NULL)
  6107. {
  6108. WinConfiguration->AutoSaveWorkspacePasswords = SavePasswords;
  6109. }
  6110. }
  6111. }
  6112. return Result;
  6113. }
  6114. //---------------------------------------------------------------------------
  6115. void __fastcall TCustomScpExplorerForm::UpdateSession(TManagedTerminal * Session)
  6116. {
  6117. DebugAssert(IsLocalBrowserMode() == Session->LocalBrowser);
  6118. DebugAssert(Session == ManagedSession);
  6119. if (!IsLocalBrowserMode())
  6120. {
  6121. SAFE_DESTROY(Session->RemoteExplorerState);
  6122. if (WinConfiguration->PreservePanelState)
  6123. {
  6124. Session->RemoteExplorerState = RemoteDirView->SaveState();
  6125. }
  6126. }
  6127. UpdateSessionData(Session->StateData);
  6128. }
  6129. //---------------------------------------------------------------------------
  6130. void __fastcall TCustomScpExplorerForm::UpdateSessionData(TSessionData * Data)
  6131. {
  6132. // Keep in sync with TSessionData::CopyStateData
  6133. DebugAssert(Data != NULL);
  6134. // This is inconsistent with how color (for example) is handled.
  6135. Data->Name = ManagedSession->SessionData->Name;
  6136. if (!IsLocalBrowserMode())
  6137. {
  6138. // cannot use RemoteDirView->Path, because it is empty if connection
  6139. // was already closed
  6140. // also only peek, we may not be connected at all atm
  6141. // so make sure we do not try retrieving current directory from the server
  6142. // (particularly with FTP)
  6143. UnicodeString ACurrentDirectory = Terminal->PeekCurrentDirectory();
  6144. if (!ACurrentDirectory.IsEmpty())
  6145. {
  6146. Data->RemoteDirectory = ACurrentDirectory;
  6147. }
  6148. }
  6149. Data->Color = SessionColor;
  6150. }
  6151. //---------------------------------------------------------------------------
  6152. void __fastcall TCustomScpExplorerForm::ToolBarResize(TObject *Sender)
  6153. {
  6154. TTBXToolbar * Toolbar = dynamic_cast<TTBXToolbar*>(Sender);
  6155. DebugAssert(Toolbar != NULL);
  6156. for (int i = 0; i < Toolbar->Items->Count; i++)
  6157. {
  6158. TTBXCustomDropDownItem * DropDownItem;
  6159. DropDownItem = dynamic_cast<TTBXCustomDropDownItem *>(Toolbar->Items->Items[i]);
  6160. if (DropDownItem != NULL)
  6161. {
  6162. ToolbarItemResize(DropDownItem,
  6163. Toolbar->Width - (Toolbar->View->BaseSize.x - DropDownItem->EditWidth) -
  6164. Toolbar->NonClientWidth);
  6165. break;
  6166. }
  6167. }
  6168. }
  6169. //---------------------------------------------------------------------------
  6170. void __fastcall TCustomScpExplorerForm::ToolbarItemResize(TTBXCustomDropDownItem * Item, int Width)
  6171. {
  6172. Item->EditWidth = Width;
  6173. }
  6174. //---------------------------------------------------------------------------
  6175. void __fastcall TCustomScpExplorerForm::ToolbarGetBaseSize(
  6176. TTBCustomToolbar * Toolbar, TPoint & ASize)
  6177. {
  6178. for (int i = 0; i < Toolbar->Items->Count; i++)
  6179. {
  6180. TTBXCustomDropDownItem * DropDownItem;
  6181. DropDownItem = dynamic_cast<TTBXCustomDropDownItem *>(Toolbar->Items->Items[i]);
  6182. if (DropDownItem != NULL)
  6183. {
  6184. ASize.x -= DropDownItem->EditWidth;
  6185. ASize.x += ScaleByTextHeight(this, 50) /* minimal combo width */;
  6186. }
  6187. }
  6188. }
  6189. //---------------------------------------------------------------------------
  6190. void __fastcall TCustomScpExplorerForm::DoWarnLackOfTempSpace(
  6191. const UnicodeString Path, __int64 RequiredSpace, bool & Continue)
  6192. {
  6193. if (WinConfiguration->DDWarnLackOfTempSpace)
  6194. {
  6195. UnicodeString ADrive = ExtractFileDrive(ExpandFileName(Path));
  6196. if (!ADrive.IsEmpty())
  6197. {
  6198. __int64 FreeSpace = DiskFree((Byte)(ADrive[1]-'A'+1));
  6199. DebugAssert(RequiredSpace >= 0);
  6200. __int64 RequiredWithReserve;
  6201. RequiredWithReserve = (__int64)(RequiredSpace * WinConfiguration->DDWarnLackOfTempSpaceRatio);
  6202. if (FreeSpace < RequiredWithReserve)
  6203. {
  6204. unsigned int Result;
  6205. TMessageParams Params(mpNeverAskAgainCheck);
  6206. Result = MessageDialog(FMTLOAD(DD_WARN_LACK_OF_TEMP_SPACE, (Path,
  6207. FormatBytes(FreeSpace), FormatBytes(RequiredSpace))),
  6208. qtWarning, qaYes | qaNo, HELP_DD_WARN_LACK_OF_TEMP_SPACE, &Params);
  6209. if (Result == qaNeverAskAgain)
  6210. {
  6211. WinConfiguration->DDWarnLackOfTempSpace = false;
  6212. }
  6213. Continue = (Result == qaYes || Result == qaNeverAskAgain);
  6214. }
  6215. }
  6216. }
  6217. }
  6218. //---------------------------------------------------------------------------
  6219. void __fastcall TCustomScpExplorerForm::AddBookmark(TOperationSide Side)
  6220. {
  6221. DoOpenDirectoryDialog(odAddBookmark, Side);
  6222. }
  6223. //---------------------------------------------------------------------------
  6224. TStrings * __fastcall TCustomScpExplorerForm::CreateVisitedDirectories(TOperationSide Side)
  6225. {
  6226. // we should better use TCustomDirView::FCaseSensitive, but it is private
  6227. TStringList * VisitedDirectories = CreateSortedStringList(!IsSideLocalBrowser(Side));
  6228. try
  6229. {
  6230. TCustomDirView * DView = DirView(Side);
  6231. for (int Index = -DView->BackCount; Index <= DView->ForwardCount; Index++)
  6232. {
  6233. VisitedDirectories->Add(DView->HistoryPath[Index]);
  6234. }
  6235. }
  6236. catch (...)
  6237. {
  6238. delete VisitedDirectories;
  6239. throw;
  6240. }
  6241. return VisitedDirectories;
  6242. }
  6243. //---------------------------------------------------------------------------
  6244. void __fastcall TCustomScpExplorerForm::DoOpenDirectoryDialog(
  6245. TOpenDirectoryMode Mode, TOperationSide Side)
  6246. {
  6247. bool Continue = true;
  6248. if (Mode == odAddBookmark)
  6249. {
  6250. TMessageParams Params(mpNeverAskAgainCheck);
  6251. Params.NeverAskAgainTitle = LoadStr(ADD_BOOKMARK_SHARED);
  6252. Params.NeverAskAgainCheckedInitially = WinConfiguration->UseSharedBookmarks;
  6253. UnicodeString Message = MainInstructions(FMTLOAD(ADD_BOOKMARK_CONFIRM, (DirView(Side)->PathName)));
  6254. unsigned int Answer =
  6255. MessageDialog(Message,
  6256. qtConfirmation, qaOK | qaCancel, HELP_ADD_BOOKMARK_CONFIRM, &Params);
  6257. if (Answer == qaNeverAskAgain)
  6258. {
  6259. Continue = true;
  6260. WinConfiguration->UseSharedBookmarks = true;
  6261. }
  6262. else if (Answer == qaOK)
  6263. {
  6264. Continue = true;
  6265. WinConfiguration->UseSharedBookmarks = false;
  6266. }
  6267. else
  6268. {
  6269. Continue = false;
  6270. }
  6271. }
  6272. if (Continue)
  6273. {
  6274. TStrings * VisitedDirectories = CreateVisitedDirectories(Side);
  6275. try
  6276. {
  6277. UnicodeString Name = DirView(Side)->PathName;
  6278. bool Repeat;
  6279. do
  6280. {
  6281. Repeat = false;
  6282. TOperationSide BookmarkSide = IsSideLocalBrowser(Side) ? osLocal : osRemote;
  6283. if (::DoOpenDirectoryDialog(Mode, BookmarkSide, Name, VisitedDirectories, Terminal,
  6284. // do not allow switching to location profiles,
  6285. // if we are not connected
  6286. HasDirView[osLocal] && (Terminal != NULL)))
  6287. {
  6288. TWindowLock Lock(this);
  6289. if (!TryOpenDirectory(Side, Name))
  6290. {
  6291. Repeat = true;
  6292. Mode = odBrowse;
  6293. }
  6294. }
  6295. }
  6296. while (Repeat);
  6297. }
  6298. __finally
  6299. {
  6300. delete VisitedDirectories;
  6301. }
  6302. }
  6303. }
  6304. //---------------------------------------------------------------------------
  6305. bool __fastcall TCustomScpExplorerForm::CommandSessionFallback()
  6306. {
  6307. DebugAssert(!Terminal->CommandSessionOpened);
  6308. return TTerminalManager::Instance()->ConnectTerminal(Terminal->CommandSession);
  6309. }
  6310. //---------------------------------------------------------------------------
  6311. bool __fastcall TCustomScpExplorerForm::EnsureCommandSessionFallback(TFSCapability Capability)
  6312. {
  6313. bool Result = Terminal->IsCapable[Capability] ||
  6314. Terminal->CommandSessionOpened;
  6315. if (!Result)
  6316. {
  6317. DebugAssert(Terminal->IsCapable[fcSecondaryShell]);
  6318. if (!GUIConfiguration->ConfirmCommandSession)
  6319. {
  6320. Result = true;
  6321. }
  6322. else
  6323. {
  6324. TMessageParams Params(mpNeverAskAgainCheck);
  6325. const TFileSystemInfo & FileSystemInfo = Terminal->GetFileSystemInfo();
  6326. unsigned int Answer = MessageDialog(FMTLOAD(PERFORM_ON_COMMAND_SESSION2,
  6327. (FileSystemInfo.ProtocolName, FileSystemInfo.ProtocolName)), qtConfirmation,
  6328. qaOK | qaCancel, HELP_PERFORM_ON_COMMAND_SESSION, &Params);
  6329. if (Answer == qaNeverAskAgain)
  6330. {
  6331. GUIConfiguration->ConfirmCommandSession = false;
  6332. Result = true;
  6333. }
  6334. else if (Answer == qaOK)
  6335. {
  6336. Result = true;
  6337. }
  6338. }
  6339. if (Result)
  6340. {
  6341. Result = CommandSessionFallback();
  6342. }
  6343. }
  6344. return Result;
  6345. }
  6346. //---------------------------------------------------------------------------
  6347. void __fastcall TCustomScpExplorerForm::OpenConsole(UnicodeString Command)
  6348. {
  6349. if (EnsureCommandSessionFallback(fcAnyCommand))
  6350. {
  6351. DoConsoleDialog(Terminal, Command);
  6352. }
  6353. }
  6354. //---------------------------------------------------------------------------
  6355. void __fastcall TCustomScpExplorerForm::FileControlDDDragEnter(
  6356. TObject *Sender, _di_IDataObject /*DataObj*/, int /*grfKeyState*/,
  6357. const TPoint & /*Point*/, int & /*dwEffect*/, bool & Accept)
  6358. {
  6359. if (!IsSideLocalBrowser(osRemote) &&
  6360. IsFileControl(DropSourceControl, osRemote) &&
  6361. (FDDExtMapFile != NULL))
  6362. {
  6363. Accept = true;
  6364. }
  6365. FDDTargetControl = dynamic_cast<TControl*>(Sender);
  6366. DebugAssert(FDDTargetControl != NULL);
  6367. }
  6368. //---------------------------------------------------------------------------
  6369. void __fastcall TCustomScpExplorerForm::SessionsDDDragEnter(
  6370. _di_IDataObject DataObj, int KeyState,
  6371. const TPoint & Point, int & Effect, bool & Accept)
  6372. {
  6373. FileControlDDDragEnter(SessionsPageControl, DataObj, KeyState, Point, Effect, Accept);
  6374. }
  6375. //---------------------------------------------------------------------------
  6376. void __fastcall TCustomScpExplorerForm::QueueDDDragEnter(
  6377. _di_IDataObject DataObj, int KeyState,
  6378. const TPoint & Point, int & Effect, bool & Accept)
  6379. {
  6380. FileControlDDDragEnter(QueueView3, DataObj, KeyState, Point, Effect, Accept);
  6381. }
  6382. //---------------------------------------------------------------------------
  6383. void __fastcall TCustomScpExplorerForm::FileControlDDDragLeave(
  6384. TObject * Sender)
  6385. {
  6386. DebugUsedParam(Sender);
  6387. DebugAssert(FDDTargetControl == Sender);
  6388. FDDTargetControl = NULL;
  6389. }
  6390. //---------------------------------------------------------------------------
  6391. void __fastcall TCustomScpExplorerForm::SessionsDDDragLeave()
  6392. {
  6393. FileControlDDDragLeave(SessionsPageControl);
  6394. }
  6395. //---------------------------------------------------------------------------
  6396. void __fastcall TCustomScpExplorerForm::QueueDDDragLeave()
  6397. {
  6398. FileControlDDDragLeave(QueueView3);
  6399. }
  6400. //---------------------------------------------------------------------------
  6401. void __fastcall TCustomScpExplorerForm::AddEditLink(TOperationSide Side, bool Add)
  6402. {
  6403. DebugAssert(!IsSideLocalBrowser(Side));
  6404. DebugUsedParam(Side);
  6405. bool Edit = false;
  6406. TRemoteFile * File = NULL;
  6407. UnicodeString FileName;
  6408. UnicodeString PointTo;
  6409. bool SymbolicLink = true;
  6410. if (RemoteDirView->ItemFocused)
  6411. {
  6412. DebugAssert(RemoteDirView->ItemFocused->Data);
  6413. File = (TRemoteFile *)RemoteDirView->ItemFocused->Data;
  6414. Edit = !Add && File->IsSymLink && Terminal->SessionData->ResolveSymlinks;
  6415. if (Edit)
  6416. {
  6417. FileName = File->FileName;
  6418. PointTo = File->LinkTo;
  6419. }
  6420. else if (!File->IsParentDirectory)
  6421. {
  6422. PointTo = File->FileName;
  6423. }
  6424. }
  6425. if (DoSymlinkDialog(FileName, PointTo, osRemote, SymbolicLink, Edit,
  6426. Terminal->IsCapable[fcHardLink]))
  6427. {
  6428. Configuration->Usage->Inc(L"RemoteLinksCreated");
  6429. if (Edit)
  6430. {
  6431. DebugAssert(File->FileName == FileName);
  6432. int Params = dfNoRecursive;
  6433. Terminal->ExceptionOnFail = true;
  6434. try
  6435. {
  6436. Terminal->DeleteFile(L"", File, &Params);
  6437. }
  6438. __finally
  6439. {
  6440. Terminal->ExceptionOnFail = false;
  6441. }
  6442. }
  6443. Terminal->CreateLink(FileName, PointTo, SymbolicLink);
  6444. }
  6445. }
  6446. //---------------------------------------------------------------------------
  6447. bool __fastcall TCustomScpExplorerForm::CanAddEditLink(TOperationSide Side)
  6448. {
  6449. return
  6450. (IsSideLocalBrowser(Side) ||
  6451. (HasActiveTerminal() &&
  6452. Terminal->ResolvingSymlinks &&
  6453. Terminal->IsCapable[fcSymbolicLink]));
  6454. }
  6455. //---------------------------------------------------------------------------
  6456. bool __fastcall TCustomScpExplorerForm::LinkFocused()
  6457. {
  6458. return
  6459. !IsSideLocalBrowser(FCurrentSide) &&
  6460. (RemoteDirView->ItemFocused != NULL) &&
  6461. ((TRemoteFile *)RemoteDirView->ItemFocused->Data)->IsSymLink &&
  6462. Terminal->SessionData->ResolveSymlinks;
  6463. }
  6464. //---------------------------------------------------------------------------
  6465. void __fastcall TCustomScpExplorerForm::ExecuteCurrentFile()
  6466. {
  6467. DebugAssert(!WinConfiguration->DisableOpenEdit);
  6468. FForceExecution = true;
  6469. try
  6470. {
  6471. DirView(osCurrent)->ExecuteCurrentFile();
  6472. }
  6473. __finally
  6474. {
  6475. FForceExecution = false;
  6476. }
  6477. }
  6478. //---------------------------------------------------------------------------
  6479. void __fastcall TCustomScpExplorerForm::ExecuteCurrentFileWith(bool OnFocused)
  6480. {
  6481. TEditorData ExternalEditor;
  6482. ExternalEditor.Editor = edExternal;
  6483. bool Remember = false;
  6484. if (DoEditorPreferencesDialog(&ExternalEditor, Remember, epmAdHoc, !IsSideLocalBrowser(osCurrent)))
  6485. {
  6486. if (Remember)
  6487. {
  6488. TEditorList * EditorList = new TEditorList;
  6489. try
  6490. {
  6491. *EditorList = *WinConfiguration->EditorList;
  6492. bool Found = false;
  6493. int i = 0;
  6494. while (!Found && (i < EditorList->Count))
  6495. {
  6496. const TEditorPreferences * Editor = EditorList->Editors[i];
  6497. if ((Editor->Data->Editor == edExternal) &&
  6498. (Editor->Data->ExternalEditor == ExternalEditor.ExternalEditor))
  6499. {
  6500. Found = true;
  6501. }
  6502. i++;
  6503. }
  6504. if (!Found)
  6505. {
  6506. EditorList->Add(new TEditorPreferences(ExternalEditor));
  6507. WinConfiguration->EditorList = EditorList;
  6508. }
  6509. }
  6510. __finally
  6511. {
  6512. delete EditorList;
  6513. }
  6514. }
  6515. ExecuteFile(osCurrent, efExternalEditor, &ExternalEditor, true, OnFocused);
  6516. }
  6517. }
  6518. //---------------------------------------------------------------------------
  6519. void __fastcall TCustomScpExplorerForm::DetachTerminal(TObject * ATerminal)
  6520. {
  6521. FEditorManager->ProcessFiles(FileTerminalRemoved, ATerminal);
  6522. if (FFileFindTerminal == ATerminal)
  6523. {
  6524. FFileFindTerminal = NULL;
  6525. HideFileFindDialog();
  6526. }
  6527. if (FClipboardTerminal == ATerminal)
  6528. {
  6529. ClipboardClear(); // implies ClipboardStop
  6530. }
  6531. if ((FOnSynchronizeAbort != NULL) &&
  6532. (ATerminal == FManagedSession))
  6533. {
  6534. FOnSynchronizeAbort(NULL);
  6535. }
  6536. }
  6537. //---------------------------------------------------------------------------
  6538. void __fastcall TCustomScpExplorerForm::TerminalRemoved(TObject * Sender)
  6539. {
  6540. DetachTerminal(Sender);
  6541. }
  6542. //---------------------------------------------------------------------------
  6543. void __fastcall TCustomScpExplorerForm::FileTerminalRemoved(const UnicodeString FileName,
  6544. TEditedFileData * Data, TObject * /*Token*/, void * Arg)
  6545. {
  6546. TTerminal * Terminal = static_cast<TTerminal *>(Arg);
  6547. DebugAssert(Terminal != NULL);
  6548. if (Data->Terminal == Terminal)
  6549. {
  6550. Data->Terminal = NULL;
  6551. }
  6552. }
  6553. //---------------------------------------------------------------------------
  6554. TManagedTerminal * TCustomScpExplorerForm::GetReplacementForLastSession()
  6555. {
  6556. return NULL;
  6557. }
  6558. //---------------------------------------------------------------------------
  6559. void __fastcall TCustomScpExplorerForm::LastTerminalClosed()
  6560. {
  6561. UpdateControls();
  6562. SessionColor = TColor(0);
  6563. UpdateRemotePathComboBox(false);
  6564. NeedSession(false);
  6565. }
  6566. //---------------------------------------------------------------------------
  6567. void __fastcall TCustomScpExplorerForm::NeedSession(bool Startup)
  6568. {
  6569. try
  6570. {
  6571. // Cache, as the login dialog can change its value.
  6572. // See also DoShow. Also see TTerminalManager::NewSession.
  6573. bool ShowLogin = WinConfiguration->ShowLoginWhenNoSession;
  6574. try
  6575. {
  6576. if (ShowLogin)
  6577. {
  6578. bool ReloadSessions = !Startup;
  6579. TTerminalManager::Instance()->NewSession(L"", ReloadSessions, this, true);
  6580. }
  6581. }
  6582. __finally
  6583. {
  6584. // Do not terminate, if we are only starting up and we are not showing Login dialog
  6585. // (so there was no chance for the user to open any session yet)
  6586. if ((ShowLogin || !Startup) &&
  6587. !WinConfiguration->KeepOpenWhenNoSession &&
  6588. // this rules out the implicit Commander's local-local browser
  6589. ((Terminal == NULL) || (!Terminal->Active && !Terminal->Permanent)))
  6590. {
  6591. TerminateApplication();
  6592. }
  6593. }
  6594. }
  6595. catch (EAbort &)
  6596. {
  6597. // swallow
  6598. // The TTerminalManager does not expect the OnLastTerminalClose to throw without trying to connect.
  6599. // Also when called from TWinControl.UpdateShowing => CMShowingChanged, the showing is aborted on exception
  6600. }
  6601. }
  6602. //---------------------------------------------------------------------------
  6603. void __fastcall TCustomScpExplorerForm::SessionListChanged(bool ForceTruncationUpdate)
  6604. {
  6605. TTerminalManager * Manager = TTerminalManager::Instance();
  6606. DebugAssert(!Manager->Updating);
  6607. TAutoFlag UpdatingSessionTabsFlag(FUpdatingSessionTabs);
  6608. int ActiveSessionIndex = Manager->ActiveSessionIndex;
  6609. Configuration->Usage->SetMax(L"MaxOpenedSessions", Manager->Count);
  6610. SendMessage(SessionsPageControl->Handle, WM_SETREDRAW, 0, 0);
  6611. try
  6612. {
  6613. int PrevTabsWidth = SessionsPageControl->TotalTabsWidth();
  6614. while (SessionsPageControl->PageCount > Manager->Count + 1)
  6615. {
  6616. delete SessionsPageControl->Pages[SessionsPageControl->PageCount - 1];
  6617. }
  6618. for (int Index = 0; Index <= Manager->Count; Index++)
  6619. {
  6620. TThemeTabSheet * TabSheet;
  6621. if (Index >= SessionsPageControl->PageCount)
  6622. {
  6623. TabSheet = new TThemeTabSheet(SessionsPageControl);
  6624. TabSheet->PageControl = SessionsPageControl;
  6625. }
  6626. else
  6627. {
  6628. TabSheet = SessionsPageControl->Pages[Index];
  6629. }
  6630. bool IsSessionTab = (Index < Manager->Count);
  6631. if (IsSessionTab)
  6632. {
  6633. TTerminal * Terminal = Manager->Sessions[Index];
  6634. TabSheet->Tag = reinterpret_cast<int>(Terminal);
  6635. TabSheet->CaptionTruncation = tttEllipsis;
  6636. UpdateSessionTab(TabSheet);
  6637. }
  6638. else
  6639. {
  6640. TabSheet->Tag = 0; // not really needed
  6641. TabSheet->Shadowed = false;
  6642. TabSheet->Button = SupportsLocalBrowser() ? ttbDropDown : ttbNone;
  6643. TabSheet->CaptionTruncation = tttNoText;
  6644. // 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)
  6645. UpdateNewTabTab();
  6646. }
  6647. }
  6648. // Prevent flicker on the right side where scrolling buttons would be, unless really needed
  6649. if (ForceTruncationUpdate || (SessionsPageControl->TotalTabsWidth() != PrevTabsWidth))
  6650. {
  6651. SessionsPageControl->UpdateTabsCaptionTruncation();
  6652. }
  6653. }
  6654. __finally
  6655. {
  6656. SendMessage(SessionsPageControl->Handle, WM_SETREDRAW, 1, 0);
  6657. }
  6658. SessionsPageControl->ActivePageIndex = ActiveSessionIndex;
  6659. }
  6660. //---------------------------------------------------------------------------
  6661. UnicodeString TCustomScpExplorerForm::GetNewTabTabCaption()
  6662. {
  6663. return StripHotkey(StripTrailingPunctuation(NonVisualDataModule->NewTabAction->Caption));
  6664. }
  6665. //---------------------------------------------------------------------------
  6666. void __fastcall TCustomScpExplorerForm::UpdateNewTabTab()
  6667. {
  6668. TThemeTabSheet * TabSheet = SessionsPageControl->Pages[SessionsPageControl->PageCount - 1];
  6669. UnicodeString TabCaption;
  6670. if (WinConfiguration->SelectiveToolbarText)
  6671. {
  6672. TabCaption = GetNewTabTabCaption();
  6673. }
  6674. TabSheet->BaseCaption = TabCaption;
  6675. TabSheet->ImageIndex = GetNewTabTabImageIndex(osCurrent);
  6676. }
  6677. //---------------------------------------------------------------------------
  6678. TManagedTerminal * __fastcall TCustomScpExplorerForm::GetSessionTabSession(TTabSheet * TabSheet)
  6679. {
  6680. return reinterpret_cast<TManagedTerminal *>(TabSheet->Tag);
  6681. }
  6682. //---------------------------------------------------------------------------
  6683. UnicodeString TCustomScpExplorerForm::GetLocalBrowserSessionTitle(TManagedTerminal *)
  6684. {
  6685. DebugFail();
  6686. return UnicodeString();
  6687. }
  6688. //---------------------------------------------------------------------------
  6689. void TCustomScpExplorerForm::UpdateSessionTab(TThemeTabSheet * TabSheet)
  6690. {
  6691. if (DebugAlwaysTrue(TabSheet != NULL))
  6692. {
  6693. TManagedTerminal * ASession = GetSessionTabSession(TabSheet);
  6694. if (DebugAlwaysTrue(ASession != NULL))
  6695. {
  6696. TColor Color = (ASession == Terminal) ? FSessionColor : ASession->StateData->Color;
  6697. if (ASession->LocalBrowser)
  6698. {
  6699. TabSheet->ImageIndex = FLocalBrowserTabImageIndex;
  6700. }
  6701. else
  6702. {
  6703. TabSheet->ImageIndex = AddSessionColor(Color);
  6704. }
  6705. TTerminalManager * Manager = TTerminalManager::Instance();
  6706. TabSheet->Shadowed = !ASession->Active && !ASession->LocalBrowser;
  6707. TabSheet->Button = CanCloseSession(ASession) ? ttbClose : ttbNone;
  6708. TabSheet->BaseCaption = Manager->GetSessionTitle(ASession, true);
  6709. }
  6710. }
  6711. }
  6712. //---------------------------------------------------------------------------
  6713. bool TCustomScpExplorerForm::CanCloseSession(TManagedTerminal * Session)
  6714. {
  6715. return !Session->LocalBrowser || (TTerminalManager::Instance()->Count > 1);
  6716. }
  6717. //---------------------------------------------------------------------------
  6718. bool __fastcall TCustomScpExplorerForm::SessionTabSwitched()
  6719. {
  6720. DebugAssert(SessionsPageControl->ActivePage != NULL);
  6721. TManagedTerminal * Session = GetSessionTabSession(SessionsPageControl->ActivePage);
  6722. bool Result = (Session != NULL);
  6723. if (Result)
  6724. {
  6725. TTerminalManager::Instance()->ActiveSession = Session;
  6726. }
  6727. else
  6728. {
  6729. try
  6730. {
  6731. NewTab();
  6732. }
  6733. __finally
  6734. {
  6735. SessionListChanged();
  6736. }
  6737. FSessionsPageControlNewTabTime = Now();
  6738. }
  6739. return Result;
  6740. }
  6741. //---------------------------------------------------------------------------
  6742. void __fastcall TCustomScpExplorerForm::SessionsPageControlChange(TObject * /*Sender*/)
  6743. {
  6744. TWindowLock Lock(this);
  6745. SessionTabSwitched();
  6746. }
  6747. //---------------------------------------------------------------------------
  6748. void __fastcall TCustomScpExplorerForm::TransferListChange(TObject * Sender)
  6749. {
  6750. TTBXStringList * TransferList = dynamic_cast<TTBXStringList *>(Sender);
  6751. DebugAssert(TransferList != NULL);
  6752. UnicodeString Name;
  6753. if (TransferList->ItemIndex <= 0)
  6754. {
  6755. Name = L"";
  6756. }
  6757. else
  6758. {
  6759. Name = GUIConfiguration->CopyParamList->Names[TransferList->ItemIndex - 1];
  6760. }
  6761. if (FCopyParamAutoSelected.IsEmpty())
  6762. {
  6763. // if previous preset was not autoselected, make new preset the "default"
  6764. FCopyParamDefault = Name;
  6765. }
  6766. GUIConfiguration->CopyParamCurrent = Name;
  6767. }
  6768. //---------------------------------------------------------------------------
  6769. void __fastcall TCustomScpExplorerForm::UpdateTransferLabel()
  6770. {
  6771. // sanity check
  6772. bool ExistingPreset =
  6773. (FTransferListHoverIndex >= 0) &&
  6774. (FTransferListHoverIndex < 1 + GUIConfiguration->CopyParamList->Count);
  6775. DebugAssert(ExistingPreset);
  6776. if (ExistingPreset)
  6777. {
  6778. HDC DC = GetDC(0);
  6779. TCanvas * Canvas = new TCanvas();
  6780. try
  6781. {
  6782. Canvas->Handle = DC;
  6783. Canvas->Font = GetToolbarFont(this);
  6784. UnicodeString Name;
  6785. if (FTransferListHoverIndex == 0)
  6786. {
  6787. Name = L"";
  6788. }
  6789. else
  6790. {
  6791. Name = GUIConfiguration->CopyParamList->Names[FTransferListHoverIndex - 1];
  6792. }
  6793. TTBXLabelItem * TransferLabel = dynamic_cast<TTBXLabelItem*>(
  6794. static_cast<TComponent*>(GetComponent(fcTransferLabel)));
  6795. TTBXStringList * TransferList = dynamic_cast<TTBXStringList*>(
  6796. static_cast<TObject*>(GetComponent(fcTransferList)));
  6797. UnicodeString InfoStr =
  6798. GUIConfiguration->CopyParamPreset[Name].
  6799. GetInfoStr(L"; ",
  6800. FLAGMASK(HasActiveTerminal(), Terminal->UsableCopyParamAttrs(0).General));
  6801. int MaxWidth = TransferList->MinWidth - (2 * TransferLabel->Margin) - ScaleByTextHeight(this, 10);
  6802. if (Canvas->TextExtent(InfoStr).cx > MaxWidth)
  6803. {
  6804. while (Canvas->TextExtent(InfoStr + Ellipsis).cx > MaxWidth)
  6805. {
  6806. InfoStr.SetLength(InfoStr.Length() - 1);
  6807. }
  6808. InfoStr += Ellipsis;
  6809. }
  6810. // UpdateCaption does not cause invalidation of whole submenu, while
  6811. // setting Caption property does.
  6812. // also it probably does not resize the label, even if necessary
  6813. // (we do not want that anyway)
  6814. TransferLabel->UpdateCaption(InfoStr);
  6815. }
  6816. __finally
  6817. {
  6818. Canvas->Handle = NULL;
  6819. ReleaseDC(0, DC);
  6820. delete Canvas;
  6821. }
  6822. }
  6823. }
  6824. //---------------------------------------------------------------------------
  6825. void __fastcall TCustomScpExplorerForm::TransferListDrawItem(
  6826. TTBXCustomList * /*Sender*/, TCanvas * /*ACanvas*/, const TRect & /*ARect*/,
  6827. int /*AIndex*/, int AHoverIndex, bool & /*DrawDefault*/)
  6828. {
  6829. if (FTransferListHoverIndex != AHoverIndex)
  6830. {
  6831. FTransferListHoverIndex = AHoverIndex;
  6832. UpdateTransferLabel();
  6833. }
  6834. }
  6835. //---------------------------------------------------------------------------
  6836. void __fastcall TCustomScpExplorerForm::WMAppCommand(TMessage & Message)
  6837. {
  6838. int Command = GET_APPCOMMAND_LPARAM(Message.LParam);
  6839. TShiftState Shift = KeyDataToShiftState(GET_KEYSTATE_LPARAM(Message.LParam));
  6840. if ((Shift * (TShiftState() << ssShift << ssAlt << ssCtrl)).Empty())
  6841. {
  6842. if (Command == APPCOMMAND_BROWSER_FAVORITES)
  6843. {
  6844. if (!NonVisualDataModule->Busy)
  6845. {
  6846. OpenDirectory(GetSide(osCurrent));
  6847. }
  6848. Message.Result = 1;
  6849. }
  6850. else
  6851. {
  6852. TForm::Dispatch(&Message);
  6853. }
  6854. }
  6855. else
  6856. {
  6857. TForm::Dispatch(&Message);
  6858. }
  6859. }
  6860. //---------------------------------------------------------------------------
  6861. void __fastcall TCustomScpExplorerForm::CMDialogChar(TMessage & AMessage)
  6862. {
  6863. TCMDialogChar & Message = reinterpret_cast<TCMDialogChar &>(AMessage);
  6864. if ((FIgnoreNextDialogChar != 0) &&
  6865. (toupper(Message.CharCode) == toupper(FIgnoreNextDialogChar)))
  6866. {
  6867. Message.Result = 1;
  6868. }
  6869. else
  6870. {
  6871. TForm::Dispatch(&Message);
  6872. }
  6873. FIgnoreNextDialogChar = 0;
  6874. }
  6875. //---------------------------------------------------------------------------
  6876. void __fastcall TCustomScpExplorerForm::WMSysCommand(TMessage & Message)
  6877. {
  6878. // The four low-order bits are used internally by Windows
  6879. unsigned int Cmd = (Message.WParam & 0xFFF0);
  6880. // SC_RESTORE, SC_MAXIMIZE, SC_MINIMIZE - buttons on windows title
  6881. // SC_DEFAULT - double click on windows title (does not work, at least on WinXP)
  6882. // 61730 - restore thru double click - undocumented
  6883. // 61490 - maximize thru double click - undocumented
  6884. if ((Cmd == SC_RESTORE) || (Cmd == SC_MAXIMIZE) ||
  6885. (Cmd == SC_MINIMIZE) || (Cmd == SC_DEFAULT))
  6886. {
  6887. SysResizing(Cmd);
  6888. }
  6889. TForm::Dispatch(&Message);
  6890. }
  6891. //---------------------------------------------------------------------------
  6892. void __fastcall TCustomScpExplorerForm::WMQueryEndSession(TMessage & Message)
  6893. {
  6894. // We were actually never able to make ENDSESSION_CRITICAL happen.
  6895. // Also there no point returning TRUE as we are not able to
  6896. // handle the abrupt termination caused by subsequent WM_ENDSESSION cleanly.
  6897. // Hence the process termination might be safer :)
  6898. if ((Message.LParam != ENDSESSION_CRITICAL) &&
  6899. (((FQueue != NULL) && !FQueue->IsEmpty) || (FProgressForm != NULL)))
  6900. {
  6901. Message.Result = FALSE;
  6902. }
  6903. else
  6904. {
  6905. Message.Result = TRUE;
  6906. }
  6907. // Do not call default handling as that triggers OnCloseQuery,
  6908. // where our implementation will popup confirmation dialogs, what we do not want,
  6909. // as per Vista guidelines:
  6910. // https://docs.microsoft.com/en-us/windows/win32/shutdown/shutting-down
  6911. }
  6912. //---------------------------------------------------------------------------
  6913. void __fastcall TCustomScpExplorerForm::WMEndSession(TWMEndSession & Message)
  6914. {
  6915. if (Message.EndSession && IsApplicationMinimized())
  6916. {
  6917. // WORKAROUND
  6918. // TApplication.WndProc() calls Application.Terminate() before Halt(),
  6919. // when it receives WM_ENDSESSION,
  6920. // but that sometimes (particularly when application is minimized) cause crashes.
  6921. // Crash popups message that blocks log off.
  6922. // Obviously application cannot shutdown cleanly after WM_ENDSESSION,
  6923. // so we call ExitProcess() immediately, not even trying to cleanup.
  6924. // It still causes beep, so there's likely some popup, but it does not block
  6925. // log off.
  6926. ExitProcess(0);
  6927. }
  6928. TForm::Dispatch(&Message);
  6929. }
  6930. #ifdef _DEBUG
  6931. //---------------------------------------------------------------------------
  6932. void __fastcall TCustomScpExplorerForm::WMWindowPosChanged(TWMWindowPosMsg & Message)
  6933. {
  6934. TForm::Dispatch(&Message);
  6935. }
  6936. #endif
  6937. //---------------------------------------------------------------------------
  6938. void __fastcall TCustomScpExplorerForm::SysResizing(unsigned int /*Cmd*/)
  6939. {
  6940. }
  6941. //---------------------------------------------------------------------------
  6942. void __fastcall TCustomScpExplorerForm::DoShow()
  6943. {
  6944. // Called from TCustomScpExplorerForm::CMShowingChanged
  6945. // only now are the controls resized finally, so the size constraints
  6946. // will not conflict with possibly very small window size
  6947. RestoreFormParams();
  6948. // This indicates that the form was rescaled after parameters were loaded and
  6949. // as most probably the parameters were actually saved with the current form scaling
  6950. // (windows was the last time likely closed on the same monitor, where it is starting now),
  6951. // rescaling happened twice already. Reload the parameters, as now they likely won't need any scaling
  6952. // and no rounding problems will occur.
  6953. // See also RestoreForm.
  6954. // (we should be safe to call RestoreParams unconditionally)
  6955. if (PixelsPerInch != Screen->PixelsPerInch)
  6956. {
  6957. RestoreParams();
  6958. }
  6959. FixControlsPlacement();
  6960. if (Position == poDefaultPosOnly)
  6961. {
  6962. CutFormToDesktop(this);
  6963. }
  6964. TForm::DoShow();
  6965. FSessionsDragDropFilesEx->DragDropControl = SessionsPageControl;
  6966. FQueueDragDropFilesEx->DragDropControl = QueueView3;
  6967. // This was previously in NeedSession, where other related code is.
  6968. // But that is called from CMShowingChanged, when the window is already visible and the local-local window state
  6969. // unpleasantly shows briefly.
  6970. if ((ManagedSession == NULL) &&
  6971. !WinConfiguration->ShowLoginWhenNoSession &&
  6972. WinConfiguration->AutoSaveWorkspace && !WinConfiguration->AutoWorkspace.IsEmpty() &&
  6973. // This detects if workspace was saved the last time the main widow was closed
  6974. SameText(WinConfiguration->LastStoredSession, WinConfiguration->AutoWorkspace))
  6975. {
  6976. DoOpenFolderOrWorkspace(WinConfiguration->AutoWorkspace, false, true);
  6977. Configuration->Usage->Inc(L"OpenedWorkspacesAuto");
  6978. }
  6979. // Before main window displays
  6980. if (ManagedSession == NULL)
  6981. {
  6982. StartingWithoutSession();
  6983. }
  6984. UpdatePixelsPerInchMainWindowCounter();
  6985. FShowing = true;
  6986. }
  6987. //---------------------------------------------------------------------------
  6988. void __fastcall TCustomScpExplorerForm::UpdatePixelsPerInchMainWindowCounter()
  6989. {
  6990. Configuration->Usage->Set(L"PixelsPerInchMainWindow", PixelsPerInch);
  6991. }
  6992. //---------------------------------------------------------------------------
  6993. void __fastcall TCustomScpExplorerForm::StartingWithoutSession()
  6994. {
  6995. SessionListChanged();
  6996. InitStatusBar();
  6997. UpdateControls();
  6998. }
  6999. //---------------------------------------------------------------------------
  7000. void __fastcall TCustomScpExplorerForm::PopupTrayBalloon(TTerminal * Terminal,
  7001. const UnicodeString & Str, TQueryType Type, Exception * E, unsigned int Seconds,
  7002. TNotifyEvent OnBalloonClick, TObject * UserData)
  7003. {
  7004. bool Do;
  7005. UnicodeString Message;
  7006. if (E == NULL)
  7007. {
  7008. Message = Str;
  7009. Do = true;
  7010. }
  7011. else
  7012. {
  7013. Do = ExceptionMessage(E, Message);
  7014. }
  7015. if (Do && WinConfiguration->BalloonNotifications)
  7016. {
  7017. UnicodeString Title;
  7018. if ((Terminal == NULL) && (Type == qtInformation) && ExtractMainInstructions(Message, Title))
  7019. {
  7020. Message = TrimLeft(Message);
  7021. }
  7022. else
  7023. {
  7024. Message = UnformatMessage(Message);
  7025. const ResourceString * Captions[] = { &_SMsgDlgConfirm, &_SMsgDlgWarning,
  7026. &_SMsgDlgError, &_SMsgDlgInformation, NULL };
  7027. Title = LoadResourceString(Captions[Type]);
  7028. if (Terminal != NULL)
  7029. {
  7030. TManagedTerminal * Session = DebugNotNull(dynamic_cast<TManagedTerminal *>(Terminal));
  7031. Title = TTerminalManager::Instance()->GetSessionTitle(Session, true) + TitleSeparator + Title;
  7032. }
  7033. }
  7034. if (Seconds == 0)
  7035. {
  7036. Seconds = WinConfiguration->NotificationsTimeout;
  7037. }
  7038. FTrayIcon->PopupBalloon(Title, Message, Type, Seconds * MSecsPerSec, OnBalloonClick, UserData);
  7039. }
  7040. }
  7041. //---------------------------------------------------------------------------
  7042. unsigned int __fastcall TCustomScpExplorerForm::MoreMessageDialog(const UnicodeString Message,
  7043. TStrings * MoreMessages, TQueryType Type, unsigned int Answers,
  7044. UnicodeString HelpKeyword, const TMessageParams * Params,
  7045. TTerminal * Terminal)
  7046. {
  7047. if (((WinConfiguration->ContinueOnError && (FErrorList != NULL)) ||
  7048. (FOnFeedSynchronizeError != NULL)) &&
  7049. (Params != NULL) && (Params->Params & mpAllowContinueOnError) )
  7050. {
  7051. if (FOnFeedSynchronizeError != NULL)
  7052. {
  7053. FOnFeedSynchronizeError(Message, MoreMessages, Type, HelpKeyword);
  7054. }
  7055. else
  7056. {
  7057. DebugAssert(FErrorList != NULL);
  7058. TStringList * MoreMessagesCopy = NULL;
  7059. if (MoreMessages)
  7060. {
  7061. MoreMessagesCopy = new TStringList();
  7062. MoreMessagesCopy->Assign(MoreMessages);
  7063. }
  7064. FErrorList->AddObject(Message, MoreMessagesCopy);
  7065. }
  7066. PopupTrayBalloon(Terminal, Message, Type);
  7067. return ContinueAnswer(Answers);
  7068. }
  7069. else
  7070. {
  7071. bool UseBalloon = FTrayIcon->Visible;
  7072. if (UseBalloon)
  7073. {
  7074. PopupTrayBalloon(Terminal, Message, Type);
  7075. }
  7076. unsigned int Result;
  7077. try
  7078. {
  7079. Result = ::MoreMessageDialog(Message, MoreMessages, Type, Answers, HelpKeyword, Params);
  7080. }
  7081. __finally
  7082. {
  7083. // cancel only balloon we popped up, otherwise we may cancel notification
  7084. // balloon that was there before the message dialog
  7085. // (such as "dd confirmation opt in balloon)
  7086. if (UseBalloon)
  7087. {
  7088. FTrayIcon->CancelBalloon();
  7089. }
  7090. }
  7091. return Result;
  7092. }
  7093. }
  7094. //---------------------------------------------------------------------------
  7095. void __fastcall TCustomScpExplorerForm::ShowExtendedException(
  7096. TTerminal * Terminal, Exception * E)
  7097. {
  7098. if (FTrayIcon->Visible)
  7099. {
  7100. PopupTrayBalloon(Terminal, L"", qtError, E);
  7101. }
  7102. // particularly prevent opening new session from jump list,
  7103. // while exception is shown
  7104. NonVisualDataModule->StartBusy();
  7105. try
  7106. {
  7107. ShowExtendedExceptionEx(Terminal, E);
  7108. }
  7109. __finally
  7110. {
  7111. NonVisualDataModule->EndBusy();
  7112. FTrayIcon->CancelBalloon();
  7113. }
  7114. }
  7115. //---------------------------------------------------------------------------
  7116. void __fastcall TCustomScpExplorerForm::SessionReady()
  7117. {
  7118. InitStatusBar();
  7119. // cannot rely on active page being page for active terminal,
  7120. // as it can happen that active page is the "new session" page
  7121. // (e.g. when reconnecting active terminal, while login dialog
  7122. // invoked from "new session" page is modal)
  7123. int ActiveSessionIndex = TTerminalManager::Instance()->ActiveSessionIndex;
  7124. UpdateSessionTab(SessionsPageControl->Pages[ActiveSessionIndex]);
  7125. }
  7126. //---------------------------------------------------------------------------
  7127. void __fastcall TCustomScpExplorerForm::InactiveTerminalException(
  7128. TTerminal * Terminal, Exception * E)
  7129. {
  7130. Notify(Terminal, L"", qtError, false, NULL, NULL, E);
  7131. if (!Terminal->Active)
  7132. {
  7133. int Index = TTerminalManager::Instance()->IndexOf(Terminal);
  7134. if (DebugAlwaysTrue((Index >= 0) && (Index < SessionsPageControl->PageCount)))
  7135. {
  7136. UpdateSessionTab(SessionsPageControl->Pages[Index]);
  7137. }
  7138. }
  7139. }
  7140. //---------------------------------------------------------------------------
  7141. void __fastcall TCustomScpExplorerForm::Notify(TTerminal * Terminal,
  7142. UnicodeString Message, TQueryType Type,
  7143. bool Important, TNotifyEvent OnClick, TObject * UserData, Exception * E)
  7144. {
  7145. if ((E == NULL) ||
  7146. ExceptionMessage(E, Message))
  7147. {
  7148. unsigned int Seconds = WinConfiguration->NotificationsTimeout;
  7149. if (Important)
  7150. {
  7151. Seconds *= 5;
  7152. }
  7153. UnicodeString NoteMessage(UnformatMessage(Message));
  7154. if (Terminal != NULL)
  7155. {
  7156. TManagedTerminal * Session = DebugNotNull(dynamic_cast<TManagedTerminal *>(Terminal));
  7157. NoteMessage = FORMAT(L"%s: %s",
  7158. (TTerminalManager::Instance()->GetSessionTitle(Session, true), NoteMessage));
  7159. }
  7160. if (WinConfiguration->BalloonNotifications)
  7161. {
  7162. AddNote(NoteMessage);
  7163. PopupTrayBalloon(Terminal, Message, Type, NULL, Seconds, OnClick, UserData);
  7164. }
  7165. else
  7166. {
  7167. FlashOnBackground();
  7168. PostNote(NoteMessage, Seconds, OnClick, UserData);
  7169. }
  7170. }
  7171. }
  7172. //---------------------------------------------------------------------------
  7173. void __fastcall TCustomScpExplorerForm::QueueEmptyNoteClicked(TObject * Sender)
  7174. {
  7175. RestoreApp();
  7176. TTerminalNoteData * TerminalNoteData = dynamic_cast<TTerminalNoteData *>(Sender);
  7177. if (DebugAlwaysTrue(TerminalNoteData != NULL) &&
  7178. !NonVisualDataModule->Busy)
  7179. {
  7180. TManagedTerminal * Terminal = TerminalNoteData->Terminal;
  7181. TTerminalManager::Instance()->ActiveSession = Terminal;
  7182. if (!ComponentVisible[fcQueueView])
  7183. {
  7184. ToggleQueueVisibility();
  7185. GoToQueue();
  7186. }
  7187. }
  7188. }
  7189. //---------------------------------------------------------------------------
  7190. void __fastcall TCustomScpExplorerForm::QueueEvent(TManagedTerminal * ATerminal,
  7191. TTerminalQueue * /*Queue*/, TQueueEvent Event)
  7192. {
  7193. UnicodeString Message;
  7194. TNotifyEvent OnClick = NULL;
  7195. TObject * UserData = NULL;
  7196. bool QueueInvisible = !ComponentVisible[fcQueueView] || IsApplicationMinimized();
  7197. switch (Event)
  7198. {
  7199. case qeEmptyButMonitored:
  7200. if ((ATerminal != Terminal) || QueueInvisible)
  7201. {
  7202. Message = LoadStr(BALLOON_QUEUE_EMPTY);
  7203. OnClick = QueueEmptyNoteClicked;
  7204. TTerminalNoteData * TerminalNoteData = new TTerminalNoteData();
  7205. TerminalNoteData->Terminal = ATerminal;
  7206. UserData = TerminalNoteData;
  7207. }
  7208. break;
  7209. case qeEmpty:
  7210. OperationComplete(ATerminal->QueueOperationStart);
  7211. break;
  7212. case qePendingUserAction:
  7213. if ((ATerminal != Terminal) ||
  7214. (QueueInvisible && !IsQueueAutoPopup()))
  7215. {
  7216. Message = LoadStr(BALLOON_QUEUE_USER_ACTION);
  7217. }
  7218. break;
  7219. default:
  7220. DebugFail();
  7221. }
  7222. if (!Message.IsEmpty())
  7223. {
  7224. Notify(ATerminal, Message, qtInformation, false, OnClick, UserData);
  7225. }
  7226. }
  7227. //---------------------------------------------------------------------------
  7228. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDCreateDragFileList(
  7229. TObject * /*Sender*/, TFileList * FileList, bool & Created)
  7230. {
  7231. if (FDDExtMapFile != NULL)
  7232. {
  7233. CloseHandle(FDDExtMapFile);
  7234. FDDExtMapFile = NULL;
  7235. }
  7236. if (WinConfiguration->DDFakeFile)
  7237. {
  7238. DDFakeFileInitDrag(FileList, Created);
  7239. }
  7240. }
  7241. //---------------------------------------------------------------------------
  7242. void __fastcall TCustomScpExplorerForm::DDFakeCreated(TObject * /*Sender*/, const UnicodeString FileName)
  7243. {
  7244. if (DebugAlwaysTrue(!FDragFakeDirectory.IsEmpty()) &&
  7245. SameText(ExtractFileName(FileName), ExtractFileName(FDragFakeDirectory)))
  7246. {
  7247. FFakeFileDropTarget = FileName;
  7248. }
  7249. }
  7250. //---------------------------------------------------------------------------
  7251. UnicodeString __fastcall TCustomScpExplorerForm::CreateFakeTransferDirectory()
  7252. {
  7253. UnicodeString Result = ExcludeTrailingBackslash(WinConfiguration->TemporaryDir());
  7254. if (!ForceDirectories(ApiPath(Result)))
  7255. {
  7256. throw Exception(FMTLOAD(CREATE_TEMP_DIR_ERROR, (Result)));
  7257. }
  7258. FileSetAttr(ApiPath(Result), faHidden);
  7259. return Result;
  7260. }
  7261. //---------------------------------------------------------------------------
  7262. void __fastcall TCustomScpExplorerForm::DDFakeFileInitDrag(TFileList * FileList,
  7263. bool & Created)
  7264. {
  7265. FFakeFileDropTarget = UnicodeString();
  7266. FDragFakeDirectory = CreateFakeTransferDirectory();
  7267. FileList->AddItem(NULL, FDragFakeDirectory);
  7268. Created = true;
  7269. if (!WinConfiguration->IsDDExtRunning() || WinConfiguration->IsDDExtBroken())
  7270. {
  7271. FDragFakeMonitors = StartCreationDirectoryMonitorsOnEachDrive(FILE_NOTIFY_CHANGE_DIR_NAME, DDFakeCreated);
  7272. }
  7273. FDDExtMapFile = CreateFileMappingA((HANDLE)0xFFFFFFFF, NULL, PAGE_READWRITE,
  7274. 0, sizeof(TDragExtCommStruct), AnsiString(DRAG_EXT_MAPPING).c_str());
  7275. {
  7276. TMutexGuard Guard(FDDExtMutex, DRAGEXT_MUTEX_RELEASE_TIMEOUT);
  7277. TDragExtCommStruct* CommStruct;
  7278. CommStruct = static_cast<TDragExtCommStruct*>(MapViewOfFile(FDDExtMapFile,
  7279. FILE_MAP_ALL_ACCESS, 0, 0, 0));
  7280. DebugAssert(CommStruct != NULL);
  7281. CommStruct->Version = TDragExtCommStruct::CurrentVersion;
  7282. CommStruct->Dragging = true;
  7283. wcsncpy(CommStruct->DropDest, FDragFakeDirectory.c_str(),
  7284. LENOF(CommStruct->DropDest));
  7285. NULL_TERMINATE(CommStruct->DropDest);
  7286. UnmapViewOfFile(CommStruct);
  7287. }
  7288. }
  7289. //---------------------------------------------------------------------------
  7290. bool __fastcall TCustomScpExplorerForm::RemoteFileControlFileOperation(
  7291. TObject * Sender, TFileOperation Operation, bool NoConfirmation, void * Param)
  7292. {
  7293. bool Result;
  7294. DebugAssert(!IsLocalBrowserMode());
  7295. if (Sender == RemoteDirView)
  7296. {
  7297. Result = ExecuteFileOperation(Operation, osRemote, true, NoConfirmation, Param);
  7298. }
  7299. else
  7300. {
  7301. DebugAssert(Sender == RemoteDriveView);
  7302. TStrings * FileList = RemoteDriveView->DragFileList();
  7303. try
  7304. {
  7305. Result = ExecuteFileOperation(Operation, osRemote, FileList, NoConfirmation, Param);
  7306. }
  7307. __finally
  7308. {
  7309. delete FileList;
  7310. }
  7311. }
  7312. if (FDDTargetControl == RemoteDriveView)
  7313. {
  7314. RemoteDriveView->UpdateDropTarget();
  7315. }
  7316. // foRemoteMove happens when file/dir is dragged within the remote tree view
  7317. // or from tree view to dir view.
  7318. // foMove happens when file/dir is dragged from remote tree view outside of
  7319. // application via dragex
  7320. if (((Operation == foRemoteMove) || (Operation == foMove)) &&
  7321. (DropSourceControl == RemoteDriveView))
  7322. {
  7323. RemoteDriveView->UpdateDropSource();
  7324. }
  7325. return Result;
  7326. }
  7327. //---------------------------------------------------------------------------
  7328. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDEnd(TObject * Sender)
  7329. {
  7330. DebugAssert(!IsLocalBrowserMode());
  7331. // This also handles drops of remote files to queue.
  7332. // Drops of local files (uploads) are handled in QueueDDProcessDropped.
  7333. SAFE_DESTROY(FDDFileList);
  7334. if (!FFakeFileDropTarget.IsEmpty())
  7335. {
  7336. RemoveDir(ApiPath(FFakeFileDropTarget));
  7337. }
  7338. if ((FDDExtMapFile != NULL) || (FDDTargetControl == QueueView3))
  7339. {
  7340. try
  7341. {
  7342. TDragResult DDResult = (Sender == RemoteDirView) ?
  7343. RemoteDirView->LastDDResult : RemoteDriveView->LastDDResult;
  7344. // Focus is moved to the target application,
  7345. // but as we are going to present the UI, we need to steal the focus back.
  7346. // This most likely won't work though (windows does not allow application
  7347. // to steal focus most of the time)
  7348. Application->BringToFront();
  7349. // On older version of Windows we never got drMove here, see also comment below.
  7350. // On Windows 10, we get the "move".
  7351. if ((DDResult == drCopy) || (DDResult == drMove) || (DDResult == drInvalid))
  7352. {
  7353. UnicodeString TargetDirectory;
  7354. TFileOperation Operation;
  7355. // drInvalid may mean drMove, see comment below
  7356. switch (DDResult)
  7357. {
  7358. case drCopy:
  7359. Operation = foCopy;
  7360. break;
  7361. case drMove:
  7362. Operation = foMove;
  7363. break;
  7364. default:
  7365. DebugFail();
  7366. case drInvalid:
  7367. // prefer "copy" for safety
  7368. Operation = FLAGSET(FLastDropEffect, DROPEFFECT_MOVE) ? foMove : foCopy;
  7369. break;
  7370. }
  7371. TTransferOperationParam Param;
  7372. UnicodeString CounterName;
  7373. bool ForceQueue;
  7374. if (!DDGetTarget(Param.TargetDirectory, ForceQueue, CounterName))
  7375. {
  7376. // we get drInvalid both if d&d was intercepted by ddext,
  7377. // and when users drops on no-drop location.
  7378. // we tell the difference by existence of response from ddext,
  7379. // so we ignore absence of response in this case
  7380. if (DDResult != drInvalid)
  7381. {
  7382. // here we know that the extension is installed,
  7383. // as it is checked as soon as drag&drop starts from
  7384. // RemoteFileControlDDCreateDragFileList
  7385. Configuration->Usage->Inc(L"DownloadsDragDropExternalExtTargetUnknown");
  7386. if (!WinConfiguration->DDExtInstalled || WinConfiguration->IsDDExtBroken())
  7387. {
  7388. throw ExtException(NULL, LoadStr(DD_TARGET_UNKNOWN), HELP_DD_TARGET_UNKNOWN);
  7389. }
  7390. else
  7391. {
  7392. throw ExtException(NULL, LoadStr(DRAGEXT_TARGET_UNKNOWN2), HELP_DRAGEXT_TARGET_UNKNOWN);
  7393. }
  7394. }
  7395. }
  7396. else
  7397. {
  7398. // download using fake file
  7399. Param.Temp = false;
  7400. Param.DragDrop = true;
  7401. if (ForceQueue)
  7402. {
  7403. Param.Queue = asOn;
  7404. }
  7405. if (Sender == RemoteDirView)
  7406. {
  7407. Param.Options = FLAGMASK(SelectedAllFilesInDirView(RemoteDirView), coAllFiles);
  7408. }
  7409. if (RemoteFileControlFileOperation(Sender, Operation,
  7410. (WinConfiguration->DDTransferConfirmation == asOff), &Param))
  7411. {
  7412. Configuration->Usage->Inc(CounterName);
  7413. }
  7414. }
  7415. }
  7416. }
  7417. __finally
  7418. {
  7419. CloseHandle(FDDExtMapFile);
  7420. FDDExtMapFile = NULL;
  7421. delete FDragFakeMonitors;
  7422. FDragFakeMonitors = NULL;
  7423. RemoveDir(ApiPath(FDragFakeDirectory));
  7424. FDragFakeDirectory = L"";
  7425. FFakeFileDropTarget = UnicodeString();
  7426. }
  7427. }
  7428. if (FDragDropSshTerminate.get() != NULL)
  7429. {
  7430. FDragDropSshTerminate->Rethrow();
  7431. }
  7432. }
  7433. //---------------------------------------------------------------------------
  7434. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDGiveFeedback(
  7435. TObject * /*Sender*/, int dwEffect, HRESULT & /*Result*/)
  7436. {
  7437. // Remember drop effect so we know (when user drops files), if we copy or move
  7438. FLastDropEffect = dwEffect;
  7439. }
  7440. //---------------------------------------------------------------------------
  7441. bool __fastcall TCustomScpExplorerForm::DDGetTarget(
  7442. UnicodeString & Directory, bool & ForceQueue, UnicodeString & CounterName)
  7443. {
  7444. bool Result;
  7445. if (FDDTargetControl == QueueView3)
  7446. {
  7447. Directory = DefaultDownloadTargetDirectory();
  7448. Result = true;
  7449. CounterName = L"DownloadsDragDropQueue";
  7450. ForceQueue = true;
  7451. }
  7452. else if (!FFakeFileDropTarget.IsEmpty())
  7453. {
  7454. Directory = ExcludeTrailingBackslash(ExtractFilePath(FFakeFileDropTarget));
  7455. Result = true;
  7456. ForceQueue = false;
  7457. CounterName = L"DownloadsDragDropFakeFile";
  7458. }
  7459. else
  7460. {
  7461. ForceQueue = false;
  7462. Enabled = false;
  7463. try
  7464. {
  7465. int Timer = 0;
  7466. Result = false;
  7467. while (!Result && (Timer < WinConfiguration->DDExtTimeout))
  7468. {
  7469. {
  7470. TMutexGuard Guard(FDDExtMutex, DRAGEXT_MUTEX_RELEASE_TIMEOUT);
  7471. TDragExtCommStruct* CommStruct;
  7472. CommStruct = static_cast<TDragExtCommStruct*>(MapViewOfFile(FDDExtMapFile,
  7473. FILE_MAP_ALL_ACCESS, 0, 0, 0));
  7474. DebugAssert(CommStruct != NULL);
  7475. Result = !CommStruct->Dragging;
  7476. if (Result)
  7477. {
  7478. Directory = ExtractFilePath(CommStruct->DropDest);
  7479. CounterName = L"DownloadsDragDropExternalExt";
  7480. }
  7481. UnmapViewOfFile(CommStruct);
  7482. }
  7483. if (!Result)
  7484. {
  7485. Sleep(50);
  7486. Timer += 50;
  7487. Application->ProcessMessages();
  7488. }
  7489. }
  7490. }
  7491. __finally
  7492. {
  7493. Enabled = true;
  7494. }
  7495. }
  7496. return Result;
  7497. }
  7498. //---------------------------------------------------------------------------
  7499. void __fastcall TCustomScpExplorerForm::AddDelayedDirectoryDeletion(
  7500. const UnicodeString TempDir, int SecDelay)
  7501. {
  7502. TDateTime Alarm = IncSecond(Now(), SecDelay);
  7503. FDelayedDeletionList->AddObject(TempDir, reinterpret_cast<TObject*>(Alarm.FileDate()));
  7504. if (FDelayedDeletionTimer == NULL)
  7505. {
  7506. DebugAssert(HandleAllocated());
  7507. FDelayedDeletionTimer = new TTimer(this);
  7508. FDelayedDeletionTimer->Interval = 10000;
  7509. FDelayedDeletionTimer->OnTimer = DoDelayedDeletion;
  7510. }
  7511. else
  7512. {
  7513. FDelayedDeletionTimer->Enabled = true;
  7514. }
  7515. }
  7516. //---------------------------------------------------------------------------
  7517. void __fastcall TCustomScpExplorerForm::DoDelayedDeletion(TObject * Sender)
  7518. {
  7519. DebugAssert(FDelayedDeletionList != NULL);
  7520. TDateTime N = Now();
  7521. TDateTime Alert;
  7522. UnicodeString Directory;
  7523. for (int Index = FDelayedDeletionList->Count-1; Index >= 0; Index--)
  7524. {
  7525. Alert = FileDateToDateTime(reinterpret_cast<int>(FDelayedDeletionList->Objects[Index]));
  7526. if ((N >= Alert) || (Sender == NULL))
  7527. {
  7528. Directory = FDelayedDeletionList->Strings[Index];
  7529. if (DeleteDirectory(ExcludeTrailingBackslash(Directory)))
  7530. {
  7531. FDelayedDeletionList->Delete(Index);
  7532. }
  7533. }
  7534. }
  7535. if (FDelayedDeletionList->Count == 0)
  7536. {
  7537. FDelayedDeletionTimer->Enabled = false;
  7538. }
  7539. }
  7540. //---------------------------------------------------------------------------
  7541. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDTargetDrop()
  7542. {
  7543. DebugAssert(!IsLocalBrowserMode());
  7544. if (IsFileControl(FDDTargetControl, osRemote) ||
  7545. (FDDTargetControl == SessionsPageControl))
  7546. {
  7547. TManagedTerminal * TargetTerminal = NULL;
  7548. TFileOperation Operation = foNone;
  7549. if (FDDTargetControl == SessionsPageControl)
  7550. {
  7551. TPoint Point = SessionsPageControl->ScreenToClient(Mouse->CursorPos);
  7552. int Index = SessionsPageControl->IndexOfTabAt(Point.X, Point.Y);
  7553. // do not allow dropping on local-local, disconnected and the "+" tab (see also SessionsDDProcessDropped)
  7554. TargetTerminal = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  7555. if (IsActiveTerminal(TargetTerminal) && DebugAlwaysTrue(!TargetTerminal->LocalBrowser))
  7556. {
  7557. if ((FLastDropEffect == DROPEFFECT_MOVE) &&
  7558. (TargetTerminal == TTerminalManager::Instance()->ActiveTerminal))
  7559. {
  7560. Operation = foRemoteMove;
  7561. }
  7562. else
  7563. {
  7564. Operation = foRemoteCopy;
  7565. }
  7566. }
  7567. }
  7568. else
  7569. {
  7570. if (FLastDropEffect == DROPEFFECT_MOVE)
  7571. {
  7572. Operation = foRemoteMove;
  7573. }
  7574. else if (FLastDropEffect == DROPEFFECT_COPY)
  7575. {
  7576. Operation = foRemoteCopy;
  7577. }
  7578. }
  7579. if (Operation != foNone)
  7580. {
  7581. RemoteFileControlFileOperation(DropSourceControl,
  7582. Operation, (WinConfiguration->DDTransferConfirmation == asOff), TargetTerminal);
  7583. }
  7584. // abort drag&drop
  7585. Abort();
  7586. }
  7587. else if ((FDDExtMapFile == NULL) && (FLastDropEffect != DROPEFFECT_NONE) &&
  7588. // Drops of remote files to queue are handled in RemoteFileControlDDEnd
  7589. (FDDTargetControl != QueueView3))
  7590. {
  7591. DebugAssert(!FDragTempDir.IsEmpty());
  7592. TTransferType Type;
  7593. UnicodeString TempDir = FDragTempDir;
  7594. // We clear FDragTempDir before calling
  7595. // just in case it fail (raises exception)
  7596. FDragTempDir = L"";
  7597. Type = (FLastDropEffect & DROPEFFECT_MOVE ? ttMove : Type = ttCopy);
  7598. TGUICopyParamType CopyParams = GUIConfiguration->CurrentCopyParam;
  7599. // empty directory parameter means temp directory -> don't display it!
  7600. UnicodeString TargetDir = L"";
  7601. int Options = 0;
  7602. if (!CopyParamDialog(tdToLocal, Type, true, FDDFileList,
  7603. TargetDir, CopyParams, (WinConfiguration->DDTransferConfirmation != asOff), true, Options))
  7604. {
  7605. Abort();
  7606. }
  7607. // TargetDir is set when dropped on local file control
  7608. // (this was workaround for legacy dirview event handling, now it should be
  7609. // made prettier)
  7610. if (TargetDir.IsEmpty())
  7611. {
  7612. TargetDir = TempDir;
  7613. if (ForceDirectories(ApiPath(TargetDir)))
  7614. {
  7615. DebugAssert(Terminal && !TargetDir.IsEmpty());
  7616. FPendingTempSpaceWarn = true;
  7617. try
  7618. {
  7619. FDragDropOperation = true;
  7620. int Params = cpTemporary |
  7621. (Type == ttMove ? cpDelete : 0);
  7622. DDDownload(FDDFileList, TargetDir, &CopyParams, Params);
  7623. Configuration->Usage->Inc(L"DownloadsDragDropExternalTemp");
  7624. }
  7625. __finally
  7626. {
  7627. FDragDropOperation = false;
  7628. FPendingTempSpaceWarn = false;
  7629. AddDelayedDirectoryDeletion(TargetDir, WinConfiguration->DDDeleteDelay);
  7630. }
  7631. }
  7632. else
  7633. {
  7634. throw Exception(FMTLOAD(CREATE_TEMP_DIR_ERROR, (TargetDir)));
  7635. }
  7636. }
  7637. }
  7638. }
  7639. //---------------------------------------------------------------------------
  7640. void __fastcall TCustomScpExplorerForm::DDDownload(
  7641. TStrings * FilesToCopy, const UnicodeString & TargetDir, TCopyParamType * CopyParam, int Params)
  7642. {
  7643. DebugAssert(!IsLocalBrowserMode());
  7644. TAutoBatch AutoBatch(this);
  7645. CopyParam->IncludeFileMask.SetRoots(TargetDir, FilesToCopy);
  7646. UpdateCopyParamCounters(*CopyParam);
  7647. Terminal->CopyToLocal(FilesToCopy, TargetDir, CopyParam, Params, NULL);
  7648. if (FLAGSET(Params, cpDelete) && (DropSourceControl == RemoteDriveView))
  7649. {
  7650. RemoteDriveView->UpdateDropSource();
  7651. }
  7652. }
  7653. //---------------------------------------------------------------------------
  7654. class TFakeDataObjectFilesEx : public TDataObjectFilesEx
  7655. {
  7656. public:
  7657. __fastcall TFakeDataObjectFilesEx(TFileList * AFileList, bool RenderPIDL,
  7658. bool RenderFilename) : TDataObjectFilesEx(AFileList, RenderPIDL, RenderFilename, true)
  7659. {
  7660. }
  7661. virtual bool __fastcall AllowData(const tagFORMATETC & FormatEtc)
  7662. {
  7663. return (FormatEtc.cfFormat == CF_HDROP) ? false :
  7664. TDataObjectFilesEx::AllowData(FormatEtc);
  7665. }
  7666. };
  7667. //---------------------------------------------------------------------------
  7668. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDCreateDataObject(
  7669. TObject * Sender, TDataObject *& DataObject)
  7670. {
  7671. if (FDDExtMapFile != NULL)
  7672. {
  7673. TFileList * FileList = DragDropFiles(Sender)->FileList;
  7674. if (!FileList->RenderPIDLs() || !FileList->RenderNames())
  7675. {
  7676. Abort();
  7677. }
  7678. if (FileList->Count > 0)
  7679. {
  7680. TDataObjectFilesEx * FilesObject = new TFakeDataObjectFilesEx(FileList, true, true);
  7681. if (!FilesObject->IsValid(true, true))
  7682. {
  7683. FilesObject->_Release();
  7684. }
  7685. else
  7686. {
  7687. DataObject = FilesObject;
  7688. }
  7689. }
  7690. }
  7691. }
  7692. //---------------------------------------------------------------------------
  7693. void __fastcall TCustomScpExplorerForm::GoToCommandLine()
  7694. {
  7695. DebugFail();
  7696. }
  7697. //---------------------------------------------------------------------------
  7698. void __fastcall TCustomScpExplorerForm::GoToTree()
  7699. {
  7700. ComponentVisible[fcRemoteTree] = true;
  7701. DriveView(osRemote)->SetFocus();
  7702. }
  7703. //---------------------------------------------------------------------------
  7704. TStrings * __fastcall TCustomScpExplorerForm::PanelExport(TOperationSide Side,
  7705. TPanelExport Export)
  7706. {
  7707. TCustomDirView * DirView = this->DirView(Side);
  7708. std::unique_ptr<TStrings> ExportData(new TStringList());
  7709. switch (Export)
  7710. {
  7711. case pePath:
  7712. ExportData->Add(DirView->PathName);
  7713. break;
  7714. case peFileList:
  7715. case peFullFileList:
  7716. {
  7717. bool FullPath = (Export == peFullFileList);
  7718. DirView->CreateFileList(false, FullPath, ExportData.get());
  7719. for (int Index = 0; Index < ExportData->Count; Index++)
  7720. {
  7721. if (ExportData->Strings[Index].Pos(L" ") > 0)
  7722. {
  7723. ExportData->Strings[Index] = FORMAT(L"\"%s\"", (ExportData->Strings[Index]));
  7724. }
  7725. }
  7726. }
  7727. break;
  7728. default:
  7729. DebugFail();
  7730. }
  7731. return ExportData.release();
  7732. }
  7733. //---------------------------------------------------------------------------
  7734. void __fastcall TCustomScpExplorerForm::PanelExport(TOperationSide Side,
  7735. TPanelExport Export, TPanelExportDestination Destination)
  7736. {
  7737. std::unique_ptr<TStrings> ExportData(PanelExport(Side, Export));
  7738. PanelExportStore(Side, Export, Destination, ExportData.get());
  7739. }
  7740. //---------------------------------------------------------------------------
  7741. void __fastcall TCustomScpExplorerForm::PanelExportStore(TOperationSide /*Side*/,
  7742. TPanelExport /*Export*/, TPanelExportDestination Destination,
  7743. TStrings * ExportData)
  7744. {
  7745. if (Destination == pedClipboard)
  7746. {
  7747. TInstantOperationVisualizer Visualizer;
  7748. CopyToClipboard(ExportData);
  7749. }
  7750. else
  7751. {
  7752. DebugFail();
  7753. }
  7754. }
  7755. //---------------------------------------------------------------------------
  7756. void __fastcall TCustomScpExplorerForm::Filter(TOperationSide Side)
  7757. {
  7758. TCustomDirView * DirView = this->DirView(Side);
  7759. UnicodeString Mask = DirView->Mask;
  7760. if (DoFilterMaskDialog(DirView, Mask))
  7761. {
  7762. DirView->Mask = TFileMasks::NormalizeMask(Mask);
  7763. Configuration->Usage->Inc(L"Filters");
  7764. }
  7765. }
  7766. //---------------------------------------------------------------------------
  7767. TQueueOperation __fastcall TCustomScpExplorerForm::DefaultQueueOperation()
  7768. {
  7769. return FQueueController->DefaultOperation();
  7770. }
  7771. //---------------------------------------------------------------------------
  7772. bool __fastcall TCustomScpExplorerForm::AllowQueueOperation(
  7773. TQueueOperation Operation, void ** Param)
  7774. {
  7775. switch (Operation)
  7776. {
  7777. case qoPreferences:
  7778. return true;
  7779. case qoGoTo:
  7780. return ComponentVisible[fcQueueView] && QueueView3->Enabled;
  7781. case qoOnceEmpty:
  7782. return IsAnythingQueued();
  7783. default:
  7784. return FQueueController->AllowOperation(Operation, Param);
  7785. }
  7786. }
  7787. //---------------------------------------------------------------------------
  7788. void __fastcall TCustomScpExplorerForm::GoToQueue()
  7789. {
  7790. if (DebugAlwaysTrue(QueueView3->Visible))
  7791. {
  7792. QueueView3->SetFocus();
  7793. }
  7794. }
  7795. //---------------------------------------------------------------------------
  7796. void __fastcall TCustomScpExplorerForm::ExecuteQueueOperation(
  7797. TQueueOperation Operation, void * Param)
  7798. {
  7799. if (Operation == qoGoTo)
  7800. {
  7801. GoToQueue();
  7802. }
  7803. else if (Operation == qoPreferences)
  7804. {
  7805. PreferencesDialog(pmQueue);
  7806. }
  7807. else
  7808. {
  7809. FQueueController->ExecuteOperation(Operation, Param);
  7810. }
  7811. }
  7812. //---------------------------------------------------------------------------
  7813. bool __fastcall TCustomScpExplorerForm::GetQueueEnabled()
  7814. {
  7815. return (Queue != NULL) && Queue->Enabled;
  7816. }
  7817. //---------------------------------------------------------------------------
  7818. void __fastcall TCustomScpExplorerForm::ToggleQueueEnabled()
  7819. {
  7820. DebugAssert(Queue != NULL);
  7821. if (Queue != NULL)
  7822. {
  7823. Queue->Enabled = !Queue->Enabled;
  7824. }
  7825. }
  7826. //---------------------------------------------------------------------------
  7827. void __fastcall TCustomScpExplorerForm::QueueView3ContextPopup(
  7828. TObject * /*Sender*/, TPoint & /*MousePos*/, bool & /*Handled*/)
  7829. {
  7830. FQueueActedItem = QueueView3->ItemFocused;
  7831. }
  7832. //---------------------------------------------------------------------------
  7833. /*virtual*/ int __fastcall TCustomScpExplorerForm::GetStaticComponentsHeight()
  7834. {
  7835. return TopDock->Height + (MessageDock->Visible ? MessageDock->Height : 0) + QueueSplitter->Height;
  7836. }
  7837. //---------------------------------------------------------------------------
  7838. int __fastcall TCustomScpExplorerForm::GetStaticQueuePanelComponentsHeight()
  7839. {
  7840. return
  7841. (QueueFileListSplitter->Visible ? QueueFileListSplitter->Height : 0) +
  7842. (QueueDock->Visible ? QueueDock->Height : 0) +
  7843. (QueueLabel->Visible ? QueueLabel->Height : 0);
  7844. }
  7845. //---------------------------------------------------------------------------
  7846. int __fastcall TCustomScpExplorerForm::GetMinQueueViewHeight()
  7847. {
  7848. return ScaleByTextHeight(this, 48);
  7849. }
  7850. //---------------------------------------------------------------------------
  7851. void __fastcall TCustomScpExplorerForm::QueueSplitterCanResize(
  7852. TObject * /*Sender*/, int & NewSize, bool & Accept)
  7853. {
  7854. // when queue is hidden by double-clicking splitter, stray attempt to
  7855. // resize the panel with strange value arrives, make sure it is ignored
  7856. if (ComponentVisible[fcQueueView])
  7857. {
  7858. int MaxHeightLimit = ClientHeight - GetStaticComponentsHeight() - RemotePanel->Constraints->MinHeight;
  7859. if (NewSize > MaxHeightLimit)
  7860. {
  7861. NewSize = MaxHeightLimit;
  7862. }
  7863. int MinHeightLimit =
  7864. GetStaticQueuePanelComponentsHeight() +
  7865. (QueueFileList->Visible ? QueueFileList->Height : 0) +
  7866. GetMinQueueViewHeight();
  7867. if (NewSize < MinHeightLimit)
  7868. {
  7869. NewSize = MinHeightLimit;
  7870. }
  7871. }
  7872. else
  7873. {
  7874. Accept = false;
  7875. }
  7876. }
  7877. //---------------------------------------------------------------------------
  7878. void __fastcall TCustomScpExplorerForm::QueueFileListSplitterCanResize(TObject *, int & NewSize, bool &)
  7879. {
  7880. int TotalHeight = GetStaticQueuePanelComponentsHeight() + NewSize;
  7881. int QueueViewHeight = QueuePanel->ClientHeight - TotalHeight;
  7882. if (QueueViewHeight < GetMinQueueViewHeight())
  7883. {
  7884. NewSize -= (GetMinQueueViewHeight() - QueueViewHeight);
  7885. }
  7886. }
  7887. //---------------------------------------------------------------------------
  7888. void __fastcall TCustomScpExplorerForm::QueueView3StartDrag(TObject * /*Sender*/,
  7889. TDragObject *& /*DragObject*/)
  7890. {
  7891. FQueueActedItem = QueueView3->ItemFocused;
  7892. }
  7893. //---------------------------------------------------------------------------
  7894. void __fastcall TCustomScpExplorerForm::QueueView3DragOver(TObject * /*Sender*/,
  7895. TObject * Source, int X, int Y, TDragState /*State*/, bool & Accept)
  7896. {
  7897. Accept = true;
  7898. if (Source == QueueView3)
  7899. {
  7900. TListItem * DropTarget = QueueView3->GetItemAt(X, Y);
  7901. Accept = (DropTarget != NULL) && (FQueueActedItem != NULL);
  7902. if (Accept)
  7903. {
  7904. TQueueItemProxy * QueueItem;
  7905. TQueueItemProxy * DestQueueItem;
  7906. QueueItem = static_cast<TQueueItemProxy *>(FQueueActedItem->Data);
  7907. DestQueueItem = static_cast<TQueueItemProxy *>(DropTarget->Data);
  7908. Accept = (QueueItem != DestQueueItem) &&
  7909. (QueueItem->Status == TQueueItem::qsPending) &&
  7910. (DestQueueItem->Status == TQueueItem::qsPending);
  7911. }
  7912. }
  7913. }
  7914. //---------------------------------------------------------------------------
  7915. void __fastcall TCustomScpExplorerForm::QueueView3DragDrop(TObject * /*Sender*/,
  7916. TObject * /*Source*/, int /*X*/, int /*Y*/)
  7917. {
  7918. if ((FQueueActedItem != NULL) && (QueueView3->DropTarget != NULL))
  7919. {
  7920. TQueueItemProxy * QueueItem;
  7921. TQueueItemProxy * DestQueueItem;
  7922. QueueItem = static_cast<TQueueItemProxy *>(FQueueActedItem->Data);
  7923. DestQueueItem = static_cast<TQueueItemProxy *>(QueueView3->DropTarget->Data);
  7924. QueueItem->Move(DestQueueItem);
  7925. }
  7926. }
  7927. //---------------------------------------------------------------------------
  7928. void __fastcall TCustomScpExplorerForm::QueueView3Enter(TObject * /*Sender*/)
  7929. {
  7930. if ((QueueView3->ItemFocused == NULL) &&
  7931. (QueueView3->Items->Count > 0))
  7932. {
  7933. QueueView3->ItemFocused = QueueView3->Items->Item[0];
  7934. }
  7935. QueueLabelUpdateStatus();
  7936. }
  7937. //---------------------------------------------------------------------------
  7938. void __fastcall TCustomScpExplorerForm::QueueView3Exit(TObject * /*Sender*/)
  7939. {
  7940. QueueLabelUpdateStatus();
  7941. }
  7942. //---------------------------------------------------------------------------
  7943. void __fastcall TCustomScpExplorerForm::QueueLabelUpdateStatus()
  7944. {
  7945. QueueLabel->UpdateStatus();
  7946. }
  7947. //---------------------------------------------------------------------------
  7948. void __fastcall TCustomScpExplorerForm::QueueView3SelectItem(
  7949. TObject * /*Sender*/, TListItem * /*Item*/, bool Selected)
  7950. {
  7951. if (Selected)
  7952. {
  7953. NonVisualDataModule->UpdateNonVisibleActions();
  7954. }
  7955. }
  7956. //---------------------------------------------------------------------------
  7957. TDragDropFilesEx * __fastcall TCustomScpExplorerForm::DragDropFiles(TObject * Sender)
  7958. {
  7959. TDragDropFilesEx * Result = NULL;
  7960. if (Sender == SessionsPageControl)
  7961. {
  7962. Result = FSessionsDragDropFilesEx;
  7963. }
  7964. else if (Sender == QueueView3)
  7965. {
  7966. Result = FQueueDragDropFilesEx;
  7967. }
  7968. else
  7969. {
  7970. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  7971. if (DirView != NULL)
  7972. {
  7973. Result = DirView->DragDropFilesEx;
  7974. }
  7975. else
  7976. {
  7977. TCustomDriveView * DriveView = dynamic_cast<TCustomDriveView *>(Sender);
  7978. if (DriveView != NULL)
  7979. {
  7980. Result = DriveView->DragDropFilesEx;
  7981. }
  7982. }
  7983. }
  7984. DebugAssert(Result != NULL);
  7985. return Result;
  7986. }
  7987. //---------------------------------------------------------------------------
  7988. bool __fastcall TCustomScpExplorerForm::SelectedAllFilesInDirView(TCustomDirView * DView)
  7989. {
  7990. return (DView->SelCount == DView->FilesCount);
  7991. }
  7992. //---------------------------------------------------------------------------
  7993. bool __fastcall TCustomScpExplorerForm::DraggingAllFilesFromDirView(TOperationSide Side, TStrings * FileList)
  7994. {
  7995. return HasDirView[Side] && (DropSourceControl == DirView(Side)) && (FileList->Count == DirView(Side)->FilesCount);
  7996. }
  7997. //---------------------------------------------------------------------------
  7998. void __fastcall TCustomScpExplorerForm::RemoteFileControlDragDropFileOperation(
  7999. TObject * Sender, int Effect, UnicodeString TargetPath, bool ForceQueue, bool DragDrop)
  8000. {
  8001. DebugAssert(!IsLocalBrowserMode());
  8002. TFileOperation Operation;
  8003. switch (Effect)
  8004. {
  8005. case DROPEFFECT_MOVE:
  8006. Operation = foMove;
  8007. break;
  8008. case DROPEFFECT_COPY:
  8009. // occures on WinXP (reported by user)
  8010. default:
  8011. Operation = foCopy;
  8012. break;
  8013. };
  8014. TDragDropFilesEx * DragDropFilesEx = DragDropFiles(Sender);
  8015. // see a comment in TUnixDirView::PerformItemDragDropOperation
  8016. if (DragDropFilesEx->FileList->Count > 0)
  8017. {
  8018. TStrings * FileList = new TStringList();
  8019. try
  8020. {
  8021. for (int Index = 0; Index < DragDropFilesEx->FileList->Count; Index++)
  8022. {
  8023. FileList->Add(DragDropFilesEx->FileList->Items[Index]->Name);
  8024. }
  8025. FDragDropOperation = true;
  8026. TTransferOperationParam Param;
  8027. Param.TargetDirectory = TargetPath;
  8028. // upload, no temp dirs
  8029. Param.Temp = false;
  8030. Param.DragDrop = DragDrop;
  8031. Param.Options =
  8032. FLAGMASK(DraggingAllFilesFromDirView(osLocal, FileList), coAllFiles);
  8033. if (ForceQueue)
  8034. {
  8035. Param.Queue = asOn;
  8036. }
  8037. bool NoConfirmation = DragDrop ? (WinConfiguration->DDTransferConfirmation == asOff) : false;
  8038. if (ExecuteFileOperation(Operation, osLocal, FileList, NoConfirmation, &Param))
  8039. {
  8040. if (IsFileControl(DropSourceControl, osLocal))
  8041. {
  8042. Configuration->Usage->Inc(L"UploadsDragDropInternal");
  8043. }
  8044. else
  8045. {
  8046. Configuration->Usage->Inc(L"UploadsDragDropExternal");
  8047. }
  8048. }
  8049. }
  8050. __finally
  8051. {
  8052. FDragDropOperation = false;
  8053. delete FileList;
  8054. }
  8055. }
  8056. }
  8057. //---------------------------------------------------------------------------
  8058. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDFileOperation(
  8059. TObject * Sender, int Effect, UnicodeString /*SourcePath*/,
  8060. UnicodeString TargetPath, bool Paste, bool & /*DoOperation*/)
  8061. {
  8062. RemoteFileControlDragDropFileOperation(Sender, Effect, TargetPath, false, !Paste);
  8063. }
  8064. //---------------------------------------------------------------------------
  8065. void __fastcall TCustomScpExplorerForm::RemoteFileContolDDChooseEffect(
  8066. TObject * Sender, int grfKeyState, int & dwEffect)
  8067. {
  8068. DebugAssert(!IsLocalBrowserMode());
  8069. // if any drop effect is allowed at all (e.g. no drop to self and drop to parent)
  8070. if ((dwEffect != DROPEFFECT_NONE) &&
  8071. IsFileControl(DropSourceControl, osRemote))
  8072. {
  8073. // do not allow drop on remote panel (on free space, still allow drop on directories)
  8074. if ((Sender == RemoteDirView) && (DropSourceControl == RemoteDirView) &&
  8075. (RemoteDirView->DropTarget == NULL))
  8076. {
  8077. dwEffect = DROPEFFECT_NONE;
  8078. }
  8079. else
  8080. {
  8081. if (dwEffect == DROPEFFECT_COPY)
  8082. {
  8083. bool MoveCapable = Terminal->IsCapable[fcRemoteMove];
  8084. // currently we support copying always (at least via temporary directory);
  8085. // remove associated checks once this all proves stable and working
  8086. bool CopyCapable = true;
  8087. // if we do not support neither of operations, there's no discussion
  8088. if (!MoveCapable && !CopyCapable)
  8089. {
  8090. dwEffect = DROPEFFECT_NONE;
  8091. }
  8092. else
  8093. {
  8094. // The default effect inside remote panel is move,
  8095. // unless we do not support it, but support copy
  8096. if (FLAGCLEAR(grfKeyState, MK_CONTROL))
  8097. {
  8098. dwEffect = MoveCapable ? DROPEFFECT_MOVE : DROPEFFECT_COPY;
  8099. }
  8100. else
  8101. {
  8102. // with ctrl-down, we want copy unless it is not supported
  8103. dwEffect = CopyCapable ? DROPEFFECT_COPY : DROPEFFECT_NONE;
  8104. }
  8105. }
  8106. }
  8107. }
  8108. }
  8109. }
  8110. //---------------------------------------------------------------------------
  8111. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDDragFileName(
  8112. TObject * Sender, TRemoteFile * File, UnicodeString & FileName)
  8113. {
  8114. DebugAssert(!IsLocalBrowserMode());
  8115. if (FDDTotalSize >= 0)
  8116. {
  8117. if (File->IsDirectory)
  8118. {
  8119. FDDTotalSize = -1;
  8120. }
  8121. else
  8122. {
  8123. FDDTotalSize += File->Size;
  8124. }
  8125. }
  8126. DebugAssert(!FDragTempDir.IsEmpty());
  8127. // TODO: this is quite ineffective
  8128. // TODO: what if invalid character replacement is disabled?
  8129. FileName = FDragTempDir + GUIConfiguration->CurrentCopyParam.ValidLocalFileName(File->FileName);
  8130. UnicodeString TransferFileName;
  8131. if (Sender == RemoteDriveView)
  8132. {
  8133. TransferFileName = UnixExcludeTrailingBackslash(File->FullFileName);
  8134. }
  8135. else
  8136. {
  8137. TransferFileName = File->FileName;
  8138. }
  8139. FDDFileList->AddObject(TransferFileName, File);
  8140. }
  8141. //---------------------------------------------------------------------------
  8142. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDDragDetect(
  8143. TObject * /*Sender*/, int /*grfKeyState*/, const TPoint & /*DetectStart*/,
  8144. const TPoint & /*Point*/, TDragDetectStatus /*DragStatus*/)
  8145. {
  8146. // sometimes we do not get DDEnd so the list is not released
  8147. SAFE_DESTROY(FDDFileList);
  8148. FDDFileList = new TStringList();
  8149. FDragTempDir = WinConfiguration->TemporaryDir();
  8150. FDDTotalSize = 0;
  8151. FDragDropSshTerminate.reset(NULL);
  8152. }
  8153. //---------------------------------------------------------------------------
  8154. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDQueryContinueDrag(
  8155. TObject * /*Sender*/, BOOL /*FEscapePressed*/, int /*grfKeyState*/,
  8156. HRESULT & Result)
  8157. {
  8158. if (Result == DRAGDROP_S_DROP)
  8159. {
  8160. try
  8161. {
  8162. GlobalDragImageList->HideDragImage();
  8163. try
  8164. {
  8165. RemoteFileControlDDTargetDrop();
  8166. }
  8167. catch(ESshTerminate & E)
  8168. {
  8169. FDragDropSshTerminate.reset(E.Clone());
  8170. }
  8171. }
  8172. catch (Exception &E)
  8173. {
  8174. // If downloading fails we need to cancel drag&drop, otherwise
  8175. // Explorer shows error
  8176. // But by the way exception probably never reach this point as
  8177. // it's catched on way.
  8178. // Fatal exceptions get here (like when opening a secondary shell extension for file duplication fails).
  8179. Result = DRAGDROP_S_CANCEL;
  8180. ShowExtendedException(Terminal, &E);
  8181. }
  8182. }
  8183. }
  8184. //---------------------------------------------------------------------------
  8185. void __fastcall TCustomScpExplorerForm::DirViewMatchMask(
  8186. TObject * /*Sender*/, UnicodeString FileName, bool Directory, __int64 Size,
  8187. TDateTime Modification, UnicodeString Masks, bool & Matches, bool AllowImplicitMatches)
  8188. {
  8189. TFileMasks::TParams MaskParams;
  8190. MaskParams.Size = Size;
  8191. MaskParams.Modification = Modification;
  8192. // this does not re-parse the mask if it is the same as the last time
  8193. FDirViewMatchMask = Masks;
  8194. bool ImplicitMatch;
  8195. Matches =
  8196. // Local and RecurseInclude parameters have no effect as there's no path.
  8197. // This should use potential MatchesFileName overload with ImplicitMatch argument.
  8198. FDirViewMatchMask.Matches(FileName, false, Directory, &MaskParams, true, ImplicitMatch) &&
  8199. (AllowImplicitMatches || !ImplicitMatch);
  8200. }
  8201. //---------------------------------------------------------------------------
  8202. void __fastcall TCustomScpExplorerForm::DirViewGetOverlay(
  8203. TObject * Sender, TListItem * Item, WORD & Indexes)
  8204. {
  8205. TCustomDirView * DirView = reinterpret_cast<TCustomDirView *>(Sender);
  8206. UnicodeString Ext;
  8207. if (dynamic_cast<TUnixDirView *>(DirView) != NULL)
  8208. {
  8209. Ext = UnixExtractFileExt(DirView->ItemFileName(Item));
  8210. }
  8211. else
  8212. {
  8213. Ext = ExtractFileExt(DirView->ItemFileName(Item));
  8214. }
  8215. if (SameText(Ext, Configuration->PartialExt))
  8216. {
  8217. Indexes |= oiPartial;
  8218. }
  8219. }
  8220. //---------------------------------------------------------------------------
  8221. bool __fastcall TCustomScpExplorerForm::CanPasteToDirViewFromClipBoard()
  8222. {
  8223. return
  8224. DirViewEnabled(osCurrent) &&
  8225. DirView(osCurrent)->CanPasteFromClipBoard();
  8226. }
  8227. //---------------------------------------------------------------------------
  8228. bool __fastcall TCustomScpExplorerForm::CanPasteFromClipBoard()
  8229. {
  8230. bool Result = false;
  8231. if (CanPasteToDirViewFromClipBoard())
  8232. {
  8233. Result = true;
  8234. }
  8235. else
  8236. {
  8237. UnicodeString ClipboardText;
  8238. if (NonEmptyTextFromClipboard(ClipboardText) &&
  8239. (ClipboardText.Pos(L"\n") == 0)) // it's already trimmed
  8240. {
  8241. if (StoredSessions->IsUrl(ClipboardText))
  8242. {
  8243. Result = true;
  8244. }
  8245. else
  8246. {
  8247. Result = DirViewEnabled(osCurrent);
  8248. }
  8249. }
  8250. }
  8251. return Result;
  8252. }
  8253. //---------------------------------------------------------------------------
  8254. void __fastcall TCustomScpExplorerForm::PasteFromClipBoard()
  8255. {
  8256. // Pasting to any local panel is handled in TScpCommanderForm::PasteFromClipBoard()
  8257. if (DoesClipboardContainOurFiles())
  8258. {
  8259. if (DebugAlwaysTrue(CanPasteToDirViewFromClipBoard()))
  8260. {
  8261. DebugAssert(!IsLocalBrowserMode());
  8262. TTerminalManager * Manager = TTerminalManager::Instance();
  8263. TTerminal * TargetTerminal = Manager->ActiveTerminal;
  8264. Manager->ActiveSession = FClipboardTerminal;
  8265. ExecuteFileOperation(foRemoteCopy, osRemote, FClipboardFileList.get(), false, TargetTerminal);
  8266. }
  8267. }
  8268. else if (CanPasteToDirViewFromClipBoard())
  8269. {
  8270. DirView(osCurrent)->PasteFromClipBoard();
  8271. }
  8272. else
  8273. {
  8274. UnicodeString ClipboardText;
  8275. if (NonEmptyTextFromClipboard(ClipboardText))
  8276. {
  8277. if (StoredSessions->IsUrl(ClipboardText))
  8278. {
  8279. NewSession(ClipboardText);
  8280. }
  8281. else
  8282. {
  8283. DirView(osCurrent)->Path = ClipboardText;
  8284. }
  8285. }
  8286. }
  8287. }
  8288. //---------------------------------------------------------------------------
  8289. void __fastcall TCustomScpExplorerForm::FileListFromClipboard()
  8290. {
  8291. // TBD
  8292. }
  8293. //---------------------------------------------------------------------------
  8294. void __fastcall TCustomScpExplorerForm::SelectAll(TOperationSide Side, TSelectMode Mode)
  8295. {
  8296. TCustomDirView * ADirView = DirView(Side);
  8297. ADirView->SelectAll(Mode);
  8298. ADirView->SetFocus();
  8299. }
  8300. //---------------------------------------------------------------------------
  8301. void __fastcall TCustomScpExplorerForm::SelectByMask(TOperationSide Side, bool Select)
  8302. {
  8303. TCustomDirView * ADirView = DirView(Side);
  8304. TFileFilter Filter;
  8305. DefaultFileFilter(Filter);
  8306. if (DoSelectMaskDialog(ADirView, Select, Filter))
  8307. {
  8308. Configuration->Usage->Inc(L"MaskSelections");
  8309. ADirView->SelectFiles(Filter, Select);
  8310. ADirView->SetFocus();
  8311. }
  8312. }
  8313. //---------------------------------------------------------------------------
  8314. void __fastcall TCustomScpExplorerForm::RestoreSelectedNames(TOperationSide Side)
  8315. {
  8316. TCustomDirView * ADirView = DirView(Side);
  8317. ADirView->RestoreSelectedNames();
  8318. ADirView->SetFocus();
  8319. }
  8320. //---------------------------------------------------------------------------
  8321. void __fastcall TCustomScpExplorerForm::SelectSameExt(bool Select)
  8322. {
  8323. TCustomDirView * CurrentDirView = DirView(osCurrent);
  8324. if (DebugAlwaysTrue(CurrentDirView->ItemFocused != NULL))
  8325. {
  8326. UnicodeString FileName = CurrentDirView->ItemFileName(CurrentDirView->ItemFocused);
  8327. UnicodeString Ext;
  8328. if (!IsSideLocalBrowser(osCurrent))
  8329. {
  8330. Ext = UnixExtractFileExt(FileName);
  8331. }
  8332. else
  8333. {
  8334. Ext = ExtractFileExt(FileName);
  8335. }
  8336. if (Ext.IsEmpty())
  8337. {
  8338. Ext = L".";
  8339. }
  8340. TFileFilter Filter;
  8341. Filter.Masks = FORMAT(L"*%s", (Ext));
  8342. Filter.Directories = false;
  8343. CurrentDirView->SelectFiles(Filter, Select);
  8344. }
  8345. }
  8346. //---------------------------------------------------------------------------
  8347. UnicodeString __fastcall TCustomScpExplorerForm::FileStatusBarText(
  8348. const TStatusFileInfo & FileInfo, TOperationSide Side)
  8349. {
  8350. UnicodeString Result;
  8351. if (!FIncrementalSearch.IsEmpty() && (Side == GetSide(osCurrent)))
  8352. {
  8353. Result = FormatIncrementalSearchStatus(FIncrementalSearch, FIncrementalSearchHaveNext);
  8354. }
  8355. else if (!IsSideLocalBrowser(Side) && ((Terminal == NULL) || Terminal->Disconnected))
  8356. {
  8357. // noop
  8358. }
  8359. else
  8360. {
  8361. Result =
  8362. FMTLOAD(FILE_INFO_FORMAT,
  8363. (FormatBytes(FileInfo.SelectedSize),
  8364. FormatBytes(FileInfo.FilesSize),
  8365. FormatNumber(FileInfo.SelectedCount),
  8366. FormatNumber(FileInfo.FilesCount)));
  8367. }
  8368. return Result;
  8369. }
  8370. //---------------------------------------------------------------------------
  8371. void __fastcall TCustomScpExplorerForm::FileStatusBarPanelClick(
  8372. TTBXStatusPanel * Panel, TOperationSide Side)
  8373. {
  8374. if (Panel->Index == 1)
  8375. {
  8376. ToggleShowHiddenFiles();
  8377. }
  8378. else if (Panel->Index == 2)
  8379. {
  8380. Filter(Side);
  8381. }
  8382. }
  8383. //---------------------------------------------------------------------------
  8384. void __fastcall TCustomScpExplorerForm::UpdateFileStatusBar(
  8385. TTBXStatusBar * StatusBar, const TStatusFileInfo & FileInfo, TOperationSide Side)
  8386. {
  8387. DebugAssert(!StatusBar->SimplePanel);
  8388. StatusBar->Panels->Items[0]->Caption = FileStatusBarText(FileInfo, Side);
  8389. UpdateFileStatusExtendedPanels(StatusBar, FileInfo);
  8390. }
  8391. //---------------------------------------------------------------------------
  8392. void __fastcall TCustomScpExplorerForm::UpdateFileStatusExtendedPanels(
  8393. TTBXStatusBar * StatusBar, const TStatusFileInfo & FileInfo)
  8394. {
  8395. DebugAssert(StatusBar->Panels->Count >= 3);
  8396. TTBXStatusPanel * HiddenFilesPanel = StatusBar->Panels->Items[1];
  8397. if (FileInfo.HiddenCount > 0)
  8398. {
  8399. HiddenFilesPanel->Caption = FMTLOAD(FILE_INFO_HIDDEN2, (FormatNumber(FileInfo.HiddenCount)));
  8400. HiddenFilesPanel->ViewPriority = 90; // <100 allows hiding panel when it does not fit
  8401. }
  8402. else
  8403. {
  8404. HiddenFilesPanel->ViewPriority = 0;
  8405. // not really necessary, just to cleanup no-longer-valid data
  8406. HiddenFilesPanel->Caption = L"";
  8407. }
  8408. TTBXStatusPanel * FilteredFilesPanel = StatusBar->Panels->Items[2];
  8409. if (FileInfo.FilteredCount > 0)
  8410. {
  8411. FilteredFilesPanel->Caption = FMTLOAD(FILE_INFO_FILTERED2, (FormatNumber(FileInfo.FilteredCount)));
  8412. FilteredFilesPanel->ViewPriority = 90; // <100 allows hiding panel when it does not fit
  8413. }
  8414. else
  8415. {
  8416. FilteredFilesPanel->ViewPriority = 0;
  8417. // not really necessary, just to cleanup no-longer-valid data
  8418. FilteredFilesPanel->Caption = L"";
  8419. }
  8420. }
  8421. //---------------------------------------------------------------------------
  8422. void __fastcall TCustomScpExplorerForm::RemoteStatusBarClick(
  8423. TObject * /*Sender*/)
  8424. {
  8425. if (DirView(osOther)->Enabled)
  8426. {
  8427. DirView(osOther)->SetFocus();
  8428. }
  8429. }
  8430. //---------------------------------------------------------------------------
  8431. void __fastcall TCustomScpExplorerForm::ToggleQueueVisibility()
  8432. {
  8433. TQueueViewConfiguration Config = WinConfiguration->QueueView;
  8434. switch (Config.Show)
  8435. {
  8436. case qvShow:
  8437. if ((FQueueStatus != NULL) && (FQueueStatus->Count > 0))
  8438. {
  8439. Config.Show = qvHide;
  8440. }
  8441. else
  8442. {
  8443. Config.Show = Config.LastHideShow;
  8444. }
  8445. break;
  8446. case qvHideWhenEmpty:
  8447. if (ComponentVisible[fcQueueView])
  8448. {
  8449. Config.Show = qvHide;
  8450. }
  8451. else
  8452. {
  8453. Config.LastHideShow = Config.Show;
  8454. Config.Show = qvShow;
  8455. }
  8456. break;
  8457. case qvHide:
  8458. Config.LastHideShow = Config.Show;
  8459. Config.Show = qvShow;
  8460. break;
  8461. }
  8462. WinConfiguration->QueueView = Config;
  8463. }
  8464. //---------------------------------------------------------------------------
  8465. UnicodeString __fastcall TCustomScpExplorerForm::PathForCaption()
  8466. {
  8467. UnicodeString Result;
  8468. if (Terminal != NULL)
  8469. {
  8470. switch (WinConfiguration->PathInCaption)
  8471. {
  8472. case picShort:
  8473. Result = ExtractShortName(Terminal->CurrentDirectory, true);
  8474. break;
  8475. case picFull:
  8476. Result = Terminal->CurrentDirectory;
  8477. break;
  8478. }
  8479. }
  8480. return Result;
  8481. }
  8482. //---------------------------------------------------------------------------
  8483. TColor __fastcall TCustomScpExplorerForm::PanelColor()
  8484. {
  8485. TColor Result = GetWindowColor(FSessionColor);
  8486. return Result;
  8487. }
  8488. //---------------------------------------------------------------------------
  8489. TColor __fastcall TCustomScpExplorerForm::DisabledPanelColor()
  8490. {
  8491. TColor Result = (WinConfiguration->UseDarkTheme() ? static_cast<TColor>(RGB(0x40, 0x40, 0x40)) : clBtnFace);
  8492. return Result;
  8493. }
  8494. //---------------------------------------------------------------------------
  8495. void __fastcall TCustomScpExplorerForm::UpdateControls()
  8496. {
  8497. if (!FSessionChanging)
  8498. {
  8499. TTerminalManager::Instance()->UpdateAppTitle();
  8500. }
  8501. // WORKAROUND: Disabling list view when it is not showing yet does not set its
  8502. // background to gray on Windows 7 (works on Windows 10).
  8503. // See also EnableControl
  8504. if (Showing)
  8505. {
  8506. if (ManagedSession != NULL)
  8507. {
  8508. // Update path when it changes
  8509. if ((SessionsPageControl->ActivePage != NULL) &&
  8510. (GetSessionTabSession(SessionsPageControl->ActivePage) == ManagedSession))
  8511. {
  8512. UpdateSessionTab(SessionsPageControl->ActivePage);
  8513. }
  8514. }
  8515. NonVisualDataModule->ReconnectSessionAction->Update();
  8516. ReconnectToolbar->Visible = NonVisualDataModule->ReconnectSessionAction->Visible;
  8517. // ReconnectSessionAction is hidden when disabled, so enabling it actualy resizes the toolbar
  8518. CenterReconnectToolbar();
  8519. bool HasTerminal = HasActiveTerminal();
  8520. RemoteDirView->Enabled = HasTerminal;
  8521. RemoteDirView->Color = HasTerminal ? PanelColor() : DisabledPanelColor();
  8522. RemoteDirView->Font->Color = GetWindowTextColor(RemoteDirView->Color);
  8523. RemoteDriveView->Enabled = RemoteDirView->Enabled;
  8524. RemoteDriveView->Color = RemoteDirView->Color;
  8525. RemoteDriveView->Font->Color = RemoteDirView->Font->Color;
  8526. QueueView3->Enabled = HasTerminal && Terminal->IsCapable[fcBackgroundTransfers];
  8527. QueueView3->Color = QueueView3->Enabled ? GetWindowColor() : DisabledPanelColor();
  8528. QueueView3->Font->Color = GetWindowTextColor(QueueView3->Color);
  8529. QueueFileList->Enabled = QueueView3->Enabled;
  8530. QueueFileList->Color = QueueView3->Color;
  8531. QueueFileList->Font->Color = QueueView3->Font->Color;
  8532. QueueLabelUpdateStatus();
  8533. RemoteDirView->DarkMode = WinConfiguration->UseDarkTheme();
  8534. RemoteDriveView->DarkMode = RemoteDirView->DarkMode;
  8535. if (FImmersiveDarkMode != WinConfiguration->UseDarkTheme())
  8536. {
  8537. UpdateDarkMode();
  8538. }
  8539. reinterpret_cast<TTBCustomItem *>(GetComponent(fcRemotePathComboBox))->Enabled = HasTerminal || IsLocalBrowserMode();
  8540. MessageDock->Visible = (MessageDock->ToolbarCount > 0);
  8541. }
  8542. }
  8543. //---------------------------------------------------------------------------
  8544. void __fastcall TCustomScpExplorerForm::DoDirViewLoaded(TCustomDirView * /*Sender*/)
  8545. {
  8546. UpdateControls();
  8547. }
  8548. //---------------------------------------------------------------------------
  8549. void __fastcall TCustomScpExplorerForm::DirViewLoaded(
  8550. TObject * Sender)
  8551. {
  8552. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  8553. DebugAssert(DirView != NULL);
  8554. DoDirViewLoaded(DirView);
  8555. TransferPresetAutoSelect();
  8556. }
  8557. //---------------------------------------------------------------------------
  8558. void __fastcall TCustomScpExplorerForm::StartUpdates()
  8559. {
  8560. TUpdatesConfiguration Updates = WinConfiguration->Updates;
  8561. // first run after installation
  8562. if (double(Updates.LastCheck) == 0)
  8563. {
  8564. AppLog(L"First run, scheduling updates check for the next run");
  8565. // make sure next time there will be an update (if enabled)
  8566. Updates.LastCheck = TDateTime(1);
  8567. WinConfiguration->Updates = Updates;
  8568. }
  8569. else
  8570. {
  8571. TDateTime Period;
  8572. if (IsUWP() && !IsOfficialPackage())
  8573. {
  8574. Period = DefaultUpdatesPeriod;
  8575. AppLog(L"Thirdparty UWP package, using default updates check period");
  8576. }
  8577. else
  8578. {
  8579. Period = Updates.Period;
  8580. }
  8581. AppLogFmt(L"Updates check period: %.2f", (double(Period)));
  8582. if (double(Period) > 0)
  8583. {
  8584. TDateTime Interval = Now() - Updates.LastCheck;
  8585. AppLogFmt(L"Interval since the last updates check: %.2f", (double(Interval)));
  8586. if (Interval >= Period)
  8587. {
  8588. StartUpdateThread(UpdatesChecked);
  8589. }
  8590. }
  8591. }
  8592. }
  8593. //---------------------------------------------------------------------------
  8594. void __fastcall TCustomScpExplorerForm::UpdatesChecked()
  8595. {
  8596. AppLog(L"Updates check done");
  8597. UnicodeString Message;
  8598. bool New;
  8599. TQueryType Type;
  8600. GetUpdatesMessage(Message, New, Type, false);
  8601. if (!Message.IsEmpty())
  8602. {
  8603. AppLogFmt(L"Updates check message: %s", (Message));
  8604. if (New)
  8605. {
  8606. AppLog(L"New version detected");
  8607. Message = FMTLOAD(NEW_VERSION_CLICK, (Message));
  8608. }
  8609. if (!New && (Type != qtWarning))
  8610. {
  8611. AppLog(L"Posting non-critical updates note");
  8612. PostNote(UnformatMessage(Message), 0, UpdatesNoteClicked, NULL);
  8613. }
  8614. else
  8615. {
  8616. AppLog(L"Posting new version or critical message notification");
  8617. Notify(NULL, Message, Type, true, UpdatesNoteClicked);
  8618. }
  8619. if (New)
  8620. {
  8621. Configuration->Usage->Inc(L"UpdateNotifications");
  8622. }
  8623. }
  8624. }
  8625. //---------------------------------------------------------------------------
  8626. void __fastcall TCustomScpExplorerForm::UpdatesNoteClicked(TObject * /*Sender*/)
  8627. {
  8628. RestoreApp();
  8629. bool CanDisplay = !NonVisualDataModule->Busy;
  8630. if (!CanDisplay && (Screen->ActiveForm != NULL))
  8631. {
  8632. CanDisplay = (Screen->ActiveForm->Perform(WM_CAN_DISPLAY_UPDATES, 0, 0) != 0);
  8633. }
  8634. if (CanDisplay)
  8635. {
  8636. Configuration->Usage->Inc(L"UpdateNotificationsClicked");
  8637. CheckForUpdates(true);
  8638. }
  8639. else
  8640. {
  8641. Configuration->Usage->Inc(L"UpdateNotificationsBusy");
  8642. }
  8643. }
  8644. //---------------------------------------------------------------------------
  8645. void __fastcall TCustomScpExplorerForm::GetTransferPresetAutoSelectData(
  8646. TCopyParamRuleData & Data)
  8647. {
  8648. DebugAssert(Terminal != NULL);
  8649. Data.HostName = Terminal->SessionData->HostNameExpanded;
  8650. Data.UserName = Terminal->SessionData->UserNameExpanded;
  8651. Data.RemoteDirectory = RemoteDirView->PathName;
  8652. }
  8653. //---------------------------------------------------------------------------
  8654. void __fastcall TCustomScpExplorerForm::TransferPresetAutoSelect()
  8655. {
  8656. // Terminal can be null when we are changing local directory implicitly,
  8657. // because it has been deleted, while no session is connected
  8658. // (Login dialog is open)
  8659. if (FAllowTransferPresetAutoSelect && (Terminal != NULL))
  8660. {
  8661. DebugAssert(!IsLocalBrowserMode());
  8662. TCopyParamRuleData Data;
  8663. GetTransferPresetAutoSelectData(Data);
  8664. int CopyParamIndex = GUIConfiguration->CopyParamList->Find(Data);
  8665. UnicodeString CopyParamCurrent = GUIConfiguration->CopyParamCurrent;
  8666. if (CopyParamIndex < 0)
  8667. {
  8668. // there is no preset that matches autoselection
  8669. // set preset that we consider "default"
  8670. FCopyParamAutoSelected = L""; // forget last autoselected preset
  8671. GUIConfiguration->CopyParamCurrent = FCopyParamDefault;
  8672. }
  8673. else
  8674. {
  8675. // there is preset matching autoselection
  8676. UnicodeString CopyParamName = GUIConfiguration->CopyParamList->Names[CopyParamIndex];
  8677. if (CopyParamName == FCopyParamAutoSelected)
  8678. {
  8679. // autoselected the same preset as the last time
  8680. // make no change (i.e. preserve custom user preset, if any)
  8681. }
  8682. else
  8683. {
  8684. // autoselected the different preset then the last time (or there
  8685. // was default preset set)
  8686. FCopyParamAutoSelected = CopyParamName; // remember autoselection
  8687. GUIConfiguration->CopyParamCurrent = CopyParamName;
  8688. }
  8689. }
  8690. if (GUIConfiguration->CopyParamCurrent != CopyParamCurrent)
  8691. {
  8692. if (CopyParamIndex >= 0)
  8693. {
  8694. Configuration->Usage->Inc(L"CopyParamAutoSelects");
  8695. }
  8696. TTransferPresetNoteData * Data = new TTransferPresetNoteData;
  8697. try
  8698. {
  8699. int Fmt =
  8700. (CopyParamIndex < 0) ?
  8701. (GUIConfiguration->CopyParamIndex < 0 ? COPY_PARAM_DEFAULT_NORM : COPY_PARAM_DEFAULT_CUSTOM) :
  8702. COPY_PARAM_AUTOSELECTED;
  8703. UnicodeString Message = FMTLOAD(Fmt, (StripHotkey(GUIConfiguration->CopyParamCurrent)));
  8704. Data->Message = MainInstructions(Message);
  8705. int CopyParamAttrs = Terminal->UsableCopyParamAttrs(0).General;
  8706. UnicodeString Info = GUIConfiguration->CurrentCopyParam.GetInfoStr(L"\n",
  8707. CopyParamAttrs);
  8708. if (CopyParamIndex >= 0)
  8709. {
  8710. DebugAssert(GUIConfiguration->CopyParamList->Rules[CopyParamIndex] != NULL);
  8711. Info = FORMAT(L"%s\n \n%s", (Info,
  8712. FMTLOAD(COPY_PARAM_RULE,
  8713. (GUIConfiguration->CopyParamList->Rules[CopyParamIndex]->GetInfoStr(L"\n")))));
  8714. }
  8715. Data->Message += L"\n\n" + Info;
  8716. if (WinConfiguration->CopyParamAutoSelectNotice)
  8717. {
  8718. TransferPresetNoteMessage(Data, true);
  8719. }
  8720. else
  8721. {
  8722. PostNote(Message, 0, TransferPresetNoteClicked, Data);
  8723. Data = NULL; // ownership passed
  8724. }
  8725. }
  8726. __finally
  8727. {
  8728. delete Data;
  8729. }
  8730. }
  8731. }
  8732. }
  8733. //---------------------------------------------------------------------------
  8734. void __fastcall TCustomScpExplorerForm::TransferPresetNoteMessage(
  8735. TTransferPresetNoteData * NoteData, bool AllowNeverAskAgain)
  8736. {
  8737. DebugAssert(NoteData != NULL);
  8738. DebugAssert(!IsLocalBrowserMode());
  8739. TMessageParams Params(AllowNeverAskAgain ? mpNeverAskAgainCheck : 0);
  8740. TQueryButtonAlias Aliases[1];
  8741. Aliases[0].Button = qaIgnore; // "ignore" is after "ok"
  8742. Aliases[0].Alias = LoadStr(CONFIGURE_BUTTON);
  8743. Params.Aliases = Aliases;
  8744. Params.AliasesCount = LENOF(Aliases);
  8745. unsigned int Result =
  8746. MoreMessageDialog(NoteData->Message, NULL, qtInformation,
  8747. qaOK | qaIgnore, HELP_COPY_PARAM_AUTOSELECTED, &Params);
  8748. switch (Result)
  8749. {
  8750. case qaNeverAskAgain:
  8751. DebugAssert(AllowNeverAskAgain);
  8752. WinConfiguration->CopyParamAutoSelectNotice = false;
  8753. break;
  8754. case qaIgnore:
  8755. PreferencesDialog(pmPresets);
  8756. break;
  8757. }
  8758. }
  8759. //---------------------------------------------------------------------------
  8760. void __fastcall TCustomScpExplorerForm::TransferPresetNoteClicked(TObject * Sender)
  8761. {
  8762. // as of now useless, as this is used for notes only, never for balloons, ...
  8763. RestoreApp();
  8764. // .. and we should never be busy here
  8765. if (DebugAlwaysTrue(!NonVisualDataModule->Busy))
  8766. {
  8767. TransferPresetNoteMessage(DebugNotNull(dynamic_cast<TTransferPresetNoteData *>(Sender)), false);
  8768. }
  8769. }
  8770. //---------------------------------------------------------------------------
  8771. void __fastcall TCustomScpExplorerForm::PreferencesDialog(
  8772. TPreferencesMode APreferencesMode)
  8773. {
  8774. std::unique_ptr<TPreferencesDialogData> PreferencesData;
  8775. TCopyParamRuleData Data;
  8776. if (Terminal != NULL)
  8777. {
  8778. PreferencesData.reset(new TPreferencesDialogData());
  8779. GetTransferPresetAutoSelectData(Data);
  8780. PreferencesData->CopyParamRuleData = &Data;
  8781. }
  8782. DoPreferencesDialog(APreferencesMode, PreferencesData.get());
  8783. }
  8784. //---------------------------------------------------------------------------
  8785. void __fastcall TCustomScpExplorerForm::AdHocCustomCommandValidate(
  8786. const TCustomCommandType & Command)
  8787. {
  8788. if (CustomCommandState(Command, FEditingFocusedAdHocCommand, ccltAll) <= 0)
  8789. {
  8790. throw Exception(FMTLOAD(CUSTOM_COMMAND_IMPOSSIBLE2, (Command.Command)));
  8791. }
  8792. }
  8793. //---------------------------------------------------------------------------
  8794. void __fastcall TCustomScpExplorerForm::AdHocCustomCommand(bool OnFocused)
  8795. {
  8796. bool RemoteAllowed = CustomCommandRemoteAllowed();
  8797. TCustomCommandType Command;
  8798. // make sure we use local custom command when remote are not supported
  8799. if (RemoteAllowed || FLAGSET(FLastCustomCommand.Params, ccLocal))
  8800. {
  8801. Command = FLastCustomCommand;
  8802. }
  8803. else
  8804. {
  8805. Command.Params = Command.Params | ccLocal;
  8806. }
  8807. Command.Name = LoadStr(CUSTOM_COMMAND_AD_HOC_NAME);
  8808. FEditingFocusedAdHocCommand = OnFocused;
  8809. bool LocalSide = IsSideLocalBrowser(FCurrentSide);
  8810. int Options =
  8811. FLAGMASK((!RemoteAllowed || LocalSide), ccoDisableRemote) |
  8812. FLAGMASK(LocalSide, ccoDisableRemoteFiles);
  8813. if (DoCustomCommandDialog(Command, WinConfiguration->CustomCommandList,
  8814. ccmAdHoc, Options, AdHocCustomCommandValidate, NULL))
  8815. {
  8816. FLastCustomCommand = Command;
  8817. UpdateCustomCommandsToolbar();
  8818. ExecuteFileOperation(foCustomCommand, osRemote, OnFocused, false, &Command);
  8819. }
  8820. }
  8821. //---------------------------------------------------------------------------
  8822. void __fastcall TCustomScpExplorerForm::LastCustomCommand(bool OnFocused)
  8823. {
  8824. DebugAssert(!FLastCustomCommand.Command.IsEmpty());
  8825. int State = CustomCommandState(FLastCustomCommand, OnFocused, ccltAll);
  8826. DebugAssert(State > 0);
  8827. if (State <= 0)
  8828. {
  8829. throw Exception(FMTLOAD(CUSTOM_COMMAND_IMPOSSIBLE2, (FLastCustomCommand.Command)));
  8830. }
  8831. ExecuteFileOperation(foCustomCommand, osRemote, OnFocused, false, &FLastCustomCommand);
  8832. }
  8833. //---------------------------------------------------------------------------
  8834. bool __fastcall TCustomScpExplorerForm::GetLastCustomCommand(bool OnFocused,
  8835. TCustomCommandType & Command, int & State)
  8836. {
  8837. bool Result = !FLastCustomCommand.Command.IsEmpty();
  8838. if (Result)
  8839. {
  8840. Command = FLastCustomCommand;
  8841. State = CustomCommandState(FLastCustomCommand, OnFocused, ccltAll);
  8842. }
  8843. return Result;
  8844. }
  8845. //---------------------------------------------------------------------------
  8846. void __fastcall TCustomScpExplorerForm::BeforeAction()
  8847. {
  8848. ResetIncrementalSearch();
  8849. }
  8850. //---------------------------------------------------------------------------
  8851. void __fastcall TCustomScpExplorerForm::PostComponentHide(Byte Component)
  8852. {
  8853. DebugAssert(ComponentVisible[Component]);
  8854. PostMessage(Handle, WM_COMPONENT_HIDE, Component, 0);
  8855. }
  8856. //---------------------------------------------------------------------------
  8857. void __fastcall TCustomScpExplorerForm::QueueSplitterDblClick(TObject * /*Sender*/)
  8858. {
  8859. // when queue panel is resized here directly, the status bar is stretched
  8860. // over whole space the panel occupied
  8861. PostComponentHide(fcQueueView);
  8862. }
  8863. //---------------------------------------------------------------------------
  8864. void __fastcall TCustomScpExplorerForm::QueueFileListSplitterDblClick(TObject *)
  8865. {
  8866. ComponentVisible[fcQueueFileList] = false;
  8867. }
  8868. //---------------------------------------------------------------------------
  8869. void __fastcall TCustomScpExplorerForm::ThemeChanged()
  8870. {
  8871. // We hoped this will refresh scrollbar colors, but it does not have any effect here.
  8872. RefreshColorMode();
  8873. ResetSysDarkTheme();
  8874. ConfigurationChanged();
  8875. ConfigureInterface();
  8876. // Should be called for all controls
  8877. RemoteDirView->Perform(WM_THEMECHANGED, 0, 0);
  8878. }
  8879. //---------------------------------------------------------------------------
  8880. void __fastcall TCustomScpExplorerForm::WMSettingChange(TMessage & Message)
  8881. {
  8882. // Do not handle, when shutting down anyway (maybe also when not setup completelly yet?)
  8883. if (!FInvalid &&
  8884. (Message.LParam != 0) &&
  8885. (wcscmp(reinterpret_cast<LPCWCH>(Message.LParam), L"ImmersiveColorSet") == 0))
  8886. {
  8887. ThemeChanged();
  8888. }
  8889. TForm::Dispatch(&Message);
  8890. }
  8891. //---------------------------------------------------------------------------
  8892. void __fastcall TCustomScpExplorerForm::Dispatch(void * Message)
  8893. {
  8894. TMessage * M = static_cast<TMessage*>(Message);
  8895. switch (M->Msg)
  8896. {
  8897. case CM_DIALOGCHAR:
  8898. CMDialogChar(*M);
  8899. break;
  8900. case WM_APPCOMMAND:
  8901. WMAppCommand(*M);
  8902. break;
  8903. case WM_SYSCOMMAND:
  8904. WMSysCommand(*M);
  8905. break;
  8906. case WM_QUERYENDSESSION:
  8907. WMQueryEndSession(*M);
  8908. break;
  8909. case WM_ENDSESSION:
  8910. WMEndSession(*reinterpret_cast<TWMEndSession *>(M));
  8911. break;
  8912. #ifdef _DEBUG
  8913. case WM_WINDOWPOSCHANGED:
  8914. WMWindowPosChanged(*reinterpret_cast<TWMWindowPosMsg *>(M));
  8915. break;
  8916. case CM_CUSTOMSTYLECHANGED:
  8917. TForm::Dispatch(Message);
  8918. break;
  8919. #endif
  8920. case WM_COMPONENT_HIDE:
  8921. {
  8922. Byte Component = static_cast<Byte>(M->WParam);
  8923. // sanity check
  8924. if (ComponentVisible[Component])
  8925. {
  8926. // special treatment
  8927. if (Component == fcQueueView)
  8928. {
  8929. ToggleQueueVisibility();
  8930. DebugAssert(!ComponentVisible[fcQueueView]);
  8931. }
  8932. else
  8933. {
  8934. ComponentVisible[Component] = false;
  8935. }
  8936. }
  8937. }
  8938. break;
  8939. case WM_COPYDATA:
  8940. WMCopyData(*M);
  8941. break;
  8942. case WM_MANAGES_CAPTION:
  8943. // caption managed in TTerminalManager::UpdateAppTitle()
  8944. M->Result = 1;
  8945. break;
  8946. case WM_WANTS_MOUSEWHEEL:
  8947. M->Result = 1;
  8948. break;
  8949. case CM_SHOWINGCHANGED:
  8950. CMShowingChanged(*M);
  8951. break;
  8952. case WM_CLOSE:
  8953. WMClose(*M);
  8954. break;
  8955. case WM_DPICHANGED:
  8956. WMDpiChanged(*M);
  8957. break;
  8958. case CM_DPICHANGED:
  8959. CMDpiChanged(*M);
  8960. break;
  8961. case WM_SETTINGCHANGE:
  8962. WMSettingChange(*M);
  8963. break;
  8964. case CM_DIALOGKEY:
  8965. CMDialogKey(*reinterpret_cast<TWMKeyDown *>(M));
  8966. break;
  8967. default:
  8968. TForm::Dispatch(Message);
  8969. break;
  8970. }
  8971. }
  8972. //---------------------------------------------------------------------------
  8973. void __fastcall TCustomScpExplorerForm::UpdateImages()
  8974. {
  8975. // noop
  8976. }
  8977. //---------------------------------------------------------------------------
  8978. void __fastcall TCustomScpExplorerForm::CMDpiChanged(TMessage & Message)
  8979. {
  8980. TForm::Dispatch(&Message);
  8981. FSessionColors->Clear();
  8982. AddFixedSessionImages();
  8983. RegenerateSessionColorsImageList(FSessionColors, FSessionColorMaskImageIndex);
  8984. UpdateImages();
  8985. }
  8986. //---------------------------------------------------------------------------
  8987. void __fastcall TCustomScpExplorerForm::WMDpiChanged(TMessage & Message)
  8988. {
  8989. TForm::Dispatch(&Message);
  8990. Configuration->Usage->Inc(L"PixelsPerInchChanged");
  8991. UpdatePixelsPerInchMainWindowCounter();
  8992. }
  8993. //---------------------------------------------------------------------------
  8994. void __fastcall TCustomScpExplorerForm::WMClose(TMessage & Message)
  8995. {
  8996. // Cannot close window while we are busy.
  8997. // We cannot test this in FormCloseQuery as that is called also from
  8998. // Close(), which is called by CloseApplicationAction. So we can be busy
  8999. // there already even, when it is legitimate to close the application.
  9000. // Possibly a better place to handle this would be WMSysCommand.
  9001. if (NonVisualDataModule->Busy)
  9002. {
  9003. Message.Result = 1;
  9004. }
  9005. else
  9006. {
  9007. TForm::Dispatch(&Message);
  9008. }
  9009. }
  9010. //---------------------------------------------------------------------------
  9011. void __fastcall TCustomScpExplorerForm::CMShowingChanged(TMessage & Message)
  9012. {
  9013. if (!FShowingChanged)
  9014. {
  9015. AddStartupSequence(L"D");
  9016. FShowingChanged = true;
  9017. }
  9018. // Ignoring implicit local-local browser of Commander, opened from DoShow
  9019. bool NoSession = (ManagedSession == NULL);
  9020. // This calls TCustomScpExplorerForm::DoShow
  9021. TForm::Dispatch(&Message);
  9022. // Now the window is visible (TForm::Dispatch is what usually takes long, like when loading a "local" network directory)
  9023. if (Showing && !FStarted)
  9024. {
  9025. FStarted = true;
  9026. InterfaceStarted();
  9027. }
  9028. if (Showing && NoSession)
  9029. {
  9030. // When we are starting minimized (i.e. from an installer),
  9031. // postpone showing Login dialog until we get restored.
  9032. // Otherwise the Login dialog (and Authentication window) show restored
  9033. // over invisible (minimized) main window.
  9034. if (WindowState == wsMinimized)
  9035. {
  9036. FNeedSession = true;
  9037. }
  9038. else
  9039. {
  9040. // This happens before application ever goes idle, so the toolbars would
  9041. // stay enabled (initial state) until the Login dialog is dismissed.
  9042. UpdateActions();
  9043. NonVisualDataModule->StartBusy();
  9044. try
  9045. {
  9046. // Need to process WM_ACTIVATEAPP before showing the Login dialog,
  9047. // otherwise the dialog does not receive focus.
  9048. // With Commander interface the ProcessMessages is called already
  9049. // by TDriveView, but with Explorer interface, we need to call it explicily
  9050. Application->ProcessMessages();
  9051. // do not reload sessions, they have been loaded just now (optimization)
  9052. NeedSession(true);
  9053. }
  9054. __finally
  9055. {
  9056. NonVisualDataModule->EndBusy();
  9057. }
  9058. }
  9059. }
  9060. }
  9061. //---------------------------------------------------------------------------
  9062. void __fastcall TCustomScpExplorerForm::CenterReconnectToolbar()
  9063. {
  9064. ReconnectToolbar->Left = (ReconnectToolbar->Parent->ClientWidth - ReconnectToolbar->Width) / 2;
  9065. ReconnectToolbar->Top = (ReconnectToolbar->Parent->ClientHeight - ReconnectToolbar->Height) / 2;
  9066. }
  9067. //---------------------------------------------------------------------------
  9068. void __fastcall TCustomScpExplorerForm::FormConstrainedResize(
  9069. TObject * /*Sender*/, int & MinWidth, int & MinHeight, int & MaxWidth,
  9070. int & MaxHeight)
  9071. {
  9072. // workaround for bug in TWinControl.CalcConstraints
  9073. // Check for empty rect (restore from iconic state) is done there only after
  9074. // call to AdjustClientRect, which enlarges the rect (for forms).
  9075. TRect R = GetClientRect();
  9076. // when restoring from iconic state, place no restrictions
  9077. if (IsRectEmpty(R))
  9078. {
  9079. MinWidth = 0;
  9080. MinHeight = 0;
  9081. MaxWidth = 32000;
  9082. MaxHeight = 32000;
  9083. }
  9084. }
  9085. //---------------------------------------------------------------------------
  9086. void __fastcall TCustomScpExplorerForm::GetSpaceAvailable(const UnicodeString Path,
  9087. TSpaceAvailable & ASpaceAvailable, bool & Close)
  9088. {
  9089. // terminal can be already closed (e.g. dropped connection)
  9090. if ((Terminal != NULL) && Terminal->IsCapable[fcCheckingSpaceAvailable])
  9091. {
  9092. Configuration->Usage->Inc(L"SpaceAvailableChecks");
  9093. try
  9094. {
  9095. Terminal->SpaceAvailable(Path, ASpaceAvailable);
  9096. }
  9097. catch(...)
  9098. {
  9099. if (!Terminal->Active)
  9100. {
  9101. Close = true;
  9102. }
  9103. throw;
  9104. }
  9105. }
  9106. }
  9107. //---------------------------------------------------------------------------
  9108. void __fastcall TCustomScpExplorerForm::FileSystemInfo()
  9109. {
  9110. const TSessionInfo & SessionInfo = Terminal->GetSessionInfo();
  9111. const TFileSystemInfo & FileSystemInfo = Terminal->GetFileSystemInfo(true);
  9112. TGetSpaceAvailable OnGetSpaceAvailable = NULL;
  9113. if (Terminal->IsCapable[fcCheckingSpaceAvailable])
  9114. {
  9115. OnGetSpaceAvailable = GetSpaceAvailable;
  9116. }
  9117. DoFileSystemInfoDialog(SessionInfo, FileSystemInfo, Terminal->CurrentDirectory,
  9118. OnGetSpaceAvailable);
  9119. }
  9120. //---------------------------------------------------------------------------
  9121. TSessionData * __fastcall TCustomScpExplorerForm::SessionDataForCode()
  9122. {
  9123. std::unique_ptr<TSessionData> Data(CloneCurrentSessionData());
  9124. Terminal->FillSessionDataForCode(Data.get());
  9125. return Data.release();
  9126. }
  9127. //---------------------------------------------------------------------------
  9128. void __fastcall TCustomScpExplorerForm::GenerateUrl(TStrings * Paths)
  9129. {
  9130. std::unique_ptr<TSessionData> Data(SessionDataForCode());
  9131. DoGenerateUrlDialog(Data.get(), Paths);
  9132. }
  9133. //---------------------------------------------------------------------------
  9134. void __fastcall TCustomScpExplorerForm::SessionGenerateUrl()
  9135. {
  9136. GenerateUrl(NULL);
  9137. }
  9138. //---------------------------------------------------------------------------
  9139. void __fastcall TCustomScpExplorerForm::FileGenerateUrl()
  9140. {
  9141. std::unique_ptr<TStrings> Paths(new TStringList());
  9142. DirView(osCurrent)->CreateFileList(false, true, Paths.get());
  9143. GenerateUrl(Paths.get());
  9144. }
  9145. //---------------------------------------------------------------------------
  9146. void __fastcall TCustomScpExplorerForm::UpdateSessionColor(TColor value)
  9147. {
  9148. FSessionColor = value;
  9149. TColor C = (value != 0 ? value : Vcl::Graphics::clNone);
  9150. TTBXColorItem * ColorItem = dynamic_cast<TTBXColorItem *>(
  9151. static_cast<TObject *>(GetComponent(fcColorMenu)));
  9152. DebugAssert(ColorItem != NULL);
  9153. ColorItem->Color = C;
  9154. NonVisualDataModule->ColorMenuItem->Color = C;
  9155. UpdateControls();
  9156. }
  9157. //---------------------------------------------------------------------------
  9158. void __fastcall TCustomScpExplorerForm::SetSessionColor(TColor value)
  9159. {
  9160. if (value != FSessionColor)
  9161. {
  9162. UpdateSessionColor(value);
  9163. // Is null when called from LastTerminalClosed
  9164. if (Terminal != NULL)
  9165. {
  9166. SessionsPageControl->ActivePage->ImageIndex = AddSessionColor(value);
  9167. }
  9168. }
  9169. }
  9170. //---------------------------------------------------------------------------
  9171. bool __fastcall TCustomScpExplorerForm::CancelNote(bool Force)
  9172. {
  9173. bool Result = FNoteTimer->Enabled;
  9174. if (Result)
  9175. {
  9176. // cannot cancel note too early
  9177. bool NotEarly =
  9178. (Now() - FNoteShown >
  9179. EncodeTimeVerbose(0, 0, (unsigned short)(WinConfiguration->NotificationsStickTime), 0));
  9180. if (Force || NotEarly)
  9181. {
  9182. FNoteTimer->Enabled = false;
  9183. FNote = L"";
  9184. SAFE_DESTROY(FNoteData);
  9185. FOnNoteClick = NULL;
  9186. FNoteHints = FNotes->Text;
  9187. FNoteHints.Delete(FNoteHints.Length() - 1, 2);
  9188. UpdateStatusBar();
  9189. }
  9190. }
  9191. return Result;
  9192. }
  9193. //---------------------------------------------------------------------------
  9194. void __fastcall TCustomScpExplorerForm::NoteTimer(TObject * /*Sender*/)
  9195. {
  9196. DebugAssert(FNoteTimer->Enabled);
  9197. CancelNote(true);
  9198. }
  9199. //---------------------------------------------------------------------------
  9200. void __fastcall TCustomScpExplorerForm::UpdateNoteHints()
  9201. {
  9202. FNoteHints = FNotes->Text.TrimRight();
  9203. }
  9204. //---------------------------------------------------------------------------
  9205. void __fastcall TCustomScpExplorerForm::AddNote(UnicodeString Note, bool UpdateNow)
  9206. {
  9207. int P = Note.Pos(L"\n");
  9208. if (P > 0)
  9209. {
  9210. Note.SetLength(P - 1);
  9211. }
  9212. FNotes->Add(FORMAT(L"[%s] %s",
  9213. (FormatDateTime(Configuration->TimeFormat, Now()), Note)));
  9214. while (FNotes->Count > 10)
  9215. {
  9216. FNotes->Delete(0);
  9217. }
  9218. if (UpdateNow)
  9219. {
  9220. UpdateNoteHints();
  9221. UpdateStatusBar();
  9222. }
  9223. }
  9224. //---------------------------------------------------------------------------
  9225. void __fastcall TCustomScpExplorerForm::PostNote(UnicodeString Note,
  9226. unsigned int Seconds, TNotifyEvent OnNoteClick, TObject * NoteData)
  9227. {
  9228. int P = Note.Pos(L"\n");
  9229. if (P > 0)
  9230. {
  9231. Note.SetLength(P - 1);
  9232. }
  9233. UpdateNoteHints();
  9234. FNote = Note;
  9235. SAFE_DESTROY(FNoteData);
  9236. FNoteData = NoteData;
  9237. FOnNoteClick = OnNoteClick;
  9238. AddNote(Note, false);
  9239. UpdateStatusBar();
  9240. FNoteShown = Now();
  9241. FNoteTimer->Enabled = false;
  9242. if (Seconds == 0)
  9243. {
  9244. Seconds = WinConfiguration->NotificationsTimeout;
  9245. }
  9246. FNoteTimer->Interval = Seconds * MSecsPerSec;
  9247. FNoteTimer->Enabled = true;
  9248. }
  9249. //---------------------------------------------------------------------------
  9250. void __fastcall TCustomScpExplorerForm::ReadDirectoryCancelled()
  9251. {
  9252. PostNote(LoadStr(DIRECTORY_READING_CANCELLED), 0, NULL, NULL);
  9253. }
  9254. //---------------------------------------------------------------------------
  9255. void __fastcall TCustomScpExplorerForm::SynchronizeBrowsingChanged()
  9256. {
  9257. if (NonVisualDataModule->SynchronizeBrowsingAction2->Checked)
  9258. {
  9259. Configuration->Usage->Inc(L"SynchronizeBrowsingEnabled");
  9260. }
  9261. PostNote(FORMAT(LoadStrPart(SYNC_DIR_BROWSE_TOGGLE, 1),
  9262. (LoadStrPart(SYNC_DIR_BROWSE_TOGGLE,
  9263. (NonVisualDataModule->SynchronizeBrowsingAction2->Checked ? 2 : 3)))),
  9264. 0, NULL, NULL);
  9265. }
  9266. //---------------------------------------------------------------------------
  9267. void __fastcall TCustomScpExplorerForm::ToggleShowHiddenFiles()
  9268. {
  9269. bool NewShowHiddenFiles = !WinConfiguration->ShowHiddenFiles;
  9270. UnicodeString Note =
  9271. FORMAT(LoadStrPart(SHOW_HIDDEN_FILES_TOGGLE, 1), (LoadStrPart(SHOW_HIDDEN_FILES_TOGGLE, (NewShowHiddenFiles ? 2 : 3))));
  9272. PostNote(Note, 0, NULL, NULL);
  9273. GetComponent(fcStatusBar)->Repaint(); // toggling ShowHiddenFiles takes time, force repaint beforehand
  9274. WinConfiguration->ShowHiddenFiles = NewShowHiddenFiles;
  9275. }
  9276. //---------------------------------------------------------------------------
  9277. void __fastcall TCustomScpExplorerForm::SetFormatSizeBytes(TFormatBytesStyle Style)
  9278. {
  9279. WinConfiguration->FormatSizeBytes = Style;
  9280. }
  9281. //---------------------------------------------------------------------------
  9282. void __fastcall TCustomScpExplorerForm::ToggleAutoReadDirectoryAfterOp()
  9283. {
  9284. Configuration->AutoReadDirectoryAfterOp = !Configuration->AutoReadDirectoryAfterOp;
  9285. PostNote(FORMAT(LoadStrPart(AUTO_READ_DIRECTORY_TOGGLE, 1),
  9286. (LoadStrPart(AUTO_READ_DIRECTORY_TOGGLE,
  9287. (Configuration->AutoReadDirectoryAfterOp ? 2 : 3)))), 0, NULL, NULL);
  9288. }
  9289. //---------------------------------------------------------------------------
  9290. void __fastcall TCustomScpExplorerForm::StatusBarPanelDblClick(
  9291. TTBXCustomStatusBar * Sender, TTBXStatusPanel * Panel)
  9292. {
  9293. if (Panel->Index == 0)
  9294. {
  9295. if (FOnNoteClick != NULL)
  9296. {
  9297. // prevent the user data from being freed by possible call
  9298. // to CancelNote or PostNote during call to OnNoteClick
  9299. std::unique_ptr<TObject> NoteData(FNoteData);
  9300. TNotifyEvent OnNoteClick = FOnNoteClick;
  9301. FNoteData = NULL;
  9302. // need to cancel the note as we are going to delete its user data
  9303. CancelNote(true);
  9304. OnNoteClick(NoteData.get());
  9305. }
  9306. }
  9307. if (HasActiveTerminal() &&
  9308. (Panel->Index >= Sender->Panels->Count - SessionPanelCount))
  9309. {
  9310. FileSystemInfo();
  9311. }
  9312. }
  9313. //---------------------------------------------------------------------------
  9314. void __fastcall TCustomScpExplorerForm::LockWindow(bool Force)
  9315. {
  9316. // workaround:
  9317. // 1) for unknown reason, disabling window, while minimized,
  9318. // prevents it from restoring, even if it was enabled again meanwhile
  9319. // 2) when disabling the main window, while another has focus
  9320. // minimize is no longer possible
  9321. // ("keep up to date" dialog - though does not seem to be the case with "find file" -
  9322. // so it has probably something to do with the semi-modalness of the "keep up to date" dialog)
  9323. // Shouldn't we use IsApplicationMinimized() here?
  9324. if ((FLockSuspendLevel == 0) && !IsIconic(Application->Handle) && (Force || (Screen->ActiveForm == this)))
  9325. {
  9326. Enabled = false;
  9327. }
  9328. FLockLevel++;
  9329. }
  9330. //---------------------------------------------------------------------------
  9331. void __fastcall TCustomScpExplorerForm::UnlockWindow()
  9332. {
  9333. DebugAssert(FLockLevel > 0);
  9334. FLockLevel--;
  9335. if (FLockLevel == 0)
  9336. {
  9337. DebugAssert(FLockSuspendLevel == 0);
  9338. Enabled = true;
  9339. }
  9340. }
  9341. //---------------------------------------------------------------------------
  9342. void __fastcall TCustomScpExplorerForm::SuspendWindowLock()
  9343. {
  9344. // We need to make sure that window is enabled when the last modal window closes
  9345. // otherwise focus is not restored correctly.
  9346. // So we re-enable the window when modal window opens and
  9347. // disable it back after is closes
  9348. if (FLockLevel > 0)
  9349. {
  9350. // while we have nesting counter, we know that we never be called
  9351. // recursivelly as Application->OnModalBegin is called only
  9352. // for the top-level modal window
  9353. if (DebugAlwaysTrue(FLockSuspendLevel == 0))
  9354. {
  9355. // won't be disabled when conditions in LockWindow() were not satisfied
  9356. FDisabledOnLockSuspend = !Enabled;
  9357. // When minimized to tray (or actually when set to SW_HIDE),
  9358. // setting Enabled makes the window focusable even when there's
  9359. // modal window over it
  9360. if (!FTrayIcon->Visible)
  9361. {
  9362. Enabled = true;
  9363. }
  9364. }
  9365. FLockSuspendLevel++;
  9366. }
  9367. }
  9368. //---------------------------------------------------------------------------
  9369. void __fastcall TCustomScpExplorerForm::ResumeWindowLock()
  9370. {
  9371. if (FLockSuspendLevel > 0)
  9372. {
  9373. DebugAssert(FLockLevel > 0);
  9374. FLockSuspendLevel--;
  9375. // see comment in SuspendWindowLock
  9376. if (DebugAlwaysTrue(FLockSuspendLevel == 0))
  9377. {
  9378. // Note that window can be enabled here, when we were minized to tray when
  9379. // was SuspendWindowLock() called.
  9380. // We should possibly do the same check as in LockWindow(),
  9381. // if it is ever possible that the consitions change between
  9382. // SuspendWindowLock() and ResumeWindowLock()
  9383. Enabled = !FDisabledOnLockSuspend;
  9384. }
  9385. }
  9386. }
  9387. //---------------------------------------------------------------------------
  9388. void __fastcall TCustomScpExplorerForm::UpdateRemotePathComboBox(bool TextOnly)
  9389. {
  9390. TTBXComboBoxItem * RemotePathComboBox =
  9391. reinterpret_cast<TTBXComboBoxItem *>(GetComponent(fcRemotePathComboBox));
  9392. if (!TextOnly)
  9393. {
  9394. TStrings * Items = RemotePathComboBox->Strings;
  9395. Items->BeginUpdate();
  9396. try
  9397. {
  9398. Items->Clear();
  9399. if ((Terminal != NULL) && !RemoteDirView->Path.IsEmpty())
  9400. {
  9401. UnicodeString APath = UnixExcludeTrailingBackslash(RemoteDirView->Path);
  9402. while (!IsUnixRootPath(APath))
  9403. {
  9404. int P = APath.LastDelimiter(L'/');
  9405. DebugAssert(P >= 0);
  9406. Items->Insert(0, APath.SubString(P + 1, APath.Length() - P));
  9407. APath.SetLength(P - 1);
  9408. }
  9409. Items->Insert(0, Customunixdirview_SUnixDefaultRootName);
  9410. }
  9411. }
  9412. __finally
  9413. {
  9414. Items->EndUpdate();
  9415. }
  9416. }
  9417. RemotePathComboBox->ItemIndex = RemotePathComboBox->Strings->Count - 1;
  9418. // Setting ItemIndex to -1 does not reset its text
  9419. if (RemotePathComboBox->Strings->Count == 0)
  9420. {
  9421. RemotePathComboBox->Text = L"";
  9422. }
  9423. }
  9424. //---------------------------------------------------------------------------
  9425. void __fastcall TCustomScpExplorerForm::DoRemotePathComboBoxAdjustImageIndex(
  9426. TTBXComboBoxItem * Sender, const UnicodeString DebugUsedArg(AText), int AIndex, int & ImageIndex)
  9427. {
  9428. if (AIndex < 0)
  9429. {
  9430. AIndex = Sender->ItemIndex;
  9431. }
  9432. ImageIndex = (AIndex < Sender->Strings->Count - 1 ? StdDirIcon : StdDirSelIcon);
  9433. }
  9434. //---------------------------------------------------------------------------
  9435. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxAdjustImageIndex(
  9436. TTBXComboBoxItem * Sender, const UnicodeString AText, int AIndex,
  9437. int & ImageIndex)
  9438. {
  9439. DoRemotePathComboBoxAdjustImageIndex(Sender, AText, AIndex, ImageIndex);
  9440. }
  9441. //---------------------------------------------------------------------------
  9442. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxDrawItem(
  9443. TTBXCustomList * /*Sender*/, TCanvas * /*ACanvas*/, TRect & ARect, int AIndex,
  9444. int /*AHoverIndex*/, bool & /*DrawDefault*/)
  9445. {
  9446. if (!IsLocalBrowserMode())
  9447. {
  9448. ARect.Left += (10 * AIndex);
  9449. }
  9450. }
  9451. //---------------------------------------------------------------------------
  9452. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxMeasureWidth(
  9453. TTBXCustomList * /*Sender*/, TCanvas * /*ACanvas*/, int AIndex, int &AWidth)
  9454. {
  9455. AWidth += (10 * AIndex);
  9456. }
  9457. //---------------------------------------------------------------------------
  9458. void __fastcall TCustomScpExplorerForm::DoRemotePathComboBoxItemClick(TObject * Sender)
  9459. {
  9460. TTBXComboBoxItem * RemotePathComboBox = dynamic_cast<TTBXComboBoxItem*>(Sender);
  9461. UnicodeString APath = UnixExcludeTrailingBackslash(RemoteDirView->Path);
  9462. int Index = RemotePathComboBox->ItemIndex;
  9463. while (Index < RemotePathComboBox->Strings->Count - 1)
  9464. {
  9465. APath = UnixExtractFileDir(APath);
  9466. Index++;
  9467. }
  9468. // VanDyke style paths
  9469. if (APath.IsEmpty())
  9470. {
  9471. DebugAssert(RemotePathComboBox->ItemIndex == 0);
  9472. APath = ROOTDIRECTORY;
  9473. }
  9474. if (RemoteDirView->Path != APath)
  9475. {
  9476. RemoteDirView->Path = APath;
  9477. }
  9478. }
  9479. //---------------------------------------------------------------------------
  9480. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxItemClick(TObject * Sender)
  9481. {
  9482. DoRemotePathComboBoxItemClick(Sender);
  9483. }
  9484. //---------------------------------------------------------------------------
  9485. void __fastcall TCustomScpExplorerForm::DoRemotePathComboBoxCancel(TObject * Sender)
  9486. {
  9487. DebugAssert(Sender == GetComponent(fcRemotePathComboBox));
  9488. DebugUsedParam(Sender);
  9489. UpdateRemotePathComboBox(true);
  9490. }
  9491. //---------------------------------------------------------------------------
  9492. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxCancel(TObject * Sender)
  9493. {
  9494. DoRemotePathComboBoxCancel(Sender);
  9495. }
  9496. //---------------------------------------------------------------------------
  9497. void __fastcall TCustomScpExplorerForm::DirViewEditing(
  9498. TObject * Sender, TListItem * Item, bool & /*AllowEdit*/)
  9499. {
  9500. ResetIncrementalSearch();
  9501. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  9502. DebugAssert(DirView != NULL);
  9503. if (!WinConfiguration->RenameWholeName && !DirView->ItemIsDirectory(Item))
  9504. {
  9505. HWND Edit = ListView_GetEditControl(DirView->Handle);
  9506. // OnEditing is called also from TCustomListView::CanEdit
  9507. if (Edit != NULL)
  9508. {
  9509. EditSelectBaseName(Edit);
  9510. }
  9511. }
  9512. }
  9513. //---------------------------------------------------------------------------
  9514. //---------------------------------------------------------------------------
  9515. TDragDropFilesEx * __fastcall TCustomScpExplorerForm::CreateDragDropFilesEx()
  9516. {
  9517. TDragDropFilesEx * Result = new TDragDropFilesEx(this);
  9518. Result->AutoDetectDnD = false;
  9519. Result->NeedValid = TFileExMustDnDSet() << nvFilename;
  9520. Result->RenderDataOn = rdoEnterAndDropSync;
  9521. Result->TargetEffects = TDropEffectSet() << deCopy << deMove;
  9522. return Result;
  9523. }
  9524. //---------------------------------------------------------------------------
  9525. void __fastcall TCustomScpExplorerForm::UpdateDarkMode()
  9526. {
  9527. if (IsWin10Build(2004))
  9528. {
  9529. FImmersiveDarkMode = WinConfiguration->UseDarkTheme();
  9530. BOOL DarkMode = FImmersiveDarkMode ? TRUE : FALSE;
  9531. const int DWMWA_USE_IMMERSIVE_DARK_MODE = 20;
  9532. DwmSetWindowAttribute(Handle, DWMWA_USE_IMMERSIVE_DARK_MODE, &DarkMode, sizeof(DarkMode));
  9533. }
  9534. }
  9535. //---------------------------------------------------------------------------
  9536. void __fastcall TCustomScpExplorerForm::CreateWnd()
  9537. {
  9538. TForm::CreateWnd();
  9539. // win32-darkmode calls AllowDarkModeForWindow(this, true) here, but it does not seem to have any effect
  9540. UpdateDarkMode();
  9541. if (FSessionsDragDropFilesEx == NULL)
  9542. {
  9543. FSessionsDragDropFilesEx = CreateDragDropFilesEx();
  9544. FSessionsDragDropFilesEx->OnDragOver = SessionsDDDragOver;
  9545. FSessionsDragDropFilesEx->OnProcessDropped = SessionsDDProcessDropped;
  9546. FSessionsDragDropFilesEx->OnDragEnter = SessionsDDDragEnter;
  9547. FSessionsDragDropFilesEx->OnDragLeave = SessionsDDDragLeave;
  9548. }
  9549. if (FQueueDragDropFilesEx == NULL)
  9550. {
  9551. FQueueDragDropFilesEx = CreateDragDropFilesEx();
  9552. // No need to set OnDragOver as we do not have any restrictions
  9553. FQueueDragDropFilesEx->OnProcessDropped = QueueDDProcessDropped;
  9554. FQueueDragDropFilesEx->OnDragEnter = QueueDDDragEnter;
  9555. FQueueDragDropFilesEx->OnDragLeave = QueueDDDragLeave;
  9556. }
  9557. }
  9558. //---------------------------------------------------------------------------
  9559. void __fastcall TCustomScpExplorerForm::DestroyWnd()
  9560. {
  9561. TForm::DestroyWnd();
  9562. FSessionsDragDropFilesEx->DragDropControl = NULL;
  9563. FQueueDragDropFilesEx->DragDropControl = NULL;
  9564. }
  9565. //---------------------------------------------------------------------------
  9566. void __fastcall TCustomScpExplorerForm::FormShow(TObject * /*Sender*/)
  9567. {
  9568. SideEnter(FCurrentSide);
  9569. FEverShown = true;
  9570. }
  9571. //---------------------------------------------------------------------------
  9572. void __fastcall TCustomScpExplorerForm::DoFindFiles(
  9573. TTerminal * ATerminal, UnicodeString Directory, const TFileMasks & FileMask,
  9574. TFileFoundEvent OnFileFound, TFindingFileEvent OnFindingFile)
  9575. {
  9576. if (!NonVisualDataModule->Busy)
  9577. {
  9578. TTerminalManager::Instance()->ActiveSession = DebugNotNull(dynamic_cast<TManagedTerminal *>(ATerminal));
  9579. Configuration->Usage->Inc(L"FileFinds");
  9580. LockWindow(true);
  9581. NonVisualDataModule->StartBusy();
  9582. WinConfiguration->LockedInterface = true;
  9583. try
  9584. {
  9585. Terminal->FilesFind(Directory, FileMask, OnFileFound, OnFindingFile);
  9586. }
  9587. __finally
  9588. {
  9589. WinConfiguration->LockedInterface = false;
  9590. NonVisualDataModule->EndBusy();
  9591. UnlockWindow();
  9592. }
  9593. }
  9594. }
  9595. //---------------------------------------------------------------------------
  9596. void __fastcall TCustomScpExplorerForm::DoFocusRemotePath(TTerminal * ATerminal, const UnicodeString & Path)
  9597. {
  9598. if (!NonVisualDataModule->Busy)
  9599. {
  9600. TTerminalManager::Instance()->ActiveSession = DebugNotNull(dynamic_cast<TManagedTerminal *>(ATerminal));
  9601. SetFocus();
  9602. RemoteDirView->Path = UnixExtractFilePath(Path);
  9603. UnicodeString FileName = UnixExtractFileName(Path);
  9604. TListItem * Item = RemoteDirView->FindFileItem(FileName);
  9605. // If not found, it can because we have loaded directory from cache and the file is new, try again after reload.
  9606. // (tbd: we should not even try this, if we have not loaded the directory from the cache)
  9607. if (Item == NULL)
  9608. {
  9609. ReloadDirectory(osRemote);
  9610. Item = RemoteDirView->FindFileItem(FileName);
  9611. }
  9612. if (Item != NULL)
  9613. {
  9614. RemoteDirView->ItemFocused = Item;
  9615. RemoteDirView->ItemFocused->MakeVisible(false);
  9616. RemoteDirView->SetFocus();
  9617. }
  9618. }
  9619. }
  9620. //---------------------------------------------------------------------------
  9621. bool __fastcall TCustomScpExplorerForm::CanOperateOnFoundFiles(TTerminal * ATerminal)
  9622. {
  9623. bool Result = !NonVisualDataModule->Busy;
  9624. if (Result)
  9625. {
  9626. TTerminalManager::Instance()->ActiveSession = DebugNotNull(dynamic_cast<TManagedTerminal *>(ATerminal));
  9627. }
  9628. return Result;
  9629. }
  9630. //---------------------------------------------------------------------------
  9631. void __fastcall TCustomScpExplorerForm::DoOperationOnFoundFiles(
  9632. TFileOperation Operation, TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  9633. {
  9634. if (CanOperateOnFoundFiles(ATerminal))
  9635. {
  9636. TValueRestorer<TFileOperationFinishedEvent> OnFileOperationFinishedRestorer(FOnFileOperationFinished);
  9637. FOnFileOperationFinished = OnFileOperationFinished;
  9638. ExecuteFileOperation(Operation, osRemote, FileList, false, NULL);
  9639. }
  9640. }
  9641. //---------------------------------------------------------------------------
  9642. void __fastcall TCustomScpExplorerForm::DoDeleteFoundFiles(
  9643. TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  9644. {
  9645. DoOperationOnFoundFiles(foDelete, ATerminal, FileList, OnFileOperationFinished);
  9646. }
  9647. //---------------------------------------------------------------------------
  9648. void __fastcall TCustomScpExplorerForm::DoDownloadFoundFiles(
  9649. TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  9650. {
  9651. DoOperationOnFoundFiles(foCopy, ATerminal, FileList, OnFileOperationFinished);
  9652. }
  9653. //---------------------------------------------------------------------------
  9654. void __fastcall TCustomScpExplorerForm::DoEditFoundFiles(
  9655. TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  9656. {
  9657. if (CanOperateOnFoundFiles(ATerminal))
  9658. {
  9659. for (int Index = 0; Index < FileList->Count; Index++)
  9660. {
  9661. UnicodeString FileName = FileList->Strings[Index];
  9662. TRemoteFile * File = static_cast<TRemoteFile *>(FileList->Objects[Index]);
  9663. ExecuteRemoteFile(FileName, File, efDefaultEditor);
  9664. OnFileOperationFinished(osRemote, FileName, true);
  9665. }
  9666. }
  9667. }
  9668. //---------------------------------------------------------------------------
  9669. void __fastcall TCustomScpExplorerForm::RemoteFindFiles()
  9670. {
  9671. FFileFindTerminal = Terminal;
  9672. ShowFileFindDialog(
  9673. Terminal, RemoteDirView->Path, DoFindFiles, DoFocusRemotePath, DoDeleteFoundFiles,
  9674. DoDownloadFoundFiles, DoEditFoundFiles);
  9675. }
  9676. //---------------------------------------------------------------------------
  9677. void __fastcall TCustomScpExplorerForm::UpdateTaskbarList(ITaskbarList3 * TaskbarList)
  9678. {
  9679. FTaskbarList = TaskbarList;
  9680. }
  9681. //---------------------------------------------------------------------------
  9682. void __fastcall TCustomScpExplorerForm::SessionsPageControlMouseDown(
  9683. TObject * /*Sender*/, TMouseButton Button, TShiftState /*Shift*/, int X, int Y)
  9684. {
  9685. int Index = SessionsPageControl->IndexOfTabAt(X, Y);
  9686. if (Index >= 0)
  9687. {
  9688. if (Button == mbLeft)
  9689. {
  9690. // WORKAROUND: "Mouse down" is raised only after tab is switched.
  9691. // If switching tab (switching session) takes long enough for user
  9692. // to actually release the button, "mouse down" is still raised,
  9693. // but we do not get "mouse up" event, so dragging is not cancelled,
  9694. // prevent that by not beginning dragging in the first place.
  9695. if (IsKeyPressed(VK_LBUTTON))
  9696. {
  9697. // when user clicks the "+", we get mouse down only after the session
  9698. // is closed, when new session tab is already on X:Y, so dragging
  9699. // starts, prevent that
  9700. if (MilliSecondsBetween(Now(), FSessionsPageControlNewTabTime) > 500)
  9701. {
  9702. TTerminal * Terminal = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  9703. if (Terminal != NULL)
  9704. {
  9705. SessionsPageControl->BeginDrag(false);
  9706. }
  9707. }
  9708. }
  9709. }
  9710. else if (Button == mbMiddle)
  9711. {
  9712. // ignore middle-click for "New session tab" and the last local-local tab
  9713. TManagedTerminal * Session = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  9714. if ((Session != NULL) && CanCloseSession(Session))
  9715. {
  9716. CloseSessionTab(Index);
  9717. }
  9718. }
  9719. }
  9720. }
  9721. //---------------------------------------------------------------------------
  9722. void __fastcall TCustomScpExplorerForm::SessionsPageControlDragDrop(
  9723. TObject * /*Sender*/, TObject * /*Source*/, int X, int Y)
  9724. {
  9725. int Index = SessionsPageControl->IndexOfTabAt(X, Y);
  9726. // do not allow dropping on the "+" tab
  9727. TTerminal * TargetTerminal = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  9728. if ((TargetTerminal != NULL) &&
  9729. (SessionsPageControl->ActivePage->PageIndex != Index))
  9730. {
  9731. Configuration->Usage->Inc(L"SessionTabMoves");
  9732. // this is almost redundant as we would recreate tabs in SessionListChanged,
  9733. // but we want to actually prevent that to avoid flicker
  9734. SessionsPageControl->ActivePage->PageIndex = Index;
  9735. TTerminal * Terminal = GetSessionTabSession(SessionsPageControl->ActivePage);
  9736. TTerminalManager::Instance()->Move(Terminal, TargetTerminal);
  9737. }
  9738. }
  9739. //---------------------------------------------------------------------------
  9740. void __fastcall TCustomScpExplorerForm::SessionsPageControlDragOver(
  9741. TObject * Sender, TObject * Source, int X, int Y,
  9742. TDragState /*State*/, bool & Accept)
  9743. {
  9744. Accept = (Sender == Source);
  9745. if (Accept)
  9746. {
  9747. int Index = SessionsPageControl->IndexOfTabAt(X, Y);
  9748. TTerminal * Terminal = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  9749. // do not allow dragging to the "+" tab
  9750. Accept = (Terminal != NULL);
  9751. }
  9752. }
  9753. //---------------------------------------------------------------------------
  9754. void __fastcall TCustomScpExplorerForm::SessionsDDDragOver(
  9755. int DebugUsedArg(KeyState), const TPoint & Point, int & Effect, int DebugUsedArg(PreferredEffect))
  9756. {
  9757. int Index = SessionsPageControl->IndexOfTabAt(Point.X, Point.Y);
  9758. if (Index < 0)
  9759. {
  9760. Effect = DROPEFFECT_NONE;
  9761. }
  9762. else
  9763. {
  9764. TManagedTerminal * TargetSession = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  9765. // do not allow dropping on the "+" tab or on disconnected or local-local tabs
  9766. if (!IsActiveTerminal(TargetSession) || DebugAlwaysFalse(TargetSession->LocalBrowser))
  9767. {
  9768. Effect = DROPEFFECT_NONE;
  9769. }
  9770. else if ((TargetSession != Terminal) && (Effect == DROPEFFECT_MOVE))
  9771. {
  9772. Effect = DROPEFFECT_COPY;
  9773. }
  9774. }
  9775. }
  9776. //---------------------------------------------------------------------------
  9777. void __fastcall TCustomScpExplorerForm::SessionsDDProcessDropped(
  9778. TObject * /*Sender*/, int /*KeyState*/, const TPoint & Point, int Effect)
  9779. {
  9780. int Index = SessionsPageControl->IndexOfTabAt(Point.X, Point.Y);
  9781. // do not allow dropping on local-local, disconnected and the "+" tab
  9782. // (though we do not seem to get here in that case anyway, contrary to RemoteFileControlDDTargetDrop)
  9783. TManagedTerminal * TargetTerminal = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  9784. if (DebugAlwaysTrue(IsActiveTerminal(TargetTerminal) && !TargetTerminal->LocalBrowser))
  9785. {
  9786. DebugAssert(!IsFileControl(DropSourceControl, osRemote));
  9787. if (!IsFileControl(DropSourceControl, osRemote))
  9788. {
  9789. TTerminalManager::Instance()->ActiveSession = TargetTerminal;
  9790. RemoteFileControlDragDropFileOperation(SessionsPageControl, Effect,
  9791. // Why don't we use Terminal->CurrentDirectory directly?
  9792. TTerminalManager::Instance()->ActiveTerminal->CurrentDirectory,
  9793. // do not force queue, drag drop
  9794. false, true);
  9795. }
  9796. }
  9797. }
  9798. //---------------------------------------------------------------------------
  9799. void __fastcall TCustomScpExplorerForm::QueueDDProcessDropped(
  9800. TObject * /*Sender*/, int /*KeyState*/, const TPoint & /*Point*/, int Effect)
  9801. {
  9802. // Downloads are handled in RemoteFileControlDDEnd
  9803. if (!IsFileControl(DropSourceControl, osRemote))
  9804. {
  9805. RemoteFileControlDragDropFileOperation(QueueView3, Effect,
  9806. Terminal->CurrentDirectory,
  9807. // force queue, drag drop
  9808. true, true);
  9809. }
  9810. }
  9811. //---------------------------------------------------------------------------
  9812. void __fastcall TCustomScpExplorerForm::FormClose(TObject * /*Sender*/, TCloseAction & /*Action*/)
  9813. {
  9814. FShowing = false;
  9815. // Do not save empty workspace
  9816. if (WinConfiguration->AutoSaveWorkspace && (ManagedSession != NULL))
  9817. {
  9818. std::unique_ptr<TObjectList> DataList(DoCollectWorkspace());
  9819. UnicodeString Name = WorkspaceName();
  9820. DoSaveWorkspace(
  9821. Name, DataList.get(),
  9822. !Configuration->DisablePasswordStoring &&
  9823. WinConfiguration->AutoSaveWorkspacePasswords, false);
  9824. WinConfiguration->LastStoredSession = Name;
  9825. }
  9826. }
  9827. //---------------------------------------------------------------------------
  9828. void __fastcall TCustomScpExplorerForm::RemoteDirViewRead(TObject * /*Sender*/)
  9829. {
  9830. TManagedTerminal * ManagedTerminal =
  9831. dynamic_cast<TManagedTerminal *>(RemoteDirView->Terminal);
  9832. if (ManagedTerminal != NULL)
  9833. {
  9834. ManagedTerminal->DirectoryLoaded = Now();
  9835. }
  9836. }
  9837. //---------------------------------------------------------------------------
  9838. void __fastcall TCustomScpExplorerForm::DirViewSelectItem(TObject * Sender,
  9839. TListItem * /*Item*/, bool /*Selected*/)
  9840. {
  9841. TCustomDirView * DirView = reinterpret_cast<TCustomDirView *>(Sender);
  9842. switch (DirView->LastSelectMethod)
  9843. {
  9844. case smKeyboard:
  9845. Configuration->Usage->Inc(L"KeyboardSelections");
  9846. break;
  9847. case smMouse:
  9848. Configuration->Usage->Inc(L"MouseSelections");
  9849. break;
  9850. }
  9851. }
  9852. //---------------------------------------------------------------------------
  9853. int __fastcall TCustomScpExplorerForm::AddSessionColor(TColor Color)
  9854. {
  9855. if (Color != 0)
  9856. {
  9857. return GetSessionColorImage(FSessionColors, Color, FSessionColorMaskImageIndex);
  9858. }
  9859. else
  9860. {
  9861. return FSessionTabImageIndex;
  9862. }
  9863. }
  9864. //---------------------------------------------------------------------------
  9865. int __fastcall TCustomScpExplorerForm::AddFixedSessionImage(int GlyphsSourceIndex)
  9866. {
  9867. TPngImageCollectionItem * Item =
  9868. GlyphsModule->ExplorerImages->PngImages->Items[GlyphsSourceIndex];
  9869. FSessionColors->AddPng(Item->PngImage);
  9870. return FSessionColors->Count - 1;
  9871. }
  9872. //---------------------------------------------------------------------------
  9873. void __fastcall TCustomScpExplorerForm::AddFixedSessionImages()
  9874. {
  9875. FSessionColors->SetSize(GlyphsModule->ExplorerImages->Width, GlyphsModule->ExplorerImages->Height);
  9876. FNewRemoteTabTabImageIndex = AddFixedSessionImage(NonVisualDataModule->NewRemoteTabAction->ImageIndex);
  9877. FNewLocalTabTabImageIndex = AddFixedSessionImage(NonVisualDataModule->NewLocalTabAction->ImageIndex);
  9878. FSessionTabImageIndex = AddFixedSessionImage(SiteImageIndex);
  9879. FSessionColorMaskImageIndex = AddFixedSessionImage(SiteColorMaskImageIndex);
  9880. FLocalBrowserTabImageIndex = AddFixedSessionImage(LocalBrowserImageIndex);
  9881. }
  9882. //---------------------------------------------------------------------------
  9883. int TCustomScpExplorerForm::GetNewTabActionImageIndex()
  9884. {
  9885. return NonVisualDataModule->NewRemoteTabAction->ImageIndex;
  9886. }
  9887. //---------------------------------------------------------------------------
  9888. int TCustomScpExplorerForm::GetNewTabTabImageIndex(TOperationSide Side)
  9889. {
  9890. switch (Side)
  9891. {
  9892. case osLocal:
  9893. return FNewLocalTabTabImageIndex;
  9894. case osRemote:
  9895. case osCurrent:
  9896. return FNewRemoteTabTabImageIndex;
  9897. default:
  9898. DebugFail();
  9899. return -1;
  9900. }
  9901. }
  9902. //---------------------------------------------------------------------------
  9903. void __fastcall TCustomScpExplorerForm::CollectItemsWithTextDisplayMode(TWinControl * Control)
  9904. {
  9905. for (int Index = 0; Index < Control->ControlCount; Index++)
  9906. {
  9907. TControl * AControl = Control->Controls[Index];
  9908. TWinControl * WinControl = dynamic_cast<TWinControl *>(AControl);
  9909. if (WinControl != NULL)
  9910. {
  9911. CollectItemsWithTextDisplayMode(WinControl);
  9912. }
  9913. TTBCustomToolbar * Toolbar = dynamic_cast<TTBCustomToolbar *>(AControl);
  9914. if (Toolbar != NULL)
  9915. {
  9916. // we care for top-level items only
  9917. TTBCustomItem * Items = Toolbar->Items;
  9918. for (int ItemIndex = 0; ItemIndex < Items->Count; ItemIndex++)
  9919. {
  9920. TTBCustomItem * Item = Items->Items[ItemIndex];
  9921. if (((Item->DisplayMode == nbdmImageAndText) ||
  9922. (dynamic_cast<TTBXLabelItem *>(Item) != NULL)) &&
  9923. EligibleForImageDisplayMode(Item))
  9924. {
  9925. FItemsWithTextDisplayMode.insert(Item);
  9926. }
  9927. }
  9928. }
  9929. }
  9930. }
  9931. //---------------------------------------------------------------------------
  9932. bool __fastcall TCustomScpExplorerForm::EligibleForImageDisplayMode(TTBCustomItem * /*Item*/)
  9933. {
  9934. return true;
  9935. }
  9936. //---------------------------------------------------------------------------
  9937. bool __fastcall TCustomScpExplorerForm::UpdateToolbarDisplayMode()
  9938. {
  9939. bool SelectiveToolbarText = WinConfiguration->SelectiveToolbarText;
  9940. TTBItemDisplayMode DisplayMode = (SelectiveToolbarText ? nbdmImageAndText : nbdmDefault);
  9941. typedef std::set<TTBCustomItem *> TItemsWithTextDisplayMode;
  9942. TItemsWithTextDisplayMode::iterator i = FItemsWithTextDisplayMode.begin();
  9943. bool Result = true;
  9944. while (Result && (i != FItemsWithTextDisplayMode.end()))
  9945. {
  9946. TTBCustomItem * Item = *i;
  9947. TTBXLabelItem * Label = dynamic_cast<TTBXLabelItem *>(Item);
  9948. if (Label != NULL)
  9949. {
  9950. // optimization
  9951. if (Label->Visible == SelectiveToolbarText)
  9952. {
  9953. Result = false;
  9954. }
  9955. else
  9956. {
  9957. Label->Visible = SelectiveToolbarText;
  9958. }
  9959. }
  9960. else
  9961. {
  9962. // optimization
  9963. if (Item->DisplayMode == DisplayMode)
  9964. {
  9965. Result = false;
  9966. }
  9967. else
  9968. {
  9969. Item->DisplayMode = DisplayMode;
  9970. }
  9971. }
  9972. ++i;
  9973. }
  9974. UpdateNewTabTab();
  9975. return Result;
  9976. }
  9977. //---------------------------------------------------------------------------
  9978. void __fastcall TCustomScpExplorerForm::DisplaySystemContextMenu()
  9979. {
  9980. DebugFail();
  9981. }
  9982. //---------------------------------------------------------------------------
  9983. bool __fastcall TCustomScpExplorerForm::IsBusy()
  9984. {
  9985. // Among other, a lock level is non zero, while directory is being loaded,
  9986. // even when it happens as a result of dirview navigation,
  9987. // i.e. when TNonVisualDataModule is NOT FBusy.
  9988. // That's why the TNonVisualDataModule::GetBusy calls this method.
  9989. // Among other this prevents a panel auto update to occur while
  9990. // directory is changing.
  9991. return (FLockLevel > 0) || DirView(osCurrent)->IsEditing();
  9992. }
  9993. //---------------------------------------------------------------------------
  9994. Boolean __fastcall TCustomScpExplorerForm::AllowedAction(TAction * Action, TActionAllowed Allowed)
  9995. {
  9996. // While the window is disabled, we still seem to process menu shortcuts at least,
  9997. // so stop it at least here.
  9998. // See also TCustomScpExplorerForm::RemoteDirViewBusy
  9999. bool Result;
  10000. if (Allowed == aaUpdate)
  10001. {
  10002. Result = true;
  10003. }
  10004. else
  10005. {
  10006. Result = !NonVisualDataModule->Busy;
  10007. if (Result && (Allowed == aaShortCut))
  10008. {
  10009. // Otherwise F5 for local-local will be swallowed by disabled local<>remote actions in TCustomScpExplorerForm::KeyDown
  10010. Action->Update();
  10011. Result = Action->Enabled;
  10012. }
  10013. }
  10014. return Result;
  10015. }
  10016. //---------------------------------------------------------------------------
  10017. void __fastcall TCustomScpExplorerForm::EditMenuItemPopup(TTBCustomItem * Sender, bool FromLink)
  10018. {
  10019. NonVisualDataModule->EditMenuItemPopup(Sender, FromLink);
  10020. }
  10021. //---------------------------------------------------------------------------
  10022. void __fastcall TCustomScpExplorerForm::DirViewBusy(TObject * /*Sender*/, int Busy, bool & State)
  10023. {
  10024. // This is somewhat redundant to LockWindow() call from
  10025. // TTerminalManager::TerminalReadDirectoryProgress.
  10026. // But disabling window is known not to block keyboard shorcuts
  10027. // (see TCustomScpExplorerForm::AllowedAction), this hopefully works.
  10028. if (Busy > 0)
  10029. {
  10030. if (NonVisualDataModule->Busy)
  10031. {
  10032. State = false;
  10033. }
  10034. else
  10035. {
  10036. NonVisualDataModule->StartBusy();
  10037. // Will be countered for TCustomDirView.DoExecute in DoDirViewExecFile
  10038. LockWindow();
  10039. }
  10040. }
  10041. else if (Busy < 0)
  10042. {
  10043. UnlockWindow();
  10044. NonVisualDataModule->EndBusy();
  10045. }
  10046. else
  10047. {
  10048. State = NonVisualDataModule->Busy;
  10049. }
  10050. }
  10051. //---------------------------------------------------------------------------
  10052. void __fastcall TCustomScpExplorerForm::SessionsPageControlContextPopup(TObject * /*Sender*/, TPoint & MousePos, bool & Handled)
  10053. {
  10054. int Index = SessionsPageControl->IndexOfTabAt(MousePos.X, MousePos.Y);
  10055. if (Index >= 0)
  10056. {
  10057. TPopupMenu * PopupMenu = NULL;
  10058. TManagedTerminal * Session = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  10059. if (Session != NULL)
  10060. {
  10061. SessionsPageControl->ActivePageIndex = Index;
  10062. if (DebugAlwaysTrue(SessionTabSwitched()))
  10063. {
  10064. // copied from TControl.WMContextMenu
  10065. SendCancelMode(SessionsPageControl);
  10066. // explicit popup instead of using PopupMenu property
  10067. // to avoid menu to popup somewhere within SessionTabSwitched above,
  10068. // while connecting yet not-connected session and hence
  10069. // allowing an access to commands over not-completelly connected session
  10070. PopupMenu = Session->LocalBrowser ? NonVisualDataModule->LocalBrowserPopup : NonVisualDataModule->SessionsPopup;
  10071. }
  10072. }
  10073. else
  10074. {
  10075. if (SupportsLocalBrowser())
  10076. {
  10077. PopupMenu = NonVisualDataModule->NewTabPopup;
  10078. }
  10079. }
  10080. if (PopupMenu != NULL)
  10081. {
  10082. TPoint Point = SessionsPageControl->ClientToScreen(MousePos);
  10083. PopupMenu->PopupComponent = SessionsPageControl;
  10084. PopupMenu->Popup(Point.x, Point.y);
  10085. }
  10086. }
  10087. Handled = true;
  10088. }
  10089. //---------------------------------------------------------------------------
  10090. bool __fastcall TCustomScpExplorerForm::CanChangePassword()
  10091. {
  10092. return HasActiveTerminal() && Terminal->IsCapable[fcChangePassword];
  10093. }
  10094. //---------------------------------------------------------------------------
  10095. void __fastcall TCustomScpExplorerForm::ChangePassword()
  10096. {
  10097. Configuration->Usage->Inc(L"PasswordChanges");
  10098. std::unique_ptr<TSessionData> ChangePasswordData(Terminal->SessionData->Clone());
  10099. ChangePasswordData->DisableAuthentationsExceptPassword();
  10100. ChangePasswordData->ChangePassword = true;
  10101. std::unique_ptr<TTerminal> ChangePasswordSession(
  10102. Terminal->CreateSecondarySession(L"ChangePassword", ChangePasswordData.get()));
  10103. ChangePasswordSession->AutoReadDirectory = false;
  10104. if (TTerminalManager::Instance()->ConnectTerminal(ChangePasswordSession.get()))
  10105. {
  10106. MessageDialog(MainInstructions(LoadStr(PASSWORD_CHANGED)), qtInformation, qaOK, HELP_CHANGE_PASSWORD);
  10107. }
  10108. else
  10109. {
  10110. Configuration->Usage->Inc(L"PasswordChangesFailed");
  10111. }
  10112. }
  10113. //---------------------------------------------------------------------------
  10114. bool __fastcall TCustomScpExplorerForm::CanPrivateKeyUpload()
  10115. {
  10116. // No nice way to assert SSH2
  10117. return HasActiveTerminal() && (Terminal->FSProtocol == cfsSFTP);
  10118. }
  10119. //---------------------------------------------------------------------------
  10120. void __fastcall TCustomScpExplorerForm::PrivateKeyUpload()
  10121. {
  10122. TOperationVisualizer OperationVisualizer;
  10123. UnicodeString FileName = Terminal->SessionData->PublicKeyFile;
  10124. TTerminalManager::Instance()->UploadPublicKey(Terminal, NULL, FileName);
  10125. }
  10126. //---------------------------------------------------------------------------
  10127. void __fastcall TCustomScpExplorerForm::ChangeScale(int M, int D)
  10128. {
  10129. TForm::ChangeScale(M, D);
  10130. int APixelsPerInch = GetControlPixelsPerInch(this);
  10131. HANDLE ResourceModule = GUIConfiguration->ChangeToDefaultResourceModule();
  10132. try
  10133. {
  10134. GlyphsModule->SetPixelsPerInch(APixelsPerInch);
  10135. }
  10136. __finally
  10137. {
  10138. GUIConfiguration->ChangeResourceModule(ResourceModule);
  10139. }
  10140. }
  10141. //---------------------------------------------------------------------------
  10142. void __fastcall TCustomScpExplorerForm::DockContextPopup(TObject * Sender, TPoint & MousePos, bool & /*Handled*/)
  10143. {
  10144. NonVisualDataModule->ControlContextPopup(Sender, MousePos);
  10145. }
  10146. //---------------------------------------------------------------------
  10147. class TPublicControl : public TControl
  10148. {
  10149. friend class TCustomScpExplorerForm;
  10150. };
  10151. //---------------------------------------------------------------------------
  10152. void __fastcall TCustomScpExplorerForm::CopyPopup(TControl * DestControl, TControl * SourceControl)
  10153. {
  10154. static_cast<TPublicControl *>(DestControl)->PopupMenu = static_cast<TPublicControl *>(SourceControl)->PopupMenu;
  10155. static_cast<TPublicControl *>(DestControl)->OnContextPopup = static_cast<TPublicControl *>(SourceControl)->OnContextPopup;
  10156. }
  10157. //---------------------------------------------------------------------------
  10158. void __fastcall TCustomScpExplorerForm::DoBookmarkClick(TOperationSide Side, TObject * Sender)
  10159. {
  10160. TBookmark * Bookmark = DebugNotNull(reinterpret_cast<TBookmark *>(dynamic_cast<TTBCustomItem *>(Sender)->Tag));
  10161. OpenBookmark(Side, Bookmark);
  10162. }
  10163. //---------------------------------------------------------------------------
  10164. void __fastcall TCustomScpExplorerForm::LocalBookmarkClick(TObject * Sender)
  10165. {
  10166. DoBookmarkClick(osLocal, Sender);
  10167. }
  10168. //---------------------------------------------------------------------------
  10169. void __fastcall TCustomScpExplorerForm::RemoteBookmarkClick(TObject * Sender)
  10170. {
  10171. DoBookmarkClick(osRemote, Sender);
  10172. }
  10173. //---------------------------------------------------------------------------
  10174. void __fastcall TCustomScpExplorerForm::CreateOpenDirMenuList(
  10175. TTBCustomItem * Menu, TOperationSide Side, TBookmarkList * BookmarkList)
  10176. {
  10177. if (BookmarkList != NULL)
  10178. {
  10179. TNotifyEvent OnBookmarkClick = (Side == osLocal) ? &LocalBookmarkClick : &RemoteBookmarkClick;
  10180. if (IsLocalBrowserMode())
  10181. {
  10182. Side = osLocal;
  10183. }
  10184. if (!WinConfiguration->UseLocationProfiles)
  10185. {
  10186. std::unique_ptr<TStringList> Directories(new TStringList());
  10187. Directories->CaseSensitive = (Side == osRemote);
  10188. for (int Index = 0; Index < BookmarkList->Count; Index++)
  10189. {
  10190. TBookmark * Bookmark = BookmarkList->Bookmarks[Index];
  10191. UnicodeString Directory = Bookmark->GetSideDirectory(Side);
  10192. if (!Directory.IsEmpty() && (Directories->IndexOf(Directory) < 0))
  10193. {
  10194. std::unique_ptr<TTBCustomItem> Item(new TTBXItem(Owner));
  10195. Item->Caption = EscapeHotkey(Directory);
  10196. Item->ShortCut = Bookmark->ShortCut;
  10197. Item->OnClick = OnBookmarkClick;
  10198. Item->Tag = reinterpret_cast<int>(Bookmark);
  10199. Directories->Add(Directory);
  10200. Menu->Add(Item.release());
  10201. }
  10202. }
  10203. }
  10204. else
  10205. {
  10206. std::unique_ptr<TStrings> Folders(CreateSortedStringList());
  10207. for (int Index = 0; Index < BookmarkList->Count; Index++)
  10208. {
  10209. TBookmark * Bookmark = BookmarkList->Bookmarks[Index];
  10210. if (!Bookmark->Node.IsEmpty())
  10211. {
  10212. Folders->Add(Bookmark->Node);
  10213. }
  10214. }
  10215. for (int Index = 0; Index < Folders->Count; Index++)
  10216. {
  10217. std::unique_ptr<TTBCustomItem> Item(new TTBXSubmenuItem(Owner));
  10218. Item->Caption = Folders->Strings[Index];
  10219. Item->ImageIndex = NonVisualDataModule->RemoteChangePathAction2->ImageIndex;
  10220. Folders->Objects[Index] = Item.get();
  10221. Menu->Add(Item.release());
  10222. }
  10223. for (int Index = 0; Index < BookmarkList->Count; Index++)
  10224. {
  10225. TBookmark * Bookmark = BookmarkList->Bookmarks[Index];
  10226. TTBCustomItem * Parent;
  10227. if (!Bookmark->Node.IsEmpty())
  10228. {
  10229. DebugAssert(Folders->IndexOf(Bookmark->Node) >= 0);
  10230. Parent = dynamic_cast<TTBCustomItem *>(Folders->Objects[Folders->IndexOf(Bookmark->Node)]);
  10231. }
  10232. else
  10233. {
  10234. Parent = Menu;
  10235. }
  10236. std::unique_ptr<TTBCustomItem> Item(new TTBXItem(Owner));
  10237. Item->Caption = Bookmark->Name;
  10238. Item->ShortCut = Bookmark->ShortCut;
  10239. Item->OnClick = OnBookmarkClick;
  10240. Item->Tag = reinterpret_cast<int>(Bookmark);
  10241. if (((Bookmark->Name == Bookmark->Local) || (Bookmark->Name == Bookmark->Remote)) &&
  10242. (Bookmark->Local.IsEmpty() || Bookmark->Remote.IsEmpty()))
  10243. {
  10244. // No hint for location profiles that are actually mere "bookmarks"
  10245. }
  10246. else
  10247. {
  10248. UnicodeString Hint = FORMAT(LoadStrPart(LOCATION_PROFILE_HINT, 1), (Bookmark->Name));
  10249. UnicodeString LongHint;
  10250. if (!Bookmark->Local.IsEmpty())
  10251. {
  10252. AddToList(LongHint, FORMAT(LoadStrPart(LOCATION_PROFILE_HINT, 2), (Bookmark->Local)), L"\n");
  10253. }
  10254. if (!Bookmark->Remote.IsEmpty())
  10255. {
  10256. AddToList(LongHint, FORMAT(LoadStrPart(LOCATION_PROFILE_HINT, 3), (Bookmark->Remote)), L"\n");
  10257. }
  10258. AddToList(Hint, LongHint, L"|");
  10259. Item->Hint = Hint;
  10260. }
  10261. Parent->Add(Item.release());
  10262. }
  10263. }
  10264. AddMenuSeparator(Menu);
  10265. }
  10266. }
  10267. //---------------------------------------------------------------------------
  10268. void __fastcall TCustomScpExplorerForm::CreateOpenDirMenu(TTBCustomItem * Menu, TOperationSide Side)
  10269. {
  10270. Menu->Clear();
  10271. std::unique_ptr<TTBCustomItem> Item;
  10272. Item.reset(new TTBXItem(Owner));
  10273. Item->Action = (Side == osLocal) ? NonVisualDataModule->LocalOpenDirAction : NonVisualDataModule->RemoteOpenDirAction;
  10274. Item->Options = Item->Options << tboDefault;
  10275. Menu->Add(Item.release());
  10276. AddMenuSeparator(Menu);
  10277. if (Terminal != NULL)
  10278. {
  10279. CreateOpenDirMenuList(Menu, Side, WinConfiguration->Bookmarks[Terminal->SessionData->SessionKey]);
  10280. }
  10281. CreateOpenDirMenuList(Menu, Side, WinConfiguration->SharedBookmarks);
  10282. Item.reset(new TTBXItem(Owner));
  10283. Item->Action = (Side == osLocal) ? NonVisualDataModule->LocalAddBookmarkAction2 : NonVisualDataModule->RemoteAddBookmarkAction2;
  10284. Menu->Add(Item.release());
  10285. }
  10286. //---------------------------------------------------------------------------
  10287. bool __fastcall TCustomScpExplorerForm::TryOpenDirectory(TOperationSide Side, const UnicodeString & Path)
  10288. {
  10289. bool Result = true;
  10290. bool Remote = !IsSideLocalBrowser(Side);
  10291. try
  10292. {
  10293. UnicodeString APath = Path;
  10294. if (Remote)
  10295. {
  10296. Terminal->ExceptionOnFail = true;
  10297. }
  10298. else
  10299. {
  10300. APath = ExpandEnvironmentVariables(Path);
  10301. }
  10302. try
  10303. {
  10304. DirView(Side)->Path = APath;
  10305. }
  10306. __finally
  10307. {
  10308. if (Remote)
  10309. {
  10310. Terminal->ExceptionOnFail = false;
  10311. }
  10312. }
  10313. }
  10314. catch (Exception & E)
  10315. {
  10316. if (!Remote || Terminal->Active)
  10317. {
  10318. ShowExtendedExceptionEx(Terminal, &E);
  10319. Result = false;
  10320. }
  10321. else
  10322. {
  10323. throw;
  10324. }
  10325. }
  10326. return Result;
  10327. }
  10328. //---------------------------------------------------------------------------
  10329. void __fastcall TCustomScpExplorerForm::ReloadDirectory(TOperationSide Side)
  10330. {
  10331. // Make sure there some feedback even when loading tiny local folders
  10332. TInstantOperationVisualizer Visualizer;
  10333. DirView(Side)->ReloadDirectory();
  10334. }
  10335. //---------------------------------------------------------------------------
  10336. void __fastcall TCustomScpExplorerForm::CloseSessionTab(int Index)
  10337. {
  10338. if (Index == SessionsPageControl->TabIndex)
  10339. {
  10340. CloseSession();
  10341. }
  10342. else
  10343. {
  10344. TTabSheet * TabSheet = SessionsPageControl->Pages[Index];
  10345. TTerminal * Terminal = GetSessionTabSession(TabSheet);
  10346. TTerminalManager * Manager = TTerminalManager::Instance();
  10347. if (!Terminal->Active)
  10348. {
  10349. Manager->FreeTerminal(Terminal);
  10350. }
  10351. else
  10352. {
  10353. TTerminalQueue * Queue = Manager->FindQueueForTerminal(Terminal);
  10354. if (CanCloseQueue(Queue))
  10355. {
  10356. Manager->FreeTerminal(Terminal);
  10357. }
  10358. }
  10359. }
  10360. }
  10361. //---------------------------------------------------------------------------
  10362. void __fastcall TCustomScpExplorerForm::SessionsPageControlTabButtonClick(TPageControl *, int Index)
  10363. {
  10364. TManagedTerminal * Session = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  10365. if (Session == NULL)
  10366. {
  10367. TRect ButtonRect = SessionsPageControl->TabButtonRect(Index);
  10368. ButtonRect = TRect(SessionsPageControl->ClientToScreen(ButtonRect.TopLeft()), SessionsPageControl->ClientToScreen(ButtonRect.BottomRight()));
  10369. //TPoint P = TPoint(ButtonRect.Left, ButtonRect.Bottom);
  10370. //P = SessionsPageControl->ClientToScreen(P);
  10371. MenuPopup(NonVisualDataModule->NewTabPopup, ButtonRect, SessionsPageControl);
  10372. }
  10373. else
  10374. {
  10375. CloseSessionTab(Index);
  10376. }
  10377. }
  10378. //---------------------------------------------------------------------------
  10379. void __fastcall TCustomScpExplorerForm::CopyFilesToClipboard(TOperationSide Side, bool OnFocused)
  10380. {
  10381. if (DebugAlwaysTrue(!IsSideLocalBrowser(Side)))
  10382. {
  10383. TInstantOperationVisualizer Visualizer;
  10384. // To trigger ClipboardDataObjectRelease (in case the clipboard already contains our data)
  10385. // before we create a new data object
  10386. ClipboardClear();
  10387. DebugAssert(FClipboardFakeDirectory.IsEmpty());
  10388. DebugAssert(FClipboardFakeMonitors.get() == NULL);
  10389. FClipboardFakeDirectory = CreateFakeTransferDirectory();
  10390. // Intermediate variable to WORKAROUND a crash in compiler
  10391. TObjectList * ClipboardFakeMonitors =
  10392. StartCreationDirectoryMonitorsOnEachDrive(FILE_NOTIFY_CHANGE_DIR_NAME, ClipboardFakeCreated);
  10393. FClipboardFakeMonitors.reset(ClipboardFakeMonitors);
  10394. if (FClipboardDragDropFilesEx.get() == NULL)
  10395. {
  10396. FClipboardDragDropFilesEx.reset(new TDragDropFilesEx(this));
  10397. FClipboardDragDropFilesEx->OnDataObjectRelease = ClipboardDataObjectRelease;
  10398. }
  10399. FClipboardDragDropFilesEx->FileList->Clear();
  10400. FClipboardDragDropFilesEx->FileList->AddItem(NULL, FClipboardFakeDirectory);
  10401. FClipboardDragDropFilesEx->CopyToClipboard();
  10402. FClipboardTerminal = Terminal;
  10403. // Need full paths, as cwd can be different once files are pasted
  10404. FClipboardFileList.reset(TRemoteFileList::CloneStrings(RemoteDirView->CreateFileList(OnFocused, true)));
  10405. for (int Index = 0; Index < FClipboardFileList->Count; Index++)
  10406. {
  10407. FClipboardFileList->Strings[Index] = UnixExcludeTrailingBackslash(FClipboardFileList->Strings[Index]);
  10408. }
  10409. }
  10410. }
  10411. //---------------------------------------------------------------------------
  10412. void __fastcall TCustomScpExplorerForm::ClipboardClear()
  10413. {
  10414. if (OpenClipboard(0))
  10415. {
  10416. EmptyClipboard(); // Calls ClipboardDataObjectRelease, if clipboard contains our data
  10417. CloseClipboard();
  10418. }
  10419. }
  10420. //---------------------------------------------------------------------------
  10421. bool __fastcall TCustomScpExplorerForm::DoesClipboardContainOurFiles()
  10422. {
  10423. return !FClipboardFakeDirectory.IsEmpty();
  10424. }
  10425. //---------------------------------------------------------------------------
  10426. void __fastcall TCustomScpExplorerForm::ClipboardStop()
  10427. {
  10428. RemoveDir(ApiPath(FClipboardFakeDirectory));
  10429. FClipboardFakeDirectory = UnicodeString();
  10430. FClipboardTerminal = NULL;
  10431. if (FDownloadingFromClipboard)
  10432. {
  10433. // We are called by the monitor, so attempt to release it would deadlock
  10434. FClipboardFakeMonitorsPendingReset = true;
  10435. }
  10436. else
  10437. {
  10438. FClipboardFakeMonitors.reset(NULL);
  10439. }
  10440. }
  10441. //---------------------------------------------------------------------------
  10442. void __fastcall TCustomScpExplorerForm::ClipboardDataObjectRelease(TObject * /*Sender*/)
  10443. {
  10444. if (DebugAlwaysTrue(!FClipboardFakeDirectory.IsEmpty()))
  10445. {
  10446. ClipboardStop();
  10447. }
  10448. }
  10449. //---------------------------------------------------------------------------
  10450. void __fastcall TCustomScpExplorerForm::ClipboardDownload(const UnicodeString & TargetDirectory, bool NoConfirmation, bool DragDrop)
  10451. {
  10452. TTerminalManager * Manager = TTerminalManager::Instance();
  10453. Manager->ActiveSession = FClipboardTerminal;
  10454. TTransferOperationParam Params;
  10455. Params.TargetDirectory = TargetDirectory;
  10456. Params.DragDrop = DragDrop;
  10457. ExecuteFileOperation(foCopy, osRemote, FClipboardFileList.get(), NoConfirmation, &Params);
  10458. }
  10459. //---------------------------------------------------------------------------
  10460. void __fastcall TCustomScpExplorerForm::ClipboardFakeCreated(TObject * /*Sender*/, const UnicodeString FileName)
  10461. {
  10462. // It can actually rarelly happen that some random file is created, while we are shutting down the monitor
  10463. // (as it pumps a Windows message queue while being shutted down)
  10464. if (DebugAlwaysTrue(!FClipboardFakeDirectory.IsEmpty()) &&
  10465. SameText(ExtractFileName(FileName), ExtractFileName(FClipboardFakeDirectory)))
  10466. {
  10467. // Can fail as it can be e.g. locked by AV, so we retry that later
  10468. bool Removed = RemoveDir(ApiPath(FileName));
  10469. try
  10470. {
  10471. if (!NonVisualDataModule->Busy)
  10472. {
  10473. DebugAssert(!FClipboardFakeMonitorsPendingReset);
  10474. FClipboardFakeMonitorsPendingReset = false;
  10475. bool NoConfirmation = (WinConfiguration->DDTransferConfirmation == asOff);
  10476. TAutoFlag Flag(FDownloadingFromClipboard);
  10477. ClipboardDownload(ExtractFilePath(FileName), NoConfirmation, true);
  10478. }
  10479. }
  10480. __finally
  10481. {
  10482. if (!Removed)
  10483. {
  10484. RemoveDir(ApiPath(FileName));
  10485. }
  10486. }
  10487. }
  10488. }
  10489. //---------------------------------------------------------------------------
  10490. void __fastcall TCustomScpExplorerForm::DirViewGetItemColor(
  10491. TObject * Sender, UnicodeString FileName, bool Directory, __int64 Size, TDateTime Modification, TColor & Color)
  10492. {
  10493. TFileMasks::TParams MaskParams;
  10494. MaskParams.Size = Size;
  10495. MaskParams.Modification = Modification;
  10496. TCustomDirView * DirView = DebugNotNull(dynamic_cast<TCustomDirView *>(Sender));
  10497. bool Local = (dynamic_cast<TUnixDirView *>(DirView) == NULL);
  10498. // TUnixDirView::ItemFullFileName returns trailing backslash for directories, what filemask cannot deal with
  10499. if (!Local && Directory)
  10500. {
  10501. FileName = UnixExcludeTrailingBackslash(FileName);
  10502. }
  10503. for (TFileColorData::TList::const_iterator Iter = FFileColors.begin(); Iter != FFileColors.end(); Iter++)
  10504. {
  10505. bool ImplicitMatch;
  10506. if (Iter->FileMask.Matches(FileName, Local, Directory, &MaskParams, false, ImplicitMatch) &&
  10507. !ImplicitMatch)
  10508. {
  10509. Color = Iter->Color;
  10510. break;
  10511. }
  10512. }
  10513. }
  10514. //---------------------------------------------------------------------------
  10515. void __fastcall TCustomScpExplorerForm::DirViewKeyDown(TObject * Sender, WORD & Key, TShiftState)
  10516. {
  10517. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  10518. if (!DirView->IsEditing())
  10519. {
  10520. // Handled here, instead of RemoteDirViewKeyPress (as on Login dialog),
  10521. // as VK_BACK is handled by TCustomDirView.KeyDown and we can swallow it here,
  10522. // while searching
  10523. if (Key == VK_BACK)
  10524. {
  10525. if (!FIncrementalSearch.IsEmpty())
  10526. {
  10527. if (FIncrementalSearch.Length() == 1)
  10528. {
  10529. ResetIncrementalSearch();
  10530. }
  10531. else
  10532. {
  10533. UnicodeString NewText = FIncrementalSearch.SubString(1, FIncrementalSearch.Length() - 1);
  10534. IncrementalSearch(NewText, false, false);
  10535. }
  10536. Key = 0;
  10537. }
  10538. }
  10539. }
  10540. }
  10541. //---------------------------------------------------------------------------
  10542. void __fastcall TCustomScpExplorerForm::DirViewKeyPress(TObject * Sender, wchar_t & Key)
  10543. {
  10544. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  10545. if (!DirView->IsEditing() && (WinConfiguration->PanelSearch != isOff))
  10546. {
  10547. // Filter control sequences.
  10548. // When not searching yet, prefer use of the space for toggling file selection
  10549. // (so we cannot incrementally search for a string starting with a space).
  10550. if ((Key > VK_SPACE) ||
  10551. ((Key == VK_SPACE) && (GetKeyState(VK_CONTROL) >= 0) && !FIncrementalSearch.IsEmpty()))
  10552. {
  10553. IncrementalSearch(FIncrementalSearch + Key, false, false);
  10554. Key = 0;
  10555. }
  10556. }
  10557. }
  10558. //---------------------------------------------------------------------------
  10559. void __fastcall TCustomScpExplorerForm::ResetIncrementalSearch()
  10560. {
  10561. if (!FIncrementalSearch.IsEmpty())
  10562. {
  10563. FIncrementalSearch = L"";
  10564. DirView(osCurrent)->UpdateStatusBar();
  10565. }
  10566. }
  10567. //---------------------------------------------------------------------------
  10568. void __fastcall TCustomScpExplorerForm::IncrementalSearch(const UnicodeString & Text, bool SkipCurrent, bool Reverse)
  10569. {
  10570. TListItem * Item = SearchFile(Text, SkipCurrent, Reverse);
  10571. if (Item == NULL)
  10572. {
  10573. MessageBeep(MB_ICONHAND);
  10574. }
  10575. else
  10576. {
  10577. TCustomDirView * ADirView = DirView(osCurrent);
  10578. {
  10579. TAutoNestingCounter Guard(FIncrementalSearching);
  10580. ADirView->FocusItem(Item);
  10581. if (ADirView->NortonLike == nlOff)
  10582. {
  10583. if ((ADirView->Selected != Item) ||
  10584. (ADirView->SelCount > 1))
  10585. {
  10586. ADirView->ClearSelection();
  10587. ADirView->Selected = Item;
  10588. }
  10589. }
  10590. }
  10591. FIncrementalSearch = Text;
  10592. Item->MakeVisible(false);
  10593. TListItem * NextItem = SearchFile(Text, true, Reverse);
  10594. FIncrementalSearchHaveNext = (NextItem != NULL) && (NextItem != Item);
  10595. ADirView->UpdateStatusBar();
  10596. }
  10597. }
  10598. //---------------------------------------------------------------------------
  10599. TListItem * __fastcall TCustomScpExplorerForm::GetNextFile(TListItem * Item, bool Reverse)
  10600. {
  10601. int Index = Item->Index;
  10602. if (!Reverse)
  10603. {
  10604. Index++;
  10605. if (Index >= Item->Owner->Count)
  10606. {
  10607. Index = 0;
  10608. }
  10609. }
  10610. else
  10611. {
  10612. Index--;
  10613. if (Index < 0)
  10614. {
  10615. Index = Item->Owner->Count - 1;
  10616. }
  10617. }
  10618. return Item->Owner->Item[Index];
  10619. }
  10620. //---------------------------------------------------------------------------
  10621. TListItem * __fastcall TCustomScpExplorerForm::SearchFile(const UnicodeString & Text, bool SkipCurrent, bool Reverse)
  10622. {
  10623. TCustomDirView * ADirView = DirView(osCurrent);
  10624. if (ADirView->Items->Count == 0)
  10625. {
  10626. return NULL;
  10627. }
  10628. else
  10629. {
  10630. TListItem * CurrentItem = (ADirView->ItemFocused != NULL) ? ADirView->ItemFocused : ADirView->Items->Item[0];
  10631. TListItem * Item = CurrentItem;
  10632. if (SkipCurrent)
  10633. {
  10634. Item = DebugNotNull(GetNextFile(Item, Reverse));
  10635. }
  10636. while (true)
  10637. {
  10638. bool Matches = false;
  10639. switch (WinConfiguration->PanelSearch)
  10640. {
  10641. case isNameStartOnly:
  10642. Matches = ContainsTextSemiCaseSensitive(ADirView->ItemFileName(Item).SubString(1, Text.Length()), Text);
  10643. break;
  10644. case isName:
  10645. Matches = ContainsTextSemiCaseSensitive(ADirView->ItemFileName(Item), Text);
  10646. break;
  10647. case isAll:
  10648. int ColCount = (ADirView->ViewStyle == vsReport) ? ADirView->ColProperties->Count : 1;
  10649. int Index = 0;
  10650. while ((Index < ColCount) && !Matches)
  10651. {
  10652. bool Visible = (ADirView->ViewStyle == vsReport) ? ADirView->ColProperties->Visible[Index] : true;
  10653. if (Visible)
  10654. {
  10655. Matches = ContainsTextSemiCaseSensitive(ADirView->GetColumnText(Item, Index), Text);
  10656. }
  10657. Index++;
  10658. }
  10659. break;
  10660. }
  10661. if (Matches)
  10662. {
  10663. return Item;
  10664. }
  10665. Item = GetNextFile(Item, Reverse);
  10666. if (Item == CurrentItem)
  10667. {
  10668. return NULL;
  10669. }
  10670. }
  10671. }
  10672. }
  10673. //---------------------------------------------------------------------------
  10674. void __fastcall TCustomScpExplorerForm::DirViewExit(TObject *)
  10675. {
  10676. ResetIncrementalSearch();
  10677. }
  10678. //---------------------------------------------------------------------------
  10679. void __fastcall TCustomScpExplorerForm::CMDialogKey(TWMKeyDown & Message)
  10680. {
  10681. if (Message.CharCode == VK_TAB)
  10682. {
  10683. if (!FIncrementalSearch.IsEmpty())
  10684. {
  10685. TShiftState Shift = KeyDataToShiftState(Message.KeyData);
  10686. bool Reverse = Shift.Contains(ssShift);
  10687. IncrementalSearch(FIncrementalSearch, true, Reverse);
  10688. Message.Result = 1;
  10689. return;
  10690. }
  10691. }
  10692. else if (Message.CharCode == VK_ESCAPE)
  10693. {
  10694. if (!FIncrementalSearch.IsEmpty())
  10695. {
  10696. ResetIncrementalSearch();
  10697. Message.Result = 1;
  10698. return;
  10699. }
  10700. }
  10701. TForm::Dispatch(&Message);
  10702. }
  10703. //---------------------------------------------------------------------------
  10704. void __fastcall TCustomScpExplorerForm::Deactivate()
  10705. {
  10706. TForm::Deactivate();
  10707. // E.g. when switching to an internal editor window
  10708. ResetIncrementalSearch();
  10709. }
  10710. //---------------------------------------------------------------------------
  10711. void __fastcall TCustomScpExplorerForm::ApplicationEventsDeactivate(TObject *)
  10712. {
  10713. // When switching to another application
  10714. ResetIncrementalSearch();
  10715. }
  10716. //---------------------------------------------------------------------------
  10717. void __fastcall TCustomScpExplorerForm::ApplicationEventsModalBegin(TObject *)
  10718. {
  10719. ResetIncrementalSearch();
  10720. }
  10721. //---------------------------------------------------------------------------
  10722. void __fastcall TCustomScpExplorerForm::DirViewChangeFocus(TObject *, TListItem *)
  10723. {
  10724. if (FIncrementalSearching <= 0)
  10725. {
  10726. ResetIncrementalSearch();
  10727. }
  10728. }
  10729. //---------------------------------------------------------------------------
  10730. void __fastcall TCustomScpExplorerForm::RemoteStatusBarMouseDown(TObject *, TMouseButton, TShiftState, int, int)
  10731. {
  10732. CountClicksForWindowPrint(this);
  10733. }
  10734. //---------------------------------------------------------------------------
  10735. void __fastcall TCustomScpExplorerForm::RemoteDirViewResize(TObject *)
  10736. {
  10737. CenterReconnectToolbar();
  10738. }
  10739. //---------------------------------------------------------------------------
  10740. void __fastcall TCustomScpExplorerForm::BrowseFile()
  10741. {
  10742. if (RemoteDirView->ItemFocused != NULL)
  10743. {
  10744. RemoteDirView->ItemFocused->Selected = true;
  10745. }
  10746. }
  10747. //---------------------------------------------------------------------------
  10748. void __fastcall TCustomScpExplorerForm::UpdateQueueFileList()
  10749. {
  10750. if (!FQueueStatusUpdating)
  10751. {
  10752. bool Refresh;
  10753. if ((FQueueStatus != NULL) && QueueFileList->Visible)
  10754. {
  10755. Refresh = FQueueStatus->UpdateFileList(FQueueController->GetFocusedPrimaryItem(), FQueueFileList.get());
  10756. }
  10757. else
  10758. {
  10759. if (FQueueFileList->GetCount() > 0)
  10760. {
  10761. FQueueFileList->Clear();
  10762. Refresh = true;
  10763. }
  10764. }
  10765. int Count = FQueueFileList->GetCount();
  10766. if (QueueFileList->Items->Count != Count)
  10767. {
  10768. DebugAssert(Refresh);
  10769. QueueFileList->Items->Count = Count;
  10770. }
  10771. if (Refresh)
  10772. {
  10773. QueueFileList->Invalidate();
  10774. }
  10775. DebugAssert(QueueFileList->Items->Count == FQueueFileList->GetCount());
  10776. }
  10777. }
  10778. //---------------------------------------------------------------------------
  10779. void __fastcall TCustomScpExplorerForm::QueueFileListData(TObject *, TListItem * Item)
  10780. {
  10781. Item->Caption = FQueueFileList->GetFileName(Item->Index);
  10782. }
  10783. //---------------------------------------------------------------------------
  10784. void __fastcall TCustomScpExplorerForm::QueueView3Change(TObject *, TListItem *, TItemChange)
  10785. {
  10786. // Should be improved, once we do not refresh the list when switching between details of the same batch.
  10787. // See TSynchronizeChecklistDialog::UpdateTimer
  10788. if (QueueFileList->Items->Count > 0)
  10789. {
  10790. QueueFileList->Items->Item[0]->MakeVisible(false);
  10791. }
  10792. UpdateQueueFileList();
  10793. }
  10794. //---------------------------------------------------------------------------
  10795. void __fastcall TCustomScpExplorerForm::QueueLabelGetStatus(TCustomPathLabel *, bool & Active)
  10796. {
  10797. Active = QueueView3->Focused() || QueueFileList->Focused();
  10798. }
  10799. //---------------------------------------------------------------------------
  10800. void __fastcall TCustomScpExplorerForm::QueueFileListEnterExit(TObject *)
  10801. {
  10802. QueueLabelUpdateStatus();
  10803. }
  10804. //---------------------------------------------------------------------------
  10805. void __fastcall TCustomScpExplorerForm::QueueFileListCustomDrawItem(
  10806. TCustomListView * Sender, TListItem * Item, TCustomDrawState, bool & DebugUsedArg(DefaultDraw))
  10807. {
  10808. int State = FQueueFileList->GetState(Item->Index);
  10809. if (State == qfsProcessed)
  10810. {
  10811. Sender->Canvas->Font->Color = clGrayText;
  10812. }
  10813. }
  10814. //---------------------------------------------------------------------------
  10815. void __fastcall TCustomScpExplorerForm::QueueFileListColumnAutoSize()
  10816. {
  10817. // We do not really need whole width, so we always preemptivelly keep free space for the vertical scrollbar
  10818. QueueFileList->Column[0]->Width =
  10819. QueueFileList->ClientWidth - GetSystemMetricsForControl(QueueFileList, SM_CXVSCROLL) - ScaleByTextHeight(QueueFileList, 8);
  10820. }
  10821. //---------------------------------------------------------------------------
  10822. void __fastcall TCustomScpExplorerForm::QueueFileListResize(TObject *)
  10823. {
  10824. QueueFileListColumnAutoSize();
  10825. }
  10826. //---------------------------------------------------------------------------
  10827. void __fastcall TCustomScpExplorerForm::CloseApp()
  10828. {
  10829. // Called from TNonVisualDataModule::ExplorerActionsExecute, which sets busy flag.
  10830. // FormCloseQuery must check busy state, so that the application cannot be closed while busy by Alt+F4 and X button.
  10831. // So we clear the flag here.
  10832. DebugAssert(NonVisualDataModule->Busy);
  10833. NonVisualDataModule->EndBusy();
  10834. try
  10835. {
  10836. Close();
  10837. }
  10838. __finally
  10839. {
  10840. NonVisualDataModule->StartBusy();
  10841. }
  10842. }
  10843. //---------------------------------------------------------------------------
  10844. bool __fastcall TCustomScpExplorerForm::IsActiveTerminal(TTerminal * Terminal)
  10845. {
  10846. return (Terminal != NULL) && Terminal->Active;
  10847. }
  10848. //---------------------------------------------------------------------------
  10849. bool __fastcall TCustomScpExplorerForm::HasManagedSession()
  10850. {
  10851. return (ManagedSession != NULL);
  10852. }
  10853. //---------------------------------------------------------------------------
  10854. bool __fastcall TCustomScpExplorerForm::HasActiveTerminal()
  10855. {
  10856. return IsActiveTerminal(Terminal);
  10857. }
  10858. //---------------------------------------------------------------------------
  10859. void TCustomScpExplorerForm::LocalLocalCopy(
  10860. TFileOperation, TOperationSide, bool DebugUsedArg(OnFocused), bool DebugUsedArg(NoConfirmation),
  10861. bool DebugUsedArg(DragDrop), unsigned int DebugUsedArg(Flags))
  10862. {
  10863. DebugFail();
  10864. }
  10865. //---------------------------------------------------------------------------
  10866. void TCustomScpExplorerForm::LocalLocalCopyCommand(TFileOperation Operation, TOperationSide Side, bool OnFocused, unsigned int Flags)
  10867. {
  10868. LocalLocalCopy(Operation, Side, OnFocused, !WinConfiguration->ConfirmTransferring, false, Flags);
  10869. }
  10870. //---------------------------------------------------------------------------
  10871. void __fastcall TCustomScpExplorerForm::SessionsPageControlResize(TObject *)
  10872. {
  10873. // Changing tab list triggers the OnResize.
  10874. if (!FUpdatingSessionTabs)
  10875. {
  10876. // UpdateTabsCaptionTruncation should be enough
  10877. SessionListChanged(true);
  10878. }
  10879. }
  10880. //---------------------------------------------------------------------------
  10881. UnicodeString TCustomScpExplorerForm::GetSessionPath(TManagedTerminal * ASession, TOperationSide Side)
  10882. {
  10883. if (ASession == ManagedSession)
  10884. {
  10885. UpdateSession(ASession);
  10886. }
  10887. TSessionData * Data = ASession->StateData;
  10888. UnicodeString Result;
  10889. if (Side == osLocal)
  10890. {
  10891. Result = Data->LocalDirectory;
  10892. }
  10893. else if (DebugAlwaysTrue(Side == osOther))
  10894. {
  10895. Result = ASession->LocalBrowser ? Data->OtherLocalDirectory : Data->RemoteDirectory;
  10896. }
  10897. return Result;
  10898. }
  10899. //---------------------------------------------------------------------------
  10900. UnicodeString TCustomScpExplorerForm::GetTabHintDetails(TManagedTerminal * ASession)
  10901. {
  10902. UnicodeString Result;
  10903. if (!ASession->Active)
  10904. {
  10905. Result = LoadStr(STATUS_NOT_CONNECTED2);
  10906. }
  10907. else
  10908. {
  10909. Result = GetSessionPath(ASession, osRemote);
  10910. }
  10911. return Result;
  10912. }
  10913. //---------------------------------------------------------------------------
  10914. UnicodeString TCustomScpExplorerForm::GetNewTabHintDetails()
  10915. {
  10916. return LoadStr(NEW_REMOTE_TAB_HINT);
  10917. }
  10918. //---------------------------------------------------------------------------
  10919. void __fastcall TCustomScpExplorerForm::SessionsPageControlTabHint(TPageControl *, int Index, UnicodeString & Hint)
  10920. {
  10921. if (Index >= 0)
  10922. {
  10923. TThemeTabSheet * TabSheet = SessionsPageControl->Pages[Index];
  10924. TManagedTerminal * TabSession = GetSessionTabSession(TabSheet);
  10925. if (TabSession == NULL)
  10926. {
  10927. UnicodeString NewTabTabCaption = GetNewTabTabCaption();
  10928. if (NewTabTabCaption != TabSheet->Caption)
  10929. {
  10930. Hint = NewTabTabCaption + L"|" + GetNewTabHintDetails();
  10931. }
  10932. }
  10933. else
  10934. {
  10935. TTerminalManager * Manager = TTerminalManager::Instance();
  10936. UnicodeString TabCaption = Manager->GetSessionTitle(TabSession, true);
  10937. Hint = TabCaption + L"|" + GetTabHintDetails(TabSession);
  10938. }
  10939. }
  10940. }
  10941. //---------------------------------------------------------------------------
  10942. typedef std::vector<TListItem *> TListItemsVector;
  10943. struct TCalculateSizeOperation
  10944. {
  10945. TCalculateSizeStats Stats;
  10946. TListItemsVector ListItems;
  10947. size_t Index;
  10948. };
  10949. //---------------------------------------------------------------------------
  10950. void TCustomScpExplorerForm::CalculateDirectorySizes()
  10951. {
  10952. TCalculateSizeOperation CalculateSizeOperation;
  10953. TOperationSide Side = GetSide(osCurrent);
  10954. TCustomDirView * ADirView = DirView(Side);
  10955. bool FullPath = IsSideLocalBrowser(Side);
  10956. std::unique_ptr<TStrings> AllFileList(ADirView->CreateFileList(false, FullPath, NULL, true));
  10957. std::unique_ptr<TStrings> FileList(new TStringList());
  10958. for (int Index = 0; Index < AllFileList->Count; Index++)
  10959. {
  10960. TListItem * Item = DebugNotNull(dynamic_cast<TListItem *>(AllFileList->Objects[Index]));
  10961. if (ADirView->ItemIsDirectory(Item))
  10962. {
  10963. FileList->AddObject(AllFileList->Strings[Index], ADirView->ItemData(Item));
  10964. CalculateSizeOperation.ListItems.push_back(Item);
  10965. }
  10966. }
  10967. CalculateSizeOperation.Index = 0;
  10968. __int64 Size = 0;
  10969. TValueRestorer<TCalculateSizeOperation *> DirectorySizeOperationRestorer(FCalculateSizeOperation);
  10970. FCalculateSizeOperation = &CalculateSizeOperation;
  10971. TValueRestorer<TFileOperationFinishedEvent> OnFileOperationFinishedRestorer(FOnFileOperationFinished);
  10972. FOnFileOperationFinished = DirectorySizeCalculated;
  10973. TValueRestorer<TFileOperation> PrimaryOperationRestorer(FPrimaryOperation);
  10974. FPrimaryOperation = foCalculateSize;
  10975. TCalculatedSizes CalculatedSizes;
  10976. CalculateSizeOperation.Stats.CalculatedSizes = &CalculatedSizes;
  10977. if (Side == osLocal)
  10978. {
  10979. Terminal->CalculateLocalFilesSize(FileList.get(), Size, NULL, true, NULL, &CalculatedSizes);
  10980. }
  10981. else
  10982. {
  10983. TCalculateSizeParams Params;
  10984. Params.Stats = &CalculateSizeOperation.Stats;
  10985. Params.UseCache = true;
  10986. Terminal->CalculateFilesSize(FileList.get(), Size, Params);
  10987. }
  10988. }
  10989. //---------------------------------------------------------------------------
  10990. void __fastcall TCustomScpExplorerForm::DirectorySizeCalculated(TOperationSide Side, const UnicodeString & FileName, bool)
  10991. {
  10992. DebugAlwaysTrue(FCalculateSizeOperation != NULL);
  10993. TListItem * Item = VisualiseOperationFinished(Side, FileName, false);
  10994. TCustomDirView * ADirView = dynamic_cast<TCustomDirView *>(Item->Owner->Owner);
  10995. // optimization
  10996. bool GotExpectedItem =
  10997. (FCalculateSizeOperation->Index < FCalculateSizeOperation->ListItems.size()) &&
  10998. (FCalculateSizeOperation->ListItems[FCalculateSizeOperation->Index] == Item);
  10999. int Index = -1;
  11000. if (DebugAlwaysTrue(GotExpectedItem))
  11001. {
  11002. Index = FCalculateSizeOperation->Index;
  11003. FCalculateSizeOperation->Index++;
  11004. }
  11005. else
  11006. {
  11007. TListItemsVector::iterator I =
  11008. std::find(FCalculateSizeOperation->ListItems.begin(), FCalculateSizeOperation->ListItems.end(), Item);
  11009. if (DebugAlwaysTrue(I != FCalculateSizeOperation->ListItems.end()))
  11010. {
  11011. Index = I - FCalculateSizeOperation->ListItems.begin();
  11012. }
  11013. }
  11014. if (DebugAlwaysTrue(Index >= 0))
  11015. {
  11016. __int64 Size = (*FCalculateSizeOperation->Stats.CalculatedSizes)[Index];
  11017. ADirView->SetItemCalculatedSize(Item, Size);
  11018. }
  11019. }
  11020. //---------------------------------------------------------------------------
  11021. void __fastcall TCustomScpExplorerForm::MessageDockRequestDock(TObject *, TTBCustomDockableWindow *, bool & Accept)
  11022. {
  11023. Accept = false;
  11024. }
  11025. //---------------------------------------------------------------------------