CustomScpExplorer.cpp 298 KB

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