CustomScpExplorer.cpp 405 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108
  1. //---------------------------------------------------------------------------
  2. #define NO_WIN32_LEAN_AND_MEAN
  3. #include <vcl.h>
  4. #pragma hdrstop
  5. #include <Common.h>
  6. #include "CustomScpExplorer.h"
  7. #include <algorithm>
  8. #include <Bookmarks.h>
  9. #include <Interface.h>
  10. #include <Exceptions.h>
  11. #include <CoreMain.h>
  12. #include <FileSystems.h>
  13. #include <TextsCore.h>
  14. #include <TextsWin.h>
  15. #include <HelpWin.h>
  16. #include <VCLCommon.h>
  17. #include <Progress.h>
  18. #include <SynchronizeProgress.h>
  19. #include <DragExt.h>
  20. #include <WinApi.h>
  21. #include "GUITools.h"
  22. #include "NonVisual.h"
  23. #include "Glyphs.h"
  24. #include "Tools.h"
  25. #include "WinConfiguration.h"
  26. #include "TerminalManager.h"
  27. #include "EditorManager.h"
  28. #include "ProgParams.h"
  29. #include "Setup.h"
  30. #include <Consts.hpp>
  31. #include <DateUtils.hpp>
  32. #include <TB2Common.hpp>
  33. #include <DirectoryMonitor.hpp>
  34. #include <System.IOUtils.hpp>
  35. //---------------------------------------------------------------------------
  36. #pragma package(smart_init)
  37. #pragma link "CustomDirView"
  38. #pragma link "CustomUnixDirView"
  39. #pragma link "IEListView"
  40. #pragma link "NortonLikeListView"
  41. #pragma link "UnixDirView"
  42. #pragma link "CustomDriveView"
  43. #pragma link "UnixDriveView"
  44. #pragma link "CustomDriveView"
  45. #pragma link "TB2Dock"
  46. #pragma link "TBXStatusBars"
  47. #pragma link "TB2Item"
  48. #pragma link "TB2Toolbar"
  49. #pragma link "ThemePageControl"
  50. #pragma link "PathLabel"
  51. #pragma resource "*.dfm"
  52. //---------------------------------------------------------------------------
  53. #define WM_COMPONENT_HIDE (WM_WINSCP_USER + 4)
  54. #define WM_PASTE_FILES (WM_WINSCP_USER + 14)
  55. static const int SessionPanelCount = 3;
  56. //---------------------------------------------------------------------------
  57. class TMutexGuard
  58. {
  59. public:
  60. TMutexGuard(HANDLE AMutex, int Message = MUTEX_RELEASE_TIMEOUT,
  61. int Timeout = 5000)
  62. {
  63. FMutex = NULL;
  64. unsigned long WaitResult = WaitForSingleObject(AMutex, Timeout);
  65. if (WaitResult == WAIT_TIMEOUT)
  66. {
  67. throw Exception(LoadStr(MUTEX_RELEASE_TIMEOUT));
  68. }
  69. else
  70. {
  71. FMutex = AMutex;
  72. }
  73. }
  74. ~TMutexGuard()
  75. {
  76. if (FMutex != NULL)
  77. {
  78. ReleaseMutex(FMutex);
  79. }
  80. }
  81. private:
  82. HANDLE FMutex;
  83. };
  84. //---------------------------------------------------------------------------
  85. class TWindowLock
  86. {
  87. public:
  88. TWindowLock(TCustomScpExplorerForm * Form) :
  89. FForm(Form)
  90. {
  91. FForm->LockWindow();
  92. }
  93. ~TWindowLock()
  94. {
  95. FForm->UnlockWindow();
  96. }
  97. private:
  98. TCustomScpExplorerForm * FForm;
  99. };
  100. //---------------------------------------------------------------------------
  101. TAutoBatch::TAutoBatch(TCustomScpExplorerForm * Form) :
  102. FForm(Form)
  103. {
  104. FForm->BatchStart(FBatchStorage);
  105. }
  106. //---------------------------------------------------------------------------
  107. TAutoBatch::~TAutoBatch()
  108. {
  109. FForm->BatchEnd(FBatchStorage);
  110. }
  111. //---------------------------------------------------------------------------
  112. struct TTransferOperationParam
  113. {
  114. TTransferOperationParam();
  115. UnicodeString TargetDirectory;
  116. bool Temp;
  117. bool DragDrop;
  118. int Options;
  119. TAutoSwitch Queue;
  120. };
  121. //---------------------------------------------------------------------------
  122. TTransferOperationParam::TTransferOperationParam()
  123. {
  124. Temp = false;
  125. DragDrop = false;
  126. Options = 0;
  127. Queue = asAuto;
  128. }
  129. //---------------------------------------------------------------------------
  130. class TTransferPresetNoteData : public TObject
  131. {
  132. public:
  133. UnicodeString Message;
  134. };
  135. //---------------------------------------------------------------------------
  136. class TTerminalNoteData : public TObject
  137. {
  138. public:
  139. TManagedTerminal * Terminal;
  140. };
  141. //---------------------------------------------------------------------------
  142. __fastcall TCustomScpExplorerForm::TCustomScpExplorerForm(TComponent* Owner):
  143. FFormRestored(false),
  144. TForm(Owner)
  145. {
  146. AddStartupSequence(L"F");
  147. FInvalid = false;
  148. FCurrentSide = osRemote;
  149. FEverShown = false;
  150. FDocks = new TList();
  151. InitControls();
  152. DebugAssert(NonVisualDataModule && !NonVisualDataModule->ScpExplorer);
  153. // So that UpdateCustomCommandsToolbar called from RestoreParams works
  154. NonVisualDataModule->ScpExplorer = this;
  155. RestoreParams();
  156. ConfigurationChanged();
  157. RemoteDirView->Invalidate();
  158. FAutoOperation = false;
  159. FOnFileOperationFinished = NULL;
  160. FPrimaryOperation = foNone;
  161. FForceExecution = false;
  162. FIgnoreNextDialogChar = 0;
  163. FErrorList = NULL;
  164. FSynchronizeProgressForm = NULL;
  165. FProgressForm = NULL;
  166. FRefreshLocalDirectory = false;
  167. FRefreshRemoteDirectory = false;
  168. FDDExtMapFile = NULL;
  169. // CreateMutexW keeps failing with ERROR_NOACCESS
  170. FDDExtMutex = CreateMutexA(NULL, false, AnsiString(DRAG_EXT_MUTEX).c_str());
  171. DebugAssert(FDDExtMutex != NULL);
  172. FDDTargetControl = NULL;
  173. FDelayedDeletionTimer = NULL;
  174. FDelayedDeletionList = new TStringList();
  175. FDDFileList = NULL;
  176. FPendingTempSpaceWarn = false;
  177. FCapturedLog = NULL;
  178. FDragDropOperation = false;
  179. memset(&FHistoryMenu, 0, sizeof(FHistoryMenu));
  180. FAllowTransferPresetAutoSelect = true;
  181. FSessionChanging = false;
  182. FCopyParamDefault = L"";
  183. FSynchronizeController = NULL;
  184. FPendingQueueActionItem = NULL;
  185. FLockLevel = 0;
  186. FLockSuspendLevel = 0;
  187. FDisabledOnLockSuspend = false;
  188. FAlternativeDelete = false;
  189. FTrayIcon = new ::TTrayIcon(0);
  190. FTrayIcon->OnClick = TrayIconClick;
  191. FMaxQueueLength = 0;
  192. FLastContextPopupScreenPoint = TPoint(-1, -1);
  193. FTransferResumeList = NULL;
  194. FDeletedFiles = NULL;
  195. FMoveToQueue = false;
  196. StandaloneOperation = false;
  197. FOnFeedSynchronizeError = NULL;
  198. FOnSynchronizeAbort = NULL;
  199. FSynchronizeTerminal = NULL;
  200. FNeedSession = false;
  201. FDoNotIdleCurrentTerminal = 0;
  202. FIncrementalSearching = 0;
  203. FIncrementalSearchHaveNext = false;
  204. FQueueFileList.reset(new TQueueFileList());
  205. FProgressSide = osCurrent;
  206. FCalculateSizeOperation = NULL;
  207. FDownloadingFromClipboard = false;
  208. FClipboardFakeMonitorsPendingReset = false;
  209. FHiContrastTheme = NULL;
  210. FEditorManager = new TEditorManager();
  211. FEditorManager->OnFileChange = ExecutedFileChanged;
  212. FEditorManager->OnFileReload = ExecutedFileReload;
  213. FEditorManager->OnFileEarlyClosed = ExecutedFileEarlyClosed;
  214. FEditorManager->OnFileUploadComplete = ExecutedFileUploadComplete;
  215. FLocalEditors = new TList();
  216. FQueueStatus = NULL;
  217. FQueueStatusSection = new TCriticalSection();
  218. FQueueStatusInvalidated = false;
  219. FQueueItemInvalidated = false;
  220. FQueueStatusUpdating = false;
  221. FQueueActedItem = NULL;
  222. FQueueController = new TQueueController(QueueView3);
  223. FQueueScrollOnDragOver = new TListViewScrollOnDragOver(QueueView3, true);
  224. FUserActionTimer = new TTimer(this);
  225. FUserActionTimer->Enabled = false;
  226. FUserActionTimer->Interval = 10;
  227. FUserActionTimer->OnTimer = UserActionTimer;
  228. FNotes = new TStringList();
  229. FNoteTimer = new TTimer(this);
  230. FNoteTimer->Enabled = false;
  231. FNoteTimer->OnTimer = NoteTimer;
  232. FOnNoteClick = NULL;
  233. FOle32Library = LoadLibrary(L"Ole32.dll");
  234. FDragMoveCursor = FOle32Library != NULL ?
  235. LoadCursor(FOle32Library, MAKEINTRESOURCE(2)) : NULL;
  236. UseSystemSettings(this);
  237. TTBXStringList * TransferList = dynamic_cast<TTBXStringList*>(
  238. static_cast<TObject*>(GetComponent(fcTransferList)));
  239. DebugAssert(TransferList != NULL);
  240. FTransferListHoverIndex = -1;
  241. TransferList->OnChange = TransferListChange;
  242. TransferList->OnDrawItem = TransferListDrawItem;
  243. SetSubmenu(dynamic_cast<TTBXCustomItem *>(static_cast<TObject *>(GetComponent(fcColorMenu))), true);
  244. SetSubmenu(NonVisualDataModule->ColorMenuItem, true);
  245. UseDesktopFont(SessionsPageControl);
  246. UpdateSessionsPageControlHeight();
  247. UseDesktopFont(RemoteDirView);
  248. UseDesktopFont(RemoteDriveView);
  249. UseDesktopFont(QueueView3);
  250. UseDesktopFont(QueueFileList);
  251. UseDesktopFont(QueueLabel);
  252. UseDesktopFont(RemoteStatusBar);
  253. reinterpret_cast<TLabel*>(QueueSplitter)->OnDblClick = QueueSplitterDblClick;
  254. QueueSplitter->ShowHint = true;
  255. reinterpret_cast<TLabel*>(QueueFileListSplitter)->OnDblClick = QueueFileListSplitterDblClick;
  256. QueueFileListSplitter->ShowHint = true;
  257. RemotePanelSplitter->ShowHint = true;
  258. UpdateImages();
  259. FCustomCommandMenu = CreateTBXPopupMenu(this);
  260. FCustomCommandLocalFileList = NULL;
  261. FCustomCommandRemoteFileList = NULL;
  262. FSessionColors = new TPngImageList(this);
  263. FSessionColors->ColorDepth = cd32Bit;
  264. AddFixedSessionImages();
  265. SessionsPageControl->Images = FSessionColors;
  266. UpdateQueueLabel();
  267. CheckStoreTransition();
  268. CreateHiddenWindow();
  269. StartUpdates();
  270. // Start loading drive data, so that they are ready once we need them for StartCreationDirectoryMonitorsOnEachDrive
  271. // (necessary for "explorer" only, as "commander" loads it for its drive menu)
  272. DriveInfo->NeedData();
  273. }
  274. //---------------------------------------------------------------------------
  275. __fastcall TCustomScpExplorerForm::~TCustomScpExplorerForm()
  276. {
  277. FInvalid = true;
  278. ReleaseHiContrastTheme();
  279. if (FClipboardTerminal != NULL)
  280. {
  281. ClipboardClear(); // implies ClipboardStop
  282. }
  283. // this has to be one of the very first things to do
  284. StopUpdateThread();
  285. // This is needed when shuting down Windows only. Otherwise it's already set NULL from Execute()
  286. TTerminalManager::Instance()->ScpExplorer = NULL;
  287. delete FCustomCommandLocalFileList;
  288. delete FCustomCommandRemoteFileList;
  289. delete FCustomCommandMenu;
  290. delete FTrayIcon;
  291. FTrayIcon = NULL;
  292. FEditorManager->CloseInternalEditors(ForceCloseInternalEditor);
  293. delete FEditorManager;
  294. ForceCloseLocalEditors();
  295. delete FLocalEditors;
  296. HideFileFindDialog();
  297. if (FDelayedDeletionTimer)
  298. {
  299. DoDelayedDeletion(NULL);
  300. SAFE_DESTROY(FDelayedDeletionTimer);
  301. }
  302. SAFE_DESTROY(FDelayedDeletionList);
  303. // sometimes we do not get DDEnd so the list is not released
  304. SAFE_DESTROY(FDDFileList);
  305. DebugAssert(FSynchronizeController == NULL);
  306. CloseHandle(FDDExtMutex);
  307. FDDExtMutex = NULL;
  308. FreeLibrary(FOle32Library);
  309. FOle32Library = NULL;
  310. FDragMoveCursor = NULL;
  311. DebugAssert(!FErrorList);
  312. if (FEverShown)
  313. {
  314. // when window is never shown (like when running command-line operation),
  315. // particularly window site is not restored correctly (BoundsRect value set
  316. // in RestoreForm gets lost during handle allocation), so we do not want
  317. // it to be stored
  318. StoreParams();
  319. }
  320. ManagedSession = NULL;
  321. Queue = NULL;
  322. DebugAssert(NonVisualDataModule && (NonVisualDataModule->ScpExplorer == this));
  323. NonVisualDataModule->ScpExplorer = NULL;
  324. delete FQueueScrollOnDragOver;
  325. FQueueScrollOnDragOver = NULL;
  326. delete FQueueController;
  327. FQueueController = NULL;
  328. delete FQueueStatusSection;
  329. FQueueStatusSection = NULL;
  330. delete FQueueStatus;
  331. FQueueStatus = NULL;
  332. delete FUserActionTimer;
  333. FUserActionTimer = NULL;
  334. delete FNoteTimer;
  335. delete FNotes;
  336. delete FNoteData;
  337. SAFE_DESTROY(FDocks);
  338. SessionsPageControl->Images = NULL;
  339. // only after clearing Terminal (after SessionListChanged => ... => AddSessionColor is called)
  340. SAFE_DESTROY(FSessionColors);
  341. SAFE_DESTROY(FSessionsDragDropFilesEx);
  342. SAFE_DESTROY(FHistoryMenu[0][0]);
  343. SAFE_DESTROY(FHistoryMenu[0][1]);
  344. SAFE_DESTROY(FHistoryMenu[1][0]);
  345. SAFE_DESTROY(FHistoryMenu[1][1]);
  346. if (FHiddenWindow != NULL)
  347. {
  348. DestroyWindow(FHiddenWindow);
  349. FHiddenWindow = NULL;
  350. }
  351. }
  352. //---------------------------------------------------------------------------
  353. void TCustomScpExplorerForm::ReleaseHiContrastTheme()
  354. {
  355. if (FHiContrastTheme != NULL)
  356. {
  357. ReleaseTBXTheme(FHiContrastTheme);
  358. FHiContrastTheme = NULL;
  359. }
  360. }
  361. //---------------------------------------------------------------------------
  362. void __fastcall TCustomScpExplorerForm::RefreshPanel(const UnicodeString & Session, const UnicodeString & Path)
  363. {
  364. std::unique_ptr<TSessionData> Data;
  365. if (!Session.IsEmpty())
  366. {
  367. bool DefaultsOnly;
  368. Data.reset(StoredSessions->ParseUrl(Session, NULL, DefaultsOnly));
  369. }
  370. TTerminalManager * Manager = TTerminalManager::Instance();
  371. for (int Index = 0; Index < Manager->Count; Index++)
  372. {
  373. TTerminal * Terminal = Manager->Sessions[Index];
  374. if (Session.IsEmpty() ||
  375. Manager->IsActiveTerminalForSite(Terminal, Data.get()))
  376. {
  377. if (Path.IsEmpty())
  378. {
  379. Terminal->ClearCaches();
  380. }
  381. else
  382. {
  383. Terminal->DirectoryModified(Path, true);
  384. }
  385. }
  386. }
  387. // We should flag a pending refresh for the background terminals or busy foreground terminals
  388. if (HasActiveTerminal() &&
  389. CanCommandLineFromAnotherInstance() &&
  390. (Session.IsEmpty() || Manager->IsActiveTerminalForSite(Terminal, Data.get())) &&
  391. (Path.IsEmpty() || UnixIsChildPath(Path, Terminal->CurrentDirectory)))
  392. {
  393. Terminal->ReloadDirectory();
  394. }
  395. }
  396. //---------------------------------------------------------------------------
  397. void __fastcall TCustomScpExplorerForm::WMCopyData(TMessage & Message)
  398. {
  399. PCOPYDATASTRUCT CopyData = reinterpret_cast<PCOPYDATASTRUCT>(Message.LParam);
  400. AppLogFmt(L"Received copy data message with size %d", (int(CopyData->cbData)));
  401. size_t MessageSize = sizeof(TCopyDataMessage);
  402. bool Result = DebugAlwaysTrue(CopyData->cbData == MessageSize);
  403. if (Result)
  404. {
  405. const TCopyDataMessage & Message = *reinterpret_cast<const TCopyDataMessage *>(CopyData->lpData);
  406. Result = (Message.Version == TCopyDataMessage::Version1);
  407. if (Result)
  408. {
  409. switch (Message.Command)
  410. {
  411. case TCopyDataMessage::CommandCanCommandLine:
  412. Result = CanCommandLineFromAnotherInstance();
  413. if (Result)
  414. {
  415. AppLog(L"Can process command-line from other instance");
  416. }
  417. else
  418. {
  419. AppLog(L"Cannot process command-line from other instance");
  420. }
  421. break;
  422. case TCopyDataMessage::CommandCommandLine:
  423. {
  424. UnicodeString CommandLine(Message.CommandLine);
  425. Result = CommandLineFromAnotherInstance(CommandLine);
  426. }
  427. break;
  428. case TCopyDataMessage::RefreshPanel:
  429. RefreshPanel(Message.Refresh.Session, Message.Refresh.Path);
  430. break;
  431. case TCopyDataMessage::MainWindowCheck:
  432. Result = true;
  433. break;
  434. default:
  435. Result = false;
  436. break;
  437. }
  438. }
  439. }
  440. Message.Result = Result ? 1 : 0;
  441. }
  442. //---------------------------------------------------------------------------
  443. void __fastcall TCustomScpExplorerForm::CreateHiddenWindow()
  444. {
  445. WNDCLASS WindowClass = {0};
  446. WindowClass.lpfnWndProc = DefWindowProc;
  447. WindowClass.hInstance = HInstance;
  448. WindowClass.lpszClassName = HIDDEN_WINDOW_NAME;
  449. FHiddenWindow = NULL;
  450. if (RegisterClass(&WindowClass))
  451. {
  452. FHiddenWindow = CreateWindow(HIDDEN_WINDOW_NAME, L"",
  453. WS_POPUP, 0, 0, 0, 0, 0, 0, HInstance, NULL);
  454. }
  455. }
  456. //---------------------------------------------------------------------------
  457. bool __fastcall TCustomScpExplorerForm::CanConsole()
  458. {
  459. return
  460. HasActiveTerminal() &&
  461. (Terminal->IsCapable[fcAnyCommand] || Terminal->IsCapable[fcSecondaryShell]);
  462. }
  463. //---------------------------------------------------------------------------
  464. bool __fastcall TCustomScpExplorerForm::CanCommandLineFromAnotherInstance()
  465. {
  466. bool Result = Visible && !NonVisualDataModule->Busy;
  467. return Result;
  468. }
  469. //---------------------------------------------------------------------------
  470. bool __fastcall TCustomScpExplorerForm::CommandLineFromAnotherInstance(
  471. const UnicodeString & CommandLine)
  472. {
  473. TProgramParams Params(CommandLine);
  474. bool Result = CanCommandLineFromAnotherInstance() && DebugAlwaysTrue(Params.ParamCount > 0);
  475. if (Result)
  476. {
  477. NonVisualDataModule->StartBusy();
  478. try
  479. {
  480. // this action is initiated from another process,
  481. // so it's likely that our window is not visible,
  482. // and user won't see what is going on
  483. Application->BringToFront();
  484. // reload sessions as we may be asked to open a session
  485. // just stored by another instance
  486. StoredSessions->Reload();
  487. UnicodeString SessionName = Params.Param[1];
  488. std::unique_ptr<TObjectList> DataList(new TObjectList());
  489. try
  490. {
  491. UnicodeString DownloadFile; // unused
  492. int Flags = GetCommandLineParseUrlFlags(&Params);
  493. GetLoginData(SessionName, &Params, DataList.get(), DownloadFile, true, this, Flags);
  494. }
  495. catch (EAbort &)
  496. {
  497. // Happens when opening session in PuTTY and in other situations,
  498. // in which we do not want to fail WM_COPYDATA, as that would cause master instance
  499. // to process the command-line on its own (or to pass it to yet another instance)
  500. }
  501. if (DataList->Count > 0)
  502. {
  503. TTerminalManager * Manager = TTerminalManager::Instance();
  504. Manager->ActiveSession = Manager->NewSessions(DataList.get());
  505. }
  506. }
  507. __finally
  508. {
  509. NonVisualDataModule->EndBusy();
  510. }
  511. }
  512. return Result;
  513. }
  514. //---------------------------------------------------------------------------
  515. TManagedTerminal * TCustomScpExplorerForm::GetTerminal()
  516. {
  517. TManagedTerminal * Result;
  518. if ((FManagedSession != NULL) && !FManagedSession->LocalBrowser)
  519. {
  520. Result = FManagedSession;
  521. }
  522. else
  523. {
  524. Result = NULL;
  525. }
  526. return Result;
  527. }
  528. //---------------------------------------------------------------------------
  529. void __fastcall TCustomScpExplorerForm::SetManagedSession(TManagedTerminal * value)
  530. {
  531. if (FManagedSession != value)
  532. {
  533. TWindowLock Lock(this);
  534. SessionChanging();
  535. DoSetManagedSession(value, false);
  536. }
  537. }
  538. //---------------------------------------------------------------------------
  539. void __fastcall TCustomScpExplorerForm::DoSetManagedSession(TManagedTerminal * value, bool Replace)
  540. {
  541. DebugAssert(!Replace || ((value != NULL) && !value->LocalBrowser));
  542. FManagedSession = value;
  543. {
  544. TValueRestorer<bool> AllowTransferPresetAutoSelectRestorer(FAllowTransferPresetAutoSelect, false);
  545. TAutoFlag SessionChangingFlag(FSessionChanging);
  546. SessionChanged(Replace);
  547. }
  548. if (Terminal != NULL)
  549. {
  550. TransferPresetAutoSelect();
  551. }
  552. // Update app and tab titles, prevented above by FSessionChanging.
  553. UpdateControls();
  554. }
  555. //---------------------------------------------------------------------------
  556. void __fastcall TCustomScpExplorerForm::ReplaceTerminal(TManagedTerminal * value)
  557. {
  558. FEditorManager->ProcessFiles(FileTerminalReplaced, value);
  559. DoSetManagedSession(value, true);
  560. }
  561. //---------------------------------------------------------------------------
  562. void __fastcall TCustomScpExplorerForm::SessionChanging()
  563. {
  564. if (ManagedSession != NULL)
  565. {
  566. UpdateSession(ManagedSession);
  567. }
  568. }
  569. //---------------------------------------------------------------------------
  570. void __fastcall TCustomScpExplorerForm::SessionChanged(bool Replaced)
  571. {
  572. if (ManagedSession != NULL)
  573. {
  574. UpdateSessionColor(static_cast<TColor>(ManagedSession->StateData->Color));
  575. }
  576. if (!TTerminalManager::Instance()->Updating)
  577. {
  578. SessionListChanged();
  579. }
  580. if (Terminal != NULL)
  581. {
  582. TObject * State = NULL;
  583. if (WinConfiguration->PreservePanelState)
  584. {
  585. State = Terminal->RemoteExplorerState;
  586. DirView(osRemote)->AnnounceState(State);
  587. }
  588. try
  589. {
  590. if (Replaced)
  591. {
  592. RemoteDirView->ReplaceTerminal(Terminal);
  593. }
  594. else
  595. {
  596. RemoteDirView->Terminal = Terminal;
  597. }
  598. if (Terminal->Active)
  599. {
  600. Terminal->RefreshDirectory();
  601. }
  602. if (State != NULL)
  603. {
  604. DirView(osRemote)->RestoreState(State);
  605. }
  606. }
  607. __finally
  608. {
  609. DirView(osRemote)->AnnounceState(NULL);
  610. }
  611. }
  612. else
  613. {
  614. RemoteDirView->Terminal = NULL;
  615. }
  616. InitStatusBar();
  617. UpdateTransferList();
  618. // Update panels Enable state before refreshing the labels
  619. UpdateControls();
  620. }
  621. //---------------------------------------------------------------------------
  622. void __fastcall TCustomScpExplorerForm::SetQueue(TTerminalQueue * value)
  623. {
  624. if (Queue != value)
  625. {
  626. if (FQueue != NULL)
  627. {
  628. FQueue->OnListUpdate = NULL;
  629. FQueue->OnQueueItemUpdate = NULL;
  630. }
  631. FQueue = value;
  632. if (FQueue != NULL)
  633. {
  634. DebugAssert(FQueue->OnListUpdate == NULL);
  635. FQueue->OnListUpdate = QueueListUpdate;
  636. DebugAssert(FQueue->OnQueueItemUpdate == NULL);
  637. FQueue->OnQueueItemUpdate = QueueItemUpdate;
  638. }
  639. QueueChanged();
  640. }
  641. }
  642. //---------------------------------------------------------------------------
  643. void __fastcall TCustomScpExplorerForm::QueueView3Deletion(TObject * /*Sender*/,
  644. TListItem * Item)
  645. {
  646. if (FQueueActedItem == Item)
  647. {
  648. FQueueActedItem = NULL;
  649. if ((QueueView3->PopupMenu != NULL) &&
  650. (QueueView3->PopupMenu->PopupComponent == QueueView3))
  651. {
  652. // rather "trick", suggested by Jordan on jrsoftware.toolbar2000
  653. ReleaseCapture();
  654. }
  655. }
  656. if (Item->Data == FPendingQueueActionItem)
  657. {
  658. FPendingQueueActionItem = NULL;
  659. }
  660. }
  661. //---------------------------------------------------------------------------
  662. bool TCustomScpExplorerForm::IsAnythingQueued()
  663. {
  664. return (FQueueStatus != NULL) && (FQueueStatus->ActiveAndPendingPrimaryCount > 0);
  665. }
  666. //---------------------------------------------------------------------------
  667. void __fastcall TCustomScpExplorerForm::UpdateQueueStatus(bool QueueChanging)
  668. {
  669. {
  670. TGuard Guard(FQueueStatusSection);
  671. FQueueStatusInvalidated = false;
  672. if (FQueue != NULL)
  673. {
  674. FQueueStatus = FQueue->CreateStatus(FQueueStatus);
  675. }
  676. }
  677. if ((FQueueStatus != NULL) && (FQueueStatus->Count > FMaxQueueLength))
  678. {
  679. FMaxQueueLength = FQueueStatus->Count;
  680. Configuration->Usage->SetMax(L"MaxQueueLength", FMaxQueueLength);
  681. }
  682. {
  683. TAutoFlag Flag(FQueueStatusUpdating);
  684. FQueueController->UpdateQueueStatus(FQueueStatus);
  685. }
  686. UpdateQueueFileList();
  687. SetQueueProgress();
  688. UpdateQueueView();
  689. bool IsEmpty = !IsAnythingQueued();
  690. if (IsEmpty && (Terminal != NULL))
  691. {
  692. TOnceDoneOperation OnceDoneOperation =
  693. NonVisualDataModule->CurrentQueueOnceEmptyOperation();
  694. NonVisualDataModule->ResetQueueOnceEmptyOperation();
  695. if ((FQueue != NULL) && !WinConfiguration->EnableQueueByDefault && !QueueChanging)
  696. {
  697. FQueue->Enabled = false;
  698. }
  699. if ((OnceDoneOperation != odoIdle) && !NonVisualDataModule->Busy)
  700. {
  701. Terminal->CloseOnCompletion(OnceDoneOperation, LoadStr(CLOSED_ON_QUEUE_EMPTY));
  702. }
  703. }
  704. }
  705. //---------------------------------------------------------------------------
  706. UnicodeString __fastcall TCustomScpExplorerForm::GetQueueProgressTitle()
  707. {
  708. UnicodeString Result;
  709. if (FQueueStatus != NULL)
  710. {
  711. if (FQueueStatus->IsOnlyOneActiveAndNoPending())
  712. {
  713. TFileOperationProgressType * ProgressData =
  714. FQueueStatus->Items[FQueueStatus->DoneCount]->ProgressData;
  715. if ((ProgressData != NULL) && ProgressData->InProgress)
  716. {
  717. Result = TProgressForm::ProgressStr(NULL, ProgressData);
  718. }
  719. }
  720. else if (FQueueStatus->ActiveAndPendingPrimaryCount > 1)
  721. {
  722. Result = FMTLOAD(PROGRESS_IN_QUEUE, (FQueueStatus->ActiveAndPendingPrimaryCount));
  723. }
  724. }
  725. return Result;
  726. }
  727. //---------------------------------------------------------------------------
  728. void __fastcall TCustomScpExplorerForm::UpdateQueueView()
  729. {
  730. ComponentVisible[fcQueueView] =
  731. !IsLocalBrowserMode() &&
  732. ((WinConfiguration->QueueView.Show == qvShow) ||
  733. ((WinConfiguration->QueueView.Show == qvHideWhenEmpty) &&
  734. (FQueueStatus != NULL) && (FQueueStatus->Count > 0)));
  735. }
  736. //---------------------------------------------------------------------------
  737. void __fastcall TCustomScpExplorerForm::QueueChanged()
  738. {
  739. if (FQueueStatus != NULL)
  740. {
  741. // UpdateFileList implementation relies on the status being recreated when session changes
  742. delete FQueueStatus;
  743. FQueueStatus = NULL;
  744. }
  745. UpdateQueueStatus(true);
  746. }
  747. //---------------------------------------------------------------------------
  748. void __fastcall TCustomScpExplorerForm::QueueListUpdate(TTerminalQueue * Queue)
  749. {
  750. if (FQueue == Queue)
  751. {
  752. FQueueStatusInvalidated = true;
  753. }
  754. }
  755. //---------------------------------------------------------------------------
  756. void __fastcall TCustomScpExplorerForm::QueueItemUpdate(TTerminalQueue * Queue,
  757. TQueueItem * Item)
  758. {
  759. if (FQueue == Queue)
  760. {
  761. TGuard Guard(FQueueStatusSection);
  762. // this may be running in parallel with QueueChanged
  763. if (FQueueStatus != NULL)
  764. {
  765. TQueueItemProxy * QueueItem = FQueueStatus->FindByQueueItem(Item);
  766. if ((Item->Status == TQueueItem::qsDone) && (Terminal != NULL))
  767. {
  768. FRefreshLocalDirectory = (QueueItem == NULL) ||
  769. (!QueueItem->Info->ModifiedLocal.IsEmpty());
  770. FRefreshRemoteDirectory = (QueueItem == NULL) ||
  771. (!QueueItem->Info->ModifiedRemote.IsEmpty());
  772. }
  773. if (QueueItem != NULL)
  774. {
  775. QueueItem->UserData = (void*)true;
  776. FQueueItemInvalidated = true;
  777. }
  778. }
  779. }
  780. }
  781. //---------------------------------------------------------------------------
  782. bool __fastcall TCustomScpExplorerForm::IsQueueAutoPopup()
  783. {
  784. // during standalone editing, we have no way to see/control queue,
  785. // so we have to always popup prompts automatically
  786. return StandaloneOperation || GUIConfiguration->QueueAutoPopup;
  787. }
  788. //---------------------------------------------------------------------------
  789. void __fastcall TCustomScpExplorerForm::RefreshQueueItems()
  790. {
  791. if (FQueueStatus != NULL)
  792. {
  793. bool QueueAutoPopup = IsQueueAutoPopup();
  794. bool NeedRefresh = FQueueController->NeedRefresh();
  795. bool Refresh = FQueueItemInvalidated || NeedRefresh;
  796. FQueueItemInvalidated = false;
  797. int Limit = Refresh ? FQueueStatus->Count : FQueueStatus->DoneAndActiveCount;
  798. bool Updated = false;
  799. TQueueItemProxy * QueueItem;
  800. bool WasUserAction;
  801. for (int Index = 0; Index < Limit; Index++)
  802. {
  803. bool Update = false;
  804. QueueItem = FQueueStatus->Items[Index];
  805. WasUserAction = TQueueItem::IsUserActionStatus(QueueItem->Status);
  806. if (!NonVisualDataModule->Busy && QueueAutoPopup && WasUserAction &&
  807. (FPendingQueueActionItem == NULL))
  808. {
  809. FPendingQueueActionItem = QueueItem;
  810. FUserActionTimer->Enabled = true;
  811. }
  812. if ((bool)QueueItem->UserData)
  813. {
  814. QueueItem->UserData = (void*)false;
  815. QueueItem->Update();
  816. Updated = true;
  817. Update = true;
  818. }
  819. else if (FQueueController->QueueItemNeedsFrequentRefresh(QueueItem) || NeedRefresh)
  820. {
  821. Update = true;
  822. }
  823. if (Update)
  824. {
  825. FQueueController->RefreshQueueItem(QueueItem);
  826. }
  827. }
  828. if (Updated)
  829. {
  830. NonVisualDataModule->UpdateNonVisibleActions();
  831. SetQueueProgress();
  832. UpdateQueueFileList();
  833. }
  834. }
  835. }
  836. //---------------------------------------------------------------------------
  837. void __fastcall TCustomScpExplorerForm::SetTaskbarListProgressState(TBPFLAG Flags)
  838. {
  839. if (FTaskbarList != NULL)
  840. {
  841. // Could now use Application->MainFormHandle, now that we implement Application->OnGetMainFormHandle
  842. FTaskbarList->SetProgressState(GetMainForm()->Handle, Flags);
  843. }
  844. }
  845. //---------------------------------------------------------------------------
  846. void __fastcall TCustomScpExplorerForm::SetTaskbarListProgressValue(int Progress)
  847. {
  848. if (Progress >= 0)
  849. {
  850. if (FTaskbarList != NULL)
  851. {
  852. FTaskbarList->SetProgressValue(GetMainForm()->Handle, Progress, 100);
  853. }
  854. }
  855. else
  856. {
  857. SetTaskbarListProgressState(TBPF_INDETERMINATE);
  858. }
  859. }
  860. //---------------------------------------------------------------------------
  861. void __fastcall TCustomScpExplorerForm::SetTaskbarListProgressValue(TFileOperationProgressType * ProgressData)
  862. {
  863. int OverallProgress;
  864. // FProgressForm is null when this is called from SetQueueProgress
  865. if ((FProgressForm != NULL) && (FProgressForm->SynchronizeProgress != NULL))
  866. {
  867. OverallProgress = FProgressForm->SynchronizeProgress->Progress(ProgressData);
  868. }
  869. else if (!ProgressData->IsIndeterminate())
  870. {
  871. OverallProgress = ProgressData->OverallProgress();
  872. }
  873. else
  874. {
  875. OverallProgress = -1;
  876. }
  877. SetTaskbarListProgressValue(OverallProgress);
  878. }
  879. //---------------------------------------------------------------------------
  880. void __fastcall TCustomScpExplorerForm::SetQueueProgress()
  881. {
  882. TTerminalManager::Instance()->QueueStatusUpdated();
  883. if ((FTaskbarList != NULL) && (FProgressForm == NULL))
  884. {
  885. // Do not animate task bar button, when items are queued, but processing is disabled
  886. if ((FQueueStatus != NULL) && (FQueueStatus->ActivePrimaryCount > 0))
  887. {
  888. if (FQueueStatus->IsOnlyOneActiveAndNoPending())
  889. {
  890. TFileOperationProgressType * ProgressData = NULL;
  891. if (FQueueStatus->Items[FQueueStatus->DoneCount] != NULL)
  892. {
  893. ProgressData = FQueueStatus->Items[FQueueStatus->DoneCount]->ProgressData;
  894. }
  895. if ((ProgressData != NULL) &&
  896. ProgressData->InProgress)
  897. {
  898. SetTaskbarListProgressValue(ProgressData);
  899. }
  900. else
  901. {
  902. SetTaskbarListProgressState(TBPF_NOPROGRESS);
  903. }
  904. }
  905. else
  906. {
  907. SetTaskbarListProgressState(TBPF_INDETERMINATE);
  908. }
  909. }
  910. else
  911. {
  912. SetTaskbarListProgressState(TBPF_NOPROGRESS);
  913. }
  914. }
  915. UpdateQueueLabel();
  916. }
  917. //---------------------------------------------------------------------------
  918. void __fastcall TCustomScpExplorerForm::UpdateQueueLabel()
  919. {
  920. UnicodeString Caption = LoadStr(QUEUE_CAPTION);
  921. if (FQueueStatus != NULL)
  922. {
  923. int ActiveAndPendingPrimaryCount = FQueueStatus->ActiveAndPendingPrimaryCount;
  924. if (ActiveAndPendingPrimaryCount > 0)
  925. {
  926. Caption = FORMAT("%s (%d)", (Caption, ActiveAndPendingPrimaryCount));
  927. }
  928. }
  929. QueueLabel->Caption = Caption;
  930. }
  931. //---------------------------------------------------------------------------
  932. void TCustomScpExplorerForm::CheckStoreTransition()
  933. {
  934. if (WinConfiguration->StoreTransition == stInit)
  935. {
  936. if (IsUWP())
  937. {
  938. WinConfiguration->StoreTransition = stStoreFresh;
  939. AppLog(L"Fresh UWP/store installation");
  940. }
  941. else
  942. {
  943. WinConfiguration->StoreTransition = stStandard;
  944. AppLog(L"Fresh standard installation");
  945. }
  946. }
  947. else if ((WinConfiguration->StoreTransition == stStandard) && IsUWP())
  948. {
  949. WinConfiguration->StoreTransition = stStoreMigrated;
  950. WinConfiguration->FirstRun = WinConfiguration->FirstRun + L"=>" + StandardDatestamp();
  951. AppLog(L"Standard installation migrated to store installation");
  952. }
  953. if (WinConfiguration->StoreTransition == stStoreMigrated)
  954. {
  955. TTBXToolbar * Toolbar = new TTBXToolbar(MessageDock);
  956. Toolbar->Parent = MessageDock;
  957. Toolbar->FullSize = true;
  958. Toolbar->Color = Application->HintColor;
  959. TTBXLabelItem * LabelItem;
  960. LabelItem = new TTBXLabelItem(Toolbar);
  961. // Convert to explicit RGB "black" color, to make sure it is contrasting even in Dark theme
  962. // (TTBXOfficeXPTheme.GetStandardColor would convert clWindow to "white" in dark theme)
  963. LabelItem->FontSettings->Color = static_cast<TColor>(GetSysColor(clWindow));
  964. LabelItem->Margin = ScaleByTextHeight(MessageDock, 2); // Is this automatically rescaled?
  965. LabelItem->Caption = LoadStr(STORE_MIGRATION_THANKS);
  966. Toolbar->Items->Add(LabelItem);
  967. TTBXCustomItem * LinkItem = new TTBXItem(Toolbar);
  968. LinkItem->FontSettings->Color = LinkColor;
  969. LinkItem->Caption = LoadStr(STORE_MIGRATION_LEARN);
  970. LinkItem->OnClick = StoreTransitionLinkClick;
  971. Toolbar->Items->Add(LinkItem);
  972. AddMenuSeparator(Toolbar->Items);
  973. TTBXCustomItem * CloseItem = new TTBXItem(Toolbar);
  974. CloseItem->FontSettings->Color = static_cast<TColor>(GetSysColor(clWindow));
  975. CloseItem->Caption = LoadStr(MESSAGE_DISMISS);
  976. CloseItem->OnClick = StoreTransitionCloseClick;
  977. Toolbar->Items->Add(CloseItem);
  978. GiveTBItemPriority(CloseItem);
  979. }
  980. }
  981. //---------------------------------------------------------------------------
  982. void __fastcall TCustomScpExplorerForm::StoreTransitionLinkClick(TObject *)
  983. {
  984. ShowHelp(HELP_STORE_TRANSITION);
  985. }
  986. //---------------------------------------------------------------------------
  987. void __fastcall TCustomScpExplorerForm::StoreTransitionCloseClick(TObject * Sender)
  988. {
  989. TTBCustomItem * Item = DebugNotNull(dynamic_cast<TTBCustomItem *>(Sender));
  990. TTBXToolbar * Toolbar = DebugNotNull(dynamic_cast<TTBXToolbar *>(DebugNotNull(DebugNotNull(Item->Parent)->ParentComponent)));
  991. delete Toolbar;
  992. WinConfiguration->StoreTransition = stStoreAcknowledged;
  993. UpdateControls();
  994. }
  995. //---------------------------------------------------------------------------
  996. void __fastcall TCustomScpExplorerForm::UpdateTransferList()
  997. {
  998. TTBXStringList * TransferList = dynamic_cast<TTBXStringList*>(
  999. static_cast<TComponent*>(GetComponent(fcTransferList)));
  1000. TTBXDropDownItem * TransferDropDown = dynamic_cast<TTBXDropDownItem*>(
  1001. static_cast<TComponent*>(GetComponent(fcTransferDropDown)));
  1002. TransferList->Strings->BeginUpdate();
  1003. try
  1004. {
  1005. TransferList->Strings->Assign(GUIConfiguration->CopyParamList->NameList);
  1006. TransferList->Strings->Insert(0, StripHotkey(LoadStr(COPY_PARAM_DEFAULT)));
  1007. TransferList->ItemIndex = GUIConfiguration->CopyParamIndex + 1;
  1008. if (FTransferDropDownHint.IsEmpty())
  1009. {
  1010. FTransferDropDownHint = TransferDropDown->Hint;
  1011. }
  1012. // this way we get name for "default" settings (COPY_PARAM_DEFAULT)
  1013. UnicodeString Name = TransferList->Strings->Strings[TransferList->ItemIndex];
  1014. TransferDropDown->Text = StripHotkey(Name);
  1015. TransferDropDown->Hint = FORMAT(L"%s|%s:\n%s",
  1016. (FTransferDropDownHint, StripHotkey(Name),
  1017. GUIConfiguration->CurrentCopyParam.GetInfoStr(L"; ",
  1018. FLAGMASK(HasActiveTerminal(), Terminal->UsableCopyParamAttrs(0).General))));
  1019. // update the label, otherwise when it is updated only on the first draw
  1020. // of the list, it is drawn "bold" for some reason
  1021. FTransferListHoverIndex = TransferList->ItemIndex;
  1022. UpdateTransferLabel();
  1023. }
  1024. __finally
  1025. {
  1026. TransferList->Strings->EndUpdate();
  1027. }
  1028. }
  1029. //---------------------------------------------------------------------------
  1030. void __fastcall TCustomScpExplorerForm::UpdateCustomCommandsToolbar()
  1031. {
  1032. TTBXToolbar * Toolbar = dynamic_cast<TTBXToolbar *>(GetComponent(fcCustomCommandsBand));
  1033. DebugAssert(Toolbar != NULL);
  1034. NonVisualDataModule->UpdateCustomCommandsToolbar(Toolbar);
  1035. }
  1036. //---------------------------------------------------------------------------
  1037. void __fastcall TCustomScpExplorerForm::UpdateActions()
  1038. {
  1039. TForm::UpdateActions();
  1040. if (ComponentVisible[fcCustomCommandsBand])
  1041. {
  1042. UpdateCustomCommandsToolbar();
  1043. }
  1044. }
  1045. //---------------------------------------------------------------------------
  1046. void __fastcall TCustomScpExplorerForm::UpdateSessionsPageControlHeight()
  1047. {
  1048. SessionsPageControl->Height = SessionsPageControl->GetTabsHeight();
  1049. }
  1050. //---------------------------------------------------------------------------
  1051. void __fastcall TCustomScpExplorerForm::UpdateRowSelect(TCustomDirView * DirView)
  1052. {
  1053. if (DirView->RowSelect != WinConfiguration->FullRowSelect)
  1054. {
  1055. DirView->RowSelect = WinConfiguration->FullRowSelect;
  1056. // Without this, the panel turns gray and unused part of header turns black
  1057. DirView->Perform(CM_COLORCHANGED, 0, 0);
  1058. }
  1059. }
  1060. //---------------------------------------------------------------------------
  1061. void __fastcall TCustomScpExplorerForm::ConfigurationChanged()
  1062. {
  1063. Color = GetBtnFaceColor();
  1064. DebugAssert(Configuration && RemoteDirView);
  1065. RemoteDirView->DDAllowMove = !WinConfiguration->DDDisableMove;
  1066. RemoteDirView->DimmHiddenFiles = WinConfiguration->DimmHiddenFiles;
  1067. RemoteDirView->ShowHiddenFiles = WinConfiguration->ShowHiddenFiles;
  1068. RemoteDirView->FormatSizeBytes = WinConfiguration->FormatSizeBytes;
  1069. RemoteDirView->ShowInaccesibleDirectories = WinConfiguration->ShowInaccesibleDirectories;
  1070. RemoteDirView->NaturalOrderNumericalSorting = WinConfiguration->NaturalOrderNumericalSorting;
  1071. RemoteDirView->AlwaysSortDirectoriesByName = WinConfiguration->AlwaysSortDirectoriesByName;
  1072. UpdateRowSelect(RemoteDirView);
  1073. RemoteDriveView->DDAllowMove = !WinConfiguration->DDDisableMove;
  1074. RemoteDriveView->DimmHiddenDirs = WinConfiguration->DimmHiddenFiles;
  1075. RemoteDriveView->ShowHiddenDirs = WinConfiguration->ShowHiddenFiles;
  1076. RemoteDriveView->ShowInaccesibleDirectories = WinConfiguration->ShowInaccesibleDirectories;
  1077. RemoteDriveView->NaturalOrderNumericalSorting = WinConfiguration->NaturalOrderNumericalSorting;
  1078. UpdateSessionsPageControlHeight();
  1079. SetDockAllowDrag(!WinConfiguration->LockToolbars);
  1080. UpdateToolbarDisplayMode();
  1081. UpdateTransferList();
  1082. if (Terminal != NULL)
  1083. {
  1084. TransferPresetAutoSelect();
  1085. }
  1086. UpdateQueueView();
  1087. UpdateControls();
  1088. // this can be called even before constuctor finishes.
  1089. if (FEditorManager != NULL)
  1090. {
  1091. FEditorManager->ProcessFiles(FileConfigurationChanged, NULL);
  1092. }
  1093. // this can be called even before constuctor finishes.
  1094. if (FLocalEditors != NULL)
  1095. {
  1096. for (int Index = 0; Index < FLocalEditors->Count; Index++)
  1097. {
  1098. ReconfigureEditorForm(static_cast<TForm *>(FLocalEditors->Items[Index]));
  1099. }
  1100. }
  1101. if (ComponentVisible[fcCustomCommandsBand])
  1102. {
  1103. UpdateCustomCommandsToolbar();
  1104. }
  1105. // show only when keeping queue items forever,
  1106. // otherwise, it's enough to have it in the context menu
  1107. QueueDeleteAllDoneQueueToolbarItem->Visible =
  1108. WinConfiguration->QueueKeepDoneItems && (WinConfiguration->QueueKeepDoneItemsFor < 0);
  1109. if (FFileColorsCurrent != WinConfiguration->FileColors)
  1110. {
  1111. TFileColorData::LoadList(WinConfiguration->FileColors, FFileColors);
  1112. FFileColorsCurrent = WinConfiguration->FileColors;
  1113. FileColorsChanged();
  1114. }
  1115. if ((GetNewTabTab()->CaptionTruncation != GetNewTabTabCaptionTruncation()) &&
  1116. (FSessionColors != NULL))
  1117. {
  1118. SessionListChanged();
  1119. }
  1120. }
  1121. //---------------------------------------------------------------------------
  1122. void __fastcall TCustomScpExplorerForm::DoFileColorsChanged(TCustomDirView * DirView)
  1123. {
  1124. DirView->OnGetItemColor = FFileColors.empty() ? TDirViewGetItemColorEvent(NULL) : TDirViewGetItemColorEvent(&DirViewGetItemColor);
  1125. DirView->Invalidate();
  1126. }
  1127. //---------------------------------------------------------------------------
  1128. void __fastcall TCustomScpExplorerForm::FileColorsChanged()
  1129. {
  1130. DoFileColorsChanged(RemoteDirView);
  1131. }
  1132. //---------------------------------------------------------------------------
  1133. void __fastcall TCustomScpExplorerForm::FileConfigurationChanged(
  1134. const UnicodeString FileName, TEditedFileData * /*Data*/, TObject * Token,
  1135. void * /*Arg*/)
  1136. {
  1137. if (Token != NULL)
  1138. {
  1139. TForm * Editor = dynamic_cast<TForm*>(Token);
  1140. DebugAssert(Editor != NULL);
  1141. ReconfigureEditorForm(Editor);
  1142. }
  1143. }
  1144. //---------------------------------------------------------------------------
  1145. void __fastcall TCustomScpExplorerForm::EnableDDTransferConfirmation(TObject * /*Sender*/)
  1146. {
  1147. WinConfiguration->DDTransferConfirmation = asOn;
  1148. }
  1149. //---------------------------------------------------------------------------
  1150. void __fastcall TCustomScpExplorerForm::CopyParamDialogAfter(
  1151. TTransferDirection /*Direction*/, bool /*Temp*/, const UnicodeString & /*TargetDirectory*/)
  1152. {
  1153. // noop
  1154. }
  1155. //---------------------------------------------------------------------------
  1156. bool TCustomScpExplorerForm::GetDoNotShowCopyDialogDefault(bool DragDrop)
  1157. {
  1158. return DragDrop && (WinConfiguration->DDTransferConfirmation == asAuto);
  1159. }
  1160. //---------------------------------------------------------------------------
  1161. void TCustomScpExplorerForm::HandleDoNotShowCopyDialogAgain(bool DragDrop, bool DoNotShowAgain)
  1162. {
  1163. if (DoNotShowAgain)
  1164. {
  1165. if (DragDrop)
  1166. {
  1167. if (WinConfiguration->DDTransferConfirmation == asAuto)
  1168. {
  1169. PopupTrayBalloon(
  1170. NULL, LoadStr(DD_TRANSFER_CONFIRM_OFF2), qtInformation, NULL, 0, EnableDDTransferConfirmation, NULL);
  1171. }
  1172. WinConfiguration->DDTransferConfirmation = asOff;
  1173. }
  1174. else
  1175. {
  1176. WinConfiguration->ConfirmTransferring = false;
  1177. }
  1178. }
  1179. else
  1180. {
  1181. // User explicitly unchecked "do not show again",
  1182. // so show him the dialog the next time
  1183. if (DragDrop && (WinConfiguration->DDTransferConfirmation == asAuto))
  1184. {
  1185. WinConfiguration->DDTransferConfirmation = asOn;
  1186. }
  1187. }
  1188. }
  1189. //---------------------------------------------------------------------------
  1190. bool __fastcall TCustomScpExplorerForm::CopyParamDialog(
  1191. TTransferDirection Direction, TTransferType Type, bool Temp,
  1192. TStrings * FileList, UnicodeString & TargetDirectory, TGUICopyParamType & CopyParam,
  1193. bool Confirm, bool DragDrop, int Options)
  1194. {
  1195. bool Result = true;
  1196. DebugAssert(HasActiveTerminal());
  1197. // these parameters are known in advance
  1198. int Params =
  1199. FLAGMASK(Type == ttMove, cpDelete);
  1200. bool ToTemp = (Temp && (Direction == tdToLocal));
  1201. if (Result && Confirm && WinConfiguration->ConfirmTransferring)
  1202. {
  1203. bool DisableNewerOnly =
  1204. (!Terminal->IsCapable[fcNewerOnlyUpload] && (Direction == tdToRemote)) ||
  1205. ToTemp;
  1206. Options |=
  1207. FLAGMASK(ToTemp, coTemp) |
  1208. FLAGMASK(!Terminal->IsCapable[fcBackgroundTransfers], coDisableQueue) |
  1209. coDoNotShowAgain;
  1210. TUsableCopyParamAttrs UsableCopyParamAttrs = Terminal->UsableCopyParamAttrs(Params);
  1211. int CopyParamAttrs = (Direction == tdToRemote ?
  1212. UsableCopyParamAttrs.Upload : UsableCopyParamAttrs.Download) |
  1213. FLAGMASK(DisableNewerOnly, cpaNoNewerOnly);
  1214. int OutputOptions = FLAGMASK(GetDoNotShowCopyDialogDefault(DragDrop), cooDoNotShowAgain);
  1215. std::unique_ptr<TSessionData> SessionData(SessionDataForCode());
  1216. FlashOnBackground(); // Particularly when called from ClipboardFakeCreated
  1217. Result = DoCopyDialog(Direction == tdToRemote, Type == ttMove,
  1218. FileList, TargetDirectory, &CopyParam, Options, CopyParamAttrs, SessionData.get(), &OutputOptions, -1);
  1219. if (Result)
  1220. {
  1221. HandleDoNotShowCopyDialogAgain(DragDrop, FLAGSET(OutputOptions, cooDoNotShowAgain));
  1222. CopyParamDialogAfter(Direction, Temp, TargetDirectory);
  1223. }
  1224. }
  1225. if (Result && CopyParam.Queue && !ToTemp && Terminal->IsCapable[fcBackgroundTransfers])
  1226. {
  1227. Configuration->Usage->Inc(L"TransfersOnBackground");
  1228. // these parameter are known only after transfer dialog
  1229. Params |=
  1230. (CopyParam.QueueNoConfirmation ? cpNoConfirmation : 0);
  1231. AddQueueItem(Queue, Direction, FileList, TargetDirectory, CopyParam, Params);
  1232. Result = false;
  1233. ClearTransferSourceSelection(Direction);
  1234. }
  1235. return Result;
  1236. }
  1237. //---------------------------------------------------------------------------
  1238. void TCustomScpExplorerForm::ClearOperationSelection(TOperationSide Side)
  1239. {
  1240. if (FOnFileOperationFinished != NULL)
  1241. {
  1242. FOnFileOperationFinished(Side, UnicodeString(), true);
  1243. }
  1244. else
  1245. {
  1246. if (HasDirView[Side])
  1247. {
  1248. DirView(Side)->SelectAll(smNone);
  1249. }
  1250. }
  1251. }
  1252. //---------------------------------------------------------------------------
  1253. void __fastcall TCustomScpExplorerForm::ClearTransferSourceSelection(TTransferDirection Direction)
  1254. {
  1255. TOperationSide Side = ((Direction == tdToRemote) ? osLocal : osRemote);
  1256. ClearOperationSelection(Side);
  1257. }
  1258. //---------------------------------------------------------------------------
  1259. void __fastcall TCustomScpExplorerForm::AddQueueItem(
  1260. TTerminalQueue * Queue, TTransferDirection Direction, TStrings * FileList,
  1261. const UnicodeString TargetDirectory, TGUICopyParamType & CopyParam,
  1262. int Params)
  1263. {
  1264. DebugAssert(Queue != NULL);
  1265. bool SingleFile = false;
  1266. if (FileList->Count == 1)
  1267. {
  1268. if (Direction == tdToRemote)
  1269. {
  1270. UnicodeString FileName = FileList->Strings[0];
  1271. SingleFile = FileExists(ApiPath(FileName));
  1272. }
  1273. else
  1274. {
  1275. TRemoteFile * File = static_cast<TRemoteFile *>(FileList->Objects[0]);
  1276. SingleFile = !File->IsDirectory;
  1277. }
  1278. }
  1279. TQueueItem * QueueItem;
  1280. if (Direction == tdToRemote)
  1281. {
  1282. CopyParam.IncludeFileMask.SetRoots(FileList, TargetDirectory);
  1283. QueueItem = new TUploadQueueItem(Terminal, FileList, TargetDirectory,
  1284. &CopyParam, Params, SingleFile, CopyParam.QueueParallel);
  1285. }
  1286. else
  1287. {
  1288. CopyParam.IncludeFileMask.SetRoots(TargetDirectory, FileList);
  1289. QueueItem = new TDownloadQueueItem(Terminal, FileList, TargetDirectory,
  1290. &CopyParam, Params, SingleFile, CopyParam.QueueParallel);
  1291. }
  1292. AddQueueItem(Queue, QueueItem, Terminal);
  1293. }
  1294. //---------------------------------------------------------------------------
  1295. void __fastcall TCustomScpExplorerForm::AddQueueItem(TTerminalQueue * Queue, TQueueItem * QueueItem, TManagedTerminal * Terminal)
  1296. {
  1297. if (Queue->IsEmpty)
  1298. {
  1299. Terminal->QueueOperationStart = Now();
  1300. }
  1301. Queue->AddItem(QueueItem);
  1302. }
  1303. //---------------------------------------------------------------------------
  1304. void __fastcall TCustomScpExplorerForm::RestoreFormParams()
  1305. {
  1306. }
  1307. //---------------------------------------------------------------------------
  1308. void __fastcall TCustomScpExplorerForm::InitControls()
  1309. {
  1310. DebugAssert(FDocks != NULL);
  1311. for (int Index = 0; Index < ComponentCount; Index++)
  1312. {
  1313. TTBDock * Dock = dynamic_cast<TTBDock *>(Components[Index]);
  1314. if ((Dock != NULL) && (Dock->Tag == 0))
  1315. {
  1316. FDocks->Add(Dock);
  1317. }
  1318. }
  1319. CollectItemsWithTextDisplayMode(this);
  1320. }
  1321. //---------------------------------------------------------------------------
  1322. void __fastcall TCustomScpExplorerForm::RestoreParams()
  1323. {
  1324. ConfigurationChanged();
  1325. QueuePanel->Height =
  1326. LoadDimension(WinConfiguration->QueueView.Height, WinConfiguration->QueueView.HeightPixelsPerInch, this);
  1327. LoadListViewStr(QueueView3, WinConfiguration->QueueView.Layout);
  1328. QueueDock->Visible = WinConfiguration->QueueView.ToolBar;
  1329. QueueLabel->Visible = WinConfiguration->QueueView.Label;
  1330. QueueFileList->Visible = WinConfiguration->QueueView.FileList;
  1331. QueueFileList->Height =
  1332. LoadDimension(WinConfiguration->QueueView.FileListHeight, WinConfiguration->QueueView.FileListHeightPixelsPerInch, this);
  1333. if (QueueFileList->Visible)
  1334. {
  1335. AdjustQueueLayout();
  1336. }
  1337. UpdateCustomCommandsToolbar();
  1338. }
  1339. //---------------------------------------------------------------------------
  1340. void __fastcall TCustomScpExplorerForm::StoreParams()
  1341. {
  1342. int APixelsPerInch = GetControlPixelsPerInch(this);
  1343. WinConfiguration->QueueView.Height = QueuePanel->Height;
  1344. WinConfiguration->QueueView.HeightPixelsPerInch = APixelsPerInch;
  1345. WinConfiguration->QueueView.Layout = GetListViewStr(QueueView3);
  1346. WinConfiguration->QueueView.ToolBar = QueueDock->Visible;
  1347. WinConfiguration->QueueView.Label = QueueLabel->Visible;
  1348. WinConfiguration->QueueView.FileList = QueueFileList->Visible;
  1349. WinConfiguration->QueueView.FileListHeight = QueueFileList->Height;
  1350. WinConfiguration->QueueView.FileListHeightPixelsPerInch = APixelsPerInch;
  1351. }
  1352. //---------------------------------------------------------------------------
  1353. void __fastcall TCustomScpExplorerForm::CreateParams(TCreateParams & Params)
  1354. {
  1355. if (!FFormRestored)
  1356. {
  1357. FFormRestored = true;
  1358. RestoreFormParams();
  1359. }
  1360. TForm::CreateParams(Params);
  1361. }
  1362. //---------------------------------------------------------------------------
  1363. void __fastcall TCustomScpExplorerForm::SetDockAllowDrag(bool value)
  1364. {
  1365. DebugAssert(FDocks != NULL);
  1366. for (int Index = 0; Index < FDocks->Count; Index++)
  1367. {
  1368. static_cast<TTBDock*>(FDocks->Items[Index])->AllowDrag = value;
  1369. }
  1370. }
  1371. //---------------------------------------------------------------------------
  1372. void __fastcall TCustomScpExplorerForm::LoadToolbarsLayoutStr(UnicodeString LayoutStr, UnicodeString ButtonsStr)
  1373. {
  1374. SetDockAllowDrag(true);
  1375. while (!ButtonsStr.IsEmpty())
  1376. {
  1377. UnicodeString S = CutToChar(ButtonsStr, L';', true);
  1378. UnicodeString Name = CutToChar(S, L'=', true);
  1379. for (int ToolbarIndex = 0; ToolbarIndex < ComponentCount; ToolbarIndex++)
  1380. {
  1381. TTBCustomToolbar * Toolbar = dynamic_cast<TTBCustomToolbar *>(Components[ToolbarIndex]);
  1382. if ((Toolbar != NULL) && SameText(Name, GetToolbarKey(Toolbar->Name)))
  1383. {
  1384. while (!S.IsEmpty())
  1385. {
  1386. UnicodeString S2 = CutToChar(S, L',', true);
  1387. UnicodeString Name = CutToChar(S2, L':', true);
  1388. for (int ItemIndex = 0; ItemIndex < Toolbar->Items->Count; ItemIndex++)
  1389. {
  1390. TTBCustomItem * Item = Toolbar->Items->Items[ItemIndex];
  1391. if (SameText(GetToolbarItemName(Item), Name))
  1392. {
  1393. // forward compatibility
  1394. UnicodeString S3 = CutToChar(S2, L':', true);
  1395. bool Visible = StrToIntDef(S3, 0);
  1396. Item->Visible = Visible;
  1397. }
  1398. }
  1399. }
  1400. }
  1401. }
  1402. }
  1403. ::LoadToolbarsLayoutStr(this, LayoutStr);
  1404. SetDockAllowDrag(!WinConfiguration->LockToolbars);
  1405. }
  1406. //---------------------------------------------------------------------------
  1407. UnicodeString __fastcall TCustomScpExplorerForm::GetToolbarsLayoutStr()
  1408. {
  1409. UnicodeString Result;
  1410. SetDockAllowDrag(true);
  1411. Result = ::GetToolbarsLayoutStr(this);
  1412. SetDockAllowDrag(!WinConfiguration->LockToolbars);
  1413. return Result;
  1414. }
  1415. //---------------------------------------------------------------------------
  1416. UnicodeString __fastcall TCustomScpExplorerForm::GetToolbarItemName(TTBCustomItem * Item)
  1417. {
  1418. UnicodeString Result;
  1419. if (Item->Action != NULL)
  1420. {
  1421. Result = Item->Action->Name;
  1422. Result = RemoveSuffix(Result, L"Action", true);
  1423. }
  1424. else
  1425. {
  1426. Result = Item->Name;
  1427. Result = RemoveSuffix(Result, L"SubmenuItem", true);
  1428. Result = RemoveSuffix(Result, L"Item", true);
  1429. }
  1430. return Result;
  1431. }
  1432. //---------------------------------------------------------------------------
  1433. UnicodeString __fastcall TCustomScpExplorerForm::GetToolbarsButtonsStr()
  1434. {
  1435. UnicodeString Result;
  1436. for (int ToolbarIndex = 0; ToolbarIndex < ComponentCount; ToolbarIndex++)
  1437. {
  1438. UnicodeString ToolbarStr;
  1439. TTBCustomToolbar * Toolbar = dynamic_cast<TTBCustomToolbar *>(Components[ToolbarIndex]);
  1440. if ((Toolbar != NULL) && (Toolbar != QueueToolbar))
  1441. {
  1442. for (int ItemIndex = 0; ItemIndex < Toolbar->Items->Count; ItemIndex++)
  1443. {
  1444. TTBCustomItem * Item = Toolbar->Items->Items[ItemIndex];
  1445. // Currently all buttons are visible by default, so we can safely remember all hidden buttons.
  1446. // Once we introduce any buttons that are hidden by default, we would have to remember their initial state
  1447. // and save the changes here only.
  1448. if (NonVisualDataModule->IsCustomizableToolbarItem(Item) && !Item->Visible)
  1449. {
  1450. UnicodeString Name = GetToolbarItemName(Item);
  1451. DebugAssert(Name.Pos(L"TBX") == 0);
  1452. AddToList(ToolbarStr, FORMAT(L"%s:0", (Name)), L",");
  1453. }
  1454. }
  1455. }
  1456. if (!ToolbarStr.IsEmpty())
  1457. {
  1458. AddToList(Result, FORMAT(L"%s=%s", (GetToolbarKey(Toolbar->Name), ToolbarStr)), L";");
  1459. }
  1460. }
  1461. return Result;
  1462. }
  1463. //---------------------------------------------------------------------------
  1464. void __fastcall TCustomScpExplorerForm::CreateProgressForm(TSynchronizeProgress * SynchronizeProgress)
  1465. {
  1466. DebugAssert(FProgressForm == NULL);
  1467. bool AllowSkip = HasActiveTerminal() ? Terminal->IsCapable[fcSkipTransfer] : false;
  1468. bool AllowMoveToQueue = (FTransferResumeList != NULL) || (FDeletedFiles != NULL);
  1469. FProgressForm = new TProgressForm(Application, AllowMoveToQueue, AllowSkip, SynchronizeProgress);
  1470. FProgressForm->DeleteLocalToRecycleBin =
  1471. (WinConfiguration->DeleteToRecycleBin != FAlternativeDelete);
  1472. FProgressForm->DeleteRemoteToRecycleBin =
  1473. HasActiveTerminal() &&
  1474. (Terminal->SessionData->DeleteToRecycleBin != FAlternativeDelete) &&
  1475. !Terminal->SessionData->RecycleBinPath.IsEmpty();
  1476. // As progress window has delayed show now, we need to lock ourselves, (at least) until then
  1477. LockWindow();
  1478. // Actually, do not know what hides the progress once the operation finishes
  1479. // (it is possibly the SetQueueProgress - and we should not rely on that)
  1480. SetTaskbarListProgressState(TBPF_NORMAL);
  1481. }
  1482. //---------------------------------------------------------------------------
  1483. void __fastcall TCustomScpExplorerForm::DestroyProgressForm()
  1484. {
  1485. UnlockWindow();
  1486. SAFE_DESTROY(FProgressForm);
  1487. SetQueueProgress();
  1488. }
  1489. //---------------------------------------------------------------------------
  1490. void __fastcall TCustomScpExplorerForm::FileOperationProgress(
  1491. TFileOperationProgressType & ProgressData)
  1492. {
  1493. // Download to temporary local directory
  1494. if (FPendingTempSpaceWarn && ProgressData.InProgress && ProgressData.TotalSizeSet)
  1495. {
  1496. bool Continue = true;
  1497. FPendingTempSpaceWarn = false;
  1498. DoWarnLackOfTempSpace(ProgressData.Directory, ProgressData.TotalSize, Continue);
  1499. if (!Continue)
  1500. {
  1501. Abort();
  1502. }
  1503. }
  1504. // operation is being executed and we still didn't show up progress form
  1505. if (ProgressData.InProgress && !FProgressForm)
  1506. {
  1507. CreateProgressForm(NULL);
  1508. DebugAssert(FProgressForm->OnceDoneOperation == odoIdle);
  1509. FProgressForm->OnceDoneOperation = ProgressData.InitialOnceDoneOperation;
  1510. }
  1511. // operation is finished (or terminated), so we hide progress form
  1512. else if (!ProgressData.InProgress && (FProgressForm != NULL) && (FProgressForm->SynchronizeProgress == NULL))
  1513. {
  1514. DestroyProgressForm();
  1515. if (ProgressData.Operation == foCalculateSize)
  1516. {
  1517. // When calculating size before transfer, the abort caused by
  1518. // cancel flag set due to "MoveToQueue" below,
  1519. // is not propagated back to us in ExecuteFileOperation,
  1520. // as it is silently swallowed.
  1521. // So we explicitly (re)throw it here.
  1522. if (FMoveToQueue)
  1523. {
  1524. Abort();
  1525. }
  1526. }
  1527. else
  1528. {
  1529. if ((ProgressData.Cancel == csContinue) &&
  1530. !FAutoOperation)
  1531. {
  1532. OperationComplete(ProgressData.StartTime);
  1533. }
  1534. }
  1535. }
  1536. if (FProgressForm &&
  1537. // Can happen only during synchronization, when ending one of the operations
  1538. ProgressData.InProgress)
  1539. {
  1540. FProgressForm->SetProgressData(ProgressData);
  1541. DebugAssert(ProgressData.InProgress);
  1542. SetTaskbarListProgressValue(&ProgressData);
  1543. if (FProgressForm->Cancel > csContinue)
  1544. {
  1545. ProgressData.SetCancelAtLeast(FProgressForm->Cancel);
  1546. if (FProgressForm->Cancel == csCancelFile)
  1547. {
  1548. FProgressForm->ClearCancel();
  1549. }
  1550. // cancel cancels even the move
  1551. FMoveToQueue = false;
  1552. }
  1553. else if (FProgressForm->MoveToQueue)
  1554. {
  1555. FMoveToQueue = true;
  1556. ProgressData.SetCancelAtLeast(csCancel);
  1557. }
  1558. if ((FTransferResumeList != NULL) &&
  1559. ProgressData.InProgress &&
  1560. ProgressData.IsTransfer() &&
  1561. !ProgressData.FullFileName.IsEmpty())
  1562. {
  1563. if ((FTransferResumeList->Count == 0) ||
  1564. (FTransferResumeList->Strings[FTransferResumeList->Count - 1] != ProgressData.FullFileName))
  1565. {
  1566. // note that we do not recognize directories from files here
  1567. FTransferResumeList->Add(ProgressData.FullFileName);
  1568. }
  1569. }
  1570. }
  1571. }
  1572. //---------------------------------------------------------------------------
  1573. void __fastcall TCustomScpExplorerForm::OperationComplete(
  1574. const TDateTime & StartTime)
  1575. {
  1576. if (GUIConfiguration->BeepOnFinish &&
  1577. (Now() - StartTime > GUIConfiguration->BeepOnFinishAfter))
  1578. {
  1579. UnicodeString BeepSound = GUIConfiguration->BeepSound;
  1580. DWORD Sound;
  1581. if (!ExtractFileExt(BeepSound).IsEmpty())
  1582. {
  1583. Sound = SND_FILENAME;
  1584. }
  1585. else
  1586. {
  1587. Sound = SND_ALIAS;
  1588. }
  1589. PlaySound(BeepSound.c_str(), NULL, Sound | SND_ASYNC);
  1590. }
  1591. }
  1592. //---------------------------------------------------------------------------
  1593. void __fastcall TCustomScpExplorerForm::OperationProgress(
  1594. TFileOperationProgressType & ProgressData)
  1595. {
  1596. // For example, when calling TFileOperationProgressType::Suspend in TTerminal::QueryReopen,
  1597. // we cannot recurse back to TTerminal::Idle as that may detect another error, calling "suspend" again.
  1598. TAutoNestingCounter Counter(FDoNotIdleCurrentTerminal);
  1599. FileOperationProgress(ProgressData);
  1600. }
  1601. //---------------------------------------------------------------------------
  1602. UnicodeString __fastcall TCustomScpExplorerForm::GetProgressTitle()
  1603. {
  1604. UnicodeString Result;
  1605. if (FProgressForm != NULL)
  1606. {
  1607. Result = FProgressForm->ProgressStr();
  1608. }
  1609. return Result;
  1610. }
  1611. //---------------------------------------------------------------------------
  1612. bool __fastcall TCustomScpExplorerForm::PanelOperation(TOperationSide /*Side*/,
  1613. bool DragDrop)
  1614. {
  1615. return (!DragDrop && (DropSourceControl == NULL)) ||
  1616. (DropSourceControl == DirView(osOther));
  1617. }
  1618. //---------------------------------------------------------------------------
  1619. TListItem * TCustomScpExplorerForm::VisualiseOperationFinished(TOperationSide Side, const UnicodeString & FileName, bool Unselect)
  1620. {
  1621. TCustomDirView * ADirView = DirView(Side);
  1622. UnicodeString FileNameOnly = ExtractFileName(FileName, !IsSideLocalBrowser(Side));
  1623. TListItem * Item = ADirView->FindFileItem(FileNameOnly);
  1624. // this can happen when local drive is unplugged in the middle of the operation
  1625. if (Item != NULL)
  1626. {
  1627. if (Unselect)
  1628. {
  1629. Item->Selected = false;
  1630. }
  1631. ADirView->MakeProgressVisible(Item);
  1632. }
  1633. return Item;
  1634. }
  1635. //---------------------------------------------------------------------------
  1636. void __fastcall TCustomScpExplorerForm::DoOperationFinished(
  1637. TFileOperation Operation, TOperationSide Side,
  1638. bool /*Temp*/, const UnicodeString & FileName, bool Success,
  1639. TOnceDoneOperation & OnceDoneOperation)
  1640. {
  1641. if (!FAutoOperation)
  1642. {
  1643. // no selection on "/upload", form serves only as event handler
  1644. // (it is not displayed)
  1645. if (PanelOperation(Side, FDragDropOperation) && Visible &&
  1646. ((Operation == FPrimaryOperation) ||
  1647. ((Operation != foCalculateSize) && (Operation != foGetProperties) && (Operation != foCalculateChecksum))))
  1648. {
  1649. TOperationSide DViewSide;
  1650. if ((FProgressSide != osCurrent) &&
  1651. IsLocalBrowserMode() && (Side == osLocal)) // Only to limit the impact
  1652. {
  1653. // assume the operation is over the focused panel
  1654. DViewSide = FProgressSide;
  1655. }
  1656. else
  1657. {
  1658. DViewSide = Side;
  1659. }
  1660. if (FOnFileOperationFinished != NULL)
  1661. {
  1662. FOnFileOperationFinished(DViewSide, FileName, Success);
  1663. }
  1664. else
  1665. {
  1666. VisualiseOperationFinished(DViewSide, FileName, Success);
  1667. }
  1668. }
  1669. if (TFileOperationProgressType::IsTransferOperation(Operation))
  1670. {
  1671. if (DebugAlwaysTrue(!IsLocalBrowserMode()))
  1672. {
  1673. if (Side == osLocal)
  1674. {
  1675. Configuration->Usage->Inc(L"UploadedFiles");
  1676. }
  1677. else
  1678. {
  1679. Configuration->Usage->Inc(L"DownloadedFiles");
  1680. }
  1681. }
  1682. }
  1683. }
  1684. if (Success && (FSynchronizeController != NULL))
  1685. {
  1686. DebugAssert(!IsSideLocalBrowser(osRemote));
  1687. if (Operation == foCopy)
  1688. {
  1689. DebugAssert(Side == osLocal);
  1690. FSynchronizeController->LogOperation(soUpload, FileName);
  1691. }
  1692. else if (Operation == foDelete)
  1693. {
  1694. DebugAssert(Side == osRemote);
  1695. FSynchronizeController->LogOperation(soDelete, FileName);
  1696. }
  1697. }
  1698. if (FProgressForm)
  1699. {
  1700. OnceDoneOperation = FProgressForm->OnceDoneOperation;
  1701. }
  1702. }
  1703. //---------------------------------------------------------------------------
  1704. void __fastcall TCustomScpExplorerForm::OperationFinished(
  1705. TFileOperation Operation, TOperationSide Side,
  1706. bool Temp, const UnicodeString & FileName, Boolean Success,
  1707. TOnceDoneOperation & OnceDoneOperation)
  1708. {
  1709. TAutoNestingCounter Counter(FDoNotIdleCurrentTerminal);
  1710. DoOperationFinished(Operation, Side, Temp, FileName, Success,
  1711. OnceDoneOperation);
  1712. }
  1713. //---------------------------------------------------------------------------
  1714. bool TCustomScpExplorerForm::IsLocalBrowserMode()
  1715. {
  1716. return false;
  1717. }
  1718. //---------------------------------------------------------------------------
  1719. bool TCustomScpExplorerForm::SupportsLocalBrowser()
  1720. {
  1721. return false;
  1722. }
  1723. //---------------------------------------------------------------------------
  1724. bool TCustomScpExplorerForm::IsSideLocalBrowser(TOperationSide)
  1725. {
  1726. return false;
  1727. }
  1728. //---------------------------------------------------------------------------
  1729. TCustomDirView * TCustomScpExplorerForm::GetCurrentLocalBrowser()
  1730. {
  1731. return NULL;
  1732. }
  1733. //---------------------------------------------------------------------------
  1734. TCustomDirView * __fastcall TCustomScpExplorerForm::DirView(TOperationSide Side)
  1735. {
  1736. DebugAssert(GetSide(Side) == osRemote);
  1737. DebugUsedParam(Side);
  1738. return RemoteDirView;
  1739. }
  1740. //---------------------------------------------------------------------------
  1741. TCustomDriveView * __fastcall TCustomScpExplorerForm::DriveView(TOperationSide Side)
  1742. {
  1743. DebugAssert(GetSide(Side) == osRemote);
  1744. DebugUsedParam(Side);
  1745. return RemoteDriveView;
  1746. }
  1747. //---------------------------------------------------------------------------
  1748. bool __fastcall TCustomScpExplorerForm::DirViewEnabled(TOperationSide Side)
  1749. {
  1750. DebugAssert(GetSide(Side) == osRemote);
  1751. DebugUsedParam(Side);
  1752. return HasActiveTerminal();
  1753. }
  1754. //---------------------------------------------------------------------------
  1755. bool __fastcall TCustomScpExplorerForm::GetEnableFocusedOperation(
  1756. TOperationSide Side, int FilesOnly)
  1757. {
  1758. return DirView(Side)->AnyFileSelected(true, (FilesOnly != 0), true);
  1759. }
  1760. //---------------------------------------------------------------------------
  1761. bool __fastcall TCustomScpExplorerForm::GetEnableSelectedOperation(
  1762. TOperationSide Side, int FilesOnly)
  1763. {
  1764. return DirView(Side)->AnyFileSelected(false, (FilesOnly != 0), true);
  1765. }
  1766. //---------------------------------------------------------------------------
  1767. struct THistoryItemData
  1768. {
  1769. short int Side;
  1770. short int Index;
  1771. };
  1772. //---------------------------------------------------------------------------
  1773. void __fastcall TCustomScpExplorerForm::HistoryGo(TOperationSide Side, int Index)
  1774. {
  1775. DirView(Side)->HistoryGo(Index);
  1776. }
  1777. //---------------------------------------------------------------------------
  1778. void __fastcall TCustomScpExplorerForm::HistoryItemClick(System::TObject* Sender)
  1779. {
  1780. TTBCustomItem * Item = dynamic_cast<TTBCustomItem *>(Sender);
  1781. THistoryItemData Data = *reinterpret_cast<THistoryItemData*>(&(Item->Tag));
  1782. HistoryGo((TOperationSide)Data.Side, Data.Index);
  1783. }
  1784. //---------------------------------------------------------------------------
  1785. void __fastcall TCustomScpExplorerForm::UpdateHistoryMenu(TOperationSide Side,
  1786. bool Back)
  1787. {
  1788. if (FHistoryMenu[Side == osLocal][Back] != NULL)
  1789. {
  1790. TCustomDirView * DView = DirView(Side);
  1791. TTBXPopupMenu * Menu = FHistoryMenu[Side == osLocal][Back];
  1792. int ICount = Back ? DView->BackCount : DView->ForwardCount;
  1793. if (ICount > 10)
  1794. {
  1795. ICount = 10;
  1796. }
  1797. Menu->Items->Clear();
  1798. THistoryItemData Data;
  1799. Data.Side = Side;
  1800. for (short int i = 1; i <= ICount; i++)
  1801. {
  1802. TTBCustomItem * Item = new TTBXItem(Menu);
  1803. Data.Index = static_cast<short int>(i * (Back ? -1 : 1));
  1804. Item->Caption = MinimizeName(DView->HistoryPath[Data.Index], 50, !IsSideLocalBrowser(Side));
  1805. Item->Hint = DView->HistoryPath[Data.Index];
  1806. DebugAssert(sizeof(int) == sizeof(THistoryItemData));
  1807. Item->Tag = *reinterpret_cast<int*>(&Data);
  1808. Item->OnClick = HistoryItemClick;
  1809. Menu->Items->Add(Item);
  1810. }
  1811. }
  1812. }
  1813. //---------------------------------------------------------------------------
  1814. TTBXPopupMenu * __fastcall TCustomScpExplorerForm::HistoryMenu(
  1815. TOperationSide Side, bool Back)
  1816. {
  1817. if (FHistoryMenu[Side == osLocal][Back] == NULL)
  1818. {
  1819. // workaround
  1820. // In Pascal the size of TTBXPopupMenu is 132, in C++ 136,
  1821. // operator new allocates memory in Pascal code, but calls inline
  1822. // constructor in C++, leading in problems, the function does
  1823. // both in Pascal code
  1824. FHistoryMenu[Side == osLocal][Back] = CreateTBXPopupMenu(this);
  1825. UpdateHistoryMenu(Side, Back);
  1826. }
  1827. return FHistoryMenu[Side == osLocal][Back];
  1828. }
  1829. //---------------------------------------------------------------------------
  1830. void __fastcall TCustomScpExplorerForm::DirViewHistoryChange(
  1831. TCustomDirView *Sender)
  1832. {
  1833. TOperationSide Side = (Sender == DirView(osOther) ? osOther : osLocal);
  1834. UpdateHistoryMenu(Side, true);
  1835. UpdateHistoryMenu(Side, false);
  1836. }
  1837. //---------------------------------------------------------------------------
  1838. bool __fastcall TCustomScpExplorerForm::CustomCommandRemoteAllowed()
  1839. {
  1840. // remote custom commands can be executed only if the server supports shell commands
  1841. // or have secondary shell
  1842. return HasActiveTerminal() && (Terminal->IsCapable[fcSecondaryShell] || Terminal->IsCapable[fcShellAnyCommand]);
  1843. }
  1844. //---------------------------------------------------------------------------
  1845. int __fastcall TCustomScpExplorerForm::CustomCommandState(
  1846. const TCustomCommandType & Command, bool OnFocused, TCustomCommandListType ListType)
  1847. {
  1848. // -1 = hidden, 0 = disabled, 1 = enabled
  1849. int Result;
  1850. TFileCustomCommand RemoteCustomCommand;
  1851. TLocalCustomCommand LocalCustomCommand;
  1852. TFileCustomCommand * NonInteractiveCustomCommand =
  1853. FLAGCLEAR(Command.Params, ccLocal) ? &RemoteCustomCommand : &LocalCustomCommand;
  1854. TInteractiveCustomCommand InteractiveCustomCommand(NonInteractiveCustomCommand);
  1855. UnicodeString Cmd = InteractiveCustomCommand.Complete(Command.Command, false);
  1856. if (FLAGCLEAR(Command.Params, ccLocal))
  1857. {
  1858. int AllowedState = CustomCommandRemoteAllowed() ? 1 : 0;
  1859. // custom command that does not operate with files can be executed anytime ...
  1860. if (!NonInteractiveCustomCommand->IsFileCommand(Cmd))
  1861. {
  1862. // ... but do not show such command in remote file menu
  1863. if ((ListType == ccltAll) || (ListType == ccltNonFile))
  1864. {
  1865. Result = AllowedState;
  1866. }
  1867. else
  1868. {
  1869. Result = -1;
  1870. }
  1871. }
  1872. else
  1873. {
  1874. if ((ListType == ccltAll) || ((ListType == ccltFile) && !IsSideLocalBrowser(FCurrentSide)))
  1875. {
  1876. Result = (DirView(FCurrentSide)->AnyFileSelected(OnFocused, false, true)) ? AllowedState : 0;
  1877. }
  1878. else
  1879. {
  1880. Result = -1;
  1881. }
  1882. }
  1883. }
  1884. else
  1885. {
  1886. // custom command that does not operate with files can be executed anytime
  1887. if (!NonInteractiveCustomCommand->IsFileCommand(Cmd))
  1888. {
  1889. Result = ((ListType == ccltAll) || (ListType == ccltNonFile)) ? 1 : -1;
  1890. }
  1891. else if (LocalCustomCommand.HasLocalFileName(Cmd))
  1892. {
  1893. if ((ListType == ccltAll) || (ListType == ccltFile))
  1894. {
  1895. // special case is "diff"-style command that can be executed over any side,
  1896. // if we have both sides
  1897. Result =
  1898. // Cannot have focus on both panels, so we have to call AnyFileSelected
  1899. // directly (instead of EnableSelectedOperation) to pass
  1900. // false to FocusedFileOnlyWhenFocused when panel is inactive.
  1901. ((HasDirView[osLocal] && !IsSideLocalBrowser(osRemote) &&
  1902. DirView(osLocal)->AnyFileSelected(false, false, (FCurrentSide == osLocal))) &&
  1903. DirView(osRemote)->AnyFileSelected(false, false, (FCurrentSide == osRemote))) ? 1 : 0;
  1904. }
  1905. else if (ListType == ccltBoth)
  1906. {
  1907. DebugAssert(!IsLocalBrowserMode());
  1908. Result = 1;
  1909. }
  1910. else
  1911. {
  1912. Result = -1;
  1913. }
  1914. }
  1915. else
  1916. {
  1917. if ((ListType == ccltAll) ||
  1918. ((ListType == ccltFile) && (!IsSideLocalBrowser(FCurrentSide) || FLAGCLEAR(Command.Params, ccRemoteFiles))))
  1919. {
  1920. Result = DirView(FCurrentSide)->AnyFileSelected(OnFocused, false, true) ? 1 : 0;
  1921. }
  1922. else
  1923. {
  1924. Result = -1;
  1925. }
  1926. }
  1927. if ((Result > 0) &&
  1928. LocalCustomCommand.IsSessionCommand(Cmd) &&
  1929. !HasActiveTerminal())
  1930. {
  1931. Result = 0;
  1932. }
  1933. }
  1934. return Result;
  1935. }
  1936. //---------------------------------------------------------------------------
  1937. void __fastcall TCustomScpExplorerForm::RemoteCustomCommand(
  1938. TStrings * FileList, const TCustomCommandType & ACommand,
  1939. const TCustomCommandData & Data, const UnicodeString & CommandCommand)
  1940. {
  1941. if (EnsureCommandSessionFallback(fcShellAnyCommand))
  1942. {
  1943. TRemoteCustomCommand RemoteCustomCommand(Data, Terminal->CurrentDirectory);
  1944. TWinInteractiveCustomCommand InteractiveCustomCommand(
  1945. &RemoteCustomCommand, ACommand.Name, ACommand.HomePage);
  1946. UnicodeString Command = InteractiveCustomCommand.Complete(CommandCommand, false);
  1947. Configuration->Usage->Inc(L"RemoteCustomCommandRuns2");
  1948. bool Capture =
  1949. FLAGSET(ACommand.Params, ccShowResults) ||
  1950. FLAGSET(ACommand.Params, ccCopyResults) ||
  1951. FLAGSET(ACommand.Params, ccShowResultsInMsgBox);
  1952. TCaptureOutputEvent OutputEvent = NULL;
  1953. DebugAssert(FCapturedLog == NULL);
  1954. if (Capture)
  1955. {
  1956. FCapturedLog = new TStringList();
  1957. OutputEvent = TerminalCaptureLog;
  1958. }
  1959. try
  1960. {
  1961. if (!RemoteCustomCommand.IsFileCommand(Command))
  1962. {
  1963. Terminal->AnyCommand(RemoteCustomCommand.Complete(Command, true),
  1964. OutputEvent);
  1965. }
  1966. else
  1967. {
  1968. Terminal->CustomCommandOnFiles(Command, ACommand.Params, FileList, OutputEvent);
  1969. }
  1970. if ((FCapturedLog != NULL) && (FCapturedLog->Count > 0))
  1971. {
  1972. if (FLAGSET(ACommand.Params, ccCopyResults))
  1973. {
  1974. CopyToClipboard(FCapturedLog);
  1975. }
  1976. if (FLAGSET(ACommand.Params, ccShowResults))
  1977. {
  1978. DoConsoleDialog(Terminal, L"", FCapturedLog);
  1979. }
  1980. else if (FLAGSET(ACommand.Params, ccShowResultsInMsgBox))
  1981. {
  1982. MessageDialog(FCapturedLog->Text, qtInformation, qaOK);
  1983. }
  1984. }
  1985. }
  1986. __finally
  1987. {
  1988. SAFE_DESTROY(FCapturedLog);
  1989. }
  1990. }
  1991. }
  1992. //---------------------------------------------------------------------------
  1993. void __fastcall TCustomScpExplorerForm::LocalCustomCommandPure(
  1994. TStrings * FileList, const TCustomCommandType & ACommand, const UnicodeString & Command, TStrings * ALocalFileList,
  1995. const TCustomCommandData & Data, bool LocalFileCommand, bool FileListCommand, UnicodeString * POutput)
  1996. {
  1997. DebugAssert(!IsLocalBrowserMode());
  1998. TStrings * LocalFileList = NULL;
  1999. TStrings * RemoteFileList = NULL;
  2000. TStrings * RemoteFileListFull = NULL;
  2001. try
  2002. {
  2003. if (LocalFileCommand)
  2004. {
  2005. if (ALocalFileList == NULL)
  2006. {
  2007. DebugAssert(HasDirView[osLocal]);
  2008. // Cannot have focus on both panels, so we have to call AnyFileSelected
  2009. // directly (instead of EnableSelectedOperation) to pass
  2010. // false to FocusedFileOnlyWhenFocused
  2011. DebugAssert(DirView(osLocal)->AnyFileSelected(false, false, false));
  2012. LocalFileList = DirView(osLocal)->CreateFileList(false, true, NULL);
  2013. }
  2014. else
  2015. {
  2016. LocalFileList = ALocalFileList;
  2017. }
  2018. if (FileListCommand)
  2019. {
  2020. if (LocalFileList->Count != 1)
  2021. {
  2022. throw Exception(LoadStr(CUSTOM_COMMAND_SELECTED_UNMATCH1));
  2023. }
  2024. }
  2025. else
  2026. {
  2027. if ((LocalFileList->Count != 1) &&
  2028. (FileList->Count != 1) &&
  2029. (LocalFileList->Count != FileList->Count))
  2030. {
  2031. throw Exception(LoadStr(CUSTOM_COMMAND_SELECTED_UNMATCH));
  2032. }
  2033. }
  2034. }
  2035. UnicodeString RootTempDir;
  2036. UnicodeString TempDir;
  2037. bool RemoteFiles = FLAGSET(ACommand.Params, ccRemoteFiles);
  2038. if (!RemoteFiles)
  2039. {
  2040. TemporarilyDownloadFiles(FileList, false, RootTempDir, TempDir, false, true, true);
  2041. }
  2042. try
  2043. {
  2044. TDateTimes RemoteFileTimes;
  2045. if (RemoteFiles)
  2046. {
  2047. RemoteFileList = FileList;
  2048. }
  2049. else
  2050. {
  2051. RemoteFileList = new TStringList();
  2052. TMakeLocalFileListParams MakeFileListParam;
  2053. MakeFileListParam.FileList = RemoteFileList;
  2054. MakeFileListParam.FileTimes = &RemoteFileTimes;
  2055. MakeFileListParam.IncludeDirs = FLAGSET(ACommand.Params, ccApplyToDirectories);
  2056. MakeFileListParam.Recursive =
  2057. FLAGSET(ACommand.Params, ccRecursive) && !FileListCommand;
  2058. ProcessLocalDirectory(TempDir, Terminal->MakeLocalFileList, &MakeFileListParam);
  2059. if (!MakeFileListParam.Recursive)
  2060. {
  2061. RemoteFileTimes.clear();
  2062. RemoteFileListFull = new TStringList();
  2063. MakeFileListParam.FileList = RemoteFileListFull;
  2064. MakeFileListParam.Recursive = true;
  2065. MakeFileListParam.IncludeDirs = false;
  2066. ProcessLocalDirectory(TempDir, Terminal->MakeLocalFileList, &MakeFileListParam);
  2067. }
  2068. else
  2069. {
  2070. RemoteFileListFull = RemoteFileList;
  2071. }
  2072. }
  2073. bool NonBlocking = FileListCommand && RemoteFiles && !POutput;
  2074. TFileOperationProgressType Progress(&OperationProgress, &OperationFinished);
  2075. if (!NonBlocking)
  2076. {
  2077. int Count;
  2078. if (FileListCommand)
  2079. {
  2080. Count = 1;
  2081. }
  2082. else if (LocalFileCommand)
  2083. {
  2084. Count = std::max(LocalFileList->Count, RemoteFileList->Count);
  2085. }
  2086. else
  2087. {
  2088. Count = RemoteFileList->Count;
  2089. }
  2090. Progress.Start(foCustomCommand, osRemote, Count);
  2091. DebugAssert(FProgressForm != NULL);
  2092. FProgressForm->ReadOnly = true;
  2093. }
  2094. try
  2095. {
  2096. if (FileListCommand)
  2097. {
  2098. UnicodeString LocalFile;
  2099. // MakeFileList does not delimit filenames
  2100. UnicodeString FileList = MakeFileList(RemoteFileList);
  2101. if (LocalFileCommand)
  2102. {
  2103. DebugAssert(LocalFileList->Count == 1);
  2104. LocalFile = LocalFileList->Strings[0];
  2105. }
  2106. TLocalCustomCommand CustomCommand(Data,
  2107. Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(), L"", LocalFile, FileList);
  2108. UnicodeString ShellCommand = CustomCommand.Complete(Command, true);
  2109. if (NonBlocking)
  2110. {
  2111. DebugAssert(!POutput);
  2112. ExecuteShellChecked(ShellCommand);
  2113. }
  2114. else
  2115. {
  2116. ExecuteProcessCheckedAndWait(ShellCommand, HelpKeyword, POutput);
  2117. }
  2118. }
  2119. else if (LocalFileCommand)
  2120. {
  2121. if (LocalFileList->Count == 1)
  2122. {
  2123. UnicodeString LocalFile = LocalFileList->Strings[0];
  2124. for (int Index = 0; Index < RemoteFileList->Count; Index++)
  2125. {
  2126. UnicodeString FileName = RemoteFileList->Strings[Index];
  2127. UnicodeString FileNameForProgress = RemoteFiles ? FileName : ExtractFileName(FileName);
  2128. Progress.SetFile(FileNameForProgress);
  2129. TLocalCustomCommand CustomCommand(Data,
  2130. Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(), FileName, LocalFile, L"");
  2131. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  2132. TOnceDoneOperation OnceDoneOperation;
  2133. // Do not unselect anything, as it won't work with two panels anyway
  2134. Progress.Finish(UnicodeString(), true, OnceDoneOperation);
  2135. }
  2136. }
  2137. else if (RemoteFileList->Count == 1)
  2138. {
  2139. UnicodeString FileName = RemoteFileList->Strings[0];
  2140. for (int Index = 0; Index < LocalFileList->Count; Index++)
  2141. {
  2142. UnicodeString LocalFileName = LocalFileList->Strings[Index];
  2143. UnicodeString LocalFileNameOnly = ExtractFileName(LocalFileName);
  2144. Progress.SetFile(LocalFileNameOnly);
  2145. TLocalCustomCommand CustomCommand(
  2146. Data, Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(),
  2147. FileName, LocalFileName, L"");
  2148. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  2149. TOnceDoneOperation OnceDoneOperation;
  2150. // Do not unselect anything, as it won't work with two panels anyway
  2151. Progress.Finish(UnicodeString(), true, OnceDoneOperation);
  2152. }
  2153. }
  2154. else
  2155. {
  2156. if (LocalFileList->Count != RemoteFileList->Count)
  2157. {
  2158. throw Exception(LoadStr(CUSTOM_COMMAND_PAIRS_DOWNLOAD_FAILED));
  2159. }
  2160. for (int Index = 0; Index < LocalFileList->Count; Index++)
  2161. {
  2162. UnicodeString FileName = RemoteFileList->Strings[Index];
  2163. UnicodeString LocalFileName = LocalFileList->Strings[Index];
  2164. UnicodeString LocalFileNameOnly = ExtractFileName(LocalFileName);
  2165. Progress.SetFile(LocalFileNameOnly); // sic
  2166. TLocalCustomCommand CustomCommand(
  2167. Data, Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(),
  2168. FileName, LocalFileName, L"");
  2169. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  2170. TOnceDoneOperation OnceDoneOperation;
  2171. // Do not unselect anything, as it won't work with two panels anyway
  2172. Progress.Finish(UnicodeString(), true, OnceDoneOperation);
  2173. }
  2174. }
  2175. }
  2176. else
  2177. {
  2178. for (int Index = 0; Index < RemoteFileList->Count; Index++)
  2179. {
  2180. UnicodeString FileName = RemoteFileList->Strings[Index];
  2181. UnicodeString FileNameForProgress = RemoteFiles ? FileName : ExtractFileName(FileName);
  2182. Progress.SetFile(FileNameForProgress);
  2183. TLocalCustomCommand CustomCommand(Data,
  2184. Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(),
  2185. FileName, L"", L"");
  2186. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  2187. TOnceDoneOperation OnceDoneOperation;
  2188. Progress.Finish(FileNameForProgress, true, OnceDoneOperation);
  2189. }
  2190. }
  2191. }
  2192. __finally
  2193. {
  2194. if (!NonBlocking)
  2195. {
  2196. Progress.Stop();
  2197. }
  2198. }
  2199. DebugAssert(!FAutoOperation);
  2200. if (!RemoteFiles)
  2201. {
  2202. TempDir = IncludeTrailingBackslash(TempDir);
  2203. for (int Index = 0; Index < RemoteFileListFull->Count; Index++)
  2204. {
  2205. UnicodeString FileName = RemoteFileListFull->Strings[Index];
  2206. if (DebugAlwaysTrue(SameText(TempDir, FileName.SubString(1, TempDir.Length()))) &&
  2207. // Skip directories, process only nested files.
  2208. // The check is redundant as FileAge fails for directories anyway.
  2209. !DirectoryExists(FileName))
  2210. {
  2211. UnicodeString RemoteDir =
  2212. UnixExtractFileDir(
  2213. UnixIncludeTrailingBackslash(Terminal->CurrentDirectory) +
  2214. ToUnixPath(FileName.SubString(TempDir.Length() + 1, FileName.Length() - TempDir.Length())));
  2215. TDateTime NewTime;
  2216. if (FileAge(FileName, NewTime) &&
  2217. (NewTime != RemoteFileTimes[Index]))
  2218. {
  2219. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  2220. TemporaryFileCopyParam(CopyParam);
  2221. CopyParam.FileMask = L"";
  2222. FAutoOperation = true;
  2223. std::unique_ptr<TStrings> TemporaryFilesList(new TStringList());
  2224. TemporaryFilesList->Add(FileName);
  2225. Terminal->CopyToRemote(TemporaryFilesList.get(), RemoteDir, &CopyParam, cpTemporary, NULL);
  2226. }
  2227. }
  2228. }
  2229. }
  2230. }
  2231. __finally
  2232. {
  2233. FAutoOperation = false;
  2234. if (!RootTempDir.IsEmpty() && DebugAlwaysTrue(!RemoteFiles))
  2235. {
  2236. RecursiveDeleteFile(ExcludeTrailingBackslash(RootTempDir));
  2237. }
  2238. }
  2239. }
  2240. __finally
  2241. {
  2242. if (RemoteFileList != FileList)
  2243. {
  2244. delete RemoteFileList;
  2245. }
  2246. if (LocalFileList != ALocalFileList)
  2247. {
  2248. delete LocalFileList;
  2249. }
  2250. if (RemoteFileListFull != RemoteFileList)
  2251. {
  2252. delete RemoteFileListFull;
  2253. }
  2254. }
  2255. }
  2256. //---------------------------------------------------------------------------
  2257. void __fastcall TCustomScpExplorerForm::LocalCustomCommandWithLocalFiles(
  2258. const TCustomCommandType & ACommand, const UnicodeString & Command, const TCustomCommandData & Data,
  2259. bool FileListCommand, UnicodeString * POutput)
  2260. {
  2261. std::unique_ptr<TStrings> SelectedFileList(DirView(osCurrent)->CreateFileList(false, true, NULL));
  2262. std::unique_ptr<TStrings> LocalFileList(new TStringList());
  2263. for (int Index = 0; Index < SelectedFileList->Count; Index++)
  2264. {
  2265. UnicodeString FileName = SelectedFileList->Strings[Index];
  2266. if (DirectoryExists(FileName))
  2267. {
  2268. if (FLAGSET(ACommand.Params, ccApplyToDirectories))
  2269. {
  2270. LocalFileList->Add(FileName);
  2271. }
  2272. if (FLAGSET(ACommand.Params, ccRecursive))
  2273. {
  2274. TMakeLocalFileListParams MakeFileListParam;
  2275. MakeFileListParam.FileList = LocalFileList.get();
  2276. MakeFileListParam.FileTimes = NULL;
  2277. MakeFileListParam.IncludeDirs = FLAGSET(ACommand.Params, ccApplyToDirectories);
  2278. MakeFileListParam.Recursive = true;
  2279. ProcessLocalDirectory(FileName, Terminal->MakeLocalFileList, &MakeFileListParam);
  2280. }
  2281. }
  2282. else
  2283. {
  2284. LocalFileList->Add(FileName);
  2285. }
  2286. }
  2287. UnicodeString RemotePath;
  2288. if (Terminal != NULL)
  2289. {
  2290. RemotePath = Terminal->CurrentDirectory;
  2291. }
  2292. if (FileListCommand)
  2293. {
  2294. UnicodeString FileList = MakeFileList(LocalFileList.get());
  2295. TLocalCustomCommand CustomCommand(
  2296. Data, RemotePath, DefaultDownloadTargetDirectory(),
  2297. L"", L"", FileList);
  2298. ExecuteProcessChecked(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  2299. }
  2300. else
  2301. {
  2302. TFileOperationProgressType Progress(&OperationProgress, &OperationFinished);
  2303. Progress.Start(foCustomCommand, osLocal, LocalFileList->Count);
  2304. DebugAssert(FProgressForm != NULL);
  2305. FProgressForm->ReadOnly = true;
  2306. try
  2307. {
  2308. for (int Index = 0; Index < LocalFileList->Count; Index++)
  2309. {
  2310. UnicodeString FileName = LocalFileList->Strings[Index];
  2311. Progress.SetFile(FileName);
  2312. TLocalCustomCommand CustomCommand(
  2313. Data, RemotePath, DefaultDownloadTargetDirectory(),
  2314. FileName, L"", L"");
  2315. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  2316. TOnceDoneOperation OnceDoneOperation;
  2317. Progress.Finish(FileName, true, OnceDoneOperation);
  2318. }
  2319. }
  2320. __finally
  2321. {
  2322. Progress.Stop();
  2323. }
  2324. }
  2325. }
  2326. //---------------------------------------------------------------------------
  2327. void __fastcall TCustomScpExplorerForm::LocalCustomCommand(TStrings * FileList,
  2328. const TCustomCommandType & ACommand, TStrings * ALocalFileList,
  2329. const TCustomCommandData & Data, const UnicodeString & CommandCommand)
  2330. {
  2331. UnicodeString RemotePath = (Terminal != NULL) ? Terminal->CurrentDirectory : UnicodeString();
  2332. TLocalCustomCommand LocalCustomCommand(Data, RemotePath, DefaultDownloadTargetDirectory());
  2333. TWinInteractiveCustomCommand InteractiveCustomCommand(
  2334. &LocalCustomCommand, ACommand.Name, ACommand.HomePage);
  2335. UnicodeString Command = InteractiveCustomCommand.Complete(CommandCommand, false);
  2336. bool FileListCommand = LocalCustomCommand.IsFileListCommand(Command);
  2337. bool LocalFileCommand = LocalCustomCommand.HasLocalFileName(Command);
  2338. Configuration->Usage->Inc(L"LocalCustomCommandRuns2");
  2339. std::unique_ptr<UnicodeString> POutput;
  2340. if (FLAGSET(ACommand.Params, ccShowResultsInMsgBox) ||
  2341. FLAGSET(ACommand.Params, ccCopyResults))
  2342. {
  2343. POutput.reset(new UnicodeString());
  2344. }
  2345. TValueRestorer<TOperationSide> ProgressSideRestorer(FProgressSide, FCurrentSide);
  2346. if (!LocalCustomCommand.IsFileCommand(Command))
  2347. {
  2348. ExecuteProcessChecked(LocalCustomCommand.Complete(Command, true), HelpKeyword, POutput.get());
  2349. }
  2350. // remote files?
  2351. else if (!IsSideLocalBrowser(FCurrentSide) || LocalFileCommand)
  2352. {
  2353. LocalCustomCommandPure(FileList, ACommand, Command, ALocalFileList, Data, LocalFileCommand, FileListCommand, POutput.get());
  2354. }
  2355. // local files
  2356. else
  2357. {
  2358. LocalCustomCommandWithLocalFiles(ACommand, Command, Data, FileListCommand, POutput.get());
  2359. }
  2360. if (POutput.get() != NULL)
  2361. {
  2362. // If the output is single-line, we do not want the trailing CRLF, as the CopyToClipboard(TStrings *) does
  2363. int P = POutput->Pos(sLineBreak);
  2364. if (P == POutput->Length() - static_cast<int>(strlen(sLineBreak)) + 1)
  2365. {
  2366. POutput->SetLength(P - 1);
  2367. }
  2368. // Copy even empty output to clipboard
  2369. if (FLAGSET(ACommand.Params, ccCopyResults))
  2370. {
  2371. CopyToClipboard(*POutput);
  2372. }
  2373. // But do not show an empty message box.
  2374. // This way the ShowResultsInMsgBox can be used to suppress a console window of commands with no output
  2375. if (FLAGSET(ACommand.Params, ccShowResultsInMsgBox) &&
  2376. !POutput->IsEmpty())
  2377. {
  2378. TClipboardHandler ClipboardHandler;
  2379. ClipboardHandler.Text = *POutput;
  2380. TMessageParams Params;
  2381. TQueryButtonAlias Aliases[1];
  2382. Aliases[0].Button = qaRetry;
  2383. Aliases[0].Alias = LoadStr(EDIT_COPY);
  2384. Aliases[0].OnSubmit = &ClipboardHandler.Copy;
  2385. Params.Aliases = Aliases;
  2386. Params.AliasesCount = LENOF(Aliases);
  2387. MessageDialog(*POutput, qtInformation, qaOK | qaRetry, HelpKeyword, &Params);
  2388. }
  2389. }
  2390. }
  2391. //---------------------------------------------------------------------------
  2392. void __fastcall TCustomScpExplorerForm::CustomCommand(TStrings * FileList,
  2393. const TCustomCommandType & ACommand, TStrings * ALocalFileList)
  2394. {
  2395. TCustomCommandData Data;
  2396. UnicodeString Site;
  2397. UnicodeString RemotePath;
  2398. if (HasActiveTerminal())
  2399. {
  2400. std::unique_ptr<TSessionData> SessionData(SessionDataForCode());
  2401. Data = TCustomCommandData(SessionData.get());
  2402. Site = Terminal->SessionData->SessionKey;
  2403. RemotePath = Terminal->CurrentDirectory;
  2404. }
  2405. UnicodeString HelpKeyword = ACommand.HomePage;
  2406. std::unique_ptr<TStrings> CustomCommandOptions(CloneStrings(WinConfiguration->CustomCommandOptions));
  2407. if (ACommand.AnyOptionWithFlag(TCustomCommandType::ofRun))
  2408. {
  2409. std::unique_ptr<TCustomCommand> CustomCommandForOptions;
  2410. if (FLAGCLEAR(ACommand.Params, ccLocal))
  2411. {
  2412. CustomCommandForOptions.reset(new TRemoteCustomCommand(Data, RemotePath));
  2413. }
  2414. else
  2415. {
  2416. CustomCommandForOptions.reset(new TLocalCustomCommand(Data, RemotePath, DefaultDownloadTargetDirectory()));
  2417. }
  2418. if (!DoCustomCommandOptionsDialog(
  2419. &ACommand, CustomCommandOptions.get(), NULL, TCustomCommandType::ofRun, CustomCommandForOptions.get(), Site, NULL))
  2420. {
  2421. Abort();
  2422. }
  2423. }
  2424. UnicodeString CommandCommand = ACommand.GetCommandWithExpandedOptions(CustomCommandOptions.get(), Site);
  2425. if (FLAGCLEAR(ACommand.Params, ccLocal))
  2426. {
  2427. RemoteCustomCommand(FileList, ACommand, Data, CommandCommand);
  2428. }
  2429. else
  2430. {
  2431. LocalCustomCommand(FileList, ACommand, ALocalFileList, Data, CommandCommand);
  2432. }
  2433. }
  2434. //---------------------------------------------------------------------------
  2435. void __fastcall TCustomScpExplorerForm::BothCustomCommand(
  2436. const TCustomCommandType & Command)
  2437. {
  2438. DebugAssert(FCustomCommandLocalFileList != NULL);
  2439. DebugAssert(FCustomCommandRemoteFileList != NULL);
  2440. DebugAssert(FCustomCommandLocalFileList->Count == FCustomCommandRemoteFileList->Count);
  2441. TStrings * LocalFileList = new TStringList();
  2442. TStrings * RemoteFileList = new TStringList();
  2443. try
  2444. {
  2445. for (int Index = 0; Index < FCustomCommandLocalFileList->Count; Index++)
  2446. {
  2447. LocalFileList->Clear();
  2448. LocalFileList->AddObject(
  2449. FCustomCommandLocalFileList->Strings[Index],
  2450. FCustomCommandLocalFileList->Objects[Index]);
  2451. RemoteFileList->Clear();
  2452. RemoteFileList->AddObject(
  2453. FCustomCommandRemoteFileList->Strings[Index],
  2454. FCustomCommandRemoteFileList->Objects[Index]);
  2455. CustomCommand(RemoteFileList, Command, LocalFileList);
  2456. }
  2457. }
  2458. __finally
  2459. {
  2460. delete LocalFileList;
  2461. delete RemoteFileList;
  2462. }
  2463. }
  2464. //---------------------------------------------------------------------------
  2465. void __fastcall TCustomScpExplorerForm::CustomCommandMenu(
  2466. TAction * Action, TStrings * LocalFileList, TStrings * RemoteFileList)
  2467. {
  2468. delete FCustomCommandLocalFileList;
  2469. delete FCustomCommandRemoteFileList;
  2470. // takeover ownership,
  2471. // the lists must survive the MenuPopup as OnClick occurs only after it exits
  2472. FCustomCommandLocalFileList = LocalFileList;
  2473. FCustomCommandRemoteFileList = RemoteFileList;
  2474. TButton * Button = dynamic_cast<TButton *>(Action->ActionComponent);
  2475. if (Button != NULL)
  2476. {
  2477. FCustomCommandMenu->Items->Clear();
  2478. NonVisualDataModule->CreateCustomCommandsMenu(FCustomCommandMenu->Items, false, false, ccltBoth, NULL);
  2479. MenuPopup(FCustomCommandMenu, Button);
  2480. }
  2481. else
  2482. {
  2483. NonVisualDataModule->CreateCustomCommandsMenu(Action, false, ccltBoth);
  2484. }
  2485. }
  2486. //---------------------------------------------------------------------------
  2487. void __fastcall TCustomScpExplorerForm::TerminalCaptureLog(
  2488. const UnicodeString & AddedLine, TCaptureOutputType OutputType)
  2489. {
  2490. DebugAssert(FCapturedLog != NULL);
  2491. if ((OutputType == cotOutput) || (OutputType == cotError))
  2492. {
  2493. FCapturedLog->Add(AddedLine);
  2494. }
  2495. }
  2496. //---------------------------------------------------------------------------
  2497. bool __fastcall TCustomScpExplorerForm::IsFileControl(TObject * Control,
  2498. TOperationSide Side)
  2499. {
  2500. return
  2501. (Side == osOther) &&
  2502. ((Control == DirView(osOther)) || (Control == DriveView(osOther)));
  2503. }
  2504. //---------------------------------------------------------------------------
  2505. void __fastcall TCustomScpExplorerForm::DirViewContextPopupDefaultItem(
  2506. TOperationSide Side, TTBXCustomItem * Item,
  2507. TResolvedDoubleClickAction DoubleClickAction1, TResolvedDoubleClickAction DoubleClickAction2)
  2508. {
  2509. TCustomDirView * DView = DirView(Side);
  2510. bool IsDefault = false;
  2511. if (DView->ItemFocused != NULL)
  2512. {
  2513. TTerminal * ATerminal = !IsSideLocalBrowser(Side) ? Terminal : NULL;
  2514. TResolvedDoubleClickAction DefaultAction =
  2515. WinConfiguration->ResolveDoubleClickAction(DView->ItemIsDirectory(DView->ItemFocused), ATerminal);
  2516. IsDefault = (DefaultAction == DoubleClickAction1) || (DefaultAction == DoubleClickAction2);
  2517. }
  2518. TTBItemOptions O = Item->Options;
  2519. if (IsDefault)
  2520. {
  2521. Item->Options = O << tboDefault;
  2522. }
  2523. else
  2524. {
  2525. Item->Options = O >> tboDefault;
  2526. }
  2527. }
  2528. //---------------------------------------------------------------------------
  2529. void __fastcall TCustomScpExplorerForm::DirViewContextPopup(
  2530. TOperationSide Side, Byte PopupComponent, const TPoint &MousePos)
  2531. {
  2532. TCustomDirView * DView = DirView(Side);
  2533. TListItem * Item = DView->ItemFocused;
  2534. if ((DView->GetItemAt(MousePos.x, MousePos.y) == Item) &&
  2535. EnableFocusedOperation[Side])
  2536. {
  2537. TPoint ClientPoint;
  2538. ClientPoint = ((MousePos.x < 0) && (MousePos.y < 0)) ?
  2539. TPoint(0, 0) : MousePos;
  2540. FLastContextPopupScreenPoint = DView->ClientToScreen(ClientPoint);
  2541. reinterpret_cast<TPopupMenu*>(GetComponent(PopupComponent))->Popup(
  2542. FLastContextPopupScreenPoint.x, FLastContextPopupScreenPoint.y);
  2543. }
  2544. }
  2545. //---------------------------------------------------------------------------
  2546. void __fastcall TCustomScpExplorerForm::RemoteDirViewContextPopup(
  2547. TObject * /*Sender*/, const TPoint &MousePos, bool &Handled)
  2548. {
  2549. DirViewContextPopupDefaultItem(osRemote, NonVisualDataModule->RemoteOpenMenuItem, rdcaChangeDir, rdcaOpen);
  2550. DirViewContextPopupDefaultItem(osRemote, NonVisualDataModule->RemoteEditMenuItem, rdcaEdit, rdcaNone);
  2551. DirViewContextPopupDefaultItem(osRemote, NonVisualDataModule->RemoteCopyMenuItem, rdcaCopy, rdcaNone);
  2552. DirViewContextPopup(osRemote, fcRemotePopup, MousePos);
  2553. Handled = true;
  2554. }
  2555. //---------------------------------------------------------------------------
  2556. void __fastcall TCustomScpExplorerForm::ReloadLocalDirectory(const UnicodeString Directory)
  2557. {
  2558. }
  2559. //---------------------------------------------------------------------------
  2560. void __fastcall TCustomScpExplorerForm::BatchStart(void *& /*Storage*/)
  2561. {
  2562. DebugAssert(FErrorList == NULL);
  2563. if (WinConfiguration->ContinueOnError)
  2564. {
  2565. FErrorList = new TStringList();
  2566. Configuration->Usage->Inc(L"ContinuationsOnError");
  2567. }
  2568. NonVisualDataModule->StartBusy();
  2569. }
  2570. //---------------------------------------------------------------------------
  2571. void __fastcall TCustomScpExplorerForm::BatchEnd(void * /*Storage*/)
  2572. {
  2573. NonVisualDataModule->EndBusy();
  2574. if (FErrorList)
  2575. {
  2576. HandleErrorList(FErrorList);
  2577. }
  2578. }
  2579. //---------------------------------------------------------------------------
  2580. void __fastcall TCustomScpExplorerForm::UpdateCopyParamCounters(
  2581. const TCopyParamType & CopyParam)
  2582. {
  2583. if (!CopyParam.IncludeFileMask.Masks.IsEmpty())
  2584. {
  2585. Configuration->Usage->Inc(L"FileMaskUses");
  2586. }
  2587. if (IsEffectiveFileNameMask(CopyParam.FileMask))
  2588. {
  2589. Configuration->Usage->Inc(L"OperationMaskUses");
  2590. }
  2591. }
  2592. //---------------------------------------------------------------------------
  2593. bool __fastcall TCustomScpExplorerForm::ExecuteCopyMoveFileOperation(
  2594. TFileOperation Operation, TOperationSide Side, TStrings * FileList, bool NoConfirmation, void * AParam)
  2595. {
  2596. DebugAssert(!IsLocalBrowserMode());
  2597. TTransferDirection Direction = (Side == osLocal ? tdToRemote : tdToLocal);
  2598. TTransferType Type = (Operation == foCopy ? ttCopy : ttMove);
  2599. TTransferOperationParam DefaultParam;
  2600. TTransferOperationParam & Param = (AParam != NULL) ? *static_cast<TTransferOperationParam *>(AParam) : DefaultParam;
  2601. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  2602. switch (Param.Queue)
  2603. {
  2604. case asOn:
  2605. CopyParam.Queue = true;
  2606. break;
  2607. case asOff:
  2608. CopyParam.Queue = false;
  2609. break;
  2610. case asAuto:
  2611. default:
  2612. // keep default
  2613. break;
  2614. }
  2615. bool Result =
  2616. CopyParamDialog(Direction, Type, Param.Temp, FileList, Param.TargetDirectory, CopyParam, !NoConfirmation, Param.DragDrop, Param.Options);
  2617. if (Result)
  2618. {
  2619. DebugAssert(Terminal);
  2620. bool SelectionRestored = false;
  2621. TCustomDirView * DView = NULL;
  2622. if (HasDirView[Side])
  2623. {
  2624. DView = DirView(Side);
  2625. DView->SaveSelection();
  2626. DView->SaveSelectedNames();
  2627. }
  2628. UpdateCopyParamCounters(CopyParam);
  2629. std::unique_ptr<TStringList> TransferResumeList(new TStringList());
  2630. DebugAssert(FTransferResumeList == NULL);
  2631. FTransferResumeList = Terminal->IsCapable[fcMoveToQueue] ? TransferResumeList.get() : NULL;
  2632. FMoveToQueue = false;
  2633. int Params = FLAGMASK(Operation == foMove, cpDelete);
  2634. try
  2635. {
  2636. TStrings * PermanentFileList;
  2637. std::unique_ptr<TStrings> PermanentFileListOwner;
  2638. try
  2639. {
  2640. if (Side == osLocal)
  2641. {
  2642. PermanentFileList = FileList;
  2643. Params |= FLAGMASK(Param.Temp, cpTemporary);
  2644. CopyParam.IncludeFileMask.SetRoots(FileList, Param.TargetDirectory);
  2645. Terminal->CopyToRemote(FileList, Param.TargetDirectory, &CopyParam, Params, NULL);
  2646. if (Operation == foMove)
  2647. {
  2648. ReloadLocalDirectory();
  2649. if (DView != NULL)
  2650. {
  2651. DView->RestoreSelection();
  2652. }
  2653. SelectionRestored = true;
  2654. }
  2655. }
  2656. else
  2657. {
  2658. // Clone the file list as it may refer to current directory files,
  2659. // which get destroyed, when the source directory is reloaded after foMove operation.
  2660. // We should actually clone the file list for whole ExecuteFileOperation to protect against reloads.
  2661. // But for a hotfix, we are not going to do such a big change.
  2662. PermanentFileListOwner.reset(TRemoteFileList::CloneStrings(FileList));
  2663. PermanentFileList = PermanentFileListOwner.get();
  2664. try
  2665. {
  2666. CopyParam.IncludeFileMask.SetRoots(Param.TargetDirectory, FileList);
  2667. Terminal->CopyToLocal(FileList, Param.TargetDirectory, &CopyParam, Params, NULL);
  2668. }
  2669. __finally
  2670. {
  2671. if (Operation == foMove)
  2672. {
  2673. if (DView != NULL)
  2674. {
  2675. DView->RestoreSelection();
  2676. }
  2677. SelectionRestored = true;
  2678. }
  2679. ReloadLocalDirectory(Param.TargetDirectory);
  2680. }
  2681. }
  2682. // When the transfer batch is aborted between individual file transfers, the Abort is not called.
  2683. if (FMoveToQueue)
  2684. {
  2685. Abort();
  2686. }
  2687. }
  2688. catch (EAbort &)
  2689. {
  2690. if (FMoveToQueue)
  2691. {
  2692. Params |= (CopyParam.QueueNoConfirmation ? cpNoConfirmation : 0);
  2693. DebugAssert(CopyParam.TransferSkipList == NULL);
  2694. DebugAssert(CopyParam.TransferResumeFile.IsEmpty());
  2695. if (TransferResumeList->Count > 0)
  2696. {
  2697. CopyParam.TransferResumeFile = TransferResumeList->Strings[TransferResumeList->Count - 1];
  2698. TransferResumeList->Delete(TransferResumeList->Count - 1);
  2699. }
  2700. CopyParam.TransferSkipList = TransferResumeList.release();
  2701. // not really needed, just to keep it consistent with TransferResumeList
  2702. FTransferResumeList = NULL;
  2703. FMoveToQueue = false;
  2704. Configuration->Usage->Inc("MovesToBackground");
  2705. AddQueueItem(Queue, Direction, PermanentFileList, Param.TargetDirectory, CopyParam, Params);
  2706. ClearTransferSourceSelection(Direction);
  2707. }
  2708. throw;
  2709. }
  2710. }
  2711. __finally
  2712. {
  2713. if (!SelectionRestored && (DView != NULL))
  2714. {
  2715. DView->DiscardSavedSelection();
  2716. }
  2717. FTransferResumeList = NULL;
  2718. }
  2719. }
  2720. return Result;
  2721. }
  2722. //---------------------------------------------------------------------------
  2723. bool __fastcall TCustomScpExplorerForm::ExecuteDeleteFileOperation(
  2724. TOperationSide Side, TStrings * FileList, void * Param)
  2725. {
  2726. DebugAssert(FileList->Count);
  2727. // We deliberately do not toggle alternative flag (Param), but use OR,
  2728. // because the Param is set only when command is invoked using Shift-Del/F8 keyboard
  2729. // shortcut of CurrentDeleteAlternativeAction
  2730. bool Alternative =
  2731. bool(Param) || UseAlternativeFunction();
  2732. bool Recycle;
  2733. if (IsSideLocalBrowser(Side))
  2734. {
  2735. Recycle = (WinConfiguration->DeleteToRecycleBin != Alternative);
  2736. }
  2737. else
  2738. {
  2739. Recycle =
  2740. (Terminal->SessionData->DeleteToRecycleBin != Alternative) &&
  2741. !Terminal->SessionData->RecycleBinPath.IsEmpty() &&
  2742. !Terminal->IsRecycledFile(FileList->Strings[0]);
  2743. }
  2744. bool Result = !(Recycle ? WinConfiguration->ConfirmRecycling : WinConfiguration->ConfirmDeleting);
  2745. if (!Result)
  2746. {
  2747. UnicodeString Query;
  2748. if (FileList->Count == 1)
  2749. {
  2750. if (IsSideLocalBrowser(Side))
  2751. {
  2752. Query = ExtractFileName(FileList->Strings[0]);
  2753. }
  2754. else
  2755. {
  2756. Query = UnixExtractFileName(FileList->Strings[0]);
  2757. }
  2758. Query = FMTLOAD((Recycle ? CONFIRM_RECYCLE_FILE : CONFIRM_DELETE_FILE), (Query));
  2759. }
  2760. else
  2761. {
  2762. Query = FMTLOAD((Recycle ? CONFIRM_RECYCLE_FILES : CONFIRM_DELETE_FILES), (FileList->Count));
  2763. }
  2764. TMessageParams Params(mpNeverAskAgainCheck);
  2765. Params.ImageName = L"Delete file";
  2766. unsigned int Answer =
  2767. MessageDialog(MainInstructions(Query), qtConfirmation, qaOK | qaCancel, HELP_DELETE_FILE, &Params);
  2768. if (Answer == qaNeverAskAgain)
  2769. {
  2770. Result = true;
  2771. if (Recycle)
  2772. {
  2773. WinConfiguration->ConfirmRecycling = false;
  2774. }
  2775. else
  2776. {
  2777. WinConfiguration->ConfirmDeleting = false;
  2778. }
  2779. }
  2780. else
  2781. {
  2782. Result = (Answer == qaOK);
  2783. }
  2784. }
  2785. if (Result)
  2786. {
  2787. DeleteFiles(Side, FileList, FLAGMASK(Alternative, dfAlternative));
  2788. }
  2789. return Result;
  2790. }
  2791. //---------------------------------------------------------------------------
  2792. bool __fastcall TCustomScpExplorerForm::ExecuteFileOperation(TFileOperation Operation,
  2793. TOperationSide Side, TStrings * FileList, bool NoConfirmation, void * Param)
  2794. {
  2795. TAutoBatch AutoBatch(this);
  2796. bool Result;
  2797. if (TFileOperationProgressType::IsTransferOperation(Operation))
  2798. {
  2799. Result = ExecuteCopyMoveFileOperation(Operation, Side, FileList, NoConfirmation, Param);
  2800. }
  2801. else if (Operation == foRename)
  2802. {
  2803. DebugAssert(DirView(Side)->ItemFocused);
  2804. DirView(Side)->ItemFocused->EditCaption();
  2805. Result = true;
  2806. }
  2807. else if (Operation == foDelete)
  2808. {
  2809. Result = ExecuteDeleteFileOperation(Side, FileList, Param);
  2810. }
  2811. else if (Operation == foSetProperties)
  2812. {
  2813. DirView(osRemote)->SaveSelectedNames();
  2814. Result = SetProperties(Side, FileList);
  2815. }
  2816. else if (Operation == foCustomCommand)
  2817. {
  2818. DebugAssert(Param);
  2819. DebugAssert(Side == osRemote); // Side carries no meaning for foCustomCommand
  2820. DirView(osRemote)->SaveSelectedNames();
  2821. const TCustomCommandType * Command = static_cast<const TCustomCommandType*>(Param);
  2822. CustomCommand(FileList, *Command, NULL);
  2823. Result = true;
  2824. }
  2825. else if ((Operation == foRemoteMove) || (Operation == foRemoteCopy))
  2826. {
  2827. DebugAssert(!IsSideLocalBrowser(Side));
  2828. Result = RemoteTransferFiles(FileList, NoConfirmation,
  2829. (Operation == foRemoteMove), reinterpret_cast<TManagedTerminal *>(Param));
  2830. }
  2831. else if (Operation == foLock)
  2832. {
  2833. DebugAssert(!IsSideLocalBrowser(Side));
  2834. LockFiles(FileList, true);
  2835. Result = true;
  2836. }
  2837. else if (Operation == foUnlock)
  2838. {
  2839. DebugAssert(!IsSideLocalBrowser(Side));
  2840. LockFiles(FileList, false);
  2841. Result = true;
  2842. }
  2843. else
  2844. {
  2845. DebugFail();
  2846. }
  2847. return Result;
  2848. }
  2849. //---------------------------------------------------------------------------
  2850. TOperationSide __fastcall TCustomScpExplorerForm::GetSide(TOperationSide Side)
  2851. {
  2852. if (Side == osCurrent)
  2853. {
  2854. Side = FCurrentSide;
  2855. }
  2856. return Side;
  2857. }
  2858. //---------------------------------------------------------------------------
  2859. TOperationSide TCustomScpExplorerForm::GetOtherSide(TOperationSide Side)
  2860. {
  2861. TOperationSide Result;
  2862. switch (GetSide(Side))
  2863. {
  2864. case osLocal:
  2865. Result = osOther;
  2866. break;
  2867. case osOther:
  2868. Result = osLocal;
  2869. break;
  2870. default:
  2871. DebugFail();
  2872. Abort();
  2873. }
  2874. return Result;
  2875. }
  2876. //---------------------------------------------------------------------------
  2877. bool __fastcall TCustomScpExplorerForm::ExecuteFileOperation(TFileOperation Operation,
  2878. TOperationSide Side, bool OnFocused, bool NoConfirmation, void * Param)
  2879. {
  2880. Side = GetSide(Side);
  2881. bool FullPath = IsSideLocalBrowser(Side);
  2882. TCustomDirView * ADirView = DirView(Side);
  2883. std::unique_ptr<TStringList> FileList(new TStringList());
  2884. ADirView->CreateFileList(OnFocused, FullPath, FileList.get());
  2885. bool ReloadProperties =
  2886. !IsSideLocalBrowser(Side) && (Operation == foSetProperties) &&
  2887. DebugAlwaysTrue(HasActiveTerminal()) && Terminal->IsCapable[fcLoadingAdditionalProperties];
  2888. bool Result = ExecuteFileOperation(Operation, Side, FileList.get(), NoConfirmation, Param);
  2889. if (Result && ReloadProperties)
  2890. {
  2891. std::unique_ptr<TStringList> FileList2(new TStringList());
  2892. FileList->CaseSensitive = true;
  2893. FileList->Sort();
  2894. for (int Index = 0; Index < ADirView->Items->Count; Index++)
  2895. {
  2896. TListItem * Item = ADirView->Items->Item[Index];
  2897. UnicodeString FileName = ADirView->ItemFileName(Item);
  2898. int Index2;
  2899. if (FileList->Find(FileName, Index2))
  2900. {
  2901. FileList2->AddObject(FileName, ADirView->ItemData(Item));
  2902. }
  2903. }
  2904. LoadFilesProperties(FileList2.get());
  2905. }
  2906. return Result;
  2907. }
  2908. //---------------------------------------------------------------------------
  2909. void __fastcall TCustomScpExplorerForm::ExecuteFileOperationCommand(
  2910. TFileOperation Operation, TOperationSide Side, bool OnFocused,
  2911. bool NoConfirmation, void * Param)
  2912. {
  2913. if (ExecuteFileOperation(Operation, Side, OnFocused, NoConfirmation, Param))
  2914. {
  2915. if (TFileOperationProgressType::IsTransferOperation(Operation))
  2916. {
  2917. DebugAssert(!IsLocalBrowserMode());
  2918. if (GetSide(Side) == osLocal)
  2919. {
  2920. Configuration->Usage->Inc(L"UploadsCommand");
  2921. }
  2922. else
  2923. {
  2924. Configuration->Usage->Inc(L"DownloadsCommand");
  2925. }
  2926. }
  2927. }
  2928. }
  2929. //---------------------------------------------------------------------------
  2930. void __fastcall TCustomScpExplorerForm::ExecuteCopyOperationCommand(
  2931. TOperationSide Side, bool OnFocused, unsigned int Flags)
  2932. {
  2933. TTransferOperationParam Param;
  2934. if ((WinConfiguration->Interface != ifCommander) ||
  2935. WinConfiguration->ScpCommander.ExplorerKeyboardShortcuts)
  2936. {
  2937. Flags &= ~cocShortCutHint;
  2938. }
  2939. TCustomDirView * DView = DirView(Side);
  2940. Param.Options =
  2941. FLAGMASK(FLAGSET(Flags, cocShortCutHint), coShortCutHint) |
  2942. FLAGMASK(SelectedAllFilesInDirView(DView), coAllFiles);
  2943. if (FLAGSET(Flags, cocQueue))
  2944. {
  2945. Param.Queue = asOn;
  2946. }
  2947. else if (FLAGSET(Flags, cocNonQueue))
  2948. {
  2949. Param.Queue = asOff;
  2950. }
  2951. ExecuteFileOperationCommand(foCopy, Side, OnFocused, false, &Param);
  2952. }
  2953. //---------------------------------------------------------------------------
  2954. void __fastcall TCustomScpExplorerForm::HandleErrorList(TStringList *& ErrorList)
  2955. {
  2956. int Count = ErrorList->Count;
  2957. try
  2958. {
  2959. if (ErrorList->Count)
  2960. {
  2961. UnicodeString Message = MainInstructions(FMTLOAD(ERROR_LIST_COUNT, (ErrorList->Count)));
  2962. if (MessageDialog(Message, qtError,
  2963. qaOK | qaCancel, HELP_NONE) == qaOK)
  2964. {
  2965. unsigned int Answer;
  2966. int Index = 0;
  2967. do
  2968. {
  2969. DebugAssert(Index >= 0 && Index < Count);
  2970. TQueryButtonAlias Aliases[2];
  2971. Aliases[0].Button = qaYes;
  2972. Aliases[0].Alias = LoadStr(PREV_BUTTON);
  2973. Aliases[1].Button = qaNo;
  2974. Aliases[1].Alias = LoadStr(NEXT_BUTTON);
  2975. TMessageParams Params;
  2976. Params.Aliases = Aliases;
  2977. Params.AliasesCount = LENOF(Aliases);
  2978. int No = Index + 1;
  2979. UnicodeString Message = ErrorList->Strings[Index];
  2980. UnicodeString AMainInstructions;
  2981. UnicodeString Details;
  2982. if (ExtractMainInstructions(Message, AMainInstructions))
  2983. {
  2984. Details = Message;
  2985. Message = AMainInstructions;
  2986. }
  2987. Message = MainInstructions(FMTLOAD(ERROR_LIST_NUMBER, (No, Count, Message))) + Details;
  2988. Answer = MoreMessageDialog(
  2989. Message,
  2990. dynamic_cast<TStrings *>(ErrorList->Objects[Index]), qtError,
  2991. (Index ? qaYes : 0) | (Index < Count - 1 ? qaNo : 0) |
  2992. qaOK, HELP_NONE, &Params);
  2993. if (Answer == qaNo)
  2994. {
  2995. Index++;
  2996. }
  2997. if (Answer == qaYes)
  2998. {
  2999. Index--;
  3000. }
  3001. }
  3002. while (Answer != qaOK);
  3003. }
  3004. }
  3005. }
  3006. __finally
  3007. {
  3008. TStrings * List = ErrorList;
  3009. ErrorList = NULL;
  3010. for (int i = 0; i < Count; i++)
  3011. {
  3012. delete List->Objects[i];
  3013. }
  3014. delete List;
  3015. }
  3016. }
  3017. //---------------------------------------------------------------------------
  3018. void __fastcall TCustomScpExplorerForm::ExecuteRemoteFile(
  3019. const UnicodeString & FullFileName, TRemoteFile * File, TExecuteFileBy ExecuteFileBy)
  3020. {
  3021. // needed for checking filemasks, as there's no directory object
  3022. // associated with the file object
  3023. File->FullFileName = FullFileName;
  3024. TFileMasks::TParams MaskParams;
  3025. MaskParams.Size = File->Size;
  3026. MaskParams.Modification = File->Modification;
  3027. ExecuteFile(osRemote, ExecuteFileBy, NULL, FullFileName, File, MaskParams);
  3028. }
  3029. //---------------------------------------------------------------------------
  3030. void __fastcall TCustomScpExplorerForm::EditNew(TOperationSide Side)
  3031. {
  3032. DebugAssert(!WinConfiguration->DisableOpenEdit);
  3033. Side = GetSide(Side);
  3034. TCustomDirView * DView = DirView(Side);
  3035. TListItem * FocusedItem = DView->ItemFocused;
  3036. UnicodeString Name;
  3037. if ((FocusedItem != NULL) && !DView->ItemIsDirectory(FocusedItem))
  3038. {
  3039. Name = DView->ItemFileName(FocusedItem);
  3040. }
  3041. else
  3042. {
  3043. Name = LoadStr(NEW_FILE);
  3044. }
  3045. UnicodeString Names = Name;
  3046. std::unique_ptr<TStrings> History(CloneStrings(CustomWinConfiguration->History[L"EditFile"]));
  3047. if (InputDialog(LoadStr(EDIT_FILE_CAPTION), LoadStr(EDIT_FILE_PROMPT), Names,
  3048. HELP_EDIT_NEW, History.get(), true, NULL, true, 400))
  3049. {
  3050. while (!Names.IsEmpty())
  3051. {
  3052. Name = CutToChar(Names, FileMasksDelimiters[1], false);
  3053. CustomWinConfiguration->History[L"EditFile"] = History.get();
  3054. UnicodeString TargetFileName;
  3055. UnicodeString LocalFileName;
  3056. UnicodeString RootTempDir;
  3057. UnicodeString TempDir;
  3058. UnicodeString RemoteDirectory;
  3059. bool ExistingFile = false;
  3060. if (!IsSideLocalBrowser(Side))
  3061. {
  3062. Name = AbsolutePath(Terminal->CurrentDirectory, Name);
  3063. std::unique_ptr<TRemoteFile> File(Terminal->TryReadFile(Name));
  3064. if ((File.get() != NULL) &&
  3065. !File->IsDirectory)
  3066. {
  3067. ExecuteRemoteFile(Name, File.get(), efDefaultEditor);
  3068. ExistingFile = true;
  3069. }
  3070. File.reset(NULL);
  3071. if (!ExistingFile)
  3072. {
  3073. RemoteDirectory = UnixExtractFilePath(Name);
  3074. TemporaryDirectoryForRemoteFiles(
  3075. RemoteDirectory, GUIConfiguration->CurrentCopyParam, false, TempDir, RootTempDir);
  3076. TargetFileName = UnixExtractFileName(Name);
  3077. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  3078. LocalFileName = TempDir +
  3079. // We probably do not want to trim the VMS version here
  3080. Terminal->ChangeFileName(&CopyParam, TargetFileName, osRemote, false);
  3081. }
  3082. }
  3083. else
  3084. {
  3085. if (ExtractFilePath(Name).IsEmpty())
  3086. {
  3087. LocalFileName = IncludeTrailingBackslash(DView->PathName) + Name;
  3088. }
  3089. else
  3090. {
  3091. LocalFileName = ExpandFileName(Name);
  3092. }
  3093. TargetFileName = ExtractFileName(Name);
  3094. }
  3095. if (!ExistingFile)
  3096. {
  3097. bool NewFile = !FileExists(ApiPath(LocalFileName));
  3098. if (NewFile)
  3099. {
  3100. int File = FileCreate(ApiPath(LocalFileName));
  3101. if (File < 0)
  3102. {
  3103. if (!RootTempDir.IsEmpty())
  3104. {
  3105. RecursiveDeleteFile(ExcludeTrailingBackslash(RootTempDir));
  3106. }
  3107. throw Exception(FMTLOAD(CREATE_FILE_ERROR, (LocalFileName)));
  3108. }
  3109. else
  3110. {
  3111. FileClose(File);
  3112. }
  3113. }
  3114. TExecuteFileBy ExecuteFileBy = efDefaultEditor;
  3115. const TEditorData * ExternalEditor = NULL;
  3116. TFileMasks::TParams MaskParams; // size not known
  3117. ExecuteFileNormalize(ExecuteFileBy, ExternalEditor, TargetFileName,
  3118. false, MaskParams);
  3119. CustomExecuteFile(Side, ExecuteFileBy, LocalFileName, TargetFileName,
  3120. ExternalEditor, RootTempDir, RemoteDirectory, NewFile, TDateTime());
  3121. }
  3122. }
  3123. }
  3124. }
  3125. //---------------------------------------------------------------------------
  3126. bool __fastcall TCustomScpExplorerForm::RemoteExecuteForceText(
  3127. TExecuteFileBy ExecuteFileBy, const TEditorData * ExternalEditor)
  3128. {
  3129. DebugAssert((ExecuteFileBy == efExternalEditor) ==
  3130. ((ExternalEditor != NULL) && (ExternalEditor->Editor == edExternal)));
  3131. DebugAssert(ExecuteFileBy != efDefaultEditor);
  3132. return
  3133. ((ExecuteFileBy == efInternalEditor)) ||
  3134. ((ExecuteFileBy == efExternalEditor) && ExternalEditor->ExternalEditorText);
  3135. }
  3136. //---------------------------------------------------------------------------
  3137. void __fastcall TCustomScpExplorerForm::CustomExecuteFile(TOperationSide Side,
  3138. TExecuteFileBy ExecuteFileBy, UnicodeString FileName, UnicodeString OriginalFileName,
  3139. const TEditorData * ExternalEditor, UnicodeString LocalRootDirectory,
  3140. UnicodeString RemoteDirectory, bool NewFile, const TDateTime & SourceTimestamp)
  3141. {
  3142. DebugAssert(!WinConfiguration->DisableOpenEdit);
  3143. DebugAssert((ExecuteFileBy == efExternalEditor) ==
  3144. ((ExternalEditor != NULL) && (ExternalEditor->Editor == edExternal)));
  3145. DebugAssert(ExecuteFileBy != efDefaultEditor);
  3146. Side = GetSide(Side);
  3147. std::unique_ptr<TEditedFileData> Data(new TEditedFileData);
  3148. if (!IsSideLocalBrowser(Side))
  3149. {
  3150. Data->Terminal = Terminal;
  3151. Data->SessionData = CloneCurrentSessionData();
  3152. Data->ForceText = RemoteExecuteForceText(ExecuteFileBy, ExternalEditor);
  3153. Data->RemoteDirectory = RemoteDirectory;
  3154. Data->SessionName = Terminal->SessionData->SessionName;
  3155. Data->LocalRootDirectory = LocalRootDirectory;
  3156. Data->OriginalFileName = OriginalFileName;
  3157. Data->Command = L""; // will be changed later for external editor
  3158. // Empty SourceTimestamp indicates that checking was disabled when the file was opened
  3159. if (WinConfiguration->EditorCheckNotModified)
  3160. {
  3161. Data->SourceTimestamp = SourceTimestamp;
  3162. }
  3163. }
  3164. if (ExecuteFileBy == efInternalEditor)
  3165. {
  3166. if (!IsSideLocalBrowser(Side))
  3167. {
  3168. UnicodeString RemotePath = UnixCombinePaths(RemoteDirectory, OriginalFileName);
  3169. UnicodeString Caption = RemotePath + TitleSeparator + Terminal->SessionData->SessionName;
  3170. TForm * Editor;
  3171. try
  3172. {
  3173. Editor =
  3174. ShowEditorForm(
  3175. FileName, this,
  3176. FEditorManager->FileChanged, FEditorManager->FileReload, FEditorManager->FileClosed,
  3177. SaveAllInternalEditors, AnyInternalEditorModified,
  3178. Caption, StandaloneOperation, SessionColor, Terminal->SessionData->InternalEditorEncoding, NewFile);
  3179. }
  3180. catch(...)
  3181. {
  3182. if (!LocalRootDirectory.IsEmpty())
  3183. {
  3184. RecursiveDeleteFile(ExcludeTrailingBackslash(LocalRootDirectory));
  3185. }
  3186. throw;
  3187. }
  3188. FEditorManager->AddFileInternal(FileName, Data.release(), Editor);
  3189. AppLogFmt(L"Opened internal editor with \"%s\"", (FileName));
  3190. }
  3191. else
  3192. {
  3193. DebugAssert(!StandaloneOperation);
  3194. TForm * Editor =
  3195. ShowEditorForm(FileName, this, NULL, NULL, LocalEditorClosed,
  3196. SaveAllInternalEditors, AnyInternalEditorModified,
  3197. L"", false, SessionColor, -1, NewFile);
  3198. FLocalEditors->Add(Editor);
  3199. }
  3200. }
  3201. else
  3202. {
  3203. HANDLE Process;
  3204. if (ExecuteFileBy == efExternalEditor)
  3205. {
  3206. UnicodeString Program, Params, Dir;
  3207. Data->Command = ExternalEditor->ExternalEditor;
  3208. ReformatFileNameCommand(Data->Command);
  3209. SplitCommand(Data->Command, Program, Params, Dir);
  3210. Params = ExpandFileNameCommand(Params, FileName);
  3211. Program = ExpandEnvironmentVariables(Program);
  3212. if (!ExecuteShell(Program, Params, Process))
  3213. {
  3214. throw EOSExtException(FMTLOAD(EDITOR_ERROR, (Program)));
  3215. }
  3216. }
  3217. else
  3218. {
  3219. DebugAssert(!IsSideLocalBrowser(Side));
  3220. if (!ExecuteShell(FileName, L"", Process))
  3221. {
  3222. throw EOSExtException(FMTLOAD(EXECUTE_FILE_ERROR, (FileName)));
  3223. }
  3224. }
  3225. if (IsSideLocalBrowser(Side) ||
  3226. ((ExecuteFileBy == efShell) &&
  3227. !WinConfiguration->Editor.SDIShellEditor) ||
  3228. ((ExecuteFileBy == efExternalEditor) &&
  3229. !ExternalEditor->SDIExternalEditor))
  3230. {
  3231. // no need for handle
  3232. if (Process != NULL)
  3233. {
  3234. DebugCheck(CloseHandle(Process));
  3235. }
  3236. Process = INVALID_HANDLE_VALUE;
  3237. }
  3238. else
  3239. {
  3240. if (Process == NULL)
  3241. {
  3242. throw ExtException(LoadStr(OPEN_FILE_NO_PROCESS2), L"", HELP_OPEN_FILE_NO_PROCESS);
  3243. }
  3244. }
  3245. if (!IsSideLocalBrowser(Side))
  3246. {
  3247. FEditorManager->AddFileExternal(FileName, Data.release(), Process);
  3248. AppLogFmt(L"Opened external editor with remote file \"%s\".", (FileName));
  3249. }
  3250. }
  3251. }
  3252. //---------------------------------------------------------------------------
  3253. void __fastcall TCustomScpExplorerForm::SaveInternalEditor(
  3254. const UnicodeString /*FileName*/, TEditedFileData * /*Data*/, TObject * Token,
  3255. void * /*Arg*/)
  3256. {
  3257. if (Token != NULL)
  3258. {
  3259. EditorFormFileSave(static_cast<TForm *>(Token));
  3260. }
  3261. }
  3262. //---------------------------------------------------------------------------
  3263. void __fastcall TCustomScpExplorerForm::SaveAllInternalEditors(TObject * /*Sender*/)
  3264. {
  3265. for (int Index = 0; Index < FLocalEditors->Count; Index++)
  3266. {
  3267. EditorFormFileSave(static_cast<TForm *>(FLocalEditors->Items[Index]));
  3268. }
  3269. FEditorManager->ProcessFiles(SaveInternalEditor, NULL);
  3270. }
  3271. //---------------------------------------------------------------------------
  3272. void __fastcall TCustomScpExplorerForm::InternalEditorModified(
  3273. const UnicodeString /*FileName*/, TEditedFileData * /*Data*/, TObject * Token,
  3274. void * Arg)
  3275. {
  3276. if ((Token != NULL) &&
  3277. IsEditorFormModified(static_cast<TForm *>(Token)))
  3278. {
  3279. *static_cast<bool *>(Arg) = true;
  3280. }
  3281. }
  3282. //---------------------------------------------------------------------------
  3283. void __fastcall TCustomScpExplorerForm::AnyInternalEditorModified(
  3284. TObject * /*Sender*/, bool & Modified)
  3285. {
  3286. for (int Index = 0; !Modified && (Index < FLocalEditors->Count); Index++)
  3287. {
  3288. if (IsEditorFormModified(static_cast<TForm *>(FLocalEditors->Items[Index])))
  3289. {
  3290. Modified = true;
  3291. }
  3292. }
  3293. if (!Modified)
  3294. {
  3295. FEditorManager->ProcessFiles(InternalEditorModified, &Modified);
  3296. }
  3297. }
  3298. //---------------------------------------------------------------------------
  3299. void __fastcall TCustomScpExplorerForm::LocalEditorClosed(TObject * Sender, bool /*Forced*/)
  3300. {
  3301. DebugCheck(FLocalEditors->Extract(Sender) >= 0);
  3302. }
  3303. //---------------------------------------------------------------------------
  3304. void __fastcall TCustomScpExplorerForm::TemporaryDirectoryForRemoteFiles(
  3305. const UnicodeString & RemoteDirectory, const TCopyParamType & CopyParam, bool Simple,
  3306. UnicodeString & Result, UnicodeString & RootDirectory)
  3307. {
  3308. if (!WinConfiguration->TemporaryDirectoryDeterministic || Simple)
  3309. {
  3310. RootDirectory = IncludeTrailingBackslash(WinConfiguration->TemporaryDir());
  3311. Result = RootDirectory;
  3312. }
  3313. else
  3314. {
  3315. RootDirectory = L"";
  3316. Result = WinConfiguration->ExpandedTemporaryDirectory();
  3317. Result = IncludeTrailingBackslash(Result);
  3318. }
  3319. if (WinConfiguration->TemporaryDirectoryAppendSession && !Simple)
  3320. {
  3321. Result = IncludeTrailingBackslash(Result + MakeValidFileName(Terminal->SessionData->SessionName));
  3322. }
  3323. if (WinConfiguration->TemporaryDirectoryAppendPath && !Simple)
  3324. {
  3325. UnicodeString ARemoteDirectory = RemoteDirectory;
  3326. if (!ARemoteDirectory.IsEmpty() && (ARemoteDirectory[1] == L'/'))
  3327. {
  3328. ARemoteDirectory.Delete(1, 1);
  3329. }
  3330. Result = IncludeTrailingBackslash(Result + CopyParam.ValidLocalPath(FromUnixPath(ARemoteDirectory)));
  3331. }
  3332. if (!ForceDirectories(ApiPath(Result)))
  3333. {
  3334. throw EOSExtException(FMTLOAD(CREATE_TEMP_DIR_ERROR, (Result)));
  3335. }
  3336. }
  3337. //---------------------------------------------------------------------------
  3338. void __fastcall TCustomScpExplorerForm::TemporarilyDownloadFiles(
  3339. TStrings * FileList, bool ForceText, UnicodeString & RootTempDir, UnicodeString & TempDir,
  3340. bool GetTargetNames, bool AutoOperation, bool SimpleTempDir)
  3341. {
  3342. DebugAssert(!IsLocalBrowserMode());
  3343. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  3344. if (ForceText)
  3345. {
  3346. CopyParam.TransferMode = tmAscii;
  3347. }
  3348. TemporaryFileCopyParam(CopyParam);
  3349. if (TempDir.IsEmpty())
  3350. {
  3351. TemporaryDirectoryForRemoteFiles(Terminal->CurrentDirectory, CopyParam, SimpleTempDir, TempDir, RootTempDir);
  3352. }
  3353. DebugAssert(!FAutoOperation);
  3354. FAutoOperation = AutoOperation;
  3355. Terminal->ExceptionOnFail = true;
  3356. try
  3357. {
  3358. try
  3359. {
  3360. // turn off confirmations, as for MDI editors we may possibly download
  3361. // the same file over
  3362. Terminal->CopyToLocal(
  3363. FileList, TempDir, &CopyParam, cpNoConfirmation | cpTemporary, NULL);
  3364. if (GetTargetNames)
  3365. {
  3366. for (int i = 0; i < FileList->Count; i++)
  3367. {
  3368. FileList->Strings[i] =
  3369. Terminal->ChangeFileName(&CopyParam, UnixExtractFileName(FileList->Strings[i]), osRemote, false);
  3370. }
  3371. }
  3372. }
  3373. catch(...)
  3374. {
  3375. if (!RootTempDir.IsEmpty())
  3376. {
  3377. RecursiveDeleteFile(ExcludeTrailingBackslash(RootTempDir));
  3378. }
  3379. throw;
  3380. }
  3381. }
  3382. __finally
  3383. {
  3384. FAutoOperation = false;
  3385. Terminal->ExceptionOnFail = false;
  3386. }
  3387. }
  3388. //---------------------------------------------------------------------------
  3389. void __fastcall TCustomScpExplorerForm::EditorAutoConfig()
  3390. {
  3391. // Do not waste time checking for default editor list the next time,
  3392. // or testing if default editor is not notepad.
  3393. bool TryNextTime = false;
  3394. UnicodeString UsageState;
  3395. if (!WinConfiguration->EditorList->IsDefaultList())
  3396. {
  3397. UsageState = "H";
  3398. }
  3399. else
  3400. {
  3401. UnicodeString Executable;
  3402. UnicodeString ExecutableDescription;
  3403. if (DetectSystemExternalEditor(false, Executable, ExecutableDescription, UsageState, TryNextTime))
  3404. {
  3405. UnicodeString Message =
  3406. FMTLOAD(EDITOR_AUTO_CONFIG2, (ExecutableDescription, ExecutableDescription));
  3407. unsigned int Answer =
  3408. MessageDialog(Message, qtConfirmation, qaOK | qaCancel, HELP_EDITOR_AUTO_CONFIG);
  3409. if (Answer != qaOK)
  3410. {
  3411. UsageState = "R";
  3412. }
  3413. else
  3414. {
  3415. UsageState = "A";
  3416. TEditorData EditorData;
  3417. EditorData.Editor = edExternal;
  3418. EditorData.ExternalEditor = FormatCommand(Executable, L"");
  3419. EditorData.ExternalEditorOptionsAutodetect();
  3420. TEditorList EditorList;
  3421. EditorList = *WinConfiguration->EditorList;
  3422. EditorList.Insert(0, new TEditorPreferences(EditorData));
  3423. WinConfiguration->EditorList = &EditorList;
  3424. }
  3425. }
  3426. }
  3427. WinConfiguration->OfferedEditorAutoConfig = !TryNextTime;
  3428. WinConfiguration->Usage->Set(L"EditorAutoConfig", UsageState);
  3429. }
  3430. //---------------------------------------------------------------------------
  3431. void __fastcall TCustomScpExplorerForm::ExecuteFileNormalize(
  3432. TExecuteFileBy & ExecuteFileBy, const TEditorData *& ExternalEditor,
  3433. const UnicodeString & FileName, bool Local, const TFileMasks::TParams & MaskParams)
  3434. {
  3435. if (ExecuteFileBy == efDefaultEditor)
  3436. {
  3437. if (!WinConfiguration->OfferedEditorAutoConfig)
  3438. {
  3439. EditorAutoConfig();
  3440. }
  3441. const TEditorPreferences * Editor =
  3442. WinConfiguration->DefaultEditorForFile(FileName, Local, MaskParams);
  3443. if ((Editor == NULL) || (Editor->Data->Editor == edInternal))
  3444. {
  3445. ExecuteFileBy = efInternalEditor;
  3446. ExternalEditor = NULL;
  3447. }
  3448. else if (Editor->Data->Editor == edOpen)
  3449. {
  3450. ExecuteFileBy = efShell;
  3451. ExternalEditor = NULL;
  3452. }
  3453. else
  3454. {
  3455. ExecuteFileBy = efExternalEditor;
  3456. ExternalEditor = Editor->Data;
  3457. }
  3458. }
  3459. }
  3460. //---------------------------------------------------------------------------
  3461. void __fastcall TCustomScpExplorerForm::ExecuteFile(TOperationSide Side,
  3462. TExecuteFileBy ExecuteFileBy, const TEditorData * ExternalEditor,
  3463. UnicodeString FullFileName, TObject * Object, const TFileMasks::TParams & MaskParams)
  3464. {
  3465. UnicodeString OriginalFileName;
  3466. UnicodeString LocalRootDirectory;
  3467. UnicodeString RemoteDirectory;
  3468. ExecuteFileNormalize(ExecuteFileBy, ExternalEditor, FullFileName,
  3469. IsSideLocalBrowser(Side), MaskParams);
  3470. UnicodeString Counter;
  3471. UnicodeString LocalFileName;
  3472. bool Handled = false;
  3473. if (!IsSideLocalBrowser(Side))
  3474. {
  3475. // We need to trim VMS version here, so that we use name without version
  3476. // when uploading back to create a new version of the file
  3477. OriginalFileName = Terminal->GetBaseFileName(UnixExtractFileName(FullFileName));
  3478. RemoteDirectory = UnixExtractFilePath(FullFileName);
  3479. TObject * Token = NULL;
  3480. UnicodeString LocalDirectory;
  3481. if (!FEditorManager->CanAddFile(RemoteDirectory, OriginalFileName,
  3482. Terminal->SessionData->SessionName, Token, LocalRootDirectory,
  3483. LocalDirectory))
  3484. {
  3485. if (Token != NULL)
  3486. {
  3487. TForm * Form = dynamic_cast<TForm *>(Token);
  3488. if (Form->WindowState == wsMinimized)
  3489. {
  3490. ShowWindow(Form->Handle, SW_RESTORE);
  3491. }
  3492. else
  3493. {
  3494. Form->SetFocus();
  3495. }
  3496. Handled = true;
  3497. }
  3498. else
  3499. {
  3500. throw Exception(FMTLOAD(ALREADY_EDITED_EXTERNALLY_OR_UPLOADED, (OriginalFileName)));
  3501. }
  3502. }
  3503. if (!Handled)
  3504. {
  3505. TStringList * FileList1 = new TStringList();
  3506. try
  3507. {
  3508. FileList1->AddObject(FullFileName, Object);
  3509. TemporarilyDownloadFiles(FileList1,
  3510. RemoteExecuteForceText(ExecuteFileBy, ExternalEditor),
  3511. LocalRootDirectory, LocalDirectory, true, true, false);
  3512. LocalFileName = LocalDirectory + FileList1->Strings[0];
  3513. }
  3514. __finally
  3515. {
  3516. delete FileList1;
  3517. }
  3518. switch (ExecuteFileBy)
  3519. {
  3520. case efShell:
  3521. Counter = "RemoteFilesExecuted";
  3522. break;
  3523. case efInternalEditor:
  3524. Counter = "RemoteFilesOpenedInInternalEditor";
  3525. break;
  3526. case efExternalEditor:
  3527. Counter = "RemoteFilesOpenedInExternalEditor";
  3528. break;
  3529. default:
  3530. DebugFail();
  3531. }
  3532. }
  3533. }
  3534. else
  3535. {
  3536. LocalFileName = FullFileName;
  3537. OriginalFileName = ExtractFileName(FullFileName);
  3538. switch (ExecuteFileBy)
  3539. {
  3540. case efShell:
  3541. Counter = "LocalFilesExecuted";
  3542. break;
  3543. case efInternalEditor:
  3544. Counter = "LocalFilesOpenedInInternalEditor";
  3545. break;
  3546. case efExternalEditor:
  3547. Counter = "LocalFilesOpenedInExternalEditor";
  3548. break;
  3549. default:
  3550. DebugFail();
  3551. }
  3552. }
  3553. if (!Handled)
  3554. {
  3555. Configuration->Usage->Inc(Counter);
  3556. CustomExecuteFile(Side, ExecuteFileBy, LocalFileName, OriginalFileName,
  3557. ExternalEditor, LocalRootDirectory, RemoteDirectory, false, MaskParams.Modification);
  3558. }
  3559. }
  3560. //---------------------------------------------------------------------------
  3561. void __fastcall TCustomScpExplorerForm::ExecuteFile(TOperationSide Side,
  3562. TExecuteFileBy ExecuteFileBy, const TEditorData * ExternalEditor,
  3563. bool AllSelected, bool OnFocused)
  3564. {
  3565. DebugAssert(!WinConfiguration->DisableOpenEdit);
  3566. DebugAssert((ExecuteFileBy == efExternalEditor) ==
  3567. ((ExternalEditor != NULL) && (ExternalEditor->Editor == edExternal)));
  3568. Side = GetSide(Side);
  3569. TCustomDirView * DView = DirView(Side);
  3570. TStrings * FileList =
  3571. AllSelected ?
  3572. DView->CreateFileList(OnFocused, IsSideLocalBrowser(Side)) :
  3573. DView->CreateFocusedFileList(IsSideLocalBrowser(Side));
  3574. try
  3575. {
  3576. DebugAssert(AllSelected || (FileList->Count == 1));
  3577. for (int i = 0; i < FileList->Count; i++)
  3578. {
  3579. UnicodeString ListFileName = FileList->Strings[i];
  3580. UnicodeString FileNameOnly =
  3581. !IsSideLocalBrowser(Side) ? UnixExtractFileName(ListFileName) : ExtractFileName(ListFileName);
  3582. TListItem * Item = DView->FindFileItem(FileNameOnly);
  3583. if (!DView->ItemIsDirectory(Item))
  3584. {
  3585. UnicodeString FullFileName;
  3586. if (!IsSideLocalBrowser(Side))
  3587. {
  3588. FullFileName = DirView(Side)->Path + ListFileName;
  3589. }
  3590. else
  3591. {
  3592. FullFileName = ListFileName;
  3593. }
  3594. TObject * Object = FileList->Objects[i];
  3595. TFileMasks::TParams MaskParams;
  3596. MaskParams.Size = DView->ItemFileSize(Item);
  3597. TDateTimePrecision Precision;
  3598. MaskParams.Modification = DView->ItemFileTime(Item, Precision);
  3599. std::unique_ptr<TRemoteFile> File;
  3600. if (!IsSideLocalBrowser(Side))
  3601. {
  3602. File.reset(Terminal->TryReadFile(FullFileName));
  3603. if (File.get() != NULL)
  3604. {
  3605. File->FullFileName = FullFileName;
  3606. Object = File.get();
  3607. MaskParams.Size = File->Size;
  3608. MaskParams.Modification = File->Modification;
  3609. }
  3610. }
  3611. ExecuteFile(Side, ExecuteFileBy, ExternalEditor, FullFileName, Object, MaskParams);
  3612. }
  3613. }
  3614. }
  3615. __finally
  3616. {
  3617. delete FileList;
  3618. }
  3619. }
  3620. //---------------------------------------------------------------------------
  3621. void __fastcall TCustomScpExplorerForm::TemporaryFileCopyParam(TCopyParamType & CopyParam)
  3622. {
  3623. CopyParam.FileNameCase = ncNoChange;
  3624. CopyParam.PreserveRights = false;
  3625. CopyParam.PreserveReadOnly = false;
  3626. CopyParam.ReplaceInvalidChars = true;
  3627. CopyParam.IncludeFileMask = TFileMasks();
  3628. CopyParam.NewerOnly = false;
  3629. CopyParam.FileMask = L"";
  3630. }
  3631. //---------------------------------------------------------------------------
  3632. bool TCustomScpExplorerForm::EditorCheckNotModified(const TEditedFileData * Data)
  3633. {
  3634. return WinConfiguration->EditorCheckNotModified && (Data->SourceTimestamp != TDateTime());
  3635. }
  3636. //---------------------------------------------------------------------------
  3637. class TEditorUploadQueueItem : public TUploadQueueItem
  3638. {
  3639. public:
  3640. __fastcall TEditorUploadQueueItem(
  3641. TTerminal * Terminal, TStrings * FilesToCopy, const UnicodeString & TargetDir,
  3642. const TCopyParamType * CopyParam, int Params) :
  3643. TUploadQueueItem(Terminal, FilesToCopy, TargetDir, CopyParam, Params, true, false)
  3644. {
  3645. }
  3646. protected:
  3647. virtual void __fastcall DoTransferExecute(TTerminal * Terminal, TParallelOperation * ParallelOperation)
  3648. {
  3649. TUploadQueueItem::DoTransferExecute(Terminal, ParallelOperation);
  3650. TTerminalManager::Instance()->ScpExplorer->EditedFileUploaded(Terminal, CompleteEvent);
  3651. }
  3652. };
  3653. //---------------------------------------------------------------------------
  3654. void TCustomScpExplorerForm::EditedFileUploaded(TTerminal * ATerminal, HANDLE UploadCompleteEvent)
  3655. {
  3656. if (WinConfiguration->EditorCheckNotModified) // optimization
  3657. {
  3658. UnicodeString RemoteFilePath;
  3659. {
  3660. TGuard Guard(FEditorManager->Section);
  3661. TEditedFileData * Data = FEditorManager->FindByUploadCompleteEvent(UploadCompleteEvent);
  3662. if (DebugAlwaysTrue(Data != NULL) &&
  3663. EditorCheckNotModified(Data))
  3664. {
  3665. RemoteFilePath = UnixCombinePaths(Data->RemoteDirectory, Data->OriginalFileName);
  3666. }
  3667. }
  3668. if (!RemoteFilePath.IsEmpty())
  3669. {
  3670. std::unique_ptr<TRemoteFile> File(ATerminal->TryReadFile(RemoteFilePath));
  3671. if (File.get() != NULL)
  3672. {
  3673. TGuard Guard(FEditorManager->Section);
  3674. TEditedFileData * Data = FEditorManager->FindByUploadCompleteEvent(UploadCompleteEvent);
  3675. if (DebugAlwaysTrue(Data != NULL))
  3676. {
  3677. Data->SourceTimestamp = File->Modification;
  3678. AppLogFmt(L"Uploaded edited remote file timestamp: %s", (StandardTimestamp(Data->SourceTimestamp)));
  3679. }
  3680. }
  3681. }
  3682. }
  3683. }
  3684. //---------------------------------------------------------------------------
  3685. void __fastcall TCustomScpExplorerForm::ExecutedFileChanged(
  3686. const UnicodeString & FileName, TEditedFileData * Data, HANDLE UploadCompleteEvent, bool & Retry)
  3687. {
  3688. TTerminalManager * Manager = TTerminalManager::Instance();
  3689. if ((Manager->ActiveTerminal == Data->Terminal) &&
  3690. Manager->ScheduleTerminalReconnnect(Data->Terminal))
  3691. {
  3692. AppLog(L"Scheduled session reconnect, will retry the upload later");
  3693. Retry = true;
  3694. }
  3695. else if (!IsActiveTerminal(Data->Terminal))
  3696. {
  3697. if (!NonVisualDataModule->Busy)
  3698. {
  3699. UnicodeString FileNameOnly = ExtractFileName(FileName);
  3700. UnicodeString EditInactiveSessionReopenAcceptedCounter = L"EditInactiveSessionReopenAccepted";
  3701. UnicodeString EditInactiveSessionReopenRejectedCounter = L"EditInactiveSessionReopenRejected";
  3702. if (Data->Terminal == NULL)
  3703. {
  3704. TManagedTerminal * SameSiteTerminal = Manager->FindActiveTerminalForSite(Data->SessionData);
  3705. if (SameSiteTerminal != NULL)
  3706. {
  3707. UnicodeString Message =
  3708. FMTLOAD(EDIT_SESSION_REATTACH,
  3709. (FileNameOnly, Data->SessionName, FileNameOnly));
  3710. if (MessageDialog(Message, qtConfirmation, qaOK | qaCancel) == qaOK)
  3711. {
  3712. Data->Terminal = SameSiteTerminal;
  3713. Data->SessionName = SameSiteTerminal->SessionData->SessionName;
  3714. // We might also overwrite session data
  3715. Configuration->Usage->Inc(EditInactiveSessionReopenAcceptedCounter);
  3716. }
  3717. else
  3718. {
  3719. Configuration->Usage->Inc(EditInactiveSessionReopenRejectedCounter);
  3720. Abort();
  3721. }
  3722. }
  3723. }
  3724. // Foreground session should reconnect itself (except for those disconnected explicitly by the user)
  3725. else if (DebugAlwaysTrue(Terminal->Disconnected || (Terminal != Data->Terminal)))
  3726. {
  3727. UnicodeString Message =
  3728. MainInstructions(
  3729. FMTLOAD(EDIT_SESSION_RECONNECT, (Data->SessionName, FileNameOnly)));
  3730. if (MessageDialog(Message, qtConfirmation, qaOK | qaCancel) == qaOK)
  3731. {
  3732. if (Terminal->Disconnected)
  3733. {
  3734. Manager->ActiveSession = Terminal;
  3735. ReconnectSession();
  3736. // Was it reconnected? (if not, Data->Terminal should be nulled now)
  3737. if (!IsActiveTerminal(Data->Terminal) || (Terminal != Data->Terminal))
  3738. {
  3739. Abort();
  3740. }
  3741. }
  3742. else
  3743. {
  3744. Manager->SetActiveTerminalWithAutoReconnect(Data->Terminal);
  3745. }
  3746. Configuration->Usage->Inc(EditInactiveSessionReopenAcceptedCounter);
  3747. }
  3748. else
  3749. {
  3750. Configuration->Usage->Inc(EditInactiveSessionReopenRejectedCounter);
  3751. Abort();
  3752. }
  3753. }
  3754. }
  3755. if (!IsActiveTerminal(Data->Terminal))
  3756. {
  3757. Configuration->Usage->Inc(L"EditInactiveSession");
  3758. // Prevent this when not idle (!NonVisualDataModule->Busy)?
  3759. throw Exception(FMTLOAD(EDIT_SESSION_CLOSED2,
  3760. (ExtractFileName(FileName), Data->SessionName)));
  3761. }
  3762. }
  3763. if (!Retry && EditorCheckNotModified(Data))
  3764. {
  3765. if (NonVisualDataModule->Busy)
  3766. {
  3767. Retry = true;
  3768. }
  3769. else
  3770. {
  3771. UnicodeString RemoteFilePath = UnixCombinePaths(Data->RemoteDirectory, Data->OriginalFileName);
  3772. std::unique_ptr<TRemoteFile> File(Terminal->TryReadFile(RemoteFilePath));
  3773. if (File.get() != NULL)
  3774. {
  3775. AppLogFmt(L"Edited remote file timestamp: %s, Original timestamp: %s", (StandardTimestamp(File->Modification), StandardTimestamp(Data->SourceTimestamp)));
  3776. if (File->Modification != Data->SourceTimestamp)
  3777. {
  3778. UnicodeString Message = MainInstructions(LoadStr(EDIT_CHANGED_EXTERNALLY));
  3779. if (MessageDialog(Message, qtConfirmation, qaOK | qaCancel) != qaOK)
  3780. {
  3781. Abort();
  3782. }
  3783. }
  3784. }
  3785. }
  3786. }
  3787. if (!Retry)
  3788. {
  3789. TStrings * FileList = new TStringList();
  3790. try
  3791. {
  3792. FileList->Add(FileName);
  3793. // Consider using the same settings (preset) as when the file was downloaded.
  3794. // More over this does not reflect the actual session that will do the upload.
  3795. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  3796. TemporaryFileCopyParam(CopyParam);
  3797. if (Data->ForceText)
  3798. {
  3799. CopyParam.TransferMode = tmAscii;
  3800. }
  3801. // so i do not need to worry if masking algorithm works in all cases
  3802. // ("" means "copy file name", no masking is actually done)
  3803. if (ExtractFileName(FileName) == Data->OriginalFileName)
  3804. {
  3805. CopyParam.FileMask = L"";
  3806. }
  3807. else
  3808. {
  3809. CopyParam.FileMask = DelimitFileNameMask(Data->OriginalFileName);
  3810. }
  3811. int Params = cpNoConfirmation | cpTemporary;
  3812. if (Data->Terminal->IsCapable[fcBackgroundTransfers])
  3813. {
  3814. TTerminalQueue * AQueue = Manager->FindQueueForTerminal(Data->Terminal);
  3815. TQueueItem * QueueItem =
  3816. new TEditorUploadQueueItem(Data->Terminal, FileList, Data->RemoteDirectory, &CopyParam, Params);
  3817. QueueItem->CompleteEvent = UploadCompleteEvent;
  3818. AddQueueItem(AQueue, QueueItem, Data->Terminal);
  3819. }
  3820. else
  3821. {
  3822. if (NonVisualDataModule->Busy)
  3823. {
  3824. Retry = true;
  3825. }
  3826. else
  3827. {
  3828. Data->Terminal->CopyToRemote(FileList, Data->RemoteDirectory, &CopyParam, Params, NULL);
  3829. SetEvent(UploadCompleteEvent);
  3830. }
  3831. }
  3832. }
  3833. __finally
  3834. {
  3835. delete FileList;
  3836. }
  3837. }
  3838. }
  3839. //---------------------------------------------------------------------------
  3840. void __fastcall TCustomScpExplorerForm::ExecutedFileReload(
  3841. const UnicodeString & FileName, TEditedFileData * Data)
  3842. {
  3843. // Sanity check, we should not be busy otherwise user would not be able to click Reload button.
  3844. DebugAssert(!NonVisualDataModule->Busy);
  3845. if (!IsActiveTerminal(Data->Terminal))
  3846. {
  3847. throw Exception(FMTLOAD(EDIT_SESSION_CLOSED_RELOAD,
  3848. (ExtractFileName(FileName), Data->SessionName)));
  3849. }
  3850. TManagedTerminal * PrevSession = TTerminalManager::Instance()->ActiveSession;
  3851. TTerminalManager::Instance()->ActiveSession = Data->Terminal;
  3852. NonVisualDataModule->StartBusy();
  3853. try
  3854. {
  3855. std::unique_ptr<TRemoteFile> File;
  3856. UnicodeString RemoteFileName =
  3857. UnixIncludeTrailingBackslash(Data->RemoteDirectory) + Data->OriginalFileName;
  3858. Terminal->ExceptionOnFail = true;
  3859. try
  3860. {
  3861. File.reset(Terminal->ReadFile(RemoteFileName));
  3862. if (!File->HaveFullFileName)
  3863. {
  3864. File->FullFileName = RemoteFileName;
  3865. }
  3866. }
  3867. __finally
  3868. {
  3869. Terminal->ExceptionOnFail = false;
  3870. }
  3871. std::unique_ptr<TStrings> FileList(new TStringList());
  3872. FileList->AddObject(RemoteFileName, File.get());
  3873. UnicodeString RootTempDir = Data->LocalRootDirectory;
  3874. UnicodeString TempDir = ExtractFilePath(FileName);
  3875. TemporarilyDownloadFiles(FileList.get(), Data->ForceText, RootTempDir, TempDir, true, true, false);
  3876. if (EditorCheckNotModified(Data))
  3877. {
  3878. Data->SourceTimestamp = File->Modification;
  3879. }
  3880. // sanity check, the target file name should be still the same
  3881. DebugAssert(ExtractFileName(FileName) == FileList->Strings[0]);
  3882. }
  3883. __finally
  3884. {
  3885. NonVisualDataModule->EndBusy();
  3886. // it actually may not exist anymore...
  3887. TTerminalManager::Instance()->ActiveSession = PrevSession;
  3888. }
  3889. }
  3890. //---------------------------------------------------------------------------
  3891. void __fastcall TCustomScpExplorerForm::ExecutedFileEarlyClosed(
  3892. const TEditedFileData * Data, bool & KeepOpen)
  3893. {
  3894. // Command is set for external editors only (not for "shell" open).
  3895. if (!Data->Command.IsEmpty())
  3896. {
  3897. bool AnyFound = false;
  3898. bool AnyMDI = false;
  3899. bool AnyNonMDI = false;
  3900. bool AnyDetect = false;
  3901. TEditorList * EditorList = new TEditorList();
  3902. try
  3903. {
  3904. *EditorList = *WinConfiguration->EditorList;
  3905. for (int i = 0; i < EditorList->Count; i++)
  3906. {
  3907. const TEditorPreferences * Editor = EditorList->Editors[i];
  3908. if ((Editor->Data->Editor == edExternal) &&
  3909. (Editor->Data->ExternalEditor == Data->Command))
  3910. {
  3911. AnyFound = true;
  3912. if (Editor->Data->SDIExternalEditor)
  3913. {
  3914. AnyNonMDI = true;
  3915. if (Editor->Data->DetectMDIExternalEditor)
  3916. {
  3917. AnyDetect = true;
  3918. }
  3919. }
  3920. else
  3921. {
  3922. AnyMDI = true;
  3923. }
  3924. }
  3925. }
  3926. bool EnableMDI = false;
  3927. bool DisableDetect = false;
  3928. if (AnyMDI)
  3929. {
  3930. KeepOpen = true;
  3931. if (AnyNonMDI)
  3932. {
  3933. // there is at least one instance of the editor with MDI support enabled,
  3934. // and one with disabled, enable it for all instances
  3935. EnableMDI = true;
  3936. }
  3937. }
  3938. else if (AnyFound && !AnyDetect)
  3939. {
  3940. // at least once instance found but all have MDI autodetection disabled
  3941. // => close the file (default action)
  3942. }
  3943. else
  3944. {
  3945. // no instance of the editor has MDI support enabled
  3946. TMessageParams Params;
  3947. if (AnyFound)
  3948. {
  3949. // there is at least one instance of the editor
  3950. Params.Params |= mpNeverAskAgainCheck;
  3951. }
  3952. unsigned int Answer = MessageDialog(FMTLOAD(EDITOR_EARLY_CLOSED2, (Data->OriginalFileName)), qtWarning,
  3953. qaYes | qaNo, HELP_EDITOR_EARLY_CLOSED, &Params);
  3954. switch (Answer)
  3955. {
  3956. case qaNeverAskAgain:
  3957. DisableDetect = true;
  3958. break;
  3959. case qaNo:
  3960. EnableMDI = true;
  3961. KeepOpen = true;
  3962. break;
  3963. }
  3964. }
  3965. if (AnyFound && (EnableMDI || DisableDetect))
  3966. {
  3967. bool Changed = false;
  3968. for (int i = 0; i < EditorList->Count; i++)
  3969. {
  3970. const TEditorPreferences * Editor = EditorList->Editors[i];
  3971. if ((Editor->Data->Editor == edExternal) &&
  3972. (Editor->Data->ExternalEditor == Data->Command) &&
  3973. ((EnableMDI && Editor->Data->SDIExternalEditor) ||
  3974. (DisableDetect && Editor->Data->DetectMDIExternalEditor)))
  3975. {
  3976. Changed = true;
  3977. TEditorPreferences * UpdatedEditor = new TEditorPreferences(*Editor);
  3978. if (EnableMDI)
  3979. {
  3980. UpdatedEditor->GetData()->SDIExternalEditor = false;
  3981. }
  3982. if (DisableDetect)
  3983. {
  3984. UpdatedEditor->GetData()->DetectMDIExternalEditor = false;
  3985. }
  3986. EditorList->Change(i, UpdatedEditor);
  3987. }
  3988. }
  3989. if (Changed)
  3990. {
  3991. WinConfiguration->EditorList = EditorList;
  3992. }
  3993. }
  3994. }
  3995. __finally
  3996. {
  3997. delete EditorList;
  3998. }
  3999. }
  4000. else
  4001. {
  4002. // "open" case
  4003. MessageDialog(FMTLOAD(APP_EARLY_CLOSED, (Data->OriginalFileName)), qtWarning,
  4004. qaOK, HELP_APP_EARLY_CLOSED);
  4005. }
  4006. }
  4007. //---------------------------------------------------------------------------
  4008. void __fastcall TCustomScpExplorerForm::ExecutedFileUploadComplete(TObject * Sender)
  4009. {
  4010. EditorFormFileUploadComplete(DebugNotNull(dynamic_cast<TForm *>(Sender)));
  4011. }
  4012. //---------------------------------------------------------------------------
  4013. void __fastcall TCustomScpExplorerForm::RemoteDirViewEnter(TObject * /*Sender*/)
  4014. {
  4015. SideEnter(osRemote);
  4016. }
  4017. //---------------------------------------------------------------------------
  4018. void __fastcall TCustomScpExplorerForm::RemoteDriveViewEnter(TObject * /*Sender*/)
  4019. {
  4020. MakeNextInTabOrder(RemoteDirPanel, RemoteDrivePanel);
  4021. SideEnter(osRemote);
  4022. }
  4023. //---------------------------------------------------------------------------
  4024. void __fastcall TCustomScpExplorerForm::SideEnter(TOperationSide Side)
  4025. {
  4026. FCurrentSide = Side;
  4027. if (Visible)
  4028. {
  4029. UpdateControls();
  4030. }
  4031. }
  4032. //---------------------------------------------------------------------------
  4033. void __fastcall TCustomScpExplorerForm::FileDeleted(TOperationSide Side, const UnicodeString & FileName, bool Success)
  4034. {
  4035. VisualiseOperationFinished(Side, FileName, Success);
  4036. if (DebugAlwaysTrue(FDeletedFiles != NULL) && Success)
  4037. {
  4038. FDeletedFiles->Add(FileName);
  4039. }
  4040. }
  4041. //---------------------------------------------------------------------------
  4042. void __fastcall TCustomScpExplorerForm::DeleteFiles(TOperationSide Side,
  4043. TStrings * FileList, bool Alternative)
  4044. {
  4045. TCustomDirView * DView = DirView(Side);
  4046. DView->SaveSelection();
  4047. DView->SaveSelectedNames();
  4048. DebugAssert(!FAlternativeDelete);
  4049. FAlternativeDelete = Alternative;
  4050. try
  4051. {
  4052. if (!IsSideLocalBrowser(Side))
  4053. {
  4054. DebugAssert(Terminal != NULL);
  4055. // Clone the file list as it may refer to current directory files, which get destroyed, when the directory is reloaded after the operation
  4056. std::unique_ptr<TStrings> PermanentFileList(TRemoteFileList::CloneStrings(FileList));
  4057. std::unique_ptr<TStringList> DeletedFiles(new TStringList());
  4058. DeletedFiles->CaseSensitive = DebugAlwaysTrue(!IsSideLocalBrowser(Side));
  4059. int Params = FLAGMASK(Alternative, dfAlternative);
  4060. try
  4061. {
  4062. DebugAssert(FDeletedFiles == NULL);
  4063. TValueRestorer<TStrings *> DeletedFilesRestorer(FDeletedFiles, DeletedFiles.get());
  4064. TValueRestorer<TFileOperationFinishedEvent> OnFileOperationFinishedRestorer(FOnFileOperationFinished, FileDeleted);
  4065. FMoveToQueue = false;
  4066. Terminal->DeleteFiles(FileList, Params);
  4067. // Probably not needed for deleting, just for consistency with transfer code
  4068. if (FMoveToQueue)
  4069. {
  4070. Abort();
  4071. }
  4072. }
  4073. catch (EAbort &)
  4074. {
  4075. if (FMoveToQueue)
  4076. {
  4077. DeletedFiles->Sorted = true;
  4078. for (int Index = 0; Index < PermanentFileList->Count; Index++)
  4079. {
  4080. if (DeletedFiles->IndexOf(PermanentFileList->Strings[Index]) >= 0)
  4081. {
  4082. // We should always be deleting the first item => what can be used to optimize this code, if needed
  4083. DebugAssert(Index == 0);
  4084. PermanentFileList->Delete(Index);
  4085. Index--;
  4086. }
  4087. }
  4088. Configuration->Usage->Inc("MovesToBackgroundDelete");
  4089. TQueueItem * QueueItem = new TDeleteQueueItem(Terminal, PermanentFileList.get(), Params);
  4090. AddQueueItem(Queue, QueueItem, Terminal);
  4091. ClearOperationSelection(osRemote);
  4092. }
  4093. }
  4094. }
  4095. else
  4096. {
  4097. if (IsLocalBrowserMode())
  4098. {
  4099. Configuration->Usage->Inc(L"LocalLocalDeletes");
  4100. }
  4101. TValueRestorer<TOperationSide> ProgressSideRestorer(FProgressSide, FCurrentSide);
  4102. try
  4103. {
  4104. TTerminalManager::Instance()->LocalTerminal->DeleteLocalFiles(FileList, FLAGMASK(Alternative, dfAlternative));
  4105. }
  4106. __finally
  4107. {
  4108. ReloadLocalDirectory();
  4109. }
  4110. }
  4111. FAlternativeDelete = false;
  4112. }
  4113. catch(...)
  4114. {
  4115. FAlternativeDelete = false;
  4116. DView->DiscardSavedSelection();
  4117. throw;
  4118. }
  4119. DView->RestoreSelection();
  4120. }
  4121. //---------------------------------------------------------------------------
  4122. void __fastcall TCustomScpExplorerForm::LockFiles(TStrings * FileList, bool Lock)
  4123. {
  4124. DebugAssert(Terminal);
  4125. DebugAssert(!IsLocalBrowserMode());
  4126. RemoteDirView->SaveSelection();
  4127. RemoteDirView->SaveSelectedNames();
  4128. try
  4129. {
  4130. if (Lock)
  4131. {
  4132. Terminal->LockFiles(FileList);
  4133. }
  4134. else
  4135. {
  4136. Terminal->UnlockFiles(FileList);
  4137. }
  4138. }
  4139. catch(...)
  4140. {
  4141. RemoteDirView->DiscardSavedSelection();
  4142. throw;
  4143. }
  4144. RemoteDirView->RestoreSelection();
  4145. }
  4146. //---------------------------------------------------------------------------
  4147. bool TCustomScpExplorerForm::DoDirectoryExists(void * Session, const UnicodeString & Directory)
  4148. {
  4149. bool Result = false;
  4150. TTerminal * ATerminal = (Session == NULL) ? Terminal : reinterpret_cast<TTerminal *>(Session);
  4151. if (IsActiveTerminal(ATerminal))
  4152. {
  4153. std::unique_ptr<TRemoteFile> File(ATerminal->TryReadFile(Directory));
  4154. Result = File && File->IsDirectory;
  4155. }
  4156. return Result;
  4157. }
  4158. //---------------------------------------------------------------------------
  4159. bool TCustomScpExplorerForm::NeedSecondarySessionForRemoteCopy(TStrings * FileList)
  4160. {
  4161. bool CopyDirsOnSecondarySession = Terminal->IsCapable[fcSecondaryShell];
  4162. return
  4163. !Terminal->IsCapable[fcRemoteCopy] ||
  4164. (CopyDirsOnSecondarySession && TRemoteFileList::AnyDirectory(FileList));
  4165. }
  4166. //---------------------------------------------------------------------------
  4167. bool __fastcall TCustomScpExplorerForm::RemoteTransferDialog(TManagedTerminal *& Session,
  4168. TStrings * FileList, UnicodeString & Target, UnicodeString & FileMask, bool & DirectCopy,
  4169. bool NoConfirmation, bool Move)
  4170. {
  4171. DebugAssert(Terminal != NULL);
  4172. DebugAssert(Terminal == TTerminalManager::Instance()->ActiveTerminal);
  4173. DebugAssert(!IsLocalBrowserMode());
  4174. if (Session == NULL)
  4175. {
  4176. Session = Terminal;
  4177. }
  4178. bool TargetConfirmed = false;
  4179. if (Session == Terminal)
  4180. {
  4181. if (RemoteDriveView->DropTarget != NULL)
  4182. {
  4183. Target = RemoteDriveView->NodePathName(RemoteDriveView->DropTarget);
  4184. TargetConfirmed = true;
  4185. }
  4186. else if (RemoteDirView->DropTarget != NULL)
  4187. {
  4188. DebugAssert(RemoteDirView->ItemIsDirectory(RemoteDirView->DropTarget));
  4189. Target = RemoteDirView->ItemFullFileName(RemoteDirView->DropTarget);
  4190. TargetConfirmed = true;
  4191. }
  4192. else
  4193. {
  4194. Target = RemoteDirView->Path;
  4195. }
  4196. }
  4197. else
  4198. {
  4199. Target = GetSessionPath(Session, osRemote);
  4200. }
  4201. Target = UnixIncludeTrailingBackslash(Target);
  4202. if (FileList->Count == 1)
  4203. {
  4204. FileMask = DelimitFileNameMask(UnixExtractFileName(FileList->Strings[0]));
  4205. }
  4206. else
  4207. {
  4208. FileMask = AnyMask;
  4209. }
  4210. DirectCopy = Terminal->IsCapable[fcRemoteCopy] || Terminal->IsCapable[fcSecondaryShell];
  4211. bool Result = true;
  4212. if (!NoConfirmation)
  4213. {
  4214. bool Multi = (FileList->Count > 1);
  4215. if (Move)
  4216. {
  4217. Result = DoRemoteMoveDialog(Multi, Target, FileMask, DoDirectoryExists);
  4218. }
  4219. else
  4220. {
  4221. std::unique_ptr<TStrings> Sessions(new TStringList);
  4222. std::unique_ptr<TStrings> Directories(new TStringList);
  4223. TStrings * SessionList = TTerminalManager::Instance()->SessionList;
  4224. for (int Index = 0; Index < SessionList->Count; Index++)
  4225. {
  4226. TManagedTerminal * ASession = DebugNotNull(dynamic_cast<TManagedTerminal *>(SessionList->Objects[Index]));
  4227. if (IsActiveTerminal(ASession) && DebugAlwaysTrue(!ASession->LocalBrowser))
  4228. {
  4229. Sessions->AddObject(SessionList->Strings[Index], ASession);
  4230. Directories->Add(GetSessionPath(ASession, osRemote));
  4231. }
  4232. }
  4233. bool AnyDirectory = false;
  4234. bool CopyDirsOnSecondarySession = Terminal->IsCapable[fcSecondaryShell];
  4235. if (CopyDirsOnSecondarySession && !Terminal->CommandSessionOpened) // optimization
  4236. {
  4237. for (int Index = 0; !AnyDirectory && (Index < FileList->Count); Index++)
  4238. {
  4239. AnyDirectory = DebugNotNull(dynamic_cast<TRemoteFile *>(FileList->Objects[Index]))->IsDirectory;
  4240. }
  4241. }
  4242. TDirectRemoteCopy AllowDirectCopy;
  4243. if (Terminal->CommandSessionOpened || !NeedSecondarySessionForRemoteCopy(FileList))
  4244. {
  4245. DebugAssert(DirectCopy);
  4246. AllowDirectCopy = drcAllow;
  4247. }
  4248. else if (Terminal->IsCapable[fcSecondaryShell])
  4249. {
  4250. DebugAssert(DirectCopy);
  4251. AllowDirectCopy = Terminal->IsCapable[fcRemoteCopy] ? drcConfirmCommandSessionDirs : drcConfirmCommandSession;
  4252. }
  4253. else
  4254. {
  4255. DebugAssert(!DirectCopy);
  4256. AllowDirectCopy = drcDisallow;
  4257. }
  4258. void * ASession = Session;
  4259. Result = DoRemoteCopyDialog(
  4260. Sessions.get(), Directories.get(), AllowDirectCopy, Multi, ASession, Target, FileMask, DirectCopy, Terminal,
  4261. DoDirectoryExists, TargetConfirmed);
  4262. Session = static_cast<TManagedTerminal *>(ASession);
  4263. }
  4264. }
  4265. return Result;
  4266. }
  4267. //---------------------------------------------------------------------------
  4268. bool __fastcall TCustomScpExplorerForm::RemoteTransferFiles(
  4269. TStrings * FileList, bool NoConfirmation, bool Move, TManagedTerminal * Session)
  4270. {
  4271. DebugAssert(!IsLocalBrowserMode());
  4272. bool DirectCopy;
  4273. UnicodeString Target, FileMask;
  4274. bool Result = RemoteTransferDialog(Session, FileList, Target, FileMask, DirectCopy, NoConfirmation, Move);
  4275. if (Result)
  4276. {
  4277. if (!Move && !DirectCopy)
  4278. {
  4279. Configuration->Usage->Inc("RemoteCopyTemp");
  4280. UnicodeString RootTempDir;
  4281. UnicodeString TempDir;
  4282. TemporarilyDownloadFiles(FileList, false, RootTempDir, TempDir, false, false, true);
  4283. TStrings * TemporaryFilesList = new TStringList();
  4284. try
  4285. {
  4286. TMakeLocalFileListParams MakeFileListParam;
  4287. MakeFileListParam.FileList = TemporaryFilesList;
  4288. MakeFileListParam.FileTimes = NULL;
  4289. MakeFileListParam.IncludeDirs = true;
  4290. MakeFileListParam.Recursive = false;
  4291. ProcessLocalDirectory(TempDir, Terminal->MakeLocalFileList, &MakeFileListParam);
  4292. TTerminalManager::Instance()->ActiveSession = Session;
  4293. if (TemporaryFilesList->Count > 0)
  4294. {
  4295. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  4296. CopyParam.FileMask = FileMask;
  4297. DebugAssert(!FAutoOperation);
  4298. FAutoOperation = true;
  4299. Terminal->CopyToRemote(TemporaryFilesList, Target, &CopyParam, cpTemporary, NULL);
  4300. }
  4301. }
  4302. __finally
  4303. {
  4304. delete TemporaryFilesList;
  4305. FAutoOperation = false;
  4306. if (!RootTempDir.IsEmpty())
  4307. {
  4308. RecursiveDeleteFile(ExcludeTrailingBackslash(RootTempDir));
  4309. }
  4310. }
  4311. }
  4312. else
  4313. {
  4314. RemoteDirView->SaveSelection();
  4315. RemoteDirView->SaveSelectedNames();
  4316. try
  4317. {
  4318. if (Move)
  4319. {
  4320. Configuration->Usage->Inc("RemoteMove");
  4321. Terminal->MoveFiles(FileList, Target, FileMask, false);
  4322. }
  4323. else
  4324. {
  4325. Configuration->Usage->Inc("RemoteCopyDirect");
  4326. DebugAssert(DirectCopy);
  4327. DebugAssert(Session == Terminal);
  4328. if (!NeedSecondarySessionForRemoteCopy(FileList) ||
  4329. Terminal->CommandSessionOpened ||
  4330. CommandSessionFallback())
  4331. {
  4332. Terminal->CopyFiles(FileList, Target, FileMask, false);
  4333. }
  4334. }
  4335. }
  4336. catch(...)
  4337. {
  4338. RemoteDirView->DiscardSavedSelection();
  4339. throw;
  4340. }
  4341. RemoteDirView->RestoreSelection();
  4342. }
  4343. }
  4344. return Result;
  4345. }
  4346. //---------------------------------------------------------------------------
  4347. void __fastcall TCustomScpExplorerForm::CreateRemoteDirectory(
  4348. const UnicodeString & Path, TRemoteProperties & Properties)
  4349. {
  4350. DebugAssert(!IsLocalBrowserMode());
  4351. Properties.Valid = Properties.Valid << vpEncrypt;
  4352. Properties.Encrypt = GUIConfiguration->CurrentCopyParam.EncryptNewFiles;
  4353. RemoteDirView->CreateDirectoryEx(Path, &Properties);
  4354. }
  4355. //---------------------------------------------------------------------------
  4356. void __fastcall TCustomScpExplorerForm::CreateDirectory(TOperationSide Side)
  4357. {
  4358. Side = GetSide(Side);
  4359. TRemoteProperties Properties = GUIConfiguration->NewDirectoryProperties;
  4360. TRemoteProperties * AProperties = (!IsSideLocalBrowser(Side) ? &Properties : NULL);
  4361. UnicodeString Name = LoadStr(NEW_FOLDER);
  4362. int AllowedChanges =
  4363. FLAGMASK(!IsSideLocalBrowser(Side) && Terminal->IsCapable[fcModeChanging], cpMode);
  4364. bool SaveSettings = false;
  4365. if (DoCreateDirectoryDialog(Name, AProperties, AllowedChanges, SaveSettings))
  4366. {
  4367. TWindowLock Lock(this);
  4368. if (!IsSideLocalBrowser(Side))
  4369. {
  4370. if (SaveSettings)
  4371. {
  4372. GUIConfiguration->NewDirectoryProperties = Properties;
  4373. }
  4374. CreateRemoteDirectory(Name, Properties);
  4375. }
  4376. else
  4377. {
  4378. DirView(Side)->CreateDirectory(Name);
  4379. }
  4380. }
  4381. }
  4382. //---------------------------------------------------------------------------
  4383. void __fastcall TCustomScpExplorerForm::HomeDirectory(TOperationSide Side)
  4384. {
  4385. TWindowLock Lock(this);
  4386. DirView(Side)->ExecuteHomeDirectory();
  4387. }
  4388. //---------------------------------------------------------------------------
  4389. void __fastcall TCustomScpExplorerForm::OpenDirectory(TOperationSide Side)
  4390. {
  4391. DoOpenDirectoryDialog(odBrowse, Side);
  4392. }
  4393. //---------------------------------------------------------------------------
  4394. bool __fastcall TCustomScpExplorerForm::OpenBookmark(TOperationSide Side, TBookmark * Bookmark)
  4395. {
  4396. TOperationSide BookmarkSide = IsLocalBrowserMode() ? osLocal : Side;
  4397. UnicodeString Path = Bookmark->GetSideDirectory(BookmarkSide);
  4398. bool Result = !Path.IsEmpty();
  4399. if (Result)
  4400. {
  4401. TryOpenDirectory(Side, Path);
  4402. }
  4403. return Result;
  4404. }
  4405. //---------------------------------------------------------------------------
  4406. void __fastcall TCustomScpExplorerForm::CalculateSize(
  4407. TStrings * FileList, __int64 & Size, TCalculateSizeStats & Stats,
  4408. bool & Close)
  4409. {
  4410. // terminal can be already closed (e.g. dropped connection)
  4411. if (Terminal != NULL)
  4412. {
  4413. try
  4414. {
  4415. TCalculateSizeParams Params;
  4416. Params.Stats = &Stats;
  4417. Terminal->CalculateFilesSize(FileList, Size, Params);
  4418. }
  4419. catch(...)
  4420. {
  4421. if (!Terminal->Active)
  4422. {
  4423. Close = true;
  4424. }
  4425. throw;
  4426. }
  4427. }
  4428. }
  4429. //---------------------------------------------------------------------------
  4430. void __fastcall TCustomScpExplorerForm::CalculateChecksum(const UnicodeString & Alg,
  4431. TStrings * FileList, TCalculatedChecksumEvent OnCalculatedChecksum,
  4432. bool & Close)
  4433. {
  4434. if ((Terminal != NULL) && // terminal can be already closed (e.g. dropped connection)
  4435. EnsureCommandSessionFallback(fcCalculatingChecksum))
  4436. {
  4437. Configuration->Usage->Inc(L"ChecksumCalculated");
  4438. try
  4439. {
  4440. Terminal->CalculateFilesChecksum(Alg, FileList, OnCalculatedChecksum);
  4441. }
  4442. catch(...)
  4443. {
  4444. if (!Terminal->Active)
  4445. {
  4446. Close = true;
  4447. }
  4448. throw;
  4449. }
  4450. }
  4451. }
  4452. //---------------------------------------------------------------------------
  4453. void TCustomScpExplorerForm::LoadFilesProperties(TStrings * FileList)
  4454. {
  4455. if (Terminal->LoadFilesProperties(FileList))
  4456. {
  4457. RemoteDirView->Invalidate();
  4458. }
  4459. }
  4460. //---------------------------------------------------------------------------
  4461. bool TCustomScpExplorerForm::CanCalculateChecksum()
  4462. {
  4463. return
  4464. Terminal->IsCapable[fcCalculatingChecksum] ||
  4465. (Terminal->IsCapable[fcSecondaryShell] && !Terminal->IsEncryptingFiles());
  4466. }
  4467. //---------------------------------------------------------------------------
  4468. bool __fastcall TCustomScpExplorerForm::SetProperties(TOperationSide Side, TStrings * FileList)
  4469. {
  4470. bool Result;
  4471. if (!IsSideLocalBrowser(Side))
  4472. {
  4473. TRemoteTokenList * GroupList = NULL;
  4474. TRemoteTokenList * UserList = NULL;
  4475. try
  4476. {
  4477. TRemoteProperties CurrentProperties;
  4478. LoadFilesProperties(FileList);
  4479. bool CapableGroupChanging = Terminal->IsCapable[fcGroupChanging];
  4480. bool CapableOwnerChanging = Terminal->IsCapable[fcOwnerChanging];
  4481. if (CapableGroupChanging || CapableOwnerChanging)
  4482. {
  4483. if (CapableGroupChanging)
  4484. {
  4485. GroupList = Terminal->Groups->Duplicate();
  4486. }
  4487. if (CapableOwnerChanging)
  4488. {
  4489. UserList = Terminal->Users->Duplicate();
  4490. }
  4491. TRemoteDirectory * Files = Terminal->Files;
  4492. int Count = Files->Count;
  4493. if (Count > 100)
  4494. {
  4495. Count = 100;
  4496. }
  4497. for (int Index = 0; Index < Count; Index++)
  4498. {
  4499. TRemoteFile * File = Files->Files[Index];
  4500. if (CapableGroupChanging)
  4501. {
  4502. GroupList->AddUnique(File->Group);
  4503. }
  4504. if (CapableOwnerChanging)
  4505. {
  4506. UserList->AddUnique(File->Owner);
  4507. }
  4508. }
  4509. // if we haven't collected tokens for all files in current directory,
  4510. // make sure we collect them at least for all selected files.
  4511. // (note that so far the files in FileList has to be from current direcotry)
  4512. if (Count < Files->Count)
  4513. {
  4514. for (int Index = 0; Index < FileList->Count; Index++)
  4515. {
  4516. TRemoteFile * File = (TRemoteFile *)(FileList->Objects[Index]);
  4517. if (CapableGroupChanging)
  4518. {
  4519. GroupList->AddUnique(File->Group);
  4520. }
  4521. if (CapableOwnerChanging)
  4522. {
  4523. UserList->AddUnique(File->Owner);
  4524. }
  4525. }
  4526. }
  4527. }
  4528. CurrentProperties = TRemoteProperties::CommonProperties(FileList);
  4529. bool CapableAclChanging = Terminal->IsCapable[fcAclChangingFiles];
  4530. if (CapableAclChanging && TRemoteFileList::AnyDirectory(FileList))
  4531. {
  4532. CapableAclChanging = false;
  4533. CurrentProperties.Valid = CurrentProperties.Valid >> vpRights;
  4534. }
  4535. int Flags = 0;
  4536. if (Terminal->IsCapable[fcModeChanging]) Flags |= cpMode;
  4537. if (CapableAclChanging) Flags |= cpAcl;
  4538. if (CapableOwnerChanging) Flags |= cpOwner;
  4539. if (CapableGroupChanging) Flags |= cpGroup;
  4540. TCalculateChecksumEvent CalculateChecksumEvent = NULL;
  4541. if (CanCalculateChecksum())
  4542. {
  4543. CalculateChecksumEvent = CalculateChecksum;
  4544. }
  4545. std::unique_ptr<TStrings> ChecksumAlgs(new TStringList());
  4546. Terminal->GetSupportedChecksumAlgs(ChecksumAlgs.get());
  4547. TRemoteProperties NewProperties = CurrentProperties;
  4548. Result =
  4549. DoPropertiesDialog(FileList, RemoteDirView->PathName,
  4550. GroupList, UserList, ChecksumAlgs.get(), &NewProperties, Flags,
  4551. Terminal->IsCapable[fcGroupOwnerChangingByID],
  4552. CalculateSize, CalculateChecksumEvent);
  4553. if (Result)
  4554. {
  4555. NewProperties = TRemoteProperties::ChangedProperties(CurrentProperties, NewProperties);
  4556. Terminal->ChangeFilesProperties(FileList, &NewProperties);
  4557. }
  4558. }
  4559. __finally
  4560. {
  4561. delete GroupList;
  4562. delete UserList;
  4563. }
  4564. }
  4565. else
  4566. {
  4567. DirView(Side)->DisplayPropertiesMenu();
  4568. Result = true;
  4569. }
  4570. return Result;
  4571. }
  4572. //---------------------------------------------------------------------------
  4573. void __fastcall TCustomScpExplorerForm::KeyProcessed(Word & Key, TShiftState Shift)
  4574. {
  4575. if (Shift * AllKeyShiftStates() == (TShiftState() << ssAlt))
  4576. {
  4577. FIgnoreNextDialogChar = Key;
  4578. }
  4579. Key = 0;
  4580. }
  4581. //---------------------------------------------------------------------------
  4582. void __fastcall TCustomScpExplorerForm::CheckCustomCommandShortCut(
  4583. TCustomCommandList * List, Word & Key, Classes::TShiftState Shift, TShortCut KeyShortCut)
  4584. {
  4585. const TCustomCommandType * Command = List->Find(KeyShortCut);
  4586. if (Command != NULL)
  4587. {
  4588. KeyProcessed(Key, Shift);
  4589. if (CustomCommandState(*Command, false, ccltAll) > 0)
  4590. {
  4591. TAutoFlag DontCopyCommandToClipboardFlag(DontCopyCommandToClipboard);
  4592. ExecuteFileOperationCommand(foCustomCommand, osRemote,
  4593. false, false, const_cast<TCustomCommandType *>(Command));
  4594. }
  4595. }
  4596. }
  4597. //---------------------------------------------------------------------------
  4598. void __fastcall TCustomScpExplorerForm::KeyDown(Word & Key, Classes::TShiftState Shift)
  4599. {
  4600. if (!NonVisualDataModule->Busy)
  4601. {
  4602. if (QueueView3->Focused() && (QueueView3->OnKeyDown != NULL))
  4603. {
  4604. QueueView3->OnKeyDown(QueueView3, Key, Shift);
  4605. }
  4606. if ((Key != 0) && !DirView(osCurrent)->IsEditing())
  4607. {
  4608. TShortCut KeyShortCut = ShortCut(Key, Shift);
  4609. for (int Index = 0; Index < NonVisualDataModule->ExplorerActions->ActionCount; Index++)
  4610. {
  4611. // Note that handling shortcuts on panel (current control) popup menu has precendence over this.
  4612. // But so far it's not a problem, as it does what we want.
  4613. TAction * Action = (TAction *)NonVisualDataModule->ExplorerActions->Actions[Index];
  4614. if (((Action->ShortCut == KeyShortCut) ||
  4615. (Action->SecondaryShortCuts->IndexOfShortCut(KeyShortCut) >= 0)) &&
  4616. AllowedAction(Action, aaShortCut))
  4617. {
  4618. // Has to be called before the action as the dialog char is already in queue.
  4619. // So when the action consumes message queue, we already need to have the
  4620. // FIgnoreNextDialogChar set
  4621. KeyProcessed(Key, Shift);
  4622. // Reset reference to previous component (when menu/toolbar was clicked).
  4623. // Needed to detect that action was invoked by keyboard shortcut
  4624. // in TNonVisualDataModule::ExplorerActionsExecute
  4625. Action->ActionComponent = NULL;
  4626. Action->Execute();
  4627. return;
  4628. }
  4629. }
  4630. for (int i = 0; i < TTerminalManager::Instance()->Count; i++)
  4631. {
  4632. if (NonVisualDataModule->OpenSessionShortCut(i) == KeyShortCut)
  4633. {
  4634. KeyProcessed(Key, Shift);
  4635. TTerminalManager::Instance()->ActiveSessionIndex = i;
  4636. return;
  4637. }
  4638. }
  4639. if (Key == VK_TAB && Shift.Contains(ssCtrl))
  4640. {
  4641. KeyProcessed(Key, Shift);
  4642. TTerminalManager::Instance()->CycleTerminals(!Shift.Contains(ssShift));
  4643. }
  4644. TShortCut CustomShortCut = NormalizeCustomShortCut(KeyShortCut);
  4645. if (IsCustomShortCut(CustomShortCut))
  4646. {
  4647. CheckCustomCommandShortCut(WinConfiguration->CustomCommandList, Key, Shift, CustomShortCut);
  4648. CheckCustomCommandShortCut(WinConfiguration->ExtensionList, Key, Shift, CustomShortCut);
  4649. if (WinConfiguration->SharedBookmarks != NULL)
  4650. {
  4651. TBookmark * Bookmark = WinConfiguration->SharedBookmarks->FindByShortCut(CustomShortCut);
  4652. if ((Bookmark != NULL) &&
  4653. OpenBookmark(FCurrentSide, Bookmark))
  4654. {
  4655. KeyProcessed(Key, Shift);
  4656. }
  4657. }
  4658. }
  4659. }
  4660. }
  4661. TForm::KeyDown(Key, Shift);
  4662. }
  4663. //---------------------------------------------------------------------------
  4664. void __fastcall TCustomScpExplorerForm::InitStatusBar()
  4665. {
  4666. bool OpenedTerminal = false;
  4667. UnicodeString ProtocolName;
  4668. UnicodeString ProtocolNameHint;
  4669. UnicodeString SecurityProtocolName;
  4670. UnicodeString DurationHint;
  4671. // See the comment in TScpCommanderForm::UpdateControls()
  4672. TTerminal * ActiveTerminal = TTerminalManager::Instance()->ActiveTerminal;
  4673. if (ActiveTerminal != NULL)
  4674. {
  4675. // Active is not enough here
  4676. if (ActiveTerminal->Status == ssOpened)
  4677. {
  4678. const TSessionInfo & SessionInfo = ActiveTerminal->GetSessionInfo();
  4679. const TFileSystemInfo & FileSystemInfo = ActiveTerminal->GetFileSystemInfo();
  4680. if (FileSystemInfo.ProtocolName.IsEmpty())
  4681. {
  4682. ProtocolName = SessionInfo.ProtocolName;
  4683. }
  4684. else
  4685. {
  4686. ProtocolName = FileSystemInfo.ProtocolName;
  4687. }
  4688. ProtocolNameHint = LoadStr(STATUS_PROTOCOL_HINT);
  4689. SecurityProtocolName = SessionInfo.SecurityProtocolName;
  4690. DurationHint = LoadStr(STATUS_DURATION_HINT);
  4691. OpenedTerminal = true;
  4692. }
  4693. else if (ActiveTerminal->Status > ssClosed)
  4694. {
  4695. ProtocolName = LoadStr(STATUS_CONNECTING);
  4696. }
  4697. else
  4698. {
  4699. ProtocolName = LoadStr(STATUS_NOT_CONNECTED2);
  4700. }
  4701. }
  4702. TTBXStatusBar * SessionStatusBar = (TTBXStatusBar *)GetComponent(fcStatusBar);
  4703. int Offset = SessionStatusBar->Panels->Count - SessionPanelCount;
  4704. TTBXStatusPanel * ProtocolStatusPanel = SessionStatusBar->Panels->Items[Offset + 0];
  4705. ProtocolStatusPanel->ViewPriority = (ActiveTerminal != NULL) ? 100 : 0;
  4706. if (ProtocolStatusPanel->Caption != ProtocolName)
  4707. {
  4708. ProtocolStatusPanel->Caption = ProtocolName;
  4709. std::unique_ptr<TCanvas> Canvas(CreateControlCanvas(SessionStatusBar));
  4710. ProtocolStatusPanel->Size = Canvas->TextWidth(ProtocolName) + ScaleByTextHeight(SessionStatusBar, 32);
  4711. }
  4712. ProtocolStatusPanel->Hint = ProtocolNameHint;
  4713. TTBXStatusPanel * SecurityStatusPanel = SessionStatusBar->Panels->Items[Offset + 1];
  4714. SecurityStatusPanel->ViewPriority = OpenedTerminal ? 98 : 0;
  4715. bool SecurityEnabled = !SecurityProtocolName.IsEmpty();
  4716. SecurityStatusPanel->Enabled = SecurityEnabled;
  4717. UnicodeString SecurityProtocolHint;
  4718. if (SecurityEnabled)
  4719. {
  4720. SecurityProtocolHint = FMTLOAD(STATUS_SECURE, (SecurityProtocolName));
  4721. }
  4722. else
  4723. {
  4724. SecurityProtocolHint = LoadStr(STATUS_INSECURE);
  4725. }
  4726. SecurityStatusPanel->Hint = SecurityProtocolHint;
  4727. TTBXStatusPanel * DurationStatusPanel = SessionStatusBar->Panels->Items[Offset + 2];
  4728. DurationStatusPanel->ViewPriority = OpenedTerminal ? 99 : 0;
  4729. DurationStatusPanel->Hint = DurationHint;
  4730. UpdateStatusBar();
  4731. }
  4732. //---------------------------------------------------------------------------
  4733. void __fastcall TCustomScpExplorerForm::UpdateStatusBar()
  4734. {
  4735. TTBXStatusBar * SessionStatusBar = (TTBXStatusBar *)GetComponent(fcStatusBar);
  4736. DebugAssert(SessionStatusBar != NULL);
  4737. TTBXStatusPanel * NoteStatusPanel = SessionStatusBar->Panels->Items[0];
  4738. if (!FNote.IsEmpty())
  4739. {
  4740. NoteStatusPanel->Caption = FNote;
  4741. }
  4742. else
  4743. {
  4744. UpdateStatusPanelText(NoteStatusPanel);
  4745. }
  4746. NoteStatusPanel->Hint = FNoteHints;
  4747. UnicodeString LoginTime;
  4748. // See the comment in TScpCommanderForm::UpdateControls()
  4749. TTerminal * ActiveTerminal = TTerminalManager::Instance()->ActiveTerminal;
  4750. // Active is not enough here
  4751. if ((ActiveTerminal != NULL) && (ActiveTerminal->Status == ssOpened))
  4752. {
  4753. const TSessionInfo & SessionInfo = ActiveTerminal->GetSessionInfo();
  4754. LoginTime = FormatDateTimeSpan(Now() - SessionInfo.LoginTime);
  4755. }
  4756. TTBXStatusPanel * DurationStatusPanel = SessionStatusBar->Panels->Items[SessionStatusBar->Panels->Count - 1];
  4757. DurationStatusPanel->Caption = LoginTime;
  4758. }
  4759. //---------------------------------------------------------------------------
  4760. void __fastcall TCustomScpExplorerForm::UpdateStatusPanelText(TTBXStatusPanel * Panel)
  4761. {
  4762. Panel->Caption = L"";
  4763. }
  4764. //---------------------------------------------------------------------------
  4765. void __fastcall TCustomScpExplorerForm::Idle()
  4766. {
  4767. if (FShowing ||
  4768. // Particularly for
  4769. // - Standalone editing
  4770. // - Detecting closed connection and automatically reconnect it while waiting for changes
  4771. // while "keeping remote directory up to date".
  4772. StandaloneOperation)
  4773. {
  4774. FEditorManager->Check();
  4775. // make sure that Idle is called before update queue, as it may invoke QueueEvent
  4776. // that needs to know if queue view is visible (and it may be closed after queue update)
  4777. TTerminalManager::Instance()->Idle(FDoNotIdleCurrentTerminal > 0);
  4778. }
  4779. if (FShowing)
  4780. {
  4781. if (!NonVisualDataModule->Busy &&
  4782. // Menu is opened or drag&drop is going on
  4783. (Mouse->Capture == NULL))
  4784. {
  4785. if (FRefreshRemoteDirectory)
  4786. {
  4787. if ((Terminal != NULL) && (Terminal->Status == ssOpened))
  4788. {
  4789. Terminal->RefreshDirectory();
  4790. }
  4791. FRefreshRemoteDirectory = false;
  4792. }
  4793. if (FRefreshLocalDirectory)
  4794. {
  4795. ReloadLocalDirectory();
  4796. FRefreshLocalDirectory = false;
  4797. }
  4798. if (WinConfiguration->RefreshRemotePanel)
  4799. {
  4800. if ((Terminal != NULL) && (Terminal->Status == ssOpened) &&
  4801. (Now() - Terminal->DirectoryLoaded > WinConfiguration->RefreshRemotePanelInterval))
  4802. {
  4803. RemoteDirView->ReloadDirectory();
  4804. }
  4805. }
  4806. }
  4807. // TB2 shows accels on Alt key down, but hides it only after menu modal loop finishes.
  4808. // But if Alt is used to switch apps or for keyboard shortcut, the menu modal loop does not happen and accelerators stay displayed.
  4809. // It's difficult to fix this in TB2, as there's no "up" equivalent to CM_DIALOGKEY, which shows the accels.
  4810. // And the Alt key can be released even when other window or application is active, so handling the key up in explorer window won't do either.
  4811. TTBCustomToolbar * MenuToolbar = DebugNotNull(dynamic_cast<TTBCustomToolbar *>(GetComponent(fcMenu)));
  4812. MenuToolbar->Idle();
  4813. }
  4814. if (FShowing || StandaloneOperation)
  4815. {
  4816. if (FQueueStatusInvalidated)
  4817. {
  4818. UpdateQueueStatus(false);
  4819. }
  4820. RefreshQueueItems();
  4821. }
  4822. if (FShowing)
  4823. {
  4824. UpdateStatusBar();
  4825. }
  4826. if (FClipboardFakeMonitorsPendingReset && !FDownloadingFromClipboard)
  4827. {
  4828. FClipboardFakeMonitorsPendingReset = false;
  4829. FClipboardFakeMonitors.reset(NULL);
  4830. }
  4831. FIgnoreNextDialogChar = 0;
  4832. }
  4833. //---------------------------------------------------------------------------
  4834. void __fastcall TCustomScpExplorerForm::UserActionTimer(TObject * /*Sender*/)
  4835. {
  4836. try
  4837. {
  4838. FUserActionTimer->Enabled = false;
  4839. if (IsQueueAutoPopup() && (FPendingQueueActionItem != NULL))
  4840. {
  4841. if (TQueueItem::IsUserActionStatus(FPendingQueueActionItem->Status))
  4842. {
  4843. FPendingQueueActionItem->ProcessUserAction();
  4844. }
  4845. }
  4846. }
  4847. __finally
  4848. {
  4849. FPendingQueueActionItem = NULL;
  4850. }
  4851. }
  4852. //---------------------------------------------------------------------------
  4853. void __fastcall TCustomScpExplorerForm::ApplicationMinimize(TObject * /*Sender*/)
  4854. {
  4855. if (WinConfiguration->MinimizeToTray)
  4856. {
  4857. UpdateTrayIcon();
  4858. FTrayIcon->Visible = true;
  4859. if (Visible)
  4860. {
  4861. ShowWindow(Handle, SW_HIDE);
  4862. }
  4863. }
  4864. }
  4865. //---------------------------------------------------------------------------
  4866. void __fastcall TCustomScpExplorerForm::ApplicationRestore(TObject * /*Sender*/)
  4867. {
  4868. // WORKAROUND
  4869. // When restoring maximized window from minimization,
  4870. // rarely some controls do not align properly.
  4871. // Two instances seen (both for Commander):
  4872. // - When restoring, window is temporarily narrower (not maximized),
  4873. // causing toolbars on TopDock to wrap and dock to expand horizontally.
  4874. // Once maximized already, top dock shinks back, but the session PageControl,
  4875. // do not align up, leaving space between TopDock and PageControl.
  4876. // - Similar issue seen with LocalDirView not aligning down to status bar.
  4877. for (int Index = 0; Index < ControlCount; Index++)
  4878. {
  4879. RealignControl(Controls[Index]);
  4880. }
  4881. if (FTrayIcon->Visible)
  4882. {
  4883. FTrayIcon->Visible = false;
  4884. if (Visible)
  4885. {
  4886. ShowWindow(Handle, SW_SHOW);
  4887. }
  4888. }
  4889. if (FNeedSession && DebugAlwaysTrue(Terminal == NULL))
  4890. {
  4891. FNeedSession = false;
  4892. NonVisualDataModule->StartBusy();
  4893. try
  4894. {
  4895. NeedSession(true);
  4896. }
  4897. __finally
  4898. {
  4899. NonVisualDataModule->EndBusy();
  4900. }
  4901. }
  4902. }
  4903. //---------------------------------------------------------------------------
  4904. void __fastcall TCustomScpExplorerForm::UpdateTrayIcon()
  4905. {
  4906. FTrayIcon->Hint = GetMainForm()->Caption;
  4907. }
  4908. //---------------------------------------------------------------------------
  4909. void __fastcall TCustomScpExplorerForm::ApplicationTitleChanged()
  4910. {
  4911. UpdateTrayIcon();
  4912. }
  4913. //---------------------------------------------------------------------------
  4914. void __fastcall TCustomScpExplorerForm::RestoreApp()
  4915. {
  4916. // workaround
  4917. // TApplication.WndProc sets TApplication.FAppIconic to false,
  4918. // on WM_ACTIVATEAPP, what renders TApplication.Restore no-op function.
  4919. // But WM_ACTIVATEAPP message can be received even when
  4920. // the main window is minimized to the tray and internal editor window is focused
  4921. // (after another application was previously active)
  4922. if (::IsIconic(Handle))
  4923. {
  4924. if (!IsAppIconic())
  4925. {
  4926. SetAppIconic(true);
  4927. }
  4928. }
  4929. ::ApplicationRestore();
  4930. Application->BringToFront();
  4931. }
  4932. //---------------------------------------------------------------------------
  4933. void __fastcall TCustomScpExplorerForm::TrayIconClick(TObject * /*Sender*/)
  4934. {
  4935. RestoreApp();
  4936. }
  4937. //---------------------------------------------------------------------------
  4938. void __fastcall TCustomScpExplorerForm::NewSession(const UnicodeString & SessionUrl)
  4939. {
  4940. if (OpenInNewWindow())
  4941. {
  4942. ExecuteNewInstance(SessionUrl);
  4943. }
  4944. else
  4945. {
  4946. TTerminalManager::Instance()->NewSession(SessionUrl);
  4947. }
  4948. }
  4949. //---------------------------------------------------------------------------
  4950. void TCustomScpExplorerForm::NewTab(TOperationSide, bool)
  4951. {
  4952. NewSession();
  4953. }
  4954. //---------------------------------------------------------------------------
  4955. UnicodeString __fastcall TCustomScpExplorerForm::SaveHiddenDuplicateSession(TSessionData * SessionData)
  4956. {
  4957. UnicodeString SessionName = StoredSessions->HiddenPrefix + ManagedSession->SessionData->SessionName;
  4958. StoredSessions->NewSession(SessionName, SessionData);
  4959. // modified only, explicit
  4960. StoredSessions->Save(false, true);
  4961. // encode session name because of slashes in hierarchical sessions
  4962. return EncodeUrlString(SessionName);
  4963. }
  4964. //---------------------------------------------------------------------------
  4965. UnicodeString __fastcall TCustomScpExplorerForm::CreateHiddenDuplicateSession()
  4966. {
  4967. // current working directories become defaults here, what is not right
  4968. std::unique_ptr<TSessionData> SessionData(CloneCurrentSessionData());
  4969. return SaveHiddenDuplicateSession(SessionData.get());
  4970. }
  4971. //---------------------------------------------------------------------------
  4972. void __fastcall TCustomScpExplorerForm::DuplicateTab()
  4973. {
  4974. if (OpenInNewWindow())
  4975. {
  4976. ExecuteNewInstance(CreateHiddenDuplicateSession());
  4977. }
  4978. else
  4979. {
  4980. // current working directories become defaults here, what is not right
  4981. std::unique_ptr<TSessionData> SessionData(CloneCurrentSessionData());
  4982. TTerminalManager * Manager = TTerminalManager::Instance();
  4983. TManagedTerminal * ASession = Manager->NewManagedTerminal(SessionData.get());
  4984. DebugAssert(ASession->LocalBrowser == ManagedSession->LocalBrowser);
  4985. // We definitely want these
  4986. ASession->Disconnected = ManagedSession->Disconnected;
  4987. // Not sure about these two
  4988. ASession->Permanent = ManagedSession->Permanent;
  4989. ASession->DisconnectedTemporarily = ManagedSession->DisconnectedTemporarily;
  4990. Manager->ActiveSession = ASession;
  4991. }
  4992. }
  4993. //---------------------------------------------------------------------------
  4994. void __fastcall TCustomScpExplorerForm::RenameTab()
  4995. {
  4996. UnicodeString Name = ManagedSession->SessionData->SessionName;
  4997. if (InputDialog(LoadStr(RENAME_TAB_TITLE), LoadStr(RENAME_TAB_PROMPT), Name, HELP_TAB_RENAME) &&
  4998. // When submitting the default name, do not fix it
  4999. (Name != ManagedSession->SessionData->SessionName))
  5000. {
  5001. // Checks for a slash only, so it's not that big deal that we do the check after submitting the dialog only.
  5002. TSessionData::ValidateName(Name);
  5003. // This is inconsistent with how color (for example) is handled.
  5004. ManagedSession->SessionData->Name = Name;
  5005. UpdateControls();
  5006. // Add/Remove distinguishing paths from sessions of the same name.
  5007. SessionListChanged();
  5008. }
  5009. }
  5010. //---------------------------------------------------------------------------
  5011. bool __fastcall TCustomScpExplorerForm::CanCloseQueue(TTerminalQueue * Queue)
  5012. {
  5013. DebugAssert(Queue != NULL);
  5014. bool Result = Queue->IsEmpty;
  5015. if (!Result)
  5016. {
  5017. SetFocus();
  5018. Result = (MessageDialog(LoadStr(PENDING_QUEUE_ITEMS2), qtWarning, qaOK | qaCancel, HELP_NONE) == qaOK);
  5019. }
  5020. return Result;
  5021. }
  5022. //---------------------------------------------------------------------------
  5023. bool __fastcall TCustomScpExplorerForm::CanCloseQueue()
  5024. {
  5025. return CanCloseQueue(FQueue);
  5026. }
  5027. //---------------------------------------------------------------------------
  5028. void __fastcall TCustomScpExplorerForm::CloseTab()
  5029. {
  5030. if (CanCloseQueue())
  5031. {
  5032. TTerminalManager::Instance()->FreeActiveTerminal();
  5033. }
  5034. }
  5035. //---------------------------------------------------------------------------
  5036. void __fastcall TCustomScpExplorerForm::DisconnectSession()
  5037. {
  5038. if (CanCloseQueue())
  5039. {
  5040. TTerminalManager::Instance()->DisconnectActiveTerminal();
  5041. }
  5042. }
  5043. //---------------------------------------------------------------------------
  5044. void __fastcall TCustomScpExplorerForm::TerminalDisconnected()
  5045. {
  5046. DetachTerminal(Terminal);
  5047. RemoteDirView->Terminal = Terminal;
  5048. UpdateRemotePathComboBox(false);
  5049. InitStatusBar();
  5050. UpdateControls();
  5051. }
  5052. //---------------------------------------------------------------------------
  5053. void __fastcall TCustomScpExplorerForm::TerminalConnecting()
  5054. {
  5055. InitStatusBar();
  5056. }
  5057. //---------------------------------------------------------------------------
  5058. void __fastcall TCustomScpExplorerForm::ReconnectSession()
  5059. {
  5060. if (DebugAlwaysTrue(!Terminal->Active))
  5061. {
  5062. TTerminalManager::Instance()->ReconnectActiveTerminal();
  5063. }
  5064. }
  5065. //---------------------------------------------------------------------------
  5066. void __fastcall TCustomScpExplorerForm::OpenStoredSession(TSessionData * Data)
  5067. {
  5068. if (OpenInNewWindow())
  5069. {
  5070. // encode session name because of slashes in hierarchical sessions
  5071. ExecuteNewInstance(EncodeUrlString(Data->Name));
  5072. }
  5073. else
  5074. {
  5075. TTerminalManager * Manager = TTerminalManager::Instance();
  5076. TManagedTerminal * Terminal = Manager->NewManagedTerminal(Data);
  5077. Manager->ActiveSession = Terminal;
  5078. }
  5079. }
  5080. //---------------------------------------------------------------------------
  5081. void TCustomScpExplorerForm::DoOpenFolderOrWorkspace(
  5082. const UnicodeString & Name, bool ConnectFirstTerminal, bool CheckMaxSessions)
  5083. {
  5084. TTerminalManager * Manager = TTerminalManager::Instance();
  5085. std::unique_ptr<TObjectList> DataList(new TObjectList());
  5086. // This asks for a master password. Maybe we should postpone asking until a connection is actually opened.
  5087. StoredSessions->GetFolderOrWorkspace(Name, DataList.get());
  5088. if (!CheckMaxSessions || (DataList->Count <= WinConfiguration->MaxSessions))
  5089. {
  5090. TManagedTerminal * FirstSession = Manager->NewSessions(DataList.get());
  5091. // FirstSession can be null, if none of the workspace sites exist anymore
  5092. // or if all workspace sessions are not suported by the interface
  5093. // (when trying to open all local-local workspace in "explorer" interface)
  5094. if (FirstSession != NULL)
  5095. {
  5096. if (!ConnectFirstTerminal)
  5097. {
  5098. FirstSession->Disconnected = true;
  5099. FirstSession->DisconnectedTemporarily = true;
  5100. }
  5101. Manager->ActiveSession = FirstSession;
  5102. }
  5103. }
  5104. }
  5105. //---------------------------------------------------------------------------
  5106. void __fastcall TCustomScpExplorerForm::OpenFolderOrWorkspace(const UnicodeString & Name)
  5107. {
  5108. if (OpenInNewWindow())
  5109. {
  5110. ExecuteNewInstance(Name);
  5111. }
  5112. else
  5113. {
  5114. DoOpenFolderOrWorkspace(Name, true, false);
  5115. }
  5116. }
  5117. //---------------------------------------------------------------------------
  5118. void __fastcall TCustomScpExplorerForm::FormCloseQuery(TObject * /*Sender*/,
  5119. bool &CanClose)
  5120. {
  5121. AppLog(L"Windows close query");
  5122. // See the comment in CloseApp()
  5123. if (NonVisualDataModule->Busy)
  5124. {
  5125. CanClose = false;
  5126. }
  5127. else
  5128. {
  5129. TTerminalManager * Manager = TTerminalManager::Instance();
  5130. int ActiveSessions = 0;
  5131. for (int Index = 0; Index < Manager->Count; Index++)
  5132. {
  5133. if (Manager->Sessions[Index]->Active)
  5134. {
  5135. ActiveSessions++;
  5136. }
  5137. }
  5138. // Confirm, when there's at least one active remote session or
  5139. // when there are multiple (inactive or local) sessions and auto workspace saving is not enabled.
  5140. if (((ActiveSessions > 0) || ((Manager->Count > 1) && !WinConfiguration->AutoSaveWorkspace)) &&
  5141. WinConfiguration->ConfirmClosingSession)
  5142. {
  5143. unsigned int Result;
  5144. TMessageParams Params(mpNeverAskAgainCheck);
  5145. UnicodeString Message;
  5146. int Answers = qaOK | qaCancel;
  5147. // The current session is the only active session
  5148. if ((ActiveSessions == 1) && (Terminal != NULL) && Terminal->Active)
  5149. {
  5150. if (!WinConfiguration->AutoSaveWorkspace)
  5151. {
  5152. Message = LoadStr(CLOSE_SESSION_WORKSPACE);
  5153. }
  5154. else
  5155. {
  5156. Message = LoadStr(CLOSE_SESSION);
  5157. }
  5158. Message = FORMAT(Message, (Terminal->SessionData->SessionName));
  5159. }
  5160. // Multiple active sessions or one active session, but it's not the current one
  5161. else if (ActiveSessions > 0)
  5162. {
  5163. if (!WinConfiguration->AutoSaveWorkspace)
  5164. {
  5165. Message = LoadStr(CLOSE_SESSIONS_WORKSPACE3);
  5166. }
  5167. else
  5168. {
  5169. Message = LoadStr(CLOSE_SESSIONS);
  5170. }
  5171. }
  5172. // Multiple tabs, but all are inactive or local, and saving of worspace is not enabled
  5173. else
  5174. {
  5175. DebugAlwaysTrue(Manager->Count > 1);
  5176. DebugAlwaysTrue(!WinConfiguration->AutoSaveWorkspace);
  5177. Message = LoadStr(CLOSE_WORKSPACE);
  5178. }
  5179. UnicodeString Note;
  5180. if (WinConfiguration->AutoSaveWorkspace)
  5181. {
  5182. Note = FMTLOAD(AUTO_WORKSPACE, (WorkspaceName()));
  5183. }
  5184. else
  5185. {
  5186. Note = LoadStr(AUTO_WORKSPACE_ENABLE);
  5187. Answers = qaYes | qaNo | qaCancel;
  5188. }
  5189. Message = FORMAT("%s\n\n%s", (MainInstructions(Message), Note));
  5190. SetFocus();
  5191. Result = MessageDialog(Message, qtConfirmation,
  5192. Answers, HELP_CLOSE_SESSION_WORKSPACE, &Params);
  5193. if (Result == qaNeverAskAgain)
  5194. {
  5195. WinConfiguration->ConfirmClosingSession = false;
  5196. }
  5197. if (Result == qaNo)
  5198. {
  5199. CanClose = SaveWorkspace(true);
  5200. // note that the workspace will be saved redundatly again from FormClose
  5201. }
  5202. else
  5203. {
  5204. CanClose =
  5205. (Result == qaOK) ||
  5206. (Result == qaYes) || // CLOSE_SESSIONS_WORKSPACE variant
  5207. (Result == qaNeverAskAgain);
  5208. }
  5209. }
  5210. if (CanClose && HasActiveTerminal())
  5211. {
  5212. CanClose = CanCloseQueue();
  5213. }
  5214. }
  5215. if (CanClose)
  5216. {
  5217. CanClose =
  5218. FEditorManager->CloseInternalEditors(CloseInternalEditor) &&
  5219. FEditorManager->CloseExternalFilesWithoutProcess();
  5220. if (CanClose)
  5221. {
  5222. while (CanClose && (FLocalEditors->Count > 0))
  5223. {
  5224. int PrevCount = FLocalEditors->Count;
  5225. static_cast<TForm *>(FLocalEditors->Items[0])->Close();
  5226. CanClose = (FLocalEditors->Count < PrevCount);
  5227. }
  5228. if (CanClose)
  5229. {
  5230. CanClose = FEditorManager->Empty(true);
  5231. if (!CanClose)
  5232. {
  5233. SetFocus();
  5234. CanClose =
  5235. (MessageDialog(
  5236. LoadStr(PENDING_EDITORS), qtWarning, qaIgnore | qaCancel, HELP_NONE) == qaIgnore);
  5237. }
  5238. }
  5239. }
  5240. }
  5241. }
  5242. //---------------------------------------------------------------------------
  5243. void __fastcall TCustomScpExplorerForm::CloseInternalEditor(TObject * Sender)
  5244. {
  5245. TForm * Form = dynamic_cast<TForm *>(Sender);
  5246. DebugAssert(Form != NULL);
  5247. Form->Close();
  5248. }
  5249. //---------------------------------------------------------------------------
  5250. void __fastcall TCustomScpExplorerForm::ForceCloseInternalEditor(TObject * Sender)
  5251. {
  5252. TForm * Form = dynamic_cast<TForm *>(Sender);
  5253. delete Form;
  5254. }
  5255. //---------------------------------------------------------------------------
  5256. void __fastcall TCustomScpExplorerForm::ForceCloseLocalEditors()
  5257. {
  5258. while (FLocalEditors->Count > 0)
  5259. {
  5260. delete static_cast<TForm *>(FLocalEditors->Items[0]);
  5261. }
  5262. }
  5263. //---------------------------------------------------------------------------
  5264. void __fastcall TCustomScpExplorerForm::RemoteDirViewDisplayProperties(
  5265. TObject *Sender)
  5266. {
  5267. TStrings *FileList = ((TUnixDirView*)Sender)->CreateFileList(True, False, NULL);
  5268. try
  5269. {
  5270. SetProperties(osRemote, FileList);
  5271. }
  5272. __finally
  5273. {
  5274. delete FileList;
  5275. }
  5276. }
  5277. //---------------------------------------------------------------------------
  5278. void __fastcall TCustomScpExplorerForm::ComponentShowing(Byte Component, bool value)
  5279. {
  5280. if (value)
  5281. {
  5282. if (Component == fcCustomCommandsBand)
  5283. {
  5284. UpdateCustomCommandsToolbar();
  5285. }
  5286. if (Component == fcQueueFileList)
  5287. {
  5288. DebugAssert(!QueueFileListSplitter->Visible);
  5289. AdjustQueueLayout();
  5290. UpdateQueueFileList();
  5291. }
  5292. }
  5293. }
  5294. //---------------------------------------------------------------------------
  5295. void __fastcall TCustomScpExplorerForm::AdjustQueueLayout()
  5296. {
  5297. int MinHeightLimit =
  5298. GetStaticQueuePanelComponentsHeight() +
  5299. QueueFileListSplitter->Height +
  5300. QueueFileList->Height +
  5301. GetMinQueueViewHeight();
  5302. if (QueuePanel->ClientHeight < MinHeightLimit)
  5303. {
  5304. QueuePanel->ClientHeight = MinHeightLimit;
  5305. }
  5306. }
  5307. //---------------------------------------------------------------------------
  5308. void __fastcall TCustomScpExplorerForm::SetComponentVisible(Byte Component, Boolean value)
  5309. {
  5310. TControl * Control = GetComponent(Component);
  5311. DebugAssert(Control);
  5312. bool Changed = (Control->Visible != value);
  5313. if (Changed)
  5314. {
  5315. ComponentShowing(Component, value);
  5316. TWinControl * WinControl = dynamic_cast<TWinControl*>(Control);
  5317. bool WasFocused = (WinControl != NULL) && (ActiveControl != NULL) &&
  5318. ((ActiveControl == WinControl) || (ActiveControl->Parent == WinControl));
  5319. if (value)
  5320. {
  5321. int RemainingHeight = Control->Parent->ClientHeight;
  5322. int RemainingWidth = Control->Parent->ClientWidth;
  5323. for (int i = 0; i < Control->Parent->ControlCount; i++)
  5324. {
  5325. TControl * ChildControl = Control->Parent->Controls[i];
  5326. if (ChildControl->Visible)
  5327. {
  5328. switch (ChildControl->Align)
  5329. {
  5330. case alTop:
  5331. case alBottom:
  5332. RemainingHeight -= ChildControl->Height;
  5333. break;
  5334. case alLeft:
  5335. case alRight:
  5336. RemainingWidth -= ChildControl->Width;
  5337. break;
  5338. }
  5339. }
  5340. }
  5341. int Reserve = ScaleByTextHeight(this, 32);
  5342. // queue in explorer, trees in commander
  5343. if (Control->Height > RemainingHeight - Reserve)
  5344. {
  5345. Control->Height = RemainingHeight / 2;
  5346. }
  5347. if (Control->Width > RemainingWidth - Reserve)
  5348. {
  5349. Control->Width = RemainingWidth / 2;
  5350. }
  5351. }
  5352. Control->Visible = value;
  5353. if (WasFocused && Visible)
  5354. {
  5355. DirView(osCurrent)->SetFocus();
  5356. }
  5357. FixControlsPlacement();
  5358. }
  5359. }
  5360. //---------------------------------------------------------------------------
  5361. bool __fastcall TCustomScpExplorerForm::GetComponentVisible(Byte Component)
  5362. {
  5363. TControl * Control = GetComponent(Component);
  5364. if (Control == NULL)
  5365. {
  5366. return false;
  5367. }
  5368. else
  5369. {
  5370. return Control->Visible;
  5371. }
  5372. }
  5373. //---------------------------------------------------------------------------
  5374. bool __fastcall TCustomScpExplorerForm::IsComponentPossible(Byte Component)
  5375. {
  5376. bool Result;
  5377. if ((Component == fcExplorerUpdatesBand) || (Component == fcCommanderUpdatesBand))
  5378. {
  5379. Result = !IsUWP();
  5380. }
  5381. else
  5382. {
  5383. Result = true;
  5384. }
  5385. return Result;
  5386. }
  5387. //---------------------------------------------------------------------------
  5388. void __fastcall TCustomScpExplorerForm::MakeFocusedItemVisible(TCustomDirView * DirView)
  5389. {
  5390. if (DirView->ItemFocused != NULL)
  5391. {
  5392. DirView->ItemFocused->MakeVisible(false);
  5393. }
  5394. }
  5395. //---------------------------------------------------------------------------
  5396. void __fastcall TCustomScpExplorerForm::FixControlsPlacement()
  5397. {
  5398. MakeFocusedItemVisible(RemoteDirView);
  5399. QueueSplitter->Visible = QueuePanel->Visible;
  5400. QueueFileListSplitter->Visible = QueueFileList->Visible;
  5401. RemotePanelSplitter->Visible = RemoteDrivePanel->Visible;
  5402. TControl * TopControlsOrder[] =
  5403. { TopDock, MessageDock, SessionsPageControl };
  5404. SetVerticalControlsOrder(TopControlsOrder, LENOF(TopControlsOrder));
  5405. TControl * QueueControlsOrder[] =
  5406. { QueueDock, QueueView3, QueueFileListSplitter, QueueFileList };
  5407. SetVerticalControlsOrder(QueueControlsOrder, LENOF(QueueControlsOrder));
  5408. }
  5409. //---------------------------------------------------------------------------
  5410. TControl * __fastcall TCustomScpExplorerForm::GetComponent(Byte Component)
  5411. {
  5412. switch (Component) {
  5413. case fcStatusBar: return RemoteStatusBar;
  5414. case fcRemotePopup: return reinterpret_cast<TControl *>(NonVisualDataModule->RemoteFilePopup);
  5415. case fcQueueView: return QueuePanel;
  5416. case fcQueueToolbar: return QueueDock;
  5417. case fcRemoteTree: return RemoteDrivePanel;
  5418. case fcSessionsTabs: return SessionsPageControl;
  5419. case fcQueueFileList: return QueueFileList;
  5420. default: return NULL;
  5421. }
  5422. }
  5423. //---------------------------------------------------------------------------
  5424. void __fastcall TCustomScpExplorerForm::DirViewColumnRightClick(
  5425. TObject *Sender, TListColumn *Column, TPoint &Point)
  5426. {
  5427. DebugAssert(NonVisualDataModule && Column && Sender);
  5428. NonVisualDataModule->ListColumn = Column;
  5429. TPopupMenu * DirViewColumnMenu;
  5430. if (Sender == DirView(osOther))
  5431. {
  5432. DirViewColumnMenu = NonVisualDataModule->RemoteDirViewColumnPopup;
  5433. NonVisualDataModule->RemoteSortByExtColumnPopupItem->Visible = (Column->Index == uvName);
  5434. bool SizeColumn = (Column->Index == uvSize);
  5435. NonVisualDataModule->RemoteFormatSizeBytesPopupItem->Visible = SizeColumn;
  5436. NonVisualDataModule->RemoteCalculateDirectorySizesPopupItem->Visible = SizeColumn;
  5437. }
  5438. else
  5439. {
  5440. DirViewColumnMenu = NonVisualDataModule->LocalDirViewColumnPopup;
  5441. NonVisualDataModule->LocalSortByExtColumnPopupItem->Visible = (Column->Index == dvName);
  5442. bool SizeColumn = (Column->Index == dvSize);
  5443. NonVisualDataModule->LocalFormatSizeBytesPopupItem->Visible = SizeColumn;
  5444. NonVisualDataModule->LocalCalculateDirectorySizesPopupItem->Visible = SizeColumn;
  5445. }
  5446. TCustomListView * ListView = DebugNotNull(dynamic_cast<TCustomListView *>(Sender));
  5447. TPoint ScreenPoint = ListView->ClientToScreen(Point);
  5448. ScreenPoint.x -= GetScrollPos(ListView->Handle, SB_HORZ);
  5449. DirViewColumnMenu->Popup(ScreenPoint.x, ScreenPoint.y);
  5450. }
  5451. //---------------------------------------------------------------------------
  5452. void __fastcall TCustomScpExplorerForm::DirViewExecFile(
  5453. TObject *Sender, TListItem *Item, bool &AllowExec)
  5454. {
  5455. DoDirViewExecFile(Sender, Item, AllowExec);
  5456. }
  5457. //---------------------------------------------------------------------------
  5458. void __fastcall TCustomScpExplorerForm::DoDirViewExecFile(TObject * Sender,
  5459. TListItem * Item, bool & AllowExec)
  5460. {
  5461. DebugAssert(Sender && Item && Configuration);
  5462. DebugAssert(AllowExec);
  5463. TCustomDirView * ADirView = (TCustomDirView *)Sender;
  5464. TOperationSide Side = ((ADirView == DirView(osRemote)) ? osRemote : osLocal);
  5465. bool Remote = !IsSideLocalBrowser(Side);
  5466. DebugAssert(Item == ADirView->ItemFocused);
  5467. bool IsDirectory = ADirView->ItemIsDirectory(Item);
  5468. TResolvedDoubleClickAction Action;
  5469. if (FForceExecution)
  5470. {
  5471. Action = IsDirectory ? rdcaChangeDir : rdcaOpen;
  5472. }
  5473. else
  5474. {
  5475. TTerminal * ATerminal = !IsSideLocalBrowser(Side) ? Terminal : NULL;
  5476. Action = WinConfiguration->ResolveDoubleClickAction(IsDirectory, ATerminal);
  5477. }
  5478. if (Action == rdcaCopy)
  5479. {
  5480. // To counter the lock in DirViewBusy (called from TCustomDirView.DoExecute)
  5481. UnlockWindow();
  5482. try
  5483. {
  5484. if (IsLocalBrowserMode())
  5485. {
  5486. LocalLocalCopy(foCopy, Side, true, !WinConfiguration->CopyOnDoubleClickConfirmation, false, 0);
  5487. }
  5488. else
  5489. {
  5490. if (HasActiveTerminal())
  5491. {
  5492. ExecuteFileOperation(foCopy, Side, true, !WinConfiguration->CopyOnDoubleClickConfirmation);
  5493. }
  5494. }
  5495. }
  5496. __finally
  5497. {
  5498. LockWindow();
  5499. }
  5500. AllowExec = false;
  5501. }
  5502. else if (Action == rdcaEdit)
  5503. {
  5504. if (!Remote || !WinConfiguration->DisableOpenEdit)
  5505. {
  5506. ExecuteFile(Side, efDefaultEditor);
  5507. AllowExec = false;
  5508. }
  5509. }
  5510. else if (Action == rdcaOpen)
  5511. {
  5512. if (DebugAlwaysTrue(!IsDirectory) && Remote && !WinConfiguration->DisableOpenEdit)
  5513. {
  5514. ExecuteFile(osRemote, efShell);
  5515. AllowExec = false;
  5516. }
  5517. }
  5518. }
  5519. //---------------------------------------------------------------------------
  5520. bool __fastcall TCustomScpExplorerForm::GetHasDirView(TOperationSide Side)
  5521. {
  5522. return ((Side == osRemote) || (Side == osCurrent));
  5523. }
  5524. //---------------------------------------------------------------------------
  5525. void __fastcall TCustomScpExplorerForm::CompareDirectories()
  5526. {
  5527. DebugFail();
  5528. }
  5529. //---------------------------------------------------------------------------
  5530. void __fastcall TCustomScpExplorerForm::SynchronizeDirectories()
  5531. {
  5532. DebugFail();
  5533. }
  5534. //---------------------------------------------------------------------------
  5535. bool __fastcall TCustomScpExplorerForm::DoSynchronizeDirectories(
  5536. UnicodeString & LocalDirectory, UnicodeString & RemoteDirectory, int UseDefaults)
  5537. {
  5538. TSynchronizeParamType Params;
  5539. Params.LocalDirectory = LocalDirectory;
  5540. Params.RemoteDirectory = RemoteDirectory;
  5541. int UnusedParams =
  5542. (GUIConfiguration->SynchronizeParams &
  5543. (TTerminal::spPreviewChanges | TTerminal::spTimestamp | TTerminal::spNotByTime | TTerminal::spBySize |
  5544. TTerminal::spCaseSensitive | TTerminal::spMirror | TTerminal::spByChecksum));
  5545. Params.Params = GUIConfiguration->SynchronizeParams & ~UnusedParams;
  5546. Params.Options = GUIConfiguration->SynchronizeOptions;
  5547. bool SaveSettings = false;
  5548. TSynchronizeController Controller(&DoSynchronize, &DoSynchronizeInvalid,
  5549. &DoSynchronizeTooManyDirectories);
  5550. DebugAssert(FSynchronizeController == NULL);
  5551. FSynchronizeController = &Controller;
  5552. bool Result;
  5553. try
  5554. {
  5555. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  5556. int CopyParamAttrs = Terminal->UsableCopyParamAttrs(0).Upload;
  5557. int Options =
  5558. FLAGMASK(SynchronizeAllowSelectedOnly(), soAllowSelectedOnly);
  5559. DebugAssert(FOnFeedSynchronizeError == NULL);
  5560. DebugAssert(FOnSynchronizeAbort == NULL);
  5561. DebugAssert(FSynchronizeTerminal == NULL);
  5562. FSynchronizeTerminal = Terminal;
  5563. Result = DoSynchronizeDialog(Params, &CopyParam, Controller.StartStop,
  5564. SaveSettings, Options, CopyParamAttrs, GetSynchronizeOptions, SynchronizeSessionLog,
  5565. FOnFeedSynchronizeError, FOnSynchronizeAbort, SynchronizeInNewWindow, UseDefaults);
  5566. if (Result)
  5567. {
  5568. if (SaveSettings)
  5569. {
  5570. GUIConfiguration->SynchronizeParams = Params.Params | UnusedParams;
  5571. GUIConfiguration->SynchronizeOptions = Params.Options;
  5572. }
  5573. else
  5574. {
  5575. if (FLAGSET(GUIConfiguration->SynchronizeOptions, soSynchronizeAsk) &&
  5576. FLAGCLEAR(Params.Options, soSynchronizeAsk) &&
  5577. FLAGSET(Params.Options, soSynchronize))
  5578. {
  5579. GUIConfiguration->SynchronizeOptions =
  5580. (GUIConfiguration->SynchronizeOptions & ~soSynchronizeAsk) |
  5581. soSynchronize;
  5582. }
  5583. }
  5584. LocalDirectory = Params.LocalDirectory;
  5585. RemoteDirectory = Params.RemoteDirectory;
  5586. }
  5587. }
  5588. __finally
  5589. {
  5590. FSynchronizeController = NULL;
  5591. DebugAssert(FOnFeedSynchronizeError == NULL);
  5592. FOnFeedSynchronizeError = NULL;
  5593. DebugAssert(FOnSynchronizeAbort == NULL);
  5594. FOnSynchronizeAbort = NULL;
  5595. FSynchronizeTerminal = NULL;
  5596. }
  5597. return Result;
  5598. }
  5599. //---------------------------------------------------------------------------
  5600. void __fastcall TCustomScpExplorerForm::DoSynchronize(
  5601. TSynchronizeController * /*Sender*/, const UnicodeString LocalDirectory,
  5602. const UnicodeString RemoteDirectory, const TCopyParamType & CopyParam,
  5603. const TSynchronizeParamType & Params, TSynchronizeChecklist ** Checklist,
  5604. TSynchronizeOptions * Options, bool Full)
  5605. {
  5606. // Keep-up-to-date mode
  5607. if (Terminal->Status == ssOpened)
  5608. {
  5609. try
  5610. {
  5611. int PParams = Params.Params;
  5612. if (!Full)
  5613. {
  5614. PParams |= TTerminal::spNoRecurse | TTerminal::spUseCache | TTerminal::spDelayProgress;
  5615. if (FLAGSET(Params.Options, soRecurse))
  5616. {
  5617. PParams |= TTerminal::spSubDirs;
  5618. }
  5619. }
  5620. else
  5621. {
  5622. // if keepuptodate is non-recursive,
  5623. // full sync before has to be non-recursive as well
  5624. if (FLAGCLEAR(Params.Options, soRecurse))
  5625. {
  5626. PParams |= TTerminal::spNoRecurse;
  5627. }
  5628. }
  5629. Synchronize(LocalDirectory, RemoteDirectory, smRemote, CopyParam,
  5630. PParams, Checklist, Options);
  5631. }
  5632. catch (Exception & E)
  5633. {
  5634. if (!Terminal->Active)
  5635. {
  5636. ShowExtendedExceptionEx(Terminal, &E);
  5637. // Do not abort the "keep up to date" when the session was succesfully reconnected.
  5638. if (!Terminal->Active)
  5639. {
  5640. throw;
  5641. }
  5642. }
  5643. else
  5644. {
  5645. if (FLAGSET(Params.Options, soContinueOnError) &&
  5646. DebugAlwaysTrue(FOnFeedSynchronizeError != NULL))
  5647. {
  5648. // noop, already logged in MoreMessageDialog
  5649. }
  5650. else
  5651. {
  5652. // We get mostly EAbort here, so this is noop.
  5653. // Exception is an error when listing directories while looking for differences.
  5654. ShowExtendedExceptionEx(Terminal, &E);
  5655. throw;
  5656. }
  5657. }
  5658. }
  5659. }
  5660. }
  5661. //---------------------------------------------------------------------------
  5662. void __fastcall TCustomScpExplorerForm::DoSynchronizeInvalid(
  5663. TSynchronizeController * /*Sender*/, const UnicodeString Directory,
  5664. const UnicodeString ErrorStr)
  5665. {
  5666. if (!Directory.IsEmpty())
  5667. {
  5668. SimpleErrorDialog(FMTLOAD(WATCH_ERROR_DIRECTORY, (Directory)), ErrorStr);
  5669. }
  5670. else
  5671. {
  5672. SimpleErrorDialog(LoadStr(WATCH_ERROR_GENERAL), ErrorStr);
  5673. }
  5674. }
  5675. //---------------------------------------------------------------------------
  5676. void __fastcall TCustomScpExplorerForm::DoSynchronizeTooManyDirectories(
  5677. TSynchronizeController * /*Sender*/, int & MaxDirectories)
  5678. {
  5679. if (MaxDirectories < GUIConfiguration->MaxWatchDirectories)
  5680. {
  5681. MaxDirectories = GUIConfiguration->MaxWatchDirectories;
  5682. }
  5683. else
  5684. {
  5685. TMessageParams Params(mpNeverAskAgainCheck);
  5686. unsigned int Result = MessageDialog(
  5687. FMTLOAD(TOO_MANY_WATCH_DIRECTORIES, (MaxDirectories, MaxDirectories)),
  5688. qtConfirmation, qaYes | qaNo, HELP_TOO_MANY_WATCH_DIRECTORIES, &Params);
  5689. if ((Result == qaYes) || (Result == qaNeverAskAgain))
  5690. {
  5691. MaxDirectories *= 2;
  5692. if (Result == qaNeverAskAgain)
  5693. {
  5694. GUIConfiguration->MaxWatchDirectories = MaxDirectories;
  5695. }
  5696. }
  5697. else
  5698. {
  5699. Abort();
  5700. }
  5701. }
  5702. }
  5703. //---------------------------------------------------------------------------
  5704. void __fastcall TCustomScpExplorerForm::Synchronize(const UnicodeString LocalDirectory,
  5705. const UnicodeString RemoteDirectory, TSynchronizeMode Mode,
  5706. const TCopyParamType & CopyParam, int Params, TSynchronizeChecklist ** Checklist,
  5707. TSynchronizeOptions * Options)
  5708. {
  5709. // Keep-up-to-date mode
  5710. DebugAssert(!FAutoOperation);
  5711. void * BatchStorage;
  5712. BatchStart(BatchStorage);
  5713. FAutoOperation = true;
  5714. bool AnyOperation = false;
  5715. TDateTime StartTime = Now();
  5716. TSynchronizeChecklist * AChecklist = NULL;
  5717. try
  5718. {
  5719. FSynchronizeProgressForm = new TSynchronizeProgressForm(Application, true, -1);
  5720. if (FLAGCLEAR(Params, TTerminal::spDelayProgress))
  5721. {
  5722. FSynchronizeProgressForm->Start();
  5723. }
  5724. AChecklist = Terminal->SynchronizeCollect(LocalDirectory, RemoteDirectory,
  5725. static_cast<TTerminal::TSynchronizeMode>(Mode),
  5726. &CopyParam, Params | TTerminal::spNoConfirmation, TerminalSynchronizeDirectory,
  5727. Options);
  5728. SetTaskbarListProgressState(TBPF_NOPROGRESS);
  5729. SAFE_DESTROY(FSynchronizeProgressForm);
  5730. AnyOperation = (AChecklist->CheckedCount > 0);
  5731. if (AnyOperation)
  5732. {
  5733. TSynchronizeProgress SynchronizeProgress(AChecklist);
  5734. CreateProgressForm(&SynchronizeProgress);
  5735. try
  5736. {
  5737. Terminal->SynchronizeApply(
  5738. AChecklist, &CopyParam, Params | TTerminal::spNoConfirmation, TerminalSynchronizeDirectory,
  5739. SynchronizeProcessedItem, NULL, NULL, NULL);
  5740. }
  5741. __finally
  5742. {
  5743. DestroyProgressForm();
  5744. }
  5745. }
  5746. }
  5747. __finally
  5748. {
  5749. if (Checklist == NULL)
  5750. {
  5751. delete AChecklist;
  5752. }
  5753. else
  5754. {
  5755. *Checklist = AChecklist;
  5756. }
  5757. FAutoOperation = false;
  5758. SAFE_DESTROY(FSynchronizeProgressForm);
  5759. BatchEnd(BatchStorage);
  5760. ReloadLocalDirectory();
  5761. if (AnyOperation)
  5762. {
  5763. OperationComplete(StartTime);
  5764. }
  5765. }
  5766. }
  5767. //---------------------------------------------------------------------------
  5768. bool __fastcall TCustomScpExplorerForm::SynchronizeAllowSelectedOnly()
  5769. {
  5770. // can be called from command line
  5771. return Visible &&
  5772. ((DirView(osRemote)->SelCount > 0) ||
  5773. (HasDirView[osLocal] && (DirView(osLocal)->SelCount > 0)));
  5774. }
  5775. //---------------------------------------------------------------------------
  5776. void __fastcall TCustomScpExplorerForm::SynchronizeSessionLog(const UnicodeString & Message)
  5777. {
  5778. LogSynchronizeEvent(Terminal, Message);
  5779. }
  5780. //---------------------------------------------------------------------------
  5781. void __fastcall TCustomScpExplorerForm::GetSynchronizeOptions(
  5782. int Params, TSynchronizeOptions & Options)
  5783. {
  5784. DebugAssert(!IsLocalBrowserMode());
  5785. if (FLAGSET(Params, TTerminal::spSelectedOnly) && SynchronizeAllowSelectedOnly())
  5786. {
  5787. Options.Filter = new TStringList();
  5788. Options.Filter->CaseSensitive = false;
  5789. Options.Filter->Duplicates = Types::dupAccept;
  5790. if (DirView(osRemote)->SelCount > 0)
  5791. {
  5792. DirView(osRemote)->CreateFileList(false, false, Options.Filter);
  5793. }
  5794. if (HasDirView[osLocal] && (DirView(osLocal)->SelCount > 0))
  5795. {
  5796. DirView(osLocal)->CreateFileList(false, false, Options.Filter);
  5797. }
  5798. Options.Filter->Sort();
  5799. }
  5800. }
  5801. //---------------------------------------------------------------------------
  5802. UnicodeString __fastcall TCustomScpExplorerForm::SerializeCopyParamForCommandLine(const TCopyParamType * CopyParams)
  5803. {
  5804. TCopyParamType Defaults;
  5805. std::unique_ptr<THierarchicalStorage> ConfigStorage(Configuration->CreateConfigStorage());
  5806. ConfigStorage->AccessMode = smRead;
  5807. if (ConfigStorage->OpenSubKey(Configuration->ConfigurationSubKey, false))
  5808. {
  5809. GUIConfiguration->LoadCopyParam(ConfigStorage.get(), &Defaults);
  5810. }
  5811. ConfigStorage.reset(NULL);
  5812. std::unique_ptr<TStringList> Options(new TStringList());
  5813. std::unique_ptr<TOptionsStorage> OptionsStorage(new TOptionsStorage(Options.get(), true));
  5814. CopyParams->Save(OptionsStorage.get(), &Defaults);
  5815. UnicodeString Result;
  5816. if (Options->Count > 0)
  5817. {
  5818. Result =
  5819. FORMAT(L" %s%s", (TProgramParams::FormatSwitch(RAWTRANSFERSETTINGS_SWITCH), StringsToParams(Options.get())));
  5820. }
  5821. return Result;
  5822. }
  5823. //---------------------------------------------------------------------------
  5824. void __fastcall TCustomScpExplorerForm::SynchronizeInNewWindow(
  5825. const TSynchronizeParamType & Params, const TCopyParamType * CopyParams)
  5826. {
  5827. UnicodeString SessionName = CreateHiddenDuplicateSession();
  5828. UnicodeString AdditionalParams =
  5829. FORMAT(L"%s%s %s // \"%s\" \"%s\" %d %d", (
  5830. TProgramParams::FormatSwitch(DEFAULTS_SWITCH),
  5831. SerializeCopyParamForCommandLine(CopyParams),
  5832. TProgramParams::FormatSwitch(KEEP_UP_TO_DATE_SWITCH),
  5833. Params.LocalDirectory, Params.RemoteDirectory, Params.Params, Params.Options));
  5834. ExecuteNewInstance(SessionName, AdditionalParams);
  5835. }
  5836. //---------------------------------------------------------------------------
  5837. struct TSynchronizeParams
  5838. {
  5839. UnicodeString LocalDirectory;
  5840. UnicodeString RemoteDirectory;
  5841. TSynchronizeMode Mode;
  5842. TSynchronizeChecklist * Checklist;
  5843. int Params;
  5844. TCopyParamType * CopyParam;
  5845. TDateTime CollectElapsed;
  5846. TDateTime * StartTime;
  5847. TProcessedSynchronizationChecklistItem OnProcessedItem;
  5848. };
  5849. //---------------------------------------------------------------------------
  5850. void __fastcall TCustomScpExplorerForm::FullSynchronize(
  5851. TSynchronizeParams & Params, TProcessedSynchronizationChecklistItem OnProcessedItem,
  5852. TUpdatedSynchronizationChecklistItems OnUpdatedSynchronizationChecklistItems)
  5853. {
  5854. DebugAssert(!FAutoOperation);
  5855. void * BatchStorage;
  5856. BatchStart(BatchStorage);
  5857. FAutoOperation = true;
  5858. TFileOperationStatistics Statistics;
  5859. TDateTime Start = Now();
  5860. try
  5861. {
  5862. Params.OnProcessedItem = OnProcessedItem;
  5863. TSynchronizeProgress SynchronizeProgress(Params.Checklist);
  5864. CreateProgressForm(&SynchronizeProgress);
  5865. Terminal->SynchronizeApply(
  5866. Params.Checklist, Params.CopyParam, Params.Params | TTerminal::spNoConfirmation,
  5867. TerminalSynchronizeDirectory, SynchronizeProcessedItem, OnUpdatedSynchronizationChecklistItems, &Params,
  5868. &Statistics);
  5869. }
  5870. __finally
  5871. {
  5872. Params.OnProcessedItem = NULL;
  5873. FAutoOperation = false;
  5874. DestroyProgressForm();
  5875. BatchEnd(BatchStorage);
  5876. ReloadLocalDirectory();
  5877. }
  5878. if (WinConfiguration->SynchronizeSummary)
  5879. {
  5880. UnicodeString Message = MainInstructions(LoadStr(SYNCHRONIZE_COMPLETE)) + L"\n";
  5881. // The statistics should be 0 anyway in this case
  5882. if (FLAGCLEAR(Params.Params, TTerminal::spTimestamp))
  5883. {
  5884. Message += L"\n";
  5885. if (Statistics.FilesUploaded > 0)
  5886. {
  5887. Message += FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 1), (FormatNumber(Statistics.FilesUploaded), FormatBytes(Statistics.TotalUploaded))) + L"\n";
  5888. }
  5889. if (Statistics.FilesDownloaded > 0)
  5890. {
  5891. Message += FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 2), (FormatNumber(Statistics.FilesDownloaded), FormatBytes(Statistics.TotalDownloaded))) + L"\n";
  5892. }
  5893. if (Statistics.FilesDeletedLocal > 0)
  5894. {
  5895. Message += FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 3), (FormatNumber(Statistics.FilesDeletedLocal))) + L"\n";
  5896. }
  5897. if (Statistics.FilesDeletedRemote > 0)
  5898. {
  5899. Message += FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 4), (FormatNumber(Statistics.FilesDeletedRemote))) + L"\n";
  5900. }
  5901. }
  5902. TDateTime Elapsed = (Now() - Start);
  5903. Message +=
  5904. L"\n" +
  5905. FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 5), (FormatDateTimeSpan(Params.CollectElapsed))) + L"\n" +
  5906. FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 6), (FormatDateTimeSpan(Elapsed)));
  5907. TMessageParams Params(mpNeverAskAgainCheck);
  5908. unsigned int Result = MessageDialog(Message, qtInformation, qaOK, HELP_NONE, &Params);
  5909. if (Result == qaNeverAskAgain)
  5910. {
  5911. WinConfiguration->SynchronizeSummary = false;
  5912. }
  5913. }
  5914. }
  5915. //---------------------------------------------------------------------------
  5916. void __fastcall TCustomScpExplorerForm::SynchronizeProcessedItem(void * Token, const TSynchronizeChecklist::TItem * ChecklistItem)
  5917. {
  5918. if (DebugAlwaysTrue(FProgressForm != NULL) && DebugAlwaysTrue(FProgressForm->SynchronizeProgress != NULL))
  5919. {
  5920. FProgressForm->SynchronizeProgress->ItemProcessed(ChecklistItem);
  5921. }
  5922. // Not set in keep-up-to-date mode - Synchronize() method
  5923. if (Token != NULL)
  5924. {
  5925. TSynchronizeParams & Params = *static_cast<TSynchronizeParams *>(Token);
  5926. if (Params.OnProcessedItem != NULL)
  5927. {
  5928. Params.OnProcessedItem(NULL, ChecklistItem);
  5929. }
  5930. }
  5931. }
  5932. //---------------------------------------------------------------------------
  5933. void __fastcall TCustomScpExplorerForm::DoFullSynchronize(
  5934. void * Token, TProcessedSynchronizationChecklistItem OnProcessedItem,
  5935. TUpdatedSynchronizationChecklistItems OnUpdatedSynchronizationChecklistItems)
  5936. {
  5937. TSynchronizeParams & Params = *static_cast<TSynchronizeParams *>(Token);
  5938. *Params.StartTime = Now();
  5939. FullSynchronize(Params, OnProcessedItem, OnUpdatedSynchronizationChecklistItems);
  5940. }
  5941. //---------------------------------------------------------------------------
  5942. void __fastcall TCustomScpExplorerForm::DoSynchronizeChecklistCalculateSize(
  5943. TSynchronizeChecklist * Checklist, const TSynchronizeChecklist::TItemList & Items, void * Token)
  5944. {
  5945. // terminal can be already closed (e.g. dropped connection)
  5946. if (Terminal != NULL)
  5947. {
  5948. TSynchronizeParams & Params = *static_cast<TSynchronizeParams *>(Token);
  5949. Terminal->SynchronizeChecklistCalculateSize(Checklist, Items, Params.CopyParam);
  5950. }
  5951. }
  5952. //---------------------------------------------------------------------------
  5953. void __fastcall TCustomScpExplorerForm::DoSynchronizeMove(
  5954. TOperationSide Side, const UnicodeString & FileName, const UnicodeString & NewFileName, TRemoteFile * RemoteFile)
  5955. {
  5956. DebugAssert(!IsLocalBrowserMode());
  5957. TAutoBatch AutoBatch(this);
  5958. TAutoFlag AutoOperationFlag(FAutoOperation);
  5959. if (Side == osRemote)
  5960. {
  5961. std::unique_ptr<TStrings> FileList(new TStringList());
  5962. FileList->AddObject(FileName, RemoteFile);
  5963. UnicodeString Target = UnixExtractFileDir(NewFileName);
  5964. UnicodeString FileMask = DelimitFileNameMask(UnixExtractFileName(NewFileName));
  5965. RemoteDirView->SaveSelection();
  5966. RemoteDirView->SaveSelectedNames();
  5967. try
  5968. {
  5969. Terminal->MoveFiles(FileList.get(), Target, FileMask, false);
  5970. }
  5971. catch(...)
  5972. {
  5973. RemoteDirView->DiscardSavedSelection();
  5974. throw;
  5975. }
  5976. RemoteDirView->RestoreSelection();
  5977. }
  5978. else if (DebugAlwaysTrue(Side == osLocal))
  5979. {
  5980. if (!MoveFile(FileName.c_str(), NewFileName.c_str()))
  5981. {
  5982. throw EOSExtException(FMTLOAD(RENAME_FILE_ERROR, (FileName, NewFileName)));
  5983. }
  5984. UnicodeString Directory = ExtractFileDir(FileName);
  5985. ReloadLocalDirectory(Directory);
  5986. UnicodeString NewDirectory = ExtractFileDir(NewFileName);
  5987. if (!SamePaths(Directory, NewDirectory))
  5988. {
  5989. ReloadLocalDirectory(NewDirectory);
  5990. }
  5991. }
  5992. }
  5993. //---------------------------------------------------------------------------
  5994. void __fastcall TCustomScpExplorerForm::DoSynchronizeBrowse(TOperationSide Side, TSynchronizeChecklist::TAction Action, const TSynchronizeChecklist::TItem * Item)
  5995. {
  5996. UnicodeString LocalPath = ExcludeTrailingBackslash(Item->Local.Directory);
  5997. if (Side == osLocal)
  5998. {
  5999. if (Action == TSynchronizeChecklist::saDownloadNew)
  6000. {
  6001. OpenFolderInExplorer(LocalPath);
  6002. }
  6003. else
  6004. {
  6005. OpenFileInExplorer(CombinePaths(LocalPath, Item->GetFileName()));
  6006. }
  6007. }
  6008. else if (DebugAlwaysTrue(Side == osRemote))
  6009. {
  6010. // Similar to CreateHiddenDuplicateSession, except that it modifies the initial directories
  6011. std::unique_ptr<TSessionData> SessionData(CloneCurrentSessionData());
  6012. SessionData->RemoteDirectory = UnixExcludeTrailingBackslash(Item->Remote.Directory);
  6013. if (!LocalPath.IsEmpty())
  6014. {
  6015. SessionData->LocalDirectory = LocalPath;
  6016. }
  6017. UnicodeString SessionName = SaveHiddenDuplicateSession(SessionData.get());
  6018. ExecuteNewInstance(SessionName, FORMAT(L"%s=\"%s\"", (TProgramParams::FormatSwitch(BROWSE_SWITCH), Item->GetFileName())));
  6019. }
  6020. }
  6021. //---------------------------------------------------------------------------
  6022. void __fastcall TCustomScpExplorerForm::FullSynchronizeInNewWindow(
  6023. TSynchronizeMode Mode, int Params, const UnicodeString & LocalDirectory, const UnicodeString & RemoteDirectory,
  6024. const TCopyParamType * CopyParams)
  6025. {
  6026. UnicodeString SessionName = CreateHiddenDuplicateSession();
  6027. UnicodeString AdditionalParams =
  6028. FORMAT(L"%s%s %s // \"%s\" \"%s\" %d %d", (
  6029. TProgramParams::FormatSwitch(DEFAULTS_SWITCH),
  6030. SerializeCopyParamForCommandLine(CopyParams),
  6031. TProgramParams::FormatSwitch(SYNCHRONIZE_SWITCH),
  6032. LocalDirectory, RemoteDirectory, Mode, Params));
  6033. ExecuteNewInstance(SessionName, AdditionalParams);
  6034. }
  6035. //---------------------------------------------------------------------------
  6036. int __fastcall TCustomScpExplorerForm::DoFullSynchronizeDirectories(
  6037. UnicodeString & LocalDirectory, UnicodeString & RemoteDirectory,
  6038. TSynchronizeMode & Mode, int Params, bool & SaveMode, int UseDefaults)
  6039. {
  6040. int Result;
  6041. bool SaveSettings = false;
  6042. int Options =
  6043. FLAGMASK(!Terminal->IsCapable[fcTimestampChanging], fsoDisableTimestamp) |
  6044. FLAGMASK(!CanCalculateChecksum(), fsoDisableByChecksum) |
  6045. FLAGMASK(SynchronizeAllowSelectedOnly(), fsoAllowSelectedOnly);
  6046. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  6047. TUsableCopyParamAttrs CopyParamAttrs = Terminal->UsableCopyParamAttrs(0);
  6048. bool Continue =
  6049. ((UseDefaults == 0) ||
  6050. DoFullSynchronizeDialog(Mode, Params, LocalDirectory, RemoteDirectory,
  6051. &CopyParam, SaveSettings, SaveMode, Options, CopyParamAttrs, FullSynchronizeInNewWindow, UseDefaults)) &&
  6052. (FLAGCLEAR(Params, TTerminal::spByChecksum) || EnsureCommandSessionFallback(fcCalculatingChecksum));
  6053. if (Continue)
  6054. {
  6055. Configuration->Usage->Inc(L"Synchronizations");
  6056. CopyParam.IncludeFileMask.SetRoots(LocalDirectory, RemoteDirectory);
  6057. UpdateCopyParamCounters(CopyParam);
  6058. TSynchronizeOptions SynchronizeOptions;
  6059. GetSynchronizeOptions(Params, SynchronizeOptions);
  6060. if (SaveSettings)
  6061. {
  6062. GUIConfiguration->SynchronizeParams = Params;
  6063. }
  6064. else
  6065. {
  6066. SaveMode = false;
  6067. }
  6068. TDateTime StartTime = Now();
  6069. TSynchronizeChecklist * Checklist = NULL;
  6070. try
  6071. {
  6072. DebugAssert(!FAutoOperation);
  6073. FAutoOperation = true;
  6074. try
  6075. {
  6076. UnicodeString SessionKey = Terminal->SessionData->SessionKey;
  6077. std::unique_ptr<TStrings> DataList(Configuration->LoadDirectoryStatisticsCache(SessionKey, RemoteDirectory, CopyParam));
  6078. int Files = -1;
  6079. if (DataList->Count >= 1)
  6080. {
  6081. Files = StrToIntDef(DataList->Strings[0], Files);
  6082. }
  6083. else
  6084. {
  6085. DataList->Add(UnicodeString());
  6086. }
  6087. FSynchronizeProgressForm = new TSynchronizeProgressForm(Application, true, Files);
  6088. FSynchronizeProgressForm->Start();
  6089. Checklist = Terminal->SynchronizeCollect(LocalDirectory, RemoteDirectory,
  6090. static_cast<TTerminal::TSynchronizeMode>(Mode),
  6091. &CopyParam, Params | TTerminal::spNoConfirmation, TerminalSynchronizeDirectory,
  6092. &SynchronizeOptions);
  6093. if (Terminal->SessionData->CacheDirectories)
  6094. {
  6095. DataList->Strings[0] = IntToStr(SynchronizeOptions.Files);
  6096. Configuration->SaveDirectoryStatisticsCache(SessionKey, RemoteDirectory, CopyParam, DataList.get());
  6097. }
  6098. }
  6099. __finally
  6100. {
  6101. FAutoOperation = false;
  6102. SetTaskbarListProgressState(TBPF_NOPROGRESS);
  6103. SAFE_DESTROY(FSynchronizeProgressForm);
  6104. }
  6105. TSynchronizeParams SynchronizeParams;
  6106. SynchronizeParams.LocalDirectory = LocalDirectory;
  6107. SynchronizeParams.RemoteDirectory = RemoteDirectory;
  6108. SynchronizeParams.Mode = Mode;
  6109. SynchronizeParams.CopyParam = &CopyParam;
  6110. SynchronizeParams.Params = Params;
  6111. SynchronizeParams.Checklist = Checklist;
  6112. SynchronizeParams.CollectElapsed = (Now() - StartTime);
  6113. SynchronizeParams.StartTime = &StartTime;
  6114. SynchronizeParams.OnProcessedItem = NULL;
  6115. Result = Checklist->Count;
  6116. if (Checklist->Count > 0)
  6117. {
  6118. if (FLAGSET(Params, TTerminal::spPreviewChanges))
  6119. {
  6120. if (!DoSynchronizeChecklistDialog(
  6121. Checklist, Mode, Params, LocalDirectory, RemoteDirectory, CustomCommandMenu, DoFullSynchronize,
  6122. DoSynchronizeChecklistCalculateSize, DoSynchronizeMove, DoSynchronizeBrowse, &SynchronizeParams))
  6123. {
  6124. Result = -1;
  6125. }
  6126. }
  6127. else
  6128. {
  6129. FullSynchronize(SynchronizeParams, NULL, NULL);
  6130. }
  6131. }
  6132. else
  6133. {
  6134. UnicodeString Message = MainInstructions(LoadStr(COMPARE_NO_DIFFERENCES));
  6135. MessageDialog(Message, qtInformation, qaOK, HELP_SYNCHRONIZE_NO_DIFFERENCES);
  6136. }
  6137. }
  6138. __finally
  6139. {
  6140. delete Checklist;
  6141. }
  6142. OperationComplete(StartTime);
  6143. }
  6144. else
  6145. {
  6146. Result = -1;
  6147. }
  6148. return Result;
  6149. }
  6150. //---------------------------------------------------------------------------
  6151. void __fastcall TCustomScpExplorerForm::TerminalSynchronizeDirectory(
  6152. const UnicodeString & LocalDirectory, const UnicodeString & RemoteDirectory,
  6153. bool & Continue, bool Collect, const TSynchronizeOptions * Options)
  6154. {
  6155. if (Collect)
  6156. {
  6157. DebugAssert(FSynchronizeProgressForm != NULL);
  6158. if (!FSynchronizeProgressForm->Started)
  6159. {
  6160. FSynchronizeProgressForm->Start();
  6161. }
  6162. int CompareProgress = FSynchronizeProgressForm->SetData(LocalDirectory, RemoteDirectory, Options->Files, Continue);
  6163. SetTaskbarListProgressValue(CompareProgress);
  6164. }
  6165. else
  6166. {
  6167. DebugAssert(FSynchronizeProgressForm == NULL);
  6168. DebugAssert(FProgressForm != NULL);
  6169. }
  6170. }
  6171. //---------------------------------------------------------------------------
  6172. void __fastcall TCustomScpExplorerForm::StandaloneEdit(const UnicodeString & FileName)
  6173. {
  6174. UnicodeString FullFileName = AbsolutePath(Terminal->CurrentDirectory, FileName);
  6175. std::unique_ptr<TRemoteFile> File(Terminal->ReadFile(FullFileName));
  6176. if (File.get() != NULL)
  6177. {
  6178. ExecuteRemoteFile(FullFileName, File.get(), efInternalEditor);
  6179. Application->ShowMainForm = false;
  6180. Application->Run();
  6181. }
  6182. }
  6183. //---------------------------------------------------------------------------
  6184. void __fastcall TCustomScpExplorerForm::ExploreLocalDirectory(TOperationSide)
  6185. {
  6186. DebugFail();
  6187. }
  6188. //---------------------------------------------------------------------------
  6189. TSessionData * __fastcall TCustomScpExplorerForm::CloneCurrentSessionData()
  6190. {
  6191. std::unique_ptr<TSessionData> SessionData(new TSessionData(L""));
  6192. SessionData->Assign(ManagedSession->SessionData);
  6193. UpdateSessionData(SessionData.get());
  6194. DebugAssert(SessionData->IsLocalBrowser == IsLocalBrowserMode());
  6195. if (IsActiveTerminal(ManagedSession))
  6196. {
  6197. ManagedSession->UpdateSessionCredentials(SessionData.get());
  6198. }
  6199. if (!ManagedSession->SessionData->HasSessionName())
  6200. {
  6201. // Particularly for "Workspace/XXXX" name, we need to reset it, as it would become user-visible
  6202. // once IsWorkspace is cleared
  6203. SessionData->Name = UnicodeString();
  6204. }
  6205. // Most uses of this method ends with saving the session, and we do not want to store the IsWorkspace flag.
  6206. // Particularly, with SaveHiddenDuplicateSession, it won't work as TSessionData::ParseUrl explicitly ignored workspace sessions.
  6207. SessionData->IsWorkspace = false;
  6208. return SessionData.release();
  6209. }
  6210. //---------------------------------------------------------------------------
  6211. void __fastcall TCustomScpExplorerForm::SaveCurrentSession()
  6212. {
  6213. TSessionData * SessionData = CloneCurrentSessionData();
  6214. try
  6215. {
  6216. TSessionData * EditingSessionData = StoredSessions->FindSame(SessionData);
  6217. DoSaveSession(SessionData, EditingSessionData, true, NULL);
  6218. }
  6219. __finally
  6220. {
  6221. delete SessionData;
  6222. }
  6223. }
  6224. //---------------------------------------------------------------------------
  6225. TObjectList * __fastcall TCustomScpExplorerForm::DoCollectWorkspace()
  6226. {
  6227. TTerminalManager * Manager = TTerminalManager::Instance();
  6228. std::unique_ptr<TObjectList> DataList(new TObjectList());
  6229. if (DebugAlwaysTrue(ManagedSession != NULL))
  6230. {
  6231. // Update ManagedSession->StateData
  6232. UpdateSession(ManagedSession);
  6233. }
  6234. Manager->SaveWorkspace(DataList.get());
  6235. return DataList.release();
  6236. }
  6237. //---------------------------------------------------------------------------
  6238. void __fastcall TCustomScpExplorerForm::DoSaveWorkspace(
  6239. const UnicodeString & Name, TObjectList * DataList, bool SavePasswords, bool Explicit)
  6240. {
  6241. WinConfiguration->LastWorkspace = Name;
  6242. if (!SavePasswords)
  6243. {
  6244. for (int Index = 0; Index < DataList->Count; Index++)
  6245. {
  6246. TSessionData * SessionData = dynamic_cast<TSessionData *>(DataList->Items[Index]);
  6247. if (SessionData->Link.IsEmpty())
  6248. {
  6249. SessionData->ClearSessionPasswords();
  6250. }
  6251. }
  6252. }
  6253. StoredSessions->NewWorkspace(Name, DataList);
  6254. // modified only
  6255. StoredSessions->Save(false, Explicit);
  6256. }
  6257. //---------------------------------------------------------------------------
  6258. UnicodeString __fastcall TCustomScpExplorerForm::WorkspaceName()
  6259. {
  6260. return
  6261. DefaultStr(WinConfiguration->LastWorkspace,
  6262. DefaultStr(WinConfiguration->AutoWorkspace, LoadStr(NEW_WORKSPACE)));
  6263. }
  6264. //---------------------------------------------------------------------------
  6265. bool __fastcall TCustomScpExplorerForm::SaveWorkspace(bool EnableAutoSave)
  6266. {
  6267. std::unique_ptr<TObjectList> DataList(DoCollectWorkspace());
  6268. bool AnyNonStoredSessionWithPassword = false;
  6269. bool AnyNonStoredNonWorkspaceSessionWithPassword = false;
  6270. bool AllNonStoredSessionsAnonymous = true;
  6271. TTerminalManager * Manager = TTerminalManager::Instance();
  6272. for (int Index = 0; Index < DataList->Count; Index++)
  6273. {
  6274. TSessionData * SessionData = dynamic_cast<TSessionData *>(DataList->Items[Index]);
  6275. if (SessionData->Link.IsEmpty())
  6276. {
  6277. if (SessionData->HasAnySessionPassword())
  6278. {
  6279. AnyNonStoredSessionWithPassword = true;
  6280. if (!Manager->Sessions[Index]->SessionData->IsWorkspace)
  6281. {
  6282. AnyNonStoredNonWorkspaceSessionWithPassword = true;
  6283. }
  6284. }
  6285. if (!SameText(SessionData->UserName, AnonymousUserName))
  6286. {
  6287. AllNonStoredSessionsAnonymous = false;
  6288. }
  6289. }
  6290. }
  6291. bool SavePasswords;
  6292. bool * PSavePasswords;
  6293. bool NotRecommendedSavingPasswords =
  6294. !CustomWinConfiguration->UseMasterPassword &&
  6295. !AllNonStoredSessionsAnonymous;
  6296. if (Configuration->DisablePasswordStoring ||
  6297. !AnyNonStoredSessionWithPassword)
  6298. {
  6299. PSavePasswords = NULL;
  6300. SavePasswords = false;
  6301. }
  6302. else
  6303. {
  6304. PSavePasswords = &SavePasswords;
  6305. SavePasswords =
  6306. !AnyNonStoredNonWorkspaceSessionWithPassword ||
  6307. !NotRecommendedSavingPasswords;
  6308. }
  6309. UnicodeString Name = WorkspaceName();
  6310. bool CreateShortcut = false;
  6311. bool Result =
  6312. DoSaveWorkspaceDialog(
  6313. Name, PSavePasswords, NotRecommendedSavingPasswords, CreateShortcut,
  6314. EnableAutoSave);
  6315. if (Result)
  6316. {
  6317. DoSaveWorkspace(Name, DataList.get(), SavePasswords, true);
  6318. if (CreateShortcut)
  6319. {
  6320. TOperationVisualizer Visualizer;
  6321. UnicodeString AdditionalParams = TProgramParams::FormatSwitch(DESKTOP_SWITCH);
  6322. CreateDesktopSessionShortCut(Name, L"", AdditionalParams, -1, WORKSPACE_ICON);
  6323. }
  6324. if (EnableAutoSave)
  6325. {
  6326. WinConfiguration->AutoSaveWorkspace = true;
  6327. WinConfiguration->AutoWorkspace = Name;
  6328. if (PSavePasswords != NULL)
  6329. {
  6330. WinConfiguration->AutoSaveWorkspacePasswords = SavePasswords;
  6331. }
  6332. }
  6333. }
  6334. return Result;
  6335. }
  6336. //---------------------------------------------------------------------------
  6337. void __fastcall TCustomScpExplorerForm::UpdateSession(TManagedTerminal * Session)
  6338. {
  6339. DebugAssert(IsLocalBrowserMode() == Session->LocalBrowser);
  6340. DebugAssert(Session == ManagedSession);
  6341. if (!IsLocalBrowserMode())
  6342. {
  6343. SAFE_DESTROY(Session->RemoteExplorerState);
  6344. if (WinConfiguration->PreservePanelState)
  6345. {
  6346. Session->RemoteExplorerState = RemoteDirView->SaveState();
  6347. }
  6348. }
  6349. UpdateSessionData(Session->StateData);
  6350. }
  6351. //---------------------------------------------------------------------------
  6352. void __fastcall TCustomScpExplorerForm::UpdateSessionData(TSessionData * Data)
  6353. {
  6354. // Keep in sync with TSessionData::CopyStateData
  6355. DebugAssert(Data != NULL);
  6356. // This is inconsistent with how color (for example) is handled.
  6357. Data->Name = ManagedSession->SessionData->Name;
  6358. if (!IsLocalBrowserMode())
  6359. {
  6360. // cannot use RemoteDirView->Path, because it is empty if connection
  6361. // was already closed
  6362. // also only peek, we may not be connected at all atm
  6363. // so make sure we do not try retrieving current directory from the server
  6364. // (particularly with FTP)
  6365. UnicodeString ACurrentDirectory = Terminal->PeekCurrentDirectory();
  6366. if (!ACurrentDirectory.IsEmpty())
  6367. {
  6368. Data->RemoteDirectory = ACurrentDirectory;
  6369. }
  6370. }
  6371. Data->Color = SessionColor;
  6372. }
  6373. //---------------------------------------------------------------------------
  6374. void __fastcall TCustomScpExplorerForm::ToolBarResize(TObject *Sender)
  6375. {
  6376. TTBXToolbar * Toolbar = dynamic_cast<TTBXToolbar*>(Sender);
  6377. DebugAssert(Toolbar != NULL);
  6378. for (int i = 0; i < Toolbar->Items->Count; i++)
  6379. {
  6380. TTBXCustomDropDownItem * DropDownItem;
  6381. DropDownItem = dynamic_cast<TTBXCustomDropDownItem *>(Toolbar->Items->Items[i]);
  6382. if (DropDownItem != NULL)
  6383. {
  6384. ToolbarItemResize(DropDownItem,
  6385. Toolbar->Width - (Toolbar->View->BaseSize.x - DropDownItem->EditWidth) -
  6386. Toolbar->NonClientWidth);
  6387. break;
  6388. }
  6389. }
  6390. }
  6391. //---------------------------------------------------------------------------
  6392. void __fastcall TCustomScpExplorerForm::ToolbarItemResize(TTBXCustomDropDownItem * Item, int Width)
  6393. {
  6394. Item->EditWidth = Width;
  6395. }
  6396. //---------------------------------------------------------------------------
  6397. void __fastcall TCustomScpExplorerForm::ToolbarGetBaseSize(
  6398. TTBCustomToolbar * Toolbar, TPoint & ASize)
  6399. {
  6400. for (int i = 0; i < Toolbar->Items->Count; i++)
  6401. {
  6402. TTBXCustomDropDownItem * DropDownItem;
  6403. DropDownItem = dynamic_cast<TTBXCustomDropDownItem *>(Toolbar->Items->Items[i]);
  6404. if (DropDownItem != NULL)
  6405. {
  6406. ASize.x -= DropDownItem->EditWidth;
  6407. ASize.x += ScaleByTextHeight(this, 50) /* minimal combo width */;
  6408. }
  6409. }
  6410. }
  6411. //---------------------------------------------------------------------------
  6412. void __fastcall TCustomScpExplorerForm::DoWarnLackOfTempSpace(
  6413. const UnicodeString Path, __int64 RequiredSpace, bool & Continue)
  6414. {
  6415. if (WinConfiguration->DDWarnLackOfTempSpace)
  6416. {
  6417. UnicodeString ADrive = ExtractFileDrive(ExpandFileName(Path));
  6418. if (!ADrive.IsEmpty())
  6419. {
  6420. __int64 FreeSpace = DiskFree((Byte)(ADrive[1]-'A'+1));
  6421. DebugAssert(RequiredSpace >= 0);
  6422. __int64 RequiredWithReserve;
  6423. RequiredWithReserve = (__int64)(RequiredSpace * WinConfiguration->DDWarnLackOfTempSpaceRatio);
  6424. if (FreeSpace < RequiredWithReserve)
  6425. {
  6426. unsigned int Result;
  6427. TMessageParams Params(mpNeverAskAgainCheck);
  6428. Result = MessageDialog(FMTLOAD(DD_WARN_LACK_OF_TEMP_SPACE, (Path,
  6429. FormatBytes(FreeSpace), FormatBytes(RequiredSpace))),
  6430. qtWarning, qaYes | qaNo, HELP_DD_WARN_LACK_OF_TEMP_SPACE, &Params);
  6431. if (Result == qaNeverAskAgain)
  6432. {
  6433. WinConfiguration->DDWarnLackOfTempSpace = false;
  6434. }
  6435. Continue = (Result == qaYes || Result == qaNeverAskAgain);
  6436. }
  6437. }
  6438. }
  6439. }
  6440. //---------------------------------------------------------------------------
  6441. void __fastcall TCustomScpExplorerForm::AddBookmark(TOperationSide Side)
  6442. {
  6443. DoOpenDirectoryDialog(odAddBookmark, Side);
  6444. }
  6445. //---------------------------------------------------------------------------
  6446. TStrings * __fastcall TCustomScpExplorerForm::CreateVisitedDirectories(TOperationSide Side)
  6447. {
  6448. // we should better use TCustomDirView::FCaseSensitive, but it is private
  6449. TStringList * VisitedDirectories = CreateSortedStringList(!IsSideLocalBrowser(Side));
  6450. try
  6451. {
  6452. TCustomDirView * DView = DirView(Side);
  6453. for (int Index = -DView->BackCount; Index <= DView->ForwardCount; Index++)
  6454. {
  6455. VisitedDirectories->Add(DView->HistoryPath[Index]);
  6456. }
  6457. }
  6458. catch (...)
  6459. {
  6460. delete VisitedDirectories;
  6461. throw;
  6462. }
  6463. return VisitedDirectories;
  6464. }
  6465. //---------------------------------------------------------------------------
  6466. void __fastcall TCustomScpExplorerForm::DoOpenDirectoryDialog(
  6467. TOpenDirectoryMode Mode, TOperationSide Side)
  6468. {
  6469. bool Continue = true;
  6470. if (Mode == odAddBookmark)
  6471. {
  6472. TMessageParams Params(mpNeverAskAgainCheck);
  6473. Params.NeverAskAgainTitle = LoadStr(ADD_BOOKMARK_SHARED);
  6474. Params.NeverAskAgainCheckedInitially = WinConfiguration->UseSharedBookmarks;
  6475. UnicodeString Message = MainInstructions(FMTLOAD(ADD_BOOKMARK_CONFIRM, (DirView(Side)->PathName)));
  6476. unsigned int Answer =
  6477. MessageDialog(Message,
  6478. qtConfirmation, qaOK | qaCancel, HELP_ADD_BOOKMARK_CONFIRM, &Params);
  6479. if (Answer == qaNeverAskAgain)
  6480. {
  6481. Continue = true;
  6482. WinConfiguration->UseSharedBookmarks = true;
  6483. }
  6484. else if (Answer == qaOK)
  6485. {
  6486. Continue = true;
  6487. WinConfiguration->UseSharedBookmarks = false;
  6488. }
  6489. else
  6490. {
  6491. Continue = false;
  6492. }
  6493. }
  6494. if (Continue)
  6495. {
  6496. TStrings * VisitedDirectories = CreateVisitedDirectories(Side);
  6497. try
  6498. {
  6499. UnicodeString Name = DirView(Side)->PathName;
  6500. bool Repeat;
  6501. do
  6502. {
  6503. Repeat = false;
  6504. TOperationSide BookmarkSide = IsSideLocalBrowser(Side) ? osLocal : osRemote;
  6505. if (::DoOpenDirectoryDialog(Mode, BookmarkSide, Name, VisitedDirectories, Terminal,
  6506. // do not allow switching to location profiles,
  6507. // if we are not connected
  6508. HasDirView[osLocal] && (Terminal != NULL)))
  6509. {
  6510. TWindowLock Lock(this);
  6511. if (!TryOpenDirectory(Side, Name))
  6512. {
  6513. Repeat = true;
  6514. Mode = odBrowse;
  6515. }
  6516. }
  6517. }
  6518. while (Repeat);
  6519. }
  6520. __finally
  6521. {
  6522. delete VisitedDirectories;
  6523. }
  6524. }
  6525. }
  6526. //---------------------------------------------------------------------------
  6527. bool __fastcall TCustomScpExplorerForm::CommandSessionFallback()
  6528. {
  6529. DebugAssert(!Terminal->CommandSessionOpened);
  6530. return TTerminalManager::Instance()->ConnectTerminal(Terminal->CommandSession);
  6531. }
  6532. //---------------------------------------------------------------------------
  6533. bool __fastcall TCustomScpExplorerForm::EnsureCommandSessionFallback(TFSCapability Capability)
  6534. {
  6535. bool Result = Terminal->IsCapable[Capability] ||
  6536. Terminal->CommandSessionOpened;
  6537. if (!Result)
  6538. {
  6539. // We might get here for example when "checksum" synchronization is requested from command-line
  6540. // on server that supports neither checksum nor shell
  6541. if (!Terminal->IsCapable[fcSecondaryShell])
  6542. {
  6543. NotSupported();
  6544. }
  6545. else if (!GUIConfiguration->ConfirmCommandSession)
  6546. {
  6547. Result = true;
  6548. }
  6549. else
  6550. {
  6551. TMessageParams Params(mpNeverAskAgainCheck);
  6552. const TFileSystemInfo & FileSystemInfo = Terminal->GetFileSystemInfo();
  6553. unsigned int Answer = MessageDialog(FMTLOAD(PERFORM_ON_COMMAND_SESSION2,
  6554. (FileSystemInfo.ProtocolName, FileSystemInfo.ProtocolName)), qtConfirmation,
  6555. qaOK | qaCancel, HELP_PERFORM_ON_COMMAND_SESSION, &Params);
  6556. if (Answer == qaNeverAskAgain)
  6557. {
  6558. GUIConfiguration->ConfirmCommandSession = false;
  6559. Result = true;
  6560. }
  6561. else if (Answer == qaOK)
  6562. {
  6563. Result = true;
  6564. }
  6565. }
  6566. if (Result)
  6567. {
  6568. Result = CommandSessionFallback();
  6569. }
  6570. }
  6571. return Result;
  6572. }
  6573. //---------------------------------------------------------------------------
  6574. void __fastcall TCustomScpExplorerForm::OpenConsole(UnicodeString Command)
  6575. {
  6576. if (EnsureCommandSessionFallback(fcAnyCommand))
  6577. {
  6578. DoConsoleDialog(Terminal, Command);
  6579. }
  6580. }
  6581. //---------------------------------------------------------------------------
  6582. void __fastcall TCustomScpExplorerForm::FileControlDDDragEnter(
  6583. TObject *Sender, _di_IDataObject /*DataObj*/, int /*grfKeyState*/,
  6584. const TPoint & /*Point*/, int & /*dwEffect*/, bool & Accept)
  6585. {
  6586. if (!IsSideLocalBrowser(osRemote) &&
  6587. IsFileControl(DropSourceControl, osRemote) &&
  6588. (FDDExtMapFile != NULL))
  6589. {
  6590. Accept = true;
  6591. }
  6592. FDDTargetControl = dynamic_cast<TControl*>(Sender);
  6593. DebugAssert(FDDTargetControl != NULL);
  6594. }
  6595. //---------------------------------------------------------------------------
  6596. void __fastcall TCustomScpExplorerForm::SessionsDDDragEnter(
  6597. _di_IDataObject DataObj, int KeyState,
  6598. const TPoint & Point, int & Effect, bool & Accept)
  6599. {
  6600. FileControlDDDragEnter(SessionsPageControl, DataObj, KeyState, Point, Effect, Accept);
  6601. }
  6602. //---------------------------------------------------------------------------
  6603. void __fastcall TCustomScpExplorerForm::QueueDDDragEnter(
  6604. _di_IDataObject DataObj, int KeyState,
  6605. const TPoint & Point, int & Effect, bool & Accept)
  6606. {
  6607. FileControlDDDragEnter(QueueView3, DataObj, KeyState, Point, Effect, Accept);
  6608. }
  6609. //---------------------------------------------------------------------------
  6610. void __fastcall TCustomScpExplorerForm::FileControlDDDragLeave(
  6611. TObject * Sender)
  6612. {
  6613. DebugUsedParam(Sender);
  6614. DebugAssert(FDDTargetControl == Sender);
  6615. FDDTargetControl = NULL;
  6616. }
  6617. //---------------------------------------------------------------------------
  6618. void __fastcall TCustomScpExplorerForm::SessionsDDDragLeave()
  6619. {
  6620. FileControlDDDragLeave(SessionsPageControl);
  6621. }
  6622. //---------------------------------------------------------------------------
  6623. void __fastcall TCustomScpExplorerForm::QueueDDDragLeave()
  6624. {
  6625. FileControlDDDragLeave(QueueView3);
  6626. }
  6627. //---------------------------------------------------------------------------
  6628. void __fastcall TCustomScpExplorerForm::AddEditLink(TOperationSide Side, bool Add)
  6629. {
  6630. DebugAssert(!IsSideLocalBrowser(Side));
  6631. DebugUsedParam(Side);
  6632. bool Edit = false;
  6633. TRemoteFile * File = NULL;
  6634. UnicodeString FileName;
  6635. UnicodeString PointTo;
  6636. bool SymbolicLink = true;
  6637. if (RemoteDirView->ItemFocused)
  6638. {
  6639. DebugAssert(RemoteDirView->ItemFocused->Data);
  6640. File = (TRemoteFile *)RemoteDirView->ItemFocused->Data;
  6641. Edit = !Add && File->IsSymLink && Terminal->SessionData->ResolveSymlinks;
  6642. if (Edit)
  6643. {
  6644. FileName = File->FileName;
  6645. PointTo = File->LinkTo;
  6646. }
  6647. else if (!File->IsParentDirectory)
  6648. {
  6649. PointTo = File->FileName;
  6650. }
  6651. }
  6652. if (DoSymlinkDialog(FileName, PointTo, osRemote, SymbolicLink, Edit,
  6653. Terminal->IsCapable[fcHardLink]))
  6654. {
  6655. Configuration->Usage->Inc(L"RemoteLinksCreated");
  6656. if (Edit)
  6657. {
  6658. DebugAssert(File->FileName == FileName);
  6659. int Params = dfNoRecursive;
  6660. Terminal->ExceptionOnFail = true;
  6661. try
  6662. {
  6663. Terminal->DeleteFile(L"", File, &Params);
  6664. }
  6665. __finally
  6666. {
  6667. Terminal->ExceptionOnFail = false;
  6668. }
  6669. }
  6670. Terminal->CreateLink(FileName, PointTo, SymbolicLink);
  6671. }
  6672. }
  6673. //---------------------------------------------------------------------------
  6674. bool __fastcall TCustomScpExplorerForm::CanAddEditLink(TOperationSide Side)
  6675. {
  6676. return
  6677. (IsSideLocalBrowser(Side) ||
  6678. (HasActiveTerminal() &&
  6679. Terminal->ResolvingSymlinks &&
  6680. Terminal->IsCapable[fcSymbolicLink]));
  6681. }
  6682. //---------------------------------------------------------------------------
  6683. bool __fastcall TCustomScpExplorerForm::LinkFocused()
  6684. {
  6685. return
  6686. !IsSideLocalBrowser(FCurrentSide) &&
  6687. (RemoteDirView->ItemFocused != NULL) &&
  6688. ((TRemoteFile *)RemoteDirView->ItemFocused->Data)->IsSymLink &&
  6689. Terminal->SessionData->ResolveSymlinks;
  6690. }
  6691. //---------------------------------------------------------------------------
  6692. void __fastcall TCustomScpExplorerForm::ExecuteCurrentFile()
  6693. {
  6694. DebugAssert(!WinConfiguration->DisableOpenEdit);
  6695. FForceExecution = true;
  6696. try
  6697. {
  6698. DirView(osCurrent)->ExecuteCurrentFile();
  6699. }
  6700. __finally
  6701. {
  6702. FForceExecution = false;
  6703. }
  6704. }
  6705. //---------------------------------------------------------------------------
  6706. void __fastcall TCustomScpExplorerForm::ExecuteCurrentFileWith(bool OnFocused)
  6707. {
  6708. TEditorData ExternalEditor;
  6709. ExternalEditor.Editor = edExternal;
  6710. bool Remember = false;
  6711. if (DoEditorPreferencesDialog(&ExternalEditor, Remember, epmAdHoc, !IsSideLocalBrowser(osCurrent)))
  6712. {
  6713. if (Remember)
  6714. {
  6715. TEditorList * EditorList = new TEditorList;
  6716. try
  6717. {
  6718. *EditorList = *WinConfiguration->EditorList;
  6719. bool Found = false;
  6720. int i = 0;
  6721. while (!Found && (i < EditorList->Count))
  6722. {
  6723. const TEditorPreferences * Editor = EditorList->Editors[i];
  6724. if ((Editor->Data->Editor == edExternal) &&
  6725. (Editor->Data->ExternalEditor == ExternalEditor.ExternalEditor))
  6726. {
  6727. Found = true;
  6728. }
  6729. i++;
  6730. }
  6731. if (!Found)
  6732. {
  6733. EditorList->Add(new TEditorPreferences(ExternalEditor));
  6734. WinConfiguration->EditorList = EditorList;
  6735. }
  6736. }
  6737. __finally
  6738. {
  6739. delete EditorList;
  6740. }
  6741. }
  6742. ExecuteFile(osCurrent, efExternalEditor, &ExternalEditor, true, OnFocused);
  6743. }
  6744. }
  6745. //---------------------------------------------------------------------------
  6746. void __fastcall TCustomScpExplorerForm::DetachTerminal(TObject * ATerminal)
  6747. {
  6748. if (FFileFindTerminal == ATerminal)
  6749. {
  6750. FFileFindTerminal = NULL;
  6751. HideFileFindDialog();
  6752. }
  6753. if (FClipboardTerminal == ATerminal)
  6754. {
  6755. ClipboardClear(); // implies ClipboardStop
  6756. }
  6757. if ((FOnSynchronizeAbort != NULL) &&
  6758. (ATerminal == FManagedSession))
  6759. {
  6760. FOnSynchronizeAbort(NULL);
  6761. }
  6762. }
  6763. //---------------------------------------------------------------------------
  6764. void __fastcall TCustomScpExplorerForm::TerminalRemoved(TObject * Sender)
  6765. {
  6766. FEditorManager->ProcessFiles(FileTerminalRemoved, Sender);
  6767. DetachTerminal(Sender);
  6768. }
  6769. //---------------------------------------------------------------------------
  6770. void __fastcall TCustomScpExplorerForm::FileTerminalReplaced(
  6771. const UnicodeString FileName, TEditedFileData * Data, TObject * /*Token*/, void * Arg)
  6772. {
  6773. TManagedTerminal * ATerminal = static_cast<TManagedTerminal *>(Arg);
  6774. if (Data->Terminal == Terminal)
  6775. {
  6776. Data->Terminal = ATerminal;
  6777. }
  6778. }
  6779. //---------------------------------------------------------------------------
  6780. void __fastcall TCustomScpExplorerForm::FileTerminalRemoved(const UnicodeString FileName,
  6781. TEditedFileData * Data, TObject * /*Token*/, void * Arg)
  6782. {
  6783. TTerminal * Terminal = static_cast<TTerminal *>(Arg);
  6784. DebugAssert(Terminal != NULL);
  6785. if (Data->Terminal == Terminal)
  6786. {
  6787. Data->Terminal = NULL;
  6788. }
  6789. }
  6790. //---------------------------------------------------------------------------
  6791. TManagedTerminal * TCustomScpExplorerForm::GetReplacementForLastSession()
  6792. {
  6793. return NULL;
  6794. }
  6795. //---------------------------------------------------------------------------
  6796. void __fastcall TCustomScpExplorerForm::LastTerminalClosed()
  6797. {
  6798. UpdateControls();
  6799. SessionColor = TColor(0);
  6800. UpdateRemotePathComboBox(false);
  6801. NeedSession(false);
  6802. }
  6803. //---------------------------------------------------------------------------
  6804. void __fastcall TCustomScpExplorerForm::NeedSession(bool Startup)
  6805. {
  6806. try
  6807. {
  6808. // Cache, as the login dialog can change its value.
  6809. // See also DoShow. Also see TTerminalManager::NewSession.
  6810. bool ShowLogin = WinConfiguration->ShowLoginWhenNoSession;
  6811. try
  6812. {
  6813. if (ShowLogin)
  6814. {
  6815. bool ReloadSessions = !Startup;
  6816. TTerminalManager::Instance()->NewSession(L"", ReloadSessions, this, true);
  6817. }
  6818. }
  6819. __finally
  6820. {
  6821. // Do not terminate, if we are only starting up and we are not showing Login dialog
  6822. // (so there was no chance for the user to open any session yet)
  6823. if ((ShowLogin || !Startup) &&
  6824. !WinConfiguration->KeepOpenWhenNoSession &&
  6825. // this rules out the implicit Commander's local-local browser
  6826. ((Terminal == NULL) || (!Terminal->Active && !Terminal->Permanent)))
  6827. {
  6828. TerminateApplication();
  6829. }
  6830. }
  6831. }
  6832. catch (EAbort &)
  6833. {
  6834. // swallow
  6835. // The TTerminalManager does not expect the OnLastTerminalClose to throw without trying to connect.
  6836. // Also when called from TWinControl.UpdateShowing => CMShowingChanged, the showing is aborted on exception
  6837. }
  6838. }
  6839. //---------------------------------------------------------------------------
  6840. void __fastcall TCustomScpExplorerForm::SessionListChanged(bool ForceTruncationUpdate)
  6841. {
  6842. TTerminalManager * Manager = TTerminalManager::Instance();
  6843. DebugAssert(!Manager->Updating);
  6844. TAutoFlag UpdatingSessionTabsFlag(FUpdatingSessionTabs);
  6845. int ActiveSessionIndex = Manager->ActiveSessionIndex;
  6846. Configuration->Usage->SetMax(L"MaxOpenedSessions", Manager->Count);
  6847. SendMessage(SessionsPageControl->Handle, WM_SETREDRAW, 0, 0);
  6848. try
  6849. {
  6850. int PrevTabsWidth = SessionsPageControl->TotalTabsWidth();
  6851. while (SessionsPageControl->PageCount > Manager->Count + 1)
  6852. {
  6853. delete SessionsPageControl->Pages[SessionsPageControl->PageCount - 1];
  6854. }
  6855. for (int Index = 0; Index <= Manager->Count; Index++)
  6856. {
  6857. TThemeTabSheet * TabSheet;
  6858. if (Index >= SessionsPageControl->PageCount)
  6859. {
  6860. TabSheet = new TThemeTabSheet(SessionsPageControl);
  6861. TabSheet->PageControl = SessionsPageControl;
  6862. }
  6863. else
  6864. {
  6865. TabSheet = SessionsPageControl->Pages[Index];
  6866. }
  6867. bool IsSessionTab = (Index < Manager->Count);
  6868. if (IsSessionTab)
  6869. {
  6870. TTerminal * Terminal = Manager->Sessions[Index];
  6871. TabSheet->Tag = reinterpret_cast<int>(Terminal);
  6872. TabSheet->CaptionTruncation = WinConfiguration->SessionTabCaptionTruncation ? tttEllipsis : tttNone;
  6873. UpdateSessionTab(TabSheet);
  6874. }
  6875. else
  6876. {
  6877. TabSheet->Tag = 0; // not really needed
  6878. TabSheet->Shadowed = false;
  6879. TabSheet->Button = SupportsLocalBrowser() ? ttbDropDown : ttbNone;
  6880. TabSheet->CaptionTruncation = GetNewTabTabCaptionTruncation();
  6881. // 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)
  6882. UpdateNewTabTab();
  6883. }
  6884. }
  6885. // Prevent flicker on the right side where scrolling buttons would be, unless really needed
  6886. if (ForceTruncationUpdate || (SessionsPageControl->TotalTabsWidth() != PrevTabsWidth))
  6887. {
  6888. SessionsPageControl->UpdateTabsCaptionTruncation();
  6889. }
  6890. }
  6891. __finally
  6892. {
  6893. SendMessage(SessionsPageControl->Handle, WM_SETREDRAW, 1, 0);
  6894. }
  6895. SessionsPageControl->ActivePageIndex = ActiveSessionIndex;
  6896. }
  6897. //---------------------------------------------------------------------------
  6898. TThemeTabCaptionTruncation TCustomScpExplorerForm::GetNewTabTabCaptionTruncation()
  6899. {
  6900. return WinConfiguration->SessionTabCaptionTruncation ? tttNoText : tttNone;
  6901. }
  6902. //---------------------------------------------------------------------------
  6903. UnicodeString TCustomScpExplorerForm::GetNewTabTabCaption()
  6904. {
  6905. return StripHotkey(StripTrailingPunctuation(NonVisualDataModule->NewTabAction->Caption));
  6906. }
  6907. //---------------------------------------------------------------------------
  6908. TThemeTabSheet * TCustomScpExplorerForm::GetNewTabTab()
  6909. {
  6910. return SessionsPageControl->Pages[SessionsPageControl->PageCount - 1];
  6911. }
  6912. //---------------------------------------------------------------------------
  6913. void __fastcall TCustomScpExplorerForm::UpdateNewTabTab()
  6914. {
  6915. TThemeTabSheet * TabSheet = GetNewTabTab();
  6916. UnicodeString TabCaption;
  6917. if (WinConfiguration->SelectiveToolbarText)
  6918. {
  6919. TabCaption = GetNewTabTabCaption();
  6920. }
  6921. TabSheet->BaseCaption = TabCaption;
  6922. TabSheet->ImageIndex = GetNewTabTabImageIndex(osCurrent);
  6923. }
  6924. //---------------------------------------------------------------------------
  6925. TManagedTerminal * __fastcall TCustomScpExplorerForm::GetSessionTabSession(TTabSheet * TabSheet)
  6926. {
  6927. return reinterpret_cast<TManagedTerminal *>(TabSheet->Tag);
  6928. }
  6929. //---------------------------------------------------------------------------
  6930. UnicodeString TCustomScpExplorerForm::GetLocalBrowserSessionTitle(TManagedTerminal *)
  6931. {
  6932. DebugFail();
  6933. return UnicodeString();
  6934. }
  6935. //---------------------------------------------------------------------------
  6936. void TCustomScpExplorerForm::UpdateSessionTab(TThemeTabSheet * TabSheet)
  6937. {
  6938. if (DebugAlwaysTrue(TabSheet != NULL))
  6939. {
  6940. TManagedTerminal * ASession = GetSessionTabSession(TabSheet);
  6941. if (DebugAlwaysTrue(ASession != NULL))
  6942. {
  6943. TColor Color = (ASession == Terminal) ? FSessionColor : ASession->StateData->Color;
  6944. if (ASession->LocalBrowser)
  6945. {
  6946. TabSheet->ImageIndex = FLocalBrowserTabImageIndex;
  6947. }
  6948. else
  6949. {
  6950. TabSheet->ImageIndex = AddSessionColor(Color);
  6951. }
  6952. TTerminalManager * Manager = TTerminalManager::Instance();
  6953. TabSheet->Shadowed = !ASession->Active && !ASession->LocalBrowser;
  6954. TabSheet->Button = CanCloseSession(ASession) ? ttbClose : ttbNone;
  6955. TabSheet->BaseCaption = Manager->GetSessionTitle(ASession, true);
  6956. }
  6957. }
  6958. }
  6959. //---------------------------------------------------------------------------
  6960. bool TCustomScpExplorerForm::CanCloseSession(TManagedTerminal * Session)
  6961. {
  6962. return !Session->LocalBrowser || (TTerminalManager::Instance()->Count > 1);
  6963. }
  6964. //---------------------------------------------------------------------------
  6965. bool __fastcall TCustomScpExplorerForm::SessionTabSwitched()
  6966. {
  6967. DebugAssert(SessionsPageControl->ActivePage != NULL);
  6968. TManagedTerminal * Session = GetSessionTabSession(SessionsPageControl->ActivePage);
  6969. bool Result = (Session != NULL);
  6970. if (Result)
  6971. {
  6972. TTerminalManager::Instance()->ActiveSession = Session;
  6973. }
  6974. else
  6975. {
  6976. try
  6977. {
  6978. NewTab();
  6979. }
  6980. __finally
  6981. {
  6982. SessionListChanged();
  6983. }
  6984. FSessionsPageControlNewTabTime = Now();
  6985. }
  6986. return Result;
  6987. }
  6988. //---------------------------------------------------------------------------
  6989. void __fastcall TCustomScpExplorerForm::SessionsPageControlChange(TObject * /*Sender*/)
  6990. {
  6991. TWindowLock Lock(this);
  6992. SessionTabSwitched();
  6993. }
  6994. //---------------------------------------------------------------------------
  6995. void __fastcall TCustomScpExplorerForm::TransferListChange(TObject * Sender)
  6996. {
  6997. TTBXStringList * TransferList = dynamic_cast<TTBXStringList *>(Sender);
  6998. DebugAssert(TransferList != NULL);
  6999. UnicodeString Name;
  7000. if (TransferList->ItemIndex <= 0)
  7001. {
  7002. Name = L"";
  7003. }
  7004. else
  7005. {
  7006. Name = GUIConfiguration->CopyParamList->Names[TransferList->ItemIndex - 1];
  7007. }
  7008. if (FCopyParamAutoSelected.IsEmpty())
  7009. {
  7010. // if previous preset was not autoselected, make new preset the "default"
  7011. FCopyParamDefault = Name;
  7012. }
  7013. GUIConfiguration->CopyParamCurrent = Name;
  7014. }
  7015. //---------------------------------------------------------------------------
  7016. void __fastcall TCustomScpExplorerForm::UpdateTransferLabel()
  7017. {
  7018. // sanity check
  7019. bool ExistingPreset =
  7020. (FTransferListHoverIndex >= 0) &&
  7021. (FTransferListHoverIndex < 1 + GUIConfiguration->CopyParamList->Count);
  7022. DebugAssert(ExistingPreset);
  7023. if (ExistingPreset)
  7024. {
  7025. HDC DC = GetDC(0);
  7026. TCanvas * Canvas = new TCanvas();
  7027. try
  7028. {
  7029. Canvas->Handle = DC;
  7030. Canvas->Font = GetToolbarFont(this);
  7031. UnicodeString Name;
  7032. if (FTransferListHoverIndex == 0)
  7033. {
  7034. Name = L"";
  7035. }
  7036. else
  7037. {
  7038. Name = GUIConfiguration->CopyParamList->Names[FTransferListHoverIndex - 1];
  7039. }
  7040. TTBXLabelItem * TransferLabel = dynamic_cast<TTBXLabelItem*>(
  7041. static_cast<TComponent*>(GetComponent(fcTransferLabel)));
  7042. TTBXStringList * TransferList = dynamic_cast<TTBXStringList*>(
  7043. static_cast<TObject*>(GetComponent(fcTransferList)));
  7044. UnicodeString InfoStr =
  7045. GUIConfiguration->CopyParamPreset[Name].
  7046. GetInfoStr(L"; ",
  7047. FLAGMASK(HasActiveTerminal(), Terminal->UsableCopyParamAttrs(0).General));
  7048. int MaxWidth = TransferList->MinWidth - (2 * TransferLabel->Margin) - ScaleByTextHeight(this, 10);
  7049. if (Canvas->TextExtent(InfoStr).cx > MaxWidth)
  7050. {
  7051. while (Canvas->TextExtent(InfoStr + Ellipsis).cx > MaxWidth)
  7052. {
  7053. InfoStr.SetLength(InfoStr.Length() - 1);
  7054. }
  7055. InfoStr += Ellipsis;
  7056. }
  7057. // UpdateCaption does not cause invalidation of whole submenu, while
  7058. // setting Caption property does.
  7059. // also it probably does not resize the label, even if necessary
  7060. // (we do not want that anyway)
  7061. TransferLabel->UpdateCaption(InfoStr);
  7062. }
  7063. __finally
  7064. {
  7065. Canvas->Handle = NULL;
  7066. ReleaseDC(0, DC);
  7067. delete Canvas;
  7068. }
  7069. }
  7070. }
  7071. //---------------------------------------------------------------------------
  7072. void __fastcall TCustomScpExplorerForm::TransferListDrawItem(
  7073. TTBXCustomList * /*Sender*/, TCanvas * /*ACanvas*/, const TRect & /*ARect*/,
  7074. int /*AIndex*/, int AHoverIndex, bool & /*DrawDefault*/)
  7075. {
  7076. if (FTransferListHoverIndex != AHoverIndex)
  7077. {
  7078. FTransferListHoverIndex = AHoverIndex;
  7079. UpdateTransferLabel();
  7080. }
  7081. }
  7082. //---------------------------------------------------------------------------
  7083. void __fastcall TCustomScpExplorerForm::WMAppCommand(TMessage & Message)
  7084. {
  7085. int Command = GET_APPCOMMAND_LPARAM(Message.LParam);
  7086. TShiftState Shift = KeyDataToShiftState(GET_KEYSTATE_LPARAM(Message.LParam));
  7087. if ((Shift * (TShiftState() << ssShift << ssAlt << ssCtrl)).Empty())
  7088. {
  7089. if (Command == APPCOMMAND_BROWSER_FAVORITES)
  7090. {
  7091. if (!NonVisualDataModule->Busy)
  7092. {
  7093. OpenDirectory(GetSide(osCurrent));
  7094. }
  7095. Message.Result = 1;
  7096. }
  7097. else
  7098. {
  7099. TForm::Dispatch(&Message);
  7100. }
  7101. }
  7102. else
  7103. {
  7104. TForm::Dispatch(&Message);
  7105. }
  7106. }
  7107. //---------------------------------------------------------------------------
  7108. void __fastcall TCustomScpExplorerForm::CMDialogChar(TMessage & AMessage)
  7109. {
  7110. TCMDialogChar & Message = reinterpret_cast<TCMDialogChar &>(AMessage);
  7111. if ((FIgnoreNextDialogChar != 0) &&
  7112. (towupper(Message.CharCode) == towupper(FIgnoreNextDialogChar)))
  7113. {
  7114. Message.Result = 1;
  7115. }
  7116. else
  7117. {
  7118. TForm::Dispatch(&Message);
  7119. }
  7120. FIgnoreNextDialogChar = 0;
  7121. }
  7122. //---------------------------------------------------------------------------
  7123. void __fastcall TCustomScpExplorerForm::WMSysCommand(TMessage & Message)
  7124. {
  7125. // The four low-order bits are used internally by Windows
  7126. unsigned int Cmd = (Message.WParam & 0xFFF0);
  7127. // SC_RESTORE, SC_MAXIMIZE, SC_MINIMIZE - buttons on windows title
  7128. // SC_DEFAULT - double click on windows title (does not work, at least on WinXP)
  7129. // 61730 - restore thru double click - undocumented
  7130. // 61490 - maximize thru double click - undocumented
  7131. if ((Cmd == SC_RESTORE) || (Cmd == SC_MAXIMIZE) ||
  7132. (Cmd == SC_MINIMIZE) || (Cmd == SC_DEFAULT))
  7133. {
  7134. SysResizing(Cmd);
  7135. }
  7136. TForm::Dispatch(&Message);
  7137. }
  7138. //---------------------------------------------------------------------------
  7139. void __fastcall TCustomScpExplorerForm::WMQueryEndSession(TMessage & Message)
  7140. {
  7141. AppLog(L"Windows session end query");
  7142. // We were actually never able to make ENDSESSION_CRITICAL happen.
  7143. // Also there no point returning TRUE as we are not able to
  7144. // handle the abrupt termination caused by subsequent WM_ENDSESSION cleanly.
  7145. // Hence the process termination might be safer :)
  7146. if ((Message.LParam != ENDSESSION_CRITICAL) &&
  7147. (((FQueue != NULL) && !FQueue->IsEmpty) || (FProgressForm != NULL)))
  7148. {
  7149. Message.Result = FALSE;
  7150. }
  7151. else
  7152. {
  7153. Message.Result = TRUE;
  7154. }
  7155. // Do not call default handling as that triggers OnCloseQuery,
  7156. // where our implementation will popup confirmation dialogs, what we do not want,
  7157. // as per Vista guidelines:
  7158. // https://learn.microsoft.com/en-us/windows/win32/shutdown/shutting-down
  7159. }
  7160. //---------------------------------------------------------------------------
  7161. void __fastcall TCustomScpExplorerForm::WMEndSession(TWMEndSession & Message)
  7162. {
  7163. AppLog(L"Windows session end");
  7164. if (Message.EndSession && IsApplicationMinimized())
  7165. {
  7166. // WORKAROUND
  7167. // TApplication.WndProc() calls Application.Terminate() before Halt(),
  7168. // when it receives WM_ENDSESSION,
  7169. // but that sometimes (particularly when application is minimized) cause crashes.
  7170. // Crash popups message that blocks log off.
  7171. // Obviously application cannot shutdown cleanly after WM_ENDSESSION,
  7172. // so we call ExitProcess() immediately, not even trying to cleanup.
  7173. // It still causes beep, so there's likely some popup, but it does not block
  7174. // log off.
  7175. ExitProcess(0);
  7176. }
  7177. TForm::Dispatch(&Message);
  7178. }
  7179. #ifdef _DEBUG
  7180. //---------------------------------------------------------------------------
  7181. void __fastcall TCustomScpExplorerForm::WMWindowPosChanged(TWMWindowPosMsg & Message)
  7182. {
  7183. TForm::Dispatch(&Message);
  7184. }
  7185. #endif
  7186. //---------------------------------------------------------------------------
  7187. void __fastcall TCustomScpExplorerForm::SysResizing(unsigned int /*Cmd*/)
  7188. {
  7189. }
  7190. //---------------------------------------------------------------------------
  7191. void __fastcall TCustomScpExplorerForm::DoShow()
  7192. {
  7193. // Called from TCustomScpExplorerForm::CMShowingChanged
  7194. // only now are the controls resized finally, so the size constraints
  7195. // will not conflict with possibly very small window size
  7196. RestoreFormParams();
  7197. // This indicates that the form was rescaled after parameters were loaded and
  7198. // as most probably the parameters were actually saved with the current form scaling
  7199. // (windows was the last time likely closed on the same monitor, where it is starting now),
  7200. // rescaling happened twice already. Reload the parameters, as now they likely won't need any scaling
  7201. // and no rounding problems will occur.
  7202. // See also RestoreForm.
  7203. // (we should be safe to call RestoreParams unconditionally)
  7204. if (PixelsPerInch != Screen->PixelsPerInch)
  7205. {
  7206. RestoreParams();
  7207. }
  7208. FixControlsPlacement();
  7209. if (Position == poDefaultPosOnly)
  7210. {
  7211. CutFormToDesktop(this);
  7212. }
  7213. TForm::DoShow();
  7214. FSessionsDragDropFilesEx->DragDropControl = SessionsPageControl;
  7215. FQueueDragDropFilesEx->DragDropControl = QueueView3;
  7216. // This was previously in NeedSession, where other related code is.
  7217. // But that is called from CMShowingChanged, when the window is already visible and the local-local window state
  7218. // unpleasantly shows briefly.
  7219. if ((ManagedSession == NULL) &&
  7220. !WinConfiguration->ShowLoginWhenNoSession &&
  7221. WinConfiguration->AutoSaveWorkspace && !WinConfiguration->AutoWorkspace.IsEmpty() &&
  7222. // This detects if workspace was saved the last time the main widow was closed
  7223. SameText(WinConfiguration->LastStoredSession, WinConfiguration->AutoWorkspace))
  7224. {
  7225. DoOpenFolderOrWorkspace(WinConfiguration->AutoWorkspace, false, true);
  7226. Configuration->Usage->Inc(L"OpenedWorkspacesAuto");
  7227. }
  7228. // Before main window displays
  7229. if (ManagedSession == NULL)
  7230. {
  7231. StartingWithoutSession();
  7232. }
  7233. UpdatePixelsPerInchMainWindowCounter();
  7234. FShowing = true;
  7235. }
  7236. //---------------------------------------------------------------------------
  7237. void __fastcall TCustomScpExplorerForm::UpdatePixelsPerInchMainWindowCounter()
  7238. {
  7239. Configuration->Usage->Set(L"PixelsPerInchMainWindow", PixelsPerInch);
  7240. }
  7241. //---------------------------------------------------------------------------
  7242. void __fastcall TCustomScpExplorerForm::StartingWithoutSession()
  7243. {
  7244. SessionListChanged();
  7245. InitStatusBar();
  7246. UpdateControls();
  7247. }
  7248. //---------------------------------------------------------------------------
  7249. void __fastcall TCustomScpExplorerForm::PopupTrayBalloon(TTerminal * Terminal,
  7250. const UnicodeString & Str, TQueryType Type, Exception * E, unsigned int Seconds,
  7251. TNotifyEvent OnBalloonClick, TObject * UserData)
  7252. {
  7253. bool Do;
  7254. UnicodeString Message;
  7255. if (E == NULL)
  7256. {
  7257. Message = Str;
  7258. Do = true;
  7259. }
  7260. else
  7261. {
  7262. Do = ExceptionMessage(E, Message);
  7263. }
  7264. if (Do && WinConfiguration->BalloonNotifications)
  7265. {
  7266. UnicodeString Title;
  7267. if ((Terminal == NULL) && (Type == qtInformation) && ExtractMainInstructions(Message, Title))
  7268. {
  7269. Message = TrimLeft(Message);
  7270. }
  7271. else
  7272. {
  7273. Message = UnformatMessage(Message);
  7274. const ResourceString * Captions[] = { &_SMsgDlgConfirm, &_SMsgDlgWarning,
  7275. &_SMsgDlgError, &_SMsgDlgInformation, NULL };
  7276. Title = LoadResourceString(Captions[Type]);
  7277. if (Terminal != NULL)
  7278. {
  7279. TManagedTerminal * Session = DebugNotNull(dynamic_cast<TManagedTerminal *>(Terminal->PrimaryTerminal));
  7280. Title = TTerminalManager::Instance()->GetSessionTitle(Session, true) + TitleSeparator + Title;
  7281. }
  7282. }
  7283. if (Seconds == 0)
  7284. {
  7285. Seconds = WinConfiguration->NotificationsTimeout;
  7286. }
  7287. FTrayIcon->PopupBalloon(Title, Message, Type, Seconds * MSecsPerSec, OnBalloonClick, UserData);
  7288. }
  7289. }
  7290. //---------------------------------------------------------------------------
  7291. unsigned int __fastcall TCustomScpExplorerForm::MoreMessageDialog(const UnicodeString Message,
  7292. TStrings * MoreMessages, TQueryType Type, unsigned int Answers,
  7293. UnicodeString HelpKeyword, const TMessageParams * Params,
  7294. TTerminal * Terminal)
  7295. {
  7296. if (((WinConfiguration->ContinueOnError && (FErrorList != NULL)) ||
  7297. (FOnFeedSynchronizeError != NULL)) &&
  7298. (Params != NULL) && (Params->Params & mpAllowContinueOnError) )
  7299. {
  7300. if (FOnFeedSynchronizeError != NULL)
  7301. {
  7302. FOnFeedSynchronizeError(Message, MoreMessages, Type, HelpKeyword);
  7303. }
  7304. else
  7305. {
  7306. DebugAssert(FErrorList != NULL);
  7307. TStringList * MoreMessagesCopy = NULL;
  7308. if (MoreMessages)
  7309. {
  7310. MoreMessagesCopy = new TStringList();
  7311. MoreMessagesCopy->Assign(MoreMessages);
  7312. }
  7313. FErrorList->AddObject(Message, MoreMessagesCopy);
  7314. }
  7315. PopupTrayBalloon(Terminal, Message, Type);
  7316. return ContinueAnswer(Answers);
  7317. }
  7318. else
  7319. {
  7320. bool UseBalloon = FTrayIcon->Visible;
  7321. if (UseBalloon)
  7322. {
  7323. PopupTrayBalloon(Terminal, Message, Type);
  7324. }
  7325. unsigned int Result;
  7326. try
  7327. {
  7328. Result = ::MoreMessageDialog(Message, MoreMessages, Type, Answers, HelpKeyword, Params);
  7329. }
  7330. __finally
  7331. {
  7332. // cancel only balloon we popped up, otherwise we may cancel notification
  7333. // balloon that was there before the message dialog
  7334. // (such as "dd confirmation opt in balloon)
  7335. if (UseBalloon)
  7336. {
  7337. FTrayIcon->CancelBalloon();
  7338. }
  7339. }
  7340. return Result;
  7341. }
  7342. }
  7343. //---------------------------------------------------------------------------
  7344. void __fastcall TCustomScpExplorerForm::ShowExtendedException(
  7345. TTerminal * Terminal, Exception * E)
  7346. {
  7347. if (FTrayIcon->Visible)
  7348. {
  7349. PopupTrayBalloon(Terminal, L"", qtError, E);
  7350. }
  7351. // particularly prevent opening new session from jump list,
  7352. // while exception is shown
  7353. NonVisualDataModule->StartBusy();
  7354. try
  7355. {
  7356. ShowExtendedExceptionEx(Terminal, E);
  7357. }
  7358. __finally
  7359. {
  7360. NonVisualDataModule->EndBusy();
  7361. FTrayIcon->CancelBalloon();
  7362. }
  7363. }
  7364. //---------------------------------------------------------------------------
  7365. void __fastcall TCustomScpExplorerForm::SessionReady()
  7366. {
  7367. InitStatusBar();
  7368. // cannot rely on active page being page for active terminal,
  7369. // as it can happen that active page is the "new session" page
  7370. // (e.g. when reconnecting active terminal, while login dialog
  7371. // invoked from "new session" page is modal)
  7372. int ActiveSessionIndex = TTerminalManager::Instance()->ActiveSessionIndex;
  7373. UpdateSessionTab(SessionsPageControl->Pages[ActiveSessionIndex]);
  7374. }
  7375. //---------------------------------------------------------------------------
  7376. void __fastcall TCustomScpExplorerForm::InactiveTerminalException(
  7377. TTerminal * Terminal, Exception * E)
  7378. {
  7379. Notify(Terminal, L"", qtError, false, NULL, NULL, E);
  7380. if (!Terminal->Active)
  7381. {
  7382. int Index = TTerminalManager::Instance()->IndexOf(Terminal);
  7383. if (DebugAlwaysTrue((Index >= 0) && (Index < SessionsPageControl->PageCount)))
  7384. {
  7385. UpdateSessionTab(SessionsPageControl->Pages[Index]);
  7386. }
  7387. }
  7388. }
  7389. //---------------------------------------------------------------------------
  7390. void __fastcall TCustomScpExplorerForm::Notify(TTerminal * Terminal,
  7391. UnicodeString Message, TQueryType Type,
  7392. bool Important, TNotifyEvent OnClick, TObject * UserData, Exception * E)
  7393. {
  7394. if ((E == NULL) ||
  7395. ExceptionMessage(E, Message))
  7396. {
  7397. unsigned int Seconds = WinConfiguration->NotificationsTimeout;
  7398. if (Important)
  7399. {
  7400. Seconds *= 5;
  7401. }
  7402. UnicodeString NoteMessage(UnformatMessage(Message));
  7403. if (Terminal != NULL)
  7404. {
  7405. TManagedTerminal * Session = DebugNotNull(dynamic_cast<TManagedTerminal *>(Terminal));
  7406. NoteMessage = FORMAT(L"%s: %s",
  7407. (TTerminalManager::Instance()->GetSessionTitle(Session, true), NoteMessage));
  7408. }
  7409. if (WinConfiguration->BalloonNotifications)
  7410. {
  7411. AddNote(NoteMessage);
  7412. PopupTrayBalloon(Terminal, Message, Type, NULL, Seconds, OnClick, UserData);
  7413. }
  7414. else
  7415. {
  7416. FlashOnBackground();
  7417. PostNote(NoteMessage, Seconds, OnClick, UserData);
  7418. }
  7419. }
  7420. }
  7421. //---------------------------------------------------------------------------
  7422. void __fastcall TCustomScpExplorerForm::QueueEmptyNoteClicked(TObject * Sender)
  7423. {
  7424. RestoreApp();
  7425. TTerminalNoteData * TerminalNoteData = dynamic_cast<TTerminalNoteData *>(Sender);
  7426. if (DebugAlwaysTrue(TerminalNoteData != NULL) &&
  7427. !NonVisualDataModule->Busy)
  7428. {
  7429. TManagedTerminal * Terminal = TerminalNoteData->Terminal;
  7430. TTerminalManager::Instance()->ActiveSession = Terminal;
  7431. if (!ComponentVisible[fcQueueView])
  7432. {
  7433. ToggleQueueVisibility();
  7434. GoToQueue();
  7435. }
  7436. }
  7437. }
  7438. //---------------------------------------------------------------------------
  7439. void __fastcall TCustomScpExplorerForm::QueueEvent(TManagedTerminal * ATerminal,
  7440. TTerminalQueue * /*Queue*/, TQueueEvent Event)
  7441. {
  7442. UnicodeString Message;
  7443. TNotifyEvent OnClick = NULL;
  7444. TObject * UserData = NULL;
  7445. bool QueueInvisible = !ComponentVisible[fcQueueView] || IsApplicationMinimized();
  7446. switch (Event)
  7447. {
  7448. case qeEmptyButMonitored:
  7449. if ((ATerminal != Terminal) || QueueInvisible)
  7450. {
  7451. Message = LoadStr(BALLOON_QUEUE_EMPTY);
  7452. OnClick = QueueEmptyNoteClicked;
  7453. TTerminalNoteData * TerminalNoteData = new TTerminalNoteData();
  7454. TerminalNoteData->Terminal = ATerminal;
  7455. UserData = TerminalNoteData;
  7456. }
  7457. break;
  7458. case qeEmpty:
  7459. OperationComplete(ATerminal->QueueOperationStart);
  7460. break;
  7461. case qePendingUserAction:
  7462. if ((ATerminal != Terminal) ||
  7463. (QueueInvisible && !IsQueueAutoPopup()))
  7464. {
  7465. Message = LoadStr(BALLOON_QUEUE_USER_ACTION);
  7466. }
  7467. break;
  7468. default:
  7469. DebugFail();
  7470. }
  7471. if (!Message.IsEmpty())
  7472. {
  7473. Notify(ATerminal, Message, qtInformation, false, OnClick, UserData);
  7474. }
  7475. }
  7476. //---------------------------------------------------------------------------
  7477. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDCreateDragFileList(
  7478. TObject * /*Sender*/, TFileList * FileList, bool & Created)
  7479. {
  7480. if (FDDExtMapFile != NULL)
  7481. {
  7482. CloseHandle(FDDExtMapFile);
  7483. FDDExtMapFile = NULL;
  7484. }
  7485. if (WinConfiguration->DDFakeFile)
  7486. {
  7487. DDFakeFileInitDrag(FileList, Created);
  7488. }
  7489. }
  7490. //---------------------------------------------------------------------------
  7491. void __fastcall TCustomScpExplorerForm::DDFakeCreated(TObject * /*Sender*/, const UnicodeString FileName)
  7492. {
  7493. if (DebugAlwaysTrue(!FDragFakeDirectory.IsEmpty()) &&
  7494. SameText(ExtractFileName(FileName), ExtractFileName(FDragFakeDirectory)))
  7495. {
  7496. FFakeFileDropTarget = FileName;
  7497. }
  7498. }
  7499. //---------------------------------------------------------------------------
  7500. UnicodeString __fastcall TCustomScpExplorerForm::CreateFakeTransferDirectory()
  7501. {
  7502. UnicodeString Result = ExcludeTrailingBackslash(WinConfiguration->TemporaryDir());
  7503. if (!ForceDirectories(ApiPath(Result)))
  7504. {
  7505. throw Exception(FMTLOAD(CREATE_TEMP_DIR_ERROR, (Result)));
  7506. }
  7507. FileSetAttr(ApiPath(Result), faHidden);
  7508. return Result;
  7509. }
  7510. //---------------------------------------------------------------------------
  7511. void __fastcall TCustomScpExplorerForm::DDFakeFileInitDrag(TFileList * FileList,
  7512. bool & Created)
  7513. {
  7514. FFakeFileDropTarget = UnicodeString();
  7515. FDragFakeDirectory = CreateFakeTransferDirectory();
  7516. FileList->AddItem(NULL, FDragFakeDirectory);
  7517. Created = true;
  7518. if (!WinConfiguration->IsDDExtRunning() || WinConfiguration->IsDDExtBroken())
  7519. {
  7520. FDragFakeMonitors = StartCreationDirectoryMonitorsOnEachDrive(FILE_NOTIFY_CHANGE_DIR_NAME, DDFakeCreated);
  7521. }
  7522. FDDExtMapFile = CreateFileMappingA((HANDLE)0xFFFFFFFF, NULL, PAGE_READWRITE,
  7523. 0, sizeof(TDragExtCommStruct), AnsiString(DRAG_EXT_MAPPING).c_str());
  7524. {
  7525. TMutexGuard Guard(FDDExtMutex, DRAGEXT_MUTEX_RELEASE_TIMEOUT);
  7526. TDragExtCommStruct* CommStruct;
  7527. CommStruct = static_cast<TDragExtCommStruct*>(MapViewOfFile(FDDExtMapFile,
  7528. FILE_MAP_ALL_ACCESS, 0, 0, 0));
  7529. DebugAssert(CommStruct != NULL);
  7530. CommStruct->Version = TDragExtCommStruct::CurrentVersion;
  7531. CommStruct->Dragging = true;
  7532. wcsncpy(CommStruct->DropDest, FDragFakeDirectory.c_str(),
  7533. LENOF(CommStruct->DropDest));
  7534. NULL_TERMINATE(CommStruct->DropDest);
  7535. UnmapViewOfFile(CommStruct);
  7536. }
  7537. }
  7538. //---------------------------------------------------------------------------
  7539. bool __fastcall TCustomScpExplorerForm::RemoteFileControlFileOperation(
  7540. TObject * Sender, TFileOperation Operation, bool NoConfirmation, void * Param)
  7541. {
  7542. bool Result;
  7543. DebugAssert(!IsLocalBrowserMode());
  7544. if (Sender == RemoteDirView)
  7545. {
  7546. Result = ExecuteFileOperation(Operation, osRemote, true, NoConfirmation, Param);
  7547. }
  7548. else
  7549. {
  7550. DebugAssert(Sender == RemoteDriveView);
  7551. TStrings * FileList = RemoteDriveView->DragFileList();
  7552. try
  7553. {
  7554. Result = ExecuteFileOperation(Operation, osRemote, FileList, NoConfirmation, Param);
  7555. }
  7556. __finally
  7557. {
  7558. delete FileList;
  7559. }
  7560. }
  7561. if (FDDTargetControl == RemoteDriveView)
  7562. {
  7563. RemoteDriveView->UpdateDropTarget();
  7564. }
  7565. // foRemoteMove happens when file/dir is dragged within the remote tree view
  7566. // or from tree view to dir view.
  7567. // foMove happens when file/dir is dragged from remote tree view outside of
  7568. // application via dragex
  7569. if (((Operation == foRemoteMove) || (Operation == foMove)) &&
  7570. (DropSourceControl == RemoteDriveView))
  7571. {
  7572. RemoteDriveView->UpdateDropSource();
  7573. }
  7574. return Result;
  7575. }
  7576. //---------------------------------------------------------------------------
  7577. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDEnd(TObject * Sender)
  7578. {
  7579. DebugAssert(!IsLocalBrowserMode());
  7580. // This also handles drops of remote files to queue.
  7581. // Drops of local files (uploads) are handled in QueueDDProcessDropped.
  7582. SAFE_DESTROY(FDDFileList);
  7583. if (!FFakeFileDropTarget.IsEmpty())
  7584. {
  7585. RemoveDir(ApiPath(FFakeFileDropTarget));
  7586. }
  7587. if ((FDDExtMapFile != NULL) || (FDDTargetControl == QueueView3))
  7588. {
  7589. try
  7590. {
  7591. TDragResult DDResult = (Sender == RemoteDirView) ?
  7592. RemoteDirView->LastDDResult : RemoteDriveView->LastDDResult;
  7593. // Focus is moved to the target application,
  7594. // but as we are going to present the UI, we need to steal the focus back.
  7595. // This most likely won't work though (windows does not allow application
  7596. // to steal focus most of the time)
  7597. Application->BringToFront();
  7598. // On older version of Windows we never got drMove here, see also comment below.
  7599. // On Windows 10, we get the "move".
  7600. if ((DDResult == drCopy) || (DDResult == drMove) || (DDResult == drInvalid))
  7601. {
  7602. UnicodeString TargetDirectory;
  7603. TFileOperation Operation;
  7604. // drInvalid may mean drMove, see comment below
  7605. switch (DDResult)
  7606. {
  7607. case drCopy:
  7608. Operation = foCopy;
  7609. break;
  7610. case drMove:
  7611. Operation = foMove;
  7612. break;
  7613. default:
  7614. DebugFail();
  7615. case drInvalid:
  7616. // prefer "copy" for safety
  7617. Operation = FLAGSET(FLastDropEffect, DROPEFFECT_MOVE) ? foMove : foCopy;
  7618. break;
  7619. }
  7620. TTransferOperationParam Param;
  7621. UnicodeString CounterName;
  7622. bool ForceQueue;
  7623. if (!DDGetTarget(Param.TargetDirectory, ForceQueue, CounterName))
  7624. {
  7625. // we get drInvalid both if d&d was intercepted by ddext,
  7626. // and when users drops on no-drop location.
  7627. // we tell the difference by existence of response from ddext,
  7628. // so we ignore absence of response in this case
  7629. if (DDResult != drInvalid)
  7630. {
  7631. // here we know that the extension is installed,
  7632. // as it is checked as soon as drag&drop starts from
  7633. // RemoteFileControlDDCreateDragFileList
  7634. Configuration->Usage->Inc(L"DownloadsDragDropExternalExtTargetUnknown");
  7635. if (!WinConfiguration->DDExtInstalled || WinConfiguration->IsDDExtBroken())
  7636. {
  7637. throw ExtException(NULL, LoadStr(DD_TARGET_UNKNOWN), HELP_DD_TARGET_UNKNOWN);
  7638. }
  7639. else
  7640. {
  7641. throw ExtException(NULL, LoadStr(DRAGEXT_TARGET_UNKNOWN2), HELP_DRAGEXT_TARGET_UNKNOWN);
  7642. }
  7643. }
  7644. }
  7645. else
  7646. {
  7647. // download using fake file
  7648. Param.Temp = false;
  7649. Param.DragDrop = true;
  7650. if (ForceQueue)
  7651. {
  7652. Param.Queue = asOn;
  7653. }
  7654. if (Sender == RemoteDirView)
  7655. {
  7656. Param.Options = FLAGMASK(SelectedAllFilesInDirView(RemoteDirView), coAllFiles);
  7657. }
  7658. if (RemoteFileControlFileOperation(Sender, Operation,
  7659. (WinConfiguration->DDTransferConfirmation == asOff), &Param))
  7660. {
  7661. Configuration->Usage->Inc(CounterName);
  7662. }
  7663. }
  7664. }
  7665. }
  7666. __finally
  7667. {
  7668. CloseHandle(FDDExtMapFile);
  7669. FDDExtMapFile = NULL;
  7670. delete FDragFakeMonitors;
  7671. FDragFakeMonitors = NULL;
  7672. RemoveDir(ApiPath(FDragFakeDirectory));
  7673. FDragFakeDirectory = L"";
  7674. FFakeFileDropTarget = UnicodeString();
  7675. }
  7676. }
  7677. if (FDragDropSshTerminate.get() != NULL)
  7678. {
  7679. FDragDropSshTerminate->Rethrow();
  7680. }
  7681. }
  7682. //---------------------------------------------------------------------------
  7683. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDGiveFeedback(
  7684. TObject * /*Sender*/, int dwEffect, HRESULT & /*Result*/)
  7685. {
  7686. // Remember drop effect so we know (when user drops files), if we copy or move
  7687. FLastDropEffect = dwEffect;
  7688. }
  7689. //---------------------------------------------------------------------------
  7690. bool __fastcall TCustomScpExplorerForm::DDGetTarget(
  7691. UnicodeString & Directory, bool & ForceQueue, UnicodeString & CounterName)
  7692. {
  7693. bool Result;
  7694. if (FDDTargetControl == QueueView3)
  7695. {
  7696. Directory = DefaultDownloadTargetDirectory();
  7697. Result = true;
  7698. CounterName = L"DownloadsDragDropQueue";
  7699. ForceQueue = true;
  7700. }
  7701. else if (!FFakeFileDropTarget.IsEmpty())
  7702. {
  7703. Directory = ExcludeTrailingBackslash(ExtractFilePath(FFakeFileDropTarget));
  7704. Result = true;
  7705. ForceQueue = false;
  7706. CounterName = L"DownloadsDragDropFakeFile";
  7707. }
  7708. else
  7709. {
  7710. ForceQueue = false;
  7711. Enabled = false;
  7712. try
  7713. {
  7714. int Timer = 0;
  7715. Result = false;
  7716. while (!Result && (Timer < WinConfiguration->DDExtTimeout))
  7717. {
  7718. {
  7719. TMutexGuard Guard(FDDExtMutex, DRAGEXT_MUTEX_RELEASE_TIMEOUT);
  7720. TDragExtCommStruct* CommStruct;
  7721. CommStruct = static_cast<TDragExtCommStruct*>(MapViewOfFile(FDDExtMapFile,
  7722. FILE_MAP_ALL_ACCESS, 0, 0, 0));
  7723. DebugAssert(CommStruct != NULL);
  7724. Result = !CommStruct->Dragging;
  7725. if (Result)
  7726. {
  7727. Directory = ExtractFilePath(CommStruct->DropDest);
  7728. CounterName = L"DownloadsDragDropExternalExt";
  7729. }
  7730. UnmapViewOfFile(CommStruct);
  7731. }
  7732. if (!Result)
  7733. {
  7734. Sleep(50);
  7735. Timer += 50;
  7736. Application->ProcessMessages();
  7737. }
  7738. }
  7739. }
  7740. __finally
  7741. {
  7742. Enabled = true;
  7743. }
  7744. }
  7745. return Result;
  7746. }
  7747. //---------------------------------------------------------------------------
  7748. void __fastcall TCustomScpExplorerForm::AddDelayedDirectoryDeletion(
  7749. const UnicodeString TempDir, int SecDelay)
  7750. {
  7751. TDateTime Alarm = IncSecond(Now(), SecDelay);
  7752. FDelayedDeletionList->AddObject(TempDir, reinterpret_cast<TObject*>(Alarm.FileDate()));
  7753. if (FDelayedDeletionTimer == NULL)
  7754. {
  7755. DebugAssert(HandleAllocated());
  7756. FDelayedDeletionTimer = new TTimer(this);
  7757. FDelayedDeletionTimer->Interval = 10000;
  7758. FDelayedDeletionTimer->OnTimer = DoDelayedDeletion;
  7759. }
  7760. else
  7761. {
  7762. FDelayedDeletionTimer->Enabled = true;
  7763. }
  7764. }
  7765. //---------------------------------------------------------------------------
  7766. void __fastcall TCustomScpExplorerForm::DoDelayedDeletion(TObject * Sender)
  7767. {
  7768. DebugAssert(FDelayedDeletionList != NULL);
  7769. TDateTime N = Now();
  7770. TDateTime Alert;
  7771. UnicodeString Directory;
  7772. for (int Index = FDelayedDeletionList->Count-1; Index >= 0; Index--)
  7773. {
  7774. Alert = FileDateToDateTime(reinterpret_cast<int>(FDelayedDeletionList->Objects[Index]));
  7775. if ((N >= Alert) || (Sender == NULL))
  7776. {
  7777. Directory = FDelayedDeletionList->Strings[Index];
  7778. if (RecursiveDeleteFile(ExcludeTrailingBackslash(Directory)))
  7779. {
  7780. FDelayedDeletionList->Delete(Index);
  7781. }
  7782. }
  7783. }
  7784. if (FDelayedDeletionList->Count == 0)
  7785. {
  7786. FDelayedDeletionTimer->Enabled = false;
  7787. }
  7788. }
  7789. //---------------------------------------------------------------------------
  7790. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDTargetDrop()
  7791. {
  7792. DebugAssert(!IsLocalBrowserMode());
  7793. if (IsFileControl(FDDTargetControl, osRemote) ||
  7794. (FDDTargetControl == SessionsPageControl))
  7795. {
  7796. TManagedTerminal * TargetTerminal = NULL;
  7797. TFileOperation Operation = foNone;
  7798. if (FDDTargetControl == SessionsPageControl)
  7799. {
  7800. TPoint Point = SessionsPageControl->ScreenToClient(Mouse->CursorPos);
  7801. int Index = SessionsPageControl->IndexOfTabAt(Point.X, Point.Y);
  7802. // do not allow dropping on local-local, disconnected and the "+" tab (see also SessionsDDProcessDropped)
  7803. TargetTerminal = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  7804. if (IsActiveTerminal(TargetTerminal) && DebugAlwaysTrue(!TargetTerminal->LocalBrowser))
  7805. {
  7806. if ((FLastDropEffect == DROPEFFECT_MOVE) &&
  7807. (TargetTerminal == TTerminalManager::Instance()->ActiveTerminal))
  7808. {
  7809. Operation = foRemoteMove;
  7810. }
  7811. else
  7812. {
  7813. Operation = foRemoteCopy;
  7814. }
  7815. }
  7816. }
  7817. else
  7818. {
  7819. if (FLastDropEffect == DROPEFFECT_MOVE)
  7820. {
  7821. Operation = foRemoteMove;
  7822. }
  7823. else if (FLastDropEffect == DROPEFFECT_COPY)
  7824. {
  7825. Operation = foRemoteCopy;
  7826. }
  7827. }
  7828. if (Operation != foNone)
  7829. {
  7830. RemoteFileControlFileOperation(DropSourceControl,
  7831. Operation, (WinConfiguration->DDTransferConfirmation == asOff), TargetTerminal);
  7832. }
  7833. // abort drag&drop
  7834. Abort();
  7835. }
  7836. else if ((FDDExtMapFile == NULL) && (FLastDropEffect != DROPEFFECT_NONE) &&
  7837. // Drops of remote files to queue are handled in RemoteFileControlDDEnd
  7838. (FDDTargetControl != QueueView3))
  7839. {
  7840. DebugAssert(!FDragTempDir.IsEmpty());
  7841. TTransferType Type;
  7842. UnicodeString TempDir = FDragTempDir;
  7843. // We clear FDragTempDir before calling
  7844. // just in case it fail (raises exception)
  7845. FDragTempDir = L"";
  7846. Type = (FLastDropEffect & DROPEFFECT_MOVE ? ttMove : Type = ttCopy);
  7847. TGUICopyParamType CopyParams = GUIConfiguration->CurrentCopyParam;
  7848. // empty directory parameter means temp directory -> don't display it!
  7849. UnicodeString TargetDir = L"";
  7850. int Options = 0;
  7851. if (!CopyParamDialog(tdToLocal, Type, true, FDDFileList,
  7852. TargetDir, CopyParams, (WinConfiguration->DDTransferConfirmation != asOff), true, Options))
  7853. {
  7854. Abort();
  7855. }
  7856. // TargetDir is set when dropped on local file control
  7857. // (this was workaround for legacy dirview event handling, now it should be
  7858. // made prettier)
  7859. if (TargetDir.IsEmpty())
  7860. {
  7861. TargetDir = TempDir;
  7862. if (ForceDirectories(ApiPath(TargetDir)))
  7863. {
  7864. DebugAssert(Terminal && !TargetDir.IsEmpty());
  7865. FPendingTempSpaceWarn = true;
  7866. try
  7867. {
  7868. FDragDropOperation = true;
  7869. int Params = cpTemporary |
  7870. (Type == ttMove ? cpDelete : 0);
  7871. DDDownload(FDDFileList, TargetDir, &CopyParams, Params);
  7872. Configuration->Usage->Inc(L"DownloadsDragDropExternalTemp");
  7873. }
  7874. __finally
  7875. {
  7876. FDragDropOperation = false;
  7877. FPendingTempSpaceWarn = false;
  7878. AddDelayedDirectoryDeletion(TargetDir, WinConfiguration->DDDeleteDelay);
  7879. }
  7880. }
  7881. else
  7882. {
  7883. throw Exception(FMTLOAD(CREATE_TEMP_DIR_ERROR, (TargetDir)));
  7884. }
  7885. }
  7886. }
  7887. }
  7888. //---------------------------------------------------------------------------
  7889. void __fastcall TCustomScpExplorerForm::DDDownload(
  7890. TStrings * FilesToCopy, const UnicodeString & TargetDir, TCopyParamType * CopyParam, int Params)
  7891. {
  7892. DebugAssert(!IsLocalBrowserMode());
  7893. TAutoBatch AutoBatch(this);
  7894. CopyParam->IncludeFileMask.SetRoots(TargetDir, FilesToCopy);
  7895. UpdateCopyParamCounters(*CopyParam);
  7896. Terminal->CopyToLocal(FilesToCopy, TargetDir, CopyParam, Params, NULL);
  7897. if (FLAGSET(Params, cpDelete) && (DropSourceControl == RemoteDriveView))
  7898. {
  7899. RemoteDriveView->UpdateDropSource();
  7900. }
  7901. }
  7902. //---------------------------------------------------------------------------
  7903. class TFakeDataObjectFilesEx : public TDataObjectFilesEx
  7904. {
  7905. public:
  7906. __fastcall TFakeDataObjectFilesEx(TFileList * AFileList, bool RenderPIDL,
  7907. bool RenderFilename) : TDataObjectFilesEx(AFileList, RenderPIDL, RenderFilename, true)
  7908. {
  7909. }
  7910. virtual bool __fastcall AllowData(const tagFORMATETC & FormatEtc)
  7911. {
  7912. return (FormatEtc.cfFormat == CF_HDROP) ? false :
  7913. TDataObjectFilesEx::AllowData(FormatEtc);
  7914. }
  7915. };
  7916. //---------------------------------------------------------------------------
  7917. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDCreateDataObject(
  7918. TObject * Sender, TDataObject *& DataObject)
  7919. {
  7920. if (FDDExtMapFile != NULL)
  7921. {
  7922. TFileList * FileList = DragDropFiles(Sender)->FileList;
  7923. if (!FileList->RenderPIDLs() || !FileList->RenderNames())
  7924. {
  7925. Abort();
  7926. }
  7927. if (FileList->Count > 0)
  7928. {
  7929. TDataObjectFilesEx * FilesObject = new TFakeDataObjectFilesEx(FileList, true, true);
  7930. if (!FilesObject->IsValid(true, true))
  7931. {
  7932. FilesObject->_Release();
  7933. }
  7934. else
  7935. {
  7936. DataObject = FilesObject;
  7937. }
  7938. }
  7939. }
  7940. }
  7941. //---------------------------------------------------------------------------
  7942. void __fastcall TCustomScpExplorerForm::GoToCommandLine()
  7943. {
  7944. DebugFail();
  7945. }
  7946. //---------------------------------------------------------------------------
  7947. void __fastcall TCustomScpExplorerForm::GoToTree()
  7948. {
  7949. ComponentVisible[fcRemoteTree] = true;
  7950. DriveView(osRemote)->SetFocus();
  7951. }
  7952. //---------------------------------------------------------------------------
  7953. TStrings * __fastcall TCustomScpExplorerForm::PanelExport(TOperationSide Side,
  7954. TPanelExport Export)
  7955. {
  7956. TCustomDirView * DirView = this->DirView(Side);
  7957. std::unique_ptr<TStrings> ExportData(new TStringList());
  7958. switch (Export)
  7959. {
  7960. case pePath:
  7961. ExportData->Add(DirView->PathName);
  7962. break;
  7963. case peFileList:
  7964. case peFullFileList:
  7965. {
  7966. bool FullPath = (Export == peFullFileList);
  7967. DirView->CreateFileList(false, FullPath, ExportData.get());
  7968. for (int Index = 0; Index < ExportData->Count; Index++)
  7969. {
  7970. if (ExportData->Strings[Index].Pos(L" ") > 0)
  7971. {
  7972. ExportData->Strings[Index] = FORMAT(L"\"%s\"", (ExportData->Strings[Index]));
  7973. }
  7974. }
  7975. }
  7976. break;
  7977. default:
  7978. DebugFail();
  7979. }
  7980. return ExportData.release();
  7981. }
  7982. //---------------------------------------------------------------------------
  7983. void __fastcall TCustomScpExplorerForm::PanelExport(TOperationSide Side,
  7984. TPanelExport Export, TPanelExportDestination Destination)
  7985. {
  7986. std::unique_ptr<TStrings> ExportData(PanelExport(Side, Export));
  7987. PanelExportStore(Side, Export, Destination, ExportData.get());
  7988. }
  7989. //---------------------------------------------------------------------------
  7990. void __fastcall TCustomScpExplorerForm::PanelExportStore(TOperationSide /*Side*/,
  7991. TPanelExport /*Export*/, TPanelExportDestination Destination,
  7992. TStrings * ExportData)
  7993. {
  7994. if (Destination == pedClipboard)
  7995. {
  7996. TInstantOperationVisualizer Visualizer;
  7997. CopyToClipboard(ExportData);
  7998. }
  7999. else
  8000. {
  8001. DebugFail();
  8002. }
  8003. }
  8004. //---------------------------------------------------------------------------
  8005. void __fastcall TCustomScpExplorerForm::Filter(TOperationSide Side)
  8006. {
  8007. TCustomDirView * DirView = this->DirView(Side);
  8008. UnicodeString Mask = DirView->Mask;
  8009. if (DoFilterMaskDialog(DirView, Mask))
  8010. {
  8011. DirView->Mask = TFileMasks::NormalizeMask(Mask);
  8012. Configuration->Usage->Inc(L"Filters");
  8013. }
  8014. }
  8015. //---------------------------------------------------------------------------
  8016. TQueueOperation __fastcall TCustomScpExplorerForm::DefaultQueueOperation()
  8017. {
  8018. return FQueueController->DefaultOperation();
  8019. }
  8020. //---------------------------------------------------------------------------
  8021. bool __fastcall TCustomScpExplorerForm::AllowQueueOperation(
  8022. TQueueOperation Operation, void ** Param)
  8023. {
  8024. switch (Operation)
  8025. {
  8026. case qoPreferences:
  8027. return true;
  8028. case qoGoTo:
  8029. return ComponentVisible[fcQueueView] && QueueView3->Enabled;
  8030. case qoOnceEmpty:
  8031. return IsAnythingQueued();
  8032. default:
  8033. return FQueueController->AllowOperation(Operation, Param);
  8034. }
  8035. }
  8036. //---------------------------------------------------------------------------
  8037. void __fastcall TCustomScpExplorerForm::GoToQueue()
  8038. {
  8039. if (DebugAlwaysTrue(QueueView3->Visible))
  8040. {
  8041. QueueView3->SetFocus();
  8042. }
  8043. }
  8044. //---------------------------------------------------------------------------
  8045. void __fastcall TCustomScpExplorerForm::ExecuteQueueOperation(
  8046. TQueueOperation Operation, void * Param)
  8047. {
  8048. if (Operation == qoGoTo)
  8049. {
  8050. GoToQueue();
  8051. }
  8052. else if (Operation == qoPreferences)
  8053. {
  8054. PreferencesDialog(pmQueue);
  8055. }
  8056. else
  8057. {
  8058. FQueueController->ExecuteOperation(Operation, Param);
  8059. }
  8060. }
  8061. //---------------------------------------------------------------------------
  8062. bool __fastcall TCustomScpExplorerForm::GetQueueEnabled()
  8063. {
  8064. return (Queue != NULL) && Queue->Enabled;
  8065. }
  8066. //---------------------------------------------------------------------------
  8067. void __fastcall TCustomScpExplorerForm::ToggleQueueEnabled()
  8068. {
  8069. DebugAssert(Queue != NULL);
  8070. if (Queue != NULL)
  8071. {
  8072. Queue->Enabled = !Queue->Enabled;
  8073. }
  8074. }
  8075. //---------------------------------------------------------------------------
  8076. void __fastcall TCustomScpExplorerForm::QueueView3ContextPopup(
  8077. TObject * /*Sender*/, TPoint & /*MousePos*/, bool & /*Handled*/)
  8078. {
  8079. FQueueActedItem = QueueView3->ItemFocused;
  8080. }
  8081. //---------------------------------------------------------------------------
  8082. /*virtual*/ int __fastcall TCustomScpExplorerForm::GetStaticComponentsHeight()
  8083. {
  8084. return TopDock->Height + (MessageDock->Visible ? MessageDock->Height : 0) + QueueSplitter->Height;
  8085. }
  8086. //---------------------------------------------------------------------------
  8087. int __fastcall TCustomScpExplorerForm::GetStaticQueuePanelComponentsHeight()
  8088. {
  8089. return
  8090. (QueueFileListSplitter->Visible ? QueueFileListSplitter->Height : 0) +
  8091. (QueueDock->Visible ? QueueDock->Height : 0) +
  8092. (QueueLabel->Visible ? QueueLabel->Height : 0);
  8093. }
  8094. //---------------------------------------------------------------------------
  8095. int __fastcall TCustomScpExplorerForm::GetMinQueueViewHeight()
  8096. {
  8097. return ScaleByTextHeight(this, 48);
  8098. }
  8099. //---------------------------------------------------------------------------
  8100. void __fastcall TCustomScpExplorerForm::QueueSplitterCanResize(
  8101. TObject * /*Sender*/, int & NewSize, bool & Accept)
  8102. {
  8103. // when queue is hidden by double-clicking splitter, stray attempt to
  8104. // resize the panel with strange value arrives, make sure it is ignored
  8105. if (ComponentVisible[fcQueueView])
  8106. {
  8107. int MaxHeightLimit = ClientHeight - GetStaticComponentsHeight() - RemotePanel->Constraints->MinHeight;
  8108. if (NewSize > MaxHeightLimit)
  8109. {
  8110. NewSize = MaxHeightLimit;
  8111. }
  8112. int MinHeightLimit =
  8113. GetStaticQueuePanelComponentsHeight() +
  8114. (QueueFileList->Visible ? QueueFileList->Height : 0) +
  8115. GetMinQueueViewHeight();
  8116. if (NewSize < MinHeightLimit)
  8117. {
  8118. NewSize = MinHeightLimit;
  8119. }
  8120. }
  8121. else
  8122. {
  8123. Accept = false;
  8124. }
  8125. }
  8126. //---------------------------------------------------------------------------
  8127. void __fastcall TCustomScpExplorerForm::QueueFileListSplitterCanResize(TObject *, int & NewSize, bool &)
  8128. {
  8129. int TotalHeight = GetStaticQueuePanelComponentsHeight() + NewSize;
  8130. int QueueViewHeight = QueuePanel->ClientHeight - TotalHeight;
  8131. if (QueueViewHeight < GetMinQueueViewHeight())
  8132. {
  8133. NewSize -= (GetMinQueueViewHeight() - QueueViewHeight);
  8134. }
  8135. }
  8136. //---------------------------------------------------------------------------
  8137. void __fastcall TCustomScpExplorerForm::QueueView3StartDrag(TObject * /*Sender*/,
  8138. TDragObject *& /*DragObject*/)
  8139. {
  8140. FQueueActedItem = QueueView3->ItemFocused;
  8141. FQueueScrollOnDragOver->StartDrag();
  8142. }
  8143. //---------------------------------------------------------------------------
  8144. void __fastcall TCustomScpExplorerForm::QueueView3EndDrag(
  8145. TObject *, TObject * DebugUsedArg(Target), int DebugUsedArg(X), int DebugUsedArg(Y))
  8146. {
  8147. FQueueScrollOnDragOver->EndDrag();
  8148. }
  8149. //---------------------------------------------------------------------------
  8150. void __fastcall TCustomScpExplorerForm::QueueView3DragOver(TObject * /*Sender*/,
  8151. TObject * Source, int X, int Y, TDragState /*State*/, bool & Accept)
  8152. {
  8153. Accept = true;
  8154. if (Source == QueueView3)
  8155. {
  8156. TListItem * DropTarget = QueueView3->GetItemAt(X, Y);
  8157. Accept = (DropTarget != NULL) && (FQueueActedItem != NULL);
  8158. if (Accept)
  8159. {
  8160. TQueueItemProxy * QueueItem;
  8161. TQueueItemProxy * DestQueueItem;
  8162. QueueItem = static_cast<TQueueItemProxy *>(FQueueActedItem->Data);
  8163. DestQueueItem = static_cast<TQueueItemProxy *>(DropTarget->Data);
  8164. Accept = (QueueItem != DestQueueItem) &&
  8165. (QueueItem->Status == TQueueItem::qsPending) &&
  8166. (DestQueueItem->Status == TQueueItem::qsPending);
  8167. }
  8168. FQueueScrollOnDragOver->DragOver(TPoint(X, Y));
  8169. }
  8170. }
  8171. //---------------------------------------------------------------------------
  8172. void __fastcall TCustomScpExplorerForm::QueueView3DragDrop(TObject * /*Sender*/,
  8173. TObject * /*Source*/, int /*X*/, int /*Y*/)
  8174. {
  8175. if ((FQueueActedItem != NULL) && (QueueView3->DropTarget != NULL))
  8176. {
  8177. TQueueItemProxy * QueueItem;
  8178. TQueueItemProxy * DestQueueItem;
  8179. QueueItem = static_cast<TQueueItemProxy *>(FQueueActedItem->Data);
  8180. DestQueueItem = static_cast<TQueueItemProxy *>(QueueView3->DropTarget->Data);
  8181. QueueItem->Move(DestQueueItem);
  8182. }
  8183. }
  8184. //---------------------------------------------------------------------------
  8185. void __fastcall TCustomScpExplorerForm::QueueView3Enter(TObject * /*Sender*/)
  8186. {
  8187. if ((QueueView3->ItemFocused == NULL) &&
  8188. (QueueView3->Items->Count > 0))
  8189. {
  8190. QueueView3->ItemFocused = QueueView3->Items->Item[0];
  8191. }
  8192. QueueLabelUpdateStatus();
  8193. }
  8194. //---------------------------------------------------------------------------
  8195. void __fastcall TCustomScpExplorerForm::QueueView3Exit(TObject * /*Sender*/)
  8196. {
  8197. QueueLabelUpdateStatus();
  8198. }
  8199. //---------------------------------------------------------------------------
  8200. void __fastcall TCustomScpExplorerForm::QueueLabelUpdateStatus()
  8201. {
  8202. QueueLabel->UpdateStatus();
  8203. }
  8204. //---------------------------------------------------------------------------
  8205. void __fastcall TCustomScpExplorerForm::QueueView3SelectItem(
  8206. TObject * /*Sender*/, TListItem * /*Item*/, bool Selected)
  8207. {
  8208. if (Selected)
  8209. {
  8210. NonVisualDataModule->UpdateNonVisibleActions();
  8211. }
  8212. }
  8213. //---------------------------------------------------------------------------
  8214. TDragDropFilesEx * __fastcall TCustomScpExplorerForm::DragDropFiles(TObject * Sender)
  8215. {
  8216. TDragDropFilesEx * Result = NULL;
  8217. if (Sender == SessionsPageControl)
  8218. {
  8219. Result = FSessionsDragDropFilesEx;
  8220. }
  8221. else if (Sender == QueueView3)
  8222. {
  8223. Result = FQueueDragDropFilesEx;
  8224. }
  8225. else
  8226. {
  8227. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  8228. if (DirView != NULL)
  8229. {
  8230. Result = DirView->DragDropFilesEx;
  8231. }
  8232. else
  8233. {
  8234. TCustomDriveView * DriveView = dynamic_cast<TCustomDriveView *>(Sender);
  8235. if (DriveView != NULL)
  8236. {
  8237. Result = DriveView->DragDropFilesEx;
  8238. }
  8239. }
  8240. }
  8241. DebugAssert(Result != NULL);
  8242. return Result;
  8243. }
  8244. //---------------------------------------------------------------------------
  8245. bool __fastcall TCustomScpExplorerForm::SelectedAllFilesInDirView(TCustomDirView * DView)
  8246. {
  8247. return (DView->SelCount == DView->FilesCount);
  8248. }
  8249. //---------------------------------------------------------------------------
  8250. bool __fastcall TCustomScpExplorerForm::DraggingAllFilesFromDirView(TOperationSide Side, TStrings * FileList)
  8251. {
  8252. return HasDirView[Side] && (DropSourceControl == DirView(Side)) && (FileList->Count == DirView(Side)->FilesCount);
  8253. }
  8254. //---------------------------------------------------------------------------
  8255. void __fastcall TCustomScpExplorerForm::RemoteFileControlDragDropFileOperation(
  8256. TObject * Sender, int Effect, UnicodeString TargetPath, bool ForceQueue, bool DragDrop)
  8257. {
  8258. DebugAssert(!IsLocalBrowserMode());
  8259. TFileOperation Operation;
  8260. switch (Effect)
  8261. {
  8262. case DROPEFFECT_MOVE:
  8263. Operation = foMove;
  8264. break;
  8265. case DROPEFFECT_COPY:
  8266. // occures on WinXP (reported by user)
  8267. default:
  8268. Operation = foCopy;
  8269. break;
  8270. };
  8271. TDragDropFilesEx * DragDropFilesEx = DragDropFiles(Sender);
  8272. // see a comment in TUnixDirView::PerformItemDragDropOperation
  8273. if (DragDropFilesEx->FileList->Count > 0)
  8274. {
  8275. TStrings * FileList = new TStringList();
  8276. try
  8277. {
  8278. for (int Index = 0; Index < DragDropFilesEx->FileList->Count; Index++)
  8279. {
  8280. FileList->Add(DragDropFilesEx->FileList->Items[Index]->Name);
  8281. }
  8282. FDragDropOperation = true;
  8283. TTransferOperationParam Param;
  8284. Param.TargetDirectory = TargetPath;
  8285. // upload, no temp dirs
  8286. Param.Temp = false;
  8287. Param.DragDrop = DragDrop;
  8288. Param.Options =
  8289. FLAGMASK(DraggingAllFilesFromDirView(osLocal, FileList), coAllFiles);
  8290. if (ForceQueue)
  8291. {
  8292. Param.Queue = asOn;
  8293. }
  8294. bool NoConfirmation = DragDrop ? (WinConfiguration->DDTransferConfirmation == asOff) : false;
  8295. if (ExecuteFileOperation(Operation, osLocal, FileList, NoConfirmation, &Param))
  8296. {
  8297. if (IsFileControl(DropSourceControl, osLocal))
  8298. {
  8299. Configuration->Usage->Inc(L"UploadsDragDropInternal");
  8300. }
  8301. else
  8302. {
  8303. Configuration->Usage->Inc(L"UploadsDragDropExternal");
  8304. }
  8305. }
  8306. }
  8307. __finally
  8308. {
  8309. FDragDropOperation = false;
  8310. delete FileList;
  8311. }
  8312. }
  8313. }
  8314. //---------------------------------------------------------------------------
  8315. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDFileOperation(
  8316. TObject * Sender, int Effect, UnicodeString /*SourcePath*/,
  8317. UnicodeString TargetPath, bool Paste, bool & /*DoOperation*/)
  8318. {
  8319. RemoteFileControlDragDropFileOperation(Sender, Effect, TargetPath, false, !Paste);
  8320. }
  8321. //---------------------------------------------------------------------------
  8322. void __fastcall TCustomScpExplorerForm::RemoteFileContolDDChooseEffect(
  8323. TObject * Sender, int grfKeyState, int & dwEffect)
  8324. {
  8325. DebugAssert(!IsLocalBrowserMode());
  8326. // if any drop effect is allowed at all (e.g. no drop to self and drop to parent)
  8327. if ((dwEffect != DROPEFFECT_NONE) &&
  8328. IsFileControl(DropSourceControl, osRemote))
  8329. {
  8330. // do not allow drop on remote panel (on free space, still allow drop on directories)
  8331. if ((Sender == RemoteDirView) && (DropSourceControl == RemoteDirView) &&
  8332. (RemoteDirView->DropTarget == NULL))
  8333. {
  8334. dwEffect = DROPEFFECT_NONE;
  8335. }
  8336. else
  8337. {
  8338. if (dwEffect == DROPEFFECT_COPY)
  8339. {
  8340. bool MoveCapable = Terminal->IsCapable[fcRemoteMove];
  8341. // currently we support copying always (at least via temporary directory);
  8342. // remove associated checks once this all proves stable and working
  8343. bool CopyCapable = true;
  8344. // if we do not support neither of operations, there's no discussion
  8345. if (!MoveCapable && !CopyCapable)
  8346. {
  8347. dwEffect = DROPEFFECT_NONE;
  8348. }
  8349. else
  8350. {
  8351. // The default effect inside remote panel is move,
  8352. // unless we do not support it, but support copy
  8353. if (FLAGCLEAR(grfKeyState, MK_CONTROL))
  8354. {
  8355. dwEffect = MoveCapable ? DROPEFFECT_MOVE : DROPEFFECT_COPY;
  8356. }
  8357. else
  8358. {
  8359. // with ctrl-down, we want copy unless it is not supported
  8360. dwEffect = CopyCapable ? DROPEFFECT_COPY : DROPEFFECT_NONE;
  8361. }
  8362. }
  8363. }
  8364. }
  8365. }
  8366. }
  8367. //---------------------------------------------------------------------------
  8368. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDDragFileName(
  8369. TObject * Sender, TRemoteFile * File, UnicodeString & FileName)
  8370. {
  8371. DebugAssert(!IsLocalBrowserMode());
  8372. if (FDDTotalSize >= 0)
  8373. {
  8374. if (File->IsDirectory)
  8375. {
  8376. FDDTotalSize = -1;
  8377. }
  8378. else
  8379. {
  8380. FDDTotalSize += File->Size;
  8381. }
  8382. }
  8383. DebugAssert(!FDragTempDir.IsEmpty());
  8384. // TODO: this is quite ineffective
  8385. // TODO: what if invalid character replacement is disabled?
  8386. FileName = FDragTempDir + GUIConfiguration->CurrentCopyParam.ValidLocalFileName(File->FileName);
  8387. UnicodeString TransferFileName;
  8388. if (Sender == RemoteDriveView)
  8389. {
  8390. TransferFileName = UnixExcludeTrailingBackslash(File->FullFileName);
  8391. }
  8392. else
  8393. {
  8394. TransferFileName = File->FileName;
  8395. }
  8396. FDDFileList->AddObject(TransferFileName, File);
  8397. }
  8398. //---------------------------------------------------------------------------
  8399. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDDragDetect(
  8400. TObject * /*Sender*/, int /*grfKeyState*/, const TPoint & /*DetectStart*/,
  8401. const TPoint & /*Point*/, TDragDetectStatus /*DragStatus*/)
  8402. {
  8403. // sometimes we do not get DDEnd so the list is not released
  8404. SAFE_DESTROY(FDDFileList);
  8405. FDDFileList = new TStringList();
  8406. FDragTempDir = WinConfiguration->TemporaryDir();
  8407. FDDTotalSize = 0;
  8408. FDragDropSshTerminate.reset(NULL);
  8409. }
  8410. //---------------------------------------------------------------------------
  8411. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDQueryContinueDrag(
  8412. TObject * /*Sender*/, BOOL /*FEscapePressed*/, int /*grfKeyState*/,
  8413. HRESULT & Result)
  8414. {
  8415. if (Result == DRAGDROP_S_DROP)
  8416. {
  8417. try
  8418. {
  8419. GlobalDragImageList->HideDragImage();
  8420. try
  8421. {
  8422. RemoteFileControlDDTargetDrop();
  8423. }
  8424. catch(ESshTerminate & E)
  8425. {
  8426. FDragDropSshTerminate.reset(E.Clone());
  8427. }
  8428. }
  8429. catch (Exception &E)
  8430. {
  8431. // If downloading fails we need to cancel drag&drop, otherwise
  8432. // Explorer shows error
  8433. // But by the way exception probably never reach this point as
  8434. // it's catched on way.
  8435. // Fatal exceptions get here (like when opening a secondary shell extension for file duplication fails).
  8436. Result = DRAGDROP_S_CANCEL;
  8437. ShowExtendedException(Terminal, &E);
  8438. }
  8439. }
  8440. }
  8441. //---------------------------------------------------------------------------
  8442. void __fastcall TCustomScpExplorerForm::DirViewMatchMask(
  8443. TObject * /*Sender*/, UnicodeString FileName, bool Directory, __int64 Size,
  8444. TDateTime Modification, UnicodeString Masks, bool & Matches, bool AllowImplicitMatches)
  8445. {
  8446. TFileMasks::TParams MaskParams;
  8447. MaskParams.Size = Size;
  8448. MaskParams.Modification = Modification;
  8449. // this does not re-parse the mask if it is the same as the last time
  8450. FDirViewMatchMask = Masks;
  8451. bool ImplicitMatch;
  8452. Matches =
  8453. // Local and RecurseInclude parameters have no effect as there's no path.
  8454. // This should use potential MatchesFileName overload with ImplicitMatch argument.
  8455. FDirViewMatchMask.Matches(FileName, false, Directory, &MaskParams, true, ImplicitMatch) &&
  8456. (AllowImplicitMatches || !ImplicitMatch);
  8457. }
  8458. //---------------------------------------------------------------------------
  8459. void __fastcall TCustomScpExplorerForm::DirViewGetOverlay(
  8460. TObject * Sender, TListItem * Item, WORD & Indexes)
  8461. {
  8462. TCustomDirView * DirView = reinterpret_cast<TCustomDirView *>(Sender);
  8463. if (GetPartialFileExtLen(DirView->ItemFileName(Item)) > 0)
  8464. {
  8465. Indexes |= oiPartial;
  8466. }
  8467. }
  8468. //---------------------------------------------------------------------------
  8469. bool __fastcall TCustomScpExplorerForm::CanPasteToDirViewFromClipBoard()
  8470. {
  8471. return
  8472. DirViewEnabled(osCurrent) &&
  8473. DirView(osCurrent)->CanPasteFromClipBoard();
  8474. }
  8475. //---------------------------------------------------------------------------
  8476. bool __fastcall TCustomScpExplorerForm::CanPasteFromClipBoard()
  8477. {
  8478. bool Result = false;
  8479. if (CanPasteToDirViewFromClipBoard())
  8480. {
  8481. Result = true;
  8482. }
  8483. else
  8484. {
  8485. UnicodeString ClipboardText;
  8486. if (NonEmptyTextFromClipboard(ClipboardText) &&
  8487. (ClipboardText.Pos(L"\n") == 0)) // it's already trimmed
  8488. {
  8489. if (StoredSessions->IsUrl(ClipboardText))
  8490. {
  8491. Result = true;
  8492. }
  8493. else
  8494. {
  8495. Result = DirViewEnabled(osCurrent);
  8496. }
  8497. }
  8498. }
  8499. return Result;
  8500. }
  8501. //---------------------------------------------------------------------------
  8502. void __fastcall TCustomScpExplorerForm::PasteFromClipBoard()
  8503. {
  8504. // Pasting to any local panel is handled in TScpCommanderForm::PasteFromClipBoard()
  8505. if (DoesClipboardContainOurFiles())
  8506. {
  8507. if (DebugAlwaysTrue(CanPasteToDirViewFromClipBoard()))
  8508. {
  8509. DebugAssert(!IsLocalBrowserMode());
  8510. TTerminalManager * Manager = TTerminalManager::Instance();
  8511. TTerminal * TargetTerminal = Manager->ActiveTerminal;
  8512. Manager->ActiveSession = FClipboardTerminal;
  8513. ExecuteFileOperation(foRemoteCopy, osRemote, FClipboardFileList.get(), false, TargetTerminal);
  8514. }
  8515. }
  8516. else if (CanPasteToDirViewFromClipBoard())
  8517. {
  8518. DirView(osCurrent)->PasteFromClipBoard();
  8519. }
  8520. else
  8521. {
  8522. UnicodeString ClipboardText;
  8523. if (NonEmptyTextFromClipboard(ClipboardText))
  8524. {
  8525. if (StoredSessions->IsUrl(ClipboardText))
  8526. {
  8527. NewSession(ClipboardText);
  8528. }
  8529. else
  8530. {
  8531. DirView(osCurrent)->Path = ClipboardText;
  8532. }
  8533. }
  8534. }
  8535. }
  8536. //---------------------------------------------------------------------------
  8537. void __fastcall TCustomScpExplorerForm::FileListFromClipboard()
  8538. {
  8539. // TBD
  8540. }
  8541. //---------------------------------------------------------------------------
  8542. void __fastcall TCustomScpExplorerForm::SelectAll(TOperationSide Side, TSelectMode Mode)
  8543. {
  8544. TCustomDirView * ADirView = DirView(Side);
  8545. ADirView->SelectAll(Mode);
  8546. ADirView->SetFocus();
  8547. }
  8548. //---------------------------------------------------------------------------
  8549. void __fastcall TCustomScpExplorerForm::SelectByMask(TOperationSide Side, bool Select)
  8550. {
  8551. TCustomDirView * ADirView = DirView(Side);
  8552. TFileFilter Filter;
  8553. DefaultFileFilter(Filter);
  8554. if (DoSelectMaskDialog(ADirView, Select, Filter))
  8555. {
  8556. Configuration->Usage->Inc(L"MaskSelections");
  8557. ADirView->SelectFiles(Filter, Select);
  8558. ADirView->SetFocus();
  8559. }
  8560. }
  8561. //---------------------------------------------------------------------------
  8562. void __fastcall TCustomScpExplorerForm::RestoreSelectedNames(TOperationSide Side)
  8563. {
  8564. TCustomDirView * ADirView = DirView(Side);
  8565. ADirView->RestoreSelectedNames();
  8566. ADirView->SetFocus();
  8567. }
  8568. //---------------------------------------------------------------------------
  8569. void __fastcall TCustomScpExplorerForm::SelectSameExt(bool Select)
  8570. {
  8571. TCustomDirView * CurrentDirView = DirView(osCurrent);
  8572. if (DebugAlwaysTrue(CurrentDirView->ItemFocused != NULL))
  8573. {
  8574. UnicodeString FileName = CurrentDirView->ItemFileName(CurrentDirView->ItemFocused);
  8575. UnicodeString Ext;
  8576. if (!IsSideLocalBrowser(osCurrent))
  8577. {
  8578. Ext = UnixExtractFileExt(FileName);
  8579. }
  8580. else
  8581. {
  8582. Ext = ExtractFileExt(FileName);
  8583. }
  8584. if (Ext.IsEmpty())
  8585. {
  8586. Ext = L".";
  8587. }
  8588. TFileFilter Filter;
  8589. Filter.Masks = FORMAT(L"*%s", (Ext));
  8590. Filter.Directories = false;
  8591. CurrentDirView->SelectFiles(Filter, Select);
  8592. }
  8593. }
  8594. //---------------------------------------------------------------------------
  8595. UnicodeString __fastcall TCustomScpExplorerForm::FileStatusBarText(
  8596. const TStatusFileInfo & FileInfo, TOperationSide Side)
  8597. {
  8598. UnicodeString Result;
  8599. if (!FIncrementalSearch.IsEmpty() && (Side == GetSide(osCurrent)))
  8600. {
  8601. Result = FormatIncrementalSearchStatus(FIncrementalSearch, FIncrementalSearchHaveNext);
  8602. }
  8603. else if (!IsSideLocalBrowser(Side) && ((Terminal == NULL) || Terminal->Disconnected))
  8604. {
  8605. // noop
  8606. }
  8607. else
  8608. {
  8609. Result =
  8610. FMTLOAD(FILE_INFO_FORMAT,
  8611. (FormatBytes(FileInfo.SelectedSize),
  8612. FormatBytes(FileInfo.FilesSize),
  8613. FormatNumber(FileInfo.SelectedCount),
  8614. FormatNumber(FileInfo.FilesCount)));
  8615. }
  8616. return Result;
  8617. }
  8618. //---------------------------------------------------------------------------
  8619. void __fastcall TCustomScpExplorerForm::FileStatusBarPanelClick(
  8620. TTBXStatusPanel * Panel, TOperationSide Side)
  8621. {
  8622. if (Panel->Index == 1)
  8623. {
  8624. ToggleShowHiddenFiles();
  8625. }
  8626. else if (Panel->Index == 2)
  8627. {
  8628. Filter(Side);
  8629. }
  8630. }
  8631. //---------------------------------------------------------------------------
  8632. void __fastcall TCustomScpExplorerForm::UpdateFileStatusBar(
  8633. TTBXStatusBar * StatusBar, const TStatusFileInfo & FileInfo, TOperationSide Side)
  8634. {
  8635. DebugAssert(!StatusBar->SimplePanel);
  8636. StatusBar->Panels->Items[0]->Caption = FileStatusBarText(FileInfo, Side);
  8637. UpdateFileStatusExtendedPanels(StatusBar, FileInfo);
  8638. }
  8639. //---------------------------------------------------------------------------
  8640. void __fastcall TCustomScpExplorerForm::UpdateFileStatusExtendedPanels(
  8641. TTBXStatusBar * StatusBar, const TStatusFileInfo & FileInfo)
  8642. {
  8643. DebugAssert(StatusBar->Panels->Count >= 3);
  8644. TTBXStatusPanel * HiddenFilesPanel = StatusBar->Panels->Items[1];
  8645. if (FileInfo.HiddenCount > 0)
  8646. {
  8647. HiddenFilesPanel->Caption = FMTLOAD(FILE_INFO_HIDDEN2, (FormatNumber(FileInfo.HiddenCount)));
  8648. HiddenFilesPanel->ViewPriority = 90; // <100 allows hiding panel when it does not fit
  8649. }
  8650. else
  8651. {
  8652. HiddenFilesPanel->ViewPriority = 0;
  8653. // not really necessary, just to cleanup no-longer-valid data
  8654. HiddenFilesPanel->Caption = L"";
  8655. }
  8656. TTBXStatusPanel * FilteredFilesPanel = StatusBar->Panels->Items[2];
  8657. if (FileInfo.FilteredCount > 0)
  8658. {
  8659. FilteredFilesPanel->Caption = FMTLOAD(FILE_INFO_FILTERED2, (FormatNumber(FileInfo.FilteredCount)));
  8660. FilteredFilesPanel->ViewPriority = 90; // <100 allows hiding panel when it does not fit
  8661. }
  8662. else
  8663. {
  8664. FilteredFilesPanel->ViewPriority = 0;
  8665. // not really necessary, just to cleanup no-longer-valid data
  8666. FilteredFilesPanel->Caption = L"";
  8667. }
  8668. }
  8669. //---------------------------------------------------------------------------
  8670. void __fastcall TCustomScpExplorerForm::RemoteStatusBarClick(
  8671. TObject * /*Sender*/)
  8672. {
  8673. if (DirView(osOther)->Enabled)
  8674. {
  8675. DirView(osOther)->SetFocus();
  8676. }
  8677. }
  8678. //---------------------------------------------------------------------------
  8679. void __fastcall TCustomScpExplorerForm::ToggleQueueVisibility()
  8680. {
  8681. TQueueViewConfiguration Config = WinConfiguration->QueueView;
  8682. switch (Config.Show)
  8683. {
  8684. case qvShow:
  8685. if ((FQueueStatus != NULL) && (FQueueStatus->Count > 0))
  8686. {
  8687. Config.Show = qvHide;
  8688. }
  8689. else
  8690. {
  8691. Config.Show = Config.LastHideShow;
  8692. }
  8693. break;
  8694. case qvHideWhenEmpty:
  8695. if (ComponentVisible[fcQueueView])
  8696. {
  8697. Config.Show = qvHide;
  8698. }
  8699. else
  8700. {
  8701. Config.LastHideShow = Config.Show;
  8702. Config.Show = qvShow;
  8703. }
  8704. break;
  8705. case qvHide:
  8706. Config.LastHideShow = Config.Show;
  8707. Config.Show = qvShow;
  8708. break;
  8709. }
  8710. WinConfiguration->QueueView = Config;
  8711. }
  8712. //---------------------------------------------------------------------------
  8713. UnicodeString __fastcall TCustomScpExplorerForm::PathForCaption()
  8714. {
  8715. UnicodeString Result;
  8716. if (Terminal != NULL)
  8717. {
  8718. switch (WinConfiguration->PathInCaption)
  8719. {
  8720. case picShort:
  8721. Result = ExtractShortName(Terminal->CurrentDirectory, true);
  8722. break;
  8723. case picFull:
  8724. Result = Terminal->CurrentDirectory;
  8725. break;
  8726. }
  8727. }
  8728. return Result;
  8729. }
  8730. //---------------------------------------------------------------------------
  8731. TColor __fastcall TCustomScpExplorerForm::PanelColor()
  8732. {
  8733. TColor Result = GetWindowColor(FSessionColor);
  8734. return Result;
  8735. }
  8736. //---------------------------------------------------------------------------
  8737. TColor __fastcall TCustomScpExplorerForm::DisabledPanelColor()
  8738. {
  8739. TColor Result = (WinConfiguration->UseDarkTheme() ? static_cast<TColor>(RGB(0x40, 0x40, 0x40)) : clBtnFace);
  8740. return Result;
  8741. }
  8742. //---------------------------------------------------------------------------
  8743. void __fastcall TCustomScpExplorerForm::UpdateControls()
  8744. {
  8745. if (!FSessionChanging)
  8746. {
  8747. TTerminalManager::Instance()->UpdateAppTitle();
  8748. }
  8749. // WORKAROUND: Disabling list view when it is not showing yet does not set its
  8750. // background to gray on Windows 7 (works on Windows 10).
  8751. // See also EnableControl
  8752. if (Showing)
  8753. {
  8754. if (ManagedSession != NULL)
  8755. {
  8756. // Update path when it changes
  8757. if ((SessionsPageControl->ActivePage != NULL) &&
  8758. (GetSessionTabSession(SessionsPageControl->ActivePage) == ManagedSession))
  8759. {
  8760. UpdateSessionTab(SessionsPageControl->ActivePage);
  8761. }
  8762. }
  8763. NonVisualDataModule->ReconnectSessionAction->Update();
  8764. ReconnectToolbar->Visible = NonVisualDataModule->ReconnectSessionAction->Visible;
  8765. // ReconnectSessionAction is hidden when disabled, so enabling it actualy resizes the toolbar
  8766. CenterReconnectToolbar();
  8767. bool HasTerminal = HasActiveTerminal();
  8768. RemoteDirView->Enabled = HasTerminal;
  8769. RemoteDirView->Color = HasTerminal ? PanelColor() : DisabledPanelColor();
  8770. RemoteDirView->Font->Color = GetWindowTextColor(RemoteDirView->Color);
  8771. RemoteDriveView->Enabled = RemoteDirView->Enabled;
  8772. RemoteDriveView->Color = RemoteDirView->Color;
  8773. RemoteDriveView->Font->Color = RemoteDirView->Font->Color;
  8774. QueueView3->Enabled = HasTerminal && Terminal->IsCapable[fcBackgroundTransfers];
  8775. QueueView3->Color = QueueView3->Enabled ? GetWindowColor() : DisabledPanelColor();
  8776. QueueView3->Font->Color = GetWindowTextColor(QueueView3->Color);
  8777. QueueFileList->Enabled = QueueView3->Enabled;
  8778. QueueFileList->Color = QueueView3->Color;
  8779. QueueFileList->Font->Color = QueueView3->Font->Color;
  8780. QueueLabelUpdateStatus();
  8781. RemoteDirView->DarkMode = WinConfiguration->UseDarkTheme();
  8782. RemoteDriveView->DarkMode = RemoteDirView->DarkMode;
  8783. if (FImmersiveDarkMode != WinConfiguration->UseDarkTheme())
  8784. {
  8785. UpdateDarkMode();
  8786. }
  8787. // As the hi-contrast theme is currently used for session tabs only, which currently do not support dark mode,
  8788. // we always use dark theme. But ultimatelly, we should use an opposite theme to the main one.
  8789. UnicodeString CurrentHiContrastThemeName = (FHiContrastTheme != NULL) ? FHiContrastTheme->Name : EmptyStr;
  8790. UnicodeString NewHiContrastThemeName = WinConfiguration->HiContrast ? GetThemeName(true) : EmptyStr;
  8791. if (CurrentHiContrastThemeName != NewHiContrastThemeName)
  8792. {
  8793. ReleaseHiContrastTheme();
  8794. if (!NewHiContrastThemeName.IsEmpty())
  8795. {
  8796. FHiContrastTheme = GetTBXTheme(NewHiContrastThemeName);
  8797. }
  8798. SessionsPageControl->ActiveTabTheme = FHiContrastTheme;
  8799. }
  8800. reinterpret_cast<TTBCustomItem *>(GetComponent(fcRemotePathComboBox))->Enabled = HasTerminal || IsLocalBrowserMode();
  8801. MessageDock->Visible = (MessageDock->ToolbarCount > 0);
  8802. }
  8803. }
  8804. //---------------------------------------------------------------------------
  8805. void __fastcall TCustomScpExplorerForm::DoDirViewLoaded(TCustomDirView * /*Sender*/)
  8806. {
  8807. UpdateControls();
  8808. }
  8809. //---------------------------------------------------------------------------
  8810. void __fastcall TCustomScpExplorerForm::DirViewLoaded(
  8811. TObject * Sender)
  8812. {
  8813. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  8814. DebugAssert(DirView != NULL);
  8815. DoDirViewLoaded(DirView);
  8816. TransferPresetAutoSelect();
  8817. }
  8818. //---------------------------------------------------------------------------
  8819. void __fastcall TCustomScpExplorerForm::StartUpdates()
  8820. {
  8821. TUpdatesConfiguration Updates = WinConfiguration->Updates;
  8822. // first run after installation
  8823. if (double(Updates.LastCheck) == 0)
  8824. {
  8825. AppLog(L"First run, scheduling updates check for the next run");
  8826. // make sure next time there will be an update (if enabled)
  8827. Updates.LastCheck = TDateTime(1);
  8828. WinConfiguration->Updates = Updates;
  8829. }
  8830. else
  8831. {
  8832. TDateTime Period;
  8833. if (IsUWP() && !IsOfficialPackage())
  8834. {
  8835. Period = DefaultUpdatesPeriod;
  8836. AppLog(L"Thirdparty UWP package, using default updates check period");
  8837. }
  8838. else
  8839. {
  8840. Period = Updates.Period;
  8841. }
  8842. AppLogFmt(L"Updates check period: %.2f", (double(Period)));
  8843. if (double(Period) > 0)
  8844. {
  8845. TDateTime Interval = Now() - Updates.LastCheck;
  8846. AppLogFmt(L"Interval since the last updates check: %.2f", (double(Interval)));
  8847. if (Interval >= Period)
  8848. {
  8849. StartUpdateThread(UpdatesChecked);
  8850. }
  8851. }
  8852. }
  8853. }
  8854. //---------------------------------------------------------------------------
  8855. void __fastcall TCustomScpExplorerForm::UpdatesChecked()
  8856. {
  8857. AppLog(L"Updates check done");
  8858. UnicodeString Message;
  8859. bool New;
  8860. TQueryType Type;
  8861. GetUpdatesMessage(Message, New, Type, false);
  8862. if (!Message.IsEmpty())
  8863. {
  8864. AppLogFmt(L"Updates check message: %s", (Message));
  8865. if (New)
  8866. {
  8867. AppLog(L"New version detected");
  8868. Message = FMTLOAD(NEW_VERSION_CLICK, (Message));
  8869. }
  8870. if (!New && (Type != qtWarning))
  8871. {
  8872. AppLog(L"Posting non-critical updates note");
  8873. PostNote(UnformatMessage(Message), 0, UpdatesNoteClicked, NULL);
  8874. }
  8875. else
  8876. {
  8877. AppLog(L"Posting new version or critical message notification");
  8878. Notify(NULL, Message, Type, true, UpdatesNoteClicked);
  8879. }
  8880. if (New)
  8881. {
  8882. Configuration->Usage->Inc(L"UpdateNotifications");
  8883. }
  8884. }
  8885. }
  8886. //---------------------------------------------------------------------------
  8887. void __fastcall TCustomScpExplorerForm::UpdatesNoteClicked(TObject * /*Sender*/)
  8888. {
  8889. RestoreApp();
  8890. bool CanDisplay = !NonVisualDataModule->Busy;
  8891. if (!CanDisplay && (Screen->ActiveForm != NULL))
  8892. {
  8893. CanDisplay = (Screen->ActiveForm->Perform(WM_CAN_DISPLAY_UPDATES, 0, 0) != 0);
  8894. }
  8895. if (CanDisplay)
  8896. {
  8897. Configuration->Usage->Inc(L"UpdateNotificationsClicked");
  8898. // Probably not needed as CheckForUpdates is blocking since we started using neon for HTTP implementation.
  8899. NonVisualDataModule->StartBusy();
  8900. try
  8901. {
  8902. CheckForUpdates(true);
  8903. }
  8904. __finally
  8905. {
  8906. NonVisualDataModule->EndBusy();
  8907. }
  8908. }
  8909. else
  8910. {
  8911. Configuration->Usage->Inc(L"UpdateNotificationsBusy");
  8912. }
  8913. }
  8914. //---------------------------------------------------------------------------
  8915. void __fastcall TCustomScpExplorerForm::GetTransferPresetAutoSelectData(
  8916. TCopyParamRuleData & Data)
  8917. {
  8918. DebugAssert(Terminal != NULL);
  8919. Data.HostName = Terminal->SessionData->HostNameExpanded;
  8920. Data.UserName = Terminal->SessionData->UserNameExpanded;
  8921. Data.RemoteDirectory = RemoteDirView->PathName;
  8922. }
  8923. //---------------------------------------------------------------------------
  8924. void __fastcall TCustomScpExplorerForm::TransferPresetAutoSelect()
  8925. {
  8926. // Terminal can be null when we are changing local directory implicitly,
  8927. // because it has been deleted, while no session is connected
  8928. // (Login dialog is open)
  8929. if (FAllowTransferPresetAutoSelect && (Terminal != NULL))
  8930. {
  8931. DebugAssert(!IsLocalBrowserMode());
  8932. TCopyParamRuleData Data;
  8933. GetTransferPresetAutoSelectData(Data);
  8934. int CopyParamIndex = GUIConfiguration->CopyParamList->Find(Data);
  8935. UnicodeString CopyParamCurrent = GUIConfiguration->CopyParamCurrent;
  8936. if (CopyParamIndex < 0)
  8937. {
  8938. // there is no preset that matches autoselection
  8939. // set preset that we consider "default"
  8940. FCopyParamAutoSelected = L""; // forget last autoselected preset
  8941. GUIConfiguration->CopyParamCurrent = FCopyParamDefault;
  8942. }
  8943. else
  8944. {
  8945. // there is preset matching autoselection
  8946. UnicodeString CopyParamName = GUIConfiguration->CopyParamList->Names[CopyParamIndex];
  8947. if (CopyParamName == FCopyParamAutoSelected)
  8948. {
  8949. // autoselected the same preset as the last time
  8950. // make no change (i.e. preserve custom user preset, if any)
  8951. }
  8952. else
  8953. {
  8954. // autoselected the different preset then the last time (or there
  8955. // was default preset set)
  8956. FCopyParamAutoSelected = CopyParamName; // remember autoselection
  8957. GUIConfiguration->CopyParamCurrent = CopyParamName;
  8958. }
  8959. }
  8960. if (GUIConfiguration->CopyParamCurrent != CopyParamCurrent)
  8961. {
  8962. if (CopyParamIndex >= 0)
  8963. {
  8964. Configuration->Usage->Inc(L"CopyParamAutoSelects");
  8965. }
  8966. TTransferPresetNoteData * Data = new TTransferPresetNoteData;
  8967. try
  8968. {
  8969. int Fmt =
  8970. (CopyParamIndex < 0) ?
  8971. (GUIConfiguration->CopyParamIndex < 0 ? COPY_PARAM_DEFAULT_NORM : COPY_PARAM_DEFAULT_CUSTOM) :
  8972. COPY_PARAM_AUTOSELECTED;
  8973. UnicodeString Message = FMTLOAD(Fmt, (StripHotkey(GUIConfiguration->CopyParamCurrent)));
  8974. Data->Message = MainInstructions(Message);
  8975. int CopyParamAttrs = Terminal->UsableCopyParamAttrs(0).General;
  8976. UnicodeString Info = GUIConfiguration->CurrentCopyParam.GetInfoStr(L"\n",
  8977. CopyParamAttrs);
  8978. if (CopyParamIndex >= 0)
  8979. {
  8980. DebugAssert(GUIConfiguration->CopyParamList->Rules[CopyParamIndex] != NULL);
  8981. Info = FORMAT(L"%s\n \n%s", (Info,
  8982. FMTLOAD(COPY_PARAM_RULE,
  8983. (GUIConfiguration->CopyParamList->Rules[CopyParamIndex]->GetInfoStr(L"\n")))));
  8984. }
  8985. Data->Message += L"\n\n" + Info;
  8986. if (WinConfiguration->CopyParamAutoSelectNotice)
  8987. {
  8988. TransferPresetNoteMessage(Data, true);
  8989. }
  8990. else
  8991. {
  8992. PostNote(Message, 0, TransferPresetNoteClicked, Data);
  8993. Data = NULL; // ownership passed
  8994. }
  8995. }
  8996. __finally
  8997. {
  8998. delete Data;
  8999. }
  9000. }
  9001. }
  9002. }
  9003. //---------------------------------------------------------------------------
  9004. void __fastcall TCustomScpExplorerForm::TransferPresetNoteMessage(
  9005. TTransferPresetNoteData * NoteData, bool AllowNeverAskAgain)
  9006. {
  9007. DebugAssert(NoteData != NULL);
  9008. DebugAssert(!IsLocalBrowserMode());
  9009. TMessageParams Params(AllowNeverAskAgain ? mpNeverAskAgainCheck : 0);
  9010. TQueryButtonAlias Aliases[1];
  9011. Aliases[0].Button = qaIgnore; // "ignore" is after "ok"
  9012. Aliases[0].Alias = LoadStr(CONFIGURE_BUTTON);
  9013. Params.Aliases = Aliases;
  9014. Params.AliasesCount = LENOF(Aliases);
  9015. unsigned int Result =
  9016. MoreMessageDialog(NoteData->Message, NULL, qtInformation,
  9017. qaOK | qaIgnore, HELP_COPY_PARAM_AUTOSELECTED, &Params);
  9018. switch (Result)
  9019. {
  9020. case qaNeverAskAgain:
  9021. DebugAssert(AllowNeverAskAgain);
  9022. WinConfiguration->CopyParamAutoSelectNotice = false;
  9023. break;
  9024. case qaIgnore:
  9025. PreferencesDialog(pmPresets);
  9026. break;
  9027. }
  9028. }
  9029. //---------------------------------------------------------------------------
  9030. void __fastcall TCustomScpExplorerForm::TransferPresetNoteClicked(TObject * Sender)
  9031. {
  9032. // as of now useless, as this is used for notes only, never for balloons, ...
  9033. RestoreApp();
  9034. // .. and we should never be busy here
  9035. if (DebugAlwaysTrue(!NonVisualDataModule->Busy))
  9036. {
  9037. TransferPresetNoteMessage(DebugNotNull(dynamic_cast<TTransferPresetNoteData *>(Sender)), false);
  9038. }
  9039. }
  9040. //---------------------------------------------------------------------------
  9041. void __fastcall TCustomScpExplorerForm::PreferencesDialog(
  9042. TPreferencesMode APreferencesMode)
  9043. {
  9044. std::unique_ptr<TPreferencesDialogData> PreferencesData;
  9045. TCopyParamRuleData Data;
  9046. if (Terminal != NULL)
  9047. {
  9048. PreferencesData.reset(new TPreferencesDialogData());
  9049. GetTransferPresetAutoSelectData(Data);
  9050. PreferencesData->CopyParamRuleData = &Data;
  9051. }
  9052. DoPreferencesDialog(APreferencesMode, PreferencesData.get());
  9053. }
  9054. //---------------------------------------------------------------------------
  9055. void __fastcall TCustomScpExplorerForm::AdHocCustomCommandValidate(
  9056. const TCustomCommandType & Command)
  9057. {
  9058. if (CustomCommandState(Command, FEditingFocusedAdHocCommand, ccltAll) <= 0)
  9059. {
  9060. throw Exception(FMTLOAD(CUSTOM_COMMAND_IMPOSSIBLE2, (Command.Command)));
  9061. }
  9062. }
  9063. //---------------------------------------------------------------------------
  9064. void __fastcall TCustomScpExplorerForm::AdHocCustomCommand(bool OnFocused)
  9065. {
  9066. bool RemoteAllowed = CustomCommandRemoteAllowed();
  9067. TCustomCommandType Command;
  9068. // make sure we use local custom command when remote are not supported
  9069. if (RemoteAllowed || FLAGSET(FLastCustomCommand.Params, ccLocal))
  9070. {
  9071. Command = FLastCustomCommand;
  9072. }
  9073. else
  9074. {
  9075. Command.Params = Command.Params | ccLocal;
  9076. }
  9077. Command.Name = LoadStr(CUSTOM_COMMAND_AD_HOC_NAME);
  9078. FEditingFocusedAdHocCommand = OnFocused;
  9079. bool LocalSide = IsSideLocalBrowser(FCurrentSide);
  9080. int Options =
  9081. FLAGMASK((!RemoteAllowed || LocalSide), ccoDisableRemote) |
  9082. FLAGMASK(LocalSide, ccoDisableRemoteFiles);
  9083. if (DoCustomCommandDialog(Command, WinConfiguration->CustomCommandList,
  9084. ccmAdHoc, Options, AdHocCustomCommandValidate, NULL))
  9085. {
  9086. FLastCustomCommand = Command;
  9087. UpdateCustomCommandsToolbar();
  9088. ExecuteFileOperation(foCustomCommand, osRemote, OnFocused, false, &Command);
  9089. }
  9090. }
  9091. //---------------------------------------------------------------------------
  9092. void __fastcall TCustomScpExplorerForm::LastCustomCommand(bool OnFocused)
  9093. {
  9094. DebugAssert(!FLastCustomCommand.Command.IsEmpty());
  9095. int State = CustomCommandState(FLastCustomCommand, OnFocused, ccltAll);
  9096. DebugAssert(State > 0);
  9097. if (State <= 0)
  9098. {
  9099. throw Exception(FMTLOAD(CUSTOM_COMMAND_IMPOSSIBLE2, (FLastCustomCommand.Command)));
  9100. }
  9101. ExecuteFileOperation(foCustomCommand, osRemote, OnFocused, false, &FLastCustomCommand);
  9102. }
  9103. //---------------------------------------------------------------------------
  9104. bool __fastcall TCustomScpExplorerForm::GetLastCustomCommand(bool OnFocused,
  9105. TCustomCommandType & Command, int & State)
  9106. {
  9107. bool Result = !FLastCustomCommand.Command.IsEmpty();
  9108. if (Result)
  9109. {
  9110. Command = FLastCustomCommand;
  9111. State = CustomCommandState(FLastCustomCommand, OnFocused, ccltAll);
  9112. }
  9113. return Result;
  9114. }
  9115. //---------------------------------------------------------------------------
  9116. void __fastcall TCustomScpExplorerForm::BeforeAction()
  9117. {
  9118. ResetIncrementalSearch();
  9119. }
  9120. //---------------------------------------------------------------------------
  9121. void __fastcall TCustomScpExplorerForm::PostComponentHide(Byte Component)
  9122. {
  9123. DebugAssert(ComponentVisible[Component]);
  9124. PostMessage(Handle, WM_COMPONENT_HIDE, Component, 0);
  9125. }
  9126. //---------------------------------------------------------------------------
  9127. void __fastcall TCustomScpExplorerForm::QueueSplitterDblClick(TObject * /*Sender*/)
  9128. {
  9129. // when queue panel is resized here directly, the status bar is stretched
  9130. // over whole space the panel occupied
  9131. PostComponentHide(fcQueueView);
  9132. }
  9133. //---------------------------------------------------------------------------
  9134. void __fastcall TCustomScpExplorerForm::QueueFileListSplitterDblClick(TObject *)
  9135. {
  9136. ComponentVisible[fcQueueFileList] = false;
  9137. }
  9138. //---------------------------------------------------------------------------
  9139. void __fastcall TCustomScpExplorerForm::ThemeChanged()
  9140. {
  9141. // We hoped this will refresh scrollbar colors, but it does not have any effect here.
  9142. RefreshColorMode();
  9143. ResetSysDarkTheme();
  9144. ConfigurationChanged();
  9145. ConfigureInterface();
  9146. // Should be called for all controls
  9147. RemoteDirView->Perform(WM_THEMECHANGED, 0, 0);
  9148. }
  9149. //---------------------------------------------------------------------------
  9150. void __fastcall TCustomScpExplorerForm::WMSettingChange(TMessage & Message)
  9151. {
  9152. // Do not handle, when shutting down anyway (maybe also when not setup completely yet?)
  9153. if (!FInvalid &&
  9154. (Message.LParam != 0) &&
  9155. (wcscmp(reinterpret_cast<LPCWCH>(Message.LParam), L"ImmersiveColorSet") == 0))
  9156. {
  9157. ThemeChanged();
  9158. }
  9159. TForm::Dispatch(&Message);
  9160. }
  9161. //---------------------------------------------------------------------------
  9162. void __fastcall TCustomScpExplorerForm::Dispatch(void * Message)
  9163. {
  9164. TMessage * M = static_cast<TMessage*>(Message);
  9165. switch (M->Msg)
  9166. {
  9167. case CM_DIALOGCHAR:
  9168. CMDialogChar(*M);
  9169. break;
  9170. case WM_APPCOMMAND:
  9171. WMAppCommand(*M);
  9172. break;
  9173. case WM_SYSCOMMAND:
  9174. WMSysCommand(*M);
  9175. break;
  9176. case WM_QUERYENDSESSION:
  9177. WMQueryEndSession(*M);
  9178. break;
  9179. case WM_ENDSESSION:
  9180. WMEndSession(*reinterpret_cast<TWMEndSession *>(M));
  9181. break;
  9182. #ifdef _DEBUG
  9183. case WM_WINDOWPOSCHANGED:
  9184. WMWindowPosChanged(*reinterpret_cast<TWMWindowPosMsg *>(M));
  9185. break;
  9186. case CM_CUSTOMSTYLECHANGED:
  9187. TForm::Dispatch(Message);
  9188. break;
  9189. #endif
  9190. case WM_COMPONENT_HIDE:
  9191. {
  9192. Byte Component = static_cast<Byte>(M->WParam);
  9193. // sanity check
  9194. if (ComponentVisible[Component])
  9195. {
  9196. // special treatment
  9197. if (Component == fcQueueView)
  9198. {
  9199. ToggleQueueVisibility();
  9200. DebugAssert(!ComponentVisible[fcQueueView]);
  9201. }
  9202. else
  9203. {
  9204. ComponentVisible[Component] = false;
  9205. }
  9206. }
  9207. }
  9208. break;
  9209. case WM_COPYDATA:
  9210. WMCopyData(*M);
  9211. break;
  9212. case WM_MANAGES_CAPTION:
  9213. // caption managed in TTerminalManager::UpdateAppTitle()
  9214. M->Result = 1;
  9215. break;
  9216. case WM_WANTS_MOUSEWHEEL:
  9217. M->Result = 1;
  9218. break;
  9219. case CM_SHOWINGCHANGED:
  9220. CMShowingChanged(*M);
  9221. break;
  9222. case WM_CLOSE:
  9223. WMClose(*M);
  9224. break;
  9225. case WM_DPICHANGED:
  9226. WMDpiChanged(*M);
  9227. break;
  9228. case CM_DPICHANGED:
  9229. CMDpiChanged(*M);
  9230. break;
  9231. case WM_SETTINGCHANGE:
  9232. WMSettingChange(*M);
  9233. break;
  9234. case CM_DIALOGKEY:
  9235. CMDialogKey(*reinterpret_cast<TWMKeyDown *>(M));
  9236. break;
  9237. case WM_PASTE_FILES:
  9238. PasteFiles();
  9239. break;
  9240. case WM_IS_HIDDEN:
  9241. // To treat master password prompt as a main window while showing it when workspace is loading while
  9242. // the exporer is showing up (Visible is true already)
  9243. M->Result = !FShowing;
  9244. break;
  9245. case WM_SHOWWINDOW:
  9246. if (M->WParam)
  9247. {
  9248. AddStartupSequence(L"J");
  9249. }
  9250. TForm::Dispatch(Message);
  9251. break;
  9252. default:
  9253. TForm::Dispatch(Message);
  9254. break;
  9255. }
  9256. }
  9257. //---------------------------------------------------------------------------
  9258. void __fastcall TCustomScpExplorerForm::UpdateImages()
  9259. {
  9260. // noop
  9261. }
  9262. //---------------------------------------------------------------------------
  9263. void __fastcall TCustomScpExplorerForm::CMDpiChanged(TMessage & Message)
  9264. {
  9265. TForm::Dispatch(&Message);
  9266. FSessionColors->Clear();
  9267. AddFixedSessionImages();
  9268. RegenerateSessionColorsImageList(FSessionColors, FSessionColorMaskImageIndex);
  9269. UpdateImages();
  9270. }
  9271. //---------------------------------------------------------------------------
  9272. void __fastcall TCustomScpExplorerForm::WMDpiChanged(TMessage & Message)
  9273. {
  9274. TForm::Dispatch(&Message);
  9275. Configuration->Usage->Inc(L"PixelsPerInchChanged");
  9276. UpdatePixelsPerInchMainWindowCounter();
  9277. }
  9278. //---------------------------------------------------------------------------
  9279. void __fastcall TCustomScpExplorerForm::WMClose(TMessage & Message)
  9280. {
  9281. AppLog(L"Main window WM_CLOSE");
  9282. // Cannot close window while we are busy.
  9283. // We cannot test this in FormCloseQuery as that is called also from
  9284. // Close(), which is called by CloseApplicationAction. So we can be busy
  9285. // there already even, when it is legitimate to close the application.
  9286. // Possibly a better place to handle this would be WMSysCommand.
  9287. if (NonVisualDataModule->Busy)
  9288. {
  9289. Message.Result = 1;
  9290. }
  9291. else
  9292. {
  9293. TForm::Dispatch(&Message);
  9294. }
  9295. }
  9296. //---------------------------------------------------------------------------
  9297. void __fastcall TCustomScpExplorerForm::CMShowingChanged(TMessage & Message)
  9298. {
  9299. if (!FShowingChanged)
  9300. {
  9301. AddStartupSequence(L"D");
  9302. FShowingChanged = true;
  9303. }
  9304. // Ignoring implicit local-local browser of Commander, opened from DoShow
  9305. bool NoSession = (ManagedSession == NULL);
  9306. // This calls TCustomScpExplorerForm::DoShow
  9307. TForm::Dispatch(&Message);
  9308. // Now the window is visible (TForm::Dispatch is what usually takes long, like when loading a "local" network directory)
  9309. if (Showing && !FStarted)
  9310. {
  9311. FStarted = true;
  9312. InterfaceStarted();
  9313. }
  9314. if (Showing && NoSession)
  9315. {
  9316. // When we are starting minimized (i.e. from an installer),
  9317. // postpone showing Login dialog until we get restored.
  9318. // Otherwise the Login dialog (and Authentication window) show restored
  9319. // over invisible (minimized) main window.
  9320. if (WindowState == wsMinimized)
  9321. {
  9322. FNeedSession = true;
  9323. }
  9324. else
  9325. {
  9326. // This happens before application ever goes idle, so the toolbars would
  9327. // stay enabled (initial state) until the Login dialog is dismissed.
  9328. UpdateActions();
  9329. NonVisualDataModule->StartBusy();
  9330. try
  9331. {
  9332. // Need to process WM_ACTIVATEAPP before showing the Login dialog,
  9333. // otherwise the dialog does not receive focus.
  9334. // With Commander interface the ProcessMessages is called already
  9335. // by TDriveView, but with Explorer interface, we need to call it explicily
  9336. Application->ProcessMessages();
  9337. // do not reload sessions, they have been loaded just now (optimization)
  9338. NeedSession(true);
  9339. }
  9340. __finally
  9341. {
  9342. NonVisualDataModule->EndBusy();
  9343. }
  9344. }
  9345. }
  9346. }
  9347. //---------------------------------------------------------------------------
  9348. void __fastcall TCustomScpExplorerForm::CenterReconnectToolbar()
  9349. {
  9350. ReconnectToolbar->Left = (ReconnectToolbar->Parent->ClientWidth - ReconnectToolbar->Width) / 2;
  9351. ReconnectToolbar->Top = (ReconnectToolbar->Parent->ClientHeight - ReconnectToolbar->Height) / 2;
  9352. }
  9353. //---------------------------------------------------------------------------
  9354. void __fastcall TCustomScpExplorerForm::FormConstrainedResize(
  9355. TObject * /*Sender*/, int & MinWidth, int & MinHeight, int & MaxWidth,
  9356. int & MaxHeight)
  9357. {
  9358. // workaround for bug in TWinControl.CalcConstraints
  9359. // Check for empty rect (restore from iconic state) is done there only after
  9360. // call to AdjustClientRect, which enlarges the rect (for forms).
  9361. TRect R = GetClientRect();
  9362. // when restoring from iconic state, place no restrictions
  9363. if (IsRectEmpty(R))
  9364. {
  9365. MinWidth = 0;
  9366. MinHeight = 0;
  9367. MaxWidth = 32000;
  9368. MaxHeight = 32000;
  9369. }
  9370. }
  9371. //---------------------------------------------------------------------------
  9372. void __fastcall TCustomScpExplorerForm::GetSpaceAvailable(const UnicodeString Path,
  9373. TSpaceAvailable & ASpaceAvailable, bool & Close)
  9374. {
  9375. // terminal can be already closed (e.g. dropped connection)
  9376. if ((Terminal != NULL) && Terminal->IsCapable[fcCheckingSpaceAvailable])
  9377. {
  9378. Configuration->Usage->Inc(L"SpaceAvailableChecks");
  9379. try
  9380. {
  9381. Terminal->SpaceAvailable(Path, ASpaceAvailable);
  9382. }
  9383. catch(...)
  9384. {
  9385. if (!Terminal->Active)
  9386. {
  9387. Close = true;
  9388. }
  9389. throw;
  9390. }
  9391. }
  9392. }
  9393. //---------------------------------------------------------------------------
  9394. void __fastcall TCustomScpExplorerForm::FileSystemInfo()
  9395. {
  9396. const TSessionInfo & SessionInfo = Terminal->GetSessionInfo();
  9397. const TFileSystemInfo & FileSystemInfo = Terminal->GetFileSystemInfo(true);
  9398. TGetSpaceAvailable OnGetSpaceAvailable = NULL;
  9399. if (Terminal->IsCapable[fcCheckingSpaceAvailable])
  9400. {
  9401. OnGetSpaceAvailable = GetSpaceAvailable;
  9402. }
  9403. DoFileSystemInfoDialog(SessionInfo, FileSystemInfo, Terminal->CurrentDirectory,
  9404. OnGetSpaceAvailable);
  9405. }
  9406. //---------------------------------------------------------------------------
  9407. TSessionData * __fastcall TCustomScpExplorerForm::SessionDataForCode()
  9408. {
  9409. std::unique_ptr<TSessionData> Data(CloneCurrentSessionData());
  9410. Terminal->FillSessionDataForCode(Data.get());
  9411. return Data.release();
  9412. }
  9413. //---------------------------------------------------------------------------
  9414. void __fastcall TCustomScpExplorerForm::GenerateUrl(TStrings * Paths)
  9415. {
  9416. std::unique_ptr<TSessionData> Data(SessionDataForCode());
  9417. DoGenerateUrlDialog(Data.get(), Paths);
  9418. }
  9419. //---------------------------------------------------------------------------
  9420. void __fastcall TCustomScpExplorerForm::SessionGenerateUrl()
  9421. {
  9422. GenerateUrl(NULL);
  9423. }
  9424. //---------------------------------------------------------------------------
  9425. void __fastcall TCustomScpExplorerForm::FileGenerateUrl()
  9426. {
  9427. std::unique_ptr<TStrings> Paths(new TStringList());
  9428. DirView(osCurrent)->CreateFileList(false, true, Paths.get());
  9429. GenerateUrl(Paths.get());
  9430. }
  9431. //---------------------------------------------------------------------------
  9432. void __fastcall TCustomScpExplorerForm::UpdateSessionColor(TColor value)
  9433. {
  9434. FSessionColor = value;
  9435. TColor C = (value != 0 ? value : Vcl::Graphics::clNone);
  9436. TTBXColorItem * ColorItem = dynamic_cast<TTBXColorItem *>(
  9437. static_cast<TObject *>(GetComponent(fcColorMenu)));
  9438. DebugAssert(ColorItem != NULL);
  9439. ColorItem->Color = C;
  9440. NonVisualDataModule->ColorMenuItem->Color = C;
  9441. UpdateControls();
  9442. }
  9443. //---------------------------------------------------------------------------
  9444. void __fastcall TCustomScpExplorerForm::SetSessionColor(TColor value)
  9445. {
  9446. if (value != FSessionColor)
  9447. {
  9448. UpdateSessionColor(value);
  9449. // Is null when called from LastTerminalClosed
  9450. if (Terminal != NULL)
  9451. {
  9452. SessionsPageControl->ActivePage->ImageIndex = AddSessionColor(value);
  9453. }
  9454. }
  9455. }
  9456. //---------------------------------------------------------------------------
  9457. bool __fastcall TCustomScpExplorerForm::CancelNote(bool Force)
  9458. {
  9459. bool Result = FNoteTimer->Enabled;
  9460. if (Result)
  9461. {
  9462. // cannot cancel note too early
  9463. bool NotEarly =
  9464. (Now() - FNoteShown >
  9465. EncodeTimeVerbose(0, 0, (unsigned short)(WinConfiguration->NotificationsStickTime), 0));
  9466. if (Force || NotEarly)
  9467. {
  9468. FNoteTimer->Enabled = false;
  9469. FNote = L"";
  9470. SAFE_DESTROY(FNoteData);
  9471. FOnNoteClick = NULL;
  9472. FNoteHints = FNotes->Text;
  9473. FNoteHints.Delete(FNoteHints.Length() - 1, 2);
  9474. UpdateStatusBar();
  9475. }
  9476. }
  9477. return Result;
  9478. }
  9479. //---------------------------------------------------------------------------
  9480. void __fastcall TCustomScpExplorerForm::NoteTimer(TObject * /*Sender*/)
  9481. {
  9482. DebugAssert(FNoteTimer->Enabled);
  9483. CancelNote(true);
  9484. }
  9485. //---------------------------------------------------------------------------
  9486. void __fastcall TCustomScpExplorerForm::UpdateNoteHints()
  9487. {
  9488. FNoteHints = FNotes->Text.TrimRight();
  9489. }
  9490. //---------------------------------------------------------------------------
  9491. void __fastcall TCustomScpExplorerForm::AddNote(UnicodeString Note, bool UpdateNow)
  9492. {
  9493. int P = Note.Pos(L"\n");
  9494. if (P > 0)
  9495. {
  9496. Note.SetLength(P - 1);
  9497. }
  9498. FNotes->Add(FORMAT(L"[%s] %s",
  9499. (FormatDateTime(Configuration->TimeFormat, Now()), Note)));
  9500. while (FNotes->Count > 10)
  9501. {
  9502. FNotes->Delete(0);
  9503. }
  9504. if (UpdateNow)
  9505. {
  9506. UpdateNoteHints();
  9507. UpdateStatusBar();
  9508. }
  9509. }
  9510. //---------------------------------------------------------------------------
  9511. void __fastcall TCustomScpExplorerForm::PostNote(UnicodeString Note,
  9512. unsigned int Seconds, TNotifyEvent OnNoteClick, TObject * NoteData)
  9513. {
  9514. int P = Note.Pos(L"\n");
  9515. if (P > 0)
  9516. {
  9517. Note.SetLength(P - 1);
  9518. }
  9519. UpdateNoteHints();
  9520. FNote = Note;
  9521. SAFE_DESTROY(FNoteData);
  9522. FNoteData = NoteData;
  9523. FOnNoteClick = OnNoteClick;
  9524. AddNote(Note, false);
  9525. UpdateStatusBar();
  9526. FNoteShown = Now();
  9527. FNoteTimer->Enabled = false;
  9528. if (Seconds == 0)
  9529. {
  9530. Seconds = WinConfiguration->NotificationsTimeout;
  9531. }
  9532. FNoteTimer->Interval = Seconds * MSecsPerSec;
  9533. FNoteTimer->Enabled = true;
  9534. }
  9535. //---------------------------------------------------------------------------
  9536. void __fastcall TCustomScpExplorerForm::ReadDirectoryCancelled()
  9537. {
  9538. PostNote(LoadStr(DIRECTORY_READING_CANCELLED), 0, NULL, NULL);
  9539. }
  9540. //---------------------------------------------------------------------------
  9541. void __fastcall TCustomScpExplorerForm::SynchronizeBrowsingChanged()
  9542. {
  9543. if (NonVisualDataModule->SynchronizeBrowsingAction2->Checked)
  9544. {
  9545. Configuration->Usage->Inc(L"SynchronizeBrowsingEnabled");
  9546. }
  9547. PostNote(FORMAT(LoadStrPart(SYNC_DIR_BROWSE_TOGGLE, 1),
  9548. (LoadStrPart(SYNC_DIR_BROWSE_TOGGLE,
  9549. (NonVisualDataModule->SynchronizeBrowsingAction2->Checked ? 2 : 3)))),
  9550. 0, NULL, NULL);
  9551. }
  9552. //---------------------------------------------------------------------------
  9553. void __fastcall TCustomScpExplorerForm::ToggleShowHiddenFiles()
  9554. {
  9555. bool NewShowHiddenFiles = !WinConfiguration->ShowHiddenFiles;
  9556. UnicodeString Note =
  9557. FORMAT(LoadStrPart(SHOW_HIDDEN_FILES_TOGGLE, 1), (LoadStrPart(SHOW_HIDDEN_FILES_TOGGLE, (NewShowHiddenFiles ? 2 : 3))));
  9558. PostNote(Note, 0, NULL, NULL);
  9559. GetComponent(fcStatusBar)->Repaint(); // toggling ShowHiddenFiles takes time, force repaint beforehand
  9560. WinConfiguration->ShowHiddenFiles = NewShowHiddenFiles;
  9561. }
  9562. //---------------------------------------------------------------------------
  9563. void __fastcall TCustomScpExplorerForm::SetFormatSizeBytes(TFormatBytesStyle Style)
  9564. {
  9565. WinConfiguration->FormatSizeBytes = Style;
  9566. }
  9567. //---------------------------------------------------------------------------
  9568. void __fastcall TCustomScpExplorerForm::ToggleAutoReadDirectoryAfterOp()
  9569. {
  9570. Configuration->AutoReadDirectoryAfterOp = !Configuration->AutoReadDirectoryAfterOp;
  9571. PostNote(FORMAT(LoadStrPart(AUTO_READ_DIRECTORY_TOGGLE, 1),
  9572. (LoadStrPart(AUTO_READ_DIRECTORY_TOGGLE,
  9573. (Configuration->AutoReadDirectoryAfterOp ? 2 : 3)))), 0, NULL, NULL);
  9574. }
  9575. //---------------------------------------------------------------------------
  9576. void __fastcall TCustomScpExplorerForm::StatusBarPanelDblClick(
  9577. TTBXCustomStatusBar * Sender, TTBXStatusPanel * Panel)
  9578. {
  9579. if (Panel->Index == 0)
  9580. {
  9581. if (FOnNoteClick != NULL)
  9582. {
  9583. // prevent the user data from being freed by possible call
  9584. // to CancelNote or PostNote during call to OnNoteClick
  9585. std::unique_ptr<TObject> NoteData(FNoteData);
  9586. TNotifyEvent OnNoteClick = FOnNoteClick;
  9587. FNoteData = NULL;
  9588. // need to cancel the note as we are going to delete its user data
  9589. CancelNote(true);
  9590. OnNoteClick(NoteData.get());
  9591. }
  9592. }
  9593. if (HasActiveTerminal() &&
  9594. (Panel->Index >= Sender->Panels->Count - SessionPanelCount))
  9595. {
  9596. FileSystemInfo();
  9597. }
  9598. }
  9599. //---------------------------------------------------------------------------
  9600. void __fastcall TCustomScpExplorerForm::LockWindow(bool Force)
  9601. {
  9602. // workaround:
  9603. // 1) for unknown reason, disabling window, while minimized,
  9604. // prevents it from restoring, even if it was enabled again meanwhile
  9605. // 2) when disabling the main window, while another has focus
  9606. // minimize is no longer possible
  9607. // ("keep up to date" dialog - though does not seem to be the case with "find file" -
  9608. // so it has probably something to do with the semi-modalness of the "keep up to date" dialog)
  9609. // Shouldn't we use IsApplicationMinimized() here?
  9610. if ((FLockSuspendLevel == 0) && !IsIconic(Application->Handle) && (Force || (Screen->ActiveForm == this)))
  9611. {
  9612. Enabled = false;
  9613. }
  9614. FLockLevel++;
  9615. }
  9616. //---------------------------------------------------------------------------
  9617. void __fastcall TCustomScpExplorerForm::UnlockWindow()
  9618. {
  9619. DebugAssert(FLockLevel > 0);
  9620. FLockLevel--;
  9621. if (FLockLevel == 0)
  9622. {
  9623. DebugAssert(FLockSuspendLevel == 0);
  9624. Enabled = true;
  9625. // VCL_COPY (TCustomForm.SetWindowFocus)
  9626. if (Active && (ActiveControl != NULL))
  9627. {
  9628. ::SetFocus(ActiveControl->Handle);
  9629. if (GetFocus() == ActiveControl->Handle)
  9630. {
  9631. ActiveControl->Perform(CM_UIACTIVATE, 0, 0);
  9632. }
  9633. }
  9634. }
  9635. }
  9636. //---------------------------------------------------------------------------
  9637. void __fastcall TCustomScpExplorerForm::SuspendWindowLock()
  9638. {
  9639. // We need to make sure that window is enabled when the last modal window closes
  9640. // otherwise focus is not restored correctly.
  9641. // So we re-enable the window when modal window opens and
  9642. // disable it back after is closes
  9643. if (FLockLevel > 0)
  9644. {
  9645. // while we have nesting counter, we know that we never be called
  9646. // recursivelly as Application->OnModalBegin is called only
  9647. // for the top-level modal window
  9648. if (DebugAlwaysTrue(FLockSuspendLevel == 0))
  9649. {
  9650. // won't be disabled when conditions in LockWindow() were not satisfied
  9651. FDisabledOnLockSuspend = !Enabled;
  9652. // When minimized to tray (or actually when set to SW_HIDE),
  9653. // setting Enabled makes the window focusable even when there's
  9654. // modal window over it
  9655. if (!FTrayIcon->Visible)
  9656. {
  9657. Enabled = true;
  9658. }
  9659. }
  9660. FLockSuspendLevel++;
  9661. }
  9662. }
  9663. //---------------------------------------------------------------------------
  9664. void __fastcall TCustomScpExplorerForm::ResumeWindowLock()
  9665. {
  9666. if (FLockSuspendLevel > 0)
  9667. {
  9668. DebugAssert(FLockLevel > 0);
  9669. FLockSuspendLevel--;
  9670. // see comment in SuspendWindowLock
  9671. if (DebugAlwaysTrue(FLockSuspendLevel == 0))
  9672. {
  9673. // Note that window can be enabled here, when we were minized to tray when
  9674. // was SuspendWindowLock() called.
  9675. // We should possibly do the same check as in LockWindow(),
  9676. // if it is ever possible that the consitions change between
  9677. // SuspendWindowLock() and ResumeWindowLock()
  9678. Enabled = !FDisabledOnLockSuspend;
  9679. }
  9680. }
  9681. }
  9682. //---------------------------------------------------------------------------
  9683. void __fastcall TCustomScpExplorerForm::UpdateRemotePathComboBox(bool TextOnly)
  9684. {
  9685. TTBXComboBoxItem * RemotePathComboBox =
  9686. reinterpret_cast<TTBXComboBoxItem *>(GetComponent(fcRemotePathComboBox));
  9687. if (!TextOnly)
  9688. {
  9689. TStrings * Items = RemotePathComboBox->Strings;
  9690. Items->BeginUpdate();
  9691. try
  9692. {
  9693. Items->Clear();
  9694. if ((Terminal != NULL) && !RemoteDirView->Path.IsEmpty())
  9695. {
  9696. UnicodeString APath = UnixExcludeTrailingBackslash(RemoteDirView->Path);
  9697. while (!IsUnixRootPath(APath))
  9698. {
  9699. int P = APath.LastDelimiter(L'/');
  9700. DebugAssert(P >= 0);
  9701. Items->Insert(0, APath.SubString(P + 1, APath.Length() - P));
  9702. APath.SetLength(P - 1);
  9703. }
  9704. Items->Insert(0, Customunixdirview_SUnixDefaultRootName);
  9705. }
  9706. }
  9707. __finally
  9708. {
  9709. Items->EndUpdate();
  9710. }
  9711. }
  9712. RemotePathComboBox->ItemIndex = RemotePathComboBox->Strings->Count - 1;
  9713. // Setting ItemIndex to -1 does not reset its text
  9714. if (RemotePathComboBox->Strings->Count == 0)
  9715. {
  9716. RemotePathComboBox->Text = L"";
  9717. }
  9718. }
  9719. //---------------------------------------------------------------------------
  9720. void __fastcall TCustomScpExplorerForm::DoRemotePathComboBoxAdjustImageIndex(
  9721. TTBXComboBoxItem * Sender, const UnicodeString DebugUsedArg(AText), int AIndex, int & ImageIndex)
  9722. {
  9723. if (AIndex < 0)
  9724. {
  9725. AIndex = Sender->ItemIndex;
  9726. }
  9727. ImageIndex = (AIndex < Sender->Strings->Count - 1 ? StdDirIcon : StdDirSelIcon);
  9728. }
  9729. //---------------------------------------------------------------------------
  9730. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxAdjustImageIndex(
  9731. TTBXComboBoxItem * Sender, const UnicodeString AText, int AIndex,
  9732. int & ImageIndex)
  9733. {
  9734. DoRemotePathComboBoxAdjustImageIndex(Sender, AText, AIndex, ImageIndex);
  9735. }
  9736. //---------------------------------------------------------------------------
  9737. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxDrawItem(
  9738. TTBXCustomList * /*Sender*/, TCanvas * /*ACanvas*/, TRect & ARect, int AIndex,
  9739. int /*AHoverIndex*/, bool & /*DrawDefault*/)
  9740. {
  9741. if (!IsLocalBrowserMode())
  9742. {
  9743. ARect.Left += (10 * AIndex);
  9744. }
  9745. }
  9746. //---------------------------------------------------------------------------
  9747. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxMeasureWidth(
  9748. TTBXCustomList * /*Sender*/, TCanvas * /*ACanvas*/, int AIndex, int &AWidth)
  9749. {
  9750. AWidth += (10 * AIndex);
  9751. }
  9752. //---------------------------------------------------------------------------
  9753. void __fastcall TCustomScpExplorerForm::DoRemotePathComboBoxItemClick(TObject * Sender)
  9754. {
  9755. TTBXComboBoxItem * RemotePathComboBox = dynamic_cast<TTBXComboBoxItem*>(Sender);
  9756. UnicodeString APath = UnixExcludeTrailingBackslash(RemoteDirView->Path);
  9757. int Index = RemotePathComboBox->ItemIndex;
  9758. while (Index < RemotePathComboBox->Strings->Count - 1)
  9759. {
  9760. APath = UnixExtractFileDir(APath);
  9761. Index++;
  9762. }
  9763. // VanDyke style paths
  9764. if (APath.IsEmpty())
  9765. {
  9766. DebugAssert(RemotePathComboBox->ItemIndex == 0);
  9767. APath = ROOTDIRECTORY;
  9768. }
  9769. if (RemoteDirView->Path != APath)
  9770. {
  9771. RemoteDirView->Path = APath;
  9772. }
  9773. }
  9774. //---------------------------------------------------------------------------
  9775. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxItemClick(TObject * Sender)
  9776. {
  9777. DoRemotePathComboBoxItemClick(Sender);
  9778. }
  9779. //---------------------------------------------------------------------------
  9780. void __fastcall TCustomScpExplorerForm::DoRemotePathComboBoxCancel(TObject * Sender)
  9781. {
  9782. DebugAssert(Sender == GetComponent(fcRemotePathComboBox));
  9783. DebugUsedParam(Sender);
  9784. UpdateRemotePathComboBox(true);
  9785. }
  9786. //---------------------------------------------------------------------------
  9787. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxCancel(TObject * Sender)
  9788. {
  9789. DoRemotePathComboBoxCancel(Sender);
  9790. }
  9791. //---------------------------------------------------------------------------
  9792. void __fastcall TCustomScpExplorerForm::DirViewEditing(
  9793. TObject * Sender, TListItem * Item, bool & /*AllowEdit*/)
  9794. {
  9795. ResetIncrementalSearch();
  9796. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  9797. DebugAssert(DirView != NULL);
  9798. if (!WinConfiguration->RenameWholeName && !DirView->ItemIsDirectory(Item))
  9799. {
  9800. HWND Edit = ListView_GetEditControl(DirView->Handle);
  9801. // OnEditing is called also from TCustomListView::CanEdit
  9802. if (Edit != NULL)
  9803. {
  9804. EditSelectBaseName(Edit);
  9805. }
  9806. }
  9807. }
  9808. //---------------------------------------------------------------------------
  9809. //---------------------------------------------------------------------------
  9810. TDragDropFilesEx * __fastcall TCustomScpExplorerForm::CreateDragDropFilesEx()
  9811. {
  9812. TDragDropFilesEx * Result = new TDragDropFilesEx(this);
  9813. Result->AutoDetectDnD = false;
  9814. Result->NeedValid = TFileExMustDnDSet() << nvFilename;
  9815. Result->RenderDataOn = rdoEnterAndDropSync;
  9816. Result->TargetEffects = TDropEffectSet() << deCopy << deMove;
  9817. return Result;
  9818. }
  9819. //---------------------------------------------------------------------------
  9820. void __fastcall TCustomScpExplorerForm::UpdateDarkMode()
  9821. {
  9822. if (IsWin10Build(19041))
  9823. {
  9824. FImmersiveDarkMode = WinConfiguration->UseDarkTheme();
  9825. BOOL DarkMode = FImmersiveDarkMode ? TRUE : FALSE;
  9826. const int DWMWA_USE_IMMERSIVE_DARK_MODE = 20;
  9827. DwmSetWindowAttribute(Handle, DWMWA_USE_IMMERSIVE_DARK_MODE, &DarkMode, sizeof(DarkMode));
  9828. }
  9829. }
  9830. //---------------------------------------------------------------------------
  9831. void __fastcall TCustomScpExplorerForm::CreateWnd()
  9832. {
  9833. TForm::CreateWnd();
  9834. // win32-darkmode calls AllowDarkModeForWindow(this, true) here, but it does not seem to have any effect
  9835. UpdateDarkMode();
  9836. if (FSessionsDragDropFilesEx == NULL)
  9837. {
  9838. FSessionsDragDropFilesEx = CreateDragDropFilesEx();
  9839. FSessionsDragDropFilesEx->OnDragOver = SessionsDDDragOver;
  9840. FSessionsDragDropFilesEx->OnProcessDropped = SessionsDDProcessDropped;
  9841. FSessionsDragDropFilesEx->OnDragEnter = SessionsDDDragEnter;
  9842. FSessionsDragDropFilesEx->OnDragLeave = SessionsDDDragLeave;
  9843. }
  9844. if (FQueueDragDropFilesEx == NULL)
  9845. {
  9846. FQueueDragDropFilesEx = CreateDragDropFilesEx();
  9847. // No need to set OnDragOver as we do not have any restrictions
  9848. FQueueDragDropFilesEx->OnProcessDropped = QueueDDProcessDropped;
  9849. FQueueDragDropFilesEx->OnDragEnter = QueueDDDragEnter;
  9850. FQueueDragDropFilesEx->OnDragLeave = QueueDDDragLeave;
  9851. }
  9852. }
  9853. //---------------------------------------------------------------------------
  9854. void __fastcall TCustomScpExplorerForm::DestroyWnd()
  9855. {
  9856. TForm::DestroyWnd();
  9857. FSessionsDragDropFilesEx->DragDropControl = NULL;
  9858. FQueueDragDropFilesEx->DragDropControl = NULL;
  9859. }
  9860. //---------------------------------------------------------------------------
  9861. void __fastcall TCustomScpExplorerForm::FormShow(TObject * /*Sender*/)
  9862. {
  9863. SideEnter(FCurrentSide);
  9864. FEverShown = true;
  9865. }
  9866. //---------------------------------------------------------------------------
  9867. void __fastcall TCustomScpExplorerForm::DoFindFiles(
  9868. TTerminal * ATerminal, UnicodeString Directory, const TFileMasks & FileMask,
  9869. TFileFoundEvent OnFileFound, TFindingFileEvent OnFindingFile)
  9870. {
  9871. if (!NonVisualDataModule->Busy)
  9872. {
  9873. TTerminalManager::Instance()->ActiveSession = DebugNotNull(dynamic_cast<TManagedTerminal *>(ATerminal));
  9874. Configuration->Usage->Inc(L"FileFinds");
  9875. LockWindow(true);
  9876. NonVisualDataModule->StartBusy();
  9877. WinConfiguration->LockedInterface = true;
  9878. try
  9879. {
  9880. Terminal->FilesFind(Directory, FileMask, OnFileFound, OnFindingFile);
  9881. }
  9882. __finally
  9883. {
  9884. WinConfiguration->LockedInterface = false;
  9885. NonVisualDataModule->EndBusy();
  9886. UnlockWindow();
  9887. }
  9888. }
  9889. }
  9890. //---------------------------------------------------------------------------
  9891. void __fastcall TCustomScpExplorerForm::DoFocusRemotePath(TTerminal * ATerminal, const UnicodeString & Path)
  9892. {
  9893. if (!NonVisualDataModule->Busy)
  9894. {
  9895. TTerminalManager::Instance()->ActiveSession = DebugNotNull(dynamic_cast<TManagedTerminal *>(ATerminal));
  9896. SetFocus();
  9897. RemoteDirView->Path = UnixExtractFilePath(Path);
  9898. UnicodeString FileName = UnixExtractFileName(Path);
  9899. TListItem * Item = RemoteDirView->FindFileItem(FileName);
  9900. // If not found, it can because we have loaded directory from cache and the file is new, try again after reload.
  9901. // (tbd: we should not even try this, if we have not loaded the directory from the cache)
  9902. if (Item == NULL)
  9903. {
  9904. ReloadDirectory(osRemote);
  9905. Item = RemoteDirView->FindFileItem(FileName);
  9906. }
  9907. if (Item != NULL)
  9908. {
  9909. RemoteDirView->ItemFocused = Item;
  9910. RemoteDirView->ItemFocused->MakeVisible(false);
  9911. RemoteDirView->SetFocus();
  9912. }
  9913. }
  9914. }
  9915. //---------------------------------------------------------------------------
  9916. bool __fastcall TCustomScpExplorerForm::CanOperateOnFoundFiles(TTerminal * ATerminal)
  9917. {
  9918. bool Result = !NonVisualDataModule->Busy;
  9919. if (Result)
  9920. {
  9921. TTerminalManager::Instance()->ActiveSession = DebugNotNull(dynamic_cast<TManagedTerminal *>(ATerminal));
  9922. }
  9923. return Result;
  9924. }
  9925. //---------------------------------------------------------------------------
  9926. void __fastcall TCustomScpExplorerForm::DoOperationOnFoundFiles(
  9927. TFileOperation Operation, TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  9928. {
  9929. if (CanOperateOnFoundFiles(ATerminal))
  9930. {
  9931. TValueRestorer<TFileOperationFinishedEvent> OnFileOperationFinishedRestorer(FOnFileOperationFinished, OnFileOperationFinished);
  9932. ExecuteFileOperation(Operation, osRemote, FileList, false, NULL);
  9933. }
  9934. }
  9935. //---------------------------------------------------------------------------
  9936. void __fastcall TCustomScpExplorerForm::DoDeleteFoundFiles(
  9937. TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  9938. {
  9939. DoOperationOnFoundFiles(foDelete, ATerminal, FileList, OnFileOperationFinished);
  9940. }
  9941. //---------------------------------------------------------------------------
  9942. void __fastcall TCustomScpExplorerForm::DoDownloadFoundFiles(
  9943. TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  9944. {
  9945. DoOperationOnFoundFiles(foCopy, ATerminal, FileList, OnFileOperationFinished);
  9946. }
  9947. //---------------------------------------------------------------------------
  9948. void __fastcall TCustomScpExplorerForm::DoEditFoundFiles(
  9949. TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  9950. {
  9951. if (CanOperateOnFoundFiles(ATerminal))
  9952. {
  9953. for (int Index = 0; Index < FileList->Count; Index++)
  9954. {
  9955. UnicodeString FileName = FileList->Strings[Index];
  9956. TRemoteFile * File = static_cast<TRemoteFile *>(FileList->Objects[Index]);
  9957. ExecuteRemoteFile(FileName, File, efDefaultEditor);
  9958. OnFileOperationFinished(osRemote, FileName, true);
  9959. }
  9960. }
  9961. }
  9962. //---------------------------------------------------------------------------
  9963. void __fastcall TCustomScpExplorerForm::RemoteFindFiles()
  9964. {
  9965. FFileFindTerminal = Terminal;
  9966. ShowFileFindDialog(
  9967. Terminal, RemoteDirView->Path, DoFindFiles, DoFocusRemotePath, DoDeleteFoundFiles,
  9968. DoDownloadFoundFiles, DoEditFoundFiles);
  9969. }
  9970. //---------------------------------------------------------------------------
  9971. void __fastcall TCustomScpExplorerForm::UpdateTaskbarList(ITaskbarList3 * TaskbarList)
  9972. {
  9973. FTaskbarList = TaskbarList;
  9974. }
  9975. //---------------------------------------------------------------------------
  9976. void __fastcall TCustomScpExplorerForm::SessionsPageControlMouseDown(
  9977. TObject * /*Sender*/, TMouseButton Button, TShiftState /*Shift*/, int X, int Y)
  9978. {
  9979. int Index = SessionsPageControl->IndexOfTabAt(X, Y);
  9980. if (Index >= 0)
  9981. {
  9982. if (Button == mbLeft)
  9983. {
  9984. // WORKAROUND: "Mouse down" is raised only after tab is switched.
  9985. // If switching tab (switching session) takes long enough for user
  9986. // to actually release the button, "mouse down" is still raised,
  9987. // but we do not get "mouse up" event, so dragging is not cancelled,
  9988. // prevent that by not beginning dragging in the first place.
  9989. int Key = ((GetSystemMetrics(SM_SWAPBUTTON) == 0) ? VK_LBUTTON : VK_RBUTTON);
  9990. if (IsKeyPressed(Key))
  9991. {
  9992. // when user clicks the "+", we get mouse down only after the session
  9993. // is closed, when new session tab is already on X:Y, so dragging
  9994. // starts, prevent that
  9995. if (MilliSecondsBetween(Now(), FSessionsPageControlNewTabTime) > 500)
  9996. {
  9997. TTerminal * Terminal = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  9998. if (Terminal != NULL)
  9999. {
  10000. SessionsPageControl->BeginDrag(false);
  10001. }
  10002. }
  10003. }
  10004. }
  10005. else if (Button == mbMiddle)
  10006. {
  10007. // ignore middle-click for "New session tab" and the last local-local tab
  10008. TManagedTerminal * Session = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  10009. if ((Session != NULL) && CanCloseSession(Session))
  10010. {
  10011. CloseSessionTab(Index);
  10012. }
  10013. }
  10014. }
  10015. }
  10016. //---------------------------------------------------------------------------
  10017. void __fastcall TCustomScpExplorerForm::SessionsPageControlDragDrop(
  10018. TObject * /*Sender*/, TObject * /*Source*/, int X, int Y)
  10019. {
  10020. int Index = SessionsPageControl->IndexOfTabAt(X, Y);
  10021. // do not allow dropping on the "+" tab
  10022. TTerminal * TargetTerminal = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  10023. if ((TargetTerminal != NULL) &&
  10024. (SessionsPageControl->ActivePage->PageIndex != Index))
  10025. {
  10026. Configuration->Usage->Inc(L"SessionTabMoves");
  10027. // this is almost redundant as we would recreate tabs in SessionListChanged,
  10028. // but we want to actually prevent that to avoid flicker
  10029. SessionsPageControl->ActivePage->PageIndex = Index;
  10030. TTerminal * Terminal = GetSessionTabSession(SessionsPageControl->ActivePage);
  10031. TTerminalManager::Instance()->Move(Terminal, TargetTerminal);
  10032. }
  10033. }
  10034. //---------------------------------------------------------------------------
  10035. void __fastcall TCustomScpExplorerForm::SessionsPageControlDragOver(
  10036. TObject * Sender, TObject * Source, int X, int Y,
  10037. TDragState /*State*/, bool & Accept)
  10038. {
  10039. Accept = (Sender == Source);
  10040. if (Accept)
  10041. {
  10042. int Index = SessionsPageControl->IndexOfTabAt(X, Y);
  10043. TTerminal * Terminal = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  10044. // do not allow dragging to the "+" tab
  10045. Accept = (Terminal != NULL);
  10046. }
  10047. }
  10048. //---------------------------------------------------------------------------
  10049. void __fastcall TCustomScpExplorerForm::SessionsDDDragOver(
  10050. int DebugUsedArg(KeyState), const TPoint & Point, int & Effect, int DebugUsedArg(PreferredEffect))
  10051. {
  10052. int Index = SessionsPageControl->IndexOfTabAt(Point.X, Point.Y);
  10053. if (Index < 0)
  10054. {
  10055. Effect = DROPEFFECT_NONE;
  10056. }
  10057. else
  10058. {
  10059. TManagedTerminal * TargetSession = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  10060. // do not allow dropping on the "+" tab or on disconnected or local-local tabs
  10061. if (!IsActiveTerminal(TargetSession) || DebugAlwaysFalse(TargetSession->LocalBrowser))
  10062. {
  10063. Effect = DROPEFFECT_NONE;
  10064. }
  10065. else if ((TargetSession != Terminal) && (Effect == DROPEFFECT_MOVE))
  10066. {
  10067. Effect = DROPEFFECT_COPY;
  10068. }
  10069. }
  10070. }
  10071. //---------------------------------------------------------------------------
  10072. void __fastcall TCustomScpExplorerForm::SessionsDDProcessDropped(
  10073. TObject * /*Sender*/, int /*KeyState*/, const TPoint & Point, int Effect)
  10074. {
  10075. int Index = SessionsPageControl->IndexOfTabAt(Point.X, Point.Y);
  10076. // do not allow dropping on local-local, disconnected and the "+" tab
  10077. // (though we do not seem to get here in that case anyway, contrary to RemoteFileControlDDTargetDrop)
  10078. TManagedTerminal * TargetTerminal = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  10079. if (DebugAlwaysTrue(IsActiveTerminal(TargetTerminal) && !TargetTerminal->LocalBrowser))
  10080. {
  10081. DebugAssert(!IsFileControl(DropSourceControl, osRemote));
  10082. if (!IsFileControl(DropSourceControl, osRemote))
  10083. {
  10084. TTerminalManager::Instance()->ActiveSession = TargetTerminal;
  10085. RemoteFileControlDragDropFileOperation(SessionsPageControl, Effect,
  10086. // Why don't we use Terminal->CurrentDirectory directly?
  10087. TTerminalManager::Instance()->ActiveTerminal->CurrentDirectory,
  10088. // do not force queue, drag drop
  10089. false, true);
  10090. }
  10091. }
  10092. }
  10093. //---------------------------------------------------------------------------
  10094. void __fastcall TCustomScpExplorerForm::QueueDDProcessDropped(
  10095. TObject * /*Sender*/, int /*KeyState*/, const TPoint & /*Point*/, int Effect)
  10096. {
  10097. // Downloads are handled in RemoteFileControlDDEnd
  10098. if (!IsFileControl(DropSourceControl, osRemote))
  10099. {
  10100. RemoteFileControlDragDropFileOperation(QueueView3, Effect,
  10101. Terminal->CurrentDirectory,
  10102. // force queue, drag drop
  10103. true, true);
  10104. }
  10105. }
  10106. //---------------------------------------------------------------------------
  10107. void __fastcall TCustomScpExplorerForm::FormClose(TObject * /*Sender*/, TCloseAction & /*Action*/)
  10108. {
  10109. FShowing = false;
  10110. // Do not save empty workspace
  10111. if (WinConfiguration->AutoSaveWorkspace && (ManagedSession != NULL))
  10112. {
  10113. std::unique_ptr<TObjectList> DataList(DoCollectWorkspace());
  10114. UnicodeString Name = WorkspaceName();
  10115. DoSaveWorkspace(
  10116. Name, DataList.get(),
  10117. !Configuration->DisablePasswordStoring &&
  10118. WinConfiguration->AutoSaveWorkspacePasswords, false);
  10119. WinConfiguration->LastStoredSession = Name;
  10120. }
  10121. AppLog(L"Closing main window");
  10122. }
  10123. //---------------------------------------------------------------------------
  10124. void __fastcall TCustomScpExplorerForm::RemoteDirViewRead(TObject * /*Sender*/)
  10125. {
  10126. TManagedTerminal * ManagedTerminal =
  10127. dynamic_cast<TManagedTerminal *>(RemoteDirView->Terminal);
  10128. if (ManagedTerminal != NULL)
  10129. {
  10130. ManagedTerminal->DirectoryLoaded = Now();
  10131. }
  10132. }
  10133. //---------------------------------------------------------------------------
  10134. void __fastcall TCustomScpExplorerForm::DirViewSelectItem(TObject * Sender,
  10135. TListItem * /*Item*/, bool /*Selected*/)
  10136. {
  10137. TCustomDirView * DirView = reinterpret_cast<TCustomDirView *>(Sender);
  10138. switch (DirView->LastSelectMethod)
  10139. {
  10140. case smKeyboard:
  10141. Configuration->Usage->Inc(L"KeyboardSelections");
  10142. break;
  10143. case smMouse:
  10144. Configuration->Usage->Inc(L"MouseSelections");
  10145. break;
  10146. }
  10147. }
  10148. //---------------------------------------------------------------------------
  10149. int __fastcall TCustomScpExplorerForm::AddSessionColor(TColor Color)
  10150. {
  10151. if (Color != 0)
  10152. {
  10153. return GetSessionColorImage(FSessionColors, Color, FSessionColorMaskImageIndex);
  10154. }
  10155. else
  10156. {
  10157. return FSessionTabImageIndex;
  10158. }
  10159. }
  10160. //---------------------------------------------------------------------------
  10161. int __fastcall TCustomScpExplorerForm::AddFixedSessionImage(int GlyphsSourceIndex)
  10162. {
  10163. TPngImageCollectionItem * Item =
  10164. GlyphsModule->ExplorerImages->PngImages->Items[GlyphsSourceIndex];
  10165. FSessionColors->AddPng(Item->PngImage);
  10166. return FSessionColors->Count - 1;
  10167. }
  10168. //---------------------------------------------------------------------------
  10169. void __fastcall TCustomScpExplorerForm::AddFixedSessionImages()
  10170. {
  10171. FSessionColors->SetSize(GlyphsModule->ExplorerImages->Width, GlyphsModule->ExplorerImages->Height);
  10172. FNewRemoteTabTabImageIndex = AddFixedSessionImage(NonVisualDataModule->NewRemoteTabAction->ImageIndex);
  10173. FNewLocalTabTabImageIndex = AddFixedSessionImage(NonVisualDataModule->NewLocalTabAction->ImageIndex);
  10174. FSessionTabImageIndex = AddFixedSessionImage(SiteImageIndex);
  10175. FSessionColorMaskImageIndex = AddFixedSessionImage(SiteColorMaskImageIndex);
  10176. FLocalBrowserTabImageIndex = AddFixedSessionImage(LocalBrowserImageIndex);
  10177. }
  10178. //---------------------------------------------------------------------------
  10179. int TCustomScpExplorerForm::GetNewTabActionImageIndex()
  10180. {
  10181. return NonVisualDataModule->NewRemoteTabAction->ImageIndex;
  10182. }
  10183. //---------------------------------------------------------------------------
  10184. int TCustomScpExplorerForm::GetNewTabTabImageIndex(TOperationSide Side)
  10185. {
  10186. switch (Side)
  10187. {
  10188. case osLocal:
  10189. return FNewLocalTabTabImageIndex;
  10190. case osRemote:
  10191. case osCurrent:
  10192. return FNewRemoteTabTabImageIndex;
  10193. default:
  10194. DebugFail();
  10195. return -1;
  10196. }
  10197. }
  10198. //---------------------------------------------------------------------------
  10199. void __fastcall TCustomScpExplorerForm::CollectItemsWithTextDisplayMode(TWinControl * Control)
  10200. {
  10201. for (int Index = 0; Index < Control->ControlCount; Index++)
  10202. {
  10203. TControl * AControl = Control->Controls[Index];
  10204. TWinControl * WinControl = dynamic_cast<TWinControl *>(AControl);
  10205. if (WinControl != NULL)
  10206. {
  10207. CollectItemsWithTextDisplayMode(WinControl);
  10208. }
  10209. TTBCustomToolbar * Toolbar = dynamic_cast<TTBCustomToolbar *>(AControl);
  10210. if (Toolbar != NULL)
  10211. {
  10212. // we care for top-level items only
  10213. TTBCustomItem * Items = Toolbar->Items;
  10214. for (int ItemIndex = 0; ItemIndex < Items->Count; ItemIndex++)
  10215. {
  10216. TTBCustomItem * Item = Items->Items[ItemIndex];
  10217. if (((Item->DisplayMode == nbdmImageAndText) ||
  10218. (dynamic_cast<TTBXLabelItem *>(Item) != NULL)) &&
  10219. EligibleForImageDisplayMode(Item))
  10220. {
  10221. FItemsWithTextDisplayMode.insert(Item);
  10222. }
  10223. }
  10224. }
  10225. }
  10226. }
  10227. //---------------------------------------------------------------------------
  10228. bool __fastcall TCustomScpExplorerForm::EligibleForImageDisplayMode(TTBCustomItem * /*Item*/)
  10229. {
  10230. return true;
  10231. }
  10232. //---------------------------------------------------------------------------
  10233. bool __fastcall TCustomScpExplorerForm::UpdateToolbarDisplayMode()
  10234. {
  10235. bool SelectiveToolbarText = WinConfiguration->SelectiveToolbarText;
  10236. TTBItemDisplayMode DisplayMode = (SelectiveToolbarText ? nbdmImageAndText : nbdmDefault);
  10237. typedef std::set<TTBCustomItem *> TItemsWithTextDisplayMode;
  10238. TItemsWithTextDisplayMode::iterator i = FItemsWithTextDisplayMode.begin();
  10239. bool Result = true;
  10240. while (Result && (i != FItemsWithTextDisplayMode.end()))
  10241. {
  10242. TTBCustomItem * Item = *i;
  10243. TTBXLabelItem * Label = dynamic_cast<TTBXLabelItem *>(Item);
  10244. if (Label != NULL)
  10245. {
  10246. // optimization
  10247. if (Label->Visible == SelectiveToolbarText)
  10248. {
  10249. Result = false;
  10250. }
  10251. else
  10252. {
  10253. Label->Visible = SelectiveToolbarText;
  10254. }
  10255. }
  10256. else
  10257. {
  10258. // optimization
  10259. if (Item->DisplayMode == DisplayMode)
  10260. {
  10261. Result = false;
  10262. }
  10263. else
  10264. {
  10265. Item->DisplayMode = DisplayMode;
  10266. }
  10267. }
  10268. ++i;
  10269. }
  10270. UpdateNewTabTab();
  10271. return Result;
  10272. }
  10273. //---------------------------------------------------------------------------
  10274. void __fastcall TCustomScpExplorerForm::DisplaySystemContextMenu()
  10275. {
  10276. DebugFail();
  10277. }
  10278. //---------------------------------------------------------------------------
  10279. bool __fastcall TCustomScpExplorerForm::IsBusy()
  10280. {
  10281. // Among other, a lock level is non zero, while directory is being loaded,
  10282. // even when it happens as a result of dirview navigation,
  10283. // i.e. when TNonVisualDataModule is NOT FBusy.
  10284. // That's why the TNonVisualDataModule::GetBusy calls this method.
  10285. // Among other this prevents a panel auto update to occur while
  10286. // directory is changing.
  10287. return (FLockLevel > 0) || DirView(osCurrent)->IsEditing();
  10288. }
  10289. //---------------------------------------------------------------------------
  10290. Boolean __fastcall TCustomScpExplorerForm::AllowedAction(TAction * Action, TActionAllowed Allowed)
  10291. {
  10292. // While the window is disabled, we still seem to process menu shortcuts at least,
  10293. // so stop it at least here.
  10294. // See also TCustomScpExplorerForm::RemoteDirViewBusy
  10295. bool Result;
  10296. if (Allowed == aaUpdate)
  10297. {
  10298. Result = true;
  10299. }
  10300. else
  10301. {
  10302. Result = !NonVisualDataModule->Busy;
  10303. if (Result && (Allowed == aaShortCut))
  10304. {
  10305. // Otherwise F5 for local-local will be swallowed by disabled local<>remote actions in TCustomScpExplorerForm::KeyDown
  10306. Action->Update();
  10307. Result = Action->Enabled;
  10308. }
  10309. }
  10310. return Result;
  10311. }
  10312. //---------------------------------------------------------------------------
  10313. void __fastcall TCustomScpExplorerForm::EditMenuItemPopup(TTBCustomItem * Sender, bool FromLink)
  10314. {
  10315. NonVisualDataModule->EditMenuItemPopup(Sender, FromLink);
  10316. }
  10317. //---------------------------------------------------------------------------
  10318. void __fastcall TCustomScpExplorerForm::DirViewBusy(TObject * /*Sender*/, int Busy, bool & State)
  10319. {
  10320. // This is somewhat redundant to LockWindow() call from
  10321. // TTerminalManager::TerminalReadDirectoryProgress.
  10322. // But disabling window is known not to block keyboard shorcuts
  10323. // (see TCustomScpExplorerForm::AllowedAction), this hopefully works.
  10324. if (Busy > 0)
  10325. {
  10326. if (NonVisualDataModule->Busy)
  10327. {
  10328. State = false;
  10329. }
  10330. else
  10331. {
  10332. NonVisualDataModule->StartBusy();
  10333. // Will be countered for TCustomDirView.DoExecute in DoDirViewExecFile
  10334. LockWindow();
  10335. }
  10336. }
  10337. else if (Busy < 0)
  10338. {
  10339. UnlockWindow();
  10340. NonVisualDataModule->EndBusy();
  10341. }
  10342. else
  10343. {
  10344. State = NonVisualDataModule->Busy;
  10345. }
  10346. }
  10347. //---------------------------------------------------------------------------
  10348. void __fastcall TCustomScpExplorerForm::SessionsPageControlContextPopup(TObject * /*Sender*/, TPoint & MousePos, bool & Handled)
  10349. {
  10350. int Index = SessionsPageControl->IndexOfTabAt(MousePos.X, MousePos.Y);
  10351. if (Index >= 0)
  10352. {
  10353. TPopupMenu * PopupMenu = NULL;
  10354. TManagedTerminal * Session = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  10355. if (Session != NULL)
  10356. {
  10357. SessionsPageControl->ActivePageIndex = Index;
  10358. if (DebugAlwaysTrue(SessionTabSwitched()))
  10359. {
  10360. // copied from TControl.WMContextMenu
  10361. SendCancelMode(SessionsPageControl);
  10362. // explicit popup instead of using PopupMenu property
  10363. // to avoid menu to popup somewhere within SessionTabSwitched above,
  10364. // while connecting yet not-connected session and hence
  10365. // allowing an access to commands over not-completely connected session
  10366. PopupMenu = Session->LocalBrowser ? NonVisualDataModule->LocalBrowserPopup : NonVisualDataModule->SessionsPopup;
  10367. }
  10368. }
  10369. else
  10370. {
  10371. if (SupportsLocalBrowser())
  10372. {
  10373. PopupMenu = NonVisualDataModule->NewTabPopup;
  10374. }
  10375. }
  10376. if (PopupMenu != NULL)
  10377. {
  10378. TPoint Point = SessionsPageControl->ClientToScreen(MousePos);
  10379. PopupMenu->PopupComponent = SessionsPageControl;
  10380. PopupMenu->Popup(Point.x, Point.y);
  10381. }
  10382. }
  10383. Handled = true;
  10384. }
  10385. //---------------------------------------------------------------------------
  10386. bool __fastcall TCustomScpExplorerForm::CanChangePassword()
  10387. {
  10388. return HasActiveTerminal() && Terminal->IsCapable[fcChangePassword];
  10389. }
  10390. //---------------------------------------------------------------------------
  10391. void __fastcall TCustomScpExplorerForm::ChangePassword()
  10392. {
  10393. Configuration->Usage->Inc(L"PasswordChanges");
  10394. std::unique_ptr<TSessionData> ChangePasswordData(Terminal->SessionData->Clone());
  10395. ChangePasswordData->DisableAuthentationsExceptPassword();
  10396. ChangePasswordData->ChangePassword = true;
  10397. std::unique_ptr<TTerminal> ChangePasswordSession(
  10398. Terminal->CreateSecondarySession(L"ChangePassword", ChangePasswordData.get()));
  10399. ChangePasswordSession->AutoReadDirectory = false;
  10400. if (TTerminalManager::Instance()->ConnectTerminal(ChangePasswordSession.get()))
  10401. {
  10402. MessageDialog(MainInstructions(LoadStr(PASSWORD_CHANGED)), qtInformation, qaOK, HELP_CHANGE_PASSWORD);
  10403. }
  10404. else
  10405. {
  10406. Configuration->Usage->Inc(L"PasswordChangesFailed");
  10407. }
  10408. }
  10409. //---------------------------------------------------------------------------
  10410. bool __fastcall TCustomScpExplorerForm::CanPrivateKeyUpload()
  10411. {
  10412. // No nice way to assert SSH2
  10413. return HasActiveTerminal() && (Terminal->FSProtocol == cfsSFTP);
  10414. }
  10415. //---------------------------------------------------------------------------
  10416. void __fastcall TCustomScpExplorerForm::PrivateKeyUpload()
  10417. {
  10418. TOperationVisualizer OperationVisualizer;
  10419. UnicodeString FileName = Terminal->SessionData->PublicKeyFile;
  10420. TTerminalManager::Instance()->UploadPublicKey(Terminal, NULL, FileName);
  10421. }
  10422. //---------------------------------------------------------------------------
  10423. void __fastcall TCustomScpExplorerForm::ChangeScale(int M, int D)
  10424. {
  10425. TForm::ChangeScale(M, D);
  10426. int APixelsPerInch = GetControlPixelsPerInch(this);
  10427. HANDLE ResourceModule = GUIConfiguration->ChangeToDefaultResourceModule();
  10428. try
  10429. {
  10430. GlyphsModule->SetPixelsPerInch(APixelsPerInch);
  10431. }
  10432. __finally
  10433. {
  10434. GUIConfiguration->ChangeResourceModule(ResourceModule);
  10435. }
  10436. }
  10437. //---------------------------------------------------------------------------
  10438. void __fastcall TCustomScpExplorerForm::DockContextPopup(TObject * Sender, TPoint & MousePos, bool & /*Handled*/)
  10439. {
  10440. NonVisualDataModule->ControlContextPopup(Sender, MousePos);
  10441. }
  10442. //---------------------------------------------------------------------
  10443. class TPublicControl : public TControl
  10444. {
  10445. friend class TCustomScpExplorerForm;
  10446. };
  10447. //---------------------------------------------------------------------------
  10448. void __fastcall TCustomScpExplorerForm::CopyPopup(TControl * DestControl, TControl * SourceControl)
  10449. {
  10450. static_cast<TPublicControl *>(DestControl)->PopupMenu = static_cast<TPublicControl *>(SourceControl)->PopupMenu;
  10451. static_cast<TPublicControl *>(DestControl)->OnContextPopup = static_cast<TPublicControl *>(SourceControl)->OnContextPopup;
  10452. }
  10453. //---------------------------------------------------------------------------
  10454. void __fastcall TCustomScpExplorerForm::DoBookmarkClick(TOperationSide Side, TObject * Sender)
  10455. {
  10456. TBookmark * Bookmark = DebugNotNull(reinterpret_cast<TBookmark *>(dynamic_cast<TTBCustomItem *>(Sender)->Tag));
  10457. OpenBookmark(Side, Bookmark);
  10458. }
  10459. //---------------------------------------------------------------------------
  10460. void __fastcall TCustomScpExplorerForm::LocalBookmarkClick(TObject * Sender)
  10461. {
  10462. DoBookmarkClick(osLocal, Sender);
  10463. }
  10464. //---------------------------------------------------------------------------
  10465. void __fastcall TCustomScpExplorerForm::RemoteBookmarkClick(TObject * Sender)
  10466. {
  10467. DoBookmarkClick(osRemote, Sender);
  10468. }
  10469. //---------------------------------------------------------------------------
  10470. void __fastcall TCustomScpExplorerForm::CreateOpenDirMenuList(
  10471. TTBCustomItem * Menu, TOperationSide Side, TBookmarkList * BookmarkList)
  10472. {
  10473. if (BookmarkList != NULL)
  10474. {
  10475. TNotifyEvent OnBookmarkClick = (Side == osLocal) ? &LocalBookmarkClick : &RemoteBookmarkClick;
  10476. if (IsLocalBrowserMode())
  10477. {
  10478. Side = osLocal;
  10479. }
  10480. if (!WinConfiguration->UseLocationProfiles)
  10481. {
  10482. std::unique_ptr<TStringList> Directories(new TStringList());
  10483. Directories->CaseSensitive = (Side == osRemote);
  10484. for (int Index = 0; Index < BookmarkList->Count; Index++)
  10485. {
  10486. TBookmark * Bookmark = BookmarkList->Bookmarks[Index];
  10487. UnicodeString Directory = Bookmark->GetSideDirectory(Side);
  10488. if (!Directory.IsEmpty() && (Directories->IndexOf(Directory) < 0))
  10489. {
  10490. std::unique_ptr<TTBCustomItem> Item(new TTBXItem(Owner));
  10491. Item->Caption = EscapeHotkey(Directory);
  10492. Item->ShortCut = Bookmark->ShortCut;
  10493. Item->OnClick = OnBookmarkClick;
  10494. Item->Tag = reinterpret_cast<int>(Bookmark);
  10495. Directories->Add(Directory);
  10496. Menu->Add(Item.release());
  10497. }
  10498. }
  10499. }
  10500. else
  10501. {
  10502. std::unique_ptr<TStrings> Folders(CreateSortedStringList());
  10503. for (int Index = 0; Index < BookmarkList->Count; Index++)
  10504. {
  10505. TBookmark * Bookmark = BookmarkList->Bookmarks[Index];
  10506. if (!Bookmark->Node.IsEmpty())
  10507. {
  10508. Folders->Add(Bookmark->Node);
  10509. }
  10510. }
  10511. for (int Index = 0; Index < Folders->Count; Index++)
  10512. {
  10513. std::unique_ptr<TTBCustomItem> Item(new TTBXSubmenuItem(Owner));
  10514. Item->Caption = Folders->Strings[Index];
  10515. Item->ImageIndex = NonVisualDataModule->RemoteChangePathAction2->ImageIndex;
  10516. Folders->Objects[Index] = Item.get();
  10517. Menu->Add(Item.release());
  10518. }
  10519. for (int Index = 0; Index < BookmarkList->Count; Index++)
  10520. {
  10521. TBookmark * Bookmark = BookmarkList->Bookmarks[Index];
  10522. TTBCustomItem * Parent;
  10523. if (!Bookmark->Node.IsEmpty())
  10524. {
  10525. DebugAssert(Folders->IndexOf(Bookmark->Node) >= 0);
  10526. Parent = dynamic_cast<TTBCustomItem *>(Folders->Objects[Folders->IndexOf(Bookmark->Node)]);
  10527. }
  10528. else
  10529. {
  10530. Parent = Menu;
  10531. }
  10532. std::unique_ptr<TTBCustomItem> Item(new TTBXItem(Owner));
  10533. Item->Caption = Bookmark->Name;
  10534. Item->ShortCut = Bookmark->ShortCut;
  10535. Item->OnClick = OnBookmarkClick;
  10536. Item->Tag = reinterpret_cast<int>(Bookmark);
  10537. if (((Bookmark->Name == Bookmark->Local) || (Bookmark->Name == Bookmark->Remote)) &&
  10538. (Bookmark->Local.IsEmpty() || Bookmark->Remote.IsEmpty()))
  10539. {
  10540. // No hint for location profiles that are actually mere "bookmarks"
  10541. }
  10542. else
  10543. {
  10544. UnicodeString Hint = FORMAT(LoadStrPart(LOCATION_PROFILE_HINT, 1), (Bookmark->Name));
  10545. UnicodeString LongHint;
  10546. if (!Bookmark->Local.IsEmpty())
  10547. {
  10548. AddToList(LongHint, FORMAT(LoadStrPart(LOCATION_PROFILE_HINT, 2), (Bookmark->Local)), L"\n");
  10549. }
  10550. if (!Bookmark->Remote.IsEmpty())
  10551. {
  10552. AddToList(LongHint, FORMAT(LoadStrPart(LOCATION_PROFILE_HINT, 3), (Bookmark->Remote)), L"\n");
  10553. }
  10554. AddToList(Hint, LongHint, L"|");
  10555. Item->Hint = Hint;
  10556. }
  10557. Parent->Add(Item.release());
  10558. }
  10559. }
  10560. AddMenuSeparator(Menu);
  10561. }
  10562. }
  10563. //---------------------------------------------------------------------------
  10564. void __fastcall TCustomScpExplorerForm::CreateOpenDirMenu(TTBCustomItem * Menu, TOperationSide Side)
  10565. {
  10566. Menu->Clear();
  10567. std::unique_ptr<TTBCustomItem> Item;
  10568. Item.reset(new TTBXItem(Owner));
  10569. Item->Action = (Side == osLocal) ? NonVisualDataModule->LocalOpenDirAction : NonVisualDataModule->RemoteOpenDirAction;
  10570. Item->Options = Item->Options << tboDefault;
  10571. Menu->Add(Item.release());
  10572. AddMenuSeparator(Menu);
  10573. if (Terminal != NULL)
  10574. {
  10575. CreateOpenDirMenuList(Menu, Side, WinConfiguration->Bookmarks[Terminal->SessionData->SessionKey]);
  10576. }
  10577. CreateOpenDirMenuList(Menu, Side, WinConfiguration->SharedBookmarks);
  10578. Item.reset(new TTBXItem(Owner));
  10579. Item->Action = (Side == osLocal) ? NonVisualDataModule->LocalAddBookmarkAction2 : NonVisualDataModule->RemoteAddBookmarkAction2;
  10580. Menu->Add(Item.release());
  10581. }
  10582. //---------------------------------------------------------------------------
  10583. bool __fastcall TCustomScpExplorerForm::TryOpenDirectory(TOperationSide Side, const UnicodeString & Path)
  10584. {
  10585. bool Result = true;
  10586. bool Remote = !IsSideLocalBrowser(Side);
  10587. try
  10588. {
  10589. UnicodeString APath = Path;
  10590. if (Remote)
  10591. {
  10592. Terminal->ExceptionOnFail = true;
  10593. }
  10594. else
  10595. {
  10596. APath = ExpandEnvironmentVariables(Path);
  10597. }
  10598. try
  10599. {
  10600. DirView(Side)->Path = APath;
  10601. }
  10602. __finally
  10603. {
  10604. if (Remote)
  10605. {
  10606. Terminal->ExceptionOnFail = false;
  10607. }
  10608. }
  10609. }
  10610. catch (Exception & E)
  10611. {
  10612. if (!Remote || Terminal->Active)
  10613. {
  10614. ShowExtendedExceptionEx(Terminal, &E);
  10615. Result = false;
  10616. }
  10617. else
  10618. {
  10619. throw;
  10620. }
  10621. }
  10622. return Result;
  10623. }
  10624. //---------------------------------------------------------------------------
  10625. void __fastcall TCustomScpExplorerForm::ReloadDirectory(TOperationSide Side)
  10626. {
  10627. // Make sure there some feedback even when loading tiny local folders
  10628. TInstantOperationVisualizer Visualizer;
  10629. DirView(Side)->ReloadDirectory();
  10630. }
  10631. //---------------------------------------------------------------------------
  10632. void __fastcall TCustomScpExplorerForm::CloseSessionTab(int Index)
  10633. {
  10634. if (Index == SessionsPageControl->TabIndex)
  10635. {
  10636. CloseTab();
  10637. }
  10638. else
  10639. {
  10640. TTabSheet * TabSheet = SessionsPageControl->Pages[Index];
  10641. TTerminal * Terminal = GetSessionTabSession(TabSheet);
  10642. TTerminalManager * Manager = TTerminalManager::Instance();
  10643. if (!Terminal->Active)
  10644. {
  10645. Manager->FreeTerminal(Terminal);
  10646. }
  10647. else
  10648. {
  10649. TTerminalQueue * Queue = Manager->FindQueueForTerminal(Terminal);
  10650. if (CanCloseQueue(Queue))
  10651. {
  10652. Manager->FreeTerminal(Terminal);
  10653. }
  10654. }
  10655. }
  10656. }
  10657. //---------------------------------------------------------------------------
  10658. void __fastcall TCustomScpExplorerForm::SessionsPageControlTabButtonClick(TPageControl *, int Index)
  10659. {
  10660. TManagedTerminal * Session = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  10661. if (Session == NULL)
  10662. {
  10663. TRect ButtonRect = SessionsPageControl->TabButtonRect(Index);
  10664. ButtonRect = TRect(SessionsPageControl->ClientToScreen(ButtonRect.TopLeft()), SessionsPageControl->ClientToScreen(ButtonRect.BottomRight()));
  10665. //TPoint P = TPoint(ButtonRect.Left, ButtonRect.Bottom);
  10666. //P = SessionsPageControl->ClientToScreen(P);
  10667. MenuPopup(NonVisualDataModule->NewTabPopup, ButtonRect, SessionsPageControl);
  10668. }
  10669. else
  10670. {
  10671. CloseSessionTab(Index);
  10672. }
  10673. }
  10674. //---------------------------------------------------------------------------
  10675. void __fastcall TCustomScpExplorerForm::CopyFilesToClipboard(TOperationSide Side, bool OnFocused)
  10676. {
  10677. if (DebugAlwaysTrue(!IsSideLocalBrowser(Side)))
  10678. {
  10679. TInstantOperationVisualizer Visualizer;
  10680. // To trigger ClipboardDataObjectRelease (in case the clipboard already contains our data)
  10681. // before we create a new data object
  10682. ClipboardClear();
  10683. DebugAssert(FClipboardFakeDirectory.IsEmpty());
  10684. DebugAssert(FClipboardFakeMonitors.get() == NULL);
  10685. FClipboardFakeDirectory = CreateFakeTransferDirectory();
  10686. // Intermediate variable to WORKAROUND a crash in compiler
  10687. TObjectList * ClipboardFakeMonitors =
  10688. StartCreationDirectoryMonitorsOnEachDrive(FILE_NOTIFY_CHANGE_DIR_NAME, ClipboardFakeCreated);
  10689. FClipboardFakeMonitors.reset(ClipboardFakeMonitors);
  10690. if (FClipboardDragDropFilesEx.get() == NULL)
  10691. {
  10692. FClipboardDragDropFilesEx.reset(new TDragDropFilesEx(this));
  10693. FClipboardDragDropFilesEx->OnDataObjectRelease = ClipboardDataObjectRelease;
  10694. }
  10695. FClipboardDragDropFilesEx->FileList->Clear();
  10696. FClipboardDragDropFilesEx->FileList->AddItem(NULL, FClipboardFakeDirectory);
  10697. FClipboardDragDropFilesEx->CopyToClipboard();
  10698. FClipboardTerminal = Terminal;
  10699. // Need full paths, as cwd can be different once files are pasted
  10700. FClipboardFileList.reset(TRemoteFileList::CloneStrings(RemoteDirView->CreateFileList(OnFocused, true)));
  10701. for (int Index = 0; Index < FClipboardFileList->Count; Index++)
  10702. {
  10703. FClipboardFileList->Strings[Index] = UnixExcludeTrailingBackslash(FClipboardFileList->Strings[Index]);
  10704. }
  10705. AppLogFmt(L"Created fake clipboard directory \"%s\" and started drive monitors", (FClipboardFakeDirectory));
  10706. }
  10707. }
  10708. //---------------------------------------------------------------------------
  10709. void __fastcall TCustomScpExplorerForm::ClipboardClear()
  10710. {
  10711. if (OpenClipboard(0))
  10712. {
  10713. AppLog(L"Clearing clipboard");
  10714. EmptyClipboard(); // Calls ClipboardDataObjectRelease, if clipboard contains our data
  10715. CloseClipboard();
  10716. }
  10717. }
  10718. //---------------------------------------------------------------------------
  10719. bool __fastcall TCustomScpExplorerForm::DoesClipboardContainOurFiles()
  10720. {
  10721. return !FClipboardFakeDirectory.IsEmpty();
  10722. }
  10723. //---------------------------------------------------------------------------
  10724. void __fastcall TCustomScpExplorerForm::ClipboardStop()
  10725. {
  10726. bool Deleted = RemoveDir(ApiPath(FClipboardFakeDirectory));
  10727. AppLogFmt(L"Clearing clipboard, deleted clipboard temporary folder \"%s\" - %d", (FClipboardFakeDirectory, int(Deleted)));
  10728. FClipboardFakeDirectory = UnicodeString();
  10729. FClipboardTerminal = NULL;
  10730. if (FDownloadingFromClipboard)
  10731. {
  10732. // We are called by the monitor, so attempt to release it would deadlock
  10733. FClipboardFakeMonitorsPendingReset = true;
  10734. }
  10735. else
  10736. {
  10737. FClipboardFakeMonitors.reset(NULL);
  10738. }
  10739. }
  10740. //---------------------------------------------------------------------------
  10741. void __fastcall TCustomScpExplorerForm::ClipboardDataObjectRelease(TObject * /*Sender*/)
  10742. {
  10743. if (DebugAlwaysTrue(!FClipboardFakeDirectory.IsEmpty()))
  10744. {
  10745. ClipboardStop();
  10746. }
  10747. }
  10748. //---------------------------------------------------------------------------
  10749. void __fastcall TCustomScpExplorerForm::ClipboardDownload(const UnicodeString & TargetDirectory, bool NoConfirmation, bool DragDrop)
  10750. {
  10751. TTerminalManager * Manager = TTerminalManager::Instance();
  10752. Manager->ActiveSession = FClipboardTerminal;
  10753. TTransferOperationParam Params;
  10754. Params.TargetDirectory = TargetDirectory;
  10755. Params.DragDrop = DragDrop;
  10756. ExecuteFileOperation(foCopy, osRemote, FClipboardFileList.get(), NoConfirmation, &Params);
  10757. }
  10758. //---------------------------------------------------------------------------
  10759. void TCustomScpExplorerForm::PasteFiles()
  10760. {
  10761. // Guard against possible race conditions
  10762. if (!FClipboardPasteTarget.IsEmpty())
  10763. {
  10764. // ClipboardFakeCreated might be called while we are here, so make sure we finish operating on the same path we started with
  10765. UnicodeString Target = FClipboardPasteTarget;
  10766. FClipboardPasteTarget = EmptyStr;
  10767. // Can fail as it can be e.g. locked by AV, so we retry that later
  10768. bool Removed = RemoveDir(ApiPath(Target));
  10769. AppLogFmt(L"First attempt to delete pasted fake clipboard directory \"%s\" - %d", (Target, int(Removed)));
  10770. try
  10771. {
  10772. if (NonVisualDataModule->Busy)
  10773. {
  10774. AppLog(L"Is busy, cannot paste");
  10775. }
  10776. else
  10777. {
  10778. DebugAssert(!FClipboardFakeMonitorsPendingReset);
  10779. FClipboardFakeMonitorsPendingReset = false;
  10780. bool NoConfirmation = (WinConfiguration->DDTransferConfirmation == asOff);
  10781. TAutoFlag Flag(FDownloadingFromClipboard);
  10782. AppLog(L"Downloading to paste target...");
  10783. ClipboardDownload(ExtractFilePath(Target), NoConfirmation, true);
  10784. AppLog(L"Downloaded to paste target...");
  10785. }
  10786. }
  10787. __finally
  10788. {
  10789. if (!Removed)
  10790. {
  10791. Removed = RemoveDir(ApiPath(Target));
  10792. AppLogFmt(L"Second attempt to delete pasted fake clipboard directory \"%s\" - %d", (Target, int(Removed)));
  10793. }
  10794. }
  10795. }
  10796. }
  10797. //---------------------------------------------------------------------------
  10798. void __fastcall TCustomScpExplorerForm::ClipboardFakeCreated(TObject * /*Sender*/, const UnicodeString FileName)
  10799. {
  10800. // It can actually rarelly happen that some random file is created, while we are shutting down the monitor
  10801. // (as it pumps a Windows message queue while being shutted down)
  10802. if (DebugAlwaysTrue(!FClipboardFakeDirectory.IsEmpty()) &&
  10803. // Is can happen that creation of our temporary directory is detected (even though we are creating it before starting the monitors).
  10804. // It tent do happen with later copy-pastes, probably because everything is cached already and monitors start quickly.
  10805. !IsPathToSameFile(FClipboardFakeDirectory, FileName) &&
  10806. SameText(ExtractFileName(FileName), ExtractFileName(FClipboardFakeDirectory)))
  10807. {
  10808. AppLogFmt(L"Fake clipboard directory pasted to \"%s\"", (FileName));
  10809. FClipboardPasteTarget = FileName;
  10810. // Particularly when pasting to external HDD, the fake directory is strangely recreated if removed here in the monitor callback.
  10811. // Postponing removing (or actually whole pasting operation) seems to help.
  10812. PostMessage(Handle, WM_PASTE_FILES, 0, 0);
  10813. }
  10814. }
  10815. //---------------------------------------------------------------------------
  10816. void __fastcall TCustomScpExplorerForm::DirViewGetItemColor(
  10817. TObject * Sender, UnicodeString FileName, bool Directory, __int64 Size, TDateTime Modification, TColor & Color)
  10818. {
  10819. TFileMasks::TParams MaskParams;
  10820. MaskParams.Size = Size;
  10821. MaskParams.Modification = Modification;
  10822. TCustomDirView * DirView = DebugNotNull(dynamic_cast<TCustomDirView *>(Sender));
  10823. bool Local = (dynamic_cast<TUnixDirView *>(DirView) == NULL);
  10824. // TUnixDirView::ItemFullFileName returns trailing backslash for directories, what filemask cannot deal with
  10825. if (!Local && Directory)
  10826. {
  10827. FileName = UnixExcludeTrailingBackslash(FileName);
  10828. }
  10829. for (TFileColorData::TList::const_iterator Iter = FFileColors.begin(); Iter != FFileColors.end(); Iter++)
  10830. {
  10831. bool ImplicitMatch;
  10832. if (Iter->FileMask.Matches(FileName, Local, Directory, &MaskParams, false, ImplicitMatch) &&
  10833. !ImplicitMatch)
  10834. {
  10835. Color = Iter->Color;
  10836. break;
  10837. }
  10838. }
  10839. }
  10840. //---------------------------------------------------------------------------
  10841. void __fastcall TCustomScpExplorerForm::DirViewKeyDown(TObject * Sender, WORD & Key, TShiftState)
  10842. {
  10843. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  10844. if (!DirView->IsEditing())
  10845. {
  10846. // Handled here, instead of RemoteDirViewKeyPress (as on Login dialog),
  10847. // as VK_BACK is handled by TCustomDirView.KeyDown and we can swallow it here,
  10848. // while searching
  10849. if (Key == VK_BACK)
  10850. {
  10851. if (!FIncrementalSearch.IsEmpty())
  10852. {
  10853. if (FIncrementalSearch.Length() == 1)
  10854. {
  10855. ResetIncrementalSearch();
  10856. }
  10857. else
  10858. {
  10859. UnicodeString NewText = FIncrementalSearch.SubString(1, FIncrementalSearch.Length() - 1);
  10860. IncrementalSearch(NewText, false, false);
  10861. }
  10862. Key = 0;
  10863. }
  10864. }
  10865. }
  10866. }
  10867. //---------------------------------------------------------------------------
  10868. void __fastcall TCustomScpExplorerForm::DirViewKeyPress(TObject * Sender, wchar_t & Key)
  10869. {
  10870. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  10871. if (!DirView->IsEditing() && (WinConfiguration->PanelSearch != isOff))
  10872. {
  10873. // Filter control sequences.
  10874. // When not searching yet, prefer use of the space for toggling file selection
  10875. // (so we cannot incrementally search for a string starting with a space).
  10876. if ((Key > VK_SPACE) ||
  10877. ((Key == VK_SPACE) && (GetKeyState(VK_CONTROL) >= 0) && !FIncrementalSearch.IsEmpty()))
  10878. {
  10879. IncrementalSearch(FIncrementalSearch + Key, false, false);
  10880. Key = 0;
  10881. }
  10882. }
  10883. }
  10884. //---------------------------------------------------------------------------
  10885. void __fastcall TCustomScpExplorerForm::ResetIncrementalSearch()
  10886. {
  10887. if (!FIncrementalSearch.IsEmpty())
  10888. {
  10889. FIncrementalSearch = L"";
  10890. DirView(osCurrent)->UpdateStatusBar();
  10891. }
  10892. }
  10893. //---------------------------------------------------------------------------
  10894. void __fastcall TCustomScpExplorerForm::IncrementalSearch(const UnicodeString & Text, bool SkipCurrent, bool Reverse)
  10895. {
  10896. TListItem * Item = SearchFile(Text, SkipCurrent, Reverse);
  10897. if (Item == NULL)
  10898. {
  10899. MessageBeep(MB_ICONHAND);
  10900. }
  10901. else
  10902. {
  10903. TCustomDirView * ADirView = DirView(osCurrent);
  10904. {
  10905. TAutoNestingCounter Guard(FIncrementalSearching);
  10906. ADirView->FocusItem(Item);
  10907. if (ADirView->NortonLike == nlOff)
  10908. {
  10909. if ((ADirView->Selected != Item) ||
  10910. (ADirView->SelCount > 1))
  10911. {
  10912. ADirView->ClearSelection();
  10913. ADirView->Selected = Item;
  10914. }
  10915. }
  10916. }
  10917. FIncrementalSearch = Text;
  10918. Item->MakeVisible(false);
  10919. TListItem * NextItem = SearchFile(Text, true, Reverse);
  10920. FIncrementalSearchHaveNext = (NextItem != NULL) && (NextItem != Item);
  10921. ADirView->UpdateStatusBar();
  10922. }
  10923. }
  10924. //---------------------------------------------------------------------------
  10925. TListItem * __fastcall TCustomScpExplorerForm::GetNextFile(TListItem * Item, bool Reverse)
  10926. {
  10927. int Index = Item->Index;
  10928. if (!Reverse)
  10929. {
  10930. Index++;
  10931. if (Index >= Item->Owner->Count)
  10932. {
  10933. Index = 0;
  10934. }
  10935. }
  10936. else
  10937. {
  10938. Index--;
  10939. if (Index < 0)
  10940. {
  10941. Index = Item->Owner->Count - 1;
  10942. }
  10943. }
  10944. return Item->Owner->Item[Index];
  10945. }
  10946. //---------------------------------------------------------------------------
  10947. TListItem * __fastcall TCustomScpExplorerForm::SearchFile(const UnicodeString & Text, bool SkipCurrent, bool Reverse)
  10948. {
  10949. TCustomDirView * ADirView = DirView(osCurrent);
  10950. if (ADirView->Items->Count == 0)
  10951. {
  10952. return NULL;
  10953. }
  10954. else
  10955. {
  10956. TListItem * CurrentItem = (ADirView->ItemFocused != NULL) ? ADirView->ItemFocused : ADirView->Items->Item[0];
  10957. TListItem * Item = CurrentItem;
  10958. if (SkipCurrent)
  10959. {
  10960. Item = DebugNotNull(GetNextFile(Item, Reverse));
  10961. }
  10962. while (true)
  10963. {
  10964. bool Matches = false;
  10965. switch (WinConfiguration->PanelSearch)
  10966. {
  10967. case isNameStartOnly:
  10968. Matches = ContainsTextSemiCaseSensitive(ADirView->ItemFileName(Item).SubString(1, Text.Length()), Text);
  10969. break;
  10970. case isName:
  10971. Matches = ContainsTextSemiCaseSensitive(ADirView->ItemFileName(Item), Text);
  10972. break;
  10973. case isAll:
  10974. int ColCount = (ADirView->ViewStyle == vsReport) ? ADirView->ColProperties->Count : 1;
  10975. int Index = 0;
  10976. while ((Index < ColCount) && !Matches)
  10977. {
  10978. bool Visible = (ADirView->ViewStyle == vsReport) ? ADirView->ColProperties->Visible[Index] : true;
  10979. if (Visible)
  10980. {
  10981. Matches = ContainsTextSemiCaseSensitive(ADirView->GetColumnText(Item, Index), Text);
  10982. }
  10983. Index++;
  10984. }
  10985. break;
  10986. }
  10987. if (Matches)
  10988. {
  10989. return Item;
  10990. }
  10991. Item = GetNextFile(Item, Reverse);
  10992. if (Item == CurrentItem)
  10993. {
  10994. return NULL;
  10995. }
  10996. }
  10997. }
  10998. }
  10999. //---------------------------------------------------------------------------
  11000. void __fastcall TCustomScpExplorerForm::DirViewExit(TObject *)
  11001. {
  11002. ResetIncrementalSearch();
  11003. }
  11004. //---------------------------------------------------------------------------
  11005. void __fastcall TCustomScpExplorerForm::CMDialogKey(TWMKeyDown & Message)
  11006. {
  11007. if (Message.CharCode == VK_TAB)
  11008. {
  11009. if (!FIncrementalSearch.IsEmpty())
  11010. {
  11011. TShiftState Shift = KeyDataToShiftState(Message.KeyData);
  11012. bool Reverse = Shift.Contains(ssShift);
  11013. IncrementalSearch(FIncrementalSearch, true, Reverse);
  11014. Message.Result = 1;
  11015. return;
  11016. }
  11017. }
  11018. else if (Message.CharCode == VK_ESCAPE)
  11019. {
  11020. if (!FIncrementalSearch.IsEmpty())
  11021. {
  11022. ResetIncrementalSearch();
  11023. Message.Result = 1;
  11024. return;
  11025. }
  11026. }
  11027. TForm::Dispatch(&Message);
  11028. }
  11029. //---------------------------------------------------------------------------
  11030. void __fastcall TCustomScpExplorerForm::Deactivate()
  11031. {
  11032. TForm::Deactivate();
  11033. // E.g. when switching to an internal editor window
  11034. ResetIncrementalSearch();
  11035. }
  11036. //---------------------------------------------------------------------------
  11037. void __fastcall TCustomScpExplorerForm::ApplicationEventsDeactivate(TObject *)
  11038. {
  11039. // When switching to another application
  11040. ResetIncrementalSearch();
  11041. }
  11042. //---------------------------------------------------------------------------
  11043. void __fastcall TCustomScpExplorerForm::ApplicationEventsModalBegin(TObject *)
  11044. {
  11045. ResetIncrementalSearch();
  11046. }
  11047. //---------------------------------------------------------------------------
  11048. void __fastcall TCustomScpExplorerForm::DirViewChangeFocus(TObject *, TListItem *)
  11049. {
  11050. if (FIncrementalSearching <= 0)
  11051. {
  11052. ResetIncrementalSearch();
  11053. }
  11054. }
  11055. //---------------------------------------------------------------------------
  11056. void __fastcall TCustomScpExplorerForm::RemoteStatusBarMouseDown(TObject *, TMouseButton, TShiftState, int, int)
  11057. {
  11058. CountClicksForWindowPrint(this);
  11059. }
  11060. //---------------------------------------------------------------------------
  11061. void __fastcall TCustomScpExplorerForm::RemoteDirViewResize(TObject *)
  11062. {
  11063. CenterReconnectToolbar();
  11064. }
  11065. //---------------------------------------------------------------------------
  11066. void TCustomScpExplorerForm::DoBrowseFile(TCustomDirView * DirView, const UnicodeString & FileName)
  11067. {
  11068. if (!FileName.IsEmpty())
  11069. {
  11070. DirView->RestoreFocus(FileName);
  11071. if (DirView->ItemFocused != NULL)
  11072. {
  11073. DirView->ItemFocused->Selected = true;
  11074. }
  11075. }
  11076. }
  11077. //---------------------------------------------------------------------------
  11078. void __fastcall TCustomScpExplorerForm::BrowseFile(const UnicodeString & FileName)
  11079. {
  11080. DoBrowseFile(RemoteDirView, FileName);
  11081. }
  11082. //---------------------------------------------------------------------------
  11083. void __fastcall TCustomScpExplorerForm::UpdateQueueFileList()
  11084. {
  11085. if (!FQueueStatusUpdating)
  11086. {
  11087. bool Refresh;
  11088. if ((FQueueStatus != NULL) && QueueFileList->Visible)
  11089. {
  11090. Refresh = FQueueStatus->UpdateFileList(FQueueController->GetFocusedPrimaryItem(), FQueueFileList.get());
  11091. }
  11092. else
  11093. {
  11094. if (FQueueFileList->GetCount() > 0)
  11095. {
  11096. FQueueFileList->Clear();
  11097. Refresh = true;
  11098. }
  11099. }
  11100. int Count = FQueueFileList->GetCount();
  11101. if (QueueFileList->Items->Count != Count)
  11102. {
  11103. DebugAssert(Refresh);
  11104. QueueFileList->Items->Count = Count;
  11105. }
  11106. if (Refresh)
  11107. {
  11108. QueueFileList->Invalidate();
  11109. }
  11110. DebugAssert(QueueFileList->Items->Count == FQueueFileList->GetCount());
  11111. }
  11112. }
  11113. //---------------------------------------------------------------------------
  11114. void __fastcall TCustomScpExplorerForm::QueueFileListData(TObject *, TListItem * Item)
  11115. {
  11116. Item->Caption = FQueueFileList->GetFileName(Item->Index);
  11117. }
  11118. //---------------------------------------------------------------------------
  11119. void __fastcall TCustomScpExplorerForm::QueueView3Change(TObject *, TListItem *, TItemChange)
  11120. {
  11121. // Should be improved, once we do not refresh the list when switching between details of the same batch.
  11122. // See TSynchronizeChecklistDialog::UpdateTimer
  11123. if (QueueFileList->Items->Count > 0)
  11124. {
  11125. QueueFileList->Items->Item[0]->MakeVisible(false);
  11126. }
  11127. UpdateQueueFileList();
  11128. }
  11129. //---------------------------------------------------------------------------
  11130. void __fastcall TCustomScpExplorerForm::QueueLabelGetStatus(TCustomPathLabel *, bool & Active)
  11131. {
  11132. Active = QueueView3->Focused() || QueueFileList->Focused();
  11133. }
  11134. //---------------------------------------------------------------------------
  11135. void __fastcall TCustomScpExplorerForm::QueueFileListEnterExit(TObject *)
  11136. {
  11137. QueueLabelUpdateStatus();
  11138. }
  11139. //---------------------------------------------------------------------------
  11140. void __fastcall TCustomScpExplorerForm::QueueFileListCustomDrawItem(
  11141. TCustomListView * Sender, TListItem * Item, TCustomDrawState, bool & DebugUsedArg(DefaultDraw))
  11142. {
  11143. int State = FQueueFileList->GetState(Item->Index);
  11144. if (State == qfsProcessed)
  11145. {
  11146. Sender->Canvas->Font->Color = clGrayText;
  11147. }
  11148. }
  11149. //---------------------------------------------------------------------------
  11150. void __fastcall TCustomScpExplorerForm::QueueFileListColumnAutoSize()
  11151. {
  11152. // We do not really need whole width, so we always preemptivelly keep free space for the vertical scrollbar
  11153. QueueFileList->Column[0]->Width =
  11154. QueueFileList->ClientWidth - GetSystemMetricsForControl(QueueFileList, SM_CXVSCROLL) - ScaleByTextHeight(QueueFileList, 8);
  11155. }
  11156. //---------------------------------------------------------------------------
  11157. void __fastcall TCustomScpExplorerForm::QueueFileListResize(TObject *)
  11158. {
  11159. QueueFileListColumnAutoSize();
  11160. }
  11161. //---------------------------------------------------------------------------
  11162. void __fastcall TCustomScpExplorerForm::CloseApp()
  11163. {
  11164. // Called from TNonVisualDataModule::ExplorerActionsExecute, which sets busy flag.
  11165. // FormCloseQuery must check busy state, so that the application cannot be closed while busy by Alt+F4 and X button.
  11166. // So we clear the flag here.
  11167. DebugAssert(NonVisualDataModule->Busy);
  11168. NonVisualDataModule->EndBusy();
  11169. try
  11170. {
  11171. Close();
  11172. }
  11173. __finally
  11174. {
  11175. NonVisualDataModule->StartBusy();
  11176. }
  11177. }
  11178. //---------------------------------------------------------------------------
  11179. bool __fastcall TCustomScpExplorerForm::IsActiveTerminal(TTerminal * Terminal)
  11180. {
  11181. return (Terminal != NULL) && Terminal->Active;
  11182. }
  11183. //---------------------------------------------------------------------------
  11184. bool __fastcall TCustomScpExplorerForm::HasManagedSession()
  11185. {
  11186. return (ManagedSession != NULL);
  11187. }
  11188. //---------------------------------------------------------------------------
  11189. bool __fastcall TCustomScpExplorerForm::HasActiveTerminal()
  11190. {
  11191. return IsActiveTerminal(Terminal);
  11192. }
  11193. //---------------------------------------------------------------------------
  11194. void TCustomScpExplorerForm::LocalLocalCopy(
  11195. TFileOperation, TOperationSide, bool DebugUsedArg(OnFocused), bool DebugUsedArg(NoConfirmation),
  11196. bool DebugUsedArg(DragDrop), unsigned int DebugUsedArg(Flags))
  11197. {
  11198. DebugFail();
  11199. }
  11200. //---------------------------------------------------------------------------
  11201. void TCustomScpExplorerForm::LocalLocalCopyCommand(TFileOperation Operation, TOperationSide Side, bool OnFocused, unsigned int Flags)
  11202. {
  11203. LocalLocalCopy(Operation, Side, OnFocused, !WinConfiguration->ConfirmTransferring, false, Flags);
  11204. }
  11205. //---------------------------------------------------------------------------
  11206. void __fastcall TCustomScpExplorerForm::SessionsPageControlResize(TObject *)
  11207. {
  11208. // 1) Changing tab list triggers the OnResize.
  11209. // 2) Is called in TForm constructor, when we are not ready to call SessionListChanged (particularly when starting with colored session)
  11210. if (!FUpdatingSessionTabs && (FSessionColors != NULL))
  11211. {
  11212. // UpdateTabsCaptionTruncation should be enough
  11213. SessionListChanged(true);
  11214. }
  11215. }
  11216. //---------------------------------------------------------------------------
  11217. UnicodeString TCustomScpExplorerForm::GetSessionPath(TManagedTerminal * ASession, TOperationSide Side)
  11218. {
  11219. if (ASession == ManagedSession)
  11220. {
  11221. UpdateSession(ASession);
  11222. }
  11223. TSessionData * Data = ASession->StateData;
  11224. UnicodeString Result;
  11225. if (Side == osLocal)
  11226. {
  11227. Result = Data->LocalDirectory;
  11228. }
  11229. else if (DebugAlwaysTrue(Side == osOther))
  11230. {
  11231. Result = ASession->LocalBrowser ? Data->OtherLocalDirectory : Data->RemoteDirectory;
  11232. }
  11233. return Result;
  11234. }
  11235. //---------------------------------------------------------------------------
  11236. UnicodeString TCustomScpExplorerForm::GetTabHintSessionDetails(TManagedTerminal * ASession)
  11237. {
  11238. UnicodeString Title = TTerminalManager::Instance()->GetSessionTitle(ASession, false);
  11239. UnicodeString Result = ASession->SessionData->DefaultSessionName;
  11240. if (Title == Result)
  11241. {
  11242. Result = EmptyStr;
  11243. }
  11244. else
  11245. {
  11246. Result += L"\n";
  11247. }
  11248. return Result;
  11249. }
  11250. //---------------------------------------------------------------------------
  11251. UnicodeString TCustomScpExplorerForm::GetTabHintDetails(TManagedTerminal * ASession)
  11252. {
  11253. UnicodeString Result = GetTabHintSessionDetails(ASession);
  11254. if (!ASession->Active)
  11255. {
  11256. Result += LoadStr(STATUS_NOT_CONNECTED2);
  11257. }
  11258. else
  11259. {
  11260. Result += GetSessionPath(ASession, osRemote);
  11261. }
  11262. return Result;
  11263. }
  11264. //---------------------------------------------------------------------------
  11265. UnicodeString TCustomScpExplorerForm::GetNewTabHintDetails()
  11266. {
  11267. return LoadStr(NEW_REMOTE_TAB_HINT);
  11268. }
  11269. //---------------------------------------------------------------------------
  11270. void __fastcall TCustomScpExplorerForm::SessionsPageControlTabHint(TPageControl *, int Index, UnicodeString & Hint)
  11271. {
  11272. if (Index >= 0)
  11273. {
  11274. TThemeTabSheet * TabSheet = SessionsPageControl->Pages[Index];
  11275. TManagedTerminal * TabSession = GetSessionTabSession(TabSheet);
  11276. if (TabSession == NULL)
  11277. {
  11278. UnicodeString NewTabTabCaption = GetNewTabTabCaption();
  11279. if (NewTabTabCaption != TabSheet->Caption)
  11280. {
  11281. Hint = NewTabTabCaption + L"|" + GetNewTabHintDetails();
  11282. }
  11283. }
  11284. else
  11285. {
  11286. TTerminalManager * Manager = TTerminalManager::Instance();
  11287. UnicodeString TabCaption = Manager->GetSessionTitle(TabSession, true);
  11288. Hint = TabCaption + L"|" + GetTabHintDetails(TabSession);
  11289. }
  11290. }
  11291. }
  11292. //---------------------------------------------------------------------------
  11293. typedef std::vector<TListItem *> TListItemsVector;
  11294. struct TCalculateSizeOperation
  11295. {
  11296. TCalculateSizeStats Stats;
  11297. TListItemsVector ListItems;
  11298. size_t Index;
  11299. };
  11300. //---------------------------------------------------------------------------
  11301. void TCustomScpExplorerForm::CalculateDirectorySizes(TOperationSide Side)
  11302. {
  11303. TCalculateSizeOperation CalculateSizeOperation;
  11304. Side = GetSide(Side);
  11305. TCustomDirView * ADirView = DirView(Side);
  11306. bool LocalSide = IsSideLocalBrowser(Side);
  11307. std::unique_ptr<TStrings> AllFileList(ADirView->CreateFileList(false, LocalSide, NULL, true));
  11308. std::unique_ptr<TStrings> FileList(new TStringList());
  11309. for (int Index = 0; Index < AllFileList->Count; Index++)
  11310. {
  11311. TListItem * Item = DebugNotNull(dynamic_cast<TListItem *>(AllFileList->Objects[Index]));
  11312. if (ADirView->ItemIsDirectory(Item))
  11313. {
  11314. FileList->AddObject(AllFileList->Strings[Index], ADirView->ItemData(Item));
  11315. CalculateSizeOperation.ListItems.push_back(Item);
  11316. }
  11317. }
  11318. CalculateSizeOperation.Index = 0;
  11319. __int64 Size = 0;
  11320. TValueRestorer<TCalculateSizeOperation *> DirectorySizeOperationRestorer(FCalculateSizeOperation, &CalculateSizeOperation);
  11321. TValueRestorer<TFileOperationFinishedEvent> OnFileOperationFinishedRestorer(FOnFileOperationFinished, DirectorySizeCalculated);
  11322. TValueRestorer<TFileOperation> PrimaryOperationRestorer(FPrimaryOperation, foCalculateSize);
  11323. TCalculatedSizes CalculatedSizes;
  11324. CalculateSizeOperation.Stats.CalculatedSizes = &CalculatedSizes;
  11325. if (LocalSide)
  11326. {
  11327. TValueRestorer<TOperationSide> ProgressSideRestorer(FProgressSide, Side);
  11328. ManagedSession->CalculateLocalFilesSize(FileList.get(), Size, NULL, true, NULL, &CalculatedSizes);
  11329. }
  11330. else
  11331. {
  11332. TCalculateSizeParams Params;
  11333. Params.Stats = &CalculateSizeOperation.Stats;
  11334. Params.UseCache = true;
  11335. Terminal->CalculateFilesSize(FileList.get(), Size, Params);
  11336. }
  11337. }
  11338. //---------------------------------------------------------------------------
  11339. void __fastcall TCustomScpExplorerForm::DirectorySizeCalculated(TOperationSide Side, const UnicodeString & FileName, bool Success)
  11340. {
  11341. DebugAlwaysTrue(FCalculateSizeOperation != NULL);
  11342. TListItem * Item = VisualiseOperationFinished(Side, FileName, false);
  11343. TCustomDirView * ADirView = dynamic_cast<TCustomDirView *>(Item->Owner->Owner);
  11344. // optimization
  11345. bool GotExpectedItem =
  11346. (FCalculateSizeOperation->Index < FCalculateSizeOperation->ListItems.size()) &&
  11347. (FCalculateSizeOperation->ListItems[FCalculateSizeOperation->Index] == Item);
  11348. int Index = -1;
  11349. if (DebugAlwaysTrue(GotExpectedItem))
  11350. {
  11351. Index = FCalculateSizeOperation->Index;
  11352. FCalculateSizeOperation->Index++;
  11353. }
  11354. else
  11355. {
  11356. TListItemsVector::iterator I =
  11357. std::find(FCalculateSizeOperation->ListItems.begin(), FCalculateSizeOperation->ListItems.end(), Item);
  11358. if (DebugAlwaysTrue(I != FCalculateSizeOperation->ListItems.end()))
  11359. {
  11360. Index = I - FCalculateSizeOperation->ListItems.begin();
  11361. }
  11362. }
  11363. if (DebugAlwaysTrue(Index >= 0) && Success)
  11364. {
  11365. __int64 Size = (*FCalculateSizeOperation->Stats.CalculatedSizes)[Index];
  11366. ADirView->SetItemCalculatedSize(Item, Size);
  11367. }
  11368. }
  11369. //---------------------------------------------------------------------------
  11370. void __fastcall TCustomScpExplorerForm::MessageDockRequestDock(TObject *, TTBCustomDockableWindow *, bool & Accept)
  11371. {
  11372. Accept = false;
  11373. }
  11374. //---------------------------------------------------------------------------
  11375. void TCustomScpExplorerForm::AutoSizeColumns(TOperationSide Side)
  11376. {
  11377. // Send Ctrl+Gray+ to the list view
  11378. TKeyboardState KeyState;
  11379. GetKeyboardState(KeyState);
  11380. TKeyboardState NewKeyState;
  11381. memset(NewKeyState, 0, sizeof(NewKeyState));
  11382. NewKeyState[VK_CONTROL] = 0x81;
  11383. SetKeyboardState(NewKeyState);
  11384. SendMessage(DirView(Side)->Handle, WM_KEYDOWN, VK_ADD, 0);
  11385. SetKeyboardState(KeyState);
  11386. }
  11387. //---------------------------------------------------------------------------
  11388. void TCustomScpExplorerForm::QueueResetLayoutColumns()
  11389. {
  11390. LoadListViewStr(QueueView3, QueueViewLayoutDefault);
  11391. }
  11392. //---------------------------------------------------------------------------
  11393. void * TCustomScpExplorerForm::SaveFocus()
  11394. {
  11395. return NULL;
  11396. }
  11397. //---------------------------------------------------------------------------
  11398. void TCustomScpExplorerForm::RestoreFocus(void * Focus)
  11399. {
  11400. DebugUsedParam(Focus);
  11401. DebugAssert(Focus == NULL);
  11402. }