CustomScpExplorer.cpp 352 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737
  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[fcBackgroundTransfers], 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[fcBackgroundTransfers])
  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[fcSkipTransfer] : 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[fcBackgroundTransfers])
  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. }
  4296. }
  4297. if (FShowing || FStandaloneEditing)
  4298. {
  4299. if (FQueueStatusInvalidated)
  4300. {
  4301. UpdateQueueStatus(false);
  4302. }
  4303. RefreshQueueItems();
  4304. }
  4305. if (FShowing)
  4306. {
  4307. UpdateStatusBar();
  4308. }
  4309. FIgnoreNextDialogChar = 0;
  4310. }
  4311. //---------------------------------------------------------------------------
  4312. void __fastcall TCustomScpExplorerForm::UserActionTimer(TObject * /*Sender*/)
  4313. {
  4314. try
  4315. {
  4316. FUserActionTimer->Enabled = false;
  4317. if (IsQueueAutoPopup() && (FPendingQueueActionItem != NULL))
  4318. {
  4319. if (TQueueItem::IsUserActionStatus(FPendingQueueActionItem->Status))
  4320. {
  4321. FPendingQueueActionItem->ProcessUserAction();
  4322. }
  4323. }
  4324. }
  4325. __finally
  4326. {
  4327. FPendingQueueActionItem = NULL;
  4328. }
  4329. }
  4330. //---------------------------------------------------------------------------
  4331. void __fastcall TCustomScpExplorerForm::ApplicationMinimize(TObject * /*Sender*/)
  4332. {
  4333. if (WinConfiguration->MinimizeToTray)
  4334. {
  4335. UpdateTrayIcon();
  4336. FTrayIcon->Visible = true;
  4337. if (Visible)
  4338. {
  4339. ShowWindow(Handle, SW_HIDE);
  4340. }
  4341. }
  4342. }
  4343. //---------------------------------------------------------------------------
  4344. void __fastcall TCustomScpExplorerForm::ApplicationRestore(TObject * /*Sender*/)
  4345. {
  4346. // WORKAROUND
  4347. // When restoring maximized window from minimization,
  4348. // rarely some controls do not align properly.
  4349. // Two instances seen (both for Commander):
  4350. // - When restoring, window is temporarily narrower (not maximized),
  4351. // causing toolbars on TopDock to wrap and dock to expand horizontally.
  4352. // Once maximized already, top dock shinks back, but the session PageControl,
  4353. // do not align up, leaving space between TopDock and PageControl.
  4354. // - Similar issue seen with LocalDirView not aligning down to status bar.
  4355. for (int Index = 0; Index < ControlCount; Index++)
  4356. {
  4357. RealignControl(Controls[Index]);
  4358. }
  4359. if (FTrayIcon->Visible)
  4360. {
  4361. FTrayIcon->Visible = false;
  4362. if (Visible)
  4363. {
  4364. ShowWindow(Handle, SW_SHOW);
  4365. }
  4366. }
  4367. if (FNeedSession && DebugAlwaysTrue(Terminal == NULL))
  4368. {
  4369. FNeedSession = false;
  4370. NonVisualDataModule->StartBusy();
  4371. try
  4372. {
  4373. NeedSession(true);
  4374. }
  4375. __finally
  4376. {
  4377. NonVisualDataModule->EndBusy();
  4378. }
  4379. }
  4380. }
  4381. //---------------------------------------------------------------------------
  4382. void __fastcall TCustomScpExplorerForm::UpdateTrayIcon()
  4383. {
  4384. FTrayIcon->Hint = GetMainForm()->Caption;
  4385. }
  4386. //---------------------------------------------------------------------------
  4387. void __fastcall TCustomScpExplorerForm::ApplicationTitleChanged()
  4388. {
  4389. UpdateTrayIcon();
  4390. }
  4391. //---------------------------------------------------------------------------
  4392. void __fastcall TCustomScpExplorerForm::RestoreApp()
  4393. {
  4394. // workaround
  4395. // TApplication.WndProc sets TApplication.FAppIconic to false,
  4396. // on WM_ACTIVATEAPP, what renders TApplication.Restore no-op function.
  4397. // But WM_ACTIVATEAPP message can be received even when
  4398. // the main window is minimized to the tray and internal editor window is focused
  4399. // (after another application was previously active)
  4400. if (::IsIconic(Handle))
  4401. {
  4402. if (!IsAppIconic())
  4403. {
  4404. SetAppIconic(true);
  4405. }
  4406. }
  4407. ::ApplicationRestore();
  4408. Application->BringToFront();
  4409. }
  4410. //---------------------------------------------------------------------------
  4411. void __fastcall TCustomScpExplorerForm::TrayIconClick(TObject * /*Sender*/)
  4412. {
  4413. RestoreApp();
  4414. }
  4415. //---------------------------------------------------------------------------
  4416. void __fastcall TCustomScpExplorerForm::NewSession(bool FromSite, const UnicodeString & SessionUrl)
  4417. {
  4418. if (OpenInNewWindow())
  4419. {
  4420. // todo: Pass FromSite
  4421. ExecuteNewInstance(SessionUrl);
  4422. }
  4423. else
  4424. {
  4425. TTerminalManager::Instance()->NewSession(FromSite, SessionUrl);
  4426. }
  4427. }
  4428. //---------------------------------------------------------------------------
  4429. UnicodeString __fastcall TCustomScpExplorerForm::SaveHiddenDuplicateSession(TSessionData * SessionData)
  4430. {
  4431. UnicodeString SessionName = StoredSessions->HiddenPrefix + Terminal->SessionData->SessionName;
  4432. StoredSessions->NewSession(SessionName, SessionData);
  4433. // modified only, explicit
  4434. StoredSessions->Save(false, true);
  4435. // encode session name because of slashes in hierarchical sessions
  4436. return EncodeUrlString(SessionName);
  4437. }
  4438. //---------------------------------------------------------------------------
  4439. UnicodeString __fastcall TCustomScpExplorerForm::CreateHiddenDuplicateSession()
  4440. {
  4441. // current working directories become defaults here, what is not right
  4442. std::unique_ptr<TSessionData> SessionData(CloneCurrentSessionData());
  4443. return SaveHiddenDuplicateSession(SessionData.get());
  4444. }
  4445. //---------------------------------------------------------------------------
  4446. void __fastcall TCustomScpExplorerForm::DuplicateSession()
  4447. {
  4448. if (OpenInNewWindow())
  4449. {
  4450. ExecuteNewInstance(CreateHiddenDuplicateSession());
  4451. }
  4452. else
  4453. {
  4454. // current working directories become defaults here, what is not right
  4455. std::unique_ptr<TSessionData> SessionData(CloneCurrentSessionData());
  4456. TTerminalManager * Manager = TTerminalManager::Instance();
  4457. TManagedTerminal * Terminal = Manager->NewManagedTerminal(SessionData.get());
  4458. Manager->ActiveTerminal = Terminal;
  4459. }
  4460. }
  4461. //---------------------------------------------------------------------------
  4462. void __fastcall TCustomScpExplorerForm::RenameSession()
  4463. {
  4464. UnicodeString Name = Terminal->SessionData->SessionName;
  4465. if (InputDialog(LoadStr(RENAME_SESSION_TITLE), LoadStr(RENAME_SESSION_PROMPT), Name, HELP_SESSION_RENAME))
  4466. {
  4467. // Checks for a slash only, so it's not that big deal that we do the check after submitting the dialog only.
  4468. TSessionData::ValidateName(Name);
  4469. // This is inconsistent with how color (for example) is handled.
  4470. Terminal->SessionData->Name = Name;
  4471. UpdateControls();
  4472. // Add/Remove distinguishing paths from sessions of the same name.
  4473. DoTerminalListChanged();
  4474. }
  4475. }
  4476. //---------------------------------------------------------------------------
  4477. bool __fastcall TCustomScpExplorerForm::CanCloseQueue(TTerminalQueue * Queue)
  4478. {
  4479. DebugAssert(Queue != NULL);
  4480. bool Result = Queue->IsEmpty;
  4481. if (!Result)
  4482. {
  4483. SetFocus();
  4484. Result = (MessageDialog(LoadStr(PENDING_QUEUE_ITEMS2), qtWarning, qaOK | qaCancel, HELP_NONE) == qaOK);
  4485. }
  4486. return Result;
  4487. }
  4488. //---------------------------------------------------------------------------
  4489. bool __fastcall TCustomScpExplorerForm::CanCloseQueue()
  4490. {
  4491. return CanCloseQueue(FQueue);
  4492. }
  4493. //---------------------------------------------------------------------------
  4494. void __fastcall TCustomScpExplorerForm::CloseSession()
  4495. {
  4496. if (CanCloseQueue())
  4497. {
  4498. TTerminalManager::Instance()->FreeActiveTerminal();
  4499. }
  4500. }
  4501. //---------------------------------------------------------------------------
  4502. void __fastcall TCustomScpExplorerForm::DisconnectSession()
  4503. {
  4504. if (CanCloseQueue())
  4505. {
  4506. TTerminalManager::Instance()->DisconnectActiveTerminal();
  4507. }
  4508. }
  4509. //---------------------------------------------------------------------------
  4510. void __fastcall TCustomScpExplorerForm::TerminalDisconnected()
  4511. {
  4512. RemoteDirView->Terminal = Terminal;
  4513. UpdateRemotePathComboBox(false);
  4514. UpdateControls();
  4515. }
  4516. //---------------------------------------------------------------------------
  4517. void __fastcall TCustomScpExplorerForm::ReconnectSession()
  4518. {
  4519. if (DebugAlwaysTrue(!Terminal->Active))
  4520. {
  4521. TTerminalManager::Instance()->ReconnectActiveTerminal();
  4522. }
  4523. }
  4524. //---------------------------------------------------------------------------
  4525. void __fastcall TCustomScpExplorerForm::OpenStoredSession(TSessionData * Data)
  4526. {
  4527. if (OpenInNewWindow())
  4528. {
  4529. // encode session name because of slashes in hierarchical sessions
  4530. ExecuteNewInstance(EncodeUrlString(Data->Name));
  4531. }
  4532. else
  4533. {
  4534. TTerminalManager * Manager = TTerminalManager::Instance();
  4535. TManagedTerminal * Terminal = Manager->NewManagedTerminal(Data);
  4536. Manager->ActiveTerminal = Terminal;
  4537. }
  4538. }
  4539. //---------------------------------------------------------------------------
  4540. void __fastcall TCustomScpExplorerForm::DoOpenFolderOrWorkspace(const UnicodeString & Name, bool ConnectFirstTerminal)
  4541. {
  4542. TTerminalManager * Manager = TTerminalManager::Instance();
  4543. std::unique_ptr<TObjectList> DataList(new TObjectList());
  4544. StoredSessions->GetFolderOrWorkspace(Name, DataList.get());
  4545. TManagedTerminal * FirstTerminal = Manager->NewTerminals(DataList.get());
  4546. // FirstTerminal can be null, if some of the
  4547. if (!ConnectFirstTerminal && (FirstTerminal != NULL))
  4548. {
  4549. FirstTerminal->Disconnected = true;
  4550. FirstTerminal->DisconnectedTemporarily = true;
  4551. }
  4552. Manager->ActiveTerminal = FirstTerminal;
  4553. }
  4554. //---------------------------------------------------------------------------
  4555. void __fastcall TCustomScpExplorerForm::OpenFolderOrWorkspace(const UnicodeString & Name)
  4556. {
  4557. if (OpenInNewWindow())
  4558. {
  4559. ExecuteNewInstance(Name);
  4560. }
  4561. else
  4562. {
  4563. DoOpenFolderOrWorkspace(Name, true);
  4564. }
  4565. }
  4566. //---------------------------------------------------------------------------
  4567. void __fastcall TCustomScpExplorerForm::FormCloseQuery(TObject * /*Sender*/,
  4568. bool &CanClose)
  4569. {
  4570. if (Terminal != NULL)
  4571. {
  4572. if (Terminal->Active && WinConfiguration->ConfirmClosingSession)
  4573. {
  4574. unsigned int Result;
  4575. TMessageParams Params(mpNeverAskAgainCheck);
  4576. UnicodeString Message;
  4577. int Answers = qaOK | qaCancel;
  4578. if (TTerminalManager::Instance()->Count > 1)
  4579. {
  4580. if (!WinConfiguration->AutoSaveWorkspace)
  4581. {
  4582. Message = LoadStr(CLOSE_SESSIONS_WORKSPACE3);
  4583. }
  4584. else
  4585. {
  4586. Message = LoadStr(CLOSE_SESSIONS);
  4587. }
  4588. }
  4589. else
  4590. {
  4591. if (!WinConfiguration->AutoSaveWorkspace)
  4592. {
  4593. Message = LoadStr(CLOSE_SESSION_WORKSPACE);
  4594. }
  4595. else
  4596. {
  4597. Message = LoadStr(CLOSE_SESSION);
  4598. }
  4599. Message = FORMAT(Message, (Terminal->SessionData->SessionName));
  4600. }
  4601. UnicodeString Note;
  4602. if (WinConfiguration->AutoSaveWorkspace)
  4603. {
  4604. Note = FMTLOAD(AUTO_WORKSPACE, (WorkspaceName()));
  4605. }
  4606. else
  4607. {
  4608. Note = LoadStr(AUTO_WORKSPACE_ENABLE);
  4609. Answers = qaYes | qaNo | qaCancel;
  4610. }
  4611. Message = FORMAT("%s\n\n%s", (MainInstructions(Message), Note));
  4612. SetFocus();
  4613. Result = MessageDialog(Message, qtConfirmation,
  4614. Answers, HELP_CLOSE_SESSION_WORKSPACE, &Params);
  4615. if (Result == qaNeverAskAgain)
  4616. {
  4617. WinConfiguration->ConfirmClosingSession = false;
  4618. }
  4619. if (Result == qaNo)
  4620. {
  4621. CanClose = SaveWorkspace(true);
  4622. // note that the workspace will be saved redundatly again from FormClose
  4623. }
  4624. else
  4625. {
  4626. CanClose =
  4627. (Result == qaOK) ||
  4628. (Result == qaYes) || // CLOSE_SESSIONS_WORKSPACE variant
  4629. (Result == qaNeverAskAgain);
  4630. }
  4631. }
  4632. if (CanClose)
  4633. {
  4634. CanClose = CanCloseQueue();
  4635. }
  4636. }
  4637. if (CanClose)
  4638. {
  4639. CanClose =
  4640. FEditorManager->CloseInternalEditors(CloseInternalEditor) &&
  4641. FEditorManager->CloseExternalFilesWithoutProcess();
  4642. if (CanClose)
  4643. {
  4644. while (CanClose && (FLocalEditors->Count > 0))
  4645. {
  4646. int PrevCount = FLocalEditors->Count;
  4647. static_cast<TForm *>(FLocalEditors->Items[0])->Close();
  4648. CanClose = (FLocalEditors->Count < PrevCount);
  4649. }
  4650. if (CanClose)
  4651. {
  4652. CanClose = FEditorManager->Empty(true);
  4653. if (!CanClose)
  4654. {
  4655. SetFocus();
  4656. CanClose =
  4657. (MessageDialog(
  4658. LoadStr(PENDING_EDITORS), qtWarning, qaIgnore | qaCancel, HELP_NONE) == qaIgnore);
  4659. }
  4660. }
  4661. }
  4662. }
  4663. }
  4664. //---------------------------------------------------------------------------
  4665. void __fastcall TCustomScpExplorerForm::CloseInternalEditor(TObject * Sender)
  4666. {
  4667. TForm * Form = dynamic_cast<TForm *>(Sender);
  4668. DebugAssert(Form != NULL);
  4669. Form->Close();
  4670. }
  4671. //---------------------------------------------------------------------------
  4672. void __fastcall TCustomScpExplorerForm::ForceCloseInternalEditor(TObject * Sender)
  4673. {
  4674. TForm * Form = dynamic_cast<TForm *>(Sender);
  4675. delete Form;
  4676. }
  4677. //---------------------------------------------------------------------------
  4678. void __fastcall TCustomScpExplorerForm::ForceCloseLocalEditors()
  4679. {
  4680. while (FLocalEditors->Count > 0)
  4681. {
  4682. delete static_cast<TForm *>(FLocalEditors->Items[0]);
  4683. }
  4684. }
  4685. //---------------------------------------------------------------------------
  4686. void __fastcall TCustomScpExplorerForm::RemoteDirViewDisplayProperties(
  4687. TObject *Sender)
  4688. {
  4689. TStrings *FileList = ((TUnixDirView*)Sender)->CreateFileList(True, False, NULL);
  4690. try
  4691. {
  4692. SetProperties(osRemote, FileList);
  4693. }
  4694. __finally
  4695. {
  4696. delete FileList;
  4697. }
  4698. }
  4699. //---------------------------------------------------------------------------
  4700. void __fastcall TCustomScpExplorerForm::ComponentShowing(Byte Component, bool value)
  4701. {
  4702. if (value)
  4703. {
  4704. if (Component == fcCustomCommandsBand)
  4705. {
  4706. UpdateCustomCommandsToolbar();
  4707. }
  4708. }
  4709. }
  4710. //---------------------------------------------------------------------------
  4711. void __fastcall TCustomScpExplorerForm::SetComponentVisible(Byte Component, Boolean value)
  4712. {
  4713. TControl * Control = GetComponent(Component);
  4714. DebugAssert(Control);
  4715. bool Changed = (Control->Visible != value);
  4716. if (Changed)
  4717. {
  4718. ComponentShowing(Component, value);
  4719. TWinControl * WinControl = dynamic_cast<TWinControl*>(Control);
  4720. bool WasFocused = (WinControl != NULL) && (ActiveControl != NULL) &&
  4721. ((ActiveControl == WinControl) || (ActiveControl->Parent == WinControl));
  4722. if (value)
  4723. {
  4724. int RemainingHeight = Control->Parent->ClientHeight;
  4725. int RemainingWidth = Control->Parent->ClientWidth;
  4726. for (int i = 0; i < Control->Parent->ControlCount; i++)
  4727. {
  4728. TControl * ChildControl = Control->Parent->Controls[i];
  4729. if (ChildControl->Visible)
  4730. {
  4731. switch (ChildControl->Align)
  4732. {
  4733. case alTop:
  4734. case alBottom:
  4735. RemainingHeight -= ChildControl->Height;
  4736. break;
  4737. case alLeft:
  4738. case alRight:
  4739. RemainingWidth -= ChildControl->Width;
  4740. break;
  4741. }
  4742. }
  4743. }
  4744. int Reserve = ScaleByTextHeight(this, 32);
  4745. // queue in explorer, trees in commander
  4746. if (Control->Height > RemainingHeight - Reserve)
  4747. {
  4748. Control->Height = RemainingHeight / 2;
  4749. }
  4750. if (Control->Width > RemainingWidth - Reserve)
  4751. {
  4752. Control->Width = RemainingWidth / 2;
  4753. }
  4754. }
  4755. Control->Visible = value;
  4756. if (WasFocused && Visible)
  4757. {
  4758. DirView(osCurrent)->SetFocus();
  4759. }
  4760. FixControlsPlacement();
  4761. }
  4762. }
  4763. //---------------------------------------------------------------------------
  4764. bool __fastcall TCustomScpExplorerForm::GetComponentVisible(Byte Component)
  4765. {
  4766. TControl * Control = GetComponent(Component);
  4767. if (Control == NULL)
  4768. {
  4769. return false;
  4770. }
  4771. else
  4772. {
  4773. return Control->Visible;
  4774. }
  4775. }
  4776. //---------------------------------------------------------------------------
  4777. bool __fastcall TCustomScpExplorerForm::IsComponentPossible(Byte Component)
  4778. {
  4779. bool Result;
  4780. if ((Component == fcExplorerUpdatesBand) || (Component == fcCommanderUpdatesBand))
  4781. {
  4782. Result = !IsUWP();
  4783. }
  4784. else
  4785. {
  4786. Result = true;
  4787. }
  4788. return Result;
  4789. }
  4790. //---------------------------------------------------------------------------
  4791. void __fastcall TCustomScpExplorerForm::FixControlsPlacement()
  4792. {
  4793. if (DirView(osOther)->ItemFocused != NULL)
  4794. {
  4795. DirView(osOther)->ItemFocused->MakeVisible(false);
  4796. }
  4797. QueueSplitter->Visible = QueuePanel->Visible;
  4798. RemotePanelSplitter->Visible = RemoteDrivePanel->Visible;
  4799. }
  4800. //---------------------------------------------------------------------------
  4801. TControl * __fastcall TCustomScpExplorerForm::GetComponent(Byte Component)
  4802. {
  4803. switch (Component) {
  4804. case fcStatusBar: return RemoteStatusBar;
  4805. case fcRemotePopup: return reinterpret_cast<TControl *>(NonVisualDataModule->RemoteFilePopup);
  4806. case fcQueueView: return QueuePanel;
  4807. case fcQueueToolbar: return QueueDock;
  4808. case fcRemoteTree: return RemoteDrivePanel;
  4809. case fcSessionsTabs: return SessionsPageControl;
  4810. default: return NULL;
  4811. }
  4812. }
  4813. //---------------------------------------------------------------------------
  4814. void __fastcall TCustomScpExplorerForm::DirViewColumnRightClick(
  4815. TObject *Sender, TListColumn *Column, TPoint &Point)
  4816. {
  4817. DebugAssert(NonVisualDataModule && Column && Sender);
  4818. NonVisualDataModule->ListColumn = Column;
  4819. TPopupMenu * DirViewColumnMenu;
  4820. if (dynamic_cast<TUnixDirView *>(Sender) != NULL)
  4821. {
  4822. DirViewColumnMenu = NonVisualDataModule->RemoteDirViewColumnPopup;
  4823. NonVisualDataModule->RemoteSortByExtColumnPopupItem->Visible =
  4824. (Column->Index == uvName);
  4825. NonVisualDataModule->RemoteFormatSizeBytesPopupItem->Visible =
  4826. (Column->Index == uvSize);
  4827. }
  4828. else
  4829. {
  4830. DirViewColumnMenu = NonVisualDataModule->LocalDirViewColumnPopup;
  4831. NonVisualDataModule->LocalSortByExtColumnPopupItem->Visible =
  4832. (Column->Index == dvName);
  4833. NonVisualDataModule->LocalFormatSizeBytesPopupItem->Visible =
  4834. (Column->Index == dvSize);
  4835. }
  4836. TCustomListView * ListView = DebugNotNull(dynamic_cast<TCustomListView *>(Sender));
  4837. TPoint ScreenPoint = ListView->ClientToScreen(Point);
  4838. ScreenPoint.x -= GetScrollPos(ListView->Handle, SB_HORZ);
  4839. DirViewColumnMenu->Popup(ScreenPoint.x, ScreenPoint.y);
  4840. }
  4841. //---------------------------------------------------------------------------
  4842. void __fastcall TCustomScpExplorerForm::DirViewExecFile(
  4843. TObject *Sender, TListItem *Item, bool &AllowExec)
  4844. {
  4845. DoDirViewExecFile(Sender, Item, AllowExec);
  4846. }
  4847. //---------------------------------------------------------------------------
  4848. void __fastcall TCustomScpExplorerForm::DoDirViewExecFile(TObject * Sender,
  4849. TListItem * Item, bool & AllowExec)
  4850. {
  4851. DebugAssert(Sender && Item && Configuration);
  4852. DebugAssert(AllowExec);
  4853. TCustomDirView * ADirView = (TCustomDirView *)Sender;
  4854. bool Remote = (ADirView == DirView(osRemote)) && !IsSideLocalBrowser(osRemote);
  4855. bool ResolvedSymlinks = !Remote || Terminal->ResolvingSymlinks;
  4856. TOperationSide Side = (Remote ? osRemote : osLocal); // TODO
  4857. // Anything special is done on double click only (not on "open" indicated by FForceExecution),
  4858. // on files only (not directories)
  4859. // and only when symlinks are resolved (apply to remote panel only)
  4860. if (!ADirView->ItemIsDirectory(Item) &&
  4861. (ResolvedSymlinks || FForceExecution || Terminal->IsEncryptingFiles()))
  4862. {
  4863. if ((WinConfiguration->DoubleClickAction != dcaOpen) &&
  4864. !FForceExecution &&
  4865. (ResolvedSymlinks || Terminal->IsEncryptingFiles()))
  4866. {
  4867. if (WinConfiguration->DoubleClickAction == dcaCopy)
  4868. {
  4869. // To counter the lock in DirViewBusy (called from TCustomDirView.DoExecute)
  4870. UnlockWindow();
  4871. try
  4872. {
  4873. ExecuteFileOperation(
  4874. foCopy, Side, true, !WinConfiguration->CopyOnDoubleClickConfirmation);
  4875. }
  4876. __finally
  4877. {
  4878. LockWindow();
  4879. }
  4880. AllowExec = false;
  4881. }
  4882. else if (WinConfiguration->DoubleClickAction == dcaEdit)
  4883. {
  4884. if (!Remote || !WinConfiguration->DisableOpenEdit)
  4885. {
  4886. ExecuteFile(Side, efDefaultEditor);
  4887. AllowExec = false;
  4888. }
  4889. }
  4890. else
  4891. {
  4892. DebugFail();
  4893. }
  4894. }
  4895. // if we have not done anything special, fall back to default behavior
  4896. if (AllowExec)
  4897. {
  4898. if (Remote && !WinConfiguration->DisableOpenEdit)
  4899. {
  4900. ExecuteFile(osRemote, efShell);
  4901. AllowExec = false;
  4902. }
  4903. }
  4904. }
  4905. }
  4906. //---------------------------------------------------------------------------
  4907. bool __fastcall TCustomScpExplorerForm::GetHasDirView(TOperationSide Side)
  4908. {
  4909. return ((Side == osRemote) || (Side == osCurrent));
  4910. }
  4911. //---------------------------------------------------------------------------
  4912. void __fastcall TCustomScpExplorerForm::CompareDirectories()
  4913. {
  4914. DebugFail();
  4915. }
  4916. //---------------------------------------------------------------------------
  4917. void __fastcall TCustomScpExplorerForm::SynchronizeDirectories()
  4918. {
  4919. DebugFail();
  4920. }
  4921. //---------------------------------------------------------------------------
  4922. bool __fastcall TCustomScpExplorerForm::DoSynchronizeDirectories(
  4923. UnicodeString & LocalDirectory, UnicodeString & RemoteDirectory, int UseDefaults)
  4924. {
  4925. TSynchronizeParamType Params;
  4926. Params.LocalDirectory = LocalDirectory;
  4927. Params.RemoteDirectory = RemoteDirectory;
  4928. int UnusedParams =
  4929. (GUIConfiguration->SynchronizeParams &
  4930. (TTerminal::spPreviewChanges | TTerminal::spTimestamp | TTerminal::spNotByTime | TTerminal::spBySize));
  4931. Params.Params = GUIConfiguration->SynchronizeParams & ~UnusedParams;
  4932. Params.Options = GUIConfiguration->SynchronizeOptions;
  4933. bool SaveSettings = false;
  4934. TSynchronizeController Controller(&DoSynchronize, &DoSynchronizeInvalid,
  4935. &DoSynchronizeTooManyDirectories);
  4936. DebugAssert(FSynchronizeController == NULL);
  4937. FSynchronizeController = &Controller;
  4938. bool Result;
  4939. try
  4940. {
  4941. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  4942. int CopyParamAttrs = Terminal->UsableCopyParamAttrs(0).Upload;
  4943. int Options =
  4944. FLAGMASK(SynchronizeAllowSelectedOnly(), soAllowSelectedOnly);
  4945. DebugAssert(FOnFeedSynchronizeError == NULL);
  4946. Result = DoSynchronizeDialog(Params, &CopyParam, Controller.StartStop,
  4947. SaveSettings, Options, CopyParamAttrs, GetSynchronizeOptions, SynchronizeSessionLog,
  4948. FOnFeedSynchronizeError, SynchronizeInNewWindow, UseDefaults);
  4949. if (Result)
  4950. {
  4951. if (SaveSettings)
  4952. {
  4953. GUIConfiguration->SynchronizeParams = Params.Params | UnusedParams;
  4954. GUIConfiguration->SynchronizeOptions = Params.Options;
  4955. }
  4956. else
  4957. {
  4958. if (FLAGSET(GUIConfiguration->SynchronizeOptions, soSynchronizeAsk) &&
  4959. FLAGCLEAR(Params.Options, soSynchronizeAsk) &&
  4960. FLAGSET(Params.Options, soSynchronize))
  4961. {
  4962. GUIConfiguration->SynchronizeOptions =
  4963. (GUIConfiguration->SynchronizeOptions & ~soSynchronizeAsk) |
  4964. soSynchronize;
  4965. }
  4966. }
  4967. LocalDirectory = Params.LocalDirectory;
  4968. RemoteDirectory = Params.RemoteDirectory;
  4969. }
  4970. }
  4971. __finally
  4972. {
  4973. FSynchronizeController = NULL;
  4974. DebugAssert(FOnFeedSynchronizeError == NULL);
  4975. FOnFeedSynchronizeError = NULL;
  4976. }
  4977. return Result;
  4978. }
  4979. //---------------------------------------------------------------------------
  4980. void __fastcall TCustomScpExplorerForm::DoSynchronize(
  4981. TSynchronizeController * /*Sender*/, const UnicodeString LocalDirectory,
  4982. const UnicodeString RemoteDirectory, const TCopyParamType & CopyParam,
  4983. const TSynchronizeParamType & Params, TSynchronizeChecklist ** Checklist,
  4984. TSynchronizeOptions * Options, bool Full)
  4985. {
  4986. // Keep-up-to-date mode
  4987. if (Terminal->Status == ssOpened)
  4988. {
  4989. try
  4990. {
  4991. int PParams = Params.Params;
  4992. if (!Full)
  4993. {
  4994. PParams |= TTerminal::spNoRecurse | TTerminal::spUseCache | TTerminal::spDelayProgress;
  4995. if (FLAGSET(Params.Options, soRecurse))
  4996. {
  4997. PParams |= TTerminal::spSubDirs;
  4998. }
  4999. }
  5000. else
  5001. {
  5002. // if keepuptodate is non-recursive,
  5003. // full sync before has to be non-recursive as well
  5004. if (FLAGCLEAR(Params.Options, soRecurse))
  5005. {
  5006. PParams |= TTerminal::spNoRecurse;
  5007. }
  5008. }
  5009. Synchronize(LocalDirectory, RemoteDirectory, smRemote, CopyParam,
  5010. PParams, Checklist, Options);
  5011. }
  5012. catch (Exception & E)
  5013. {
  5014. if (FLAGSET(Params.Options, soContinueOnError) && Terminal->Active &&
  5015. DebugAlwaysTrue(FOnFeedSynchronizeError != NULL))
  5016. {
  5017. // noop, already logged in MoreMessageDialog
  5018. }
  5019. else
  5020. {
  5021. ShowExtendedExceptionEx(Terminal, &E);
  5022. throw;
  5023. }
  5024. }
  5025. }
  5026. }
  5027. //---------------------------------------------------------------------------
  5028. void __fastcall TCustomScpExplorerForm::DoSynchronizeInvalid(
  5029. TSynchronizeController * /*Sender*/, const UnicodeString Directory,
  5030. const UnicodeString ErrorStr)
  5031. {
  5032. if (!Directory.IsEmpty())
  5033. {
  5034. SimpleErrorDialog(FMTLOAD(WATCH_ERROR_DIRECTORY, (Directory)), ErrorStr);
  5035. }
  5036. else
  5037. {
  5038. SimpleErrorDialog(LoadStr(WATCH_ERROR_GENERAL), ErrorStr);
  5039. }
  5040. }
  5041. //---------------------------------------------------------------------------
  5042. void __fastcall TCustomScpExplorerForm::DoSynchronizeTooManyDirectories(
  5043. TSynchronizeController * /*Sender*/, int & MaxDirectories)
  5044. {
  5045. if (MaxDirectories < GUIConfiguration->MaxWatchDirectories)
  5046. {
  5047. MaxDirectories = GUIConfiguration->MaxWatchDirectories;
  5048. }
  5049. else
  5050. {
  5051. TMessageParams Params(mpNeverAskAgainCheck);
  5052. unsigned int Result = MessageDialog(
  5053. FMTLOAD(TOO_MANY_WATCH_DIRECTORIES, (MaxDirectories, MaxDirectories)),
  5054. qtConfirmation, qaYes | qaNo, HELP_TOO_MANY_WATCH_DIRECTORIES, &Params);
  5055. if ((Result == qaYes) || (Result == qaNeverAskAgain))
  5056. {
  5057. MaxDirectories *= 2;
  5058. if (Result == qaNeverAskAgain)
  5059. {
  5060. GUIConfiguration->MaxWatchDirectories = MaxDirectories;
  5061. }
  5062. }
  5063. else
  5064. {
  5065. Abort();
  5066. }
  5067. }
  5068. }
  5069. //---------------------------------------------------------------------------
  5070. void __fastcall TCustomScpExplorerForm::Synchronize(const UnicodeString LocalDirectory,
  5071. const UnicodeString RemoteDirectory, TSynchronizeMode Mode,
  5072. const TCopyParamType & CopyParam, int Params, TSynchronizeChecklist ** Checklist,
  5073. TSynchronizeOptions * Options)
  5074. {
  5075. // Keep-up-to-date mode
  5076. DebugAssert(!FAutoOperation);
  5077. void * BatchStorage;
  5078. BatchStart(BatchStorage);
  5079. FAutoOperation = true;
  5080. bool AnyOperation = false;
  5081. TDateTime StartTime = Now();
  5082. TSynchronizeChecklist * AChecklist = NULL;
  5083. try
  5084. {
  5085. FSynchronizeProgressForm = new TSynchronizeProgressForm(Application, true, -1);
  5086. if (FLAGCLEAR(Params, TTerminal::spDelayProgress))
  5087. {
  5088. FSynchronizeProgressForm->Start();
  5089. }
  5090. AChecklist = Terminal->SynchronizeCollect(LocalDirectory, RemoteDirectory,
  5091. static_cast<TTerminal::TSynchronizeMode>(Mode),
  5092. &CopyParam, Params | TTerminal::spNoConfirmation, TerminalSynchronizeDirectory,
  5093. Options);
  5094. SetTaskbarListProgressState(TBPF_NOPROGRESS);
  5095. SAFE_DESTROY(FSynchronizeProgressForm);
  5096. AnyOperation = (AChecklist->CheckedCount > 0);
  5097. if (AnyOperation)
  5098. {
  5099. TSynchronizeProgress SynchronizeProgress(AChecklist);
  5100. CreateProgressForm(&SynchronizeProgress);
  5101. try
  5102. {
  5103. Terminal->SynchronizeApply(
  5104. AChecklist, &CopyParam, Params | TTerminal::spNoConfirmation, TerminalSynchronizeDirectory,
  5105. SynchronizeProcessedItem, NULL, NULL, NULL);
  5106. }
  5107. __finally
  5108. {
  5109. DestroyProgressForm();
  5110. }
  5111. }
  5112. }
  5113. __finally
  5114. {
  5115. if (Checklist == NULL)
  5116. {
  5117. delete AChecklist;
  5118. }
  5119. else
  5120. {
  5121. *Checklist = AChecklist;
  5122. }
  5123. FAutoOperation = false;
  5124. SAFE_DESTROY(FSynchronizeProgressForm);
  5125. BatchEnd(BatchStorage);
  5126. ReloadLocalDirectory();
  5127. if (AnyOperation)
  5128. {
  5129. OperationComplete(StartTime);
  5130. }
  5131. }
  5132. }
  5133. //---------------------------------------------------------------------------
  5134. bool __fastcall TCustomScpExplorerForm::SynchronizeAllowSelectedOnly()
  5135. {
  5136. // can be called from command line
  5137. return Visible &&
  5138. ((DirView(osRemote)->SelCount > 0) ||
  5139. (HasDirView[osLocal] && (DirView(osLocal)->SelCount > 0)));
  5140. }
  5141. //---------------------------------------------------------------------------
  5142. void __fastcall TCustomScpExplorerForm::SynchronizeSessionLog(const UnicodeString & Message)
  5143. {
  5144. LogSynchronizeEvent(Terminal, Message);
  5145. }
  5146. //---------------------------------------------------------------------------
  5147. void __fastcall TCustomScpExplorerForm::GetSynchronizeOptions(
  5148. int Params, TSynchronizeOptions & Options)
  5149. {
  5150. DebugAssert(!IsLocalBrowserMode());
  5151. if (FLAGSET(Params, TTerminal::spSelectedOnly) && SynchronizeAllowSelectedOnly())
  5152. {
  5153. Options.Filter = new TStringList();
  5154. Options.Filter->CaseSensitive = false;
  5155. Options.Filter->Duplicates = Types::dupAccept;
  5156. if (DirView(osRemote)->SelCount > 0)
  5157. {
  5158. DirView(osRemote)->CreateFileList(false, false, Options.Filter);
  5159. }
  5160. if (HasDirView[osLocal] && (DirView(osLocal)->SelCount > 0))
  5161. {
  5162. DirView(osLocal)->CreateFileList(false, false, Options.Filter);
  5163. }
  5164. Options.Filter->Sort();
  5165. }
  5166. }
  5167. //---------------------------------------------------------------------------
  5168. UnicodeString __fastcall TCustomScpExplorerForm::SerializeCopyParamForCommandLine(const TCopyParamType * CopyParams)
  5169. {
  5170. TCopyParamType Defaults;
  5171. std::unique_ptr<THierarchicalStorage> ConfigStorage(Configuration->CreateConfigStorage());
  5172. ConfigStorage->AccessMode = smRead;
  5173. if (ConfigStorage->OpenSubKey(Configuration->ConfigurationSubKey, false))
  5174. {
  5175. GUIConfiguration->LoadCopyParam(ConfigStorage.get(), &Defaults);
  5176. }
  5177. ConfigStorage.reset(NULL);
  5178. std::unique_ptr<TStringList> Options(new TStringList());
  5179. std::unique_ptr<TOptionsStorage> OptionsStorage(new TOptionsStorage(Options.get(), true));
  5180. CopyParams->Save(OptionsStorage.get(), &Defaults);
  5181. UnicodeString Result;
  5182. if (Options->Count > 0)
  5183. {
  5184. Result =
  5185. FORMAT(L" %s%s", (TProgramParams::FormatSwitch(RAWTRANSFERSETTINGS_SWITCH), StringsToParams(Options.get())));
  5186. }
  5187. return Result;
  5188. }
  5189. //---------------------------------------------------------------------------
  5190. void __fastcall TCustomScpExplorerForm::SynchronizeInNewWindow(
  5191. const TSynchronizeParamType & Params, const TCopyParamType * CopyParams)
  5192. {
  5193. UnicodeString SessionName = CreateHiddenDuplicateSession();
  5194. UnicodeString AdditionalParams =
  5195. FORMAT(L"%s \"%s\" \"%s\" %d %d %s%s", (
  5196. TProgramParams::FormatSwitch(KEEP_UP_TO_DATE_SWITCH),
  5197. Params.LocalDirectory, Params.RemoteDirectory, Params.Params, Params.Options,
  5198. TProgramParams::FormatSwitch(DEFAULTS_SWITCH),
  5199. SerializeCopyParamForCommandLine(CopyParams)));
  5200. ExecuteNewInstance(SessionName, AdditionalParams);
  5201. }
  5202. //---------------------------------------------------------------------------
  5203. struct TSynchronizeParams
  5204. {
  5205. UnicodeString LocalDirectory;
  5206. UnicodeString RemoteDirectory;
  5207. TSynchronizeMode Mode;
  5208. TSynchronizeChecklist * Checklist;
  5209. int Params;
  5210. TCopyParamType * CopyParam;
  5211. TDateTime CollectElapsed;
  5212. TDateTime * StartTime;
  5213. TProcessedSynchronizationChecklistItem OnProcessedItem;
  5214. };
  5215. //---------------------------------------------------------------------------
  5216. void __fastcall TCustomScpExplorerForm::FullSynchronize(
  5217. TSynchronizeParams & Params, TProcessedSynchronizationChecklistItem OnProcessedItem,
  5218. TUpdatedSynchronizationChecklistItems OnUpdatedSynchronizationChecklistItems)
  5219. {
  5220. DebugAssert(!FAutoOperation);
  5221. void * BatchStorage;
  5222. BatchStart(BatchStorage);
  5223. FAutoOperation = true;
  5224. TFileOperationStatistics Statistics;
  5225. TDateTime Start = Now();
  5226. try
  5227. {
  5228. Params.OnProcessedItem = OnProcessedItem;
  5229. TSynchronizeProgress SynchronizeProgress(Params.Checklist);
  5230. CreateProgressForm(&SynchronizeProgress);
  5231. Terminal->SynchronizeApply(
  5232. Params.Checklist, Params.CopyParam, Params.Params | TTerminal::spNoConfirmation,
  5233. TerminalSynchronizeDirectory, SynchronizeProcessedItem, OnUpdatedSynchronizationChecklistItems, &Params,
  5234. &Statistics);
  5235. }
  5236. __finally
  5237. {
  5238. Params.OnProcessedItem = NULL;
  5239. FAutoOperation = false;
  5240. DestroyProgressForm();
  5241. BatchEnd(BatchStorage);
  5242. ReloadLocalDirectory();
  5243. }
  5244. if (WinConfiguration->SynchronizeSummary)
  5245. {
  5246. UnicodeString Message = MainInstructions(LoadStr(SYNCHRONIZE_COMPLETE)) + L"\n";
  5247. // The statistics should be 0 anyway in this case
  5248. if (FLAGCLEAR(Params.Params, TTerminal::spTimestamp))
  5249. {
  5250. Message += L"\n";
  5251. if (Statistics.FilesUploaded > 0)
  5252. {
  5253. Message += FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 1), (FormatNumber(Statistics.FilesUploaded), FormatBytes(Statistics.TotalUploaded))) + L"\n";
  5254. }
  5255. if (Statistics.FilesDownloaded > 0)
  5256. {
  5257. Message += FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 2), (FormatNumber(Statistics.FilesDownloaded), FormatBytes(Statistics.TotalDownloaded))) + L"\n";
  5258. }
  5259. if (Statistics.FilesDeletedLocal > 0)
  5260. {
  5261. Message += FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 3), (FormatNumber(Statistics.FilesDeletedLocal))) + L"\n";
  5262. }
  5263. if (Statistics.FilesDeletedRemote > 0)
  5264. {
  5265. Message += FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 4), (FormatNumber(Statistics.FilesDeletedRemote))) + L"\n";
  5266. }
  5267. }
  5268. TDateTime Elapsed = (Now() - Start);
  5269. Message +=
  5270. L"\n" +
  5271. FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 5), (FormatDateTimeSpan(Configuration->TimeFormat, Params.CollectElapsed))) + L"\n" +
  5272. FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 6), (FormatDateTimeSpan(Configuration->TimeFormat, Elapsed)));
  5273. TMessageParams Params(mpNeverAskAgainCheck);
  5274. unsigned int Result = MessageDialog(Message, qtInformation, qaOK, HELP_NONE, &Params);
  5275. if (Result == qaNeverAskAgain)
  5276. {
  5277. WinConfiguration->SynchronizeSummary = false;
  5278. }
  5279. }
  5280. }
  5281. //---------------------------------------------------------------------------
  5282. void __fastcall TCustomScpExplorerForm::SynchronizeProcessedItem(void * Token, const TSynchronizeChecklist::TItem * ChecklistItem)
  5283. {
  5284. if (DebugAlwaysTrue(FProgressForm != NULL) && DebugAlwaysTrue(FProgressForm->SynchronizeProgress != NULL))
  5285. {
  5286. FProgressForm->SynchronizeProgress->ItemProcessed(ChecklistItem);
  5287. }
  5288. // Not set in keep-up-to-date mode - Synchronize() method
  5289. if (Token != NULL)
  5290. {
  5291. TSynchronizeParams & Params = *static_cast<TSynchronizeParams *>(Token);
  5292. if (Params.OnProcessedItem != NULL)
  5293. {
  5294. Params.OnProcessedItem(NULL, ChecklistItem);
  5295. }
  5296. }
  5297. }
  5298. //---------------------------------------------------------------------------
  5299. void __fastcall TCustomScpExplorerForm::DoFullSynchronize(
  5300. void * Token, TProcessedSynchronizationChecklistItem OnProcessedItem,
  5301. TUpdatedSynchronizationChecklistItems OnUpdatedSynchronizationChecklistItems)
  5302. {
  5303. TSynchronizeParams & Params = *static_cast<TSynchronizeParams *>(Token);
  5304. *Params.StartTime = Now();
  5305. FullSynchronize(Params, OnProcessedItem, OnUpdatedSynchronizationChecklistItems);
  5306. }
  5307. //---------------------------------------------------------------------------
  5308. void __fastcall TCustomScpExplorerForm::DoSynchronizeChecklistCalculateSize(
  5309. TSynchronizeChecklist * Checklist, const TSynchronizeChecklist::TItemList & Items, void * Token)
  5310. {
  5311. // terminal can be already closed (e.g. dropped connection)
  5312. if (Terminal != NULL)
  5313. {
  5314. TSynchronizeParams & Params = *static_cast<TSynchronizeParams *>(Token);
  5315. Terminal->SynchronizeChecklistCalculateSize(Checklist, Items, Params.CopyParam);
  5316. }
  5317. }
  5318. //---------------------------------------------------------------------------
  5319. void __fastcall TCustomScpExplorerForm::DoSynchronizeMove(
  5320. TOperationSide Side, const UnicodeString & FileName, const UnicodeString & NewFileName, TRemoteFile * RemoteFile)
  5321. {
  5322. DebugAssert(!IsLocalBrowserMode());
  5323. TAutoBatch AutoBatch(this);
  5324. TAutoFlag AutoOperationFlag(FAutoOperation);
  5325. if (Side == osRemote)
  5326. {
  5327. std::unique_ptr<TStrings> FileList(new TStringList());
  5328. FileList->AddObject(FileName, RemoteFile);
  5329. UnicodeString Target = UnixExtractFileDir(NewFileName);
  5330. UnicodeString FileMask = DelimitFileNameMask(UnixExtractFileName(NewFileName));
  5331. RemoteDirView->SaveSelection();
  5332. RemoteDirView->SaveSelectedNames();
  5333. try
  5334. {
  5335. Terminal->MoveFiles(FileList.get(), Target, FileMask);
  5336. }
  5337. catch(...)
  5338. {
  5339. RemoteDirView->DiscardSavedSelection();
  5340. throw;
  5341. }
  5342. RemoteDirView->RestoreSelection();
  5343. }
  5344. else if (DebugAlwaysTrue(Side == osLocal))
  5345. {
  5346. if (!MoveFile(FileName.c_str(), NewFileName.c_str()))
  5347. {
  5348. throw EOSExtException(FMTLOAD(RENAME_FILE_ERROR, (FileName, NewFileName)));
  5349. }
  5350. UnicodeString Directory = ExtractFileDir(FileName);
  5351. ReloadLocalDirectory(Directory);
  5352. UnicodeString NewDirectory = ExtractFileDir(NewFileName);
  5353. if (!SamePaths(Directory, NewDirectory))
  5354. {
  5355. ReloadLocalDirectory(NewDirectory);
  5356. }
  5357. }
  5358. }
  5359. //---------------------------------------------------------------------------
  5360. void __fastcall TCustomScpExplorerForm::DoSynchronizeBrowse(TOperationSide Side, TSynchronizeChecklist::TAction Action, const TSynchronizeChecklist::TItem * Item)
  5361. {
  5362. UnicodeString LocalPath = ExcludeTrailingBackslash(Item->Local.Directory);
  5363. if (Side == osLocal)
  5364. {
  5365. if (Action == TSynchronizeChecklist::saDownloadNew)
  5366. {
  5367. OpenFolderInExplorer(LocalPath);
  5368. }
  5369. else
  5370. {
  5371. OpenFileInExplorer(TPath::Combine(LocalPath, Item->GetFileName()));
  5372. }
  5373. }
  5374. else if (DebugAlwaysTrue(Side == osRemote))
  5375. {
  5376. // Similar to CreateHiddenDuplicateSession, except that it modifies the initial directories
  5377. std::unique_ptr<TSessionData> SessionData(CloneCurrentSessionData());
  5378. SessionData->RemoteDirectory = UnixExcludeTrailingBackslash(Item->Remote.Directory);
  5379. if (!LocalPath.IsEmpty())
  5380. {
  5381. SessionData->LocalDirectory = LocalPath;
  5382. }
  5383. UnicodeString SessionName = SaveHiddenDuplicateSession(SessionData.get());
  5384. ExecuteNewInstance(SessionName, FORMAT(L"%s=%s", (TProgramParams::FormatSwitch(BROWSE_SWITCH), Item->GetFileName())));
  5385. }
  5386. }
  5387. //---------------------------------------------------------------------------
  5388. void __fastcall TCustomScpExplorerForm::FullSynchronizeInNewWindow(
  5389. TSynchronizeMode Mode, int Params, const UnicodeString & LocalDirectory, const UnicodeString & RemoteDirectory,
  5390. const TCopyParamType * CopyParams)
  5391. {
  5392. UnicodeString SessionName = CreateHiddenDuplicateSession();
  5393. UnicodeString AdditionalParams =
  5394. FORMAT(L"%s \"%s\" \"%s\" %d %d %s%s", (
  5395. TProgramParams::FormatSwitch(SYNCHRONIZE_SWITCH),
  5396. LocalDirectory, RemoteDirectory, Mode, Params,
  5397. TProgramParams::FormatSwitch(DEFAULTS_SWITCH),
  5398. SerializeCopyParamForCommandLine(CopyParams)));
  5399. ExecuteNewInstance(SessionName, AdditionalParams);
  5400. }
  5401. //---------------------------------------------------------------------------
  5402. int __fastcall TCustomScpExplorerForm::DoFullSynchronizeDirectories(
  5403. UnicodeString & LocalDirectory, UnicodeString & RemoteDirectory,
  5404. TSynchronizeMode & Mode, int Params, bool & SaveMode, int UseDefaults)
  5405. {
  5406. int Result;
  5407. bool SaveSettings = false;
  5408. int Options =
  5409. FLAGMASK(!Terminal->IsCapable[fcTimestampChanging], fsoDisableTimestamp) |
  5410. FLAGMASK(SynchronizeAllowSelectedOnly(), fsoAllowSelectedOnly);
  5411. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  5412. TUsableCopyParamAttrs CopyParamAttrs = Terminal->UsableCopyParamAttrs(0);
  5413. bool Continue =
  5414. (UseDefaults == 0) ||
  5415. DoFullSynchronizeDialog(Mode, Params, LocalDirectory, RemoteDirectory,
  5416. &CopyParam, SaveSettings, SaveMode, Options, CopyParamAttrs, FullSynchronizeInNewWindow, UseDefaults);
  5417. if (Continue)
  5418. {
  5419. Configuration->Usage->Inc(L"Synchronizations");
  5420. UpdateCopyParamCounters(CopyParam);
  5421. TSynchronizeOptions SynchronizeOptions;
  5422. GetSynchronizeOptions(Params, SynchronizeOptions);
  5423. if (SaveSettings)
  5424. {
  5425. GUIConfiguration->SynchronizeParams = Params;
  5426. }
  5427. else
  5428. {
  5429. SaveMode = false;
  5430. }
  5431. TDateTime StartTime = Now();
  5432. TSynchronizeChecklist * Checklist = NULL;
  5433. try
  5434. {
  5435. DebugAssert(!FAutoOperation);
  5436. FAutoOperation = true;
  5437. try
  5438. {
  5439. UnicodeString SessionKey = Terminal->SessionData->SessionKey;
  5440. std::unique_ptr<TStrings> DataList(Configuration->LoadDirectoryStatisticsCache(SessionKey, RemoteDirectory, CopyParam));
  5441. int Files = -1;
  5442. if (DataList->Count >= 1)
  5443. {
  5444. Files = StrToIntDef(DataList->Strings[0], Files);
  5445. }
  5446. else
  5447. {
  5448. DataList->Add(UnicodeString());
  5449. }
  5450. FSynchronizeProgressForm = new TSynchronizeProgressForm(Application, true, Files);
  5451. FSynchronizeProgressForm->Start();
  5452. Checklist = Terminal->SynchronizeCollect(LocalDirectory, RemoteDirectory,
  5453. static_cast<TTerminal::TSynchronizeMode>(Mode),
  5454. &CopyParam, Params | TTerminal::spNoConfirmation, TerminalSynchronizeDirectory,
  5455. &SynchronizeOptions);
  5456. if (Terminal->SessionData->CacheDirectories)
  5457. {
  5458. DataList->Strings[0] = IntToStr(SynchronizeOptions.Files);
  5459. Configuration->SaveDirectoryStatisticsCache(SessionKey, RemoteDirectory, CopyParam, DataList.get());
  5460. }
  5461. }
  5462. __finally
  5463. {
  5464. FAutoOperation = false;
  5465. SetTaskbarListProgressState(TBPF_NOPROGRESS);
  5466. SAFE_DESTROY(FSynchronizeProgressForm);
  5467. }
  5468. TSynchronizeParams SynchronizeParams;
  5469. SynchronizeParams.LocalDirectory = LocalDirectory;
  5470. SynchronizeParams.RemoteDirectory = RemoteDirectory;
  5471. SynchronizeParams.Mode = Mode;
  5472. SynchronizeParams.CopyParam = &CopyParam;
  5473. SynchronizeParams.Params = Params;
  5474. SynchronizeParams.Checklist = Checklist;
  5475. SynchronizeParams.CollectElapsed = (Now() - StartTime);
  5476. SynchronizeParams.StartTime = &StartTime;
  5477. SynchronizeParams.OnProcessedItem = NULL;
  5478. Result = Checklist->Count;
  5479. if (Checklist->Count > 0)
  5480. {
  5481. if (FLAGSET(Params, TTerminal::spPreviewChanges))
  5482. {
  5483. if (!DoSynchronizeChecklistDialog(
  5484. Checklist, Mode, Params, LocalDirectory, RemoteDirectory, CustomCommandMenu, DoFullSynchronize,
  5485. DoSynchronizeChecklistCalculateSize, DoSynchronizeMove, DoSynchronizeBrowse, &SynchronizeParams))
  5486. {
  5487. Result = -1;
  5488. }
  5489. }
  5490. else
  5491. {
  5492. FullSynchronize(SynchronizeParams, NULL, NULL);
  5493. }
  5494. }
  5495. else
  5496. {
  5497. UnicodeString Message = MainInstructions(LoadStr(COMPARE_NO_DIFFERENCES));
  5498. MessageDialog(Message, qtInformation, qaOK, HELP_SYNCHRONIZE_NO_DIFFERENCES);
  5499. }
  5500. }
  5501. __finally
  5502. {
  5503. delete Checklist;
  5504. }
  5505. OperationComplete(StartTime);
  5506. }
  5507. else
  5508. {
  5509. Result = -1;
  5510. }
  5511. return Result;
  5512. }
  5513. //---------------------------------------------------------------------------
  5514. void __fastcall TCustomScpExplorerForm::TerminalSynchronizeDirectory(
  5515. const UnicodeString & LocalDirectory, const UnicodeString & RemoteDirectory,
  5516. bool & Continue, bool Collect, const TSynchronizeOptions * Options)
  5517. {
  5518. if (Collect)
  5519. {
  5520. DebugAssert(FSynchronizeProgressForm != NULL);
  5521. if (!FSynchronizeProgressForm->Started)
  5522. {
  5523. FSynchronizeProgressForm->Start();
  5524. }
  5525. int CompareProgress = FSynchronizeProgressForm->SetData(LocalDirectory, RemoteDirectory, Options->Files, Continue);
  5526. SetTaskbarListProgressValue(CompareProgress);
  5527. }
  5528. else
  5529. {
  5530. DebugAssert(FSynchronizeProgressForm == NULL);
  5531. DebugAssert(FProgressForm != NULL);
  5532. }
  5533. }
  5534. //---------------------------------------------------------------------------
  5535. void __fastcall TCustomScpExplorerForm::StandaloneEdit(const UnicodeString & FileName)
  5536. {
  5537. UnicodeString FullFileName = AbsolutePath(FTerminal->CurrentDirectory, FileName);
  5538. TRemoteFile * File = NULL;
  5539. Terminal->ReadFile(FullFileName, File);
  5540. if (File != NULL)
  5541. {
  5542. std::unique_ptr<TRemoteFile> FileOwner(File);
  5543. TAutoFlag Flag(FStandaloneEditing);
  5544. ExecuteRemoteFile(FullFileName, File, efInternalEditor);
  5545. Application->ShowMainForm = false;
  5546. Application->Run();
  5547. }
  5548. }
  5549. //---------------------------------------------------------------------------
  5550. void __fastcall TCustomScpExplorerForm::ExploreLocalDirectory()
  5551. {
  5552. DebugFail();
  5553. }
  5554. //---------------------------------------------------------------------------
  5555. TSessionData * __fastcall TCustomScpExplorerForm::CloneCurrentSessionData()
  5556. {
  5557. std::unique_ptr<TSessionData> SessionData(new TSessionData(L""));
  5558. SessionData->Assign(Terminal->SessionData);
  5559. UpdateSessionData(SessionData.get());
  5560. Terminal->UpdateSessionCredentials(SessionData.get());
  5561. if (!Terminal->SessionData->HasSessionName())
  5562. {
  5563. // Particularly for "Workspace/XXXX" name, we need to reset it, as it would become user-visible
  5564. // once IsWorkspace is cleared
  5565. SessionData->Name = UnicodeString();
  5566. SessionData->IsWorkspace = false;
  5567. }
  5568. return SessionData.release();
  5569. }
  5570. //---------------------------------------------------------------------------
  5571. void __fastcall TCustomScpExplorerForm::SaveCurrentSession()
  5572. {
  5573. TSessionData * SessionData = CloneCurrentSessionData();
  5574. try
  5575. {
  5576. TSessionData * EditingSessionData = StoredSessions->FindSame(SessionData);
  5577. DoSaveSession(SessionData, EditingSessionData, true, NULL);
  5578. }
  5579. __finally
  5580. {
  5581. delete SessionData;
  5582. }
  5583. }
  5584. //---------------------------------------------------------------------------
  5585. TObjectList * __fastcall TCustomScpExplorerForm::DoCollectWorkspace()
  5586. {
  5587. TTerminalManager * Manager = TTerminalManager::Instance();
  5588. std::unique_ptr<TObjectList> DataList(new TObjectList());
  5589. if (DebugAlwaysTrue(Terminal != NULL))
  5590. {
  5591. // Update (Managed)Terminal->StateData
  5592. UpdateTerminal(Terminal);
  5593. }
  5594. Manager->SaveWorkspace(DataList.get());
  5595. return DataList.release();
  5596. }
  5597. //---------------------------------------------------------------------------
  5598. void __fastcall TCustomScpExplorerForm::DoSaveWorkspace(
  5599. const UnicodeString & Name, TObjectList * DataList, bool SavePasswords, bool Explicit)
  5600. {
  5601. WinConfiguration->LastWorkspace = Name;
  5602. if (!SavePasswords)
  5603. {
  5604. for (int Index = 0; Index < DataList->Count; Index++)
  5605. {
  5606. TSessionData * SessionData = dynamic_cast<TSessionData *>(DataList->Items[Index]);
  5607. if (SessionData->Link.IsEmpty())
  5608. {
  5609. SessionData->ClearSessionPasswords();
  5610. }
  5611. }
  5612. }
  5613. StoredSessions->NewWorkspace(Name, DataList);
  5614. // modified only
  5615. StoredSessions->Save(false, Explicit);
  5616. }
  5617. //---------------------------------------------------------------------------
  5618. UnicodeString __fastcall TCustomScpExplorerForm::WorkspaceName()
  5619. {
  5620. return
  5621. DefaultStr(WinConfiguration->LastWorkspace,
  5622. DefaultStr(WinConfiguration->AutoWorkspace, LoadStr(NEW_WORKSPACE)));
  5623. }
  5624. //---------------------------------------------------------------------------
  5625. bool __fastcall TCustomScpExplorerForm::SaveWorkspace(bool EnableAutoSave)
  5626. {
  5627. std::unique_ptr<TObjectList> DataList(DoCollectWorkspace());
  5628. bool AnyNonStoredSessionWithPassword = false;
  5629. bool AnyNonStoredNonWorkspaceSessionWithPassword = false;
  5630. bool AllNonStoredSessionsAnonymous = true;
  5631. TTerminalManager * Manager = TTerminalManager::Instance();
  5632. for (int Index = 0; Index < DataList->Count; Index++)
  5633. {
  5634. TSessionData * SessionData = dynamic_cast<TSessionData *>(DataList->Items[Index]);
  5635. if (SessionData->Link.IsEmpty())
  5636. {
  5637. if (SessionData->HasAnySessionPassword())
  5638. {
  5639. AnyNonStoredSessionWithPassword = true;
  5640. if (!Manager->Terminals[Index]->SessionData->IsWorkspace)
  5641. {
  5642. AnyNonStoredNonWorkspaceSessionWithPassword = true;
  5643. }
  5644. }
  5645. if (!SameText(SessionData->UserName, AnonymousUserName))
  5646. {
  5647. AllNonStoredSessionsAnonymous = false;
  5648. }
  5649. }
  5650. }
  5651. bool SavePasswords;
  5652. bool * PSavePasswords;
  5653. bool NotRecommendedSavingPasswords =
  5654. !CustomWinConfiguration->UseMasterPassword &&
  5655. !AllNonStoredSessionsAnonymous;
  5656. if (Configuration->DisablePasswordStoring ||
  5657. !AnyNonStoredSessionWithPassword)
  5658. {
  5659. PSavePasswords = NULL;
  5660. SavePasswords = false;
  5661. }
  5662. else
  5663. {
  5664. PSavePasswords = &SavePasswords;
  5665. SavePasswords =
  5666. !AnyNonStoredNonWorkspaceSessionWithPassword ||
  5667. !NotRecommendedSavingPasswords;
  5668. }
  5669. UnicodeString Name = WorkspaceName();
  5670. bool CreateShortcut = false;
  5671. bool Result =
  5672. DoSaveWorkspaceDialog(
  5673. Name, PSavePasswords, NotRecommendedSavingPasswords, CreateShortcut,
  5674. EnableAutoSave);
  5675. if (Result)
  5676. {
  5677. DoSaveWorkspace(Name, DataList.get(), SavePasswords, true);
  5678. if (CreateShortcut)
  5679. {
  5680. TOperationVisualizer Visualizer;
  5681. UnicodeString AdditionalParams = TProgramParams::FormatSwitch(DESKTOP_SWITCH);
  5682. CreateDesktopSessionShortCut(Name, L"", AdditionalParams, -1, WORKSPACE_ICON);
  5683. }
  5684. if (EnableAutoSave)
  5685. {
  5686. WinConfiguration->AutoSaveWorkspace = true;
  5687. WinConfiguration->AutoWorkspace = Name;
  5688. if (PSavePasswords != NULL)
  5689. {
  5690. WinConfiguration->AutoSaveWorkspacePasswords = SavePasswords;
  5691. }
  5692. }
  5693. }
  5694. return Result;
  5695. }
  5696. //---------------------------------------------------------------------------
  5697. void __fastcall TCustomScpExplorerForm::UpdateTerminal(TManagedTerminal * Terminal)
  5698. {
  5699. DebugAssert(!IsLocalBrowserMode());
  5700. SAFE_DESTROY(Terminal->RemoteExplorerState);
  5701. if (WinConfiguration->PreservePanelState)
  5702. {
  5703. Terminal->RemoteExplorerState = RemoteDirView->SaveState();
  5704. }
  5705. UpdateSessionData(Terminal->StateData);
  5706. }
  5707. //---------------------------------------------------------------------------
  5708. void __fastcall TCustomScpExplorerForm::UpdateSessionData(TSessionData * Data)
  5709. {
  5710. // Keep in sync with TSessionData::CopyStateData
  5711. DebugAssert(!IsLocalBrowserMode());
  5712. DebugAssert(Data != NULL);
  5713. // This is inconsistent with how color (for example) is handled.
  5714. Data->Name = Terminal->SessionData->Name;
  5715. // cannot use RemoteDirView->Path, because it is empty if connection
  5716. // was already closed
  5717. // also only peek, we may not be connected at all atm
  5718. // so make sure we do not try retrieving current directory from the server
  5719. // (particularly with FTP)
  5720. UnicodeString ACurrentDirectory = Terminal->PeekCurrentDirectory();
  5721. if (!ACurrentDirectory.IsEmpty())
  5722. {
  5723. Data->RemoteDirectory = ACurrentDirectory;
  5724. }
  5725. Data->Color = SessionColor;
  5726. }
  5727. //---------------------------------------------------------------------------
  5728. void __fastcall TCustomScpExplorerForm::ToolBarResize(TObject *Sender)
  5729. {
  5730. TTBXToolbar * Toolbar = dynamic_cast<TTBXToolbar*>(Sender);
  5731. DebugAssert(Toolbar != NULL);
  5732. for (int i = 0; i < Toolbar->Items->Count; i++)
  5733. {
  5734. TTBXCustomDropDownItem * DropDownItem;
  5735. DropDownItem = dynamic_cast<TTBXCustomDropDownItem *>(Toolbar->Items->Items[i]);
  5736. if (DropDownItem != NULL)
  5737. {
  5738. ToolbarItemResize(DropDownItem,
  5739. Toolbar->Width - (Toolbar->View->BaseSize.x - DropDownItem->EditWidth) -
  5740. Toolbar->NonClientWidth);
  5741. break;
  5742. }
  5743. }
  5744. }
  5745. //---------------------------------------------------------------------------
  5746. void __fastcall TCustomScpExplorerForm::ToolbarItemResize(TTBXCustomDropDownItem * Item, int Width)
  5747. {
  5748. Item->EditWidth = Width;
  5749. }
  5750. //---------------------------------------------------------------------------
  5751. void __fastcall TCustomScpExplorerForm::ToolbarGetBaseSize(
  5752. TTBCustomToolbar * Toolbar, TPoint & ASize)
  5753. {
  5754. for (int i = 0; i < Toolbar->Items->Count; i++)
  5755. {
  5756. TTBXCustomDropDownItem * DropDownItem;
  5757. DropDownItem = dynamic_cast<TTBXCustomDropDownItem *>(Toolbar->Items->Items[i]);
  5758. if (DropDownItem != NULL)
  5759. {
  5760. ASize.x -= DropDownItem->EditWidth;
  5761. ASize.x += ScaleByTextHeight(this, 50) /* minimal combo width */;
  5762. }
  5763. }
  5764. }
  5765. //---------------------------------------------------------------------------
  5766. void __fastcall TCustomScpExplorerForm::DoWarnLackOfTempSpace(
  5767. const UnicodeString Path, __int64 RequiredSpace, bool & Continue)
  5768. {
  5769. if (WinConfiguration->DDWarnLackOfTempSpace)
  5770. {
  5771. UnicodeString ADrive = ExtractFileDrive(ExpandFileName(Path));
  5772. if (!ADrive.IsEmpty())
  5773. {
  5774. __int64 FreeSpace = DiskFree((Byte)(ADrive[1]-'A'+1));
  5775. DebugAssert(RequiredSpace >= 0);
  5776. __int64 RequiredWithReserve;
  5777. RequiredWithReserve = (__int64)(RequiredSpace * WinConfiguration->DDWarnLackOfTempSpaceRatio);
  5778. if (FreeSpace < RequiredWithReserve)
  5779. {
  5780. unsigned int Result;
  5781. TMessageParams Params(mpNeverAskAgainCheck);
  5782. Result = MessageDialog(FMTLOAD(DD_WARN_LACK_OF_TEMP_SPACE, (Path,
  5783. FormatBytes(FreeSpace), FormatBytes(RequiredSpace))),
  5784. qtWarning, qaYes | qaNo, HELP_DD_WARN_LACK_OF_TEMP_SPACE, &Params);
  5785. if (Result == qaNeverAskAgain)
  5786. {
  5787. WinConfiguration->DDWarnLackOfTempSpace = false;
  5788. }
  5789. Continue = (Result == qaYes || Result == qaNeverAskAgain);
  5790. }
  5791. }
  5792. }
  5793. }
  5794. //---------------------------------------------------------------------------
  5795. void __fastcall TCustomScpExplorerForm::AddBookmark(TOperationSide Side)
  5796. {
  5797. DoOpenDirectoryDialog(odAddBookmark, Side);
  5798. }
  5799. //---------------------------------------------------------------------------
  5800. TStrings * __fastcall TCustomScpExplorerForm::CreateVisitedDirectories(TOperationSide Side)
  5801. {
  5802. // we should better use TCustomDirView::FCaseSensitive, but it is private
  5803. TStringList * VisitedDirectories = CreateSortedStringList(!IsSideLocalBrowser(Side));
  5804. try
  5805. {
  5806. TCustomDirView * DView = DirView(Side);
  5807. for (int Index = -DView->BackCount; Index <= DView->ForwardCount; Index++)
  5808. {
  5809. VisitedDirectories->Add(DView->HistoryPath[Index]);
  5810. }
  5811. }
  5812. catch (...)
  5813. {
  5814. delete VisitedDirectories;
  5815. throw;
  5816. }
  5817. return VisitedDirectories;
  5818. }
  5819. //---------------------------------------------------------------------------
  5820. void __fastcall TCustomScpExplorerForm::DoOpenDirectoryDialog(
  5821. TOpenDirectoryMode Mode, TOperationSide Side)
  5822. {
  5823. bool Continue = true;
  5824. if (Mode == odAddBookmark)
  5825. {
  5826. TMessageParams Params(mpNeverAskAgainCheck);
  5827. Params.NeverAskAgainTitle = LoadStr(ADD_BOOKMARK_SHARED);
  5828. Params.NeverAskAgainCheckedInitially = WinConfiguration->UseSharedBookmarks;
  5829. UnicodeString Message = MainInstructions(FMTLOAD(ADD_BOOKMARK_CONFIRM, (DirView(Side)->PathName)));
  5830. unsigned int Answer =
  5831. MessageDialog(Message,
  5832. qtConfirmation, qaOK | qaCancel, HELP_ADD_BOOKMARK_CONFIRM, &Params);
  5833. if (Answer == qaNeverAskAgain)
  5834. {
  5835. Continue = true;
  5836. WinConfiguration->UseSharedBookmarks = true;
  5837. }
  5838. else if (Answer == qaOK)
  5839. {
  5840. Continue = true;
  5841. WinConfiguration->UseSharedBookmarks = false;
  5842. }
  5843. else
  5844. {
  5845. Continue = false;
  5846. }
  5847. }
  5848. if (Continue)
  5849. {
  5850. TStrings * VisitedDirectories = CreateVisitedDirectories(Side);
  5851. try
  5852. {
  5853. UnicodeString Name = DirView(Side)->PathName;
  5854. bool Repeat;
  5855. do
  5856. {
  5857. Repeat = false;
  5858. if (::DoOpenDirectoryDialog(Mode, Side, Name, VisitedDirectories, Terminal,
  5859. // do not allow switching to location profiles,
  5860. // if we are not connected
  5861. HasDirView[osLocal] && (Terminal != NULL)))
  5862. {
  5863. TWindowLock Lock(this);
  5864. if (!TryOpenDirectory(Side, Name))
  5865. {
  5866. Repeat = true;
  5867. Mode = odBrowse;
  5868. }
  5869. }
  5870. }
  5871. while (Repeat);
  5872. }
  5873. __finally
  5874. {
  5875. delete VisitedDirectories;
  5876. }
  5877. }
  5878. }
  5879. //---------------------------------------------------------------------------
  5880. bool __fastcall TCustomScpExplorerForm::CommandSessionFallback()
  5881. {
  5882. DebugAssert(!FTerminal->CommandSessionOpened);
  5883. return TTerminalManager::Instance()->ConnectTerminal(FTerminal->CommandSession);
  5884. }
  5885. //---------------------------------------------------------------------------
  5886. bool __fastcall TCustomScpExplorerForm::EnsureCommandSessionFallback(TFSCapability Capability)
  5887. {
  5888. bool Result = FTerminal->IsCapable[Capability] ||
  5889. FTerminal->CommandSessionOpened;
  5890. if (!Result)
  5891. {
  5892. DebugAssert(FTerminal->IsCapable[fcSecondaryShell]);
  5893. if (!GUIConfiguration->ConfirmCommandSession)
  5894. {
  5895. Result = true;
  5896. }
  5897. else
  5898. {
  5899. TMessageParams Params(mpNeverAskAgainCheck);
  5900. const TFileSystemInfo & FileSystemInfo = Terminal->GetFileSystemInfo();
  5901. unsigned int Answer = MessageDialog(FMTLOAD(PERFORM_ON_COMMAND_SESSION2,
  5902. (FileSystemInfo.ProtocolName, FileSystemInfo.ProtocolName)), qtConfirmation,
  5903. qaOK | qaCancel, HELP_PERFORM_ON_COMMAND_SESSION, &Params);
  5904. if (Answer == qaNeverAskAgain)
  5905. {
  5906. GUIConfiguration->ConfirmCommandSession = false;
  5907. Result = true;
  5908. }
  5909. else if (Answer == qaOK)
  5910. {
  5911. Result = true;
  5912. }
  5913. }
  5914. if (Result)
  5915. {
  5916. Result = CommandSessionFallback();
  5917. }
  5918. }
  5919. return Result;
  5920. }
  5921. //---------------------------------------------------------------------------
  5922. void __fastcall TCustomScpExplorerForm::OpenConsole(UnicodeString Command)
  5923. {
  5924. if (EnsureCommandSessionFallback(fcAnyCommand))
  5925. {
  5926. DoConsoleDialog(Terminal, Command);
  5927. }
  5928. }
  5929. //---------------------------------------------------------------------------
  5930. void __fastcall TCustomScpExplorerForm::FileControlDDDragEnter(
  5931. TObject *Sender, _di_IDataObject /*DataObj*/, int /*grfKeyState*/,
  5932. const TPoint & /*Point*/, int & /*dwEffect*/, bool & Accept)
  5933. {
  5934. if (!IsSideLocalBrowser(osRemote) &&
  5935. IsFileControl(DropSourceControl, osRemote) &&
  5936. (FDDExtMapFile != NULL))
  5937. {
  5938. Accept = true;
  5939. }
  5940. FDDTargetControl = dynamic_cast<TControl*>(Sender);
  5941. DebugAssert(FDDTargetControl != NULL);
  5942. }
  5943. //---------------------------------------------------------------------------
  5944. void __fastcall TCustomScpExplorerForm::SessionsDDDragEnter(
  5945. _di_IDataObject DataObj, int KeyState,
  5946. const TPoint & Point, int & Effect, bool & Accept)
  5947. {
  5948. FileControlDDDragEnter(SessionsPageControl, DataObj, KeyState, Point, Effect, Accept);
  5949. }
  5950. //---------------------------------------------------------------------------
  5951. void __fastcall TCustomScpExplorerForm::QueueDDDragEnter(
  5952. _di_IDataObject DataObj, int KeyState,
  5953. const TPoint & Point, int & Effect, bool & Accept)
  5954. {
  5955. FileControlDDDragEnter(QueueView3, DataObj, KeyState, Point, Effect, Accept);
  5956. }
  5957. //---------------------------------------------------------------------------
  5958. void __fastcall TCustomScpExplorerForm::FileControlDDDragLeave(
  5959. TObject * Sender)
  5960. {
  5961. DebugUsedParam(Sender);
  5962. DebugAssert(FDDTargetControl == Sender);
  5963. FDDTargetControl = NULL;
  5964. }
  5965. //---------------------------------------------------------------------------
  5966. void __fastcall TCustomScpExplorerForm::SessionsDDDragLeave()
  5967. {
  5968. FileControlDDDragLeave(SessionsPageControl);
  5969. }
  5970. //---------------------------------------------------------------------------
  5971. void __fastcall TCustomScpExplorerForm::QueueDDDragLeave()
  5972. {
  5973. FileControlDDDragLeave(QueueView3);
  5974. }
  5975. //---------------------------------------------------------------------------
  5976. void __fastcall TCustomScpExplorerForm::AddEditLink(TOperationSide Side, bool Add)
  5977. {
  5978. DebugAssert(!IsSideLocalBrowser(Side));
  5979. DebugUsedParam(Side);
  5980. bool Edit = false;
  5981. TRemoteFile * File = NULL;
  5982. UnicodeString FileName;
  5983. UnicodeString PointTo;
  5984. bool SymbolicLink = true;
  5985. if (RemoteDirView->ItemFocused)
  5986. {
  5987. DebugAssert(RemoteDirView->ItemFocused->Data);
  5988. File = (TRemoteFile *)RemoteDirView->ItemFocused->Data;
  5989. Edit = !Add && File->IsSymLink && Terminal->SessionData->ResolveSymlinks;
  5990. if (Edit)
  5991. {
  5992. FileName = File->FileName;
  5993. PointTo = File->LinkTo;
  5994. }
  5995. else
  5996. {
  5997. PointTo = File->FileName;
  5998. }
  5999. }
  6000. if (DoSymlinkDialog(FileName, PointTo, osRemote, SymbolicLink, Edit,
  6001. Terminal->IsCapable[fcHardLink]))
  6002. {
  6003. Configuration->Usage->Inc(L"RemoteLinksCreated");
  6004. if (Edit)
  6005. {
  6006. DebugAssert(File->FileName == FileName);
  6007. int Params = dfNoRecursive;
  6008. Terminal->ExceptionOnFail = true;
  6009. try
  6010. {
  6011. Terminal->DeleteFile(L"", File, &Params);
  6012. }
  6013. __finally
  6014. {
  6015. Terminal->ExceptionOnFail = false;
  6016. }
  6017. }
  6018. Terminal->CreateLink(FileName, PointTo, SymbolicLink);
  6019. }
  6020. }
  6021. //---------------------------------------------------------------------------
  6022. bool __fastcall TCustomScpExplorerForm::CanAddEditLink(TOperationSide Side)
  6023. {
  6024. return
  6025. (IsSideLocalBrowser(Side) ||
  6026. ((Terminal != NULL) &&
  6027. Terminal->Active &&
  6028. Terminal->ResolvingSymlinks &&
  6029. Terminal->IsCapable[fcSymbolicLink]));
  6030. }
  6031. //---------------------------------------------------------------------------
  6032. bool __fastcall TCustomScpExplorerForm::LinkFocused()
  6033. {
  6034. return
  6035. !IsSideLocalBrowser(FCurrentSide) &&
  6036. (RemoteDirView->ItemFocused != NULL) &&
  6037. ((TRemoteFile *)RemoteDirView->ItemFocused->Data)->IsSymLink &&
  6038. Terminal->SessionData->ResolveSymlinks;
  6039. }
  6040. //---------------------------------------------------------------------------
  6041. void __fastcall TCustomScpExplorerForm::ExecuteCurrentFile()
  6042. {
  6043. DebugAssert(!WinConfiguration->DisableOpenEdit);
  6044. FForceExecution = true;
  6045. try
  6046. {
  6047. DirView(osCurrent)->ExecuteCurrentFile();
  6048. }
  6049. __finally
  6050. {
  6051. FForceExecution = false;
  6052. }
  6053. }
  6054. //---------------------------------------------------------------------------
  6055. void __fastcall TCustomScpExplorerForm::ExecuteCurrentFileWith(bool OnFocused)
  6056. {
  6057. TEditorData ExternalEditor;
  6058. ExternalEditor.Editor = edExternal;
  6059. bool Remember = false;
  6060. if (DoEditorPreferencesDialog(&ExternalEditor, Remember, epmAdHoc, !IsSideLocalBrowser(osCurrent)))
  6061. {
  6062. if (Remember)
  6063. {
  6064. TEditorList * EditorList = new TEditorList;
  6065. try
  6066. {
  6067. *EditorList = *WinConfiguration->EditorList;
  6068. bool Found = false;
  6069. int i = 0;
  6070. while (!Found && (i < EditorList->Count))
  6071. {
  6072. const TEditorPreferences * Editor = EditorList->Editors[i];
  6073. if ((Editor->Data->Editor == edExternal) &&
  6074. (Editor->Data->ExternalEditor == ExternalEditor.ExternalEditor))
  6075. {
  6076. Found = true;
  6077. }
  6078. i++;
  6079. }
  6080. if (!Found)
  6081. {
  6082. EditorList->Add(new TEditorPreferences(ExternalEditor));
  6083. WinConfiguration->EditorList = EditorList;
  6084. }
  6085. }
  6086. __finally
  6087. {
  6088. delete EditorList;
  6089. }
  6090. }
  6091. ExecuteFile(osCurrent, efExternalEditor, &ExternalEditor, true, OnFocused);
  6092. }
  6093. }
  6094. //---------------------------------------------------------------------------
  6095. void __fastcall TCustomScpExplorerForm::TerminalRemoved(TObject * Sender)
  6096. {
  6097. FEditorManager->ProcessFiles(FileTerminalRemoved, Sender);
  6098. if (FFileFindTerminal == Sender)
  6099. {
  6100. FFileFindTerminal = NULL;
  6101. HideFileFindDialog();
  6102. }
  6103. if (FClipboardTerminal == Sender)
  6104. {
  6105. ClipboardClear(); // implies ClipboardStop
  6106. }
  6107. }
  6108. //---------------------------------------------------------------------------
  6109. void __fastcall TCustomScpExplorerForm::FileTerminalRemoved(const UnicodeString FileName,
  6110. TEditedFileData * Data, TObject * /*Token*/, void * Arg)
  6111. {
  6112. TTerminal * Terminal = static_cast<TTerminal *>(Arg);
  6113. DebugAssert(Terminal != NULL);
  6114. if (Data->Terminal == Terminal)
  6115. {
  6116. Data->Terminal = NULL;
  6117. }
  6118. }
  6119. //---------------------------------------------------------------------------
  6120. void __fastcall TCustomScpExplorerForm::LastTerminalClosed(TObject * /*Sender*/)
  6121. {
  6122. UpdateControls();
  6123. SessionColor = TColor(0);
  6124. UpdateRemotePathComboBox(false);
  6125. NeedSession(false);
  6126. }
  6127. //---------------------------------------------------------------------------
  6128. void __fastcall TCustomScpExplorerForm::NeedSession(bool Startup)
  6129. {
  6130. try
  6131. {
  6132. // Cache, as the login dialog can change its value
  6133. bool ShowLogin = WinConfiguration->ShowLoginWhenNoSession;
  6134. try
  6135. {
  6136. if (ShowLogin)
  6137. {
  6138. bool ReloadSessions = !Startup;
  6139. TTerminalManager::Instance()->NewSession(false, L"", ReloadSessions, this);
  6140. }
  6141. else if (Startup && WinConfiguration->AutoSaveWorkspace && !WinConfiguration->AutoWorkspace.IsEmpty() &&
  6142. // This detects if workspace was saved the last time the main widow was closed
  6143. SameText(WinConfiguration->LastStoredSession, WinConfiguration->AutoWorkspace))
  6144. {
  6145. DoOpenFolderOrWorkspace(WinConfiguration->AutoWorkspace, false);
  6146. Configuration->Usage->Inc(L"OpenedWorkspacesAuto");
  6147. }
  6148. }
  6149. __finally
  6150. {
  6151. // Do not terminate, if we are only starting up and we are not showing Login dialog
  6152. // (so there was no chance for the user to open any session yet)
  6153. if ((ShowLogin || !Startup) &&
  6154. !WinConfiguration->KeepOpenWhenNoSession &&
  6155. ((Terminal == NULL) || (!Terminal->Active && !Terminal->Permanent)))
  6156. {
  6157. TerminateApplication();
  6158. }
  6159. }
  6160. }
  6161. catch (EAbort &)
  6162. {
  6163. // swallow
  6164. // The TTerminalManager does not expect the OnLastTerminalClose to throw without trying to connect.
  6165. // Also when called from TWinControl.UpdateShowing => CMShowingChanged, the showing is aborted on exception
  6166. }
  6167. }
  6168. //---------------------------------------------------------------------------
  6169. void __fastcall TCustomScpExplorerForm::DoTerminalListChanged()
  6170. {
  6171. TStrings * TerminalList = TTerminalManager::Instance()->TerminalList;
  6172. int ActiveTerminalIndex = TTerminalManager::Instance()->ActiveTerminalIndex;
  6173. Configuration->Usage->SetMax(L"MaxOpenedSessions", TerminalList->Count);
  6174. SendMessage(SessionsPageControl->Handle, WM_SETREDRAW, 0, 0);
  6175. try
  6176. {
  6177. while ((SessionsPageControl->PageCount > TerminalList->Count + 1) ||
  6178. // Clear the design time unthemed tab
  6179. ((SessionsPageControl->PageCount > 0) &&
  6180. (dynamic_cast<TThemeTabSheet *>(SessionsPageControl->Pages[SessionsPageControl->PageCount - 1]) == NULL)))
  6181. {
  6182. delete SessionsPageControl->Pages[SessionsPageControl->PageCount - 1];
  6183. }
  6184. for (int Index = 0; Index <= TerminalList->Count; Index++)
  6185. {
  6186. TThemeTabSheet * TabSheet;
  6187. if (Index >= SessionsPageControl->PageCount)
  6188. {
  6189. TabSheet = new TThemeTabSheet(SessionsPageControl);
  6190. TabSheet->PageControl = SessionsPageControl;
  6191. }
  6192. else
  6193. {
  6194. TabSheet = DebugNotNull(dynamic_cast<TThemeTabSheet *>(SessionsPageControl->Pages[Index]));
  6195. }
  6196. bool IsSessionTab = (Index < TerminalList->Count);
  6197. if (IsSessionTab)
  6198. {
  6199. TTerminal * Terminal = dynamic_cast<TTerminal *>(TerminalList->Objects[Index]);
  6200. TabSheet->Tag = reinterpret_cast<int>(Terminal);
  6201. UpdateSessionTab(TabSheet);
  6202. }
  6203. else
  6204. {
  6205. TabSheet->ImageIndex = FNewSessionTabImageIndex;
  6206. TabSheet->Tag = 0; // not really needed
  6207. TabSheet->Shadowed = false;
  6208. // 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)
  6209. UpdateNewSessionTab();
  6210. }
  6211. TabSheet->ShowCloseButton = IsSessionTab;
  6212. }
  6213. }
  6214. __finally
  6215. {
  6216. SendMessage(SessionsPageControl->Handle, WM_SETREDRAW, 1, 0);
  6217. }
  6218. SessionsPageControl->ActivePageIndex = ActiveTerminalIndex;
  6219. }
  6220. //---------------------------------------------------------------------------
  6221. void __fastcall TCustomScpExplorerForm::TerminalListChanged(TObject * /*Sender*/)
  6222. {
  6223. DoTerminalListChanged();
  6224. }
  6225. //---------------------------------------------------------------------------
  6226. void __fastcall TCustomScpExplorerForm::UpdateNewSessionTab()
  6227. {
  6228. TTabSheet * TabSheet = SessionsPageControl->Pages[SessionsPageControl->PageCount - 1];
  6229. DebugAssert(TabSheet->ImageIndex == 0);
  6230. TabSheet->Caption =
  6231. WinConfiguration->SelectiveToolbarText ?
  6232. StripHotkey(StripTrailingPunctuation(NonVisualDataModule->NewSessionAction->Caption)) :
  6233. UnicodeString();
  6234. }
  6235. //---------------------------------------------------------------------------
  6236. TManagedTerminal * __fastcall TCustomScpExplorerForm::GetSessionTabTerminal(TTabSheet * TabSheet)
  6237. {
  6238. return reinterpret_cast<TManagedTerminal *>(TabSheet->Tag);
  6239. }
  6240. //---------------------------------------------------------------------------
  6241. void __fastcall TCustomScpExplorerForm::UpdateSessionTab(TTabSheet * TabSheet)
  6242. {
  6243. if (DebugAlwaysTrue(TabSheet != NULL))
  6244. {
  6245. TManagedTerminal * ATerminal = GetSessionTabTerminal(TabSheet);
  6246. if (DebugAlwaysTrue(ATerminal != NULL))
  6247. {
  6248. TColor Color = (ATerminal == FTerminal) ? FSessionColor : ATerminal->StateData->Color;
  6249. TabSheet->ImageIndex = AddSessionColor(Color);
  6250. UnicodeString TabCaption = TTerminalManager::Instance()->GetTerminalTitle(ATerminal, true);
  6251. TabSheet->Caption = SessionsPageControl->FormatCaptionWithCloseButton(TabCaption);
  6252. TThemeTabSheet * ThemeTabSheet = dynamic_cast<TThemeTabSheet *>(TabSheet);
  6253. if (DebugAlwaysTrue(ThemeTabSheet != NULL))
  6254. {
  6255. ThemeTabSheet->Shadowed = !ATerminal->Active;
  6256. }
  6257. }
  6258. }
  6259. }
  6260. //---------------------------------------------------------------------------
  6261. bool __fastcall TCustomScpExplorerForm::SessionTabSwitched()
  6262. {
  6263. DebugAssert(SessionsPageControl->ActivePage != NULL);
  6264. TManagedTerminal * Terminal = GetSessionTabTerminal(SessionsPageControl->ActivePage);
  6265. bool Result = (Terminal != NULL);
  6266. if (Result)
  6267. {
  6268. TTerminalManager::Instance()->ActiveTerminal = Terminal;
  6269. }
  6270. else
  6271. {
  6272. try
  6273. {
  6274. NewSession(false);
  6275. }
  6276. __finally
  6277. {
  6278. DoTerminalListChanged();
  6279. }
  6280. FSessionsPageControlNewSessionTime = Now();
  6281. }
  6282. return Result;
  6283. }
  6284. //---------------------------------------------------------------------------
  6285. void __fastcall TCustomScpExplorerForm::SessionsPageControlChange(TObject * /*Sender*/)
  6286. {
  6287. SessionTabSwitched();
  6288. }
  6289. //---------------------------------------------------------------------------
  6290. void __fastcall TCustomScpExplorerForm::TransferListChange(TObject * Sender)
  6291. {
  6292. TTBXStringList * TransferList = dynamic_cast<TTBXStringList *>(Sender);
  6293. DebugAssert(TransferList != NULL);
  6294. UnicodeString Name;
  6295. if (TransferList->ItemIndex <= 0)
  6296. {
  6297. Name = L"";
  6298. }
  6299. else
  6300. {
  6301. Name = GUIConfiguration->CopyParamList->Names[TransferList->ItemIndex - 1];
  6302. }
  6303. if (FCopyParamAutoSelected.IsEmpty())
  6304. {
  6305. // if previous preset was not autoselected, make new preset the "default"
  6306. FCopyParamDefault = Name;
  6307. }
  6308. GUIConfiguration->CopyParamCurrent = Name;
  6309. }
  6310. //---------------------------------------------------------------------------
  6311. void __fastcall TCustomScpExplorerForm::UpdateTransferLabel()
  6312. {
  6313. // sanity check
  6314. bool ExistingPreset =
  6315. (FTransferListHoverIndex >= 0) &&
  6316. (FTransferListHoverIndex < 1 + GUIConfiguration->CopyParamList->Count);
  6317. DebugAssert(ExistingPreset);
  6318. if (ExistingPreset)
  6319. {
  6320. HDC DC = GetDC(0);
  6321. TCanvas * Canvas = new TCanvas();
  6322. try
  6323. {
  6324. Canvas->Handle = DC;
  6325. Canvas->Font = GetToolbarFont(this);
  6326. UnicodeString Name;
  6327. if (FTransferListHoverIndex == 0)
  6328. {
  6329. Name = L"";
  6330. }
  6331. else
  6332. {
  6333. Name = GUIConfiguration->CopyParamList->Names[FTransferListHoverIndex - 1];
  6334. }
  6335. TTBXLabelItem * TransferLabel = dynamic_cast<TTBXLabelItem*>(
  6336. static_cast<TComponent*>(GetComponent(fcTransferLabel)));
  6337. TTBXStringList * TransferList = dynamic_cast<TTBXStringList*>(
  6338. static_cast<TObject*>(GetComponent(fcTransferList)));
  6339. UnicodeString InfoStr =
  6340. GUIConfiguration->CopyParamPreset[Name].
  6341. GetInfoStr(L"; ",
  6342. FLAGMASK(((Terminal != NULL) && Terminal->Active), Terminal->UsableCopyParamAttrs(0).General));
  6343. int MaxWidth = TransferList->MinWidth - (2 * TransferLabel->Margin) - ScaleByTextHeight(this, 10);
  6344. if (Canvas->TextExtent(InfoStr).cx > MaxWidth)
  6345. {
  6346. while (Canvas->TextExtent(InfoStr + Ellipsis).cx > MaxWidth)
  6347. {
  6348. InfoStr.SetLength(InfoStr.Length() - 1);
  6349. }
  6350. InfoStr += Ellipsis;
  6351. }
  6352. // UpdateCaption does not cause invalidation of whole submenu, while
  6353. // setting Caption property does.
  6354. // also it probably does not resize the label, even if necessary
  6355. // (we do not want that anyway)
  6356. TransferLabel->UpdateCaption(InfoStr);
  6357. }
  6358. __finally
  6359. {
  6360. Canvas->Handle = NULL;
  6361. ReleaseDC(0, DC);
  6362. delete Canvas;
  6363. }
  6364. }
  6365. }
  6366. //---------------------------------------------------------------------------
  6367. void __fastcall TCustomScpExplorerForm::TransferListDrawItem(
  6368. TTBXCustomList * /*Sender*/, TCanvas * /*ACanvas*/, const TRect & /*ARect*/,
  6369. int /*AIndex*/, int AHoverIndex, bool & /*DrawDefault*/)
  6370. {
  6371. if (FTransferListHoverIndex != AHoverIndex)
  6372. {
  6373. FTransferListHoverIndex = AHoverIndex;
  6374. UpdateTransferLabel();
  6375. }
  6376. }
  6377. //---------------------------------------------------------------------------
  6378. void __fastcall TCustomScpExplorerForm::WMAppCommand(TMessage & Message)
  6379. {
  6380. int Command = GET_APPCOMMAND_LPARAM(Message.LParam);
  6381. TShiftState Shift = KeyDataToShiftState(GET_KEYSTATE_LPARAM(Message.LParam));
  6382. if ((Shift * (TShiftState() << ssShift << ssAlt << ssCtrl)).Empty())
  6383. {
  6384. if (Command == APPCOMMAND_BROWSER_FAVORITES)
  6385. {
  6386. OpenDirectory(GetSide(osCurrent));
  6387. Message.Result = 1;
  6388. }
  6389. else
  6390. {
  6391. TForm::Dispatch(&Message);
  6392. }
  6393. }
  6394. else
  6395. {
  6396. TForm::Dispatch(&Message);
  6397. }
  6398. }
  6399. //---------------------------------------------------------------------------
  6400. void __fastcall TCustomScpExplorerForm::CMDialogChar(TMessage & AMessage)
  6401. {
  6402. TCMDialogChar & Message = reinterpret_cast<TCMDialogChar &>(AMessage);
  6403. if ((FIgnoreNextDialogChar != 0) &&
  6404. (toupper(Message.CharCode) == toupper(FIgnoreNextDialogChar)))
  6405. {
  6406. Message.Result = 1;
  6407. }
  6408. else
  6409. {
  6410. TForm::Dispatch(&Message);
  6411. }
  6412. FIgnoreNextDialogChar = 0;
  6413. }
  6414. //---------------------------------------------------------------------------
  6415. void __fastcall TCustomScpExplorerForm::WMSysCommand(TMessage & Message)
  6416. {
  6417. // The four low-order bits are used internally by Windows
  6418. unsigned int Cmd = (Message.WParam & 0xFFF0);
  6419. // SC_RESTORE, SC_MAXIMIZE, SC_MINIMIZE - buttons on windows title
  6420. // SC_DEFAULT - double click on windows title (does not work, at least on WinXP)
  6421. // 61730 - restore thru double click - undocumented
  6422. // 61490 - maximize thru double click - undocumented
  6423. if ((Cmd == SC_RESTORE) || (Cmd == SC_MAXIMIZE) ||
  6424. (Cmd == SC_MINIMIZE) || (Cmd == SC_DEFAULT))
  6425. {
  6426. SysResizing(Cmd);
  6427. }
  6428. TForm::Dispatch(&Message);
  6429. }
  6430. //---------------------------------------------------------------------------
  6431. void __fastcall TCustomScpExplorerForm::WMQueryEndSession(TMessage & Message)
  6432. {
  6433. // We were actually never able to make ENDSESSION_CRITICAL happen.
  6434. // Also there no point returning TRUE as we are not able to
  6435. // handle the abrupt termination caused by subsequent WM_ENDSESSION cleanly.
  6436. // Hence the process termination might be safer :)
  6437. if ((Message.LParam != ENDSESSION_CRITICAL) &&
  6438. (((FQueue != NULL) && !FQueue->IsEmpty) || (FProgressForm != NULL)))
  6439. {
  6440. Message.Result = FALSE;
  6441. }
  6442. else
  6443. {
  6444. Message.Result = TRUE;
  6445. }
  6446. // Do not call default handling as that triggers OnCloseQuery,
  6447. // where our implementation will popup confirmation dialogs, what we do not want,
  6448. // as per Vista guidelines:
  6449. // https://docs.microsoft.com/en-us/windows/win32/shutdown/shutting-down
  6450. }
  6451. //---------------------------------------------------------------------------
  6452. void __fastcall TCustomScpExplorerForm::WMEndSession(TWMEndSession & Message)
  6453. {
  6454. if (Message.EndSession && IsApplicationMinimized())
  6455. {
  6456. // WORKAROUND
  6457. // TApplication.WndProc() calls Application.Terminate() before Halt(),
  6458. // when it receives WM_ENDSESSION,
  6459. // but that sometimes (particularly when application is minimized) cause crashes.
  6460. // Crash popups message that blocks log off.
  6461. // Obviously application cannot shutdown cleanly after WM_ENDSESSION,
  6462. // so we call ExitProcess() immediately, not even trying to cleanup.
  6463. // It still causes beep, so there's likely some popup, but it does not block
  6464. // log off.
  6465. ExitProcess(0);
  6466. }
  6467. TForm::Dispatch(&Message);
  6468. }
  6469. #ifdef _DEBUG
  6470. //---------------------------------------------------------------------------
  6471. void __fastcall TCustomScpExplorerForm::WMWindowPosChanged(TWMWindowPosMsg & Message)
  6472. {
  6473. TForm::Dispatch(&Message);
  6474. }
  6475. #endif
  6476. //---------------------------------------------------------------------------
  6477. void __fastcall TCustomScpExplorerForm::SysResizing(unsigned int /*Cmd*/)
  6478. {
  6479. }
  6480. //---------------------------------------------------------------------------
  6481. void __fastcall TCustomScpExplorerForm::DoShow()
  6482. {
  6483. // only now are the controls resized finally, so the size constraints
  6484. // will not conflict with possibly very small window size
  6485. RestoreFormParams();
  6486. // This indicates that the form was rescaled after parameters were loaded and
  6487. // as most probably the parameters were actually saved with the current form scaling
  6488. // (windows was the last time likely closed on the same monitor, where it is starting now),
  6489. // rescaling happened twice already. Reload the parameters, as now they likely won't need any scaling
  6490. // and no rounding problems will occur.
  6491. // See also RestoreForm.
  6492. // (we should be safe to call RestoreParams unconditionally)
  6493. if (PixelsPerInch != Screen->PixelsPerInch)
  6494. {
  6495. RestoreParams();
  6496. }
  6497. FixControlsPlacement();
  6498. if (Position == poDefaultPosOnly)
  6499. {
  6500. CutFormToDesktop(this);
  6501. }
  6502. TForm::DoShow();
  6503. FSessionsDragDropFilesEx->DragDropControl = SessionsPageControl;
  6504. FQueueDragDropFilesEx->DragDropControl = QueueView3;
  6505. if (Terminal == NULL)
  6506. {
  6507. StartingDisconnected();
  6508. }
  6509. UpdatePixelsPerInchMainWindowCounter();
  6510. FShowing = true;
  6511. }
  6512. //---------------------------------------------------------------------------
  6513. void __fastcall TCustomScpExplorerForm::UpdatePixelsPerInchMainWindowCounter()
  6514. {
  6515. Configuration->Usage->Set(L"PixelsPerInchMainWindow", PixelsPerInch);
  6516. }
  6517. //---------------------------------------------------------------------------
  6518. void __fastcall TCustomScpExplorerForm::StartingDisconnected()
  6519. {
  6520. DoTerminalListChanged();
  6521. }
  6522. //---------------------------------------------------------------------------
  6523. void __fastcall TCustomScpExplorerForm::PopupTrayBalloon(TTerminal * Terminal,
  6524. const UnicodeString & Str, TQueryType Type, Exception * E, unsigned int Seconds,
  6525. TNotifyEvent OnBalloonClick, TObject * UserData)
  6526. {
  6527. bool Do;
  6528. UnicodeString Message;
  6529. if (E == NULL)
  6530. {
  6531. Message = Str;
  6532. Do = true;
  6533. }
  6534. else
  6535. {
  6536. Do = ExceptionMessage(E, Message);
  6537. }
  6538. if (Do && WinConfiguration->BalloonNotifications)
  6539. {
  6540. UnicodeString Title;
  6541. if ((Terminal == NULL) && (Type == qtInformation) && ExtractMainInstructions(Message, Title))
  6542. {
  6543. Message = TrimLeft(Message);
  6544. }
  6545. else
  6546. {
  6547. Message = UnformatMessage(Message);
  6548. const ResourceString * Captions[] = { &_SMsgDlgConfirm, &_SMsgDlgWarning,
  6549. &_SMsgDlgError, &_SMsgDlgInformation, NULL };
  6550. Title = LoadResourceString(Captions[Type]);
  6551. if (Terminal != NULL)
  6552. {
  6553. Title = FORMAT(L"%s - %s",
  6554. (TTerminalManager::Instance()->GetTerminalTitle(Terminal, true), Title));
  6555. }
  6556. }
  6557. if (Seconds == 0)
  6558. {
  6559. Seconds = WinConfiguration->NotificationsTimeout;
  6560. }
  6561. FTrayIcon->PopupBalloon(Title, Message, Type, Seconds * MSecsPerSec, OnBalloonClick, UserData);
  6562. }
  6563. }
  6564. //---------------------------------------------------------------------------
  6565. unsigned int __fastcall TCustomScpExplorerForm::MoreMessageDialog(const UnicodeString Message,
  6566. TStrings * MoreMessages, TQueryType Type, unsigned int Answers,
  6567. UnicodeString HelpKeyword, const TMessageParams * Params,
  6568. TTerminal * Terminal)
  6569. {
  6570. if (((WinConfiguration->ContinueOnError && (FErrorList != NULL)) ||
  6571. (FOnFeedSynchronizeError != NULL)) &&
  6572. (Params != NULL) && (Params->Params & mpAllowContinueOnError) )
  6573. {
  6574. if (FOnFeedSynchronizeError != NULL)
  6575. {
  6576. FOnFeedSynchronizeError(Message, MoreMessages, Type, HelpKeyword);
  6577. }
  6578. else
  6579. {
  6580. DebugAssert(FErrorList != NULL);
  6581. TStringList * MoreMessagesCopy = NULL;
  6582. if (MoreMessages)
  6583. {
  6584. MoreMessagesCopy = new TStringList();
  6585. MoreMessagesCopy->Assign(MoreMessages);
  6586. }
  6587. FErrorList->AddObject(Message, MoreMessagesCopy);
  6588. }
  6589. PopupTrayBalloon(Terminal, Message, Type);
  6590. return ContinueAnswer(Answers);
  6591. }
  6592. else
  6593. {
  6594. bool UseBalloon = FTrayIcon->Visible;
  6595. if (UseBalloon)
  6596. {
  6597. PopupTrayBalloon(Terminal, Message, Type);
  6598. }
  6599. unsigned int Result;
  6600. try
  6601. {
  6602. Result = ::MoreMessageDialog(Message, MoreMessages, Type, Answers, HelpKeyword, Params);
  6603. }
  6604. __finally
  6605. {
  6606. // cancel only balloon we popped up, otherwise we may cancel notification
  6607. // balloon that was there before the message dialog
  6608. // (such as "dd confirmation opt in balloon)
  6609. if (UseBalloon)
  6610. {
  6611. FTrayIcon->CancelBalloon();
  6612. }
  6613. }
  6614. return Result;
  6615. }
  6616. }
  6617. //---------------------------------------------------------------------------
  6618. void __fastcall TCustomScpExplorerForm::ShowExtendedException(
  6619. TTerminal * Terminal, Exception * E)
  6620. {
  6621. if (FTrayIcon->Visible)
  6622. {
  6623. PopupTrayBalloon(Terminal, L"", qtError, E);
  6624. }
  6625. // particularly prevent opening new session from jump list,
  6626. // while exception is shown
  6627. NonVisualDataModule->StartBusy();
  6628. try
  6629. {
  6630. ShowExtendedExceptionEx(Terminal, E);
  6631. }
  6632. __finally
  6633. {
  6634. NonVisualDataModule->EndBusy();
  6635. FTrayIcon->CancelBalloon();
  6636. }
  6637. }
  6638. //---------------------------------------------------------------------------
  6639. void __fastcall TCustomScpExplorerForm::TerminalReady()
  6640. {
  6641. if (Terminal->Active)
  6642. {
  6643. InitStatusBar();
  6644. }
  6645. // cannot rely on active page being page for active terminal,
  6646. // as it can happen that active page is the "new session" page
  6647. // (e.g. when reconnecting active terminal, while login dialog
  6648. // invoked from "new session" page is modal)
  6649. int ActiveTerminalIndex = TTerminalManager::Instance()->ActiveTerminalIndex;
  6650. UpdateSessionTab(SessionsPageControl->Pages[ActiveTerminalIndex]);
  6651. }
  6652. //---------------------------------------------------------------------------
  6653. void __fastcall TCustomScpExplorerForm::InactiveTerminalException(
  6654. TTerminal * Terminal, Exception * E)
  6655. {
  6656. Notify(Terminal, L"", qtError, false, NULL, NULL, E);
  6657. if (!Terminal->Active)
  6658. {
  6659. int Index = TTerminalManager::Instance()->IndexOf(Terminal);
  6660. if (DebugAlwaysTrue((Index >= 0) && (Index < SessionsPageControl->PageCount)))
  6661. {
  6662. UpdateSessionTab(SessionsPageControl->Pages[Index]);
  6663. }
  6664. }
  6665. }
  6666. //---------------------------------------------------------------------------
  6667. void __fastcall TCustomScpExplorerForm::Notify(TTerminal * Terminal,
  6668. UnicodeString Message, TQueryType Type,
  6669. bool Important, TNotifyEvent OnClick, TObject * UserData, Exception * E)
  6670. {
  6671. if ((E == NULL) ||
  6672. ExceptionMessage(E, Message))
  6673. {
  6674. unsigned int Seconds = WinConfiguration->NotificationsTimeout;
  6675. if (Important)
  6676. {
  6677. Seconds *= 5;
  6678. }
  6679. UnicodeString NoteMessage(UnformatMessage(Message));
  6680. if (Terminal != NULL)
  6681. {
  6682. NoteMessage = FORMAT(L"%s: %s",
  6683. (TTerminalManager::Instance()->GetTerminalTitle(Terminal, true), NoteMessage));
  6684. }
  6685. if (WinConfiguration->BalloonNotifications)
  6686. {
  6687. AddNote(NoteMessage);
  6688. PopupTrayBalloon(Terminal, Message, Type, NULL, Seconds, OnClick, UserData);
  6689. }
  6690. else
  6691. {
  6692. FlashOnBackground();
  6693. PostNote(NoteMessage, Seconds, OnClick, UserData);
  6694. }
  6695. }
  6696. }
  6697. //---------------------------------------------------------------------------
  6698. void __fastcall TCustomScpExplorerForm::QueueEmptyNoteClicked(TObject * Sender)
  6699. {
  6700. RestoreApp();
  6701. TTerminalNoteData * TerminalNoteData = dynamic_cast<TTerminalNoteData *>(Sender);
  6702. if (DebugAlwaysTrue(TerminalNoteData != NULL) &&
  6703. !NonVisualDataModule->Busy)
  6704. {
  6705. TManagedTerminal * Terminal = TerminalNoteData->Terminal;
  6706. TTerminalManager::Instance()->ActiveTerminal = Terminal;
  6707. if (!ComponentVisible[fcQueueView])
  6708. {
  6709. ToggleQueueVisibility();
  6710. GoToQueue();
  6711. }
  6712. }
  6713. }
  6714. //---------------------------------------------------------------------------
  6715. void __fastcall TCustomScpExplorerForm::QueueEvent(TManagedTerminal * ATerminal,
  6716. TTerminalQueue * /*Queue*/, TQueueEvent Event)
  6717. {
  6718. UnicodeString Message;
  6719. TNotifyEvent OnClick = NULL;
  6720. TObject * UserData = NULL;
  6721. bool QueueInvisible = !ComponentVisible[fcQueueView] || IsApplicationMinimized();
  6722. switch (Event)
  6723. {
  6724. case qeEmptyButMonitored:
  6725. if ((ATerminal != Terminal) || QueueInvisible)
  6726. {
  6727. Message = LoadStr(BALLOON_QUEUE_EMPTY);
  6728. OnClick = QueueEmptyNoteClicked;
  6729. TTerminalNoteData * TerminalNoteData = new TTerminalNoteData();
  6730. TerminalNoteData->Terminal = ATerminal;
  6731. UserData = TerminalNoteData;
  6732. }
  6733. break;
  6734. case qeEmpty:
  6735. OperationComplete(ATerminal->QueueOperationStart);
  6736. break;
  6737. case qePendingUserAction:
  6738. if ((ATerminal != Terminal) ||
  6739. (QueueInvisible && !IsQueueAutoPopup()))
  6740. {
  6741. Message = LoadStr(BALLOON_QUEUE_USER_ACTION);
  6742. }
  6743. break;
  6744. default:
  6745. DebugFail();
  6746. }
  6747. if (!Message.IsEmpty())
  6748. {
  6749. Notify(ATerminal, Message, qtInformation, false, OnClick, UserData);
  6750. }
  6751. }
  6752. //---------------------------------------------------------------------------
  6753. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDCreateDragFileList(
  6754. TObject * /*Sender*/, TFileList * FileList, bool & Created)
  6755. {
  6756. if (FDDExtMapFile != NULL)
  6757. {
  6758. CloseHandle(FDDExtMapFile);
  6759. FDDExtMapFile = NULL;
  6760. }
  6761. if (WinConfiguration->DDFakeFile)
  6762. {
  6763. DDFakeFileInitDrag(FileList, Created);
  6764. }
  6765. }
  6766. //---------------------------------------------------------------------------
  6767. void __fastcall TCustomScpExplorerForm::DDFakeCreated(TObject * /*Sender*/, const UnicodeString FileName)
  6768. {
  6769. if (DebugAlwaysTrue(!FDragFakeDirectory.IsEmpty()) &&
  6770. SameText(ExtractFileName(FileName), ExtractFileName(FDragFakeDirectory)))
  6771. {
  6772. FFakeFileDropTarget = FileName;
  6773. }
  6774. }
  6775. //---------------------------------------------------------------------------
  6776. UnicodeString __fastcall TCustomScpExplorerForm::CreateFakeTransferDirectory()
  6777. {
  6778. UnicodeString Result = ExcludeTrailingBackslash(WinConfiguration->TemporaryDir());
  6779. if (!ForceDirectories(ApiPath(Result)))
  6780. {
  6781. throw Exception(FMTLOAD(CREATE_TEMP_DIR_ERROR, (Result)));
  6782. }
  6783. FileSetAttr(ApiPath(Result), faHidden);
  6784. return Result;
  6785. }
  6786. //---------------------------------------------------------------------------
  6787. void __fastcall TCustomScpExplorerForm::DDFakeFileInitDrag(TFileList * FileList,
  6788. bool & Created)
  6789. {
  6790. FFakeFileDropTarget = UnicodeString();
  6791. FDragFakeDirectory = CreateFakeTransferDirectory();
  6792. FileList->AddItem(NULL, FDragFakeDirectory);
  6793. Created = true;
  6794. if (!WinConfiguration->IsDDExtRunning() || WinConfiguration->IsDDExtBroken())
  6795. {
  6796. FDragFakeMonitors = StartCreationDirectoryMonitorsOnEachDrive(FILE_NOTIFY_CHANGE_DIR_NAME, DDFakeCreated);
  6797. }
  6798. FDDExtMapFile = CreateFileMappingA((HANDLE)0xFFFFFFFF, NULL, PAGE_READWRITE,
  6799. 0, sizeof(TDragExtCommStruct), AnsiString(DRAG_EXT_MAPPING).c_str());
  6800. {
  6801. TMutexGuard Guard(FDDExtMutex, DRAGEXT_MUTEX_RELEASE_TIMEOUT);
  6802. TDragExtCommStruct* CommStruct;
  6803. CommStruct = static_cast<TDragExtCommStruct*>(MapViewOfFile(FDDExtMapFile,
  6804. FILE_MAP_ALL_ACCESS, 0, 0, 0));
  6805. DebugAssert(CommStruct != NULL);
  6806. CommStruct->Version = TDragExtCommStruct::CurrentVersion;
  6807. CommStruct->Dragging = true;
  6808. wcsncpy(CommStruct->DropDest, FDragFakeDirectory.c_str(),
  6809. LENOF(CommStruct->DropDest));
  6810. NULL_TERMINATE(CommStruct->DropDest);
  6811. UnmapViewOfFile(CommStruct);
  6812. }
  6813. }
  6814. //---------------------------------------------------------------------------
  6815. bool __fastcall TCustomScpExplorerForm::RemoteFileControlFileOperation(
  6816. TObject * Sender, TFileOperation Operation, bool NoConfirmation, void * Param)
  6817. {
  6818. bool Result;
  6819. DebugAssert(!IsLocalBrowserMode());
  6820. if (Sender == RemoteDirView)
  6821. {
  6822. Result = ExecuteFileOperation(Operation, osRemote, true, NoConfirmation, Param);
  6823. }
  6824. else
  6825. {
  6826. DebugAssert(Sender == RemoteDriveView);
  6827. TStrings * FileList = RemoteDriveView->DragFileList();
  6828. try
  6829. {
  6830. Result = ExecuteFileOperation(Operation, osRemote, FileList, NoConfirmation, Param);
  6831. }
  6832. __finally
  6833. {
  6834. delete FileList;
  6835. }
  6836. }
  6837. if (FDDTargetControl == RemoteDriveView)
  6838. {
  6839. RemoteDriveView->UpdateDropTarget();
  6840. }
  6841. // foRemoteMove happens when file/dir is dragged within the remote tree view
  6842. // or from tree view to dir view.
  6843. // foMove happens when file/dir is dragged from remote tree view outside of
  6844. // application via dragex
  6845. if (((Operation == foRemoteMove) || (Operation == foMove)) &&
  6846. (DropSourceControl == RemoteDriveView))
  6847. {
  6848. RemoteDriveView->UpdateDropSource();
  6849. }
  6850. return Result;
  6851. }
  6852. //---------------------------------------------------------------------------
  6853. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDEnd(TObject * Sender)
  6854. {
  6855. DebugAssert(!IsLocalBrowserMode());
  6856. // This also handles drops of remote files to queue.
  6857. // Drops of local files (uploads) are handled in QueueDDProcessDropped.
  6858. SAFE_DESTROY(FDDFileList);
  6859. if (!FFakeFileDropTarget.IsEmpty())
  6860. {
  6861. RemoveDir(ApiPath(FFakeFileDropTarget));
  6862. }
  6863. if ((FDDExtMapFile != NULL) || (FDDTargetControl == QueueView3))
  6864. {
  6865. try
  6866. {
  6867. TDragResult DDResult = (Sender == RemoteDirView) ?
  6868. RemoteDirView->LastDDResult : RemoteDriveView->LastDDResult;
  6869. // Focus is moved to the target application,
  6870. // but as we are going to present the UI, we need to steal the focus back.
  6871. // This most likely won't work though (windows does not allow application
  6872. // to steal focus most of the time)
  6873. Application->BringToFront();
  6874. // On older version of Windows we never got drMove here, see also comment below.
  6875. // On Windows 10, we get the "move".
  6876. if ((DDResult == drCopy) || (DDResult == drMove) || (DDResult == drInvalid))
  6877. {
  6878. UnicodeString TargetDirectory;
  6879. TFileOperation Operation;
  6880. // drInvalid may mean drMove, see comment below
  6881. switch (DDResult)
  6882. {
  6883. case drCopy:
  6884. Operation = foCopy;
  6885. break;
  6886. case drMove:
  6887. Operation = foMove;
  6888. break;
  6889. default:
  6890. DebugFail();
  6891. case drInvalid:
  6892. // prefer "copy" for safety
  6893. Operation = FLAGSET(FLastDropEffect, DROPEFFECT_MOVE) ? foMove : foCopy;
  6894. break;
  6895. }
  6896. TTransferOperationParam Param;
  6897. UnicodeString CounterName;
  6898. bool ForceQueue;
  6899. if (!DDGetTarget(Param.TargetDirectory, ForceQueue, CounterName))
  6900. {
  6901. // we get drInvalid both if d&d was intercepted by ddext,
  6902. // and when users drops on no-drop location.
  6903. // we tell the difference by existence of response from ddext,
  6904. // so we ignore absence of response in this case
  6905. if (DDResult != drInvalid)
  6906. {
  6907. // here we know that the extension is installed,
  6908. // as it is checked as soon as drag&drop starts from
  6909. // RemoteFileControlDDCreateDragFileList
  6910. Configuration->Usage->Inc(L"DownloadsDragDropExternalExtTargetUnknown");
  6911. if (!WinConfiguration->DDExtInstalled || WinConfiguration->IsDDExtBroken())
  6912. {
  6913. throw ExtException(NULL, LoadStr(DD_TARGET_UNKNOWN), HELP_DD_TARGET_UNKNOWN);
  6914. }
  6915. else
  6916. {
  6917. throw ExtException(NULL, LoadStr(DRAGEXT_TARGET_UNKNOWN2), HELP_DRAGEXT_TARGET_UNKNOWN);
  6918. }
  6919. }
  6920. }
  6921. else
  6922. {
  6923. // download using fake file
  6924. Param.Temp = false;
  6925. Param.DragDrop = true;
  6926. if (ForceQueue)
  6927. {
  6928. Param.Queue = asOn;
  6929. }
  6930. if (Sender == RemoteDirView)
  6931. {
  6932. Param.Options = FLAGMASK(SelectedAllFilesInDirView(RemoteDirView), coAllFiles);
  6933. }
  6934. if (RemoteFileControlFileOperation(Sender, Operation,
  6935. (WinConfiguration->DDTransferConfirmation == asOff), &Param))
  6936. {
  6937. Configuration->Usage->Inc(CounterName);
  6938. }
  6939. }
  6940. }
  6941. }
  6942. __finally
  6943. {
  6944. CloseHandle(FDDExtMapFile);
  6945. FDDExtMapFile = NULL;
  6946. delete FDragFakeMonitors;
  6947. FDragFakeMonitors = NULL;
  6948. RemoveDir(ApiPath(FDragFakeDirectory));
  6949. FDragFakeDirectory = L"";
  6950. FFakeFileDropTarget = UnicodeString();
  6951. }
  6952. }
  6953. if (FDragDropSshTerminate.get() != NULL)
  6954. {
  6955. FDragDropSshTerminate->Rethrow();
  6956. }
  6957. }
  6958. //---------------------------------------------------------------------------
  6959. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDGiveFeedback(
  6960. TObject * /*Sender*/, int dwEffect, HRESULT & /*Result*/)
  6961. {
  6962. // Remember drop effect so we know (when user drops files), if we copy or move
  6963. FLastDropEffect = dwEffect;
  6964. }
  6965. //---------------------------------------------------------------------------
  6966. bool __fastcall TCustomScpExplorerForm::DDGetTarget(
  6967. UnicodeString & Directory, bool & ForceQueue, UnicodeString & CounterName)
  6968. {
  6969. bool Result;
  6970. if (FDDTargetControl == QueueView3)
  6971. {
  6972. Directory = DefaultDownloadTargetDirectory();
  6973. Result = true;
  6974. CounterName = L"DownloadsDragDropQueue";
  6975. ForceQueue = true;
  6976. }
  6977. else if (!FFakeFileDropTarget.IsEmpty())
  6978. {
  6979. Directory = ExcludeTrailingBackslash(ExtractFilePath(FFakeFileDropTarget));
  6980. Result = true;
  6981. ForceQueue = false;
  6982. CounterName = L"DownloadsDragDropFakeFile";
  6983. }
  6984. else
  6985. {
  6986. ForceQueue = false;
  6987. Enabled = false;
  6988. try
  6989. {
  6990. int Timer = 0;
  6991. Result = false;
  6992. while (!Result && (Timer < WinConfiguration->DDExtTimeout))
  6993. {
  6994. {
  6995. TMutexGuard Guard(FDDExtMutex, DRAGEXT_MUTEX_RELEASE_TIMEOUT);
  6996. TDragExtCommStruct* CommStruct;
  6997. CommStruct = static_cast<TDragExtCommStruct*>(MapViewOfFile(FDDExtMapFile,
  6998. FILE_MAP_ALL_ACCESS, 0, 0, 0));
  6999. DebugAssert(CommStruct != NULL);
  7000. Result = !CommStruct->Dragging;
  7001. if (Result)
  7002. {
  7003. Directory = ExtractFilePath(CommStruct->DropDest);
  7004. CounterName = L"DownloadsDragDropExternalExt";
  7005. }
  7006. UnmapViewOfFile(CommStruct);
  7007. }
  7008. if (!Result)
  7009. {
  7010. Sleep(50);
  7011. Timer += 50;
  7012. Application->ProcessMessages();
  7013. }
  7014. }
  7015. }
  7016. __finally
  7017. {
  7018. Enabled = true;
  7019. }
  7020. }
  7021. return Result;
  7022. }
  7023. //---------------------------------------------------------------------------
  7024. void __fastcall TCustomScpExplorerForm::AddDelayedDirectoryDeletion(
  7025. const UnicodeString TempDir, int SecDelay)
  7026. {
  7027. TDateTime Alarm = IncSecond(Now(), SecDelay);
  7028. FDelayedDeletionList->AddObject(TempDir, reinterpret_cast<TObject*>(Alarm.FileDate()));
  7029. if (FDelayedDeletionTimer == NULL)
  7030. {
  7031. DebugAssert(HandleAllocated());
  7032. FDelayedDeletionTimer = new TTimer(this);
  7033. FDelayedDeletionTimer->Interval = 10000;
  7034. FDelayedDeletionTimer->OnTimer = DoDelayedDeletion;
  7035. }
  7036. else
  7037. {
  7038. FDelayedDeletionTimer->Enabled = true;
  7039. }
  7040. }
  7041. //---------------------------------------------------------------------------
  7042. void __fastcall TCustomScpExplorerForm::DoDelayedDeletion(TObject * Sender)
  7043. {
  7044. DebugAssert(FDelayedDeletionList != NULL);
  7045. TDateTime N = Now();
  7046. TDateTime Alert;
  7047. UnicodeString Directory;
  7048. for (int Index = FDelayedDeletionList->Count-1; Index >= 0; Index--)
  7049. {
  7050. Alert = FileDateToDateTime(reinterpret_cast<int>(FDelayedDeletionList->Objects[Index]));
  7051. if ((N >= Alert) || (Sender == NULL))
  7052. {
  7053. Directory = FDelayedDeletionList->Strings[Index];
  7054. if (DeleteDirectory(ExcludeTrailingBackslash(Directory)))
  7055. {
  7056. FDelayedDeletionList->Delete(Index);
  7057. }
  7058. }
  7059. }
  7060. if (FDelayedDeletionList->Count == 0)
  7061. {
  7062. FDelayedDeletionTimer->Enabled = false;
  7063. }
  7064. }
  7065. //---------------------------------------------------------------------------
  7066. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDTargetDrop()
  7067. {
  7068. DebugAssert(!IsLocalBrowserMode());
  7069. if (IsFileControl(FDDTargetControl, osRemote) ||
  7070. (FDDTargetControl == SessionsPageControl))
  7071. {
  7072. TTerminal * TargetTerminal = NULL;
  7073. TFileOperation Operation = foNone;
  7074. if (FDDTargetControl == SessionsPageControl)
  7075. {
  7076. TPoint Point = SessionsPageControl->ScreenToClient(Mouse->CursorPos);
  7077. int Index = SessionsPageControl->IndexOfTabAt(Point.X, Point.Y);
  7078. // do not allow dropping on the "+" tab
  7079. TargetTerminal = GetSessionTabTerminal(SessionsPageControl->Pages[Index]);
  7080. if (TargetTerminal != NULL)
  7081. {
  7082. if ((FLastDropEffect == DROPEFFECT_MOVE) &&
  7083. (TargetTerminal == TTerminalManager::Instance()->ActiveTerminal))
  7084. {
  7085. Operation = foRemoteMove;
  7086. }
  7087. else
  7088. {
  7089. Operation = foRemoteCopy;
  7090. }
  7091. }
  7092. }
  7093. else
  7094. {
  7095. if (FLastDropEffect == DROPEFFECT_MOVE)
  7096. {
  7097. Operation = foRemoteMove;
  7098. }
  7099. else if (FLastDropEffect == DROPEFFECT_COPY)
  7100. {
  7101. Operation = foRemoteCopy;
  7102. }
  7103. }
  7104. if (Operation != foNone)
  7105. {
  7106. RemoteFileControlFileOperation(DropSourceControl,
  7107. Operation, (WinConfiguration->DDTransferConfirmation == asOff), TargetTerminal);
  7108. }
  7109. // abort drag&drop
  7110. Abort();
  7111. }
  7112. else if ((FDDExtMapFile == NULL) && (FLastDropEffect != DROPEFFECT_NONE) &&
  7113. // Drops of remote files to queue are handled in RemoteFileControlDDEnd
  7114. (FDDTargetControl != QueueView3))
  7115. {
  7116. DebugAssert(!FDragTempDir.IsEmpty());
  7117. TTransferType Type;
  7118. UnicodeString TempDir = FDragTempDir;
  7119. // We clear FDragTempDir before calling
  7120. // just in case it fail (raises exception)
  7121. FDragTempDir = L"";
  7122. Type = (FLastDropEffect & DROPEFFECT_MOVE ? ttMove : Type = ttCopy);
  7123. TGUICopyParamType CopyParams = GUIConfiguration->CurrentCopyParam;
  7124. // empty directory parameter means temp directory -> don't display it!
  7125. UnicodeString TargetDir = L"";
  7126. int Options = 0;
  7127. if (!CopyParamDialog(tdToLocal, Type, true, FDDFileList,
  7128. TargetDir, CopyParams, (WinConfiguration->DDTransferConfirmation != asOff), true, Options))
  7129. {
  7130. Abort();
  7131. }
  7132. // TargetDir is set when dropped on local file control
  7133. // (this was workaround for legacy dirview event handling, now it should be
  7134. // made prettier)
  7135. if (TargetDir.IsEmpty())
  7136. {
  7137. TargetDir = TempDir;
  7138. if (ForceDirectories(ApiPath(TargetDir)))
  7139. {
  7140. DebugAssert(Terminal && !TargetDir.IsEmpty());
  7141. FPendingTempSpaceWarn = true;
  7142. try
  7143. {
  7144. FDragDropOperation = true;
  7145. int Params = cpTemporary |
  7146. (Type == ttMove ? cpDelete : 0);
  7147. DDDownload(FDDFileList, TargetDir, &CopyParams, Params);
  7148. Configuration->Usage->Inc(L"DownloadsDragDropExternalTemp");
  7149. }
  7150. __finally
  7151. {
  7152. FDragDropOperation = false;
  7153. FPendingTempSpaceWarn = false;
  7154. AddDelayedDirectoryDeletion(TargetDir, WinConfiguration->DDDeleteDelay);
  7155. }
  7156. }
  7157. else
  7158. {
  7159. throw Exception(FMTLOAD(CREATE_TEMP_DIR_ERROR, (TargetDir)));
  7160. }
  7161. }
  7162. }
  7163. }
  7164. //---------------------------------------------------------------------------
  7165. void __fastcall TCustomScpExplorerForm::DDDownload(
  7166. TStrings * FilesToCopy, const UnicodeString & TargetDir, const TCopyParamType * CopyParam, int Params)
  7167. {
  7168. DebugAssert(!IsLocalBrowserMode());
  7169. TAutoBatch AutoBatch(this);
  7170. UpdateCopyParamCounters(*CopyParam);
  7171. Terminal->CopyToLocal(FilesToCopy, TargetDir, CopyParam, Params, NULL);
  7172. if (FLAGSET(Params, cpDelete) && (DropSourceControl == RemoteDriveView))
  7173. {
  7174. RemoteDriveView->UpdateDropSource();
  7175. }
  7176. }
  7177. //---------------------------------------------------------------------------
  7178. class TFakeDataObjectFilesEx : public TDataObjectFilesEx
  7179. {
  7180. public:
  7181. __fastcall TFakeDataObjectFilesEx(TFileList * AFileList, bool RenderPIDL,
  7182. bool RenderFilename) : TDataObjectFilesEx(AFileList, RenderPIDL, RenderFilename, true)
  7183. {
  7184. }
  7185. virtual bool __fastcall AllowData(const tagFORMATETC & FormatEtc)
  7186. {
  7187. return (FormatEtc.cfFormat == CF_HDROP) ? false :
  7188. TDataObjectFilesEx::AllowData(FormatEtc);
  7189. }
  7190. };
  7191. //---------------------------------------------------------------------------
  7192. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDCreateDataObject(
  7193. TObject * Sender, TDataObject *& DataObject)
  7194. {
  7195. if (FDDExtMapFile != NULL)
  7196. {
  7197. TFileList * FileList = DragDropFiles(Sender)->FileList;
  7198. if (!FileList->RenderPIDLs() || !FileList->RenderNames())
  7199. {
  7200. Abort();
  7201. }
  7202. if (FileList->Count > 0)
  7203. {
  7204. TDataObjectFilesEx * FilesObject = new TFakeDataObjectFilesEx(FileList, true, true);
  7205. if (!FilesObject->IsValid(true, true))
  7206. {
  7207. FilesObject->_Release();
  7208. }
  7209. else
  7210. {
  7211. DataObject = FilesObject;
  7212. }
  7213. }
  7214. }
  7215. }
  7216. //---------------------------------------------------------------------------
  7217. void __fastcall TCustomScpExplorerForm::GoToCommandLine()
  7218. {
  7219. DebugFail();
  7220. }
  7221. //---------------------------------------------------------------------------
  7222. void __fastcall TCustomScpExplorerForm::GoToTree()
  7223. {
  7224. ComponentVisible[fcRemoteTree] = true;
  7225. DriveView(osRemote)->SetFocus();
  7226. }
  7227. //---------------------------------------------------------------------------
  7228. TStrings * __fastcall TCustomScpExplorerForm::PanelExport(TOperationSide Side,
  7229. TPanelExport Export)
  7230. {
  7231. TCustomDirView * DirView = this->DirView(Side);
  7232. std::unique_ptr<TStrings> ExportData(new TStringList());
  7233. switch (Export)
  7234. {
  7235. case pePath:
  7236. ExportData->Add(DirView->PathName);
  7237. break;
  7238. case peFileList:
  7239. case peFullFileList:
  7240. {
  7241. bool FullPath = (Export == peFullFileList);
  7242. DirView->CreateFileList(false, FullPath, ExportData.get());
  7243. for (int Index = 0; Index < ExportData->Count; Index++)
  7244. {
  7245. if (ExportData->Strings[Index].Pos(L" ") > 0)
  7246. {
  7247. ExportData->Strings[Index] = FORMAT(L"\"%s\"", (ExportData->Strings[Index]));
  7248. }
  7249. }
  7250. }
  7251. break;
  7252. default:
  7253. DebugFail();
  7254. }
  7255. return ExportData.release();
  7256. }
  7257. //---------------------------------------------------------------------------
  7258. void __fastcall TCustomScpExplorerForm::PanelExport(TOperationSide Side,
  7259. TPanelExport Export, TPanelExportDestination Destination)
  7260. {
  7261. std::unique_ptr<TStrings> ExportData(PanelExport(Side, Export));
  7262. PanelExportStore(Side, Export, Destination, ExportData.get());
  7263. }
  7264. //---------------------------------------------------------------------------
  7265. void __fastcall TCustomScpExplorerForm::PanelExportStore(TOperationSide /*Side*/,
  7266. TPanelExport /*Export*/, TPanelExportDestination Destination,
  7267. TStrings * ExportData)
  7268. {
  7269. if (Destination == pedClipboard)
  7270. {
  7271. TInstantOperationVisualizer Visualizer;
  7272. CopyToClipboard(ExportData);
  7273. }
  7274. else
  7275. {
  7276. DebugFail();
  7277. }
  7278. }
  7279. //---------------------------------------------------------------------------
  7280. void __fastcall TCustomScpExplorerForm::Filter(TOperationSide Side)
  7281. {
  7282. TCustomDirView * DirView = this->DirView(Side);
  7283. UnicodeString Mask = DirView->Mask;
  7284. if (DoFilterMaskDialog(DirView, Mask))
  7285. {
  7286. DirView->Mask = TFileMasks::NormalizeMask(Mask);
  7287. Configuration->Usage->Inc(L"Filters");
  7288. }
  7289. }
  7290. //---------------------------------------------------------------------------
  7291. TQueueOperation __fastcall TCustomScpExplorerForm::DefaultQueueOperation()
  7292. {
  7293. return FQueueController->DefaultOperation();
  7294. }
  7295. //---------------------------------------------------------------------------
  7296. bool __fastcall TCustomScpExplorerForm::AllowQueueOperation(
  7297. TQueueOperation Operation, void ** Param)
  7298. {
  7299. switch (Operation)
  7300. {
  7301. case qoPreferences:
  7302. return true;
  7303. case qoGoTo:
  7304. return ComponentVisible[fcQueueView] && QueueView3->Enabled;
  7305. case qoOnceEmpty:
  7306. return IsAnythingQueued();
  7307. default:
  7308. return FQueueController->AllowOperation(Operation, Param);
  7309. }
  7310. }
  7311. //---------------------------------------------------------------------------
  7312. void __fastcall TCustomScpExplorerForm::GoToQueue()
  7313. {
  7314. if (DebugAlwaysTrue(QueueView3->Visible))
  7315. {
  7316. QueueView3->SetFocus();
  7317. }
  7318. }
  7319. //---------------------------------------------------------------------------
  7320. void __fastcall TCustomScpExplorerForm::ExecuteQueueOperation(
  7321. TQueueOperation Operation, void * Param)
  7322. {
  7323. if (Operation == qoGoTo)
  7324. {
  7325. GoToQueue();
  7326. }
  7327. else if (Operation == qoPreferences)
  7328. {
  7329. PreferencesDialog(pmQueue);
  7330. }
  7331. else
  7332. {
  7333. FQueueController->ExecuteOperation(Operation, Param);
  7334. }
  7335. }
  7336. //---------------------------------------------------------------------------
  7337. bool __fastcall TCustomScpExplorerForm::GetQueueEnabled()
  7338. {
  7339. return (Queue != NULL) && Queue->Enabled;
  7340. }
  7341. //---------------------------------------------------------------------------
  7342. void __fastcall TCustomScpExplorerForm::ToggleQueueEnabled()
  7343. {
  7344. DebugAssert(Queue != NULL);
  7345. if (Queue != NULL)
  7346. {
  7347. Queue->Enabled = !Queue->Enabled;
  7348. }
  7349. }
  7350. //---------------------------------------------------------------------------
  7351. void __fastcall TCustomScpExplorerForm::QueueView3ContextPopup(
  7352. TObject * /*Sender*/, TPoint & /*MousePos*/, bool & /*Handled*/)
  7353. {
  7354. FQueueActedItem = QueueView3->ItemFocused;
  7355. }
  7356. //---------------------------------------------------------------------------
  7357. /*virtual*/ int __fastcall TCustomScpExplorerForm::GetStaticComponentsHeight()
  7358. {
  7359. return TopDock->Height + QueueSplitter->Height;
  7360. }
  7361. //---------------------------------------------------------------------------
  7362. void __fastcall TCustomScpExplorerForm::QueueSplitterCanResize(
  7363. TObject * /*Sender*/, int & NewSize, bool & Accept)
  7364. {
  7365. // when queue is hidden by double-clicking splitter, stray attempt to
  7366. // resize the panel with strange value arrives, make sure it is ignored
  7367. if (ComponentVisible[fcQueueView])
  7368. {
  7369. int HeightLimit = ClientHeight - GetStaticComponentsHeight() -
  7370. RemotePanel->Constraints->MinHeight;
  7371. if (NewSize > HeightLimit)
  7372. {
  7373. NewSize = HeightLimit;
  7374. }
  7375. }
  7376. else
  7377. {
  7378. Accept = false;
  7379. }
  7380. }
  7381. //---------------------------------------------------------------------------
  7382. void __fastcall TCustomScpExplorerForm::QueueView3StartDrag(TObject * /*Sender*/,
  7383. TDragObject *& /*DragObject*/)
  7384. {
  7385. FQueueActedItem = QueueView3->ItemFocused;
  7386. }
  7387. //---------------------------------------------------------------------------
  7388. void __fastcall TCustomScpExplorerForm::QueueView3DragOver(TObject * /*Sender*/,
  7389. TObject * Source, int X, int Y, TDragState /*State*/, bool & Accept)
  7390. {
  7391. Accept = true;
  7392. if (Source == QueueView3)
  7393. {
  7394. TListItem * DropTarget = QueueView3->GetItemAt(X, Y);
  7395. Accept = (DropTarget != NULL) && (FQueueActedItem != NULL);
  7396. if (Accept)
  7397. {
  7398. TQueueItemProxy * QueueItem;
  7399. TQueueItemProxy * DestQueueItem;
  7400. QueueItem = static_cast<TQueueItemProxy *>(FQueueActedItem->Data);
  7401. DestQueueItem = static_cast<TQueueItemProxy *>(DropTarget->Data);
  7402. Accept = (QueueItem != DestQueueItem) &&
  7403. (QueueItem->Status == TQueueItem::qsPending) &&
  7404. (DestQueueItem->Status == TQueueItem::qsPending);
  7405. }
  7406. }
  7407. }
  7408. //---------------------------------------------------------------------------
  7409. void __fastcall TCustomScpExplorerForm::QueueView3DragDrop(TObject * /*Sender*/,
  7410. TObject * /*Source*/, int /*X*/, int /*Y*/)
  7411. {
  7412. if ((FQueueActedItem != NULL) && (QueueView3->DropTarget != NULL))
  7413. {
  7414. TQueueItemProxy * QueueItem;
  7415. TQueueItemProxy * DestQueueItem;
  7416. QueueItem = static_cast<TQueueItemProxy *>(FQueueActedItem->Data);
  7417. DestQueueItem = static_cast<TQueueItemProxy *>(QueueView3->DropTarget->Data);
  7418. QueueItem->Move(DestQueueItem);
  7419. }
  7420. }
  7421. //---------------------------------------------------------------------------
  7422. void __fastcall TCustomScpExplorerForm::QueueView3Enter(TObject * /*Sender*/)
  7423. {
  7424. if ((QueueView3->ItemFocused == NULL) &&
  7425. (QueueView3->Items->Count > 0))
  7426. {
  7427. QueueView3->ItemFocused = QueueView3->Items->Item[0];
  7428. }
  7429. QueueLabelUpdateStatus();
  7430. }
  7431. //---------------------------------------------------------------------------
  7432. void __fastcall TCustomScpExplorerForm::QueueView3Exit(TObject * /*Sender*/)
  7433. {
  7434. QueueLabelUpdateStatus();
  7435. }
  7436. //---------------------------------------------------------------------------
  7437. void __fastcall TCustomScpExplorerForm::QueueLabelUpdateStatus()
  7438. {
  7439. QueueLabel->UpdateStatus();
  7440. }
  7441. //---------------------------------------------------------------------------
  7442. void __fastcall TCustomScpExplorerForm::QueueView3SelectItem(
  7443. TObject * /*Sender*/, TListItem * /*Item*/, bool Selected)
  7444. {
  7445. if (Selected)
  7446. {
  7447. NonVisualDataModule->UpdateNonVisibleActions();
  7448. }
  7449. }
  7450. //---------------------------------------------------------------------------
  7451. TDragDropFilesEx * __fastcall TCustomScpExplorerForm::DragDropFiles(TObject * Sender)
  7452. {
  7453. TDragDropFilesEx * Result = NULL;
  7454. if (Sender == SessionsPageControl)
  7455. {
  7456. Result = FSessionsDragDropFilesEx;
  7457. }
  7458. else if (Sender == QueueView3)
  7459. {
  7460. Result = FQueueDragDropFilesEx;
  7461. }
  7462. else
  7463. {
  7464. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  7465. if (DirView != NULL)
  7466. {
  7467. Result = DirView->DragDropFilesEx;
  7468. }
  7469. else
  7470. {
  7471. TCustomDriveView * DriveView = dynamic_cast<TCustomDriveView *>(Sender);
  7472. if (DriveView != NULL)
  7473. {
  7474. Result = DriveView->DragDropFilesEx;
  7475. }
  7476. }
  7477. }
  7478. DebugAssert(Result != NULL);
  7479. return Result;
  7480. }
  7481. //---------------------------------------------------------------------------
  7482. bool __fastcall TCustomScpExplorerForm::SelectedAllFilesInDirView(TCustomDirView * DView)
  7483. {
  7484. return (DView->SelCount == DView->FilesCount);
  7485. }
  7486. //---------------------------------------------------------------------------
  7487. bool __fastcall TCustomScpExplorerForm::DraggingAllFilesFromDirView(TOperationSide Side, TStrings * FileList)
  7488. {
  7489. return HasDirView[Side] && (DropSourceControl == DirView(Side)) && (FileList->Count == DirView(Side)->FilesCount);
  7490. }
  7491. //---------------------------------------------------------------------------
  7492. void __fastcall TCustomScpExplorerForm::RemoteFileControlDragDropFileOperation(
  7493. TObject * Sender, int Effect, UnicodeString TargetPath, bool ForceQueue, bool DragDrop)
  7494. {
  7495. DebugAssert(!IsLocalBrowserMode());
  7496. TFileOperation Operation;
  7497. switch (Effect)
  7498. {
  7499. case DROPEFFECT_MOVE:
  7500. Operation = foMove;
  7501. break;
  7502. case DROPEFFECT_COPY:
  7503. // occures on WinXP (reported by user)
  7504. default:
  7505. Operation = foCopy;
  7506. break;
  7507. };
  7508. TDragDropFilesEx * DragDropFilesEx = DragDropFiles(Sender);
  7509. // see a comment in TUnixDirView::PerformItemDragDropOperation
  7510. if (DragDropFilesEx->FileList->Count > 0)
  7511. {
  7512. TStrings * FileList = new TStringList();
  7513. try
  7514. {
  7515. for (int Index = 0; Index < DragDropFilesEx->FileList->Count; Index++)
  7516. {
  7517. FileList->Add(DragDropFilesEx->FileList->Items[Index]->Name);
  7518. }
  7519. FDragDropOperation = true;
  7520. TTransferOperationParam Param;
  7521. Param.TargetDirectory = TargetPath;
  7522. // upload, no temp dirs
  7523. Param.Temp = false;
  7524. Param.DragDrop = DragDrop;
  7525. Param.Options =
  7526. FLAGMASK(DraggingAllFilesFromDirView(osLocal, FileList), coAllFiles);
  7527. if (ForceQueue)
  7528. {
  7529. Param.Queue = asOn;
  7530. }
  7531. bool NoConfirmation = DragDrop ? (WinConfiguration->DDTransferConfirmation == asOff) : false;
  7532. if (ExecuteFileOperation(Operation, osLocal, FileList, NoConfirmation, &Param))
  7533. {
  7534. if (IsFileControl(DropSourceControl, osLocal))
  7535. {
  7536. Configuration->Usage->Inc(L"UploadsDragDropInternal");
  7537. }
  7538. else
  7539. {
  7540. Configuration->Usage->Inc(L"UploadsDragDropExternal");
  7541. }
  7542. }
  7543. }
  7544. __finally
  7545. {
  7546. FDragDropOperation = false;
  7547. delete FileList;
  7548. }
  7549. }
  7550. }
  7551. //---------------------------------------------------------------------------
  7552. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDFileOperation(
  7553. TObject * Sender, int Effect, UnicodeString /*SourcePath*/,
  7554. UnicodeString TargetPath, bool Paste, bool & /*DoOperation*/)
  7555. {
  7556. RemoteFileControlDragDropFileOperation(Sender, Effect, TargetPath, false, !Paste);
  7557. }
  7558. //---------------------------------------------------------------------------
  7559. void __fastcall TCustomScpExplorerForm::RemoteFileContolDDChooseEffect(
  7560. TObject * Sender, int grfKeyState, int & dwEffect)
  7561. {
  7562. DebugAssert(!IsLocalBrowserMode());
  7563. // if any drop effect is allowed at all (e.g. no drop to self and drop to parent)
  7564. if ((dwEffect != DROPEFFECT_NONE) &&
  7565. IsFileControl(DropSourceControl, osRemote))
  7566. {
  7567. // do not allow drop on remote panel (on free space, still allow drop on directories)
  7568. if ((Sender == RemoteDirView) && (DropSourceControl == RemoteDirView) &&
  7569. (RemoteDirView->DropTarget == NULL))
  7570. {
  7571. dwEffect = DROPEFFECT_NONE;
  7572. }
  7573. else
  7574. {
  7575. if (dwEffect == DROPEFFECT_COPY)
  7576. {
  7577. bool MoveCapable = FTerminal->IsCapable[fcRemoteMove];
  7578. // currently we support copying always (at least via temporary directory);
  7579. // remove associated checks once this all proves stable and working
  7580. bool CopyCapable = true;
  7581. // if we do not support neither of operations, there's no discussion
  7582. if (!MoveCapable && !CopyCapable)
  7583. {
  7584. dwEffect = DROPEFFECT_NONE;
  7585. }
  7586. else
  7587. {
  7588. // The default effect inside remote panel is move,
  7589. // unless we do not support it, but support copy
  7590. if (FLAGCLEAR(grfKeyState, MK_CONTROL))
  7591. {
  7592. dwEffect = MoveCapable ? DROPEFFECT_MOVE : DROPEFFECT_COPY;
  7593. }
  7594. else
  7595. {
  7596. // with ctrl-down, we want copy unless it is not supported
  7597. dwEffect = CopyCapable ? DROPEFFECT_COPY : DROPEFFECT_NONE;
  7598. }
  7599. }
  7600. }
  7601. }
  7602. }
  7603. }
  7604. //---------------------------------------------------------------------------
  7605. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDDragFileName(
  7606. TObject * Sender, TRemoteFile * File, UnicodeString & FileName)
  7607. {
  7608. DebugAssert(!IsLocalBrowserMode());
  7609. if (FDDTotalSize >= 0)
  7610. {
  7611. if (File->IsDirectory)
  7612. {
  7613. FDDTotalSize = -1;
  7614. }
  7615. else
  7616. {
  7617. FDDTotalSize += File->Size;
  7618. }
  7619. }
  7620. DebugAssert(!FDragTempDir.IsEmpty());
  7621. // TODO: this is quite ineffective
  7622. // TODO: what if invalid character replacement is disabled?
  7623. FileName = FDragTempDir + GUIConfiguration->CurrentCopyParam.ValidLocalFileName(File->FileName);
  7624. UnicodeString TransferFileName;
  7625. if (Sender == RemoteDriveView)
  7626. {
  7627. TransferFileName = UnixExcludeTrailingBackslash(File->FullFileName);
  7628. }
  7629. else
  7630. {
  7631. TransferFileName = File->FileName;
  7632. }
  7633. FDDFileList->AddObject(TransferFileName, File);
  7634. }
  7635. //---------------------------------------------------------------------------
  7636. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDDragDetect(
  7637. TObject * /*Sender*/, int /*grfKeyState*/, const TPoint & /*DetectStart*/,
  7638. const TPoint & /*Point*/, TDragDetectStatus /*DragStatus*/)
  7639. {
  7640. // sometimes we do not get DDEnd so the list is not released
  7641. SAFE_DESTROY(FDDFileList);
  7642. FDDFileList = new TStringList();
  7643. FDragTempDir = WinConfiguration->TemporaryDir();
  7644. FDDTotalSize = 0;
  7645. FDragDropSshTerminate.reset(NULL);
  7646. }
  7647. //---------------------------------------------------------------------------
  7648. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDQueryContinueDrag(
  7649. TObject * /*Sender*/, BOOL /*FEscapePressed*/, int /*grfKeyState*/,
  7650. HRESULT & Result)
  7651. {
  7652. if (Result == DRAGDROP_S_DROP)
  7653. {
  7654. try
  7655. {
  7656. GlobalDragImageList->HideDragImage();
  7657. try
  7658. {
  7659. RemoteFileControlDDTargetDrop();
  7660. }
  7661. catch(ESshTerminate & E)
  7662. {
  7663. FDragDropSshTerminate.reset(E.Clone());
  7664. }
  7665. }
  7666. catch (Exception &E)
  7667. {
  7668. // If downloading fails we need to cancel drag&drop, otherwise
  7669. // Explorer shows error
  7670. // But by the way exception probably never reach this point as
  7671. // it's catched on way.
  7672. // Fatal exceptions get here (like when opening a secondary shell extension for file duplication fails).
  7673. Result = DRAGDROP_S_CANCEL;
  7674. ShowExtendedException(Terminal, &E);
  7675. }
  7676. }
  7677. }
  7678. //---------------------------------------------------------------------------
  7679. void __fastcall TCustomScpExplorerForm::DirViewMatchMask(
  7680. TObject * /*Sender*/, UnicodeString FileName, bool Directory, __int64 Size,
  7681. TDateTime Modification, UnicodeString Masks, bool & Matches, bool AllowImplicitMatches)
  7682. {
  7683. TFileMasks::TParams MaskParams;
  7684. MaskParams.Size = Size;
  7685. MaskParams.Modification = Modification;
  7686. // this does not re-parse the mask if it is the same as the last time
  7687. FDirViewMatchMask = Masks;
  7688. bool ImplicitMatch;
  7689. Matches =
  7690. // RecurseInclude parameter has no effect as the Path is empty
  7691. FDirViewMatchMask.Matches(FileName, Directory, UnicodeString(L""), &MaskParams, true, ImplicitMatch) &&
  7692. (AllowImplicitMatches || !ImplicitMatch);
  7693. }
  7694. //---------------------------------------------------------------------------
  7695. void __fastcall TCustomScpExplorerForm::DirViewGetOverlay(
  7696. TObject * Sender, TListItem * Item, WORD & Indexes)
  7697. {
  7698. TCustomDirView * DirView = reinterpret_cast<TCustomDirView *>(Sender);
  7699. UnicodeString Ext;
  7700. if (dynamic_cast<TUnixDirView *>(DirView) != NULL)
  7701. {
  7702. Ext = UnixExtractFileExt(DirView->ItemFileName(Item));
  7703. }
  7704. else
  7705. {
  7706. Ext = ExtractFileExt(DirView->ItemFileName(Item));
  7707. }
  7708. if (SameText(Ext, Configuration->PartialExt))
  7709. {
  7710. Indexes |= oiPartial;
  7711. }
  7712. }
  7713. //---------------------------------------------------------------------------
  7714. bool __fastcall TCustomScpExplorerForm::CanPasteToDirViewFromClipBoard()
  7715. {
  7716. return
  7717. DirViewEnabled(osCurrent) &&
  7718. DirView(osCurrent)->CanPasteFromClipBoard();
  7719. }
  7720. //---------------------------------------------------------------------------
  7721. bool __fastcall TCustomScpExplorerForm::CanPasteFromClipBoard()
  7722. {
  7723. bool Result = false;
  7724. if (CanPasteToDirViewFromClipBoard())
  7725. {
  7726. Result = true;
  7727. }
  7728. else
  7729. {
  7730. UnicodeString ClipboardText;
  7731. if (NonEmptyTextFromClipboard(ClipboardText))
  7732. {
  7733. if (StoredSessions->IsUrl(ClipboardText))
  7734. {
  7735. Result = true;
  7736. }
  7737. else
  7738. {
  7739. Result = DirViewEnabled(osCurrent);
  7740. }
  7741. }
  7742. }
  7743. return Result;
  7744. }
  7745. //---------------------------------------------------------------------------
  7746. void __fastcall TCustomScpExplorerForm::PasteFromClipBoard()
  7747. {
  7748. if (DoesClipboardContainOurFiles())
  7749. {
  7750. if (DebugAlwaysTrue(CanPasteToDirViewFromClipBoard()))
  7751. {
  7752. DebugAssert(!IsLocalBrowserMode());
  7753. TTerminalManager * Manager = TTerminalManager::Instance();
  7754. TTerminal * TergetTerminal = Manager->ActiveTerminal;
  7755. Manager->ActiveTerminal = FClipboardTerminal;
  7756. ExecuteFileOperation(foRemoteCopy, osRemote, FClipboardFileList.get(), false, TergetTerminal);
  7757. }
  7758. }
  7759. else if (CanPasteToDirViewFromClipBoard())
  7760. {
  7761. DirView(osCurrent)->PasteFromClipBoard();
  7762. }
  7763. else
  7764. {
  7765. UnicodeString ClipboardText;
  7766. if (NonEmptyTextFromClipboard(ClipboardText))
  7767. {
  7768. if (StoredSessions->IsUrl(ClipboardText))
  7769. {
  7770. NewSession(false, ClipboardText);
  7771. }
  7772. else
  7773. {
  7774. DirView(osCurrent)->Path = ClipboardText;
  7775. }
  7776. }
  7777. }
  7778. }
  7779. //---------------------------------------------------------------------------
  7780. void __fastcall TCustomScpExplorerForm::FileListFromClipboard()
  7781. {
  7782. // TBD
  7783. }
  7784. //---------------------------------------------------------------------------
  7785. void __fastcall TCustomScpExplorerForm::SelectAll(TOperationSide Side, TSelectMode Mode)
  7786. {
  7787. TCustomDirView * ADirView = DirView(Side);
  7788. ADirView->SelectAll(Mode);
  7789. ADirView->SetFocus();
  7790. }
  7791. //---------------------------------------------------------------------------
  7792. void __fastcall TCustomScpExplorerForm::SelectByMask(TOperationSide Side, bool Select)
  7793. {
  7794. TCustomDirView * ADirView = DirView(Side);
  7795. TFileFilter Filter;
  7796. DefaultFileFilter(Filter);
  7797. if (DoSelectMaskDialog(ADirView, Select, Filter))
  7798. {
  7799. Configuration->Usage->Inc(L"MaskSelections");
  7800. ADirView->SelectFiles(Filter, Select);
  7801. ADirView->SetFocus();
  7802. }
  7803. }
  7804. //---------------------------------------------------------------------------
  7805. void __fastcall TCustomScpExplorerForm::RestoreSelectedNames(TOperationSide Side)
  7806. {
  7807. TCustomDirView * ADirView = DirView(Side);
  7808. ADirView->RestoreSelectedNames();
  7809. ADirView->SetFocus();
  7810. }
  7811. //---------------------------------------------------------------------------
  7812. void __fastcall TCustomScpExplorerForm::SelectSameExt(bool Select)
  7813. {
  7814. TCustomDirView * CurrentDirView = DirView(osCurrent);
  7815. if (DebugAlwaysTrue(CurrentDirView->ItemFocused != NULL))
  7816. {
  7817. UnicodeString FileName = CurrentDirView->ItemFileName(CurrentDirView->ItemFocused);
  7818. UnicodeString Ext;
  7819. if (!IsSideLocalBrowser(osCurrent))
  7820. {
  7821. Ext = UnixExtractFileExt(FileName);
  7822. }
  7823. else
  7824. {
  7825. Ext = ExtractFileExt(FileName);
  7826. }
  7827. if (Ext.IsEmpty())
  7828. {
  7829. Ext = L".";
  7830. }
  7831. TFileFilter Filter;
  7832. Filter.Masks = FORMAT(L"*%s", (Ext));
  7833. Filter.Directories = false;
  7834. CurrentDirView->SelectFiles(Filter, Select);
  7835. }
  7836. }
  7837. //---------------------------------------------------------------------------
  7838. UnicodeString __fastcall TCustomScpExplorerForm::FileStatusBarText(
  7839. const TStatusFileInfo & FileInfo, TOperationSide Side)
  7840. {
  7841. UnicodeString Result;
  7842. if (!FIncrementalSearch.IsEmpty() && (Side == GetSide(osCurrent)))
  7843. {
  7844. Result = FormatIncrementalSearchStatus(FIncrementalSearch, FIncrementalSearchHaveNext);
  7845. }
  7846. else if (!IsSideLocalBrowser(Side) && ((Terminal == NULL) || Terminal->Disconnected))
  7847. {
  7848. // noop
  7849. }
  7850. else
  7851. {
  7852. Result =
  7853. FMTLOAD(FILE_INFO_FORMAT,
  7854. (FormatBytes(FileInfo.SelectedSize),
  7855. FormatBytes(FileInfo.FilesSize),
  7856. FormatNumber(FileInfo.SelectedCount),
  7857. FormatNumber(FileInfo.FilesCount)));
  7858. }
  7859. return Result;
  7860. }
  7861. //---------------------------------------------------------------------------
  7862. void __fastcall TCustomScpExplorerForm::FileStatusBarPanelClick(
  7863. TTBXStatusPanel * Panel, TOperationSide Side)
  7864. {
  7865. if (Panel->Index == 1)
  7866. {
  7867. ToggleShowHiddenFiles();
  7868. }
  7869. else if (Panel->Index == 2)
  7870. {
  7871. Filter(Side);
  7872. }
  7873. }
  7874. //---------------------------------------------------------------------------
  7875. void __fastcall TCustomScpExplorerForm::UpdateFileStatusBar(
  7876. TTBXStatusBar * StatusBar, const TStatusFileInfo & FileInfo, TOperationSide Side)
  7877. {
  7878. DebugAssert(!StatusBar->SimplePanel);
  7879. StatusBar->Panels->Items[0]->Caption = FileStatusBarText(FileInfo, Side);
  7880. UpdateFileStatusExtendedPanels(StatusBar, FileInfo);
  7881. }
  7882. //---------------------------------------------------------------------------
  7883. void __fastcall TCustomScpExplorerForm::UpdateFileStatusExtendedPanels(
  7884. TTBXStatusBar * StatusBar, const TStatusFileInfo & FileInfo)
  7885. {
  7886. DebugAssert(StatusBar->Panels->Count >= 3);
  7887. TTBXStatusPanel * HiddenFilesPanel = StatusBar->Panels->Items[1];
  7888. if (FileInfo.HiddenCount > 0)
  7889. {
  7890. HiddenFilesPanel->Caption = FMTLOAD(FILE_INFO_HIDDEN2, (FormatNumber(FileInfo.HiddenCount)));
  7891. HiddenFilesPanel->ViewPriority = 90; // <100 allows hiding panel when it does not fit
  7892. }
  7893. else
  7894. {
  7895. HiddenFilesPanel->ViewPriority = 0;
  7896. // not really necessary, just to cleanup no-longer-valid data
  7897. HiddenFilesPanel->Caption = L"";
  7898. }
  7899. TTBXStatusPanel * FilteredFilesPanel = StatusBar->Panels->Items[2];
  7900. if (FileInfo.FilteredCount > 0)
  7901. {
  7902. FilteredFilesPanel->Caption = FMTLOAD(FILE_INFO_FILTERED2, (FormatNumber(FileInfo.FilteredCount)));
  7903. FilteredFilesPanel->ViewPriority = 90; // <100 allows hiding panel when it does not fit
  7904. }
  7905. else
  7906. {
  7907. FilteredFilesPanel->ViewPriority = 0;
  7908. // not really necessary, just to cleanup no-longer-valid data
  7909. FilteredFilesPanel->Caption = L"";
  7910. }
  7911. }
  7912. //---------------------------------------------------------------------------
  7913. void __fastcall TCustomScpExplorerForm::RemoteStatusBarClick(
  7914. TObject * /*Sender*/)
  7915. {
  7916. if (DirView(osRemote)->Enabled)
  7917. {
  7918. DirView(osRemote)->SetFocus();
  7919. }
  7920. }
  7921. //---------------------------------------------------------------------------
  7922. void __fastcall TCustomScpExplorerForm::ToggleQueueVisibility()
  7923. {
  7924. TQueueViewConfiguration Config = WinConfiguration->QueueView;
  7925. switch (Config.Show)
  7926. {
  7927. case qvShow:
  7928. if ((FQueueStatus != NULL) && (FQueueStatus->Count > 0))
  7929. {
  7930. Config.Show = qvHide;
  7931. }
  7932. else
  7933. {
  7934. Config.Show = Config.LastHideShow;
  7935. }
  7936. break;
  7937. case qvHideWhenEmpty:
  7938. if (ComponentVisible[fcQueueView])
  7939. {
  7940. Config.Show = qvHide;
  7941. }
  7942. else
  7943. {
  7944. Config.LastHideShow = Config.Show;
  7945. Config.Show = qvShow;
  7946. }
  7947. break;
  7948. case qvHide:
  7949. Config.LastHideShow = Config.Show;
  7950. Config.Show = qvShow;
  7951. break;
  7952. }
  7953. WinConfiguration->QueueView = Config;
  7954. }
  7955. //---------------------------------------------------------------------------
  7956. UnicodeString __fastcall TCustomScpExplorerForm::PathForCaption()
  7957. {
  7958. UnicodeString Result;
  7959. if (FTerminal != NULL)
  7960. {
  7961. switch (WinConfiguration->PathInCaption)
  7962. {
  7963. case picShort:
  7964. Result = TTerminalManager::Instance()->GetTerminalShortPath(FTerminal);
  7965. break;
  7966. case picFull:
  7967. Result = FTerminal->CurrentDirectory;
  7968. break;
  7969. }
  7970. }
  7971. return Result;
  7972. }
  7973. //---------------------------------------------------------------------------
  7974. TColor __fastcall TCustomScpExplorerForm::PanelColor()
  7975. {
  7976. TColor Result = GetWindowColor(FSessionColor);
  7977. return Result;
  7978. }
  7979. //---------------------------------------------------------------------------
  7980. TColor __fastcall TCustomScpExplorerForm::DisabledPanelColor()
  7981. {
  7982. TColor Result = (WinConfiguration->UseDarkTheme() ? static_cast<TColor>(RGB(0x40, 0x40, 0x40)) : clBtnFace);
  7983. return Result;
  7984. }
  7985. //---------------------------------------------------------------------------
  7986. void __fastcall TCustomScpExplorerForm::UpdateControls()
  7987. {
  7988. TTerminalManager::Instance()->UpdateAppTitle();
  7989. // WORAKRDOUND: Disabling list view when it is not showing yet does not set its
  7990. // background to gray on Windows 7 (works on Windows 10).
  7991. // See also EnableControl
  7992. if (Showing)
  7993. {
  7994. if (Terminal != NULL)
  7995. {
  7996. // Update path when it changes
  7997. if ((SessionsPageControl->ActivePage != NULL) && (GetSessionTabTerminal(SessionsPageControl->ActivePage) == Terminal))
  7998. {
  7999. UpdateSessionTab(SessionsPageControl->ActivePage);
  8000. }
  8001. }
  8002. NonVisualDataModule->ReconnectSessionAction->Update();
  8003. ReconnectToolbar->Visible = NonVisualDataModule->ReconnectSessionAction->Visible;
  8004. // ReconnectSessionAction is hidden when disabled, so enabling it actualy resizes the toolbar
  8005. CenterReconnectToolbar();
  8006. bool HasTerminal = (Terminal != NULL) && Terminal->Active;
  8007. if (HasTerminal)
  8008. {
  8009. // TODO needed yet with second local browser?
  8010. if (!RemoteDirView->Enabled)
  8011. {
  8012. RemoteDirView->Enabled = true;
  8013. if (FRemoteDirViewWasFocused)
  8014. {
  8015. ActiveControl = RemoteDirView;
  8016. }
  8017. }
  8018. RemoteDirView->Color = PanelColor();
  8019. }
  8020. else
  8021. {
  8022. if (RemoteDirView->Enabled)
  8023. {
  8024. // This is first called when the form is being constructed
  8025. // (not anymore due to Showing test above)
  8026. // but the false is overriden in the constructor later.
  8027. // An even later in TScpCommanderForm::DoShow()
  8028. FRemoteDirViewWasFocused = (ActiveControl == RemoteDirView);
  8029. RemoteDirView->Enabled = false;
  8030. }
  8031. RemoteDirView->Color = DisabledPanelColor();
  8032. }
  8033. RemoteDirView->Font->Color = GetWindowTextColor(RemoteDirView->Color);
  8034. RemoteDriveView->Enabled = RemoteDirView->Enabled;
  8035. RemoteDriveView->Color = RemoteDirView->Color;
  8036. RemoteDriveView->Font->Color = RemoteDirView->Font->Color;
  8037. QueueView3->Enabled = HasTerminal && Terminal->IsCapable[fcBackgroundTransfers];
  8038. QueueView3->Color = QueueView3->Enabled ? GetWindowColor() : DisabledPanelColor();
  8039. QueueView3->Font->Color = GetWindowTextColor(QueueView3->Color);
  8040. QueueLabelUpdateStatus();
  8041. RemoteDirView->DarkMode = WinConfiguration->UseDarkTheme();
  8042. RemoteDriveView->DarkMode = RemoteDirView->DarkMode;
  8043. reinterpret_cast<TTBCustomItem *>(GetComponent(fcRemotePathComboBox))->Enabled = HasTerminal;
  8044. }
  8045. }
  8046. //---------------------------------------------------------------------------
  8047. void __fastcall TCustomScpExplorerForm::DoDirViewLoaded(TCustomDirView * /*Sender*/)
  8048. {
  8049. UpdateControls();
  8050. }
  8051. //---------------------------------------------------------------------------
  8052. void __fastcall TCustomScpExplorerForm::DirViewLoaded(
  8053. TObject * Sender)
  8054. {
  8055. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  8056. DebugAssert(DirView != NULL);
  8057. DoDirViewLoaded(DirView);
  8058. TransferPresetAutoSelect();
  8059. }
  8060. //---------------------------------------------------------------------------
  8061. void __fastcall TCustomScpExplorerForm::StartUpdates()
  8062. {
  8063. TUpdatesConfiguration Updates = WinConfiguration->Updates;
  8064. // first run after installation
  8065. if (double(Updates.LastCheck) == 0)
  8066. {
  8067. // make sure next time there will be an update (if enabled)
  8068. Updates.LastCheck = TDateTime(1);
  8069. WinConfiguration->Updates = Updates;
  8070. }
  8071. else if ((double(Updates.Period) > 0) &&
  8072. (Now() - Updates.LastCheck >= Updates.Period))
  8073. {
  8074. TThreadMethod OnUpdatesChecked = NULL;
  8075. if (!IsUWP())
  8076. {
  8077. OnUpdatesChecked = UpdatesChecked;
  8078. }
  8079. StartUpdateThread(OnUpdatesChecked);
  8080. }
  8081. }
  8082. //---------------------------------------------------------------------------
  8083. void __fastcall TCustomScpExplorerForm::UpdatesChecked()
  8084. {
  8085. UnicodeString Message;
  8086. bool New;
  8087. TQueryType Type;
  8088. GetUpdatesMessage(Message, New, Type, false);
  8089. if (!Message.IsEmpty())
  8090. {
  8091. if (New)
  8092. {
  8093. Message = FMTLOAD(NEW_VERSION_CLICK, (Message));
  8094. }
  8095. if (!New && (Type != qtWarning))
  8096. {
  8097. PostNote(UnformatMessage(Message), 0, UpdatesNoteClicked, NULL);
  8098. }
  8099. else
  8100. {
  8101. Notify(NULL, Message, Type, true, UpdatesNoteClicked);
  8102. }
  8103. if (New)
  8104. {
  8105. Configuration->Usage->Inc(L"UpdateNotifications");
  8106. }
  8107. }
  8108. }
  8109. //---------------------------------------------------------------------------
  8110. void __fastcall TCustomScpExplorerForm::UpdatesNoteClicked(TObject * /*Sender*/)
  8111. {
  8112. RestoreApp();
  8113. bool CanDisplay = !NonVisualDataModule->Busy;
  8114. if (!CanDisplay && (Screen->ActiveForm != NULL))
  8115. {
  8116. CanDisplay = (Screen->ActiveForm->Perform(WM_CAN_DISPLAY_UPDATES, 0, 0) != 0);
  8117. }
  8118. if (CanDisplay)
  8119. {
  8120. Configuration->Usage->Inc(L"UpdateNotificationsClicked");
  8121. CheckForUpdates(true);
  8122. }
  8123. else
  8124. {
  8125. Configuration->Usage->Inc(L"UpdateNotificationsBusy");
  8126. }
  8127. }
  8128. //---------------------------------------------------------------------------
  8129. void __fastcall TCustomScpExplorerForm::GetTransferPresetAutoSelectData(
  8130. TCopyParamRuleData & Data)
  8131. {
  8132. DebugAssert(Terminal != NULL);
  8133. Data.HostName = Terminal->SessionData->HostNameExpanded;
  8134. Data.UserName = Terminal->SessionData->UserNameExpanded;
  8135. Data.RemoteDirectory = RemoteDirView->PathName;
  8136. }
  8137. //---------------------------------------------------------------------------
  8138. void __fastcall TCustomScpExplorerForm::TransferPresetAutoSelect()
  8139. {
  8140. // Terminal can be null when we are changing local directory implicitly,
  8141. // because it has been deleted, while no session is connected
  8142. // (Login dialog is open)
  8143. if (FAllowTransferPresetAutoSelect && (Terminal != NULL))
  8144. {
  8145. DebugAssert(!IsLocalBrowserMode());
  8146. TCopyParamRuleData Data;
  8147. GetTransferPresetAutoSelectData(Data);
  8148. int CopyParamIndex = GUIConfiguration->CopyParamList->Find(Data);
  8149. UnicodeString CopyParamCurrent = GUIConfiguration->CopyParamCurrent;
  8150. if (CopyParamIndex < 0)
  8151. {
  8152. // there is no preset that matches autoselection
  8153. // set preset that we consider "default"
  8154. FCopyParamAutoSelected = L""; // forget last autoselected preset
  8155. GUIConfiguration->CopyParamCurrent = FCopyParamDefault;
  8156. }
  8157. else
  8158. {
  8159. // there is preset matching autoselection
  8160. UnicodeString CopyParamName = GUIConfiguration->CopyParamList->Names[CopyParamIndex];
  8161. if (CopyParamName == FCopyParamAutoSelected)
  8162. {
  8163. // autoselected the same preset as the last time
  8164. // make no change (i.e. preserve custom user preset, if any)
  8165. }
  8166. else
  8167. {
  8168. // autoselected the different preset then the last time (or there
  8169. // was default preset set)
  8170. FCopyParamAutoSelected = CopyParamName; // remember autoselection
  8171. GUIConfiguration->CopyParamCurrent = CopyParamName;
  8172. }
  8173. }
  8174. if (GUIConfiguration->CopyParamCurrent != CopyParamCurrent)
  8175. {
  8176. if (CopyParamIndex >= 0)
  8177. {
  8178. Configuration->Usage->Inc(L"CopyParamAutoSelects");
  8179. }
  8180. TTransferPresetNoteData * Data = new TTransferPresetNoteData;
  8181. try
  8182. {
  8183. int Fmt =
  8184. (CopyParamIndex < 0) ?
  8185. (GUIConfiguration->CopyParamIndex < 0 ? COPY_PARAM_DEFAULT_NORM : COPY_PARAM_DEFAULT_CUSTOM) :
  8186. COPY_PARAM_AUTOSELECTED;
  8187. UnicodeString Message = FMTLOAD(Fmt, (StripHotkey(GUIConfiguration->CopyParamCurrent)));
  8188. Data->Message = MainInstructions(Message);
  8189. int CopyParamAttrs = Terminal->UsableCopyParamAttrs(0).General;
  8190. UnicodeString Info = GUIConfiguration->CurrentCopyParam.GetInfoStr(L"\n",
  8191. CopyParamAttrs);
  8192. if (CopyParamIndex >= 0)
  8193. {
  8194. DebugAssert(GUIConfiguration->CopyParamList->Rules[CopyParamIndex] != NULL);
  8195. Info = FORMAT(L"%s\n \n%s", (Info,
  8196. FMTLOAD(COPY_PARAM_RULE,
  8197. (GUIConfiguration->CopyParamList->Rules[CopyParamIndex]->GetInfoStr(L"\n")))));
  8198. }
  8199. Data->Message += L"\n\n" + Info;
  8200. if (WinConfiguration->CopyParamAutoSelectNotice)
  8201. {
  8202. TransferPresetNoteMessage(Data, true);
  8203. }
  8204. else
  8205. {
  8206. PostNote(Message, 0, TransferPresetNoteClicked, Data);
  8207. Data = NULL; // ownership passed
  8208. }
  8209. }
  8210. __finally
  8211. {
  8212. delete Data;
  8213. }
  8214. }
  8215. }
  8216. }
  8217. //---------------------------------------------------------------------------
  8218. void __fastcall TCustomScpExplorerForm::TransferPresetNoteMessage(
  8219. TTransferPresetNoteData * NoteData, bool AllowNeverAskAgain)
  8220. {
  8221. DebugAssert(NoteData != NULL);
  8222. DebugAssert(!IsLocalBrowserMode());
  8223. TMessageParams Params(AllowNeverAskAgain ? mpNeverAskAgainCheck : 0);
  8224. TQueryButtonAlias Aliases[1];
  8225. Aliases[0].Button = qaIgnore; // "ignore" is after "ok"
  8226. Aliases[0].Alias = LoadStr(CONFIGURE_BUTTON);
  8227. Params.Aliases = Aliases;
  8228. Params.AliasesCount = LENOF(Aliases);
  8229. unsigned int Result =
  8230. MoreMessageDialog(NoteData->Message, NULL, qtInformation,
  8231. qaOK | qaIgnore, HELP_COPY_PARAM_AUTOSELECTED, &Params);
  8232. switch (Result)
  8233. {
  8234. case qaNeverAskAgain:
  8235. DebugAssert(AllowNeverAskAgain);
  8236. WinConfiguration->CopyParamAutoSelectNotice = false;
  8237. break;
  8238. case qaIgnore:
  8239. PreferencesDialog(pmPresets);
  8240. break;
  8241. }
  8242. }
  8243. //---------------------------------------------------------------------------
  8244. void __fastcall TCustomScpExplorerForm::TransferPresetNoteClicked(TObject * Sender)
  8245. {
  8246. // as of now useless, as this is used for notes only, never for balloons, ...
  8247. RestoreApp();
  8248. // .. and we should never be busy here
  8249. if (DebugAlwaysTrue(!NonVisualDataModule->Busy))
  8250. {
  8251. TransferPresetNoteMessage(DebugNotNull(dynamic_cast<TTransferPresetNoteData *>(Sender)), false);
  8252. }
  8253. }
  8254. //---------------------------------------------------------------------------
  8255. void __fastcall TCustomScpExplorerForm::PreferencesDialog(
  8256. TPreferencesMode APreferencesMode)
  8257. {
  8258. std::unique_ptr<TPreferencesDialogData> PreferencesData;
  8259. TCopyParamRuleData Data;
  8260. if (Terminal != NULL)
  8261. {
  8262. PreferencesData.reset(new TPreferencesDialogData());
  8263. GetTransferPresetAutoSelectData(Data);
  8264. PreferencesData->CopyParamRuleData = &Data;
  8265. }
  8266. DoPreferencesDialog(APreferencesMode, PreferencesData.get());
  8267. }
  8268. //---------------------------------------------------------------------------
  8269. void __fastcall TCustomScpExplorerForm::AdHocCustomCommandValidate(
  8270. const TCustomCommandType & Command)
  8271. {
  8272. if (CustomCommandState(Command, FEditingFocusedAdHocCommand, ccltAll) <= 0)
  8273. {
  8274. throw Exception(FMTLOAD(CUSTOM_COMMAND_IMPOSSIBLE, (Command.Command)));
  8275. }
  8276. }
  8277. //---------------------------------------------------------------------------
  8278. void __fastcall TCustomScpExplorerForm::AdHocCustomCommand(bool OnFocused)
  8279. {
  8280. bool RemoteAllowed = CustomCommandRemoteAllowed();
  8281. TCustomCommandType Command;
  8282. // make sure we use local custom command when remote are not supported
  8283. if (RemoteAllowed || FLAGSET(FLastCustomCommand.Params, ccLocal))
  8284. {
  8285. Command = FLastCustomCommand;
  8286. }
  8287. else
  8288. {
  8289. Command.Params = Command.Params | ccLocal;
  8290. }
  8291. Command.Name = LoadStr(CUSTOM_COMMAND_AD_HOC_NAME);
  8292. FEditingFocusedAdHocCommand = OnFocused;
  8293. bool LocalSide = IsSideLocalBrowser(FCurrentSide);
  8294. int Options =
  8295. FLAGMASK((!RemoteAllowed || LocalSide), ccoDisableRemote) |
  8296. FLAGMASK(LocalSide, ccoDisableRemoteFiles);
  8297. if (DoCustomCommandDialog(Command, WinConfiguration->CustomCommandList,
  8298. ccmAdHoc, Options, AdHocCustomCommandValidate, NULL))
  8299. {
  8300. FLastCustomCommand = Command;
  8301. UpdateCustomCommandsToolbar();
  8302. ExecuteFileOperation(foCustomCommand, osRemote, OnFocused, false, &Command);
  8303. }
  8304. }
  8305. //---------------------------------------------------------------------------
  8306. void __fastcall TCustomScpExplorerForm::LastCustomCommand(bool OnFocused)
  8307. {
  8308. DebugAssert(!FLastCustomCommand.Command.IsEmpty());
  8309. int State = CustomCommandState(FLastCustomCommand, OnFocused, ccltAll);
  8310. DebugAssert(State > 0);
  8311. if (State <= 0)
  8312. {
  8313. throw Exception(FMTLOAD(CUSTOM_COMMAND_IMPOSSIBLE, (FLastCustomCommand.Command)));
  8314. }
  8315. ExecuteFileOperation(foCustomCommand, osRemote, OnFocused, false, &FLastCustomCommand);
  8316. }
  8317. //---------------------------------------------------------------------------
  8318. bool __fastcall TCustomScpExplorerForm::GetLastCustomCommand(bool OnFocused,
  8319. TCustomCommandType & Command, int & State)
  8320. {
  8321. bool Result = !FLastCustomCommand.Command.IsEmpty();
  8322. if (Result)
  8323. {
  8324. Command = FLastCustomCommand;
  8325. State = CustomCommandState(FLastCustomCommand, OnFocused, ccltAll);
  8326. }
  8327. return Result;
  8328. }
  8329. //---------------------------------------------------------------------------
  8330. void __fastcall TCustomScpExplorerForm::BeforeAction()
  8331. {
  8332. ResetIncrementalSearch();
  8333. }
  8334. //---------------------------------------------------------------------------
  8335. void __fastcall TCustomScpExplorerForm::PostComponentHide(Byte Component)
  8336. {
  8337. DebugAssert(ComponentVisible[Component]);
  8338. PostMessage(Handle, WM_COMPONENT_HIDE, Component, 0);
  8339. }
  8340. //---------------------------------------------------------------------------
  8341. void __fastcall TCustomScpExplorerForm::QueueSplitterDblClick(TObject * /*Sender*/)
  8342. {
  8343. // when queue panel is resized here directly, the status bar is stretched
  8344. // over whole space the panel occupied
  8345. PostComponentHide(fcQueueView);
  8346. }
  8347. //---------------------------------------------------------------------------
  8348. void __fastcall TCustomScpExplorerForm::ThemeChanged()
  8349. {
  8350. // We hoped this will refresh scrollbar colors, but it does not have any effect here.
  8351. RefreshColorMode();
  8352. WinConfiguration->ResetSysDarkTheme();
  8353. ConfigurationChanged();
  8354. ConfigureInterface();
  8355. // Should be called for all controls
  8356. RemoteDirView->Perform(WM_THEMECHANGED, 0, 0);
  8357. }
  8358. //---------------------------------------------------------------------------
  8359. void __fastcall TCustomScpExplorerForm::WMSettingChange(TMessage & Message)
  8360. {
  8361. // Do not handle, when shutting down anyway (maybe also when not setup completelly yet?)
  8362. if (!FInvalid &&
  8363. (Message.LParam != 0) &&
  8364. (wcscmp(reinterpret_cast<LPCWCH>(Message.LParam), L"ImmersiveColorSet") == 0))
  8365. {
  8366. ThemeChanged();
  8367. }
  8368. TForm::Dispatch(&Message);
  8369. }
  8370. //---------------------------------------------------------------------------
  8371. void __fastcall TCustomScpExplorerForm::Dispatch(void * Message)
  8372. {
  8373. TMessage * M = static_cast<TMessage*>(Message);
  8374. switch (M->Msg)
  8375. {
  8376. case CM_DIALOGCHAR:
  8377. CMDialogChar(*M);
  8378. break;
  8379. case WM_APPCOMMAND:
  8380. WMAppCommand(*M);
  8381. break;
  8382. case WM_SYSCOMMAND:
  8383. WMSysCommand(*M);
  8384. break;
  8385. case WM_QUERYENDSESSION:
  8386. WMQueryEndSession(*M);
  8387. break;
  8388. case WM_ENDSESSION:
  8389. WMEndSession(*reinterpret_cast<TWMEndSession *>(M));
  8390. break;
  8391. #ifdef _DEBUG
  8392. case WM_WINDOWPOSCHANGED:
  8393. WMWindowPosChanged(*reinterpret_cast<TWMWindowPosMsg *>(M));
  8394. break;
  8395. case CM_CUSTOMSTYLECHANGED:
  8396. TForm::Dispatch(Message);
  8397. break;
  8398. #endif
  8399. case WM_COMPONENT_HIDE:
  8400. {
  8401. Byte Component = static_cast<Byte>(M->WParam);
  8402. // sanity check
  8403. if (ComponentVisible[Component])
  8404. {
  8405. // special treatment
  8406. if (Component == fcQueueView)
  8407. {
  8408. ToggleQueueVisibility();
  8409. DebugAssert(!ComponentVisible[fcQueueView]);
  8410. }
  8411. else
  8412. {
  8413. ComponentVisible[Component] = false;
  8414. }
  8415. }
  8416. }
  8417. break;
  8418. case WM_COPYDATA:
  8419. WMCopyData(*M);
  8420. break;
  8421. case WM_MANAGES_CAPTION:
  8422. // caption managed in TTerminalManager::UpdateAppTitle()
  8423. M->Result = 1;
  8424. break;
  8425. case WM_WANTS_MOUSEWHEEL:
  8426. M->Result = 1;
  8427. break;
  8428. case CM_SHOWINGCHANGED:
  8429. CMShowingChanged(*M);
  8430. break;
  8431. case WM_CLOSE:
  8432. WMClose(*M);
  8433. break;
  8434. case WM_DPICHANGED:
  8435. WMDpiChanged(*M);
  8436. break;
  8437. case CM_DPICHANGED:
  8438. CMDpiChanged(*M);
  8439. break;
  8440. case WM_SETTINGCHANGE:
  8441. WMSettingChange(*M);
  8442. break;
  8443. case CM_DIALOGKEY:
  8444. CMDialogKey(*reinterpret_cast<TWMKeyDown *>(M));
  8445. break;
  8446. default:
  8447. TForm::Dispatch(Message);
  8448. break;
  8449. }
  8450. }
  8451. //---------------------------------------------------------------------------
  8452. void __fastcall TCustomScpExplorerForm::UpdateImages()
  8453. {
  8454. // noop
  8455. }
  8456. //---------------------------------------------------------------------------
  8457. void __fastcall TCustomScpExplorerForm::CMDpiChanged(TMessage & Message)
  8458. {
  8459. TForm::Dispatch(&Message);
  8460. FSessionColors->Clear();
  8461. AddFixedSessionImages();
  8462. RegenerateSessionColorsImageList(FSessionColors, FSessionColorMaskImageIndex);
  8463. UpdateImages();
  8464. }
  8465. //---------------------------------------------------------------------------
  8466. void __fastcall TCustomScpExplorerForm::WMDpiChanged(TMessage & Message)
  8467. {
  8468. TForm::Dispatch(&Message);
  8469. Configuration->Usage->Inc(L"PixelsPerInchChanged");
  8470. UpdatePixelsPerInchMainWindowCounter();
  8471. }
  8472. //---------------------------------------------------------------------------
  8473. void __fastcall TCustomScpExplorerForm::WMClose(TMessage & Message)
  8474. {
  8475. // Cannot close window while we are busy.
  8476. // We cannot test this in FormCloseQuery as that is called also from
  8477. // Close(), which is called by CloseApplicationAction. So we can be busy
  8478. // there already even, when it is legitimate to close the application.
  8479. // Possibly a better place to handle this would be WMSysCommand.
  8480. if (NonVisualDataModule->Busy)
  8481. {
  8482. Message.Result = 1;
  8483. }
  8484. else
  8485. {
  8486. TForm::Dispatch(&Message);
  8487. }
  8488. }
  8489. //---------------------------------------------------------------------------
  8490. void __fastcall TCustomScpExplorerForm::CMShowingChanged(TMessage & Message)
  8491. {
  8492. TForm::Dispatch(&Message);
  8493. if (Showing && (Terminal == NULL))
  8494. {
  8495. // When we are starting minimized (i.e. from an installer),
  8496. // postpone showing Login dialog until we get restored.
  8497. // Otherwise the Login dialog (and Authentication window) show restored
  8498. // over invisible (minimized) main window.
  8499. if (WindowState == wsMinimized)
  8500. {
  8501. FNeedSession = true;
  8502. }
  8503. else
  8504. {
  8505. // This happens before application ever goes idle, so the toolbars would
  8506. // stay enabled (initial state) until the Login dialog is dismissed.
  8507. UpdateActions();
  8508. NonVisualDataModule->StartBusy();
  8509. try
  8510. {
  8511. // Need to process WM_ACTIVATEAPP before showing the Login dialog,
  8512. // otherwise the dialog does not receive focus.
  8513. // With Commander interface the ProcessMessages is called already
  8514. // by TDriveView, but with Explorer interface, we need to call it explicily
  8515. Application->ProcessMessages();
  8516. // do not reload sessions, they have been loaded just now (optimization)
  8517. NeedSession(true);
  8518. }
  8519. __finally
  8520. {
  8521. NonVisualDataModule->EndBusy();
  8522. }
  8523. }
  8524. }
  8525. }
  8526. //---------------------------------------------------------------------------
  8527. void __fastcall TCustomScpExplorerForm::CenterReconnectToolbar()
  8528. {
  8529. ReconnectToolbar->Left = (ReconnectToolbar->Parent->ClientWidth - ReconnectToolbar->Width) / 2;
  8530. ReconnectToolbar->Top = (ReconnectToolbar->Parent->ClientHeight - ReconnectToolbar->Height) / 2;
  8531. }
  8532. //---------------------------------------------------------------------------
  8533. void __fastcall TCustomScpExplorerForm::FormConstrainedResize(
  8534. TObject * /*Sender*/, int & MinWidth, int & MinHeight, int & MaxWidth,
  8535. int & MaxHeight)
  8536. {
  8537. // workaround for bug in TWinControl.CalcConstraints
  8538. // Check for empty rect (restore from iconic state) is done there only after
  8539. // call to AdjustClientRect, which enlarges the rect (for forms).
  8540. TRect R = GetClientRect();
  8541. // when restoring from iconic state, place no restrictions
  8542. if (IsRectEmpty(R))
  8543. {
  8544. MinWidth = 0;
  8545. MinHeight = 0;
  8546. MaxWidth = 32000;
  8547. MaxHeight = 32000;
  8548. }
  8549. }
  8550. //---------------------------------------------------------------------------
  8551. void __fastcall TCustomScpExplorerForm::GetSpaceAvailable(const UnicodeString Path,
  8552. TSpaceAvailable & ASpaceAvailable, bool & Close)
  8553. {
  8554. // terminal can be already closed (e.g. dropped connection)
  8555. if ((Terminal != NULL) && Terminal->IsCapable[fcCheckingSpaceAvailable])
  8556. {
  8557. Configuration->Usage->Inc(L"SpaceAvailableChecks");
  8558. try
  8559. {
  8560. Terminal->SpaceAvailable(Path, ASpaceAvailable);
  8561. }
  8562. catch(...)
  8563. {
  8564. if (!Terminal->Active)
  8565. {
  8566. Close = true;
  8567. }
  8568. throw;
  8569. }
  8570. }
  8571. }
  8572. //---------------------------------------------------------------------------
  8573. void __fastcall TCustomScpExplorerForm::FileSystemInfo()
  8574. {
  8575. const TSessionInfo & SessionInfo = Terminal->GetSessionInfo();
  8576. const TFileSystemInfo & FileSystemInfo = Terminal->GetFileSystemInfo(true);
  8577. TGetSpaceAvailable OnGetSpaceAvailable = NULL;
  8578. if (Terminal->IsCapable[fcCheckingSpaceAvailable])
  8579. {
  8580. OnGetSpaceAvailable = GetSpaceAvailable;
  8581. }
  8582. DoFileSystemInfoDialog(SessionInfo, FileSystemInfo, Terminal->CurrentDirectory,
  8583. OnGetSpaceAvailable);
  8584. }
  8585. //---------------------------------------------------------------------------
  8586. TSessionData * __fastcall TCustomScpExplorerForm::SessionDataForCode()
  8587. {
  8588. std::unique_ptr<TSessionData> Data(CloneCurrentSessionData());
  8589. Terminal->FillSessionDataForCode(Data.get());
  8590. return Data.release();
  8591. }
  8592. //---------------------------------------------------------------------------
  8593. void __fastcall TCustomScpExplorerForm::GenerateUrl(TStrings * Paths)
  8594. {
  8595. std::unique_ptr<TSessionData> Data(SessionDataForCode());
  8596. DoGenerateUrlDialog(Data.get(), Paths);
  8597. }
  8598. //---------------------------------------------------------------------------
  8599. void __fastcall TCustomScpExplorerForm::SessionGenerateUrl()
  8600. {
  8601. GenerateUrl(NULL);
  8602. }
  8603. //---------------------------------------------------------------------------
  8604. void __fastcall TCustomScpExplorerForm::FileGenerateUrl()
  8605. {
  8606. std::unique_ptr<TStrings> Paths(new TStringList());
  8607. DirView(osCurrent)->CreateFileList(false, true, Paths.get());
  8608. GenerateUrl(Paths.get());
  8609. }
  8610. //---------------------------------------------------------------------------
  8611. void __fastcall TCustomScpExplorerForm::UpdateSessionColor(TColor value)
  8612. {
  8613. FSessionColor = value;
  8614. TColor C = (value != 0 ? value : Vcl::Graphics::clNone);
  8615. TTBXColorItem * ColorItem = dynamic_cast<TTBXColorItem *>(
  8616. static_cast<TObject *>(GetComponent(fcColorMenu)));
  8617. DebugAssert(ColorItem != NULL);
  8618. ColorItem->Color = C;
  8619. NonVisualDataModule->ColorMenuItem->Color = C;
  8620. UpdateControls();
  8621. }
  8622. //---------------------------------------------------------------------------
  8623. void __fastcall TCustomScpExplorerForm::SetSessionColor(TColor value)
  8624. {
  8625. if (value != FSessionColor)
  8626. {
  8627. UpdateSessionColor(value);
  8628. // Is null when called from LastTerminalClosed
  8629. if (Terminal != NULL)
  8630. {
  8631. SessionsPageControl->ActivePage->ImageIndex = AddSessionColor(value);
  8632. }
  8633. }
  8634. }
  8635. //---------------------------------------------------------------------------
  8636. bool __fastcall TCustomScpExplorerForm::CancelNote(bool Force)
  8637. {
  8638. bool Result = FNoteTimer->Enabled;
  8639. if (Result)
  8640. {
  8641. // cannot cancel note too early
  8642. bool NotEarly =
  8643. (Now() - FNoteShown >
  8644. EncodeTimeVerbose(0, 0, (unsigned short)(WinConfiguration->NotificationsStickTime), 0));
  8645. if (Force || NotEarly)
  8646. {
  8647. FNoteTimer->Enabled = false;
  8648. FNote = L"";
  8649. SAFE_DESTROY(FNoteData);
  8650. FOnNoteClick = NULL;
  8651. FNoteHints = FNotes->Text;
  8652. FNoteHints.Delete(FNoteHints.Length() - 1, 2);
  8653. UpdateStatusBar();
  8654. }
  8655. }
  8656. return Result;
  8657. }
  8658. //---------------------------------------------------------------------------
  8659. void __fastcall TCustomScpExplorerForm::NoteTimer(TObject * /*Sender*/)
  8660. {
  8661. DebugAssert(FNoteTimer->Enabled);
  8662. CancelNote(true);
  8663. }
  8664. //---------------------------------------------------------------------------
  8665. void __fastcall TCustomScpExplorerForm::AddNote(UnicodeString Note, bool UpdateNow)
  8666. {
  8667. int P = Note.Pos(L"\n");
  8668. if (P > 0)
  8669. {
  8670. Note.SetLength(P - 1);
  8671. }
  8672. FNotes->Add(FORMAT(L"[%s] %s",
  8673. (FormatDateTime(Configuration->TimeFormat, Now()), Note)));
  8674. while (FNotes->Count > 10)
  8675. {
  8676. FNotes->Delete(0);
  8677. }
  8678. if (UpdateNow)
  8679. {
  8680. FNoteHints = FNotes->Text;
  8681. FNoteHints.Delete(FNoteHints.Length() - 1, 2);
  8682. UpdateStatusBar();
  8683. }
  8684. }
  8685. //---------------------------------------------------------------------------
  8686. void __fastcall TCustomScpExplorerForm::PostNote(UnicodeString Note,
  8687. unsigned int Seconds, TNotifyEvent OnNoteClick, TObject * NoteData)
  8688. {
  8689. int P = Note.Pos(L"\n");
  8690. if (P > 0)
  8691. {
  8692. Note.SetLength(P - 1);
  8693. }
  8694. FNoteHints = FNotes->Text;
  8695. FNoteHints.Delete(FNoteHints.Length() - 1, 2);
  8696. FNote = Note;
  8697. SAFE_DESTROY(FNoteData);
  8698. FNoteData = NoteData;
  8699. FOnNoteClick = OnNoteClick;
  8700. AddNote(Note, false);
  8701. UpdateStatusBar();
  8702. FNoteShown = Now();
  8703. FNoteTimer->Enabled = false;
  8704. if (Seconds == 0)
  8705. {
  8706. Seconds = WinConfiguration->NotificationsTimeout;
  8707. }
  8708. FNoteTimer->Interval = Seconds * MSecsPerSec;
  8709. FNoteTimer->Enabled = true;
  8710. }
  8711. //---------------------------------------------------------------------------
  8712. void __fastcall TCustomScpExplorerForm::ReadDirectoryCancelled()
  8713. {
  8714. PostNote(LoadStr(DIRECTORY_READING_CANCELLED), 0, NULL, NULL);
  8715. }
  8716. //---------------------------------------------------------------------------
  8717. void __fastcall TCustomScpExplorerForm::SynchronizeBrowsingChanged()
  8718. {
  8719. if (NonVisualDataModule->SynchronizeBrowsingAction->Checked)
  8720. {
  8721. Configuration->Usage->Inc(L"SynchronizeBrowsingEnabled");
  8722. }
  8723. PostNote(FORMAT(LoadStrPart(SYNC_DIR_BROWSE_TOGGLE, 1),
  8724. (LoadStrPart(SYNC_DIR_BROWSE_TOGGLE,
  8725. (NonVisualDataModule->SynchronizeBrowsingAction->Checked ? 2 : 3)))),
  8726. 0, NULL, NULL);
  8727. }
  8728. //---------------------------------------------------------------------------
  8729. void __fastcall TCustomScpExplorerForm::ToggleShowHiddenFiles()
  8730. {
  8731. WinConfiguration->ShowHiddenFiles = !WinConfiguration->ShowHiddenFiles;
  8732. PostNote(FORMAT(LoadStrPart(SHOW_HIDDEN_FILES_TOGGLE, 1),
  8733. (LoadStrPart(SHOW_HIDDEN_FILES_TOGGLE,
  8734. (WinConfiguration->ShowHiddenFiles ? 2 : 3)))), 0, NULL, NULL);
  8735. }
  8736. //---------------------------------------------------------------------------
  8737. void __fastcall TCustomScpExplorerForm::SetFormatSizeBytes(TFormatBytesStyle Style)
  8738. {
  8739. WinConfiguration->FormatSizeBytes = Style;
  8740. }
  8741. //---------------------------------------------------------------------------
  8742. void __fastcall TCustomScpExplorerForm::ToggleAutoReadDirectoryAfterOp()
  8743. {
  8744. Configuration->AutoReadDirectoryAfterOp = !Configuration->AutoReadDirectoryAfterOp;
  8745. PostNote(FORMAT(LoadStrPart(AUTO_READ_DIRECTORY_TOGGLE, 1),
  8746. (LoadStrPart(AUTO_READ_DIRECTORY_TOGGLE,
  8747. (Configuration->AutoReadDirectoryAfterOp ? 2 : 3)))), 0, NULL, NULL);
  8748. }
  8749. //---------------------------------------------------------------------------
  8750. void __fastcall TCustomScpExplorerForm::StatusBarPanelDblClick(
  8751. TTBXCustomStatusBar * Sender, TTBXStatusPanel * Panel)
  8752. {
  8753. if (Panel->Index == 0)
  8754. {
  8755. if (FOnNoteClick != NULL)
  8756. {
  8757. // prevent the user data from being freed by possible call
  8758. // to CancelNote or PostNote during call to OnNoteClick
  8759. std::unique_ptr<TObject> NoteData(FNoteData);
  8760. TNotifyEvent OnNoteClick = FOnNoteClick;
  8761. FNoteData = NULL;
  8762. // need to cancel the note as we are going to delete its user data
  8763. CancelNote(true);
  8764. OnNoteClick(NoteData.get());
  8765. }
  8766. }
  8767. if (Panel->Index >= Sender->Panels->Count - SessionPanelCount)
  8768. {
  8769. FileSystemInfo();
  8770. }
  8771. }
  8772. //---------------------------------------------------------------------------
  8773. void __fastcall TCustomScpExplorerForm::LockWindow(bool Force)
  8774. {
  8775. // workaround:
  8776. // 1) for unknown reason, disabling window, while minimized,
  8777. // prevents it from restoring, even if it was enabled again meanwhile
  8778. // 2) when disabling the main window, while another has focus
  8779. // minimize is no longer possible
  8780. // ("keep up to date" dialog - though does not seem to be the case with "find file" -
  8781. // so it has probably something to do with the semi-modalness of the "keep up to date" dialog)
  8782. // Shouldn't we use IsApplicationMinimized() here?
  8783. if ((FLockSuspendLevel == 0) && !IsIconic(Application->Handle) && (Force || (Screen->ActiveForm == this)))
  8784. {
  8785. Enabled = false;
  8786. }
  8787. FLockLevel++;
  8788. }
  8789. //---------------------------------------------------------------------------
  8790. void __fastcall TCustomScpExplorerForm::UnlockWindow()
  8791. {
  8792. DebugAssert(FLockLevel > 0);
  8793. FLockLevel--;
  8794. if (FLockLevel == 0)
  8795. {
  8796. DebugAssert(FLockSuspendLevel == 0);
  8797. Enabled = true;
  8798. }
  8799. }
  8800. //---------------------------------------------------------------------------
  8801. void __fastcall TCustomScpExplorerForm::SuspendWindowLock()
  8802. {
  8803. // We need to make sure that window is enabled when the last modal window closes
  8804. // otherwise focus is not restored correctly.
  8805. // So we re-enable the window when modal window opens and
  8806. // disable it back after is closes
  8807. if (FLockLevel > 0)
  8808. {
  8809. // while we have nesting counter, we know that we never be called
  8810. // recursivelly as Application->OnModalBegin is called only
  8811. // for the top-level modal window
  8812. if (DebugAlwaysTrue(FLockSuspendLevel == 0))
  8813. {
  8814. // won't be disabled when conditions in LockWindow() were not satisfied
  8815. FDisabledOnLockSuspend = !Enabled;
  8816. // When minimized to tray (or actually when set to SW_HIDE),
  8817. // setting Enabled makes the window focusable even when there's
  8818. // modal window over it
  8819. if (!FTrayIcon->Visible)
  8820. {
  8821. Enabled = true;
  8822. }
  8823. }
  8824. FLockSuspendLevel++;
  8825. }
  8826. }
  8827. //---------------------------------------------------------------------------
  8828. void __fastcall TCustomScpExplorerForm::ResumeWindowLock()
  8829. {
  8830. if (FLockSuspendLevel > 0)
  8831. {
  8832. DebugAssert(FLockLevel > 0);
  8833. FLockSuspendLevel--;
  8834. // see comment in SuspendWindowLock
  8835. if (DebugAlwaysTrue(FLockSuspendLevel == 0))
  8836. {
  8837. // Note that window can be enabled here, when we were minized to tray when
  8838. // was SuspendWindowLock() called.
  8839. // We should possibly do the same check as in LockWindow(),
  8840. // if it is ever possible that the consitions change between
  8841. // SuspendWindowLock() and ResumeWindowLock()
  8842. Enabled = !FDisabledOnLockSuspend;
  8843. }
  8844. }
  8845. }
  8846. //---------------------------------------------------------------------------
  8847. void __fastcall TCustomScpExplorerForm::UpdateRemotePathComboBox(bool TextOnly)
  8848. {
  8849. if (!TextOnly)
  8850. {
  8851. TTBXComboBoxItem * RemotePathComboBox =
  8852. reinterpret_cast<TTBXComboBoxItem *>(GetComponent(fcRemotePathComboBox));
  8853. TStrings * Items = RemotePathComboBox->Strings;
  8854. Items->BeginUpdate();
  8855. try
  8856. {
  8857. Items->Clear();
  8858. if ((Terminal != NULL) && !RemoteDirView->Path.IsEmpty())
  8859. {
  8860. UnicodeString APath = UnixExcludeTrailingBackslash(RemoteDirView->Path);
  8861. while (!IsUnixRootPath(APath))
  8862. {
  8863. int P = APath.LastDelimiter(L'/');
  8864. DebugAssert(P >= 0);
  8865. Items->Insert(0, APath.SubString(P + 1, APath.Length() - P));
  8866. APath.SetLength(P - 1);
  8867. }
  8868. Items->Insert(0, Customunixdirview_SUnixDefaultRootName);
  8869. }
  8870. }
  8871. __finally
  8872. {
  8873. RemotePathComboBox->ItemIndex = Items->Count - 1;
  8874. // Setting ItemIndex to -1 does not reset its text
  8875. if (Items->Count == 0)
  8876. {
  8877. RemotePathComboBox->Text = L"";
  8878. }
  8879. Items->EndUpdate();
  8880. }
  8881. }
  8882. }
  8883. //---------------------------------------------------------------------------
  8884. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxAdjustImageIndex(
  8885. TTBXComboBoxItem * Sender, const UnicodeString /*AText*/, int AIndex,
  8886. int & ImageIndex)
  8887. {
  8888. if (AIndex < 0)
  8889. {
  8890. AIndex = Sender->ItemIndex;
  8891. }
  8892. ImageIndex = (AIndex < Sender->Strings->Count - 1 ? StdDirIcon : StdDirSelIcon);
  8893. }
  8894. //---------------------------------------------------------------------------
  8895. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxDrawItem(
  8896. TTBXCustomList * /*Sender*/, TCanvas * /*ACanvas*/, TRect & ARect, int AIndex,
  8897. int /*AHoverIndex*/, bool & /*DrawDefault*/)
  8898. {
  8899. ARect.Left += (10 * AIndex);
  8900. }
  8901. //---------------------------------------------------------------------------
  8902. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxMeasureWidth(
  8903. TTBXCustomList * /*Sender*/, TCanvas * /*ACanvas*/, int AIndex, int &AWidth)
  8904. {
  8905. AWidth += (10 * AIndex);
  8906. }
  8907. //---------------------------------------------------------------------------
  8908. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxItemClick(
  8909. TObject * Sender)
  8910. {
  8911. TTBXComboBoxItem * RemotePathComboBox = dynamic_cast<TTBXComboBoxItem*>(Sender);
  8912. UnicodeString APath = UnixExcludeTrailingBackslash(RemoteDirView->Path);
  8913. int Index = RemotePathComboBox->ItemIndex;
  8914. while (Index < RemotePathComboBox->Strings->Count - 1)
  8915. {
  8916. APath = UnixExtractFileDir(APath);
  8917. Index++;
  8918. }
  8919. // VanDyke style paths
  8920. if (APath.IsEmpty())
  8921. {
  8922. DebugAssert(RemotePathComboBox->ItemIndex == 0);
  8923. APath = ROOTDIRECTORY;
  8924. }
  8925. if (RemoteDirView->Path != APath)
  8926. {
  8927. RemoteDirView->Path = APath;
  8928. }
  8929. }
  8930. //---------------------------------------------------------------------------
  8931. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxCancel(TObject * Sender)
  8932. {
  8933. DebugAssert(Sender == GetComponent(fcRemotePathComboBox));
  8934. DebugUsedParam(Sender);
  8935. UpdateRemotePathComboBox(true);
  8936. }
  8937. //---------------------------------------------------------------------------
  8938. void __fastcall TCustomScpExplorerForm::DirViewEditing(
  8939. TObject * Sender, TListItem * Item, bool & /*AllowEdit*/)
  8940. {
  8941. ResetIncrementalSearch();
  8942. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  8943. DebugAssert(DirView != NULL);
  8944. if (!WinConfiguration->RenameWholeName && !DirView->ItemIsDirectory(Item))
  8945. {
  8946. HWND Edit = ListView_GetEditControl(DirView->Handle);
  8947. // OnEditing is called also from TCustomListView::CanEdit
  8948. if (Edit != NULL)
  8949. {
  8950. EditSelectBaseName(Edit);
  8951. }
  8952. }
  8953. }
  8954. //---------------------------------------------------------------------------
  8955. //---------------------------------------------------------------------------
  8956. TDragDropFilesEx * __fastcall TCustomScpExplorerForm::CreateDragDropFilesEx()
  8957. {
  8958. TDragDropFilesEx * Result = new TDragDropFilesEx(this);
  8959. Result->AutoDetectDnD = false;
  8960. Result->NeedValid = TFileExMustDnDSet() << nvFilename;
  8961. Result->RenderDataOn = rdoEnterAndDropSync;
  8962. Result->TargetEffects = TDropEffectSet() << deCopy << deMove;
  8963. return Result;
  8964. }
  8965. //---------------------------------------------------------------------------
  8966. void __fastcall TCustomScpExplorerForm::CreateWnd()
  8967. {
  8968. TForm::CreateWnd();
  8969. // win32-darkmode calls AllowDarkModeForWindow(this, true) here, but it does not seem to have any effect
  8970. if (FSessionsDragDropFilesEx == NULL)
  8971. {
  8972. FSessionsDragDropFilesEx = CreateDragDropFilesEx();
  8973. FSessionsDragDropFilesEx->OnDragOver = SessionsDDDragOver;
  8974. FSessionsDragDropFilesEx->OnProcessDropped = SessionsDDProcessDropped;
  8975. FSessionsDragDropFilesEx->OnDragEnter = SessionsDDDragEnter;
  8976. FSessionsDragDropFilesEx->OnDragLeave = SessionsDDDragLeave;
  8977. }
  8978. if (FQueueDragDropFilesEx == NULL)
  8979. {
  8980. FQueueDragDropFilesEx = CreateDragDropFilesEx();
  8981. // No need to set OnDragOver as we do not have any restrictions
  8982. FQueueDragDropFilesEx->OnProcessDropped = QueueDDProcessDropped;
  8983. FQueueDragDropFilesEx->OnDragEnter = QueueDDDragEnter;
  8984. FQueueDragDropFilesEx->OnDragLeave = QueueDDDragLeave;
  8985. }
  8986. }
  8987. //---------------------------------------------------------------------------
  8988. void __fastcall TCustomScpExplorerForm::DestroyWnd()
  8989. {
  8990. TForm::DestroyWnd();
  8991. FSessionsDragDropFilesEx->DragDropControl = NULL;
  8992. FQueueDragDropFilesEx->DragDropControl = NULL;
  8993. }
  8994. //---------------------------------------------------------------------------
  8995. void __fastcall TCustomScpExplorerForm::FormShow(TObject * /*Sender*/)
  8996. {
  8997. SideEnter(FCurrentSide);
  8998. FEverShown = true;
  8999. }
  9000. //---------------------------------------------------------------------------
  9001. void __fastcall TCustomScpExplorerForm::DoFindFiles(
  9002. TTerminal * ATerminal, UnicodeString Directory, const TFileMasks & FileMask,
  9003. TFileFoundEvent OnFileFound, TFindingFileEvent OnFindingFile)
  9004. {
  9005. if (!NonVisualDataModule->Busy)
  9006. {
  9007. TTerminalManager::Instance()->ActiveTerminal = DebugNotNull(dynamic_cast<TManagedTerminal *>(ATerminal));
  9008. Configuration->Usage->Inc(L"FileFinds");
  9009. LockWindow(true);
  9010. NonVisualDataModule->StartBusy();
  9011. WinConfiguration->LockedInterface = true;
  9012. try
  9013. {
  9014. FTerminal->FilesFind(Directory, FileMask, OnFileFound, OnFindingFile);
  9015. }
  9016. __finally
  9017. {
  9018. WinConfiguration->LockedInterface = false;
  9019. NonVisualDataModule->EndBusy();
  9020. UnlockWindow();
  9021. }
  9022. }
  9023. }
  9024. //---------------------------------------------------------------------------
  9025. void __fastcall TCustomScpExplorerForm::DoFocusRemotePath(TTerminal * ATerminal, const UnicodeString & Path)
  9026. {
  9027. if (!NonVisualDataModule->Busy)
  9028. {
  9029. TTerminalManager::Instance()->ActiveTerminal = DebugNotNull(dynamic_cast<TManagedTerminal *>(ATerminal));
  9030. SetFocus();
  9031. RemoteDirView->Path = UnixExtractFilePath(Path);
  9032. UnicodeString FileName = UnixExtractFileName(Path);
  9033. TListItem * Item = RemoteDirView->FindFileItem(FileName);
  9034. // If not found, it can because we have loaded directory from cache and the file is new, try again after reload.
  9035. // (tbd: we should not even try this, if we have not loaded the directory from the cache)
  9036. if (Item == NULL)
  9037. {
  9038. ReloadDirectory(osRemote);
  9039. Item = RemoteDirView->FindFileItem(FileName);
  9040. }
  9041. if (Item != NULL)
  9042. {
  9043. RemoteDirView->ItemFocused = Item;
  9044. RemoteDirView->ItemFocused->MakeVisible(false);
  9045. RemoteDirView->SetFocus();
  9046. }
  9047. }
  9048. }
  9049. //---------------------------------------------------------------------------
  9050. bool __fastcall TCustomScpExplorerForm::CanOperateOnFoundFiles(TTerminal * ATerminal)
  9051. {
  9052. bool Result = !NonVisualDataModule->Busy;
  9053. if (Result)
  9054. {
  9055. TTerminalManager::Instance()->ActiveTerminal = DebugNotNull(dynamic_cast<TManagedTerminal *>(ATerminal));
  9056. }
  9057. return Result;
  9058. }
  9059. //---------------------------------------------------------------------------
  9060. void __fastcall TCustomScpExplorerForm::DoOperationOnFoundFiles(
  9061. TFileOperation Operation, TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  9062. {
  9063. if (CanOperateOnFoundFiles(ATerminal))
  9064. {
  9065. TValueRestorer<TFileOperationFinishedEvent> OnFileOperationFinishedRestorer(FOnFileOperationFinished);
  9066. FOnFileOperationFinished = OnFileOperationFinished;
  9067. ExecuteFileOperation(Operation, osRemote, FileList, false, NULL);
  9068. }
  9069. }
  9070. //---------------------------------------------------------------------------
  9071. void __fastcall TCustomScpExplorerForm::DoDeleteFoundFiles(
  9072. TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  9073. {
  9074. DoOperationOnFoundFiles(foDelete, ATerminal, FileList, OnFileOperationFinished);
  9075. }
  9076. //---------------------------------------------------------------------------
  9077. void __fastcall TCustomScpExplorerForm::DoDownloadFoundFiles(
  9078. TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  9079. {
  9080. DoOperationOnFoundFiles(foCopy, ATerminal, FileList, OnFileOperationFinished);
  9081. }
  9082. //---------------------------------------------------------------------------
  9083. void __fastcall TCustomScpExplorerForm::DoEditFoundFiles(
  9084. TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  9085. {
  9086. if (CanOperateOnFoundFiles(ATerminal))
  9087. {
  9088. for (int Index = 0; Index < FileList->Count; Index++)
  9089. {
  9090. UnicodeString FileName = FileList->Strings[Index];
  9091. TRemoteFile * File = static_cast<TRemoteFile *>(FileList->Objects[Index]);
  9092. ExecuteRemoteFile(FileName, File, efDefaultEditor);
  9093. OnFileOperationFinished(FileName, true);
  9094. }
  9095. }
  9096. }
  9097. //---------------------------------------------------------------------------
  9098. void __fastcall TCustomScpExplorerForm::RemoteFindFiles()
  9099. {
  9100. FFileFindTerminal = Terminal;
  9101. ShowFileFindDialog(
  9102. Terminal, RemoteDirView->Path, DoFindFiles, DoFocusRemotePath, DoDeleteFoundFiles,
  9103. DoDownloadFoundFiles, DoEditFoundFiles);
  9104. }
  9105. //---------------------------------------------------------------------------
  9106. void __fastcall TCustomScpExplorerForm::UpdateTaskbarList(ITaskbarList3 * TaskbarList)
  9107. {
  9108. FTaskbarList = TaskbarList;
  9109. }
  9110. //---------------------------------------------------------------------------
  9111. void __fastcall TCustomScpExplorerForm::SessionsPageControlMouseDown(
  9112. TObject * /*Sender*/, TMouseButton Button, TShiftState /*Shift*/, int X, int Y)
  9113. {
  9114. int Index = SessionsPageControl->IndexOfTabAt(X, Y);
  9115. if (Index >= 0)
  9116. {
  9117. if (Button == mbLeft)
  9118. {
  9119. // "Mouse down" is raised only after tab is switched.
  9120. // If switching tab (switching session) takes long enough for user
  9121. // to actually release the button, "mouse down" is still raised,
  9122. // but we do not get "mouse up" event, so dragging is not cancelled,
  9123. // prevent that by not beginning dragging in the first place.
  9124. if (IsKeyPressed(VK_LBUTTON))
  9125. {
  9126. // when user clicks the "+", we get mouse down only after the session
  9127. // is closed, when new session tab is already on X:Y, so dragging
  9128. // starts, prevent that
  9129. if (MilliSecondsBetween(Now(), FSessionsPageControlNewSessionTime) > 500)
  9130. {
  9131. TTerminal * Terminal = GetSessionTabTerminal(SessionsPageControl->Pages[Index]);
  9132. if (Terminal != NULL)
  9133. {
  9134. SessionsPageControl->BeginDrag(false);
  9135. }
  9136. }
  9137. }
  9138. }
  9139. else if (Button == mbMiddle)
  9140. {
  9141. // ignore middle-click for "New session tab"
  9142. TTerminal * Terminal = GetSessionTabTerminal(SessionsPageControl->Pages[Index]);
  9143. if (Terminal != NULL)
  9144. {
  9145. CloseSessionTab(Index);
  9146. }
  9147. }
  9148. }
  9149. }
  9150. //---------------------------------------------------------------------------
  9151. void __fastcall TCustomScpExplorerForm::SessionsPageControlDragDrop(
  9152. TObject * /*Sender*/, TObject * /*Source*/, int X, int Y)
  9153. {
  9154. int Index = SessionsPageControl->IndexOfTabAt(X, Y);
  9155. // do not allow dropping on the "+" tab
  9156. TTerminal * TargetTerminal = GetSessionTabTerminal(SessionsPageControl->Pages[Index]);
  9157. if ((TargetTerminal != NULL) &&
  9158. (SessionsPageControl->ActivePage->PageIndex != Index))
  9159. {
  9160. Configuration->Usage->Inc(L"SessionTabMoves");
  9161. // this is almost redundant as we would recreate tabs in DoTerminalListChanged,
  9162. // but we want to actually prevent that to avoid flicker
  9163. SessionsPageControl->ActivePage->PageIndex = Index;
  9164. TTerminal * Terminal = GetSessionTabTerminal(SessionsPageControl->ActivePage);
  9165. TTerminalManager::Instance()->Move(Terminal, TargetTerminal);
  9166. }
  9167. }
  9168. //---------------------------------------------------------------------------
  9169. void __fastcall TCustomScpExplorerForm::SessionsPageControlDragOver(
  9170. TObject * Sender, TObject * Source, int X, int Y,
  9171. TDragState /*State*/, bool & Accept)
  9172. {
  9173. Accept = (Sender == Source);
  9174. if (Accept)
  9175. {
  9176. int Index = SessionsPageControl->IndexOfTabAt(X, Y);
  9177. TTerminal * Terminal = GetSessionTabTerminal(SessionsPageControl->Pages[Index]);
  9178. // do not allow dragging to the "+" tab
  9179. Accept = (Terminal != NULL);
  9180. }
  9181. }
  9182. //---------------------------------------------------------------------------
  9183. void __fastcall TCustomScpExplorerForm::SessionsDDDragOver(
  9184. int DebugUsedArg(KeyState), const TPoint & Point, int & Effect, int DebugUsedArg(PreferredEffect))
  9185. {
  9186. int Index = SessionsPageControl->IndexOfTabAt(Point.X, Point.Y);
  9187. if (Index < 0)
  9188. {
  9189. Effect = DROPEFFECT_NONE;
  9190. }
  9191. else
  9192. {
  9193. TTerminal * TargetTerminal = GetSessionTabTerminal(SessionsPageControl->Pages[Index]);
  9194. // do not allow dropping on the "+" tab
  9195. if (TargetTerminal == NULL)
  9196. {
  9197. Effect = DROPEFFECT_NONE;
  9198. }
  9199. else if ((TargetTerminal != Terminal) && (Effect == DROPEFFECT_MOVE))
  9200. {
  9201. Effect = DROPEFFECT_COPY;
  9202. }
  9203. }
  9204. }
  9205. //---------------------------------------------------------------------------
  9206. void __fastcall TCustomScpExplorerForm::SessionsDDProcessDropped(
  9207. TObject * /*Sender*/, int /*KeyState*/, const TPoint & Point, int Effect)
  9208. {
  9209. int Index = SessionsPageControl->IndexOfTabAt(Point.X, Point.Y);
  9210. // do not allow dropping on the "+" tab
  9211. TManagedTerminal * TargetTerminal = GetSessionTabTerminal(SessionsPageControl->Pages[Index]);
  9212. if (TargetTerminal != NULL)
  9213. {
  9214. DebugAssert(!IsFileControl(DropSourceControl, osRemote));
  9215. if (!IsFileControl(DropSourceControl, osRemote))
  9216. {
  9217. TTerminalManager::Instance()->ActiveTerminal = TargetTerminal;
  9218. RemoteFileControlDragDropFileOperation(SessionsPageControl, Effect,
  9219. // Why don't we use Terminal->CurrentDirectory directly?
  9220. TTerminalManager::Instance()->ActiveTerminal->CurrentDirectory,
  9221. // do not force queue, drag drop
  9222. false, true);
  9223. }
  9224. }
  9225. }
  9226. //---------------------------------------------------------------------------
  9227. void __fastcall TCustomScpExplorerForm::QueueDDProcessDropped(
  9228. TObject * /*Sender*/, int /*KeyState*/, const TPoint & /*Point*/, int Effect)
  9229. {
  9230. // Downloads are handled in RemoteFileControlDDEnd
  9231. if (!IsFileControl(DropSourceControl, osRemote))
  9232. {
  9233. RemoteFileControlDragDropFileOperation(QueueView3, Effect,
  9234. Terminal->CurrentDirectory,
  9235. // force queue, drag drop
  9236. true, true);
  9237. }
  9238. }
  9239. //---------------------------------------------------------------------------
  9240. void __fastcall TCustomScpExplorerForm::FormClose(TObject * /*Sender*/, TCloseAction & /*Action*/)
  9241. {
  9242. FShowing = false;
  9243. // Do not save empty workspace
  9244. if (WinConfiguration->AutoSaveWorkspace && (Terminal != NULL))
  9245. {
  9246. std::unique_ptr<TObjectList> DataList(DoCollectWorkspace());
  9247. UnicodeString Name = WorkspaceName();
  9248. DoSaveWorkspace(
  9249. Name, DataList.get(),
  9250. !Configuration->DisablePasswordStoring &&
  9251. WinConfiguration->AutoSaveWorkspacePasswords, false);
  9252. WinConfiguration->LastStoredSession = Name;
  9253. }
  9254. }
  9255. //---------------------------------------------------------------------------
  9256. void __fastcall TCustomScpExplorerForm::RemoteDirViewRead(TObject * /*Sender*/)
  9257. {
  9258. TManagedTerminal * ManagedTerminal =
  9259. dynamic_cast<TManagedTerminal *>(RemoteDirView->Terminal);
  9260. if (ManagedTerminal != NULL)
  9261. {
  9262. ManagedTerminal->DirectoryLoaded = Now();
  9263. }
  9264. }
  9265. //---------------------------------------------------------------------------
  9266. void __fastcall TCustomScpExplorerForm::DirViewSelectItem(TObject * Sender,
  9267. TListItem * /*Item*/, bool /*Selected*/)
  9268. {
  9269. TCustomDirView * DirView = reinterpret_cast<TCustomDirView *>(Sender);
  9270. switch (DirView->LastSelectMethod)
  9271. {
  9272. case smKeyboard:
  9273. Configuration->Usage->Inc(L"KeyboardSelections");
  9274. break;
  9275. case smMouse:
  9276. Configuration->Usage->Inc(L"MouseSelections");
  9277. break;
  9278. }
  9279. }
  9280. //---------------------------------------------------------------------------
  9281. int __fastcall TCustomScpExplorerForm::AddSessionColor(TColor Color)
  9282. {
  9283. if (Color != 0)
  9284. {
  9285. return GetSessionColorImage(FSessionColors, Color, FSessionColorMaskImageIndex);
  9286. }
  9287. else
  9288. {
  9289. return FSessionTabImageIndex;
  9290. }
  9291. }
  9292. //---------------------------------------------------------------------------
  9293. int __fastcall TCustomScpExplorerForm::AddFixedSessionImage(int GlyphsSourceIndex)
  9294. {
  9295. TPngImageCollectionItem * Item =
  9296. GlyphsModule->ExplorerImages->PngImages->Items[GlyphsSourceIndex];
  9297. FSessionColors->AddPng(Item->PngImage);
  9298. return FSessionColors->Count - 1;
  9299. }
  9300. //---------------------------------------------------------------------------
  9301. void __fastcall TCustomScpExplorerForm::AddFixedSessionImages()
  9302. {
  9303. FSessionColors->SetSize(GlyphsModule->ExplorerImages->Width, GlyphsModule->ExplorerImages->Height);
  9304. FNewSessionTabImageIndex = AddFixedSessionImage(NonVisualDataModule->NewSessionAction->ImageIndex);
  9305. FSessionTabImageIndex = AddFixedSessionImage(SiteImageIndex);
  9306. FSessionColorMaskImageIndex = AddFixedSessionImage(SiteColorMaskImageIndex);
  9307. }
  9308. //---------------------------------------------------------------------------
  9309. void __fastcall TCustomScpExplorerForm::CollectItemsWithTextDisplayMode(TWinControl * Control)
  9310. {
  9311. for (int Index = 0; Index < Control->ControlCount; Index++)
  9312. {
  9313. TControl * AControl = Control->Controls[Index];
  9314. TWinControl * WinControl = dynamic_cast<TWinControl *>(AControl);
  9315. if (WinControl != NULL)
  9316. {
  9317. CollectItemsWithTextDisplayMode(WinControl);
  9318. }
  9319. TTBCustomToolbar * Toolbar = dynamic_cast<TTBCustomToolbar *>(AControl);
  9320. if (Toolbar != NULL)
  9321. {
  9322. // we care for top-level items only
  9323. TTBCustomItem * Items = Toolbar->Items;
  9324. for (int ItemIndex = 0; ItemIndex < Items->Count; ItemIndex++)
  9325. {
  9326. TTBCustomItem * Item = Items->Items[ItemIndex];
  9327. if (((Item->DisplayMode == nbdmImageAndText) ||
  9328. (dynamic_cast<TTBXLabelItem *>(Item) != NULL)) &&
  9329. EligibleForImageDisplayMode(Item))
  9330. {
  9331. FItemsWithTextDisplayMode.insert(Item);
  9332. }
  9333. }
  9334. }
  9335. }
  9336. }
  9337. //---------------------------------------------------------------------------
  9338. bool __fastcall TCustomScpExplorerForm::EligibleForImageDisplayMode(TTBCustomItem * /*Item*/)
  9339. {
  9340. return true;
  9341. }
  9342. //---------------------------------------------------------------------------
  9343. bool __fastcall TCustomScpExplorerForm::UpdateToolbarDisplayMode()
  9344. {
  9345. bool SelectiveToolbarText = WinConfiguration->SelectiveToolbarText;
  9346. TTBItemDisplayMode DisplayMode = (SelectiveToolbarText ? nbdmImageAndText : nbdmDefault);
  9347. typedef std::set<TTBCustomItem *> TItemsWithTextDisplayMode;
  9348. TItemsWithTextDisplayMode::iterator i = FItemsWithTextDisplayMode.begin();
  9349. bool Result = true;
  9350. while (Result && (i != FItemsWithTextDisplayMode.end()))
  9351. {
  9352. TTBCustomItem * Item = *i;
  9353. TTBXLabelItem * Label = dynamic_cast<TTBXLabelItem *>(Item);
  9354. if (Label != NULL)
  9355. {
  9356. // optimization
  9357. if (Label->Visible == SelectiveToolbarText)
  9358. {
  9359. Result = false;
  9360. }
  9361. else
  9362. {
  9363. Label->Visible = SelectiveToolbarText;
  9364. }
  9365. }
  9366. else
  9367. {
  9368. // optimization
  9369. if (Item->DisplayMode == DisplayMode)
  9370. {
  9371. Result = false;
  9372. }
  9373. else
  9374. {
  9375. Item->DisplayMode = DisplayMode;
  9376. }
  9377. }
  9378. ++i;
  9379. }
  9380. if (Result)
  9381. {
  9382. UpdateNewSessionTab();
  9383. }
  9384. return Result;
  9385. }
  9386. //---------------------------------------------------------------------------
  9387. void __fastcall TCustomScpExplorerForm::DisplaySystemContextMenu()
  9388. {
  9389. DebugFail();
  9390. }
  9391. //---------------------------------------------------------------------------
  9392. bool __fastcall TCustomScpExplorerForm::IsBusy()
  9393. {
  9394. // Among other, a lock level is non zero, while directory is being loaded,
  9395. // even when it happens as a result of dirview navigation,
  9396. // i.e. when TNonVisualDataModule is NOT FBusy.
  9397. // That's why the TNonVisualDataModule::GetBusy calls this method.
  9398. // Among other this prevents a panel auto update to occur while
  9399. // directory is changing.
  9400. return (FLockLevel > 0) || DirView(osCurrent)->IsEditing();
  9401. }
  9402. //---------------------------------------------------------------------------
  9403. Boolean __fastcall TCustomScpExplorerForm::AllowedAction(TAction * /*Action*/, TActionAllowed Allowed)
  9404. {
  9405. // While the window is disabled, we still seem to process menu shortcuts at least,
  9406. // so stop it at least here.
  9407. // See also TCustomScpExplorerForm::RemoteDirViewBusy
  9408. return
  9409. (Allowed == aaUpdate) ||
  9410. !NonVisualDataModule->Busy;
  9411. }
  9412. //---------------------------------------------------------------------------
  9413. void __fastcall TCustomScpExplorerForm::EditMenuItemPopup(TTBCustomItem * Sender, bool FromLink)
  9414. {
  9415. NonVisualDataModule->EditMenuItemPopup(Sender, FromLink);
  9416. }
  9417. //---------------------------------------------------------------------------
  9418. void __fastcall TCustomScpExplorerForm::DirViewBusy(TObject * /*Sender*/, int Busy, bool & State)
  9419. {
  9420. // This is somewhat redundant to LockWindow() call from
  9421. // TTerminalManager::TerminalReadDirectoryProgress.
  9422. // But disabling window is known not to block keyboard shorcuts
  9423. // (see TCustomScpExplorerForm::AllowedAction), this hopefully works.
  9424. if (Busy > 0)
  9425. {
  9426. if (NonVisualDataModule->Busy)
  9427. {
  9428. State = false;
  9429. }
  9430. else
  9431. {
  9432. NonVisualDataModule->StartBusy();
  9433. // Will be countered for TCustomDirView.DoExecute in DoDirViewExecFile
  9434. LockWindow();
  9435. }
  9436. }
  9437. else if (Busy < 0)
  9438. {
  9439. UnlockWindow();
  9440. NonVisualDataModule->EndBusy();
  9441. }
  9442. else
  9443. {
  9444. State = NonVisualDataModule->Busy;
  9445. }
  9446. }
  9447. //---------------------------------------------------------------------------
  9448. void __fastcall TCustomScpExplorerForm::SessionsPageControlContextPopup(TObject * /*Sender*/, TPoint & MousePos, bool & Handled)
  9449. {
  9450. int Index = SessionsPageControl->IndexOfTabAt(MousePos.X, MousePos.Y);
  9451. // no context menu for "New session tab"
  9452. if ((Index >= 0) && (GetSessionTabTerminal(SessionsPageControl->Pages[Index]) != NULL))
  9453. {
  9454. SessionsPageControl->ActivePageIndex = Index;
  9455. if (DebugAlwaysTrue(SessionTabSwitched()))
  9456. {
  9457. // copied from TControl.WMContextMenu
  9458. SendCancelMode(SessionsPageControl);
  9459. // explicit popup instead of using PopupMenu property
  9460. // to avoid menu to popup somewhere within SessionTabSwitched above,
  9461. // while connecting yet not-connected session and hence
  9462. // allowing an access to commands over not-completelly connected session
  9463. TPoint Point = SessionsPageControl->ClientToScreen(MousePos);
  9464. TPopupMenu * PopupMenu = NonVisualDataModule->SessionsPopup;
  9465. PopupMenu->PopupComponent = SessionsPageControl;
  9466. PopupMenu->Popup(Point.x, Point.y);
  9467. }
  9468. }
  9469. Handled = true;
  9470. }
  9471. //---------------------------------------------------------------------------
  9472. bool __fastcall TCustomScpExplorerForm::CanChangePassword()
  9473. {
  9474. return (Terminal != NULL) && Terminal->Active && Terminal->IsCapable[fcChangePassword];
  9475. }
  9476. //---------------------------------------------------------------------------
  9477. void __fastcall TCustomScpExplorerForm::ChangePassword()
  9478. {
  9479. Configuration->Usage->Inc(L"PasswordChanges");
  9480. std::unique_ptr<TSessionData> ChangePasswordData(Terminal->SessionData->Clone());
  9481. ChangePasswordData->DisableAuthentationsExceptPassword();
  9482. ChangePasswordData->ChangePassword = true;
  9483. std::unique_ptr<TTerminal> ChangePasswordSession(
  9484. Terminal->CreateSecondarySession(L"ChangePassword", ChangePasswordData.get()));
  9485. ChangePasswordSession->AutoReadDirectory = false;
  9486. if (TTerminalManager::Instance()->ConnectTerminal(ChangePasswordSession.get()))
  9487. {
  9488. MessageDialog(MainInstructions(LoadStr(PASSWORD_CHANGED)), qtInformation, qaOK, HELP_CHANGE_PASSWORD);
  9489. }
  9490. else
  9491. {
  9492. Configuration->Usage->Inc(L"PasswordChangesFailed");
  9493. }
  9494. }
  9495. //---------------------------------------------------------------------------
  9496. bool __fastcall TCustomScpExplorerForm::CanPrivateKeyUpload()
  9497. {
  9498. // No nice way to assert SSH2
  9499. return (Terminal != NULL) && Terminal->Active && (Terminal->FSProtocol == cfsSFTP);
  9500. }
  9501. //---------------------------------------------------------------------------
  9502. void __fastcall TCustomScpExplorerForm::PrivateKeyUpload()
  9503. {
  9504. TOperationVisualizer OperationVisualizer;
  9505. UnicodeString FileName = Terminal->SessionData->PublicKeyFile;
  9506. TTerminalManager::Instance()->UploadPublicKey(Terminal, NULL, FileName);
  9507. }
  9508. //---------------------------------------------------------------------------
  9509. void __fastcall TCustomScpExplorerForm::ChangeScale(int M, int D)
  9510. {
  9511. TForm::ChangeScale(M, D);
  9512. int APixelsPerInch = GetControlPixelsPerInch(this);
  9513. HANDLE ResourceModule = GUIConfiguration->ChangeToDefaultResourceModule();
  9514. try
  9515. {
  9516. GlyphsModule->SetPixelsPerInch(APixelsPerInch);
  9517. }
  9518. __finally
  9519. {
  9520. GUIConfiguration->ChangeResourceModule(ResourceModule);
  9521. }
  9522. }
  9523. //---------------------------------------------------------------------------
  9524. void __fastcall TCustomScpExplorerForm::DockContextPopup(TObject * Sender, TPoint & MousePos, bool & /*Handled*/)
  9525. {
  9526. NonVisualDataModule->ControlContextPopup(Sender, MousePos);
  9527. }
  9528. //---------------------------------------------------------------------
  9529. class TPublicControl : public TControl
  9530. {
  9531. friend class TCustomScpExplorerForm;
  9532. };
  9533. //---------------------------------------------------------------------------
  9534. void __fastcall TCustomScpExplorerForm::CopyPopup(TControl * DestControl, TControl * SourceControl)
  9535. {
  9536. static_cast<TPublicControl *>(DestControl)->PopupMenu = static_cast<TPublicControl *>(SourceControl)->PopupMenu;
  9537. static_cast<TPublicControl *>(DestControl)->OnContextPopup = static_cast<TPublicControl *>(SourceControl)->OnContextPopup;
  9538. }
  9539. //---------------------------------------------------------------------------
  9540. void __fastcall TCustomScpExplorerForm::DoBookmarkClick(TOperationSide Side, TObject * Sender)
  9541. {
  9542. TBookmark * Bookmark = DebugNotNull(reinterpret_cast<TBookmark *>(dynamic_cast<TTBCustomItem *>(Sender)->Tag));
  9543. OpenBookmark(Side, Bookmark);
  9544. }
  9545. //---------------------------------------------------------------------------
  9546. void __fastcall TCustomScpExplorerForm::LocalBookmarkClick(TObject * Sender)
  9547. {
  9548. DoBookmarkClick(osLocal, Sender);
  9549. }
  9550. //---------------------------------------------------------------------------
  9551. void __fastcall TCustomScpExplorerForm::RemoteBookmarkClick(TObject * Sender)
  9552. {
  9553. DoBookmarkClick(osRemote, Sender);
  9554. }
  9555. //---------------------------------------------------------------------------
  9556. void __fastcall TCustomScpExplorerForm::CreateOpenDirMenuList(
  9557. TTBCustomItem * Menu, TOperationSide Side, TBookmarkList * BookmarkList)
  9558. {
  9559. // TODO
  9560. if (BookmarkList != NULL)
  9561. {
  9562. TNotifyEvent OnBookmarkClick = (Side == osLocal) ? &LocalBookmarkClick : &RemoteBookmarkClick;
  9563. if (!WinConfiguration->UseLocationProfiles)
  9564. {
  9565. std::unique_ptr<TStringList> Directories(new TStringList());
  9566. Directories->CaseSensitive = (Side == osRemote);
  9567. for (int Index = 0; Index < BookmarkList->Count; Index++)
  9568. {
  9569. TBookmark * Bookmark = BookmarkList->Bookmarks[Index];
  9570. UnicodeString Directory = Bookmark->GetSideDirectory(Side);
  9571. if (!Directory.IsEmpty() && (Directories->IndexOf(Directory) < 0))
  9572. {
  9573. std::unique_ptr<TTBCustomItem> Item(new TTBXItem(Owner));
  9574. Item->Caption = EscapeHotkey(Directory);
  9575. Item->ShortCut = Bookmark->ShortCut;
  9576. Item->OnClick = OnBookmarkClick;
  9577. Item->Tag = reinterpret_cast<int>(Bookmark);
  9578. Directories->Add(Directory);
  9579. Menu->Add(Item.release());
  9580. }
  9581. }
  9582. }
  9583. else
  9584. {
  9585. std::unique_ptr<TStrings> Folders(CreateSortedStringList());
  9586. for (int Index = 0; Index < BookmarkList->Count; Index++)
  9587. {
  9588. TBookmark * Bookmark = BookmarkList->Bookmarks[Index];
  9589. if (!Bookmark->Node.IsEmpty())
  9590. {
  9591. Folders->Add(Bookmark->Node);
  9592. }
  9593. }
  9594. for (int Index = 0; Index < Folders->Count; Index++)
  9595. {
  9596. std::unique_ptr<TTBCustomItem> Item(new TTBXSubmenuItem(Owner));
  9597. Item->Caption = Folders->Strings[Index];
  9598. Item->ImageIndex = NonVisualDataModule->RemoteChangePathAction->ImageIndex;
  9599. Folders->Objects[Index] = Item.get();
  9600. Menu->Add(Item.release());
  9601. }
  9602. for (int Index = 0; Index < BookmarkList->Count; Index++)
  9603. {
  9604. TBookmark * Bookmark = BookmarkList->Bookmarks[Index];
  9605. TTBCustomItem * Parent;
  9606. if (!Bookmark->Node.IsEmpty())
  9607. {
  9608. DebugAssert(Folders->IndexOf(Bookmark->Node) >= 0);
  9609. Parent = dynamic_cast<TTBCustomItem *>(Folders->Objects[Folders->IndexOf(Bookmark->Node)]);
  9610. }
  9611. else
  9612. {
  9613. Parent = Menu;
  9614. }
  9615. std::unique_ptr<TTBCustomItem> Item(new TTBXItem(Owner));
  9616. Item->Caption = Bookmark->Name;
  9617. Item->ShortCut = Bookmark->ShortCut;
  9618. Item->OnClick = OnBookmarkClick;
  9619. Item->Tag = reinterpret_cast<int>(Bookmark);
  9620. if (((Bookmark->Name == Bookmark->Local) || (Bookmark->Name == Bookmark->Remote)) &&
  9621. (Bookmark->Local.IsEmpty() || Bookmark->Remote.IsEmpty()))
  9622. {
  9623. // No hint for location profiles that are actually mere "bookmarks"
  9624. }
  9625. else
  9626. {
  9627. UnicodeString Hint = FORMAT(LoadStrPart(LOCATION_PROFILE_HINT, 1), (Bookmark->Name));
  9628. UnicodeString LongHint;
  9629. if (!Bookmark->Local.IsEmpty())
  9630. {
  9631. AddToList(LongHint, FORMAT(LoadStrPart(LOCATION_PROFILE_HINT, 2), (Bookmark->Local)), L"\n");
  9632. }
  9633. if (!Bookmark->Remote.IsEmpty())
  9634. {
  9635. AddToList(LongHint, FORMAT(LoadStrPart(LOCATION_PROFILE_HINT, 3), (Bookmark->Remote)), L"\n");
  9636. }
  9637. AddToList(Hint, LongHint, L"|");
  9638. Item->Hint = Hint;
  9639. }
  9640. Parent->Add(Item.release());
  9641. }
  9642. }
  9643. AddMenuSeparator(Menu);
  9644. }
  9645. }
  9646. //---------------------------------------------------------------------------
  9647. void __fastcall TCustomScpExplorerForm::CreateOpenDirMenu(TTBCustomItem * Menu, TOperationSide Side)
  9648. {
  9649. Menu->Clear();
  9650. std::unique_ptr<TTBCustomItem> Item;
  9651. Item.reset(new TTBXItem(Owner));
  9652. Item->Action = (Side == osLocal) ? NonVisualDataModule->LocalOpenDirAction : NonVisualDataModule->RemoteOpenDirAction;
  9653. Item->Options = Item->Options << tboDefault;
  9654. Menu->Add(Item.release());
  9655. AddMenuSeparator(Menu);
  9656. if (Terminal != NULL)
  9657. {
  9658. CreateOpenDirMenuList(Menu, Side, WinConfiguration->Bookmarks[Terminal->SessionData->SessionKey]);
  9659. }
  9660. CreateOpenDirMenuList(Menu, Side, WinConfiguration->SharedBookmarks);
  9661. Item.reset(new TTBXItem(Owner));
  9662. Item->Action = (Side == osLocal) ? NonVisualDataModule->LocalAddBookmarkAction : NonVisualDataModule->RemoteAddBookmarkAction;
  9663. Menu->Add(Item.release());
  9664. }
  9665. //---------------------------------------------------------------------------
  9666. bool __fastcall TCustomScpExplorerForm::TryOpenDirectory(TOperationSide Side, const UnicodeString & Path)
  9667. {
  9668. bool Result = true;
  9669. try
  9670. {
  9671. if (Side == osRemote)
  9672. {
  9673. Terminal->ExceptionOnFail = true;
  9674. }
  9675. try
  9676. {
  9677. DirView(Side)->Path = Path;
  9678. }
  9679. __finally
  9680. {
  9681. if (Side == osRemote)
  9682. {
  9683. Terminal->ExceptionOnFail = false;
  9684. }
  9685. }
  9686. }
  9687. catch (Exception & E)
  9688. {
  9689. if ((Side != osRemote) || Terminal->Active)
  9690. {
  9691. ShowExtendedExceptionEx(Terminal, &E);
  9692. Result = false;
  9693. }
  9694. else
  9695. {
  9696. throw;
  9697. }
  9698. }
  9699. return Result;
  9700. }
  9701. //---------------------------------------------------------------------------
  9702. void __fastcall TCustomScpExplorerForm::ReloadDirectory(TOperationSide Side)
  9703. {
  9704. // Make sure there some feedback even when loading tiny local folders
  9705. TInstantOperationVisualizer Visualizer;
  9706. DirView(Side)->ReloadDirectory();
  9707. }
  9708. //---------------------------------------------------------------------------
  9709. void __fastcall TCustomScpExplorerForm::CloseSessionTab(int Index)
  9710. {
  9711. if (Index == SessionsPageControl->TabIndex)
  9712. {
  9713. CloseSession();
  9714. }
  9715. else
  9716. {
  9717. TTabSheet * TabSheet = SessionsPageControl->Pages[Index];
  9718. TTerminal * Terminal = GetSessionTabTerminal(TabSheet);
  9719. TTerminalManager * Manager = TTerminalManager::Instance();
  9720. if (!Terminal->Active)
  9721. {
  9722. Manager->FreeTerminal(Terminal);
  9723. }
  9724. else
  9725. {
  9726. TTerminalQueue * Queue = Manager->FindQueueForTerminal(Terminal);
  9727. if (CanCloseQueue(Queue))
  9728. {
  9729. Manager->FreeTerminal(Terminal);
  9730. }
  9731. }
  9732. }
  9733. }
  9734. //---------------------------------------------------------------------------
  9735. void __fastcall TCustomScpExplorerForm::SessionsPageControlCloseButtonClick(TPageControl * /*Sender*/, int Index)
  9736. {
  9737. CloseSessionTab(Index);
  9738. }
  9739. //---------------------------------------------------------------------------
  9740. void __fastcall TCustomScpExplorerForm::CopyFilesToClipboard(TOperationSide Side, bool OnFocused)
  9741. {
  9742. if (DebugAlwaysTrue(!IsSideLocalBrowser(Side)))
  9743. {
  9744. TInstantOperationVisualizer Visualizer;
  9745. // To trigger ClipboardDataObjectRelease (in case the clipboard already contains our data)
  9746. // before we create a new data object
  9747. ClipboardClear();
  9748. DebugAssert(FClipboardFakeDirectory.IsEmpty());
  9749. DebugAssert(FClipboardFakeMonitors.get() == NULL);
  9750. FClipboardFakeDirectory = CreateFakeTransferDirectory();
  9751. // Intermediate variable to WORKAROUND a crash in compiler
  9752. TObjectList * ClipboardFakeMonitors =
  9753. StartCreationDirectoryMonitorsOnEachDrive(FILE_NOTIFY_CHANGE_DIR_NAME, ClipboardFakeCreated);
  9754. FClipboardFakeMonitors.reset(ClipboardFakeMonitors);
  9755. if (FClipboardDragDropFilesEx.get() == NULL)
  9756. {
  9757. FClipboardDragDropFilesEx.reset(new TDragDropFilesEx(this));
  9758. FClipboardDragDropFilesEx->OnDataObjectRelease = ClipboardDataObjectRelease;
  9759. }
  9760. FClipboardDragDropFilesEx->FileList->Clear();
  9761. FClipboardDragDropFilesEx->FileList->AddItem(NULL, FClipboardFakeDirectory);
  9762. FClipboardDragDropFilesEx->CopyToClipboard();
  9763. FClipboardTerminal = FTerminal;
  9764. // Need full paths, as cwd can be different once files are pasted
  9765. FClipboardFileList.reset(TRemoteFileList::CloneStrings(RemoteDirView->CreateFileList(OnFocused, true)));
  9766. for (int Index = 0; Index < FClipboardFileList->Count; Index++)
  9767. {
  9768. FClipboardFileList->Strings[Index] = UnixExcludeTrailingBackslash(FClipboardFileList->Strings[Index]);
  9769. }
  9770. }
  9771. }
  9772. //---------------------------------------------------------------------------
  9773. void __fastcall TCustomScpExplorerForm::ClipboardClear()
  9774. {
  9775. if (OpenClipboard(0))
  9776. {
  9777. EmptyClipboard(); // Calls ClipboardDataObjectRelease, if clipboard contains our data
  9778. CloseClipboard();
  9779. }
  9780. }
  9781. //---------------------------------------------------------------------------
  9782. bool __fastcall TCustomScpExplorerForm::DoesClipboardContainOurFiles()
  9783. {
  9784. return !FClipboardFakeDirectory.IsEmpty();
  9785. }
  9786. //---------------------------------------------------------------------------
  9787. void __fastcall TCustomScpExplorerForm::ClipboardStop()
  9788. {
  9789. RemoveDir(ApiPath(FClipboardFakeDirectory));
  9790. FClipboardFakeDirectory = UnicodeString();
  9791. FClipboardTerminal = NULL;
  9792. // Also called in Idle()
  9793. FClipboardFakeMonitors.reset(NULL);
  9794. }
  9795. //---------------------------------------------------------------------------
  9796. void __fastcall TCustomScpExplorerForm::ClipboardDataObjectRelease(TObject * /*Sender*/)
  9797. {
  9798. if (DebugAlwaysTrue(!FClipboardFakeDirectory.IsEmpty()))
  9799. {
  9800. ClipboardStop();
  9801. }
  9802. }
  9803. //---------------------------------------------------------------------------
  9804. void __fastcall TCustomScpExplorerForm::ClipboardDownload(const UnicodeString & TargetDirectory, bool NoConfirmation, bool DragDrop)
  9805. {
  9806. TTerminalManager * Manager = TTerminalManager::Instance();
  9807. Manager->ActiveTerminal = FClipboardTerminal;
  9808. TTransferOperationParam Params;
  9809. Params.TargetDirectory = TargetDirectory;
  9810. Params.DragDrop = DragDrop;
  9811. ExecuteFileOperation(foCopy, osRemote, FClipboardFileList.get(), NoConfirmation, &Params);
  9812. }
  9813. //---------------------------------------------------------------------------
  9814. void __fastcall TCustomScpExplorerForm::ClipboardFakeCreated(TObject * /*Sender*/, const UnicodeString FileName)
  9815. {
  9816. // It can actually rarelly happen that some random file is created, while we are shuttting down the monitor
  9817. // (as it pumps a Windows message queue while being shutted down)
  9818. if (DebugAlwaysTrue(!FClipboardFakeDirectory.IsEmpty()) &&
  9819. SameText(ExtractFileName(FileName), ExtractFileName(FClipboardFakeDirectory)))
  9820. {
  9821. // Can fail as it can be e.g. locked by AV, so we retry that later
  9822. bool Removed = RemoveDir(ApiPath(FileName));
  9823. try
  9824. {
  9825. if (!NonVisualDataModule->Busy)
  9826. {
  9827. bool NoConfirmation = (WinConfiguration->DDTransferConfirmation == asOff);
  9828. ClipboardDownload(ExtractFilePath(FileName), NoConfirmation, true);
  9829. }
  9830. }
  9831. __finally
  9832. {
  9833. if (!Removed)
  9834. {
  9835. RemoveDir(ApiPath(FileName));
  9836. }
  9837. }
  9838. }
  9839. }
  9840. //---------------------------------------------------------------------------
  9841. void __fastcall TCustomScpExplorerForm::DirViewGetItemColor(
  9842. TObject * Sender, UnicodeString FileName, bool Directory, __int64 Size, TDateTime Modification, TColor & Color)
  9843. {
  9844. TFileMasks::TParams MaskParams;
  9845. MaskParams.Size = Size;
  9846. MaskParams.Modification = Modification;
  9847. TCustomDirView * DirView = DebugNotNull(dynamic_cast<TCustomDirView *>(Sender));
  9848. for (TFileColorData::TList::const_iterator Iter = FFileColors.begin(); Iter != FFileColors.end(); Iter++)
  9849. {
  9850. bool ImplicitMatch;
  9851. if (Iter->FileMask.Matches(FileName, Directory, DirView->PathName, &MaskParams, false, ImplicitMatch) &&
  9852. !ImplicitMatch)
  9853. {
  9854. Color = Iter->Color;
  9855. break;
  9856. }
  9857. }
  9858. }
  9859. //---------------------------------------------------------------------------
  9860. void __fastcall TCustomScpExplorerForm::DirViewKeyDown(TObject * Sender, WORD & Key, TShiftState)
  9861. {
  9862. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  9863. if (!DirView->IsEditing())
  9864. {
  9865. // Handled here, instead of RemoteDirViewKeyPress (as on Login dialog),
  9866. // as VK_BACK is handled by TCustomDirView.KeyDown and we can swallow it here,
  9867. // while searching
  9868. if (Key == VK_BACK)
  9869. {
  9870. if (!FIncrementalSearch.IsEmpty())
  9871. {
  9872. if (FIncrementalSearch.Length() == 1)
  9873. {
  9874. ResetIncrementalSearch();
  9875. }
  9876. else
  9877. {
  9878. UnicodeString NewText = FIncrementalSearch.SubString(1, FIncrementalSearch.Length() - 1);
  9879. IncrementalSearch(NewText, false, false);
  9880. }
  9881. Key = 0;
  9882. }
  9883. }
  9884. }
  9885. }
  9886. //---------------------------------------------------------------------------
  9887. void __fastcall TCustomScpExplorerForm::DirViewKeyPress(TObject * Sender, wchar_t & Key)
  9888. {
  9889. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  9890. if (!DirView->IsEditing() && (WinConfiguration->PanelSearch != isOff))
  9891. {
  9892. // Filter control sequences.
  9893. // When not searching yet, prefer use of the space for toggling file selection
  9894. // (so we cannot incrementally search for a string starting with a space).
  9895. if ((Key > VK_SPACE) ||
  9896. ((Key == VK_SPACE) && (GetKeyState(VK_CONTROL) >= 0) && !FIncrementalSearch.IsEmpty()))
  9897. {
  9898. IncrementalSearch(FIncrementalSearch + Key, false, false);
  9899. Key = 0;
  9900. }
  9901. }
  9902. }
  9903. //---------------------------------------------------------------------------
  9904. void __fastcall TCustomScpExplorerForm::ResetIncrementalSearch()
  9905. {
  9906. if (!FIncrementalSearch.IsEmpty())
  9907. {
  9908. FIncrementalSearch = L"";
  9909. DirView(osCurrent)->UpdateStatusBar();
  9910. }
  9911. }
  9912. //---------------------------------------------------------------------------
  9913. void __fastcall TCustomScpExplorerForm::IncrementalSearch(const UnicodeString & Text, bool SkipCurrent, bool Reverse)
  9914. {
  9915. TListItem * Item = SearchFile(Text, SkipCurrent, Reverse);
  9916. if (Item == NULL)
  9917. {
  9918. MessageBeep(MB_ICONHAND);
  9919. }
  9920. else
  9921. {
  9922. TCustomDirView * ADirView = DirView(osCurrent);
  9923. {
  9924. TAutoNestingCounter Guard(FIncrementalSearching);
  9925. ADirView->FocusItem(Item);
  9926. if (ADirView->NortonLike == nlOff)
  9927. {
  9928. if ((ADirView->Selected != Item) ||
  9929. (ADirView->SelCount > 1))
  9930. {
  9931. ADirView->ClearSelection();
  9932. ADirView->Selected = Item;
  9933. }
  9934. }
  9935. }
  9936. FIncrementalSearch = Text;
  9937. Item->MakeVisible(false);
  9938. TListItem * NextItem = SearchFile(Text, true, Reverse);
  9939. FIncrementalSearchHaveNext = (NextItem != NULL) && (NextItem != Item);
  9940. ADirView->UpdateStatusBar();
  9941. }
  9942. }
  9943. //---------------------------------------------------------------------------
  9944. TListItem * __fastcall TCustomScpExplorerForm::GetNextFile(TListItem * Item, bool Reverse)
  9945. {
  9946. int Index = Item->Index;
  9947. if (!Reverse)
  9948. {
  9949. Index++;
  9950. if (Index >= Item->Owner->Count)
  9951. {
  9952. Index = 0;
  9953. }
  9954. }
  9955. else
  9956. {
  9957. Index--;
  9958. if (Index < 0)
  9959. {
  9960. Index = Item->Owner->Count - 1;
  9961. }
  9962. }
  9963. return Item->Owner->Item[Index];
  9964. }
  9965. //---------------------------------------------------------------------------
  9966. TListItem * __fastcall TCustomScpExplorerForm::SearchFile(const UnicodeString & Text, bool SkipCurrent, bool Reverse)
  9967. {
  9968. TCustomDirView * ADirView = DirView(osCurrent);
  9969. if (ADirView->Items->Count == 0)
  9970. {
  9971. return NULL;
  9972. }
  9973. else
  9974. {
  9975. TListItem * CurrentItem = (ADirView->ItemFocused != NULL) ? ADirView->ItemFocused : ADirView->Items->Item[0];
  9976. TListItem * Item = CurrentItem;
  9977. if (SkipCurrent)
  9978. {
  9979. Item = DebugNotNull(GetNextFile(Item, Reverse));
  9980. }
  9981. while (true)
  9982. {
  9983. bool Matches = false;
  9984. switch (WinConfiguration->PanelSearch)
  9985. {
  9986. case isNameStartOnly:
  9987. Matches = ContainsTextSemiCaseSensitive(Item->Caption.SubString(1, Text.Length()), Text);
  9988. break;
  9989. case isName:
  9990. Matches = ContainsTextSemiCaseSensitive(Item->Caption, Text);
  9991. break;
  9992. case isAll:
  9993. int ColCount = (ADirView->ViewStyle == vsReport) ? ADirView->ColProperties->Count : 1;
  9994. int Index = 0;
  9995. while ((Index < ColCount) && !Matches)
  9996. {
  9997. bool Visible = (ADirView->ViewStyle == vsReport) ? ADirView->ColProperties->Visible[Index] : true;
  9998. if (Visible)
  9999. {
  10000. Matches = ContainsTextSemiCaseSensitive(ADirView->GetColumnText(Item, Index), Text);
  10001. }
  10002. Index++;
  10003. }
  10004. break;
  10005. }
  10006. if (Matches)
  10007. {
  10008. return Item;
  10009. }
  10010. Item = GetNextFile(Item, Reverse);
  10011. if (Item == CurrentItem)
  10012. {
  10013. return NULL;
  10014. }
  10015. }
  10016. }
  10017. }
  10018. //---------------------------------------------------------------------------
  10019. void __fastcall TCustomScpExplorerForm::DirViewExit(TObject *)
  10020. {
  10021. ResetIncrementalSearch();
  10022. }
  10023. //---------------------------------------------------------------------------
  10024. void __fastcall TCustomScpExplorerForm::CMDialogKey(TWMKeyDown & Message)
  10025. {
  10026. if (Message.CharCode == VK_TAB)
  10027. {
  10028. if (!FIncrementalSearch.IsEmpty())
  10029. {
  10030. TShiftState Shift = KeyDataToShiftState(Message.KeyData);
  10031. bool Reverse = Shift.Contains(ssShift);
  10032. IncrementalSearch(FIncrementalSearch, true, Reverse);
  10033. Message.Result = 1;
  10034. return;
  10035. }
  10036. }
  10037. else if (Message.CharCode == VK_ESCAPE)
  10038. {
  10039. if (!FIncrementalSearch.IsEmpty())
  10040. {
  10041. ResetIncrementalSearch();
  10042. Message.Result = 1;
  10043. return;
  10044. }
  10045. }
  10046. TForm::Dispatch(&Message);
  10047. }
  10048. //---------------------------------------------------------------------------
  10049. void __fastcall TCustomScpExplorerForm::Deactivate()
  10050. {
  10051. TForm::Deactivate();
  10052. // E.g. when switching to an internal editor window
  10053. ResetIncrementalSearch();
  10054. }
  10055. //---------------------------------------------------------------------------
  10056. void __fastcall TCustomScpExplorerForm::ApplicationEventsDeactivate(TObject *)
  10057. {
  10058. // When switching to another application
  10059. ResetIncrementalSearch();
  10060. }
  10061. //---------------------------------------------------------------------------
  10062. void __fastcall TCustomScpExplorerForm::ApplicationEventsModalBegin(TObject *)
  10063. {
  10064. ResetIncrementalSearch();
  10065. }
  10066. //---------------------------------------------------------------------------
  10067. void __fastcall TCustomScpExplorerForm::DirViewChangeFocus(TObject *, TListItem *)
  10068. {
  10069. if (FIncrementalSearching <= 0)
  10070. {
  10071. ResetIncrementalSearch();
  10072. }
  10073. }
  10074. //---------------------------------------------------------------------------
  10075. void __fastcall TCustomScpExplorerForm::RemoteStatusBarMouseDown(TObject *, TMouseButton, TShiftState, int, int)
  10076. {
  10077. CountClicksForWindowPrint(this);
  10078. }
  10079. //---------------------------------------------------------------------------
  10080. void __fastcall TCustomScpExplorerForm::RemoteDirViewResize(TObject *)
  10081. {
  10082. CenterReconnectToolbar();
  10083. }
  10084. //---------------------------------------------------------------------------
  10085. void __fastcall TCustomScpExplorerForm::BrowseFile()
  10086. {
  10087. if (RemoteDirView->ItemFocused != NULL)
  10088. {
  10089. RemoteDirView->ItemFocused->Selected = true;
  10090. }
  10091. }
  10092. //---------------------------------------------------------------------------