1
0

CustomScpExplorer.cpp 318 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810
  1. //---------------------------------------------------------------------------
  2. #define NO_WIN32_LEAN_AND_MEAN
  3. #include <vcl.h>
  4. #pragma hdrstop
  5. #include <Common.h>
  6. #include "CustomScpExplorer.h"
  7. #include <Bookmarks.h>
  8. #include <Interface.h>
  9. #include <Exceptions.h>
  10. #include <CoreMain.h>
  11. #include <FileSystems.h>
  12. #include <TextsCore.h>
  13. #include <TextsWin.h>
  14. #include <HelpWin.h>
  15. #include <VCLCommon.h>
  16. #include <Progress.h>
  17. #include <SynchronizeProgress.h>
  18. #include <DragExt.h>
  19. #include <WinApi.h>
  20. #include "GUITools.h"
  21. #include "NonVisual.h"
  22. #include "Glyphs.h"
  23. #include "Tools.h"
  24. #include "WinConfiguration.h"
  25. #include "TerminalManager.h"
  26. #include "EditorManager.h"
  27. #include "ProgParams.h"
  28. #include "Setup.h"
  29. #include <Consts.hpp>
  30. #include <DateUtils.hpp>
  31. #include <TB2Common.hpp>
  32. #include <DirectoryMonitor.hpp>
  33. //---------------------------------------------------------------------------
  34. #pragma package(smart_init)
  35. #pragma link "CustomDirView"
  36. #pragma link "CustomUnixDirView"
  37. #pragma link "IEListView"
  38. #pragma link "NortonLikeListView"
  39. #pragma link "UnixDirView"
  40. #pragma link "CustomDriveView"
  41. #pragma link "UnixDriveView"
  42. #pragma link "CustomDriveView"
  43. #pragma link "TB2Dock"
  44. #pragma link "TBXStatusBars"
  45. #pragma link "TB2Item"
  46. #pragma link "TB2Toolbar"
  47. #pragma link "ThemePageControl"
  48. #pragma link "PathLabel"
  49. #ifndef NO_RESOURCES
  50. #pragma resource "*.dfm"
  51. #endif
  52. //---------------------------------------------------------------------------
  53. #define SAVE_SELECTION(DIRVIEW) \
  54. UnicodeString FocusFile = L""; \
  55. UnicodeString LastFocusedFile = L""; \
  56. if (DIRVIEW->ItemFocused) LastFocusedFile = DIRVIEW->ItemFocused->Caption; \
  57. { TListItem * ClosestUnselected = DIRVIEW->ClosestUnselected(DIRVIEW->ItemFocused); \
  58. if (ClosestUnselected) FocusFile = ClosestUnselected->Caption; }
  59. #define RESTORE_SELECTION(DIRVIEW) \
  60. if (!LastFocusedFile.IsEmpty() && \
  61. (!DIRVIEW->ItemFocused || (DIRVIEW->ItemFocused->Caption != LastFocusedFile))) \
  62. { \
  63. TListItem *ItemToSelect = DIRVIEW->FindFileItem(FocusFile); \
  64. if (ItemToSelect) \
  65. { \
  66. DIRVIEW->ItemFocused = ItemToSelect; \
  67. DIRVIEW->ItemFocused->MakeVisible(False); \
  68. } \
  69. }
  70. //---------------------------------------------------------------------------
  71. #define WM_COMPONENT_HIDE (WM_WINSCP_USER + 4)
  72. static const int SessionPanelCount = 4;
  73. //---------------------------------------------------------------------------
  74. class TMutexGuard
  75. {
  76. public:
  77. TMutexGuard(HANDLE AMutex, int Message = MUTEX_RELEASE_TIMEOUT,
  78. int Timeout = 5000)
  79. {
  80. FMutex = NULL;
  81. unsigned long WaitResult = WaitForSingleObject(AMutex, Timeout);
  82. if (WaitResult == WAIT_TIMEOUT)
  83. {
  84. throw Exception(LoadStr(MUTEX_RELEASE_TIMEOUT));
  85. }
  86. else
  87. {
  88. FMutex = AMutex;
  89. }
  90. }
  91. ~TMutexGuard()
  92. {
  93. if (FMutex != NULL)
  94. {
  95. ReleaseMutex(FMutex);
  96. }
  97. }
  98. private:
  99. HANDLE FMutex;
  100. };
  101. //---------------------------------------------------------------------------
  102. class TWindowLock
  103. {
  104. public:
  105. TWindowLock(TCustomScpExplorerForm * Form) :
  106. FForm(Form)
  107. {
  108. FForm->LockWindow();
  109. }
  110. ~TWindowLock()
  111. {
  112. FForm->UnlockWindow();
  113. }
  114. private:
  115. TCustomScpExplorerForm * FForm;
  116. };
  117. //---------------------------------------------------------------------------
  118. struct TTransferOperationParam
  119. {
  120. TTransferOperationParam();
  121. UnicodeString TargetDirectory;
  122. bool Temp;
  123. bool DragDrop;
  124. int Options;
  125. TAutoSwitch Queue;
  126. };
  127. //---------------------------------------------------------------------------
  128. TTransferOperationParam::TTransferOperationParam()
  129. {
  130. Temp = false;
  131. DragDrop = false;
  132. Options = 0;
  133. Queue = asAuto;
  134. }
  135. //---------------------------------------------------------------------------
  136. class TTransferPresetNoteData : public TObject
  137. {
  138. public:
  139. UnicodeString Message;
  140. };
  141. //---------------------------------------------------------------------------
  142. class TTerminalNoteData : public TObject
  143. {
  144. public:
  145. TTerminal * Terminal;
  146. };
  147. //---------------------------------------------------------------------------
  148. __fastcall TCustomScpExplorerForm::TCustomScpExplorerForm(TComponent* Owner):
  149. FFormRestored(false),
  150. TForm(Owner)
  151. {
  152. FCurrentSide = osRemote;
  153. FEverShown = false;
  154. FDocks = new TList();
  155. RestoreParams();
  156. ConfigurationChanged();
  157. RemoteDirView->Invalidate();
  158. DebugAssert(NonVisualDataModule && !NonVisualDataModule->ScpExplorer);
  159. NonVisualDataModule->ScpExplorer = this;
  160. FAutoOperation = false;
  161. FOnFileOperationFinished = NULL;
  162. FForceExecution = false;
  163. FIgnoreNextDialogChar = 0;
  164. FErrorList = NULL;
  165. FSynchronizeProgressForm = NULL;
  166. FProgressForm = NULL;
  167. FRefreshLocalDirectory = false;
  168. FRefreshRemoteDirectory = false;
  169. FDDMoveSlipped = false;
  170. FDDExtMapFile = NULL;
  171. // CreateMutexW keeps failing with ERROR_NOACCESS
  172. FDDExtMutex = CreateMutexA(NULL, false, AnsiString(DRAG_EXT_MUTEX).c_str());
  173. DebugAssert(FDDExtMutex != NULL);
  174. FDDTargetControl = NULL;
  175. FDelayedDeletionTimer = NULL;
  176. FDelayedDeletionList = new TStringList();
  177. FDDFileList = NULL;
  178. FPendingTempSpaceWarn = false;
  179. FCapturedLog = NULL;
  180. FDragDropOperation = false;
  181. memset(&FHistoryMenu, 0, sizeof(FHistoryMenu));
  182. FAllowTransferPresetAutoSelect = true;
  183. FCopyParamDefault = L"";
  184. FSynchronizeController = NULL;
  185. FPendingQueueActionItem = NULL;
  186. FLockLevel = 0;
  187. FLockSuspendLevel = 0;
  188. FDisabledOnLockSuspend = false;
  189. FAlternativeDelete = false;
  190. FTrayIcon = new ::TTrayIcon(0);
  191. FTrayIcon->OnClick = TrayIconClick;
  192. FMaxQueueLength = 0;
  193. FLastContextPopupScreenPoint = TPoint(-1, -1);
  194. FTransferResumeList = NULL;
  195. FMoveToQueue = false;
  196. FStandaloneEditing = false;
  197. FOnFeedSynchronizeError = NULL;
  198. FNeedSession = false;
  199. FDoNotIdleCurrentTerminal = 0;
  200. FEditorManager = new TEditorManager();
  201. FEditorManager->OnFileChange = ExecutedFileChanged;
  202. FEditorManager->OnFileReload = ExecutedFileReload;
  203. FEditorManager->OnFileEarlyClosed = ExecutedFileEarlyClosed;
  204. FEditorManager->OnFileUploadComplete = ExecutedFileUploadComplete;
  205. FLocalEditors = new TList();
  206. FQueueStatus = NULL;
  207. FQueueStatusSection = new TCriticalSection();
  208. FQueueStatusInvalidated = false;
  209. FQueueItemInvalidated = false;
  210. FQueueActedItem = NULL;
  211. FQueueController = new TQueueController(QueueView3);
  212. FUserActionTimer = new TTimer(this);
  213. FUserActionTimer->Enabled = false;
  214. FUserActionTimer->Interval = 10;
  215. FUserActionTimer->OnTimer = UserActionTimer;
  216. FNotes = new TStringList();
  217. FNoteTimer = new TTimer(this);
  218. FNoteTimer->Enabled = false;
  219. FNoteTimer->OnTimer = NoteTimer;
  220. FOnNoteClick = NULL;
  221. FOle32Library = LoadLibrary(L"Ole32.dll");
  222. FDragMoveCursor = FOle32Library != NULL ?
  223. LoadCursor(FOle32Library, MAKEINTRESOURCE(2)) : NULL;
  224. UseSystemSettings(this);
  225. TTBXStringList * TransferList = dynamic_cast<TTBXStringList*>(
  226. static_cast<TObject*>(GetComponent(fcTransferList)));
  227. DebugAssert(TransferList != NULL);
  228. FTransferListHoverIndex = -1;
  229. TransferList->OnChange = TransferListChange;
  230. TransferList->OnDrawItem = TransferListDrawItem;
  231. SetSubmenu(dynamic_cast<TTBXCustomItem *>(static_cast<TObject *>(GetComponent(fcColorMenu))));
  232. SetSubmenu(NonVisualDataModule->ColorMenuItem);
  233. UseDesktopFont(SessionsPageControl);
  234. UpdateSessionsPageControlHeight();
  235. UseDesktopFont(RemoteDirView);
  236. UseDesktopFont(RemoteDriveView);
  237. UseDesktopFont(QueueView3);
  238. UseDesktopFont(QueueLabel);
  239. UseDesktopFont(RemoteStatusBar);
  240. reinterpret_cast<TLabel*>(QueueSplitter)->OnDblClick = QueueSplitterDblClick;
  241. QueueSplitter->ShowHint = true;
  242. RemotePanelSplitter->ShowHint = true;
  243. UpdateImages();
  244. FCustomCommandMenu = CreateTBXPopupMenu(this);
  245. FCustomCommandLocalFileList = NULL;
  246. FCustomCommandRemoteFileList = NULL;
  247. FSessionColors = new TPngImageList(this);
  248. FSessionColors->ColorDepth = cd32Bit;
  249. AddFixedSessionImages();
  250. SessionsPageControl->Images = FSessionColors;
  251. QueueLabel->FocusControl = QueueView3;
  252. UpdateQueueLabel();
  253. FRemoteDirViewWasFocused = true;
  254. CreateHiddenWindow();
  255. StartUpdates();
  256. }
  257. //---------------------------------------------------------------------------
  258. __fastcall TCustomScpExplorerForm::~TCustomScpExplorerForm()
  259. {
  260. // this has to be one of the very first things to do
  261. StopUpdateThread();
  262. SessionsPageControl->Images = NULL;
  263. SAFE_DESTROY(FSessionColors);
  264. SAFE_DESTROY(FSessionsDragDropFilesEx);
  265. delete FCustomCommandLocalFileList;
  266. delete FCustomCommandRemoteFileList;
  267. delete FCustomCommandMenu;
  268. delete FTrayIcon;
  269. FTrayIcon = NULL;
  270. FEditorManager->CloseInternalEditors(ForceCloseInternalEditor);
  271. delete FEditorManager;
  272. ForceCloseLocalEditors();
  273. delete FLocalEditors;
  274. HideFileFindDialog();
  275. if (FDelayedDeletionTimer)
  276. {
  277. DoDelayedDeletion(NULL);
  278. SAFE_DESTROY(FDelayedDeletionTimer);
  279. }
  280. SAFE_DESTROY(FDelayedDeletionList);
  281. // sometimes we do not get DDEnd so the list is not released
  282. SAFE_DESTROY(FDDFileList);
  283. DebugAssert(FSynchronizeController == NULL);
  284. CloseHandle(FDDExtMutex);
  285. FDDExtMutex = NULL;
  286. FreeLibrary(FOle32Library);
  287. FOle32Library = NULL;
  288. FDragMoveCursor = NULL;
  289. DebugAssert(!FErrorList);
  290. if (FEverShown)
  291. {
  292. // when window is never shown (like when running command-line operation),
  293. // particularly window site is not restored correctly (BoundsRect value set
  294. // in RestoreForm gets lost during handle allocation), so we do not want
  295. // it to be stored
  296. StoreParams();
  297. }
  298. Terminal = NULL;
  299. Queue = NULL;
  300. DebugAssert(NonVisualDataModule && (NonVisualDataModule->ScpExplorer == this));
  301. NonVisualDataModule->ScpExplorer = NULL;
  302. delete FQueueController;
  303. FQueueController = NULL;
  304. delete FQueueStatusSection;
  305. FQueueStatusSection = NULL;
  306. delete FQueueStatus;
  307. FQueueStatus = NULL;
  308. delete FUserActionTimer;
  309. FUserActionTimer = NULL;
  310. delete FNoteTimer;
  311. delete FNotes;
  312. delete FNoteData;
  313. SAFE_DESTROY(FDocks);
  314. SAFE_DESTROY(FHistoryMenu[0][0]);
  315. SAFE_DESTROY(FHistoryMenu[0][1]);
  316. SAFE_DESTROY(FHistoryMenu[1][0]);
  317. SAFE_DESTROY(FHistoryMenu[1][1]);
  318. if (FHiddenWindow != NULL)
  319. {
  320. DestroyWindow(FHiddenWindow);
  321. FHiddenWindow = NULL;
  322. }
  323. }
  324. //---------------------------------------------------------------------------
  325. void __fastcall TCustomScpExplorerForm::RefreshPanel(const UnicodeString & Session, const UnicodeString & Path)
  326. {
  327. std::unique_ptr<TSessionData> Data;
  328. if (!Session.IsEmpty())
  329. {
  330. bool DefaultsOnly;
  331. Data.reset(StoredSessions->ParseUrl(Session, NULL, DefaultsOnly));
  332. }
  333. TTerminalManager * Manager = TTerminalManager::Instance();
  334. for (int Index = 0; Index < Manager->Count; Index++)
  335. {
  336. TTerminal * Terminal = Manager->Terminals[Index];
  337. if (Session.IsEmpty() ||
  338. Terminal->SessionData->IsSameSite(Data.get()))
  339. {
  340. if (Path.IsEmpty())
  341. {
  342. Terminal->ClearCaches();
  343. }
  344. else
  345. {
  346. Terminal->DirectoryModified(Path, true);
  347. }
  348. }
  349. }
  350. // We should flag a pending refresh for the background terminals or busy foreground terminals
  351. if ((Terminal != NULL) && Terminal->Active &&
  352. CanCommandLineFromAnotherInstance() &&
  353. (Session.IsEmpty() || Terminal->SessionData->IsSameSite(Data.get())) &&
  354. (Path.IsEmpty() || UnixIsChildPath(Path, Terminal->CurrentDirectory)))
  355. {
  356. Terminal->ReloadDirectory();
  357. }
  358. }
  359. //---------------------------------------------------------------------------
  360. void __fastcall TCustomScpExplorerForm::WMCopyData(TMessage & Message)
  361. {
  362. PCOPYDATASTRUCT CopyData = reinterpret_cast<PCOPYDATASTRUCT>(Message.LParam);
  363. size_t MessageSize = sizeof(TCopyDataMessage);
  364. bool Result = DebugAlwaysTrue(CopyData->cbData == MessageSize);
  365. if (Result)
  366. {
  367. const TCopyDataMessage & Message = *reinterpret_cast<const TCopyDataMessage *>(CopyData->lpData);
  368. Result = (Message.Version == TCopyDataMessage::Version1);
  369. if (Result)
  370. {
  371. switch (Message.Command)
  372. {
  373. case TCopyDataMessage::CommandCanCommandLine:
  374. Result = CanCommandLineFromAnotherInstance();
  375. break;
  376. case TCopyDataMessage::CommandCommandLine:
  377. {
  378. UnicodeString CommandLine(Message.CommandLine);
  379. Result = CommandLineFromAnotherInstance(CommandLine);
  380. }
  381. break;
  382. case TCopyDataMessage::RefreshPanel:
  383. RefreshPanel(Message.Refresh.Session, Message.Refresh.Path);
  384. break;
  385. case TCopyDataMessage::MainWindowCheck:
  386. Result = true;
  387. break;
  388. default:
  389. Result = false;
  390. break;
  391. }
  392. }
  393. }
  394. Message.Result = Result ? 1 : 0;
  395. }
  396. //---------------------------------------------------------------------------
  397. void __fastcall TCustomScpExplorerForm::CreateHiddenWindow()
  398. {
  399. WNDCLASS WindowClass = {0};
  400. WindowClass.lpfnWndProc = DefWindowProc;
  401. WindowClass.hInstance = HInstance;
  402. WindowClass.lpszClassName = HIDDEN_WINDOW_NAME;
  403. FHiddenWindow = NULL;
  404. if (RegisterClass(&WindowClass))
  405. {
  406. FHiddenWindow = CreateWindow(HIDDEN_WINDOW_NAME, L"",
  407. WS_POPUP, 0, 0, 0, 0, 0, 0, HInstance, NULL);
  408. }
  409. }
  410. //---------------------------------------------------------------------------
  411. bool __fastcall TCustomScpExplorerForm::CanConsole()
  412. {
  413. return (Terminal != NULL) && (Terminal->IsCapable[fcAnyCommand] || Terminal->IsCapable[fcSecondaryShell]);
  414. }
  415. //---------------------------------------------------------------------------
  416. bool __fastcall TCustomScpExplorerForm::CanCommandLineFromAnotherInstance()
  417. {
  418. bool Result = Visible && !NonVisualDataModule->Busy;
  419. return Result;
  420. }
  421. //---------------------------------------------------------------------------
  422. bool __fastcall TCustomScpExplorerForm::CommandLineFromAnotherInstance(
  423. const UnicodeString & CommandLine)
  424. {
  425. TProgramParams Params(CommandLine);
  426. bool Result = CanCommandLineFromAnotherInstance() && DebugAlwaysTrue(Params.ParamCount > 0);
  427. if (Result)
  428. {
  429. NonVisualDataModule->StartBusy();
  430. try
  431. {
  432. // this action is initiated from another process,
  433. // so it's likely that our window is not visible,
  434. // and user won't see what is going on
  435. Application->BringToFront();
  436. // reload sessions as we may be asked to open a session
  437. // just stored by another instance
  438. StoredSessions->Reload();
  439. UnicodeString SessionName = Params.Param[1];
  440. std::unique_ptr<TObjectList> DataList(new TObjectList());
  441. UnicodeString DownloadFile; // unused
  442. try
  443. {
  444. GetLoginData(SessionName, &Params, DataList.get(), DownloadFile, true, this);
  445. }
  446. catch (EAbort &)
  447. {
  448. // Happens when opening session in PuTTY and in other situations,
  449. // in which we do not want to fail WM_COPYDATA, as that would cause master instance
  450. // to process the command-line on its own (or to pass it to yet another instance)
  451. }
  452. if (DataList->Count > 0)
  453. {
  454. TTerminalManager * Manager = TTerminalManager::Instance();
  455. Manager->ActiveTerminal = Manager->NewTerminals(DataList.get());
  456. }
  457. }
  458. __finally
  459. {
  460. NonVisualDataModule->EndBusy();
  461. }
  462. }
  463. return Result;
  464. }
  465. //---------------------------------------------------------------------------
  466. void __fastcall TCustomScpExplorerForm::SetTerminal(TTerminal * value)
  467. {
  468. if (FTerminal != value)
  469. {
  470. TerminalChanging();
  471. FTerminal = value;
  472. bool PrevAllowTransferPresetAutoSelect = FAllowTransferPresetAutoSelect;
  473. FAllowTransferPresetAutoSelect = false;
  474. try
  475. {
  476. TerminalChanged();
  477. }
  478. __finally
  479. {
  480. FAllowTransferPresetAutoSelect = PrevAllowTransferPresetAutoSelect;
  481. }
  482. if (Terminal != NULL)
  483. {
  484. TransferPresetAutoSelect();
  485. }
  486. }
  487. }
  488. //---------------------------------------------------------------------------
  489. void __fastcall TCustomScpExplorerForm::TerminalChanging()
  490. {
  491. if (FTerminal != NULL)
  492. {
  493. UpdateTerminal(Terminal);
  494. }
  495. }
  496. //---------------------------------------------------------------------------
  497. void __fastcall TCustomScpExplorerForm::TerminalChanged()
  498. {
  499. RemoteDirView->Terminal = Terminal;
  500. NonVisualDataModule->ResetQueueOnceEmptyOperation();
  501. TManagedTerminal * ManagedTerminal = NULL;
  502. if (Terminal != NULL)
  503. {
  504. if (Terminal->Active)
  505. {
  506. Terminal->RefreshDirectory();
  507. }
  508. ManagedTerminal = dynamic_cast<TManagedTerminal *>(Terminal);
  509. DebugAssert(ManagedTerminal != NULL);
  510. if (WinConfiguration->PreservePanelState)
  511. {
  512. if (ManagedTerminal->RemoteExplorerState != NULL)
  513. {
  514. RemoteDirView->RestoreState(ManagedTerminal->RemoteExplorerState);
  515. }
  516. else
  517. {
  518. RemoteDirView->ClearState();
  519. }
  520. }
  521. InitStatusBar();
  522. }
  523. DoTerminalListChanged(false);
  524. if (ManagedTerminal != NULL)
  525. {
  526. // this has to be set only after the tab is switched from DoTerminalListChanged,
  527. // otherwise we are changing color of wrong tab
  528. SessionColor = (TColor)ManagedTerminal->StateData->Color;
  529. }
  530. UpdateTransferList();
  531. // Update panels Enable state before refreshing the labels
  532. UpdateControls();
  533. }
  534. //---------------------------------------------------------------------------
  535. void __fastcall TCustomScpExplorerForm::SetQueue(TTerminalQueue * value)
  536. {
  537. if (Queue != value)
  538. {
  539. if (FQueue != NULL)
  540. {
  541. FQueue->OnListUpdate = NULL;
  542. FQueue->OnQueueItemUpdate = NULL;
  543. }
  544. FQueue = value;
  545. if (FQueue != NULL)
  546. {
  547. DebugAssert(FQueue->OnListUpdate == NULL);
  548. FQueue->OnListUpdate = QueueListUpdate;
  549. DebugAssert(FQueue->OnQueueItemUpdate == NULL);
  550. FQueue->OnQueueItemUpdate = QueueItemUpdate;
  551. }
  552. QueueChanged();
  553. }
  554. }
  555. //---------------------------------------------------------------------------
  556. void __fastcall TCustomScpExplorerForm::QueueView3Deletion(TObject * /*Sender*/,
  557. TListItem * Item)
  558. {
  559. if (FQueueActedItem == Item)
  560. {
  561. FQueueActedItem = NULL;
  562. if ((QueueView3->PopupMenu != NULL) &&
  563. (QueueView3->PopupMenu->PopupComponent == QueueView3))
  564. {
  565. // rather "trick", suggested by Jordan on jrsoftware.toolbar2000
  566. ReleaseCapture();
  567. }
  568. }
  569. if (Item->Data == FPendingQueueActionItem)
  570. {
  571. FPendingQueueActionItem = NULL;
  572. }
  573. }
  574. //---------------------------------------------------------------------------
  575. void __fastcall TCustomScpExplorerForm::UpdateQueueStatus(bool QueueChanging)
  576. {
  577. {
  578. TGuard Guard(FQueueStatusSection);
  579. FQueueStatusInvalidated = false;
  580. if (FQueue != NULL)
  581. {
  582. FQueueStatus = FQueue->CreateStatus(FQueueStatus);
  583. }
  584. }
  585. if ((FQueueStatus != NULL) && (FQueueStatus->Count > FMaxQueueLength))
  586. {
  587. FMaxQueueLength = FQueueStatus->Count;
  588. Configuration->Usage->SetMax(L"MaxQueueLength", FMaxQueueLength);
  589. }
  590. FQueueController->UpdateQueueStatus(FQueueStatus);
  591. SetQueueProgress();
  592. UpdateQueueView();
  593. bool IsEmpty = (FQueueStatus == NULL) || (FQueueStatus->Count == 0);
  594. if (IsEmpty && (Terminal != NULL))
  595. {
  596. TOnceDoneOperation OnceDoneOperation =
  597. NonVisualDataModule->CurrentQueueOnceEmptyOperation();
  598. NonVisualDataModule->ResetQueueOnceEmptyOperation();
  599. if ((FQueue != NULL) && !WinConfiguration->EnableQueueByDefault && !QueueChanging)
  600. {
  601. FQueue->Enabled = false;
  602. }
  603. if ((OnceDoneOperation != odoIdle) && !NonVisualDataModule->Busy)
  604. {
  605. Terminal->CloseOnCompletion(OnceDoneOperation, LoadStr(CLOSED_ON_QUEUE_EMPTY));
  606. }
  607. }
  608. }
  609. //---------------------------------------------------------------------------
  610. UnicodeString __fastcall TCustomScpExplorerForm::GetQueueProgressTitle()
  611. {
  612. UnicodeString Result;
  613. if (FQueueStatus != NULL)
  614. {
  615. if (FQueueStatus->IsOnlyOneActiveAndNoPending())
  616. {
  617. TFileOperationProgressType * ProgressData =
  618. FQueueStatus->Items[FQueueStatus->DoneCount]->ProgressData;
  619. if ((ProgressData != NULL) && ProgressData->InProgress)
  620. {
  621. Result = TProgressForm::ProgressStr(NULL, ProgressData);
  622. }
  623. }
  624. else if (FQueueStatus->ActiveAndPendingPrimaryCount > 1)
  625. {
  626. Result = FMTLOAD(PROGRESS_IN_QUEUE, (FQueueStatus->ActiveAndPendingPrimaryCount));
  627. }
  628. }
  629. return Result;
  630. }
  631. //---------------------------------------------------------------------------
  632. void __fastcall TCustomScpExplorerForm::UpdateQueueView()
  633. {
  634. ComponentVisible[fcQueueView] =
  635. (WinConfiguration->QueueView.Show == qvShow) ||
  636. ((WinConfiguration->QueueView.Show == qvHideWhenEmpty) &&
  637. (FQueueStatus != NULL) && (FQueueStatus->Count > 0));
  638. }
  639. //---------------------------------------------------------------------------
  640. void __fastcall TCustomScpExplorerForm::QueueChanged()
  641. {
  642. if (FQueueStatus != NULL)
  643. {
  644. delete FQueueStatus;
  645. FQueueStatus = NULL;
  646. }
  647. UpdateQueueStatus(true);
  648. }
  649. //---------------------------------------------------------------------------
  650. void __fastcall TCustomScpExplorerForm::QueueListUpdate(TTerminalQueue * Queue)
  651. {
  652. if (FQueue == Queue)
  653. {
  654. FQueueStatusInvalidated = true;
  655. }
  656. }
  657. //---------------------------------------------------------------------------
  658. void __fastcall TCustomScpExplorerForm::QueueItemUpdate(TTerminalQueue * Queue,
  659. TQueueItem * Item)
  660. {
  661. if (FQueue == Queue)
  662. {
  663. TGuard Guard(FQueueStatusSection);
  664. // this may be running in parallel with QueueChanged
  665. if (FQueueStatus != NULL)
  666. {
  667. TQueueItemProxy * QueueItem = FQueueStatus->FindByQueueItem(Item);
  668. if ((Item->Status == TQueueItem::qsDone) && (Terminal != NULL))
  669. {
  670. FRefreshLocalDirectory = (QueueItem == NULL) ||
  671. (!QueueItem->Info->ModifiedLocal.IsEmpty());
  672. FRefreshRemoteDirectory = (QueueItem == NULL) ||
  673. (!QueueItem->Info->ModifiedRemote.IsEmpty());
  674. }
  675. if (QueueItem != NULL)
  676. {
  677. QueueItem->UserData = (void*)true;
  678. FQueueItemInvalidated = true;
  679. }
  680. }
  681. }
  682. }
  683. //---------------------------------------------------------------------------
  684. bool __fastcall TCustomScpExplorerForm::IsQueueAutoPopup()
  685. {
  686. // during standalone editing, we have no way to see/control queue,
  687. // so we have to always popup prompts automatically
  688. return FStandaloneEditing || GUIConfiguration->QueueAutoPopup;
  689. }
  690. //---------------------------------------------------------------------------
  691. void __fastcall TCustomScpExplorerForm::RefreshQueueItems()
  692. {
  693. if (FQueueStatus != NULL)
  694. {
  695. bool QueueAutoPopup = IsQueueAutoPopup();
  696. bool NeedRefresh = FQueueController->NeedRefresh();
  697. bool Refresh = FQueueItemInvalidated || NeedRefresh;
  698. FQueueItemInvalidated = false;
  699. int Limit = Refresh ? FQueueStatus->Count : FQueueStatus->DoneAndActiveCount;
  700. bool Updated = false;
  701. TQueueItemProxy * QueueItem;
  702. bool WasUserAction;
  703. for (int Index = 0; Index < Limit; Index++)
  704. {
  705. bool Update = false;
  706. QueueItem = FQueueStatus->Items[Index];
  707. WasUserAction = TQueueItem::IsUserActionStatus(QueueItem->Status);
  708. if (!NonVisualDataModule->Busy && QueueAutoPopup && WasUserAction &&
  709. (FPendingQueueActionItem == NULL))
  710. {
  711. FPendingQueueActionItem = QueueItem;
  712. FUserActionTimer->Enabled = true;
  713. }
  714. if ((bool)QueueItem->UserData)
  715. {
  716. QueueItem->UserData = (void*)false;
  717. QueueItem->Update();
  718. Updated = true;
  719. Update = true;
  720. }
  721. else if (FQueueController->QueueItemNeedsFrequentRefresh(QueueItem) || NeedRefresh)
  722. {
  723. Update = true;
  724. }
  725. if (Update)
  726. {
  727. FQueueController->RefreshQueueItem(QueueItem);
  728. }
  729. }
  730. if (Updated)
  731. {
  732. NonVisualDataModule->UpdateNonVisibleActions();
  733. SetQueueProgress();
  734. }
  735. }
  736. }
  737. //---------------------------------------------------------------------------
  738. void __fastcall TCustomScpExplorerForm::SetTaskbarListProgressState(TBPFLAG Flags)
  739. {
  740. // Could now use Application->MainFormHandle, now that we implement Application->OnGetMainFormHandle
  741. FTaskbarList->SetProgressState(GetMainForm()->Handle, Flags);
  742. }
  743. //---------------------------------------------------------------------------
  744. void __fastcall TCustomScpExplorerForm::SetTaskbarListProgressValue(TFileOperationProgressType * ProgressData)
  745. {
  746. if (ProgressData->Operation != foCalculateSize)
  747. {
  748. int OverallProgress;
  749. if (DebugAlwaysTrue(FProgressForm != NULL) && (FProgressForm->SynchronizeProgress != NULL))
  750. {
  751. OverallProgress = FProgressForm->SynchronizeProgress->Progress(ProgressData);
  752. }
  753. else
  754. {
  755. OverallProgress = ProgressData->OverallProgress();
  756. }
  757. // implies TBPF_NORMAL
  758. FTaskbarList->SetProgressValue(GetMainForm()->Handle, OverallProgress, 100);
  759. }
  760. else
  761. {
  762. SetTaskbarListProgressState(TBPF_INDETERMINATE);
  763. }
  764. }
  765. //---------------------------------------------------------------------------
  766. void __fastcall TCustomScpExplorerForm::SetQueueProgress()
  767. {
  768. TTerminalManager::Instance()->QueueStatusUpdated();
  769. if ((FTaskbarList != NULL) && (FProgressForm == NULL))
  770. {
  771. if ((FQueueStatus != NULL) && (FQueueStatus->ActiveAndPendingPrimaryCount > 0))
  772. {
  773. if (FQueueStatus->IsOnlyOneActiveAndNoPending())
  774. {
  775. TFileOperationProgressType * ProgressData = NULL;
  776. if (FQueueStatus->Items[FQueueStatus->DoneCount] != NULL)
  777. {
  778. ProgressData = FQueueStatus->Items[FQueueStatus->DoneCount]->ProgressData;
  779. }
  780. if ((ProgressData != NULL) &&
  781. ProgressData->InProgress)
  782. {
  783. SetTaskbarListProgressValue(ProgressData);
  784. }
  785. else
  786. {
  787. SetTaskbarListProgressState(TBPF_NOPROGRESS);
  788. }
  789. }
  790. else
  791. {
  792. SetTaskbarListProgressState(TBPF_INDETERMINATE);
  793. }
  794. }
  795. else
  796. {
  797. SetTaskbarListProgressState(TBPF_NOPROGRESS);
  798. }
  799. }
  800. UpdateQueueLabel();
  801. }
  802. //---------------------------------------------------------------------------
  803. void __fastcall TCustomScpExplorerForm::UpdateQueueLabel()
  804. {
  805. UnicodeString Caption = LoadStr(QUEUE_CAPTION);
  806. if (FQueueStatus != NULL)
  807. {
  808. int ActiveAndPendingPrimaryCount = FQueueStatus->ActiveAndPendingPrimaryCount;
  809. if (ActiveAndPendingPrimaryCount > 0)
  810. {
  811. Caption = FORMAT("%s (%d)", (Caption, ActiveAndPendingPrimaryCount));
  812. }
  813. }
  814. QueueLabel->Caption = Caption;
  815. }
  816. //---------------------------------------------------------------------------
  817. void __fastcall TCustomScpExplorerForm::UpdateTransferList()
  818. {
  819. TTBXStringList * TransferList = dynamic_cast<TTBXStringList*>(
  820. static_cast<TComponent*>(GetComponent(fcTransferList)));
  821. TTBXDropDownItem * TransferDropDown = dynamic_cast<TTBXDropDownItem*>(
  822. static_cast<TComponent*>(GetComponent(fcTransferDropDown)));
  823. TransferList->Strings->BeginUpdate();
  824. try
  825. {
  826. TransferList->Strings->Assign(GUIConfiguration->CopyParamList->NameList);
  827. TransferList->Strings->Insert(0, StripHotkey(LoadStr(COPY_PARAM_DEFAULT)));
  828. TransferList->ItemIndex = GUIConfiguration->CopyParamIndex + 1;
  829. if (FTransferDropDownHint.IsEmpty())
  830. {
  831. FTransferDropDownHint = TransferDropDown->Hint;
  832. }
  833. // this way we get name for "default" settings (COPY_PARAM_DEFAULT)
  834. UnicodeString Name = TransferList->Strings->Strings[TransferList->ItemIndex];
  835. TransferDropDown->Text = StripHotkey(Name);
  836. TransferDropDown->Hint = FORMAT(L"%s|%s:\n%s",
  837. (FTransferDropDownHint, StripHotkey(Name),
  838. GUIConfiguration->CurrentCopyParam.GetInfoStr(L"; ",
  839. FLAGMASK(Terminal != NULL, Terminal->UsableCopyParamAttrs(0).General))));
  840. // update the label, otherwise when it is updated only on the first draw
  841. // of the list, it is drawn "bold" for some reason
  842. FTransferListHoverIndex = TransferList->ItemIndex;
  843. UpdateTransferLabel();
  844. }
  845. __finally
  846. {
  847. TransferList->Strings->EndUpdate();
  848. }
  849. }
  850. //---------------------------------------------------------------------------
  851. void __fastcall TCustomScpExplorerForm::UpdateCustomCommandsToolbar()
  852. {
  853. TTBXToolbar * Toolbar = dynamic_cast<TTBXToolbar *>(GetComponent(fcCustomCommandsBand));
  854. DebugAssert(Toolbar != NULL);
  855. NonVisualDataModule->UpdateCustomCommandsToolbar(Toolbar);
  856. }
  857. //---------------------------------------------------------------------------
  858. void __fastcall TCustomScpExplorerForm::UpdateActions()
  859. {
  860. TForm::UpdateActions();
  861. if (ComponentVisible[fcCustomCommandsBand])
  862. {
  863. UpdateCustomCommandsToolbar();
  864. }
  865. }
  866. //---------------------------------------------------------------------------
  867. void __fastcall TCustomScpExplorerForm::UpdateSessionsPageControlHeight()
  868. {
  869. SessionsPageControl->Height = SessionsPageControl->GetTabsHeight();
  870. }
  871. //---------------------------------------------------------------------------
  872. void __fastcall TCustomScpExplorerForm::ConfigurationChanged()
  873. {
  874. DebugAssert(Configuration && RemoteDirView);
  875. RemoteDirView->DDAllowMove = WinConfiguration->DDAllowMoveInit;
  876. RemoteDirView->DimmHiddenFiles = WinConfiguration->DimmHiddenFiles;
  877. RemoteDirView->ShowHiddenFiles = WinConfiguration->ShowHiddenFiles;
  878. RemoteDirView->FormatSizeBytes = WinConfiguration->FormatSizeBytes;
  879. RemoteDirView->ShowInaccesibleDirectories = WinConfiguration->ShowInaccesibleDirectories;
  880. RemoteDirView->NaturalOrderNumericalSorting = WinConfiguration->NaturalOrderNumericalSorting;
  881. if (RemoteDirView->RowSelect != WinConfiguration->FullRowSelect)
  882. {
  883. RemoteDirView->RowSelect = WinConfiguration->FullRowSelect;
  884. // selection is not redrawn automatically when RowSelect changes
  885. RemoteDirView->Invalidate();
  886. }
  887. RemoteDriveView->DDAllowMove = WinConfiguration->DDAllowMoveInit;
  888. RemoteDriveView->DimmHiddenDirs = WinConfiguration->DimmHiddenFiles;
  889. RemoteDriveView->ShowHiddenDirs = WinConfiguration->ShowHiddenFiles;
  890. RemoteDriveView->ShowInaccesibleDirectories = WinConfiguration->ShowInaccesibleDirectories;
  891. UpdateSessionsPageControlHeight();
  892. SetDockAllowDrag(!WinConfiguration->LockToolbars);
  893. UpdateToolbarDisplayMode();
  894. UpdateTransferList();
  895. if (Terminal != NULL)
  896. {
  897. TransferPresetAutoSelect();
  898. }
  899. UpdateQueueView();
  900. UpdateControls();
  901. // this can be called even before constuctor finishes.
  902. if (FEditorManager != NULL)
  903. {
  904. FEditorManager->ProcessFiles(FileConfigurationChanged, NULL);
  905. }
  906. // this can be called even before constuctor finishes.
  907. if (FLocalEditors != NULL)
  908. {
  909. for (int Index = 0; Index < FLocalEditors->Count; Index++)
  910. {
  911. ReconfigureEditorForm(static_cast<TForm *>(FLocalEditors->Items[Index]));
  912. }
  913. }
  914. if (ComponentVisible[fcCustomCommandsBand])
  915. {
  916. UpdateCustomCommandsToolbar();
  917. }
  918. // show only when keeping queue items forever,
  919. // otherwise, its enough to have in in the context menu
  920. QueueDeleteAllDoneQueueToolbarItem->Visible =
  921. WinConfiguration->QueueKeepDoneItems && (WinConfiguration->QueueKeepDoneItemsFor < 0);
  922. }
  923. //---------------------------------------------------------------------------
  924. void __fastcall TCustomScpExplorerForm::FileConfigurationChanged(
  925. const UnicodeString FileName, TEditedFileData * /*Data*/, TObject * Token,
  926. void * /*Arg*/)
  927. {
  928. if (Token != NULL)
  929. {
  930. TForm * Editor = dynamic_cast<TForm*>(Token);
  931. DebugAssert(Editor != NULL);
  932. ReconfigureEditorForm(Editor);
  933. }
  934. }
  935. //---------------------------------------------------------------------------
  936. void __fastcall TCustomScpExplorerForm::EnableDDTransferConfirmation(TObject * /*Sender*/)
  937. {
  938. WinConfiguration->DDTransferConfirmation = asOn;
  939. }
  940. //---------------------------------------------------------------------------
  941. void __fastcall TCustomScpExplorerForm::CopyParamDialogAfter(
  942. TTransferDirection /*Direction*/, bool /*Temp*/, const UnicodeString & /*TargetDirectory*/)
  943. {
  944. // noop
  945. }
  946. //---------------------------------------------------------------------------
  947. bool __fastcall TCustomScpExplorerForm::CopyParamDialog(
  948. TTransferDirection Direction, TTransferType Type, bool Temp,
  949. TStrings * FileList, UnicodeString & TargetDirectory, TGUICopyParamType & CopyParam,
  950. bool Confirm, bool DragDrop, int Options)
  951. {
  952. bool Result = true;
  953. DebugAssert(Terminal && Terminal->Active);
  954. // Temp means d&d here so far, may change in future!
  955. if (Temp && (Direction == tdToLocal) && (Type == ttMove) &&
  956. !WinConfiguration->DDAllowMove)
  957. {
  958. TMessageParams Params(mpNeverAskAgainCheck);
  959. unsigned int Answer = MessageDialog(LoadStr(DND_DOWNLOAD_MOVE_WARNING), qtWarning,
  960. qaOK | qaCancel, HELP_DND_DOWNLOAD_MOVE_WARNING, &Params);
  961. if (Answer == qaNeverAskAgain)
  962. {
  963. WinConfiguration->DDAllowMove = true;
  964. }
  965. else if (Answer == qaCancel)
  966. {
  967. Result = false;
  968. }
  969. }
  970. // these parameters are known in advance
  971. int Params =
  972. FLAGMASK(Type == ttMove, cpDelete);
  973. bool ToTemp = (Temp && (Direction == tdToLocal));
  974. if (Result && Confirm && WinConfiguration->ConfirmTransferring)
  975. {
  976. bool DisableNewerOnly =
  977. (!Terminal->IsCapable[fcNewerOnlyUpload] && (Direction == tdToRemote)) ||
  978. ToTemp;
  979. Options |=
  980. FLAGMASK(ToTemp, coTemp) |
  981. FLAGMASK(!Terminal->IsCapable[fsBackgroundTransfers], coDisableQueue) |
  982. coDoNotShowAgain;
  983. TUsableCopyParamAttrs UsableCopyParamAttrs = Terminal->UsableCopyParamAttrs(Params);
  984. int CopyParamAttrs = (Direction == tdToRemote ?
  985. UsableCopyParamAttrs.Upload : UsableCopyParamAttrs.Download) |
  986. FLAGMASK(DisableNewerOnly, cpaNoNewerOnly);
  987. int OutputOptions =
  988. FLAGMASK(DragDrop && (WinConfiguration->DDTransferConfirmation == asAuto),
  989. cooDoNotShowAgain);
  990. std::unique_ptr<TSessionData> SessionData(SessionDataForCode());
  991. Result = DoCopyDialog(Direction == tdToRemote, Type == ttMove,
  992. FileList, TargetDirectory, &CopyParam, Options, CopyParamAttrs, SessionData.get(), &OutputOptions);
  993. if (Result)
  994. {
  995. if (FLAGSET(OutputOptions, cooDoNotShowAgain))
  996. {
  997. if (DragDrop)
  998. {
  999. if (WinConfiguration->DDTransferConfirmation == asAuto)
  1000. {
  1001. PopupTrayBalloon(NULL, LoadStr(DD_TRANSFER_CONFIRM_OFF2), qtInformation,
  1002. NULL, 0, EnableDDTransferConfirmation, NULL);
  1003. }
  1004. WinConfiguration->DDTransferConfirmation = asOff;
  1005. }
  1006. else
  1007. {
  1008. WinConfiguration->ConfirmTransferring = false;
  1009. }
  1010. }
  1011. else
  1012. {
  1013. // User exclicitly unchecked "do not show again",
  1014. // so show him the dialog the next time
  1015. if (DragDrop && (WinConfiguration->DDTransferConfirmation == asAuto))
  1016. {
  1017. WinConfiguration->DDTransferConfirmation = asOn;
  1018. }
  1019. }
  1020. CopyParamDialogAfter(Direction, Temp, TargetDirectory);
  1021. }
  1022. }
  1023. if (Result && CopyParam.Queue && !ToTemp && Terminal->IsCapable[fsBackgroundTransfers])
  1024. {
  1025. Configuration->Usage->Inc(L"TransfersOnBackground");
  1026. // these parameter are known only after transfer dialog
  1027. Params |=
  1028. (CopyParam.QueueNoConfirmation ? cpNoConfirmation : 0);
  1029. AddQueueItem(Queue, Direction, FileList, TargetDirectory, CopyParam, Params);
  1030. Result = false;
  1031. ClearTransferSourceSelection(Direction);
  1032. }
  1033. return Result;
  1034. }
  1035. //---------------------------------------------------------------------------
  1036. void __fastcall TCustomScpExplorerForm::ClearTransferSourceSelection(TTransferDirection Direction)
  1037. {
  1038. if (FOnFileOperationFinished != NULL)
  1039. {
  1040. FOnFileOperationFinished(UnicodeString(), true);
  1041. }
  1042. else
  1043. {
  1044. TOperationSide Side = ((Direction == tdToRemote) ? osLocal : osRemote);
  1045. if (HasDirView[Side])
  1046. {
  1047. DirView(Side)->SelectAll(smNone);
  1048. }
  1049. }
  1050. }
  1051. //---------------------------------------------------------------------------
  1052. void __fastcall TCustomScpExplorerForm::AddQueueItem(
  1053. TTerminalQueue * Queue, TTransferDirection Direction, TStrings * FileList,
  1054. const UnicodeString TargetDirectory, const TGUICopyParamType & CopyParam,
  1055. int Params)
  1056. {
  1057. DebugAssert(Queue != NULL);
  1058. bool SingleFile = false;
  1059. if (FileList->Count == 1)
  1060. {
  1061. if (Direction == tdToRemote)
  1062. {
  1063. UnicodeString FileName = FileList->Strings[0];
  1064. SingleFile = FileExists(ApiPath(FileName));
  1065. }
  1066. else
  1067. {
  1068. TRemoteFile * File = static_cast<TRemoteFile *>(FileList->Objects[0]);
  1069. SingleFile = !File->IsDirectory;
  1070. }
  1071. }
  1072. TQueueItem * QueueItem;
  1073. if (Direction == tdToRemote)
  1074. {
  1075. QueueItem = new TUploadQueueItem(Terminal, FileList, TargetDirectory,
  1076. &CopyParam, Params, SingleFile, CopyParam.QueueParallel);
  1077. }
  1078. else
  1079. {
  1080. QueueItem = new TDownloadQueueItem(Terminal, FileList, TargetDirectory,
  1081. &CopyParam, Params, SingleFile, CopyParam.QueueParallel);
  1082. }
  1083. AddQueueItem(Queue, QueueItem, Terminal);
  1084. }
  1085. //---------------------------------------------------------------------------
  1086. void __fastcall TCustomScpExplorerForm::AddQueueItem(TTerminalQueue * Queue, TQueueItem * QueueItem, TTerminal * Terminal)
  1087. {
  1088. if (Queue->IsEmpty)
  1089. {
  1090. TManagedTerminal * ManagedTerminal = DebugNotNull(dynamic_cast<TManagedTerminal *>(Terminal));
  1091. ManagedTerminal->QueueOperationStart = Now();
  1092. }
  1093. Queue->AddItem(QueueItem);
  1094. }
  1095. //---------------------------------------------------------------------------
  1096. void __fastcall TCustomScpExplorerForm::RestoreFormParams()
  1097. {
  1098. }
  1099. //---------------------------------------------------------------------------
  1100. void __fastcall TCustomScpExplorerForm::RestoreParams()
  1101. {
  1102. DebugAssert(FDocks != NULL);
  1103. for (int Index = 0; Index < ComponentCount; Index++)
  1104. {
  1105. TTBDock * Dock = dynamic_cast<TTBDock *>(Components[Index]);
  1106. if ((Dock != NULL) && (Dock->Tag == 0))
  1107. {
  1108. FDocks->Add(Dock);
  1109. }
  1110. }
  1111. CollectItemsWithTextDisplayMode(this);
  1112. ConfigurationChanged();
  1113. QueuePanel->Height =
  1114. LoadDimension(WinConfiguration->QueueView.Height, WinConfiguration->QueueView.HeightPixelsPerInch, this);
  1115. LoadListViewStr(QueueView3, WinConfiguration->QueueView.Layout);
  1116. QueueDock->Visible = WinConfiguration->QueueView.ToolBar;
  1117. QueueLabel->Visible = WinConfiguration->QueueView.Label;
  1118. }
  1119. //---------------------------------------------------------------------------
  1120. void __fastcall TCustomScpExplorerForm::StoreParams()
  1121. {
  1122. WinConfiguration->QueueView.Height = QueuePanel->Height;
  1123. WinConfiguration->QueueView.HeightPixelsPerInch = GetControlPixelsPerInch(this);
  1124. WinConfiguration->QueueView.Layout = GetListViewStr(QueueView3);
  1125. WinConfiguration->QueueView.ToolBar = QueueDock->Visible;
  1126. WinConfiguration->QueueView.Label = QueueLabel->Visible;
  1127. }
  1128. //---------------------------------------------------------------------------
  1129. void __fastcall TCustomScpExplorerForm::CreateParams(TCreateParams & Params)
  1130. {
  1131. if (!FFormRestored)
  1132. {
  1133. FFormRestored = true;
  1134. RestoreFormParams();
  1135. }
  1136. TForm::CreateParams(Params);
  1137. }
  1138. //---------------------------------------------------------------------------
  1139. void __fastcall TCustomScpExplorerForm::SetDockAllowDrag(bool value)
  1140. {
  1141. DebugAssert(FDocks != NULL);
  1142. for (int Index = 0; Index < FDocks->Count; Index++)
  1143. {
  1144. static_cast<TTBDock*>(FDocks->Items[Index])->AllowDrag = value;
  1145. }
  1146. }
  1147. //---------------------------------------------------------------------------
  1148. void __fastcall TCustomScpExplorerForm::LoadToolbarsLayoutStr(UnicodeString LayoutStr, UnicodeString ButtonsStr)
  1149. {
  1150. SetDockAllowDrag(true);
  1151. while (!ButtonsStr.IsEmpty())
  1152. {
  1153. UnicodeString S = CutToChar(ButtonsStr, L';', true);
  1154. UnicodeString Name = CutToChar(S, L'=', true);
  1155. for (int ToolbarIndex = 0; ToolbarIndex < ComponentCount; ToolbarIndex++)
  1156. {
  1157. TTBCustomToolbar * Toolbar = dynamic_cast<TTBCustomToolbar *>(Components[ToolbarIndex]);
  1158. if ((Toolbar != NULL) && SameText(Name, GetToolbarKey(Toolbar->Name)))
  1159. {
  1160. while (!S.IsEmpty())
  1161. {
  1162. UnicodeString S2 = CutToChar(S, L',', true);
  1163. UnicodeString Name = CutToChar(S2, L':', true);
  1164. for (int ItemIndex = 0; ItemIndex < Toolbar->Items->Count; ItemIndex++)
  1165. {
  1166. TTBCustomItem * Item = Toolbar->Items->Items[ItemIndex];
  1167. if (SameText(GetToolbarItemName(Item), Name))
  1168. {
  1169. // forward compatibility
  1170. UnicodeString S3 = CutToChar(S2, L':', true);
  1171. bool Visible = StrToIntDef(S3, 0);
  1172. Item->Visible = Visible;
  1173. }
  1174. }
  1175. }
  1176. }
  1177. }
  1178. }
  1179. ::LoadToolbarsLayoutStr(this, LayoutStr);
  1180. SetDockAllowDrag(!WinConfiguration->LockToolbars);
  1181. }
  1182. //---------------------------------------------------------------------------
  1183. UnicodeString __fastcall TCustomScpExplorerForm::GetToolbarsLayoutStr()
  1184. {
  1185. UnicodeString Result;
  1186. SetDockAllowDrag(true);
  1187. Result = ::GetToolbarsLayoutStr(this);
  1188. SetDockAllowDrag(!WinConfiguration->LockToolbars);
  1189. return Result;
  1190. }
  1191. //---------------------------------------------------------------------------
  1192. UnicodeString __fastcall TCustomScpExplorerForm::GetToolbarItemName(TTBCustomItem * Item)
  1193. {
  1194. UnicodeString Result;
  1195. if (Item->Action != NULL)
  1196. {
  1197. Result = Item->Action->Name;
  1198. Result = RemoveSuffix(Result, L"Action");
  1199. }
  1200. else
  1201. {
  1202. Result = Item->Name;
  1203. Result = RemoveSuffix(Result, L"SubmenuItem");
  1204. Result = RemoveSuffix(Result, L"Item");
  1205. }
  1206. return Result;
  1207. }
  1208. //---------------------------------------------------------------------------
  1209. UnicodeString __fastcall TCustomScpExplorerForm::GetToolbarsButtonsStr()
  1210. {
  1211. UnicodeString Result;
  1212. for (int ToolbarIndex = 0; ToolbarIndex < ComponentCount; ToolbarIndex++)
  1213. {
  1214. UnicodeString ToolbarStr;
  1215. TTBCustomToolbar * Toolbar = dynamic_cast<TTBCustomToolbar *>(Components[ToolbarIndex]);
  1216. if ((Toolbar != NULL) && (Toolbar != QueueToolbar))
  1217. {
  1218. for (int ItemIndex = 0; ItemIndex < Toolbar->Items->Count; ItemIndex++)
  1219. {
  1220. TTBCustomItem * Item = Toolbar->Items->Items[ItemIndex];
  1221. // Currently all buttons are visible by default, so we can safely remember all hidden buttons.
  1222. // Once we introduce any buttons that aer hidden by default, we would have to remember their initial state
  1223. // and save the changes here only.
  1224. if (!Item->Visible)
  1225. {
  1226. UnicodeString Name = GetToolbarItemName(Item);
  1227. DebugAssert(Name.Pos(L"TBX") == 0);
  1228. AddToList(ToolbarStr, FORMAT(L"%s:0", (Name)), L",");
  1229. }
  1230. }
  1231. }
  1232. if (!ToolbarStr.IsEmpty())
  1233. {
  1234. AddToList(Result, FORMAT(L"%s=%s", (GetToolbarKey(Toolbar->Name), ToolbarStr)), L";");
  1235. }
  1236. }
  1237. return Result;
  1238. }
  1239. //---------------------------------------------------------------------------
  1240. void __fastcall TCustomScpExplorerForm::CreateProgressForm(TSynchronizeProgress * SynchronizeProgress)
  1241. {
  1242. DebugAssert(FProgressForm == NULL);
  1243. FProgressForm =
  1244. new TProgressForm(Application, (FTransferResumeList != NULL), Terminal->IsCapable[fsSkipTransfer], SynchronizeProgress);
  1245. FProgressForm->DeleteLocalToRecycleBin =
  1246. (WinConfiguration->DeleteToRecycleBin != FAlternativeDelete);
  1247. FProgressForm->DeleteRemoteToRecycleBin =
  1248. (Terminal->SessionData->DeleteToRecycleBin != FAlternativeDelete) &&
  1249. !Terminal->SessionData->RecycleBinPath.IsEmpty();
  1250. // As progress window has delayed show now, we need to lock ourselves, (at least) until then
  1251. LockWindow();
  1252. if (FTaskbarList != NULL)
  1253. {
  1254. // Actually, do not know what hides the progress once the operation finishes
  1255. SetTaskbarListProgressState(TBPF_NORMAL);
  1256. }
  1257. }
  1258. //---------------------------------------------------------------------------
  1259. void __fastcall TCustomScpExplorerForm::DestroyProgressForm()
  1260. {
  1261. UnlockWindow();
  1262. SAFE_DESTROY(FProgressForm);
  1263. SetQueueProgress();
  1264. }
  1265. //---------------------------------------------------------------------------
  1266. void __fastcall TCustomScpExplorerForm::FileOperationProgress(
  1267. TFileOperationProgressType & ProgressData)
  1268. {
  1269. // Download to temporary local directory
  1270. if (FPendingTempSpaceWarn && ProgressData.InProgress && ProgressData.TotalSizeSet)
  1271. {
  1272. bool Continue = true;
  1273. FPendingTempSpaceWarn = false;
  1274. DoWarnLackOfTempSpace(ProgressData.Directory, ProgressData.TotalSize, Continue);
  1275. if (!Continue)
  1276. {
  1277. Abort();
  1278. }
  1279. }
  1280. // operation is being executed and we still didn't show up progress form
  1281. if (ProgressData.InProgress && !FProgressForm)
  1282. {
  1283. CreateProgressForm(NULL);
  1284. // When main window is hidden, synchronisation form does not exist (we do not get here during synchronization - only comparing),
  1285. // we suppose "/upload" or URL download mode
  1286. if (!Visible && (ProgressData.Operation != foCalculateSize) &&
  1287. (ProgressData.Operation != foCalculateChecksum) &&
  1288. (FSynchronizeProgressForm == NULL) &&
  1289. !FStandaloneEditing)
  1290. {
  1291. FProgressForm->OnceDoneOperation = odoDisconnect;
  1292. }
  1293. }
  1294. // operation is finished (or terminated), so we hide progress form
  1295. else if (!ProgressData.InProgress && (FProgressForm != NULL) && (FProgressForm->SynchronizeProgress == NULL))
  1296. {
  1297. DestroyProgressForm();
  1298. if (ProgressData.Operation == foCalculateSize)
  1299. {
  1300. // When calculating size before transfer, the abort caused by
  1301. // cancel flag set due to "MoveToQueue" below,
  1302. // is not propagated back to us in ExecuteFileOperation,
  1303. // as it is silently swallowed.
  1304. // So we explicitly (re)throw it here.
  1305. if (FMoveToQueue)
  1306. {
  1307. Abort();
  1308. }
  1309. }
  1310. else
  1311. {
  1312. if ((ProgressData.Cancel == csContinue) &&
  1313. !FAutoOperation)
  1314. {
  1315. OperationComplete(ProgressData.StartTime);
  1316. }
  1317. }
  1318. }
  1319. if (FProgressForm &&
  1320. // Can happen only during synchronization, when ending one of the operations
  1321. ProgressData.InProgress)
  1322. {
  1323. FProgressForm->SetProgressData(ProgressData);
  1324. if (FTaskbarList != NULL)
  1325. {
  1326. DebugAssert(ProgressData.InProgress);
  1327. SetTaskbarListProgressValue(&ProgressData);
  1328. }
  1329. if (FProgressForm->Cancel > csContinue)
  1330. {
  1331. ProgressData.SetCancelAtLeast(FProgressForm->Cancel);
  1332. if (FProgressForm->Cancel == csCancelFile)
  1333. {
  1334. FProgressForm->ClearCancel();
  1335. }
  1336. // cancel cancels even the move
  1337. FMoveToQueue = false;
  1338. }
  1339. else if (FProgressForm->MoveToQueue)
  1340. {
  1341. FMoveToQueue = true;
  1342. ProgressData.SetCancelAtLeast(csCancel);
  1343. }
  1344. if ((FTransferResumeList != NULL) &&
  1345. ProgressData.InProgress &&
  1346. ((ProgressData.Operation == foCopy) || (ProgressData.Operation == foMove)) &&
  1347. !ProgressData.FullFileName.IsEmpty())
  1348. {
  1349. if ((FTransferResumeList->Count == 0) ||
  1350. (FTransferResumeList->Strings[FTransferResumeList->Count - 1] != ProgressData.FullFileName))
  1351. {
  1352. // note that we do not recognize directories from files here
  1353. FTransferResumeList->Add(ProgressData.FullFileName);
  1354. }
  1355. }
  1356. }
  1357. }
  1358. //---------------------------------------------------------------------------
  1359. void __fastcall TCustomScpExplorerForm::OperationComplete(
  1360. const TDateTime & StartTime)
  1361. {
  1362. if (GUIConfiguration->BeepOnFinish &&
  1363. (Now() - StartTime > GUIConfiguration->BeepOnFinishAfter))
  1364. {
  1365. UnicodeString BeepSound = GUIConfiguration->BeepSound;
  1366. DWORD Sound;
  1367. if (!ExtractFileExt(BeepSound).IsEmpty())
  1368. {
  1369. Sound = SND_FILENAME;
  1370. }
  1371. else
  1372. {
  1373. Sound = SND_ALIAS;
  1374. }
  1375. PlaySound(BeepSound.c_str(), NULL, Sound | SND_ASYNC);
  1376. }
  1377. }
  1378. //---------------------------------------------------------------------------
  1379. void __fastcall TCustomScpExplorerForm::OperationProgress(
  1380. TFileOperationProgressType & ProgressData)
  1381. {
  1382. // For example, when calling TFileOperationProgressType::Suspend in TTerminal::QueryReopen,
  1383. // we cannot recurse back to TTerminal::Idle as that may detect another error, calling "suspend" again.
  1384. TAutoNestingCounter Counter(FDoNotIdleCurrentTerminal);
  1385. FileOperationProgress(ProgressData);
  1386. }
  1387. //---------------------------------------------------------------------------
  1388. UnicodeString __fastcall TCustomScpExplorerForm::GetProgressTitle()
  1389. {
  1390. UnicodeString Result;
  1391. if (FProgressForm != NULL)
  1392. {
  1393. Result = FProgressForm->ProgressStr();
  1394. }
  1395. return Result;
  1396. }
  1397. //---------------------------------------------------------------------------
  1398. bool __fastcall TCustomScpExplorerForm::PanelOperation(TOperationSide /*Side*/,
  1399. bool DragDrop)
  1400. {
  1401. return (!DragDrop && (DropSourceControl == NULL)) ||
  1402. (DropSourceControl == RemoteDirView);
  1403. }
  1404. //---------------------------------------------------------------------------
  1405. void __fastcall TCustomScpExplorerForm::DoOperationFinished(
  1406. TFileOperation Operation, TOperationSide Side,
  1407. bool /*Temp*/, const UnicodeString & FileName, bool Success,
  1408. TOnceDoneOperation & OnceDoneOperation)
  1409. {
  1410. if (!FAutoOperation)
  1411. {
  1412. // no selection on "/upload", form servers only as event handler
  1413. // (it is not displayed)
  1414. if (PanelOperation(Side, FDragDropOperation) &&
  1415. Visible && (Operation != foCalculateSize) &&
  1416. (Operation != foGetProperties) && (Operation != foCalculateChecksum))
  1417. {
  1418. if (FOnFileOperationFinished != NULL)
  1419. {
  1420. FOnFileOperationFinished(FileName, Success);
  1421. }
  1422. else
  1423. {
  1424. TCustomDirView * DView = DirView(Side);
  1425. UnicodeString FileNameOnly = ExtractFileName(FileName, (Side == osRemote));
  1426. TListItem *Item = DView->FindFileItem(FileNameOnly);
  1427. // this can happen when local drive is unplugged in the middle of the operation
  1428. if (Item != NULL)
  1429. {
  1430. if (Success) Item->Selected = false;
  1431. DView->MakeProgressVisible(Item);
  1432. }
  1433. }
  1434. }
  1435. if ((Operation == foCopy) || (Operation == foMove))
  1436. {
  1437. if (Side == osLocal)
  1438. {
  1439. Configuration->Usage->Inc(L"UploadedFiles");
  1440. }
  1441. else
  1442. {
  1443. Configuration->Usage->Inc(L"DownloadedFiles");
  1444. }
  1445. }
  1446. }
  1447. if (Success && (FSynchronizeController != NULL))
  1448. {
  1449. if (Operation == foCopy)
  1450. {
  1451. DebugAssert(Side == osLocal);
  1452. FSynchronizeController->LogOperation(soUpload, FileName);
  1453. }
  1454. else if (Operation == foDelete)
  1455. {
  1456. DebugAssert(Side == osRemote);
  1457. FSynchronizeController->LogOperation(soDelete, FileName);
  1458. }
  1459. }
  1460. if (FProgressForm)
  1461. {
  1462. OnceDoneOperation = FProgressForm->OnceDoneOperation;
  1463. }
  1464. }
  1465. //---------------------------------------------------------------------------
  1466. void __fastcall TCustomScpExplorerForm::OperationFinished(
  1467. TFileOperation Operation, TOperationSide Side,
  1468. bool Temp, const UnicodeString & FileName, Boolean Success,
  1469. TOnceDoneOperation & OnceDoneOperation)
  1470. {
  1471. TAutoNestingCounter Counter(FDoNotIdleCurrentTerminal);
  1472. DoOperationFinished(Operation, Side, Temp, FileName, Success,
  1473. OnceDoneOperation);
  1474. }
  1475. //---------------------------------------------------------------------------
  1476. TCustomDirView * __fastcall TCustomScpExplorerForm::DirView(TOperationSide Side)
  1477. {
  1478. DebugAssert(GetSide(Side) == osRemote);
  1479. DebugUsedParam(Side);
  1480. return RemoteDirView;
  1481. }
  1482. //---------------------------------------------------------------------------
  1483. bool __fastcall TCustomScpExplorerForm::DirViewEnabled(TOperationSide Side)
  1484. {
  1485. DebugAssert(GetSide(Side) == osRemote);
  1486. DebugUsedParam(Side);
  1487. return (Terminal != NULL);
  1488. }
  1489. //---------------------------------------------------------------------------
  1490. bool __fastcall TCustomScpExplorerForm::GetEnableFocusedOperation(
  1491. TOperationSide Side, int FilesOnly)
  1492. {
  1493. return DirView(Side)->AnyFileSelected(true, (FilesOnly != 0), true);
  1494. }
  1495. //---------------------------------------------------------------------------
  1496. bool __fastcall TCustomScpExplorerForm::GetEnableSelectedOperation(
  1497. TOperationSide Side, int FilesOnly)
  1498. {
  1499. return DirView(Side)->AnyFileSelected(false, (FilesOnly != 0), true);
  1500. }
  1501. //---------------------------------------------------------------------------
  1502. struct THistoryItemData
  1503. {
  1504. short int Side;
  1505. short int Index;
  1506. };
  1507. //---------------------------------------------------------------------------
  1508. void __fastcall TCustomScpExplorerForm::HistoryGo(TOperationSide Side, int Index)
  1509. {
  1510. DirView(Side)->HistoryGo(Index);
  1511. }
  1512. //---------------------------------------------------------------------------
  1513. void __fastcall TCustomScpExplorerForm::HistoryItemClick(System::TObject* Sender)
  1514. {
  1515. TTBCustomItem * Item = dynamic_cast<TTBCustomItem *>(Sender);
  1516. THistoryItemData Data = *reinterpret_cast<THistoryItemData*>(&(Item->Tag));
  1517. HistoryGo((TOperationSide)Data.Side, Data.Index);
  1518. }
  1519. //---------------------------------------------------------------------------
  1520. void __fastcall TCustomScpExplorerForm::UpdateHistoryMenu(TOperationSide Side,
  1521. bool Back)
  1522. {
  1523. if (FHistoryMenu[Side == osLocal][Back] != NULL)
  1524. {
  1525. TCustomDirView * DView = DirView(Side);
  1526. TTBXPopupMenu * Menu = FHistoryMenu[Side == osLocal][Back];
  1527. int ICount = Back ? DView->BackCount : DView->ForwardCount;
  1528. if (ICount > 10)
  1529. {
  1530. ICount = 10;
  1531. }
  1532. Menu->Items->Clear();
  1533. THistoryItemData Data;
  1534. Data.Side = Side;
  1535. for (short int i = 1; i <= ICount; i++)
  1536. {
  1537. TTBCustomItem * Item = new TTBXItem(Menu);
  1538. Data.Index = static_cast<short int>(i * (Back ? -1 : 1));
  1539. Item->Caption = MinimizeName(DView->HistoryPath[Data.Index], 50, (Side == osRemote));
  1540. Item->Hint = DView->HistoryPath[Data.Index];
  1541. DebugAssert(sizeof(int) == sizeof(THistoryItemData));
  1542. Item->Tag = *reinterpret_cast<int*>(&Data);
  1543. Item->OnClick = HistoryItemClick;
  1544. Menu->Items->Add(Item);
  1545. }
  1546. }
  1547. }
  1548. //---------------------------------------------------------------------------
  1549. TTBXPopupMenu * __fastcall TCustomScpExplorerForm::HistoryMenu(
  1550. TOperationSide Side, bool Back)
  1551. {
  1552. if (FHistoryMenu[Side == osLocal][Back] == NULL)
  1553. {
  1554. // workaround
  1555. // In Pascal the size of TTBXPopupMenu is 132, in C++ 136,
  1556. // operator new allocates memory in Pascal code, but calls inline
  1557. // contructor in C++, leading in problems, the function does
  1558. // both in Pascal code
  1559. FHistoryMenu[Side == osLocal][Back] = CreateTBXPopupMenu(this);
  1560. UpdateHistoryMenu(Side, Back);
  1561. }
  1562. return FHistoryMenu[Side == osLocal][Back];
  1563. }
  1564. //---------------------------------------------------------------------------
  1565. void __fastcall TCustomScpExplorerForm::DirViewHistoryChange(
  1566. TCustomDirView *Sender)
  1567. {
  1568. TOperationSide Side = (Sender == DirView(osRemote) ? osRemote : osLocal);
  1569. UpdateHistoryMenu(Side, true);
  1570. UpdateHistoryMenu(Side, false);
  1571. }
  1572. //---------------------------------------------------------------------------
  1573. bool __fastcall TCustomScpExplorerForm::CustomCommandRemoteAllowed()
  1574. {
  1575. // remote custom commands can be executed only if the server supports shell commands
  1576. // or have secondary shell
  1577. return (FTerminal != NULL) && (FTerminal->IsCapable[fcSecondaryShell] || FTerminal->IsCapable[fcShellAnyCommand]);
  1578. }
  1579. //---------------------------------------------------------------------------
  1580. int __fastcall TCustomScpExplorerForm::CustomCommandState(
  1581. const TCustomCommandType & Command, bool OnFocused, TCustomCommandListType ListType)
  1582. {
  1583. int Result;
  1584. TFileCustomCommand RemoteCustomCommand;
  1585. TLocalCustomCommand LocalCustomCommand;
  1586. TFileCustomCommand * NonInteractiveCustomCommand =
  1587. FLAGCLEAR(Command.Params, ccLocal) ? &RemoteCustomCommand : &LocalCustomCommand;
  1588. TInteractiveCustomCommand InteractiveCustomCommand(NonInteractiveCustomCommand);
  1589. UnicodeString Cmd = InteractiveCustomCommand.Complete(Command.Command, false);
  1590. if (FLAGCLEAR(Command.Params, ccLocal))
  1591. {
  1592. int AllowedState = CustomCommandRemoteAllowed() ? 1 : 0;
  1593. // custom command that does not operate with files can be executed anytime ...
  1594. if (!NonInteractiveCustomCommand->IsFileCommand(Cmd))
  1595. {
  1596. if ((ListType == ccltAll) || (ListType == ccltNonFile))
  1597. {
  1598. // ... but do not show such command in remote file menu (TODO)
  1599. Result = AllowedState;
  1600. }
  1601. else
  1602. {
  1603. Result = -1;
  1604. }
  1605. }
  1606. else
  1607. {
  1608. if ((ListType == ccltAll) || (ListType == ccltFile))
  1609. {
  1610. Result = ((FCurrentSide == osRemote) && DirView(osRemote)->AnyFileSelected(OnFocused, false, true)) ? AllowedState : 0;
  1611. }
  1612. else
  1613. {
  1614. Result = -1;
  1615. }
  1616. }
  1617. }
  1618. else
  1619. {
  1620. // custom command that does not operate with files can be executed anytime
  1621. if (!NonInteractiveCustomCommand->IsFileCommand(Cmd))
  1622. {
  1623. Result = ((ListType == ccltAll) || (ListType == ccltNonFile)) ? 1 : -1;
  1624. }
  1625. else if (LocalCustomCommand.HasLocalFileName(Cmd))
  1626. {
  1627. if ((ListType == ccltAll) || (ListType == ccltFile))
  1628. {
  1629. // special case is "diff"-style command that can be executed over any side,
  1630. // if we have both sides
  1631. Result =
  1632. // Cannot have focus on both panels, so we have to call AnyFileSelected
  1633. // directly (instead of EnableSelectedOperation) to pass
  1634. // false to FocusedFileOnlyWhenFocused when panel is inactive.
  1635. ((HasDirView[osLocal] && DirView(osLocal)->AnyFileSelected(false, false, (FCurrentSide == osLocal))) &&
  1636. DirView(osRemote)->AnyFileSelected(false, false, (FCurrentSide == osRemote))) ? 1 : 0;
  1637. }
  1638. else if (ListType == ccltBoth)
  1639. {
  1640. Result = 1;
  1641. }
  1642. else
  1643. {
  1644. Result = -1;
  1645. }
  1646. }
  1647. else
  1648. {
  1649. if ((ListType == ccltAll) || (ListType == ccltFile))
  1650. {
  1651. Result = DirView(FCurrentSide)->AnyFileSelected(OnFocused, false, true) ? 1 : 0;
  1652. }
  1653. else
  1654. {
  1655. Result = -1;
  1656. }
  1657. }
  1658. }
  1659. return Result;
  1660. }
  1661. //---------------------------------------------------------------------------
  1662. void __fastcall TCustomScpExplorerForm::RemoteCustomCommand(
  1663. TStrings * FileList, const TCustomCommandType & ACommand,
  1664. const TCustomCommandData & Data, const UnicodeString & CommandCommand)
  1665. {
  1666. if (EnsureCommandSessionFallback(fcShellAnyCommand))
  1667. {
  1668. TRemoteCustomCommand RemoteCustomCommand(Data, Terminal->CurrentDirectory);
  1669. TWinInteractiveCustomCommand InteractiveCustomCommand(
  1670. &RemoteCustomCommand, ACommand.Name, ACommand.HomePage);
  1671. UnicodeString Command = InteractiveCustomCommand.Complete(CommandCommand, false);
  1672. Configuration->Usage->Inc(L"RemoteCustomCommandRuns2");
  1673. bool Capture =
  1674. FLAGSET(ACommand.Params, ccShowResults) ||
  1675. FLAGSET(ACommand.Params, ccCopyResults) ||
  1676. FLAGSET(ACommand.Params, ccShowResultsInMsgBox);
  1677. TCaptureOutputEvent OutputEvent = NULL;
  1678. DebugAssert(FCapturedLog == NULL);
  1679. if (Capture)
  1680. {
  1681. FCapturedLog = new TStringList();
  1682. OutputEvent = TerminalCaptureLog;
  1683. }
  1684. try
  1685. {
  1686. if (!RemoteCustomCommand.IsFileCommand(Command))
  1687. {
  1688. Terminal->AnyCommand(RemoteCustomCommand.Complete(Command, true),
  1689. OutputEvent);
  1690. }
  1691. else
  1692. {
  1693. Terminal->CustomCommandOnFiles(Command, ACommand.Params, FileList, OutputEvent);
  1694. }
  1695. if ((FCapturedLog != NULL) && (FCapturedLog->Count > 0))
  1696. {
  1697. if (FLAGSET(ACommand.Params, ccCopyResults))
  1698. {
  1699. CopyToClipboard(FCapturedLog);
  1700. }
  1701. if (FLAGSET(ACommand.Params, ccShowResults))
  1702. {
  1703. DoConsoleDialog(Terminal, L"", FCapturedLog);
  1704. }
  1705. else if (FLAGSET(ACommand.Params, ccShowResultsInMsgBox))
  1706. {
  1707. MessageDialog(FCapturedLog->Text, qtInformation, qaOK);
  1708. }
  1709. }
  1710. }
  1711. __finally
  1712. {
  1713. SAFE_DESTROY(FCapturedLog);
  1714. }
  1715. }
  1716. }
  1717. //---------------------------------------------------------------------------
  1718. void __fastcall TCustomScpExplorerForm::LocalCustomCommandPure(
  1719. TStrings * FileList, const TCustomCommandType & ACommand, const UnicodeString & Command, TStrings * ALocalFileList,
  1720. const TCustomCommandData & Data, bool LocalFileCommand, bool FileListCommand, UnicodeString * POutput)
  1721. {
  1722. TStrings * LocalFileList = NULL;
  1723. TStrings * RemoteFileList = NULL;
  1724. TStrings * RemoteFileListFull = NULL;
  1725. try
  1726. {
  1727. if (LocalFileCommand)
  1728. {
  1729. if (ALocalFileList == NULL)
  1730. {
  1731. DebugAssert(HasDirView[osLocal]);
  1732. // Cannot have focus on both panels, so we have to call AnyFileSelected
  1733. // directly (instead of EnableSelectedOperation) to pass
  1734. // false to FocusedFileOnlyWhenFocused
  1735. DebugAssert(DirView(osLocal)->AnyFileSelected(false, false, false));
  1736. LocalFileList = DirView(osLocal)->CreateFileList(false, true, NULL);
  1737. }
  1738. else
  1739. {
  1740. LocalFileList = ALocalFileList;
  1741. }
  1742. if (FileListCommand)
  1743. {
  1744. if (LocalFileList->Count != 1)
  1745. {
  1746. throw Exception(LoadStr(CUSTOM_COMMAND_SELECTED_UNMATCH1));
  1747. }
  1748. }
  1749. else
  1750. {
  1751. if ((LocalFileList->Count != 1) &&
  1752. (FileList->Count != 1) &&
  1753. (LocalFileList->Count != FileList->Count))
  1754. {
  1755. throw Exception(LoadStr(CUSTOM_COMMAND_SELECTED_UNMATCH));
  1756. }
  1757. }
  1758. }
  1759. UnicodeString RootTempDir;
  1760. UnicodeString TempDir;
  1761. bool RemoteFiles = FLAGSET(ACommand.Params, ccRemoteFiles);
  1762. if (!RemoteFiles)
  1763. {
  1764. TemporarilyDownloadFiles(FileList, false, RootTempDir, TempDir, false, false, true);
  1765. }
  1766. try
  1767. {
  1768. TDateTimes RemoteFileTimes;
  1769. if (RemoteFiles)
  1770. {
  1771. RemoteFileList = FileList;
  1772. }
  1773. else
  1774. {
  1775. RemoteFileList = new TStringList();
  1776. TMakeLocalFileListParams MakeFileListParam;
  1777. MakeFileListParam.FileList = RemoteFileList;
  1778. MakeFileListParam.FileTimes = &RemoteFileTimes;
  1779. MakeFileListParam.IncludeDirs = FLAGSET(ACommand.Params, ccApplyToDirectories);
  1780. MakeFileListParam.Recursive =
  1781. FLAGSET(ACommand.Params, ccRecursive) && !FileListCommand;
  1782. ProcessLocalDirectory(TempDir, Terminal->MakeLocalFileList, &MakeFileListParam);
  1783. if (!MakeFileListParam.Recursive)
  1784. {
  1785. RemoteFileTimes.clear();
  1786. RemoteFileListFull = new TStringList();
  1787. MakeFileListParam.FileList = RemoteFileListFull;
  1788. MakeFileListParam.Recursive = true;
  1789. MakeFileListParam.IncludeDirs = false;
  1790. ProcessLocalDirectory(TempDir, Terminal->MakeLocalFileList, &MakeFileListParam);
  1791. }
  1792. else
  1793. {
  1794. RemoteFileListFull = RemoteFileList;
  1795. }
  1796. }
  1797. bool NonBlocking = FileListCommand && RemoteFiles && !POutput;
  1798. TFileOperationProgressType Progress(&OperationProgress, &OperationFinished);
  1799. if (!NonBlocking)
  1800. {
  1801. Progress.Start(foCustomCommand, osRemote, FileListCommand ? 1 : FileList->Count);
  1802. DebugAssert(FProgressForm != NULL);
  1803. FProgressForm->ReadOnly = true;
  1804. }
  1805. try
  1806. {
  1807. if (FileListCommand)
  1808. {
  1809. UnicodeString LocalFile;
  1810. // MakeFileList does not delimit filenames
  1811. UnicodeString FileList = MakeFileList(RemoteFileList);
  1812. if (LocalFileCommand)
  1813. {
  1814. DebugAssert(LocalFileList->Count == 1);
  1815. LocalFile = LocalFileList->Strings[0];
  1816. }
  1817. TLocalCustomCommand CustomCommand(Data,
  1818. Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(), L"", LocalFile, FileList);
  1819. UnicodeString ShellCommand = CustomCommand.Complete(Command, true);
  1820. if (NonBlocking)
  1821. {
  1822. DebugAssert(!POutput);
  1823. ExecuteShellChecked(ShellCommand);
  1824. }
  1825. else
  1826. {
  1827. ExecuteProcessCheckedAndWait(ShellCommand, HelpKeyword, POutput);
  1828. }
  1829. }
  1830. else if (LocalFileCommand)
  1831. {
  1832. if (LocalFileList->Count == 1)
  1833. {
  1834. UnicodeString LocalFile = LocalFileList->Strings[0];
  1835. for (int Index = 0; Index < RemoteFileList->Count; Index++)
  1836. {
  1837. UnicodeString FileName = RemoteFileList->Strings[Index];
  1838. TLocalCustomCommand CustomCommand(Data,
  1839. Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(), FileName, LocalFile, L"");
  1840. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  1841. }
  1842. }
  1843. else if (RemoteFileList->Count == 1)
  1844. {
  1845. UnicodeString FileName = RemoteFileList->Strings[0];
  1846. for (int Index = 0; Index < LocalFileList->Count; Index++)
  1847. {
  1848. TLocalCustomCommand CustomCommand(
  1849. Data, Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(),
  1850. FileName, LocalFileList->Strings[Index], L"");
  1851. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  1852. }
  1853. }
  1854. else
  1855. {
  1856. if (LocalFileList->Count != RemoteFileList->Count)
  1857. {
  1858. throw Exception(LoadStr(CUSTOM_COMMAND_PAIRS_DOWNLOAD_FAILED));
  1859. }
  1860. for (int Index = 0; Index < LocalFileList->Count; Index++)
  1861. {
  1862. UnicodeString FileName = RemoteFileList->Strings[Index];
  1863. TLocalCustomCommand CustomCommand(
  1864. Data, Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(),
  1865. FileName, LocalFileList->Strings[Index], L"");
  1866. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  1867. }
  1868. }
  1869. }
  1870. else
  1871. {
  1872. for (int Index = 0; Index < RemoteFileList->Count; Index++)
  1873. {
  1874. TLocalCustomCommand CustomCommand(Data,
  1875. Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(),
  1876. RemoteFileList->Strings[Index], L"", L"");
  1877. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  1878. }
  1879. }
  1880. }
  1881. __finally
  1882. {
  1883. if (!NonBlocking)
  1884. {
  1885. Progress.Stop();
  1886. }
  1887. }
  1888. DebugAssert(!FAutoOperation);
  1889. if (!RemoteFiles)
  1890. {
  1891. TempDir = IncludeTrailingBackslash(TempDir);
  1892. for (int Index = 0; Index < RemoteFileListFull->Count; Index++)
  1893. {
  1894. UnicodeString FileName = RemoteFileListFull->Strings[Index];
  1895. if (DebugAlwaysTrue(SameText(TempDir, FileName.SubString(1, TempDir.Length()))) &&
  1896. // Skip directories, process only nested files.
  1897. // The check is redundant as FileAge fails for directories anyway.
  1898. !DirectoryExists(FileName))
  1899. {
  1900. UnicodeString RemoteDir =
  1901. UnixExtractFileDir(
  1902. UnixIncludeTrailingBackslash(FTerminal->CurrentDirectory) +
  1903. ToUnixPath(FileName.SubString(TempDir.Length() + 1, FileName.Length() - TempDir.Length())));
  1904. TDateTime NewTime;
  1905. if (FileAge(FileName, NewTime) &&
  1906. (NewTime != RemoteFileTimes[Index]))
  1907. {
  1908. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  1909. TemporaryFileCopyParam(CopyParam);
  1910. CopyParam.FileMask = L"";
  1911. FAutoOperation = true;
  1912. std::unique_ptr<TStrings> TemporaryFilesList(new TStringList());
  1913. TemporaryFilesList->Add(FileName);
  1914. FTerminal->CopyToRemote(TemporaryFilesList.get(), RemoteDir, &CopyParam, cpTemporary, NULL);
  1915. }
  1916. }
  1917. }
  1918. }
  1919. }
  1920. __finally
  1921. {
  1922. FAutoOperation = false;
  1923. if (!RootTempDir.IsEmpty() && DebugAlwaysTrue(!RemoteFiles))
  1924. {
  1925. RecursiveDeleteFile(ExcludeTrailingBackslash(RootTempDir), false);
  1926. }
  1927. }
  1928. }
  1929. __finally
  1930. {
  1931. if (RemoteFileList != FileList)
  1932. {
  1933. delete RemoteFileList;
  1934. }
  1935. if (LocalFileList != ALocalFileList)
  1936. {
  1937. delete LocalFileList;
  1938. }
  1939. if (RemoteFileListFull != RemoteFileList)
  1940. {
  1941. delete RemoteFileListFull;
  1942. }
  1943. }
  1944. }
  1945. //---------------------------------------------------------------------------
  1946. void __fastcall TCustomScpExplorerForm::LocalCustomCommandWithRemoteFiles(
  1947. const TCustomCommandType & ACommand, const UnicodeString & Command, const TCustomCommandData & Data,
  1948. bool FileListCommand, UnicodeString * POutput)
  1949. {
  1950. std::unique_ptr<TStrings> SelectedFileList(DirView(osLocal)->CreateFileList(false, true, NULL));
  1951. std::unique_ptr<TStrings> LocalFileList(new TStringList());
  1952. for (int Index = 0; Index < SelectedFileList->Count; Index++)
  1953. {
  1954. UnicodeString FileName = SelectedFileList->Strings[Index];
  1955. if (DirectoryExists(FileName))
  1956. {
  1957. if (FLAGSET(ACommand.Params, ccApplyToDirectories))
  1958. {
  1959. LocalFileList->Add(FileName);
  1960. }
  1961. if (FLAGSET(ACommand.Params, ccRecursive))
  1962. {
  1963. TMakeLocalFileListParams MakeFileListParam;
  1964. MakeFileListParam.FileList = LocalFileList.get();
  1965. MakeFileListParam.FileTimes = NULL;
  1966. MakeFileListParam.IncludeDirs = FLAGSET(ACommand.Params, ccApplyToDirectories);
  1967. MakeFileListParam.Recursive = true;
  1968. ProcessLocalDirectory(FileName, Terminal->MakeLocalFileList, &MakeFileListParam);
  1969. }
  1970. }
  1971. else
  1972. {
  1973. LocalFileList->Add(FileName);
  1974. }
  1975. }
  1976. if (FileListCommand)
  1977. {
  1978. UnicodeString FileList = MakeFileList(LocalFileList.get());
  1979. TLocalCustomCommand CustomCommand(
  1980. Data, Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(),
  1981. L"", L"", FileList);
  1982. ExecuteProcessChecked(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  1983. }
  1984. else
  1985. {
  1986. TFileOperationProgressType Progress(&OperationProgress, &OperationFinished);
  1987. Progress.Start(foCustomCommand, osRemote, FileListCommand ? 1 : LocalFileList->Count);
  1988. DebugAssert(FProgressForm != NULL);
  1989. FProgressForm->ReadOnly = true;
  1990. try
  1991. {
  1992. for (int Index = 0; Index < LocalFileList->Count; Index++)
  1993. {
  1994. UnicodeString FileName = LocalFileList->Strings[Index];
  1995. TLocalCustomCommand CustomCommand(
  1996. Data, Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(),
  1997. FileName, L"", L"");
  1998. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  1999. }
  2000. }
  2001. __finally
  2002. {
  2003. Progress.Stop();
  2004. }
  2005. }
  2006. }
  2007. //---------------------------------------------------------------------------
  2008. void __fastcall TCustomScpExplorerForm::LocalCustomCommand(TStrings * FileList,
  2009. const TCustomCommandType & ACommand, TStrings * ALocalFileList,
  2010. const TCustomCommandData & Data, const UnicodeString & CommandCommand)
  2011. {
  2012. TLocalCustomCommand LocalCustomCommand(Data, Terminal->CurrentDirectory, DefaultDownloadTargetDirectory());
  2013. TWinInteractiveCustomCommand InteractiveCustomCommand(
  2014. &LocalCustomCommand, ACommand.Name, ACommand.HomePage);
  2015. UnicodeString Command = InteractiveCustomCommand.Complete(CommandCommand, false);
  2016. bool FileListCommand = LocalCustomCommand.IsFileListCommand(Command);
  2017. bool LocalFileCommand = LocalCustomCommand.HasLocalFileName(Command);
  2018. Configuration->Usage->Inc(L"LocalCustomCommandRuns2");
  2019. std::unique_ptr<UnicodeString> POutput;
  2020. if (FLAGSET(ACommand.Params, ccShowResultsInMsgBox) ||
  2021. FLAGSET(ACommand.Params, ccCopyResults))
  2022. {
  2023. POutput.reset(new UnicodeString());
  2024. }
  2025. if (!LocalCustomCommand.IsFileCommand(Command))
  2026. {
  2027. ExecuteProcessChecked(LocalCustomCommand.Complete(Command, true), HelpKeyword, POutput.get());
  2028. }
  2029. // remote files?
  2030. else if ((FCurrentSide == osRemote) || LocalFileCommand)
  2031. {
  2032. LocalCustomCommandPure(FileList, ACommand, Command, ALocalFileList, Data, LocalFileCommand, FileListCommand, POutput.get());
  2033. }
  2034. // local files
  2035. else
  2036. {
  2037. LocalCustomCommandWithRemoteFiles(ACommand, Command, Data, FileListCommand, POutput.get());
  2038. }
  2039. if (POutput.get() != NULL)
  2040. {
  2041. // If the output is single-line, we do not want the trailing CRLF, as the CopyToClipboard(TStrings *) does
  2042. int P = POutput->Pos(sLineBreak);
  2043. if (P == POutput->Length() - static_cast<int>(strlen(sLineBreak)) + 1)
  2044. {
  2045. POutput->SetLength(P - 1);
  2046. }
  2047. // Copy even empty output to clipboard
  2048. if (FLAGSET(ACommand.Params, ccCopyResults))
  2049. {
  2050. CopyToClipboard(*POutput);
  2051. }
  2052. // But do not show an empty message box.
  2053. // This way the ShowResultsInMsgBox can be used to suppress a console window of commands with no output
  2054. if (FLAGSET(ACommand.Params, ccShowResultsInMsgBox) &&
  2055. !POutput->IsEmpty())
  2056. {
  2057. TClipboardHandler ClipboardHandler;
  2058. ClipboardHandler.Text = *POutput;
  2059. TMessageParams Params;
  2060. TQueryButtonAlias Aliases[1];
  2061. Aliases[0].Button = qaRetry;
  2062. Aliases[0].Alias = LoadStr(EDIT_COPY);
  2063. Aliases[0].OnSubmit = &ClipboardHandler.Copy;
  2064. Params.Aliases = Aliases;
  2065. Params.AliasesCount = LENOF(Aliases);
  2066. MessageDialog(*POutput, qtInformation, qaOK | qaRetry, HelpKeyword, &Params);
  2067. }
  2068. }
  2069. }
  2070. //---------------------------------------------------------------------------
  2071. void __fastcall TCustomScpExplorerForm::CustomCommand(TStrings * FileList,
  2072. const TCustomCommandType & ACommand, TStrings * ALocalFileList)
  2073. {
  2074. TCustomCommandData Data(Terminal);
  2075. UnicodeString Site = Terminal->SessionData->SessionKey;
  2076. UnicodeString HelpKeyword = ACommand.HomePage;
  2077. std::unique_ptr<TStrings> CustomCommandOptions(CloneStrings(WinConfiguration->CustomCommandOptions));
  2078. if (ACommand.AnyOptionWithFlag(TCustomCommandType::ofRun))
  2079. {
  2080. std::unique_ptr<TCustomCommand> CustomCommandForOptions;
  2081. if (FLAGCLEAR(ACommand.Params, ccLocal))
  2082. {
  2083. CustomCommandForOptions.reset(new TRemoteCustomCommand(Data, Terminal->CurrentDirectory));
  2084. }
  2085. else
  2086. {
  2087. CustomCommandForOptions.reset(new TLocalCustomCommand(Data, Terminal->CurrentDirectory, DefaultDownloadTargetDirectory()));
  2088. }
  2089. if (!DoCustomCommandOptionsDialog(
  2090. &ACommand, CustomCommandOptions.get(), NULL, TCustomCommandType::ofRun, CustomCommandForOptions.get(), Site, NULL))
  2091. {
  2092. Abort();
  2093. }
  2094. }
  2095. UnicodeString CommandCommand = ACommand.GetCommandWithExpandedOptions(CustomCommandOptions.get(), Site);
  2096. if (FLAGCLEAR(ACommand.Params, ccLocal))
  2097. {
  2098. RemoteCustomCommand(FileList, ACommand, Data, CommandCommand);
  2099. }
  2100. else
  2101. {
  2102. LocalCustomCommand(FileList, ACommand, ALocalFileList, Data, CommandCommand);
  2103. }
  2104. }
  2105. //---------------------------------------------------------------------------
  2106. void __fastcall TCustomScpExplorerForm::BothCustomCommand(
  2107. const TCustomCommandType & Command)
  2108. {
  2109. DebugAssert(FCustomCommandLocalFileList != NULL);
  2110. DebugAssert(FCustomCommandRemoteFileList != NULL);
  2111. DebugAssert(FCustomCommandLocalFileList->Count == FCustomCommandRemoteFileList->Count);
  2112. TStrings * LocalFileList = new TStringList();
  2113. TStrings * RemoteFileList = new TStringList();
  2114. try
  2115. {
  2116. for (int Index = 0; Index < FCustomCommandLocalFileList->Count; Index++)
  2117. {
  2118. LocalFileList->Clear();
  2119. LocalFileList->AddObject(
  2120. FCustomCommandLocalFileList->Strings[Index],
  2121. FCustomCommandLocalFileList->Objects[Index]);
  2122. RemoteFileList->Clear();
  2123. RemoteFileList->AddObject(
  2124. FCustomCommandRemoteFileList->Strings[Index],
  2125. FCustomCommandRemoteFileList->Objects[Index]);
  2126. CustomCommand(RemoteFileList, Command, LocalFileList);
  2127. }
  2128. }
  2129. __finally
  2130. {
  2131. delete LocalFileList;
  2132. delete RemoteFileList;
  2133. }
  2134. }
  2135. //---------------------------------------------------------------------------
  2136. void __fastcall TCustomScpExplorerForm::CustomCommandMenu(
  2137. TAction * Action, TStrings * LocalFileList, TStrings * RemoteFileList)
  2138. {
  2139. delete FCustomCommandLocalFileList;
  2140. delete FCustomCommandRemoteFileList;
  2141. // takeover ownership,
  2142. // the lists must survive the MenuPopup as OnClick occurs only after it exits
  2143. FCustomCommandLocalFileList = LocalFileList;
  2144. FCustomCommandRemoteFileList = RemoteFileList;
  2145. TButton * Button = dynamic_cast<TButton *>(Action->ActionComponent);
  2146. if (Button != NULL)
  2147. {
  2148. FCustomCommandMenu->Items->Clear();
  2149. NonVisualDataModule->CreateCustomCommandsMenu(FCustomCommandMenu->Items, false, false, ccltBoth);
  2150. MenuPopup(FCustomCommandMenu, Button);
  2151. }
  2152. else
  2153. {
  2154. NonVisualDataModule->CreateCustomCommandsMenu(Action, false, ccltBoth);
  2155. }
  2156. }
  2157. //---------------------------------------------------------------------------
  2158. void __fastcall TCustomScpExplorerForm::TerminalCaptureLog(
  2159. const UnicodeString & AddedLine, TCaptureOutputType OutputType)
  2160. {
  2161. DebugAssert(FCapturedLog != NULL);
  2162. if ((OutputType == cotOutput) || (OutputType == cotError))
  2163. {
  2164. FCapturedLog->Add(AddedLine);
  2165. }
  2166. }
  2167. //---------------------------------------------------------------------------
  2168. bool __fastcall TCustomScpExplorerForm::IsFileControl(TObject * Control,
  2169. TOperationSide Side)
  2170. {
  2171. return (Side == osRemote) &&
  2172. ((Control == RemoteDirView) || (Control == RemoteDriveView));
  2173. }
  2174. //---------------------------------------------------------------------------
  2175. void __fastcall TCustomScpExplorerForm::DirViewContextPopupDefaultItem(
  2176. TOperationSide Side, TTBXCustomItem * Item, TDoubleClickAction DoubleClickAction)
  2177. {
  2178. TTBItemOptions O;
  2179. O = Item->Options;
  2180. TCustomDirView * DView = DirView(Side);
  2181. if ((DView->ItemFocused != NULL) &&
  2182. (WinConfiguration->DoubleClickAction == DoubleClickAction) &&
  2183. // when resolving links is disabled, default action is to enter the directory,
  2184. // no matter what DoubleClickAction is configured to
  2185. ((Side != osRemote) || Terminal->ResolvingSymlinks || Terminal->IsEncryptingFiles()) &&
  2186. // Can only Edit files, but can Open/Copy even directories
  2187. ((DoubleClickAction != dcaEdit) ||
  2188. !DView->ItemIsDirectory(DView->ItemFocused)))
  2189. {
  2190. Item->Options = O << tboDefault;
  2191. }
  2192. else
  2193. {
  2194. Item->Options = O >> tboDefault;
  2195. }
  2196. }
  2197. //---------------------------------------------------------------------------
  2198. void __fastcall TCustomScpExplorerForm::DirViewContextPopup(
  2199. TOperationSide Side, Byte PopupComponent, const TPoint &MousePos)
  2200. {
  2201. TCustomDirView * DView = DirView(Side);
  2202. TListItem * Item = DView->ItemFocused;
  2203. if ((DView->GetItemAt(MousePos.x, MousePos.y) == Item) &&
  2204. EnableFocusedOperation[Side])
  2205. {
  2206. TPoint ClientPoint;
  2207. ClientPoint = ((MousePos.x < 0) && (MousePos.y < 0)) ?
  2208. TPoint(0, 0) : MousePos;
  2209. FLastContextPopupScreenPoint = DView->ClientToScreen(ClientPoint);
  2210. reinterpret_cast<TPopupMenu*>(GetComponent(PopupComponent))->Popup(
  2211. FLastContextPopupScreenPoint.x, FLastContextPopupScreenPoint.y);
  2212. }
  2213. }
  2214. //---------------------------------------------------------------------------
  2215. void __fastcall TCustomScpExplorerForm::RemoteDirViewContextPopup(
  2216. TObject * /*Sender*/, const TPoint &MousePos, bool &Handled)
  2217. {
  2218. DirViewContextPopupDefaultItem(osRemote, NonVisualDataModule->RemoteOpenMenuItem, dcaOpen);
  2219. DirViewContextPopupDefaultItem(osRemote, NonVisualDataModule->RemoteEditMenuItem, dcaEdit);
  2220. DirViewContextPopupDefaultItem(osRemote, NonVisualDataModule->RemoteCopyMenuItem, dcaCopy);
  2221. DirViewContextPopup(osRemote, fcRemotePopup, MousePos);
  2222. Handled = true;
  2223. }
  2224. //---------------------------------------------------------------------------
  2225. void __fastcall TCustomScpExplorerForm::ReloadLocalDirectory(const UnicodeString Directory)
  2226. {
  2227. }
  2228. //---------------------------------------------------------------------------
  2229. void __fastcall TCustomScpExplorerForm::BatchStart(void *& /*Storage*/)
  2230. {
  2231. DebugAssert(FErrorList == NULL);
  2232. if (WinConfiguration->ContinueOnError)
  2233. {
  2234. FErrorList = new TStringList();
  2235. Configuration->Usage->Inc(L"ContinuationsOnError");
  2236. }
  2237. NonVisualDataModule->StartBusy();
  2238. }
  2239. //---------------------------------------------------------------------------
  2240. void __fastcall TCustomScpExplorerForm::BatchEnd(void * /*Storage*/)
  2241. {
  2242. NonVisualDataModule->EndBusy();
  2243. if (FErrorList)
  2244. {
  2245. HandleErrorList(FErrorList);
  2246. }
  2247. }
  2248. //---------------------------------------------------------------------------
  2249. void __fastcall TCustomScpExplorerForm::UpdateCopyParamCounters(
  2250. const TCopyParamType & CopyParam)
  2251. {
  2252. if (!CopyParam.IncludeFileMask.Masks.IsEmpty())
  2253. {
  2254. Configuration->Usage->Inc(L"FileMaskUses");
  2255. }
  2256. if (IsEffectiveFileNameMask(CopyParam.FileMask))
  2257. {
  2258. Configuration->Usage->Inc(L"OperationMaskUses");
  2259. }
  2260. }
  2261. //---------------------------------------------------------------------------
  2262. bool __fastcall TCustomScpExplorerForm::ExecuteFileOperation(TFileOperation Operation,
  2263. TOperationSide Side, TStrings * FileList, bool NoConfirmation, void * Param)
  2264. {
  2265. void * BatchStorage;
  2266. BatchStart(BatchStorage);
  2267. bool Result;
  2268. try
  2269. {
  2270. if ((Operation == foCopy) || (Operation == foMove))
  2271. {
  2272. TTransferDirection Direction = (Side == osLocal ? tdToRemote : tdToLocal);
  2273. TTransferType Type = (Operation == foCopy ? ttCopy : ttMove);
  2274. UnicodeString TargetDirectory;
  2275. bool Temp = false;
  2276. bool DragDrop = false;
  2277. int Options = 0;
  2278. TAutoSwitch UseQueue = asAuto;
  2279. if (Param != NULL)
  2280. {
  2281. TTransferOperationParam& TParam =
  2282. *static_cast<TTransferOperationParam*>(Param);
  2283. TargetDirectory = TParam.TargetDirectory;
  2284. Temp = TParam.Temp;
  2285. DragDrop = TParam.DragDrop;
  2286. Options = TParam.Options;
  2287. UseQueue = TParam.Queue;
  2288. }
  2289. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  2290. switch (UseQueue)
  2291. {
  2292. case asOn:
  2293. CopyParam.Queue = true;
  2294. break;
  2295. case asOff:
  2296. CopyParam.Queue = false;
  2297. break;
  2298. case asAuto:
  2299. default:
  2300. // keep default
  2301. break;
  2302. }
  2303. Result =
  2304. CopyParamDialog(Direction, Type, Temp, FileList, TargetDirectory,
  2305. CopyParam, !NoConfirmation, DragDrop, Options);
  2306. if (Result)
  2307. {
  2308. DebugAssert(Terminal);
  2309. bool SelectionRestored = false;
  2310. TCustomDirView * DView = NULL;
  2311. if (HasDirView[Side])
  2312. {
  2313. DView = DirView(Side);
  2314. DView->SaveSelection();
  2315. DView->SaveSelectedNames();
  2316. }
  2317. UpdateCopyParamCounters(CopyParam);
  2318. std::unique_ptr<TStringList> TransferResumeList(new TStringList());
  2319. DebugAssert(FTransferResumeList == NULL);
  2320. FTransferResumeList =
  2321. Terminal->IsCapable[fcMoveToQueue] ? TransferResumeList.get() : NULL;
  2322. FMoveToQueue = false;
  2323. int Params = FLAGMASK(Operation == foMove, cpDelete);
  2324. try
  2325. {
  2326. TStrings * PermanentFileList;
  2327. std::unique_ptr<TStrings> PermanentFileListOwner;
  2328. try
  2329. {
  2330. if (Side == osLocal)
  2331. {
  2332. PermanentFileList = FileList;
  2333. Params |= FLAGMASK(Temp, cpTemporary);
  2334. Terminal->CopyToRemote(FileList, TargetDirectory, &CopyParam, Params, NULL);
  2335. if (Operation == foMove)
  2336. {
  2337. ReloadLocalDirectory();
  2338. if (DView != NULL)
  2339. {
  2340. DView->RestoreSelection();
  2341. }
  2342. SelectionRestored = true;
  2343. }
  2344. }
  2345. else
  2346. {
  2347. // Clone the file list as it may refer to current directory files,
  2348. // which get destroyed, when the source directory is reloaded after foMove operation.
  2349. // We should actually clone the file list for whole ExecuteFileOperation to protect against reloads.
  2350. // But for a hotfix, we are not going to do such a big change.
  2351. PermanentFileListOwner.reset(TRemoteFileList::CloneStrings(FileList));
  2352. PermanentFileList = PermanentFileListOwner.get();
  2353. try
  2354. {
  2355. Terminal->CopyToLocal(
  2356. FileList, TargetDirectory, &CopyParam, Params, NULL);
  2357. }
  2358. __finally
  2359. {
  2360. if (Operation == foMove)
  2361. {
  2362. if (DView != NULL)
  2363. {
  2364. DView->RestoreSelection();
  2365. }
  2366. SelectionRestored = true;
  2367. }
  2368. ReloadLocalDirectory(TargetDirectory);
  2369. }
  2370. }
  2371. }
  2372. catch (EAbort &)
  2373. {
  2374. if (FMoveToQueue)
  2375. {
  2376. Params |=
  2377. (CopyParam.QueueNoConfirmation ? cpNoConfirmation : 0);
  2378. DebugAssert(CopyParam.TransferSkipList == NULL);
  2379. DebugAssert(CopyParam.TransferResumeFile.IsEmpty());
  2380. if (TransferResumeList->Count > 0)
  2381. {
  2382. CopyParam.TransferResumeFile = TransferResumeList->Strings[TransferResumeList->Count - 1];
  2383. TransferResumeList->Delete(TransferResumeList->Count - 1);
  2384. }
  2385. CopyParam.TransferSkipList = TransferResumeList.release();
  2386. // not really needed, just to keep it consistent with TransferResumeList
  2387. FTransferResumeList = NULL;
  2388. FMoveToQueue = false;
  2389. Configuration->Usage->Inc("MovesToBackground");
  2390. AddQueueItem(Queue, Direction, PermanentFileList, TargetDirectory, CopyParam, Params);
  2391. ClearTransferSourceSelection(Direction);
  2392. }
  2393. throw;
  2394. }
  2395. }
  2396. __finally
  2397. {
  2398. if (!SelectionRestored && (DView != NULL))
  2399. {
  2400. DView->DiscardSavedSelection();
  2401. }
  2402. FTransferResumeList = NULL;
  2403. }
  2404. }
  2405. }
  2406. else if (Operation == foRename)
  2407. {
  2408. DebugAssert(DirView(Side)->ItemFocused);
  2409. DirView(Side)->ItemFocused->EditCaption();
  2410. Result = true;
  2411. }
  2412. else if (Operation == foDelete)
  2413. {
  2414. DebugAssert(FileList->Count);
  2415. // We deliberately do not toggle alternative flag (Param), but use OR,
  2416. // because the Param is set only when command is invoked using Shift-Del/F8 keyboard
  2417. // shortcut of CurrentDeleteAlternativeAction
  2418. bool Alternative =
  2419. bool(Param) || UseAlternativeFunction();
  2420. bool Recycle;
  2421. if (Side == osLocal)
  2422. {
  2423. Recycle = (WinConfiguration->DeleteToRecycleBin != Alternative);
  2424. }
  2425. else
  2426. {
  2427. Recycle = (Terminal->SessionData->DeleteToRecycleBin != Alternative) &&
  2428. !Terminal->SessionData->RecycleBinPath.IsEmpty() &&
  2429. !Terminal->IsRecycledFile(FileList->Strings[0]);
  2430. }
  2431. Result =
  2432. !(Recycle ? WinConfiguration->ConfirmRecycling : WinConfiguration->ConfirmDeleting);
  2433. if (!Result)
  2434. {
  2435. UnicodeString Query;
  2436. if (FileList->Count == 1)
  2437. {
  2438. if (Side == osLocal)
  2439. {
  2440. Query = ExtractFileName(FileList->Strings[0]);
  2441. }
  2442. else
  2443. {
  2444. Query = UnixExtractFileName(FileList->Strings[0]);
  2445. }
  2446. Query = FMTLOAD(
  2447. (Recycle ? CONFIRM_RECYCLE_FILE : CONFIRM_DELETE_FILE), (Query));
  2448. }
  2449. else
  2450. {
  2451. Query = FMTLOAD(
  2452. (Recycle ? CONFIRM_RECYCLE_FILES : CONFIRM_DELETE_FILES), (FileList->Count));
  2453. }
  2454. TMessageParams Params(mpNeverAskAgainCheck);
  2455. Params.ImageName = L"Delete file";
  2456. unsigned int Answer = MessageDialog(MainInstructions(Query), qtConfirmation,
  2457. qaOK | qaCancel, HELP_DELETE_FILE, &Params);
  2458. if (Answer == qaNeverAskAgain)
  2459. {
  2460. Result = true;
  2461. if (Recycle)
  2462. {
  2463. WinConfiguration->ConfirmRecycling = false;
  2464. }
  2465. else
  2466. {
  2467. WinConfiguration->ConfirmDeleting = false;
  2468. }
  2469. }
  2470. else
  2471. {
  2472. Result = (Answer == qaOK);
  2473. }
  2474. }
  2475. if (Result)
  2476. {
  2477. DeleteFiles(Side, FileList, FLAGMASK(Alternative, dfAlternative));
  2478. }
  2479. }
  2480. else if (Operation == foSetProperties)
  2481. {
  2482. RemoteDirView->SaveSelectedNames();
  2483. Result = SetProperties(Side, FileList);
  2484. }
  2485. else if (Operation == foCustomCommand)
  2486. {
  2487. DebugAssert(Param);
  2488. DebugAssert(Side == osRemote);
  2489. RemoteDirView->SaveSelectedNames();
  2490. const TCustomCommandType * Command = static_cast<const TCustomCommandType*>(Param);
  2491. CustomCommand(FileList, *Command, NULL);
  2492. Result = true;
  2493. }
  2494. else if ((Operation == foRemoteMove) || (Operation == foRemoteCopy))
  2495. {
  2496. DebugAssert(Side == osRemote);
  2497. Result = RemoteTransferFiles(FileList, NoConfirmation,
  2498. (Operation == foRemoteMove), reinterpret_cast<TTerminal *>(Param));
  2499. }
  2500. else if (Operation == foLock)
  2501. {
  2502. DebugAssert(Side == osRemote);
  2503. LockFiles(FileList, true);
  2504. Result = true;
  2505. }
  2506. else if (Operation == foUnlock)
  2507. {
  2508. DebugAssert(Side == osRemote);
  2509. LockFiles(FileList, false);
  2510. Result = true;
  2511. }
  2512. else
  2513. {
  2514. DebugFail();
  2515. }
  2516. }
  2517. __finally
  2518. {
  2519. BatchEnd(BatchStorage);
  2520. }
  2521. return Result;
  2522. }
  2523. //---------------------------------------------------------------------------
  2524. TOperationSide __fastcall TCustomScpExplorerForm::GetSide(TOperationSide Side)
  2525. {
  2526. if (Side == osCurrent)
  2527. {
  2528. Side = FCurrentSide;
  2529. }
  2530. return Side;
  2531. }
  2532. //---------------------------------------------------------------------------
  2533. bool __fastcall TCustomScpExplorerForm::ExecuteFileOperation(TFileOperation Operation,
  2534. TOperationSide Side, bool OnFocused, bool NoConfirmation, void * Param)
  2535. {
  2536. Side = GetSide(Side);
  2537. bool Result;
  2538. TStrings * FileList = DirView(Side)->CreateFileList(OnFocused, (Side == osLocal), NULL);
  2539. try
  2540. {
  2541. Result = ExecuteFileOperation(Operation, Side, FileList, NoConfirmation, Param);
  2542. }
  2543. __finally
  2544. {
  2545. delete FileList;
  2546. }
  2547. return Result;
  2548. }
  2549. //---------------------------------------------------------------------------
  2550. void __fastcall TCustomScpExplorerForm::ExecuteFileOperationCommand(
  2551. TFileOperation Operation, TOperationSide Side, bool OnFocused,
  2552. bool NoConfirmation, void * Param)
  2553. {
  2554. if (ExecuteFileOperation(Operation, Side, OnFocused, NoConfirmation, Param))
  2555. {
  2556. if ((Operation == foCopy) || (Operation == foMove))
  2557. {
  2558. if (GetSide(Side) == osLocal)
  2559. {
  2560. Configuration->Usage->Inc(L"UploadsCommand");
  2561. }
  2562. else
  2563. {
  2564. Configuration->Usage->Inc(L"DownloadsCommand");
  2565. }
  2566. }
  2567. }
  2568. }
  2569. //---------------------------------------------------------------------------
  2570. void __fastcall TCustomScpExplorerForm::ExecuteCopyOperationCommand(
  2571. TOperationSide Side, bool OnFocused, unsigned int Flags)
  2572. {
  2573. TTransferOperationParam Param;
  2574. if ((WinConfiguration->Interface != ifCommander) ||
  2575. WinConfiguration->ScpCommander.ExplorerKeyboardShortcuts)
  2576. {
  2577. Flags &= ~cocShortCutHint;
  2578. }
  2579. TCustomDirView * DView = DirView(Side);
  2580. Param.Options =
  2581. FLAGMASK(FLAGSET(Flags, cocShortCutHint), coShortCutHint) |
  2582. FLAGMASK(SelectedAllFilesInDirView(DView), coAllFiles);
  2583. if (FLAGSET(Flags, cocQueue))
  2584. {
  2585. Param.Queue = asOn;
  2586. }
  2587. else if (FLAGSET(Flags, cocNonQueue))
  2588. {
  2589. Param.Queue = asOff;
  2590. }
  2591. ExecuteFileOperationCommand(foCopy, Side, OnFocused, false, &Param);
  2592. }
  2593. //---------------------------------------------------------------------------
  2594. void __fastcall TCustomScpExplorerForm::HandleErrorList(TStringList *& ErrorList)
  2595. {
  2596. try
  2597. {
  2598. if (ErrorList->Count)
  2599. {
  2600. UnicodeString Message = MainInstructions(FMTLOAD(ERROR_LIST_COUNT, (ErrorList->Count)));
  2601. if (MessageDialog(Message, qtError,
  2602. qaOK | qaCancel, HELP_NONE) == qaOK)
  2603. {
  2604. unsigned int Answer;
  2605. int Index = 0;
  2606. do
  2607. {
  2608. DebugAssert(Index >= 0 && Index < ErrorList->Count);
  2609. TQueryButtonAlias Aliases[2];
  2610. Aliases[0].Button = qaYes;
  2611. Aliases[0].Alias = LoadStr(PREV_BUTTON);
  2612. Aliases[1].Button = qaNo;
  2613. Aliases[1].Alias = LoadStr(NEXT_BUTTON);
  2614. TMessageParams Params;
  2615. Params.Aliases = Aliases;
  2616. Params.AliasesCount = LENOF(Aliases);
  2617. Answer = MoreMessageDialog(
  2618. FMTLOAD(ERROR_LIST_NUMBER, (Index+1, ErrorList->Count, ErrorList->Strings[Index])),
  2619. dynamic_cast<TStrings *>(ErrorList->Objects[Index]), qtError,
  2620. (Index ? qaYes : 0) | (Index < ErrorList->Count - 1 ? qaNo : 0) |
  2621. qaOK, HELP_NONE, &Params);
  2622. if (Answer == qaNo)
  2623. {
  2624. Index++;
  2625. }
  2626. if (Answer == qaYes)
  2627. {
  2628. Index--;
  2629. }
  2630. }
  2631. while (Answer != qaOK);
  2632. }
  2633. }
  2634. }
  2635. __finally
  2636. {
  2637. TStrings * List = ErrorList;
  2638. ErrorList = NULL;
  2639. for (int i = 0; i < List->Count; i++)
  2640. {
  2641. delete List->Objects[i];
  2642. }
  2643. delete List;
  2644. }
  2645. }
  2646. //---------------------------------------------------------------------------
  2647. void __fastcall TCustomScpExplorerForm::ExecuteRemoteFile(
  2648. const UnicodeString & FullFileName, TRemoteFile * File, TExecuteFileBy ExecuteFileBy)
  2649. {
  2650. // needed for checking filemasks, as there's no directory object
  2651. // associated with the file object
  2652. File->FullFileName = FullFileName;
  2653. TFileMasks::TParams MaskParams;
  2654. MaskParams.Size = File->Size;
  2655. MaskParams.Modification = File->Modification;
  2656. ExecuteFile(osRemote, ExecuteFileBy, NULL, FullFileName, File, MaskParams);
  2657. }
  2658. //---------------------------------------------------------------------------
  2659. void __fastcall TCustomScpExplorerForm::EditNew(TOperationSide Side)
  2660. {
  2661. DebugAssert(!WinConfiguration->DisableOpenEdit);
  2662. Side = GetSide(Side);
  2663. TCustomDirView * DView = DirView(Side);
  2664. TListItem * FocusedItem = DView->ItemFocused;
  2665. UnicodeString Name;
  2666. if ((FocusedItem != NULL) && !DView->ItemIsParentDirectory(FocusedItem))
  2667. {
  2668. Name = DView->ItemFileName(FocusedItem);
  2669. }
  2670. else
  2671. {
  2672. Name = LoadStr(NEW_FILE);
  2673. }
  2674. UnicodeString Names = Name;
  2675. std::unique_ptr<TStrings> History(CloneStrings(CustomWinConfiguration->History[L"EditFile"]));
  2676. if (InputDialog(LoadStr(EDIT_FILE_CAPTION), LoadStr(EDIT_FILE_PROMPT), Names,
  2677. HELP_EDIT_NEW, History.get(), true))
  2678. {
  2679. while (!Names.IsEmpty())
  2680. {
  2681. Name = CutToChar(Names, FileMasksDelimiters[1], false);
  2682. CustomWinConfiguration->History[L"EditFile"] = History.get();
  2683. UnicodeString TargetFileName;
  2684. UnicodeString LocalFileName;
  2685. UnicodeString RootTempDir;
  2686. UnicodeString TempDir;
  2687. UnicodeString RemoteDirectory;
  2688. bool ExistingFile = false;
  2689. if (Side == osRemote)
  2690. {
  2691. Name = AbsolutePath(FTerminal->CurrentDirectory, Name);
  2692. TRemoteFile * File = NULL;
  2693. if (FTerminal->FileExists(Name, &File))
  2694. {
  2695. try
  2696. {
  2697. ExecuteRemoteFile(Name, File, efDefaultEditor);
  2698. ExistingFile = true;
  2699. }
  2700. __finally
  2701. {
  2702. delete File;
  2703. }
  2704. }
  2705. if (!ExistingFile)
  2706. {
  2707. RemoteDirectory = UnixExtractFilePath(Name);
  2708. TemporaryDirectoryForRemoteFiles(
  2709. RemoteDirectory, GUIConfiguration->CurrentCopyParam, TempDir, RootTempDir);
  2710. TargetFileName = UnixExtractFileName(Name);
  2711. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  2712. LocalFileName = TempDir +
  2713. // We probably do not want to trim the VMS version here
  2714. FTerminal->ChangeFileName(&CopyParam, TargetFileName, osRemote, false);
  2715. }
  2716. }
  2717. else
  2718. {
  2719. if (ExtractFilePath(Name).IsEmpty())
  2720. {
  2721. LocalFileName = IncludeTrailingBackslash(DView->PathName) + Name;
  2722. }
  2723. else
  2724. {
  2725. LocalFileName = ExpandFileName(Name);
  2726. }
  2727. TargetFileName = ExtractFileName(Name);
  2728. }
  2729. if (!ExistingFile)
  2730. {
  2731. if (!FileExists(ApiPath(LocalFileName)))
  2732. {
  2733. int File = FileCreate(ApiPath(LocalFileName));
  2734. if (File < 0)
  2735. {
  2736. if (!RootTempDir.IsEmpty())
  2737. {
  2738. RecursiveDeleteFile(ExcludeTrailingBackslash(RootTempDir), false);
  2739. }
  2740. throw Exception(FMTLOAD(CREATE_FILE_ERROR, (LocalFileName)));
  2741. }
  2742. else
  2743. {
  2744. FileClose(File);
  2745. }
  2746. }
  2747. TExecuteFileBy ExecuteFileBy = efDefaultEditor;
  2748. const TEditorData * ExternalEditor = NULL;
  2749. TFileMasks::TParams MaskParams; // size not known
  2750. ExecuteFileNormalize(ExecuteFileBy, ExternalEditor, TargetFileName,
  2751. false, MaskParams);
  2752. CustomExecuteFile(Side, ExecuteFileBy, LocalFileName, TargetFileName,
  2753. ExternalEditor, RootTempDir, RemoteDirectory);
  2754. }
  2755. }
  2756. }
  2757. }
  2758. //---------------------------------------------------------------------------
  2759. bool __fastcall TCustomScpExplorerForm::RemoteExecuteForceText(
  2760. TExecuteFileBy ExecuteFileBy, const TEditorData * ExternalEditor)
  2761. {
  2762. DebugAssert((ExecuteFileBy == efExternalEditor) ==
  2763. ((ExternalEditor != NULL) && (ExternalEditor->Editor == edExternal)));
  2764. DebugAssert(ExecuteFileBy != efDefaultEditor);
  2765. return
  2766. ((ExecuteFileBy == efInternalEditor)) ||
  2767. ((ExecuteFileBy == efExternalEditor) && ExternalEditor->ExternalEditorText);
  2768. }
  2769. //---------------------------------------------------------------------------
  2770. void __fastcall TCustomScpExplorerForm::CustomExecuteFile(TOperationSide Side,
  2771. TExecuteFileBy ExecuteFileBy, UnicodeString FileName, UnicodeString OriginalFileName,
  2772. const TEditorData * ExternalEditor, UnicodeString LocalRootDirectory,
  2773. UnicodeString RemoteDirectory)
  2774. {
  2775. DebugAssert(!WinConfiguration->DisableOpenEdit);
  2776. DebugAssert((ExecuteFileBy == efExternalEditor) ==
  2777. ((ExternalEditor != NULL) && (ExternalEditor->Editor == edExternal)));
  2778. DebugAssert(ExecuteFileBy != efDefaultEditor);
  2779. Side = GetSide(Side);
  2780. std::unique_ptr<TEditedFileData> Data(new TEditedFileData);
  2781. if (Side == osRemote)
  2782. {
  2783. Data->Terminal = Terminal;
  2784. Data->Queue = Queue;
  2785. Data->SessionData = CloneCurrentSessionData();
  2786. Data->ForceText = RemoteExecuteForceText(ExecuteFileBy, ExternalEditor);
  2787. Data->RemoteDirectory = RemoteDirectory;
  2788. Data->SessionName = Terminal->SessionData->SessionName;
  2789. Data->LocalRootDirectory = LocalRootDirectory;
  2790. Data->OriginalFileName = OriginalFileName;
  2791. Data->Command = L""; // will be changed later for external editor
  2792. }
  2793. if (ExecuteFileBy == efInternalEditor)
  2794. {
  2795. if (Side == osRemote)
  2796. {
  2797. UnicodeString Caption = UnixIncludeTrailingBackslash(RemoteDirectory) + OriginalFileName +
  2798. L" - " + Terminal->SessionData->SessionName;
  2799. TForm * Editor;
  2800. try
  2801. {
  2802. TNotifyEvent OnFileChanged = NULL;
  2803. TNotifyEvent OnSaveAll = NULL;
  2804. TAnyModifiedEvent OnAnyModified = NULL;
  2805. // Edited files are uploaded in background queue, what we do not support with encrypted files
  2806. if (Terminal->IsCapable[fsBackgroundTransfers])
  2807. {
  2808. OnFileChanged = FEditorManager->FileChanged;
  2809. OnSaveAll = SaveAllInternalEditors;
  2810. OnAnyModified = AnyInternalEditorModified;
  2811. }
  2812. Editor = ShowEditorForm(FileName, this, OnFileChanged,
  2813. FEditorManager->FileReload, FEditorManager->FileClosed,
  2814. OnSaveAll, OnAnyModified,
  2815. Caption, FStandaloneEditing, SessionColor, Terminal->SessionData->InternalEditorEncoding);
  2816. }
  2817. catch(...)
  2818. {
  2819. if (!LocalRootDirectory.IsEmpty())
  2820. {
  2821. RecursiveDeleteFile(ExcludeTrailingBackslash(LocalRootDirectory), false);
  2822. }
  2823. throw;
  2824. }
  2825. FEditorManager->AddFileInternal(FileName, Data.release(), Editor);
  2826. }
  2827. else
  2828. {
  2829. DebugAssert(!FStandaloneEditing);
  2830. TForm * Editor =
  2831. ShowEditorForm(FileName, this, NULL, NULL, LocalEditorClosed,
  2832. SaveAllInternalEditors, AnyInternalEditorModified,
  2833. L"", false, SessionColor, -1);
  2834. FLocalEditors->Add(Editor);
  2835. }
  2836. }
  2837. else
  2838. {
  2839. HANDLE Process;
  2840. if (ExecuteFileBy == efExternalEditor)
  2841. {
  2842. UnicodeString Program, Params, Dir;
  2843. Data->Command = ExternalEditor->ExternalEditor;
  2844. ReformatFileNameCommand(Data->Command);
  2845. SplitCommand(Data->Command, Program, Params, Dir);
  2846. Params = ExpandFileNameCommand(Params, FileName);
  2847. Program = ExpandEnvironmentVariables(Program);
  2848. if (!ExecuteShell(Program, Params, Process))
  2849. {
  2850. throw EOSExtException(FMTLOAD(EDITOR_ERROR, (Program)));
  2851. }
  2852. }
  2853. else
  2854. {
  2855. DebugAssert(Side == osRemote);
  2856. if (!ExecuteShell(FileName, L"", Process))
  2857. {
  2858. throw EOSExtException(FMTLOAD(EXECUTE_FILE_ERROR, (FileName)));
  2859. }
  2860. }
  2861. if ((Side == osLocal) ||
  2862. ((ExecuteFileBy == efShell) &&
  2863. !WinConfiguration->Editor.SDIShellEditor) ||
  2864. ((ExecuteFileBy == efExternalEditor) &&
  2865. !ExternalEditor->SDIExternalEditor))
  2866. {
  2867. // no need for handle
  2868. if (Process != NULL)
  2869. {
  2870. DebugCheck(CloseHandle(Process));
  2871. }
  2872. Process = INVALID_HANDLE_VALUE;
  2873. }
  2874. else
  2875. {
  2876. if (Process == NULL)
  2877. {
  2878. throw ExtException(LoadStr(OPEN_FILE_NO_PROCESS2), L"", HELP_OPEN_FILE_NO_PROCESS);
  2879. }
  2880. }
  2881. if (Side == osRemote)
  2882. {
  2883. FEditorManager->AddFileExternal(FileName, Data.release(), Process);
  2884. }
  2885. }
  2886. }
  2887. //---------------------------------------------------------------------------
  2888. void __fastcall TCustomScpExplorerForm::SaveInternalEditor(
  2889. const UnicodeString /*FileName*/, TEditedFileData * /*Data*/, TObject * Token,
  2890. void * /*Arg*/)
  2891. {
  2892. if (Token != NULL)
  2893. {
  2894. EditorFormFileSave(static_cast<TForm *>(Token));
  2895. }
  2896. }
  2897. //---------------------------------------------------------------------------
  2898. void __fastcall TCustomScpExplorerForm::SaveAllInternalEditors(TObject * /*Sender*/)
  2899. {
  2900. for (int Index = 0; Index < FLocalEditors->Count; Index++)
  2901. {
  2902. EditorFormFileSave(static_cast<TForm *>(FLocalEditors->Items[Index]));
  2903. }
  2904. FEditorManager->ProcessFiles(SaveInternalEditor, NULL);
  2905. }
  2906. //---------------------------------------------------------------------------
  2907. void __fastcall TCustomScpExplorerForm::InternalEditorModified(
  2908. const UnicodeString /*FileName*/, TEditedFileData * /*Data*/, TObject * Token,
  2909. void * Arg)
  2910. {
  2911. if ((Token != NULL) &&
  2912. IsEditorFormModified(static_cast<TForm *>(Token)))
  2913. {
  2914. *static_cast<bool *>(Arg) = true;
  2915. }
  2916. }
  2917. //---------------------------------------------------------------------------
  2918. void __fastcall TCustomScpExplorerForm::AnyInternalEditorModified(
  2919. TObject * /*Sender*/, bool & Modified)
  2920. {
  2921. for (int Index = 0; !Modified && (Index < FLocalEditors->Count); Index++)
  2922. {
  2923. if (IsEditorFormModified(static_cast<TForm *>(FLocalEditors->Items[Index])))
  2924. {
  2925. Modified = true;
  2926. }
  2927. }
  2928. if (!Modified)
  2929. {
  2930. FEditorManager->ProcessFiles(InternalEditorModified, &Modified);
  2931. }
  2932. }
  2933. //---------------------------------------------------------------------------
  2934. void __fastcall TCustomScpExplorerForm::LocalEditorClosed(TObject * Sender, bool /*Forced*/)
  2935. {
  2936. DebugCheck(FLocalEditors->Extract(Sender) >= 0);
  2937. }
  2938. //---------------------------------------------------------------------------
  2939. void __fastcall TCustomScpExplorerForm::TemporaryDirectoryForRemoteFiles(
  2940. UnicodeString RemoteDirectory, TCopyParamType CopyParam,
  2941. UnicodeString & Result, UnicodeString & RootDirectory)
  2942. {
  2943. if (!WinConfiguration->TemporaryDirectoryDeterministic)
  2944. {
  2945. RootDirectory = IncludeTrailingBackslash(WinConfiguration->TemporaryDir());
  2946. Result = RootDirectory;
  2947. }
  2948. else
  2949. {
  2950. RootDirectory = L"";
  2951. Result = WinConfiguration->ExpandedTemporaryDirectory();
  2952. Result = IncludeTrailingBackslash(Result);
  2953. }
  2954. if (WinConfiguration->TemporaryDirectoryAppendSession)
  2955. {
  2956. Result = IncludeTrailingBackslash(Result + MakeValidFileName(Terminal->SessionData->SessionName));
  2957. }
  2958. if (WinConfiguration->TemporaryDirectoryAppendPath)
  2959. {
  2960. if (!RemoteDirectory.IsEmpty() && (RemoteDirectory[1] == L'/'))
  2961. {
  2962. RemoteDirectory.Delete(1, 1);
  2963. }
  2964. Result = IncludeTrailingBackslash(Result + CopyParam.ValidLocalPath(FromUnixPath(RemoteDirectory)));
  2965. }
  2966. if (!ForceDirectories(ApiPath(Result)))
  2967. {
  2968. throw EOSExtException(FMTLOAD(CREATE_TEMP_DIR_ERROR, (Result)));
  2969. }
  2970. }
  2971. //---------------------------------------------------------------------------
  2972. void __fastcall TCustomScpExplorerForm::TemporarilyDownloadFiles(
  2973. TStrings * FileList, bool ForceText, UnicodeString & RootTempDir, UnicodeString & TempDir,
  2974. bool AllFiles, bool GetTargetNames, bool AutoOperation)
  2975. {
  2976. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  2977. if (ForceText)
  2978. {
  2979. CopyParam.TransferMode = tmAscii;
  2980. }
  2981. TemporaryFileCopyParam(CopyParam);
  2982. if (AllFiles)
  2983. {
  2984. CopyParam.IncludeFileMask = TFileMasks();
  2985. }
  2986. if (TempDir.IsEmpty())
  2987. {
  2988. TemporaryDirectoryForRemoteFiles(FTerminal->CurrentDirectory, CopyParam, TempDir, RootTempDir);
  2989. }
  2990. DebugAssert(!FAutoOperation);
  2991. FAutoOperation = AutoOperation;
  2992. Terminal->ExceptionOnFail = true;
  2993. try
  2994. {
  2995. try
  2996. {
  2997. // turn off confirmations, as for MDI editors we may possibly download
  2998. // the same file over
  2999. Terminal->CopyToLocal(
  3000. FileList, TempDir, &CopyParam, cpNoConfirmation | cpTemporary, NULL);
  3001. if (GetTargetNames)
  3002. {
  3003. for (int i = 0; i < FileList->Count; i++)
  3004. {
  3005. FileList->Strings[i] =
  3006. Terminal->ChangeFileName(&CopyParam, UnixExtractFileName(FileList->Strings[i]), osRemote, false);
  3007. }
  3008. }
  3009. }
  3010. catch(...)
  3011. {
  3012. if (!RootTempDir.IsEmpty())
  3013. {
  3014. RecursiveDeleteFile(ExcludeTrailingBackslash(RootTempDir), false);
  3015. }
  3016. throw;
  3017. }
  3018. }
  3019. __finally
  3020. {
  3021. FAutoOperation = false;
  3022. Terminal->ExceptionOnFail = false;
  3023. }
  3024. }
  3025. //---------------------------------------------------------------------------
  3026. void __fastcall TCustomScpExplorerForm::EditorAutoConfig()
  3027. {
  3028. // Do not waste time checking for default editor list the next time,
  3029. // or testing if default editor is not notepad.
  3030. bool TryNextTime = false;
  3031. UnicodeString UsageState;
  3032. if (!WinConfiguration->EditorList->IsDefaultList())
  3033. {
  3034. UsageState = "H";
  3035. }
  3036. else
  3037. {
  3038. UnicodeString Executable;
  3039. UnicodeString ExecutableDescription;
  3040. if (DetectSystemExternalEditor(false, Executable, ExecutableDescription, UsageState, TryNextTime))
  3041. {
  3042. UnicodeString Message =
  3043. FMTLOAD(EDITOR_AUTO_CONFIG2, (ExecutableDescription, ExecutableDescription));
  3044. unsigned int Answer =
  3045. MessageDialog(Message, qtConfirmation, qaOK | qaCancel, HELP_EDITOR_AUTO_CONFIG);
  3046. if (Answer != qaOK)
  3047. {
  3048. UsageState = "R";
  3049. }
  3050. else
  3051. {
  3052. UsageState = "A";
  3053. TEditorData EditorData;
  3054. EditorData.Editor = edExternal;
  3055. EditorData.ExternalEditor = FormatCommand(Executable, L"");
  3056. EditorData.DecideExternalEditorText();
  3057. TEditorList EditorList;
  3058. EditorList = *WinConfiguration->EditorList;
  3059. EditorList.Insert(0, new TEditorPreferences(EditorData));
  3060. WinConfiguration->EditorList = &EditorList;
  3061. }
  3062. }
  3063. }
  3064. WinConfiguration->OfferedEditorAutoConfig = !TryNextTime;
  3065. WinConfiguration->Usage->Set(L"EditorAutoConfig", UsageState);
  3066. }
  3067. //---------------------------------------------------------------------------
  3068. void __fastcall TCustomScpExplorerForm::ExecuteFileNormalize(
  3069. TExecuteFileBy & ExecuteFileBy, const TEditorData *& ExternalEditor,
  3070. const UnicodeString & FileName, bool Local, const TFileMasks::TParams & MaskParams)
  3071. {
  3072. if (ExecuteFileBy == efDefaultEditor)
  3073. {
  3074. if (!WinConfiguration->OfferedEditorAutoConfig)
  3075. {
  3076. EditorAutoConfig();
  3077. }
  3078. const TEditorPreferences * Editor =
  3079. WinConfiguration->DefaultEditorForFile(FileName, Local, MaskParams);
  3080. if ((Editor == NULL) || (Editor->Data->Editor == edInternal))
  3081. {
  3082. ExecuteFileBy = efInternalEditor;
  3083. ExternalEditor = NULL;
  3084. }
  3085. else if (Editor->Data->Editor == edOpen)
  3086. {
  3087. ExecuteFileBy = efShell;
  3088. ExternalEditor = NULL;
  3089. }
  3090. else
  3091. {
  3092. ExecuteFileBy = efExternalEditor;
  3093. ExternalEditor = Editor->Data;
  3094. }
  3095. }
  3096. }
  3097. //---------------------------------------------------------------------------
  3098. void __fastcall TCustomScpExplorerForm::ExecuteFile(TOperationSide Side,
  3099. TExecuteFileBy ExecuteFileBy, const TEditorData * ExternalEditor,
  3100. UnicodeString FullFileName, TObject * Object, const TFileMasks::TParams & MaskParams)
  3101. {
  3102. UnicodeString OriginalFileName;
  3103. UnicodeString LocalRootDirectory;
  3104. UnicodeString RemoteDirectory;
  3105. ExecuteFileNormalize(ExecuteFileBy, ExternalEditor, FullFileName,
  3106. (Side == osLocal), MaskParams);
  3107. UnicodeString Counter;
  3108. UnicodeString LocalFileName;
  3109. bool Handled = false;
  3110. if (Side == osRemote)
  3111. {
  3112. // We need to trim VMS version here, so that we use name without version
  3113. // when uploading back to create a new version of the file
  3114. OriginalFileName = FTerminal->GetBaseFileName(UnixExtractFileName(FullFileName));
  3115. RemoteDirectory = UnixExtractFilePath(FullFileName);
  3116. TObject * Token = NULL;
  3117. UnicodeString LocalDirectory;
  3118. if (!FEditorManager->CanAddFile(RemoteDirectory, OriginalFileName,
  3119. Terminal->SessionData->SessionName, Token, LocalRootDirectory,
  3120. LocalDirectory))
  3121. {
  3122. if (Token != NULL)
  3123. {
  3124. TForm * Form = dynamic_cast<TForm *>(Token);
  3125. if (Form->WindowState == wsMinimized)
  3126. {
  3127. ShowWindow(Form->Handle, SW_RESTORE);
  3128. }
  3129. else
  3130. {
  3131. Form->SetFocus();
  3132. }
  3133. Handled = true;
  3134. }
  3135. else
  3136. {
  3137. throw Exception(FMTLOAD(ALREADY_EDITED_EXTERNALLY_OR_UPLOADED, (OriginalFileName)));
  3138. }
  3139. }
  3140. if (!Handled)
  3141. {
  3142. TStringList * FileList1 = new TStringList();
  3143. try
  3144. {
  3145. FileList1->AddObject(FullFileName, Object);
  3146. TemporarilyDownloadFiles(FileList1,
  3147. RemoteExecuteForceText(ExecuteFileBy, ExternalEditor),
  3148. LocalRootDirectory, LocalDirectory, true, true, true);
  3149. LocalFileName = LocalDirectory + FileList1->Strings[0];
  3150. }
  3151. __finally
  3152. {
  3153. delete FileList1;
  3154. }
  3155. switch (ExecuteFileBy)
  3156. {
  3157. case efShell:
  3158. Counter = "RemoteFilesExecuted";
  3159. break;
  3160. case efInternalEditor:
  3161. Counter = "RemoteFilesOpenedInInternalEditor";
  3162. break;
  3163. case efExternalEditor:
  3164. Counter = "RemoteFilesOpenedInExternalEditor";
  3165. break;
  3166. default:
  3167. DebugFail();
  3168. }
  3169. }
  3170. }
  3171. else
  3172. {
  3173. LocalFileName = FullFileName;
  3174. OriginalFileName = ExtractFileName(FullFileName);
  3175. switch (ExecuteFileBy)
  3176. {
  3177. case efShell:
  3178. Counter = "LocalFilesExecuted";
  3179. break;
  3180. case efInternalEditor:
  3181. Counter = "LocalFilesOpenedInInternalEditor";
  3182. break;
  3183. case efExternalEditor:
  3184. Counter = "LocalFilesOpenedInExternalEditor";
  3185. break;
  3186. default:
  3187. DebugFail();
  3188. }
  3189. }
  3190. if (!Handled)
  3191. {
  3192. Configuration->Usage->Inc(Counter);
  3193. CustomExecuteFile(Side, ExecuteFileBy, LocalFileName, OriginalFileName,
  3194. ExternalEditor, LocalRootDirectory, RemoteDirectory);
  3195. }
  3196. }
  3197. //---------------------------------------------------------------------------
  3198. void __fastcall TCustomScpExplorerForm::ExecuteFile(TOperationSide Side,
  3199. TExecuteFileBy ExecuteFileBy, const TEditorData * ExternalEditor,
  3200. bool AllSelected, bool OnFocused)
  3201. {
  3202. DebugAssert(!WinConfiguration->DisableOpenEdit);
  3203. DebugAssert((ExecuteFileBy == efExternalEditor) ==
  3204. ((ExternalEditor != NULL) && (ExternalEditor->Editor == edExternal)));
  3205. Side = GetSide(Side);
  3206. TCustomDirView * DView = DirView(Side);
  3207. TStrings * FileList = AllSelected ?
  3208. DView->CreateFileList(OnFocused, Side == osLocal) :
  3209. DView->CreateFocusedFileList(Side == osLocal);
  3210. try
  3211. {
  3212. DebugAssert(AllSelected || (FileList->Count == 1));
  3213. for (int i = 0; i < FileList->Count; i++)
  3214. {
  3215. UnicodeString ListFileName = FileList->Strings[i];
  3216. UnicodeString FileNameOnly = (Side == osRemote) ?
  3217. UnixExtractFileName(ListFileName) : ExtractFileName(ListFileName);
  3218. TListItem * Item = DView->FindFileItem(FileNameOnly);
  3219. if (!DView->ItemIsDirectory(Item))
  3220. {
  3221. UnicodeString FullFileName;
  3222. if (Side == osRemote)
  3223. {
  3224. FullFileName = RemoteDirView->Path + ListFileName;
  3225. }
  3226. else
  3227. {
  3228. FullFileName = ListFileName;
  3229. }
  3230. TFileMasks::TParams MaskParams;
  3231. MaskParams.Size = DView->ItemFileSize(Item);
  3232. TDateTimePrecision Precision;
  3233. MaskParams.Modification = DView->ItemFileTime(Item, Precision);
  3234. ExecuteFile(Side, ExecuteFileBy, ExternalEditor, FullFileName,
  3235. FileList->Objects[i], MaskParams);
  3236. }
  3237. }
  3238. }
  3239. __finally
  3240. {
  3241. delete FileList;
  3242. }
  3243. }
  3244. //---------------------------------------------------------------------------
  3245. void __fastcall TCustomScpExplorerForm::TemporaryFileCopyParam(TCopyParamType & CopyParam)
  3246. {
  3247. // do not forget to add additional options to TemporarilyDownloadFiles, and AS
  3248. CopyParam.FileNameCase = ncNoChange;
  3249. CopyParam.PreserveRights = false;
  3250. CopyParam.PreserveReadOnly = false;
  3251. CopyParam.ReplaceInvalidChars = true;
  3252. CopyParam.IncludeFileMask = TFileMasks();
  3253. CopyParam.NewerOnly = false;
  3254. CopyParam.FileMask = L"";
  3255. }
  3256. //---------------------------------------------------------------------------
  3257. void __fastcall TCustomScpExplorerForm::ExecutedFileChanged(const UnicodeString FileName,
  3258. TEditedFileData * Data, HANDLE UploadCompleteEvent)
  3259. {
  3260. TTerminalManager * Manager = TTerminalManager::Instance();
  3261. if ((Data->Terminal == NULL) || !Data->Terminal->Active)
  3262. {
  3263. if (!NonVisualDataModule->Busy)
  3264. {
  3265. UnicodeString FileNameOnly = ExtractFileName(FileName);
  3266. UnicodeString EditInactiveSessionReopenAcceptedCounter = L"EditInactiveSessionReopenAccepted";
  3267. UnicodeString EditInactiveSessionReopenRejectedCounter = L"EditInactiveSessionReopenRejected";
  3268. if (Data->Terminal == NULL)
  3269. {
  3270. TTerminal * SameSiteTerminal = Manager->FindActiveTerminalForSite(Data->SessionData);
  3271. if (SameSiteTerminal != NULL)
  3272. {
  3273. UnicodeString Message =
  3274. FMTLOAD(EDIT_SESSION_REATTACH,
  3275. (FileNameOnly, Data->SessionName, FileNameOnly));
  3276. if (MessageDialog(Message, qtConfirmation, qaOK | qaCancel) == qaOK)
  3277. {
  3278. Data->Terminal = Terminal;
  3279. Data->Queue = Manager->FindQueueForTerminal(Terminal);
  3280. Data->SessionName = Terminal->SessionData->SessionName;
  3281. // We might also overwrite session data
  3282. Configuration->Usage->Inc(EditInactiveSessionReopenAcceptedCounter);
  3283. }
  3284. else
  3285. {
  3286. Configuration->Usage->Inc(EditInactiveSessionReopenRejectedCounter);
  3287. Abort();
  3288. }
  3289. }
  3290. }
  3291. // foreground session should reconnect itself
  3292. else if (Terminal != Data->Terminal)
  3293. {
  3294. UnicodeString Message =
  3295. MainInstructions(
  3296. FMTLOAD(EDIT_SESSION_RECONNECT, (Data->SessionName, FileNameOnly)));
  3297. if (MessageDialog(Message, qtConfirmation, qaOK | qaCancel) == qaOK)
  3298. {
  3299. Manager->SetActiveTerminalWithAutoReconnect(Data->Terminal);
  3300. Configuration->Usage->Inc(EditInactiveSessionReopenAcceptedCounter);
  3301. }
  3302. else
  3303. {
  3304. Configuration->Usage->Inc(EditInactiveSessionReopenRejectedCounter);
  3305. Abort();
  3306. }
  3307. }
  3308. }
  3309. if ((Data->Terminal == NULL) || !Data->Terminal->Active)
  3310. {
  3311. Configuration->Usage->Inc(L"EditInactiveSession");
  3312. // Prevent this when not idle (!NonVisualDataModule->Busy)?
  3313. throw Exception(FMTLOAD(EDIT_SESSION_CLOSED2,
  3314. (ExtractFileName(FileName), Data->SessionName)));
  3315. }
  3316. }
  3317. TStrings * FileList = new TStringList();
  3318. try
  3319. {
  3320. FileList->Add(FileName);
  3321. // consider using the same settings (preset) as when the file was downloaded
  3322. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  3323. TemporaryFileCopyParam(CopyParam);
  3324. if (Data->ForceText)
  3325. {
  3326. CopyParam.TransferMode = tmAscii;
  3327. }
  3328. // so i do not need to worry if masking algorithm works in all cases
  3329. // ("" means "copy file name", no masking is actually done)
  3330. if (ExtractFileName(FileName) == Data->OriginalFileName)
  3331. {
  3332. CopyParam.FileMask = L"";
  3333. }
  3334. else
  3335. {
  3336. CopyParam.FileMask = DelimitFileNameMask(Data->OriginalFileName);
  3337. }
  3338. DebugAssert(Data->Queue != NULL);
  3339. int Params = cpNoConfirmation | cpTemporary;
  3340. TQueueItem * QueueItem = new TUploadQueueItem(Data->Terminal, FileList,
  3341. Data->RemoteDirectory, &CopyParam, Params, true, false);
  3342. QueueItem->CompleteEvent = UploadCompleteEvent;
  3343. AddQueueItem(Data->Queue, QueueItem, Data->Terminal);
  3344. }
  3345. __finally
  3346. {
  3347. delete FileList;
  3348. }
  3349. }
  3350. //---------------------------------------------------------------------------
  3351. void __fastcall TCustomScpExplorerForm::ExecutedFileReload(
  3352. const UnicodeString FileName, const TEditedFileData * Data)
  3353. {
  3354. // Sanity check, we should not be busy otherwise user would not be able to click Reload button.
  3355. DebugAssert(!NonVisualDataModule->Busy);
  3356. if ((Data->Terminal == NULL) || !Data->Terminal->Active)
  3357. {
  3358. throw Exception(FMTLOAD(EDIT_SESSION_CLOSED_RELOAD,
  3359. (ExtractFileName(FileName), Data->SessionName)));
  3360. }
  3361. TTerminal * PrevTerminal = TTerminalManager::Instance()->ActiveTerminal;
  3362. TTerminalManager::Instance()->ActiveTerminal = Data->Terminal;
  3363. NonVisualDataModule->StartBusy();
  3364. try
  3365. {
  3366. std::unique_ptr<TRemoteFile> File;
  3367. UnicodeString RemoteFileName =
  3368. UnixIncludeTrailingBackslash(Data->RemoteDirectory) + Data->OriginalFileName;
  3369. FTerminal->ExceptionOnFail = true;
  3370. try
  3371. {
  3372. TRemoteFile * AFile = NULL;
  3373. FTerminal->ReadFile(RemoteFileName, AFile);
  3374. File.reset(AFile);
  3375. if (!File->HaveFullFileName)
  3376. {
  3377. File->FullFileName = RemoteFileName;
  3378. }
  3379. }
  3380. __finally
  3381. {
  3382. FTerminal->ExceptionOnFail = false;
  3383. }
  3384. std::unique_ptr<TStrings> FileList(new TStringList());
  3385. FileList->AddObject(RemoteFileName, File.get());
  3386. UnicodeString RootTempDir = Data->LocalRootDirectory;
  3387. UnicodeString TempDir = ExtractFilePath(FileName);
  3388. TemporarilyDownloadFiles(FileList.get(), Data->ForceText, RootTempDir,
  3389. TempDir, true, true, true);
  3390. // sanity check, the target file name should be still the same
  3391. DebugAssert(ExtractFileName(FileName) == FileList->Strings[0]);
  3392. }
  3393. __finally
  3394. {
  3395. NonVisualDataModule->EndBusy();
  3396. // it actually may not exist anymore...
  3397. TTerminalManager::Instance()->ActiveTerminal = PrevTerminal;
  3398. }
  3399. }
  3400. //---------------------------------------------------------------------------
  3401. void __fastcall TCustomScpExplorerForm::ExecutedFileEarlyClosed(
  3402. const TEditedFileData * Data, bool & KeepOpen)
  3403. {
  3404. // Command is set for external editors only (not for "shell" open).
  3405. if (!Data->Command.IsEmpty())
  3406. {
  3407. bool AnyFound = false;
  3408. bool AnyMDI = false;
  3409. bool AnyNonMDI = false;
  3410. bool AnyDetect = false;
  3411. TEditorList * EditorList = new TEditorList();
  3412. try
  3413. {
  3414. *EditorList = *WinConfiguration->EditorList;
  3415. for (int i = 0; i < EditorList->Count; i++)
  3416. {
  3417. const TEditorPreferences * Editor = EditorList->Editors[i];
  3418. if ((Editor->Data->Editor == edExternal) &&
  3419. (Editor->Data->ExternalEditor == Data->Command))
  3420. {
  3421. AnyFound = true;
  3422. if (Editor->Data->SDIExternalEditor)
  3423. {
  3424. AnyNonMDI = true;
  3425. if (Editor->Data->DetectMDIExternalEditor)
  3426. {
  3427. AnyDetect = true;
  3428. }
  3429. }
  3430. else
  3431. {
  3432. AnyMDI = true;
  3433. }
  3434. }
  3435. }
  3436. bool EnableMDI = false;
  3437. bool DisableDetect = false;
  3438. if (AnyMDI)
  3439. {
  3440. KeepOpen = true;
  3441. if (AnyNonMDI)
  3442. {
  3443. // there is at least one instance of the editor with MDI support enabled,
  3444. // and one with disabled, enable it for all instances
  3445. EnableMDI = true;
  3446. }
  3447. }
  3448. else if (AnyFound && !AnyDetect)
  3449. {
  3450. // at least once instance found but all have MDI autodetection disabled
  3451. // => close the file (default action)
  3452. }
  3453. else
  3454. {
  3455. // no instance of the editor has MDI support enabled
  3456. TMessageParams Params;
  3457. if (AnyFound)
  3458. {
  3459. // there is at least one instance of the editor
  3460. Params.Params |= mpNeverAskAgainCheck;
  3461. }
  3462. unsigned int Answer = MessageDialog(FMTLOAD(EDITOR_EARLY_CLOSED2, (Data->OriginalFileName)), qtWarning,
  3463. qaYes | qaNo, HELP_EDITOR_EARLY_CLOSED, &Params);
  3464. switch (Answer)
  3465. {
  3466. case qaNeverAskAgain:
  3467. DisableDetect = true;
  3468. break;
  3469. case qaNo:
  3470. EnableMDI = true;
  3471. KeepOpen = true;
  3472. break;
  3473. }
  3474. }
  3475. if (AnyFound && (EnableMDI || DisableDetect))
  3476. {
  3477. bool Changed = false;
  3478. for (int i = 0; i < EditorList->Count; i++)
  3479. {
  3480. const TEditorPreferences * Editor = EditorList->Editors[i];
  3481. if ((Editor->Data->Editor == edExternal) &&
  3482. (Editor->Data->ExternalEditor == Data->Command) &&
  3483. ((EnableMDI && Editor->Data->SDIExternalEditor) ||
  3484. (DisableDetect && Editor->Data->DetectMDIExternalEditor)))
  3485. {
  3486. Changed = true;
  3487. TEditorPreferences * UpdatedEditor = new TEditorPreferences(*Editor);
  3488. if (EnableMDI)
  3489. {
  3490. UpdatedEditor->GetData()->SDIExternalEditor = false;
  3491. }
  3492. if (DisableDetect)
  3493. {
  3494. UpdatedEditor->GetData()->DetectMDIExternalEditor = false;
  3495. }
  3496. EditorList->Change(i, UpdatedEditor);
  3497. }
  3498. }
  3499. if (Changed)
  3500. {
  3501. WinConfiguration->EditorList = EditorList;
  3502. }
  3503. }
  3504. }
  3505. __finally
  3506. {
  3507. delete EditorList;
  3508. }
  3509. }
  3510. else
  3511. {
  3512. // "open" case
  3513. MessageDialog(FMTLOAD(APP_EARLY_CLOSED, (Data->OriginalFileName)), qtWarning,
  3514. qaOK, HELP_APP_EARLY_CLOSED);
  3515. }
  3516. }
  3517. //---------------------------------------------------------------------------
  3518. void __fastcall TCustomScpExplorerForm::ExecutedFileUploadComplete(TObject * Sender)
  3519. {
  3520. EditorFormFileUploadComplete(DebugNotNull(dynamic_cast<TForm *>(Sender)));
  3521. }
  3522. //---------------------------------------------------------------------------
  3523. void __fastcall TCustomScpExplorerForm::RemoteDirViewEnter(TObject * /*Sender*/)
  3524. {
  3525. SideEnter(osRemote);
  3526. }
  3527. //---------------------------------------------------------------------------
  3528. void __fastcall TCustomScpExplorerForm::RemoteDriveViewEnter(TObject * /*Sender*/)
  3529. {
  3530. MakeNextInTabOrder(RemoteDirView, RemoteDriveView);
  3531. SideEnter(osRemote);
  3532. }
  3533. //---------------------------------------------------------------------------
  3534. void __fastcall TCustomScpExplorerForm::SideEnter(TOperationSide Side)
  3535. {
  3536. FCurrentSide = Side;
  3537. if (Visible)
  3538. {
  3539. UpdateControls();
  3540. }
  3541. }
  3542. //---------------------------------------------------------------------------
  3543. void __fastcall TCustomScpExplorerForm::DeleteFiles(TOperationSide Side,
  3544. TStrings * FileList, bool Alternative)
  3545. {
  3546. TCustomDirView * DView = DirView(Side);
  3547. DView->SaveSelection();
  3548. DView->SaveSelectedNames();
  3549. DebugAssert(!FAlternativeDelete);
  3550. FAlternativeDelete = Alternative;
  3551. try
  3552. {
  3553. if (Side == osRemote)
  3554. {
  3555. DebugAssert(Terminal != NULL);
  3556. Terminal->DeleteFiles(FileList, FLAGMASK(Alternative, dfAlternative));
  3557. }
  3558. else
  3559. {
  3560. try
  3561. {
  3562. TTerminalManager::Instance()->LocalTerminal->DeleteLocalFiles(FileList, FLAGMASK(Alternative, dfAlternative));
  3563. }
  3564. __finally
  3565. {
  3566. ReloadLocalDirectory();
  3567. }
  3568. }
  3569. FAlternativeDelete = false;
  3570. }
  3571. catch(...)
  3572. {
  3573. FAlternativeDelete = false;
  3574. DView->DiscardSavedSelection();
  3575. throw;
  3576. }
  3577. DView->RestoreSelection();
  3578. }
  3579. //---------------------------------------------------------------------------
  3580. void __fastcall TCustomScpExplorerForm::LockFiles(TStrings * FileList, bool Lock)
  3581. {
  3582. DebugAssert(Terminal);
  3583. RemoteDirView->SaveSelection();
  3584. RemoteDirView->SaveSelectedNames();
  3585. try
  3586. {
  3587. if (Lock)
  3588. {
  3589. Terminal->LockFiles(FileList);
  3590. }
  3591. else
  3592. {
  3593. Terminal->UnlockFiles(FileList);
  3594. }
  3595. }
  3596. catch(...)
  3597. {
  3598. RemoteDirView->DiscardSavedSelection();
  3599. throw;
  3600. }
  3601. RemoteDirView->RestoreSelection();
  3602. }
  3603. //---------------------------------------------------------------------------
  3604. bool __fastcall TCustomScpExplorerForm::RemoteTransferDialog(TTerminal *& Session,
  3605. TStrings * FileList, UnicodeString & Target, UnicodeString & FileMask, bool & DirectCopy,
  3606. bool NoConfirmation, bool Move)
  3607. {
  3608. if (RemoteDriveView->DropTarget != NULL)
  3609. {
  3610. Target = RemoteDriveView->NodePathName(RemoteDriveView->DropTarget);
  3611. }
  3612. else if (RemoteDirView->DropTarget != NULL)
  3613. {
  3614. DebugAssert(RemoteDirView->ItemIsDirectory(RemoteDirView->DropTarget));
  3615. Target = RemoteDirView->ItemFullFileName(RemoteDirView->DropTarget);
  3616. }
  3617. else
  3618. {
  3619. Target = RemoteDirView->Path;
  3620. }
  3621. if (Session == NULL)
  3622. {
  3623. Session = TTerminalManager::Instance()->ActiveTerminal;
  3624. }
  3625. Target = UnixIncludeTrailingBackslash(Target);
  3626. if (FileList->Count == 1)
  3627. {
  3628. FileMask = DelimitFileNameMask(UnixExtractFileName(FileList->Strings[0]));
  3629. }
  3630. else
  3631. {
  3632. FileMask = AnyMask;
  3633. }
  3634. DirectCopy = FTerminal->IsCapable[fcRemoteCopy] || FTerminal->IsCapable[fcSecondaryShell];
  3635. bool Result = true;
  3636. if (!NoConfirmation)
  3637. {
  3638. bool Multi = (FileList->Count > 1);
  3639. if (Move)
  3640. {
  3641. Result = DoRemoteMoveDialog(Multi, Target, FileMask);
  3642. }
  3643. else
  3644. {
  3645. DebugAssert(Terminal != NULL);
  3646. // update Terminal->StateData->RemoteDirectory
  3647. UpdateTerminal(Terminal);
  3648. TStrings * Sessions = TTerminalManager::Instance()->TerminalList;
  3649. TStrings * Directories = new TStringList;
  3650. try
  3651. {
  3652. for (int Index = 0; Index < Sessions->Count; Index++)
  3653. {
  3654. TManagedTerminal * Terminal =
  3655. dynamic_cast<TManagedTerminal *>(Sessions->Objects[Index]);
  3656. Directories->Add(Terminal->StateData->RemoteDirectory);
  3657. }
  3658. TDirectRemoteCopy AllowDirectCopy;
  3659. if (FTerminal->IsCapable[fcRemoteCopy] || FTerminal->CommandSessionOpened)
  3660. {
  3661. DebugAssert(DirectCopy);
  3662. AllowDirectCopy = drcAllow;
  3663. }
  3664. else if (FTerminal->IsCapable[fcSecondaryShell])
  3665. {
  3666. DebugAssert(DirectCopy);
  3667. AllowDirectCopy = drcConfirmCommandSession;
  3668. }
  3669. else
  3670. {
  3671. DebugAssert(!DirectCopy);
  3672. AllowDirectCopy = drcDisallow;
  3673. }
  3674. void * ASession = Session;
  3675. Result = DoRemoteCopyDialog(Sessions, Directories, AllowDirectCopy,
  3676. Multi, ASession, Target, FileMask, DirectCopy, TTerminalManager::Instance()->ActiveTerminal);
  3677. Session = static_cast<TTerminal *>(ASession);
  3678. }
  3679. __finally
  3680. {
  3681. delete Directories;
  3682. }
  3683. }
  3684. }
  3685. return Result;
  3686. }
  3687. //---------------------------------------------------------------------------
  3688. bool __fastcall TCustomScpExplorerForm::RemoteTransferFiles(
  3689. TStrings * FileList, bool NoConfirmation, bool Move, TTerminal * Session)
  3690. {
  3691. bool DirectCopy;
  3692. UnicodeString Target, FileMask;
  3693. bool Result = RemoteTransferDialog(Session, FileList, Target, FileMask, DirectCopy, NoConfirmation, Move);
  3694. if (Result)
  3695. {
  3696. if (!Move && !DirectCopy)
  3697. {
  3698. Configuration->Usage->Inc("RemoteCopyTemp");
  3699. UnicodeString RootTempDir;
  3700. UnicodeString TempDir;
  3701. TemporarilyDownloadFiles(FileList, false, RootTempDir, TempDir, false, false, false);
  3702. TStrings * TemporaryFilesList = new TStringList();
  3703. try
  3704. {
  3705. TMakeLocalFileListParams MakeFileListParam;
  3706. MakeFileListParam.FileList = TemporaryFilesList;
  3707. MakeFileListParam.FileTimes = NULL;
  3708. MakeFileListParam.IncludeDirs = true;
  3709. MakeFileListParam.Recursive = false;
  3710. ProcessLocalDirectory(TempDir, FTerminal->MakeLocalFileList, &MakeFileListParam);
  3711. TTerminalManager::Instance()->ActiveTerminal = Session;
  3712. if (TemporaryFilesList->Count > 0)
  3713. {
  3714. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  3715. CopyParam.FileMask = FileMask;
  3716. DebugAssert(!FAutoOperation);
  3717. FAutoOperation = true;
  3718. FTerminal->CopyToRemote(TemporaryFilesList, Target, &CopyParam, cpTemporary, NULL);
  3719. }
  3720. }
  3721. __finally
  3722. {
  3723. delete TemporaryFilesList;
  3724. FAutoOperation = false;
  3725. if (!RootTempDir.IsEmpty())
  3726. {
  3727. RecursiveDeleteFile(ExcludeTrailingBackslash(RootTempDir), false);
  3728. }
  3729. }
  3730. }
  3731. else
  3732. {
  3733. RemoteDirView->SaveSelection();
  3734. RemoteDirView->SaveSelectedNames();
  3735. try
  3736. {
  3737. if (Move)
  3738. {
  3739. Configuration->Usage->Inc("RemoteMove");
  3740. Terminal->MoveFiles(FileList, Target, FileMask);
  3741. }
  3742. else
  3743. {
  3744. Configuration->Usage->Inc("RemoteCopyDirect");
  3745. DebugAssert(DirectCopy);
  3746. DebugAssert(Session == FTerminal);
  3747. if (FTerminal->IsCapable[fcRemoteCopy] ||
  3748. FTerminal->CommandSessionOpened ||
  3749. CommandSessionFallback())
  3750. {
  3751. Terminal->CopyFiles(FileList, Target, FileMask);
  3752. }
  3753. }
  3754. }
  3755. catch(...)
  3756. {
  3757. RemoteDirView->DiscardSavedSelection();
  3758. throw;
  3759. }
  3760. RemoteDirView->RestoreSelection();
  3761. }
  3762. }
  3763. return Result;
  3764. }
  3765. //---------------------------------------------------------------------------
  3766. void __fastcall TCustomScpExplorerForm::CreateRemoteDirectory(
  3767. const UnicodeString & Path, TRemoteProperties & Properties)
  3768. {
  3769. Properties.Valid = Properties.Valid << vpEncrypt;
  3770. Properties.Encrypt = GUIConfiguration->CurrentCopyParam.EncryptNewFiles;
  3771. RemoteDirView->CreateDirectoryEx(Path, &Properties);
  3772. }
  3773. //---------------------------------------------------------------------------
  3774. void __fastcall TCustomScpExplorerForm::CreateDirectory(TOperationSide Side)
  3775. {
  3776. Side = GetSide(Side);
  3777. TRemoteProperties Properties = GUIConfiguration->NewDirectoryProperties;
  3778. TRemoteProperties * AProperties = (Side == osRemote ? &Properties : NULL);
  3779. UnicodeString Name = LoadStr(NEW_FOLDER);
  3780. int AllowedChanges =
  3781. FLAGMASK((Side == osRemote) && Terminal->IsCapable[fcModeChanging], cpMode);
  3782. bool SaveSettings = false;
  3783. if (DoCreateDirectoryDialog(Name, AProperties, AllowedChanges, SaveSettings))
  3784. {
  3785. TWindowLock Lock(this);
  3786. if (Side == osRemote)
  3787. {
  3788. if (SaveSettings)
  3789. {
  3790. GUIConfiguration->NewDirectoryProperties = Properties;
  3791. }
  3792. CreateRemoteDirectory(Name, Properties);
  3793. }
  3794. else
  3795. {
  3796. DirView(Side)->CreateDirectory(Name);
  3797. }
  3798. }
  3799. }
  3800. //---------------------------------------------------------------------------
  3801. void __fastcall TCustomScpExplorerForm::HomeDirectory(TOperationSide Side)
  3802. {
  3803. TWindowLock Lock(this);
  3804. DirView(Side)->ExecuteHomeDirectory();
  3805. }
  3806. //---------------------------------------------------------------------------
  3807. void __fastcall TCustomScpExplorerForm::OpenDirectory(TOperationSide Side)
  3808. {
  3809. DoOpenDirectoryDialog(odBrowse, Side);
  3810. }
  3811. //---------------------------------------------------------------------------
  3812. bool __fastcall TCustomScpExplorerForm::OpenBookmark(TOperationSide Side, TBookmark * Bookmark)
  3813. {
  3814. UnicodeString Path = Bookmark->GetSideDirectory(Side);
  3815. bool Result = !Path.IsEmpty();
  3816. if (Result)
  3817. {
  3818. // While we might get here when the session is closed (from location profiles),
  3819. // it's not a problem as the Path setter is noop then
  3820. DirView(Side)->Path = Path;
  3821. }
  3822. return Result;
  3823. }
  3824. //---------------------------------------------------------------------------
  3825. void __fastcall TCustomScpExplorerForm::CalculateSize(
  3826. TStrings * FileList, __int64 & Size, TCalculateSizeStats & Stats,
  3827. bool & Close)
  3828. {
  3829. // terminal can be already closed (e.g. dropped connection)
  3830. if (Terminal != NULL)
  3831. {
  3832. try
  3833. {
  3834. Terminal->CalculateFilesSize(FileList, Size, 0, NULL, true, Stats);
  3835. }
  3836. catch(...)
  3837. {
  3838. if (!Terminal->Active)
  3839. {
  3840. Close = true;
  3841. }
  3842. throw;
  3843. }
  3844. }
  3845. }
  3846. //---------------------------------------------------------------------------
  3847. void __fastcall TCustomScpExplorerForm::CalculateChecksum(const UnicodeString & Alg,
  3848. TStrings * FileList, TCalculatedChecksumEvent OnCalculatedChecksum,
  3849. bool & Close)
  3850. {
  3851. // terminal can be already closed (e.g. dropped connection)
  3852. if (Terminal != NULL)
  3853. {
  3854. Configuration->Usage->Inc(L"ChecksumCalculated");
  3855. try
  3856. {
  3857. Terminal->CalculateFilesChecksum(Alg, FileList, NULL, OnCalculatedChecksum);
  3858. }
  3859. catch(...)
  3860. {
  3861. if (!Terminal->Active)
  3862. {
  3863. Close = true;
  3864. }
  3865. throw;
  3866. }
  3867. }
  3868. }
  3869. //---------------------------------------------------------------------------
  3870. bool __fastcall TCustomScpExplorerForm::SetProperties(TOperationSide Side, TStrings * FileList)
  3871. {
  3872. bool Result;
  3873. if (Side == osRemote)
  3874. {
  3875. TRemoteTokenList * GroupList = NULL;
  3876. TRemoteTokenList * UserList = NULL;
  3877. try
  3878. {
  3879. TRemoteProperties CurrentProperties;
  3880. if (Terminal->LoadFilesProperties(FileList))
  3881. {
  3882. RemoteDirView->Invalidate();
  3883. }
  3884. bool CapableGroupChanging = Terminal->IsCapable[fcGroupChanging];
  3885. bool CapableOwnerChanging = Terminal->IsCapable[fcOwnerChanging];
  3886. if (CapableGroupChanging || CapableOwnerChanging)
  3887. {
  3888. if (CapableGroupChanging)
  3889. {
  3890. GroupList = Terminal->Groups->Duplicate();
  3891. }
  3892. if (CapableOwnerChanging)
  3893. {
  3894. UserList = Terminal->Users->Duplicate();
  3895. }
  3896. TRemoteDirectory * Files = Terminal->Files;
  3897. int Count = Files->Count;
  3898. if (Count > 100)
  3899. {
  3900. Count = 100;
  3901. }
  3902. for (int Index = 0; Index < Count; Index++)
  3903. {
  3904. TRemoteFile * File = Files->Files[Index];
  3905. if (CapableGroupChanging)
  3906. {
  3907. GroupList->AddUnique(File->Group);
  3908. }
  3909. if (CapableOwnerChanging)
  3910. {
  3911. UserList->AddUnique(File->Owner);
  3912. }
  3913. }
  3914. // if we haven't collected tokens for all files in current directory,
  3915. // make sure we collect them at least for all selected files.
  3916. // (note that so far the files in FileList has to be from current direcotry)
  3917. if (Count < Files->Count)
  3918. {
  3919. for (int Index = 0; Index < FileList->Count; Index++)
  3920. {
  3921. TRemoteFile * File = (TRemoteFile *)(FileList->Objects[Index]);
  3922. if (CapableGroupChanging)
  3923. {
  3924. GroupList->AddUnique(File->Group);
  3925. }
  3926. if (CapableOwnerChanging)
  3927. {
  3928. UserList->AddUnique(File->Owner);
  3929. }
  3930. }
  3931. }
  3932. }
  3933. CurrentProperties = TRemoteProperties::CommonProperties(FileList);
  3934. int Flags = 0;
  3935. if (Terminal->IsCapable[fcModeChanging]) Flags |= cpMode;
  3936. if (CapableOwnerChanging) Flags |= cpOwner;
  3937. if (CapableGroupChanging) Flags |= cpGroup;
  3938. TCalculateChecksumEvent CalculateChecksumEvent = NULL;
  3939. if (Terminal->IsCapable[fcCalculatingChecksum])
  3940. {
  3941. CalculateChecksumEvent = CalculateChecksum;
  3942. }
  3943. std::unique_ptr<TStrings> ChecksumAlgs(new TStringList());
  3944. Terminal->GetSupportedChecksumAlgs(ChecksumAlgs.get());
  3945. TRemoteProperties NewProperties = CurrentProperties;
  3946. Result =
  3947. DoPropertiesDialog(FileList, RemoteDirView->PathName,
  3948. GroupList, UserList, ChecksumAlgs.get(), &NewProperties, Flags,
  3949. Terminal->IsCapable[fcGroupOwnerChangingByID],
  3950. CalculateSize, CalculateChecksumEvent);
  3951. if (Result)
  3952. {
  3953. NewProperties = TRemoteProperties::ChangedProperties(CurrentProperties, NewProperties);
  3954. Terminal->ChangeFilesProperties(FileList, &NewProperties);
  3955. }
  3956. }
  3957. __finally
  3958. {
  3959. delete GroupList;
  3960. delete UserList;
  3961. }
  3962. }
  3963. else
  3964. {
  3965. DirView(Side)->DisplayPropertiesMenu();
  3966. Result = true;
  3967. }
  3968. return Result;
  3969. }
  3970. //---------------------------------------------------------------------------
  3971. void __fastcall TCustomScpExplorerForm::KeyProcessed(Word & Key, TShiftState Shift)
  3972. {
  3973. if (Shift * AllKeyShiftStates() == (TShiftState() << ssAlt))
  3974. {
  3975. FIgnoreNextDialogChar = Key;
  3976. }
  3977. Key = 0;
  3978. }
  3979. //---------------------------------------------------------------------------
  3980. void __fastcall TCustomScpExplorerForm::CheckCustomCommandShortCut(
  3981. TCustomCommandList * List, Word & Key, Classes::TShiftState Shift, TShortCut KeyShortCut)
  3982. {
  3983. const TCustomCommandType * Command = List->Find(KeyShortCut);
  3984. if (Command != NULL)
  3985. {
  3986. KeyProcessed(Key, Shift);
  3987. if (CustomCommandState(*Command, false, ccltAll) > 0)
  3988. {
  3989. ExecuteFileOperationCommand(foCustomCommand, osRemote,
  3990. false, false, const_cast<TCustomCommandType *>(Command));
  3991. }
  3992. }
  3993. }
  3994. //---------------------------------------------------------------------------
  3995. void __fastcall TCustomScpExplorerForm::KeyDown(Word & Key, Classes::TShiftState Shift)
  3996. {
  3997. if (QueueView3->Focused() && (QueueView3->OnKeyDown != NULL))
  3998. {
  3999. QueueView3->OnKeyDown(QueueView3, Key, Shift);
  4000. }
  4001. if (!DirView(osCurrent)->IsEditing())
  4002. {
  4003. TShortCut KeyShortCut = ShortCut(Key, Shift);
  4004. for (int Index = 0; Index < NonVisualDataModule->ExplorerActions->ActionCount; Index++)
  4005. {
  4006. TAction * Action = (TAction *)NonVisualDataModule->ExplorerActions->Actions[Index];
  4007. if (((Action->ShortCut == KeyShortCut) ||
  4008. (Action->SecondaryShortCuts->IndexOfShortCut(KeyShortCut) >= 0)) &&
  4009. AllowedAction(Action, aaShortCut))
  4010. {
  4011. // Has to be called before the action as the dialog char is already in queue.
  4012. // So when the action consumes message queue, we already need to have the
  4013. // FIgnoreNextDialogChar set
  4014. KeyProcessed(Key, Shift);
  4015. // Reset reference to previous component (when menu/toolbar was clicked).
  4016. // Needed to detect that action was invoked by keyboard shortcut
  4017. // in TNonVisualDataModule::ExplorerActionsExecute
  4018. Action->ActionComponent = NULL;
  4019. Action->Execute();
  4020. return;
  4021. }
  4022. }
  4023. for (int i = 0; i < TTerminalManager::Instance()->Count; i++)
  4024. {
  4025. if (NonVisualDataModule->OpenSessionShortCut(i) == KeyShortCut)
  4026. {
  4027. KeyProcessed(Key, Shift);
  4028. TTerminalManager::Instance()->ActiveTerminalIndex = i;
  4029. return;
  4030. }
  4031. }
  4032. if (Key == VK_TAB && Shift.Contains(ssCtrl))
  4033. {
  4034. KeyProcessed(Key, Shift);
  4035. TTerminalManager::Instance()->CycleTerminals(!Shift.Contains(ssShift));
  4036. }
  4037. TShortCut CustomShortCut = NormalizeCustomShortCut(KeyShortCut);
  4038. if (IsCustomShortCut(CustomShortCut))
  4039. {
  4040. CheckCustomCommandShortCut(WinConfiguration->CustomCommandList, Key, Shift, CustomShortCut);
  4041. CheckCustomCommandShortCut(WinConfiguration->ExtensionList, Key, Shift, CustomShortCut);
  4042. if (WinConfiguration->SharedBookmarks != NULL)
  4043. {
  4044. TBookmark * Bookmark = WinConfiguration->SharedBookmarks->FindByShortCut(CustomShortCut);
  4045. if ((Bookmark != NULL) &&
  4046. OpenBookmark(FCurrentSide, Bookmark))
  4047. {
  4048. KeyProcessed(Key, Shift);
  4049. }
  4050. }
  4051. }
  4052. }
  4053. TForm::KeyDown(Key, Shift);
  4054. }
  4055. //---------------------------------------------------------------------------
  4056. void __fastcall TCustomScpExplorerForm::InitStatusBar()
  4057. {
  4058. const TSessionInfo & SessionInfo = Terminal->GetSessionInfo();
  4059. const TFileSystemInfo & FileSystemInfo = Terminal->GetFileSystemInfo();
  4060. TTBXStatusBar * SessionStatusBar = (TTBXStatusBar *)GetComponent(fcStatusBar);
  4061. DebugAssert(Terminal);
  4062. int Offset = SessionStatusBar->Panels->Count - SessionPanelCount;
  4063. bool SecurityEnabled = !SessionInfo.SecurityProtocolName.IsEmpty();
  4064. SessionStatusBar->Panels->Items[Offset + 0]->Enabled = SecurityEnabled;
  4065. // expanded from ?: to avoid memory leaks
  4066. if (SecurityEnabled)
  4067. {
  4068. SessionStatusBar->Panels->Items[Offset + 0]->Hint =
  4069. FMTLOAD(STATUS_SECURE, (SessionInfo.SecurityProtocolName));
  4070. }
  4071. else
  4072. {
  4073. SessionStatusBar->Panels->Items[Offset + 0]->Hint = LoadStr(STATUS_INSECURE);
  4074. }
  4075. if (FileSystemInfo.ProtocolName.IsEmpty())
  4076. {
  4077. SessionStatusBar->Panels->Items[Offset + 1]->Caption = SessionInfo.ProtocolName;
  4078. }
  4079. else
  4080. {
  4081. SessionStatusBar->Panels->Items[Offset + 1]->Caption = FileSystemInfo.ProtocolName;
  4082. }
  4083. SessionStatusBar->Panels->Items[Offset + 1]->Hint = LoadStr(STATUS_PROTOCOL_HINT);
  4084. SessionStatusBar->Panels->Items[Offset + 2]->Enabled =
  4085. (!SessionInfo.CSCompression.IsEmpty() || !SessionInfo.SCCompression.IsEmpty());
  4086. UnicodeString CSCompressionStr = DefaultStr(SessionInfo.CSCompression, LoadStr(NO_STR));
  4087. UnicodeString SCCompressionStr = DefaultStr(SessionInfo.SCCompression, LoadStr(NO_STR));
  4088. UnicodeString CompressionStr = CSCompressionStr;
  4089. if (CSCompressionStr != SCCompressionStr)
  4090. {
  4091. CompressionStr += UnicodeString(L"/") + SCCompressionStr;
  4092. }
  4093. SessionStatusBar->Panels->Items[Offset + 2]->Hint = FMTLOAD(STATUS_COMPRESSION_HINT, (CompressionStr));
  4094. SessionStatusBar->Panels->Items[Offset + 3]->Hint = LoadStr(STATUS_DURATION_HINT);
  4095. UpdateStatusBar();
  4096. }
  4097. //---------------------------------------------------------------------------
  4098. void __fastcall TCustomScpExplorerForm::UpdateStatusBar()
  4099. {
  4100. TTBXStatusBar * SessionStatusBar = (TTBXStatusBar *)GetComponent(fcStatusBar);
  4101. DebugAssert(SessionStatusBar != NULL);
  4102. if (!Terminal || !Terminal->Active || Terminal->Status < ssOpened)
  4103. {
  4104. // note: (Terminal->Status < sshReady) currently never happens here,
  4105. // so STATUS_CONNECTING is never used
  4106. SessionStatusBar->SimplePanel = true;
  4107. SessionStatusBar->SimpleText = LoadStr(
  4108. !Terminal || !Terminal->Active ? STATUS_NOT_CONNECTED : STATUS_CONNECTING);
  4109. }
  4110. else
  4111. {
  4112. DebugAssert(Terminal);
  4113. SessionStatusBar->SimplePanel = false;
  4114. const TSessionInfo & SessionInfo = Terminal->GetSessionInfo();
  4115. if (!FNote.IsEmpty())
  4116. {
  4117. SessionStatusBar->Panels->Items[0]->Caption = FNote;
  4118. }
  4119. else
  4120. {
  4121. UpdateStatusPanelText(SessionStatusBar->Panels->Items[0]);
  4122. }
  4123. SessionStatusBar->Panels->Items[0]->Hint = FNoteHints;
  4124. SessionStatusBar->Panels->Items[SessionStatusBar->Panels->Count - 1]->Caption =
  4125. FormatDateTimeSpan(Configuration->TimeFormat, Now() - SessionInfo.LoginTime);
  4126. }
  4127. }
  4128. //---------------------------------------------------------------------------
  4129. void __fastcall TCustomScpExplorerForm::UpdateStatusPanelText(TTBXStatusPanel * Panel)
  4130. {
  4131. Panel->Caption = L"";
  4132. }
  4133. //---------------------------------------------------------------------------
  4134. void __fastcall TCustomScpExplorerForm::Idle()
  4135. {
  4136. if (FShowing || FStandaloneEditing)
  4137. {
  4138. FEditorManager->Check();
  4139. // make sure that Idle is called before update queue, as it may invoke QueueEvent
  4140. // that needs to know if queue view is visible (and it may be closed after queue update)
  4141. TTerminalManager::Instance()->Idle(FDoNotIdleCurrentTerminal > 0);
  4142. }
  4143. if (FShowing)
  4144. {
  4145. if (!NonVisualDataModule->Busy &&
  4146. // Menu is opened or drag&drop is going on
  4147. (Mouse->Capture == NULL))
  4148. {
  4149. if (FRefreshRemoteDirectory)
  4150. {
  4151. if ((Terminal != NULL) && (Terminal->Status == ssOpened))
  4152. {
  4153. Terminal->RefreshDirectory();
  4154. }
  4155. FRefreshRemoteDirectory = false;
  4156. }
  4157. if (FRefreshLocalDirectory)
  4158. {
  4159. ReloadLocalDirectory();
  4160. FRefreshLocalDirectory = false;
  4161. }
  4162. if (WinConfiguration->RefreshRemotePanel)
  4163. {
  4164. TManagedTerminal * ManagedTerminal =
  4165. dynamic_cast<TManagedTerminal *>(Terminal);
  4166. if ((ManagedTerminal != NULL) && (Terminal->Status == ssOpened) &&
  4167. (Now() - ManagedTerminal->DirectoryLoaded >
  4168. WinConfiguration->RefreshRemotePanelInterval))
  4169. {
  4170. RemoteDirView->ReloadDirectory();
  4171. }
  4172. }
  4173. }
  4174. }
  4175. if (FShowing || FStandaloneEditing)
  4176. {
  4177. if (FQueueStatusInvalidated)
  4178. {
  4179. UpdateQueueStatus(false);
  4180. }
  4181. RefreshQueueItems();
  4182. }
  4183. if (FShowing)
  4184. {
  4185. UpdateStatusBar();
  4186. }
  4187. FIgnoreNextDialogChar = 0;
  4188. }
  4189. //---------------------------------------------------------------------------
  4190. void __fastcall TCustomScpExplorerForm::UserActionTimer(TObject * /*Sender*/)
  4191. {
  4192. try
  4193. {
  4194. FUserActionTimer->Enabled = false;
  4195. if (IsQueueAutoPopup() && (FPendingQueueActionItem != NULL))
  4196. {
  4197. if (TQueueItem::IsUserActionStatus(FPendingQueueActionItem->Status))
  4198. {
  4199. FPendingQueueActionItem->ProcessUserAction();
  4200. }
  4201. }
  4202. }
  4203. __finally
  4204. {
  4205. FPendingQueueActionItem = NULL;
  4206. }
  4207. }
  4208. //---------------------------------------------------------------------------
  4209. void __fastcall TCustomScpExplorerForm::ApplicationMinimize(TObject * /*Sender*/)
  4210. {
  4211. if (WinConfiguration->MinimizeToTray)
  4212. {
  4213. UpdateTrayIcon();
  4214. FTrayIcon->Visible = true;
  4215. if (Visible)
  4216. {
  4217. ShowWindow(Handle, SW_HIDE);
  4218. }
  4219. }
  4220. }
  4221. //---------------------------------------------------------------------------
  4222. void __fastcall TCustomScpExplorerForm::ApplicationRestore(TObject * /*Sender*/)
  4223. {
  4224. // WORKAROUND
  4225. // When restoring maximized window from minimization,
  4226. // rarely some controls do not align properly.
  4227. // Two instances seen (both for Commander):
  4228. // - When restoring, window is temporarily narrower (not maximized),
  4229. // causing toolbars on TopDock to wrap and dock to expand horizontally.
  4230. // Once maximized already, top dock shinks back, but the session PageControl,
  4231. // do not align up, leaving space between TopDock and PageControl.
  4232. // - Similar issue seen with LocalDirView not aligning down to status bar.
  4233. for (int Index = 0; Index < ControlCount; Index++)
  4234. {
  4235. RealignControl(Controls[Index]);
  4236. }
  4237. if (FTrayIcon->Visible)
  4238. {
  4239. FTrayIcon->Visible = false;
  4240. if (Visible)
  4241. {
  4242. ShowWindow(Handle, SW_SHOW);
  4243. }
  4244. }
  4245. if (FNeedSession && DebugAlwaysTrue(Terminal == NULL))
  4246. {
  4247. FNeedSession = false;
  4248. NonVisualDataModule->StartBusy();
  4249. try
  4250. {
  4251. NeedSession(false);
  4252. }
  4253. __finally
  4254. {
  4255. NonVisualDataModule->EndBusy();
  4256. }
  4257. }
  4258. }
  4259. //---------------------------------------------------------------------------
  4260. void __fastcall TCustomScpExplorerForm::UpdateTrayIcon()
  4261. {
  4262. FTrayIcon->Hint = GetMainForm()->Caption;
  4263. }
  4264. //---------------------------------------------------------------------------
  4265. void __fastcall TCustomScpExplorerForm::ApplicationTitleChanged()
  4266. {
  4267. UpdateTrayIcon();
  4268. }
  4269. //---------------------------------------------------------------------------
  4270. void __fastcall TCustomScpExplorerForm::RestoreApp()
  4271. {
  4272. // workaround
  4273. // TApplication.WndProc sets TApplication.FAppIconic to false,
  4274. // on WM_ACTIVATEAPP, what renders TApplication.Restore no-op function.
  4275. // But WM_ACTIVATEAPP message can be received even when
  4276. // the main window is minimized to the tray and internal editor window is focused
  4277. // (after another application was previously active)
  4278. if (::IsIconic(Handle))
  4279. {
  4280. if (!IsAppIconic())
  4281. {
  4282. SetAppIconic(true);
  4283. }
  4284. }
  4285. ::ApplicationRestore();
  4286. Application->BringToFront();
  4287. }
  4288. //---------------------------------------------------------------------------
  4289. void __fastcall TCustomScpExplorerForm::TrayIconClick(TObject * /*Sender*/)
  4290. {
  4291. RestoreApp();
  4292. }
  4293. //---------------------------------------------------------------------------
  4294. void __fastcall TCustomScpExplorerForm::NewSession(bool FromSite, const UnicodeString & SessionUrl)
  4295. {
  4296. if (OpenInNewWindow())
  4297. {
  4298. // todo: Pass FromSite
  4299. ExecuteNewInstance(SessionUrl);
  4300. }
  4301. else
  4302. {
  4303. TTerminalManager::Instance()->NewSession(FromSite, SessionUrl);
  4304. }
  4305. }
  4306. //---------------------------------------------------------------------------
  4307. UnicodeString __fastcall TCustomScpExplorerForm::CreateHiddenDuplicateSession()
  4308. {
  4309. UnicodeString SessionName = StoredSessions->HiddenPrefix + Terminal->SessionData->SessionName;
  4310. // current working directories become defaults here, what is not right
  4311. std::unique_ptr<TSessionData> SessionData(CloneCurrentSessionData());
  4312. StoredSessions->NewSession(SessionName, SessionData.get());
  4313. // modified only, explicit
  4314. StoredSessions->Save(false, true);
  4315. // encode session name because of slashes in hierarchical sessions
  4316. return EncodeUrlString(SessionName);
  4317. }
  4318. //---------------------------------------------------------------------------
  4319. void __fastcall TCustomScpExplorerForm::DuplicateSession()
  4320. {
  4321. if (OpenInNewWindow())
  4322. {
  4323. ExecuteNewInstance(CreateHiddenDuplicateSession());
  4324. }
  4325. else
  4326. {
  4327. // current working directories become defaults here, what is not right
  4328. std::unique_ptr<TSessionData> SessionData(CloneCurrentSessionData());
  4329. TTerminalManager * Manager = TTerminalManager::Instance();
  4330. TTerminal * Terminal = Manager->NewTerminal(SessionData.get());
  4331. Manager->ActiveTerminal = Terminal;
  4332. }
  4333. }
  4334. //---------------------------------------------------------------------------
  4335. bool __fastcall TCustomScpExplorerForm::CanCloseQueue()
  4336. {
  4337. DebugAssert(FQueue != NULL);
  4338. bool Result = FQueue->IsEmpty;
  4339. if (!Result)
  4340. {
  4341. SetFocus();
  4342. Result = (MessageDialog(LoadStr(PENDING_QUEUE_ITEMS2), qtWarning, qaOK | qaCancel, HELP_NONE) == qaOK);
  4343. }
  4344. return Result;
  4345. }
  4346. //---------------------------------------------------------------------------
  4347. void __fastcall TCustomScpExplorerForm::CloseSession()
  4348. {
  4349. if (CanCloseQueue())
  4350. {
  4351. TTerminalManager::Instance()->FreeActiveTerminal();
  4352. }
  4353. }
  4354. //---------------------------------------------------------------------------
  4355. void __fastcall TCustomScpExplorerForm::OpenStoredSession(TSessionData * Data)
  4356. {
  4357. if (OpenInNewWindow())
  4358. {
  4359. // encode session name because of slashes in hierarchical sessions
  4360. ExecuteNewInstance(EncodeUrlString(Data->Name));
  4361. }
  4362. else
  4363. {
  4364. TTerminalManager * Manager = TTerminalManager::Instance();
  4365. TTerminal * Terminal = Manager->NewTerminal(Data);
  4366. Manager->ActiveTerminal = Terminal;
  4367. }
  4368. }
  4369. //---------------------------------------------------------------------------
  4370. void __fastcall TCustomScpExplorerForm::OpenFolderOrWorkspace(const UnicodeString & Name)
  4371. {
  4372. if (OpenInNewWindow())
  4373. {
  4374. ExecuteNewInstance(Name);
  4375. }
  4376. else
  4377. {
  4378. TTerminalManager * Manager = TTerminalManager::Instance();
  4379. std::unique_ptr<TObjectList> DataList(new TObjectList());
  4380. StoredSessions->GetFolderOrWorkspace(Name, DataList.get());
  4381. Manager->ActiveTerminal = Manager->NewTerminals(DataList.get());
  4382. }
  4383. }
  4384. //---------------------------------------------------------------------------
  4385. void __fastcall TCustomScpExplorerForm::FormCloseQuery(TObject * /*Sender*/,
  4386. bool &CanClose)
  4387. {
  4388. if (Terminal != NULL)
  4389. {
  4390. if (Terminal->Active && WinConfiguration->ConfirmClosingSession)
  4391. {
  4392. unsigned int Result;
  4393. TMessageParams Params(mpNeverAskAgainCheck);
  4394. UnicodeString Message;
  4395. int Answers = qaOK | qaCancel;
  4396. if (TTerminalManager::Instance()->Count > 1)
  4397. {
  4398. if (!WinConfiguration->AutoSaveWorkspace)
  4399. {
  4400. Message = LoadStr(CLOSE_SESSIONS_WORKSPACE2);
  4401. Answers = qaYes | qaNo | qaCancel;
  4402. }
  4403. else
  4404. {
  4405. Message = MainInstructions(LoadStr(CLOSE_SESSIONS));
  4406. }
  4407. }
  4408. else
  4409. {
  4410. Message = MainInstructions(FMTLOAD(CLOSE_SESSION, (Terminal->SessionData->SessionName)));
  4411. }
  4412. if (WinConfiguration->AutoSaveWorkspace)
  4413. {
  4414. Message =
  4415. FORMAT("%s\n\n%s", (Message,
  4416. FMTLOAD(AUTO_WORKSPACE, (WorkspaceName()))));
  4417. }
  4418. SetFocus();
  4419. Result = MessageDialog(Message, qtConfirmation,
  4420. Answers, HELP_NONE, &Params);
  4421. if (Result == qaNeverAskAgain)
  4422. {
  4423. WinConfiguration->ConfirmClosingSession = false;
  4424. }
  4425. if (Result == qaNo)
  4426. {
  4427. CanClose = SaveWorkspace(true);
  4428. // note that the workspace will be saved redundatly again from FormClose
  4429. }
  4430. else
  4431. {
  4432. CanClose =
  4433. (Result == qaOK) ||
  4434. (Result == qaYes) || // CLOSE_SESSIONS_WORKSPACE variant
  4435. (Result == qaNeverAskAgain);
  4436. }
  4437. }
  4438. if (CanClose)
  4439. {
  4440. CanClose = CanCloseQueue();
  4441. }
  4442. }
  4443. if (CanClose)
  4444. {
  4445. CanClose =
  4446. FEditorManager->CloseInternalEditors(CloseInternalEditor) &&
  4447. FEditorManager->CloseExternalFilesWithoutProcess();
  4448. if (CanClose)
  4449. {
  4450. while (CanClose && (FLocalEditors->Count > 0))
  4451. {
  4452. int PrevCount = FLocalEditors->Count;
  4453. static_cast<TForm *>(FLocalEditors->Items[0])->Close();
  4454. CanClose = (FLocalEditors->Count < PrevCount);
  4455. }
  4456. if (CanClose)
  4457. {
  4458. CanClose = FEditorManager->Empty(true);
  4459. if (!CanClose)
  4460. {
  4461. SetFocus();
  4462. CanClose =
  4463. (MessageDialog(
  4464. LoadStr(PENDING_EDITORS), qtWarning, qaIgnore | qaCancel, HELP_NONE) == qaIgnore);
  4465. }
  4466. }
  4467. }
  4468. }
  4469. }
  4470. //---------------------------------------------------------------------------
  4471. void __fastcall TCustomScpExplorerForm::CloseInternalEditor(TObject * Sender)
  4472. {
  4473. TForm * Form = dynamic_cast<TForm *>(Sender);
  4474. DebugAssert(Form != NULL);
  4475. Form->Close();
  4476. }
  4477. //---------------------------------------------------------------------------
  4478. void __fastcall TCustomScpExplorerForm::ForceCloseInternalEditor(TObject * Sender)
  4479. {
  4480. TForm * Form = dynamic_cast<TForm *>(Sender);
  4481. delete Form;
  4482. }
  4483. //---------------------------------------------------------------------------
  4484. void __fastcall TCustomScpExplorerForm::ForceCloseLocalEditors()
  4485. {
  4486. while (FLocalEditors->Count > 0)
  4487. {
  4488. delete static_cast<TForm *>(FLocalEditors->Items[0]);
  4489. }
  4490. }
  4491. //---------------------------------------------------------------------------
  4492. void __fastcall TCustomScpExplorerForm::RemoteDirViewDisplayProperties(
  4493. TObject *Sender)
  4494. {
  4495. TStrings *FileList = ((TUnixDirView*)Sender)->CreateFileList(True, False, NULL);
  4496. try
  4497. {
  4498. SetProperties(osRemote, FileList);
  4499. }
  4500. __finally
  4501. {
  4502. delete FileList;
  4503. }
  4504. }
  4505. //---------------------------------------------------------------------------
  4506. void __fastcall TCustomScpExplorerForm::ComponentShowing(Byte Component, bool value)
  4507. {
  4508. if (value)
  4509. {
  4510. if (Component == fcCustomCommandsBand)
  4511. {
  4512. UpdateCustomCommandsToolbar();
  4513. }
  4514. }
  4515. }
  4516. //---------------------------------------------------------------------------
  4517. void __fastcall TCustomScpExplorerForm::SetComponentVisible(Byte Component, Boolean value)
  4518. {
  4519. TControl * Control = GetComponent(Component);
  4520. DebugAssert(Control);
  4521. bool Changed = (Control->Visible != value);
  4522. if (Changed)
  4523. {
  4524. ComponentShowing(Component, value);
  4525. TWinControl * WinControl = dynamic_cast<TWinControl*>(Control);
  4526. bool WasFocused = (WinControl != NULL) && (ActiveControl != NULL) &&
  4527. ((ActiveControl == WinControl) || (ActiveControl->Parent == WinControl));
  4528. if (value)
  4529. {
  4530. int RemainingHeight = Control->Parent->ClientHeight;
  4531. int RemainingWidth = Control->Parent->ClientWidth;
  4532. for (int i = 0; i < Control->Parent->ControlCount; i++)
  4533. {
  4534. TControl * ChildControl = Control->Parent->Controls[i];
  4535. if (ChildControl->Visible)
  4536. {
  4537. switch (ChildControl->Align)
  4538. {
  4539. case alTop:
  4540. case alBottom:
  4541. RemainingHeight -= ChildControl->Height;
  4542. break;
  4543. case alLeft:
  4544. case alRight:
  4545. RemainingWidth -= ChildControl->Width;
  4546. break;
  4547. }
  4548. }
  4549. }
  4550. int Reserve = ScaleByTextHeight(this, 32);
  4551. // queue in explorer, trees in commander
  4552. if (Control->Height > RemainingHeight - Reserve)
  4553. {
  4554. Control->Height = RemainingHeight / 2;
  4555. }
  4556. if (Control->Width > RemainingWidth - Reserve)
  4557. {
  4558. Control->Width = RemainingWidth / 2;
  4559. }
  4560. }
  4561. Control->Visible = value;
  4562. if (WasFocused && Visible)
  4563. {
  4564. DirView(osCurrent)->SetFocus();
  4565. }
  4566. FixControlsPlacement();
  4567. }
  4568. }
  4569. //---------------------------------------------------------------------------
  4570. bool __fastcall TCustomScpExplorerForm::GetComponentVisible(Byte Component)
  4571. {
  4572. TControl * Control = GetComponent(Component);
  4573. if (Control == NULL)
  4574. {
  4575. return false;
  4576. }
  4577. else
  4578. {
  4579. return Control->Visible;
  4580. }
  4581. }
  4582. //---------------------------------------------------------------------------
  4583. bool __fastcall TCustomScpExplorerForm::IsComponentPossible(Byte Component)
  4584. {
  4585. bool Result;
  4586. if ((Component == fcExplorerUpdatesBand) || (Component == fcCommanderUpdatesBand))
  4587. {
  4588. Result = !IsUWP();
  4589. }
  4590. else
  4591. {
  4592. Result = true;
  4593. }
  4594. return Result;
  4595. }
  4596. //---------------------------------------------------------------------------
  4597. void __fastcall TCustomScpExplorerForm::FixControlsPlacement()
  4598. {
  4599. if (RemoteDirView->ItemFocused != NULL)
  4600. {
  4601. RemoteDirView->ItemFocused->MakeVisible(false);
  4602. }
  4603. QueueSplitter->Visible = QueuePanel->Visible;
  4604. RemotePanelSplitter->Visible = RemoteDriveView->Visible;
  4605. }
  4606. //---------------------------------------------------------------------------
  4607. TControl * __fastcall TCustomScpExplorerForm::GetComponent(Byte Component)
  4608. {
  4609. switch (Component) {
  4610. case fcStatusBar: return RemoteStatusBar;
  4611. case fcRemotePopup: return reinterpret_cast<TControl *>(NonVisualDataModule->RemoteFilePopup);
  4612. case fcQueueView: return QueuePanel;
  4613. case fcQueueToolbar: return QueueDock;
  4614. case fcRemoteTree: return RemoteDriveView;
  4615. case fcSessionsTabs: return SessionsPageControl;
  4616. default: return NULL;
  4617. }
  4618. }
  4619. //---------------------------------------------------------------------------
  4620. void __fastcall TCustomScpExplorerForm::DirViewColumnRightClick(
  4621. TObject *Sender, TListColumn *Column, TPoint &Point)
  4622. {
  4623. DebugAssert(NonVisualDataModule && Column && Sender);
  4624. NonVisualDataModule->ListColumn = Column;
  4625. TPopupMenu * DirViewColumnMenu;
  4626. if (Sender == RemoteDirView)
  4627. {
  4628. DirViewColumnMenu = NonVisualDataModule->RemoteDirViewColumnPopup;
  4629. NonVisualDataModule->RemoteSortByExtColumnPopupItem->Visible =
  4630. (Column->Index == uvName);
  4631. NonVisualDataModule->RemoteFormatSizeBytesPopupItem->Visible =
  4632. (Column->Index == uvSize);
  4633. }
  4634. else
  4635. {
  4636. DirViewColumnMenu = NonVisualDataModule->LocalDirViewColumnPopup;
  4637. NonVisualDataModule->LocalSortByExtColumnPopupItem->Visible =
  4638. (Column->Index == dvName);
  4639. NonVisualDataModule->LocalFormatSizeBytesPopupItem->Visible =
  4640. (Column->Index == dvSize);
  4641. }
  4642. TCustomListView * ListView = DebugNotNull(dynamic_cast<TCustomListView *>(Sender));
  4643. TPoint ScreenPoint = ListView->ClientToScreen(Point);
  4644. ScreenPoint.x -= GetScrollPos(ListView->Handle, SB_HORZ);
  4645. DirViewColumnMenu->Popup(ScreenPoint.x, ScreenPoint.y);
  4646. }
  4647. //---------------------------------------------------------------------------
  4648. void __fastcall TCustomScpExplorerForm::DirViewExecFile(
  4649. TObject *Sender, TListItem *Item, bool &AllowExec)
  4650. {
  4651. DoDirViewExecFile(Sender, Item, AllowExec);
  4652. }
  4653. //---------------------------------------------------------------------------
  4654. void __fastcall TCustomScpExplorerForm::DoDirViewExecFile(TObject * Sender,
  4655. TListItem * Item, bool & AllowExec)
  4656. {
  4657. DebugAssert(Sender && Item && Configuration);
  4658. DebugAssert(AllowExec);
  4659. TCustomDirView * ADirView = (TCustomDirView *)Sender;
  4660. bool Remote = (ADirView == DirView(osRemote));
  4661. bool ResolvedSymlinks = !Remote || Terminal->ResolvingSymlinks;
  4662. TOperationSide Side = (Remote ? osRemote : osLocal);
  4663. // Anything special is done on double click only (not on "open" indicated by FForceExecution),
  4664. // on files only (not directories)
  4665. // and only when symlinks are resolved (apply to remote panel only)
  4666. if (!ADirView->ItemIsDirectory(Item) &&
  4667. (ResolvedSymlinks || FForceExecution || Terminal->IsEncryptingFiles()))
  4668. {
  4669. if ((WinConfiguration->DoubleClickAction != dcaOpen) &&
  4670. !FForceExecution &&
  4671. (ResolvedSymlinks || Terminal->IsEncryptingFiles()))
  4672. {
  4673. if (WinConfiguration->DoubleClickAction == dcaCopy)
  4674. {
  4675. // To counter the lock in DirViewBusy (called from TCustomDirView.DoExecute)
  4676. UnlockWindow();
  4677. try
  4678. {
  4679. ExecuteFileOperation(
  4680. foCopy, Side, true, !WinConfiguration->CopyOnDoubleClickConfirmation);
  4681. }
  4682. __finally
  4683. {
  4684. LockWindow();
  4685. }
  4686. AllowExec = false;
  4687. }
  4688. else if (WinConfiguration->DoubleClickAction == dcaEdit)
  4689. {
  4690. if (!Remote || !WinConfiguration->DisableOpenEdit)
  4691. {
  4692. ExecuteFile(Side, efDefaultEditor);
  4693. AllowExec = false;
  4694. }
  4695. }
  4696. else
  4697. {
  4698. DebugFail();
  4699. }
  4700. }
  4701. // if we have not done anything special, fall back to default behavior
  4702. if (AllowExec)
  4703. {
  4704. if (Remote && !WinConfiguration->DisableOpenEdit)
  4705. {
  4706. ExecuteFile(osRemote, efShell);
  4707. AllowExec = false;
  4708. }
  4709. }
  4710. }
  4711. }
  4712. //---------------------------------------------------------------------------
  4713. bool __fastcall TCustomScpExplorerForm::GetHasDirView(TOperationSide Side)
  4714. {
  4715. return ((Side == osRemote) || (Side == osCurrent));
  4716. }
  4717. //---------------------------------------------------------------------------
  4718. void __fastcall TCustomScpExplorerForm::CompareDirectories()
  4719. {
  4720. DebugFail();
  4721. }
  4722. //---------------------------------------------------------------------------
  4723. void __fastcall TCustomScpExplorerForm::SynchronizeDirectories()
  4724. {
  4725. DebugFail();
  4726. }
  4727. //---------------------------------------------------------------------------
  4728. bool __fastcall TCustomScpExplorerForm::DoSynchronizeDirectories(
  4729. UnicodeString & LocalDirectory, UnicodeString & RemoteDirectory, bool UseDefaults)
  4730. {
  4731. TSynchronizeParamType Params;
  4732. Params.LocalDirectory = LocalDirectory;
  4733. Params.RemoteDirectory = RemoteDirectory;
  4734. int UnusedParams =
  4735. (GUIConfiguration->SynchronizeParams &
  4736. (TTerminal::spPreviewChanges | TTerminal::spTimestamp | TTerminal::spNotByTime | TTerminal::spBySize));
  4737. Params.Params = GUIConfiguration->SynchronizeParams & ~UnusedParams;
  4738. Params.Options = GUIConfiguration->SynchronizeOptions;
  4739. bool SaveSettings = false;
  4740. TSynchronizeController Controller(&DoSynchronize, &DoSynchronizeInvalid,
  4741. &DoSynchronizeTooManyDirectories);
  4742. DebugAssert(FSynchronizeController == NULL);
  4743. FSynchronizeController = &Controller;
  4744. bool Result;
  4745. try
  4746. {
  4747. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  4748. int CopyParamAttrs = Terminal->UsableCopyParamAttrs(0).Upload;
  4749. int Options =
  4750. FLAGMASK(SynchronizeAllowSelectedOnly(), soAllowSelectedOnly);
  4751. DebugAssert(FOnFeedSynchronizeError == NULL);
  4752. Result = DoSynchronizeDialog(Params, &CopyParam, Controller.StartStop,
  4753. SaveSettings, Options, CopyParamAttrs, GetSynchronizeOptions, SynchronizeSessionLog,
  4754. FOnFeedSynchronizeError, SynchronizeInNewWindow, UseDefaults);
  4755. if (Result)
  4756. {
  4757. if (SaveSettings)
  4758. {
  4759. GUIConfiguration->SynchronizeParams = Params.Params | UnusedParams;
  4760. GUIConfiguration->SynchronizeOptions = Params.Options;
  4761. }
  4762. else
  4763. {
  4764. if (FLAGSET(GUIConfiguration->SynchronizeOptions, soSynchronizeAsk) &&
  4765. FLAGCLEAR(Params.Options, soSynchronizeAsk) &&
  4766. FLAGSET(Params.Options, soSynchronize))
  4767. {
  4768. GUIConfiguration->SynchronizeOptions =
  4769. (GUIConfiguration->SynchronizeOptions & ~soSynchronizeAsk) |
  4770. soSynchronize;
  4771. }
  4772. }
  4773. LocalDirectory = Params.LocalDirectory;
  4774. RemoteDirectory = Params.RemoteDirectory;
  4775. }
  4776. }
  4777. __finally
  4778. {
  4779. FSynchronizeController = NULL;
  4780. DebugAssert(FOnFeedSynchronizeError == NULL);
  4781. FOnFeedSynchronizeError = NULL;
  4782. }
  4783. return Result;
  4784. }
  4785. //---------------------------------------------------------------------------
  4786. void __fastcall TCustomScpExplorerForm::DoSynchronize(
  4787. TSynchronizeController * /*Sender*/, const UnicodeString LocalDirectory,
  4788. const UnicodeString RemoteDirectory, const TCopyParamType & CopyParam,
  4789. const TSynchronizeParamType & Params, TSynchronizeChecklist ** Checklist,
  4790. TSynchronizeOptions * Options, bool Full)
  4791. {
  4792. // Keep-up-to-date mode
  4793. if (Terminal->Status == ssOpened)
  4794. {
  4795. try
  4796. {
  4797. int PParams = Params.Params;
  4798. if (!Full)
  4799. {
  4800. PParams |= TTerminal::spNoRecurse | TTerminal::spUseCache | TTerminal::spDelayProgress;
  4801. if (FLAGSET(Params.Options, soRecurse))
  4802. {
  4803. PParams |= TTerminal::spSubDirs;
  4804. }
  4805. }
  4806. else
  4807. {
  4808. // if keepuptodate is non-recursive,
  4809. // full sync before has to be non-recursive as well
  4810. if (FLAGCLEAR(Params.Options, soRecurse))
  4811. {
  4812. PParams |= TTerminal::spNoRecurse;
  4813. }
  4814. }
  4815. Synchronize(LocalDirectory, RemoteDirectory, smRemote, CopyParam,
  4816. PParams, Checklist, Options);
  4817. }
  4818. catch (Exception & E)
  4819. {
  4820. if (FLAGSET(Params.Options, soContinueOnError) && Terminal->Active &&
  4821. DebugAlwaysTrue(FOnFeedSynchronizeError != NULL))
  4822. {
  4823. // noop, already logged in MoreMessageDialog
  4824. }
  4825. else
  4826. {
  4827. ShowExtendedExceptionEx(Terminal, &E);
  4828. throw;
  4829. }
  4830. }
  4831. }
  4832. }
  4833. //---------------------------------------------------------------------------
  4834. void __fastcall TCustomScpExplorerForm::DoSynchronizeInvalid(
  4835. TSynchronizeController * /*Sender*/, const UnicodeString Directory,
  4836. const UnicodeString ErrorStr)
  4837. {
  4838. if (!Directory.IsEmpty())
  4839. {
  4840. SimpleErrorDialog(FMTLOAD(WATCH_ERROR_DIRECTORY, (Directory)), ErrorStr);
  4841. }
  4842. else
  4843. {
  4844. SimpleErrorDialog(LoadStr(WATCH_ERROR_GENERAL), ErrorStr);
  4845. }
  4846. }
  4847. //---------------------------------------------------------------------------
  4848. void __fastcall TCustomScpExplorerForm::DoSynchronizeTooManyDirectories(
  4849. TSynchronizeController * /*Sender*/, int & MaxDirectories)
  4850. {
  4851. if (MaxDirectories < GUIConfiguration->MaxWatchDirectories)
  4852. {
  4853. MaxDirectories = GUIConfiguration->MaxWatchDirectories;
  4854. }
  4855. else
  4856. {
  4857. TMessageParams Params(mpNeverAskAgainCheck);
  4858. unsigned int Result = MessageDialog(
  4859. FMTLOAD(TOO_MANY_WATCH_DIRECTORIES, (MaxDirectories, MaxDirectories)),
  4860. qtConfirmation, qaYes | qaNo, HELP_TOO_MANY_WATCH_DIRECTORIES, &Params);
  4861. if ((Result == qaYes) || (Result == qaNeverAskAgain))
  4862. {
  4863. MaxDirectories *= 2;
  4864. if (Result == qaNeverAskAgain)
  4865. {
  4866. GUIConfiguration->MaxWatchDirectories = MaxDirectories;
  4867. }
  4868. }
  4869. else
  4870. {
  4871. Abort();
  4872. }
  4873. }
  4874. }
  4875. //---------------------------------------------------------------------------
  4876. void __fastcall TCustomScpExplorerForm::Synchronize(const UnicodeString LocalDirectory,
  4877. const UnicodeString RemoteDirectory, TSynchronizeMode Mode,
  4878. const TCopyParamType & CopyParam, int Params, TSynchronizeChecklist ** Checklist,
  4879. TSynchronizeOptions * Options)
  4880. {
  4881. // Keep-up-to-date mode
  4882. DebugAssert(!FAutoOperation);
  4883. void * BatchStorage;
  4884. BatchStart(BatchStorage);
  4885. FAutoOperation = true;
  4886. bool AnyOperation = false;
  4887. TDateTime StartTime = Now();
  4888. TSynchronizeChecklist * AChecklist = NULL;
  4889. try
  4890. {
  4891. FSynchronizeProgressForm = new TSynchronizeProgressForm(Application, true, true);
  4892. if (FLAGCLEAR(Params, TTerminal::spDelayProgress))
  4893. {
  4894. FSynchronizeProgressForm->Start();
  4895. }
  4896. AChecklist = Terminal->SynchronizeCollect(LocalDirectory, RemoteDirectory,
  4897. static_cast<TTerminal::TSynchronizeMode>(Mode),
  4898. &CopyParam, Params | TTerminal::spNoConfirmation, TerminalSynchronizeDirectory,
  4899. Options);
  4900. SAFE_DESTROY(FSynchronizeProgressForm);
  4901. AnyOperation = (AChecklist->CheckedCount > 0);
  4902. if (AnyOperation)
  4903. {
  4904. TSynchronizeProgress SynchronizeProgress(AChecklist);
  4905. CreateProgressForm(&SynchronizeProgress);
  4906. try
  4907. {
  4908. Terminal->SynchronizeApply(
  4909. AChecklist, &CopyParam, Params | TTerminal::spNoConfirmation, TerminalSynchronizeDirectory,
  4910. SynchronizeProcessedItem, NULL, NULL);
  4911. }
  4912. __finally
  4913. {
  4914. DestroyProgressForm();
  4915. }
  4916. }
  4917. }
  4918. __finally
  4919. {
  4920. if (Checklist == NULL)
  4921. {
  4922. delete AChecklist;
  4923. }
  4924. else
  4925. {
  4926. *Checklist = AChecklist;
  4927. }
  4928. FAutoOperation = false;
  4929. SAFE_DESTROY(FSynchronizeProgressForm);
  4930. BatchEnd(BatchStorage);
  4931. ReloadLocalDirectory();
  4932. if (AnyOperation)
  4933. {
  4934. OperationComplete(StartTime);
  4935. }
  4936. }
  4937. }
  4938. //---------------------------------------------------------------------------
  4939. bool __fastcall TCustomScpExplorerForm::SynchronizeAllowSelectedOnly()
  4940. {
  4941. // can be called from command line
  4942. return Visible &&
  4943. ((DirView(osRemote)->SelCount > 0) ||
  4944. (HasDirView[osLocal] && (DirView(osLocal)->SelCount > 0)));
  4945. }
  4946. //---------------------------------------------------------------------------
  4947. void __fastcall TCustomScpExplorerForm::SynchronizeSessionLog(const UnicodeString & Message)
  4948. {
  4949. LogSynchronizeEvent(Terminal, Message);
  4950. }
  4951. //---------------------------------------------------------------------------
  4952. void __fastcall TCustomScpExplorerForm::GetSynchronizeOptions(
  4953. int Params, TSynchronizeOptions & Options)
  4954. {
  4955. if (FLAGSET(Params, TTerminal::spSelectedOnly) && SynchronizeAllowSelectedOnly())
  4956. {
  4957. Options.Filter = new TStringList();
  4958. Options.Filter->CaseSensitive = false;
  4959. Options.Filter->Duplicates = Types::dupAccept;
  4960. if (DirView(osRemote)->SelCount > 0)
  4961. {
  4962. DirView(osRemote)->CreateFileList(false, false, Options.Filter);
  4963. }
  4964. if (HasDirView[osLocal] && (DirView(osLocal)->SelCount > 0))
  4965. {
  4966. DirView(osLocal)->CreateFileList(false, false, Options.Filter);
  4967. }
  4968. Options.Filter->Sort();
  4969. }
  4970. }
  4971. //---------------------------------------------------------------------------
  4972. void __fastcall TCustomScpExplorerForm::SynchronizeInNewWindow(
  4973. const TSynchronizeParamType & Params, const TCopyParamType * CopyParams)
  4974. {
  4975. UnicodeString SessionName = CreateHiddenDuplicateSession();
  4976. UnicodeString AdditionalParams =
  4977. FORMAT(L"%s \"%s\" \"%s\" %d %d %s", (
  4978. TProgramParams::FormatSwitch(KEEP_UP_TO_DATE_SWITCH),
  4979. Params.LocalDirectory, Params.RemoteDirectory, Params.Params, Params.Options,
  4980. TProgramParams::FormatSwitch(DEFAULTS_SWITCH)));
  4981. TCopyParamType Defaults;
  4982. std::unique_ptr<THierarchicalStorage> ConfigStorage(Configuration->CreateConfigStorage());
  4983. ConfigStorage->AccessMode = smRead;
  4984. if (ConfigStorage->OpenSubKey(Configuration->ConfigurationSubKey, false, false))
  4985. {
  4986. GUIConfiguration->LoadCopyParam(ConfigStorage.get(), &Defaults);
  4987. }
  4988. ConfigStorage.reset(NULL);
  4989. std::unique_ptr<TStringList> Options(new TStringList());
  4990. std::unique_ptr<TOptionsStorage> OptionsStorage(new TOptionsStorage(Options.get(), true));
  4991. GUIConfiguration->SaveCopyParam(OptionsStorage.get(), CopyParams, &Defaults);
  4992. if (Options->Count > 0)
  4993. {
  4994. AdditionalParams +=
  4995. FORMAT(L" %s%s", (TProgramParams::FormatSwitch(RAW_CONFIG_SWITCH), StringsToParams(Options.get())));
  4996. }
  4997. ExecuteNewInstance(SessionName, AdditionalParams);
  4998. }
  4999. //---------------------------------------------------------------------------
  5000. struct TSynchronizeParams
  5001. {
  5002. UnicodeString LocalDirectory;
  5003. UnicodeString RemoteDirectory;
  5004. TSynchronizeMode Mode;
  5005. TSynchronizeChecklist * Checklist;
  5006. int Params;
  5007. TCopyParamType * CopyParam;
  5008. TDateTime * StartTime;
  5009. TProcessedSynchronizationChecklistItem OnProcessedItem;
  5010. };
  5011. //---------------------------------------------------------------------------
  5012. void __fastcall TCustomScpExplorerForm::FullSynchronize(
  5013. TSynchronizeParams & Params, TProcessedSynchronizationChecklistItem OnProcessedItem,
  5014. TUpdatedSynchronizationChecklistItems OnUpdatedSynchronizationChecklistItems)
  5015. {
  5016. DebugAssert(!FAutoOperation);
  5017. void * BatchStorage;
  5018. BatchStart(BatchStorage);
  5019. FAutoOperation = true;
  5020. try
  5021. {
  5022. Params.OnProcessedItem = OnProcessedItem;
  5023. TSynchronizeProgress SynchronizeProgress(Params.Checklist);
  5024. CreateProgressForm(&SynchronizeProgress);
  5025. Terminal->SynchronizeApply(
  5026. Params.Checklist, Params.CopyParam, Params.Params | TTerminal::spNoConfirmation,
  5027. TerminalSynchronizeDirectory, SynchronizeProcessedItem, OnUpdatedSynchronizationChecklistItems, &Params);
  5028. }
  5029. __finally
  5030. {
  5031. Params.OnProcessedItem = NULL;
  5032. FAutoOperation = false;
  5033. DestroyProgressForm();
  5034. BatchEnd(BatchStorage);
  5035. ReloadLocalDirectory();
  5036. }
  5037. }
  5038. //---------------------------------------------------------------------------
  5039. void __fastcall TCustomScpExplorerForm::SynchronizeProcessedItem(void * Token, const TSynchronizeChecklist::TItem * ChecklistItem)
  5040. {
  5041. if (DebugAlwaysTrue(FProgressForm != NULL) && DebugAlwaysTrue(FProgressForm->SynchronizeProgress != NULL))
  5042. {
  5043. FProgressForm->SynchronizeProgress->ItemProcessed(ChecklistItem);
  5044. }
  5045. // Not set in keep-up-to-date mode - Synchronize() method
  5046. if (Token != NULL)
  5047. {
  5048. TSynchronizeParams & Params = *static_cast<TSynchronizeParams *>(Token);
  5049. if (Params.OnProcessedItem != NULL)
  5050. {
  5051. Params.OnProcessedItem(NULL, ChecklistItem);
  5052. }
  5053. }
  5054. }
  5055. //---------------------------------------------------------------------------
  5056. void __fastcall TCustomScpExplorerForm::DoFullSynchronize(
  5057. void * Token, TProcessedSynchronizationChecklistItem OnProcessedItem,
  5058. TUpdatedSynchronizationChecklistItems OnUpdatedSynchronizationChecklistItems)
  5059. {
  5060. TSynchronizeParams & Params = *static_cast<TSynchronizeParams *>(Token);
  5061. *Params.StartTime = Now();
  5062. FullSynchronize(Params, OnProcessedItem, OnUpdatedSynchronizationChecklistItems);
  5063. }
  5064. //---------------------------------------------------------------------------
  5065. void __fastcall TCustomScpExplorerForm::DoSynchronizeChecklistCalculateSize(
  5066. TSynchronizeChecklist * Checklist, const TSynchronizeChecklist::TItemList & Items, void * Token)
  5067. {
  5068. // terminal can be already closed (e.g. dropped connection)
  5069. if (Terminal != NULL)
  5070. {
  5071. TSynchronizeParams & Params = *static_cast<TSynchronizeParams *>(Token);
  5072. Terminal->SynchronizeChecklistCalculateSize(Checklist, Items, Params.CopyParam);
  5073. }
  5074. }
  5075. //---------------------------------------------------------------------------
  5076. void __fastcall TCustomScpExplorerForm::SynchronizeNoDifferences()
  5077. {
  5078. UnicodeString Message = MainInstructions(LoadStr(COMPARE_NO_DIFFERENCES));
  5079. MessageDialog(Message, qtInformation, qaOK, HELP_SYNCHRONIZE_NO_DIFFERENCES);
  5080. }
  5081. //---------------------------------------------------------------------------
  5082. int __fastcall TCustomScpExplorerForm::DoFullSynchronizeDirectories(
  5083. UnicodeString & LocalDirectory, UnicodeString & RemoteDirectory,
  5084. TSynchronizeMode & Mode, bool & SaveMode, bool UseDefaults)
  5085. {
  5086. int Result;
  5087. int Params = GUIConfiguration->SynchronizeParams;
  5088. bool SaveSettings = false;
  5089. int Options =
  5090. FLAGMASK(!Terminal->IsCapable[fcTimestampChanging], fsoDisableTimestamp) |
  5091. FLAGMASK(SynchronizeAllowSelectedOnly(), fsoAllowSelectedOnly);
  5092. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  5093. TUsableCopyParamAttrs CopyParamAttrs = Terminal->UsableCopyParamAttrs(0);
  5094. bool Continue =
  5095. UseDefaults ||
  5096. DoFullSynchronizeDialog(Mode, Params, LocalDirectory, RemoteDirectory,
  5097. &CopyParam, SaveSettings, SaveMode, Options, CopyParamAttrs);
  5098. if (Continue)
  5099. {
  5100. Configuration->Usage->Inc(L"Synchronizations");
  5101. UpdateCopyParamCounters(CopyParam);
  5102. TSynchronizeOptions SynchronizeOptions;
  5103. GetSynchronizeOptions(Params, SynchronizeOptions);
  5104. if (SaveSettings)
  5105. {
  5106. GUIConfiguration->SynchronizeParams = Params;
  5107. }
  5108. else
  5109. {
  5110. SaveMode = false;
  5111. }
  5112. TDateTime StartTime = Now();
  5113. TSynchronizeChecklist * Checklist = NULL;
  5114. try
  5115. {
  5116. DebugAssert(!FAutoOperation);
  5117. FAutoOperation = true;
  5118. try
  5119. {
  5120. FSynchronizeProgressForm = new TSynchronizeProgressForm(Application, true, true);
  5121. FSynchronizeProgressForm->Start();
  5122. Checklist = Terminal->SynchronizeCollect(LocalDirectory, RemoteDirectory,
  5123. static_cast<TTerminal::TSynchronizeMode>(Mode),
  5124. &CopyParam, Params | TTerminal::spNoConfirmation, TerminalSynchronizeDirectory,
  5125. &SynchronizeOptions);
  5126. }
  5127. __finally
  5128. {
  5129. FAutoOperation = false;
  5130. SAFE_DESTROY(FSynchronizeProgressForm);
  5131. }
  5132. TSynchronizeParams SynchronizeParams;
  5133. SynchronizeParams.LocalDirectory = LocalDirectory;
  5134. SynchronizeParams.RemoteDirectory = RemoteDirectory;
  5135. SynchronizeParams.Mode = Mode;
  5136. SynchronizeParams.CopyParam = &CopyParam;
  5137. SynchronizeParams.Params = Params;
  5138. SynchronizeParams.Checklist = Checklist;
  5139. SynchronizeParams.StartTime = &StartTime;
  5140. SynchronizeParams.OnProcessedItem = NULL;
  5141. if (FLAGSET(Params, TTerminal::spPreviewChanges))
  5142. {
  5143. if (Checklist->Count > 0)
  5144. {
  5145. if (DoSynchronizeChecklistDialog(
  5146. Checklist, Mode, Params, LocalDirectory, RemoteDirectory, CustomCommandMenu, DoFullSynchronize,
  5147. DoSynchronizeChecklistCalculateSize, &SynchronizeParams))
  5148. {
  5149. Result = Checklist->Count;
  5150. }
  5151. else
  5152. {
  5153. Result = -1;
  5154. }
  5155. }
  5156. else
  5157. {
  5158. SynchronizeNoDifferences();
  5159. }
  5160. }
  5161. else
  5162. {
  5163. Result = Checklist->CheckedCount;
  5164. if (Checklist->CheckedCount > 0)
  5165. {
  5166. FullSynchronize(SynchronizeParams, NULL, NULL);
  5167. }
  5168. else
  5169. {
  5170. SynchronizeNoDifferences();
  5171. }
  5172. }
  5173. }
  5174. __finally
  5175. {
  5176. delete Checklist;
  5177. }
  5178. OperationComplete(StartTime);
  5179. }
  5180. else
  5181. {
  5182. Result = -1;
  5183. }
  5184. return Result;
  5185. }
  5186. //---------------------------------------------------------------------------
  5187. void __fastcall TCustomScpExplorerForm::TerminalSynchronizeDirectory(
  5188. const UnicodeString LocalDirectory, const UnicodeString RemoteDirectory,
  5189. bool & Continue, bool Collect)
  5190. {
  5191. if (Collect)
  5192. {
  5193. DebugAssert(FSynchronizeProgressForm != NULL);
  5194. if (!FSynchronizeProgressForm->Started)
  5195. {
  5196. FSynchronizeProgressForm->Start();
  5197. }
  5198. FSynchronizeProgressForm->SetData(LocalDirectory, RemoteDirectory, Continue);
  5199. }
  5200. else
  5201. {
  5202. DebugAssert(FSynchronizeProgressForm == NULL);
  5203. DebugAssert(FProgressForm != NULL);
  5204. }
  5205. }
  5206. //---------------------------------------------------------------------------
  5207. void __fastcall TCustomScpExplorerForm::StandaloneEdit(const UnicodeString & FileName)
  5208. {
  5209. UnicodeString FullFileName = AbsolutePath(FTerminal->CurrentDirectory, FileName);
  5210. TRemoteFile * File = NULL;
  5211. Terminal->ReadFile(FullFileName, File);
  5212. if (File != NULL)
  5213. {
  5214. std::unique_ptr<TRemoteFile> FileOwner(File);
  5215. TAutoFlag Flag(FStandaloneEditing);
  5216. ExecuteRemoteFile(FullFileName, File, efInternalEditor);
  5217. Application->ShowMainForm = false;
  5218. Application->Run();
  5219. }
  5220. }
  5221. //---------------------------------------------------------------------------
  5222. void __fastcall TCustomScpExplorerForm::ExploreLocalDirectory()
  5223. {
  5224. DebugFail();
  5225. }
  5226. //---------------------------------------------------------------------------
  5227. TSessionData * __fastcall TCustomScpExplorerForm::CloneCurrentSessionData()
  5228. {
  5229. std::unique_ptr<TSessionData> SessionData(new TSessionData(L""));
  5230. SessionData->Assign(Terminal->SessionData);
  5231. UpdateSessionData(SessionData.get());
  5232. TTerminalManager::Instance()->UpdateSessionCredentials(SessionData.get());
  5233. if (Terminal->SessionData->IsWorkspace)
  5234. {
  5235. // Have to reset the "Workspace/XXX" name which would become user-visible
  5236. // once IsWorkspace is cleared
  5237. SessionData->Name = UnicodeString();
  5238. SessionData->IsWorkspace = false;
  5239. }
  5240. return SessionData.release();
  5241. }
  5242. //---------------------------------------------------------------------------
  5243. void __fastcall TCustomScpExplorerForm::SaveCurrentSession()
  5244. {
  5245. TSessionData * SessionData = CloneCurrentSessionData();
  5246. try
  5247. {
  5248. TSessionData * EditingSessionData = StoredSessions->FindSame(SessionData);
  5249. DoSaveSession(SessionData, EditingSessionData, true, NULL);
  5250. }
  5251. __finally
  5252. {
  5253. delete SessionData;
  5254. }
  5255. }
  5256. //---------------------------------------------------------------------------
  5257. TObjectList * __fastcall TCustomScpExplorerForm::DoCollectWorkspace()
  5258. {
  5259. TTerminalManager * Manager = TTerminalManager::Instance();
  5260. std::unique_ptr<TObjectList> DataList(new TObjectList());
  5261. if (DebugAlwaysTrue(Terminal != NULL))
  5262. {
  5263. // Update (Managed)Terminal->StateData
  5264. UpdateTerminal(Terminal);
  5265. }
  5266. Manager->SaveWorkspace(DataList.get());
  5267. return DataList.release();
  5268. }
  5269. //---------------------------------------------------------------------------
  5270. void __fastcall TCustomScpExplorerForm::DoSaveWorkspace(
  5271. const UnicodeString & Name, TObjectList * DataList, bool SavePasswords, bool Explicit)
  5272. {
  5273. WinConfiguration->LastWorkspace = Name;
  5274. if (!SavePasswords)
  5275. {
  5276. for (int Index = 0; Index < DataList->Count; Index++)
  5277. {
  5278. TSessionData * SessionData = dynamic_cast<TSessionData *>(DataList->Items[Index]);
  5279. if (SessionData->Link.IsEmpty())
  5280. {
  5281. SessionData->ClearSessionPasswords();
  5282. }
  5283. }
  5284. }
  5285. StoredSessions->NewWorkspace(Name, DataList);
  5286. // modified only
  5287. StoredSessions->Save(false, Explicit);
  5288. }
  5289. //---------------------------------------------------------------------------
  5290. UnicodeString __fastcall TCustomScpExplorerForm::WorkspaceName()
  5291. {
  5292. return
  5293. DefaultStr(WinConfiguration->LastWorkspace,
  5294. DefaultStr(WinConfiguration->AutoWorkspace, LoadStr(NEW_WORKSPACE)));
  5295. }
  5296. //---------------------------------------------------------------------------
  5297. bool __fastcall TCustomScpExplorerForm::SaveWorkspace(bool EnableAutoSave)
  5298. {
  5299. std::unique_ptr<TObjectList> DataList(DoCollectWorkspace());
  5300. bool AnyNonStoredSessionWithPassword = false;
  5301. bool AnyNonStoredNonWorkspaceSessionWithPassword = false;
  5302. bool AllNonStoredSessionsAnonymous = true;
  5303. TTerminalManager * Manager = TTerminalManager::Instance();
  5304. for (int Index = 0; Index < DataList->Count; Index++)
  5305. {
  5306. TSessionData * SessionData = dynamic_cast<TSessionData *>(DataList->Items[Index]);
  5307. if (SessionData->Link.IsEmpty())
  5308. {
  5309. if (SessionData->HasAnySessionPassword())
  5310. {
  5311. AnyNonStoredSessionWithPassword = true;
  5312. if (!Manager->Terminals[Index]->SessionData->IsWorkspace)
  5313. {
  5314. AnyNonStoredNonWorkspaceSessionWithPassword = true;
  5315. }
  5316. }
  5317. if (!SameText(SessionData->UserName, AnonymousUserName))
  5318. {
  5319. AllNonStoredSessionsAnonymous = false;
  5320. }
  5321. }
  5322. }
  5323. bool SavePasswords;
  5324. bool * PSavePasswords;
  5325. bool NotRecommendedSavingPasswords =
  5326. !CustomWinConfiguration->UseMasterPassword &&
  5327. !AllNonStoredSessionsAnonymous;
  5328. if (Configuration->DisablePasswordStoring ||
  5329. !AnyNonStoredSessionWithPassword)
  5330. {
  5331. PSavePasswords = NULL;
  5332. SavePasswords = false;
  5333. }
  5334. else
  5335. {
  5336. PSavePasswords = &SavePasswords;
  5337. SavePasswords =
  5338. !AnyNonStoredNonWorkspaceSessionWithPassword ||
  5339. !NotRecommendedSavingPasswords;
  5340. }
  5341. UnicodeString Name = WorkspaceName();
  5342. bool CreateShortcut = false;
  5343. bool Result =
  5344. DoSaveWorkspaceDialog(
  5345. Name, PSavePasswords, NotRecommendedSavingPasswords, CreateShortcut,
  5346. EnableAutoSave);
  5347. if (Result)
  5348. {
  5349. DoSaveWorkspace(Name, DataList.get(), SavePasswords, true);
  5350. if (CreateShortcut)
  5351. {
  5352. TOperationVisualizer Visualizer;
  5353. UnicodeString AdditionalParams = TProgramParams::FormatSwitch(DESKTOP_SWITCH);
  5354. CreateDesktopSessionShortCut(Name, L"", AdditionalParams, -1, WORKSPACE_ICON);
  5355. }
  5356. if (EnableAutoSave)
  5357. {
  5358. WinConfiguration->AutoSaveWorkspace = true;
  5359. WinConfiguration->AutoWorkspace = Name;
  5360. if (PSavePasswords != NULL)
  5361. {
  5362. WinConfiguration->AutoSaveWorkspacePasswords = SavePasswords;
  5363. }
  5364. }
  5365. }
  5366. return Result;
  5367. }
  5368. //---------------------------------------------------------------------------
  5369. void __fastcall TCustomScpExplorerForm::UpdateTerminal(TTerminal * Terminal)
  5370. {
  5371. TManagedTerminal * ManagedTerminal = dynamic_cast<TManagedTerminal *>(Terminal);
  5372. DebugAssert(ManagedTerminal != NULL);
  5373. SAFE_DESTROY(ManagedTerminal->RemoteExplorerState);
  5374. if (WinConfiguration->PreservePanelState)
  5375. {
  5376. ManagedTerminal->RemoteExplorerState = RemoteDirView->SaveState();
  5377. }
  5378. UpdateSessionData(ManagedTerminal->StateData);
  5379. }
  5380. //---------------------------------------------------------------------------
  5381. void __fastcall TCustomScpExplorerForm::UpdateSessionData(TSessionData * Data)
  5382. {
  5383. // Keep in sync with TSessionData::CopyStateData
  5384. DebugAssert(Data != NULL);
  5385. // cannot use RemoteDirView->Path, because it is empty if connection
  5386. // was already closed
  5387. // also only peek, we may not be connected at all atm
  5388. // so make sure we do not try retrieving current directory from the server
  5389. // (particularly with FTP)
  5390. UnicodeString ACurrentDirectory = Terminal->PeekCurrentDirectory();
  5391. if (!ACurrentDirectory.IsEmpty())
  5392. {
  5393. Data->RemoteDirectory = ACurrentDirectory;
  5394. }
  5395. Data->Color = SessionColor;
  5396. }
  5397. //---------------------------------------------------------------------------
  5398. void __fastcall TCustomScpExplorerForm::ToolBarResize(TObject *Sender)
  5399. {
  5400. TTBXToolbar * Toolbar = dynamic_cast<TTBXToolbar*>(Sender);
  5401. DebugAssert(Toolbar != NULL);
  5402. for (int i = 0; i < Toolbar->Items->Count; i++)
  5403. {
  5404. TTBXCustomDropDownItem * DropDownItem;
  5405. DropDownItem = dynamic_cast<TTBXCustomDropDownItem *>(Toolbar->Items->Items[i]);
  5406. if (DropDownItem != NULL)
  5407. {
  5408. ToolbarItemResize(DropDownItem,
  5409. Toolbar->Width - (Toolbar->View->BaseSize.x - DropDownItem->EditWidth) -
  5410. Toolbar->NonClientWidth);
  5411. break;
  5412. }
  5413. }
  5414. }
  5415. //---------------------------------------------------------------------------
  5416. void __fastcall TCustomScpExplorerForm::ToolbarItemResize(TTBXCustomDropDownItem * Item, int Width)
  5417. {
  5418. Item->EditWidth = Width;
  5419. }
  5420. //---------------------------------------------------------------------------
  5421. void __fastcall TCustomScpExplorerForm::ToolbarGetBaseSize(
  5422. TTBCustomToolbar * Toolbar, TPoint & ASize)
  5423. {
  5424. for (int i = 0; i < Toolbar->Items->Count; i++)
  5425. {
  5426. TTBXCustomDropDownItem * DropDownItem;
  5427. DropDownItem = dynamic_cast<TTBXCustomDropDownItem *>(Toolbar->Items->Items[i]);
  5428. if (DropDownItem != NULL)
  5429. {
  5430. ASize.x -= DropDownItem->EditWidth;
  5431. ASize.x += ScaleByTextHeight(this, 50) /* minimal combo width */;
  5432. }
  5433. }
  5434. }
  5435. //---------------------------------------------------------------------------
  5436. void __fastcall TCustomScpExplorerForm::DoWarnLackOfTempSpace(
  5437. const UnicodeString Path, __int64 RequiredSpace, bool & Continue)
  5438. {
  5439. if (WinConfiguration->DDWarnLackOfTempSpace)
  5440. {
  5441. UnicodeString ADrive = ExtractFileDrive(ExpandFileName(Path));
  5442. if (!ADrive.IsEmpty())
  5443. {
  5444. __int64 FreeSpace = DiskFree((Byte)(ADrive[1]-'A'+1));
  5445. DebugAssert(RequiredSpace >= 0);
  5446. __int64 RequiredWithReserve;
  5447. RequiredWithReserve = (__int64)(RequiredSpace * WinConfiguration->DDWarnLackOfTempSpaceRatio);
  5448. if (FreeSpace < RequiredWithReserve)
  5449. {
  5450. unsigned int Result;
  5451. TMessageParams Params(mpNeverAskAgainCheck);
  5452. Result = MessageDialog(FMTLOAD(DD_WARN_LACK_OF_TEMP_SPACE, (Path,
  5453. FormatBytes(FreeSpace), FormatBytes(RequiredSpace))),
  5454. qtWarning, qaYes | qaNo, HELP_DD_WARN_LACK_OF_TEMP_SPACE, &Params);
  5455. if (Result == qaNeverAskAgain)
  5456. {
  5457. WinConfiguration->DDWarnLackOfTempSpace = false;
  5458. }
  5459. Continue = (Result == qaYes || Result == qaNeverAskAgain);
  5460. }
  5461. }
  5462. }
  5463. }
  5464. //---------------------------------------------------------------------------
  5465. void __fastcall TCustomScpExplorerForm::AddBookmark(TOperationSide Side)
  5466. {
  5467. DoOpenDirectoryDialog(odAddBookmark, Side);
  5468. }
  5469. //---------------------------------------------------------------------------
  5470. TStrings * __fastcall TCustomScpExplorerForm::CreateVisitedDirectories(TOperationSide Side)
  5471. {
  5472. // we should better use TCustomDirView::FCaseSensitive, but it is private
  5473. TStringList * VisitedDirectories = CreateSortedStringList((Side == osRemote));
  5474. try
  5475. {
  5476. TCustomDirView * DView = DirView(Side);
  5477. for (int Index = -DView->BackCount; Index <= DView->ForwardCount; Index++)
  5478. {
  5479. VisitedDirectories->Add(DView->HistoryPath[Index]);
  5480. }
  5481. }
  5482. catch (...)
  5483. {
  5484. delete VisitedDirectories;
  5485. throw;
  5486. }
  5487. return VisitedDirectories;
  5488. }
  5489. //---------------------------------------------------------------------------
  5490. void __fastcall TCustomScpExplorerForm::DoOpenDirectoryDialog(
  5491. TOpenDirectoryMode Mode, TOperationSide Side)
  5492. {
  5493. bool Continue = true;
  5494. if (Mode == odAddBookmark)
  5495. {
  5496. TMessageParams Params(mpNeverAskAgainCheck);
  5497. Params.NeverAskAgainTitle = LoadStr(ADD_BOOKMARK_SHARED);
  5498. Params.NeverAskAgainCheckedInitially = WinConfiguration->UseSharedBookmarks;
  5499. UnicodeString Message = MainInstructions(FMTLOAD(ADD_BOOKMARK_CONFIRM, (DirView(Side)->PathName)));
  5500. unsigned int Answer =
  5501. MessageDialog(Message,
  5502. qtConfirmation, qaOK | qaCancel, HELP_ADD_BOOKMARK_CONFIRM, &Params);
  5503. if (Answer == qaNeverAskAgain)
  5504. {
  5505. Continue = true;
  5506. WinConfiguration->UseSharedBookmarks = true;
  5507. }
  5508. else if (Answer == qaOK)
  5509. {
  5510. Continue = true;
  5511. WinConfiguration->UseSharedBookmarks = false;
  5512. }
  5513. else
  5514. {
  5515. Continue = false;
  5516. }
  5517. }
  5518. if (Continue)
  5519. {
  5520. TStrings * VisitedDirectories = CreateVisitedDirectories(Side);
  5521. try
  5522. {
  5523. UnicodeString Name = DirView(Side)->PathName;
  5524. if (::DoOpenDirectoryDialog(Mode, Side, Name, VisitedDirectories, Terminal,
  5525. // do not allow switching to location profiles,
  5526. // if we are not connected
  5527. HasDirView[osLocal] && (Terminal != NULL)))
  5528. {
  5529. TWindowLock Lock(this);
  5530. DirView(Side)->Path = Name;
  5531. }
  5532. }
  5533. __finally
  5534. {
  5535. delete VisitedDirectories;
  5536. }
  5537. }
  5538. }
  5539. //---------------------------------------------------------------------------
  5540. bool __fastcall TCustomScpExplorerForm::CommandSessionFallback()
  5541. {
  5542. DebugAssert(!FTerminal->CommandSessionOpened);
  5543. return TTerminalManager::Instance()->ConnectTerminal(FTerminal->CommandSession);
  5544. }
  5545. //---------------------------------------------------------------------------
  5546. bool __fastcall TCustomScpExplorerForm::EnsureCommandSessionFallback(TFSCapability Capability)
  5547. {
  5548. bool Result = FTerminal->IsCapable[Capability] ||
  5549. FTerminal->CommandSessionOpened;
  5550. if (!Result)
  5551. {
  5552. DebugAssert(FTerminal->IsCapable[fcSecondaryShell]);
  5553. if (!GUIConfiguration->ConfirmCommandSession)
  5554. {
  5555. Result = true;
  5556. }
  5557. else
  5558. {
  5559. TMessageParams Params(mpNeverAskAgainCheck);
  5560. const TFileSystemInfo & FileSystemInfo = Terminal->GetFileSystemInfo();
  5561. unsigned int Answer = MessageDialog(FMTLOAD(PERFORM_ON_COMMAND_SESSION2,
  5562. (FileSystemInfo.ProtocolName, FileSystemInfo.ProtocolName)), qtConfirmation,
  5563. qaOK | qaCancel, HELP_PERFORM_ON_COMMAND_SESSION, &Params);
  5564. if (Answer == qaNeverAskAgain)
  5565. {
  5566. GUIConfiguration->ConfirmCommandSession = false;
  5567. Result = true;
  5568. }
  5569. else if (Answer == qaOK)
  5570. {
  5571. Result = true;
  5572. }
  5573. }
  5574. if (Result)
  5575. {
  5576. Result = CommandSessionFallback();
  5577. }
  5578. }
  5579. return Result;
  5580. }
  5581. //---------------------------------------------------------------------------
  5582. void __fastcall TCustomScpExplorerForm::OpenConsole(UnicodeString Command)
  5583. {
  5584. if (EnsureCommandSessionFallback(fcAnyCommand))
  5585. {
  5586. DoConsoleDialog(Terminal, Command);
  5587. }
  5588. }
  5589. //---------------------------------------------------------------------------
  5590. void __fastcall TCustomScpExplorerForm::FileControlDDDragEnter(
  5591. TObject *Sender, _di_IDataObject /*DataObj*/, int /*grfKeyState*/,
  5592. const TPoint & /*Point*/, int & /*dwEffect*/, bool & Accept)
  5593. {
  5594. if (IsFileControl(DropSourceControl, osRemote) &&
  5595. (FDDExtMapFile != NULL))
  5596. {
  5597. Accept = true;
  5598. }
  5599. FDDTargetControl = dynamic_cast<TControl*>(Sender);
  5600. DebugAssert(FDDTargetControl != NULL);
  5601. }
  5602. //---------------------------------------------------------------------------
  5603. void __fastcall TCustomScpExplorerForm::SessionsDDDragEnter(
  5604. _di_IDataObject DataObj, int KeyState,
  5605. const TPoint & Point, int & Effect, bool & Accept)
  5606. {
  5607. FileControlDDDragEnter(SessionsPageControl, DataObj, KeyState, Point, Effect, Accept);
  5608. }
  5609. //---------------------------------------------------------------------------
  5610. void __fastcall TCustomScpExplorerForm::QueueDDDragEnter(
  5611. _di_IDataObject DataObj, int KeyState,
  5612. const TPoint & Point, int & Effect, bool & Accept)
  5613. {
  5614. FileControlDDDragEnter(QueueView3, DataObj, KeyState, Point, Effect, Accept);
  5615. }
  5616. //---------------------------------------------------------------------------
  5617. void __fastcall TCustomScpExplorerForm::FileControlDDDragLeave(
  5618. TObject * Sender)
  5619. {
  5620. DebugUsedParam(Sender);
  5621. DebugAssert(FDDTargetControl == Sender);
  5622. FDDTargetControl = NULL;
  5623. }
  5624. //---------------------------------------------------------------------------
  5625. void __fastcall TCustomScpExplorerForm::SessionsDDDragLeave()
  5626. {
  5627. FileControlDDDragLeave(SessionsPageControl);
  5628. }
  5629. //---------------------------------------------------------------------------
  5630. void __fastcall TCustomScpExplorerForm::QueueDDDragLeave()
  5631. {
  5632. FileControlDDDragLeave(QueueView3);
  5633. }
  5634. //---------------------------------------------------------------------------
  5635. void __fastcall TCustomScpExplorerForm::AddEditLink(TOperationSide Side, bool Add)
  5636. {
  5637. DebugAssert(GetSide(Side) == osRemote);
  5638. DebugUsedParam(Side);
  5639. bool Edit = false;
  5640. TRemoteFile * File = NULL;
  5641. UnicodeString FileName;
  5642. UnicodeString PointTo;
  5643. bool SymbolicLink = true;
  5644. if (RemoteDirView->ItemFocused)
  5645. {
  5646. DebugAssert(RemoteDirView->ItemFocused->Data);
  5647. File = (TRemoteFile *)RemoteDirView->ItemFocused->Data;
  5648. Edit = !Add && File->IsSymLink && Terminal->SessionData->ResolveSymlinks;
  5649. if (Edit)
  5650. {
  5651. FileName = File->FileName;
  5652. PointTo = File->LinkTo;
  5653. }
  5654. else
  5655. {
  5656. PointTo = File->FileName;
  5657. }
  5658. }
  5659. if (DoSymlinkDialog(FileName, PointTo, osRemote, SymbolicLink, Edit,
  5660. Terminal->IsCapable[fcHardLink]))
  5661. {
  5662. Configuration->Usage->Inc(L"RemoteLinksCreated");
  5663. if (Edit)
  5664. {
  5665. DebugAssert(File->FileName == FileName);
  5666. int Params = dfNoRecursive;
  5667. Terminal->ExceptionOnFail = true;
  5668. try
  5669. {
  5670. Terminal->DeleteFile(L"", File, &Params);
  5671. }
  5672. __finally
  5673. {
  5674. Terminal->ExceptionOnFail = false;
  5675. }
  5676. }
  5677. Terminal->CreateLink(FileName, PointTo, SymbolicLink);
  5678. }
  5679. }
  5680. //---------------------------------------------------------------------------
  5681. bool __fastcall TCustomScpExplorerForm::CanAddEditLink(TOperationSide Side)
  5682. {
  5683. return
  5684. (Terminal != NULL) &&
  5685. ((GetSide(Side) != osRemote) ||
  5686. (Terminal->ResolvingSymlinks &&
  5687. Terminal->IsCapable[fcSymbolicLink]));
  5688. }
  5689. //---------------------------------------------------------------------------
  5690. bool __fastcall TCustomScpExplorerForm::LinkFocused()
  5691. {
  5692. return
  5693. (FCurrentSide == osRemote) &&
  5694. (RemoteDirView->ItemFocused != NULL) &&
  5695. ((TRemoteFile *)RemoteDirView->ItemFocused->Data)->IsSymLink &&
  5696. Terminal->SessionData->ResolveSymlinks;
  5697. }
  5698. //---------------------------------------------------------------------------
  5699. void __fastcall TCustomScpExplorerForm::ExecuteCurrentFile()
  5700. {
  5701. DebugAssert(!WinConfiguration->DisableOpenEdit);
  5702. FForceExecution = true;
  5703. try
  5704. {
  5705. DirView(osCurrent)->ExecuteCurrentFile();
  5706. }
  5707. __finally
  5708. {
  5709. FForceExecution = false;
  5710. }
  5711. }
  5712. //---------------------------------------------------------------------------
  5713. void __fastcall TCustomScpExplorerForm::ExecuteCurrentFileWith(bool OnFocused)
  5714. {
  5715. TEditorData ExternalEditor;
  5716. ExternalEditor.Editor = edExternal;
  5717. bool Remember = false;
  5718. if (DoEditorPreferencesDialog(&ExternalEditor, Remember, epmAdHoc,
  5719. (GetSide(osCurrent) == osRemote)))
  5720. {
  5721. if (Remember)
  5722. {
  5723. TEditorList * EditorList = new TEditorList;
  5724. try
  5725. {
  5726. *EditorList = *WinConfiguration->EditorList;
  5727. bool Found = false;
  5728. int i = 0;
  5729. while (!Found && (i < EditorList->Count))
  5730. {
  5731. const TEditorPreferences * Editor = EditorList->Editors[i];
  5732. if ((Editor->Data->Editor == edExternal) &&
  5733. (Editor->Data->ExternalEditor == ExternalEditor.ExternalEditor))
  5734. {
  5735. Found = true;
  5736. }
  5737. i++;
  5738. }
  5739. if (!Found)
  5740. {
  5741. EditorList->Add(new TEditorPreferences(ExternalEditor));
  5742. WinConfiguration->EditorList = EditorList;
  5743. }
  5744. }
  5745. __finally
  5746. {
  5747. delete EditorList;
  5748. }
  5749. }
  5750. ExecuteFile(osCurrent, efExternalEditor, &ExternalEditor, true, OnFocused);
  5751. }
  5752. }
  5753. //---------------------------------------------------------------------------
  5754. void __fastcall TCustomScpExplorerForm::TerminalRemoved(TObject * Sender)
  5755. {
  5756. FEditorManager->ProcessFiles(FileTerminalRemoved, Sender);
  5757. if (FFileFindTerminal == Sender)
  5758. {
  5759. FFileFindTerminal = NULL;
  5760. HideFileFindDialog();
  5761. }
  5762. }
  5763. //---------------------------------------------------------------------------
  5764. void __fastcall TCustomScpExplorerForm::FileTerminalRemoved(const UnicodeString FileName,
  5765. TEditedFileData * Data, TObject * /*Token*/, void * Arg)
  5766. {
  5767. TTerminal * Terminal = static_cast<TTerminal *>(Arg);
  5768. DebugAssert(Terminal != NULL);
  5769. if (Data->Terminal == Terminal)
  5770. {
  5771. Data->Terminal = NULL;
  5772. }
  5773. }
  5774. //---------------------------------------------------------------------------
  5775. void __fastcall TCustomScpExplorerForm::LastTerminalClosed(TObject * /*Sender*/)
  5776. {
  5777. UpdateControls();
  5778. SessionColor = TColor(0);
  5779. UpdateRemotePathComboBox(false);
  5780. NeedSession(true);
  5781. }
  5782. //---------------------------------------------------------------------------
  5783. void __fastcall TCustomScpExplorerForm::NeedSession(bool ReloadSessions)
  5784. {
  5785. try
  5786. {
  5787. try
  5788. {
  5789. TTerminalManager::Instance()->NewSession(false, L"", ReloadSessions, this);
  5790. }
  5791. __finally
  5792. {
  5793. if (!WinConfiguration->KeepOpenWhenNoSession &&
  5794. (!Terminal || !Terminal->Active))
  5795. {
  5796. TerminateApplication();
  5797. }
  5798. }
  5799. }
  5800. catch (EAbort &)
  5801. {
  5802. // swallow
  5803. // The TTerminalManager does not expect the OnLastTerminalClose to throw without trying to connect.
  5804. // Also when called from TWinControl.UpdateShowing => CMShowingChanged, the showing is aborted on exception
  5805. }
  5806. }
  5807. //---------------------------------------------------------------------------
  5808. void __fastcall TCustomScpExplorerForm::DoTerminalListChanged(bool Force)
  5809. {
  5810. TStrings * TerminalList = TTerminalManager::Instance()->TerminalList;
  5811. int ActiveTerminalIndex = TTerminalManager::Instance()->ActiveTerminalIndex;
  5812. Configuration->Usage->SetMax(L"MaxOpenedSessions", TerminalList->Count);
  5813. bool ListChanged = Force || (TerminalList->Count + 1 != SessionsPageControl->PageCount);
  5814. if (!ListChanged)
  5815. {
  5816. int Index = 0;
  5817. while (!ListChanged && (Index < TerminalList->Count))
  5818. {
  5819. ListChanged =
  5820. (GetSessionTabTerminal(SessionsPageControl->Pages[Index]) != TerminalList->Objects[Index]) ||
  5821. (SessionsPageControl->Pages[Index]->Caption != TerminalList->Strings[Index]);
  5822. Index++;
  5823. }
  5824. }
  5825. if (ListChanged)
  5826. {
  5827. SendMessage(SessionsPageControl->Handle, WM_SETREDRAW, 0, 0);
  5828. try
  5829. {
  5830. FSessionColors->Clear();
  5831. AddFixedSessionImages();
  5832. while (SessionsPageControl->PageCount > 0)
  5833. {
  5834. delete SessionsPageControl->Pages[0];
  5835. }
  5836. for (int Index = 0; Index < TerminalList->Count; Index++)
  5837. {
  5838. TThemeTabSheet * TabSheet = new TThemeTabSheet(SessionsPageControl);
  5839. TabSheet->Caption = TerminalList->Strings[Index];
  5840. TTerminal * Terminal = dynamic_cast<TTerminal *>(TerminalList->Objects[Index]);
  5841. TabSheet->Tag = reinterpret_cast<int>(Terminal);
  5842. TabSheet->PageControl = SessionsPageControl;
  5843. UpdateSessionTab(TabSheet);
  5844. }
  5845. TTabSheet * TabSheet = new TTabSheet(SessionsPageControl);
  5846. TabSheet->PageControl = SessionsPageControl;
  5847. TabSheet->ImageIndex = FNewSessionTabImageIndex;
  5848. UpdateNewSessionTab();
  5849. }
  5850. __finally
  5851. {
  5852. SendMessage(SessionsPageControl->Handle, WM_SETREDRAW, 1, 0);
  5853. }
  5854. }
  5855. SessionsPageControl->ActivePageIndex = ActiveTerminalIndex;
  5856. }
  5857. //---------------------------------------------------------------------------
  5858. void __fastcall TCustomScpExplorerForm::TerminalListChanged(TObject * /*Sender*/)
  5859. {
  5860. DoTerminalListChanged(false);
  5861. }
  5862. //---------------------------------------------------------------------------
  5863. void __fastcall TCustomScpExplorerForm::UpdateNewSessionTab()
  5864. {
  5865. TTabSheet * TabSheet = SessionsPageControl->Pages[SessionsPageControl->PageCount - 1];
  5866. DebugAssert(TabSheet->ImageIndex == 0);
  5867. TabSheet->Caption =
  5868. WinConfiguration->SelectiveToolbarText ?
  5869. StripHotkey(StripTrailingPunctuation(NonVisualDataModule->NewSessionAction->Caption)) :
  5870. UnicodeString();
  5871. }
  5872. //---------------------------------------------------------------------------
  5873. TTerminal * __fastcall TCustomScpExplorerForm::GetSessionTabTerminal(TTabSheet * TabSheet)
  5874. {
  5875. return reinterpret_cast<TTerminal *>(TabSheet->Tag);
  5876. }
  5877. //---------------------------------------------------------------------------
  5878. void __fastcall TCustomScpExplorerForm::UpdateSessionTab(TTabSheet * TabSheet)
  5879. {
  5880. if (DebugAlwaysTrue(TabSheet != NULL))
  5881. {
  5882. TManagedTerminal * ManagedTerminal =
  5883. dynamic_cast<TManagedTerminal *>(GetSessionTabTerminal(TabSheet));
  5884. if (DebugAlwaysTrue(ManagedTerminal != NULL))
  5885. {
  5886. TColor Color =
  5887. (ManagedTerminal == FTerminal) ? FSessionColor : ManagedTerminal->StateData->Color;
  5888. TabSheet->ImageIndex = AddSessionColor(Color);
  5889. TabSheet->Caption = TTerminalManager::Instance()->GetTerminalTitle(ManagedTerminal, true);
  5890. TThemeTabSheet * ThemeTabSheet = dynamic_cast<TThemeTabSheet *>(TabSheet);
  5891. if (DebugAlwaysTrue(ThemeTabSheet != NULL))
  5892. {
  5893. ThemeTabSheet->Shadowed = !ManagedTerminal->Active;
  5894. }
  5895. }
  5896. }
  5897. }
  5898. //---------------------------------------------------------------------------
  5899. bool __fastcall TCustomScpExplorerForm::SessionTabSwitched()
  5900. {
  5901. DebugAssert(SessionsPageControl->ActivePage != NULL);
  5902. TTerminal * Terminal = GetSessionTabTerminal(SessionsPageControl->ActivePage);
  5903. bool Result = (Terminal != NULL);
  5904. if (Result)
  5905. {
  5906. TTerminalManager::Instance()->ActiveTerminal = Terminal;
  5907. }
  5908. else
  5909. {
  5910. try
  5911. {
  5912. NewSession(false);
  5913. }
  5914. __finally
  5915. {
  5916. DoTerminalListChanged(false);
  5917. }
  5918. FSessionsPageControlNewSessionTime = Now();
  5919. }
  5920. return Result;
  5921. }
  5922. //---------------------------------------------------------------------------
  5923. void __fastcall TCustomScpExplorerForm::SessionsPageControlChange(TObject * /*Sender*/)
  5924. {
  5925. SessionTabSwitched();
  5926. }
  5927. //---------------------------------------------------------------------------
  5928. void __fastcall TCustomScpExplorerForm::TransferListChange(TObject * Sender)
  5929. {
  5930. TTBXStringList * TransferList = dynamic_cast<TTBXStringList *>(Sender);
  5931. DebugAssert(TransferList != NULL);
  5932. UnicodeString Name;
  5933. if (TransferList->ItemIndex <= 0)
  5934. {
  5935. Name = L"";
  5936. }
  5937. else
  5938. {
  5939. Name = GUIConfiguration->CopyParamList->Names[TransferList->ItemIndex - 1];
  5940. }
  5941. if (FCopyParamAutoSelected.IsEmpty())
  5942. {
  5943. // if previous preset was not autoselected, make new preset the "default"
  5944. FCopyParamDefault = Name;
  5945. }
  5946. GUIConfiguration->CopyParamCurrent = Name;
  5947. }
  5948. //---------------------------------------------------------------------------
  5949. void __fastcall TCustomScpExplorerForm::UpdateTransferLabel()
  5950. {
  5951. // sanity check
  5952. bool ExistingPreset =
  5953. (FTransferListHoverIndex >= 0) &&
  5954. (FTransferListHoverIndex < 1 + GUIConfiguration->CopyParamList->Count);
  5955. DebugAssert(ExistingPreset);
  5956. if (ExistingPreset)
  5957. {
  5958. HDC DC = GetDC(0);
  5959. TCanvas * Canvas = new TCanvas();
  5960. try
  5961. {
  5962. Canvas->Handle = DC;
  5963. Canvas->Font = GetToolbarFont(this);
  5964. UnicodeString Name;
  5965. if (FTransferListHoverIndex == 0)
  5966. {
  5967. Name = L"";
  5968. }
  5969. else
  5970. {
  5971. Name = GUIConfiguration->CopyParamList->Names[FTransferListHoverIndex - 1];
  5972. }
  5973. TTBXLabelItem * TransferLabel = dynamic_cast<TTBXLabelItem*>(
  5974. static_cast<TComponent*>(GetComponent(fcTransferLabel)));
  5975. TTBXStringList * TransferList = dynamic_cast<TTBXStringList*>(
  5976. static_cast<TObject*>(GetComponent(fcTransferList)));
  5977. UnicodeString InfoStr =
  5978. GUIConfiguration->CopyParamPreset[Name].
  5979. GetInfoStr(L"; ",
  5980. FLAGMASK(Terminal != NULL, Terminal->UsableCopyParamAttrs(0).General));
  5981. int MaxWidth = TransferList->MinWidth - (2 * TransferLabel->Margin) - ScaleByTextHeight(this, 10);
  5982. if (Canvas->TextExtent(InfoStr).cx > MaxWidth)
  5983. {
  5984. while (Canvas->TextExtent(InfoStr + Ellipsis).cx > MaxWidth)
  5985. {
  5986. InfoStr.SetLength(InfoStr.Length() - 1);
  5987. }
  5988. InfoStr += Ellipsis;
  5989. }
  5990. // UpdateCaption does not cause invalidation of whole submenu, while
  5991. // setting Caption property does.
  5992. // also it probably does not resize the label, even if necessary
  5993. // (we do not want that anyway)
  5994. TransferLabel->UpdateCaption(InfoStr);
  5995. }
  5996. __finally
  5997. {
  5998. Canvas->Handle = NULL;
  5999. ReleaseDC(0, DC);
  6000. delete Canvas;
  6001. }
  6002. }
  6003. }
  6004. //---------------------------------------------------------------------------
  6005. void __fastcall TCustomScpExplorerForm::TransferListDrawItem(
  6006. TTBXCustomList * /*Sender*/, TCanvas * /*ACanvas*/, const TRect & /*ARect*/,
  6007. int /*AIndex*/, int AHoverIndex, bool & /*DrawDefault*/)
  6008. {
  6009. if (FTransferListHoverIndex != AHoverIndex)
  6010. {
  6011. FTransferListHoverIndex = AHoverIndex;
  6012. UpdateTransferLabel();
  6013. }
  6014. }
  6015. //---------------------------------------------------------------------------
  6016. void __fastcall TCustomScpExplorerForm::WMAppCommand(TMessage & Message)
  6017. {
  6018. int Command = GET_APPCOMMAND_LPARAM(Message.LParam);
  6019. TShiftState Shift = KeyDataToShiftState(GET_KEYSTATE_LPARAM(Message.LParam));
  6020. if ((Shift * (TShiftState() << ssShift << ssAlt << ssCtrl)).Empty())
  6021. {
  6022. if (Command == APPCOMMAND_BROWSER_FAVORITES)
  6023. {
  6024. OpenDirectory(GetSide(osCurrent));
  6025. Message.Result = 1;
  6026. }
  6027. else
  6028. {
  6029. TForm::Dispatch(&Message);
  6030. }
  6031. }
  6032. else
  6033. {
  6034. TForm::Dispatch(&Message);
  6035. }
  6036. }
  6037. //---------------------------------------------------------------------------
  6038. void __fastcall TCustomScpExplorerForm::CMDialogChar(TMessage & AMessage)
  6039. {
  6040. TCMDialogChar & Message = reinterpret_cast<TCMDialogChar &>(AMessage);
  6041. if ((FIgnoreNextDialogChar != 0) &&
  6042. (toupper(Message.CharCode) == toupper(FIgnoreNextDialogChar)))
  6043. {
  6044. Message.Result = 1;
  6045. }
  6046. else
  6047. {
  6048. TForm::Dispatch(&Message);
  6049. }
  6050. FIgnoreNextDialogChar = 0;
  6051. }
  6052. //---------------------------------------------------------------------------
  6053. void __fastcall TCustomScpExplorerForm::WMSysCommand(TMessage & Message)
  6054. {
  6055. // The four low-order bits are used internally by Windows
  6056. unsigned int Cmd = (Message.WParam & 0xFFF0);
  6057. // SC_RESTORE, SC_MAXIMIZE, SC_MINIMIZE - buttons on windows title
  6058. // SC_DEFAULT - double click on windows title (does not work, at least on WinXP)
  6059. // 61730 - restore thru double click - undocumented
  6060. // 61490 - maximize thru double click - undocumented
  6061. if ((Cmd == SC_RESTORE) || (Cmd == SC_MAXIMIZE) ||
  6062. (Cmd == SC_MINIMIZE) || (Cmd == SC_DEFAULT))
  6063. {
  6064. SysResizing(Cmd);
  6065. }
  6066. TForm::Dispatch(&Message);
  6067. }
  6068. //---------------------------------------------------------------------------
  6069. void __fastcall TCustomScpExplorerForm::WMQueryEndSession(TMessage & Message)
  6070. {
  6071. // We were actually never able to make ENDSESSION_CRITICAL happen.
  6072. // Also there no point returning TRUE as we are not able to
  6073. // handle the abrupt termination caused by subsequent WM_ENDSESSION cleanly.
  6074. // Hence the process termination might be safer :)
  6075. if ((Message.LParam != ENDSESSION_CRITICAL) &&
  6076. (((FQueue != NULL) && !FQueue->IsEmpty) || (FProgressForm != NULL)))
  6077. {
  6078. Message.Result = FALSE;
  6079. }
  6080. else
  6081. {
  6082. Message.Result = TRUE;
  6083. }
  6084. // Do not call default handling as that triggers OnCloseQuery,
  6085. // where our implementation will popup confirmation dialogs, what we do not want,
  6086. // as per Vista guidelines:
  6087. // https://docs.microsoft.com/en-us/windows/desktop/shutdown/shutting-down
  6088. }
  6089. //---------------------------------------------------------------------------
  6090. void __fastcall TCustomScpExplorerForm::WMEndSession(TWMEndSession & Message)
  6091. {
  6092. if (Message.EndSession && IsApplicationMinimized())
  6093. {
  6094. // WORKAROUND
  6095. // TApplication.WndProc() calls Application.Terminate() before Halt(),
  6096. // when it receives WM_ENDSESSION,
  6097. // but that sometimes (particularly when application is minimized) cause crashes.
  6098. // Crash popups message that blocks log off.
  6099. // Obviously application cannot shutdown cleanly after WM_ENDSESSION,
  6100. // so we call ExitProcess() immediately, not even trying to cleanup.
  6101. // It still causes beep, so there's likely some popup, but it does not block
  6102. // log off.
  6103. ExitProcess(0);
  6104. }
  6105. TForm::Dispatch(&Message);
  6106. }
  6107. #ifdef _DEBUG
  6108. //---------------------------------------------------------------------------
  6109. void __fastcall TCustomScpExplorerForm::WMWindowPosChanged(TWMWindowPosMsg & Message)
  6110. {
  6111. TForm::Dispatch(&Message);
  6112. }
  6113. #endif
  6114. //---------------------------------------------------------------------------
  6115. void __fastcall TCustomScpExplorerForm::SysResizing(unsigned int /*Cmd*/)
  6116. {
  6117. }
  6118. //---------------------------------------------------------------------------
  6119. void __fastcall TCustomScpExplorerForm::DoShow()
  6120. {
  6121. // only now are the controls resized finally, so the size constraints
  6122. // will not conflict with possibly very small window size
  6123. RestoreFormParams();
  6124. FixControlsPlacement();
  6125. if (Position == poDefaultPosOnly)
  6126. {
  6127. CutFormToDesktop(this);
  6128. }
  6129. TForm::DoShow();
  6130. FSessionsDragDropFilesEx->DragDropControl = SessionsPageControl;
  6131. FQueueDragDropFilesEx->DragDropControl = QueueView3;
  6132. if (Terminal == NULL)
  6133. {
  6134. StartingDisconnected();
  6135. }
  6136. UpdatePixelsPerInchMainWindowCounter();
  6137. FShowing = true;
  6138. }
  6139. //---------------------------------------------------------------------------
  6140. void __fastcall TCustomScpExplorerForm::UpdatePixelsPerInchMainWindowCounter()
  6141. {
  6142. Configuration->Usage->Set(L"PixelsPerInchMainWindow", PixelsPerInch);
  6143. }
  6144. //---------------------------------------------------------------------------
  6145. void __fastcall TCustomScpExplorerForm::StartingDisconnected()
  6146. {
  6147. DoTerminalListChanged(true);
  6148. }
  6149. //---------------------------------------------------------------------------
  6150. void __fastcall TCustomScpExplorerForm::PopupTrayBalloon(TTerminal * Terminal,
  6151. const UnicodeString & Str, TQueryType Type, Exception * E, unsigned int Seconds,
  6152. TNotifyEvent OnBalloonClick, TObject * UserData)
  6153. {
  6154. bool Do;
  6155. UnicodeString Message;
  6156. if (E == NULL)
  6157. {
  6158. Message = Str;
  6159. Do = true;
  6160. }
  6161. else
  6162. {
  6163. Do = ExceptionMessage(E, Message);
  6164. }
  6165. if (Do && WinConfiguration->BalloonNotifications)
  6166. {
  6167. UnicodeString Title;
  6168. if ((Terminal == NULL) && (Type == qtInformation) && ExtractMainInstructions(Message, Title))
  6169. {
  6170. Message = TrimLeft(Message);
  6171. }
  6172. else
  6173. {
  6174. Message = UnformatMessage(Message);
  6175. const ResourceString * Captions[] = { &_SMsgDlgConfirm, &_SMsgDlgWarning,
  6176. &_SMsgDlgError, &_SMsgDlgInformation, NULL };
  6177. Title = LoadResourceString(Captions[Type]);
  6178. if (Terminal != NULL)
  6179. {
  6180. Title = FORMAT(L"%s - %s",
  6181. (TTerminalManager::Instance()->GetTerminalTitle(Terminal, true), Title));
  6182. }
  6183. }
  6184. if (Seconds == 0)
  6185. {
  6186. Seconds = WinConfiguration->NotificationsTimeout;
  6187. }
  6188. FTrayIcon->PopupBalloon(Title, Message, Type, Seconds * MSecsPerSec, OnBalloonClick, UserData);
  6189. }
  6190. }
  6191. //---------------------------------------------------------------------------
  6192. unsigned int __fastcall TCustomScpExplorerForm::MoreMessageDialog(const UnicodeString Message,
  6193. TStrings * MoreMessages, TQueryType Type, unsigned int Answers,
  6194. UnicodeString HelpKeyword, const TMessageParams * Params,
  6195. TTerminal * Terminal)
  6196. {
  6197. if (((WinConfiguration->ContinueOnError && (FErrorList != NULL)) ||
  6198. (FOnFeedSynchronizeError != NULL)) &&
  6199. (Params != NULL) && (Params->Params & mpAllowContinueOnError) )
  6200. {
  6201. if (FOnFeedSynchronizeError != NULL)
  6202. {
  6203. FOnFeedSynchronizeError(Message, MoreMessages, Type, HelpKeyword);
  6204. }
  6205. else
  6206. {
  6207. DebugAssert(FErrorList != NULL);
  6208. TStringList * MoreMessagesCopy = NULL;
  6209. if (MoreMessages)
  6210. {
  6211. MoreMessagesCopy = new TStringList();
  6212. MoreMessagesCopy->Assign(MoreMessages);
  6213. }
  6214. FErrorList->AddObject(Message, MoreMessagesCopy);
  6215. }
  6216. PopupTrayBalloon(Terminal, Message, Type);
  6217. return ContinueAnswer(Answers);
  6218. }
  6219. else
  6220. {
  6221. bool UseBalloon = FTrayIcon->Visible;
  6222. if (UseBalloon)
  6223. {
  6224. PopupTrayBalloon(Terminal, Message, Type);
  6225. }
  6226. unsigned int Result;
  6227. try
  6228. {
  6229. Result = ::MoreMessageDialog(Message, MoreMessages, Type, Answers, HelpKeyword, Params);
  6230. }
  6231. __finally
  6232. {
  6233. // cancel only balloon we popped up, otherwise we may cancel notification
  6234. // balloon that was there before the message dialog
  6235. // (such as "dd confirmation opt in balloon)
  6236. if (UseBalloon)
  6237. {
  6238. FTrayIcon->CancelBalloon();
  6239. }
  6240. }
  6241. return Result;
  6242. }
  6243. }
  6244. //---------------------------------------------------------------------------
  6245. void __fastcall TCustomScpExplorerForm::ShowExtendedException(
  6246. TTerminal * Terminal, Exception * E)
  6247. {
  6248. if (FTrayIcon->Visible)
  6249. {
  6250. PopupTrayBalloon(Terminal, L"", qtError, E);
  6251. }
  6252. // particularly prevent opening new session from jump list,
  6253. // while exception is shown
  6254. NonVisualDataModule->StartBusy();
  6255. try
  6256. {
  6257. ShowExtendedExceptionEx(Terminal, E);
  6258. }
  6259. __finally
  6260. {
  6261. NonVisualDataModule->EndBusy();
  6262. FTrayIcon->CancelBalloon();
  6263. }
  6264. }
  6265. //---------------------------------------------------------------------------
  6266. void __fastcall TCustomScpExplorerForm::TerminalReady()
  6267. {
  6268. // cannot rely on active page being page for active terminal,
  6269. // as it can happen that active page is the "new session" page
  6270. // (e.g. when reconnecting active terminal, while login dialog
  6271. // invoked from "new session" page is modal)
  6272. int ActiveTerminalIndex = TTerminalManager::Instance()->ActiveTerminalIndex;
  6273. UpdateSessionTab(SessionsPageControl->Pages[ActiveTerminalIndex]);
  6274. }
  6275. //---------------------------------------------------------------------------
  6276. void __fastcall TCustomScpExplorerForm::InactiveTerminalException(
  6277. TTerminal * Terminal, Exception * E)
  6278. {
  6279. Notify(Terminal, L"", qtError, false, NULL, NULL, E);
  6280. if (!Terminal->Active)
  6281. {
  6282. int Index = TTerminalManager::Instance()->IndexOf(Terminal);
  6283. if (DebugAlwaysTrue((Index >= 0) && (Index < SessionsPageControl->PageCount)))
  6284. {
  6285. UpdateSessionTab(SessionsPageControl->Pages[Index]);
  6286. }
  6287. }
  6288. }
  6289. //---------------------------------------------------------------------------
  6290. void __fastcall TCustomScpExplorerForm::Notify(TTerminal * Terminal,
  6291. UnicodeString Message, TQueryType Type,
  6292. bool Important, TNotifyEvent OnClick, TObject * UserData, Exception * E)
  6293. {
  6294. if ((E == NULL) ||
  6295. ExceptionMessage(E, Message))
  6296. {
  6297. unsigned int Seconds = WinConfiguration->NotificationsTimeout;
  6298. if (Important)
  6299. {
  6300. Seconds *= 5;
  6301. }
  6302. UnicodeString NoteMessage(UnformatMessage(Message));
  6303. if (Terminal != NULL)
  6304. {
  6305. NoteMessage = FORMAT(L"%s: %s",
  6306. (TTerminalManager::Instance()->GetTerminalTitle(Terminal, true), NoteMessage));
  6307. }
  6308. if (WinConfiguration->BalloonNotifications)
  6309. {
  6310. AddNote(NoteMessage);
  6311. PopupTrayBalloon(Terminal, Message, Type, NULL, Seconds, OnClick, UserData);
  6312. }
  6313. else
  6314. {
  6315. FlashOnBackground();
  6316. PostNote(NoteMessage, Seconds, OnClick, UserData);
  6317. }
  6318. }
  6319. }
  6320. //---------------------------------------------------------------------------
  6321. void __fastcall TCustomScpExplorerForm::QueueEmptyNoteClicked(TObject * Sender)
  6322. {
  6323. RestoreApp();
  6324. TTerminalNoteData * TerminalNoteData = dynamic_cast<TTerminalNoteData *>(Sender);
  6325. if (DebugAlwaysTrue(TerminalNoteData != NULL) &&
  6326. !NonVisualDataModule->Busy)
  6327. {
  6328. TTerminal * Terminal = TerminalNoteData->Terminal;
  6329. TTerminalManager::Instance()->ActiveTerminal = Terminal;
  6330. if (!ComponentVisible[fcQueueView])
  6331. {
  6332. ToggleQueueVisibility();
  6333. GoToQueue();
  6334. }
  6335. }
  6336. }
  6337. //---------------------------------------------------------------------------
  6338. void __fastcall TCustomScpExplorerForm::QueueEvent(TTerminal * ATerminal,
  6339. TTerminalQueue * /*Queue*/, TQueueEvent Event)
  6340. {
  6341. TManagedTerminal * ManagedTerminal = DebugNotNull(dynamic_cast<TManagedTerminal *>(ATerminal));
  6342. UnicodeString Message;
  6343. TNotifyEvent OnClick = NULL;
  6344. TObject * UserData = NULL;
  6345. bool QueueInvisible = !ComponentVisible[fcQueueView] || IsApplicationMinimized();
  6346. switch (Event)
  6347. {
  6348. case qeEmptyButMonitored:
  6349. if ((ATerminal != Terminal) || QueueInvisible)
  6350. {
  6351. Message = LoadStr(BALLOON_QUEUE_EMPTY);
  6352. OnClick = QueueEmptyNoteClicked;
  6353. TTerminalNoteData * TerminalNoteData = new TTerminalNoteData();
  6354. TerminalNoteData->Terminal = ATerminal;
  6355. UserData = TerminalNoteData;
  6356. }
  6357. break;
  6358. case qeEmpty:
  6359. OperationComplete(ManagedTerminal->QueueOperationStart);
  6360. break;
  6361. case qePendingUserAction:
  6362. if ((ATerminal != Terminal) ||
  6363. (QueueInvisible && !IsQueueAutoPopup()))
  6364. {
  6365. Message = LoadStr(BALLOON_QUEUE_USER_ACTION);
  6366. }
  6367. break;
  6368. default:
  6369. DebugFail();
  6370. }
  6371. if (!Message.IsEmpty())
  6372. {
  6373. Notify(ATerminal, Message, qtInformation, false, OnClick, UserData);
  6374. }
  6375. }
  6376. //---------------------------------------------------------------------------
  6377. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDCreateDragFileList(
  6378. TObject * /*Sender*/, TFileList * FileList, bool & Created)
  6379. {
  6380. if (FDDExtMapFile != NULL)
  6381. {
  6382. CloseHandle(FDDExtMapFile);
  6383. FDDExtMapFile = NULL;
  6384. }
  6385. if (WinConfiguration->DDFakeFile)
  6386. {
  6387. DDFakeFileInitDrag(FileList, Created);
  6388. }
  6389. }
  6390. //---------------------------------------------------------------------------
  6391. void __fastcall TCustomScpExplorerForm::DDFakeCreated(TObject * /*Sender*/, const UnicodeString FileName)
  6392. {
  6393. if (DebugAlwaysTrue(!FDragFakeDirectory.IsEmpty()) &&
  6394. SameText(ExtractFileName(FileName), ExtractFileName(FDragFakeDirectory)))
  6395. {
  6396. FFakeFileDropTarget = FileName;
  6397. }
  6398. }
  6399. //---------------------------------------------------------------------------
  6400. void __fastcall TCustomScpExplorerForm::DDFakeFileInitDrag(TFileList * FileList,
  6401. bool & Created)
  6402. {
  6403. FFakeFileDropTarget = UnicodeString();
  6404. FDragFakeDirectory =
  6405. ExcludeTrailingBackslash(WinConfiguration->TemporaryDir());
  6406. if (!ForceDirectories(ApiPath(FDragFakeDirectory)))
  6407. {
  6408. throw Exception(FMTLOAD(CREATE_TEMP_DIR_ERROR, (FDragFakeDirectory)));
  6409. }
  6410. FileSetAttr(ApiPath(FDragFakeDirectory), faHidden);
  6411. FileList->AddItem(NULL, FDragFakeDirectory);
  6412. Created = true;
  6413. if (!WinConfiguration->IsDDExtRunning() || (GetWindowsBuild() >= 17134))
  6414. {
  6415. FDragFakeMonitors = StartCreationDirectoryMonitorsOnEachDrive(FILE_NOTIFY_CHANGE_DIR_NAME, DDFakeCreated);
  6416. }
  6417. FDDExtMapFile = CreateFileMappingA((HANDLE)0xFFFFFFFF, NULL, PAGE_READWRITE,
  6418. 0, sizeof(TDragExtCommStruct), AnsiString(DRAG_EXT_MAPPING).c_str());
  6419. {
  6420. TMutexGuard Guard(FDDExtMutex, DRAGEXT_MUTEX_RELEASE_TIMEOUT);
  6421. TDragExtCommStruct* CommStruct;
  6422. CommStruct = static_cast<TDragExtCommStruct*>(MapViewOfFile(FDDExtMapFile,
  6423. FILE_MAP_ALL_ACCESS, 0, 0, 0));
  6424. DebugAssert(CommStruct != NULL);
  6425. CommStruct->Version = TDragExtCommStruct::CurrentVersion;
  6426. CommStruct->Dragging = true;
  6427. wcsncpy(CommStruct->DropDest, FDragFakeDirectory.c_str(),
  6428. LENOF(CommStruct->DropDest));
  6429. NULL_TERMINATE(CommStruct->DropDest);
  6430. UnmapViewOfFile(CommStruct);
  6431. }
  6432. FDDMoveSlipped = false;
  6433. }
  6434. //---------------------------------------------------------------------------
  6435. bool __fastcall TCustomScpExplorerForm::RemoteFileControlFileOperation(
  6436. TObject * Sender, TFileOperation Operation, bool NoConfirmation, void * Param)
  6437. {
  6438. bool Result;
  6439. if (Sender == RemoteDirView)
  6440. {
  6441. Result = ExecuteFileOperation(Operation, osRemote, true, NoConfirmation, Param);
  6442. }
  6443. else
  6444. {
  6445. DebugAssert(Sender == RemoteDriveView);
  6446. TStrings * FileList = RemoteDriveView->DragFileList();
  6447. try
  6448. {
  6449. Result = ExecuteFileOperation(Operation, osRemote, FileList, NoConfirmation, Param);
  6450. }
  6451. __finally
  6452. {
  6453. delete FileList;
  6454. }
  6455. }
  6456. if (FDDTargetControl == RemoteDriveView)
  6457. {
  6458. RemoteDriveView->UpdateDropTarget();
  6459. }
  6460. // foRemoteMove happens when file/dir is dragged within the remote tree view
  6461. // or from tree view to dir view.
  6462. // foMove happens when file/dir is dragged from remote tree view outside of
  6463. // application via dragex
  6464. if (((Operation == foRemoteMove) || (Operation == foMove)) &&
  6465. (DropSourceControl == RemoteDriveView))
  6466. {
  6467. RemoteDriveView->UpdateDropSource();
  6468. }
  6469. return Result;
  6470. }
  6471. //---------------------------------------------------------------------------
  6472. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDEnd(TObject * Sender)
  6473. {
  6474. // This also handles drops of remote files to queue.
  6475. // Drops of local files (uploads) are handled in QueueDDProcessDropped.
  6476. SAFE_DESTROY(FDDFileList);
  6477. if (!FFakeFileDropTarget.IsEmpty())
  6478. {
  6479. RemoveDir(ApiPath(FFakeFileDropTarget));
  6480. }
  6481. if ((FDDExtMapFile != NULL) || (FDDTargetControl == QueueView3))
  6482. {
  6483. try
  6484. {
  6485. TDragResult DDResult = (Sender == RemoteDirView) ?
  6486. RemoteDirView->LastDDResult : RemoteDriveView->LastDDResult;
  6487. // Focus is moved to the target application,
  6488. // but as we are going to present the UI, we need to steal the focus back.
  6489. // This most likely won't work though (windows does not allow application
  6490. // to steal focus most of the time)
  6491. Application->BringToFront();
  6492. // On older version of Windows we never got drMove here, see also comment below.
  6493. // On Windows 10, we get the "move".
  6494. if ((DDResult == drCopy) || (DDResult == drMove) || (DDResult == drInvalid))
  6495. {
  6496. UnicodeString TargetDirectory;
  6497. TFileOperation Operation;
  6498. // drInvalid may mean drMove, see comment below
  6499. switch (DDResult)
  6500. {
  6501. case drCopy:
  6502. Operation = foCopy;
  6503. break;
  6504. case drMove:
  6505. Operation = foMove;
  6506. break;
  6507. default:
  6508. DebugFail();
  6509. case drInvalid:
  6510. // prefer "copy" for safety
  6511. Operation = FLAGSET(FLastDropEffect, DROPEFFECT_MOVE) ? foMove : foCopy;
  6512. break;
  6513. }
  6514. if (FDDMoveSlipped)
  6515. {
  6516. Operation = foMove;
  6517. }
  6518. TTransferOperationParam Param;
  6519. UnicodeString CounterName;
  6520. bool ForceQueue;
  6521. if (!DDGetTarget(Param.TargetDirectory, ForceQueue, CounterName))
  6522. {
  6523. // we get drInvalid both if d&d was intercepted by ddext,
  6524. // and when users drops on no-drop location.
  6525. // we tell the difference by existence of response from ddext,
  6526. // so we ignore absence of response in this case
  6527. if (DDResult != drInvalid)
  6528. {
  6529. // here we know that the extension is installed,
  6530. // as it is checked as soon as drag&drop starts from
  6531. // RemoteFileControlDDCreateDragFileList
  6532. Configuration->Usage->Inc(L"DownloadsDragDropExternalExtTargetUnknown");
  6533. throw ExtException(NULL, LoadStr(DRAGEXT_TARGET_UNKNOWN2), HELP_DRAGEXT_TARGET_UNKNOWN);
  6534. }
  6535. }
  6536. else
  6537. {
  6538. // download using fake file
  6539. Param.Temp = false;
  6540. Param.DragDrop = true;
  6541. if (ForceQueue)
  6542. {
  6543. Param.Queue = asOn;
  6544. }
  6545. if (Sender == RemoteDirView)
  6546. {
  6547. Param.Options = FLAGMASK(SelectedAllFilesInDirView(RemoteDirView), coAllFiles);
  6548. }
  6549. if (RemoteFileControlFileOperation(Sender, Operation,
  6550. (WinConfiguration->DDTransferConfirmation == asOff), &Param))
  6551. {
  6552. Configuration->Usage->Inc(CounterName);
  6553. }
  6554. }
  6555. }
  6556. }
  6557. __finally
  6558. {
  6559. CloseHandle(FDDExtMapFile);
  6560. FDDExtMapFile = NULL;
  6561. delete FDragFakeMonitors;
  6562. FDragFakeMonitors = NULL;
  6563. RemoveDir(ApiPath(FDragFakeDirectory));
  6564. FDragFakeDirectory = L"";
  6565. FFakeFileDropTarget = UnicodeString();
  6566. }
  6567. }
  6568. if (FDragDropSshTerminate.get() != NULL)
  6569. {
  6570. FDragDropSshTerminate->Rethrow();
  6571. }
  6572. }
  6573. //---------------------------------------------------------------------------
  6574. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDGiveFeedback(
  6575. TObject * Sender, int dwEffect, HRESULT & /*Result*/)
  6576. {
  6577. HCURSOR SlippedCopyCursor;
  6578. FDDMoveSlipped =
  6579. (FDragMoveCursor != NULL) &&
  6580. (!WinConfiguration->DDAllowMoveInit) && (dwEffect == DROPEFFECT_Copy) &&
  6581. ((IsFileControl(FDDTargetControl, osRemote) && (GetKeyState(VK_CONTROL) >= 0) &&
  6582. FTerminal->IsCapable[fcRemoteMove]) ||
  6583. (IsFileControl(FDDTargetControl, osLocal) && (GetKeyState(VK_SHIFT) < 0)));
  6584. SlippedCopyCursor = FDDMoveSlipped ? FDragMoveCursor : Dragdrop::DefaultCursor;
  6585. DragDropFiles(Sender)->CHCopy = SlippedCopyCursor;
  6586. DragDropFiles(Sender)->CHScrollCopy = SlippedCopyCursor;
  6587. // Remember drop effect so we know (when user drops files), if we copy or move
  6588. FLastDropEffect = dwEffect;
  6589. }
  6590. //---------------------------------------------------------------------------
  6591. bool __fastcall TCustomScpExplorerForm::DDGetTarget(
  6592. UnicodeString & Directory, bool & ForceQueue, UnicodeString & CounterName)
  6593. {
  6594. bool Result;
  6595. if (FDDTargetControl == QueueView3)
  6596. {
  6597. Directory = DefaultDownloadTargetDirectory();
  6598. Result = true;
  6599. CounterName = L"DownloadsDragDropQueue";
  6600. ForceQueue = true;
  6601. }
  6602. else if (!FFakeFileDropTarget.IsEmpty())
  6603. {
  6604. Directory = ExcludeTrailingBackslash(ExtractFilePath(FFakeFileDropTarget));
  6605. Result = true;
  6606. ForceQueue = false;
  6607. CounterName = L"DownloadsDragDropFakeFile";
  6608. }
  6609. else
  6610. {
  6611. ForceQueue = false;
  6612. Enabled = false;
  6613. try
  6614. {
  6615. int Timer = 0;
  6616. Result = false;
  6617. while (!Result && (Timer < WinConfiguration->DDExtTimeout))
  6618. {
  6619. {
  6620. TMutexGuard Guard(FDDExtMutex, DRAGEXT_MUTEX_RELEASE_TIMEOUT);
  6621. TDragExtCommStruct* CommStruct;
  6622. CommStruct = static_cast<TDragExtCommStruct*>(MapViewOfFile(FDDExtMapFile,
  6623. FILE_MAP_ALL_ACCESS, 0, 0, 0));
  6624. DebugAssert(CommStruct != NULL);
  6625. Result = !CommStruct->Dragging;
  6626. if (Result)
  6627. {
  6628. Directory = ExtractFilePath(CommStruct->DropDest);
  6629. CounterName = L"DownloadsDragDropExternalExt";
  6630. }
  6631. UnmapViewOfFile(CommStruct);
  6632. }
  6633. if (!Result)
  6634. {
  6635. Sleep(50);
  6636. Timer += 50;
  6637. Application->ProcessMessages();
  6638. }
  6639. }
  6640. }
  6641. __finally
  6642. {
  6643. Enabled = true;
  6644. }
  6645. }
  6646. return Result;
  6647. }
  6648. //---------------------------------------------------------------------------
  6649. void __fastcall TCustomScpExplorerForm::AddDelayedDirectoryDeletion(
  6650. const UnicodeString TempDir, int SecDelay)
  6651. {
  6652. TDateTime Alarm = Now() + (double)((double)SecDelay*OneMillisecond);
  6653. FDelayedDeletionList->AddObject(TempDir, reinterpret_cast<TObject*>(Alarm.FileDate()));
  6654. if (FDelayedDeletionTimer == NULL)
  6655. {
  6656. DebugAssert(HandleAllocated());
  6657. FDelayedDeletionTimer = new TTimer(this);
  6658. FDelayedDeletionTimer->Interval = 10000;
  6659. FDelayedDeletionTimer->OnTimer = DoDelayedDeletion;
  6660. }
  6661. else
  6662. {
  6663. FDelayedDeletionTimer->Enabled = true;
  6664. }
  6665. }
  6666. //---------------------------------------------------------------------------
  6667. void __fastcall TCustomScpExplorerForm::DoDelayedDeletion(TObject * Sender)
  6668. {
  6669. DebugAssert(FDelayedDeletionList != NULL);
  6670. TDateTime N = Now();
  6671. TDateTime Alert;
  6672. UnicodeString Directory;
  6673. for (int Index = FDelayedDeletionList->Count-1; Index >= 0; Index--)
  6674. {
  6675. Alert = FileDateToDateTime(reinterpret_cast<int>(FDelayedDeletionList->Objects[Index]));
  6676. if ((N >= Alert) || (Sender == NULL))
  6677. {
  6678. Directory = FDelayedDeletionList->Strings[Index];
  6679. if (DeleteDirectory(ExcludeTrailingBackslash(Directory)))
  6680. {
  6681. FDelayedDeletionList->Delete(Index);
  6682. }
  6683. }
  6684. }
  6685. if (FDelayedDeletionList->Count == 0)
  6686. {
  6687. FDelayedDeletionTimer->Enabled = false;
  6688. }
  6689. }
  6690. //---------------------------------------------------------------------------
  6691. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDTargetDrop()
  6692. {
  6693. if (IsFileControl(FDDTargetControl, osRemote) ||
  6694. (FDDTargetControl == SessionsPageControl))
  6695. {
  6696. TTerminal * TargetTerminal = NULL;
  6697. TFileOperation Operation = foNone;
  6698. if (FDDTargetControl == SessionsPageControl)
  6699. {
  6700. TPoint Point = SessionsPageControl->ScreenToClient(Mouse->CursorPos);
  6701. int Index = SessionsPageControl->IndexOfTabAt(Point.X, Point.Y);
  6702. // do not allow dropping on the "+" tab
  6703. TargetTerminal = GetSessionTabTerminal(SessionsPageControl->Pages[Index]);
  6704. if (TargetTerminal != NULL)
  6705. {
  6706. if ((FLastDropEffect == DROPEFFECT_MOVE) &&
  6707. (TargetTerminal == TTerminalManager::Instance()->ActiveTerminal))
  6708. {
  6709. Operation = foRemoteMove;
  6710. }
  6711. else
  6712. {
  6713. Operation = foRemoteCopy;
  6714. }
  6715. }
  6716. }
  6717. else
  6718. {
  6719. // when move from remote side is disabled, we allow copying inside the remote
  6720. // panel, but we interpret is as moving (we also slip in the move cursor)
  6721. if ((FLastDropEffect == DROPEFFECT_MOVE) ||
  6722. (!WinConfiguration->DDAllowMoveInit && (FLastDropEffect == DROPEFFECT_COPY) &&
  6723. FDDMoveSlipped))
  6724. {
  6725. Operation = foRemoteMove;
  6726. }
  6727. else if (FLastDropEffect == DROPEFFECT_COPY)
  6728. {
  6729. Operation = foRemoteCopy;
  6730. }
  6731. }
  6732. if (Operation != foNone)
  6733. {
  6734. RemoteFileControlFileOperation(DropSourceControl,
  6735. Operation, (WinConfiguration->DDTransferConfirmation == asOff), TargetTerminal);
  6736. }
  6737. // abort drag&drop
  6738. Abort();
  6739. }
  6740. else if ((FDDExtMapFile == NULL) && (FLastDropEffect != DROPEFFECT_NONE) &&
  6741. // Drops of remote files to queue are handled in RemoteFileControlDDEnd
  6742. (FDDTargetControl != QueueView3))
  6743. {
  6744. DebugAssert(!FDragTempDir.IsEmpty());
  6745. TTransferType Type;
  6746. UnicodeString TempDir = FDragTempDir;
  6747. // We clear FDragTempDir before calling
  6748. // just in case it fail (raises exception)
  6749. FDragTempDir = L"";
  6750. Type = (FLastDropEffect & DROPEFFECT_MOVE ? ttMove : Type = ttCopy);
  6751. TGUICopyParamType CopyParams = GUIConfiguration->CurrentCopyParam;
  6752. // empty directory parameter means temp directory -> don't display it!
  6753. UnicodeString TargetDir = L"";
  6754. int Options = 0;
  6755. if (!CopyParamDialog(tdToLocal, Type, true, FDDFileList,
  6756. TargetDir, CopyParams, (WinConfiguration->DDTransferConfirmation != asOff), true, Options))
  6757. {
  6758. Abort();
  6759. }
  6760. // TargetDir is set when dropped on local file control
  6761. // (this was workaround for legacy dirview event handling, now it should be
  6762. // made prettier)
  6763. if (TargetDir.IsEmpty())
  6764. {
  6765. TargetDir = TempDir;
  6766. if (ForceDirectories(ApiPath(TargetDir)))
  6767. {
  6768. DebugAssert(Terminal && !TargetDir.IsEmpty());
  6769. FPendingTempSpaceWarn = true;
  6770. try
  6771. {
  6772. FDragDropOperation = true;
  6773. int Params = cpTemporary |
  6774. (Type == ttMove ? cpDelete : 0);
  6775. DDDownload(FDDFileList, TargetDir, &CopyParams, Params);
  6776. Configuration->Usage->Inc(L"DownloadsDragDropExternalTemp");
  6777. }
  6778. __finally
  6779. {
  6780. FDragDropOperation = false;
  6781. FPendingTempSpaceWarn = false;
  6782. AddDelayedDirectoryDeletion(TargetDir, WinConfiguration->DDDeleteDelay);
  6783. }
  6784. }
  6785. else
  6786. {
  6787. throw Exception(FMTLOAD(CREATE_TEMP_DIR_ERROR, (TargetDir)));
  6788. }
  6789. }
  6790. }
  6791. }
  6792. //---------------------------------------------------------------------------
  6793. void __fastcall TCustomScpExplorerForm::DDDownload(
  6794. TStrings * FilesToCopy, const UnicodeString & TargetDir, const TCopyParamType * CopyParam, int Params)
  6795. {
  6796. void * BatchStorage;
  6797. BatchStart(BatchStorage);
  6798. try
  6799. {
  6800. UpdateCopyParamCounters(*CopyParam);
  6801. Terminal->CopyToLocal(FilesToCopy, TargetDir, CopyParam, Params, NULL);
  6802. if (FLAGSET(Params, cpDelete) && (DropSourceControl == RemoteDriveView))
  6803. {
  6804. RemoteDriveView->UpdateDropSource();
  6805. }
  6806. }
  6807. __finally
  6808. {
  6809. BatchEnd(BatchStorage);
  6810. }
  6811. }
  6812. //---------------------------------------------------------------------------
  6813. class TFakeDataObjectFilesEx : public TDataObjectFilesEx
  6814. {
  6815. public:
  6816. __fastcall TFakeDataObjectFilesEx(TFileList * AFileList, bool RenderPIDL,
  6817. bool RenderFilename) : TDataObjectFilesEx(AFileList, RenderPIDL, RenderFilename)
  6818. {
  6819. }
  6820. virtual bool __fastcall AllowData(const tagFORMATETC & FormatEtc)
  6821. {
  6822. return (FormatEtc.cfFormat == CF_HDROP) ? false :
  6823. TDataObjectFilesEx::AllowData(FormatEtc);
  6824. }
  6825. };
  6826. //---------------------------------------------------------------------------
  6827. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDCreateDataObject(
  6828. TObject * Sender, TDataObject *& DataObject)
  6829. {
  6830. if (FDDExtMapFile != NULL)
  6831. {
  6832. TFileList * FileList = DragDropFiles(Sender)->FileList;
  6833. if (!FileList->RenderPIDLs() || !FileList->RenderNames())
  6834. {
  6835. Abort();
  6836. }
  6837. if (FileList->Count > 0)
  6838. {
  6839. TDataObjectFilesEx * FilesObject = new TFakeDataObjectFilesEx(FileList, true, true);
  6840. if (!FilesObject->IsValid(true, true))
  6841. {
  6842. FilesObject->_Release();
  6843. }
  6844. else
  6845. {
  6846. DataObject = FilesObject;
  6847. }
  6848. }
  6849. }
  6850. }
  6851. //---------------------------------------------------------------------------
  6852. void __fastcall TCustomScpExplorerForm::GoToCommandLine()
  6853. {
  6854. DebugFail();
  6855. }
  6856. //---------------------------------------------------------------------------
  6857. void __fastcall TCustomScpExplorerForm::GoToTree()
  6858. {
  6859. ComponentVisible[fcRemoteTree] = true;
  6860. RemoteDriveView->SetFocus();
  6861. }
  6862. //---------------------------------------------------------------------------
  6863. TStrings * __fastcall TCustomScpExplorerForm::PanelExport(TOperationSide Side,
  6864. TPanelExport Export)
  6865. {
  6866. TCustomDirView * DirView = this->DirView(Side);
  6867. std::unique_ptr<TStrings> ExportData(new TStringList());
  6868. switch (Export)
  6869. {
  6870. case pePath:
  6871. ExportData->Add(DirView->PathName);
  6872. break;
  6873. case peFileList:
  6874. case peFullFileList:
  6875. {
  6876. bool FullPath = (Export == peFullFileList);
  6877. DirView->CreateFileList(false, FullPath, ExportData.get());
  6878. for (int Index = 0; Index < ExportData->Count; Index++)
  6879. {
  6880. if (ExportData->Strings[Index].Pos(L" ") > 0)
  6881. {
  6882. ExportData->Strings[Index] = FORMAT(L"\"%s\"", (ExportData->Strings[Index]));
  6883. }
  6884. }
  6885. }
  6886. break;
  6887. default:
  6888. DebugFail();
  6889. }
  6890. return ExportData.release();
  6891. }
  6892. //---------------------------------------------------------------------------
  6893. void __fastcall TCustomScpExplorerForm::PanelExport(TOperationSide Side,
  6894. TPanelExport Export, TPanelExportDestination Destination)
  6895. {
  6896. std::unique_ptr<TStrings> ExportData(PanelExport(Side, Export));
  6897. PanelExportStore(Side, Export, Destination, ExportData.get());
  6898. }
  6899. //---------------------------------------------------------------------------
  6900. void __fastcall TCustomScpExplorerForm::PanelExportStore(TOperationSide /*Side*/,
  6901. TPanelExport /*Export*/, TPanelExportDestination Destination,
  6902. TStrings * ExportData)
  6903. {
  6904. if (Destination == pedClipboard)
  6905. {
  6906. TInstantOperationVisualizer Visualizer;
  6907. CopyToClipboard(ExportData);
  6908. }
  6909. else
  6910. {
  6911. DebugFail();
  6912. }
  6913. }
  6914. //---------------------------------------------------------------------------
  6915. void __fastcall TCustomScpExplorerForm::Filter(TOperationSide Side)
  6916. {
  6917. TCustomDirView * DirView = this->DirView(Side);
  6918. TFileFilter Filter;
  6919. DefaultFileFilter(Filter);
  6920. Filter.Masks = DirView->Mask;
  6921. if (DoFilterMaskDialog(DirView, &Filter))
  6922. {
  6923. DirView->Mask = TFileMasks::NormalizeMask(Filter.Masks);
  6924. Configuration->Usage->Inc(L"Filters");
  6925. }
  6926. }
  6927. //---------------------------------------------------------------------------
  6928. TQueueOperation __fastcall TCustomScpExplorerForm::DefaultQueueOperation()
  6929. {
  6930. return FQueueController->DefaultOperation();
  6931. }
  6932. //---------------------------------------------------------------------------
  6933. bool __fastcall TCustomScpExplorerForm::AllowQueueOperation(
  6934. TQueueOperation Operation, void ** Param)
  6935. {
  6936. switch (Operation)
  6937. {
  6938. case qoPreferences:
  6939. return true;
  6940. case qoGoTo:
  6941. return ComponentVisible[fcQueueView] && QueueView3->Enabled;
  6942. case qoOnceEmpty:
  6943. return !FQueueController->Empty;
  6944. default:
  6945. return FQueueController->AllowOperation(Operation, Param);
  6946. }
  6947. }
  6948. //---------------------------------------------------------------------------
  6949. void __fastcall TCustomScpExplorerForm::GoToQueue()
  6950. {
  6951. if (DebugAlwaysTrue(QueueView3->Visible))
  6952. {
  6953. QueueView3->SetFocus();
  6954. }
  6955. }
  6956. //---------------------------------------------------------------------------
  6957. void __fastcall TCustomScpExplorerForm::ExecuteQueueOperation(
  6958. TQueueOperation Operation, void * Param)
  6959. {
  6960. if (Operation == qoGoTo)
  6961. {
  6962. GoToQueue();
  6963. }
  6964. else if (Operation == qoPreferences)
  6965. {
  6966. PreferencesDialog(pmQueue);
  6967. }
  6968. else
  6969. {
  6970. FQueueController->ExecuteOperation(Operation, Param);
  6971. }
  6972. }
  6973. //---------------------------------------------------------------------------
  6974. bool __fastcall TCustomScpExplorerForm::GetQueueEnabled()
  6975. {
  6976. return (Queue != NULL) && Queue->Enabled;
  6977. }
  6978. //---------------------------------------------------------------------------
  6979. void __fastcall TCustomScpExplorerForm::ToggleQueueEnabled()
  6980. {
  6981. DebugAssert(Queue != NULL);
  6982. if (Queue != NULL)
  6983. {
  6984. Queue->Enabled = !Queue->Enabled;
  6985. }
  6986. }
  6987. //---------------------------------------------------------------------------
  6988. void __fastcall TCustomScpExplorerForm::QueueView3ContextPopup(
  6989. TObject * /*Sender*/, TPoint & /*MousePos*/, bool & /*Handled*/)
  6990. {
  6991. FQueueActedItem = QueueView3->ItemFocused;
  6992. }
  6993. //---------------------------------------------------------------------------
  6994. /*virtual*/ int __fastcall TCustomScpExplorerForm::GetStaticComponentsHeight()
  6995. {
  6996. return TopDock->Height + QueueSplitter->Height;
  6997. }
  6998. //---------------------------------------------------------------------------
  6999. void __fastcall TCustomScpExplorerForm::QueueSplitterCanResize(
  7000. TObject * /*Sender*/, int & NewSize, bool & Accept)
  7001. {
  7002. // when queue is hidden by double-clicking splitter, stray attempt to
  7003. // resize the panel with strange value arrives, make sure it is ignored
  7004. if (ComponentVisible[fcQueueView])
  7005. {
  7006. int HeightLimit = ClientHeight - GetStaticComponentsHeight() -
  7007. RemotePanel->Constraints->MinHeight;
  7008. if (NewSize > HeightLimit)
  7009. {
  7010. NewSize = HeightLimit;
  7011. }
  7012. }
  7013. else
  7014. {
  7015. Accept = false;
  7016. }
  7017. }
  7018. //---------------------------------------------------------------------------
  7019. void __fastcall TCustomScpExplorerForm::QueueView3StartDrag(TObject * /*Sender*/,
  7020. TDragObject *& /*DragObject*/)
  7021. {
  7022. FQueueActedItem = QueueView3->ItemFocused;
  7023. }
  7024. //---------------------------------------------------------------------------
  7025. void __fastcall TCustomScpExplorerForm::QueueView3DragOver(TObject * /*Sender*/,
  7026. TObject * Source, int X, int Y, TDragState /*State*/, bool & Accept)
  7027. {
  7028. Accept = true;
  7029. if (Source == QueueView3)
  7030. {
  7031. TListItem * DropTarget = QueueView3->GetItemAt(X, Y);
  7032. Accept = (DropTarget != NULL) && (FQueueActedItem != NULL);
  7033. if (Accept)
  7034. {
  7035. TQueueItemProxy * QueueItem;
  7036. TQueueItemProxy * DestQueueItem;
  7037. QueueItem = static_cast<TQueueItemProxy *>(FQueueActedItem->Data);
  7038. DestQueueItem = static_cast<TQueueItemProxy *>(DropTarget->Data);
  7039. Accept = (QueueItem != DestQueueItem) &&
  7040. (QueueItem->Status == TQueueItem::qsPending) &&
  7041. (DestQueueItem->Status == TQueueItem::qsPending);
  7042. }
  7043. }
  7044. }
  7045. //---------------------------------------------------------------------------
  7046. void __fastcall TCustomScpExplorerForm::QueueView3DragDrop(TObject * /*Sender*/,
  7047. TObject * /*Source*/, int /*X*/, int /*Y*/)
  7048. {
  7049. if ((FQueueActedItem != NULL) && (QueueView3->DropTarget != NULL))
  7050. {
  7051. TQueueItemProxy * QueueItem;
  7052. TQueueItemProxy * DestQueueItem;
  7053. QueueItem = static_cast<TQueueItemProxy *>(FQueueActedItem->Data);
  7054. DestQueueItem = static_cast<TQueueItemProxy *>(QueueView3->DropTarget->Data);
  7055. QueueItem->Move(DestQueueItem);
  7056. }
  7057. }
  7058. //---------------------------------------------------------------------------
  7059. void __fastcall TCustomScpExplorerForm::QueueView3Enter(TObject * /*Sender*/)
  7060. {
  7061. if ((QueueView3->ItemFocused == NULL) &&
  7062. (QueueView3->Items->Count > 0))
  7063. {
  7064. QueueView3->ItemFocused = QueueView3->Items->Item[0];
  7065. }
  7066. QueueLabelUpdateStatus();
  7067. }
  7068. //---------------------------------------------------------------------------
  7069. void __fastcall TCustomScpExplorerForm::QueueView3Exit(TObject * /*Sender*/)
  7070. {
  7071. QueueLabelUpdateStatus();
  7072. }
  7073. //---------------------------------------------------------------------------
  7074. void __fastcall TCustomScpExplorerForm::QueueLabelUpdateStatus()
  7075. {
  7076. QueueLabel->UpdateStatus();
  7077. }
  7078. //---------------------------------------------------------------------------
  7079. void __fastcall TCustomScpExplorerForm::QueueView3SelectItem(
  7080. TObject * /*Sender*/, TListItem * /*Item*/, bool Selected)
  7081. {
  7082. if (Selected)
  7083. {
  7084. NonVisualDataModule->UpdateNonVisibleActions();
  7085. }
  7086. }
  7087. //---------------------------------------------------------------------------
  7088. TDragDropFilesEx * __fastcall TCustomScpExplorerForm::DragDropFiles(TObject * Sender)
  7089. {
  7090. TDragDropFilesEx * Result = NULL;
  7091. if (Sender == SessionsPageControl)
  7092. {
  7093. Result = FSessionsDragDropFilesEx;
  7094. }
  7095. else if (Sender == QueueView3)
  7096. {
  7097. Result = FQueueDragDropFilesEx;
  7098. }
  7099. else
  7100. {
  7101. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  7102. if (DirView != NULL)
  7103. {
  7104. Result = DirView->DragDropFilesEx;
  7105. }
  7106. else
  7107. {
  7108. TCustomDriveView * DriveView = dynamic_cast<TCustomDriveView *>(Sender);
  7109. if (DriveView != NULL)
  7110. {
  7111. Result = DriveView->DragDropFilesEx;
  7112. }
  7113. }
  7114. }
  7115. DebugAssert(Result != NULL);
  7116. return Result;
  7117. }
  7118. //---------------------------------------------------------------------------
  7119. bool __fastcall TCustomScpExplorerForm::SelectedAllFilesInDirView(TCustomDirView * DView)
  7120. {
  7121. return (DView->SelCount == DView->FilesCount);
  7122. }
  7123. //---------------------------------------------------------------------------
  7124. bool __fastcall TCustomScpExplorerForm::DraggingAllFilesFromDirView(TOperationSide Side, TStrings * FileList)
  7125. {
  7126. return HasDirView[Side] && (DropSourceControl == DirView(Side)) && (FileList->Count == DirView(Side)->FilesCount);
  7127. }
  7128. //---------------------------------------------------------------------------
  7129. void __fastcall TCustomScpExplorerForm::RemoteFileControlDragDropFileOperation(
  7130. TObject * Sender, int Effect, UnicodeString TargetPath, bool ForceQueue)
  7131. {
  7132. TFileOperation Operation;
  7133. switch (Effect)
  7134. {
  7135. case DROPEFFECT_MOVE:
  7136. Operation = foMove;
  7137. break;
  7138. case DROPEFFECT_COPY:
  7139. // occures on WinXP (reported by user)
  7140. default:
  7141. Operation = foCopy;
  7142. break;
  7143. };
  7144. TDragDropFilesEx * DragDropFilesEx = DragDropFiles(Sender);
  7145. // see a comment in TUnixDirView::PerformItemDragDropOperation
  7146. if (DragDropFilesEx->FileList->Count > 0)
  7147. {
  7148. TStrings * FileList = new TStringList();
  7149. try
  7150. {
  7151. for (int Index = 0; Index < DragDropFilesEx->FileList->Count; Index++)
  7152. {
  7153. FileList->Add(DragDropFilesEx->FileList->Items[Index]->Name);
  7154. }
  7155. FDragDropOperation = true;
  7156. TTransferOperationParam Param;
  7157. Param.TargetDirectory = TargetPath;
  7158. // upload, no temp dirs
  7159. Param.Temp = false;
  7160. Param.DragDrop = true;
  7161. Param.Options =
  7162. FLAGMASK(DraggingAllFilesFromDirView(osLocal, FileList), coAllFiles);
  7163. if (ForceQueue)
  7164. {
  7165. Param.Queue = asOn;
  7166. }
  7167. if (ExecuteFileOperation(Operation, osLocal, FileList,
  7168. (WinConfiguration->DDTransferConfirmation == asOff), &Param))
  7169. {
  7170. if (IsFileControl(DropSourceControl, osLocal))
  7171. {
  7172. Configuration->Usage->Inc(L"UploadsDragDropInternal");
  7173. }
  7174. else
  7175. {
  7176. Configuration->Usage->Inc(L"UploadsDragDropExternal");
  7177. }
  7178. }
  7179. }
  7180. __finally
  7181. {
  7182. FDragDropOperation = false;
  7183. delete FileList;
  7184. }
  7185. }
  7186. }
  7187. //---------------------------------------------------------------------------
  7188. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDFileOperation(
  7189. TObject * Sender, int Effect, UnicodeString /*SourcePath*/,
  7190. UnicodeString TargetPath, bool & /*DoOperation*/)
  7191. {
  7192. RemoteFileControlDragDropFileOperation(Sender, Effect, TargetPath, false);
  7193. }
  7194. //---------------------------------------------------------------------------
  7195. void __fastcall TCustomScpExplorerForm::RemoteFileContolDDChooseEffect(
  7196. TObject * Sender, int grfKeyState, int & dwEffect)
  7197. {
  7198. // if any drop effect is allowed at all (e.g. no drop to self and drop to parent)
  7199. if ((dwEffect != DROPEFFECT_None) &&
  7200. IsFileControl(DropSourceControl, osRemote))
  7201. {
  7202. // do not allow drop on remote panel (on free space, still allow drop on directories)
  7203. if ((Sender == RemoteDirView) && (DropSourceControl == RemoteDirView) &&
  7204. (RemoteDirView->DropTarget == NULL))
  7205. {
  7206. dwEffect = DROPEFFECT_None;
  7207. }
  7208. else
  7209. {
  7210. if (dwEffect == DROPEFFECT_Copy)
  7211. {
  7212. bool MoveCapable = FTerminal->IsCapable[fcRemoteMove];
  7213. // currently we support copying always (at least via temporary directory);
  7214. // remove associated checks once this all proves stable and working
  7215. bool CopyCapable = true;
  7216. // if we do not support neither of operations, there's no discussion
  7217. if (!MoveCapable && !CopyCapable)
  7218. {
  7219. dwEffect = DROPEFFECT_None;
  7220. }
  7221. // when moving is disabled, we need to keep effect to "copy",
  7222. // which will be later interpretted as move (with slipped-in cursor)
  7223. else if (!WinConfiguration->DDAllowMoveInit && FLAGCLEAR(grfKeyState, MK_CONTROL))
  7224. {
  7225. // no-op, keep copy
  7226. }
  7227. else
  7228. {
  7229. // The default effect inside remote panel is move,
  7230. // unless we do not support it, but support copy
  7231. if (FLAGCLEAR(grfKeyState, MK_CONTROL))
  7232. {
  7233. dwEffect = MoveCapable ? DROPEFFECT_Move : DROPEFFECT_Copy;
  7234. }
  7235. else
  7236. {
  7237. // with ctrl-down, we want copy unless it is not supported
  7238. dwEffect = CopyCapable ? DROPEFFECT_Copy : DROPEFFECT_None;
  7239. }
  7240. }
  7241. }
  7242. }
  7243. }
  7244. }
  7245. //---------------------------------------------------------------------------
  7246. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDDragFileName(
  7247. TObject * Sender, TRemoteFile * File, UnicodeString & FileName)
  7248. {
  7249. if (FDDTotalSize >= 0)
  7250. {
  7251. if (File->IsDirectory)
  7252. {
  7253. FDDTotalSize = -1;
  7254. }
  7255. else
  7256. {
  7257. FDDTotalSize += File->Size;
  7258. }
  7259. }
  7260. DebugAssert(!FDragTempDir.IsEmpty());
  7261. // TODO: this is quite ineffective
  7262. // TODO: what if invalid character replacement is disabled?
  7263. FileName = FDragTempDir + GUIConfiguration->CurrentCopyParam.ValidLocalFileName(File->FileName);
  7264. UnicodeString TransferFileName;
  7265. if (Sender == RemoteDriveView)
  7266. {
  7267. TransferFileName = UnixExcludeTrailingBackslash(File->FullFileName);
  7268. }
  7269. else
  7270. {
  7271. TransferFileName = File->FileName;
  7272. }
  7273. FDDFileList->AddObject(TransferFileName, File);
  7274. }
  7275. //---------------------------------------------------------------------------
  7276. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDDragDetect(
  7277. TObject * /*Sender*/, int /*grfKeyState*/, const TPoint & /*DetectStart*/,
  7278. const TPoint & /*Point*/, TDragDetectStatus /*DragStatus*/)
  7279. {
  7280. // sometimes we do not get DDEnd so the list is not released
  7281. SAFE_DESTROY(FDDFileList);
  7282. FDDFileList = new TStringList();
  7283. FDragTempDir = WinConfiguration->TemporaryDir();
  7284. FDDTotalSize = 0;
  7285. FDragDropSshTerminate.reset(NULL);
  7286. }
  7287. //---------------------------------------------------------------------------
  7288. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDQueryContinueDrag(
  7289. TObject * /*Sender*/, BOOL /*FEscapePressed*/, int /*grfKeyState*/,
  7290. HRESULT & Result)
  7291. {
  7292. if (Result == DRAGDROP_S_DROP)
  7293. {
  7294. try
  7295. {
  7296. GlobalDragImageList->HideDragImage();
  7297. try
  7298. {
  7299. RemoteFileControlDDTargetDrop();
  7300. }
  7301. catch(ESshTerminate & E)
  7302. {
  7303. FDragDropSshTerminate.reset(E.Clone());
  7304. }
  7305. }
  7306. catch (Exception &E)
  7307. {
  7308. // If downloading fails we need to cancel drag&drop, otherwise
  7309. // Explorer shows error
  7310. // But by the way exception probably never reach this point as
  7311. // it's catched on way.
  7312. // Fatal exceptions get here (like when opening a secondary shell extension for file duplication fails).
  7313. Result = DRAGDROP_S_CANCEL;
  7314. ShowExtendedException(Terminal, &E);
  7315. }
  7316. }
  7317. }
  7318. //---------------------------------------------------------------------------
  7319. void __fastcall TCustomScpExplorerForm::DirViewMatchMask(
  7320. TObject * /*Sender*/, UnicodeString FileName, bool Directory, __int64 Size,
  7321. TDateTime Modification, UnicodeString Masks, bool & Matches, bool AllowImplicitMatches)
  7322. {
  7323. TFileMasks::TParams MaskParams;
  7324. MaskParams.Size = Size;
  7325. MaskParams.Modification = Modification;
  7326. // this does not re-parse the mask if it is the same as the last time
  7327. FDirViewMatchMask = Masks;
  7328. bool ImplicitMatch;
  7329. Matches =
  7330. // RecurseInclude parameter has no effect as the Path is empty
  7331. FDirViewMatchMask.Matches(FileName, Directory, UnicodeString(L""), &MaskParams, true, ImplicitMatch) &&
  7332. (AllowImplicitMatches || !ImplicitMatch);
  7333. }
  7334. //---------------------------------------------------------------------------
  7335. void __fastcall TCustomScpExplorerForm::DirViewGetOverlay(
  7336. TObject * Sender, TListItem * Item, WORD & Indexes)
  7337. {
  7338. TCustomDirView * DirView = reinterpret_cast<TCustomDirView *>(Sender);
  7339. UnicodeString Ext;
  7340. if (DirView == RemoteDirView)
  7341. {
  7342. Ext = UnixExtractFileExt(DirView->ItemFileName(Item));
  7343. }
  7344. else
  7345. {
  7346. Ext = ExtractFileExt(DirView->ItemFileName(Item));
  7347. }
  7348. if (SameText(Ext, Configuration->PartialExt))
  7349. {
  7350. Indexes |= oiPartial;
  7351. }
  7352. }
  7353. //---------------------------------------------------------------------------
  7354. bool __fastcall TCustomScpExplorerForm::CanPasteToDirViewFromClipBoard()
  7355. {
  7356. return
  7357. DirViewEnabled(osCurrent) &&
  7358. DirView(osCurrent)->CanPasteFromClipBoard();
  7359. }
  7360. //---------------------------------------------------------------------------
  7361. bool __fastcall TCustomScpExplorerForm::CanPasteFromClipBoard()
  7362. {
  7363. bool Result = false;
  7364. if (CanPasteToDirViewFromClipBoard())
  7365. {
  7366. Result = true;
  7367. }
  7368. else
  7369. {
  7370. UnicodeString ClipboardText;
  7371. if (NonEmptyTextFromClipboard(ClipboardText))
  7372. {
  7373. if (StoredSessions->IsUrl(ClipboardText))
  7374. {
  7375. Result = true;
  7376. }
  7377. else
  7378. {
  7379. Result = DirViewEnabled(osCurrent);
  7380. }
  7381. }
  7382. }
  7383. return Result;
  7384. }
  7385. //---------------------------------------------------------------------------
  7386. void __fastcall TCustomScpExplorerForm::PasteFromClipBoard()
  7387. {
  7388. if (CanPasteToDirViewFromClipBoard())
  7389. {
  7390. DirView(osCurrent)->PasteFromClipBoard();
  7391. }
  7392. else
  7393. {
  7394. UnicodeString ClipboardText;
  7395. if (NonEmptyTextFromClipboard(ClipboardText))
  7396. {
  7397. if (StoredSessions->IsUrl(ClipboardText))
  7398. {
  7399. NewSession(false, ClipboardText);
  7400. }
  7401. else
  7402. {
  7403. DirView(osCurrent)->Path = ClipboardText;
  7404. }
  7405. }
  7406. }
  7407. }
  7408. //---------------------------------------------------------------------------
  7409. void __fastcall TCustomScpExplorerForm::FileListFromClipboard()
  7410. {
  7411. // TBD
  7412. }
  7413. //---------------------------------------------------------------------------
  7414. void __fastcall TCustomScpExplorerForm::SelectAll(TOperationSide Side, TSelectMode Mode)
  7415. {
  7416. TCustomDirView * ADirView = DirView(Side);
  7417. ADirView->SelectAll(Mode);
  7418. ADirView->SetFocus();
  7419. }
  7420. //---------------------------------------------------------------------------
  7421. void __fastcall TCustomScpExplorerForm::SelectByMask(TOperationSide Side, bool Select)
  7422. {
  7423. TCustomDirView * ADirView = DirView(Side);
  7424. TFileFilter Filter;
  7425. DefaultFileFilter(Filter);
  7426. if (DoSelectMaskDialog(ADirView, Select, &Filter, Configuration))
  7427. {
  7428. Configuration->Usage->Inc(L"MaskSelections");
  7429. ADirView->SelectFiles(Filter, Select);
  7430. ADirView->SetFocus();
  7431. }
  7432. }
  7433. //---------------------------------------------------------------------------
  7434. void __fastcall TCustomScpExplorerForm::RestoreSelectedNames(TOperationSide Side)
  7435. {
  7436. TCustomDirView * ADirView = DirView(Side);
  7437. ADirView->RestoreSelectedNames();
  7438. ADirView->SetFocus();
  7439. }
  7440. //---------------------------------------------------------------------------
  7441. void __fastcall TCustomScpExplorerForm::SelectSameExt(bool Select)
  7442. {
  7443. TCustomDirView * CurrentDirView = DirView(osCurrent);
  7444. if (DebugAlwaysTrue(CurrentDirView->ItemFocused != NULL))
  7445. {
  7446. UnicodeString FileName = CurrentDirView->ItemFileName(CurrentDirView->ItemFocused);
  7447. UnicodeString Ext;
  7448. if (GetSide(osCurrent) == osRemote)
  7449. {
  7450. Ext = UnixExtractFileExt(FileName);
  7451. }
  7452. else
  7453. {
  7454. Ext = ExtractFileExt(FileName);
  7455. }
  7456. if (Ext.IsEmpty())
  7457. {
  7458. Ext = L".";
  7459. }
  7460. TFileFilter Filter;
  7461. Filter.Masks = FORMAT(L"*%s", (Ext));
  7462. Filter.Directories = false;
  7463. CurrentDirView->SelectFiles(Filter, Select);
  7464. }
  7465. }
  7466. //---------------------------------------------------------------------------
  7467. UnicodeString __fastcall TCustomScpExplorerForm::FileStatusBarText(
  7468. const TStatusFileInfo & FileInfo, TOperationSide Side)
  7469. {
  7470. UnicodeString Result;
  7471. if ((Side == osRemote) && (Terminal == NULL))
  7472. {
  7473. // noop
  7474. }
  7475. else
  7476. {
  7477. Result =
  7478. FMTLOAD(FILE_INFO_FORMAT,
  7479. (FormatBytes(FileInfo.SelectedSize),
  7480. FormatBytes(FileInfo.FilesSize),
  7481. FormatNumber(FileInfo.SelectedCount),
  7482. FormatNumber(FileInfo.FilesCount)));
  7483. }
  7484. return Result;
  7485. }
  7486. //---------------------------------------------------------------------------
  7487. void __fastcall TCustomScpExplorerForm::FileStatusBarPanelClick(
  7488. TTBXStatusPanel * Panel, TOperationSide Side)
  7489. {
  7490. if (Panel->Index == 1)
  7491. {
  7492. ToggleShowHiddenFiles();
  7493. }
  7494. else if (Panel->Index == 2)
  7495. {
  7496. Filter(Side);
  7497. }
  7498. }
  7499. //---------------------------------------------------------------------------
  7500. void __fastcall TCustomScpExplorerForm::UpdateFileStatusBar(
  7501. TTBXStatusBar * StatusBar, const TStatusFileInfo & FileInfo, TOperationSide Side)
  7502. {
  7503. DebugAssert(!StatusBar->SimplePanel);
  7504. StatusBar->Panels->Items[0]->Caption = FileStatusBarText(FileInfo, Side);
  7505. UpdateFileStatusExtendedPanels(StatusBar, FileInfo);
  7506. }
  7507. //---------------------------------------------------------------------------
  7508. void __fastcall TCustomScpExplorerForm::UpdateFileStatusExtendedPanels(
  7509. TTBXStatusBar * StatusBar, const TStatusFileInfo & FileInfo)
  7510. {
  7511. DebugAssert(StatusBar->Panels->Count >= 3);
  7512. TTBXStatusPanel * HiddenFilesPanel = StatusBar->Panels->Items[1];
  7513. if (FileInfo.HiddenCount > 0)
  7514. {
  7515. HiddenFilesPanel->Caption = FMTLOAD(FILE_INFO_HIDDEN2, (FormatNumber(FileInfo.HiddenCount)));
  7516. HiddenFilesPanel->ViewPriority = 90; // <100 allows hiding panel when it does not fit
  7517. }
  7518. else
  7519. {
  7520. HiddenFilesPanel->ViewPriority = 0;
  7521. // not really necessary, just to cleanup no-longer-valid data
  7522. HiddenFilesPanel->Caption = L"";
  7523. }
  7524. TTBXStatusPanel * FilteredFilesPanel = StatusBar->Panels->Items[2];
  7525. if (FileInfo.FilteredCount > 0)
  7526. {
  7527. FilteredFilesPanel->Caption = FMTLOAD(FILE_INFO_FILTERED2, (FormatNumber(FileInfo.FilteredCount)));
  7528. FilteredFilesPanel->ViewPriority = 90; // <100 allows hiding panel when it does not fit
  7529. }
  7530. else
  7531. {
  7532. FilteredFilesPanel->ViewPriority = 0;
  7533. // not really necessary, just to cleanup no-longer-valid data
  7534. FilteredFilesPanel->Caption = L"";
  7535. }
  7536. }
  7537. //---------------------------------------------------------------------------
  7538. void __fastcall TCustomScpExplorerForm::RemoteStatusBarClick(
  7539. TObject * /*Sender*/)
  7540. {
  7541. if (RemoteDirView->Enabled)
  7542. {
  7543. RemoteDirView->SetFocus();
  7544. }
  7545. }
  7546. //---------------------------------------------------------------------------
  7547. void __fastcall TCustomScpExplorerForm::ToggleQueueVisibility()
  7548. {
  7549. TQueueViewConfiguration Config = WinConfiguration->QueueView;
  7550. switch (Config.Show)
  7551. {
  7552. case qvShow:
  7553. if ((FQueueStatus != NULL) && (FQueueStatus->Count > 0))
  7554. {
  7555. Config.Show = qvHide;
  7556. }
  7557. else
  7558. {
  7559. Config.Show = Config.LastHideShow;
  7560. }
  7561. break;
  7562. case qvHideWhenEmpty:
  7563. if (ComponentVisible[fcQueueView])
  7564. {
  7565. Config.Show = qvHide;
  7566. }
  7567. else
  7568. {
  7569. Config.LastHideShow = Config.Show;
  7570. Config.Show = qvShow;
  7571. }
  7572. break;
  7573. case qvHide:
  7574. Config.LastHideShow = Config.Show;
  7575. Config.Show = qvShow;
  7576. break;
  7577. }
  7578. WinConfiguration->QueueView = Config;
  7579. }
  7580. //---------------------------------------------------------------------------
  7581. UnicodeString __fastcall TCustomScpExplorerForm::PathForCaption()
  7582. {
  7583. UnicodeString Result;
  7584. if (FTerminal != NULL)
  7585. {
  7586. switch (WinConfiguration->PathInCaption)
  7587. {
  7588. case picShort:
  7589. Result = TTerminalManager::Instance()->GetTerminalShortPath(FTerminal);
  7590. break;
  7591. case picFull:
  7592. Result = FTerminal->CurrentDirectory;
  7593. break;
  7594. }
  7595. }
  7596. return Result;
  7597. }
  7598. //---------------------------------------------------------------------------
  7599. void __fastcall TCustomScpExplorerForm::UpdateControls()
  7600. {
  7601. TTerminalManager::Instance()->UpdateAppTitle();
  7602. // WORAKRDOUND: Disabling list view when it is not showing yet does not set its
  7603. // background to gray on Windows 7 (works on Windows 10).
  7604. // See also EnableControl
  7605. if (Showing)
  7606. {
  7607. bool HasTerminal = (Terminal != NULL);
  7608. if (HasTerminal)
  7609. {
  7610. // Update path when it changes
  7611. if ((SessionsPageControl->ActivePage != NULL) && (GetSessionTabTerminal(SessionsPageControl->ActivePage) == Terminal))
  7612. {
  7613. UpdateSessionTab(SessionsPageControl->ActivePage);
  7614. }
  7615. if (!RemoteDirView->Enabled)
  7616. {
  7617. RemoteDirView->Enabled = true;
  7618. if (FRemoteDirViewWasFocused)
  7619. {
  7620. ActiveControl = RemoteDirView;
  7621. }
  7622. }
  7623. RemoteDriveView->Enabled = true;
  7624. RemoteDirView->Color = (FSessionColor != 0 ? FSessionColor : clWindow);
  7625. RemoteDriveView->Color = RemoteDirView->Color;
  7626. }
  7627. else
  7628. {
  7629. if (RemoteDirView->Enabled)
  7630. {
  7631. // This is first called when the form is being constructed
  7632. // (not anymore due to Showing test above)
  7633. // but the false is overriden in the constructor later.
  7634. // An even later in TScpCommanderForm::DoShow()
  7635. FRemoteDirViewWasFocused = (ActiveControl == RemoteDirView);
  7636. EnableControl(RemoteDirView, false);
  7637. }
  7638. EnableControl(RemoteDriveView, false);
  7639. }
  7640. EnableControl(QueueView3, HasTerminal && Terminal->IsCapable[fsBackgroundTransfers]);
  7641. QueueLabelUpdateStatus();
  7642. reinterpret_cast<TTBCustomItem *>(GetComponent(fcRemotePathComboBox))->Enabled = HasTerminal;
  7643. }
  7644. }
  7645. //---------------------------------------------------------------------------
  7646. void __fastcall TCustomScpExplorerForm::DoDirViewLoaded(TCustomDirView * /*Sender*/)
  7647. {
  7648. UpdateControls();
  7649. }
  7650. //---------------------------------------------------------------------------
  7651. void __fastcall TCustomScpExplorerForm::DirViewLoaded(
  7652. TObject * Sender)
  7653. {
  7654. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  7655. DebugAssert(DirView != NULL);
  7656. DoDirViewLoaded(DirView);
  7657. TransferPresetAutoSelect();
  7658. }
  7659. //---------------------------------------------------------------------------
  7660. void __fastcall TCustomScpExplorerForm::StartUpdates()
  7661. {
  7662. TUpdatesConfiguration Updates = WinConfiguration->Updates;
  7663. // first run after installation
  7664. if (double(Updates.LastCheck) == 0)
  7665. {
  7666. // make sure next time there will be an update (if enabled)
  7667. Updates.LastCheck = TDateTime(1);
  7668. WinConfiguration->Updates = Updates;
  7669. }
  7670. else if ((double(Updates.Period) > 0) &&
  7671. (Now() - Updates.LastCheck >= Updates.Period))
  7672. {
  7673. TThreadMethod OnUpdatesChecked = NULL;
  7674. if (!IsUWP())
  7675. {
  7676. OnUpdatesChecked = UpdatesChecked;
  7677. }
  7678. StartUpdateThread(OnUpdatesChecked);
  7679. }
  7680. }
  7681. //---------------------------------------------------------------------------
  7682. void __fastcall TCustomScpExplorerForm::UpdatesChecked()
  7683. {
  7684. UnicodeString Message;
  7685. bool New;
  7686. TQueryType Type;
  7687. GetUpdatesMessage(Message, New, Type, false);
  7688. if (!Message.IsEmpty())
  7689. {
  7690. if (New)
  7691. {
  7692. Message = FMTLOAD(NEW_VERSION_CLICK, (Message));
  7693. }
  7694. if (!New && (Type != qtWarning))
  7695. {
  7696. PostNote(UnformatMessage(Message), 0, UpdatesNoteClicked, NULL);
  7697. }
  7698. else
  7699. {
  7700. Notify(NULL, Message, Type, true, UpdatesNoteClicked);
  7701. }
  7702. if (New)
  7703. {
  7704. Configuration->Usage->Inc(L"UpdateNotifications");
  7705. }
  7706. }
  7707. }
  7708. //---------------------------------------------------------------------------
  7709. void __fastcall TCustomScpExplorerForm::UpdatesNoteClicked(TObject * /*Sender*/)
  7710. {
  7711. RestoreApp();
  7712. bool CanDisplay = !NonVisualDataModule->Busy;
  7713. if (!CanDisplay && (Screen->ActiveForm != NULL))
  7714. {
  7715. CanDisplay = (Screen->ActiveForm->Perform(WM_CAN_DISPLAY_UPDATES, 0, 0) != 0);
  7716. }
  7717. if (CanDisplay)
  7718. {
  7719. Configuration->Usage->Inc(L"UpdateNotificationsClicked");
  7720. CheckForUpdates(true);
  7721. }
  7722. else
  7723. {
  7724. Configuration->Usage->Inc(L"UpdateNotificationsBusy");
  7725. }
  7726. }
  7727. //---------------------------------------------------------------------------
  7728. void __fastcall TCustomScpExplorerForm::GetTransferPresetAutoSelectData(
  7729. TCopyParamRuleData & Data)
  7730. {
  7731. DebugAssert(Terminal != NULL);
  7732. Data.HostName = Terminal->SessionData->HostNameExpanded;
  7733. Data.UserName = Terminal->SessionData->UserNameExpanded;
  7734. Data.RemoteDirectory = RemoteDirView->PathName;
  7735. }
  7736. //---------------------------------------------------------------------------
  7737. void __fastcall TCustomScpExplorerForm::TransferPresetAutoSelect()
  7738. {
  7739. // Terminal can be null when we are changing local directory implicitly,
  7740. // because it has been deleted, while no session is connected
  7741. // (Login dialog is open)
  7742. if (FAllowTransferPresetAutoSelect && (Terminal != NULL))
  7743. {
  7744. TCopyParamRuleData Data;
  7745. GetTransferPresetAutoSelectData(Data);
  7746. int CopyParamIndex = GUIConfiguration->CopyParamList->Find(Data);
  7747. UnicodeString CopyParamCurrent = GUIConfiguration->CopyParamCurrent;
  7748. if (CopyParamIndex < 0)
  7749. {
  7750. // there is no preset that matches autoselection
  7751. // set preset that we consider "default"
  7752. FCopyParamAutoSelected = L""; // forget last autoselected preset
  7753. GUIConfiguration->CopyParamCurrent = FCopyParamDefault;
  7754. }
  7755. else
  7756. {
  7757. // there is preset matching autoselection
  7758. UnicodeString CopyParamName = GUIConfiguration->CopyParamList->Names[CopyParamIndex];
  7759. if (CopyParamName == FCopyParamAutoSelected)
  7760. {
  7761. // autoselected the same preset as the last time
  7762. // make no change (i.e. preserve custom user preset, if any)
  7763. }
  7764. else
  7765. {
  7766. // autoselected the different preset then the last time (or there
  7767. // was default preset set)
  7768. FCopyParamAutoSelected = CopyParamName; // remember autoselection
  7769. GUIConfiguration->CopyParamCurrent = CopyParamName;
  7770. }
  7771. }
  7772. if (GUIConfiguration->CopyParamCurrent != CopyParamCurrent)
  7773. {
  7774. if (CopyParamIndex >= 0)
  7775. {
  7776. Configuration->Usage->Inc(L"CopyParamAutoSelects");
  7777. }
  7778. TTransferPresetNoteData * Data = new TTransferPresetNoteData;
  7779. try
  7780. {
  7781. int Fmt =
  7782. (CopyParamIndex < 0) ?
  7783. (GUIConfiguration->CopyParamIndex < 0 ? COPY_PARAM_DEFAULT_NORM : COPY_PARAM_DEFAULT_CUSTOM) :
  7784. COPY_PARAM_AUTOSELECTED;
  7785. UnicodeString Message = FMTLOAD(Fmt, (StripHotkey(GUIConfiguration->CopyParamCurrent)));
  7786. Data->Message = MainInstructions(Message);
  7787. int CopyParamAttrs = Terminal->UsableCopyParamAttrs(0).General;
  7788. UnicodeString Info = GUIConfiguration->CurrentCopyParam.GetInfoStr(L"\n",
  7789. CopyParamAttrs);
  7790. if (CopyParamIndex >= 0)
  7791. {
  7792. DebugAssert(GUIConfiguration->CopyParamList->Rules[CopyParamIndex] != NULL);
  7793. Info = FORMAT(L"%s\n \n%s", (Info,
  7794. FMTLOAD(COPY_PARAM_RULE,
  7795. (GUIConfiguration->CopyParamList->Rules[CopyParamIndex]->GetInfoStr(L"\n")))));
  7796. }
  7797. Data->Message += L"\n\n" + Info;
  7798. if (WinConfiguration->CopyParamAutoSelectNotice)
  7799. {
  7800. TransferPresetNoteMessage(Data, true);
  7801. }
  7802. else
  7803. {
  7804. PostNote(Message, 0, TransferPresetNoteClicked, Data);
  7805. Data = NULL; // ownership passed
  7806. }
  7807. }
  7808. __finally
  7809. {
  7810. delete Data;
  7811. }
  7812. }
  7813. }
  7814. }
  7815. //---------------------------------------------------------------------------
  7816. void __fastcall TCustomScpExplorerForm::TransferPresetNoteMessage(
  7817. TTransferPresetNoteData * NoteData, bool AllowNeverAskAgain)
  7818. {
  7819. DebugAssert(NoteData != NULL);
  7820. TMessageParams Params(AllowNeverAskAgain ? mpNeverAskAgainCheck : 0);
  7821. TQueryButtonAlias Aliases[1];
  7822. Aliases[0].Button = qaIgnore; // "ignore" is after "ok"
  7823. Aliases[0].Alias = LoadStr(CONFIGURE_BUTTON);
  7824. Params.Aliases = Aliases;
  7825. Params.AliasesCount = LENOF(Aliases);
  7826. unsigned int Result =
  7827. MoreMessageDialog(NoteData->Message, NULL, qtInformation,
  7828. qaOK | qaIgnore, HELP_COPY_PARAM_AUTOSELECTED, &Params);
  7829. switch (Result)
  7830. {
  7831. case qaNeverAskAgain:
  7832. DebugAssert(AllowNeverAskAgain);
  7833. WinConfiguration->CopyParamAutoSelectNotice = false;
  7834. break;
  7835. case qaIgnore:
  7836. PreferencesDialog(pmPresets);
  7837. break;
  7838. }
  7839. }
  7840. //---------------------------------------------------------------------------
  7841. void __fastcall TCustomScpExplorerForm::TransferPresetNoteClicked(TObject * Sender)
  7842. {
  7843. // as of now useless, as this is used for notes only, never for balloons, ...
  7844. RestoreApp();
  7845. // .. and we should never be busy here
  7846. if (DebugAlwaysTrue(!NonVisualDataModule->Busy))
  7847. {
  7848. TransferPresetNoteMessage(DebugNotNull(dynamic_cast<TTransferPresetNoteData *>(Sender)), false);
  7849. }
  7850. }
  7851. //---------------------------------------------------------------------------
  7852. void __fastcall TCustomScpExplorerForm::PreferencesDialog(
  7853. TPreferencesMode APreferencesMode)
  7854. {
  7855. std::unique_ptr<TPreferencesDialogData> PreferencesData;
  7856. TCopyParamRuleData Data;
  7857. if (Terminal != NULL)
  7858. {
  7859. PreferencesData.reset(new TPreferencesDialogData());
  7860. GetTransferPresetAutoSelectData(Data);
  7861. PreferencesData->CopyParamRuleData = &Data;
  7862. }
  7863. DoPreferencesDialog(APreferencesMode, PreferencesData.get());
  7864. }
  7865. //---------------------------------------------------------------------------
  7866. void __fastcall TCustomScpExplorerForm::AdHocCustomCommandValidate(
  7867. const TCustomCommandType & Command)
  7868. {
  7869. if (CustomCommandState(Command, FEditingFocusedAdHocCommand, ccltAll) <= 0)
  7870. {
  7871. throw Exception(FMTLOAD(CUSTOM_COMMAND_IMPOSSIBLE, (Command.Command)));
  7872. }
  7873. }
  7874. //---------------------------------------------------------------------------
  7875. void __fastcall TCustomScpExplorerForm::AdHocCustomCommand(bool OnFocused)
  7876. {
  7877. bool RemoteAllowed = CustomCommandRemoteAllowed();
  7878. TCustomCommandType Command;
  7879. // make sure we use local custom command when remote are not supported
  7880. if (RemoteAllowed || FLAGSET(FLastCustomCommand.Params, ccLocal))
  7881. {
  7882. Command = FLastCustomCommand;
  7883. }
  7884. else
  7885. {
  7886. Command.Params = Command.Params | ccLocal;
  7887. }
  7888. Command.Name = LoadStr(CUSTOM_COMMAND_AD_HOC_NAME);
  7889. FEditingFocusedAdHocCommand = OnFocused;
  7890. bool LocalSide = (FCurrentSide == osLocal);
  7891. int Options =
  7892. FLAGMASK((!RemoteAllowed || LocalSide), ccoDisableRemote) |
  7893. FLAGMASK(LocalSide, ccoDisableRemoteFiles);
  7894. if (DoCustomCommandDialog(Command, WinConfiguration->CustomCommandList,
  7895. ccmAdHoc, Options, AdHocCustomCommandValidate, NULL))
  7896. {
  7897. FLastCustomCommand = Command;
  7898. UpdateCustomCommandsToolbar();
  7899. ExecuteFileOperation(foCustomCommand, osRemote, OnFocused, false, &Command);
  7900. }
  7901. }
  7902. //---------------------------------------------------------------------------
  7903. void __fastcall TCustomScpExplorerForm::LastCustomCommand(bool OnFocused)
  7904. {
  7905. DebugAssert(!FLastCustomCommand.Command.IsEmpty());
  7906. int State = CustomCommandState(FLastCustomCommand, OnFocused, ccltAll);
  7907. DebugAssert(State > 0);
  7908. if (State <= 0)
  7909. {
  7910. throw Exception(FMTLOAD(CUSTOM_COMMAND_IMPOSSIBLE, (FLastCustomCommand.Command)));
  7911. }
  7912. ExecuteFileOperation(foCustomCommand, osRemote, OnFocused, false, &FLastCustomCommand);
  7913. }
  7914. //---------------------------------------------------------------------------
  7915. bool __fastcall TCustomScpExplorerForm::GetLastCustomCommand(bool OnFocused,
  7916. TCustomCommandType & Command, int & State)
  7917. {
  7918. bool Result = !FLastCustomCommand.Command.IsEmpty();
  7919. if (Result)
  7920. {
  7921. Command = FLastCustomCommand;
  7922. State = CustomCommandState(FLastCustomCommand, OnFocused, ccltAll);
  7923. }
  7924. return Result;
  7925. }
  7926. //---------------------------------------------------------------------------
  7927. void __fastcall TCustomScpExplorerForm::BeforeAction()
  7928. {
  7929. if (RemoteDirView->ItemFocused != NULL)
  7930. {
  7931. RemoteDirView->ItemFocused->CancelEdit();
  7932. }
  7933. }
  7934. //---------------------------------------------------------------------------
  7935. void __fastcall TCustomScpExplorerForm::PostComponentHide(Byte Component)
  7936. {
  7937. DebugAssert(ComponentVisible[Component]);
  7938. PostMessage(Handle, WM_COMPONENT_HIDE, Component, 0);
  7939. }
  7940. //---------------------------------------------------------------------------
  7941. void __fastcall TCustomScpExplorerForm::QueueSplitterDblClick(TObject * /*Sender*/)
  7942. {
  7943. // when queue panel is resized here directly, the status bar is stretched
  7944. // over whole space the panel occupied
  7945. PostComponentHide(fcQueueView);
  7946. }
  7947. //---------------------------------------------------------------------------
  7948. void __fastcall TCustomScpExplorerForm::Dispatch(void * Message)
  7949. {
  7950. TMessage * M = static_cast<TMessage*>(Message);
  7951. switch (M->Msg)
  7952. {
  7953. case CM_DIALOGCHAR:
  7954. CMDialogChar(*M);
  7955. break;
  7956. case WM_APPCOMMAND:
  7957. WMAppCommand(*M);
  7958. break;
  7959. case WM_SYSCOMMAND:
  7960. WMSysCommand(*M);
  7961. break;
  7962. case WM_QUERYENDSESSION:
  7963. WMQueryEndSession(*M);
  7964. break;
  7965. case WM_ENDSESSION:
  7966. WMEndSession(*reinterpret_cast<TWMEndSession *>(M));
  7967. break;
  7968. #ifdef _DEBUG
  7969. case WM_WINDOWPOSCHANGED:
  7970. WMWindowPosChanged(*reinterpret_cast<TWMWindowPosMsg *>(M));
  7971. break;
  7972. case CM_CUSTOMSTYLECHANGED:
  7973. TForm::Dispatch(Message);
  7974. break;
  7975. #endif
  7976. case WM_COMPONENT_HIDE:
  7977. {
  7978. Byte Component = static_cast<Byte>(M->WParam);
  7979. // sanity check
  7980. if (ComponentVisible[Component])
  7981. {
  7982. // special treatment
  7983. if (Component == fcQueueView)
  7984. {
  7985. ToggleQueueVisibility();
  7986. DebugAssert(!ComponentVisible[fcQueueView]);
  7987. }
  7988. else
  7989. {
  7990. ComponentVisible[Component] = false;
  7991. }
  7992. }
  7993. }
  7994. break;
  7995. case WM_COPYDATA:
  7996. WMCopyData(*M);
  7997. break;
  7998. case WM_MANAGES_CAPTION:
  7999. // caption managed in TTerminalManager::UpdateAppTitle()
  8000. M->Result = 1;
  8001. break;
  8002. case WM_WANTS_MOUSEWHEEL:
  8003. M->Result = 1;
  8004. break;
  8005. case CM_SHOWINGCHANGED:
  8006. CMShowingChanged(*M);
  8007. break;
  8008. case WM_CLOSE:
  8009. WMClose(*M);
  8010. break;
  8011. case WM_DPICHANGED:
  8012. WMDpiChanged(*M);
  8013. break;
  8014. case CM_DPICHANGED:
  8015. CMDpiChanged(*M);
  8016. break;
  8017. default:
  8018. TForm::Dispatch(Message);
  8019. break;
  8020. }
  8021. }
  8022. //---------------------------------------------------------------------------
  8023. void __fastcall TCustomScpExplorerForm::UpdateImages()
  8024. {
  8025. // noop
  8026. }
  8027. //---------------------------------------------------------------------------
  8028. void __fastcall TCustomScpExplorerForm::CMDpiChanged(TMessage & Message)
  8029. {
  8030. TForm::Dispatch(&Message);
  8031. // regenerate session images
  8032. DoTerminalListChanged(true);
  8033. UpdateImages();
  8034. }
  8035. //---------------------------------------------------------------------------
  8036. void __fastcall TCustomScpExplorerForm::WMDpiChanged(TMessage & Message)
  8037. {
  8038. TForm::Dispatch(&Message);
  8039. Configuration->Usage->Inc(L"PixelsPerInchChanged");
  8040. UpdatePixelsPerInchMainWindowCounter();
  8041. }
  8042. //---------------------------------------------------------------------------
  8043. void __fastcall TCustomScpExplorerForm::WMClose(TMessage & Message)
  8044. {
  8045. // Cannot close window while we are busy.
  8046. // We cannot test this in FormCloseQuery as that is called also from
  8047. // Close(), which is called by CloseApplicationAction. So we can be busy
  8048. // there already even, when it is legitimate to close the application.
  8049. // Possibly a better place to handle this would be WMSysCommand.
  8050. if (NonVisualDataModule->Busy)
  8051. {
  8052. Message.Result = 1;
  8053. }
  8054. else
  8055. {
  8056. TForm::Dispatch(&Message);
  8057. }
  8058. }
  8059. //---------------------------------------------------------------------------
  8060. void __fastcall TCustomScpExplorerForm::CMShowingChanged(TMessage & Message)
  8061. {
  8062. TForm::Dispatch(&Message);
  8063. if (Showing && (Terminal == NULL))
  8064. {
  8065. // When we are starting minimized (i.e. from an installer),
  8066. // postpone showing Login dialog until we get restored.
  8067. // Otherwise the Login dialog (and Authentication window) show restored
  8068. // over invidible (minimized) main window.
  8069. if (WindowState == wsMinimized)
  8070. {
  8071. FNeedSession = true;
  8072. }
  8073. else
  8074. {
  8075. // This happens before application ever goes idle, so the toolbars would
  8076. // stay enabled (initial state) until the Login dialog is dismissed.
  8077. UpdateActions();
  8078. NonVisualDataModule->StartBusy();
  8079. try
  8080. {
  8081. // Need to process WM_ACTIVATEAPP before showing the Login dialog,
  8082. // otherwise the dialog does not receive focus.
  8083. // With Commander interface the ProcessMessages is called already
  8084. // by TDriveView, but with Explorer interface, we need to call it explicily
  8085. Application->ProcessMessages();
  8086. // do not reload sessions, they have been loaded just now (optimization)
  8087. NeedSession(false);
  8088. }
  8089. __finally
  8090. {
  8091. NonVisualDataModule->EndBusy();
  8092. }
  8093. }
  8094. }
  8095. }
  8096. //---------------------------------------------------------------------------
  8097. void __fastcall TCustomScpExplorerForm::FormConstrainedResize(
  8098. TObject * /*Sender*/, int & MinWidth, int & MinHeight, int & MaxWidth,
  8099. int & MaxHeight)
  8100. {
  8101. // workaround for bug in TWinControl.CalcConstraints
  8102. // Check for empty rect (restore from iconic state) is done there only after
  8103. // call to AdjustClientRect, which enlarges the rect (for forms).
  8104. TRect R = GetClientRect();
  8105. // when restoring from iconic state, place no restrictions
  8106. if (IsRectEmpty(R))
  8107. {
  8108. MinWidth = 0;
  8109. MinHeight = 0;
  8110. MaxWidth = 32000;
  8111. MaxHeight = 32000;
  8112. }
  8113. }
  8114. //---------------------------------------------------------------------------
  8115. void __fastcall TCustomScpExplorerForm::GetSpaceAvailable(const UnicodeString Path,
  8116. TSpaceAvailable & ASpaceAvailable, bool & Close)
  8117. {
  8118. // terminal can be already closed (e.g. dropped connection)
  8119. if ((Terminal != NULL) && Terminal->IsCapable[fcCheckingSpaceAvailable])
  8120. {
  8121. Configuration->Usage->Inc(L"SpaceAvailableChecks");
  8122. try
  8123. {
  8124. Terminal->SpaceAvailable(Path, ASpaceAvailable);
  8125. }
  8126. catch(...)
  8127. {
  8128. if (!Terminal->Active)
  8129. {
  8130. Close = true;
  8131. }
  8132. throw;
  8133. }
  8134. }
  8135. }
  8136. //---------------------------------------------------------------------------
  8137. void __fastcall TCustomScpExplorerForm::FileSystemInfo()
  8138. {
  8139. const TSessionInfo & SessionInfo = Terminal->GetSessionInfo();
  8140. const TFileSystemInfo & FileSystemInfo = Terminal->GetFileSystemInfo(true);
  8141. TGetSpaceAvailable OnGetSpaceAvailable = NULL;
  8142. if (Terminal->IsCapable[fcCheckingSpaceAvailable])
  8143. {
  8144. OnGetSpaceAvailable = GetSpaceAvailable;
  8145. }
  8146. DoFileSystemInfoDialog(SessionInfo, FileSystemInfo, Terminal->CurrentDirectory,
  8147. OnGetSpaceAvailable);
  8148. }
  8149. //---------------------------------------------------------------------------
  8150. TSessionData * __fastcall TCustomScpExplorerForm::SessionDataForCode()
  8151. {
  8152. std::unique_ptr<TSessionData> Data(CloneCurrentSessionData());
  8153. Terminal->FillSessionDataForCode(Data.get());
  8154. return Data.release();
  8155. }
  8156. //---------------------------------------------------------------------------
  8157. void __fastcall TCustomScpExplorerForm::GenerateUrl(TStrings * Paths)
  8158. {
  8159. std::unique_ptr<TSessionData> Data(SessionDataForCode());
  8160. DoGenerateUrlDialog(Data.get(), Paths);
  8161. }
  8162. //---------------------------------------------------------------------------
  8163. void __fastcall TCustomScpExplorerForm::SessionGenerateUrl()
  8164. {
  8165. GenerateUrl(NULL);
  8166. }
  8167. //---------------------------------------------------------------------------
  8168. void __fastcall TCustomScpExplorerForm::FileGenerateUrl()
  8169. {
  8170. std::unique_ptr<TStrings> Paths(new TStringList());
  8171. DirView(osCurrent)->CreateFileList(false, true, Paths.get());
  8172. GenerateUrl(Paths.get());
  8173. }
  8174. //---------------------------------------------------------------------------
  8175. void __fastcall TCustomScpExplorerForm::SetSessionColor(TColor value)
  8176. {
  8177. if (value != FSessionColor)
  8178. {
  8179. FSessionColor = value;
  8180. TColor C = (value != 0 ? value : Vcl::Graphics::clNone);
  8181. TTBXColorItem * ColorItem = dynamic_cast<TTBXColorItem *>(
  8182. static_cast<TObject *>(GetComponent(fcColorMenu)));
  8183. DebugAssert(ColorItem != NULL);
  8184. ColorItem->Color = C;
  8185. NonVisualDataModule->ColorMenuItem->Color = C;
  8186. // Is null when called from LastTerminalClosed
  8187. if (Terminal != NULL)
  8188. {
  8189. SessionsPageControl->ActivePage->ImageIndex = AddSessionColor(value);
  8190. }
  8191. UpdateControls();
  8192. }
  8193. }
  8194. //---------------------------------------------------------------------------
  8195. bool __fastcall TCustomScpExplorerForm::CancelNote(bool Force)
  8196. {
  8197. bool Result = FNoteTimer->Enabled;
  8198. if (Result)
  8199. {
  8200. // cannot cancel note too early
  8201. bool NotEarly =
  8202. (Now() - FNoteShown >
  8203. EncodeTimeVerbose(0, 0, (unsigned short)(WinConfiguration->NotificationsStickTime), 0));
  8204. if (Force || NotEarly)
  8205. {
  8206. FNoteTimer->Enabled = false;
  8207. FNote = L"";
  8208. SAFE_DESTROY(FNoteData);
  8209. FOnNoteClick = NULL;
  8210. FNoteHints = FNotes->Text;
  8211. FNoteHints.Delete(FNoteHints.Length() - 1, 2);
  8212. UpdateStatusBar();
  8213. }
  8214. }
  8215. return Result;
  8216. }
  8217. //---------------------------------------------------------------------------
  8218. void __fastcall TCustomScpExplorerForm::NoteTimer(TObject * /*Sender*/)
  8219. {
  8220. DebugAssert(FNoteTimer->Enabled);
  8221. CancelNote(true);
  8222. }
  8223. //---------------------------------------------------------------------------
  8224. void __fastcall TCustomScpExplorerForm::AddNote(UnicodeString Note, bool UpdateNow)
  8225. {
  8226. int P = Note.Pos(L"\n");
  8227. if (P > 0)
  8228. {
  8229. Note.SetLength(P - 1);
  8230. }
  8231. FNotes->Add(FORMAT(L"[%s] %s",
  8232. (FormatDateTime(Configuration->TimeFormat, Now()), Note)));
  8233. while (FNotes->Count > 10)
  8234. {
  8235. FNotes->Delete(0);
  8236. }
  8237. if (UpdateNow)
  8238. {
  8239. FNoteHints = FNotes->Text;
  8240. FNoteHints.Delete(FNoteHints.Length() - 1, 2);
  8241. UpdateStatusBar();
  8242. }
  8243. }
  8244. //---------------------------------------------------------------------------
  8245. void __fastcall TCustomScpExplorerForm::PostNote(UnicodeString Note,
  8246. unsigned int Seconds, TNotifyEvent OnNoteClick, TObject * NoteData)
  8247. {
  8248. int P = Note.Pos(L"\n");
  8249. if (P > 0)
  8250. {
  8251. Note.SetLength(P - 1);
  8252. }
  8253. FNoteHints = FNotes->Text;
  8254. FNoteHints.Delete(FNoteHints.Length() - 1, 2);
  8255. FNote = Note;
  8256. SAFE_DESTROY(FNoteData);
  8257. FNoteData = NoteData;
  8258. FOnNoteClick = OnNoteClick;
  8259. AddNote(Note, false);
  8260. UpdateStatusBar();
  8261. FNoteShown = Now();
  8262. FNoteTimer->Enabled = false;
  8263. if (Seconds == 0)
  8264. {
  8265. Seconds = WinConfiguration->NotificationsTimeout;
  8266. }
  8267. FNoteTimer->Interval = Seconds * MSecsPerSec;
  8268. FNoteTimer->Enabled = true;
  8269. }
  8270. //---------------------------------------------------------------------------
  8271. void __fastcall TCustomScpExplorerForm::ReadDirectoryCancelled()
  8272. {
  8273. PostNote(LoadStr(DIRECTORY_READING_CANCELLED), 0, NULL, NULL);
  8274. }
  8275. //---------------------------------------------------------------------------
  8276. void __fastcall TCustomScpExplorerForm::SynchronizeBrowsingChanged()
  8277. {
  8278. if (NonVisualDataModule->SynchronizeBrowsingAction->Checked)
  8279. {
  8280. Configuration->Usage->Inc(L"SynchronizeBrowsingEnabled");
  8281. }
  8282. PostNote(FORMAT(LoadStrPart(SYNC_DIR_BROWSE_TOGGLE, 1),
  8283. (LoadStrPart(SYNC_DIR_BROWSE_TOGGLE,
  8284. (NonVisualDataModule->SynchronizeBrowsingAction->Checked ? 2 : 3)))),
  8285. 0, NULL, NULL);
  8286. }
  8287. //---------------------------------------------------------------------------
  8288. void __fastcall TCustomScpExplorerForm::ToggleShowHiddenFiles()
  8289. {
  8290. WinConfiguration->ShowHiddenFiles = !WinConfiguration->ShowHiddenFiles;
  8291. PostNote(FORMAT(LoadStrPart(SHOW_HIDDEN_FILES_TOGGLE, 1),
  8292. (LoadStrPart(SHOW_HIDDEN_FILES_TOGGLE,
  8293. (WinConfiguration->ShowHiddenFiles ? 2 : 3)))), 0, NULL, NULL);
  8294. }
  8295. //---------------------------------------------------------------------------
  8296. void __fastcall TCustomScpExplorerForm::SetFormatSizeBytes(TFormatBytesStyle Style)
  8297. {
  8298. WinConfiguration->FormatSizeBytes = Style;
  8299. }
  8300. //---------------------------------------------------------------------------
  8301. void __fastcall TCustomScpExplorerForm::ToggleAutoReadDirectoryAfterOp()
  8302. {
  8303. Configuration->AutoReadDirectoryAfterOp = !Configuration->AutoReadDirectoryAfterOp;
  8304. PostNote(FORMAT(LoadStrPart(AUTO_READ_DIRECTORY_TOGGLE, 1),
  8305. (LoadStrPart(AUTO_READ_DIRECTORY_TOGGLE,
  8306. (Configuration->AutoReadDirectoryAfterOp ? 2 : 3)))), 0, NULL, NULL);
  8307. }
  8308. //---------------------------------------------------------------------------
  8309. void __fastcall TCustomScpExplorerForm::StatusBarPanelDblClick(
  8310. TTBXCustomStatusBar * Sender, TTBXStatusPanel * Panel)
  8311. {
  8312. if (Panel->Index == 0)
  8313. {
  8314. if (FOnNoteClick != NULL)
  8315. {
  8316. // prevent the user data from being freed by possible call
  8317. // to CancelNote or PostNote during call to OnNoteClick
  8318. std::unique_ptr<TObject> NoteData(FNoteData);
  8319. TNotifyEvent OnNoteClick = FOnNoteClick;
  8320. FNoteData = NULL;
  8321. // need to cancel the note as we are going to delete its user data
  8322. CancelNote(true);
  8323. OnNoteClick(NoteData.get());
  8324. }
  8325. }
  8326. if (Panel->Index >= Sender->Panels->Count - SessionPanelCount)
  8327. {
  8328. FileSystemInfo();
  8329. }
  8330. }
  8331. //---------------------------------------------------------------------------
  8332. void __fastcall TCustomScpExplorerForm::LockWindow(bool Force)
  8333. {
  8334. // workaround:
  8335. // 1) for unknown reason, disabling window, while minimized,
  8336. // prevents it from restoring, even if it was enabled again meanwhile
  8337. // 2) when disabling the main window, while another has focus
  8338. // minimize is no longer possible
  8339. // ("keep up to date" dialog - though does not seem to be the case with "find file" -
  8340. // so it has probably something to do with the semi-modalness of the "keep up to date" dialog)
  8341. // Shouldn't we use IsApplicationMinimized() here?
  8342. if ((FLockSuspendLevel == 0) && !IsIconic(Application->Handle) && (Force || (Screen->ActiveForm == this)))
  8343. {
  8344. Enabled = false;
  8345. }
  8346. FLockLevel++;
  8347. }
  8348. //---------------------------------------------------------------------------
  8349. void __fastcall TCustomScpExplorerForm::UnlockWindow()
  8350. {
  8351. DebugAssert(FLockLevel > 0);
  8352. FLockLevel--;
  8353. if (FLockLevel == 0)
  8354. {
  8355. DebugAssert(FLockSuspendLevel == 0);
  8356. Enabled = true;
  8357. }
  8358. }
  8359. //---------------------------------------------------------------------------
  8360. void __fastcall TCustomScpExplorerForm::SuspendWindowLock()
  8361. {
  8362. // We need to make sure that window is enabled when the last modal window closes
  8363. // otherwise focus is not restored correctly.
  8364. // So we re-enable the window when modal window opens and
  8365. // disable it back after is closes
  8366. if (FLockLevel > 0)
  8367. {
  8368. // while we have nesting counter, we know that we never be called
  8369. // recursivelly as Application->OnModalBegin is called only
  8370. // for the top-level modal window
  8371. if (DebugAlwaysTrue(FLockSuspendLevel == 0))
  8372. {
  8373. // won't be disabled when conditions in LockWindow() were not satisfied
  8374. FDisabledOnLockSuspend = !Enabled;
  8375. // When minimized to tray (or actually when set to SW_HIDE),
  8376. // setting Enabled makes the window focusable even when there's
  8377. // modal window over it
  8378. if (!FTrayIcon->Visible)
  8379. {
  8380. Enabled = true;
  8381. }
  8382. }
  8383. FLockSuspendLevel++;
  8384. }
  8385. }
  8386. //---------------------------------------------------------------------------
  8387. void __fastcall TCustomScpExplorerForm::ResumeWindowLock()
  8388. {
  8389. if (FLockSuspendLevel > 0)
  8390. {
  8391. DebugAssert(FLockLevel > 0);
  8392. FLockSuspendLevel--;
  8393. // see comment in SuspendWindowLock
  8394. if (DebugAlwaysTrue(FLockSuspendLevel == 0))
  8395. {
  8396. // Note that window can be enabled here, when we were minized to tray when
  8397. // was SuspendWindowLock() called.
  8398. // We should possibly do the same check as in LockWindow(),
  8399. // if it is ever possible that the consitions change between
  8400. // SuspendWindowLock() and ResumeWindowLock()
  8401. Enabled = !FDisabledOnLockSuspend;
  8402. }
  8403. }
  8404. }
  8405. //---------------------------------------------------------------------------
  8406. void __fastcall TCustomScpExplorerForm::UpdateRemotePathComboBox(bool TextOnly)
  8407. {
  8408. if (!TextOnly)
  8409. {
  8410. TTBXComboBoxItem * RemotePathComboBox =
  8411. reinterpret_cast<TTBXComboBoxItem *>(GetComponent(fcRemotePathComboBox));
  8412. TStrings * Items = RemotePathComboBox->Strings;
  8413. Items->BeginUpdate();
  8414. try
  8415. {
  8416. Items->Clear();
  8417. if (Terminal != NULL)
  8418. {
  8419. UnicodeString APath = UnixExcludeTrailingBackslash(RemoteDirView->Path);
  8420. while (!IsUnixRootPath(APath))
  8421. {
  8422. int P = APath.LastDelimiter(L'/');
  8423. DebugAssert(P >= 0);
  8424. Items->Insert(0, APath.SubString(P + 1, APath.Length() - P));
  8425. APath.SetLength(P - 1);
  8426. }
  8427. Items->Insert(0, Customunixdirview_SUnixDefaultRootName);
  8428. }
  8429. }
  8430. __finally
  8431. {
  8432. RemotePathComboBox->ItemIndex = Items->Count - 1;
  8433. // Setting ItemIndex to -1 does not reset its text
  8434. if (Items->Count == 0)
  8435. {
  8436. RemotePathComboBox->Text = L"";
  8437. }
  8438. Items->EndUpdate();
  8439. }
  8440. }
  8441. }
  8442. //---------------------------------------------------------------------------
  8443. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxAdjustImageIndex(
  8444. TTBXComboBoxItem * Sender, const UnicodeString /*AText*/, int AIndex,
  8445. int & ImageIndex)
  8446. {
  8447. if (AIndex < 0)
  8448. {
  8449. AIndex = Sender->ItemIndex;
  8450. }
  8451. ImageIndex = (AIndex < Sender->Strings->Count - 1 ? StdDirIcon : StdDirSelIcon);
  8452. }
  8453. //---------------------------------------------------------------------------
  8454. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxDrawItem(
  8455. TTBXCustomList * /*Sender*/, TCanvas * /*ACanvas*/, TRect & ARect, int AIndex,
  8456. int /*AHoverIndex*/, bool & /*DrawDefault*/)
  8457. {
  8458. ARect.Left += (10 * AIndex);
  8459. }
  8460. //---------------------------------------------------------------------------
  8461. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxMeasureWidth(
  8462. TTBXCustomList * /*Sender*/, TCanvas * /*ACanvas*/, int AIndex, int &AWidth)
  8463. {
  8464. AWidth += (10 * AIndex);
  8465. }
  8466. //---------------------------------------------------------------------------
  8467. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxItemClick(
  8468. TObject * Sender)
  8469. {
  8470. TTBXComboBoxItem * RemotePathComboBox = dynamic_cast<TTBXComboBoxItem*>(Sender);
  8471. UnicodeString APath = UnixExcludeTrailingBackslash(RemoteDirView->Path);
  8472. int Index = RemotePathComboBox->ItemIndex;
  8473. while (Index < RemotePathComboBox->Strings->Count - 1)
  8474. {
  8475. APath = UnixExtractFileDir(APath);
  8476. Index++;
  8477. }
  8478. // VanDyke style paths
  8479. if (APath.IsEmpty())
  8480. {
  8481. DebugAssert(RemotePathComboBox->ItemIndex == 0);
  8482. APath = ROOTDIRECTORY;
  8483. }
  8484. if (RemoteDirView->Path != APath)
  8485. {
  8486. RemoteDirView->Path = APath;
  8487. }
  8488. }
  8489. //---------------------------------------------------------------------------
  8490. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxCancel(TObject * Sender)
  8491. {
  8492. DebugAssert(Sender == GetComponent(fcRemotePathComboBox));
  8493. DebugUsedParam(Sender);
  8494. UpdateRemotePathComboBox(true);
  8495. }
  8496. //---------------------------------------------------------------------------
  8497. void __fastcall TCustomScpExplorerForm::DirViewEditing(
  8498. TObject * Sender, TListItem * Item, bool & /*AllowEdit*/)
  8499. {
  8500. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  8501. DebugAssert(DirView != NULL);
  8502. if (!WinConfiguration->RenameWholeName && !DirView->ItemIsDirectory(Item))
  8503. {
  8504. HWND Edit = ListView_GetEditControl(DirView->Handle);
  8505. // OnEditing is called also from TCustomListView::CanEdit
  8506. if (Edit != NULL)
  8507. {
  8508. EditSelectBaseName(Edit);
  8509. }
  8510. }
  8511. }
  8512. //---------------------------------------------------------------------------
  8513. //---------------------------------------------------------------------------
  8514. TDragDropFilesEx * __fastcall TCustomScpExplorerForm::CreateDragDropFilesEx()
  8515. {
  8516. TDragDropFilesEx * Result = new TDragDropFilesEx(this);
  8517. Result->AutoDetectDnD = false;
  8518. Result->NeedValid = TFileExMustDnDSet() << nvFilename;
  8519. Result->RenderDataOn = rdoEnterAndDropSync;
  8520. Result->TargetEffects = TDropEffectSet() << deCopy << deMove;
  8521. return Result;
  8522. }
  8523. //---------------------------------------------------------------------------
  8524. void __fastcall TCustomScpExplorerForm::CreateWnd()
  8525. {
  8526. TForm::CreateWnd();
  8527. if (FSessionsDragDropFilesEx == NULL)
  8528. {
  8529. FSessionsDragDropFilesEx = CreateDragDropFilesEx();
  8530. FSessionsDragDropFilesEx->OnDragOver = SessionsDDDragOver;
  8531. FSessionsDragDropFilesEx->OnProcessDropped = SessionsDDProcessDropped;
  8532. FSessionsDragDropFilesEx->OnDragEnter = SessionsDDDragEnter;
  8533. FSessionsDragDropFilesEx->OnDragLeave = SessionsDDDragLeave;
  8534. }
  8535. if (FQueueDragDropFilesEx == NULL)
  8536. {
  8537. FQueueDragDropFilesEx = CreateDragDropFilesEx();
  8538. // No need to set OnDragOver as we do not have any restrictions
  8539. FQueueDragDropFilesEx->OnProcessDropped = QueueDDProcessDropped;
  8540. FQueueDragDropFilesEx->OnDragEnter = QueueDDDragEnter;
  8541. FQueueDragDropFilesEx->OnDragLeave = QueueDDDragLeave;
  8542. }
  8543. }
  8544. //---------------------------------------------------------------------------
  8545. void __fastcall TCustomScpExplorerForm::DestroyWnd()
  8546. {
  8547. TForm::DestroyWnd();
  8548. FSessionsDragDropFilesEx->DragDropControl = NULL;
  8549. FQueueDragDropFilesEx->DragDropControl = NULL;
  8550. }
  8551. //---------------------------------------------------------------------------
  8552. void __fastcall TCustomScpExplorerForm::FormShow(TObject * /*Sender*/)
  8553. {
  8554. SideEnter(FCurrentSide);
  8555. FEverShown = true;
  8556. }
  8557. //---------------------------------------------------------------------------
  8558. void __fastcall TCustomScpExplorerForm::DoFindFiles(
  8559. TTerminal * ATerminal, UnicodeString Directory, const TFileMasks & FileMask,
  8560. TFileFoundEvent OnFileFound, TFindingFileEvent OnFindingFile)
  8561. {
  8562. if (!NonVisualDataModule->Busy)
  8563. {
  8564. TTerminalManager::Instance()->ActiveTerminal = ATerminal;
  8565. Configuration->Usage->Inc(L"FileFinds");
  8566. LockWindow(true);
  8567. NonVisualDataModule->StartBusy();
  8568. WinConfiguration->LockedInterface = true;
  8569. try
  8570. {
  8571. FTerminal->FilesFind(Directory, FileMask, OnFileFound, OnFindingFile);
  8572. }
  8573. __finally
  8574. {
  8575. WinConfiguration->LockedInterface = false;
  8576. NonVisualDataModule->EndBusy();
  8577. UnlockWindow();
  8578. }
  8579. }
  8580. }
  8581. //---------------------------------------------------------------------------
  8582. void __fastcall TCustomScpExplorerForm::DoFocusRemotePath(TTerminal * ATerminal, const UnicodeString & Path)
  8583. {
  8584. if (!NonVisualDataModule->Busy)
  8585. {
  8586. TTerminalManager::Instance()->ActiveTerminal = ATerminal;
  8587. SetFocus();
  8588. RemoteDirView->Path = UnixExtractFilePath(Path);
  8589. TListItem * Item = RemoteDirView->FindFileItem(UnixExtractFileName(Path));
  8590. if (Item != NULL)
  8591. {
  8592. RemoteDirView->ItemFocused = Item;
  8593. RemoteDirView->ItemFocused->MakeVisible(false);
  8594. RemoteDirView->SetFocus();
  8595. }
  8596. }
  8597. }
  8598. //---------------------------------------------------------------------------
  8599. bool __fastcall TCustomScpExplorerForm::CanOperateOnFoundFiles(TTerminal * ATerminal)
  8600. {
  8601. bool Result = !NonVisualDataModule->Busy;
  8602. if (Result)
  8603. {
  8604. TTerminalManager::Instance()->ActiveTerminal = ATerminal;
  8605. }
  8606. return Result;
  8607. }
  8608. //---------------------------------------------------------------------------
  8609. void __fastcall TCustomScpExplorerForm::DoOperationOnFoundFiles(
  8610. TFileOperation Operation, TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  8611. {
  8612. if (CanOperateOnFoundFiles(ATerminal))
  8613. {
  8614. TValueRestorer<TFileOperationFinishedEvent> OnFileOperationFinishedRestorer(FOnFileOperationFinished);
  8615. FOnFileOperationFinished = OnFileOperationFinished;
  8616. ExecuteFileOperation(Operation, osRemote, FileList, false, NULL);
  8617. }
  8618. }
  8619. //---------------------------------------------------------------------------
  8620. void __fastcall TCustomScpExplorerForm::DoDeleteFoundFiles(
  8621. TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  8622. {
  8623. DoOperationOnFoundFiles(foDelete, ATerminal, FileList, OnFileOperationFinished);
  8624. }
  8625. //---------------------------------------------------------------------------
  8626. void __fastcall TCustomScpExplorerForm::DoDownloadFoundFiles(
  8627. TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  8628. {
  8629. DoOperationOnFoundFiles(foCopy, ATerminal, FileList, OnFileOperationFinished);
  8630. }
  8631. //---------------------------------------------------------------------------
  8632. void __fastcall TCustomScpExplorerForm::DoEditFoundFiles(
  8633. TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  8634. {
  8635. if (CanOperateOnFoundFiles(ATerminal))
  8636. {
  8637. for (int Index = 0; Index < FileList->Count; Index++)
  8638. {
  8639. UnicodeString FileName = FileList->Strings[Index];
  8640. TRemoteFile * File = static_cast<TRemoteFile *>(FileList->Objects[Index]);
  8641. ExecuteRemoteFile(FileName, File, efDefaultEditor);
  8642. OnFileOperationFinished(FileName, true);
  8643. }
  8644. }
  8645. }
  8646. //---------------------------------------------------------------------------
  8647. void __fastcall TCustomScpExplorerForm::RemoteFindFiles()
  8648. {
  8649. FFileFindTerminal = Terminal;
  8650. ShowFileFindDialog(
  8651. Terminal, RemoteDirView->Path, DoFindFiles, DoFocusRemotePath, DoDeleteFoundFiles,
  8652. DoDownloadFoundFiles, DoEditFoundFiles);
  8653. }
  8654. //---------------------------------------------------------------------------
  8655. void __fastcall TCustomScpExplorerForm::UpdateTaskbarList(ITaskbarList3 * TaskbarList)
  8656. {
  8657. FTaskbarList = TaskbarList;
  8658. }
  8659. //---------------------------------------------------------------------------
  8660. void __fastcall TCustomScpExplorerForm::SessionsPageControlMouseDown(
  8661. TObject * /*Sender*/, TMouseButton Button, TShiftState /*Shift*/, int X, int Y)
  8662. {
  8663. int Index = SessionsPageControl->IndexOfTabAt(X, Y);
  8664. if (Index >= 0)
  8665. {
  8666. if (Button == mbLeft)
  8667. {
  8668. // "Mouse down" is raised only after tab is switched.
  8669. // If switching tab (switching session) takes long enough for user
  8670. // to actually release the button, "mouse down" is still raised,
  8671. // but we do not get "mouse up" event, so dragging is not cancelled,
  8672. // prevent that by not beginning dragging in the first place.
  8673. if (IsKeyPressed(VK_LBUTTON))
  8674. {
  8675. // when user clicks the "+", we get mouse down only after the session
  8676. // is closed, when new session tab is already on X:Y, so dragging
  8677. // starts, prevent that
  8678. if (MilliSecondsBetween(Now(), FSessionsPageControlNewSessionTime) > 500)
  8679. {
  8680. TTerminal * Terminal = GetSessionTabTerminal(SessionsPageControl->Pages[Index]);
  8681. if (Terminal != NULL)
  8682. {
  8683. SessionsPageControl->BeginDrag(false);
  8684. }
  8685. }
  8686. }
  8687. }
  8688. else if (Button == mbMiddle)
  8689. {
  8690. // ignore middle-click for "New session tab"
  8691. TTerminal * Terminal = GetSessionTabTerminal(SessionsPageControl->Pages[Index]);
  8692. if (Terminal != NULL)
  8693. {
  8694. SessionsPageControl->ActivePageIndex = Index;
  8695. // Switch to session tab (has to be session tab, due to previous check)
  8696. if (DebugAlwaysTrue(SessionTabSwitched()))
  8697. {
  8698. CloseSession();
  8699. }
  8700. }
  8701. }
  8702. }
  8703. }
  8704. //---------------------------------------------------------------------------
  8705. void __fastcall TCustomScpExplorerForm::SessionsPageControlDragDrop(
  8706. TObject * /*Sender*/, TObject * /*Source*/, int X, int Y)
  8707. {
  8708. int Index = SessionsPageControl->IndexOfTabAt(X, Y);
  8709. // do not allow dropping on the "+" tab
  8710. TTerminal * TargetTerminal = GetSessionTabTerminal(SessionsPageControl->Pages[Index]);
  8711. if ((TargetTerminal != NULL) &&
  8712. (SessionsPageControl->ActivePage->PageIndex != Index))
  8713. {
  8714. Configuration->Usage->Inc(L"SessionTabMoves");
  8715. // this is almost redundant as we would recreate tabs in DoTerminalListChanged,
  8716. // but we want to actually prevent that to avoid flicker
  8717. SessionsPageControl->ActivePage->PageIndex = Index;
  8718. TTerminal * Terminal = GetSessionTabTerminal(SessionsPageControl->ActivePage);
  8719. TTerminalManager::Instance()->Move(Terminal, TargetTerminal);
  8720. }
  8721. }
  8722. //---------------------------------------------------------------------------
  8723. void __fastcall TCustomScpExplorerForm::SessionsPageControlDragOver(
  8724. TObject * Sender, TObject * Source, int X, int Y,
  8725. TDragState /*State*/, bool & Accept)
  8726. {
  8727. Accept = (Sender == Source);
  8728. if (Accept)
  8729. {
  8730. int Index = SessionsPageControl->IndexOfTabAt(X, Y);
  8731. TTerminal * Terminal = GetSessionTabTerminal(SessionsPageControl->Pages[Index]);
  8732. // do not allow dragging to the "+" tab
  8733. Accept = (Terminal != NULL);
  8734. }
  8735. }
  8736. //---------------------------------------------------------------------------
  8737. void __fastcall TCustomScpExplorerForm::SessionsDDDragOver(int /*KeyState*/,
  8738. const TPoint & Point, int & Effect)
  8739. {
  8740. int Index = SessionsPageControl->IndexOfTabAt(Point.X, Point.Y);
  8741. if (Index < 0)
  8742. {
  8743. Effect = DROPEFFECT_None;
  8744. }
  8745. else
  8746. {
  8747. TTerminal * TargetTerminal = GetSessionTabTerminal(SessionsPageControl->Pages[Index]);
  8748. // do not allow dropping on the "+" tab
  8749. if (TargetTerminal == NULL)
  8750. {
  8751. Effect = DROPEFFECT_None;
  8752. }
  8753. }
  8754. }
  8755. //---------------------------------------------------------------------------
  8756. void __fastcall TCustomScpExplorerForm::SessionsDDProcessDropped(
  8757. TObject * /*Sender*/, int /*KeyState*/, const TPoint & Point, int Effect)
  8758. {
  8759. int Index = SessionsPageControl->IndexOfTabAt(Point.X, Point.Y);
  8760. // do not allow dropping on the "+" tab
  8761. TTerminal * TargetTerminal = GetSessionTabTerminal(SessionsPageControl->Pages[Index]);
  8762. if (TargetTerminal != NULL)
  8763. {
  8764. DebugAssert(!IsFileControl(DropSourceControl, osRemote));
  8765. if (!IsFileControl(DropSourceControl, osRemote))
  8766. {
  8767. TTerminalManager::Instance()->ActiveTerminal = TargetTerminal;
  8768. RemoteFileControlDragDropFileOperation(SessionsPageControl, Effect,
  8769. // Why don't we use Terminal->CurrentDirectory directly?
  8770. TTerminalManager::Instance()->ActiveTerminal->CurrentDirectory, false);
  8771. }
  8772. }
  8773. }
  8774. //---------------------------------------------------------------------------
  8775. void __fastcall TCustomScpExplorerForm::QueueDDProcessDropped(
  8776. TObject * /*Sender*/, int /*KeyState*/, const TPoint & /*Point*/, int Effect)
  8777. {
  8778. // Downloads are handled in RemoteFileControlDDEnd
  8779. if (!IsFileControl(DropSourceControl, osRemote))
  8780. {
  8781. RemoteFileControlDragDropFileOperation(QueueView3, Effect,
  8782. Terminal->CurrentDirectory,
  8783. // force queue
  8784. true);
  8785. }
  8786. }
  8787. //---------------------------------------------------------------------------
  8788. void __fastcall TCustomScpExplorerForm::FormClose(TObject * /*Sender*/, TCloseAction & /*Action*/)
  8789. {
  8790. FShowing = false;
  8791. // Do not save empty workspace
  8792. if (WinConfiguration->AutoSaveWorkspace && (Terminal != NULL))
  8793. {
  8794. std::unique_ptr<TObjectList> DataList(DoCollectWorkspace());
  8795. UnicodeString Name = WorkspaceName();
  8796. DoSaveWorkspace(
  8797. Name, DataList.get(),
  8798. !Configuration->DisablePasswordStoring &&
  8799. WinConfiguration->AutoSaveWorkspacePasswords, false);
  8800. WinConfiguration->LastStoredSession = Name;
  8801. }
  8802. }
  8803. //---------------------------------------------------------------------------
  8804. void __fastcall TCustomScpExplorerForm::RemoteDirViewRead(TObject * /*Sender*/)
  8805. {
  8806. TManagedTerminal * ManagedTerminal =
  8807. dynamic_cast<TManagedTerminal *>(RemoteDirView->Terminal);
  8808. if (ManagedTerminal != NULL)
  8809. {
  8810. ManagedTerminal->DirectoryLoaded = Now();
  8811. }
  8812. }
  8813. //---------------------------------------------------------------------------
  8814. void __fastcall TCustomScpExplorerForm::DirViewSelectItem(TObject * Sender,
  8815. TListItem * /*Item*/, bool /*Selected*/)
  8816. {
  8817. TCustomDirView * DirView = reinterpret_cast<TCustomDirView *>(Sender);
  8818. switch (DirView->LastSelectMethod)
  8819. {
  8820. case smKeyboard:
  8821. Configuration->Usage->Inc(L"KeyboardSelections");
  8822. break;
  8823. case smMouse:
  8824. Configuration->Usage->Inc(L"MouseSelections");
  8825. break;
  8826. }
  8827. }
  8828. //---------------------------------------------------------------------------
  8829. int __fastcall TCustomScpExplorerForm::AddSessionColor(TColor Color)
  8830. {
  8831. if (Color != 0)
  8832. {
  8833. AddSessionColorImage(FSessionColors, Color, FSessionColorMaskImageIndex);
  8834. return FSessionColors->Count - 1;
  8835. }
  8836. else
  8837. {
  8838. return FSessionTabImageIndex;
  8839. }
  8840. }
  8841. //---------------------------------------------------------------------------
  8842. int __fastcall TCustomScpExplorerForm::AddFixedSessionImage(int GlyphsSourceIndex)
  8843. {
  8844. TPngImageCollectionItem * Item =
  8845. GlyphsModule->ExplorerImages->PngImages->Items[GlyphsSourceIndex];
  8846. FSessionColors->AddPng(Item->PngImage);
  8847. return FSessionColors->Count - 1;
  8848. }
  8849. //---------------------------------------------------------------------------
  8850. void __fastcall TCustomScpExplorerForm::AddFixedSessionImages()
  8851. {
  8852. FSessionColors->SetSize(GlyphsModule->ExplorerImages->Width, GlyphsModule->ExplorerImages->Height);
  8853. FNewSessionTabImageIndex = AddFixedSessionImage(NonVisualDataModule->NewSessionAction->ImageIndex);
  8854. FSessionTabImageIndex = AddFixedSessionImage(SiteImageIndex);
  8855. FSessionColorMaskImageIndex = AddFixedSessionImage(SiteColorMaskImageIndex);
  8856. }
  8857. //---------------------------------------------------------------------------
  8858. void __fastcall TCustomScpExplorerForm::CollectItemsWithTextDisplayMode(TWinControl * Control)
  8859. {
  8860. for (int Index = 0; Index < Control->ControlCount; Index++)
  8861. {
  8862. TControl * AControl = Control->Controls[Index];
  8863. TWinControl * WinControl = dynamic_cast<TWinControl *>(AControl);
  8864. if (WinControl != NULL)
  8865. {
  8866. CollectItemsWithTextDisplayMode(WinControl);
  8867. }
  8868. TTBCustomToolbar * Toolbar = dynamic_cast<TTBCustomToolbar *>(AControl);
  8869. if (Toolbar != NULL)
  8870. {
  8871. // we care for top-level items only
  8872. TTBCustomItem * Items = Toolbar->Items;
  8873. for (int ItemIndex = 0; ItemIndex < Items->Count; ItemIndex++)
  8874. {
  8875. TTBCustomItem * Item = Items->Items[ItemIndex];
  8876. if (((Item->DisplayMode == nbdmImageAndText) ||
  8877. (dynamic_cast<TTBXLabelItem *>(Item) != NULL)) &&
  8878. EligibleForImageDisplayMode(Item))
  8879. {
  8880. FItemsWithTextDisplayMode.insert(Item);
  8881. }
  8882. }
  8883. }
  8884. }
  8885. }
  8886. //---------------------------------------------------------------------------
  8887. bool __fastcall TCustomScpExplorerForm::EligibleForImageDisplayMode(TTBCustomItem * /*Item*/)
  8888. {
  8889. return true;
  8890. }
  8891. //---------------------------------------------------------------------------
  8892. bool __fastcall TCustomScpExplorerForm::UpdateToolbarDisplayMode()
  8893. {
  8894. bool SelectiveToolbarText = WinConfiguration->SelectiveToolbarText;
  8895. TTBItemDisplayMode DisplayMode = (SelectiveToolbarText ? nbdmImageAndText : nbdmDefault);
  8896. typedef std::set<TTBCustomItem *> TItemsWithTextDisplayMode;
  8897. TItemsWithTextDisplayMode::iterator i = FItemsWithTextDisplayMode.begin();
  8898. bool Result = true;
  8899. while (Result && (i != FItemsWithTextDisplayMode.end()))
  8900. {
  8901. TTBCustomItem * Item = *i;
  8902. TTBXLabelItem * Label = dynamic_cast<TTBXLabelItem *>(Item);
  8903. if (Label != NULL)
  8904. {
  8905. // optimization
  8906. if (Label->Visible == SelectiveToolbarText)
  8907. {
  8908. Result = false;
  8909. }
  8910. else
  8911. {
  8912. Label->Visible = SelectiveToolbarText;
  8913. }
  8914. }
  8915. else
  8916. {
  8917. // optimization
  8918. if (Item->DisplayMode == DisplayMode)
  8919. {
  8920. Result = false;
  8921. }
  8922. else
  8923. {
  8924. Item->DisplayMode = DisplayMode;
  8925. }
  8926. }
  8927. ++i;
  8928. }
  8929. if (Result)
  8930. {
  8931. UpdateNewSessionTab();
  8932. }
  8933. return Result;
  8934. }
  8935. //---------------------------------------------------------------------------
  8936. void __fastcall TCustomScpExplorerForm::DisplaySystemContextMenu()
  8937. {
  8938. DebugFail();
  8939. }
  8940. //---------------------------------------------------------------------------
  8941. bool __fastcall TCustomScpExplorerForm::IsBusy()
  8942. {
  8943. // Among other, a lock level is non zero, while directory is being loaded,
  8944. // even when it happens as a result of dirview navigation,
  8945. // i.e. when TNonVisualDataModule is NOT FBusy.
  8946. // That's why the TNonVisualDataModule::GetBusy calls this method.
  8947. // Among other this prevents a panel auto update to occur while
  8948. // directory is changing.
  8949. return (FLockLevel > 0) || DirView(osCurrent)->IsEditing();
  8950. }
  8951. //---------------------------------------------------------------------------
  8952. Boolean __fastcall TCustomScpExplorerForm::AllowedAction(TAction * /*Action*/, TActionAllowed Allowed)
  8953. {
  8954. // While the window is disabled, we still seem to process menu shortcuts at least,
  8955. // so stop it at least here.
  8956. // See also TCustomScpExplorerForm::RemoteDirViewBusy
  8957. return
  8958. (Allowed == aaUpdate) ||
  8959. !NonVisualDataModule->Busy;
  8960. }
  8961. //---------------------------------------------------------------------------
  8962. void __fastcall TCustomScpExplorerForm::EditMenuItemPopup(TTBCustomItem * Sender, bool FromLink)
  8963. {
  8964. NonVisualDataModule->EditMenuItemPopup(Sender, FromLink);
  8965. }
  8966. //---------------------------------------------------------------------------
  8967. void __fastcall TCustomScpExplorerForm::DirViewBusy(TObject * /*Sender*/, int Busy, bool & State)
  8968. {
  8969. // This is somewhat redundant to LockWindow() call from
  8970. // TTerminalManager::TerminalReadDirectoryProgress.
  8971. // But disabling window is known not to block keyboard shorcuts
  8972. // (see TCustomScpExplorerForm::AllowedAction), this hopefully works.
  8973. if (Busy > 0)
  8974. {
  8975. if (NonVisualDataModule->Busy)
  8976. {
  8977. State = false;
  8978. }
  8979. else
  8980. {
  8981. NonVisualDataModule->StartBusy();
  8982. // Will be countered for TCustomDirView.DoExecute in DoDirViewExecFile
  8983. LockWindow();
  8984. }
  8985. }
  8986. else if (Busy < 0)
  8987. {
  8988. UnlockWindow();
  8989. NonVisualDataModule->EndBusy();
  8990. }
  8991. else
  8992. {
  8993. State = NonVisualDataModule->Busy;
  8994. }
  8995. }
  8996. //---------------------------------------------------------------------------
  8997. void __fastcall TCustomScpExplorerForm::SessionsPageControlContextPopup(TObject * /*Sender*/, TPoint & MousePos, bool & Handled)
  8998. {
  8999. int Index = SessionsPageControl->IndexOfTabAt(MousePos.X, MousePos.Y);
  9000. // no context menu for "New session tab"
  9001. if ((Index >= 0) && (GetSessionTabTerminal(SessionsPageControl->Pages[Index]) != NULL))
  9002. {
  9003. SessionsPageControl->ActivePageIndex = Index;
  9004. if (DebugAlwaysTrue(SessionTabSwitched()))
  9005. {
  9006. // copied from TControl.WMContextMenu
  9007. SendCancelMode(SessionsPageControl);
  9008. // explicit popup instead of using PopupMenu property
  9009. // to avoid menu to popup somewhere within SessionTabSwitched above,
  9010. // while connecting yet not-connected session and hence
  9011. // allowing an access to commands over not-completelly connected session
  9012. TPoint Point = SessionsPageControl->ClientToScreen(MousePos);
  9013. TPopupMenu * PopupMenu = NonVisualDataModule->SessionsPopup;
  9014. PopupMenu->PopupComponent = SessionsPageControl;
  9015. PopupMenu->Popup(Point.x, Point.y);
  9016. }
  9017. }
  9018. Handled = true;
  9019. }
  9020. //---------------------------------------------------------------------------
  9021. bool __fastcall TCustomScpExplorerForm::CanChangePassword()
  9022. {
  9023. return (Terminal != NULL) && Terminal->Active && Terminal->IsCapable[fcChangePassword];
  9024. }
  9025. //---------------------------------------------------------------------------
  9026. void __fastcall TCustomScpExplorerForm::ChangePassword()
  9027. {
  9028. Configuration->Usage->Inc(L"PasswordChanges");
  9029. std::unique_ptr<TSessionData> ChangePasswordData(Terminal->SessionData->Clone());
  9030. ChangePasswordData->DisableAuthentationsExceptPassword();
  9031. ChangePasswordData->ChangePassword = true;
  9032. std::unique_ptr<TTerminal> ChangePasswordSession(
  9033. Terminal->CreateSecondarySession(L"ChangePassword", ChangePasswordData.get()));
  9034. ChangePasswordSession->AutoReadDirectory = false;
  9035. if (TTerminalManager::Instance()->ConnectTerminal(ChangePasswordSession.get()))
  9036. {
  9037. MessageDialog(MainInstructions(LoadStr(PASSWORD_CHANGED)), qtInformation, qaOK, HELP_CHANGE_PASSWORD);
  9038. }
  9039. else
  9040. {
  9041. Configuration->Usage->Inc(L"PasswordChangesFailed");
  9042. }
  9043. }
  9044. //---------------------------------------------------------------------------
  9045. bool __fastcall TCustomScpExplorerForm::CanPrivateKeyUpload()
  9046. {
  9047. // No nice way to assert SSH2
  9048. return (Terminal != NULL) && Terminal->Active && (Terminal->FSProtocol == cfsSFTP);
  9049. }
  9050. //---------------------------------------------------------------------------
  9051. void __fastcall TCustomScpExplorerForm::PrivateKeyUpload()
  9052. {
  9053. TOperationVisualizer OperationVisualizer;
  9054. UnicodeString FileName = Terminal->SessionData->PublicKeyFile;
  9055. TTerminalManager::Instance()->UploadPublicKey(Terminal, NULL, FileName);
  9056. }
  9057. //---------------------------------------------------------------------------
  9058. void __fastcall TCustomScpExplorerForm::ChangeScale(int M, int D)
  9059. {
  9060. TForm::ChangeScale(M, D);
  9061. int APixelsPerInch = GetControlPixelsPerInch(this);
  9062. HANDLE ResourceModule = GUIConfiguration->ChangeToDefaultResourceModule();
  9063. try
  9064. {
  9065. GlyphsModule->SetPixelsPerInch(APixelsPerInch);
  9066. }
  9067. __finally
  9068. {
  9069. GUIConfiguration->ChangeResourceModule(ResourceModule);
  9070. }
  9071. }
  9072. //---------------------------------------------------------------------------
  9073. void __fastcall TCustomScpExplorerForm::DockContextPopup(TObject * Sender, TPoint & MousePos, bool & /*Handled*/)
  9074. {
  9075. NonVisualDataModule->ControlContextPopup(Sender, MousePos);
  9076. }
  9077. //---------------------------------------------------------------------
  9078. class TPublicControl : public TControl
  9079. {
  9080. friend class TCustomScpExplorerForm;
  9081. };
  9082. //---------------------------------------------------------------------------
  9083. void __fastcall TCustomScpExplorerForm::CopyPopup(TControl * DestControl, TControl * SourceControl)
  9084. {
  9085. static_cast<TPublicControl *>(DestControl)->PopupMenu = static_cast<TPublicControl *>(SourceControl)->PopupMenu;
  9086. static_cast<TPublicControl *>(DestControl)->OnContextPopup = static_cast<TPublicControl *>(SourceControl)->OnContextPopup;
  9087. }
  9088. //---------------------------------------------------------------------------
  9089. void __fastcall TCustomScpExplorerForm::DoBookmarkClick(TOperationSide Side, TObject * Sender)
  9090. {
  9091. TBookmark * Bookmark = DebugNotNull(reinterpret_cast<TBookmark *>(dynamic_cast<TTBCustomItem *>(Sender)->Tag));
  9092. OpenBookmark(Side, Bookmark);
  9093. }
  9094. //---------------------------------------------------------------------------
  9095. void __fastcall TCustomScpExplorerForm::LocalBookmarkClick(TObject * Sender)
  9096. {
  9097. DoBookmarkClick(osLocal, Sender);
  9098. }
  9099. //---------------------------------------------------------------------------
  9100. void __fastcall TCustomScpExplorerForm::RemoteBookmarkClick(TObject * Sender)
  9101. {
  9102. DoBookmarkClick(osRemote, Sender);
  9103. }
  9104. //---------------------------------------------------------------------------
  9105. void __fastcall TCustomScpExplorerForm::CreateOpenDirMenuList(
  9106. TTBCustomItem * Menu, TOperationSide Side, TBookmarkList * BookmarkList)
  9107. {
  9108. TNotifyEvent OnBookmarkClick = (Side == osLocal) ? &LocalBookmarkClick : &RemoteBookmarkClick;
  9109. if (!WinConfiguration->UseLocationProfiles)
  9110. {
  9111. std::unique_ptr<TStringList> Directories(new TStringList());
  9112. Directories->CaseSensitive = (Side == osRemote);
  9113. for (int Index = 0; Index < BookmarkList->Count; Index++)
  9114. {
  9115. TBookmark * Bookmark = BookmarkList->Bookmarks[Index];
  9116. UnicodeString Directory = Bookmark->GetSideDirectory(Side);
  9117. if (!Directory.IsEmpty() && (Directories->IndexOf(Directory) < 0))
  9118. {
  9119. std::unique_ptr<TTBCustomItem> Item(new TTBXItem(Owner));
  9120. Item->Caption = EscapeHotkey(Directory);
  9121. Item->ShortCut = Bookmark->ShortCut;
  9122. Item->OnClick = OnBookmarkClick;
  9123. Item->Tag = reinterpret_cast<int>(Bookmark);
  9124. Directories->Add(Directory);
  9125. Menu->Add(Item.release());
  9126. }
  9127. }
  9128. }
  9129. else
  9130. {
  9131. std::unique_ptr<TStrings> Folders(CreateSortedStringList());
  9132. for (int Index = 0; Index < BookmarkList->Count; Index++)
  9133. {
  9134. TBookmark * Bookmark = BookmarkList->Bookmarks[Index];
  9135. if (!Bookmark->Node.IsEmpty())
  9136. {
  9137. Folders->Add(Bookmark->Node);
  9138. }
  9139. }
  9140. for (int Index = 0; Index < Folders->Count; Index++)
  9141. {
  9142. std::unique_ptr<TTBCustomItem> Item(new TTBXSubmenuItem(Owner));
  9143. Item->Caption = Folders->Strings[Index];
  9144. Item->ImageIndex = NonVisualDataModule->RemoteChangePathAction->ImageIndex;
  9145. Folders->Objects[Index] = Item.get();
  9146. Menu->Add(Item.release());
  9147. }
  9148. for (int Index = 0; Index < BookmarkList->Count; Index++)
  9149. {
  9150. TBookmark * Bookmark = BookmarkList->Bookmarks[Index];
  9151. TTBCustomItem * Parent;
  9152. if (!Bookmark->Node.IsEmpty())
  9153. {
  9154. DebugAssert(Folders->IndexOf(Bookmark->Node) >= 0);
  9155. Parent = dynamic_cast<TTBCustomItem *>(Folders->Objects[Folders->IndexOf(Bookmark->Node)]);
  9156. }
  9157. else
  9158. {
  9159. Parent = Menu;
  9160. }
  9161. std::unique_ptr<TTBCustomItem> Item(new TTBXItem(Owner));
  9162. Item->Caption = Bookmark->Name;
  9163. Item->ShortCut = Bookmark->ShortCut;
  9164. Item->OnClick = OnBookmarkClick;
  9165. Item->Tag = reinterpret_cast<int>(Bookmark);
  9166. if (((Bookmark->Name == Bookmark->Local) || (Bookmark->Name == Bookmark->Remote)) &&
  9167. (Bookmark->Local.IsEmpty() || Bookmark->Remote.IsEmpty()))
  9168. {
  9169. // No hint for location profiles that are actually mere "bookmarks"
  9170. }
  9171. else
  9172. {
  9173. UnicodeString Hint = FORMAT(LoadStrPart(LOCATION_PROFILE_HINT, 1), (Bookmark->Name));
  9174. UnicodeString LongHint;
  9175. if (!Bookmark->Local.IsEmpty())
  9176. {
  9177. AddToList(LongHint, FORMAT(LoadStrPart(LOCATION_PROFILE_HINT, 2), (Bookmark->Local)), L"\n");
  9178. }
  9179. if (!Bookmark->Remote.IsEmpty())
  9180. {
  9181. AddToList(LongHint, FORMAT(LoadStrPart(LOCATION_PROFILE_HINT, 3), (Bookmark->Remote)), L"\n");
  9182. }
  9183. AddToList(Hint, LongHint, L"|");
  9184. Item->Hint = Hint;
  9185. }
  9186. Parent->Add(Item.release());
  9187. }
  9188. }
  9189. }
  9190. //---------------------------------------------------------------------------
  9191. void __fastcall TCustomScpExplorerForm::CreateOpenDirMenu(TTBCustomItem * Menu, TOperationSide Side)
  9192. {
  9193. Menu->Clear();
  9194. CreateOpenDirMenuList(Menu, Side, WinConfiguration->Bookmarks[Terminal->SessionData->SessionKey]);
  9195. AddMenuSeparator(Menu);
  9196. CreateOpenDirMenuList(Menu, Side, WinConfiguration->SharedBookmarks);
  9197. AddMenuSeparator(Menu);
  9198. std::unique_ptr<TTBCustomItem> Item(new TTBXItem(Owner));
  9199. Item->Action = (Side == osLocal) ? NonVisualDataModule->LocalAddBookmarkAction : NonVisualDataModule->RemoteAddBookmarkAction;
  9200. Menu->Add(Item.release());
  9201. }
  9202. //---------------------------------------------------------------------------