CustomScpExplorer.cpp 374 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328
  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 = 3;
  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. TAutoBatch::TAutoBatch(TCustomScpExplorerForm * Form) :
  100. FForm(Form)
  101. {
  102. FForm->BatchStart(FBatchStorage);
  103. }
  104. //---------------------------------------------------------------------------
  105. TAutoBatch::~TAutoBatch()
  106. {
  107. FForm->BatchEnd(FBatchStorage);
  108. }
  109. //---------------------------------------------------------------------------
  110. struct TTransferOperationParam
  111. {
  112. TTransferOperationParam();
  113. UnicodeString TargetDirectory;
  114. bool Temp;
  115. bool DragDrop;
  116. int Options;
  117. TAutoSwitch Queue;
  118. };
  119. //---------------------------------------------------------------------------
  120. TTransferOperationParam::TTransferOperationParam()
  121. {
  122. Temp = false;
  123. DragDrop = false;
  124. Options = 0;
  125. Queue = asAuto;
  126. }
  127. //---------------------------------------------------------------------------
  128. class TTransferPresetNoteData : public TObject
  129. {
  130. public:
  131. UnicodeString Message;
  132. };
  133. //---------------------------------------------------------------------------
  134. class TTerminalNoteData : public TObject
  135. {
  136. public:
  137. TManagedTerminal * Terminal;
  138. };
  139. //---------------------------------------------------------------------------
  140. __fastcall TCustomScpExplorerForm::TCustomScpExplorerForm(TComponent* Owner):
  141. FFormRestored(false),
  142. TForm(Owner)
  143. {
  144. FInvalid = false;
  145. FCurrentSide = osRemote;
  146. FEverShown = false;
  147. FDocks = new TList();
  148. InitControls();
  149. DebugAssert(NonVisualDataModule && !NonVisualDataModule->ScpExplorer);
  150. // So that UpdateCustomCommandsToolbar called from RestoreParams works
  151. NonVisualDataModule->ScpExplorer = this;
  152. RestoreParams();
  153. ConfigurationChanged();
  154. RemoteDirView->Invalidate();
  155. FAutoOperation = false;
  156. FOnFileOperationFinished = NULL;
  157. FForceExecution = false;
  158. FIgnoreNextDialogChar = 0;
  159. FErrorList = NULL;
  160. FSynchronizeProgressForm = NULL;
  161. FProgressForm = NULL;
  162. FRefreshLocalDirectory = false;
  163. FRefreshRemoteDirectory = false;
  164. FDDExtMapFile = NULL;
  165. // CreateMutexW keeps failing with ERROR_NOACCESS
  166. FDDExtMutex = CreateMutexA(NULL, false, AnsiString(DRAG_EXT_MUTEX).c_str());
  167. DebugAssert(FDDExtMutex != NULL);
  168. FDDTargetControl = NULL;
  169. FDelayedDeletionTimer = NULL;
  170. FDelayedDeletionList = new TStringList();
  171. FDDFileList = NULL;
  172. FPendingTempSpaceWarn = false;
  173. FCapturedLog = NULL;
  174. FDragDropOperation = false;
  175. memset(&FHistoryMenu, 0, sizeof(FHistoryMenu));
  176. FAllowTransferPresetAutoSelect = true;
  177. FSessionChanging = false;
  178. FCopyParamDefault = L"";
  179. FSynchronizeController = NULL;
  180. FPendingQueueActionItem = NULL;
  181. FLockLevel = 0;
  182. FLockSuspendLevel = 0;
  183. FDisabledOnLockSuspend = false;
  184. FAlternativeDelete = false;
  185. FTrayIcon = new ::TTrayIcon(0);
  186. FTrayIcon->OnClick = TrayIconClick;
  187. FMaxQueueLength = 0;
  188. FLastContextPopupScreenPoint = TPoint(-1, -1);
  189. FTransferResumeList = NULL;
  190. FMoveToQueue = false;
  191. FStandaloneEditing = false;
  192. FOnFeedSynchronizeError = NULL;
  193. FNeedSession = false;
  194. FDoNotIdleCurrentTerminal = 0;
  195. FIncrementalSearching = 0;
  196. FIncrementalSearchHaveNext = false;
  197. FQueueFileList.reset(new TQueueFileList());
  198. FProgressSide = osCurrent;
  199. FEditorManager = new TEditorManager();
  200. FEditorManager->OnFileChange = ExecutedFileChanged;
  201. FEditorManager->OnFileReload = ExecutedFileReload;
  202. FEditorManager->OnFileEarlyClosed = ExecutedFileEarlyClosed;
  203. FEditorManager->OnFileUploadComplete = ExecutedFileUploadComplete;
  204. FLocalEditors = new TList();
  205. FQueueStatus = NULL;
  206. FQueueStatusSection = new TCriticalSection();
  207. FQueueStatusInvalidated = false;
  208. FQueueItemInvalidated = false;
  209. FQueueStatusUpdating = false;
  210. FQueueActedItem = NULL;
  211. FQueueController = new TQueueController(QueueView3);
  212. FUserActionTimer = new TTimer(this);
  213. FUserActionTimer->Enabled = false;
  214. FUserActionTimer->Interval = 10;
  215. FUserActionTimer->OnTimer = UserActionTimer;
  216. FNotes = new TStringList();
  217. FNoteTimer = new TTimer(this);
  218. FNoteTimer->Enabled = false;
  219. FNoteTimer->OnTimer = NoteTimer;
  220. FOnNoteClick = NULL;
  221. FOle32Library = LoadLibrary(L"Ole32.dll");
  222. FDragMoveCursor = FOle32Library != NULL ?
  223. LoadCursor(FOle32Library, MAKEINTRESOURCE(2)) : NULL;
  224. UseSystemSettings(this);
  225. TTBXStringList * TransferList = dynamic_cast<TTBXStringList*>(
  226. static_cast<TObject*>(GetComponent(fcTransferList)));
  227. DebugAssert(TransferList != NULL);
  228. FTransferListHoverIndex = -1;
  229. TransferList->OnChange = TransferListChange;
  230. TransferList->OnDrawItem = TransferListDrawItem;
  231. SetSubmenu(dynamic_cast<TTBXCustomItem *>(static_cast<TObject *>(GetComponent(fcColorMenu))), true);
  232. SetSubmenu(NonVisualDataModule->ColorMenuItem, true);
  233. UseDesktopFont(SessionsPageControl);
  234. UpdateSessionsPageControlHeight();
  235. UseDesktopFont(RemoteDirView);
  236. UseDesktopFont(RemoteDriveView);
  237. UseDesktopFont(QueueView3);
  238. UseDesktopFont(QueueFileList);
  239. UseDesktopFont(QueueLabel);
  240. UseDesktopFont(RemoteStatusBar);
  241. reinterpret_cast<TLabel*>(QueueSplitter)->OnDblClick = QueueSplitterDblClick;
  242. QueueSplitter->ShowHint = true;
  243. reinterpret_cast<TLabel*>(QueueFileListSplitter)->OnDblClick = QueueFileListSplitterDblClick;
  244. QueueFileListSplitter->ShowHint = true;
  245. RemotePanelSplitter->ShowHint = true;
  246. UpdateImages();
  247. FCustomCommandMenu = CreateTBXPopupMenu(this);
  248. FCustomCommandLocalFileList = NULL;
  249. FCustomCommandRemoteFileList = NULL;
  250. FSessionColors = new TPngImageList(this);
  251. FSessionColors->ColorDepth = cd32Bit;
  252. AddFixedSessionImages();
  253. SessionsPageControl->Images = FSessionColors;
  254. UpdateQueueLabel();
  255. CreateHiddenWindow();
  256. StartUpdates();
  257. }
  258. //---------------------------------------------------------------------------
  259. __fastcall TCustomScpExplorerForm::~TCustomScpExplorerForm()
  260. {
  261. FInvalid = true;
  262. // this has to be one of the very first things to do
  263. StopUpdateThread();
  264. // This is needed when shuting down Windows only. Otherwise it's already set NULL from Execute()
  265. TTerminalManager::Instance()->ScpExplorer = NULL;
  266. delete FCustomCommandLocalFileList;
  267. delete FCustomCommandRemoteFileList;
  268. delete FCustomCommandMenu;
  269. delete FTrayIcon;
  270. FTrayIcon = NULL;
  271. FEditorManager->CloseInternalEditors(ForceCloseInternalEditor);
  272. delete FEditorManager;
  273. ForceCloseLocalEditors();
  274. delete FLocalEditors;
  275. HideFileFindDialog();
  276. if (FDelayedDeletionTimer)
  277. {
  278. DoDelayedDeletion(NULL);
  279. SAFE_DESTROY(FDelayedDeletionTimer);
  280. }
  281. SAFE_DESTROY(FDelayedDeletionList);
  282. // sometimes we do not get DDEnd so the list is not released
  283. SAFE_DESTROY(FDDFileList);
  284. DebugAssert(FSynchronizeController == NULL);
  285. CloseHandle(FDDExtMutex);
  286. FDDExtMutex = NULL;
  287. FreeLibrary(FOle32Library);
  288. FOle32Library = NULL;
  289. FDragMoveCursor = NULL;
  290. DebugAssert(!FErrorList);
  291. if (FEverShown)
  292. {
  293. // when window is never shown (like when running command-line operation),
  294. // particularly window site is not restored correctly (BoundsRect value set
  295. // in RestoreForm gets lost during handle allocation), so we do not want
  296. // it to be stored
  297. StoreParams();
  298. }
  299. ManagedSession = NULL;
  300. Queue = NULL;
  301. DebugAssert(NonVisualDataModule && (NonVisualDataModule->ScpExplorer == this));
  302. NonVisualDataModule->ScpExplorer = NULL;
  303. delete FQueueController;
  304. FQueueController = NULL;
  305. delete FQueueStatusSection;
  306. FQueueStatusSection = NULL;
  307. delete FQueueStatus;
  308. FQueueStatus = NULL;
  309. delete FUserActionTimer;
  310. FUserActionTimer = NULL;
  311. delete FNoteTimer;
  312. delete FNotes;
  313. delete FNoteData;
  314. SAFE_DESTROY(FDocks);
  315. SessionsPageControl->Images = NULL;
  316. // only after clearing Terminal (after SessionListChanged => ... => AddSessionColor is called)
  317. SAFE_DESTROY(FSessionColors);
  318. SAFE_DESTROY(FSessionsDragDropFilesEx);
  319. SAFE_DESTROY(FHistoryMenu[0][0]);
  320. SAFE_DESTROY(FHistoryMenu[0][1]);
  321. SAFE_DESTROY(FHistoryMenu[1][0]);
  322. SAFE_DESTROY(FHistoryMenu[1][1]);
  323. if (FHiddenWindow != NULL)
  324. {
  325. DestroyWindow(FHiddenWindow);
  326. FHiddenWindow = NULL;
  327. }
  328. }
  329. //---------------------------------------------------------------------------
  330. void __fastcall TCustomScpExplorerForm::RefreshPanel(const UnicodeString & Session, const UnicodeString & Path)
  331. {
  332. std::unique_ptr<TSessionData> Data;
  333. if (!Session.IsEmpty())
  334. {
  335. bool DefaultsOnly;
  336. Data.reset(StoredSessions->ParseUrl(Session, NULL, DefaultsOnly));
  337. }
  338. TTerminalManager * Manager = TTerminalManager::Instance();
  339. for (int Index = 0; Index < Manager->Count; Index++)
  340. {
  341. TTerminal * Terminal = Manager->Sessions[Index];
  342. if (Session.IsEmpty() ||
  343. Manager->IsActiveTerminalForSite(Terminal, Data.get()))
  344. {
  345. if (Path.IsEmpty())
  346. {
  347. Terminal->ClearCaches();
  348. }
  349. else
  350. {
  351. Terminal->DirectoryModified(Path, true);
  352. }
  353. }
  354. }
  355. // We should flag a pending refresh for the background terminals or busy foreground terminals
  356. if (HasActiveTerminal() &&
  357. CanCommandLineFromAnotherInstance() &&
  358. (Session.IsEmpty() || Manager->IsActiveTerminalForSite(Terminal, Data.get())) &&
  359. (Path.IsEmpty() || UnixIsChildPath(Path, Terminal->CurrentDirectory)))
  360. {
  361. Terminal->ReloadDirectory();
  362. }
  363. }
  364. //---------------------------------------------------------------------------
  365. void __fastcall TCustomScpExplorerForm::WMCopyData(TMessage & Message)
  366. {
  367. PCOPYDATASTRUCT CopyData = reinterpret_cast<PCOPYDATASTRUCT>(Message.LParam);
  368. size_t MessageSize = sizeof(TCopyDataMessage);
  369. bool Result = DebugAlwaysTrue(CopyData->cbData == MessageSize);
  370. if (Result)
  371. {
  372. const TCopyDataMessage & Message = *reinterpret_cast<const TCopyDataMessage *>(CopyData->lpData);
  373. Result = (Message.Version == TCopyDataMessage::Version1);
  374. if (Result)
  375. {
  376. switch (Message.Command)
  377. {
  378. case TCopyDataMessage::CommandCanCommandLine:
  379. Result = CanCommandLineFromAnotherInstance();
  380. break;
  381. case TCopyDataMessage::CommandCommandLine:
  382. {
  383. UnicodeString CommandLine(Message.CommandLine);
  384. Result = CommandLineFromAnotherInstance(CommandLine);
  385. }
  386. break;
  387. case TCopyDataMessage::RefreshPanel:
  388. RefreshPanel(Message.Refresh.Session, Message.Refresh.Path);
  389. break;
  390. case TCopyDataMessage::MainWindowCheck:
  391. Result = true;
  392. break;
  393. default:
  394. Result = false;
  395. break;
  396. }
  397. }
  398. }
  399. Message.Result = Result ? 1 : 0;
  400. }
  401. //---------------------------------------------------------------------------
  402. void __fastcall TCustomScpExplorerForm::CreateHiddenWindow()
  403. {
  404. WNDCLASS WindowClass = {0};
  405. WindowClass.lpfnWndProc = DefWindowProc;
  406. WindowClass.hInstance = HInstance;
  407. WindowClass.lpszClassName = HIDDEN_WINDOW_NAME;
  408. FHiddenWindow = NULL;
  409. if (RegisterClass(&WindowClass))
  410. {
  411. FHiddenWindow = CreateWindow(HIDDEN_WINDOW_NAME, L"",
  412. WS_POPUP, 0, 0, 0, 0, 0, 0, HInstance, NULL);
  413. }
  414. }
  415. //---------------------------------------------------------------------------
  416. bool __fastcall TCustomScpExplorerForm::CanConsole()
  417. {
  418. return
  419. HasActiveTerminal() &&
  420. (Terminal->IsCapable[fcAnyCommand] || Terminal->IsCapable[fcSecondaryShell]);
  421. }
  422. //---------------------------------------------------------------------------
  423. bool __fastcall TCustomScpExplorerForm::CanCommandLineFromAnotherInstance()
  424. {
  425. bool Result = Visible && !NonVisualDataModule->Busy;
  426. return Result;
  427. }
  428. //---------------------------------------------------------------------------
  429. bool __fastcall TCustomScpExplorerForm::CommandLineFromAnotherInstance(
  430. const UnicodeString & CommandLine)
  431. {
  432. TProgramParams Params(CommandLine);
  433. bool Result = CanCommandLineFromAnotherInstance() && DebugAlwaysTrue(Params.ParamCount > 0);
  434. if (Result)
  435. {
  436. NonVisualDataModule->StartBusy();
  437. try
  438. {
  439. // this action is initiated from another process,
  440. // so it's likely that our window is not visible,
  441. // and user won't see what is going on
  442. Application->BringToFront();
  443. // reload sessions as we may be asked to open a session
  444. // just stored by another instance
  445. StoredSessions->Reload();
  446. UnicodeString SessionName = Params.Param[1];
  447. std::unique_ptr<TObjectList> DataList(new TObjectList());
  448. UnicodeString DownloadFile; // unused
  449. try
  450. {
  451. GetLoginData(SessionName, &Params, DataList.get(), DownloadFile, true, this);
  452. }
  453. catch (EAbort &)
  454. {
  455. // Happens when opening session in PuTTY and in other situations,
  456. // in which we do not want to fail WM_COPYDATA, as that would cause master instance
  457. // to process the command-line on its own (or to pass it to yet another instance)
  458. }
  459. if (DataList->Count > 0)
  460. {
  461. TTerminalManager * Manager = TTerminalManager::Instance();
  462. Manager->ActiveSession = Manager->NewSessions(DataList.get());
  463. }
  464. }
  465. __finally
  466. {
  467. NonVisualDataModule->EndBusy();
  468. }
  469. }
  470. return Result;
  471. }
  472. //---------------------------------------------------------------------------
  473. TManagedTerminal * TCustomScpExplorerForm::GetTerminal()
  474. {
  475. TManagedTerminal * Result;
  476. if ((FManagedSession != NULL) && !FManagedSession->LocalBrowser)
  477. {
  478. Result = FManagedSession;
  479. }
  480. else
  481. {
  482. Result = NULL;
  483. }
  484. return Result;
  485. }
  486. //---------------------------------------------------------------------------
  487. void __fastcall TCustomScpExplorerForm::SetManagedSession(TManagedTerminal * value)
  488. {
  489. if (FManagedSession != value)
  490. {
  491. SessionChanging();
  492. DoSetManagedSession(value, false);
  493. }
  494. }
  495. //---------------------------------------------------------------------------
  496. void __fastcall TCustomScpExplorerForm::DoSetManagedSession(TManagedTerminal * value, bool Replace)
  497. {
  498. DebugAssert(!Replace || ((value != NULL) && !value->LocalBrowser));
  499. FManagedSession = value;
  500. {
  501. TValueRestorer<bool> AllowTransferPresetAutoSelectRestorer(FAllowTransferPresetAutoSelect);
  502. FAllowTransferPresetAutoSelect = false;
  503. TAutoFlag SessionChangingFlag(FSessionChanging);
  504. SessionChanged(Replace);
  505. }
  506. if (Terminal != NULL)
  507. {
  508. TransferPresetAutoSelect();
  509. }
  510. // Update app and tab titles, prevented above by FSessionChanging.
  511. UpdateControls();
  512. }
  513. //---------------------------------------------------------------------------
  514. void __fastcall TCustomScpExplorerForm::ReplaceTerminal(TManagedTerminal * value)
  515. {
  516. DoSetManagedSession(value, true);
  517. }
  518. //---------------------------------------------------------------------------
  519. void __fastcall TCustomScpExplorerForm::SessionChanging()
  520. {
  521. if (ManagedSession != NULL)
  522. {
  523. UpdateSession(ManagedSession);
  524. }
  525. }
  526. //---------------------------------------------------------------------------
  527. void __fastcall TCustomScpExplorerForm::SessionChanged(bool Replaced)
  528. {
  529. if (Terminal != NULL)
  530. {
  531. UpdateSessionColor((TColor)Terminal->StateData->Color);
  532. }
  533. if (!TTerminalManager::Instance()->Updating)
  534. {
  535. SessionListChanged();
  536. }
  537. if (Terminal != NULL)
  538. {
  539. TObject * State = NULL;
  540. if (WinConfiguration->PreservePanelState)
  541. {
  542. State = Terminal->RemoteExplorerState;
  543. DirView(osRemote)->AnnounceState(State);
  544. }
  545. try
  546. {
  547. if (Replaced)
  548. {
  549. RemoteDirView->ReplaceTerminal(Terminal);
  550. }
  551. else
  552. {
  553. RemoteDirView->Terminal = Terminal;
  554. }
  555. if (Terminal->Active)
  556. {
  557. Terminal->RefreshDirectory();
  558. }
  559. if (State != NULL)
  560. {
  561. DirView(osRemote)->RestoreState(State);
  562. }
  563. }
  564. __finally
  565. {
  566. DirView(osRemote)->AnnounceState(NULL);
  567. }
  568. }
  569. else
  570. {
  571. RemoteDirView->Terminal = NULL;
  572. }
  573. InitStatusBar();
  574. UpdateTransferList();
  575. // Update panels Enable state before refreshing the labels
  576. UpdateControls();
  577. }
  578. //---------------------------------------------------------------------------
  579. void __fastcall TCustomScpExplorerForm::SetQueue(TTerminalQueue * value)
  580. {
  581. if (Queue != value)
  582. {
  583. if (FQueue != NULL)
  584. {
  585. FQueue->OnListUpdate = NULL;
  586. FQueue->OnQueueItemUpdate = NULL;
  587. }
  588. FQueue = value;
  589. if (FQueue != NULL)
  590. {
  591. DebugAssert(FQueue->OnListUpdate == NULL);
  592. FQueue->OnListUpdate = QueueListUpdate;
  593. DebugAssert(FQueue->OnQueueItemUpdate == NULL);
  594. FQueue->OnQueueItemUpdate = QueueItemUpdate;
  595. }
  596. QueueChanged();
  597. }
  598. }
  599. //---------------------------------------------------------------------------
  600. void __fastcall TCustomScpExplorerForm::QueueView3Deletion(TObject * /*Sender*/,
  601. TListItem * Item)
  602. {
  603. if (FQueueActedItem == Item)
  604. {
  605. FQueueActedItem = NULL;
  606. if ((QueueView3->PopupMenu != NULL) &&
  607. (QueueView3->PopupMenu->PopupComponent == QueueView3))
  608. {
  609. // rather "trick", suggested by Jordan on jrsoftware.toolbar2000
  610. ReleaseCapture();
  611. }
  612. }
  613. if (Item->Data == FPendingQueueActionItem)
  614. {
  615. FPendingQueueActionItem = NULL;
  616. }
  617. }
  618. //---------------------------------------------------------------------------
  619. bool TCustomScpExplorerForm::IsAnythingQueued()
  620. {
  621. return (FQueueStatus != NULL) && (FQueueStatus->ActiveAndPendingPrimaryCount > 0);
  622. }
  623. //---------------------------------------------------------------------------
  624. void __fastcall TCustomScpExplorerForm::UpdateQueueStatus(bool QueueChanging)
  625. {
  626. {
  627. TGuard Guard(FQueueStatusSection);
  628. FQueueStatusInvalidated = false;
  629. if (FQueue != NULL)
  630. {
  631. FQueueStatus = FQueue->CreateStatus(FQueueStatus);
  632. }
  633. }
  634. if ((FQueueStatus != NULL) && (FQueueStatus->Count > FMaxQueueLength))
  635. {
  636. FMaxQueueLength = FQueueStatus->Count;
  637. Configuration->Usage->SetMax(L"MaxQueueLength", FMaxQueueLength);
  638. }
  639. {
  640. TAutoFlag Flag(FQueueStatusUpdating);
  641. FQueueController->UpdateQueueStatus(FQueueStatus);
  642. }
  643. UpdateQueueFileList();
  644. SetQueueProgress();
  645. UpdateQueueView();
  646. bool IsEmpty = !IsAnythingQueued();
  647. if (IsEmpty && (Terminal != NULL))
  648. {
  649. TOnceDoneOperation OnceDoneOperation =
  650. NonVisualDataModule->CurrentQueueOnceEmptyOperation();
  651. NonVisualDataModule->ResetQueueOnceEmptyOperation();
  652. if ((FQueue != NULL) && !WinConfiguration->EnableQueueByDefault && !QueueChanging)
  653. {
  654. FQueue->Enabled = false;
  655. }
  656. if ((OnceDoneOperation != odoIdle) && !NonVisualDataModule->Busy)
  657. {
  658. Terminal->CloseOnCompletion(OnceDoneOperation, LoadStr(CLOSED_ON_QUEUE_EMPTY));
  659. }
  660. }
  661. }
  662. //---------------------------------------------------------------------------
  663. UnicodeString __fastcall TCustomScpExplorerForm::GetQueueProgressTitle()
  664. {
  665. UnicodeString Result;
  666. if (FQueueStatus != NULL)
  667. {
  668. if (FQueueStatus->IsOnlyOneActiveAndNoPending())
  669. {
  670. TFileOperationProgressType * ProgressData =
  671. FQueueStatus->Items[FQueueStatus->DoneCount]->ProgressData;
  672. if ((ProgressData != NULL) && ProgressData->InProgress)
  673. {
  674. Result = TProgressForm::ProgressStr(NULL, ProgressData);
  675. }
  676. }
  677. else if (FQueueStatus->ActiveAndPendingPrimaryCount > 1)
  678. {
  679. Result = FMTLOAD(PROGRESS_IN_QUEUE, (FQueueStatus->ActiveAndPendingPrimaryCount));
  680. }
  681. }
  682. return Result;
  683. }
  684. //---------------------------------------------------------------------------
  685. void __fastcall TCustomScpExplorerForm::UpdateQueueView()
  686. {
  687. ComponentVisible[fcQueueView] =
  688. !IsLocalBrowserMode() &&
  689. ((WinConfiguration->QueueView.Show == qvShow) ||
  690. ((WinConfiguration->QueueView.Show == qvHideWhenEmpty) &&
  691. (FQueueStatus != NULL) && (FQueueStatus->Count > 0)));
  692. }
  693. //---------------------------------------------------------------------------
  694. void __fastcall TCustomScpExplorerForm::QueueChanged()
  695. {
  696. if (FQueueStatus != NULL)
  697. {
  698. // UpdateFileList implementation relies on the status being recreated when session changes
  699. delete FQueueStatus;
  700. FQueueStatus = NULL;
  701. }
  702. UpdateQueueStatus(true);
  703. }
  704. //---------------------------------------------------------------------------
  705. void __fastcall TCustomScpExplorerForm::QueueListUpdate(TTerminalQueue * Queue)
  706. {
  707. if (FQueue == Queue)
  708. {
  709. FQueueStatusInvalidated = true;
  710. }
  711. }
  712. //---------------------------------------------------------------------------
  713. void __fastcall TCustomScpExplorerForm::QueueItemUpdate(TTerminalQueue * Queue,
  714. TQueueItem * Item)
  715. {
  716. if (FQueue == Queue)
  717. {
  718. TGuard Guard(FQueueStatusSection);
  719. // this may be running in parallel with QueueChanged
  720. if (FQueueStatus != NULL)
  721. {
  722. TQueueItemProxy * QueueItem = FQueueStatus->FindByQueueItem(Item);
  723. if ((Item->Status == TQueueItem::qsDone) && (Terminal != NULL))
  724. {
  725. FRefreshLocalDirectory = (QueueItem == NULL) ||
  726. (!QueueItem->Info->ModifiedLocal.IsEmpty());
  727. FRefreshRemoteDirectory = (QueueItem == NULL) ||
  728. (!QueueItem->Info->ModifiedRemote.IsEmpty());
  729. }
  730. if (QueueItem != NULL)
  731. {
  732. QueueItem->UserData = (void*)true;
  733. FQueueItemInvalidated = true;
  734. }
  735. }
  736. }
  737. }
  738. //---------------------------------------------------------------------------
  739. bool __fastcall TCustomScpExplorerForm::IsQueueAutoPopup()
  740. {
  741. // during standalone editing, we have no way to see/control queue,
  742. // so we have to always popup prompts automatically
  743. return FStandaloneEditing || GUIConfiguration->QueueAutoPopup;
  744. }
  745. //---------------------------------------------------------------------------
  746. void __fastcall TCustomScpExplorerForm::RefreshQueueItems()
  747. {
  748. if (FQueueStatus != NULL)
  749. {
  750. bool QueueAutoPopup = IsQueueAutoPopup();
  751. bool NeedRefresh = FQueueController->NeedRefresh();
  752. bool Refresh = FQueueItemInvalidated || NeedRefresh;
  753. FQueueItemInvalidated = false;
  754. int Limit = Refresh ? FQueueStatus->Count : FQueueStatus->DoneAndActiveCount;
  755. bool Updated = false;
  756. TQueueItemProxy * QueueItem;
  757. bool WasUserAction;
  758. for (int Index = 0; Index < Limit; Index++)
  759. {
  760. bool Update = false;
  761. QueueItem = FQueueStatus->Items[Index];
  762. WasUserAction = TQueueItem::IsUserActionStatus(QueueItem->Status);
  763. if (!NonVisualDataModule->Busy && QueueAutoPopup && WasUserAction &&
  764. (FPendingQueueActionItem == NULL))
  765. {
  766. FPendingQueueActionItem = QueueItem;
  767. FUserActionTimer->Enabled = true;
  768. }
  769. if ((bool)QueueItem->UserData)
  770. {
  771. QueueItem->UserData = (void*)false;
  772. QueueItem->Update();
  773. Updated = true;
  774. Update = true;
  775. }
  776. else if (FQueueController->QueueItemNeedsFrequentRefresh(QueueItem) || NeedRefresh)
  777. {
  778. Update = true;
  779. }
  780. if (Update)
  781. {
  782. FQueueController->RefreshQueueItem(QueueItem);
  783. }
  784. }
  785. if (Updated)
  786. {
  787. NonVisualDataModule->UpdateNonVisibleActions();
  788. SetQueueProgress();
  789. UpdateQueueFileList();
  790. }
  791. }
  792. }
  793. //---------------------------------------------------------------------------
  794. void __fastcall TCustomScpExplorerForm::SetTaskbarListProgressState(TBPFLAG Flags)
  795. {
  796. if (FTaskbarList != NULL)
  797. {
  798. // Could now use Application->MainFormHandle, now that we implement Application->OnGetMainFormHandle
  799. FTaskbarList->SetProgressState(GetMainForm()->Handle, Flags);
  800. }
  801. }
  802. //---------------------------------------------------------------------------
  803. void __fastcall TCustomScpExplorerForm::SetTaskbarListProgressValue(int Progress)
  804. {
  805. if (Progress >= 0)
  806. {
  807. if (FTaskbarList != NULL)
  808. {
  809. FTaskbarList->SetProgressValue(GetMainForm()->Handle, Progress, 100);
  810. }
  811. }
  812. else
  813. {
  814. SetTaskbarListProgressState(TBPF_INDETERMINATE);
  815. }
  816. }
  817. //---------------------------------------------------------------------------
  818. void __fastcall TCustomScpExplorerForm::SetTaskbarListProgressValue(TFileOperationProgressType * ProgressData)
  819. {
  820. int OverallProgress;
  821. // FProgressForm is null when this is called from SetQueueProgress
  822. if ((FProgressForm != NULL) && (FProgressForm->SynchronizeProgress != NULL))
  823. {
  824. OverallProgress = FProgressForm->SynchronizeProgress->Progress(ProgressData);
  825. }
  826. else if (!TFileOperationProgressType::IsIndeterminateOperation(ProgressData->Operation))
  827. {
  828. OverallProgress = ProgressData->OverallProgress();
  829. }
  830. else
  831. {
  832. OverallProgress = -1;
  833. }
  834. SetTaskbarListProgressValue(OverallProgress);
  835. }
  836. //---------------------------------------------------------------------------
  837. void __fastcall TCustomScpExplorerForm::SetQueueProgress()
  838. {
  839. TTerminalManager::Instance()->QueueStatusUpdated();
  840. if ((FTaskbarList != NULL) && (FProgressForm == NULL))
  841. {
  842. if ((FQueueStatus != NULL) && (FQueueStatus->ActiveAndPendingPrimaryCount > 0))
  843. {
  844. if (FQueueStatus->IsOnlyOneActiveAndNoPending())
  845. {
  846. TFileOperationProgressType * ProgressData = NULL;
  847. if (FQueueStatus->Items[FQueueStatus->DoneCount] != NULL)
  848. {
  849. ProgressData = FQueueStatus->Items[FQueueStatus->DoneCount]->ProgressData;
  850. }
  851. if ((ProgressData != NULL) &&
  852. ProgressData->InProgress)
  853. {
  854. SetTaskbarListProgressValue(ProgressData);
  855. }
  856. else
  857. {
  858. SetTaskbarListProgressState(TBPF_NOPROGRESS);
  859. }
  860. }
  861. else
  862. {
  863. SetTaskbarListProgressState(TBPF_INDETERMINATE);
  864. }
  865. }
  866. else
  867. {
  868. SetTaskbarListProgressState(TBPF_NOPROGRESS);
  869. }
  870. }
  871. UpdateQueueLabel();
  872. }
  873. //---------------------------------------------------------------------------
  874. void __fastcall TCustomScpExplorerForm::UpdateQueueLabel()
  875. {
  876. UnicodeString Caption = LoadStr(QUEUE_CAPTION);
  877. if (FQueueStatus != NULL)
  878. {
  879. int ActiveAndPendingPrimaryCount = FQueueStatus->ActiveAndPendingPrimaryCount;
  880. if (ActiveAndPendingPrimaryCount > 0)
  881. {
  882. Caption = FORMAT("%s (%d)", (Caption, ActiveAndPendingPrimaryCount));
  883. }
  884. }
  885. QueueLabel->Caption = Caption;
  886. }
  887. //---------------------------------------------------------------------------
  888. void __fastcall TCustomScpExplorerForm::UpdateTransferList()
  889. {
  890. TTBXStringList * TransferList = dynamic_cast<TTBXStringList*>(
  891. static_cast<TComponent*>(GetComponent(fcTransferList)));
  892. TTBXDropDownItem * TransferDropDown = dynamic_cast<TTBXDropDownItem*>(
  893. static_cast<TComponent*>(GetComponent(fcTransferDropDown)));
  894. TransferList->Strings->BeginUpdate();
  895. try
  896. {
  897. TransferList->Strings->Assign(GUIConfiguration->CopyParamList->NameList);
  898. TransferList->Strings->Insert(0, StripHotkey(LoadStr(COPY_PARAM_DEFAULT)));
  899. TransferList->ItemIndex = GUIConfiguration->CopyParamIndex + 1;
  900. if (FTransferDropDownHint.IsEmpty())
  901. {
  902. FTransferDropDownHint = TransferDropDown->Hint;
  903. }
  904. // this way we get name for "default" settings (COPY_PARAM_DEFAULT)
  905. UnicodeString Name = TransferList->Strings->Strings[TransferList->ItemIndex];
  906. TransferDropDown->Text = StripHotkey(Name);
  907. TransferDropDown->Hint = FORMAT(L"%s|%s:\n%s",
  908. (FTransferDropDownHint, StripHotkey(Name),
  909. GUIConfiguration->CurrentCopyParam.GetInfoStr(L"; ",
  910. FLAGMASK(HasActiveTerminal(), Terminal->UsableCopyParamAttrs(0).General))));
  911. // update the label, otherwise when it is updated only on the first draw
  912. // of the list, it is drawn "bold" for some reason
  913. FTransferListHoverIndex = TransferList->ItemIndex;
  914. UpdateTransferLabel();
  915. }
  916. __finally
  917. {
  918. TransferList->Strings->EndUpdate();
  919. }
  920. }
  921. //---------------------------------------------------------------------------
  922. void __fastcall TCustomScpExplorerForm::UpdateCustomCommandsToolbar()
  923. {
  924. TTBXToolbar * Toolbar = dynamic_cast<TTBXToolbar *>(GetComponent(fcCustomCommandsBand));
  925. DebugAssert(Toolbar != NULL);
  926. NonVisualDataModule->UpdateCustomCommandsToolbar(Toolbar);
  927. }
  928. //---------------------------------------------------------------------------
  929. void __fastcall TCustomScpExplorerForm::UpdateActions()
  930. {
  931. TForm::UpdateActions();
  932. if (ComponentVisible[fcCustomCommandsBand])
  933. {
  934. UpdateCustomCommandsToolbar();
  935. }
  936. }
  937. //---------------------------------------------------------------------------
  938. void __fastcall TCustomScpExplorerForm::UpdateSessionsPageControlHeight()
  939. {
  940. SessionsPageControl->Height = SessionsPageControl->GetTabsHeight();
  941. }
  942. //---------------------------------------------------------------------------
  943. void __fastcall TCustomScpExplorerForm::UpdateRowSelect(TCustomDirView * DirView)
  944. {
  945. if (DirView->RowSelect != WinConfiguration->FullRowSelect)
  946. {
  947. DirView->RowSelect = WinConfiguration->FullRowSelect;
  948. // selection is not redrawn automatically when RowSelect changes
  949. DirView->Invalidate();
  950. }
  951. }
  952. //---------------------------------------------------------------------------
  953. void __fastcall TCustomScpExplorerForm::ConfigurationChanged()
  954. {
  955. Color = GetBtnFaceColor();
  956. DebugAssert(Configuration && RemoteDirView);
  957. RemoteDirView->DDAllowMove = !WinConfiguration->DDDisableMove;
  958. RemoteDirView->DimmHiddenFiles = WinConfiguration->DimmHiddenFiles;
  959. RemoteDirView->ShowHiddenFiles = WinConfiguration->ShowHiddenFiles;
  960. RemoteDirView->FormatSizeBytes = WinConfiguration->FormatSizeBytes;
  961. RemoteDirView->ShowInaccesibleDirectories = WinConfiguration->ShowInaccesibleDirectories;
  962. RemoteDirView->NaturalOrderNumericalSorting = WinConfiguration->NaturalOrderNumericalSorting;
  963. UpdateRowSelect(RemoteDirView);
  964. RemoteDriveView->DDAllowMove = !WinConfiguration->DDDisableMove;
  965. RemoteDriveView->DimmHiddenDirs = WinConfiguration->DimmHiddenFiles;
  966. RemoteDriveView->ShowHiddenDirs = WinConfiguration->ShowHiddenFiles;
  967. RemoteDriveView->ShowInaccesibleDirectories = WinConfiguration->ShowInaccesibleDirectories;
  968. RemoteDriveView->NaturalOrderNumericalSorting = WinConfiguration->NaturalOrderNumericalSorting;
  969. UpdateSessionsPageControlHeight();
  970. SetDockAllowDrag(!WinConfiguration->LockToolbars);
  971. UpdateToolbarDisplayMode();
  972. UpdateTransferList();
  973. if (Terminal != NULL)
  974. {
  975. TransferPresetAutoSelect();
  976. }
  977. UpdateQueueView();
  978. UpdateControls();
  979. // this can be called even before constuctor finishes.
  980. if (FEditorManager != NULL)
  981. {
  982. FEditorManager->ProcessFiles(FileConfigurationChanged, NULL);
  983. }
  984. // this can be called even before constuctor finishes.
  985. if (FLocalEditors != NULL)
  986. {
  987. for (int Index = 0; Index < FLocalEditors->Count; Index++)
  988. {
  989. ReconfigureEditorForm(static_cast<TForm *>(FLocalEditors->Items[Index]));
  990. }
  991. }
  992. if (ComponentVisible[fcCustomCommandsBand])
  993. {
  994. UpdateCustomCommandsToolbar();
  995. }
  996. // show only when keeping queue items forever,
  997. // otherwise, its enough to have in in the context menu
  998. QueueDeleteAllDoneQueueToolbarItem->Visible =
  999. WinConfiguration->QueueKeepDoneItems && (WinConfiguration->QueueKeepDoneItemsFor < 0);
  1000. if (FFileColorsCurrent != WinConfiguration->FileColors)
  1001. {
  1002. TFileColorData::LoadList(WinConfiguration->FileColors, FFileColors);
  1003. FFileColorsCurrent = WinConfiguration->FileColors;
  1004. FileColorsChanged();
  1005. }
  1006. }
  1007. //---------------------------------------------------------------------------
  1008. void __fastcall TCustomScpExplorerForm::DoFileColorsChanged(TCustomDirView * DirView)
  1009. {
  1010. DirView->OnGetItemColor = FFileColors.empty() ? TDirViewGetItemColorEvent(NULL) : TDirViewGetItemColorEvent(&DirViewGetItemColor);
  1011. DirView->Invalidate();
  1012. }
  1013. //---------------------------------------------------------------------------
  1014. void __fastcall TCustomScpExplorerForm::FileColorsChanged()
  1015. {
  1016. DoFileColorsChanged(RemoteDirView);
  1017. }
  1018. //---------------------------------------------------------------------------
  1019. void __fastcall TCustomScpExplorerForm::FileConfigurationChanged(
  1020. const UnicodeString FileName, TEditedFileData * /*Data*/, TObject * Token,
  1021. void * /*Arg*/)
  1022. {
  1023. if (Token != NULL)
  1024. {
  1025. TForm * Editor = dynamic_cast<TForm*>(Token);
  1026. DebugAssert(Editor != NULL);
  1027. ReconfigureEditorForm(Editor);
  1028. }
  1029. }
  1030. //---------------------------------------------------------------------------
  1031. void __fastcall TCustomScpExplorerForm::EnableDDTransferConfirmation(TObject * /*Sender*/)
  1032. {
  1033. WinConfiguration->DDTransferConfirmation = asOn;
  1034. }
  1035. //---------------------------------------------------------------------------
  1036. void __fastcall TCustomScpExplorerForm::CopyParamDialogAfter(
  1037. TTransferDirection /*Direction*/, bool /*Temp*/, const UnicodeString & /*TargetDirectory*/)
  1038. {
  1039. // noop
  1040. }
  1041. //---------------------------------------------------------------------------
  1042. bool TCustomScpExplorerForm::GetDoNotShowCopyDialogDefault(bool DragDrop)
  1043. {
  1044. return DragDrop && (WinConfiguration->DDTransferConfirmation == asAuto);
  1045. }
  1046. //---------------------------------------------------------------------------
  1047. void TCustomScpExplorerForm::HandleDoNotShowCopyDialogAgain(bool DragDrop, bool DoNotShowAgain)
  1048. {
  1049. if (DoNotShowAgain)
  1050. {
  1051. if (DragDrop)
  1052. {
  1053. if (WinConfiguration->DDTransferConfirmation == asAuto)
  1054. {
  1055. PopupTrayBalloon(
  1056. NULL, LoadStr(DD_TRANSFER_CONFIRM_OFF2), qtInformation, NULL, 0, EnableDDTransferConfirmation, NULL);
  1057. }
  1058. WinConfiguration->DDTransferConfirmation = asOff;
  1059. }
  1060. else
  1061. {
  1062. WinConfiguration->ConfirmTransferring = false;
  1063. }
  1064. }
  1065. else
  1066. {
  1067. // User explicitly unchecked "do not show again",
  1068. // so show him the dialog the next time
  1069. if (DragDrop && (WinConfiguration->DDTransferConfirmation == asAuto))
  1070. {
  1071. WinConfiguration->DDTransferConfirmation = asOn;
  1072. }
  1073. }
  1074. }
  1075. //---------------------------------------------------------------------------
  1076. bool __fastcall TCustomScpExplorerForm::CopyParamDialog(
  1077. TTransferDirection Direction, TTransferType Type, bool Temp,
  1078. TStrings * FileList, UnicodeString & TargetDirectory, TGUICopyParamType & CopyParam,
  1079. bool Confirm, bool DragDrop, int Options)
  1080. {
  1081. bool Result = true;
  1082. DebugAssert(HasActiveTerminal());
  1083. // these parameters are known in advance
  1084. int Params =
  1085. FLAGMASK(Type == ttMove, cpDelete);
  1086. bool ToTemp = (Temp && (Direction == tdToLocal));
  1087. if (Result && Confirm && WinConfiguration->ConfirmTransferring)
  1088. {
  1089. bool DisableNewerOnly =
  1090. (!Terminal->IsCapable[fcNewerOnlyUpload] && (Direction == tdToRemote)) ||
  1091. ToTemp;
  1092. Options |=
  1093. FLAGMASK(ToTemp, coTemp) |
  1094. FLAGMASK(!Terminal->IsCapable[fcBackgroundTransfers], coDisableQueue) |
  1095. coDoNotShowAgain;
  1096. TUsableCopyParamAttrs UsableCopyParamAttrs = Terminal->UsableCopyParamAttrs(Params);
  1097. int CopyParamAttrs = (Direction == tdToRemote ?
  1098. UsableCopyParamAttrs.Upload : UsableCopyParamAttrs.Download) |
  1099. FLAGMASK(DisableNewerOnly, cpaNoNewerOnly);
  1100. int OutputOptions = FLAGMASK(GetDoNotShowCopyDialogDefault(DragDrop), cooDoNotShowAgain);
  1101. std::unique_ptr<TSessionData> SessionData(SessionDataForCode());
  1102. FlashOnBackground(); // Particularly when called from ClipboardFakeCreated
  1103. Result = DoCopyDialog(Direction == tdToRemote, Type == ttMove,
  1104. FileList, TargetDirectory, &CopyParam, Options, CopyParamAttrs, SessionData.get(), &OutputOptions, -1);
  1105. if (Result)
  1106. {
  1107. HandleDoNotShowCopyDialogAgain(DragDrop, FLAGSET(OutputOptions, cooDoNotShowAgain));
  1108. CopyParamDialogAfter(Direction, Temp, TargetDirectory);
  1109. }
  1110. }
  1111. if (Result && CopyParam.Queue && !ToTemp && Terminal->IsCapable[fcBackgroundTransfers])
  1112. {
  1113. Configuration->Usage->Inc(L"TransfersOnBackground");
  1114. // these parameter are known only after transfer dialog
  1115. Params |=
  1116. (CopyParam.QueueNoConfirmation ? cpNoConfirmation : 0);
  1117. AddQueueItem(Queue, Direction, FileList, TargetDirectory, CopyParam, Params);
  1118. Result = false;
  1119. ClearTransferSourceSelection(Direction);
  1120. }
  1121. return Result;
  1122. }
  1123. //---------------------------------------------------------------------------
  1124. void __fastcall TCustomScpExplorerForm::ClearTransferSourceSelection(TTransferDirection Direction)
  1125. {
  1126. if (FOnFileOperationFinished != NULL)
  1127. {
  1128. FOnFileOperationFinished(UnicodeString(), true);
  1129. }
  1130. else
  1131. {
  1132. TOperationSide Side = ((Direction == tdToRemote) ? osLocal : osRemote);
  1133. if (HasDirView[Side])
  1134. {
  1135. DirView(Side)->SelectAll(smNone);
  1136. }
  1137. }
  1138. }
  1139. //---------------------------------------------------------------------------
  1140. void __fastcall TCustomScpExplorerForm::AddQueueItem(
  1141. TTerminalQueue * Queue, TTransferDirection Direction, TStrings * FileList,
  1142. const UnicodeString TargetDirectory, const TGUICopyParamType & CopyParam,
  1143. int Params)
  1144. {
  1145. DebugAssert(Queue != NULL);
  1146. bool SingleFile = false;
  1147. if (FileList->Count == 1)
  1148. {
  1149. if (Direction == tdToRemote)
  1150. {
  1151. UnicodeString FileName = FileList->Strings[0];
  1152. SingleFile = FileExists(ApiPath(FileName));
  1153. }
  1154. else
  1155. {
  1156. TRemoteFile * File = static_cast<TRemoteFile *>(FileList->Objects[0]);
  1157. SingleFile = !File->IsDirectory;
  1158. }
  1159. }
  1160. TQueueItem * QueueItem;
  1161. if (Direction == tdToRemote)
  1162. {
  1163. QueueItem = new TUploadQueueItem(Terminal, FileList, TargetDirectory,
  1164. &CopyParam, Params, SingleFile, CopyParam.QueueParallel);
  1165. }
  1166. else
  1167. {
  1168. QueueItem = new TDownloadQueueItem(Terminal, FileList, TargetDirectory,
  1169. &CopyParam, Params, SingleFile, CopyParam.QueueParallel);
  1170. }
  1171. AddQueueItem(Queue, QueueItem, Terminal);
  1172. }
  1173. //---------------------------------------------------------------------------
  1174. void __fastcall TCustomScpExplorerForm::AddQueueItem(TTerminalQueue * Queue, TQueueItem * QueueItem, TManagedTerminal * Terminal)
  1175. {
  1176. if (Queue->IsEmpty)
  1177. {
  1178. Terminal->QueueOperationStart = Now();
  1179. }
  1180. Queue->AddItem(QueueItem);
  1181. }
  1182. //---------------------------------------------------------------------------
  1183. void __fastcall TCustomScpExplorerForm::RestoreFormParams()
  1184. {
  1185. }
  1186. //---------------------------------------------------------------------------
  1187. void __fastcall TCustomScpExplorerForm::InitControls()
  1188. {
  1189. DebugAssert(FDocks != NULL);
  1190. for (int Index = 0; Index < ComponentCount; Index++)
  1191. {
  1192. TTBDock * Dock = dynamic_cast<TTBDock *>(Components[Index]);
  1193. if ((Dock != NULL) && (Dock->Tag == 0))
  1194. {
  1195. FDocks->Add(Dock);
  1196. }
  1197. }
  1198. CollectItemsWithTextDisplayMode(this);
  1199. }
  1200. //---------------------------------------------------------------------------
  1201. void __fastcall TCustomScpExplorerForm::RestoreParams()
  1202. {
  1203. ConfigurationChanged();
  1204. QueuePanel->Height =
  1205. LoadDimension(WinConfiguration->QueueView.Height, WinConfiguration->QueueView.HeightPixelsPerInch, this);
  1206. LoadListViewStr(QueueView3, WinConfiguration->QueueView.Layout);
  1207. QueueDock->Visible = WinConfiguration->QueueView.ToolBar;
  1208. QueueLabel->Visible = WinConfiguration->QueueView.Label;
  1209. QueueFileList->Visible = WinConfiguration->QueueView.FileList;
  1210. QueueFileList->Height =
  1211. LoadDimension(WinConfiguration->QueueView.FileListHeight, WinConfiguration->QueueView.FileListHeightPixelsPerInch, this);
  1212. if (QueueFileList->Visible)
  1213. {
  1214. AdjustQueueLayout();
  1215. }
  1216. UpdateCustomCommandsToolbar();
  1217. }
  1218. //---------------------------------------------------------------------------
  1219. void __fastcall TCustomScpExplorerForm::StoreParams()
  1220. {
  1221. int APixelsPerInch = GetControlPixelsPerInch(this);
  1222. WinConfiguration->QueueView.Height = QueuePanel->Height;
  1223. WinConfiguration->QueueView.HeightPixelsPerInch = APixelsPerInch;
  1224. WinConfiguration->QueueView.Layout = GetListViewStr(QueueView3);
  1225. WinConfiguration->QueueView.ToolBar = QueueDock->Visible;
  1226. WinConfiguration->QueueView.Label = QueueLabel->Visible;
  1227. WinConfiguration->QueueView.FileList = QueueFileList->Visible;
  1228. WinConfiguration->QueueView.FileListHeight = QueueFileList->Height;
  1229. WinConfiguration->QueueView.FileListHeightPixelsPerInch = APixelsPerInch;
  1230. }
  1231. //---------------------------------------------------------------------------
  1232. void __fastcall TCustomScpExplorerForm::CreateParams(TCreateParams & Params)
  1233. {
  1234. if (!FFormRestored)
  1235. {
  1236. FFormRestored = true;
  1237. RestoreFormParams();
  1238. }
  1239. TForm::CreateParams(Params);
  1240. }
  1241. //---------------------------------------------------------------------------
  1242. void __fastcall TCustomScpExplorerForm::SetDockAllowDrag(bool value)
  1243. {
  1244. DebugAssert(FDocks != NULL);
  1245. for (int Index = 0; Index < FDocks->Count; Index++)
  1246. {
  1247. static_cast<TTBDock*>(FDocks->Items[Index])->AllowDrag = value;
  1248. }
  1249. }
  1250. //---------------------------------------------------------------------------
  1251. void __fastcall TCustomScpExplorerForm::LoadToolbarsLayoutStr(UnicodeString LayoutStr, UnicodeString ButtonsStr)
  1252. {
  1253. SetDockAllowDrag(true);
  1254. while (!ButtonsStr.IsEmpty())
  1255. {
  1256. UnicodeString S = CutToChar(ButtonsStr, L';', true);
  1257. UnicodeString Name = CutToChar(S, L'=', true);
  1258. for (int ToolbarIndex = 0; ToolbarIndex < ComponentCount; ToolbarIndex++)
  1259. {
  1260. TTBCustomToolbar * Toolbar = dynamic_cast<TTBCustomToolbar *>(Components[ToolbarIndex]);
  1261. if ((Toolbar != NULL) && SameText(Name, GetToolbarKey(Toolbar->Name)))
  1262. {
  1263. while (!S.IsEmpty())
  1264. {
  1265. UnicodeString S2 = CutToChar(S, L',', true);
  1266. UnicodeString Name = CutToChar(S2, L':', true);
  1267. for (int ItemIndex = 0; ItemIndex < Toolbar->Items->Count; ItemIndex++)
  1268. {
  1269. TTBCustomItem * Item = Toolbar->Items->Items[ItemIndex];
  1270. if (SameText(GetToolbarItemName(Item), Name))
  1271. {
  1272. // forward compatibility
  1273. UnicodeString S3 = CutToChar(S2, L':', true);
  1274. bool Visible = StrToIntDef(S3, 0);
  1275. Item->Visible = Visible;
  1276. }
  1277. }
  1278. }
  1279. }
  1280. }
  1281. }
  1282. ::LoadToolbarsLayoutStr(this, LayoutStr);
  1283. SetDockAllowDrag(!WinConfiguration->LockToolbars);
  1284. }
  1285. //---------------------------------------------------------------------------
  1286. UnicodeString __fastcall TCustomScpExplorerForm::GetToolbarsLayoutStr()
  1287. {
  1288. UnicodeString Result;
  1289. SetDockAllowDrag(true);
  1290. Result = ::GetToolbarsLayoutStr(this);
  1291. SetDockAllowDrag(!WinConfiguration->LockToolbars);
  1292. return Result;
  1293. }
  1294. //---------------------------------------------------------------------------
  1295. UnicodeString __fastcall TCustomScpExplorerForm::GetToolbarItemName(TTBCustomItem * Item)
  1296. {
  1297. UnicodeString Result;
  1298. if (Item->Action != NULL)
  1299. {
  1300. Result = Item->Action->Name;
  1301. Result = RemoveSuffix(Result, L"Action", true);
  1302. }
  1303. else
  1304. {
  1305. Result = Item->Name;
  1306. Result = RemoveSuffix(Result, L"SubmenuItem", true);
  1307. Result = RemoveSuffix(Result, L"Item", true);
  1308. }
  1309. return Result;
  1310. }
  1311. //---------------------------------------------------------------------------
  1312. UnicodeString __fastcall TCustomScpExplorerForm::GetToolbarsButtonsStr()
  1313. {
  1314. UnicodeString Result;
  1315. for (int ToolbarIndex = 0; ToolbarIndex < ComponentCount; ToolbarIndex++)
  1316. {
  1317. UnicodeString ToolbarStr;
  1318. TTBCustomToolbar * Toolbar = dynamic_cast<TTBCustomToolbar *>(Components[ToolbarIndex]);
  1319. if ((Toolbar != NULL) && (Toolbar != QueueToolbar))
  1320. {
  1321. for (int ItemIndex = 0; ItemIndex < Toolbar->Items->Count; ItemIndex++)
  1322. {
  1323. TTBCustomItem * Item = Toolbar->Items->Items[ItemIndex];
  1324. // Currently all buttons are visible by default, so we can safely remember all hidden buttons.
  1325. // Once we introduce any buttons that are hidden by default, we would have to remember their initial state
  1326. // and save the changes here only.
  1327. if (NonVisualDataModule->IsCustomizableToolbarItem(Item) && !Item->Visible)
  1328. {
  1329. UnicodeString Name = GetToolbarItemName(Item);
  1330. DebugAssert(Name.Pos(L"TBX") == 0);
  1331. AddToList(ToolbarStr, FORMAT(L"%s:0", (Name)), L",");
  1332. }
  1333. }
  1334. }
  1335. if (!ToolbarStr.IsEmpty())
  1336. {
  1337. AddToList(Result, FORMAT(L"%s=%s", (GetToolbarKey(Toolbar->Name), ToolbarStr)), L";");
  1338. }
  1339. }
  1340. return Result;
  1341. }
  1342. //---------------------------------------------------------------------------
  1343. void __fastcall TCustomScpExplorerForm::CreateProgressForm(TSynchronizeProgress * SynchronizeProgress)
  1344. {
  1345. DebugAssert(FProgressForm == NULL);
  1346. bool AllowSkip = (Terminal != NULL) ? Terminal->IsCapable[fcSkipTransfer] : false;
  1347. FProgressForm = new TProgressForm(Application, (FTransferResumeList != NULL), AllowSkip, SynchronizeProgress);
  1348. FProgressForm->DeleteLocalToRecycleBin =
  1349. (WinConfiguration->DeleteToRecycleBin != FAlternativeDelete);
  1350. FProgressForm->DeleteRemoteToRecycleBin =
  1351. (Terminal != NULL) &&
  1352. (Terminal->SessionData->DeleteToRecycleBin != FAlternativeDelete) &&
  1353. !Terminal->SessionData->RecycleBinPath.IsEmpty();
  1354. // As progress window has delayed show now, we need to lock ourselves, (at least) until then
  1355. LockWindow();
  1356. // Actually, do not know what hides the progress once the operation finishes
  1357. // (it is possibly the SetQueueProgress - and we should not rely on that)
  1358. SetTaskbarListProgressState(TBPF_NORMAL);
  1359. }
  1360. //---------------------------------------------------------------------------
  1361. void __fastcall TCustomScpExplorerForm::DestroyProgressForm()
  1362. {
  1363. UnlockWindow();
  1364. SAFE_DESTROY(FProgressForm);
  1365. SetQueueProgress();
  1366. }
  1367. //---------------------------------------------------------------------------
  1368. void __fastcall TCustomScpExplorerForm::FileOperationProgress(
  1369. TFileOperationProgressType & ProgressData)
  1370. {
  1371. // Download to temporary local directory
  1372. if (FPendingTempSpaceWarn && ProgressData.InProgress && ProgressData.TotalSizeSet)
  1373. {
  1374. bool Continue = true;
  1375. FPendingTempSpaceWarn = false;
  1376. DoWarnLackOfTempSpace(ProgressData.Directory, ProgressData.TotalSize, Continue);
  1377. if (!Continue)
  1378. {
  1379. Abort();
  1380. }
  1381. }
  1382. // operation is being executed and we still didn't show up progress form
  1383. if (ProgressData.InProgress && !FProgressForm)
  1384. {
  1385. CreateProgressForm(NULL);
  1386. DebugAssert(FProgressForm->OnceDoneOperation == odoIdle);
  1387. FProgressForm->OnceDoneOperation = ProgressData.InitialOnceDoneOperation;
  1388. }
  1389. // operation is finished (or terminated), so we hide progress form
  1390. else if (!ProgressData.InProgress && (FProgressForm != NULL) && (FProgressForm->SynchronizeProgress == NULL))
  1391. {
  1392. DestroyProgressForm();
  1393. if (ProgressData.Operation == foCalculateSize)
  1394. {
  1395. // When calculating size before transfer, the abort caused by
  1396. // cancel flag set due to "MoveToQueue" below,
  1397. // is not propagated back to us in ExecuteFileOperation,
  1398. // as it is silently swallowed.
  1399. // So we explicitly (re)throw it here.
  1400. if (FMoveToQueue)
  1401. {
  1402. Abort();
  1403. }
  1404. }
  1405. else
  1406. {
  1407. if ((ProgressData.Cancel == csContinue) &&
  1408. !FAutoOperation)
  1409. {
  1410. OperationComplete(ProgressData.StartTime);
  1411. }
  1412. }
  1413. }
  1414. if (FProgressForm &&
  1415. // Can happen only during synchronization, when ending one of the operations
  1416. ProgressData.InProgress)
  1417. {
  1418. FProgressForm->SetProgressData(ProgressData);
  1419. DebugAssert(ProgressData.InProgress);
  1420. SetTaskbarListProgressValue(&ProgressData);
  1421. if (FProgressForm->Cancel > csContinue)
  1422. {
  1423. ProgressData.SetCancelAtLeast(FProgressForm->Cancel);
  1424. if (FProgressForm->Cancel == csCancelFile)
  1425. {
  1426. FProgressForm->ClearCancel();
  1427. }
  1428. // cancel cancels even the move
  1429. FMoveToQueue = false;
  1430. }
  1431. else if (FProgressForm->MoveToQueue)
  1432. {
  1433. FMoveToQueue = true;
  1434. ProgressData.SetCancelAtLeast(csCancel);
  1435. }
  1436. if ((FTransferResumeList != NULL) &&
  1437. ProgressData.InProgress &&
  1438. ((ProgressData.Operation == foCopy) || (ProgressData.Operation == foMove)) &&
  1439. !ProgressData.FullFileName.IsEmpty())
  1440. {
  1441. if ((FTransferResumeList->Count == 0) ||
  1442. (FTransferResumeList->Strings[FTransferResumeList->Count - 1] != ProgressData.FullFileName))
  1443. {
  1444. // note that we do not recognize directories from files here
  1445. FTransferResumeList->Add(ProgressData.FullFileName);
  1446. }
  1447. }
  1448. }
  1449. }
  1450. //---------------------------------------------------------------------------
  1451. void __fastcall TCustomScpExplorerForm::OperationComplete(
  1452. const TDateTime & StartTime)
  1453. {
  1454. if (GUIConfiguration->BeepOnFinish &&
  1455. (Now() - StartTime > GUIConfiguration->BeepOnFinishAfter))
  1456. {
  1457. UnicodeString BeepSound = GUIConfiguration->BeepSound;
  1458. DWORD Sound;
  1459. if (!ExtractFileExt(BeepSound).IsEmpty())
  1460. {
  1461. Sound = SND_FILENAME;
  1462. }
  1463. else
  1464. {
  1465. Sound = SND_ALIAS;
  1466. }
  1467. PlaySound(BeepSound.c_str(), NULL, Sound | SND_ASYNC);
  1468. }
  1469. }
  1470. //---------------------------------------------------------------------------
  1471. void __fastcall TCustomScpExplorerForm::OperationProgress(
  1472. TFileOperationProgressType & ProgressData)
  1473. {
  1474. // For example, when calling TFileOperationProgressType::Suspend in TTerminal::QueryReopen,
  1475. // we cannot recurse back to TTerminal::Idle as that may detect another error, calling "suspend" again.
  1476. TAutoNestingCounter Counter(FDoNotIdleCurrentTerminal);
  1477. FileOperationProgress(ProgressData);
  1478. }
  1479. //---------------------------------------------------------------------------
  1480. UnicodeString __fastcall TCustomScpExplorerForm::GetProgressTitle()
  1481. {
  1482. UnicodeString Result;
  1483. if (FProgressForm != NULL)
  1484. {
  1485. Result = FProgressForm->ProgressStr();
  1486. }
  1487. return Result;
  1488. }
  1489. //---------------------------------------------------------------------------
  1490. bool __fastcall TCustomScpExplorerForm::PanelOperation(TOperationSide /*Side*/,
  1491. bool DragDrop)
  1492. {
  1493. return (!DragDrop && (DropSourceControl == NULL)) ||
  1494. (DropSourceControl == DirView(osOther));
  1495. }
  1496. //---------------------------------------------------------------------------
  1497. void __fastcall TCustomScpExplorerForm::DoOperationFinished(
  1498. TFileOperation Operation, TOperationSide Side,
  1499. bool /*Temp*/, const UnicodeString & FileName, bool Success,
  1500. TOnceDoneOperation & OnceDoneOperation)
  1501. {
  1502. if (!FAutoOperation)
  1503. {
  1504. // no selection on "/upload", form serves only as event handler
  1505. // (it is not displayed)
  1506. if (PanelOperation(Side, FDragDropOperation) &&
  1507. Visible && (Operation != foCalculateSize) &&
  1508. (Operation != foGetProperties) && (Operation != foCalculateChecksum))
  1509. {
  1510. if (FOnFileOperationFinished != NULL)
  1511. {
  1512. FOnFileOperationFinished(FileName, Success);
  1513. }
  1514. else
  1515. {
  1516. TOperationSide DViewSide;
  1517. if ((FProgressSide != osCurrent) &&
  1518. IsLocalBrowserMode() && (Side == osLocal)) // Only to limit the impact
  1519. {
  1520. // assume the operation is over the focused panel
  1521. DViewSide = FProgressSide;
  1522. }
  1523. else
  1524. {
  1525. DViewSide = Side;
  1526. }
  1527. TCustomDirView * DView = DirView(DViewSide);
  1528. UnicodeString FileNameOnly = ExtractFileName(FileName, !IsSideLocalBrowser(DViewSide));
  1529. TListItem *Item = DView->FindFileItem(FileNameOnly);
  1530. // this can happen when local drive is unplugged in the middle of the operation
  1531. if (Item != NULL)
  1532. {
  1533. if (Success) Item->Selected = false;
  1534. DView->MakeProgressVisible(Item);
  1535. }
  1536. }
  1537. }
  1538. if ((Operation == foCopy) || (Operation == foMove))
  1539. {
  1540. if (DebugAlwaysTrue(!IsLocalBrowserMode()))
  1541. {
  1542. if (Side == osLocal)
  1543. {
  1544. Configuration->Usage->Inc(L"UploadedFiles");
  1545. }
  1546. else
  1547. {
  1548. Configuration->Usage->Inc(L"DownloadedFiles");
  1549. }
  1550. }
  1551. }
  1552. }
  1553. if (Success && (FSynchronizeController != NULL))
  1554. {
  1555. DebugAssert(!IsSideLocalBrowser(osRemote));
  1556. if (Operation == foCopy)
  1557. {
  1558. DebugAssert(Side == osLocal);
  1559. FSynchronizeController->LogOperation(soUpload, FileName);
  1560. }
  1561. else if (Operation == foDelete)
  1562. {
  1563. DebugAssert(Side == osRemote);
  1564. FSynchronizeController->LogOperation(soDelete, FileName);
  1565. }
  1566. }
  1567. if (FProgressForm)
  1568. {
  1569. OnceDoneOperation = FProgressForm->OnceDoneOperation;
  1570. }
  1571. }
  1572. //---------------------------------------------------------------------------
  1573. void __fastcall TCustomScpExplorerForm::OperationFinished(
  1574. TFileOperation Operation, TOperationSide Side,
  1575. bool Temp, const UnicodeString & FileName, Boolean Success,
  1576. TOnceDoneOperation & OnceDoneOperation)
  1577. {
  1578. TAutoNestingCounter Counter(FDoNotIdleCurrentTerminal);
  1579. DoOperationFinished(Operation, Side, Temp, FileName, Success,
  1580. OnceDoneOperation);
  1581. }
  1582. //---------------------------------------------------------------------------
  1583. bool TCustomScpExplorerForm::IsLocalBrowserMode()
  1584. {
  1585. return false;
  1586. }
  1587. //---------------------------------------------------------------------------
  1588. bool TCustomScpExplorerForm::SupportsLocalBrowser()
  1589. {
  1590. return false;
  1591. }
  1592. //---------------------------------------------------------------------------
  1593. bool TCustomScpExplorerForm::IsSideLocalBrowser(TOperationSide)
  1594. {
  1595. return false;
  1596. }
  1597. //---------------------------------------------------------------------------
  1598. TCustomDirView * TCustomScpExplorerForm::GetCurrentLocalBrowser()
  1599. {
  1600. return NULL;
  1601. }
  1602. //---------------------------------------------------------------------------
  1603. TCustomDirView * __fastcall TCustomScpExplorerForm::DirView(TOperationSide Side)
  1604. {
  1605. DebugAssert(GetSide(Side) == osRemote);
  1606. DebugUsedParam(Side);
  1607. return RemoteDirView;
  1608. }
  1609. //---------------------------------------------------------------------------
  1610. TCustomDriveView * __fastcall TCustomScpExplorerForm::DriveView(TOperationSide Side)
  1611. {
  1612. DebugAssert(GetSide(Side) == osRemote);
  1613. DebugUsedParam(Side);
  1614. return RemoteDriveView;
  1615. }
  1616. //---------------------------------------------------------------------------
  1617. bool __fastcall TCustomScpExplorerForm::DirViewEnabled(TOperationSide Side)
  1618. {
  1619. DebugAssert(GetSide(Side) == osRemote);
  1620. DebugUsedParam(Side);
  1621. return HasActiveTerminal();
  1622. }
  1623. //---------------------------------------------------------------------------
  1624. bool __fastcall TCustomScpExplorerForm::GetEnableFocusedOperation(
  1625. TOperationSide Side, int FilesOnly)
  1626. {
  1627. return DirView(Side)->AnyFileSelected(true, (FilesOnly != 0), true);
  1628. }
  1629. //---------------------------------------------------------------------------
  1630. bool __fastcall TCustomScpExplorerForm::GetEnableSelectedOperation(
  1631. TOperationSide Side, int FilesOnly)
  1632. {
  1633. return DirView(Side)->AnyFileSelected(false, (FilesOnly != 0), true);
  1634. }
  1635. //---------------------------------------------------------------------------
  1636. struct THistoryItemData
  1637. {
  1638. short int Side;
  1639. short int Index;
  1640. };
  1641. //---------------------------------------------------------------------------
  1642. void __fastcall TCustomScpExplorerForm::HistoryGo(TOperationSide Side, int Index)
  1643. {
  1644. DirView(Side)->HistoryGo(Index);
  1645. }
  1646. //---------------------------------------------------------------------------
  1647. void __fastcall TCustomScpExplorerForm::HistoryItemClick(System::TObject* Sender)
  1648. {
  1649. TTBCustomItem * Item = dynamic_cast<TTBCustomItem *>(Sender);
  1650. THistoryItemData Data = *reinterpret_cast<THistoryItemData*>(&(Item->Tag));
  1651. HistoryGo((TOperationSide)Data.Side, Data.Index);
  1652. }
  1653. //---------------------------------------------------------------------------
  1654. void __fastcall TCustomScpExplorerForm::UpdateHistoryMenu(TOperationSide Side,
  1655. bool Back)
  1656. {
  1657. if (FHistoryMenu[Side == osLocal][Back] != NULL)
  1658. {
  1659. TCustomDirView * DView = DirView(Side);
  1660. TTBXPopupMenu * Menu = FHistoryMenu[Side == osLocal][Back];
  1661. int ICount = Back ? DView->BackCount : DView->ForwardCount;
  1662. if (ICount > 10)
  1663. {
  1664. ICount = 10;
  1665. }
  1666. Menu->Items->Clear();
  1667. THistoryItemData Data;
  1668. Data.Side = Side;
  1669. for (short int i = 1; i <= ICount; i++)
  1670. {
  1671. TTBCustomItem * Item = new TTBXItem(Menu);
  1672. Data.Index = static_cast<short int>(i * (Back ? -1 : 1));
  1673. Item->Caption = MinimizeName(DView->HistoryPath[Data.Index], 50, !IsSideLocalBrowser(Side));
  1674. Item->Hint = DView->HistoryPath[Data.Index];
  1675. DebugAssert(sizeof(int) == sizeof(THistoryItemData));
  1676. Item->Tag = *reinterpret_cast<int*>(&Data);
  1677. Item->OnClick = HistoryItemClick;
  1678. Menu->Items->Add(Item);
  1679. }
  1680. }
  1681. }
  1682. //---------------------------------------------------------------------------
  1683. TTBXPopupMenu * __fastcall TCustomScpExplorerForm::HistoryMenu(
  1684. TOperationSide Side, bool Back)
  1685. {
  1686. if (FHistoryMenu[Side == osLocal][Back] == NULL)
  1687. {
  1688. // workaround
  1689. // In Pascal the size of TTBXPopupMenu is 132, in C++ 136,
  1690. // operator new allocates memory in Pascal code, but calls inline
  1691. // contructor in C++, leading in problems, the function does
  1692. // both in Pascal code
  1693. FHistoryMenu[Side == osLocal][Back] = CreateTBXPopupMenu(this);
  1694. UpdateHistoryMenu(Side, Back);
  1695. }
  1696. return FHistoryMenu[Side == osLocal][Back];
  1697. }
  1698. //---------------------------------------------------------------------------
  1699. void __fastcall TCustomScpExplorerForm::DirViewHistoryChange(
  1700. TCustomDirView *Sender)
  1701. {
  1702. TOperationSide Side = (Sender == DirView(osOther) ? osOther : osLocal);
  1703. UpdateHistoryMenu(Side, true);
  1704. UpdateHistoryMenu(Side, false);
  1705. }
  1706. //---------------------------------------------------------------------------
  1707. bool __fastcall TCustomScpExplorerForm::CustomCommandRemoteAllowed()
  1708. {
  1709. // remote custom commands can be executed only if the server supports shell commands
  1710. // or have secondary shell
  1711. return HasActiveTerminal() && (Terminal->IsCapable[fcSecondaryShell] || Terminal->IsCapable[fcShellAnyCommand]);
  1712. }
  1713. //---------------------------------------------------------------------------
  1714. int __fastcall TCustomScpExplorerForm::CustomCommandState(
  1715. const TCustomCommandType & Command, bool OnFocused, TCustomCommandListType ListType)
  1716. {
  1717. // -1 = hidden, 0 = disabled, 1 = enabled
  1718. int Result;
  1719. TFileCustomCommand RemoteCustomCommand;
  1720. TLocalCustomCommand LocalCustomCommand;
  1721. TFileCustomCommand * NonInteractiveCustomCommand =
  1722. FLAGCLEAR(Command.Params, ccLocal) ? &RemoteCustomCommand : &LocalCustomCommand;
  1723. TInteractiveCustomCommand InteractiveCustomCommand(NonInteractiveCustomCommand);
  1724. UnicodeString Cmd = InteractiveCustomCommand.Complete(Command.Command, false);
  1725. if (FLAGCLEAR(Command.Params, ccLocal))
  1726. {
  1727. int AllowedState = CustomCommandRemoteAllowed() ? 1 : 0;
  1728. // custom command that does not operate with files can be executed anytime ...
  1729. if (!NonInteractiveCustomCommand->IsFileCommand(Cmd))
  1730. {
  1731. // ... but do not show such command in remote file menu
  1732. if ((ListType == ccltAll) || (ListType == ccltNonFile))
  1733. {
  1734. Result = AllowedState;
  1735. }
  1736. else
  1737. {
  1738. Result = -1;
  1739. }
  1740. }
  1741. else
  1742. {
  1743. if ((ListType == ccltAll) || ((ListType == ccltFile) && !IsSideLocalBrowser(FCurrentSide)))
  1744. {
  1745. Result = (DirView(FCurrentSide)->AnyFileSelected(OnFocused, false, true)) ? AllowedState : 0;
  1746. }
  1747. else
  1748. {
  1749. Result = -1;
  1750. }
  1751. }
  1752. }
  1753. else
  1754. {
  1755. // custom command that does not operate with files can be executed anytime
  1756. if (!NonInteractiveCustomCommand->IsFileCommand(Cmd))
  1757. {
  1758. Result = ((ListType == ccltAll) || (ListType == ccltNonFile)) ? 1 : -1;
  1759. }
  1760. else if (LocalCustomCommand.HasLocalFileName(Cmd))
  1761. {
  1762. if ((ListType == ccltAll) || (ListType == ccltFile))
  1763. {
  1764. // special case is "diff"-style command that can be executed over any side,
  1765. // if we have both sides
  1766. Result =
  1767. // Cannot have focus on both panels, so we have to call AnyFileSelected
  1768. // directly (instead of EnableSelectedOperation) to pass
  1769. // false to FocusedFileOnlyWhenFocused when panel is inactive.
  1770. ((HasDirView[osLocal] && !IsSideLocalBrowser(osRemote) &&
  1771. DirView(osLocal)->AnyFileSelected(false, false, (FCurrentSide == osLocal))) &&
  1772. DirView(osRemote)->AnyFileSelected(false, false, (FCurrentSide == osRemote))) ? 1 : 0;
  1773. }
  1774. else if (ListType == ccltBoth)
  1775. {
  1776. DebugAssert(!IsLocalBrowserMode());
  1777. Result = 1;
  1778. }
  1779. else
  1780. {
  1781. Result = -1;
  1782. }
  1783. }
  1784. else
  1785. {
  1786. if ((ListType == ccltAll) ||
  1787. ((ListType == ccltFile) && (!IsSideLocalBrowser(FCurrentSide) || FLAGCLEAR(Command.Params, ccRemoteFiles))))
  1788. {
  1789. Result = DirView(FCurrentSide)->AnyFileSelected(OnFocused, false, true) ? 1 : 0;
  1790. }
  1791. else
  1792. {
  1793. Result = -1;
  1794. }
  1795. }
  1796. if ((Result > 0) &&
  1797. LocalCustomCommand.IsSessionCommand(Cmd) &&
  1798. !HasActiveTerminal())
  1799. {
  1800. Result = 0;
  1801. }
  1802. }
  1803. return Result;
  1804. }
  1805. //---------------------------------------------------------------------------
  1806. void __fastcall TCustomScpExplorerForm::RemoteCustomCommand(
  1807. TStrings * FileList, const TCustomCommandType & ACommand,
  1808. const TCustomCommandData & Data, const UnicodeString & CommandCommand)
  1809. {
  1810. if (EnsureCommandSessionFallback(fcShellAnyCommand))
  1811. {
  1812. TRemoteCustomCommand RemoteCustomCommand(Data, Terminal->CurrentDirectory);
  1813. TWinInteractiveCustomCommand InteractiveCustomCommand(
  1814. &RemoteCustomCommand, ACommand.Name, ACommand.HomePage);
  1815. UnicodeString Command = InteractiveCustomCommand.Complete(CommandCommand, false);
  1816. Configuration->Usage->Inc(L"RemoteCustomCommandRuns2");
  1817. bool Capture =
  1818. FLAGSET(ACommand.Params, ccShowResults) ||
  1819. FLAGSET(ACommand.Params, ccCopyResults) ||
  1820. FLAGSET(ACommand.Params, ccShowResultsInMsgBox);
  1821. TCaptureOutputEvent OutputEvent = NULL;
  1822. DebugAssert(FCapturedLog == NULL);
  1823. if (Capture)
  1824. {
  1825. FCapturedLog = new TStringList();
  1826. OutputEvent = TerminalCaptureLog;
  1827. }
  1828. try
  1829. {
  1830. if (!RemoteCustomCommand.IsFileCommand(Command))
  1831. {
  1832. Terminal->AnyCommand(RemoteCustomCommand.Complete(Command, true),
  1833. OutputEvent);
  1834. }
  1835. else
  1836. {
  1837. Terminal->CustomCommandOnFiles(Command, ACommand.Params, FileList, OutputEvent);
  1838. }
  1839. if ((FCapturedLog != NULL) && (FCapturedLog->Count > 0))
  1840. {
  1841. if (FLAGSET(ACommand.Params, ccCopyResults))
  1842. {
  1843. CopyToClipboard(FCapturedLog);
  1844. }
  1845. if (FLAGSET(ACommand.Params, ccShowResults))
  1846. {
  1847. DoConsoleDialog(Terminal, L"", FCapturedLog);
  1848. }
  1849. else if (FLAGSET(ACommand.Params, ccShowResultsInMsgBox))
  1850. {
  1851. MessageDialog(FCapturedLog->Text, qtInformation, qaOK);
  1852. }
  1853. }
  1854. }
  1855. __finally
  1856. {
  1857. SAFE_DESTROY(FCapturedLog);
  1858. }
  1859. }
  1860. }
  1861. //---------------------------------------------------------------------------
  1862. void __fastcall TCustomScpExplorerForm::LocalCustomCommandPure(
  1863. TStrings * FileList, const TCustomCommandType & ACommand, const UnicodeString & Command, TStrings * ALocalFileList,
  1864. const TCustomCommandData & Data, bool LocalFileCommand, bool FileListCommand, UnicodeString * POutput)
  1865. {
  1866. DebugAssert(!IsLocalBrowserMode());
  1867. TStrings * LocalFileList = NULL;
  1868. TStrings * RemoteFileList = NULL;
  1869. TStrings * RemoteFileListFull = NULL;
  1870. try
  1871. {
  1872. if (LocalFileCommand)
  1873. {
  1874. if (ALocalFileList == NULL)
  1875. {
  1876. DebugAssert(HasDirView[osLocal]);
  1877. // Cannot have focus on both panels, so we have to call AnyFileSelected
  1878. // directly (instead of EnableSelectedOperation) to pass
  1879. // false to FocusedFileOnlyWhenFocused
  1880. DebugAssert(DirView(osLocal)->AnyFileSelected(false, false, false));
  1881. LocalFileList = DirView(osLocal)->CreateFileList(false, true, NULL);
  1882. }
  1883. else
  1884. {
  1885. LocalFileList = ALocalFileList;
  1886. }
  1887. if (FileListCommand)
  1888. {
  1889. if (LocalFileList->Count != 1)
  1890. {
  1891. throw Exception(LoadStr(CUSTOM_COMMAND_SELECTED_UNMATCH1));
  1892. }
  1893. }
  1894. else
  1895. {
  1896. if ((LocalFileList->Count != 1) &&
  1897. (FileList->Count != 1) &&
  1898. (LocalFileList->Count != FileList->Count))
  1899. {
  1900. throw Exception(LoadStr(CUSTOM_COMMAND_SELECTED_UNMATCH));
  1901. }
  1902. }
  1903. }
  1904. UnicodeString RootTempDir;
  1905. UnicodeString TempDir;
  1906. bool RemoteFiles = FLAGSET(ACommand.Params, ccRemoteFiles);
  1907. if (!RemoteFiles)
  1908. {
  1909. TemporarilyDownloadFiles(FileList, false, RootTempDir, TempDir, false, false, true);
  1910. }
  1911. try
  1912. {
  1913. TDateTimes RemoteFileTimes;
  1914. if (RemoteFiles)
  1915. {
  1916. RemoteFileList = FileList;
  1917. }
  1918. else
  1919. {
  1920. RemoteFileList = new TStringList();
  1921. TMakeLocalFileListParams MakeFileListParam;
  1922. MakeFileListParam.FileList = RemoteFileList;
  1923. MakeFileListParam.FileTimes = &RemoteFileTimes;
  1924. MakeFileListParam.IncludeDirs = FLAGSET(ACommand.Params, ccApplyToDirectories);
  1925. MakeFileListParam.Recursive =
  1926. FLAGSET(ACommand.Params, ccRecursive) && !FileListCommand;
  1927. ProcessLocalDirectory(TempDir, Terminal->MakeLocalFileList, &MakeFileListParam);
  1928. if (!MakeFileListParam.Recursive)
  1929. {
  1930. RemoteFileTimes.clear();
  1931. RemoteFileListFull = new TStringList();
  1932. MakeFileListParam.FileList = RemoteFileListFull;
  1933. MakeFileListParam.Recursive = true;
  1934. MakeFileListParam.IncludeDirs = false;
  1935. ProcessLocalDirectory(TempDir, Terminal->MakeLocalFileList, &MakeFileListParam);
  1936. }
  1937. else
  1938. {
  1939. RemoteFileListFull = RemoteFileList;
  1940. }
  1941. }
  1942. bool NonBlocking = FileListCommand && RemoteFiles && !POutput;
  1943. TFileOperationProgressType Progress(&OperationProgress, &OperationFinished);
  1944. if (!NonBlocking)
  1945. {
  1946. int Count;
  1947. if (FileListCommand)
  1948. {
  1949. Count = 1;
  1950. }
  1951. else if (LocalFileCommand)
  1952. {
  1953. Count = std::max(LocalFileList->Count, RemoteFileList->Count);
  1954. }
  1955. else
  1956. {
  1957. Count = RemoteFileList->Count;
  1958. }
  1959. Progress.Start(foCustomCommand, osRemote, Count);
  1960. DebugAssert(FProgressForm != NULL);
  1961. FProgressForm->ReadOnly = true;
  1962. }
  1963. try
  1964. {
  1965. if (FileListCommand)
  1966. {
  1967. UnicodeString LocalFile;
  1968. // MakeFileList does not delimit filenames
  1969. UnicodeString FileList = MakeFileList(RemoteFileList);
  1970. if (LocalFileCommand)
  1971. {
  1972. DebugAssert(LocalFileList->Count == 1);
  1973. LocalFile = LocalFileList->Strings[0];
  1974. }
  1975. TLocalCustomCommand CustomCommand(Data,
  1976. Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(), L"", LocalFile, FileList);
  1977. UnicodeString ShellCommand = CustomCommand.Complete(Command, true);
  1978. if (NonBlocking)
  1979. {
  1980. DebugAssert(!POutput);
  1981. ExecuteShellChecked(ShellCommand);
  1982. }
  1983. else
  1984. {
  1985. ExecuteProcessCheckedAndWait(ShellCommand, HelpKeyword, POutput);
  1986. }
  1987. }
  1988. else if (LocalFileCommand)
  1989. {
  1990. if (LocalFileList->Count == 1)
  1991. {
  1992. UnicodeString LocalFile = LocalFileList->Strings[0];
  1993. for (int Index = 0; Index < RemoteFileList->Count; Index++)
  1994. {
  1995. UnicodeString FileName = RemoteFileList->Strings[Index];
  1996. UnicodeString FileNameForProgress = RemoteFiles ? FileName : ExtractFileName(FileName);
  1997. Progress.SetFile(FileNameForProgress);
  1998. TLocalCustomCommand CustomCommand(Data,
  1999. Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(), FileName, LocalFile, L"");
  2000. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  2001. TOnceDoneOperation OnceDoneOperation;
  2002. // Do not unselect anything, as it won't work with two panels anyway
  2003. Progress.Finish(UnicodeString(), true, OnceDoneOperation);
  2004. }
  2005. }
  2006. else if (RemoteFileList->Count == 1)
  2007. {
  2008. UnicodeString FileName = RemoteFileList->Strings[0];
  2009. for (int Index = 0; Index < LocalFileList->Count; Index++)
  2010. {
  2011. UnicodeString LocalFileName = LocalFileList->Strings[Index];
  2012. UnicodeString LocalFileNameOnly = ExtractFileName(LocalFileName);
  2013. Progress.SetFile(LocalFileNameOnly);
  2014. TLocalCustomCommand CustomCommand(
  2015. Data, Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(),
  2016. FileName, LocalFileName, L"");
  2017. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  2018. TOnceDoneOperation OnceDoneOperation;
  2019. // Do not unselect anything, as it won't work with two panels anyway
  2020. Progress.Finish(UnicodeString(), true, OnceDoneOperation);
  2021. }
  2022. }
  2023. else
  2024. {
  2025. if (LocalFileList->Count != RemoteFileList->Count)
  2026. {
  2027. throw Exception(LoadStr(CUSTOM_COMMAND_PAIRS_DOWNLOAD_FAILED));
  2028. }
  2029. for (int Index = 0; Index < LocalFileList->Count; Index++)
  2030. {
  2031. UnicodeString FileName = RemoteFileList->Strings[Index];
  2032. UnicodeString LocalFileName = LocalFileList->Strings[Index];
  2033. UnicodeString LocalFileNameOnly = ExtractFileName(LocalFileName);
  2034. Progress.SetFile(LocalFileNameOnly); // sic
  2035. TLocalCustomCommand CustomCommand(
  2036. Data, Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(),
  2037. FileName, LocalFileName, L"");
  2038. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  2039. TOnceDoneOperation OnceDoneOperation;
  2040. // Do not unselect anything, as it won't work with two panels anyway
  2041. Progress.Finish(UnicodeString(), true, OnceDoneOperation);
  2042. }
  2043. }
  2044. }
  2045. else
  2046. {
  2047. for (int Index = 0; Index < RemoteFileList->Count; Index++)
  2048. {
  2049. UnicodeString FileName = RemoteFileList->Strings[Index];
  2050. UnicodeString FileNameForProgress = RemoteFiles ? FileName : ExtractFileName(FileName);
  2051. Progress.SetFile(FileNameForProgress);
  2052. TLocalCustomCommand CustomCommand(Data,
  2053. Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(),
  2054. FileName, L"", L"");
  2055. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  2056. TOnceDoneOperation OnceDoneOperation;
  2057. Progress.Finish(FileNameForProgress, true, OnceDoneOperation);
  2058. }
  2059. }
  2060. }
  2061. __finally
  2062. {
  2063. if (!NonBlocking)
  2064. {
  2065. Progress.Stop();
  2066. }
  2067. }
  2068. DebugAssert(!FAutoOperation);
  2069. if (!RemoteFiles)
  2070. {
  2071. TempDir = IncludeTrailingBackslash(TempDir);
  2072. for (int Index = 0; Index < RemoteFileListFull->Count; Index++)
  2073. {
  2074. UnicodeString FileName = RemoteFileListFull->Strings[Index];
  2075. if (DebugAlwaysTrue(SameText(TempDir, FileName.SubString(1, TempDir.Length()))) &&
  2076. // Skip directories, process only nested files.
  2077. // The check is redundant as FileAge fails for directories anyway.
  2078. !DirectoryExists(FileName))
  2079. {
  2080. UnicodeString RemoteDir =
  2081. UnixExtractFileDir(
  2082. UnixIncludeTrailingBackslash(Terminal->CurrentDirectory) +
  2083. ToUnixPath(FileName.SubString(TempDir.Length() + 1, FileName.Length() - TempDir.Length())));
  2084. TDateTime NewTime;
  2085. if (FileAge(FileName, NewTime) &&
  2086. (NewTime != RemoteFileTimes[Index]))
  2087. {
  2088. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  2089. TemporaryFileCopyParam(CopyParam);
  2090. CopyParam.FileMask = L"";
  2091. FAutoOperation = true;
  2092. std::unique_ptr<TStrings> TemporaryFilesList(new TStringList());
  2093. TemporaryFilesList->Add(FileName);
  2094. Terminal->CopyToRemote(TemporaryFilesList.get(), RemoteDir, &CopyParam, cpTemporary, NULL);
  2095. }
  2096. }
  2097. }
  2098. }
  2099. }
  2100. __finally
  2101. {
  2102. FAutoOperation = false;
  2103. if (!RootTempDir.IsEmpty() && DebugAlwaysTrue(!RemoteFiles))
  2104. {
  2105. RecursiveDeleteFile(ExcludeTrailingBackslash(RootTempDir), false);
  2106. }
  2107. }
  2108. }
  2109. __finally
  2110. {
  2111. if (RemoteFileList != FileList)
  2112. {
  2113. delete RemoteFileList;
  2114. }
  2115. if (LocalFileList != ALocalFileList)
  2116. {
  2117. delete LocalFileList;
  2118. }
  2119. if (RemoteFileListFull != RemoteFileList)
  2120. {
  2121. delete RemoteFileListFull;
  2122. }
  2123. }
  2124. }
  2125. //---------------------------------------------------------------------------
  2126. void __fastcall TCustomScpExplorerForm::LocalCustomCommandWithLocalFiles(
  2127. const TCustomCommandType & ACommand, const UnicodeString & Command, const TCustomCommandData & Data,
  2128. bool FileListCommand, UnicodeString * POutput)
  2129. {
  2130. std::unique_ptr<TStrings> SelectedFileList(DirView(osCurrent)->CreateFileList(false, true, NULL));
  2131. std::unique_ptr<TStrings> LocalFileList(new TStringList());
  2132. for (int Index = 0; Index < SelectedFileList->Count; Index++)
  2133. {
  2134. UnicodeString FileName = SelectedFileList->Strings[Index];
  2135. if (DirectoryExists(FileName))
  2136. {
  2137. if (FLAGSET(ACommand.Params, ccApplyToDirectories))
  2138. {
  2139. LocalFileList->Add(FileName);
  2140. }
  2141. if (FLAGSET(ACommand.Params, ccRecursive))
  2142. {
  2143. TMakeLocalFileListParams MakeFileListParam;
  2144. MakeFileListParam.FileList = LocalFileList.get();
  2145. MakeFileListParam.FileTimes = NULL;
  2146. MakeFileListParam.IncludeDirs = FLAGSET(ACommand.Params, ccApplyToDirectories);
  2147. MakeFileListParam.Recursive = true;
  2148. ProcessLocalDirectory(FileName, Terminal->MakeLocalFileList, &MakeFileListParam);
  2149. }
  2150. }
  2151. else
  2152. {
  2153. LocalFileList->Add(FileName);
  2154. }
  2155. }
  2156. UnicodeString RemotePath;
  2157. if (Terminal != NULL)
  2158. {
  2159. RemotePath = Terminal->CurrentDirectory;
  2160. }
  2161. if (FileListCommand)
  2162. {
  2163. UnicodeString FileList = MakeFileList(LocalFileList.get());
  2164. TLocalCustomCommand CustomCommand(
  2165. Data, RemotePath, DefaultDownloadTargetDirectory(),
  2166. L"", L"", FileList);
  2167. ExecuteProcessChecked(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  2168. }
  2169. else
  2170. {
  2171. TFileOperationProgressType Progress(&OperationProgress, &OperationFinished);
  2172. Progress.Start(foCustomCommand, osLocal, LocalFileList->Count);
  2173. DebugAssert(FProgressForm != NULL);
  2174. FProgressForm->ReadOnly = true;
  2175. try
  2176. {
  2177. for (int Index = 0; Index < LocalFileList->Count; Index++)
  2178. {
  2179. UnicodeString FileName = LocalFileList->Strings[Index];
  2180. Progress.SetFile(FileName);
  2181. TLocalCustomCommand CustomCommand(
  2182. Data, RemotePath, DefaultDownloadTargetDirectory(),
  2183. FileName, L"", L"");
  2184. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  2185. TOnceDoneOperation OnceDoneOperation;
  2186. Progress.Finish(FileName, true, OnceDoneOperation);
  2187. }
  2188. }
  2189. __finally
  2190. {
  2191. Progress.Stop();
  2192. }
  2193. }
  2194. }
  2195. //---------------------------------------------------------------------------
  2196. void __fastcall TCustomScpExplorerForm::LocalCustomCommand(TStrings * FileList,
  2197. const TCustomCommandType & ACommand, TStrings * ALocalFileList,
  2198. const TCustomCommandData & Data, const UnicodeString & CommandCommand)
  2199. {
  2200. UnicodeString RemotePath = (Terminal != NULL) ? Terminal->CurrentDirectory : UnicodeString();
  2201. TLocalCustomCommand LocalCustomCommand(Data, RemotePath, DefaultDownloadTargetDirectory());
  2202. TWinInteractiveCustomCommand InteractiveCustomCommand(
  2203. &LocalCustomCommand, ACommand.Name, ACommand.HomePage);
  2204. UnicodeString Command = InteractiveCustomCommand.Complete(CommandCommand, false);
  2205. bool FileListCommand = LocalCustomCommand.IsFileListCommand(Command);
  2206. bool LocalFileCommand = LocalCustomCommand.HasLocalFileName(Command);
  2207. Configuration->Usage->Inc(L"LocalCustomCommandRuns2");
  2208. std::unique_ptr<UnicodeString> POutput;
  2209. if (FLAGSET(ACommand.Params, ccShowResultsInMsgBox) ||
  2210. FLAGSET(ACommand.Params, ccCopyResults))
  2211. {
  2212. POutput.reset(new UnicodeString());
  2213. }
  2214. TValueRestorer<TOperationSide> ProgressSideRestorer(FProgressSide);
  2215. FProgressSide = FCurrentSide;
  2216. if (!LocalCustomCommand.IsFileCommand(Command))
  2217. {
  2218. ExecuteProcessChecked(LocalCustomCommand.Complete(Command, true), HelpKeyword, POutput.get());
  2219. }
  2220. // remote files?
  2221. else if (!IsSideLocalBrowser(FCurrentSide) || LocalFileCommand)
  2222. {
  2223. LocalCustomCommandPure(FileList, ACommand, Command, ALocalFileList, Data, LocalFileCommand, FileListCommand, POutput.get());
  2224. }
  2225. // local files
  2226. else
  2227. {
  2228. LocalCustomCommandWithLocalFiles(ACommand, Command, Data, FileListCommand, POutput.get());
  2229. }
  2230. if (POutput.get() != NULL)
  2231. {
  2232. // If the output is single-line, we do not want the trailing CRLF, as the CopyToClipboard(TStrings *) does
  2233. int P = POutput->Pos(sLineBreak);
  2234. if (P == POutput->Length() - static_cast<int>(strlen(sLineBreak)) + 1)
  2235. {
  2236. POutput->SetLength(P - 1);
  2237. }
  2238. // Copy even empty output to clipboard
  2239. if (FLAGSET(ACommand.Params, ccCopyResults))
  2240. {
  2241. CopyToClipboard(*POutput);
  2242. }
  2243. // But do not show an empty message box.
  2244. // This way the ShowResultsInMsgBox can be used to suppress a console window of commands with no output
  2245. if (FLAGSET(ACommand.Params, ccShowResultsInMsgBox) &&
  2246. !POutput->IsEmpty())
  2247. {
  2248. TClipboardHandler ClipboardHandler;
  2249. ClipboardHandler.Text = *POutput;
  2250. TMessageParams Params;
  2251. TQueryButtonAlias Aliases[1];
  2252. Aliases[0].Button = qaRetry;
  2253. Aliases[0].Alias = LoadStr(EDIT_COPY);
  2254. Aliases[0].OnSubmit = &ClipboardHandler.Copy;
  2255. Params.Aliases = Aliases;
  2256. Params.AliasesCount = LENOF(Aliases);
  2257. MessageDialog(*POutput, qtInformation, qaOK | qaRetry, HelpKeyword, &Params);
  2258. }
  2259. }
  2260. }
  2261. //---------------------------------------------------------------------------
  2262. void __fastcall TCustomScpExplorerForm::CustomCommand(TStrings * FileList,
  2263. const TCustomCommandType & ACommand, TStrings * ALocalFileList)
  2264. {
  2265. TCustomCommandData Data;
  2266. UnicodeString Site;
  2267. UnicodeString RemotePath;
  2268. if (HasActiveTerminal())
  2269. {
  2270. std::unique_ptr<TSessionData> SessionData(SessionDataForCode());
  2271. Data = TCustomCommandData(SessionData.get());
  2272. Site = Terminal->SessionData->SessionKey;
  2273. RemotePath = Terminal->CurrentDirectory;
  2274. }
  2275. UnicodeString HelpKeyword = ACommand.HomePage;
  2276. std::unique_ptr<TStrings> CustomCommandOptions(CloneStrings(WinConfiguration->CustomCommandOptions));
  2277. if (ACommand.AnyOptionWithFlag(TCustomCommandType::ofRun))
  2278. {
  2279. std::unique_ptr<TCustomCommand> CustomCommandForOptions;
  2280. if (FLAGCLEAR(ACommand.Params, ccLocal))
  2281. {
  2282. CustomCommandForOptions.reset(new TRemoteCustomCommand(Data, RemotePath));
  2283. }
  2284. else
  2285. {
  2286. CustomCommandForOptions.reset(new TLocalCustomCommand(Data, RemotePath, DefaultDownloadTargetDirectory()));
  2287. }
  2288. if (!DoCustomCommandOptionsDialog(
  2289. &ACommand, CustomCommandOptions.get(), NULL, TCustomCommandType::ofRun, CustomCommandForOptions.get(), Site, NULL))
  2290. {
  2291. Abort();
  2292. }
  2293. }
  2294. UnicodeString CommandCommand = ACommand.GetCommandWithExpandedOptions(CustomCommandOptions.get(), Site);
  2295. if (FLAGCLEAR(ACommand.Params, ccLocal))
  2296. {
  2297. RemoteCustomCommand(FileList, ACommand, Data, CommandCommand);
  2298. }
  2299. else
  2300. {
  2301. LocalCustomCommand(FileList, ACommand, ALocalFileList, Data, CommandCommand);
  2302. }
  2303. }
  2304. //---------------------------------------------------------------------------
  2305. void __fastcall TCustomScpExplorerForm::BothCustomCommand(
  2306. const TCustomCommandType & Command)
  2307. {
  2308. DebugAssert(FCustomCommandLocalFileList != NULL);
  2309. DebugAssert(FCustomCommandRemoteFileList != NULL);
  2310. DebugAssert(FCustomCommandLocalFileList->Count == FCustomCommandRemoteFileList->Count);
  2311. TStrings * LocalFileList = new TStringList();
  2312. TStrings * RemoteFileList = new TStringList();
  2313. try
  2314. {
  2315. for (int Index = 0; Index < FCustomCommandLocalFileList->Count; Index++)
  2316. {
  2317. LocalFileList->Clear();
  2318. LocalFileList->AddObject(
  2319. FCustomCommandLocalFileList->Strings[Index],
  2320. FCustomCommandLocalFileList->Objects[Index]);
  2321. RemoteFileList->Clear();
  2322. RemoteFileList->AddObject(
  2323. FCustomCommandRemoteFileList->Strings[Index],
  2324. FCustomCommandRemoteFileList->Objects[Index]);
  2325. CustomCommand(RemoteFileList, Command, LocalFileList);
  2326. }
  2327. }
  2328. __finally
  2329. {
  2330. delete LocalFileList;
  2331. delete RemoteFileList;
  2332. }
  2333. }
  2334. //---------------------------------------------------------------------------
  2335. void __fastcall TCustomScpExplorerForm::CustomCommandMenu(
  2336. TAction * Action, TStrings * LocalFileList, TStrings * RemoteFileList)
  2337. {
  2338. delete FCustomCommandLocalFileList;
  2339. delete FCustomCommandRemoteFileList;
  2340. // takeover ownership,
  2341. // the lists must survive the MenuPopup as OnClick occurs only after it exits
  2342. FCustomCommandLocalFileList = LocalFileList;
  2343. FCustomCommandRemoteFileList = RemoteFileList;
  2344. TButton * Button = dynamic_cast<TButton *>(Action->ActionComponent);
  2345. if (Button != NULL)
  2346. {
  2347. FCustomCommandMenu->Items->Clear();
  2348. NonVisualDataModule->CreateCustomCommandsMenu(FCustomCommandMenu->Items, false, false, ccltBoth, NULL);
  2349. MenuPopup(FCustomCommandMenu, Button);
  2350. }
  2351. else
  2352. {
  2353. NonVisualDataModule->CreateCustomCommandsMenu(Action, false, ccltBoth);
  2354. }
  2355. }
  2356. //---------------------------------------------------------------------------
  2357. void __fastcall TCustomScpExplorerForm::TerminalCaptureLog(
  2358. const UnicodeString & AddedLine, TCaptureOutputType OutputType)
  2359. {
  2360. DebugAssert(FCapturedLog != NULL);
  2361. if ((OutputType == cotOutput) || (OutputType == cotError))
  2362. {
  2363. FCapturedLog->Add(AddedLine);
  2364. }
  2365. }
  2366. //---------------------------------------------------------------------------
  2367. bool __fastcall TCustomScpExplorerForm::IsFileControl(TObject * Control,
  2368. TOperationSide Side)
  2369. {
  2370. return
  2371. (Side == osOther) &&
  2372. ((Control == DirView(osOther)) || (Control == DriveView(osOther)));
  2373. }
  2374. //---------------------------------------------------------------------------
  2375. void __fastcall TCustomScpExplorerForm::DirViewContextPopupDefaultItem(
  2376. TOperationSide Side, TTBXCustomItem * Item, TDoubleClickAction DoubleClickAction)
  2377. {
  2378. TTBItemOptions O;
  2379. O = Item->Options;
  2380. TCustomDirView * DView = DirView(Side);
  2381. if ((DView->ItemFocused != NULL) &&
  2382. (WinConfiguration->DoubleClickAction == DoubleClickAction) &&
  2383. // when resolving links is disabled, default action is to enter the directory,
  2384. // no matter what DoubleClickAction is configured to
  2385. (IsSideLocalBrowser(Side) || Terminal->ResolvingSymlinks || Terminal->IsEncryptingFiles()) &&
  2386. // Can only Edit files, but can Open/Copy even directories
  2387. ((DoubleClickAction != dcaEdit) ||
  2388. !DView->ItemIsDirectory(DView->ItemFocused)))
  2389. {
  2390. Item->Options = O << tboDefault;
  2391. }
  2392. else
  2393. {
  2394. Item->Options = O >> tboDefault;
  2395. }
  2396. }
  2397. //---------------------------------------------------------------------------
  2398. void __fastcall TCustomScpExplorerForm::DirViewContextPopup(
  2399. TOperationSide Side, Byte PopupComponent, const TPoint &MousePos)
  2400. {
  2401. TCustomDirView * DView = DirView(Side);
  2402. TListItem * Item = DView->ItemFocused;
  2403. if ((DView->GetItemAt(MousePos.x, MousePos.y) == Item) &&
  2404. EnableFocusedOperation[Side])
  2405. {
  2406. TPoint ClientPoint;
  2407. ClientPoint = ((MousePos.x < 0) && (MousePos.y < 0)) ?
  2408. TPoint(0, 0) : MousePos;
  2409. FLastContextPopupScreenPoint = DView->ClientToScreen(ClientPoint);
  2410. reinterpret_cast<TPopupMenu*>(GetComponent(PopupComponent))->Popup(
  2411. FLastContextPopupScreenPoint.x, FLastContextPopupScreenPoint.y);
  2412. }
  2413. }
  2414. //---------------------------------------------------------------------------
  2415. void __fastcall TCustomScpExplorerForm::RemoteDirViewContextPopup(
  2416. TObject * /*Sender*/, const TPoint &MousePos, bool &Handled)
  2417. {
  2418. DirViewContextPopupDefaultItem(osRemote, NonVisualDataModule->RemoteOpenMenuItem, dcaOpen);
  2419. DirViewContextPopupDefaultItem(osRemote, NonVisualDataModule->RemoteEditMenuItem, dcaEdit);
  2420. DirViewContextPopupDefaultItem(osRemote, NonVisualDataModule->RemoteCopyMenuItem, dcaCopy);
  2421. DirViewContextPopup(osRemote, fcRemotePopup, MousePos);
  2422. Handled = true;
  2423. }
  2424. //---------------------------------------------------------------------------
  2425. void __fastcall TCustomScpExplorerForm::ReloadLocalDirectory(const UnicodeString Directory)
  2426. {
  2427. }
  2428. //---------------------------------------------------------------------------
  2429. void __fastcall TCustomScpExplorerForm::BatchStart(void *& /*Storage*/)
  2430. {
  2431. DebugAssert(FErrorList == NULL);
  2432. if (WinConfiguration->ContinueOnError)
  2433. {
  2434. FErrorList = new TStringList();
  2435. Configuration->Usage->Inc(L"ContinuationsOnError");
  2436. }
  2437. NonVisualDataModule->StartBusy();
  2438. }
  2439. //---------------------------------------------------------------------------
  2440. void __fastcall TCustomScpExplorerForm::BatchEnd(void * /*Storage*/)
  2441. {
  2442. NonVisualDataModule->EndBusy();
  2443. if (FErrorList)
  2444. {
  2445. HandleErrorList(FErrorList);
  2446. }
  2447. }
  2448. //---------------------------------------------------------------------------
  2449. void __fastcall TCustomScpExplorerForm::UpdateCopyParamCounters(
  2450. const TCopyParamType & CopyParam)
  2451. {
  2452. if (!CopyParam.IncludeFileMask.Masks.IsEmpty())
  2453. {
  2454. Configuration->Usage->Inc(L"FileMaskUses");
  2455. }
  2456. if (IsEffectiveFileNameMask(CopyParam.FileMask))
  2457. {
  2458. Configuration->Usage->Inc(L"OperationMaskUses");
  2459. }
  2460. }
  2461. //---------------------------------------------------------------------------
  2462. bool __fastcall TCustomScpExplorerForm::ExecuteCopyMoveFileOperation(
  2463. TFileOperation Operation, TOperationSide Side, TStrings * FileList, bool NoConfirmation, void * AParam)
  2464. {
  2465. DebugAssert(!IsLocalBrowserMode());
  2466. TTransferDirection Direction = (Side == osLocal ? tdToRemote : tdToLocal);
  2467. TTransferType Type = (Operation == foCopy ? ttCopy : ttMove);
  2468. TTransferOperationParam DefaultParam;
  2469. TTransferOperationParam & Param = (AParam != NULL) ? *static_cast<TTransferOperationParam *>(AParam) : DefaultParam;
  2470. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  2471. switch (Param.Queue)
  2472. {
  2473. case asOn:
  2474. CopyParam.Queue = true;
  2475. break;
  2476. case asOff:
  2477. CopyParam.Queue = false;
  2478. break;
  2479. case asAuto:
  2480. default:
  2481. // keep default
  2482. break;
  2483. }
  2484. bool Result =
  2485. CopyParamDialog(Direction, Type, Param.Temp, FileList, Param.TargetDirectory, CopyParam, !NoConfirmation, Param.DragDrop, Param.Options);
  2486. if (Result)
  2487. {
  2488. DebugAssert(Terminal);
  2489. bool SelectionRestored = false;
  2490. TCustomDirView * DView = NULL;
  2491. if (HasDirView[Side])
  2492. {
  2493. DView = DirView(Side);
  2494. DView->SaveSelection();
  2495. DView->SaveSelectedNames();
  2496. }
  2497. UpdateCopyParamCounters(CopyParam);
  2498. std::unique_ptr<TStringList> TransferResumeList(new TStringList());
  2499. DebugAssert(FTransferResumeList == NULL);
  2500. FTransferResumeList = Terminal->IsCapable[fcMoveToQueue] ? TransferResumeList.get() : NULL;
  2501. FMoveToQueue = false;
  2502. int Params = FLAGMASK(Operation == foMove, cpDelete);
  2503. try
  2504. {
  2505. TStrings * PermanentFileList;
  2506. std::unique_ptr<TStrings> PermanentFileListOwner;
  2507. try
  2508. {
  2509. if (Side == osLocal)
  2510. {
  2511. PermanentFileList = FileList;
  2512. Params |= FLAGMASK(Param.Temp, cpTemporary);
  2513. Terminal->CopyToRemote(FileList, Param.TargetDirectory, &CopyParam, Params, NULL);
  2514. if (Operation == foMove)
  2515. {
  2516. ReloadLocalDirectory();
  2517. if (DView != NULL)
  2518. {
  2519. DView->RestoreSelection();
  2520. }
  2521. SelectionRestored = true;
  2522. }
  2523. }
  2524. else
  2525. {
  2526. // Clone the file list as it may refer to current directory files,
  2527. // which get destroyed, when the source directory is reloaded after foMove operation.
  2528. // We should actually clone the file list for whole ExecuteFileOperation to protect against reloads.
  2529. // But for a hotfix, we are not going to do such a big change.
  2530. PermanentFileListOwner.reset(TRemoteFileList::CloneStrings(FileList));
  2531. PermanentFileList = PermanentFileListOwner.get();
  2532. try
  2533. {
  2534. Terminal->CopyToLocal(FileList, Param.TargetDirectory, &CopyParam, Params, NULL);
  2535. }
  2536. __finally
  2537. {
  2538. if (Operation == foMove)
  2539. {
  2540. if (DView != NULL)
  2541. {
  2542. DView->RestoreSelection();
  2543. }
  2544. SelectionRestored = true;
  2545. }
  2546. ReloadLocalDirectory(Param.TargetDirectory);
  2547. }
  2548. }
  2549. }
  2550. catch (EAbort &)
  2551. {
  2552. if (FMoveToQueue)
  2553. {
  2554. Params |= (CopyParam.QueueNoConfirmation ? cpNoConfirmation : 0);
  2555. DebugAssert(CopyParam.TransferSkipList == NULL);
  2556. DebugAssert(CopyParam.TransferResumeFile.IsEmpty());
  2557. if (TransferResumeList->Count > 0)
  2558. {
  2559. CopyParam.TransferResumeFile = TransferResumeList->Strings[TransferResumeList->Count - 1];
  2560. TransferResumeList->Delete(TransferResumeList->Count - 1);
  2561. }
  2562. CopyParam.TransferSkipList = TransferResumeList.release();
  2563. // not really needed, just to keep it consistent with TransferResumeList
  2564. FTransferResumeList = NULL;
  2565. FMoveToQueue = false;
  2566. Configuration->Usage->Inc("MovesToBackground");
  2567. AddQueueItem(Queue, Direction, PermanentFileList, Param.TargetDirectory, CopyParam, Params);
  2568. ClearTransferSourceSelection(Direction);
  2569. }
  2570. throw;
  2571. }
  2572. }
  2573. __finally
  2574. {
  2575. if (!SelectionRestored && (DView != NULL))
  2576. {
  2577. DView->DiscardSavedSelection();
  2578. }
  2579. FTransferResumeList = NULL;
  2580. }
  2581. }
  2582. return Result;
  2583. }
  2584. //---------------------------------------------------------------------------
  2585. bool __fastcall TCustomScpExplorerForm::ExecuteDeleteFileOperation(
  2586. TOperationSide Side, TStrings * FileList, void * Param)
  2587. {
  2588. DebugAssert(FileList->Count);
  2589. // We deliberately do not toggle alternative flag (Param), but use OR,
  2590. // because the Param is set only when command is invoked using Shift-Del/F8 keyboard
  2591. // shortcut of CurrentDeleteAlternativeAction
  2592. bool Alternative =
  2593. bool(Param) || UseAlternativeFunction();
  2594. bool Recycle;
  2595. if (IsSideLocalBrowser(Side))
  2596. {
  2597. Recycle = (WinConfiguration->DeleteToRecycleBin != Alternative);
  2598. }
  2599. else
  2600. {
  2601. Recycle =
  2602. (Terminal->SessionData->DeleteToRecycleBin != Alternative) &&
  2603. !Terminal->SessionData->RecycleBinPath.IsEmpty() &&
  2604. !Terminal->IsRecycledFile(FileList->Strings[0]);
  2605. }
  2606. bool Result = !(Recycle ? WinConfiguration->ConfirmRecycling : WinConfiguration->ConfirmDeleting);
  2607. if (!Result)
  2608. {
  2609. UnicodeString Query;
  2610. if (FileList->Count == 1)
  2611. {
  2612. if (IsSideLocalBrowser(Side))
  2613. {
  2614. Query = ExtractFileName(FileList->Strings[0]);
  2615. }
  2616. else
  2617. {
  2618. Query = UnixExtractFileName(FileList->Strings[0]);
  2619. }
  2620. Query = FMTLOAD((Recycle ? CONFIRM_RECYCLE_FILE : CONFIRM_DELETE_FILE), (Query));
  2621. }
  2622. else
  2623. {
  2624. Query = FMTLOAD((Recycle ? CONFIRM_RECYCLE_FILES : CONFIRM_DELETE_FILES), (FileList->Count));
  2625. }
  2626. TMessageParams Params(mpNeverAskAgainCheck);
  2627. Params.ImageName = L"Delete file";
  2628. unsigned int Answer =
  2629. MessageDialog(MainInstructions(Query), qtConfirmation, qaOK | qaCancel, HELP_DELETE_FILE, &Params);
  2630. if (Answer == qaNeverAskAgain)
  2631. {
  2632. Result = true;
  2633. if (Recycle)
  2634. {
  2635. WinConfiguration->ConfirmRecycling = false;
  2636. }
  2637. else
  2638. {
  2639. WinConfiguration->ConfirmDeleting = false;
  2640. }
  2641. }
  2642. else
  2643. {
  2644. Result = (Answer == qaOK);
  2645. }
  2646. }
  2647. if (Result)
  2648. {
  2649. DeleteFiles(Side, FileList, FLAGMASK(Alternative, dfAlternative));
  2650. }
  2651. return Result;
  2652. }
  2653. //---------------------------------------------------------------------------
  2654. bool __fastcall TCustomScpExplorerForm::ExecuteFileOperation(TFileOperation Operation,
  2655. TOperationSide Side, TStrings * FileList, bool NoConfirmation, void * Param)
  2656. {
  2657. TAutoBatch AutoBatch(this);
  2658. bool Result;
  2659. if ((Operation == foCopy) || (Operation == foMove))
  2660. {
  2661. Result = ExecuteCopyMoveFileOperation(Operation, Side, FileList, NoConfirmation, Param);
  2662. }
  2663. else if (Operation == foRename)
  2664. {
  2665. DebugAssert(DirView(Side)->ItemFocused);
  2666. DirView(Side)->ItemFocused->EditCaption();
  2667. Result = true;
  2668. }
  2669. else if (Operation == foDelete)
  2670. {
  2671. Result = ExecuteDeleteFileOperation(Side, FileList, Param);
  2672. }
  2673. else if (Operation == foSetProperties)
  2674. {
  2675. DirView(osRemote)->SaveSelectedNames();
  2676. Result = SetProperties(Side, FileList);
  2677. }
  2678. else if (Operation == foCustomCommand)
  2679. {
  2680. DebugAssert(Param);
  2681. DebugAssert(Side == osRemote); // Side carries no meaning for foCustomCommand
  2682. DirView(osRemote)->SaveSelectedNames();
  2683. const TCustomCommandType * Command = static_cast<const TCustomCommandType*>(Param);
  2684. CustomCommand(FileList, *Command, NULL);
  2685. Result = true;
  2686. }
  2687. else if ((Operation == foRemoteMove) || (Operation == foRemoteCopy))
  2688. {
  2689. DebugAssert(!IsSideLocalBrowser(Side));
  2690. Result = RemoteTransferFiles(FileList, NoConfirmation,
  2691. (Operation == foRemoteMove), reinterpret_cast<TManagedTerminal *>(Param));
  2692. }
  2693. else if (Operation == foLock)
  2694. {
  2695. DebugAssert(!IsSideLocalBrowser(Side));
  2696. LockFiles(FileList, true);
  2697. Result = true;
  2698. }
  2699. else if (Operation == foUnlock)
  2700. {
  2701. DebugAssert(!IsSideLocalBrowser(Side));
  2702. LockFiles(FileList, false);
  2703. Result = true;
  2704. }
  2705. else
  2706. {
  2707. DebugFail();
  2708. }
  2709. return Result;
  2710. }
  2711. //---------------------------------------------------------------------------
  2712. TOperationSide __fastcall TCustomScpExplorerForm::GetSide(TOperationSide Side)
  2713. {
  2714. if (Side == osCurrent)
  2715. {
  2716. Side = FCurrentSide;
  2717. }
  2718. return Side;
  2719. }
  2720. //---------------------------------------------------------------------------
  2721. bool __fastcall TCustomScpExplorerForm::ExecuteFileOperation(TFileOperation Operation,
  2722. TOperationSide Side, bool OnFocused, bool NoConfirmation, void * Param)
  2723. {
  2724. Side = GetSide(Side);
  2725. bool Result;
  2726. TStrings * FileList = DirView(Side)->CreateFileList(OnFocused, IsSideLocalBrowser(Side), NULL);
  2727. try
  2728. {
  2729. Result = ExecuteFileOperation(Operation, Side, FileList, NoConfirmation, Param);
  2730. }
  2731. __finally
  2732. {
  2733. delete FileList;
  2734. }
  2735. return Result;
  2736. }
  2737. //---------------------------------------------------------------------------
  2738. void __fastcall TCustomScpExplorerForm::ExecuteFileOperationCommand(
  2739. TFileOperation Operation, TOperationSide Side, bool OnFocused,
  2740. bool NoConfirmation, void * Param)
  2741. {
  2742. if (ExecuteFileOperation(Operation, Side, OnFocused, NoConfirmation, Param))
  2743. {
  2744. if ((Operation == foCopy) || (Operation == foMove))
  2745. {
  2746. DebugAssert(!IsLocalBrowserMode());
  2747. if (GetSide(Side) == osLocal)
  2748. {
  2749. Configuration->Usage->Inc(L"UploadsCommand");
  2750. }
  2751. else
  2752. {
  2753. Configuration->Usage->Inc(L"DownloadsCommand");
  2754. }
  2755. }
  2756. }
  2757. }
  2758. //---------------------------------------------------------------------------
  2759. void __fastcall TCustomScpExplorerForm::ExecuteCopyOperationCommand(
  2760. TOperationSide Side, bool OnFocused, unsigned int Flags)
  2761. {
  2762. TTransferOperationParam Param;
  2763. if ((WinConfiguration->Interface != ifCommander) ||
  2764. WinConfiguration->ScpCommander.ExplorerKeyboardShortcuts)
  2765. {
  2766. Flags &= ~cocShortCutHint;
  2767. }
  2768. TCustomDirView * DView = DirView(Side);
  2769. Param.Options =
  2770. FLAGMASK(FLAGSET(Flags, cocShortCutHint), coShortCutHint) |
  2771. FLAGMASK(SelectedAllFilesInDirView(DView), coAllFiles);
  2772. if (FLAGSET(Flags, cocQueue))
  2773. {
  2774. Param.Queue = asOn;
  2775. }
  2776. else if (FLAGSET(Flags, cocNonQueue))
  2777. {
  2778. Param.Queue = asOff;
  2779. }
  2780. ExecuteFileOperationCommand(foCopy, Side, OnFocused, false, &Param);
  2781. }
  2782. //---------------------------------------------------------------------------
  2783. void __fastcall TCustomScpExplorerForm::HandleErrorList(TStringList *& ErrorList)
  2784. {
  2785. int Count = ErrorList->Count;
  2786. try
  2787. {
  2788. if (ErrorList->Count)
  2789. {
  2790. UnicodeString Message = MainInstructions(FMTLOAD(ERROR_LIST_COUNT, (ErrorList->Count)));
  2791. if (MessageDialog(Message, qtError,
  2792. qaOK | qaCancel, HELP_NONE) == qaOK)
  2793. {
  2794. unsigned int Answer;
  2795. int Index = 0;
  2796. do
  2797. {
  2798. DebugAssert(Index >= 0 && Index < Count);
  2799. TQueryButtonAlias Aliases[2];
  2800. Aliases[0].Button = qaYes;
  2801. Aliases[0].Alias = LoadStr(PREV_BUTTON);
  2802. Aliases[1].Button = qaNo;
  2803. Aliases[1].Alias = LoadStr(NEXT_BUTTON);
  2804. TMessageParams Params;
  2805. Params.Aliases = Aliases;
  2806. Params.AliasesCount = LENOF(Aliases);
  2807. int No = Index + 1;
  2808. UnicodeString Message = ErrorList->Strings[Index];
  2809. UnicodeString AMainInstructions;
  2810. UnicodeString Details;
  2811. if (ExtractMainInstructions(Message, AMainInstructions))
  2812. {
  2813. Details = Message;
  2814. Message = AMainInstructions;
  2815. }
  2816. Message = MainInstructions(FMTLOAD(ERROR_LIST_NUMBER, (No, Count, Message))) + Details;
  2817. Answer = MoreMessageDialog(
  2818. Message,
  2819. dynamic_cast<TStrings *>(ErrorList->Objects[Index]), qtError,
  2820. (Index ? qaYes : 0) | (Index < Count - 1 ? qaNo : 0) |
  2821. qaOK, HELP_NONE, &Params);
  2822. if (Answer == qaNo)
  2823. {
  2824. Index++;
  2825. }
  2826. if (Answer == qaYes)
  2827. {
  2828. Index--;
  2829. }
  2830. }
  2831. while (Answer != qaOK);
  2832. }
  2833. }
  2834. }
  2835. __finally
  2836. {
  2837. TStrings * List = ErrorList;
  2838. ErrorList = NULL;
  2839. for (int i = 0; i < Count; i++)
  2840. {
  2841. delete List->Objects[i];
  2842. }
  2843. delete List;
  2844. }
  2845. }
  2846. //---------------------------------------------------------------------------
  2847. void __fastcall TCustomScpExplorerForm::ExecuteRemoteFile(
  2848. const UnicodeString & FullFileName, TRemoteFile * File, TExecuteFileBy ExecuteFileBy)
  2849. {
  2850. // needed for checking filemasks, as there's no directory object
  2851. // associated with the file object
  2852. File->FullFileName = FullFileName;
  2853. TFileMasks::TParams MaskParams;
  2854. MaskParams.Size = File->Size;
  2855. MaskParams.Modification = File->Modification;
  2856. ExecuteFile(osRemote, ExecuteFileBy, NULL, FullFileName, File, MaskParams);
  2857. }
  2858. //---------------------------------------------------------------------------
  2859. void __fastcall TCustomScpExplorerForm::EditNew(TOperationSide Side)
  2860. {
  2861. DebugAssert(!WinConfiguration->DisableOpenEdit);
  2862. Side = GetSide(Side);
  2863. TCustomDirView * DView = DirView(Side);
  2864. TListItem * FocusedItem = DView->ItemFocused;
  2865. UnicodeString Name;
  2866. if ((FocusedItem != NULL) && !DView->ItemIsDirectory(FocusedItem))
  2867. {
  2868. Name = DView->ItemFileName(FocusedItem);
  2869. }
  2870. else
  2871. {
  2872. Name = LoadStr(NEW_FILE);
  2873. }
  2874. UnicodeString Names = Name;
  2875. std::unique_ptr<TStrings> History(CloneStrings(CustomWinConfiguration->History[L"EditFile"]));
  2876. if (InputDialog(LoadStr(EDIT_FILE_CAPTION), LoadStr(EDIT_FILE_PROMPT), Names,
  2877. HELP_EDIT_NEW, History.get(), true, NULL, true, 400))
  2878. {
  2879. while (!Names.IsEmpty())
  2880. {
  2881. Name = CutToChar(Names, FileMasksDelimiters[1], false);
  2882. CustomWinConfiguration->History[L"EditFile"] = History.get();
  2883. UnicodeString TargetFileName;
  2884. UnicodeString LocalFileName;
  2885. UnicodeString RootTempDir;
  2886. UnicodeString TempDir;
  2887. UnicodeString RemoteDirectory;
  2888. bool ExistingFile = false;
  2889. if (!IsSideLocalBrowser(Side))
  2890. {
  2891. Name = AbsolutePath(Terminal->CurrentDirectory, Name);
  2892. TRemoteFile * File = NULL;
  2893. if (Terminal->FileExists(Name, &File) &&
  2894. !File->IsDirectory)
  2895. {
  2896. try
  2897. {
  2898. ExecuteRemoteFile(Name, File, efDefaultEditor);
  2899. ExistingFile = true;
  2900. }
  2901. __finally
  2902. {
  2903. delete File;
  2904. }
  2905. }
  2906. if (!ExistingFile)
  2907. {
  2908. RemoteDirectory = UnixExtractFilePath(Name);
  2909. TemporaryDirectoryForRemoteFiles(
  2910. RemoteDirectory, GUIConfiguration->CurrentCopyParam, TempDir, RootTempDir);
  2911. TargetFileName = UnixExtractFileName(Name);
  2912. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  2913. LocalFileName = TempDir +
  2914. // We probably do not want to trim the VMS version here
  2915. Terminal->ChangeFileName(&CopyParam, TargetFileName, osRemote, false);
  2916. }
  2917. }
  2918. else
  2919. {
  2920. if (ExtractFilePath(Name).IsEmpty())
  2921. {
  2922. LocalFileName = IncludeTrailingBackslash(DView->PathName) + Name;
  2923. }
  2924. else
  2925. {
  2926. LocalFileName = ExpandFileName(Name);
  2927. }
  2928. TargetFileName = ExtractFileName(Name);
  2929. }
  2930. if (!ExistingFile)
  2931. {
  2932. bool NewFile = !FileExists(ApiPath(LocalFileName));
  2933. if (NewFile)
  2934. {
  2935. int File = FileCreate(ApiPath(LocalFileName));
  2936. if (File < 0)
  2937. {
  2938. if (!RootTempDir.IsEmpty())
  2939. {
  2940. RecursiveDeleteFile(ExcludeTrailingBackslash(RootTempDir), false);
  2941. }
  2942. throw Exception(FMTLOAD(CREATE_FILE_ERROR, (LocalFileName)));
  2943. }
  2944. else
  2945. {
  2946. FileClose(File);
  2947. }
  2948. }
  2949. TExecuteFileBy ExecuteFileBy = efDefaultEditor;
  2950. const TEditorData * ExternalEditor = NULL;
  2951. TFileMasks::TParams MaskParams; // size not known
  2952. ExecuteFileNormalize(ExecuteFileBy, ExternalEditor, TargetFileName,
  2953. false, MaskParams);
  2954. CustomExecuteFile(Side, ExecuteFileBy, LocalFileName, TargetFileName,
  2955. ExternalEditor, RootTempDir, RemoteDirectory, NewFile);
  2956. }
  2957. }
  2958. }
  2959. }
  2960. //---------------------------------------------------------------------------
  2961. bool __fastcall TCustomScpExplorerForm::RemoteExecuteForceText(
  2962. TExecuteFileBy ExecuteFileBy, const TEditorData * ExternalEditor)
  2963. {
  2964. DebugAssert((ExecuteFileBy == efExternalEditor) ==
  2965. ((ExternalEditor != NULL) && (ExternalEditor->Editor == edExternal)));
  2966. DebugAssert(ExecuteFileBy != efDefaultEditor);
  2967. return
  2968. ((ExecuteFileBy == efInternalEditor)) ||
  2969. ((ExecuteFileBy == efExternalEditor) && ExternalEditor->ExternalEditorText);
  2970. }
  2971. //---------------------------------------------------------------------------
  2972. void __fastcall TCustomScpExplorerForm::CustomExecuteFile(TOperationSide Side,
  2973. TExecuteFileBy ExecuteFileBy, UnicodeString FileName, UnicodeString OriginalFileName,
  2974. const TEditorData * ExternalEditor, UnicodeString LocalRootDirectory,
  2975. UnicodeString RemoteDirectory, bool NewFile)
  2976. {
  2977. DebugAssert(!WinConfiguration->DisableOpenEdit);
  2978. DebugAssert((ExecuteFileBy == efExternalEditor) ==
  2979. ((ExternalEditor != NULL) && (ExternalEditor->Editor == edExternal)));
  2980. DebugAssert(ExecuteFileBy != efDefaultEditor);
  2981. Side = GetSide(Side);
  2982. std::unique_ptr<TEditedFileData> Data(new TEditedFileData);
  2983. if (!IsSideLocalBrowser(Side))
  2984. {
  2985. Data->Terminal = Terminal;
  2986. Data->Queue = Queue;
  2987. Data->SessionData = CloneCurrentSessionData();
  2988. Data->ForceText = RemoteExecuteForceText(ExecuteFileBy, ExternalEditor);
  2989. Data->RemoteDirectory = RemoteDirectory;
  2990. Data->SessionName = Terminal->SessionData->SessionName;
  2991. Data->LocalRootDirectory = LocalRootDirectory;
  2992. Data->OriginalFileName = OriginalFileName;
  2993. Data->Command = L""; // will be changed later for external editor
  2994. }
  2995. if (ExecuteFileBy == efInternalEditor)
  2996. {
  2997. if (!IsSideLocalBrowser(Side))
  2998. {
  2999. UnicodeString RemotePath = UnixCombinePaths(RemoteDirectory, OriginalFileName);
  3000. UnicodeString Caption = RemotePath + TitleSeparator + Terminal->SessionData->SessionName;
  3001. TForm * Editor;
  3002. try
  3003. {
  3004. TNotifyEvent OnFileChanged = NULL;
  3005. TNotifyEvent OnSaveAll = NULL;
  3006. TAnyModifiedEvent OnAnyModified = NULL;
  3007. // Edited files are uploaded in background queue, what we do not support with encrypted files
  3008. if (Terminal->IsCapable[fcBackgroundTransfers])
  3009. {
  3010. OnFileChanged = FEditorManager->FileChanged;
  3011. OnSaveAll = SaveAllInternalEditors;
  3012. OnAnyModified = AnyInternalEditorModified;
  3013. }
  3014. Editor = ShowEditorForm(FileName, this, OnFileChanged,
  3015. FEditorManager->FileReload, FEditorManager->FileClosed,
  3016. OnSaveAll, OnAnyModified,
  3017. Caption, FStandaloneEditing, SessionColor, Terminal->SessionData->InternalEditorEncoding, NewFile);
  3018. }
  3019. catch(...)
  3020. {
  3021. if (!LocalRootDirectory.IsEmpty())
  3022. {
  3023. RecursiveDeleteFile(ExcludeTrailingBackslash(LocalRootDirectory), false);
  3024. }
  3025. throw;
  3026. }
  3027. FEditorManager->AddFileInternal(FileName, Data.release(), Editor);
  3028. }
  3029. else
  3030. {
  3031. DebugAssert(!FStandaloneEditing);
  3032. TForm * Editor =
  3033. ShowEditorForm(FileName, this, NULL, NULL, LocalEditorClosed,
  3034. SaveAllInternalEditors, AnyInternalEditorModified,
  3035. L"", false, SessionColor, -1, NewFile);
  3036. FLocalEditors->Add(Editor);
  3037. }
  3038. }
  3039. else
  3040. {
  3041. HANDLE Process;
  3042. if (ExecuteFileBy == efExternalEditor)
  3043. {
  3044. UnicodeString Program, Params, Dir;
  3045. Data->Command = ExternalEditor->ExternalEditor;
  3046. ReformatFileNameCommand(Data->Command);
  3047. SplitCommand(Data->Command, Program, Params, Dir);
  3048. Params = ExpandFileNameCommand(Params, FileName);
  3049. Program = ExpandEnvironmentVariables(Program);
  3050. if (!ExecuteShell(Program, Params, Process))
  3051. {
  3052. throw EOSExtException(FMTLOAD(EDITOR_ERROR, (Program)));
  3053. }
  3054. }
  3055. else
  3056. {
  3057. DebugAssert(!IsSideLocalBrowser(Side));
  3058. if (!ExecuteShell(FileName, L"", Process))
  3059. {
  3060. throw EOSExtException(FMTLOAD(EXECUTE_FILE_ERROR, (FileName)));
  3061. }
  3062. }
  3063. if (IsSideLocalBrowser(Side) ||
  3064. ((ExecuteFileBy == efShell) &&
  3065. !WinConfiguration->Editor.SDIShellEditor) ||
  3066. ((ExecuteFileBy == efExternalEditor) &&
  3067. !ExternalEditor->SDIExternalEditor))
  3068. {
  3069. // no need for handle
  3070. if (Process != NULL)
  3071. {
  3072. DebugCheck(CloseHandle(Process));
  3073. }
  3074. Process = INVALID_HANDLE_VALUE;
  3075. }
  3076. else
  3077. {
  3078. if (Process == NULL)
  3079. {
  3080. throw ExtException(LoadStr(OPEN_FILE_NO_PROCESS2), L"", HELP_OPEN_FILE_NO_PROCESS);
  3081. }
  3082. }
  3083. if (!IsSideLocalBrowser(Side))
  3084. {
  3085. FEditorManager->AddFileExternal(FileName, Data.release(), Process);
  3086. }
  3087. }
  3088. }
  3089. //---------------------------------------------------------------------------
  3090. void __fastcall TCustomScpExplorerForm::SaveInternalEditor(
  3091. const UnicodeString /*FileName*/, TEditedFileData * /*Data*/, TObject * Token,
  3092. void * /*Arg*/)
  3093. {
  3094. if (Token != NULL)
  3095. {
  3096. EditorFormFileSave(static_cast<TForm *>(Token));
  3097. }
  3098. }
  3099. //---------------------------------------------------------------------------
  3100. void __fastcall TCustomScpExplorerForm::SaveAllInternalEditors(TObject * /*Sender*/)
  3101. {
  3102. for (int Index = 0; Index < FLocalEditors->Count; Index++)
  3103. {
  3104. EditorFormFileSave(static_cast<TForm *>(FLocalEditors->Items[Index]));
  3105. }
  3106. FEditorManager->ProcessFiles(SaveInternalEditor, NULL);
  3107. }
  3108. //---------------------------------------------------------------------------
  3109. void __fastcall TCustomScpExplorerForm::InternalEditorModified(
  3110. const UnicodeString /*FileName*/, TEditedFileData * /*Data*/, TObject * Token,
  3111. void * Arg)
  3112. {
  3113. if ((Token != NULL) &&
  3114. IsEditorFormModified(static_cast<TForm *>(Token)))
  3115. {
  3116. *static_cast<bool *>(Arg) = true;
  3117. }
  3118. }
  3119. //---------------------------------------------------------------------------
  3120. void __fastcall TCustomScpExplorerForm::AnyInternalEditorModified(
  3121. TObject * /*Sender*/, bool & Modified)
  3122. {
  3123. for (int Index = 0; !Modified && (Index < FLocalEditors->Count); Index++)
  3124. {
  3125. if (IsEditorFormModified(static_cast<TForm *>(FLocalEditors->Items[Index])))
  3126. {
  3127. Modified = true;
  3128. }
  3129. }
  3130. if (!Modified)
  3131. {
  3132. FEditorManager->ProcessFiles(InternalEditorModified, &Modified);
  3133. }
  3134. }
  3135. //---------------------------------------------------------------------------
  3136. void __fastcall TCustomScpExplorerForm::LocalEditorClosed(TObject * Sender, bool /*Forced*/)
  3137. {
  3138. DebugCheck(FLocalEditors->Extract(Sender) >= 0);
  3139. }
  3140. //---------------------------------------------------------------------------
  3141. void __fastcall TCustomScpExplorerForm::TemporaryDirectoryForRemoteFiles(
  3142. UnicodeString RemoteDirectory, TCopyParamType CopyParam,
  3143. UnicodeString & Result, UnicodeString & RootDirectory)
  3144. {
  3145. if (!WinConfiguration->TemporaryDirectoryDeterministic)
  3146. {
  3147. RootDirectory = IncludeTrailingBackslash(WinConfiguration->TemporaryDir());
  3148. Result = RootDirectory;
  3149. }
  3150. else
  3151. {
  3152. RootDirectory = L"";
  3153. Result = WinConfiguration->ExpandedTemporaryDirectory();
  3154. Result = IncludeTrailingBackslash(Result);
  3155. }
  3156. if (WinConfiguration->TemporaryDirectoryAppendSession)
  3157. {
  3158. Result = IncludeTrailingBackslash(Result + MakeValidFileName(Terminal->SessionData->SessionName));
  3159. }
  3160. if (WinConfiguration->TemporaryDirectoryAppendPath)
  3161. {
  3162. if (!RemoteDirectory.IsEmpty() && (RemoteDirectory[1] == L'/'))
  3163. {
  3164. RemoteDirectory.Delete(1, 1);
  3165. }
  3166. Result = IncludeTrailingBackslash(Result + CopyParam.ValidLocalPath(FromUnixPath(RemoteDirectory)));
  3167. }
  3168. if (!ForceDirectories(ApiPath(Result)))
  3169. {
  3170. throw EOSExtException(FMTLOAD(CREATE_TEMP_DIR_ERROR, (Result)));
  3171. }
  3172. }
  3173. //---------------------------------------------------------------------------
  3174. void __fastcall TCustomScpExplorerForm::TemporarilyDownloadFiles(
  3175. TStrings * FileList, bool ForceText, UnicodeString & RootTempDir, UnicodeString & TempDir,
  3176. bool AllFiles, bool GetTargetNames, bool AutoOperation)
  3177. {
  3178. DebugAssert(!IsLocalBrowserMode());
  3179. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  3180. if (ForceText)
  3181. {
  3182. CopyParam.TransferMode = tmAscii;
  3183. }
  3184. TemporaryFileCopyParam(CopyParam);
  3185. if (AllFiles)
  3186. {
  3187. CopyParam.IncludeFileMask = TFileMasks();
  3188. }
  3189. if (TempDir.IsEmpty())
  3190. {
  3191. TemporaryDirectoryForRemoteFiles(Terminal->CurrentDirectory, CopyParam, TempDir, RootTempDir);
  3192. }
  3193. DebugAssert(!FAutoOperation);
  3194. FAutoOperation = AutoOperation;
  3195. Terminal->ExceptionOnFail = true;
  3196. try
  3197. {
  3198. try
  3199. {
  3200. // turn off confirmations, as for MDI editors we may possibly download
  3201. // the same file over
  3202. Terminal->CopyToLocal(
  3203. FileList, TempDir, &CopyParam, cpNoConfirmation | cpTemporary, NULL);
  3204. if (GetTargetNames)
  3205. {
  3206. for (int i = 0; i < FileList->Count; i++)
  3207. {
  3208. FileList->Strings[i] =
  3209. Terminal->ChangeFileName(&CopyParam, UnixExtractFileName(FileList->Strings[i]), osRemote, false);
  3210. }
  3211. }
  3212. }
  3213. catch(...)
  3214. {
  3215. if (!RootTempDir.IsEmpty())
  3216. {
  3217. RecursiveDeleteFile(ExcludeTrailingBackslash(RootTempDir), false);
  3218. }
  3219. throw;
  3220. }
  3221. }
  3222. __finally
  3223. {
  3224. FAutoOperation = false;
  3225. Terminal->ExceptionOnFail = false;
  3226. }
  3227. }
  3228. //---------------------------------------------------------------------------
  3229. void __fastcall TCustomScpExplorerForm::EditorAutoConfig()
  3230. {
  3231. // Do not waste time checking for default editor list the next time,
  3232. // or testing if default editor is not notepad.
  3233. bool TryNextTime = false;
  3234. UnicodeString UsageState;
  3235. if (!WinConfiguration->EditorList->IsDefaultList())
  3236. {
  3237. UsageState = "H";
  3238. }
  3239. else
  3240. {
  3241. UnicodeString Executable;
  3242. UnicodeString ExecutableDescription;
  3243. if (DetectSystemExternalEditor(false, Executable, ExecutableDescription, UsageState, TryNextTime))
  3244. {
  3245. UnicodeString Message =
  3246. FMTLOAD(EDITOR_AUTO_CONFIG2, (ExecutableDescription, ExecutableDescription));
  3247. unsigned int Answer =
  3248. MessageDialog(Message, qtConfirmation, qaOK | qaCancel, HELP_EDITOR_AUTO_CONFIG);
  3249. if (Answer != qaOK)
  3250. {
  3251. UsageState = "R";
  3252. }
  3253. else
  3254. {
  3255. UsageState = "A";
  3256. TEditorData EditorData;
  3257. EditorData.Editor = edExternal;
  3258. EditorData.ExternalEditor = FormatCommand(Executable, L"");
  3259. EditorData.ExternalEditorOptionsAutodetect();
  3260. TEditorList EditorList;
  3261. EditorList = *WinConfiguration->EditorList;
  3262. EditorList.Insert(0, new TEditorPreferences(EditorData));
  3263. WinConfiguration->EditorList = &EditorList;
  3264. }
  3265. }
  3266. }
  3267. WinConfiguration->OfferedEditorAutoConfig = !TryNextTime;
  3268. WinConfiguration->Usage->Set(L"EditorAutoConfig", UsageState);
  3269. }
  3270. //---------------------------------------------------------------------------
  3271. void __fastcall TCustomScpExplorerForm::ExecuteFileNormalize(
  3272. TExecuteFileBy & ExecuteFileBy, const TEditorData *& ExternalEditor,
  3273. const UnicodeString & FileName, bool Local, const TFileMasks::TParams & MaskParams)
  3274. {
  3275. if (ExecuteFileBy == efDefaultEditor)
  3276. {
  3277. if (!WinConfiguration->OfferedEditorAutoConfig)
  3278. {
  3279. EditorAutoConfig();
  3280. }
  3281. const TEditorPreferences * Editor =
  3282. WinConfiguration->DefaultEditorForFile(FileName, Local, MaskParams);
  3283. if ((Editor == NULL) || (Editor->Data->Editor == edInternal))
  3284. {
  3285. ExecuteFileBy = efInternalEditor;
  3286. ExternalEditor = NULL;
  3287. }
  3288. else if (Editor->Data->Editor == edOpen)
  3289. {
  3290. ExecuteFileBy = efShell;
  3291. ExternalEditor = NULL;
  3292. }
  3293. else
  3294. {
  3295. ExecuteFileBy = efExternalEditor;
  3296. ExternalEditor = Editor->Data;
  3297. }
  3298. }
  3299. }
  3300. //---------------------------------------------------------------------------
  3301. void __fastcall TCustomScpExplorerForm::ExecuteFile(TOperationSide Side,
  3302. TExecuteFileBy ExecuteFileBy, const TEditorData * ExternalEditor,
  3303. UnicodeString FullFileName, TObject * Object, const TFileMasks::TParams & MaskParams)
  3304. {
  3305. UnicodeString OriginalFileName;
  3306. UnicodeString LocalRootDirectory;
  3307. UnicodeString RemoteDirectory;
  3308. ExecuteFileNormalize(ExecuteFileBy, ExternalEditor, FullFileName,
  3309. IsSideLocalBrowser(Side), MaskParams);
  3310. UnicodeString Counter;
  3311. UnicodeString LocalFileName;
  3312. bool Handled = false;
  3313. if (!IsSideLocalBrowser(Side))
  3314. {
  3315. // We need to trim VMS version here, so that we use name without version
  3316. // when uploading back to create a new version of the file
  3317. OriginalFileName = Terminal->GetBaseFileName(UnixExtractFileName(FullFileName));
  3318. RemoteDirectory = UnixExtractFilePath(FullFileName);
  3319. TObject * Token = NULL;
  3320. UnicodeString LocalDirectory;
  3321. if (!FEditorManager->CanAddFile(RemoteDirectory, OriginalFileName,
  3322. Terminal->SessionData->SessionName, Token, LocalRootDirectory,
  3323. LocalDirectory))
  3324. {
  3325. if (Token != NULL)
  3326. {
  3327. TForm * Form = dynamic_cast<TForm *>(Token);
  3328. if (Form->WindowState == wsMinimized)
  3329. {
  3330. ShowWindow(Form->Handle, SW_RESTORE);
  3331. }
  3332. else
  3333. {
  3334. Form->SetFocus();
  3335. }
  3336. Handled = true;
  3337. }
  3338. else
  3339. {
  3340. throw Exception(FMTLOAD(ALREADY_EDITED_EXTERNALLY_OR_UPLOADED, (OriginalFileName)));
  3341. }
  3342. }
  3343. if (!Handled)
  3344. {
  3345. TStringList * FileList1 = new TStringList();
  3346. try
  3347. {
  3348. FileList1->AddObject(FullFileName, Object);
  3349. TemporarilyDownloadFiles(FileList1,
  3350. RemoteExecuteForceText(ExecuteFileBy, ExternalEditor),
  3351. LocalRootDirectory, LocalDirectory, true, true, true);
  3352. LocalFileName = LocalDirectory + FileList1->Strings[0];
  3353. }
  3354. __finally
  3355. {
  3356. delete FileList1;
  3357. }
  3358. switch (ExecuteFileBy)
  3359. {
  3360. case efShell:
  3361. Counter = "RemoteFilesExecuted";
  3362. break;
  3363. case efInternalEditor:
  3364. Counter = "RemoteFilesOpenedInInternalEditor";
  3365. break;
  3366. case efExternalEditor:
  3367. Counter = "RemoteFilesOpenedInExternalEditor";
  3368. break;
  3369. default:
  3370. DebugFail();
  3371. }
  3372. }
  3373. }
  3374. else
  3375. {
  3376. LocalFileName = FullFileName;
  3377. OriginalFileName = ExtractFileName(FullFileName);
  3378. switch (ExecuteFileBy)
  3379. {
  3380. case efShell:
  3381. Counter = "LocalFilesExecuted";
  3382. break;
  3383. case efInternalEditor:
  3384. Counter = "LocalFilesOpenedInInternalEditor";
  3385. break;
  3386. case efExternalEditor:
  3387. Counter = "LocalFilesOpenedInExternalEditor";
  3388. break;
  3389. default:
  3390. DebugFail();
  3391. }
  3392. }
  3393. if (!Handled)
  3394. {
  3395. Configuration->Usage->Inc(Counter);
  3396. CustomExecuteFile(Side, ExecuteFileBy, LocalFileName, OriginalFileName,
  3397. ExternalEditor, LocalRootDirectory, RemoteDirectory, false);
  3398. }
  3399. }
  3400. //---------------------------------------------------------------------------
  3401. void __fastcall TCustomScpExplorerForm::ExecuteFile(TOperationSide Side,
  3402. TExecuteFileBy ExecuteFileBy, const TEditorData * ExternalEditor,
  3403. bool AllSelected, bool OnFocused)
  3404. {
  3405. DebugAssert(!WinConfiguration->DisableOpenEdit);
  3406. DebugAssert((ExecuteFileBy == efExternalEditor) ==
  3407. ((ExternalEditor != NULL) && (ExternalEditor->Editor == edExternal)));
  3408. Side = GetSide(Side);
  3409. TCustomDirView * DView = DirView(Side);
  3410. TStrings * FileList =
  3411. AllSelected ?
  3412. DView->CreateFileList(OnFocused, IsSideLocalBrowser(Side)) :
  3413. DView->CreateFocusedFileList(IsSideLocalBrowser(Side));
  3414. try
  3415. {
  3416. DebugAssert(AllSelected || (FileList->Count == 1));
  3417. for (int i = 0; i < FileList->Count; i++)
  3418. {
  3419. UnicodeString ListFileName = FileList->Strings[i];
  3420. UnicodeString FileNameOnly =
  3421. !IsSideLocalBrowser(Side) ? UnixExtractFileName(ListFileName) : ExtractFileName(ListFileName);
  3422. TListItem * Item = DView->FindFileItem(FileNameOnly);
  3423. if (!DView->ItemIsDirectory(Item))
  3424. {
  3425. UnicodeString FullFileName;
  3426. if (!IsSideLocalBrowser(Side))
  3427. {
  3428. FullFileName = DirView(Side)->Path + ListFileName;
  3429. }
  3430. else
  3431. {
  3432. FullFileName = ListFileName;
  3433. }
  3434. TFileMasks::TParams MaskParams;
  3435. MaskParams.Size = DView->ItemFileSize(Item);
  3436. TDateTimePrecision Precision;
  3437. MaskParams.Modification = DView->ItemFileTime(Item, Precision);
  3438. ExecuteFile(Side, ExecuteFileBy, ExternalEditor, FullFileName,
  3439. FileList->Objects[i], MaskParams);
  3440. }
  3441. }
  3442. }
  3443. __finally
  3444. {
  3445. delete FileList;
  3446. }
  3447. }
  3448. //---------------------------------------------------------------------------
  3449. void __fastcall TCustomScpExplorerForm::TemporaryFileCopyParam(TCopyParamType & CopyParam)
  3450. {
  3451. CopyParam.FileNameCase = ncNoChange;
  3452. CopyParam.PreserveRights = false;
  3453. CopyParam.PreserveReadOnly = false;
  3454. CopyParam.ReplaceInvalidChars = true;
  3455. CopyParam.IncludeFileMask = TFileMasks();
  3456. CopyParam.NewerOnly = false;
  3457. CopyParam.FileMask = L"";
  3458. }
  3459. //---------------------------------------------------------------------------
  3460. void __fastcall TCustomScpExplorerForm::ExecutedFileChanged(const UnicodeString FileName,
  3461. TEditedFileData * Data, HANDLE UploadCompleteEvent)
  3462. {
  3463. TTerminalManager * Manager = TTerminalManager::Instance();
  3464. if (!IsActiveTerminal(Data->Terminal))
  3465. {
  3466. if (!NonVisualDataModule->Busy)
  3467. {
  3468. UnicodeString FileNameOnly = ExtractFileName(FileName);
  3469. UnicodeString EditInactiveSessionReopenAcceptedCounter = L"EditInactiveSessionReopenAccepted";
  3470. UnicodeString EditInactiveSessionReopenRejectedCounter = L"EditInactiveSessionReopenRejected";
  3471. if (Data->Terminal == NULL)
  3472. {
  3473. TTerminal * SameSiteTerminal = Manager->FindActiveTerminalForSite(Data->SessionData);
  3474. if (SameSiteTerminal != NULL)
  3475. {
  3476. UnicodeString Message =
  3477. FMTLOAD(EDIT_SESSION_REATTACH,
  3478. (FileNameOnly, Data->SessionName, FileNameOnly));
  3479. if (MessageDialog(Message, qtConfirmation, qaOK | qaCancel) == qaOK)
  3480. {
  3481. Data->Terminal = Terminal;
  3482. Data->Queue = Manager->FindQueueForTerminal(Terminal);
  3483. Data->SessionName = Terminal->SessionData->SessionName;
  3484. // We might also overwrite session data
  3485. Configuration->Usage->Inc(EditInactiveSessionReopenAcceptedCounter);
  3486. }
  3487. else
  3488. {
  3489. Configuration->Usage->Inc(EditInactiveSessionReopenRejectedCounter);
  3490. Abort();
  3491. }
  3492. }
  3493. }
  3494. // foreground session should reconnect itself
  3495. else if (Terminal != Data->Terminal)
  3496. {
  3497. UnicodeString Message =
  3498. MainInstructions(
  3499. FMTLOAD(EDIT_SESSION_RECONNECT, (Data->SessionName, FileNameOnly)));
  3500. if (MessageDialog(Message, qtConfirmation, qaOK | qaCancel) == qaOK)
  3501. {
  3502. Manager->SetActiveTerminalWithAutoReconnect(Data->Terminal);
  3503. Configuration->Usage->Inc(EditInactiveSessionReopenAcceptedCounter);
  3504. }
  3505. else
  3506. {
  3507. Configuration->Usage->Inc(EditInactiveSessionReopenRejectedCounter);
  3508. Abort();
  3509. }
  3510. }
  3511. }
  3512. if (!IsActiveTerminal(Data->Terminal))
  3513. {
  3514. Configuration->Usage->Inc(L"EditInactiveSession");
  3515. // Prevent this when not idle (!NonVisualDataModule->Busy)?
  3516. throw Exception(FMTLOAD(EDIT_SESSION_CLOSED2,
  3517. (ExtractFileName(FileName), Data->SessionName)));
  3518. }
  3519. }
  3520. TStrings * FileList = new TStringList();
  3521. try
  3522. {
  3523. FileList->Add(FileName);
  3524. // consider using the same settings (preset) as when the file was downloaded
  3525. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  3526. TemporaryFileCopyParam(CopyParam);
  3527. if (Data->ForceText)
  3528. {
  3529. CopyParam.TransferMode = tmAscii;
  3530. }
  3531. // so i do not need to worry if masking algorithm works in all cases
  3532. // ("" means "copy file name", no masking is actually done)
  3533. if (ExtractFileName(FileName) == Data->OriginalFileName)
  3534. {
  3535. CopyParam.FileMask = L"";
  3536. }
  3537. else
  3538. {
  3539. CopyParam.FileMask = DelimitFileNameMask(Data->OriginalFileName);
  3540. }
  3541. DebugAssert(Data->Queue != NULL);
  3542. int Params = cpNoConfirmation | cpTemporary;
  3543. TQueueItem * QueueItem = new TUploadQueueItem(Data->Terminal, FileList,
  3544. Data->RemoteDirectory, &CopyParam, Params, true, false);
  3545. QueueItem->CompleteEvent = UploadCompleteEvent;
  3546. AddQueueItem(Data->Queue, QueueItem, Data->Terminal);
  3547. }
  3548. __finally
  3549. {
  3550. delete FileList;
  3551. }
  3552. }
  3553. //---------------------------------------------------------------------------
  3554. void __fastcall TCustomScpExplorerForm::ExecutedFileReload(
  3555. const UnicodeString FileName, const TEditedFileData * Data)
  3556. {
  3557. // Sanity check, we should not be busy otherwise user would not be able to click Reload button.
  3558. DebugAssert(!NonVisualDataModule->Busy);
  3559. if (!IsActiveTerminal(Data->Terminal))
  3560. {
  3561. throw Exception(FMTLOAD(EDIT_SESSION_CLOSED_RELOAD,
  3562. (ExtractFileName(FileName), Data->SessionName)));
  3563. }
  3564. TManagedTerminal * PrevSession = TTerminalManager::Instance()->ActiveSession;
  3565. TTerminalManager::Instance()->ActiveSession = Data->Terminal;
  3566. NonVisualDataModule->StartBusy();
  3567. try
  3568. {
  3569. std::unique_ptr<TRemoteFile> File;
  3570. UnicodeString RemoteFileName =
  3571. UnixIncludeTrailingBackslash(Data->RemoteDirectory) + Data->OriginalFileName;
  3572. Terminal->ExceptionOnFail = true;
  3573. try
  3574. {
  3575. TRemoteFile * AFile = NULL;
  3576. Terminal->ReadFile(RemoteFileName, AFile);
  3577. File.reset(AFile);
  3578. if (!File->HaveFullFileName)
  3579. {
  3580. File->FullFileName = RemoteFileName;
  3581. }
  3582. }
  3583. __finally
  3584. {
  3585. Terminal->ExceptionOnFail = false;
  3586. }
  3587. std::unique_ptr<TStrings> FileList(new TStringList());
  3588. FileList->AddObject(RemoteFileName, File.get());
  3589. UnicodeString RootTempDir = Data->LocalRootDirectory;
  3590. UnicodeString TempDir = ExtractFilePath(FileName);
  3591. TemporarilyDownloadFiles(FileList.get(), Data->ForceText, RootTempDir,
  3592. TempDir, true, true, true);
  3593. // sanity check, the target file name should be still the same
  3594. DebugAssert(ExtractFileName(FileName) == FileList->Strings[0]);
  3595. }
  3596. __finally
  3597. {
  3598. NonVisualDataModule->EndBusy();
  3599. // it actually may not exist anymore...
  3600. TTerminalManager::Instance()->ActiveSession = PrevSession;
  3601. }
  3602. }
  3603. //---------------------------------------------------------------------------
  3604. void __fastcall TCustomScpExplorerForm::ExecutedFileEarlyClosed(
  3605. const TEditedFileData * Data, bool & KeepOpen)
  3606. {
  3607. // Command is set for external editors only (not for "shell" open).
  3608. if (!Data->Command.IsEmpty())
  3609. {
  3610. bool AnyFound = false;
  3611. bool AnyMDI = false;
  3612. bool AnyNonMDI = false;
  3613. bool AnyDetect = false;
  3614. TEditorList * EditorList = new TEditorList();
  3615. try
  3616. {
  3617. *EditorList = *WinConfiguration->EditorList;
  3618. for (int i = 0; i < EditorList->Count; i++)
  3619. {
  3620. const TEditorPreferences * Editor = EditorList->Editors[i];
  3621. if ((Editor->Data->Editor == edExternal) &&
  3622. (Editor->Data->ExternalEditor == Data->Command))
  3623. {
  3624. AnyFound = true;
  3625. if (Editor->Data->SDIExternalEditor)
  3626. {
  3627. AnyNonMDI = true;
  3628. if (Editor->Data->DetectMDIExternalEditor)
  3629. {
  3630. AnyDetect = true;
  3631. }
  3632. }
  3633. else
  3634. {
  3635. AnyMDI = true;
  3636. }
  3637. }
  3638. }
  3639. bool EnableMDI = false;
  3640. bool DisableDetect = false;
  3641. if (AnyMDI)
  3642. {
  3643. KeepOpen = true;
  3644. if (AnyNonMDI)
  3645. {
  3646. // there is at least one instance of the editor with MDI support enabled,
  3647. // and one with disabled, enable it for all instances
  3648. EnableMDI = true;
  3649. }
  3650. }
  3651. else if (AnyFound && !AnyDetect)
  3652. {
  3653. // at least once instance found but all have MDI autodetection disabled
  3654. // => close the file (default action)
  3655. }
  3656. else
  3657. {
  3658. // no instance of the editor has MDI support enabled
  3659. TMessageParams Params;
  3660. if (AnyFound)
  3661. {
  3662. // there is at least one instance of the editor
  3663. Params.Params |= mpNeverAskAgainCheck;
  3664. }
  3665. unsigned int Answer = MessageDialog(FMTLOAD(EDITOR_EARLY_CLOSED2, (Data->OriginalFileName)), qtWarning,
  3666. qaYes | qaNo, HELP_EDITOR_EARLY_CLOSED, &Params);
  3667. switch (Answer)
  3668. {
  3669. case qaNeverAskAgain:
  3670. DisableDetect = true;
  3671. break;
  3672. case qaNo:
  3673. EnableMDI = true;
  3674. KeepOpen = true;
  3675. break;
  3676. }
  3677. }
  3678. if (AnyFound && (EnableMDI || DisableDetect))
  3679. {
  3680. bool Changed = false;
  3681. for (int i = 0; i < EditorList->Count; i++)
  3682. {
  3683. const TEditorPreferences * Editor = EditorList->Editors[i];
  3684. if ((Editor->Data->Editor == edExternal) &&
  3685. (Editor->Data->ExternalEditor == Data->Command) &&
  3686. ((EnableMDI && Editor->Data->SDIExternalEditor) ||
  3687. (DisableDetect && Editor->Data->DetectMDIExternalEditor)))
  3688. {
  3689. Changed = true;
  3690. TEditorPreferences * UpdatedEditor = new TEditorPreferences(*Editor);
  3691. if (EnableMDI)
  3692. {
  3693. UpdatedEditor->GetData()->SDIExternalEditor = false;
  3694. }
  3695. if (DisableDetect)
  3696. {
  3697. UpdatedEditor->GetData()->DetectMDIExternalEditor = false;
  3698. }
  3699. EditorList->Change(i, UpdatedEditor);
  3700. }
  3701. }
  3702. if (Changed)
  3703. {
  3704. WinConfiguration->EditorList = EditorList;
  3705. }
  3706. }
  3707. }
  3708. __finally
  3709. {
  3710. delete EditorList;
  3711. }
  3712. }
  3713. else
  3714. {
  3715. // "open" case
  3716. MessageDialog(FMTLOAD(APP_EARLY_CLOSED, (Data->OriginalFileName)), qtWarning,
  3717. qaOK, HELP_APP_EARLY_CLOSED);
  3718. }
  3719. }
  3720. //---------------------------------------------------------------------------
  3721. void __fastcall TCustomScpExplorerForm::ExecutedFileUploadComplete(TObject * Sender)
  3722. {
  3723. EditorFormFileUploadComplete(DebugNotNull(dynamic_cast<TForm *>(Sender)));
  3724. }
  3725. //---------------------------------------------------------------------------
  3726. void __fastcall TCustomScpExplorerForm::RemoteDirViewEnter(TObject * /*Sender*/)
  3727. {
  3728. SideEnter(osRemote);
  3729. }
  3730. //---------------------------------------------------------------------------
  3731. void __fastcall TCustomScpExplorerForm::RemoteDriveViewEnter(TObject * /*Sender*/)
  3732. {
  3733. MakeNextInTabOrder(RemoteDirPanel, RemoteDrivePanel);
  3734. SideEnter(osRemote);
  3735. }
  3736. //---------------------------------------------------------------------------
  3737. void __fastcall TCustomScpExplorerForm::SideEnter(TOperationSide Side)
  3738. {
  3739. FCurrentSide = Side;
  3740. if (Visible)
  3741. {
  3742. UpdateControls();
  3743. }
  3744. }
  3745. //---------------------------------------------------------------------------
  3746. void __fastcall TCustomScpExplorerForm::DeleteFiles(TOperationSide Side,
  3747. TStrings * FileList, bool Alternative)
  3748. {
  3749. TCustomDirView * DView = DirView(Side);
  3750. DView->SaveSelection();
  3751. DView->SaveSelectedNames();
  3752. DebugAssert(!FAlternativeDelete);
  3753. FAlternativeDelete = Alternative;
  3754. try
  3755. {
  3756. if (!IsSideLocalBrowser(Side))
  3757. {
  3758. DebugAssert(Terminal != NULL);
  3759. Terminal->DeleteFiles(FileList, FLAGMASK(Alternative, dfAlternative));
  3760. }
  3761. else
  3762. {
  3763. if (IsLocalBrowserMode())
  3764. {
  3765. Configuration->Usage->Inc(L"LocalLocalDeletes");
  3766. }
  3767. TValueRestorer<TOperationSide> ProgressSideRestorer(FProgressSide);
  3768. FProgressSide = FCurrentSide;
  3769. try
  3770. {
  3771. TTerminalManager::Instance()->LocalTerminal->DeleteLocalFiles(FileList, FLAGMASK(Alternative, dfAlternative));
  3772. }
  3773. __finally
  3774. {
  3775. ReloadLocalDirectory();
  3776. }
  3777. }
  3778. FAlternativeDelete = false;
  3779. }
  3780. catch(...)
  3781. {
  3782. FAlternativeDelete = false;
  3783. DView->DiscardSavedSelection();
  3784. throw;
  3785. }
  3786. DView->RestoreSelection();
  3787. }
  3788. //---------------------------------------------------------------------------
  3789. void __fastcall TCustomScpExplorerForm::LockFiles(TStrings * FileList, bool Lock)
  3790. {
  3791. DebugAssert(Terminal);
  3792. DebugAssert(!IsLocalBrowserMode());
  3793. RemoteDirView->SaveSelection();
  3794. RemoteDirView->SaveSelectedNames();
  3795. try
  3796. {
  3797. if (Lock)
  3798. {
  3799. Terminal->LockFiles(FileList);
  3800. }
  3801. else
  3802. {
  3803. Terminal->UnlockFiles(FileList);
  3804. }
  3805. }
  3806. catch(...)
  3807. {
  3808. RemoteDirView->DiscardSavedSelection();
  3809. throw;
  3810. }
  3811. RemoteDirView->RestoreSelection();
  3812. }
  3813. //---------------------------------------------------------------------------
  3814. bool __fastcall TCustomScpExplorerForm::RemoteTransferDialog(TManagedTerminal *& Session,
  3815. TStrings * FileList, UnicodeString & Target, UnicodeString & FileMask, bool & DirectCopy,
  3816. bool NoConfirmation, bool Move)
  3817. {
  3818. DebugAssert(Terminal != NULL);
  3819. DebugAssert(Terminal == TTerminalManager::Instance()->ActiveTerminal);
  3820. DebugAssert(!IsLocalBrowserMode());
  3821. if (Session == NULL)
  3822. {
  3823. Session = Terminal;
  3824. }
  3825. if (Session == Terminal)
  3826. {
  3827. if (RemoteDriveView->DropTarget != NULL)
  3828. {
  3829. Target = RemoteDriveView->NodePathName(RemoteDriveView->DropTarget);
  3830. }
  3831. else if (RemoteDirView->DropTarget != NULL)
  3832. {
  3833. DebugAssert(RemoteDirView->ItemIsDirectory(RemoteDirView->DropTarget));
  3834. Target = RemoteDirView->ItemFullFileName(RemoteDirView->DropTarget);
  3835. }
  3836. else
  3837. {
  3838. Target = RemoteDirView->Path;
  3839. }
  3840. }
  3841. else
  3842. {
  3843. Target = GetSessionPath(Session, osRemote);
  3844. }
  3845. Target = UnixIncludeTrailingBackslash(Target);
  3846. if (FileList->Count == 1)
  3847. {
  3848. FileMask = DelimitFileNameMask(UnixExtractFileName(FileList->Strings[0]));
  3849. }
  3850. else
  3851. {
  3852. FileMask = AnyMask;
  3853. }
  3854. DirectCopy = Terminal->IsCapable[fcRemoteCopy] || Terminal->IsCapable[fcSecondaryShell];
  3855. bool Result = true;
  3856. if (!NoConfirmation)
  3857. {
  3858. bool Multi = (FileList->Count > 1);
  3859. if (Move)
  3860. {
  3861. Result = DoRemoteMoveDialog(Multi, Target, FileMask);
  3862. }
  3863. else
  3864. {
  3865. std::unique_ptr<TStrings> Sessions(new TStringList);
  3866. std::unique_ptr<TStrings> Directories(new TStringList);
  3867. TStrings * SessionList = TTerminalManager::Instance()->SessionList;
  3868. for (int Index = 0; Index < SessionList->Count; Index++)
  3869. {
  3870. TManagedTerminal * ASession = DebugNotNull(dynamic_cast<TManagedTerminal *>(SessionList->Objects[Index]));
  3871. if (IsActiveTerminal(ASession) && DebugAlwaysTrue(!ASession->LocalBrowser))
  3872. {
  3873. Sessions->AddObject(SessionList->Strings[Index], ASession);
  3874. Directories->Add(GetSessionPath(ASession, osRemote));
  3875. }
  3876. }
  3877. TDirectRemoteCopy AllowDirectCopy;
  3878. if (Terminal->IsCapable[fcRemoteCopy] || Terminal->CommandSessionOpened)
  3879. {
  3880. DebugAssert(DirectCopy);
  3881. AllowDirectCopy = drcAllow;
  3882. }
  3883. else if (Terminal->IsCapable[fcSecondaryShell])
  3884. {
  3885. DebugAssert(DirectCopy);
  3886. AllowDirectCopy = drcConfirmCommandSession;
  3887. }
  3888. else
  3889. {
  3890. DebugAssert(!DirectCopy);
  3891. AllowDirectCopy = drcDisallow;
  3892. }
  3893. void * ASession = Session;
  3894. Result = DoRemoteCopyDialog(
  3895. Sessions.get(), Directories.get(), AllowDirectCopy, Multi, ASession, Target, FileMask, DirectCopy, Terminal);
  3896. Session = static_cast<TManagedTerminal *>(ASession);
  3897. }
  3898. }
  3899. return Result;
  3900. }
  3901. //---------------------------------------------------------------------------
  3902. bool __fastcall TCustomScpExplorerForm::RemoteTransferFiles(
  3903. TStrings * FileList, bool NoConfirmation, bool Move, TManagedTerminal * Session)
  3904. {
  3905. DebugAssert(!IsLocalBrowserMode());
  3906. bool DirectCopy;
  3907. UnicodeString Target, FileMask;
  3908. bool Result = RemoteTransferDialog(Session, FileList, Target, FileMask, DirectCopy, NoConfirmation, Move);
  3909. if (Result)
  3910. {
  3911. if (!Move && !DirectCopy)
  3912. {
  3913. Configuration->Usage->Inc("RemoteCopyTemp");
  3914. UnicodeString RootTempDir;
  3915. UnicodeString TempDir;
  3916. TemporarilyDownloadFiles(FileList, false, RootTempDir, TempDir, false, false, false);
  3917. TStrings * TemporaryFilesList = new TStringList();
  3918. try
  3919. {
  3920. TMakeLocalFileListParams MakeFileListParam;
  3921. MakeFileListParam.FileList = TemporaryFilesList;
  3922. MakeFileListParam.FileTimes = NULL;
  3923. MakeFileListParam.IncludeDirs = true;
  3924. MakeFileListParam.Recursive = false;
  3925. ProcessLocalDirectory(TempDir, Terminal->MakeLocalFileList, &MakeFileListParam);
  3926. TTerminalManager::Instance()->ActiveSession = Session;
  3927. if (TemporaryFilesList->Count > 0)
  3928. {
  3929. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  3930. CopyParam.FileMask = FileMask;
  3931. DebugAssert(!FAutoOperation);
  3932. FAutoOperation = true;
  3933. Terminal->CopyToRemote(TemporaryFilesList, Target, &CopyParam, cpTemporary, NULL);
  3934. }
  3935. }
  3936. __finally
  3937. {
  3938. delete TemporaryFilesList;
  3939. FAutoOperation = false;
  3940. if (!RootTempDir.IsEmpty())
  3941. {
  3942. RecursiveDeleteFile(ExcludeTrailingBackslash(RootTempDir), false);
  3943. }
  3944. }
  3945. }
  3946. else
  3947. {
  3948. RemoteDirView->SaveSelection();
  3949. RemoteDirView->SaveSelectedNames();
  3950. try
  3951. {
  3952. if (Move)
  3953. {
  3954. Configuration->Usage->Inc("RemoteMove");
  3955. Terminal->MoveFiles(FileList, Target, FileMask);
  3956. }
  3957. else
  3958. {
  3959. Configuration->Usage->Inc("RemoteCopyDirect");
  3960. DebugAssert(DirectCopy);
  3961. DebugAssert(Session == Terminal);
  3962. if (Terminal->IsCapable[fcRemoteCopy] ||
  3963. Terminal->CommandSessionOpened ||
  3964. CommandSessionFallback())
  3965. {
  3966. Terminal->CopyFiles(FileList, Target, FileMask);
  3967. }
  3968. }
  3969. }
  3970. catch(...)
  3971. {
  3972. RemoteDirView->DiscardSavedSelection();
  3973. throw;
  3974. }
  3975. RemoteDirView->RestoreSelection();
  3976. }
  3977. }
  3978. return Result;
  3979. }
  3980. //---------------------------------------------------------------------------
  3981. void __fastcall TCustomScpExplorerForm::CreateRemoteDirectory(
  3982. const UnicodeString & Path, TRemoteProperties & Properties)
  3983. {
  3984. DebugAssert(!IsLocalBrowserMode());
  3985. Properties.Valid = Properties.Valid << vpEncrypt;
  3986. Properties.Encrypt = GUIConfiguration->CurrentCopyParam.EncryptNewFiles;
  3987. RemoteDirView->CreateDirectoryEx(Path, &Properties);
  3988. }
  3989. //---------------------------------------------------------------------------
  3990. void __fastcall TCustomScpExplorerForm::CreateDirectory(TOperationSide Side)
  3991. {
  3992. Side = GetSide(Side);
  3993. TRemoteProperties Properties = GUIConfiguration->NewDirectoryProperties;
  3994. TRemoteProperties * AProperties = (!IsSideLocalBrowser(Side) ? &Properties : NULL);
  3995. UnicodeString Name = LoadStr(NEW_FOLDER);
  3996. int AllowedChanges =
  3997. FLAGMASK(!IsSideLocalBrowser(Side) && Terminal->IsCapable[fcModeChanging], cpMode);
  3998. bool SaveSettings = false;
  3999. if (DoCreateDirectoryDialog(Name, AProperties, AllowedChanges, SaveSettings))
  4000. {
  4001. TWindowLock Lock(this);
  4002. if (!IsSideLocalBrowser(Side))
  4003. {
  4004. if (SaveSettings)
  4005. {
  4006. GUIConfiguration->NewDirectoryProperties = Properties;
  4007. }
  4008. CreateRemoteDirectory(Name, Properties);
  4009. }
  4010. else
  4011. {
  4012. DirView(Side)->CreateDirectory(Name);
  4013. }
  4014. }
  4015. }
  4016. //---------------------------------------------------------------------------
  4017. void __fastcall TCustomScpExplorerForm::HomeDirectory(TOperationSide Side)
  4018. {
  4019. TWindowLock Lock(this);
  4020. DirView(Side)->ExecuteHomeDirectory();
  4021. }
  4022. //---------------------------------------------------------------------------
  4023. void __fastcall TCustomScpExplorerForm::OpenDirectory(TOperationSide Side)
  4024. {
  4025. DoOpenDirectoryDialog(odBrowse, Side);
  4026. }
  4027. //---------------------------------------------------------------------------
  4028. bool __fastcall TCustomScpExplorerForm::OpenBookmark(TOperationSide Side, TBookmark * Bookmark)
  4029. {
  4030. TOperationSide BookmarkSide = IsLocalBrowserMode() ? osLocal : Side;
  4031. UnicodeString Path = Bookmark->GetSideDirectory(BookmarkSide);
  4032. bool Result = !Path.IsEmpty();
  4033. if (Result)
  4034. {
  4035. // While we might get here when the session is closed (from location profiles),
  4036. // it's not a problem as the Path setter is noop then
  4037. DirView(Side)->Path = Path;
  4038. }
  4039. return Result;
  4040. }
  4041. //---------------------------------------------------------------------------
  4042. void __fastcall TCustomScpExplorerForm::CalculateSize(
  4043. TStrings * FileList, __int64 & Size, TCalculateSizeStats & Stats,
  4044. bool & Close)
  4045. {
  4046. // terminal can be already closed (e.g. dropped connection)
  4047. if (Terminal != NULL)
  4048. {
  4049. try
  4050. {
  4051. Terminal->CalculateFilesSize(FileList, Size, 0, NULL, true, Stats);
  4052. }
  4053. catch(...)
  4054. {
  4055. if (!Terminal->Active)
  4056. {
  4057. Close = true;
  4058. }
  4059. throw;
  4060. }
  4061. }
  4062. }
  4063. //---------------------------------------------------------------------------
  4064. void __fastcall TCustomScpExplorerForm::CalculateChecksum(const UnicodeString & Alg,
  4065. TStrings * FileList, TCalculatedChecksumEvent OnCalculatedChecksum,
  4066. bool & Close)
  4067. {
  4068. // terminal can be already closed (e.g. dropped connection)
  4069. if (Terminal != NULL)
  4070. {
  4071. Configuration->Usage->Inc(L"ChecksumCalculated");
  4072. try
  4073. {
  4074. Terminal->CalculateFilesChecksum(Alg, FileList, NULL, OnCalculatedChecksum);
  4075. }
  4076. catch(...)
  4077. {
  4078. if (!Terminal->Active)
  4079. {
  4080. Close = true;
  4081. }
  4082. throw;
  4083. }
  4084. }
  4085. }
  4086. //---------------------------------------------------------------------------
  4087. bool __fastcall TCustomScpExplorerForm::SetProperties(TOperationSide Side, TStrings * FileList)
  4088. {
  4089. bool Result;
  4090. if (!IsSideLocalBrowser(Side))
  4091. {
  4092. TRemoteTokenList * GroupList = NULL;
  4093. TRemoteTokenList * UserList = NULL;
  4094. try
  4095. {
  4096. TRemoteProperties CurrentProperties;
  4097. if (Terminal->LoadFilesProperties(FileList))
  4098. {
  4099. RemoteDirView->Invalidate();
  4100. }
  4101. bool CapableGroupChanging = Terminal->IsCapable[fcGroupChanging];
  4102. bool CapableOwnerChanging = Terminal->IsCapable[fcOwnerChanging];
  4103. if (CapableGroupChanging || CapableOwnerChanging)
  4104. {
  4105. if (CapableGroupChanging)
  4106. {
  4107. GroupList = Terminal->Groups->Duplicate();
  4108. }
  4109. if (CapableOwnerChanging)
  4110. {
  4111. UserList = Terminal->Users->Duplicate();
  4112. }
  4113. TRemoteDirectory * Files = Terminal->Files;
  4114. int Count = Files->Count;
  4115. if (Count > 100)
  4116. {
  4117. Count = 100;
  4118. }
  4119. for (int Index = 0; Index < Count; Index++)
  4120. {
  4121. TRemoteFile * File = Files->Files[Index];
  4122. if (CapableGroupChanging)
  4123. {
  4124. GroupList->AddUnique(File->Group);
  4125. }
  4126. if (CapableOwnerChanging)
  4127. {
  4128. UserList->AddUnique(File->Owner);
  4129. }
  4130. }
  4131. // if we haven't collected tokens for all files in current directory,
  4132. // make sure we collect them at least for all selected files.
  4133. // (note that so far the files in FileList has to be from current direcotry)
  4134. if (Count < Files->Count)
  4135. {
  4136. for (int Index = 0; Index < FileList->Count; Index++)
  4137. {
  4138. TRemoteFile * File = (TRemoteFile *)(FileList->Objects[Index]);
  4139. if (CapableGroupChanging)
  4140. {
  4141. GroupList->AddUnique(File->Group);
  4142. }
  4143. if (CapableOwnerChanging)
  4144. {
  4145. UserList->AddUnique(File->Owner);
  4146. }
  4147. }
  4148. }
  4149. }
  4150. CurrentProperties = TRemoteProperties::CommonProperties(FileList);
  4151. int Flags = 0;
  4152. if (Terminal->IsCapable[fcModeChanging]) Flags |= cpMode;
  4153. if (CapableOwnerChanging) Flags |= cpOwner;
  4154. if (CapableGroupChanging) Flags |= cpGroup;
  4155. TCalculateChecksumEvent CalculateChecksumEvent = NULL;
  4156. if (Terminal->IsCapable[fcCalculatingChecksum])
  4157. {
  4158. CalculateChecksumEvent = CalculateChecksum;
  4159. }
  4160. std::unique_ptr<TStrings> ChecksumAlgs(new TStringList());
  4161. Terminal->GetSupportedChecksumAlgs(ChecksumAlgs.get());
  4162. TRemoteProperties NewProperties = CurrentProperties;
  4163. Result =
  4164. DoPropertiesDialog(FileList, RemoteDirView->PathName,
  4165. GroupList, UserList, ChecksumAlgs.get(), &NewProperties, Flags,
  4166. Terminal->IsCapable[fcGroupOwnerChangingByID],
  4167. CalculateSize, CalculateChecksumEvent);
  4168. if (Result)
  4169. {
  4170. NewProperties = TRemoteProperties::ChangedProperties(CurrentProperties, NewProperties);
  4171. Terminal->ChangeFilesProperties(FileList, &NewProperties);
  4172. }
  4173. }
  4174. __finally
  4175. {
  4176. delete GroupList;
  4177. delete UserList;
  4178. }
  4179. }
  4180. else
  4181. {
  4182. DirView(Side)->DisplayPropertiesMenu();
  4183. Result = true;
  4184. }
  4185. return Result;
  4186. }
  4187. //---------------------------------------------------------------------------
  4188. void __fastcall TCustomScpExplorerForm::KeyProcessed(Word & Key, TShiftState Shift)
  4189. {
  4190. if (Shift * AllKeyShiftStates() == (TShiftState() << ssAlt))
  4191. {
  4192. FIgnoreNextDialogChar = Key;
  4193. }
  4194. Key = 0;
  4195. }
  4196. //---------------------------------------------------------------------------
  4197. void __fastcall TCustomScpExplorerForm::CheckCustomCommandShortCut(
  4198. TCustomCommandList * List, Word & Key, Classes::TShiftState Shift, TShortCut KeyShortCut)
  4199. {
  4200. const TCustomCommandType * Command = List->Find(KeyShortCut);
  4201. if (Command != NULL)
  4202. {
  4203. KeyProcessed(Key, Shift);
  4204. if (CustomCommandState(*Command, false, ccltAll) > 0)
  4205. {
  4206. TAutoFlag DontCopyCommandToClipboardFlag(DontCopyCommandToClipboard);
  4207. ExecuteFileOperationCommand(foCustomCommand, osRemote,
  4208. false, false, const_cast<TCustomCommandType *>(Command));
  4209. }
  4210. }
  4211. }
  4212. //---------------------------------------------------------------------------
  4213. void __fastcall TCustomScpExplorerForm::KeyDown(Word & Key, Classes::TShiftState Shift)
  4214. {
  4215. if (QueueView3->Focused() && (QueueView3->OnKeyDown != NULL))
  4216. {
  4217. QueueView3->OnKeyDown(QueueView3, Key, Shift);
  4218. }
  4219. if (!DirView(osCurrent)->IsEditing())
  4220. {
  4221. TShortCut KeyShortCut = ShortCut(Key, Shift);
  4222. for (int Index = 0; Index < NonVisualDataModule->ExplorerActions->ActionCount; Index++)
  4223. {
  4224. // Note that handling shortcuts on panel (current control) popup menu has precendence over this.
  4225. // But so far it's not a problem, as it does what we want.
  4226. TAction * Action = (TAction *)NonVisualDataModule->ExplorerActions->Actions[Index];
  4227. if (((Action->ShortCut == KeyShortCut) ||
  4228. (Action->SecondaryShortCuts->IndexOfShortCut(KeyShortCut) >= 0)) &&
  4229. AllowedAction(Action, aaShortCut))
  4230. {
  4231. // Has to be called before the action as the dialog char is already in queue.
  4232. // So when the action consumes message queue, we already need to have the
  4233. // FIgnoreNextDialogChar set
  4234. KeyProcessed(Key, Shift);
  4235. // Reset reference to previous component (when menu/toolbar was clicked).
  4236. // Needed to detect that action was invoked by keyboard shortcut
  4237. // in TNonVisualDataModule::ExplorerActionsExecute
  4238. Action->ActionComponent = NULL;
  4239. Action->Execute();
  4240. return;
  4241. }
  4242. }
  4243. for (int i = 0; i < TTerminalManager::Instance()->Count; i++)
  4244. {
  4245. if (NonVisualDataModule->OpenSessionShortCut(i) == KeyShortCut)
  4246. {
  4247. KeyProcessed(Key, Shift);
  4248. TTerminalManager::Instance()->ActiveSessionIndex = i;
  4249. return;
  4250. }
  4251. }
  4252. if (Key == VK_TAB && Shift.Contains(ssCtrl))
  4253. {
  4254. KeyProcessed(Key, Shift);
  4255. TTerminalManager::Instance()->CycleTerminals(!Shift.Contains(ssShift));
  4256. }
  4257. TShortCut CustomShortCut = NormalizeCustomShortCut(KeyShortCut);
  4258. if (IsCustomShortCut(CustomShortCut))
  4259. {
  4260. CheckCustomCommandShortCut(WinConfiguration->CustomCommandList, Key, Shift, CustomShortCut);
  4261. CheckCustomCommandShortCut(WinConfiguration->ExtensionList, Key, Shift, CustomShortCut);
  4262. if (WinConfiguration->SharedBookmarks != NULL)
  4263. {
  4264. TBookmark * Bookmark = WinConfiguration->SharedBookmarks->FindByShortCut(CustomShortCut);
  4265. if ((Bookmark != NULL) &&
  4266. OpenBookmark(FCurrentSide, Bookmark))
  4267. {
  4268. KeyProcessed(Key, Shift);
  4269. }
  4270. }
  4271. }
  4272. }
  4273. TForm::KeyDown(Key, Shift);
  4274. }
  4275. //---------------------------------------------------------------------------
  4276. void __fastcall TCustomScpExplorerForm::InitStatusBar()
  4277. {
  4278. bool OpenedTerminal = false;
  4279. UnicodeString ProtocolName;
  4280. UnicodeString ProtocolNameHint;
  4281. UnicodeString SecurityProtocolName;
  4282. UnicodeString DurationHint;
  4283. // See the comment in TScpCommanderForm::UpdateControls()
  4284. TTerminal * ActiveTerminal = TTerminalManager::Instance()->ActiveTerminal;
  4285. if (ActiveTerminal != NULL)
  4286. {
  4287. // Active is not enough here
  4288. if (ActiveTerminal->Status == ssOpened)
  4289. {
  4290. const TSessionInfo & SessionInfo = ActiveTerminal->GetSessionInfo();
  4291. const TFileSystemInfo & FileSystemInfo = ActiveTerminal->GetFileSystemInfo();
  4292. if (FileSystemInfo.ProtocolName.IsEmpty())
  4293. {
  4294. ProtocolName = SessionInfo.ProtocolName;
  4295. }
  4296. else
  4297. {
  4298. ProtocolName = FileSystemInfo.ProtocolName;
  4299. }
  4300. ProtocolNameHint = LoadStr(STATUS_PROTOCOL_HINT);
  4301. SecurityProtocolName = SessionInfo.SecurityProtocolName;
  4302. DurationHint = LoadStr(STATUS_DURATION_HINT);
  4303. OpenedTerminal = true;
  4304. }
  4305. else if (ActiveTerminal->Status > ssClosed)
  4306. {
  4307. ProtocolName = LoadStr(STATUS_CONNECTING);
  4308. }
  4309. else
  4310. {
  4311. ProtocolName = LoadStr(STATUS_NOT_CONNECTED2);
  4312. }
  4313. }
  4314. TTBXStatusBar * SessionStatusBar = (TTBXStatusBar *)GetComponent(fcStatusBar);
  4315. int Offset = SessionStatusBar->Panels->Count - SessionPanelCount;
  4316. TTBXStatusPanel * ProtocolStatusPanel = SessionStatusBar->Panels->Items[Offset + 0];
  4317. ProtocolStatusPanel->ViewPriority = (ActiveTerminal != NULL) ? 100 : 0;
  4318. if (ProtocolStatusPanel->Caption != ProtocolName)
  4319. {
  4320. ProtocolStatusPanel->Caption = ProtocolName;
  4321. std::unique_ptr<TCanvas> Canvas(CreateControlCanvas(SessionStatusBar));
  4322. ProtocolStatusPanel->Size = Canvas->TextWidth(ProtocolName) + ScaleByTextHeight(SessionStatusBar, 32);
  4323. }
  4324. ProtocolStatusPanel->Hint = ProtocolNameHint;
  4325. TTBXStatusPanel * SecurityStatusPanel = SessionStatusBar->Panels->Items[Offset + 1];
  4326. SecurityStatusPanel->ViewPriority = OpenedTerminal ? 98 : 0;
  4327. bool SecurityEnabled = !SecurityProtocolName.IsEmpty();
  4328. SecurityStatusPanel->Enabled = SecurityEnabled;
  4329. UnicodeString SecurityProtocolHint;
  4330. if (SecurityEnabled)
  4331. {
  4332. SecurityProtocolHint = FMTLOAD(STATUS_SECURE, (SecurityProtocolName));
  4333. }
  4334. else
  4335. {
  4336. SecurityProtocolHint = LoadStr(STATUS_INSECURE);
  4337. }
  4338. SecurityStatusPanel->Hint = SecurityProtocolHint;
  4339. TTBXStatusPanel * DurationStatusPanel = SessionStatusBar->Panels->Items[Offset + 2];
  4340. DurationStatusPanel->ViewPriority = OpenedTerminal ? 99 : 0;
  4341. DurationStatusPanel->Hint = DurationHint;
  4342. UpdateStatusBar();
  4343. }
  4344. //---------------------------------------------------------------------------
  4345. void __fastcall TCustomScpExplorerForm::UpdateStatusBar()
  4346. {
  4347. TTBXStatusBar * SessionStatusBar = (TTBXStatusBar *)GetComponent(fcStatusBar);
  4348. DebugAssert(SessionStatusBar != NULL);
  4349. TTBXStatusPanel * NoteStatusPanel = SessionStatusBar->Panels->Items[0];
  4350. if (!FNote.IsEmpty())
  4351. {
  4352. NoteStatusPanel->Caption = FNote;
  4353. }
  4354. else
  4355. {
  4356. UpdateStatusPanelText(NoteStatusPanel);
  4357. }
  4358. NoteStatusPanel->Hint = FNoteHints;
  4359. UnicodeString LoginTime;
  4360. // See the comment in TScpCommanderForm::UpdateControls()
  4361. TTerminal * ActiveTerminal = TTerminalManager::Instance()->ActiveTerminal;
  4362. // Active is not enough here
  4363. if ((ActiveTerminal != NULL) && (ActiveTerminal->Status == ssOpened))
  4364. {
  4365. const TSessionInfo & SessionInfo = ActiveTerminal->GetSessionInfo();
  4366. LoginTime = FormatDateTimeSpan(Configuration->TimeFormat, Now() - SessionInfo.LoginTime);
  4367. }
  4368. TTBXStatusPanel * DurationStatusPanel = SessionStatusBar->Panels->Items[SessionStatusBar->Panels->Count - 1];
  4369. DurationStatusPanel->Caption = LoginTime;
  4370. }
  4371. //---------------------------------------------------------------------------
  4372. void __fastcall TCustomScpExplorerForm::UpdateStatusPanelText(TTBXStatusPanel * Panel)
  4373. {
  4374. Panel->Caption = L"";
  4375. }
  4376. //---------------------------------------------------------------------------
  4377. void __fastcall TCustomScpExplorerForm::Idle()
  4378. {
  4379. if (FShowing || FStandaloneEditing)
  4380. {
  4381. FEditorManager->Check();
  4382. // make sure that Idle is called before update queue, as it may invoke QueueEvent
  4383. // that needs to know if queue view is visible (and it may be closed after queue update)
  4384. TTerminalManager::Instance()->Idle(FDoNotIdleCurrentTerminal > 0);
  4385. }
  4386. if (FShowing)
  4387. {
  4388. if (!NonVisualDataModule->Busy &&
  4389. // Menu is opened or drag&drop is going on
  4390. (Mouse->Capture == NULL))
  4391. {
  4392. if (FRefreshRemoteDirectory)
  4393. {
  4394. if ((Terminal != NULL) && (Terminal->Status == ssOpened))
  4395. {
  4396. Terminal->RefreshDirectory();
  4397. }
  4398. FRefreshRemoteDirectory = false;
  4399. }
  4400. if (FRefreshLocalDirectory)
  4401. {
  4402. ReloadLocalDirectory();
  4403. FRefreshLocalDirectory = false;
  4404. }
  4405. if (WinConfiguration->RefreshRemotePanel)
  4406. {
  4407. if ((Terminal != NULL) && (Terminal->Status == ssOpened) &&
  4408. (Now() - Terminal->DirectoryLoaded > WinConfiguration->RefreshRemotePanelInterval))
  4409. {
  4410. RemoteDirView->ReloadDirectory();
  4411. }
  4412. }
  4413. }
  4414. }
  4415. if (FShowing || FStandaloneEditing)
  4416. {
  4417. if (FQueueStatusInvalidated)
  4418. {
  4419. UpdateQueueStatus(false);
  4420. }
  4421. RefreshQueueItems();
  4422. }
  4423. if (FShowing)
  4424. {
  4425. UpdateStatusBar();
  4426. }
  4427. FIgnoreNextDialogChar = 0;
  4428. }
  4429. //---------------------------------------------------------------------------
  4430. void __fastcall TCustomScpExplorerForm::UserActionTimer(TObject * /*Sender*/)
  4431. {
  4432. try
  4433. {
  4434. FUserActionTimer->Enabled = false;
  4435. if (IsQueueAutoPopup() && (FPendingQueueActionItem != NULL))
  4436. {
  4437. if (TQueueItem::IsUserActionStatus(FPendingQueueActionItem->Status))
  4438. {
  4439. FPendingQueueActionItem->ProcessUserAction();
  4440. }
  4441. }
  4442. }
  4443. __finally
  4444. {
  4445. FPendingQueueActionItem = NULL;
  4446. }
  4447. }
  4448. //---------------------------------------------------------------------------
  4449. void __fastcall TCustomScpExplorerForm::ApplicationMinimize(TObject * /*Sender*/)
  4450. {
  4451. if (WinConfiguration->MinimizeToTray)
  4452. {
  4453. UpdateTrayIcon();
  4454. FTrayIcon->Visible = true;
  4455. if (Visible)
  4456. {
  4457. ShowWindow(Handle, SW_HIDE);
  4458. }
  4459. }
  4460. }
  4461. //---------------------------------------------------------------------------
  4462. void __fastcall TCustomScpExplorerForm::ApplicationRestore(TObject * /*Sender*/)
  4463. {
  4464. // WORKAROUND
  4465. // When restoring maximized window from minimization,
  4466. // rarely some controls do not align properly.
  4467. // Two instances seen (both for Commander):
  4468. // - When restoring, window is temporarily narrower (not maximized),
  4469. // causing toolbars on TopDock to wrap and dock to expand horizontally.
  4470. // Once maximized already, top dock shinks back, but the session PageControl,
  4471. // do not align up, leaving space between TopDock and PageControl.
  4472. // - Similar issue seen with LocalDirView not aligning down to status bar.
  4473. for (int Index = 0; Index < ControlCount; Index++)
  4474. {
  4475. RealignControl(Controls[Index]);
  4476. }
  4477. if (FTrayIcon->Visible)
  4478. {
  4479. FTrayIcon->Visible = false;
  4480. if (Visible)
  4481. {
  4482. ShowWindow(Handle, SW_SHOW);
  4483. }
  4484. }
  4485. if (FNeedSession && DebugAlwaysTrue(Terminal == NULL))
  4486. {
  4487. FNeedSession = false;
  4488. NonVisualDataModule->StartBusy();
  4489. try
  4490. {
  4491. NeedSession(true);
  4492. }
  4493. __finally
  4494. {
  4495. NonVisualDataModule->EndBusy();
  4496. }
  4497. }
  4498. }
  4499. //---------------------------------------------------------------------------
  4500. void __fastcall TCustomScpExplorerForm::UpdateTrayIcon()
  4501. {
  4502. FTrayIcon->Hint = GetMainForm()->Caption;
  4503. }
  4504. //---------------------------------------------------------------------------
  4505. void __fastcall TCustomScpExplorerForm::ApplicationTitleChanged()
  4506. {
  4507. UpdateTrayIcon();
  4508. }
  4509. //---------------------------------------------------------------------------
  4510. void __fastcall TCustomScpExplorerForm::RestoreApp()
  4511. {
  4512. // workaround
  4513. // TApplication.WndProc sets TApplication.FAppIconic to false,
  4514. // on WM_ACTIVATEAPP, what renders TApplication.Restore no-op function.
  4515. // But WM_ACTIVATEAPP message can be received even when
  4516. // the main window is minimized to the tray and internal editor window is focused
  4517. // (after another application was previously active)
  4518. if (::IsIconic(Handle))
  4519. {
  4520. if (!IsAppIconic())
  4521. {
  4522. SetAppIconic(true);
  4523. }
  4524. }
  4525. ::ApplicationRestore();
  4526. Application->BringToFront();
  4527. }
  4528. //---------------------------------------------------------------------------
  4529. void __fastcall TCustomScpExplorerForm::TrayIconClick(TObject * /*Sender*/)
  4530. {
  4531. RestoreApp();
  4532. }
  4533. //---------------------------------------------------------------------------
  4534. void __fastcall TCustomScpExplorerForm::NewSession(const UnicodeString & SessionUrl)
  4535. {
  4536. if (OpenInNewWindow())
  4537. {
  4538. ExecuteNewInstance(SessionUrl);
  4539. }
  4540. else
  4541. {
  4542. TTerminalManager::Instance()->NewSession(SessionUrl);
  4543. }
  4544. }
  4545. //---------------------------------------------------------------------------
  4546. void TCustomScpExplorerForm::NewTab(TOperationSide)
  4547. {
  4548. NewSession();
  4549. }
  4550. //---------------------------------------------------------------------------
  4551. UnicodeString __fastcall TCustomScpExplorerForm::SaveHiddenDuplicateSession(TSessionData * SessionData)
  4552. {
  4553. UnicodeString SessionName = StoredSessions->HiddenPrefix + ManagedSession->SessionData->SessionName;
  4554. StoredSessions->NewSession(SessionName, SessionData);
  4555. // modified only, explicit
  4556. StoredSessions->Save(false, true);
  4557. // encode session name because of slashes in hierarchical sessions
  4558. return EncodeUrlString(SessionName);
  4559. }
  4560. //---------------------------------------------------------------------------
  4561. UnicodeString __fastcall TCustomScpExplorerForm::CreateHiddenDuplicateSession()
  4562. {
  4563. // current working directories become defaults here, what is not right
  4564. std::unique_ptr<TSessionData> SessionData(CloneCurrentSessionData());
  4565. return SaveHiddenDuplicateSession(SessionData.get());
  4566. }
  4567. //---------------------------------------------------------------------------
  4568. void __fastcall TCustomScpExplorerForm::DuplicateSession()
  4569. {
  4570. if (OpenInNewWindow())
  4571. {
  4572. ExecuteNewInstance(CreateHiddenDuplicateSession());
  4573. }
  4574. else
  4575. {
  4576. // current working directories become defaults here, what is not right
  4577. std::unique_ptr<TSessionData> SessionData(CloneCurrentSessionData());
  4578. TTerminalManager * Manager = TTerminalManager::Instance();
  4579. TManagedTerminal * ASession = Manager->NewManagedTerminal(SessionData.get());
  4580. DebugAssert(ASession->LocalBrowser == ManagedSession->LocalBrowser);
  4581. // We definitelly want these
  4582. ASession->Disconnected = ManagedSession->Disconnected;
  4583. // Not sure about these two
  4584. ASession->Permanent = ManagedSession->Permanent;
  4585. ASession->DisconnectedTemporarily = ManagedSession->DisconnectedTemporarily;
  4586. Manager->ActiveSession = ASession;
  4587. }
  4588. }
  4589. //---------------------------------------------------------------------------
  4590. void __fastcall TCustomScpExplorerForm::RenameSession()
  4591. {
  4592. UnicodeString Name = ManagedSession->SessionData->SessionName;
  4593. if (InputDialog(LoadStr(RENAME_SESSION_TITLE), LoadStr(RENAME_SESSION_PROMPT), Name, HELP_SESSION_RENAME) &&
  4594. // When submitting the default name, do not fix it
  4595. (Name != ManagedSession->SessionData->SessionName))
  4596. {
  4597. // Checks for a slash only, so it's not that big deal that we do the check after submitting the dialog only.
  4598. TSessionData::ValidateName(Name);
  4599. // This is inconsistent with how color (for example) is handled.
  4600. ManagedSession->SessionData->Name = Name;
  4601. UpdateControls();
  4602. // Add/Remove distinguishing paths from sessions of the same name.
  4603. SessionListChanged();
  4604. }
  4605. }
  4606. //---------------------------------------------------------------------------
  4607. bool __fastcall TCustomScpExplorerForm::CanCloseQueue(TTerminalQueue * Queue)
  4608. {
  4609. DebugAssert(Queue != NULL);
  4610. bool Result = Queue->IsEmpty;
  4611. if (!Result)
  4612. {
  4613. SetFocus();
  4614. Result = (MessageDialog(LoadStr(PENDING_QUEUE_ITEMS2), qtWarning, qaOK | qaCancel, HELP_NONE) == qaOK);
  4615. }
  4616. return Result;
  4617. }
  4618. //---------------------------------------------------------------------------
  4619. bool __fastcall TCustomScpExplorerForm::CanCloseQueue()
  4620. {
  4621. return CanCloseQueue(FQueue);
  4622. }
  4623. //---------------------------------------------------------------------------
  4624. void __fastcall TCustomScpExplorerForm::CloseSession()
  4625. {
  4626. if (CanCloseQueue())
  4627. {
  4628. TTerminalManager::Instance()->FreeActiveTerminal();
  4629. }
  4630. }
  4631. //---------------------------------------------------------------------------
  4632. void __fastcall TCustomScpExplorerForm::DisconnectSession()
  4633. {
  4634. if (CanCloseQueue())
  4635. {
  4636. TTerminalManager::Instance()->DisconnectActiveTerminal();
  4637. }
  4638. }
  4639. //---------------------------------------------------------------------------
  4640. void __fastcall TCustomScpExplorerForm::TerminalDisconnected()
  4641. {
  4642. DetachTerminal(Terminal);
  4643. RemoteDirView->Terminal = Terminal;
  4644. UpdateRemotePathComboBox(false);
  4645. InitStatusBar();
  4646. UpdateControls();
  4647. }
  4648. //---------------------------------------------------------------------------
  4649. void __fastcall TCustomScpExplorerForm::TerminalConnecting()
  4650. {
  4651. InitStatusBar();
  4652. }
  4653. //---------------------------------------------------------------------------
  4654. void __fastcall TCustomScpExplorerForm::ReconnectSession()
  4655. {
  4656. if (DebugAlwaysTrue(!Terminal->Active))
  4657. {
  4658. TTerminalManager::Instance()->ReconnectActiveTerminal();
  4659. }
  4660. }
  4661. //---------------------------------------------------------------------------
  4662. void __fastcall TCustomScpExplorerForm::OpenStoredSession(TSessionData * Data)
  4663. {
  4664. if (OpenInNewWindow())
  4665. {
  4666. // encode session name because of slashes in hierarchical sessions
  4667. ExecuteNewInstance(EncodeUrlString(Data->Name));
  4668. }
  4669. else
  4670. {
  4671. TTerminalManager * Manager = TTerminalManager::Instance();
  4672. TManagedTerminal * Terminal = Manager->NewManagedTerminal(Data);
  4673. Manager->ActiveSession = Terminal;
  4674. }
  4675. }
  4676. //---------------------------------------------------------------------------
  4677. void __fastcall TCustomScpExplorerForm::DoOpenFolderOrWorkspace(const UnicodeString & Name, bool ConnectFirstTerminal)
  4678. {
  4679. TTerminalManager * Manager = TTerminalManager::Instance();
  4680. std::unique_ptr<TObjectList> DataList(new TObjectList());
  4681. StoredSessions->GetFolderOrWorkspace(Name, DataList.get());
  4682. TManagedTerminal * FirstSession = Manager->NewSessions(DataList.get());
  4683. // FirstSession can be null, if some of the
  4684. if (!ConnectFirstTerminal && (FirstSession != NULL))
  4685. {
  4686. FirstSession->Disconnected = true;
  4687. FirstSession->DisconnectedTemporarily = true;
  4688. }
  4689. Manager->ActiveSession = FirstSession;
  4690. }
  4691. //---------------------------------------------------------------------------
  4692. void __fastcall TCustomScpExplorerForm::OpenFolderOrWorkspace(const UnicodeString & Name)
  4693. {
  4694. if (OpenInNewWindow())
  4695. {
  4696. ExecuteNewInstance(Name);
  4697. }
  4698. else
  4699. {
  4700. DoOpenFolderOrWorkspace(Name, true);
  4701. }
  4702. }
  4703. //---------------------------------------------------------------------------
  4704. void __fastcall TCustomScpExplorerForm::FormCloseQuery(TObject * /*Sender*/,
  4705. bool &CanClose)
  4706. {
  4707. // See the comment in CloseApp()
  4708. if (NonVisualDataModule->Busy)
  4709. {
  4710. CanClose = false;
  4711. }
  4712. else
  4713. {
  4714. TTerminalManager * Manager = TTerminalManager::Instance();
  4715. int ActiveSessions = 0;
  4716. for (int Index = 0; Index < Manager->Count; Index++)
  4717. {
  4718. if (Manager->Sessions[Index]->Active)
  4719. {
  4720. ActiveSessions++;
  4721. }
  4722. }
  4723. // Confirm, when there's at least one active remote session or
  4724. // when there are multiple (inactive or local) sessions and auto workspace saving is not enabled.
  4725. if (((ActiveSessions > 0) || ((Manager->Count > 1) && !WinConfiguration->AutoSaveWorkspace)) &&
  4726. WinConfiguration->ConfirmClosingSession)
  4727. {
  4728. unsigned int Result;
  4729. TMessageParams Params(mpNeverAskAgainCheck);
  4730. UnicodeString Message;
  4731. int Answers = qaOK | qaCancel;
  4732. // The current session is the only active session
  4733. if ((ActiveSessions == 1) && (Terminal != NULL) && Terminal->Active)
  4734. {
  4735. if (!WinConfiguration->AutoSaveWorkspace)
  4736. {
  4737. Message = LoadStr(CLOSE_SESSION_WORKSPACE);
  4738. }
  4739. else
  4740. {
  4741. Message = LoadStr(CLOSE_SESSION);
  4742. }
  4743. Message = FORMAT(Message, (Terminal->SessionData->SessionName));
  4744. }
  4745. // Multiple active sessions or one active session, but it's not the current one
  4746. else if (ActiveSessions > 0)
  4747. {
  4748. if (!WinConfiguration->AutoSaveWorkspace)
  4749. {
  4750. Message = LoadStr(CLOSE_SESSIONS_WORKSPACE3);
  4751. }
  4752. else
  4753. {
  4754. Message = LoadStr(CLOSE_SESSIONS);
  4755. }
  4756. }
  4757. // Multiple tabs, but all are intactive or local, and saving of worspace is not enabled
  4758. else
  4759. {
  4760. DebugAlwaysTrue(Manager->Count > 1);
  4761. DebugAlwaysTrue(!WinConfiguration->AutoSaveWorkspace);
  4762. Message = LoadStr(CLOSE_WORKSPACE);
  4763. }
  4764. UnicodeString Note;
  4765. if (WinConfiguration->AutoSaveWorkspace)
  4766. {
  4767. Note = FMTLOAD(AUTO_WORKSPACE, (WorkspaceName()));
  4768. }
  4769. else
  4770. {
  4771. Note = LoadStr(AUTO_WORKSPACE_ENABLE);
  4772. Answers = qaYes | qaNo | qaCancel;
  4773. }
  4774. Message = FORMAT("%s\n\n%s", (MainInstructions(Message), Note));
  4775. SetFocus();
  4776. Result = MessageDialog(Message, qtConfirmation,
  4777. Answers, HELP_CLOSE_SESSION_WORKSPACE, &Params);
  4778. if (Result == qaNeverAskAgain)
  4779. {
  4780. WinConfiguration->ConfirmClosingSession = false;
  4781. }
  4782. if (Result == qaNo)
  4783. {
  4784. CanClose = SaveWorkspace(true);
  4785. // note that the workspace will be saved redundatly again from FormClose
  4786. }
  4787. else
  4788. {
  4789. CanClose =
  4790. (Result == qaOK) ||
  4791. (Result == qaYes) || // CLOSE_SESSIONS_WORKSPACE variant
  4792. (Result == qaNeverAskAgain);
  4793. }
  4794. }
  4795. if (CanClose && HasActiveTerminal())
  4796. {
  4797. CanClose = CanCloseQueue();
  4798. }
  4799. }
  4800. if (CanClose)
  4801. {
  4802. CanClose =
  4803. FEditorManager->CloseInternalEditors(CloseInternalEditor) &&
  4804. FEditorManager->CloseExternalFilesWithoutProcess();
  4805. if (CanClose)
  4806. {
  4807. while (CanClose && (FLocalEditors->Count > 0))
  4808. {
  4809. int PrevCount = FLocalEditors->Count;
  4810. static_cast<TForm *>(FLocalEditors->Items[0])->Close();
  4811. CanClose = (FLocalEditors->Count < PrevCount);
  4812. }
  4813. if (CanClose)
  4814. {
  4815. CanClose = FEditorManager->Empty(true);
  4816. if (!CanClose)
  4817. {
  4818. SetFocus();
  4819. CanClose =
  4820. (MessageDialog(
  4821. LoadStr(PENDING_EDITORS), qtWarning, qaIgnore | qaCancel, HELP_NONE) == qaIgnore);
  4822. }
  4823. }
  4824. }
  4825. }
  4826. }
  4827. //---------------------------------------------------------------------------
  4828. void __fastcall TCustomScpExplorerForm::CloseInternalEditor(TObject * Sender)
  4829. {
  4830. TForm * Form = dynamic_cast<TForm *>(Sender);
  4831. DebugAssert(Form != NULL);
  4832. Form->Close();
  4833. }
  4834. //---------------------------------------------------------------------------
  4835. void __fastcall TCustomScpExplorerForm::ForceCloseInternalEditor(TObject * Sender)
  4836. {
  4837. TForm * Form = dynamic_cast<TForm *>(Sender);
  4838. delete Form;
  4839. }
  4840. //---------------------------------------------------------------------------
  4841. void __fastcall TCustomScpExplorerForm::ForceCloseLocalEditors()
  4842. {
  4843. while (FLocalEditors->Count > 0)
  4844. {
  4845. delete static_cast<TForm *>(FLocalEditors->Items[0]);
  4846. }
  4847. }
  4848. //---------------------------------------------------------------------------
  4849. void __fastcall TCustomScpExplorerForm::RemoteDirViewDisplayProperties(
  4850. TObject *Sender)
  4851. {
  4852. TStrings *FileList = ((TUnixDirView*)Sender)->CreateFileList(True, False, NULL);
  4853. try
  4854. {
  4855. SetProperties(osRemote, FileList);
  4856. }
  4857. __finally
  4858. {
  4859. delete FileList;
  4860. }
  4861. }
  4862. //---------------------------------------------------------------------------
  4863. void __fastcall TCustomScpExplorerForm::ComponentShowing(Byte Component, bool value)
  4864. {
  4865. if (value)
  4866. {
  4867. if (Component == fcCustomCommandsBand)
  4868. {
  4869. UpdateCustomCommandsToolbar();
  4870. }
  4871. if (Component == fcQueueFileList)
  4872. {
  4873. DebugAssert(!QueueFileListSplitter->Visible);
  4874. AdjustQueueLayout();
  4875. UpdateQueueFileList();
  4876. }
  4877. }
  4878. }
  4879. //---------------------------------------------------------------------------
  4880. void __fastcall TCustomScpExplorerForm::AdjustQueueLayout()
  4881. {
  4882. int MinHeightLimit =
  4883. GetStaticQueuePanelComponentsHeight() +
  4884. QueueFileListSplitter->Height +
  4885. QueueFileList->Height +
  4886. GetMinQueueViewHeight();
  4887. if (QueuePanel->ClientHeight < MinHeightLimit)
  4888. {
  4889. QueuePanel->ClientHeight = MinHeightLimit;
  4890. }
  4891. }
  4892. //---------------------------------------------------------------------------
  4893. void __fastcall TCustomScpExplorerForm::SetComponentVisible(Byte Component, Boolean value)
  4894. {
  4895. TControl * Control = GetComponent(Component);
  4896. DebugAssert(Control);
  4897. bool Changed = (Control->Visible != value);
  4898. if (Changed)
  4899. {
  4900. ComponentShowing(Component, value);
  4901. TWinControl * WinControl = dynamic_cast<TWinControl*>(Control);
  4902. bool WasFocused = (WinControl != NULL) && (ActiveControl != NULL) &&
  4903. ((ActiveControl == WinControl) || (ActiveControl->Parent == WinControl));
  4904. if (value)
  4905. {
  4906. int RemainingHeight = Control->Parent->ClientHeight;
  4907. int RemainingWidth = Control->Parent->ClientWidth;
  4908. for (int i = 0; i < Control->Parent->ControlCount; i++)
  4909. {
  4910. TControl * ChildControl = Control->Parent->Controls[i];
  4911. if (ChildControl->Visible)
  4912. {
  4913. switch (ChildControl->Align)
  4914. {
  4915. case alTop:
  4916. case alBottom:
  4917. RemainingHeight -= ChildControl->Height;
  4918. break;
  4919. case alLeft:
  4920. case alRight:
  4921. RemainingWidth -= ChildControl->Width;
  4922. break;
  4923. }
  4924. }
  4925. }
  4926. int Reserve = ScaleByTextHeight(this, 32);
  4927. // queue in explorer, trees in commander
  4928. if (Control->Height > RemainingHeight - Reserve)
  4929. {
  4930. Control->Height = RemainingHeight / 2;
  4931. }
  4932. if (Control->Width > RemainingWidth - Reserve)
  4933. {
  4934. Control->Width = RemainingWidth / 2;
  4935. }
  4936. }
  4937. Control->Visible = value;
  4938. if (WasFocused && Visible)
  4939. {
  4940. DirView(osCurrent)->SetFocus();
  4941. }
  4942. FixControlsPlacement();
  4943. }
  4944. }
  4945. //---------------------------------------------------------------------------
  4946. bool __fastcall TCustomScpExplorerForm::GetComponentVisible(Byte Component)
  4947. {
  4948. TControl * Control = GetComponent(Component);
  4949. if (Control == NULL)
  4950. {
  4951. return false;
  4952. }
  4953. else
  4954. {
  4955. return Control->Visible;
  4956. }
  4957. }
  4958. //---------------------------------------------------------------------------
  4959. bool __fastcall TCustomScpExplorerForm::IsComponentPossible(Byte Component)
  4960. {
  4961. bool Result;
  4962. if ((Component == fcExplorerUpdatesBand) || (Component == fcCommanderUpdatesBand))
  4963. {
  4964. Result = !IsUWP();
  4965. }
  4966. else
  4967. {
  4968. Result = true;
  4969. }
  4970. return Result;
  4971. }
  4972. //---------------------------------------------------------------------------
  4973. void __fastcall TCustomScpExplorerForm::MakeFocusedItemVisible(TCustomDirView * DirView)
  4974. {
  4975. if (DirView->ItemFocused != NULL)
  4976. {
  4977. DirView->ItemFocused->MakeVisible(false);
  4978. }
  4979. }
  4980. //---------------------------------------------------------------------------
  4981. void __fastcall TCustomScpExplorerForm::FixControlsPlacement()
  4982. {
  4983. MakeFocusedItemVisible(RemoteDirView);
  4984. QueueSplitter->Visible = QueuePanel->Visible;
  4985. QueueFileListSplitter->Visible = QueueFileList->Visible;
  4986. RemotePanelSplitter->Visible = RemoteDrivePanel->Visible;
  4987. TControl * QueueControlsOrder[] =
  4988. { QueueDock, QueueView3, QueueFileListSplitter, QueueFileList };
  4989. SetVerticalControlsOrder(QueueControlsOrder, LENOF(QueueControlsOrder));
  4990. }
  4991. //---------------------------------------------------------------------------
  4992. TControl * __fastcall TCustomScpExplorerForm::GetComponent(Byte Component)
  4993. {
  4994. switch (Component) {
  4995. case fcStatusBar: return RemoteStatusBar;
  4996. case fcRemotePopup: return reinterpret_cast<TControl *>(NonVisualDataModule->RemoteFilePopup);
  4997. case fcQueueView: return QueuePanel;
  4998. case fcQueueToolbar: return QueueDock;
  4999. case fcRemoteTree: return RemoteDrivePanel;
  5000. case fcSessionsTabs: return SessionsPageControl;
  5001. case fcQueueFileList: return QueueFileList;
  5002. default: return NULL;
  5003. }
  5004. }
  5005. //---------------------------------------------------------------------------
  5006. void __fastcall TCustomScpExplorerForm::DirViewColumnRightClick(
  5007. TObject *Sender, TListColumn *Column, TPoint &Point)
  5008. {
  5009. DebugAssert(NonVisualDataModule && Column && Sender);
  5010. NonVisualDataModule->ListColumn = Column;
  5011. TPopupMenu * DirViewColumnMenu;
  5012. if (Sender == DirView(osOther))
  5013. {
  5014. DirViewColumnMenu = NonVisualDataModule->RemoteDirViewColumnPopup;
  5015. NonVisualDataModule->RemoteSortByExtColumnPopupItem->Visible =
  5016. (Column->Index == uvName);
  5017. NonVisualDataModule->RemoteFormatSizeBytesPopupItem->Visible =
  5018. (Column->Index == uvSize);
  5019. }
  5020. else
  5021. {
  5022. DirViewColumnMenu = NonVisualDataModule->LocalDirViewColumnPopup;
  5023. NonVisualDataModule->LocalSortByExtColumnPopupItem->Visible =
  5024. (Column->Index == dvName);
  5025. NonVisualDataModule->LocalFormatSizeBytesPopupItem->Visible =
  5026. (Column->Index == dvSize);
  5027. }
  5028. TCustomListView * ListView = DebugNotNull(dynamic_cast<TCustomListView *>(Sender));
  5029. TPoint ScreenPoint = ListView->ClientToScreen(Point);
  5030. ScreenPoint.x -= GetScrollPos(ListView->Handle, SB_HORZ);
  5031. DirViewColumnMenu->Popup(ScreenPoint.x, ScreenPoint.y);
  5032. }
  5033. //---------------------------------------------------------------------------
  5034. void __fastcall TCustomScpExplorerForm::DirViewExecFile(
  5035. TObject *Sender, TListItem *Item, bool &AllowExec)
  5036. {
  5037. DoDirViewExecFile(Sender, Item, AllowExec);
  5038. }
  5039. //---------------------------------------------------------------------------
  5040. void __fastcall TCustomScpExplorerForm::DoDirViewExecFile(TObject * Sender,
  5041. TListItem * Item, bool & AllowExec)
  5042. {
  5043. DebugAssert(Sender && Item && Configuration);
  5044. DebugAssert(AllowExec);
  5045. TCustomDirView * ADirView = (TCustomDirView *)Sender;
  5046. TOperationSide Side = ((ADirView == DirView(osRemote)) ? osRemote : osLocal);
  5047. bool Remote = !IsSideLocalBrowser(Side);
  5048. bool ResolvedSymlinks = !Remote || Terminal->ResolvingSymlinks || Terminal->IsEncryptingFiles();
  5049. // Anything special is done on double click only (not on "open" indicated by FForceExecution),
  5050. // on files only (not directories)
  5051. // and only when symlinks are resolved (apply to remote panel only)
  5052. if (!ADirView->ItemIsDirectory(Item) &&
  5053. (ResolvedSymlinks || FForceExecution))
  5054. {
  5055. if ((WinConfiguration->DoubleClickAction != dcaOpen) &&
  5056. !FForceExecution && ResolvedSymlinks)
  5057. {
  5058. if (WinConfiguration->DoubleClickAction == dcaCopy)
  5059. {
  5060. // To counter the lock in DirViewBusy (called from TCustomDirView.DoExecute)
  5061. UnlockWindow();
  5062. try
  5063. {
  5064. if (IsLocalBrowserMode())
  5065. {
  5066. LocalLocalCopy(foCopy, Side, true, !WinConfiguration->CopyOnDoubleClickConfirmation, false, 0);
  5067. }
  5068. else
  5069. {
  5070. ExecuteFileOperation(foCopy, Side, true, !WinConfiguration->CopyOnDoubleClickConfirmation);
  5071. }
  5072. }
  5073. __finally
  5074. {
  5075. LockWindow();
  5076. }
  5077. AllowExec = false;
  5078. }
  5079. else if (WinConfiguration->DoubleClickAction == dcaEdit)
  5080. {
  5081. if (!Remote || !WinConfiguration->DisableOpenEdit)
  5082. {
  5083. ExecuteFile(Side, efDefaultEditor);
  5084. AllowExec = false;
  5085. }
  5086. }
  5087. else
  5088. {
  5089. DebugFail();
  5090. }
  5091. }
  5092. // if we have not done anything special, fall back to default behavior
  5093. if (AllowExec)
  5094. {
  5095. if (Remote && !WinConfiguration->DisableOpenEdit)
  5096. {
  5097. ExecuteFile(osRemote, efShell);
  5098. AllowExec = false;
  5099. }
  5100. }
  5101. }
  5102. }
  5103. //---------------------------------------------------------------------------
  5104. bool __fastcall TCustomScpExplorerForm::GetHasDirView(TOperationSide Side)
  5105. {
  5106. return ((Side == osRemote) || (Side == osCurrent));
  5107. }
  5108. //---------------------------------------------------------------------------
  5109. void __fastcall TCustomScpExplorerForm::CompareDirectories()
  5110. {
  5111. DebugFail();
  5112. }
  5113. //---------------------------------------------------------------------------
  5114. void __fastcall TCustomScpExplorerForm::SynchronizeDirectories()
  5115. {
  5116. DebugFail();
  5117. }
  5118. //---------------------------------------------------------------------------
  5119. bool __fastcall TCustomScpExplorerForm::DoSynchronizeDirectories(
  5120. UnicodeString & LocalDirectory, UnicodeString & RemoteDirectory, int UseDefaults)
  5121. {
  5122. TSynchronizeParamType Params;
  5123. Params.LocalDirectory = LocalDirectory;
  5124. Params.RemoteDirectory = RemoteDirectory;
  5125. int UnusedParams =
  5126. (GUIConfiguration->SynchronizeParams &
  5127. (TTerminal::spPreviewChanges | TTerminal::spTimestamp | TTerminal::spNotByTime | TTerminal::spBySize |
  5128. TTerminal::spCaseSensitive));
  5129. Params.Params = GUIConfiguration->SynchronizeParams & ~UnusedParams;
  5130. Params.Options = GUIConfiguration->SynchronizeOptions;
  5131. bool SaveSettings = false;
  5132. TSynchronizeController Controller(&DoSynchronize, &DoSynchronizeInvalid,
  5133. &DoSynchronizeTooManyDirectories);
  5134. DebugAssert(FSynchronizeController == NULL);
  5135. FSynchronizeController = &Controller;
  5136. bool Result;
  5137. try
  5138. {
  5139. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  5140. int CopyParamAttrs = Terminal->UsableCopyParamAttrs(0).Upload;
  5141. int Options =
  5142. FLAGMASK(SynchronizeAllowSelectedOnly(), soAllowSelectedOnly);
  5143. DebugAssert(FOnFeedSynchronizeError == NULL);
  5144. Result = DoSynchronizeDialog(Params, &CopyParam, Controller.StartStop,
  5145. SaveSettings, Options, CopyParamAttrs, GetSynchronizeOptions, SynchronizeSessionLog,
  5146. FOnFeedSynchronizeError, SynchronizeInNewWindow, UseDefaults);
  5147. if (Result)
  5148. {
  5149. if (SaveSettings)
  5150. {
  5151. GUIConfiguration->SynchronizeParams = Params.Params | UnusedParams;
  5152. GUIConfiguration->SynchronizeOptions = Params.Options;
  5153. }
  5154. else
  5155. {
  5156. if (FLAGSET(GUIConfiguration->SynchronizeOptions, soSynchronizeAsk) &&
  5157. FLAGCLEAR(Params.Options, soSynchronizeAsk) &&
  5158. FLAGSET(Params.Options, soSynchronize))
  5159. {
  5160. GUIConfiguration->SynchronizeOptions =
  5161. (GUIConfiguration->SynchronizeOptions & ~soSynchronizeAsk) |
  5162. soSynchronize;
  5163. }
  5164. }
  5165. LocalDirectory = Params.LocalDirectory;
  5166. RemoteDirectory = Params.RemoteDirectory;
  5167. }
  5168. }
  5169. __finally
  5170. {
  5171. FSynchronizeController = NULL;
  5172. DebugAssert(FOnFeedSynchronizeError == NULL);
  5173. FOnFeedSynchronizeError = NULL;
  5174. }
  5175. return Result;
  5176. }
  5177. //---------------------------------------------------------------------------
  5178. void __fastcall TCustomScpExplorerForm::DoSynchronize(
  5179. TSynchronizeController * /*Sender*/, const UnicodeString LocalDirectory,
  5180. const UnicodeString RemoteDirectory, const TCopyParamType & CopyParam,
  5181. const TSynchronizeParamType & Params, TSynchronizeChecklist ** Checklist,
  5182. TSynchronizeOptions * Options, bool Full)
  5183. {
  5184. // Keep-up-to-date mode
  5185. if (Terminal->Status == ssOpened)
  5186. {
  5187. try
  5188. {
  5189. int PParams = Params.Params;
  5190. if (!Full)
  5191. {
  5192. PParams |= TTerminal::spNoRecurse | TTerminal::spUseCache | TTerminal::spDelayProgress;
  5193. if (FLAGSET(Params.Options, soRecurse))
  5194. {
  5195. PParams |= TTerminal::spSubDirs;
  5196. }
  5197. }
  5198. else
  5199. {
  5200. // if keepuptodate is non-recursive,
  5201. // full sync before has to be non-recursive as well
  5202. if (FLAGCLEAR(Params.Options, soRecurse))
  5203. {
  5204. PParams |= TTerminal::spNoRecurse;
  5205. }
  5206. }
  5207. Synchronize(LocalDirectory, RemoteDirectory, smRemote, CopyParam,
  5208. PParams, Checklist, Options);
  5209. }
  5210. catch (Exception & E)
  5211. {
  5212. if (FLAGSET(Params.Options, soContinueOnError) && Terminal->Active &&
  5213. DebugAlwaysTrue(FOnFeedSynchronizeError != NULL))
  5214. {
  5215. // noop, already logged in MoreMessageDialog
  5216. }
  5217. else
  5218. {
  5219. ShowExtendedExceptionEx(Terminal, &E);
  5220. throw;
  5221. }
  5222. }
  5223. }
  5224. }
  5225. //---------------------------------------------------------------------------
  5226. void __fastcall TCustomScpExplorerForm::DoSynchronizeInvalid(
  5227. TSynchronizeController * /*Sender*/, const UnicodeString Directory,
  5228. const UnicodeString ErrorStr)
  5229. {
  5230. if (!Directory.IsEmpty())
  5231. {
  5232. SimpleErrorDialog(FMTLOAD(WATCH_ERROR_DIRECTORY, (Directory)), ErrorStr);
  5233. }
  5234. else
  5235. {
  5236. SimpleErrorDialog(LoadStr(WATCH_ERROR_GENERAL), ErrorStr);
  5237. }
  5238. }
  5239. //---------------------------------------------------------------------------
  5240. void __fastcall TCustomScpExplorerForm::DoSynchronizeTooManyDirectories(
  5241. TSynchronizeController * /*Sender*/, int & MaxDirectories)
  5242. {
  5243. if (MaxDirectories < GUIConfiguration->MaxWatchDirectories)
  5244. {
  5245. MaxDirectories = GUIConfiguration->MaxWatchDirectories;
  5246. }
  5247. else
  5248. {
  5249. TMessageParams Params(mpNeverAskAgainCheck);
  5250. unsigned int Result = MessageDialog(
  5251. FMTLOAD(TOO_MANY_WATCH_DIRECTORIES, (MaxDirectories, MaxDirectories)),
  5252. qtConfirmation, qaYes | qaNo, HELP_TOO_MANY_WATCH_DIRECTORIES, &Params);
  5253. if ((Result == qaYes) || (Result == qaNeverAskAgain))
  5254. {
  5255. MaxDirectories *= 2;
  5256. if (Result == qaNeverAskAgain)
  5257. {
  5258. GUIConfiguration->MaxWatchDirectories = MaxDirectories;
  5259. }
  5260. }
  5261. else
  5262. {
  5263. Abort();
  5264. }
  5265. }
  5266. }
  5267. //---------------------------------------------------------------------------
  5268. void __fastcall TCustomScpExplorerForm::Synchronize(const UnicodeString LocalDirectory,
  5269. const UnicodeString RemoteDirectory, TSynchronizeMode Mode,
  5270. const TCopyParamType & CopyParam, int Params, TSynchronizeChecklist ** Checklist,
  5271. TSynchronizeOptions * Options)
  5272. {
  5273. // Keep-up-to-date mode
  5274. DebugAssert(!FAutoOperation);
  5275. void * BatchStorage;
  5276. BatchStart(BatchStorage);
  5277. FAutoOperation = true;
  5278. bool AnyOperation = false;
  5279. TDateTime StartTime = Now();
  5280. TSynchronizeChecklist * AChecklist = NULL;
  5281. try
  5282. {
  5283. FSynchronizeProgressForm = new TSynchronizeProgressForm(Application, true, -1);
  5284. if (FLAGCLEAR(Params, TTerminal::spDelayProgress))
  5285. {
  5286. FSynchronizeProgressForm->Start();
  5287. }
  5288. AChecklist = Terminal->SynchronizeCollect(LocalDirectory, RemoteDirectory,
  5289. static_cast<TTerminal::TSynchronizeMode>(Mode),
  5290. &CopyParam, Params | TTerminal::spNoConfirmation, TerminalSynchronizeDirectory,
  5291. Options);
  5292. SetTaskbarListProgressState(TBPF_NOPROGRESS);
  5293. SAFE_DESTROY(FSynchronizeProgressForm);
  5294. AnyOperation = (AChecklist->CheckedCount > 0);
  5295. if (AnyOperation)
  5296. {
  5297. TSynchronizeProgress SynchronizeProgress(AChecklist);
  5298. CreateProgressForm(&SynchronizeProgress);
  5299. try
  5300. {
  5301. Terminal->SynchronizeApply(
  5302. AChecklist, &CopyParam, Params | TTerminal::spNoConfirmation, TerminalSynchronizeDirectory,
  5303. SynchronizeProcessedItem, NULL, NULL, NULL);
  5304. }
  5305. __finally
  5306. {
  5307. DestroyProgressForm();
  5308. }
  5309. }
  5310. }
  5311. __finally
  5312. {
  5313. if (Checklist == NULL)
  5314. {
  5315. delete AChecklist;
  5316. }
  5317. else
  5318. {
  5319. *Checklist = AChecklist;
  5320. }
  5321. FAutoOperation = false;
  5322. SAFE_DESTROY(FSynchronizeProgressForm);
  5323. BatchEnd(BatchStorage);
  5324. ReloadLocalDirectory();
  5325. if (AnyOperation)
  5326. {
  5327. OperationComplete(StartTime);
  5328. }
  5329. }
  5330. }
  5331. //---------------------------------------------------------------------------
  5332. bool __fastcall TCustomScpExplorerForm::SynchronizeAllowSelectedOnly()
  5333. {
  5334. // can be called from command line
  5335. return Visible &&
  5336. ((DirView(osRemote)->SelCount > 0) ||
  5337. (HasDirView[osLocal] && (DirView(osLocal)->SelCount > 0)));
  5338. }
  5339. //---------------------------------------------------------------------------
  5340. void __fastcall TCustomScpExplorerForm::SynchronizeSessionLog(const UnicodeString & Message)
  5341. {
  5342. LogSynchronizeEvent(Terminal, Message);
  5343. }
  5344. //---------------------------------------------------------------------------
  5345. void __fastcall TCustomScpExplorerForm::GetSynchronizeOptions(
  5346. int Params, TSynchronizeOptions & Options)
  5347. {
  5348. DebugAssert(!IsLocalBrowserMode());
  5349. if (FLAGSET(Params, TTerminal::spSelectedOnly) && SynchronizeAllowSelectedOnly())
  5350. {
  5351. Options.Filter = new TStringList();
  5352. Options.Filter->CaseSensitive = false;
  5353. Options.Filter->Duplicates = Types::dupAccept;
  5354. if (DirView(osRemote)->SelCount > 0)
  5355. {
  5356. DirView(osRemote)->CreateFileList(false, false, Options.Filter);
  5357. }
  5358. if (HasDirView[osLocal] && (DirView(osLocal)->SelCount > 0))
  5359. {
  5360. DirView(osLocal)->CreateFileList(false, false, Options.Filter);
  5361. }
  5362. Options.Filter->Sort();
  5363. }
  5364. }
  5365. //---------------------------------------------------------------------------
  5366. UnicodeString __fastcall TCustomScpExplorerForm::SerializeCopyParamForCommandLine(const TCopyParamType * CopyParams)
  5367. {
  5368. TCopyParamType Defaults;
  5369. std::unique_ptr<THierarchicalStorage> ConfigStorage(Configuration->CreateConfigStorage());
  5370. ConfigStorage->AccessMode = smRead;
  5371. if (ConfigStorage->OpenSubKey(Configuration->ConfigurationSubKey, false))
  5372. {
  5373. GUIConfiguration->LoadCopyParam(ConfigStorage.get(), &Defaults);
  5374. }
  5375. ConfigStorage.reset(NULL);
  5376. std::unique_ptr<TStringList> Options(new TStringList());
  5377. std::unique_ptr<TOptionsStorage> OptionsStorage(new TOptionsStorage(Options.get(), true));
  5378. CopyParams->Save(OptionsStorage.get(), &Defaults);
  5379. UnicodeString Result;
  5380. if (Options->Count > 0)
  5381. {
  5382. Result =
  5383. FORMAT(L" %s%s", (TProgramParams::FormatSwitch(RAWTRANSFERSETTINGS_SWITCH), StringsToParams(Options.get())));
  5384. }
  5385. return Result;
  5386. }
  5387. //---------------------------------------------------------------------------
  5388. void __fastcall TCustomScpExplorerForm::SynchronizeInNewWindow(
  5389. const TSynchronizeParamType & Params, const TCopyParamType * CopyParams)
  5390. {
  5391. UnicodeString SessionName = CreateHiddenDuplicateSession();
  5392. UnicodeString AdditionalParams =
  5393. FORMAT(L"%s%s %s // \"%s\" \"%s\" %d %d", (
  5394. TProgramParams::FormatSwitch(DEFAULTS_SWITCH),
  5395. SerializeCopyParamForCommandLine(CopyParams),
  5396. TProgramParams::FormatSwitch(KEEP_UP_TO_DATE_SWITCH),
  5397. Params.LocalDirectory, Params.RemoteDirectory, Params.Params, Params.Options));
  5398. ExecuteNewInstance(SessionName, AdditionalParams);
  5399. }
  5400. //---------------------------------------------------------------------------
  5401. struct TSynchronizeParams
  5402. {
  5403. UnicodeString LocalDirectory;
  5404. UnicodeString RemoteDirectory;
  5405. TSynchronizeMode Mode;
  5406. TSynchronizeChecklist * Checklist;
  5407. int Params;
  5408. TCopyParamType * CopyParam;
  5409. TDateTime CollectElapsed;
  5410. TDateTime * StartTime;
  5411. TProcessedSynchronizationChecklistItem OnProcessedItem;
  5412. };
  5413. //---------------------------------------------------------------------------
  5414. void __fastcall TCustomScpExplorerForm::FullSynchronize(
  5415. TSynchronizeParams & Params, TProcessedSynchronizationChecklistItem OnProcessedItem,
  5416. TUpdatedSynchronizationChecklistItems OnUpdatedSynchronizationChecklistItems)
  5417. {
  5418. DebugAssert(!FAutoOperation);
  5419. void * BatchStorage;
  5420. BatchStart(BatchStorage);
  5421. FAutoOperation = true;
  5422. TFileOperationStatistics Statistics;
  5423. TDateTime Start = Now();
  5424. try
  5425. {
  5426. Params.OnProcessedItem = OnProcessedItem;
  5427. TSynchronizeProgress SynchronizeProgress(Params.Checklist);
  5428. CreateProgressForm(&SynchronizeProgress);
  5429. Terminal->SynchronizeApply(
  5430. Params.Checklist, Params.CopyParam, Params.Params | TTerminal::spNoConfirmation,
  5431. TerminalSynchronizeDirectory, SynchronizeProcessedItem, OnUpdatedSynchronizationChecklistItems, &Params,
  5432. &Statistics);
  5433. }
  5434. __finally
  5435. {
  5436. Params.OnProcessedItem = NULL;
  5437. FAutoOperation = false;
  5438. DestroyProgressForm();
  5439. BatchEnd(BatchStorage);
  5440. ReloadLocalDirectory();
  5441. }
  5442. if (WinConfiguration->SynchronizeSummary)
  5443. {
  5444. UnicodeString Message = MainInstructions(LoadStr(SYNCHRONIZE_COMPLETE)) + L"\n";
  5445. // The statistics should be 0 anyway in this case
  5446. if (FLAGCLEAR(Params.Params, TTerminal::spTimestamp))
  5447. {
  5448. Message += L"\n";
  5449. if (Statistics.FilesUploaded > 0)
  5450. {
  5451. Message += FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 1), (FormatNumber(Statistics.FilesUploaded), FormatBytes(Statistics.TotalUploaded))) + L"\n";
  5452. }
  5453. if (Statistics.FilesDownloaded > 0)
  5454. {
  5455. Message += FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 2), (FormatNumber(Statistics.FilesDownloaded), FormatBytes(Statistics.TotalDownloaded))) + L"\n";
  5456. }
  5457. if (Statistics.FilesDeletedLocal > 0)
  5458. {
  5459. Message += FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 3), (FormatNumber(Statistics.FilesDeletedLocal))) + L"\n";
  5460. }
  5461. if (Statistics.FilesDeletedRemote > 0)
  5462. {
  5463. Message += FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 4), (FormatNumber(Statistics.FilesDeletedRemote))) + L"\n";
  5464. }
  5465. }
  5466. TDateTime Elapsed = (Now() - Start);
  5467. Message +=
  5468. L"\n" +
  5469. FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 5), (FormatDateTimeSpan(Configuration->TimeFormat, Params.CollectElapsed))) + L"\n" +
  5470. FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 6), (FormatDateTimeSpan(Configuration->TimeFormat, Elapsed)));
  5471. TMessageParams Params(mpNeverAskAgainCheck);
  5472. unsigned int Result = MessageDialog(Message, qtInformation, qaOK, HELP_NONE, &Params);
  5473. if (Result == qaNeverAskAgain)
  5474. {
  5475. WinConfiguration->SynchronizeSummary = false;
  5476. }
  5477. }
  5478. }
  5479. //---------------------------------------------------------------------------
  5480. void __fastcall TCustomScpExplorerForm::SynchronizeProcessedItem(void * Token, const TSynchronizeChecklist::TItem * ChecklistItem)
  5481. {
  5482. if (DebugAlwaysTrue(FProgressForm != NULL) && DebugAlwaysTrue(FProgressForm->SynchronizeProgress != NULL))
  5483. {
  5484. FProgressForm->SynchronizeProgress->ItemProcessed(ChecklistItem);
  5485. }
  5486. // Not set in keep-up-to-date mode - Synchronize() method
  5487. if (Token != NULL)
  5488. {
  5489. TSynchronizeParams & Params = *static_cast<TSynchronizeParams *>(Token);
  5490. if (Params.OnProcessedItem != NULL)
  5491. {
  5492. Params.OnProcessedItem(NULL, ChecklistItem);
  5493. }
  5494. }
  5495. }
  5496. //---------------------------------------------------------------------------
  5497. void __fastcall TCustomScpExplorerForm::DoFullSynchronize(
  5498. void * Token, TProcessedSynchronizationChecklistItem OnProcessedItem,
  5499. TUpdatedSynchronizationChecklistItems OnUpdatedSynchronizationChecklistItems)
  5500. {
  5501. TSynchronizeParams & Params = *static_cast<TSynchronizeParams *>(Token);
  5502. *Params.StartTime = Now();
  5503. FullSynchronize(Params, OnProcessedItem, OnUpdatedSynchronizationChecklistItems);
  5504. }
  5505. //---------------------------------------------------------------------------
  5506. void __fastcall TCustomScpExplorerForm::DoSynchronizeChecklistCalculateSize(
  5507. TSynchronizeChecklist * Checklist, const TSynchronizeChecklist::TItemList & Items, void * Token)
  5508. {
  5509. // terminal can be already closed (e.g. dropped connection)
  5510. if (Terminal != NULL)
  5511. {
  5512. TSynchronizeParams & Params = *static_cast<TSynchronizeParams *>(Token);
  5513. Terminal->SynchronizeChecklistCalculateSize(Checklist, Items, Params.CopyParam);
  5514. }
  5515. }
  5516. //---------------------------------------------------------------------------
  5517. void __fastcall TCustomScpExplorerForm::DoSynchronizeMove(
  5518. TOperationSide Side, const UnicodeString & FileName, const UnicodeString & NewFileName, TRemoteFile * RemoteFile)
  5519. {
  5520. DebugAssert(!IsLocalBrowserMode());
  5521. TAutoBatch AutoBatch(this);
  5522. TAutoFlag AutoOperationFlag(FAutoOperation);
  5523. if (Side == osRemote)
  5524. {
  5525. std::unique_ptr<TStrings> FileList(new TStringList());
  5526. FileList->AddObject(FileName, RemoteFile);
  5527. UnicodeString Target = UnixExtractFileDir(NewFileName);
  5528. UnicodeString FileMask = DelimitFileNameMask(UnixExtractFileName(NewFileName));
  5529. RemoteDirView->SaveSelection();
  5530. RemoteDirView->SaveSelectedNames();
  5531. try
  5532. {
  5533. Terminal->MoveFiles(FileList.get(), Target, FileMask);
  5534. }
  5535. catch(...)
  5536. {
  5537. RemoteDirView->DiscardSavedSelection();
  5538. throw;
  5539. }
  5540. RemoteDirView->RestoreSelection();
  5541. }
  5542. else if (DebugAlwaysTrue(Side == osLocal))
  5543. {
  5544. if (!MoveFile(FileName.c_str(), NewFileName.c_str()))
  5545. {
  5546. throw EOSExtException(FMTLOAD(RENAME_FILE_ERROR, (FileName, NewFileName)));
  5547. }
  5548. UnicodeString Directory = ExtractFileDir(FileName);
  5549. ReloadLocalDirectory(Directory);
  5550. UnicodeString NewDirectory = ExtractFileDir(NewFileName);
  5551. if (!SamePaths(Directory, NewDirectory))
  5552. {
  5553. ReloadLocalDirectory(NewDirectory);
  5554. }
  5555. }
  5556. }
  5557. //---------------------------------------------------------------------------
  5558. void __fastcall TCustomScpExplorerForm::DoSynchronizeBrowse(TOperationSide Side, TSynchronizeChecklist::TAction Action, const TSynchronizeChecklist::TItem * Item)
  5559. {
  5560. UnicodeString LocalPath = ExcludeTrailingBackslash(Item->Local.Directory);
  5561. if (Side == osLocal)
  5562. {
  5563. if (Action == TSynchronizeChecklist::saDownloadNew)
  5564. {
  5565. OpenFolderInExplorer(LocalPath);
  5566. }
  5567. else
  5568. {
  5569. OpenFileInExplorer(TPath::Combine(LocalPath, Item->GetFileName()));
  5570. }
  5571. }
  5572. else if (DebugAlwaysTrue(Side == osRemote))
  5573. {
  5574. // Similar to CreateHiddenDuplicateSession, except that it modifies the initial directories
  5575. std::unique_ptr<TSessionData> SessionData(CloneCurrentSessionData());
  5576. SessionData->RemoteDirectory = UnixExcludeTrailingBackslash(Item->Remote.Directory);
  5577. if (!LocalPath.IsEmpty())
  5578. {
  5579. SessionData->LocalDirectory = LocalPath;
  5580. }
  5581. UnicodeString SessionName = SaveHiddenDuplicateSession(SessionData.get());
  5582. ExecuteNewInstance(SessionName, FORMAT(L"%s=%s", (TProgramParams::FormatSwitch(BROWSE_SWITCH), Item->GetFileName())));
  5583. }
  5584. }
  5585. //---------------------------------------------------------------------------
  5586. void __fastcall TCustomScpExplorerForm::FullSynchronizeInNewWindow(
  5587. TSynchronizeMode Mode, int Params, const UnicodeString & LocalDirectory, const UnicodeString & RemoteDirectory,
  5588. const TCopyParamType * CopyParams)
  5589. {
  5590. UnicodeString SessionName = CreateHiddenDuplicateSession();
  5591. UnicodeString AdditionalParams =
  5592. FORMAT(L"%s%s %s // \"%s\" \"%s\" %d %d", (
  5593. TProgramParams::FormatSwitch(DEFAULTS_SWITCH),
  5594. SerializeCopyParamForCommandLine(CopyParams),
  5595. TProgramParams::FormatSwitch(SYNCHRONIZE_SWITCH),
  5596. LocalDirectory, RemoteDirectory, Mode, Params));
  5597. ExecuteNewInstance(SessionName, AdditionalParams);
  5598. }
  5599. //---------------------------------------------------------------------------
  5600. int __fastcall TCustomScpExplorerForm::DoFullSynchronizeDirectories(
  5601. UnicodeString & LocalDirectory, UnicodeString & RemoteDirectory,
  5602. TSynchronizeMode & Mode, int Params, bool & SaveMode, int UseDefaults)
  5603. {
  5604. int Result;
  5605. bool SaveSettings = false;
  5606. int Options =
  5607. FLAGMASK(!Terminal->IsCapable[fcTimestampChanging], fsoDisableTimestamp) |
  5608. FLAGMASK(SynchronizeAllowSelectedOnly(), fsoAllowSelectedOnly);
  5609. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  5610. TUsableCopyParamAttrs CopyParamAttrs = Terminal->UsableCopyParamAttrs(0);
  5611. bool Continue =
  5612. (UseDefaults == 0) ||
  5613. DoFullSynchronizeDialog(Mode, Params, LocalDirectory, RemoteDirectory,
  5614. &CopyParam, SaveSettings, SaveMode, Options, CopyParamAttrs, FullSynchronizeInNewWindow, UseDefaults);
  5615. if (Continue)
  5616. {
  5617. Configuration->Usage->Inc(L"Synchronizations");
  5618. UpdateCopyParamCounters(CopyParam);
  5619. TSynchronizeOptions SynchronizeOptions;
  5620. GetSynchronizeOptions(Params, SynchronizeOptions);
  5621. if (SaveSettings)
  5622. {
  5623. GUIConfiguration->SynchronizeParams = Params;
  5624. }
  5625. else
  5626. {
  5627. SaveMode = false;
  5628. }
  5629. TDateTime StartTime = Now();
  5630. TSynchronizeChecklist * Checklist = NULL;
  5631. try
  5632. {
  5633. DebugAssert(!FAutoOperation);
  5634. FAutoOperation = true;
  5635. try
  5636. {
  5637. UnicodeString SessionKey = Terminal->SessionData->SessionKey;
  5638. std::unique_ptr<TStrings> DataList(Configuration->LoadDirectoryStatisticsCache(SessionKey, RemoteDirectory, CopyParam));
  5639. int Files = -1;
  5640. if (DataList->Count >= 1)
  5641. {
  5642. Files = StrToIntDef(DataList->Strings[0], Files);
  5643. }
  5644. else
  5645. {
  5646. DataList->Add(UnicodeString());
  5647. }
  5648. FSynchronizeProgressForm = new TSynchronizeProgressForm(Application, true, Files);
  5649. FSynchronizeProgressForm->Start();
  5650. Checklist = Terminal->SynchronizeCollect(LocalDirectory, RemoteDirectory,
  5651. static_cast<TTerminal::TSynchronizeMode>(Mode),
  5652. &CopyParam, Params | TTerminal::spNoConfirmation, TerminalSynchronizeDirectory,
  5653. &SynchronizeOptions);
  5654. if (Terminal->SessionData->CacheDirectories)
  5655. {
  5656. DataList->Strings[0] = IntToStr(SynchronizeOptions.Files);
  5657. Configuration->SaveDirectoryStatisticsCache(SessionKey, RemoteDirectory, CopyParam, DataList.get());
  5658. }
  5659. }
  5660. __finally
  5661. {
  5662. FAutoOperation = false;
  5663. SetTaskbarListProgressState(TBPF_NOPROGRESS);
  5664. SAFE_DESTROY(FSynchronizeProgressForm);
  5665. }
  5666. TSynchronizeParams SynchronizeParams;
  5667. SynchronizeParams.LocalDirectory = LocalDirectory;
  5668. SynchronizeParams.RemoteDirectory = RemoteDirectory;
  5669. SynchronizeParams.Mode = Mode;
  5670. SynchronizeParams.CopyParam = &CopyParam;
  5671. SynchronizeParams.Params = Params;
  5672. SynchronizeParams.Checklist = Checklist;
  5673. SynchronizeParams.CollectElapsed = (Now() - StartTime);
  5674. SynchronizeParams.StartTime = &StartTime;
  5675. SynchronizeParams.OnProcessedItem = NULL;
  5676. Result = Checklist->Count;
  5677. if (Checklist->Count > 0)
  5678. {
  5679. if (FLAGSET(Params, TTerminal::spPreviewChanges))
  5680. {
  5681. if (!DoSynchronizeChecklistDialog(
  5682. Checklist, Mode, Params, LocalDirectory, RemoteDirectory, CustomCommandMenu, DoFullSynchronize,
  5683. DoSynchronizeChecklistCalculateSize, DoSynchronizeMove, DoSynchronizeBrowse, &SynchronizeParams))
  5684. {
  5685. Result = -1;
  5686. }
  5687. }
  5688. else
  5689. {
  5690. FullSynchronize(SynchronizeParams, NULL, NULL);
  5691. }
  5692. }
  5693. else
  5694. {
  5695. UnicodeString Message = MainInstructions(LoadStr(COMPARE_NO_DIFFERENCES));
  5696. MessageDialog(Message, qtInformation, qaOK, HELP_SYNCHRONIZE_NO_DIFFERENCES);
  5697. }
  5698. }
  5699. __finally
  5700. {
  5701. delete Checklist;
  5702. }
  5703. OperationComplete(StartTime);
  5704. }
  5705. else
  5706. {
  5707. Result = -1;
  5708. }
  5709. return Result;
  5710. }
  5711. //---------------------------------------------------------------------------
  5712. void __fastcall TCustomScpExplorerForm::TerminalSynchronizeDirectory(
  5713. const UnicodeString & LocalDirectory, const UnicodeString & RemoteDirectory,
  5714. bool & Continue, bool Collect, const TSynchronizeOptions * Options)
  5715. {
  5716. if (Collect)
  5717. {
  5718. DebugAssert(FSynchronizeProgressForm != NULL);
  5719. if (!FSynchronizeProgressForm->Started)
  5720. {
  5721. FSynchronizeProgressForm->Start();
  5722. }
  5723. int CompareProgress = FSynchronizeProgressForm->SetData(LocalDirectory, RemoteDirectory, Options->Files, Continue);
  5724. SetTaskbarListProgressValue(CompareProgress);
  5725. }
  5726. else
  5727. {
  5728. DebugAssert(FSynchronizeProgressForm == NULL);
  5729. DebugAssert(FProgressForm != NULL);
  5730. }
  5731. }
  5732. //---------------------------------------------------------------------------
  5733. void __fastcall TCustomScpExplorerForm::StandaloneEdit(const UnicodeString & FileName)
  5734. {
  5735. UnicodeString FullFileName = AbsolutePath(Terminal->CurrentDirectory, FileName);
  5736. TRemoteFile * File = NULL;
  5737. Terminal->ReadFile(FullFileName, File);
  5738. if (File != NULL)
  5739. {
  5740. std::unique_ptr<TRemoteFile> FileOwner(File);
  5741. TAutoFlag Flag(FStandaloneEditing);
  5742. ExecuteRemoteFile(FullFileName, File, efInternalEditor);
  5743. Application->ShowMainForm = false;
  5744. Application->Run();
  5745. }
  5746. }
  5747. //---------------------------------------------------------------------------
  5748. void __fastcall TCustomScpExplorerForm::ExploreLocalDirectory(TOperationSide)
  5749. {
  5750. DebugFail();
  5751. }
  5752. //---------------------------------------------------------------------------
  5753. TSessionData * __fastcall TCustomScpExplorerForm::CloneCurrentSessionData()
  5754. {
  5755. std::unique_ptr<TSessionData> SessionData(new TSessionData(L""));
  5756. SessionData->Assign(ManagedSession->SessionData);
  5757. UpdateSessionData(SessionData.get());
  5758. DebugAssert(SessionData->IsLocalBrowser == IsLocalBrowserMode());
  5759. if (IsActiveTerminal(ManagedSession))
  5760. {
  5761. ManagedSession->UpdateSessionCredentials(SessionData.get());
  5762. }
  5763. if (!ManagedSession->SessionData->HasSessionName())
  5764. {
  5765. // Particularly for "Workspace/XXXX" name, we need to reset it, as it would become user-visible
  5766. // once IsWorkspace is cleared
  5767. SessionData->Name = UnicodeString();
  5768. SessionData->IsWorkspace = false;
  5769. }
  5770. return SessionData.release();
  5771. }
  5772. //---------------------------------------------------------------------------
  5773. void __fastcall TCustomScpExplorerForm::SaveCurrentSession()
  5774. {
  5775. TSessionData * SessionData = CloneCurrentSessionData();
  5776. try
  5777. {
  5778. TSessionData * EditingSessionData = StoredSessions->FindSame(SessionData);
  5779. DoSaveSession(SessionData, EditingSessionData, true, NULL);
  5780. }
  5781. __finally
  5782. {
  5783. delete SessionData;
  5784. }
  5785. }
  5786. //---------------------------------------------------------------------------
  5787. TObjectList * __fastcall TCustomScpExplorerForm::DoCollectWorkspace()
  5788. {
  5789. TTerminalManager * Manager = TTerminalManager::Instance();
  5790. std::unique_ptr<TObjectList> DataList(new TObjectList());
  5791. if (DebugAlwaysTrue(ManagedSession != NULL))
  5792. {
  5793. // Update ManagedSession->StateData
  5794. UpdateSession(ManagedSession);
  5795. }
  5796. Manager->SaveWorkspace(DataList.get());
  5797. return DataList.release();
  5798. }
  5799. //---------------------------------------------------------------------------
  5800. void __fastcall TCustomScpExplorerForm::DoSaveWorkspace(
  5801. const UnicodeString & Name, TObjectList * DataList, bool SavePasswords, bool Explicit)
  5802. {
  5803. WinConfiguration->LastWorkspace = Name;
  5804. if (!SavePasswords)
  5805. {
  5806. for (int Index = 0; Index < DataList->Count; Index++)
  5807. {
  5808. TSessionData * SessionData = dynamic_cast<TSessionData *>(DataList->Items[Index]);
  5809. if (SessionData->Link.IsEmpty())
  5810. {
  5811. SessionData->ClearSessionPasswords();
  5812. }
  5813. }
  5814. }
  5815. StoredSessions->NewWorkspace(Name, DataList);
  5816. // modified only
  5817. StoredSessions->Save(false, Explicit);
  5818. }
  5819. //---------------------------------------------------------------------------
  5820. UnicodeString __fastcall TCustomScpExplorerForm::WorkspaceName()
  5821. {
  5822. return
  5823. DefaultStr(WinConfiguration->LastWorkspace,
  5824. DefaultStr(WinConfiguration->AutoWorkspace, LoadStr(NEW_WORKSPACE)));
  5825. }
  5826. //---------------------------------------------------------------------------
  5827. bool __fastcall TCustomScpExplorerForm::SaveWorkspace(bool EnableAutoSave)
  5828. {
  5829. std::unique_ptr<TObjectList> DataList(DoCollectWorkspace());
  5830. bool AnyNonStoredSessionWithPassword = false;
  5831. bool AnyNonStoredNonWorkspaceSessionWithPassword = false;
  5832. bool AllNonStoredSessionsAnonymous = true;
  5833. TTerminalManager * Manager = TTerminalManager::Instance();
  5834. for (int Index = 0; Index < DataList->Count; Index++)
  5835. {
  5836. TSessionData * SessionData = dynamic_cast<TSessionData *>(DataList->Items[Index]);
  5837. if (SessionData->Link.IsEmpty())
  5838. {
  5839. if (SessionData->HasAnySessionPassword())
  5840. {
  5841. AnyNonStoredSessionWithPassword = true;
  5842. if (!Manager->Sessions[Index]->SessionData->IsWorkspace)
  5843. {
  5844. AnyNonStoredNonWorkspaceSessionWithPassword = true;
  5845. }
  5846. }
  5847. if (!SameText(SessionData->UserName, AnonymousUserName))
  5848. {
  5849. AllNonStoredSessionsAnonymous = false;
  5850. }
  5851. }
  5852. }
  5853. bool SavePasswords;
  5854. bool * PSavePasswords;
  5855. bool NotRecommendedSavingPasswords =
  5856. !CustomWinConfiguration->UseMasterPassword &&
  5857. !AllNonStoredSessionsAnonymous;
  5858. if (Configuration->DisablePasswordStoring ||
  5859. !AnyNonStoredSessionWithPassword)
  5860. {
  5861. PSavePasswords = NULL;
  5862. SavePasswords = false;
  5863. }
  5864. else
  5865. {
  5866. PSavePasswords = &SavePasswords;
  5867. SavePasswords =
  5868. !AnyNonStoredNonWorkspaceSessionWithPassword ||
  5869. !NotRecommendedSavingPasswords;
  5870. }
  5871. UnicodeString Name = WorkspaceName();
  5872. bool CreateShortcut = false;
  5873. bool Result =
  5874. DoSaveWorkspaceDialog(
  5875. Name, PSavePasswords, NotRecommendedSavingPasswords, CreateShortcut,
  5876. EnableAutoSave);
  5877. if (Result)
  5878. {
  5879. DoSaveWorkspace(Name, DataList.get(), SavePasswords, true);
  5880. if (CreateShortcut)
  5881. {
  5882. TOperationVisualizer Visualizer;
  5883. UnicodeString AdditionalParams = TProgramParams::FormatSwitch(DESKTOP_SWITCH);
  5884. CreateDesktopSessionShortCut(Name, L"", AdditionalParams, -1, WORKSPACE_ICON);
  5885. }
  5886. if (EnableAutoSave)
  5887. {
  5888. WinConfiguration->AutoSaveWorkspace = true;
  5889. WinConfiguration->AutoWorkspace = Name;
  5890. if (PSavePasswords != NULL)
  5891. {
  5892. WinConfiguration->AutoSaveWorkspacePasswords = SavePasswords;
  5893. }
  5894. }
  5895. }
  5896. return Result;
  5897. }
  5898. //---------------------------------------------------------------------------
  5899. void __fastcall TCustomScpExplorerForm::UpdateSession(TManagedTerminal * Session)
  5900. {
  5901. DebugAssert(IsLocalBrowserMode() == Session->LocalBrowser);
  5902. DebugAssert(Session == ManagedSession);
  5903. if (!IsLocalBrowserMode())
  5904. {
  5905. SAFE_DESTROY(Session->RemoteExplorerState);
  5906. if (WinConfiguration->PreservePanelState)
  5907. {
  5908. Session->RemoteExplorerState = RemoteDirView->SaveState();
  5909. }
  5910. }
  5911. UpdateSessionData(Session->StateData);
  5912. }
  5913. //---------------------------------------------------------------------------
  5914. void __fastcall TCustomScpExplorerForm::UpdateSessionData(TSessionData * Data)
  5915. {
  5916. // Keep in sync with TSessionData::CopyStateData
  5917. DebugAssert(Data != NULL);
  5918. // This is inconsistent with how color (for example) is handled.
  5919. Data->Name = ManagedSession->SessionData->Name;
  5920. if (!IsLocalBrowserMode())
  5921. {
  5922. // cannot use RemoteDirView->Path, because it is empty if connection
  5923. // was already closed
  5924. // also only peek, we may not be connected at all atm
  5925. // so make sure we do not try retrieving current directory from the server
  5926. // (particularly with FTP)
  5927. UnicodeString ACurrentDirectory = Terminal->PeekCurrentDirectory();
  5928. if (!ACurrentDirectory.IsEmpty())
  5929. {
  5930. Data->RemoteDirectory = ACurrentDirectory;
  5931. }
  5932. }
  5933. Data->Color = SessionColor;
  5934. }
  5935. //---------------------------------------------------------------------------
  5936. void __fastcall TCustomScpExplorerForm::ToolBarResize(TObject *Sender)
  5937. {
  5938. TTBXToolbar * Toolbar = dynamic_cast<TTBXToolbar*>(Sender);
  5939. DebugAssert(Toolbar != NULL);
  5940. for (int i = 0; i < Toolbar->Items->Count; i++)
  5941. {
  5942. TTBXCustomDropDownItem * DropDownItem;
  5943. DropDownItem = dynamic_cast<TTBXCustomDropDownItem *>(Toolbar->Items->Items[i]);
  5944. if (DropDownItem != NULL)
  5945. {
  5946. ToolbarItemResize(DropDownItem,
  5947. Toolbar->Width - (Toolbar->View->BaseSize.x - DropDownItem->EditWidth) -
  5948. Toolbar->NonClientWidth);
  5949. break;
  5950. }
  5951. }
  5952. }
  5953. //---------------------------------------------------------------------------
  5954. void __fastcall TCustomScpExplorerForm::ToolbarItemResize(TTBXCustomDropDownItem * Item, int Width)
  5955. {
  5956. Item->EditWidth = Width;
  5957. }
  5958. //---------------------------------------------------------------------------
  5959. void __fastcall TCustomScpExplorerForm::ToolbarGetBaseSize(
  5960. TTBCustomToolbar * Toolbar, TPoint & ASize)
  5961. {
  5962. for (int i = 0; i < Toolbar->Items->Count; i++)
  5963. {
  5964. TTBXCustomDropDownItem * DropDownItem;
  5965. DropDownItem = dynamic_cast<TTBXCustomDropDownItem *>(Toolbar->Items->Items[i]);
  5966. if (DropDownItem != NULL)
  5967. {
  5968. ASize.x -= DropDownItem->EditWidth;
  5969. ASize.x += ScaleByTextHeight(this, 50) /* minimal combo width */;
  5970. }
  5971. }
  5972. }
  5973. //---------------------------------------------------------------------------
  5974. void __fastcall TCustomScpExplorerForm::DoWarnLackOfTempSpace(
  5975. const UnicodeString Path, __int64 RequiredSpace, bool & Continue)
  5976. {
  5977. if (WinConfiguration->DDWarnLackOfTempSpace)
  5978. {
  5979. UnicodeString ADrive = ExtractFileDrive(ExpandFileName(Path));
  5980. if (!ADrive.IsEmpty())
  5981. {
  5982. __int64 FreeSpace = DiskFree((Byte)(ADrive[1]-'A'+1));
  5983. DebugAssert(RequiredSpace >= 0);
  5984. __int64 RequiredWithReserve;
  5985. RequiredWithReserve = (__int64)(RequiredSpace * WinConfiguration->DDWarnLackOfTempSpaceRatio);
  5986. if (FreeSpace < RequiredWithReserve)
  5987. {
  5988. unsigned int Result;
  5989. TMessageParams Params(mpNeverAskAgainCheck);
  5990. Result = MessageDialog(FMTLOAD(DD_WARN_LACK_OF_TEMP_SPACE, (Path,
  5991. FormatBytes(FreeSpace), FormatBytes(RequiredSpace))),
  5992. qtWarning, qaYes | qaNo, HELP_DD_WARN_LACK_OF_TEMP_SPACE, &Params);
  5993. if (Result == qaNeverAskAgain)
  5994. {
  5995. WinConfiguration->DDWarnLackOfTempSpace = false;
  5996. }
  5997. Continue = (Result == qaYes || Result == qaNeverAskAgain);
  5998. }
  5999. }
  6000. }
  6001. }
  6002. //---------------------------------------------------------------------------
  6003. void __fastcall TCustomScpExplorerForm::AddBookmark(TOperationSide Side)
  6004. {
  6005. DoOpenDirectoryDialog(odAddBookmark, Side);
  6006. }
  6007. //---------------------------------------------------------------------------
  6008. TStrings * __fastcall TCustomScpExplorerForm::CreateVisitedDirectories(TOperationSide Side)
  6009. {
  6010. // we should better use TCustomDirView::FCaseSensitive, but it is private
  6011. TStringList * VisitedDirectories = CreateSortedStringList(!IsSideLocalBrowser(Side));
  6012. try
  6013. {
  6014. TCustomDirView * DView = DirView(Side);
  6015. for (int Index = -DView->BackCount; Index <= DView->ForwardCount; Index++)
  6016. {
  6017. VisitedDirectories->Add(DView->HistoryPath[Index]);
  6018. }
  6019. }
  6020. catch (...)
  6021. {
  6022. delete VisitedDirectories;
  6023. throw;
  6024. }
  6025. return VisitedDirectories;
  6026. }
  6027. //---------------------------------------------------------------------------
  6028. void __fastcall TCustomScpExplorerForm::DoOpenDirectoryDialog(
  6029. TOpenDirectoryMode Mode, TOperationSide Side)
  6030. {
  6031. bool Continue = true;
  6032. if (Mode == odAddBookmark)
  6033. {
  6034. TMessageParams Params(mpNeverAskAgainCheck);
  6035. Params.NeverAskAgainTitle = LoadStr(ADD_BOOKMARK_SHARED);
  6036. Params.NeverAskAgainCheckedInitially = WinConfiguration->UseSharedBookmarks;
  6037. UnicodeString Message = MainInstructions(FMTLOAD(ADD_BOOKMARK_CONFIRM, (DirView(Side)->PathName)));
  6038. unsigned int Answer =
  6039. MessageDialog(Message,
  6040. qtConfirmation, qaOK | qaCancel, HELP_ADD_BOOKMARK_CONFIRM, &Params);
  6041. if (Answer == qaNeverAskAgain)
  6042. {
  6043. Continue = true;
  6044. WinConfiguration->UseSharedBookmarks = true;
  6045. }
  6046. else if (Answer == qaOK)
  6047. {
  6048. Continue = true;
  6049. WinConfiguration->UseSharedBookmarks = false;
  6050. }
  6051. else
  6052. {
  6053. Continue = false;
  6054. }
  6055. }
  6056. if (Continue)
  6057. {
  6058. TStrings * VisitedDirectories = CreateVisitedDirectories(Side);
  6059. try
  6060. {
  6061. UnicodeString Name = DirView(Side)->PathName;
  6062. bool Repeat;
  6063. do
  6064. {
  6065. Repeat = false;
  6066. TOperationSide BookmarkSide = IsSideLocalBrowser(Side) ? osLocal : osRemote;
  6067. if (::DoOpenDirectoryDialog(Mode, BookmarkSide, Name, VisitedDirectories, Terminal,
  6068. // do not allow switching to location profiles,
  6069. // if we are not connected
  6070. HasDirView[osLocal] && (Terminal != NULL)))
  6071. {
  6072. TWindowLock Lock(this);
  6073. if (!TryOpenDirectory(Side, Name))
  6074. {
  6075. Repeat = true;
  6076. Mode = odBrowse;
  6077. }
  6078. }
  6079. }
  6080. while (Repeat);
  6081. }
  6082. __finally
  6083. {
  6084. delete VisitedDirectories;
  6085. }
  6086. }
  6087. }
  6088. //---------------------------------------------------------------------------
  6089. bool __fastcall TCustomScpExplorerForm::CommandSessionFallback()
  6090. {
  6091. DebugAssert(!Terminal->CommandSessionOpened);
  6092. return TTerminalManager::Instance()->ConnectTerminal(Terminal->CommandSession);
  6093. }
  6094. //---------------------------------------------------------------------------
  6095. bool __fastcall TCustomScpExplorerForm::EnsureCommandSessionFallback(TFSCapability Capability)
  6096. {
  6097. bool Result = Terminal->IsCapable[Capability] ||
  6098. Terminal->CommandSessionOpened;
  6099. if (!Result)
  6100. {
  6101. DebugAssert(Terminal->IsCapable[fcSecondaryShell]);
  6102. if (!GUIConfiguration->ConfirmCommandSession)
  6103. {
  6104. Result = true;
  6105. }
  6106. else
  6107. {
  6108. TMessageParams Params(mpNeverAskAgainCheck);
  6109. const TFileSystemInfo & FileSystemInfo = Terminal->GetFileSystemInfo();
  6110. unsigned int Answer = MessageDialog(FMTLOAD(PERFORM_ON_COMMAND_SESSION2,
  6111. (FileSystemInfo.ProtocolName, FileSystemInfo.ProtocolName)), qtConfirmation,
  6112. qaOK | qaCancel, HELP_PERFORM_ON_COMMAND_SESSION, &Params);
  6113. if (Answer == qaNeverAskAgain)
  6114. {
  6115. GUIConfiguration->ConfirmCommandSession = false;
  6116. Result = true;
  6117. }
  6118. else if (Answer == qaOK)
  6119. {
  6120. Result = true;
  6121. }
  6122. }
  6123. if (Result)
  6124. {
  6125. Result = CommandSessionFallback();
  6126. }
  6127. }
  6128. return Result;
  6129. }
  6130. //---------------------------------------------------------------------------
  6131. void __fastcall TCustomScpExplorerForm::OpenConsole(UnicodeString Command)
  6132. {
  6133. if (EnsureCommandSessionFallback(fcAnyCommand))
  6134. {
  6135. DoConsoleDialog(Terminal, Command);
  6136. }
  6137. }
  6138. //---------------------------------------------------------------------------
  6139. void __fastcall TCustomScpExplorerForm::FileControlDDDragEnter(
  6140. TObject *Sender, _di_IDataObject /*DataObj*/, int /*grfKeyState*/,
  6141. const TPoint & /*Point*/, int & /*dwEffect*/, bool & Accept)
  6142. {
  6143. if (!IsSideLocalBrowser(osRemote) &&
  6144. IsFileControl(DropSourceControl, osRemote) &&
  6145. (FDDExtMapFile != NULL))
  6146. {
  6147. Accept = true;
  6148. }
  6149. FDDTargetControl = dynamic_cast<TControl*>(Sender);
  6150. DebugAssert(FDDTargetControl != NULL);
  6151. }
  6152. //---------------------------------------------------------------------------
  6153. void __fastcall TCustomScpExplorerForm::SessionsDDDragEnter(
  6154. _di_IDataObject DataObj, int KeyState,
  6155. const TPoint & Point, int & Effect, bool & Accept)
  6156. {
  6157. FileControlDDDragEnter(SessionsPageControl, DataObj, KeyState, Point, Effect, Accept);
  6158. }
  6159. //---------------------------------------------------------------------------
  6160. void __fastcall TCustomScpExplorerForm::QueueDDDragEnter(
  6161. _di_IDataObject DataObj, int KeyState,
  6162. const TPoint & Point, int & Effect, bool & Accept)
  6163. {
  6164. FileControlDDDragEnter(QueueView3, DataObj, KeyState, Point, Effect, Accept);
  6165. }
  6166. //---------------------------------------------------------------------------
  6167. void __fastcall TCustomScpExplorerForm::FileControlDDDragLeave(
  6168. TObject * Sender)
  6169. {
  6170. DebugUsedParam(Sender);
  6171. DebugAssert(FDDTargetControl == Sender);
  6172. FDDTargetControl = NULL;
  6173. }
  6174. //---------------------------------------------------------------------------
  6175. void __fastcall TCustomScpExplorerForm::SessionsDDDragLeave()
  6176. {
  6177. FileControlDDDragLeave(SessionsPageControl);
  6178. }
  6179. //---------------------------------------------------------------------------
  6180. void __fastcall TCustomScpExplorerForm::QueueDDDragLeave()
  6181. {
  6182. FileControlDDDragLeave(QueueView3);
  6183. }
  6184. //---------------------------------------------------------------------------
  6185. void __fastcall TCustomScpExplorerForm::AddEditLink(TOperationSide Side, bool Add)
  6186. {
  6187. DebugAssert(!IsSideLocalBrowser(Side));
  6188. DebugUsedParam(Side);
  6189. bool Edit = false;
  6190. TRemoteFile * File = NULL;
  6191. UnicodeString FileName;
  6192. UnicodeString PointTo;
  6193. bool SymbolicLink = true;
  6194. if (RemoteDirView->ItemFocused)
  6195. {
  6196. DebugAssert(RemoteDirView->ItemFocused->Data);
  6197. File = (TRemoteFile *)RemoteDirView->ItemFocused->Data;
  6198. Edit = !Add && File->IsSymLink && Terminal->SessionData->ResolveSymlinks;
  6199. if (Edit)
  6200. {
  6201. FileName = File->FileName;
  6202. PointTo = File->LinkTo;
  6203. }
  6204. else
  6205. {
  6206. PointTo = File->FileName;
  6207. }
  6208. }
  6209. if (DoSymlinkDialog(FileName, PointTo, osRemote, SymbolicLink, Edit,
  6210. Terminal->IsCapable[fcHardLink]))
  6211. {
  6212. Configuration->Usage->Inc(L"RemoteLinksCreated");
  6213. if (Edit)
  6214. {
  6215. DebugAssert(File->FileName == FileName);
  6216. int Params = dfNoRecursive;
  6217. Terminal->ExceptionOnFail = true;
  6218. try
  6219. {
  6220. Terminal->DeleteFile(L"", File, &Params);
  6221. }
  6222. __finally
  6223. {
  6224. Terminal->ExceptionOnFail = false;
  6225. }
  6226. }
  6227. Terminal->CreateLink(FileName, PointTo, SymbolicLink);
  6228. }
  6229. }
  6230. //---------------------------------------------------------------------------
  6231. bool __fastcall TCustomScpExplorerForm::CanAddEditLink(TOperationSide Side)
  6232. {
  6233. return
  6234. (IsSideLocalBrowser(Side) ||
  6235. (HasActiveTerminal() &&
  6236. Terminal->ResolvingSymlinks &&
  6237. Terminal->IsCapable[fcSymbolicLink]));
  6238. }
  6239. //---------------------------------------------------------------------------
  6240. bool __fastcall TCustomScpExplorerForm::LinkFocused()
  6241. {
  6242. return
  6243. !IsSideLocalBrowser(FCurrentSide) &&
  6244. (RemoteDirView->ItemFocused != NULL) &&
  6245. ((TRemoteFile *)RemoteDirView->ItemFocused->Data)->IsSymLink &&
  6246. Terminal->SessionData->ResolveSymlinks;
  6247. }
  6248. //---------------------------------------------------------------------------
  6249. void __fastcall TCustomScpExplorerForm::ExecuteCurrentFile()
  6250. {
  6251. DebugAssert(!WinConfiguration->DisableOpenEdit);
  6252. FForceExecution = true;
  6253. try
  6254. {
  6255. DirView(osCurrent)->ExecuteCurrentFile();
  6256. }
  6257. __finally
  6258. {
  6259. FForceExecution = false;
  6260. }
  6261. }
  6262. //---------------------------------------------------------------------------
  6263. void __fastcall TCustomScpExplorerForm::ExecuteCurrentFileWith(bool OnFocused)
  6264. {
  6265. TEditorData ExternalEditor;
  6266. ExternalEditor.Editor = edExternal;
  6267. bool Remember = false;
  6268. if (DoEditorPreferencesDialog(&ExternalEditor, Remember, epmAdHoc, !IsSideLocalBrowser(osCurrent)))
  6269. {
  6270. if (Remember)
  6271. {
  6272. TEditorList * EditorList = new TEditorList;
  6273. try
  6274. {
  6275. *EditorList = *WinConfiguration->EditorList;
  6276. bool Found = false;
  6277. int i = 0;
  6278. while (!Found && (i < EditorList->Count))
  6279. {
  6280. const TEditorPreferences * Editor = EditorList->Editors[i];
  6281. if ((Editor->Data->Editor == edExternal) &&
  6282. (Editor->Data->ExternalEditor == ExternalEditor.ExternalEditor))
  6283. {
  6284. Found = true;
  6285. }
  6286. i++;
  6287. }
  6288. if (!Found)
  6289. {
  6290. EditorList->Add(new TEditorPreferences(ExternalEditor));
  6291. WinConfiguration->EditorList = EditorList;
  6292. }
  6293. }
  6294. __finally
  6295. {
  6296. delete EditorList;
  6297. }
  6298. }
  6299. ExecuteFile(osCurrent, efExternalEditor, &ExternalEditor, true, OnFocused);
  6300. }
  6301. }
  6302. //---------------------------------------------------------------------------
  6303. void __fastcall TCustomScpExplorerForm::DetachTerminal(TObject * ATerminal)
  6304. {
  6305. FEditorManager->ProcessFiles(FileTerminalRemoved, ATerminal);
  6306. if (FFileFindTerminal == ATerminal)
  6307. {
  6308. FFileFindTerminal = NULL;
  6309. HideFileFindDialog();
  6310. }
  6311. if (FClipboardTerminal == ATerminal)
  6312. {
  6313. ClipboardClear(); // implies ClipboardStop
  6314. }
  6315. }
  6316. //---------------------------------------------------------------------------
  6317. void __fastcall TCustomScpExplorerForm::TerminalRemoved(TObject * Sender)
  6318. {
  6319. DetachTerminal(Sender);
  6320. }
  6321. //---------------------------------------------------------------------------
  6322. void __fastcall TCustomScpExplorerForm::FileTerminalRemoved(const UnicodeString FileName,
  6323. TEditedFileData * Data, TObject * /*Token*/, void * Arg)
  6324. {
  6325. TTerminal * Terminal = static_cast<TTerminal *>(Arg);
  6326. DebugAssert(Terminal != NULL);
  6327. if (Data->Terminal == Terminal)
  6328. {
  6329. Data->Terminal = NULL;
  6330. }
  6331. }
  6332. //---------------------------------------------------------------------------
  6333. TManagedTerminal * TCustomScpExplorerForm::GetReplacementForLastSession()
  6334. {
  6335. return NULL;
  6336. }
  6337. //---------------------------------------------------------------------------
  6338. void __fastcall TCustomScpExplorerForm::LastTerminalClosed()
  6339. {
  6340. UpdateControls();
  6341. SessionColor = TColor(0);
  6342. UpdateRemotePathComboBox(false);
  6343. NeedSession(false);
  6344. }
  6345. //---------------------------------------------------------------------------
  6346. void __fastcall TCustomScpExplorerForm::NeedSession(bool Startup)
  6347. {
  6348. try
  6349. {
  6350. // Cache, as the login dialog can change its value.
  6351. // See also DoShow. Also see TTerminalManager::NewSession.
  6352. bool ShowLogin = WinConfiguration->ShowLoginWhenNoSession;
  6353. try
  6354. {
  6355. if (ShowLogin)
  6356. {
  6357. bool ReloadSessions = !Startup;
  6358. TTerminalManager::Instance()->NewSession(L"", ReloadSessions, this, true);
  6359. }
  6360. }
  6361. __finally
  6362. {
  6363. // Do not terminate, if we are only starting up and we are not showing Login dialog
  6364. // (so there was no chance for the user to open any session yet)
  6365. if ((ShowLogin || !Startup) &&
  6366. !WinConfiguration->KeepOpenWhenNoSession &&
  6367. // this rules out the implicit Commander's local-local browser
  6368. ((Terminal == NULL) || (!Terminal->Active && !Terminal->Permanent)))
  6369. {
  6370. TerminateApplication();
  6371. }
  6372. }
  6373. }
  6374. catch (EAbort &)
  6375. {
  6376. // swallow
  6377. // The TTerminalManager does not expect the OnLastTerminalClose to throw without trying to connect.
  6378. // Also when called from TWinControl.UpdateShowing => CMShowingChanged, the showing is aborted on exception
  6379. }
  6380. }
  6381. //---------------------------------------------------------------------------
  6382. void __fastcall TCustomScpExplorerForm::SessionListChanged(bool ForceTruncationUpdate)
  6383. {
  6384. TTerminalManager * Manager = TTerminalManager::Instance();
  6385. DebugAssert(!Manager->Updating);
  6386. TAutoFlag UpdatingSessionTabsFlag(FUpdatingSessionTabs);
  6387. int ActiveSessionIndex = Manager->ActiveSessionIndex;
  6388. Configuration->Usage->SetMax(L"MaxOpenedSessions", Manager->Count);
  6389. SendMessage(SessionsPageControl->Handle, WM_SETREDRAW, 0, 0);
  6390. try
  6391. {
  6392. int PrevTabsWidth = SessionsPageControl->TotalTabsWidth();
  6393. while (SessionsPageControl->PageCount > Manager->Count + 1)
  6394. {
  6395. delete SessionsPageControl->Pages[SessionsPageControl->PageCount - 1];
  6396. }
  6397. for (int Index = 0; Index <= Manager->Count; Index++)
  6398. {
  6399. TThemeTabSheet * TabSheet;
  6400. if (Index >= SessionsPageControl->PageCount)
  6401. {
  6402. TabSheet = new TThemeTabSheet(SessionsPageControl);
  6403. TabSheet->PageControl = SessionsPageControl;
  6404. }
  6405. else
  6406. {
  6407. TabSheet = SessionsPageControl->Pages[Index];
  6408. }
  6409. bool IsSessionTab = (Index < Manager->Count);
  6410. if (IsSessionTab)
  6411. {
  6412. TTerminal * Terminal = Manager->Sessions[Index];
  6413. TabSheet->Tag = reinterpret_cast<int>(Terminal);
  6414. TabSheet->CaptionTruncation = tttEllipsis;
  6415. UpdateSessionTab(TabSheet);
  6416. }
  6417. else
  6418. {
  6419. TabSheet->Tag = 0; // not really needed
  6420. TabSheet->Shadowed = false;
  6421. TabSheet->Button = SupportsLocalBrowser() ? ttbDropDown : ttbNone;
  6422. TabSheet->CaptionTruncation = tttNoText;
  6423. // 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)
  6424. UpdateNewTabTab();
  6425. }
  6426. }
  6427. // Prevent flicker on the right side where scrolling buttons would be, unless really needed
  6428. if (ForceTruncationUpdate || (SessionsPageControl->TotalTabsWidth() != PrevTabsWidth))
  6429. {
  6430. SessionsPageControl->UpdateTabsCaptionTruncation();
  6431. }
  6432. }
  6433. __finally
  6434. {
  6435. SendMessage(SessionsPageControl->Handle, WM_SETREDRAW, 1, 0);
  6436. }
  6437. SessionsPageControl->ActivePageIndex = ActiveSessionIndex;
  6438. }
  6439. //---------------------------------------------------------------------------
  6440. UnicodeString TCustomScpExplorerForm::GetNewTabTabCaption()
  6441. {
  6442. return StripHotkey(StripTrailingPunctuation(NonVisualDataModule->NewTabAction->Caption));
  6443. }
  6444. //---------------------------------------------------------------------------
  6445. void __fastcall TCustomScpExplorerForm::UpdateNewTabTab()
  6446. {
  6447. TThemeTabSheet * TabSheet = SessionsPageControl->Pages[SessionsPageControl->PageCount - 1];
  6448. UnicodeString TabCaption;
  6449. if (WinConfiguration->SelectiveToolbarText)
  6450. {
  6451. TabCaption = GetNewTabTabCaption();
  6452. }
  6453. TabSheet->BaseCaption = TabCaption;
  6454. TabSheet->ImageIndex = GetNewTabTabImageIndex(osCurrent);
  6455. }
  6456. //---------------------------------------------------------------------------
  6457. TManagedTerminal * __fastcall TCustomScpExplorerForm::GetSessionTabSession(TTabSheet * TabSheet)
  6458. {
  6459. return reinterpret_cast<TManagedTerminal *>(TabSheet->Tag);
  6460. }
  6461. //---------------------------------------------------------------------------
  6462. UnicodeString TCustomScpExplorerForm::GetLocalBrowserSessionTitle(TManagedTerminal *)
  6463. {
  6464. DebugFail();
  6465. return UnicodeString();
  6466. }
  6467. //---------------------------------------------------------------------------
  6468. void TCustomScpExplorerForm::UpdateSessionTab(TThemeTabSheet * TabSheet)
  6469. {
  6470. if (DebugAlwaysTrue(TabSheet != NULL))
  6471. {
  6472. TManagedTerminal * ASession = GetSessionTabSession(TabSheet);
  6473. if (DebugAlwaysTrue(ASession != NULL))
  6474. {
  6475. TColor Color = (ASession == Terminal) ? FSessionColor : ASession->StateData->Color;
  6476. if (ASession->LocalBrowser)
  6477. {
  6478. TabSheet->ImageIndex = FLocalBrowserTabImageIndex;
  6479. }
  6480. else
  6481. {
  6482. TabSheet->ImageIndex = AddSessionColor(Color);
  6483. }
  6484. TTerminalManager * Manager = TTerminalManager::Instance();
  6485. TabSheet->Shadowed = !ASession->Active && !ASession->LocalBrowser;
  6486. TabSheet->Button = CanCloseSession(ASession) ? ttbClose : ttbNone;
  6487. TabSheet->BaseCaption = Manager->GetSessionTitle(ASession, true);
  6488. }
  6489. }
  6490. }
  6491. //---------------------------------------------------------------------------
  6492. bool TCustomScpExplorerForm::CanCloseSession(TManagedTerminal * Session)
  6493. {
  6494. return !Session->LocalBrowser || (TTerminalManager::Instance()->Count > 1);
  6495. }
  6496. //---------------------------------------------------------------------------
  6497. bool __fastcall TCustomScpExplorerForm::SessionTabSwitched()
  6498. {
  6499. DebugAssert(SessionsPageControl->ActivePage != NULL);
  6500. TManagedTerminal * Session = GetSessionTabSession(SessionsPageControl->ActivePage);
  6501. bool Result = (Session != NULL);
  6502. if (Result)
  6503. {
  6504. TTerminalManager::Instance()->ActiveSession = Session;
  6505. }
  6506. else
  6507. {
  6508. try
  6509. {
  6510. NewTab();
  6511. }
  6512. __finally
  6513. {
  6514. SessionListChanged();
  6515. }
  6516. FSessionsPageControlNewTabTime = Now();
  6517. }
  6518. return Result;
  6519. }
  6520. //---------------------------------------------------------------------------
  6521. void __fastcall TCustomScpExplorerForm::SessionsPageControlChange(TObject * /*Sender*/)
  6522. {
  6523. SessionTabSwitched();
  6524. }
  6525. //---------------------------------------------------------------------------
  6526. void __fastcall TCustomScpExplorerForm::TransferListChange(TObject * Sender)
  6527. {
  6528. TTBXStringList * TransferList = dynamic_cast<TTBXStringList *>(Sender);
  6529. DebugAssert(TransferList != NULL);
  6530. UnicodeString Name;
  6531. if (TransferList->ItemIndex <= 0)
  6532. {
  6533. Name = L"";
  6534. }
  6535. else
  6536. {
  6537. Name = GUIConfiguration->CopyParamList->Names[TransferList->ItemIndex - 1];
  6538. }
  6539. if (FCopyParamAutoSelected.IsEmpty())
  6540. {
  6541. // if previous preset was not autoselected, make new preset the "default"
  6542. FCopyParamDefault = Name;
  6543. }
  6544. GUIConfiguration->CopyParamCurrent = Name;
  6545. }
  6546. //---------------------------------------------------------------------------
  6547. void __fastcall TCustomScpExplorerForm::UpdateTransferLabel()
  6548. {
  6549. // sanity check
  6550. bool ExistingPreset =
  6551. (FTransferListHoverIndex >= 0) &&
  6552. (FTransferListHoverIndex < 1 + GUIConfiguration->CopyParamList->Count);
  6553. DebugAssert(ExistingPreset);
  6554. if (ExistingPreset)
  6555. {
  6556. HDC DC = GetDC(0);
  6557. TCanvas * Canvas = new TCanvas();
  6558. try
  6559. {
  6560. Canvas->Handle = DC;
  6561. Canvas->Font = GetToolbarFont(this);
  6562. UnicodeString Name;
  6563. if (FTransferListHoverIndex == 0)
  6564. {
  6565. Name = L"";
  6566. }
  6567. else
  6568. {
  6569. Name = GUIConfiguration->CopyParamList->Names[FTransferListHoverIndex - 1];
  6570. }
  6571. TTBXLabelItem * TransferLabel = dynamic_cast<TTBXLabelItem*>(
  6572. static_cast<TComponent*>(GetComponent(fcTransferLabel)));
  6573. TTBXStringList * TransferList = dynamic_cast<TTBXStringList*>(
  6574. static_cast<TObject*>(GetComponent(fcTransferList)));
  6575. UnicodeString InfoStr =
  6576. GUIConfiguration->CopyParamPreset[Name].
  6577. GetInfoStr(L"; ",
  6578. FLAGMASK(HasActiveTerminal(), Terminal->UsableCopyParamAttrs(0).General));
  6579. int MaxWidth = TransferList->MinWidth - (2 * TransferLabel->Margin) - ScaleByTextHeight(this, 10);
  6580. if (Canvas->TextExtent(InfoStr).cx > MaxWidth)
  6581. {
  6582. while (Canvas->TextExtent(InfoStr + Ellipsis).cx > MaxWidth)
  6583. {
  6584. InfoStr.SetLength(InfoStr.Length() - 1);
  6585. }
  6586. InfoStr += Ellipsis;
  6587. }
  6588. // UpdateCaption does not cause invalidation of whole submenu, while
  6589. // setting Caption property does.
  6590. // also it probably does not resize the label, even if necessary
  6591. // (we do not want that anyway)
  6592. TransferLabel->UpdateCaption(InfoStr);
  6593. }
  6594. __finally
  6595. {
  6596. Canvas->Handle = NULL;
  6597. ReleaseDC(0, DC);
  6598. delete Canvas;
  6599. }
  6600. }
  6601. }
  6602. //---------------------------------------------------------------------------
  6603. void __fastcall TCustomScpExplorerForm::TransferListDrawItem(
  6604. TTBXCustomList * /*Sender*/, TCanvas * /*ACanvas*/, const TRect & /*ARect*/,
  6605. int /*AIndex*/, int AHoverIndex, bool & /*DrawDefault*/)
  6606. {
  6607. if (FTransferListHoverIndex != AHoverIndex)
  6608. {
  6609. FTransferListHoverIndex = AHoverIndex;
  6610. UpdateTransferLabel();
  6611. }
  6612. }
  6613. //---------------------------------------------------------------------------
  6614. void __fastcall TCustomScpExplorerForm::WMAppCommand(TMessage & Message)
  6615. {
  6616. int Command = GET_APPCOMMAND_LPARAM(Message.LParam);
  6617. TShiftState Shift = KeyDataToShiftState(GET_KEYSTATE_LPARAM(Message.LParam));
  6618. if ((Shift * (TShiftState() << ssShift << ssAlt << ssCtrl)).Empty())
  6619. {
  6620. if (Command == APPCOMMAND_BROWSER_FAVORITES)
  6621. {
  6622. if (!NonVisualDataModule->Busy)
  6623. {
  6624. OpenDirectory(GetSide(osCurrent));
  6625. }
  6626. Message.Result = 1;
  6627. }
  6628. else
  6629. {
  6630. TForm::Dispatch(&Message);
  6631. }
  6632. }
  6633. else
  6634. {
  6635. TForm::Dispatch(&Message);
  6636. }
  6637. }
  6638. //---------------------------------------------------------------------------
  6639. void __fastcall TCustomScpExplorerForm::CMDialogChar(TMessage & AMessage)
  6640. {
  6641. TCMDialogChar & Message = reinterpret_cast<TCMDialogChar &>(AMessage);
  6642. if ((FIgnoreNextDialogChar != 0) &&
  6643. (toupper(Message.CharCode) == toupper(FIgnoreNextDialogChar)))
  6644. {
  6645. Message.Result = 1;
  6646. }
  6647. else
  6648. {
  6649. TForm::Dispatch(&Message);
  6650. }
  6651. FIgnoreNextDialogChar = 0;
  6652. }
  6653. //---------------------------------------------------------------------------
  6654. void __fastcall TCustomScpExplorerForm::WMSysCommand(TMessage & Message)
  6655. {
  6656. // The four low-order bits are used internally by Windows
  6657. unsigned int Cmd = (Message.WParam & 0xFFF0);
  6658. // SC_RESTORE, SC_MAXIMIZE, SC_MINIMIZE - buttons on windows title
  6659. // SC_DEFAULT - double click on windows title (does not work, at least on WinXP)
  6660. // 61730 - restore thru double click - undocumented
  6661. // 61490 - maximize thru double click - undocumented
  6662. if ((Cmd == SC_RESTORE) || (Cmd == SC_MAXIMIZE) ||
  6663. (Cmd == SC_MINIMIZE) || (Cmd == SC_DEFAULT))
  6664. {
  6665. SysResizing(Cmd);
  6666. }
  6667. TForm::Dispatch(&Message);
  6668. }
  6669. //---------------------------------------------------------------------------
  6670. void __fastcall TCustomScpExplorerForm::WMQueryEndSession(TMessage & Message)
  6671. {
  6672. // We were actually never able to make ENDSESSION_CRITICAL happen.
  6673. // Also there no point returning TRUE as we are not able to
  6674. // handle the abrupt termination caused by subsequent WM_ENDSESSION cleanly.
  6675. // Hence the process termination might be safer :)
  6676. if ((Message.LParam != ENDSESSION_CRITICAL) &&
  6677. (((FQueue != NULL) && !FQueue->IsEmpty) || (FProgressForm != NULL)))
  6678. {
  6679. Message.Result = FALSE;
  6680. }
  6681. else
  6682. {
  6683. Message.Result = TRUE;
  6684. }
  6685. // Do not call default handling as that triggers OnCloseQuery,
  6686. // where our implementation will popup confirmation dialogs, what we do not want,
  6687. // as per Vista guidelines:
  6688. // https://docs.microsoft.com/en-us/windows/win32/shutdown/shutting-down
  6689. }
  6690. //---------------------------------------------------------------------------
  6691. void __fastcall TCustomScpExplorerForm::WMEndSession(TWMEndSession & Message)
  6692. {
  6693. if (Message.EndSession && IsApplicationMinimized())
  6694. {
  6695. // WORKAROUND
  6696. // TApplication.WndProc() calls Application.Terminate() before Halt(),
  6697. // when it receives WM_ENDSESSION,
  6698. // but that sometimes (particularly when application is minimized) cause crashes.
  6699. // Crash popups message that blocks log off.
  6700. // Obviously application cannot shutdown cleanly after WM_ENDSESSION,
  6701. // so we call ExitProcess() immediately, not even trying to cleanup.
  6702. // It still causes beep, so there's likely some popup, but it does not block
  6703. // log off.
  6704. ExitProcess(0);
  6705. }
  6706. TForm::Dispatch(&Message);
  6707. }
  6708. #ifdef _DEBUG
  6709. //---------------------------------------------------------------------------
  6710. void __fastcall TCustomScpExplorerForm::WMWindowPosChanged(TWMWindowPosMsg & Message)
  6711. {
  6712. TForm::Dispatch(&Message);
  6713. }
  6714. #endif
  6715. //---------------------------------------------------------------------------
  6716. void __fastcall TCustomScpExplorerForm::SysResizing(unsigned int /*Cmd*/)
  6717. {
  6718. }
  6719. //---------------------------------------------------------------------------
  6720. void __fastcall TCustomScpExplorerForm::DoShow()
  6721. {
  6722. // Called from TCustomScpExplorerForm::CMShowingChanged
  6723. // only now are the controls resized finally, so the size constraints
  6724. // will not conflict with possibly very small window size
  6725. RestoreFormParams();
  6726. // This indicates that the form was rescaled after parameters were loaded and
  6727. // as most probably the parameters were actually saved with the current form scaling
  6728. // (windows was the last time likely closed on the same monitor, where it is starting now),
  6729. // rescaling happened twice already. Reload the parameters, as now they likely won't need any scaling
  6730. // and no rounding problems will occur.
  6731. // See also RestoreForm.
  6732. // (we should be safe to call RestoreParams unconditionally)
  6733. if (PixelsPerInch != Screen->PixelsPerInch)
  6734. {
  6735. RestoreParams();
  6736. }
  6737. FixControlsPlacement();
  6738. if (Position == poDefaultPosOnly)
  6739. {
  6740. CutFormToDesktop(this);
  6741. }
  6742. TForm::DoShow();
  6743. FSessionsDragDropFilesEx->DragDropControl = SessionsPageControl;
  6744. FQueueDragDropFilesEx->DragDropControl = QueueView3;
  6745. // This was previously in NeedSession, where other related code is.
  6746. // But that is called from CMShowingChanged, when the window is already visible and the local-local window state
  6747. // unpleasanly shows briefly.
  6748. if ((ManagedSession == NULL) &&
  6749. !WinConfiguration->ShowLoginWhenNoSession &&
  6750. WinConfiguration->AutoSaveWorkspace && !WinConfiguration->AutoWorkspace.IsEmpty() &&
  6751. // This detects if workspace was saved the last time the main widow was closed
  6752. SameText(WinConfiguration->LastStoredSession, WinConfiguration->AutoWorkspace))
  6753. {
  6754. DoOpenFolderOrWorkspace(WinConfiguration->AutoWorkspace, false);
  6755. Configuration->Usage->Inc(L"OpenedWorkspacesAuto");
  6756. }
  6757. // Before main window displays
  6758. if (ManagedSession == NULL)
  6759. {
  6760. StartingWithoutSession();
  6761. }
  6762. UpdatePixelsPerInchMainWindowCounter();
  6763. FShowing = true;
  6764. }
  6765. //---------------------------------------------------------------------------
  6766. void __fastcall TCustomScpExplorerForm::UpdatePixelsPerInchMainWindowCounter()
  6767. {
  6768. Configuration->Usage->Set(L"PixelsPerInchMainWindow", PixelsPerInch);
  6769. }
  6770. //---------------------------------------------------------------------------
  6771. void __fastcall TCustomScpExplorerForm::StartingWithoutSession()
  6772. {
  6773. SessionListChanged();
  6774. InitStatusBar();
  6775. UpdateControls();
  6776. }
  6777. //---------------------------------------------------------------------------
  6778. void __fastcall TCustomScpExplorerForm::PopupTrayBalloon(TTerminal * Terminal,
  6779. const UnicodeString & Str, TQueryType Type, Exception * E, unsigned int Seconds,
  6780. TNotifyEvent OnBalloonClick, TObject * UserData)
  6781. {
  6782. bool Do;
  6783. UnicodeString Message;
  6784. if (E == NULL)
  6785. {
  6786. Message = Str;
  6787. Do = true;
  6788. }
  6789. else
  6790. {
  6791. Do = ExceptionMessage(E, Message);
  6792. }
  6793. if (Do && WinConfiguration->BalloonNotifications)
  6794. {
  6795. UnicodeString Title;
  6796. if ((Terminal == NULL) && (Type == qtInformation) && ExtractMainInstructions(Message, Title))
  6797. {
  6798. Message = TrimLeft(Message);
  6799. }
  6800. else
  6801. {
  6802. Message = UnformatMessage(Message);
  6803. const ResourceString * Captions[] = { &_SMsgDlgConfirm, &_SMsgDlgWarning,
  6804. &_SMsgDlgError, &_SMsgDlgInformation, NULL };
  6805. Title = LoadResourceString(Captions[Type]);
  6806. if (Terminal != NULL)
  6807. {
  6808. TManagedTerminal * Session = DebugNotNull(dynamic_cast<TManagedTerminal *>(Terminal));
  6809. Title = TTerminalManager::Instance()->GetSessionTitle(Session, true) + TitleSeparator + Title;
  6810. }
  6811. }
  6812. if (Seconds == 0)
  6813. {
  6814. Seconds = WinConfiguration->NotificationsTimeout;
  6815. }
  6816. FTrayIcon->PopupBalloon(Title, Message, Type, Seconds * MSecsPerSec, OnBalloonClick, UserData);
  6817. }
  6818. }
  6819. //---------------------------------------------------------------------------
  6820. unsigned int __fastcall TCustomScpExplorerForm::MoreMessageDialog(const UnicodeString Message,
  6821. TStrings * MoreMessages, TQueryType Type, unsigned int Answers,
  6822. UnicodeString HelpKeyword, const TMessageParams * Params,
  6823. TTerminal * Terminal)
  6824. {
  6825. if (((WinConfiguration->ContinueOnError && (FErrorList != NULL)) ||
  6826. (FOnFeedSynchronizeError != NULL)) &&
  6827. (Params != NULL) && (Params->Params & mpAllowContinueOnError) )
  6828. {
  6829. if (FOnFeedSynchronizeError != NULL)
  6830. {
  6831. FOnFeedSynchronizeError(Message, MoreMessages, Type, HelpKeyword);
  6832. }
  6833. else
  6834. {
  6835. DebugAssert(FErrorList != NULL);
  6836. TStringList * MoreMessagesCopy = NULL;
  6837. if (MoreMessages)
  6838. {
  6839. MoreMessagesCopy = new TStringList();
  6840. MoreMessagesCopy->Assign(MoreMessages);
  6841. }
  6842. FErrorList->AddObject(Message, MoreMessagesCopy);
  6843. }
  6844. PopupTrayBalloon(Terminal, Message, Type);
  6845. return ContinueAnswer(Answers);
  6846. }
  6847. else
  6848. {
  6849. bool UseBalloon = FTrayIcon->Visible;
  6850. if (UseBalloon)
  6851. {
  6852. PopupTrayBalloon(Terminal, Message, Type);
  6853. }
  6854. unsigned int Result;
  6855. try
  6856. {
  6857. Result = ::MoreMessageDialog(Message, MoreMessages, Type, Answers, HelpKeyword, Params);
  6858. }
  6859. __finally
  6860. {
  6861. // cancel only balloon we popped up, otherwise we may cancel notification
  6862. // balloon that was there before the message dialog
  6863. // (such as "dd confirmation opt in balloon)
  6864. if (UseBalloon)
  6865. {
  6866. FTrayIcon->CancelBalloon();
  6867. }
  6868. }
  6869. return Result;
  6870. }
  6871. }
  6872. //---------------------------------------------------------------------------
  6873. void __fastcall TCustomScpExplorerForm::ShowExtendedException(
  6874. TTerminal * Terminal, Exception * E)
  6875. {
  6876. if (FTrayIcon->Visible)
  6877. {
  6878. PopupTrayBalloon(Terminal, L"", qtError, E);
  6879. }
  6880. // particularly prevent opening new session from jump list,
  6881. // while exception is shown
  6882. NonVisualDataModule->StartBusy();
  6883. try
  6884. {
  6885. ShowExtendedExceptionEx(Terminal, E);
  6886. }
  6887. __finally
  6888. {
  6889. NonVisualDataModule->EndBusy();
  6890. FTrayIcon->CancelBalloon();
  6891. }
  6892. }
  6893. //---------------------------------------------------------------------------
  6894. void __fastcall TCustomScpExplorerForm::SessionReady()
  6895. {
  6896. InitStatusBar();
  6897. // cannot rely on active page being page for active terminal,
  6898. // as it can happen that active page is the "new session" page
  6899. // (e.g. when reconnecting active terminal, while login dialog
  6900. // invoked from "new session" page is modal)
  6901. int ActiveSessionIndex = TTerminalManager::Instance()->ActiveSessionIndex;
  6902. UpdateSessionTab(SessionsPageControl->Pages[ActiveSessionIndex]);
  6903. }
  6904. //---------------------------------------------------------------------------
  6905. void __fastcall TCustomScpExplorerForm::InactiveTerminalException(
  6906. TTerminal * Terminal, Exception * E)
  6907. {
  6908. Notify(Terminal, L"", qtError, false, NULL, NULL, E);
  6909. if (!Terminal->Active)
  6910. {
  6911. int Index = TTerminalManager::Instance()->IndexOf(Terminal);
  6912. if (DebugAlwaysTrue((Index >= 0) && (Index < SessionsPageControl->PageCount)))
  6913. {
  6914. UpdateSessionTab(SessionsPageControl->Pages[Index]);
  6915. }
  6916. }
  6917. }
  6918. //---------------------------------------------------------------------------
  6919. void __fastcall TCustomScpExplorerForm::Notify(TTerminal * Terminal,
  6920. UnicodeString Message, TQueryType Type,
  6921. bool Important, TNotifyEvent OnClick, TObject * UserData, Exception * E)
  6922. {
  6923. if ((E == NULL) ||
  6924. ExceptionMessage(E, Message))
  6925. {
  6926. unsigned int Seconds = WinConfiguration->NotificationsTimeout;
  6927. if (Important)
  6928. {
  6929. Seconds *= 5;
  6930. }
  6931. UnicodeString NoteMessage(UnformatMessage(Message));
  6932. if (Terminal != NULL)
  6933. {
  6934. TManagedTerminal * Session = DebugNotNull(dynamic_cast<TManagedTerminal *>(Terminal));
  6935. NoteMessage = FORMAT(L"%s: %s",
  6936. (TTerminalManager::Instance()->GetSessionTitle(Session, true), NoteMessage));
  6937. }
  6938. if (WinConfiguration->BalloonNotifications)
  6939. {
  6940. AddNote(NoteMessage);
  6941. PopupTrayBalloon(Terminal, Message, Type, NULL, Seconds, OnClick, UserData);
  6942. }
  6943. else
  6944. {
  6945. FlashOnBackground();
  6946. PostNote(NoteMessage, Seconds, OnClick, UserData);
  6947. }
  6948. }
  6949. }
  6950. //---------------------------------------------------------------------------
  6951. void __fastcall TCustomScpExplorerForm::QueueEmptyNoteClicked(TObject * Sender)
  6952. {
  6953. RestoreApp();
  6954. TTerminalNoteData * TerminalNoteData = dynamic_cast<TTerminalNoteData *>(Sender);
  6955. if (DebugAlwaysTrue(TerminalNoteData != NULL) &&
  6956. !NonVisualDataModule->Busy)
  6957. {
  6958. TManagedTerminal * Terminal = TerminalNoteData->Terminal;
  6959. TTerminalManager::Instance()->ActiveSession = Terminal;
  6960. if (!ComponentVisible[fcQueueView])
  6961. {
  6962. ToggleQueueVisibility();
  6963. GoToQueue();
  6964. }
  6965. }
  6966. }
  6967. //---------------------------------------------------------------------------
  6968. void __fastcall TCustomScpExplorerForm::QueueEvent(TManagedTerminal * ATerminal,
  6969. TTerminalQueue * /*Queue*/, TQueueEvent Event)
  6970. {
  6971. UnicodeString Message;
  6972. TNotifyEvent OnClick = NULL;
  6973. TObject * UserData = NULL;
  6974. bool QueueInvisible = !ComponentVisible[fcQueueView] || IsApplicationMinimized();
  6975. switch (Event)
  6976. {
  6977. case qeEmptyButMonitored:
  6978. if ((ATerminal != Terminal) || QueueInvisible)
  6979. {
  6980. Message = LoadStr(BALLOON_QUEUE_EMPTY);
  6981. OnClick = QueueEmptyNoteClicked;
  6982. TTerminalNoteData * TerminalNoteData = new TTerminalNoteData();
  6983. TerminalNoteData->Terminal = ATerminal;
  6984. UserData = TerminalNoteData;
  6985. }
  6986. break;
  6987. case qeEmpty:
  6988. OperationComplete(ATerminal->QueueOperationStart);
  6989. break;
  6990. case qePendingUserAction:
  6991. if ((ATerminal != Terminal) ||
  6992. (QueueInvisible && !IsQueueAutoPopup()))
  6993. {
  6994. Message = LoadStr(BALLOON_QUEUE_USER_ACTION);
  6995. }
  6996. break;
  6997. default:
  6998. DebugFail();
  6999. }
  7000. if (!Message.IsEmpty())
  7001. {
  7002. Notify(ATerminal, Message, qtInformation, false, OnClick, UserData);
  7003. }
  7004. }
  7005. //---------------------------------------------------------------------------
  7006. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDCreateDragFileList(
  7007. TObject * /*Sender*/, TFileList * FileList, bool & Created)
  7008. {
  7009. if (FDDExtMapFile != NULL)
  7010. {
  7011. CloseHandle(FDDExtMapFile);
  7012. FDDExtMapFile = NULL;
  7013. }
  7014. if (WinConfiguration->DDFakeFile)
  7015. {
  7016. DDFakeFileInitDrag(FileList, Created);
  7017. }
  7018. }
  7019. //---------------------------------------------------------------------------
  7020. void __fastcall TCustomScpExplorerForm::DDFakeCreated(TObject * /*Sender*/, const UnicodeString FileName)
  7021. {
  7022. if (DebugAlwaysTrue(!FDragFakeDirectory.IsEmpty()) &&
  7023. SameText(ExtractFileName(FileName), ExtractFileName(FDragFakeDirectory)))
  7024. {
  7025. FFakeFileDropTarget = FileName;
  7026. }
  7027. }
  7028. //---------------------------------------------------------------------------
  7029. UnicodeString __fastcall TCustomScpExplorerForm::CreateFakeTransferDirectory()
  7030. {
  7031. UnicodeString Result = ExcludeTrailingBackslash(WinConfiguration->TemporaryDir());
  7032. if (!ForceDirectories(ApiPath(Result)))
  7033. {
  7034. throw Exception(FMTLOAD(CREATE_TEMP_DIR_ERROR, (Result)));
  7035. }
  7036. FileSetAttr(ApiPath(Result), faHidden);
  7037. return Result;
  7038. }
  7039. //---------------------------------------------------------------------------
  7040. void __fastcall TCustomScpExplorerForm::DDFakeFileInitDrag(TFileList * FileList,
  7041. bool & Created)
  7042. {
  7043. FFakeFileDropTarget = UnicodeString();
  7044. FDragFakeDirectory = CreateFakeTransferDirectory();
  7045. FileList->AddItem(NULL, FDragFakeDirectory);
  7046. Created = true;
  7047. if (!WinConfiguration->IsDDExtRunning() || WinConfiguration->IsDDExtBroken())
  7048. {
  7049. FDragFakeMonitors = StartCreationDirectoryMonitorsOnEachDrive(FILE_NOTIFY_CHANGE_DIR_NAME, DDFakeCreated);
  7050. }
  7051. FDDExtMapFile = CreateFileMappingA((HANDLE)0xFFFFFFFF, NULL, PAGE_READWRITE,
  7052. 0, sizeof(TDragExtCommStruct), AnsiString(DRAG_EXT_MAPPING).c_str());
  7053. {
  7054. TMutexGuard Guard(FDDExtMutex, DRAGEXT_MUTEX_RELEASE_TIMEOUT);
  7055. TDragExtCommStruct* CommStruct;
  7056. CommStruct = static_cast<TDragExtCommStruct*>(MapViewOfFile(FDDExtMapFile,
  7057. FILE_MAP_ALL_ACCESS, 0, 0, 0));
  7058. DebugAssert(CommStruct != NULL);
  7059. CommStruct->Version = TDragExtCommStruct::CurrentVersion;
  7060. CommStruct->Dragging = true;
  7061. wcsncpy(CommStruct->DropDest, FDragFakeDirectory.c_str(),
  7062. LENOF(CommStruct->DropDest));
  7063. NULL_TERMINATE(CommStruct->DropDest);
  7064. UnmapViewOfFile(CommStruct);
  7065. }
  7066. }
  7067. //---------------------------------------------------------------------------
  7068. bool __fastcall TCustomScpExplorerForm::RemoteFileControlFileOperation(
  7069. TObject * Sender, TFileOperation Operation, bool NoConfirmation, void * Param)
  7070. {
  7071. bool Result;
  7072. DebugAssert(!IsLocalBrowserMode());
  7073. if (Sender == RemoteDirView)
  7074. {
  7075. Result = ExecuteFileOperation(Operation, osRemote, true, NoConfirmation, Param);
  7076. }
  7077. else
  7078. {
  7079. DebugAssert(Sender == RemoteDriveView);
  7080. TStrings * FileList = RemoteDriveView->DragFileList();
  7081. try
  7082. {
  7083. Result = ExecuteFileOperation(Operation, osRemote, FileList, NoConfirmation, Param);
  7084. }
  7085. __finally
  7086. {
  7087. delete FileList;
  7088. }
  7089. }
  7090. if (FDDTargetControl == RemoteDriveView)
  7091. {
  7092. RemoteDriveView->UpdateDropTarget();
  7093. }
  7094. // foRemoteMove happens when file/dir is dragged within the remote tree view
  7095. // or from tree view to dir view.
  7096. // foMove happens when file/dir is dragged from remote tree view outside of
  7097. // application via dragex
  7098. if (((Operation == foRemoteMove) || (Operation == foMove)) &&
  7099. (DropSourceControl == RemoteDriveView))
  7100. {
  7101. RemoteDriveView->UpdateDropSource();
  7102. }
  7103. return Result;
  7104. }
  7105. //---------------------------------------------------------------------------
  7106. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDEnd(TObject * Sender)
  7107. {
  7108. DebugAssert(!IsLocalBrowserMode());
  7109. // This also handles drops of remote files to queue.
  7110. // Drops of local files (uploads) are handled in QueueDDProcessDropped.
  7111. SAFE_DESTROY(FDDFileList);
  7112. if (!FFakeFileDropTarget.IsEmpty())
  7113. {
  7114. RemoveDir(ApiPath(FFakeFileDropTarget));
  7115. }
  7116. if ((FDDExtMapFile != NULL) || (FDDTargetControl == QueueView3))
  7117. {
  7118. try
  7119. {
  7120. TDragResult DDResult = (Sender == RemoteDirView) ?
  7121. RemoteDirView->LastDDResult : RemoteDriveView->LastDDResult;
  7122. // Focus is moved to the target application,
  7123. // but as we are going to present the UI, we need to steal the focus back.
  7124. // This most likely won't work though (windows does not allow application
  7125. // to steal focus most of the time)
  7126. Application->BringToFront();
  7127. // On older version of Windows we never got drMove here, see also comment below.
  7128. // On Windows 10, we get the "move".
  7129. if ((DDResult == drCopy) || (DDResult == drMove) || (DDResult == drInvalid))
  7130. {
  7131. UnicodeString TargetDirectory;
  7132. TFileOperation Operation;
  7133. // drInvalid may mean drMove, see comment below
  7134. switch (DDResult)
  7135. {
  7136. case drCopy:
  7137. Operation = foCopy;
  7138. break;
  7139. case drMove:
  7140. Operation = foMove;
  7141. break;
  7142. default:
  7143. DebugFail();
  7144. case drInvalid:
  7145. // prefer "copy" for safety
  7146. Operation = FLAGSET(FLastDropEffect, DROPEFFECT_MOVE) ? foMove : foCopy;
  7147. break;
  7148. }
  7149. TTransferOperationParam Param;
  7150. UnicodeString CounterName;
  7151. bool ForceQueue;
  7152. if (!DDGetTarget(Param.TargetDirectory, ForceQueue, CounterName))
  7153. {
  7154. // we get drInvalid both if d&d was intercepted by ddext,
  7155. // and when users drops on no-drop location.
  7156. // we tell the difference by existence of response from ddext,
  7157. // so we ignore absence of response in this case
  7158. if (DDResult != drInvalid)
  7159. {
  7160. // here we know that the extension is installed,
  7161. // as it is checked as soon as drag&drop starts from
  7162. // RemoteFileControlDDCreateDragFileList
  7163. Configuration->Usage->Inc(L"DownloadsDragDropExternalExtTargetUnknown");
  7164. if (!WinConfiguration->DDExtInstalled || WinConfiguration->IsDDExtBroken())
  7165. {
  7166. throw ExtException(NULL, LoadStr(DD_TARGET_UNKNOWN), HELP_DD_TARGET_UNKNOWN);
  7167. }
  7168. else
  7169. {
  7170. throw ExtException(NULL, LoadStr(DRAGEXT_TARGET_UNKNOWN2), HELP_DRAGEXT_TARGET_UNKNOWN);
  7171. }
  7172. }
  7173. }
  7174. else
  7175. {
  7176. // download using fake file
  7177. Param.Temp = false;
  7178. Param.DragDrop = true;
  7179. if (ForceQueue)
  7180. {
  7181. Param.Queue = asOn;
  7182. }
  7183. if (Sender == RemoteDirView)
  7184. {
  7185. Param.Options = FLAGMASK(SelectedAllFilesInDirView(RemoteDirView), coAllFiles);
  7186. }
  7187. if (RemoteFileControlFileOperation(Sender, Operation,
  7188. (WinConfiguration->DDTransferConfirmation == asOff), &Param))
  7189. {
  7190. Configuration->Usage->Inc(CounterName);
  7191. }
  7192. }
  7193. }
  7194. }
  7195. __finally
  7196. {
  7197. CloseHandle(FDDExtMapFile);
  7198. FDDExtMapFile = NULL;
  7199. delete FDragFakeMonitors;
  7200. FDragFakeMonitors = NULL;
  7201. RemoveDir(ApiPath(FDragFakeDirectory));
  7202. FDragFakeDirectory = L"";
  7203. FFakeFileDropTarget = UnicodeString();
  7204. }
  7205. }
  7206. if (FDragDropSshTerminate.get() != NULL)
  7207. {
  7208. FDragDropSshTerminate->Rethrow();
  7209. }
  7210. }
  7211. //---------------------------------------------------------------------------
  7212. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDGiveFeedback(
  7213. TObject * /*Sender*/, int dwEffect, HRESULT & /*Result*/)
  7214. {
  7215. // Remember drop effect so we know (when user drops files), if we copy or move
  7216. FLastDropEffect = dwEffect;
  7217. }
  7218. //---------------------------------------------------------------------------
  7219. bool __fastcall TCustomScpExplorerForm::DDGetTarget(
  7220. UnicodeString & Directory, bool & ForceQueue, UnicodeString & CounterName)
  7221. {
  7222. bool Result;
  7223. if (FDDTargetControl == QueueView3)
  7224. {
  7225. Directory = DefaultDownloadTargetDirectory();
  7226. Result = true;
  7227. CounterName = L"DownloadsDragDropQueue";
  7228. ForceQueue = true;
  7229. }
  7230. else if (!FFakeFileDropTarget.IsEmpty())
  7231. {
  7232. Directory = ExcludeTrailingBackslash(ExtractFilePath(FFakeFileDropTarget));
  7233. Result = true;
  7234. ForceQueue = false;
  7235. CounterName = L"DownloadsDragDropFakeFile";
  7236. }
  7237. else
  7238. {
  7239. ForceQueue = false;
  7240. Enabled = false;
  7241. try
  7242. {
  7243. int Timer = 0;
  7244. Result = false;
  7245. while (!Result && (Timer < WinConfiguration->DDExtTimeout))
  7246. {
  7247. {
  7248. TMutexGuard Guard(FDDExtMutex, DRAGEXT_MUTEX_RELEASE_TIMEOUT);
  7249. TDragExtCommStruct* CommStruct;
  7250. CommStruct = static_cast<TDragExtCommStruct*>(MapViewOfFile(FDDExtMapFile,
  7251. FILE_MAP_ALL_ACCESS, 0, 0, 0));
  7252. DebugAssert(CommStruct != NULL);
  7253. Result = !CommStruct->Dragging;
  7254. if (Result)
  7255. {
  7256. Directory = ExtractFilePath(CommStruct->DropDest);
  7257. CounterName = L"DownloadsDragDropExternalExt";
  7258. }
  7259. UnmapViewOfFile(CommStruct);
  7260. }
  7261. if (!Result)
  7262. {
  7263. Sleep(50);
  7264. Timer += 50;
  7265. Application->ProcessMessages();
  7266. }
  7267. }
  7268. }
  7269. __finally
  7270. {
  7271. Enabled = true;
  7272. }
  7273. }
  7274. return Result;
  7275. }
  7276. //---------------------------------------------------------------------------
  7277. void __fastcall TCustomScpExplorerForm::AddDelayedDirectoryDeletion(
  7278. const UnicodeString TempDir, int SecDelay)
  7279. {
  7280. TDateTime Alarm = IncSecond(Now(), SecDelay);
  7281. FDelayedDeletionList->AddObject(TempDir, reinterpret_cast<TObject*>(Alarm.FileDate()));
  7282. if (FDelayedDeletionTimer == NULL)
  7283. {
  7284. DebugAssert(HandleAllocated());
  7285. FDelayedDeletionTimer = new TTimer(this);
  7286. FDelayedDeletionTimer->Interval = 10000;
  7287. FDelayedDeletionTimer->OnTimer = DoDelayedDeletion;
  7288. }
  7289. else
  7290. {
  7291. FDelayedDeletionTimer->Enabled = true;
  7292. }
  7293. }
  7294. //---------------------------------------------------------------------------
  7295. void __fastcall TCustomScpExplorerForm::DoDelayedDeletion(TObject * Sender)
  7296. {
  7297. DebugAssert(FDelayedDeletionList != NULL);
  7298. TDateTime N = Now();
  7299. TDateTime Alert;
  7300. UnicodeString Directory;
  7301. for (int Index = FDelayedDeletionList->Count-1; Index >= 0; Index--)
  7302. {
  7303. Alert = FileDateToDateTime(reinterpret_cast<int>(FDelayedDeletionList->Objects[Index]));
  7304. if ((N >= Alert) || (Sender == NULL))
  7305. {
  7306. Directory = FDelayedDeletionList->Strings[Index];
  7307. if (DeleteDirectory(ExcludeTrailingBackslash(Directory)))
  7308. {
  7309. FDelayedDeletionList->Delete(Index);
  7310. }
  7311. }
  7312. }
  7313. if (FDelayedDeletionList->Count == 0)
  7314. {
  7315. FDelayedDeletionTimer->Enabled = false;
  7316. }
  7317. }
  7318. //---------------------------------------------------------------------------
  7319. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDTargetDrop()
  7320. {
  7321. DebugAssert(!IsLocalBrowserMode());
  7322. if (IsFileControl(FDDTargetControl, osRemote) ||
  7323. (FDDTargetControl == SessionsPageControl))
  7324. {
  7325. TManagedTerminal * TargetTerminal = NULL;
  7326. TFileOperation Operation = foNone;
  7327. if (FDDTargetControl == SessionsPageControl)
  7328. {
  7329. TPoint Point = SessionsPageControl->ScreenToClient(Mouse->CursorPos);
  7330. int Index = SessionsPageControl->IndexOfTabAt(Point.X, Point.Y);
  7331. // do not allow dropping on local-local, disconnected and the "+" tab (see also SessionsDDProcessDropped)
  7332. TargetTerminal = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  7333. if (IsActiveTerminal(TargetTerminal) && DebugAlwaysTrue(!TargetTerminal->LocalBrowser))
  7334. {
  7335. if ((FLastDropEffect == DROPEFFECT_MOVE) &&
  7336. (TargetTerminal == TTerminalManager::Instance()->ActiveTerminal))
  7337. {
  7338. Operation = foRemoteMove;
  7339. }
  7340. else
  7341. {
  7342. Operation = foRemoteCopy;
  7343. }
  7344. }
  7345. }
  7346. else
  7347. {
  7348. if (FLastDropEffect == DROPEFFECT_MOVE)
  7349. {
  7350. Operation = foRemoteMove;
  7351. }
  7352. else if (FLastDropEffect == DROPEFFECT_COPY)
  7353. {
  7354. Operation = foRemoteCopy;
  7355. }
  7356. }
  7357. if (Operation != foNone)
  7358. {
  7359. RemoteFileControlFileOperation(DropSourceControl,
  7360. Operation, (WinConfiguration->DDTransferConfirmation == asOff), TargetTerminal);
  7361. }
  7362. // abort drag&drop
  7363. Abort();
  7364. }
  7365. else if ((FDDExtMapFile == NULL) && (FLastDropEffect != DROPEFFECT_NONE) &&
  7366. // Drops of remote files to queue are handled in RemoteFileControlDDEnd
  7367. (FDDTargetControl != QueueView3))
  7368. {
  7369. DebugAssert(!FDragTempDir.IsEmpty());
  7370. TTransferType Type;
  7371. UnicodeString TempDir = FDragTempDir;
  7372. // We clear FDragTempDir before calling
  7373. // just in case it fail (raises exception)
  7374. FDragTempDir = L"";
  7375. Type = (FLastDropEffect & DROPEFFECT_MOVE ? ttMove : Type = ttCopy);
  7376. TGUICopyParamType CopyParams = GUIConfiguration->CurrentCopyParam;
  7377. // empty directory parameter means temp directory -> don't display it!
  7378. UnicodeString TargetDir = L"";
  7379. int Options = 0;
  7380. if (!CopyParamDialog(tdToLocal, Type, true, FDDFileList,
  7381. TargetDir, CopyParams, (WinConfiguration->DDTransferConfirmation != asOff), true, Options))
  7382. {
  7383. Abort();
  7384. }
  7385. // TargetDir is set when dropped on local file control
  7386. // (this was workaround for legacy dirview event handling, now it should be
  7387. // made prettier)
  7388. if (TargetDir.IsEmpty())
  7389. {
  7390. TargetDir = TempDir;
  7391. if (ForceDirectories(ApiPath(TargetDir)))
  7392. {
  7393. DebugAssert(Terminal && !TargetDir.IsEmpty());
  7394. FPendingTempSpaceWarn = true;
  7395. try
  7396. {
  7397. FDragDropOperation = true;
  7398. int Params = cpTemporary |
  7399. (Type == ttMove ? cpDelete : 0);
  7400. DDDownload(FDDFileList, TargetDir, &CopyParams, Params);
  7401. Configuration->Usage->Inc(L"DownloadsDragDropExternalTemp");
  7402. }
  7403. __finally
  7404. {
  7405. FDragDropOperation = false;
  7406. FPendingTempSpaceWarn = false;
  7407. AddDelayedDirectoryDeletion(TargetDir, WinConfiguration->DDDeleteDelay);
  7408. }
  7409. }
  7410. else
  7411. {
  7412. throw Exception(FMTLOAD(CREATE_TEMP_DIR_ERROR, (TargetDir)));
  7413. }
  7414. }
  7415. }
  7416. }
  7417. //---------------------------------------------------------------------------
  7418. void __fastcall TCustomScpExplorerForm::DDDownload(
  7419. TStrings * FilesToCopy, const UnicodeString & TargetDir, const TCopyParamType * CopyParam, int Params)
  7420. {
  7421. DebugAssert(!IsLocalBrowserMode());
  7422. TAutoBatch AutoBatch(this);
  7423. UpdateCopyParamCounters(*CopyParam);
  7424. Terminal->CopyToLocal(FilesToCopy, TargetDir, CopyParam, Params, NULL);
  7425. if (FLAGSET(Params, cpDelete) && (DropSourceControl == RemoteDriveView))
  7426. {
  7427. RemoteDriveView->UpdateDropSource();
  7428. }
  7429. }
  7430. //---------------------------------------------------------------------------
  7431. class TFakeDataObjectFilesEx : public TDataObjectFilesEx
  7432. {
  7433. public:
  7434. __fastcall TFakeDataObjectFilesEx(TFileList * AFileList, bool RenderPIDL,
  7435. bool RenderFilename) : TDataObjectFilesEx(AFileList, RenderPIDL, RenderFilename, true)
  7436. {
  7437. }
  7438. virtual bool __fastcall AllowData(const tagFORMATETC & FormatEtc)
  7439. {
  7440. return (FormatEtc.cfFormat == CF_HDROP) ? false :
  7441. TDataObjectFilesEx::AllowData(FormatEtc);
  7442. }
  7443. };
  7444. //---------------------------------------------------------------------------
  7445. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDCreateDataObject(
  7446. TObject * Sender, TDataObject *& DataObject)
  7447. {
  7448. if (FDDExtMapFile != NULL)
  7449. {
  7450. TFileList * FileList = DragDropFiles(Sender)->FileList;
  7451. if (!FileList->RenderPIDLs() || !FileList->RenderNames())
  7452. {
  7453. Abort();
  7454. }
  7455. if (FileList->Count > 0)
  7456. {
  7457. TDataObjectFilesEx * FilesObject = new TFakeDataObjectFilesEx(FileList, true, true);
  7458. if (!FilesObject->IsValid(true, true))
  7459. {
  7460. FilesObject->_Release();
  7461. }
  7462. else
  7463. {
  7464. DataObject = FilesObject;
  7465. }
  7466. }
  7467. }
  7468. }
  7469. //---------------------------------------------------------------------------
  7470. void __fastcall TCustomScpExplorerForm::GoToCommandLine()
  7471. {
  7472. DebugFail();
  7473. }
  7474. //---------------------------------------------------------------------------
  7475. void __fastcall TCustomScpExplorerForm::GoToTree()
  7476. {
  7477. ComponentVisible[fcRemoteTree] = true;
  7478. DriveView(osRemote)->SetFocus();
  7479. }
  7480. //---------------------------------------------------------------------------
  7481. TStrings * __fastcall TCustomScpExplorerForm::PanelExport(TOperationSide Side,
  7482. TPanelExport Export)
  7483. {
  7484. TCustomDirView * DirView = this->DirView(Side);
  7485. std::unique_ptr<TStrings> ExportData(new TStringList());
  7486. switch (Export)
  7487. {
  7488. case pePath:
  7489. ExportData->Add(DirView->PathName);
  7490. break;
  7491. case peFileList:
  7492. case peFullFileList:
  7493. {
  7494. bool FullPath = (Export == peFullFileList);
  7495. DirView->CreateFileList(false, FullPath, ExportData.get());
  7496. for (int Index = 0; Index < ExportData->Count; Index++)
  7497. {
  7498. if (ExportData->Strings[Index].Pos(L" ") > 0)
  7499. {
  7500. ExportData->Strings[Index] = FORMAT(L"\"%s\"", (ExportData->Strings[Index]));
  7501. }
  7502. }
  7503. }
  7504. break;
  7505. default:
  7506. DebugFail();
  7507. }
  7508. return ExportData.release();
  7509. }
  7510. //---------------------------------------------------------------------------
  7511. void __fastcall TCustomScpExplorerForm::PanelExport(TOperationSide Side,
  7512. TPanelExport Export, TPanelExportDestination Destination)
  7513. {
  7514. std::unique_ptr<TStrings> ExportData(PanelExport(Side, Export));
  7515. PanelExportStore(Side, Export, Destination, ExportData.get());
  7516. }
  7517. //---------------------------------------------------------------------------
  7518. void __fastcall TCustomScpExplorerForm::PanelExportStore(TOperationSide /*Side*/,
  7519. TPanelExport /*Export*/, TPanelExportDestination Destination,
  7520. TStrings * ExportData)
  7521. {
  7522. if (Destination == pedClipboard)
  7523. {
  7524. TInstantOperationVisualizer Visualizer;
  7525. CopyToClipboard(ExportData);
  7526. }
  7527. else
  7528. {
  7529. DebugFail();
  7530. }
  7531. }
  7532. //---------------------------------------------------------------------------
  7533. void __fastcall TCustomScpExplorerForm::Filter(TOperationSide Side)
  7534. {
  7535. TCustomDirView * DirView = this->DirView(Side);
  7536. UnicodeString Mask = DirView->Mask;
  7537. if (DoFilterMaskDialog(DirView, Mask))
  7538. {
  7539. DirView->Mask = TFileMasks::NormalizeMask(Mask);
  7540. Configuration->Usage->Inc(L"Filters");
  7541. }
  7542. }
  7543. //---------------------------------------------------------------------------
  7544. TQueueOperation __fastcall TCustomScpExplorerForm::DefaultQueueOperation()
  7545. {
  7546. return FQueueController->DefaultOperation();
  7547. }
  7548. //---------------------------------------------------------------------------
  7549. bool __fastcall TCustomScpExplorerForm::AllowQueueOperation(
  7550. TQueueOperation Operation, void ** Param)
  7551. {
  7552. switch (Operation)
  7553. {
  7554. case qoPreferences:
  7555. return true;
  7556. case qoGoTo:
  7557. return ComponentVisible[fcQueueView] && QueueView3->Enabled;
  7558. case qoOnceEmpty:
  7559. return IsAnythingQueued();
  7560. default:
  7561. return FQueueController->AllowOperation(Operation, Param);
  7562. }
  7563. }
  7564. //---------------------------------------------------------------------------
  7565. void __fastcall TCustomScpExplorerForm::GoToQueue()
  7566. {
  7567. if (DebugAlwaysTrue(QueueView3->Visible))
  7568. {
  7569. QueueView3->SetFocus();
  7570. }
  7571. }
  7572. //---------------------------------------------------------------------------
  7573. void __fastcall TCustomScpExplorerForm::ExecuteQueueOperation(
  7574. TQueueOperation Operation, void * Param)
  7575. {
  7576. if (Operation == qoGoTo)
  7577. {
  7578. GoToQueue();
  7579. }
  7580. else if (Operation == qoPreferences)
  7581. {
  7582. PreferencesDialog(pmQueue);
  7583. }
  7584. else
  7585. {
  7586. FQueueController->ExecuteOperation(Operation, Param);
  7587. }
  7588. }
  7589. //---------------------------------------------------------------------------
  7590. bool __fastcall TCustomScpExplorerForm::GetQueueEnabled()
  7591. {
  7592. return (Queue != NULL) && Queue->Enabled;
  7593. }
  7594. //---------------------------------------------------------------------------
  7595. void __fastcall TCustomScpExplorerForm::ToggleQueueEnabled()
  7596. {
  7597. DebugAssert(Queue != NULL);
  7598. if (Queue != NULL)
  7599. {
  7600. Queue->Enabled = !Queue->Enabled;
  7601. }
  7602. }
  7603. //---------------------------------------------------------------------------
  7604. void __fastcall TCustomScpExplorerForm::QueueView3ContextPopup(
  7605. TObject * /*Sender*/, TPoint & /*MousePos*/, bool & /*Handled*/)
  7606. {
  7607. FQueueActedItem = QueueView3->ItemFocused;
  7608. }
  7609. //---------------------------------------------------------------------------
  7610. /*virtual*/ int __fastcall TCustomScpExplorerForm::GetStaticComponentsHeight()
  7611. {
  7612. return TopDock->Height + QueueSplitter->Height;
  7613. }
  7614. //---------------------------------------------------------------------------
  7615. int __fastcall TCustomScpExplorerForm::GetStaticQueuePanelComponentsHeight()
  7616. {
  7617. return
  7618. (QueueFileListSplitter->Visible ? QueueFileListSplitter->Height : 0) +
  7619. (QueueDock->Visible ? QueueDock->Height : 0) +
  7620. (QueueLabel->Visible ? QueueLabel->Height : 0);
  7621. }
  7622. //---------------------------------------------------------------------------
  7623. int __fastcall TCustomScpExplorerForm::GetMinQueueViewHeight()
  7624. {
  7625. return ScaleByTextHeight(this, 48);
  7626. }
  7627. //---------------------------------------------------------------------------
  7628. void __fastcall TCustomScpExplorerForm::QueueSplitterCanResize(
  7629. TObject * /*Sender*/, int & NewSize, bool & Accept)
  7630. {
  7631. // when queue is hidden by double-clicking splitter, stray attempt to
  7632. // resize the panel with strange value arrives, make sure it is ignored
  7633. if (ComponentVisible[fcQueueView])
  7634. {
  7635. int MaxHeightLimit = ClientHeight - GetStaticComponentsHeight() - RemotePanel->Constraints->MinHeight;
  7636. if (NewSize > MaxHeightLimit)
  7637. {
  7638. NewSize = MaxHeightLimit;
  7639. }
  7640. int MinHeightLimit =
  7641. GetStaticQueuePanelComponentsHeight() +
  7642. (QueueFileList->Visible ? QueueFileList->Height : 0) +
  7643. GetMinQueueViewHeight();
  7644. if (NewSize < MinHeightLimit)
  7645. {
  7646. NewSize = MinHeightLimit;
  7647. }
  7648. }
  7649. else
  7650. {
  7651. Accept = false;
  7652. }
  7653. }
  7654. //---------------------------------------------------------------------------
  7655. void __fastcall TCustomScpExplorerForm::QueueFileListSplitterCanResize(TObject *, int & NewSize, bool &)
  7656. {
  7657. int TotalHeight = GetStaticQueuePanelComponentsHeight() + NewSize;
  7658. int QueueViewHeight = QueuePanel->ClientHeight - TotalHeight;
  7659. if (QueueViewHeight < GetMinQueueViewHeight())
  7660. {
  7661. NewSize -= (GetMinQueueViewHeight() - QueueViewHeight);
  7662. }
  7663. }
  7664. //---------------------------------------------------------------------------
  7665. void __fastcall TCustomScpExplorerForm::QueueView3StartDrag(TObject * /*Sender*/,
  7666. TDragObject *& /*DragObject*/)
  7667. {
  7668. FQueueActedItem = QueueView3->ItemFocused;
  7669. }
  7670. //---------------------------------------------------------------------------
  7671. void __fastcall TCustomScpExplorerForm::QueueView3DragOver(TObject * /*Sender*/,
  7672. TObject * Source, int X, int Y, TDragState /*State*/, bool & Accept)
  7673. {
  7674. Accept = true;
  7675. if (Source == QueueView3)
  7676. {
  7677. TListItem * DropTarget = QueueView3->GetItemAt(X, Y);
  7678. Accept = (DropTarget != NULL) && (FQueueActedItem != NULL);
  7679. if (Accept)
  7680. {
  7681. TQueueItemProxy * QueueItem;
  7682. TQueueItemProxy * DestQueueItem;
  7683. QueueItem = static_cast<TQueueItemProxy *>(FQueueActedItem->Data);
  7684. DestQueueItem = static_cast<TQueueItemProxy *>(DropTarget->Data);
  7685. Accept = (QueueItem != DestQueueItem) &&
  7686. (QueueItem->Status == TQueueItem::qsPending) &&
  7687. (DestQueueItem->Status == TQueueItem::qsPending);
  7688. }
  7689. }
  7690. }
  7691. //---------------------------------------------------------------------------
  7692. void __fastcall TCustomScpExplorerForm::QueueView3DragDrop(TObject * /*Sender*/,
  7693. TObject * /*Source*/, int /*X*/, int /*Y*/)
  7694. {
  7695. if ((FQueueActedItem != NULL) && (QueueView3->DropTarget != NULL))
  7696. {
  7697. TQueueItemProxy * QueueItem;
  7698. TQueueItemProxy * DestQueueItem;
  7699. QueueItem = static_cast<TQueueItemProxy *>(FQueueActedItem->Data);
  7700. DestQueueItem = static_cast<TQueueItemProxy *>(QueueView3->DropTarget->Data);
  7701. QueueItem->Move(DestQueueItem);
  7702. }
  7703. }
  7704. //---------------------------------------------------------------------------
  7705. void __fastcall TCustomScpExplorerForm::QueueView3Enter(TObject * /*Sender*/)
  7706. {
  7707. if ((QueueView3->ItemFocused == NULL) &&
  7708. (QueueView3->Items->Count > 0))
  7709. {
  7710. QueueView3->ItemFocused = QueueView3->Items->Item[0];
  7711. }
  7712. QueueLabelUpdateStatus();
  7713. }
  7714. //---------------------------------------------------------------------------
  7715. void __fastcall TCustomScpExplorerForm::QueueView3Exit(TObject * /*Sender*/)
  7716. {
  7717. QueueLabelUpdateStatus();
  7718. }
  7719. //---------------------------------------------------------------------------
  7720. void __fastcall TCustomScpExplorerForm::QueueLabelUpdateStatus()
  7721. {
  7722. QueueLabel->UpdateStatus();
  7723. }
  7724. //---------------------------------------------------------------------------
  7725. void __fastcall TCustomScpExplorerForm::QueueView3SelectItem(
  7726. TObject * /*Sender*/, TListItem * /*Item*/, bool Selected)
  7727. {
  7728. if (Selected)
  7729. {
  7730. NonVisualDataModule->UpdateNonVisibleActions();
  7731. }
  7732. }
  7733. //---------------------------------------------------------------------------
  7734. TDragDropFilesEx * __fastcall TCustomScpExplorerForm::DragDropFiles(TObject * Sender)
  7735. {
  7736. TDragDropFilesEx * Result = NULL;
  7737. if (Sender == SessionsPageControl)
  7738. {
  7739. Result = FSessionsDragDropFilesEx;
  7740. }
  7741. else if (Sender == QueueView3)
  7742. {
  7743. Result = FQueueDragDropFilesEx;
  7744. }
  7745. else
  7746. {
  7747. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  7748. if (DirView != NULL)
  7749. {
  7750. Result = DirView->DragDropFilesEx;
  7751. }
  7752. else
  7753. {
  7754. TCustomDriveView * DriveView = dynamic_cast<TCustomDriveView *>(Sender);
  7755. if (DriveView != NULL)
  7756. {
  7757. Result = DriveView->DragDropFilesEx;
  7758. }
  7759. }
  7760. }
  7761. DebugAssert(Result != NULL);
  7762. return Result;
  7763. }
  7764. //---------------------------------------------------------------------------
  7765. bool __fastcall TCustomScpExplorerForm::SelectedAllFilesInDirView(TCustomDirView * DView)
  7766. {
  7767. return (DView->SelCount == DView->FilesCount);
  7768. }
  7769. //---------------------------------------------------------------------------
  7770. bool __fastcall TCustomScpExplorerForm::DraggingAllFilesFromDirView(TOperationSide Side, TStrings * FileList)
  7771. {
  7772. return HasDirView[Side] && (DropSourceControl == DirView(Side)) && (FileList->Count == DirView(Side)->FilesCount);
  7773. }
  7774. //---------------------------------------------------------------------------
  7775. void __fastcall TCustomScpExplorerForm::RemoteFileControlDragDropFileOperation(
  7776. TObject * Sender, int Effect, UnicodeString TargetPath, bool ForceQueue, bool DragDrop)
  7777. {
  7778. DebugAssert(!IsLocalBrowserMode());
  7779. TFileOperation Operation;
  7780. switch (Effect)
  7781. {
  7782. case DROPEFFECT_MOVE:
  7783. Operation = foMove;
  7784. break;
  7785. case DROPEFFECT_COPY:
  7786. // occures on WinXP (reported by user)
  7787. default:
  7788. Operation = foCopy;
  7789. break;
  7790. };
  7791. TDragDropFilesEx * DragDropFilesEx = DragDropFiles(Sender);
  7792. // see a comment in TUnixDirView::PerformItemDragDropOperation
  7793. if (DragDropFilesEx->FileList->Count > 0)
  7794. {
  7795. TStrings * FileList = new TStringList();
  7796. try
  7797. {
  7798. for (int Index = 0; Index < DragDropFilesEx->FileList->Count; Index++)
  7799. {
  7800. FileList->Add(DragDropFilesEx->FileList->Items[Index]->Name);
  7801. }
  7802. FDragDropOperation = true;
  7803. TTransferOperationParam Param;
  7804. Param.TargetDirectory = TargetPath;
  7805. // upload, no temp dirs
  7806. Param.Temp = false;
  7807. Param.DragDrop = DragDrop;
  7808. Param.Options =
  7809. FLAGMASK(DraggingAllFilesFromDirView(osLocal, FileList), coAllFiles);
  7810. if (ForceQueue)
  7811. {
  7812. Param.Queue = asOn;
  7813. }
  7814. bool NoConfirmation = DragDrop ? (WinConfiguration->DDTransferConfirmation == asOff) : false;
  7815. if (ExecuteFileOperation(Operation, osLocal, FileList, NoConfirmation, &Param))
  7816. {
  7817. if (IsFileControl(DropSourceControl, osLocal))
  7818. {
  7819. Configuration->Usage->Inc(L"UploadsDragDropInternal");
  7820. }
  7821. else
  7822. {
  7823. Configuration->Usage->Inc(L"UploadsDragDropExternal");
  7824. }
  7825. }
  7826. }
  7827. __finally
  7828. {
  7829. FDragDropOperation = false;
  7830. delete FileList;
  7831. }
  7832. }
  7833. }
  7834. //---------------------------------------------------------------------------
  7835. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDFileOperation(
  7836. TObject * Sender, int Effect, UnicodeString /*SourcePath*/,
  7837. UnicodeString TargetPath, bool Paste, bool & /*DoOperation*/)
  7838. {
  7839. RemoteFileControlDragDropFileOperation(Sender, Effect, TargetPath, false, !Paste);
  7840. }
  7841. //---------------------------------------------------------------------------
  7842. void __fastcall TCustomScpExplorerForm::RemoteFileContolDDChooseEffect(
  7843. TObject * Sender, int grfKeyState, int & dwEffect)
  7844. {
  7845. DebugAssert(!IsLocalBrowserMode());
  7846. // if any drop effect is allowed at all (e.g. no drop to self and drop to parent)
  7847. if ((dwEffect != DROPEFFECT_NONE) &&
  7848. IsFileControl(DropSourceControl, osRemote))
  7849. {
  7850. // do not allow drop on remote panel (on free space, still allow drop on directories)
  7851. if ((Sender == RemoteDirView) && (DropSourceControl == RemoteDirView) &&
  7852. (RemoteDirView->DropTarget == NULL))
  7853. {
  7854. dwEffect = DROPEFFECT_NONE;
  7855. }
  7856. else
  7857. {
  7858. if (dwEffect == DROPEFFECT_COPY)
  7859. {
  7860. bool MoveCapable = Terminal->IsCapable[fcRemoteMove];
  7861. // currently we support copying always (at least via temporary directory);
  7862. // remove associated checks once this all proves stable and working
  7863. bool CopyCapable = true;
  7864. // if we do not support neither of operations, there's no discussion
  7865. if (!MoveCapable && !CopyCapable)
  7866. {
  7867. dwEffect = DROPEFFECT_NONE;
  7868. }
  7869. else
  7870. {
  7871. // The default effect inside remote panel is move,
  7872. // unless we do not support it, but support copy
  7873. if (FLAGCLEAR(grfKeyState, MK_CONTROL))
  7874. {
  7875. dwEffect = MoveCapable ? DROPEFFECT_MOVE : DROPEFFECT_COPY;
  7876. }
  7877. else
  7878. {
  7879. // with ctrl-down, we want copy unless it is not supported
  7880. dwEffect = CopyCapable ? DROPEFFECT_COPY : DROPEFFECT_NONE;
  7881. }
  7882. }
  7883. }
  7884. }
  7885. }
  7886. }
  7887. //---------------------------------------------------------------------------
  7888. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDDragFileName(
  7889. TObject * Sender, TRemoteFile * File, UnicodeString & FileName)
  7890. {
  7891. DebugAssert(!IsLocalBrowserMode());
  7892. if (FDDTotalSize >= 0)
  7893. {
  7894. if (File->IsDirectory)
  7895. {
  7896. FDDTotalSize = -1;
  7897. }
  7898. else
  7899. {
  7900. FDDTotalSize += File->Size;
  7901. }
  7902. }
  7903. DebugAssert(!FDragTempDir.IsEmpty());
  7904. // TODO: this is quite ineffective
  7905. // TODO: what if invalid character replacement is disabled?
  7906. FileName = FDragTempDir + GUIConfiguration->CurrentCopyParam.ValidLocalFileName(File->FileName);
  7907. UnicodeString TransferFileName;
  7908. if (Sender == RemoteDriveView)
  7909. {
  7910. TransferFileName = UnixExcludeTrailingBackslash(File->FullFileName);
  7911. }
  7912. else
  7913. {
  7914. TransferFileName = File->FileName;
  7915. }
  7916. FDDFileList->AddObject(TransferFileName, File);
  7917. }
  7918. //---------------------------------------------------------------------------
  7919. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDDragDetect(
  7920. TObject * /*Sender*/, int /*grfKeyState*/, const TPoint & /*DetectStart*/,
  7921. const TPoint & /*Point*/, TDragDetectStatus /*DragStatus*/)
  7922. {
  7923. // sometimes we do not get DDEnd so the list is not released
  7924. SAFE_DESTROY(FDDFileList);
  7925. FDDFileList = new TStringList();
  7926. FDragTempDir = WinConfiguration->TemporaryDir();
  7927. FDDTotalSize = 0;
  7928. FDragDropSshTerminate.reset(NULL);
  7929. }
  7930. //---------------------------------------------------------------------------
  7931. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDQueryContinueDrag(
  7932. TObject * /*Sender*/, BOOL /*FEscapePressed*/, int /*grfKeyState*/,
  7933. HRESULT & Result)
  7934. {
  7935. if (Result == DRAGDROP_S_DROP)
  7936. {
  7937. try
  7938. {
  7939. GlobalDragImageList->HideDragImage();
  7940. try
  7941. {
  7942. RemoteFileControlDDTargetDrop();
  7943. }
  7944. catch(ESshTerminate & E)
  7945. {
  7946. FDragDropSshTerminate.reset(E.Clone());
  7947. }
  7948. }
  7949. catch (Exception &E)
  7950. {
  7951. // If downloading fails we need to cancel drag&drop, otherwise
  7952. // Explorer shows error
  7953. // But by the way exception probably never reach this point as
  7954. // it's catched on way.
  7955. // Fatal exceptions get here (like when opening a secondary shell extension for file duplication fails).
  7956. Result = DRAGDROP_S_CANCEL;
  7957. ShowExtendedException(Terminal, &E);
  7958. }
  7959. }
  7960. }
  7961. //---------------------------------------------------------------------------
  7962. void __fastcall TCustomScpExplorerForm::DirViewMatchMask(
  7963. TObject * /*Sender*/, UnicodeString FileName, bool Directory, __int64 Size,
  7964. TDateTime Modification, UnicodeString Masks, bool & Matches, bool AllowImplicitMatches)
  7965. {
  7966. TFileMasks::TParams MaskParams;
  7967. MaskParams.Size = Size;
  7968. MaskParams.Modification = Modification;
  7969. // this does not re-parse the mask if it is the same as the last time
  7970. FDirViewMatchMask = Masks;
  7971. bool ImplicitMatch;
  7972. Matches =
  7973. // RecurseInclude parameter has no effect as the Path is empty
  7974. FDirViewMatchMask.Matches(FileName, Directory, UnicodeString(L""), &MaskParams, true, ImplicitMatch) &&
  7975. (AllowImplicitMatches || !ImplicitMatch);
  7976. }
  7977. //---------------------------------------------------------------------------
  7978. void __fastcall TCustomScpExplorerForm::DirViewGetOverlay(
  7979. TObject * Sender, TListItem * Item, WORD & Indexes)
  7980. {
  7981. TCustomDirView * DirView = reinterpret_cast<TCustomDirView *>(Sender);
  7982. UnicodeString Ext;
  7983. if (dynamic_cast<TUnixDirView *>(DirView) != NULL)
  7984. {
  7985. Ext = UnixExtractFileExt(DirView->ItemFileName(Item));
  7986. }
  7987. else
  7988. {
  7989. Ext = ExtractFileExt(DirView->ItemFileName(Item));
  7990. }
  7991. if (SameText(Ext, Configuration->PartialExt))
  7992. {
  7993. Indexes |= oiPartial;
  7994. }
  7995. }
  7996. //---------------------------------------------------------------------------
  7997. bool __fastcall TCustomScpExplorerForm::CanPasteToDirViewFromClipBoard()
  7998. {
  7999. return
  8000. DirViewEnabled(osCurrent) &&
  8001. DirView(osCurrent)->CanPasteFromClipBoard();
  8002. }
  8003. //---------------------------------------------------------------------------
  8004. bool __fastcall TCustomScpExplorerForm::CanPasteFromClipBoard()
  8005. {
  8006. bool Result = false;
  8007. if (CanPasteToDirViewFromClipBoard())
  8008. {
  8009. Result = true;
  8010. }
  8011. else
  8012. {
  8013. UnicodeString ClipboardText;
  8014. if (NonEmptyTextFromClipboard(ClipboardText) &&
  8015. (ClipboardText.Pos(L"\n") == 0)) // it's already trimmed
  8016. {
  8017. if (StoredSessions->IsUrl(ClipboardText))
  8018. {
  8019. Result = true;
  8020. }
  8021. else
  8022. {
  8023. Result = DirViewEnabled(osCurrent);
  8024. }
  8025. }
  8026. }
  8027. return Result;
  8028. }
  8029. //---------------------------------------------------------------------------
  8030. void __fastcall TCustomScpExplorerForm::PasteFromClipBoard()
  8031. {
  8032. // Pasting to any local panel is handled in TScpCommanderForm::PasteFromClipBoard()
  8033. if (DoesClipboardContainOurFiles())
  8034. {
  8035. if (DebugAlwaysTrue(CanPasteToDirViewFromClipBoard()))
  8036. {
  8037. DebugAssert(!IsLocalBrowserMode());
  8038. TTerminalManager * Manager = TTerminalManager::Instance();
  8039. TTerminal * TargetTerminal = Manager->ActiveTerminal;
  8040. Manager->ActiveSession = FClipboardTerminal;
  8041. ExecuteFileOperation(foRemoteCopy, osRemote, FClipboardFileList.get(), false, TargetTerminal);
  8042. }
  8043. }
  8044. else if (CanPasteToDirViewFromClipBoard())
  8045. {
  8046. DirView(osCurrent)->PasteFromClipBoard();
  8047. }
  8048. else
  8049. {
  8050. UnicodeString ClipboardText;
  8051. if (NonEmptyTextFromClipboard(ClipboardText))
  8052. {
  8053. if (StoredSessions->IsUrl(ClipboardText))
  8054. {
  8055. NewSession(ClipboardText);
  8056. }
  8057. else
  8058. {
  8059. DirView(osCurrent)->Path = ClipboardText;
  8060. }
  8061. }
  8062. }
  8063. }
  8064. //---------------------------------------------------------------------------
  8065. void __fastcall TCustomScpExplorerForm::FileListFromClipboard()
  8066. {
  8067. // TBD
  8068. }
  8069. //---------------------------------------------------------------------------
  8070. void __fastcall TCustomScpExplorerForm::SelectAll(TOperationSide Side, TSelectMode Mode)
  8071. {
  8072. TCustomDirView * ADirView = DirView(Side);
  8073. ADirView->SelectAll(Mode);
  8074. ADirView->SetFocus();
  8075. }
  8076. //---------------------------------------------------------------------------
  8077. void __fastcall TCustomScpExplorerForm::SelectByMask(TOperationSide Side, bool Select)
  8078. {
  8079. TCustomDirView * ADirView = DirView(Side);
  8080. TFileFilter Filter;
  8081. DefaultFileFilter(Filter);
  8082. if (DoSelectMaskDialog(ADirView, Select, Filter))
  8083. {
  8084. Configuration->Usage->Inc(L"MaskSelections");
  8085. ADirView->SelectFiles(Filter, Select);
  8086. ADirView->SetFocus();
  8087. }
  8088. }
  8089. //---------------------------------------------------------------------------
  8090. void __fastcall TCustomScpExplorerForm::RestoreSelectedNames(TOperationSide Side)
  8091. {
  8092. TCustomDirView * ADirView = DirView(Side);
  8093. ADirView->RestoreSelectedNames();
  8094. ADirView->SetFocus();
  8095. }
  8096. //---------------------------------------------------------------------------
  8097. void __fastcall TCustomScpExplorerForm::SelectSameExt(bool Select)
  8098. {
  8099. TCustomDirView * CurrentDirView = DirView(osCurrent);
  8100. if (DebugAlwaysTrue(CurrentDirView->ItemFocused != NULL))
  8101. {
  8102. UnicodeString FileName = CurrentDirView->ItemFileName(CurrentDirView->ItemFocused);
  8103. UnicodeString Ext;
  8104. if (!IsSideLocalBrowser(osCurrent))
  8105. {
  8106. Ext = UnixExtractFileExt(FileName);
  8107. }
  8108. else
  8109. {
  8110. Ext = ExtractFileExt(FileName);
  8111. }
  8112. if (Ext.IsEmpty())
  8113. {
  8114. Ext = L".";
  8115. }
  8116. TFileFilter Filter;
  8117. Filter.Masks = FORMAT(L"*%s", (Ext));
  8118. Filter.Directories = false;
  8119. CurrentDirView->SelectFiles(Filter, Select);
  8120. }
  8121. }
  8122. //---------------------------------------------------------------------------
  8123. UnicodeString __fastcall TCustomScpExplorerForm::FileStatusBarText(
  8124. const TStatusFileInfo & FileInfo, TOperationSide Side)
  8125. {
  8126. UnicodeString Result;
  8127. if (!FIncrementalSearch.IsEmpty() && (Side == GetSide(osCurrent)))
  8128. {
  8129. Result = FormatIncrementalSearchStatus(FIncrementalSearch, FIncrementalSearchHaveNext);
  8130. }
  8131. else if (!IsSideLocalBrowser(Side) && ((Terminal == NULL) || Terminal->Disconnected))
  8132. {
  8133. // noop
  8134. }
  8135. else
  8136. {
  8137. Result =
  8138. FMTLOAD(FILE_INFO_FORMAT,
  8139. (FormatBytes(FileInfo.SelectedSize),
  8140. FormatBytes(FileInfo.FilesSize),
  8141. FormatNumber(FileInfo.SelectedCount),
  8142. FormatNumber(FileInfo.FilesCount)));
  8143. }
  8144. return Result;
  8145. }
  8146. //---------------------------------------------------------------------------
  8147. void __fastcall TCustomScpExplorerForm::FileStatusBarPanelClick(
  8148. TTBXStatusPanel * Panel, TOperationSide Side)
  8149. {
  8150. if (Panel->Index == 1)
  8151. {
  8152. ToggleShowHiddenFiles();
  8153. }
  8154. else if (Panel->Index == 2)
  8155. {
  8156. Filter(Side);
  8157. }
  8158. }
  8159. //---------------------------------------------------------------------------
  8160. void __fastcall TCustomScpExplorerForm::UpdateFileStatusBar(
  8161. TTBXStatusBar * StatusBar, const TStatusFileInfo & FileInfo, TOperationSide Side)
  8162. {
  8163. DebugAssert(!StatusBar->SimplePanel);
  8164. StatusBar->Panels->Items[0]->Caption = FileStatusBarText(FileInfo, Side);
  8165. UpdateFileStatusExtendedPanels(StatusBar, FileInfo);
  8166. }
  8167. //---------------------------------------------------------------------------
  8168. void __fastcall TCustomScpExplorerForm::UpdateFileStatusExtendedPanels(
  8169. TTBXStatusBar * StatusBar, const TStatusFileInfo & FileInfo)
  8170. {
  8171. DebugAssert(StatusBar->Panels->Count >= 3);
  8172. TTBXStatusPanel * HiddenFilesPanel = StatusBar->Panels->Items[1];
  8173. if (FileInfo.HiddenCount > 0)
  8174. {
  8175. HiddenFilesPanel->Caption = FMTLOAD(FILE_INFO_HIDDEN2, (FormatNumber(FileInfo.HiddenCount)));
  8176. HiddenFilesPanel->ViewPriority = 90; // <100 allows hiding panel when it does not fit
  8177. }
  8178. else
  8179. {
  8180. HiddenFilesPanel->ViewPriority = 0;
  8181. // not really necessary, just to cleanup no-longer-valid data
  8182. HiddenFilesPanel->Caption = L"";
  8183. }
  8184. TTBXStatusPanel * FilteredFilesPanel = StatusBar->Panels->Items[2];
  8185. if (FileInfo.FilteredCount > 0)
  8186. {
  8187. FilteredFilesPanel->Caption = FMTLOAD(FILE_INFO_FILTERED2, (FormatNumber(FileInfo.FilteredCount)));
  8188. FilteredFilesPanel->ViewPriority = 90; // <100 allows hiding panel when it does not fit
  8189. }
  8190. else
  8191. {
  8192. FilteredFilesPanel->ViewPriority = 0;
  8193. // not really necessary, just to cleanup no-longer-valid data
  8194. FilteredFilesPanel->Caption = L"";
  8195. }
  8196. }
  8197. //---------------------------------------------------------------------------
  8198. void __fastcall TCustomScpExplorerForm::RemoteStatusBarClick(
  8199. TObject * /*Sender*/)
  8200. {
  8201. if (DirView(osOther)->Enabled)
  8202. {
  8203. DirView(osOther)->SetFocus();
  8204. }
  8205. }
  8206. //---------------------------------------------------------------------------
  8207. void __fastcall TCustomScpExplorerForm::ToggleQueueVisibility()
  8208. {
  8209. TQueueViewConfiguration Config = WinConfiguration->QueueView;
  8210. switch (Config.Show)
  8211. {
  8212. case qvShow:
  8213. if ((FQueueStatus != NULL) && (FQueueStatus->Count > 0))
  8214. {
  8215. Config.Show = qvHide;
  8216. }
  8217. else
  8218. {
  8219. Config.Show = Config.LastHideShow;
  8220. }
  8221. break;
  8222. case qvHideWhenEmpty:
  8223. if (ComponentVisible[fcQueueView])
  8224. {
  8225. Config.Show = qvHide;
  8226. }
  8227. else
  8228. {
  8229. Config.LastHideShow = Config.Show;
  8230. Config.Show = qvShow;
  8231. }
  8232. break;
  8233. case qvHide:
  8234. Config.LastHideShow = Config.Show;
  8235. Config.Show = qvShow;
  8236. break;
  8237. }
  8238. WinConfiguration->QueueView = Config;
  8239. }
  8240. //---------------------------------------------------------------------------
  8241. UnicodeString __fastcall TCustomScpExplorerForm::PathForCaption()
  8242. {
  8243. UnicodeString Result;
  8244. if (Terminal != NULL)
  8245. {
  8246. switch (WinConfiguration->PathInCaption)
  8247. {
  8248. case picShort:
  8249. Result = ExtractShortName(Terminal->CurrentDirectory, true);
  8250. break;
  8251. case picFull:
  8252. Result = Terminal->CurrentDirectory;
  8253. break;
  8254. }
  8255. }
  8256. return Result;
  8257. }
  8258. //---------------------------------------------------------------------------
  8259. TColor __fastcall TCustomScpExplorerForm::PanelColor()
  8260. {
  8261. TColor Result = GetWindowColor(FSessionColor);
  8262. return Result;
  8263. }
  8264. //---------------------------------------------------------------------------
  8265. TColor __fastcall TCustomScpExplorerForm::DisabledPanelColor()
  8266. {
  8267. TColor Result = (WinConfiguration->UseDarkTheme() ? static_cast<TColor>(RGB(0x40, 0x40, 0x40)) : clBtnFace);
  8268. return Result;
  8269. }
  8270. //---------------------------------------------------------------------------
  8271. void __fastcall TCustomScpExplorerForm::UpdateControls()
  8272. {
  8273. if (!FSessionChanging)
  8274. {
  8275. TTerminalManager::Instance()->UpdateAppTitle();
  8276. }
  8277. // WORKAROUND: Disabling list view when it is not showing yet does not set its
  8278. // background to gray on Windows 7 (works on Windows 10).
  8279. // See also EnableControl
  8280. if (Showing)
  8281. {
  8282. if (ManagedSession != NULL)
  8283. {
  8284. // Update path when it changes
  8285. if ((SessionsPageControl->ActivePage != NULL) &&
  8286. (GetSessionTabSession(SessionsPageControl->ActivePage) == ManagedSession))
  8287. {
  8288. UpdateSessionTab(SessionsPageControl->ActivePage);
  8289. }
  8290. }
  8291. NonVisualDataModule->ReconnectSessionAction->Update();
  8292. ReconnectToolbar->Visible = NonVisualDataModule->ReconnectSessionAction->Visible;
  8293. // ReconnectSessionAction is hidden when disabled, so enabling it actualy resizes the toolbar
  8294. CenterReconnectToolbar();
  8295. bool HasTerminal = HasActiveTerminal();
  8296. RemoteDirView->Enabled = HasTerminal;
  8297. RemoteDirView->Color = HasTerminal ? PanelColor() : DisabledPanelColor();
  8298. RemoteDirView->Font->Color = GetWindowTextColor(RemoteDirView->Color);
  8299. RemoteDriveView->Enabled = RemoteDirView->Enabled;
  8300. RemoteDriveView->Color = RemoteDirView->Color;
  8301. RemoteDriveView->Font->Color = RemoteDirView->Font->Color;
  8302. QueueView3->Enabled = HasTerminal && Terminal->IsCapable[fcBackgroundTransfers];
  8303. QueueView3->Color = QueueView3->Enabled ? GetWindowColor() : DisabledPanelColor();
  8304. QueueView3->Font->Color = GetWindowTextColor(QueueView3->Color);
  8305. QueueFileList->Enabled = QueueView3->Enabled;
  8306. QueueFileList->Color = QueueView3->Color;
  8307. QueueFileList->Font->Color = QueueView3->Font->Color;
  8308. QueueLabelUpdateStatus();
  8309. RemoteDirView->DarkMode = WinConfiguration->UseDarkTheme();
  8310. RemoteDriveView->DarkMode = RemoteDirView->DarkMode;
  8311. if (FImmersiveDarkMode != WinConfiguration->UseDarkTheme())
  8312. {
  8313. UpdateDarkMode();
  8314. }
  8315. reinterpret_cast<TTBCustomItem *>(GetComponent(fcRemotePathComboBox))->Enabled = HasTerminal || IsLocalBrowserMode();
  8316. }
  8317. }
  8318. //---------------------------------------------------------------------------
  8319. void __fastcall TCustomScpExplorerForm::DoDirViewLoaded(TCustomDirView * /*Sender*/)
  8320. {
  8321. UpdateControls();
  8322. }
  8323. //---------------------------------------------------------------------------
  8324. void __fastcall TCustomScpExplorerForm::DirViewLoaded(
  8325. TObject * Sender)
  8326. {
  8327. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  8328. DebugAssert(DirView != NULL);
  8329. DoDirViewLoaded(DirView);
  8330. TransferPresetAutoSelect();
  8331. }
  8332. //---------------------------------------------------------------------------
  8333. void __fastcall TCustomScpExplorerForm::StartUpdates()
  8334. {
  8335. TUpdatesConfiguration Updates = WinConfiguration->Updates;
  8336. // first run after installation
  8337. if (double(Updates.LastCheck) == 0)
  8338. {
  8339. // make sure next time there will be an update (if enabled)
  8340. Updates.LastCheck = TDateTime(1);
  8341. WinConfiguration->Updates = Updates;
  8342. }
  8343. else if ((double(Updates.Period) > 0) &&
  8344. (Now() - Updates.LastCheck >= Updates.Period))
  8345. {
  8346. TThreadMethod OnUpdatesChecked = NULL;
  8347. if (!IsUWP())
  8348. {
  8349. OnUpdatesChecked = UpdatesChecked;
  8350. }
  8351. StartUpdateThread(OnUpdatesChecked);
  8352. }
  8353. }
  8354. //---------------------------------------------------------------------------
  8355. void __fastcall TCustomScpExplorerForm::UpdatesChecked()
  8356. {
  8357. UnicodeString Message;
  8358. bool New;
  8359. TQueryType Type;
  8360. GetUpdatesMessage(Message, New, Type, false);
  8361. if (!Message.IsEmpty())
  8362. {
  8363. if (New)
  8364. {
  8365. Message = FMTLOAD(NEW_VERSION_CLICK, (Message));
  8366. }
  8367. if (!New && (Type != qtWarning))
  8368. {
  8369. PostNote(UnformatMessage(Message), 0, UpdatesNoteClicked, NULL);
  8370. }
  8371. else
  8372. {
  8373. Notify(NULL, Message, Type, true, UpdatesNoteClicked);
  8374. }
  8375. if (New)
  8376. {
  8377. Configuration->Usage->Inc(L"UpdateNotifications");
  8378. }
  8379. }
  8380. }
  8381. //---------------------------------------------------------------------------
  8382. void __fastcall TCustomScpExplorerForm::UpdatesNoteClicked(TObject * /*Sender*/)
  8383. {
  8384. RestoreApp();
  8385. bool CanDisplay = !NonVisualDataModule->Busy;
  8386. if (!CanDisplay && (Screen->ActiveForm != NULL))
  8387. {
  8388. CanDisplay = (Screen->ActiveForm->Perform(WM_CAN_DISPLAY_UPDATES, 0, 0) != 0);
  8389. }
  8390. if (CanDisplay)
  8391. {
  8392. Configuration->Usage->Inc(L"UpdateNotificationsClicked");
  8393. CheckForUpdates(true);
  8394. }
  8395. else
  8396. {
  8397. Configuration->Usage->Inc(L"UpdateNotificationsBusy");
  8398. }
  8399. }
  8400. //---------------------------------------------------------------------------
  8401. void __fastcall TCustomScpExplorerForm::GetTransferPresetAutoSelectData(
  8402. TCopyParamRuleData & Data)
  8403. {
  8404. DebugAssert(Terminal != NULL);
  8405. Data.HostName = Terminal->SessionData->HostNameExpanded;
  8406. Data.UserName = Terminal->SessionData->UserNameExpanded;
  8407. Data.RemoteDirectory = RemoteDirView->PathName;
  8408. }
  8409. //---------------------------------------------------------------------------
  8410. void __fastcall TCustomScpExplorerForm::TransferPresetAutoSelect()
  8411. {
  8412. // Terminal can be null when we are changing local directory implicitly,
  8413. // because it has been deleted, while no session is connected
  8414. // (Login dialog is open)
  8415. if (FAllowTransferPresetAutoSelect && (Terminal != NULL))
  8416. {
  8417. DebugAssert(!IsLocalBrowserMode());
  8418. TCopyParamRuleData Data;
  8419. GetTransferPresetAutoSelectData(Data);
  8420. int CopyParamIndex = GUIConfiguration->CopyParamList->Find(Data);
  8421. UnicodeString CopyParamCurrent = GUIConfiguration->CopyParamCurrent;
  8422. if (CopyParamIndex < 0)
  8423. {
  8424. // there is no preset that matches autoselection
  8425. // set preset that we consider "default"
  8426. FCopyParamAutoSelected = L""; // forget last autoselected preset
  8427. GUIConfiguration->CopyParamCurrent = FCopyParamDefault;
  8428. }
  8429. else
  8430. {
  8431. // there is preset matching autoselection
  8432. UnicodeString CopyParamName = GUIConfiguration->CopyParamList->Names[CopyParamIndex];
  8433. if (CopyParamName == FCopyParamAutoSelected)
  8434. {
  8435. // autoselected the same preset as the last time
  8436. // make no change (i.e. preserve custom user preset, if any)
  8437. }
  8438. else
  8439. {
  8440. // autoselected the different preset then the last time (or there
  8441. // was default preset set)
  8442. FCopyParamAutoSelected = CopyParamName; // remember autoselection
  8443. GUIConfiguration->CopyParamCurrent = CopyParamName;
  8444. }
  8445. }
  8446. if (GUIConfiguration->CopyParamCurrent != CopyParamCurrent)
  8447. {
  8448. if (CopyParamIndex >= 0)
  8449. {
  8450. Configuration->Usage->Inc(L"CopyParamAutoSelects");
  8451. }
  8452. TTransferPresetNoteData * Data = new TTransferPresetNoteData;
  8453. try
  8454. {
  8455. int Fmt =
  8456. (CopyParamIndex < 0) ?
  8457. (GUIConfiguration->CopyParamIndex < 0 ? COPY_PARAM_DEFAULT_NORM : COPY_PARAM_DEFAULT_CUSTOM) :
  8458. COPY_PARAM_AUTOSELECTED;
  8459. UnicodeString Message = FMTLOAD(Fmt, (StripHotkey(GUIConfiguration->CopyParamCurrent)));
  8460. Data->Message = MainInstructions(Message);
  8461. int CopyParamAttrs = Terminal->UsableCopyParamAttrs(0).General;
  8462. UnicodeString Info = GUIConfiguration->CurrentCopyParam.GetInfoStr(L"\n",
  8463. CopyParamAttrs);
  8464. if (CopyParamIndex >= 0)
  8465. {
  8466. DebugAssert(GUIConfiguration->CopyParamList->Rules[CopyParamIndex] != NULL);
  8467. Info = FORMAT(L"%s\n \n%s", (Info,
  8468. FMTLOAD(COPY_PARAM_RULE,
  8469. (GUIConfiguration->CopyParamList->Rules[CopyParamIndex]->GetInfoStr(L"\n")))));
  8470. }
  8471. Data->Message += L"\n\n" + Info;
  8472. if (WinConfiguration->CopyParamAutoSelectNotice)
  8473. {
  8474. TransferPresetNoteMessage(Data, true);
  8475. }
  8476. else
  8477. {
  8478. PostNote(Message, 0, TransferPresetNoteClicked, Data);
  8479. Data = NULL; // ownership passed
  8480. }
  8481. }
  8482. __finally
  8483. {
  8484. delete Data;
  8485. }
  8486. }
  8487. }
  8488. }
  8489. //---------------------------------------------------------------------------
  8490. void __fastcall TCustomScpExplorerForm::TransferPresetNoteMessage(
  8491. TTransferPresetNoteData * NoteData, bool AllowNeverAskAgain)
  8492. {
  8493. DebugAssert(NoteData != NULL);
  8494. DebugAssert(!IsLocalBrowserMode());
  8495. TMessageParams Params(AllowNeverAskAgain ? mpNeverAskAgainCheck : 0);
  8496. TQueryButtonAlias Aliases[1];
  8497. Aliases[0].Button = qaIgnore; // "ignore" is after "ok"
  8498. Aliases[0].Alias = LoadStr(CONFIGURE_BUTTON);
  8499. Params.Aliases = Aliases;
  8500. Params.AliasesCount = LENOF(Aliases);
  8501. unsigned int Result =
  8502. MoreMessageDialog(NoteData->Message, NULL, qtInformation,
  8503. qaOK | qaIgnore, HELP_COPY_PARAM_AUTOSELECTED, &Params);
  8504. switch (Result)
  8505. {
  8506. case qaNeverAskAgain:
  8507. DebugAssert(AllowNeverAskAgain);
  8508. WinConfiguration->CopyParamAutoSelectNotice = false;
  8509. break;
  8510. case qaIgnore:
  8511. PreferencesDialog(pmPresets);
  8512. break;
  8513. }
  8514. }
  8515. //---------------------------------------------------------------------------
  8516. void __fastcall TCustomScpExplorerForm::TransferPresetNoteClicked(TObject * Sender)
  8517. {
  8518. // as of now useless, as this is used for notes only, never for balloons, ...
  8519. RestoreApp();
  8520. // .. and we should never be busy here
  8521. if (DebugAlwaysTrue(!NonVisualDataModule->Busy))
  8522. {
  8523. TransferPresetNoteMessage(DebugNotNull(dynamic_cast<TTransferPresetNoteData *>(Sender)), false);
  8524. }
  8525. }
  8526. //---------------------------------------------------------------------------
  8527. void __fastcall TCustomScpExplorerForm::PreferencesDialog(
  8528. TPreferencesMode APreferencesMode)
  8529. {
  8530. std::unique_ptr<TPreferencesDialogData> PreferencesData;
  8531. TCopyParamRuleData Data;
  8532. if (Terminal != NULL)
  8533. {
  8534. PreferencesData.reset(new TPreferencesDialogData());
  8535. GetTransferPresetAutoSelectData(Data);
  8536. PreferencesData->CopyParamRuleData = &Data;
  8537. }
  8538. DoPreferencesDialog(APreferencesMode, PreferencesData.get());
  8539. }
  8540. //---------------------------------------------------------------------------
  8541. void __fastcall TCustomScpExplorerForm::AdHocCustomCommandValidate(
  8542. const TCustomCommandType & Command)
  8543. {
  8544. if (CustomCommandState(Command, FEditingFocusedAdHocCommand, ccltAll) <= 0)
  8545. {
  8546. throw Exception(FMTLOAD(CUSTOM_COMMAND_IMPOSSIBLE2, (Command.Command)));
  8547. }
  8548. }
  8549. //---------------------------------------------------------------------------
  8550. void __fastcall TCustomScpExplorerForm::AdHocCustomCommand(bool OnFocused)
  8551. {
  8552. bool RemoteAllowed = CustomCommandRemoteAllowed();
  8553. TCustomCommandType Command;
  8554. // make sure we use local custom command when remote are not supported
  8555. if (RemoteAllowed || FLAGSET(FLastCustomCommand.Params, ccLocal))
  8556. {
  8557. Command = FLastCustomCommand;
  8558. }
  8559. else
  8560. {
  8561. Command.Params = Command.Params | ccLocal;
  8562. }
  8563. Command.Name = LoadStr(CUSTOM_COMMAND_AD_HOC_NAME);
  8564. FEditingFocusedAdHocCommand = OnFocused;
  8565. bool LocalSide = IsSideLocalBrowser(FCurrentSide);
  8566. int Options =
  8567. FLAGMASK((!RemoteAllowed || LocalSide), ccoDisableRemote) |
  8568. FLAGMASK(LocalSide, ccoDisableRemoteFiles);
  8569. if (DoCustomCommandDialog(Command, WinConfiguration->CustomCommandList,
  8570. ccmAdHoc, Options, AdHocCustomCommandValidate, NULL))
  8571. {
  8572. FLastCustomCommand = Command;
  8573. UpdateCustomCommandsToolbar();
  8574. ExecuteFileOperation(foCustomCommand, osRemote, OnFocused, false, &Command);
  8575. }
  8576. }
  8577. //---------------------------------------------------------------------------
  8578. void __fastcall TCustomScpExplorerForm::LastCustomCommand(bool OnFocused)
  8579. {
  8580. DebugAssert(!FLastCustomCommand.Command.IsEmpty());
  8581. int State = CustomCommandState(FLastCustomCommand, OnFocused, ccltAll);
  8582. DebugAssert(State > 0);
  8583. if (State <= 0)
  8584. {
  8585. throw Exception(FMTLOAD(CUSTOM_COMMAND_IMPOSSIBLE2, (FLastCustomCommand.Command)));
  8586. }
  8587. ExecuteFileOperation(foCustomCommand, osRemote, OnFocused, false, &FLastCustomCommand);
  8588. }
  8589. //---------------------------------------------------------------------------
  8590. bool __fastcall TCustomScpExplorerForm::GetLastCustomCommand(bool OnFocused,
  8591. TCustomCommandType & Command, int & State)
  8592. {
  8593. bool Result = !FLastCustomCommand.Command.IsEmpty();
  8594. if (Result)
  8595. {
  8596. Command = FLastCustomCommand;
  8597. State = CustomCommandState(FLastCustomCommand, OnFocused, ccltAll);
  8598. }
  8599. return Result;
  8600. }
  8601. //---------------------------------------------------------------------------
  8602. void __fastcall TCustomScpExplorerForm::BeforeAction()
  8603. {
  8604. ResetIncrementalSearch();
  8605. }
  8606. //---------------------------------------------------------------------------
  8607. void __fastcall TCustomScpExplorerForm::PostComponentHide(Byte Component)
  8608. {
  8609. DebugAssert(ComponentVisible[Component]);
  8610. PostMessage(Handle, WM_COMPONENT_HIDE, Component, 0);
  8611. }
  8612. //---------------------------------------------------------------------------
  8613. void __fastcall TCustomScpExplorerForm::QueueSplitterDblClick(TObject * /*Sender*/)
  8614. {
  8615. // when queue panel is resized here directly, the status bar is stretched
  8616. // over whole space the panel occupied
  8617. PostComponentHide(fcQueueView);
  8618. }
  8619. //---------------------------------------------------------------------------
  8620. void __fastcall TCustomScpExplorerForm::QueueFileListSplitterDblClick(TObject *)
  8621. {
  8622. ComponentVisible[fcQueueFileList] = false;
  8623. }
  8624. //---------------------------------------------------------------------------
  8625. void __fastcall TCustomScpExplorerForm::ThemeChanged()
  8626. {
  8627. // We hoped this will refresh scrollbar colors, but it does not have any effect here.
  8628. RefreshColorMode();
  8629. ResetSysDarkTheme();
  8630. ConfigurationChanged();
  8631. ConfigureInterface();
  8632. // Should be called for all controls
  8633. RemoteDirView->Perform(WM_THEMECHANGED, 0, 0);
  8634. }
  8635. //---------------------------------------------------------------------------
  8636. void __fastcall TCustomScpExplorerForm::WMSettingChange(TMessage & Message)
  8637. {
  8638. // Do not handle, when shutting down anyway (maybe also when not setup completelly yet?)
  8639. if (!FInvalid &&
  8640. (Message.LParam != 0) &&
  8641. (wcscmp(reinterpret_cast<LPCWCH>(Message.LParam), L"ImmersiveColorSet") == 0))
  8642. {
  8643. ThemeChanged();
  8644. }
  8645. TForm::Dispatch(&Message);
  8646. }
  8647. //---------------------------------------------------------------------------
  8648. void __fastcall TCustomScpExplorerForm::Dispatch(void * Message)
  8649. {
  8650. TMessage * M = static_cast<TMessage*>(Message);
  8651. switch (M->Msg)
  8652. {
  8653. case CM_DIALOGCHAR:
  8654. CMDialogChar(*M);
  8655. break;
  8656. case WM_APPCOMMAND:
  8657. WMAppCommand(*M);
  8658. break;
  8659. case WM_SYSCOMMAND:
  8660. WMSysCommand(*M);
  8661. break;
  8662. case WM_QUERYENDSESSION:
  8663. WMQueryEndSession(*M);
  8664. break;
  8665. case WM_ENDSESSION:
  8666. WMEndSession(*reinterpret_cast<TWMEndSession *>(M));
  8667. break;
  8668. #ifdef _DEBUG
  8669. case WM_WINDOWPOSCHANGED:
  8670. WMWindowPosChanged(*reinterpret_cast<TWMWindowPosMsg *>(M));
  8671. break;
  8672. case CM_CUSTOMSTYLECHANGED:
  8673. TForm::Dispatch(Message);
  8674. break;
  8675. #endif
  8676. case WM_COMPONENT_HIDE:
  8677. {
  8678. Byte Component = static_cast<Byte>(M->WParam);
  8679. // sanity check
  8680. if (ComponentVisible[Component])
  8681. {
  8682. // special treatment
  8683. if (Component == fcQueueView)
  8684. {
  8685. ToggleQueueVisibility();
  8686. DebugAssert(!ComponentVisible[fcQueueView]);
  8687. }
  8688. else
  8689. {
  8690. ComponentVisible[Component] = false;
  8691. }
  8692. }
  8693. }
  8694. break;
  8695. case WM_COPYDATA:
  8696. WMCopyData(*M);
  8697. break;
  8698. case WM_MANAGES_CAPTION:
  8699. // caption managed in TTerminalManager::UpdateAppTitle()
  8700. M->Result = 1;
  8701. break;
  8702. case WM_WANTS_MOUSEWHEEL:
  8703. M->Result = 1;
  8704. break;
  8705. case CM_SHOWINGCHANGED:
  8706. CMShowingChanged(*M);
  8707. break;
  8708. case WM_CLOSE:
  8709. WMClose(*M);
  8710. break;
  8711. case WM_DPICHANGED:
  8712. WMDpiChanged(*M);
  8713. break;
  8714. case CM_DPICHANGED:
  8715. CMDpiChanged(*M);
  8716. break;
  8717. case WM_SETTINGCHANGE:
  8718. WMSettingChange(*M);
  8719. break;
  8720. case CM_DIALOGKEY:
  8721. CMDialogKey(*reinterpret_cast<TWMKeyDown *>(M));
  8722. break;
  8723. default:
  8724. TForm::Dispatch(Message);
  8725. break;
  8726. }
  8727. }
  8728. //---------------------------------------------------------------------------
  8729. void __fastcall TCustomScpExplorerForm::UpdateImages()
  8730. {
  8731. // noop
  8732. }
  8733. //---------------------------------------------------------------------------
  8734. void __fastcall TCustomScpExplorerForm::CMDpiChanged(TMessage & Message)
  8735. {
  8736. TForm::Dispatch(&Message);
  8737. FSessionColors->Clear();
  8738. AddFixedSessionImages();
  8739. RegenerateSessionColorsImageList(FSessionColors, FSessionColorMaskImageIndex);
  8740. UpdateImages();
  8741. }
  8742. //---------------------------------------------------------------------------
  8743. void __fastcall TCustomScpExplorerForm::WMDpiChanged(TMessage & Message)
  8744. {
  8745. TForm::Dispatch(&Message);
  8746. Configuration->Usage->Inc(L"PixelsPerInchChanged");
  8747. UpdatePixelsPerInchMainWindowCounter();
  8748. }
  8749. //---------------------------------------------------------------------------
  8750. void __fastcall TCustomScpExplorerForm::WMClose(TMessage & Message)
  8751. {
  8752. // Cannot close window while we are busy.
  8753. // We cannot test this in FormCloseQuery as that is called also from
  8754. // Close(), which is called by CloseApplicationAction. So we can be busy
  8755. // there already even, when it is legitimate to close the application.
  8756. // Possibly a better place to handle this would be WMSysCommand.
  8757. if (NonVisualDataModule->Busy)
  8758. {
  8759. Message.Result = 1;
  8760. }
  8761. else
  8762. {
  8763. TForm::Dispatch(&Message);
  8764. }
  8765. }
  8766. //---------------------------------------------------------------------------
  8767. void __fastcall TCustomScpExplorerForm::CMShowingChanged(TMessage & Message)
  8768. {
  8769. // Ignoring implicit local-local browser of Commander, opened from DoShow
  8770. bool NoSession = (ManagedSession == NULL);
  8771. // This calls TCustomScpExplorerForm::DoShow
  8772. TForm::Dispatch(&Message);
  8773. // Now the window is visible (TForm::Dispatch is what usually takes long, like when loading a "local" network directory)
  8774. if (Showing && !FStarted)
  8775. {
  8776. FStarted = true;
  8777. InterfaceStarted();
  8778. }
  8779. if (Showing && NoSession)
  8780. {
  8781. // When we are starting minimized (i.e. from an installer),
  8782. // postpone showing Login dialog until we get restored.
  8783. // Otherwise the Login dialog (and Authentication window) show restored
  8784. // over invisible (minimized) main window.
  8785. if (WindowState == wsMinimized)
  8786. {
  8787. FNeedSession = true;
  8788. }
  8789. else
  8790. {
  8791. // This happens before application ever goes idle, so the toolbars would
  8792. // stay enabled (initial state) until the Login dialog is dismissed.
  8793. UpdateActions();
  8794. NonVisualDataModule->StartBusy();
  8795. try
  8796. {
  8797. // Need to process WM_ACTIVATEAPP before showing the Login dialog,
  8798. // otherwise the dialog does not receive focus.
  8799. // With Commander interface the ProcessMessages is called already
  8800. // by TDriveView, but with Explorer interface, we need to call it explicily
  8801. Application->ProcessMessages();
  8802. // do not reload sessions, they have been loaded just now (optimization)
  8803. NeedSession(true);
  8804. }
  8805. __finally
  8806. {
  8807. NonVisualDataModule->EndBusy();
  8808. }
  8809. }
  8810. }
  8811. }
  8812. //---------------------------------------------------------------------------
  8813. void __fastcall TCustomScpExplorerForm::CenterReconnectToolbar()
  8814. {
  8815. ReconnectToolbar->Left = (ReconnectToolbar->Parent->ClientWidth - ReconnectToolbar->Width) / 2;
  8816. ReconnectToolbar->Top = (ReconnectToolbar->Parent->ClientHeight - ReconnectToolbar->Height) / 2;
  8817. }
  8818. //---------------------------------------------------------------------------
  8819. void __fastcall TCustomScpExplorerForm::FormConstrainedResize(
  8820. TObject * /*Sender*/, int & MinWidth, int & MinHeight, int & MaxWidth,
  8821. int & MaxHeight)
  8822. {
  8823. // workaround for bug in TWinControl.CalcConstraints
  8824. // Check for empty rect (restore from iconic state) is done there only after
  8825. // call to AdjustClientRect, which enlarges the rect (for forms).
  8826. TRect R = GetClientRect();
  8827. // when restoring from iconic state, place no restrictions
  8828. if (IsRectEmpty(R))
  8829. {
  8830. MinWidth = 0;
  8831. MinHeight = 0;
  8832. MaxWidth = 32000;
  8833. MaxHeight = 32000;
  8834. }
  8835. }
  8836. //---------------------------------------------------------------------------
  8837. void __fastcall TCustomScpExplorerForm::GetSpaceAvailable(const UnicodeString Path,
  8838. TSpaceAvailable & ASpaceAvailable, bool & Close)
  8839. {
  8840. // terminal can be already closed (e.g. dropped connection)
  8841. if ((Terminal != NULL) && Terminal->IsCapable[fcCheckingSpaceAvailable])
  8842. {
  8843. Configuration->Usage->Inc(L"SpaceAvailableChecks");
  8844. try
  8845. {
  8846. Terminal->SpaceAvailable(Path, ASpaceAvailable);
  8847. }
  8848. catch(...)
  8849. {
  8850. if (!Terminal->Active)
  8851. {
  8852. Close = true;
  8853. }
  8854. throw;
  8855. }
  8856. }
  8857. }
  8858. //---------------------------------------------------------------------------
  8859. void __fastcall TCustomScpExplorerForm::FileSystemInfo()
  8860. {
  8861. const TSessionInfo & SessionInfo = Terminal->GetSessionInfo();
  8862. const TFileSystemInfo & FileSystemInfo = Terminal->GetFileSystemInfo(true);
  8863. TGetSpaceAvailable OnGetSpaceAvailable = NULL;
  8864. if (Terminal->IsCapable[fcCheckingSpaceAvailable])
  8865. {
  8866. OnGetSpaceAvailable = GetSpaceAvailable;
  8867. }
  8868. DoFileSystemInfoDialog(SessionInfo, FileSystemInfo, Terminal->CurrentDirectory,
  8869. OnGetSpaceAvailable);
  8870. }
  8871. //---------------------------------------------------------------------------
  8872. TSessionData * __fastcall TCustomScpExplorerForm::SessionDataForCode()
  8873. {
  8874. std::unique_ptr<TSessionData> Data(CloneCurrentSessionData());
  8875. Terminal->FillSessionDataForCode(Data.get());
  8876. return Data.release();
  8877. }
  8878. //---------------------------------------------------------------------------
  8879. void __fastcall TCustomScpExplorerForm::GenerateUrl(TStrings * Paths)
  8880. {
  8881. std::unique_ptr<TSessionData> Data(SessionDataForCode());
  8882. DoGenerateUrlDialog(Data.get(), Paths);
  8883. }
  8884. //---------------------------------------------------------------------------
  8885. void __fastcall TCustomScpExplorerForm::SessionGenerateUrl()
  8886. {
  8887. GenerateUrl(NULL);
  8888. }
  8889. //---------------------------------------------------------------------------
  8890. void __fastcall TCustomScpExplorerForm::FileGenerateUrl()
  8891. {
  8892. std::unique_ptr<TStrings> Paths(new TStringList());
  8893. DirView(osCurrent)->CreateFileList(false, true, Paths.get());
  8894. GenerateUrl(Paths.get());
  8895. }
  8896. //---------------------------------------------------------------------------
  8897. void __fastcall TCustomScpExplorerForm::UpdateSessionColor(TColor value)
  8898. {
  8899. FSessionColor = value;
  8900. TColor C = (value != 0 ? value : Vcl::Graphics::clNone);
  8901. TTBXColorItem * ColorItem = dynamic_cast<TTBXColorItem *>(
  8902. static_cast<TObject *>(GetComponent(fcColorMenu)));
  8903. DebugAssert(ColorItem != NULL);
  8904. ColorItem->Color = C;
  8905. NonVisualDataModule->ColorMenuItem->Color = C;
  8906. UpdateControls();
  8907. }
  8908. //---------------------------------------------------------------------------
  8909. void __fastcall TCustomScpExplorerForm::SetSessionColor(TColor value)
  8910. {
  8911. if (value != FSessionColor)
  8912. {
  8913. UpdateSessionColor(value);
  8914. // Is null when called from LastTerminalClosed
  8915. if (Terminal != NULL)
  8916. {
  8917. SessionsPageControl->ActivePage->ImageIndex = AddSessionColor(value);
  8918. }
  8919. }
  8920. }
  8921. //---------------------------------------------------------------------------
  8922. bool __fastcall TCustomScpExplorerForm::CancelNote(bool Force)
  8923. {
  8924. bool Result = FNoteTimer->Enabled;
  8925. if (Result)
  8926. {
  8927. // cannot cancel note too early
  8928. bool NotEarly =
  8929. (Now() - FNoteShown >
  8930. EncodeTimeVerbose(0, 0, (unsigned short)(WinConfiguration->NotificationsStickTime), 0));
  8931. if (Force || NotEarly)
  8932. {
  8933. FNoteTimer->Enabled = false;
  8934. FNote = L"";
  8935. SAFE_DESTROY(FNoteData);
  8936. FOnNoteClick = NULL;
  8937. FNoteHints = FNotes->Text;
  8938. FNoteHints.Delete(FNoteHints.Length() - 1, 2);
  8939. UpdateStatusBar();
  8940. }
  8941. }
  8942. return Result;
  8943. }
  8944. //---------------------------------------------------------------------------
  8945. void __fastcall TCustomScpExplorerForm::NoteTimer(TObject * /*Sender*/)
  8946. {
  8947. DebugAssert(FNoteTimer->Enabled);
  8948. CancelNote(true);
  8949. }
  8950. //---------------------------------------------------------------------------
  8951. void __fastcall TCustomScpExplorerForm::UpdateNoteHints()
  8952. {
  8953. FNoteHints = FNotes->Text.TrimRight();
  8954. }
  8955. //---------------------------------------------------------------------------
  8956. void __fastcall TCustomScpExplorerForm::AddNote(UnicodeString Note, bool UpdateNow)
  8957. {
  8958. int P = Note.Pos(L"\n");
  8959. if (P > 0)
  8960. {
  8961. Note.SetLength(P - 1);
  8962. }
  8963. FNotes->Add(FORMAT(L"[%s] %s",
  8964. (FormatDateTime(Configuration->TimeFormat, Now()), Note)));
  8965. while (FNotes->Count > 10)
  8966. {
  8967. FNotes->Delete(0);
  8968. }
  8969. if (UpdateNow)
  8970. {
  8971. UpdateNoteHints();
  8972. UpdateStatusBar();
  8973. }
  8974. }
  8975. //---------------------------------------------------------------------------
  8976. void __fastcall TCustomScpExplorerForm::PostNote(UnicodeString Note,
  8977. unsigned int Seconds, TNotifyEvent OnNoteClick, TObject * NoteData)
  8978. {
  8979. int P = Note.Pos(L"\n");
  8980. if (P > 0)
  8981. {
  8982. Note.SetLength(P - 1);
  8983. }
  8984. UpdateNoteHints();
  8985. FNote = Note;
  8986. SAFE_DESTROY(FNoteData);
  8987. FNoteData = NoteData;
  8988. FOnNoteClick = OnNoteClick;
  8989. AddNote(Note, false);
  8990. UpdateStatusBar();
  8991. FNoteShown = Now();
  8992. FNoteTimer->Enabled = false;
  8993. if (Seconds == 0)
  8994. {
  8995. Seconds = WinConfiguration->NotificationsTimeout;
  8996. }
  8997. FNoteTimer->Interval = Seconds * MSecsPerSec;
  8998. FNoteTimer->Enabled = true;
  8999. }
  9000. //---------------------------------------------------------------------------
  9001. void __fastcall TCustomScpExplorerForm::ReadDirectoryCancelled()
  9002. {
  9003. PostNote(LoadStr(DIRECTORY_READING_CANCELLED), 0, NULL, NULL);
  9004. }
  9005. //---------------------------------------------------------------------------
  9006. void __fastcall TCustomScpExplorerForm::SynchronizeBrowsingChanged()
  9007. {
  9008. if (NonVisualDataModule->SynchronizeBrowsingAction2->Checked)
  9009. {
  9010. Configuration->Usage->Inc(L"SynchronizeBrowsingEnabled");
  9011. }
  9012. PostNote(FORMAT(LoadStrPart(SYNC_DIR_BROWSE_TOGGLE, 1),
  9013. (LoadStrPart(SYNC_DIR_BROWSE_TOGGLE,
  9014. (NonVisualDataModule->SynchronizeBrowsingAction2->Checked ? 2 : 3)))),
  9015. 0, NULL, NULL);
  9016. }
  9017. //---------------------------------------------------------------------------
  9018. void __fastcall TCustomScpExplorerForm::ToggleShowHiddenFiles()
  9019. {
  9020. PostNote(FORMAT(LoadStrPart(SHOW_HIDDEN_FILES_TOGGLE, 1),
  9021. (LoadStrPart(SHOW_HIDDEN_FILES_TOGGLE,
  9022. (WinConfiguration->ShowHiddenFiles ? 2 : 3)))), 0, NULL, NULL);
  9023. GetComponent(fcStatusBar)->Repaint(); // toggling ShowHiddenFiles takes time, force repaint beforehand
  9024. WinConfiguration->ShowHiddenFiles = !WinConfiguration->ShowHiddenFiles;
  9025. }
  9026. //---------------------------------------------------------------------------
  9027. void __fastcall TCustomScpExplorerForm::SetFormatSizeBytes(TFormatBytesStyle Style)
  9028. {
  9029. WinConfiguration->FormatSizeBytes = Style;
  9030. }
  9031. //---------------------------------------------------------------------------
  9032. void __fastcall TCustomScpExplorerForm::ToggleAutoReadDirectoryAfterOp()
  9033. {
  9034. Configuration->AutoReadDirectoryAfterOp = !Configuration->AutoReadDirectoryAfterOp;
  9035. PostNote(FORMAT(LoadStrPart(AUTO_READ_DIRECTORY_TOGGLE, 1),
  9036. (LoadStrPart(AUTO_READ_DIRECTORY_TOGGLE,
  9037. (Configuration->AutoReadDirectoryAfterOp ? 2 : 3)))), 0, NULL, NULL);
  9038. }
  9039. //---------------------------------------------------------------------------
  9040. void __fastcall TCustomScpExplorerForm::StatusBarPanelDblClick(
  9041. TTBXCustomStatusBar * Sender, TTBXStatusPanel * Panel)
  9042. {
  9043. if (Panel->Index == 0)
  9044. {
  9045. if (FOnNoteClick != NULL)
  9046. {
  9047. // prevent the user data from being freed by possible call
  9048. // to CancelNote or PostNote during call to OnNoteClick
  9049. std::unique_ptr<TObject> NoteData(FNoteData);
  9050. TNotifyEvent OnNoteClick = FOnNoteClick;
  9051. FNoteData = NULL;
  9052. // need to cancel the note as we are going to delete its user data
  9053. CancelNote(true);
  9054. OnNoteClick(NoteData.get());
  9055. }
  9056. }
  9057. if (HasActiveTerminal() &&
  9058. (Panel->Index >= Sender->Panels->Count - SessionPanelCount))
  9059. {
  9060. FileSystemInfo();
  9061. }
  9062. }
  9063. //---------------------------------------------------------------------------
  9064. void __fastcall TCustomScpExplorerForm::LockWindow(bool Force)
  9065. {
  9066. // workaround:
  9067. // 1) for unknown reason, disabling window, while minimized,
  9068. // prevents it from restoring, even if it was enabled again meanwhile
  9069. // 2) when disabling the main window, while another has focus
  9070. // minimize is no longer possible
  9071. // ("keep up to date" dialog - though does not seem to be the case with "find file" -
  9072. // so it has probably something to do with the semi-modalness of the "keep up to date" dialog)
  9073. // Shouldn't we use IsApplicationMinimized() here?
  9074. if ((FLockSuspendLevel == 0) && !IsIconic(Application->Handle) && (Force || (Screen->ActiveForm == this)))
  9075. {
  9076. Enabled = false;
  9077. }
  9078. FLockLevel++;
  9079. }
  9080. //---------------------------------------------------------------------------
  9081. void __fastcall TCustomScpExplorerForm::UnlockWindow()
  9082. {
  9083. DebugAssert(FLockLevel > 0);
  9084. FLockLevel--;
  9085. if (FLockLevel == 0)
  9086. {
  9087. DebugAssert(FLockSuspendLevel == 0);
  9088. Enabled = true;
  9089. }
  9090. }
  9091. //---------------------------------------------------------------------------
  9092. void __fastcall TCustomScpExplorerForm::SuspendWindowLock()
  9093. {
  9094. // We need to make sure that window is enabled when the last modal window closes
  9095. // otherwise focus is not restored correctly.
  9096. // So we re-enable the window when modal window opens and
  9097. // disable it back after is closes
  9098. if (FLockLevel > 0)
  9099. {
  9100. // while we have nesting counter, we know that we never be called
  9101. // recursivelly as Application->OnModalBegin is called only
  9102. // for the top-level modal window
  9103. if (DebugAlwaysTrue(FLockSuspendLevel == 0))
  9104. {
  9105. // won't be disabled when conditions in LockWindow() were not satisfied
  9106. FDisabledOnLockSuspend = !Enabled;
  9107. // When minimized to tray (or actually when set to SW_HIDE),
  9108. // setting Enabled makes the window focusable even when there's
  9109. // modal window over it
  9110. if (!FTrayIcon->Visible)
  9111. {
  9112. Enabled = true;
  9113. }
  9114. }
  9115. FLockSuspendLevel++;
  9116. }
  9117. }
  9118. //---------------------------------------------------------------------------
  9119. void __fastcall TCustomScpExplorerForm::ResumeWindowLock()
  9120. {
  9121. if (FLockSuspendLevel > 0)
  9122. {
  9123. DebugAssert(FLockLevel > 0);
  9124. FLockSuspendLevel--;
  9125. // see comment in SuspendWindowLock
  9126. if (DebugAlwaysTrue(FLockSuspendLevel == 0))
  9127. {
  9128. // Note that window can be enabled here, when we were minized to tray when
  9129. // was SuspendWindowLock() called.
  9130. // We should possibly do the same check as in LockWindow(),
  9131. // if it is ever possible that the consitions change between
  9132. // SuspendWindowLock() and ResumeWindowLock()
  9133. Enabled = !FDisabledOnLockSuspend;
  9134. }
  9135. }
  9136. }
  9137. //---------------------------------------------------------------------------
  9138. void __fastcall TCustomScpExplorerForm::UpdateRemotePathComboBox(bool TextOnly)
  9139. {
  9140. TTBXComboBoxItem * RemotePathComboBox =
  9141. reinterpret_cast<TTBXComboBoxItem *>(GetComponent(fcRemotePathComboBox));
  9142. if (!TextOnly)
  9143. {
  9144. TStrings * Items = RemotePathComboBox->Strings;
  9145. Items->BeginUpdate();
  9146. try
  9147. {
  9148. Items->Clear();
  9149. if ((Terminal != NULL) && !RemoteDirView->Path.IsEmpty())
  9150. {
  9151. UnicodeString APath = UnixExcludeTrailingBackslash(RemoteDirView->Path);
  9152. while (!IsUnixRootPath(APath))
  9153. {
  9154. int P = APath.LastDelimiter(L'/');
  9155. DebugAssert(P >= 0);
  9156. Items->Insert(0, APath.SubString(P + 1, APath.Length() - P));
  9157. APath.SetLength(P - 1);
  9158. }
  9159. Items->Insert(0, Customunixdirview_SUnixDefaultRootName);
  9160. }
  9161. }
  9162. __finally
  9163. {
  9164. Items->EndUpdate();
  9165. }
  9166. }
  9167. RemotePathComboBox->ItemIndex = RemotePathComboBox->Strings->Count - 1;
  9168. // Setting ItemIndex to -1 does not reset its text
  9169. if (RemotePathComboBox->Strings->Count == 0)
  9170. {
  9171. RemotePathComboBox->Text = L"";
  9172. }
  9173. }
  9174. //---------------------------------------------------------------------------
  9175. void __fastcall TCustomScpExplorerForm::DoRemotePathComboBoxAdjustImageIndex(
  9176. TTBXComboBoxItem * Sender, const UnicodeString DebugUsedArg(AText), int AIndex, int & ImageIndex)
  9177. {
  9178. if (AIndex < 0)
  9179. {
  9180. AIndex = Sender->ItemIndex;
  9181. }
  9182. ImageIndex = (AIndex < Sender->Strings->Count - 1 ? StdDirIcon : StdDirSelIcon);
  9183. }
  9184. //---------------------------------------------------------------------------
  9185. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxAdjustImageIndex(
  9186. TTBXComboBoxItem * Sender, const UnicodeString AText, int AIndex,
  9187. int & ImageIndex)
  9188. {
  9189. DoRemotePathComboBoxAdjustImageIndex(Sender, AText, AIndex, ImageIndex);
  9190. }
  9191. //---------------------------------------------------------------------------
  9192. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxDrawItem(
  9193. TTBXCustomList * /*Sender*/, TCanvas * /*ACanvas*/, TRect & ARect, int AIndex,
  9194. int /*AHoverIndex*/, bool & /*DrawDefault*/)
  9195. {
  9196. if (!IsLocalBrowserMode())
  9197. {
  9198. ARect.Left += (10 * AIndex);
  9199. }
  9200. }
  9201. //---------------------------------------------------------------------------
  9202. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxMeasureWidth(
  9203. TTBXCustomList * /*Sender*/, TCanvas * /*ACanvas*/, int AIndex, int &AWidth)
  9204. {
  9205. AWidth += (10 * AIndex);
  9206. }
  9207. //---------------------------------------------------------------------------
  9208. void __fastcall TCustomScpExplorerForm::DoRemotePathComboBoxItemClick(TObject * Sender)
  9209. {
  9210. TTBXComboBoxItem * RemotePathComboBox = dynamic_cast<TTBXComboBoxItem*>(Sender);
  9211. UnicodeString APath = UnixExcludeTrailingBackslash(RemoteDirView->Path);
  9212. int Index = RemotePathComboBox->ItemIndex;
  9213. while (Index < RemotePathComboBox->Strings->Count - 1)
  9214. {
  9215. APath = UnixExtractFileDir(APath);
  9216. Index++;
  9217. }
  9218. // VanDyke style paths
  9219. if (APath.IsEmpty())
  9220. {
  9221. DebugAssert(RemotePathComboBox->ItemIndex == 0);
  9222. APath = ROOTDIRECTORY;
  9223. }
  9224. if (RemoteDirView->Path != APath)
  9225. {
  9226. RemoteDirView->Path = APath;
  9227. }
  9228. }
  9229. //---------------------------------------------------------------------------
  9230. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxItemClick(TObject * Sender)
  9231. {
  9232. DoRemotePathComboBoxItemClick(Sender);
  9233. }
  9234. //---------------------------------------------------------------------------
  9235. void __fastcall TCustomScpExplorerForm::DoRemotePathComboBoxCancel(TObject * Sender)
  9236. {
  9237. DebugAssert(Sender == GetComponent(fcRemotePathComboBox));
  9238. DebugUsedParam(Sender);
  9239. UpdateRemotePathComboBox(true);
  9240. }
  9241. //---------------------------------------------------------------------------
  9242. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxCancel(TObject * Sender)
  9243. {
  9244. DoRemotePathComboBoxCancel(Sender);
  9245. }
  9246. //---------------------------------------------------------------------------
  9247. void __fastcall TCustomScpExplorerForm::DirViewEditing(
  9248. TObject * Sender, TListItem * Item, bool & /*AllowEdit*/)
  9249. {
  9250. ResetIncrementalSearch();
  9251. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  9252. DebugAssert(DirView != NULL);
  9253. if (!WinConfiguration->RenameWholeName && !DirView->ItemIsDirectory(Item))
  9254. {
  9255. HWND Edit = ListView_GetEditControl(DirView->Handle);
  9256. // OnEditing is called also from TCustomListView::CanEdit
  9257. if (Edit != NULL)
  9258. {
  9259. EditSelectBaseName(Edit);
  9260. }
  9261. }
  9262. }
  9263. //---------------------------------------------------------------------------
  9264. //---------------------------------------------------------------------------
  9265. TDragDropFilesEx * __fastcall TCustomScpExplorerForm::CreateDragDropFilesEx()
  9266. {
  9267. TDragDropFilesEx * Result = new TDragDropFilesEx(this);
  9268. Result->AutoDetectDnD = false;
  9269. Result->NeedValid = TFileExMustDnDSet() << nvFilename;
  9270. Result->RenderDataOn = rdoEnterAndDropSync;
  9271. Result->TargetEffects = TDropEffectSet() << deCopy << deMove;
  9272. return Result;
  9273. }
  9274. //---------------------------------------------------------------------------
  9275. void __fastcall TCustomScpExplorerForm::UpdateDarkMode()
  9276. {
  9277. if (IsWin10Build(2004))
  9278. {
  9279. FImmersiveDarkMode = WinConfiguration->UseDarkTheme();
  9280. BOOL DarkMode = FImmersiveDarkMode ? TRUE : FALSE;
  9281. const int DWMWA_USE_IMMERSIVE_DARK_MODE = 20;
  9282. DwmSetWindowAttribute(Handle, DWMWA_USE_IMMERSIVE_DARK_MODE, &DarkMode, sizeof(DarkMode));
  9283. }
  9284. }
  9285. //---------------------------------------------------------------------------
  9286. void __fastcall TCustomScpExplorerForm::CreateWnd()
  9287. {
  9288. TForm::CreateWnd();
  9289. // win32-darkmode calls AllowDarkModeForWindow(this, true) here, but it does not seem to have any effect
  9290. UpdateDarkMode();
  9291. if (FSessionsDragDropFilesEx == NULL)
  9292. {
  9293. FSessionsDragDropFilesEx = CreateDragDropFilesEx();
  9294. FSessionsDragDropFilesEx->OnDragOver = SessionsDDDragOver;
  9295. FSessionsDragDropFilesEx->OnProcessDropped = SessionsDDProcessDropped;
  9296. FSessionsDragDropFilesEx->OnDragEnter = SessionsDDDragEnter;
  9297. FSessionsDragDropFilesEx->OnDragLeave = SessionsDDDragLeave;
  9298. }
  9299. if (FQueueDragDropFilesEx == NULL)
  9300. {
  9301. FQueueDragDropFilesEx = CreateDragDropFilesEx();
  9302. // No need to set OnDragOver as we do not have any restrictions
  9303. FQueueDragDropFilesEx->OnProcessDropped = QueueDDProcessDropped;
  9304. FQueueDragDropFilesEx->OnDragEnter = QueueDDDragEnter;
  9305. FQueueDragDropFilesEx->OnDragLeave = QueueDDDragLeave;
  9306. }
  9307. }
  9308. //---------------------------------------------------------------------------
  9309. void __fastcall TCustomScpExplorerForm::DestroyWnd()
  9310. {
  9311. TForm::DestroyWnd();
  9312. FSessionsDragDropFilesEx->DragDropControl = NULL;
  9313. FQueueDragDropFilesEx->DragDropControl = NULL;
  9314. }
  9315. //---------------------------------------------------------------------------
  9316. void __fastcall TCustomScpExplorerForm::FormShow(TObject * /*Sender*/)
  9317. {
  9318. SideEnter(FCurrentSide);
  9319. FEverShown = true;
  9320. }
  9321. //---------------------------------------------------------------------------
  9322. void __fastcall TCustomScpExplorerForm::DoFindFiles(
  9323. TTerminal * ATerminal, UnicodeString Directory, const TFileMasks & FileMask,
  9324. TFileFoundEvent OnFileFound, TFindingFileEvent OnFindingFile)
  9325. {
  9326. if (!NonVisualDataModule->Busy)
  9327. {
  9328. TTerminalManager::Instance()->ActiveSession = DebugNotNull(dynamic_cast<TManagedTerminal *>(ATerminal));
  9329. Configuration->Usage->Inc(L"FileFinds");
  9330. LockWindow(true);
  9331. NonVisualDataModule->StartBusy();
  9332. WinConfiguration->LockedInterface = true;
  9333. try
  9334. {
  9335. Terminal->FilesFind(Directory, FileMask, OnFileFound, OnFindingFile);
  9336. }
  9337. __finally
  9338. {
  9339. WinConfiguration->LockedInterface = false;
  9340. NonVisualDataModule->EndBusy();
  9341. UnlockWindow();
  9342. }
  9343. }
  9344. }
  9345. //---------------------------------------------------------------------------
  9346. void __fastcall TCustomScpExplorerForm::DoFocusRemotePath(TTerminal * ATerminal, const UnicodeString & Path)
  9347. {
  9348. if (!NonVisualDataModule->Busy)
  9349. {
  9350. TTerminalManager::Instance()->ActiveSession = DebugNotNull(dynamic_cast<TManagedTerminal *>(ATerminal));
  9351. SetFocus();
  9352. RemoteDirView->Path = UnixExtractFilePath(Path);
  9353. UnicodeString FileName = UnixExtractFileName(Path);
  9354. TListItem * Item = RemoteDirView->FindFileItem(FileName);
  9355. // If not found, it can because we have loaded directory from cache and the file is new, try again after reload.
  9356. // (tbd: we should not even try this, if we have not loaded the directory from the cache)
  9357. if (Item == NULL)
  9358. {
  9359. ReloadDirectory(osRemote);
  9360. Item = RemoteDirView->FindFileItem(FileName);
  9361. }
  9362. if (Item != NULL)
  9363. {
  9364. RemoteDirView->ItemFocused = Item;
  9365. RemoteDirView->ItemFocused->MakeVisible(false);
  9366. RemoteDirView->SetFocus();
  9367. }
  9368. }
  9369. }
  9370. //---------------------------------------------------------------------------
  9371. bool __fastcall TCustomScpExplorerForm::CanOperateOnFoundFiles(TTerminal * ATerminal)
  9372. {
  9373. bool Result = !NonVisualDataModule->Busy;
  9374. if (Result)
  9375. {
  9376. TTerminalManager::Instance()->ActiveSession = DebugNotNull(dynamic_cast<TManagedTerminal *>(ATerminal));
  9377. }
  9378. return Result;
  9379. }
  9380. //---------------------------------------------------------------------------
  9381. void __fastcall TCustomScpExplorerForm::DoOperationOnFoundFiles(
  9382. TFileOperation Operation, TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  9383. {
  9384. if (CanOperateOnFoundFiles(ATerminal))
  9385. {
  9386. TValueRestorer<TFileOperationFinishedEvent> OnFileOperationFinishedRestorer(FOnFileOperationFinished);
  9387. FOnFileOperationFinished = OnFileOperationFinished;
  9388. ExecuteFileOperation(Operation, osRemote, FileList, false, NULL);
  9389. }
  9390. }
  9391. //---------------------------------------------------------------------------
  9392. void __fastcall TCustomScpExplorerForm::DoDeleteFoundFiles(
  9393. TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  9394. {
  9395. DoOperationOnFoundFiles(foDelete, ATerminal, FileList, OnFileOperationFinished);
  9396. }
  9397. //---------------------------------------------------------------------------
  9398. void __fastcall TCustomScpExplorerForm::DoDownloadFoundFiles(
  9399. TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  9400. {
  9401. DoOperationOnFoundFiles(foCopy, ATerminal, FileList, OnFileOperationFinished);
  9402. }
  9403. //---------------------------------------------------------------------------
  9404. void __fastcall TCustomScpExplorerForm::DoEditFoundFiles(
  9405. TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  9406. {
  9407. if (CanOperateOnFoundFiles(ATerminal))
  9408. {
  9409. for (int Index = 0; Index < FileList->Count; Index++)
  9410. {
  9411. UnicodeString FileName = FileList->Strings[Index];
  9412. TRemoteFile * File = static_cast<TRemoteFile *>(FileList->Objects[Index]);
  9413. ExecuteRemoteFile(FileName, File, efDefaultEditor);
  9414. OnFileOperationFinished(FileName, true);
  9415. }
  9416. }
  9417. }
  9418. //---------------------------------------------------------------------------
  9419. void __fastcall TCustomScpExplorerForm::RemoteFindFiles()
  9420. {
  9421. FFileFindTerminal = Terminal;
  9422. ShowFileFindDialog(
  9423. Terminal, RemoteDirView->Path, DoFindFiles, DoFocusRemotePath, DoDeleteFoundFiles,
  9424. DoDownloadFoundFiles, DoEditFoundFiles);
  9425. }
  9426. //---------------------------------------------------------------------------
  9427. void __fastcall TCustomScpExplorerForm::UpdateTaskbarList(ITaskbarList3 * TaskbarList)
  9428. {
  9429. FTaskbarList = TaskbarList;
  9430. }
  9431. //---------------------------------------------------------------------------
  9432. void __fastcall TCustomScpExplorerForm::SessionsPageControlMouseDown(
  9433. TObject * /*Sender*/, TMouseButton Button, TShiftState /*Shift*/, int X, int Y)
  9434. {
  9435. int Index = SessionsPageControl->IndexOfTabAt(X, Y);
  9436. if (Index >= 0)
  9437. {
  9438. if (Button == mbLeft)
  9439. {
  9440. // WORKAROUND: "Mouse down" is raised only after tab is switched.
  9441. // If switching tab (switching session) takes long enough for user
  9442. // to actually release the button, "mouse down" is still raised,
  9443. // but we do not get "mouse up" event, so dragging is not cancelled,
  9444. // prevent that by not beginning dragging in the first place.
  9445. if (IsKeyPressed(VK_LBUTTON))
  9446. {
  9447. // when user clicks the "+", we get mouse down only after the session
  9448. // is closed, when new session tab is already on X:Y, so dragging
  9449. // starts, prevent that
  9450. if (MilliSecondsBetween(Now(), FSessionsPageControlNewTabTime) > 500)
  9451. {
  9452. TTerminal * Terminal = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  9453. if (Terminal != NULL)
  9454. {
  9455. SessionsPageControl->BeginDrag(false);
  9456. }
  9457. }
  9458. }
  9459. }
  9460. else if (Button == mbMiddle)
  9461. {
  9462. // ignore middle-click for "New session tab" and the last local-local tab
  9463. TManagedTerminal * Session = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  9464. if ((Session != NULL) && CanCloseSession(Session))
  9465. {
  9466. CloseSessionTab(Index);
  9467. }
  9468. }
  9469. }
  9470. }
  9471. //---------------------------------------------------------------------------
  9472. void __fastcall TCustomScpExplorerForm::SessionsPageControlDragDrop(
  9473. TObject * /*Sender*/, TObject * /*Source*/, int X, int Y)
  9474. {
  9475. int Index = SessionsPageControl->IndexOfTabAt(X, Y);
  9476. // do not allow dropping on the "+" tab
  9477. TTerminal * TargetTerminal = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  9478. if ((TargetTerminal != NULL) &&
  9479. (SessionsPageControl->ActivePage->PageIndex != Index))
  9480. {
  9481. Configuration->Usage->Inc(L"SessionTabMoves");
  9482. // this is almost redundant as we would recreate tabs in SessionListChanged,
  9483. // but we want to actually prevent that to avoid flicker
  9484. SessionsPageControl->ActivePage->PageIndex = Index;
  9485. TTerminal * Terminal = GetSessionTabSession(SessionsPageControl->ActivePage);
  9486. TTerminalManager::Instance()->Move(Terminal, TargetTerminal);
  9487. }
  9488. }
  9489. //---------------------------------------------------------------------------
  9490. void __fastcall TCustomScpExplorerForm::SessionsPageControlDragOver(
  9491. TObject * Sender, TObject * Source, int X, int Y,
  9492. TDragState /*State*/, bool & Accept)
  9493. {
  9494. Accept = (Sender == Source);
  9495. if (Accept)
  9496. {
  9497. int Index = SessionsPageControl->IndexOfTabAt(X, Y);
  9498. TTerminal * Terminal = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  9499. // do not allow dragging to the "+" tab
  9500. Accept = (Terminal != NULL);
  9501. }
  9502. }
  9503. //---------------------------------------------------------------------------
  9504. void __fastcall TCustomScpExplorerForm::SessionsDDDragOver(
  9505. int DebugUsedArg(KeyState), const TPoint & Point, int & Effect, int DebugUsedArg(PreferredEffect))
  9506. {
  9507. int Index = SessionsPageControl->IndexOfTabAt(Point.X, Point.Y);
  9508. if (Index < 0)
  9509. {
  9510. Effect = DROPEFFECT_NONE;
  9511. }
  9512. else
  9513. {
  9514. TManagedTerminal * TargetSession = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  9515. // do not allow dropping on the "+" tab or on disconnected or local-local tabs
  9516. if (!IsActiveTerminal(TargetSession) || DebugAlwaysFalse(TargetSession->LocalBrowser))
  9517. {
  9518. Effect = DROPEFFECT_NONE;
  9519. }
  9520. else if ((TargetSession != Terminal) && (Effect == DROPEFFECT_MOVE))
  9521. {
  9522. Effect = DROPEFFECT_COPY;
  9523. }
  9524. }
  9525. }
  9526. //---------------------------------------------------------------------------
  9527. void __fastcall TCustomScpExplorerForm::SessionsDDProcessDropped(
  9528. TObject * /*Sender*/, int /*KeyState*/, const TPoint & Point, int Effect)
  9529. {
  9530. int Index = SessionsPageControl->IndexOfTabAt(Point.X, Point.Y);
  9531. // do not allow dropping on local-local, disconnected and the "+" tab
  9532. // (though we do not seem to get here in that case anyway, contrary to RemoteFileControlDDTargetDrop)
  9533. TManagedTerminal * TargetTerminal = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  9534. if (DebugAlwaysTrue(IsActiveTerminal(TargetTerminal) && !TargetTerminal->LocalBrowser))
  9535. {
  9536. DebugAssert(!IsFileControl(DropSourceControl, osRemote));
  9537. if (!IsFileControl(DropSourceControl, osRemote))
  9538. {
  9539. TTerminalManager::Instance()->ActiveSession = TargetTerminal;
  9540. RemoteFileControlDragDropFileOperation(SessionsPageControl, Effect,
  9541. // Why don't we use Terminal->CurrentDirectory directly?
  9542. TTerminalManager::Instance()->ActiveTerminal->CurrentDirectory,
  9543. // do not force queue, drag drop
  9544. false, true);
  9545. }
  9546. }
  9547. }
  9548. //---------------------------------------------------------------------------
  9549. void __fastcall TCustomScpExplorerForm::QueueDDProcessDropped(
  9550. TObject * /*Sender*/, int /*KeyState*/, const TPoint & /*Point*/, int Effect)
  9551. {
  9552. // Downloads are handled in RemoteFileControlDDEnd
  9553. if (!IsFileControl(DropSourceControl, osRemote))
  9554. {
  9555. RemoteFileControlDragDropFileOperation(QueueView3, Effect,
  9556. Terminal->CurrentDirectory,
  9557. // force queue, drag drop
  9558. true, true);
  9559. }
  9560. }
  9561. //---------------------------------------------------------------------------
  9562. void __fastcall TCustomScpExplorerForm::FormClose(TObject * /*Sender*/, TCloseAction & /*Action*/)
  9563. {
  9564. FShowing = false;
  9565. // Do not save empty workspace
  9566. if (WinConfiguration->AutoSaveWorkspace && (ManagedSession != NULL))
  9567. {
  9568. std::unique_ptr<TObjectList> DataList(DoCollectWorkspace());
  9569. UnicodeString Name = WorkspaceName();
  9570. DoSaveWorkspace(
  9571. Name, DataList.get(),
  9572. !Configuration->DisablePasswordStoring &&
  9573. WinConfiguration->AutoSaveWorkspacePasswords, false);
  9574. WinConfiguration->LastStoredSession = Name;
  9575. }
  9576. }
  9577. //---------------------------------------------------------------------------
  9578. void __fastcall TCustomScpExplorerForm::RemoteDirViewRead(TObject * /*Sender*/)
  9579. {
  9580. TManagedTerminal * ManagedTerminal =
  9581. dynamic_cast<TManagedTerminal *>(RemoteDirView->Terminal);
  9582. if (ManagedTerminal != NULL)
  9583. {
  9584. ManagedTerminal->DirectoryLoaded = Now();
  9585. }
  9586. }
  9587. //---------------------------------------------------------------------------
  9588. void __fastcall TCustomScpExplorerForm::DirViewSelectItem(TObject * Sender,
  9589. TListItem * /*Item*/, bool /*Selected*/)
  9590. {
  9591. TCustomDirView * DirView = reinterpret_cast<TCustomDirView *>(Sender);
  9592. switch (DirView->LastSelectMethod)
  9593. {
  9594. case smKeyboard:
  9595. Configuration->Usage->Inc(L"KeyboardSelections");
  9596. break;
  9597. case smMouse:
  9598. Configuration->Usage->Inc(L"MouseSelections");
  9599. break;
  9600. }
  9601. }
  9602. //---------------------------------------------------------------------------
  9603. int __fastcall TCustomScpExplorerForm::AddSessionColor(TColor Color)
  9604. {
  9605. if (Color != 0)
  9606. {
  9607. return GetSessionColorImage(FSessionColors, Color, FSessionColorMaskImageIndex);
  9608. }
  9609. else
  9610. {
  9611. return FSessionTabImageIndex;
  9612. }
  9613. }
  9614. //---------------------------------------------------------------------------
  9615. int __fastcall TCustomScpExplorerForm::AddFixedSessionImage(int GlyphsSourceIndex)
  9616. {
  9617. TPngImageCollectionItem * Item =
  9618. GlyphsModule->ExplorerImages->PngImages->Items[GlyphsSourceIndex];
  9619. FSessionColors->AddPng(Item->PngImage);
  9620. return FSessionColors->Count - 1;
  9621. }
  9622. //---------------------------------------------------------------------------
  9623. void __fastcall TCustomScpExplorerForm::AddFixedSessionImages()
  9624. {
  9625. FSessionColors->SetSize(GlyphsModule->ExplorerImages->Width, GlyphsModule->ExplorerImages->Height);
  9626. FNewRemoteTabTabImageIndex = AddFixedSessionImage(NonVisualDataModule->NewRemoteTabAction->ImageIndex);
  9627. FNewLocalTabTabImageIndex = AddFixedSessionImage(NonVisualDataModule->NewLocalTabAction->ImageIndex);
  9628. FSessionTabImageIndex = AddFixedSessionImage(SiteImageIndex);
  9629. FSessionColorMaskImageIndex = AddFixedSessionImage(SiteColorMaskImageIndex);
  9630. FLocalBrowserTabImageIndex = AddFixedSessionImage(LocalBrowserImageIndex);
  9631. }
  9632. //---------------------------------------------------------------------------
  9633. int TCustomScpExplorerForm::GetNewTabActionImageIndex()
  9634. {
  9635. return NonVisualDataModule->NewRemoteTabAction->ImageIndex;
  9636. }
  9637. //---------------------------------------------------------------------------
  9638. int TCustomScpExplorerForm::GetNewTabTabImageIndex(TOperationSide Side)
  9639. {
  9640. switch (Side)
  9641. {
  9642. case osLocal:
  9643. return FNewLocalTabTabImageIndex;
  9644. case osRemote:
  9645. case osCurrent:
  9646. return FNewRemoteTabTabImageIndex;
  9647. default:
  9648. DebugFail();
  9649. return -1;
  9650. }
  9651. }
  9652. //---------------------------------------------------------------------------
  9653. void __fastcall TCustomScpExplorerForm::CollectItemsWithTextDisplayMode(TWinControl * Control)
  9654. {
  9655. for (int Index = 0; Index < Control->ControlCount; Index++)
  9656. {
  9657. TControl * AControl = Control->Controls[Index];
  9658. TWinControl * WinControl = dynamic_cast<TWinControl *>(AControl);
  9659. if (WinControl != NULL)
  9660. {
  9661. CollectItemsWithTextDisplayMode(WinControl);
  9662. }
  9663. TTBCustomToolbar * Toolbar = dynamic_cast<TTBCustomToolbar *>(AControl);
  9664. if (Toolbar != NULL)
  9665. {
  9666. // we care for top-level items only
  9667. TTBCustomItem * Items = Toolbar->Items;
  9668. for (int ItemIndex = 0; ItemIndex < Items->Count; ItemIndex++)
  9669. {
  9670. TTBCustomItem * Item = Items->Items[ItemIndex];
  9671. if (((Item->DisplayMode == nbdmImageAndText) ||
  9672. (dynamic_cast<TTBXLabelItem *>(Item) != NULL)) &&
  9673. EligibleForImageDisplayMode(Item))
  9674. {
  9675. FItemsWithTextDisplayMode.insert(Item);
  9676. }
  9677. }
  9678. }
  9679. }
  9680. }
  9681. //---------------------------------------------------------------------------
  9682. bool __fastcall TCustomScpExplorerForm::EligibleForImageDisplayMode(TTBCustomItem * /*Item*/)
  9683. {
  9684. return true;
  9685. }
  9686. //---------------------------------------------------------------------------
  9687. bool __fastcall TCustomScpExplorerForm::UpdateToolbarDisplayMode()
  9688. {
  9689. bool SelectiveToolbarText = WinConfiguration->SelectiveToolbarText;
  9690. TTBItemDisplayMode DisplayMode = (SelectiveToolbarText ? nbdmImageAndText : nbdmDefault);
  9691. typedef std::set<TTBCustomItem *> TItemsWithTextDisplayMode;
  9692. TItemsWithTextDisplayMode::iterator i = FItemsWithTextDisplayMode.begin();
  9693. bool Result = true;
  9694. while (Result && (i != FItemsWithTextDisplayMode.end()))
  9695. {
  9696. TTBCustomItem * Item = *i;
  9697. TTBXLabelItem * Label = dynamic_cast<TTBXLabelItem *>(Item);
  9698. if (Label != NULL)
  9699. {
  9700. // optimization
  9701. if (Label->Visible == SelectiveToolbarText)
  9702. {
  9703. Result = false;
  9704. }
  9705. else
  9706. {
  9707. Label->Visible = SelectiveToolbarText;
  9708. }
  9709. }
  9710. else
  9711. {
  9712. // optimization
  9713. if (Item->DisplayMode == DisplayMode)
  9714. {
  9715. Result = false;
  9716. }
  9717. else
  9718. {
  9719. Item->DisplayMode = DisplayMode;
  9720. }
  9721. }
  9722. ++i;
  9723. }
  9724. UpdateNewTabTab();
  9725. return Result;
  9726. }
  9727. //---------------------------------------------------------------------------
  9728. void __fastcall TCustomScpExplorerForm::DisplaySystemContextMenu()
  9729. {
  9730. DebugFail();
  9731. }
  9732. //---------------------------------------------------------------------------
  9733. bool __fastcall TCustomScpExplorerForm::IsBusy()
  9734. {
  9735. // Among other, a lock level is non zero, while directory is being loaded,
  9736. // even when it happens as a result of dirview navigation,
  9737. // i.e. when TNonVisualDataModule is NOT FBusy.
  9738. // That's why the TNonVisualDataModule::GetBusy calls this method.
  9739. // Among other this prevents a panel auto update to occur while
  9740. // directory is changing.
  9741. return (FLockLevel > 0) || DirView(osCurrent)->IsEditing();
  9742. }
  9743. //---------------------------------------------------------------------------
  9744. Boolean __fastcall TCustomScpExplorerForm::AllowedAction(TAction * Action, TActionAllowed Allowed)
  9745. {
  9746. // While the window is disabled, we still seem to process menu shortcuts at least,
  9747. // so stop it at least here.
  9748. // See also TCustomScpExplorerForm::RemoteDirViewBusy
  9749. bool Result;
  9750. if (Allowed == aaUpdate)
  9751. {
  9752. Result = true;
  9753. }
  9754. else
  9755. {
  9756. Result = !NonVisualDataModule->Busy;
  9757. if (Result && (Allowed == aaShortCut))
  9758. {
  9759. // Otherwise F5 for local-local will be swallowed by disabled local<>remote actions in TCustomScpExplorerForm::KeyDown
  9760. Action->Update();
  9761. Result = Action->Enabled;
  9762. }
  9763. }
  9764. return Result;
  9765. }
  9766. //---------------------------------------------------------------------------
  9767. void __fastcall TCustomScpExplorerForm::EditMenuItemPopup(TTBCustomItem * Sender, bool FromLink)
  9768. {
  9769. NonVisualDataModule->EditMenuItemPopup(Sender, FromLink);
  9770. }
  9771. //---------------------------------------------------------------------------
  9772. void __fastcall TCustomScpExplorerForm::DirViewBusy(TObject * /*Sender*/, int Busy, bool & State)
  9773. {
  9774. // This is somewhat redundant to LockWindow() call from
  9775. // TTerminalManager::TerminalReadDirectoryProgress.
  9776. // But disabling window is known not to block keyboard shorcuts
  9777. // (see TCustomScpExplorerForm::AllowedAction), this hopefully works.
  9778. if (Busy > 0)
  9779. {
  9780. if (NonVisualDataModule->Busy)
  9781. {
  9782. State = false;
  9783. }
  9784. else
  9785. {
  9786. NonVisualDataModule->StartBusy();
  9787. // Will be countered for TCustomDirView.DoExecute in DoDirViewExecFile
  9788. LockWindow();
  9789. }
  9790. }
  9791. else if (Busy < 0)
  9792. {
  9793. UnlockWindow();
  9794. NonVisualDataModule->EndBusy();
  9795. }
  9796. else
  9797. {
  9798. State = NonVisualDataModule->Busy;
  9799. }
  9800. }
  9801. //---------------------------------------------------------------------------
  9802. void __fastcall TCustomScpExplorerForm::SessionsPageControlContextPopup(TObject * /*Sender*/, TPoint & MousePos, bool & Handled)
  9803. {
  9804. int Index = SessionsPageControl->IndexOfTabAt(MousePos.X, MousePos.Y);
  9805. if (Index >= 0)
  9806. {
  9807. TPopupMenu * PopupMenu = NULL;
  9808. TManagedTerminal * Session = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  9809. if (Session != NULL)
  9810. {
  9811. SessionsPageControl->ActivePageIndex = Index;
  9812. if (DebugAlwaysTrue(SessionTabSwitched()))
  9813. {
  9814. // copied from TControl.WMContextMenu
  9815. SendCancelMode(SessionsPageControl);
  9816. // explicit popup instead of using PopupMenu property
  9817. // to avoid menu to popup somewhere within SessionTabSwitched above,
  9818. // while connecting yet not-connected session and hence
  9819. // allowing an access to commands over not-completelly connected session
  9820. PopupMenu = Session->LocalBrowser ? NonVisualDataModule->LocalBrowserPopup : NonVisualDataModule->SessionsPopup;
  9821. }
  9822. }
  9823. else
  9824. {
  9825. if (SupportsLocalBrowser())
  9826. {
  9827. PopupMenu = NonVisualDataModule->NewTabPopup;
  9828. }
  9829. }
  9830. if (PopupMenu != NULL)
  9831. {
  9832. TPoint Point = SessionsPageControl->ClientToScreen(MousePos);
  9833. PopupMenu->PopupComponent = SessionsPageControl;
  9834. PopupMenu->Popup(Point.x, Point.y);
  9835. }
  9836. }
  9837. Handled = true;
  9838. }
  9839. //---------------------------------------------------------------------------
  9840. bool __fastcall TCustomScpExplorerForm::CanChangePassword()
  9841. {
  9842. return HasActiveTerminal() && Terminal->IsCapable[fcChangePassword];
  9843. }
  9844. //---------------------------------------------------------------------------
  9845. void __fastcall TCustomScpExplorerForm::ChangePassword()
  9846. {
  9847. Configuration->Usage->Inc(L"PasswordChanges");
  9848. std::unique_ptr<TSessionData> ChangePasswordData(Terminal->SessionData->Clone());
  9849. ChangePasswordData->DisableAuthentationsExceptPassword();
  9850. ChangePasswordData->ChangePassword = true;
  9851. std::unique_ptr<TTerminal> ChangePasswordSession(
  9852. Terminal->CreateSecondarySession(L"ChangePassword", ChangePasswordData.get()));
  9853. ChangePasswordSession->AutoReadDirectory = false;
  9854. if (TTerminalManager::Instance()->ConnectTerminal(ChangePasswordSession.get()))
  9855. {
  9856. MessageDialog(MainInstructions(LoadStr(PASSWORD_CHANGED)), qtInformation, qaOK, HELP_CHANGE_PASSWORD);
  9857. }
  9858. else
  9859. {
  9860. Configuration->Usage->Inc(L"PasswordChangesFailed");
  9861. }
  9862. }
  9863. //---------------------------------------------------------------------------
  9864. bool __fastcall TCustomScpExplorerForm::CanPrivateKeyUpload()
  9865. {
  9866. // No nice way to assert SSH2
  9867. return HasActiveTerminal() && (Terminal->FSProtocol == cfsSFTP);
  9868. }
  9869. //---------------------------------------------------------------------------
  9870. void __fastcall TCustomScpExplorerForm::PrivateKeyUpload()
  9871. {
  9872. TOperationVisualizer OperationVisualizer;
  9873. UnicodeString FileName = Terminal->SessionData->PublicKeyFile;
  9874. TTerminalManager::Instance()->UploadPublicKey(Terminal, NULL, FileName);
  9875. }
  9876. //---------------------------------------------------------------------------
  9877. void __fastcall TCustomScpExplorerForm::ChangeScale(int M, int D)
  9878. {
  9879. TForm::ChangeScale(M, D);
  9880. int APixelsPerInch = GetControlPixelsPerInch(this);
  9881. HANDLE ResourceModule = GUIConfiguration->ChangeToDefaultResourceModule();
  9882. try
  9883. {
  9884. GlyphsModule->SetPixelsPerInch(APixelsPerInch);
  9885. }
  9886. __finally
  9887. {
  9888. GUIConfiguration->ChangeResourceModule(ResourceModule);
  9889. }
  9890. }
  9891. //---------------------------------------------------------------------------
  9892. void __fastcall TCustomScpExplorerForm::DockContextPopup(TObject * Sender, TPoint & MousePos, bool & /*Handled*/)
  9893. {
  9894. NonVisualDataModule->ControlContextPopup(Sender, MousePos);
  9895. }
  9896. //---------------------------------------------------------------------
  9897. class TPublicControl : public TControl
  9898. {
  9899. friend class TCustomScpExplorerForm;
  9900. };
  9901. //---------------------------------------------------------------------------
  9902. void __fastcall TCustomScpExplorerForm::CopyPopup(TControl * DestControl, TControl * SourceControl)
  9903. {
  9904. static_cast<TPublicControl *>(DestControl)->PopupMenu = static_cast<TPublicControl *>(SourceControl)->PopupMenu;
  9905. static_cast<TPublicControl *>(DestControl)->OnContextPopup = static_cast<TPublicControl *>(SourceControl)->OnContextPopup;
  9906. }
  9907. //---------------------------------------------------------------------------
  9908. void __fastcall TCustomScpExplorerForm::DoBookmarkClick(TOperationSide Side, TObject * Sender)
  9909. {
  9910. TBookmark * Bookmark = DebugNotNull(reinterpret_cast<TBookmark *>(dynamic_cast<TTBCustomItem *>(Sender)->Tag));
  9911. OpenBookmark(Side, Bookmark);
  9912. }
  9913. //---------------------------------------------------------------------------
  9914. void __fastcall TCustomScpExplorerForm::LocalBookmarkClick(TObject * Sender)
  9915. {
  9916. DoBookmarkClick(osLocal, Sender);
  9917. }
  9918. //---------------------------------------------------------------------------
  9919. void __fastcall TCustomScpExplorerForm::RemoteBookmarkClick(TObject * Sender)
  9920. {
  9921. DoBookmarkClick(osRemote, Sender);
  9922. }
  9923. //---------------------------------------------------------------------------
  9924. void __fastcall TCustomScpExplorerForm::CreateOpenDirMenuList(
  9925. TTBCustomItem * Menu, TOperationSide Side, TBookmarkList * BookmarkList)
  9926. {
  9927. if (BookmarkList != NULL)
  9928. {
  9929. TNotifyEvent OnBookmarkClick = (Side == osLocal) ? &LocalBookmarkClick : &RemoteBookmarkClick;
  9930. if (IsLocalBrowserMode())
  9931. {
  9932. Side = osLocal;
  9933. }
  9934. if (!WinConfiguration->UseLocationProfiles)
  9935. {
  9936. std::unique_ptr<TStringList> Directories(new TStringList());
  9937. Directories->CaseSensitive = (Side == osRemote);
  9938. for (int Index = 0; Index < BookmarkList->Count; Index++)
  9939. {
  9940. TBookmark * Bookmark = BookmarkList->Bookmarks[Index];
  9941. UnicodeString Directory = Bookmark->GetSideDirectory(Side);
  9942. if (!Directory.IsEmpty() && (Directories->IndexOf(Directory) < 0))
  9943. {
  9944. std::unique_ptr<TTBCustomItem> Item(new TTBXItem(Owner));
  9945. Item->Caption = EscapeHotkey(Directory);
  9946. Item->ShortCut = Bookmark->ShortCut;
  9947. Item->OnClick = OnBookmarkClick;
  9948. Item->Tag = reinterpret_cast<int>(Bookmark);
  9949. Directories->Add(Directory);
  9950. Menu->Add(Item.release());
  9951. }
  9952. }
  9953. }
  9954. else
  9955. {
  9956. std::unique_ptr<TStrings> Folders(CreateSortedStringList());
  9957. for (int Index = 0; Index < BookmarkList->Count; Index++)
  9958. {
  9959. TBookmark * Bookmark = BookmarkList->Bookmarks[Index];
  9960. if (!Bookmark->Node.IsEmpty())
  9961. {
  9962. Folders->Add(Bookmark->Node);
  9963. }
  9964. }
  9965. for (int Index = 0; Index < Folders->Count; Index++)
  9966. {
  9967. std::unique_ptr<TTBCustomItem> Item(new TTBXSubmenuItem(Owner));
  9968. Item->Caption = Folders->Strings[Index];
  9969. Item->ImageIndex = NonVisualDataModule->RemoteChangePathAction2->ImageIndex;
  9970. Folders->Objects[Index] = Item.get();
  9971. Menu->Add(Item.release());
  9972. }
  9973. for (int Index = 0; Index < BookmarkList->Count; Index++)
  9974. {
  9975. TBookmark * Bookmark = BookmarkList->Bookmarks[Index];
  9976. TTBCustomItem * Parent;
  9977. if (!Bookmark->Node.IsEmpty())
  9978. {
  9979. DebugAssert(Folders->IndexOf(Bookmark->Node) >= 0);
  9980. Parent = dynamic_cast<TTBCustomItem *>(Folders->Objects[Folders->IndexOf(Bookmark->Node)]);
  9981. }
  9982. else
  9983. {
  9984. Parent = Menu;
  9985. }
  9986. std::unique_ptr<TTBCustomItem> Item(new TTBXItem(Owner));
  9987. Item->Caption = Bookmark->Name;
  9988. Item->ShortCut = Bookmark->ShortCut;
  9989. Item->OnClick = OnBookmarkClick;
  9990. Item->Tag = reinterpret_cast<int>(Bookmark);
  9991. if (((Bookmark->Name == Bookmark->Local) || (Bookmark->Name == Bookmark->Remote)) &&
  9992. (Bookmark->Local.IsEmpty() || Bookmark->Remote.IsEmpty()))
  9993. {
  9994. // No hint for location profiles that are actually mere "bookmarks"
  9995. }
  9996. else
  9997. {
  9998. UnicodeString Hint = FORMAT(LoadStrPart(LOCATION_PROFILE_HINT, 1), (Bookmark->Name));
  9999. UnicodeString LongHint;
  10000. if (!Bookmark->Local.IsEmpty())
  10001. {
  10002. AddToList(LongHint, FORMAT(LoadStrPart(LOCATION_PROFILE_HINT, 2), (Bookmark->Local)), L"\n");
  10003. }
  10004. if (!Bookmark->Remote.IsEmpty())
  10005. {
  10006. AddToList(LongHint, FORMAT(LoadStrPart(LOCATION_PROFILE_HINT, 3), (Bookmark->Remote)), L"\n");
  10007. }
  10008. AddToList(Hint, LongHint, L"|");
  10009. Item->Hint = Hint;
  10010. }
  10011. Parent->Add(Item.release());
  10012. }
  10013. }
  10014. AddMenuSeparator(Menu);
  10015. }
  10016. }
  10017. //---------------------------------------------------------------------------
  10018. void __fastcall TCustomScpExplorerForm::CreateOpenDirMenu(TTBCustomItem * Menu, TOperationSide Side)
  10019. {
  10020. Menu->Clear();
  10021. std::unique_ptr<TTBCustomItem> Item;
  10022. Item.reset(new TTBXItem(Owner));
  10023. Item->Action = (Side == osLocal) ? NonVisualDataModule->LocalOpenDirAction : NonVisualDataModule->RemoteOpenDirAction;
  10024. Item->Options = Item->Options << tboDefault;
  10025. Menu->Add(Item.release());
  10026. AddMenuSeparator(Menu);
  10027. if (Terminal != NULL)
  10028. {
  10029. CreateOpenDirMenuList(Menu, Side, WinConfiguration->Bookmarks[Terminal->SessionData->SessionKey]);
  10030. }
  10031. CreateOpenDirMenuList(Menu, Side, WinConfiguration->SharedBookmarks);
  10032. Item.reset(new TTBXItem(Owner));
  10033. Item->Action = (Side == osLocal) ? NonVisualDataModule->LocalAddBookmarkAction2 : NonVisualDataModule->RemoteAddBookmarkAction2;
  10034. Menu->Add(Item.release());
  10035. }
  10036. //---------------------------------------------------------------------------
  10037. bool __fastcall TCustomScpExplorerForm::TryOpenDirectory(TOperationSide Side, const UnicodeString & Path)
  10038. {
  10039. bool Result = true;
  10040. bool Remote = !IsSideLocalBrowser(Side);
  10041. try
  10042. {
  10043. if (Remote)
  10044. {
  10045. Terminal->ExceptionOnFail = true;
  10046. }
  10047. try
  10048. {
  10049. DirView(Side)->Path = Path;
  10050. }
  10051. __finally
  10052. {
  10053. if (Remote)
  10054. {
  10055. Terminal->ExceptionOnFail = false;
  10056. }
  10057. }
  10058. }
  10059. catch (Exception & E)
  10060. {
  10061. if (!Remote || Terminal->Active)
  10062. {
  10063. ShowExtendedExceptionEx(Terminal, &E);
  10064. Result = false;
  10065. }
  10066. else
  10067. {
  10068. throw;
  10069. }
  10070. }
  10071. return Result;
  10072. }
  10073. //---------------------------------------------------------------------------
  10074. void __fastcall TCustomScpExplorerForm::ReloadDirectory(TOperationSide Side)
  10075. {
  10076. // Make sure there some feedback even when loading tiny local folders
  10077. TInstantOperationVisualizer Visualizer;
  10078. DirView(Side)->ReloadDirectory();
  10079. }
  10080. //---------------------------------------------------------------------------
  10081. void __fastcall TCustomScpExplorerForm::CloseSessionTab(int Index)
  10082. {
  10083. if (Index == SessionsPageControl->TabIndex)
  10084. {
  10085. CloseSession();
  10086. }
  10087. else
  10088. {
  10089. TTabSheet * TabSheet = SessionsPageControl->Pages[Index];
  10090. TTerminal * Terminal = GetSessionTabSession(TabSheet);
  10091. TTerminalManager * Manager = TTerminalManager::Instance();
  10092. if (!Terminal->Active)
  10093. {
  10094. Manager->FreeTerminal(Terminal);
  10095. }
  10096. else
  10097. {
  10098. TTerminalQueue * Queue = Manager->FindQueueForTerminal(Terminal);
  10099. if (CanCloseQueue(Queue))
  10100. {
  10101. Manager->FreeTerminal(Terminal);
  10102. }
  10103. }
  10104. }
  10105. }
  10106. //---------------------------------------------------------------------------
  10107. void __fastcall TCustomScpExplorerForm::SessionsPageControlTabButtonClick(TPageControl *, int Index)
  10108. {
  10109. TManagedTerminal * Session = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  10110. if (Session == NULL)
  10111. {
  10112. TRect ButtonRect = SessionsPageControl->TabButtonRect(Index);
  10113. ButtonRect = TRect(SessionsPageControl->ClientToScreen(ButtonRect.TopLeft()), SessionsPageControl->ClientToScreen(ButtonRect.BottomRight()));
  10114. //TPoint P = TPoint(ButtonRect.Left, ButtonRect.Bottom);
  10115. //P = SessionsPageControl->ClientToScreen(P);
  10116. MenuPopup(NonVisualDataModule->NewTabPopup, ButtonRect, SessionsPageControl);
  10117. }
  10118. else
  10119. {
  10120. CloseSessionTab(Index);
  10121. }
  10122. }
  10123. //---------------------------------------------------------------------------
  10124. void __fastcall TCustomScpExplorerForm::CopyFilesToClipboard(TOperationSide Side, bool OnFocused)
  10125. {
  10126. if (DebugAlwaysTrue(!IsSideLocalBrowser(Side)))
  10127. {
  10128. TInstantOperationVisualizer Visualizer;
  10129. // To trigger ClipboardDataObjectRelease (in case the clipboard already contains our data)
  10130. // before we create a new data object
  10131. ClipboardClear();
  10132. DebugAssert(FClipboardFakeDirectory.IsEmpty());
  10133. DebugAssert(FClipboardFakeMonitors.get() == NULL);
  10134. FClipboardFakeDirectory = CreateFakeTransferDirectory();
  10135. // Intermediate variable to WORKAROUND a crash in compiler
  10136. TObjectList * ClipboardFakeMonitors =
  10137. StartCreationDirectoryMonitorsOnEachDrive(FILE_NOTIFY_CHANGE_DIR_NAME, ClipboardFakeCreated);
  10138. FClipboardFakeMonitors.reset(ClipboardFakeMonitors);
  10139. if (FClipboardDragDropFilesEx.get() == NULL)
  10140. {
  10141. FClipboardDragDropFilesEx.reset(new TDragDropFilesEx(this));
  10142. FClipboardDragDropFilesEx->OnDataObjectRelease = ClipboardDataObjectRelease;
  10143. }
  10144. FClipboardDragDropFilesEx->FileList->Clear();
  10145. FClipboardDragDropFilesEx->FileList->AddItem(NULL, FClipboardFakeDirectory);
  10146. FClipboardDragDropFilesEx->CopyToClipboard();
  10147. FClipboardTerminal = Terminal;
  10148. // Need full paths, as cwd can be different once files are pasted
  10149. FClipboardFileList.reset(TRemoteFileList::CloneStrings(RemoteDirView->CreateFileList(OnFocused, true)));
  10150. for (int Index = 0; Index < FClipboardFileList->Count; Index++)
  10151. {
  10152. FClipboardFileList->Strings[Index] = UnixExcludeTrailingBackslash(FClipboardFileList->Strings[Index]);
  10153. }
  10154. }
  10155. }
  10156. //---------------------------------------------------------------------------
  10157. void __fastcall TCustomScpExplorerForm::ClipboardClear()
  10158. {
  10159. if (OpenClipboard(0))
  10160. {
  10161. EmptyClipboard(); // Calls ClipboardDataObjectRelease, if clipboard contains our data
  10162. CloseClipboard();
  10163. }
  10164. }
  10165. //---------------------------------------------------------------------------
  10166. bool __fastcall TCustomScpExplorerForm::DoesClipboardContainOurFiles()
  10167. {
  10168. return !FClipboardFakeDirectory.IsEmpty();
  10169. }
  10170. //---------------------------------------------------------------------------
  10171. void __fastcall TCustomScpExplorerForm::ClipboardStop()
  10172. {
  10173. RemoveDir(ApiPath(FClipboardFakeDirectory));
  10174. FClipboardFakeDirectory = UnicodeString();
  10175. FClipboardTerminal = NULL;
  10176. // Also called in Idle()
  10177. FClipboardFakeMonitors.reset(NULL);
  10178. }
  10179. //---------------------------------------------------------------------------
  10180. void __fastcall TCustomScpExplorerForm::ClipboardDataObjectRelease(TObject * /*Sender*/)
  10181. {
  10182. if (DebugAlwaysTrue(!FClipboardFakeDirectory.IsEmpty()))
  10183. {
  10184. ClipboardStop();
  10185. }
  10186. }
  10187. //---------------------------------------------------------------------------
  10188. void __fastcall TCustomScpExplorerForm::ClipboardDownload(const UnicodeString & TargetDirectory, bool NoConfirmation, bool DragDrop)
  10189. {
  10190. TTerminalManager * Manager = TTerminalManager::Instance();
  10191. Manager->ActiveSession = FClipboardTerminal;
  10192. TTransferOperationParam Params;
  10193. Params.TargetDirectory = TargetDirectory;
  10194. Params.DragDrop = DragDrop;
  10195. ExecuteFileOperation(foCopy, osRemote, FClipboardFileList.get(), NoConfirmation, &Params);
  10196. }
  10197. //---------------------------------------------------------------------------
  10198. void __fastcall TCustomScpExplorerForm::ClipboardFakeCreated(TObject * /*Sender*/, const UnicodeString FileName)
  10199. {
  10200. // It can actually rarelly happen that some random file is created, while we are shuttting down the monitor
  10201. // (as it pumps a Windows message queue while being shutted down)
  10202. if (DebugAlwaysTrue(!FClipboardFakeDirectory.IsEmpty()) &&
  10203. SameText(ExtractFileName(FileName), ExtractFileName(FClipboardFakeDirectory)))
  10204. {
  10205. // Can fail as it can be e.g. locked by AV, so we retry that later
  10206. bool Removed = RemoveDir(ApiPath(FileName));
  10207. try
  10208. {
  10209. if (!NonVisualDataModule->Busy)
  10210. {
  10211. bool NoConfirmation = (WinConfiguration->DDTransferConfirmation == asOff);
  10212. ClipboardDownload(ExtractFilePath(FileName), NoConfirmation, true);
  10213. }
  10214. }
  10215. __finally
  10216. {
  10217. if (!Removed)
  10218. {
  10219. RemoveDir(ApiPath(FileName));
  10220. }
  10221. }
  10222. }
  10223. }
  10224. //---------------------------------------------------------------------------
  10225. void __fastcall TCustomScpExplorerForm::DirViewGetItemColor(
  10226. TObject * Sender, UnicodeString FileName, bool Directory, __int64 Size, TDateTime Modification, TColor & Color)
  10227. {
  10228. TFileMasks::TParams MaskParams;
  10229. MaskParams.Size = Size;
  10230. MaskParams.Modification = Modification;
  10231. TCustomDirView * DirView = DebugNotNull(dynamic_cast<TCustomDirView *>(Sender));
  10232. for (TFileColorData::TList::const_iterator Iter = FFileColors.begin(); Iter != FFileColors.end(); Iter++)
  10233. {
  10234. bool ImplicitMatch;
  10235. if (Iter->FileMask.Matches(FileName, Directory, DirView->PathName, &MaskParams, false, ImplicitMatch) &&
  10236. !ImplicitMatch)
  10237. {
  10238. Color = Iter->Color;
  10239. break;
  10240. }
  10241. }
  10242. }
  10243. //---------------------------------------------------------------------------
  10244. void __fastcall TCustomScpExplorerForm::DirViewKeyDown(TObject * Sender, WORD & Key, TShiftState)
  10245. {
  10246. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  10247. if (!DirView->IsEditing())
  10248. {
  10249. // Handled here, instead of RemoteDirViewKeyPress (as on Login dialog),
  10250. // as VK_BACK is handled by TCustomDirView.KeyDown and we can swallow it here,
  10251. // while searching
  10252. if (Key == VK_BACK)
  10253. {
  10254. if (!FIncrementalSearch.IsEmpty())
  10255. {
  10256. if (FIncrementalSearch.Length() == 1)
  10257. {
  10258. ResetIncrementalSearch();
  10259. }
  10260. else
  10261. {
  10262. UnicodeString NewText = FIncrementalSearch.SubString(1, FIncrementalSearch.Length() - 1);
  10263. IncrementalSearch(NewText, false, false);
  10264. }
  10265. Key = 0;
  10266. }
  10267. }
  10268. }
  10269. }
  10270. //---------------------------------------------------------------------------
  10271. void __fastcall TCustomScpExplorerForm::DirViewKeyPress(TObject * Sender, wchar_t & Key)
  10272. {
  10273. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  10274. if (!DirView->IsEditing() && (WinConfiguration->PanelSearch != isOff))
  10275. {
  10276. // Filter control sequences.
  10277. // When not searching yet, prefer use of the space for toggling file selection
  10278. // (so we cannot incrementally search for a string starting with a space).
  10279. if ((Key > VK_SPACE) ||
  10280. ((Key == VK_SPACE) && (GetKeyState(VK_CONTROL) >= 0) && !FIncrementalSearch.IsEmpty()))
  10281. {
  10282. IncrementalSearch(FIncrementalSearch + Key, false, false);
  10283. Key = 0;
  10284. }
  10285. }
  10286. }
  10287. //---------------------------------------------------------------------------
  10288. void __fastcall TCustomScpExplorerForm::ResetIncrementalSearch()
  10289. {
  10290. if (!FIncrementalSearch.IsEmpty())
  10291. {
  10292. FIncrementalSearch = L"";
  10293. DirView(osCurrent)->UpdateStatusBar();
  10294. }
  10295. }
  10296. //---------------------------------------------------------------------------
  10297. void __fastcall TCustomScpExplorerForm::IncrementalSearch(const UnicodeString & Text, bool SkipCurrent, bool Reverse)
  10298. {
  10299. TListItem * Item = SearchFile(Text, SkipCurrent, Reverse);
  10300. if (Item == NULL)
  10301. {
  10302. MessageBeep(MB_ICONHAND);
  10303. }
  10304. else
  10305. {
  10306. TCustomDirView * ADirView = DirView(osCurrent);
  10307. {
  10308. TAutoNestingCounter Guard(FIncrementalSearching);
  10309. ADirView->FocusItem(Item);
  10310. if (ADirView->NortonLike == nlOff)
  10311. {
  10312. if ((ADirView->Selected != Item) ||
  10313. (ADirView->SelCount > 1))
  10314. {
  10315. ADirView->ClearSelection();
  10316. ADirView->Selected = Item;
  10317. }
  10318. }
  10319. }
  10320. FIncrementalSearch = Text;
  10321. Item->MakeVisible(false);
  10322. TListItem * NextItem = SearchFile(Text, true, Reverse);
  10323. FIncrementalSearchHaveNext = (NextItem != NULL) && (NextItem != Item);
  10324. ADirView->UpdateStatusBar();
  10325. }
  10326. }
  10327. //---------------------------------------------------------------------------
  10328. TListItem * __fastcall TCustomScpExplorerForm::GetNextFile(TListItem * Item, bool Reverse)
  10329. {
  10330. int Index = Item->Index;
  10331. if (!Reverse)
  10332. {
  10333. Index++;
  10334. if (Index >= Item->Owner->Count)
  10335. {
  10336. Index = 0;
  10337. }
  10338. }
  10339. else
  10340. {
  10341. Index--;
  10342. if (Index < 0)
  10343. {
  10344. Index = Item->Owner->Count - 1;
  10345. }
  10346. }
  10347. return Item->Owner->Item[Index];
  10348. }
  10349. //---------------------------------------------------------------------------
  10350. TListItem * __fastcall TCustomScpExplorerForm::SearchFile(const UnicodeString & Text, bool SkipCurrent, bool Reverse)
  10351. {
  10352. TCustomDirView * ADirView = DirView(osCurrent);
  10353. if (ADirView->Items->Count == 0)
  10354. {
  10355. return NULL;
  10356. }
  10357. else
  10358. {
  10359. TListItem * CurrentItem = (ADirView->ItemFocused != NULL) ? ADirView->ItemFocused : ADirView->Items->Item[0];
  10360. TListItem * Item = CurrentItem;
  10361. if (SkipCurrent)
  10362. {
  10363. Item = DebugNotNull(GetNextFile(Item, Reverse));
  10364. }
  10365. while (true)
  10366. {
  10367. bool Matches = false;
  10368. switch (WinConfiguration->PanelSearch)
  10369. {
  10370. case isNameStartOnly:
  10371. Matches = ContainsTextSemiCaseSensitive(Item->Caption.SubString(1, Text.Length()), Text);
  10372. break;
  10373. case isName:
  10374. Matches = ContainsTextSemiCaseSensitive(Item->Caption, Text);
  10375. break;
  10376. case isAll:
  10377. int ColCount = (ADirView->ViewStyle == vsReport) ? ADirView->ColProperties->Count : 1;
  10378. int Index = 0;
  10379. while ((Index < ColCount) && !Matches)
  10380. {
  10381. bool Visible = (ADirView->ViewStyle == vsReport) ? ADirView->ColProperties->Visible[Index] : true;
  10382. if (Visible)
  10383. {
  10384. Matches = ContainsTextSemiCaseSensitive(ADirView->GetColumnText(Item, Index), Text);
  10385. }
  10386. Index++;
  10387. }
  10388. break;
  10389. }
  10390. if (Matches)
  10391. {
  10392. return Item;
  10393. }
  10394. Item = GetNextFile(Item, Reverse);
  10395. if (Item == CurrentItem)
  10396. {
  10397. return NULL;
  10398. }
  10399. }
  10400. }
  10401. }
  10402. //---------------------------------------------------------------------------
  10403. void __fastcall TCustomScpExplorerForm::DirViewExit(TObject *)
  10404. {
  10405. ResetIncrementalSearch();
  10406. }
  10407. //---------------------------------------------------------------------------
  10408. void __fastcall TCustomScpExplorerForm::CMDialogKey(TWMKeyDown & Message)
  10409. {
  10410. if (Message.CharCode == VK_TAB)
  10411. {
  10412. if (!FIncrementalSearch.IsEmpty())
  10413. {
  10414. TShiftState Shift = KeyDataToShiftState(Message.KeyData);
  10415. bool Reverse = Shift.Contains(ssShift);
  10416. IncrementalSearch(FIncrementalSearch, true, Reverse);
  10417. Message.Result = 1;
  10418. return;
  10419. }
  10420. }
  10421. else if (Message.CharCode == VK_ESCAPE)
  10422. {
  10423. if (!FIncrementalSearch.IsEmpty())
  10424. {
  10425. ResetIncrementalSearch();
  10426. Message.Result = 1;
  10427. return;
  10428. }
  10429. }
  10430. TForm::Dispatch(&Message);
  10431. }
  10432. //---------------------------------------------------------------------------
  10433. void __fastcall TCustomScpExplorerForm::Deactivate()
  10434. {
  10435. TForm::Deactivate();
  10436. // E.g. when switching to an internal editor window
  10437. ResetIncrementalSearch();
  10438. }
  10439. //---------------------------------------------------------------------------
  10440. void __fastcall TCustomScpExplorerForm::ApplicationEventsDeactivate(TObject *)
  10441. {
  10442. // When switching to another application
  10443. ResetIncrementalSearch();
  10444. }
  10445. //---------------------------------------------------------------------------
  10446. void __fastcall TCustomScpExplorerForm::ApplicationEventsModalBegin(TObject *)
  10447. {
  10448. ResetIncrementalSearch();
  10449. }
  10450. //---------------------------------------------------------------------------
  10451. void __fastcall TCustomScpExplorerForm::DirViewChangeFocus(TObject *, TListItem *)
  10452. {
  10453. if (FIncrementalSearching <= 0)
  10454. {
  10455. ResetIncrementalSearch();
  10456. }
  10457. }
  10458. //---------------------------------------------------------------------------
  10459. void __fastcall TCustomScpExplorerForm::RemoteStatusBarMouseDown(TObject *, TMouseButton, TShiftState, int, int)
  10460. {
  10461. CountClicksForWindowPrint(this);
  10462. }
  10463. //---------------------------------------------------------------------------
  10464. void __fastcall TCustomScpExplorerForm::RemoteDirViewResize(TObject *)
  10465. {
  10466. CenterReconnectToolbar();
  10467. }
  10468. //---------------------------------------------------------------------------
  10469. void __fastcall TCustomScpExplorerForm::BrowseFile()
  10470. {
  10471. if (RemoteDirView->ItemFocused != NULL)
  10472. {
  10473. RemoteDirView->ItemFocused->Selected = true;
  10474. }
  10475. }
  10476. //---------------------------------------------------------------------------
  10477. void __fastcall TCustomScpExplorerForm::UpdateQueueFileList()
  10478. {
  10479. if (!FQueueStatusUpdating)
  10480. {
  10481. bool Refresh;
  10482. if ((FQueueStatus != NULL) && QueueFileList->Visible)
  10483. {
  10484. Refresh = FQueueStatus->UpdateFileList(FQueueController->GetFocusedPrimaryItem(), FQueueFileList.get());
  10485. }
  10486. else
  10487. {
  10488. if (FQueueFileList->GetCount() > 0)
  10489. {
  10490. FQueueFileList->Clear();
  10491. Refresh = true;
  10492. }
  10493. }
  10494. int Count = FQueueFileList->GetCount();
  10495. if (QueueFileList->Items->Count != Count)
  10496. {
  10497. DebugAssert(Refresh);
  10498. QueueFileList->Items->Count = Count;
  10499. }
  10500. if (Refresh)
  10501. {
  10502. QueueFileList->Invalidate();
  10503. }
  10504. DebugAssert(QueueFileList->Items->Count == FQueueFileList->GetCount());
  10505. }
  10506. }
  10507. //---------------------------------------------------------------------------
  10508. void __fastcall TCustomScpExplorerForm::QueueFileListData(TObject *, TListItem * Item)
  10509. {
  10510. Item->Caption = FQueueFileList->GetFileName(Item->Index);
  10511. }
  10512. //---------------------------------------------------------------------------
  10513. void __fastcall TCustomScpExplorerForm::QueueView3Change(TObject *, TListItem *, TItemChange)
  10514. {
  10515. // Should be improved, once we do not refresh the list when switching between details of the same batch.
  10516. // See TSynchronizeChecklistDialog::UpdateTimer
  10517. if (QueueFileList->Items->Count > 0)
  10518. {
  10519. QueueFileList->Items->Item[0]->MakeVisible(false);
  10520. }
  10521. UpdateQueueFileList();
  10522. }
  10523. //---------------------------------------------------------------------------
  10524. void __fastcall TCustomScpExplorerForm::QueueLabelGetStatus(TCustomPathLabel *, bool & Active)
  10525. {
  10526. Active = QueueView3->Focused() || QueueFileList->Focused();
  10527. }
  10528. //---------------------------------------------------------------------------
  10529. void __fastcall TCustomScpExplorerForm::QueueFileListEnterExit(TObject *)
  10530. {
  10531. QueueLabelUpdateStatus();
  10532. }
  10533. //---------------------------------------------------------------------------
  10534. void __fastcall TCustomScpExplorerForm::QueueFileListCustomDrawItem(
  10535. TCustomListView * Sender, TListItem * Item, TCustomDrawState, bool & DebugUsedArg(DefaultDraw))
  10536. {
  10537. int State = FQueueFileList->GetState(Item->Index);
  10538. if (State == qfsProcessed)
  10539. {
  10540. Sender->Canvas->Font->Color = clGrayText;
  10541. }
  10542. }
  10543. //---------------------------------------------------------------------------
  10544. void __fastcall TCustomScpExplorerForm::QueueFileListColumnAutoSize()
  10545. {
  10546. // We do not really need whole width, so we always preemptivelly keep free space for the vertical scrollbar
  10547. QueueFileList->Column[0]->Width =
  10548. QueueFileList->ClientWidth - GetSystemMetricsForControl(QueueFileList, SM_CXVSCROLL) - ScaleByTextHeight(QueueFileList, 8);
  10549. }
  10550. //---------------------------------------------------------------------------
  10551. void __fastcall TCustomScpExplorerForm::QueueFileListResize(TObject *)
  10552. {
  10553. QueueFileListColumnAutoSize();
  10554. }
  10555. //---------------------------------------------------------------------------
  10556. void __fastcall TCustomScpExplorerForm::CloseApp()
  10557. {
  10558. // Called from TNonVisualDataModule::ExplorerActionsExecute, which sets busy flag.
  10559. // FormCloseQuery must check busy state, so that the application cannot be closed while busy by Alt+F4 and X button.
  10560. // So we clear the flag here.
  10561. DebugAssert(NonVisualDataModule->Busy);
  10562. NonVisualDataModule->EndBusy();
  10563. try
  10564. {
  10565. Close();
  10566. }
  10567. __finally
  10568. {
  10569. NonVisualDataModule->StartBusy();
  10570. }
  10571. }
  10572. //---------------------------------------------------------------------------
  10573. bool __fastcall TCustomScpExplorerForm::IsActiveTerminal(TTerminal * Terminal)
  10574. {
  10575. return (Terminal != NULL) && Terminal->Active;
  10576. }
  10577. //---------------------------------------------------------------------------
  10578. bool __fastcall TCustomScpExplorerForm::HasManagedSession()
  10579. {
  10580. return (ManagedSession != NULL);
  10581. }
  10582. //---------------------------------------------------------------------------
  10583. bool __fastcall TCustomScpExplorerForm::HasActiveTerminal()
  10584. {
  10585. return IsActiveTerminal(Terminal);
  10586. }
  10587. //---------------------------------------------------------------------------
  10588. void TCustomScpExplorerForm::LocalLocalCopy(
  10589. TFileOperation, TOperationSide, bool DebugUsedArg(OnFocused), bool DebugUsedArg(NoConfirmation),
  10590. bool DebugUsedArg(DragDrop), unsigned int DebugUsedArg(Flags))
  10591. {
  10592. DebugFail();
  10593. }
  10594. //---------------------------------------------------------------------------
  10595. void TCustomScpExplorerForm::LocalLocalCopyCommand(TFileOperation Operation, TOperationSide Side, bool OnFocused, unsigned int Flags)
  10596. {
  10597. LocalLocalCopy(Operation, Side, OnFocused, !WinConfiguration->ConfirmTransferring, false, Flags);
  10598. }
  10599. //---------------------------------------------------------------------------
  10600. void __fastcall TCustomScpExplorerForm::SessionsPageControlResize(TObject *)
  10601. {
  10602. // Changing tab list triggers the OnResize.
  10603. if (!FUpdatingSessionTabs)
  10604. {
  10605. // UpdateTabsCaptionTruncation should be enough
  10606. SessionListChanged(true);
  10607. }
  10608. }
  10609. //---------------------------------------------------------------------------
  10610. UnicodeString TCustomScpExplorerForm::GetSessionPath(TManagedTerminal * ASession, TOperationSide Side)
  10611. {
  10612. if (ASession == ManagedSession)
  10613. {
  10614. UpdateSession(ASession);
  10615. }
  10616. TSessionData * Data = ASession->StateData;
  10617. UnicodeString Result;
  10618. if (Side == osLocal)
  10619. {
  10620. Result = Data->LocalDirectory;
  10621. }
  10622. else if (DebugAlwaysTrue(Side == osOther))
  10623. {
  10624. Result = ASession->LocalBrowser ? Data->OtherLocalDirectory : Data->RemoteDirectory;
  10625. }
  10626. return Result;
  10627. }
  10628. //---------------------------------------------------------------------------
  10629. UnicodeString TCustomScpExplorerForm::GetTabHintDetails(TManagedTerminal * ASession)
  10630. {
  10631. UnicodeString Result;
  10632. if (!ASession->Active)
  10633. {
  10634. Result = LoadStr(STATUS_NOT_CONNECTED2);
  10635. }
  10636. else
  10637. {
  10638. Result = GetSessionPath(ASession, osRemote);
  10639. }
  10640. return Result;
  10641. }
  10642. //---------------------------------------------------------------------------
  10643. UnicodeString TCustomScpExplorerForm::GetNewTabHintDetails()
  10644. {
  10645. return LoadStr(NEW_REMOTE_TAB_HINT);
  10646. }
  10647. //---------------------------------------------------------------------------
  10648. void __fastcall TCustomScpExplorerForm::SessionsPageControlTabHint(TPageControl *, int Index, UnicodeString & Hint)
  10649. {
  10650. if (Index >= 0)
  10651. {
  10652. TThemeTabSheet * TabSheet = SessionsPageControl->Pages[Index];
  10653. TManagedTerminal * TabSession = GetSessionTabSession(TabSheet);
  10654. if (TabSession == NULL)
  10655. {
  10656. UnicodeString NewTabTabCaption = GetNewTabTabCaption();
  10657. if (NewTabTabCaption != TabSheet->Caption)
  10658. {
  10659. Hint = NewTabTabCaption + L"|" + GetNewTabHintDetails();
  10660. }
  10661. }
  10662. else
  10663. {
  10664. TTerminalManager * Manager = TTerminalManager::Instance();
  10665. UnicodeString TabCaption = Manager->GetSessionTitle(TabSession, true);
  10666. Hint = TabCaption + L"|" + GetTabHintDetails(TabSession);
  10667. }
  10668. }
  10669. }
  10670. //---------------------------------------------------------------------------