CustomScpExplorer.cpp 307 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519
  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. //---------------------------------------------------------------------------
  34. #pragma package(smart_init)
  35. #pragma link "CustomDirView"
  36. #pragma link "CustomUnixDirView"
  37. #pragma link "IEListView"
  38. #pragma link "NortonLikeListView"
  39. #pragma link "UnixDirView"
  40. #pragma link "CustomDriveView"
  41. #pragma link "UnixDriveView"
  42. #pragma link "CustomDriveView"
  43. #pragma link "TB2Dock"
  44. #pragma link "TBXStatusBars"
  45. #pragma link "TB2Item"
  46. #pragma link "TB2Toolbar"
  47. #pragma link "ThemePageControl"
  48. #pragma link "PathLabel"
  49. #ifndef NO_RESOURCES
  50. #pragma resource "*.dfm"
  51. #endif
  52. //---------------------------------------------------------------------------
  53. #define SAVE_SELECTION(DIRVIEW) \
  54. UnicodeString FocusFile = L""; \
  55. UnicodeString LastFocusedFile = L""; \
  56. if (DIRVIEW->ItemFocused) LastFocusedFile = DIRVIEW->ItemFocused->Caption; \
  57. { TListItem * ClosestUnselected = DIRVIEW->ClosestUnselected(DIRVIEW->ItemFocused); \
  58. if (ClosestUnselected) FocusFile = ClosestUnselected->Caption; }
  59. #define RESTORE_SELECTION(DIRVIEW) \
  60. if (!LastFocusedFile.IsEmpty() && \
  61. (!DIRVIEW->ItemFocused || (DIRVIEW->ItemFocused->Caption != LastFocusedFile))) \
  62. { \
  63. TListItem *ItemToSelect = DIRVIEW->FindFileItem(FocusFile); \
  64. if (ItemToSelect) \
  65. { \
  66. DIRVIEW->ItemFocused = ItemToSelect; \
  67. DIRVIEW->ItemFocused->MakeVisible(False); \
  68. } \
  69. }
  70. //---------------------------------------------------------------------------
  71. #define WM_COMPONENT_HIDE (WM_WINSCP_USER + 4)
  72. static const int SessionPanelCount = 4;
  73. //---------------------------------------------------------------------------
  74. class TMutexGuard
  75. {
  76. public:
  77. TMutexGuard(HANDLE AMutex, int Message = MUTEX_RELEASE_TIMEOUT,
  78. int Timeout = 5000)
  79. {
  80. FMutex = NULL;
  81. unsigned long WaitResult = WaitForSingleObject(AMutex, Timeout);
  82. if (WaitResult == WAIT_TIMEOUT)
  83. {
  84. throw Exception(LoadStr(MUTEX_RELEASE_TIMEOUT));
  85. }
  86. else
  87. {
  88. FMutex = AMutex;
  89. }
  90. }
  91. ~TMutexGuard()
  92. {
  93. if (FMutex != NULL)
  94. {
  95. ReleaseMutex(FMutex);
  96. }
  97. }
  98. private:
  99. HANDLE FMutex;
  100. };
  101. //---------------------------------------------------------------------------
  102. class TWindowLock
  103. {
  104. public:
  105. TWindowLock(TCustomScpExplorerForm * Form) :
  106. FForm(Form)
  107. {
  108. FForm->LockWindow();
  109. }
  110. ~TWindowLock()
  111. {
  112. FForm->UnlockWindow();
  113. }
  114. private:
  115. TCustomScpExplorerForm * FForm;
  116. };
  117. //---------------------------------------------------------------------------
  118. struct TTransferOperationParam
  119. {
  120. TTransferOperationParam();
  121. UnicodeString TargetDirectory;
  122. bool Temp;
  123. bool DragDrop;
  124. int Options;
  125. TAutoSwitch Queue;
  126. };
  127. //---------------------------------------------------------------------------
  128. TTransferOperationParam::TTransferOperationParam()
  129. {
  130. Temp = false;
  131. DragDrop = false;
  132. Options = 0;
  133. Queue = asAuto;
  134. }
  135. //---------------------------------------------------------------------------
  136. class TTransferPresetNoteData : public TObject
  137. {
  138. public:
  139. UnicodeString Message;
  140. };
  141. //---------------------------------------------------------------------------
  142. class TTerminalNoteData : public TObject
  143. {
  144. public:
  145. TTerminal * Terminal;
  146. };
  147. //---------------------------------------------------------------------------
  148. __fastcall TCustomScpExplorerForm::TCustomScpExplorerForm(TComponent* Owner):
  149. FFormRestored(false),
  150. TForm(Owner)
  151. {
  152. FCurrentSide = osRemote;
  153. FEverShown = false;
  154. FDocks = new TList();
  155. RestoreParams();
  156. ConfigurationChanged();
  157. RemoteDirView->Invalidate();
  158. DebugAssert(NonVisualDataModule && !NonVisualDataModule->ScpExplorer);
  159. NonVisualDataModule->ScpExplorer = this;
  160. FAutoOperation = false;
  161. FOnFileOperationFinished = NULL;
  162. FForceExecution = false;
  163. FIgnoreNextDialogChar = 0;
  164. FErrorList = NULL;
  165. FSynchronizeProgressForm = NULL;
  166. FProgressForm = NULL;
  167. FRefreshLocalDirectory = false;
  168. FRefreshRemoteDirectory = false;
  169. FDDMoveSlipped = false;
  170. FDDExtMapFile = NULL;
  171. // CreateMutexW keeps failing with ERROR_NOACCESS
  172. FDDExtMutex = CreateMutexA(NULL, false, AnsiString(DRAG_EXT_MUTEX).c_str());
  173. DebugAssert(FDDExtMutex != NULL);
  174. FDDTargetControl = NULL;
  175. FDelayedDeletionTimer = NULL;
  176. FDelayedDeletionList = new TStringList();
  177. FDDFileList = NULL;
  178. FPendingTempSpaceWarn = false;
  179. FCapturedLog = NULL;
  180. FDragDropOperation = false;
  181. memset(&FHistoryMenu, 0, sizeof(FHistoryMenu));
  182. FAllowTransferPresetAutoSelect = true;
  183. FCopyParamDefault = L"";
  184. FSynchronizeController = NULL;
  185. FPendingQueueActionItem = NULL;
  186. FLockLevel = 0;
  187. FLockSuspendLevel = 0;
  188. FDisabledOnLockSuspend = false;
  189. FAlternativeDelete = false;
  190. FTrayIcon = new ::TTrayIcon(0);
  191. FTrayIcon->OnClick = TrayIconClick;
  192. FMaxQueueLength = 0;
  193. FLastContextPopupScreenPoint = TPoint(-1, -1);
  194. FTransferResumeList = NULL;
  195. FMoveToQueue = false;
  196. FStandaloneEditing = false;
  197. FOnFeedSynchronizeError = NULL;
  198. FNeedSession = false;
  199. FDoNotIdleCurrentTerminal = false;
  200. FEditorManager = new TEditorManager();
  201. FEditorManager->OnFileChange = ExecutedFileChanged;
  202. FEditorManager->OnFileReload = ExecutedFileReload;
  203. FEditorManager->OnFileEarlyClosed = ExecutedFileEarlyClosed;
  204. FEditorManager->OnFileUploadComplete = ExecutedFileUploadComplete;
  205. FLocalEditors = new TList();
  206. FQueueStatus = NULL;
  207. FQueueStatusSection = new TCriticalSection();
  208. FQueueStatusInvalidated = false;
  209. FQueueItemInvalidated = 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))));
  232. SetSubmenu(NonVisualDataModule->ColorMenuItem);
  233. UseDesktopFont(SessionsPageControl);
  234. UpdateSessionsPageControlHeight();
  235. UseDesktopFont(RemoteDirView);
  236. UseDesktopFont(RemoteDriveView);
  237. UseDesktopFont(QueueView3);
  238. UseDesktopFont(QueueLabel);
  239. UseDesktopFont(RemoteStatusBar);
  240. reinterpret_cast<TLabel*>(QueueSplitter)->OnDblClick = QueueSplitterDblClick;
  241. QueueSplitter->ShowHint = true;
  242. RemotePanelSplitter->ShowHint = true;
  243. UpdateImages();
  244. FCustomCommandMenu = CreateTBXPopupMenu(this);
  245. FCustomCommandLocalFileList = NULL;
  246. FCustomCommandRemoteFileList = NULL;
  247. FSessionColors = new TPngImageList(this);
  248. FSessionColors->ColorDepth = cd32Bit;
  249. AddFixedSessionImages();
  250. SessionsPageControl->Images = FSessionColors;
  251. QueueLabel->FocusControl = QueueView3;
  252. UpdateQueueLabel();
  253. FRemoteDirViewWasFocused = true;
  254. CreateHiddenWindow();
  255. StartUpdates();
  256. }
  257. //---------------------------------------------------------------------------
  258. __fastcall TCustomScpExplorerForm::~TCustomScpExplorerForm()
  259. {
  260. // this has to be one of the very first things to do
  261. StopUpdateThread();
  262. SessionsPageControl->Images = NULL;
  263. SAFE_DESTROY(FSessionColors);
  264. SAFE_DESTROY(FSessionsDragDropFilesEx);
  265. delete FCustomCommandLocalFileList;
  266. delete FCustomCommandRemoteFileList;
  267. delete FCustomCommandMenu;
  268. delete FTrayIcon;
  269. FTrayIcon = NULL;
  270. FEditorManager->CloseInternalEditors(ForceCloseInternalEditor);
  271. delete FEditorManager;
  272. ForceCloseLocalEditors();
  273. delete FLocalEditors;
  274. HideFileFindDialog();
  275. if (FDelayedDeletionTimer)
  276. {
  277. DoDelayedDeletion(NULL);
  278. SAFE_DESTROY(FDelayedDeletionTimer);
  279. }
  280. SAFE_DESTROY(FDelayedDeletionList);
  281. // sometimes we do not get DDEnd so the list is not released
  282. SAFE_DESTROY(FDDFileList);
  283. DebugAssert(FSynchronizeController == NULL);
  284. CloseHandle(FDDExtMutex);
  285. FDDExtMutex = NULL;
  286. FreeLibrary(FOle32Library);
  287. FOle32Library = NULL;
  288. FDragMoveCursor = NULL;
  289. DebugAssert(!FErrorList);
  290. if (FEverShown)
  291. {
  292. // when window is never shown (like when running command-line operation),
  293. // particularly window site is not restored correctly (BoundsRect value set
  294. // in RestoreForm gets lost during handle allocation), so we do not want
  295. // it to be stored
  296. StoreParams();
  297. }
  298. Terminal = NULL;
  299. Queue = NULL;
  300. DebugAssert(NonVisualDataModule && (NonVisualDataModule->ScpExplorer == this));
  301. NonVisualDataModule->ScpExplorer = NULL;
  302. delete FQueueController;
  303. FQueueController = NULL;
  304. delete FQueueStatusSection;
  305. FQueueStatusSection = NULL;
  306. delete FQueueStatus;
  307. FQueueStatus = NULL;
  308. delete FUserActionTimer;
  309. FUserActionTimer = NULL;
  310. delete FNoteTimer;
  311. delete FNotes;
  312. delete FNoteData;
  313. SAFE_DESTROY(FDocks);
  314. SAFE_DESTROY(FHistoryMenu[0][0]);
  315. SAFE_DESTROY(FHistoryMenu[0][1]);
  316. SAFE_DESTROY(FHistoryMenu[1][0]);
  317. SAFE_DESTROY(FHistoryMenu[1][1]);
  318. if (FHiddenWindow != NULL)
  319. {
  320. DestroyWindow(FHiddenWindow);
  321. FHiddenWindow = NULL;
  322. }
  323. }
  324. //---------------------------------------------------------------------------
  325. void __fastcall TCustomScpExplorerForm::RefreshPanel(const UnicodeString & Session, const UnicodeString & Path)
  326. {
  327. std::unique_ptr<TSessionData> Data;
  328. if (!Session.IsEmpty())
  329. {
  330. bool DefaultsOnly;
  331. Data.reset(StoredSessions->ParseUrl(Session, NULL, DefaultsOnly));
  332. }
  333. TTerminalManager * Manager = TTerminalManager::Instance();
  334. for (int Index = 0; Index < Manager->Count; Index++)
  335. {
  336. TTerminal * Terminal = Manager->Terminals[Index];
  337. if (Session.IsEmpty() ||
  338. Terminal->SessionData->IsSameSite(Data.get()))
  339. {
  340. if (Path.IsEmpty())
  341. {
  342. Terminal->ClearCaches();
  343. }
  344. else
  345. {
  346. Terminal->DirectoryModified(Path, true);
  347. }
  348. }
  349. }
  350. // We should flag a pending refresh for the background terminals or busy foreground terminals
  351. if ((Terminal != NULL) && Terminal->Active &&
  352. CanCommandLineFromAnotherInstance() &&
  353. (Session.IsEmpty() || Terminal->SessionData->IsSameSite(Data.get())) &&
  354. (Path.IsEmpty() || UnixIsChildPath(Path, Terminal->CurrentDirectory)))
  355. {
  356. Terminal->ReloadDirectory();
  357. }
  358. }
  359. //---------------------------------------------------------------------------
  360. void __fastcall TCustomScpExplorerForm::WMCopyData(TMessage & Message)
  361. {
  362. PCOPYDATASTRUCT CopyData = reinterpret_cast<PCOPYDATASTRUCT>(Message.LParam);
  363. size_t MessageSize = sizeof(TCopyDataMessage);
  364. bool Result = DebugAlwaysTrue(CopyData->cbData == MessageSize);
  365. if (Result)
  366. {
  367. const TCopyDataMessage & Message = *reinterpret_cast<const TCopyDataMessage *>(CopyData->lpData);
  368. Result = (Message.Version == TCopyDataMessage::Version1);
  369. if (Result)
  370. {
  371. switch (Message.Command)
  372. {
  373. case TCopyDataMessage::CommandCanCommandLine:
  374. Result = CanCommandLineFromAnotherInstance();
  375. break;
  376. case TCopyDataMessage::CommandCommandLine:
  377. {
  378. UnicodeString CommandLine(Message.CommandLine);
  379. Result = CommandLineFromAnotherInstance(CommandLine);
  380. }
  381. break;
  382. case TCopyDataMessage::RefreshPanel:
  383. RefreshPanel(Message.Refresh.Session, Message.Refresh.Path);
  384. break;
  385. case TCopyDataMessage::MainWindowCheck:
  386. Result = true;
  387. break;
  388. default:
  389. Result = false;
  390. break;
  391. }
  392. }
  393. }
  394. Message.Result = Result ? 1 : 0;
  395. }
  396. //---------------------------------------------------------------------------
  397. void __fastcall TCustomScpExplorerForm::CreateHiddenWindow()
  398. {
  399. WNDCLASS WindowClass = {0};
  400. WindowClass.lpfnWndProc = DefWindowProc;
  401. WindowClass.hInstance = HInstance;
  402. WindowClass.lpszClassName = HIDDEN_WINDOW_NAME;
  403. FHiddenWindow = NULL;
  404. if (RegisterClass(&WindowClass))
  405. {
  406. FHiddenWindow = CreateWindow(HIDDEN_WINDOW_NAME, L"",
  407. WS_POPUP, 0, 0, 0, 0, 0, 0, HInstance, NULL);
  408. }
  409. }
  410. //---------------------------------------------------------------------------
  411. bool __fastcall TCustomScpExplorerForm::CanConsole()
  412. {
  413. return (Terminal != NULL) && (Terminal->IsCapable[fcAnyCommand] || Terminal->IsCapable[fcSecondaryShell]);
  414. }
  415. //---------------------------------------------------------------------------
  416. bool __fastcall TCustomScpExplorerForm::CanCommandLineFromAnotherInstance()
  417. {
  418. bool Result = !NonVisualDataModule->Busy;
  419. return Result;
  420. }
  421. //---------------------------------------------------------------------------
  422. bool __fastcall TCustomScpExplorerForm::CommandLineFromAnotherInstance(
  423. const UnicodeString & CommandLine)
  424. {
  425. TProgramParams Params(CommandLine);
  426. bool Result = CanCommandLineFromAnotherInstance() && DebugAlwaysTrue(Params.ParamCount > 0);
  427. if (Result)
  428. {
  429. NonVisualDataModule->StartBusy();
  430. try
  431. {
  432. // this action is initiated from another process,
  433. // so it's likely that our window is not visible,
  434. // and user won't see what is going on
  435. Application->BringToFront();
  436. // reload sessions as we may be asked to open a session
  437. // just stored by another instance
  438. StoredSessions->Load();
  439. UnicodeString SessionName = Params.Param[1];
  440. std::unique_ptr<TObjectList> DataList(new TObjectList());
  441. UnicodeString DownloadFile; // unused
  442. try
  443. {
  444. GetLoginData(SessionName, &Params, DataList.get(), DownloadFile, true, this);
  445. }
  446. catch (EAbort &)
  447. {
  448. // Happens when opening session in PuTTY and in other situations,
  449. // in which we do not want to fail WM_COPYDATA, as that would cause master instance
  450. // to process the command-line on its own (or to pass it to yet another instance)
  451. }
  452. if (DataList->Count > 0)
  453. {
  454. TTerminalManager * Manager = TTerminalManager::Instance();
  455. Manager->ActiveTerminal = Manager->NewTerminals(DataList.get());
  456. }
  457. }
  458. __finally
  459. {
  460. NonVisualDataModule->EndBusy();
  461. }
  462. }
  463. return Result;
  464. }
  465. //---------------------------------------------------------------------------
  466. void __fastcall TCustomScpExplorerForm::SetTerminal(TTerminal * value)
  467. {
  468. if (FTerminal != value)
  469. {
  470. TerminalChanging();
  471. FTerminal = value;
  472. bool PrevAllowTransferPresetAutoSelect = FAllowTransferPresetAutoSelect;
  473. FAllowTransferPresetAutoSelect = false;
  474. try
  475. {
  476. TerminalChanged();
  477. }
  478. __finally
  479. {
  480. FAllowTransferPresetAutoSelect = PrevAllowTransferPresetAutoSelect;
  481. }
  482. if (Terminal != NULL)
  483. {
  484. TransferPresetAutoSelect();
  485. }
  486. }
  487. }
  488. //---------------------------------------------------------------------------
  489. void __fastcall TCustomScpExplorerForm::TerminalChanging()
  490. {
  491. if (FTerminal != NULL)
  492. {
  493. UpdateTerminal(Terminal);
  494. }
  495. }
  496. //---------------------------------------------------------------------------
  497. void __fastcall TCustomScpExplorerForm::TerminalChanged()
  498. {
  499. RemoteDirView->Terminal = Terminal;
  500. NonVisualDataModule->ResetQueueOnceEmptyOperation();
  501. TManagedTerminal * ManagedTerminal = NULL;
  502. if (Terminal != NULL)
  503. {
  504. if (Terminal->Active)
  505. {
  506. Terminal->RefreshDirectory();
  507. }
  508. ManagedTerminal = dynamic_cast<TManagedTerminal *>(Terminal);
  509. DebugAssert(ManagedTerminal != NULL);
  510. if (WinConfiguration->PreservePanelState)
  511. {
  512. if (ManagedTerminal->RemoteExplorerState != NULL)
  513. {
  514. RemoteDirView->RestoreState(ManagedTerminal->RemoteExplorerState);
  515. }
  516. else
  517. {
  518. RemoteDirView->ClearState();
  519. }
  520. }
  521. InitStatusBar();
  522. }
  523. DoTerminalListChanged(false);
  524. if (ManagedTerminal != NULL)
  525. {
  526. // this has to be set only after the tab is switched from DoTerminalListChanged,
  527. // otherwise we are changing color of wrong tab
  528. SessionColor = (TColor)ManagedTerminal->StateData->Color;
  529. }
  530. UpdateTransferList();
  531. // Update panels Enable state before refreshing the labels
  532. UpdateControls();
  533. }
  534. //---------------------------------------------------------------------------
  535. void __fastcall TCustomScpExplorerForm::SetQueue(TTerminalQueue * value)
  536. {
  537. if (Queue != value)
  538. {
  539. if (FQueue != NULL)
  540. {
  541. FQueue->OnListUpdate = NULL;
  542. FQueue->OnQueueItemUpdate = NULL;
  543. }
  544. FQueue = value;
  545. if (FQueue != NULL)
  546. {
  547. DebugAssert(FQueue->OnListUpdate == NULL);
  548. FQueue->OnListUpdate = QueueListUpdate;
  549. DebugAssert(FQueue->OnQueueItemUpdate == NULL);
  550. FQueue->OnQueueItemUpdate = QueueItemUpdate;
  551. }
  552. QueueChanged();
  553. }
  554. }
  555. //---------------------------------------------------------------------------
  556. void __fastcall TCustomScpExplorerForm::QueueView3Deletion(TObject * /*Sender*/,
  557. TListItem * Item)
  558. {
  559. if (FQueueActedItem == Item)
  560. {
  561. FQueueActedItem = NULL;
  562. if ((QueueView3->PopupMenu != NULL) &&
  563. (QueueView3->PopupMenu->PopupComponent == QueueView3))
  564. {
  565. // rather "trick", suggested by Jordan on jrsoftware.toolbar2000
  566. ReleaseCapture();
  567. }
  568. }
  569. if (Item->Data == FPendingQueueActionItem)
  570. {
  571. FPendingQueueActionItem = NULL;
  572. }
  573. }
  574. //---------------------------------------------------------------------------
  575. void __fastcall TCustomScpExplorerForm::UpdateQueueStatus(bool QueueChanging)
  576. {
  577. {
  578. TGuard Guard(FQueueStatusSection);
  579. FQueueStatusInvalidated = false;
  580. if (FQueue != NULL)
  581. {
  582. FQueueStatus = FQueue->CreateStatus(FQueueStatus);
  583. }
  584. }
  585. if ((FQueueStatus != NULL) && (FQueueStatus->Count > FMaxQueueLength))
  586. {
  587. FMaxQueueLength = FQueueStatus->Count;
  588. Configuration->Usage->SetMax(L"MaxQueueLength", FMaxQueueLength);
  589. }
  590. FQueueController->UpdateQueueStatus(FQueueStatus);
  591. SetQueueProgress();
  592. UpdateQueueView();
  593. bool IsEmpty = (FQueueStatus == NULL) || (FQueueStatus->Count == 0);
  594. if (IsEmpty && (Terminal != NULL))
  595. {
  596. TOnceDoneOperation OnceDoneOperation =
  597. NonVisualDataModule->CurrentQueueOnceEmptyOperation();
  598. NonVisualDataModule->ResetQueueOnceEmptyOperation();
  599. if ((FQueue != NULL) && !WinConfiguration->EnableQueueByDefault && !QueueChanging)
  600. {
  601. FQueue->Enabled = false;
  602. }
  603. if ((OnceDoneOperation != odoIdle) && !NonVisualDataModule->Busy)
  604. {
  605. Terminal->CloseOnCompletion(OnceDoneOperation, LoadStr(CLOSED_ON_QUEUE_EMPTY));
  606. }
  607. }
  608. }
  609. //---------------------------------------------------------------------------
  610. UnicodeString __fastcall TCustomScpExplorerForm::GetQueueProgressTitle()
  611. {
  612. UnicodeString Result;
  613. if (FQueueStatus != NULL)
  614. {
  615. if (FQueueStatus->IsOnlyOneActiveAndNoPending())
  616. {
  617. TFileOperationProgressType * ProgressData =
  618. FQueueStatus->Items[FQueueStatus->DoneCount]->ProgressData;
  619. if ((ProgressData != NULL) && ProgressData->InProgress)
  620. {
  621. Result = TProgressForm::ProgressStr(ProgressData);
  622. }
  623. }
  624. else if (FQueueStatus->ActiveAndPendingPrimaryCount > 1)
  625. {
  626. Result = FMTLOAD(PROGRESS_IN_QUEUE, (FQueueStatus->ActiveAndPendingPrimaryCount));
  627. }
  628. }
  629. return Result;
  630. }
  631. //---------------------------------------------------------------------------
  632. void __fastcall TCustomScpExplorerForm::UpdateQueueView()
  633. {
  634. ComponentVisible[fcQueueView] =
  635. (WinConfiguration->QueueView.Show == qvShow) ||
  636. ((WinConfiguration->QueueView.Show == qvHideWhenEmpty) &&
  637. (FQueueStatus != NULL) && (FQueueStatus->Count > 0));
  638. }
  639. //---------------------------------------------------------------------------
  640. void __fastcall TCustomScpExplorerForm::QueueChanged()
  641. {
  642. if (FQueueStatus != NULL)
  643. {
  644. delete FQueueStatus;
  645. FQueueStatus = NULL;
  646. }
  647. UpdateQueueStatus(true);
  648. }
  649. //---------------------------------------------------------------------------
  650. void __fastcall TCustomScpExplorerForm::QueueListUpdate(TTerminalQueue * Queue)
  651. {
  652. if (FQueue == Queue)
  653. {
  654. FQueueStatusInvalidated = true;
  655. }
  656. }
  657. //---------------------------------------------------------------------------
  658. void __fastcall TCustomScpExplorerForm::QueueItemUpdate(TTerminalQueue * Queue,
  659. TQueueItem * Item)
  660. {
  661. if (FQueue == Queue)
  662. {
  663. TGuard Guard(FQueueStatusSection);
  664. // this may be running in parallel with QueueChanged
  665. if (FQueueStatus != NULL)
  666. {
  667. TQueueItemProxy * QueueItem = FQueueStatus->FindByQueueItem(Item);
  668. if ((Item->Status == TQueueItem::qsDone) && (Terminal != NULL))
  669. {
  670. FRefreshLocalDirectory = (QueueItem == NULL) ||
  671. (!QueueItem->Info->ModifiedLocal.IsEmpty());
  672. FRefreshRemoteDirectory = (QueueItem == NULL) ||
  673. (!QueueItem->Info->ModifiedRemote.IsEmpty());
  674. }
  675. if (QueueItem != NULL)
  676. {
  677. QueueItem->UserData = (void*)true;
  678. FQueueItemInvalidated = true;
  679. }
  680. }
  681. }
  682. }
  683. //---------------------------------------------------------------------------
  684. bool __fastcall TCustomScpExplorerForm::IsQueueAutoPopup()
  685. {
  686. // during standalone editing, we have no way to see/control queue,
  687. // so we have to always popup prompts automatically
  688. return FStandaloneEditing || GUIConfiguration->QueueAutoPopup;
  689. }
  690. //---------------------------------------------------------------------------
  691. void __fastcall TCustomScpExplorerForm::RefreshQueueItems()
  692. {
  693. if (FQueueStatus != NULL)
  694. {
  695. bool QueueAutoPopup = IsQueueAutoPopup();
  696. bool NeedRefresh = FQueueController->NeedRefresh();
  697. bool Refresh = FQueueItemInvalidated || NeedRefresh;
  698. FQueueItemInvalidated = false;
  699. int Limit = Refresh ? FQueueStatus->Count : FQueueStatus->DoneAndActiveCount;
  700. bool Updated = false;
  701. TQueueItemProxy * QueueItem;
  702. bool WasUserAction;
  703. for (int Index = 0; Index < Limit; Index++)
  704. {
  705. bool Update = false;
  706. QueueItem = FQueueStatus->Items[Index];
  707. WasUserAction = TQueueItem::IsUserActionStatus(QueueItem->Status);
  708. if (!NonVisualDataModule->Busy && QueueAutoPopup && WasUserAction &&
  709. (FPendingQueueActionItem == NULL))
  710. {
  711. FPendingQueueActionItem = QueueItem;
  712. FUserActionTimer->Enabled = true;
  713. }
  714. if ((bool)QueueItem->UserData)
  715. {
  716. QueueItem->UserData = (void*)false;
  717. QueueItem->Update();
  718. Updated = true;
  719. Update = true;
  720. }
  721. else if (FQueueController->QueueItemNeedsFrequentRefresh(QueueItem) || NeedRefresh)
  722. {
  723. Update = true;
  724. }
  725. if (Update)
  726. {
  727. FQueueController->RefreshQueueItem(QueueItem);
  728. }
  729. }
  730. if (Updated)
  731. {
  732. NonVisualDataModule->UpdateNonVisibleActions();
  733. SetQueueProgress();
  734. }
  735. }
  736. }
  737. //---------------------------------------------------------------------------
  738. void __fastcall TCustomScpExplorerForm::SetTaskbarListProgressState(TBPFLAG Flags)
  739. {
  740. FTaskbarList->SetProgressState(GetMainForm()->Handle, Flags);
  741. }
  742. //---------------------------------------------------------------------------
  743. void __fastcall TCustomScpExplorerForm::SetTaskbarListProgressValue(TFileOperationProgressType * ProgressData)
  744. {
  745. if (ProgressData->Operation != foCalculateSize)
  746. {
  747. // implies TBPF_NORMAL
  748. FTaskbarList->SetProgressValue(GetMainForm()->Handle, ProgressData->OverallProgress(), 100);
  749. }
  750. else
  751. {
  752. SetTaskbarListProgressState(TBPF_INDETERMINATE);
  753. }
  754. }
  755. //---------------------------------------------------------------------------
  756. void __fastcall TCustomScpExplorerForm::SetQueueProgress()
  757. {
  758. TTerminalManager::Instance()->QueueStatusUpdated();
  759. if ((FTaskbarList != NULL) && (FProgressForm == NULL))
  760. {
  761. if ((FQueueStatus != NULL) && (FQueueStatus->ActiveAndPendingPrimaryCount > 0))
  762. {
  763. if (FQueueStatus->IsOnlyOneActiveAndNoPending())
  764. {
  765. TFileOperationProgressType * ProgressData = NULL;
  766. if (FQueueStatus->Items[FQueueStatus->DoneCount] != NULL)
  767. {
  768. ProgressData = FQueueStatus->Items[FQueueStatus->DoneCount]->ProgressData;
  769. }
  770. if ((ProgressData != NULL) &&
  771. ProgressData->InProgress)
  772. {
  773. SetTaskbarListProgressValue(ProgressData);
  774. }
  775. else
  776. {
  777. SetTaskbarListProgressState(TBPF_NOPROGRESS);
  778. }
  779. }
  780. else
  781. {
  782. SetTaskbarListProgressState(TBPF_INDETERMINATE);
  783. }
  784. }
  785. else
  786. {
  787. SetTaskbarListProgressState(TBPF_NOPROGRESS);
  788. }
  789. }
  790. UpdateQueueLabel();
  791. }
  792. //---------------------------------------------------------------------------
  793. void __fastcall TCustomScpExplorerForm::UpdateQueueLabel()
  794. {
  795. UnicodeString Caption = LoadStr(QUEUE_CAPTION);
  796. if (FQueueStatus != NULL)
  797. {
  798. int ActiveAndPendingPrimaryCount = FQueueStatus->ActiveAndPendingPrimaryCount;
  799. if (ActiveAndPendingPrimaryCount > 0)
  800. {
  801. Caption = FORMAT("%s (%d)", (Caption, ActiveAndPendingPrimaryCount));
  802. }
  803. }
  804. QueueLabel->Caption = Caption;
  805. }
  806. //---------------------------------------------------------------------------
  807. void __fastcall TCustomScpExplorerForm::UpdateTransferList()
  808. {
  809. TTBXStringList * TransferList = dynamic_cast<TTBXStringList*>(
  810. static_cast<TComponent*>(GetComponent(fcTransferList)));
  811. TTBXDropDownItem * TransferDropDown = dynamic_cast<TTBXDropDownItem*>(
  812. static_cast<TComponent*>(GetComponent(fcTransferDropDown)));
  813. TransferList->Strings->BeginUpdate();
  814. try
  815. {
  816. TransferList->Strings->Assign(GUIConfiguration->CopyParamList->NameList);
  817. TransferList->Strings->Insert(0, StripHotkey(LoadStr(COPY_PARAM_DEFAULT)));
  818. TransferList->ItemIndex = GUIConfiguration->CopyParamIndex + 1;
  819. if (FTransferDropDownHint.IsEmpty())
  820. {
  821. FTransferDropDownHint = TransferDropDown->Hint;
  822. }
  823. // this way we get name for "default" settings (COPY_PARAM_DEFAULT)
  824. UnicodeString Name = TransferList->Strings->Strings[TransferList->ItemIndex];
  825. TransferDropDown->Text = StripHotkey(Name);
  826. TransferDropDown->Hint = FORMAT(L"%s|%s:\n%s",
  827. (FTransferDropDownHint, StripHotkey(Name),
  828. GUIConfiguration->CurrentCopyParam.GetInfoStr(L"; ",
  829. FLAGMASK(Terminal != NULL, Terminal->UsableCopyParamAttrs(0).General))));
  830. // update the label, otherwise when it is updated only on the first draw
  831. // of the list, it is drawn "bold" for some reason
  832. FTransferListHoverIndex = TransferList->ItemIndex;
  833. UpdateTransferLabel();
  834. }
  835. __finally
  836. {
  837. TransferList->Strings->EndUpdate();
  838. }
  839. }
  840. //---------------------------------------------------------------------------
  841. void __fastcall TCustomScpExplorerForm::UpdateCustomCommandsToolbar()
  842. {
  843. TTBXToolbar * Toolbar = dynamic_cast<TTBXToolbar *>(GetComponent(fcCustomCommandsBand));
  844. DebugAssert(Toolbar != NULL);
  845. NonVisualDataModule->UpdateCustomCommandsToolbar(Toolbar);
  846. }
  847. //---------------------------------------------------------------------------
  848. void __fastcall TCustomScpExplorerForm::UpdateActions()
  849. {
  850. TForm::UpdateActions();
  851. if (ComponentVisible[fcCustomCommandsBand])
  852. {
  853. UpdateCustomCommandsToolbar();
  854. }
  855. }
  856. //---------------------------------------------------------------------------
  857. void __fastcall TCustomScpExplorerForm::UpdateSessionsPageControlHeight()
  858. {
  859. SessionsPageControl->Height = SessionsPageControl->GetTabsHeight();
  860. }
  861. //---------------------------------------------------------------------------
  862. void __fastcall TCustomScpExplorerForm::ConfigurationChanged()
  863. {
  864. DebugAssert(Configuration && RemoteDirView);
  865. RemoteDirView->DDAllowMove = WinConfiguration->DDAllowMoveInit;
  866. RemoteDirView->DimmHiddenFiles = WinConfiguration->DimmHiddenFiles;
  867. RemoteDirView->ShowHiddenFiles = WinConfiguration->ShowHiddenFiles;
  868. RemoteDirView->FormatSizeBytes = WinConfiguration->FormatSizeBytes;
  869. RemoteDirView->ShowInaccesibleDirectories = WinConfiguration->ShowInaccesibleDirectories;
  870. RemoteDirView->NaturalOrderNumericalSorting = WinConfiguration->NaturalOrderNumericalSorting;
  871. if (RemoteDirView->RowSelect != WinConfiguration->FullRowSelect)
  872. {
  873. RemoteDirView->RowSelect = WinConfiguration->FullRowSelect;
  874. // selection is not redrawn automatically when RowSelect changes
  875. RemoteDirView->Invalidate();
  876. }
  877. RemoteDriveView->DDAllowMove = WinConfiguration->DDAllowMoveInit;
  878. RemoteDriveView->DimmHiddenDirs = WinConfiguration->DimmHiddenFiles;
  879. RemoteDriveView->ShowHiddenDirs = WinConfiguration->ShowHiddenFiles;
  880. RemoteDriveView->ShowInaccesibleDirectories = WinConfiguration->ShowInaccesibleDirectories;
  881. UpdateSessionsPageControlHeight();
  882. SetDockAllowDrag(!WinConfiguration->LockToolbars);
  883. UpdateToolbarDisplayMode();
  884. UpdateTransferList();
  885. if (Terminal != NULL)
  886. {
  887. TransferPresetAutoSelect();
  888. }
  889. UpdateQueueView();
  890. UpdateControls();
  891. // this can be called even before constuctor finishes.
  892. if (FEditorManager != NULL)
  893. {
  894. FEditorManager->ProcessFiles(FileConfigurationChanged, NULL);
  895. }
  896. // this can be called even before constuctor finishes.
  897. if (FLocalEditors != NULL)
  898. {
  899. for (int Index = 0; Index < FLocalEditors->Count; Index++)
  900. {
  901. ReconfigureEditorForm(static_cast<TForm *>(FLocalEditors->Items[Index]));
  902. }
  903. }
  904. if (ComponentVisible[fcCustomCommandsBand])
  905. {
  906. UpdateCustomCommandsToolbar();
  907. }
  908. // show only when keeping queue items forever,
  909. // otherwise, its enough to have in in the context menu
  910. QueueDeleteAllDoneQueueToolbarItem->Visible =
  911. WinConfiguration->QueueKeepDoneItems && (WinConfiguration->QueueKeepDoneItemsFor < 0);
  912. }
  913. //---------------------------------------------------------------------------
  914. void __fastcall TCustomScpExplorerForm::FileConfigurationChanged(
  915. const UnicodeString FileName, TEditedFileData * /*Data*/, TObject * Token,
  916. void * /*Arg*/)
  917. {
  918. if (Token != NULL)
  919. {
  920. TForm * Editor = dynamic_cast<TForm*>(Token);
  921. DebugAssert(Editor != NULL);
  922. ReconfigureEditorForm(Editor);
  923. }
  924. }
  925. //---------------------------------------------------------------------------
  926. void __fastcall TCustomScpExplorerForm::EnableDDTransferConfirmation(TObject * /*Sender*/)
  927. {
  928. WinConfiguration->DDTransferConfirmation = asOn;
  929. }
  930. //---------------------------------------------------------------------------
  931. void __fastcall TCustomScpExplorerForm::CopyParamDialogAfter(
  932. TTransferDirection /*Direction*/, bool /*Temp*/, const UnicodeString & /*TargetDirectory*/)
  933. {
  934. // noop
  935. }
  936. //---------------------------------------------------------------------------
  937. bool __fastcall TCustomScpExplorerForm::CopyParamDialog(
  938. TTransferDirection Direction, TTransferType Type, bool Temp,
  939. TStrings * FileList, UnicodeString & TargetDirectory, TGUICopyParamType & CopyParam,
  940. bool Confirm, bool DragDrop, int Options)
  941. {
  942. bool Result = true;
  943. DebugAssert(Terminal && Terminal->Active);
  944. // Temp means d&d here so far, may change in future!
  945. if (Temp && (Direction == tdToLocal) && (Type == ttMove) &&
  946. !WinConfiguration->DDAllowMove)
  947. {
  948. TMessageParams Params(mpNeverAskAgainCheck);
  949. unsigned int Answer = MessageDialog(LoadStr(DND_DOWNLOAD_MOVE_WARNING), qtWarning,
  950. qaOK | qaCancel, HELP_DND_DOWNLOAD_MOVE_WARNING, &Params);
  951. if (Answer == qaNeverAskAgain)
  952. {
  953. WinConfiguration->DDAllowMove = true;
  954. }
  955. else if (Answer == qaCancel)
  956. {
  957. Result = false;
  958. }
  959. }
  960. // these parameters are known in advance
  961. int Params =
  962. FLAGMASK(Type == ttMove, cpDelete);
  963. bool ToTemp = (Temp && (Direction == tdToLocal));
  964. if (Result && Confirm && WinConfiguration->ConfirmTransferring)
  965. {
  966. bool DisableNewerOnly =
  967. (!Terminal->IsCapable[fcNewerOnlyUpload] && (Direction == tdToRemote)) ||
  968. ToTemp;
  969. Options |=
  970. FLAGMASK(ToTemp, coTemp) |
  971. coDoNotShowAgain;
  972. TUsableCopyParamAttrs UsableCopyParamAttrs = Terminal->UsableCopyParamAttrs(Params);
  973. int CopyParamAttrs = (Direction == tdToRemote ?
  974. UsableCopyParamAttrs.Upload : UsableCopyParamAttrs.Download) |
  975. FLAGMASK(DisableNewerOnly, cpaNoNewerOnly);
  976. int OutputOptions =
  977. FLAGMASK(DragDrop && (WinConfiguration->DDTransferConfirmation == asAuto),
  978. cooDoNotShowAgain);
  979. std::unique_ptr<TSessionData> SessionData(SessionDataForCode());
  980. Result = DoCopyDialog(Direction == tdToRemote, Type == ttMove,
  981. FileList, TargetDirectory, &CopyParam, Options, CopyParamAttrs, SessionData.get(), &OutputOptions);
  982. if (Result)
  983. {
  984. if (FLAGSET(OutputOptions, cooDoNotShowAgain))
  985. {
  986. if (DragDrop)
  987. {
  988. if (WinConfiguration->DDTransferConfirmation == asAuto)
  989. {
  990. PopupTrayBalloon(NULL, LoadStr(DD_TRANSFER_CONFIRM_OFF2), qtInformation,
  991. NULL, 0, EnableDDTransferConfirmation, NULL);
  992. }
  993. WinConfiguration->DDTransferConfirmation = asOff;
  994. }
  995. else
  996. {
  997. WinConfiguration->ConfirmTransferring = false;
  998. }
  999. }
  1000. else
  1001. {
  1002. // User exclicitly unchecked "do not show again",
  1003. // so show him the dialog the next time
  1004. if (DragDrop && (WinConfiguration->DDTransferConfirmation == asAuto))
  1005. {
  1006. WinConfiguration->DDTransferConfirmation = asOn;
  1007. }
  1008. }
  1009. CopyParamDialogAfter(Direction, Temp, TargetDirectory);
  1010. }
  1011. }
  1012. if (Result && CopyParam.Queue && !ToTemp)
  1013. {
  1014. Configuration->Usage->Inc(L"TransfersOnBackground");
  1015. // these parameter are known only after transfer dialog
  1016. Params |=
  1017. (CopyParam.QueueNoConfirmation ? cpNoConfirmation : 0);
  1018. AddQueueItem(Queue, Direction, FileList, TargetDirectory, CopyParam, Params);
  1019. Result = false;
  1020. ClearTransferSourceSelection(Direction);
  1021. }
  1022. return Result;
  1023. }
  1024. //---------------------------------------------------------------------------
  1025. void __fastcall TCustomScpExplorerForm::ClearTransferSourceSelection(TTransferDirection Direction)
  1026. {
  1027. if (FOnFileOperationFinished != NULL)
  1028. {
  1029. FOnFileOperationFinished(UnicodeString(), true);
  1030. }
  1031. else
  1032. {
  1033. TOperationSide Side = ((Direction == tdToRemote) ? osLocal : osRemote);
  1034. if (HasDirView[Side])
  1035. {
  1036. DirView(Side)->SelectAll(smNone);
  1037. }
  1038. }
  1039. }
  1040. //---------------------------------------------------------------------------
  1041. void __fastcall TCustomScpExplorerForm::AddQueueItem(
  1042. TTerminalQueue * Queue, TTransferDirection Direction, TStrings * FileList,
  1043. const UnicodeString TargetDirectory, const TGUICopyParamType & CopyParam,
  1044. int Params)
  1045. {
  1046. DebugAssert(Queue != NULL);
  1047. bool SingleFile = false;
  1048. if (FileList->Count == 1)
  1049. {
  1050. if (Direction == tdToRemote)
  1051. {
  1052. UnicodeString FileName = FileList->Strings[0];
  1053. SingleFile = FileExists(ApiPath(FileName));
  1054. }
  1055. else
  1056. {
  1057. TRemoteFile * File = static_cast<TRemoteFile *>(FileList->Objects[0]);
  1058. SingleFile = !File->IsDirectory;
  1059. }
  1060. }
  1061. TQueueItem * QueueItem;
  1062. if (Direction == tdToRemote)
  1063. {
  1064. QueueItem = new TUploadQueueItem(Terminal, FileList, TargetDirectory,
  1065. &CopyParam, Params, SingleFile, CopyParam.QueueParallel);
  1066. }
  1067. else
  1068. {
  1069. QueueItem = new TDownloadQueueItem(Terminal, FileList, TargetDirectory,
  1070. &CopyParam, Params, SingleFile, CopyParam.QueueParallel);
  1071. }
  1072. AddQueueItem(Queue, QueueItem, Terminal);
  1073. }
  1074. //---------------------------------------------------------------------------
  1075. void __fastcall TCustomScpExplorerForm::AddQueueItem(TTerminalQueue * Queue, TQueueItem * QueueItem, TTerminal * Terminal)
  1076. {
  1077. if (Queue->IsEmpty)
  1078. {
  1079. TManagedTerminal * ManagedTerminal = DebugNotNull(dynamic_cast<TManagedTerminal *>(Terminal));
  1080. ManagedTerminal->QueueOperationStart = Now();
  1081. }
  1082. Queue->AddItem(QueueItem);
  1083. }
  1084. //---------------------------------------------------------------------------
  1085. void __fastcall TCustomScpExplorerForm::RestoreFormParams()
  1086. {
  1087. }
  1088. //---------------------------------------------------------------------------
  1089. void __fastcall TCustomScpExplorerForm::RestoreParams()
  1090. {
  1091. DebugAssert(FDocks != NULL);
  1092. for (int Index = 0; Index < ComponentCount; Index++)
  1093. {
  1094. TTBDock * Dock = dynamic_cast<TTBDock *>(Components[Index]);
  1095. if ((Dock != NULL) && (Dock->Tag == 0))
  1096. {
  1097. FDocks->Add(Dock);
  1098. }
  1099. }
  1100. CollectItemsWithTextDisplayMode(this);
  1101. ConfigurationChanged();
  1102. QueuePanel->Height =
  1103. LoadDimension(WinConfiguration->QueueView.Height, WinConfiguration->QueueView.HeightPixelsPerInch, this);
  1104. LoadListViewStr(QueueView3, WinConfiguration->QueueView.Layout);
  1105. QueueDock->Visible = WinConfiguration->QueueView.ToolBar;
  1106. QueueLabel->Visible = WinConfiguration->QueueView.Label;
  1107. }
  1108. //---------------------------------------------------------------------------
  1109. void __fastcall TCustomScpExplorerForm::StoreParams()
  1110. {
  1111. WinConfiguration->QueueView.Height = QueuePanel->Height;
  1112. WinConfiguration->QueueView.HeightPixelsPerInch = GetControlPixelsPerInch(this);
  1113. WinConfiguration->QueueView.Layout = GetListViewStr(QueueView3);
  1114. WinConfiguration->QueueView.ToolBar = QueueDock->Visible;
  1115. WinConfiguration->QueueView.Label = QueueLabel->Visible;
  1116. }
  1117. //---------------------------------------------------------------------------
  1118. void __fastcall TCustomScpExplorerForm::CreateParams(TCreateParams & Params)
  1119. {
  1120. if (!FFormRestored)
  1121. {
  1122. FFormRestored = true;
  1123. RestoreFormParams();
  1124. }
  1125. TForm::CreateParams(Params);
  1126. }
  1127. //---------------------------------------------------------------------------
  1128. void __fastcall TCustomScpExplorerForm::SetDockAllowDrag(bool value)
  1129. {
  1130. DebugAssert(FDocks != NULL);
  1131. for (int Index = 0; Index < FDocks->Count; Index++)
  1132. {
  1133. static_cast<TTBDock*>(FDocks->Items[Index])->AllowDrag = value;
  1134. }
  1135. }
  1136. //---------------------------------------------------------------------------
  1137. void __fastcall TCustomScpExplorerForm::LoadToolbarsLayoutStr(UnicodeString LayoutStr, UnicodeString ButtonsStr)
  1138. {
  1139. SetDockAllowDrag(true);
  1140. while (!ButtonsStr.IsEmpty())
  1141. {
  1142. UnicodeString S = CutToChar(ButtonsStr, L';', true);
  1143. UnicodeString Name = CutToChar(S, L'=', true);
  1144. for (int ToolbarIndex = 0; ToolbarIndex < ComponentCount; ToolbarIndex++)
  1145. {
  1146. TTBCustomToolbar * Toolbar = dynamic_cast<TTBCustomToolbar *>(Components[ToolbarIndex]);
  1147. if ((Toolbar != NULL) && SameText(Name, GetToolbarKey(Toolbar->Name)))
  1148. {
  1149. while (!S.IsEmpty())
  1150. {
  1151. UnicodeString S2 = CutToChar(S, L',', true);
  1152. UnicodeString Name = CutToChar(S2, L':', true);
  1153. for (int ItemIndex = 0; ItemIndex < Toolbar->Items->Count; ItemIndex++)
  1154. {
  1155. TTBCustomItem * Item = Toolbar->Items->Items[ItemIndex];
  1156. if (SameText(GetToolbarItemName(Item), Name))
  1157. {
  1158. // forward compatibility
  1159. UnicodeString S3 = CutToChar(S2, L':', true);
  1160. bool Visible = StrToIntDef(S3, 0);
  1161. Item->Visible = Visible;
  1162. }
  1163. }
  1164. }
  1165. }
  1166. }
  1167. }
  1168. ::LoadToolbarsLayoutStr(this, LayoutStr);
  1169. SetDockAllowDrag(!WinConfiguration->LockToolbars);
  1170. }
  1171. //---------------------------------------------------------------------------
  1172. UnicodeString __fastcall TCustomScpExplorerForm::GetToolbarsLayoutStr()
  1173. {
  1174. UnicodeString Result;
  1175. SetDockAllowDrag(true);
  1176. Result = ::GetToolbarsLayoutStr(this);
  1177. SetDockAllowDrag(!WinConfiguration->LockToolbars);
  1178. return Result;
  1179. }
  1180. //---------------------------------------------------------------------------
  1181. UnicodeString __fastcall TCustomScpExplorerForm::GetToolbarItemName(TTBCustomItem * Item)
  1182. {
  1183. UnicodeString Result;
  1184. if (Item->Action != NULL)
  1185. {
  1186. Result = Item->Action->Name;
  1187. Result = RemoveSuffix(Result, L"Action");
  1188. }
  1189. else
  1190. {
  1191. Result = Item->Name;
  1192. Result = RemoveSuffix(Result, L"SubmenuItem");
  1193. Result = RemoveSuffix(Result, L"Item");
  1194. }
  1195. return Result;
  1196. }
  1197. //---------------------------------------------------------------------------
  1198. UnicodeString __fastcall TCustomScpExplorerForm::GetToolbarsButtonsStr()
  1199. {
  1200. UnicodeString Result;
  1201. for (int ToolbarIndex = 0; ToolbarIndex < ComponentCount; ToolbarIndex++)
  1202. {
  1203. UnicodeString ToolbarStr;
  1204. TTBCustomToolbar * Toolbar = dynamic_cast<TTBCustomToolbar *>(Components[ToolbarIndex]);
  1205. if ((Toolbar != NULL) && (Toolbar != QueueToolbar))
  1206. {
  1207. for (int ItemIndex = 0; ItemIndex < Toolbar->Items->Count; ItemIndex++)
  1208. {
  1209. TTBCustomItem * Item = Toolbar->Items->Items[ItemIndex];
  1210. // Currently all buttons are visible by default, so we can safely remember all hidden buttons.
  1211. // Once we introduce any buttons that aer hidden by default, we would have to remember their initial state
  1212. // and save the changes here only.
  1213. if (!Item->Visible)
  1214. {
  1215. UnicodeString Name = GetToolbarItemName(Item);
  1216. DebugAssert(Name.Pos(L"TBX") == 0);
  1217. AddToList(ToolbarStr, FORMAT(L"%s:0", (Name)), L",");
  1218. }
  1219. }
  1220. }
  1221. if (!ToolbarStr.IsEmpty())
  1222. {
  1223. AddToList(Result, FORMAT(L"%s=%s", (GetToolbarKey(Toolbar->Name), ToolbarStr)), L";");
  1224. }
  1225. }
  1226. return Result;
  1227. }
  1228. //---------------------------------------------------------------------------
  1229. void __fastcall TCustomScpExplorerForm::FileOperationProgress(
  1230. TFileOperationProgressType & ProgressData)
  1231. {
  1232. // Download to temporary local directory
  1233. if (FPendingTempSpaceWarn && ProgressData.InProgress && ProgressData.TotalSizeSet)
  1234. {
  1235. bool Continue = true;
  1236. FPendingTempSpaceWarn = false;
  1237. DoWarnLackOfTempSpace(ProgressData.Directory, ProgressData.TotalSize, Continue);
  1238. if (!Continue)
  1239. {
  1240. Abort();
  1241. }
  1242. }
  1243. // operation is being executed and we still didn't show up progress form
  1244. if (ProgressData.InProgress && !FProgressForm)
  1245. {
  1246. bool AllowSkip =
  1247. ((ProgressData.Operation == foCopy) || (ProgressData.Operation == foMove)) &&
  1248. Terminal->IsCapable[fsSkipTransfer];
  1249. FProgressForm = new TProgressForm(Application, (FTransferResumeList != NULL), AllowSkip);
  1250. // As progress window has delayed show now, we need to lock ourselves,
  1251. // (at least) until then
  1252. LockWindow();
  1253. FProgressForm->DeleteToRecycleBin = (ProgressData.Side == osLocal ?
  1254. (WinConfiguration->DeleteToRecycleBin != FAlternativeDelete) :
  1255. ((Terminal->SessionData->DeleteToRecycleBin != FAlternativeDelete) &&
  1256. !Terminal->SessionData->RecycleBinPath.IsEmpty()));
  1257. // When main window is hidden, synchronisation form does not exist,
  1258. // we suppose "/upload" or URL download mode
  1259. if (!Visible && (ProgressData.Operation != foCalculateSize) &&
  1260. (ProgressData.Operation != foCalculateChecksum) &&
  1261. (FSynchronizeProgressForm == NULL) &&
  1262. !FStandaloneEditing)
  1263. {
  1264. FProgressForm->OnceDoneOperation = odoDisconnect;
  1265. }
  1266. if (FTaskbarList != NULL)
  1267. {
  1268. // Actually, do not know what hides the progress once the operation finishes
  1269. SetTaskbarListProgressState(TBPF_NORMAL);
  1270. }
  1271. }
  1272. // operation is finished (or terminated), so we hide progress form
  1273. else if (!ProgressData.InProgress && FProgressForm)
  1274. {
  1275. UnlockWindow();
  1276. SAFE_DESTROY(FProgressForm);
  1277. SetQueueProgress();
  1278. if (ProgressData.Operation == foCalculateSize)
  1279. {
  1280. // When calculating size before transfer, the abort caused by
  1281. // cancel flag set due to "MoveToQueue" below,
  1282. // is not propagated back to us in ExecuteFileOperation,
  1283. // as it is silently swallowed.
  1284. // So we explicitly (re)throw it here.
  1285. if (FMoveToQueue)
  1286. {
  1287. Abort();
  1288. }
  1289. }
  1290. else
  1291. {
  1292. if ((ProgressData.Cancel == csContinue) &&
  1293. !FAutoOperation)
  1294. {
  1295. OperationComplete(ProgressData.StartTime);
  1296. }
  1297. }
  1298. }
  1299. if (FProgressForm)
  1300. {
  1301. FProgressForm->SetProgressData(ProgressData);
  1302. if (FTaskbarList != NULL)
  1303. {
  1304. DebugAssert(ProgressData.InProgress);
  1305. SetTaskbarListProgressValue(&ProgressData);
  1306. }
  1307. if (FProgressForm->Cancel > csContinue)
  1308. {
  1309. ProgressData.SetCancelAtLeast(FProgressForm->Cancel);
  1310. if (FProgressForm->Cancel == csCancelFile)
  1311. {
  1312. FProgressForm->ClearCancel();
  1313. }
  1314. // cancel cancels even the move
  1315. FMoveToQueue = false;
  1316. }
  1317. else if (FProgressForm->MoveToQueue)
  1318. {
  1319. FMoveToQueue = true;
  1320. ProgressData.SetCancelAtLeast(csCancel);
  1321. }
  1322. if ((FTransferResumeList != NULL) &&
  1323. ProgressData.InProgress &&
  1324. ((ProgressData.Operation == foCopy) || (ProgressData.Operation == foMove)) &&
  1325. !ProgressData.FullFileName.IsEmpty())
  1326. {
  1327. if ((FTransferResumeList->Count == 0) ||
  1328. (FTransferResumeList->Strings[FTransferResumeList->Count - 1] != ProgressData.FullFileName))
  1329. {
  1330. // note that we do not recognize directories from files here
  1331. FTransferResumeList->Add(ProgressData.FullFileName);
  1332. }
  1333. }
  1334. }
  1335. }
  1336. //---------------------------------------------------------------------------
  1337. void __fastcall TCustomScpExplorerForm::OperationComplete(
  1338. const TDateTime & StartTime)
  1339. {
  1340. if (GUIConfiguration->BeepOnFinish &&
  1341. (Now() - StartTime > GUIConfiguration->BeepOnFinishAfter))
  1342. {
  1343. UnicodeString BeepSound = GUIConfiguration->BeepSound;
  1344. DWORD Sound;
  1345. if (!ExtractFileExt(BeepSound).IsEmpty())
  1346. {
  1347. Sound = SND_FILENAME;
  1348. }
  1349. else
  1350. {
  1351. Sound = SND_ALIAS;
  1352. }
  1353. PlaySound(BeepSound.c_str(), NULL, Sound | SND_ASYNC);
  1354. }
  1355. }
  1356. //---------------------------------------------------------------------------
  1357. void __fastcall TCustomScpExplorerForm::OperationProgress(
  1358. TFileOperationProgressType & ProgressData)
  1359. {
  1360. // For example, when calling TFileOperationProgressType::Suspend in TTerminal::QueryReopen,
  1361. // we cannot recurse back to TTerminal::Idle as that may detect another error, calling "suspend" again.
  1362. TAutoFlag Flag(FDoNotIdleCurrentTerminal);
  1363. FileOperationProgress(ProgressData);
  1364. }
  1365. //---------------------------------------------------------------------------
  1366. bool __fastcall TCustomScpExplorerForm::PanelOperation(TOperationSide /*Side*/,
  1367. bool DragDrop)
  1368. {
  1369. return (!DragDrop && (DropSourceControl == NULL)) ||
  1370. (DropSourceControl == RemoteDirView);
  1371. }
  1372. //---------------------------------------------------------------------------
  1373. void __fastcall TCustomScpExplorerForm::DoOperationFinished(
  1374. TFileOperation Operation, TOperationSide Side,
  1375. bool /*Temp*/, const UnicodeString & FileName, bool Success,
  1376. TOnceDoneOperation & OnceDoneOperation)
  1377. {
  1378. if (!FAutoOperation)
  1379. {
  1380. // no selection on "/upload", form servers only as event handler
  1381. // (it is not displayed)
  1382. if (PanelOperation(Side, FDragDropOperation) &&
  1383. Visible && (Operation != foCalculateSize) &&
  1384. (Operation != foGetProperties) && (Operation != foCalculateChecksum))
  1385. {
  1386. if (FOnFileOperationFinished != NULL)
  1387. {
  1388. FOnFileOperationFinished(FileName, Success);
  1389. }
  1390. else
  1391. {
  1392. TCustomDirView * DView = DirView(Side);
  1393. UnicodeString FileNameOnly = ExtractFileName(FileName, (Side == osRemote));
  1394. TListItem *Item = DView->FindFileItem(FileNameOnly);
  1395. // this can happen when local drive is unplugged in the middle of the operation
  1396. if (Item != NULL)
  1397. {
  1398. if (Success) Item->Selected = false;
  1399. DView->MakeProgressVisible(Item);
  1400. }
  1401. }
  1402. }
  1403. if ((Operation == foCopy) || (Operation == foMove))
  1404. {
  1405. if (Side == osLocal)
  1406. {
  1407. Configuration->Usage->Inc(L"UploadedFiles");
  1408. }
  1409. else
  1410. {
  1411. Configuration->Usage->Inc(L"DownloadedFiles");
  1412. }
  1413. }
  1414. }
  1415. if (Success && (FSynchronizeController != NULL))
  1416. {
  1417. if (Operation == foCopy)
  1418. {
  1419. DebugAssert(Side == osLocal);
  1420. FSynchronizeController->LogOperation(soUpload, FileName);
  1421. }
  1422. else if (Operation == foDelete)
  1423. {
  1424. DebugAssert(Side == osRemote);
  1425. FSynchronizeController->LogOperation(soDelete, FileName);
  1426. }
  1427. }
  1428. if (FProgressForm)
  1429. {
  1430. OnceDoneOperation = FProgressForm->OnceDoneOperation;
  1431. }
  1432. }
  1433. //---------------------------------------------------------------------------
  1434. void __fastcall TCustomScpExplorerForm::OperationFinished(
  1435. TFileOperation Operation, TOperationSide Side,
  1436. bool Temp, const UnicodeString & FileName, Boolean Success,
  1437. TOnceDoneOperation & OnceDoneOperation)
  1438. {
  1439. TAutoFlag Flag(FDoNotIdleCurrentTerminal);
  1440. DoOperationFinished(Operation, Side, Temp, FileName, Success,
  1441. OnceDoneOperation);
  1442. }
  1443. //---------------------------------------------------------------------------
  1444. TCustomDirView * __fastcall TCustomScpExplorerForm::DirView(TOperationSide Side)
  1445. {
  1446. DebugAssert(GetSide(Side) == osRemote);
  1447. DebugUsedParam(Side);
  1448. return RemoteDirView;
  1449. }
  1450. //---------------------------------------------------------------------------
  1451. bool __fastcall TCustomScpExplorerForm::DirViewEnabled(TOperationSide Side)
  1452. {
  1453. DebugAssert(GetSide(Side) == osRemote);
  1454. DebugUsedParam(Side);
  1455. return (Terminal != NULL);
  1456. }
  1457. //---------------------------------------------------------------------------
  1458. bool __fastcall TCustomScpExplorerForm::GetEnableFocusedOperation(
  1459. TOperationSide Side, int FilesOnly)
  1460. {
  1461. return DirView(Side)->AnyFileSelected(true, (FilesOnly != 0), true);
  1462. }
  1463. //---------------------------------------------------------------------------
  1464. bool __fastcall TCustomScpExplorerForm::GetEnableSelectedOperation(
  1465. TOperationSide Side, int FilesOnly)
  1466. {
  1467. return DirView(Side)->AnyFileSelected(false, (FilesOnly != 0), true);
  1468. }
  1469. //---------------------------------------------------------------------------
  1470. struct THistoryItemData
  1471. {
  1472. short int Side;
  1473. short int Index;
  1474. };
  1475. //---------------------------------------------------------------------------
  1476. void __fastcall TCustomScpExplorerForm::HistoryGo(TOperationSide Side, int Index)
  1477. {
  1478. DirView(Side)->HistoryGo(Index);
  1479. }
  1480. //---------------------------------------------------------------------------
  1481. void __fastcall TCustomScpExplorerForm::HistoryItemClick(System::TObject* Sender)
  1482. {
  1483. TTBCustomItem * Item = dynamic_cast<TTBCustomItem *>(Sender);
  1484. THistoryItemData Data = *reinterpret_cast<THistoryItemData*>(&(Item->Tag));
  1485. HistoryGo((TOperationSide)Data.Side, Data.Index);
  1486. }
  1487. //---------------------------------------------------------------------------
  1488. void __fastcall TCustomScpExplorerForm::UpdateHistoryMenu(TOperationSide Side,
  1489. bool Back)
  1490. {
  1491. if (FHistoryMenu[Side == osLocal][Back] != NULL)
  1492. {
  1493. TCustomDirView * DView = DirView(Side);
  1494. TTBXPopupMenu * Menu = FHistoryMenu[Side == osLocal][Back];
  1495. int ICount = Back ? DView->BackCount : DView->ForwardCount;
  1496. if (ICount > 10)
  1497. {
  1498. ICount = 10;
  1499. }
  1500. Menu->Items->Clear();
  1501. THistoryItemData Data;
  1502. Data.Side = Side;
  1503. for (short int i = 1; i <= ICount; i++)
  1504. {
  1505. TTBCustomItem * Item = new TTBXItem(Menu);
  1506. Data.Index = static_cast<short int>(i * (Back ? -1 : 1));
  1507. Item->Caption = MinimizeName(DView->HistoryPath[Data.Index], 50, (Side == osRemote));
  1508. Item->Hint = DView->HistoryPath[Data.Index];
  1509. DebugAssert(sizeof(int) == sizeof(THistoryItemData));
  1510. Item->Tag = *reinterpret_cast<int*>(&Data);
  1511. Item->OnClick = HistoryItemClick;
  1512. Menu->Items->Add(Item);
  1513. }
  1514. }
  1515. }
  1516. //---------------------------------------------------------------------------
  1517. TTBXPopupMenu * __fastcall TCustomScpExplorerForm::HistoryMenu(
  1518. TOperationSide Side, bool Back)
  1519. {
  1520. if (FHistoryMenu[Side == osLocal][Back] == NULL)
  1521. {
  1522. // workaround
  1523. // In Pascal the size of TTBXPopupMenu is 132, in C++ 136,
  1524. // operator new allocates memory in Pascal code, but calls inline
  1525. // contructor in C++, leading in problems, the function does
  1526. // both in Pascal code
  1527. FHistoryMenu[Side == osLocal][Back] = CreateTBXPopupMenu(this);
  1528. UpdateHistoryMenu(Side, Back);
  1529. }
  1530. return FHistoryMenu[Side == osLocal][Back];
  1531. }
  1532. //---------------------------------------------------------------------------
  1533. void __fastcall TCustomScpExplorerForm::DirViewHistoryChange(
  1534. TCustomDirView *Sender)
  1535. {
  1536. TOperationSide Side = (Sender == DirView(osRemote) ? osRemote : osLocal);
  1537. UpdateHistoryMenu(Side, true);
  1538. UpdateHistoryMenu(Side, false);
  1539. }
  1540. //---------------------------------------------------------------------------
  1541. bool __fastcall TCustomScpExplorerForm::CustomCommandRemoteAllowed()
  1542. {
  1543. // remote custom commands can be executed only if the server supports shell commands
  1544. // or have secondary shell
  1545. return (FTerminal != NULL) && (FTerminal->IsCapable[fcSecondaryShell] || FTerminal->IsCapable[fcShellAnyCommand]);
  1546. }
  1547. //---------------------------------------------------------------------------
  1548. int __fastcall TCustomScpExplorerForm::CustomCommandState(
  1549. const TCustomCommandType & Command, bool OnFocused, TCustomCommandListType ListType)
  1550. {
  1551. int Result;
  1552. TFileCustomCommand RemoteCustomCommand;
  1553. TLocalCustomCommand LocalCustomCommand;
  1554. TFileCustomCommand * NonInteractiveCustomCommand =
  1555. FLAGCLEAR(Command.Params, ccLocal) ? &RemoteCustomCommand : &LocalCustomCommand;
  1556. TInteractiveCustomCommand InteractiveCustomCommand(NonInteractiveCustomCommand);
  1557. UnicodeString Cmd = InteractiveCustomCommand.Complete(Command.Command, false);
  1558. if (FLAGCLEAR(Command.Params, ccLocal))
  1559. {
  1560. int AllowedState = CustomCommandRemoteAllowed() ? 1 : 0;
  1561. // custom command that does not operate with files can be executed anytime ...
  1562. if (!NonInteractiveCustomCommand->IsFileCommand(Cmd))
  1563. {
  1564. if ((ListType == ccltAll) || (ListType == ccltNonFile))
  1565. {
  1566. // ... but do not show such command in remote file menu (TODO)
  1567. Result = AllowedState;
  1568. }
  1569. else
  1570. {
  1571. Result = -1;
  1572. }
  1573. }
  1574. else
  1575. {
  1576. if ((ListType == ccltAll) || (ListType == ccltFile))
  1577. {
  1578. Result = ((FCurrentSide == osRemote) && DirView(osRemote)->AnyFileSelected(OnFocused, false, true)) ? AllowedState : 0;
  1579. }
  1580. else
  1581. {
  1582. Result = -1;
  1583. }
  1584. }
  1585. }
  1586. else
  1587. {
  1588. // custom command that does not operate with files can be executed anytime
  1589. if (!NonInteractiveCustomCommand->IsFileCommand(Cmd))
  1590. {
  1591. Result = ((ListType == ccltAll) || (ListType == ccltNonFile)) ? 1 : -1;
  1592. }
  1593. else if (LocalCustomCommand.HasLocalFileName(Cmd))
  1594. {
  1595. if ((ListType == ccltAll) || (ListType == ccltFile))
  1596. {
  1597. // special case is "diff"-style command that can be executed over any side,
  1598. // if we have both sides
  1599. Result =
  1600. // Cannot have focus on both panels, so we have to call AnyFileSelected
  1601. // directly (instead of EnableSelectedOperation) to pass
  1602. // false to FocusedFileOnlyWhenFocused when panel is inactive.
  1603. ((HasDirView[osLocal] && DirView(osLocal)->AnyFileSelected(false, false, (FCurrentSide == osLocal))) &&
  1604. DirView(osRemote)->AnyFileSelected(false, false, (FCurrentSide == osRemote))) ? 1 : 0;
  1605. }
  1606. else if (ListType == ccltBoth)
  1607. {
  1608. Result = 1;
  1609. }
  1610. else
  1611. {
  1612. Result = -1;
  1613. }
  1614. }
  1615. else
  1616. {
  1617. if ((ListType == ccltAll) || (ListType == ccltFile))
  1618. {
  1619. Result = DirView(FCurrentSide)->AnyFileSelected(OnFocused, false, true) ? 1 : 0;
  1620. }
  1621. else
  1622. {
  1623. Result = -1;
  1624. }
  1625. }
  1626. }
  1627. return Result;
  1628. }
  1629. //---------------------------------------------------------------------------
  1630. void __fastcall TCustomScpExplorerForm::RemoteCustomCommand(
  1631. TStrings * FileList, const TCustomCommandType & ACommand,
  1632. const TCustomCommandData & Data, const UnicodeString & CommandCommand)
  1633. {
  1634. if (EnsureCommandSessionFallback(fcShellAnyCommand))
  1635. {
  1636. TRemoteCustomCommand RemoteCustomCommand(Data, Terminal->CurrentDirectory);
  1637. TWinInteractiveCustomCommand InteractiveCustomCommand(
  1638. &RemoteCustomCommand, ACommand.Name, ACommand.HomePage);
  1639. UnicodeString Command = InteractiveCustomCommand.Complete(CommandCommand, false);
  1640. Configuration->Usage->Inc(L"RemoteCustomCommandRuns2");
  1641. bool Capture =
  1642. FLAGSET(ACommand.Params, ccShowResults) ||
  1643. FLAGSET(ACommand.Params, ccCopyResults) ||
  1644. FLAGSET(ACommand.Params, ccShowResultsInMsgBox);
  1645. TCaptureOutputEvent OutputEvent = NULL;
  1646. DebugAssert(FCapturedLog == NULL);
  1647. if (Capture)
  1648. {
  1649. FCapturedLog = new TStringList();
  1650. OutputEvent = TerminalCaptureLog;
  1651. }
  1652. try
  1653. {
  1654. if (!RemoteCustomCommand.IsFileCommand(Command))
  1655. {
  1656. Terminal->AnyCommand(RemoteCustomCommand.Complete(Command, true),
  1657. OutputEvent);
  1658. }
  1659. else
  1660. {
  1661. Terminal->CustomCommandOnFiles(Command, ACommand.Params, FileList, OutputEvent);
  1662. }
  1663. if ((FCapturedLog != NULL) && (FCapturedLog->Count > 0))
  1664. {
  1665. if (FLAGSET(ACommand.Params, ccCopyResults))
  1666. {
  1667. CopyToClipboard(FCapturedLog);
  1668. }
  1669. if (FLAGSET(ACommand.Params, ccShowResults))
  1670. {
  1671. DoConsoleDialog(Terminal, L"", FCapturedLog);
  1672. }
  1673. else if (FLAGSET(ACommand.Params, ccShowResultsInMsgBox))
  1674. {
  1675. MessageDialog(FCapturedLog->Text, qtInformation, qaOK);
  1676. }
  1677. }
  1678. }
  1679. __finally
  1680. {
  1681. SAFE_DESTROY(FCapturedLog);
  1682. }
  1683. }
  1684. }
  1685. //---------------------------------------------------------------------------
  1686. void __fastcall TCustomScpExplorerForm::LocalCustomCommandPure(
  1687. TStrings * FileList, const TCustomCommandType & ACommand, const UnicodeString & Command, TStrings * ALocalFileList,
  1688. const TCustomCommandData & Data, bool LocalFileCommand, bool FileListCommand, UnicodeString * POutput)
  1689. {
  1690. TStrings * LocalFileList = NULL;
  1691. TStrings * RemoteFileList = NULL;
  1692. TStrings * RemoteFileListFull = NULL;
  1693. try
  1694. {
  1695. if (LocalFileCommand)
  1696. {
  1697. if (ALocalFileList == NULL)
  1698. {
  1699. DebugAssert(HasDirView[osLocal]);
  1700. // Cannot have focus on both panels, so we have to call AnyFileSelected
  1701. // directly (instead of EnableSelectedOperation) to pass
  1702. // false to FocusedFileOnlyWhenFocused
  1703. DebugAssert(DirView(osLocal)->AnyFileSelected(false, false, false));
  1704. LocalFileList = DirView(osLocal)->CreateFileList(false, true, NULL);
  1705. }
  1706. else
  1707. {
  1708. LocalFileList = ALocalFileList;
  1709. }
  1710. if (FileListCommand)
  1711. {
  1712. if (LocalFileList->Count != 1)
  1713. {
  1714. throw Exception(LoadStr(CUSTOM_COMMAND_SELECTED_UNMATCH1));
  1715. }
  1716. }
  1717. else
  1718. {
  1719. if ((LocalFileList->Count != 1) &&
  1720. (FileList->Count != 1) &&
  1721. (LocalFileList->Count != FileList->Count))
  1722. {
  1723. throw Exception(LoadStr(CUSTOM_COMMAND_SELECTED_UNMATCH));
  1724. }
  1725. }
  1726. }
  1727. UnicodeString RootTempDir;
  1728. UnicodeString TempDir;
  1729. bool RemoteFiles = FLAGSET(ACommand.Params, ccRemoteFiles);
  1730. if (!RemoteFiles)
  1731. {
  1732. TemporarilyDownloadFiles(FileList, false, RootTempDir, TempDir, false, false, true);
  1733. }
  1734. try
  1735. {
  1736. TDateTimes RemoteFileTimes;
  1737. if (RemoteFiles)
  1738. {
  1739. RemoteFileList = FileList;
  1740. }
  1741. else
  1742. {
  1743. RemoteFileList = new TStringList();
  1744. TMakeLocalFileListParams MakeFileListParam;
  1745. MakeFileListParam.FileList = RemoteFileList;
  1746. MakeFileListParam.FileTimes = &RemoteFileTimes;
  1747. MakeFileListParam.IncludeDirs = FLAGSET(ACommand.Params, ccApplyToDirectories);
  1748. MakeFileListParam.Recursive =
  1749. FLAGSET(ACommand.Params, ccRecursive) && !FileListCommand;
  1750. ProcessLocalDirectory(TempDir, Terminal->MakeLocalFileList, &MakeFileListParam);
  1751. if (!MakeFileListParam.Recursive)
  1752. {
  1753. RemoteFileTimes.clear();
  1754. RemoteFileListFull = new TStringList();
  1755. MakeFileListParam.FileList = RemoteFileListFull;
  1756. MakeFileListParam.Recursive = true;
  1757. MakeFileListParam.IncludeDirs = false;
  1758. ProcessLocalDirectory(TempDir, Terminal->MakeLocalFileList, &MakeFileListParam);
  1759. }
  1760. else
  1761. {
  1762. RemoteFileListFull = RemoteFileList;
  1763. }
  1764. }
  1765. bool NonBlocking = FileListCommand && RemoteFiles && !POutput;
  1766. TFileOperationProgressType Progress(&OperationProgress, &OperationFinished);
  1767. if (!NonBlocking)
  1768. {
  1769. Progress.Start(foCustomCommand, osRemote, FileListCommand ? 1 : FileList->Count);
  1770. DebugAssert(FProgressForm != NULL);
  1771. FProgressForm->ReadOnly = true;
  1772. }
  1773. try
  1774. {
  1775. if (FileListCommand)
  1776. {
  1777. UnicodeString LocalFile;
  1778. // MakeFileList does not delimit filenames
  1779. UnicodeString FileList = MakeFileList(RemoteFileList);
  1780. if (LocalFileCommand)
  1781. {
  1782. DebugAssert(LocalFileList->Count == 1);
  1783. LocalFile = LocalFileList->Strings[0];
  1784. }
  1785. TLocalCustomCommand CustomCommand(Data,
  1786. Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(), L"", LocalFile, FileList);
  1787. UnicodeString ShellCommand = CustomCommand.Complete(Command, true);
  1788. if (NonBlocking)
  1789. {
  1790. DebugAssert(!POutput);
  1791. ExecuteShellChecked(ShellCommand);
  1792. }
  1793. else
  1794. {
  1795. ExecuteProcessCheckedAndWait(ShellCommand, HelpKeyword, POutput);
  1796. }
  1797. }
  1798. else if (LocalFileCommand)
  1799. {
  1800. if (LocalFileList->Count == 1)
  1801. {
  1802. UnicodeString LocalFile = LocalFileList->Strings[0];
  1803. for (int Index = 0; Index < RemoteFileList->Count; Index++)
  1804. {
  1805. UnicodeString FileName = RemoteFileList->Strings[Index];
  1806. TLocalCustomCommand CustomCommand(Data,
  1807. Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(), FileName, LocalFile, L"");
  1808. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  1809. }
  1810. }
  1811. else if (RemoteFileList->Count == 1)
  1812. {
  1813. UnicodeString FileName = RemoteFileList->Strings[0];
  1814. for (int Index = 0; Index < LocalFileList->Count; Index++)
  1815. {
  1816. TLocalCustomCommand CustomCommand(
  1817. Data, Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(),
  1818. FileName, LocalFileList->Strings[Index], L"");
  1819. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  1820. }
  1821. }
  1822. else
  1823. {
  1824. if (LocalFileList->Count != RemoteFileList->Count)
  1825. {
  1826. throw Exception(LoadStr(CUSTOM_COMMAND_PAIRS_DOWNLOAD_FAILED));
  1827. }
  1828. for (int Index = 0; Index < LocalFileList->Count; Index++)
  1829. {
  1830. UnicodeString FileName = RemoteFileList->Strings[Index];
  1831. TLocalCustomCommand CustomCommand(
  1832. Data, Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(),
  1833. FileName, LocalFileList->Strings[Index], L"");
  1834. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  1835. }
  1836. }
  1837. }
  1838. else
  1839. {
  1840. for (int Index = 0; Index < RemoteFileList->Count; Index++)
  1841. {
  1842. TLocalCustomCommand CustomCommand(Data,
  1843. Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(),
  1844. RemoteFileList->Strings[Index], L"", L"");
  1845. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  1846. }
  1847. }
  1848. }
  1849. __finally
  1850. {
  1851. if (!NonBlocking)
  1852. {
  1853. Progress.Stop();
  1854. }
  1855. }
  1856. DebugAssert(!FAutoOperation);
  1857. if (!RemoteFiles)
  1858. {
  1859. TempDir = IncludeTrailingBackslash(TempDir);
  1860. for (int Index = 0; Index < RemoteFileListFull->Count; Index++)
  1861. {
  1862. UnicodeString FileName = RemoteFileListFull->Strings[Index];
  1863. if (DebugAlwaysTrue(SameText(TempDir, FileName.SubString(1, TempDir.Length()))) &&
  1864. // Skip directories, process only nested files.
  1865. // The check is redundant as FileAge fails for directories anyway.
  1866. !DirectoryExists(FileName))
  1867. {
  1868. UnicodeString RemoteDir =
  1869. UnixExtractFileDir(
  1870. UnixIncludeTrailingBackslash(FTerminal->CurrentDirectory) +
  1871. ToUnixPath(FileName.SubString(TempDir.Length() + 1, FileName.Length() - TempDir.Length())));
  1872. TDateTime NewTime;
  1873. if (FileAge(FileName, NewTime) &&
  1874. (NewTime != RemoteFileTimes[Index]))
  1875. {
  1876. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  1877. TemporaryFileCopyParam(CopyParam);
  1878. CopyParam.FileMask = L"";
  1879. FAutoOperation = true;
  1880. std::unique_ptr<TStrings> TemporaryFilesList(new TStringList());
  1881. TemporaryFilesList->Add(FileName);
  1882. FTerminal->CopyToRemote(TemporaryFilesList.get(), RemoteDir, &CopyParam, cpTemporary, NULL);
  1883. }
  1884. }
  1885. }
  1886. }
  1887. }
  1888. __finally
  1889. {
  1890. FAutoOperation = false;
  1891. if (!RootTempDir.IsEmpty() && DebugAlwaysTrue(!RemoteFiles))
  1892. {
  1893. RecursiveDeleteFile(ExcludeTrailingBackslash(RootTempDir), false);
  1894. }
  1895. }
  1896. }
  1897. __finally
  1898. {
  1899. if (RemoteFileList != FileList)
  1900. {
  1901. delete RemoteFileList;
  1902. }
  1903. if (LocalFileList != ALocalFileList)
  1904. {
  1905. delete LocalFileList;
  1906. }
  1907. if (RemoteFileListFull != RemoteFileList)
  1908. {
  1909. delete RemoteFileListFull;
  1910. }
  1911. }
  1912. }
  1913. //---------------------------------------------------------------------------
  1914. void __fastcall TCustomScpExplorerForm::LocalCustomCommandWithRemoteFiles(
  1915. const TCustomCommandType & ACommand, const UnicodeString & Command, const TCustomCommandData & Data,
  1916. bool FileListCommand, UnicodeString * POutput)
  1917. {
  1918. std::unique_ptr<TStrings> SelectedFileList(DirView(osLocal)->CreateFileList(false, true, NULL));
  1919. std::unique_ptr<TStrings> LocalFileList(new TStringList());
  1920. for (int Index = 0; Index < SelectedFileList->Count; Index++)
  1921. {
  1922. UnicodeString FileName = SelectedFileList->Strings[Index];
  1923. if (DirectoryExists(FileName))
  1924. {
  1925. if (FLAGSET(ACommand.Params, ccApplyToDirectories))
  1926. {
  1927. LocalFileList->Add(FileName);
  1928. }
  1929. if (FLAGSET(ACommand.Params, ccRecursive))
  1930. {
  1931. TMakeLocalFileListParams MakeFileListParam;
  1932. MakeFileListParam.FileList = LocalFileList.get();
  1933. MakeFileListParam.FileTimes = NULL;
  1934. MakeFileListParam.IncludeDirs = FLAGSET(ACommand.Params, ccApplyToDirectories);
  1935. MakeFileListParam.Recursive = true;
  1936. ProcessLocalDirectory(FileName, Terminal->MakeLocalFileList, &MakeFileListParam);
  1937. }
  1938. }
  1939. else
  1940. {
  1941. LocalFileList->Add(FileName);
  1942. }
  1943. }
  1944. if (FileListCommand)
  1945. {
  1946. UnicodeString FileList = MakeFileList(LocalFileList.get());
  1947. TLocalCustomCommand CustomCommand(
  1948. Data, Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(),
  1949. L"", L"", FileList);
  1950. ExecuteProcessChecked(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  1951. }
  1952. else
  1953. {
  1954. TFileOperationProgressType Progress(&OperationProgress, &OperationFinished);
  1955. Progress.Start(foCustomCommand, osRemote, FileListCommand ? 1 : LocalFileList->Count);
  1956. DebugAssert(FProgressForm != NULL);
  1957. FProgressForm->ReadOnly = true;
  1958. try
  1959. {
  1960. for (int Index = 0; Index < LocalFileList->Count; Index++)
  1961. {
  1962. UnicodeString FileName = LocalFileList->Strings[Index];
  1963. TLocalCustomCommand CustomCommand(
  1964. Data, Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(),
  1965. FileName, L"", L"");
  1966. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  1967. }
  1968. }
  1969. __finally
  1970. {
  1971. Progress.Stop();
  1972. }
  1973. }
  1974. }
  1975. //---------------------------------------------------------------------------
  1976. void __fastcall TCustomScpExplorerForm::LocalCustomCommand(TStrings * FileList,
  1977. const TCustomCommandType & ACommand, TStrings * ALocalFileList,
  1978. const TCustomCommandData & Data, const UnicodeString & CommandCommand)
  1979. {
  1980. TLocalCustomCommand LocalCustomCommand(Data, Terminal->CurrentDirectory, DefaultDownloadTargetDirectory());
  1981. TWinInteractiveCustomCommand InteractiveCustomCommand(
  1982. &LocalCustomCommand, ACommand.Name, ACommand.HomePage);
  1983. UnicodeString Command = InteractiveCustomCommand.Complete(CommandCommand, false);
  1984. bool FileListCommand = LocalCustomCommand.IsFileListCommand(Command);
  1985. bool LocalFileCommand = LocalCustomCommand.HasLocalFileName(Command);
  1986. Configuration->Usage->Inc(L"LocalCustomCommandRuns2");
  1987. std::unique_ptr<UnicodeString> POutput;
  1988. if (FLAGSET(ACommand.Params, ccShowResultsInMsgBox) ||
  1989. FLAGSET(ACommand.Params, ccCopyResults))
  1990. {
  1991. POutput.reset(new UnicodeString());
  1992. }
  1993. if (!LocalCustomCommand.IsFileCommand(Command))
  1994. {
  1995. ExecuteProcessChecked(LocalCustomCommand.Complete(Command, true), HelpKeyword, POutput.get());
  1996. }
  1997. // remote files?
  1998. else if ((FCurrentSide == osRemote) || LocalFileCommand)
  1999. {
  2000. LocalCustomCommandPure(FileList, ACommand, Command, ALocalFileList, Data, LocalFileCommand, FileListCommand, POutput.get());
  2001. }
  2002. // local files
  2003. else
  2004. {
  2005. LocalCustomCommandWithRemoteFiles(ACommand, Command, Data, FileListCommand, POutput.get());
  2006. }
  2007. if (POutput.get() != NULL)
  2008. {
  2009. // If the output is single-line, we do not want the trailing CRLF, as the CopyToClipboard(TStrings *) does
  2010. int P = POutput->Pos(sLineBreak);
  2011. if (P == POutput->Length() - static_cast<int>(strlen(sLineBreak)) + 1)
  2012. {
  2013. POutput->SetLength(P - 1);
  2014. }
  2015. // Copy even empty output to clipboard
  2016. if (FLAGSET(ACommand.Params, ccCopyResults))
  2017. {
  2018. CopyToClipboard(*POutput);
  2019. }
  2020. // But do not show an empty message box.
  2021. // This way the ShowResultsInMsgBox can be used to suppress a console window of commands with no output
  2022. if (FLAGSET(ACommand.Params, ccShowResultsInMsgBox) &&
  2023. !POutput->IsEmpty())
  2024. {
  2025. TClipboardHandler ClipboardHandler;
  2026. ClipboardHandler.Text = *POutput;
  2027. TMessageParams Params;
  2028. TQueryButtonAlias Aliases[1];
  2029. Aliases[0].Button = qaRetry;
  2030. Aliases[0].Alias = LoadStr(EDIT_COPY);
  2031. Aliases[0].OnSubmit = &ClipboardHandler.Copy;
  2032. Params.Aliases = Aliases;
  2033. Params.AliasesCount = LENOF(Aliases);
  2034. MessageDialog(*POutput, qtInformation, qaOK | qaRetry, HelpKeyword, &Params);
  2035. }
  2036. }
  2037. }
  2038. //---------------------------------------------------------------------------
  2039. void __fastcall TCustomScpExplorerForm::CustomCommand(TStrings * FileList,
  2040. const TCustomCommandType & ACommand, TStrings * ALocalFileList)
  2041. {
  2042. TCustomCommandData Data(Terminal);
  2043. UnicodeString Site = Terminal->SessionData->SessionKey;
  2044. UnicodeString HelpKeyword = ACommand.HomePage;
  2045. std::unique_ptr<TStrings> CustomCommandOptions(CloneStrings(WinConfiguration->CustomCommandOptions));
  2046. if (ACommand.AnyOptionWithFlag(TCustomCommandType::ofRun))
  2047. {
  2048. std::unique_ptr<TCustomCommand> CustomCommandForOptions;
  2049. if (FLAGCLEAR(ACommand.Params, ccLocal))
  2050. {
  2051. CustomCommandForOptions.reset(new TRemoteCustomCommand(Data, Terminal->CurrentDirectory));
  2052. }
  2053. else
  2054. {
  2055. CustomCommandForOptions.reset(new TLocalCustomCommand(Data, Terminal->CurrentDirectory, DefaultDownloadTargetDirectory()));
  2056. }
  2057. if (!DoCustomCommandOptionsDialog(
  2058. &ACommand, CustomCommandOptions.get(), NULL, TCustomCommandType::ofRun, CustomCommandForOptions.get(), Site, NULL))
  2059. {
  2060. Abort();
  2061. }
  2062. }
  2063. UnicodeString CommandCommand = ACommand.GetCommandWithExpandedOptions(CustomCommandOptions.get(), Site);
  2064. if (FLAGCLEAR(ACommand.Params, ccLocal))
  2065. {
  2066. RemoteCustomCommand(FileList, ACommand, Data, CommandCommand);
  2067. }
  2068. else
  2069. {
  2070. LocalCustomCommand(FileList, ACommand, ALocalFileList, Data, CommandCommand);
  2071. }
  2072. }
  2073. //---------------------------------------------------------------------------
  2074. void __fastcall TCustomScpExplorerForm::BothCustomCommand(
  2075. const TCustomCommandType & Command)
  2076. {
  2077. DebugAssert(FCustomCommandLocalFileList != NULL);
  2078. DebugAssert(FCustomCommandRemoteFileList != NULL);
  2079. DebugAssert(FCustomCommandLocalFileList->Count == FCustomCommandRemoteFileList->Count);
  2080. TStrings * LocalFileList = new TStringList();
  2081. TStrings * RemoteFileList = new TStringList();
  2082. try
  2083. {
  2084. for (int Index = 0; Index < FCustomCommandLocalFileList->Count; Index++)
  2085. {
  2086. LocalFileList->Clear();
  2087. LocalFileList->AddObject(
  2088. FCustomCommandLocalFileList->Strings[Index],
  2089. FCustomCommandLocalFileList->Objects[Index]);
  2090. RemoteFileList->Clear();
  2091. RemoteFileList->AddObject(
  2092. FCustomCommandRemoteFileList->Strings[Index],
  2093. FCustomCommandRemoteFileList->Objects[Index]);
  2094. CustomCommand(RemoteFileList, Command, LocalFileList);
  2095. }
  2096. }
  2097. __finally
  2098. {
  2099. delete LocalFileList;
  2100. delete RemoteFileList;
  2101. }
  2102. }
  2103. //---------------------------------------------------------------------------
  2104. void __fastcall TCustomScpExplorerForm::CustomCommandMenu(
  2105. TAction * Action, TStrings * LocalFileList, TStrings * RemoteFileList)
  2106. {
  2107. delete FCustomCommandLocalFileList;
  2108. delete FCustomCommandRemoteFileList;
  2109. // takeover ownership,
  2110. // the lists must survive the MenuPopup as OnClick occurs only after it exits
  2111. FCustomCommandLocalFileList = LocalFileList;
  2112. FCustomCommandRemoteFileList = RemoteFileList;
  2113. TButton * Button = dynamic_cast<TButton *>(Action->ActionComponent);
  2114. if (Button != NULL)
  2115. {
  2116. FCustomCommandMenu->Items->Clear();
  2117. NonVisualDataModule->CreateCustomCommandsMenu(FCustomCommandMenu->Items, false, false, ccltBoth);
  2118. MenuPopup(FCustomCommandMenu, Button);
  2119. }
  2120. else
  2121. {
  2122. NonVisualDataModule->CreateCustomCommandsMenu(Action, false, ccltBoth);
  2123. }
  2124. }
  2125. //---------------------------------------------------------------------------
  2126. void __fastcall TCustomScpExplorerForm::TerminalCaptureLog(
  2127. const UnicodeString & AddedLine, TCaptureOutputType OutputType)
  2128. {
  2129. DebugAssert(FCapturedLog != NULL);
  2130. if ((OutputType == cotOutput) || (OutputType == cotError))
  2131. {
  2132. FCapturedLog->Add(AddedLine);
  2133. }
  2134. }
  2135. //---------------------------------------------------------------------------
  2136. bool __fastcall TCustomScpExplorerForm::IsFileControl(TObject * Control,
  2137. TOperationSide Side)
  2138. {
  2139. return (Side == osRemote) &&
  2140. ((Control == RemoteDirView) || (Control == RemoteDriveView));
  2141. }
  2142. //---------------------------------------------------------------------------
  2143. void __fastcall TCustomScpExplorerForm::DirViewContextPopupDefaultItem(
  2144. TOperationSide Side, TTBXCustomItem * Item, TDoubleClickAction DoubleClickAction)
  2145. {
  2146. TTBItemOptions O;
  2147. O = Item->Options;
  2148. TCustomDirView * DView = DirView(Side);
  2149. if ((DView->ItemFocused != NULL) &&
  2150. (WinConfiguration->DoubleClickAction == DoubleClickAction) &&
  2151. // when resolving links is disabled, default action is to enter the directory,
  2152. // no matter what DoubleClickAction is configured to
  2153. ((Side != osRemote) || Terminal->ResolvingSymlinks) &&
  2154. // Can only Edit files, but can Open/Copy even directories
  2155. ((DoubleClickAction != dcaEdit) ||
  2156. !DView->ItemIsDirectory(DView->ItemFocused)))
  2157. {
  2158. Item->Options = O << tboDefault;
  2159. }
  2160. else
  2161. {
  2162. Item->Options = O >> tboDefault;
  2163. }
  2164. }
  2165. //---------------------------------------------------------------------------
  2166. void __fastcall TCustomScpExplorerForm::DirViewContextPopup(
  2167. TOperationSide Side, Byte PopupComponent, const TPoint &MousePos)
  2168. {
  2169. TCustomDirView * DView = DirView(Side);
  2170. TListItem * Item = DView->ItemFocused;
  2171. if ((DView->GetItemAt(MousePos.x, MousePos.y) == Item) &&
  2172. EnableFocusedOperation[Side])
  2173. {
  2174. TPoint ClientPoint;
  2175. ClientPoint = ((MousePos.x < 0) && (MousePos.y < 0)) ?
  2176. TPoint(0, 0) : MousePos;
  2177. FLastContextPopupScreenPoint = DView->ClientToScreen(ClientPoint);
  2178. reinterpret_cast<TPopupMenu*>(GetComponent(PopupComponent))->Popup(
  2179. FLastContextPopupScreenPoint.x, FLastContextPopupScreenPoint.y);
  2180. }
  2181. }
  2182. //---------------------------------------------------------------------------
  2183. void __fastcall TCustomScpExplorerForm::RemoteDirViewContextPopup(
  2184. TObject * /*Sender*/, const TPoint &MousePos, bool &Handled)
  2185. {
  2186. DirViewContextPopupDefaultItem(osRemote, NonVisualDataModule->RemoteOpenMenuItem, dcaOpen);
  2187. DirViewContextPopupDefaultItem(osRemote, NonVisualDataModule->RemoteEditMenuItem, dcaEdit);
  2188. DirViewContextPopupDefaultItem(osRemote, NonVisualDataModule->RemoteCopyMenuItem, dcaCopy);
  2189. DirViewContextPopup(osRemote, fcRemotePopup, MousePos);
  2190. Handled = true;
  2191. }
  2192. //---------------------------------------------------------------------------
  2193. void __fastcall TCustomScpExplorerForm::ReloadLocalDirectory(const UnicodeString Directory)
  2194. {
  2195. }
  2196. //---------------------------------------------------------------------------
  2197. void __fastcall TCustomScpExplorerForm::BatchStart(void *& /*Storage*/)
  2198. {
  2199. DebugAssert(FErrorList == NULL);
  2200. if (WinConfiguration->ContinueOnError)
  2201. {
  2202. FErrorList = new TStringList();
  2203. Configuration->Usage->Inc(L"ContinuationsOnError");
  2204. }
  2205. NonVisualDataModule->StartBusy();
  2206. }
  2207. //---------------------------------------------------------------------------
  2208. void __fastcall TCustomScpExplorerForm::BatchEnd(void * /*Storage*/)
  2209. {
  2210. NonVisualDataModule->EndBusy();
  2211. if (FErrorList)
  2212. {
  2213. HandleErrorList(FErrorList);
  2214. }
  2215. }
  2216. //---------------------------------------------------------------------------
  2217. void __fastcall TCustomScpExplorerForm::UpdateCopyParamCounters(
  2218. const TCopyParamType & CopyParam)
  2219. {
  2220. if (!CopyParam.IncludeFileMask.Masks.IsEmpty())
  2221. {
  2222. Configuration->Usage->Inc(L"FileMaskUses");
  2223. }
  2224. if (IsEffectiveFileNameMask(CopyParam.FileMask))
  2225. {
  2226. Configuration->Usage->Inc(L"OperationMaskUses");
  2227. }
  2228. }
  2229. //---------------------------------------------------------------------------
  2230. bool __fastcall TCustomScpExplorerForm::ExecuteFileOperation(TFileOperation Operation,
  2231. TOperationSide Side, TStrings * FileList, bool NoConfirmation, void * Param)
  2232. {
  2233. void * BatchStorage;
  2234. BatchStart(BatchStorage);
  2235. bool Result;
  2236. try
  2237. {
  2238. if ((Operation == foCopy) || (Operation == foMove))
  2239. {
  2240. TTransferDirection Direction = (Side == osLocal ? tdToRemote : tdToLocal);
  2241. TTransferType Type = (Operation == foCopy ? ttCopy : ttMove);
  2242. UnicodeString TargetDirectory;
  2243. bool Temp = false;
  2244. bool DragDrop = false;
  2245. int Options = 0;
  2246. TAutoSwitch UseQueue = asAuto;
  2247. if (Param != NULL)
  2248. {
  2249. TTransferOperationParam& TParam =
  2250. *static_cast<TTransferOperationParam*>(Param);
  2251. TargetDirectory = TParam.TargetDirectory;
  2252. Temp = TParam.Temp;
  2253. DragDrop = TParam.DragDrop;
  2254. Options = TParam.Options;
  2255. UseQueue = TParam.Queue;
  2256. }
  2257. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  2258. switch (UseQueue)
  2259. {
  2260. case asOn:
  2261. CopyParam.Queue = true;
  2262. break;
  2263. case asOff:
  2264. CopyParam.Queue = false;
  2265. break;
  2266. case asAuto:
  2267. default:
  2268. // keep default
  2269. break;
  2270. }
  2271. Result =
  2272. CopyParamDialog(Direction, Type, Temp, FileList, TargetDirectory,
  2273. CopyParam, !NoConfirmation, DragDrop, Options);
  2274. if (Result)
  2275. {
  2276. DebugAssert(Terminal);
  2277. bool SelectionRestored = false;
  2278. TCustomDirView * DView = NULL;
  2279. if (HasDirView[Side])
  2280. {
  2281. DView = DirView(Side);
  2282. DView->SaveSelection();
  2283. DView->SaveSelectedNames();
  2284. }
  2285. UpdateCopyParamCounters(CopyParam);
  2286. std::unique_ptr<TStringList> TransferResumeList(new TStringList());
  2287. DebugAssert(FTransferResumeList == NULL);
  2288. FTransferResumeList =
  2289. Terminal->IsCapable[fcMoveToQueue] ? TransferResumeList.get() : NULL;
  2290. FMoveToQueue = false;
  2291. int Params = FLAGMASK(Operation == foMove, cpDelete);
  2292. try
  2293. {
  2294. TStrings * PermanentFileList;
  2295. std::unique_ptr<TStrings> PermanentFileListOwner;
  2296. try
  2297. {
  2298. if (Side == osLocal)
  2299. {
  2300. PermanentFileList = FileList;
  2301. Params |= FLAGMASK(Temp, cpTemporary);
  2302. Terminal->CopyToRemote(FileList, TargetDirectory, &CopyParam, Params, NULL);
  2303. if (Operation == foMove)
  2304. {
  2305. ReloadLocalDirectory();
  2306. if (DView != NULL)
  2307. {
  2308. DView->RestoreSelection();
  2309. }
  2310. SelectionRestored = true;
  2311. }
  2312. }
  2313. else
  2314. {
  2315. // Clone the file list as it may refer to current directory files,
  2316. // which get destroyed, when the source directory is reloaded after foMove operation.
  2317. // We should actually clone the file list for whole ExecuteFileOperation to protect against reloads.
  2318. // But for a hotfix, we are not going to do such a big change.
  2319. PermanentFileListOwner.reset(TRemoteFileList::CloneStrings(FileList));
  2320. PermanentFileList = PermanentFileListOwner.get();
  2321. try
  2322. {
  2323. Terminal->CopyToLocal(
  2324. FileList, TargetDirectory, &CopyParam, Params, NULL);
  2325. }
  2326. __finally
  2327. {
  2328. if (Operation == foMove)
  2329. {
  2330. if (DView != NULL)
  2331. {
  2332. DView->RestoreSelection();
  2333. }
  2334. SelectionRestored = true;
  2335. }
  2336. ReloadLocalDirectory(TargetDirectory);
  2337. }
  2338. }
  2339. }
  2340. catch (EAbort &)
  2341. {
  2342. if (FMoveToQueue)
  2343. {
  2344. Params |=
  2345. (CopyParam.QueueNoConfirmation ? cpNoConfirmation : 0);
  2346. DebugAssert(CopyParam.TransferSkipList == NULL);
  2347. DebugAssert(CopyParam.TransferResumeFile.IsEmpty());
  2348. if (TransferResumeList->Count > 0)
  2349. {
  2350. CopyParam.TransferResumeFile = TransferResumeList->Strings[TransferResumeList->Count - 1];
  2351. TransferResumeList->Delete(TransferResumeList->Count - 1);
  2352. }
  2353. CopyParam.TransferSkipList = TransferResumeList.release();
  2354. // not really needed, just to keep it consistent with TransferResumeList
  2355. FTransferResumeList = NULL;
  2356. FMoveToQueue = false;
  2357. Configuration->Usage->Inc("MovesToBackground");
  2358. AddQueueItem(Queue, Direction, PermanentFileList, TargetDirectory, CopyParam, Params);
  2359. ClearTransferSourceSelection(Direction);
  2360. }
  2361. throw;
  2362. }
  2363. }
  2364. __finally
  2365. {
  2366. if (!SelectionRestored && (DView != NULL))
  2367. {
  2368. DView->DiscardSavedSelection();
  2369. }
  2370. FTransferResumeList = NULL;
  2371. }
  2372. }
  2373. }
  2374. else if (Operation == foRename)
  2375. {
  2376. DebugAssert(DirView(Side)->ItemFocused);
  2377. DirView(Side)->ItemFocused->EditCaption();
  2378. Result = true;
  2379. }
  2380. else if (Operation == foDelete)
  2381. {
  2382. DebugAssert(FileList->Count);
  2383. // We deliberately do not toggle alternative flag (Param), but use OR,
  2384. // because the Param is set only when command is invoked using Shift-Del/F8 keyboard
  2385. // shortcut of CurrentDeleteAlternativeAction
  2386. bool Alternative =
  2387. bool(Param) || UseAlternativeFunction();
  2388. bool Recycle;
  2389. if (Side == osLocal)
  2390. {
  2391. Recycle = (WinConfiguration->DeleteToRecycleBin != Alternative);
  2392. }
  2393. else
  2394. {
  2395. Recycle = (Terminal->SessionData->DeleteToRecycleBin != Alternative) &&
  2396. !Terminal->SessionData->RecycleBinPath.IsEmpty() &&
  2397. !Terminal->IsRecycledFile(FileList->Strings[0]);
  2398. }
  2399. Result =
  2400. !(Recycle ? WinConfiguration->ConfirmRecycling : WinConfiguration->ConfirmDeleting);
  2401. if (!Result)
  2402. {
  2403. UnicodeString Query;
  2404. if (FileList->Count == 1)
  2405. {
  2406. if (Side == osLocal)
  2407. {
  2408. Query = ExtractFileName(FileList->Strings[0]);
  2409. }
  2410. else
  2411. {
  2412. Query = UnixExtractFileName(FileList->Strings[0]);
  2413. }
  2414. Query = FMTLOAD(
  2415. (Recycle ? CONFIRM_RECYCLE_FILE : CONFIRM_DELETE_FILE), (Query));
  2416. }
  2417. else
  2418. {
  2419. Query = FMTLOAD(
  2420. (Recycle ? CONFIRM_RECYCLE_FILES : CONFIRM_DELETE_FILES), (FileList->Count));
  2421. }
  2422. TMessageParams Params(mpNeverAskAgainCheck);
  2423. Params.ImageName = L"Delete file";
  2424. unsigned int Answer = MessageDialog(MainInstructions(Query), qtConfirmation,
  2425. qaOK | qaCancel, HELP_DELETE_FILE, &Params);
  2426. if (Answer == qaNeverAskAgain)
  2427. {
  2428. Result = true;
  2429. if (Recycle)
  2430. {
  2431. WinConfiguration->ConfirmRecycling = false;
  2432. }
  2433. else
  2434. {
  2435. WinConfiguration->ConfirmDeleting = false;
  2436. }
  2437. }
  2438. else
  2439. {
  2440. Result = (Answer == qaOK);
  2441. }
  2442. }
  2443. if (Result)
  2444. {
  2445. DeleteFiles(Side, FileList, FLAGMASK(Alternative, dfAlternative));
  2446. }
  2447. }
  2448. else if (Operation == foSetProperties)
  2449. {
  2450. RemoteDirView->SaveSelectedNames();
  2451. Result = SetProperties(Side, FileList);
  2452. }
  2453. else if (Operation == foCustomCommand)
  2454. {
  2455. DebugAssert(Param);
  2456. DebugAssert(Side == osRemote);
  2457. RemoteDirView->SaveSelectedNames();
  2458. const TCustomCommandType * Command = static_cast<const TCustomCommandType*>(Param);
  2459. CustomCommand(FileList, *Command, NULL);
  2460. Result = true;
  2461. }
  2462. else if ((Operation == foRemoteMove) || (Operation == foRemoteCopy))
  2463. {
  2464. DebugAssert(Side == osRemote);
  2465. Result = RemoteTransferFiles(FileList, NoConfirmation,
  2466. (Operation == foRemoteMove), reinterpret_cast<TTerminal *>(Param));
  2467. }
  2468. else if (Operation == foLock)
  2469. {
  2470. DebugAssert(Side == osRemote);
  2471. LockFiles(FileList, true);
  2472. Result = true;
  2473. }
  2474. else if (Operation == foUnlock)
  2475. {
  2476. DebugAssert(Side == osRemote);
  2477. LockFiles(FileList, false);
  2478. Result = true;
  2479. }
  2480. else
  2481. {
  2482. DebugFail();
  2483. }
  2484. }
  2485. __finally
  2486. {
  2487. BatchEnd(BatchStorage);
  2488. }
  2489. return Result;
  2490. }
  2491. //---------------------------------------------------------------------------
  2492. TOperationSide __fastcall TCustomScpExplorerForm::GetSide(TOperationSide Side)
  2493. {
  2494. if (Side == osCurrent)
  2495. {
  2496. Side = FCurrentSide;
  2497. }
  2498. return Side;
  2499. }
  2500. //---------------------------------------------------------------------------
  2501. bool __fastcall TCustomScpExplorerForm::ExecuteFileOperation(TFileOperation Operation,
  2502. TOperationSide Side, bool OnFocused, bool NoConfirmation, void * Param)
  2503. {
  2504. Side = GetSide(Side);
  2505. bool Result;
  2506. TStrings * FileList = DirView(Side)->CreateFileList(OnFocused, (Side == osLocal), NULL);
  2507. try
  2508. {
  2509. Result = ExecuteFileOperation(Operation, Side, FileList, NoConfirmation, Param);
  2510. }
  2511. __finally
  2512. {
  2513. delete FileList;
  2514. }
  2515. return Result;
  2516. }
  2517. //---------------------------------------------------------------------------
  2518. void __fastcall TCustomScpExplorerForm::ExecuteFileOperationCommand(
  2519. TFileOperation Operation, TOperationSide Side, bool OnFocused,
  2520. bool NoConfirmation, void * Param)
  2521. {
  2522. if (ExecuteFileOperation(Operation, Side, OnFocused, NoConfirmation, Param))
  2523. {
  2524. if ((Operation == foCopy) || (Operation == foMove))
  2525. {
  2526. if (GetSide(Side) == osLocal)
  2527. {
  2528. Configuration->Usage->Inc(L"UploadsCommand");
  2529. }
  2530. else
  2531. {
  2532. Configuration->Usage->Inc(L"DownloadsCommand");
  2533. }
  2534. }
  2535. }
  2536. }
  2537. //---------------------------------------------------------------------------
  2538. void __fastcall TCustomScpExplorerForm::ExecuteCopyOperationCommand(
  2539. TOperationSide Side, bool OnFocused, unsigned int Flags)
  2540. {
  2541. TTransferOperationParam Param;
  2542. if ((WinConfiguration->Interface != ifCommander) ||
  2543. WinConfiguration->ScpCommander.ExplorerKeyboardShortcuts)
  2544. {
  2545. Flags &= ~cocShortCutHint;
  2546. }
  2547. TCustomDirView * DView = DirView(Side);
  2548. Param.Options =
  2549. FLAGMASK(FLAGSET(Flags, cocShortCutHint), coShortCutHint) |
  2550. FLAGMASK(SelectedAllFilesInDirView(DView), coAllFiles);
  2551. if (FLAGSET(Flags, cocQueue))
  2552. {
  2553. Param.Queue = asOn;
  2554. }
  2555. else if (FLAGSET(Flags, cocNonQueue))
  2556. {
  2557. Param.Queue = asOff;
  2558. }
  2559. ExecuteFileOperationCommand(foCopy, Side, OnFocused, false, &Param);
  2560. }
  2561. //---------------------------------------------------------------------------
  2562. void __fastcall TCustomScpExplorerForm::HandleErrorList(TStringList *& ErrorList)
  2563. {
  2564. try
  2565. {
  2566. if (ErrorList->Count)
  2567. {
  2568. UnicodeString Message = MainInstructions(FMTLOAD(ERROR_LIST_COUNT, (ErrorList->Count)));
  2569. if (MessageDialog(Message, qtError,
  2570. qaOK | qaCancel, HELP_NONE) == qaOK)
  2571. {
  2572. unsigned int Answer;
  2573. int Index = 0;
  2574. do
  2575. {
  2576. DebugAssert(Index >= 0 && Index < ErrorList->Count);
  2577. TQueryButtonAlias Aliases[2];
  2578. Aliases[0].Button = qaYes;
  2579. Aliases[0].Alias = LoadStr(PREV_BUTTON);
  2580. Aliases[1].Button = qaNo;
  2581. Aliases[1].Alias = LoadStr(NEXT_BUTTON);
  2582. TMessageParams Params;
  2583. Params.Aliases = Aliases;
  2584. Params.AliasesCount = LENOF(Aliases);
  2585. Answer = MoreMessageDialog(
  2586. FMTLOAD(ERROR_LIST_NUMBER, (Index+1, ErrorList->Count, ErrorList->Strings[Index])),
  2587. dynamic_cast<TStrings *>(ErrorList->Objects[Index]), qtError,
  2588. (Index ? qaYes : 0) | (Index < ErrorList->Count - 1 ? qaNo : 0) |
  2589. qaOK, HELP_NONE, &Params);
  2590. if (Answer == qaNo)
  2591. {
  2592. Index++;
  2593. }
  2594. if (Answer == qaYes)
  2595. {
  2596. Index--;
  2597. }
  2598. }
  2599. while (Answer != qaOK);
  2600. }
  2601. }
  2602. }
  2603. __finally
  2604. {
  2605. TStrings * List = ErrorList;
  2606. ErrorList = NULL;
  2607. for (int i = 0; i < List->Count; i++)
  2608. {
  2609. delete List->Objects[i];
  2610. }
  2611. delete List;
  2612. }
  2613. }
  2614. //---------------------------------------------------------------------------
  2615. void __fastcall TCustomScpExplorerForm::ExecuteRemoteFile(
  2616. const UnicodeString & FullFileName, TRemoteFile * File, TExecuteFileBy ExecuteFileBy)
  2617. {
  2618. // needed for checking filemasks, as there's no directory object
  2619. // associated with the file object
  2620. File->FullFileName = FullFileName;
  2621. TFileMasks::TParams MaskParams;
  2622. MaskParams.Size = File->Size;
  2623. MaskParams.Modification = File->Modification;
  2624. ExecuteFile(osRemote, ExecuteFileBy, NULL, FullFileName, File, MaskParams);
  2625. }
  2626. //---------------------------------------------------------------------------
  2627. void __fastcall TCustomScpExplorerForm::EditNew(TOperationSide Side)
  2628. {
  2629. DebugAssert(!WinConfiguration->DisableOpenEdit);
  2630. Side = GetSide(Side);
  2631. TCustomDirView * DView = DirView(Side);
  2632. TListItem * FocusedItem = DView->ItemFocused;
  2633. UnicodeString Name;
  2634. if ((FocusedItem != NULL) && !DView->ItemIsParentDirectory(FocusedItem))
  2635. {
  2636. Name = DView->ItemFileName(FocusedItem);
  2637. }
  2638. else
  2639. {
  2640. Name = LoadStr(NEW_FILE);
  2641. }
  2642. UnicodeString Names = Name;
  2643. std::unique_ptr<TStrings> History(CloneStrings(CustomWinConfiguration->History[L"EditFile"]));
  2644. if (InputDialog(LoadStr(EDIT_FILE_CAPTION), LoadStr(EDIT_FILE_PROMPT), Names,
  2645. HELP_EDIT_NEW, History.get(), true))
  2646. {
  2647. while (!Names.IsEmpty())
  2648. {
  2649. Name = CutToChar(Names, FileMasksDelimiters[1], false);
  2650. CustomWinConfiguration->History[L"EditFile"] = History.get();
  2651. UnicodeString TargetFileName;
  2652. UnicodeString LocalFileName;
  2653. UnicodeString RootTempDir;
  2654. UnicodeString TempDir;
  2655. UnicodeString RemoteDirectory;
  2656. bool ExistingFile = false;
  2657. if (Side == osRemote)
  2658. {
  2659. Name = AbsolutePath(FTerminal->CurrentDirectory, Name);
  2660. TRemoteFile * File = NULL;
  2661. if (FTerminal->FileExists(Name, &File))
  2662. {
  2663. try
  2664. {
  2665. ExecuteRemoteFile(Name, File, efDefaultEditor);
  2666. ExistingFile = true;
  2667. }
  2668. __finally
  2669. {
  2670. delete File;
  2671. }
  2672. }
  2673. if (!ExistingFile)
  2674. {
  2675. RemoteDirectory = UnixExtractFilePath(Name);
  2676. TemporaryDirectoryForRemoteFiles(
  2677. RemoteDirectory, GUIConfiguration->CurrentCopyParam, TempDir, RootTempDir);
  2678. TargetFileName = UnixExtractFileName(Name);
  2679. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  2680. LocalFileName = TempDir +
  2681. // We probably do not want to trim the VMS version here
  2682. FTerminal->ChangeFileName(&CopyParam, TargetFileName, osRemote, false);
  2683. }
  2684. }
  2685. else
  2686. {
  2687. if (ExtractFilePath(Name).IsEmpty())
  2688. {
  2689. LocalFileName = IncludeTrailingBackslash(DView->PathName) + Name;
  2690. }
  2691. else
  2692. {
  2693. LocalFileName = ExpandFileName(Name);
  2694. }
  2695. TargetFileName = ExtractFileName(Name);
  2696. }
  2697. if (!ExistingFile)
  2698. {
  2699. if (!FileExists(ApiPath(LocalFileName)))
  2700. {
  2701. int File = FileCreate(ApiPath(LocalFileName));
  2702. if (File < 0)
  2703. {
  2704. if (!RootTempDir.IsEmpty())
  2705. {
  2706. RecursiveDeleteFile(ExcludeTrailingBackslash(RootTempDir), false);
  2707. }
  2708. throw Exception(FMTLOAD(CREATE_FILE_ERROR, (LocalFileName)));
  2709. }
  2710. else
  2711. {
  2712. FileClose(File);
  2713. }
  2714. }
  2715. TExecuteFileBy ExecuteFileBy = efDefaultEditor;
  2716. const TEditorData * ExternalEditor = NULL;
  2717. TFileMasks::TParams MaskParams; // size not known
  2718. ExecuteFileNormalize(ExecuteFileBy, ExternalEditor, TargetFileName,
  2719. false, MaskParams);
  2720. CustomExecuteFile(Side, ExecuteFileBy, LocalFileName, TargetFileName,
  2721. ExternalEditor, RootTempDir, RemoteDirectory);
  2722. }
  2723. }
  2724. }
  2725. }
  2726. //---------------------------------------------------------------------------
  2727. bool __fastcall TCustomScpExplorerForm::RemoteExecuteForceText(
  2728. TExecuteFileBy ExecuteFileBy, const TEditorData * ExternalEditor)
  2729. {
  2730. DebugAssert((ExecuteFileBy == efExternalEditor) ==
  2731. ((ExternalEditor != NULL) && (ExternalEditor->Editor == edExternal)));
  2732. DebugAssert(ExecuteFileBy != efDefaultEditor);
  2733. return
  2734. ((ExecuteFileBy == efInternalEditor)) ||
  2735. ((ExecuteFileBy == efExternalEditor) && ExternalEditor->ExternalEditorText);
  2736. }
  2737. //---------------------------------------------------------------------------
  2738. void __fastcall TCustomScpExplorerForm::CustomExecuteFile(TOperationSide Side,
  2739. TExecuteFileBy ExecuteFileBy, UnicodeString FileName, UnicodeString OriginalFileName,
  2740. const TEditorData * ExternalEditor, UnicodeString LocalRootDirectory,
  2741. UnicodeString RemoteDirectory)
  2742. {
  2743. DebugAssert(!WinConfiguration->DisableOpenEdit);
  2744. DebugAssert((ExecuteFileBy == efExternalEditor) ==
  2745. ((ExternalEditor != NULL) && (ExternalEditor->Editor == edExternal)));
  2746. DebugAssert(ExecuteFileBy != efDefaultEditor);
  2747. Side = GetSide(Side);
  2748. std::unique_ptr<TEditedFileData> Data(new TEditedFileData);
  2749. if (Side == osRemote)
  2750. {
  2751. Data->Terminal = Terminal;
  2752. Data->Queue = Queue;
  2753. Data->SessionData = CloneCurrentSessionData();
  2754. Data->ForceText = RemoteExecuteForceText(ExecuteFileBy, ExternalEditor);
  2755. Data->RemoteDirectory = RemoteDirectory;
  2756. Data->SessionName = Terminal->SessionData->SessionName;
  2757. Data->LocalRootDirectory = LocalRootDirectory;
  2758. Data->OriginalFileName = OriginalFileName;
  2759. Data->Command = L""; // will be changed later for external editor
  2760. }
  2761. if (ExecuteFileBy == efInternalEditor)
  2762. {
  2763. if (Side == osRemote)
  2764. {
  2765. UnicodeString Caption = UnixIncludeTrailingBackslash(RemoteDirectory) + OriginalFileName +
  2766. L" - " + Terminal->SessionData->SessionName;
  2767. TForm * Editor;
  2768. try
  2769. {
  2770. Editor = ShowEditorForm(FileName, this, FEditorManager->FileChanged,
  2771. FEditorManager->FileReload, FEditorManager->FileClosed,
  2772. SaveAllInternalEditors, AnyInternalEditorModified,
  2773. Caption, FStandaloneEditing, SessionColor, Terminal->SessionData->InternalEditorEncoding);
  2774. }
  2775. catch(...)
  2776. {
  2777. if (!LocalRootDirectory.IsEmpty())
  2778. {
  2779. RecursiveDeleteFile(ExcludeTrailingBackslash(LocalRootDirectory), false);
  2780. }
  2781. throw;
  2782. }
  2783. FEditorManager->AddFileInternal(FileName, Data.release(), Editor);
  2784. }
  2785. else
  2786. {
  2787. DebugAssert(!FStandaloneEditing);
  2788. TForm * Editor =
  2789. ShowEditorForm(FileName, this, NULL, NULL, LocalEditorClosed,
  2790. SaveAllInternalEditors, AnyInternalEditorModified,
  2791. L"", false, SessionColor, -1);
  2792. FLocalEditors->Add(Editor);
  2793. }
  2794. }
  2795. else
  2796. {
  2797. HANDLE Process;
  2798. if (ExecuteFileBy == efExternalEditor)
  2799. {
  2800. UnicodeString Program, Params, Dir;
  2801. Data->Command = ExternalEditor->ExternalEditor;
  2802. ReformatFileNameCommand(Data->Command);
  2803. SplitCommand(Data->Command, Program, Params, Dir);
  2804. Params = ExpandFileNameCommand(Params, FileName);
  2805. Program = ExpandEnvironmentVariables(Program);
  2806. if (!ExecuteShell(Program, Params, Process))
  2807. {
  2808. throw EOSExtException(FMTLOAD(EDITOR_ERROR, (Program)));
  2809. }
  2810. }
  2811. else
  2812. {
  2813. DebugAssert(Side == osRemote);
  2814. if (!ExecuteShell(FileName, L"", Process))
  2815. {
  2816. throw EOSExtException(FMTLOAD(EXECUTE_FILE_ERROR, (FileName)));
  2817. }
  2818. }
  2819. if ((Side == osLocal) ||
  2820. ((ExecuteFileBy == efShell) &&
  2821. !WinConfiguration->Editor.SDIShellEditor) ||
  2822. ((ExecuteFileBy == efExternalEditor) &&
  2823. !ExternalEditor->SDIExternalEditor))
  2824. {
  2825. // no need for handle
  2826. if (Process != NULL)
  2827. {
  2828. DebugCheck(CloseHandle(Process));
  2829. }
  2830. Process = INVALID_HANDLE_VALUE;
  2831. }
  2832. else
  2833. {
  2834. if (Process == NULL)
  2835. {
  2836. throw ExtException(LoadStr(OPEN_FILE_NO_PROCESS2), L"", HELP_OPEN_FILE_NO_PROCESS);
  2837. }
  2838. }
  2839. if (Side == osRemote)
  2840. {
  2841. FEditorManager->AddFileExternal(FileName, Data.release(), Process);
  2842. }
  2843. }
  2844. }
  2845. //---------------------------------------------------------------------------
  2846. void __fastcall TCustomScpExplorerForm::SaveInternalEditor(
  2847. const UnicodeString /*FileName*/, TEditedFileData * /*Data*/, TObject * Token,
  2848. void * /*Arg*/)
  2849. {
  2850. if (Token != NULL)
  2851. {
  2852. EditorFormFileSave(static_cast<TForm *>(Token));
  2853. }
  2854. }
  2855. //---------------------------------------------------------------------------
  2856. void __fastcall TCustomScpExplorerForm::SaveAllInternalEditors(TObject * /*Sender*/)
  2857. {
  2858. for (int Index = 0; Index < FLocalEditors->Count; Index++)
  2859. {
  2860. EditorFormFileSave(static_cast<TForm *>(FLocalEditors->Items[Index]));
  2861. }
  2862. FEditorManager->ProcessFiles(SaveInternalEditor, NULL);
  2863. }
  2864. //---------------------------------------------------------------------------
  2865. void __fastcall TCustomScpExplorerForm::InternalEditorModified(
  2866. const UnicodeString /*FileName*/, TEditedFileData * /*Data*/, TObject * Token,
  2867. void * Arg)
  2868. {
  2869. if ((Token != NULL) &&
  2870. IsEditorFormModified(static_cast<TForm *>(Token)))
  2871. {
  2872. *static_cast<bool *>(Arg) = true;
  2873. }
  2874. }
  2875. //---------------------------------------------------------------------------
  2876. void __fastcall TCustomScpExplorerForm::AnyInternalEditorModified(
  2877. TObject * /*Sender*/, bool & Modified)
  2878. {
  2879. for (int Index = 0; !Modified && (Index < FLocalEditors->Count); Index++)
  2880. {
  2881. if (IsEditorFormModified(static_cast<TForm *>(FLocalEditors->Items[Index])))
  2882. {
  2883. Modified = true;
  2884. }
  2885. }
  2886. if (!Modified)
  2887. {
  2888. FEditorManager->ProcessFiles(InternalEditorModified, &Modified);
  2889. }
  2890. }
  2891. //---------------------------------------------------------------------------
  2892. void __fastcall TCustomScpExplorerForm::LocalEditorClosed(TObject * Sender, bool /*Forced*/)
  2893. {
  2894. DebugCheck(FLocalEditors->Extract(Sender) >= 0);
  2895. }
  2896. //---------------------------------------------------------------------------
  2897. void __fastcall TCustomScpExplorerForm::TemporaryDirectoryForRemoteFiles(
  2898. UnicodeString RemoteDirectory, TCopyParamType CopyParam,
  2899. UnicodeString & Result, UnicodeString & RootDirectory)
  2900. {
  2901. if (!WinConfiguration->TemporaryDirectoryDeterministic)
  2902. {
  2903. RootDirectory = IncludeTrailingBackslash(WinConfiguration->TemporaryDir());
  2904. Result = RootDirectory;
  2905. }
  2906. else
  2907. {
  2908. RootDirectory = L"";
  2909. Result = WinConfiguration->ExpandedTemporaryDirectory();
  2910. Result = IncludeTrailingBackslash(Result);
  2911. }
  2912. if (WinConfiguration->TemporaryDirectoryAppendSession)
  2913. {
  2914. Result = IncludeTrailingBackslash(Result + MakeValidFileName(Terminal->SessionData->SessionName));
  2915. }
  2916. if (WinConfiguration->TemporaryDirectoryAppendPath)
  2917. {
  2918. if (!RemoteDirectory.IsEmpty() && (RemoteDirectory[1] == L'/'))
  2919. {
  2920. RemoteDirectory.Delete(1, 1);
  2921. }
  2922. Result = IncludeTrailingBackslash(Result + CopyParam.ValidLocalPath(FromUnixPath(RemoteDirectory)));
  2923. }
  2924. if (!ForceDirectories(ApiPath(Result)))
  2925. {
  2926. throw EOSExtException(FMTLOAD(CREATE_TEMP_DIR_ERROR, (Result)));
  2927. }
  2928. }
  2929. //---------------------------------------------------------------------------
  2930. void __fastcall TCustomScpExplorerForm::TemporarilyDownloadFiles(
  2931. TStrings * FileList, bool ForceText, UnicodeString & RootTempDir, UnicodeString & TempDir,
  2932. bool AllFiles, bool GetTargetNames, bool AutoOperation)
  2933. {
  2934. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  2935. if (ForceText)
  2936. {
  2937. CopyParam.TransferMode = tmAscii;
  2938. }
  2939. TemporaryFileCopyParam(CopyParam);
  2940. if (AllFiles)
  2941. {
  2942. CopyParam.IncludeFileMask = TFileMasks();
  2943. }
  2944. if (TempDir.IsEmpty())
  2945. {
  2946. TemporaryDirectoryForRemoteFiles(FTerminal->CurrentDirectory, CopyParam, TempDir, RootTempDir);
  2947. }
  2948. DebugAssert(!FAutoOperation);
  2949. FAutoOperation = AutoOperation;
  2950. Terminal->ExceptionOnFail = true;
  2951. try
  2952. {
  2953. try
  2954. {
  2955. // turn off confirmations, as for MDI editors we may possibly download
  2956. // the same file over
  2957. Terminal->CopyToLocal(
  2958. FileList, TempDir, &CopyParam, cpNoConfirmation | cpTemporary, NULL);
  2959. if (GetTargetNames)
  2960. {
  2961. for (int i = 0; i < FileList->Count; i++)
  2962. {
  2963. FileList->Strings[i] =
  2964. Terminal->ChangeFileName(&CopyParam, UnixExtractFileName(FileList->Strings[i]), osRemote, false);
  2965. }
  2966. }
  2967. }
  2968. catch(...)
  2969. {
  2970. if (!RootTempDir.IsEmpty())
  2971. {
  2972. RecursiveDeleteFile(ExcludeTrailingBackslash(RootTempDir), false);
  2973. }
  2974. throw;
  2975. }
  2976. }
  2977. __finally
  2978. {
  2979. FAutoOperation = false;
  2980. Terminal->ExceptionOnFail = false;
  2981. }
  2982. }
  2983. //---------------------------------------------------------------------------
  2984. void __fastcall TCustomScpExplorerForm::EditorAutoConfig()
  2985. {
  2986. // Do not waste time checking for default editor list the next time,
  2987. // or testing if default editor is not notepad.
  2988. bool TryNextTime = false;
  2989. UnicodeString UsageState;
  2990. if (!WinConfiguration->EditorList->IsDefaultList())
  2991. {
  2992. UsageState = "H";
  2993. }
  2994. else
  2995. {
  2996. UnicodeString Executable;
  2997. UnicodeString ExecutableDescription;
  2998. if (DetectSystemExternalEditor(false, Executable, ExecutableDescription, UsageState, TryNextTime))
  2999. {
  3000. UnicodeString Message =
  3001. FMTLOAD(EDITOR_AUTO_CONFIG2, (ExecutableDescription, ExecutableDescription));
  3002. unsigned int Answer =
  3003. MessageDialog(Message, qtConfirmation, qaOK | qaCancel, HELP_EDITOR_AUTO_CONFIG);
  3004. if (Answer != qaOK)
  3005. {
  3006. UsageState = "R";
  3007. }
  3008. else
  3009. {
  3010. UsageState = "A";
  3011. TEditorData EditorData;
  3012. EditorData.Editor = edExternal;
  3013. EditorData.ExternalEditor = FormatCommand(Executable, L"");
  3014. EditorData.DecideExternalEditorText();
  3015. TEditorList EditorList;
  3016. EditorList = *WinConfiguration->EditorList;
  3017. EditorList.Insert(0, new TEditorPreferences(EditorData));
  3018. WinConfiguration->EditorList = &EditorList;
  3019. }
  3020. }
  3021. }
  3022. WinConfiguration->OfferedEditorAutoConfig = !TryNextTime;
  3023. WinConfiguration->Usage->Set(L"EditorAutoConfig", UsageState);
  3024. }
  3025. //---------------------------------------------------------------------------
  3026. void __fastcall TCustomScpExplorerForm::ExecuteFileNormalize(
  3027. TExecuteFileBy & ExecuteFileBy, const TEditorData *& ExternalEditor,
  3028. const UnicodeString & FileName, bool Local, const TFileMasks::TParams & MaskParams)
  3029. {
  3030. if (ExecuteFileBy == efDefaultEditor)
  3031. {
  3032. if (!WinConfiguration->OfferedEditorAutoConfig)
  3033. {
  3034. EditorAutoConfig();
  3035. }
  3036. const TEditorPreferences * Editor =
  3037. WinConfiguration->DefaultEditorForFile(FileName, Local, MaskParams);
  3038. if ((Editor == NULL) || (Editor->Data->Editor == edInternal))
  3039. {
  3040. ExecuteFileBy = efInternalEditor;
  3041. ExternalEditor = NULL;
  3042. }
  3043. else if (Editor->Data->Editor == edOpen)
  3044. {
  3045. ExecuteFileBy = efShell;
  3046. ExternalEditor = NULL;
  3047. }
  3048. else
  3049. {
  3050. ExecuteFileBy = efExternalEditor;
  3051. ExternalEditor = Editor->Data;
  3052. }
  3053. }
  3054. }
  3055. //---------------------------------------------------------------------------
  3056. void __fastcall TCustomScpExplorerForm::ExecuteFile(TOperationSide Side,
  3057. TExecuteFileBy ExecuteFileBy, const TEditorData * ExternalEditor,
  3058. UnicodeString FullFileName, TObject * Object, const TFileMasks::TParams & MaskParams)
  3059. {
  3060. UnicodeString OriginalFileName;
  3061. UnicodeString LocalRootDirectory;
  3062. UnicodeString RemoteDirectory;
  3063. ExecuteFileNormalize(ExecuteFileBy, ExternalEditor, FullFileName,
  3064. (Side == osLocal), MaskParams);
  3065. UnicodeString Counter;
  3066. UnicodeString LocalFileName;
  3067. bool Handled = false;
  3068. if (Side == osRemote)
  3069. {
  3070. // We need to trim VMS version here, so that we use name without version
  3071. // when uploading back to create a new version of the file
  3072. OriginalFileName = FTerminal->GetBaseFileName(UnixExtractFileName(FullFileName));
  3073. RemoteDirectory = UnixExtractFilePath(FullFileName);
  3074. TObject * Token = NULL;
  3075. UnicodeString LocalDirectory;
  3076. if (!FEditorManager->CanAddFile(RemoteDirectory, OriginalFileName,
  3077. Terminal->SessionData->SessionName, Token, LocalRootDirectory,
  3078. LocalDirectory))
  3079. {
  3080. if (Token != NULL)
  3081. {
  3082. TForm * Form = dynamic_cast<TForm *>(Token);
  3083. if (Form->WindowState == wsMinimized)
  3084. {
  3085. ShowWindow(Form->Handle, SW_RESTORE);
  3086. }
  3087. else
  3088. {
  3089. Form->SetFocus();
  3090. }
  3091. Handled = true;
  3092. }
  3093. else
  3094. {
  3095. throw Exception(FMTLOAD(ALREADY_EDITED_EXTERNALLY_OR_UPLOADED, (OriginalFileName)));
  3096. }
  3097. }
  3098. if (!Handled)
  3099. {
  3100. TStringList * FileList1 = new TStringList();
  3101. try
  3102. {
  3103. FileList1->AddObject(FullFileName, Object);
  3104. TemporarilyDownloadFiles(FileList1,
  3105. RemoteExecuteForceText(ExecuteFileBy, ExternalEditor),
  3106. LocalRootDirectory, LocalDirectory, true, true, true);
  3107. LocalFileName = LocalDirectory + FileList1->Strings[0];
  3108. }
  3109. __finally
  3110. {
  3111. delete FileList1;
  3112. }
  3113. switch (ExecuteFileBy)
  3114. {
  3115. case efShell:
  3116. Counter = "RemoteFilesExecuted";
  3117. break;
  3118. case efInternalEditor:
  3119. Counter = "RemoteFilesOpenedInInternalEditor";
  3120. break;
  3121. case efExternalEditor:
  3122. Counter = "RemoteFilesOpenedInExternalEditor";
  3123. break;
  3124. default:
  3125. DebugFail();
  3126. }
  3127. }
  3128. }
  3129. else
  3130. {
  3131. LocalFileName = FullFileName;
  3132. OriginalFileName = ExtractFileName(FullFileName);
  3133. switch (ExecuteFileBy)
  3134. {
  3135. case efShell:
  3136. Counter = "LocalFilesExecuted";
  3137. break;
  3138. case efInternalEditor:
  3139. Counter = "LocalFilesOpenedInInternalEditor";
  3140. break;
  3141. case efExternalEditor:
  3142. Counter = "LocalFilesOpenedInExternalEditor";
  3143. break;
  3144. default:
  3145. DebugFail();
  3146. }
  3147. }
  3148. if (!Handled)
  3149. {
  3150. Configuration->Usage->Inc(Counter);
  3151. CustomExecuteFile(Side, ExecuteFileBy, LocalFileName, OriginalFileName,
  3152. ExternalEditor, LocalRootDirectory, RemoteDirectory);
  3153. }
  3154. }
  3155. //---------------------------------------------------------------------------
  3156. void __fastcall TCustomScpExplorerForm::ExecuteFile(TOperationSide Side,
  3157. TExecuteFileBy ExecuteFileBy, const TEditorData * ExternalEditor,
  3158. bool AllSelected, bool OnFocused)
  3159. {
  3160. DebugAssert(!WinConfiguration->DisableOpenEdit);
  3161. DebugAssert((ExecuteFileBy == efExternalEditor) ==
  3162. ((ExternalEditor != NULL) && (ExternalEditor->Editor == edExternal)));
  3163. Side = GetSide(Side);
  3164. TCustomDirView * DView = DirView(Side);
  3165. TStrings * FileList = AllSelected ?
  3166. DView->CreateFileList(OnFocused, Side == osLocal) :
  3167. DView->CreateFocusedFileList(Side == osLocal);
  3168. try
  3169. {
  3170. DebugAssert(AllSelected || (FileList->Count == 1));
  3171. for (int i = 0; i < FileList->Count; i++)
  3172. {
  3173. UnicodeString ListFileName = FileList->Strings[i];
  3174. UnicodeString FileNameOnly = (Side == osRemote) ?
  3175. UnixExtractFileName(ListFileName) : ExtractFileName(ListFileName);
  3176. TListItem * Item = DView->FindFileItem(FileNameOnly);
  3177. if (!DView->ItemIsDirectory(Item))
  3178. {
  3179. UnicodeString FullFileName;
  3180. if (Side == osRemote)
  3181. {
  3182. FullFileName = RemoteDirView->Path + ListFileName;
  3183. }
  3184. else
  3185. {
  3186. FullFileName = ListFileName;
  3187. }
  3188. TFileMasks::TParams MaskParams;
  3189. MaskParams.Size = DView->ItemFileSize(Item);
  3190. TDateTimePrecision Precision;
  3191. MaskParams.Modification = DView->ItemFileTime(Item, Precision);
  3192. ExecuteFile(Side, ExecuteFileBy, ExternalEditor, FullFileName,
  3193. FileList->Objects[i], MaskParams);
  3194. }
  3195. }
  3196. }
  3197. __finally
  3198. {
  3199. delete FileList;
  3200. }
  3201. }
  3202. //---------------------------------------------------------------------------
  3203. void __fastcall TCustomScpExplorerForm::TemporaryFileCopyParam(TCopyParamType & CopyParam)
  3204. {
  3205. // do not forget to add additional options to TemporarilyDownloadFiles, and AS
  3206. CopyParam.FileNameCase = ncNoChange;
  3207. CopyParam.PreserveRights = false;
  3208. CopyParam.PreserveReadOnly = false;
  3209. CopyParam.ReplaceInvalidChars = true;
  3210. CopyParam.IncludeFileMask = TFileMasks();
  3211. CopyParam.NewerOnly = false;
  3212. CopyParam.FileMask = L"";
  3213. }
  3214. //---------------------------------------------------------------------------
  3215. void __fastcall TCustomScpExplorerForm::ExecutedFileChanged(const UnicodeString FileName,
  3216. TEditedFileData * Data, HANDLE UploadCompleteEvent)
  3217. {
  3218. TTerminalManager * Manager = TTerminalManager::Instance();
  3219. if ((Data->Terminal == NULL) || !Data->Terminal->Active)
  3220. {
  3221. if (!NonVisualDataModule->Busy)
  3222. {
  3223. UnicodeString FileNameOnly = ExtractFileName(FileName);
  3224. UnicodeString EditInactiveSessionReopenAcceptedCounter = L"EditInactiveSessionReopenAccepted";
  3225. UnicodeString EditInactiveSessionReopenRejectedCounter = L"EditInactiveSessionReopenRejected";
  3226. if (Data->Terminal == NULL)
  3227. {
  3228. TTerminal * SameSiteTerminal = Manager->FindActiveTerminalForSite(Data->SessionData);
  3229. if (SameSiteTerminal != NULL)
  3230. {
  3231. UnicodeString Message =
  3232. FMTLOAD(EDIT_SESSION_REATTACH,
  3233. (FileNameOnly, Data->SessionName, FileNameOnly));
  3234. if (MessageDialog(Message, qtConfirmation, qaOK | qaCancel) == qaOK)
  3235. {
  3236. Data->Terminal = Terminal;
  3237. Data->Queue = Manager->FindQueueForTerminal(Terminal);
  3238. Data->SessionName = Terminal->SessionData->SessionName;
  3239. // We might also overwrite session data
  3240. Configuration->Usage->Inc(EditInactiveSessionReopenAcceptedCounter);
  3241. }
  3242. else
  3243. {
  3244. Configuration->Usage->Inc(EditInactiveSessionReopenRejectedCounter);
  3245. Abort();
  3246. }
  3247. }
  3248. }
  3249. // foreground session should reconnect itself
  3250. else if (Terminal != Data->Terminal)
  3251. {
  3252. UnicodeString Message =
  3253. MainInstructions(
  3254. FMTLOAD(EDIT_SESSION_RECONNECT, (Data->SessionName, FileNameOnly)));
  3255. if (MessageDialog(Message, qtConfirmation, qaOK | qaCancel) == qaOK)
  3256. {
  3257. Manager->SetActiveTerminalWithAutoReconnect(Data->Terminal);
  3258. Configuration->Usage->Inc(EditInactiveSessionReopenAcceptedCounter);
  3259. }
  3260. else
  3261. {
  3262. Configuration->Usage->Inc(EditInactiveSessionReopenRejectedCounter);
  3263. Abort();
  3264. }
  3265. }
  3266. }
  3267. if ((Data->Terminal == NULL) || !Data->Terminal->Active)
  3268. {
  3269. Configuration->Usage->Inc(L"EditInactiveSession");
  3270. // Prevent this when not idle (!NonVisualDataModule->Busy)?
  3271. throw Exception(FMTLOAD(EDIT_SESSION_CLOSED2,
  3272. (ExtractFileName(FileName), Data->SessionName)));
  3273. }
  3274. }
  3275. TStrings * FileList = new TStringList();
  3276. try
  3277. {
  3278. FileList->Add(FileName);
  3279. // consider using the same settings (preset) as when the file was downloaded
  3280. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  3281. TemporaryFileCopyParam(CopyParam);
  3282. if (Data->ForceText)
  3283. {
  3284. CopyParam.TransferMode = tmAscii;
  3285. }
  3286. // so i do not need to worry if masking algorithm works in all cases
  3287. // ("" means "copy file name", no masking is actually done)
  3288. if (ExtractFileName(FileName) == Data->OriginalFileName)
  3289. {
  3290. CopyParam.FileMask = L"";
  3291. }
  3292. else
  3293. {
  3294. CopyParam.FileMask = DelimitFileNameMask(Data->OriginalFileName);
  3295. }
  3296. DebugAssert(Data->Queue != NULL);
  3297. int Params = cpNoConfirmation | cpTemporary;
  3298. TQueueItem * QueueItem = new TUploadQueueItem(Data->Terminal, FileList,
  3299. Data->RemoteDirectory, &CopyParam, Params, true, false);
  3300. QueueItem->CompleteEvent = UploadCompleteEvent;
  3301. AddQueueItem(Data->Queue, QueueItem, Data->Terminal);
  3302. }
  3303. __finally
  3304. {
  3305. delete FileList;
  3306. }
  3307. }
  3308. //---------------------------------------------------------------------------
  3309. void __fastcall TCustomScpExplorerForm::ExecutedFileReload(
  3310. const UnicodeString FileName, const TEditedFileData * Data)
  3311. {
  3312. // Sanity check, we should not be busy otherwise user would not be able to click Reload button.
  3313. DebugAssert(!NonVisualDataModule->Busy);
  3314. if ((Data->Terminal == NULL) || !Data->Terminal->Active)
  3315. {
  3316. throw Exception(FMTLOAD(EDIT_SESSION_CLOSED_RELOAD,
  3317. (ExtractFileName(FileName), Data->SessionName)));
  3318. }
  3319. TTerminal * PrevTerminal = TTerminalManager::Instance()->ActiveTerminal;
  3320. TTerminalManager::Instance()->ActiveTerminal = Data->Terminal;
  3321. NonVisualDataModule->StartBusy();
  3322. try
  3323. {
  3324. std::unique_ptr<TRemoteFile> File;
  3325. UnicodeString RemoteFileName =
  3326. UnixIncludeTrailingBackslash(Data->RemoteDirectory) + Data->OriginalFileName;
  3327. FTerminal->ExceptionOnFail = true;
  3328. try
  3329. {
  3330. TRemoteFile * AFile = NULL;
  3331. FTerminal->ReadFile(RemoteFileName, AFile);
  3332. File.reset(AFile);
  3333. if (!File->HaveFullFileName)
  3334. {
  3335. File->FullFileName = RemoteFileName;
  3336. }
  3337. }
  3338. __finally
  3339. {
  3340. FTerminal->ExceptionOnFail = false;
  3341. }
  3342. std::unique_ptr<TStrings> FileList(new TStringList());
  3343. FileList->AddObject(RemoteFileName, File.get());
  3344. UnicodeString RootTempDir = Data->LocalRootDirectory;
  3345. UnicodeString TempDir = ExtractFilePath(FileName);
  3346. TemporarilyDownloadFiles(FileList.get(), Data->ForceText, RootTempDir,
  3347. TempDir, true, true, true);
  3348. // sanity check, the target file name should be still the same
  3349. DebugAssert(ExtractFileName(FileName) == FileList->Strings[0]);
  3350. }
  3351. __finally
  3352. {
  3353. NonVisualDataModule->EndBusy();
  3354. // it actually may not exist anymore...
  3355. TTerminalManager::Instance()->ActiveTerminal = PrevTerminal;
  3356. }
  3357. }
  3358. //---------------------------------------------------------------------------
  3359. void __fastcall TCustomScpExplorerForm::ExecutedFileEarlyClosed(
  3360. const TEditedFileData * Data, bool & KeepOpen)
  3361. {
  3362. // Command is set for external editors only (not for "shell" open).
  3363. if (!Data->Command.IsEmpty())
  3364. {
  3365. bool AnyFound = false;
  3366. bool AnyMDI = false;
  3367. bool AnyNonMDI = false;
  3368. bool AnyDetect = false;
  3369. TEditorList * EditorList = new TEditorList();
  3370. try
  3371. {
  3372. *EditorList = *WinConfiguration->EditorList;
  3373. for (int i = 0; i < EditorList->Count; i++)
  3374. {
  3375. const TEditorPreferences * Editor = EditorList->Editors[i];
  3376. if ((Editor->Data->Editor == edExternal) &&
  3377. (Editor->Data->ExternalEditor == Data->Command))
  3378. {
  3379. AnyFound = true;
  3380. if (Editor->Data->SDIExternalEditor)
  3381. {
  3382. AnyNonMDI = true;
  3383. if (Editor->Data->DetectMDIExternalEditor)
  3384. {
  3385. AnyDetect = true;
  3386. }
  3387. }
  3388. else
  3389. {
  3390. AnyMDI = true;
  3391. }
  3392. }
  3393. }
  3394. bool EnableMDI = false;
  3395. bool DisableDetect = false;
  3396. if (AnyMDI)
  3397. {
  3398. KeepOpen = true;
  3399. if (AnyNonMDI)
  3400. {
  3401. // there is at least one instance of the editor with MDI support enabled,
  3402. // and one with disabled, enable it for all instances
  3403. EnableMDI = true;
  3404. }
  3405. }
  3406. else if (AnyFound && !AnyDetect)
  3407. {
  3408. // at least once instance found but all have MDI autodetection disabled
  3409. // => close the file (default action)
  3410. }
  3411. else
  3412. {
  3413. // no instance of the editor has MDI support enabled
  3414. TMessageParams Params;
  3415. if (AnyFound)
  3416. {
  3417. // there is at least one instance of the editor
  3418. Params.Params |= mpNeverAskAgainCheck;
  3419. }
  3420. unsigned int Answer = MessageDialog(FMTLOAD(EDITOR_EARLY_CLOSED2, (Data->OriginalFileName)), qtWarning,
  3421. qaYes | qaNo, HELP_EDITOR_EARLY_CLOSED, &Params);
  3422. switch (Answer)
  3423. {
  3424. case qaNeverAskAgain:
  3425. DisableDetect = true;
  3426. break;
  3427. case qaNo:
  3428. EnableMDI = true;
  3429. KeepOpen = true;
  3430. break;
  3431. }
  3432. }
  3433. if (AnyFound && (EnableMDI || DisableDetect))
  3434. {
  3435. bool Changed = false;
  3436. for (int i = 0; i < EditorList->Count; i++)
  3437. {
  3438. const TEditorPreferences * Editor = EditorList->Editors[i];
  3439. if ((Editor->Data->Editor == edExternal) &&
  3440. (Editor->Data->ExternalEditor == Data->Command) &&
  3441. ((EnableMDI && Editor->Data->SDIExternalEditor) ||
  3442. (DisableDetect && Editor->Data->DetectMDIExternalEditor)))
  3443. {
  3444. Changed = true;
  3445. TEditorPreferences * UpdatedEditor = new TEditorPreferences(*Editor);
  3446. if (EnableMDI)
  3447. {
  3448. UpdatedEditor->GetData()->SDIExternalEditor = false;
  3449. }
  3450. if (DisableDetect)
  3451. {
  3452. UpdatedEditor->GetData()->DetectMDIExternalEditor = false;
  3453. }
  3454. EditorList->Change(i, UpdatedEditor);
  3455. }
  3456. }
  3457. if (Changed)
  3458. {
  3459. WinConfiguration->EditorList = EditorList;
  3460. }
  3461. }
  3462. }
  3463. __finally
  3464. {
  3465. delete EditorList;
  3466. }
  3467. }
  3468. else
  3469. {
  3470. // "open" case
  3471. MessageDialog(FMTLOAD(APP_EARLY_CLOSED, (Data->OriginalFileName)), qtWarning,
  3472. qaOK, HELP_APP_EARLY_CLOSED);
  3473. }
  3474. }
  3475. //---------------------------------------------------------------------------
  3476. void __fastcall TCustomScpExplorerForm::ExecutedFileUploadComplete(TObject * Sender)
  3477. {
  3478. EditorFormFileUploadComplete(DebugNotNull(dynamic_cast<TForm *>(Sender)));
  3479. }
  3480. //---------------------------------------------------------------------------
  3481. void __fastcall TCustomScpExplorerForm::RemoteDirViewEnter(TObject * /*Sender*/)
  3482. {
  3483. SideEnter(osRemote);
  3484. }
  3485. //---------------------------------------------------------------------------
  3486. void __fastcall TCustomScpExplorerForm::RemoteDriveViewEnter(TObject * /*Sender*/)
  3487. {
  3488. MakeNextInTabOrder(RemoteDirView, RemoteDriveView);
  3489. SideEnter(osRemote);
  3490. }
  3491. //---------------------------------------------------------------------------
  3492. void __fastcall TCustomScpExplorerForm::SideEnter(TOperationSide Side)
  3493. {
  3494. FCurrentSide = Side;
  3495. if (Visible)
  3496. {
  3497. UpdateControls();
  3498. }
  3499. }
  3500. //---------------------------------------------------------------------------
  3501. void __fastcall TCustomScpExplorerForm::DeleteFiles(TOperationSide Side,
  3502. TStrings * FileList, bool Alternative)
  3503. {
  3504. TCustomDirView * DView = DirView(Side);
  3505. DView->SaveSelection();
  3506. DView->SaveSelectedNames();
  3507. DebugAssert(!FAlternativeDelete);
  3508. FAlternativeDelete = Alternative;
  3509. try
  3510. {
  3511. if (Side == osRemote)
  3512. {
  3513. DebugAssert(Terminal != NULL);
  3514. Terminal->DeleteFiles(FileList, FLAGMASK(Alternative, dfAlternative));
  3515. }
  3516. else
  3517. {
  3518. try
  3519. {
  3520. TTerminalManager::Instance()->LocalTerminal->DeleteLocalFiles(FileList, FLAGMASK(Alternative, dfAlternative));
  3521. }
  3522. __finally
  3523. {
  3524. ReloadLocalDirectory();
  3525. }
  3526. }
  3527. FAlternativeDelete = false;
  3528. }
  3529. catch(...)
  3530. {
  3531. FAlternativeDelete = false;
  3532. DView->DiscardSavedSelection();
  3533. throw;
  3534. }
  3535. DView->RestoreSelection();
  3536. }
  3537. //---------------------------------------------------------------------------
  3538. void __fastcall TCustomScpExplorerForm::LockFiles(TStrings * FileList, bool Lock)
  3539. {
  3540. DebugAssert(Terminal);
  3541. RemoteDirView->SaveSelection();
  3542. RemoteDirView->SaveSelectedNames();
  3543. try
  3544. {
  3545. if (Lock)
  3546. {
  3547. Terminal->LockFiles(FileList);
  3548. }
  3549. else
  3550. {
  3551. Terminal->UnlockFiles(FileList);
  3552. }
  3553. }
  3554. catch(...)
  3555. {
  3556. RemoteDirView->DiscardSavedSelection();
  3557. throw;
  3558. }
  3559. RemoteDirView->RestoreSelection();
  3560. }
  3561. //---------------------------------------------------------------------------
  3562. bool __fastcall TCustomScpExplorerForm::RemoteTransferDialog(TTerminal *& Session,
  3563. TStrings * FileList, UnicodeString & Target, UnicodeString & FileMask, bool & DirectCopy,
  3564. bool NoConfirmation, bool Move)
  3565. {
  3566. if (RemoteDriveView->DropTarget != NULL)
  3567. {
  3568. Target = RemoteDriveView->NodePathName(RemoteDriveView->DropTarget);
  3569. }
  3570. else if (RemoteDirView->DropTarget != NULL)
  3571. {
  3572. DebugAssert(RemoteDirView->ItemIsDirectory(RemoteDirView->DropTarget));
  3573. Target = RemoteDirView->ItemFullFileName(RemoteDirView->DropTarget);
  3574. }
  3575. else
  3576. {
  3577. Target = RemoteDirView->Path;
  3578. }
  3579. if (Session == NULL)
  3580. {
  3581. Session = TTerminalManager::Instance()->ActiveTerminal;
  3582. }
  3583. Target = UnixIncludeTrailingBackslash(Target);
  3584. if (FileList->Count == 1)
  3585. {
  3586. FileMask = DelimitFileNameMask(UnixExtractFileName(FileList->Strings[0]));
  3587. }
  3588. else
  3589. {
  3590. FileMask = AnyMask;
  3591. }
  3592. DirectCopy = FTerminal->IsCapable[fcRemoteCopy] || FTerminal->IsCapable[fcSecondaryShell];
  3593. bool Result = true;
  3594. if (!NoConfirmation)
  3595. {
  3596. bool Multi = (FileList->Count > 1);
  3597. if (Move)
  3598. {
  3599. Result = DoRemoteMoveDialog(Multi, Target, FileMask);
  3600. }
  3601. else
  3602. {
  3603. DebugAssert(Terminal != NULL);
  3604. // update Terminal->StateData->RemoteDirectory
  3605. UpdateTerminal(Terminal);
  3606. TStrings * Sessions = TTerminalManager::Instance()->TerminalList;
  3607. TStrings * Directories = new TStringList;
  3608. try
  3609. {
  3610. for (int Index = 0; Index < Sessions->Count; Index++)
  3611. {
  3612. TManagedTerminal * Terminal =
  3613. dynamic_cast<TManagedTerminal *>(Sessions->Objects[Index]);
  3614. Directories->Add(Terminal->StateData->RemoteDirectory);
  3615. }
  3616. TDirectRemoteCopy AllowDirectCopy;
  3617. if (FTerminal->IsCapable[fcRemoteCopy] || FTerminal->CommandSessionOpened)
  3618. {
  3619. DebugAssert(DirectCopy);
  3620. AllowDirectCopy = drcAllow;
  3621. }
  3622. else if (FTerminal->IsCapable[fcSecondaryShell])
  3623. {
  3624. DebugAssert(DirectCopy);
  3625. AllowDirectCopy = drcConfirmCommandSession;
  3626. }
  3627. else
  3628. {
  3629. DebugAssert(!DirectCopy);
  3630. AllowDirectCopy = drcDisallow;
  3631. }
  3632. void * ASession = Session;
  3633. Result = DoRemoteCopyDialog(Sessions, Directories, AllowDirectCopy,
  3634. Multi, ASession, Target, FileMask, DirectCopy, TTerminalManager::Instance()->ActiveTerminal);
  3635. Session = static_cast<TTerminal *>(ASession);
  3636. }
  3637. __finally
  3638. {
  3639. delete Directories;
  3640. }
  3641. }
  3642. }
  3643. return Result;
  3644. }
  3645. //---------------------------------------------------------------------------
  3646. bool __fastcall TCustomScpExplorerForm::RemoteTransferFiles(
  3647. TStrings * FileList, bool NoConfirmation, bool Move, TTerminal * Session)
  3648. {
  3649. bool DirectCopy;
  3650. UnicodeString Target, FileMask;
  3651. bool Result = RemoteTransferDialog(Session, FileList, Target, FileMask, DirectCopy, NoConfirmation, Move);
  3652. if (Result)
  3653. {
  3654. if (!Move && !DirectCopy)
  3655. {
  3656. Configuration->Usage->Inc("RemoteCopyTemp");
  3657. UnicodeString RootTempDir;
  3658. UnicodeString TempDir;
  3659. TemporarilyDownloadFiles(FileList, false, RootTempDir, TempDir, false, false, false);
  3660. TStrings * TemporaryFilesList = new TStringList();
  3661. try
  3662. {
  3663. TMakeLocalFileListParams MakeFileListParam;
  3664. MakeFileListParam.FileList = TemporaryFilesList;
  3665. MakeFileListParam.FileTimes = NULL;
  3666. MakeFileListParam.IncludeDirs = true;
  3667. MakeFileListParam.Recursive = false;
  3668. ProcessLocalDirectory(TempDir, FTerminal->MakeLocalFileList, &MakeFileListParam);
  3669. TTerminalManager::Instance()->ActiveTerminal = Session;
  3670. if (TemporaryFilesList->Count > 0)
  3671. {
  3672. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  3673. CopyParam.FileMask = FileMask;
  3674. DebugAssert(!FAutoOperation);
  3675. FAutoOperation = true;
  3676. FTerminal->CopyToRemote(TemporaryFilesList, Target, &CopyParam, cpTemporary, NULL);
  3677. }
  3678. }
  3679. __finally
  3680. {
  3681. delete TemporaryFilesList;
  3682. FAutoOperation = false;
  3683. if (!RootTempDir.IsEmpty())
  3684. {
  3685. RecursiveDeleteFile(ExcludeTrailingBackslash(RootTempDir), false);
  3686. }
  3687. }
  3688. }
  3689. else
  3690. {
  3691. RemoteDirView->SaveSelection();
  3692. RemoteDirView->SaveSelectedNames();
  3693. try
  3694. {
  3695. if (Move)
  3696. {
  3697. Configuration->Usage->Inc("RemoteMove");
  3698. Terminal->MoveFiles(FileList, Target, FileMask);
  3699. }
  3700. else
  3701. {
  3702. Configuration->Usage->Inc("RemoteCopyDirect");
  3703. DebugAssert(DirectCopy);
  3704. DebugAssert(Session == FTerminal);
  3705. if (FTerminal->IsCapable[fcRemoteCopy] ||
  3706. FTerminal->CommandSessionOpened ||
  3707. CommandSessionFallback())
  3708. {
  3709. Terminal->CopyFiles(FileList, Target, FileMask);
  3710. }
  3711. }
  3712. }
  3713. catch(...)
  3714. {
  3715. RemoteDirView->DiscardSavedSelection();
  3716. throw;
  3717. }
  3718. RemoteDirView->RestoreSelection();
  3719. }
  3720. }
  3721. return Result;
  3722. }
  3723. //---------------------------------------------------------------------------
  3724. void __fastcall TCustomScpExplorerForm::CreateDirectory(TOperationSide Side)
  3725. {
  3726. Side = GetSide(Side);
  3727. TRemoteProperties Properties = GUIConfiguration->NewDirectoryProperties;
  3728. TRemoteProperties * AProperties = (Side == osRemote ? &Properties : NULL);
  3729. UnicodeString Name = LoadStr(NEW_FOLDER);
  3730. int AllowedChanges =
  3731. FLAGMASK((Side == osRemote) && Terminal->IsCapable[fcModeChanging], cpMode);
  3732. bool SaveSettings = false;
  3733. if (DoCreateDirectoryDialog(Name, AProperties, AllowedChanges, SaveSettings))
  3734. {
  3735. TWindowLock Lock(this);
  3736. if (Side == osRemote)
  3737. {
  3738. if (SaveSettings)
  3739. {
  3740. GUIConfiguration->NewDirectoryProperties = Properties;
  3741. }
  3742. RemoteDirView->CreateDirectoryEx(Name, &Properties);
  3743. }
  3744. else
  3745. {
  3746. DirView(Side)->CreateDirectory(Name);
  3747. }
  3748. }
  3749. }
  3750. //---------------------------------------------------------------------------
  3751. void __fastcall TCustomScpExplorerForm::HomeDirectory(TOperationSide Side)
  3752. {
  3753. TWindowLock Lock(this);
  3754. DirView(Side)->ExecuteHomeDirectory();
  3755. }
  3756. //---------------------------------------------------------------------------
  3757. void __fastcall TCustomScpExplorerForm::OpenDirectory(TOperationSide Side)
  3758. {
  3759. DoOpenDirectoryDialog(odBrowse, Side);
  3760. }
  3761. //---------------------------------------------------------------------------
  3762. bool __fastcall TCustomScpExplorerForm::OpenBookmark(UnicodeString Local, UnicodeString Remote)
  3763. {
  3764. UnicodeString Path;
  3765. if (FCurrentSide == osRemote)
  3766. {
  3767. Path = Remote;
  3768. }
  3769. else
  3770. {
  3771. Path = Local;
  3772. }
  3773. bool Result = !Path.IsEmpty();
  3774. if (Result)
  3775. {
  3776. // While we might get here when the session is closed (from location profiles),
  3777. // it's not a problem as the Path setter is noop then
  3778. DirView(FCurrentSide)->Path = Path;
  3779. }
  3780. return Result;
  3781. }
  3782. //---------------------------------------------------------------------------
  3783. void __fastcall TCustomScpExplorerForm::CalculateSize(
  3784. TStrings * FileList, __int64 & Size, TCalculateSizeStats & Stats,
  3785. bool & Close)
  3786. {
  3787. // terminal can be already closed (e.g. dropped connection)
  3788. if (Terminal != NULL)
  3789. {
  3790. try
  3791. {
  3792. Terminal->CalculateFilesSize(FileList, Size, 0, NULL, true, Stats);
  3793. }
  3794. catch(...)
  3795. {
  3796. if (!Terminal->Active)
  3797. {
  3798. Close = true;
  3799. }
  3800. throw;
  3801. }
  3802. }
  3803. }
  3804. //---------------------------------------------------------------------------
  3805. void __fastcall TCustomScpExplorerForm::CalculateChecksum(const UnicodeString & Alg,
  3806. TStrings * FileList, TCalculatedChecksumEvent OnCalculatedChecksum,
  3807. bool & Close)
  3808. {
  3809. // terminal can be already closed (e.g. dropped connection)
  3810. if (Terminal != NULL)
  3811. {
  3812. Configuration->Usage->Inc(L"ChecksumCalculated");
  3813. try
  3814. {
  3815. Terminal->CalculateFilesChecksum(Alg, FileList, NULL, OnCalculatedChecksum);
  3816. }
  3817. catch(...)
  3818. {
  3819. if (!Terminal->Active)
  3820. {
  3821. Close = true;
  3822. }
  3823. throw;
  3824. }
  3825. }
  3826. }
  3827. //---------------------------------------------------------------------------
  3828. bool __fastcall TCustomScpExplorerForm::SetProperties(TOperationSide Side, TStrings * FileList)
  3829. {
  3830. bool Result;
  3831. if (Side == osRemote)
  3832. {
  3833. TRemoteTokenList * GroupList = NULL;
  3834. TRemoteTokenList * UserList = NULL;
  3835. try
  3836. {
  3837. TRemoteProperties CurrentProperties;
  3838. if (Terminal->LoadFilesProperties(FileList))
  3839. {
  3840. RemoteDirView->Invalidate();
  3841. }
  3842. bool CapableGroupChanging = Terminal->IsCapable[fcGroupChanging];
  3843. bool CapableOwnerChanging = Terminal->IsCapable[fcOwnerChanging];
  3844. if (CapableGroupChanging || CapableOwnerChanging)
  3845. {
  3846. if (CapableGroupChanging)
  3847. {
  3848. GroupList = Terminal->Groups->Duplicate();
  3849. }
  3850. if (CapableOwnerChanging)
  3851. {
  3852. UserList = Terminal->Users->Duplicate();
  3853. }
  3854. TRemoteDirectory * Files = Terminal->Files;
  3855. int Count = Files->Count;
  3856. if (Count > 100)
  3857. {
  3858. Count = 100;
  3859. }
  3860. for (int Index = 0; Index < Count; Index++)
  3861. {
  3862. TRemoteFile * File = Files->Files[Index];
  3863. if (CapableGroupChanging)
  3864. {
  3865. GroupList->AddUnique(File->Group);
  3866. }
  3867. if (CapableOwnerChanging)
  3868. {
  3869. UserList->AddUnique(File->Owner);
  3870. }
  3871. }
  3872. // if we haven't collected tokens for all files in current directory,
  3873. // make sure we collect them at least for all selected files.
  3874. // (note that so far the files in FileList has to be from current direcotry)
  3875. if (Count < Files->Count)
  3876. {
  3877. for (int Index = 0; Index < FileList->Count; Index++)
  3878. {
  3879. TRemoteFile * File = (TRemoteFile *)(FileList->Objects[Index]);
  3880. if (CapableGroupChanging)
  3881. {
  3882. GroupList->AddUnique(File->Group);
  3883. }
  3884. if (CapableOwnerChanging)
  3885. {
  3886. UserList->AddUnique(File->Owner);
  3887. }
  3888. }
  3889. }
  3890. }
  3891. CurrentProperties = TRemoteProperties::CommonProperties(FileList);
  3892. int Flags = 0;
  3893. if (Terminal->IsCapable[fcModeChanging]) Flags |= cpMode;
  3894. if (CapableOwnerChanging) Flags |= cpOwner;
  3895. if (CapableGroupChanging) Flags |= cpGroup;
  3896. TCalculateChecksumEvent CalculateChecksumEvent = NULL;
  3897. if (Terminal->IsCapable[fcCalculatingChecksum])
  3898. {
  3899. CalculateChecksumEvent = CalculateChecksum;
  3900. }
  3901. std::unique_ptr<TStrings> ChecksumAlgs(new TStringList());
  3902. Terminal->GetSupportedChecksumAlgs(ChecksumAlgs.get());
  3903. TRemoteProperties NewProperties = CurrentProperties;
  3904. Result =
  3905. DoPropertiesDialog(FileList, RemoteDirView->PathName,
  3906. GroupList, UserList, ChecksumAlgs.get(), &NewProperties, Flags,
  3907. Terminal->IsCapable[fcGroupOwnerChangingByID],
  3908. CalculateSize, CalculateChecksumEvent);
  3909. if (Result)
  3910. {
  3911. NewProperties = TRemoteProperties::ChangedProperties(CurrentProperties, NewProperties);
  3912. Terminal->ChangeFilesProperties(FileList, &NewProperties);
  3913. }
  3914. }
  3915. __finally
  3916. {
  3917. delete GroupList;
  3918. delete UserList;
  3919. }
  3920. }
  3921. else
  3922. {
  3923. DirView(Side)->DisplayPropertiesMenu();
  3924. Result = true;
  3925. }
  3926. return Result;
  3927. }
  3928. //---------------------------------------------------------------------------
  3929. void __fastcall TCustomScpExplorerForm::KeyProcessed(Word & Key, TShiftState Shift)
  3930. {
  3931. if (Shift * AllKeyShiftStates() == (TShiftState() << ssAlt))
  3932. {
  3933. FIgnoreNextDialogChar = Key;
  3934. }
  3935. Key = 0;
  3936. }
  3937. //---------------------------------------------------------------------------
  3938. void __fastcall TCustomScpExplorerForm::CheckCustomCommandShortCut(
  3939. TCustomCommandList * List, Word & Key, Classes::TShiftState Shift, TShortCut KeyShortCut)
  3940. {
  3941. const TCustomCommandType * Command = List->Find(KeyShortCut);
  3942. if (Command != NULL)
  3943. {
  3944. KeyProcessed(Key, Shift);
  3945. if (CustomCommandState(*Command, false, ccltAll) > 0)
  3946. {
  3947. ExecuteFileOperationCommand(foCustomCommand, osRemote,
  3948. false, false, const_cast<TCustomCommandType *>(Command));
  3949. }
  3950. }
  3951. }
  3952. //---------------------------------------------------------------------------
  3953. void __fastcall TCustomScpExplorerForm::KeyDown(Word & Key, Classes::TShiftState Shift)
  3954. {
  3955. if (QueueView3->Focused() && (QueueView3->OnKeyDown != NULL))
  3956. {
  3957. QueueView3->OnKeyDown(QueueView3, Key, Shift);
  3958. }
  3959. if (!DirView(osCurrent)->IsEditing())
  3960. {
  3961. TShortCut KeyShortCut = ShortCut(Key, Shift);
  3962. for (int Index = 0; Index < NonVisualDataModule->ExplorerActions->ActionCount; Index++)
  3963. {
  3964. TAction * Action = (TAction *)NonVisualDataModule->ExplorerActions->Actions[Index];
  3965. if (((Action->ShortCut == KeyShortCut) ||
  3966. (Action->SecondaryShortCuts->IndexOfShortCut(KeyShortCut) >= 0)) &&
  3967. AllowedAction(Action, aaShortCut))
  3968. {
  3969. // Has to be called before the action as the dialog char is already in queue.
  3970. // So when the action consumes message queue, we already need to have the
  3971. // FIgnoreNextDialogChar set
  3972. KeyProcessed(Key, Shift);
  3973. // Reset reference to previous component (when menu/toolbar was clicked).
  3974. // Needed to detect that action was invoked by keyboard shortcut
  3975. // in TNonVisualDataModule::ExplorerActionsExecute
  3976. Action->ActionComponent = NULL;
  3977. Action->Execute();
  3978. return;
  3979. }
  3980. }
  3981. for (int i = 0; i < TTerminalManager::Instance()->Count; i++)
  3982. {
  3983. if (NonVisualDataModule->OpenSessionShortCut(i) == KeyShortCut)
  3984. {
  3985. KeyProcessed(Key, Shift);
  3986. TTerminalManager::Instance()->ActiveTerminalIndex = i;
  3987. return;
  3988. }
  3989. }
  3990. if (Key == VK_TAB && Shift.Contains(ssCtrl))
  3991. {
  3992. KeyProcessed(Key, Shift);
  3993. TTerminalManager::Instance()->CycleTerminals(!Shift.Contains(ssShift));
  3994. }
  3995. TShortCut CustomShortCut = NormalizeCustomShortCut(KeyShortCut);
  3996. if (IsCustomShortCut(CustomShortCut))
  3997. {
  3998. CheckCustomCommandShortCut(WinConfiguration->CustomCommandList, Key, Shift, CustomShortCut);
  3999. CheckCustomCommandShortCut(WinConfiguration->ExtensionList, Key, Shift, CustomShortCut);
  4000. if (WinConfiguration->SharedBookmarks != NULL)
  4001. {
  4002. TBookmark * Bookmark = WinConfiguration->SharedBookmarks->FindByShortCut(CustomShortCut);
  4003. if ((Bookmark != NULL) &&
  4004. OpenBookmark(Bookmark->Local, Bookmark->Remote))
  4005. {
  4006. KeyProcessed(Key, Shift);
  4007. }
  4008. }
  4009. }
  4010. }
  4011. TForm::KeyDown(Key, Shift);
  4012. }
  4013. //---------------------------------------------------------------------------
  4014. void __fastcall TCustomScpExplorerForm::InitStatusBar()
  4015. {
  4016. const TSessionInfo & SessionInfo = Terminal->GetSessionInfo();
  4017. const TFileSystemInfo & FileSystemInfo = Terminal->GetFileSystemInfo();
  4018. TTBXStatusBar * SessionStatusBar = (TTBXStatusBar *)GetComponent(fcStatusBar);
  4019. DebugAssert(Terminal);
  4020. int Offset = SessionStatusBar->Panels->Count - SessionPanelCount;
  4021. bool SecurityEnabled = !SessionInfo.SecurityProtocolName.IsEmpty();
  4022. SessionStatusBar->Panels->Items[Offset + 0]->Enabled = SecurityEnabled;
  4023. // expanded from ?: to avoid memory leaks
  4024. if (SecurityEnabled)
  4025. {
  4026. SessionStatusBar->Panels->Items[Offset + 0]->Hint =
  4027. FMTLOAD(STATUS_SECURE, (SessionInfo.SecurityProtocolName));
  4028. }
  4029. else
  4030. {
  4031. SessionStatusBar->Panels->Items[Offset + 0]->Hint = LoadStr(STATUS_INSECURE);
  4032. }
  4033. if (FileSystemInfo.ProtocolName.IsEmpty())
  4034. {
  4035. SessionStatusBar->Panels->Items[Offset + 1]->Caption = SessionInfo.ProtocolName;
  4036. }
  4037. else
  4038. {
  4039. SessionStatusBar->Panels->Items[Offset + 1]->Caption = FileSystemInfo.ProtocolName;
  4040. }
  4041. SessionStatusBar->Panels->Items[Offset + 1]->Hint = LoadStr(STATUS_PROTOCOL_HINT);
  4042. SessionStatusBar->Panels->Items[Offset + 2]->Enabled =
  4043. (!SessionInfo.CSCompression.IsEmpty() || !SessionInfo.SCCompression.IsEmpty());
  4044. UnicodeString CSCompressionStr = DefaultStr(SessionInfo.CSCompression, LoadStr(NO_STR));
  4045. UnicodeString SCCompressionStr = DefaultStr(SessionInfo.SCCompression, LoadStr(NO_STR));
  4046. UnicodeString CompressionStr = CSCompressionStr;
  4047. if (CSCompressionStr != SCCompressionStr)
  4048. {
  4049. CompressionStr += UnicodeString(L"/") + SCCompressionStr;
  4050. }
  4051. SessionStatusBar->Panels->Items[Offset + 2]->Hint = FMTLOAD(STATUS_COMPRESSION_HINT, (CompressionStr));
  4052. SessionStatusBar->Panels->Items[Offset + 3]->Hint = LoadStr(STATUS_DURATION_HINT);
  4053. UpdateStatusBar();
  4054. }
  4055. //---------------------------------------------------------------------------
  4056. void __fastcall TCustomScpExplorerForm::UpdateStatusBar()
  4057. {
  4058. TTBXStatusBar * SessionStatusBar = (TTBXStatusBar *)GetComponent(fcStatusBar);
  4059. DebugAssert(SessionStatusBar != NULL);
  4060. if (!Terminal || !Terminal->Active || Terminal->Status < ssOpened)
  4061. {
  4062. // note: (Terminal->Status < sshReady) currently never happens here,
  4063. // so STATUS_CONNECTING is never used
  4064. SessionStatusBar->SimplePanel = true;
  4065. SessionStatusBar->SimpleText = LoadStr(
  4066. !Terminal || !Terminal->Active ? STATUS_NOT_CONNECTED : STATUS_CONNECTING);
  4067. }
  4068. else
  4069. {
  4070. DebugAssert(Terminal);
  4071. SessionStatusBar->SimplePanel = false;
  4072. const TSessionInfo & SessionInfo = Terminal->GetSessionInfo();
  4073. if (!FNote.IsEmpty())
  4074. {
  4075. SessionStatusBar->Panels->Items[0]->Caption = FNote;
  4076. }
  4077. else
  4078. {
  4079. UpdateStatusPanelText(SessionStatusBar->Panels->Items[0]);
  4080. }
  4081. SessionStatusBar->Panels->Items[0]->Hint = FNoteHints;
  4082. SessionStatusBar->Panels->Items[SessionStatusBar->Panels->Count - 1]->Caption =
  4083. FormatDateTimeSpan(Configuration->TimeFormat, Now() - SessionInfo.LoginTime);
  4084. }
  4085. }
  4086. //---------------------------------------------------------------------------
  4087. void __fastcall TCustomScpExplorerForm::UpdateStatusPanelText(TTBXStatusPanel * Panel)
  4088. {
  4089. Panel->Caption = L"";
  4090. }
  4091. //---------------------------------------------------------------------------
  4092. void __fastcall TCustomScpExplorerForm::Idle()
  4093. {
  4094. if (FShowing || FStandaloneEditing)
  4095. {
  4096. FEditorManager->Check();
  4097. // make sure that Idle is called before update queue, as it may invoke QueueEvent
  4098. // that needs to know if queue view is visible (and it may be closed after queue update)
  4099. TTerminalManager::Instance()->Idle(FDoNotIdleCurrentTerminal);
  4100. }
  4101. if (FShowing)
  4102. {
  4103. if (!NonVisualDataModule->Busy &&
  4104. // Menu is opened or drag&drop is going on
  4105. (Mouse->Capture == NULL))
  4106. {
  4107. if (FRefreshRemoteDirectory)
  4108. {
  4109. if ((Terminal != NULL) && (Terminal->Status == ssOpened))
  4110. {
  4111. Terminal->RefreshDirectory();
  4112. }
  4113. FRefreshRemoteDirectory = false;
  4114. }
  4115. if (FRefreshLocalDirectory)
  4116. {
  4117. ReloadLocalDirectory();
  4118. FRefreshLocalDirectory = false;
  4119. }
  4120. if (WinConfiguration->RefreshRemotePanel)
  4121. {
  4122. TManagedTerminal * ManagedTerminal =
  4123. dynamic_cast<TManagedTerminal *>(Terminal);
  4124. if ((ManagedTerminal != NULL) && (Terminal->Status == ssOpened) &&
  4125. (Now() - ManagedTerminal->DirectoryLoaded >
  4126. WinConfiguration->RefreshRemotePanelInterval))
  4127. {
  4128. RemoteDirView->ReloadDirectory();
  4129. }
  4130. }
  4131. }
  4132. }
  4133. if (FShowing || FStandaloneEditing)
  4134. {
  4135. if (FQueueStatusInvalidated)
  4136. {
  4137. UpdateQueueStatus(false);
  4138. }
  4139. RefreshQueueItems();
  4140. }
  4141. if (FShowing)
  4142. {
  4143. UpdateStatusBar();
  4144. }
  4145. FIgnoreNextDialogChar = 0;
  4146. }
  4147. //---------------------------------------------------------------------------
  4148. void __fastcall TCustomScpExplorerForm::UserActionTimer(TObject * /*Sender*/)
  4149. {
  4150. try
  4151. {
  4152. FUserActionTimer->Enabled = false;
  4153. if (IsQueueAutoPopup() && (FPendingQueueActionItem != NULL))
  4154. {
  4155. if (TQueueItem::IsUserActionStatus(FPendingQueueActionItem->Status))
  4156. {
  4157. FPendingQueueActionItem->ProcessUserAction();
  4158. }
  4159. }
  4160. }
  4161. __finally
  4162. {
  4163. FPendingQueueActionItem = NULL;
  4164. }
  4165. }
  4166. //---------------------------------------------------------------------------
  4167. void __fastcall TCustomScpExplorerForm::ApplicationMinimize(TObject * /*Sender*/)
  4168. {
  4169. if (WinConfiguration->MinimizeToTray)
  4170. {
  4171. UpdateTrayIcon();
  4172. FTrayIcon->Visible = true;
  4173. if (Visible)
  4174. {
  4175. ShowWindow(Handle, SW_HIDE);
  4176. }
  4177. }
  4178. }
  4179. //---------------------------------------------------------------------------
  4180. void __fastcall TCustomScpExplorerForm::ApplicationRestore(TObject * /*Sender*/)
  4181. {
  4182. // WORKAROUND
  4183. // When restoring maximized window from minimization,
  4184. // rarely some controls do not align properly.
  4185. // Two instances seen (both for Commander):
  4186. // - When restoring, window is temporarily narrower (not maximized),
  4187. // causing toolbars on TopDock to wrap and dock to expand horizontally.
  4188. // Once maximized already, top dock shinks back, but the session PageControl,
  4189. // do not align up, leaving space between TopDock and PageControl.
  4190. // - Similar issue seen with LocalDirView not aligning down to status bar.
  4191. for (int Index = 0; Index < ControlCount; Index++)
  4192. {
  4193. RealignControl(Controls[Index]);
  4194. }
  4195. if (FTrayIcon->Visible)
  4196. {
  4197. FTrayIcon->Visible = false;
  4198. if (Visible)
  4199. {
  4200. ShowWindow(Handle, SW_SHOW);
  4201. }
  4202. }
  4203. if (FNeedSession && DebugAlwaysTrue(Terminal == NULL))
  4204. {
  4205. FNeedSession = false;
  4206. NonVisualDataModule->StartBusy();
  4207. try
  4208. {
  4209. NeedSession(false);
  4210. }
  4211. __finally
  4212. {
  4213. NonVisualDataModule->EndBusy();
  4214. }
  4215. }
  4216. }
  4217. //---------------------------------------------------------------------------
  4218. void __fastcall TCustomScpExplorerForm::UpdateTrayIcon()
  4219. {
  4220. FTrayIcon->Hint = Caption;
  4221. }
  4222. //---------------------------------------------------------------------------
  4223. void __fastcall TCustomScpExplorerForm::ApplicationTitleChanged()
  4224. {
  4225. UpdateTrayIcon();
  4226. }
  4227. //---------------------------------------------------------------------------
  4228. void __fastcall TCustomScpExplorerForm::RestoreApp()
  4229. {
  4230. // workaround
  4231. // TApplication.WndProc sets TApplication.FAppIconic to false,
  4232. // on WM_ACTIVATEAPP, what renders TApplication.Restore no-op function.
  4233. // But WM_ACTIVATEAPP message can be received even when
  4234. // the main window is minimized to the tray and internal editor window is focused
  4235. // (after another application was previously active)
  4236. if (::IsIconic(Handle))
  4237. {
  4238. if (!IsAppIconic())
  4239. {
  4240. SetAppIconic(true);
  4241. }
  4242. }
  4243. ::ApplicationRestore();
  4244. Application->BringToFront();
  4245. }
  4246. //---------------------------------------------------------------------------
  4247. void __fastcall TCustomScpExplorerForm::TrayIconClick(TObject * /*Sender*/)
  4248. {
  4249. RestoreApp();
  4250. }
  4251. //---------------------------------------------------------------------------
  4252. void __fastcall TCustomScpExplorerForm::NewSession(bool FromSite, const UnicodeString & SessionUrl)
  4253. {
  4254. if (OpenInNewWindow())
  4255. {
  4256. // todo: Pass FromSite
  4257. ExecuteNewInstance(SessionUrl);
  4258. }
  4259. else
  4260. {
  4261. TTerminalManager::Instance()->NewSession(FromSite, SessionUrl);
  4262. }
  4263. }
  4264. //---------------------------------------------------------------------------
  4265. UnicodeString __fastcall TCustomScpExplorerForm::CreateHiddenDuplicateSession()
  4266. {
  4267. UnicodeString SessionName = StoredSessions->HiddenPrefix + Terminal->SessionData->SessionName;
  4268. // current working directories become defaults here, what is not right
  4269. std::unique_ptr<TSessionData> SessionData(CloneCurrentSessionData());
  4270. StoredSessions->NewSession(SessionName, SessionData.get());
  4271. // modified only, explicit
  4272. StoredSessions->Save(false, true);
  4273. // encode session name because of slashes in hierarchical sessions
  4274. return EncodeUrlString(SessionName);
  4275. }
  4276. //---------------------------------------------------------------------------
  4277. void __fastcall TCustomScpExplorerForm::DuplicateSession()
  4278. {
  4279. if (OpenInNewWindow())
  4280. {
  4281. ExecuteNewInstance(CreateHiddenDuplicateSession());
  4282. }
  4283. else
  4284. {
  4285. // current working directories become defaults here, what is not right
  4286. std::unique_ptr<TSessionData> SessionData(CloneCurrentSessionData());
  4287. TTerminalManager * Manager = TTerminalManager::Instance();
  4288. TTerminal * Terminal = Manager->NewTerminal(SessionData.get());
  4289. Manager->ActiveTerminal = Terminal;
  4290. }
  4291. }
  4292. //---------------------------------------------------------------------------
  4293. bool __fastcall TCustomScpExplorerForm::CanCloseQueue()
  4294. {
  4295. DebugAssert(FQueue != NULL);
  4296. bool Result = FQueue->IsEmpty;
  4297. if (!Result)
  4298. {
  4299. SetFocus();
  4300. Result = (MessageDialog(LoadStr(PENDING_QUEUE_ITEMS2), qtWarning, qaOK | qaCancel, HELP_NONE) == qaOK);
  4301. }
  4302. return Result;
  4303. }
  4304. //---------------------------------------------------------------------------
  4305. void __fastcall TCustomScpExplorerForm::CloseSession()
  4306. {
  4307. if (CanCloseQueue())
  4308. {
  4309. TTerminalManager::Instance()->FreeActiveTerminal();
  4310. }
  4311. }
  4312. //---------------------------------------------------------------------------
  4313. void __fastcall TCustomScpExplorerForm::OpenStoredSession(TSessionData * Data)
  4314. {
  4315. if (OpenInNewWindow())
  4316. {
  4317. // encode session name because of slashes in hierarchical sessions
  4318. ExecuteNewInstance(EncodeUrlString(Data->Name));
  4319. }
  4320. else
  4321. {
  4322. TTerminalManager * Manager = TTerminalManager::Instance();
  4323. TTerminal * Terminal = Manager->NewTerminal(Data);
  4324. Manager->ActiveTerminal = Terminal;
  4325. }
  4326. }
  4327. //---------------------------------------------------------------------------
  4328. void __fastcall TCustomScpExplorerForm::OpenFolderOrWorkspace(const UnicodeString & Name)
  4329. {
  4330. if (OpenInNewWindow())
  4331. {
  4332. ExecuteNewInstance(Name);
  4333. }
  4334. else
  4335. {
  4336. TTerminalManager * Manager = TTerminalManager::Instance();
  4337. std::unique_ptr<TObjectList> DataList(new TObjectList());
  4338. StoredSessions->GetFolderOrWorkspace(Name, DataList.get());
  4339. Manager->ActiveTerminal = Manager->NewTerminals(DataList.get());
  4340. }
  4341. }
  4342. //---------------------------------------------------------------------------
  4343. void __fastcall TCustomScpExplorerForm::FormCloseQuery(TObject * /*Sender*/,
  4344. bool &CanClose)
  4345. {
  4346. if (Terminal != NULL)
  4347. {
  4348. if (Terminal->Active && WinConfiguration->ConfirmClosingSession)
  4349. {
  4350. unsigned int Result;
  4351. TMessageParams Params(mpNeverAskAgainCheck);
  4352. UnicodeString Message;
  4353. int Answers = qaOK | qaCancel;
  4354. if (TTerminalManager::Instance()->Count > 1)
  4355. {
  4356. if (!WinConfiguration->AutoSaveWorkspace)
  4357. {
  4358. Message = LoadStr(CLOSE_SESSIONS_WORKSPACE2);
  4359. Answers = qaYes | qaNo | qaCancel;
  4360. }
  4361. else
  4362. {
  4363. Message = MainInstructions(LoadStr(CLOSE_SESSIONS));
  4364. }
  4365. }
  4366. else
  4367. {
  4368. Message = MainInstructions(FMTLOAD(CLOSE_SESSION, (Terminal->SessionData->SessionName)));
  4369. }
  4370. if (WinConfiguration->AutoSaveWorkspace)
  4371. {
  4372. Message =
  4373. FORMAT("%s\n\n%s", (Message,
  4374. FMTLOAD(AUTO_WORKSPACE, (WorkspaceName()))));
  4375. }
  4376. SetFocus();
  4377. Result = MessageDialog(Message, qtConfirmation,
  4378. Answers, HELP_NONE, &Params);
  4379. if (Result == qaNeverAskAgain)
  4380. {
  4381. WinConfiguration->ConfirmClosingSession = false;
  4382. }
  4383. if (Result == qaNo)
  4384. {
  4385. CanClose = SaveWorkspace(true);
  4386. // note that the workspace will be saved redundatly again from FormClose
  4387. }
  4388. else
  4389. {
  4390. CanClose =
  4391. (Result == qaOK) ||
  4392. (Result == qaYes) || // CLOSE_SESSIONS_WORKSPACE variant
  4393. (Result == qaNeverAskAgain);
  4394. }
  4395. }
  4396. if (CanClose)
  4397. {
  4398. CanClose = CanCloseQueue();
  4399. }
  4400. }
  4401. if (CanClose)
  4402. {
  4403. CanClose =
  4404. FEditorManager->CloseInternalEditors(CloseInternalEditor) &&
  4405. FEditorManager->CloseExternalFilesWithoutProcess();
  4406. if (CanClose)
  4407. {
  4408. while (CanClose && (FLocalEditors->Count > 0))
  4409. {
  4410. int PrevCount = FLocalEditors->Count;
  4411. static_cast<TForm *>(FLocalEditors->Items[0])->Close();
  4412. CanClose = (FLocalEditors->Count < PrevCount);
  4413. }
  4414. if (CanClose)
  4415. {
  4416. CanClose = FEditorManager->Empty(true);
  4417. if (!CanClose)
  4418. {
  4419. SetFocus();
  4420. CanClose =
  4421. (MessageDialog(
  4422. LoadStr(PENDING_EDITORS), qtWarning, qaIgnore | qaCancel, HELP_NONE) == qaIgnore);
  4423. }
  4424. }
  4425. }
  4426. }
  4427. }
  4428. //---------------------------------------------------------------------------
  4429. void __fastcall TCustomScpExplorerForm::CloseInternalEditor(TObject * Sender)
  4430. {
  4431. TForm * Form = dynamic_cast<TForm *>(Sender);
  4432. DebugAssert(Form != NULL);
  4433. Form->Close();
  4434. }
  4435. //---------------------------------------------------------------------------
  4436. void __fastcall TCustomScpExplorerForm::ForceCloseInternalEditor(TObject * Sender)
  4437. {
  4438. TForm * Form = dynamic_cast<TForm *>(Sender);
  4439. delete Form;
  4440. }
  4441. //---------------------------------------------------------------------------
  4442. void __fastcall TCustomScpExplorerForm::ForceCloseLocalEditors()
  4443. {
  4444. while (FLocalEditors->Count > 0)
  4445. {
  4446. delete static_cast<TForm *>(FLocalEditors->Items[0]);
  4447. }
  4448. }
  4449. //---------------------------------------------------------------------------
  4450. void __fastcall TCustomScpExplorerForm::RemoteDirViewDisplayProperties(
  4451. TObject *Sender)
  4452. {
  4453. TStrings *FileList = ((TUnixDirView*)Sender)->CreateFileList(True, False, NULL);
  4454. try
  4455. {
  4456. SetProperties(osRemote, FileList);
  4457. }
  4458. __finally
  4459. {
  4460. delete FileList;
  4461. }
  4462. }
  4463. //---------------------------------------------------------------------------
  4464. void __fastcall TCustomScpExplorerForm::ComponentShowing(Byte Component, bool value)
  4465. {
  4466. if (value)
  4467. {
  4468. if (Component == fcCustomCommandsBand)
  4469. {
  4470. UpdateCustomCommandsToolbar();
  4471. }
  4472. }
  4473. }
  4474. //---------------------------------------------------------------------------
  4475. void __fastcall TCustomScpExplorerForm::SetComponentVisible(Byte Component, Boolean value)
  4476. {
  4477. TControl * Control = GetComponent(Component);
  4478. DebugAssert(Control);
  4479. bool Changed = (Control->Visible != value);
  4480. if (Changed)
  4481. {
  4482. ComponentShowing(Component, value);
  4483. TWinControl * WinControl = dynamic_cast<TWinControl*>(Control);
  4484. bool WasFocused = (WinControl != NULL) && (ActiveControl != NULL) &&
  4485. ((ActiveControl == WinControl) || (ActiveControl->Parent == WinControl));
  4486. if (value)
  4487. {
  4488. int RemainingHeight = Control->Parent->ClientHeight;
  4489. int RemainingWidth = Control->Parent->ClientWidth;
  4490. for (int i = 0; i < Control->Parent->ControlCount; i++)
  4491. {
  4492. TControl * ChildControl = Control->Parent->Controls[i];
  4493. if (ChildControl->Visible)
  4494. {
  4495. switch (ChildControl->Align)
  4496. {
  4497. case alTop:
  4498. case alBottom:
  4499. RemainingHeight -= ChildControl->Height;
  4500. break;
  4501. case alLeft:
  4502. case alRight:
  4503. RemainingWidth -= ChildControl->Width;
  4504. break;
  4505. }
  4506. }
  4507. }
  4508. int Reserve = ScaleByTextHeight(this, 32);
  4509. // queue in explorer, trees in commander
  4510. if (Control->Height > RemainingHeight - Reserve)
  4511. {
  4512. Control->Height = RemainingHeight / 2;
  4513. }
  4514. if (Control->Width > RemainingWidth - Reserve)
  4515. {
  4516. Control->Width = RemainingWidth / 2;
  4517. }
  4518. }
  4519. Control->Visible = value;
  4520. if (WasFocused && Visible)
  4521. {
  4522. DirView(osCurrent)->SetFocus();
  4523. }
  4524. FixControlsPlacement();
  4525. }
  4526. }
  4527. //---------------------------------------------------------------------------
  4528. bool __fastcall TCustomScpExplorerForm::GetComponentVisible(Byte Component)
  4529. {
  4530. TControl * Control = GetComponent(Component);
  4531. if (Control == NULL)
  4532. {
  4533. return false;
  4534. }
  4535. else
  4536. {
  4537. return Control->Visible;
  4538. }
  4539. }
  4540. //---------------------------------------------------------------------------
  4541. bool __fastcall TCustomScpExplorerForm::IsComponentPossible(Byte Component)
  4542. {
  4543. bool Result;
  4544. if ((Component == fcExplorerUpdatesBand) || (Component == fcCommanderUpdatesBand))
  4545. {
  4546. Result = !IsUWP();
  4547. }
  4548. else
  4549. {
  4550. Result = true;
  4551. }
  4552. return Result;
  4553. }
  4554. //---------------------------------------------------------------------------
  4555. void __fastcall TCustomScpExplorerForm::FixControlsPlacement()
  4556. {
  4557. if (RemoteDirView->ItemFocused != NULL)
  4558. {
  4559. RemoteDirView->ItemFocused->MakeVisible(false);
  4560. }
  4561. QueueSplitter->Visible = QueuePanel->Visible;
  4562. RemotePanelSplitter->Visible = RemoteDriveView->Visible;
  4563. }
  4564. //---------------------------------------------------------------------------
  4565. TControl * __fastcall TCustomScpExplorerForm::GetComponent(Byte Component)
  4566. {
  4567. switch (Component) {
  4568. case fcStatusBar: return RemoteStatusBar;
  4569. case fcRemotePopup: return reinterpret_cast<TControl *>(NonVisualDataModule->RemoteFilePopup);
  4570. case fcQueueView: return QueuePanel;
  4571. case fcQueueToolbar: return QueueDock;
  4572. case fcRemoteTree: return RemoteDriveView;
  4573. case fcSessionsTabs: return SessionsPageControl;
  4574. default: return NULL;
  4575. }
  4576. }
  4577. //---------------------------------------------------------------------------
  4578. void __fastcall TCustomScpExplorerForm::DirViewColumnRightClick(
  4579. TObject *Sender, TListColumn *Column, TPoint &Point)
  4580. {
  4581. DebugAssert(NonVisualDataModule && Column && Sender);
  4582. NonVisualDataModule->ListColumn = Column;
  4583. TPopupMenu * DirViewColumnMenu;
  4584. if (Sender == RemoteDirView)
  4585. {
  4586. DirViewColumnMenu = NonVisualDataModule->RemoteDirViewColumnPopup;
  4587. NonVisualDataModule->RemoteSortByExtColumnPopupItem->Visible =
  4588. (Column->Index == uvName);
  4589. NonVisualDataModule->RemoteFormatSizeBytesPopupItem->Visible =
  4590. (Column->Index == uvSize);
  4591. }
  4592. else
  4593. {
  4594. DirViewColumnMenu = NonVisualDataModule->LocalDirViewColumnPopup;
  4595. NonVisualDataModule->LocalSortByExtColumnPopupItem->Visible =
  4596. (Column->Index == dvName);
  4597. NonVisualDataModule->LocalFormatSizeBytesPopupItem->Visible =
  4598. (Column->Index == dvSize);
  4599. }
  4600. TCustomListView * ListView = DebugNotNull(dynamic_cast<TCustomListView *>(Sender));
  4601. TPoint ScreenPoint = ListView->ClientToScreen(Point);
  4602. ScreenPoint.x -= GetScrollPos(ListView->Handle, SB_HORZ);
  4603. DirViewColumnMenu->Popup(ScreenPoint.x, ScreenPoint.y);
  4604. }
  4605. //---------------------------------------------------------------------------
  4606. void __fastcall TCustomScpExplorerForm::DirViewExecFile(
  4607. TObject *Sender, TListItem *Item, bool &AllowExec)
  4608. {
  4609. DoDirViewExecFile(Sender, Item, AllowExec);
  4610. }
  4611. //---------------------------------------------------------------------------
  4612. void __fastcall TCustomScpExplorerForm::DoDirViewExecFile(TObject * Sender,
  4613. TListItem * Item, bool & AllowExec)
  4614. {
  4615. DebugAssert(Sender && Item && Configuration);
  4616. DebugAssert(AllowExec);
  4617. TCustomDirView * ADirView = (TCustomDirView *)Sender;
  4618. bool Remote = (ADirView == DirView(osRemote));
  4619. bool ResolvedSymlinks = !Remote || Terminal->ResolvingSymlinks;
  4620. TOperationSide Side = (Remote ? osRemote : osLocal);
  4621. // Anything special is done on double click only (not on "open" indicated by FForceExecution),
  4622. // on files only (not directories)
  4623. // and only when symlinks are resolved (apply to remote panel only)
  4624. if (!ADirView->ItemIsDirectory(Item) &&
  4625. (ResolvedSymlinks || FForceExecution))
  4626. {
  4627. if ((WinConfiguration->DoubleClickAction != dcaOpen) &&
  4628. !FForceExecution &&
  4629. ResolvedSymlinks)
  4630. {
  4631. if (WinConfiguration->DoubleClickAction == dcaCopy)
  4632. {
  4633. // To counter the lock in DirViewBusy (called from TCustomDirView.DoExecute)
  4634. UnlockWindow();
  4635. try
  4636. {
  4637. ExecuteFileOperation(
  4638. foCopy, Side, true, !WinConfiguration->CopyOnDoubleClickConfirmation);
  4639. }
  4640. __finally
  4641. {
  4642. LockWindow();
  4643. }
  4644. AllowExec = false;
  4645. }
  4646. else if (WinConfiguration->DoubleClickAction == dcaEdit)
  4647. {
  4648. if (!Remote || !WinConfiguration->DisableOpenEdit)
  4649. {
  4650. ExecuteFile(Side, efDefaultEditor);
  4651. AllowExec = false;
  4652. }
  4653. }
  4654. else
  4655. {
  4656. DebugFail();
  4657. }
  4658. }
  4659. // if we have not done anything special, fall back to default behavior
  4660. if (AllowExec)
  4661. {
  4662. if (Remote && !WinConfiguration->DisableOpenEdit)
  4663. {
  4664. ExecuteFile(osRemote, efShell);
  4665. AllowExec = false;
  4666. }
  4667. }
  4668. }
  4669. }
  4670. //---------------------------------------------------------------------------
  4671. bool __fastcall TCustomScpExplorerForm::GetHasDirView(TOperationSide Side)
  4672. {
  4673. return ((Side == osRemote) || (Side == osCurrent));
  4674. }
  4675. //---------------------------------------------------------------------------
  4676. void __fastcall TCustomScpExplorerForm::CompareDirectories()
  4677. {
  4678. DebugFail();
  4679. }
  4680. //---------------------------------------------------------------------------
  4681. void __fastcall TCustomScpExplorerForm::SynchronizeDirectories()
  4682. {
  4683. DebugFail();
  4684. }
  4685. //---------------------------------------------------------------------------
  4686. bool __fastcall TCustomScpExplorerForm::DoSynchronizeDirectories(
  4687. UnicodeString & LocalDirectory, UnicodeString & RemoteDirectory, bool UseDefaults)
  4688. {
  4689. TSynchronizeParamType Params;
  4690. Params.LocalDirectory = LocalDirectory;
  4691. Params.RemoteDirectory = RemoteDirectory;
  4692. int UnusedParams =
  4693. (GUIConfiguration->SynchronizeParams &
  4694. (TTerminal::spPreviewChanges | TTerminal::spTimestamp | TTerminal::spNotByTime | TTerminal::spBySize));
  4695. Params.Params = GUIConfiguration->SynchronizeParams & ~UnusedParams;
  4696. Params.Options = GUIConfiguration->SynchronizeOptions;
  4697. bool SaveSettings = false;
  4698. TSynchronizeController Controller(&DoSynchronize, &DoSynchronizeInvalid,
  4699. &DoSynchronizeTooManyDirectories);
  4700. DebugAssert(FSynchronizeController == NULL);
  4701. FSynchronizeController = &Controller;
  4702. bool Result;
  4703. try
  4704. {
  4705. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  4706. int CopyParamAttrs = Terminal->UsableCopyParamAttrs(0).Upload;
  4707. int Options =
  4708. FLAGMASK(SynchronizeAllowSelectedOnly(), soAllowSelectedOnly);
  4709. DebugAssert(FOnFeedSynchronizeError == NULL);
  4710. Result = DoSynchronizeDialog(Params, &CopyParam, Controller.StartStop,
  4711. SaveSettings, Options, CopyParamAttrs, GetSynchronizeOptions, SynchronizeSessionLog,
  4712. FOnFeedSynchronizeError, SynchronizeInNewWindow, UseDefaults);
  4713. if (Result)
  4714. {
  4715. if (SaveSettings)
  4716. {
  4717. GUIConfiguration->SynchronizeParams = Params.Params | UnusedParams;
  4718. GUIConfiguration->SynchronizeOptions = Params.Options;
  4719. }
  4720. else
  4721. {
  4722. if (FLAGSET(GUIConfiguration->SynchronizeOptions, soSynchronizeAsk) &&
  4723. FLAGCLEAR(Params.Options, soSynchronizeAsk) &&
  4724. FLAGSET(Params.Options, soSynchronize))
  4725. {
  4726. GUIConfiguration->SynchronizeOptions =
  4727. (GUIConfiguration->SynchronizeOptions & ~soSynchronizeAsk) |
  4728. soSynchronize;
  4729. }
  4730. }
  4731. LocalDirectory = Params.LocalDirectory;
  4732. RemoteDirectory = Params.RemoteDirectory;
  4733. }
  4734. }
  4735. __finally
  4736. {
  4737. FSynchronizeController = NULL;
  4738. DebugAssert(FOnFeedSynchronizeError == NULL);
  4739. FOnFeedSynchronizeError = NULL;
  4740. }
  4741. return Result;
  4742. }
  4743. //---------------------------------------------------------------------------
  4744. void __fastcall TCustomScpExplorerForm::DoSynchronize(
  4745. TSynchronizeController * /*Sender*/, const UnicodeString LocalDirectory,
  4746. const UnicodeString RemoteDirectory, const TCopyParamType & CopyParam,
  4747. const TSynchronizeParamType & Params, TSynchronizeChecklist ** Checklist,
  4748. TSynchronizeOptions * Options, bool Full)
  4749. {
  4750. if (Terminal->Status == ssOpened)
  4751. {
  4752. try
  4753. {
  4754. int PParams = Params.Params;
  4755. if (!Full)
  4756. {
  4757. PParams |= TTerminal::spNoRecurse | TTerminal::spUseCache | TTerminal::spDelayProgress;
  4758. if (FLAGSET(Params.Options, soRecurse))
  4759. {
  4760. PParams |= TTerminal::spSubDirs;
  4761. }
  4762. }
  4763. else
  4764. {
  4765. // if keepuptodate is non-recursive,
  4766. // full sync before has to be non-recursive as well
  4767. if (FLAGCLEAR(Params.Options, soRecurse))
  4768. {
  4769. PParams |= TTerminal::spNoRecurse;
  4770. }
  4771. }
  4772. Synchronize(LocalDirectory, RemoteDirectory, smRemote, CopyParam,
  4773. PParams, Checklist, Options);
  4774. }
  4775. catch (Exception & E)
  4776. {
  4777. if (FLAGSET(Params.Options, soContinueOnError) && Terminal->Active &&
  4778. DebugAlwaysTrue(FOnFeedSynchronizeError != NULL))
  4779. {
  4780. // noop, already logged in MoreMessageDialog
  4781. }
  4782. else
  4783. {
  4784. ShowExtendedExceptionEx(Terminal, &E);
  4785. throw;
  4786. }
  4787. }
  4788. }
  4789. }
  4790. //---------------------------------------------------------------------------
  4791. void __fastcall TCustomScpExplorerForm::DoSynchronizeInvalid(
  4792. TSynchronizeController * /*Sender*/, const UnicodeString Directory,
  4793. const UnicodeString ErrorStr)
  4794. {
  4795. if (!Directory.IsEmpty())
  4796. {
  4797. SimpleErrorDialog(FMTLOAD(WATCH_ERROR_DIRECTORY, (Directory)), ErrorStr);
  4798. }
  4799. else
  4800. {
  4801. SimpleErrorDialog(LoadStr(WATCH_ERROR_GENERAL), ErrorStr);
  4802. }
  4803. }
  4804. //---------------------------------------------------------------------------
  4805. void __fastcall TCustomScpExplorerForm::DoSynchronizeTooManyDirectories(
  4806. TSynchronizeController * /*Sender*/, int & MaxDirectories)
  4807. {
  4808. if (MaxDirectories < GUIConfiguration->MaxWatchDirectories)
  4809. {
  4810. MaxDirectories = GUIConfiguration->MaxWatchDirectories;
  4811. }
  4812. else
  4813. {
  4814. TMessageParams Params(mpNeverAskAgainCheck);
  4815. unsigned int Result = MessageDialog(
  4816. FMTLOAD(TOO_MANY_WATCH_DIRECTORIES, (MaxDirectories, MaxDirectories)),
  4817. qtConfirmation, qaYes | qaNo, HELP_TOO_MANY_WATCH_DIRECTORIES, &Params);
  4818. if ((Result == qaYes) || (Result == qaNeverAskAgain))
  4819. {
  4820. MaxDirectories *= 2;
  4821. if (Result == qaNeverAskAgain)
  4822. {
  4823. GUIConfiguration->MaxWatchDirectories = MaxDirectories;
  4824. }
  4825. }
  4826. else
  4827. {
  4828. Abort();
  4829. }
  4830. }
  4831. }
  4832. //---------------------------------------------------------------------------
  4833. void __fastcall TCustomScpExplorerForm::Synchronize(const UnicodeString LocalDirectory,
  4834. const UnicodeString RemoteDirectory, TSynchronizeMode Mode,
  4835. const TCopyParamType & CopyParam, int Params, TSynchronizeChecklist ** Checklist,
  4836. TSynchronizeOptions * Options)
  4837. {
  4838. DebugAssert(!FAutoOperation);
  4839. void * BatchStorage;
  4840. BatchStart(BatchStorage);
  4841. FAutoOperation = true;
  4842. bool AnyOperation = false;
  4843. TDateTime StartTime = Now();
  4844. TSynchronizeChecklist * AChecklist = NULL;
  4845. try
  4846. {
  4847. FSynchronizeProgressForm = new TSynchronizeProgressForm(Application, true, true);
  4848. if (FLAGCLEAR(Params, TTerminal::spDelayProgress))
  4849. {
  4850. FSynchronizeProgressForm->Start();
  4851. }
  4852. AChecklist = Terminal->SynchronizeCollect(LocalDirectory, RemoteDirectory,
  4853. static_cast<TTerminal::TSynchronizeMode>(Mode),
  4854. &CopyParam, Params | TTerminal::spNoConfirmation, TerminalSynchronizeDirectory,
  4855. Options);
  4856. SAFE_DESTROY(FSynchronizeProgressForm);
  4857. FSynchronizeProgressForm = new TSynchronizeProgressForm(Application, true, false);
  4858. if (FLAGCLEAR(Params, TTerminal::spDelayProgress))
  4859. {
  4860. FSynchronizeProgressForm->Start();
  4861. }
  4862. for (int Index = 0; !AnyOperation && (Index < AChecklist->Count); Index++)
  4863. {
  4864. AnyOperation = AChecklist->Item[Index]->Checked;
  4865. }
  4866. // No need to call if !AnyOperation
  4867. Terminal->SynchronizeApply(AChecklist, LocalDirectory, RemoteDirectory,
  4868. &CopyParam, Params | TTerminal::spNoConfirmation, TerminalSynchronizeDirectory);
  4869. }
  4870. __finally
  4871. {
  4872. if (Checklist == NULL)
  4873. {
  4874. delete AChecklist;
  4875. }
  4876. else
  4877. {
  4878. *Checklist = AChecklist;
  4879. }
  4880. FAutoOperation = false;
  4881. SAFE_DESTROY(FSynchronizeProgressForm);
  4882. BatchEnd(BatchStorage);
  4883. ReloadLocalDirectory();
  4884. if (AnyOperation)
  4885. {
  4886. OperationComplete(StartTime);
  4887. }
  4888. }
  4889. }
  4890. //---------------------------------------------------------------------------
  4891. bool __fastcall TCustomScpExplorerForm::SynchronizeAllowSelectedOnly()
  4892. {
  4893. // can be called from command line
  4894. return Visible &&
  4895. ((DirView(osRemote)->SelCount > 0) ||
  4896. (HasDirView[osLocal] && (DirView(osLocal)->SelCount > 0)));
  4897. }
  4898. //---------------------------------------------------------------------------
  4899. void __fastcall TCustomScpExplorerForm::SynchronizeSessionLog(const UnicodeString & Message)
  4900. {
  4901. LogSynchronizeEvent(Terminal, Message);
  4902. }
  4903. //---------------------------------------------------------------------------
  4904. void __fastcall TCustomScpExplorerForm::GetSynchronizeOptions(
  4905. int Params, TSynchronizeOptions & Options)
  4906. {
  4907. if (FLAGSET(Params, TTerminal::spSelectedOnly) && SynchronizeAllowSelectedOnly())
  4908. {
  4909. Options.Filter = new TStringList();
  4910. Options.Filter->CaseSensitive = false;
  4911. Options.Filter->Duplicates = Types::dupAccept;
  4912. if (DirView(osRemote)->SelCount > 0)
  4913. {
  4914. DirView(osRemote)->CreateFileList(false, false, Options.Filter);
  4915. }
  4916. if (HasDirView[osLocal] && (DirView(osLocal)->SelCount > 0))
  4917. {
  4918. DirView(osLocal)->CreateFileList(false, false, Options.Filter);
  4919. }
  4920. Options.Filter->Sort();
  4921. }
  4922. }
  4923. //---------------------------------------------------------------------------
  4924. void __fastcall TCustomScpExplorerForm::SynchronizeInNewWindow(
  4925. const TSynchronizeParamType & Params, const TCopyParamType * CopyParams)
  4926. {
  4927. UnicodeString SessionName = CreateHiddenDuplicateSession();
  4928. UnicodeString AdditionalParams =
  4929. FORMAT(L"%s \"%s\" \"%s\" %d %d %s", (
  4930. TProgramParams::FormatSwitch(KEEP_UP_TO_DATE_SWITCH),
  4931. Params.LocalDirectory, Params.RemoteDirectory, Params.Params, Params.Options,
  4932. TProgramParams::FormatSwitch(DEFAULTS_SWITCH)));
  4933. TCopyParamType Defaults;
  4934. std::unique_ptr<THierarchicalStorage> ConfigStorage(Configuration->CreateConfigStorage());
  4935. ConfigStorage->AccessMode = smRead;
  4936. if (ConfigStorage->OpenSubKey(Configuration->ConfigurationSubKey, false, false))
  4937. {
  4938. GUIConfiguration->LoadCopyParam(ConfigStorage.get(), &Defaults);
  4939. }
  4940. ConfigStorage.reset(NULL);
  4941. std::unique_ptr<TStringList> Options(new TStringList());
  4942. std::unique_ptr<TOptionsStorage> OptionsStorage(new TOptionsStorage(Options.get(), true));
  4943. GUIConfiguration->SaveCopyParam(OptionsStorage.get(), CopyParams, &Defaults);
  4944. if (Options->Count > 0)
  4945. {
  4946. AdditionalParams +=
  4947. FORMAT(L" %s%s", (TProgramParams::FormatSwitch(RAW_CONFIG_SWITCH), StringsToParams(Options.get())));
  4948. }
  4949. ExecuteNewInstance(SessionName, AdditionalParams);
  4950. }
  4951. //---------------------------------------------------------------------------
  4952. bool __fastcall TCustomScpExplorerForm::DoFullSynchronizeDirectories(
  4953. UnicodeString & LocalDirectory, UnicodeString & RemoteDirectory,
  4954. TSynchronizeMode & Mode, bool & SaveMode, bool UseDefaults)
  4955. {
  4956. bool Result;
  4957. int Params = GUIConfiguration->SynchronizeParams;
  4958. bool SaveSettings = false;
  4959. int Options =
  4960. FLAGMASK(!Terminal->IsCapable[fcTimestampChanging], fsoDisableTimestamp) |
  4961. FLAGMASK(SynchronizeAllowSelectedOnly(), fsoAllowSelectedOnly);
  4962. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  4963. TUsableCopyParamAttrs CopyParamAttrs = Terminal->UsableCopyParamAttrs(0);
  4964. Result = UseDefaults ||
  4965. DoFullSynchronizeDialog(Mode, Params, LocalDirectory, RemoteDirectory,
  4966. &CopyParam, SaveSettings, SaveMode, Options, CopyParamAttrs);
  4967. if (Result)
  4968. {
  4969. Configuration->Usage->Inc(L"Synchronizations");
  4970. UpdateCopyParamCounters(CopyParam);
  4971. TSynchronizeOptions SynchronizeOptions;
  4972. GetSynchronizeOptions(Params, SynchronizeOptions);
  4973. if (SaveSettings)
  4974. {
  4975. GUIConfiguration->SynchronizeParams = Params;
  4976. }
  4977. else
  4978. {
  4979. SaveMode = false;
  4980. }
  4981. TDateTime StartTime = Now();
  4982. TSynchronizeChecklist * Checklist = NULL;
  4983. try
  4984. {
  4985. DebugAssert(!FAutoOperation);
  4986. FAutoOperation = true;
  4987. try
  4988. {
  4989. FSynchronizeProgressForm = new TSynchronizeProgressForm(Application, true, true);
  4990. FSynchronizeProgressForm->Start();
  4991. Checklist = Terminal->SynchronizeCollect(LocalDirectory, RemoteDirectory,
  4992. static_cast<TTerminal::TSynchronizeMode>(Mode),
  4993. &CopyParam, Params | TTerminal::spNoConfirmation, TerminalSynchronizeDirectory,
  4994. &SynchronizeOptions);
  4995. }
  4996. __finally
  4997. {
  4998. FAutoOperation = false;
  4999. SAFE_DESTROY(FSynchronizeProgressForm);
  5000. }
  5001. if (Checklist->Count == 0)
  5002. {
  5003. UnicodeString Message = MainInstructions(LoadStr(COMPARE_NO_DIFFERENCES));
  5004. MessageDialog(Message, qtInformation, qaOK,
  5005. HELP_SYNCHRONIZE_NO_DIFFERENCES);
  5006. }
  5007. else if (FLAGCLEAR(Params, TTerminal::spPreviewChanges) ||
  5008. DoSynchronizeChecklistDialog(Checklist, Mode, Params,
  5009. LocalDirectory, RemoteDirectory, CustomCommandMenu))
  5010. {
  5011. DebugAssert(!FAutoOperation);
  5012. void * BatchStorage;
  5013. BatchStart(BatchStorage);
  5014. FAutoOperation = true;
  5015. if (FLAGSET(Params, TTerminal::spPreviewChanges))
  5016. {
  5017. StartTime = Now();
  5018. }
  5019. try
  5020. {
  5021. FSynchronizeProgressForm = new TSynchronizeProgressForm(Application, true, false);
  5022. FSynchronizeProgressForm->Start();
  5023. Terminal->SynchronizeApply(Checklist, LocalDirectory, RemoteDirectory,
  5024. &CopyParam, Params | TTerminal::spNoConfirmation, TerminalSynchronizeDirectory);
  5025. }
  5026. __finally
  5027. {
  5028. FAutoOperation = false;
  5029. SAFE_DESTROY(FSynchronizeProgressForm);
  5030. BatchEnd(BatchStorage);
  5031. ReloadLocalDirectory();
  5032. }
  5033. }
  5034. }
  5035. __finally
  5036. {
  5037. delete Checklist;
  5038. }
  5039. OperationComplete(StartTime);
  5040. }
  5041. return Result;
  5042. }
  5043. //---------------------------------------------------------------------------
  5044. void __fastcall TCustomScpExplorerForm::TerminalSynchronizeDirectory(
  5045. const UnicodeString LocalDirectory, const UnicodeString RemoteDirectory,
  5046. bool & Continue, bool /*Collect*/)
  5047. {
  5048. DebugAssert(FSynchronizeProgressForm != NULL);
  5049. if (!FSynchronizeProgressForm->Started)
  5050. {
  5051. FSynchronizeProgressForm->Start();
  5052. }
  5053. FSynchronizeProgressForm->SetData(LocalDirectory, RemoteDirectory, Continue);
  5054. }
  5055. //---------------------------------------------------------------------------
  5056. void __fastcall TCustomScpExplorerForm::StandaloneEdit(const UnicodeString & FileName)
  5057. {
  5058. UnicodeString FullFileName = AbsolutePath(FTerminal->CurrentDirectory, FileName);
  5059. TRemoteFile * File = NULL;
  5060. Terminal->ReadFile(FullFileName, File);
  5061. if (File != NULL)
  5062. {
  5063. std::unique_ptr<TRemoteFile> FileOwner(File);
  5064. TAutoFlag Flag(FStandaloneEditing);
  5065. ExecuteRemoteFile(FullFileName, File, efInternalEditor);
  5066. Application->ShowMainForm = false;
  5067. Application->Run();
  5068. }
  5069. }
  5070. //---------------------------------------------------------------------------
  5071. void __fastcall TCustomScpExplorerForm::ExploreLocalDirectory()
  5072. {
  5073. DebugFail();
  5074. }
  5075. //---------------------------------------------------------------------------
  5076. TSessionData * __fastcall TCustomScpExplorerForm::CloneCurrentSessionData()
  5077. {
  5078. std::unique_ptr<TSessionData> SessionData(new TSessionData(L""));
  5079. SessionData->Assign(Terminal->SessionData);
  5080. UpdateSessionData(SessionData.get());
  5081. TTerminalManager::Instance()->UpdateSessionCredentials(SessionData.get());
  5082. if (Terminal->SessionData->IsWorkspace)
  5083. {
  5084. // Have to reset the "Workspace/XXX" name which would become user-visible
  5085. // once IsWorkspace is cleared
  5086. SessionData->Name = UnicodeString();
  5087. SessionData->IsWorkspace = false;
  5088. }
  5089. return SessionData.release();
  5090. }
  5091. //---------------------------------------------------------------------------
  5092. void __fastcall TCustomScpExplorerForm::SaveCurrentSession()
  5093. {
  5094. TSessionData * SessionData = CloneCurrentSessionData();
  5095. try
  5096. {
  5097. TSessionData * EditingSessionData = StoredSessions->FindSame(SessionData);
  5098. DoSaveSession(SessionData, EditingSessionData, true, NULL);
  5099. }
  5100. __finally
  5101. {
  5102. delete SessionData;
  5103. }
  5104. }
  5105. //---------------------------------------------------------------------------
  5106. TObjectList * __fastcall TCustomScpExplorerForm::DoCollectWorkspace()
  5107. {
  5108. TTerminalManager * Manager = TTerminalManager::Instance();
  5109. std::unique_ptr<TObjectList> DataList(new TObjectList());
  5110. if (DebugAlwaysTrue(Terminal != NULL))
  5111. {
  5112. // Update (Managed)Terminal->StateData
  5113. UpdateTerminal(Terminal);
  5114. }
  5115. Manager->SaveWorkspace(DataList.get());
  5116. return DataList.release();
  5117. }
  5118. //---------------------------------------------------------------------------
  5119. void __fastcall TCustomScpExplorerForm::DoSaveWorkspace(
  5120. const UnicodeString & Name, TObjectList * DataList, bool SavePasswords)
  5121. {
  5122. WinConfiguration->LastWorkspace = Name;
  5123. if (!SavePasswords)
  5124. {
  5125. for (int Index = 0; Index < DataList->Count; Index++)
  5126. {
  5127. TSessionData * SessionData = dynamic_cast<TSessionData *>(DataList->Items[Index]);
  5128. if (SessionData->Link.IsEmpty())
  5129. {
  5130. SessionData->ClearSessionPasswords();
  5131. }
  5132. }
  5133. }
  5134. StoredSessions->NewWorkspace(Name, DataList);
  5135. // modified only, explicit
  5136. StoredSessions->Save(false, true);
  5137. }
  5138. //---------------------------------------------------------------------------
  5139. UnicodeString __fastcall TCustomScpExplorerForm::WorkspaceName()
  5140. {
  5141. return
  5142. DefaultStr(WinConfiguration->LastWorkspace,
  5143. DefaultStr(WinConfiguration->AutoWorkspace, LoadStr(NEW_WORKSPACE)));
  5144. }
  5145. //---------------------------------------------------------------------------
  5146. bool __fastcall TCustomScpExplorerForm::SaveWorkspace(bool EnableAutoSave)
  5147. {
  5148. std::unique_ptr<TObjectList> DataList(DoCollectWorkspace());
  5149. bool AnyNonStoredSessionWithPassword = false;
  5150. bool AnyNonStoredNonWorkspaceSessionWithPassword = false;
  5151. bool AllNonStoredSessionsAnonymous = true;
  5152. TTerminalManager * Manager = TTerminalManager::Instance();
  5153. for (int Index = 0; Index < DataList->Count; Index++)
  5154. {
  5155. TSessionData * SessionData = dynamic_cast<TSessionData *>(DataList->Items[Index]);
  5156. if (SessionData->Link.IsEmpty())
  5157. {
  5158. if (SessionData->HasAnySessionPassword())
  5159. {
  5160. AnyNonStoredSessionWithPassword = true;
  5161. if (!Manager->Terminals[Index]->SessionData->IsWorkspace)
  5162. {
  5163. AnyNonStoredNonWorkspaceSessionWithPassword = true;
  5164. }
  5165. }
  5166. if (!SameText(SessionData->UserName, AnonymousUserName))
  5167. {
  5168. AllNonStoredSessionsAnonymous = false;
  5169. }
  5170. }
  5171. }
  5172. bool SavePasswords;
  5173. bool * PSavePasswords;
  5174. bool NotRecommendedSavingPasswords =
  5175. !CustomWinConfiguration->UseMasterPassword &&
  5176. !AllNonStoredSessionsAnonymous;
  5177. if (Configuration->DisablePasswordStoring ||
  5178. !AnyNonStoredSessionWithPassword)
  5179. {
  5180. PSavePasswords = NULL;
  5181. SavePasswords = false;
  5182. }
  5183. else
  5184. {
  5185. PSavePasswords = &SavePasswords;
  5186. SavePasswords =
  5187. !AnyNonStoredNonWorkspaceSessionWithPassword ||
  5188. !NotRecommendedSavingPasswords;
  5189. }
  5190. UnicodeString Name = WorkspaceName();
  5191. bool CreateShortcut = false;
  5192. bool Result =
  5193. DoSaveWorkspaceDialog(
  5194. Name, PSavePasswords, NotRecommendedSavingPasswords, CreateShortcut,
  5195. EnableAutoSave);
  5196. if (Result)
  5197. {
  5198. DoSaveWorkspace(Name, DataList.get(), SavePasswords);
  5199. if (CreateShortcut)
  5200. {
  5201. TOperationVisualizer Visualizer;
  5202. UnicodeString AdditionalParams = TProgramParams::FormatSwitch(DESKTOP_SWITCH);
  5203. CreateDesktopSessionShortCut(Name, L"", AdditionalParams, -1, WORKSPACE_ICON);
  5204. }
  5205. if (EnableAutoSave)
  5206. {
  5207. WinConfiguration->AutoSaveWorkspace = true;
  5208. WinConfiguration->AutoWorkspace = Name;
  5209. if (PSavePasswords != NULL)
  5210. {
  5211. WinConfiguration->AutoSaveWorkspacePasswords = SavePasswords;
  5212. }
  5213. }
  5214. }
  5215. return Result;
  5216. }
  5217. //---------------------------------------------------------------------------
  5218. void __fastcall TCustomScpExplorerForm::UpdateTerminal(TTerminal * Terminal)
  5219. {
  5220. TManagedTerminal * ManagedTerminal = dynamic_cast<TManagedTerminal *>(Terminal);
  5221. DebugAssert(ManagedTerminal != NULL);
  5222. SAFE_DESTROY(ManagedTerminal->RemoteExplorerState);
  5223. if (WinConfiguration->PreservePanelState)
  5224. {
  5225. ManagedTerminal->RemoteExplorerState = RemoteDirView->SaveState();
  5226. }
  5227. UpdateSessionData(ManagedTerminal->StateData);
  5228. }
  5229. //---------------------------------------------------------------------------
  5230. void __fastcall TCustomScpExplorerForm::UpdateSessionData(TSessionData * Data)
  5231. {
  5232. // Keep in sync with TSessionData::CopyStateData
  5233. DebugAssert(Data != NULL);
  5234. // cannot use RemoteDirView->Path, because it is empty if connection
  5235. // was already closed
  5236. // also only peek, we may not be connected at all atm
  5237. // so make sure we do not try retrieving current directory from the server
  5238. // (particularly with FTP)
  5239. UnicodeString ACurrentDirectory = Terminal->PeekCurrentDirectory();
  5240. if (!ACurrentDirectory.IsEmpty())
  5241. {
  5242. Data->RemoteDirectory = ACurrentDirectory;
  5243. }
  5244. Data->Color = SessionColor;
  5245. }
  5246. //---------------------------------------------------------------------------
  5247. void __fastcall TCustomScpExplorerForm::ToolBarResize(TObject *Sender)
  5248. {
  5249. TTBXToolbar * Toolbar = dynamic_cast<TTBXToolbar*>(Sender);
  5250. DebugAssert(Toolbar != NULL);
  5251. for (int i = 0; i < Toolbar->Items->Count; i++)
  5252. {
  5253. TTBXCustomDropDownItem * DropDownItem;
  5254. DropDownItem = dynamic_cast<TTBXCustomDropDownItem *>(Toolbar->Items->Items[i]);
  5255. if (DropDownItem != NULL)
  5256. {
  5257. ToolbarItemResize(DropDownItem,
  5258. Toolbar->Width - (Toolbar->View->BaseSize.x - DropDownItem->EditWidth) -
  5259. Toolbar->NonClientWidth);
  5260. break;
  5261. }
  5262. }
  5263. }
  5264. //---------------------------------------------------------------------------
  5265. void __fastcall TCustomScpExplorerForm::ToolbarItemResize(TTBXCustomDropDownItem * Item, int Width)
  5266. {
  5267. Item->EditWidth = Width;
  5268. }
  5269. //---------------------------------------------------------------------------
  5270. void __fastcall TCustomScpExplorerForm::ToolbarGetBaseSize(
  5271. TTBCustomToolbar * Toolbar, TPoint & ASize)
  5272. {
  5273. for (int i = 0; i < Toolbar->Items->Count; i++)
  5274. {
  5275. TTBXCustomDropDownItem * DropDownItem;
  5276. DropDownItem = dynamic_cast<TTBXCustomDropDownItem *>(Toolbar->Items->Items[i]);
  5277. if (DropDownItem != NULL)
  5278. {
  5279. ASize.x -= DropDownItem->EditWidth;
  5280. ASize.x += ScaleByTextHeight(this, 50) /* minimal combo width */;
  5281. }
  5282. }
  5283. }
  5284. //---------------------------------------------------------------------------
  5285. void __fastcall TCustomScpExplorerForm::DoWarnLackOfTempSpace(
  5286. const UnicodeString Path, __int64 RequiredSpace, bool & Continue)
  5287. {
  5288. if (WinConfiguration->DDWarnLackOfTempSpace)
  5289. {
  5290. UnicodeString ADrive = ExtractFileDrive(ExpandFileName(Path));
  5291. if (!ADrive.IsEmpty())
  5292. {
  5293. __int64 FreeSpace = DiskFree((Byte)(ADrive[1]-'A'+1));
  5294. DebugAssert(RequiredSpace >= 0);
  5295. __int64 RequiredWithReserve;
  5296. RequiredWithReserve = (__int64)(RequiredSpace * WinConfiguration->DDWarnLackOfTempSpaceRatio);
  5297. if (FreeSpace < RequiredWithReserve)
  5298. {
  5299. unsigned int Result;
  5300. TMessageParams Params(mpNeverAskAgainCheck);
  5301. Result = MessageDialog(FMTLOAD(DD_WARN_LACK_OF_TEMP_SPACE, (Path,
  5302. FormatBytes(FreeSpace), FormatBytes(RequiredSpace))),
  5303. qtWarning, qaYes | qaNo, HELP_DD_WARN_LACK_OF_TEMP_SPACE, &Params);
  5304. if (Result == qaNeverAskAgain)
  5305. {
  5306. WinConfiguration->DDWarnLackOfTempSpace = false;
  5307. }
  5308. Continue = (Result == qaYes || Result == qaNeverAskAgain);
  5309. }
  5310. }
  5311. }
  5312. }
  5313. //---------------------------------------------------------------------------
  5314. void __fastcall TCustomScpExplorerForm::AddBookmark(TOperationSide Side)
  5315. {
  5316. DoOpenDirectoryDialog(odAddBookmark, Side);
  5317. }
  5318. //---------------------------------------------------------------------------
  5319. TStrings * __fastcall TCustomScpExplorerForm::CreateVisitedDirectories(TOperationSide Side)
  5320. {
  5321. // we should better use TCustomDirView::FCaseSensitive, but it is private
  5322. TStringList * VisitedDirectories = CreateSortedStringList((Side == osRemote));
  5323. try
  5324. {
  5325. TCustomDirView * DView = DirView(Side);
  5326. for (int Index = -DView->BackCount; Index <= DView->ForwardCount; Index++)
  5327. {
  5328. VisitedDirectories->Add(DView->HistoryPath[Index]);
  5329. }
  5330. }
  5331. catch (...)
  5332. {
  5333. delete VisitedDirectories;
  5334. throw;
  5335. }
  5336. return VisitedDirectories;
  5337. }
  5338. //---------------------------------------------------------------------------
  5339. void __fastcall TCustomScpExplorerForm::DoOpenDirectoryDialog(
  5340. TOpenDirectoryMode Mode, TOperationSide Side)
  5341. {
  5342. bool Continue = true;
  5343. if (Mode == odAddBookmark)
  5344. {
  5345. TMessageParams Params(mpNeverAskAgainCheck);
  5346. Params.NeverAskAgainTitle = LoadStr(ADD_BOOKMARK_SHARED);
  5347. Params.NeverAskAgainCheckedInitially = WinConfiguration->UseSharedBookmarks;
  5348. UnicodeString Message = MainInstructions(FMTLOAD(ADD_BOOKMARK_CONFIRM, (DirView(Side)->PathName)));
  5349. unsigned int Answer =
  5350. MessageDialog(Message,
  5351. qtConfirmation, qaOK | qaCancel, HELP_ADD_BOOKMARK_CONFIRM, &Params);
  5352. if (Answer == qaNeverAskAgain)
  5353. {
  5354. Continue = true;
  5355. WinConfiguration->UseSharedBookmarks = true;
  5356. }
  5357. else if (Answer == qaOK)
  5358. {
  5359. Continue = true;
  5360. WinConfiguration->UseSharedBookmarks = false;
  5361. }
  5362. else
  5363. {
  5364. Continue = false;
  5365. }
  5366. }
  5367. if (Continue)
  5368. {
  5369. TStrings * VisitedDirectories = CreateVisitedDirectories(Side);
  5370. try
  5371. {
  5372. UnicodeString Name = DirView(Side)->PathName;
  5373. if (::DoOpenDirectoryDialog(Mode, Side, Name, VisitedDirectories, Terminal,
  5374. // do not allow switching to location profiles,
  5375. // if we are not connected
  5376. HasDirView[osLocal] && (Terminal != NULL)))
  5377. {
  5378. TWindowLock Lock(this);
  5379. DirView(Side)->Path = Name;
  5380. }
  5381. }
  5382. __finally
  5383. {
  5384. delete VisitedDirectories;
  5385. }
  5386. }
  5387. }
  5388. //---------------------------------------------------------------------------
  5389. bool __fastcall TCustomScpExplorerForm::CommandSessionFallback()
  5390. {
  5391. DebugAssert(!FTerminal->CommandSessionOpened);
  5392. return TTerminalManager::Instance()->ConnectTerminal(FTerminal->CommandSession);
  5393. }
  5394. //---------------------------------------------------------------------------
  5395. bool __fastcall TCustomScpExplorerForm::EnsureCommandSessionFallback(TFSCapability Capability)
  5396. {
  5397. bool Result = FTerminal->IsCapable[Capability] ||
  5398. FTerminal->CommandSessionOpened;
  5399. if (!Result)
  5400. {
  5401. DebugAssert(FTerminal->IsCapable[fcSecondaryShell]);
  5402. if (!GUIConfiguration->ConfirmCommandSession)
  5403. {
  5404. Result = true;
  5405. }
  5406. else
  5407. {
  5408. TMessageParams Params(mpNeverAskAgainCheck);
  5409. const TFileSystemInfo & FileSystemInfo = Terminal->GetFileSystemInfo();
  5410. unsigned int Answer = MessageDialog(FMTLOAD(PERFORM_ON_COMMAND_SESSION2,
  5411. (FileSystemInfo.ProtocolName, FileSystemInfo.ProtocolName)), qtConfirmation,
  5412. qaOK | qaCancel, HELP_PERFORM_ON_COMMAND_SESSION, &Params);
  5413. if (Answer == qaNeverAskAgain)
  5414. {
  5415. GUIConfiguration->ConfirmCommandSession = false;
  5416. Result = true;
  5417. }
  5418. else if (Answer == qaOK)
  5419. {
  5420. Result = true;
  5421. }
  5422. }
  5423. if (Result)
  5424. {
  5425. Result = CommandSessionFallback();
  5426. }
  5427. }
  5428. return Result;
  5429. }
  5430. //---------------------------------------------------------------------------
  5431. void __fastcall TCustomScpExplorerForm::OpenConsole(UnicodeString Command)
  5432. {
  5433. if (EnsureCommandSessionFallback(fcAnyCommand))
  5434. {
  5435. DoConsoleDialog(Terminal, Command);
  5436. }
  5437. }
  5438. //---------------------------------------------------------------------------
  5439. void __fastcall TCustomScpExplorerForm::FileControlDDDragEnter(
  5440. TObject *Sender, _di_IDataObject /*DataObj*/, int /*grfKeyState*/,
  5441. const TPoint & /*Point*/, int & /*dwEffect*/, bool & Accept)
  5442. {
  5443. if (IsFileControl(DropSourceControl, osRemote) &&
  5444. (FDDExtMapFile != NULL))
  5445. {
  5446. Accept = true;
  5447. }
  5448. FDDTargetControl = dynamic_cast<TControl*>(Sender);
  5449. DebugAssert(FDDTargetControl != NULL);
  5450. }
  5451. //---------------------------------------------------------------------------
  5452. void __fastcall TCustomScpExplorerForm::SessionsDDDragEnter(
  5453. _di_IDataObject DataObj, int KeyState,
  5454. const TPoint & Point, int & Effect, bool & Accept)
  5455. {
  5456. FileControlDDDragEnter(SessionsPageControl, DataObj, KeyState, Point, Effect, Accept);
  5457. }
  5458. //---------------------------------------------------------------------------
  5459. void __fastcall TCustomScpExplorerForm::QueueDDDragEnter(
  5460. _di_IDataObject DataObj, int KeyState,
  5461. const TPoint & Point, int & Effect, bool & Accept)
  5462. {
  5463. FileControlDDDragEnter(QueueView3, DataObj, KeyState, Point, Effect, Accept);
  5464. }
  5465. //---------------------------------------------------------------------------
  5466. void __fastcall TCustomScpExplorerForm::FileControlDDDragLeave(
  5467. TObject * Sender)
  5468. {
  5469. DebugUsedParam(Sender);
  5470. DebugAssert(FDDTargetControl == Sender);
  5471. FDDTargetControl = NULL;
  5472. }
  5473. //---------------------------------------------------------------------------
  5474. void __fastcall TCustomScpExplorerForm::SessionsDDDragLeave()
  5475. {
  5476. FileControlDDDragLeave(SessionsPageControl);
  5477. }
  5478. //---------------------------------------------------------------------------
  5479. void __fastcall TCustomScpExplorerForm::QueueDDDragLeave()
  5480. {
  5481. FileControlDDDragLeave(QueueView3);
  5482. }
  5483. //---------------------------------------------------------------------------
  5484. void __fastcall TCustomScpExplorerForm::AddEditLink(TOperationSide Side, bool Add)
  5485. {
  5486. DebugAssert(GetSide(Side) == osRemote);
  5487. DebugUsedParam(Side);
  5488. bool Edit = false;
  5489. TRemoteFile * File = NULL;
  5490. UnicodeString FileName;
  5491. UnicodeString PointTo;
  5492. bool SymbolicLink = true;
  5493. if (RemoteDirView->ItemFocused)
  5494. {
  5495. DebugAssert(RemoteDirView->ItemFocused->Data);
  5496. File = (TRemoteFile *)RemoteDirView->ItemFocused->Data;
  5497. Edit = !Add && File->IsSymLink && Terminal->SessionData->ResolveSymlinks;
  5498. if (Edit)
  5499. {
  5500. FileName = File->FileName;
  5501. PointTo = File->LinkTo;
  5502. }
  5503. else
  5504. {
  5505. PointTo = File->FileName;
  5506. }
  5507. }
  5508. if (DoSymlinkDialog(FileName, PointTo, osRemote, SymbolicLink, Edit,
  5509. Terminal->IsCapable[fcHardLink]))
  5510. {
  5511. Configuration->Usage->Inc(L"RemoteLinksCreated");
  5512. if (Edit)
  5513. {
  5514. DebugAssert(File->FileName == FileName);
  5515. int Params = dfNoRecursive;
  5516. Terminal->ExceptionOnFail = true;
  5517. try
  5518. {
  5519. Terminal->DeleteFile(L"", File, &Params);
  5520. }
  5521. __finally
  5522. {
  5523. Terminal->ExceptionOnFail = false;
  5524. }
  5525. }
  5526. Terminal->CreateLink(FileName, PointTo, SymbolicLink);
  5527. }
  5528. }
  5529. //---------------------------------------------------------------------------
  5530. bool __fastcall TCustomScpExplorerForm::CanAddEditLink(TOperationSide Side)
  5531. {
  5532. return
  5533. (Terminal != NULL) &&
  5534. ((GetSide(Side) != osRemote) ||
  5535. (Terminal->ResolvingSymlinks &&
  5536. Terminal->IsCapable[fcSymbolicLink]));
  5537. }
  5538. //---------------------------------------------------------------------------
  5539. bool __fastcall TCustomScpExplorerForm::LinkFocused()
  5540. {
  5541. return
  5542. (FCurrentSide == osRemote) &&
  5543. (RemoteDirView->ItemFocused != NULL) &&
  5544. ((TRemoteFile *)RemoteDirView->ItemFocused->Data)->IsSymLink &&
  5545. Terminal->SessionData->ResolveSymlinks;
  5546. }
  5547. //---------------------------------------------------------------------------
  5548. void __fastcall TCustomScpExplorerForm::ExecuteCurrentFile()
  5549. {
  5550. DebugAssert(!WinConfiguration->DisableOpenEdit);
  5551. FForceExecution = true;
  5552. try
  5553. {
  5554. DirView(osCurrent)->ExecuteCurrentFile();
  5555. }
  5556. __finally
  5557. {
  5558. FForceExecution = false;
  5559. }
  5560. }
  5561. //---------------------------------------------------------------------------
  5562. void __fastcall TCustomScpExplorerForm::ExecuteCurrentFileWith(bool OnFocused)
  5563. {
  5564. TEditorData ExternalEditor;
  5565. ExternalEditor.Editor = edExternal;
  5566. bool Remember = false;
  5567. if (DoEditorPreferencesDialog(&ExternalEditor, Remember, epmAdHoc,
  5568. (GetSide(osCurrent) == osRemote)))
  5569. {
  5570. if (Remember)
  5571. {
  5572. TEditorList * EditorList = new TEditorList;
  5573. try
  5574. {
  5575. *EditorList = *WinConfiguration->EditorList;
  5576. bool Found = false;
  5577. int i = 0;
  5578. while (!Found && (i < EditorList->Count))
  5579. {
  5580. const TEditorPreferences * Editor = EditorList->Editors[i];
  5581. if ((Editor->Data->Editor == edExternal) &&
  5582. (Editor->Data->ExternalEditor == ExternalEditor.ExternalEditor))
  5583. {
  5584. Found = true;
  5585. }
  5586. i++;
  5587. }
  5588. if (!Found)
  5589. {
  5590. EditorList->Add(new TEditorPreferences(ExternalEditor));
  5591. WinConfiguration->EditorList = EditorList;
  5592. }
  5593. }
  5594. __finally
  5595. {
  5596. delete EditorList;
  5597. }
  5598. }
  5599. ExecuteFile(osCurrent, efExternalEditor, &ExternalEditor, true, OnFocused);
  5600. }
  5601. }
  5602. //---------------------------------------------------------------------------
  5603. void __fastcall TCustomScpExplorerForm::TerminalRemoved(TObject * Sender)
  5604. {
  5605. FEditorManager->ProcessFiles(FileTerminalRemoved, Sender);
  5606. if (FFileFindTerminal == Sender)
  5607. {
  5608. FFileFindTerminal = NULL;
  5609. HideFileFindDialog();
  5610. }
  5611. }
  5612. //---------------------------------------------------------------------------
  5613. void __fastcall TCustomScpExplorerForm::FileTerminalRemoved(const UnicodeString FileName,
  5614. TEditedFileData * Data, TObject * /*Token*/, void * Arg)
  5615. {
  5616. TTerminal * Terminal = static_cast<TTerminal *>(Arg);
  5617. DebugAssert(Terminal != NULL);
  5618. if (Data->Terminal == Terminal)
  5619. {
  5620. Data->Terminal = NULL;
  5621. }
  5622. }
  5623. //---------------------------------------------------------------------------
  5624. void __fastcall TCustomScpExplorerForm::LastTerminalClosed(TObject * /*Sender*/)
  5625. {
  5626. UpdateControls();
  5627. SessionColor = TColor(0);
  5628. UpdateRemotePathComboBox(false);
  5629. NeedSession(true);
  5630. }
  5631. //---------------------------------------------------------------------------
  5632. void __fastcall TCustomScpExplorerForm::NeedSession(bool ReloadSessions)
  5633. {
  5634. try
  5635. {
  5636. try
  5637. {
  5638. TTerminalManager::Instance()->NewSession(false, L"", ReloadSessions, this);
  5639. }
  5640. __finally
  5641. {
  5642. if (!WinConfiguration->KeepOpenWhenNoSession &&
  5643. (!Terminal || !Terminal->Active))
  5644. {
  5645. TerminateApplication();
  5646. }
  5647. }
  5648. }
  5649. catch (EAbort &)
  5650. {
  5651. // swallow
  5652. // The TTerminalManager does not expect the OnLastTerminalClose to throw without trying to connect.
  5653. // Also when called from TWinControl.UpdateShowing => CMShowingChanged, the showing is aborted on exception
  5654. }
  5655. }
  5656. //---------------------------------------------------------------------------
  5657. void __fastcall TCustomScpExplorerForm::DoTerminalListChanged(bool Force)
  5658. {
  5659. TStrings * TerminalList = TTerminalManager::Instance()->TerminalList;
  5660. int ActiveTerminalIndex = TTerminalManager::Instance()->ActiveTerminalIndex;
  5661. Configuration->Usage->SetMax(L"MaxOpenedSessions", TerminalList->Count);
  5662. bool ListChanged = Force || (TerminalList->Count + 1 != SessionsPageControl->PageCount);
  5663. if (!ListChanged)
  5664. {
  5665. int Index = 0;
  5666. while (!ListChanged && (Index < TerminalList->Count))
  5667. {
  5668. ListChanged =
  5669. (GetSessionTabTerminal(SessionsPageControl->Pages[Index]) != TerminalList->Objects[Index]) ||
  5670. (SessionsPageControl->Pages[Index]->Caption != TerminalList->Strings[Index]);
  5671. Index++;
  5672. }
  5673. }
  5674. if (ListChanged)
  5675. {
  5676. SendMessage(SessionsPageControl->Handle, WM_SETREDRAW, 0, 0);
  5677. try
  5678. {
  5679. FSessionColors->Clear();
  5680. AddFixedSessionImages();
  5681. while (SessionsPageControl->PageCount > 0)
  5682. {
  5683. delete SessionsPageControl->Pages[0];
  5684. }
  5685. for (int Index = 0; Index < TerminalList->Count; Index++)
  5686. {
  5687. TThemeTabSheet * TabSheet = new TThemeTabSheet(SessionsPageControl);
  5688. TabSheet->Caption = TerminalList->Strings[Index];
  5689. TTerminal * Terminal = dynamic_cast<TTerminal *>(TerminalList->Objects[Index]);
  5690. TabSheet->Tag = reinterpret_cast<int>(Terminal);
  5691. TabSheet->PageControl = SessionsPageControl;
  5692. UpdateSessionTab(TabSheet);
  5693. }
  5694. TTabSheet * TabSheet = new TTabSheet(SessionsPageControl);
  5695. TabSheet->PageControl = SessionsPageControl;
  5696. TabSheet->ImageIndex = FNewSessionTabImageIndex;
  5697. UpdateNewSessionTab();
  5698. }
  5699. __finally
  5700. {
  5701. SendMessage(SessionsPageControl->Handle, WM_SETREDRAW, 1, 0);
  5702. }
  5703. }
  5704. SessionsPageControl->ActivePageIndex = ActiveTerminalIndex;
  5705. }
  5706. //---------------------------------------------------------------------------
  5707. void __fastcall TCustomScpExplorerForm::TerminalListChanged(TObject * /*Sender*/)
  5708. {
  5709. DoTerminalListChanged(false);
  5710. }
  5711. //---------------------------------------------------------------------------
  5712. void __fastcall TCustomScpExplorerForm::UpdateNewSessionTab()
  5713. {
  5714. TTabSheet * TabSheet = SessionsPageControl->Pages[SessionsPageControl->PageCount - 1];
  5715. DebugAssert(TabSheet->ImageIndex == 0);
  5716. TabSheet->Caption =
  5717. WinConfiguration->SelectiveToolbarText ?
  5718. StripHotkey(StripTrailingPunctuation(NonVisualDataModule->NewSessionAction->Caption)) :
  5719. UnicodeString();
  5720. }
  5721. //---------------------------------------------------------------------------
  5722. TTerminal * __fastcall TCustomScpExplorerForm::GetSessionTabTerminal(TTabSheet * TabSheet)
  5723. {
  5724. return reinterpret_cast<TTerminal *>(TabSheet->Tag);
  5725. }
  5726. //---------------------------------------------------------------------------
  5727. void __fastcall TCustomScpExplorerForm::UpdateSessionTab(TTabSheet * TabSheet)
  5728. {
  5729. if (DebugAlwaysTrue(TabSheet != NULL))
  5730. {
  5731. TManagedTerminal * ManagedTerminal =
  5732. dynamic_cast<TManagedTerminal *>(GetSessionTabTerminal(TabSheet));
  5733. if (DebugAlwaysTrue(ManagedTerminal != NULL))
  5734. {
  5735. TColor Color =
  5736. (ManagedTerminal == FTerminal) ? FSessionColor : ManagedTerminal->StateData->Color;
  5737. TabSheet->ImageIndex = AddSessionColor(Color);
  5738. TabSheet->Caption = TTerminalManager::Instance()->GetTerminalTitle(ManagedTerminal, true);
  5739. TThemeTabSheet * ThemeTabSheet = dynamic_cast<TThemeTabSheet *>(TabSheet);
  5740. if (DebugAlwaysTrue(ThemeTabSheet != NULL))
  5741. {
  5742. ThemeTabSheet->Shadowed = !ManagedTerminal->Active;
  5743. }
  5744. }
  5745. }
  5746. }
  5747. //---------------------------------------------------------------------------
  5748. bool __fastcall TCustomScpExplorerForm::SessionTabSwitched()
  5749. {
  5750. DebugAssert(SessionsPageControl->ActivePage != NULL);
  5751. TTerminal * Terminal = GetSessionTabTerminal(SessionsPageControl->ActivePage);
  5752. bool Result = (Terminal != NULL);
  5753. if (Result)
  5754. {
  5755. TTerminalManager::Instance()->ActiveTerminal = Terminal;
  5756. }
  5757. else
  5758. {
  5759. try
  5760. {
  5761. NewSession(false);
  5762. }
  5763. __finally
  5764. {
  5765. DoTerminalListChanged(false);
  5766. }
  5767. FSessionsPageControlNewSessionTime = Now();
  5768. }
  5769. return Result;
  5770. }
  5771. //---------------------------------------------------------------------------
  5772. void __fastcall TCustomScpExplorerForm::SessionsPageControlChange(TObject * /*Sender*/)
  5773. {
  5774. SessionTabSwitched();
  5775. }
  5776. //---------------------------------------------------------------------------
  5777. void __fastcall TCustomScpExplorerForm::TransferListChange(TObject * Sender)
  5778. {
  5779. TTBXStringList * TransferList = dynamic_cast<TTBXStringList *>(Sender);
  5780. DebugAssert(TransferList != NULL);
  5781. UnicodeString Name;
  5782. if (TransferList->ItemIndex <= 0)
  5783. {
  5784. Name = L"";
  5785. }
  5786. else
  5787. {
  5788. Name = GUIConfiguration->CopyParamList->Names[TransferList->ItemIndex - 1];
  5789. }
  5790. if (FCopyParamAutoSelected.IsEmpty())
  5791. {
  5792. // if previous preset was not autoselected, make new preset the "default"
  5793. FCopyParamDefault = Name;
  5794. }
  5795. GUIConfiguration->CopyParamCurrent = Name;
  5796. }
  5797. //---------------------------------------------------------------------------
  5798. void __fastcall TCustomScpExplorerForm::UpdateTransferLabel()
  5799. {
  5800. // sanity check
  5801. bool ExistingPreset =
  5802. (FTransferListHoverIndex >= 0) &&
  5803. (FTransferListHoverIndex < 1 + GUIConfiguration->CopyParamList->Count);
  5804. DebugAssert(ExistingPreset);
  5805. if (ExistingPreset)
  5806. {
  5807. HDC DC = GetDC(0);
  5808. TCanvas * Canvas = new TCanvas();
  5809. try
  5810. {
  5811. Canvas->Handle = DC;
  5812. Canvas->Font = GetToolbarFont(this);
  5813. UnicodeString Name;
  5814. if (FTransferListHoverIndex == 0)
  5815. {
  5816. Name = L"";
  5817. }
  5818. else
  5819. {
  5820. Name = GUIConfiguration->CopyParamList->Names[FTransferListHoverIndex - 1];
  5821. }
  5822. TTBXLabelItem * TransferLabel = dynamic_cast<TTBXLabelItem*>(
  5823. static_cast<TComponent*>(GetComponent(fcTransferLabel)));
  5824. TTBXStringList * TransferList = dynamic_cast<TTBXStringList*>(
  5825. static_cast<TObject*>(GetComponent(fcTransferList)));
  5826. UnicodeString InfoStr =
  5827. GUIConfiguration->CopyParamPreset[Name].
  5828. GetInfoStr(L"; ",
  5829. FLAGMASK(Terminal != NULL, Terminal->UsableCopyParamAttrs(0).General));
  5830. int MaxWidth = TransferList->MinWidth - (2 * TransferLabel->Margin) - ScaleByTextHeight(this, 10);
  5831. if (Canvas->TextExtent(InfoStr).cx > MaxWidth)
  5832. {
  5833. while (Canvas->TextExtent(InfoStr + Ellipsis).cx > MaxWidth)
  5834. {
  5835. InfoStr.SetLength(InfoStr.Length() - 1);
  5836. }
  5837. InfoStr += Ellipsis;
  5838. }
  5839. // UpdateCaption does not cause invalidation of whole submenu, while
  5840. // setting Caption property does.
  5841. // also it probably does not resize the label, even if necessary
  5842. // (we do not want that anyway)
  5843. TransferLabel->UpdateCaption(InfoStr);
  5844. }
  5845. __finally
  5846. {
  5847. Canvas->Handle = NULL;
  5848. ReleaseDC(0, DC);
  5849. delete Canvas;
  5850. }
  5851. }
  5852. }
  5853. //---------------------------------------------------------------------------
  5854. void __fastcall TCustomScpExplorerForm::TransferListDrawItem(
  5855. TTBXCustomList * /*Sender*/, TCanvas * /*ACanvas*/, const TRect & /*ARect*/,
  5856. int /*AIndex*/, int AHoverIndex, bool & /*DrawDefault*/)
  5857. {
  5858. if (FTransferListHoverIndex != AHoverIndex)
  5859. {
  5860. FTransferListHoverIndex = AHoverIndex;
  5861. UpdateTransferLabel();
  5862. }
  5863. }
  5864. //---------------------------------------------------------------------------
  5865. void __fastcall TCustomScpExplorerForm::WMAppCommand(TMessage & Message)
  5866. {
  5867. int Command = GET_APPCOMMAND_LPARAM(Message.LParam);
  5868. TShiftState Shift = KeyDataToShiftState(GET_KEYSTATE_LPARAM(Message.LParam));
  5869. if ((Shift * (TShiftState() << ssShift << ssAlt << ssCtrl)).Empty())
  5870. {
  5871. if (Command == APPCOMMAND_BROWSER_FAVORITES)
  5872. {
  5873. OpenDirectory(GetSide(osCurrent));
  5874. Message.Result = 1;
  5875. }
  5876. else
  5877. {
  5878. TForm::Dispatch(&Message);
  5879. }
  5880. }
  5881. else
  5882. {
  5883. TForm::Dispatch(&Message);
  5884. }
  5885. }
  5886. //---------------------------------------------------------------------------
  5887. void __fastcall TCustomScpExplorerForm::CMDialogChar(TMessage & AMessage)
  5888. {
  5889. TCMDialogChar & Message = reinterpret_cast<TCMDialogChar &>(AMessage);
  5890. if ((FIgnoreNextDialogChar != 0) &&
  5891. (toupper(Message.CharCode) == toupper(FIgnoreNextDialogChar)))
  5892. {
  5893. Message.Result = 1;
  5894. }
  5895. else
  5896. {
  5897. TForm::Dispatch(&Message);
  5898. }
  5899. FIgnoreNextDialogChar = 0;
  5900. }
  5901. //---------------------------------------------------------------------------
  5902. void __fastcall TCustomScpExplorerForm::WMSysCommand(TMessage & Message)
  5903. {
  5904. // The four low-order bits are used internally by Windows
  5905. unsigned int Cmd = (Message.WParam & 0xFFF0);
  5906. // SC_RESTORE, SC_MAXIMIZE, SC_MINIMIZE - buttons on windows title
  5907. // SC_DEFAULT - double click on windows title (does not work, at least on WinXP)
  5908. // 61730 - restore thru double click - undocumented
  5909. // 61490 - maximize thru double click - undocumented
  5910. if ((Cmd == SC_RESTORE) || (Cmd == SC_MAXIMIZE) ||
  5911. (Cmd == SC_MINIMIZE) || (Cmd == SC_DEFAULT))
  5912. {
  5913. SysResizing(Cmd);
  5914. }
  5915. TForm::Dispatch(&Message);
  5916. }
  5917. //---------------------------------------------------------------------------
  5918. void __fastcall TCustomScpExplorerForm::WMQueryEndSession(TMessage & Message)
  5919. {
  5920. // We were actually never able to make ENDSESSION_CRITICAL happen.
  5921. // Also there no point returning TRUE as we are not able to
  5922. // handle the abrupt termination caused by subsequent WM_ENDSESSION cleanly.
  5923. // Hence the process termination might be safer :)
  5924. if ((Message.LParam != ENDSESSION_CRITICAL) &&
  5925. (((FQueue != NULL) && !FQueue->IsEmpty) || (FProgressForm != NULL)))
  5926. {
  5927. Message.Result = FALSE;
  5928. }
  5929. else
  5930. {
  5931. Message.Result = TRUE;
  5932. }
  5933. // Do not call default handling as that triggers OnCloseQuery,
  5934. // where our implementation will popup confirmation dialogs, what we do not want,
  5935. // as per Vista guidelines:
  5936. // https://msdn.microsoft.com/en-us/library/ms700677.aspx
  5937. }
  5938. //---------------------------------------------------------------------------
  5939. void __fastcall TCustomScpExplorerForm::WMEndSession(TWMEndSession & Message)
  5940. {
  5941. if (Message.EndSession && IsApplicationMinimized())
  5942. {
  5943. // WORKAROUND
  5944. // TApplication.WndProc() calls Application.Terminate() before Halt(),
  5945. // when it receives WM_ENDSESSION,
  5946. // but that sometimes (particularly when application is minimized) cause crashes.
  5947. // Crash popups message that blocks log off.
  5948. // Obviously application cannot shutdown cleanly after WM_ENDSESSION,
  5949. // so we call ExitProcess() immediately, not even trying to cleanup.
  5950. // It still causes beep, so there's likely some popup, but it does not block
  5951. // log off.
  5952. ExitProcess(0);
  5953. }
  5954. TForm::Dispatch(&Message);
  5955. }
  5956. #ifdef _DEBUG
  5957. //---------------------------------------------------------------------------
  5958. void __fastcall TCustomScpExplorerForm::WMWindowPosChanged(TWMWindowPosMsg & Message)
  5959. {
  5960. TForm::Dispatch(&Message);
  5961. }
  5962. #endif
  5963. //---------------------------------------------------------------------------
  5964. void __fastcall TCustomScpExplorerForm::SysResizing(unsigned int /*Cmd*/)
  5965. {
  5966. }
  5967. //---------------------------------------------------------------------------
  5968. void __fastcall TCustomScpExplorerForm::DoShow()
  5969. {
  5970. // only now are the controls resized finally, so the size constraints
  5971. // will not conflict with possibly very small window size
  5972. RestoreFormParams();
  5973. FixControlsPlacement();
  5974. if (Position == poDefaultPosOnly)
  5975. {
  5976. CutFormToDesktop(this);
  5977. }
  5978. TForm::DoShow();
  5979. FSessionsDragDropFilesEx->DragDropControl = SessionsPageControl;
  5980. FQueueDragDropFilesEx->DragDropControl = QueueView3;
  5981. if (Terminal == NULL)
  5982. {
  5983. StartingDisconnected();
  5984. }
  5985. UpdatePixelsPerInchMainWindowCounter();
  5986. FShowing = true;
  5987. }
  5988. //---------------------------------------------------------------------------
  5989. void __fastcall TCustomScpExplorerForm::UpdatePixelsPerInchMainWindowCounter()
  5990. {
  5991. Configuration->Usage->Set(L"PixelsPerInchMainWindow", PixelsPerInch);
  5992. }
  5993. //---------------------------------------------------------------------------
  5994. void __fastcall TCustomScpExplorerForm::StartingDisconnected()
  5995. {
  5996. DoTerminalListChanged(true);
  5997. }
  5998. //---------------------------------------------------------------------------
  5999. void __fastcall TCustomScpExplorerForm::PopupTrayBalloon(TTerminal * Terminal,
  6000. const UnicodeString & Str, TQueryType Type, Exception * E, unsigned int Seconds,
  6001. TNotifyEvent OnBalloonClick, TObject * UserData)
  6002. {
  6003. bool Do;
  6004. UnicodeString Message;
  6005. if (E == NULL)
  6006. {
  6007. Message = Str;
  6008. Do = true;
  6009. }
  6010. else
  6011. {
  6012. Do = ExceptionMessage(E, Message);
  6013. }
  6014. if (Do && WinConfiguration->BalloonNotifications)
  6015. {
  6016. UnicodeString Title;
  6017. if ((Terminal == NULL) && (Type == qtInformation) && ExtractMainInstructions(Message, Title))
  6018. {
  6019. Message = TrimLeft(Message);
  6020. }
  6021. else
  6022. {
  6023. Message = UnformatMessage(Message);
  6024. const ResourceString * Captions[] = { &_SMsgDlgConfirm, &_SMsgDlgWarning,
  6025. &_SMsgDlgError, &_SMsgDlgInformation, NULL };
  6026. Title = LoadResourceString(Captions[Type]);
  6027. if (Terminal != NULL)
  6028. {
  6029. Title = FORMAT(L"%s - %s",
  6030. (TTerminalManager::Instance()->GetTerminalTitle(Terminal, true), Title));
  6031. }
  6032. }
  6033. if (Seconds == 0)
  6034. {
  6035. Seconds = WinConfiguration->NotificationsTimeout;
  6036. }
  6037. FTrayIcon->PopupBalloon(Title, Message, Type, Seconds * MSecsPerSec, OnBalloonClick, UserData);
  6038. }
  6039. }
  6040. //---------------------------------------------------------------------------
  6041. unsigned int __fastcall TCustomScpExplorerForm::MoreMessageDialog(const UnicodeString Message,
  6042. TStrings * MoreMessages, TQueryType Type, unsigned int Answers,
  6043. UnicodeString HelpKeyword, const TMessageParams * Params,
  6044. TTerminal * Terminal)
  6045. {
  6046. if (((WinConfiguration->ContinueOnError && (FErrorList != NULL)) ||
  6047. (FOnFeedSynchronizeError != NULL)) &&
  6048. (Params != NULL) && (Params->Params & mpAllowContinueOnError) )
  6049. {
  6050. if (FOnFeedSynchronizeError != NULL)
  6051. {
  6052. FOnFeedSynchronizeError(Message, MoreMessages, Type, HelpKeyword);
  6053. }
  6054. else
  6055. {
  6056. DebugAssert(FErrorList != NULL);
  6057. TStringList * MoreMessagesCopy = NULL;
  6058. if (MoreMessages)
  6059. {
  6060. MoreMessagesCopy = new TStringList();
  6061. MoreMessagesCopy->Assign(MoreMessages);
  6062. }
  6063. FErrorList->AddObject(Message, MoreMessagesCopy);
  6064. }
  6065. PopupTrayBalloon(Terminal, Message, Type);
  6066. return ContinueAnswer(Answers);
  6067. }
  6068. else
  6069. {
  6070. bool UseBalloon = FTrayIcon->Visible;
  6071. if (UseBalloon)
  6072. {
  6073. PopupTrayBalloon(Terminal, Message, Type);
  6074. }
  6075. unsigned int Result;
  6076. try
  6077. {
  6078. Result = ::MoreMessageDialog(Message, MoreMessages, Type, Answers, HelpKeyword, Params);
  6079. }
  6080. __finally
  6081. {
  6082. // cancel only balloon we popped up, otherwise we may cancel notification
  6083. // balloon that was there before the message dialog
  6084. // (such as "dd confirmation opt in balloon)
  6085. if (UseBalloon)
  6086. {
  6087. FTrayIcon->CancelBalloon();
  6088. }
  6089. }
  6090. return Result;
  6091. }
  6092. }
  6093. //---------------------------------------------------------------------------
  6094. void __fastcall TCustomScpExplorerForm::ShowExtendedException(
  6095. TTerminal * Terminal, Exception * E)
  6096. {
  6097. if (FTrayIcon->Visible)
  6098. {
  6099. PopupTrayBalloon(Terminal, L"", qtError, E);
  6100. }
  6101. // particularly prevent opening new session from jump list,
  6102. // while exception is shown
  6103. NonVisualDataModule->StartBusy();
  6104. try
  6105. {
  6106. ShowExtendedExceptionEx(Terminal, E);
  6107. }
  6108. __finally
  6109. {
  6110. NonVisualDataModule->EndBusy();
  6111. FTrayIcon->CancelBalloon();
  6112. }
  6113. }
  6114. //---------------------------------------------------------------------------
  6115. void __fastcall TCustomScpExplorerForm::TerminalReady()
  6116. {
  6117. // cannot rely on active page being page for active terminal,
  6118. // as it can happen that active page is the "new session" page
  6119. // (e.g. when reconnecting active terminal, while login dialog
  6120. // invoked from "new session" page is modal)
  6121. int ActiveTerminalIndex = TTerminalManager::Instance()->ActiveTerminalIndex;
  6122. UpdateSessionTab(SessionsPageControl->Pages[ActiveTerminalIndex]);
  6123. }
  6124. //---------------------------------------------------------------------------
  6125. void __fastcall TCustomScpExplorerForm::InactiveTerminalException(
  6126. TTerminal * Terminal, Exception * E)
  6127. {
  6128. Notify(Terminal, L"", qtError, false, NULL, NULL, E);
  6129. if (!Terminal->Active)
  6130. {
  6131. int Index = TTerminalManager::Instance()->IndexOf(Terminal);
  6132. if (DebugAlwaysTrue((Index >= 0) && (Index < SessionsPageControl->PageCount)))
  6133. {
  6134. UpdateSessionTab(SessionsPageControl->Pages[Index]);
  6135. }
  6136. }
  6137. }
  6138. //---------------------------------------------------------------------------
  6139. void __fastcall TCustomScpExplorerForm::Notify(TTerminal * Terminal,
  6140. UnicodeString Message, TQueryType Type,
  6141. bool Important, TNotifyEvent OnClick, TObject * UserData, Exception * E)
  6142. {
  6143. if ((E == NULL) ||
  6144. ExceptionMessage(E, Message))
  6145. {
  6146. unsigned int Seconds = WinConfiguration->NotificationsTimeout;
  6147. if (Important)
  6148. {
  6149. Seconds *= 5;
  6150. }
  6151. UnicodeString NoteMessage(UnformatMessage(Message));
  6152. if (Terminal != NULL)
  6153. {
  6154. NoteMessage = FORMAT(L"%s: %s",
  6155. (TTerminalManager::Instance()->GetTerminalTitle(Terminal, true), NoteMessage));
  6156. }
  6157. if (WinConfiguration->BalloonNotifications)
  6158. {
  6159. AddNote(NoteMessage);
  6160. PopupTrayBalloon(Terminal, Message, Type, NULL, Seconds, OnClick, UserData);
  6161. }
  6162. else
  6163. {
  6164. FlashOnBackground();
  6165. PostNote(NoteMessage, Seconds, OnClick, UserData);
  6166. }
  6167. }
  6168. }
  6169. //---------------------------------------------------------------------------
  6170. void __fastcall TCustomScpExplorerForm::QueueEmptyNoteClicked(TObject * Sender)
  6171. {
  6172. RestoreApp();
  6173. TTerminalNoteData * TerminalNoteData = dynamic_cast<TTerminalNoteData *>(Sender);
  6174. if (DebugAlwaysTrue(TerminalNoteData != NULL) &&
  6175. !NonVisualDataModule->Busy)
  6176. {
  6177. TTerminal * Terminal = TerminalNoteData->Terminal;
  6178. TTerminalManager::Instance()->ActiveTerminal = Terminal;
  6179. if (!ComponentVisible[fcQueueView])
  6180. {
  6181. ToggleQueueVisibility();
  6182. GoToQueue();
  6183. }
  6184. }
  6185. }
  6186. //---------------------------------------------------------------------------
  6187. void __fastcall TCustomScpExplorerForm::QueueEvent(TTerminal * ATerminal,
  6188. TTerminalQueue * /*Queue*/, TQueueEvent Event)
  6189. {
  6190. TManagedTerminal * ManagedTerminal = DebugNotNull(dynamic_cast<TManagedTerminal *>(ATerminal));
  6191. UnicodeString Message;
  6192. TNotifyEvent OnClick = NULL;
  6193. TObject * UserData = NULL;
  6194. bool QueueInvisible = !ComponentVisible[fcQueueView] || IsApplicationMinimized();
  6195. switch (Event)
  6196. {
  6197. case qeEmptyButMonitored:
  6198. if ((ATerminal != Terminal) || QueueInvisible)
  6199. {
  6200. Message = LoadStr(BALLOON_QUEUE_EMPTY);
  6201. OnClick = QueueEmptyNoteClicked;
  6202. TTerminalNoteData * TerminalNoteData = new TTerminalNoteData();
  6203. TerminalNoteData->Terminal = ATerminal;
  6204. UserData = TerminalNoteData;
  6205. }
  6206. break;
  6207. case qeEmpty:
  6208. OperationComplete(ManagedTerminal->QueueOperationStart);
  6209. break;
  6210. case qePendingUserAction:
  6211. if ((ATerminal != Terminal) ||
  6212. (QueueInvisible && !IsQueueAutoPopup()))
  6213. {
  6214. Message = LoadStr(BALLOON_QUEUE_USER_ACTION);
  6215. }
  6216. break;
  6217. default:
  6218. DebugFail();
  6219. }
  6220. if (!Message.IsEmpty())
  6221. {
  6222. Notify(ATerminal, Message, qtInformation, false, OnClick, UserData);
  6223. }
  6224. }
  6225. //---------------------------------------------------------------------------
  6226. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDCreateDragFileList(
  6227. TObject * /*Sender*/, TFileList * FileList, bool & Created)
  6228. {
  6229. if (FDDExtMapFile != NULL)
  6230. {
  6231. CloseHandle(FDDExtMapFile);
  6232. FDDExtMapFile = NULL;
  6233. }
  6234. if (WinConfiguration->DDFakeFile)
  6235. {
  6236. DDFakeFileInitDrag(FileList, Created);
  6237. }
  6238. }
  6239. //---------------------------------------------------------------------------
  6240. void __fastcall TCustomScpExplorerForm::DDFakeCreated(TObject * /*Sender*/, const UnicodeString FileName)
  6241. {
  6242. if (DebugAlwaysTrue(!FDragFakeDirectory.IsEmpty()) &&
  6243. SameText(ExtractFileName(FileName), ExtractFileName(FDragFakeDirectory)))
  6244. {
  6245. FFakeFileDropTarget = FileName;
  6246. }
  6247. }
  6248. //---------------------------------------------------------------------------
  6249. void __fastcall TCustomScpExplorerForm::DDFakeFileInitDrag(TFileList * FileList,
  6250. bool & Created)
  6251. {
  6252. FFakeFileDropTarget = UnicodeString();
  6253. FDragFakeDirectory =
  6254. ExcludeTrailingBackslash(WinConfiguration->TemporaryDir());
  6255. if (!ForceDirectories(ApiPath(FDragFakeDirectory)))
  6256. {
  6257. throw Exception(FMTLOAD(CREATE_TEMP_DIR_ERROR, (FDragFakeDirectory)));
  6258. }
  6259. FileSetAttr(ApiPath(FDragFakeDirectory), faHidden);
  6260. FileList->AddItem(NULL, FDragFakeDirectory);
  6261. Created = true;
  6262. if (!WinConfiguration->IsDDExtRunning())
  6263. {
  6264. FDragFakeMonitors = StartCreationDirectoryMonitorsOnEachDrive(FILE_NOTIFY_CHANGE_DIR_NAME, DDFakeCreated);
  6265. }
  6266. FDDExtMapFile = CreateFileMappingA((HANDLE)0xFFFFFFFF, NULL, PAGE_READWRITE,
  6267. 0, sizeof(TDragExtCommStruct), AnsiString(DRAG_EXT_MAPPING).c_str());
  6268. {
  6269. TMutexGuard Guard(FDDExtMutex, DRAGEXT_MUTEX_RELEASE_TIMEOUT);
  6270. TDragExtCommStruct* CommStruct;
  6271. CommStruct = static_cast<TDragExtCommStruct*>(MapViewOfFile(FDDExtMapFile,
  6272. FILE_MAP_ALL_ACCESS, 0, 0, 0));
  6273. DebugAssert(CommStruct != NULL);
  6274. CommStruct->Version = TDragExtCommStruct::CurrentVersion;
  6275. CommStruct->Dragging = true;
  6276. wcsncpy(CommStruct->DropDest, FDragFakeDirectory.c_str(),
  6277. LENOF(CommStruct->DropDest));
  6278. NULL_TERMINATE(CommStruct->DropDest);
  6279. UnmapViewOfFile(CommStruct);
  6280. }
  6281. FDDMoveSlipped = false;
  6282. }
  6283. //---------------------------------------------------------------------------
  6284. bool __fastcall TCustomScpExplorerForm::RemoteFileControlFileOperation(
  6285. TObject * Sender, TFileOperation Operation, bool NoConfirmation, void * Param)
  6286. {
  6287. bool Result;
  6288. if (Sender == RemoteDirView)
  6289. {
  6290. Result = ExecuteFileOperation(Operation, osRemote, true, NoConfirmation, Param);
  6291. }
  6292. else
  6293. {
  6294. DebugAssert(Sender == RemoteDriveView);
  6295. TStrings * FileList = RemoteDriveView->DragFileList();
  6296. try
  6297. {
  6298. Result = ExecuteFileOperation(Operation, osRemote, FileList, NoConfirmation, Param);
  6299. }
  6300. __finally
  6301. {
  6302. delete FileList;
  6303. }
  6304. }
  6305. if (FDDTargetControl == RemoteDriveView)
  6306. {
  6307. RemoteDriveView->UpdateDropTarget();
  6308. }
  6309. // foRemoteMove happens when file/dir is dragged within the remote tree view
  6310. // or from tree view to dir view.
  6311. // foMove happens when file/dir is dragged from remote tree view outside of
  6312. // application via dragex
  6313. if (((Operation == foRemoteMove) || (Operation == foMove)) &&
  6314. (DropSourceControl == RemoteDriveView))
  6315. {
  6316. RemoteDriveView->UpdateDropSource();
  6317. }
  6318. return Result;
  6319. }
  6320. //---------------------------------------------------------------------------
  6321. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDEnd(TObject * Sender)
  6322. {
  6323. // This also handles drops of remote files to queue.
  6324. // Drops of local files (uploads) are handled in QueueDDProcessDropped.
  6325. SAFE_DESTROY(FDDFileList);
  6326. if (!FFakeFileDropTarget.IsEmpty())
  6327. {
  6328. RemoveDir(ApiPath(FFakeFileDropTarget));
  6329. }
  6330. if ((FDDExtMapFile != NULL) || (FDDTargetControl == QueueView3))
  6331. {
  6332. try
  6333. {
  6334. TDragResult DDResult = (Sender == RemoteDirView) ?
  6335. RemoteDirView->LastDDResult : RemoteDriveView->LastDDResult;
  6336. // Focus is moved to the target application,
  6337. // but as we are going to present the UI, we need to steal the focus back.
  6338. // This most likely won't work though (windows does not allow application
  6339. // to steal focus most of the time)
  6340. Application->BringToFront();
  6341. // On older version of Windows we never got drMove here, see also comment below.
  6342. // On Windows 10, we get the "move".
  6343. if ((DDResult == drCopy) || (DDResult == drMove) || (DDResult == drInvalid))
  6344. {
  6345. UnicodeString TargetDirectory;
  6346. TFileOperation Operation;
  6347. // drInvalid may mean drMove, see comment below
  6348. switch (DDResult)
  6349. {
  6350. case drCopy:
  6351. Operation = foCopy;
  6352. break;
  6353. case drMove:
  6354. Operation = foMove;
  6355. break;
  6356. default:
  6357. DebugFail();
  6358. case drInvalid:
  6359. // prefer "copy" for safety
  6360. Operation = FLAGSET(FLastDropEffect, DROPEFFECT_MOVE) ? foMove : foCopy;
  6361. break;
  6362. }
  6363. if (FDDMoveSlipped)
  6364. {
  6365. Operation = foMove;
  6366. }
  6367. TTransferOperationParam Param;
  6368. UnicodeString CounterName;
  6369. bool ForceQueue;
  6370. if (!DDGetTarget(Param.TargetDirectory, ForceQueue, CounterName))
  6371. {
  6372. // we get drInvalid both if d&d was intercepted by ddext,
  6373. // and when users drops on no-drop location.
  6374. // we tell the difference by existence of response from ddext,
  6375. // so we ignore absence of response in this case
  6376. if (DDResult != drInvalid)
  6377. {
  6378. // here we know that the extension is installed,
  6379. // as it is checked as soon as drag&drop starts from
  6380. // RemoteFileControlDDCreateDragFileList
  6381. Configuration->Usage->Inc(L"DownloadsDragDropExternalExtTargetUnknown");
  6382. throw ExtException(NULL, LoadStr(DRAGEXT_TARGET_UNKNOWN2), HELP_DRAGEXT_TARGET_UNKNOWN);
  6383. }
  6384. }
  6385. else
  6386. {
  6387. // download using fake file
  6388. Param.Temp = false;
  6389. Param.DragDrop = true;
  6390. if (ForceQueue)
  6391. {
  6392. Param.Queue = asOn;
  6393. }
  6394. if (Sender == RemoteDirView)
  6395. {
  6396. Param.Options = FLAGMASK(SelectedAllFilesInDirView(RemoteDirView), coAllFiles);
  6397. }
  6398. if (RemoteFileControlFileOperation(Sender, Operation,
  6399. (WinConfiguration->DDTransferConfirmation == asOff), &Param))
  6400. {
  6401. Configuration->Usage->Inc(CounterName);
  6402. }
  6403. }
  6404. }
  6405. }
  6406. __finally
  6407. {
  6408. CloseHandle(FDDExtMapFile);
  6409. FDDExtMapFile = NULL;
  6410. delete FDragFakeMonitors;
  6411. FDragFakeMonitors = NULL;
  6412. RemoveDir(ApiPath(FDragFakeDirectory));
  6413. FDragFakeDirectory = L"";
  6414. FFakeFileDropTarget = UnicodeString();
  6415. }
  6416. }
  6417. if (FDragDropSshTerminate.get() != NULL)
  6418. {
  6419. FDragDropSshTerminate->Rethrow();
  6420. }
  6421. }
  6422. //---------------------------------------------------------------------------
  6423. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDGiveFeedback(
  6424. TObject * Sender, int dwEffect, HRESULT & /*Result*/)
  6425. {
  6426. HCURSOR SlippedCopyCursor;
  6427. FDDMoveSlipped =
  6428. (FDragMoveCursor != NULL) &&
  6429. (!WinConfiguration->DDAllowMoveInit) && (dwEffect == DROPEFFECT_Copy) &&
  6430. ((IsFileControl(FDDTargetControl, osRemote) && (GetKeyState(VK_CONTROL) >= 0) &&
  6431. FTerminal->IsCapable[fcRemoteMove]) ||
  6432. (IsFileControl(FDDTargetControl, osLocal) && (GetKeyState(VK_SHIFT) < 0)));
  6433. SlippedCopyCursor = FDDMoveSlipped ? FDragMoveCursor : Dragdrop::DefaultCursor;
  6434. DragDropFiles(Sender)->CHCopy = SlippedCopyCursor;
  6435. DragDropFiles(Sender)->CHScrollCopy = SlippedCopyCursor;
  6436. // Remember drop effect so we know (when user drops files), if we copy or move
  6437. FLastDropEffect = dwEffect;
  6438. }
  6439. //---------------------------------------------------------------------------
  6440. bool __fastcall TCustomScpExplorerForm::DDGetTarget(
  6441. UnicodeString & Directory, bool & ForceQueue, UnicodeString & CounterName)
  6442. {
  6443. bool Result;
  6444. if (FDDTargetControl == QueueView3)
  6445. {
  6446. Directory = DefaultDownloadTargetDirectory();
  6447. Result = true;
  6448. CounterName = L"DownloadsDragDropQueue";
  6449. ForceQueue = true;
  6450. }
  6451. else if (!FFakeFileDropTarget.IsEmpty())
  6452. {
  6453. Directory = ExcludeTrailingBackslash(ExtractFilePath(FFakeFileDropTarget));
  6454. Result = true;
  6455. ForceQueue = false;
  6456. CounterName = L"DownloadsDragDropFakeFile";
  6457. }
  6458. else
  6459. {
  6460. ForceQueue = false;
  6461. Enabled = false;
  6462. try
  6463. {
  6464. int Timer = 0;
  6465. Result = false;
  6466. while (!Result && (Timer < WinConfiguration->DDExtTimeout))
  6467. {
  6468. {
  6469. TMutexGuard Guard(FDDExtMutex, DRAGEXT_MUTEX_RELEASE_TIMEOUT);
  6470. TDragExtCommStruct* CommStruct;
  6471. CommStruct = static_cast<TDragExtCommStruct*>(MapViewOfFile(FDDExtMapFile,
  6472. FILE_MAP_ALL_ACCESS, 0, 0, 0));
  6473. DebugAssert(CommStruct != NULL);
  6474. Result = !CommStruct->Dragging;
  6475. if (Result)
  6476. {
  6477. Directory = ExtractFilePath(CommStruct->DropDest);
  6478. CounterName = L"DownloadsDragDropExternalExt";
  6479. }
  6480. UnmapViewOfFile(CommStruct);
  6481. }
  6482. if (!Result)
  6483. {
  6484. Sleep(50);
  6485. Timer += 50;
  6486. Application->ProcessMessages();
  6487. }
  6488. }
  6489. }
  6490. __finally
  6491. {
  6492. Enabled = true;
  6493. }
  6494. }
  6495. return Result;
  6496. }
  6497. //---------------------------------------------------------------------------
  6498. void __fastcall TCustomScpExplorerForm::AddDelayedDirectoryDeletion(
  6499. const UnicodeString TempDir, int SecDelay)
  6500. {
  6501. TDateTime Alarm = Now() + (double)((double)SecDelay*OneMillisecond);
  6502. FDelayedDeletionList->AddObject(TempDir, reinterpret_cast<TObject*>(Alarm.FileDate()));
  6503. if (FDelayedDeletionTimer == NULL)
  6504. {
  6505. DebugAssert(HandleAllocated());
  6506. FDelayedDeletionTimer = new TTimer(this);
  6507. FDelayedDeletionTimer->Interval = 10000;
  6508. FDelayedDeletionTimer->OnTimer = DoDelayedDeletion;
  6509. }
  6510. else
  6511. {
  6512. FDelayedDeletionTimer->Enabled = true;
  6513. }
  6514. }
  6515. //---------------------------------------------------------------------------
  6516. void __fastcall TCustomScpExplorerForm::DoDelayedDeletion(TObject * Sender)
  6517. {
  6518. DebugAssert(FDelayedDeletionList != NULL);
  6519. TDateTime N = Now();
  6520. TDateTime Alert;
  6521. UnicodeString Directory;
  6522. for (int Index = FDelayedDeletionList->Count-1; Index >= 0; Index--)
  6523. {
  6524. Alert = FileDateToDateTime(reinterpret_cast<int>(FDelayedDeletionList->Objects[Index]));
  6525. if ((N >= Alert) || (Sender == NULL))
  6526. {
  6527. Directory = FDelayedDeletionList->Strings[Index];
  6528. if (DeleteDirectory(ExcludeTrailingBackslash(Directory)))
  6529. {
  6530. FDelayedDeletionList->Delete(Index);
  6531. }
  6532. }
  6533. }
  6534. if (FDelayedDeletionList->Count == 0)
  6535. {
  6536. FDelayedDeletionTimer->Enabled = false;
  6537. }
  6538. }
  6539. //---------------------------------------------------------------------------
  6540. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDTargetDrop()
  6541. {
  6542. if (IsFileControl(FDDTargetControl, osRemote) ||
  6543. (FDDTargetControl == SessionsPageControl))
  6544. {
  6545. TTerminal * TargetTerminal = NULL;
  6546. TFileOperation Operation = foNone;
  6547. if (FDDTargetControl == SessionsPageControl)
  6548. {
  6549. TPoint Point = SessionsPageControl->ScreenToClient(Mouse->CursorPos);
  6550. int Index = SessionsPageControl->IndexOfTabAt(Point.X, Point.Y);
  6551. // do not allow dropping on the "+" tab
  6552. TargetTerminal = GetSessionTabTerminal(SessionsPageControl->Pages[Index]);
  6553. if (TargetTerminal != NULL)
  6554. {
  6555. if ((FLastDropEffect == DROPEFFECT_MOVE) &&
  6556. (TargetTerminal == TTerminalManager::Instance()->ActiveTerminal))
  6557. {
  6558. Operation = foRemoteMove;
  6559. }
  6560. else
  6561. {
  6562. Operation = foRemoteCopy;
  6563. }
  6564. }
  6565. }
  6566. else
  6567. {
  6568. // when move from remote side is disabled, we allow copying inside the remote
  6569. // panel, but we interpret is as moving (we also slip in the move cursor)
  6570. if ((FLastDropEffect == DROPEFFECT_MOVE) ||
  6571. (!WinConfiguration->DDAllowMoveInit && (FLastDropEffect == DROPEFFECT_COPY) &&
  6572. FDDMoveSlipped))
  6573. {
  6574. Operation = foRemoteMove;
  6575. }
  6576. else if (FLastDropEffect == DROPEFFECT_COPY)
  6577. {
  6578. Operation = foRemoteCopy;
  6579. }
  6580. }
  6581. if (Operation != foNone)
  6582. {
  6583. RemoteFileControlFileOperation(DropSourceControl,
  6584. Operation, (WinConfiguration->DDTransferConfirmation == asOff), TargetTerminal);
  6585. }
  6586. // abort drag&drop
  6587. Abort();
  6588. }
  6589. else if ((FDDExtMapFile == NULL) && (FLastDropEffect != DROPEFFECT_NONE) &&
  6590. // Drops of remote files to queue are handled in RemoteFileControlDDEnd
  6591. (FDDTargetControl != QueueView3))
  6592. {
  6593. DebugAssert(!FDragTempDir.IsEmpty());
  6594. TTransferType Type;
  6595. UnicodeString TempDir = FDragTempDir;
  6596. // We clear FDragTempDir before calling
  6597. // just in case it fail (raises exception)
  6598. FDragTempDir = L"";
  6599. Type = (FLastDropEffect & DROPEFFECT_MOVE ? ttMove : Type = ttCopy);
  6600. TGUICopyParamType CopyParams = GUIConfiguration->CurrentCopyParam;
  6601. // empty directory parameter means temp directory -> don't display it!
  6602. UnicodeString TargetDir = L"";
  6603. int Options = 0;
  6604. if (!CopyParamDialog(tdToLocal, Type, true, FDDFileList,
  6605. TargetDir, CopyParams, (WinConfiguration->DDTransferConfirmation != asOff), true, Options))
  6606. {
  6607. Abort();
  6608. }
  6609. // TargetDir is set when dropped on local file control
  6610. // (this was workaround for legacy dirview event handling, now it should be
  6611. // made prettier)
  6612. if (TargetDir.IsEmpty())
  6613. {
  6614. TargetDir = TempDir;
  6615. if (ForceDirectories(ApiPath(TargetDir)))
  6616. {
  6617. DebugAssert(Terminal && !TargetDir.IsEmpty());
  6618. FPendingTempSpaceWarn = true;
  6619. try
  6620. {
  6621. FDragDropOperation = true;
  6622. int Params = cpTemporary |
  6623. (Type == ttMove ? cpDelete : 0);
  6624. DDDownload(FDDFileList, TargetDir, &CopyParams, Params);
  6625. Configuration->Usage->Inc(L"DownloadsDragDropExternalTemp");
  6626. }
  6627. __finally
  6628. {
  6629. FDragDropOperation = false;
  6630. FPendingTempSpaceWarn = false;
  6631. AddDelayedDirectoryDeletion(TargetDir, WinConfiguration->DDDeleteDelay);
  6632. }
  6633. }
  6634. else
  6635. {
  6636. throw Exception(FMTLOAD(CREATE_TEMP_DIR_ERROR, (TargetDir)));
  6637. }
  6638. }
  6639. }
  6640. }
  6641. //---------------------------------------------------------------------------
  6642. void __fastcall TCustomScpExplorerForm::DDDownload(TStrings * FilesToCopy,
  6643. const UnicodeString TargetDir, const TCopyParamType * CopyParam, int Params)
  6644. {
  6645. void * BatchStorage;
  6646. BatchStart(BatchStorage);
  6647. try
  6648. {
  6649. UpdateCopyParamCounters(*CopyParam);
  6650. Terminal->CopyToLocal(FilesToCopy, TargetDir, CopyParam, Params, NULL);
  6651. if (FLAGSET(Params, cpDelete) && (DropSourceControl == RemoteDriveView))
  6652. {
  6653. RemoteDriveView->UpdateDropSource();
  6654. }
  6655. }
  6656. __finally
  6657. {
  6658. BatchEnd(BatchStorage);
  6659. }
  6660. }
  6661. //---------------------------------------------------------------------------
  6662. class TFakeDataObjectFilesEx : public TDataObjectFilesEx
  6663. {
  6664. public:
  6665. __fastcall TFakeDataObjectFilesEx(TFileList * AFileList, bool RenderPIDL,
  6666. bool RenderFilename) : TDataObjectFilesEx(AFileList, RenderPIDL, RenderFilename)
  6667. {
  6668. }
  6669. virtual bool __fastcall AllowData(const tagFORMATETC & FormatEtc)
  6670. {
  6671. return (FormatEtc.cfFormat == CF_HDROP) ? false :
  6672. TDataObjectFilesEx::AllowData(FormatEtc);
  6673. }
  6674. };
  6675. //---------------------------------------------------------------------------
  6676. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDCreateDataObject(
  6677. TObject * Sender, TDataObject *& DataObject)
  6678. {
  6679. if (FDDExtMapFile != NULL)
  6680. {
  6681. TFileList * FileList = DragDropFiles(Sender)->FileList;
  6682. if (!FileList->RenderPIDLs() || !FileList->RenderNames())
  6683. {
  6684. Abort();
  6685. }
  6686. if (FileList->Count > 0)
  6687. {
  6688. TDataObjectFilesEx * FilesObject = new TFakeDataObjectFilesEx(FileList, true, true);
  6689. if (!FilesObject->IsValid(true, true))
  6690. {
  6691. FilesObject->_Release();
  6692. }
  6693. else
  6694. {
  6695. DataObject = FilesObject;
  6696. }
  6697. }
  6698. }
  6699. }
  6700. //---------------------------------------------------------------------------
  6701. void __fastcall TCustomScpExplorerForm::GoToCommandLine()
  6702. {
  6703. DebugFail();
  6704. }
  6705. //---------------------------------------------------------------------------
  6706. void __fastcall TCustomScpExplorerForm::GoToTree()
  6707. {
  6708. ComponentVisible[fcRemoteTree] = true;
  6709. RemoteDriveView->SetFocus();
  6710. }
  6711. //---------------------------------------------------------------------------
  6712. TStrings * __fastcall TCustomScpExplorerForm::PanelExport(TOperationSide Side,
  6713. TPanelExport Export)
  6714. {
  6715. TCustomDirView * DirView = this->DirView(Side);
  6716. std::unique_ptr<TStrings> ExportData(new TStringList());
  6717. switch (Export)
  6718. {
  6719. case pePath:
  6720. ExportData->Add(DirView->PathName);
  6721. break;
  6722. case peFileList:
  6723. case peFullFileList:
  6724. {
  6725. bool FullPath = (Export == peFullFileList);
  6726. DirView->CreateFileList(false, FullPath, ExportData.get());
  6727. for (int Index = 0; Index < ExportData->Count; Index++)
  6728. {
  6729. if (ExportData->Strings[Index].Pos(L" ") > 0)
  6730. {
  6731. ExportData->Strings[Index] = FORMAT(L"\"%s\"", (ExportData->Strings[Index]));
  6732. }
  6733. }
  6734. }
  6735. break;
  6736. default:
  6737. DebugFail();
  6738. }
  6739. return ExportData.release();
  6740. }
  6741. //---------------------------------------------------------------------------
  6742. void __fastcall TCustomScpExplorerForm::PanelExport(TOperationSide Side,
  6743. TPanelExport Export, TPanelExportDestination Destination)
  6744. {
  6745. std::unique_ptr<TStrings> ExportData(PanelExport(Side, Export));
  6746. PanelExportStore(Side, Export, Destination, ExportData.get());
  6747. }
  6748. //---------------------------------------------------------------------------
  6749. void __fastcall TCustomScpExplorerForm::PanelExportStore(TOperationSide /*Side*/,
  6750. TPanelExport /*Export*/, TPanelExportDestination Destination,
  6751. TStrings * ExportData)
  6752. {
  6753. if (Destination == pedClipboard)
  6754. {
  6755. TInstantOperationVisualizer Visualizer;
  6756. CopyToClipboard(ExportData);
  6757. }
  6758. else
  6759. {
  6760. DebugFail();
  6761. }
  6762. }
  6763. //---------------------------------------------------------------------------
  6764. void __fastcall TCustomScpExplorerForm::Filter(TOperationSide Side)
  6765. {
  6766. TCustomDirView * DirView = this->DirView(Side);
  6767. TFileFilter Filter;
  6768. DefaultFileFilter(Filter);
  6769. Filter.Masks = DirView->Mask;
  6770. if (DoFilterMaskDialog(DirView, &Filter))
  6771. {
  6772. DirView->Mask = TFileMasks::NormalizeMask(Filter.Masks);
  6773. Configuration->Usage->Inc(L"Filters");
  6774. }
  6775. }
  6776. //---------------------------------------------------------------------------
  6777. TQueueOperation __fastcall TCustomScpExplorerForm::DefaultQueueOperation()
  6778. {
  6779. return FQueueController->DefaultOperation();
  6780. }
  6781. //---------------------------------------------------------------------------
  6782. bool __fastcall TCustomScpExplorerForm::AllowQueueOperation(
  6783. TQueueOperation Operation, void ** Param)
  6784. {
  6785. switch (Operation)
  6786. {
  6787. case qoPreferences:
  6788. return true;
  6789. case qoGoTo:
  6790. return ComponentVisible[fcQueueView] && QueueView3->Enabled;
  6791. case qoOnceEmpty:
  6792. return !FQueueController->Empty;
  6793. default:
  6794. return FQueueController->AllowOperation(Operation, Param);
  6795. }
  6796. }
  6797. //---------------------------------------------------------------------------
  6798. void __fastcall TCustomScpExplorerForm::GoToQueue()
  6799. {
  6800. if (DebugAlwaysTrue(QueueView3->Visible))
  6801. {
  6802. QueueView3->SetFocus();
  6803. }
  6804. }
  6805. //---------------------------------------------------------------------------
  6806. void __fastcall TCustomScpExplorerForm::ExecuteQueueOperation(
  6807. TQueueOperation Operation, void * Param)
  6808. {
  6809. if (Operation == qoGoTo)
  6810. {
  6811. GoToQueue();
  6812. }
  6813. else if (Operation == qoPreferences)
  6814. {
  6815. PreferencesDialog(pmQueue);
  6816. }
  6817. else
  6818. {
  6819. FQueueController->ExecuteOperation(Operation, Param);
  6820. }
  6821. }
  6822. //---------------------------------------------------------------------------
  6823. bool __fastcall TCustomScpExplorerForm::GetQueueEnabled()
  6824. {
  6825. return (Queue != NULL) && Queue->Enabled;
  6826. }
  6827. //---------------------------------------------------------------------------
  6828. void __fastcall TCustomScpExplorerForm::ToggleQueueEnabled()
  6829. {
  6830. DebugAssert(Queue != NULL);
  6831. if (Queue != NULL)
  6832. {
  6833. Queue->Enabled = !Queue->Enabled;
  6834. }
  6835. }
  6836. //---------------------------------------------------------------------------
  6837. void __fastcall TCustomScpExplorerForm::QueueView3ContextPopup(
  6838. TObject * /*Sender*/, TPoint & /*MousePos*/, bool & /*Handled*/)
  6839. {
  6840. FQueueActedItem = QueueView3->ItemFocused;
  6841. }
  6842. //---------------------------------------------------------------------------
  6843. /*virtual*/ int __fastcall TCustomScpExplorerForm::GetStaticComponentsHeight()
  6844. {
  6845. return TopDock->Height + QueueSplitter->Height;
  6846. }
  6847. //---------------------------------------------------------------------------
  6848. void __fastcall TCustomScpExplorerForm::QueueSplitterCanResize(
  6849. TObject * /*Sender*/, int & NewSize, bool & Accept)
  6850. {
  6851. // when queue is hidden by double-clicking splitter, stray attempt to
  6852. // resize the panel with strange value arrives, make sure it is ignored
  6853. if (ComponentVisible[fcQueueView])
  6854. {
  6855. int HeightLimit = ClientHeight - GetStaticComponentsHeight() -
  6856. RemotePanel->Constraints->MinHeight;
  6857. if (NewSize > HeightLimit)
  6858. {
  6859. NewSize = HeightLimit;
  6860. }
  6861. }
  6862. else
  6863. {
  6864. Accept = false;
  6865. }
  6866. }
  6867. //---------------------------------------------------------------------------
  6868. void __fastcall TCustomScpExplorerForm::QueueView3StartDrag(TObject * /*Sender*/,
  6869. TDragObject *& /*DragObject*/)
  6870. {
  6871. FQueueActedItem = QueueView3->ItemFocused;
  6872. }
  6873. //---------------------------------------------------------------------------
  6874. void __fastcall TCustomScpExplorerForm::QueueView3DragOver(TObject * /*Sender*/,
  6875. TObject * Source, int X, int Y, TDragState /*State*/, bool & Accept)
  6876. {
  6877. Accept = true;
  6878. if (Source == QueueView3)
  6879. {
  6880. TListItem * DropTarget = QueueView3->GetItemAt(X, Y);
  6881. Accept = (DropTarget != NULL) && (FQueueActedItem != NULL);
  6882. if (Accept)
  6883. {
  6884. TQueueItemProxy * QueueItem;
  6885. TQueueItemProxy * DestQueueItem;
  6886. QueueItem = static_cast<TQueueItemProxy *>(FQueueActedItem->Data);
  6887. DestQueueItem = static_cast<TQueueItemProxy *>(DropTarget->Data);
  6888. Accept = (QueueItem != DestQueueItem) &&
  6889. (QueueItem->Status == TQueueItem::qsPending) &&
  6890. (DestQueueItem->Status == TQueueItem::qsPending);
  6891. }
  6892. }
  6893. }
  6894. //---------------------------------------------------------------------------
  6895. void __fastcall TCustomScpExplorerForm::QueueView3DragDrop(TObject * /*Sender*/,
  6896. TObject * /*Source*/, int /*X*/, int /*Y*/)
  6897. {
  6898. if ((FQueueActedItem != NULL) && (QueueView3->DropTarget != NULL))
  6899. {
  6900. TQueueItemProxy * QueueItem;
  6901. TQueueItemProxy * DestQueueItem;
  6902. QueueItem = static_cast<TQueueItemProxy *>(FQueueActedItem->Data);
  6903. DestQueueItem = static_cast<TQueueItemProxy *>(QueueView3->DropTarget->Data);
  6904. QueueItem->Move(DestQueueItem);
  6905. }
  6906. }
  6907. //---------------------------------------------------------------------------
  6908. void __fastcall TCustomScpExplorerForm::QueueView3Enter(TObject * /*Sender*/)
  6909. {
  6910. if ((QueueView3->ItemFocused == NULL) &&
  6911. (QueueView3->Items->Count > 0))
  6912. {
  6913. QueueView3->ItemFocused = QueueView3->Items->Item[0];
  6914. }
  6915. QueueLabelUpdateStatus();
  6916. }
  6917. //---------------------------------------------------------------------------
  6918. void __fastcall TCustomScpExplorerForm::QueueView3Exit(TObject * /*Sender*/)
  6919. {
  6920. QueueLabelUpdateStatus();
  6921. }
  6922. //---------------------------------------------------------------------------
  6923. void __fastcall TCustomScpExplorerForm::QueueLabelUpdateStatus()
  6924. {
  6925. QueueLabel->UpdateStatus();
  6926. }
  6927. //---------------------------------------------------------------------------
  6928. void __fastcall TCustomScpExplorerForm::QueueView3SelectItem(
  6929. TObject * /*Sender*/, TListItem * /*Item*/, bool Selected)
  6930. {
  6931. if (Selected)
  6932. {
  6933. NonVisualDataModule->UpdateNonVisibleActions();
  6934. }
  6935. }
  6936. //---------------------------------------------------------------------------
  6937. TDragDropFilesEx * __fastcall TCustomScpExplorerForm::DragDropFiles(TObject * Sender)
  6938. {
  6939. TDragDropFilesEx * Result = NULL;
  6940. if (Sender == SessionsPageControl)
  6941. {
  6942. Result = FSessionsDragDropFilesEx;
  6943. }
  6944. else if (Sender == QueueView3)
  6945. {
  6946. Result = FQueueDragDropFilesEx;
  6947. }
  6948. else
  6949. {
  6950. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  6951. if (DirView != NULL)
  6952. {
  6953. Result = DirView->DragDropFilesEx;
  6954. }
  6955. else
  6956. {
  6957. TCustomDriveView * DriveView = dynamic_cast<TCustomDriveView *>(Sender);
  6958. if (DriveView != NULL)
  6959. {
  6960. Result = DriveView->DragDropFilesEx;
  6961. }
  6962. }
  6963. }
  6964. DebugAssert(Result != NULL);
  6965. return Result;
  6966. }
  6967. //---------------------------------------------------------------------------
  6968. bool __fastcall TCustomScpExplorerForm::SelectedAllFilesInDirView(TCustomDirView * DView)
  6969. {
  6970. return (DView->SelCount == DView->FilesCount);
  6971. }
  6972. //---------------------------------------------------------------------------
  6973. bool __fastcall TCustomScpExplorerForm::DraggingAllFilesFromDirView(TOperationSide Side, TStrings * FileList)
  6974. {
  6975. return HasDirView[Side] && (DropSourceControl == DirView(Side)) && (FileList->Count == DirView(Side)->FilesCount);
  6976. }
  6977. //---------------------------------------------------------------------------
  6978. void __fastcall TCustomScpExplorerForm::RemoteFileControlDragDropFileOperation(
  6979. TObject * Sender, int Effect, UnicodeString TargetPath, bool ForceQueue)
  6980. {
  6981. TFileOperation Operation;
  6982. switch (Effect)
  6983. {
  6984. case DROPEFFECT_MOVE:
  6985. Operation = foMove;
  6986. break;
  6987. case DROPEFFECT_COPY:
  6988. // occures on WinXP (reported by user)
  6989. default:
  6990. Operation = foCopy;
  6991. break;
  6992. };
  6993. TDragDropFilesEx * DragDropFilesEx = DragDropFiles(Sender);
  6994. // see a comment in TUnixDirView::PerformItemDragDropOperation
  6995. if (DragDropFilesEx->FileList->Count > 0)
  6996. {
  6997. TStrings * FileList = new TStringList();
  6998. try
  6999. {
  7000. for (int Index = 0; Index < DragDropFilesEx->FileList->Count; Index++)
  7001. {
  7002. FileList->Add(DragDropFilesEx->FileList->Items[Index]->Name);
  7003. }
  7004. FDragDropOperation = true;
  7005. TTransferOperationParam Param;
  7006. Param.TargetDirectory = TargetPath;
  7007. // upload, no temp dirs
  7008. Param.Temp = false;
  7009. Param.DragDrop = true;
  7010. Param.Options =
  7011. FLAGMASK(DraggingAllFilesFromDirView(osLocal, FileList), coAllFiles);
  7012. if (ForceQueue)
  7013. {
  7014. Param.Queue = asOn;
  7015. }
  7016. if (ExecuteFileOperation(Operation, osLocal, FileList,
  7017. (WinConfiguration->DDTransferConfirmation == asOff), &Param))
  7018. {
  7019. if (IsFileControl(DropSourceControl, osLocal))
  7020. {
  7021. Configuration->Usage->Inc(L"UploadsDragDropInternal");
  7022. }
  7023. else
  7024. {
  7025. Configuration->Usage->Inc(L"UploadsDragDropExternal");
  7026. }
  7027. }
  7028. }
  7029. __finally
  7030. {
  7031. FDragDropOperation = false;
  7032. delete FileList;
  7033. }
  7034. }
  7035. }
  7036. //---------------------------------------------------------------------------
  7037. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDFileOperation(
  7038. TObject * Sender, int Effect, UnicodeString /*SourcePath*/,
  7039. UnicodeString TargetPath, bool & /*DoOperation*/)
  7040. {
  7041. RemoteFileControlDragDropFileOperation(Sender, Effect, TargetPath, false);
  7042. }
  7043. //---------------------------------------------------------------------------
  7044. void __fastcall TCustomScpExplorerForm::RemoteFileContolDDChooseEffect(
  7045. TObject * Sender, int grfKeyState, int & dwEffect)
  7046. {
  7047. // if any drop effect is allowed at all (e.g. no drop to self and drop to parent)
  7048. if ((dwEffect != DROPEFFECT_None) &&
  7049. IsFileControl(DropSourceControl, osRemote))
  7050. {
  7051. // do not allow drop on remote panel (on free space, still allow drop on directories)
  7052. if ((Sender == RemoteDirView) && (DropSourceControl == RemoteDirView) &&
  7053. (RemoteDirView->DropTarget == NULL))
  7054. {
  7055. dwEffect = DROPEFFECT_None;
  7056. }
  7057. else
  7058. {
  7059. if (dwEffect == DROPEFFECT_Copy)
  7060. {
  7061. bool MoveCapable = FTerminal->IsCapable[fcRemoteMove];
  7062. // currently we support copying always (at least via temporary directory);
  7063. // remove associated checks once this all proves stable and working
  7064. bool CopyCapable = true;
  7065. // if we do not support neither of operations, there's no discussion
  7066. if (!MoveCapable && !CopyCapable)
  7067. {
  7068. dwEffect = DROPEFFECT_None;
  7069. }
  7070. // when moving is disabled, we need to keep effect to "copy",
  7071. // which will be later interpretted as move (with slipped-in cursor)
  7072. else if (!WinConfiguration->DDAllowMoveInit && FLAGCLEAR(grfKeyState, MK_CONTROL))
  7073. {
  7074. // no-op, keep copy
  7075. }
  7076. else
  7077. {
  7078. // The default effect inside remote panel is move,
  7079. // unless we do not support it, but support copy
  7080. if (FLAGCLEAR(grfKeyState, MK_CONTROL))
  7081. {
  7082. dwEffect = MoveCapable ? DROPEFFECT_Move : DROPEFFECT_Copy;
  7083. }
  7084. else
  7085. {
  7086. // with ctrl-down, we want copy unless it is not supported
  7087. dwEffect = CopyCapable ? DROPEFFECT_Copy : DROPEFFECT_None;
  7088. }
  7089. }
  7090. }
  7091. }
  7092. }
  7093. }
  7094. //---------------------------------------------------------------------------
  7095. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDDragFileName(
  7096. TObject * Sender, TRemoteFile * File, UnicodeString & FileName)
  7097. {
  7098. if (FDDTotalSize >= 0)
  7099. {
  7100. if (File->IsDirectory)
  7101. {
  7102. FDDTotalSize = -1;
  7103. }
  7104. else
  7105. {
  7106. FDDTotalSize += File->Size;
  7107. }
  7108. }
  7109. DebugAssert(!FDragTempDir.IsEmpty());
  7110. // TODO: this is quite ineffective
  7111. // TODO: what if invalid character replacement is disabled?
  7112. FileName = FDragTempDir + GUIConfiguration->CurrentCopyParam.ValidLocalFileName(File->FileName);
  7113. UnicodeString TransferFileName;
  7114. if (Sender == RemoteDriveView)
  7115. {
  7116. TransferFileName = UnixExcludeTrailingBackslash(File->FullFileName);
  7117. }
  7118. else
  7119. {
  7120. TransferFileName = File->FileName;
  7121. }
  7122. FDDFileList->AddObject(TransferFileName, File);
  7123. }
  7124. //---------------------------------------------------------------------------
  7125. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDDragDetect(
  7126. TObject * /*Sender*/, int /*grfKeyState*/, const TPoint & /*DetectStart*/,
  7127. const TPoint & /*Point*/, TDragDetectStatus /*DragStatus*/)
  7128. {
  7129. // sometimes we do not get DDEnd so the list is not released
  7130. SAFE_DESTROY(FDDFileList);
  7131. FDDFileList = new TStringList();
  7132. FDragTempDir = WinConfiguration->TemporaryDir();
  7133. FDDTotalSize = 0;
  7134. FDragDropSshTerminate.reset(NULL);
  7135. }
  7136. //---------------------------------------------------------------------------
  7137. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDQueryContinueDrag(
  7138. TObject * /*Sender*/, BOOL /*FEscapePressed*/, int /*grfKeyState*/,
  7139. HRESULT & Result)
  7140. {
  7141. if (Result == DRAGDROP_S_DROP)
  7142. {
  7143. try
  7144. {
  7145. GlobalDragImageList->HideDragImage();
  7146. try
  7147. {
  7148. RemoteFileControlDDTargetDrop();
  7149. }
  7150. catch(ESshTerminate & E)
  7151. {
  7152. FDragDropSshTerminate.reset(E.Clone());
  7153. }
  7154. }
  7155. catch (Exception &E)
  7156. {
  7157. // If downloading fails we need to cancel drag&drop, otherwise
  7158. // Explorer shows error
  7159. // But by the way exception probably never reach this point as
  7160. // it's catched on way.
  7161. // Fatal exceptions get here (like when opening a secondary shell extension for file duplication fails).
  7162. Result = DRAGDROP_S_CANCEL;
  7163. ShowExtendedException(Terminal, &E);
  7164. }
  7165. }
  7166. }
  7167. //---------------------------------------------------------------------------
  7168. void __fastcall TCustomScpExplorerForm::DirViewMatchMask(
  7169. TObject * /*Sender*/, UnicodeString FileName, bool Directory, __int64 Size,
  7170. TDateTime Modification, UnicodeString Masks, bool & Matches, bool AllowImplicitMatches)
  7171. {
  7172. TFileMasks::TParams MaskParams;
  7173. MaskParams.Size = Size;
  7174. MaskParams.Modification = Modification;
  7175. // this does not re-parse the mask if it is the same as the last time
  7176. FDirViewMatchMask = Masks;
  7177. bool ImplicitMatch;
  7178. Matches =
  7179. // RecurseInclude parameter has no effect as the Path is empty
  7180. FDirViewMatchMask.Matches(FileName, Directory, UnicodeString(L""), &MaskParams, true, ImplicitMatch) &&
  7181. (AllowImplicitMatches || !ImplicitMatch);
  7182. }
  7183. //---------------------------------------------------------------------------
  7184. void __fastcall TCustomScpExplorerForm::DirViewGetOverlay(
  7185. TObject * Sender, TListItem * Item, WORD & Indexes)
  7186. {
  7187. TCustomDirView * DirView = reinterpret_cast<TCustomDirView *>(Sender);
  7188. UnicodeString Ext;
  7189. if (DirView == RemoteDirView)
  7190. {
  7191. Ext = UnixExtractFileExt(DirView->ItemFileName(Item));
  7192. }
  7193. else
  7194. {
  7195. Ext = ExtractFileExt(DirView->ItemFileName(Item));
  7196. }
  7197. if (SameText(Ext, Configuration->PartialExt))
  7198. {
  7199. Indexes |= oiPartial;
  7200. }
  7201. }
  7202. //---------------------------------------------------------------------------
  7203. bool __fastcall TCustomScpExplorerForm::CanPasteToDirViewFromClipBoard()
  7204. {
  7205. return
  7206. DirViewEnabled(osCurrent) &&
  7207. DirView(osCurrent)->CanPasteFromClipBoard();
  7208. }
  7209. //---------------------------------------------------------------------------
  7210. bool __fastcall TCustomScpExplorerForm::CanPasteFromClipBoard()
  7211. {
  7212. bool Result = false;
  7213. if (CanPasteToDirViewFromClipBoard())
  7214. {
  7215. Result = true;
  7216. }
  7217. else
  7218. {
  7219. UnicodeString ClipboardText;
  7220. if (NonEmptyTextFromClipboard(ClipboardText))
  7221. {
  7222. if (StoredSessions->IsUrl(ClipboardText))
  7223. {
  7224. Result = true;
  7225. }
  7226. else
  7227. {
  7228. Result = DirViewEnabled(osCurrent);
  7229. }
  7230. }
  7231. }
  7232. return Result;
  7233. }
  7234. //---------------------------------------------------------------------------
  7235. void __fastcall TCustomScpExplorerForm::PasteFromClipBoard()
  7236. {
  7237. if (CanPasteToDirViewFromClipBoard())
  7238. {
  7239. DirView(osCurrent)->PasteFromClipBoard();
  7240. }
  7241. else
  7242. {
  7243. UnicodeString ClipboardText;
  7244. if (NonEmptyTextFromClipboard(ClipboardText))
  7245. {
  7246. if (StoredSessions->IsUrl(ClipboardText))
  7247. {
  7248. NewSession(false, ClipboardText);
  7249. }
  7250. else
  7251. {
  7252. DirView(osCurrent)->Path = ClipboardText;
  7253. }
  7254. }
  7255. }
  7256. }
  7257. //---------------------------------------------------------------------------
  7258. void __fastcall TCustomScpExplorerForm::FileListFromClipboard()
  7259. {
  7260. // TBD
  7261. }
  7262. //---------------------------------------------------------------------------
  7263. void __fastcall TCustomScpExplorerForm::SelectAll(TOperationSide Side, TSelectMode Mode)
  7264. {
  7265. TCustomDirView * ADirView = DirView(Side);
  7266. ADirView->SelectAll(Mode);
  7267. ADirView->SetFocus();
  7268. }
  7269. //---------------------------------------------------------------------------
  7270. void __fastcall TCustomScpExplorerForm::SelectByMask(TOperationSide Side, bool Select)
  7271. {
  7272. TCustomDirView * ADirView = DirView(Side);
  7273. TFileFilter Filter;
  7274. DefaultFileFilter(Filter);
  7275. if (DoSelectMaskDialog(ADirView, Select, &Filter, Configuration))
  7276. {
  7277. Configuration->Usage->Inc(L"MaskSelections");
  7278. ADirView->SelectFiles(Filter, Select);
  7279. ADirView->SetFocus();
  7280. }
  7281. }
  7282. //---------------------------------------------------------------------------
  7283. void __fastcall TCustomScpExplorerForm::RestoreSelectedNames(TOperationSide Side)
  7284. {
  7285. TCustomDirView * ADirView = DirView(Side);
  7286. ADirView->RestoreSelectedNames();
  7287. ADirView->SetFocus();
  7288. }
  7289. //---------------------------------------------------------------------------
  7290. void __fastcall TCustomScpExplorerForm::SelectSameExt(bool Select)
  7291. {
  7292. TCustomDirView * CurrentDirView = DirView(osCurrent);
  7293. if (DebugAlwaysTrue(CurrentDirView->ItemFocused != NULL))
  7294. {
  7295. UnicodeString FileName = CurrentDirView->ItemFileName(CurrentDirView->ItemFocused);
  7296. UnicodeString Ext;
  7297. if (GetSide(osCurrent) == osRemote)
  7298. {
  7299. Ext = UnixExtractFileExt(FileName);
  7300. }
  7301. else
  7302. {
  7303. Ext = ExtractFileExt(FileName);
  7304. }
  7305. if (Ext.IsEmpty())
  7306. {
  7307. Ext = L".";
  7308. }
  7309. TFileFilter Filter;
  7310. Filter.Masks = FORMAT(L"*%s", (Ext));
  7311. Filter.Directories = false;
  7312. CurrentDirView->SelectFiles(Filter, Select);
  7313. }
  7314. }
  7315. //---------------------------------------------------------------------------
  7316. UnicodeString __fastcall TCustomScpExplorerForm::FileStatusBarText(
  7317. const TStatusFileInfo & FileInfo, TOperationSide Side)
  7318. {
  7319. UnicodeString Result;
  7320. if ((Side == osRemote) && (Terminal == NULL))
  7321. {
  7322. // noop
  7323. }
  7324. else
  7325. {
  7326. Result =
  7327. FMTLOAD(FILE_INFO_FORMAT,
  7328. (FormatBytes(FileInfo.SelectedSize),
  7329. FormatBytes(FileInfo.FilesSize),
  7330. FormatNumber(FileInfo.SelectedCount),
  7331. FormatNumber(FileInfo.FilesCount)));
  7332. }
  7333. return Result;
  7334. }
  7335. //---------------------------------------------------------------------------
  7336. void __fastcall TCustomScpExplorerForm::FileStatusBarPanelClick(
  7337. TTBXStatusPanel * Panel, TOperationSide Side)
  7338. {
  7339. if (Panel->Index == 1)
  7340. {
  7341. ToggleShowHiddenFiles();
  7342. }
  7343. else if (Panel->Index == 2)
  7344. {
  7345. Filter(Side);
  7346. }
  7347. }
  7348. //---------------------------------------------------------------------------
  7349. void __fastcall TCustomScpExplorerForm::UpdateFileStatusBar(
  7350. TTBXStatusBar * StatusBar, const TStatusFileInfo & FileInfo, TOperationSide Side)
  7351. {
  7352. DebugAssert(!StatusBar->SimplePanel);
  7353. StatusBar->Panels->Items[0]->Caption = FileStatusBarText(FileInfo, Side);
  7354. UpdateFileStatusExtendedPanels(StatusBar, FileInfo);
  7355. }
  7356. //---------------------------------------------------------------------------
  7357. void __fastcall TCustomScpExplorerForm::UpdateFileStatusExtendedPanels(
  7358. TTBXStatusBar * StatusBar, const TStatusFileInfo & FileInfo)
  7359. {
  7360. DebugAssert(StatusBar->Panels->Count >= 3);
  7361. TTBXStatusPanel * HiddenFilesPanel = StatusBar->Panels->Items[1];
  7362. if (FileInfo.HiddenCount > 0)
  7363. {
  7364. HiddenFilesPanel->Caption = FMTLOAD(FILE_INFO_HIDDEN2, (FormatNumber(FileInfo.HiddenCount)));
  7365. HiddenFilesPanel->ViewPriority = 90; // <100 allows hiding panel when it does not fit
  7366. }
  7367. else
  7368. {
  7369. HiddenFilesPanel->ViewPriority = 0;
  7370. // not really necessary, just to cleanup no-longer-valid data
  7371. HiddenFilesPanel->Caption = L"";
  7372. }
  7373. TTBXStatusPanel * FilteredFilesPanel = StatusBar->Panels->Items[2];
  7374. if (FileInfo.FilteredCount > 0)
  7375. {
  7376. FilteredFilesPanel->Caption = FMTLOAD(FILE_INFO_FILTERED2, (FormatNumber(FileInfo.FilteredCount)));
  7377. FilteredFilesPanel->ViewPriority = 90; // <100 allows hiding panel when it does not fit
  7378. }
  7379. else
  7380. {
  7381. FilteredFilesPanel->ViewPriority = 0;
  7382. // not really necessary, just to cleanup no-longer-valid data
  7383. FilteredFilesPanel->Caption = L"";
  7384. }
  7385. }
  7386. //---------------------------------------------------------------------------
  7387. void __fastcall TCustomScpExplorerForm::RemoteStatusBarClick(
  7388. TObject * /*Sender*/)
  7389. {
  7390. if (RemoteDirView->Enabled)
  7391. {
  7392. RemoteDirView->SetFocus();
  7393. }
  7394. }
  7395. //---------------------------------------------------------------------------
  7396. void __fastcall TCustomScpExplorerForm::ToggleQueueVisibility()
  7397. {
  7398. TQueueViewConfiguration Config = WinConfiguration->QueueView;
  7399. switch (Config.Show)
  7400. {
  7401. case qvShow:
  7402. if ((FQueueStatus != NULL) && (FQueueStatus->Count > 0))
  7403. {
  7404. Config.Show = qvHide;
  7405. }
  7406. else
  7407. {
  7408. Config.Show = Config.LastHideShow;
  7409. }
  7410. break;
  7411. case qvHideWhenEmpty:
  7412. if (ComponentVisible[fcQueueView])
  7413. {
  7414. Config.Show = qvHide;
  7415. }
  7416. else
  7417. {
  7418. Config.LastHideShow = Config.Show;
  7419. Config.Show = qvShow;
  7420. }
  7421. break;
  7422. case qvHide:
  7423. Config.LastHideShow = Config.Show;
  7424. Config.Show = qvShow;
  7425. break;
  7426. }
  7427. WinConfiguration->QueueView = Config;
  7428. }
  7429. //---------------------------------------------------------------------------
  7430. UnicodeString __fastcall TCustomScpExplorerForm::PathForCaption()
  7431. {
  7432. UnicodeString Result;
  7433. if (FTerminal != NULL)
  7434. {
  7435. switch (WinConfiguration->PathInCaption)
  7436. {
  7437. case picShort:
  7438. Result = TTerminalManager::Instance()->GetTerminalShortPath(FTerminal);
  7439. break;
  7440. case picFull:
  7441. Result = FTerminal->CurrentDirectory;
  7442. break;
  7443. }
  7444. }
  7445. return Result;
  7446. }
  7447. //---------------------------------------------------------------------------
  7448. void __fastcall TCustomScpExplorerForm::UpdateControls()
  7449. {
  7450. TTerminalManager::Instance()->UpdateAppTitle();
  7451. // WORAKRDOUND: Disabling list view when it is not showing yet does not set its
  7452. // background to gray on Windows 7 (works on Windows 10).
  7453. // See also EnableControl
  7454. if (Showing)
  7455. {
  7456. bool HasTerminal = (Terminal != NULL);
  7457. if (HasTerminal)
  7458. {
  7459. // Update path when it changes
  7460. if ((SessionsPageControl->ActivePage != NULL) && (GetSessionTabTerminal(SessionsPageControl->ActivePage) == Terminal))
  7461. {
  7462. UpdateSessionTab(SessionsPageControl->ActivePage);
  7463. }
  7464. if (!RemoteDirView->Enabled)
  7465. {
  7466. RemoteDirView->Enabled = true;
  7467. if (FRemoteDirViewWasFocused)
  7468. {
  7469. ActiveControl = RemoteDirView;
  7470. }
  7471. }
  7472. RemoteDriveView->Enabled = true;
  7473. RemoteDirView->Color = (FSessionColor != 0 ? FSessionColor : clWindow);
  7474. RemoteDriveView->Color = RemoteDirView->Color;
  7475. }
  7476. else
  7477. {
  7478. if (RemoteDirView->Enabled)
  7479. {
  7480. // This is first called when the form is being constructed
  7481. // (not anymore due to Showing test above)
  7482. // but the false is overriden in the constructor later.
  7483. // An even later in TScpCommanderForm::DoShow()
  7484. FRemoteDirViewWasFocused = (ActiveControl == RemoteDirView);
  7485. EnableControl(RemoteDirView, false);
  7486. }
  7487. EnableControl(RemoteDriveView, false);
  7488. }
  7489. EnableControl(QueueView3, HasTerminal);
  7490. QueueLabelUpdateStatus();
  7491. reinterpret_cast<TTBCustomItem *>(GetComponent(fcRemotePathComboBox))->Enabled = HasTerminal;
  7492. }
  7493. }
  7494. //---------------------------------------------------------------------------
  7495. void __fastcall TCustomScpExplorerForm::DoDirViewLoaded(TCustomDirView * /*Sender*/)
  7496. {
  7497. UpdateControls();
  7498. }
  7499. //---------------------------------------------------------------------------
  7500. void __fastcall TCustomScpExplorerForm::DirViewLoaded(
  7501. TObject * Sender)
  7502. {
  7503. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  7504. DebugAssert(DirView != NULL);
  7505. DoDirViewLoaded(DirView);
  7506. TransferPresetAutoSelect();
  7507. }
  7508. //---------------------------------------------------------------------------
  7509. void __fastcall TCustomScpExplorerForm::StartUpdates()
  7510. {
  7511. TUpdatesConfiguration Updates = WinConfiguration->Updates;
  7512. // first run after installation
  7513. if (double(Updates.LastCheck) == 0)
  7514. {
  7515. // make sure next time there will be an update (if enabled)
  7516. Updates.LastCheck = TDateTime(1);
  7517. WinConfiguration->Updates = Updates;
  7518. }
  7519. else if ((double(Updates.Period) > 0) &&
  7520. (Now() - Updates.LastCheck >= Updates.Period))
  7521. {
  7522. TThreadMethod OnUpdatesChecked = NULL;
  7523. if (!IsUWP())
  7524. {
  7525. OnUpdatesChecked = UpdatesChecked;
  7526. }
  7527. StartUpdateThread(OnUpdatesChecked);
  7528. }
  7529. }
  7530. //---------------------------------------------------------------------------
  7531. void __fastcall TCustomScpExplorerForm::UpdatesChecked()
  7532. {
  7533. UnicodeString Message;
  7534. bool New;
  7535. TQueryType Type;
  7536. GetUpdatesMessage(Message, New, Type, false);
  7537. if (!Message.IsEmpty())
  7538. {
  7539. if (New)
  7540. {
  7541. Message = FMTLOAD(NEW_VERSION_CLICK, (Message));
  7542. }
  7543. if (!New && (Type != qtWarning))
  7544. {
  7545. PostNote(UnformatMessage(Message), 0, UpdatesNoteClicked, NULL);
  7546. }
  7547. else
  7548. {
  7549. Notify(NULL, Message, Type, true, UpdatesNoteClicked);
  7550. }
  7551. if (New)
  7552. {
  7553. Configuration->Usage->Inc(L"UpdateNotifications");
  7554. }
  7555. }
  7556. }
  7557. //---------------------------------------------------------------------------
  7558. void __fastcall TCustomScpExplorerForm::UpdatesNoteClicked(TObject * /*Sender*/)
  7559. {
  7560. RestoreApp();
  7561. bool CanDisplay = !NonVisualDataModule->Busy;
  7562. if (!CanDisplay && (Screen->ActiveForm != NULL))
  7563. {
  7564. CanDisplay = (Screen->ActiveForm->Perform(WM_CAN_DISPLAY_UPDATES, 0, 0) != 0);
  7565. }
  7566. if (CanDisplay)
  7567. {
  7568. Configuration->Usage->Inc(L"UpdateNotificationsClicked");
  7569. CheckForUpdates(true);
  7570. }
  7571. else
  7572. {
  7573. Configuration->Usage->Inc(L"UpdateNotificationsBusy");
  7574. }
  7575. }
  7576. //---------------------------------------------------------------------------
  7577. void __fastcall TCustomScpExplorerForm::GetTransferPresetAutoSelectData(
  7578. TCopyParamRuleData & Data)
  7579. {
  7580. DebugAssert(Terminal != NULL);
  7581. Data.HostName = Terminal->SessionData->HostNameExpanded;
  7582. Data.UserName = Terminal->SessionData->UserNameExpanded;
  7583. Data.RemoteDirectory = RemoteDirView->PathName;
  7584. }
  7585. //---------------------------------------------------------------------------
  7586. void __fastcall TCustomScpExplorerForm::TransferPresetAutoSelect()
  7587. {
  7588. // Terminal can be null when we are changing local directory implicitly,
  7589. // because it has been deleted, while no session is connected
  7590. // (Login dialog is open)
  7591. if (FAllowTransferPresetAutoSelect && (Terminal != NULL))
  7592. {
  7593. TCopyParamRuleData Data;
  7594. GetTransferPresetAutoSelectData(Data);
  7595. int CopyParamIndex = GUIConfiguration->CopyParamList->Find(Data);
  7596. UnicodeString CopyParamCurrent = GUIConfiguration->CopyParamCurrent;
  7597. if (CopyParamIndex < 0)
  7598. {
  7599. // there is no preset that matches autoselection
  7600. // set preset that we consider "default"
  7601. FCopyParamAutoSelected = L""; // forget last autoselected preset
  7602. GUIConfiguration->CopyParamCurrent = FCopyParamDefault;
  7603. }
  7604. else
  7605. {
  7606. // there is preset matching autoselection
  7607. UnicodeString CopyParamName = GUIConfiguration->CopyParamList->Names[CopyParamIndex];
  7608. if (CopyParamName == FCopyParamAutoSelected)
  7609. {
  7610. // autoselected the same preset as the last time
  7611. // make no change (i.e. preserve custom user preset, if any)
  7612. }
  7613. else
  7614. {
  7615. // autoselected the different preset then the last time (or there
  7616. // was default preset set)
  7617. FCopyParamAutoSelected = CopyParamName; // remember autoselection
  7618. GUIConfiguration->CopyParamCurrent = CopyParamName;
  7619. }
  7620. }
  7621. if (GUIConfiguration->CopyParamCurrent != CopyParamCurrent)
  7622. {
  7623. if (CopyParamIndex >= 0)
  7624. {
  7625. Configuration->Usage->Inc(L"CopyParamAutoSelects");
  7626. }
  7627. TTransferPresetNoteData * Data = new TTransferPresetNoteData;
  7628. try
  7629. {
  7630. int Fmt =
  7631. (CopyParamIndex < 0) ?
  7632. (GUIConfiguration->CopyParamIndex < 0 ? COPY_PARAM_DEFAULT_NORM : COPY_PARAM_DEFAULT_CUSTOM) :
  7633. COPY_PARAM_AUTOSELECTED;
  7634. UnicodeString Message = FMTLOAD(Fmt, (StripHotkey(GUIConfiguration->CopyParamCurrent)));
  7635. Data->Message = MainInstructions(Message);
  7636. int CopyParamAttrs = Terminal->UsableCopyParamAttrs(0).General;
  7637. UnicodeString Info = GUIConfiguration->CurrentCopyParam.GetInfoStr(L"\n",
  7638. CopyParamAttrs);
  7639. if (CopyParamIndex >= 0)
  7640. {
  7641. DebugAssert(GUIConfiguration->CopyParamList->Rules[CopyParamIndex] != NULL);
  7642. Info = FORMAT(L"%s\n \n%s", (Info,
  7643. FMTLOAD(COPY_PARAM_RULE,
  7644. (GUIConfiguration->CopyParamList->Rules[CopyParamIndex]->GetInfoStr(L"\n")))));
  7645. }
  7646. Data->Message += L"\n\n" + Info;
  7647. if (WinConfiguration->CopyParamAutoSelectNotice)
  7648. {
  7649. TransferPresetNoteMessage(Data, true);
  7650. }
  7651. else
  7652. {
  7653. PostNote(Message, 0, TransferPresetNoteClicked, Data);
  7654. Data = NULL; // ownership passed
  7655. }
  7656. }
  7657. __finally
  7658. {
  7659. delete Data;
  7660. }
  7661. }
  7662. }
  7663. }
  7664. //---------------------------------------------------------------------------
  7665. void __fastcall TCustomScpExplorerForm::TransferPresetNoteMessage(
  7666. TTransferPresetNoteData * NoteData, bool AllowNeverAskAgain)
  7667. {
  7668. DebugAssert(NoteData != NULL);
  7669. TMessageParams Params(AllowNeverAskAgain ? mpNeverAskAgainCheck : 0);
  7670. TQueryButtonAlias Aliases[1];
  7671. Aliases[0].Button = qaIgnore; // "ignore" is after "ok"
  7672. Aliases[0].Alias = LoadStr(CONFIGURE_BUTTON);
  7673. Params.Aliases = Aliases;
  7674. Params.AliasesCount = LENOF(Aliases);
  7675. unsigned int Result =
  7676. MoreMessageDialog(NoteData->Message, NULL, qtInformation,
  7677. qaOK | qaIgnore, HELP_COPY_PARAM_AUTOSELECTED, &Params);
  7678. switch (Result)
  7679. {
  7680. case qaNeverAskAgain:
  7681. DebugAssert(AllowNeverAskAgain);
  7682. WinConfiguration->CopyParamAutoSelectNotice = false;
  7683. break;
  7684. case qaIgnore:
  7685. PreferencesDialog(pmPresets);
  7686. break;
  7687. }
  7688. }
  7689. //---------------------------------------------------------------------------
  7690. void __fastcall TCustomScpExplorerForm::TransferPresetNoteClicked(TObject * Sender)
  7691. {
  7692. // as of now useless, as this is used for notes only, never for balloons, ...
  7693. RestoreApp();
  7694. // .. and we should never be busy here
  7695. if (DebugAlwaysTrue(!NonVisualDataModule->Busy))
  7696. {
  7697. TransferPresetNoteMessage(DebugNotNull(dynamic_cast<TTransferPresetNoteData *>(Sender)), false);
  7698. }
  7699. }
  7700. //---------------------------------------------------------------------------
  7701. void __fastcall TCustomScpExplorerForm::PreferencesDialog(
  7702. TPreferencesMode APreferencesMode)
  7703. {
  7704. std::unique_ptr<TPreferencesDialogData> PreferencesData;
  7705. TCopyParamRuleData Data;
  7706. if (Terminal != NULL)
  7707. {
  7708. PreferencesData.reset(new TPreferencesDialogData());
  7709. GetTransferPresetAutoSelectData(Data);
  7710. PreferencesData->CopyParamRuleData = &Data;
  7711. }
  7712. DoPreferencesDialog(APreferencesMode, PreferencesData.get());
  7713. }
  7714. //---------------------------------------------------------------------------
  7715. void __fastcall TCustomScpExplorerForm::AdHocCustomCommandValidate(
  7716. const TCustomCommandType & Command)
  7717. {
  7718. if (CustomCommandState(Command, FEditingFocusedAdHocCommand, ccltAll) <= 0)
  7719. {
  7720. throw Exception(FMTLOAD(CUSTOM_COMMAND_IMPOSSIBLE, (Command.Command)));
  7721. }
  7722. }
  7723. //---------------------------------------------------------------------------
  7724. void __fastcall TCustomScpExplorerForm::AdHocCustomCommand(bool OnFocused)
  7725. {
  7726. bool RemoteAllowed = CustomCommandRemoteAllowed();
  7727. TCustomCommandType Command;
  7728. // make sure we use local custom command when remote are not supported
  7729. if (RemoteAllowed || FLAGSET(FLastCustomCommand.Params, ccLocal))
  7730. {
  7731. Command = FLastCustomCommand;
  7732. }
  7733. else
  7734. {
  7735. Command.Params = Command.Params | ccLocal;
  7736. }
  7737. Command.Name = LoadStr(CUSTOM_COMMAND_AD_HOC_NAME);
  7738. FEditingFocusedAdHocCommand = OnFocused;
  7739. bool LocalSide = (FCurrentSide == osLocal);
  7740. int Options =
  7741. FLAGMASK((!RemoteAllowed || LocalSide), ccoDisableRemote) |
  7742. FLAGMASK(LocalSide, ccoDisableRemoteFiles);
  7743. if (DoCustomCommandDialog(Command, WinConfiguration->CustomCommandList,
  7744. ccmAdHoc, Options, AdHocCustomCommandValidate, NULL))
  7745. {
  7746. FLastCustomCommand = Command;
  7747. UpdateCustomCommandsToolbar();
  7748. ExecuteFileOperation(foCustomCommand, osRemote, OnFocused, false, &Command);
  7749. }
  7750. }
  7751. //---------------------------------------------------------------------------
  7752. void __fastcall TCustomScpExplorerForm::LastCustomCommand(bool OnFocused)
  7753. {
  7754. DebugAssert(!FLastCustomCommand.Command.IsEmpty());
  7755. int State = CustomCommandState(FLastCustomCommand, OnFocused, ccltAll);
  7756. DebugAssert(State > 0);
  7757. if (State <= 0)
  7758. {
  7759. throw Exception(FMTLOAD(CUSTOM_COMMAND_IMPOSSIBLE, (FLastCustomCommand.Command)));
  7760. }
  7761. ExecuteFileOperation(foCustomCommand, osRemote, OnFocused, false, &FLastCustomCommand);
  7762. }
  7763. //---------------------------------------------------------------------------
  7764. bool __fastcall TCustomScpExplorerForm::GetLastCustomCommand(bool OnFocused,
  7765. TCustomCommandType & Command, int & State)
  7766. {
  7767. bool Result = !FLastCustomCommand.Command.IsEmpty();
  7768. if (Result)
  7769. {
  7770. Command = FLastCustomCommand;
  7771. State = CustomCommandState(FLastCustomCommand, OnFocused, ccltAll);
  7772. }
  7773. return Result;
  7774. }
  7775. //---------------------------------------------------------------------------
  7776. void __fastcall TCustomScpExplorerForm::BeforeAction()
  7777. {
  7778. if (RemoteDirView->ItemFocused != NULL)
  7779. {
  7780. RemoteDirView->ItemFocused->CancelEdit();
  7781. }
  7782. }
  7783. //---------------------------------------------------------------------------
  7784. void __fastcall TCustomScpExplorerForm::PostComponentHide(Byte Component)
  7785. {
  7786. DebugAssert(ComponentVisible[Component]);
  7787. PostMessage(Handle, WM_COMPONENT_HIDE, Component, 0);
  7788. }
  7789. //---------------------------------------------------------------------------
  7790. void __fastcall TCustomScpExplorerForm::QueueSplitterDblClick(TObject * /*Sender*/)
  7791. {
  7792. // when queue panel is resized here directly, the status bar is stretched
  7793. // over whole space the panel occupied
  7794. PostComponentHide(fcQueueView);
  7795. }
  7796. //---------------------------------------------------------------------------
  7797. void __fastcall TCustomScpExplorerForm::Dispatch(void * Message)
  7798. {
  7799. TMessage * M = static_cast<TMessage*>(Message);
  7800. switch (M->Msg)
  7801. {
  7802. case CM_DIALOGCHAR:
  7803. CMDialogChar(*M);
  7804. break;
  7805. case WM_APPCOMMAND:
  7806. WMAppCommand(*M);
  7807. break;
  7808. case WM_SYSCOMMAND:
  7809. WMSysCommand(*M);
  7810. break;
  7811. case WM_QUERYENDSESSION:
  7812. WMQueryEndSession(*M);
  7813. break;
  7814. case WM_ENDSESSION:
  7815. WMEndSession(*reinterpret_cast<TWMEndSession *>(M));
  7816. break;
  7817. #ifdef _DEBUG
  7818. case WM_WINDOWPOSCHANGED:
  7819. WMWindowPosChanged(*reinterpret_cast<TWMWindowPosMsg *>(M));
  7820. break;
  7821. case CM_CUSTOMSTYLECHANGED:
  7822. TForm::Dispatch(Message);
  7823. break;
  7824. #endif
  7825. case WM_COMPONENT_HIDE:
  7826. {
  7827. Byte Component = static_cast<Byte>(M->WParam);
  7828. // sanity check
  7829. if (ComponentVisible[Component])
  7830. {
  7831. // special treatment
  7832. if (Component == fcQueueView)
  7833. {
  7834. ToggleQueueVisibility();
  7835. DebugAssert(!ComponentVisible[fcQueueView]);
  7836. }
  7837. else
  7838. {
  7839. ComponentVisible[Component] = false;
  7840. }
  7841. }
  7842. }
  7843. break;
  7844. case WM_COPYDATA:
  7845. WMCopyData(*M);
  7846. break;
  7847. case WM_MANAGES_CAPTION:
  7848. // caption managed in TTerminalManager::UpdateAppTitle()
  7849. M->Result = 1;
  7850. break;
  7851. case WM_WANTS_MOUSEWHEEL:
  7852. M->Result = 1;
  7853. break;
  7854. case CM_SHOWINGCHANGED:
  7855. CMShowingChanged(*M);
  7856. break;
  7857. case WM_CLOSE:
  7858. WMClose(*M);
  7859. break;
  7860. case WM_DPICHANGED:
  7861. WMDpiChanged(*M);
  7862. break;
  7863. case CM_DPICHANGED:
  7864. CMDpiChanged(*M);
  7865. break;
  7866. default:
  7867. TForm::Dispatch(Message);
  7868. break;
  7869. }
  7870. }
  7871. //---------------------------------------------------------------------------
  7872. void __fastcall TCustomScpExplorerForm::UpdateImages()
  7873. {
  7874. // noop
  7875. }
  7876. //---------------------------------------------------------------------------
  7877. void __fastcall TCustomScpExplorerForm::CMDpiChanged(TMessage & Message)
  7878. {
  7879. TForm::Dispatch(&Message);
  7880. // regenerate session images
  7881. DoTerminalListChanged(true);
  7882. UpdateImages();
  7883. }
  7884. //---------------------------------------------------------------------------
  7885. void __fastcall TCustomScpExplorerForm::WMDpiChanged(TMessage & Message)
  7886. {
  7887. TForm::Dispatch(&Message);
  7888. Configuration->Usage->Inc(L"PixelsPerInchChanged");
  7889. UpdatePixelsPerInchMainWindowCounter();
  7890. }
  7891. //---------------------------------------------------------------------------
  7892. void __fastcall TCustomScpExplorerForm::WMClose(TMessage & Message)
  7893. {
  7894. // Cannot close window while we are busy.
  7895. // We cannot test this in FormCloseQuery as that is called also from
  7896. // Close(), which is called by CloseApplicationAction. So we can be busy
  7897. // there already even, when it is legitimate to close the application.
  7898. // Possibly a better place to handle this would be WMSysCommand.
  7899. if (NonVisualDataModule->Busy)
  7900. {
  7901. Message.Result = 1;
  7902. }
  7903. else
  7904. {
  7905. TForm::Dispatch(&Message);
  7906. }
  7907. }
  7908. //---------------------------------------------------------------------------
  7909. void __fastcall TCustomScpExplorerForm::CMShowingChanged(TMessage & Message)
  7910. {
  7911. TForm::Dispatch(&Message);
  7912. if (Showing && (Terminal == NULL))
  7913. {
  7914. // When we are starting minimized (i.e. from an installer),
  7915. // postpone showing Login dialog until we get restored.
  7916. // Otherwise the Login dialog (and Authentication window) show restored
  7917. // over invidible (minimized) main window.
  7918. if (WindowState == wsMinimized)
  7919. {
  7920. FNeedSession = true;
  7921. }
  7922. else
  7923. {
  7924. // This happens before application ever goes idle, so the toolbars would
  7925. // stay enabled (initial state) until the Login dialog is dismissed.
  7926. UpdateActions();
  7927. NonVisualDataModule->StartBusy();
  7928. try
  7929. {
  7930. // Need to process WM_ACTIVATEAPP before showing the Login dialog,
  7931. // otherwise the dialog does not receive focus.
  7932. // With Commander interface the ProcessMessages is called already
  7933. // by TDriveView, but with Explorer interface, we need to call it explicily
  7934. Application->ProcessMessages();
  7935. // do not reload sessions, they have been loaded just now (optimization)
  7936. NeedSession(false);
  7937. }
  7938. __finally
  7939. {
  7940. NonVisualDataModule->EndBusy();
  7941. }
  7942. }
  7943. }
  7944. }
  7945. //---------------------------------------------------------------------------
  7946. void __fastcall TCustomScpExplorerForm::FormConstrainedResize(
  7947. TObject * /*Sender*/, int & MinWidth, int & MinHeight, int & MaxWidth,
  7948. int & MaxHeight)
  7949. {
  7950. // workaround for bug in TWinControl.CalcConstraints
  7951. // Check for empty rect (restore from iconic state) is done there only after
  7952. // call to AdjustClientRect, which enlarges the rect (for forms).
  7953. TRect R = GetClientRect();
  7954. // when restoring from iconic state, place no restrictions
  7955. if (IsRectEmpty(R))
  7956. {
  7957. MinWidth = 0;
  7958. MinHeight = 0;
  7959. MaxWidth = 32000;
  7960. MaxHeight = 32000;
  7961. }
  7962. }
  7963. //---------------------------------------------------------------------------
  7964. void __fastcall TCustomScpExplorerForm::GetSpaceAvailable(const UnicodeString Path,
  7965. TSpaceAvailable & ASpaceAvailable, bool & Close)
  7966. {
  7967. // terminal can be already closed (e.g. dropped connection)
  7968. if ((Terminal != NULL) && Terminal->IsCapable[fcCheckingSpaceAvailable])
  7969. {
  7970. Configuration->Usage->Inc(L"SpaceAvailableChecks");
  7971. try
  7972. {
  7973. Terminal->SpaceAvailable(Path, ASpaceAvailable);
  7974. }
  7975. catch(...)
  7976. {
  7977. if (!Terminal->Active)
  7978. {
  7979. Close = true;
  7980. }
  7981. throw;
  7982. }
  7983. }
  7984. }
  7985. //---------------------------------------------------------------------------
  7986. void __fastcall TCustomScpExplorerForm::FileSystemInfo()
  7987. {
  7988. const TSessionInfo & SessionInfo = Terminal->GetSessionInfo();
  7989. const TFileSystemInfo & FileSystemInfo = Terminal->GetFileSystemInfo(true);
  7990. TGetSpaceAvailable OnGetSpaceAvailable = NULL;
  7991. if (Terminal->IsCapable[fcCheckingSpaceAvailable])
  7992. {
  7993. OnGetSpaceAvailable = GetSpaceAvailable;
  7994. }
  7995. DoFileSystemInfoDialog(SessionInfo, FileSystemInfo, Terminal->CurrentDirectory,
  7996. OnGetSpaceAvailable);
  7997. }
  7998. //---------------------------------------------------------------------------
  7999. TSessionData * __fastcall TCustomScpExplorerForm::SessionDataForCode()
  8000. {
  8001. std::unique_ptr<TSessionData> Data(CloneCurrentSessionData());
  8002. Terminal->FillSessionDataForCode(Data.get());
  8003. return Data.release();
  8004. }
  8005. //---------------------------------------------------------------------------
  8006. void __fastcall TCustomScpExplorerForm::GenerateUrl(TStrings * Paths)
  8007. {
  8008. std::unique_ptr<TSessionData> Data(SessionDataForCode());
  8009. DoGenerateUrlDialog(Data.get(), Paths);
  8010. }
  8011. //---------------------------------------------------------------------------
  8012. void __fastcall TCustomScpExplorerForm::SessionGenerateUrl()
  8013. {
  8014. GenerateUrl(NULL);
  8015. }
  8016. //---------------------------------------------------------------------------
  8017. void __fastcall TCustomScpExplorerForm::FileGenerateUrl()
  8018. {
  8019. std::unique_ptr<TStrings> Paths(new TStringList());
  8020. DirView(osCurrent)->CreateFileList(false, true, Paths.get());
  8021. GenerateUrl(Paths.get());
  8022. }
  8023. //---------------------------------------------------------------------------
  8024. void __fastcall TCustomScpExplorerForm::SetSessionColor(TColor value)
  8025. {
  8026. if (value != FSessionColor)
  8027. {
  8028. FSessionColor = value;
  8029. TColor C = (value != 0 ? value : Vcl::Graphics::clNone);
  8030. TTBXColorItem * ColorItem = dynamic_cast<TTBXColorItem *>(
  8031. static_cast<TObject *>(GetComponent(fcColorMenu)));
  8032. DebugAssert(ColorItem != NULL);
  8033. ColorItem->Color = C;
  8034. NonVisualDataModule->ColorMenuItem->Color = C;
  8035. // Is null when called from LastTerminalClosed
  8036. if (Terminal != NULL)
  8037. {
  8038. SessionsPageControl->ActivePage->ImageIndex = AddSessionColor(value);
  8039. }
  8040. UpdateControls();
  8041. }
  8042. }
  8043. //---------------------------------------------------------------------------
  8044. bool __fastcall TCustomScpExplorerForm::CancelNote(bool Force)
  8045. {
  8046. bool Result = FNoteTimer->Enabled;
  8047. if (Result)
  8048. {
  8049. // cannot cancel note too early
  8050. bool NotEarly =
  8051. (Now() - FNoteShown >
  8052. EncodeTimeVerbose(0, 0, (unsigned short)(WinConfiguration->NotificationsStickTime), 0));
  8053. if (Force || NotEarly)
  8054. {
  8055. FNoteTimer->Enabled = false;
  8056. FNote = L"";
  8057. SAFE_DESTROY(FNoteData);
  8058. FOnNoteClick = NULL;
  8059. FNoteHints = FNotes->Text;
  8060. FNoteHints.Delete(FNoteHints.Length() - 1, 2);
  8061. UpdateStatusBar();
  8062. }
  8063. }
  8064. return Result;
  8065. }
  8066. //---------------------------------------------------------------------------
  8067. void __fastcall TCustomScpExplorerForm::NoteTimer(TObject * /*Sender*/)
  8068. {
  8069. DebugAssert(FNoteTimer->Enabled);
  8070. CancelNote(true);
  8071. }
  8072. //---------------------------------------------------------------------------
  8073. void __fastcall TCustomScpExplorerForm::AddNote(UnicodeString Note, bool UpdateNow)
  8074. {
  8075. int P = Note.Pos(L"\n");
  8076. if (P > 0)
  8077. {
  8078. Note.SetLength(P - 1);
  8079. }
  8080. FNotes->Add(FORMAT(L"[%s] %s",
  8081. (FormatDateTime(Configuration->TimeFormat, Now()), Note)));
  8082. while (FNotes->Count > 10)
  8083. {
  8084. FNotes->Delete(0);
  8085. }
  8086. if (UpdateNow)
  8087. {
  8088. FNoteHints = FNotes->Text;
  8089. FNoteHints.Delete(FNoteHints.Length() - 1, 2);
  8090. UpdateStatusBar();
  8091. }
  8092. }
  8093. //---------------------------------------------------------------------------
  8094. void __fastcall TCustomScpExplorerForm::PostNote(UnicodeString Note,
  8095. unsigned int Seconds, TNotifyEvent OnNoteClick, TObject * NoteData)
  8096. {
  8097. int P = Note.Pos(L"\n");
  8098. if (P > 0)
  8099. {
  8100. Note.SetLength(P - 1);
  8101. }
  8102. FNoteHints = FNotes->Text;
  8103. FNoteHints.Delete(FNoteHints.Length() - 1, 2);
  8104. FNote = Note;
  8105. SAFE_DESTROY(FNoteData);
  8106. FNoteData = NoteData;
  8107. FOnNoteClick = OnNoteClick;
  8108. AddNote(Note, false);
  8109. UpdateStatusBar();
  8110. FNoteShown = Now();
  8111. FNoteTimer->Enabled = false;
  8112. if (Seconds == 0)
  8113. {
  8114. Seconds = WinConfiguration->NotificationsTimeout;
  8115. }
  8116. FNoteTimer->Interval = Seconds * MSecsPerSec;
  8117. FNoteTimer->Enabled = true;
  8118. }
  8119. //---------------------------------------------------------------------------
  8120. void __fastcall TCustomScpExplorerForm::ReadDirectoryCancelled()
  8121. {
  8122. PostNote(LoadStr(DIRECTORY_READING_CANCELLED), 0, NULL, NULL);
  8123. }
  8124. //---------------------------------------------------------------------------
  8125. void __fastcall TCustomScpExplorerForm::SynchronizeBrowsingChanged()
  8126. {
  8127. if (NonVisualDataModule->SynchronizeBrowsingAction->Checked)
  8128. {
  8129. Configuration->Usage->Inc(L"SynchronizeBrowsingEnabled");
  8130. }
  8131. PostNote(FORMAT(LoadStrPart(SYNC_DIR_BROWSE_TOGGLE, 1),
  8132. (LoadStrPart(SYNC_DIR_BROWSE_TOGGLE,
  8133. (NonVisualDataModule->SynchronizeBrowsingAction->Checked ? 2 : 3)))),
  8134. 0, NULL, NULL);
  8135. }
  8136. //---------------------------------------------------------------------------
  8137. void __fastcall TCustomScpExplorerForm::ToggleShowHiddenFiles()
  8138. {
  8139. WinConfiguration->ShowHiddenFiles = !WinConfiguration->ShowHiddenFiles;
  8140. PostNote(FORMAT(LoadStrPart(SHOW_HIDDEN_FILES_TOGGLE, 1),
  8141. (LoadStrPart(SHOW_HIDDEN_FILES_TOGGLE,
  8142. (WinConfiguration->ShowHiddenFiles ? 2 : 3)))), 0, NULL, NULL);
  8143. }
  8144. //---------------------------------------------------------------------------
  8145. void __fastcall TCustomScpExplorerForm::SetFormatSizeBytes(TFormatBytesStyle Style)
  8146. {
  8147. WinConfiguration->FormatSizeBytes = Style;
  8148. }
  8149. //---------------------------------------------------------------------------
  8150. void __fastcall TCustomScpExplorerForm::ToggleAutoReadDirectoryAfterOp()
  8151. {
  8152. Configuration->AutoReadDirectoryAfterOp = !Configuration->AutoReadDirectoryAfterOp;
  8153. PostNote(FORMAT(LoadStrPart(AUTO_READ_DIRECTORY_TOGGLE, 1),
  8154. (LoadStrPart(AUTO_READ_DIRECTORY_TOGGLE,
  8155. (Configuration->AutoReadDirectoryAfterOp ? 2 : 3)))), 0, NULL, NULL);
  8156. }
  8157. //---------------------------------------------------------------------------
  8158. void __fastcall TCustomScpExplorerForm::StatusBarPanelDblClick(
  8159. TTBXCustomStatusBar * Sender, TTBXStatusPanel * Panel)
  8160. {
  8161. if (Panel->Index == 0)
  8162. {
  8163. if (FOnNoteClick != NULL)
  8164. {
  8165. // prevent the user data from being freed by possible call
  8166. // to CancelNote or PostNote during call to OnNoteClick
  8167. std::unique_ptr<TObject> NoteData(FNoteData);
  8168. TNotifyEvent OnNoteClick = FOnNoteClick;
  8169. FNoteData = NULL;
  8170. // need to cancel the note as we are going to delete its user data
  8171. CancelNote(true);
  8172. OnNoteClick(NoteData.get());
  8173. }
  8174. }
  8175. if (Panel->Index >= Sender->Panels->Count - SessionPanelCount)
  8176. {
  8177. FileSystemInfo();
  8178. }
  8179. }
  8180. //---------------------------------------------------------------------------
  8181. void __fastcall TCustomScpExplorerForm::LockWindow(bool Force)
  8182. {
  8183. // workaround:
  8184. // 1) for unknown reason, disabling window, while minimized,
  8185. // prevents it from restoring, even if it was enabled again meanwhile
  8186. // 2) when disabling the main window, while another has focus
  8187. // minimize is no longer possible
  8188. // ("keep up to date" dialog - though does not seem to be the case with "find file" -
  8189. // so it has probably something to do with the semi-modalness of the "keep up to date" dialog)
  8190. // Shouldn't we use IsApplicationMinimized() here?
  8191. if ((FLockSuspendLevel == 0) && !IsIconic(Application->Handle) && (Force || (Screen->ActiveForm == this)))
  8192. {
  8193. Enabled = false;
  8194. }
  8195. FLockLevel++;
  8196. }
  8197. //---------------------------------------------------------------------------
  8198. void __fastcall TCustomScpExplorerForm::UnlockWindow()
  8199. {
  8200. DebugAssert(FLockLevel > 0);
  8201. FLockLevel--;
  8202. if (FLockLevel == 0)
  8203. {
  8204. DebugAssert(FLockSuspendLevel == 0);
  8205. Enabled = true;
  8206. }
  8207. }
  8208. //---------------------------------------------------------------------------
  8209. void __fastcall TCustomScpExplorerForm::SuspendWindowLock()
  8210. {
  8211. // We need to make sure that window is enabled when the last modal window closes
  8212. // otherwise focus is not restored correctly.
  8213. // So we re-enable the window when modal window opens and
  8214. // disable it back after is closes
  8215. if (FLockLevel > 0)
  8216. {
  8217. // while we have nesting counter, we know that we never be called
  8218. // recursivelly as Application->OnModalBegin is called only
  8219. // for the top-level modal window
  8220. if (DebugAlwaysTrue(FLockSuspendLevel == 0))
  8221. {
  8222. // won't be disabled when conditions in LockWindow() were not satisfied
  8223. FDisabledOnLockSuspend = !Enabled;
  8224. // When minimized to tray (or actually when set to SW_HIDE),
  8225. // setting Enabled makes the window focusable even when there's
  8226. // modal window over it
  8227. if (!FTrayIcon->Visible)
  8228. {
  8229. Enabled = true;
  8230. }
  8231. }
  8232. FLockSuspendLevel++;
  8233. }
  8234. }
  8235. //---------------------------------------------------------------------------
  8236. void __fastcall TCustomScpExplorerForm::ResumeWindowLock()
  8237. {
  8238. if (FLockSuspendLevel > 0)
  8239. {
  8240. DebugAssert(FLockLevel > 0);
  8241. FLockSuspendLevel--;
  8242. // see comment in SuspendWindowLock
  8243. if (DebugAlwaysTrue(FLockSuspendLevel == 0))
  8244. {
  8245. // Note that window can be enabled here, when we were minized to tray when
  8246. // was SuspendWindowLock() called.
  8247. // We should possibly do the same check as in LockWindow(),
  8248. // if it is ever possible that the consitions change between
  8249. // SuspendWindowLock() and ResumeWindowLock()
  8250. Enabled = !FDisabledOnLockSuspend;
  8251. }
  8252. }
  8253. }
  8254. //---------------------------------------------------------------------------
  8255. void __fastcall TCustomScpExplorerForm::UpdateRemotePathComboBox(bool TextOnly)
  8256. {
  8257. if (!TextOnly)
  8258. {
  8259. TTBXComboBoxItem * RemotePathComboBox =
  8260. reinterpret_cast<TTBXComboBoxItem *>(GetComponent(fcRemotePathComboBox));
  8261. TStrings * Items = RemotePathComboBox->Strings;
  8262. Items->BeginUpdate();
  8263. try
  8264. {
  8265. Items->Clear();
  8266. if (Terminal != NULL)
  8267. {
  8268. UnicodeString APath = UnixExcludeTrailingBackslash(RemoteDirView->Path);
  8269. while (!IsUnixRootPath(APath))
  8270. {
  8271. int P = APath.LastDelimiter(L'/');
  8272. DebugAssert(P >= 0);
  8273. Items->Insert(0, APath.SubString(P + 1, APath.Length() - P));
  8274. APath.SetLength(P - 1);
  8275. }
  8276. Items->Insert(0, Customunixdirview_SUnixDefaultRootName);
  8277. }
  8278. }
  8279. __finally
  8280. {
  8281. RemotePathComboBox->ItemIndex = Items->Count - 1;
  8282. // Setting ItemIndex to -1 does not reset its text
  8283. if (Items->Count == 0)
  8284. {
  8285. RemotePathComboBox->Text = L"";
  8286. }
  8287. Items->EndUpdate();
  8288. }
  8289. }
  8290. }
  8291. //---------------------------------------------------------------------------
  8292. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxAdjustImageIndex(
  8293. TTBXComboBoxItem * Sender, const UnicodeString /*AText*/, int AIndex,
  8294. int & ImageIndex)
  8295. {
  8296. if (AIndex < 0)
  8297. {
  8298. AIndex = Sender->ItemIndex;
  8299. }
  8300. ImageIndex = (AIndex < Sender->Strings->Count - 1 ? StdDirIcon : StdDirSelIcon);
  8301. }
  8302. //---------------------------------------------------------------------------
  8303. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxDrawItem(
  8304. TTBXCustomList * /*Sender*/, TCanvas * /*ACanvas*/, TRect & ARect, int AIndex,
  8305. int /*AHoverIndex*/, bool & /*DrawDefault*/)
  8306. {
  8307. ARect.Left += (10 * AIndex);
  8308. }
  8309. //---------------------------------------------------------------------------
  8310. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxMeasureWidth(
  8311. TTBXCustomList * /*Sender*/, TCanvas * /*ACanvas*/, int AIndex, int &AWidth)
  8312. {
  8313. AWidth += (10 * AIndex);
  8314. }
  8315. //---------------------------------------------------------------------------
  8316. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxItemClick(
  8317. TObject * Sender)
  8318. {
  8319. TTBXComboBoxItem * RemotePathComboBox = dynamic_cast<TTBXComboBoxItem*>(Sender);
  8320. UnicodeString APath = UnixExcludeTrailingBackslash(RemoteDirView->Path);
  8321. int Index = RemotePathComboBox->ItemIndex;
  8322. while (Index < RemotePathComboBox->Strings->Count - 1)
  8323. {
  8324. APath = UnixExtractFileDir(APath);
  8325. Index++;
  8326. }
  8327. // VanDyke style paths
  8328. if (APath.IsEmpty())
  8329. {
  8330. DebugAssert(RemotePathComboBox->ItemIndex == 0);
  8331. APath = ROOTDIRECTORY;
  8332. }
  8333. if (RemoteDirView->Path != APath)
  8334. {
  8335. RemoteDirView->Path = APath;
  8336. }
  8337. }
  8338. //---------------------------------------------------------------------------
  8339. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxCancel(TObject * Sender)
  8340. {
  8341. DebugAssert(Sender == GetComponent(fcRemotePathComboBox));
  8342. DebugUsedParam(Sender);
  8343. UpdateRemotePathComboBox(true);
  8344. }
  8345. //---------------------------------------------------------------------------
  8346. void __fastcall TCustomScpExplorerForm::DirViewEditing(
  8347. TObject * Sender, TListItem * Item, bool & /*AllowEdit*/)
  8348. {
  8349. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  8350. DebugAssert(DirView != NULL);
  8351. if (!WinConfiguration->RenameWholeName && !DirView->ItemIsDirectory(Item))
  8352. {
  8353. HWND Edit = ListView_GetEditControl(DirView->Handle);
  8354. // OnEditing is called also from TCustomListView::CanEdit
  8355. if (Edit != NULL)
  8356. {
  8357. EditSelectBaseName(Edit);
  8358. }
  8359. }
  8360. }
  8361. //---------------------------------------------------------------------------
  8362. //---------------------------------------------------------------------------
  8363. TDragDropFilesEx * __fastcall TCustomScpExplorerForm::CreateDragDropFilesEx()
  8364. {
  8365. TDragDropFilesEx * Result = new TDragDropFilesEx(this);
  8366. Result->AutoDetectDnD = false;
  8367. Result->NeedValid = TFileExMustDnDSet() << nvFilename;
  8368. Result->RenderDataOn = rdoEnterAndDropSync;
  8369. Result->TargetEffects = TDropEffectSet() << deCopy << deMove;
  8370. return Result;
  8371. }
  8372. //---------------------------------------------------------------------------
  8373. void __fastcall TCustomScpExplorerForm::CreateWnd()
  8374. {
  8375. TForm::CreateWnd();
  8376. if (FSessionsDragDropFilesEx == NULL)
  8377. {
  8378. FSessionsDragDropFilesEx = CreateDragDropFilesEx();
  8379. FSessionsDragDropFilesEx->OnDragOver = SessionsDDDragOver;
  8380. FSessionsDragDropFilesEx->OnProcessDropped = SessionsDDProcessDropped;
  8381. FSessionsDragDropFilesEx->OnDragEnter = SessionsDDDragEnter;
  8382. FSessionsDragDropFilesEx->OnDragLeave = SessionsDDDragLeave;
  8383. }
  8384. if (FQueueDragDropFilesEx == NULL)
  8385. {
  8386. FQueueDragDropFilesEx = CreateDragDropFilesEx();
  8387. // No need to set OnDragOver as we do not have any restrictions
  8388. FQueueDragDropFilesEx->OnProcessDropped = QueueDDProcessDropped;
  8389. FQueueDragDropFilesEx->OnDragEnter = QueueDDDragEnter;
  8390. FQueueDragDropFilesEx->OnDragLeave = QueueDDDragLeave;
  8391. }
  8392. }
  8393. //---------------------------------------------------------------------------
  8394. void __fastcall TCustomScpExplorerForm::DestroyWnd()
  8395. {
  8396. TForm::DestroyWnd();
  8397. FSessionsDragDropFilesEx->DragDropControl = NULL;
  8398. FQueueDragDropFilesEx->DragDropControl = NULL;
  8399. }
  8400. //---------------------------------------------------------------------------
  8401. void __fastcall TCustomScpExplorerForm::FormShow(TObject * /*Sender*/)
  8402. {
  8403. SideEnter(FCurrentSide);
  8404. FEverShown = true;
  8405. }
  8406. //---------------------------------------------------------------------------
  8407. void __fastcall TCustomScpExplorerForm::DoFindFiles(
  8408. TTerminal * ATerminal, UnicodeString Directory, const TFileMasks & FileMask,
  8409. TFileFoundEvent OnFileFound, TFindingFileEvent OnFindingFile)
  8410. {
  8411. if (!NonVisualDataModule->Busy)
  8412. {
  8413. TTerminalManager::Instance()->ActiveTerminal = ATerminal;
  8414. Configuration->Usage->Inc(L"FileFinds");
  8415. LockWindow(true);
  8416. NonVisualDataModule->StartBusy();
  8417. WinConfiguration->LockedInterface = true;
  8418. try
  8419. {
  8420. FTerminal->FilesFind(Directory, FileMask, OnFileFound, OnFindingFile);
  8421. }
  8422. __finally
  8423. {
  8424. WinConfiguration->LockedInterface = false;
  8425. NonVisualDataModule->EndBusy();
  8426. UnlockWindow();
  8427. }
  8428. }
  8429. }
  8430. //---------------------------------------------------------------------------
  8431. void __fastcall TCustomScpExplorerForm::DoFocusRemotePath(TTerminal * ATerminal, const UnicodeString & Path)
  8432. {
  8433. if (!NonVisualDataModule->Busy)
  8434. {
  8435. TTerminalManager::Instance()->ActiveTerminal = ATerminal;
  8436. SetFocus();
  8437. RemoteDirView->Path = UnixExtractFilePath(Path);
  8438. TListItem * Item = RemoteDirView->FindFileItem(UnixExtractFileName(Path));
  8439. if (Item != NULL)
  8440. {
  8441. RemoteDirView->ItemFocused = Item;
  8442. RemoteDirView->ItemFocused->MakeVisible(false);
  8443. RemoteDirView->SetFocus();
  8444. }
  8445. }
  8446. }
  8447. //---------------------------------------------------------------------------
  8448. bool __fastcall TCustomScpExplorerForm::CanOperateOnFoundFiles(TTerminal * ATerminal)
  8449. {
  8450. bool Result = !NonVisualDataModule->Busy;
  8451. if (Result)
  8452. {
  8453. TTerminalManager::Instance()->ActiveTerminal = ATerminal;
  8454. }
  8455. return Result;
  8456. }
  8457. //---------------------------------------------------------------------------
  8458. void __fastcall TCustomScpExplorerForm::DoOperationOnFoundFiles(
  8459. TFileOperation Operation, TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  8460. {
  8461. if (CanOperateOnFoundFiles(ATerminal))
  8462. {
  8463. TValueRestorer<TFileOperationFinishedEvent> OnFileOperationFinishedRestorer(FOnFileOperationFinished);
  8464. FOnFileOperationFinished = OnFileOperationFinished;
  8465. ExecuteFileOperation(Operation, osRemote, FileList, false, NULL);
  8466. }
  8467. }
  8468. //---------------------------------------------------------------------------
  8469. void __fastcall TCustomScpExplorerForm::DoDeleteFoundFiles(
  8470. TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  8471. {
  8472. DoOperationOnFoundFiles(foDelete, ATerminal, FileList, OnFileOperationFinished);
  8473. }
  8474. //---------------------------------------------------------------------------
  8475. void __fastcall TCustomScpExplorerForm::DoDownloadFoundFiles(
  8476. TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  8477. {
  8478. DoOperationOnFoundFiles(foCopy, ATerminal, FileList, OnFileOperationFinished);
  8479. }
  8480. //---------------------------------------------------------------------------
  8481. void __fastcall TCustomScpExplorerForm::DoEditFoundFiles(
  8482. TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  8483. {
  8484. if (CanOperateOnFoundFiles(ATerminal))
  8485. {
  8486. for (int Index = 0; Index < FileList->Count; Index++)
  8487. {
  8488. UnicodeString FileName = FileList->Strings[Index];
  8489. TRemoteFile * File = static_cast<TRemoteFile *>(FileList->Objects[Index]);
  8490. ExecuteRemoteFile(FileName, File, efDefaultEditor);
  8491. OnFileOperationFinished(FileName, true);
  8492. }
  8493. }
  8494. }
  8495. //---------------------------------------------------------------------------
  8496. void __fastcall TCustomScpExplorerForm::RemoteFindFiles()
  8497. {
  8498. FFileFindTerminal = Terminal;
  8499. ShowFileFindDialog(
  8500. Terminal, RemoteDirView->Path, DoFindFiles, DoFocusRemotePath, DoDeleteFoundFiles,
  8501. DoDownloadFoundFiles, DoEditFoundFiles);
  8502. }
  8503. //---------------------------------------------------------------------------
  8504. void __fastcall TCustomScpExplorerForm::UpdateTaskbarList(ITaskbarList3 * TaskbarList)
  8505. {
  8506. FTaskbarList = TaskbarList;
  8507. }
  8508. //---------------------------------------------------------------------------
  8509. void __fastcall TCustomScpExplorerForm::SessionsPageControlMouseDown(
  8510. TObject * /*Sender*/, TMouseButton Button, TShiftState /*Shift*/, int X, int Y)
  8511. {
  8512. int Index = SessionsPageControl->IndexOfTabAt(X, Y);
  8513. if (Index >= 0)
  8514. {
  8515. if (Button == mbLeft)
  8516. {
  8517. // "Mouse down" is raised only after tab is switched.
  8518. // If switching tab (switching session) takes long enough for user
  8519. // to actually release the button, "mouse down" is still raised,
  8520. // but we do not get "mouse up" event, so dragging is not cancelled,
  8521. // prevent that by not beginning dragging in the first place.
  8522. if (IsKeyPressed(VK_LBUTTON))
  8523. {
  8524. // when user clicks the "+", we get mouse down only after the session
  8525. // is closed, when new session tab is already on X:Y, so dragging
  8526. // starts, prevent that
  8527. if (MilliSecondsBetween(Now(), FSessionsPageControlNewSessionTime) > 500)
  8528. {
  8529. TTerminal * Terminal = GetSessionTabTerminal(SessionsPageControl->Pages[Index]);
  8530. if (Terminal != NULL)
  8531. {
  8532. SessionsPageControl->BeginDrag(false);
  8533. }
  8534. }
  8535. }
  8536. }
  8537. else if (Button == mbMiddle)
  8538. {
  8539. // ignore middle-click for "New session tab"
  8540. TTerminal * Terminal = GetSessionTabTerminal(SessionsPageControl->Pages[Index]);
  8541. if (Terminal != NULL)
  8542. {
  8543. SessionsPageControl->ActivePageIndex = Index;
  8544. // Switch to session tab (has to be session tab, due to previous check)
  8545. if (DebugAlwaysTrue(SessionTabSwitched()))
  8546. {
  8547. CloseSession();
  8548. }
  8549. }
  8550. }
  8551. }
  8552. }
  8553. //---------------------------------------------------------------------------
  8554. void __fastcall TCustomScpExplorerForm::SessionsPageControlDragDrop(
  8555. TObject * /*Sender*/, TObject * /*Source*/, int X, int Y)
  8556. {
  8557. int Index = SessionsPageControl->IndexOfTabAt(X, Y);
  8558. // do not allow dropping on the "+" tab
  8559. TTerminal * TargetTerminal = GetSessionTabTerminal(SessionsPageControl->Pages[Index]);
  8560. if ((TargetTerminal != NULL) &&
  8561. (SessionsPageControl->ActivePage->PageIndex != Index))
  8562. {
  8563. Configuration->Usage->Inc(L"SessionTabMoves");
  8564. // this is almost redundant as we would recreate tabs in DoTerminalListChanged,
  8565. // but we want to actually prevent that to avoid flicker
  8566. SessionsPageControl->ActivePage->PageIndex = Index;
  8567. TTerminal * Terminal = GetSessionTabTerminal(SessionsPageControl->ActivePage);
  8568. TTerminalManager::Instance()->Move(Terminal, TargetTerminal);
  8569. }
  8570. }
  8571. //---------------------------------------------------------------------------
  8572. void __fastcall TCustomScpExplorerForm::SessionsPageControlDragOver(
  8573. TObject * Sender, TObject * Source, int X, int Y,
  8574. TDragState /*State*/, bool & Accept)
  8575. {
  8576. Accept = (Sender == Source);
  8577. if (Accept)
  8578. {
  8579. int Index = SessionsPageControl->IndexOfTabAt(X, Y);
  8580. TTerminal * Terminal = GetSessionTabTerminal(SessionsPageControl->Pages[Index]);
  8581. // do not allow dragging to the "+" tab
  8582. Accept = (Terminal != NULL);
  8583. }
  8584. }
  8585. //---------------------------------------------------------------------------
  8586. void __fastcall TCustomScpExplorerForm::SessionsDDDragOver(int /*KeyState*/,
  8587. const TPoint & Point, int & Effect)
  8588. {
  8589. int Index = SessionsPageControl->IndexOfTabAt(Point.X, Point.Y);
  8590. if (Index < 0)
  8591. {
  8592. Effect = DROPEFFECT_None;
  8593. }
  8594. else
  8595. {
  8596. TTerminal * TargetTerminal = GetSessionTabTerminal(SessionsPageControl->Pages[Index]);
  8597. // do not allow dropping on the "+" tab
  8598. if (TargetTerminal == NULL)
  8599. {
  8600. Effect = DROPEFFECT_None;
  8601. }
  8602. }
  8603. }
  8604. //---------------------------------------------------------------------------
  8605. void __fastcall TCustomScpExplorerForm::SessionsDDProcessDropped(
  8606. TObject * /*Sender*/, int /*KeyState*/, const TPoint & Point, int Effect)
  8607. {
  8608. int Index = SessionsPageControl->IndexOfTabAt(Point.X, Point.Y);
  8609. // do not allow dropping on the "+" tab
  8610. TTerminal * TargetTerminal = GetSessionTabTerminal(SessionsPageControl->Pages[Index]);
  8611. if (TargetTerminal != NULL)
  8612. {
  8613. DebugAssert(!IsFileControl(DropSourceControl, osRemote));
  8614. if (!IsFileControl(DropSourceControl, osRemote))
  8615. {
  8616. TTerminalManager::Instance()->ActiveTerminal = TargetTerminal;
  8617. RemoteFileControlDragDropFileOperation(SessionsPageControl, Effect,
  8618. // Why don't we use Terminal->CurrentDirectory directly?
  8619. TTerminalManager::Instance()->ActiveTerminal->CurrentDirectory, false);
  8620. }
  8621. }
  8622. }
  8623. //---------------------------------------------------------------------------
  8624. void __fastcall TCustomScpExplorerForm::QueueDDProcessDropped(
  8625. TObject * /*Sender*/, int /*KeyState*/, const TPoint & /*Point*/, int Effect)
  8626. {
  8627. // Downloads are handled in RemoteFileControlDDEnd
  8628. if (!IsFileControl(DropSourceControl, osRemote))
  8629. {
  8630. RemoteFileControlDragDropFileOperation(QueueView3, Effect,
  8631. Terminal->CurrentDirectory,
  8632. // force queue
  8633. true);
  8634. }
  8635. }
  8636. //---------------------------------------------------------------------------
  8637. void __fastcall TCustomScpExplorerForm::FormClose(TObject * /*Sender*/, TCloseAction & /*Action*/)
  8638. {
  8639. FShowing = false;
  8640. // Do not save empty workspace
  8641. if (WinConfiguration->AutoSaveWorkspace && (Terminal != NULL))
  8642. {
  8643. std::unique_ptr<TObjectList> DataList(DoCollectWorkspace());
  8644. UnicodeString Name = WorkspaceName();
  8645. DoSaveWorkspace(
  8646. Name, DataList.get(),
  8647. !Configuration->DisablePasswordStoring &&
  8648. WinConfiguration->AutoSaveWorkspacePasswords);
  8649. WinConfiguration->LastStoredSession = Name;
  8650. }
  8651. }
  8652. //---------------------------------------------------------------------------
  8653. void __fastcall TCustomScpExplorerForm::RemoteDirViewRead(TObject * /*Sender*/)
  8654. {
  8655. TManagedTerminal * ManagedTerminal =
  8656. dynamic_cast<TManagedTerminal *>(RemoteDirView->Terminal);
  8657. if (ManagedTerminal != NULL)
  8658. {
  8659. ManagedTerminal->DirectoryLoaded = Now();
  8660. }
  8661. }
  8662. //---------------------------------------------------------------------------
  8663. void __fastcall TCustomScpExplorerForm::DirViewSelectItem(TObject * Sender,
  8664. TListItem * /*Item*/, bool /*Selected*/)
  8665. {
  8666. TCustomDirView * DirView = reinterpret_cast<TCustomDirView *>(Sender);
  8667. switch (DirView->LastSelectMethod)
  8668. {
  8669. case smKeyboard:
  8670. Configuration->Usage->Inc(L"KeyboardSelections");
  8671. break;
  8672. case smMouse:
  8673. Configuration->Usage->Inc(L"MouseSelections");
  8674. break;
  8675. }
  8676. }
  8677. //---------------------------------------------------------------------------
  8678. int __fastcall TCustomScpExplorerForm::AddSessionColor(TColor Color)
  8679. {
  8680. if (Color != 0)
  8681. {
  8682. AddSessionColorImage(FSessionColors, Color, FSessionColorMaskImageIndex);
  8683. return FSessionColors->Count - 1;
  8684. }
  8685. else
  8686. {
  8687. return FSessionTabImageIndex;
  8688. }
  8689. }
  8690. //---------------------------------------------------------------------------
  8691. int __fastcall TCustomScpExplorerForm::AddFixedSessionImage(int GlyphsSourceIndex)
  8692. {
  8693. TPngImageCollectionItem * Item =
  8694. GlyphsModule->ExplorerImages->PngImages->Items[GlyphsSourceIndex];
  8695. FSessionColors->AddPng(Item->PngImage);
  8696. return FSessionColors->Count - 1;
  8697. }
  8698. //---------------------------------------------------------------------------
  8699. void __fastcall TCustomScpExplorerForm::AddFixedSessionImages()
  8700. {
  8701. FSessionColors->SetSize(GlyphsModule->ExplorerImages->Width, GlyphsModule->ExplorerImages->Height);
  8702. FNewSessionTabImageIndex = AddFixedSessionImage(NonVisualDataModule->NewSessionAction->ImageIndex);
  8703. FSessionTabImageIndex = AddFixedSessionImage(SiteImageIndex);
  8704. FSessionColorMaskImageIndex = AddFixedSessionImage(SiteColorMaskImageIndex);
  8705. }
  8706. //---------------------------------------------------------------------------
  8707. void __fastcall TCustomScpExplorerForm::CollectItemsWithTextDisplayMode(TWinControl * Control)
  8708. {
  8709. for (int Index = 0; Index < Control->ControlCount; Index++)
  8710. {
  8711. TControl * AControl = Control->Controls[Index];
  8712. TWinControl * WinControl = dynamic_cast<TWinControl *>(AControl);
  8713. if (WinControl != NULL)
  8714. {
  8715. CollectItemsWithTextDisplayMode(WinControl);
  8716. }
  8717. TTBCustomToolbar * Toolbar = dynamic_cast<TTBCustomToolbar *>(AControl);
  8718. if (Toolbar != NULL)
  8719. {
  8720. // we care for top-level items only
  8721. TTBCustomItem * Items = Toolbar->Items;
  8722. for (int ItemIndex = 0; ItemIndex < Items->Count; ItemIndex++)
  8723. {
  8724. TTBCustomItem * Item = Items->Items[ItemIndex];
  8725. if (((Item->DisplayMode == nbdmImageAndText) ||
  8726. (dynamic_cast<TTBXLabelItem *>(Item) != NULL)) &&
  8727. EligibleForImageDisplayMode(Item))
  8728. {
  8729. FItemsWithTextDisplayMode.insert(Item);
  8730. }
  8731. }
  8732. }
  8733. }
  8734. }
  8735. //---------------------------------------------------------------------------
  8736. bool __fastcall TCustomScpExplorerForm::EligibleForImageDisplayMode(TTBCustomItem * /*Item*/)
  8737. {
  8738. return true;
  8739. }
  8740. //---------------------------------------------------------------------------
  8741. bool __fastcall TCustomScpExplorerForm::UpdateToolbarDisplayMode()
  8742. {
  8743. bool SelectiveToolbarText = WinConfiguration->SelectiveToolbarText;
  8744. TTBItemDisplayMode DisplayMode = (SelectiveToolbarText ? nbdmImageAndText : nbdmDefault);
  8745. typedef std::set<TTBCustomItem *> TItemsWithTextDisplayMode;
  8746. TItemsWithTextDisplayMode::iterator i = FItemsWithTextDisplayMode.begin();
  8747. bool Result = true;
  8748. while (Result && (i != FItemsWithTextDisplayMode.end()))
  8749. {
  8750. TTBCustomItem * Item = *i;
  8751. TTBXLabelItem * Label = dynamic_cast<TTBXLabelItem *>(Item);
  8752. if (Label != NULL)
  8753. {
  8754. // optimization
  8755. if (Label->Visible == SelectiveToolbarText)
  8756. {
  8757. Result = false;
  8758. }
  8759. else
  8760. {
  8761. Label->Visible = SelectiveToolbarText;
  8762. }
  8763. }
  8764. else
  8765. {
  8766. // optimization
  8767. if (Item->DisplayMode == DisplayMode)
  8768. {
  8769. Result = false;
  8770. }
  8771. else
  8772. {
  8773. Item->DisplayMode = DisplayMode;
  8774. }
  8775. }
  8776. ++i;
  8777. }
  8778. if (Result)
  8779. {
  8780. UpdateNewSessionTab();
  8781. }
  8782. return Result;
  8783. }
  8784. //---------------------------------------------------------------------------
  8785. void __fastcall TCustomScpExplorerForm::DisplaySystemContextMenu()
  8786. {
  8787. DebugFail();
  8788. }
  8789. //---------------------------------------------------------------------------
  8790. bool __fastcall TCustomScpExplorerForm::IsBusy()
  8791. {
  8792. // Among other, a lock level is non zero, while directory is being loaded,
  8793. // even when it happens as a result of dirview navigation,
  8794. // i.e. when TNonVisualDataModule is NOT FBusy.
  8795. // That's why the TNonVisualDataModule::GetBusy calls this method.
  8796. // Among other this prevents a panel auto update to occur while
  8797. // directory is changing.
  8798. return (FLockLevel > 0) || DirView(osCurrent)->IsEditing();
  8799. }
  8800. //---------------------------------------------------------------------------
  8801. Boolean __fastcall TCustomScpExplorerForm::AllowedAction(TAction * /*Action*/, TActionAllowed Allowed)
  8802. {
  8803. // While the window is disabled, we still seem to process menu shortcuts at least,
  8804. // so stop it at least here.
  8805. // See also TCustomScpExplorerForm::RemoteDirViewBusy
  8806. return
  8807. (Allowed == aaUpdate) ||
  8808. !NonVisualDataModule->Busy;
  8809. }
  8810. //---------------------------------------------------------------------------
  8811. void __fastcall TCustomScpExplorerForm::EditMenuItemPopup(TTBCustomItem * Sender, bool FromLink)
  8812. {
  8813. NonVisualDataModule->EditMenuItemPopup(Sender, FromLink);
  8814. }
  8815. //---------------------------------------------------------------------------
  8816. void __fastcall TCustomScpExplorerForm::DirViewBusy(TObject * /*Sender*/, int Busy, bool & State)
  8817. {
  8818. // This is somewhat redundant to LockWindow() call from
  8819. // TTerminalManager::TerminalReadDirectoryProgress.
  8820. // But disabling window is known not to block keyboard shorcuts
  8821. // (see TCustomScpExplorerForm::AllowedAction), this hopefully works.
  8822. if (Busy > 0)
  8823. {
  8824. if (NonVisualDataModule->Busy)
  8825. {
  8826. State = false;
  8827. }
  8828. else
  8829. {
  8830. NonVisualDataModule->StartBusy();
  8831. // Will be countered for TCustomDirView.DoExecute in DoDirViewExecFile
  8832. LockWindow();
  8833. }
  8834. }
  8835. else if (Busy < 0)
  8836. {
  8837. UnlockWindow();
  8838. NonVisualDataModule->EndBusy();
  8839. }
  8840. else
  8841. {
  8842. State = NonVisualDataModule->Busy;
  8843. }
  8844. }
  8845. //---------------------------------------------------------------------------
  8846. void __fastcall TCustomScpExplorerForm::SessionsPageControlContextPopup(TObject * /*Sender*/, TPoint & MousePos, bool & Handled)
  8847. {
  8848. int Index = SessionsPageControl->IndexOfTabAt(MousePos.X, MousePos.Y);
  8849. // no context menu for "New session tab"
  8850. if ((Index >= 0) && (GetSessionTabTerminal(SessionsPageControl->Pages[Index]) != NULL))
  8851. {
  8852. SessionsPageControl->ActivePageIndex = Index;
  8853. if (DebugAlwaysTrue(SessionTabSwitched()))
  8854. {
  8855. // copied from TControl.WMContextMenu
  8856. SendCancelMode(SessionsPageControl);
  8857. // explicit popup instead of using PopupMenu property
  8858. // to avoid menu to popup somewhere within SessionTabSwitched above,
  8859. // while connecting yet not-connected session and hence
  8860. // allowing an access to commands over not-completelly connected session
  8861. TPoint Point = SessionsPageControl->ClientToScreen(MousePos);
  8862. TPopupMenu * PopupMenu = NonVisualDataModule->SessionsPopup;
  8863. PopupMenu->PopupComponent = SessionsPageControl;
  8864. PopupMenu->Popup(Point.x, Point.y);
  8865. }
  8866. }
  8867. Handled = true;
  8868. }
  8869. //---------------------------------------------------------------------------
  8870. bool __fastcall TCustomScpExplorerForm::CanChangePassword()
  8871. {
  8872. return (Terminal != NULL) && Terminal->Active && Terminal->IsCapable[fcChangePassword];
  8873. }
  8874. //---------------------------------------------------------------------------
  8875. void __fastcall TCustomScpExplorerForm::ChangePassword()
  8876. {
  8877. Configuration->Usage->Inc(L"PasswordChanges");
  8878. std::unique_ptr<TSessionData> ChangePasswordData(Terminal->SessionData->Clone());
  8879. ChangePasswordData->DisableAuthentationsExceptPassword();
  8880. ChangePasswordData->ChangePassword = true;
  8881. std::unique_ptr<TTerminal> ChangePasswordSession(
  8882. Terminal->CreateSecondarySession(L"ChangePassword", ChangePasswordData.get()));
  8883. ChangePasswordSession->AutoReadDirectory = false;
  8884. if (TTerminalManager::Instance()->ConnectTerminal(ChangePasswordSession.get()))
  8885. {
  8886. MessageDialog(MainInstructions(LoadStr(PASSWORD_CHANGED)), qtInformation, qaOK, HELP_CHANGE_PASSWORD);
  8887. }
  8888. else
  8889. {
  8890. Configuration->Usage->Inc(L"PasswordChangesFailed");
  8891. }
  8892. }
  8893. //---------------------------------------------------------------------------
  8894. void __fastcall TCustomScpExplorerForm::ChangeScale(int M, int D)
  8895. {
  8896. TForm::ChangeScale(M, D);
  8897. int APixelsPerInch = GetControlPixelsPerInch(this);
  8898. HANDLE ResourceModule = GUIConfiguration->ChangeToDefaultResourceModule();
  8899. try
  8900. {
  8901. GlyphsModule->SetPixelsPerInch(APixelsPerInch);
  8902. }
  8903. __finally
  8904. {
  8905. GUIConfiguration->ChangeResourceModule(ResourceModule);
  8906. }
  8907. }
  8908. //---------------------------------------------------------------------------
  8909. void __fastcall TCustomScpExplorerForm::DockContextPopup(TObject * Sender, TPoint & MousePos, bool & /*Handled*/)
  8910. {
  8911. NonVisualDataModule->ControlContextPopup(Sender, MousePos);
  8912. }
  8913. //---------------------------------------------------------------------
  8914. class TPublicControl : public TControl
  8915. {
  8916. friend class TCustomScpExplorerForm;
  8917. };
  8918. //---------------------------------------------------------------------------
  8919. void __fastcall TCustomScpExplorerForm::CopyPopup(TControl * DestControl, TControl * SourceControl)
  8920. {
  8921. static_cast<TPublicControl *>(DestControl)->PopupMenu = static_cast<TPublicControl *>(SourceControl)->PopupMenu;
  8922. static_cast<TPublicControl *>(DestControl)->OnContextPopup = static_cast<TPublicControl *>(SourceControl)->OnContextPopup;
  8923. }
  8924. //---------------------------------------------------------------------------