CustomScpExplorer.cpp 299 KB

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