CustomScpExplorer.cpp 353 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773
  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 <Bookmarks.h>
  8. #include <Interface.h>
  9. #include <Exceptions.h>
  10. #include <CoreMain.h>
  11. #include <FileSystems.h>
  12. #include <TextsCore.h>
  13. #include <TextsWin.h>
  14. #include <HelpWin.h>
  15. #include <VCLCommon.h>
  16. #include <Progress.h>
  17. #include <SynchronizeProgress.h>
  18. #include <DragExt.h>
  19. #include <WinApi.h>
  20. #include "GUITools.h"
  21. #include "NonVisual.h"
  22. #include "Glyphs.h"
  23. #include "Tools.h"
  24. #include "WinConfiguration.h"
  25. #include "TerminalManager.h"
  26. #include "EditorManager.h"
  27. #include "ProgParams.h"
  28. #include "Setup.h"
  29. #include <Consts.hpp>
  30. #include <DateUtils.hpp>
  31. #include <TB2Common.hpp>
  32. #include <DirectoryMonitor.hpp>
  33. #include <System.IOUtils.hpp>
  34. //---------------------------------------------------------------------------
  35. #pragma package(smart_init)
  36. #pragma link "CustomDirView"
  37. #pragma link "CustomUnixDirView"
  38. #pragma link "IEListView"
  39. #pragma link "NortonLikeListView"
  40. #pragma link "UnixDirView"
  41. #pragma link "CustomDriveView"
  42. #pragma link "UnixDriveView"
  43. #pragma link "CustomDriveView"
  44. #pragma link "TB2Dock"
  45. #pragma link "TBXStatusBars"
  46. #pragma link "TB2Item"
  47. #pragma link "TB2Toolbar"
  48. #pragma link "ThemePageControl"
  49. #pragma link "PathLabel"
  50. #pragma resource "*.dfm"
  51. //---------------------------------------------------------------------------
  52. #define WM_COMPONENT_HIDE (WM_WINSCP_USER + 4)
  53. static const int SessionPanelCount = 4;
  54. //---------------------------------------------------------------------------
  55. class TMutexGuard
  56. {
  57. public:
  58. TMutexGuard(HANDLE AMutex, int Message = MUTEX_RELEASE_TIMEOUT,
  59. int Timeout = 5000)
  60. {
  61. FMutex = NULL;
  62. unsigned long WaitResult = WaitForSingleObject(AMutex, Timeout);
  63. if (WaitResult == WAIT_TIMEOUT)
  64. {
  65. throw Exception(LoadStr(MUTEX_RELEASE_TIMEOUT));
  66. }
  67. else
  68. {
  69. FMutex = AMutex;
  70. }
  71. }
  72. ~TMutexGuard()
  73. {
  74. if (FMutex != NULL)
  75. {
  76. ReleaseMutex(FMutex);
  77. }
  78. }
  79. private:
  80. HANDLE FMutex;
  81. };
  82. //---------------------------------------------------------------------------
  83. class TWindowLock
  84. {
  85. public:
  86. TWindowLock(TCustomScpExplorerForm * Form) :
  87. FForm(Form)
  88. {
  89. FForm->LockWindow();
  90. }
  91. ~TWindowLock()
  92. {
  93. FForm->UnlockWindow();
  94. }
  95. private:
  96. TCustomScpExplorerForm * FForm;
  97. };
  98. //---------------------------------------------------------------------------
  99. class TAutoBatch
  100. {
  101. public:
  102. TAutoBatch(TCustomScpExplorerForm * Form) :
  103. FForm(Form)
  104. {
  105. FForm->BatchStart(FBatchStorage);
  106. }
  107. ~TAutoBatch()
  108. {
  109. FForm->BatchEnd(FBatchStorage);
  110. }
  111. private:
  112. TCustomScpExplorerForm * FForm;
  113. void * FBatchStorage;
  114. };
  115. //---------------------------------------------------------------------------
  116. struct TTransferOperationParam
  117. {
  118. TTransferOperationParam();
  119. UnicodeString TargetDirectory;
  120. bool Temp;
  121. bool DragDrop;
  122. int Options;
  123. TAutoSwitch Queue;
  124. };
  125. //---------------------------------------------------------------------------
  126. TTransferOperationParam::TTransferOperationParam()
  127. {
  128. Temp = false;
  129. DragDrop = false;
  130. Options = 0;
  131. Queue = asAuto;
  132. }
  133. //---------------------------------------------------------------------------
  134. class TTransferPresetNoteData : public TObject
  135. {
  136. public:
  137. UnicodeString Message;
  138. };
  139. //---------------------------------------------------------------------------
  140. class TTerminalNoteData : public TObject
  141. {
  142. public:
  143. TManagedTerminal * Terminal;
  144. };
  145. //---------------------------------------------------------------------------
  146. __fastcall TCustomScpExplorerForm::TCustomScpExplorerForm(TComponent* Owner):
  147. FFormRestored(false),
  148. TForm(Owner)
  149. {
  150. FInvalid = false;
  151. FCurrentSide = osRemote;
  152. FEverShown = false;
  153. FDocks = new TList();
  154. InitControls();
  155. DebugAssert(NonVisualDataModule && !NonVisualDataModule->ScpExplorer);
  156. // So that UpdateCustomCommandsToolbar called from RestoreParams works
  157. NonVisualDataModule->ScpExplorer = this;
  158. RestoreParams();
  159. ConfigurationChanged();
  160. RemoteDirView->Invalidate();
  161. FAutoOperation = false;
  162. FOnFileOperationFinished = NULL;
  163. FForceExecution = false;
  164. FIgnoreNextDialogChar = 0;
  165. FErrorList = NULL;
  166. FSynchronizeProgressForm = NULL;
  167. FProgressForm = NULL;
  168. FRefreshLocalDirectory = false;
  169. FRefreshRemoteDirectory = false;
  170. FDDExtMapFile = NULL;
  171. // CreateMutexW keeps failing with ERROR_NOACCESS
  172. FDDExtMutex = CreateMutexA(NULL, false, AnsiString(DRAG_EXT_MUTEX).c_str());
  173. DebugAssert(FDDExtMutex != NULL);
  174. FDDTargetControl = NULL;
  175. FDelayedDeletionTimer = NULL;
  176. FDelayedDeletionList = new TStringList();
  177. FDDFileList = NULL;
  178. FPendingTempSpaceWarn = false;
  179. FCapturedLog = NULL;
  180. FDragDropOperation = false;
  181. memset(&FHistoryMenu, 0, sizeof(FHistoryMenu));
  182. FAllowTransferPresetAutoSelect = true;
  183. FCopyParamDefault = L"";
  184. FSynchronizeController = NULL;
  185. FPendingQueueActionItem = NULL;
  186. FLockLevel = 0;
  187. FLockSuspendLevel = 0;
  188. FDisabledOnLockSuspend = false;
  189. FAlternativeDelete = false;
  190. FTrayIcon = new ::TTrayIcon(0);
  191. FTrayIcon->OnClick = TrayIconClick;
  192. FMaxQueueLength = 0;
  193. FLastContextPopupScreenPoint = TPoint(-1, -1);
  194. FTransferResumeList = NULL;
  195. FMoveToQueue = false;
  196. FStandaloneEditing = false;
  197. FOnFeedSynchronizeError = NULL;
  198. FNeedSession = false;
  199. FDoNotIdleCurrentTerminal = 0;
  200. FIncrementalSearching = 0;
  201. FIncrementalSearchHaveNext = false;
  202. FEditorManager = new TEditorManager();
  203. FEditorManager->OnFileChange = ExecutedFileChanged;
  204. FEditorManager->OnFileReload = ExecutedFileReload;
  205. FEditorManager->OnFileEarlyClosed = ExecutedFileEarlyClosed;
  206. FEditorManager->OnFileUploadComplete = ExecutedFileUploadComplete;
  207. FLocalEditors = new TList();
  208. FQueueStatus = NULL;
  209. FQueueStatusSection = new TCriticalSection();
  210. FQueueStatusInvalidated = false;
  211. FQueueItemInvalidated = false;
  212. FQueueActedItem = NULL;
  213. FQueueController = new TQueueController(QueueView3);
  214. FUserActionTimer = new TTimer(this);
  215. FUserActionTimer->Enabled = false;
  216. FUserActionTimer->Interval = 10;
  217. FUserActionTimer->OnTimer = UserActionTimer;
  218. FNotes = new TStringList();
  219. FNoteTimer = new TTimer(this);
  220. FNoteTimer->Enabled = false;
  221. FNoteTimer->OnTimer = NoteTimer;
  222. FOnNoteClick = NULL;
  223. FOle32Library = LoadLibrary(L"Ole32.dll");
  224. FDragMoveCursor = FOle32Library != NULL ?
  225. LoadCursor(FOle32Library, MAKEINTRESOURCE(2)) : NULL;
  226. UseSystemSettings(this);
  227. TTBXStringList * TransferList = dynamic_cast<TTBXStringList*>(
  228. static_cast<TObject*>(GetComponent(fcTransferList)));
  229. DebugAssert(TransferList != NULL);
  230. FTransferListHoverIndex = -1;
  231. TransferList->OnChange = TransferListChange;
  232. TransferList->OnDrawItem = TransferListDrawItem;
  233. SetSubmenu(dynamic_cast<TTBXCustomItem *>(static_cast<TObject *>(GetComponent(fcColorMenu))));
  234. SetSubmenu(NonVisualDataModule->ColorMenuItem);
  235. UseDesktopFont(SessionsPageControl);
  236. UpdateSessionsPageControlHeight();
  237. UseDesktopFont(RemoteDirView);
  238. UseDesktopFont(RemoteDriveView);
  239. UseDesktopFont(QueueView3);
  240. UseDesktopFont(QueueLabel);
  241. UseDesktopFont(RemoteStatusBar);
  242. reinterpret_cast<TLabel*>(QueueSplitter)->OnDblClick = QueueSplitterDblClick;
  243. QueueSplitter->ShowHint = true;
  244. RemotePanelSplitter->ShowHint = true;
  245. UpdateImages();
  246. FCustomCommandMenu = CreateTBXPopupMenu(this);
  247. FCustomCommandLocalFileList = NULL;
  248. FCustomCommandRemoteFileList = NULL;
  249. FSessionColors = new TPngImageList(this);
  250. FSessionColors->ColorDepth = cd32Bit;
  251. AddFixedSessionImages();
  252. SessionsPageControl->Images = FSessionColors;
  253. QueueLabel->FocusControl = QueueView3;
  254. UpdateQueueLabel();
  255. FRemoteDirViewWasFocused = true;
  256. CreateHiddenWindow();
  257. StartUpdates();
  258. }
  259. //---------------------------------------------------------------------------
  260. __fastcall TCustomScpExplorerForm::~TCustomScpExplorerForm()
  261. {
  262. FInvalid = true;
  263. // this has to be one of the very first things to do
  264. StopUpdateThread();
  265. // This is needed when shuting down Windows only. Otherwise it's already set NULL from Execute()
  266. TTerminalManager::Instance()->ScpExplorer = NULL;
  267. delete FCustomCommandLocalFileList;
  268. delete FCustomCommandRemoteFileList;
  269. delete FCustomCommandMenu;
  270. delete FTrayIcon;
  271. FTrayIcon = NULL;
  272. FEditorManager->CloseInternalEditors(ForceCloseInternalEditor);
  273. delete FEditorManager;
  274. ForceCloseLocalEditors();
  275. delete FLocalEditors;
  276. HideFileFindDialog();
  277. if (FDelayedDeletionTimer)
  278. {
  279. DoDelayedDeletion(NULL);
  280. SAFE_DESTROY(FDelayedDeletionTimer);
  281. }
  282. SAFE_DESTROY(FDelayedDeletionList);
  283. // sometimes we do not get DDEnd so the list is not released
  284. SAFE_DESTROY(FDDFileList);
  285. DebugAssert(FSynchronizeController == NULL);
  286. CloseHandle(FDDExtMutex);
  287. FDDExtMutex = NULL;
  288. FreeLibrary(FOle32Library);
  289. FOle32Library = NULL;
  290. FDragMoveCursor = NULL;
  291. DebugAssert(!FErrorList);
  292. if (FEverShown)
  293. {
  294. // when window is never shown (like when running command-line operation),
  295. // particularly window site is not restored correctly (BoundsRect value set
  296. // in RestoreForm gets lost during handle allocation), so we do not want
  297. // it to be stored
  298. StoreParams();
  299. }
  300. Terminal = NULL;
  301. Queue = NULL;
  302. DebugAssert(NonVisualDataModule && (NonVisualDataModule->ScpExplorer == this));
  303. NonVisualDataModule->ScpExplorer = NULL;
  304. delete FQueueController;
  305. FQueueController = NULL;
  306. delete FQueueStatusSection;
  307. FQueueStatusSection = NULL;
  308. delete FQueueStatus;
  309. FQueueStatus = NULL;
  310. delete FUserActionTimer;
  311. FUserActionTimer = NULL;
  312. delete FNoteTimer;
  313. delete FNotes;
  314. delete FNoteData;
  315. SAFE_DESTROY(FDocks);
  316. SessionsPageControl->Images = NULL;
  317. // only after clearing Terminal (after DoTerminalListChanged => ... => AddSessionColor is called)
  318. SAFE_DESTROY(FSessionColors);
  319. SAFE_DESTROY(FSessionsDragDropFilesEx);
  320. SAFE_DESTROY(FHistoryMenu[0][0]);
  321. SAFE_DESTROY(FHistoryMenu[0][1]);
  322. SAFE_DESTROY(FHistoryMenu[1][0]);
  323. SAFE_DESTROY(FHistoryMenu[1][1]);
  324. if (FHiddenWindow != NULL)
  325. {
  326. DestroyWindow(FHiddenWindow);
  327. FHiddenWindow = NULL;
  328. }
  329. }
  330. //---------------------------------------------------------------------------
  331. void __fastcall TCustomScpExplorerForm::RefreshPanel(const UnicodeString & Session, const UnicodeString & Path)
  332. {
  333. std::unique_ptr<TSessionData> Data;
  334. if (!Session.IsEmpty())
  335. {
  336. bool DefaultsOnly;
  337. Data.reset(StoredSessions->ParseUrl(Session, NULL, DefaultsOnly));
  338. }
  339. TTerminalManager * Manager = TTerminalManager::Instance();
  340. for (int Index = 0; Index < Manager->Count; Index++)
  341. {
  342. TTerminal * Terminal = Manager->Terminals[Index];
  343. if (Session.IsEmpty() ||
  344. Terminal->SessionData->IsSameSite(Data.get()))
  345. {
  346. if (Path.IsEmpty())
  347. {
  348. Terminal->ClearCaches();
  349. }
  350. else
  351. {
  352. Terminal->DirectoryModified(Path, true);
  353. }
  354. }
  355. }
  356. // We should flag a pending refresh for the background terminals or busy foreground terminals
  357. if ((Terminal != NULL) && Terminal->Active &&
  358. CanCommandLineFromAnotherInstance() &&
  359. (Session.IsEmpty() || Terminal->SessionData->IsSameSite(Data.get())) &&
  360. (Path.IsEmpty() || UnixIsChildPath(Path, Terminal->CurrentDirectory)))
  361. {
  362. Terminal->ReloadDirectory();
  363. }
  364. }
  365. //---------------------------------------------------------------------------
  366. void __fastcall TCustomScpExplorerForm::WMCopyData(TMessage & Message)
  367. {
  368. PCOPYDATASTRUCT CopyData = reinterpret_cast<PCOPYDATASTRUCT>(Message.LParam);
  369. size_t MessageSize = sizeof(TCopyDataMessage);
  370. bool Result = DebugAlwaysTrue(CopyData->cbData == MessageSize);
  371. if (Result)
  372. {
  373. const TCopyDataMessage & Message = *reinterpret_cast<const TCopyDataMessage *>(CopyData->lpData);
  374. Result = (Message.Version == TCopyDataMessage::Version1);
  375. if (Result)
  376. {
  377. switch (Message.Command)
  378. {
  379. case TCopyDataMessage::CommandCanCommandLine:
  380. Result = CanCommandLineFromAnotherInstance();
  381. break;
  382. case TCopyDataMessage::CommandCommandLine:
  383. {
  384. UnicodeString CommandLine(Message.CommandLine);
  385. Result = CommandLineFromAnotherInstance(CommandLine);
  386. }
  387. break;
  388. case TCopyDataMessage::RefreshPanel:
  389. RefreshPanel(Message.Refresh.Session, Message.Refresh.Path);
  390. break;
  391. case TCopyDataMessage::MainWindowCheck:
  392. Result = true;
  393. break;
  394. default:
  395. Result = false;
  396. break;
  397. }
  398. }
  399. }
  400. Message.Result = Result ? 1 : 0;
  401. }
  402. //---------------------------------------------------------------------------
  403. void __fastcall TCustomScpExplorerForm::CreateHiddenWindow()
  404. {
  405. WNDCLASS WindowClass = {0};
  406. WindowClass.lpfnWndProc = DefWindowProc;
  407. WindowClass.hInstance = HInstance;
  408. WindowClass.lpszClassName = HIDDEN_WINDOW_NAME;
  409. FHiddenWindow = NULL;
  410. if (RegisterClass(&WindowClass))
  411. {
  412. FHiddenWindow = CreateWindow(HIDDEN_WINDOW_NAME, L"",
  413. WS_POPUP, 0, 0, 0, 0, 0, 0, HInstance, NULL);
  414. }
  415. }
  416. //---------------------------------------------------------------------------
  417. bool __fastcall TCustomScpExplorerForm::CanConsole()
  418. {
  419. return
  420. (Terminal != NULL) &&
  421. Terminal->Active &&
  422. (Terminal->IsCapable[fcAnyCommand] || Terminal->IsCapable[fcSecondaryShell]);
  423. }
  424. //---------------------------------------------------------------------------
  425. bool __fastcall TCustomScpExplorerForm::CanCommandLineFromAnotherInstance()
  426. {
  427. bool Result = Visible && !NonVisualDataModule->Busy;
  428. return Result;
  429. }
  430. //---------------------------------------------------------------------------
  431. bool __fastcall TCustomScpExplorerForm::CommandLineFromAnotherInstance(
  432. const UnicodeString & CommandLine)
  433. {
  434. TProgramParams Params(CommandLine);
  435. bool Result = CanCommandLineFromAnotherInstance() && DebugAlwaysTrue(Params.ParamCount > 0);
  436. if (Result)
  437. {
  438. NonVisualDataModule->StartBusy();
  439. try
  440. {
  441. // this action is initiated from another process,
  442. // so it's likely that our window is not visible,
  443. // and user won't see what is going on
  444. Application->BringToFront();
  445. // reload sessions as we may be asked to open a session
  446. // just stored by another instance
  447. StoredSessions->Reload();
  448. UnicodeString SessionName = Params.Param[1];
  449. std::unique_ptr<TObjectList> DataList(new TObjectList());
  450. UnicodeString DownloadFile; // unused
  451. try
  452. {
  453. GetLoginData(SessionName, &Params, DataList.get(), DownloadFile, true, this);
  454. }
  455. catch (EAbort &)
  456. {
  457. // Happens when opening session in PuTTY and in other situations,
  458. // in which we do not want to fail WM_COPYDATA, as that would cause master instance
  459. // to process the command-line on its own (or to pass it to yet another instance)
  460. }
  461. if (DataList->Count > 0)
  462. {
  463. TTerminalManager * Manager = TTerminalManager::Instance();
  464. Manager->ActiveTerminal = Manager->NewTerminals(DataList.get());
  465. }
  466. }
  467. __finally
  468. {
  469. NonVisualDataModule->EndBusy();
  470. }
  471. }
  472. return Result;
  473. }
  474. //---------------------------------------------------------------------------
  475. void __fastcall TCustomScpExplorerForm::SetTerminal(TManagedTerminal * value)
  476. {
  477. if (FTerminal != value)
  478. {
  479. TerminalChanging();
  480. DoSetTerminal(value, false);
  481. }
  482. }
  483. //---------------------------------------------------------------------------
  484. void __fastcall TCustomScpExplorerForm::DoSetTerminal(TManagedTerminal * value, bool Replace)
  485. {
  486. FTerminal = value;
  487. bool PrevAllowTransferPresetAutoSelect = FAllowTransferPresetAutoSelect;
  488. FAllowTransferPresetAutoSelect = false;
  489. try
  490. {
  491. TerminalChanged(Replace);
  492. }
  493. __finally
  494. {
  495. FAllowTransferPresetAutoSelect = PrevAllowTransferPresetAutoSelect;
  496. }
  497. if (Terminal != NULL)
  498. {
  499. TransferPresetAutoSelect();
  500. }
  501. }
  502. //---------------------------------------------------------------------------
  503. void __fastcall TCustomScpExplorerForm::ReplaceTerminal(TManagedTerminal * value)
  504. {
  505. DoSetTerminal(value, true);
  506. }
  507. //---------------------------------------------------------------------------
  508. void __fastcall TCustomScpExplorerForm::TerminalChanging()
  509. {
  510. if (FTerminal != NULL)
  511. {
  512. UpdateTerminal(Terminal);
  513. }
  514. }
  515. //---------------------------------------------------------------------------
  516. void __fastcall TCustomScpExplorerForm::TerminalChanged(bool Replaced)
  517. {
  518. if (Terminal != NULL)
  519. {
  520. UpdateSessionColor((TColor)Terminal->StateData->Color);
  521. }
  522. DoTerminalListChanged();
  523. DebugAssert(!IsLocalBrowserMode()); // TODO
  524. if (Replaced)
  525. {
  526. RemoteDirView->ReplaceTerminal(Terminal);
  527. }
  528. else
  529. {
  530. RemoteDirView->Terminal = Terminal;
  531. }
  532. NonVisualDataModule->ResetQueueOnceEmptyOperation();
  533. if (Terminal != NULL)
  534. {
  535. if (Terminal->Active)
  536. {
  537. Terminal->RefreshDirectory();
  538. }
  539. if (WinConfiguration->PreservePanelState)
  540. {
  541. if (Terminal->RemoteExplorerState != NULL)
  542. {
  543. DirView(osRemote)->RestoreState(Terminal->RemoteExplorerState);
  544. }
  545. else
  546. {
  547. DirView(osRemote)->ClearState();
  548. }
  549. }
  550. if (!Replaced && Terminal->Active)
  551. {
  552. InitStatusBar();
  553. }
  554. }
  555. UpdateTransferList();
  556. // Update panels Enable state before refreshing the labels
  557. UpdateControls();
  558. }
  559. //---------------------------------------------------------------------------
  560. void __fastcall TCustomScpExplorerForm::SetQueue(TTerminalQueue * value)
  561. {
  562. if (Queue != value)
  563. {
  564. if (FQueue != NULL)
  565. {
  566. FQueue->OnListUpdate = NULL;
  567. FQueue->OnQueueItemUpdate = NULL;
  568. }
  569. FQueue = value;
  570. if (FQueue != NULL)
  571. {
  572. DebugAssert(FQueue->OnListUpdate == NULL);
  573. FQueue->OnListUpdate = QueueListUpdate;
  574. DebugAssert(FQueue->OnQueueItemUpdate == NULL);
  575. FQueue->OnQueueItemUpdate = QueueItemUpdate;
  576. }
  577. QueueChanged();
  578. }
  579. }
  580. //---------------------------------------------------------------------------
  581. void __fastcall TCustomScpExplorerForm::QueueView3Deletion(TObject * /*Sender*/,
  582. TListItem * Item)
  583. {
  584. if (FQueueActedItem == Item)
  585. {
  586. FQueueActedItem = NULL;
  587. if ((QueueView3->PopupMenu != NULL) &&
  588. (QueueView3->PopupMenu->PopupComponent == QueueView3))
  589. {
  590. // rather "trick", suggested by Jordan on jrsoftware.toolbar2000
  591. ReleaseCapture();
  592. }
  593. }
  594. if (Item->Data == FPendingQueueActionItem)
  595. {
  596. FPendingQueueActionItem = NULL;
  597. }
  598. }
  599. //---------------------------------------------------------------------------
  600. bool TCustomScpExplorerForm::IsAnythingQueued()
  601. {
  602. return (FQueueStatus != NULL) && (FQueueStatus->ActiveAndPendingPrimaryCount > 0);
  603. }
  604. //---------------------------------------------------------------------------
  605. void __fastcall TCustomScpExplorerForm::UpdateQueueStatus(bool QueueChanging)
  606. {
  607. {
  608. TGuard Guard(FQueueStatusSection);
  609. FQueueStatusInvalidated = false;
  610. if (FQueue != NULL)
  611. {
  612. FQueueStatus = FQueue->CreateStatus(FQueueStatus);
  613. }
  614. }
  615. if ((FQueueStatus != NULL) && (FQueueStatus->Count > FMaxQueueLength))
  616. {
  617. FMaxQueueLength = FQueueStatus->Count;
  618. Configuration->Usage->SetMax(L"MaxQueueLength", FMaxQueueLength);
  619. }
  620. FQueueController->UpdateQueueStatus(FQueueStatus);
  621. SetQueueProgress();
  622. UpdateQueueView();
  623. bool IsEmpty = !IsAnythingQueued();
  624. if (IsEmpty && (Terminal != NULL))
  625. {
  626. TOnceDoneOperation OnceDoneOperation =
  627. NonVisualDataModule->CurrentQueueOnceEmptyOperation();
  628. NonVisualDataModule->ResetQueueOnceEmptyOperation();
  629. if ((FQueue != NULL) && !WinConfiguration->EnableQueueByDefault && !QueueChanging)
  630. {
  631. FQueue->Enabled = false;
  632. }
  633. if ((OnceDoneOperation != odoIdle) && !NonVisualDataModule->Busy)
  634. {
  635. Terminal->CloseOnCompletion(OnceDoneOperation, LoadStr(CLOSED_ON_QUEUE_EMPTY));
  636. }
  637. }
  638. }
  639. //---------------------------------------------------------------------------
  640. UnicodeString __fastcall TCustomScpExplorerForm::GetQueueProgressTitle()
  641. {
  642. UnicodeString Result;
  643. if (FQueueStatus != NULL)
  644. {
  645. if (FQueueStatus->IsOnlyOneActiveAndNoPending())
  646. {
  647. TFileOperationProgressType * ProgressData =
  648. FQueueStatus->Items[FQueueStatus->DoneCount]->ProgressData;
  649. if ((ProgressData != NULL) && ProgressData->InProgress)
  650. {
  651. Result = TProgressForm::ProgressStr(NULL, ProgressData);
  652. }
  653. }
  654. else if (FQueueStatus->ActiveAndPendingPrimaryCount > 1)
  655. {
  656. Result = FMTLOAD(PROGRESS_IN_QUEUE, (FQueueStatus->ActiveAndPendingPrimaryCount));
  657. }
  658. }
  659. return Result;
  660. }
  661. //---------------------------------------------------------------------------
  662. void __fastcall TCustomScpExplorerForm::UpdateQueueView()
  663. {
  664. ComponentVisible[fcQueueView] =
  665. (WinConfiguration->QueueView.Show == qvShow) ||
  666. ((WinConfiguration->QueueView.Show == qvHideWhenEmpty) &&
  667. (FQueueStatus != NULL) && (FQueueStatus->Count > 0));
  668. }
  669. //---------------------------------------------------------------------------
  670. void __fastcall TCustomScpExplorerForm::QueueChanged()
  671. {
  672. if (FQueueStatus != NULL)
  673. {
  674. delete FQueueStatus;
  675. FQueueStatus = NULL;
  676. }
  677. UpdateQueueStatus(true);
  678. }
  679. //---------------------------------------------------------------------------
  680. void __fastcall TCustomScpExplorerForm::QueueListUpdate(TTerminalQueue * Queue)
  681. {
  682. if (FQueue == Queue)
  683. {
  684. FQueueStatusInvalidated = true;
  685. }
  686. }
  687. //---------------------------------------------------------------------------
  688. void __fastcall TCustomScpExplorerForm::QueueItemUpdate(TTerminalQueue * Queue,
  689. TQueueItem * Item)
  690. {
  691. if (FQueue == Queue)
  692. {
  693. TGuard Guard(FQueueStatusSection);
  694. // this may be running in parallel with QueueChanged
  695. if (FQueueStatus != NULL)
  696. {
  697. TQueueItemProxy * QueueItem = FQueueStatus->FindByQueueItem(Item);
  698. if ((Item->Status == TQueueItem::qsDone) && (Terminal != NULL))
  699. {
  700. FRefreshLocalDirectory = (QueueItem == NULL) ||
  701. (!QueueItem->Info->ModifiedLocal.IsEmpty());
  702. FRefreshRemoteDirectory = (QueueItem == NULL) ||
  703. (!QueueItem->Info->ModifiedRemote.IsEmpty());
  704. }
  705. if (QueueItem != NULL)
  706. {
  707. QueueItem->UserData = (void*)true;
  708. FQueueItemInvalidated = true;
  709. }
  710. }
  711. }
  712. }
  713. //---------------------------------------------------------------------------
  714. bool __fastcall TCustomScpExplorerForm::IsQueueAutoPopup()
  715. {
  716. // during standalone editing, we have no way to see/control queue,
  717. // so we have to always popup prompts automatically
  718. return FStandaloneEditing || GUIConfiguration->QueueAutoPopup;
  719. }
  720. //---------------------------------------------------------------------------
  721. void __fastcall TCustomScpExplorerForm::RefreshQueueItems()
  722. {
  723. if (FQueueStatus != NULL)
  724. {
  725. bool QueueAutoPopup = IsQueueAutoPopup();
  726. bool NeedRefresh = FQueueController->NeedRefresh();
  727. bool Refresh = FQueueItemInvalidated || NeedRefresh;
  728. FQueueItemInvalidated = false;
  729. int Limit = Refresh ? FQueueStatus->Count : FQueueStatus->DoneAndActiveCount;
  730. bool Updated = false;
  731. TQueueItemProxy * QueueItem;
  732. bool WasUserAction;
  733. for (int Index = 0; Index < Limit; Index++)
  734. {
  735. bool Update = false;
  736. QueueItem = FQueueStatus->Items[Index];
  737. WasUserAction = TQueueItem::IsUserActionStatus(QueueItem->Status);
  738. if (!NonVisualDataModule->Busy && QueueAutoPopup && WasUserAction &&
  739. (FPendingQueueActionItem == NULL))
  740. {
  741. FPendingQueueActionItem = QueueItem;
  742. FUserActionTimer->Enabled = true;
  743. }
  744. if ((bool)QueueItem->UserData)
  745. {
  746. QueueItem->UserData = (void*)false;
  747. QueueItem->Update();
  748. Updated = true;
  749. Update = true;
  750. }
  751. else if (FQueueController->QueueItemNeedsFrequentRefresh(QueueItem) || NeedRefresh)
  752. {
  753. Update = true;
  754. }
  755. if (Update)
  756. {
  757. FQueueController->RefreshQueueItem(QueueItem);
  758. }
  759. }
  760. if (Updated)
  761. {
  762. NonVisualDataModule->UpdateNonVisibleActions();
  763. SetQueueProgress();
  764. }
  765. }
  766. }
  767. //---------------------------------------------------------------------------
  768. void __fastcall TCustomScpExplorerForm::SetTaskbarListProgressState(TBPFLAG Flags)
  769. {
  770. if (FTaskbarList != NULL)
  771. {
  772. // Could now use Application->MainFormHandle, now that we implement Application->OnGetMainFormHandle
  773. FTaskbarList->SetProgressState(GetMainForm()->Handle, Flags);
  774. }
  775. }
  776. //---------------------------------------------------------------------------
  777. void __fastcall TCustomScpExplorerForm::SetTaskbarListProgressValue(int Progress)
  778. {
  779. if (Progress >= 0)
  780. {
  781. if (FTaskbarList != NULL)
  782. {
  783. FTaskbarList->SetProgressValue(GetMainForm()->Handle, Progress, 100);
  784. }
  785. }
  786. else
  787. {
  788. SetTaskbarListProgressState(TBPF_INDETERMINATE);
  789. }
  790. }
  791. //---------------------------------------------------------------------------
  792. void __fastcall TCustomScpExplorerForm::SetTaskbarListProgressValue(TFileOperationProgressType * ProgressData)
  793. {
  794. int OverallProgress;
  795. // FProgressForm is null when this is called from SetQueueProgress
  796. if ((FProgressForm != NULL) && (FProgressForm->SynchronizeProgress != NULL))
  797. {
  798. OverallProgress = FProgressForm->SynchronizeProgress->Progress(ProgressData);
  799. }
  800. else if (!TFileOperationProgressType::IsIndeterminateOperation(ProgressData->Operation))
  801. {
  802. OverallProgress = ProgressData->OverallProgress();
  803. }
  804. else
  805. {
  806. OverallProgress = -1;
  807. }
  808. SetTaskbarListProgressValue(OverallProgress);
  809. }
  810. //---------------------------------------------------------------------------
  811. void __fastcall TCustomScpExplorerForm::SetQueueProgress()
  812. {
  813. TTerminalManager::Instance()->QueueStatusUpdated();
  814. if ((FTaskbarList != NULL) && (FProgressForm == NULL))
  815. {
  816. if ((FQueueStatus != NULL) && (FQueueStatus->ActiveAndPendingPrimaryCount > 0))
  817. {
  818. if (FQueueStatus->IsOnlyOneActiveAndNoPending())
  819. {
  820. TFileOperationProgressType * ProgressData = NULL;
  821. if (FQueueStatus->Items[FQueueStatus->DoneCount] != NULL)
  822. {
  823. ProgressData = FQueueStatus->Items[FQueueStatus->DoneCount]->ProgressData;
  824. }
  825. if ((ProgressData != NULL) &&
  826. ProgressData->InProgress)
  827. {
  828. SetTaskbarListProgressValue(ProgressData);
  829. }
  830. else
  831. {
  832. SetTaskbarListProgressState(TBPF_NOPROGRESS);
  833. }
  834. }
  835. else
  836. {
  837. SetTaskbarListProgressState(TBPF_INDETERMINATE);
  838. }
  839. }
  840. else
  841. {
  842. SetTaskbarListProgressState(TBPF_NOPROGRESS);
  843. }
  844. }
  845. UpdateQueueLabel();
  846. }
  847. //---------------------------------------------------------------------------
  848. void __fastcall TCustomScpExplorerForm::UpdateQueueLabel()
  849. {
  850. UnicodeString Caption = LoadStr(QUEUE_CAPTION);
  851. if (FQueueStatus != NULL)
  852. {
  853. int ActiveAndPendingPrimaryCount = FQueueStatus->ActiveAndPendingPrimaryCount;
  854. if (ActiveAndPendingPrimaryCount > 0)
  855. {
  856. Caption = FORMAT("%s (%d)", (Caption, ActiveAndPendingPrimaryCount));
  857. }
  858. }
  859. QueueLabel->Caption = Caption;
  860. }
  861. //---------------------------------------------------------------------------
  862. void __fastcall TCustomScpExplorerForm::UpdateTransferList()
  863. {
  864. TTBXStringList * TransferList = dynamic_cast<TTBXStringList*>(
  865. static_cast<TComponent*>(GetComponent(fcTransferList)));
  866. TTBXDropDownItem * TransferDropDown = dynamic_cast<TTBXDropDownItem*>(
  867. static_cast<TComponent*>(GetComponent(fcTransferDropDown)));
  868. TransferList->Strings->BeginUpdate();
  869. try
  870. {
  871. TransferList->Strings->Assign(GUIConfiguration->CopyParamList->NameList);
  872. TransferList->Strings->Insert(0, StripHotkey(LoadStr(COPY_PARAM_DEFAULT)));
  873. TransferList->ItemIndex = GUIConfiguration->CopyParamIndex + 1;
  874. if (FTransferDropDownHint.IsEmpty())
  875. {
  876. FTransferDropDownHint = TransferDropDown->Hint;
  877. }
  878. // this way we get name for "default" settings (COPY_PARAM_DEFAULT)
  879. UnicodeString Name = TransferList->Strings->Strings[TransferList->ItemIndex];
  880. TransferDropDown->Text = StripHotkey(Name);
  881. TransferDropDown->Hint = FORMAT(L"%s|%s:\n%s",
  882. (FTransferDropDownHint, StripHotkey(Name),
  883. GUIConfiguration->CurrentCopyParam.GetInfoStr(L"; ",
  884. FLAGMASK(((Terminal != NULL) && Terminal->Active), Terminal->UsableCopyParamAttrs(0).General))));
  885. // update the label, otherwise when it is updated only on the first draw
  886. // of the list, it is drawn "bold" for some reason
  887. FTransferListHoverIndex = TransferList->ItemIndex;
  888. UpdateTransferLabel();
  889. }
  890. __finally
  891. {
  892. TransferList->Strings->EndUpdate();
  893. }
  894. }
  895. //---------------------------------------------------------------------------
  896. void __fastcall TCustomScpExplorerForm::UpdateCustomCommandsToolbar()
  897. {
  898. TTBXToolbar * Toolbar = dynamic_cast<TTBXToolbar *>(GetComponent(fcCustomCommandsBand));
  899. DebugAssert(Toolbar != NULL);
  900. NonVisualDataModule->UpdateCustomCommandsToolbar(Toolbar);
  901. }
  902. //---------------------------------------------------------------------------
  903. void __fastcall TCustomScpExplorerForm::UpdateActions()
  904. {
  905. TForm::UpdateActions();
  906. if (ComponentVisible[fcCustomCommandsBand])
  907. {
  908. UpdateCustomCommandsToolbar();
  909. }
  910. }
  911. //---------------------------------------------------------------------------
  912. void __fastcall TCustomScpExplorerForm::UpdateSessionsPageControlHeight()
  913. {
  914. SessionsPageControl->Height = SessionsPageControl->GetTabsHeight();
  915. }
  916. //---------------------------------------------------------------------------
  917. void __fastcall TCustomScpExplorerForm::ConfigurationChanged()
  918. {
  919. Color = GetBtnFaceColor();
  920. DebugAssert(Configuration && RemoteDirView);
  921. RemoteDirView->DimmHiddenFiles = WinConfiguration->DimmHiddenFiles;
  922. RemoteDirView->ShowHiddenFiles = WinConfiguration->ShowHiddenFiles;
  923. RemoteDirView->FormatSizeBytes = WinConfiguration->FormatSizeBytes;
  924. RemoteDirView->ShowInaccesibleDirectories = WinConfiguration->ShowInaccesibleDirectories;
  925. RemoteDirView->NaturalOrderNumericalSorting = WinConfiguration->NaturalOrderNumericalSorting;
  926. if (RemoteDirView->RowSelect != WinConfiguration->FullRowSelect)
  927. {
  928. RemoteDirView->RowSelect = WinConfiguration->FullRowSelect;
  929. // selection is not redrawn automatically when RowSelect changes
  930. RemoteDirView->Invalidate();
  931. }
  932. RemoteDriveView->DimmHiddenDirs = WinConfiguration->DimmHiddenFiles;
  933. RemoteDriveView->ShowHiddenDirs = WinConfiguration->ShowHiddenFiles;
  934. RemoteDriveView->ShowInaccesibleDirectories = WinConfiguration->ShowInaccesibleDirectories;
  935. RemoteDriveView->NaturalOrderNumericalSorting = WinConfiguration->NaturalOrderNumericalSorting;
  936. UpdateSessionsPageControlHeight();
  937. SetDockAllowDrag(!WinConfiguration->LockToolbars);
  938. UpdateToolbarDisplayMode();
  939. UpdateTransferList();
  940. if (Terminal != NULL)
  941. {
  942. TransferPresetAutoSelect();
  943. }
  944. UpdateQueueView();
  945. UpdateControls();
  946. // this can be called even before constuctor finishes.
  947. if (FEditorManager != NULL)
  948. {
  949. FEditorManager->ProcessFiles(FileConfigurationChanged, NULL);
  950. }
  951. // this can be called even before constuctor finishes.
  952. if (FLocalEditors != NULL)
  953. {
  954. for (int Index = 0; Index < FLocalEditors->Count; Index++)
  955. {
  956. ReconfigureEditorForm(static_cast<TForm *>(FLocalEditors->Items[Index]));
  957. }
  958. }
  959. if (ComponentVisible[fcCustomCommandsBand])
  960. {
  961. UpdateCustomCommandsToolbar();
  962. }
  963. // show only when keeping queue items forever,
  964. // otherwise, its enough to have in in the context menu
  965. QueueDeleteAllDoneQueueToolbarItem->Visible =
  966. WinConfiguration->QueueKeepDoneItems && (WinConfiguration->QueueKeepDoneItemsFor < 0);
  967. if (FFileColorsCurrent != WinConfiguration->FileColors)
  968. {
  969. TFileColorData::LoadList(WinConfiguration->FileColors, FFileColors);
  970. FFileColorsCurrent = WinConfiguration->FileColors;
  971. FileColorsChanged();
  972. }
  973. }
  974. //---------------------------------------------------------------------------
  975. void __fastcall TCustomScpExplorerForm::DoFileColorsChanged(TCustomDirView * DirView)
  976. {
  977. DirView->OnGetItemColor = FFileColors.empty() ? TDirViewGetItemColorEvent(NULL) : TDirViewGetItemColorEvent(&DirViewGetItemColor);
  978. DirView->Invalidate();
  979. }
  980. //---------------------------------------------------------------------------
  981. void __fastcall TCustomScpExplorerForm::FileColorsChanged()
  982. {
  983. DoFileColorsChanged(RemoteDirView);
  984. }
  985. //---------------------------------------------------------------------------
  986. void __fastcall TCustomScpExplorerForm::FileConfigurationChanged(
  987. const UnicodeString FileName, TEditedFileData * /*Data*/, TObject * Token,
  988. void * /*Arg*/)
  989. {
  990. if (Token != NULL)
  991. {
  992. TForm * Editor = dynamic_cast<TForm*>(Token);
  993. DebugAssert(Editor != NULL);
  994. ReconfigureEditorForm(Editor);
  995. }
  996. }
  997. //---------------------------------------------------------------------------
  998. void __fastcall TCustomScpExplorerForm::EnableDDTransferConfirmation(TObject * /*Sender*/)
  999. {
  1000. WinConfiguration->DDTransferConfirmation = asOn;
  1001. }
  1002. //---------------------------------------------------------------------------
  1003. void __fastcall TCustomScpExplorerForm::CopyParamDialogAfter(
  1004. TTransferDirection /*Direction*/, bool /*Temp*/, const UnicodeString & /*TargetDirectory*/)
  1005. {
  1006. // noop
  1007. }
  1008. //---------------------------------------------------------------------------
  1009. bool __fastcall TCustomScpExplorerForm::CopyParamDialog(
  1010. TTransferDirection Direction, TTransferType Type, bool Temp,
  1011. TStrings * FileList, UnicodeString & TargetDirectory, TGUICopyParamType & CopyParam,
  1012. bool Confirm, bool DragDrop, int Options)
  1013. {
  1014. bool Result = true;
  1015. DebugAssert(Terminal && Terminal->Active);
  1016. // these parameters are known in advance
  1017. int Params =
  1018. FLAGMASK(Type == ttMove, cpDelete);
  1019. bool ToTemp = (Temp && (Direction == tdToLocal));
  1020. if (Result && Confirm && WinConfiguration->ConfirmTransferring)
  1021. {
  1022. bool DisableNewerOnly =
  1023. (!Terminal->IsCapable[fcNewerOnlyUpload] && (Direction == tdToRemote)) ||
  1024. ToTemp;
  1025. Options |=
  1026. FLAGMASK(ToTemp, coTemp) |
  1027. FLAGMASK(!Terminal->IsCapable[fsBackgroundTransfers], coDisableQueue) |
  1028. coDoNotShowAgain;
  1029. TUsableCopyParamAttrs UsableCopyParamAttrs = Terminal->UsableCopyParamAttrs(Params);
  1030. int CopyParamAttrs = (Direction == tdToRemote ?
  1031. UsableCopyParamAttrs.Upload : UsableCopyParamAttrs.Download) |
  1032. FLAGMASK(DisableNewerOnly, cpaNoNewerOnly);
  1033. int OutputOptions =
  1034. FLAGMASK(DragDrop && (WinConfiguration->DDTransferConfirmation == asAuto),
  1035. cooDoNotShowAgain);
  1036. std::unique_ptr<TSessionData> SessionData(SessionDataForCode());
  1037. FlashOnBackground(); // Particularly when called from ClipboardFakeCreated
  1038. Result = DoCopyDialog(Direction == tdToRemote, Type == ttMove,
  1039. FileList, TargetDirectory, &CopyParam, Options, CopyParamAttrs, SessionData.get(), &OutputOptions, -1);
  1040. if (Result)
  1041. {
  1042. if (FLAGSET(OutputOptions, cooDoNotShowAgain))
  1043. {
  1044. if (DragDrop)
  1045. {
  1046. if (WinConfiguration->DDTransferConfirmation == asAuto)
  1047. {
  1048. PopupTrayBalloon(NULL, LoadStr(DD_TRANSFER_CONFIRM_OFF2), qtInformation,
  1049. NULL, 0, EnableDDTransferConfirmation, NULL);
  1050. }
  1051. WinConfiguration->DDTransferConfirmation = asOff;
  1052. }
  1053. else
  1054. {
  1055. WinConfiguration->ConfirmTransferring = false;
  1056. }
  1057. }
  1058. else
  1059. {
  1060. // User explicitly unchecked "do not show again",
  1061. // so show him the dialog the next time
  1062. if (DragDrop && (WinConfiguration->DDTransferConfirmation == asAuto))
  1063. {
  1064. WinConfiguration->DDTransferConfirmation = asOn;
  1065. }
  1066. }
  1067. CopyParamDialogAfter(Direction, Temp, TargetDirectory);
  1068. }
  1069. }
  1070. if (Result && CopyParam.Queue && !ToTemp && Terminal->IsCapable[fsBackgroundTransfers])
  1071. {
  1072. Configuration->Usage->Inc(L"TransfersOnBackground");
  1073. // these parameter are known only after transfer dialog
  1074. Params |=
  1075. (CopyParam.QueueNoConfirmation ? cpNoConfirmation : 0);
  1076. AddQueueItem(Queue, Direction, FileList, TargetDirectory, CopyParam, Params);
  1077. Result = false;
  1078. ClearTransferSourceSelection(Direction);
  1079. }
  1080. return Result;
  1081. }
  1082. //---------------------------------------------------------------------------
  1083. void __fastcall TCustomScpExplorerForm::ClearTransferSourceSelection(TTransferDirection Direction)
  1084. {
  1085. if (FOnFileOperationFinished != NULL)
  1086. {
  1087. FOnFileOperationFinished(UnicodeString(), true);
  1088. }
  1089. else
  1090. {
  1091. TOperationSide Side = ((Direction == tdToRemote) ? osLocal : osRemote);
  1092. if (HasDirView[Side])
  1093. {
  1094. DirView(Side)->SelectAll(smNone);
  1095. }
  1096. }
  1097. }
  1098. //---------------------------------------------------------------------------
  1099. void __fastcall TCustomScpExplorerForm::AddQueueItem(
  1100. TTerminalQueue * Queue, TTransferDirection Direction, TStrings * FileList,
  1101. const UnicodeString TargetDirectory, const TGUICopyParamType & CopyParam,
  1102. int Params)
  1103. {
  1104. DebugAssert(Queue != NULL);
  1105. bool SingleFile = false;
  1106. if (FileList->Count == 1)
  1107. {
  1108. if (Direction == tdToRemote)
  1109. {
  1110. UnicodeString FileName = FileList->Strings[0];
  1111. SingleFile = FileExists(ApiPath(FileName));
  1112. }
  1113. else
  1114. {
  1115. TRemoteFile * File = static_cast<TRemoteFile *>(FileList->Objects[0]);
  1116. SingleFile = !File->IsDirectory;
  1117. }
  1118. }
  1119. TQueueItem * QueueItem;
  1120. if (Direction == tdToRemote)
  1121. {
  1122. QueueItem = new TUploadQueueItem(Terminal, FileList, TargetDirectory,
  1123. &CopyParam, Params, SingleFile, CopyParam.QueueParallel);
  1124. }
  1125. else
  1126. {
  1127. QueueItem = new TDownloadQueueItem(Terminal, FileList, TargetDirectory,
  1128. &CopyParam, Params, SingleFile, CopyParam.QueueParallel);
  1129. }
  1130. AddQueueItem(Queue, QueueItem, Terminal);
  1131. }
  1132. //---------------------------------------------------------------------------
  1133. void __fastcall TCustomScpExplorerForm::AddQueueItem(TTerminalQueue * Queue, TQueueItem * QueueItem, TManagedTerminal * Terminal)
  1134. {
  1135. if (Queue->IsEmpty)
  1136. {
  1137. Terminal->QueueOperationStart = Now();
  1138. }
  1139. Queue->AddItem(QueueItem);
  1140. }
  1141. //---------------------------------------------------------------------------
  1142. void __fastcall TCustomScpExplorerForm::RestoreFormParams()
  1143. {
  1144. }
  1145. //---------------------------------------------------------------------------
  1146. void __fastcall TCustomScpExplorerForm::InitControls()
  1147. {
  1148. DebugAssert(FDocks != NULL);
  1149. for (int Index = 0; Index < ComponentCount; Index++)
  1150. {
  1151. TTBDock * Dock = dynamic_cast<TTBDock *>(Components[Index]);
  1152. if ((Dock != NULL) && (Dock->Tag == 0))
  1153. {
  1154. FDocks->Add(Dock);
  1155. }
  1156. }
  1157. CollectItemsWithTextDisplayMode(this);
  1158. }
  1159. //---------------------------------------------------------------------------
  1160. void __fastcall TCustomScpExplorerForm::RestoreParams()
  1161. {
  1162. ConfigurationChanged();
  1163. QueuePanel->Height =
  1164. LoadDimension(WinConfiguration->QueueView.Height, WinConfiguration->QueueView.HeightPixelsPerInch, this);
  1165. LoadListViewStr(QueueView3, WinConfiguration->QueueView.Layout);
  1166. QueueDock->Visible = WinConfiguration->QueueView.ToolBar;
  1167. QueueLabel->Visible = WinConfiguration->QueueView.Label;
  1168. UpdateCustomCommandsToolbar();
  1169. }
  1170. //---------------------------------------------------------------------------
  1171. void __fastcall TCustomScpExplorerForm::StoreParams()
  1172. {
  1173. WinConfiguration->QueueView.Height = QueuePanel->Height;
  1174. WinConfiguration->QueueView.HeightPixelsPerInch = GetControlPixelsPerInch(this);
  1175. WinConfiguration->QueueView.Layout = GetListViewStr(QueueView3);
  1176. WinConfiguration->QueueView.ToolBar = QueueDock->Visible;
  1177. WinConfiguration->QueueView.Label = QueueLabel->Visible;
  1178. }
  1179. //---------------------------------------------------------------------------
  1180. void __fastcall TCustomScpExplorerForm::CreateParams(TCreateParams & Params)
  1181. {
  1182. if (!FFormRestored)
  1183. {
  1184. FFormRestored = true;
  1185. RestoreFormParams();
  1186. }
  1187. TForm::CreateParams(Params);
  1188. }
  1189. //---------------------------------------------------------------------------
  1190. void __fastcall TCustomScpExplorerForm::SetDockAllowDrag(bool value)
  1191. {
  1192. DebugAssert(FDocks != NULL);
  1193. for (int Index = 0; Index < FDocks->Count; Index++)
  1194. {
  1195. static_cast<TTBDock*>(FDocks->Items[Index])->AllowDrag = value;
  1196. }
  1197. }
  1198. //---------------------------------------------------------------------------
  1199. void __fastcall TCustomScpExplorerForm::LoadToolbarsLayoutStr(UnicodeString LayoutStr, UnicodeString ButtonsStr)
  1200. {
  1201. SetDockAllowDrag(true);
  1202. while (!ButtonsStr.IsEmpty())
  1203. {
  1204. UnicodeString S = CutToChar(ButtonsStr, L';', true);
  1205. UnicodeString Name = CutToChar(S, L'=', true);
  1206. for (int ToolbarIndex = 0; ToolbarIndex < ComponentCount; ToolbarIndex++)
  1207. {
  1208. TTBCustomToolbar * Toolbar = dynamic_cast<TTBCustomToolbar *>(Components[ToolbarIndex]);
  1209. if ((Toolbar != NULL) && SameText(Name, GetToolbarKey(Toolbar->Name)))
  1210. {
  1211. while (!S.IsEmpty())
  1212. {
  1213. UnicodeString S2 = CutToChar(S, L',', true);
  1214. UnicodeString Name = CutToChar(S2, L':', true);
  1215. for (int ItemIndex = 0; ItemIndex < Toolbar->Items->Count; ItemIndex++)
  1216. {
  1217. TTBCustomItem * Item = Toolbar->Items->Items[ItemIndex];
  1218. if (SameText(GetToolbarItemName(Item), Name))
  1219. {
  1220. // forward compatibility
  1221. UnicodeString S3 = CutToChar(S2, L':', true);
  1222. bool Visible = StrToIntDef(S3, 0);
  1223. Item->Visible = Visible;
  1224. }
  1225. }
  1226. }
  1227. }
  1228. }
  1229. }
  1230. ::LoadToolbarsLayoutStr(this, LayoutStr);
  1231. SetDockAllowDrag(!WinConfiguration->LockToolbars);
  1232. }
  1233. //---------------------------------------------------------------------------
  1234. UnicodeString __fastcall TCustomScpExplorerForm::GetToolbarsLayoutStr()
  1235. {
  1236. UnicodeString Result;
  1237. SetDockAllowDrag(true);
  1238. Result = ::GetToolbarsLayoutStr(this);
  1239. SetDockAllowDrag(!WinConfiguration->LockToolbars);
  1240. return Result;
  1241. }
  1242. //---------------------------------------------------------------------------
  1243. UnicodeString __fastcall TCustomScpExplorerForm::GetToolbarItemName(TTBCustomItem * Item)
  1244. {
  1245. UnicodeString Result;
  1246. if (Item->Action != NULL)
  1247. {
  1248. Result = Item->Action->Name;
  1249. Result = RemoveSuffix(Result, L"Action");
  1250. }
  1251. else
  1252. {
  1253. Result = Item->Name;
  1254. Result = RemoveSuffix(Result, L"SubmenuItem");
  1255. Result = RemoveSuffix(Result, L"Item");
  1256. }
  1257. return Result;
  1258. }
  1259. //---------------------------------------------------------------------------
  1260. UnicodeString __fastcall TCustomScpExplorerForm::GetToolbarsButtonsStr()
  1261. {
  1262. UnicodeString Result;
  1263. for (int ToolbarIndex = 0; ToolbarIndex < ComponentCount; ToolbarIndex++)
  1264. {
  1265. UnicodeString ToolbarStr;
  1266. TTBCustomToolbar * Toolbar = dynamic_cast<TTBCustomToolbar *>(Components[ToolbarIndex]);
  1267. if ((Toolbar != NULL) && (Toolbar != QueueToolbar))
  1268. {
  1269. for (int ItemIndex = 0; ItemIndex < Toolbar->Items->Count; ItemIndex++)
  1270. {
  1271. TTBCustomItem * Item = Toolbar->Items->Items[ItemIndex];
  1272. // Currently all buttons are visible by default, so we can safely remember all hidden buttons.
  1273. // Once we introduce any buttons that are hidden by default, we would have to remember their initial state
  1274. // and save the changes here only.
  1275. if (NonVisualDataModule->IsCustomizableToolbarItem(Item) && !Item->Visible)
  1276. {
  1277. UnicodeString Name = GetToolbarItemName(Item);
  1278. DebugAssert(Name.Pos(L"TBX") == 0);
  1279. AddToList(ToolbarStr, FORMAT(L"%s:0", (Name)), L",");
  1280. }
  1281. }
  1282. }
  1283. if (!ToolbarStr.IsEmpty())
  1284. {
  1285. AddToList(Result, FORMAT(L"%s=%s", (GetToolbarKey(Toolbar->Name), ToolbarStr)), L";");
  1286. }
  1287. }
  1288. return Result;
  1289. }
  1290. //---------------------------------------------------------------------------
  1291. void __fastcall TCustomScpExplorerForm::CreateProgressForm(TSynchronizeProgress * SynchronizeProgress)
  1292. {
  1293. DebugAssert(FProgressForm == NULL);
  1294. bool AllowSkip = (Terminal != NULL) ? Terminal->IsCapable[fsSkipTransfer] : false;
  1295. FProgressForm = new TProgressForm(Application, (FTransferResumeList != NULL), AllowSkip, SynchronizeProgress);
  1296. FProgressForm->DeleteLocalToRecycleBin =
  1297. (WinConfiguration->DeleteToRecycleBin != FAlternativeDelete);
  1298. FProgressForm->DeleteRemoteToRecycleBin =
  1299. (Terminal != NULL) &&
  1300. (Terminal->SessionData->DeleteToRecycleBin != FAlternativeDelete) &&
  1301. !Terminal->SessionData->RecycleBinPath.IsEmpty();
  1302. // As progress window has delayed show now, we need to lock ourselves, (at least) until then
  1303. LockWindow();
  1304. // Actually, do not know what hides the progress once the operation finishes
  1305. // (it is possibly the SetQueueProgress - and we should not rely on that)
  1306. SetTaskbarListProgressState(TBPF_NORMAL);
  1307. }
  1308. //---------------------------------------------------------------------------
  1309. void __fastcall TCustomScpExplorerForm::DestroyProgressForm()
  1310. {
  1311. UnlockWindow();
  1312. SAFE_DESTROY(FProgressForm);
  1313. SetQueueProgress();
  1314. }
  1315. //---------------------------------------------------------------------------
  1316. void __fastcall TCustomScpExplorerForm::FileOperationProgress(
  1317. TFileOperationProgressType & ProgressData)
  1318. {
  1319. // Download to temporary local directory
  1320. if (FPendingTempSpaceWarn && ProgressData.InProgress && ProgressData.TotalSizeSet)
  1321. {
  1322. bool Continue = true;
  1323. FPendingTempSpaceWarn = false;
  1324. DoWarnLackOfTempSpace(ProgressData.Directory, ProgressData.TotalSize, Continue);
  1325. if (!Continue)
  1326. {
  1327. Abort();
  1328. }
  1329. }
  1330. // operation is being executed and we still didn't show up progress form
  1331. if (ProgressData.InProgress && !FProgressForm)
  1332. {
  1333. CreateProgressForm(NULL);
  1334. DebugAssert(FProgressForm->OnceDoneOperation == odoIdle);
  1335. FProgressForm->OnceDoneOperation = ProgressData.InitialOnceDoneOperation;
  1336. }
  1337. // operation is finished (or terminated), so we hide progress form
  1338. else if (!ProgressData.InProgress && (FProgressForm != NULL) && (FProgressForm->SynchronizeProgress == NULL))
  1339. {
  1340. DestroyProgressForm();
  1341. if (ProgressData.Operation == foCalculateSize)
  1342. {
  1343. // When calculating size before transfer, the abort caused by
  1344. // cancel flag set due to "MoveToQueue" below,
  1345. // is not propagated back to us in ExecuteFileOperation,
  1346. // as it is silently swallowed.
  1347. // So we explicitly (re)throw it here.
  1348. if (FMoveToQueue)
  1349. {
  1350. Abort();
  1351. }
  1352. }
  1353. else
  1354. {
  1355. if ((ProgressData.Cancel == csContinue) &&
  1356. !FAutoOperation)
  1357. {
  1358. OperationComplete(ProgressData.StartTime);
  1359. }
  1360. }
  1361. }
  1362. if (FProgressForm &&
  1363. // Can happen only during synchronization, when ending one of the operations
  1364. ProgressData.InProgress)
  1365. {
  1366. FProgressForm->SetProgressData(ProgressData);
  1367. DebugAssert(ProgressData.InProgress);
  1368. SetTaskbarListProgressValue(&ProgressData);
  1369. if (FProgressForm->Cancel > csContinue)
  1370. {
  1371. ProgressData.SetCancelAtLeast(FProgressForm->Cancel);
  1372. if (FProgressForm->Cancel == csCancelFile)
  1373. {
  1374. FProgressForm->ClearCancel();
  1375. }
  1376. // cancel cancels even the move
  1377. FMoveToQueue = false;
  1378. }
  1379. else if (FProgressForm->MoveToQueue)
  1380. {
  1381. FMoveToQueue = true;
  1382. ProgressData.SetCancelAtLeast(csCancel);
  1383. }
  1384. if ((FTransferResumeList != NULL) &&
  1385. ProgressData.InProgress &&
  1386. ((ProgressData.Operation == foCopy) || (ProgressData.Operation == foMove)) &&
  1387. !ProgressData.FullFileName.IsEmpty())
  1388. {
  1389. if ((FTransferResumeList->Count == 0) ||
  1390. (FTransferResumeList->Strings[FTransferResumeList->Count - 1] != ProgressData.FullFileName))
  1391. {
  1392. // note that we do not recognize directories from files here
  1393. FTransferResumeList->Add(ProgressData.FullFileName);
  1394. }
  1395. }
  1396. }
  1397. }
  1398. //---------------------------------------------------------------------------
  1399. void __fastcall TCustomScpExplorerForm::OperationComplete(
  1400. const TDateTime & StartTime)
  1401. {
  1402. if (GUIConfiguration->BeepOnFinish &&
  1403. (Now() - StartTime > GUIConfiguration->BeepOnFinishAfter))
  1404. {
  1405. UnicodeString BeepSound = GUIConfiguration->BeepSound;
  1406. DWORD Sound;
  1407. if (!ExtractFileExt(BeepSound).IsEmpty())
  1408. {
  1409. Sound = SND_FILENAME;
  1410. }
  1411. else
  1412. {
  1413. Sound = SND_ALIAS;
  1414. }
  1415. PlaySound(BeepSound.c_str(), NULL, Sound | SND_ASYNC);
  1416. }
  1417. }
  1418. //---------------------------------------------------------------------------
  1419. void __fastcall TCustomScpExplorerForm::OperationProgress(
  1420. TFileOperationProgressType & ProgressData)
  1421. {
  1422. // For example, when calling TFileOperationProgressType::Suspend in TTerminal::QueryReopen,
  1423. // we cannot recurse back to TTerminal::Idle as that may detect another error, calling "suspend" again.
  1424. TAutoNestingCounter Counter(FDoNotIdleCurrentTerminal);
  1425. FileOperationProgress(ProgressData);
  1426. }
  1427. //---------------------------------------------------------------------------
  1428. UnicodeString __fastcall TCustomScpExplorerForm::GetProgressTitle()
  1429. {
  1430. UnicodeString Result;
  1431. if (FProgressForm != NULL)
  1432. {
  1433. Result = FProgressForm->ProgressStr();
  1434. }
  1435. return Result;
  1436. }
  1437. //---------------------------------------------------------------------------
  1438. bool __fastcall TCustomScpExplorerForm::PanelOperation(TOperationSide /*Side*/,
  1439. bool DragDrop)
  1440. {
  1441. return (!DragDrop && (DropSourceControl == NULL)) ||
  1442. (DropSourceControl == DirView(osOther));
  1443. }
  1444. //---------------------------------------------------------------------------
  1445. void __fastcall TCustomScpExplorerForm::DoOperationFinished(
  1446. TFileOperation Operation, TOperationSide Side,
  1447. bool /*Temp*/, const UnicodeString & FileName, bool Success,
  1448. TOnceDoneOperation & OnceDoneOperation)
  1449. {
  1450. if (!FAutoOperation)
  1451. {
  1452. // no selection on "/upload", form servers only as event handler
  1453. // (it is not displayed)
  1454. if (PanelOperation(Side, FDragDropOperation) &&
  1455. Visible && (Operation != foCalculateSize) &&
  1456. (Operation != foGetProperties) && (Operation != foCalculateChecksum))
  1457. {
  1458. if (FOnFileOperationFinished != NULL)
  1459. {
  1460. FOnFileOperationFinished(FileName, Success);
  1461. }
  1462. else
  1463. {
  1464. TCustomDirView * DView = DirView(Side);
  1465. UnicodeString FileNameOnly = ExtractFileName(FileName, !IsSideLocalBrowser(Side));
  1466. TListItem *Item = DView->FindFileItem(FileNameOnly);
  1467. // this can happen when local drive is unplugged in the middle of the operation
  1468. if (Item != NULL)
  1469. {
  1470. if (Success) Item->Selected = false;
  1471. DView->MakeProgressVisible(Item);
  1472. }
  1473. }
  1474. }
  1475. if ((Operation == foCopy) || (Operation == foMove))
  1476. {
  1477. DebugAssert(!IsLocalBrowserMode()); // TODO
  1478. if (Side == osLocal)
  1479. {
  1480. Configuration->Usage->Inc(L"UploadedFiles");
  1481. }
  1482. else
  1483. {
  1484. Configuration->Usage->Inc(L"DownloadedFiles");
  1485. }
  1486. }
  1487. }
  1488. if (Success && (FSynchronizeController != NULL))
  1489. {
  1490. DebugAssert(!IsSideLocalBrowser(osRemote));
  1491. if (Operation == foCopy)
  1492. {
  1493. DebugAssert(Side == osLocal);
  1494. FSynchronizeController->LogOperation(soUpload, FileName);
  1495. }
  1496. else if (Operation == foDelete)
  1497. {
  1498. DebugAssert(Side == osRemote);
  1499. FSynchronizeController->LogOperation(soDelete, FileName);
  1500. }
  1501. }
  1502. if (FProgressForm)
  1503. {
  1504. OnceDoneOperation = FProgressForm->OnceDoneOperation;
  1505. }
  1506. }
  1507. //---------------------------------------------------------------------------
  1508. void __fastcall TCustomScpExplorerForm::OperationFinished(
  1509. TFileOperation Operation, TOperationSide Side,
  1510. bool Temp, const UnicodeString & FileName, Boolean Success,
  1511. TOnceDoneOperation & OnceDoneOperation)
  1512. {
  1513. TAutoNestingCounter Counter(FDoNotIdleCurrentTerminal);
  1514. DoOperationFinished(Operation, Side, Temp, FileName, Success,
  1515. OnceDoneOperation);
  1516. }
  1517. //---------------------------------------------------------------------------
  1518. bool TCustomScpExplorerForm::IsLocalBrowserMode()
  1519. {
  1520. return false;
  1521. }
  1522. //---------------------------------------------------------------------------
  1523. bool TCustomScpExplorerForm::IsSideLocalBrowser(TOperationSide)
  1524. {
  1525. return false;
  1526. }
  1527. //---------------------------------------------------------------------------
  1528. TCustomDirView * TCustomScpExplorerForm::GetCurrentLocalBrowser()
  1529. {
  1530. return NULL;
  1531. }
  1532. //---------------------------------------------------------------------------
  1533. TCustomDirView * __fastcall TCustomScpExplorerForm::DirView(TOperationSide Side)
  1534. {
  1535. DebugAssert(GetSide(Side) == osRemote);
  1536. DebugUsedParam(Side);
  1537. return RemoteDirView;
  1538. }
  1539. //---------------------------------------------------------------------------
  1540. TCustomDriveView * __fastcall TCustomScpExplorerForm::DriveView(TOperationSide Side)
  1541. {
  1542. DebugAssert(GetSide(Side) == osRemote);
  1543. DebugUsedParam(Side);
  1544. return RemoteDriveView;
  1545. }
  1546. //---------------------------------------------------------------------------
  1547. bool __fastcall TCustomScpExplorerForm::DirViewEnabled(TOperationSide Side)
  1548. {
  1549. DebugAssert(GetSide(Side) == osRemote);
  1550. DebugUsedParam(Side);
  1551. return (Terminal != NULL) && Terminal->Active;
  1552. }
  1553. //---------------------------------------------------------------------------
  1554. bool __fastcall TCustomScpExplorerForm::GetEnableFocusedOperation(
  1555. TOperationSide Side, int FilesOnly)
  1556. {
  1557. return DirView(Side)->AnyFileSelected(true, (FilesOnly != 0), true);
  1558. }
  1559. //---------------------------------------------------------------------------
  1560. bool __fastcall TCustomScpExplorerForm::GetEnableSelectedOperation(
  1561. TOperationSide Side, int FilesOnly)
  1562. {
  1563. return DirView(Side)->AnyFileSelected(false, (FilesOnly != 0), true);
  1564. }
  1565. //---------------------------------------------------------------------------
  1566. struct THistoryItemData
  1567. {
  1568. short int Side;
  1569. short int Index;
  1570. };
  1571. //---------------------------------------------------------------------------
  1572. void __fastcall TCustomScpExplorerForm::HistoryGo(TOperationSide Side, int Index)
  1573. {
  1574. DirView(Side)->HistoryGo(Index);
  1575. }
  1576. //---------------------------------------------------------------------------
  1577. void __fastcall TCustomScpExplorerForm::HistoryItemClick(System::TObject* Sender)
  1578. {
  1579. TTBCustomItem * Item = dynamic_cast<TTBCustomItem *>(Sender);
  1580. THistoryItemData Data = *reinterpret_cast<THistoryItemData*>(&(Item->Tag));
  1581. HistoryGo((TOperationSide)Data.Side, Data.Index);
  1582. }
  1583. //---------------------------------------------------------------------------
  1584. void __fastcall TCustomScpExplorerForm::UpdateHistoryMenu(TOperationSide Side,
  1585. bool Back)
  1586. {
  1587. if (FHistoryMenu[Side == osLocal][Back] != NULL)
  1588. {
  1589. TCustomDirView * DView = DirView(Side);
  1590. TTBXPopupMenu * Menu = FHistoryMenu[Side == osLocal][Back];
  1591. int ICount = Back ? DView->BackCount : DView->ForwardCount;
  1592. if (ICount > 10)
  1593. {
  1594. ICount = 10;
  1595. }
  1596. Menu->Items->Clear();
  1597. THistoryItemData Data;
  1598. Data.Side = Side;
  1599. for (short int i = 1; i <= ICount; i++)
  1600. {
  1601. TTBCustomItem * Item = new TTBXItem(Menu);
  1602. Data.Index = static_cast<short int>(i * (Back ? -1 : 1));
  1603. Item->Caption = MinimizeName(DView->HistoryPath[Data.Index], 50, !IsSideLocalBrowser(Side));
  1604. Item->Hint = DView->HistoryPath[Data.Index];
  1605. DebugAssert(sizeof(int) == sizeof(THistoryItemData));
  1606. Item->Tag = *reinterpret_cast<int*>(&Data);
  1607. Item->OnClick = HistoryItemClick;
  1608. Menu->Items->Add(Item);
  1609. }
  1610. }
  1611. }
  1612. //---------------------------------------------------------------------------
  1613. TTBXPopupMenu * __fastcall TCustomScpExplorerForm::HistoryMenu(
  1614. TOperationSide Side, bool Back)
  1615. {
  1616. if (FHistoryMenu[Side == osLocal][Back] == NULL)
  1617. {
  1618. // workaround
  1619. // In Pascal the size of TTBXPopupMenu is 132, in C++ 136,
  1620. // operator new allocates memory in Pascal code, but calls inline
  1621. // contructor in C++, leading in problems, the function does
  1622. // both in Pascal code
  1623. FHistoryMenu[Side == osLocal][Back] = CreateTBXPopupMenu(this);
  1624. UpdateHistoryMenu(Side, Back);
  1625. }
  1626. return FHistoryMenu[Side == osLocal][Back];
  1627. }
  1628. //---------------------------------------------------------------------------
  1629. void __fastcall TCustomScpExplorerForm::DirViewHistoryChange(
  1630. TCustomDirView *Sender)
  1631. {
  1632. TOperationSide Side = (Sender == DirView(osOther) ? osOther : osLocal);
  1633. UpdateHistoryMenu(Side, true);
  1634. UpdateHistoryMenu(Side, false);
  1635. }
  1636. //---------------------------------------------------------------------------
  1637. bool __fastcall TCustomScpExplorerForm::CustomCommandRemoteAllowed()
  1638. {
  1639. // remote custom commands can be executed only if the server supports shell commands
  1640. // or have secondary shell
  1641. return (FTerminal != NULL) && FTerminal->Active && (FTerminal->IsCapable[fcSecondaryShell] || FTerminal->IsCapable[fcShellAnyCommand]);
  1642. }
  1643. //---------------------------------------------------------------------------
  1644. int __fastcall TCustomScpExplorerForm::CustomCommandState(
  1645. const TCustomCommandType & Command, bool OnFocused, TCustomCommandListType ListType)
  1646. {
  1647. int Result;
  1648. TFileCustomCommand RemoteCustomCommand;
  1649. TLocalCustomCommand LocalCustomCommand;
  1650. TFileCustomCommand * NonInteractiveCustomCommand =
  1651. FLAGCLEAR(Command.Params, ccLocal) ? &RemoteCustomCommand : &LocalCustomCommand;
  1652. TInteractiveCustomCommand InteractiveCustomCommand(NonInteractiveCustomCommand);
  1653. UnicodeString Cmd = InteractiveCustomCommand.Complete(Command.Command, false);
  1654. if (FLAGCLEAR(Command.Params, ccLocal))
  1655. {
  1656. int AllowedState = CustomCommandRemoteAllowed() ? 1 : 0;
  1657. // custom command that does not operate with files can be executed anytime ...
  1658. if (!NonInteractiveCustomCommand->IsFileCommand(Cmd))
  1659. {
  1660. // ... but do not show such command in remote file menu
  1661. if ((ListType == ccltAll) || (ListType == ccltNonFile))
  1662. {
  1663. Result = AllowedState;
  1664. }
  1665. else
  1666. {
  1667. Result = -1;
  1668. }
  1669. }
  1670. else
  1671. {
  1672. if ((ListType == ccltAll) || ((ListType == ccltFile) && !IsSideLocalBrowser(FCurrentSide)))
  1673. {
  1674. Result = (DirView(FCurrentSide)->AnyFileSelected(OnFocused, false, true)) ? AllowedState : 0;
  1675. }
  1676. else
  1677. {
  1678. Result = -1;
  1679. }
  1680. }
  1681. }
  1682. else
  1683. {
  1684. // custom command that does not operate with files can be executed anytime
  1685. if (!NonInteractiveCustomCommand->IsFileCommand(Cmd))
  1686. {
  1687. Result = ((ListType == ccltAll) || (ListType == ccltNonFile)) ? 1 : -1;
  1688. }
  1689. else if (LocalCustomCommand.HasLocalFileName(Cmd))
  1690. {
  1691. if ((ListType == ccltAll) || (ListType == ccltFile))
  1692. {
  1693. // special case is "diff"-style command that can be executed over any side,
  1694. // if we have both sides
  1695. Result =
  1696. // Cannot have focus on both panels, so we have to call AnyFileSelected
  1697. // directly (instead of EnableSelectedOperation) to pass
  1698. // false to FocusedFileOnlyWhenFocused when panel is inactive.
  1699. ((HasDirView[osLocal] && !IsSideLocalBrowser(osRemote) &&
  1700. DirView(osLocal)->AnyFileSelected(false, false, (FCurrentSide == osLocal))) &&
  1701. DirView(osRemote)->AnyFileSelected(false, false, (FCurrentSide == osRemote))) ? 1 : 0;
  1702. }
  1703. else if (ListType == ccltBoth)
  1704. {
  1705. DebugAssert(!IsLocalBrowserMode());
  1706. Result = 1;
  1707. }
  1708. else
  1709. {
  1710. Result = -1;
  1711. }
  1712. }
  1713. else
  1714. {
  1715. if ((ListType == ccltAll) ||
  1716. ((ListType == ccltFile) && (!IsSideLocalBrowser(FCurrentSide) || FLAGCLEAR(Command.Params, ccRemoteFiles))))
  1717. {
  1718. Result = DirView(FCurrentSide)->AnyFileSelected(OnFocused, false, true) ? 1 : 0;
  1719. }
  1720. else
  1721. {
  1722. Result = -1;
  1723. }
  1724. }
  1725. }
  1726. return Result;
  1727. }
  1728. //---------------------------------------------------------------------------
  1729. void __fastcall TCustomScpExplorerForm::RemoteCustomCommand(
  1730. TStrings * FileList, const TCustomCommandType & ACommand,
  1731. const TCustomCommandData & Data, const UnicodeString & CommandCommand)
  1732. {
  1733. if (EnsureCommandSessionFallback(fcShellAnyCommand))
  1734. {
  1735. TRemoteCustomCommand RemoteCustomCommand(Data, Terminal->CurrentDirectory);
  1736. TWinInteractiveCustomCommand InteractiveCustomCommand(
  1737. &RemoteCustomCommand, ACommand.Name, ACommand.HomePage);
  1738. UnicodeString Command = InteractiveCustomCommand.Complete(CommandCommand, false);
  1739. Configuration->Usage->Inc(L"RemoteCustomCommandRuns2");
  1740. bool Capture =
  1741. FLAGSET(ACommand.Params, ccShowResults) ||
  1742. FLAGSET(ACommand.Params, ccCopyResults) ||
  1743. FLAGSET(ACommand.Params, ccShowResultsInMsgBox);
  1744. TCaptureOutputEvent OutputEvent = NULL;
  1745. DebugAssert(FCapturedLog == NULL);
  1746. if (Capture)
  1747. {
  1748. FCapturedLog = new TStringList();
  1749. OutputEvent = TerminalCaptureLog;
  1750. }
  1751. try
  1752. {
  1753. if (!RemoteCustomCommand.IsFileCommand(Command))
  1754. {
  1755. Terminal->AnyCommand(RemoteCustomCommand.Complete(Command, true),
  1756. OutputEvent);
  1757. }
  1758. else
  1759. {
  1760. Terminal->CustomCommandOnFiles(Command, ACommand.Params, FileList, OutputEvent);
  1761. }
  1762. if ((FCapturedLog != NULL) && (FCapturedLog->Count > 0))
  1763. {
  1764. if (FLAGSET(ACommand.Params, ccCopyResults))
  1765. {
  1766. CopyToClipboard(FCapturedLog);
  1767. }
  1768. if (FLAGSET(ACommand.Params, ccShowResults))
  1769. {
  1770. DoConsoleDialog(Terminal, L"", FCapturedLog);
  1771. }
  1772. else if (FLAGSET(ACommand.Params, ccShowResultsInMsgBox))
  1773. {
  1774. MessageDialog(FCapturedLog->Text, qtInformation, qaOK);
  1775. }
  1776. }
  1777. }
  1778. __finally
  1779. {
  1780. SAFE_DESTROY(FCapturedLog);
  1781. }
  1782. }
  1783. }
  1784. //---------------------------------------------------------------------------
  1785. void __fastcall TCustomScpExplorerForm::LocalCustomCommandPure(
  1786. TStrings * FileList, const TCustomCommandType & ACommand, const UnicodeString & Command, TStrings * ALocalFileList,
  1787. const TCustomCommandData & Data, bool LocalFileCommand, bool FileListCommand, UnicodeString * POutput)
  1788. {
  1789. DebugAssert(!IsLocalBrowserMode());
  1790. TStrings * LocalFileList = NULL;
  1791. TStrings * RemoteFileList = NULL;
  1792. TStrings * RemoteFileListFull = NULL;
  1793. try
  1794. {
  1795. if (LocalFileCommand)
  1796. {
  1797. if (ALocalFileList == NULL)
  1798. {
  1799. DebugAssert(HasDirView[osLocal]);
  1800. // Cannot have focus on both panels, so we have to call AnyFileSelected
  1801. // directly (instead of EnableSelectedOperation) to pass
  1802. // false to FocusedFileOnlyWhenFocused
  1803. DebugAssert(DirView(osLocal)->AnyFileSelected(false, false, false));
  1804. LocalFileList = DirView(osLocal)->CreateFileList(false, true, NULL);
  1805. }
  1806. else
  1807. {
  1808. LocalFileList = ALocalFileList;
  1809. }
  1810. if (FileListCommand)
  1811. {
  1812. if (LocalFileList->Count != 1)
  1813. {
  1814. throw Exception(LoadStr(CUSTOM_COMMAND_SELECTED_UNMATCH1));
  1815. }
  1816. }
  1817. else
  1818. {
  1819. if ((LocalFileList->Count != 1) &&
  1820. (FileList->Count != 1) &&
  1821. (LocalFileList->Count != FileList->Count))
  1822. {
  1823. throw Exception(LoadStr(CUSTOM_COMMAND_SELECTED_UNMATCH));
  1824. }
  1825. }
  1826. }
  1827. UnicodeString RootTempDir;
  1828. UnicodeString TempDir;
  1829. bool RemoteFiles = FLAGSET(ACommand.Params, ccRemoteFiles);
  1830. if (!RemoteFiles)
  1831. {
  1832. TemporarilyDownloadFiles(FileList, false, RootTempDir, TempDir, false, false, true);
  1833. }
  1834. try
  1835. {
  1836. TDateTimes RemoteFileTimes;
  1837. if (RemoteFiles)
  1838. {
  1839. RemoteFileList = FileList;
  1840. }
  1841. else
  1842. {
  1843. RemoteFileList = new TStringList();
  1844. TMakeLocalFileListParams MakeFileListParam;
  1845. MakeFileListParam.FileList = RemoteFileList;
  1846. MakeFileListParam.FileTimes = &RemoteFileTimes;
  1847. MakeFileListParam.IncludeDirs = FLAGSET(ACommand.Params, ccApplyToDirectories);
  1848. MakeFileListParam.Recursive =
  1849. FLAGSET(ACommand.Params, ccRecursive) && !FileListCommand;
  1850. ProcessLocalDirectory(TempDir, Terminal->MakeLocalFileList, &MakeFileListParam);
  1851. if (!MakeFileListParam.Recursive)
  1852. {
  1853. RemoteFileTimes.clear();
  1854. RemoteFileListFull = new TStringList();
  1855. MakeFileListParam.FileList = RemoteFileListFull;
  1856. MakeFileListParam.Recursive = true;
  1857. MakeFileListParam.IncludeDirs = false;
  1858. ProcessLocalDirectory(TempDir, Terminal->MakeLocalFileList, &MakeFileListParam);
  1859. }
  1860. else
  1861. {
  1862. RemoteFileListFull = RemoteFileList;
  1863. }
  1864. }
  1865. bool NonBlocking = FileListCommand && RemoteFiles && !POutput;
  1866. TFileOperationProgressType Progress(&OperationProgress, &OperationFinished);
  1867. if (!NonBlocking)
  1868. {
  1869. int Count;
  1870. if (FileListCommand)
  1871. {
  1872. Count = 1;
  1873. }
  1874. else if (LocalFileCommand)
  1875. {
  1876. Count = std::max(LocalFileList->Count, RemoteFileList->Count);
  1877. }
  1878. else
  1879. {
  1880. Count = RemoteFileList->Count;
  1881. }
  1882. Progress.Start(foCustomCommand, osRemote, Count);
  1883. DebugAssert(FProgressForm != NULL);
  1884. FProgressForm->ReadOnly = true;
  1885. }
  1886. try
  1887. {
  1888. if (FileListCommand)
  1889. {
  1890. UnicodeString LocalFile;
  1891. // MakeFileList does not delimit filenames
  1892. UnicodeString FileList = MakeFileList(RemoteFileList);
  1893. if (LocalFileCommand)
  1894. {
  1895. DebugAssert(LocalFileList->Count == 1);
  1896. LocalFile = LocalFileList->Strings[0];
  1897. }
  1898. TLocalCustomCommand CustomCommand(Data,
  1899. Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(), L"", LocalFile, FileList);
  1900. UnicodeString ShellCommand = CustomCommand.Complete(Command, true);
  1901. if (NonBlocking)
  1902. {
  1903. DebugAssert(!POutput);
  1904. ExecuteShellChecked(ShellCommand);
  1905. }
  1906. else
  1907. {
  1908. ExecuteProcessCheckedAndWait(ShellCommand, HelpKeyword, POutput);
  1909. }
  1910. }
  1911. else if (LocalFileCommand)
  1912. {
  1913. if (LocalFileList->Count == 1)
  1914. {
  1915. UnicodeString LocalFile = LocalFileList->Strings[0];
  1916. for (int Index = 0; Index < RemoteFileList->Count; Index++)
  1917. {
  1918. UnicodeString FileName = RemoteFileList->Strings[Index];
  1919. UnicodeString FileNameForProgress = RemoteFiles ? FileName : ExtractFileName(FileName);
  1920. Progress.SetFile(FileNameForProgress);
  1921. TLocalCustomCommand CustomCommand(Data,
  1922. Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(), FileName, LocalFile, L"");
  1923. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  1924. TOnceDoneOperation OnceDoneOperation;
  1925. // Do not unselect anything, as it won't work with two panels anyway
  1926. Progress.Finish(UnicodeString(), true, OnceDoneOperation);
  1927. }
  1928. }
  1929. else if (RemoteFileList->Count == 1)
  1930. {
  1931. UnicodeString FileName = RemoteFileList->Strings[0];
  1932. for (int Index = 0; Index < LocalFileList->Count; Index++)
  1933. {
  1934. UnicodeString LocalFileName = LocalFileList->Strings[Index];
  1935. UnicodeString LocalFileNameOnly = ExtractFileName(LocalFileName);
  1936. Progress.SetFile(LocalFileNameOnly);
  1937. TLocalCustomCommand CustomCommand(
  1938. Data, Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(),
  1939. FileName, LocalFileName, L"");
  1940. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  1941. TOnceDoneOperation OnceDoneOperation;
  1942. // Do not unselect anything, as it won't work with two panels anyway
  1943. Progress.Finish(UnicodeString(), true, OnceDoneOperation);
  1944. }
  1945. }
  1946. else
  1947. {
  1948. if (LocalFileList->Count != RemoteFileList->Count)
  1949. {
  1950. throw Exception(LoadStr(CUSTOM_COMMAND_PAIRS_DOWNLOAD_FAILED));
  1951. }
  1952. for (int Index = 0; Index < LocalFileList->Count; Index++)
  1953. {
  1954. UnicodeString FileName = RemoteFileList->Strings[Index];
  1955. UnicodeString LocalFileName = LocalFileList->Strings[Index];
  1956. UnicodeString LocalFileNameOnly = ExtractFileName(LocalFileName);
  1957. Progress.SetFile(LocalFileNameOnly); // sic
  1958. TLocalCustomCommand CustomCommand(
  1959. Data, Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(),
  1960. FileName, LocalFileName, L"");
  1961. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  1962. TOnceDoneOperation OnceDoneOperation;
  1963. // Do not unselect anything, as it won't work with two panels anyway
  1964. Progress.Finish(UnicodeString(), true, OnceDoneOperation);
  1965. }
  1966. }
  1967. }
  1968. else
  1969. {
  1970. for (int Index = 0; Index < RemoteFileList->Count; Index++)
  1971. {
  1972. UnicodeString FileName = RemoteFileList->Strings[Index];
  1973. UnicodeString FileNameForProgress = RemoteFiles ? FileName : ExtractFileName(FileName);
  1974. Progress.SetFile(FileNameForProgress);
  1975. TLocalCustomCommand CustomCommand(Data,
  1976. Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(),
  1977. FileName, L"", L"");
  1978. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  1979. TOnceDoneOperation OnceDoneOperation;
  1980. Progress.Finish(FileNameForProgress, true, OnceDoneOperation);
  1981. }
  1982. }
  1983. }
  1984. __finally
  1985. {
  1986. if (!NonBlocking)
  1987. {
  1988. Progress.Stop();
  1989. }
  1990. }
  1991. DebugAssert(!FAutoOperation);
  1992. if (!RemoteFiles)
  1993. {
  1994. TempDir = IncludeTrailingBackslash(TempDir);
  1995. for (int Index = 0; Index < RemoteFileListFull->Count; Index++)
  1996. {
  1997. UnicodeString FileName = RemoteFileListFull->Strings[Index];
  1998. if (DebugAlwaysTrue(SameText(TempDir, FileName.SubString(1, TempDir.Length()))) &&
  1999. // Skip directories, process only nested files.
  2000. // The check is redundant as FileAge fails for directories anyway.
  2001. !DirectoryExists(FileName))
  2002. {
  2003. UnicodeString RemoteDir =
  2004. UnixExtractFileDir(
  2005. UnixIncludeTrailingBackslash(FTerminal->CurrentDirectory) +
  2006. ToUnixPath(FileName.SubString(TempDir.Length() + 1, FileName.Length() - TempDir.Length())));
  2007. TDateTime NewTime;
  2008. if (FileAge(FileName, NewTime) &&
  2009. (NewTime != RemoteFileTimes[Index]))
  2010. {
  2011. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  2012. TemporaryFileCopyParam(CopyParam);
  2013. CopyParam.FileMask = L"";
  2014. FAutoOperation = true;
  2015. std::unique_ptr<TStrings> TemporaryFilesList(new TStringList());
  2016. TemporaryFilesList->Add(FileName);
  2017. FTerminal->CopyToRemote(TemporaryFilesList.get(), RemoteDir, &CopyParam, cpTemporary, NULL);
  2018. }
  2019. }
  2020. }
  2021. }
  2022. }
  2023. __finally
  2024. {
  2025. FAutoOperation = false;
  2026. if (!RootTempDir.IsEmpty() && DebugAlwaysTrue(!RemoteFiles))
  2027. {
  2028. RecursiveDeleteFile(ExcludeTrailingBackslash(RootTempDir), false);
  2029. }
  2030. }
  2031. }
  2032. __finally
  2033. {
  2034. if (RemoteFileList != FileList)
  2035. {
  2036. delete RemoteFileList;
  2037. }
  2038. if (LocalFileList != ALocalFileList)
  2039. {
  2040. delete LocalFileList;
  2041. }
  2042. if (RemoteFileListFull != RemoteFileList)
  2043. {
  2044. delete RemoteFileListFull;
  2045. }
  2046. }
  2047. }
  2048. //---------------------------------------------------------------------------
  2049. void __fastcall TCustomScpExplorerForm::LocalCustomCommandWithLocalFiles(
  2050. const TCustomCommandType & ACommand, const UnicodeString & Command, const TCustomCommandData & Data,
  2051. bool FileListCommand, UnicodeString * POutput)
  2052. {
  2053. std::unique_ptr<TStrings> SelectedFileList(DirView(osLocal)->CreateFileList(false, true, NULL));
  2054. std::unique_ptr<TStrings> LocalFileList(new TStringList());
  2055. for (int Index = 0; Index < SelectedFileList->Count; Index++)
  2056. {
  2057. UnicodeString FileName = SelectedFileList->Strings[Index];
  2058. if (DirectoryExists(FileName))
  2059. {
  2060. if (FLAGSET(ACommand.Params, ccApplyToDirectories))
  2061. {
  2062. LocalFileList->Add(FileName);
  2063. }
  2064. if (FLAGSET(ACommand.Params, ccRecursive))
  2065. {
  2066. TMakeLocalFileListParams MakeFileListParam;
  2067. MakeFileListParam.FileList = LocalFileList.get();
  2068. MakeFileListParam.FileTimes = NULL;
  2069. MakeFileListParam.IncludeDirs = FLAGSET(ACommand.Params, ccApplyToDirectories);
  2070. MakeFileListParam.Recursive = true;
  2071. ProcessLocalDirectory(FileName, Terminal->MakeLocalFileList, &MakeFileListParam);
  2072. }
  2073. }
  2074. else
  2075. {
  2076. LocalFileList->Add(FileName);
  2077. }
  2078. }
  2079. UnicodeString RemotePath;
  2080. if (Terminal != NULL)
  2081. {
  2082. RemotePath = Terminal->CurrentDirectory;
  2083. }
  2084. if (FileListCommand)
  2085. {
  2086. UnicodeString FileList = MakeFileList(LocalFileList.get());
  2087. TLocalCustomCommand CustomCommand(
  2088. Data, RemotePath, DefaultDownloadTargetDirectory(),
  2089. L"", L"", FileList);
  2090. ExecuteProcessChecked(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  2091. }
  2092. else
  2093. {
  2094. TFileOperationProgressType Progress(&OperationProgress, &OperationFinished);
  2095. Progress.Start(foCustomCommand, osLocal, LocalFileList->Count);
  2096. DebugAssert(FProgressForm != NULL);
  2097. FProgressForm->ReadOnly = true;
  2098. try
  2099. {
  2100. for (int Index = 0; Index < LocalFileList->Count; Index++)
  2101. {
  2102. UnicodeString FileName = LocalFileList->Strings[Index];
  2103. Progress.SetFile(FileName);
  2104. TLocalCustomCommand CustomCommand(
  2105. Data, RemotePath, DefaultDownloadTargetDirectory(),
  2106. FileName, L"", L"");
  2107. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  2108. TOnceDoneOperation OnceDoneOperation;
  2109. Progress.Finish(FileName, true, OnceDoneOperation);
  2110. }
  2111. }
  2112. __finally
  2113. {
  2114. Progress.Stop();
  2115. }
  2116. }
  2117. }
  2118. //---------------------------------------------------------------------------
  2119. void __fastcall TCustomScpExplorerForm::LocalCustomCommand(TStrings * FileList,
  2120. const TCustomCommandType & ACommand, TStrings * ALocalFileList,
  2121. const TCustomCommandData & Data, const UnicodeString & CommandCommand)
  2122. {
  2123. UnicodeString RemotePath = (Terminal != NULL) ? Terminal->CurrentDirectory : UnicodeString();
  2124. TLocalCustomCommand LocalCustomCommand(Data, RemotePath, DefaultDownloadTargetDirectory());
  2125. TWinInteractiveCustomCommand InteractiveCustomCommand(
  2126. &LocalCustomCommand, ACommand.Name, ACommand.HomePage);
  2127. UnicodeString Command = InteractiveCustomCommand.Complete(CommandCommand, false);
  2128. bool FileListCommand = LocalCustomCommand.IsFileListCommand(Command);
  2129. bool LocalFileCommand = LocalCustomCommand.HasLocalFileName(Command);
  2130. Configuration->Usage->Inc(L"LocalCustomCommandRuns2");
  2131. std::unique_ptr<UnicodeString> POutput;
  2132. if (FLAGSET(ACommand.Params, ccShowResultsInMsgBox) ||
  2133. FLAGSET(ACommand.Params, ccCopyResults))
  2134. {
  2135. POutput.reset(new UnicodeString());
  2136. }
  2137. if (!LocalCustomCommand.IsFileCommand(Command))
  2138. {
  2139. ExecuteProcessChecked(LocalCustomCommand.Complete(Command, true), HelpKeyword, POutput.get());
  2140. }
  2141. // remote files?
  2142. else if ((FCurrentSide == osRemote) || LocalFileCommand)
  2143. {
  2144. LocalCustomCommandPure(FileList, ACommand, Command, ALocalFileList, Data, LocalFileCommand, FileListCommand, POutput.get());
  2145. }
  2146. // local files
  2147. else
  2148. {
  2149. LocalCustomCommandWithLocalFiles(ACommand, Command, Data, FileListCommand, POutput.get());
  2150. }
  2151. if (POutput.get() != NULL)
  2152. {
  2153. // If the output is single-line, we do not want the trailing CRLF, as the CopyToClipboard(TStrings *) does
  2154. int P = POutput->Pos(sLineBreak);
  2155. if (P == POutput->Length() - static_cast<int>(strlen(sLineBreak)) + 1)
  2156. {
  2157. POutput->SetLength(P - 1);
  2158. }
  2159. // Copy even empty output to clipboard
  2160. if (FLAGSET(ACommand.Params, ccCopyResults))
  2161. {
  2162. CopyToClipboard(*POutput);
  2163. }
  2164. // But do not show an empty message box.
  2165. // This way the ShowResultsInMsgBox can be used to suppress a console window of commands with no output
  2166. if (FLAGSET(ACommand.Params, ccShowResultsInMsgBox) &&
  2167. !POutput->IsEmpty())
  2168. {
  2169. TClipboardHandler ClipboardHandler;
  2170. ClipboardHandler.Text = *POutput;
  2171. TMessageParams Params;
  2172. TQueryButtonAlias Aliases[1];
  2173. Aliases[0].Button = qaRetry;
  2174. Aliases[0].Alias = LoadStr(EDIT_COPY);
  2175. Aliases[0].OnSubmit = &ClipboardHandler.Copy;
  2176. Params.Aliases = Aliases;
  2177. Params.AliasesCount = LENOF(Aliases);
  2178. MessageDialog(*POutput, qtInformation, qaOK | qaRetry, HelpKeyword, &Params);
  2179. }
  2180. }
  2181. }
  2182. //---------------------------------------------------------------------------
  2183. void __fastcall TCustomScpExplorerForm::CustomCommand(TStrings * FileList,
  2184. const TCustomCommandType & ACommand, TStrings * ALocalFileList)
  2185. {
  2186. TCustomCommandData Data;
  2187. UnicodeString Site;
  2188. UnicodeString RemotePath;
  2189. if (Terminal != NULL)
  2190. {
  2191. Data = TCustomCommandData(Terminal);
  2192. Site = Terminal->SessionData->SessionKey;
  2193. RemotePath = Terminal->CurrentDirectory;
  2194. }
  2195. UnicodeString HelpKeyword = ACommand.HomePage;
  2196. std::unique_ptr<TStrings> CustomCommandOptions(CloneStrings(WinConfiguration->CustomCommandOptions));
  2197. if (ACommand.AnyOptionWithFlag(TCustomCommandType::ofRun))
  2198. {
  2199. std::unique_ptr<TCustomCommand> CustomCommandForOptions;
  2200. if (FLAGCLEAR(ACommand.Params, ccLocal))
  2201. {
  2202. CustomCommandForOptions.reset(new TRemoteCustomCommand(Data, RemotePath));
  2203. }
  2204. else
  2205. {
  2206. CustomCommandForOptions.reset(new TLocalCustomCommand(Data, RemotePath, DefaultDownloadTargetDirectory()));
  2207. }
  2208. if (!DoCustomCommandOptionsDialog(
  2209. &ACommand, CustomCommandOptions.get(), NULL, TCustomCommandType::ofRun, CustomCommandForOptions.get(), Site, NULL))
  2210. {
  2211. Abort();
  2212. }
  2213. }
  2214. UnicodeString CommandCommand = ACommand.GetCommandWithExpandedOptions(CustomCommandOptions.get(), Site);
  2215. if (FLAGCLEAR(ACommand.Params, ccLocal))
  2216. {
  2217. RemoteCustomCommand(FileList, ACommand, Data, CommandCommand);
  2218. }
  2219. else
  2220. {
  2221. LocalCustomCommand(FileList, ACommand, ALocalFileList, Data, CommandCommand);
  2222. }
  2223. }
  2224. //---------------------------------------------------------------------------
  2225. void __fastcall TCustomScpExplorerForm::BothCustomCommand(
  2226. const TCustomCommandType & Command)
  2227. {
  2228. DebugAssert(FCustomCommandLocalFileList != NULL);
  2229. DebugAssert(FCustomCommandRemoteFileList != NULL);
  2230. DebugAssert(FCustomCommandLocalFileList->Count == FCustomCommandRemoteFileList->Count);
  2231. TStrings * LocalFileList = new TStringList();
  2232. TStrings * RemoteFileList = new TStringList();
  2233. try
  2234. {
  2235. for (int Index = 0; Index < FCustomCommandLocalFileList->Count; Index++)
  2236. {
  2237. LocalFileList->Clear();
  2238. LocalFileList->AddObject(
  2239. FCustomCommandLocalFileList->Strings[Index],
  2240. FCustomCommandLocalFileList->Objects[Index]);
  2241. RemoteFileList->Clear();
  2242. RemoteFileList->AddObject(
  2243. FCustomCommandRemoteFileList->Strings[Index],
  2244. FCustomCommandRemoteFileList->Objects[Index]);
  2245. CustomCommand(RemoteFileList, Command, LocalFileList);
  2246. }
  2247. }
  2248. __finally
  2249. {
  2250. delete LocalFileList;
  2251. delete RemoteFileList;
  2252. }
  2253. }
  2254. //---------------------------------------------------------------------------
  2255. void __fastcall TCustomScpExplorerForm::CustomCommandMenu(
  2256. TAction * Action, TStrings * LocalFileList, TStrings * RemoteFileList)
  2257. {
  2258. delete FCustomCommandLocalFileList;
  2259. delete FCustomCommandRemoteFileList;
  2260. // takeover ownership,
  2261. // the lists must survive the MenuPopup as OnClick occurs only after it exits
  2262. FCustomCommandLocalFileList = LocalFileList;
  2263. FCustomCommandRemoteFileList = RemoteFileList;
  2264. TButton * Button = dynamic_cast<TButton *>(Action->ActionComponent);
  2265. if (Button != NULL)
  2266. {
  2267. FCustomCommandMenu->Items->Clear();
  2268. NonVisualDataModule->CreateCustomCommandsMenu(FCustomCommandMenu->Items, false, false, ccltBoth, NULL);
  2269. MenuPopup(FCustomCommandMenu, Button);
  2270. }
  2271. else
  2272. {
  2273. NonVisualDataModule->CreateCustomCommandsMenu(Action, false, ccltBoth);
  2274. }
  2275. }
  2276. //---------------------------------------------------------------------------
  2277. void __fastcall TCustomScpExplorerForm::TerminalCaptureLog(
  2278. const UnicodeString & AddedLine, TCaptureOutputType OutputType)
  2279. {
  2280. DebugAssert(FCapturedLog != NULL);
  2281. if ((OutputType == cotOutput) || (OutputType == cotError))
  2282. {
  2283. FCapturedLog->Add(AddedLine);
  2284. }
  2285. }
  2286. //---------------------------------------------------------------------------
  2287. bool __fastcall TCustomScpExplorerForm::IsFileControl(TObject * Control,
  2288. TOperationSide Side)
  2289. {
  2290. return
  2291. (Side == osOther) &&
  2292. ((Control == DirView(osOther)) || (Control == DriveView(osOther)));
  2293. }
  2294. //---------------------------------------------------------------------------
  2295. void __fastcall TCustomScpExplorerForm::DirViewContextPopupDefaultItem(
  2296. TOperationSide Side, TTBXCustomItem * Item, TDoubleClickAction DoubleClickAction)
  2297. {
  2298. TTBItemOptions O;
  2299. O = Item->Options;
  2300. TCustomDirView * DView = DirView(Side);
  2301. if ((DView->ItemFocused != NULL) &&
  2302. (WinConfiguration->DoubleClickAction == DoubleClickAction) &&
  2303. // when resolving links is disabled, default action is to enter the directory,
  2304. // no matter what DoubleClickAction is configured to
  2305. (IsSideLocalBrowser(Side) || Terminal->ResolvingSymlinks || Terminal->IsEncryptingFiles()) &&
  2306. // Can only Edit files, but can Open/Copy even directories
  2307. ((DoubleClickAction != dcaEdit) ||
  2308. !DView->ItemIsDirectory(DView->ItemFocused)))
  2309. {
  2310. Item->Options = O << tboDefault;
  2311. }
  2312. else
  2313. {
  2314. Item->Options = O >> tboDefault;
  2315. }
  2316. }
  2317. //---------------------------------------------------------------------------
  2318. void __fastcall TCustomScpExplorerForm::DirViewContextPopup(
  2319. TOperationSide Side, Byte PopupComponent, const TPoint &MousePos)
  2320. {
  2321. TCustomDirView * DView = DirView(Side);
  2322. TListItem * Item = DView->ItemFocused;
  2323. if ((DView->GetItemAt(MousePos.x, MousePos.y) == Item) &&
  2324. EnableFocusedOperation[Side])
  2325. {
  2326. TPoint ClientPoint;
  2327. ClientPoint = ((MousePos.x < 0) && (MousePos.y < 0)) ?
  2328. TPoint(0, 0) : MousePos;
  2329. FLastContextPopupScreenPoint = DView->ClientToScreen(ClientPoint);
  2330. reinterpret_cast<TPopupMenu*>(GetComponent(PopupComponent))->Popup(
  2331. FLastContextPopupScreenPoint.x, FLastContextPopupScreenPoint.y);
  2332. }
  2333. }
  2334. //---------------------------------------------------------------------------
  2335. void __fastcall TCustomScpExplorerForm::RemoteDirViewContextPopup(
  2336. TObject * /*Sender*/, const TPoint &MousePos, bool &Handled)
  2337. {
  2338. DirViewContextPopupDefaultItem(osRemote, NonVisualDataModule->RemoteOpenMenuItem, dcaOpen);
  2339. DirViewContextPopupDefaultItem(osRemote, NonVisualDataModule->RemoteEditMenuItem, dcaEdit);
  2340. DirViewContextPopupDefaultItem(osRemote, NonVisualDataModule->RemoteCopyMenuItem, dcaCopy);
  2341. DirViewContextPopup(osRemote, fcRemotePopup, MousePos);
  2342. Handled = true;
  2343. }
  2344. //---------------------------------------------------------------------------
  2345. void __fastcall TCustomScpExplorerForm::ReloadLocalDirectory(const UnicodeString Directory)
  2346. {
  2347. }
  2348. //---------------------------------------------------------------------------
  2349. void __fastcall TCustomScpExplorerForm::BatchStart(void *& /*Storage*/)
  2350. {
  2351. DebugAssert(FErrorList == NULL);
  2352. if (WinConfiguration->ContinueOnError)
  2353. {
  2354. FErrorList = new TStringList();
  2355. Configuration->Usage->Inc(L"ContinuationsOnError");
  2356. }
  2357. NonVisualDataModule->StartBusy();
  2358. }
  2359. //---------------------------------------------------------------------------
  2360. void __fastcall TCustomScpExplorerForm::BatchEnd(void * /*Storage*/)
  2361. {
  2362. NonVisualDataModule->EndBusy();
  2363. if (FErrorList)
  2364. {
  2365. HandleErrorList(FErrorList);
  2366. }
  2367. }
  2368. //---------------------------------------------------------------------------
  2369. void __fastcall TCustomScpExplorerForm::UpdateCopyParamCounters(
  2370. const TCopyParamType & CopyParam)
  2371. {
  2372. if (!CopyParam.IncludeFileMask.Masks.IsEmpty())
  2373. {
  2374. Configuration->Usage->Inc(L"FileMaskUses");
  2375. }
  2376. if (IsEffectiveFileNameMask(CopyParam.FileMask))
  2377. {
  2378. Configuration->Usage->Inc(L"OperationMaskUses");
  2379. }
  2380. }
  2381. //---------------------------------------------------------------------------
  2382. bool __fastcall TCustomScpExplorerForm::ExecuteCopyMoveFileOperation(
  2383. TFileOperation Operation, TOperationSide Side, TStrings * FileList, bool NoConfirmation, void * AParam)
  2384. {
  2385. DebugAssert(!IsLocalBrowserMode());
  2386. TTransferDirection Direction = (Side == osLocal ? tdToRemote : tdToLocal);
  2387. TTransferType Type = (Operation == foCopy ? ttCopy : ttMove);
  2388. TTransferOperationParam DefaultParam;
  2389. TTransferOperationParam & Param = (AParam != NULL) ? *static_cast<TTransferOperationParam *>(AParam) : DefaultParam;
  2390. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  2391. switch (Param.Queue)
  2392. {
  2393. case asOn:
  2394. CopyParam.Queue = true;
  2395. break;
  2396. case asOff:
  2397. CopyParam.Queue = false;
  2398. break;
  2399. case asAuto:
  2400. default:
  2401. // keep default
  2402. break;
  2403. }
  2404. bool Result =
  2405. CopyParamDialog(Direction, Type, Param.Temp, FileList, Param.TargetDirectory, CopyParam, !NoConfirmation, Param.DragDrop, Param.Options);
  2406. if (Result)
  2407. {
  2408. DebugAssert(Terminal);
  2409. bool SelectionRestored = false;
  2410. TCustomDirView * DView = NULL;
  2411. if (HasDirView[Side])
  2412. {
  2413. DView = DirView(Side);
  2414. DView->SaveSelection();
  2415. DView->SaveSelectedNames();
  2416. }
  2417. UpdateCopyParamCounters(CopyParam);
  2418. std::unique_ptr<TStringList> TransferResumeList(new TStringList());
  2419. DebugAssert(FTransferResumeList == NULL);
  2420. FTransferResumeList = Terminal->IsCapable[fcMoveToQueue] ? TransferResumeList.get() : NULL;
  2421. FMoveToQueue = false;
  2422. int Params = FLAGMASK(Operation == foMove, cpDelete);
  2423. try
  2424. {
  2425. TStrings * PermanentFileList;
  2426. std::unique_ptr<TStrings> PermanentFileListOwner;
  2427. try
  2428. {
  2429. if (Side == osLocal)
  2430. {
  2431. PermanentFileList = FileList;
  2432. Params |= FLAGMASK(Param.Temp, cpTemporary);
  2433. Terminal->CopyToRemote(FileList, Param.TargetDirectory, &CopyParam, Params, NULL);
  2434. if (Operation == foMove)
  2435. {
  2436. ReloadLocalDirectory();
  2437. if (DView != NULL)
  2438. {
  2439. DView->RestoreSelection();
  2440. }
  2441. SelectionRestored = true;
  2442. }
  2443. }
  2444. else
  2445. {
  2446. // Clone the file list as it may refer to current directory files,
  2447. // which get destroyed, when the source directory is reloaded after foMove operation.
  2448. // We should actually clone the file list for whole ExecuteFileOperation to protect against reloads.
  2449. // But for a hotfix, we are not going to do such a big change.
  2450. PermanentFileListOwner.reset(TRemoteFileList::CloneStrings(FileList));
  2451. PermanentFileList = PermanentFileListOwner.get();
  2452. try
  2453. {
  2454. Terminal->CopyToLocal(FileList, Param.TargetDirectory, &CopyParam, Params, NULL);
  2455. }
  2456. __finally
  2457. {
  2458. if (Operation == foMove)
  2459. {
  2460. if (DView != NULL)
  2461. {
  2462. DView->RestoreSelection();
  2463. }
  2464. SelectionRestored = true;
  2465. }
  2466. ReloadLocalDirectory(Param.TargetDirectory);
  2467. }
  2468. }
  2469. }
  2470. catch (EAbort &)
  2471. {
  2472. if (FMoveToQueue)
  2473. {
  2474. Params |= (CopyParam.QueueNoConfirmation ? cpNoConfirmation : 0);
  2475. DebugAssert(CopyParam.TransferSkipList == NULL);
  2476. DebugAssert(CopyParam.TransferResumeFile.IsEmpty());
  2477. if (TransferResumeList->Count > 0)
  2478. {
  2479. CopyParam.TransferResumeFile = TransferResumeList->Strings[TransferResumeList->Count - 1];
  2480. TransferResumeList->Delete(TransferResumeList->Count - 1);
  2481. }
  2482. CopyParam.TransferSkipList = TransferResumeList.release();
  2483. // not really needed, just to keep it consistent with TransferResumeList
  2484. FTransferResumeList = NULL;
  2485. FMoveToQueue = false;
  2486. Configuration->Usage->Inc("MovesToBackground");
  2487. AddQueueItem(Queue, Direction, PermanentFileList, Param.TargetDirectory, CopyParam, Params);
  2488. ClearTransferSourceSelection(Direction);
  2489. }
  2490. throw;
  2491. }
  2492. }
  2493. __finally
  2494. {
  2495. if (!SelectionRestored && (DView != NULL))
  2496. {
  2497. DView->DiscardSavedSelection();
  2498. }
  2499. FTransferResumeList = NULL;
  2500. }
  2501. }
  2502. return Result;
  2503. }
  2504. //---------------------------------------------------------------------------
  2505. bool __fastcall TCustomScpExplorerForm::ExecuteDeleteFileOperation(
  2506. TOperationSide Side, TStrings * FileList, void * Param)
  2507. {
  2508. DebugAssert(FileList->Count);
  2509. // We deliberately do not toggle alternative flag (Param), but use OR,
  2510. // because the Param is set only when command is invoked using Shift-Del/F8 keyboard
  2511. // shortcut of CurrentDeleteAlternativeAction
  2512. bool Alternative =
  2513. bool(Param) || UseAlternativeFunction();
  2514. bool Recycle;
  2515. if (IsSideLocalBrowser(Side))
  2516. {
  2517. Recycle = (WinConfiguration->DeleteToRecycleBin != Alternative);
  2518. }
  2519. else
  2520. {
  2521. Recycle =
  2522. (Terminal->SessionData->DeleteToRecycleBin != Alternative) &&
  2523. !Terminal->SessionData->RecycleBinPath.IsEmpty() &&
  2524. !Terminal->IsRecycledFile(FileList->Strings[0]);
  2525. }
  2526. bool Result = !(Recycle ? WinConfiguration->ConfirmRecycling : WinConfiguration->ConfirmDeleting);
  2527. if (!Result)
  2528. {
  2529. UnicodeString Query;
  2530. if (FileList->Count == 1)
  2531. {
  2532. if (IsSideLocalBrowser(Side))
  2533. {
  2534. Query = ExtractFileName(FileList->Strings[0]);
  2535. }
  2536. else
  2537. {
  2538. Query = UnixExtractFileName(FileList->Strings[0]);
  2539. }
  2540. Query = FMTLOAD((Recycle ? CONFIRM_RECYCLE_FILE : CONFIRM_DELETE_FILE), (Query));
  2541. }
  2542. else
  2543. {
  2544. Query = FMTLOAD((Recycle ? CONFIRM_RECYCLE_FILES : CONFIRM_DELETE_FILES), (FileList->Count));
  2545. }
  2546. TMessageParams Params(mpNeverAskAgainCheck);
  2547. Params.ImageName = L"Delete file";
  2548. unsigned int Answer =
  2549. MessageDialog(MainInstructions(Query), qtConfirmation, qaOK | qaCancel, HELP_DELETE_FILE, &Params);
  2550. if (Answer == qaNeverAskAgain)
  2551. {
  2552. Result = true;
  2553. if (Recycle)
  2554. {
  2555. WinConfiguration->ConfirmRecycling = false;
  2556. }
  2557. else
  2558. {
  2559. WinConfiguration->ConfirmDeleting = false;
  2560. }
  2561. }
  2562. else
  2563. {
  2564. Result = (Answer == qaOK);
  2565. }
  2566. }
  2567. if (Result)
  2568. {
  2569. DeleteFiles(Side, FileList, FLAGMASK(Alternative, dfAlternative));
  2570. }
  2571. return Result;
  2572. }
  2573. //---------------------------------------------------------------------------
  2574. bool __fastcall TCustomScpExplorerForm::ExecuteFileOperation(TFileOperation Operation,
  2575. TOperationSide Side, TStrings * FileList, bool NoConfirmation, void * Param)
  2576. {
  2577. TAutoBatch AutoBatch(this);
  2578. bool Result;
  2579. if ((Operation == foCopy) || (Operation == foMove))
  2580. {
  2581. Result = ExecuteCopyMoveFileOperation(Operation, Side, FileList, NoConfirmation, Param);
  2582. }
  2583. else if (Operation == foRename)
  2584. {
  2585. DebugAssert(DirView(Side)->ItemFocused);
  2586. DirView(Side)->ItemFocused->EditCaption();
  2587. Result = true;
  2588. }
  2589. else if (Operation == foDelete)
  2590. {
  2591. Result = ExecuteDeleteFileOperation(Side, FileList, Param);
  2592. }
  2593. else if (Operation == foSetProperties)
  2594. {
  2595. DirView(osRemote)->SaveSelectedNames();
  2596. Result = SetProperties(Side, FileList);
  2597. }
  2598. else if (Operation == foCustomCommand)
  2599. {
  2600. DebugAssert(Param);
  2601. DebugAssert(Side == osRemote); // Side carries no meaning for foCustomCommand
  2602. DirView(osRemote)->SaveSelectedNames();
  2603. const TCustomCommandType * Command = static_cast<const TCustomCommandType*>(Param);
  2604. CustomCommand(FileList, *Command, NULL);
  2605. Result = true;
  2606. }
  2607. else if ((Operation == foRemoteMove) || (Operation == foRemoteCopy))
  2608. {
  2609. DebugAssert(!IsSideLocalBrowser(Side));
  2610. Result = RemoteTransferFiles(FileList, NoConfirmation,
  2611. (Operation == foRemoteMove), reinterpret_cast<TManagedTerminal *>(Param));
  2612. }
  2613. else if (Operation == foLock)
  2614. {
  2615. DebugAssert(!IsSideLocalBrowser(Side));
  2616. LockFiles(FileList, true);
  2617. Result = true;
  2618. }
  2619. else if (Operation == foUnlock)
  2620. {
  2621. DebugAssert(!IsSideLocalBrowser(Side));
  2622. LockFiles(FileList, false);
  2623. Result = true;
  2624. }
  2625. else
  2626. {
  2627. DebugFail();
  2628. }
  2629. return Result;
  2630. }
  2631. //---------------------------------------------------------------------------
  2632. TOperationSide __fastcall TCustomScpExplorerForm::GetSide(TOperationSide Side)
  2633. {
  2634. if (Side == osCurrent)
  2635. {
  2636. Side = FCurrentSide;
  2637. }
  2638. return Side;
  2639. }
  2640. //---------------------------------------------------------------------------
  2641. bool __fastcall TCustomScpExplorerForm::ExecuteFileOperation(TFileOperation Operation,
  2642. TOperationSide Side, bool OnFocused, bool NoConfirmation, void * Param)
  2643. {
  2644. Side = GetSide(Side);
  2645. bool Result;
  2646. TStrings * FileList = DirView(Side)->CreateFileList(OnFocused, IsSideLocalBrowser(Side), NULL);
  2647. try
  2648. {
  2649. Result = ExecuteFileOperation(Operation, Side, FileList, NoConfirmation, Param);
  2650. }
  2651. __finally
  2652. {
  2653. delete FileList;
  2654. }
  2655. return Result;
  2656. }
  2657. //---------------------------------------------------------------------------
  2658. void __fastcall TCustomScpExplorerForm::ExecuteFileOperationCommand(
  2659. TFileOperation Operation, TOperationSide Side, bool OnFocused,
  2660. bool NoConfirmation, void * Param)
  2661. {
  2662. if (ExecuteFileOperation(Operation, Side, OnFocused, NoConfirmation, Param))
  2663. {
  2664. if ((Operation == foCopy) || (Operation == foMove))
  2665. {
  2666. DebugAssert(!IsLocalBrowserMode());
  2667. if (GetSide(Side) == osLocal)
  2668. {
  2669. Configuration->Usage->Inc(L"UploadsCommand");
  2670. }
  2671. else
  2672. {
  2673. Configuration->Usage->Inc(L"DownloadsCommand");
  2674. }
  2675. }
  2676. }
  2677. }
  2678. //---------------------------------------------------------------------------
  2679. void __fastcall TCustomScpExplorerForm::ExecuteCopyOperationCommand(
  2680. TOperationSide Side, bool OnFocused, unsigned int Flags)
  2681. {
  2682. TTransferOperationParam Param;
  2683. if ((WinConfiguration->Interface != ifCommander) ||
  2684. WinConfiguration->ScpCommander.ExplorerKeyboardShortcuts)
  2685. {
  2686. Flags &= ~cocShortCutHint;
  2687. }
  2688. TCustomDirView * DView = DirView(Side);
  2689. Param.Options =
  2690. FLAGMASK(FLAGSET(Flags, cocShortCutHint), coShortCutHint) |
  2691. FLAGMASK(SelectedAllFilesInDirView(DView), coAllFiles);
  2692. if (FLAGSET(Flags, cocQueue))
  2693. {
  2694. Param.Queue = asOn;
  2695. }
  2696. else if (FLAGSET(Flags, cocNonQueue))
  2697. {
  2698. Param.Queue = asOff;
  2699. }
  2700. ExecuteFileOperationCommand(foCopy, Side, OnFocused, false, &Param);
  2701. }
  2702. //---------------------------------------------------------------------------
  2703. void __fastcall TCustomScpExplorerForm::HandleErrorList(TStringList *& ErrorList)
  2704. {
  2705. try
  2706. {
  2707. if (ErrorList->Count)
  2708. {
  2709. UnicodeString Message = MainInstructions(FMTLOAD(ERROR_LIST_COUNT, (ErrorList->Count)));
  2710. if (MessageDialog(Message, qtError,
  2711. qaOK | qaCancel, HELP_NONE) == qaOK)
  2712. {
  2713. unsigned int Answer;
  2714. int Index = 0;
  2715. do
  2716. {
  2717. DebugAssert(Index >= 0 && Index < ErrorList->Count);
  2718. TQueryButtonAlias Aliases[2];
  2719. Aliases[0].Button = qaYes;
  2720. Aliases[0].Alias = LoadStr(PREV_BUTTON);
  2721. Aliases[1].Button = qaNo;
  2722. Aliases[1].Alias = LoadStr(NEXT_BUTTON);
  2723. TMessageParams Params;
  2724. Params.Aliases = Aliases;
  2725. Params.AliasesCount = LENOF(Aliases);
  2726. Answer = MoreMessageDialog(
  2727. FMTLOAD(ERROR_LIST_NUMBER, (Index+1, ErrorList->Count, ErrorList->Strings[Index])),
  2728. dynamic_cast<TStrings *>(ErrorList->Objects[Index]), qtError,
  2729. (Index ? qaYes : 0) | (Index < ErrorList->Count - 1 ? qaNo : 0) |
  2730. qaOK, HELP_NONE, &Params);
  2731. if (Answer == qaNo)
  2732. {
  2733. Index++;
  2734. }
  2735. if (Answer == qaYes)
  2736. {
  2737. Index--;
  2738. }
  2739. }
  2740. while (Answer != qaOK);
  2741. }
  2742. }
  2743. }
  2744. __finally
  2745. {
  2746. TStrings * List = ErrorList;
  2747. ErrorList = NULL;
  2748. for (int i = 0; i < List->Count; i++)
  2749. {
  2750. delete List->Objects[i];
  2751. }
  2752. delete List;
  2753. }
  2754. }
  2755. //---------------------------------------------------------------------------
  2756. void __fastcall TCustomScpExplorerForm::ExecuteRemoteFile(
  2757. const UnicodeString & FullFileName, TRemoteFile * File, TExecuteFileBy ExecuteFileBy)
  2758. {
  2759. // needed for checking filemasks, as there's no directory object
  2760. // associated with the file object
  2761. File->FullFileName = FullFileName;
  2762. TFileMasks::TParams MaskParams;
  2763. MaskParams.Size = File->Size;
  2764. MaskParams.Modification = File->Modification;
  2765. ExecuteFile(osRemote, ExecuteFileBy, NULL, FullFileName, File, MaskParams);
  2766. }
  2767. //---------------------------------------------------------------------------
  2768. void __fastcall TCustomScpExplorerForm::EditNew(TOperationSide Side)
  2769. {
  2770. DebugAssert(!WinConfiguration->DisableOpenEdit);
  2771. Side = GetSide(Side);
  2772. TCustomDirView * DView = DirView(Side);
  2773. TListItem * FocusedItem = DView->ItemFocused;
  2774. UnicodeString Name;
  2775. if ((FocusedItem != NULL) && !DView->ItemIsDirectory(FocusedItem))
  2776. {
  2777. Name = DView->ItemFileName(FocusedItem);
  2778. }
  2779. else
  2780. {
  2781. Name = LoadStr(NEW_FILE);
  2782. }
  2783. UnicodeString Names = Name;
  2784. std::unique_ptr<TStrings> History(CloneStrings(CustomWinConfiguration->History[L"EditFile"]));
  2785. if (InputDialog(LoadStr(EDIT_FILE_CAPTION), LoadStr(EDIT_FILE_PROMPT), Names,
  2786. HELP_EDIT_NEW, History.get(), true))
  2787. {
  2788. while (!Names.IsEmpty())
  2789. {
  2790. Name = CutToChar(Names, FileMasksDelimiters[1], false);
  2791. CustomWinConfiguration->History[L"EditFile"] = History.get();
  2792. UnicodeString TargetFileName;
  2793. UnicodeString LocalFileName;
  2794. UnicodeString RootTempDir;
  2795. UnicodeString TempDir;
  2796. UnicodeString RemoteDirectory;
  2797. bool ExistingFile = false;
  2798. if (!IsSideLocalBrowser(Side))
  2799. {
  2800. Name = AbsolutePath(FTerminal->CurrentDirectory, Name);
  2801. TRemoteFile * File = NULL;
  2802. if (FTerminal->FileExists(Name, &File) &&
  2803. !File->IsDirectory)
  2804. {
  2805. try
  2806. {
  2807. ExecuteRemoteFile(Name, File, efDefaultEditor);
  2808. ExistingFile = true;
  2809. }
  2810. __finally
  2811. {
  2812. delete File;
  2813. }
  2814. }
  2815. if (!ExistingFile)
  2816. {
  2817. RemoteDirectory = UnixExtractFilePath(Name);
  2818. TemporaryDirectoryForRemoteFiles(
  2819. RemoteDirectory, GUIConfiguration->CurrentCopyParam, TempDir, RootTempDir);
  2820. TargetFileName = UnixExtractFileName(Name);
  2821. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  2822. LocalFileName = TempDir +
  2823. // We probably do not want to trim the VMS version here
  2824. FTerminal->ChangeFileName(&CopyParam, TargetFileName, osRemote, false);
  2825. }
  2826. }
  2827. else
  2828. {
  2829. if (ExtractFilePath(Name).IsEmpty())
  2830. {
  2831. LocalFileName = IncludeTrailingBackslash(DView->PathName) + Name;
  2832. }
  2833. else
  2834. {
  2835. LocalFileName = ExpandFileName(Name);
  2836. }
  2837. TargetFileName = ExtractFileName(Name);
  2838. }
  2839. if (!ExistingFile)
  2840. {
  2841. bool NewFile = !FileExists(ApiPath(LocalFileName));
  2842. if (NewFile)
  2843. {
  2844. int File = FileCreate(ApiPath(LocalFileName));
  2845. if (File < 0)
  2846. {
  2847. if (!RootTempDir.IsEmpty())
  2848. {
  2849. RecursiveDeleteFile(ExcludeTrailingBackslash(RootTempDir), false);
  2850. }
  2851. throw Exception(FMTLOAD(CREATE_FILE_ERROR, (LocalFileName)));
  2852. }
  2853. else
  2854. {
  2855. FileClose(File);
  2856. }
  2857. }
  2858. TExecuteFileBy ExecuteFileBy = efDefaultEditor;
  2859. const TEditorData * ExternalEditor = NULL;
  2860. TFileMasks::TParams MaskParams; // size not known
  2861. ExecuteFileNormalize(ExecuteFileBy, ExternalEditor, TargetFileName,
  2862. false, MaskParams);
  2863. CustomExecuteFile(Side, ExecuteFileBy, LocalFileName, TargetFileName,
  2864. ExternalEditor, RootTempDir, RemoteDirectory, NewFile);
  2865. }
  2866. }
  2867. }
  2868. }
  2869. //---------------------------------------------------------------------------
  2870. bool __fastcall TCustomScpExplorerForm::RemoteExecuteForceText(
  2871. TExecuteFileBy ExecuteFileBy, const TEditorData * ExternalEditor)
  2872. {
  2873. DebugAssert((ExecuteFileBy == efExternalEditor) ==
  2874. ((ExternalEditor != NULL) && (ExternalEditor->Editor == edExternal)));
  2875. DebugAssert(ExecuteFileBy != efDefaultEditor);
  2876. return
  2877. ((ExecuteFileBy == efInternalEditor)) ||
  2878. ((ExecuteFileBy == efExternalEditor) && ExternalEditor->ExternalEditorText);
  2879. }
  2880. //---------------------------------------------------------------------------
  2881. void __fastcall TCustomScpExplorerForm::CustomExecuteFile(TOperationSide Side,
  2882. TExecuteFileBy ExecuteFileBy, UnicodeString FileName, UnicodeString OriginalFileName,
  2883. const TEditorData * ExternalEditor, UnicodeString LocalRootDirectory,
  2884. UnicodeString RemoteDirectory, bool NewFile)
  2885. {
  2886. DebugAssert(!WinConfiguration->DisableOpenEdit);
  2887. DebugAssert((ExecuteFileBy == efExternalEditor) ==
  2888. ((ExternalEditor != NULL) && (ExternalEditor->Editor == edExternal)));
  2889. DebugAssert(ExecuteFileBy != efDefaultEditor);
  2890. Side = GetSide(Side);
  2891. std::unique_ptr<TEditedFileData> Data(new TEditedFileData);
  2892. if (!IsSideLocalBrowser(Side))
  2893. {
  2894. Data->Terminal = Terminal;
  2895. Data->Queue = Queue;
  2896. Data->SessionData = CloneCurrentSessionData();
  2897. Data->ForceText = RemoteExecuteForceText(ExecuteFileBy, ExternalEditor);
  2898. Data->RemoteDirectory = RemoteDirectory;
  2899. Data->SessionName = Terminal->SessionData->SessionName;
  2900. Data->LocalRootDirectory = LocalRootDirectory;
  2901. Data->OriginalFileName = OriginalFileName;
  2902. Data->Command = L""; // will be changed later for external editor
  2903. }
  2904. if (ExecuteFileBy == efInternalEditor)
  2905. {
  2906. if (!IsSideLocalBrowser(Side))
  2907. {
  2908. UnicodeString Caption = UnixIncludeTrailingBackslash(RemoteDirectory) + OriginalFileName +
  2909. L" - " + Terminal->SessionData->SessionName;
  2910. TForm * Editor;
  2911. try
  2912. {
  2913. TNotifyEvent OnFileChanged = NULL;
  2914. TNotifyEvent OnSaveAll = NULL;
  2915. TAnyModifiedEvent OnAnyModified = NULL;
  2916. // Edited files are uploaded in background queue, what we do not support with encrypted files
  2917. if (Terminal->IsCapable[fsBackgroundTransfers])
  2918. {
  2919. OnFileChanged = FEditorManager->FileChanged;
  2920. OnSaveAll = SaveAllInternalEditors;
  2921. OnAnyModified = AnyInternalEditorModified;
  2922. }
  2923. Editor = ShowEditorForm(FileName, this, OnFileChanged,
  2924. FEditorManager->FileReload, FEditorManager->FileClosed,
  2925. OnSaveAll, OnAnyModified,
  2926. Caption, FStandaloneEditing, SessionColor, Terminal->SessionData->InternalEditorEncoding, NewFile);
  2927. }
  2928. catch(...)
  2929. {
  2930. if (!LocalRootDirectory.IsEmpty())
  2931. {
  2932. RecursiveDeleteFile(ExcludeTrailingBackslash(LocalRootDirectory), false);
  2933. }
  2934. throw;
  2935. }
  2936. FEditorManager->AddFileInternal(FileName, Data.release(), Editor);
  2937. }
  2938. else
  2939. {
  2940. DebugAssert(!FStandaloneEditing);
  2941. TForm * Editor =
  2942. ShowEditorForm(FileName, this, NULL, NULL, LocalEditorClosed,
  2943. SaveAllInternalEditors, AnyInternalEditorModified,
  2944. L"", false, SessionColor, -1, NewFile);
  2945. FLocalEditors->Add(Editor);
  2946. }
  2947. }
  2948. else
  2949. {
  2950. HANDLE Process;
  2951. if (ExecuteFileBy == efExternalEditor)
  2952. {
  2953. UnicodeString Program, Params, Dir;
  2954. Data->Command = ExternalEditor->ExternalEditor;
  2955. ReformatFileNameCommand(Data->Command);
  2956. SplitCommand(Data->Command, Program, Params, Dir);
  2957. Params = ExpandFileNameCommand(Params, FileName);
  2958. Program = ExpandEnvironmentVariables(Program);
  2959. if (!ExecuteShell(Program, Params, Process))
  2960. {
  2961. throw EOSExtException(FMTLOAD(EDITOR_ERROR, (Program)));
  2962. }
  2963. }
  2964. else
  2965. {
  2966. DebugAssert(!IsSideLocalBrowser(Side));
  2967. if (!ExecuteShell(FileName, L"", Process))
  2968. {
  2969. throw EOSExtException(FMTLOAD(EXECUTE_FILE_ERROR, (FileName)));
  2970. }
  2971. }
  2972. if (IsSideLocalBrowser(Side) ||
  2973. ((ExecuteFileBy == efShell) &&
  2974. !WinConfiguration->Editor.SDIShellEditor) ||
  2975. ((ExecuteFileBy == efExternalEditor) &&
  2976. !ExternalEditor->SDIExternalEditor))
  2977. {
  2978. // no need for handle
  2979. if (Process != NULL)
  2980. {
  2981. DebugCheck(CloseHandle(Process));
  2982. }
  2983. Process = INVALID_HANDLE_VALUE;
  2984. }
  2985. else
  2986. {
  2987. if (Process == NULL)
  2988. {
  2989. throw ExtException(LoadStr(OPEN_FILE_NO_PROCESS2), L"", HELP_OPEN_FILE_NO_PROCESS);
  2990. }
  2991. }
  2992. if (!IsSideLocalBrowser(Side))
  2993. {
  2994. FEditorManager->AddFileExternal(FileName, Data.release(), Process);
  2995. }
  2996. }
  2997. }
  2998. //---------------------------------------------------------------------------
  2999. void __fastcall TCustomScpExplorerForm::SaveInternalEditor(
  3000. const UnicodeString /*FileName*/, TEditedFileData * /*Data*/, TObject * Token,
  3001. void * /*Arg*/)
  3002. {
  3003. if (Token != NULL)
  3004. {
  3005. EditorFormFileSave(static_cast<TForm *>(Token));
  3006. }
  3007. }
  3008. //---------------------------------------------------------------------------
  3009. void __fastcall TCustomScpExplorerForm::SaveAllInternalEditors(TObject * /*Sender*/)
  3010. {
  3011. for (int Index = 0; Index < FLocalEditors->Count; Index++)
  3012. {
  3013. EditorFormFileSave(static_cast<TForm *>(FLocalEditors->Items[Index]));
  3014. }
  3015. FEditorManager->ProcessFiles(SaveInternalEditor, NULL);
  3016. }
  3017. //---------------------------------------------------------------------------
  3018. void __fastcall TCustomScpExplorerForm::InternalEditorModified(
  3019. const UnicodeString /*FileName*/, TEditedFileData * /*Data*/, TObject * Token,
  3020. void * Arg)
  3021. {
  3022. if ((Token != NULL) &&
  3023. IsEditorFormModified(static_cast<TForm *>(Token)))
  3024. {
  3025. *static_cast<bool *>(Arg) = true;
  3026. }
  3027. }
  3028. //---------------------------------------------------------------------------
  3029. void __fastcall TCustomScpExplorerForm::AnyInternalEditorModified(
  3030. TObject * /*Sender*/, bool & Modified)
  3031. {
  3032. for (int Index = 0; !Modified && (Index < FLocalEditors->Count); Index++)
  3033. {
  3034. if (IsEditorFormModified(static_cast<TForm *>(FLocalEditors->Items[Index])))
  3035. {
  3036. Modified = true;
  3037. }
  3038. }
  3039. if (!Modified)
  3040. {
  3041. FEditorManager->ProcessFiles(InternalEditorModified, &Modified);
  3042. }
  3043. }
  3044. //---------------------------------------------------------------------------
  3045. void __fastcall TCustomScpExplorerForm::LocalEditorClosed(TObject * Sender, bool /*Forced*/)
  3046. {
  3047. DebugCheck(FLocalEditors->Extract(Sender) >= 0);
  3048. }
  3049. //---------------------------------------------------------------------------
  3050. void __fastcall TCustomScpExplorerForm::TemporaryDirectoryForRemoteFiles(
  3051. UnicodeString RemoteDirectory, TCopyParamType CopyParam,
  3052. UnicodeString & Result, UnicodeString & RootDirectory)
  3053. {
  3054. if (!WinConfiguration->TemporaryDirectoryDeterministic)
  3055. {
  3056. RootDirectory = IncludeTrailingBackslash(WinConfiguration->TemporaryDir());
  3057. Result = RootDirectory;
  3058. }
  3059. else
  3060. {
  3061. RootDirectory = L"";
  3062. Result = WinConfiguration->ExpandedTemporaryDirectory();
  3063. Result = IncludeTrailingBackslash(Result);
  3064. }
  3065. if (WinConfiguration->TemporaryDirectoryAppendSession)
  3066. {
  3067. Result = IncludeTrailingBackslash(Result + MakeValidFileName(Terminal->SessionData->SessionName));
  3068. }
  3069. if (WinConfiguration->TemporaryDirectoryAppendPath)
  3070. {
  3071. if (!RemoteDirectory.IsEmpty() && (RemoteDirectory[1] == L'/'))
  3072. {
  3073. RemoteDirectory.Delete(1, 1);
  3074. }
  3075. Result = IncludeTrailingBackslash(Result + CopyParam.ValidLocalPath(FromUnixPath(RemoteDirectory)));
  3076. }
  3077. if (!ForceDirectories(ApiPath(Result)))
  3078. {
  3079. throw EOSExtException(FMTLOAD(CREATE_TEMP_DIR_ERROR, (Result)));
  3080. }
  3081. }
  3082. //---------------------------------------------------------------------------
  3083. void __fastcall TCustomScpExplorerForm::TemporarilyDownloadFiles(
  3084. TStrings * FileList, bool ForceText, UnicodeString & RootTempDir, UnicodeString & TempDir,
  3085. bool AllFiles, bool GetTargetNames, bool AutoOperation)
  3086. {
  3087. DebugAssert(!IsLocalBrowserMode());
  3088. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  3089. if (ForceText)
  3090. {
  3091. CopyParam.TransferMode = tmAscii;
  3092. }
  3093. TemporaryFileCopyParam(CopyParam);
  3094. if (AllFiles)
  3095. {
  3096. CopyParam.IncludeFileMask = TFileMasks();
  3097. }
  3098. if (TempDir.IsEmpty())
  3099. {
  3100. TemporaryDirectoryForRemoteFiles(FTerminal->CurrentDirectory, CopyParam, TempDir, RootTempDir);
  3101. }
  3102. DebugAssert(!FAutoOperation);
  3103. FAutoOperation = AutoOperation;
  3104. Terminal->ExceptionOnFail = true;
  3105. try
  3106. {
  3107. try
  3108. {
  3109. // turn off confirmations, as for MDI editors we may possibly download
  3110. // the same file over
  3111. Terminal->CopyToLocal(
  3112. FileList, TempDir, &CopyParam, cpNoConfirmation | cpTemporary, NULL);
  3113. if (GetTargetNames)
  3114. {
  3115. for (int i = 0; i < FileList->Count; i++)
  3116. {
  3117. FileList->Strings[i] =
  3118. Terminal->ChangeFileName(&CopyParam, UnixExtractFileName(FileList->Strings[i]), osRemote, false);
  3119. }
  3120. }
  3121. }
  3122. catch(...)
  3123. {
  3124. if (!RootTempDir.IsEmpty())
  3125. {
  3126. RecursiveDeleteFile(ExcludeTrailingBackslash(RootTempDir), false);
  3127. }
  3128. throw;
  3129. }
  3130. }
  3131. __finally
  3132. {
  3133. FAutoOperation = false;
  3134. Terminal->ExceptionOnFail = false;
  3135. }
  3136. }
  3137. //---------------------------------------------------------------------------
  3138. void __fastcall TCustomScpExplorerForm::EditorAutoConfig()
  3139. {
  3140. // Do not waste time checking for default editor list the next time,
  3141. // or testing if default editor is not notepad.
  3142. bool TryNextTime = false;
  3143. UnicodeString UsageState;
  3144. if (!WinConfiguration->EditorList->IsDefaultList())
  3145. {
  3146. UsageState = "H";
  3147. }
  3148. else
  3149. {
  3150. UnicodeString Executable;
  3151. UnicodeString ExecutableDescription;
  3152. if (DetectSystemExternalEditor(false, Executable, ExecutableDescription, UsageState, TryNextTime))
  3153. {
  3154. UnicodeString Message =
  3155. FMTLOAD(EDITOR_AUTO_CONFIG2, (ExecutableDescription, ExecutableDescription));
  3156. unsigned int Answer =
  3157. MessageDialog(Message, qtConfirmation, qaOK | qaCancel, HELP_EDITOR_AUTO_CONFIG);
  3158. if (Answer != qaOK)
  3159. {
  3160. UsageState = "R";
  3161. }
  3162. else
  3163. {
  3164. UsageState = "A";
  3165. TEditorData EditorData;
  3166. EditorData.Editor = edExternal;
  3167. EditorData.ExternalEditor = FormatCommand(Executable, L"");
  3168. EditorData.DecideExternalEditorText();
  3169. TEditorList EditorList;
  3170. EditorList = *WinConfiguration->EditorList;
  3171. EditorList.Insert(0, new TEditorPreferences(EditorData));
  3172. WinConfiguration->EditorList = &EditorList;
  3173. }
  3174. }
  3175. }
  3176. WinConfiguration->OfferedEditorAutoConfig = !TryNextTime;
  3177. WinConfiguration->Usage->Set(L"EditorAutoConfig", UsageState);
  3178. }
  3179. //---------------------------------------------------------------------------
  3180. void __fastcall TCustomScpExplorerForm::ExecuteFileNormalize(
  3181. TExecuteFileBy & ExecuteFileBy, const TEditorData *& ExternalEditor,
  3182. const UnicodeString & FileName, bool Local, const TFileMasks::TParams & MaskParams)
  3183. {
  3184. if (ExecuteFileBy == efDefaultEditor)
  3185. {
  3186. if (!WinConfiguration->OfferedEditorAutoConfig)
  3187. {
  3188. EditorAutoConfig();
  3189. }
  3190. const TEditorPreferences * Editor =
  3191. WinConfiguration->DefaultEditorForFile(FileName, Local, MaskParams);
  3192. if ((Editor == NULL) || (Editor->Data->Editor == edInternal))
  3193. {
  3194. ExecuteFileBy = efInternalEditor;
  3195. ExternalEditor = NULL;
  3196. }
  3197. else if (Editor->Data->Editor == edOpen)
  3198. {
  3199. ExecuteFileBy = efShell;
  3200. ExternalEditor = NULL;
  3201. }
  3202. else
  3203. {
  3204. ExecuteFileBy = efExternalEditor;
  3205. ExternalEditor = Editor->Data;
  3206. }
  3207. }
  3208. }
  3209. //---------------------------------------------------------------------------
  3210. void __fastcall TCustomScpExplorerForm::ExecuteFile(TOperationSide Side,
  3211. TExecuteFileBy ExecuteFileBy, const TEditorData * ExternalEditor,
  3212. UnicodeString FullFileName, TObject * Object, const TFileMasks::TParams & MaskParams)
  3213. {
  3214. UnicodeString OriginalFileName;
  3215. UnicodeString LocalRootDirectory;
  3216. UnicodeString RemoteDirectory;
  3217. ExecuteFileNormalize(ExecuteFileBy, ExternalEditor, FullFileName,
  3218. IsSideLocalBrowser(Side), MaskParams);
  3219. UnicodeString Counter;
  3220. UnicodeString LocalFileName;
  3221. bool Handled = false;
  3222. if (!IsSideLocalBrowser(Side))
  3223. {
  3224. // We need to trim VMS version here, so that we use name without version
  3225. // when uploading back to create a new version of the file
  3226. OriginalFileName = FTerminal->GetBaseFileName(UnixExtractFileName(FullFileName));
  3227. RemoteDirectory = UnixExtractFilePath(FullFileName);
  3228. TObject * Token = NULL;
  3229. UnicodeString LocalDirectory;
  3230. if (!FEditorManager->CanAddFile(RemoteDirectory, OriginalFileName,
  3231. Terminal->SessionData->SessionName, Token, LocalRootDirectory,
  3232. LocalDirectory))
  3233. {
  3234. if (Token != NULL)
  3235. {
  3236. TForm * Form = dynamic_cast<TForm *>(Token);
  3237. if (Form->WindowState == wsMinimized)
  3238. {
  3239. ShowWindow(Form->Handle, SW_RESTORE);
  3240. }
  3241. else
  3242. {
  3243. Form->SetFocus();
  3244. }
  3245. Handled = true;
  3246. }
  3247. else
  3248. {
  3249. throw Exception(FMTLOAD(ALREADY_EDITED_EXTERNALLY_OR_UPLOADED, (OriginalFileName)));
  3250. }
  3251. }
  3252. if (!Handled)
  3253. {
  3254. TStringList * FileList1 = new TStringList();
  3255. try
  3256. {
  3257. FileList1->AddObject(FullFileName, Object);
  3258. TemporarilyDownloadFiles(FileList1,
  3259. RemoteExecuteForceText(ExecuteFileBy, ExternalEditor),
  3260. LocalRootDirectory, LocalDirectory, true, true, true);
  3261. LocalFileName = LocalDirectory + FileList1->Strings[0];
  3262. }
  3263. __finally
  3264. {
  3265. delete FileList1;
  3266. }
  3267. switch (ExecuteFileBy)
  3268. {
  3269. case efShell:
  3270. Counter = "RemoteFilesExecuted";
  3271. break;
  3272. case efInternalEditor:
  3273. Counter = "RemoteFilesOpenedInInternalEditor";
  3274. break;
  3275. case efExternalEditor:
  3276. Counter = "RemoteFilesOpenedInExternalEditor";
  3277. break;
  3278. default:
  3279. DebugFail();
  3280. }
  3281. }
  3282. }
  3283. else
  3284. {
  3285. LocalFileName = FullFileName;
  3286. OriginalFileName = ExtractFileName(FullFileName);
  3287. switch (ExecuteFileBy)
  3288. {
  3289. case efShell:
  3290. Counter = "LocalFilesExecuted";
  3291. break;
  3292. case efInternalEditor:
  3293. Counter = "LocalFilesOpenedInInternalEditor";
  3294. break;
  3295. case efExternalEditor:
  3296. Counter = "LocalFilesOpenedInExternalEditor";
  3297. break;
  3298. default:
  3299. DebugFail();
  3300. }
  3301. }
  3302. if (!Handled)
  3303. {
  3304. Configuration->Usage->Inc(Counter);
  3305. CustomExecuteFile(Side, ExecuteFileBy, LocalFileName, OriginalFileName,
  3306. ExternalEditor, LocalRootDirectory, RemoteDirectory, false);
  3307. }
  3308. }
  3309. //---------------------------------------------------------------------------
  3310. void __fastcall TCustomScpExplorerForm::ExecuteFile(TOperationSide Side,
  3311. TExecuteFileBy ExecuteFileBy, const TEditorData * ExternalEditor,
  3312. bool AllSelected, bool OnFocused)
  3313. {
  3314. DebugAssert(!WinConfiguration->DisableOpenEdit);
  3315. DebugAssert((ExecuteFileBy == efExternalEditor) ==
  3316. ((ExternalEditor != NULL) && (ExternalEditor->Editor == edExternal)));
  3317. Side = GetSide(Side);
  3318. TCustomDirView * DView = DirView(Side);
  3319. TStrings * FileList =
  3320. AllSelected ?
  3321. DView->CreateFileList(OnFocused, IsSideLocalBrowser(Side)) :
  3322. DView->CreateFocusedFileList(IsSideLocalBrowser(Side));
  3323. try
  3324. {
  3325. DebugAssert(AllSelected || (FileList->Count == 1));
  3326. for (int i = 0; i < FileList->Count; i++)
  3327. {
  3328. UnicodeString ListFileName = FileList->Strings[i];
  3329. UnicodeString FileNameOnly =
  3330. !IsSideLocalBrowser(Side) ? UnixExtractFileName(ListFileName) : ExtractFileName(ListFileName);
  3331. TListItem * Item = DView->FindFileItem(FileNameOnly);
  3332. if (!DView->ItemIsDirectory(Item))
  3333. {
  3334. UnicodeString FullFileName;
  3335. if (!IsSideLocalBrowser(Side))
  3336. {
  3337. FullFileName = DirView(Side)->Path + ListFileName;
  3338. }
  3339. else
  3340. {
  3341. FullFileName = ListFileName;
  3342. }
  3343. TFileMasks::TParams MaskParams;
  3344. MaskParams.Size = DView->ItemFileSize(Item);
  3345. TDateTimePrecision Precision;
  3346. MaskParams.Modification = DView->ItemFileTime(Item, Precision);
  3347. ExecuteFile(Side, ExecuteFileBy, ExternalEditor, FullFileName,
  3348. FileList->Objects[i], MaskParams);
  3349. }
  3350. }
  3351. }
  3352. __finally
  3353. {
  3354. delete FileList;
  3355. }
  3356. }
  3357. //---------------------------------------------------------------------------
  3358. void __fastcall TCustomScpExplorerForm::TemporaryFileCopyParam(TCopyParamType & CopyParam)
  3359. {
  3360. // do not forget to add additional options to TemporarilyDownloadFiles, and AS
  3361. CopyParam.FileNameCase = ncNoChange;
  3362. CopyParam.PreserveRights = false;
  3363. CopyParam.PreserveReadOnly = false;
  3364. CopyParam.ReplaceInvalidChars = true;
  3365. CopyParam.IncludeFileMask = TFileMasks();
  3366. CopyParam.NewerOnly = false;
  3367. CopyParam.FileMask = L"";
  3368. }
  3369. //---------------------------------------------------------------------------
  3370. void __fastcall TCustomScpExplorerForm::ExecutedFileChanged(const UnicodeString FileName,
  3371. TEditedFileData * Data, HANDLE UploadCompleteEvent)
  3372. {
  3373. TTerminalManager * Manager = TTerminalManager::Instance();
  3374. if ((Data->Terminal == NULL) || !Data->Terminal->Active)
  3375. {
  3376. if (!NonVisualDataModule->Busy)
  3377. {
  3378. UnicodeString FileNameOnly = ExtractFileName(FileName);
  3379. UnicodeString EditInactiveSessionReopenAcceptedCounter = L"EditInactiveSessionReopenAccepted";
  3380. UnicodeString EditInactiveSessionReopenRejectedCounter = L"EditInactiveSessionReopenRejected";
  3381. if (Data->Terminal == NULL)
  3382. {
  3383. TTerminal * SameSiteTerminal = Manager->FindActiveTerminalForSite(Data->SessionData);
  3384. if (SameSiteTerminal != NULL)
  3385. {
  3386. UnicodeString Message =
  3387. FMTLOAD(EDIT_SESSION_REATTACH,
  3388. (FileNameOnly, Data->SessionName, FileNameOnly));
  3389. if (MessageDialog(Message, qtConfirmation, qaOK | qaCancel) == qaOK)
  3390. {
  3391. Data->Terminal = Terminal;
  3392. Data->Queue = Manager->FindQueueForTerminal(Terminal);
  3393. Data->SessionName = Terminal->SessionData->SessionName;
  3394. // We might also overwrite session data
  3395. Configuration->Usage->Inc(EditInactiveSessionReopenAcceptedCounter);
  3396. }
  3397. else
  3398. {
  3399. Configuration->Usage->Inc(EditInactiveSessionReopenRejectedCounter);
  3400. Abort();
  3401. }
  3402. }
  3403. }
  3404. // foreground session should reconnect itself
  3405. else if (Terminal != Data->Terminal)
  3406. {
  3407. UnicodeString Message =
  3408. MainInstructions(
  3409. FMTLOAD(EDIT_SESSION_RECONNECT, (Data->SessionName, FileNameOnly)));
  3410. if (MessageDialog(Message, qtConfirmation, qaOK | qaCancel) == qaOK)
  3411. {
  3412. Manager->SetActiveTerminalWithAutoReconnect(Data->Terminal);
  3413. Configuration->Usage->Inc(EditInactiveSessionReopenAcceptedCounter);
  3414. }
  3415. else
  3416. {
  3417. Configuration->Usage->Inc(EditInactiveSessionReopenRejectedCounter);
  3418. Abort();
  3419. }
  3420. }
  3421. }
  3422. if ((Data->Terminal == NULL) || !Data->Terminal->Active)
  3423. {
  3424. Configuration->Usage->Inc(L"EditInactiveSession");
  3425. // Prevent this when not idle (!NonVisualDataModule->Busy)?
  3426. throw Exception(FMTLOAD(EDIT_SESSION_CLOSED2,
  3427. (ExtractFileName(FileName), Data->SessionName)));
  3428. }
  3429. }
  3430. TStrings * FileList = new TStringList();
  3431. try
  3432. {
  3433. FileList->Add(FileName);
  3434. // consider using the same settings (preset) as when the file was downloaded
  3435. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  3436. TemporaryFileCopyParam(CopyParam);
  3437. if (Data->ForceText)
  3438. {
  3439. CopyParam.TransferMode = tmAscii;
  3440. }
  3441. // so i do not need to worry if masking algorithm works in all cases
  3442. // ("" means "copy file name", no masking is actually done)
  3443. if (ExtractFileName(FileName) == Data->OriginalFileName)
  3444. {
  3445. CopyParam.FileMask = L"";
  3446. }
  3447. else
  3448. {
  3449. CopyParam.FileMask = DelimitFileNameMask(Data->OriginalFileName);
  3450. }
  3451. DebugAssert(Data->Queue != NULL);
  3452. int Params = cpNoConfirmation | cpTemporary;
  3453. TQueueItem * QueueItem = new TUploadQueueItem(Data->Terminal, FileList,
  3454. Data->RemoteDirectory, &CopyParam, Params, true, false);
  3455. QueueItem->CompleteEvent = UploadCompleteEvent;
  3456. AddQueueItem(Data->Queue, QueueItem, Data->Terminal);
  3457. }
  3458. __finally
  3459. {
  3460. delete FileList;
  3461. }
  3462. }
  3463. //---------------------------------------------------------------------------
  3464. void __fastcall TCustomScpExplorerForm::ExecutedFileReload(
  3465. const UnicodeString FileName, const TEditedFileData * Data)
  3466. {
  3467. // Sanity check, we should not be busy otherwise user would not be able to click Reload button.
  3468. DebugAssert(!NonVisualDataModule->Busy);
  3469. if ((Data->Terminal == NULL) || !Data->Terminal->Active)
  3470. {
  3471. throw Exception(FMTLOAD(EDIT_SESSION_CLOSED_RELOAD,
  3472. (ExtractFileName(FileName), Data->SessionName)));
  3473. }
  3474. TManagedTerminal * PrevTerminal = TTerminalManager::Instance()->ActiveTerminal;
  3475. TTerminalManager::Instance()->ActiveTerminal = Data->Terminal;
  3476. NonVisualDataModule->StartBusy();
  3477. try
  3478. {
  3479. std::unique_ptr<TRemoteFile> File;
  3480. UnicodeString RemoteFileName =
  3481. UnixIncludeTrailingBackslash(Data->RemoteDirectory) + Data->OriginalFileName;
  3482. FTerminal->ExceptionOnFail = true;
  3483. try
  3484. {
  3485. TRemoteFile * AFile = NULL;
  3486. FTerminal->ReadFile(RemoteFileName, AFile);
  3487. File.reset(AFile);
  3488. if (!File->HaveFullFileName)
  3489. {
  3490. File->FullFileName = RemoteFileName;
  3491. }
  3492. }
  3493. __finally
  3494. {
  3495. FTerminal->ExceptionOnFail = false;
  3496. }
  3497. std::unique_ptr<TStrings> FileList(new TStringList());
  3498. FileList->AddObject(RemoteFileName, File.get());
  3499. UnicodeString RootTempDir = Data->LocalRootDirectory;
  3500. UnicodeString TempDir = ExtractFilePath(FileName);
  3501. TemporarilyDownloadFiles(FileList.get(), Data->ForceText, RootTempDir,
  3502. TempDir, true, true, true);
  3503. // sanity check, the target file name should be still the same
  3504. DebugAssert(ExtractFileName(FileName) == FileList->Strings[0]);
  3505. }
  3506. __finally
  3507. {
  3508. NonVisualDataModule->EndBusy();
  3509. // it actually may not exist anymore...
  3510. TTerminalManager::Instance()->ActiveTerminal = PrevTerminal;
  3511. }
  3512. }
  3513. //---------------------------------------------------------------------------
  3514. void __fastcall TCustomScpExplorerForm::ExecutedFileEarlyClosed(
  3515. const TEditedFileData * Data, bool & KeepOpen)
  3516. {
  3517. // Command is set for external editors only (not for "shell" open).
  3518. if (!Data->Command.IsEmpty())
  3519. {
  3520. bool AnyFound = false;
  3521. bool AnyMDI = false;
  3522. bool AnyNonMDI = false;
  3523. bool AnyDetect = false;
  3524. TEditorList * EditorList = new TEditorList();
  3525. try
  3526. {
  3527. *EditorList = *WinConfiguration->EditorList;
  3528. for (int i = 0; i < EditorList->Count; i++)
  3529. {
  3530. const TEditorPreferences * Editor = EditorList->Editors[i];
  3531. if ((Editor->Data->Editor == edExternal) &&
  3532. (Editor->Data->ExternalEditor == Data->Command))
  3533. {
  3534. AnyFound = true;
  3535. if (Editor->Data->SDIExternalEditor)
  3536. {
  3537. AnyNonMDI = true;
  3538. if (Editor->Data->DetectMDIExternalEditor)
  3539. {
  3540. AnyDetect = true;
  3541. }
  3542. }
  3543. else
  3544. {
  3545. AnyMDI = true;
  3546. }
  3547. }
  3548. }
  3549. bool EnableMDI = false;
  3550. bool DisableDetect = false;
  3551. if (AnyMDI)
  3552. {
  3553. KeepOpen = true;
  3554. if (AnyNonMDI)
  3555. {
  3556. // there is at least one instance of the editor with MDI support enabled,
  3557. // and one with disabled, enable it for all instances
  3558. EnableMDI = true;
  3559. }
  3560. }
  3561. else if (AnyFound && !AnyDetect)
  3562. {
  3563. // at least once instance found but all have MDI autodetection disabled
  3564. // => close the file (default action)
  3565. }
  3566. else
  3567. {
  3568. // no instance of the editor has MDI support enabled
  3569. TMessageParams Params;
  3570. if (AnyFound)
  3571. {
  3572. // there is at least one instance of the editor
  3573. Params.Params |= mpNeverAskAgainCheck;
  3574. }
  3575. unsigned int Answer = MessageDialog(FMTLOAD(EDITOR_EARLY_CLOSED2, (Data->OriginalFileName)), qtWarning,
  3576. qaYes | qaNo, HELP_EDITOR_EARLY_CLOSED, &Params);
  3577. switch (Answer)
  3578. {
  3579. case qaNeverAskAgain:
  3580. DisableDetect = true;
  3581. break;
  3582. case qaNo:
  3583. EnableMDI = true;
  3584. KeepOpen = true;
  3585. break;
  3586. }
  3587. }
  3588. if (AnyFound && (EnableMDI || DisableDetect))
  3589. {
  3590. bool Changed = false;
  3591. for (int i = 0; i < EditorList->Count; i++)
  3592. {
  3593. const TEditorPreferences * Editor = EditorList->Editors[i];
  3594. if ((Editor->Data->Editor == edExternal) &&
  3595. (Editor->Data->ExternalEditor == Data->Command) &&
  3596. ((EnableMDI && Editor->Data->SDIExternalEditor) ||
  3597. (DisableDetect && Editor->Data->DetectMDIExternalEditor)))
  3598. {
  3599. Changed = true;
  3600. TEditorPreferences * UpdatedEditor = new TEditorPreferences(*Editor);
  3601. if (EnableMDI)
  3602. {
  3603. UpdatedEditor->GetData()->SDIExternalEditor = false;
  3604. }
  3605. if (DisableDetect)
  3606. {
  3607. UpdatedEditor->GetData()->DetectMDIExternalEditor = false;
  3608. }
  3609. EditorList->Change(i, UpdatedEditor);
  3610. }
  3611. }
  3612. if (Changed)
  3613. {
  3614. WinConfiguration->EditorList = EditorList;
  3615. }
  3616. }
  3617. }
  3618. __finally
  3619. {
  3620. delete EditorList;
  3621. }
  3622. }
  3623. else
  3624. {
  3625. // "open" case
  3626. MessageDialog(FMTLOAD(APP_EARLY_CLOSED, (Data->OriginalFileName)), qtWarning,
  3627. qaOK, HELP_APP_EARLY_CLOSED);
  3628. }
  3629. }
  3630. //---------------------------------------------------------------------------
  3631. void __fastcall TCustomScpExplorerForm::ExecutedFileUploadComplete(TObject * Sender)
  3632. {
  3633. EditorFormFileUploadComplete(DebugNotNull(dynamic_cast<TForm *>(Sender)));
  3634. }
  3635. //---------------------------------------------------------------------------
  3636. void __fastcall TCustomScpExplorerForm::RemoteDirViewEnter(TObject * /*Sender*/)
  3637. {
  3638. SideEnter(osRemote);
  3639. }
  3640. //---------------------------------------------------------------------------
  3641. void __fastcall TCustomScpExplorerForm::RemoteDriveViewEnter(TObject * /*Sender*/)
  3642. {
  3643. MakeNextInTabOrder(RemoteDirPanel, RemoteDrivePanel);
  3644. SideEnter(osRemote);
  3645. }
  3646. //---------------------------------------------------------------------------
  3647. void __fastcall TCustomScpExplorerForm::SideEnter(TOperationSide Side)
  3648. {
  3649. FCurrentSide = Side;
  3650. if (Visible)
  3651. {
  3652. UpdateControls();
  3653. }
  3654. }
  3655. //---------------------------------------------------------------------------
  3656. void __fastcall TCustomScpExplorerForm::DeleteFiles(TOperationSide Side,
  3657. TStrings * FileList, bool Alternative)
  3658. {
  3659. TCustomDirView * DView = DirView(Side);
  3660. DView->SaveSelection();
  3661. DView->SaveSelectedNames();
  3662. DebugAssert(!FAlternativeDelete);
  3663. FAlternativeDelete = Alternative;
  3664. try
  3665. {
  3666. if (!IsSideLocalBrowser(Side))
  3667. {
  3668. DebugAssert(Terminal != NULL);
  3669. Terminal->DeleteFiles(FileList, FLAGMASK(Alternative, dfAlternative));
  3670. }
  3671. else
  3672. {
  3673. try
  3674. {
  3675. TTerminalManager::Instance()->LocalTerminal->DeleteLocalFiles(FileList, FLAGMASK(Alternative, dfAlternative));
  3676. }
  3677. __finally
  3678. {
  3679. ReloadLocalDirectory();
  3680. }
  3681. }
  3682. FAlternativeDelete = false;
  3683. }
  3684. catch(...)
  3685. {
  3686. FAlternativeDelete = false;
  3687. DView->DiscardSavedSelection();
  3688. throw;
  3689. }
  3690. DView->RestoreSelection();
  3691. }
  3692. //---------------------------------------------------------------------------
  3693. void __fastcall TCustomScpExplorerForm::LockFiles(TStrings * FileList, bool Lock)
  3694. {
  3695. DebugAssert(Terminal);
  3696. DebugAssert(!IsLocalBrowserMode());
  3697. RemoteDirView->SaveSelection();
  3698. RemoteDirView->SaveSelectedNames();
  3699. try
  3700. {
  3701. if (Lock)
  3702. {
  3703. Terminal->LockFiles(FileList);
  3704. }
  3705. else
  3706. {
  3707. Terminal->UnlockFiles(FileList);
  3708. }
  3709. }
  3710. catch(...)
  3711. {
  3712. RemoteDirView->DiscardSavedSelection();
  3713. throw;
  3714. }
  3715. RemoteDirView->RestoreSelection();
  3716. }
  3717. //---------------------------------------------------------------------------
  3718. bool __fastcall TCustomScpExplorerForm::RemoteTransferDialog(TManagedTerminal *& Session,
  3719. TStrings * FileList, UnicodeString & Target, UnicodeString & FileMask, bool & DirectCopy,
  3720. bool NoConfirmation, bool Move)
  3721. {
  3722. DebugAssert(Terminal != NULL);
  3723. DebugAssert(!IsLocalBrowserMode());
  3724. // update Terminal->StateData->RemoteDirectory
  3725. UpdateTerminal(Terminal);
  3726. if (Session == NULL)
  3727. {
  3728. Session = TTerminalManager::Instance()->ActiveTerminal;
  3729. }
  3730. if (Session == TTerminalManager::Instance()->ActiveTerminal)
  3731. {
  3732. if (RemoteDriveView->DropTarget != NULL)
  3733. {
  3734. Target = RemoteDriveView->NodePathName(RemoteDriveView->DropTarget);
  3735. }
  3736. else if (RemoteDirView->DropTarget != NULL)
  3737. {
  3738. DebugAssert(RemoteDirView->ItemIsDirectory(RemoteDirView->DropTarget));
  3739. Target = RemoteDirView->ItemFullFileName(RemoteDirView->DropTarget);
  3740. }
  3741. else
  3742. {
  3743. Target = RemoteDirView->Path;
  3744. }
  3745. }
  3746. else
  3747. {
  3748. Target = Session->StateData->RemoteDirectory;
  3749. }
  3750. Target = UnixIncludeTrailingBackslash(Target);
  3751. if (FileList->Count == 1)
  3752. {
  3753. FileMask = DelimitFileNameMask(UnixExtractFileName(FileList->Strings[0]));
  3754. }
  3755. else
  3756. {
  3757. FileMask = AnyMask;
  3758. }
  3759. DirectCopy = FTerminal->IsCapable[fcRemoteCopy] || FTerminal->IsCapable[fcSecondaryShell];
  3760. bool Result = true;
  3761. if (!NoConfirmation)
  3762. {
  3763. bool Multi = (FileList->Count > 1);
  3764. if (Move)
  3765. {
  3766. Result = DoRemoteMoveDialog(Multi, Target, FileMask);
  3767. }
  3768. else
  3769. {
  3770. TStrings * Sessions = TTerminalManager::Instance()->TerminalList;
  3771. TStrings * Directories = new TStringList;
  3772. try
  3773. {
  3774. for (int Index = 0; Index < Sessions->Count; Index++)
  3775. {
  3776. TManagedTerminal * Terminal =
  3777. dynamic_cast<TManagedTerminal *>(Sessions->Objects[Index]);
  3778. Directories->Add(Terminal->StateData->RemoteDirectory);
  3779. }
  3780. TDirectRemoteCopy AllowDirectCopy;
  3781. if (FTerminal->IsCapable[fcRemoteCopy] || FTerminal->CommandSessionOpened)
  3782. {
  3783. DebugAssert(DirectCopy);
  3784. AllowDirectCopy = drcAllow;
  3785. }
  3786. else if (FTerminal->IsCapable[fcSecondaryShell])
  3787. {
  3788. DebugAssert(DirectCopy);
  3789. AllowDirectCopy = drcConfirmCommandSession;
  3790. }
  3791. else
  3792. {
  3793. DebugAssert(!DirectCopy);
  3794. AllowDirectCopy = drcDisallow;
  3795. }
  3796. void * ASession = Session;
  3797. Result = DoRemoteCopyDialog(Sessions, Directories, AllowDirectCopy,
  3798. Multi, ASession, Target, FileMask, DirectCopy, TTerminalManager::Instance()->ActiveTerminal);
  3799. Session = static_cast<TManagedTerminal *>(ASession);
  3800. }
  3801. __finally
  3802. {
  3803. delete Directories;
  3804. }
  3805. }
  3806. }
  3807. return Result;
  3808. }
  3809. //---------------------------------------------------------------------------
  3810. bool __fastcall TCustomScpExplorerForm::RemoteTransferFiles(
  3811. TStrings * FileList, bool NoConfirmation, bool Move, TManagedTerminal * Session)
  3812. {
  3813. DebugAssert(!IsLocalBrowserMode());
  3814. bool DirectCopy;
  3815. UnicodeString Target, FileMask;
  3816. bool Result = RemoteTransferDialog(Session, FileList, Target, FileMask, DirectCopy, NoConfirmation, Move);
  3817. if (Result)
  3818. {
  3819. if (!Move && !DirectCopy)
  3820. {
  3821. Configuration->Usage->Inc("RemoteCopyTemp");
  3822. UnicodeString RootTempDir;
  3823. UnicodeString TempDir;
  3824. TemporarilyDownloadFiles(FileList, false, RootTempDir, TempDir, false, false, false);
  3825. TStrings * TemporaryFilesList = new TStringList();
  3826. try
  3827. {
  3828. TMakeLocalFileListParams MakeFileListParam;
  3829. MakeFileListParam.FileList = TemporaryFilesList;
  3830. MakeFileListParam.FileTimes = NULL;
  3831. MakeFileListParam.IncludeDirs = true;
  3832. MakeFileListParam.Recursive = false;
  3833. ProcessLocalDirectory(TempDir, FTerminal->MakeLocalFileList, &MakeFileListParam);
  3834. TTerminalManager::Instance()->ActiveTerminal = Session;
  3835. if (TemporaryFilesList->Count > 0)
  3836. {
  3837. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  3838. CopyParam.FileMask = FileMask;
  3839. DebugAssert(!FAutoOperation);
  3840. FAutoOperation = true;
  3841. FTerminal->CopyToRemote(TemporaryFilesList, Target, &CopyParam, cpTemporary, NULL);
  3842. }
  3843. }
  3844. __finally
  3845. {
  3846. delete TemporaryFilesList;
  3847. FAutoOperation = false;
  3848. if (!RootTempDir.IsEmpty())
  3849. {
  3850. RecursiveDeleteFile(ExcludeTrailingBackslash(RootTempDir), false);
  3851. }
  3852. }
  3853. }
  3854. else
  3855. {
  3856. RemoteDirView->SaveSelection();
  3857. RemoteDirView->SaveSelectedNames();
  3858. try
  3859. {
  3860. if (Move)
  3861. {
  3862. Configuration->Usage->Inc("RemoteMove");
  3863. Terminal->MoveFiles(FileList, Target, FileMask);
  3864. }
  3865. else
  3866. {
  3867. Configuration->Usage->Inc("RemoteCopyDirect");
  3868. DebugAssert(DirectCopy);
  3869. DebugAssert(Session == FTerminal);
  3870. if (FTerminal->IsCapable[fcRemoteCopy] ||
  3871. FTerminal->CommandSessionOpened ||
  3872. CommandSessionFallback())
  3873. {
  3874. Terminal->CopyFiles(FileList, Target, FileMask);
  3875. }
  3876. }
  3877. }
  3878. catch(...)
  3879. {
  3880. RemoteDirView->DiscardSavedSelection();
  3881. throw;
  3882. }
  3883. RemoteDirView->RestoreSelection();
  3884. }
  3885. }
  3886. return Result;
  3887. }
  3888. //---------------------------------------------------------------------------
  3889. void __fastcall TCustomScpExplorerForm::CreateRemoteDirectory(
  3890. const UnicodeString & Path, TRemoteProperties & Properties)
  3891. {
  3892. DebugAssert(!IsLocalBrowserMode());
  3893. Properties.Valid = Properties.Valid << vpEncrypt;
  3894. Properties.Encrypt = GUIConfiguration->CurrentCopyParam.EncryptNewFiles;
  3895. RemoteDirView->CreateDirectoryEx(Path, &Properties);
  3896. }
  3897. //---------------------------------------------------------------------------
  3898. void __fastcall TCustomScpExplorerForm::CreateDirectory(TOperationSide Side)
  3899. {
  3900. Side = GetSide(Side);
  3901. TRemoteProperties Properties = GUIConfiguration->NewDirectoryProperties;
  3902. TRemoteProperties * AProperties = (!IsSideLocalBrowser(Side) ? &Properties : NULL);
  3903. UnicodeString Name = LoadStr(NEW_FOLDER);
  3904. int AllowedChanges =
  3905. FLAGMASK(!IsSideLocalBrowser(Side) && Terminal->IsCapable[fcModeChanging], cpMode);
  3906. bool SaveSettings = false;
  3907. if (DoCreateDirectoryDialog(Name, AProperties, AllowedChanges, SaveSettings))
  3908. {
  3909. TWindowLock Lock(this);
  3910. if (!IsSideLocalBrowser(Side))
  3911. {
  3912. if (SaveSettings)
  3913. {
  3914. GUIConfiguration->NewDirectoryProperties = Properties;
  3915. }
  3916. CreateRemoteDirectory(Name, Properties);
  3917. }
  3918. else
  3919. {
  3920. DirView(Side)->CreateDirectory(Name);
  3921. }
  3922. }
  3923. }
  3924. //---------------------------------------------------------------------------
  3925. void __fastcall TCustomScpExplorerForm::HomeDirectory(TOperationSide Side)
  3926. {
  3927. TWindowLock Lock(this);
  3928. DirView(Side)->ExecuteHomeDirectory();
  3929. }
  3930. //---------------------------------------------------------------------------
  3931. void __fastcall TCustomScpExplorerForm::OpenDirectory(TOperationSide Side)
  3932. {
  3933. DoOpenDirectoryDialog(odBrowse, Side);
  3934. }
  3935. //---------------------------------------------------------------------------
  3936. bool __fastcall TCustomScpExplorerForm::OpenBookmark(TOperationSide Side, TBookmark * Bookmark)
  3937. {
  3938. UnicodeString Path = Bookmark->GetSideDirectory(Side);
  3939. bool Result = !Path.IsEmpty();
  3940. if (Result)
  3941. {
  3942. // While we might get here when the session is closed (from location profiles),
  3943. // it's not a problem as the Path setter is noop then
  3944. DirView(Side)->Path = Path;
  3945. }
  3946. return Result;
  3947. }
  3948. //---------------------------------------------------------------------------
  3949. void __fastcall TCustomScpExplorerForm::CalculateSize(
  3950. TStrings * FileList, __int64 & Size, TCalculateSizeStats & Stats,
  3951. bool & Close)
  3952. {
  3953. // terminal can be already closed (e.g. dropped connection)
  3954. if (Terminal != NULL)
  3955. {
  3956. try
  3957. {
  3958. Terminal->CalculateFilesSize(FileList, Size, 0, NULL, true, Stats);
  3959. }
  3960. catch(...)
  3961. {
  3962. if (!Terminal->Active)
  3963. {
  3964. Close = true;
  3965. }
  3966. throw;
  3967. }
  3968. }
  3969. }
  3970. //---------------------------------------------------------------------------
  3971. void __fastcall TCustomScpExplorerForm::CalculateChecksum(const UnicodeString & Alg,
  3972. TStrings * FileList, TCalculatedChecksumEvent OnCalculatedChecksum,
  3973. bool & Close)
  3974. {
  3975. // terminal can be already closed (e.g. dropped connection)
  3976. if (Terminal != NULL)
  3977. {
  3978. Configuration->Usage->Inc(L"ChecksumCalculated");
  3979. try
  3980. {
  3981. Terminal->CalculateFilesChecksum(Alg, FileList, NULL, OnCalculatedChecksum);
  3982. }
  3983. catch(...)
  3984. {
  3985. if (!Terminal->Active)
  3986. {
  3987. Close = true;
  3988. }
  3989. throw;
  3990. }
  3991. }
  3992. }
  3993. //---------------------------------------------------------------------------
  3994. bool __fastcall TCustomScpExplorerForm::SetProperties(TOperationSide Side, TStrings * FileList)
  3995. {
  3996. bool Result;
  3997. if (!IsSideLocalBrowser(Side))
  3998. {
  3999. TRemoteTokenList * GroupList = NULL;
  4000. TRemoteTokenList * UserList = NULL;
  4001. try
  4002. {
  4003. TRemoteProperties CurrentProperties;
  4004. if (Terminal->LoadFilesProperties(FileList))
  4005. {
  4006. RemoteDirView->Invalidate();
  4007. }
  4008. bool CapableGroupChanging = Terminal->IsCapable[fcGroupChanging];
  4009. bool CapableOwnerChanging = Terminal->IsCapable[fcOwnerChanging];
  4010. if (CapableGroupChanging || CapableOwnerChanging)
  4011. {
  4012. if (CapableGroupChanging)
  4013. {
  4014. GroupList = Terminal->Groups->Duplicate();
  4015. }
  4016. if (CapableOwnerChanging)
  4017. {
  4018. UserList = Terminal->Users->Duplicate();
  4019. }
  4020. TRemoteDirectory * Files = Terminal->Files;
  4021. int Count = Files->Count;
  4022. if (Count > 100)
  4023. {
  4024. Count = 100;
  4025. }
  4026. for (int Index = 0; Index < Count; Index++)
  4027. {
  4028. TRemoteFile * File = Files->Files[Index];
  4029. if (CapableGroupChanging)
  4030. {
  4031. GroupList->AddUnique(File->Group);
  4032. }
  4033. if (CapableOwnerChanging)
  4034. {
  4035. UserList->AddUnique(File->Owner);
  4036. }
  4037. }
  4038. // if we haven't collected tokens for all files in current directory,
  4039. // make sure we collect them at least for all selected files.
  4040. // (note that so far the files in FileList has to be from current direcotry)
  4041. if (Count < Files->Count)
  4042. {
  4043. for (int Index = 0; Index < FileList->Count; Index++)
  4044. {
  4045. TRemoteFile * File = (TRemoteFile *)(FileList->Objects[Index]);
  4046. if (CapableGroupChanging)
  4047. {
  4048. GroupList->AddUnique(File->Group);
  4049. }
  4050. if (CapableOwnerChanging)
  4051. {
  4052. UserList->AddUnique(File->Owner);
  4053. }
  4054. }
  4055. }
  4056. }
  4057. CurrentProperties = TRemoteProperties::CommonProperties(FileList);
  4058. int Flags = 0;
  4059. if (Terminal->IsCapable[fcModeChanging]) Flags |= cpMode;
  4060. if (CapableOwnerChanging) Flags |= cpOwner;
  4061. if (CapableGroupChanging) Flags |= cpGroup;
  4062. TCalculateChecksumEvent CalculateChecksumEvent = NULL;
  4063. if (Terminal->IsCapable[fcCalculatingChecksum])
  4064. {
  4065. CalculateChecksumEvent = CalculateChecksum;
  4066. }
  4067. std::unique_ptr<TStrings> ChecksumAlgs(new TStringList());
  4068. Terminal->GetSupportedChecksumAlgs(ChecksumAlgs.get());
  4069. TRemoteProperties NewProperties = CurrentProperties;
  4070. Result =
  4071. DoPropertiesDialog(FileList, RemoteDirView->PathName,
  4072. GroupList, UserList, ChecksumAlgs.get(), &NewProperties, Flags,
  4073. Terminal->IsCapable[fcGroupOwnerChangingByID],
  4074. CalculateSize, CalculateChecksumEvent);
  4075. if (Result)
  4076. {
  4077. NewProperties = TRemoteProperties::ChangedProperties(CurrentProperties, NewProperties);
  4078. Terminal->ChangeFilesProperties(FileList, &NewProperties);
  4079. }
  4080. }
  4081. __finally
  4082. {
  4083. delete GroupList;
  4084. delete UserList;
  4085. }
  4086. }
  4087. else
  4088. {
  4089. DirView(Side)->DisplayPropertiesMenu();
  4090. Result = true;
  4091. }
  4092. return Result;
  4093. }
  4094. //---------------------------------------------------------------------------
  4095. void __fastcall TCustomScpExplorerForm::KeyProcessed(Word & Key, TShiftState Shift)
  4096. {
  4097. if (Shift * AllKeyShiftStates() == (TShiftState() << ssAlt))
  4098. {
  4099. FIgnoreNextDialogChar = Key;
  4100. }
  4101. Key = 0;
  4102. }
  4103. //---------------------------------------------------------------------------
  4104. void __fastcall TCustomScpExplorerForm::CheckCustomCommandShortCut(
  4105. TCustomCommandList * List, Word & Key, Classes::TShiftState Shift, TShortCut KeyShortCut)
  4106. {
  4107. const TCustomCommandType * Command = List->Find(KeyShortCut);
  4108. if (Command != NULL)
  4109. {
  4110. KeyProcessed(Key, Shift);
  4111. if (CustomCommandState(*Command, false, ccltAll) > 0)
  4112. {
  4113. TAutoFlag DontCopyCommandToClipboardFlag(DontCopyCommandToClipboard);
  4114. ExecuteFileOperationCommand(foCustomCommand, osRemote,
  4115. false, false, const_cast<TCustomCommandType *>(Command));
  4116. }
  4117. }
  4118. }
  4119. //---------------------------------------------------------------------------
  4120. void __fastcall TCustomScpExplorerForm::KeyDown(Word & Key, Classes::TShiftState Shift)
  4121. {
  4122. if (QueueView3->Focused() && (QueueView3->OnKeyDown != NULL))
  4123. {
  4124. QueueView3->OnKeyDown(QueueView3, Key, Shift);
  4125. }
  4126. if (!DirView(osCurrent)->IsEditing())
  4127. {
  4128. TShortCut KeyShortCut = ShortCut(Key, Shift);
  4129. for (int Index = 0; Index < NonVisualDataModule->ExplorerActions->ActionCount; Index++)
  4130. {
  4131. TAction * Action = (TAction *)NonVisualDataModule->ExplorerActions->Actions[Index];
  4132. if (((Action->ShortCut == KeyShortCut) ||
  4133. (Action->SecondaryShortCuts->IndexOfShortCut(KeyShortCut) >= 0)) &&
  4134. AllowedAction(Action, aaShortCut))
  4135. {
  4136. // Has to be called before the action as the dialog char is already in queue.
  4137. // So when the action consumes message queue, we already need to have the
  4138. // FIgnoreNextDialogChar set
  4139. KeyProcessed(Key, Shift);
  4140. // Reset reference to previous component (when menu/toolbar was clicked).
  4141. // Needed to detect that action was invoked by keyboard shortcut
  4142. // in TNonVisualDataModule::ExplorerActionsExecute
  4143. Action->ActionComponent = NULL;
  4144. Action->Execute();
  4145. return;
  4146. }
  4147. }
  4148. for (int i = 0; i < TTerminalManager::Instance()->Count; i++)
  4149. {
  4150. if (NonVisualDataModule->OpenSessionShortCut(i) == KeyShortCut)
  4151. {
  4152. KeyProcessed(Key, Shift);
  4153. TTerminalManager::Instance()->ActiveTerminalIndex = i;
  4154. return;
  4155. }
  4156. }
  4157. if (Key == VK_TAB && Shift.Contains(ssCtrl))
  4158. {
  4159. KeyProcessed(Key, Shift);
  4160. TTerminalManager::Instance()->CycleTerminals(!Shift.Contains(ssShift));
  4161. }
  4162. TShortCut CustomShortCut = NormalizeCustomShortCut(KeyShortCut);
  4163. if (IsCustomShortCut(CustomShortCut))
  4164. {
  4165. CheckCustomCommandShortCut(WinConfiguration->CustomCommandList, Key, Shift, CustomShortCut);
  4166. CheckCustomCommandShortCut(WinConfiguration->ExtensionList, Key, Shift, CustomShortCut);
  4167. if (WinConfiguration->SharedBookmarks != NULL)
  4168. {
  4169. TBookmark * Bookmark = WinConfiguration->SharedBookmarks->FindByShortCut(CustomShortCut);
  4170. if ((Bookmark != NULL) &&
  4171. OpenBookmark(FCurrentSide, Bookmark))
  4172. {
  4173. KeyProcessed(Key, Shift);
  4174. }
  4175. }
  4176. }
  4177. }
  4178. TForm::KeyDown(Key, Shift);
  4179. }
  4180. //---------------------------------------------------------------------------
  4181. void __fastcall TCustomScpExplorerForm::InitStatusBar()
  4182. {
  4183. DebugAssert(Terminal != NULL);
  4184. const TSessionInfo & SessionInfo = Terminal->GetSessionInfo();
  4185. const TFileSystemInfo & FileSystemInfo = Terminal->GetFileSystemInfo();
  4186. TTBXStatusBar * SessionStatusBar = (TTBXStatusBar *)GetComponent(fcStatusBar);
  4187. int Offset = SessionStatusBar->Panels->Count - SessionPanelCount;
  4188. bool SecurityEnabled = !SessionInfo.SecurityProtocolName.IsEmpty();
  4189. SessionStatusBar->Panels->Items[Offset + 0]->Enabled = SecurityEnabled;
  4190. // expanded from ?: to avoid memory leaks
  4191. if (SecurityEnabled)
  4192. {
  4193. SessionStatusBar->Panels->Items[Offset + 0]->Hint =
  4194. FMTLOAD(STATUS_SECURE, (SessionInfo.SecurityProtocolName));
  4195. }
  4196. else
  4197. {
  4198. SessionStatusBar->Panels->Items[Offset + 0]->Hint = LoadStr(STATUS_INSECURE);
  4199. }
  4200. if (FileSystemInfo.ProtocolName.IsEmpty())
  4201. {
  4202. SessionStatusBar->Panels->Items[Offset + 1]->Caption = SessionInfo.ProtocolName;
  4203. }
  4204. else
  4205. {
  4206. SessionStatusBar->Panels->Items[Offset + 1]->Caption = FileSystemInfo.ProtocolName;
  4207. }
  4208. SessionStatusBar->Panels->Items[Offset + 1]->Hint = LoadStr(STATUS_PROTOCOL_HINT);
  4209. SessionStatusBar->Panels->Items[Offset + 2]->Enabled =
  4210. (!SessionInfo.CSCompression.IsEmpty() || !SessionInfo.SCCompression.IsEmpty());
  4211. UnicodeString CSCompressionStr = DefaultStr(SessionInfo.CSCompression, LoadStr(NO_STR));
  4212. UnicodeString SCCompressionStr = DefaultStr(SessionInfo.SCCompression, LoadStr(NO_STR));
  4213. UnicodeString CompressionStr = CSCompressionStr;
  4214. if (CSCompressionStr != SCCompressionStr)
  4215. {
  4216. CompressionStr += UnicodeString(L"/") + SCCompressionStr;
  4217. }
  4218. SessionStatusBar->Panels->Items[Offset + 2]->Hint = FMTLOAD(STATUS_COMPRESSION_HINT, (CompressionStr));
  4219. SessionStatusBar->Panels->Items[Offset + 3]->Hint = LoadStr(STATUS_DURATION_HINT);
  4220. UpdateStatusBar();
  4221. }
  4222. //---------------------------------------------------------------------------
  4223. void __fastcall TCustomScpExplorerForm::UpdateStatusBar()
  4224. {
  4225. TTBXStatusBar * SessionStatusBar = (TTBXStatusBar *)GetComponent(fcStatusBar);
  4226. DebugAssert(SessionStatusBar != NULL);
  4227. if (!Terminal || !Terminal->Active || Terminal->Status < ssOpened)
  4228. {
  4229. // note: (Terminal->Status < sshReady) currently never happens here,
  4230. // so STATUS_CONNECTING is never used
  4231. SessionStatusBar->SimplePanel = true;
  4232. SessionStatusBar->SimpleText = LoadStr(
  4233. !Terminal || !Terminal->Active ? STATUS_NOT_CONNECTED : STATUS_CONNECTING);
  4234. }
  4235. else
  4236. {
  4237. DebugAssert(Terminal);
  4238. SessionStatusBar->SimplePanel = false;
  4239. const TSessionInfo & SessionInfo = Terminal->GetSessionInfo();
  4240. if (!FNote.IsEmpty())
  4241. {
  4242. SessionStatusBar->Panels->Items[0]->Caption = FNote;
  4243. }
  4244. else
  4245. {
  4246. UpdateStatusPanelText(SessionStatusBar->Panels->Items[0]);
  4247. }
  4248. SessionStatusBar->Panels->Items[0]->Hint = FNoteHints;
  4249. SessionStatusBar->Panels->Items[SessionStatusBar->Panels->Count - 1]->Caption =
  4250. FormatDateTimeSpan(Configuration->TimeFormat, Now() - SessionInfo.LoginTime);
  4251. }
  4252. }
  4253. //---------------------------------------------------------------------------
  4254. void __fastcall TCustomScpExplorerForm::UpdateStatusPanelText(TTBXStatusPanel * Panel)
  4255. {
  4256. Panel->Caption = L"";
  4257. }
  4258. //---------------------------------------------------------------------------
  4259. void __fastcall TCustomScpExplorerForm::Idle()
  4260. {
  4261. if (FShowing || FStandaloneEditing)
  4262. {
  4263. FEditorManager->Check();
  4264. // make sure that Idle is called before update queue, as it may invoke QueueEvent
  4265. // that needs to know if queue view is visible (and it may be closed after queue update)
  4266. TTerminalManager::Instance()->Idle(FDoNotIdleCurrentTerminal > 0);
  4267. }
  4268. if (FShowing)
  4269. {
  4270. if (!NonVisualDataModule->Busy &&
  4271. // Menu is opened or drag&drop is going on
  4272. (Mouse->Capture == NULL))
  4273. {
  4274. if (FRefreshRemoteDirectory)
  4275. {
  4276. if ((Terminal != NULL) && (Terminal->Status == ssOpened))
  4277. {
  4278. Terminal->RefreshDirectory();
  4279. }
  4280. FRefreshRemoteDirectory = false;
  4281. }
  4282. if (FRefreshLocalDirectory)
  4283. {
  4284. ReloadLocalDirectory();
  4285. FRefreshLocalDirectory = false;
  4286. }
  4287. if (WinConfiguration->RefreshRemotePanel)
  4288. {
  4289. if ((Terminal != NULL) && (Terminal->Status == ssOpened) &&
  4290. (Now() - Terminal->DirectoryLoaded > WinConfiguration->RefreshRemotePanelInterval))
  4291. {
  4292. RemoteDirView->ReloadDirectory();
  4293. }
  4294. }
  4295. if (!FStarted)
  4296. {
  4297. FStarted = true;
  4298. InterfaceStarted();
  4299. }
  4300. }
  4301. }
  4302. if (FShowing || FStandaloneEditing)
  4303. {
  4304. if (FQueueStatusInvalidated)
  4305. {
  4306. UpdateQueueStatus(false);
  4307. }
  4308. RefreshQueueItems();
  4309. }
  4310. if (FShowing)
  4311. {
  4312. UpdateStatusBar();
  4313. }
  4314. FIgnoreNextDialogChar = 0;
  4315. }
  4316. //---------------------------------------------------------------------------
  4317. void __fastcall TCustomScpExplorerForm::UserActionTimer(TObject * /*Sender*/)
  4318. {
  4319. try
  4320. {
  4321. FUserActionTimer->Enabled = false;
  4322. if (IsQueueAutoPopup() && (FPendingQueueActionItem != NULL))
  4323. {
  4324. if (TQueueItem::IsUserActionStatus(FPendingQueueActionItem->Status))
  4325. {
  4326. FPendingQueueActionItem->ProcessUserAction();
  4327. }
  4328. }
  4329. }
  4330. __finally
  4331. {
  4332. FPendingQueueActionItem = NULL;
  4333. }
  4334. }
  4335. //---------------------------------------------------------------------------
  4336. void __fastcall TCustomScpExplorerForm::ApplicationMinimize(TObject * /*Sender*/)
  4337. {
  4338. if (WinConfiguration->MinimizeToTray)
  4339. {
  4340. UpdateTrayIcon();
  4341. FTrayIcon->Visible = true;
  4342. if (Visible)
  4343. {
  4344. ShowWindow(Handle, SW_HIDE);
  4345. }
  4346. }
  4347. }
  4348. //---------------------------------------------------------------------------
  4349. void __fastcall TCustomScpExplorerForm::ApplicationRestore(TObject * /*Sender*/)
  4350. {
  4351. // WORKAROUND
  4352. // When restoring maximized window from minimization,
  4353. // rarely some controls do not align properly.
  4354. // Two instances seen (both for Commander):
  4355. // - When restoring, window is temporarily narrower (not maximized),
  4356. // causing toolbars on TopDock to wrap and dock to expand horizontally.
  4357. // Once maximized already, top dock shinks back, but the session PageControl,
  4358. // do not align up, leaving space between TopDock and PageControl.
  4359. // - Similar issue seen with LocalDirView not aligning down to status bar.
  4360. for (int Index = 0; Index < ControlCount; Index++)
  4361. {
  4362. RealignControl(Controls[Index]);
  4363. }
  4364. if (FTrayIcon->Visible)
  4365. {
  4366. FTrayIcon->Visible = false;
  4367. if (Visible)
  4368. {
  4369. ShowWindow(Handle, SW_SHOW);
  4370. }
  4371. }
  4372. if (FNeedSession && DebugAlwaysTrue(Terminal == NULL))
  4373. {
  4374. FNeedSession = false;
  4375. NonVisualDataModule->StartBusy();
  4376. try
  4377. {
  4378. NeedSession(true);
  4379. }
  4380. __finally
  4381. {
  4382. NonVisualDataModule->EndBusy();
  4383. }
  4384. }
  4385. }
  4386. //---------------------------------------------------------------------------
  4387. void __fastcall TCustomScpExplorerForm::UpdateTrayIcon()
  4388. {
  4389. FTrayIcon->Hint = GetMainForm()->Caption;
  4390. }
  4391. //---------------------------------------------------------------------------
  4392. void __fastcall TCustomScpExplorerForm::ApplicationTitleChanged()
  4393. {
  4394. UpdateTrayIcon();
  4395. }
  4396. //---------------------------------------------------------------------------
  4397. void __fastcall TCustomScpExplorerForm::RestoreApp()
  4398. {
  4399. // workaround
  4400. // TApplication.WndProc sets TApplication.FAppIconic to false,
  4401. // on WM_ACTIVATEAPP, what renders TApplication.Restore no-op function.
  4402. // But WM_ACTIVATEAPP message can be received even when
  4403. // the main window is minimized to the tray and internal editor window is focused
  4404. // (after another application was previously active)
  4405. if (::IsIconic(Handle))
  4406. {
  4407. if (!IsAppIconic())
  4408. {
  4409. SetAppIconic(true);
  4410. }
  4411. }
  4412. ::ApplicationRestore();
  4413. Application->BringToFront();
  4414. }
  4415. //---------------------------------------------------------------------------
  4416. void __fastcall TCustomScpExplorerForm::TrayIconClick(TObject * /*Sender*/)
  4417. {
  4418. RestoreApp();
  4419. }
  4420. //---------------------------------------------------------------------------
  4421. void __fastcall TCustomScpExplorerForm::NewSession(bool FromSite, const UnicodeString & SessionUrl)
  4422. {
  4423. if (OpenInNewWindow())
  4424. {
  4425. // todo: Pass FromSite
  4426. ExecuteNewInstance(SessionUrl);
  4427. }
  4428. else
  4429. {
  4430. TTerminalManager::Instance()->NewSession(FromSite, SessionUrl);
  4431. }
  4432. }
  4433. //---------------------------------------------------------------------------
  4434. UnicodeString __fastcall TCustomScpExplorerForm::SaveHiddenDuplicateSession(TSessionData * SessionData)
  4435. {
  4436. UnicodeString SessionName = StoredSessions->HiddenPrefix + Terminal->SessionData->SessionName;
  4437. StoredSessions->NewSession(SessionName, SessionData);
  4438. // modified only, explicit
  4439. StoredSessions->Save(false, true);
  4440. // encode session name because of slashes in hierarchical sessions
  4441. return EncodeUrlString(SessionName);
  4442. }
  4443. //---------------------------------------------------------------------------
  4444. UnicodeString __fastcall TCustomScpExplorerForm::CreateHiddenDuplicateSession()
  4445. {
  4446. // current working directories become defaults here, what is not right
  4447. std::unique_ptr<TSessionData> SessionData(CloneCurrentSessionData());
  4448. return SaveHiddenDuplicateSession(SessionData.get());
  4449. }
  4450. //---------------------------------------------------------------------------
  4451. void __fastcall TCustomScpExplorerForm::DuplicateSession()
  4452. {
  4453. if (OpenInNewWindow())
  4454. {
  4455. ExecuteNewInstance(CreateHiddenDuplicateSession());
  4456. }
  4457. else
  4458. {
  4459. // current working directories become defaults here, what is not right
  4460. std::unique_ptr<TSessionData> SessionData(CloneCurrentSessionData());
  4461. TTerminalManager * Manager = TTerminalManager::Instance();
  4462. TManagedTerminal * Terminal = Manager->NewManagedTerminal(SessionData.get());
  4463. Manager->ActiveTerminal = Terminal;
  4464. }
  4465. }
  4466. //---------------------------------------------------------------------------
  4467. void __fastcall TCustomScpExplorerForm::RenameSession()
  4468. {
  4469. UnicodeString Name = Terminal->SessionData->SessionName;
  4470. if (InputDialog(LoadStr(RENAME_SESSION_TITLE), LoadStr(RENAME_SESSION_PROMPT), Name, HELP_SESSION_RENAME))
  4471. {
  4472. // Checks for a slash only, so it's not that big deal that we do the check after submitting the dialog only.
  4473. TSessionData::ValidateName(Name);
  4474. // This is inconsistent with how color (for example) is handled.
  4475. Terminal->SessionData->Name = Name;
  4476. UpdateControls();
  4477. // Add/Remove distinguishing paths from sessions of the same name.
  4478. DoTerminalListChanged();
  4479. }
  4480. }
  4481. //---------------------------------------------------------------------------
  4482. bool __fastcall TCustomScpExplorerForm::CanCloseQueue(TTerminalQueue * Queue)
  4483. {
  4484. DebugAssert(Queue != NULL);
  4485. bool Result = Queue->IsEmpty;
  4486. if (!Result)
  4487. {
  4488. SetFocus();
  4489. Result = (MessageDialog(LoadStr(PENDING_QUEUE_ITEMS2), qtWarning, qaOK | qaCancel, HELP_NONE) == qaOK);
  4490. }
  4491. return Result;
  4492. }
  4493. //---------------------------------------------------------------------------
  4494. bool __fastcall TCustomScpExplorerForm::CanCloseQueue()
  4495. {
  4496. return CanCloseQueue(FQueue);
  4497. }
  4498. //---------------------------------------------------------------------------
  4499. void __fastcall TCustomScpExplorerForm::CloseSession()
  4500. {
  4501. if (CanCloseQueue())
  4502. {
  4503. TTerminalManager::Instance()->FreeActiveTerminal();
  4504. }
  4505. }
  4506. //---------------------------------------------------------------------------
  4507. void __fastcall TCustomScpExplorerForm::DisconnectSession()
  4508. {
  4509. if (CanCloseQueue())
  4510. {
  4511. TTerminalManager::Instance()->DisconnectActiveTerminal();
  4512. }
  4513. }
  4514. //---------------------------------------------------------------------------
  4515. void __fastcall TCustomScpExplorerForm::TerminalDisconnected()
  4516. {
  4517. DetachTerminal(Terminal);
  4518. RemoteDirView->Terminal = Terminal;
  4519. UpdateRemotePathComboBox(false);
  4520. UpdateControls();
  4521. }
  4522. //---------------------------------------------------------------------------
  4523. void __fastcall TCustomScpExplorerForm::ReconnectSession()
  4524. {
  4525. if (DebugAlwaysTrue(!Terminal->Active))
  4526. {
  4527. TTerminalManager::Instance()->ReconnectActiveTerminal();
  4528. }
  4529. }
  4530. //---------------------------------------------------------------------------
  4531. void __fastcall TCustomScpExplorerForm::OpenStoredSession(TSessionData * Data)
  4532. {
  4533. if (OpenInNewWindow())
  4534. {
  4535. // encode session name because of slashes in hierarchical sessions
  4536. ExecuteNewInstance(EncodeUrlString(Data->Name));
  4537. }
  4538. else
  4539. {
  4540. TTerminalManager * Manager = TTerminalManager::Instance();
  4541. TManagedTerminal * Terminal = Manager->NewManagedTerminal(Data);
  4542. Manager->ActiveTerminal = Terminal;
  4543. }
  4544. }
  4545. //---------------------------------------------------------------------------
  4546. void __fastcall TCustomScpExplorerForm::DoOpenFolderOrWorkspace(const UnicodeString & Name, bool ConnectFirstTerminal)
  4547. {
  4548. TTerminalManager * Manager = TTerminalManager::Instance();
  4549. std::unique_ptr<TObjectList> DataList(new TObjectList());
  4550. StoredSessions->GetFolderOrWorkspace(Name, DataList.get());
  4551. TManagedTerminal * FirstTerminal = Manager->NewTerminals(DataList.get());
  4552. // FirstTerminal can be null, if some of the
  4553. if (!ConnectFirstTerminal && (FirstTerminal != NULL))
  4554. {
  4555. FirstTerminal->Disconnected = true;
  4556. FirstTerminal->DisconnectedTemporarily = true;
  4557. }
  4558. Manager->ActiveTerminal = FirstTerminal;
  4559. }
  4560. //---------------------------------------------------------------------------
  4561. void __fastcall TCustomScpExplorerForm::OpenFolderOrWorkspace(const UnicodeString & Name)
  4562. {
  4563. if (OpenInNewWindow())
  4564. {
  4565. ExecuteNewInstance(Name);
  4566. }
  4567. else
  4568. {
  4569. DoOpenFolderOrWorkspace(Name, true);
  4570. }
  4571. }
  4572. //---------------------------------------------------------------------------
  4573. void __fastcall TCustomScpExplorerForm::FormCloseQuery(TObject * /*Sender*/,
  4574. bool &CanClose)
  4575. {
  4576. // See the comment in CloseApp()
  4577. if (NonVisualDataModule->Busy)
  4578. {
  4579. CanClose = false;
  4580. }
  4581. else if (Terminal != NULL)
  4582. {
  4583. if (Terminal->Active && WinConfiguration->ConfirmClosingSession)
  4584. {
  4585. unsigned int Result;
  4586. TMessageParams Params(mpNeverAskAgainCheck);
  4587. UnicodeString Message;
  4588. int Answers = qaOK | qaCancel;
  4589. if (TTerminalManager::Instance()->Count > 1)
  4590. {
  4591. if (!WinConfiguration->AutoSaveWorkspace)
  4592. {
  4593. Message = LoadStr(CLOSE_SESSIONS_WORKSPACE3);
  4594. }
  4595. else
  4596. {
  4597. Message = LoadStr(CLOSE_SESSIONS);
  4598. }
  4599. }
  4600. else
  4601. {
  4602. if (!WinConfiguration->AutoSaveWorkspace)
  4603. {
  4604. Message = LoadStr(CLOSE_SESSION_WORKSPACE);
  4605. }
  4606. else
  4607. {
  4608. Message = LoadStr(CLOSE_SESSION);
  4609. }
  4610. Message = FORMAT(Message, (Terminal->SessionData->SessionName));
  4611. }
  4612. UnicodeString Note;
  4613. if (WinConfiguration->AutoSaveWorkspace)
  4614. {
  4615. Note = FMTLOAD(AUTO_WORKSPACE, (WorkspaceName()));
  4616. }
  4617. else
  4618. {
  4619. Note = LoadStr(AUTO_WORKSPACE_ENABLE);
  4620. Answers = qaYes | qaNo | qaCancel;
  4621. }
  4622. Message = FORMAT("%s\n\n%s", (MainInstructions(Message), Note));
  4623. SetFocus();
  4624. Result = MessageDialog(Message, qtConfirmation,
  4625. Answers, HELP_CLOSE_SESSION_WORKSPACE, &Params);
  4626. if (Result == qaNeverAskAgain)
  4627. {
  4628. WinConfiguration->ConfirmClosingSession = false;
  4629. }
  4630. if (Result == qaNo)
  4631. {
  4632. CanClose = SaveWorkspace(true);
  4633. // note that the workspace will be saved redundatly again from FormClose
  4634. }
  4635. else
  4636. {
  4637. CanClose =
  4638. (Result == qaOK) ||
  4639. (Result == qaYes) || // CLOSE_SESSIONS_WORKSPACE variant
  4640. (Result == qaNeverAskAgain);
  4641. }
  4642. }
  4643. if (CanClose)
  4644. {
  4645. CanClose = CanCloseQueue();
  4646. }
  4647. }
  4648. if (CanClose)
  4649. {
  4650. CanClose =
  4651. FEditorManager->CloseInternalEditors(CloseInternalEditor) &&
  4652. FEditorManager->CloseExternalFilesWithoutProcess();
  4653. if (CanClose)
  4654. {
  4655. while (CanClose && (FLocalEditors->Count > 0))
  4656. {
  4657. int PrevCount = FLocalEditors->Count;
  4658. static_cast<TForm *>(FLocalEditors->Items[0])->Close();
  4659. CanClose = (FLocalEditors->Count < PrevCount);
  4660. }
  4661. if (CanClose)
  4662. {
  4663. CanClose = FEditorManager->Empty(true);
  4664. if (!CanClose)
  4665. {
  4666. SetFocus();
  4667. CanClose =
  4668. (MessageDialog(
  4669. LoadStr(PENDING_EDITORS), qtWarning, qaIgnore | qaCancel, HELP_NONE) == qaIgnore);
  4670. }
  4671. }
  4672. }
  4673. }
  4674. }
  4675. //---------------------------------------------------------------------------
  4676. void __fastcall TCustomScpExplorerForm::CloseInternalEditor(TObject * Sender)
  4677. {
  4678. TForm * Form = dynamic_cast<TForm *>(Sender);
  4679. DebugAssert(Form != NULL);
  4680. Form->Close();
  4681. }
  4682. //---------------------------------------------------------------------------
  4683. void __fastcall TCustomScpExplorerForm::ForceCloseInternalEditor(TObject * Sender)
  4684. {
  4685. TForm * Form = dynamic_cast<TForm *>(Sender);
  4686. delete Form;
  4687. }
  4688. //---------------------------------------------------------------------------
  4689. void __fastcall TCustomScpExplorerForm::ForceCloseLocalEditors()
  4690. {
  4691. while (FLocalEditors->Count > 0)
  4692. {
  4693. delete static_cast<TForm *>(FLocalEditors->Items[0]);
  4694. }
  4695. }
  4696. //---------------------------------------------------------------------------
  4697. void __fastcall TCustomScpExplorerForm::RemoteDirViewDisplayProperties(
  4698. TObject *Sender)
  4699. {
  4700. TStrings *FileList = ((TUnixDirView*)Sender)->CreateFileList(True, False, NULL);
  4701. try
  4702. {
  4703. SetProperties(osRemote, FileList);
  4704. }
  4705. __finally
  4706. {
  4707. delete FileList;
  4708. }
  4709. }
  4710. //---------------------------------------------------------------------------
  4711. void __fastcall TCustomScpExplorerForm::ComponentShowing(Byte Component, bool value)
  4712. {
  4713. if (value)
  4714. {
  4715. if (Component == fcCustomCommandsBand)
  4716. {
  4717. UpdateCustomCommandsToolbar();
  4718. }
  4719. }
  4720. }
  4721. //---------------------------------------------------------------------------
  4722. void __fastcall TCustomScpExplorerForm::SetComponentVisible(Byte Component, Boolean value)
  4723. {
  4724. TControl * Control = GetComponent(Component);
  4725. DebugAssert(Control);
  4726. bool Changed = (Control->Visible != value);
  4727. if (Changed)
  4728. {
  4729. ComponentShowing(Component, value);
  4730. TWinControl * WinControl = dynamic_cast<TWinControl*>(Control);
  4731. bool WasFocused = (WinControl != NULL) && (ActiveControl != NULL) &&
  4732. ((ActiveControl == WinControl) || (ActiveControl->Parent == WinControl));
  4733. if (value)
  4734. {
  4735. int RemainingHeight = Control->Parent->ClientHeight;
  4736. int RemainingWidth = Control->Parent->ClientWidth;
  4737. for (int i = 0; i < Control->Parent->ControlCount; i++)
  4738. {
  4739. TControl * ChildControl = Control->Parent->Controls[i];
  4740. if (ChildControl->Visible)
  4741. {
  4742. switch (ChildControl->Align)
  4743. {
  4744. case alTop:
  4745. case alBottom:
  4746. RemainingHeight -= ChildControl->Height;
  4747. break;
  4748. case alLeft:
  4749. case alRight:
  4750. RemainingWidth -= ChildControl->Width;
  4751. break;
  4752. }
  4753. }
  4754. }
  4755. int Reserve = ScaleByTextHeight(this, 32);
  4756. // queue in explorer, trees in commander
  4757. if (Control->Height > RemainingHeight - Reserve)
  4758. {
  4759. Control->Height = RemainingHeight / 2;
  4760. }
  4761. if (Control->Width > RemainingWidth - Reserve)
  4762. {
  4763. Control->Width = RemainingWidth / 2;
  4764. }
  4765. }
  4766. Control->Visible = value;
  4767. if (WasFocused && Visible)
  4768. {
  4769. DirView(osCurrent)->SetFocus();
  4770. }
  4771. FixControlsPlacement();
  4772. }
  4773. }
  4774. //---------------------------------------------------------------------------
  4775. bool __fastcall TCustomScpExplorerForm::GetComponentVisible(Byte Component)
  4776. {
  4777. TControl * Control = GetComponent(Component);
  4778. if (Control == NULL)
  4779. {
  4780. return false;
  4781. }
  4782. else
  4783. {
  4784. return Control->Visible;
  4785. }
  4786. }
  4787. //---------------------------------------------------------------------------
  4788. bool __fastcall TCustomScpExplorerForm::IsComponentPossible(Byte Component)
  4789. {
  4790. bool Result;
  4791. if ((Component == fcExplorerUpdatesBand) || (Component == fcCommanderUpdatesBand))
  4792. {
  4793. Result = !IsUWP();
  4794. }
  4795. else
  4796. {
  4797. Result = true;
  4798. }
  4799. return Result;
  4800. }
  4801. //---------------------------------------------------------------------------
  4802. void __fastcall TCustomScpExplorerForm::FixControlsPlacement()
  4803. {
  4804. if (DirView(osOther)->ItemFocused != NULL)
  4805. {
  4806. DirView(osOther)->ItemFocused->MakeVisible(false);
  4807. }
  4808. QueueSplitter->Visible = QueuePanel->Visible;
  4809. RemotePanelSplitter->Visible = RemoteDrivePanel->Visible;
  4810. }
  4811. //---------------------------------------------------------------------------
  4812. TControl * __fastcall TCustomScpExplorerForm::GetComponent(Byte Component)
  4813. {
  4814. switch (Component) {
  4815. case fcStatusBar: return RemoteStatusBar;
  4816. case fcRemotePopup: return reinterpret_cast<TControl *>(NonVisualDataModule->RemoteFilePopup);
  4817. case fcQueueView: return QueuePanel;
  4818. case fcQueueToolbar: return QueueDock;
  4819. case fcRemoteTree: return RemoteDrivePanel;
  4820. case fcSessionsTabs: return SessionsPageControl;
  4821. default: return NULL;
  4822. }
  4823. }
  4824. //---------------------------------------------------------------------------
  4825. void __fastcall TCustomScpExplorerForm::DirViewColumnRightClick(
  4826. TObject *Sender, TListColumn *Column, TPoint &Point)
  4827. {
  4828. DebugAssert(NonVisualDataModule && Column && Sender);
  4829. NonVisualDataModule->ListColumn = Column;
  4830. TPopupMenu * DirViewColumnMenu;
  4831. if (dynamic_cast<TUnixDirView *>(Sender) != NULL)
  4832. {
  4833. DirViewColumnMenu = NonVisualDataModule->RemoteDirViewColumnPopup;
  4834. NonVisualDataModule->RemoteSortByExtColumnPopupItem->Visible =
  4835. (Column->Index == uvName);
  4836. NonVisualDataModule->RemoteFormatSizeBytesPopupItem->Visible =
  4837. (Column->Index == uvSize);
  4838. }
  4839. else
  4840. {
  4841. DirViewColumnMenu = NonVisualDataModule->LocalDirViewColumnPopup;
  4842. NonVisualDataModule->LocalSortByExtColumnPopupItem->Visible =
  4843. (Column->Index == dvName);
  4844. NonVisualDataModule->LocalFormatSizeBytesPopupItem->Visible =
  4845. (Column->Index == dvSize);
  4846. }
  4847. TCustomListView * ListView = DebugNotNull(dynamic_cast<TCustomListView *>(Sender));
  4848. TPoint ScreenPoint = ListView->ClientToScreen(Point);
  4849. ScreenPoint.x -= GetScrollPos(ListView->Handle, SB_HORZ);
  4850. DirViewColumnMenu->Popup(ScreenPoint.x, ScreenPoint.y);
  4851. }
  4852. //---------------------------------------------------------------------------
  4853. void __fastcall TCustomScpExplorerForm::DirViewExecFile(
  4854. TObject *Sender, TListItem *Item, bool &AllowExec)
  4855. {
  4856. DoDirViewExecFile(Sender, Item, AllowExec);
  4857. }
  4858. //---------------------------------------------------------------------------
  4859. void __fastcall TCustomScpExplorerForm::DoDirViewExecFile(TObject * Sender,
  4860. TListItem * Item, bool & AllowExec)
  4861. {
  4862. DebugAssert(Sender && Item && Configuration);
  4863. DebugAssert(AllowExec);
  4864. TCustomDirView * ADirView = (TCustomDirView *)Sender;
  4865. bool Remote = (ADirView == DirView(osRemote)) && !IsSideLocalBrowser(osRemote);
  4866. bool ResolvedSymlinks = !Remote || Terminal->ResolvingSymlinks;
  4867. TOperationSide Side = (Remote ? osRemote : osLocal); // TODO
  4868. // Anything special is done on double click only (not on "open" indicated by FForceExecution),
  4869. // on files only (not directories)
  4870. // and only when symlinks are resolved (apply to remote panel only)
  4871. if (!ADirView->ItemIsDirectory(Item) &&
  4872. (ResolvedSymlinks || FForceExecution || Terminal->IsEncryptingFiles()))
  4873. {
  4874. if ((WinConfiguration->DoubleClickAction != dcaOpen) &&
  4875. !FForceExecution &&
  4876. (ResolvedSymlinks || Terminal->IsEncryptingFiles()))
  4877. {
  4878. if (WinConfiguration->DoubleClickAction == dcaCopy)
  4879. {
  4880. // To counter the lock in DirViewBusy (called from TCustomDirView.DoExecute)
  4881. UnlockWindow();
  4882. try
  4883. {
  4884. ExecuteFileOperation(
  4885. foCopy, Side, true, !WinConfiguration->CopyOnDoubleClickConfirmation);
  4886. }
  4887. __finally
  4888. {
  4889. LockWindow();
  4890. }
  4891. AllowExec = false;
  4892. }
  4893. else if (WinConfiguration->DoubleClickAction == dcaEdit)
  4894. {
  4895. if (!Remote || !WinConfiguration->DisableOpenEdit)
  4896. {
  4897. ExecuteFile(Side, efDefaultEditor);
  4898. AllowExec = false;
  4899. }
  4900. }
  4901. else
  4902. {
  4903. DebugFail();
  4904. }
  4905. }
  4906. // if we have not done anything special, fall back to default behavior
  4907. if (AllowExec)
  4908. {
  4909. if (Remote && !WinConfiguration->DisableOpenEdit)
  4910. {
  4911. ExecuteFile(osRemote, efShell);
  4912. AllowExec = false;
  4913. }
  4914. }
  4915. }
  4916. }
  4917. //---------------------------------------------------------------------------
  4918. bool __fastcall TCustomScpExplorerForm::GetHasDirView(TOperationSide Side)
  4919. {
  4920. return ((Side == osRemote) || (Side == osCurrent));
  4921. }
  4922. //---------------------------------------------------------------------------
  4923. void __fastcall TCustomScpExplorerForm::CompareDirectories()
  4924. {
  4925. DebugFail();
  4926. }
  4927. //---------------------------------------------------------------------------
  4928. void __fastcall TCustomScpExplorerForm::SynchronizeDirectories()
  4929. {
  4930. DebugFail();
  4931. }
  4932. //---------------------------------------------------------------------------
  4933. bool __fastcall TCustomScpExplorerForm::DoSynchronizeDirectories(
  4934. UnicodeString & LocalDirectory, UnicodeString & RemoteDirectory, int UseDefaults)
  4935. {
  4936. TSynchronizeParamType Params;
  4937. Params.LocalDirectory = LocalDirectory;
  4938. Params.RemoteDirectory = RemoteDirectory;
  4939. int UnusedParams =
  4940. (GUIConfiguration->SynchronizeParams &
  4941. (TTerminal::spPreviewChanges | TTerminal::spTimestamp | TTerminal::spNotByTime | TTerminal::spBySize));
  4942. Params.Params = GUIConfiguration->SynchronizeParams & ~UnusedParams;
  4943. Params.Options = GUIConfiguration->SynchronizeOptions;
  4944. bool SaveSettings = false;
  4945. TSynchronizeController Controller(&DoSynchronize, &DoSynchronizeInvalid,
  4946. &DoSynchronizeTooManyDirectories);
  4947. DebugAssert(FSynchronizeController == NULL);
  4948. FSynchronizeController = &Controller;
  4949. bool Result;
  4950. try
  4951. {
  4952. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  4953. int CopyParamAttrs = Terminal->UsableCopyParamAttrs(0).Upload;
  4954. int Options =
  4955. FLAGMASK(SynchronizeAllowSelectedOnly(), soAllowSelectedOnly);
  4956. DebugAssert(FOnFeedSynchronizeError == NULL);
  4957. Result = DoSynchronizeDialog(Params, &CopyParam, Controller.StartStop,
  4958. SaveSettings, Options, CopyParamAttrs, GetSynchronizeOptions, SynchronizeSessionLog,
  4959. FOnFeedSynchronizeError, SynchronizeInNewWindow, UseDefaults);
  4960. if (Result)
  4961. {
  4962. if (SaveSettings)
  4963. {
  4964. GUIConfiguration->SynchronizeParams = Params.Params | UnusedParams;
  4965. GUIConfiguration->SynchronizeOptions = Params.Options;
  4966. }
  4967. else
  4968. {
  4969. if (FLAGSET(GUIConfiguration->SynchronizeOptions, soSynchronizeAsk) &&
  4970. FLAGCLEAR(Params.Options, soSynchronizeAsk) &&
  4971. FLAGSET(Params.Options, soSynchronize))
  4972. {
  4973. GUIConfiguration->SynchronizeOptions =
  4974. (GUIConfiguration->SynchronizeOptions & ~soSynchronizeAsk) |
  4975. soSynchronize;
  4976. }
  4977. }
  4978. LocalDirectory = Params.LocalDirectory;
  4979. RemoteDirectory = Params.RemoteDirectory;
  4980. }
  4981. }
  4982. __finally
  4983. {
  4984. FSynchronizeController = NULL;
  4985. DebugAssert(FOnFeedSynchronizeError == NULL);
  4986. FOnFeedSynchronizeError = NULL;
  4987. }
  4988. return Result;
  4989. }
  4990. //---------------------------------------------------------------------------
  4991. void __fastcall TCustomScpExplorerForm::DoSynchronize(
  4992. TSynchronizeController * /*Sender*/, const UnicodeString LocalDirectory,
  4993. const UnicodeString RemoteDirectory, const TCopyParamType & CopyParam,
  4994. const TSynchronizeParamType & Params, TSynchronizeChecklist ** Checklist,
  4995. TSynchronizeOptions * Options, bool Full)
  4996. {
  4997. // Keep-up-to-date mode
  4998. if (Terminal->Status == ssOpened)
  4999. {
  5000. try
  5001. {
  5002. int PParams = Params.Params;
  5003. if (!Full)
  5004. {
  5005. PParams |= TTerminal::spNoRecurse | TTerminal::spUseCache | TTerminal::spDelayProgress;
  5006. if (FLAGSET(Params.Options, soRecurse))
  5007. {
  5008. PParams |= TTerminal::spSubDirs;
  5009. }
  5010. }
  5011. else
  5012. {
  5013. // if keepuptodate is non-recursive,
  5014. // full sync before has to be non-recursive as well
  5015. if (FLAGCLEAR(Params.Options, soRecurse))
  5016. {
  5017. PParams |= TTerminal::spNoRecurse;
  5018. }
  5019. }
  5020. Synchronize(LocalDirectory, RemoteDirectory, smRemote, CopyParam,
  5021. PParams, Checklist, Options);
  5022. }
  5023. catch (Exception & E)
  5024. {
  5025. if (FLAGSET(Params.Options, soContinueOnError) && Terminal->Active &&
  5026. DebugAlwaysTrue(FOnFeedSynchronizeError != NULL))
  5027. {
  5028. // noop, already logged in MoreMessageDialog
  5029. }
  5030. else
  5031. {
  5032. ShowExtendedExceptionEx(Terminal, &E);
  5033. throw;
  5034. }
  5035. }
  5036. }
  5037. }
  5038. //---------------------------------------------------------------------------
  5039. void __fastcall TCustomScpExplorerForm::DoSynchronizeInvalid(
  5040. TSynchronizeController * /*Sender*/, const UnicodeString Directory,
  5041. const UnicodeString ErrorStr)
  5042. {
  5043. if (!Directory.IsEmpty())
  5044. {
  5045. SimpleErrorDialog(FMTLOAD(WATCH_ERROR_DIRECTORY, (Directory)), ErrorStr);
  5046. }
  5047. else
  5048. {
  5049. SimpleErrorDialog(LoadStr(WATCH_ERROR_GENERAL), ErrorStr);
  5050. }
  5051. }
  5052. //---------------------------------------------------------------------------
  5053. void __fastcall TCustomScpExplorerForm::DoSynchronizeTooManyDirectories(
  5054. TSynchronizeController * /*Sender*/, int & MaxDirectories)
  5055. {
  5056. if (MaxDirectories < GUIConfiguration->MaxWatchDirectories)
  5057. {
  5058. MaxDirectories = GUIConfiguration->MaxWatchDirectories;
  5059. }
  5060. else
  5061. {
  5062. TMessageParams Params(mpNeverAskAgainCheck);
  5063. unsigned int Result = MessageDialog(
  5064. FMTLOAD(TOO_MANY_WATCH_DIRECTORIES, (MaxDirectories, MaxDirectories)),
  5065. qtConfirmation, qaYes | qaNo, HELP_TOO_MANY_WATCH_DIRECTORIES, &Params);
  5066. if ((Result == qaYes) || (Result == qaNeverAskAgain))
  5067. {
  5068. MaxDirectories *= 2;
  5069. if (Result == qaNeverAskAgain)
  5070. {
  5071. GUIConfiguration->MaxWatchDirectories = MaxDirectories;
  5072. }
  5073. }
  5074. else
  5075. {
  5076. Abort();
  5077. }
  5078. }
  5079. }
  5080. //---------------------------------------------------------------------------
  5081. void __fastcall TCustomScpExplorerForm::Synchronize(const UnicodeString LocalDirectory,
  5082. const UnicodeString RemoteDirectory, TSynchronizeMode Mode,
  5083. const TCopyParamType & CopyParam, int Params, TSynchronizeChecklist ** Checklist,
  5084. TSynchronizeOptions * Options)
  5085. {
  5086. // Keep-up-to-date mode
  5087. DebugAssert(!FAutoOperation);
  5088. void * BatchStorage;
  5089. BatchStart(BatchStorage);
  5090. FAutoOperation = true;
  5091. bool AnyOperation = false;
  5092. TDateTime StartTime = Now();
  5093. TSynchronizeChecklist * AChecklist = NULL;
  5094. try
  5095. {
  5096. FSynchronizeProgressForm = new TSynchronizeProgressForm(Application, true, -1);
  5097. if (FLAGCLEAR(Params, TTerminal::spDelayProgress))
  5098. {
  5099. FSynchronizeProgressForm->Start();
  5100. }
  5101. AChecklist = Terminal->SynchronizeCollect(LocalDirectory, RemoteDirectory,
  5102. static_cast<TTerminal::TSynchronizeMode>(Mode),
  5103. &CopyParam, Params | TTerminal::spNoConfirmation, TerminalSynchronizeDirectory,
  5104. Options);
  5105. SetTaskbarListProgressState(TBPF_NOPROGRESS);
  5106. SAFE_DESTROY(FSynchronizeProgressForm);
  5107. AnyOperation = (AChecklist->CheckedCount > 0);
  5108. if (AnyOperation)
  5109. {
  5110. TSynchronizeProgress SynchronizeProgress(AChecklist);
  5111. CreateProgressForm(&SynchronizeProgress);
  5112. try
  5113. {
  5114. Terminal->SynchronizeApply(
  5115. AChecklist, &CopyParam, Params | TTerminal::spNoConfirmation, TerminalSynchronizeDirectory,
  5116. SynchronizeProcessedItem, NULL, NULL, NULL);
  5117. }
  5118. __finally
  5119. {
  5120. DestroyProgressForm();
  5121. }
  5122. }
  5123. }
  5124. __finally
  5125. {
  5126. if (Checklist == NULL)
  5127. {
  5128. delete AChecklist;
  5129. }
  5130. else
  5131. {
  5132. *Checklist = AChecklist;
  5133. }
  5134. FAutoOperation = false;
  5135. SAFE_DESTROY(FSynchronizeProgressForm);
  5136. BatchEnd(BatchStorage);
  5137. ReloadLocalDirectory();
  5138. if (AnyOperation)
  5139. {
  5140. OperationComplete(StartTime);
  5141. }
  5142. }
  5143. }
  5144. //---------------------------------------------------------------------------
  5145. bool __fastcall TCustomScpExplorerForm::SynchronizeAllowSelectedOnly()
  5146. {
  5147. // can be called from command line
  5148. return Visible &&
  5149. ((DirView(osOther)->SelCount > 0) ||
  5150. (HasDirView[osLocal] && (DirView(osLocal)->SelCount > 0)));
  5151. }
  5152. //---------------------------------------------------------------------------
  5153. void __fastcall TCustomScpExplorerForm::SynchronizeSessionLog(const UnicodeString & Message)
  5154. {
  5155. LogSynchronizeEvent(Terminal, Message);
  5156. }
  5157. //---------------------------------------------------------------------------
  5158. void __fastcall TCustomScpExplorerForm::GetSynchronizeOptions(
  5159. int Params, TSynchronizeOptions & Options)
  5160. {
  5161. DebugAssert(!IsLocalBrowserMode());
  5162. if (FLAGSET(Params, TTerminal::spSelectedOnly) && SynchronizeAllowSelectedOnly())
  5163. {
  5164. Options.Filter = new TStringList();
  5165. Options.Filter->CaseSensitive = false;
  5166. Options.Filter->Duplicates = Types::dupAccept;
  5167. if (DirView(osRemote)->SelCount > 0)
  5168. {
  5169. DirView(osRemote)->CreateFileList(false, false, Options.Filter);
  5170. }
  5171. if (HasDirView[osLocal] && (DirView(osLocal)->SelCount > 0))
  5172. {
  5173. DirView(osLocal)->CreateFileList(false, false, Options.Filter);
  5174. }
  5175. Options.Filter->Sort();
  5176. }
  5177. }
  5178. //---------------------------------------------------------------------------
  5179. UnicodeString __fastcall TCustomScpExplorerForm::SerializeCopyParamForCommandLine(const TCopyParamType * CopyParams)
  5180. {
  5181. TCopyParamType Defaults;
  5182. std::unique_ptr<THierarchicalStorage> ConfigStorage(Configuration->CreateConfigStorage());
  5183. ConfigStorage->AccessMode = smRead;
  5184. if (ConfigStorage->OpenSubKey(Configuration->ConfigurationSubKey, false))
  5185. {
  5186. GUIConfiguration->LoadCopyParam(ConfigStorage.get(), &Defaults);
  5187. }
  5188. ConfigStorage.reset(NULL);
  5189. std::unique_ptr<TStringList> Options(new TStringList());
  5190. std::unique_ptr<TOptionsStorage> OptionsStorage(new TOptionsStorage(Options.get(), true));
  5191. CopyParams->Save(OptionsStorage.get(), &Defaults);
  5192. UnicodeString Result;
  5193. if (Options->Count > 0)
  5194. {
  5195. Result =
  5196. FORMAT(L" %s%s", (TProgramParams::FormatSwitch(RAWTRANSFERSETTINGS_SWITCH), StringsToParams(Options.get())));
  5197. }
  5198. return Result;
  5199. }
  5200. //---------------------------------------------------------------------------
  5201. void __fastcall TCustomScpExplorerForm::SynchronizeInNewWindow(
  5202. const TSynchronizeParamType & Params, const TCopyParamType * CopyParams)
  5203. {
  5204. UnicodeString SessionName = CreateHiddenDuplicateSession();
  5205. UnicodeString AdditionalParams =
  5206. FORMAT(L"%s%s %s // \"%s\" \"%s\" %d %d", (
  5207. TProgramParams::FormatSwitch(DEFAULTS_SWITCH),
  5208. SerializeCopyParamForCommandLine(CopyParams),
  5209. TProgramParams::FormatSwitch(KEEP_UP_TO_DATE_SWITCH),
  5210. Params.LocalDirectory, Params.RemoteDirectory, Params.Params, Params.Options));
  5211. ExecuteNewInstance(SessionName, AdditionalParams);
  5212. }
  5213. //---------------------------------------------------------------------------
  5214. struct TSynchronizeParams
  5215. {
  5216. UnicodeString LocalDirectory;
  5217. UnicodeString RemoteDirectory;
  5218. TSynchronizeMode Mode;
  5219. TSynchronizeChecklist * Checklist;
  5220. int Params;
  5221. TCopyParamType * CopyParam;
  5222. TDateTime CollectElapsed;
  5223. TDateTime * StartTime;
  5224. TProcessedSynchronizationChecklistItem OnProcessedItem;
  5225. };
  5226. //---------------------------------------------------------------------------
  5227. void __fastcall TCustomScpExplorerForm::FullSynchronize(
  5228. TSynchronizeParams & Params, TProcessedSynchronizationChecklistItem OnProcessedItem,
  5229. TUpdatedSynchronizationChecklistItems OnUpdatedSynchronizationChecklistItems)
  5230. {
  5231. DebugAssert(!FAutoOperation);
  5232. void * BatchStorage;
  5233. BatchStart(BatchStorage);
  5234. FAutoOperation = true;
  5235. TFileOperationStatistics Statistics;
  5236. TDateTime Start = Now();
  5237. try
  5238. {
  5239. Params.OnProcessedItem = OnProcessedItem;
  5240. TSynchronizeProgress SynchronizeProgress(Params.Checklist);
  5241. CreateProgressForm(&SynchronizeProgress);
  5242. Terminal->SynchronizeApply(
  5243. Params.Checklist, Params.CopyParam, Params.Params | TTerminal::spNoConfirmation,
  5244. TerminalSynchronizeDirectory, SynchronizeProcessedItem, OnUpdatedSynchronizationChecklistItems, &Params,
  5245. &Statistics);
  5246. }
  5247. __finally
  5248. {
  5249. Params.OnProcessedItem = NULL;
  5250. FAutoOperation = false;
  5251. DestroyProgressForm();
  5252. BatchEnd(BatchStorage);
  5253. ReloadLocalDirectory();
  5254. }
  5255. if (WinConfiguration->SynchronizeSummary)
  5256. {
  5257. UnicodeString Message = MainInstructions(LoadStr(SYNCHRONIZE_COMPLETE)) + L"\n";
  5258. // The statistics should be 0 anyway in this case
  5259. if (FLAGCLEAR(Params.Params, TTerminal::spTimestamp))
  5260. {
  5261. Message += L"\n";
  5262. if (Statistics.FilesUploaded > 0)
  5263. {
  5264. Message += FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 1), (FormatNumber(Statistics.FilesUploaded), FormatBytes(Statistics.TotalUploaded))) + L"\n";
  5265. }
  5266. if (Statistics.FilesDownloaded > 0)
  5267. {
  5268. Message += FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 2), (FormatNumber(Statistics.FilesDownloaded), FormatBytes(Statistics.TotalDownloaded))) + L"\n";
  5269. }
  5270. if (Statistics.FilesDeletedLocal > 0)
  5271. {
  5272. Message += FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 3), (FormatNumber(Statistics.FilesDeletedLocal))) + L"\n";
  5273. }
  5274. if (Statistics.FilesDeletedRemote > 0)
  5275. {
  5276. Message += FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 4), (FormatNumber(Statistics.FilesDeletedRemote))) + L"\n";
  5277. }
  5278. }
  5279. TDateTime Elapsed = (Now() - Start);
  5280. Message +=
  5281. L"\n" +
  5282. FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 5), (FormatDateTimeSpan(Configuration->TimeFormat, Params.CollectElapsed))) + L"\n" +
  5283. FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 6), (FormatDateTimeSpan(Configuration->TimeFormat, Elapsed)));
  5284. TMessageParams Params(mpNeverAskAgainCheck);
  5285. unsigned int Result = MessageDialog(Message, qtInformation, qaOK, HELP_NONE, &Params);
  5286. if (Result == qaNeverAskAgain)
  5287. {
  5288. WinConfiguration->SynchronizeSummary = false;
  5289. }
  5290. }
  5291. }
  5292. //---------------------------------------------------------------------------
  5293. void __fastcall TCustomScpExplorerForm::SynchronizeProcessedItem(void * Token, const TSynchronizeChecklist::TItem * ChecklistItem)
  5294. {
  5295. if (DebugAlwaysTrue(FProgressForm != NULL) && DebugAlwaysTrue(FProgressForm->SynchronizeProgress != NULL))
  5296. {
  5297. FProgressForm->SynchronizeProgress->ItemProcessed(ChecklistItem);
  5298. }
  5299. // Not set in keep-up-to-date mode - Synchronize() method
  5300. if (Token != NULL)
  5301. {
  5302. TSynchronizeParams & Params = *static_cast<TSynchronizeParams *>(Token);
  5303. if (Params.OnProcessedItem != NULL)
  5304. {
  5305. Params.OnProcessedItem(NULL, ChecklistItem);
  5306. }
  5307. }
  5308. }
  5309. //---------------------------------------------------------------------------
  5310. void __fastcall TCustomScpExplorerForm::DoFullSynchronize(
  5311. void * Token, TProcessedSynchronizationChecklistItem OnProcessedItem,
  5312. TUpdatedSynchronizationChecklistItems OnUpdatedSynchronizationChecklistItems)
  5313. {
  5314. TSynchronizeParams & Params = *static_cast<TSynchronizeParams *>(Token);
  5315. *Params.StartTime = Now();
  5316. FullSynchronize(Params, OnProcessedItem, OnUpdatedSynchronizationChecklistItems);
  5317. }
  5318. //---------------------------------------------------------------------------
  5319. void __fastcall TCustomScpExplorerForm::DoSynchronizeChecklistCalculateSize(
  5320. TSynchronizeChecklist * Checklist, const TSynchronizeChecklist::TItemList & Items, void * Token)
  5321. {
  5322. // terminal can be already closed (e.g. dropped connection)
  5323. if (Terminal != NULL)
  5324. {
  5325. TSynchronizeParams & Params = *static_cast<TSynchronizeParams *>(Token);
  5326. Terminal->SynchronizeChecklistCalculateSize(Checklist, Items, Params.CopyParam);
  5327. }
  5328. }
  5329. //---------------------------------------------------------------------------
  5330. void __fastcall TCustomScpExplorerForm::DoSynchronizeMove(
  5331. TOperationSide Side, const UnicodeString & FileName, const UnicodeString & NewFileName, TRemoteFile * RemoteFile)
  5332. {
  5333. DebugAssert(!IsLocalBrowserMode());
  5334. TAutoBatch AutoBatch(this);
  5335. TAutoFlag AutoOperationFlag(FAutoOperation);
  5336. if (Side == osRemote)
  5337. {
  5338. std::unique_ptr<TStrings> FileList(new TStringList());
  5339. FileList->AddObject(FileName, RemoteFile);
  5340. UnicodeString Target = UnixExtractFileDir(NewFileName);
  5341. UnicodeString FileMask = DelimitFileNameMask(UnixExtractFileName(NewFileName));
  5342. RemoteDirView->SaveSelection();
  5343. RemoteDirView->SaveSelectedNames();
  5344. try
  5345. {
  5346. Terminal->MoveFiles(FileList.get(), Target, FileMask);
  5347. }
  5348. catch(...)
  5349. {
  5350. RemoteDirView->DiscardSavedSelection();
  5351. throw;
  5352. }
  5353. RemoteDirView->RestoreSelection();
  5354. }
  5355. else if (DebugAlwaysTrue(Side == osLocal))
  5356. {
  5357. if (!MoveFile(FileName.c_str(), NewFileName.c_str()))
  5358. {
  5359. throw EOSExtException(FMTLOAD(RENAME_FILE_ERROR, (FileName, NewFileName)));
  5360. }
  5361. UnicodeString Directory = ExtractFileDir(FileName);
  5362. ReloadLocalDirectory(Directory);
  5363. UnicodeString NewDirectory = ExtractFileDir(NewFileName);
  5364. if (!SamePaths(Directory, NewDirectory))
  5365. {
  5366. ReloadLocalDirectory(NewDirectory);
  5367. }
  5368. }
  5369. }
  5370. //---------------------------------------------------------------------------
  5371. void __fastcall TCustomScpExplorerForm::DoSynchronizeBrowse(TOperationSide Side, TSynchronizeChecklist::TAction Action, const TSynchronizeChecklist::TItem * Item)
  5372. {
  5373. UnicodeString LocalPath = ExcludeTrailingBackslash(Item->Local.Directory);
  5374. if (Side == osLocal)
  5375. {
  5376. if (Action == TSynchronizeChecklist::saDownloadNew)
  5377. {
  5378. OpenFolderInExplorer(LocalPath);
  5379. }
  5380. else
  5381. {
  5382. OpenFileInExplorer(TPath::Combine(LocalPath, Item->GetFileName()));
  5383. }
  5384. }
  5385. else if (DebugAlwaysTrue(Side == osRemote))
  5386. {
  5387. // Similar to CreateHiddenDuplicateSession, except that it modifies the initial directories
  5388. std::unique_ptr<TSessionData> SessionData(CloneCurrentSessionData());
  5389. SessionData->RemoteDirectory = UnixExcludeTrailingBackslash(Item->Remote.Directory);
  5390. if (!LocalPath.IsEmpty())
  5391. {
  5392. SessionData->LocalDirectory = LocalPath;
  5393. }
  5394. UnicodeString SessionName = SaveHiddenDuplicateSession(SessionData.get());
  5395. ExecuteNewInstance(SessionName, FORMAT(L"%s=%s", (TProgramParams::FormatSwitch(BROWSE_SWITCH), Item->GetFileName())));
  5396. }
  5397. }
  5398. //---------------------------------------------------------------------------
  5399. void __fastcall TCustomScpExplorerForm::FullSynchronizeInNewWindow(
  5400. TSynchronizeMode Mode, int Params, const UnicodeString & LocalDirectory, const UnicodeString & RemoteDirectory,
  5401. const TCopyParamType * CopyParams)
  5402. {
  5403. UnicodeString SessionName = CreateHiddenDuplicateSession();
  5404. UnicodeString AdditionalParams =
  5405. FORMAT(L"%s%s %s // \"%s\" \"%s\" %d %d", (
  5406. TProgramParams::FormatSwitch(DEFAULTS_SWITCH),
  5407. SerializeCopyParamForCommandLine(CopyParams),
  5408. TProgramParams::FormatSwitch(SYNCHRONIZE_SWITCH),
  5409. LocalDirectory, RemoteDirectory, Mode, Params));
  5410. ExecuteNewInstance(SessionName, AdditionalParams);
  5411. }
  5412. //---------------------------------------------------------------------------
  5413. int __fastcall TCustomScpExplorerForm::DoFullSynchronizeDirectories(
  5414. UnicodeString & LocalDirectory, UnicodeString & RemoteDirectory,
  5415. TSynchronizeMode & Mode, int Params, bool & SaveMode, int UseDefaults)
  5416. {
  5417. int Result;
  5418. bool SaveSettings = false;
  5419. int Options =
  5420. FLAGMASK(!Terminal->IsCapable[fcTimestampChanging], fsoDisableTimestamp) |
  5421. FLAGMASK(SynchronizeAllowSelectedOnly(), fsoAllowSelectedOnly);
  5422. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  5423. TUsableCopyParamAttrs CopyParamAttrs = Terminal->UsableCopyParamAttrs(0);
  5424. bool Continue =
  5425. (UseDefaults == 0) ||
  5426. DoFullSynchronizeDialog(Mode, Params, LocalDirectory, RemoteDirectory,
  5427. &CopyParam, SaveSettings, SaveMode, Options, CopyParamAttrs, FullSynchronizeInNewWindow, UseDefaults);
  5428. if (Continue)
  5429. {
  5430. Configuration->Usage->Inc(L"Synchronizations");
  5431. UpdateCopyParamCounters(CopyParam);
  5432. TSynchronizeOptions SynchronizeOptions;
  5433. GetSynchronizeOptions(Params, SynchronizeOptions);
  5434. if (SaveSettings)
  5435. {
  5436. GUIConfiguration->SynchronizeParams = Params;
  5437. }
  5438. else
  5439. {
  5440. SaveMode = false;
  5441. }
  5442. TDateTime StartTime = Now();
  5443. TSynchronizeChecklist * Checklist = NULL;
  5444. try
  5445. {
  5446. DebugAssert(!FAutoOperation);
  5447. FAutoOperation = true;
  5448. try
  5449. {
  5450. UnicodeString SessionKey = Terminal->SessionData->SessionKey;
  5451. std::unique_ptr<TStrings> DataList(Configuration->LoadDirectoryStatisticsCache(SessionKey, RemoteDirectory, CopyParam));
  5452. int Files = -1;
  5453. if (DataList->Count >= 1)
  5454. {
  5455. Files = StrToIntDef(DataList->Strings[0], Files);
  5456. }
  5457. else
  5458. {
  5459. DataList->Add(UnicodeString());
  5460. }
  5461. FSynchronizeProgressForm = new TSynchronizeProgressForm(Application, true, Files);
  5462. FSynchronizeProgressForm->Start();
  5463. Checklist = Terminal->SynchronizeCollect(LocalDirectory, RemoteDirectory,
  5464. static_cast<TTerminal::TSynchronizeMode>(Mode),
  5465. &CopyParam, Params | TTerminal::spNoConfirmation, TerminalSynchronizeDirectory,
  5466. &SynchronizeOptions);
  5467. if (Terminal->SessionData->CacheDirectories)
  5468. {
  5469. DataList->Strings[0] = IntToStr(SynchronizeOptions.Files);
  5470. Configuration->SaveDirectoryStatisticsCache(SessionKey, RemoteDirectory, CopyParam, DataList.get());
  5471. }
  5472. }
  5473. __finally
  5474. {
  5475. FAutoOperation = false;
  5476. SetTaskbarListProgressState(TBPF_NOPROGRESS);
  5477. SAFE_DESTROY(FSynchronizeProgressForm);
  5478. }
  5479. TSynchronizeParams SynchronizeParams;
  5480. SynchronizeParams.LocalDirectory = LocalDirectory;
  5481. SynchronizeParams.RemoteDirectory = RemoteDirectory;
  5482. SynchronizeParams.Mode = Mode;
  5483. SynchronizeParams.CopyParam = &CopyParam;
  5484. SynchronizeParams.Params = Params;
  5485. SynchronizeParams.Checklist = Checklist;
  5486. SynchronizeParams.CollectElapsed = (Now() - StartTime);
  5487. SynchronizeParams.StartTime = &StartTime;
  5488. SynchronizeParams.OnProcessedItem = NULL;
  5489. Result = Checklist->Count;
  5490. if (Checklist->Count > 0)
  5491. {
  5492. if (FLAGSET(Params, TTerminal::spPreviewChanges))
  5493. {
  5494. if (!DoSynchronizeChecklistDialog(
  5495. Checklist, Mode, Params, LocalDirectory, RemoteDirectory, CustomCommandMenu, DoFullSynchronize,
  5496. DoSynchronizeChecklistCalculateSize, DoSynchronizeMove, DoSynchronizeBrowse, &SynchronizeParams))
  5497. {
  5498. Result = -1;
  5499. }
  5500. }
  5501. else
  5502. {
  5503. FullSynchronize(SynchronizeParams, NULL, NULL);
  5504. }
  5505. }
  5506. else
  5507. {
  5508. UnicodeString Message = MainInstructions(LoadStr(COMPARE_NO_DIFFERENCES));
  5509. MessageDialog(Message, qtInformation, qaOK, HELP_SYNCHRONIZE_NO_DIFFERENCES);
  5510. }
  5511. }
  5512. __finally
  5513. {
  5514. delete Checklist;
  5515. }
  5516. OperationComplete(StartTime);
  5517. }
  5518. else
  5519. {
  5520. Result = -1;
  5521. }
  5522. return Result;
  5523. }
  5524. //---------------------------------------------------------------------------
  5525. void __fastcall TCustomScpExplorerForm::TerminalSynchronizeDirectory(
  5526. const UnicodeString & LocalDirectory, const UnicodeString & RemoteDirectory,
  5527. bool & Continue, bool Collect, const TSynchronizeOptions * Options)
  5528. {
  5529. if (Collect)
  5530. {
  5531. DebugAssert(FSynchronizeProgressForm != NULL);
  5532. if (!FSynchronizeProgressForm->Started)
  5533. {
  5534. FSynchronizeProgressForm->Start();
  5535. }
  5536. int CompareProgress = FSynchronizeProgressForm->SetData(LocalDirectory, RemoteDirectory, Options->Files, Continue);
  5537. SetTaskbarListProgressValue(CompareProgress);
  5538. }
  5539. else
  5540. {
  5541. DebugAssert(FSynchronizeProgressForm == NULL);
  5542. DebugAssert(FProgressForm != NULL);
  5543. }
  5544. }
  5545. //---------------------------------------------------------------------------
  5546. void __fastcall TCustomScpExplorerForm::StandaloneEdit(const UnicodeString & FileName)
  5547. {
  5548. UnicodeString FullFileName = AbsolutePath(FTerminal->CurrentDirectory, FileName);
  5549. TRemoteFile * File = NULL;
  5550. Terminal->ReadFile(FullFileName, File);
  5551. if (File != NULL)
  5552. {
  5553. std::unique_ptr<TRemoteFile> FileOwner(File);
  5554. TAutoFlag Flag(FStandaloneEditing);
  5555. ExecuteRemoteFile(FullFileName, File, efInternalEditor);
  5556. Application->ShowMainForm = false;
  5557. Application->Run();
  5558. }
  5559. }
  5560. //---------------------------------------------------------------------------
  5561. void __fastcall TCustomScpExplorerForm::ExploreLocalDirectory()
  5562. {
  5563. DebugFail();
  5564. }
  5565. //---------------------------------------------------------------------------
  5566. TSessionData * __fastcall TCustomScpExplorerForm::CloneCurrentSessionData()
  5567. {
  5568. std::unique_ptr<TSessionData> SessionData(new TSessionData(L""));
  5569. SessionData->Assign(Terminal->SessionData);
  5570. UpdateSessionData(SessionData.get());
  5571. Terminal->UpdateSessionCredentials(SessionData.get());
  5572. if (!Terminal->SessionData->HasSessionName())
  5573. {
  5574. // Particularly for "Workspace/XXXX" name, we need to reset it, as it would become user-visible
  5575. // once IsWorkspace is cleared
  5576. SessionData->Name = UnicodeString();
  5577. SessionData->IsWorkspace = false;
  5578. }
  5579. return SessionData.release();
  5580. }
  5581. //---------------------------------------------------------------------------
  5582. void __fastcall TCustomScpExplorerForm::SaveCurrentSession()
  5583. {
  5584. TSessionData * SessionData = CloneCurrentSessionData();
  5585. try
  5586. {
  5587. TSessionData * EditingSessionData = StoredSessions->FindSame(SessionData);
  5588. DoSaveSession(SessionData, EditingSessionData, true, NULL);
  5589. }
  5590. __finally
  5591. {
  5592. delete SessionData;
  5593. }
  5594. }
  5595. //---------------------------------------------------------------------------
  5596. TObjectList * __fastcall TCustomScpExplorerForm::DoCollectWorkspace()
  5597. {
  5598. TTerminalManager * Manager = TTerminalManager::Instance();
  5599. std::unique_ptr<TObjectList> DataList(new TObjectList());
  5600. if (DebugAlwaysTrue(Terminal != NULL))
  5601. {
  5602. // Update (Managed)Terminal->StateData
  5603. UpdateTerminal(Terminal);
  5604. }
  5605. Manager->SaveWorkspace(DataList.get());
  5606. return DataList.release();
  5607. }
  5608. //---------------------------------------------------------------------------
  5609. void __fastcall TCustomScpExplorerForm::DoSaveWorkspace(
  5610. const UnicodeString & Name, TObjectList * DataList, bool SavePasswords, bool Explicit)
  5611. {
  5612. WinConfiguration->LastWorkspace = Name;
  5613. if (!SavePasswords)
  5614. {
  5615. for (int Index = 0; Index < DataList->Count; Index++)
  5616. {
  5617. TSessionData * SessionData = dynamic_cast<TSessionData *>(DataList->Items[Index]);
  5618. if (SessionData->Link.IsEmpty())
  5619. {
  5620. SessionData->ClearSessionPasswords();
  5621. }
  5622. }
  5623. }
  5624. StoredSessions->NewWorkspace(Name, DataList);
  5625. // modified only
  5626. StoredSessions->Save(false, Explicit);
  5627. }
  5628. //---------------------------------------------------------------------------
  5629. UnicodeString __fastcall TCustomScpExplorerForm::WorkspaceName()
  5630. {
  5631. return
  5632. DefaultStr(WinConfiguration->LastWorkspace,
  5633. DefaultStr(WinConfiguration->AutoWorkspace, LoadStr(NEW_WORKSPACE)));
  5634. }
  5635. //---------------------------------------------------------------------------
  5636. bool __fastcall TCustomScpExplorerForm::SaveWorkspace(bool EnableAutoSave)
  5637. {
  5638. std::unique_ptr<TObjectList> DataList(DoCollectWorkspace());
  5639. bool AnyNonStoredSessionWithPassword = false;
  5640. bool AnyNonStoredNonWorkspaceSessionWithPassword = false;
  5641. bool AllNonStoredSessionsAnonymous = true;
  5642. TTerminalManager * Manager = TTerminalManager::Instance();
  5643. for (int Index = 0; Index < DataList->Count; Index++)
  5644. {
  5645. TSessionData * SessionData = dynamic_cast<TSessionData *>(DataList->Items[Index]);
  5646. if (SessionData->Link.IsEmpty())
  5647. {
  5648. if (SessionData->HasAnySessionPassword())
  5649. {
  5650. AnyNonStoredSessionWithPassword = true;
  5651. if (!Manager->Terminals[Index]->SessionData->IsWorkspace)
  5652. {
  5653. AnyNonStoredNonWorkspaceSessionWithPassword = true;
  5654. }
  5655. }
  5656. if (!SameText(SessionData->UserName, AnonymousUserName))
  5657. {
  5658. AllNonStoredSessionsAnonymous = false;
  5659. }
  5660. }
  5661. }
  5662. bool SavePasswords;
  5663. bool * PSavePasswords;
  5664. bool NotRecommendedSavingPasswords =
  5665. !CustomWinConfiguration->UseMasterPassword &&
  5666. !AllNonStoredSessionsAnonymous;
  5667. if (Configuration->DisablePasswordStoring ||
  5668. !AnyNonStoredSessionWithPassword)
  5669. {
  5670. PSavePasswords = NULL;
  5671. SavePasswords = false;
  5672. }
  5673. else
  5674. {
  5675. PSavePasswords = &SavePasswords;
  5676. SavePasswords =
  5677. !AnyNonStoredNonWorkspaceSessionWithPassword ||
  5678. !NotRecommendedSavingPasswords;
  5679. }
  5680. UnicodeString Name = WorkspaceName();
  5681. bool CreateShortcut = false;
  5682. bool Result =
  5683. DoSaveWorkspaceDialog(
  5684. Name, PSavePasswords, NotRecommendedSavingPasswords, CreateShortcut,
  5685. EnableAutoSave);
  5686. if (Result)
  5687. {
  5688. DoSaveWorkspace(Name, DataList.get(), SavePasswords, true);
  5689. if (CreateShortcut)
  5690. {
  5691. TOperationVisualizer Visualizer;
  5692. UnicodeString AdditionalParams = TProgramParams::FormatSwitch(DESKTOP_SWITCH);
  5693. CreateDesktopSessionShortCut(Name, L"", AdditionalParams, -1, WORKSPACE_ICON);
  5694. }
  5695. if (EnableAutoSave)
  5696. {
  5697. WinConfiguration->AutoSaveWorkspace = true;
  5698. WinConfiguration->AutoWorkspace = Name;
  5699. if (PSavePasswords != NULL)
  5700. {
  5701. WinConfiguration->AutoSaveWorkspacePasswords = SavePasswords;
  5702. }
  5703. }
  5704. }
  5705. return Result;
  5706. }
  5707. //---------------------------------------------------------------------------
  5708. void __fastcall TCustomScpExplorerForm::UpdateTerminal(TManagedTerminal * Terminal)
  5709. {
  5710. DebugAssert(!IsLocalBrowserMode());
  5711. SAFE_DESTROY(Terminal->RemoteExplorerState);
  5712. if (WinConfiguration->PreservePanelState)
  5713. {
  5714. Terminal->RemoteExplorerState = RemoteDirView->SaveState();
  5715. }
  5716. UpdateSessionData(Terminal->StateData);
  5717. }
  5718. //---------------------------------------------------------------------------
  5719. void __fastcall TCustomScpExplorerForm::UpdateSessionData(TSessionData * Data)
  5720. {
  5721. // Keep in sync with TSessionData::CopyStateData
  5722. DebugAssert(!IsLocalBrowserMode());
  5723. DebugAssert(Data != NULL);
  5724. // This is inconsistent with how color (for example) is handled.
  5725. Data->Name = Terminal->SessionData->Name;
  5726. // cannot use RemoteDirView->Path, because it is empty if connection
  5727. // was already closed
  5728. // also only peek, we may not be connected at all atm
  5729. // so make sure we do not try retrieving current directory from the server
  5730. // (particularly with FTP)
  5731. UnicodeString ACurrentDirectory = Terminal->PeekCurrentDirectory();
  5732. if (!ACurrentDirectory.IsEmpty())
  5733. {
  5734. Data->RemoteDirectory = ACurrentDirectory;
  5735. }
  5736. Data->Color = SessionColor;
  5737. }
  5738. //---------------------------------------------------------------------------
  5739. void __fastcall TCustomScpExplorerForm::ToolBarResize(TObject *Sender)
  5740. {
  5741. TTBXToolbar * Toolbar = dynamic_cast<TTBXToolbar*>(Sender);
  5742. DebugAssert(Toolbar != NULL);
  5743. for (int i = 0; i < Toolbar->Items->Count; i++)
  5744. {
  5745. TTBXCustomDropDownItem * DropDownItem;
  5746. DropDownItem = dynamic_cast<TTBXCustomDropDownItem *>(Toolbar->Items->Items[i]);
  5747. if (DropDownItem != NULL)
  5748. {
  5749. ToolbarItemResize(DropDownItem,
  5750. Toolbar->Width - (Toolbar->View->BaseSize.x - DropDownItem->EditWidth) -
  5751. Toolbar->NonClientWidth);
  5752. break;
  5753. }
  5754. }
  5755. }
  5756. //---------------------------------------------------------------------------
  5757. void __fastcall TCustomScpExplorerForm::ToolbarItemResize(TTBXCustomDropDownItem * Item, int Width)
  5758. {
  5759. Item->EditWidth = Width;
  5760. }
  5761. //---------------------------------------------------------------------------
  5762. void __fastcall TCustomScpExplorerForm::ToolbarGetBaseSize(
  5763. TTBCustomToolbar * Toolbar, TPoint & ASize)
  5764. {
  5765. for (int i = 0; i < Toolbar->Items->Count; i++)
  5766. {
  5767. TTBXCustomDropDownItem * DropDownItem;
  5768. DropDownItem = dynamic_cast<TTBXCustomDropDownItem *>(Toolbar->Items->Items[i]);
  5769. if (DropDownItem != NULL)
  5770. {
  5771. ASize.x -= DropDownItem->EditWidth;
  5772. ASize.x += ScaleByTextHeight(this, 50) /* minimal combo width */;
  5773. }
  5774. }
  5775. }
  5776. //---------------------------------------------------------------------------
  5777. void __fastcall TCustomScpExplorerForm::DoWarnLackOfTempSpace(
  5778. const UnicodeString Path, __int64 RequiredSpace, bool & Continue)
  5779. {
  5780. if (WinConfiguration->DDWarnLackOfTempSpace)
  5781. {
  5782. UnicodeString ADrive = ExtractFileDrive(ExpandFileName(Path));
  5783. if (!ADrive.IsEmpty())
  5784. {
  5785. __int64 FreeSpace = DiskFree((Byte)(ADrive[1]-'A'+1));
  5786. DebugAssert(RequiredSpace >= 0);
  5787. __int64 RequiredWithReserve;
  5788. RequiredWithReserve = (__int64)(RequiredSpace * WinConfiguration->DDWarnLackOfTempSpaceRatio);
  5789. if (FreeSpace < RequiredWithReserve)
  5790. {
  5791. unsigned int Result;
  5792. TMessageParams Params(mpNeverAskAgainCheck);
  5793. Result = MessageDialog(FMTLOAD(DD_WARN_LACK_OF_TEMP_SPACE, (Path,
  5794. FormatBytes(FreeSpace), FormatBytes(RequiredSpace))),
  5795. qtWarning, qaYes | qaNo, HELP_DD_WARN_LACK_OF_TEMP_SPACE, &Params);
  5796. if (Result == qaNeverAskAgain)
  5797. {
  5798. WinConfiguration->DDWarnLackOfTempSpace = false;
  5799. }
  5800. Continue = (Result == qaYes || Result == qaNeverAskAgain);
  5801. }
  5802. }
  5803. }
  5804. }
  5805. //---------------------------------------------------------------------------
  5806. void __fastcall TCustomScpExplorerForm::AddBookmark(TOperationSide Side)
  5807. {
  5808. DoOpenDirectoryDialog(odAddBookmark, Side);
  5809. }
  5810. //---------------------------------------------------------------------------
  5811. TStrings * __fastcall TCustomScpExplorerForm::CreateVisitedDirectories(TOperationSide Side)
  5812. {
  5813. // we should better use TCustomDirView::FCaseSensitive, but it is private
  5814. TStringList * VisitedDirectories = CreateSortedStringList(!IsSideLocalBrowser(Side));
  5815. try
  5816. {
  5817. TCustomDirView * DView = DirView(Side);
  5818. for (int Index = -DView->BackCount; Index <= DView->ForwardCount; Index++)
  5819. {
  5820. VisitedDirectories->Add(DView->HistoryPath[Index]);
  5821. }
  5822. }
  5823. catch (...)
  5824. {
  5825. delete VisitedDirectories;
  5826. throw;
  5827. }
  5828. return VisitedDirectories;
  5829. }
  5830. //---------------------------------------------------------------------------
  5831. void __fastcall TCustomScpExplorerForm::DoOpenDirectoryDialog(
  5832. TOpenDirectoryMode Mode, TOperationSide Side)
  5833. {
  5834. bool Continue = true;
  5835. if (Mode == odAddBookmark)
  5836. {
  5837. TMessageParams Params(mpNeverAskAgainCheck);
  5838. Params.NeverAskAgainTitle = LoadStr(ADD_BOOKMARK_SHARED);
  5839. Params.NeverAskAgainCheckedInitially = WinConfiguration->UseSharedBookmarks;
  5840. UnicodeString Message = MainInstructions(FMTLOAD(ADD_BOOKMARK_CONFIRM, (DirView(Side)->PathName)));
  5841. unsigned int Answer =
  5842. MessageDialog(Message,
  5843. qtConfirmation, qaOK | qaCancel, HELP_ADD_BOOKMARK_CONFIRM, &Params);
  5844. if (Answer == qaNeverAskAgain)
  5845. {
  5846. Continue = true;
  5847. WinConfiguration->UseSharedBookmarks = true;
  5848. }
  5849. else if (Answer == qaOK)
  5850. {
  5851. Continue = true;
  5852. WinConfiguration->UseSharedBookmarks = false;
  5853. }
  5854. else
  5855. {
  5856. Continue = false;
  5857. }
  5858. }
  5859. if (Continue)
  5860. {
  5861. TStrings * VisitedDirectories = CreateVisitedDirectories(Side);
  5862. try
  5863. {
  5864. UnicodeString Name = DirView(Side)->PathName;
  5865. bool Repeat;
  5866. do
  5867. {
  5868. Repeat = false;
  5869. if (::DoOpenDirectoryDialog(Mode, Side, Name, VisitedDirectories, Terminal,
  5870. // do not allow switching to location profiles,
  5871. // if we are not connected
  5872. HasDirView[osLocal] && (Terminal != NULL)))
  5873. {
  5874. TWindowLock Lock(this);
  5875. if (!TryOpenDirectory(Side, Name))
  5876. {
  5877. Repeat = true;
  5878. Mode = odBrowse;
  5879. }
  5880. }
  5881. }
  5882. while (Repeat);
  5883. }
  5884. __finally
  5885. {
  5886. delete VisitedDirectories;
  5887. }
  5888. }
  5889. }
  5890. //---------------------------------------------------------------------------
  5891. bool __fastcall TCustomScpExplorerForm::CommandSessionFallback()
  5892. {
  5893. DebugAssert(!FTerminal->CommandSessionOpened);
  5894. return TTerminalManager::Instance()->ConnectTerminal(FTerminal->CommandSession);
  5895. }
  5896. //---------------------------------------------------------------------------
  5897. bool __fastcall TCustomScpExplorerForm::EnsureCommandSessionFallback(TFSCapability Capability)
  5898. {
  5899. bool Result = FTerminal->IsCapable[Capability] ||
  5900. FTerminal->CommandSessionOpened;
  5901. if (!Result)
  5902. {
  5903. DebugAssert(FTerminal->IsCapable[fcSecondaryShell]);
  5904. if (!GUIConfiguration->ConfirmCommandSession)
  5905. {
  5906. Result = true;
  5907. }
  5908. else
  5909. {
  5910. TMessageParams Params(mpNeverAskAgainCheck);
  5911. const TFileSystemInfo & FileSystemInfo = Terminal->GetFileSystemInfo();
  5912. unsigned int Answer = MessageDialog(FMTLOAD(PERFORM_ON_COMMAND_SESSION2,
  5913. (FileSystemInfo.ProtocolName, FileSystemInfo.ProtocolName)), qtConfirmation,
  5914. qaOK | qaCancel, HELP_PERFORM_ON_COMMAND_SESSION, &Params);
  5915. if (Answer == qaNeverAskAgain)
  5916. {
  5917. GUIConfiguration->ConfirmCommandSession = false;
  5918. Result = true;
  5919. }
  5920. else if (Answer == qaOK)
  5921. {
  5922. Result = true;
  5923. }
  5924. }
  5925. if (Result)
  5926. {
  5927. Result = CommandSessionFallback();
  5928. }
  5929. }
  5930. return Result;
  5931. }
  5932. //---------------------------------------------------------------------------
  5933. void __fastcall TCustomScpExplorerForm::OpenConsole(UnicodeString Command)
  5934. {
  5935. if (EnsureCommandSessionFallback(fcAnyCommand))
  5936. {
  5937. DoConsoleDialog(Terminal, Command);
  5938. }
  5939. }
  5940. //---------------------------------------------------------------------------
  5941. void __fastcall TCustomScpExplorerForm::FileControlDDDragEnter(
  5942. TObject *Sender, _di_IDataObject /*DataObj*/, int /*grfKeyState*/,
  5943. const TPoint & /*Point*/, int & /*dwEffect*/, bool & Accept)
  5944. {
  5945. if (!IsSideLocalBrowser(osRemote) &&
  5946. IsFileControl(DropSourceControl, osRemote) &&
  5947. (FDDExtMapFile != NULL))
  5948. {
  5949. Accept = true;
  5950. }
  5951. FDDTargetControl = dynamic_cast<TControl*>(Sender);
  5952. DebugAssert(FDDTargetControl != NULL);
  5953. }
  5954. //---------------------------------------------------------------------------
  5955. void __fastcall TCustomScpExplorerForm::SessionsDDDragEnter(
  5956. _di_IDataObject DataObj, int KeyState,
  5957. const TPoint & Point, int & Effect, bool & Accept)
  5958. {
  5959. FileControlDDDragEnter(SessionsPageControl, DataObj, KeyState, Point, Effect, Accept);
  5960. }
  5961. //---------------------------------------------------------------------------
  5962. void __fastcall TCustomScpExplorerForm::QueueDDDragEnter(
  5963. _di_IDataObject DataObj, int KeyState,
  5964. const TPoint & Point, int & Effect, bool & Accept)
  5965. {
  5966. FileControlDDDragEnter(QueueView3, DataObj, KeyState, Point, Effect, Accept);
  5967. }
  5968. //---------------------------------------------------------------------------
  5969. void __fastcall TCustomScpExplorerForm::FileControlDDDragLeave(
  5970. TObject * Sender)
  5971. {
  5972. DebugUsedParam(Sender);
  5973. DebugAssert(FDDTargetControl == Sender);
  5974. FDDTargetControl = NULL;
  5975. }
  5976. //---------------------------------------------------------------------------
  5977. void __fastcall TCustomScpExplorerForm::SessionsDDDragLeave()
  5978. {
  5979. FileControlDDDragLeave(SessionsPageControl);
  5980. }
  5981. //---------------------------------------------------------------------------
  5982. void __fastcall TCustomScpExplorerForm::QueueDDDragLeave()
  5983. {
  5984. FileControlDDDragLeave(QueueView3);
  5985. }
  5986. //---------------------------------------------------------------------------
  5987. void __fastcall TCustomScpExplorerForm::AddEditLink(TOperationSide Side, bool Add)
  5988. {
  5989. DebugAssert(!IsSideLocalBrowser(Side));
  5990. DebugUsedParam(Side);
  5991. bool Edit = false;
  5992. TRemoteFile * File = NULL;
  5993. UnicodeString FileName;
  5994. UnicodeString PointTo;
  5995. bool SymbolicLink = true;
  5996. if (RemoteDirView->ItemFocused)
  5997. {
  5998. DebugAssert(RemoteDirView->ItemFocused->Data);
  5999. File = (TRemoteFile *)RemoteDirView->ItemFocused->Data;
  6000. Edit = !Add && File->IsSymLink && Terminal->SessionData->ResolveSymlinks;
  6001. if (Edit)
  6002. {
  6003. FileName = File->FileName;
  6004. PointTo = File->LinkTo;
  6005. }
  6006. else
  6007. {
  6008. PointTo = File->FileName;
  6009. }
  6010. }
  6011. if (DoSymlinkDialog(FileName, PointTo, osRemote, SymbolicLink, Edit,
  6012. Terminal->IsCapable[fcHardLink]))
  6013. {
  6014. Configuration->Usage->Inc(L"RemoteLinksCreated");
  6015. if (Edit)
  6016. {
  6017. DebugAssert(File->FileName == FileName);
  6018. int Params = dfNoRecursive;
  6019. Terminal->ExceptionOnFail = true;
  6020. try
  6021. {
  6022. Terminal->DeleteFile(L"", File, &Params);
  6023. }
  6024. __finally
  6025. {
  6026. Terminal->ExceptionOnFail = false;
  6027. }
  6028. }
  6029. Terminal->CreateLink(FileName, PointTo, SymbolicLink);
  6030. }
  6031. }
  6032. //---------------------------------------------------------------------------
  6033. bool __fastcall TCustomScpExplorerForm::CanAddEditLink(TOperationSide Side)
  6034. {
  6035. return
  6036. (IsSideLocalBrowser(Side) ||
  6037. ((Terminal != NULL) &&
  6038. Terminal->Active &&
  6039. Terminal->ResolvingSymlinks &&
  6040. Terminal->IsCapable[fcSymbolicLink]));
  6041. }
  6042. //---------------------------------------------------------------------------
  6043. bool __fastcall TCustomScpExplorerForm::LinkFocused()
  6044. {
  6045. return
  6046. !IsSideLocalBrowser(FCurrentSide) &&
  6047. (RemoteDirView->ItemFocused != NULL) &&
  6048. ((TRemoteFile *)RemoteDirView->ItemFocused->Data)->IsSymLink &&
  6049. Terminal->SessionData->ResolveSymlinks;
  6050. }
  6051. //---------------------------------------------------------------------------
  6052. void __fastcall TCustomScpExplorerForm::ExecuteCurrentFile()
  6053. {
  6054. DebugAssert(!WinConfiguration->DisableOpenEdit);
  6055. FForceExecution = true;
  6056. try
  6057. {
  6058. DirView(osCurrent)->ExecuteCurrentFile();
  6059. }
  6060. __finally
  6061. {
  6062. FForceExecution = false;
  6063. }
  6064. }
  6065. //---------------------------------------------------------------------------
  6066. void __fastcall TCustomScpExplorerForm::ExecuteCurrentFileWith(bool OnFocused)
  6067. {
  6068. TEditorData ExternalEditor;
  6069. ExternalEditor.Editor = edExternal;
  6070. bool Remember = false;
  6071. if (DoEditorPreferencesDialog(&ExternalEditor, Remember, epmAdHoc, !IsSideLocalBrowser(osCurrent)))
  6072. {
  6073. if (Remember)
  6074. {
  6075. TEditorList * EditorList = new TEditorList;
  6076. try
  6077. {
  6078. *EditorList = *WinConfiguration->EditorList;
  6079. bool Found = false;
  6080. int i = 0;
  6081. while (!Found && (i < EditorList->Count))
  6082. {
  6083. const TEditorPreferences * Editor = EditorList->Editors[i];
  6084. if ((Editor->Data->Editor == edExternal) &&
  6085. (Editor->Data->ExternalEditor == ExternalEditor.ExternalEditor))
  6086. {
  6087. Found = true;
  6088. }
  6089. i++;
  6090. }
  6091. if (!Found)
  6092. {
  6093. EditorList->Add(new TEditorPreferences(ExternalEditor));
  6094. WinConfiguration->EditorList = EditorList;
  6095. }
  6096. }
  6097. __finally
  6098. {
  6099. delete EditorList;
  6100. }
  6101. }
  6102. ExecuteFile(osCurrent, efExternalEditor, &ExternalEditor, true, OnFocused);
  6103. }
  6104. }
  6105. //---------------------------------------------------------------------------
  6106. void __fastcall TCustomScpExplorerForm::DetachTerminal(TObject * ATerminal)
  6107. {
  6108. FEditorManager->ProcessFiles(FileTerminalRemoved, ATerminal);
  6109. if (FFileFindTerminal == ATerminal)
  6110. {
  6111. FFileFindTerminal = NULL;
  6112. HideFileFindDialog();
  6113. }
  6114. if (FClipboardTerminal == ATerminal)
  6115. {
  6116. ClipboardClear(); // implies ClipboardStop
  6117. }
  6118. }
  6119. //---------------------------------------------------------------------------
  6120. void __fastcall TCustomScpExplorerForm::TerminalRemoved(TObject * Sender)
  6121. {
  6122. DetachTerminal(Sender);
  6123. }
  6124. //---------------------------------------------------------------------------
  6125. void __fastcall TCustomScpExplorerForm::FileTerminalRemoved(const UnicodeString FileName,
  6126. TEditedFileData * Data, TObject * /*Token*/, void * Arg)
  6127. {
  6128. TTerminal * Terminal = static_cast<TTerminal *>(Arg);
  6129. DebugAssert(Terminal != NULL);
  6130. if (Data->Terminal == Terminal)
  6131. {
  6132. Data->Terminal = NULL;
  6133. }
  6134. }
  6135. //---------------------------------------------------------------------------
  6136. void __fastcall TCustomScpExplorerForm::LastTerminalClosed(TObject * /*Sender*/)
  6137. {
  6138. UpdateControls();
  6139. SessionColor = TColor(0);
  6140. UpdateRemotePathComboBox(false);
  6141. NeedSession(false);
  6142. }
  6143. //---------------------------------------------------------------------------
  6144. void __fastcall TCustomScpExplorerForm::NeedSession(bool Startup)
  6145. {
  6146. try
  6147. {
  6148. // Cache, as the login dialog can change its value
  6149. bool ShowLogin = WinConfiguration->ShowLoginWhenNoSession;
  6150. try
  6151. {
  6152. if (ShowLogin)
  6153. {
  6154. bool ReloadSessions = !Startup;
  6155. TTerminalManager::Instance()->NewSession(false, L"", ReloadSessions, this);
  6156. }
  6157. else if (Startup && WinConfiguration->AutoSaveWorkspace && !WinConfiguration->AutoWorkspace.IsEmpty() &&
  6158. // This detects if workspace was saved the last time the main widow was closed
  6159. SameText(WinConfiguration->LastStoredSession, WinConfiguration->AutoWorkspace))
  6160. {
  6161. DoOpenFolderOrWorkspace(WinConfiguration->AutoWorkspace, false);
  6162. Configuration->Usage->Inc(L"OpenedWorkspacesAuto");
  6163. }
  6164. }
  6165. __finally
  6166. {
  6167. // Do not terminate, if we are only starting up and we are not showing Login dialog
  6168. // (so there was no chance for the user to open any session yet)
  6169. if ((ShowLogin || !Startup) &&
  6170. !WinConfiguration->KeepOpenWhenNoSession &&
  6171. ((Terminal == NULL) || (!Terminal->Active && !Terminal->Permanent)))
  6172. {
  6173. TerminateApplication();
  6174. }
  6175. }
  6176. }
  6177. catch (EAbort &)
  6178. {
  6179. // swallow
  6180. // The TTerminalManager does not expect the OnLastTerminalClose to throw without trying to connect.
  6181. // Also when called from TWinControl.UpdateShowing => CMShowingChanged, the showing is aborted on exception
  6182. }
  6183. }
  6184. //---------------------------------------------------------------------------
  6185. void __fastcall TCustomScpExplorerForm::DoTerminalListChanged()
  6186. {
  6187. TStrings * TerminalList = TTerminalManager::Instance()->TerminalList;
  6188. int ActiveTerminalIndex = TTerminalManager::Instance()->ActiveTerminalIndex;
  6189. Configuration->Usage->SetMax(L"MaxOpenedSessions", TerminalList->Count);
  6190. SendMessage(SessionsPageControl->Handle, WM_SETREDRAW, 0, 0);
  6191. try
  6192. {
  6193. while ((SessionsPageControl->PageCount > TerminalList->Count + 1) ||
  6194. // Clear the design time unthemed tab
  6195. ((SessionsPageControl->PageCount > 0) &&
  6196. (dynamic_cast<TThemeTabSheet *>(SessionsPageControl->Pages[SessionsPageControl->PageCount - 1]) == NULL)))
  6197. {
  6198. delete SessionsPageControl->Pages[SessionsPageControl->PageCount - 1];
  6199. }
  6200. for (int Index = 0; Index <= TerminalList->Count; Index++)
  6201. {
  6202. TThemeTabSheet * TabSheet;
  6203. if (Index >= SessionsPageControl->PageCount)
  6204. {
  6205. TabSheet = new TThemeTabSheet(SessionsPageControl);
  6206. TabSheet->PageControl = SessionsPageControl;
  6207. }
  6208. else
  6209. {
  6210. TabSheet = DebugNotNull(dynamic_cast<TThemeTabSheet *>(SessionsPageControl->Pages[Index]));
  6211. }
  6212. bool IsSessionTab = (Index < TerminalList->Count);
  6213. if (IsSessionTab)
  6214. {
  6215. TTerminal * Terminal = dynamic_cast<TTerminal *>(TerminalList->Objects[Index]);
  6216. TabSheet->Tag = reinterpret_cast<int>(Terminal);
  6217. UpdateSessionTab(TabSheet);
  6218. }
  6219. else
  6220. {
  6221. TabSheet->ImageIndex = FNewSessionTabImageIndex;
  6222. TabSheet->Tag = 0; // not really needed
  6223. TabSheet->Shadowed = false;
  6224. // 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)
  6225. UpdateNewSessionTab();
  6226. }
  6227. TabSheet->ShowCloseButton = IsSessionTab;
  6228. }
  6229. }
  6230. __finally
  6231. {
  6232. SendMessage(SessionsPageControl->Handle, WM_SETREDRAW, 1, 0);
  6233. }
  6234. SessionsPageControl->ActivePageIndex = ActiveTerminalIndex;
  6235. }
  6236. //---------------------------------------------------------------------------
  6237. void __fastcall TCustomScpExplorerForm::TerminalListChanged(TObject * /*Sender*/)
  6238. {
  6239. DoTerminalListChanged();
  6240. }
  6241. //---------------------------------------------------------------------------
  6242. void __fastcall TCustomScpExplorerForm::UpdateNewSessionTab()
  6243. {
  6244. TTabSheet * TabSheet = SessionsPageControl->Pages[SessionsPageControl->PageCount - 1];
  6245. DebugAssert(TabSheet->ImageIndex == 0);
  6246. TabSheet->Caption =
  6247. WinConfiguration->SelectiveToolbarText ?
  6248. StripHotkey(StripTrailingPunctuation(NonVisualDataModule->NewSessionAction->Caption)) :
  6249. UnicodeString();
  6250. }
  6251. //---------------------------------------------------------------------------
  6252. TManagedTerminal * __fastcall TCustomScpExplorerForm::GetSessionTabTerminal(TTabSheet * TabSheet)
  6253. {
  6254. return reinterpret_cast<TManagedTerminal *>(TabSheet->Tag);
  6255. }
  6256. //---------------------------------------------------------------------------
  6257. void __fastcall TCustomScpExplorerForm::UpdateSessionTab(TTabSheet * TabSheet)
  6258. {
  6259. if (DebugAlwaysTrue(TabSheet != NULL))
  6260. {
  6261. TManagedTerminal * ATerminal = GetSessionTabTerminal(TabSheet);
  6262. if (DebugAlwaysTrue(ATerminal != NULL))
  6263. {
  6264. TColor Color = (ATerminal == FTerminal) ? FSessionColor : ATerminal->StateData->Color;
  6265. TabSheet->ImageIndex = AddSessionColor(Color);
  6266. UnicodeString TabCaption = TTerminalManager::Instance()->GetTerminalTitle(ATerminal, true);
  6267. TabSheet->Caption = SessionsPageControl->FormatCaptionWithCloseButton(TabCaption);
  6268. TThemeTabSheet * ThemeTabSheet = dynamic_cast<TThemeTabSheet *>(TabSheet);
  6269. if (DebugAlwaysTrue(ThemeTabSheet != NULL))
  6270. {
  6271. ThemeTabSheet->Shadowed = !ATerminal->Active;
  6272. }
  6273. }
  6274. }
  6275. }
  6276. //---------------------------------------------------------------------------
  6277. bool __fastcall TCustomScpExplorerForm::SessionTabSwitched()
  6278. {
  6279. DebugAssert(SessionsPageControl->ActivePage != NULL);
  6280. TManagedTerminal * Terminal = GetSessionTabTerminal(SessionsPageControl->ActivePage);
  6281. bool Result = (Terminal != NULL);
  6282. if (Result)
  6283. {
  6284. TTerminalManager::Instance()->ActiveTerminal = Terminal;
  6285. }
  6286. else
  6287. {
  6288. try
  6289. {
  6290. NewSession(false);
  6291. }
  6292. __finally
  6293. {
  6294. DoTerminalListChanged();
  6295. }
  6296. FSessionsPageControlNewSessionTime = Now();
  6297. }
  6298. return Result;
  6299. }
  6300. //---------------------------------------------------------------------------
  6301. void __fastcall TCustomScpExplorerForm::SessionsPageControlChange(TObject * /*Sender*/)
  6302. {
  6303. SessionTabSwitched();
  6304. }
  6305. //---------------------------------------------------------------------------
  6306. void __fastcall TCustomScpExplorerForm::TransferListChange(TObject * Sender)
  6307. {
  6308. TTBXStringList * TransferList = dynamic_cast<TTBXStringList *>(Sender);
  6309. DebugAssert(TransferList != NULL);
  6310. UnicodeString Name;
  6311. if (TransferList->ItemIndex <= 0)
  6312. {
  6313. Name = L"";
  6314. }
  6315. else
  6316. {
  6317. Name = GUIConfiguration->CopyParamList->Names[TransferList->ItemIndex - 1];
  6318. }
  6319. if (FCopyParamAutoSelected.IsEmpty())
  6320. {
  6321. // if previous preset was not autoselected, make new preset the "default"
  6322. FCopyParamDefault = Name;
  6323. }
  6324. GUIConfiguration->CopyParamCurrent = Name;
  6325. }
  6326. //---------------------------------------------------------------------------
  6327. void __fastcall TCustomScpExplorerForm::UpdateTransferLabel()
  6328. {
  6329. // sanity check
  6330. bool ExistingPreset =
  6331. (FTransferListHoverIndex >= 0) &&
  6332. (FTransferListHoverIndex < 1 + GUIConfiguration->CopyParamList->Count);
  6333. DebugAssert(ExistingPreset);
  6334. if (ExistingPreset)
  6335. {
  6336. HDC DC = GetDC(0);
  6337. TCanvas * Canvas = new TCanvas();
  6338. try
  6339. {
  6340. Canvas->Handle = DC;
  6341. Canvas->Font = GetToolbarFont(this);
  6342. UnicodeString Name;
  6343. if (FTransferListHoverIndex == 0)
  6344. {
  6345. Name = L"";
  6346. }
  6347. else
  6348. {
  6349. Name = GUIConfiguration->CopyParamList->Names[FTransferListHoverIndex - 1];
  6350. }
  6351. TTBXLabelItem * TransferLabel = dynamic_cast<TTBXLabelItem*>(
  6352. static_cast<TComponent*>(GetComponent(fcTransferLabel)));
  6353. TTBXStringList * TransferList = dynamic_cast<TTBXStringList*>(
  6354. static_cast<TObject*>(GetComponent(fcTransferList)));
  6355. UnicodeString InfoStr =
  6356. GUIConfiguration->CopyParamPreset[Name].
  6357. GetInfoStr(L"; ",
  6358. FLAGMASK(((Terminal != NULL) && Terminal->Active), Terminal->UsableCopyParamAttrs(0).General));
  6359. int MaxWidth = TransferList->MinWidth - (2 * TransferLabel->Margin) - ScaleByTextHeight(this, 10);
  6360. if (Canvas->TextExtent(InfoStr).cx > MaxWidth)
  6361. {
  6362. while (Canvas->TextExtent(InfoStr + Ellipsis).cx > MaxWidth)
  6363. {
  6364. InfoStr.SetLength(InfoStr.Length() - 1);
  6365. }
  6366. InfoStr += Ellipsis;
  6367. }
  6368. // UpdateCaption does not cause invalidation of whole submenu, while
  6369. // setting Caption property does.
  6370. // also it probably does not resize the label, even if necessary
  6371. // (we do not want that anyway)
  6372. TransferLabel->UpdateCaption(InfoStr);
  6373. }
  6374. __finally
  6375. {
  6376. Canvas->Handle = NULL;
  6377. ReleaseDC(0, DC);
  6378. delete Canvas;
  6379. }
  6380. }
  6381. }
  6382. //---------------------------------------------------------------------------
  6383. void __fastcall TCustomScpExplorerForm::TransferListDrawItem(
  6384. TTBXCustomList * /*Sender*/, TCanvas * /*ACanvas*/, const TRect & /*ARect*/,
  6385. int /*AIndex*/, int AHoverIndex, bool & /*DrawDefault*/)
  6386. {
  6387. if (FTransferListHoverIndex != AHoverIndex)
  6388. {
  6389. FTransferListHoverIndex = AHoverIndex;
  6390. UpdateTransferLabel();
  6391. }
  6392. }
  6393. //---------------------------------------------------------------------------
  6394. void __fastcall TCustomScpExplorerForm::WMAppCommand(TMessage & Message)
  6395. {
  6396. int Command = GET_APPCOMMAND_LPARAM(Message.LParam);
  6397. TShiftState Shift = KeyDataToShiftState(GET_KEYSTATE_LPARAM(Message.LParam));
  6398. if ((Shift * (TShiftState() << ssShift << ssAlt << ssCtrl)).Empty())
  6399. {
  6400. if (Command == APPCOMMAND_BROWSER_FAVORITES)
  6401. {
  6402. if (!NonVisualDataModule->Busy)
  6403. {
  6404. OpenDirectory(GetSide(osCurrent));
  6405. }
  6406. Message.Result = 1;
  6407. }
  6408. else
  6409. {
  6410. TForm::Dispatch(&Message);
  6411. }
  6412. }
  6413. else
  6414. {
  6415. TForm::Dispatch(&Message);
  6416. }
  6417. }
  6418. //---------------------------------------------------------------------------
  6419. void __fastcall TCustomScpExplorerForm::CMDialogChar(TMessage & AMessage)
  6420. {
  6421. TCMDialogChar & Message = reinterpret_cast<TCMDialogChar &>(AMessage);
  6422. if ((FIgnoreNextDialogChar != 0) &&
  6423. (toupper(Message.CharCode) == toupper(FIgnoreNextDialogChar)))
  6424. {
  6425. Message.Result = 1;
  6426. }
  6427. else
  6428. {
  6429. TForm::Dispatch(&Message);
  6430. }
  6431. FIgnoreNextDialogChar = 0;
  6432. }
  6433. //---------------------------------------------------------------------------
  6434. void __fastcall TCustomScpExplorerForm::WMSysCommand(TMessage & Message)
  6435. {
  6436. // The four low-order bits are used internally by Windows
  6437. unsigned int Cmd = (Message.WParam & 0xFFF0);
  6438. // SC_RESTORE, SC_MAXIMIZE, SC_MINIMIZE - buttons on windows title
  6439. // SC_DEFAULT - double click on windows title (does not work, at least on WinXP)
  6440. // 61730 - restore thru double click - undocumented
  6441. // 61490 - maximize thru double click - undocumented
  6442. if ((Cmd == SC_RESTORE) || (Cmd == SC_MAXIMIZE) ||
  6443. (Cmd == SC_MINIMIZE) || (Cmd == SC_DEFAULT))
  6444. {
  6445. SysResizing(Cmd);
  6446. }
  6447. TForm::Dispatch(&Message);
  6448. }
  6449. //---------------------------------------------------------------------------
  6450. void __fastcall TCustomScpExplorerForm::WMQueryEndSession(TMessage & Message)
  6451. {
  6452. // We were actually never able to make ENDSESSION_CRITICAL happen.
  6453. // Also there no point returning TRUE as we are not able to
  6454. // handle the abrupt termination caused by subsequent WM_ENDSESSION cleanly.
  6455. // Hence the process termination might be safer :)
  6456. if ((Message.LParam != ENDSESSION_CRITICAL) &&
  6457. (((FQueue != NULL) && !FQueue->IsEmpty) || (FProgressForm != NULL)))
  6458. {
  6459. Message.Result = FALSE;
  6460. }
  6461. else
  6462. {
  6463. Message.Result = TRUE;
  6464. }
  6465. // Do not call default handling as that triggers OnCloseQuery,
  6466. // where our implementation will popup confirmation dialogs, what we do not want,
  6467. // as per Vista guidelines:
  6468. // https://docs.microsoft.com/en-us/windows/win32/shutdown/shutting-down
  6469. }
  6470. //---------------------------------------------------------------------------
  6471. void __fastcall TCustomScpExplorerForm::WMEndSession(TWMEndSession & Message)
  6472. {
  6473. if (Message.EndSession && IsApplicationMinimized())
  6474. {
  6475. // WORKAROUND
  6476. // TApplication.WndProc() calls Application.Terminate() before Halt(),
  6477. // when it receives WM_ENDSESSION,
  6478. // but that sometimes (particularly when application is minimized) cause crashes.
  6479. // Crash popups message that blocks log off.
  6480. // Obviously application cannot shutdown cleanly after WM_ENDSESSION,
  6481. // so we call ExitProcess() immediately, not even trying to cleanup.
  6482. // It still causes beep, so there's likely some popup, but it does not block
  6483. // log off.
  6484. ExitProcess(0);
  6485. }
  6486. TForm::Dispatch(&Message);
  6487. }
  6488. #ifdef _DEBUG
  6489. //---------------------------------------------------------------------------
  6490. void __fastcall TCustomScpExplorerForm::WMWindowPosChanged(TWMWindowPosMsg & Message)
  6491. {
  6492. TForm::Dispatch(&Message);
  6493. }
  6494. #endif
  6495. //---------------------------------------------------------------------------
  6496. void __fastcall TCustomScpExplorerForm::SysResizing(unsigned int /*Cmd*/)
  6497. {
  6498. }
  6499. //---------------------------------------------------------------------------
  6500. void __fastcall TCustomScpExplorerForm::DoShow()
  6501. {
  6502. // only now are the controls resized finally, so the size constraints
  6503. // will not conflict with possibly very small window size
  6504. RestoreFormParams();
  6505. // This indicates that the form was rescaled after parameters were loaded and
  6506. // as most probably the parameters were actually saved with the current form scaling
  6507. // (windows was the last time likely closed on the same monitor, where it is starting now),
  6508. // rescaling happened twice already. Reload the parameters, as now they likely won't need any scaling
  6509. // and no rounding problems will occur.
  6510. // See also RestoreForm.
  6511. // (we should be safe to call RestoreParams unconditionally)
  6512. if (PixelsPerInch != Screen->PixelsPerInch)
  6513. {
  6514. RestoreParams();
  6515. }
  6516. FixControlsPlacement();
  6517. if (Position == poDefaultPosOnly)
  6518. {
  6519. CutFormToDesktop(this);
  6520. }
  6521. TForm::DoShow();
  6522. FSessionsDragDropFilesEx->DragDropControl = SessionsPageControl;
  6523. FQueueDragDropFilesEx->DragDropControl = QueueView3;
  6524. if (Terminal == NULL)
  6525. {
  6526. StartingDisconnected();
  6527. }
  6528. UpdatePixelsPerInchMainWindowCounter();
  6529. FShowing = true;
  6530. }
  6531. //---------------------------------------------------------------------------
  6532. void __fastcall TCustomScpExplorerForm::UpdatePixelsPerInchMainWindowCounter()
  6533. {
  6534. Configuration->Usage->Set(L"PixelsPerInchMainWindow", PixelsPerInch);
  6535. }
  6536. //---------------------------------------------------------------------------
  6537. void __fastcall TCustomScpExplorerForm::StartingDisconnected()
  6538. {
  6539. DoTerminalListChanged();
  6540. }
  6541. //---------------------------------------------------------------------------
  6542. void __fastcall TCustomScpExplorerForm::PopupTrayBalloon(TTerminal * Terminal,
  6543. const UnicodeString & Str, TQueryType Type, Exception * E, unsigned int Seconds,
  6544. TNotifyEvent OnBalloonClick, TObject * UserData)
  6545. {
  6546. bool Do;
  6547. UnicodeString Message;
  6548. if (E == NULL)
  6549. {
  6550. Message = Str;
  6551. Do = true;
  6552. }
  6553. else
  6554. {
  6555. Do = ExceptionMessage(E, Message);
  6556. }
  6557. if (Do && WinConfiguration->BalloonNotifications)
  6558. {
  6559. UnicodeString Title;
  6560. if ((Terminal == NULL) && (Type == qtInformation) && ExtractMainInstructions(Message, Title))
  6561. {
  6562. Message = TrimLeft(Message);
  6563. }
  6564. else
  6565. {
  6566. Message = UnformatMessage(Message);
  6567. const ResourceString * Captions[] = { &_SMsgDlgConfirm, &_SMsgDlgWarning,
  6568. &_SMsgDlgError, &_SMsgDlgInformation, NULL };
  6569. Title = LoadResourceString(Captions[Type]);
  6570. if (Terminal != NULL)
  6571. {
  6572. Title = FORMAT(L"%s - %s",
  6573. (TTerminalManager::Instance()->GetTerminalTitle(Terminal, true), Title));
  6574. }
  6575. }
  6576. if (Seconds == 0)
  6577. {
  6578. Seconds = WinConfiguration->NotificationsTimeout;
  6579. }
  6580. FTrayIcon->PopupBalloon(Title, Message, Type, Seconds * MSecsPerSec, OnBalloonClick, UserData);
  6581. }
  6582. }
  6583. //---------------------------------------------------------------------------
  6584. unsigned int __fastcall TCustomScpExplorerForm::MoreMessageDialog(const UnicodeString Message,
  6585. TStrings * MoreMessages, TQueryType Type, unsigned int Answers,
  6586. UnicodeString HelpKeyword, const TMessageParams * Params,
  6587. TTerminal * Terminal)
  6588. {
  6589. if (((WinConfiguration->ContinueOnError && (FErrorList != NULL)) ||
  6590. (FOnFeedSynchronizeError != NULL)) &&
  6591. (Params != NULL) && (Params->Params & mpAllowContinueOnError) )
  6592. {
  6593. if (FOnFeedSynchronizeError != NULL)
  6594. {
  6595. FOnFeedSynchronizeError(Message, MoreMessages, Type, HelpKeyword);
  6596. }
  6597. else
  6598. {
  6599. DebugAssert(FErrorList != NULL);
  6600. TStringList * MoreMessagesCopy = NULL;
  6601. if (MoreMessages)
  6602. {
  6603. MoreMessagesCopy = new TStringList();
  6604. MoreMessagesCopy->Assign(MoreMessages);
  6605. }
  6606. FErrorList->AddObject(Message, MoreMessagesCopy);
  6607. }
  6608. PopupTrayBalloon(Terminal, Message, Type);
  6609. return ContinueAnswer(Answers);
  6610. }
  6611. else
  6612. {
  6613. bool UseBalloon = FTrayIcon->Visible;
  6614. if (UseBalloon)
  6615. {
  6616. PopupTrayBalloon(Terminal, Message, Type);
  6617. }
  6618. unsigned int Result;
  6619. try
  6620. {
  6621. Result = ::MoreMessageDialog(Message, MoreMessages, Type, Answers, HelpKeyword, Params);
  6622. }
  6623. __finally
  6624. {
  6625. // cancel only balloon we popped up, otherwise we may cancel notification
  6626. // balloon that was there before the message dialog
  6627. // (such as "dd confirmation opt in balloon)
  6628. if (UseBalloon)
  6629. {
  6630. FTrayIcon->CancelBalloon();
  6631. }
  6632. }
  6633. return Result;
  6634. }
  6635. }
  6636. //---------------------------------------------------------------------------
  6637. void __fastcall TCustomScpExplorerForm::ShowExtendedException(
  6638. TTerminal * Terminal, Exception * E)
  6639. {
  6640. if (FTrayIcon->Visible)
  6641. {
  6642. PopupTrayBalloon(Terminal, L"", qtError, E);
  6643. }
  6644. // particularly prevent opening new session from jump list,
  6645. // while exception is shown
  6646. NonVisualDataModule->StartBusy();
  6647. try
  6648. {
  6649. ShowExtendedExceptionEx(Terminal, E);
  6650. }
  6651. __finally
  6652. {
  6653. NonVisualDataModule->EndBusy();
  6654. FTrayIcon->CancelBalloon();
  6655. }
  6656. }
  6657. //---------------------------------------------------------------------------
  6658. void __fastcall TCustomScpExplorerForm::TerminalReady()
  6659. {
  6660. if (Terminal->Active)
  6661. {
  6662. InitStatusBar();
  6663. }
  6664. // cannot rely on active page being page for active terminal,
  6665. // as it can happen that active page is the "new session" page
  6666. // (e.g. when reconnecting active terminal, while login dialog
  6667. // invoked from "new session" page is modal)
  6668. int ActiveTerminalIndex = TTerminalManager::Instance()->ActiveTerminalIndex;
  6669. UpdateSessionTab(SessionsPageControl->Pages[ActiveTerminalIndex]);
  6670. }
  6671. //---------------------------------------------------------------------------
  6672. void __fastcall TCustomScpExplorerForm::InactiveTerminalException(
  6673. TTerminal * Terminal, Exception * E)
  6674. {
  6675. Notify(Terminal, L"", qtError, false, NULL, NULL, E);
  6676. if (!Terminal->Active)
  6677. {
  6678. int Index = TTerminalManager::Instance()->IndexOf(Terminal);
  6679. if (DebugAlwaysTrue((Index >= 0) && (Index < SessionsPageControl->PageCount)))
  6680. {
  6681. UpdateSessionTab(SessionsPageControl->Pages[Index]);
  6682. }
  6683. }
  6684. }
  6685. //---------------------------------------------------------------------------
  6686. void __fastcall TCustomScpExplorerForm::Notify(TTerminal * Terminal,
  6687. UnicodeString Message, TQueryType Type,
  6688. bool Important, TNotifyEvent OnClick, TObject * UserData, Exception * E)
  6689. {
  6690. if ((E == NULL) ||
  6691. ExceptionMessage(E, Message))
  6692. {
  6693. unsigned int Seconds = WinConfiguration->NotificationsTimeout;
  6694. if (Important)
  6695. {
  6696. Seconds *= 5;
  6697. }
  6698. UnicodeString NoteMessage(UnformatMessage(Message));
  6699. if (Terminal != NULL)
  6700. {
  6701. NoteMessage = FORMAT(L"%s: %s",
  6702. (TTerminalManager::Instance()->GetTerminalTitle(Terminal, true), NoteMessage));
  6703. }
  6704. if (WinConfiguration->BalloonNotifications)
  6705. {
  6706. AddNote(NoteMessage);
  6707. PopupTrayBalloon(Terminal, Message, Type, NULL, Seconds, OnClick, UserData);
  6708. }
  6709. else
  6710. {
  6711. FlashOnBackground();
  6712. PostNote(NoteMessage, Seconds, OnClick, UserData);
  6713. }
  6714. }
  6715. }
  6716. //---------------------------------------------------------------------------
  6717. void __fastcall TCustomScpExplorerForm::QueueEmptyNoteClicked(TObject * Sender)
  6718. {
  6719. RestoreApp();
  6720. TTerminalNoteData * TerminalNoteData = dynamic_cast<TTerminalNoteData *>(Sender);
  6721. if (DebugAlwaysTrue(TerminalNoteData != NULL) &&
  6722. !NonVisualDataModule->Busy)
  6723. {
  6724. TManagedTerminal * Terminal = TerminalNoteData->Terminal;
  6725. TTerminalManager::Instance()->ActiveTerminal = Terminal;
  6726. if (!ComponentVisible[fcQueueView])
  6727. {
  6728. ToggleQueueVisibility();
  6729. GoToQueue();
  6730. }
  6731. }
  6732. }
  6733. //---------------------------------------------------------------------------
  6734. void __fastcall TCustomScpExplorerForm::QueueEvent(TManagedTerminal * ATerminal,
  6735. TTerminalQueue * /*Queue*/, TQueueEvent Event)
  6736. {
  6737. UnicodeString Message;
  6738. TNotifyEvent OnClick = NULL;
  6739. TObject * UserData = NULL;
  6740. bool QueueInvisible = !ComponentVisible[fcQueueView] || IsApplicationMinimized();
  6741. switch (Event)
  6742. {
  6743. case qeEmptyButMonitored:
  6744. if ((ATerminal != Terminal) || QueueInvisible)
  6745. {
  6746. Message = LoadStr(BALLOON_QUEUE_EMPTY);
  6747. OnClick = QueueEmptyNoteClicked;
  6748. TTerminalNoteData * TerminalNoteData = new TTerminalNoteData();
  6749. TerminalNoteData->Terminal = ATerminal;
  6750. UserData = TerminalNoteData;
  6751. }
  6752. break;
  6753. case qeEmpty:
  6754. OperationComplete(ATerminal->QueueOperationStart);
  6755. break;
  6756. case qePendingUserAction:
  6757. if ((ATerminal != Terminal) ||
  6758. (QueueInvisible && !IsQueueAutoPopup()))
  6759. {
  6760. Message = LoadStr(BALLOON_QUEUE_USER_ACTION);
  6761. }
  6762. break;
  6763. default:
  6764. DebugFail();
  6765. }
  6766. if (!Message.IsEmpty())
  6767. {
  6768. Notify(ATerminal, Message, qtInformation, false, OnClick, UserData);
  6769. }
  6770. }
  6771. //---------------------------------------------------------------------------
  6772. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDCreateDragFileList(
  6773. TObject * /*Sender*/, TFileList * FileList, bool & Created)
  6774. {
  6775. if (FDDExtMapFile != NULL)
  6776. {
  6777. CloseHandle(FDDExtMapFile);
  6778. FDDExtMapFile = NULL;
  6779. }
  6780. if (WinConfiguration->DDFakeFile)
  6781. {
  6782. DDFakeFileInitDrag(FileList, Created);
  6783. }
  6784. }
  6785. //---------------------------------------------------------------------------
  6786. void __fastcall TCustomScpExplorerForm::DDFakeCreated(TObject * /*Sender*/, const UnicodeString FileName)
  6787. {
  6788. if (DebugAlwaysTrue(!FDragFakeDirectory.IsEmpty()) &&
  6789. SameText(ExtractFileName(FileName), ExtractFileName(FDragFakeDirectory)))
  6790. {
  6791. FFakeFileDropTarget = FileName;
  6792. }
  6793. }
  6794. //---------------------------------------------------------------------------
  6795. UnicodeString __fastcall TCustomScpExplorerForm::CreateFakeTransferDirectory()
  6796. {
  6797. UnicodeString Result = ExcludeTrailingBackslash(WinConfiguration->TemporaryDir());
  6798. if (!ForceDirectories(ApiPath(Result)))
  6799. {
  6800. throw Exception(FMTLOAD(CREATE_TEMP_DIR_ERROR, (Result)));
  6801. }
  6802. FileSetAttr(ApiPath(Result), faHidden);
  6803. return Result;
  6804. }
  6805. //---------------------------------------------------------------------------
  6806. void __fastcall TCustomScpExplorerForm::DDFakeFileInitDrag(TFileList * FileList,
  6807. bool & Created)
  6808. {
  6809. FFakeFileDropTarget = UnicodeString();
  6810. FDragFakeDirectory = CreateFakeTransferDirectory();
  6811. FileList->AddItem(NULL, FDragFakeDirectory);
  6812. Created = true;
  6813. if (!WinConfiguration->IsDDExtRunning() || WinConfiguration->IsDDExtBroken())
  6814. {
  6815. FDragFakeMonitors = StartCreationDirectoryMonitorsOnEachDrive(FILE_NOTIFY_CHANGE_DIR_NAME, DDFakeCreated);
  6816. }
  6817. FDDExtMapFile = CreateFileMappingA((HANDLE)0xFFFFFFFF, NULL, PAGE_READWRITE,
  6818. 0, sizeof(TDragExtCommStruct), AnsiString(DRAG_EXT_MAPPING).c_str());
  6819. {
  6820. TMutexGuard Guard(FDDExtMutex, DRAGEXT_MUTEX_RELEASE_TIMEOUT);
  6821. TDragExtCommStruct* CommStruct;
  6822. CommStruct = static_cast<TDragExtCommStruct*>(MapViewOfFile(FDDExtMapFile,
  6823. FILE_MAP_ALL_ACCESS, 0, 0, 0));
  6824. DebugAssert(CommStruct != NULL);
  6825. CommStruct->Version = TDragExtCommStruct::CurrentVersion;
  6826. CommStruct->Dragging = true;
  6827. wcsncpy(CommStruct->DropDest, FDragFakeDirectory.c_str(),
  6828. LENOF(CommStruct->DropDest));
  6829. NULL_TERMINATE(CommStruct->DropDest);
  6830. UnmapViewOfFile(CommStruct);
  6831. }
  6832. }
  6833. //---------------------------------------------------------------------------
  6834. bool __fastcall TCustomScpExplorerForm::RemoteFileControlFileOperation(
  6835. TObject * Sender, TFileOperation Operation, bool NoConfirmation, void * Param)
  6836. {
  6837. bool Result;
  6838. DebugAssert(!IsLocalBrowserMode());
  6839. if (Sender == RemoteDirView)
  6840. {
  6841. Result = ExecuteFileOperation(Operation, osRemote, true, NoConfirmation, Param);
  6842. }
  6843. else
  6844. {
  6845. DebugAssert(Sender == RemoteDriveView);
  6846. TStrings * FileList = RemoteDriveView->DragFileList();
  6847. try
  6848. {
  6849. Result = ExecuteFileOperation(Operation, osRemote, FileList, NoConfirmation, Param);
  6850. }
  6851. __finally
  6852. {
  6853. delete FileList;
  6854. }
  6855. }
  6856. if (FDDTargetControl == RemoteDriveView)
  6857. {
  6858. RemoteDriveView->UpdateDropTarget();
  6859. }
  6860. // foRemoteMove happens when file/dir is dragged within the remote tree view
  6861. // or from tree view to dir view.
  6862. // foMove happens when file/dir is dragged from remote tree view outside of
  6863. // application via dragex
  6864. if (((Operation == foRemoteMove) || (Operation == foMove)) &&
  6865. (DropSourceControl == RemoteDriveView))
  6866. {
  6867. RemoteDriveView->UpdateDropSource();
  6868. }
  6869. return Result;
  6870. }
  6871. //---------------------------------------------------------------------------
  6872. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDEnd(TObject * Sender)
  6873. {
  6874. DebugAssert(!IsLocalBrowserMode());
  6875. // This also handles drops of remote files to queue.
  6876. // Drops of local files (uploads) are handled in QueueDDProcessDropped.
  6877. SAFE_DESTROY(FDDFileList);
  6878. if (!FFakeFileDropTarget.IsEmpty())
  6879. {
  6880. RemoveDir(ApiPath(FFakeFileDropTarget));
  6881. }
  6882. if ((FDDExtMapFile != NULL) || (FDDTargetControl == QueueView3))
  6883. {
  6884. try
  6885. {
  6886. TDragResult DDResult = (Sender == RemoteDirView) ?
  6887. RemoteDirView->LastDDResult : RemoteDriveView->LastDDResult;
  6888. // Focus is moved to the target application,
  6889. // but as we are going to present the UI, we need to steal the focus back.
  6890. // This most likely won't work though (windows does not allow application
  6891. // to steal focus most of the time)
  6892. Application->BringToFront();
  6893. // On older version of Windows we never got drMove here, see also comment below.
  6894. // On Windows 10, we get the "move".
  6895. if ((DDResult == drCopy) || (DDResult == drMove) || (DDResult == drInvalid))
  6896. {
  6897. UnicodeString TargetDirectory;
  6898. TFileOperation Operation;
  6899. // drInvalid may mean drMove, see comment below
  6900. switch (DDResult)
  6901. {
  6902. case drCopy:
  6903. Operation = foCopy;
  6904. break;
  6905. case drMove:
  6906. Operation = foMove;
  6907. break;
  6908. default:
  6909. DebugFail();
  6910. case drInvalid:
  6911. // prefer "copy" for safety
  6912. Operation = FLAGSET(FLastDropEffect, DROPEFFECT_MOVE) ? foMove : foCopy;
  6913. break;
  6914. }
  6915. TTransferOperationParam Param;
  6916. UnicodeString CounterName;
  6917. bool ForceQueue;
  6918. if (!DDGetTarget(Param.TargetDirectory, ForceQueue, CounterName))
  6919. {
  6920. // we get drInvalid both if d&d was intercepted by ddext,
  6921. // and when users drops on no-drop location.
  6922. // we tell the difference by existence of response from ddext,
  6923. // so we ignore absence of response in this case
  6924. if (DDResult != drInvalid)
  6925. {
  6926. // here we know that the extension is installed,
  6927. // as it is checked as soon as drag&drop starts from
  6928. // RemoteFileControlDDCreateDragFileList
  6929. Configuration->Usage->Inc(L"DownloadsDragDropExternalExtTargetUnknown");
  6930. if (!WinConfiguration->DDExtInstalled || WinConfiguration->IsDDExtBroken())
  6931. {
  6932. throw ExtException(NULL, LoadStr(DD_TARGET_UNKNOWN), HELP_DD_TARGET_UNKNOWN);
  6933. }
  6934. else
  6935. {
  6936. throw ExtException(NULL, LoadStr(DRAGEXT_TARGET_UNKNOWN2), HELP_DRAGEXT_TARGET_UNKNOWN);
  6937. }
  6938. }
  6939. }
  6940. else
  6941. {
  6942. // download using fake file
  6943. Param.Temp = false;
  6944. Param.DragDrop = true;
  6945. if (ForceQueue)
  6946. {
  6947. Param.Queue = asOn;
  6948. }
  6949. if (Sender == RemoteDirView)
  6950. {
  6951. Param.Options = FLAGMASK(SelectedAllFilesInDirView(RemoteDirView), coAllFiles);
  6952. }
  6953. if (RemoteFileControlFileOperation(Sender, Operation,
  6954. (WinConfiguration->DDTransferConfirmation == asOff), &Param))
  6955. {
  6956. Configuration->Usage->Inc(CounterName);
  6957. }
  6958. }
  6959. }
  6960. }
  6961. __finally
  6962. {
  6963. CloseHandle(FDDExtMapFile);
  6964. FDDExtMapFile = NULL;
  6965. delete FDragFakeMonitors;
  6966. FDragFakeMonitors = NULL;
  6967. RemoveDir(ApiPath(FDragFakeDirectory));
  6968. FDragFakeDirectory = L"";
  6969. FFakeFileDropTarget = UnicodeString();
  6970. }
  6971. }
  6972. if (FDragDropSshTerminate.get() != NULL)
  6973. {
  6974. FDragDropSshTerminate->Rethrow();
  6975. }
  6976. }
  6977. //---------------------------------------------------------------------------
  6978. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDGiveFeedback(
  6979. TObject * /*Sender*/, int dwEffect, HRESULT & /*Result*/)
  6980. {
  6981. // Remember drop effect so we know (when user drops files), if we copy or move
  6982. FLastDropEffect = dwEffect;
  6983. }
  6984. //---------------------------------------------------------------------------
  6985. bool __fastcall TCustomScpExplorerForm::DDGetTarget(
  6986. UnicodeString & Directory, bool & ForceQueue, UnicodeString & CounterName)
  6987. {
  6988. bool Result;
  6989. if (FDDTargetControl == QueueView3)
  6990. {
  6991. Directory = DefaultDownloadTargetDirectory();
  6992. Result = true;
  6993. CounterName = L"DownloadsDragDropQueue";
  6994. ForceQueue = true;
  6995. }
  6996. else if (!FFakeFileDropTarget.IsEmpty())
  6997. {
  6998. Directory = ExcludeTrailingBackslash(ExtractFilePath(FFakeFileDropTarget));
  6999. Result = true;
  7000. ForceQueue = false;
  7001. CounterName = L"DownloadsDragDropFakeFile";
  7002. }
  7003. else
  7004. {
  7005. ForceQueue = false;
  7006. Enabled = false;
  7007. try
  7008. {
  7009. int Timer = 0;
  7010. Result = false;
  7011. while (!Result && (Timer < WinConfiguration->DDExtTimeout))
  7012. {
  7013. {
  7014. TMutexGuard Guard(FDDExtMutex, DRAGEXT_MUTEX_RELEASE_TIMEOUT);
  7015. TDragExtCommStruct* CommStruct;
  7016. CommStruct = static_cast<TDragExtCommStruct*>(MapViewOfFile(FDDExtMapFile,
  7017. FILE_MAP_ALL_ACCESS, 0, 0, 0));
  7018. DebugAssert(CommStruct != NULL);
  7019. Result = !CommStruct->Dragging;
  7020. if (Result)
  7021. {
  7022. Directory = ExtractFilePath(CommStruct->DropDest);
  7023. CounterName = L"DownloadsDragDropExternalExt";
  7024. }
  7025. UnmapViewOfFile(CommStruct);
  7026. }
  7027. if (!Result)
  7028. {
  7029. Sleep(50);
  7030. Timer += 50;
  7031. Application->ProcessMessages();
  7032. }
  7033. }
  7034. }
  7035. __finally
  7036. {
  7037. Enabled = true;
  7038. }
  7039. }
  7040. return Result;
  7041. }
  7042. //---------------------------------------------------------------------------
  7043. void __fastcall TCustomScpExplorerForm::AddDelayedDirectoryDeletion(
  7044. const UnicodeString TempDir, int SecDelay)
  7045. {
  7046. TDateTime Alarm = IncSecond(Now(), SecDelay);
  7047. FDelayedDeletionList->AddObject(TempDir, reinterpret_cast<TObject*>(Alarm.FileDate()));
  7048. if (FDelayedDeletionTimer == NULL)
  7049. {
  7050. DebugAssert(HandleAllocated());
  7051. FDelayedDeletionTimer = new TTimer(this);
  7052. FDelayedDeletionTimer->Interval = 10000;
  7053. FDelayedDeletionTimer->OnTimer = DoDelayedDeletion;
  7054. }
  7055. else
  7056. {
  7057. FDelayedDeletionTimer->Enabled = true;
  7058. }
  7059. }
  7060. //---------------------------------------------------------------------------
  7061. void __fastcall TCustomScpExplorerForm::DoDelayedDeletion(TObject * Sender)
  7062. {
  7063. DebugAssert(FDelayedDeletionList != NULL);
  7064. TDateTime N = Now();
  7065. TDateTime Alert;
  7066. UnicodeString Directory;
  7067. for (int Index = FDelayedDeletionList->Count-1; Index >= 0; Index--)
  7068. {
  7069. Alert = FileDateToDateTime(reinterpret_cast<int>(FDelayedDeletionList->Objects[Index]));
  7070. if ((N >= Alert) || (Sender == NULL))
  7071. {
  7072. Directory = FDelayedDeletionList->Strings[Index];
  7073. if (DeleteDirectory(ExcludeTrailingBackslash(Directory)))
  7074. {
  7075. FDelayedDeletionList->Delete(Index);
  7076. }
  7077. }
  7078. }
  7079. if (FDelayedDeletionList->Count == 0)
  7080. {
  7081. FDelayedDeletionTimer->Enabled = false;
  7082. }
  7083. }
  7084. //---------------------------------------------------------------------------
  7085. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDTargetDrop()
  7086. {
  7087. DebugAssert(!IsLocalBrowserMode());
  7088. if (IsFileControl(FDDTargetControl, osRemote) ||
  7089. (FDDTargetControl == SessionsPageControl))
  7090. {
  7091. TTerminal * TargetTerminal = NULL;
  7092. TFileOperation Operation = foNone;
  7093. if (FDDTargetControl == SessionsPageControl)
  7094. {
  7095. TPoint Point = SessionsPageControl->ScreenToClient(Mouse->CursorPos);
  7096. int Index = SessionsPageControl->IndexOfTabAt(Point.X, Point.Y);
  7097. // do not allow dropping on the "+" tab
  7098. TargetTerminal = GetSessionTabTerminal(SessionsPageControl->Pages[Index]);
  7099. if (TargetTerminal != NULL)
  7100. {
  7101. if ((FLastDropEffect == DROPEFFECT_MOVE) &&
  7102. (TargetTerminal == TTerminalManager::Instance()->ActiveTerminal))
  7103. {
  7104. Operation = foRemoteMove;
  7105. }
  7106. else
  7107. {
  7108. Operation = foRemoteCopy;
  7109. }
  7110. }
  7111. }
  7112. else
  7113. {
  7114. if (FLastDropEffect == DROPEFFECT_MOVE)
  7115. {
  7116. Operation = foRemoteMove;
  7117. }
  7118. else if (FLastDropEffect == DROPEFFECT_COPY)
  7119. {
  7120. Operation = foRemoteCopy;
  7121. }
  7122. }
  7123. if (Operation != foNone)
  7124. {
  7125. RemoteFileControlFileOperation(DropSourceControl,
  7126. Operation, (WinConfiguration->DDTransferConfirmation == asOff), TargetTerminal);
  7127. }
  7128. // abort drag&drop
  7129. Abort();
  7130. }
  7131. else if ((FDDExtMapFile == NULL) && (FLastDropEffect != DROPEFFECT_NONE) &&
  7132. // Drops of remote files to queue are handled in RemoteFileControlDDEnd
  7133. (FDDTargetControl != QueueView3))
  7134. {
  7135. DebugAssert(!FDragTempDir.IsEmpty());
  7136. TTransferType Type;
  7137. UnicodeString TempDir = FDragTempDir;
  7138. // We clear FDragTempDir before calling
  7139. // just in case it fail (raises exception)
  7140. FDragTempDir = L"";
  7141. Type = (FLastDropEffect & DROPEFFECT_MOVE ? ttMove : Type = ttCopy);
  7142. TGUICopyParamType CopyParams = GUIConfiguration->CurrentCopyParam;
  7143. // empty directory parameter means temp directory -> don't display it!
  7144. UnicodeString TargetDir = L"";
  7145. int Options = 0;
  7146. if (!CopyParamDialog(tdToLocal, Type, true, FDDFileList,
  7147. TargetDir, CopyParams, (WinConfiguration->DDTransferConfirmation != asOff), true, Options))
  7148. {
  7149. Abort();
  7150. }
  7151. // TargetDir is set when dropped on local file control
  7152. // (this was workaround for legacy dirview event handling, now it should be
  7153. // made prettier)
  7154. if (TargetDir.IsEmpty())
  7155. {
  7156. TargetDir = TempDir;
  7157. if (ForceDirectories(ApiPath(TargetDir)))
  7158. {
  7159. DebugAssert(Terminal && !TargetDir.IsEmpty());
  7160. FPendingTempSpaceWarn = true;
  7161. try
  7162. {
  7163. FDragDropOperation = true;
  7164. int Params = cpTemporary |
  7165. (Type == ttMove ? cpDelete : 0);
  7166. DDDownload(FDDFileList, TargetDir, &CopyParams, Params);
  7167. Configuration->Usage->Inc(L"DownloadsDragDropExternalTemp");
  7168. }
  7169. __finally
  7170. {
  7171. FDragDropOperation = false;
  7172. FPendingTempSpaceWarn = false;
  7173. AddDelayedDirectoryDeletion(TargetDir, WinConfiguration->DDDeleteDelay);
  7174. }
  7175. }
  7176. else
  7177. {
  7178. throw Exception(FMTLOAD(CREATE_TEMP_DIR_ERROR, (TargetDir)));
  7179. }
  7180. }
  7181. }
  7182. }
  7183. //---------------------------------------------------------------------------
  7184. void __fastcall TCustomScpExplorerForm::DDDownload(
  7185. TStrings * FilesToCopy, const UnicodeString & TargetDir, const TCopyParamType * CopyParam, int Params)
  7186. {
  7187. DebugAssert(!IsLocalBrowserMode());
  7188. TAutoBatch AutoBatch(this);
  7189. UpdateCopyParamCounters(*CopyParam);
  7190. Terminal->CopyToLocal(FilesToCopy, TargetDir, CopyParam, Params, NULL);
  7191. if (FLAGSET(Params, cpDelete) && (DropSourceControl == RemoteDriveView))
  7192. {
  7193. RemoteDriveView->UpdateDropSource();
  7194. }
  7195. }
  7196. //---------------------------------------------------------------------------
  7197. class TFakeDataObjectFilesEx : public TDataObjectFilesEx
  7198. {
  7199. public:
  7200. __fastcall TFakeDataObjectFilesEx(TFileList * AFileList, bool RenderPIDL,
  7201. bool RenderFilename) : TDataObjectFilesEx(AFileList, RenderPIDL, RenderFilename, true)
  7202. {
  7203. }
  7204. virtual bool __fastcall AllowData(const tagFORMATETC & FormatEtc)
  7205. {
  7206. return (FormatEtc.cfFormat == CF_HDROP) ? false :
  7207. TDataObjectFilesEx::AllowData(FormatEtc);
  7208. }
  7209. };
  7210. //---------------------------------------------------------------------------
  7211. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDCreateDataObject(
  7212. TObject * Sender, TDataObject *& DataObject)
  7213. {
  7214. if (FDDExtMapFile != NULL)
  7215. {
  7216. TFileList * FileList = DragDropFiles(Sender)->FileList;
  7217. if (!FileList->RenderPIDLs() || !FileList->RenderNames())
  7218. {
  7219. Abort();
  7220. }
  7221. if (FileList->Count > 0)
  7222. {
  7223. TDataObjectFilesEx * FilesObject = new TFakeDataObjectFilesEx(FileList, true, true);
  7224. if (!FilesObject->IsValid(true, true))
  7225. {
  7226. FilesObject->_Release();
  7227. }
  7228. else
  7229. {
  7230. DataObject = FilesObject;
  7231. }
  7232. }
  7233. }
  7234. }
  7235. //---------------------------------------------------------------------------
  7236. void __fastcall TCustomScpExplorerForm::GoToCommandLine()
  7237. {
  7238. DebugFail();
  7239. }
  7240. //---------------------------------------------------------------------------
  7241. void __fastcall TCustomScpExplorerForm::GoToTree()
  7242. {
  7243. ComponentVisible[fcRemoteTree] = true;
  7244. DriveView(osRemote)->SetFocus();
  7245. }
  7246. //---------------------------------------------------------------------------
  7247. TStrings * __fastcall TCustomScpExplorerForm::PanelExport(TOperationSide Side,
  7248. TPanelExport Export)
  7249. {
  7250. TCustomDirView * DirView = this->DirView(Side);
  7251. std::unique_ptr<TStrings> ExportData(new TStringList());
  7252. switch (Export)
  7253. {
  7254. case pePath:
  7255. ExportData->Add(DirView->PathName);
  7256. break;
  7257. case peFileList:
  7258. case peFullFileList:
  7259. {
  7260. bool FullPath = (Export == peFullFileList);
  7261. DirView->CreateFileList(false, FullPath, ExportData.get());
  7262. for (int Index = 0; Index < ExportData->Count; Index++)
  7263. {
  7264. if (ExportData->Strings[Index].Pos(L" ") > 0)
  7265. {
  7266. ExportData->Strings[Index] = FORMAT(L"\"%s\"", (ExportData->Strings[Index]));
  7267. }
  7268. }
  7269. }
  7270. break;
  7271. default:
  7272. DebugFail();
  7273. }
  7274. return ExportData.release();
  7275. }
  7276. //---------------------------------------------------------------------------
  7277. void __fastcall TCustomScpExplorerForm::PanelExport(TOperationSide Side,
  7278. TPanelExport Export, TPanelExportDestination Destination)
  7279. {
  7280. std::unique_ptr<TStrings> ExportData(PanelExport(Side, Export));
  7281. PanelExportStore(Side, Export, Destination, ExportData.get());
  7282. }
  7283. //---------------------------------------------------------------------------
  7284. void __fastcall TCustomScpExplorerForm::PanelExportStore(TOperationSide /*Side*/,
  7285. TPanelExport /*Export*/, TPanelExportDestination Destination,
  7286. TStrings * ExportData)
  7287. {
  7288. if (Destination == pedClipboard)
  7289. {
  7290. TInstantOperationVisualizer Visualizer;
  7291. CopyToClipboard(ExportData);
  7292. }
  7293. else
  7294. {
  7295. DebugFail();
  7296. }
  7297. }
  7298. //---------------------------------------------------------------------------
  7299. void __fastcall TCustomScpExplorerForm::Filter(TOperationSide Side)
  7300. {
  7301. TCustomDirView * DirView = this->DirView(Side);
  7302. UnicodeString Mask = DirView->Mask;
  7303. if (DoFilterMaskDialog(DirView, Mask))
  7304. {
  7305. DirView->Mask = TFileMasks::NormalizeMask(Mask);
  7306. Configuration->Usage->Inc(L"Filters");
  7307. }
  7308. }
  7309. //---------------------------------------------------------------------------
  7310. TQueueOperation __fastcall TCustomScpExplorerForm::DefaultQueueOperation()
  7311. {
  7312. return FQueueController->DefaultOperation();
  7313. }
  7314. //---------------------------------------------------------------------------
  7315. bool __fastcall TCustomScpExplorerForm::AllowQueueOperation(
  7316. TQueueOperation Operation, void ** Param)
  7317. {
  7318. switch (Operation)
  7319. {
  7320. case qoPreferences:
  7321. return true;
  7322. case qoGoTo:
  7323. return ComponentVisible[fcQueueView] && QueueView3->Enabled;
  7324. case qoOnceEmpty:
  7325. return IsAnythingQueued();
  7326. default:
  7327. return FQueueController->AllowOperation(Operation, Param);
  7328. }
  7329. }
  7330. //---------------------------------------------------------------------------
  7331. void __fastcall TCustomScpExplorerForm::GoToQueue()
  7332. {
  7333. if (DebugAlwaysTrue(QueueView3->Visible))
  7334. {
  7335. QueueView3->SetFocus();
  7336. }
  7337. }
  7338. //---------------------------------------------------------------------------
  7339. void __fastcall TCustomScpExplorerForm::ExecuteQueueOperation(
  7340. TQueueOperation Operation, void * Param)
  7341. {
  7342. if (Operation == qoGoTo)
  7343. {
  7344. GoToQueue();
  7345. }
  7346. else if (Operation == qoPreferences)
  7347. {
  7348. PreferencesDialog(pmQueue);
  7349. }
  7350. else
  7351. {
  7352. FQueueController->ExecuteOperation(Operation, Param);
  7353. }
  7354. }
  7355. //---------------------------------------------------------------------------
  7356. bool __fastcall TCustomScpExplorerForm::GetQueueEnabled()
  7357. {
  7358. return (Queue != NULL) && Queue->Enabled;
  7359. }
  7360. //---------------------------------------------------------------------------
  7361. void __fastcall TCustomScpExplorerForm::ToggleQueueEnabled()
  7362. {
  7363. DebugAssert(Queue != NULL);
  7364. if (Queue != NULL)
  7365. {
  7366. Queue->Enabled = !Queue->Enabled;
  7367. }
  7368. }
  7369. //---------------------------------------------------------------------------
  7370. void __fastcall TCustomScpExplorerForm::QueueView3ContextPopup(
  7371. TObject * /*Sender*/, TPoint & /*MousePos*/, bool & /*Handled*/)
  7372. {
  7373. FQueueActedItem = QueueView3->ItemFocused;
  7374. }
  7375. //---------------------------------------------------------------------------
  7376. /*virtual*/ int __fastcall TCustomScpExplorerForm::GetStaticComponentsHeight()
  7377. {
  7378. return TopDock->Height + QueueSplitter->Height;
  7379. }
  7380. //---------------------------------------------------------------------------
  7381. void __fastcall TCustomScpExplorerForm::QueueSplitterCanResize(
  7382. TObject * /*Sender*/, int & NewSize, bool & Accept)
  7383. {
  7384. // when queue is hidden by double-clicking splitter, stray attempt to
  7385. // resize the panel with strange value arrives, make sure it is ignored
  7386. if (ComponentVisible[fcQueueView])
  7387. {
  7388. int HeightLimit = ClientHeight - GetStaticComponentsHeight() -
  7389. RemotePanel->Constraints->MinHeight;
  7390. if (NewSize > HeightLimit)
  7391. {
  7392. NewSize = HeightLimit;
  7393. }
  7394. }
  7395. else
  7396. {
  7397. Accept = false;
  7398. }
  7399. }
  7400. //---------------------------------------------------------------------------
  7401. void __fastcall TCustomScpExplorerForm::QueueView3StartDrag(TObject * /*Sender*/,
  7402. TDragObject *& /*DragObject*/)
  7403. {
  7404. FQueueActedItem = QueueView3->ItemFocused;
  7405. }
  7406. //---------------------------------------------------------------------------
  7407. void __fastcall TCustomScpExplorerForm::QueueView3DragOver(TObject * /*Sender*/,
  7408. TObject * Source, int X, int Y, TDragState /*State*/, bool & Accept)
  7409. {
  7410. Accept = true;
  7411. if (Source == QueueView3)
  7412. {
  7413. TListItem * DropTarget = QueueView3->GetItemAt(X, Y);
  7414. Accept = (DropTarget != NULL) && (FQueueActedItem != NULL);
  7415. if (Accept)
  7416. {
  7417. TQueueItemProxy * QueueItem;
  7418. TQueueItemProxy * DestQueueItem;
  7419. QueueItem = static_cast<TQueueItemProxy *>(FQueueActedItem->Data);
  7420. DestQueueItem = static_cast<TQueueItemProxy *>(DropTarget->Data);
  7421. Accept = (QueueItem != DestQueueItem) &&
  7422. (QueueItem->Status == TQueueItem::qsPending) &&
  7423. (DestQueueItem->Status == TQueueItem::qsPending);
  7424. }
  7425. }
  7426. }
  7427. //---------------------------------------------------------------------------
  7428. void __fastcall TCustomScpExplorerForm::QueueView3DragDrop(TObject * /*Sender*/,
  7429. TObject * /*Source*/, int /*X*/, int /*Y*/)
  7430. {
  7431. if ((FQueueActedItem != NULL) && (QueueView3->DropTarget != NULL))
  7432. {
  7433. TQueueItemProxy * QueueItem;
  7434. TQueueItemProxy * DestQueueItem;
  7435. QueueItem = static_cast<TQueueItemProxy *>(FQueueActedItem->Data);
  7436. DestQueueItem = static_cast<TQueueItemProxy *>(QueueView3->DropTarget->Data);
  7437. QueueItem->Move(DestQueueItem);
  7438. }
  7439. }
  7440. //---------------------------------------------------------------------------
  7441. void __fastcall TCustomScpExplorerForm::QueueView3Enter(TObject * /*Sender*/)
  7442. {
  7443. if ((QueueView3->ItemFocused == NULL) &&
  7444. (QueueView3->Items->Count > 0))
  7445. {
  7446. QueueView3->ItemFocused = QueueView3->Items->Item[0];
  7447. }
  7448. QueueLabelUpdateStatus();
  7449. }
  7450. //---------------------------------------------------------------------------
  7451. void __fastcall TCustomScpExplorerForm::QueueView3Exit(TObject * /*Sender*/)
  7452. {
  7453. QueueLabelUpdateStatus();
  7454. }
  7455. //---------------------------------------------------------------------------
  7456. void __fastcall TCustomScpExplorerForm::QueueLabelUpdateStatus()
  7457. {
  7458. QueueLabel->UpdateStatus();
  7459. }
  7460. //---------------------------------------------------------------------------
  7461. void __fastcall TCustomScpExplorerForm::QueueView3SelectItem(
  7462. TObject * /*Sender*/, TListItem * /*Item*/, bool Selected)
  7463. {
  7464. if (Selected)
  7465. {
  7466. NonVisualDataModule->UpdateNonVisibleActions();
  7467. }
  7468. }
  7469. //---------------------------------------------------------------------------
  7470. TDragDropFilesEx * __fastcall TCustomScpExplorerForm::DragDropFiles(TObject * Sender)
  7471. {
  7472. TDragDropFilesEx * Result = NULL;
  7473. if (Sender == SessionsPageControl)
  7474. {
  7475. Result = FSessionsDragDropFilesEx;
  7476. }
  7477. else if (Sender == QueueView3)
  7478. {
  7479. Result = FQueueDragDropFilesEx;
  7480. }
  7481. else
  7482. {
  7483. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  7484. if (DirView != NULL)
  7485. {
  7486. Result = DirView->DragDropFilesEx;
  7487. }
  7488. else
  7489. {
  7490. TCustomDriveView * DriveView = dynamic_cast<TCustomDriveView *>(Sender);
  7491. if (DriveView != NULL)
  7492. {
  7493. Result = DriveView->DragDropFilesEx;
  7494. }
  7495. }
  7496. }
  7497. DebugAssert(Result != NULL);
  7498. return Result;
  7499. }
  7500. //---------------------------------------------------------------------------
  7501. bool __fastcall TCustomScpExplorerForm::SelectedAllFilesInDirView(TCustomDirView * DView)
  7502. {
  7503. return (DView->SelCount == DView->FilesCount);
  7504. }
  7505. //---------------------------------------------------------------------------
  7506. bool __fastcall TCustomScpExplorerForm::DraggingAllFilesFromDirView(TOperationSide Side, TStrings * FileList)
  7507. {
  7508. return HasDirView[Side] && (DropSourceControl == DirView(Side)) && (FileList->Count == DirView(Side)->FilesCount);
  7509. }
  7510. //---------------------------------------------------------------------------
  7511. void __fastcall TCustomScpExplorerForm::RemoteFileControlDragDropFileOperation(
  7512. TObject * Sender, int Effect, UnicodeString TargetPath, bool ForceQueue, bool DragDrop)
  7513. {
  7514. DebugAssert(!IsLocalBrowserMode());
  7515. TFileOperation Operation;
  7516. switch (Effect)
  7517. {
  7518. case DROPEFFECT_MOVE:
  7519. Operation = foMove;
  7520. break;
  7521. case DROPEFFECT_COPY:
  7522. // occures on WinXP (reported by user)
  7523. default:
  7524. Operation = foCopy;
  7525. break;
  7526. };
  7527. TDragDropFilesEx * DragDropFilesEx = DragDropFiles(Sender);
  7528. // see a comment in TUnixDirView::PerformItemDragDropOperation
  7529. if (DragDropFilesEx->FileList->Count > 0)
  7530. {
  7531. TStrings * FileList = new TStringList();
  7532. try
  7533. {
  7534. for (int Index = 0; Index < DragDropFilesEx->FileList->Count; Index++)
  7535. {
  7536. FileList->Add(DragDropFilesEx->FileList->Items[Index]->Name);
  7537. }
  7538. FDragDropOperation = true;
  7539. TTransferOperationParam Param;
  7540. Param.TargetDirectory = TargetPath;
  7541. // upload, no temp dirs
  7542. Param.Temp = false;
  7543. Param.DragDrop = DragDrop;
  7544. Param.Options =
  7545. FLAGMASK(DraggingAllFilesFromDirView(osLocal, FileList), coAllFiles);
  7546. if (ForceQueue)
  7547. {
  7548. Param.Queue = asOn;
  7549. }
  7550. bool NoConfirmation = DragDrop ? (WinConfiguration->DDTransferConfirmation == asOff) : false;
  7551. if (ExecuteFileOperation(Operation, osLocal, FileList, NoConfirmation, &Param))
  7552. {
  7553. if (IsFileControl(DropSourceControl, osLocal))
  7554. {
  7555. Configuration->Usage->Inc(L"UploadsDragDropInternal");
  7556. }
  7557. else
  7558. {
  7559. Configuration->Usage->Inc(L"UploadsDragDropExternal");
  7560. }
  7561. }
  7562. }
  7563. __finally
  7564. {
  7565. FDragDropOperation = false;
  7566. delete FileList;
  7567. }
  7568. }
  7569. }
  7570. //---------------------------------------------------------------------------
  7571. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDFileOperation(
  7572. TObject * Sender, int Effect, UnicodeString /*SourcePath*/,
  7573. UnicodeString TargetPath, bool Paste, bool & /*DoOperation*/)
  7574. {
  7575. RemoteFileControlDragDropFileOperation(Sender, Effect, TargetPath, false, !Paste);
  7576. }
  7577. //---------------------------------------------------------------------------
  7578. void __fastcall TCustomScpExplorerForm::RemoteFileContolDDChooseEffect(
  7579. TObject * Sender, int grfKeyState, int & dwEffect)
  7580. {
  7581. DebugAssert(!IsLocalBrowserMode());
  7582. // if any drop effect is allowed at all (e.g. no drop to self and drop to parent)
  7583. if ((dwEffect != DROPEFFECT_NONE) &&
  7584. IsFileControl(DropSourceControl, osRemote))
  7585. {
  7586. // do not allow drop on remote panel (on free space, still allow drop on directories)
  7587. if ((Sender == RemoteDirView) && (DropSourceControl == RemoteDirView) &&
  7588. (RemoteDirView->DropTarget == NULL))
  7589. {
  7590. dwEffect = DROPEFFECT_NONE;
  7591. }
  7592. else
  7593. {
  7594. if (dwEffect == DROPEFFECT_COPY)
  7595. {
  7596. bool MoveCapable = FTerminal->IsCapable[fcRemoteMove];
  7597. // currently we support copying always (at least via temporary directory);
  7598. // remove associated checks once this all proves stable and working
  7599. bool CopyCapable = true;
  7600. // if we do not support neither of operations, there's no discussion
  7601. if (!MoveCapable && !CopyCapable)
  7602. {
  7603. dwEffect = DROPEFFECT_NONE;
  7604. }
  7605. else
  7606. {
  7607. // The default effect inside remote panel is move,
  7608. // unless we do not support it, but support copy
  7609. if (FLAGCLEAR(grfKeyState, MK_CONTROL))
  7610. {
  7611. dwEffect = MoveCapable ? DROPEFFECT_MOVE : DROPEFFECT_COPY;
  7612. }
  7613. else
  7614. {
  7615. // with ctrl-down, we want copy unless it is not supported
  7616. dwEffect = CopyCapable ? DROPEFFECT_COPY : DROPEFFECT_NONE;
  7617. }
  7618. }
  7619. }
  7620. }
  7621. }
  7622. }
  7623. //---------------------------------------------------------------------------
  7624. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDDragFileName(
  7625. TObject * Sender, TRemoteFile * File, UnicodeString & FileName)
  7626. {
  7627. DebugAssert(!IsLocalBrowserMode());
  7628. if (FDDTotalSize >= 0)
  7629. {
  7630. if (File->IsDirectory)
  7631. {
  7632. FDDTotalSize = -1;
  7633. }
  7634. else
  7635. {
  7636. FDDTotalSize += File->Size;
  7637. }
  7638. }
  7639. DebugAssert(!FDragTempDir.IsEmpty());
  7640. // TODO: this is quite ineffective
  7641. // TODO: what if invalid character replacement is disabled?
  7642. FileName = FDragTempDir + GUIConfiguration->CurrentCopyParam.ValidLocalFileName(File->FileName);
  7643. UnicodeString TransferFileName;
  7644. if (Sender == RemoteDriveView)
  7645. {
  7646. TransferFileName = UnixExcludeTrailingBackslash(File->FullFileName);
  7647. }
  7648. else
  7649. {
  7650. TransferFileName = File->FileName;
  7651. }
  7652. FDDFileList->AddObject(TransferFileName, File);
  7653. }
  7654. //---------------------------------------------------------------------------
  7655. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDDragDetect(
  7656. TObject * /*Sender*/, int /*grfKeyState*/, const TPoint & /*DetectStart*/,
  7657. const TPoint & /*Point*/, TDragDetectStatus /*DragStatus*/)
  7658. {
  7659. // sometimes we do not get DDEnd so the list is not released
  7660. SAFE_DESTROY(FDDFileList);
  7661. FDDFileList = new TStringList();
  7662. FDragTempDir = WinConfiguration->TemporaryDir();
  7663. FDDTotalSize = 0;
  7664. FDragDropSshTerminate.reset(NULL);
  7665. }
  7666. //---------------------------------------------------------------------------
  7667. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDQueryContinueDrag(
  7668. TObject * /*Sender*/, BOOL /*FEscapePressed*/, int /*grfKeyState*/,
  7669. HRESULT & Result)
  7670. {
  7671. if (Result == DRAGDROP_S_DROP)
  7672. {
  7673. try
  7674. {
  7675. GlobalDragImageList->HideDragImage();
  7676. try
  7677. {
  7678. RemoteFileControlDDTargetDrop();
  7679. }
  7680. catch(ESshTerminate & E)
  7681. {
  7682. FDragDropSshTerminate.reset(E.Clone());
  7683. }
  7684. }
  7685. catch (Exception &E)
  7686. {
  7687. // If downloading fails we need to cancel drag&drop, otherwise
  7688. // Explorer shows error
  7689. // But by the way exception probably never reach this point as
  7690. // it's catched on way.
  7691. // Fatal exceptions get here (like when opening a secondary shell extension for file duplication fails).
  7692. Result = DRAGDROP_S_CANCEL;
  7693. ShowExtendedException(Terminal, &E);
  7694. }
  7695. }
  7696. }
  7697. //---------------------------------------------------------------------------
  7698. void __fastcall TCustomScpExplorerForm::DirViewMatchMask(
  7699. TObject * /*Sender*/, UnicodeString FileName, bool Directory, __int64 Size,
  7700. TDateTime Modification, UnicodeString Masks, bool & Matches, bool AllowImplicitMatches)
  7701. {
  7702. TFileMasks::TParams MaskParams;
  7703. MaskParams.Size = Size;
  7704. MaskParams.Modification = Modification;
  7705. // this does not re-parse the mask if it is the same as the last time
  7706. FDirViewMatchMask = Masks;
  7707. bool ImplicitMatch;
  7708. Matches =
  7709. // RecurseInclude parameter has no effect as the Path is empty
  7710. FDirViewMatchMask.Matches(FileName, Directory, UnicodeString(L""), &MaskParams, true, ImplicitMatch) &&
  7711. (AllowImplicitMatches || !ImplicitMatch);
  7712. }
  7713. //---------------------------------------------------------------------------
  7714. void __fastcall TCustomScpExplorerForm::DirViewGetOverlay(
  7715. TObject * Sender, TListItem * Item, WORD & Indexes)
  7716. {
  7717. TCustomDirView * DirView = reinterpret_cast<TCustomDirView *>(Sender);
  7718. UnicodeString Ext;
  7719. if (dynamic_cast<TUnixDirView *>(DirView) != NULL)
  7720. {
  7721. Ext = UnixExtractFileExt(DirView->ItemFileName(Item));
  7722. }
  7723. else
  7724. {
  7725. Ext = ExtractFileExt(DirView->ItemFileName(Item));
  7726. }
  7727. if (SameText(Ext, Configuration->PartialExt))
  7728. {
  7729. Indexes |= oiPartial;
  7730. }
  7731. }
  7732. //---------------------------------------------------------------------------
  7733. bool __fastcall TCustomScpExplorerForm::CanPasteToDirViewFromClipBoard()
  7734. {
  7735. return
  7736. DirViewEnabled(osCurrent) &&
  7737. DirView(osCurrent)->CanPasteFromClipBoard();
  7738. }
  7739. //---------------------------------------------------------------------------
  7740. bool __fastcall TCustomScpExplorerForm::CanPasteFromClipBoard()
  7741. {
  7742. bool Result = false;
  7743. if (CanPasteToDirViewFromClipBoard())
  7744. {
  7745. Result = true;
  7746. }
  7747. else
  7748. {
  7749. UnicodeString ClipboardText;
  7750. if (NonEmptyTextFromClipboard(ClipboardText))
  7751. {
  7752. if (StoredSessions->IsUrl(ClipboardText))
  7753. {
  7754. Result = true;
  7755. }
  7756. else
  7757. {
  7758. Result = DirViewEnabled(osCurrent);
  7759. }
  7760. }
  7761. }
  7762. return Result;
  7763. }
  7764. //---------------------------------------------------------------------------
  7765. void __fastcall TCustomScpExplorerForm::PasteFromClipBoard()
  7766. {
  7767. if (DoesClipboardContainOurFiles())
  7768. {
  7769. if (DebugAlwaysTrue(CanPasteToDirViewFromClipBoard()))
  7770. {
  7771. DebugAssert(!IsLocalBrowserMode());
  7772. TTerminalManager * Manager = TTerminalManager::Instance();
  7773. TTerminal * TergetTerminal = Manager->ActiveTerminal;
  7774. Manager->ActiveTerminal = FClipboardTerminal;
  7775. ExecuteFileOperation(foRemoteCopy, osRemote, FClipboardFileList.get(), false, TergetTerminal);
  7776. }
  7777. }
  7778. else if (CanPasteToDirViewFromClipBoard())
  7779. {
  7780. DirView(osCurrent)->PasteFromClipBoard();
  7781. }
  7782. else
  7783. {
  7784. UnicodeString ClipboardText;
  7785. if (NonEmptyTextFromClipboard(ClipboardText))
  7786. {
  7787. if (StoredSessions->IsUrl(ClipboardText))
  7788. {
  7789. NewSession(false, ClipboardText);
  7790. }
  7791. else
  7792. {
  7793. DirView(osCurrent)->Path = ClipboardText;
  7794. }
  7795. }
  7796. }
  7797. }
  7798. //---------------------------------------------------------------------------
  7799. void __fastcall TCustomScpExplorerForm::FileListFromClipboard()
  7800. {
  7801. // TBD
  7802. }
  7803. //---------------------------------------------------------------------------
  7804. void __fastcall TCustomScpExplorerForm::SelectAll(TOperationSide Side, TSelectMode Mode)
  7805. {
  7806. TCustomDirView * ADirView = DirView(Side);
  7807. ADirView->SelectAll(Mode);
  7808. ADirView->SetFocus();
  7809. }
  7810. //---------------------------------------------------------------------------
  7811. void __fastcall TCustomScpExplorerForm::SelectByMask(TOperationSide Side, bool Select)
  7812. {
  7813. TCustomDirView * ADirView = DirView(Side);
  7814. TFileFilter Filter;
  7815. DefaultFileFilter(Filter);
  7816. if (DoSelectMaskDialog(ADirView, Select, Filter))
  7817. {
  7818. Configuration->Usage->Inc(L"MaskSelections");
  7819. ADirView->SelectFiles(Filter, Select);
  7820. ADirView->SetFocus();
  7821. }
  7822. }
  7823. //---------------------------------------------------------------------------
  7824. void __fastcall TCustomScpExplorerForm::RestoreSelectedNames(TOperationSide Side)
  7825. {
  7826. TCustomDirView * ADirView = DirView(Side);
  7827. ADirView->RestoreSelectedNames();
  7828. ADirView->SetFocus();
  7829. }
  7830. //---------------------------------------------------------------------------
  7831. void __fastcall TCustomScpExplorerForm::SelectSameExt(bool Select)
  7832. {
  7833. TCustomDirView * CurrentDirView = DirView(osCurrent);
  7834. if (DebugAlwaysTrue(CurrentDirView->ItemFocused != NULL))
  7835. {
  7836. UnicodeString FileName = CurrentDirView->ItemFileName(CurrentDirView->ItemFocused);
  7837. UnicodeString Ext;
  7838. if (!IsSideLocalBrowser(osCurrent))
  7839. {
  7840. Ext = UnixExtractFileExt(FileName);
  7841. }
  7842. else
  7843. {
  7844. Ext = ExtractFileExt(FileName);
  7845. }
  7846. if (Ext.IsEmpty())
  7847. {
  7848. Ext = L".";
  7849. }
  7850. TFileFilter Filter;
  7851. Filter.Masks = FORMAT(L"*%s", (Ext));
  7852. Filter.Directories = false;
  7853. CurrentDirView->SelectFiles(Filter, Select);
  7854. }
  7855. }
  7856. //---------------------------------------------------------------------------
  7857. UnicodeString __fastcall TCustomScpExplorerForm::FileStatusBarText(
  7858. const TStatusFileInfo & FileInfo, TOperationSide Side)
  7859. {
  7860. UnicodeString Result;
  7861. if (!FIncrementalSearch.IsEmpty() && (Side == GetSide(osCurrent)))
  7862. {
  7863. Result = FormatIncrementalSearchStatus(FIncrementalSearch, FIncrementalSearchHaveNext);
  7864. }
  7865. else if (!IsSideLocalBrowser(Side) && ((Terminal == NULL) || Terminal->Disconnected))
  7866. {
  7867. // noop
  7868. }
  7869. else
  7870. {
  7871. Result =
  7872. FMTLOAD(FILE_INFO_FORMAT,
  7873. (FormatBytes(FileInfo.SelectedSize),
  7874. FormatBytes(FileInfo.FilesSize),
  7875. FormatNumber(FileInfo.SelectedCount),
  7876. FormatNumber(FileInfo.FilesCount)));
  7877. }
  7878. return Result;
  7879. }
  7880. //---------------------------------------------------------------------------
  7881. void __fastcall TCustomScpExplorerForm::FileStatusBarPanelClick(
  7882. TTBXStatusPanel * Panel, TOperationSide Side)
  7883. {
  7884. if (Panel->Index == 1)
  7885. {
  7886. ToggleShowHiddenFiles();
  7887. }
  7888. else if (Panel->Index == 2)
  7889. {
  7890. Filter(Side);
  7891. }
  7892. }
  7893. //---------------------------------------------------------------------------
  7894. void __fastcall TCustomScpExplorerForm::UpdateFileStatusBar(
  7895. TTBXStatusBar * StatusBar, const TStatusFileInfo & FileInfo, TOperationSide Side)
  7896. {
  7897. DebugAssert(!StatusBar->SimplePanel);
  7898. StatusBar->Panels->Items[0]->Caption = FileStatusBarText(FileInfo, Side);
  7899. UpdateFileStatusExtendedPanels(StatusBar, FileInfo);
  7900. }
  7901. //---------------------------------------------------------------------------
  7902. void __fastcall TCustomScpExplorerForm::UpdateFileStatusExtendedPanels(
  7903. TTBXStatusBar * StatusBar, const TStatusFileInfo & FileInfo)
  7904. {
  7905. DebugAssert(StatusBar->Panels->Count >= 3);
  7906. TTBXStatusPanel * HiddenFilesPanel = StatusBar->Panels->Items[1];
  7907. if (FileInfo.HiddenCount > 0)
  7908. {
  7909. HiddenFilesPanel->Caption = FMTLOAD(FILE_INFO_HIDDEN2, (FormatNumber(FileInfo.HiddenCount)));
  7910. HiddenFilesPanel->ViewPriority = 90; // <100 allows hiding panel when it does not fit
  7911. }
  7912. else
  7913. {
  7914. HiddenFilesPanel->ViewPriority = 0;
  7915. // not really necessary, just to cleanup no-longer-valid data
  7916. HiddenFilesPanel->Caption = L"";
  7917. }
  7918. TTBXStatusPanel * FilteredFilesPanel = StatusBar->Panels->Items[2];
  7919. if (FileInfo.FilteredCount > 0)
  7920. {
  7921. FilteredFilesPanel->Caption = FMTLOAD(FILE_INFO_FILTERED2, (FormatNumber(FileInfo.FilteredCount)));
  7922. FilteredFilesPanel->ViewPriority = 90; // <100 allows hiding panel when it does not fit
  7923. }
  7924. else
  7925. {
  7926. FilteredFilesPanel->ViewPriority = 0;
  7927. // not really necessary, just to cleanup no-longer-valid data
  7928. FilteredFilesPanel->Caption = L"";
  7929. }
  7930. }
  7931. //---------------------------------------------------------------------------
  7932. void __fastcall TCustomScpExplorerForm::RemoteStatusBarClick(
  7933. TObject * /*Sender*/)
  7934. {
  7935. if (DirView(osRemote)->Enabled)
  7936. {
  7937. DirView(osRemote)->SetFocus();
  7938. }
  7939. }
  7940. //---------------------------------------------------------------------------
  7941. void __fastcall TCustomScpExplorerForm::ToggleQueueVisibility()
  7942. {
  7943. TQueueViewConfiguration Config = WinConfiguration->QueueView;
  7944. switch (Config.Show)
  7945. {
  7946. case qvShow:
  7947. if ((FQueueStatus != NULL) && (FQueueStatus->Count > 0))
  7948. {
  7949. Config.Show = qvHide;
  7950. }
  7951. else
  7952. {
  7953. Config.Show = Config.LastHideShow;
  7954. }
  7955. break;
  7956. case qvHideWhenEmpty:
  7957. if (ComponentVisible[fcQueueView])
  7958. {
  7959. Config.Show = qvHide;
  7960. }
  7961. else
  7962. {
  7963. Config.LastHideShow = Config.Show;
  7964. Config.Show = qvShow;
  7965. }
  7966. break;
  7967. case qvHide:
  7968. Config.LastHideShow = Config.Show;
  7969. Config.Show = qvShow;
  7970. break;
  7971. }
  7972. WinConfiguration->QueueView = Config;
  7973. }
  7974. //---------------------------------------------------------------------------
  7975. UnicodeString __fastcall TCustomScpExplorerForm::PathForCaption()
  7976. {
  7977. UnicodeString Result;
  7978. if (FTerminal != NULL)
  7979. {
  7980. switch (WinConfiguration->PathInCaption)
  7981. {
  7982. case picShort:
  7983. Result = TTerminalManager::Instance()->GetTerminalShortPath(FTerminal);
  7984. break;
  7985. case picFull:
  7986. Result = FTerminal->CurrentDirectory;
  7987. break;
  7988. }
  7989. }
  7990. return Result;
  7991. }
  7992. //---------------------------------------------------------------------------
  7993. TColor __fastcall TCustomScpExplorerForm::PanelColor()
  7994. {
  7995. TColor Result = GetWindowColor(FSessionColor);
  7996. return Result;
  7997. }
  7998. //---------------------------------------------------------------------------
  7999. TColor __fastcall TCustomScpExplorerForm::DisabledPanelColor()
  8000. {
  8001. TColor Result = (WinConfiguration->UseDarkTheme() ? static_cast<TColor>(RGB(0x40, 0x40, 0x40)) : clBtnFace);
  8002. return Result;
  8003. }
  8004. //---------------------------------------------------------------------------
  8005. void __fastcall TCustomScpExplorerForm::UpdateControls()
  8006. {
  8007. TTerminalManager::Instance()->UpdateAppTitle();
  8008. // WORAKRDOUND: Disabling list view when it is not showing yet does not set its
  8009. // background to gray on Windows 7 (works on Windows 10).
  8010. // See also EnableControl
  8011. if (Showing)
  8012. {
  8013. if (Terminal != NULL)
  8014. {
  8015. // Update path when it changes
  8016. if ((SessionsPageControl->ActivePage != NULL) && (GetSessionTabTerminal(SessionsPageControl->ActivePage) == Terminal))
  8017. {
  8018. UpdateSessionTab(SessionsPageControl->ActivePage);
  8019. }
  8020. }
  8021. NonVisualDataModule->ReconnectSessionAction->Update();
  8022. ReconnectToolbar->Visible = NonVisualDataModule->ReconnectSessionAction->Visible;
  8023. // ReconnectSessionAction is hidden when disabled, so enabling it actualy resizes the toolbar
  8024. CenterReconnectToolbar();
  8025. bool HasTerminal = (Terminal != NULL) && Terminal->Active;
  8026. if (HasTerminal)
  8027. {
  8028. // TODO needed yet with second local browser?
  8029. if (!RemoteDirView->Enabled)
  8030. {
  8031. RemoteDirView->Enabled = true;
  8032. if (FRemoteDirViewWasFocused)
  8033. {
  8034. ActiveControl = RemoteDirView;
  8035. }
  8036. }
  8037. RemoteDirView->Color = PanelColor();
  8038. }
  8039. else
  8040. {
  8041. if (RemoteDirView->Enabled)
  8042. {
  8043. // This is first called when the form is being constructed
  8044. // (not anymore due to Showing test above)
  8045. // but the false is overriden in the constructor later.
  8046. // An even later in TScpCommanderForm::DoShow()
  8047. FRemoteDirViewWasFocused = (ActiveControl == RemoteDirView);
  8048. RemoteDirView->Enabled = false;
  8049. }
  8050. RemoteDirView->Color = DisabledPanelColor();
  8051. }
  8052. RemoteDirView->Font->Color = GetWindowTextColor(RemoteDirView->Color);
  8053. RemoteDriveView->Enabled = RemoteDirView->Enabled;
  8054. RemoteDriveView->Color = RemoteDirView->Color;
  8055. RemoteDriveView->Font->Color = RemoteDirView->Font->Color;
  8056. QueueView3->Enabled = HasTerminal && Terminal->IsCapable[fsBackgroundTransfers];
  8057. QueueView3->Color = QueueView3->Enabled ? GetWindowColor() : DisabledPanelColor();
  8058. QueueView3->Font->Color = GetWindowTextColor(QueueView3->Color);
  8059. QueueLabelUpdateStatus();
  8060. RemoteDirView->DarkMode = WinConfiguration->UseDarkTheme();
  8061. RemoteDriveView->DarkMode = RemoteDirView->DarkMode;
  8062. reinterpret_cast<TTBCustomItem *>(GetComponent(fcRemotePathComboBox))->Enabled = HasTerminal;
  8063. }
  8064. }
  8065. //---------------------------------------------------------------------------
  8066. void __fastcall TCustomScpExplorerForm::DoDirViewLoaded(TCustomDirView * /*Sender*/)
  8067. {
  8068. UpdateControls();
  8069. }
  8070. //---------------------------------------------------------------------------
  8071. void __fastcall TCustomScpExplorerForm::DirViewLoaded(
  8072. TObject * Sender)
  8073. {
  8074. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  8075. DebugAssert(DirView != NULL);
  8076. DoDirViewLoaded(DirView);
  8077. TransferPresetAutoSelect();
  8078. }
  8079. //---------------------------------------------------------------------------
  8080. void __fastcall TCustomScpExplorerForm::StartUpdates()
  8081. {
  8082. TUpdatesConfiguration Updates = WinConfiguration->Updates;
  8083. // first run after installation
  8084. if (double(Updates.LastCheck) == 0)
  8085. {
  8086. // make sure next time there will be an update (if enabled)
  8087. Updates.LastCheck = TDateTime(1);
  8088. WinConfiguration->Updates = Updates;
  8089. }
  8090. else if ((double(Updates.Period) > 0) &&
  8091. (Now() - Updates.LastCheck >= Updates.Period))
  8092. {
  8093. TThreadMethod OnUpdatesChecked = NULL;
  8094. if (!IsUWP())
  8095. {
  8096. OnUpdatesChecked = UpdatesChecked;
  8097. }
  8098. StartUpdateThread(OnUpdatesChecked);
  8099. }
  8100. }
  8101. //---------------------------------------------------------------------------
  8102. void __fastcall TCustomScpExplorerForm::UpdatesChecked()
  8103. {
  8104. UnicodeString Message;
  8105. bool New;
  8106. TQueryType Type;
  8107. GetUpdatesMessage(Message, New, Type, false);
  8108. if (!Message.IsEmpty())
  8109. {
  8110. if (New)
  8111. {
  8112. Message = FMTLOAD(NEW_VERSION_CLICK, (Message));
  8113. }
  8114. if (!New && (Type != qtWarning))
  8115. {
  8116. PostNote(UnformatMessage(Message), 0, UpdatesNoteClicked, NULL);
  8117. }
  8118. else
  8119. {
  8120. Notify(NULL, Message, Type, true, UpdatesNoteClicked);
  8121. }
  8122. if (New)
  8123. {
  8124. Configuration->Usage->Inc(L"UpdateNotifications");
  8125. }
  8126. }
  8127. }
  8128. //---------------------------------------------------------------------------
  8129. void __fastcall TCustomScpExplorerForm::UpdatesNoteClicked(TObject * /*Sender*/)
  8130. {
  8131. RestoreApp();
  8132. bool CanDisplay = !NonVisualDataModule->Busy;
  8133. if (!CanDisplay && (Screen->ActiveForm != NULL))
  8134. {
  8135. CanDisplay = (Screen->ActiveForm->Perform(WM_CAN_DISPLAY_UPDATES, 0, 0) != 0);
  8136. }
  8137. if (CanDisplay)
  8138. {
  8139. Configuration->Usage->Inc(L"UpdateNotificationsClicked");
  8140. CheckForUpdates(true);
  8141. }
  8142. else
  8143. {
  8144. Configuration->Usage->Inc(L"UpdateNotificationsBusy");
  8145. }
  8146. }
  8147. //---------------------------------------------------------------------------
  8148. void __fastcall TCustomScpExplorerForm::GetTransferPresetAutoSelectData(
  8149. TCopyParamRuleData & Data)
  8150. {
  8151. DebugAssert(Terminal != NULL);
  8152. Data.HostName = Terminal->SessionData->HostNameExpanded;
  8153. Data.UserName = Terminal->SessionData->UserNameExpanded;
  8154. Data.RemoteDirectory = RemoteDirView->PathName;
  8155. }
  8156. //---------------------------------------------------------------------------
  8157. void __fastcall TCustomScpExplorerForm::TransferPresetAutoSelect()
  8158. {
  8159. // Terminal can be null when we are changing local directory implicitly,
  8160. // because it has been deleted, while no session is connected
  8161. // (Login dialog is open)
  8162. if (FAllowTransferPresetAutoSelect && (Terminal != NULL))
  8163. {
  8164. DebugAssert(!IsLocalBrowserMode());
  8165. TCopyParamRuleData Data;
  8166. GetTransferPresetAutoSelectData(Data);
  8167. int CopyParamIndex = GUIConfiguration->CopyParamList->Find(Data);
  8168. UnicodeString CopyParamCurrent = GUIConfiguration->CopyParamCurrent;
  8169. if (CopyParamIndex < 0)
  8170. {
  8171. // there is no preset that matches autoselection
  8172. // set preset that we consider "default"
  8173. FCopyParamAutoSelected = L""; // forget last autoselected preset
  8174. GUIConfiguration->CopyParamCurrent = FCopyParamDefault;
  8175. }
  8176. else
  8177. {
  8178. // there is preset matching autoselection
  8179. UnicodeString CopyParamName = GUIConfiguration->CopyParamList->Names[CopyParamIndex];
  8180. if (CopyParamName == FCopyParamAutoSelected)
  8181. {
  8182. // autoselected the same preset as the last time
  8183. // make no change (i.e. preserve custom user preset, if any)
  8184. }
  8185. else
  8186. {
  8187. // autoselected the different preset then the last time (or there
  8188. // was default preset set)
  8189. FCopyParamAutoSelected = CopyParamName; // remember autoselection
  8190. GUIConfiguration->CopyParamCurrent = CopyParamName;
  8191. }
  8192. }
  8193. if (GUIConfiguration->CopyParamCurrent != CopyParamCurrent)
  8194. {
  8195. if (CopyParamIndex >= 0)
  8196. {
  8197. Configuration->Usage->Inc(L"CopyParamAutoSelects");
  8198. }
  8199. TTransferPresetNoteData * Data = new TTransferPresetNoteData;
  8200. try
  8201. {
  8202. int Fmt =
  8203. (CopyParamIndex < 0) ?
  8204. (GUIConfiguration->CopyParamIndex < 0 ? COPY_PARAM_DEFAULT_NORM : COPY_PARAM_DEFAULT_CUSTOM) :
  8205. COPY_PARAM_AUTOSELECTED;
  8206. UnicodeString Message = FMTLOAD(Fmt, (StripHotkey(GUIConfiguration->CopyParamCurrent)));
  8207. Data->Message = MainInstructions(Message);
  8208. int CopyParamAttrs = Terminal->UsableCopyParamAttrs(0).General;
  8209. UnicodeString Info = GUIConfiguration->CurrentCopyParam.GetInfoStr(L"\n",
  8210. CopyParamAttrs);
  8211. if (CopyParamIndex >= 0)
  8212. {
  8213. DebugAssert(GUIConfiguration->CopyParamList->Rules[CopyParamIndex] != NULL);
  8214. Info = FORMAT(L"%s\n \n%s", (Info,
  8215. FMTLOAD(COPY_PARAM_RULE,
  8216. (GUIConfiguration->CopyParamList->Rules[CopyParamIndex]->GetInfoStr(L"\n")))));
  8217. }
  8218. Data->Message += L"\n\n" + Info;
  8219. if (WinConfiguration->CopyParamAutoSelectNotice)
  8220. {
  8221. TransferPresetNoteMessage(Data, true);
  8222. }
  8223. else
  8224. {
  8225. PostNote(Message, 0, TransferPresetNoteClicked, Data);
  8226. Data = NULL; // ownership passed
  8227. }
  8228. }
  8229. __finally
  8230. {
  8231. delete Data;
  8232. }
  8233. }
  8234. }
  8235. }
  8236. //---------------------------------------------------------------------------
  8237. void __fastcall TCustomScpExplorerForm::TransferPresetNoteMessage(
  8238. TTransferPresetNoteData * NoteData, bool AllowNeverAskAgain)
  8239. {
  8240. DebugAssert(NoteData != NULL);
  8241. DebugAssert(!IsLocalBrowserMode());
  8242. TMessageParams Params(AllowNeverAskAgain ? mpNeverAskAgainCheck : 0);
  8243. TQueryButtonAlias Aliases[1];
  8244. Aliases[0].Button = qaIgnore; // "ignore" is after "ok"
  8245. Aliases[0].Alias = LoadStr(CONFIGURE_BUTTON);
  8246. Params.Aliases = Aliases;
  8247. Params.AliasesCount = LENOF(Aliases);
  8248. unsigned int Result =
  8249. MoreMessageDialog(NoteData->Message, NULL, qtInformation,
  8250. qaOK | qaIgnore, HELP_COPY_PARAM_AUTOSELECTED, &Params);
  8251. switch (Result)
  8252. {
  8253. case qaNeverAskAgain:
  8254. DebugAssert(AllowNeverAskAgain);
  8255. WinConfiguration->CopyParamAutoSelectNotice = false;
  8256. break;
  8257. case qaIgnore:
  8258. PreferencesDialog(pmPresets);
  8259. break;
  8260. }
  8261. }
  8262. //---------------------------------------------------------------------------
  8263. void __fastcall TCustomScpExplorerForm::TransferPresetNoteClicked(TObject * Sender)
  8264. {
  8265. // as of now useless, as this is used for notes only, never for balloons, ...
  8266. RestoreApp();
  8267. // .. and we should never be busy here
  8268. if (DebugAlwaysTrue(!NonVisualDataModule->Busy))
  8269. {
  8270. TransferPresetNoteMessage(DebugNotNull(dynamic_cast<TTransferPresetNoteData *>(Sender)), false);
  8271. }
  8272. }
  8273. //---------------------------------------------------------------------------
  8274. void __fastcall TCustomScpExplorerForm::PreferencesDialog(
  8275. TPreferencesMode APreferencesMode)
  8276. {
  8277. std::unique_ptr<TPreferencesDialogData> PreferencesData;
  8278. TCopyParamRuleData Data;
  8279. if (Terminal != NULL)
  8280. {
  8281. PreferencesData.reset(new TPreferencesDialogData());
  8282. GetTransferPresetAutoSelectData(Data);
  8283. PreferencesData->CopyParamRuleData = &Data;
  8284. }
  8285. DoPreferencesDialog(APreferencesMode, PreferencesData.get());
  8286. }
  8287. //---------------------------------------------------------------------------
  8288. void __fastcall TCustomScpExplorerForm::AdHocCustomCommandValidate(
  8289. const TCustomCommandType & Command)
  8290. {
  8291. if (CustomCommandState(Command, FEditingFocusedAdHocCommand, ccltAll) <= 0)
  8292. {
  8293. throw Exception(FMTLOAD(CUSTOM_COMMAND_IMPOSSIBLE, (Command.Command)));
  8294. }
  8295. }
  8296. //---------------------------------------------------------------------------
  8297. void __fastcall TCustomScpExplorerForm::AdHocCustomCommand(bool OnFocused)
  8298. {
  8299. bool RemoteAllowed = CustomCommandRemoteAllowed();
  8300. TCustomCommandType Command;
  8301. // make sure we use local custom command when remote are not supported
  8302. if (RemoteAllowed || FLAGSET(FLastCustomCommand.Params, ccLocal))
  8303. {
  8304. Command = FLastCustomCommand;
  8305. }
  8306. else
  8307. {
  8308. Command.Params = Command.Params | ccLocal;
  8309. }
  8310. Command.Name = LoadStr(CUSTOM_COMMAND_AD_HOC_NAME);
  8311. FEditingFocusedAdHocCommand = OnFocused;
  8312. bool LocalSide = IsSideLocalBrowser(FCurrentSide);
  8313. int Options =
  8314. FLAGMASK((!RemoteAllowed || LocalSide), ccoDisableRemote) |
  8315. FLAGMASK(LocalSide, ccoDisableRemoteFiles);
  8316. if (DoCustomCommandDialog(Command, WinConfiguration->CustomCommandList,
  8317. ccmAdHoc, Options, AdHocCustomCommandValidate, NULL))
  8318. {
  8319. FLastCustomCommand = Command;
  8320. UpdateCustomCommandsToolbar();
  8321. ExecuteFileOperation(foCustomCommand, osRemote, OnFocused, false, &Command);
  8322. }
  8323. }
  8324. //---------------------------------------------------------------------------
  8325. void __fastcall TCustomScpExplorerForm::LastCustomCommand(bool OnFocused)
  8326. {
  8327. DebugAssert(!FLastCustomCommand.Command.IsEmpty());
  8328. int State = CustomCommandState(FLastCustomCommand, OnFocused, ccltAll);
  8329. DebugAssert(State > 0);
  8330. if (State <= 0)
  8331. {
  8332. throw Exception(FMTLOAD(CUSTOM_COMMAND_IMPOSSIBLE, (FLastCustomCommand.Command)));
  8333. }
  8334. ExecuteFileOperation(foCustomCommand, osRemote, OnFocused, false, &FLastCustomCommand);
  8335. }
  8336. //---------------------------------------------------------------------------
  8337. bool __fastcall TCustomScpExplorerForm::GetLastCustomCommand(bool OnFocused,
  8338. TCustomCommandType & Command, int & State)
  8339. {
  8340. bool Result = !FLastCustomCommand.Command.IsEmpty();
  8341. if (Result)
  8342. {
  8343. Command = FLastCustomCommand;
  8344. State = CustomCommandState(FLastCustomCommand, OnFocused, ccltAll);
  8345. }
  8346. return Result;
  8347. }
  8348. //---------------------------------------------------------------------------
  8349. void __fastcall TCustomScpExplorerForm::BeforeAction()
  8350. {
  8351. ResetIncrementalSearch();
  8352. }
  8353. //---------------------------------------------------------------------------
  8354. void __fastcall TCustomScpExplorerForm::PostComponentHide(Byte Component)
  8355. {
  8356. DebugAssert(ComponentVisible[Component]);
  8357. PostMessage(Handle, WM_COMPONENT_HIDE, Component, 0);
  8358. }
  8359. //---------------------------------------------------------------------------
  8360. void __fastcall TCustomScpExplorerForm::QueueSplitterDblClick(TObject * /*Sender*/)
  8361. {
  8362. // when queue panel is resized here directly, the status bar is stretched
  8363. // over whole space the panel occupied
  8364. PostComponentHide(fcQueueView);
  8365. }
  8366. //---------------------------------------------------------------------------
  8367. void __fastcall TCustomScpExplorerForm::ThemeChanged()
  8368. {
  8369. // We hoped this will refresh scrollbar colors, but it does not have any effect here.
  8370. RefreshColorMode();
  8371. WinConfiguration->ResetSysDarkTheme();
  8372. ConfigurationChanged();
  8373. ConfigureInterface();
  8374. // Should be called for all controls
  8375. RemoteDirView->Perform(WM_THEMECHANGED, 0, 0);
  8376. }
  8377. //---------------------------------------------------------------------------
  8378. void __fastcall TCustomScpExplorerForm::WMSettingChange(TMessage & Message)
  8379. {
  8380. // Do not handle, when shutting down anyway (maybe also when not setup completelly yet?)
  8381. if (!FInvalid &&
  8382. (Message.LParam != 0) &&
  8383. (wcscmp(reinterpret_cast<LPCWCH>(Message.LParam), L"ImmersiveColorSet") == 0))
  8384. {
  8385. ThemeChanged();
  8386. }
  8387. TForm::Dispatch(&Message);
  8388. }
  8389. //---------------------------------------------------------------------------
  8390. void __fastcall TCustomScpExplorerForm::Dispatch(void * Message)
  8391. {
  8392. TMessage * M = static_cast<TMessage*>(Message);
  8393. switch (M->Msg)
  8394. {
  8395. case CM_DIALOGCHAR:
  8396. CMDialogChar(*M);
  8397. break;
  8398. case WM_APPCOMMAND:
  8399. WMAppCommand(*M);
  8400. break;
  8401. case WM_SYSCOMMAND:
  8402. WMSysCommand(*M);
  8403. break;
  8404. case WM_QUERYENDSESSION:
  8405. WMQueryEndSession(*M);
  8406. break;
  8407. case WM_ENDSESSION:
  8408. WMEndSession(*reinterpret_cast<TWMEndSession *>(M));
  8409. break;
  8410. #ifdef _DEBUG
  8411. case WM_WINDOWPOSCHANGED:
  8412. WMWindowPosChanged(*reinterpret_cast<TWMWindowPosMsg *>(M));
  8413. break;
  8414. case CM_CUSTOMSTYLECHANGED:
  8415. TForm::Dispatch(Message);
  8416. break;
  8417. #endif
  8418. case WM_COMPONENT_HIDE:
  8419. {
  8420. Byte Component = static_cast<Byte>(M->WParam);
  8421. // sanity check
  8422. if (ComponentVisible[Component])
  8423. {
  8424. // special treatment
  8425. if (Component == fcQueueView)
  8426. {
  8427. ToggleQueueVisibility();
  8428. DebugAssert(!ComponentVisible[fcQueueView]);
  8429. }
  8430. else
  8431. {
  8432. ComponentVisible[Component] = false;
  8433. }
  8434. }
  8435. }
  8436. break;
  8437. case WM_COPYDATA:
  8438. WMCopyData(*M);
  8439. break;
  8440. case WM_MANAGES_CAPTION:
  8441. // caption managed in TTerminalManager::UpdateAppTitle()
  8442. M->Result = 1;
  8443. break;
  8444. case WM_WANTS_MOUSEWHEEL:
  8445. M->Result = 1;
  8446. break;
  8447. case CM_SHOWINGCHANGED:
  8448. CMShowingChanged(*M);
  8449. break;
  8450. case WM_CLOSE:
  8451. WMClose(*M);
  8452. break;
  8453. case WM_DPICHANGED:
  8454. WMDpiChanged(*M);
  8455. break;
  8456. case CM_DPICHANGED:
  8457. CMDpiChanged(*M);
  8458. break;
  8459. case WM_SETTINGCHANGE:
  8460. WMSettingChange(*M);
  8461. break;
  8462. case CM_DIALOGKEY:
  8463. CMDialogKey(*reinterpret_cast<TWMKeyDown *>(M));
  8464. break;
  8465. default:
  8466. TForm::Dispatch(Message);
  8467. break;
  8468. }
  8469. }
  8470. //---------------------------------------------------------------------------
  8471. void __fastcall TCustomScpExplorerForm::UpdateImages()
  8472. {
  8473. // noop
  8474. }
  8475. //---------------------------------------------------------------------------
  8476. void __fastcall TCustomScpExplorerForm::CMDpiChanged(TMessage & Message)
  8477. {
  8478. TForm::Dispatch(&Message);
  8479. FSessionColors->Clear();
  8480. AddFixedSessionImages();
  8481. RegenerateSessionColorsImageList(FSessionColors, FSessionColorMaskImageIndex);
  8482. UpdateImages();
  8483. }
  8484. //---------------------------------------------------------------------------
  8485. void __fastcall TCustomScpExplorerForm::WMDpiChanged(TMessage & Message)
  8486. {
  8487. TForm::Dispatch(&Message);
  8488. Configuration->Usage->Inc(L"PixelsPerInchChanged");
  8489. UpdatePixelsPerInchMainWindowCounter();
  8490. }
  8491. //---------------------------------------------------------------------------
  8492. void __fastcall TCustomScpExplorerForm::WMClose(TMessage & Message)
  8493. {
  8494. // Cannot close window while we are busy.
  8495. // We cannot test this in FormCloseQuery as that is called also from
  8496. // Close(), which is called by CloseApplicationAction. So we can be busy
  8497. // there already even, when it is legitimate to close the application.
  8498. // Possibly a better place to handle this would be WMSysCommand.
  8499. if (NonVisualDataModule->Busy)
  8500. {
  8501. Message.Result = 1;
  8502. }
  8503. else
  8504. {
  8505. TForm::Dispatch(&Message);
  8506. }
  8507. }
  8508. //---------------------------------------------------------------------------
  8509. void __fastcall TCustomScpExplorerForm::CMShowingChanged(TMessage & Message)
  8510. {
  8511. TForm::Dispatch(&Message);
  8512. if (Showing && (Terminal == NULL))
  8513. {
  8514. // When we are starting minimized (i.e. from an installer),
  8515. // postpone showing Login dialog until we get restored.
  8516. // Otherwise the Login dialog (and Authentication window) show restored
  8517. // over invisible (minimized) main window.
  8518. if (WindowState == wsMinimized)
  8519. {
  8520. FNeedSession = true;
  8521. }
  8522. else
  8523. {
  8524. // This happens before application ever goes idle, so the toolbars would
  8525. // stay enabled (initial state) until the Login dialog is dismissed.
  8526. UpdateActions();
  8527. NonVisualDataModule->StartBusy();
  8528. try
  8529. {
  8530. // Need to process WM_ACTIVATEAPP before showing the Login dialog,
  8531. // otherwise the dialog does not receive focus.
  8532. // With Commander interface the ProcessMessages is called already
  8533. // by TDriveView, but with Explorer interface, we need to call it explicily
  8534. Application->ProcessMessages();
  8535. // do not reload sessions, they have been loaded just now (optimization)
  8536. NeedSession(true);
  8537. }
  8538. __finally
  8539. {
  8540. NonVisualDataModule->EndBusy();
  8541. }
  8542. }
  8543. }
  8544. }
  8545. //---------------------------------------------------------------------------
  8546. void __fastcall TCustomScpExplorerForm::CenterReconnectToolbar()
  8547. {
  8548. ReconnectToolbar->Left = (ReconnectToolbar->Parent->ClientWidth - ReconnectToolbar->Width) / 2;
  8549. ReconnectToolbar->Top = (ReconnectToolbar->Parent->ClientHeight - ReconnectToolbar->Height) / 2;
  8550. }
  8551. //---------------------------------------------------------------------------
  8552. void __fastcall TCustomScpExplorerForm::FormConstrainedResize(
  8553. TObject * /*Sender*/, int & MinWidth, int & MinHeight, int & MaxWidth,
  8554. int & MaxHeight)
  8555. {
  8556. // workaround for bug in TWinControl.CalcConstraints
  8557. // Check for empty rect (restore from iconic state) is done there only after
  8558. // call to AdjustClientRect, which enlarges the rect (for forms).
  8559. TRect R = GetClientRect();
  8560. // when restoring from iconic state, place no restrictions
  8561. if (IsRectEmpty(R))
  8562. {
  8563. MinWidth = 0;
  8564. MinHeight = 0;
  8565. MaxWidth = 32000;
  8566. MaxHeight = 32000;
  8567. }
  8568. }
  8569. //---------------------------------------------------------------------------
  8570. void __fastcall TCustomScpExplorerForm::GetSpaceAvailable(const UnicodeString Path,
  8571. TSpaceAvailable & ASpaceAvailable, bool & Close)
  8572. {
  8573. // terminal can be already closed (e.g. dropped connection)
  8574. if ((Terminal != NULL) && Terminal->IsCapable[fcCheckingSpaceAvailable])
  8575. {
  8576. Configuration->Usage->Inc(L"SpaceAvailableChecks");
  8577. try
  8578. {
  8579. Terminal->SpaceAvailable(Path, ASpaceAvailable);
  8580. }
  8581. catch(...)
  8582. {
  8583. if (!Terminal->Active)
  8584. {
  8585. Close = true;
  8586. }
  8587. throw;
  8588. }
  8589. }
  8590. }
  8591. //---------------------------------------------------------------------------
  8592. void __fastcall TCustomScpExplorerForm::FileSystemInfo()
  8593. {
  8594. const TSessionInfo & SessionInfo = Terminal->GetSessionInfo();
  8595. const TFileSystemInfo & FileSystemInfo = Terminal->GetFileSystemInfo(true);
  8596. TGetSpaceAvailable OnGetSpaceAvailable = NULL;
  8597. if (Terminal->IsCapable[fcCheckingSpaceAvailable])
  8598. {
  8599. OnGetSpaceAvailable = GetSpaceAvailable;
  8600. }
  8601. DoFileSystemInfoDialog(SessionInfo, FileSystemInfo, Terminal->CurrentDirectory,
  8602. OnGetSpaceAvailable);
  8603. }
  8604. //---------------------------------------------------------------------------
  8605. TSessionData * __fastcall TCustomScpExplorerForm::SessionDataForCode()
  8606. {
  8607. std::unique_ptr<TSessionData> Data(CloneCurrentSessionData());
  8608. Terminal->FillSessionDataForCode(Data.get());
  8609. return Data.release();
  8610. }
  8611. //---------------------------------------------------------------------------
  8612. void __fastcall TCustomScpExplorerForm::GenerateUrl(TStrings * Paths)
  8613. {
  8614. std::unique_ptr<TSessionData> Data(SessionDataForCode());
  8615. DoGenerateUrlDialog(Data.get(), Paths);
  8616. }
  8617. //---------------------------------------------------------------------------
  8618. void __fastcall TCustomScpExplorerForm::SessionGenerateUrl()
  8619. {
  8620. GenerateUrl(NULL);
  8621. }
  8622. //---------------------------------------------------------------------------
  8623. void __fastcall TCustomScpExplorerForm::FileGenerateUrl()
  8624. {
  8625. std::unique_ptr<TStrings> Paths(new TStringList());
  8626. DirView(osCurrent)->CreateFileList(false, true, Paths.get());
  8627. GenerateUrl(Paths.get());
  8628. }
  8629. //---------------------------------------------------------------------------
  8630. void __fastcall TCustomScpExplorerForm::UpdateSessionColor(TColor value)
  8631. {
  8632. FSessionColor = value;
  8633. TColor C = (value != 0 ? value : Vcl::Graphics::clNone);
  8634. TTBXColorItem * ColorItem = dynamic_cast<TTBXColorItem *>(
  8635. static_cast<TObject *>(GetComponent(fcColorMenu)));
  8636. DebugAssert(ColorItem != NULL);
  8637. ColorItem->Color = C;
  8638. NonVisualDataModule->ColorMenuItem->Color = C;
  8639. UpdateControls();
  8640. }
  8641. //---------------------------------------------------------------------------
  8642. void __fastcall TCustomScpExplorerForm::SetSessionColor(TColor value)
  8643. {
  8644. if (value != FSessionColor)
  8645. {
  8646. UpdateSessionColor(value);
  8647. // Is null when called from LastTerminalClosed
  8648. if (Terminal != NULL)
  8649. {
  8650. SessionsPageControl->ActivePage->ImageIndex = AddSessionColor(value);
  8651. }
  8652. }
  8653. }
  8654. //---------------------------------------------------------------------------
  8655. bool __fastcall TCustomScpExplorerForm::CancelNote(bool Force)
  8656. {
  8657. bool Result = FNoteTimer->Enabled;
  8658. if (Result)
  8659. {
  8660. // cannot cancel note too early
  8661. bool NotEarly =
  8662. (Now() - FNoteShown >
  8663. EncodeTimeVerbose(0, 0, (unsigned short)(WinConfiguration->NotificationsStickTime), 0));
  8664. if (Force || NotEarly)
  8665. {
  8666. FNoteTimer->Enabled = false;
  8667. FNote = L"";
  8668. SAFE_DESTROY(FNoteData);
  8669. FOnNoteClick = NULL;
  8670. FNoteHints = FNotes->Text;
  8671. FNoteHints.Delete(FNoteHints.Length() - 1, 2);
  8672. UpdateStatusBar();
  8673. }
  8674. }
  8675. return Result;
  8676. }
  8677. //---------------------------------------------------------------------------
  8678. void __fastcall TCustomScpExplorerForm::NoteTimer(TObject * /*Sender*/)
  8679. {
  8680. DebugAssert(FNoteTimer->Enabled);
  8681. CancelNote(true);
  8682. }
  8683. //---------------------------------------------------------------------------
  8684. void __fastcall TCustomScpExplorerForm::AddNote(UnicodeString Note, bool UpdateNow)
  8685. {
  8686. int P = Note.Pos(L"\n");
  8687. if (P > 0)
  8688. {
  8689. Note.SetLength(P - 1);
  8690. }
  8691. FNotes->Add(FORMAT(L"[%s] %s",
  8692. (FormatDateTime(Configuration->TimeFormat, Now()), Note)));
  8693. while (FNotes->Count > 10)
  8694. {
  8695. FNotes->Delete(0);
  8696. }
  8697. if (UpdateNow)
  8698. {
  8699. FNoteHints = FNotes->Text;
  8700. FNoteHints.Delete(FNoteHints.Length() - 1, 2);
  8701. UpdateStatusBar();
  8702. }
  8703. }
  8704. //---------------------------------------------------------------------------
  8705. void __fastcall TCustomScpExplorerForm::PostNote(UnicodeString Note,
  8706. unsigned int Seconds, TNotifyEvent OnNoteClick, TObject * NoteData)
  8707. {
  8708. int P = Note.Pos(L"\n");
  8709. if (P > 0)
  8710. {
  8711. Note.SetLength(P - 1);
  8712. }
  8713. FNoteHints = FNotes->Text;
  8714. FNoteHints.Delete(FNoteHints.Length() - 1, 2);
  8715. FNote = Note;
  8716. SAFE_DESTROY(FNoteData);
  8717. FNoteData = NoteData;
  8718. FOnNoteClick = OnNoteClick;
  8719. AddNote(Note, false);
  8720. UpdateStatusBar();
  8721. FNoteShown = Now();
  8722. FNoteTimer->Enabled = false;
  8723. if (Seconds == 0)
  8724. {
  8725. Seconds = WinConfiguration->NotificationsTimeout;
  8726. }
  8727. FNoteTimer->Interval = Seconds * MSecsPerSec;
  8728. FNoteTimer->Enabled = true;
  8729. }
  8730. //---------------------------------------------------------------------------
  8731. void __fastcall TCustomScpExplorerForm::ReadDirectoryCancelled()
  8732. {
  8733. PostNote(LoadStr(DIRECTORY_READING_CANCELLED), 0, NULL, NULL);
  8734. }
  8735. //---------------------------------------------------------------------------
  8736. void __fastcall TCustomScpExplorerForm::SynchronizeBrowsingChanged()
  8737. {
  8738. if (NonVisualDataModule->SynchronizeBrowsingAction->Checked)
  8739. {
  8740. Configuration->Usage->Inc(L"SynchronizeBrowsingEnabled");
  8741. }
  8742. PostNote(FORMAT(LoadStrPart(SYNC_DIR_BROWSE_TOGGLE, 1),
  8743. (LoadStrPart(SYNC_DIR_BROWSE_TOGGLE,
  8744. (NonVisualDataModule->SynchronizeBrowsingAction->Checked ? 2 : 3)))),
  8745. 0, NULL, NULL);
  8746. }
  8747. //---------------------------------------------------------------------------
  8748. void __fastcall TCustomScpExplorerForm::ToggleShowHiddenFiles()
  8749. {
  8750. WinConfiguration->ShowHiddenFiles = !WinConfiguration->ShowHiddenFiles;
  8751. PostNote(FORMAT(LoadStrPart(SHOW_HIDDEN_FILES_TOGGLE, 1),
  8752. (LoadStrPart(SHOW_HIDDEN_FILES_TOGGLE,
  8753. (WinConfiguration->ShowHiddenFiles ? 2 : 3)))), 0, NULL, NULL);
  8754. }
  8755. //---------------------------------------------------------------------------
  8756. void __fastcall TCustomScpExplorerForm::SetFormatSizeBytes(TFormatBytesStyle Style)
  8757. {
  8758. WinConfiguration->FormatSizeBytes = Style;
  8759. }
  8760. //---------------------------------------------------------------------------
  8761. void __fastcall TCustomScpExplorerForm::ToggleAutoReadDirectoryAfterOp()
  8762. {
  8763. Configuration->AutoReadDirectoryAfterOp = !Configuration->AutoReadDirectoryAfterOp;
  8764. PostNote(FORMAT(LoadStrPart(AUTO_READ_DIRECTORY_TOGGLE, 1),
  8765. (LoadStrPart(AUTO_READ_DIRECTORY_TOGGLE,
  8766. (Configuration->AutoReadDirectoryAfterOp ? 2 : 3)))), 0, NULL, NULL);
  8767. }
  8768. //---------------------------------------------------------------------------
  8769. void __fastcall TCustomScpExplorerForm::StatusBarPanelDblClick(
  8770. TTBXCustomStatusBar * Sender, TTBXStatusPanel * Panel)
  8771. {
  8772. if (Panel->Index == 0)
  8773. {
  8774. if (FOnNoteClick != NULL)
  8775. {
  8776. // prevent the user data from being freed by possible call
  8777. // to CancelNote or PostNote during call to OnNoteClick
  8778. std::unique_ptr<TObject> NoteData(FNoteData);
  8779. TNotifyEvent OnNoteClick = FOnNoteClick;
  8780. FNoteData = NULL;
  8781. // need to cancel the note as we are going to delete its user data
  8782. CancelNote(true);
  8783. OnNoteClick(NoteData.get());
  8784. }
  8785. }
  8786. if (Panel->Index >= Sender->Panels->Count - SessionPanelCount)
  8787. {
  8788. FileSystemInfo();
  8789. }
  8790. }
  8791. //---------------------------------------------------------------------------
  8792. void __fastcall TCustomScpExplorerForm::LockWindow(bool Force)
  8793. {
  8794. // workaround:
  8795. // 1) for unknown reason, disabling window, while minimized,
  8796. // prevents it from restoring, even if it was enabled again meanwhile
  8797. // 2) when disabling the main window, while another has focus
  8798. // minimize is no longer possible
  8799. // ("keep up to date" dialog - though does not seem to be the case with "find file" -
  8800. // so it has probably something to do with the semi-modalness of the "keep up to date" dialog)
  8801. // Shouldn't we use IsApplicationMinimized() here?
  8802. if ((FLockSuspendLevel == 0) && !IsIconic(Application->Handle) && (Force || (Screen->ActiveForm == this)))
  8803. {
  8804. Enabled = false;
  8805. }
  8806. FLockLevel++;
  8807. }
  8808. //---------------------------------------------------------------------------
  8809. void __fastcall TCustomScpExplorerForm::UnlockWindow()
  8810. {
  8811. DebugAssert(FLockLevel > 0);
  8812. FLockLevel--;
  8813. if (FLockLevel == 0)
  8814. {
  8815. DebugAssert(FLockSuspendLevel == 0);
  8816. Enabled = true;
  8817. }
  8818. }
  8819. //---------------------------------------------------------------------------
  8820. void __fastcall TCustomScpExplorerForm::SuspendWindowLock()
  8821. {
  8822. // We need to make sure that window is enabled when the last modal window closes
  8823. // otherwise focus is not restored correctly.
  8824. // So we re-enable the window when modal window opens and
  8825. // disable it back after is closes
  8826. if (FLockLevel > 0)
  8827. {
  8828. // while we have nesting counter, we know that we never be called
  8829. // recursivelly as Application->OnModalBegin is called only
  8830. // for the top-level modal window
  8831. if (DebugAlwaysTrue(FLockSuspendLevel == 0))
  8832. {
  8833. // won't be disabled when conditions in LockWindow() were not satisfied
  8834. FDisabledOnLockSuspend = !Enabled;
  8835. // When minimized to tray (or actually when set to SW_HIDE),
  8836. // setting Enabled makes the window focusable even when there's
  8837. // modal window over it
  8838. if (!FTrayIcon->Visible)
  8839. {
  8840. Enabled = true;
  8841. }
  8842. }
  8843. FLockSuspendLevel++;
  8844. }
  8845. }
  8846. //---------------------------------------------------------------------------
  8847. void __fastcall TCustomScpExplorerForm::ResumeWindowLock()
  8848. {
  8849. if (FLockSuspendLevel > 0)
  8850. {
  8851. DebugAssert(FLockLevel > 0);
  8852. FLockSuspendLevel--;
  8853. // see comment in SuspendWindowLock
  8854. if (DebugAlwaysTrue(FLockSuspendLevel == 0))
  8855. {
  8856. // Note that window can be enabled here, when we were minized to tray when
  8857. // was SuspendWindowLock() called.
  8858. // We should possibly do the same check as in LockWindow(),
  8859. // if it is ever possible that the consitions change between
  8860. // SuspendWindowLock() and ResumeWindowLock()
  8861. Enabled = !FDisabledOnLockSuspend;
  8862. }
  8863. }
  8864. }
  8865. //---------------------------------------------------------------------------
  8866. void __fastcall TCustomScpExplorerForm::UpdateRemotePathComboBox(bool TextOnly)
  8867. {
  8868. if (!TextOnly)
  8869. {
  8870. TTBXComboBoxItem * RemotePathComboBox =
  8871. reinterpret_cast<TTBXComboBoxItem *>(GetComponent(fcRemotePathComboBox));
  8872. TStrings * Items = RemotePathComboBox->Strings;
  8873. Items->BeginUpdate();
  8874. try
  8875. {
  8876. Items->Clear();
  8877. if ((Terminal != NULL) && !RemoteDirView->Path.IsEmpty())
  8878. {
  8879. UnicodeString APath = UnixExcludeTrailingBackslash(RemoteDirView->Path);
  8880. while (!IsUnixRootPath(APath))
  8881. {
  8882. int P = APath.LastDelimiter(L'/');
  8883. DebugAssert(P >= 0);
  8884. Items->Insert(0, APath.SubString(P + 1, APath.Length() - P));
  8885. APath.SetLength(P - 1);
  8886. }
  8887. Items->Insert(0, Customunixdirview_SUnixDefaultRootName);
  8888. }
  8889. }
  8890. __finally
  8891. {
  8892. RemotePathComboBox->ItemIndex = Items->Count - 1;
  8893. // Setting ItemIndex to -1 does not reset its text
  8894. if (Items->Count == 0)
  8895. {
  8896. RemotePathComboBox->Text = L"";
  8897. }
  8898. Items->EndUpdate();
  8899. }
  8900. }
  8901. }
  8902. //---------------------------------------------------------------------------
  8903. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxAdjustImageIndex(
  8904. TTBXComboBoxItem * Sender, const UnicodeString /*AText*/, int AIndex,
  8905. int & ImageIndex)
  8906. {
  8907. if (AIndex < 0)
  8908. {
  8909. AIndex = Sender->ItemIndex;
  8910. }
  8911. ImageIndex = (AIndex < Sender->Strings->Count - 1 ? StdDirIcon : StdDirSelIcon);
  8912. }
  8913. //---------------------------------------------------------------------------
  8914. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxDrawItem(
  8915. TTBXCustomList * /*Sender*/, TCanvas * /*ACanvas*/, TRect & ARect, int AIndex,
  8916. int /*AHoverIndex*/, bool & /*DrawDefault*/)
  8917. {
  8918. ARect.Left += (10 * AIndex);
  8919. }
  8920. //---------------------------------------------------------------------------
  8921. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxMeasureWidth(
  8922. TTBXCustomList * /*Sender*/, TCanvas * /*ACanvas*/, int AIndex, int &AWidth)
  8923. {
  8924. AWidth += (10 * AIndex);
  8925. }
  8926. //---------------------------------------------------------------------------
  8927. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxItemClick(
  8928. TObject * Sender)
  8929. {
  8930. TTBXComboBoxItem * RemotePathComboBox = dynamic_cast<TTBXComboBoxItem*>(Sender);
  8931. UnicodeString APath = UnixExcludeTrailingBackslash(RemoteDirView->Path);
  8932. int Index = RemotePathComboBox->ItemIndex;
  8933. while (Index < RemotePathComboBox->Strings->Count - 1)
  8934. {
  8935. APath = UnixExtractFileDir(APath);
  8936. Index++;
  8937. }
  8938. // VanDyke style paths
  8939. if (APath.IsEmpty())
  8940. {
  8941. DebugAssert(RemotePathComboBox->ItemIndex == 0);
  8942. APath = ROOTDIRECTORY;
  8943. }
  8944. if (RemoteDirView->Path != APath)
  8945. {
  8946. RemoteDirView->Path = APath;
  8947. }
  8948. }
  8949. //---------------------------------------------------------------------------
  8950. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxCancel(TObject * Sender)
  8951. {
  8952. DebugAssert(Sender == GetComponent(fcRemotePathComboBox));
  8953. DebugUsedParam(Sender);
  8954. UpdateRemotePathComboBox(true);
  8955. }
  8956. //---------------------------------------------------------------------------
  8957. void __fastcall TCustomScpExplorerForm::DirViewEditing(
  8958. TObject * Sender, TListItem * Item, bool & /*AllowEdit*/)
  8959. {
  8960. ResetIncrementalSearch();
  8961. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  8962. DebugAssert(DirView != NULL);
  8963. if (!WinConfiguration->RenameWholeName && !DirView->ItemIsDirectory(Item))
  8964. {
  8965. HWND Edit = ListView_GetEditControl(DirView->Handle);
  8966. // OnEditing is called also from TCustomListView::CanEdit
  8967. if (Edit != NULL)
  8968. {
  8969. EditSelectBaseName(Edit);
  8970. }
  8971. }
  8972. }
  8973. //---------------------------------------------------------------------------
  8974. //---------------------------------------------------------------------------
  8975. TDragDropFilesEx * __fastcall TCustomScpExplorerForm::CreateDragDropFilesEx()
  8976. {
  8977. TDragDropFilesEx * Result = new TDragDropFilesEx(this);
  8978. Result->AutoDetectDnD = false;
  8979. Result->NeedValid = TFileExMustDnDSet() << nvFilename;
  8980. Result->RenderDataOn = rdoEnterAndDropSync;
  8981. Result->TargetEffects = TDropEffectSet() << deCopy << deMove;
  8982. return Result;
  8983. }
  8984. //---------------------------------------------------------------------------
  8985. void __fastcall TCustomScpExplorerForm::CreateWnd()
  8986. {
  8987. TForm::CreateWnd();
  8988. // win32-darkmode calls AllowDarkModeForWindow(this, true) here, but it does not seem to have any effect
  8989. if (FSessionsDragDropFilesEx == NULL)
  8990. {
  8991. FSessionsDragDropFilesEx = CreateDragDropFilesEx();
  8992. FSessionsDragDropFilesEx->OnDragOver = SessionsDDDragOver;
  8993. FSessionsDragDropFilesEx->OnProcessDropped = SessionsDDProcessDropped;
  8994. FSessionsDragDropFilesEx->OnDragEnter = SessionsDDDragEnter;
  8995. FSessionsDragDropFilesEx->OnDragLeave = SessionsDDDragLeave;
  8996. }
  8997. if (FQueueDragDropFilesEx == NULL)
  8998. {
  8999. FQueueDragDropFilesEx = CreateDragDropFilesEx();
  9000. // No need to set OnDragOver as we do not have any restrictions
  9001. FQueueDragDropFilesEx->OnProcessDropped = QueueDDProcessDropped;
  9002. FQueueDragDropFilesEx->OnDragEnter = QueueDDDragEnter;
  9003. FQueueDragDropFilesEx->OnDragLeave = QueueDDDragLeave;
  9004. }
  9005. }
  9006. //---------------------------------------------------------------------------
  9007. void __fastcall TCustomScpExplorerForm::DestroyWnd()
  9008. {
  9009. TForm::DestroyWnd();
  9010. FSessionsDragDropFilesEx->DragDropControl = NULL;
  9011. FQueueDragDropFilesEx->DragDropControl = NULL;
  9012. }
  9013. //---------------------------------------------------------------------------
  9014. void __fastcall TCustomScpExplorerForm::FormShow(TObject * /*Sender*/)
  9015. {
  9016. SideEnter(FCurrentSide);
  9017. FEverShown = true;
  9018. }
  9019. //---------------------------------------------------------------------------
  9020. void __fastcall TCustomScpExplorerForm::DoFindFiles(
  9021. TTerminal * ATerminal, UnicodeString Directory, const TFileMasks & FileMask,
  9022. TFileFoundEvent OnFileFound, TFindingFileEvent OnFindingFile)
  9023. {
  9024. if (!NonVisualDataModule->Busy)
  9025. {
  9026. TTerminalManager::Instance()->ActiveTerminal = DebugNotNull(dynamic_cast<TManagedTerminal *>(ATerminal));
  9027. Configuration->Usage->Inc(L"FileFinds");
  9028. LockWindow(true);
  9029. NonVisualDataModule->StartBusy();
  9030. WinConfiguration->LockedInterface = true;
  9031. try
  9032. {
  9033. FTerminal->FilesFind(Directory, FileMask, OnFileFound, OnFindingFile);
  9034. }
  9035. __finally
  9036. {
  9037. WinConfiguration->LockedInterface = false;
  9038. NonVisualDataModule->EndBusy();
  9039. UnlockWindow();
  9040. }
  9041. }
  9042. }
  9043. //---------------------------------------------------------------------------
  9044. void __fastcall TCustomScpExplorerForm::DoFocusRemotePath(TTerminal * ATerminal, const UnicodeString & Path)
  9045. {
  9046. if (!NonVisualDataModule->Busy)
  9047. {
  9048. TTerminalManager::Instance()->ActiveTerminal = DebugNotNull(dynamic_cast<TManagedTerminal *>(ATerminal));
  9049. SetFocus();
  9050. RemoteDirView->Path = UnixExtractFilePath(Path);
  9051. UnicodeString FileName = UnixExtractFileName(Path);
  9052. TListItem * Item = RemoteDirView->FindFileItem(FileName);
  9053. // If not found, it can because we have loaded directory from cache and the file is new, try again after reload.
  9054. // (tbd: we should not even try this, if we have not loaded the directory from the cache)
  9055. if (Item == NULL)
  9056. {
  9057. ReloadDirectory(osRemote);
  9058. Item = RemoteDirView->FindFileItem(FileName);
  9059. }
  9060. if (Item != NULL)
  9061. {
  9062. RemoteDirView->ItemFocused = Item;
  9063. RemoteDirView->ItemFocused->MakeVisible(false);
  9064. RemoteDirView->SetFocus();
  9065. }
  9066. }
  9067. }
  9068. //---------------------------------------------------------------------------
  9069. bool __fastcall TCustomScpExplorerForm::CanOperateOnFoundFiles(TTerminal * ATerminal)
  9070. {
  9071. bool Result = !NonVisualDataModule->Busy;
  9072. if (Result)
  9073. {
  9074. TTerminalManager::Instance()->ActiveTerminal = DebugNotNull(dynamic_cast<TManagedTerminal *>(ATerminal));
  9075. }
  9076. return Result;
  9077. }
  9078. //---------------------------------------------------------------------------
  9079. void __fastcall TCustomScpExplorerForm::DoOperationOnFoundFiles(
  9080. TFileOperation Operation, TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  9081. {
  9082. if (CanOperateOnFoundFiles(ATerminal))
  9083. {
  9084. TValueRestorer<TFileOperationFinishedEvent> OnFileOperationFinishedRestorer(FOnFileOperationFinished);
  9085. FOnFileOperationFinished = OnFileOperationFinished;
  9086. ExecuteFileOperation(Operation, osRemote, FileList, false, NULL);
  9087. }
  9088. }
  9089. //---------------------------------------------------------------------------
  9090. void __fastcall TCustomScpExplorerForm::DoDeleteFoundFiles(
  9091. TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  9092. {
  9093. DoOperationOnFoundFiles(foDelete, ATerminal, FileList, OnFileOperationFinished);
  9094. }
  9095. //---------------------------------------------------------------------------
  9096. void __fastcall TCustomScpExplorerForm::DoDownloadFoundFiles(
  9097. TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  9098. {
  9099. DoOperationOnFoundFiles(foCopy, ATerminal, FileList, OnFileOperationFinished);
  9100. }
  9101. //---------------------------------------------------------------------------
  9102. void __fastcall TCustomScpExplorerForm::DoEditFoundFiles(
  9103. TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  9104. {
  9105. if (CanOperateOnFoundFiles(ATerminal))
  9106. {
  9107. for (int Index = 0; Index < FileList->Count; Index++)
  9108. {
  9109. UnicodeString FileName = FileList->Strings[Index];
  9110. TRemoteFile * File = static_cast<TRemoteFile *>(FileList->Objects[Index]);
  9111. ExecuteRemoteFile(FileName, File, efDefaultEditor);
  9112. OnFileOperationFinished(FileName, true);
  9113. }
  9114. }
  9115. }
  9116. //---------------------------------------------------------------------------
  9117. void __fastcall TCustomScpExplorerForm::RemoteFindFiles()
  9118. {
  9119. FFileFindTerminal = Terminal;
  9120. ShowFileFindDialog(
  9121. Terminal, RemoteDirView->Path, DoFindFiles, DoFocusRemotePath, DoDeleteFoundFiles,
  9122. DoDownloadFoundFiles, DoEditFoundFiles);
  9123. }
  9124. //---------------------------------------------------------------------------
  9125. void __fastcall TCustomScpExplorerForm::UpdateTaskbarList(ITaskbarList3 * TaskbarList)
  9126. {
  9127. FTaskbarList = TaskbarList;
  9128. }
  9129. //---------------------------------------------------------------------------
  9130. void __fastcall TCustomScpExplorerForm::SessionsPageControlMouseDown(
  9131. TObject * /*Sender*/, TMouseButton Button, TShiftState /*Shift*/, int X, int Y)
  9132. {
  9133. int Index = SessionsPageControl->IndexOfTabAt(X, Y);
  9134. if (Index >= 0)
  9135. {
  9136. if (Button == mbLeft)
  9137. {
  9138. // "Mouse down" is raised only after tab is switched.
  9139. // If switching tab (switching session) takes long enough for user
  9140. // to actually release the button, "mouse down" is still raised,
  9141. // but we do not get "mouse up" event, so dragging is not cancelled,
  9142. // prevent that by not beginning dragging in the first place.
  9143. if (IsKeyPressed(VK_LBUTTON))
  9144. {
  9145. // when user clicks the "+", we get mouse down only after the session
  9146. // is closed, when new session tab is already on X:Y, so dragging
  9147. // starts, prevent that
  9148. if (MilliSecondsBetween(Now(), FSessionsPageControlNewSessionTime) > 500)
  9149. {
  9150. TTerminal * Terminal = GetSessionTabTerminal(SessionsPageControl->Pages[Index]);
  9151. if (Terminal != NULL)
  9152. {
  9153. SessionsPageControl->BeginDrag(false);
  9154. }
  9155. }
  9156. }
  9157. }
  9158. else if (Button == mbMiddle)
  9159. {
  9160. // ignore middle-click for "New session tab"
  9161. TTerminal * Terminal = GetSessionTabTerminal(SessionsPageControl->Pages[Index]);
  9162. if (Terminal != NULL)
  9163. {
  9164. CloseSessionTab(Index);
  9165. }
  9166. }
  9167. }
  9168. }
  9169. //---------------------------------------------------------------------------
  9170. void __fastcall TCustomScpExplorerForm::SessionsPageControlDragDrop(
  9171. TObject * /*Sender*/, TObject * /*Source*/, int X, int Y)
  9172. {
  9173. int Index = SessionsPageControl->IndexOfTabAt(X, Y);
  9174. // do not allow dropping on the "+" tab
  9175. TTerminal * TargetTerminal = GetSessionTabTerminal(SessionsPageControl->Pages[Index]);
  9176. if ((TargetTerminal != NULL) &&
  9177. (SessionsPageControl->ActivePage->PageIndex != Index))
  9178. {
  9179. Configuration->Usage->Inc(L"SessionTabMoves");
  9180. // this is almost redundant as we would recreate tabs in DoTerminalListChanged,
  9181. // but we want to actually prevent that to avoid flicker
  9182. SessionsPageControl->ActivePage->PageIndex = Index;
  9183. TTerminal * Terminal = GetSessionTabTerminal(SessionsPageControl->ActivePage);
  9184. TTerminalManager::Instance()->Move(Terminal, TargetTerminal);
  9185. }
  9186. }
  9187. //---------------------------------------------------------------------------
  9188. void __fastcall TCustomScpExplorerForm::SessionsPageControlDragOver(
  9189. TObject * Sender, TObject * Source, int X, int Y,
  9190. TDragState /*State*/, bool & Accept)
  9191. {
  9192. Accept = (Sender == Source);
  9193. if (Accept)
  9194. {
  9195. int Index = SessionsPageControl->IndexOfTabAt(X, Y);
  9196. TTerminal * Terminal = GetSessionTabTerminal(SessionsPageControl->Pages[Index]);
  9197. // do not allow dragging to the "+" tab
  9198. Accept = (Terminal != NULL);
  9199. }
  9200. }
  9201. //---------------------------------------------------------------------------
  9202. void __fastcall TCustomScpExplorerForm::SessionsDDDragOver(
  9203. int DebugUsedArg(KeyState), const TPoint & Point, int & Effect, int DebugUsedArg(PreferredEffect))
  9204. {
  9205. int Index = SessionsPageControl->IndexOfTabAt(Point.X, Point.Y);
  9206. if (Index < 0)
  9207. {
  9208. Effect = DROPEFFECT_NONE;
  9209. }
  9210. else
  9211. {
  9212. TTerminal * TargetTerminal = GetSessionTabTerminal(SessionsPageControl->Pages[Index]);
  9213. // do not allow dropping on the "+" tab
  9214. if (TargetTerminal == NULL)
  9215. {
  9216. Effect = DROPEFFECT_NONE;
  9217. }
  9218. else if ((TargetTerminal != Terminal) && (Effect == DROPEFFECT_MOVE))
  9219. {
  9220. Effect = DROPEFFECT_COPY;
  9221. }
  9222. }
  9223. }
  9224. //---------------------------------------------------------------------------
  9225. void __fastcall TCustomScpExplorerForm::SessionsDDProcessDropped(
  9226. TObject * /*Sender*/, int /*KeyState*/, const TPoint & Point, int Effect)
  9227. {
  9228. int Index = SessionsPageControl->IndexOfTabAt(Point.X, Point.Y);
  9229. // do not allow dropping on the "+" tab
  9230. TManagedTerminal * TargetTerminal = GetSessionTabTerminal(SessionsPageControl->Pages[Index]);
  9231. if (TargetTerminal != NULL)
  9232. {
  9233. DebugAssert(!IsFileControl(DropSourceControl, osRemote));
  9234. if (!IsFileControl(DropSourceControl, osRemote))
  9235. {
  9236. TTerminalManager::Instance()->ActiveTerminal = TargetTerminal;
  9237. RemoteFileControlDragDropFileOperation(SessionsPageControl, Effect,
  9238. // Why don't we use Terminal->CurrentDirectory directly?
  9239. TTerminalManager::Instance()->ActiveTerminal->CurrentDirectory,
  9240. // do not force queue, drag drop
  9241. false, true);
  9242. }
  9243. }
  9244. }
  9245. //---------------------------------------------------------------------------
  9246. void __fastcall TCustomScpExplorerForm::QueueDDProcessDropped(
  9247. TObject * /*Sender*/, int /*KeyState*/, const TPoint & /*Point*/, int Effect)
  9248. {
  9249. // Downloads are handled in RemoteFileControlDDEnd
  9250. if (!IsFileControl(DropSourceControl, osRemote))
  9251. {
  9252. RemoteFileControlDragDropFileOperation(QueueView3, Effect,
  9253. Terminal->CurrentDirectory,
  9254. // force queue, drag drop
  9255. true, true);
  9256. }
  9257. }
  9258. //---------------------------------------------------------------------------
  9259. void __fastcall TCustomScpExplorerForm::FormClose(TObject * /*Sender*/, TCloseAction & /*Action*/)
  9260. {
  9261. FShowing = false;
  9262. // Do not save empty workspace
  9263. if (WinConfiguration->AutoSaveWorkspace && (Terminal != NULL))
  9264. {
  9265. std::unique_ptr<TObjectList> DataList(DoCollectWorkspace());
  9266. UnicodeString Name = WorkspaceName();
  9267. DoSaveWorkspace(
  9268. Name, DataList.get(),
  9269. !Configuration->DisablePasswordStoring &&
  9270. WinConfiguration->AutoSaveWorkspacePasswords, false);
  9271. WinConfiguration->LastStoredSession = Name;
  9272. }
  9273. }
  9274. //---------------------------------------------------------------------------
  9275. void __fastcall TCustomScpExplorerForm::RemoteDirViewRead(TObject * /*Sender*/)
  9276. {
  9277. TManagedTerminal * ManagedTerminal =
  9278. dynamic_cast<TManagedTerminal *>(RemoteDirView->Terminal);
  9279. if (ManagedTerminal != NULL)
  9280. {
  9281. ManagedTerminal->DirectoryLoaded = Now();
  9282. }
  9283. }
  9284. //---------------------------------------------------------------------------
  9285. void __fastcall TCustomScpExplorerForm::DirViewSelectItem(TObject * Sender,
  9286. TListItem * /*Item*/, bool /*Selected*/)
  9287. {
  9288. TCustomDirView * DirView = reinterpret_cast<TCustomDirView *>(Sender);
  9289. switch (DirView->LastSelectMethod)
  9290. {
  9291. case smKeyboard:
  9292. Configuration->Usage->Inc(L"KeyboardSelections");
  9293. break;
  9294. case smMouse:
  9295. Configuration->Usage->Inc(L"MouseSelections");
  9296. break;
  9297. }
  9298. }
  9299. //---------------------------------------------------------------------------
  9300. int __fastcall TCustomScpExplorerForm::AddSessionColor(TColor Color)
  9301. {
  9302. if (Color != 0)
  9303. {
  9304. return GetSessionColorImage(FSessionColors, Color, FSessionColorMaskImageIndex);
  9305. }
  9306. else
  9307. {
  9308. return FSessionTabImageIndex;
  9309. }
  9310. }
  9311. //---------------------------------------------------------------------------
  9312. int __fastcall TCustomScpExplorerForm::AddFixedSessionImage(int GlyphsSourceIndex)
  9313. {
  9314. TPngImageCollectionItem * Item =
  9315. GlyphsModule->ExplorerImages->PngImages->Items[GlyphsSourceIndex];
  9316. FSessionColors->AddPng(Item->PngImage);
  9317. return FSessionColors->Count - 1;
  9318. }
  9319. //---------------------------------------------------------------------------
  9320. void __fastcall TCustomScpExplorerForm::AddFixedSessionImages()
  9321. {
  9322. FSessionColors->SetSize(GlyphsModule->ExplorerImages->Width, GlyphsModule->ExplorerImages->Height);
  9323. FNewSessionTabImageIndex = AddFixedSessionImage(NonVisualDataModule->NewSessionAction->ImageIndex);
  9324. FSessionTabImageIndex = AddFixedSessionImage(SiteImageIndex);
  9325. FSessionColorMaskImageIndex = AddFixedSessionImage(SiteColorMaskImageIndex);
  9326. }
  9327. //---------------------------------------------------------------------------
  9328. void __fastcall TCustomScpExplorerForm::CollectItemsWithTextDisplayMode(TWinControl * Control)
  9329. {
  9330. for (int Index = 0; Index < Control->ControlCount; Index++)
  9331. {
  9332. TControl * AControl = Control->Controls[Index];
  9333. TWinControl * WinControl = dynamic_cast<TWinControl *>(AControl);
  9334. if (WinControl != NULL)
  9335. {
  9336. CollectItemsWithTextDisplayMode(WinControl);
  9337. }
  9338. TTBCustomToolbar * Toolbar = dynamic_cast<TTBCustomToolbar *>(AControl);
  9339. if (Toolbar != NULL)
  9340. {
  9341. // we care for top-level items only
  9342. TTBCustomItem * Items = Toolbar->Items;
  9343. for (int ItemIndex = 0; ItemIndex < Items->Count; ItemIndex++)
  9344. {
  9345. TTBCustomItem * Item = Items->Items[ItemIndex];
  9346. if (((Item->DisplayMode == nbdmImageAndText) ||
  9347. (dynamic_cast<TTBXLabelItem *>(Item) != NULL)) &&
  9348. EligibleForImageDisplayMode(Item))
  9349. {
  9350. FItemsWithTextDisplayMode.insert(Item);
  9351. }
  9352. }
  9353. }
  9354. }
  9355. }
  9356. //---------------------------------------------------------------------------
  9357. bool __fastcall TCustomScpExplorerForm::EligibleForImageDisplayMode(TTBCustomItem * /*Item*/)
  9358. {
  9359. return true;
  9360. }
  9361. //---------------------------------------------------------------------------
  9362. bool __fastcall TCustomScpExplorerForm::UpdateToolbarDisplayMode()
  9363. {
  9364. bool SelectiveToolbarText = WinConfiguration->SelectiveToolbarText;
  9365. TTBItemDisplayMode DisplayMode = (SelectiveToolbarText ? nbdmImageAndText : nbdmDefault);
  9366. typedef std::set<TTBCustomItem *> TItemsWithTextDisplayMode;
  9367. TItemsWithTextDisplayMode::iterator i = FItemsWithTextDisplayMode.begin();
  9368. bool Result = true;
  9369. while (Result && (i != FItemsWithTextDisplayMode.end()))
  9370. {
  9371. TTBCustomItem * Item = *i;
  9372. TTBXLabelItem * Label = dynamic_cast<TTBXLabelItem *>(Item);
  9373. if (Label != NULL)
  9374. {
  9375. // optimization
  9376. if (Label->Visible == SelectiveToolbarText)
  9377. {
  9378. Result = false;
  9379. }
  9380. else
  9381. {
  9382. Label->Visible = SelectiveToolbarText;
  9383. }
  9384. }
  9385. else
  9386. {
  9387. // optimization
  9388. if (Item->DisplayMode == DisplayMode)
  9389. {
  9390. Result = false;
  9391. }
  9392. else
  9393. {
  9394. Item->DisplayMode = DisplayMode;
  9395. }
  9396. }
  9397. ++i;
  9398. }
  9399. if (Result)
  9400. {
  9401. UpdateNewSessionTab();
  9402. }
  9403. return Result;
  9404. }
  9405. //---------------------------------------------------------------------------
  9406. void __fastcall TCustomScpExplorerForm::DisplaySystemContextMenu()
  9407. {
  9408. DebugFail();
  9409. }
  9410. //---------------------------------------------------------------------------
  9411. bool __fastcall TCustomScpExplorerForm::IsBusy()
  9412. {
  9413. // Among other, a lock level is non zero, while directory is being loaded,
  9414. // even when it happens as a result of dirview navigation,
  9415. // i.e. when TNonVisualDataModule is NOT FBusy.
  9416. // That's why the TNonVisualDataModule::GetBusy calls this method.
  9417. // Among other this prevents a panel auto update to occur while
  9418. // directory is changing.
  9419. return (FLockLevel > 0) || DirView(osCurrent)->IsEditing();
  9420. }
  9421. //---------------------------------------------------------------------------
  9422. Boolean __fastcall TCustomScpExplorerForm::AllowedAction(TAction * /*Action*/, TActionAllowed Allowed)
  9423. {
  9424. // While the window is disabled, we still seem to process menu shortcuts at least,
  9425. // so stop it at least here.
  9426. // See also TCustomScpExplorerForm::RemoteDirViewBusy
  9427. return
  9428. (Allowed == aaUpdate) ||
  9429. !NonVisualDataModule->Busy;
  9430. }
  9431. //---------------------------------------------------------------------------
  9432. void __fastcall TCustomScpExplorerForm::EditMenuItemPopup(TTBCustomItem * Sender, bool FromLink)
  9433. {
  9434. NonVisualDataModule->EditMenuItemPopup(Sender, FromLink);
  9435. }
  9436. //---------------------------------------------------------------------------
  9437. void __fastcall TCustomScpExplorerForm::DirViewBusy(TObject * /*Sender*/, int Busy, bool & State)
  9438. {
  9439. // This is somewhat redundant to LockWindow() call from
  9440. // TTerminalManager::TerminalReadDirectoryProgress.
  9441. // But disabling window is known not to block keyboard shorcuts
  9442. // (see TCustomScpExplorerForm::AllowedAction), this hopefully works.
  9443. if (Busy > 0)
  9444. {
  9445. if (NonVisualDataModule->Busy)
  9446. {
  9447. State = false;
  9448. }
  9449. else
  9450. {
  9451. NonVisualDataModule->StartBusy();
  9452. // Will be countered for TCustomDirView.DoExecute in DoDirViewExecFile
  9453. LockWindow();
  9454. }
  9455. }
  9456. else if (Busy < 0)
  9457. {
  9458. UnlockWindow();
  9459. NonVisualDataModule->EndBusy();
  9460. }
  9461. else
  9462. {
  9463. State = NonVisualDataModule->Busy;
  9464. }
  9465. }
  9466. //---------------------------------------------------------------------------
  9467. void __fastcall TCustomScpExplorerForm::SessionsPageControlContextPopup(TObject * /*Sender*/, TPoint & MousePos, bool & Handled)
  9468. {
  9469. int Index = SessionsPageControl->IndexOfTabAt(MousePos.X, MousePos.Y);
  9470. // no context menu for "New session tab"
  9471. if ((Index >= 0) && (GetSessionTabTerminal(SessionsPageControl->Pages[Index]) != NULL))
  9472. {
  9473. SessionsPageControl->ActivePageIndex = Index;
  9474. if (DebugAlwaysTrue(SessionTabSwitched()))
  9475. {
  9476. // copied from TControl.WMContextMenu
  9477. SendCancelMode(SessionsPageControl);
  9478. // explicit popup instead of using PopupMenu property
  9479. // to avoid menu to popup somewhere within SessionTabSwitched above,
  9480. // while connecting yet not-connected session and hence
  9481. // allowing an access to commands over not-completelly connected session
  9482. TPoint Point = SessionsPageControl->ClientToScreen(MousePos);
  9483. TPopupMenu * PopupMenu = NonVisualDataModule->SessionsPopup;
  9484. PopupMenu->PopupComponent = SessionsPageControl;
  9485. PopupMenu->Popup(Point.x, Point.y);
  9486. }
  9487. }
  9488. Handled = true;
  9489. }
  9490. //---------------------------------------------------------------------------
  9491. bool __fastcall TCustomScpExplorerForm::CanChangePassword()
  9492. {
  9493. return (Terminal != NULL) && Terminal->Active && Terminal->IsCapable[fcChangePassword];
  9494. }
  9495. //---------------------------------------------------------------------------
  9496. void __fastcall TCustomScpExplorerForm::ChangePassword()
  9497. {
  9498. Configuration->Usage->Inc(L"PasswordChanges");
  9499. std::unique_ptr<TSessionData> ChangePasswordData(Terminal->SessionData->Clone());
  9500. ChangePasswordData->DisableAuthentationsExceptPassword();
  9501. ChangePasswordData->ChangePassword = true;
  9502. std::unique_ptr<TTerminal> ChangePasswordSession(
  9503. Terminal->CreateSecondarySession(L"ChangePassword", ChangePasswordData.get()));
  9504. ChangePasswordSession->AutoReadDirectory = false;
  9505. if (TTerminalManager::Instance()->ConnectTerminal(ChangePasswordSession.get()))
  9506. {
  9507. MessageDialog(MainInstructions(LoadStr(PASSWORD_CHANGED)), qtInformation, qaOK, HELP_CHANGE_PASSWORD);
  9508. }
  9509. else
  9510. {
  9511. Configuration->Usage->Inc(L"PasswordChangesFailed");
  9512. }
  9513. }
  9514. //---------------------------------------------------------------------------
  9515. bool __fastcall TCustomScpExplorerForm::CanPrivateKeyUpload()
  9516. {
  9517. // No nice way to assert SSH2
  9518. return (Terminal != NULL) && Terminal->Active && (Terminal->FSProtocol == cfsSFTP);
  9519. }
  9520. //---------------------------------------------------------------------------
  9521. void __fastcall TCustomScpExplorerForm::PrivateKeyUpload()
  9522. {
  9523. TOperationVisualizer OperationVisualizer;
  9524. UnicodeString FileName = Terminal->SessionData->PublicKeyFile;
  9525. TTerminalManager::Instance()->UploadPublicKey(Terminal, NULL, FileName);
  9526. }
  9527. //---------------------------------------------------------------------------
  9528. void __fastcall TCustomScpExplorerForm::ChangeScale(int M, int D)
  9529. {
  9530. TForm::ChangeScale(M, D);
  9531. int APixelsPerInch = GetControlPixelsPerInch(this);
  9532. HANDLE ResourceModule = GUIConfiguration->ChangeToDefaultResourceModule();
  9533. try
  9534. {
  9535. GlyphsModule->SetPixelsPerInch(APixelsPerInch);
  9536. }
  9537. __finally
  9538. {
  9539. GUIConfiguration->ChangeResourceModule(ResourceModule);
  9540. }
  9541. }
  9542. //---------------------------------------------------------------------------
  9543. void __fastcall TCustomScpExplorerForm::DockContextPopup(TObject * Sender, TPoint & MousePos, bool & /*Handled*/)
  9544. {
  9545. NonVisualDataModule->ControlContextPopup(Sender, MousePos);
  9546. }
  9547. //---------------------------------------------------------------------
  9548. class TPublicControl : public TControl
  9549. {
  9550. friend class TCustomScpExplorerForm;
  9551. };
  9552. //---------------------------------------------------------------------------
  9553. void __fastcall TCustomScpExplorerForm::CopyPopup(TControl * DestControl, TControl * SourceControl)
  9554. {
  9555. static_cast<TPublicControl *>(DestControl)->PopupMenu = static_cast<TPublicControl *>(SourceControl)->PopupMenu;
  9556. static_cast<TPublicControl *>(DestControl)->OnContextPopup = static_cast<TPublicControl *>(SourceControl)->OnContextPopup;
  9557. }
  9558. //---------------------------------------------------------------------------
  9559. void __fastcall TCustomScpExplorerForm::DoBookmarkClick(TOperationSide Side, TObject * Sender)
  9560. {
  9561. TBookmark * Bookmark = DebugNotNull(reinterpret_cast<TBookmark *>(dynamic_cast<TTBCustomItem *>(Sender)->Tag));
  9562. OpenBookmark(Side, Bookmark);
  9563. }
  9564. //---------------------------------------------------------------------------
  9565. void __fastcall TCustomScpExplorerForm::LocalBookmarkClick(TObject * Sender)
  9566. {
  9567. DoBookmarkClick(osLocal, Sender);
  9568. }
  9569. //---------------------------------------------------------------------------
  9570. void __fastcall TCustomScpExplorerForm::RemoteBookmarkClick(TObject * Sender)
  9571. {
  9572. DoBookmarkClick(osRemote, Sender);
  9573. }
  9574. //---------------------------------------------------------------------------
  9575. void __fastcall TCustomScpExplorerForm::CreateOpenDirMenuList(
  9576. TTBCustomItem * Menu, TOperationSide Side, TBookmarkList * BookmarkList)
  9577. {
  9578. // TODO
  9579. if (BookmarkList != NULL)
  9580. {
  9581. TNotifyEvent OnBookmarkClick = (Side == osLocal) ? &LocalBookmarkClick : &RemoteBookmarkClick;
  9582. if (!WinConfiguration->UseLocationProfiles)
  9583. {
  9584. std::unique_ptr<TStringList> Directories(new TStringList());
  9585. Directories->CaseSensitive = (Side == osRemote);
  9586. for (int Index = 0; Index < BookmarkList->Count; Index++)
  9587. {
  9588. TBookmark * Bookmark = BookmarkList->Bookmarks[Index];
  9589. UnicodeString Directory = Bookmark->GetSideDirectory(Side);
  9590. if (!Directory.IsEmpty() && (Directories->IndexOf(Directory) < 0))
  9591. {
  9592. std::unique_ptr<TTBCustomItem> Item(new TTBXItem(Owner));
  9593. Item->Caption = EscapeHotkey(Directory);
  9594. Item->ShortCut = Bookmark->ShortCut;
  9595. Item->OnClick = OnBookmarkClick;
  9596. Item->Tag = reinterpret_cast<int>(Bookmark);
  9597. Directories->Add(Directory);
  9598. Menu->Add(Item.release());
  9599. }
  9600. }
  9601. }
  9602. else
  9603. {
  9604. std::unique_ptr<TStrings> Folders(CreateSortedStringList());
  9605. for (int Index = 0; Index < BookmarkList->Count; Index++)
  9606. {
  9607. TBookmark * Bookmark = BookmarkList->Bookmarks[Index];
  9608. if (!Bookmark->Node.IsEmpty())
  9609. {
  9610. Folders->Add(Bookmark->Node);
  9611. }
  9612. }
  9613. for (int Index = 0; Index < Folders->Count; Index++)
  9614. {
  9615. std::unique_ptr<TTBCustomItem> Item(new TTBXSubmenuItem(Owner));
  9616. Item->Caption = Folders->Strings[Index];
  9617. Item->ImageIndex = NonVisualDataModule->RemoteChangePathAction->ImageIndex;
  9618. Folders->Objects[Index] = Item.get();
  9619. Menu->Add(Item.release());
  9620. }
  9621. for (int Index = 0; Index < BookmarkList->Count; Index++)
  9622. {
  9623. TBookmark * Bookmark = BookmarkList->Bookmarks[Index];
  9624. TTBCustomItem * Parent;
  9625. if (!Bookmark->Node.IsEmpty())
  9626. {
  9627. DebugAssert(Folders->IndexOf(Bookmark->Node) >= 0);
  9628. Parent = dynamic_cast<TTBCustomItem *>(Folders->Objects[Folders->IndexOf(Bookmark->Node)]);
  9629. }
  9630. else
  9631. {
  9632. Parent = Menu;
  9633. }
  9634. std::unique_ptr<TTBCustomItem> Item(new TTBXItem(Owner));
  9635. Item->Caption = Bookmark->Name;
  9636. Item->ShortCut = Bookmark->ShortCut;
  9637. Item->OnClick = OnBookmarkClick;
  9638. Item->Tag = reinterpret_cast<int>(Bookmark);
  9639. if (((Bookmark->Name == Bookmark->Local) || (Bookmark->Name == Bookmark->Remote)) &&
  9640. (Bookmark->Local.IsEmpty() || Bookmark->Remote.IsEmpty()))
  9641. {
  9642. // No hint for location profiles that are actually mere "bookmarks"
  9643. }
  9644. else
  9645. {
  9646. UnicodeString Hint = FORMAT(LoadStrPart(LOCATION_PROFILE_HINT, 1), (Bookmark->Name));
  9647. UnicodeString LongHint;
  9648. if (!Bookmark->Local.IsEmpty())
  9649. {
  9650. AddToList(LongHint, FORMAT(LoadStrPart(LOCATION_PROFILE_HINT, 2), (Bookmark->Local)), L"\n");
  9651. }
  9652. if (!Bookmark->Remote.IsEmpty())
  9653. {
  9654. AddToList(LongHint, FORMAT(LoadStrPart(LOCATION_PROFILE_HINT, 3), (Bookmark->Remote)), L"\n");
  9655. }
  9656. AddToList(Hint, LongHint, L"|");
  9657. Item->Hint = Hint;
  9658. }
  9659. Parent->Add(Item.release());
  9660. }
  9661. }
  9662. AddMenuSeparator(Menu);
  9663. }
  9664. }
  9665. //---------------------------------------------------------------------------
  9666. void __fastcall TCustomScpExplorerForm::CreateOpenDirMenu(TTBCustomItem * Menu, TOperationSide Side)
  9667. {
  9668. Menu->Clear();
  9669. std::unique_ptr<TTBCustomItem> Item;
  9670. Item.reset(new TTBXItem(Owner));
  9671. Item->Action = (Side == osLocal) ? NonVisualDataModule->LocalOpenDirAction : NonVisualDataModule->RemoteOpenDirAction;
  9672. Item->Options = Item->Options << tboDefault;
  9673. Menu->Add(Item.release());
  9674. AddMenuSeparator(Menu);
  9675. if (Terminal != NULL)
  9676. {
  9677. CreateOpenDirMenuList(Menu, Side, WinConfiguration->Bookmarks[Terminal->SessionData->SessionKey]);
  9678. }
  9679. CreateOpenDirMenuList(Menu, Side, WinConfiguration->SharedBookmarks);
  9680. Item.reset(new TTBXItem(Owner));
  9681. Item->Action = (Side == osLocal) ? NonVisualDataModule->LocalAddBookmarkAction : NonVisualDataModule->RemoteAddBookmarkAction;
  9682. Menu->Add(Item.release());
  9683. }
  9684. //---------------------------------------------------------------------------
  9685. bool __fastcall TCustomScpExplorerForm::TryOpenDirectory(TOperationSide Side, const UnicodeString & Path)
  9686. {
  9687. bool Result = true;
  9688. try
  9689. {
  9690. if (Side == osRemote)
  9691. {
  9692. Terminal->ExceptionOnFail = true;
  9693. }
  9694. try
  9695. {
  9696. DirView(Side)->Path = Path;
  9697. }
  9698. __finally
  9699. {
  9700. if (Side == osRemote)
  9701. {
  9702. Terminal->ExceptionOnFail = false;
  9703. }
  9704. }
  9705. }
  9706. catch (Exception & E)
  9707. {
  9708. if ((Side != osRemote) || Terminal->Active)
  9709. {
  9710. ShowExtendedExceptionEx(Terminal, &E);
  9711. Result = false;
  9712. }
  9713. else
  9714. {
  9715. throw;
  9716. }
  9717. }
  9718. return Result;
  9719. }
  9720. //---------------------------------------------------------------------------
  9721. void __fastcall TCustomScpExplorerForm::ReloadDirectory(TOperationSide Side)
  9722. {
  9723. // Make sure there some feedback even when loading tiny local folders
  9724. TInstantOperationVisualizer Visualizer;
  9725. DirView(Side)->ReloadDirectory();
  9726. }
  9727. //---------------------------------------------------------------------------
  9728. void __fastcall TCustomScpExplorerForm::CloseSessionTab(int Index)
  9729. {
  9730. if (Index == SessionsPageControl->TabIndex)
  9731. {
  9732. CloseSession();
  9733. }
  9734. else
  9735. {
  9736. TTabSheet * TabSheet = SessionsPageControl->Pages[Index];
  9737. TTerminal * Terminal = GetSessionTabTerminal(TabSheet);
  9738. TTerminalManager * Manager = TTerminalManager::Instance();
  9739. if (!Terminal->Active)
  9740. {
  9741. Manager->FreeTerminal(Terminal);
  9742. }
  9743. else
  9744. {
  9745. TTerminalQueue * Queue = Manager->FindQueueForTerminal(Terminal);
  9746. if (CanCloseQueue(Queue))
  9747. {
  9748. Manager->FreeTerminal(Terminal);
  9749. }
  9750. }
  9751. }
  9752. }
  9753. //---------------------------------------------------------------------------
  9754. void __fastcall TCustomScpExplorerForm::SessionsPageControlCloseButtonClick(TPageControl * /*Sender*/, int Index)
  9755. {
  9756. CloseSessionTab(Index);
  9757. }
  9758. //---------------------------------------------------------------------------
  9759. void __fastcall TCustomScpExplorerForm::CopyFilesToClipboard(TOperationSide Side, bool OnFocused)
  9760. {
  9761. if (DebugAlwaysTrue(!IsSideLocalBrowser(Side)))
  9762. {
  9763. TInstantOperationVisualizer Visualizer;
  9764. // To trigger ClipboardDataObjectRelease (in case the clipboard already contains our data)
  9765. // before we create a new data object
  9766. ClipboardClear();
  9767. DebugAssert(FClipboardFakeDirectory.IsEmpty());
  9768. DebugAssert(FClipboardFakeMonitors.get() == NULL);
  9769. FClipboardFakeDirectory = CreateFakeTransferDirectory();
  9770. // Intermediate variable to WORKAROUND a crash in compiler
  9771. TObjectList * ClipboardFakeMonitors =
  9772. StartCreationDirectoryMonitorsOnEachDrive(FILE_NOTIFY_CHANGE_DIR_NAME, ClipboardFakeCreated);
  9773. FClipboardFakeMonitors.reset(ClipboardFakeMonitors);
  9774. if (FClipboardDragDropFilesEx.get() == NULL)
  9775. {
  9776. FClipboardDragDropFilesEx.reset(new TDragDropFilesEx(this));
  9777. FClipboardDragDropFilesEx->OnDataObjectRelease = ClipboardDataObjectRelease;
  9778. }
  9779. FClipboardDragDropFilesEx->FileList->Clear();
  9780. FClipboardDragDropFilesEx->FileList->AddItem(NULL, FClipboardFakeDirectory);
  9781. FClipboardDragDropFilesEx->CopyToClipboard();
  9782. FClipboardTerminal = FTerminal;
  9783. // Need full paths, as cwd can be different once files are pasted
  9784. FClipboardFileList.reset(TRemoteFileList::CloneStrings(RemoteDirView->CreateFileList(OnFocused, true)));
  9785. for (int Index = 0; Index < FClipboardFileList->Count; Index++)
  9786. {
  9787. FClipboardFileList->Strings[Index] = UnixExcludeTrailingBackslash(FClipboardFileList->Strings[Index]);
  9788. }
  9789. }
  9790. }
  9791. //---------------------------------------------------------------------------
  9792. void __fastcall TCustomScpExplorerForm::ClipboardClear()
  9793. {
  9794. if (OpenClipboard(0))
  9795. {
  9796. EmptyClipboard(); // Calls ClipboardDataObjectRelease, if clipboard contains our data
  9797. CloseClipboard();
  9798. }
  9799. }
  9800. //---------------------------------------------------------------------------
  9801. bool __fastcall TCustomScpExplorerForm::DoesClipboardContainOurFiles()
  9802. {
  9803. return !FClipboardFakeDirectory.IsEmpty();
  9804. }
  9805. //---------------------------------------------------------------------------
  9806. void __fastcall TCustomScpExplorerForm::ClipboardStop()
  9807. {
  9808. RemoveDir(ApiPath(FClipboardFakeDirectory));
  9809. FClipboardFakeDirectory = UnicodeString();
  9810. FClipboardTerminal = NULL;
  9811. // Also called in Idle()
  9812. FClipboardFakeMonitors.reset(NULL);
  9813. }
  9814. //---------------------------------------------------------------------------
  9815. void __fastcall TCustomScpExplorerForm::ClipboardDataObjectRelease(TObject * /*Sender*/)
  9816. {
  9817. if (DebugAlwaysTrue(!FClipboardFakeDirectory.IsEmpty()))
  9818. {
  9819. ClipboardStop();
  9820. }
  9821. }
  9822. //---------------------------------------------------------------------------
  9823. void __fastcall TCustomScpExplorerForm::ClipboardDownload(const UnicodeString & TargetDirectory, bool NoConfirmation, bool DragDrop)
  9824. {
  9825. TTerminalManager * Manager = TTerminalManager::Instance();
  9826. Manager->ActiveTerminal = FClipboardTerminal;
  9827. TTransferOperationParam Params;
  9828. Params.TargetDirectory = TargetDirectory;
  9829. Params.DragDrop = DragDrop;
  9830. ExecuteFileOperation(foCopy, osRemote, FClipboardFileList.get(), NoConfirmation, &Params);
  9831. }
  9832. //---------------------------------------------------------------------------
  9833. void __fastcall TCustomScpExplorerForm::ClipboardFakeCreated(TObject * /*Sender*/, const UnicodeString FileName)
  9834. {
  9835. // It can actually rarelly happen that some random file is created, while we are shuttting down the monitor
  9836. // (as it pumps a Windows message queue while being shutted down)
  9837. if (DebugAlwaysTrue(!FClipboardFakeDirectory.IsEmpty()) &&
  9838. SameText(ExtractFileName(FileName), ExtractFileName(FClipboardFakeDirectory)))
  9839. {
  9840. // Can fail as it can be e.g. locked by AV, so we retry that later
  9841. bool Removed = RemoveDir(ApiPath(FileName));
  9842. try
  9843. {
  9844. if (!NonVisualDataModule->Busy)
  9845. {
  9846. bool NoConfirmation = (WinConfiguration->DDTransferConfirmation == asOff);
  9847. ClipboardDownload(ExtractFilePath(FileName), NoConfirmation, true);
  9848. }
  9849. }
  9850. __finally
  9851. {
  9852. if (!Removed)
  9853. {
  9854. RemoveDir(ApiPath(FileName));
  9855. }
  9856. }
  9857. }
  9858. }
  9859. //---------------------------------------------------------------------------
  9860. void __fastcall TCustomScpExplorerForm::DirViewGetItemColor(
  9861. TObject * Sender, UnicodeString FileName, bool Directory, __int64 Size, TDateTime Modification, TColor & Color)
  9862. {
  9863. TFileMasks::TParams MaskParams;
  9864. MaskParams.Size = Size;
  9865. MaskParams.Modification = Modification;
  9866. TCustomDirView * DirView = DebugNotNull(dynamic_cast<TCustomDirView *>(Sender));
  9867. for (TFileColorData::TList::const_iterator Iter = FFileColors.begin(); Iter != FFileColors.end(); Iter++)
  9868. {
  9869. bool ImplicitMatch;
  9870. if (Iter->FileMask.Matches(FileName, Directory, DirView->PathName, &MaskParams, false, ImplicitMatch) &&
  9871. !ImplicitMatch)
  9872. {
  9873. Color = Iter->Color;
  9874. break;
  9875. }
  9876. }
  9877. }
  9878. //---------------------------------------------------------------------------
  9879. void __fastcall TCustomScpExplorerForm::DirViewKeyDown(TObject * Sender, WORD & Key, TShiftState)
  9880. {
  9881. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  9882. if (!DirView->IsEditing())
  9883. {
  9884. // Handled here, instead of RemoteDirViewKeyPress (as on Login dialog),
  9885. // as VK_BACK is handled by TCustomDirView.KeyDown and we can swallow it here,
  9886. // while searching
  9887. if (Key == VK_BACK)
  9888. {
  9889. if (!FIncrementalSearch.IsEmpty())
  9890. {
  9891. if (FIncrementalSearch.Length() == 1)
  9892. {
  9893. ResetIncrementalSearch();
  9894. }
  9895. else
  9896. {
  9897. UnicodeString NewText = FIncrementalSearch.SubString(1, FIncrementalSearch.Length() - 1);
  9898. IncrementalSearch(NewText, false, false);
  9899. }
  9900. Key = 0;
  9901. }
  9902. }
  9903. }
  9904. }
  9905. //---------------------------------------------------------------------------
  9906. void __fastcall TCustomScpExplorerForm::DirViewKeyPress(TObject * Sender, wchar_t & Key)
  9907. {
  9908. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  9909. if (!DirView->IsEditing() && (WinConfiguration->PanelSearch != isOff))
  9910. {
  9911. // Filter control sequences.
  9912. // When not searching yet, prefer use of the space for toggling file selection
  9913. // (so we cannot incrementally search for a string starting with a space).
  9914. if ((Key > VK_SPACE) ||
  9915. ((Key == VK_SPACE) && (GetKeyState(VK_CONTROL) >= 0) && !FIncrementalSearch.IsEmpty()))
  9916. {
  9917. IncrementalSearch(FIncrementalSearch + Key, false, false);
  9918. Key = 0;
  9919. }
  9920. }
  9921. }
  9922. //---------------------------------------------------------------------------
  9923. void __fastcall TCustomScpExplorerForm::ResetIncrementalSearch()
  9924. {
  9925. if (!FIncrementalSearch.IsEmpty())
  9926. {
  9927. FIncrementalSearch = L"";
  9928. DirView(osCurrent)->UpdateStatusBar();
  9929. }
  9930. }
  9931. //---------------------------------------------------------------------------
  9932. void __fastcall TCustomScpExplorerForm::IncrementalSearch(const UnicodeString & Text, bool SkipCurrent, bool Reverse)
  9933. {
  9934. TListItem * Item = SearchFile(Text, SkipCurrent, Reverse);
  9935. if (Item == NULL)
  9936. {
  9937. MessageBeep(MB_ICONHAND);
  9938. }
  9939. else
  9940. {
  9941. TCustomDirView * ADirView = DirView(osCurrent);
  9942. {
  9943. TAutoNestingCounter Guard(FIncrementalSearching);
  9944. ADirView->FocusItem(Item);
  9945. if (ADirView->NortonLike == nlOff)
  9946. {
  9947. if ((ADirView->Selected != Item) ||
  9948. (ADirView->SelCount > 1))
  9949. {
  9950. ADirView->ClearSelection();
  9951. ADirView->Selected = Item;
  9952. }
  9953. }
  9954. }
  9955. FIncrementalSearch = Text;
  9956. Item->MakeVisible(false);
  9957. TListItem * NextItem = SearchFile(Text, true, Reverse);
  9958. FIncrementalSearchHaveNext = (NextItem != NULL) && (NextItem != Item);
  9959. ADirView->UpdateStatusBar();
  9960. }
  9961. }
  9962. //---------------------------------------------------------------------------
  9963. TListItem * __fastcall TCustomScpExplorerForm::GetNextFile(TListItem * Item, bool Reverse)
  9964. {
  9965. int Index = Item->Index;
  9966. if (!Reverse)
  9967. {
  9968. Index++;
  9969. if (Index >= Item->Owner->Count)
  9970. {
  9971. Index = 0;
  9972. }
  9973. }
  9974. else
  9975. {
  9976. Index--;
  9977. if (Index < 0)
  9978. {
  9979. Index = Item->Owner->Count - 1;
  9980. }
  9981. }
  9982. return Item->Owner->Item[Index];
  9983. }
  9984. //---------------------------------------------------------------------------
  9985. TListItem * __fastcall TCustomScpExplorerForm::SearchFile(const UnicodeString & Text, bool SkipCurrent, bool Reverse)
  9986. {
  9987. TCustomDirView * ADirView = DirView(osCurrent);
  9988. if (ADirView->Items->Count == 0)
  9989. {
  9990. return NULL;
  9991. }
  9992. else
  9993. {
  9994. TListItem * CurrentItem = (ADirView->ItemFocused != NULL) ? ADirView->ItemFocused : ADirView->Items->Item[0];
  9995. TListItem * Item = CurrentItem;
  9996. if (SkipCurrent)
  9997. {
  9998. Item = DebugNotNull(GetNextFile(Item, Reverse));
  9999. }
  10000. while (true)
  10001. {
  10002. bool Matches = false;
  10003. switch (WinConfiguration->PanelSearch)
  10004. {
  10005. case isNameStartOnly:
  10006. Matches = ContainsTextSemiCaseSensitive(Item->Caption.SubString(1, Text.Length()), Text);
  10007. break;
  10008. case isName:
  10009. Matches = ContainsTextSemiCaseSensitive(Item->Caption, Text);
  10010. break;
  10011. case isAll:
  10012. int ColCount = (ADirView->ViewStyle == vsReport) ? ADirView->ColProperties->Count : 1;
  10013. int Index = 0;
  10014. while ((Index < ColCount) && !Matches)
  10015. {
  10016. bool Visible = (ADirView->ViewStyle == vsReport) ? ADirView->ColProperties->Visible[Index] : true;
  10017. if (Visible)
  10018. {
  10019. Matches = ContainsTextSemiCaseSensitive(ADirView->GetColumnText(Item, Index), Text);
  10020. }
  10021. Index++;
  10022. }
  10023. break;
  10024. }
  10025. if (Matches)
  10026. {
  10027. return Item;
  10028. }
  10029. Item = GetNextFile(Item, Reverse);
  10030. if (Item == CurrentItem)
  10031. {
  10032. return NULL;
  10033. }
  10034. }
  10035. }
  10036. }
  10037. //---------------------------------------------------------------------------
  10038. void __fastcall TCustomScpExplorerForm::DirViewExit(TObject *)
  10039. {
  10040. ResetIncrementalSearch();
  10041. }
  10042. //---------------------------------------------------------------------------
  10043. void __fastcall TCustomScpExplorerForm::CMDialogKey(TWMKeyDown & Message)
  10044. {
  10045. if (Message.CharCode == VK_TAB)
  10046. {
  10047. if (!FIncrementalSearch.IsEmpty())
  10048. {
  10049. TShiftState Shift = KeyDataToShiftState(Message.KeyData);
  10050. bool Reverse = Shift.Contains(ssShift);
  10051. IncrementalSearch(FIncrementalSearch, true, Reverse);
  10052. Message.Result = 1;
  10053. return;
  10054. }
  10055. }
  10056. else if (Message.CharCode == VK_ESCAPE)
  10057. {
  10058. if (!FIncrementalSearch.IsEmpty())
  10059. {
  10060. ResetIncrementalSearch();
  10061. Message.Result = 1;
  10062. return;
  10063. }
  10064. }
  10065. TForm::Dispatch(&Message);
  10066. }
  10067. //---------------------------------------------------------------------------
  10068. void __fastcall TCustomScpExplorerForm::Deactivate()
  10069. {
  10070. TForm::Deactivate();
  10071. // E.g. when switching to an internal editor window
  10072. ResetIncrementalSearch();
  10073. }
  10074. //---------------------------------------------------------------------------
  10075. void __fastcall TCustomScpExplorerForm::ApplicationEventsDeactivate(TObject *)
  10076. {
  10077. // When switching to another application
  10078. ResetIncrementalSearch();
  10079. }
  10080. //---------------------------------------------------------------------------
  10081. void __fastcall TCustomScpExplorerForm::ApplicationEventsModalBegin(TObject *)
  10082. {
  10083. ResetIncrementalSearch();
  10084. }
  10085. //---------------------------------------------------------------------------
  10086. void __fastcall TCustomScpExplorerForm::DirViewChangeFocus(TObject *, TListItem *)
  10087. {
  10088. if (FIncrementalSearching <= 0)
  10089. {
  10090. ResetIncrementalSearch();
  10091. }
  10092. }
  10093. //---------------------------------------------------------------------------
  10094. void __fastcall TCustomScpExplorerForm::RemoteStatusBarMouseDown(TObject *, TMouseButton, TShiftState, int, int)
  10095. {
  10096. CountClicksForWindowPrint(this);
  10097. }
  10098. //---------------------------------------------------------------------------
  10099. void __fastcall TCustomScpExplorerForm::RemoteDirViewResize(TObject *)
  10100. {
  10101. CenterReconnectToolbar();
  10102. }
  10103. //---------------------------------------------------------------------------
  10104. void __fastcall TCustomScpExplorerForm::BrowseFile()
  10105. {
  10106. if (RemoteDirView->ItemFocused != NULL)
  10107. {
  10108. RemoteDirView->ItemFocused->Selected = true;
  10109. }
  10110. }
  10111. //---------------------------------------------------------------------------
  10112. void __fastcall TCustomScpExplorerForm::CloseApp()
  10113. {
  10114. // Called from TNonVisualDataModule::ExplorerActionsExecute, which sets busy flag.
  10115. // FormCloseQuery must check busy state, so that the application cannot be closed while busy by Alt+F4 and X button.
  10116. // So we clear the flag here.
  10117. DebugAssert(NonVisualDataModule->Busy);
  10118. NonVisualDataModule->EndBusy();
  10119. try
  10120. {
  10121. Close();
  10122. }
  10123. __finally
  10124. {
  10125. NonVisualDataModule->StartBusy();
  10126. }
  10127. }