CustomScpExplorer.cpp 320 KB

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