CustomScpExplorer.cpp 349 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665
  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 WM_COMPONENT_HIDE (WM_WINSCP_USER + 4)
  54. static const int SessionPanelCount = 4;
  55. //---------------------------------------------------------------------------
  56. class TMutexGuard
  57. {
  58. public:
  59. TMutexGuard(HANDLE AMutex, int Message = MUTEX_RELEASE_TIMEOUT,
  60. int Timeout = 5000)
  61. {
  62. FMutex = NULL;
  63. unsigned long WaitResult = WaitForSingleObject(AMutex, Timeout);
  64. if (WaitResult == WAIT_TIMEOUT)
  65. {
  66. throw Exception(LoadStr(MUTEX_RELEASE_TIMEOUT));
  67. }
  68. else
  69. {
  70. FMutex = AMutex;
  71. }
  72. }
  73. ~TMutexGuard()
  74. {
  75. if (FMutex != NULL)
  76. {
  77. ReleaseMutex(FMutex);
  78. }
  79. }
  80. private:
  81. HANDLE FMutex;
  82. };
  83. //---------------------------------------------------------------------------
  84. class TWindowLock
  85. {
  86. public:
  87. TWindowLock(TCustomScpExplorerForm * Form) :
  88. FForm(Form)
  89. {
  90. FForm->LockWindow();
  91. }
  92. ~TWindowLock()
  93. {
  94. FForm->UnlockWindow();
  95. }
  96. private:
  97. TCustomScpExplorerForm * FForm;
  98. };
  99. //---------------------------------------------------------------------------
  100. class TAutoBatch
  101. {
  102. public:
  103. TAutoBatch(TCustomScpExplorerForm * Form) :
  104. FForm(Form)
  105. {
  106. FForm->BatchStart(FBatchStorage);
  107. }
  108. ~TAutoBatch()
  109. {
  110. FForm->BatchEnd(FBatchStorage);
  111. }
  112. private:
  113. TCustomScpExplorerForm * FForm;
  114. void * FBatchStorage;
  115. };
  116. //---------------------------------------------------------------------------
  117. struct TTransferOperationParam
  118. {
  119. TTransferOperationParam();
  120. UnicodeString TargetDirectory;
  121. bool Temp;
  122. bool DragDrop;
  123. int Options;
  124. TAutoSwitch Queue;
  125. };
  126. //---------------------------------------------------------------------------
  127. TTransferOperationParam::TTransferOperationParam()
  128. {
  129. Temp = false;
  130. DragDrop = false;
  131. Options = 0;
  132. Queue = asAuto;
  133. }
  134. //---------------------------------------------------------------------------
  135. class TTransferPresetNoteData : public TObject
  136. {
  137. public:
  138. UnicodeString Message;
  139. };
  140. //---------------------------------------------------------------------------
  141. class TTerminalNoteData : public TObject
  142. {
  143. public:
  144. TManagedTerminal * Terminal;
  145. };
  146. //---------------------------------------------------------------------------
  147. __fastcall TCustomScpExplorerForm::TCustomScpExplorerForm(TComponent* Owner):
  148. FFormRestored(false),
  149. TForm(Owner)
  150. {
  151. FCurrentSide = osRemote;
  152. FEverShown = false;
  153. FDocks = new TList();
  154. InitControls();
  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. FDDExtMapFile = NULL;
  170. // CreateMutexW keeps failing with ERROR_NOACCESS
  171. FDDExtMutex = CreateMutexA(NULL, false, AnsiString(DRAG_EXT_MUTEX).c_str());
  172. DebugAssert(FDDExtMutex != NULL);
  173. FDDTargetControl = NULL;
  174. FDelayedDeletionTimer = NULL;
  175. FDelayedDeletionList = new TStringList();
  176. FDDFileList = NULL;
  177. FPendingTempSpaceWarn = false;
  178. FCapturedLog = NULL;
  179. FDragDropOperation = false;
  180. memset(&FHistoryMenu, 0, sizeof(FHistoryMenu));
  181. FAllowTransferPresetAutoSelect = true;
  182. FCopyParamDefault = L"";
  183. FSynchronizeController = NULL;
  184. FPendingQueueActionItem = NULL;
  185. FLockLevel = 0;
  186. FLockSuspendLevel = 0;
  187. FDisabledOnLockSuspend = false;
  188. FAlternativeDelete = false;
  189. FTrayIcon = new ::TTrayIcon(0);
  190. FTrayIcon->OnClick = TrayIconClick;
  191. FMaxQueueLength = 0;
  192. FLastContextPopupScreenPoint = TPoint(-1, -1);
  193. FTransferResumeList = NULL;
  194. FMoveToQueue = false;
  195. FStandaloneEditing = false;
  196. FOnFeedSynchronizeError = NULL;
  197. FNeedSession = false;
  198. FDoNotIdleCurrentTerminal = 0;
  199. FIncrementalSearching = 0;
  200. FIncrementalSearchHaveNext = false;
  201. FEditorManager = new TEditorManager();
  202. FEditorManager->OnFileChange = ExecutedFileChanged;
  203. FEditorManager->OnFileReload = ExecutedFileReload;
  204. FEditorManager->OnFileEarlyClosed = ExecutedFileEarlyClosed;
  205. FEditorManager->OnFileUploadComplete = ExecutedFileUploadComplete;
  206. FLocalEditors = new TList();
  207. FQueueStatus = NULL;
  208. FQueueStatusSection = new TCriticalSection();
  209. FQueueStatusInvalidated = false;
  210. FQueueItemInvalidated = false;
  211. FQueueActedItem = NULL;
  212. FQueueController = new TQueueController(QueueView3);
  213. FUserActionTimer = new TTimer(this);
  214. FUserActionTimer->Enabled = false;
  215. FUserActionTimer->Interval = 10;
  216. FUserActionTimer->OnTimer = UserActionTimer;
  217. FNotes = new TStringList();
  218. FNoteTimer = new TTimer(this);
  219. FNoteTimer->Enabled = false;
  220. FNoteTimer->OnTimer = NoteTimer;
  221. FOnNoteClick = NULL;
  222. FOle32Library = LoadLibrary(L"Ole32.dll");
  223. FDragMoveCursor = FOle32Library != NULL ?
  224. LoadCursor(FOle32Library, MAKEINTRESOURCE(2)) : NULL;
  225. UseSystemSettings(this);
  226. TTBXStringList * TransferList = dynamic_cast<TTBXStringList*>(
  227. static_cast<TObject*>(GetComponent(fcTransferList)));
  228. DebugAssert(TransferList != NULL);
  229. FTransferListHoverIndex = -1;
  230. TransferList->OnChange = TransferListChange;
  231. TransferList->OnDrawItem = TransferListDrawItem;
  232. SetSubmenu(dynamic_cast<TTBXCustomItem *>(static_cast<TObject *>(GetComponent(fcColorMenu))));
  233. SetSubmenu(NonVisualDataModule->ColorMenuItem);
  234. UseDesktopFont(SessionsPageControl);
  235. UpdateSessionsPageControlHeight();
  236. UseDesktopFont(RemoteDirView);
  237. UseDesktopFont(RemoteDriveView);
  238. UseDesktopFont(QueueView3);
  239. UseDesktopFont(QueueLabel);
  240. UseDesktopFont(RemoteStatusBar);
  241. reinterpret_cast<TLabel*>(QueueSplitter)->OnDblClick = QueueSplitterDblClick;
  242. QueueSplitter->ShowHint = true;
  243. RemotePanelSplitter->ShowHint = true;
  244. UpdateImages();
  245. FCustomCommandMenu = CreateTBXPopupMenu(this);
  246. FCustomCommandLocalFileList = NULL;
  247. FCustomCommandRemoteFileList = NULL;
  248. FSessionColors = new TPngImageList(this);
  249. FSessionColors->ColorDepth = cd32Bit;
  250. AddFixedSessionImages();
  251. SessionsPageControl->Images = FSessionColors;
  252. QueueLabel->FocusControl = QueueView3;
  253. UpdateQueueLabel();
  254. FRemoteDirViewWasFocused = true;
  255. CreateHiddenWindow();
  256. StartUpdates();
  257. }
  258. //---------------------------------------------------------------------------
  259. __fastcall TCustomScpExplorerForm::~TCustomScpExplorerForm()
  260. {
  261. // this has to be one of the very first things to do
  262. StopUpdateThread();
  263. // This is needed when shuting down Windows only. Otherwise it's already set NULL from Execute()
  264. TTerminalManager::Instance()->ScpExplorer = NULL;
  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. SessionsPageControl->Images = NULL;
  315. // only after clearing Terminal (after DoTerminalListChanged => ... => AddSessionColor is called)
  316. SAFE_DESTROY(FSessionColors);
  317. SAFE_DESTROY(FSessionsDragDropFilesEx);
  318. SAFE_DESTROY(FHistoryMenu[0][0]);
  319. SAFE_DESTROY(FHistoryMenu[0][1]);
  320. SAFE_DESTROY(FHistoryMenu[1][0]);
  321. SAFE_DESTROY(FHistoryMenu[1][1]);
  322. if (FHiddenWindow != NULL)
  323. {
  324. DestroyWindow(FHiddenWindow);
  325. FHiddenWindow = NULL;
  326. }
  327. }
  328. //---------------------------------------------------------------------------
  329. void __fastcall TCustomScpExplorerForm::RefreshPanel(const UnicodeString & Session, const UnicodeString & Path)
  330. {
  331. std::unique_ptr<TSessionData> Data;
  332. if (!Session.IsEmpty())
  333. {
  334. bool DefaultsOnly;
  335. Data.reset(StoredSessions->ParseUrl(Session, NULL, DefaultsOnly));
  336. }
  337. TTerminalManager * Manager = TTerminalManager::Instance();
  338. for (int Index = 0; Index < Manager->Count; Index++)
  339. {
  340. TTerminal * Terminal = Manager->Terminals[Index];
  341. if (Session.IsEmpty() ||
  342. Terminal->SessionData->IsSameSite(Data.get()))
  343. {
  344. if (Path.IsEmpty())
  345. {
  346. Terminal->ClearCaches();
  347. }
  348. else
  349. {
  350. Terminal->DirectoryModified(Path, true);
  351. }
  352. }
  353. }
  354. // We should flag a pending refresh for the background terminals or busy foreground terminals
  355. if ((Terminal != NULL) && Terminal->Active &&
  356. CanCommandLineFromAnotherInstance() &&
  357. (Session.IsEmpty() || Terminal->SessionData->IsSameSite(Data.get())) &&
  358. (Path.IsEmpty() || UnixIsChildPath(Path, Terminal->CurrentDirectory)))
  359. {
  360. Terminal->ReloadDirectory();
  361. }
  362. }
  363. //---------------------------------------------------------------------------
  364. void __fastcall TCustomScpExplorerForm::WMCopyData(TMessage & Message)
  365. {
  366. PCOPYDATASTRUCT CopyData = reinterpret_cast<PCOPYDATASTRUCT>(Message.LParam);
  367. size_t MessageSize = sizeof(TCopyDataMessage);
  368. bool Result = DebugAlwaysTrue(CopyData->cbData == MessageSize);
  369. if (Result)
  370. {
  371. const TCopyDataMessage & Message = *reinterpret_cast<const TCopyDataMessage *>(CopyData->lpData);
  372. Result = (Message.Version == TCopyDataMessage::Version1);
  373. if (Result)
  374. {
  375. switch (Message.Command)
  376. {
  377. case TCopyDataMessage::CommandCanCommandLine:
  378. Result = CanCommandLineFromAnotherInstance();
  379. break;
  380. case TCopyDataMessage::CommandCommandLine:
  381. {
  382. UnicodeString CommandLine(Message.CommandLine);
  383. Result = CommandLineFromAnotherInstance(CommandLine);
  384. }
  385. break;
  386. case TCopyDataMessage::RefreshPanel:
  387. RefreshPanel(Message.Refresh.Session, Message.Refresh.Path);
  388. break;
  389. case TCopyDataMessage::MainWindowCheck:
  390. Result = true;
  391. break;
  392. default:
  393. Result = false;
  394. break;
  395. }
  396. }
  397. }
  398. Message.Result = Result ? 1 : 0;
  399. }
  400. //---------------------------------------------------------------------------
  401. void __fastcall TCustomScpExplorerForm::CreateHiddenWindow()
  402. {
  403. WNDCLASS WindowClass = {0};
  404. WindowClass.lpfnWndProc = DefWindowProc;
  405. WindowClass.hInstance = HInstance;
  406. WindowClass.lpszClassName = HIDDEN_WINDOW_NAME;
  407. FHiddenWindow = NULL;
  408. if (RegisterClass(&WindowClass))
  409. {
  410. FHiddenWindow = CreateWindow(HIDDEN_WINDOW_NAME, L"",
  411. WS_POPUP, 0, 0, 0, 0, 0, 0, HInstance, NULL);
  412. }
  413. }
  414. //---------------------------------------------------------------------------
  415. bool __fastcall TCustomScpExplorerForm::CanConsole()
  416. {
  417. return
  418. (Terminal != NULL) &&
  419. Terminal->Active &&
  420. (Terminal->IsCapable[fcAnyCommand] || Terminal->IsCapable[fcSecondaryShell]);
  421. }
  422. //---------------------------------------------------------------------------
  423. bool __fastcall TCustomScpExplorerForm::CanCommandLineFromAnotherInstance()
  424. {
  425. bool Result = Visible && !NonVisualDataModule->Busy;
  426. return Result;
  427. }
  428. //---------------------------------------------------------------------------
  429. bool __fastcall TCustomScpExplorerForm::CommandLineFromAnotherInstance(
  430. const UnicodeString & CommandLine)
  431. {
  432. TProgramParams Params(CommandLine);
  433. bool Result = CanCommandLineFromAnotherInstance() && DebugAlwaysTrue(Params.ParamCount > 0);
  434. if (Result)
  435. {
  436. NonVisualDataModule->StartBusy();
  437. try
  438. {
  439. // this action is initiated from another process,
  440. // so it's likely that our window is not visible,
  441. // and user won't see what is going on
  442. Application->BringToFront();
  443. // reload sessions as we may be asked to open a session
  444. // just stored by another instance
  445. StoredSessions->Reload();
  446. UnicodeString SessionName = Params.Param[1];
  447. std::unique_ptr<TObjectList> DataList(new TObjectList());
  448. UnicodeString DownloadFile; // unused
  449. try
  450. {
  451. GetLoginData(SessionName, &Params, DataList.get(), DownloadFile, true, this);
  452. }
  453. catch (EAbort &)
  454. {
  455. // Happens when opening session in PuTTY and in other situations,
  456. // in which we do not want to fail WM_COPYDATA, as that would cause master instance
  457. // to process the command-line on its own (or to pass it to yet another instance)
  458. }
  459. if (DataList->Count > 0)
  460. {
  461. TTerminalManager * Manager = TTerminalManager::Instance();
  462. Manager->ActiveTerminal = Manager->NewTerminals(DataList.get());
  463. }
  464. }
  465. __finally
  466. {
  467. NonVisualDataModule->EndBusy();
  468. }
  469. }
  470. return Result;
  471. }
  472. //---------------------------------------------------------------------------
  473. void __fastcall TCustomScpExplorerForm::SetTerminal(TManagedTerminal * value)
  474. {
  475. if (FTerminal != value)
  476. {
  477. TerminalChanging();
  478. DoSetTerminal(value, false);
  479. }
  480. }
  481. //---------------------------------------------------------------------------
  482. void __fastcall TCustomScpExplorerForm::DoSetTerminal(TManagedTerminal * value, bool Replace)
  483. {
  484. FTerminal = value;
  485. bool PrevAllowTransferPresetAutoSelect = FAllowTransferPresetAutoSelect;
  486. FAllowTransferPresetAutoSelect = false;
  487. try
  488. {
  489. TerminalChanged(Replace);
  490. }
  491. __finally
  492. {
  493. FAllowTransferPresetAutoSelect = PrevAllowTransferPresetAutoSelect;
  494. }
  495. if (Terminal != NULL)
  496. {
  497. TransferPresetAutoSelect();
  498. }
  499. }
  500. //---------------------------------------------------------------------------
  501. void __fastcall TCustomScpExplorerForm::ReplaceTerminal(TManagedTerminal * value)
  502. {
  503. DoSetTerminal(value, true);
  504. }
  505. //---------------------------------------------------------------------------
  506. void __fastcall TCustomScpExplorerForm::TerminalChanging()
  507. {
  508. if (FTerminal != NULL)
  509. {
  510. UpdateTerminal(Terminal);
  511. }
  512. }
  513. //---------------------------------------------------------------------------
  514. void __fastcall TCustomScpExplorerForm::TerminalChanged(bool Replaced)
  515. {
  516. if (Terminal != NULL)
  517. {
  518. UpdateSessionColor((TColor)Terminal->StateData->Color);
  519. }
  520. DoTerminalListChanged();
  521. DebugAssert(!IsLocalBrowserMode()); // TODO
  522. if (Replaced)
  523. {
  524. RemoteDirView->ReplaceTerminal(Terminal);
  525. }
  526. else
  527. {
  528. RemoteDirView->Terminal = Terminal;
  529. }
  530. NonVisualDataModule->ResetQueueOnceEmptyOperation();
  531. if (Terminal != NULL)
  532. {
  533. if (Terminal->Active)
  534. {
  535. Terminal->RefreshDirectory();
  536. }
  537. if (WinConfiguration->PreservePanelState)
  538. {
  539. if (Terminal->RemoteExplorerState != NULL)
  540. {
  541. DirView(osRemote)->RestoreState(Terminal->RemoteExplorerState);
  542. }
  543. else
  544. {
  545. DirView(osRemote)->ClearState();
  546. }
  547. }
  548. if (!Replaced && Terminal->Active)
  549. {
  550. InitStatusBar();
  551. }
  552. }
  553. UpdateTransferList();
  554. // Update panels Enable state before refreshing the labels
  555. UpdateControls();
  556. }
  557. //---------------------------------------------------------------------------
  558. void __fastcall TCustomScpExplorerForm::SetQueue(TTerminalQueue * value)
  559. {
  560. if (Queue != value)
  561. {
  562. if (FQueue != NULL)
  563. {
  564. FQueue->OnListUpdate = NULL;
  565. FQueue->OnQueueItemUpdate = NULL;
  566. }
  567. FQueue = value;
  568. if (FQueue != NULL)
  569. {
  570. DebugAssert(FQueue->OnListUpdate == NULL);
  571. FQueue->OnListUpdate = QueueListUpdate;
  572. DebugAssert(FQueue->OnQueueItemUpdate == NULL);
  573. FQueue->OnQueueItemUpdate = QueueItemUpdate;
  574. }
  575. QueueChanged();
  576. }
  577. }
  578. //---------------------------------------------------------------------------
  579. void __fastcall TCustomScpExplorerForm::QueueView3Deletion(TObject * /*Sender*/,
  580. TListItem * Item)
  581. {
  582. if (FQueueActedItem == Item)
  583. {
  584. FQueueActedItem = NULL;
  585. if ((QueueView3->PopupMenu != NULL) &&
  586. (QueueView3->PopupMenu->PopupComponent == QueueView3))
  587. {
  588. // rather "trick", suggested by Jordan on jrsoftware.toolbar2000
  589. ReleaseCapture();
  590. }
  591. }
  592. if (Item->Data == FPendingQueueActionItem)
  593. {
  594. FPendingQueueActionItem = NULL;
  595. }
  596. }
  597. //---------------------------------------------------------------------------
  598. void __fastcall TCustomScpExplorerForm::UpdateQueueStatus(bool QueueChanging)
  599. {
  600. {
  601. TGuard Guard(FQueueStatusSection);
  602. FQueueStatusInvalidated = false;
  603. if (FQueue != NULL)
  604. {
  605. FQueueStatus = FQueue->CreateStatus(FQueueStatus);
  606. }
  607. }
  608. if ((FQueueStatus != NULL) && (FQueueStatus->Count > FMaxQueueLength))
  609. {
  610. FMaxQueueLength = FQueueStatus->Count;
  611. Configuration->Usage->SetMax(L"MaxQueueLength", FMaxQueueLength);
  612. }
  613. FQueueController->UpdateQueueStatus(FQueueStatus);
  614. SetQueueProgress();
  615. UpdateQueueView();
  616. bool IsEmpty = (FQueueStatus == NULL) || (FQueueStatus->Count == 0);
  617. if (IsEmpty && (Terminal != NULL))
  618. {
  619. TOnceDoneOperation OnceDoneOperation =
  620. NonVisualDataModule->CurrentQueueOnceEmptyOperation();
  621. NonVisualDataModule->ResetQueueOnceEmptyOperation();
  622. if ((FQueue != NULL) && !WinConfiguration->EnableQueueByDefault && !QueueChanging)
  623. {
  624. FQueue->Enabled = false;
  625. }
  626. if ((OnceDoneOperation != odoIdle) && !NonVisualDataModule->Busy)
  627. {
  628. Terminal->CloseOnCompletion(OnceDoneOperation, LoadStr(CLOSED_ON_QUEUE_EMPTY));
  629. }
  630. }
  631. }
  632. //---------------------------------------------------------------------------
  633. UnicodeString __fastcall TCustomScpExplorerForm::GetQueueProgressTitle()
  634. {
  635. UnicodeString Result;
  636. if (FQueueStatus != NULL)
  637. {
  638. if (FQueueStatus->IsOnlyOneActiveAndNoPending())
  639. {
  640. TFileOperationProgressType * ProgressData =
  641. FQueueStatus->Items[FQueueStatus->DoneCount]->ProgressData;
  642. if ((ProgressData != NULL) && ProgressData->InProgress)
  643. {
  644. Result = TProgressForm::ProgressStr(NULL, ProgressData);
  645. }
  646. }
  647. else if (FQueueStatus->ActiveAndPendingPrimaryCount > 1)
  648. {
  649. Result = FMTLOAD(PROGRESS_IN_QUEUE, (FQueueStatus->ActiveAndPendingPrimaryCount));
  650. }
  651. }
  652. return Result;
  653. }
  654. //---------------------------------------------------------------------------
  655. void __fastcall TCustomScpExplorerForm::UpdateQueueView()
  656. {
  657. ComponentVisible[fcQueueView] =
  658. (WinConfiguration->QueueView.Show == qvShow) ||
  659. ((WinConfiguration->QueueView.Show == qvHideWhenEmpty) &&
  660. (FQueueStatus != NULL) && (FQueueStatus->Count > 0));
  661. }
  662. //---------------------------------------------------------------------------
  663. void __fastcall TCustomScpExplorerForm::QueueChanged()
  664. {
  665. if (FQueueStatus != NULL)
  666. {
  667. delete FQueueStatus;
  668. FQueueStatus = NULL;
  669. }
  670. UpdateQueueStatus(true);
  671. }
  672. //---------------------------------------------------------------------------
  673. void __fastcall TCustomScpExplorerForm::QueueListUpdate(TTerminalQueue * Queue)
  674. {
  675. if (FQueue == Queue)
  676. {
  677. FQueueStatusInvalidated = true;
  678. }
  679. }
  680. //---------------------------------------------------------------------------
  681. void __fastcall TCustomScpExplorerForm::QueueItemUpdate(TTerminalQueue * Queue,
  682. TQueueItem * Item)
  683. {
  684. if (FQueue == Queue)
  685. {
  686. TGuard Guard(FQueueStatusSection);
  687. // this may be running in parallel with QueueChanged
  688. if (FQueueStatus != NULL)
  689. {
  690. TQueueItemProxy * QueueItem = FQueueStatus->FindByQueueItem(Item);
  691. if ((Item->Status == TQueueItem::qsDone) && (Terminal != NULL))
  692. {
  693. FRefreshLocalDirectory = (QueueItem == NULL) ||
  694. (!QueueItem->Info->ModifiedLocal.IsEmpty());
  695. FRefreshRemoteDirectory = (QueueItem == NULL) ||
  696. (!QueueItem->Info->ModifiedRemote.IsEmpty());
  697. }
  698. if (QueueItem != NULL)
  699. {
  700. QueueItem->UserData = (void*)true;
  701. FQueueItemInvalidated = true;
  702. }
  703. }
  704. }
  705. }
  706. //---------------------------------------------------------------------------
  707. bool __fastcall TCustomScpExplorerForm::IsQueueAutoPopup()
  708. {
  709. // during standalone editing, we have no way to see/control queue,
  710. // so we have to always popup prompts automatically
  711. return FStandaloneEditing || GUIConfiguration->QueueAutoPopup;
  712. }
  713. //---------------------------------------------------------------------------
  714. void __fastcall TCustomScpExplorerForm::RefreshQueueItems()
  715. {
  716. if (FQueueStatus != NULL)
  717. {
  718. bool QueueAutoPopup = IsQueueAutoPopup();
  719. bool NeedRefresh = FQueueController->NeedRefresh();
  720. bool Refresh = FQueueItemInvalidated || NeedRefresh;
  721. FQueueItemInvalidated = false;
  722. int Limit = Refresh ? FQueueStatus->Count : FQueueStatus->DoneAndActiveCount;
  723. bool Updated = false;
  724. TQueueItemProxy * QueueItem;
  725. bool WasUserAction;
  726. for (int Index = 0; Index < Limit; Index++)
  727. {
  728. bool Update = false;
  729. QueueItem = FQueueStatus->Items[Index];
  730. WasUserAction = TQueueItem::IsUserActionStatus(QueueItem->Status);
  731. if (!NonVisualDataModule->Busy && QueueAutoPopup && WasUserAction &&
  732. (FPendingQueueActionItem == NULL))
  733. {
  734. FPendingQueueActionItem = QueueItem;
  735. FUserActionTimer->Enabled = true;
  736. }
  737. if ((bool)QueueItem->UserData)
  738. {
  739. QueueItem->UserData = (void*)false;
  740. QueueItem->Update();
  741. Updated = true;
  742. Update = true;
  743. }
  744. else if (FQueueController->QueueItemNeedsFrequentRefresh(QueueItem) || NeedRefresh)
  745. {
  746. Update = true;
  747. }
  748. if (Update)
  749. {
  750. FQueueController->RefreshQueueItem(QueueItem);
  751. }
  752. }
  753. if (Updated)
  754. {
  755. NonVisualDataModule->UpdateNonVisibleActions();
  756. SetQueueProgress();
  757. }
  758. }
  759. }
  760. //---------------------------------------------------------------------------
  761. void __fastcall TCustomScpExplorerForm::SetTaskbarListProgressState(TBPFLAG Flags)
  762. {
  763. // Could now use Application->MainFormHandle, now that we implement Application->OnGetMainFormHandle
  764. FTaskbarList->SetProgressState(GetMainForm()->Handle, Flags);
  765. }
  766. //---------------------------------------------------------------------------
  767. void __fastcall TCustomScpExplorerForm::SetTaskbarListProgressValue(int Progress)
  768. {
  769. if (Progress >= 0)
  770. {
  771. FTaskbarList->SetProgressValue(GetMainForm()->Handle, Progress, 100);
  772. }
  773. else
  774. {
  775. SetTaskbarListProgressState(TBPF_INDETERMINATE);
  776. }
  777. }
  778. //---------------------------------------------------------------------------
  779. void __fastcall TCustomScpExplorerForm::SetTaskbarListProgressValue(TFileOperationProgressType * ProgressData)
  780. {
  781. int OverallProgress;
  782. // FProgressForm is null when this is called from SetQueueProgress
  783. if ((FProgressForm != NULL) && (FProgressForm->SynchronizeProgress != NULL))
  784. {
  785. OverallProgress = FProgressForm->SynchronizeProgress->Progress(ProgressData);
  786. }
  787. else if (!TFileOperationProgressType::IsIndeterminateOperation(ProgressData->Operation))
  788. {
  789. OverallProgress = ProgressData->OverallProgress();
  790. }
  791. else
  792. {
  793. OverallProgress = -1;
  794. }
  795. SetTaskbarListProgressValue(OverallProgress);
  796. }
  797. //---------------------------------------------------------------------------
  798. void __fastcall TCustomScpExplorerForm::SetQueueProgress()
  799. {
  800. TTerminalManager::Instance()->QueueStatusUpdated();
  801. if ((FTaskbarList != NULL) && (FProgressForm == NULL))
  802. {
  803. if ((FQueueStatus != NULL) && (FQueueStatus->ActiveAndPendingPrimaryCount > 0))
  804. {
  805. if (FQueueStatus->IsOnlyOneActiveAndNoPending())
  806. {
  807. TFileOperationProgressType * ProgressData = NULL;
  808. if (FQueueStatus->Items[FQueueStatus->DoneCount] != NULL)
  809. {
  810. ProgressData = FQueueStatus->Items[FQueueStatus->DoneCount]->ProgressData;
  811. }
  812. if ((ProgressData != NULL) &&
  813. ProgressData->InProgress)
  814. {
  815. SetTaskbarListProgressValue(ProgressData);
  816. }
  817. else
  818. {
  819. SetTaskbarListProgressState(TBPF_NOPROGRESS);
  820. }
  821. }
  822. else
  823. {
  824. SetTaskbarListProgressState(TBPF_INDETERMINATE);
  825. }
  826. }
  827. else
  828. {
  829. SetTaskbarListProgressState(TBPF_NOPROGRESS);
  830. }
  831. }
  832. UpdateQueueLabel();
  833. }
  834. //---------------------------------------------------------------------------
  835. void __fastcall TCustomScpExplorerForm::UpdateQueueLabel()
  836. {
  837. UnicodeString Caption = LoadStr(QUEUE_CAPTION);
  838. if (FQueueStatus != NULL)
  839. {
  840. int ActiveAndPendingPrimaryCount = FQueueStatus->ActiveAndPendingPrimaryCount;
  841. if (ActiveAndPendingPrimaryCount > 0)
  842. {
  843. Caption = FORMAT("%s (%d)", (Caption, ActiveAndPendingPrimaryCount));
  844. }
  845. }
  846. QueueLabel->Caption = Caption;
  847. }
  848. //---------------------------------------------------------------------------
  849. void __fastcall TCustomScpExplorerForm::UpdateTransferList()
  850. {
  851. TTBXStringList * TransferList = dynamic_cast<TTBXStringList*>(
  852. static_cast<TComponent*>(GetComponent(fcTransferList)));
  853. TTBXDropDownItem * TransferDropDown = dynamic_cast<TTBXDropDownItem*>(
  854. static_cast<TComponent*>(GetComponent(fcTransferDropDown)));
  855. TransferList->Strings->BeginUpdate();
  856. try
  857. {
  858. TransferList->Strings->Assign(GUIConfiguration->CopyParamList->NameList);
  859. TransferList->Strings->Insert(0, StripHotkey(LoadStr(COPY_PARAM_DEFAULT)));
  860. TransferList->ItemIndex = GUIConfiguration->CopyParamIndex + 1;
  861. if (FTransferDropDownHint.IsEmpty())
  862. {
  863. FTransferDropDownHint = TransferDropDown->Hint;
  864. }
  865. // this way we get name for "default" settings (COPY_PARAM_DEFAULT)
  866. UnicodeString Name = TransferList->Strings->Strings[TransferList->ItemIndex];
  867. TransferDropDown->Text = StripHotkey(Name);
  868. TransferDropDown->Hint = FORMAT(L"%s|%s:\n%s",
  869. (FTransferDropDownHint, StripHotkey(Name),
  870. GUIConfiguration->CurrentCopyParam.GetInfoStr(L"; ",
  871. FLAGMASK(((Terminal != NULL) && Terminal->Active), Terminal->UsableCopyParamAttrs(0).General))));
  872. // update the label, otherwise when it is updated only on the first draw
  873. // of the list, it is drawn "bold" for some reason
  874. FTransferListHoverIndex = TransferList->ItemIndex;
  875. UpdateTransferLabel();
  876. }
  877. __finally
  878. {
  879. TransferList->Strings->EndUpdate();
  880. }
  881. }
  882. //---------------------------------------------------------------------------
  883. void __fastcall TCustomScpExplorerForm::UpdateCustomCommandsToolbar()
  884. {
  885. TTBXToolbar * Toolbar = dynamic_cast<TTBXToolbar *>(GetComponent(fcCustomCommandsBand));
  886. DebugAssert(Toolbar != NULL);
  887. NonVisualDataModule->UpdateCustomCommandsToolbar(Toolbar);
  888. }
  889. //---------------------------------------------------------------------------
  890. void __fastcall TCustomScpExplorerForm::UpdateActions()
  891. {
  892. TForm::UpdateActions();
  893. if (ComponentVisible[fcCustomCommandsBand])
  894. {
  895. UpdateCustomCommandsToolbar();
  896. }
  897. }
  898. //---------------------------------------------------------------------------
  899. void __fastcall TCustomScpExplorerForm::UpdateSessionsPageControlHeight()
  900. {
  901. SessionsPageControl->Height = SessionsPageControl->GetTabsHeight();
  902. }
  903. //---------------------------------------------------------------------------
  904. void __fastcall TCustomScpExplorerForm::ConfigurationChanged()
  905. {
  906. Color = GetBtnFaceColor();
  907. DebugAssert(Configuration && RemoteDirView);
  908. RemoteDirView->DimmHiddenFiles = WinConfiguration->DimmHiddenFiles;
  909. RemoteDirView->ShowHiddenFiles = WinConfiguration->ShowHiddenFiles;
  910. RemoteDirView->FormatSizeBytes = WinConfiguration->FormatSizeBytes;
  911. RemoteDirView->ShowInaccesibleDirectories = WinConfiguration->ShowInaccesibleDirectories;
  912. RemoteDirView->NaturalOrderNumericalSorting = WinConfiguration->NaturalOrderNumericalSorting;
  913. if (RemoteDirView->RowSelect != WinConfiguration->FullRowSelect)
  914. {
  915. RemoteDirView->RowSelect = WinConfiguration->FullRowSelect;
  916. // selection is not redrawn automatically when RowSelect changes
  917. RemoteDirView->Invalidate();
  918. }
  919. RemoteDriveView->DimmHiddenDirs = WinConfiguration->DimmHiddenFiles;
  920. RemoteDriveView->ShowHiddenDirs = WinConfiguration->ShowHiddenFiles;
  921. RemoteDriveView->ShowInaccesibleDirectories = WinConfiguration->ShowInaccesibleDirectories;
  922. RemoteDriveView->NaturalOrderNumericalSorting = WinConfiguration->NaturalOrderNumericalSorting;
  923. UpdateSessionsPageControlHeight();
  924. SetDockAllowDrag(!WinConfiguration->LockToolbars);
  925. UpdateToolbarDisplayMode();
  926. UpdateTransferList();
  927. if (Terminal != NULL)
  928. {
  929. TransferPresetAutoSelect();
  930. }
  931. UpdateQueueView();
  932. UpdateControls();
  933. // this can be called even before constuctor finishes.
  934. if (FEditorManager != NULL)
  935. {
  936. FEditorManager->ProcessFiles(FileConfigurationChanged, NULL);
  937. }
  938. // this can be called even before constuctor finishes.
  939. if (FLocalEditors != NULL)
  940. {
  941. for (int Index = 0; Index < FLocalEditors->Count; Index++)
  942. {
  943. ReconfigureEditorForm(static_cast<TForm *>(FLocalEditors->Items[Index]));
  944. }
  945. }
  946. if (ComponentVisible[fcCustomCommandsBand])
  947. {
  948. UpdateCustomCommandsToolbar();
  949. }
  950. // show only when keeping queue items forever,
  951. // otherwise, its enough to have in in the context menu
  952. QueueDeleteAllDoneQueueToolbarItem->Visible =
  953. WinConfiguration->QueueKeepDoneItems && (WinConfiguration->QueueKeepDoneItemsFor < 0);
  954. if (FFileColorsCurrent != WinConfiguration->FileColors)
  955. {
  956. TFileColorData::LoadList(WinConfiguration->FileColors, FFileColors);
  957. FFileColorsCurrent = WinConfiguration->FileColors;
  958. FileColorsChanged();
  959. }
  960. }
  961. //---------------------------------------------------------------------------
  962. void __fastcall TCustomScpExplorerForm::DoFileColorsChanged(TCustomDirView * DirView)
  963. {
  964. DirView->OnGetItemColor = FFileColors.empty() ? TDirViewGetItemColorEvent(NULL) : TDirViewGetItemColorEvent(&DirViewGetItemColor);
  965. DirView->Invalidate();
  966. }
  967. //---------------------------------------------------------------------------
  968. void __fastcall TCustomScpExplorerForm::FileColorsChanged()
  969. {
  970. DoFileColorsChanged(RemoteDirView);
  971. }
  972. //---------------------------------------------------------------------------
  973. void __fastcall TCustomScpExplorerForm::FileConfigurationChanged(
  974. const UnicodeString FileName, TEditedFileData * /*Data*/, TObject * Token,
  975. void * /*Arg*/)
  976. {
  977. if (Token != NULL)
  978. {
  979. TForm * Editor = dynamic_cast<TForm*>(Token);
  980. DebugAssert(Editor != NULL);
  981. ReconfigureEditorForm(Editor);
  982. }
  983. }
  984. //---------------------------------------------------------------------------
  985. void __fastcall TCustomScpExplorerForm::EnableDDTransferConfirmation(TObject * /*Sender*/)
  986. {
  987. WinConfiguration->DDTransferConfirmation = asOn;
  988. }
  989. //---------------------------------------------------------------------------
  990. void __fastcall TCustomScpExplorerForm::CopyParamDialogAfter(
  991. TTransferDirection /*Direction*/, bool /*Temp*/, const UnicodeString & /*TargetDirectory*/)
  992. {
  993. // noop
  994. }
  995. //---------------------------------------------------------------------------
  996. bool __fastcall TCustomScpExplorerForm::CopyParamDialog(
  997. TTransferDirection Direction, TTransferType Type, bool Temp,
  998. TStrings * FileList, UnicodeString & TargetDirectory, TGUICopyParamType & CopyParam,
  999. bool Confirm, bool DragDrop, int Options)
  1000. {
  1001. bool Result = true;
  1002. DebugAssert(Terminal && Terminal->Active);
  1003. // these parameters are known in advance
  1004. int Params =
  1005. FLAGMASK(Type == ttMove, cpDelete);
  1006. bool ToTemp = (Temp && (Direction == tdToLocal));
  1007. if (Result && Confirm && WinConfiguration->ConfirmTransferring)
  1008. {
  1009. bool DisableNewerOnly =
  1010. (!Terminal->IsCapable[fcNewerOnlyUpload] && (Direction == tdToRemote)) ||
  1011. ToTemp;
  1012. Options |=
  1013. FLAGMASK(ToTemp, coTemp) |
  1014. FLAGMASK(!Terminal->IsCapable[fsBackgroundTransfers], coDisableQueue) |
  1015. coDoNotShowAgain;
  1016. TUsableCopyParamAttrs UsableCopyParamAttrs = Terminal->UsableCopyParamAttrs(Params);
  1017. int CopyParamAttrs = (Direction == tdToRemote ?
  1018. UsableCopyParamAttrs.Upload : UsableCopyParamAttrs.Download) |
  1019. FLAGMASK(DisableNewerOnly, cpaNoNewerOnly);
  1020. int OutputOptions =
  1021. FLAGMASK(DragDrop && (WinConfiguration->DDTransferConfirmation == asAuto),
  1022. cooDoNotShowAgain);
  1023. std::unique_ptr<TSessionData> SessionData(SessionDataForCode());
  1024. FlashOnBackground(); // Particularly when called from ClipboardFakeCreated
  1025. Result = DoCopyDialog(Direction == tdToRemote, Type == ttMove,
  1026. FileList, TargetDirectory, &CopyParam, Options, CopyParamAttrs, SessionData.get(), &OutputOptions, -1);
  1027. if (Result)
  1028. {
  1029. if (FLAGSET(OutputOptions, cooDoNotShowAgain))
  1030. {
  1031. if (DragDrop)
  1032. {
  1033. if (WinConfiguration->DDTransferConfirmation == asAuto)
  1034. {
  1035. PopupTrayBalloon(NULL, LoadStr(DD_TRANSFER_CONFIRM_OFF2), qtInformation,
  1036. NULL, 0, EnableDDTransferConfirmation, NULL);
  1037. }
  1038. WinConfiguration->DDTransferConfirmation = asOff;
  1039. }
  1040. else
  1041. {
  1042. WinConfiguration->ConfirmTransferring = false;
  1043. }
  1044. }
  1045. else
  1046. {
  1047. // User explicitly unchecked "do not show again",
  1048. // so show him the dialog the next time
  1049. if (DragDrop && (WinConfiguration->DDTransferConfirmation == asAuto))
  1050. {
  1051. WinConfiguration->DDTransferConfirmation = asOn;
  1052. }
  1053. }
  1054. CopyParamDialogAfter(Direction, Temp, TargetDirectory);
  1055. }
  1056. }
  1057. if (Result && CopyParam.Queue && !ToTemp && Terminal->IsCapable[fsBackgroundTransfers])
  1058. {
  1059. Configuration->Usage->Inc(L"TransfersOnBackground");
  1060. // these parameter are known only after transfer dialog
  1061. Params |=
  1062. (CopyParam.QueueNoConfirmation ? cpNoConfirmation : 0);
  1063. AddQueueItem(Queue, Direction, FileList, TargetDirectory, CopyParam, Params);
  1064. Result = false;
  1065. ClearTransferSourceSelection(Direction);
  1066. }
  1067. return Result;
  1068. }
  1069. //---------------------------------------------------------------------------
  1070. void __fastcall TCustomScpExplorerForm::ClearTransferSourceSelection(TTransferDirection Direction)
  1071. {
  1072. if (FOnFileOperationFinished != NULL)
  1073. {
  1074. FOnFileOperationFinished(UnicodeString(), true);
  1075. }
  1076. else
  1077. {
  1078. TOperationSide Side = ((Direction == tdToRemote) ? osLocal : osRemote);
  1079. if (HasDirView[Side])
  1080. {
  1081. DirView(Side)->SelectAll(smNone);
  1082. }
  1083. }
  1084. }
  1085. //---------------------------------------------------------------------------
  1086. void __fastcall TCustomScpExplorerForm::AddQueueItem(
  1087. TTerminalQueue * Queue, TTransferDirection Direction, TStrings * FileList,
  1088. const UnicodeString TargetDirectory, const TGUICopyParamType & CopyParam,
  1089. int Params)
  1090. {
  1091. DebugAssert(Queue != NULL);
  1092. bool SingleFile = false;
  1093. if (FileList->Count == 1)
  1094. {
  1095. if (Direction == tdToRemote)
  1096. {
  1097. UnicodeString FileName = FileList->Strings[0];
  1098. SingleFile = FileExists(ApiPath(FileName));
  1099. }
  1100. else
  1101. {
  1102. TRemoteFile * File = static_cast<TRemoteFile *>(FileList->Objects[0]);
  1103. SingleFile = !File->IsDirectory;
  1104. }
  1105. }
  1106. TQueueItem * QueueItem;
  1107. if (Direction == tdToRemote)
  1108. {
  1109. QueueItem = new TUploadQueueItem(Terminal, FileList, TargetDirectory,
  1110. &CopyParam, Params, SingleFile, CopyParam.QueueParallel);
  1111. }
  1112. else
  1113. {
  1114. QueueItem = new TDownloadQueueItem(Terminal, FileList, TargetDirectory,
  1115. &CopyParam, Params, SingleFile, CopyParam.QueueParallel);
  1116. }
  1117. AddQueueItem(Queue, QueueItem, Terminal);
  1118. }
  1119. //---------------------------------------------------------------------------
  1120. void __fastcall TCustomScpExplorerForm::AddQueueItem(TTerminalQueue * Queue, TQueueItem * QueueItem, TManagedTerminal * Terminal)
  1121. {
  1122. if (Queue->IsEmpty)
  1123. {
  1124. Terminal->QueueOperationStart = Now();
  1125. }
  1126. Queue->AddItem(QueueItem);
  1127. }
  1128. //---------------------------------------------------------------------------
  1129. void __fastcall TCustomScpExplorerForm::RestoreFormParams()
  1130. {
  1131. }
  1132. //---------------------------------------------------------------------------
  1133. void __fastcall TCustomScpExplorerForm::InitControls()
  1134. {
  1135. DebugAssert(FDocks != NULL);
  1136. for (int Index = 0; Index < ComponentCount; Index++)
  1137. {
  1138. TTBDock * Dock = dynamic_cast<TTBDock *>(Components[Index]);
  1139. if ((Dock != NULL) && (Dock->Tag == 0))
  1140. {
  1141. FDocks->Add(Dock);
  1142. }
  1143. }
  1144. CollectItemsWithTextDisplayMode(this);
  1145. }
  1146. //---------------------------------------------------------------------------
  1147. void __fastcall TCustomScpExplorerForm::RestoreParams()
  1148. {
  1149. ConfigurationChanged();
  1150. QueuePanel->Height =
  1151. LoadDimension(WinConfiguration->QueueView.Height, WinConfiguration->QueueView.HeightPixelsPerInch, this);
  1152. LoadListViewStr(QueueView3, WinConfiguration->QueueView.Layout);
  1153. QueueDock->Visible = WinConfiguration->QueueView.ToolBar;
  1154. QueueLabel->Visible = WinConfiguration->QueueView.Label;
  1155. }
  1156. //---------------------------------------------------------------------------
  1157. void __fastcall TCustomScpExplorerForm::StoreParams()
  1158. {
  1159. WinConfiguration->QueueView.Height = QueuePanel->Height;
  1160. WinConfiguration->QueueView.HeightPixelsPerInch = GetControlPixelsPerInch(this);
  1161. WinConfiguration->QueueView.Layout = GetListViewStr(QueueView3);
  1162. WinConfiguration->QueueView.ToolBar = QueueDock->Visible;
  1163. WinConfiguration->QueueView.Label = QueueLabel->Visible;
  1164. }
  1165. //---------------------------------------------------------------------------
  1166. void __fastcall TCustomScpExplorerForm::CreateParams(TCreateParams & Params)
  1167. {
  1168. if (!FFormRestored)
  1169. {
  1170. FFormRestored = true;
  1171. RestoreFormParams();
  1172. }
  1173. TForm::CreateParams(Params);
  1174. }
  1175. //---------------------------------------------------------------------------
  1176. void __fastcall TCustomScpExplorerForm::SetDockAllowDrag(bool value)
  1177. {
  1178. DebugAssert(FDocks != NULL);
  1179. for (int Index = 0; Index < FDocks->Count; Index++)
  1180. {
  1181. static_cast<TTBDock*>(FDocks->Items[Index])->AllowDrag = value;
  1182. }
  1183. }
  1184. //---------------------------------------------------------------------------
  1185. void __fastcall TCustomScpExplorerForm::LoadToolbarsLayoutStr(UnicodeString LayoutStr, UnicodeString ButtonsStr)
  1186. {
  1187. SetDockAllowDrag(true);
  1188. while (!ButtonsStr.IsEmpty())
  1189. {
  1190. UnicodeString S = CutToChar(ButtonsStr, L';', true);
  1191. UnicodeString Name = CutToChar(S, L'=', true);
  1192. for (int ToolbarIndex = 0; ToolbarIndex < ComponentCount; ToolbarIndex++)
  1193. {
  1194. TTBCustomToolbar * Toolbar = dynamic_cast<TTBCustomToolbar *>(Components[ToolbarIndex]);
  1195. if ((Toolbar != NULL) && SameText(Name, GetToolbarKey(Toolbar->Name)))
  1196. {
  1197. while (!S.IsEmpty())
  1198. {
  1199. UnicodeString S2 = CutToChar(S, L',', true);
  1200. UnicodeString Name = CutToChar(S2, L':', true);
  1201. for (int ItemIndex = 0; ItemIndex < Toolbar->Items->Count; ItemIndex++)
  1202. {
  1203. TTBCustomItem * Item = Toolbar->Items->Items[ItemIndex];
  1204. if (SameText(GetToolbarItemName(Item), Name))
  1205. {
  1206. // forward compatibility
  1207. UnicodeString S3 = CutToChar(S2, L':', true);
  1208. bool Visible = StrToIntDef(S3, 0);
  1209. Item->Visible = Visible;
  1210. }
  1211. }
  1212. }
  1213. }
  1214. }
  1215. }
  1216. ::LoadToolbarsLayoutStr(this, LayoutStr);
  1217. SetDockAllowDrag(!WinConfiguration->LockToolbars);
  1218. }
  1219. //---------------------------------------------------------------------------
  1220. UnicodeString __fastcall TCustomScpExplorerForm::GetToolbarsLayoutStr()
  1221. {
  1222. UnicodeString Result;
  1223. SetDockAllowDrag(true);
  1224. Result = ::GetToolbarsLayoutStr(this);
  1225. SetDockAllowDrag(!WinConfiguration->LockToolbars);
  1226. return Result;
  1227. }
  1228. //---------------------------------------------------------------------------
  1229. UnicodeString __fastcall TCustomScpExplorerForm::GetToolbarItemName(TTBCustomItem * Item)
  1230. {
  1231. UnicodeString Result;
  1232. if (Item->Action != NULL)
  1233. {
  1234. Result = Item->Action->Name;
  1235. Result = RemoveSuffix(Result, L"Action");
  1236. }
  1237. else
  1238. {
  1239. Result = Item->Name;
  1240. Result = RemoveSuffix(Result, L"SubmenuItem");
  1241. Result = RemoveSuffix(Result, L"Item");
  1242. }
  1243. return Result;
  1244. }
  1245. //---------------------------------------------------------------------------
  1246. UnicodeString __fastcall TCustomScpExplorerForm::GetToolbarsButtonsStr()
  1247. {
  1248. UnicodeString Result;
  1249. for (int ToolbarIndex = 0; ToolbarIndex < ComponentCount; ToolbarIndex++)
  1250. {
  1251. UnicodeString ToolbarStr;
  1252. TTBCustomToolbar * Toolbar = dynamic_cast<TTBCustomToolbar *>(Components[ToolbarIndex]);
  1253. if ((Toolbar != NULL) && (Toolbar != QueueToolbar))
  1254. {
  1255. for (int ItemIndex = 0; ItemIndex < Toolbar->Items->Count; ItemIndex++)
  1256. {
  1257. TTBCustomItem * Item = Toolbar->Items->Items[ItemIndex];
  1258. // Currently all buttons are visible by default, so we can safely remember all hidden buttons.
  1259. // Once we introduce any buttons that aer hidden by default, we would have to remember their initial state
  1260. // and save the changes here only.
  1261. if (!Item->Visible)
  1262. {
  1263. UnicodeString Name = GetToolbarItemName(Item);
  1264. DebugAssert(Name.Pos(L"TBX") == 0);
  1265. AddToList(ToolbarStr, FORMAT(L"%s:0", (Name)), L",");
  1266. }
  1267. }
  1268. }
  1269. if (!ToolbarStr.IsEmpty())
  1270. {
  1271. AddToList(Result, FORMAT(L"%s=%s", (GetToolbarKey(Toolbar->Name), ToolbarStr)), L";");
  1272. }
  1273. }
  1274. return Result;
  1275. }
  1276. //---------------------------------------------------------------------------
  1277. void __fastcall TCustomScpExplorerForm::CreateProgressForm(TSynchronizeProgress * SynchronizeProgress)
  1278. {
  1279. DebugAssert(FProgressForm == NULL);
  1280. bool AllowSkip = (Terminal != NULL) ? Terminal->IsCapable[fsSkipTransfer] : false;
  1281. FProgressForm = new TProgressForm(Application, (FTransferResumeList != NULL), AllowSkip, SynchronizeProgress);
  1282. FProgressForm->DeleteLocalToRecycleBin =
  1283. (WinConfiguration->DeleteToRecycleBin != FAlternativeDelete);
  1284. FProgressForm->DeleteRemoteToRecycleBin =
  1285. (Terminal != NULL) &&
  1286. (Terminal->SessionData->DeleteToRecycleBin != FAlternativeDelete) &&
  1287. !Terminal->SessionData->RecycleBinPath.IsEmpty();
  1288. // As progress window has delayed show now, we need to lock ourselves, (at least) until then
  1289. LockWindow();
  1290. if (FTaskbarList != NULL)
  1291. {
  1292. // Actually, do not know what hides the progress once the operation finishes
  1293. SetTaskbarListProgressState(TBPF_NORMAL);
  1294. }
  1295. }
  1296. //---------------------------------------------------------------------------
  1297. void __fastcall TCustomScpExplorerForm::DestroyProgressForm()
  1298. {
  1299. UnlockWindow();
  1300. SAFE_DESTROY(FProgressForm);
  1301. SetQueueProgress();
  1302. }
  1303. //---------------------------------------------------------------------------
  1304. void __fastcall TCustomScpExplorerForm::FileOperationProgress(
  1305. TFileOperationProgressType & ProgressData)
  1306. {
  1307. // Download to temporary local directory
  1308. if (FPendingTempSpaceWarn && ProgressData.InProgress && ProgressData.TotalSizeSet)
  1309. {
  1310. bool Continue = true;
  1311. FPendingTempSpaceWarn = false;
  1312. DoWarnLackOfTempSpace(ProgressData.Directory, ProgressData.TotalSize, Continue);
  1313. if (!Continue)
  1314. {
  1315. Abort();
  1316. }
  1317. }
  1318. // operation is being executed and we still didn't show up progress form
  1319. if (ProgressData.InProgress && !FProgressForm)
  1320. {
  1321. CreateProgressForm(NULL);
  1322. DebugAssert(FProgressForm->OnceDoneOperation == odoIdle);
  1323. FProgressForm->OnceDoneOperation = ProgressData.InitialOnceDoneOperation;
  1324. }
  1325. // operation is finished (or terminated), so we hide progress form
  1326. else if (!ProgressData.InProgress && (FProgressForm != NULL) && (FProgressForm->SynchronizeProgress == NULL))
  1327. {
  1328. DestroyProgressForm();
  1329. if (ProgressData.Operation == foCalculateSize)
  1330. {
  1331. // When calculating size before transfer, the abort caused by
  1332. // cancel flag set due to "MoveToQueue" below,
  1333. // is not propagated back to us in ExecuteFileOperation,
  1334. // as it is silently swallowed.
  1335. // So we explicitly (re)throw it here.
  1336. if (FMoveToQueue)
  1337. {
  1338. Abort();
  1339. }
  1340. }
  1341. else
  1342. {
  1343. if ((ProgressData.Cancel == csContinue) &&
  1344. !FAutoOperation)
  1345. {
  1346. OperationComplete(ProgressData.StartTime);
  1347. }
  1348. }
  1349. }
  1350. if (FProgressForm &&
  1351. // Can happen only during synchronization, when ending one of the operations
  1352. ProgressData.InProgress)
  1353. {
  1354. FProgressForm->SetProgressData(ProgressData);
  1355. if (FTaskbarList != NULL)
  1356. {
  1357. DebugAssert(ProgressData.InProgress);
  1358. SetTaskbarListProgressValue(&ProgressData);
  1359. }
  1360. if (FProgressForm->Cancel > csContinue)
  1361. {
  1362. ProgressData.SetCancelAtLeast(FProgressForm->Cancel);
  1363. if (FProgressForm->Cancel == csCancelFile)
  1364. {
  1365. FProgressForm->ClearCancel();
  1366. }
  1367. // cancel cancels even the move
  1368. FMoveToQueue = false;
  1369. }
  1370. else if (FProgressForm->MoveToQueue)
  1371. {
  1372. FMoveToQueue = true;
  1373. ProgressData.SetCancelAtLeast(csCancel);
  1374. }
  1375. if ((FTransferResumeList != NULL) &&
  1376. ProgressData.InProgress &&
  1377. ((ProgressData.Operation == foCopy) || (ProgressData.Operation == foMove)) &&
  1378. !ProgressData.FullFileName.IsEmpty())
  1379. {
  1380. if ((FTransferResumeList->Count == 0) ||
  1381. (FTransferResumeList->Strings[FTransferResumeList->Count - 1] != ProgressData.FullFileName))
  1382. {
  1383. // note that we do not recognize directories from files here
  1384. FTransferResumeList->Add(ProgressData.FullFileName);
  1385. }
  1386. }
  1387. }
  1388. }
  1389. //---------------------------------------------------------------------------
  1390. void __fastcall TCustomScpExplorerForm::OperationComplete(
  1391. const TDateTime & StartTime)
  1392. {
  1393. if (GUIConfiguration->BeepOnFinish &&
  1394. (Now() - StartTime > GUIConfiguration->BeepOnFinishAfter))
  1395. {
  1396. UnicodeString BeepSound = GUIConfiguration->BeepSound;
  1397. DWORD Sound;
  1398. if (!ExtractFileExt(BeepSound).IsEmpty())
  1399. {
  1400. Sound = SND_FILENAME;
  1401. }
  1402. else
  1403. {
  1404. Sound = SND_ALIAS;
  1405. }
  1406. PlaySound(BeepSound.c_str(), NULL, Sound | SND_ASYNC);
  1407. }
  1408. }
  1409. //---------------------------------------------------------------------------
  1410. void __fastcall TCustomScpExplorerForm::OperationProgress(
  1411. TFileOperationProgressType & ProgressData)
  1412. {
  1413. // For example, when calling TFileOperationProgressType::Suspend in TTerminal::QueryReopen,
  1414. // we cannot recurse back to TTerminal::Idle as that may detect another error, calling "suspend" again.
  1415. TAutoNestingCounter Counter(FDoNotIdleCurrentTerminal);
  1416. FileOperationProgress(ProgressData);
  1417. }
  1418. //---------------------------------------------------------------------------
  1419. UnicodeString __fastcall TCustomScpExplorerForm::GetProgressTitle()
  1420. {
  1421. UnicodeString Result;
  1422. if (FProgressForm != NULL)
  1423. {
  1424. Result = FProgressForm->ProgressStr();
  1425. }
  1426. return Result;
  1427. }
  1428. //---------------------------------------------------------------------------
  1429. bool __fastcall TCustomScpExplorerForm::PanelOperation(TOperationSide /*Side*/,
  1430. bool DragDrop)
  1431. {
  1432. return (!DragDrop && (DropSourceControl == NULL)) ||
  1433. (DropSourceControl == DirView(osOther));
  1434. }
  1435. //---------------------------------------------------------------------------
  1436. void __fastcall TCustomScpExplorerForm::DoOperationFinished(
  1437. TFileOperation Operation, TOperationSide Side,
  1438. bool /*Temp*/, const UnicodeString & FileName, bool Success,
  1439. TOnceDoneOperation & OnceDoneOperation)
  1440. {
  1441. if (!FAutoOperation)
  1442. {
  1443. // no selection on "/upload", form servers only as event handler
  1444. // (it is not displayed)
  1445. if (PanelOperation(Side, FDragDropOperation) &&
  1446. Visible && (Operation != foCalculateSize) &&
  1447. (Operation != foGetProperties) && (Operation != foCalculateChecksum))
  1448. {
  1449. if (FOnFileOperationFinished != NULL)
  1450. {
  1451. FOnFileOperationFinished(FileName, Success);
  1452. }
  1453. else
  1454. {
  1455. TCustomDirView * DView = DirView(Side);
  1456. UnicodeString FileNameOnly = ExtractFileName(FileName, !IsSideLocalBrowser(Side));
  1457. TListItem *Item = DView->FindFileItem(FileNameOnly);
  1458. // this can happen when local drive is unplugged in the middle of the operation
  1459. if (Item != NULL)
  1460. {
  1461. if (Success) Item->Selected = false;
  1462. DView->MakeProgressVisible(Item);
  1463. }
  1464. }
  1465. }
  1466. if ((Operation == foCopy) || (Operation == foMove))
  1467. {
  1468. DebugAssert(!IsLocalBrowserMode()); // TODO
  1469. if (Side == osLocal)
  1470. {
  1471. Configuration->Usage->Inc(L"UploadedFiles");
  1472. }
  1473. else
  1474. {
  1475. Configuration->Usage->Inc(L"DownloadedFiles");
  1476. }
  1477. }
  1478. }
  1479. if (Success && (FSynchronizeController != NULL))
  1480. {
  1481. DebugAssert(!IsSideLocalBrowser(Side));
  1482. if (Operation == foCopy)
  1483. {
  1484. DebugAssert(Side == osLocal);
  1485. FSynchronizeController->LogOperation(soUpload, FileName);
  1486. }
  1487. else if (Operation == foDelete)
  1488. {
  1489. DebugAssert(Side == osRemote);
  1490. FSynchronizeController->LogOperation(soDelete, FileName);
  1491. }
  1492. }
  1493. if (FProgressForm)
  1494. {
  1495. OnceDoneOperation = FProgressForm->OnceDoneOperation;
  1496. }
  1497. }
  1498. //---------------------------------------------------------------------------
  1499. void __fastcall TCustomScpExplorerForm::OperationFinished(
  1500. TFileOperation Operation, TOperationSide Side,
  1501. bool Temp, const UnicodeString & FileName, Boolean Success,
  1502. TOnceDoneOperation & OnceDoneOperation)
  1503. {
  1504. TAutoNestingCounter Counter(FDoNotIdleCurrentTerminal);
  1505. DoOperationFinished(Operation, Side, Temp, FileName, Success,
  1506. OnceDoneOperation);
  1507. }
  1508. //---------------------------------------------------------------------------
  1509. bool TCustomScpExplorerForm::IsLocalBrowserMode()
  1510. {
  1511. return false;
  1512. }
  1513. //---------------------------------------------------------------------------
  1514. bool TCustomScpExplorerForm::IsSideLocalBrowser(TOperationSide)
  1515. {
  1516. return false;
  1517. }
  1518. //---------------------------------------------------------------------------
  1519. TCustomDirView * TCustomScpExplorerForm::GetCurrentLocalBrowser()
  1520. {
  1521. return NULL;
  1522. }
  1523. //---------------------------------------------------------------------------
  1524. TCustomDirView * __fastcall TCustomScpExplorerForm::DirView(TOperationSide Side)
  1525. {
  1526. DebugAssert(GetSide(Side) == osRemote);
  1527. DebugUsedParam(Side);
  1528. return RemoteDirView;
  1529. }
  1530. //---------------------------------------------------------------------------
  1531. TCustomDriveView * __fastcall TCustomScpExplorerForm::DriveView(TOperationSide Side)
  1532. {
  1533. DebugAssert(GetSide(Side) == osRemote);
  1534. DebugUsedParam(Side);
  1535. return RemoteDriveView;
  1536. }
  1537. //---------------------------------------------------------------------------
  1538. bool __fastcall TCustomScpExplorerForm::DirViewEnabled(TOperationSide Side)
  1539. {
  1540. DebugAssert(GetSide(Side) == osRemote);
  1541. DebugUsedParam(Side);
  1542. return (Terminal != NULL) && Terminal->Active;
  1543. }
  1544. //---------------------------------------------------------------------------
  1545. bool __fastcall TCustomScpExplorerForm::GetEnableFocusedOperation(
  1546. TOperationSide Side, int FilesOnly)
  1547. {
  1548. return DirView(Side)->AnyFileSelected(true, (FilesOnly != 0), true);
  1549. }
  1550. //---------------------------------------------------------------------------
  1551. bool __fastcall TCustomScpExplorerForm::GetEnableSelectedOperation(
  1552. TOperationSide Side, int FilesOnly)
  1553. {
  1554. return DirView(Side)->AnyFileSelected(false, (FilesOnly != 0), true);
  1555. }
  1556. //---------------------------------------------------------------------------
  1557. struct THistoryItemData
  1558. {
  1559. short int Side;
  1560. short int Index;
  1561. };
  1562. //---------------------------------------------------------------------------
  1563. void __fastcall TCustomScpExplorerForm::HistoryGo(TOperationSide Side, int Index)
  1564. {
  1565. DirView(Side)->HistoryGo(Index);
  1566. }
  1567. //---------------------------------------------------------------------------
  1568. void __fastcall TCustomScpExplorerForm::HistoryItemClick(System::TObject* Sender)
  1569. {
  1570. TTBCustomItem * Item = dynamic_cast<TTBCustomItem *>(Sender);
  1571. THistoryItemData Data = *reinterpret_cast<THistoryItemData*>(&(Item->Tag));
  1572. HistoryGo((TOperationSide)Data.Side, Data.Index);
  1573. }
  1574. //---------------------------------------------------------------------------
  1575. void __fastcall TCustomScpExplorerForm::UpdateHistoryMenu(TOperationSide Side,
  1576. bool Back)
  1577. {
  1578. if (FHistoryMenu[Side == osLocal][Back] != NULL)
  1579. {
  1580. TCustomDirView * DView = DirView(Side);
  1581. TTBXPopupMenu * Menu = FHistoryMenu[Side == osLocal][Back];
  1582. int ICount = Back ? DView->BackCount : DView->ForwardCount;
  1583. if (ICount > 10)
  1584. {
  1585. ICount = 10;
  1586. }
  1587. Menu->Items->Clear();
  1588. THistoryItemData Data;
  1589. Data.Side = Side;
  1590. for (short int i = 1; i <= ICount; i++)
  1591. {
  1592. TTBCustomItem * Item = new TTBXItem(Menu);
  1593. Data.Index = static_cast<short int>(i * (Back ? -1 : 1));
  1594. Item->Caption = MinimizeName(DView->HistoryPath[Data.Index], 50, !IsSideLocalBrowser(Side));
  1595. Item->Hint = DView->HistoryPath[Data.Index];
  1596. DebugAssert(sizeof(int) == sizeof(THistoryItemData));
  1597. Item->Tag = *reinterpret_cast<int*>(&Data);
  1598. Item->OnClick = HistoryItemClick;
  1599. Menu->Items->Add(Item);
  1600. }
  1601. }
  1602. }
  1603. //---------------------------------------------------------------------------
  1604. TTBXPopupMenu * __fastcall TCustomScpExplorerForm::HistoryMenu(
  1605. TOperationSide Side, bool Back)
  1606. {
  1607. if (FHistoryMenu[Side == osLocal][Back] == NULL)
  1608. {
  1609. // workaround
  1610. // In Pascal the size of TTBXPopupMenu is 132, in C++ 136,
  1611. // operator new allocates memory in Pascal code, but calls inline
  1612. // contructor in C++, leading in problems, the function does
  1613. // both in Pascal code
  1614. FHistoryMenu[Side == osLocal][Back] = CreateTBXPopupMenu(this);
  1615. UpdateHistoryMenu(Side, Back);
  1616. }
  1617. return FHistoryMenu[Side == osLocal][Back];
  1618. }
  1619. //---------------------------------------------------------------------------
  1620. void __fastcall TCustomScpExplorerForm::DirViewHistoryChange(
  1621. TCustomDirView *Sender)
  1622. {
  1623. TOperationSide Side = (Sender == DirView(osOther) ? osOther : osLocal);
  1624. UpdateHistoryMenu(Side, true);
  1625. UpdateHistoryMenu(Side, false);
  1626. }
  1627. //---------------------------------------------------------------------------
  1628. bool __fastcall TCustomScpExplorerForm::CustomCommandRemoteAllowed()
  1629. {
  1630. // remote custom commands can be executed only if the server supports shell commands
  1631. // or have secondary shell
  1632. return (FTerminal != NULL) && FTerminal->Active && (FTerminal->IsCapable[fcSecondaryShell] || FTerminal->IsCapable[fcShellAnyCommand]);
  1633. }
  1634. //---------------------------------------------------------------------------
  1635. int __fastcall TCustomScpExplorerForm::CustomCommandState(
  1636. const TCustomCommandType & Command, bool OnFocused, TCustomCommandListType ListType)
  1637. {
  1638. int Result;
  1639. TFileCustomCommand RemoteCustomCommand;
  1640. TLocalCustomCommand LocalCustomCommand;
  1641. TFileCustomCommand * NonInteractiveCustomCommand =
  1642. FLAGCLEAR(Command.Params, ccLocal) ? &RemoteCustomCommand : &LocalCustomCommand;
  1643. TInteractiveCustomCommand InteractiveCustomCommand(NonInteractiveCustomCommand);
  1644. UnicodeString Cmd = InteractiveCustomCommand.Complete(Command.Command, false);
  1645. if (FLAGCLEAR(Command.Params, ccLocal))
  1646. {
  1647. int AllowedState = CustomCommandRemoteAllowed() ? 1 : 0;
  1648. // custom command that does not operate with files can be executed anytime ...
  1649. if (!NonInteractiveCustomCommand->IsFileCommand(Cmd))
  1650. {
  1651. // ... but do not show such command in remote file menu
  1652. if ((ListType == ccltAll) || (ListType == ccltNonFile))
  1653. {
  1654. Result = AllowedState;
  1655. }
  1656. else
  1657. {
  1658. Result = -1;
  1659. }
  1660. }
  1661. else
  1662. {
  1663. if ((ListType == ccltAll) || ((ListType == ccltFile) && !IsSideLocalBrowser(FCurrentSide)))
  1664. {
  1665. Result = (DirView(FCurrentSide)->AnyFileSelected(OnFocused, false, true)) ? AllowedState : 0;
  1666. }
  1667. else
  1668. {
  1669. Result = -1;
  1670. }
  1671. }
  1672. }
  1673. else
  1674. {
  1675. // custom command that does not operate with files can be executed anytime
  1676. if (!NonInteractiveCustomCommand->IsFileCommand(Cmd))
  1677. {
  1678. Result = ((ListType == ccltAll) || (ListType == ccltNonFile)) ? 1 : -1;
  1679. }
  1680. else if (LocalCustomCommand.HasLocalFileName(Cmd))
  1681. {
  1682. if ((ListType == ccltAll) || (ListType == ccltFile))
  1683. {
  1684. // special case is "diff"-style command that can be executed over any side,
  1685. // if we have both sides
  1686. Result =
  1687. // Cannot have focus on both panels, so we have to call AnyFileSelected
  1688. // directly (instead of EnableSelectedOperation) to pass
  1689. // false to FocusedFileOnlyWhenFocused when panel is inactive.
  1690. ((HasDirView[osLocal] && !IsSideLocalBrowser(osRemote) &&
  1691. DirView(osLocal)->AnyFileSelected(false, false, (FCurrentSide == osLocal))) &&
  1692. DirView(osRemote)->AnyFileSelected(false, false, (FCurrentSide == osRemote))) ? 1 : 0;
  1693. }
  1694. else if (ListType == ccltBoth)
  1695. {
  1696. DebugAssert(!IsLocalBrowserMode());
  1697. Result = 1;
  1698. }
  1699. else
  1700. {
  1701. Result = -1;
  1702. }
  1703. }
  1704. else
  1705. {
  1706. if ((ListType == ccltAll) ||
  1707. ((ListType == ccltFile) && (!IsSideLocalBrowser(FCurrentSide) || FLAGCLEAR(Command.Params, ccRemoteFiles))))
  1708. {
  1709. Result = DirView(FCurrentSide)->AnyFileSelected(OnFocused, false, true) ? 1 : 0;
  1710. }
  1711. else
  1712. {
  1713. Result = -1;
  1714. }
  1715. }
  1716. }
  1717. return Result;
  1718. }
  1719. //---------------------------------------------------------------------------
  1720. void __fastcall TCustomScpExplorerForm::RemoteCustomCommand(
  1721. TStrings * FileList, const TCustomCommandType & ACommand,
  1722. const TCustomCommandData & Data, const UnicodeString & CommandCommand)
  1723. {
  1724. if (EnsureCommandSessionFallback(fcShellAnyCommand))
  1725. {
  1726. TRemoteCustomCommand RemoteCustomCommand(Data, Terminal->CurrentDirectory);
  1727. TWinInteractiveCustomCommand InteractiveCustomCommand(
  1728. &RemoteCustomCommand, ACommand.Name, ACommand.HomePage);
  1729. UnicodeString Command = InteractiveCustomCommand.Complete(CommandCommand, false);
  1730. Configuration->Usage->Inc(L"RemoteCustomCommandRuns2");
  1731. bool Capture =
  1732. FLAGSET(ACommand.Params, ccShowResults) ||
  1733. FLAGSET(ACommand.Params, ccCopyResults) ||
  1734. FLAGSET(ACommand.Params, ccShowResultsInMsgBox);
  1735. TCaptureOutputEvent OutputEvent = NULL;
  1736. DebugAssert(FCapturedLog == NULL);
  1737. if (Capture)
  1738. {
  1739. FCapturedLog = new TStringList();
  1740. OutputEvent = TerminalCaptureLog;
  1741. }
  1742. try
  1743. {
  1744. if (!RemoteCustomCommand.IsFileCommand(Command))
  1745. {
  1746. Terminal->AnyCommand(RemoteCustomCommand.Complete(Command, true),
  1747. OutputEvent);
  1748. }
  1749. else
  1750. {
  1751. Terminal->CustomCommandOnFiles(Command, ACommand.Params, FileList, OutputEvent);
  1752. }
  1753. if ((FCapturedLog != NULL) && (FCapturedLog->Count > 0))
  1754. {
  1755. if (FLAGSET(ACommand.Params, ccCopyResults))
  1756. {
  1757. CopyToClipboard(FCapturedLog);
  1758. }
  1759. if (FLAGSET(ACommand.Params, ccShowResults))
  1760. {
  1761. DoConsoleDialog(Terminal, L"", FCapturedLog);
  1762. }
  1763. else if (FLAGSET(ACommand.Params, ccShowResultsInMsgBox))
  1764. {
  1765. MessageDialog(FCapturedLog->Text, qtInformation, qaOK);
  1766. }
  1767. }
  1768. }
  1769. __finally
  1770. {
  1771. SAFE_DESTROY(FCapturedLog);
  1772. }
  1773. }
  1774. }
  1775. //---------------------------------------------------------------------------
  1776. void __fastcall TCustomScpExplorerForm::LocalCustomCommandPure(
  1777. TStrings * FileList, const TCustomCommandType & ACommand, const UnicodeString & Command, TStrings * ALocalFileList,
  1778. const TCustomCommandData & Data, bool LocalFileCommand, bool FileListCommand, UnicodeString * POutput)
  1779. {
  1780. DebugAssert(!IsLocalBrowserMode());
  1781. TStrings * LocalFileList = NULL;
  1782. TStrings * RemoteFileList = NULL;
  1783. TStrings * RemoteFileListFull = NULL;
  1784. try
  1785. {
  1786. if (LocalFileCommand)
  1787. {
  1788. if (ALocalFileList == NULL)
  1789. {
  1790. DebugAssert(HasDirView[osLocal]);
  1791. // Cannot have focus on both panels, so we have to call AnyFileSelected
  1792. // directly (instead of EnableSelectedOperation) to pass
  1793. // false to FocusedFileOnlyWhenFocused
  1794. DebugAssert(DirView(osLocal)->AnyFileSelected(false, false, false));
  1795. LocalFileList = DirView(osLocal)->CreateFileList(false, true, NULL);
  1796. }
  1797. else
  1798. {
  1799. LocalFileList = ALocalFileList;
  1800. }
  1801. if (FileListCommand)
  1802. {
  1803. if (LocalFileList->Count != 1)
  1804. {
  1805. throw Exception(LoadStr(CUSTOM_COMMAND_SELECTED_UNMATCH1));
  1806. }
  1807. }
  1808. else
  1809. {
  1810. if ((LocalFileList->Count != 1) &&
  1811. (FileList->Count != 1) &&
  1812. (LocalFileList->Count != FileList->Count))
  1813. {
  1814. throw Exception(LoadStr(CUSTOM_COMMAND_SELECTED_UNMATCH));
  1815. }
  1816. }
  1817. }
  1818. UnicodeString RootTempDir;
  1819. UnicodeString TempDir;
  1820. bool RemoteFiles = FLAGSET(ACommand.Params, ccRemoteFiles);
  1821. if (!RemoteFiles)
  1822. {
  1823. TemporarilyDownloadFiles(FileList, false, RootTempDir, TempDir, false, false, true);
  1824. }
  1825. try
  1826. {
  1827. TDateTimes RemoteFileTimes;
  1828. if (RemoteFiles)
  1829. {
  1830. RemoteFileList = FileList;
  1831. }
  1832. else
  1833. {
  1834. RemoteFileList = new TStringList();
  1835. TMakeLocalFileListParams MakeFileListParam;
  1836. MakeFileListParam.FileList = RemoteFileList;
  1837. MakeFileListParam.FileTimes = &RemoteFileTimes;
  1838. MakeFileListParam.IncludeDirs = FLAGSET(ACommand.Params, ccApplyToDirectories);
  1839. MakeFileListParam.Recursive =
  1840. FLAGSET(ACommand.Params, ccRecursive) && !FileListCommand;
  1841. ProcessLocalDirectory(TempDir, Terminal->MakeLocalFileList, &MakeFileListParam);
  1842. if (!MakeFileListParam.Recursive)
  1843. {
  1844. RemoteFileTimes.clear();
  1845. RemoteFileListFull = new TStringList();
  1846. MakeFileListParam.FileList = RemoteFileListFull;
  1847. MakeFileListParam.Recursive = true;
  1848. MakeFileListParam.IncludeDirs = false;
  1849. ProcessLocalDirectory(TempDir, Terminal->MakeLocalFileList, &MakeFileListParam);
  1850. }
  1851. else
  1852. {
  1853. RemoteFileListFull = RemoteFileList;
  1854. }
  1855. }
  1856. bool NonBlocking = FileListCommand && RemoteFiles && !POutput;
  1857. TFileOperationProgressType Progress(&OperationProgress, &OperationFinished);
  1858. if (!NonBlocking)
  1859. {
  1860. int Count;
  1861. if (FileListCommand)
  1862. {
  1863. Count = 1;
  1864. }
  1865. else if (LocalFileCommand)
  1866. {
  1867. Count = std::max(LocalFileList->Count, RemoteFileList->Count);
  1868. }
  1869. else
  1870. {
  1871. Count = RemoteFileList->Count;
  1872. }
  1873. Progress.Start(foCustomCommand, osRemote, Count);
  1874. DebugAssert(FProgressForm != NULL);
  1875. FProgressForm->ReadOnly = true;
  1876. }
  1877. try
  1878. {
  1879. if (FileListCommand)
  1880. {
  1881. UnicodeString LocalFile;
  1882. // MakeFileList does not delimit filenames
  1883. UnicodeString FileList = MakeFileList(RemoteFileList);
  1884. if (LocalFileCommand)
  1885. {
  1886. DebugAssert(LocalFileList->Count == 1);
  1887. LocalFile = LocalFileList->Strings[0];
  1888. }
  1889. TLocalCustomCommand CustomCommand(Data,
  1890. Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(), L"", LocalFile, FileList);
  1891. UnicodeString ShellCommand = CustomCommand.Complete(Command, true);
  1892. if (NonBlocking)
  1893. {
  1894. DebugAssert(!POutput);
  1895. ExecuteShellChecked(ShellCommand);
  1896. }
  1897. else
  1898. {
  1899. ExecuteProcessCheckedAndWait(ShellCommand, HelpKeyword, POutput);
  1900. }
  1901. }
  1902. else if (LocalFileCommand)
  1903. {
  1904. if (LocalFileList->Count == 1)
  1905. {
  1906. UnicodeString LocalFile = LocalFileList->Strings[0];
  1907. for (int Index = 0; Index < RemoteFileList->Count; Index++)
  1908. {
  1909. UnicodeString FileName = RemoteFileList->Strings[Index];
  1910. UnicodeString FileNameForProgress = RemoteFiles ? FileName : ExtractFileName(FileName);
  1911. Progress.SetFile(FileNameForProgress);
  1912. TLocalCustomCommand CustomCommand(Data,
  1913. Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(), FileName, LocalFile, L"");
  1914. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  1915. TOnceDoneOperation OnceDoneOperation;
  1916. // Do not unselect anything, as it won't work with two panels anyway
  1917. Progress.Finish(UnicodeString(), true, OnceDoneOperation);
  1918. }
  1919. }
  1920. else if (RemoteFileList->Count == 1)
  1921. {
  1922. UnicodeString FileName = RemoteFileList->Strings[0];
  1923. for (int Index = 0; Index < LocalFileList->Count; Index++)
  1924. {
  1925. UnicodeString LocalFileName = LocalFileList->Strings[Index];
  1926. UnicodeString LocalFileNameOnly = ExtractFileName(LocalFileName);
  1927. Progress.SetFile(LocalFileNameOnly);
  1928. TLocalCustomCommand CustomCommand(
  1929. Data, Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(),
  1930. FileName, LocalFileName, L"");
  1931. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  1932. TOnceDoneOperation OnceDoneOperation;
  1933. // Do not unselect anything, as it won't work with two panels anyway
  1934. Progress.Finish(UnicodeString(), true, OnceDoneOperation);
  1935. }
  1936. }
  1937. else
  1938. {
  1939. if (LocalFileList->Count != RemoteFileList->Count)
  1940. {
  1941. throw Exception(LoadStr(CUSTOM_COMMAND_PAIRS_DOWNLOAD_FAILED));
  1942. }
  1943. for (int Index = 0; Index < LocalFileList->Count; Index++)
  1944. {
  1945. UnicodeString FileName = RemoteFileList->Strings[Index];
  1946. UnicodeString LocalFileName = LocalFileList->Strings[Index];
  1947. UnicodeString LocalFileNameOnly = ExtractFileName(LocalFileName);
  1948. Progress.SetFile(LocalFileNameOnly); // sic
  1949. TLocalCustomCommand CustomCommand(
  1950. Data, Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(),
  1951. FileName, LocalFileName, L"");
  1952. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  1953. TOnceDoneOperation OnceDoneOperation;
  1954. // Do not unselect anything, as it won't work with two panels anyway
  1955. Progress.Finish(UnicodeString(), true, OnceDoneOperation);
  1956. }
  1957. }
  1958. }
  1959. else
  1960. {
  1961. for (int Index = 0; Index < RemoteFileList->Count; Index++)
  1962. {
  1963. UnicodeString FileName = RemoteFileList->Strings[Index];
  1964. UnicodeString FileNameForProgress = RemoteFiles ? FileName : ExtractFileName(FileName);
  1965. Progress.SetFile(FileNameForProgress);
  1966. TLocalCustomCommand CustomCommand(Data,
  1967. Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(),
  1968. FileName, L"", L"");
  1969. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  1970. TOnceDoneOperation OnceDoneOperation;
  1971. Progress.Finish(FileNameForProgress, true, OnceDoneOperation);
  1972. }
  1973. }
  1974. }
  1975. __finally
  1976. {
  1977. if (!NonBlocking)
  1978. {
  1979. Progress.Stop();
  1980. }
  1981. }
  1982. DebugAssert(!FAutoOperation);
  1983. if (!RemoteFiles)
  1984. {
  1985. TempDir = IncludeTrailingBackslash(TempDir);
  1986. for (int Index = 0; Index < RemoteFileListFull->Count; Index++)
  1987. {
  1988. UnicodeString FileName = RemoteFileListFull->Strings[Index];
  1989. if (DebugAlwaysTrue(SameText(TempDir, FileName.SubString(1, TempDir.Length()))) &&
  1990. // Skip directories, process only nested files.
  1991. // The check is redundant as FileAge fails for directories anyway.
  1992. !DirectoryExists(FileName))
  1993. {
  1994. UnicodeString RemoteDir =
  1995. UnixExtractFileDir(
  1996. UnixIncludeTrailingBackslash(FTerminal->CurrentDirectory) +
  1997. ToUnixPath(FileName.SubString(TempDir.Length() + 1, FileName.Length() - TempDir.Length())));
  1998. TDateTime NewTime;
  1999. if (FileAge(FileName, NewTime) &&
  2000. (NewTime != RemoteFileTimes[Index]))
  2001. {
  2002. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  2003. TemporaryFileCopyParam(CopyParam);
  2004. CopyParam.FileMask = L"";
  2005. FAutoOperation = true;
  2006. std::unique_ptr<TStrings> TemporaryFilesList(new TStringList());
  2007. TemporaryFilesList->Add(FileName);
  2008. FTerminal->CopyToRemote(TemporaryFilesList.get(), RemoteDir, &CopyParam, cpTemporary, NULL);
  2009. }
  2010. }
  2011. }
  2012. }
  2013. }
  2014. __finally
  2015. {
  2016. FAutoOperation = false;
  2017. if (!RootTempDir.IsEmpty() && DebugAlwaysTrue(!RemoteFiles))
  2018. {
  2019. RecursiveDeleteFile(ExcludeTrailingBackslash(RootTempDir), false);
  2020. }
  2021. }
  2022. }
  2023. __finally
  2024. {
  2025. if (RemoteFileList != FileList)
  2026. {
  2027. delete RemoteFileList;
  2028. }
  2029. if (LocalFileList != ALocalFileList)
  2030. {
  2031. delete LocalFileList;
  2032. }
  2033. if (RemoteFileListFull != RemoteFileList)
  2034. {
  2035. delete RemoteFileListFull;
  2036. }
  2037. }
  2038. }
  2039. //---------------------------------------------------------------------------
  2040. void __fastcall TCustomScpExplorerForm::LocalCustomCommandWithLocalFiles(
  2041. const TCustomCommandType & ACommand, const UnicodeString & Command, const TCustomCommandData & Data,
  2042. bool FileListCommand, UnicodeString * POutput)
  2043. {
  2044. std::unique_ptr<TStrings> SelectedFileList(DirView(osLocal)->CreateFileList(false, true, NULL));
  2045. std::unique_ptr<TStrings> LocalFileList(new TStringList());
  2046. for (int Index = 0; Index < SelectedFileList->Count; Index++)
  2047. {
  2048. UnicodeString FileName = SelectedFileList->Strings[Index];
  2049. if (DirectoryExists(FileName))
  2050. {
  2051. if (FLAGSET(ACommand.Params, ccApplyToDirectories))
  2052. {
  2053. LocalFileList->Add(FileName);
  2054. }
  2055. if (FLAGSET(ACommand.Params, ccRecursive))
  2056. {
  2057. TMakeLocalFileListParams MakeFileListParam;
  2058. MakeFileListParam.FileList = LocalFileList.get();
  2059. MakeFileListParam.FileTimes = NULL;
  2060. MakeFileListParam.IncludeDirs = FLAGSET(ACommand.Params, ccApplyToDirectories);
  2061. MakeFileListParam.Recursive = true;
  2062. ProcessLocalDirectory(FileName, Terminal->MakeLocalFileList, &MakeFileListParam);
  2063. }
  2064. }
  2065. else
  2066. {
  2067. LocalFileList->Add(FileName);
  2068. }
  2069. }
  2070. UnicodeString RemotePath;
  2071. if (Terminal != NULL)
  2072. {
  2073. RemotePath = Terminal->CurrentDirectory;
  2074. }
  2075. if (FileListCommand)
  2076. {
  2077. UnicodeString FileList = MakeFileList(LocalFileList.get());
  2078. TLocalCustomCommand CustomCommand(
  2079. Data, RemotePath, DefaultDownloadTargetDirectory(),
  2080. L"", L"", FileList);
  2081. ExecuteProcessChecked(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  2082. }
  2083. else
  2084. {
  2085. TFileOperationProgressType Progress(&OperationProgress, &OperationFinished);
  2086. Progress.Start(foCustomCommand, osLocal, LocalFileList->Count);
  2087. DebugAssert(FProgressForm != NULL);
  2088. FProgressForm->ReadOnly = true;
  2089. try
  2090. {
  2091. for (int Index = 0; Index < LocalFileList->Count; Index++)
  2092. {
  2093. UnicodeString FileName = LocalFileList->Strings[Index];
  2094. Progress.SetFile(FileName);
  2095. TLocalCustomCommand CustomCommand(
  2096. Data, RemotePath, DefaultDownloadTargetDirectory(),
  2097. FileName, L"", L"");
  2098. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  2099. TOnceDoneOperation OnceDoneOperation;
  2100. Progress.Finish(FileName, true, OnceDoneOperation);
  2101. }
  2102. }
  2103. __finally
  2104. {
  2105. Progress.Stop();
  2106. }
  2107. }
  2108. }
  2109. //---------------------------------------------------------------------------
  2110. void __fastcall TCustomScpExplorerForm::LocalCustomCommand(TStrings * FileList,
  2111. const TCustomCommandType & ACommand, TStrings * ALocalFileList,
  2112. const TCustomCommandData & Data, const UnicodeString & CommandCommand)
  2113. {
  2114. UnicodeString RemotePath = (Terminal != NULL) ? Terminal->CurrentDirectory : UnicodeString();
  2115. TLocalCustomCommand LocalCustomCommand(Data, RemotePath, DefaultDownloadTargetDirectory());
  2116. TWinInteractiveCustomCommand InteractiveCustomCommand(
  2117. &LocalCustomCommand, ACommand.Name, ACommand.HomePage);
  2118. UnicodeString Command = InteractiveCustomCommand.Complete(CommandCommand, false);
  2119. bool FileListCommand = LocalCustomCommand.IsFileListCommand(Command);
  2120. bool LocalFileCommand = LocalCustomCommand.HasLocalFileName(Command);
  2121. Configuration->Usage->Inc(L"LocalCustomCommandRuns2");
  2122. std::unique_ptr<UnicodeString> POutput;
  2123. if (FLAGSET(ACommand.Params, ccShowResultsInMsgBox) ||
  2124. FLAGSET(ACommand.Params, ccCopyResults))
  2125. {
  2126. POutput.reset(new UnicodeString());
  2127. }
  2128. if (!LocalCustomCommand.IsFileCommand(Command))
  2129. {
  2130. ExecuteProcessChecked(LocalCustomCommand.Complete(Command, true), HelpKeyword, POutput.get());
  2131. }
  2132. // remote files?
  2133. else if ((FCurrentSide == osRemote) || LocalFileCommand)
  2134. {
  2135. LocalCustomCommandPure(FileList, ACommand, Command, ALocalFileList, Data, LocalFileCommand, FileListCommand, POutput.get());
  2136. }
  2137. // local files
  2138. else
  2139. {
  2140. LocalCustomCommandWithLocalFiles(ACommand, Command, Data, FileListCommand, POutput.get());
  2141. }
  2142. if (POutput.get() != NULL)
  2143. {
  2144. // If the output is single-line, we do not want the trailing CRLF, as the CopyToClipboard(TStrings *) does
  2145. int P = POutput->Pos(sLineBreak);
  2146. if (P == POutput->Length() - static_cast<int>(strlen(sLineBreak)) + 1)
  2147. {
  2148. POutput->SetLength(P - 1);
  2149. }
  2150. // Copy even empty output to clipboard
  2151. if (FLAGSET(ACommand.Params, ccCopyResults))
  2152. {
  2153. CopyToClipboard(*POutput);
  2154. }
  2155. // But do not show an empty message box.
  2156. // This way the ShowResultsInMsgBox can be used to suppress a console window of commands with no output
  2157. if (FLAGSET(ACommand.Params, ccShowResultsInMsgBox) &&
  2158. !POutput->IsEmpty())
  2159. {
  2160. TClipboardHandler ClipboardHandler;
  2161. ClipboardHandler.Text = *POutput;
  2162. TMessageParams Params;
  2163. TQueryButtonAlias Aliases[1];
  2164. Aliases[0].Button = qaRetry;
  2165. Aliases[0].Alias = LoadStr(EDIT_COPY);
  2166. Aliases[0].OnSubmit = &ClipboardHandler.Copy;
  2167. Params.Aliases = Aliases;
  2168. Params.AliasesCount = LENOF(Aliases);
  2169. MessageDialog(*POutput, qtInformation, qaOK | qaRetry, HelpKeyword, &Params);
  2170. }
  2171. }
  2172. }
  2173. //---------------------------------------------------------------------------
  2174. void __fastcall TCustomScpExplorerForm::CustomCommand(TStrings * FileList,
  2175. const TCustomCommandType & ACommand, TStrings * ALocalFileList)
  2176. {
  2177. TCustomCommandData Data;
  2178. UnicodeString Site;
  2179. UnicodeString RemotePath;
  2180. if (Terminal != NULL)
  2181. {
  2182. Data = TCustomCommandData(Terminal);
  2183. Site = Terminal->SessionData->SessionKey;
  2184. RemotePath = Terminal->CurrentDirectory;
  2185. }
  2186. UnicodeString HelpKeyword = ACommand.HomePage;
  2187. std::unique_ptr<TStrings> CustomCommandOptions(CloneStrings(WinConfiguration->CustomCommandOptions));
  2188. if (ACommand.AnyOptionWithFlag(TCustomCommandType::ofRun))
  2189. {
  2190. std::unique_ptr<TCustomCommand> CustomCommandForOptions;
  2191. if (FLAGCLEAR(ACommand.Params, ccLocal))
  2192. {
  2193. CustomCommandForOptions.reset(new TRemoteCustomCommand(Data, RemotePath));
  2194. }
  2195. else
  2196. {
  2197. CustomCommandForOptions.reset(new TLocalCustomCommand(Data, RemotePath, DefaultDownloadTargetDirectory()));
  2198. }
  2199. if (!DoCustomCommandOptionsDialog(
  2200. &ACommand, CustomCommandOptions.get(), NULL, TCustomCommandType::ofRun, CustomCommandForOptions.get(), Site, NULL))
  2201. {
  2202. Abort();
  2203. }
  2204. }
  2205. UnicodeString CommandCommand = ACommand.GetCommandWithExpandedOptions(CustomCommandOptions.get(), Site);
  2206. if (FLAGCLEAR(ACommand.Params, ccLocal))
  2207. {
  2208. RemoteCustomCommand(FileList, ACommand, Data, CommandCommand);
  2209. }
  2210. else
  2211. {
  2212. LocalCustomCommand(FileList, ACommand, ALocalFileList, Data, CommandCommand);
  2213. }
  2214. }
  2215. //---------------------------------------------------------------------------
  2216. void __fastcall TCustomScpExplorerForm::BothCustomCommand(
  2217. const TCustomCommandType & Command)
  2218. {
  2219. DebugAssert(FCustomCommandLocalFileList != NULL);
  2220. DebugAssert(FCustomCommandRemoteFileList != NULL);
  2221. DebugAssert(FCustomCommandLocalFileList->Count == FCustomCommandRemoteFileList->Count);
  2222. TStrings * LocalFileList = new TStringList();
  2223. TStrings * RemoteFileList = new TStringList();
  2224. try
  2225. {
  2226. for (int Index = 0; Index < FCustomCommandLocalFileList->Count; Index++)
  2227. {
  2228. LocalFileList->Clear();
  2229. LocalFileList->AddObject(
  2230. FCustomCommandLocalFileList->Strings[Index],
  2231. FCustomCommandLocalFileList->Objects[Index]);
  2232. RemoteFileList->Clear();
  2233. RemoteFileList->AddObject(
  2234. FCustomCommandRemoteFileList->Strings[Index],
  2235. FCustomCommandRemoteFileList->Objects[Index]);
  2236. CustomCommand(RemoteFileList, Command, LocalFileList);
  2237. }
  2238. }
  2239. __finally
  2240. {
  2241. delete LocalFileList;
  2242. delete RemoteFileList;
  2243. }
  2244. }
  2245. //---------------------------------------------------------------------------
  2246. void __fastcall TCustomScpExplorerForm::CustomCommandMenu(
  2247. TAction * Action, TStrings * LocalFileList, TStrings * RemoteFileList)
  2248. {
  2249. delete FCustomCommandLocalFileList;
  2250. delete FCustomCommandRemoteFileList;
  2251. // takeover ownership,
  2252. // the lists must survive the MenuPopup as OnClick occurs only after it exits
  2253. FCustomCommandLocalFileList = LocalFileList;
  2254. FCustomCommandRemoteFileList = RemoteFileList;
  2255. TButton * Button = dynamic_cast<TButton *>(Action->ActionComponent);
  2256. if (Button != NULL)
  2257. {
  2258. FCustomCommandMenu->Items->Clear();
  2259. NonVisualDataModule->CreateCustomCommandsMenu(FCustomCommandMenu->Items, false, false, ccltBoth);
  2260. MenuPopup(FCustomCommandMenu, Button);
  2261. }
  2262. else
  2263. {
  2264. NonVisualDataModule->CreateCustomCommandsMenu(Action, false, ccltBoth);
  2265. }
  2266. }
  2267. //---------------------------------------------------------------------------
  2268. void __fastcall TCustomScpExplorerForm::TerminalCaptureLog(
  2269. const UnicodeString & AddedLine, TCaptureOutputType OutputType)
  2270. {
  2271. DebugAssert(FCapturedLog != NULL);
  2272. if ((OutputType == cotOutput) || (OutputType == cotError))
  2273. {
  2274. FCapturedLog->Add(AddedLine);
  2275. }
  2276. }
  2277. //---------------------------------------------------------------------------
  2278. bool __fastcall TCustomScpExplorerForm::IsFileControl(TObject * Control,
  2279. TOperationSide Side)
  2280. {
  2281. return
  2282. (Side == osOther) &&
  2283. ((Control == DirView(osOther)) || (Control == DriveView(osOther)));
  2284. }
  2285. //---------------------------------------------------------------------------
  2286. void __fastcall TCustomScpExplorerForm::DirViewContextPopupDefaultItem(
  2287. TOperationSide Side, TTBXCustomItem * Item, TDoubleClickAction DoubleClickAction)
  2288. {
  2289. TTBItemOptions O;
  2290. O = Item->Options;
  2291. TCustomDirView * DView = DirView(Side);
  2292. if ((DView->ItemFocused != NULL) &&
  2293. (WinConfiguration->DoubleClickAction == DoubleClickAction) &&
  2294. // when resolving links is disabled, default action is to enter the directory,
  2295. // no matter what DoubleClickAction is configured to
  2296. (IsSideLocalBrowser(Side) || Terminal->ResolvingSymlinks || Terminal->IsEncryptingFiles()) &&
  2297. // Can only Edit files, but can Open/Copy even directories
  2298. ((DoubleClickAction != dcaEdit) ||
  2299. !DView->ItemIsDirectory(DView->ItemFocused)))
  2300. {
  2301. Item->Options = O << tboDefault;
  2302. }
  2303. else
  2304. {
  2305. Item->Options = O >> tboDefault;
  2306. }
  2307. }
  2308. //---------------------------------------------------------------------------
  2309. void __fastcall TCustomScpExplorerForm::DirViewContextPopup(
  2310. TOperationSide Side, Byte PopupComponent, const TPoint &MousePos)
  2311. {
  2312. TCustomDirView * DView = DirView(Side);
  2313. TListItem * Item = DView->ItemFocused;
  2314. if ((DView->GetItemAt(MousePos.x, MousePos.y) == Item) &&
  2315. EnableFocusedOperation[Side])
  2316. {
  2317. TPoint ClientPoint;
  2318. ClientPoint = ((MousePos.x < 0) && (MousePos.y < 0)) ?
  2319. TPoint(0, 0) : MousePos;
  2320. FLastContextPopupScreenPoint = DView->ClientToScreen(ClientPoint);
  2321. reinterpret_cast<TPopupMenu*>(GetComponent(PopupComponent))->Popup(
  2322. FLastContextPopupScreenPoint.x, FLastContextPopupScreenPoint.y);
  2323. }
  2324. }
  2325. //---------------------------------------------------------------------------
  2326. void __fastcall TCustomScpExplorerForm::RemoteDirViewContextPopup(
  2327. TObject * /*Sender*/, const TPoint &MousePos, bool &Handled)
  2328. {
  2329. DirViewContextPopupDefaultItem(osRemote, NonVisualDataModule->RemoteOpenMenuItem, dcaOpen);
  2330. DirViewContextPopupDefaultItem(osRemote, NonVisualDataModule->RemoteEditMenuItem, dcaEdit);
  2331. DirViewContextPopupDefaultItem(osRemote, NonVisualDataModule->RemoteCopyMenuItem, dcaCopy);
  2332. DirViewContextPopup(osRemote, fcRemotePopup, MousePos);
  2333. Handled = true;
  2334. }
  2335. //---------------------------------------------------------------------------
  2336. void __fastcall TCustomScpExplorerForm::ReloadLocalDirectory(const UnicodeString Directory)
  2337. {
  2338. }
  2339. //---------------------------------------------------------------------------
  2340. void __fastcall TCustomScpExplorerForm::BatchStart(void *& /*Storage*/)
  2341. {
  2342. DebugAssert(FErrorList == NULL);
  2343. if (WinConfiguration->ContinueOnError)
  2344. {
  2345. FErrorList = new TStringList();
  2346. Configuration->Usage->Inc(L"ContinuationsOnError");
  2347. }
  2348. NonVisualDataModule->StartBusy();
  2349. }
  2350. //---------------------------------------------------------------------------
  2351. void __fastcall TCustomScpExplorerForm::BatchEnd(void * /*Storage*/)
  2352. {
  2353. NonVisualDataModule->EndBusy();
  2354. if (FErrorList)
  2355. {
  2356. HandleErrorList(FErrorList);
  2357. }
  2358. }
  2359. //---------------------------------------------------------------------------
  2360. void __fastcall TCustomScpExplorerForm::UpdateCopyParamCounters(
  2361. const TCopyParamType & CopyParam)
  2362. {
  2363. if (!CopyParam.IncludeFileMask.Masks.IsEmpty())
  2364. {
  2365. Configuration->Usage->Inc(L"FileMaskUses");
  2366. }
  2367. if (IsEffectiveFileNameMask(CopyParam.FileMask))
  2368. {
  2369. Configuration->Usage->Inc(L"OperationMaskUses");
  2370. }
  2371. }
  2372. //---------------------------------------------------------------------------
  2373. bool __fastcall TCustomScpExplorerForm::ExecuteCopyMoveFileOperation(
  2374. TFileOperation Operation, TOperationSide Side, TStrings * FileList, bool NoConfirmation, void * AParam)
  2375. {
  2376. DebugAssert(!IsLocalBrowserMode());
  2377. TTransferDirection Direction = (Side == osLocal ? tdToRemote : tdToLocal);
  2378. TTransferType Type = (Operation == foCopy ? ttCopy : ttMove);
  2379. TTransferOperationParam DefaultParam;
  2380. TTransferOperationParam & Param = (AParam != NULL) ? *static_cast<TTransferOperationParam *>(AParam) : DefaultParam;
  2381. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  2382. switch (Param.Queue)
  2383. {
  2384. case asOn:
  2385. CopyParam.Queue = true;
  2386. break;
  2387. case asOff:
  2388. CopyParam.Queue = false;
  2389. break;
  2390. case asAuto:
  2391. default:
  2392. // keep default
  2393. break;
  2394. }
  2395. bool Result =
  2396. CopyParamDialog(Direction, Type, Param.Temp, FileList, Param.TargetDirectory, CopyParam, !NoConfirmation, Param.DragDrop, Param.Options);
  2397. if (Result)
  2398. {
  2399. DebugAssert(Terminal);
  2400. bool SelectionRestored = false;
  2401. TCustomDirView * DView = NULL;
  2402. if (HasDirView[Side])
  2403. {
  2404. DView = DirView(Side);
  2405. DView->SaveSelection();
  2406. DView->SaveSelectedNames();
  2407. }
  2408. UpdateCopyParamCounters(CopyParam);
  2409. std::unique_ptr<TStringList> TransferResumeList(new TStringList());
  2410. DebugAssert(FTransferResumeList == NULL);
  2411. FTransferResumeList = Terminal->IsCapable[fcMoveToQueue] ? TransferResumeList.get() : NULL;
  2412. FMoveToQueue = false;
  2413. int Params = FLAGMASK(Operation == foMove, cpDelete);
  2414. try
  2415. {
  2416. TStrings * PermanentFileList;
  2417. std::unique_ptr<TStrings> PermanentFileListOwner;
  2418. try
  2419. {
  2420. if (Side == osLocal)
  2421. {
  2422. PermanentFileList = FileList;
  2423. Params |= FLAGMASK(Param.Temp, cpTemporary);
  2424. Terminal->CopyToRemote(FileList, Param.TargetDirectory, &CopyParam, Params, NULL);
  2425. if (Operation == foMove)
  2426. {
  2427. ReloadLocalDirectory();
  2428. if (DView != NULL)
  2429. {
  2430. DView->RestoreSelection();
  2431. }
  2432. SelectionRestored = true;
  2433. }
  2434. }
  2435. else
  2436. {
  2437. // Clone the file list as it may refer to current directory files,
  2438. // which get destroyed, when the source directory is reloaded after foMove operation.
  2439. // We should actually clone the file list for whole ExecuteFileOperation to protect against reloads.
  2440. // But for a hotfix, we are not going to do such a big change.
  2441. PermanentFileListOwner.reset(TRemoteFileList::CloneStrings(FileList));
  2442. PermanentFileList = PermanentFileListOwner.get();
  2443. try
  2444. {
  2445. Terminal->CopyToLocal(FileList, Param.TargetDirectory, &CopyParam, Params, NULL);
  2446. }
  2447. __finally
  2448. {
  2449. if (Operation == foMove)
  2450. {
  2451. if (DView != NULL)
  2452. {
  2453. DView->RestoreSelection();
  2454. }
  2455. SelectionRestored = true;
  2456. }
  2457. ReloadLocalDirectory(Param.TargetDirectory);
  2458. }
  2459. }
  2460. }
  2461. catch (EAbort &)
  2462. {
  2463. if (FMoveToQueue)
  2464. {
  2465. Params |= (CopyParam.QueueNoConfirmation ? cpNoConfirmation : 0);
  2466. DebugAssert(CopyParam.TransferSkipList == NULL);
  2467. DebugAssert(CopyParam.TransferResumeFile.IsEmpty());
  2468. if (TransferResumeList->Count > 0)
  2469. {
  2470. CopyParam.TransferResumeFile = TransferResumeList->Strings[TransferResumeList->Count - 1];
  2471. TransferResumeList->Delete(TransferResumeList->Count - 1);
  2472. }
  2473. CopyParam.TransferSkipList = TransferResumeList.release();
  2474. // not really needed, just to keep it consistent with TransferResumeList
  2475. FTransferResumeList = NULL;
  2476. FMoveToQueue = false;
  2477. Configuration->Usage->Inc("MovesToBackground");
  2478. AddQueueItem(Queue, Direction, PermanentFileList, Param.TargetDirectory, CopyParam, Params);
  2479. ClearTransferSourceSelection(Direction);
  2480. }
  2481. throw;
  2482. }
  2483. }
  2484. __finally
  2485. {
  2486. if (!SelectionRestored && (DView != NULL))
  2487. {
  2488. DView->DiscardSavedSelection();
  2489. }
  2490. FTransferResumeList = NULL;
  2491. }
  2492. }
  2493. return Result;
  2494. }
  2495. //---------------------------------------------------------------------------
  2496. bool __fastcall TCustomScpExplorerForm::ExecuteDeleteFileOperation(
  2497. TOperationSide Side, TStrings * FileList, void * Param)
  2498. {
  2499. DebugAssert(FileList->Count);
  2500. // We deliberately do not toggle alternative flag (Param), but use OR,
  2501. // because the Param is set only when command is invoked using Shift-Del/F8 keyboard
  2502. // shortcut of CurrentDeleteAlternativeAction
  2503. bool Alternative =
  2504. bool(Param) || UseAlternativeFunction();
  2505. bool Recycle;
  2506. if (IsSideLocalBrowser(Side))
  2507. {
  2508. Recycle = (WinConfiguration->DeleteToRecycleBin != Alternative);
  2509. }
  2510. else
  2511. {
  2512. Recycle =
  2513. (Terminal->SessionData->DeleteToRecycleBin != Alternative) &&
  2514. !Terminal->SessionData->RecycleBinPath.IsEmpty() &&
  2515. !Terminal->IsRecycledFile(FileList->Strings[0]);
  2516. }
  2517. bool Result = !(Recycle ? WinConfiguration->ConfirmRecycling : WinConfiguration->ConfirmDeleting);
  2518. if (!Result)
  2519. {
  2520. UnicodeString Query;
  2521. if (FileList->Count == 1)
  2522. {
  2523. if (IsSideLocalBrowser(Side))
  2524. {
  2525. Query = ExtractFileName(FileList->Strings[0]);
  2526. }
  2527. else
  2528. {
  2529. Query = UnixExtractFileName(FileList->Strings[0]);
  2530. }
  2531. Query = FMTLOAD((Recycle ? CONFIRM_RECYCLE_FILE : CONFIRM_DELETE_FILE), (Query));
  2532. }
  2533. else
  2534. {
  2535. Query = FMTLOAD((Recycle ? CONFIRM_RECYCLE_FILES : CONFIRM_DELETE_FILES), (FileList->Count));
  2536. }
  2537. TMessageParams Params(mpNeverAskAgainCheck);
  2538. Params.ImageName = L"Delete file";
  2539. unsigned int Answer =
  2540. MessageDialog(MainInstructions(Query), qtConfirmation, qaOK | qaCancel, HELP_DELETE_FILE, &Params);
  2541. if (Answer == qaNeverAskAgain)
  2542. {
  2543. Result = true;
  2544. if (Recycle)
  2545. {
  2546. WinConfiguration->ConfirmRecycling = false;
  2547. }
  2548. else
  2549. {
  2550. WinConfiguration->ConfirmDeleting = false;
  2551. }
  2552. }
  2553. else
  2554. {
  2555. Result = (Answer == qaOK);
  2556. }
  2557. }
  2558. if (Result)
  2559. {
  2560. DeleteFiles(Side, FileList, FLAGMASK(Alternative, dfAlternative));
  2561. }
  2562. return Result;
  2563. }
  2564. //---------------------------------------------------------------------------
  2565. bool __fastcall TCustomScpExplorerForm::ExecuteFileOperation(TFileOperation Operation,
  2566. TOperationSide Side, TStrings * FileList, bool NoConfirmation, void * Param)
  2567. {
  2568. TAutoBatch AutoBatch(this);
  2569. bool Result;
  2570. if ((Operation == foCopy) || (Operation == foMove))
  2571. {
  2572. Result = ExecuteCopyMoveFileOperation(Operation, Side, FileList, NoConfirmation, Param);
  2573. }
  2574. else if (Operation == foRename)
  2575. {
  2576. DebugAssert(DirView(Side)->ItemFocused);
  2577. DirView(Side)->ItemFocused->EditCaption();
  2578. Result = true;
  2579. }
  2580. else if (Operation == foDelete)
  2581. {
  2582. Result = ExecuteDeleteFileOperation(Side, FileList, Param);
  2583. }
  2584. else if (Operation == foSetProperties)
  2585. {
  2586. DirView(osRemote)->SaveSelectedNames();
  2587. Result = SetProperties(Side, FileList);
  2588. }
  2589. else if (Operation == foCustomCommand)
  2590. {
  2591. DebugAssert(Param);
  2592. DebugAssert(Side == osRemote); // Side carries no meaning for foCustomCommand
  2593. DirView(osRemote)->SaveSelectedNames();
  2594. const TCustomCommandType * Command = static_cast<const TCustomCommandType*>(Param);
  2595. CustomCommand(FileList, *Command, NULL);
  2596. Result = true;
  2597. }
  2598. else if ((Operation == foRemoteMove) || (Operation == foRemoteCopy))
  2599. {
  2600. DebugAssert(!IsSideLocalBrowser(Side));
  2601. Result = RemoteTransferFiles(FileList, NoConfirmation,
  2602. (Operation == foRemoteMove), reinterpret_cast<TManagedTerminal *>(Param));
  2603. }
  2604. else if (Operation == foLock)
  2605. {
  2606. DebugAssert(!IsSideLocalBrowser(Side));
  2607. LockFiles(FileList, true);
  2608. Result = true;
  2609. }
  2610. else if (Operation == foUnlock)
  2611. {
  2612. DebugAssert(!IsSideLocalBrowser(Side));
  2613. LockFiles(FileList, false);
  2614. Result = true;
  2615. }
  2616. else
  2617. {
  2618. DebugFail();
  2619. }
  2620. return Result;
  2621. }
  2622. //---------------------------------------------------------------------------
  2623. TOperationSide __fastcall TCustomScpExplorerForm::GetSide(TOperationSide Side)
  2624. {
  2625. if (Side == osCurrent)
  2626. {
  2627. Side = FCurrentSide;
  2628. }
  2629. return Side;
  2630. }
  2631. //---------------------------------------------------------------------------
  2632. bool __fastcall TCustomScpExplorerForm::ExecuteFileOperation(TFileOperation Operation,
  2633. TOperationSide Side, bool OnFocused, bool NoConfirmation, void * Param)
  2634. {
  2635. Side = GetSide(Side);
  2636. bool Result;
  2637. TStrings * FileList = DirView(Side)->CreateFileList(OnFocused, IsSideLocalBrowser(Side), NULL);
  2638. try
  2639. {
  2640. Result = ExecuteFileOperation(Operation, Side, FileList, NoConfirmation, Param);
  2641. }
  2642. __finally
  2643. {
  2644. delete FileList;
  2645. }
  2646. return Result;
  2647. }
  2648. //---------------------------------------------------------------------------
  2649. void __fastcall TCustomScpExplorerForm::ExecuteFileOperationCommand(
  2650. TFileOperation Operation, TOperationSide Side, bool OnFocused,
  2651. bool NoConfirmation, void * Param)
  2652. {
  2653. if (ExecuteFileOperation(Operation, Side, OnFocused, NoConfirmation, Param))
  2654. {
  2655. if ((Operation == foCopy) || (Operation == foMove))
  2656. {
  2657. DebugAssert(!IsLocalBrowserMode());
  2658. if (GetSide(Side) == osLocal)
  2659. {
  2660. Configuration->Usage->Inc(L"UploadsCommand");
  2661. }
  2662. else
  2663. {
  2664. Configuration->Usage->Inc(L"DownloadsCommand");
  2665. }
  2666. }
  2667. }
  2668. }
  2669. //---------------------------------------------------------------------------
  2670. void __fastcall TCustomScpExplorerForm::ExecuteCopyOperationCommand(
  2671. TOperationSide Side, bool OnFocused, unsigned int Flags)
  2672. {
  2673. TTransferOperationParam Param;
  2674. if ((WinConfiguration->Interface != ifCommander) ||
  2675. WinConfiguration->ScpCommander.ExplorerKeyboardShortcuts)
  2676. {
  2677. Flags &= ~cocShortCutHint;
  2678. }
  2679. TCustomDirView * DView = DirView(Side);
  2680. Param.Options =
  2681. FLAGMASK(FLAGSET(Flags, cocShortCutHint), coShortCutHint) |
  2682. FLAGMASK(SelectedAllFilesInDirView(DView), coAllFiles);
  2683. if (FLAGSET(Flags, cocQueue))
  2684. {
  2685. Param.Queue = asOn;
  2686. }
  2687. else if (FLAGSET(Flags, cocNonQueue))
  2688. {
  2689. Param.Queue = asOff;
  2690. }
  2691. ExecuteFileOperationCommand(foCopy, Side, OnFocused, false, &Param);
  2692. }
  2693. //---------------------------------------------------------------------------
  2694. void __fastcall TCustomScpExplorerForm::HandleErrorList(TStringList *& ErrorList)
  2695. {
  2696. try
  2697. {
  2698. if (ErrorList->Count)
  2699. {
  2700. UnicodeString Message = MainInstructions(FMTLOAD(ERROR_LIST_COUNT, (ErrorList->Count)));
  2701. if (MessageDialog(Message, qtError,
  2702. qaOK | qaCancel, HELP_NONE) == qaOK)
  2703. {
  2704. unsigned int Answer;
  2705. int Index = 0;
  2706. do
  2707. {
  2708. DebugAssert(Index >= 0 && Index < ErrorList->Count);
  2709. TQueryButtonAlias Aliases[2];
  2710. Aliases[0].Button = qaYes;
  2711. Aliases[0].Alias = LoadStr(PREV_BUTTON);
  2712. Aliases[1].Button = qaNo;
  2713. Aliases[1].Alias = LoadStr(NEXT_BUTTON);
  2714. TMessageParams Params;
  2715. Params.Aliases = Aliases;
  2716. Params.AliasesCount = LENOF(Aliases);
  2717. Answer = MoreMessageDialog(
  2718. FMTLOAD(ERROR_LIST_NUMBER, (Index+1, ErrorList->Count, ErrorList->Strings[Index])),
  2719. dynamic_cast<TStrings *>(ErrorList->Objects[Index]), qtError,
  2720. (Index ? qaYes : 0) | (Index < ErrorList->Count - 1 ? qaNo : 0) |
  2721. qaOK, HELP_NONE, &Params);
  2722. if (Answer == qaNo)
  2723. {
  2724. Index++;
  2725. }
  2726. if (Answer == qaYes)
  2727. {
  2728. Index--;
  2729. }
  2730. }
  2731. while (Answer != qaOK);
  2732. }
  2733. }
  2734. }
  2735. __finally
  2736. {
  2737. TStrings * List = ErrorList;
  2738. ErrorList = NULL;
  2739. for (int i = 0; i < List->Count; i++)
  2740. {
  2741. delete List->Objects[i];
  2742. }
  2743. delete List;
  2744. }
  2745. }
  2746. //---------------------------------------------------------------------------
  2747. void __fastcall TCustomScpExplorerForm::ExecuteRemoteFile(
  2748. const UnicodeString & FullFileName, TRemoteFile * File, TExecuteFileBy ExecuteFileBy)
  2749. {
  2750. // needed for checking filemasks, as there's no directory object
  2751. // associated with the file object
  2752. File->FullFileName = FullFileName;
  2753. TFileMasks::TParams MaskParams;
  2754. MaskParams.Size = File->Size;
  2755. MaskParams.Modification = File->Modification;
  2756. ExecuteFile(osRemote, ExecuteFileBy, NULL, FullFileName, File, MaskParams);
  2757. }
  2758. //---------------------------------------------------------------------------
  2759. void __fastcall TCustomScpExplorerForm::EditNew(TOperationSide Side)
  2760. {
  2761. DebugAssert(!WinConfiguration->DisableOpenEdit);
  2762. Side = GetSide(Side);
  2763. TCustomDirView * DView = DirView(Side);
  2764. TListItem * FocusedItem = DView->ItemFocused;
  2765. UnicodeString Name;
  2766. if ((FocusedItem != NULL) && !DView->ItemIsDirectory(FocusedItem))
  2767. {
  2768. Name = DView->ItemFileName(FocusedItem);
  2769. }
  2770. else
  2771. {
  2772. Name = LoadStr(NEW_FILE);
  2773. }
  2774. UnicodeString Names = Name;
  2775. std::unique_ptr<TStrings> History(CloneStrings(CustomWinConfiguration->History[L"EditFile"]));
  2776. if (InputDialog(LoadStr(EDIT_FILE_CAPTION), LoadStr(EDIT_FILE_PROMPT), Names,
  2777. HELP_EDIT_NEW, History.get(), true))
  2778. {
  2779. while (!Names.IsEmpty())
  2780. {
  2781. Name = CutToChar(Names, FileMasksDelimiters[1], false);
  2782. CustomWinConfiguration->History[L"EditFile"] = History.get();
  2783. UnicodeString TargetFileName;
  2784. UnicodeString LocalFileName;
  2785. UnicodeString RootTempDir;
  2786. UnicodeString TempDir;
  2787. UnicodeString RemoteDirectory;
  2788. bool ExistingFile = false;
  2789. if (!IsSideLocalBrowser(Side))
  2790. {
  2791. Name = AbsolutePath(FTerminal->CurrentDirectory, Name);
  2792. TRemoteFile * File = NULL;
  2793. if (FTerminal->FileExists(Name, &File) &&
  2794. !File->IsDirectory)
  2795. {
  2796. try
  2797. {
  2798. ExecuteRemoteFile(Name, File, efDefaultEditor);
  2799. ExistingFile = true;
  2800. }
  2801. __finally
  2802. {
  2803. delete File;
  2804. }
  2805. }
  2806. if (!ExistingFile)
  2807. {
  2808. RemoteDirectory = UnixExtractFilePath(Name);
  2809. TemporaryDirectoryForRemoteFiles(
  2810. RemoteDirectory, GUIConfiguration->CurrentCopyParam, TempDir, RootTempDir);
  2811. TargetFileName = UnixExtractFileName(Name);
  2812. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  2813. LocalFileName = TempDir +
  2814. // We probably do not want to trim the VMS version here
  2815. FTerminal->ChangeFileName(&CopyParam, TargetFileName, osRemote, false);
  2816. }
  2817. }
  2818. else
  2819. {
  2820. if (ExtractFilePath(Name).IsEmpty())
  2821. {
  2822. LocalFileName = IncludeTrailingBackslash(DView->PathName) + Name;
  2823. }
  2824. else
  2825. {
  2826. LocalFileName = ExpandFileName(Name);
  2827. }
  2828. TargetFileName = ExtractFileName(Name);
  2829. }
  2830. if (!ExistingFile)
  2831. {
  2832. bool NewFile = !FileExists(ApiPath(LocalFileName));
  2833. if (NewFile)
  2834. {
  2835. int File = FileCreate(ApiPath(LocalFileName));
  2836. if (File < 0)
  2837. {
  2838. if (!RootTempDir.IsEmpty())
  2839. {
  2840. RecursiveDeleteFile(ExcludeTrailingBackslash(RootTempDir), false);
  2841. }
  2842. throw Exception(FMTLOAD(CREATE_FILE_ERROR, (LocalFileName)));
  2843. }
  2844. else
  2845. {
  2846. FileClose(File);
  2847. }
  2848. }
  2849. TExecuteFileBy ExecuteFileBy = efDefaultEditor;
  2850. const TEditorData * ExternalEditor = NULL;
  2851. TFileMasks::TParams MaskParams; // size not known
  2852. ExecuteFileNormalize(ExecuteFileBy, ExternalEditor, TargetFileName,
  2853. false, MaskParams);
  2854. CustomExecuteFile(Side, ExecuteFileBy, LocalFileName, TargetFileName,
  2855. ExternalEditor, RootTempDir, RemoteDirectory, NewFile);
  2856. }
  2857. }
  2858. }
  2859. }
  2860. //---------------------------------------------------------------------------
  2861. bool __fastcall TCustomScpExplorerForm::RemoteExecuteForceText(
  2862. TExecuteFileBy ExecuteFileBy, const TEditorData * ExternalEditor)
  2863. {
  2864. DebugAssert((ExecuteFileBy == efExternalEditor) ==
  2865. ((ExternalEditor != NULL) && (ExternalEditor->Editor == edExternal)));
  2866. DebugAssert(ExecuteFileBy != efDefaultEditor);
  2867. return
  2868. ((ExecuteFileBy == efInternalEditor)) ||
  2869. ((ExecuteFileBy == efExternalEditor) && ExternalEditor->ExternalEditorText);
  2870. }
  2871. //---------------------------------------------------------------------------
  2872. void __fastcall TCustomScpExplorerForm::CustomExecuteFile(TOperationSide Side,
  2873. TExecuteFileBy ExecuteFileBy, UnicodeString FileName, UnicodeString OriginalFileName,
  2874. const TEditorData * ExternalEditor, UnicodeString LocalRootDirectory,
  2875. UnicodeString RemoteDirectory, bool NewFile)
  2876. {
  2877. DebugAssert(!WinConfiguration->DisableOpenEdit);
  2878. DebugAssert((ExecuteFileBy == efExternalEditor) ==
  2879. ((ExternalEditor != NULL) && (ExternalEditor->Editor == edExternal)));
  2880. DebugAssert(ExecuteFileBy != efDefaultEditor);
  2881. Side = GetSide(Side);
  2882. std::unique_ptr<TEditedFileData> Data(new TEditedFileData);
  2883. if (!IsSideLocalBrowser(Side))
  2884. {
  2885. Data->Terminal = Terminal;
  2886. Data->Queue = Queue;
  2887. Data->SessionData = CloneCurrentSessionData();
  2888. Data->ForceText = RemoteExecuteForceText(ExecuteFileBy, ExternalEditor);
  2889. Data->RemoteDirectory = RemoteDirectory;
  2890. Data->SessionName = Terminal->SessionData->SessionName;
  2891. Data->LocalRootDirectory = LocalRootDirectory;
  2892. Data->OriginalFileName = OriginalFileName;
  2893. Data->Command = L""; // will be changed later for external editor
  2894. }
  2895. if (ExecuteFileBy == efInternalEditor)
  2896. {
  2897. if (!IsSideLocalBrowser(Side))
  2898. {
  2899. UnicodeString Caption = UnixIncludeTrailingBackslash(RemoteDirectory) + OriginalFileName +
  2900. L" - " + Terminal->SessionData->SessionName;
  2901. TForm * Editor;
  2902. try
  2903. {
  2904. TNotifyEvent OnFileChanged = NULL;
  2905. TNotifyEvent OnSaveAll = NULL;
  2906. TAnyModifiedEvent OnAnyModified = NULL;
  2907. // Edited files are uploaded in background queue, what we do not support with encrypted files
  2908. if (Terminal->IsCapable[fsBackgroundTransfers])
  2909. {
  2910. OnFileChanged = FEditorManager->FileChanged;
  2911. OnSaveAll = SaveAllInternalEditors;
  2912. OnAnyModified = AnyInternalEditorModified;
  2913. }
  2914. Editor = ShowEditorForm(FileName, this, OnFileChanged,
  2915. FEditorManager->FileReload, FEditorManager->FileClosed,
  2916. OnSaveAll, OnAnyModified,
  2917. Caption, FStandaloneEditing, SessionColor, Terminal->SessionData->InternalEditorEncoding, NewFile);
  2918. }
  2919. catch(...)
  2920. {
  2921. if (!LocalRootDirectory.IsEmpty())
  2922. {
  2923. RecursiveDeleteFile(ExcludeTrailingBackslash(LocalRootDirectory), false);
  2924. }
  2925. throw;
  2926. }
  2927. FEditorManager->AddFileInternal(FileName, Data.release(), Editor);
  2928. }
  2929. else
  2930. {
  2931. DebugAssert(!FStandaloneEditing);
  2932. TForm * Editor =
  2933. ShowEditorForm(FileName, this, NULL, NULL, LocalEditorClosed,
  2934. SaveAllInternalEditors, AnyInternalEditorModified,
  2935. L"", false, SessionColor, -1, NewFile);
  2936. FLocalEditors->Add(Editor);
  2937. }
  2938. }
  2939. else
  2940. {
  2941. HANDLE Process;
  2942. if (ExecuteFileBy == efExternalEditor)
  2943. {
  2944. UnicodeString Program, Params, Dir;
  2945. Data->Command = ExternalEditor->ExternalEditor;
  2946. ReformatFileNameCommand(Data->Command);
  2947. SplitCommand(Data->Command, Program, Params, Dir);
  2948. Params = ExpandFileNameCommand(Params, FileName);
  2949. Program = ExpandEnvironmentVariables(Program);
  2950. if (!ExecuteShell(Program, Params, Process))
  2951. {
  2952. throw EOSExtException(FMTLOAD(EDITOR_ERROR, (Program)));
  2953. }
  2954. }
  2955. else
  2956. {
  2957. DebugAssert(IsSideLocalBrowser(Side));
  2958. if (!ExecuteShell(FileName, L"", Process))
  2959. {
  2960. throw EOSExtException(FMTLOAD(EXECUTE_FILE_ERROR, (FileName)));
  2961. }
  2962. }
  2963. if (IsSideLocalBrowser(Side) ||
  2964. ((ExecuteFileBy == efShell) &&
  2965. !WinConfiguration->Editor.SDIShellEditor) ||
  2966. ((ExecuteFileBy == efExternalEditor) &&
  2967. !ExternalEditor->SDIExternalEditor))
  2968. {
  2969. // no need for handle
  2970. if (Process != NULL)
  2971. {
  2972. DebugCheck(CloseHandle(Process));
  2973. }
  2974. Process = INVALID_HANDLE_VALUE;
  2975. }
  2976. else
  2977. {
  2978. if (Process == NULL)
  2979. {
  2980. throw ExtException(LoadStr(OPEN_FILE_NO_PROCESS2), L"", HELP_OPEN_FILE_NO_PROCESS);
  2981. }
  2982. }
  2983. if (!IsSideLocalBrowser(Side))
  2984. {
  2985. FEditorManager->AddFileExternal(FileName, Data.release(), Process);
  2986. }
  2987. }
  2988. }
  2989. //---------------------------------------------------------------------------
  2990. void __fastcall TCustomScpExplorerForm::SaveInternalEditor(
  2991. const UnicodeString /*FileName*/, TEditedFileData * /*Data*/, TObject * Token,
  2992. void * /*Arg*/)
  2993. {
  2994. if (Token != NULL)
  2995. {
  2996. EditorFormFileSave(static_cast<TForm *>(Token));
  2997. }
  2998. }
  2999. //---------------------------------------------------------------------------
  3000. void __fastcall TCustomScpExplorerForm::SaveAllInternalEditors(TObject * /*Sender*/)
  3001. {
  3002. for (int Index = 0; Index < FLocalEditors->Count; Index++)
  3003. {
  3004. EditorFormFileSave(static_cast<TForm *>(FLocalEditors->Items[Index]));
  3005. }
  3006. FEditorManager->ProcessFiles(SaveInternalEditor, NULL);
  3007. }
  3008. //---------------------------------------------------------------------------
  3009. void __fastcall TCustomScpExplorerForm::InternalEditorModified(
  3010. const UnicodeString /*FileName*/, TEditedFileData * /*Data*/, TObject * Token,
  3011. void * Arg)
  3012. {
  3013. if ((Token != NULL) &&
  3014. IsEditorFormModified(static_cast<TForm *>(Token)))
  3015. {
  3016. *static_cast<bool *>(Arg) = true;
  3017. }
  3018. }
  3019. //---------------------------------------------------------------------------
  3020. void __fastcall TCustomScpExplorerForm::AnyInternalEditorModified(
  3021. TObject * /*Sender*/, bool & Modified)
  3022. {
  3023. for (int Index = 0; !Modified && (Index < FLocalEditors->Count); Index++)
  3024. {
  3025. if (IsEditorFormModified(static_cast<TForm *>(FLocalEditors->Items[Index])))
  3026. {
  3027. Modified = true;
  3028. }
  3029. }
  3030. if (!Modified)
  3031. {
  3032. FEditorManager->ProcessFiles(InternalEditorModified, &Modified);
  3033. }
  3034. }
  3035. //---------------------------------------------------------------------------
  3036. void __fastcall TCustomScpExplorerForm::LocalEditorClosed(TObject * Sender, bool /*Forced*/)
  3037. {
  3038. DebugCheck(FLocalEditors->Extract(Sender) >= 0);
  3039. }
  3040. //---------------------------------------------------------------------------
  3041. void __fastcall TCustomScpExplorerForm::TemporaryDirectoryForRemoteFiles(
  3042. UnicodeString RemoteDirectory, TCopyParamType CopyParam,
  3043. UnicodeString & Result, UnicodeString & RootDirectory)
  3044. {
  3045. if (!WinConfiguration->TemporaryDirectoryDeterministic)
  3046. {
  3047. RootDirectory = IncludeTrailingBackslash(WinConfiguration->TemporaryDir());
  3048. Result = RootDirectory;
  3049. }
  3050. else
  3051. {
  3052. RootDirectory = L"";
  3053. Result = WinConfiguration->ExpandedTemporaryDirectory();
  3054. Result = IncludeTrailingBackslash(Result);
  3055. }
  3056. if (WinConfiguration->TemporaryDirectoryAppendSession)
  3057. {
  3058. Result = IncludeTrailingBackslash(Result + MakeValidFileName(Terminal->SessionData->SessionName));
  3059. }
  3060. if (WinConfiguration->TemporaryDirectoryAppendPath)
  3061. {
  3062. if (!RemoteDirectory.IsEmpty() && (RemoteDirectory[1] == L'/'))
  3063. {
  3064. RemoteDirectory.Delete(1, 1);
  3065. }
  3066. Result = IncludeTrailingBackslash(Result + CopyParam.ValidLocalPath(FromUnixPath(RemoteDirectory)));
  3067. }
  3068. if (!ForceDirectories(ApiPath(Result)))
  3069. {
  3070. throw EOSExtException(FMTLOAD(CREATE_TEMP_DIR_ERROR, (Result)));
  3071. }
  3072. }
  3073. //---------------------------------------------------------------------------
  3074. void __fastcall TCustomScpExplorerForm::TemporarilyDownloadFiles(
  3075. TStrings * FileList, bool ForceText, UnicodeString & RootTempDir, UnicodeString & TempDir,
  3076. bool AllFiles, bool GetTargetNames, bool AutoOperation)
  3077. {
  3078. DebugAssert(!IsLocalBrowserMode());
  3079. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  3080. if (ForceText)
  3081. {
  3082. CopyParam.TransferMode = tmAscii;
  3083. }
  3084. TemporaryFileCopyParam(CopyParam);
  3085. if (AllFiles)
  3086. {
  3087. CopyParam.IncludeFileMask = TFileMasks();
  3088. }
  3089. if (TempDir.IsEmpty())
  3090. {
  3091. TemporaryDirectoryForRemoteFiles(FTerminal->CurrentDirectory, CopyParam, TempDir, RootTempDir);
  3092. }
  3093. DebugAssert(!FAutoOperation);
  3094. FAutoOperation = AutoOperation;
  3095. Terminal->ExceptionOnFail = true;
  3096. try
  3097. {
  3098. try
  3099. {
  3100. // turn off confirmations, as for MDI editors we may possibly download
  3101. // the same file over
  3102. Terminal->CopyToLocal(
  3103. FileList, TempDir, &CopyParam, cpNoConfirmation | cpTemporary, NULL);
  3104. if (GetTargetNames)
  3105. {
  3106. for (int i = 0; i < FileList->Count; i++)
  3107. {
  3108. FileList->Strings[i] =
  3109. Terminal->ChangeFileName(&CopyParam, UnixExtractFileName(FileList->Strings[i]), osRemote, false);
  3110. }
  3111. }
  3112. }
  3113. catch(...)
  3114. {
  3115. if (!RootTempDir.IsEmpty())
  3116. {
  3117. RecursiveDeleteFile(ExcludeTrailingBackslash(RootTempDir), false);
  3118. }
  3119. throw;
  3120. }
  3121. }
  3122. __finally
  3123. {
  3124. FAutoOperation = false;
  3125. Terminal->ExceptionOnFail = false;
  3126. }
  3127. }
  3128. //---------------------------------------------------------------------------
  3129. void __fastcall TCustomScpExplorerForm::EditorAutoConfig()
  3130. {
  3131. // Do not waste time checking for default editor list the next time,
  3132. // or testing if default editor is not notepad.
  3133. bool TryNextTime = false;
  3134. UnicodeString UsageState;
  3135. if (!WinConfiguration->EditorList->IsDefaultList())
  3136. {
  3137. UsageState = "H";
  3138. }
  3139. else
  3140. {
  3141. UnicodeString Executable;
  3142. UnicodeString ExecutableDescription;
  3143. if (DetectSystemExternalEditor(false, Executable, ExecutableDescription, UsageState, TryNextTime))
  3144. {
  3145. UnicodeString Message =
  3146. FMTLOAD(EDITOR_AUTO_CONFIG2, (ExecutableDescription, ExecutableDescription));
  3147. unsigned int Answer =
  3148. MessageDialog(Message, qtConfirmation, qaOK | qaCancel, HELP_EDITOR_AUTO_CONFIG);
  3149. if (Answer != qaOK)
  3150. {
  3151. UsageState = "R";
  3152. }
  3153. else
  3154. {
  3155. UsageState = "A";
  3156. TEditorData EditorData;
  3157. EditorData.Editor = edExternal;
  3158. EditorData.ExternalEditor = FormatCommand(Executable, L"");
  3159. EditorData.DecideExternalEditorText();
  3160. TEditorList EditorList;
  3161. EditorList = *WinConfiguration->EditorList;
  3162. EditorList.Insert(0, new TEditorPreferences(EditorData));
  3163. WinConfiguration->EditorList = &EditorList;
  3164. }
  3165. }
  3166. }
  3167. WinConfiguration->OfferedEditorAutoConfig = !TryNextTime;
  3168. WinConfiguration->Usage->Set(L"EditorAutoConfig", UsageState);
  3169. }
  3170. //---------------------------------------------------------------------------
  3171. void __fastcall TCustomScpExplorerForm::ExecuteFileNormalize(
  3172. TExecuteFileBy & ExecuteFileBy, const TEditorData *& ExternalEditor,
  3173. const UnicodeString & FileName, bool Local, const TFileMasks::TParams & MaskParams)
  3174. {
  3175. if (ExecuteFileBy == efDefaultEditor)
  3176. {
  3177. if (!WinConfiguration->OfferedEditorAutoConfig)
  3178. {
  3179. EditorAutoConfig();
  3180. }
  3181. const TEditorPreferences * Editor =
  3182. WinConfiguration->DefaultEditorForFile(FileName, Local, MaskParams);
  3183. if ((Editor == NULL) || (Editor->Data->Editor == edInternal))
  3184. {
  3185. ExecuteFileBy = efInternalEditor;
  3186. ExternalEditor = NULL;
  3187. }
  3188. else if (Editor->Data->Editor == edOpen)
  3189. {
  3190. ExecuteFileBy = efShell;
  3191. ExternalEditor = NULL;
  3192. }
  3193. else
  3194. {
  3195. ExecuteFileBy = efExternalEditor;
  3196. ExternalEditor = Editor->Data;
  3197. }
  3198. }
  3199. }
  3200. //---------------------------------------------------------------------------
  3201. void __fastcall TCustomScpExplorerForm::ExecuteFile(TOperationSide Side,
  3202. TExecuteFileBy ExecuteFileBy, const TEditorData * ExternalEditor,
  3203. UnicodeString FullFileName, TObject * Object, const TFileMasks::TParams & MaskParams)
  3204. {
  3205. UnicodeString OriginalFileName;
  3206. UnicodeString LocalRootDirectory;
  3207. UnicodeString RemoteDirectory;
  3208. ExecuteFileNormalize(ExecuteFileBy, ExternalEditor, FullFileName,
  3209. IsSideLocalBrowser(Side), MaskParams);
  3210. UnicodeString Counter;
  3211. UnicodeString LocalFileName;
  3212. bool Handled = false;
  3213. if (!IsSideLocalBrowser(Side))
  3214. {
  3215. // We need to trim VMS version here, so that we use name without version
  3216. // when uploading back to create a new version of the file
  3217. OriginalFileName = FTerminal->GetBaseFileName(UnixExtractFileName(FullFileName));
  3218. RemoteDirectory = UnixExtractFilePath(FullFileName);
  3219. TObject * Token = NULL;
  3220. UnicodeString LocalDirectory;
  3221. if (!FEditorManager->CanAddFile(RemoteDirectory, OriginalFileName,
  3222. Terminal->SessionData->SessionName, Token, LocalRootDirectory,
  3223. LocalDirectory))
  3224. {
  3225. if (Token != NULL)
  3226. {
  3227. TForm * Form = dynamic_cast<TForm *>(Token);
  3228. if (Form->WindowState == wsMinimized)
  3229. {
  3230. ShowWindow(Form->Handle, SW_RESTORE);
  3231. }
  3232. else
  3233. {
  3234. Form->SetFocus();
  3235. }
  3236. Handled = true;
  3237. }
  3238. else
  3239. {
  3240. throw Exception(FMTLOAD(ALREADY_EDITED_EXTERNALLY_OR_UPLOADED, (OriginalFileName)));
  3241. }
  3242. }
  3243. if (!Handled)
  3244. {
  3245. TStringList * FileList1 = new TStringList();
  3246. try
  3247. {
  3248. FileList1->AddObject(FullFileName, Object);
  3249. TemporarilyDownloadFiles(FileList1,
  3250. RemoteExecuteForceText(ExecuteFileBy, ExternalEditor),
  3251. LocalRootDirectory, LocalDirectory, true, true, true);
  3252. LocalFileName = LocalDirectory + FileList1->Strings[0];
  3253. }
  3254. __finally
  3255. {
  3256. delete FileList1;
  3257. }
  3258. switch (ExecuteFileBy)
  3259. {
  3260. case efShell:
  3261. Counter = "RemoteFilesExecuted";
  3262. break;
  3263. case efInternalEditor:
  3264. Counter = "RemoteFilesOpenedInInternalEditor";
  3265. break;
  3266. case efExternalEditor:
  3267. Counter = "RemoteFilesOpenedInExternalEditor";
  3268. break;
  3269. default:
  3270. DebugFail();
  3271. }
  3272. }
  3273. }
  3274. else
  3275. {
  3276. LocalFileName = FullFileName;
  3277. OriginalFileName = ExtractFileName(FullFileName);
  3278. switch (ExecuteFileBy)
  3279. {
  3280. case efShell:
  3281. Counter = "LocalFilesExecuted";
  3282. break;
  3283. case efInternalEditor:
  3284. Counter = "LocalFilesOpenedInInternalEditor";
  3285. break;
  3286. case efExternalEditor:
  3287. Counter = "LocalFilesOpenedInExternalEditor";
  3288. break;
  3289. default:
  3290. DebugFail();
  3291. }
  3292. }
  3293. if (!Handled)
  3294. {
  3295. Configuration->Usage->Inc(Counter);
  3296. CustomExecuteFile(Side, ExecuteFileBy, LocalFileName, OriginalFileName,
  3297. ExternalEditor, LocalRootDirectory, RemoteDirectory, false);
  3298. }
  3299. }
  3300. //---------------------------------------------------------------------------
  3301. void __fastcall TCustomScpExplorerForm::ExecuteFile(TOperationSide Side,
  3302. TExecuteFileBy ExecuteFileBy, const TEditorData * ExternalEditor,
  3303. bool AllSelected, bool OnFocused)
  3304. {
  3305. DebugAssert(!WinConfiguration->DisableOpenEdit);
  3306. DebugAssert((ExecuteFileBy == efExternalEditor) ==
  3307. ((ExternalEditor != NULL) && (ExternalEditor->Editor == edExternal)));
  3308. Side = GetSide(Side);
  3309. TCustomDirView * DView = DirView(Side);
  3310. TStrings * FileList =
  3311. AllSelected ?
  3312. DView->CreateFileList(OnFocused, IsSideLocalBrowser(Side)) :
  3313. DView->CreateFocusedFileList(IsSideLocalBrowser(Side));
  3314. try
  3315. {
  3316. DebugAssert(AllSelected || (FileList->Count == 1));
  3317. for (int i = 0; i < FileList->Count; i++)
  3318. {
  3319. UnicodeString ListFileName = FileList->Strings[i];
  3320. UnicodeString FileNameOnly =
  3321. !IsSideLocalBrowser(Side) ? UnixExtractFileName(ListFileName) : ExtractFileName(ListFileName);
  3322. TListItem * Item = DView->FindFileItem(FileNameOnly);
  3323. if (!DView->ItemIsDirectory(Item))
  3324. {
  3325. UnicodeString FullFileName;
  3326. if (!IsSideLocalBrowser(Side))
  3327. {
  3328. FullFileName = DirView(Side)->Path + ListFileName;
  3329. }
  3330. else
  3331. {
  3332. FullFileName = ListFileName;
  3333. }
  3334. TFileMasks::TParams MaskParams;
  3335. MaskParams.Size = DView->ItemFileSize(Item);
  3336. TDateTimePrecision Precision;
  3337. MaskParams.Modification = DView->ItemFileTime(Item, Precision);
  3338. ExecuteFile(Side, ExecuteFileBy, ExternalEditor, FullFileName,
  3339. FileList->Objects[i], MaskParams);
  3340. }
  3341. }
  3342. }
  3343. __finally
  3344. {
  3345. delete FileList;
  3346. }
  3347. }
  3348. //---------------------------------------------------------------------------
  3349. void __fastcall TCustomScpExplorerForm::TemporaryFileCopyParam(TCopyParamType & CopyParam)
  3350. {
  3351. // do not forget to add additional options to TemporarilyDownloadFiles, and AS
  3352. CopyParam.FileNameCase = ncNoChange;
  3353. CopyParam.PreserveRights = false;
  3354. CopyParam.PreserveReadOnly = false;
  3355. CopyParam.ReplaceInvalidChars = true;
  3356. CopyParam.IncludeFileMask = TFileMasks();
  3357. CopyParam.NewerOnly = false;
  3358. CopyParam.FileMask = L"";
  3359. }
  3360. //---------------------------------------------------------------------------
  3361. void __fastcall TCustomScpExplorerForm::ExecutedFileChanged(const UnicodeString FileName,
  3362. TEditedFileData * Data, HANDLE UploadCompleteEvent)
  3363. {
  3364. TTerminalManager * Manager = TTerminalManager::Instance();
  3365. if ((Data->Terminal == NULL) || !Data->Terminal->Active)
  3366. {
  3367. if (!NonVisualDataModule->Busy)
  3368. {
  3369. UnicodeString FileNameOnly = ExtractFileName(FileName);
  3370. UnicodeString EditInactiveSessionReopenAcceptedCounter = L"EditInactiveSessionReopenAccepted";
  3371. UnicodeString EditInactiveSessionReopenRejectedCounter = L"EditInactiveSessionReopenRejected";
  3372. if (Data->Terminal == NULL)
  3373. {
  3374. TTerminal * SameSiteTerminal = Manager->FindActiveTerminalForSite(Data->SessionData);
  3375. if (SameSiteTerminal != NULL)
  3376. {
  3377. UnicodeString Message =
  3378. FMTLOAD(EDIT_SESSION_REATTACH,
  3379. (FileNameOnly, Data->SessionName, FileNameOnly));
  3380. if (MessageDialog(Message, qtConfirmation, qaOK | qaCancel) == qaOK)
  3381. {
  3382. Data->Terminal = Terminal;
  3383. Data->Queue = Manager->FindQueueForTerminal(Terminal);
  3384. Data->SessionName = Terminal->SessionData->SessionName;
  3385. // We might also overwrite session data
  3386. Configuration->Usage->Inc(EditInactiveSessionReopenAcceptedCounter);
  3387. }
  3388. else
  3389. {
  3390. Configuration->Usage->Inc(EditInactiveSessionReopenRejectedCounter);
  3391. Abort();
  3392. }
  3393. }
  3394. }
  3395. // foreground session should reconnect itself
  3396. else if (Terminal != Data->Terminal)
  3397. {
  3398. UnicodeString Message =
  3399. MainInstructions(
  3400. FMTLOAD(EDIT_SESSION_RECONNECT, (Data->SessionName, FileNameOnly)));
  3401. if (MessageDialog(Message, qtConfirmation, qaOK | qaCancel) == qaOK)
  3402. {
  3403. Manager->SetActiveTerminalWithAutoReconnect(Data->Terminal);
  3404. Configuration->Usage->Inc(EditInactiveSessionReopenAcceptedCounter);
  3405. }
  3406. else
  3407. {
  3408. Configuration->Usage->Inc(EditInactiveSessionReopenRejectedCounter);
  3409. Abort();
  3410. }
  3411. }
  3412. }
  3413. if ((Data->Terminal == NULL) || !Data->Terminal->Active)
  3414. {
  3415. Configuration->Usage->Inc(L"EditInactiveSession");
  3416. // Prevent this when not idle (!NonVisualDataModule->Busy)?
  3417. throw Exception(FMTLOAD(EDIT_SESSION_CLOSED2,
  3418. (ExtractFileName(FileName), Data->SessionName)));
  3419. }
  3420. }
  3421. TStrings * FileList = new TStringList();
  3422. try
  3423. {
  3424. FileList->Add(FileName);
  3425. // consider using the same settings (preset) as when the file was downloaded
  3426. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  3427. TemporaryFileCopyParam(CopyParam);
  3428. if (Data->ForceText)
  3429. {
  3430. CopyParam.TransferMode = tmAscii;
  3431. }
  3432. // so i do not need to worry if masking algorithm works in all cases
  3433. // ("" means "copy file name", no masking is actually done)
  3434. if (ExtractFileName(FileName) == Data->OriginalFileName)
  3435. {
  3436. CopyParam.FileMask = L"";
  3437. }
  3438. else
  3439. {
  3440. CopyParam.FileMask = DelimitFileNameMask(Data->OriginalFileName);
  3441. }
  3442. DebugAssert(Data->Queue != NULL);
  3443. int Params = cpNoConfirmation | cpTemporary;
  3444. TQueueItem * QueueItem = new TUploadQueueItem(Data->Terminal, FileList,
  3445. Data->RemoteDirectory, &CopyParam, Params, true, false);
  3446. QueueItem->CompleteEvent = UploadCompleteEvent;
  3447. AddQueueItem(Data->Queue, QueueItem, Data->Terminal);
  3448. }
  3449. __finally
  3450. {
  3451. delete FileList;
  3452. }
  3453. }
  3454. //---------------------------------------------------------------------------
  3455. void __fastcall TCustomScpExplorerForm::ExecutedFileReload(
  3456. const UnicodeString FileName, const TEditedFileData * Data)
  3457. {
  3458. // Sanity check, we should not be busy otherwise user would not be able to click Reload button.
  3459. DebugAssert(!NonVisualDataModule->Busy);
  3460. if ((Data->Terminal == NULL) || !Data->Terminal->Active)
  3461. {
  3462. throw Exception(FMTLOAD(EDIT_SESSION_CLOSED_RELOAD,
  3463. (ExtractFileName(FileName), Data->SessionName)));
  3464. }
  3465. TManagedTerminal * PrevTerminal = TTerminalManager::Instance()->ActiveTerminal;
  3466. TTerminalManager::Instance()->ActiveTerminal = Data->Terminal;
  3467. NonVisualDataModule->StartBusy();
  3468. try
  3469. {
  3470. std::unique_ptr<TRemoteFile> File;
  3471. UnicodeString RemoteFileName =
  3472. UnixIncludeTrailingBackslash(Data->RemoteDirectory) + Data->OriginalFileName;
  3473. FTerminal->ExceptionOnFail = true;
  3474. try
  3475. {
  3476. TRemoteFile * AFile = NULL;
  3477. FTerminal->ReadFile(RemoteFileName, AFile);
  3478. File.reset(AFile);
  3479. if (!File->HaveFullFileName)
  3480. {
  3481. File->FullFileName = RemoteFileName;
  3482. }
  3483. }
  3484. __finally
  3485. {
  3486. FTerminal->ExceptionOnFail = false;
  3487. }
  3488. std::unique_ptr<TStrings> FileList(new TStringList());
  3489. FileList->AddObject(RemoteFileName, File.get());
  3490. UnicodeString RootTempDir = Data->LocalRootDirectory;
  3491. UnicodeString TempDir = ExtractFilePath(FileName);
  3492. TemporarilyDownloadFiles(FileList.get(), Data->ForceText, RootTempDir,
  3493. TempDir, true, true, true);
  3494. // sanity check, the target file name should be still the same
  3495. DebugAssert(ExtractFileName(FileName) == FileList->Strings[0]);
  3496. }
  3497. __finally
  3498. {
  3499. NonVisualDataModule->EndBusy();
  3500. // it actually may not exist anymore...
  3501. TTerminalManager::Instance()->ActiveTerminal = PrevTerminal;
  3502. }
  3503. }
  3504. //---------------------------------------------------------------------------
  3505. void __fastcall TCustomScpExplorerForm::ExecutedFileEarlyClosed(
  3506. const TEditedFileData * Data, bool & KeepOpen)
  3507. {
  3508. // Command is set for external editors only (not for "shell" open).
  3509. if (!Data->Command.IsEmpty())
  3510. {
  3511. bool AnyFound = false;
  3512. bool AnyMDI = false;
  3513. bool AnyNonMDI = false;
  3514. bool AnyDetect = false;
  3515. TEditorList * EditorList = new TEditorList();
  3516. try
  3517. {
  3518. *EditorList = *WinConfiguration->EditorList;
  3519. for (int i = 0; i < EditorList->Count; i++)
  3520. {
  3521. const TEditorPreferences * Editor = EditorList->Editors[i];
  3522. if ((Editor->Data->Editor == edExternal) &&
  3523. (Editor->Data->ExternalEditor == Data->Command))
  3524. {
  3525. AnyFound = true;
  3526. if (Editor->Data->SDIExternalEditor)
  3527. {
  3528. AnyNonMDI = true;
  3529. if (Editor->Data->DetectMDIExternalEditor)
  3530. {
  3531. AnyDetect = true;
  3532. }
  3533. }
  3534. else
  3535. {
  3536. AnyMDI = true;
  3537. }
  3538. }
  3539. }
  3540. bool EnableMDI = false;
  3541. bool DisableDetect = false;
  3542. if (AnyMDI)
  3543. {
  3544. KeepOpen = true;
  3545. if (AnyNonMDI)
  3546. {
  3547. // there is at least one instance of the editor with MDI support enabled,
  3548. // and one with disabled, enable it for all instances
  3549. EnableMDI = true;
  3550. }
  3551. }
  3552. else if (AnyFound && !AnyDetect)
  3553. {
  3554. // at least once instance found but all have MDI autodetection disabled
  3555. // => close the file (default action)
  3556. }
  3557. else
  3558. {
  3559. // no instance of the editor has MDI support enabled
  3560. TMessageParams Params;
  3561. if (AnyFound)
  3562. {
  3563. // there is at least one instance of the editor
  3564. Params.Params |= mpNeverAskAgainCheck;
  3565. }
  3566. unsigned int Answer = MessageDialog(FMTLOAD(EDITOR_EARLY_CLOSED2, (Data->OriginalFileName)), qtWarning,
  3567. qaYes | qaNo, HELP_EDITOR_EARLY_CLOSED, &Params);
  3568. switch (Answer)
  3569. {
  3570. case qaNeverAskAgain:
  3571. DisableDetect = true;
  3572. break;
  3573. case qaNo:
  3574. EnableMDI = true;
  3575. KeepOpen = true;
  3576. break;
  3577. }
  3578. }
  3579. if (AnyFound && (EnableMDI || DisableDetect))
  3580. {
  3581. bool Changed = false;
  3582. for (int i = 0; i < EditorList->Count; i++)
  3583. {
  3584. const TEditorPreferences * Editor = EditorList->Editors[i];
  3585. if ((Editor->Data->Editor == edExternal) &&
  3586. (Editor->Data->ExternalEditor == Data->Command) &&
  3587. ((EnableMDI && Editor->Data->SDIExternalEditor) ||
  3588. (DisableDetect && Editor->Data->DetectMDIExternalEditor)))
  3589. {
  3590. Changed = true;
  3591. TEditorPreferences * UpdatedEditor = new TEditorPreferences(*Editor);
  3592. if (EnableMDI)
  3593. {
  3594. UpdatedEditor->GetData()->SDIExternalEditor = false;
  3595. }
  3596. if (DisableDetect)
  3597. {
  3598. UpdatedEditor->GetData()->DetectMDIExternalEditor = false;
  3599. }
  3600. EditorList->Change(i, UpdatedEditor);
  3601. }
  3602. }
  3603. if (Changed)
  3604. {
  3605. WinConfiguration->EditorList = EditorList;
  3606. }
  3607. }
  3608. }
  3609. __finally
  3610. {
  3611. delete EditorList;
  3612. }
  3613. }
  3614. else
  3615. {
  3616. // "open" case
  3617. MessageDialog(FMTLOAD(APP_EARLY_CLOSED, (Data->OriginalFileName)), qtWarning,
  3618. qaOK, HELP_APP_EARLY_CLOSED);
  3619. }
  3620. }
  3621. //---------------------------------------------------------------------------
  3622. void __fastcall TCustomScpExplorerForm::ExecutedFileUploadComplete(TObject * Sender)
  3623. {
  3624. EditorFormFileUploadComplete(DebugNotNull(dynamic_cast<TForm *>(Sender)));
  3625. }
  3626. //---------------------------------------------------------------------------
  3627. void __fastcall TCustomScpExplorerForm::RemoteDirViewEnter(TObject * /*Sender*/)
  3628. {
  3629. SideEnter(osRemote);
  3630. }
  3631. //---------------------------------------------------------------------------
  3632. void __fastcall TCustomScpExplorerForm::RemoteDriveViewEnter(TObject * /*Sender*/)
  3633. {
  3634. MakeNextInTabOrder(RemoteDirPanel, RemoteDrivePanel);
  3635. SideEnter(osRemote);
  3636. }
  3637. //---------------------------------------------------------------------------
  3638. void __fastcall TCustomScpExplorerForm::SideEnter(TOperationSide Side)
  3639. {
  3640. FCurrentSide = Side;
  3641. if (Visible)
  3642. {
  3643. UpdateControls();
  3644. }
  3645. }
  3646. //---------------------------------------------------------------------------
  3647. void __fastcall TCustomScpExplorerForm::DeleteFiles(TOperationSide Side,
  3648. TStrings * FileList, bool Alternative)
  3649. {
  3650. TCustomDirView * DView = DirView(Side);
  3651. DView->SaveSelection();
  3652. DView->SaveSelectedNames();
  3653. DebugAssert(!FAlternativeDelete);
  3654. FAlternativeDelete = Alternative;
  3655. try
  3656. {
  3657. if (!IsSideLocalBrowser(Side))
  3658. {
  3659. DebugAssert(Terminal != NULL);
  3660. Terminal->DeleteFiles(FileList, FLAGMASK(Alternative, dfAlternative));
  3661. }
  3662. else
  3663. {
  3664. try
  3665. {
  3666. TTerminalManager::Instance()->LocalTerminal->DeleteLocalFiles(FileList, FLAGMASK(Alternative, dfAlternative));
  3667. }
  3668. __finally
  3669. {
  3670. ReloadLocalDirectory();
  3671. }
  3672. }
  3673. FAlternativeDelete = false;
  3674. }
  3675. catch(...)
  3676. {
  3677. FAlternativeDelete = false;
  3678. DView->DiscardSavedSelection();
  3679. throw;
  3680. }
  3681. DView->RestoreSelection();
  3682. }
  3683. //---------------------------------------------------------------------------
  3684. void __fastcall TCustomScpExplorerForm::LockFiles(TStrings * FileList, bool Lock)
  3685. {
  3686. DebugAssert(Terminal);
  3687. DebugAssert(!IsLocalBrowserMode());
  3688. RemoteDirView->SaveSelection();
  3689. RemoteDirView->SaveSelectedNames();
  3690. try
  3691. {
  3692. if (Lock)
  3693. {
  3694. Terminal->LockFiles(FileList);
  3695. }
  3696. else
  3697. {
  3698. Terminal->UnlockFiles(FileList);
  3699. }
  3700. }
  3701. catch(...)
  3702. {
  3703. RemoteDirView->DiscardSavedSelection();
  3704. throw;
  3705. }
  3706. RemoteDirView->RestoreSelection();
  3707. }
  3708. //---------------------------------------------------------------------------
  3709. bool __fastcall TCustomScpExplorerForm::RemoteTransferDialog(TManagedTerminal *& Session,
  3710. TStrings * FileList, UnicodeString & Target, UnicodeString & FileMask, bool & DirectCopy,
  3711. bool NoConfirmation, bool Move)
  3712. {
  3713. DebugAssert(Terminal != NULL);
  3714. DebugAssert(!IsLocalBrowserMode());
  3715. // update Terminal->StateData->RemoteDirectory
  3716. UpdateTerminal(Terminal);
  3717. if (Session == NULL)
  3718. {
  3719. Session = TTerminalManager::Instance()->ActiveTerminal;
  3720. }
  3721. if (Session == TTerminalManager::Instance()->ActiveTerminal)
  3722. {
  3723. if (RemoteDriveView->DropTarget != NULL)
  3724. {
  3725. Target = RemoteDriveView->NodePathName(RemoteDriveView->DropTarget);
  3726. }
  3727. else if (RemoteDirView->DropTarget != NULL)
  3728. {
  3729. DebugAssert(RemoteDirView->ItemIsDirectory(RemoteDirView->DropTarget));
  3730. Target = RemoteDirView->ItemFullFileName(RemoteDirView->DropTarget);
  3731. }
  3732. else
  3733. {
  3734. Target = RemoteDirView->Path;
  3735. }
  3736. }
  3737. else
  3738. {
  3739. Target = Session->StateData->RemoteDirectory;
  3740. }
  3741. Target = UnixIncludeTrailingBackslash(Target);
  3742. if (FileList->Count == 1)
  3743. {
  3744. FileMask = DelimitFileNameMask(UnixExtractFileName(FileList->Strings[0]));
  3745. }
  3746. else
  3747. {
  3748. FileMask = AnyMask;
  3749. }
  3750. DirectCopy = FTerminal->IsCapable[fcRemoteCopy] || FTerminal->IsCapable[fcSecondaryShell];
  3751. bool Result = true;
  3752. if (!NoConfirmation)
  3753. {
  3754. bool Multi = (FileList->Count > 1);
  3755. if (Move)
  3756. {
  3757. Result = DoRemoteMoveDialog(Multi, Target, FileMask);
  3758. }
  3759. else
  3760. {
  3761. TStrings * Sessions = TTerminalManager::Instance()->TerminalList;
  3762. TStrings * Directories = new TStringList;
  3763. try
  3764. {
  3765. for (int Index = 0; Index < Sessions->Count; Index++)
  3766. {
  3767. TManagedTerminal * Terminal =
  3768. dynamic_cast<TManagedTerminal *>(Sessions->Objects[Index]);
  3769. Directories->Add(Terminal->StateData->RemoteDirectory);
  3770. }
  3771. TDirectRemoteCopy AllowDirectCopy;
  3772. if (FTerminal->IsCapable[fcRemoteCopy] || FTerminal->CommandSessionOpened)
  3773. {
  3774. DebugAssert(DirectCopy);
  3775. AllowDirectCopy = drcAllow;
  3776. }
  3777. else if (FTerminal->IsCapable[fcSecondaryShell])
  3778. {
  3779. DebugAssert(DirectCopy);
  3780. AllowDirectCopy = drcConfirmCommandSession;
  3781. }
  3782. else
  3783. {
  3784. DebugAssert(!DirectCopy);
  3785. AllowDirectCopy = drcDisallow;
  3786. }
  3787. void * ASession = Session;
  3788. Result = DoRemoteCopyDialog(Sessions, Directories, AllowDirectCopy,
  3789. Multi, ASession, Target, FileMask, DirectCopy, TTerminalManager::Instance()->ActiveTerminal);
  3790. Session = static_cast<TManagedTerminal *>(ASession);
  3791. }
  3792. __finally
  3793. {
  3794. delete Directories;
  3795. }
  3796. }
  3797. }
  3798. return Result;
  3799. }
  3800. //---------------------------------------------------------------------------
  3801. bool __fastcall TCustomScpExplorerForm::RemoteTransferFiles(
  3802. TStrings * FileList, bool NoConfirmation, bool Move, TManagedTerminal * Session)
  3803. {
  3804. DebugAssert(!IsLocalBrowserMode());
  3805. bool DirectCopy;
  3806. UnicodeString Target, FileMask;
  3807. bool Result = RemoteTransferDialog(Session, FileList, Target, FileMask, DirectCopy, NoConfirmation, Move);
  3808. if (Result)
  3809. {
  3810. if (!Move && !DirectCopy)
  3811. {
  3812. Configuration->Usage->Inc("RemoteCopyTemp");
  3813. UnicodeString RootTempDir;
  3814. UnicodeString TempDir;
  3815. TemporarilyDownloadFiles(FileList, false, RootTempDir, TempDir, false, false, false);
  3816. TStrings * TemporaryFilesList = new TStringList();
  3817. try
  3818. {
  3819. TMakeLocalFileListParams MakeFileListParam;
  3820. MakeFileListParam.FileList = TemporaryFilesList;
  3821. MakeFileListParam.FileTimes = NULL;
  3822. MakeFileListParam.IncludeDirs = true;
  3823. MakeFileListParam.Recursive = false;
  3824. ProcessLocalDirectory(TempDir, FTerminal->MakeLocalFileList, &MakeFileListParam);
  3825. TTerminalManager::Instance()->ActiveTerminal = Session;
  3826. if (TemporaryFilesList->Count > 0)
  3827. {
  3828. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  3829. CopyParam.FileMask = FileMask;
  3830. DebugAssert(!FAutoOperation);
  3831. FAutoOperation = true;
  3832. FTerminal->CopyToRemote(TemporaryFilesList, Target, &CopyParam, cpTemporary, NULL);
  3833. }
  3834. }
  3835. __finally
  3836. {
  3837. delete TemporaryFilesList;
  3838. FAutoOperation = false;
  3839. if (!RootTempDir.IsEmpty())
  3840. {
  3841. RecursiveDeleteFile(ExcludeTrailingBackslash(RootTempDir), false);
  3842. }
  3843. }
  3844. }
  3845. else
  3846. {
  3847. RemoteDirView->SaveSelection();
  3848. RemoteDirView->SaveSelectedNames();
  3849. try
  3850. {
  3851. if (Move)
  3852. {
  3853. Configuration->Usage->Inc("RemoteMove");
  3854. Terminal->MoveFiles(FileList, Target, FileMask);
  3855. }
  3856. else
  3857. {
  3858. Configuration->Usage->Inc("RemoteCopyDirect");
  3859. DebugAssert(DirectCopy);
  3860. DebugAssert(Session == FTerminal);
  3861. if (FTerminal->IsCapable[fcRemoteCopy] ||
  3862. FTerminal->CommandSessionOpened ||
  3863. CommandSessionFallback())
  3864. {
  3865. Terminal->CopyFiles(FileList, Target, FileMask);
  3866. }
  3867. }
  3868. }
  3869. catch(...)
  3870. {
  3871. RemoteDirView->DiscardSavedSelection();
  3872. throw;
  3873. }
  3874. RemoteDirView->RestoreSelection();
  3875. }
  3876. }
  3877. return Result;
  3878. }
  3879. //---------------------------------------------------------------------------
  3880. void __fastcall TCustomScpExplorerForm::CreateRemoteDirectory(
  3881. const UnicodeString & Path, TRemoteProperties & Properties)
  3882. {
  3883. DebugAssert(!IsLocalBrowserMode());
  3884. Properties.Valid = Properties.Valid << vpEncrypt;
  3885. Properties.Encrypt = GUIConfiguration->CurrentCopyParam.EncryptNewFiles;
  3886. RemoteDirView->CreateDirectoryEx(Path, &Properties);
  3887. }
  3888. //---------------------------------------------------------------------------
  3889. void __fastcall TCustomScpExplorerForm::CreateDirectory(TOperationSide Side)
  3890. {
  3891. Side = GetSide(Side);
  3892. TRemoteProperties Properties = GUIConfiguration->NewDirectoryProperties;
  3893. TRemoteProperties * AProperties = (!IsSideLocalBrowser(Side) ? &Properties : NULL);
  3894. UnicodeString Name = LoadStr(NEW_FOLDER);
  3895. int AllowedChanges =
  3896. FLAGMASK(!IsSideLocalBrowser(Side) && Terminal->IsCapable[fcModeChanging], cpMode);
  3897. bool SaveSettings = false;
  3898. if (DoCreateDirectoryDialog(Name, AProperties, AllowedChanges, SaveSettings))
  3899. {
  3900. TWindowLock Lock(this);
  3901. if (!IsSideLocalBrowser(Side))
  3902. {
  3903. if (SaveSettings)
  3904. {
  3905. GUIConfiguration->NewDirectoryProperties = Properties;
  3906. }
  3907. CreateRemoteDirectory(Name, Properties);
  3908. }
  3909. else
  3910. {
  3911. DirView(Side)->CreateDirectory(Name);
  3912. }
  3913. }
  3914. }
  3915. //---------------------------------------------------------------------------
  3916. void __fastcall TCustomScpExplorerForm::HomeDirectory(TOperationSide Side)
  3917. {
  3918. TWindowLock Lock(this);
  3919. DirView(Side)->ExecuteHomeDirectory();
  3920. }
  3921. //---------------------------------------------------------------------------
  3922. void __fastcall TCustomScpExplorerForm::OpenDirectory(TOperationSide Side)
  3923. {
  3924. DoOpenDirectoryDialog(odBrowse, Side);
  3925. }
  3926. //---------------------------------------------------------------------------
  3927. bool __fastcall TCustomScpExplorerForm::OpenBookmark(TOperationSide Side, TBookmark * Bookmark)
  3928. {
  3929. UnicodeString Path = Bookmark->GetSideDirectory(Side);
  3930. bool Result = !Path.IsEmpty();
  3931. if (Result)
  3932. {
  3933. // While we might get here when the session is closed (from location profiles),
  3934. // it's not a problem as the Path setter is noop then
  3935. DirView(Side)->Path = Path;
  3936. }
  3937. return Result;
  3938. }
  3939. //---------------------------------------------------------------------------
  3940. void __fastcall TCustomScpExplorerForm::CalculateSize(
  3941. TStrings * FileList, __int64 & Size, TCalculateSizeStats & Stats,
  3942. bool & Close)
  3943. {
  3944. // terminal can be already closed (e.g. dropped connection)
  3945. if (Terminal != NULL)
  3946. {
  3947. try
  3948. {
  3949. Terminal->CalculateFilesSize(FileList, Size, 0, NULL, true, Stats);
  3950. }
  3951. catch(...)
  3952. {
  3953. if (!Terminal->Active)
  3954. {
  3955. Close = true;
  3956. }
  3957. throw;
  3958. }
  3959. }
  3960. }
  3961. //---------------------------------------------------------------------------
  3962. void __fastcall TCustomScpExplorerForm::CalculateChecksum(const UnicodeString & Alg,
  3963. TStrings * FileList, TCalculatedChecksumEvent OnCalculatedChecksum,
  3964. bool & Close)
  3965. {
  3966. // terminal can be already closed (e.g. dropped connection)
  3967. if (Terminal != NULL)
  3968. {
  3969. Configuration->Usage->Inc(L"ChecksumCalculated");
  3970. try
  3971. {
  3972. Terminal->CalculateFilesChecksum(Alg, FileList, NULL, OnCalculatedChecksum);
  3973. }
  3974. catch(...)
  3975. {
  3976. if (!Terminal->Active)
  3977. {
  3978. Close = true;
  3979. }
  3980. throw;
  3981. }
  3982. }
  3983. }
  3984. //---------------------------------------------------------------------------
  3985. bool __fastcall TCustomScpExplorerForm::SetProperties(TOperationSide Side, TStrings * FileList)
  3986. {
  3987. bool Result;
  3988. if (!IsSideLocalBrowser(Side))
  3989. {
  3990. TRemoteTokenList * GroupList = NULL;
  3991. TRemoteTokenList * UserList = NULL;
  3992. try
  3993. {
  3994. TRemoteProperties CurrentProperties;
  3995. if (Terminal->LoadFilesProperties(FileList))
  3996. {
  3997. RemoteDirView->Invalidate();
  3998. }
  3999. bool CapableGroupChanging = Terminal->IsCapable[fcGroupChanging];
  4000. bool CapableOwnerChanging = Terminal->IsCapable[fcOwnerChanging];
  4001. if (CapableGroupChanging || CapableOwnerChanging)
  4002. {
  4003. if (CapableGroupChanging)
  4004. {
  4005. GroupList = Terminal->Groups->Duplicate();
  4006. }
  4007. if (CapableOwnerChanging)
  4008. {
  4009. UserList = Terminal->Users->Duplicate();
  4010. }
  4011. TRemoteDirectory * Files = Terminal->Files;
  4012. int Count = Files->Count;
  4013. if (Count > 100)
  4014. {
  4015. Count = 100;
  4016. }
  4017. for (int Index = 0; Index < Count; Index++)
  4018. {
  4019. TRemoteFile * File = Files->Files[Index];
  4020. if (CapableGroupChanging)
  4021. {
  4022. GroupList->AddUnique(File->Group);
  4023. }
  4024. if (CapableOwnerChanging)
  4025. {
  4026. UserList->AddUnique(File->Owner);
  4027. }
  4028. }
  4029. // if we haven't collected tokens for all files in current directory,
  4030. // make sure we collect them at least for all selected files.
  4031. // (note that so far the files in FileList has to be from current direcotry)
  4032. if (Count < Files->Count)
  4033. {
  4034. for (int Index = 0; Index < FileList->Count; Index++)
  4035. {
  4036. TRemoteFile * File = (TRemoteFile *)(FileList->Objects[Index]);
  4037. if (CapableGroupChanging)
  4038. {
  4039. GroupList->AddUnique(File->Group);
  4040. }
  4041. if (CapableOwnerChanging)
  4042. {
  4043. UserList->AddUnique(File->Owner);
  4044. }
  4045. }
  4046. }
  4047. }
  4048. CurrentProperties = TRemoteProperties::CommonProperties(FileList);
  4049. int Flags = 0;
  4050. if (Terminal->IsCapable[fcModeChanging]) Flags |= cpMode;
  4051. if (CapableOwnerChanging) Flags |= cpOwner;
  4052. if (CapableGroupChanging) Flags |= cpGroup;
  4053. TCalculateChecksumEvent CalculateChecksumEvent = NULL;
  4054. if (Terminal->IsCapable[fcCalculatingChecksum])
  4055. {
  4056. CalculateChecksumEvent = CalculateChecksum;
  4057. }
  4058. std::unique_ptr<TStrings> ChecksumAlgs(new TStringList());
  4059. Terminal->GetSupportedChecksumAlgs(ChecksumAlgs.get());
  4060. TRemoteProperties NewProperties = CurrentProperties;
  4061. Result =
  4062. DoPropertiesDialog(FileList, RemoteDirView->PathName,
  4063. GroupList, UserList, ChecksumAlgs.get(), &NewProperties, Flags,
  4064. Terminal->IsCapable[fcGroupOwnerChangingByID],
  4065. CalculateSize, CalculateChecksumEvent);
  4066. if (Result)
  4067. {
  4068. NewProperties = TRemoteProperties::ChangedProperties(CurrentProperties, NewProperties);
  4069. Terminal->ChangeFilesProperties(FileList, &NewProperties);
  4070. }
  4071. }
  4072. __finally
  4073. {
  4074. delete GroupList;
  4075. delete UserList;
  4076. }
  4077. }
  4078. else
  4079. {
  4080. DirView(Side)->DisplayPropertiesMenu();
  4081. Result = true;
  4082. }
  4083. return Result;
  4084. }
  4085. //---------------------------------------------------------------------------
  4086. void __fastcall TCustomScpExplorerForm::KeyProcessed(Word & Key, TShiftState Shift)
  4087. {
  4088. if (Shift * AllKeyShiftStates() == (TShiftState() << ssAlt))
  4089. {
  4090. FIgnoreNextDialogChar = Key;
  4091. }
  4092. Key = 0;
  4093. }
  4094. //---------------------------------------------------------------------------
  4095. void __fastcall TCustomScpExplorerForm::CheckCustomCommandShortCut(
  4096. TCustomCommandList * List, Word & Key, Classes::TShiftState Shift, TShortCut KeyShortCut)
  4097. {
  4098. const TCustomCommandType * Command = List->Find(KeyShortCut);
  4099. if (Command != NULL)
  4100. {
  4101. KeyProcessed(Key, Shift);
  4102. if (CustomCommandState(*Command, false, ccltAll) > 0)
  4103. {
  4104. TAutoFlag DontCopyCommandToClipboardFlag(DontCopyCommandToClipboard);
  4105. ExecuteFileOperationCommand(foCustomCommand, osRemote,
  4106. false, false, const_cast<TCustomCommandType *>(Command));
  4107. }
  4108. }
  4109. }
  4110. //---------------------------------------------------------------------------
  4111. void __fastcall TCustomScpExplorerForm::KeyDown(Word & Key, Classes::TShiftState Shift)
  4112. {
  4113. if (QueueView3->Focused() && (QueueView3->OnKeyDown != NULL))
  4114. {
  4115. QueueView3->OnKeyDown(QueueView3, Key, Shift);
  4116. }
  4117. if (!DirView(osCurrent)->IsEditing())
  4118. {
  4119. TShortCut KeyShortCut = ShortCut(Key, Shift);
  4120. for (int Index = 0; Index < NonVisualDataModule->ExplorerActions->ActionCount; Index++)
  4121. {
  4122. TAction * Action = (TAction *)NonVisualDataModule->ExplorerActions->Actions[Index];
  4123. if (((Action->ShortCut == KeyShortCut) ||
  4124. (Action->SecondaryShortCuts->IndexOfShortCut(KeyShortCut) >= 0)) &&
  4125. AllowedAction(Action, aaShortCut))
  4126. {
  4127. // Has to be called before the action as the dialog char is already in queue.
  4128. // So when the action consumes message queue, we already need to have the
  4129. // FIgnoreNextDialogChar set
  4130. KeyProcessed(Key, Shift);
  4131. // Reset reference to previous component (when menu/toolbar was clicked).
  4132. // Needed to detect that action was invoked by keyboard shortcut
  4133. // in TNonVisualDataModule::ExplorerActionsExecute
  4134. Action->ActionComponent = NULL;
  4135. Action->Execute();
  4136. return;
  4137. }
  4138. }
  4139. for (int i = 0; i < TTerminalManager::Instance()->Count; i++)
  4140. {
  4141. if (NonVisualDataModule->OpenSessionShortCut(i) == KeyShortCut)
  4142. {
  4143. KeyProcessed(Key, Shift);
  4144. TTerminalManager::Instance()->ActiveTerminalIndex = i;
  4145. return;
  4146. }
  4147. }
  4148. if (Key == VK_TAB && Shift.Contains(ssCtrl))
  4149. {
  4150. KeyProcessed(Key, Shift);
  4151. TTerminalManager::Instance()->CycleTerminals(!Shift.Contains(ssShift));
  4152. }
  4153. TShortCut CustomShortCut = NormalizeCustomShortCut(KeyShortCut);
  4154. if (IsCustomShortCut(CustomShortCut))
  4155. {
  4156. CheckCustomCommandShortCut(WinConfiguration->CustomCommandList, Key, Shift, CustomShortCut);
  4157. CheckCustomCommandShortCut(WinConfiguration->ExtensionList, Key, Shift, CustomShortCut);
  4158. if (WinConfiguration->SharedBookmarks != NULL)
  4159. {
  4160. TBookmark * Bookmark = WinConfiguration->SharedBookmarks->FindByShortCut(CustomShortCut);
  4161. if ((Bookmark != NULL) &&
  4162. OpenBookmark(FCurrentSide, Bookmark))
  4163. {
  4164. KeyProcessed(Key, Shift);
  4165. }
  4166. }
  4167. }
  4168. }
  4169. TForm::KeyDown(Key, Shift);
  4170. }
  4171. //---------------------------------------------------------------------------
  4172. void __fastcall TCustomScpExplorerForm::InitStatusBar()
  4173. {
  4174. DebugAssert(Terminal != NULL);
  4175. const TSessionInfo & SessionInfo = Terminal->GetSessionInfo();
  4176. const TFileSystemInfo & FileSystemInfo = Terminal->GetFileSystemInfo();
  4177. TTBXStatusBar * SessionStatusBar = (TTBXStatusBar *)GetComponent(fcStatusBar);
  4178. int Offset = SessionStatusBar->Panels->Count - SessionPanelCount;
  4179. bool SecurityEnabled = !SessionInfo.SecurityProtocolName.IsEmpty();
  4180. SessionStatusBar->Panels->Items[Offset + 0]->Enabled = SecurityEnabled;
  4181. // expanded from ?: to avoid memory leaks
  4182. if (SecurityEnabled)
  4183. {
  4184. SessionStatusBar->Panels->Items[Offset + 0]->Hint =
  4185. FMTLOAD(STATUS_SECURE, (SessionInfo.SecurityProtocolName));
  4186. }
  4187. else
  4188. {
  4189. SessionStatusBar->Panels->Items[Offset + 0]->Hint = LoadStr(STATUS_INSECURE);
  4190. }
  4191. if (FileSystemInfo.ProtocolName.IsEmpty())
  4192. {
  4193. SessionStatusBar->Panels->Items[Offset + 1]->Caption = SessionInfo.ProtocolName;
  4194. }
  4195. else
  4196. {
  4197. SessionStatusBar->Panels->Items[Offset + 1]->Caption = FileSystemInfo.ProtocolName;
  4198. }
  4199. SessionStatusBar->Panels->Items[Offset + 1]->Hint = LoadStr(STATUS_PROTOCOL_HINT);
  4200. SessionStatusBar->Panels->Items[Offset + 2]->Enabled =
  4201. (!SessionInfo.CSCompression.IsEmpty() || !SessionInfo.SCCompression.IsEmpty());
  4202. UnicodeString CSCompressionStr = DefaultStr(SessionInfo.CSCompression, LoadStr(NO_STR));
  4203. UnicodeString SCCompressionStr = DefaultStr(SessionInfo.SCCompression, LoadStr(NO_STR));
  4204. UnicodeString CompressionStr = CSCompressionStr;
  4205. if (CSCompressionStr != SCCompressionStr)
  4206. {
  4207. CompressionStr += UnicodeString(L"/") + SCCompressionStr;
  4208. }
  4209. SessionStatusBar->Panels->Items[Offset + 2]->Hint = FMTLOAD(STATUS_COMPRESSION_HINT, (CompressionStr));
  4210. SessionStatusBar->Panels->Items[Offset + 3]->Hint = LoadStr(STATUS_DURATION_HINT);
  4211. UpdateStatusBar();
  4212. }
  4213. //---------------------------------------------------------------------------
  4214. void __fastcall TCustomScpExplorerForm::UpdateStatusBar()
  4215. {
  4216. TTBXStatusBar * SessionStatusBar = (TTBXStatusBar *)GetComponent(fcStatusBar);
  4217. DebugAssert(SessionStatusBar != NULL);
  4218. if (!Terminal || !Terminal->Active || Terminal->Status < ssOpened)
  4219. {
  4220. // note: (Terminal->Status < sshReady) currently never happens here,
  4221. // so STATUS_CONNECTING is never used
  4222. SessionStatusBar->SimplePanel = true;
  4223. SessionStatusBar->SimpleText = LoadStr(
  4224. !Terminal || !Terminal->Active ? STATUS_NOT_CONNECTED : STATUS_CONNECTING);
  4225. }
  4226. else
  4227. {
  4228. DebugAssert(Terminal);
  4229. SessionStatusBar->SimplePanel = false;
  4230. const TSessionInfo & SessionInfo = Terminal->GetSessionInfo();
  4231. if (!FNote.IsEmpty())
  4232. {
  4233. SessionStatusBar->Panels->Items[0]->Caption = FNote;
  4234. }
  4235. else
  4236. {
  4237. UpdateStatusPanelText(SessionStatusBar->Panels->Items[0]);
  4238. }
  4239. SessionStatusBar->Panels->Items[0]->Hint = FNoteHints;
  4240. SessionStatusBar->Panels->Items[SessionStatusBar->Panels->Count - 1]->Caption =
  4241. FormatDateTimeSpan(Configuration->TimeFormat, Now() - SessionInfo.LoginTime);
  4242. }
  4243. }
  4244. //---------------------------------------------------------------------------
  4245. void __fastcall TCustomScpExplorerForm::UpdateStatusPanelText(TTBXStatusPanel * Panel)
  4246. {
  4247. Panel->Caption = L"";
  4248. }
  4249. //---------------------------------------------------------------------------
  4250. void __fastcall TCustomScpExplorerForm::Idle()
  4251. {
  4252. if (FShowing || FStandaloneEditing)
  4253. {
  4254. FEditorManager->Check();
  4255. // make sure that Idle is called before update queue, as it may invoke QueueEvent
  4256. // that needs to know if queue view is visible (and it may be closed after queue update)
  4257. TTerminalManager::Instance()->Idle(FDoNotIdleCurrentTerminal > 0);
  4258. }
  4259. if (FShowing)
  4260. {
  4261. if (!NonVisualDataModule->Busy &&
  4262. // Menu is opened or drag&drop is going on
  4263. (Mouse->Capture == NULL))
  4264. {
  4265. if (FRefreshRemoteDirectory)
  4266. {
  4267. if ((Terminal != NULL) && (Terminal->Status == ssOpened))
  4268. {
  4269. Terminal->RefreshDirectory();
  4270. }
  4271. FRefreshRemoteDirectory = false;
  4272. }
  4273. if (FRefreshLocalDirectory)
  4274. {
  4275. ReloadLocalDirectory();
  4276. FRefreshLocalDirectory = false;
  4277. }
  4278. if (WinConfiguration->RefreshRemotePanel)
  4279. {
  4280. if ((Terminal != NULL) && (Terminal->Status == ssOpened) &&
  4281. (Now() - Terminal->DirectoryLoaded > WinConfiguration->RefreshRemotePanelInterval))
  4282. {
  4283. RemoteDirView->ReloadDirectory();
  4284. }
  4285. }
  4286. }
  4287. }
  4288. if (FShowing || FStandaloneEditing)
  4289. {
  4290. if (FQueueStatusInvalidated)
  4291. {
  4292. UpdateQueueStatus(false);
  4293. }
  4294. RefreshQueueItems();
  4295. }
  4296. if (FShowing)
  4297. {
  4298. UpdateStatusBar();
  4299. }
  4300. FIgnoreNextDialogChar = 0;
  4301. }
  4302. //---------------------------------------------------------------------------
  4303. void __fastcall TCustomScpExplorerForm::UserActionTimer(TObject * /*Sender*/)
  4304. {
  4305. try
  4306. {
  4307. FUserActionTimer->Enabled = false;
  4308. if (IsQueueAutoPopup() && (FPendingQueueActionItem != NULL))
  4309. {
  4310. if (TQueueItem::IsUserActionStatus(FPendingQueueActionItem->Status))
  4311. {
  4312. FPendingQueueActionItem->ProcessUserAction();
  4313. }
  4314. }
  4315. }
  4316. __finally
  4317. {
  4318. FPendingQueueActionItem = NULL;
  4319. }
  4320. }
  4321. //---------------------------------------------------------------------------
  4322. void __fastcall TCustomScpExplorerForm::ApplicationMinimize(TObject * /*Sender*/)
  4323. {
  4324. if (WinConfiguration->MinimizeToTray)
  4325. {
  4326. UpdateTrayIcon();
  4327. FTrayIcon->Visible = true;
  4328. if (Visible)
  4329. {
  4330. ShowWindow(Handle, SW_HIDE);
  4331. }
  4332. }
  4333. }
  4334. //---------------------------------------------------------------------------
  4335. void __fastcall TCustomScpExplorerForm::ApplicationRestore(TObject * /*Sender*/)
  4336. {
  4337. // WORKAROUND
  4338. // When restoring maximized window from minimization,
  4339. // rarely some controls do not align properly.
  4340. // Two instances seen (both for Commander):
  4341. // - When restoring, window is temporarily narrower (not maximized),
  4342. // causing toolbars on TopDock to wrap and dock to expand horizontally.
  4343. // Once maximized already, top dock shinks back, but the session PageControl,
  4344. // do not align up, leaving space between TopDock and PageControl.
  4345. // - Similar issue seen with LocalDirView not aligning down to status bar.
  4346. for (int Index = 0; Index < ControlCount; Index++)
  4347. {
  4348. RealignControl(Controls[Index]);
  4349. }
  4350. if (FTrayIcon->Visible)
  4351. {
  4352. FTrayIcon->Visible = false;
  4353. if (Visible)
  4354. {
  4355. ShowWindow(Handle, SW_SHOW);
  4356. }
  4357. }
  4358. if (FNeedSession && DebugAlwaysTrue(Terminal == NULL))
  4359. {
  4360. FNeedSession = false;
  4361. NonVisualDataModule->StartBusy();
  4362. try
  4363. {
  4364. NeedSession(true);
  4365. }
  4366. __finally
  4367. {
  4368. NonVisualDataModule->EndBusy();
  4369. }
  4370. }
  4371. }
  4372. //---------------------------------------------------------------------------
  4373. void __fastcall TCustomScpExplorerForm::UpdateTrayIcon()
  4374. {
  4375. FTrayIcon->Hint = GetMainForm()->Caption;
  4376. }
  4377. //---------------------------------------------------------------------------
  4378. void __fastcall TCustomScpExplorerForm::ApplicationTitleChanged()
  4379. {
  4380. UpdateTrayIcon();
  4381. }
  4382. //---------------------------------------------------------------------------
  4383. void __fastcall TCustomScpExplorerForm::RestoreApp()
  4384. {
  4385. // workaround
  4386. // TApplication.WndProc sets TApplication.FAppIconic to false,
  4387. // on WM_ACTIVATEAPP, what renders TApplication.Restore no-op function.
  4388. // But WM_ACTIVATEAPP message can be received even when
  4389. // the main window is minimized to the tray and internal editor window is focused
  4390. // (after another application was previously active)
  4391. if (::IsIconic(Handle))
  4392. {
  4393. if (!IsAppIconic())
  4394. {
  4395. SetAppIconic(true);
  4396. }
  4397. }
  4398. ::ApplicationRestore();
  4399. Application->BringToFront();
  4400. }
  4401. //---------------------------------------------------------------------------
  4402. void __fastcall TCustomScpExplorerForm::TrayIconClick(TObject * /*Sender*/)
  4403. {
  4404. RestoreApp();
  4405. }
  4406. //---------------------------------------------------------------------------
  4407. void __fastcall TCustomScpExplorerForm::NewSession(bool FromSite, const UnicodeString & SessionUrl)
  4408. {
  4409. if (OpenInNewWindow())
  4410. {
  4411. // todo: Pass FromSite
  4412. ExecuteNewInstance(SessionUrl);
  4413. }
  4414. else
  4415. {
  4416. TTerminalManager::Instance()->NewSession(FromSite, SessionUrl);
  4417. }
  4418. }
  4419. //---------------------------------------------------------------------------
  4420. UnicodeString __fastcall TCustomScpExplorerForm::CreateHiddenDuplicateSession()
  4421. {
  4422. UnicodeString SessionName = StoredSessions->HiddenPrefix + Terminal->SessionData->SessionName;
  4423. // current working directories become defaults here, what is not right
  4424. std::unique_ptr<TSessionData> SessionData(CloneCurrentSessionData());
  4425. StoredSessions->NewSession(SessionName, SessionData.get());
  4426. // modified only, explicit
  4427. StoredSessions->Save(false, true);
  4428. // encode session name because of slashes in hierarchical sessions
  4429. return EncodeUrlString(SessionName);
  4430. }
  4431. //---------------------------------------------------------------------------
  4432. void __fastcall TCustomScpExplorerForm::DuplicateSession()
  4433. {
  4434. if (OpenInNewWindow())
  4435. {
  4436. ExecuteNewInstance(CreateHiddenDuplicateSession());
  4437. }
  4438. else
  4439. {
  4440. // current working directories become defaults here, what is not right
  4441. std::unique_ptr<TSessionData> SessionData(CloneCurrentSessionData());
  4442. TTerminalManager * Manager = TTerminalManager::Instance();
  4443. TManagedTerminal * Terminal = Manager->NewManagedTerminal(SessionData.get());
  4444. Manager->ActiveTerminal = Terminal;
  4445. }
  4446. }
  4447. //---------------------------------------------------------------------------
  4448. void __fastcall TCustomScpExplorerForm::RenameSession()
  4449. {
  4450. UnicodeString Name = Terminal->SessionData->SessionName;
  4451. if (InputDialog(LoadStr(RENAME_SESSION_TITLE), LoadStr(RENAME_SESSION_PROMPT), Name, HELP_SESSION_RENAME))
  4452. {
  4453. // Checks for a slash only, so it's not that big deal that we do the check after submitting the dialog only.
  4454. TSessionData::ValidateName(Name);
  4455. // This is inconsistent with how color (for example) is handled.
  4456. Terminal->SessionData->Name = Name;
  4457. UpdateControls();
  4458. // Add/Remove distinguishing paths from sessions of the same name.
  4459. DoTerminalListChanged();
  4460. }
  4461. }
  4462. //---------------------------------------------------------------------------
  4463. bool __fastcall TCustomScpExplorerForm::CanCloseQueue(TTerminalQueue * Queue)
  4464. {
  4465. DebugAssert(Queue != NULL);
  4466. bool Result = Queue->IsEmpty;
  4467. if (!Result)
  4468. {
  4469. SetFocus();
  4470. Result = (MessageDialog(LoadStr(PENDING_QUEUE_ITEMS2), qtWarning, qaOK | qaCancel, HELP_NONE) == qaOK);
  4471. }
  4472. return Result;
  4473. }
  4474. //---------------------------------------------------------------------------
  4475. bool __fastcall TCustomScpExplorerForm::CanCloseQueue()
  4476. {
  4477. return CanCloseQueue(FQueue);
  4478. }
  4479. //---------------------------------------------------------------------------
  4480. void __fastcall TCustomScpExplorerForm::CloseSession()
  4481. {
  4482. if (CanCloseQueue())
  4483. {
  4484. TTerminalManager::Instance()->FreeActiveTerminal();
  4485. }
  4486. }
  4487. //---------------------------------------------------------------------------
  4488. void __fastcall TCustomScpExplorerForm::DisconnectSession()
  4489. {
  4490. if (CanCloseQueue())
  4491. {
  4492. TTerminalManager::Instance()->DisconnectActiveTerminal();
  4493. }
  4494. }
  4495. //---------------------------------------------------------------------------
  4496. void __fastcall TCustomScpExplorerForm::TerminalDisconnected()
  4497. {
  4498. RemoteDirView->Terminal = Terminal;
  4499. UpdateRemotePathComboBox(false);
  4500. UpdateControls();
  4501. }
  4502. //---------------------------------------------------------------------------
  4503. void __fastcall TCustomScpExplorerForm::ReconnectSession()
  4504. {
  4505. if (DebugAlwaysTrue(!Terminal->Active))
  4506. {
  4507. TTerminalManager::Instance()->ReconnectActiveTerminal();
  4508. }
  4509. }
  4510. //---------------------------------------------------------------------------
  4511. void __fastcall TCustomScpExplorerForm::OpenStoredSession(TSessionData * Data)
  4512. {
  4513. if (OpenInNewWindow())
  4514. {
  4515. // encode session name because of slashes in hierarchical sessions
  4516. ExecuteNewInstance(EncodeUrlString(Data->Name));
  4517. }
  4518. else
  4519. {
  4520. TTerminalManager * Manager = TTerminalManager::Instance();
  4521. TManagedTerminal * Terminal = Manager->NewManagedTerminal(Data);
  4522. Manager->ActiveTerminal = Terminal;
  4523. }
  4524. }
  4525. //---------------------------------------------------------------------------
  4526. void __fastcall TCustomScpExplorerForm::DoOpenFolderOrWorkspace(const UnicodeString & Name, bool ConnectFirstTerminal)
  4527. {
  4528. TTerminalManager * Manager = TTerminalManager::Instance();
  4529. std::unique_ptr<TObjectList> DataList(new TObjectList());
  4530. StoredSessions->GetFolderOrWorkspace(Name, DataList.get());
  4531. TManagedTerminal * FirstTerminal = Manager->NewTerminals(DataList.get());
  4532. if (!ConnectFirstTerminal)
  4533. {
  4534. FirstTerminal->Disconnected = true;
  4535. FirstTerminal->DisconnectedTemporarily = true;
  4536. }
  4537. Manager->ActiveTerminal = FirstTerminal;
  4538. }
  4539. //---------------------------------------------------------------------------
  4540. void __fastcall TCustomScpExplorerForm::OpenFolderOrWorkspace(const UnicodeString & Name)
  4541. {
  4542. if (OpenInNewWindow())
  4543. {
  4544. ExecuteNewInstance(Name);
  4545. }
  4546. else
  4547. {
  4548. DoOpenFolderOrWorkspace(Name, true);
  4549. }
  4550. }
  4551. //---------------------------------------------------------------------------
  4552. void __fastcall TCustomScpExplorerForm::FormCloseQuery(TObject * /*Sender*/,
  4553. bool &CanClose)
  4554. {
  4555. if (Terminal != NULL)
  4556. {
  4557. if (Terminal->Active && WinConfiguration->ConfirmClosingSession)
  4558. {
  4559. unsigned int Result;
  4560. TMessageParams Params(mpNeverAskAgainCheck);
  4561. UnicodeString Message;
  4562. int Answers = qaOK | qaCancel;
  4563. if (TTerminalManager::Instance()->Count > 1)
  4564. {
  4565. if (!WinConfiguration->AutoSaveWorkspace)
  4566. {
  4567. Message = LoadStr(CLOSE_SESSIONS_WORKSPACE3);
  4568. }
  4569. else
  4570. {
  4571. Message = LoadStr(CLOSE_SESSIONS);
  4572. }
  4573. }
  4574. else
  4575. {
  4576. if (!WinConfiguration->AutoSaveWorkspace)
  4577. {
  4578. Message = LoadStr(CLOSE_SESSION_WORKSPACE);
  4579. }
  4580. else
  4581. {
  4582. Message = LoadStr(CLOSE_SESSION);
  4583. }
  4584. Message = FORMAT(Message, (Terminal->SessionData->SessionName));
  4585. }
  4586. UnicodeString Note;
  4587. if (WinConfiguration->AutoSaveWorkspace)
  4588. {
  4589. Note = FMTLOAD(AUTO_WORKSPACE, (WorkspaceName()));
  4590. }
  4591. else
  4592. {
  4593. Note = LoadStr(AUTO_WORKSPACE_ENABLE);
  4594. Answers = qaYes | qaNo | qaCancel;
  4595. }
  4596. Message = FORMAT("%s\n\n%s", (MainInstructions(Message), Note));
  4597. SetFocus();
  4598. Result = MessageDialog(Message, qtConfirmation,
  4599. Answers, HELP_NONE, &Params);
  4600. if (Result == qaNeverAskAgain)
  4601. {
  4602. WinConfiguration->ConfirmClosingSession = false;
  4603. }
  4604. if (Result == qaNo)
  4605. {
  4606. CanClose = SaveWorkspace(true);
  4607. // note that the workspace will be saved redundatly again from FormClose
  4608. }
  4609. else
  4610. {
  4611. CanClose =
  4612. (Result == qaOK) ||
  4613. (Result == qaYes) || // CLOSE_SESSIONS_WORKSPACE variant
  4614. (Result == qaNeverAskAgain);
  4615. }
  4616. }
  4617. if (CanClose)
  4618. {
  4619. CanClose = CanCloseQueue();
  4620. }
  4621. }
  4622. if (CanClose)
  4623. {
  4624. CanClose =
  4625. FEditorManager->CloseInternalEditors(CloseInternalEditor) &&
  4626. FEditorManager->CloseExternalFilesWithoutProcess();
  4627. if (CanClose)
  4628. {
  4629. while (CanClose && (FLocalEditors->Count > 0))
  4630. {
  4631. int PrevCount = FLocalEditors->Count;
  4632. static_cast<TForm *>(FLocalEditors->Items[0])->Close();
  4633. CanClose = (FLocalEditors->Count < PrevCount);
  4634. }
  4635. if (CanClose)
  4636. {
  4637. CanClose = FEditorManager->Empty(true);
  4638. if (!CanClose)
  4639. {
  4640. SetFocus();
  4641. CanClose =
  4642. (MessageDialog(
  4643. LoadStr(PENDING_EDITORS), qtWarning, qaIgnore | qaCancel, HELP_NONE) == qaIgnore);
  4644. }
  4645. }
  4646. }
  4647. }
  4648. }
  4649. //---------------------------------------------------------------------------
  4650. void __fastcall TCustomScpExplorerForm::CloseInternalEditor(TObject * Sender)
  4651. {
  4652. TForm * Form = dynamic_cast<TForm *>(Sender);
  4653. DebugAssert(Form != NULL);
  4654. Form->Close();
  4655. }
  4656. //---------------------------------------------------------------------------
  4657. void __fastcall TCustomScpExplorerForm::ForceCloseInternalEditor(TObject * Sender)
  4658. {
  4659. TForm * Form = dynamic_cast<TForm *>(Sender);
  4660. delete Form;
  4661. }
  4662. //---------------------------------------------------------------------------
  4663. void __fastcall TCustomScpExplorerForm::ForceCloseLocalEditors()
  4664. {
  4665. while (FLocalEditors->Count > 0)
  4666. {
  4667. delete static_cast<TForm *>(FLocalEditors->Items[0]);
  4668. }
  4669. }
  4670. //---------------------------------------------------------------------------
  4671. void __fastcall TCustomScpExplorerForm::RemoteDirViewDisplayProperties(
  4672. TObject *Sender)
  4673. {
  4674. TStrings *FileList = ((TUnixDirView*)Sender)->CreateFileList(True, False, NULL);
  4675. try
  4676. {
  4677. SetProperties(osRemote, FileList);
  4678. }
  4679. __finally
  4680. {
  4681. delete FileList;
  4682. }
  4683. }
  4684. //---------------------------------------------------------------------------
  4685. void __fastcall TCustomScpExplorerForm::ComponentShowing(Byte Component, bool value)
  4686. {
  4687. if (value)
  4688. {
  4689. if (Component == fcCustomCommandsBand)
  4690. {
  4691. UpdateCustomCommandsToolbar();
  4692. }
  4693. }
  4694. }
  4695. //---------------------------------------------------------------------------
  4696. void __fastcall TCustomScpExplorerForm::SetComponentVisible(Byte Component, Boolean value)
  4697. {
  4698. TControl * Control = GetComponent(Component);
  4699. DebugAssert(Control);
  4700. bool Changed = (Control->Visible != value);
  4701. if (Changed)
  4702. {
  4703. ComponentShowing(Component, value);
  4704. TWinControl * WinControl = dynamic_cast<TWinControl*>(Control);
  4705. bool WasFocused = (WinControl != NULL) && (ActiveControl != NULL) &&
  4706. ((ActiveControl == WinControl) || (ActiveControl->Parent == WinControl));
  4707. if (value)
  4708. {
  4709. int RemainingHeight = Control->Parent->ClientHeight;
  4710. int RemainingWidth = Control->Parent->ClientWidth;
  4711. for (int i = 0; i < Control->Parent->ControlCount; i++)
  4712. {
  4713. TControl * ChildControl = Control->Parent->Controls[i];
  4714. if (ChildControl->Visible)
  4715. {
  4716. switch (ChildControl->Align)
  4717. {
  4718. case alTop:
  4719. case alBottom:
  4720. RemainingHeight -= ChildControl->Height;
  4721. break;
  4722. case alLeft:
  4723. case alRight:
  4724. RemainingWidth -= ChildControl->Width;
  4725. break;
  4726. }
  4727. }
  4728. }
  4729. int Reserve = ScaleByTextHeight(this, 32);
  4730. // queue in explorer, trees in commander
  4731. if (Control->Height > RemainingHeight - Reserve)
  4732. {
  4733. Control->Height = RemainingHeight / 2;
  4734. }
  4735. if (Control->Width > RemainingWidth - Reserve)
  4736. {
  4737. Control->Width = RemainingWidth / 2;
  4738. }
  4739. }
  4740. Control->Visible = value;
  4741. if (WasFocused && Visible)
  4742. {
  4743. DirView(osCurrent)->SetFocus();
  4744. }
  4745. FixControlsPlacement();
  4746. }
  4747. }
  4748. //---------------------------------------------------------------------------
  4749. bool __fastcall TCustomScpExplorerForm::GetComponentVisible(Byte Component)
  4750. {
  4751. TControl * Control = GetComponent(Component);
  4752. if (Control == NULL)
  4753. {
  4754. return false;
  4755. }
  4756. else
  4757. {
  4758. return Control->Visible;
  4759. }
  4760. }
  4761. //---------------------------------------------------------------------------
  4762. bool __fastcall TCustomScpExplorerForm::IsComponentPossible(Byte Component)
  4763. {
  4764. bool Result;
  4765. if ((Component == fcExplorerUpdatesBand) || (Component == fcCommanderUpdatesBand))
  4766. {
  4767. Result = !IsUWP();
  4768. }
  4769. else
  4770. {
  4771. Result = true;
  4772. }
  4773. return Result;
  4774. }
  4775. //---------------------------------------------------------------------------
  4776. void __fastcall TCustomScpExplorerForm::FixControlsPlacement()
  4777. {
  4778. if (DirView(osOther)->ItemFocused != NULL)
  4779. {
  4780. DirView(osOther)->ItemFocused->MakeVisible(false);
  4781. }
  4782. QueueSplitter->Visible = QueuePanel->Visible;
  4783. RemotePanelSplitter->Visible = RemoteDrivePanel->Visible;
  4784. }
  4785. //---------------------------------------------------------------------------
  4786. TControl * __fastcall TCustomScpExplorerForm::GetComponent(Byte Component)
  4787. {
  4788. switch (Component) {
  4789. case fcStatusBar: return RemoteStatusBar;
  4790. case fcRemotePopup: return reinterpret_cast<TControl *>(NonVisualDataModule->RemoteFilePopup);
  4791. case fcQueueView: return QueuePanel;
  4792. case fcQueueToolbar: return QueueDock;
  4793. case fcRemoteTree: return RemoteDrivePanel;
  4794. case fcSessionsTabs: return SessionsPageControl;
  4795. default: return NULL;
  4796. }
  4797. }
  4798. //---------------------------------------------------------------------------
  4799. void __fastcall TCustomScpExplorerForm::DirViewColumnRightClick(
  4800. TObject *Sender, TListColumn *Column, TPoint &Point)
  4801. {
  4802. DebugAssert(NonVisualDataModule && Column && Sender);
  4803. NonVisualDataModule->ListColumn = Column;
  4804. TPopupMenu * DirViewColumnMenu;
  4805. if (dynamic_cast<TUnixDirView *>(Sender) != NULL)
  4806. {
  4807. DirViewColumnMenu = NonVisualDataModule->RemoteDirViewColumnPopup;
  4808. NonVisualDataModule->RemoteSortByExtColumnPopupItem->Visible =
  4809. (Column->Index == uvName);
  4810. NonVisualDataModule->RemoteFormatSizeBytesPopupItem->Visible =
  4811. (Column->Index == uvSize);
  4812. }
  4813. else
  4814. {
  4815. DirViewColumnMenu = NonVisualDataModule->LocalDirViewColumnPopup;
  4816. NonVisualDataModule->LocalSortByExtColumnPopupItem->Visible =
  4817. (Column->Index == dvName);
  4818. NonVisualDataModule->LocalFormatSizeBytesPopupItem->Visible =
  4819. (Column->Index == dvSize);
  4820. }
  4821. TCustomListView * ListView = DebugNotNull(dynamic_cast<TCustomListView *>(Sender));
  4822. TPoint ScreenPoint = ListView->ClientToScreen(Point);
  4823. ScreenPoint.x -= GetScrollPos(ListView->Handle, SB_HORZ);
  4824. DirViewColumnMenu->Popup(ScreenPoint.x, ScreenPoint.y);
  4825. }
  4826. //---------------------------------------------------------------------------
  4827. void __fastcall TCustomScpExplorerForm::DirViewExecFile(
  4828. TObject *Sender, TListItem *Item, bool &AllowExec)
  4829. {
  4830. DoDirViewExecFile(Sender, Item, AllowExec);
  4831. }
  4832. //---------------------------------------------------------------------------
  4833. void __fastcall TCustomScpExplorerForm::DoDirViewExecFile(TObject * Sender,
  4834. TListItem * Item, bool & AllowExec)
  4835. {
  4836. DebugAssert(Sender && Item && Configuration);
  4837. DebugAssert(AllowExec);
  4838. TCustomDirView * ADirView = (TCustomDirView *)Sender;
  4839. bool Remote = (ADirView == DirView(osRemote)) && !IsSideLocalBrowser(osRemote);
  4840. bool ResolvedSymlinks = !Remote || Terminal->ResolvingSymlinks;
  4841. TOperationSide Side = (Remote ? osRemote : osLocal); // TODO
  4842. // Anything special is done on double click only (not on "open" indicated by FForceExecution),
  4843. // on files only (not directories)
  4844. // and only when symlinks are resolved (apply to remote panel only)
  4845. if (!ADirView->ItemIsDirectory(Item) &&
  4846. (ResolvedSymlinks || FForceExecution || Terminal->IsEncryptingFiles()))
  4847. {
  4848. if ((WinConfiguration->DoubleClickAction != dcaOpen) &&
  4849. !FForceExecution &&
  4850. (ResolvedSymlinks || Terminal->IsEncryptingFiles()))
  4851. {
  4852. if (WinConfiguration->DoubleClickAction == dcaCopy)
  4853. {
  4854. // To counter the lock in DirViewBusy (called from TCustomDirView.DoExecute)
  4855. UnlockWindow();
  4856. try
  4857. {
  4858. ExecuteFileOperation(
  4859. foCopy, Side, true, !WinConfiguration->CopyOnDoubleClickConfirmation);
  4860. }
  4861. __finally
  4862. {
  4863. LockWindow();
  4864. }
  4865. AllowExec = false;
  4866. }
  4867. else if (WinConfiguration->DoubleClickAction == dcaEdit)
  4868. {
  4869. if (!Remote || !WinConfiguration->DisableOpenEdit)
  4870. {
  4871. ExecuteFile(Side, efDefaultEditor);
  4872. AllowExec = false;
  4873. }
  4874. }
  4875. else
  4876. {
  4877. DebugFail();
  4878. }
  4879. }
  4880. // if we have not done anything special, fall back to default behavior
  4881. if (AllowExec)
  4882. {
  4883. if (Remote && !WinConfiguration->DisableOpenEdit)
  4884. {
  4885. ExecuteFile(osRemote, efShell);
  4886. AllowExec = false;
  4887. }
  4888. }
  4889. }
  4890. }
  4891. //---------------------------------------------------------------------------
  4892. bool __fastcall TCustomScpExplorerForm::GetHasDirView(TOperationSide Side)
  4893. {
  4894. return ((Side == osRemote) || (Side == osCurrent));
  4895. }
  4896. //---------------------------------------------------------------------------
  4897. void __fastcall TCustomScpExplorerForm::CompareDirectories()
  4898. {
  4899. DebugFail();
  4900. }
  4901. //---------------------------------------------------------------------------
  4902. void __fastcall TCustomScpExplorerForm::SynchronizeDirectories()
  4903. {
  4904. DebugFail();
  4905. }
  4906. //---------------------------------------------------------------------------
  4907. bool __fastcall TCustomScpExplorerForm::DoSynchronizeDirectories(
  4908. UnicodeString & LocalDirectory, UnicodeString & RemoteDirectory, int UseDefaults)
  4909. {
  4910. TSynchronizeParamType Params;
  4911. Params.LocalDirectory = LocalDirectory;
  4912. Params.RemoteDirectory = RemoteDirectory;
  4913. int UnusedParams =
  4914. (GUIConfiguration->SynchronizeParams &
  4915. (TTerminal::spPreviewChanges | TTerminal::spTimestamp | TTerminal::spNotByTime | TTerminal::spBySize));
  4916. Params.Params = GUIConfiguration->SynchronizeParams & ~UnusedParams;
  4917. Params.Options = GUIConfiguration->SynchronizeOptions;
  4918. bool SaveSettings = false;
  4919. TSynchronizeController Controller(&DoSynchronize, &DoSynchronizeInvalid,
  4920. &DoSynchronizeTooManyDirectories);
  4921. DebugAssert(FSynchronizeController == NULL);
  4922. FSynchronizeController = &Controller;
  4923. bool Result;
  4924. try
  4925. {
  4926. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  4927. int CopyParamAttrs = Terminal->UsableCopyParamAttrs(0).Upload;
  4928. int Options =
  4929. FLAGMASK(SynchronizeAllowSelectedOnly(), soAllowSelectedOnly);
  4930. DebugAssert(FOnFeedSynchronizeError == NULL);
  4931. Result = DoSynchronizeDialog(Params, &CopyParam, Controller.StartStop,
  4932. SaveSettings, Options, CopyParamAttrs, GetSynchronizeOptions, SynchronizeSessionLog,
  4933. FOnFeedSynchronizeError, SynchronizeInNewWindow, UseDefaults);
  4934. if (Result)
  4935. {
  4936. if (SaveSettings)
  4937. {
  4938. GUIConfiguration->SynchronizeParams = Params.Params | UnusedParams;
  4939. GUIConfiguration->SynchronizeOptions = Params.Options;
  4940. }
  4941. else
  4942. {
  4943. if (FLAGSET(GUIConfiguration->SynchronizeOptions, soSynchronizeAsk) &&
  4944. FLAGCLEAR(Params.Options, soSynchronizeAsk) &&
  4945. FLAGSET(Params.Options, soSynchronize))
  4946. {
  4947. GUIConfiguration->SynchronizeOptions =
  4948. (GUIConfiguration->SynchronizeOptions & ~soSynchronizeAsk) |
  4949. soSynchronize;
  4950. }
  4951. }
  4952. LocalDirectory = Params.LocalDirectory;
  4953. RemoteDirectory = Params.RemoteDirectory;
  4954. }
  4955. }
  4956. __finally
  4957. {
  4958. FSynchronizeController = NULL;
  4959. DebugAssert(FOnFeedSynchronizeError == NULL);
  4960. FOnFeedSynchronizeError = NULL;
  4961. }
  4962. return Result;
  4963. }
  4964. //---------------------------------------------------------------------------
  4965. void __fastcall TCustomScpExplorerForm::DoSynchronize(
  4966. TSynchronizeController * /*Sender*/, const UnicodeString LocalDirectory,
  4967. const UnicodeString RemoteDirectory, const TCopyParamType & CopyParam,
  4968. const TSynchronizeParamType & Params, TSynchronizeChecklist ** Checklist,
  4969. TSynchronizeOptions * Options, bool Full)
  4970. {
  4971. // Keep-up-to-date mode
  4972. if (Terminal->Status == ssOpened)
  4973. {
  4974. try
  4975. {
  4976. int PParams = Params.Params;
  4977. if (!Full)
  4978. {
  4979. PParams |= TTerminal::spNoRecurse | TTerminal::spUseCache | TTerminal::spDelayProgress;
  4980. if (FLAGSET(Params.Options, soRecurse))
  4981. {
  4982. PParams |= TTerminal::spSubDirs;
  4983. }
  4984. }
  4985. else
  4986. {
  4987. // if keepuptodate is non-recursive,
  4988. // full sync before has to be non-recursive as well
  4989. if (FLAGCLEAR(Params.Options, soRecurse))
  4990. {
  4991. PParams |= TTerminal::spNoRecurse;
  4992. }
  4993. }
  4994. Synchronize(LocalDirectory, RemoteDirectory, smRemote, CopyParam,
  4995. PParams, Checklist, Options);
  4996. }
  4997. catch (Exception & E)
  4998. {
  4999. if (FLAGSET(Params.Options, soContinueOnError) && Terminal->Active &&
  5000. DebugAlwaysTrue(FOnFeedSynchronizeError != NULL))
  5001. {
  5002. // noop, already logged in MoreMessageDialog
  5003. }
  5004. else
  5005. {
  5006. ShowExtendedExceptionEx(Terminal, &E);
  5007. throw;
  5008. }
  5009. }
  5010. }
  5011. }
  5012. //---------------------------------------------------------------------------
  5013. void __fastcall TCustomScpExplorerForm::DoSynchronizeInvalid(
  5014. TSynchronizeController * /*Sender*/, const UnicodeString Directory,
  5015. const UnicodeString ErrorStr)
  5016. {
  5017. if (!Directory.IsEmpty())
  5018. {
  5019. SimpleErrorDialog(FMTLOAD(WATCH_ERROR_DIRECTORY, (Directory)), ErrorStr);
  5020. }
  5021. else
  5022. {
  5023. SimpleErrorDialog(LoadStr(WATCH_ERROR_GENERAL), ErrorStr);
  5024. }
  5025. }
  5026. //---------------------------------------------------------------------------
  5027. void __fastcall TCustomScpExplorerForm::DoSynchronizeTooManyDirectories(
  5028. TSynchronizeController * /*Sender*/, int & MaxDirectories)
  5029. {
  5030. if (MaxDirectories < GUIConfiguration->MaxWatchDirectories)
  5031. {
  5032. MaxDirectories = GUIConfiguration->MaxWatchDirectories;
  5033. }
  5034. else
  5035. {
  5036. TMessageParams Params(mpNeverAskAgainCheck);
  5037. unsigned int Result = MessageDialog(
  5038. FMTLOAD(TOO_MANY_WATCH_DIRECTORIES, (MaxDirectories, MaxDirectories)),
  5039. qtConfirmation, qaYes | qaNo, HELP_TOO_MANY_WATCH_DIRECTORIES, &Params);
  5040. if ((Result == qaYes) || (Result == qaNeverAskAgain))
  5041. {
  5042. MaxDirectories *= 2;
  5043. if (Result == qaNeverAskAgain)
  5044. {
  5045. GUIConfiguration->MaxWatchDirectories = MaxDirectories;
  5046. }
  5047. }
  5048. else
  5049. {
  5050. Abort();
  5051. }
  5052. }
  5053. }
  5054. //---------------------------------------------------------------------------
  5055. void __fastcall TCustomScpExplorerForm::Synchronize(const UnicodeString LocalDirectory,
  5056. const UnicodeString RemoteDirectory, TSynchronizeMode Mode,
  5057. const TCopyParamType & CopyParam, int Params, TSynchronizeChecklist ** Checklist,
  5058. TSynchronizeOptions * Options)
  5059. {
  5060. // Keep-up-to-date mode
  5061. DebugAssert(!FAutoOperation);
  5062. void * BatchStorage;
  5063. BatchStart(BatchStorage);
  5064. FAutoOperation = true;
  5065. bool AnyOperation = false;
  5066. TDateTime StartTime = Now();
  5067. TSynchronizeChecklist * AChecklist = NULL;
  5068. try
  5069. {
  5070. FSynchronizeProgressForm = new TSynchronizeProgressForm(Application, true, -1);
  5071. if (FLAGCLEAR(Params, TTerminal::spDelayProgress))
  5072. {
  5073. FSynchronizeProgressForm->Start();
  5074. }
  5075. AChecklist = Terminal->SynchronizeCollect(LocalDirectory, RemoteDirectory,
  5076. static_cast<TTerminal::TSynchronizeMode>(Mode),
  5077. &CopyParam, Params | TTerminal::spNoConfirmation, TerminalSynchronizeDirectory,
  5078. Options);
  5079. SAFE_DESTROY(FSynchronizeProgressForm);
  5080. AnyOperation = (AChecklist->CheckedCount > 0);
  5081. if (AnyOperation)
  5082. {
  5083. TSynchronizeProgress SynchronizeProgress(AChecklist);
  5084. CreateProgressForm(&SynchronizeProgress);
  5085. try
  5086. {
  5087. Terminal->SynchronizeApply(
  5088. AChecklist, &CopyParam, Params | TTerminal::spNoConfirmation, TerminalSynchronizeDirectory,
  5089. SynchronizeProcessedItem, NULL, NULL, NULL);
  5090. }
  5091. __finally
  5092. {
  5093. DestroyProgressForm();
  5094. }
  5095. }
  5096. }
  5097. __finally
  5098. {
  5099. if (Checklist == NULL)
  5100. {
  5101. delete AChecklist;
  5102. }
  5103. else
  5104. {
  5105. *Checklist = AChecklist;
  5106. }
  5107. FAutoOperation = false;
  5108. SAFE_DESTROY(FSynchronizeProgressForm);
  5109. BatchEnd(BatchStorage);
  5110. ReloadLocalDirectory();
  5111. if (AnyOperation)
  5112. {
  5113. OperationComplete(StartTime);
  5114. }
  5115. }
  5116. }
  5117. //---------------------------------------------------------------------------
  5118. bool __fastcall TCustomScpExplorerForm::SynchronizeAllowSelectedOnly()
  5119. {
  5120. // can be called from command line
  5121. return Visible &&
  5122. ((DirView(osOther)->SelCount > 0) ||
  5123. (HasDirView[osLocal] && (DirView(osLocal)->SelCount > 0)));
  5124. }
  5125. //---------------------------------------------------------------------------
  5126. void __fastcall TCustomScpExplorerForm::SynchronizeSessionLog(const UnicodeString & Message)
  5127. {
  5128. LogSynchronizeEvent(Terminal, Message);
  5129. }
  5130. //---------------------------------------------------------------------------
  5131. void __fastcall TCustomScpExplorerForm::GetSynchronizeOptions(
  5132. int Params, TSynchronizeOptions & Options)
  5133. {
  5134. DebugAssert(!IsLocalBrowserMode());
  5135. if (FLAGSET(Params, TTerminal::spSelectedOnly) && SynchronizeAllowSelectedOnly())
  5136. {
  5137. Options.Filter = new TStringList();
  5138. Options.Filter->CaseSensitive = false;
  5139. Options.Filter->Duplicates = Types::dupAccept;
  5140. if (DirView(osRemote)->SelCount > 0)
  5141. {
  5142. DirView(osRemote)->CreateFileList(false, false, Options.Filter);
  5143. }
  5144. if (HasDirView[osLocal] && (DirView(osLocal)->SelCount > 0))
  5145. {
  5146. DirView(osLocal)->CreateFileList(false, false, Options.Filter);
  5147. }
  5148. Options.Filter->Sort();
  5149. }
  5150. }
  5151. //---------------------------------------------------------------------------
  5152. UnicodeString __fastcall TCustomScpExplorerForm::SerializeCopyParamForCommandLine(const TCopyParamType * CopyParams)
  5153. {
  5154. TCopyParamType Defaults;
  5155. std::unique_ptr<THierarchicalStorage> ConfigStorage(Configuration->CreateConfigStorage());
  5156. ConfigStorage->AccessMode = smRead;
  5157. if (ConfigStorage->OpenSubKey(Configuration->ConfigurationSubKey, false))
  5158. {
  5159. GUIConfiguration->LoadCopyParam(ConfigStorage.get(), &Defaults);
  5160. }
  5161. ConfigStorage.reset(NULL);
  5162. std::unique_ptr<TStringList> Options(new TStringList());
  5163. std::unique_ptr<TOptionsStorage> OptionsStorage(new TOptionsStorage(Options.get(), true));
  5164. CopyParams->Save(OptionsStorage.get(), &Defaults);
  5165. UnicodeString Result;
  5166. if (Options->Count > 0)
  5167. {
  5168. Result =
  5169. FORMAT(L" %s%s", (TProgramParams::FormatSwitch(RAWTRANSFERSETTINGS_SWITCH), StringsToParams(Options.get())));
  5170. }
  5171. return Result;
  5172. }
  5173. //---------------------------------------------------------------------------
  5174. void __fastcall TCustomScpExplorerForm::SynchronizeInNewWindow(
  5175. const TSynchronizeParamType & Params, const TCopyParamType * CopyParams)
  5176. {
  5177. UnicodeString SessionName = CreateHiddenDuplicateSession();
  5178. UnicodeString AdditionalParams =
  5179. FORMAT(L"%s \"%s\" \"%s\" %d %d %s%s", (
  5180. TProgramParams::FormatSwitch(KEEP_UP_TO_DATE_SWITCH),
  5181. Params.LocalDirectory, Params.RemoteDirectory, Params.Params, Params.Options,
  5182. TProgramParams::FormatSwitch(DEFAULTS_SWITCH),
  5183. SerializeCopyParamForCommandLine(CopyParams)));
  5184. ExecuteNewInstance(SessionName, AdditionalParams);
  5185. }
  5186. //---------------------------------------------------------------------------
  5187. struct TSynchronizeParams
  5188. {
  5189. UnicodeString LocalDirectory;
  5190. UnicodeString RemoteDirectory;
  5191. TSynchronizeMode Mode;
  5192. TSynchronizeChecklist * Checklist;
  5193. int Params;
  5194. TCopyParamType * CopyParam;
  5195. TDateTime CollectElapsed;
  5196. TDateTime * StartTime;
  5197. TProcessedSynchronizationChecklistItem OnProcessedItem;
  5198. };
  5199. //---------------------------------------------------------------------------
  5200. void __fastcall TCustomScpExplorerForm::FullSynchronize(
  5201. TSynchronizeParams & Params, TProcessedSynchronizationChecklistItem OnProcessedItem,
  5202. TUpdatedSynchronizationChecklistItems OnUpdatedSynchronizationChecklistItems)
  5203. {
  5204. DebugAssert(!FAutoOperation);
  5205. void * BatchStorage;
  5206. BatchStart(BatchStorage);
  5207. FAutoOperation = true;
  5208. TFileOperationStatistics Statistics;
  5209. TDateTime Start = Now();
  5210. try
  5211. {
  5212. Params.OnProcessedItem = OnProcessedItem;
  5213. TSynchronizeProgress SynchronizeProgress(Params.Checklist);
  5214. CreateProgressForm(&SynchronizeProgress);
  5215. Terminal->SynchronizeApply(
  5216. Params.Checklist, Params.CopyParam, Params.Params | TTerminal::spNoConfirmation,
  5217. TerminalSynchronizeDirectory, SynchronizeProcessedItem, OnUpdatedSynchronizationChecklistItems, &Params,
  5218. &Statistics);
  5219. }
  5220. __finally
  5221. {
  5222. Params.OnProcessedItem = NULL;
  5223. FAutoOperation = false;
  5224. DestroyProgressForm();
  5225. BatchEnd(BatchStorage);
  5226. ReloadLocalDirectory();
  5227. }
  5228. if (WinConfiguration->SynchronizeSummary)
  5229. {
  5230. UnicodeString Message = MainInstructions(LoadStr(SYNCHRONIZE_COMPLETE)) + L"\n";
  5231. // The statistics should be 0 anyway in this case
  5232. if (FLAGCLEAR(Params.Params, TTerminal::spTimestamp))
  5233. {
  5234. Message += L"\n";
  5235. if (Statistics.FilesUploaded > 0)
  5236. {
  5237. Message += FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 1), (FormatNumber(Statistics.FilesUploaded), FormatBytes(Statistics.TotalUploaded))) + L"\n";
  5238. }
  5239. if (Statistics.FilesDownloaded > 0)
  5240. {
  5241. Message += FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 2), (FormatNumber(Statistics.FilesDownloaded), FormatBytes(Statistics.TotalDownloaded))) + L"\n";
  5242. }
  5243. if (Statistics.FilesDeletedLocal > 0)
  5244. {
  5245. Message += FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 3), (FormatNumber(Statistics.FilesDeletedLocal))) + L"\n";
  5246. }
  5247. if (Statistics.FilesDeletedRemote > 0)
  5248. {
  5249. Message += FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 4), (FormatNumber(Statistics.FilesDeletedRemote))) + L"\n";
  5250. }
  5251. }
  5252. TDateTime Elapsed = (Now() - Start);
  5253. Message +=
  5254. L"\n" +
  5255. FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 5), (FormatDateTimeSpan(Configuration->TimeFormat, Params.CollectElapsed))) + L"\n" +
  5256. FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 6), (FormatDateTimeSpan(Configuration->TimeFormat, Elapsed)));
  5257. TMessageParams Params(mpNeverAskAgainCheck);
  5258. unsigned int Result = MessageDialog(Message, qtInformation, qaOK, HELP_NONE, &Params);
  5259. if (Result == qaNeverAskAgain)
  5260. {
  5261. WinConfiguration->SynchronizeSummary = false;
  5262. }
  5263. }
  5264. }
  5265. //---------------------------------------------------------------------------
  5266. void __fastcall TCustomScpExplorerForm::SynchronizeProcessedItem(void * Token, const TSynchronizeChecklist::TItem * ChecklistItem)
  5267. {
  5268. if (DebugAlwaysTrue(FProgressForm != NULL) && DebugAlwaysTrue(FProgressForm->SynchronizeProgress != NULL))
  5269. {
  5270. FProgressForm->SynchronizeProgress->ItemProcessed(ChecklistItem);
  5271. }
  5272. // Not set in keep-up-to-date mode - Synchronize() method
  5273. if (Token != NULL)
  5274. {
  5275. TSynchronizeParams & Params = *static_cast<TSynchronizeParams *>(Token);
  5276. if (Params.OnProcessedItem != NULL)
  5277. {
  5278. Params.OnProcessedItem(NULL, ChecklistItem);
  5279. }
  5280. }
  5281. }
  5282. //---------------------------------------------------------------------------
  5283. void __fastcall TCustomScpExplorerForm::DoFullSynchronize(
  5284. void * Token, TProcessedSynchronizationChecklistItem OnProcessedItem,
  5285. TUpdatedSynchronizationChecklistItems OnUpdatedSynchronizationChecklistItems)
  5286. {
  5287. TSynchronizeParams & Params = *static_cast<TSynchronizeParams *>(Token);
  5288. *Params.StartTime = Now();
  5289. FullSynchronize(Params, OnProcessedItem, OnUpdatedSynchronizationChecklistItems);
  5290. }
  5291. //---------------------------------------------------------------------------
  5292. void __fastcall TCustomScpExplorerForm::DoSynchronizeChecklistCalculateSize(
  5293. TSynchronizeChecklist * Checklist, const TSynchronizeChecklist::TItemList & Items, void * Token)
  5294. {
  5295. // terminal can be already closed (e.g. dropped connection)
  5296. if (Terminal != NULL)
  5297. {
  5298. TSynchronizeParams & Params = *static_cast<TSynchronizeParams *>(Token);
  5299. Terminal->SynchronizeChecklistCalculateSize(Checklist, Items, Params.CopyParam);
  5300. }
  5301. }
  5302. //---------------------------------------------------------------------------
  5303. void __fastcall TCustomScpExplorerForm::DoSynchronizeMove(
  5304. TOperationSide Side, const UnicodeString & FileName, const UnicodeString & NewFileName, TRemoteFile * RemoteFile)
  5305. {
  5306. DebugAssert(!IsLocalBrowserMode());
  5307. TAutoBatch AutoBatch(this);
  5308. TAutoFlag AutoOperationFlag(FAutoOperation);
  5309. if (Side == osRemote)
  5310. {
  5311. std::unique_ptr<TStrings> FileList(new TStringList());
  5312. FileList->AddObject(FileName, RemoteFile);
  5313. UnicodeString Target = UnixExtractFileDir(NewFileName);
  5314. UnicodeString FileMask = DelimitFileNameMask(UnixExtractFileName(NewFileName));
  5315. RemoteDirView->SaveSelection();
  5316. RemoteDirView->SaveSelectedNames();
  5317. try
  5318. {
  5319. Terminal->MoveFiles(FileList.get(), Target, FileMask);
  5320. }
  5321. catch(...)
  5322. {
  5323. RemoteDirView->DiscardSavedSelection();
  5324. throw;
  5325. }
  5326. RemoteDirView->RestoreSelection();
  5327. }
  5328. else if (DebugAlwaysTrue(Side == osLocal))
  5329. {
  5330. if (!MoveFile(FileName.c_str(), NewFileName.c_str()))
  5331. {
  5332. throw EOSExtException(FMTLOAD(RENAME_FILE_ERROR, (FileName, NewFileName)));
  5333. }
  5334. UnicodeString Directory = ExtractFileDir(FileName);
  5335. ReloadLocalDirectory(Directory);
  5336. UnicodeString NewDirectory = ExtractFileDir(NewFileName);
  5337. if (!SamePaths(Directory, NewDirectory))
  5338. {
  5339. ReloadLocalDirectory(NewDirectory);
  5340. }
  5341. }
  5342. }
  5343. //---------------------------------------------------------------------------
  5344. void __fastcall TCustomScpExplorerForm::FullSynchronizeInNewWindow(
  5345. TSynchronizeMode Mode, int Params, const UnicodeString & LocalDirectory, const UnicodeString & RemoteDirectory,
  5346. const TCopyParamType * CopyParams)
  5347. {
  5348. UnicodeString SessionName = CreateHiddenDuplicateSession();
  5349. UnicodeString AdditionalParams =
  5350. FORMAT(L"%s \"%s\" \"%s\" %d %d %s%s", (
  5351. TProgramParams::FormatSwitch(SYNCHRONIZE_SWITCH),
  5352. LocalDirectory, RemoteDirectory, Mode, Params,
  5353. TProgramParams::FormatSwitch(DEFAULTS_SWITCH),
  5354. SerializeCopyParamForCommandLine(CopyParams)));
  5355. ExecuteNewInstance(SessionName, AdditionalParams);
  5356. }
  5357. //---------------------------------------------------------------------------
  5358. int __fastcall TCustomScpExplorerForm::DoFullSynchronizeDirectories(
  5359. UnicodeString & LocalDirectory, UnicodeString & RemoteDirectory,
  5360. TSynchronizeMode & Mode, int Params, bool & SaveMode, int UseDefaults)
  5361. {
  5362. int Result;
  5363. bool SaveSettings = false;
  5364. int Options =
  5365. FLAGMASK(!Terminal->IsCapable[fcTimestampChanging], fsoDisableTimestamp) |
  5366. FLAGMASK(SynchronizeAllowSelectedOnly(), fsoAllowSelectedOnly);
  5367. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  5368. TUsableCopyParamAttrs CopyParamAttrs = Terminal->UsableCopyParamAttrs(0);
  5369. bool Continue =
  5370. (UseDefaults == 0) ||
  5371. DoFullSynchronizeDialog(Mode, Params, LocalDirectory, RemoteDirectory,
  5372. &CopyParam, SaveSettings, SaveMode, Options, CopyParamAttrs, FullSynchronizeInNewWindow, UseDefaults);
  5373. if (Continue)
  5374. {
  5375. Configuration->Usage->Inc(L"Synchronizations");
  5376. UpdateCopyParamCounters(CopyParam);
  5377. TSynchronizeOptions SynchronizeOptions;
  5378. GetSynchronizeOptions(Params, SynchronizeOptions);
  5379. if (SaveSettings)
  5380. {
  5381. GUIConfiguration->SynchronizeParams = Params;
  5382. }
  5383. else
  5384. {
  5385. SaveMode = false;
  5386. }
  5387. TDateTime StartTime = Now();
  5388. TSynchronizeChecklist * Checklist = NULL;
  5389. try
  5390. {
  5391. DebugAssert(!FAutoOperation);
  5392. FAutoOperation = true;
  5393. try
  5394. {
  5395. UnicodeString SessionKey = Terminal->SessionData->SessionKey;
  5396. std::unique_ptr<TStrings> DataList(Configuration->LoadDirectoryStatisticsCache(SessionKey, RemoteDirectory, CopyParam));
  5397. int Files = -1;
  5398. if (DataList->Count >= 1)
  5399. {
  5400. Files = StrToIntDef(DataList->Strings[0], Files);
  5401. }
  5402. else
  5403. {
  5404. DataList->Add(UnicodeString());
  5405. }
  5406. FSynchronizeProgressForm = new TSynchronizeProgressForm(Application, true, Files);
  5407. FSynchronizeProgressForm->Start();
  5408. Checklist = Terminal->SynchronizeCollect(LocalDirectory, RemoteDirectory,
  5409. static_cast<TTerminal::TSynchronizeMode>(Mode),
  5410. &CopyParam, Params | TTerminal::spNoConfirmation, TerminalSynchronizeDirectory,
  5411. &SynchronizeOptions);
  5412. if (Terminal->SessionData->CacheDirectories)
  5413. {
  5414. DataList->Strings[0] = IntToStr(SynchronizeOptions.Files);
  5415. Configuration->SaveDirectoryStatisticsCache(SessionKey, RemoteDirectory, CopyParam, DataList.get());
  5416. }
  5417. }
  5418. __finally
  5419. {
  5420. FAutoOperation = false;
  5421. SAFE_DESTROY(FSynchronizeProgressForm);
  5422. }
  5423. TSynchronizeParams SynchronizeParams;
  5424. SynchronizeParams.LocalDirectory = LocalDirectory;
  5425. SynchronizeParams.RemoteDirectory = RemoteDirectory;
  5426. SynchronizeParams.Mode = Mode;
  5427. SynchronizeParams.CopyParam = &CopyParam;
  5428. SynchronizeParams.Params = Params;
  5429. SynchronizeParams.Checklist = Checklist;
  5430. SynchronizeParams.CollectElapsed = (Now() - StartTime);
  5431. SynchronizeParams.StartTime = &StartTime;
  5432. SynchronizeParams.OnProcessedItem = NULL;
  5433. Result = Checklist->Count;
  5434. if (Checklist->Count > 0)
  5435. {
  5436. if (FLAGSET(Params, TTerminal::spPreviewChanges))
  5437. {
  5438. if (!DoSynchronizeChecklistDialog(
  5439. Checklist, Mode, Params, LocalDirectory, RemoteDirectory, CustomCommandMenu, DoFullSynchronize,
  5440. DoSynchronizeChecklistCalculateSize, DoSynchronizeMove, &SynchronizeParams))
  5441. {
  5442. Result = -1;
  5443. }
  5444. }
  5445. else
  5446. {
  5447. FullSynchronize(SynchronizeParams, NULL, NULL);
  5448. }
  5449. }
  5450. else
  5451. {
  5452. UnicodeString Message = MainInstructions(LoadStr(COMPARE_NO_DIFFERENCES));
  5453. MessageDialog(Message, qtInformation, qaOK, HELP_SYNCHRONIZE_NO_DIFFERENCES);
  5454. }
  5455. }
  5456. __finally
  5457. {
  5458. delete Checklist;
  5459. }
  5460. OperationComplete(StartTime);
  5461. }
  5462. else
  5463. {
  5464. Result = -1;
  5465. }
  5466. return Result;
  5467. }
  5468. //---------------------------------------------------------------------------
  5469. void __fastcall TCustomScpExplorerForm::TerminalSynchronizeDirectory(
  5470. const UnicodeString & LocalDirectory, const UnicodeString & RemoteDirectory,
  5471. bool & Continue, bool Collect, const TSynchronizeOptions * Options)
  5472. {
  5473. if (Collect)
  5474. {
  5475. DebugAssert(FSynchronizeProgressForm != NULL);
  5476. if (!FSynchronizeProgressForm->Started)
  5477. {
  5478. FSynchronizeProgressForm->Start();
  5479. }
  5480. int CompareProgress = FSynchronizeProgressForm->SetData(LocalDirectory, RemoteDirectory, Options->Files, Continue);
  5481. SetTaskbarListProgressValue(CompareProgress);
  5482. }
  5483. else
  5484. {
  5485. DebugAssert(FSynchronizeProgressForm == NULL);
  5486. DebugAssert(FProgressForm != NULL);
  5487. }
  5488. }
  5489. //---------------------------------------------------------------------------
  5490. void __fastcall TCustomScpExplorerForm::StandaloneEdit(const UnicodeString & FileName)
  5491. {
  5492. UnicodeString FullFileName = AbsolutePath(FTerminal->CurrentDirectory, FileName);
  5493. TRemoteFile * File = NULL;
  5494. Terminal->ReadFile(FullFileName, File);
  5495. if (File != NULL)
  5496. {
  5497. std::unique_ptr<TRemoteFile> FileOwner(File);
  5498. TAutoFlag Flag(FStandaloneEditing);
  5499. ExecuteRemoteFile(FullFileName, File, efInternalEditor);
  5500. Application->ShowMainForm = false;
  5501. Application->Run();
  5502. }
  5503. }
  5504. //---------------------------------------------------------------------------
  5505. void __fastcall TCustomScpExplorerForm::ExploreLocalDirectory()
  5506. {
  5507. DebugFail();
  5508. }
  5509. //---------------------------------------------------------------------------
  5510. TSessionData * __fastcall TCustomScpExplorerForm::CloneCurrentSessionData()
  5511. {
  5512. std::unique_ptr<TSessionData> SessionData(new TSessionData(L""));
  5513. SessionData->Assign(Terminal->SessionData);
  5514. UpdateSessionData(SessionData.get());
  5515. Terminal->UpdateSessionCredentials(SessionData.get());
  5516. if (!Terminal->SessionData->HasSessionName())
  5517. {
  5518. // Particularly for "Workspace/XXXX" name, we need to reset it, as it would become user-visible
  5519. // once IsWorkspace is cleared
  5520. SessionData->Name = UnicodeString();
  5521. SessionData->IsWorkspace = false;
  5522. }
  5523. return SessionData.release();
  5524. }
  5525. //---------------------------------------------------------------------------
  5526. void __fastcall TCustomScpExplorerForm::SaveCurrentSession()
  5527. {
  5528. TSessionData * SessionData = CloneCurrentSessionData();
  5529. try
  5530. {
  5531. TSessionData * EditingSessionData = StoredSessions->FindSame(SessionData);
  5532. DoSaveSession(SessionData, EditingSessionData, true, NULL);
  5533. }
  5534. __finally
  5535. {
  5536. delete SessionData;
  5537. }
  5538. }
  5539. //---------------------------------------------------------------------------
  5540. TObjectList * __fastcall TCustomScpExplorerForm::DoCollectWorkspace()
  5541. {
  5542. TTerminalManager * Manager = TTerminalManager::Instance();
  5543. std::unique_ptr<TObjectList> DataList(new TObjectList());
  5544. if (DebugAlwaysTrue(Terminal != NULL))
  5545. {
  5546. // Update (Managed)Terminal->StateData
  5547. UpdateTerminal(Terminal);
  5548. }
  5549. Manager->SaveWorkspace(DataList.get());
  5550. return DataList.release();
  5551. }
  5552. //---------------------------------------------------------------------------
  5553. void __fastcall TCustomScpExplorerForm::DoSaveWorkspace(
  5554. const UnicodeString & Name, TObjectList * DataList, bool SavePasswords, bool Explicit)
  5555. {
  5556. WinConfiguration->LastWorkspace = Name;
  5557. if (!SavePasswords)
  5558. {
  5559. for (int Index = 0; Index < DataList->Count; Index++)
  5560. {
  5561. TSessionData * SessionData = dynamic_cast<TSessionData *>(DataList->Items[Index]);
  5562. if (SessionData->Link.IsEmpty())
  5563. {
  5564. SessionData->ClearSessionPasswords();
  5565. }
  5566. }
  5567. }
  5568. StoredSessions->NewWorkspace(Name, DataList);
  5569. // modified only
  5570. StoredSessions->Save(false, Explicit);
  5571. }
  5572. //---------------------------------------------------------------------------
  5573. UnicodeString __fastcall TCustomScpExplorerForm::WorkspaceName()
  5574. {
  5575. return
  5576. DefaultStr(WinConfiguration->LastWorkspace,
  5577. DefaultStr(WinConfiguration->AutoWorkspace, LoadStr(NEW_WORKSPACE)));
  5578. }
  5579. //---------------------------------------------------------------------------
  5580. bool __fastcall TCustomScpExplorerForm::SaveWorkspace(bool EnableAutoSave)
  5581. {
  5582. std::unique_ptr<TObjectList> DataList(DoCollectWorkspace());
  5583. bool AnyNonStoredSessionWithPassword = false;
  5584. bool AnyNonStoredNonWorkspaceSessionWithPassword = false;
  5585. bool AllNonStoredSessionsAnonymous = true;
  5586. TTerminalManager * Manager = TTerminalManager::Instance();
  5587. for (int Index = 0; Index < DataList->Count; Index++)
  5588. {
  5589. TSessionData * SessionData = dynamic_cast<TSessionData *>(DataList->Items[Index]);
  5590. if (SessionData->Link.IsEmpty())
  5591. {
  5592. if (SessionData->HasAnySessionPassword())
  5593. {
  5594. AnyNonStoredSessionWithPassword = true;
  5595. if (!Manager->Terminals[Index]->SessionData->IsWorkspace)
  5596. {
  5597. AnyNonStoredNonWorkspaceSessionWithPassword = true;
  5598. }
  5599. }
  5600. if (!SameText(SessionData->UserName, AnonymousUserName))
  5601. {
  5602. AllNonStoredSessionsAnonymous = false;
  5603. }
  5604. }
  5605. }
  5606. bool SavePasswords;
  5607. bool * PSavePasswords;
  5608. bool NotRecommendedSavingPasswords =
  5609. !CustomWinConfiguration->UseMasterPassword &&
  5610. !AllNonStoredSessionsAnonymous;
  5611. if (Configuration->DisablePasswordStoring ||
  5612. !AnyNonStoredSessionWithPassword)
  5613. {
  5614. PSavePasswords = NULL;
  5615. SavePasswords = false;
  5616. }
  5617. else
  5618. {
  5619. PSavePasswords = &SavePasswords;
  5620. SavePasswords =
  5621. !AnyNonStoredNonWorkspaceSessionWithPassword ||
  5622. !NotRecommendedSavingPasswords;
  5623. }
  5624. UnicodeString Name = WorkspaceName();
  5625. bool CreateShortcut = false;
  5626. bool Result =
  5627. DoSaveWorkspaceDialog(
  5628. Name, PSavePasswords, NotRecommendedSavingPasswords, CreateShortcut,
  5629. EnableAutoSave);
  5630. if (Result)
  5631. {
  5632. DoSaveWorkspace(Name, DataList.get(), SavePasswords, true);
  5633. if (CreateShortcut)
  5634. {
  5635. TOperationVisualizer Visualizer;
  5636. UnicodeString AdditionalParams = TProgramParams::FormatSwitch(DESKTOP_SWITCH);
  5637. CreateDesktopSessionShortCut(Name, L"", AdditionalParams, -1, WORKSPACE_ICON);
  5638. }
  5639. if (EnableAutoSave)
  5640. {
  5641. WinConfiguration->AutoSaveWorkspace = true;
  5642. WinConfiguration->AutoWorkspace = Name;
  5643. if (PSavePasswords != NULL)
  5644. {
  5645. WinConfiguration->AutoSaveWorkspacePasswords = SavePasswords;
  5646. }
  5647. }
  5648. }
  5649. return Result;
  5650. }
  5651. //---------------------------------------------------------------------------
  5652. void __fastcall TCustomScpExplorerForm::UpdateTerminal(TManagedTerminal * Terminal)
  5653. {
  5654. DebugAssert(!IsLocalBrowserMode());
  5655. SAFE_DESTROY(Terminal->RemoteExplorerState);
  5656. if (WinConfiguration->PreservePanelState)
  5657. {
  5658. Terminal->RemoteExplorerState = RemoteDirView->SaveState();
  5659. }
  5660. UpdateSessionData(Terminal->StateData);
  5661. }
  5662. //---------------------------------------------------------------------------
  5663. void __fastcall TCustomScpExplorerForm::UpdateSessionData(TSessionData * Data)
  5664. {
  5665. // Keep in sync with TSessionData::CopyStateData
  5666. DebugAssert(!IsLocalBrowserMode());
  5667. DebugAssert(Data != NULL);
  5668. // This is inconsistent with how color (for example) is handled.
  5669. Data->Name = Terminal->SessionData->Name;
  5670. // cannot use RemoteDirView->Path, because it is empty if connection
  5671. // was already closed
  5672. // also only peek, we may not be connected at all atm
  5673. // so make sure we do not try retrieving current directory from the server
  5674. // (particularly with FTP)
  5675. UnicodeString ACurrentDirectory = Terminal->PeekCurrentDirectory();
  5676. if (!ACurrentDirectory.IsEmpty())
  5677. {
  5678. Data->RemoteDirectory = ACurrentDirectory;
  5679. }
  5680. Data->Color = SessionColor;
  5681. }
  5682. //---------------------------------------------------------------------------
  5683. void __fastcall TCustomScpExplorerForm::ToolBarResize(TObject *Sender)
  5684. {
  5685. TTBXToolbar * Toolbar = dynamic_cast<TTBXToolbar*>(Sender);
  5686. DebugAssert(Toolbar != NULL);
  5687. for (int i = 0; i < Toolbar->Items->Count; i++)
  5688. {
  5689. TTBXCustomDropDownItem * DropDownItem;
  5690. DropDownItem = dynamic_cast<TTBXCustomDropDownItem *>(Toolbar->Items->Items[i]);
  5691. if (DropDownItem != NULL)
  5692. {
  5693. ToolbarItemResize(DropDownItem,
  5694. Toolbar->Width - (Toolbar->View->BaseSize.x - DropDownItem->EditWidth) -
  5695. Toolbar->NonClientWidth);
  5696. break;
  5697. }
  5698. }
  5699. }
  5700. //---------------------------------------------------------------------------
  5701. void __fastcall TCustomScpExplorerForm::ToolbarItemResize(TTBXCustomDropDownItem * Item, int Width)
  5702. {
  5703. Item->EditWidth = Width;
  5704. }
  5705. //---------------------------------------------------------------------------
  5706. void __fastcall TCustomScpExplorerForm::ToolbarGetBaseSize(
  5707. TTBCustomToolbar * Toolbar, TPoint & ASize)
  5708. {
  5709. for (int i = 0; i < Toolbar->Items->Count; i++)
  5710. {
  5711. TTBXCustomDropDownItem * DropDownItem;
  5712. DropDownItem = dynamic_cast<TTBXCustomDropDownItem *>(Toolbar->Items->Items[i]);
  5713. if (DropDownItem != NULL)
  5714. {
  5715. ASize.x -= DropDownItem->EditWidth;
  5716. ASize.x += ScaleByTextHeight(this, 50) /* minimal combo width */;
  5717. }
  5718. }
  5719. }
  5720. //---------------------------------------------------------------------------
  5721. void __fastcall TCustomScpExplorerForm::DoWarnLackOfTempSpace(
  5722. const UnicodeString Path, __int64 RequiredSpace, bool & Continue)
  5723. {
  5724. if (WinConfiguration->DDWarnLackOfTempSpace)
  5725. {
  5726. UnicodeString ADrive = ExtractFileDrive(ExpandFileName(Path));
  5727. if (!ADrive.IsEmpty())
  5728. {
  5729. __int64 FreeSpace = DiskFree((Byte)(ADrive[1]-'A'+1));
  5730. DebugAssert(RequiredSpace >= 0);
  5731. __int64 RequiredWithReserve;
  5732. RequiredWithReserve = (__int64)(RequiredSpace * WinConfiguration->DDWarnLackOfTempSpaceRatio);
  5733. if (FreeSpace < RequiredWithReserve)
  5734. {
  5735. unsigned int Result;
  5736. TMessageParams Params(mpNeverAskAgainCheck);
  5737. Result = MessageDialog(FMTLOAD(DD_WARN_LACK_OF_TEMP_SPACE, (Path,
  5738. FormatBytes(FreeSpace), FormatBytes(RequiredSpace))),
  5739. qtWarning, qaYes | qaNo, HELP_DD_WARN_LACK_OF_TEMP_SPACE, &Params);
  5740. if (Result == qaNeverAskAgain)
  5741. {
  5742. WinConfiguration->DDWarnLackOfTempSpace = false;
  5743. }
  5744. Continue = (Result == qaYes || Result == qaNeverAskAgain);
  5745. }
  5746. }
  5747. }
  5748. }
  5749. //---------------------------------------------------------------------------
  5750. void __fastcall TCustomScpExplorerForm::AddBookmark(TOperationSide Side)
  5751. {
  5752. DoOpenDirectoryDialog(odAddBookmark, Side);
  5753. }
  5754. //---------------------------------------------------------------------------
  5755. TStrings * __fastcall TCustomScpExplorerForm::CreateVisitedDirectories(TOperationSide Side)
  5756. {
  5757. // we should better use TCustomDirView::FCaseSensitive, but it is private
  5758. TStringList * VisitedDirectories = CreateSortedStringList(!IsSideLocalBrowser(Side));
  5759. try
  5760. {
  5761. TCustomDirView * DView = DirView(Side);
  5762. for (int Index = -DView->BackCount; Index <= DView->ForwardCount; Index++)
  5763. {
  5764. VisitedDirectories->Add(DView->HistoryPath[Index]);
  5765. }
  5766. }
  5767. catch (...)
  5768. {
  5769. delete VisitedDirectories;
  5770. throw;
  5771. }
  5772. return VisitedDirectories;
  5773. }
  5774. //---------------------------------------------------------------------------
  5775. void __fastcall TCustomScpExplorerForm::DoOpenDirectoryDialog(
  5776. TOpenDirectoryMode Mode, TOperationSide Side)
  5777. {
  5778. bool Continue = true;
  5779. if (Mode == odAddBookmark)
  5780. {
  5781. TMessageParams Params(mpNeverAskAgainCheck);
  5782. Params.NeverAskAgainTitle = LoadStr(ADD_BOOKMARK_SHARED);
  5783. Params.NeverAskAgainCheckedInitially = WinConfiguration->UseSharedBookmarks;
  5784. UnicodeString Message = MainInstructions(FMTLOAD(ADD_BOOKMARK_CONFIRM, (DirView(Side)->PathName)));
  5785. unsigned int Answer =
  5786. MessageDialog(Message,
  5787. qtConfirmation, qaOK | qaCancel, HELP_ADD_BOOKMARK_CONFIRM, &Params);
  5788. if (Answer == qaNeverAskAgain)
  5789. {
  5790. Continue = true;
  5791. WinConfiguration->UseSharedBookmarks = true;
  5792. }
  5793. else if (Answer == qaOK)
  5794. {
  5795. Continue = true;
  5796. WinConfiguration->UseSharedBookmarks = false;
  5797. }
  5798. else
  5799. {
  5800. Continue = false;
  5801. }
  5802. }
  5803. if (Continue)
  5804. {
  5805. TStrings * VisitedDirectories = CreateVisitedDirectories(Side);
  5806. try
  5807. {
  5808. UnicodeString Name = DirView(Side)->PathName;
  5809. bool Repeat;
  5810. do
  5811. {
  5812. Repeat = false;
  5813. if (::DoOpenDirectoryDialog(Mode, Side, Name, VisitedDirectories, Terminal,
  5814. // do not allow switching to location profiles,
  5815. // if we are not connected
  5816. HasDirView[osLocal] && (Terminal != NULL)))
  5817. {
  5818. TWindowLock Lock(this);
  5819. if (!TryOpenDirectory(Side, Name))
  5820. {
  5821. Repeat = true;
  5822. Mode = odBrowse;
  5823. }
  5824. }
  5825. }
  5826. while (Repeat);
  5827. }
  5828. __finally
  5829. {
  5830. delete VisitedDirectories;
  5831. }
  5832. }
  5833. }
  5834. //---------------------------------------------------------------------------
  5835. bool __fastcall TCustomScpExplorerForm::CommandSessionFallback()
  5836. {
  5837. DebugAssert(!FTerminal->CommandSessionOpened);
  5838. return TTerminalManager::Instance()->ConnectTerminal(FTerminal->CommandSession);
  5839. }
  5840. //---------------------------------------------------------------------------
  5841. bool __fastcall TCustomScpExplorerForm::EnsureCommandSessionFallback(TFSCapability Capability)
  5842. {
  5843. bool Result = FTerminal->IsCapable[Capability] ||
  5844. FTerminal->CommandSessionOpened;
  5845. if (!Result)
  5846. {
  5847. DebugAssert(FTerminal->IsCapable[fcSecondaryShell]);
  5848. if (!GUIConfiguration->ConfirmCommandSession)
  5849. {
  5850. Result = true;
  5851. }
  5852. else
  5853. {
  5854. TMessageParams Params(mpNeverAskAgainCheck);
  5855. const TFileSystemInfo & FileSystemInfo = Terminal->GetFileSystemInfo();
  5856. unsigned int Answer = MessageDialog(FMTLOAD(PERFORM_ON_COMMAND_SESSION2,
  5857. (FileSystemInfo.ProtocolName, FileSystemInfo.ProtocolName)), qtConfirmation,
  5858. qaOK | qaCancel, HELP_PERFORM_ON_COMMAND_SESSION, &Params);
  5859. if (Answer == qaNeverAskAgain)
  5860. {
  5861. GUIConfiguration->ConfirmCommandSession = false;
  5862. Result = true;
  5863. }
  5864. else if (Answer == qaOK)
  5865. {
  5866. Result = true;
  5867. }
  5868. }
  5869. if (Result)
  5870. {
  5871. Result = CommandSessionFallback();
  5872. }
  5873. }
  5874. return Result;
  5875. }
  5876. //---------------------------------------------------------------------------
  5877. void __fastcall TCustomScpExplorerForm::OpenConsole(UnicodeString Command)
  5878. {
  5879. if (EnsureCommandSessionFallback(fcAnyCommand))
  5880. {
  5881. DoConsoleDialog(Terminal, Command);
  5882. }
  5883. }
  5884. //---------------------------------------------------------------------------
  5885. void __fastcall TCustomScpExplorerForm::FileControlDDDragEnter(
  5886. TObject *Sender, _di_IDataObject /*DataObj*/, int /*grfKeyState*/,
  5887. const TPoint & /*Point*/, int & /*dwEffect*/, bool & Accept)
  5888. {
  5889. if (!IsSideLocalBrowser(osRemote) &&
  5890. IsFileControl(DropSourceControl, osRemote) &&
  5891. (FDDExtMapFile != NULL))
  5892. {
  5893. Accept = true;
  5894. }
  5895. FDDTargetControl = dynamic_cast<TControl*>(Sender);
  5896. DebugAssert(FDDTargetControl != NULL);
  5897. }
  5898. //---------------------------------------------------------------------------
  5899. void __fastcall TCustomScpExplorerForm::SessionsDDDragEnter(
  5900. _di_IDataObject DataObj, int KeyState,
  5901. const TPoint & Point, int & Effect, bool & Accept)
  5902. {
  5903. FileControlDDDragEnter(SessionsPageControl, DataObj, KeyState, Point, Effect, Accept);
  5904. }
  5905. //---------------------------------------------------------------------------
  5906. void __fastcall TCustomScpExplorerForm::QueueDDDragEnter(
  5907. _di_IDataObject DataObj, int KeyState,
  5908. const TPoint & Point, int & Effect, bool & Accept)
  5909. {
  5910. FileControlDDDragEnter(QueueView3, DataObj, KeyState, Point, Effect, Accept);
  5911. }
  5912. //---------------------------------------------------------------------------
  5913. void __fastcall TCustomScpExplorerForm::FileControlDDDragLeave(
  5914. TObject * Sender)
  5915. {
  5916. DebugUsedParam(Sender);
  5917. DebugAssert(FDDTargetControl == Sender);
  5918. FDDTargetControl = NULL;
  5919. }
  5920. //---------------------------------------------------------------------------
  5921. void __fastcall TCustomScpExplorerForm::SessionsDDDragLeave()
  5922. {
  5923. FileControlDDDragLeave(SessionsPageControl);
  5924. }
  5925. //---------------------------------------------------------------------------
  5926. void __fastcall TCustomScpExplorerForm::QueueDDDragLeave()
  5927. {
  5928. FileControlDDDragLeave(QueueView3);
  5929. }
  5930. //---------------------------------------------------------------------------
  5931. void __fastcall TCustomScpExplorerForm::AddEditLink(TOperationSide Side, bool Add)
  5932. {
  5933. DebugAssert(!IsSideLocalBrowser(Side));
  5934. DebugUsedParam(Side);
  5935. bool Edit = false;
  5936. TRemoteFile * File = NULL;
  5937. UnicodeString FileName;
  5938. UnicodeString PointTo;
  5939. bool SymbolicLink = true;
  5940. if (RemoteDirView->ItemFocused)
  5941. {
  5942. DebugAssert(RemoteDirView->ItemFocused->Data);
  5943. File = (TRemoteFile *)RemoteDirView->ItemFocused->Data;
  5944. Edit = !Add && File->IsSymLink && Terminal->SessionData->ResolveSymlinks;
  5945. if (Edit)
  5946. {
  5947. FileName = File->FileName;
  5948. PointTo = File->LinkTo;
  5949. }
  5950. else
  5951. {
  5952. PointTo = File->FileName;
  5953. }
  5954. }
  5955. if (DoSymlinkDialog(FileName, PointTo, osRemote, SymbolicLink, Edit,
  5956. Terminal->IsCapable[fcHardLink]))
  5957. {
  5958. Configuration->Usage->Inc(L"RemoteLinksCreated");
  5959. if (Edit)
  5960. {
  5961. DebugAssert(File->FileName == FileName);
  5962. int Params = dfNoRecursive;
  5963. Terminal->ExceptionOnFail = true;
  5964. try
  5965. {
  5966. Terminal->DeleteFile(L"", File, &Params);
  5967. }
  5968. __finally
  5969. {
  5970. Terminal->ExceptionOnFail = false;
  5971. }
  5972. }
  5973. Terminal->CreateLink(FileName, PointTo, SymbolicLink);
  5974. }
  5975. }
  5976. //---------------------------------------------------------------------------
  5977. bool __fastcall TCustomScpExplorerForm::CanAddEditLink(TOperationSide Side)
  5978. {
  5979. return
  5980. (IsSideLocalBrowser(Side) ||
  5981. ((Terminal != NULL) &&
  5982. Terminal->ResolvingSymlinks &&
  5983. Terminal->IsCapable[fcSymbolicLink]));
  5984. }
  5985. //---------------------------------------------------------------------------
  5986. bool __fastcall TCustomScpExplorerForm::LinkFocused()
  5987. {
  5988. return
  5989. !IsSideLocalBrowser(FCurrentSide) &&
  5990. (RemoteDirView->ItemFocused != NULL) &&
  5991. ((TRemoteFile *)RemoteDirView->ItemFocused->Data)->IsSymLink &&
  5992. Terminal->SessionData->ResolveSymlinks;
  5993. }
  5994. //---------------------------------------------------------------------------
  5995. void __fastcall TCustomScpExplorerForm::ExecuteCurrentFile()
  5996. {
  5997. DebugAssert(!WinConfiguration->DisableOpenEdit);
  5998. FForceExecution = true;
  5999. try
  6000. {
  6001. DirView(osCurrent)->ExecuteCurrentFile();
  6002. }
  6003. __finally
  6004. {
  6005. FForceExecution = false;
  6006. }
  6007. }
  6008. //---------------------------------------------------------------------------
  6009. void __fastcall TCustomScpExplorerForm::ExecuteCurrentFileWith(bool OnFocused)
  6010. {
  6011. TEditorData ExternalEditor;
  6012. ExternalEditor.Editor = edExternal;
  6013. bool Remember = false;
  6014. if (DoEditorPreferencesDialog(&ExternalEditor, Remember, epmAdHoc, !IsSideLocalBrowser(osCurrent)))
  6015. {
  6016. if (Remember)
  6017. {
  6018. TEditorList * EditorList = new TEditorList;
  6019. try
  6020. {
  6021. *EditorList = *WinConfiguration->EditorList;
  6022. bool Found = false;
  6023. int i = 0;
  6024. while (!Found && (i < EditorList->Count))
  6025. {
  6026. const TEditorPreferences * Editor = EditorList->Editors[i];
  6027. if ((Editor->Data->Editor == edExternal) &&
  6028. (Editor->Data->ExternalEditor == ExternalEditor.ExternalEditor))
  6029. {
  6030. Found = true;
  6031. }
  6032. i++;
  6033. }
  6034. if (!Found)
  6035. {
  6036. EditorList->Add(new TEditorPreferences(ExternalEditor));
  6037. WinConfiguration->EditorList = EditorList;
  6038. }
  6039. }
  6040. __finally
  6041. {
  6042. delete EditorList;
  6043. }
  6044. }
  6045. ExecuteFile(osCurrent, efExternalEditor, &ExternalEditor, true, OnFocused);
  6046. }
  6047. }
  6048. //---------------------------------------------------------------------------
  6049. void __fastcall TCustomScpExplorerForm::TerminalRemoved(TObject * Sender)
  6050. {
  6051. FEditorManager->ProcessFiles(FileTerminalRemoved, Sender);
  6052. if (FFileFindTerminal == Sender)
  6053. {
  6054. FFileFindTerminal = NULL;
  6055. HideFileFindDialog();
  6056. }
  6057. if (FClipboardTerminal == Sender)
  6058. {
  6059. ClipboardClear(); // implies ClipboardStop
  6060. }
  6061. }
  6062. //---------------------------------------------------------------------------
  6063. void __fastcall TCustomScpExplorerForm::FileTerminalRemoved(const UnicodeString FileName,
  6064. TEditedFileData * Data, TObject * /*Token*/, void * Arg)
  6065. {
  6066. TTerminal * Terminal = static_cast<TTerminal *>(Arg);
  6067. DebugAssert(Terminal != NULL);
  6068. if (Data->Terminal == Terminal)
  6069. {
  6070. Data->Terminal = NULL;
  6071. }
  6072. }
  6073. //---------------------------------------------------------------------------
  6074. void __fastcall TCustomScpExplorerForm::LastTerminalClosed(TObject * /*Sender*/)
  6075. {
  6076. UpdateControls();
  6077. SessionColor = TColor(0);
  6078. UpdateRemotePathComboBox(false);
  6079. NeedSession(false);
  6080. }
  6081. //---------------------------------------------------------------------------
  6082. void __fastcall TCustomScpExplorerForm::NeedSession(bool Startup)
  6083. {
  6084. try
  6085. {
  6086. // Cache, as the login dialog can change its value
  6087. bool ShowLogin = WinConfiguration->ShowLoginWhenNoSession;
  6088. try
  6089. {
  6090. if (ShowLogin)
  6091. {
  6092. bool ReloadSessions = !Startup;
  6093. TTerminalManager::Instance()->NewSession(false, L"", ReloadSessions, this);
  6094. }
  6095. else if (Startup && WinConfiguration->AutoSaveWorkspace && !WinConfiguration->AutoWorkspace.IsEmpty() &&
  6096. // This detects if workspace was saved the last time the main widow was closed
  6097. SameText(WinConfiguration->LastStoredSession, WinConfiguration->AutoWorkspace))
  6098. {
  6099. DoOpenFolderOrWorkspace(WinConfiguration->AutoWorkspace, false);
  6100. Configuration->Usage->Inc(L"OpenedWorkspacesAuto");
  6101. }
  6102. }
  6103. __finally
  6104. {
  6105. // Do not terminate, if we are only starting up and we are not showing Login dialog
  6106. // (so there was no chance for the user to open any session yet)
  6107. if ((ShowLogin || !Startup) &&
  6108. !WinConfiguration->KeepOpenWhenNoSession &&
  6109. ((Terminal == NULL) || (!Terminal->Active && !Terminal->Permanent)))
  6110. {
  6111. TerminateApplication();
  6112. }
  6113. }
  6114. }
  6115. catch (EAbort &)
  6116. {
  6117. // swallow
  6118. // The TTerminalManager does not expect the OnLastTerminalClose to throw without trying to connect.
  6119. // Also when called from TWinControl.UpdateShowing => CMShowingChanged, the showing is aborted on exception
  6120. }
  6121. }
  6122. //---------------------------------------------------------------------------
  6123. void __fastcall TCustomScpExplorerForm::DoTerminalListChanged()
  6124. {
  6125. TStrings * TerminalList = TTerminalManager::Instance()->TerminalList;
  6126. int ActiveTerminalIndex = TTerminalManager::Instance()->ActiveTerminalIndex;
  6127. Configuration->Usage->SetMax(L"MaxOpenedSessions", TerminalList->Count);
  6128. SendMessage(SessionsPageControl->Handle, WM_SETREDRAW, 0, 0);
  6129. try
  6130. {
  6131. while ((SessionsPageControl->PageCount > TerminalList->Count + 1) ||
  6132. // Clear the design time unthemed tab
  6133. ((SessionsPageControl->PageCount > 0) &&
  6134. (dynamic_cast<TThemeTabSheet *>(SessionsPageControl->Pages[SessionsPageControl->PageCount - 1]) == NULL)))
  6135. {
  6136. delete SessionsPageControl->Pages[SessionsPageControl->PageCount - 1];
  6137. }
  6138. for (int Index = 0; Index <= TerminalList->Count; Index++)
  6139. {
  6140. TThemeTabSheet * TabSheet;
  6141. if (Index >= SessionsPageControl->PageCount)
  6142. {
  6143. TabSheet = new TThemeTabSheet(SessionsPageControl);
  6144. TabSheet->PageControl = SessionsPageControl;
  6145. }
  6146. else
  6147. {
  6148. TabSheet = DebugNotNull(dynamic_cast<TThemeTabSheet *>(SessionsPageControl->Pages[Index]));
  6149. }
  6150. bool IsSessionTab = (Index < TerminalList->Count);
  6151. if (IsSessionTab)
  6152. {
  6153. TTerminal * Terminal = dynamic_cast<TTerminal *>(TerminalList->Objects[Index]);
  6154. TabSheet->Tag = reinterpret_cast<int>(Terminal);
  6155. UpdateSessionTab(TabSheet);
  6156. }
  6157. else
  6158. {
  6159. TabSheet->ImageIndex = FNewSessionTabImageIndex;
  6160. TabSheet->Tag = 0; // not really needed
  6161. TabSheet->Shadowed = false;
  6162. // We know that we are at the last page, otherwise we could not call this (it assumes that new session tab is the last one)
  6163. UpdateNewSessionTab();
  6164. }
  6165. TabSheet->ShowCloseButton = IsSessionTab;
  6166. }
  6167. }
  6168. __finally
  6169. {
  6170. SendMessage(SessionsPageControl->Handle, WM_SETREDRAW, 1, 0);
  6171. }
  6172. SessionsPageControl->ActivePageIndex = ActiveTerminalIndex;
  6173. }
  6174. //---------------------------------------------------------------------------
  6175. void __fastcall TCustomScpExplorerForm::TerminalListChanged(TObject * /*Sender*/)
  6176. {
  6177. DoTerminalListChanged();
  6178. }
  6179. //---------------------------------------------------------------------------
  6180. void __fastcall TCustomScpExplorerForm::UpdateNewSessionTab()
  6181. {
  6182. TTabSheet * TabSheet = SessionsPageControl->Pages[SessionsPageControl->PageCount - 1];
  6183. DebugAssert(TabSheet->ImageIndex == 0);
  6184. TabSheet->Caption =
  6185. WinConfiguration->SelectiveToolbarText ?
  6186. StripHotkey(StripTrailingPunctuation(NonVisualDataModule->NewSessionAction->Caption)) :
  6187. UnicodeString();
  6188. }
  6189. //---------------------------------------------------------------------------
  6190. TManagedTerminal * __fastcall TCustomScpExplorerForm::GetSessionTabTerminal(TTabSheet * TabSheet)
  6191. {
  6192. return reinterpret_cast<TManagedTerminal *>(TabSheet->Tag);
  6193. }
  6194. //---------------------------------------------------------------------------
  6195. void __fastcall TCustomScpExplorerForm::UpdateSessionTab(TTabSheet * TabSheet)
  6196. {
  6197. if (DebugAlwaysTrue(TabSheet != NULL))
  6198. {
  6199. TManagedTerminal * ATerminal = GetSessionTabTerminal(TabSheet);
  6200. if (DebugAlwaysTrue(ATerminal != NULL))
  6201. {
  6202. TColor Color = (ATerminal == FTerminal) ? FSessionColor : ATerminal->StateData->Color;
  6203. TabSheet->ImageIndex = AddSessionColor(Color);
  6204. UnicodeString TabCaption = TTerminalManager::Instance()->GetTerminalTitle(ATerminal, true);
  6205. TabSheet->Caption = SessionsPageControl->FormatCaptionWithCloseButton(TabCaption);
  6206. TThemeTabSheet * ThemeTabSheet = dynamic_cast<TThemeTabSheet *>(TabSheet);
  6207. if (DebugAlwaysTrue(ThemeTabSheet != NULL))
  6208. {
  6209. ThemeTabSheet->Shadowed = !ATerminal->Active;
  6210. }
  6211. }
  6212. }
  6213. }
  6214. //---------------------------------------------------------------------------
  6215. bool __fastcall TCustomScpExplorerForm::SessionTabSwitched()
  6216. {
  6217. DebugAssert(SessionsPageControl->ActivePage != NULL);
  6218. TManagedTerminal * Terminal = GetSessionTabTerminal(SessionsPageControl->ActivePage);
  6219. bool Result = (Terminal != NULL);
  6220. if (Result)
  6221. {
  6222. TTerminalManager::Instance()->ActiveTerminal = Terminal;
  6223. }
  6224. else
  6225. {
  6226. try
  6227. {
  6228. NewSession(false);
  6229. }
  6230. __finally
  6231. {
  6232. DoTerminalListChanged();
  6233. }
  6234. FSessionsPageControlNewSessionTime = Now();
  6235. }
  6236. return Result;
  6237. }
  6238. //---------------------------------------------------------------------------
  6239. void __fastcall TCustomScpExplorerForm::SessionsPageControlChange(TObject * /*Sender*/)
  6240. {
  6241. SessionTabSwitched();
  6242. }
  6243. //---------------------------------------------------------------------------
  6244. void __fastcall TCustomScpExplorerForm::TransferListChange(TObject * Sender)
  6245. {
  6246. TTBXStringList * TransferList = dynamic_cast<TTBXStringList *>(Sender);
  6247. DebugAssert(TransferList != NULL);
  6248. UnicodeString Name;
  6249. if (TransferList->ItemIndex <= 0)
  6250. {
  6251. Name = L"";
  6252. }
  6253. else
  6254. {
  6255. Name = GUIConfiguration->CopyParamList->Names[TransferList->ItemIndex - 1];
  6256. }
  6257. if (FCopyParamAutoSelected.IsEmpty())
  6258. {
  6259. // if previous preset was not autoselected, make new preset the "default"
  6260. FCopyParamDefault = Name;
  6261. }
  6262. GUIConfiguration->CopyParamCurrent = Name;
  6263. }
  6264. //---------------------------------------------------------------------------
  6265. void __fastcall TCustomScpExplorerForm::UpdateTransferLabel()
  6266. {
  6267. // sanity check
  6268. bool ExistingPreset =
  6269. (FTransferListHoverIndex >= 0) &&
  6270. (FTransferListHoverIndex < 1 + GUIConfiguration->CopyParamList->Count);
  6271. DebugAssert(ExistingPreset);
  6272. if (ExistingPreset)
  6273. {
  6274. HDC DC = GetDC(0);
  6275. TCanvas * Canvas = new TCanvas();
  6276. try
  6277. {
  6278. Canvas->Handle = DC;
  6279. Canvas->Font = GetToolbarFont(this);
  6280. UnicodeString Name;
  6281. if (FTransferListHoverIndex == 0)
  6282. {
  6283. Name = L"";
  6284. }
  6285. else
  6286. {
  6287. Name = GUIConfiguration->CopyParamList->Names[FTransferListHoverIndex - 1];
  6288. }
  6289. TTBXLabelItem * TransferLabel = dynamic_cast<TTBXLabelItem*>(
  6290. static_cast<TComponent*>(GetComponent(fcTransferLabel)));
  6291. TTBXStringList * TransferList = dynamic_cast<TTBXStringList*>(
  6292. static_cast<TObject*>(GetComponent(fcTransferList)));
  6293. UnicodeString InfoStr =
  6294. GUIConfiguration->CopyParamPreset[Name].
  6295. GetInfoStr(L"; ",
  6296. FLAGMASK(((Terminal != NULL) && Terminal->Active), Terminal->UsableCopyParamAttrs(0).General));
  6297. int MaxWidth = TransferList->MinWidth - (2 * TransferLabel->Margin) - ScaleByTextHeight(this, 10);
  6298. if (Canvas->TextExtent(InfoStr).cx > MaxWidth)
  6299. {
  6300. while (Canvas->TextExtent(InfoStr + Ellipsis).cx > MaxWidth)
  6301. {
  6302. InfoStr.SetLength(InfoStr.Length() - 1);
  6303. }
  6304. InfoStr += Ellipsis;
  6305. }
  6306. // UpdateCaption does not cause invalidation of whole submenu, while
  6307. // setting Caption property does.
  6308. // also it probably does not resize the label, even if necessary
  6309. // (we do not want that anyway)
  6310. TransferLabel->UpdateCaption(InfoStr);
  6311. }
  6312. __finally
  6313. {
  6314. Canvas->Handle = NULL;
  6315. ReleaseDC(0, DC);
  6316. delete Canvas;
  6317. }
  6318. }
  6319. }
  6320. //---------------------------------------------------------------------------
  6321. void __fastcall TCustomScpExplorerForm::TransferListDrawItem(
  6322. TTBXCustomList * /*Sender*/, TCanvas * /*ACanvas*/, const TRect & /*ARect*/,
  6323. int /*AIndex*/, int AHoverIndex, bool & /*DrawDefault*/)
  6324. {
  6325. if (FTransferListHoverIndex != AHoverIndex)
  6326. {
  6327. FTransferListHoverIndex = AHoverIndex;
  6328. UpdateTransferLabel();
  6329. }
  6330. }
  6331. //---------------------------------------------------------------------------
  6332. void __fastcall TCustomScpExplorerForm::WMAppCommand(TMessage & Message)
  6333. {
  6334. int Command = GET_APPCOMMAND_LPARAM(Message.LParam);
  6335. TShiftState Shift = KeyDataToShiftState(GET_KEYSTATE_LPARAM(Message.LParam));
  6336. if ((Shift * (TShiftState() << ssShift << ssAlt << ssCtrl)).Empty())
  6337. {
  6338. if (Command == APPCOMMAND_BROWSER_FAVORITES)
  6339. {
  6340. OpenDirectory(GetSide(osCurrent));
  6341. Message.Result = 1;
  6342. }
  6343. else
  6344. {
  6345. TForm::Dispatch(&Message);
  6346. }
  6347. }
  6348. else
  6349. {
  6350. TForm::Dispatch(&Message);
  6351. }
  6352. }
  6353. //---------------------------------------------------------------------------
  6354. void __fastcall TCustomScpExplorerForm::CMDialogChar(TMessage & AMessage)
  6355. {
  6356. TCMDialogChar & Message = reinterpret_cast<TCMDialogChar &>(AMessage);
  6357. if ((FIgnoreNextDialogChar != 0) &&
  6358. (toupper(Message.CharCode) == toupper(FIgnoreNextDialogChar)))
  6359. {
  6360. Message.Result = 1;
  6361. }
  6362. else
  6363. {
  6364. TForm::Dispatch(&Message);
  6365. }
  6366. FIgnoreNextDialogChar = 0;
  6367. }
  6368. //---------------------------------------------------------------------------
  6369. void __fastcall TCustomScpExplorerForm::WMSysCommand(TMessage & Message)
  6370. {
  6371. // The four low-order bits are used internally by Windows
  6372. unsigned int Cmd = (Message.WParam & 0xFFF0);
  6373. // SC_RESTORE, SC_MAXIMIZE, SC_MINIMIZE - buttons on windows title
  6374. // SC_DEFAULT - double click on windows title (does not work, at least on WinXP)
  6375. // 61730 - restore thru double click - undocumented
  6376. // 61490 - maximize thru double click - undocumented
  6377. if ((Cmd == SC_RESTORE) || (Cmd == SC_MAXIMIZE) ||
  6378. (Cmd == SC_MINIMIZE) || (Cmd == SC_DEFAULT))
  6379. {
  6380. SysResizing(Cmd);
  6381. }
  6382. TForm::Dispatch(&Message);
  6383. }
  6384. //---------------------------------------------------------------------------
  6385. void __fastcall TCustomScpExplorerForm::WMQueryEndSession(TMessage & Message)
  6386. {
  6387. // We were actually never able to make ENDSESSION_CRITICAL happen.
  6388. // Also there no point returning TRUE as we are not able to
  6389. // handle the abrupt termination caused by subsequent WM_ENDSESSION cleanly.
  6390. // Hence the process termination might be safer :)
  6391. if ((Message.LParam != ENDSESSION_CRITICAL) &&
  6392. (((FQueue != NULL) && !FQueue->IsEmpty) || (FProgressForm != NULL)))
  6393. {
  6394. Message.Result = FALSE;
  6395. }
  6396. else
  6397. {
  6398. Message.Result = TRUE;
  6399. }
  6400. // Do not call default handling as that triggers OnCloseQuery,
  6401. // where our implementation will popup confirmation dialogs, what we do not want,
  6402. // as per Vista guidelines:
  6403. // https://docs.microsoft.com/en-us/windows/win32/shutdown/shutting-down
  6404. }
  6405. //---------------------------------------------------------------------------
  6406. void __fastcall TCustomScpExplorerForm::WMEndSession(TWMEndSession & Message)
  6407. {
  6408. if (Message.EndSession && IsApplicationMinimized())
  6409. {
  6410. // WORKAROUND
  6411. // TApplication.WndProc() calls Application.Terminate() before Halt(),
  6412. // when it receives WM_ENDSESSION,
  6413. // but that sometimes (particularly when application is minimized) cause crashes.
  6414. // Crash popups message that blocks log off.
  6415. // Obviously application cannot shutdown cleanly after WM_ENDSESSION,
  6416. // so we call ExitProcess() immediately, not even trying to cleanup.
  6417. // It still causes beep, so there's likely some popup, but it does not block
  6418. // log off.
  6419. ExitProcess(0);
  6420. }
  6421. TForm::Dispatch(&Message);
  6422. }
  6423. #ifdef _DEBUG
  6424. //---------------------------------------------------------------------------
  6425. void __fastcall TCustomScpExplorerForm::WMWindowPosChanged(TWMWindowPosMsg & Message)
  6426. {
  6427. TForm::Dispatch(&Message);
  6428. }
  6429. #endif
  6430. //---------------------------------------------------------------------------
  6431. void __fastcall TCustomScpExplorerForm::SysResizing(unsigned int /*Cmd*/)
  6432. {
  6433. }
  6434. //---------------------------------------------------------------------------
  6435. void __fastcall TCustomScpExplorerForm::DoShow()
  6436. {
  6437. // only now are the controls resized finally, so the size constraints
  6438. // will not conflict with possibly very small window size
  6439. RestoreFormParams();
  6440. // This indicates that the form was rescaled after parameters were loaded and
  6441. // as most probably the parameters were actually saved with the current form scaling
  6442. // (windows was the last time likely closed on the same monitor, where it is starting now),
  6443. // rescaling happened twice already. Reload the parameters, as now they likely won't need any scaling
  6444. // and no rounding problems will occur.
  6445. // See also RestoreForm.
  6446. // (we should be safe to call RestoreParams unconditionally)
  6447. if (PixelsPerInch != Screen->PixelsPerInch)
  6448. {
  6449. RestoreParams();
  6450. }
  6451. FixControlsPlacement();
  6452. if (Position == poDefaultPosOnly)
  6453. {
  6454. CutFormToDesktop(this);
  6455. }
  6456. TForm::DoShow();
  6457. FSessionsDragDropFilesEx->DragDropControl = SessionsPageControl;
  6458. FQueueDragDropFilesEx->DragDropControl = QueueView3;
  6459. if (Terminal == NULL)
  6460. {
  6461. StartingDisconnected();
  6462. }
  6463. UpdatePixelsPerInchMainWindowCounter();
  6464. FShowing = true;
  6465. }
  6466. //---------------------------------------------------------------------------
  6467. void __fastcall TCustomScpExplorerForm::UpdatePixelsPerInchMainWindowCounter()
  6468. {
  6469. Configuration->Usage->Set(L"PixelsPerInchMainWindow", PixelsPerInch);
  6470. }
  6471. //---------------------------------------------------------------------------
  6472. void __fastcall TCustomScpExplorerForm::StartingDisconnected()
  6473. {
  6474. DoTerminalListChanged();
  6475. }
  6476. //---------------------------------------------------------------------------
  6477. void __fastcall TCustomScpExplorerForm::PopupTrayBalloon(TTerminal * Terminal,
  6478. const UnicodeString & Str, TQueryType Type, Exception * E, unsigned int Seconds,
  6479. TNotifyEvent OnBalloonClick, TObject * UserData)
  6480. {
  6481. bool Do;
  6482. UnicodeString Message;
  6483. if (E == NULL)
  6484. {
  6485. Message = Str;
  6486. Do = true;
  6487. }
  6488. else
  6489. {
  6490. Do = ExceptionMessage(E, Message);
  6491. }
  6492. if (Do && WinConfiguration->BalloonNotifications)
  6493. {
  6494. UnicodeString Title;
  6495. if ((Terminal == NULL) && (Type == qtInformation) && ExtractMainInstructions(Message, Title))
  6496. {
  6497. Message = TrimLeft(Message);
  6498. }
  6499. else
  6500. {
  6501. Message = UnformatMessage(Message);
  6502. const ResourceString * Captions[] = { &_SMsgDlgConfirm, &_SMsgDlgWarning,
  6503. &_SMsgDlgError, &_SMsgDlgInformation, NULL };
  6504. Title = LoadResourceString(Captions[Type]);
  6505. if (Terminal != NULL)
  6506. {
  6507. Title = FORMAT(L"%s - %s",
  6508. (TTerminalManager::Instance()->GetTerminalTitle(Terminal, true), Title));
  6509. }
  6510. }
  6511. if (Seconds == 0)
  6512. {
  6513. Seconds = WinConfiguration->NotificationsTimeout;
  6514. }
  6515. FTrayIcon->PopupBalloon(Title, Message, Type, Seconds * MSecsPerSec, OnBalloonClick, UserData);
  6516. }
  6517. }
  6518. //---------------------------------------------------------------------------
  6519. unsigned int __fastcall TCustomScpExplorerForm::MoreMessageDialog(const UnicodeString Message,
  6520. TStrings * MoreMessages, TQueryType Type, unsigned int Answers,
  6521. UnicodeString HelpKeyword, const TMessageParams * Params,
  6522. TTerminal * Terminal)
  6523. {
  6524. if (((WinConfiguration->ContinueOnError && (FErrorList != NULL)) ||
  6525. (FOnFeedSynchronizeError != NULL)) &&
  6526. (Params != NULL) && (Params->Params & mpAllowContinueOnError) )
  6527. {
  6528. if (FOnFeedSynchronizeError != NULL)
  6529. {
  6530. FOnFeedSynchronizeError(Message, MoreMessages, Type, HelpKeyword);
  6531. }
  6532. else
  6533. {
  6534. DebugAssert(FErrorList != NULL);
  6535. TStringList * MoreMessagesCopy = NULL;
  6536. if (MoreMessages)
  6537. {
  6538. MoreMessagesCopy = new TStringList();
  6539. MoreMessagesCopy->Assign(MoreMessages);
  6540. }
  6541. FErrorList->AddObject(Message, MoreMessagesCopy);
  6542. }
  6543. PopupTrayBalloon(Terminal, Message, Type);
  6544. return ContinueAnswer(Answers);
  6545. }
  6546. else
  6547. {
  6548. bool UseBalloon = FTrayIcon->Visible;
  6549. if (UseBalloon)
  6550. {
  6551. PopupTrayBalloon(Terminal, Message, Type);
  6552. }
  6553. unsigned int Result;
  6554. try
  6555. {
  6556. Result = ::MoreMessageDialog(Message, MoreMessages, Type, Answers, HelpKeyword, Params);
  6557. }
  6558. __finally
  6559. {
  6560. // cancel only balloon we popped up, otherwise we may cancel notification
  6561. // balloon that was there before the message dialog
  6562. // (such as "dd confirmation opt in balloon)
  6563. if (UseBalloon)
  6564. {
  6565. FTrayIcon->CancelBalloon();
  6566. }
  6567. }
  6568. return Result;
  6569. }
  6570. }
  6571. //---------------------------------------------------------------------------
  6572. void __fastcall TCustomScpExplorerForm::ShowExtendedException(
  6573. TTerminal * Terminal, Exception * E)
  6574. {
  6575. if (FTrayIcon->Visible)
  6576. {
  6577. PopupTrayBalloon(Terminal, L"", qtError, E);
  6578. }
  6579. // particularly prevent opening new session from jump list,
  6580. // while exception is shown
  6581. NonVisualDataModule->StartBusy();
  6582. try
  6583. {
  6584. ShowExtendedExceptionEx(Terminal, E);
  6585. }
  6586. __finally
  6587. {
  6588. NonVisualDataModule->EndBusy();
  6589. FTrayIcon->CancelBalloon();
  6590. }
  6591. }
  6592. //---------------------------------------------------------------------------
  6593. void __fastcall TCustomScpExplorerForm::TerminalReady()
  6594. {
  6595. if (Terminal->Active)
  6596. {
  6597. InitStatusBar();
  6598. }
  6599. // cannot rely on active page being page for active terminal,
  6600. // as it can happen that active page is the "new session" page
  6601. // (e.g. when reconnecting active terminal, while login dialog
  6602. // invoked from "new session" page is modal)
  6603. int ActiveTerminalIndex = TTerminalManager::Instance()->ActiveTerminalIndex;
  6604. UpdateSessionTab(SessionsPageControl->Pages[ActiveTerminalIndex]);
  6605. }
  6606. //---------------------------------------------------------------------------
  6607. void __fastcall TCustomScpExplorerForm::InactiveTerminalException(
  6608. TTerminal * Terminal, Exception * E)
  6609. {
  6610. Notify(Terminal, L"", qtError, false, NULL, NULL, E);
  6611. if (!Terminal->Active)
  6612. {
  6613. int Index = TTerminalManager::Instance()->IndexOf(Terminal);
  6614. if (DebugAlwaysTrue((Index >= 0) && (Index < SessionsPageControl->PageCount)))
  6615. {
  6616. UpdateSessionTab(SessionsPageControl->Pages[Index]);
  6617. }
  6618. }
  6619. }
  6620. //---------------------------------------------------------------------------
  6621. void __fastcall TCustomScpExplorerForm::Notify(TTerminal * Terminal,
  6622. UnicodeString Message, TQueryType Type,
  6623. bool Important, TNotifyEvent OnClick, TObject * UserData, Exception * E)
  6624. {
  6625. if ((E == NULL) ||
  6626. ExceptionMessage(E, Message))
  6627. {
  6628. unsigned int Seconds = WinConfiguration->NotificationsTimeout;
  6629. if (Important)
  6630. {
  6631. Seconds *= 5;
  6632. }
  6633. UnicodeString NoteMessage(UnformatMessage(Message));
  6634. if (Terminal != NULL)
  6635. {
  6636. NoteMessage = FORMAT(L"%s: %s",
  6637. (TTerminalManager::Instance()->GetTerminalTitle(Terminal, true), NoteMessage));
  6638. }
  6639. if (WinConfiguration->BalloonNotifications)
  6640. {
  6641. AddNote(NoteMessage);
  6642. PopupTrayBalloon(Terminal, Message, Type, NULL, Seconds, OnClick, UserData);
  6643. }
  6644. else
  6645. {
  6646. FlashOnBackground();
  6647. PostNote(NoteMessage, Seconds, OnClick, UserData);
  6648. }
  6649. }
  6650. }
  6651. //---------------------------------------------------------------------------
  6652. void __fastcall TCustomScpExplorerForm::QueueEmptyNoteClicked(TObject * Sender)
  6653. {
  6654. RestoreApp();
  6655. TTerminalNoteData * TerminalNoteData = dynamic_cast<TTerminalNoteData *>(Sender);
  6656. if (DebugAlwaysTrue(TerminalNoteData != NULL) &&
  6657. !NonVisualDataModule->Busy)
  6658. {
  6659. TManagedTerminal * Terminal = TerminalNoteData->Terminal;
  6660. TTerminalManager::Instance()->ActiveTerminal = Terminal;
  6661. if (!ComponentVisible[fcQueueView])
  6662. {
  6663. ToggleQueueVisibility();
  6664. GoToQueue();
  6665. }
  6666. }
  6667. }
  6668. //---------------------------------------------------------------------------
  6669. void __fastcall TCustomScpExplorerForm::QueueEvent(TManagedTerminal * ATerminal,
  6670. TTerminalQueue * /*Queue*/, TQueueEvent Event)
  6671. {
  6672. UnicodeString Message;
  6673. TNotifyEvent OnClick = NULL;
  6674. TObject * UserData = NULL;
  6675. bool QueueInvisible = !ComponentVisible[fcQueueView] || IsApplicationMinimized();
  6676. switch (Event)
  6677. {
  6678. case qeEmptyButMonitored:
  6679. if ((ATerminal != Terminal) || QueueInvisible)
  6680. {
  6681. Message = LoadStr(BALLOON_QUEUE_EMPTY);
  6682. OnClick = QueueEmptyNoteClicked;
  6683. TTerminalNoteData * TerminalNoteData = new TTerminalNoteData();
  6684. TerminalNoteData->Terminal = ATerminal;
  6685. UserData = TerminalNoteData;
  6686. }
  6687. break;
  6688. case qeEmpty:
  6689. OperationComplete(ATerminal->QueueOperationStart);
  6690. break;
  6691. case qePendingUserAction:
  6692. if ((ATerminal != Terminal) ||
  6693. (QueueInvisible && !IsQueueAutoPopup()))
  6694. {
  6695. Message = LoadStr(BALLOON_QUEUE_USER_ACTION);
  6696. }
  6697. break;
  6698. default:
  6699. DebugFail();
  6700. }
  6701. if (!Message.IsEmpty())
  6702. {
  6703. Notify(ATerminal, Message, qtInformation, false, OnClick, UserData);
  6704. }
  6705. }
  6706. //---------------------------------------------------------------------------
  6707. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDCreateDragFileList(
  6708. TObject * /*Sender*/, TFileList * FileList, bool & Created)
  6709. {
  6710. if (FDDExtMapFile != NULL)
  6711. {
  6712. CloseHandle(FDDExtMapFile);
  6713. FDDExtMapFile = NULL;
  6714. }
  6715. if (WinConfiguration->DDFakeFile)
  6716. {
  6717. DDFakeFileInitDrag(FileList, Created);
  6718. }
  6719. }
  6720. //---------------------------------------------------------------------------
  6721. void __fastcall TCustomScpExplorerForm::DDFakeCreated(TObject * /*Sender*/, const UnicodeString FileName)
  6722. {
  6723. if (DebugAlwaysTrue(!FDragFakeDirectory.IsEmpty()) &&
  6724. SameText(ExtractFileName(FileName), ExtractFileName(FDragFakeDirectory)))
  6725. {
  6726. FFakeFileDropTarget = FileName;
  6727. }
  6728. }
  6729. //---------------------------------------------------------------------------
  6730. UnicodeString __fastcall TCustomScpExplorerForm::CreateFakeTransferDirectory()
  6731. {
  6732. UnicodeString Result = ExcludeTrailingBackslash(WinConfiguration->TemporaryDir());
  6733. if (!ForceDirectories(ApiPath(Result)))
  6734. {
  6735. throw Exception(FMTLOAD(CREATE_TEMP_DIR_ERROR, (Result)));
  6736. }
  6737. FileSetAttr(ApiPath(Result), faHidden);
  6738. return Result;
  6739. }
  6740. //---------------------------------------------------------------------------
  6741. void __fastcall TCustomScpExplorerForm::DDFakeFileInitDrag(TFileList * FileList,
  6742. bool & Created)
  6743. {
  6744. FFakeFileDropTarget = UnicodeString();
  6745. FDragFakeDirectory = CreateFakeTransferDirectory();
  6746. FileList->AddItem(NULL, FDragFakeDirectory);
  6747. Created = true;
  6748. if (!WinConfiguration->IsDDExtRunning() || WinConfiguration->IsDDExtBroken())
  6749. {
  6750. FDragFakeMonitors = StartCreationDirectoryMonitorsOnEachDrive(FILE_NOTIFY_CHANGE_DIR_NAME, DDFakeCreated);
  6751. }
  6752. FDDExtMapFile = CreateFileMappingA((HANDLE)0xFFFFFFFF, NULL, PAGE_READWRITE,
  6753. 0, sizeof(TDragExtCommStruct), AnsiString(DRAG_EXT_MAPPING).c_str());
  6754. {
  6755. TMutexGuard Guard(FDDExtMutex, DRAGEXT_MUTEX_RELEASE_TIMEOUT);
  6756. TDragExtCommStruct* CommStruct;
  6757. CommStruct = static_cast<TDragExtCommStruct*>(MapViewOfFile(FDDExtMapFile,
  6758. FILE_MAP_ALL_ACCESS, 0, 0, 0));
  6759. DebugAssert(CommStruct != NULL);
  6760. CommStruct->Version = TDragExtCommStruct::CurrentVersion;
  6761. CommStruct->Dragging = true;
  6762. wcsncpy(CommStruct->DropDest, FDragFakeDirectory.c_str(),
  6763. LENOF(CommStruct->DropDest));
  6764. NULL_TERMINATE(CommStruct->DropDest);
  6765. UnmapViewOfFile(CommStruct);
  6766. }
  6767. }
  6768. //---------------------------------------------------------------------------
  6769. bool __fastcall TCustomScpExplorerForm::RemoteFileControlFileOperation(
  6770. TObject * Sender, TFileOperation Operation, bool NoConfirmation, void * Param)
  6771. {
  6772. bool Result;
  6773. DebugAssert(!IsLocalBrowserMode());
  6774. if (Sender == RemoteDirView)
  6775. {
  6776. Result = ExecuteFileOperation(Operation, osRemote, true, NoConfirmation, Param);
  6777. }
  6778. else
  6779. {
  6780. DebugAssert(Sender == RemoteDriveView);
  6781. TStrings * FileList = RemoteDriveView->DragFileList();
  6782. try
  6783. {
  6784. Result = ExecuteFileOperation(Operation, osRemote, FileList, NoConfirmation, Param);
  6785. }
  6786. __finally
  6787. {
  6788. delete FileList;
  6789. }
  6790. }
  6791. if (FDDTargetControl == RemoteDriveView)
  6792. {
  6793. RemoteDriveView->UpdateDropTarget();
  6794. }
  6795. // foRemoteMove happens when file/dir is dragged within the remote tree view
  6796. // or from tree view to dir view.
  6797. // foMove happens when file/dir is dragged from remote tree view outside of
  6798. // application via dragex
  6799. if (((Operation == foRemoteMove) || (Operation == foMove)) &&
  6800. (DropSourceControl == RemoteDriveView))
  6801. {
  6802. RemoteDriveView->UpdateDropSource();
  6803. }
  6804. return Result;
  6805. }
  6806. //---------------------------------------------------------------------------
  6807. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDEnd(TObject * Sender)
  6808. {
  6809. DebugAssert(!IsLocalBrowserMode());
  6810. // This also handles drops of remote files to queue.
  6811. // Drops of local files (uploads) are handled in QueueDDProcessDropped.
  6812. SAFE_DESTROY(FDDFileList);
  6813. if (!FFakeFileDropTarget.IsEmpty())
  6814. {
  6815. RemoveDir(ApiPath(FFakeFileDropTarget));
  6816. }
  6817. if ((FDDExtMapFile != NULL) || (FDDTargetControl == QueueView3))
  6818. {
  6819. try
  6820. {
  6821. TDragResult DDResult = (Sender == RemoteDirView) ?
  6822. RemoteDirView->LastDDResult : RemoteDriveView->LastDDResult;
  6823. // Focus is moved to the target application,
  6824. // but as we are going to present the UI, we need to steal the focus back.
  6825. // This most likely won't work though (windows does not allow application
  6826. // to steal focus most of the time)
  6827. Application->BringToFront();
  6828. // On older version of Windows we never got drMove here, see also comment below.
  6829. // On Windows 10, we get the "move".
  6830. if ((DDResult == drCopy) || (DDResult == drMove) || (DDResult == drInvalid))
  6831. {
  6832. UnicodeString TargetDirectory;
  6833. TFileOperation Operation;
  6834. // drInvalid may mean drMove, see comment below
  6835. switch (DDResult)
  6836. {
  6837. case drCopy:
  6838. Operation = foCopy;
  6839. break;
  6840. case drMove:
  6841. Operation = foMove;
  6842. break;
  6843. default:
  6844. DebugFail();
  6845. case drInvalid:
  6846. // prefer "copy" for safety
  6847. Operation = FLAGSET(FLastDropEffect, DROPEFFECT_MOVE) ? foMove : foCopy;
  6848. break;
  6849. }
  6850. TTransferOperationParam Param;
  6851. UnicodeString CounterName;
  6852. bool ForceQueue;
  6853. if (!DDGetTarget(Param.TargetDirectory, ForceQueue, CounterName))
  6854. {
  6855. // we get drInvalid both if d&d was intercepted by ddext,
  6856. // and when users drops on no-drop location.
  6857. // we tell the difference by existence of response from ddext,
  6858. // so we ignore absence of response in this case
  6859. if (DDResult != drInvalid)
  6860. {
  6861. // here we know that the extension is installed,
  6862. // as it is checked as soon as drag&drop starts from
  6863. // RemoteFileControlDDCreateDragFileList
  6864. Configuration->Usage->Inc(L"DownloadsDragDropExternalExtTargetUnknown");
  6865. if (!WinConfiguration->DDExtInstalled || WinConfiguration->IsDDExtBroken())
  6866. {
  6867. throw ExtException(NULL, LoadStr(DD_TARGET_UNKNOWN), HELP_DD_TARGET_UNKNOWN);
  6868. }
  6869. else
  6870. {
  6871. throw ExtException(NULL, LoadStr(DRAGEXT_TARGET_UNKNOWN2), HELP_DRAGEXT_TARGET_UNKNOWN);
  6872. }
  6873. }
  6874. }
  6875. else
  6876. {
  6877. // download using fake file
  6878. Param.Temp = false;
  6879. Param.DragDrop = true;
  6880. if (ForceQueue)
  6881. {
  6882. Param.Queue = asOn;
  6883. }
  6884. if (Sender == RemoteDirView)
  6885. {
  6886. Param.Options = FLAGMASK(SelectedAllFilesInDirView(RemoteDirView), coAllFiles);
  6887. }
  6888. if (RemoteFileControlFileOperation(Sender, Operation,
  6889. (WinConfiguration->DDTransferConfirmation == asOff), &Param))
  6890. {
  6891. Configuration->Usage->Inc(CounterName);
  6892. }
  6893. }
  6894. }
  6895. }
  6896. __finally
  6897. {
  6898. CloseHandle(FDDExtMapFile);
  6899. FDDExtMapFile = NULL;
  6900. delete FDragFakeMonitors;
  6901. FDragFakeMonitors = NULL;
  6902. RemoveDir(ApiPath(FDragFakeDirectory));
  6903. FDragFakeDirectory = L"";
  6904. FFakeFileDropTarget = UnicodeString();
  6905. }
  6906. }
  6907. if (FDragDropSshTerminate.get() != NULL)
  6908. {
  6909. FDragDropSshTerminate->Rethrow();
  6910. }
  6911. }
  6912. //---------------------------------------------------------------------------
  6913. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDGiveFeedback(
  6914. TObject * /*Sender*/, int dwEffect, HRESULT & /*Result*/)
  6915. {
  6916. // Remember drop effect so we know (when user drops files), if we copy or move
  6917. FLastDropEffect = dwEffect;
  6918. }
  6919. //---------------------------------------------------------------------------
  6920. bool __fastcall TCustomScpExplorerForm::DDGetTarget(
  6921. UnicodeString & Directory, bool & ForceQueue, UnicodeString & CounterName)
  6922. {
  6923. bool Result;
  6924. if (FDDTargetControl == QueueView3)
  6925. {
  6926. Directory = DefaultDownloadTargetDirectory();
  6927. Result = true;
  6928. CounterName = L"DownloadsDragDropQueue";
  6929. ForceQueue = true;
  6930. }
  6931. else if (!FFakeFileDropTarget.IsEmpty())
  6932. {
  6933. Directory = ExcludeTrailingBackslash(ExtractFilePath(FFakeFileDropTarget));
  6934. Result = true;
  6935. ForceQueue = false;
  6936. CounterName = L"DownloadsDragDropFakeFile";
  6937. }
  6938. else
  6939. {
  6940. ForceQueue = false;
  6941. Enabled = false;
  6942. try
  6943. {
  6944. int Timer = 0;
  6945. Result = false;
  6946. while (!Result && (Timer < WinConfiguration->DDExtTimeout))
  6947. {
  6948. {
  6949. TMutexGuard Guard(FDDExtMutex, DRAGEXT_MUTEX_RELEASE_TIMEOUT);
  6950. TDragExtCommStruct* CommStruct;
  6951. CommStruct = static_cast<TDragExtCommStruct*>(MapViewOfFile(FDDExtMapFile,
  6952. FILE_MAP_ALL_ACCESS, 0, 0, 0));
  6953. DebugAssert(CommStruct != NULL);
  6954. Result = !CommStruct->Dragging;
  6955. if (Result)
  6956. {
  6957. Directory = ExtractFilePath(CommStruct->DropDest);
  6958. CounterName = L"DownloadsDragDropExternalExt";
  6959. }
  6960. UnmapViewOfFile(CommStruct);
  6961. }
  6962. if (!Result)
  6963. {
  6964. Sleep(50);
  6965. Timer += 50;
  6966. Application->ProcessMessages();
  6967. }
  6968. }
  6969. }
  6970. __finally
  6971. {
  6972. Enabled = true;
  6973. }
  6974. }
  6975. return Result;
  6976. }
  6977. //---------------------------------------------------------------------------
  6978. void __fastcall TCustomScpExplorerForm::AddDelayedDirectoryDeletion(
  6979. const UnicodeString TempDir, int SecDelay)
  6980. {
  6981. TDateTime Alarm = Now() + (double)((double)SecDelay*OneMillisecond);
  6982. FDelayedDeletionList->AddObject(TempDir, reinterpret_cast<TObject*>(Alarm.FileDate()));
  6983. if (FDelayedDeletionTimer == NULL)
  6984. {
  6985. DebugAssert(HandleAllocated());
  6986. FDelayedDeletionTimer = new TTimer(this);
  6987. FDelayedDeletionTimer->Interval = 10000;
  6988. FDelayedDeletionTimer->OnTimer = DoDelayedDeletion;
  6989. }
  6990. else
  6991. {
  6992. FDelayedDeletionTimer->Enabled = true;
  6993. }
  6994. }
  6995. //---------------------------------------------------------------------------
  6996. void __fastcall TCustomScpExplorerForm::DoDelayedDeletion(TObject * Sender)
  6997. {
  6998. DebugAssert(FDelayedDeletionList != NULL);
  6999. TDateTime N = Now();
  7000. TDateTime Alert;
  7001. UnicodeString Directory;
  7002. for (int Index = FDelayedDeletionList->Count-1; Index >= 0; Index--)
  7003. {
  7004. Alert = FileDateToDateTime(reinterpret_cast<int>(FDelayedDeletionList->Objects[Index]));
  7005. if ((N >= Alert) || (Sender == NULL))
  7006. {
  7007. Directory = FDelayedDeletionList->Strings[Index];
  7008. if (DeleteDirectory(ExcludeTrailingBackslash(Directory)))
  7009. {
  7010. FDelayedDeletionList->Delete(Index);
  7011. }
  7012. }
  7013. }
  7014. if (FDelayedDeletionList->Count == 0)
  7015. {
  7016. FDelayedDeletionTimer->Enabled = false;
  7017. }
  7018. }
  7019. //---------------------------------------------------------------------------
  7020. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDTargetDrop()
  7021. {
  7022. DebugAssert(!IsLocalBrowserMode());
  7023. if (IsFileControl(FDDTargetControl, osRemote) ||
  7024. (FDDTargetControl == SessionsPageControl))
  7025. {
  7026. TTerminal * TargetTerminal = NULL;
  7027. TFileOperation Operation = foNone;
  7028. if (FDDTargetControl == SessionsPageControl)
  7029. {
  7030. TPoint Point = SessionsPageControl->ScreenToClient(Mouse->CursorPos);
  7031. int Index = SessionsPageControl->IndexOfTabAt(Point.X, Point.Y);
  7032. // do not allow dropping on the "+" tab
  7033. TargetTerminal = GetSessionTabTerminal(SessionsPageControl->Pages[Index]);
  7034. if (TargetTerminal != NULL)
  7035. {
  7036. if ((FLastDropEffect == DROPEFFECT_MOVE) &&
  7037. (TargetTerminal == TTerminalManager::Instance()->ActiveTerminal))
  7038. {
  7039. Operation = foRemoteMove;
  7040. }
  7041. else
  7042. {
  7043. Operation = foRemoteCopy;
  7044. }
  7045. }
  7046. }
  7047. else
  7048. {
  7049. if (FLastDropEffect == DROPEFFECT_MOVE)
  7050. {
  7051. Operation = foRemoteMove;
  7052. }
  7053. else if (FLastDropEffect == DROPEFFECT_COPY)
  7054. {
  7055. Operation = foRemoteCopy;
  7056. }
  7057. }
  7058. if (Operation != foNone)
  7059. {
  7060. RemoteFileControlFileOperation(DropSourceControl,
  7061. Operation, (WinConfiguration->DDTransferConfirmation == asOff), TargetTerminal);
  7062. }
  7063. // abort drag&drop
  7064. Abort();
  7065. }
  7066. else if ((FDDExtMapFile == NULL) && (FLastDropEffect != DROPEFFECT_NONE) &&
  7067. // Drops of remote files to queue are handled in RemoteFileControlDDEnd
  7068. (FDDTargetControl != QueueView3))
  7069. {
  7070. DebugAssert(!FDragTempDir.IsEmpty());
  7071. TTransferType Type;
  7072. UnicodeString TempDir = FDragTempDir;
  7073. // We clear FDragTempDir before calling
  7074. // just in case it fail (raises exception)
  7075. FDragTempDir = L"";
  7076. Type = (FLastDropEffect & DROPEFFECT_MOVE ? ttMove : Type = ttCopy);
  7077. TGUICopyParamType CopyParams = GUIConfiguration->CurrentCopyParam;
  7078. // empty directory parameter means temp directory -> don't display it!
  7079. UnicodeString TargetDir = L"";
  7080. int Options = 0;
  7081. if (!CopyParamDialog(tdToLocal, Type, true, FDDFileList,
  7082. TargetDir, CopyParams, (WinConfiguration->DDTransferConfirmation != asOff), true, Options))
  7083. {
  7084. Abort();
  7085. }
  7086. // TargetDir is set when dropped on local file control
  7087. // (this was workaround for legacy dirview event handling, now it should be
  7088. // made prettier)
  7089. if (TargetDir.IsEmpty())
  7090. {
  7091. TargetDir = TempDir;
  7092. if (ForceDirectories(ApiPath(TargetDir)))
  7093. {
  7094. DebugAssert(Terminal && !TargetDir.IsEmpty());
  7095. FPendingTempSpaceWarn = true;
  7096. try
  7097. {
  7098. FDragDropOperation = true;
  7099. int Params = cpTemporary |
  7100. (Type == ttMove ? cpDelete : 0);
  7101. DDDownload(FDDFileList, TargetDir, &CopyParams, Params);
  7102. Configuration->Usage->Inc(L"DownloadsDragDropExternalTemp");
  7103. }
  7104. __finally
  7105. {
  7106. FDragDropOperation = false;
  7107. FPendingTempSpaceWarn = false;
  7108. AddDelayedDirectoryDeletion(TargetDir, WinConfiguration->DDDeleteDelay);
  7109. }
  7110. }
  7111. else
  7112. {
  7113. throw Exception(FMTLOAD(CREATE_TEMP_DIR_ERROR, (TargetDir)));
  7114. }
  7115. }
  7116. }
  7117. }
  7118. //---------------------------------------------------------------------------
  7119. void __fastcall TCustomScpExplorerForm::DDDownload(
  7120. TStrings * FilesToCopy, const UnicodeString & TargetDir, const TCopyParamType * CopyParam, int Params)
  7121. {
  7122. DebugAssert(!IsLocalBrowserMode());
  7123. TAutoBatch AutoBatch(this);
  7124. UpdateCopyParamCounters(*CopyParam);
  7125. Terminal->CopyToLocal(FilesToCopy, TargetDir, CopyParam, Params, NULL);
  7126. if (FLAGSET(Params, cpDelete) && (DropSourceControl == RemoteDriveView))
  7127. {
  7128. RemoteDriveView->UpdateDropSource();
  7129. }
  7130. }
  7131. //---------------------------------------------------------------------------
  7132. class TFakeDataObjectFilesEx : public TDataObjectFilesEx
  7133. {
  7134. public:
  7135. __fastcall TFakeDataObjectFilesEx(TFileList * AFileList, bool RenderPIDL,
  7136. bool RenderFilename) : TDataObjectFilesEx(AFileList, RenderPIDL, RenderFilename, true)
  7137. {
  7138. }
  7139. virtual bool __fastcall AllowData(const tagFORMATETC & FormatEtc)
  7140. {
  7141. return (FormatEtc.cfFormat == CF_HDROP) ? false :
  7142. TDataObjectFilesEx::AllowData(FormatEtc);
  7143. }
  7144. };
  7145. //---------------------------------------------------------------------------
  7146. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDCreateDataObject(
  7147. TObject * Sender, TDataObject *& DataObject)
  7148. {
  7149. if (FDDExtMapFile != NULL)
  7150. {
  7151. TFileList * FileList = DragDropFiles(Sender)->FileList;
  7152. if (!FileList->RenderPIDLs() || !FileList->RenderNames())
  7153. {
  7154. Abort();
  7155. }
  7156. if (FileList->Count > 0)
  7157. {
  7158. TDataObjectFilesEx * FilesObject = new TFakeDataObjectFilesEx(FileList, true, true);
  7159. if (!FilesObject->IsValid(true, true))
  7160. {
  7161. FilesObject->_Release();
  7162. }
  7163. else
  7164. {
  7165. DataObject = FilesObject;
  7166. }
  7167. }
  7168. }
  7169. }
  7170. //---------------------------------------------------------------------------
  7171. void __fastcall TCustomScpExplorerForm::GoToCommandLine()
  7172. {
  7173. DebugFail();
  7174. }
  7175. //---------------------------------------------------------------------------
  7176. void __fastcall TCustomScpExplorerForm::GoToTree()
  7177. {
  7178. ComponentVisible[fcRemoteTree] = true;
  7179. DriveView(osRemote)->SetFocus();
  7180. }
  7181. //---------------------------------------------------------------------------
  7182. TStrings * __fastcall TCustomScpExplorerForm::PanelExport(TOperationSide Side,
  7183. TPanelExport Export)
  7184. {
  7185. TCustomDirView * DirView = this->DirView(Side);
  7186. std::unique_ptr<TStrings> ExportData(new TStringList());
  7187. switch (Export)
  7188. {
  7189. case pePath:
  7190. ExportData->Add(DirView->PathName);
  7191. break;
  7192. case peFileList:
  7193. case peFullFileList:
  7194. {
  7195. bool FullPath = (Export == peFullFileList);
  7196. DirView->CreateFileList(false, FullPath, ExportData.get());
  7197. for (int Index = 0; Index < ExportData->Count; Index++)
  7198. {
  7199. if (ExportData->Strings[Index].Pos(L" ") > 0)
  7200. {
  7201. ExportData->Strings[Index] = FORMAT(L"\"%s\"", (ExportData->Strings[Index]));
  7202. }
  7203. }
  7204. }
  7205. break;
  7206. default:
  7207. DebugFail();
  7208. }
  7209. return ExportData.release();
  7210. }
  7211. //---------------------------------------------------------------------------
  7212. void __fastcall TCustomScpExplorerForm::PanelExport(TOperationSide Side,
  7213. TPanelExport Export, TPanelExportDestination Destination)
  7214. {
  7215. std::unique_ptr<TStrings> ExportData(PanelExport(Side, Export));
  7216. PanelExportStore(Side, Export, Destination, ExportData.get());
  7217. }
  7218. //---------------------------------------------------------------------------
  7219. void __fastcall TCustomScpExplorerForm::PanelExportStore(TOperationSide /*Side*/,
  7220. TPanelExport /*Export*/, TPanelExportDestination Destination,
  7221. TStrings * ExportData)
  7222. {
  7223. if (Destination == pedClipboard)
  7224. {
  7225. TInstantOperationVisualizer Visualizer;
  7226. CopyToClipboard(ExportData);
  7227. }
  7228. else
  7229. {
  7230. DebugFail();
  7231. }
  7232. }
  7233. //---------------------------------------------------------------------------
  7234. void __fastcall TCustomScpExplorerForm::Filter(TOperationSide Side)
  7235. {
  7236. TCustomDirView * DirView = this->DirView(Side);
  7237. UnicodeString Mask = DirView->Mask;
  7238. if (DoFilterMaskDialog(DirView, Mask))
  7239. {
  7240. DirView->Mask = TFileMasks::NormalizeMask(Mask);
  7241. Configuration->Usage->Inc(L"Filters");
  7242. }
  7243. }
  7244. //---------------------------------------------------------------------------
  7245. TQueueOperation __fastcall TCustomScpExplorerForm::DefaultQueueOperation()
  7246. {
  7247. return FQueueController->DefaultOperation();
  7248. }
  7249. //---------------------------------------------------------------------------
  7250. bool __fastcall TCustomScpExplorerForm::AllowQueueOperation(
  7251. TQueueOperation Operation, void ** Param)
  7252. {
  7253. switch (Operation)
  7254. {
  7255. case qoPreferences:
  7256. return true;
  7257. case qoGoTo:
  7258. return ComponentVisible[fcQueueView] && QueueView3->Enabled;
  7259. case qoOnceEmpty:
  7260. return !FQueueController->Empty;
  7261. default:
  7262. return FQueueController->AllowOperation(Operation, Param);
  7263. }
  7264. }
  7265. //---------------------------------------------------------------------------
  7266. void __fastcall TCustomScpExplorerForm::GoToQueue()
  7267. {
  7268. if (DebugAlwaysTrue(QueueView3->Visible))
  7269. {
  7270. QueueView3->SetFocus();
  7271. }
  7272. }
  7273. //---------------------------------------------------------------------------
  7274. void __fastcall TCustomScpExplorerForm::ExecuteQueueOperation(
  7275. TQueueOperation Operation, void * Param)
  7276. {
  7277. if (Operation == qoGoTo)
  7278. {
  7279. GoToQueue();
  7280. }
  7281. else if (Operation == qoPreferences)
  7282. {
  7283. PreferencesDialog(pmQueue);
  7284. }
  7285. else
  7286. {
  7287. FQueueController->ExecuteOperation(Operation, Param);
  7288. }
  7289. }
  7290. //---------------------------------------------------------------------------
  7291. bool __fastcall TCustomScpExplorerForm::GetQueueEnabled()
  7292. {
  7293. return (Queue != NULL) && Queue->Enabled;
  7294. }
  7295. //---------------------------------------------------------------------------
  7296. void __fastcall TCustomScpExplorerForm::ToggleQueueEnabled()
  7297. {
  7298. DebugAssert(Queue != NULL);
  7299. if (Queue != NULL)
  7300. {
  7301. Queue->Enabled = !Queue->Enabled;
  7302. }
  7303. }
  7304. //---------------------------------------------------------------------------
  7305. void __fastcall TCustomScpExplorerForm::QueueView3ContextPopup(
  7306. TObject * /*Sender*/, TPoint & /*MousePos*/, bool & /*Handled*/)
  7307. {
  7308. FQueueActedItem = QueueView3->ItemFocused;
  7309. }
  7310. //---------------------------------------------------------------------------
  7311. /*virtual*/ int __fastcall TCustomScpExplorerForm::GetStaticComponentsHeight()
  7312. {
  7313. return TopDock->Height + QueueSplitter->Height;
  7314. }
  7315. //---------------------------------------------------------------------------
  7316. void __fastcall TCustomScpExplorerForm::QueueSplitterCanResize(
  7317. TObject * /*Sender*/, int & NewSize, bool & Accept)
  7318. {
  7319. // when queue is hidden by double-clicking splitter, stray attempt to
  7320. // resize the panel with strange value arrives, make sure it is ignored
  7321. if (ComponentVisible[fcQueueView])
  7322. {
  7323. int HeightLimit = ClientHeight - GetStaticComponentsHeight() -
  7324. RemotePanel->Constraints->MinHeight;
  7325. if (NewSize > HeightLimit)
  7326. {
  7327. NewSize = HeightLimit;
  7328. }
  7329. }
  7330. else
  7331. {
  7332. Accept = false;
  7333. }
  7334. }
  7335. //---------------------------------------------------------------------------
  7336. void __fastcall TCustomScpExplorerForm::QueueView3StartDrag(TObject * /*Sender*/,
  7337. TDragObject *& /*DragObject*/)
  7338. {
  7339. FQueueActedItem = QueueView3->ItemFocused;
  7340. }
  7341. //---------------------------------------------------------------------------
  7342. void __fastcall TCustomScpExplorerForm::QueueView3DragOver(TObject * /*Sender*/,
  7343. TObject * Source, int X, int Y, TDragState /*State*/, bool & Accept)
  7344. {
  7345. Accept = true;
  7346. if (Source == QueueView3)
  7347. {
  7348. TListItem * DropTarget = QueueView3->GetItemAt(X, Y);
  7349. Accept = (DropTarget != NULL) && (FQueueActedItem != NULL);
  7350. if (Accept)
  7351. {
  7352. TQueueItemProxy * QueueItem;
  7353. TQueueItemProxy * DestQueueItem;
  7354. QueueItem = static_cast<TQueueItemProxy *>(FQueueActedItem->Data);
  7355. DestQueueItem = static_cast<TQueueItemProxy *>(DropTarget->Data);
  7356. Accept = (QueueItem != DestQueueItem) &&
  7357. (QueueItem->Status == TQueueItem::qsPending) &&
  7358. (DestQueueItem->Status == TQueueItem::qsPending);
  7359. }
  7360. }
  7361. }
  7362. //---------------------------------------------------------------------------
  7363. void __fastcall TCustomScpExplorerForm::QueueView3DragDrop(TObject * /*Sender*/,
  7364. TObject * /*Source*/, int /*X*/, int /*Y*/)
  7365. {
  7366. if ((FQueueActedItem != NULL) && (QueueView3->DropTarget != NULL))
  7367. {
  7368. TQueueItemProxy * QueueItem;
  7369. TQueueItemProxy * DestQueueItem;
  7370. QueueItem = static_cast<TQueueItemProxy *>(FQueueActedItem->Data);
  7371. DestQueueItem = static_cast<TQueueItemProxy *>(QueueView3->DropTarget->Data);
  7372. QueueItem->Move(DestQueueItem);
  7373. }
  7374. }
  7375. //---------------------------------------------------------------------------
  7376. void __fastcall TCustomScpExplorerForm::QueueView3Enter(TObject * /*Sender*/)
  7377. {
  7378. if ((QueueView3->ItemFocused == NULL) &&
  7379. (QueueView3->Items->Count > 0))
  7380. {
  7381. QueueView3->ItemFocused = QueueView3->Items->Item[0];
  7382. }
  7383. QueueLabelUpdateStatus();
  7384. }
  7385. //---------------------------------------------------------------------------
  7386. void __fastcall TCustomScpExplorerForm::QueueView3Exit(TObject * /*Sender*/)
  7387. {
  7388. QueueLabelUpdateStatus();
  7389. }
  7390. //---------------------------------------------------------------------------
  7391. void __fastcall TCustomScpExplorerForm::QueueLabelUpdateStatus()
  7392. {
  7393. QueueLabel->UpdateStatus();
  7394. }
  7395. //---------------------------------------------------------------------------
  7396. void __fastcall TCustomScpExplorerForm::QueueView3SelectItem(
  7397. TObject * /*Sender*/, TListItem * /*Item*/, bool Selected)
  7398. {
  7399. if (Selected)
  7400. {
  7401. NonVisualDataModule->UpdateNonVisibleActions();
  7402. }
  7403. }
  7404. //---------------------------------------------------------------------------
  7405. TDragDropFilesEx * __fastcall TCustomScpExplorerForm::DragDropFiles(TObject * Sender)
  7406. {
  7407. TDragDropFilesEx * Result = NULL;
  7408. if (Sender == SessionsPageControl)
  7409. {
  7410. Result = FSessionsDragDropFilesEx;
  7411. }
  7412. else if (Sender == QueueView3)
  7413. {
  7414. Result = FQueueDragDropFilesEx;
  7415. }
  7416. else
  7417. {
  7418. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  7419. if (DirView != NULL)
  7420. {
  7421. Result = DirView->DragDropFilesEx;
  7422. }
  7423. else
  7424. {
  7425. TCustomDriveView * DriveView = dynamic_cast<TCustomDriveView *>(Sender);
  7426. if (DriveView != NULL)
  7427. {
  7428. Result = DriveView->DragDropFilesEx;
  7429. }
  7430. }
  7431. }
  7432. DebugAssert(Result != NULL);
  7433. return Result;
  7434. }
  7435. //---------------------------------------------------------------------------
  7436. bool __fastcall TCustomScpExplorerForm::SelectedAllFilesInDirView(TCustomDirView * DView)
  7437. {
  7438. return (DView->SelCount == DView->FilesCount);
  7439. }
  7440. //---------------------------------------------------------------------------
  7441. bool __fastcall TCustomScpExplorerForm::DraggingAllFilesFromDirView(TOperationSide Side, TStrings * FileList)
  7442. {
  7443. return HasDirView[Side] && (DropSourceControl == DirView(Side)) && (FileList->Count == DirView(Side)->FilesCount);
  7444. }
  7445. //---------------------------------------------------------------------------
  7446. void __fastcall TCustomScpExplorerForm::RemoteFileControlDragDropFileOperation(
  7447. TObject * Sender, int Effect, UnicodeString TargetPath, bool ForceQueue, bool DragDrop)
  7448. {
  7449. DebugAssert(!IsLocalBrowserMode());
  7450. TFileOperation Operation;
  7451. switch (Effect)
  7452. {
  7453. case DROPEFFECT_MOVE:
  7454. Operation = foMove;
  7455. break;
  7456. case DROPEFFECT_COPY:
  7457. // occures on WinXP (reported by user)
  7458. default:
  7459. Operation = foCopy;
  7460. break;
  7461. };
  7462. TDragDropFilesEx * DragDropFilesEx = DragDropFiles(Sender);
  7463. // see a comment in TUnixDirView::PerformItemDragDropOperation
  7464. if (DragDropFilesEx->FileList->Count > 0)
  7465. {
  7466. TStrings * FileList = new TStringList();
  7467. try
  7468. {
  7469. for (int Index = 0; Index < DragDropFilesEx->FileList->Count; Index++)
  7470. {
  7471. FileList->Add(DragDropFilesEx->FileList->Items[Index]->Name);
  7472. }
  7473. FDragDropOperation = true;
  7474. TTransferOperationParam Param;
  7475. Param.TargetDirectory = TargetPath;
  7476. // upload, no temp dirs
  7477. Param.Temp = false;
  7478. Param.DragDrop = DragDrop;
  7479. Param.Options =
  7480. FLAGMASK(DraggingAllFilesFromDirView(osLocal, FileList), coAllFiles);
  7481. if (ForceQueue)
  7482. {
  7483. Param.Queue = asOn;
  7484. }
  7485. bool NoConfirmation = DragDrop ? (WinConfiguration->DDTransferConfirmation == asOff) : false;
  7486. if (ExecuteFileOperation(Operation, osLocal, FileList, NoConfirmation, &Param))
  7487. {
  7488. if (IsFileControl(DropSourceControl, osLocal))
  7489. {
  7490. Configuration->Usage->Inc(L"UploadsDragDropInternal");
  7491. }
  7492. else
  7493. {
  7494. Configuration->Usage->Inc(L"UploadsDragDropExternal");
  7495. }
  7496. }
  7497. }
  7498. __finally
  7499. {
  7500. FDragDropOperation = false;
  7501. delete FileList;
  7502. }
  7503. }
  7504. }
  7505. //---------------------------------------------------------------------------
  7506. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDFileOperation(
  7507. TObject * Sender, int Effect, UnicodeString /*SourcePath*/,
  7508. UnicodeString TargetPath, bool Paste, bool & /*DoOperation*/)
  7509. {
  7510. RemoteFileControlDragDropFileOperation(Sender, Effect, TargetPath, false, !Paste);
  7511. }
  7512. //---------------------------------------------------------------------------
  7513. void __fastcall TCustomScpExplorerForm::RemoteFileContolDDChooseEffect(
  7514. TObject * Sender, int grfKeyState, int & dwEffect)
  7515. {
  7516. DebugAssert(!IsLocalBrowserMode());
  7517. // if any drop effect is allowed at all (e.g. no drop to self and drop to parent)
  7518. if ((dwEffect != DROPEFFECT_NONE) &&
  7519. IsFileControl(DropSourceControl, osRemote))
  7520. {
  7521. // do not allow drop on remote panel (on free space, still allow drop on directories)
  7522. if ((Sender == RemoteDirView) && (DropSourceControl == RemoteDirView) &&
  7523. (RemoteDirView->DropTarget == NULL))
  7524. {
  7525. dwEffect = DROPEFFECT_NONE;
  7526. }
  7527. else
  7528. {
  7529. if (dwEffect == DROPEFFECT_COPY)
  7530. {
  7531. bool MoveCapable = FTerminal->IsCapable[fcRemoteMove];
  7532. // currently we support copying always (at least via temporary directory);
  7533. // remove associated checks once this all proves stable and working
  7534. bool CopyCapable = true;
  7535. // if we do not support neither of operations, there's no discussion
  7536. if (!MoveCapable && !CopyCapable)
  7537. {
  7538. dwEffect = DROPEFFECT_NONE;
  7539. }
  7540. else
  7541. {
  7542. // The default effect inside remote panel is move,
  7543. // unless we do not support it, but support copy
  7544. if (FLAGCLEAR(grfKeyState, MK_CONTROL))
  7545. {
  7546. dwEffect = MoveCapable ? DROPEFFECT_MOVE : DROPEFFECT_COPY;
  7547. }
  7548. else
  7549. {
  7550. // with ctrl-down, we want copy unless it is not supported
  7551. dwEffect = CopyCapable ? DROPEFFECT_COPY : DROPEFFECT_NONE;
  7552. }
  7553. }
  7554. }
  7555. }
  7556. }
  7557. }
  7558. //---------------------------------------------------------------------------
  7559. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDDragFileName(
  7560. TObject * Sender, TRemoteFile * File, UnicodeString & FileName)
  7561. {
  7562. DebugAssert(!IsLocalBrowserMode());
  7563. if (FDDTotalSize >= 0)
  7564. {
  7565. if (File->IsDirectory)
  7566. {
  7567. FDDTotalSize = -1;
  7568. }
  7569. else
  7570. {
  7571. FDDTotalSize += File->Size;
  7572. }
  7573. }
  7574. DebugAssert(!FDragTempDir.IsEmpty());
  7575. // TODO: this is quite ineffective
  7576. // TODO: what if invalid character replacement is disabled?
  7577. FileName = FDragTempDir + GUIConfiguration->CurrentCopyParam.ValidLocalFileName(File->FileName);
  7578. UnicodeString TransferFileName;
  7579. if (Sender == RemoteDriveView)
  7580. {
  7581. TransferFileName = UnixExcludeTrailingBackslash(File->FullFileName);
  7582. }
  7583. else
  7584. {
  7585. TransferFileName = File->FileName;
  7586. }
  7587. FDDFileList->AddObject(TransferFileName, File);
  7588. }
  7589. //---------------------------------------------------------------------------
  7590. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDDragDetect(
  7591. TObject * /*Sender*/, int /*grfKeyState*/, const TPoint & /*DetectStart*/,
  7592. const TPoint & /*Point*/, TDragDetectStatus /*DragStatus*/)
  7593. {
  7594. // sometimes we do not get DDEnd so the list is not released
  7595. SAFE_DESTROY(FDDFileList);
  7596. FDDFileList = new TStringList();
  7597. FDragTempDir = WinConfiguration->TemporaryDir();
  7598. FDDTotalSize = 0;
  7599. FDragDropSshTerminate.reset(NULL);
  7600. }
  7601. //---------------------------------------------------------------------------
  7602. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDQueryContinueDrag(
  7603. TObject * /*Sender*/, BOOL /*FEscapePressed*/, int /*grfKeyState*/,
  7604. HRESULT & Result)
  7605. {
  7606. if (Result == DRAGDROP_S_DROP)
  7607. {
  7608. try
  7609. {
  7610. GlobalDragImageList->HideDragImage();
  7611. try
  7612. {
  7613. RemoteFileControlDDTargetDrop();
  7614. }
  7615. catch(ESshTerminate & E)
  7616. {
  7617. FDragDropSshTerminate.reset(E.Clone());
  7618. }
  7619. }
  7620. catch (Exception &E)
  7621. {
  7622. // If downloading fails we need to cancel drag&drop, otherwise
  7623. // Explorer shows error
  7624. // But by the way exception probably never reach this point as
  7625. // it's catched on way.
  7626. // Fatal exceptions get here (like when opening a secondary shell extension for file duplication fails).
  7627. Result = DRAGDROP_S_CANCEL;
  7628. ShowExtendedException(Terminal, &E);
  7629. }
  7630. }
  7631. }
  7632. //---------------------------------------------------------------------------
  7633. void __fastcall TCustomScpExplorerForm::DirViewMatchMask(
  7634. TObject * /*Sender*/, UnicodeString FileName, bool Directory, __int64 Size,
  7635. TDateTime Modification, UnicodeString Masks, bool & Matches, bool AllowImplicitMatches)
  7636. {
  7637. TFileMasks::TParams MaskParams;
  7638. MaskParams.Size = Size;
  7639. MaskParams.Modification = Modification;
  7640. // this does not re-parse the mask if it is the same as the last time
  7641. FDirViewMatchMask = Masks;
  7642. bool ImplicitMatch;
  7643. Matches =
  7644. // RecurseInclude parameter has no effect as the Path is empty
  7645. FDirViewMatchMask.Matches(FileName, Directory, UnicodeString(L""), &MaskParams, true, ImplicitMatch) &&
  7646. (AllowImplicitMatches || !ImplicitMatch);
  7647. }
  7648. //---------------------------------------------------------------------------
  7649. void __fastcall TCustomScpExplorerForm::DirViewGetOverlay(
  7650. TObject * Sender, TListItem * Item, WORD & Indexes)
  7651. {
  7652. TCustomDirView * DirView = reinterpret_cast<TCustomDirView *>(Sender);
  7653. UnicodeString Ext;
  7654. if (dynamic_cast<TUnixDirView *>(DirView) != NULL)
  7655. {
  7656. Ext = UnixExtractFileExt(DirView->ItemFileName(Item));
  7657. }
  7658. else
  7659. {
  7660. Ext = ExtractFileExt(DirView->ItemFileName(Item));
  7661. }
  7662. if (SameText(Ext, Configuration->PartialExt))
  7663. {
  7664. Indexes |= oiPartial;
  7665. }
  7666. }
  7667. //---------------------------------------------------------------------------
  7668. bool __fastcall TCustomScpExplorerForm::CanPasteToDirViewFromClipBoard()
  7669. {
  7670. return
  7671. DirViewEnabled(osCurrent) &&
  7672. DirView(osCurrent)->CanPasteFromClipBoard();
  7673. }
  7674. //---------------------------------------------------------------------------
  7675. bool __fastcall TCustomScpExplorerForm::CanPasteFromClipBoard()
  7676. {
  7677. bool Result = false;
  7678. if (CanPasteToDirViewFromClipBoard())
  7679. {
  7680. Result = true;
  7681. }
  7682. else
  7683. {
  7684. UnicodeString ClipboardText;
  7685. if (NonEmptyTextFromClipboard(ClipboardText))
  7686. {
  7687. if (StoredSessions->IsUrl(ClipboardText))
  7688. {
  7689. Result = true;
  7690. }
  7691. else
  7692. {
  7693. Result = DirViewEnabled(osCurrent);
  7694. }
  7695. }
  7696. }
  7697. return Result;
  7698. }
  7699. //---------------------------------------------------------------------------
  7700. void __fastcall TCustomScpExplorerForm::PasteFromClipBoard()
  7701. {
  7702. if (DoesClipboardContainOurFiles())
  7703. {
  7704. if (DebugAlwaysTrue(CanPasteToDirViewFromClipBoard()))
  7705. {
  7706. DebugAssert(!IsLocalBrowserMode());
  7707. TTerminalManager * Manager = TTerminalManager::Instance();
  7708. TTerminal * TergetTerminal = Manager->ActiveTerminal;
  7709. Manager->ActiveTerminal = FClipboardTerminal;
  7710. ExecuteFileOperation(foRemoteCopy, osRemote, FClipboardFileList.get(), false, TergetTerminal);
  7711. }
  7712. }
  7713. else if (CanPasteToDirViewFromClipBoard())
  7714. {
  7715. DirView(osCurrent)->PasteFromClipBoard();
  7716. }
  7717. else
  7718. {
  7719. UnicodeString ClipboardText;
  7720. if (NonEmptyTextFromClipboard(ClipboardText))
  7721. {
  7722. if (StoredSessions->IsUrl(ClipboardText))
  7723. {
  7724. NewSession(false, ClipboardText);
  7725. }
  7726. else
  7727. {
  7728. DirView(osCurrent)->Path = ClipboardText;
  7729. }
  7730. }
  7731. }
  7732. }
  7733. //---------------------------------------------------------------------------
  7734. void __fastcall TCustomScpExplorerForm::FileListFromClipboard()
  7735. {
  7736. // TBD
  7737. }
  7738. //---------------------------------------------------------------------------
  7739. void __fastcall TCustomScpExplorerForm::SelectAll(TOperationSide Side, TSelectMode Mode)
  7740. {
  7741. TCustomDirView * ADirView = DirView(Side);
  7742. ADirView->SelectAll(Mode);
  7743. ADirView->SetFocus();
  7744. }
  7745. //---------------------------------------------------------------------------
  7746. void __fastcall TCustomScpExplorerForm::SelectByMask(TOperationSide Side, bool Select)
  7747. {
  7748. TCustomDirView * ADirView = DirView(Side);
  7749. TFileFilter Filter;
  7750. DefaultFileFilter(Filter);
  7751. if (DoSelectMaskDialog(ADirView, Select, Filter))
  7752. {
  7753. Configuration->Usage->Inc(L"MaskSelections");
  7754. ADirView->SelectFiles(Filter, Select);
  7755. ADirView->SetFocus();
  7756. }
  7757. }
  7758. //---------------------------------------------------------------------------
  7759. void __fastcall TCustomScpExplorerForm::RestoreSelectedNames(TOperationSide Side)
  7760. {
  7761. TCustomDirView * ADirView = DirView(Side);
  7762. ADirView->RestoreSelectedNames();
  7763. ADirView->SetFocus();
  7764. }
  7765. //---------------------------------------------------------------------------
  7766. void __fastcall TCustomScpExplorerForm::SelectSameExt(bool Select)
  7767. {
  7768. TCustomDirView * CurrentDirView = DirView(osCurrent);
  7769. if (DebugAlwaysTrue(CurrentDirView->ItemFocused != NULL))
  7770. {
  7771. UnicodeString FileName = CurrentDirView->ItemFileName(CurrentDirView->ItemFocused);
  7772. UnicodeString Ext;
  7773. if (!IsSideLocalBrowser(osCurrent))
  7774. {
  7775. Ext = UnixExtractFileExt(FileName);
  7776. }
  7777. else
  7778. {
  7779. Ext = ExtractFileExt(FileName);
  7780. }
  7781. if (Ext.IsEmpty())
  7782. {
  7783. Ext = L".";
  7784. }
  7785. TFileFilter Filter;
  7786. Filter.Masks = FORMAT(L"*%s", (Ext));
  7787. Filter.Directories = false;
  7788. CurrentDirView->SelectFiles(Filter, Select);
  7789. }
  7790. }
  7791. //---------------------------------------------------------------------------
  7792. UnicodeString __fastcall TCustomScpExplorerForm::FileStatusBarText(
  7793. const TStatusFileInfo & FileInfo, TOperationSide Side)
  7794. {
  7795. UnicodeString Result;
  7796. if (!FIncrementalSearch.IsEmpty() && (Side == GetSide(osCurrent)))
  7797. {
  7798. Result = FormatIncrementalSearchStatus(FIncrementalSearch, FIncrementalSearchHaveNext);
  7799. }
  7800. else if (!IsSideLocalBrowser(Side) && ((Terminal == NULL) || Terminal->Disconnected))
  7801. {
  7802. // noop
  7803. }
  7804. else
  7805. {
  7806. Result =
  7807. FMTLOAD(FILE_INFO_FORMAT,
  7808. (FormatBytes(FileInfo.SelectedSize),
  7809. FormatBytes(FileInfo.FilesSize),
  7810. FormatNumber(FileInfo.SelectedCount),
  7811. FormatNumber(FileInfo.FilesCount)));
  7812. }
  7813. return Result;
  7814. }
  7815. //---------------------------------------------------------------------------
  7816. void __fastcall TCustomScpExplorerForm::FileStatusBarPanelClick(
  7817. TTBXStatusPanel * Panel, TOperationSide Side)
  7818. {
  7819. if (Panel->Index == 1)
  7820. {
  7821. ToggleShowHiddenFiles();
  7822. }
  7823. else if (Panel->Index == 2)
  7824. {
  7825. Filter(Side);
  7826. }
  7827. }
  7828. //---------------------------------------------------------------------------
  7829. void __fastcall TCustomScpExplorerForm::UpdateFileStatusBar(
  7830. TTBXStatusBar * StatusBar, const TStatusFileInfo & FileInfo, TOperationSide Side)
  7831. {
  7832. DebugAssert(!StatusBar->SimplePanel);
  7833. StatusBar->Panels->Items[0]->Caption = FileStatusBarText(FileInfo, Side);
  7834. UpdateFileStatusExtendedPanels(StatusBar, FileInfo);
  7835. }
  7836. //---------------------------------------------------------------------------
  7837. void __fastcall TCustomScpExplorerForm::UpdateFileStatusExtendedPanels(
  7838. TTBXStatusBar * StatusBar, const TStatusFileInfo & FileInfo)
  7839. {
  7840. DebugAssert(StatusBar->Panels->Count >= 3);
  7841. TTBXStatusPanel * HiddenFilesPanel = StatusBar->Panels->Items[1];
  7842. if (FileInfo.HiddenCount > 0)
  7843. {
  7844. HiddenFilesPanel->Caption = FMTLOAD(FILE_INFO_HIDDEN2, (FormatNumber(FileInfo.HiddenCount)));
  7845. HiddenFilesPanel->ViewPriority = 90; // <100 allows hiding panel when it does not fit
  7846. }
  7847. else
  7848. {
  7849. HiddenFilesPanel->ViewPriority = 0;
  7850. // not really necessary, just to cleanup no-longer-valid data
  7851. HiddenFilesPanel->Caption = L"";
  7852. }
  7853. TTBXStatusPanel * FilteredFilesPanel = StatusBar->Panels->Items[2];
  7854. if (FileInfo.FilteredCount > 0)
  7855. {
  7856. FilteredFilesPanel->Caption = FMTLOAD(FILE_INFO_FILTERED2, (FormatNumber(FileInfo.FilteredCount)));
  7857. FilteredFilesPanel->ViewPriority = 90; // <100 allows hiding panel when it does not fit
  7858. }
  7859. else
  7860. {
  7861. FilteredFilesPanel->ViewPriority = 0;
  7862. // not really necessary, just to cleanup no-longer-valid data
  7863. FilteredFilesPanel->Caption = L"";
  7864. }
  7865. }
  7866. //---------------------------------------------------------------------------
  7867. void __fastcall TCustomScpExplorerForm::RemoteStatusBarClick(
  7868. TObject * /*Sender*/)
  7869. {
  7870. if (DirView(osRemote)->Enabled)
  7871. {
  7872. DirView(osRemote)->SetFocus();
  7873. }
  7874. }
  7875. //---------------------------------------------------------------------------
  7876. void __fastcall TCustomScpExplorerForm::ToggleQueueVisibility()
  7877. {
  7878. TQueueViewConfiguration Config = WinConfiguration->QueueView;
  7879. switch (Config.Show)
  7880. {
  7881. case qvShow:
  7882. if ((FQueueStatus != NULL) && (FQueueStatus->Count > 0))
  7883. {
  7884. Config.Show = qvHide;
  7885. }
  7886. else
  7887. {
  7888. Config.Show = Config.LastHideShow;
  7889. }
  7890. break;
  7891. case qvHideWhenEmpty:
  7892. if (ComponentVisible[fcQueueView])
  7893. {
  7894. Config.Show = qvHide;
  7895. }
  7896. else
  7897. {
  7898. Config.LastHideShow = Config.Show;
  7899. Config.Show = qvShow;
  7900. }
  7901. break;
  7902. case qvHide:
  7903. Config.LastHideShow = Config.Show;
  7904. Config.Show = qvShow;
  7905. break;
  7906. }
  7907. WinConfiguration->QueueView = Config;
  7908. }
  7909. //---------------------------------------------------------------------------
  7910. UnicodeString __fastcall TCustomScpExplorerForm::PathForCaption()
  7911. {
  7912. UnicodeString Result;
  7913. if (FTerminal != NULL)
  7914. {
  7915. switch (WinConfiguration->PathInCaption)
  7916. {
  7917. case picShort:
  7918. Result = TTerminalManager::Instance()->GetTerminalShortPath(FTerminal);
  7919. break;
  7920. case picFull:
  7921. Result = FTerminal->CurrentDirectory;
  7922. break;
  7923. }
  7924. }
  7925. return Result;
  7926. }
  7927. //---------------------------------------------------------------------------
  7928. TColor __fastcall TCustomScpExplorerForm::PanelColor()
  7929. {
  7930. TColor Result = GetWindowColor(FSessionColor);
  7931. return Result;
  7932. }
  7933. //---------------------------------------------------------------------------
  7934. TColor __fastcall TCustomScpExplorerForm::DisabledPanelColor()
  7935. {
  7936. TColor Result = (WinConfiguration->UseDarkTheme() ? static_cast<TColor>(RGB(0x40, 0x40, 0x40)) : clBtnFace);
  7937. return Result;
  7938. }
  7939. //---------------------------------------------------------------------------
  7940. void __fastcall TCustomScpExplorerForm::UpdateControls()
  7941. {
  7942. TTerminalManager::Instance()->UpdateAppTitle();
  7943. // WORAKRDOUND: Disabling list view when it is not showing yet does not set its
  7944. // background to gray on Windows 7 (works on Windows 10).
  7945. // See also EnableControl
  7946. if (Showing)
  7947. {
  7948. if (Terminal != NULL)
  7949. {
  7950. // Update path when it changes
  7951. if ((SessionsPageControl->ActivePage != NULL) && (GetSessionTabTerminal(SessionsPageControl->ActivePage) == Terminal))
  7952. {
  7953. UpdateSessionTab(SessionsPageControl->ActivePage);
  7954. }
  7955. }
  7956. NonVisualDataModule->ReconnectSessionAction->Update();
  7957. ReconnectToolbar->Visible = NonVisualDataModule->ReconnectSessionAction->Visible;
  7958. // ReconnectSessionAction is hidden when disabled, so enabling it actualy resizes the toolbar
  7959. CenterReconnectToolbar();
  7960. bool HasTerminal = (Terminal != NULL) && Terminal->Active;
  7961. if (HasTerminal)
  7962. {
  7963. // TODO needed yet with second local browser?
  7964. if (!RemoteDirView->Enabled)
  7965. {
  7966. RemoteDirView->Enabled = true;
  7967. if (FRemoteDirViewWasFocused)
  7968. {
  7969. ActiveControl = RemoteDirView;
  7970. }
  7971. }
  7972. RemoteDirView->Color = PanelColor();
  7973. }
  7974. else
  7975. {
  7976. if (RemoteDirView->Enabled)
  7977. {
  7978. // This is first called when the form is being constructed
  7979. // (not anymore due to Showing test above)
  7980. // but the false is overriden in the constructor later.
  7981. // An even later in TScpCommanderForm::DoShow()
  7982. FRemoteDirViewWasFocused = (ActiveControl == RemoteDirView);
  7983. RemoteDirView->Enabled = false;
  7984. RemoteDirView->Color = DisabledPanelColor();
  7985. }
  7986. }
  7987. RemoteDirView->Font->Color = GetWindowTextColor(RemoteDirView->Color);
  7988. RemoteDriveView->Enabled = RemoteDirView->Enabled;
  7989. RemoteDriveView->Color = RemoteDirView->Color;
  7990. RemoteDriveView->Font->Color = RemoteDirView->Font->Color;
  7991. QueueView3->Enabled = HasTerminal && Terminal->IsCapable[fsBackgroundTransfers];
  7992. QueueView3->Color = QueueView3->Enabled ? GetWindowColor() : DisabledPanelColor();
  7993. QueueView3->Font->Color = GetWindowTextColor(QueueView3->Color);
  7994. QueueLabelUpdateStatus();
  7995. RemoteDirView->DarkMode = WinConfiguration->UseDarkTheme();
  7996. RemoteDriveView->DarkMode = RemoteDirView->DarkMode;
  7997. reinterpret_cast<TTBCustomItem *>(GetComponent(fcRemotePathComboBox))->Enabled = HasTerminal;
  7998. }
  7999. }
  8000. //---------------------------------------------------------------------------
  8001. void __fastcall TCustomScpExplorerForm::DoDirViewLoaded(TCustomDirView * /*Sender*/)
  8002. {
  8003. UpdateControls();
  8004. }
  8005. //---------------------------------------------------------------------------
  8006. void __fastcall TCustomScpExplorerForm::DirViewLoaded(
  8007. TObject * Sender)
  8008. {
  8009. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  8010. DebugAssert(DirView != NULL);
  8011. DoDirViewLoaded(DirView);
  8012. TransferPresetAutoSelect();
  8013. }
  8014. //---------------------------------------------------------------------------
  8015. void __fastcall TCustomScpExplorerForm::StartUpdates()
  8016. {
  8017. TUpdatesConfiguration Updates = WinConfiguration->Updates;
  8018. // first run after installation
  8019. if (double(Updates.LastCheck) == 0)
  8020. {
  8021. // make sure next time there will be an update (if enabled)
  8022. Updates.LastCheck = TDateTime(1);
  8023. WinConfiguration->Updates = Updates;
  8024. }
  8025. else if ((double(Updates.Period) > 0) &&
  8026. (Now() - Updates.LastCheck >= Updates.Period))
  8027. {
  8028. TThreadMethod OnUpdatesChecked = NULL;
  8029. if (!IsUWP())
  8030. {
  8031. OnUpdatesChecked = UpdatesChecked;
  8032. }
  8033. StartUpdateThread(OnUpdatesChecked);
  8034. }
  8035. }
  8036. //---------------------------------------------------------------------------
  8037. void __fastcall TCustomScpExplorerForm::UpdatesChecked()
  8038. {
  8039. UnicodeString Message;
  8040. bool New;
  8041. TQueryType Type;
  8042. GetUpdatesMessage(Message, New, Type, false);
  8043. if (!Message.IsEmpty())
  8044. {
  8045. if (New)
  8046. {
  8047. Message = FMTLOAD(NEW_VERSION_CLICK, (Message));
  8048. }
  8049. if (!New && (Type != qtWarning))
  8050. {
  8051. PostNote(UnformatMessage(Message), 0, UpdatesNoteClicked, NULL);
  8052. }
  8053. else
  8054. {
  8055. Notify(NULL, Message, Type, true, UpdatesNoteClicked);
  8056. }
  8057. if (New)
  8058. {
  8059. Configuration->Usage->Inc(L"UpdateNotifications");
  8060. }
  8061. }
  8062. }
  8063. //---------------------------------------------------------------------------
  8064. void __fastcall TCustomScpExplorerForm::UpdatesNoteClicked(TObject * /*Sender*/)
  8065. {
  8066. RestoreApp();
  8067. bool CanDisplay = !NonVisualDataModule->Busy;
  8068. if (!CanDisplay && (Screen->ActiveForm != NULL))
  8069. {
  8070. CanDisplay = (Screen->ActiveForm->Perform(WM_CAN_DISPLAY_UPDATES, 0, 0) != 0);
  8071. }
  8072. if (CanDisplay)
  8073. {
  8074. Configuration->Usage->Inc(L"UpdateNotificationsClicked");
  8075. CheckForUpdates(true);
  8076. }
  8077. else
  8078. {
  8079. Configuration->Usage->Inc(L"UpdateNotificationsBusy");
  8080. }
  8081. }
  8082. //---------------------------------------------------------------------------
  8083. void __fastcall TCustomScpExplorerForm::GetTransferPresetAutoSelectData(
  8084. TCopyParamRuleData & Data)
  8085. {
  8086. DebugAssert(Terminal != NULL);
  8087. Data.HostName = Terminal->SessionData->HostNameExpanded;
  8088. Data.UserName = Terminal->SessionData->UserNameExpanded;
  8089. Data.RemoteDirectory = RemoteDirView->PathName;
  8090. }
  8091. //---------------------------------------------------------------------------
  8092. void __fastcall TCustomScpExplorerForm::TransferPresetAutoSelect()
  8093. {
  8094. // Terminal can be null when we are changing local directory implicitly,
  8095. // because it has been deleted, while no session is connected
  8096. // (Login dialog is open)
  8097. if (FAllowTransferPresetAutoSelect && (Terminal != NULL))
  8098. {
  8099. DebugAssert(!IsLocalBrowserMode());
  8100. TCopyParamRuleData Data;
  8101. GetTransferPresetAutoSelectData(Data);
  8102. int CopyParamIndex = GUIConfiguration->CopyParamList->Find(Data);
  8103. UnicodeString CopyParamCurrent = GUIConfiguration->CopyParamCurrent;
  8104. if (CopyParamIndex < 0)
  8105. {
  8106. // there is no preset that matches autoselection
  8107. // set preset that we consider "default"
  8108. FCopyParamAutoSelected = L""; // forget last autoselected preset
  8109. GUIConfiguration->CopyParamCurrent = FCopyParamDefault;
  8110. }
  8111. else
  8112. {
  8113. // there is preset matching autoselection
  8114. UnicodeString CopyParamName = GUIConfiguration->CopyParamList->Names[CopyParamIndex];
  8115. if (CopyParamName == FCopyParamAutoSelected)
  8116. {
  8117. // autoselected the same preset as the last time
  8118. // make no change (i.e. preserve custom user preset, if any)
  8119. }
  8120. else
  8121. {
  8122. // autoselected the different preset then the last time (or there
  8123. // was default preset set)
  8124. FCopyParamAutoSelected = CopyParamName; // remember autoselection
  8125. GUIConfiguration->CopyParamCurrent = CopyParamName;
  8126. }
  8127. }
  8128. if (GUIConfiguration->CopyParamCurrent != CopyParamCurrent)
  8129. {
  8130. if (CopyParamIndex >= 0)
  8131. {
  8132. Configuration->Usage->Inc(L"CopyParamAutoSelects");
  8133. }
  8134. TTransferPresetNoteData * Data = new TTransferPresetNoteData;
  8135. try
  8136. {
  8137. int Fmt =
  8138. (CopyParamIndex < 0) ?
  8139. (GUIConfiguration->CopyParamIndex < 0 ? COPY_PARAM_DEFAULT_NORM : COPY_PARAM_DEFAULT_CUSTOM) :
  8140. COPY_PARAM_AUTOSELECTED;
  8141. UnicodeString Message = FMTLOAD(Fmt, (StripHotkey(GUIConfiguration->CopyParamCurrent)));
  8142. Data->Message = MainInstructions(Message);
  8143. int CopyParamAttrs = Terminal->UsableCopyParamAttrs(0).General;
  8144. UnicodeString Info = GUIConfiguration->CurrentCopyParam.GetInfoStr(L"\n",
  8145. CopyParamAttrs);
  8146. if (CopyParamIndex >= 0)
  8147. {
  8148. DebugAssert(GUIConfiguration->CopyParamList->Rules[CopyParamIndex] != NULL);
  8149. Info = FORMAT(L"%s\n \n%s", (Info,
  8150. FMTLOAD(COPY_PARAM_RULE,
  8151. (GUIConfiguration->CopyParamList->Rules[CopyParamIndex]->GetInfoStr(L"\n")))));
  8152. }
  8153. Data->Message += L"\n\n" + Info;
  8154. if (WinConfiguration->CopyParamAutoSelectNotice)
  8155. {
  8156. TransferPresetNoteMessage(Data, true);
  8157. }
  8158. else
  8159. {
  8160. PostNote(Message, 0, TransferPresetNoteClicked, Data);
  8161. Data = NULL; // ownership passed
  8162. }
  8163. }
  8164. __finally
  8165. {
  8166. delete Data;
  8167. }
  8168. }
  8169. }
  8170. }
  8171. //---------------------------------------------------------------------------
  8172. void __fastcall TCustomScpExplorerForm::TransferPresetNoteMessage(
  8173. TTransferPresetNoteData * NoteData, bool AllowNeverAskAgain)
  8174. {
  8175. DebugAssert(NoteData != NULL);
  8176. DebugAssert(!IsLocalBrowserMode());
  8177. TMessageParams Params(AllowNeverAskAgain ? mpNeverAskAgainCheck : 0);
  8178. TQueryButtonAlias Aliases[1];
  8179. Aliases[0].Button = qaIgnore; // "ignore" is after "ok"
  8180. Aliases[0].Alias = LoadStr(CONFIGURE_BUTTON);
  8181. Params.Aliases = Aliases;
  8182. Params.AliasesCount = LENOF(Aliases);
  8183. unsigned int Result =
  8184. MoreMessageDialog(NoteData->Message, NULL, qtInformation,
  8185. qaOK | qaIgnore, HELP_COPY_PARAM_AUTOSELECTED, &Params);
  8186. switch (Result)
  8187. {
  8188. case qaNeverAskAgain:
  8189. DebugAssert(AllowNeverAskAgain);
  8190. WinConfiguration->CopyParamAutoSelectNotice = false;
  8191. break;
  8192. case qaIgnore:
  8193. PreferencesDialog(pmPresets);
  8194. break;
  8195. }
  8196. }
  8197. //---------------------------------------------------------------------------
  8198. void __fastcall TCustomScpExplorerForm::TransferPresetNoteClicked(TObject * Sender)
  8199. {
  8200. // as of now useless, as this is used for notes only, never for balloons, ...
  8201. RestoreApp();
  8202. // .. and we should never be busy here
  8203. if (DebugAlwaysTrue(!NonVisualDataModule->Busy))
  8204. {
  8205. TransferPresetNoteMessage(DebugNotNull(dynamic_cast<TTransferPresetNoteData *>(Sender)), false);
  8206. }
  8207. }
  8208. //---------------------------------------------------------------------------
  8209. void __fastcall TCustomScpExplorerForm::PreferencesDialog(
  8210. TPreferencesMode APreferencesMode)
  8211. {
  8212. std::unique_ptr<TPreferencesDialogData> PreferencesData;
  8213. TCopyParamRuleData Data;
  8214. if (Terminal != NULL)
  8215. {
  8216. PreferencesData.reset(new TPreferencesDialogData());
  8217. GetTransferPresetAutoSelectData(Data);
  8218. PreferencesData->CopyParamRuleData = &Data;
  8219. }
  8220. DoPreferencesDialog(APreferencesMode, PreferencesData.get());
  8221. }
  8222. //---------------------------------------------------------------------------
  8223. void __fastcall TCustomScpExplorerForm::AdHocCustomCommandValidate(
  8224. const TCustomCommandType & Command)
  8225. {
  8226. if (CustomCommandState(Command, FEditingFocusedAdHocCommand, ccltAll) <= 0)
  8227. {
  8228. throw Exception(FMTLOAD(CUSTOM_COMMAND_IMPOSSIBLE, (Command.Command)));
  8229. }
  8230. }
  8231. //---------------------------------------------------------------------------
  8232. void __fastcall TCustomScpExplorerForm::AdHocCustomCommand(bool OnFocused)
  8233. {
  8234. bool RemoteAllowed = CustomCommandRemoteAllowed();
  8235. TCustomCommandType Command;
  8236. // make sure we use local custom command when remote are not supported
  8237. if (RemoteAllowed || FLAGSET(FLastCustomCommand.Params, ccLocal))
  8238. {
  8239. Command = FLastCustomCommand;
  8240. }
  8241. else
  8242. {
  8243. Command.Params = Command.Params | ccLocal;
  8244. }
  8245. Command.Name = LoadStr(CUSTOM_COMMAND_AD_HOC_NAME);
  8246. FEditingFocusedAdHocCommand = OnFocused;
  8247. bool LocalSide = IsSideLocalBrowser(FCurrentSide);
  8248. int Options =
  8249. FLAGMASK((!RemoteAllowed || LocalSide), ccoDisableRemote) |
  8250. FLAGMASK(LocalSide, ccoDisableRemoteFiles);
  8251. if (DoCustomCommandDialog(Command, WinConfiguration->CustomCommandList,
  8252. ccmAdHoc, Options, AdHocCustomCommandValidate, NULL))
  8253. {
  8254. FLastCustomCommand = Command;
  8255. UpdateCustomCommandsToolbar();
  8256. ExecuteFileOperation(foCustomCommand, osRemote, OnFocused, false, &Command);
  8257. }
  8258. }
  8259. //---------------------------------------------------------------------------
  8260. void __fastcall TCustomScpExplorerForm::LastCustomCommand(bool OnFocused)
  8261. {
  8262. DebugAssert(!FLastCustomCommand.Command.IsEmpty());
  8263. int State = CustomCommandState(FLastCustomCommand, OnFocused, ccltAll);
  8264. DebugAssert(State > 0);
  8265. if (State <= 0)
  8266. {
  8267. throw Exception(FMTLOAD(CUSTOM_COMMAND_IMPOSSIBLE, (FLastCustomCommand.Command)));
  8268. }
  8269. ExecuteFileOperation(foCustomCommand, osRemote, OnFocused, false, &FLastCustomCommand);
  8270. }
  8271. //---------------------------------------------------------------------------
  8272. bool __fastcall TCustomScpExplorerForm::GetLastCustomCommand(bool OnFocused,
  8273. TCustomCommandType & Command, int & State)
  8274. {
  8275. bool Result = !FLastCustomCommand.Command.IsEmpty();
  8276. if (Result)
  8277. {
  8278. Command = FLastCustomCommand;
  8279. State = CustomCommandState(FLastCustomCommand, OnFocused, ccltAll);
  8280. }
  8281. return Result;
  8282. }
  8283. //---------------------------------------------------------------------------
  8284. void __fastcall TCustomScpExplorerForm::BeforeAction()
  8285. {
  8286. ResetIncrementalSearch();
  8287. }
  8288. //---------------------------------------------------------------------------
  8289. void __fastcall TCustomScpExplorerForm::PostComponentHide(Byte Component)
  8290. {
  8291. DebugAssert(ComponentVisible[Component]);
  8292. PostMessage(Handle, WM_COMPONENT_HIDE, Component, 0);
  8293. }
  8294. //---------------------------------------------------------------------------
  8295. void __fastcall TCustomScpExplorerForm::QueueSplitterDblClick(TObject * /*Sender*/)
  8296. {
  8297. // when queue panel is resized here directly, the status bar is stretched
  8298. // over whole space the panel occupied
  8299. PostComponentHide(fcQueueView);
  8300. }
  8301. //---------------------------------------------------------------------------
  8302. void __fastcall TCustomScpExplorerForm::WMWinIniChange(TMessage & Message)
  8303. {
  8304. WinConfiguration->ResetSysDarkTheme();
  8305. ConfigurationChanged();
  8306. ConfigureInterface();
  8307. TForm::Dispatch(&Message);
  8308. }
  8309. //---------------------------------------------------------------------------
  8310. void __fastcall TCustomScpExplorerForm::Dispatch(void * Message)
  8311. {
  8312. TMessage * M = static_cast<TMessage*>(Message);
  8313. switch (M->Msg)
  8314. {
  8315. case CM_DIALOGCHAR:
  8316. CMDialogChar(*M);
  8317. break;
  8318. case WM_APPCOMMAND:
  8319. WMAppCommand(*M);
  8320. break;
  8321. case WM_SYSCOMMAND:
  8322. WMSysCommand(*M);
  8323. break;
  8324. case WM_QUERYENDSESSION:
  8325. WMQueryEndSession(*M);
  8326. break;
  8327. case WM_ENDSESSION:
  8328. WMEndSession(*reinterpret_cast<TWMEndSession *>(M));
  8329. break;
  8330. #ifdef _DEBUG
  8331. case WM_WINDOWPOSCHANGED:
  8332. WMWindowPosChanged(*reinterpret_cast<TWMWindowPosMsg *>(M));
  8333. break;
  8334. case CM_CUSTOMSTYLECHANGED:
  8335. TForm::Dispatch(Message);
  8336. break;
  8337. #endif
  8338. case WM_COMPONENT_HIDE:
  8339. {
  8340. Byte Component = static_cast<Byte>(M->WParam);
  8341. // sanity check
  8342. if (ComponentVisible[Component])
  8343. {
  8344. // special treatment
  8345. if (Component == fcQueueView)
  8346. {
  8347. ToggleQueueVisibility();
  8348. DebugAssert(!ComponentVisible[fcQueueView]);
  8349. }
  8350. else
  8351. {
  8352. ComponentVisible[Component] = false;
  8353. }
  8354. }
  8355. }
  8356. break;
  8357. case WM_COPYDATA:
  8358. WMCopyData(*M);
  8359. break;
  8360. case WM_MANAGES_CAPTION:
  8361. // caption managed in TTerminalManager::UpdateAppTitle()
  8362. M->Result = 1;
  8363. break;
  8364. case WM_WANTS_MOUSEWHEEL:
  8365. M->Result = 1;
  8366. break;
  8367. case CM_SHOWINGCHANGED:
  8368. CMShowingChanged(*M);
  8369. break;
  8370. case WM_CLOSE:
  8371. WMClose(*M);
  8372. break;
  8373. case WM_DPICHANGED:
  8374. WMDpiChanged(*M);
  8375. break;
  8376. case CM_DPICHANGED:
  8377. CMDpiChanged(*M);
  8378. break;
  8379. case WM_WININICHANGE:
  8380. WMWinIniChange(*M);
  8381. break;
  8382. case CM_DIALOGKEY:
  8383. CMDialogKey(*reinterpret_cast<TWMKeyDown *>(M));
  8384. break;
  8385. default:
  8386. TForm::Dispatch(Message);
  8387. break;
  8388. }
  8389. }
  8390. //---------------------------------------------------------------------------
  8391. void __fastcall TCustomScpExplorerForm::UpdateImages()
  8392. {
  8393. // noop
  8394. }
  8395. //---------------------------------------------------------------------------
  8396. void __fastcall TCustomScpExplorerForm::CMDpiChanged(TMessage & Message)
  8397. {
  8398. TForm::Dispatch(&Message);
  8399. FSessionColors->Clear();
  8400. AddFixedSessionImages();
  8401. RegenerateSessionColorsImageList(FSessionColors, FSessionColorMaskImageIndex);
  8402. UpdateImages();
  8403. }
  8404. //---------------------------------------------------------------------------
  8405. void __fastcall TCustomScpExplorerForm::WMDpiChanged(TMessage & Message)
  8406. {
  8407. TForm::Dispatch(&Message);
  8408. Configuration->Usage->Inc(L"PixelsPerInchChanged");
  8409. UpdatePixelsPerInchMainWindowCounter();
  8410. }
  8411. //---------------------------------------------------------------------------
  8412. void __fastcall TCustomScpExplorerForm::WMClose(TMessage & Message)
  8413. {
  8414. // Cannot close window while we are busy.
  8415. // We cannot test this in FormCloseQuery as that is called also from
  8416. // Close(), which is called by CloseApplicationAction. So we can be busy
  8417. // there already even, when it is legitimate to close the application.
  8418. // Possibly a better place to handle this would be WMSysCommand.
  8419. if (NonVisualDataModule->Busy)
  8420. {
  8421. Message.Result = 1;
  8422. }
  8423. else
  8424. {
  8425. TForm::Dispatch(&Message);
  8426. }
  8427. }
  8428. //---------------------------------------------------------------------------
  8429. void __fastcall TCustomScpExplorerForm::CMShowingChanged(TMessage & Message)
  8430. {
  8431. TForm::Dispatch(&Message);
  8432. if (Showing && (Terminal == NULL))
  8433. {
  8434. // When we are starting minimized (i.e. from an installer),
  8435. // postpone showing Login dialog until we get restored.
  8436. // Otherwise the Login dialog (and Authentication window) show restored
  8437. // over invisible (minimized) main window.
  8438. if (WindowState == wsMinimized)
  8439. {
  8440. FNeedSession = true;
  8441. }
  8442. else
  8443. {
  8444. // This happens before application ever goes idle, so the toolbars would
  8445. // stay enabled (initial state) until the Login dialog is dismissed.
  8446. UpdateActions();
  8447. NonVisualDataModule->StartBusy();
  8448. try
  8449. {
  8450. // Need to process WM_ACTIVATEAPP before showing the Login dialog,
  8451. // otherwise the dialog does not receive focus.
  8452. // With Commander interface the ProcessMessages is called already
  8453. // by TDriveView, but with Explorer interface, we need to call it explicily
  8454. Application->ProcessMessages();
  8455. // do not reload sessions, they have been loaded just now (optimization)
  8456. NeedSession(true);
  8457. }
  8458. __finally
  8459. {
  8460. NonVisualDataModule->EndBusy();
  8461. }
  8462. }
  8463. }
  8464. }
  8465. //---------------------------------------------------------------------------
  8466. void __fastcall TCustomScpExplorerForm::CenterReconnectToolbar()
  8467. {
  8468. ReconnectToolbar->Left = (ReconnectToolbar->Parent->ClientWidth - ReconnectToolbar->Width) / 2;
  8469. ReconnectToolbar->Top = (ReconnectToolbar->Parent->ClientHeight - ReconnectToolbar->Height) / 2;
  8470. }
  8471. //---------------------------------------------------------------------------
  8472. void __fastcall TCustomScpExplorerForm::FormConstrainedResize(
  8473. TObject * /*Sender*/, int & MinWidth, int & MinHeight, int & MaxWidth,
  8474. int & MaxHeight)
  8475. {
  8476. // workaround for bug in TWinControl.CalcConstraints
  8477. // Check for empty rect (restore from iconic state) is done there only after
  8478. // call to AdjustClientRect, which enlarges the rect (for forms).
  8479. TRect R = GetClientRect();
  8480. // when restoring from iconic state, place no restrictions
  8481. if (IsRectEmpty(R))
  8482. {
  8483. MinWidth = 0;
  8484. MinHeight = 0;
  8485. MaxWidth = 32000;
  8486. MaxHeight = 32000;
  8487. }
  8488. }
  8489. //---------------------------------------------------------------------------
  8490. void __fastcall TCustomScpExplorerForm::GetSpaceAvailable(const UnicodeString Path,
  8491. TSpaceAvailable & ASpaceAvailable, bool & Close)
  8492. {
  8493. // terminal can be already closed (e.g. dropped connection)
  8494. if ((Terminal != NULL) && Terminal->IsCapable[fcCheckingSpaceAvailable])
  8495. {
  8496. Configuration->Usage->Inc(L"SpaceAvailableChecks");
  8497. try
  8498. {
  8499. Terminal->SpaceAvailable(Path, ASpaceAvailable);
  8500. }
  8501. catch(...)
  8502. {
  8503. if (!Terminal->Active)
  8504. {
  8505. Close = true;
  8506. }
  8507. throw;
  8508. }
  8509. }
  8510. }
  8511. //---------------------------------------------------------------------------
  8512. void __fastcall TCustomScpExplorerForm::FileSystemInfo()
  8513. {
  8514. const TSessionInfo & SessionInfo = Terminal->GetSessionInfo();
  8515. const TFileSystemInfo & FileSystemInfo = Terminal->GetFileSystemInfo(true);
  8516. TGetSpaceAvailable OnGetSpaceAvailable = NULL;
  8517. if (Terminal->IsCapable[fcCheckingSpaceAvailable])
  8518. {
  8519. OnGetSpaceAvailable = GetSpaceAvailable;
  8520. }
  8521. DoFileSystemInfoDialog(SessionInfo, FileSystemInfo, Terminal->CurrentDirectory,
  8522. OnGetSpaceAvailable);
  8523. }
  8524. //---------------------------------------------------------------------------
  8525. TSessionData * __fastcall TCustomScpExplorerForm::SessionDataForCode()
  8526. {
  8527. std::unique_ptr<TSessionData> Data(CloneCurrentSessionData());
  8528. Terminal->FillSessionDataForCode(Data.get());
  8529. return Data.release();
  8530. }
  8531. //---------------------------------------------------------------------------
  8532. void __fastcall TCustomScpExplorerForm::GenerateUrl(TStrings * Paths)
  8533. {
  8534. std::unique_ptr<TSessionData> Data(SessionDataForCode());
  8535. DoGenerateUrlDialog(Data.get(), Paths);
  8536. }
  8537. //---------------------------------------------------------------------------
  8538. void __fastcall TCustomScpExplorerForm::SessionGenerateUrl()
  8539. {
  8540. GenerateUrl(NULL);
  8541. }
  8542. //---------------------------------------------------------------------------
  8543. void __fastcall TCustomScpExplorerForm::FileGenerateUrl()
  8544. {
  8545. std::unique_ptr<TStrings> Paths(new TStringList());
  8546. DirView(osCurrent)->CreateFileList(false, true, Paths.get());
  8547. GenerateUrl(Paths.get());
  8548. }
  8549. //---------------------------------------------------------------------------
  8550. void __fastcall TCustomScpExplorerForm::UpdateSessionColor(TColor value)
  8551. {
  8552. FSessionColor = value;
  8553. TColor C = (value != 0 ? value : Vcl::Graphics::clNone);
  8554. TTBXColorItem * ColorItem = dynamic_cast<TTBXColorItem *>(
  8555. static_cast<TObject *>(GetComponent(fcColorMenu)));
  8556. DebugAssert(ColorItem != NULL);
  8557. ColorItem->Color = C;
  8558. NonVisualDataModule->ColorMenuItem->Color = C;
  8559. UpdateControls();
  8560. }
  8561. //---------------------------------------------------------------------------
  8562. void __fastcall TCustomScpExplorerForm::SetSessionColor(TColor value)
  8563. {
  8564. if (value != FSessionColor)
  8565. {
  8566. UpdateSessionColor(value);
  8567. // Is null when called from LastTerminalClosed
  8568. if (Terminal != NULL)
  8569. {
  8570. SessionsPageControl->ActivePage->ImageIndex = AddSessionColor(value);
  8571. }
  8572. }
  8573. }
  8574. //---------------------------------------------------------------------------
  8575. bool __fastcall TCustomScpExplorerForm::CancelNote(bool Force)
  8576. {
  8577. bool Result = FNoteTimer->Enabled;
  8578. if (Result)
  8579. {
  8580. // cannot cancel note too early
  8581. bool NotEarly =
  8582. (Now() - FNoteShown >
  8583. EncodeTimeVerbose(0, 0, (unsigned short)(WinConfiguration->NotificationsStickTime), 0));
  8584. if (Force || NotEarly)
  8585. {
  8586. FNoteTimer->Enabled = false;
  8587. FNote = L"";
  8588. SAFE_DESTROY(FNoteData);
  8589. FOnNoteClick = NULL;
  8590. FNoteHints = FNotes->Text;
  8591. FNoteHints.Delete(FNoteHints.Length() - 1, 2);
  8592. UpdateStatusBar();
  8593. }
  8594. }
  8595. return Result;
  8596. }
  8597. //---------------------------------------------------------------------------
  8598. void __fastcall TCustomScpExplorerForm::NoteTimer(TObject * /*Sender*/)
  8599. {
  8600. DebugAssert(FNoteTimer->Enabled);
  8601. CancelNote(true);
  8602. }
  8603. //---------------------------------------------------------------------------
  8604. void __fastcall TCustomScpExplorerForm::AddNote(UnicodeString Note, bool UpdateNow)
  8605. {
  8606. int P = Note.Pos(L"\n");
  8607. if (P > 0)
  8608. {
  8609. Note.SetLength(P - 1);
  8610. }
  8611. FNotes->Add(FORMAT(L"[%s] %s",
  8612. (FormatDateTime(Configuration->TimeFormat, Now()), Note)));
  8613. while (FNotes->Count > 10)
  8614. {
  8615. FNotes->Delete(0);
  8616. }
  8617. if (UpdateNow)
  8618. {
  8619. FNoteHints = FNotes->Text;
  8620. FNoteHints.Delete(FNoteHints.Length() - 1, 2);
  8621. UpdateStatusBar();
  8622. }
  8623. }
  8624. //---------------------------------------------------------------------------
  8625. void __fastcall TCustomScpExplorerForm::PostNote(UnicodeString Note,
  8626. unsigned int Seconds, TNotifyEvent OnNoteClick, TObject * NoteData)
  8627. {
  8628. int P = Note.Pos(L"\n");
  8629. if (P > 0)
  8630. {
  8631. Note.SetLength(P - 1);
  8632. }
  8633. FNoteHints = FNotes->Text;
  8634. FNoteHints.Delete(FNoteHints.Length() - 1, 2);
  8635. FNote = Note;
  8636. SAFE_DESTROY(FNoteData);
  8637. FNoteData = NoteData;
  8638. FOnNoteClick = OnNoteClick;
  8639. AddNote(Note, false);
  8640. UpdateStatusBar();
  8641. FNoteShown = Now();
  8642. FNoteTimer->Enabled = false;
  8643. if (Seconds == 0)
  8644. {
  8645. Seconds = WinConfiguration->NotificationsTimeout;
  8646. }
  8647. FNoteTimer->Interval = Seconds * MSecsPerSec;
  8648. FNoteTimer->Enabled = true;
  8649. }
  8650. //---------------------------------------------------------------------------
  8651. void __fastcall TCustomScpExplorerForm::ReadDirectoryCancelled()
  8652. {
  8653. PostNote(LoadStr(DIRECTORY_READING_CANCELLED), 0, NULL, NULL);
  8654. }
  8655. //---------------------------------------------------------------------------
  8656. void __fastcall TCustomScpExplorerForm::SynchronizeBrowsingChanged()
  8657. {
  8658. if (NonVisualDataModule->SynchronizeBrowsingAction->Checked)
  8659. {
  8660. Configuration->Usage->Inc(L"SynchronizeBrowsingEnabled");
  8661. }
  8662. PostNote(FORMAT(LoadStrPart(SYNC_DIR_BROWSE_TOGGLE, 1),
  8663. (LoadStrPart(SYNC_DIR_BROWSE_TOGGLE,
  8664. (NonVisualDataModule->SynchronizeBrowsingAction->Checked ? 2 : 3)))),
  8665. 0, NULL, NULL);
  8666. }
  8667. //---------------------------------------------------------------------------
  8668. void __fastcall TCustomScpExplorerForm::ToggleShowHiddenFiles()
  8669. {
  8670. WinConfiguration->ShowHiddenFiles = !WinConfiguration->ShowHiddenFiles;
  8671. PostNote(FORMAT(LoadStrPart(SHOW_HIDDEN_FILES_TOGGLE, 1),
  8672. (LoadStrPart(SHOW_HIDDEN_FILES_TOGGLE,
  8673. (WinConfiguration->ShowHiddenFiles ? 2 : 3)))), 0, NULL, NULL);
  8674. }
  8675. //---------------------------------------------------------------------------
  8676. void __fastcall TCustomScpExplorerForm::SetFormatSizeBytes(TFormatBytesStyle Style)
  8677. {
  8678. WinConfiguration->FormatSizeBytes = Style;
  8679. }
  8680. //---------------------------------------------------------------------------
  8681. void __fastcall TCustomScpExplorerForm::ToggleAutoReadDirectoryAfterOp()
  8682. {
  8683. Configuration->AutoReadDirectoryAfterOp = !Configuration->AutoReadDirectoryAfterOp;
  8684. PostNote(FORMAT(LoadStrPart(AUTO_READ_DIRECTORY_TOGGLE, 1),
  8685. (LoadStrPart(AUTO_READ_DIRECTORY_TOGGLE,
  8686. (Configuration->AutoReadDirectoryAfterOp ? 2 : 3)))), 0, NULL, NULL);
  8687. }
  8688. //---------------------------------------------------------------------------
  8689. void __fastcall TCustomScpExplorerForm::StatusBarPanelDblClick(
  8690. TTBXCustomStatusBar * Sender, TTBXStatusPanel * Panel)
  8691. {
  8692. if (Panel->Index == 0)
  8693. {
  8694. if (FOnNoteClick != NULL)
  8695. {
  8696. // prevent the user data from being freed by possible call
  8697. // to CancelNote or PostNote during call to OnNoteClick
  8698. std::unique_ptr<TObject> NoteData(FNoteData);
  8699. TNotifyEvent OnNoteClick = FOnNoteClick;
  8700. FNoteData = NULL;
  8701. // need to cancel the note as we are going to delete its user data
  8702. CancelNote(true);
  8703. OnNoteClick(NoteData.get());
  8704. }
  8705. }
  8706. if (Panel->Index >= Sender->Panels->Count - SessionPanelCount)
  8707. {
  8708. FileSystemInfo();
  8709. }
  8710. }
  8711. //---------------------------------------------------------------------------
  8712. void __fastcall TCustomScpExplorerForm::LockWindow(bool Force)
  8713. {
  8714. // workaround:
  8715. // 1) for unknown reason, disabling window, while minimized,
  8716. // prevents it from restoring, even if it was enabled again meanwhile
  8717. // 2) when disabling the main window, while another has focus
  8718. // minimize is no longer possible
  8719. // ("keep up to date" dialog - though does not seem to be the case with "find file" -
  8720. // so it has probably something to do with the semi-modalness of the "keep up to date" dialog)
  8721. // Shouldn't we use IsApplicationMinimized() here?
  8722. if ((FLockSuspendLevel == 0) && !IsIconic(Application->Handle) && (Force || (Screen->ActiveForm == this)))
  8723. {
  8724. Enabled = false;
  8725. }
  8726. FLockLevel++;
  8727. }
  8728. //---------------------------------------------------------------------------
  8729. void __fastcall TCustomScpExplorerForm::UnlockWindow()
  8730. {
  8731. DebugAssert(FLockLevel > 0);
  8732. FLockLevel--;
  8733. if (FLockLevel == 0)
  8734. {
  8735. DebugAssert(FLockSuspendLevel == 0);
  8736. Enabled = true;
  8737. }
  8738. }
  8739. //---------------------------------------------------------------------------
  8740. void __fastcall TCustomScpExplorerForm::SuspendWindowLock()
  8741. {
  8742. // We need to make sure that window is enabled when the last modal window closes
  8743. // otherwise focus is not restored correctly.
  8744. // So we re-enable the window when modal window opens and
  8745. // disable it back after is closes
  8746. if (FLockLevel > 0)
  8747. {
  8748. // while we have nesting counter, we know that we never be called
  8749. // recursivelly as Application->OnModalBegin is called only
  8750. // for the top-level modal window
  8751. if (DebugAlwaysTrue(FLockSuspendLevel == 0))
  8752. {
  8753. // won't be disabled when conditions in LockWindow() were not satisfied
  8754. FDisabledOnLockSuspend = !Enabled;
  8755. // When minimized to tray (or actually when set to SW_HIDE),
  8756. // setting Enabled makes the window focusable even when there's
  8757. // modal window over it
  8758. if (!FTrayIcon->Visible)
  8759. {
  8760. Enabled = true;
  8761. }
  8762. }
  8763. FLockSuspendLevel++;
  8764. }
  8765. }
  8766. //---------------------------------------------------------------------------
  8767. void __fastcall TCustomScpExplorerForm::ResumeWindowLock()
  8768. {
  8769. if (FLockSuspendLevel > 0)
  8770. {
  8771. DebugAssert(FLockLevel > 0);
  8772. FLockSuspendLevel--;
  8773. // see comment in SuspendWindowLock
  8774. if (DebugAlwaysTrue(FLockSuspendLevel == 0))
  8775. {
  8776. // Note that window can be enabled here, when we were minized to tray when
  8777. // was SuspendWindowLock() called.
  8778. // We should possibly do the same check as in LockWindow(),
  8779. // if it is ever possible that the consitions change between
  8780. // SuspendWindowLock() and ResumeWindowLock()
  8781. Enabled = !FDisabledOnLockSuspend;
  8782. }
  8783. }
  8784. }
  8785. //---------------------------------------------------------------------------
  8786. void __fastcall TCustomScpExplorerForm::UpdateRemotePathComboBox(bool TextOnly)
  8787. {
  8788. if (!TextOnly)
  8789. {
  8790. TTBXComboBoxItem * RemotePathComboBox =
  8791. reinterpret_cast<TTBXComboBoxItem *>(GetComponent(fcRemotePathComboBox));
  8792. TStrings * Items = RemotePathComboBox->Strings;
  8793. Items->BeginUpdate();
  8794. try
  8795. {
  8796. Items->Clear();
  8797. if ((Terminal != NULL) && !RemoteDirView->Path.IsEmpty())
  8798. {
  8799. UnicodeString APath = UnixExcludeTrailingBackslash(RemoteDirView->Path);
  8800. while (!IsUnixRootPath(APath))
  8801. {
  8802. int P = APath.LastDelimiter(L'/');
  8803. DebugAssert(P >= 0);
  8804. Items->Insert(0, APath.SubString(P + 1, APath.Length() - P));
  8805. APath.SetLength(P - 1);
  8806. }
  8807. Items->Insert(0, Customunixdirview_SUnixDefaultRootName);
  8808. }
  8809. }
  8810. __finally
  8811. {
  8812. RemotePathComboBox->ItemIndex = Items->Count - 1;
  8813. // Setting ItemIndex to -1 does not reset its text
  8814. if (Items->Count == 0)
  8815. {
  8816. RemotePathComboBox->Text = L"";
  8817. }
  8818. Items->EndUpdate();
  8819. }
  8820. }
  8821. }
  8822. //---------------------------------------------------------------------------
  8823. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxAdjustImageIndex(
  8824. TTBXComboBoxItem * Sender, const UnicodeString /*AText*/, int AIndex,
  8825. int & ImageIndex)
  8826. {
  8827. if (AIndex < 0)
  8828. {
  8829. AIndex = Sender->ItemIndex;
  8830. }
  8831. ImageIndex = (AIndex < Sender->Strings->Count - 1 ? StdDirIcon : StdDirSelIcon);
  8832. }
  8833. //---------------------------------------------------------------------------
  8834. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxDrawItem(
  8835. TTBXCustomList * /*Sender*/, TCanvas * /*ACanvas*/, TRect & ARect, int AIndex,
  8836. int /*AHoverIndex*/, bool & /*DrawDefault*/)
  8837. {
  8838. ARect.Left += (10 * AIndex);
  8839. }
  8840. //---------------------------------------------------------------------------
  8841. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxMeasureWidth(
  8842. TTBXCustomList * /*Sender*/, TCanvas * /*ACanvas*/, int AIndex, int &AWidth)
  8843. {
  8844. AWidth += (10 * AIndex);
  8845. }
  8846. //---------------------------------------------------------------------------
  8847. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxItemClick(
  8848. TObject * Sender)
  8849. {
  8850. TTBXComboBoxItem * RemotePathComboBox = dynamic_cast<TTBXComboBoxItem*>(Sender);
  8851. UnicodeString APath = UnixExcludeTrailingBackslash(RemoteDirView->Path);
  8852. int Index = RemotePathComboBox->ItemIndex;
  8853. while (Index < RemotePathComboBox->Strings->Count - 1)
  8854. {
  8855. APath = UnixExtractFileDir(APath);
  8856. Index++;
  8857. }
  8858. // VanDyke style paths
  8859. if (APath.IsEmpty())
  8860. {
  8861. DebugAssert(RemotePathComboBox->ItemIndex == 0);
  8862. APath = ROOTDIRECTORY;
  8863. }
  8864. if (RemoteDirView->Path != APath)
  8865. {
  8866. RemoteDirView->Path = APath;
  8867. }
  8868. }
  8869. //---------------------------------------------------------------------------
  8870. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxCancel(TObject * Sender)
  8871. {
  8872. DebugAssert(Sender == GetComponent(fcRemotePathComboBox));
  8873. DebugUsedParam(Sender);
  8874. UpdateRemotePathComboBox(true);
  8875. }
  8876. //---------------------------------------------------------------------------
  8877. void __fastcall TCustomScpExplorerForm::DirViewEditing(
  8878. TObject * Sender, TListItem * Item, bool & /*AllowEdit*/)
  8879. {
  8880. ResetIncrementalSearch();
  8881. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  8882. DebugAssert(DirView != NULL);
  8883. if (!WinConfiguration->RenameWholeName && !DirView->ItemIsDirectory(Item))
  8884. {
  8885. HWND Edit = ListView_GetEditControl(DirView->Handle);
  8886. // OnEditing is called also from TCustomListView::CanEdit
  8887. if (Edit != NULL)
  8888. {
  8889. EditSelectBaseName(Edit);
  8890. }
  8891. }
  8892. }
  8893. //---------------------------------------------------------------------------
  8894. //---------------------------------------------------------------------------
  8895. TDragDropFilesEx * __fastcall TCustomScpExplorerForm::CreateDragDropFilesEx()
  8896. {
  8897. TDragDropFilesEx * Result = new TDragDropFilesEx(this);
  8898. Result->AutoDetectDnD = false;
  8899. Result->NeedValid = TFileExMustDnDSet() << nvFilename;
  8900. Result->RenderDataOn = rdoEnterAndDropSync;
  8901. Result->TargetEffects = TDropEffectSet() << deCopy << deMove;
  8902. return Result;
  8903. }
  8904. //---------------------------------------------------------------------------
  8905. void __fastcall TCustomScpExplorerForm::CreateWnd()
  8906. {
  8907. TForm::CreateWnd();
  8908. if (FSessionsDragDropFilesEx == NULL)
  8909. {
  8910. FSessionsDragDropFilesEx = CreateDragDropFilesEx();
  8911. FSessionsDragDropFilesEx->OnDragOver = SessionsDDDragOver;
  8912. FSessionsDragDropFilesEx->OnProcessDropped = SessionsDDProcessDropped;
  8913. FSessionsDragDropFilesEx->OnDragEnter = SessionsDDDragEnter;
  8914. FSessionsDragDropFilesEx->OnDragLeave = SessionsDDDragLeave;
  8915. }
  8916. if (FQueueDragDropFilesEx == NULL)
  8917. {
  8918. FQueueDragDropFilesEx = CreateDragDropFilesEx();
  8919. // No need to set OnDragOver as we do not have any restrictions
  8920. FQueueDragDropFilesEx->OnProcessDropped = QueueDDProcessDropped;
  8921. FQueueDragDropFilesEx->OnDragEnter = QueueDDDragEnter;
  8922. FQueueDragDropFilesEx->OnDragLeave = QueueDDDragLeave;
  8923. }
  8924. }
  8925. //---------------------------------------------------------------------------
  8926. void __fastcall TCustomScpExplorerForm::DestroyWnd()
  8927. {
  8928. TForm::DestroyWnd();
  8929. FSessionsDragDropFilesEx->DragDropControl = NULL;
  8930. FQueueDragDropFilesEx->DragDropControl = NULL;
  8931. }
  8932. //---------------------------------------------------------------------------
  8933. void __fastcall TCustomScpExplorerForm::FormShow(TObject * /*Sender*/)
  8934. {
  8935. SideEnter(FCurrentSide);
  8936. FEverShown = true;
  8937. }
  8938. //---------------------------------------------------------------------------
  8939. void __fastcall TCustomScpExplorerForm::DoFindFiles(
  8940. TTerminal * ATerminal, UnicodeString Directory, const TFileMasks & FileMask,
  8941. TFileFoundEvent OnFileFound, TFindingFileEvent OnFindingFile)
  8942. {
  8943. if (!NonVisualDataModule->Busy)
  8944. {
  8945. TTerminalManager::Instance()->ActiveTerminal = DebugNotNull(dynamic_cast<TManagedTerminal *>(ATerminal));
  8946. Configuration->Usage->Inc(L"FileFinds");
  8947. LockWindow(true);
  8948. NonVisualDataModule->StartBusy();
  8949. WinConfiguration->LockedInterface = true;
  8950. try
  8951. {
  8952. FTerminal->FilesFind(Directory, FileMask, OnFileFound, OnFindingFile);
  8953. }
  8954. __finally
  8955. {
  8956. WinConfiguration->LockedInterface = false;
  8957. NonVisualDataModule->EndBusy();
  8958. UnlockWindow();
  8959. }
  8960. }
  8961. }
  8962. //---------------------------------------------------------------------------
  8963. void __fastcall TCustomScpExplorerForm::DoFocusRemotePath(TTerminal * ATerminal, const UnicodeString & Path)
  8964. {
  8965. if (!NonVisualDataModule->Busy)
  8966. {
  8967. TTerminalManager::Instance()->ActiveTerminal = DebugNotNull(dynamic_cast<TManagedTerminal *>(ATerminal));
  8968. SetFocus();
  8969. RemoteDirView->Path = UnixExtractFilePath(Path);
  8970. UnicodeString FileName = UnixExtractFileName(Path);
  8971. TListItem * Item = RemoteDirView->FindFileItem(FileName);
  8972. // If not found, it can because we have loaded directory from cache and the file is new, try again after reload.
  8973. // (tbd: we should not even try this, if we have not loaded the directory from the cache)
  8974. if (Item == NULL)
  8975. {
  8976. ReloadDirectory(osRemote);
  8977. Item = RemoteDirView->FindFileItem(FileName);
  8978. }
  8979. if (Item != NULL)
  8980. {
  8981. RemoteDirView->ItemFocused = Item;
  8982. RemoteDirView->ItemFocused->MakeVisible(false);
  8983. RemoteDirView->SetFocus();
  8984. }
  8985. }
  8986. }
  8987. //---------------------------------------------------------------------------
  8988. bool __fastcall TCustomScpExplorerForm::CanOperateOnFoundFiles(TTerminal * ATerminal)
  8989. {
  8990. bool Result = !NonVisualDataModule->Busy;
  8991. if (Result)
  8992. {
  8993. TTerminalManager::Instance()->ActiveTerminal = DebugNotNull(dynamic_cast<TManagedTerminal *>(ATerminal));
  8994. }
  8995. return Result;
  8996. }
  8997. //---------------------------------------------------------------------------
  8998. void __fastcall TCustomScpExplorerForm::DoOperationOnFoundFiles(
  8999. TFileOperation Operation, TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  9000. {
  9001. if (CanOperateOnFoundFiles(ATerminal))
  9002. {
  9003. TValueRestorer<TFileOperationFinishedEvent> OnFileOperationFinishedRestorer(FOnFileOperationFinished);
  9004. FOnFileOperationFinished = OnFileOperationFinished;
  9005. ExecuteFileOperation(Operation, osRemote, FileList, false, NULL);
  9006. }
  9007. }
  9008. //---------------------------------------------------------------------------
  9009. void __fastcall TCustomScpExplorerForm::DoDeleteFoundFiles(
  9010. TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  9011. {
  9012. DoOperationOnFoundFiles(foDelete, ATerminal, FileList, OnFileOperationFinished);
  9013. }
  9014. //---------------------------------------------------------------------------
  9015. void __fastcall TCustomScpExplorerForm::DoDownloadFoundFiles(
  9016. TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  9017. {
  9018. DoOperationOnFoundFiles(foCopy, ATerminal, FileList, OnFileOperationFinished);
  9019. }
  9020. //---------------------------------------------------------------------------
  9021. void __fastcall TCustomScpExplorerForm::DoEditFoundFiles(
  9022. TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  9023. {
  9024. if (CanOperateOnFoundFiles(ATerminal))
  9025. {
  9026. for (int Index = 0; Index < FileList->Count; Index++)
  9027. {
  9028. UnicodeString FileName = FileList->Strings[Index];
  9029. TRemoteFile * File = static_cast<TRemoteFile *>(FileList->Objects[Index]);
  9030. ExecuteRemoteFile(FileName, File, efDefaultEditor);
  9031. OnFileOperationFinished(FileName, true);
  9032. }
  9033. }
  9034. }
  9035. //---------------------------------------------------------------------------
  9036. void __fastcall TCustomScpExplorerForm::RemoteFindFiles()
  9037. {
  9038. FFileFindTerminal = Terminal;
  9039. ShowFileFindDialog(
  9040. Terminal, RemoteDirView->Path, DoFindFiles, DoFocusRemotePath, DoDeleteFoundFiles,
  9041. DoDownloadFoundFiles, DoEditFoundFiles);
  9042. }
  9043. //---------------------------------------------------------------------------
  9044. void __fastcall TCustomScpExplorerForm::UpdateTaskbarList(ITaskbarList3 * TaskbarList)
  9045. {
  9046. FTaskbarList = TaskbarList;
  9047. }
  9048. //---------------------------------------------------------------------------
  9049. void __fastcall TCustomScpExplorerForm::SessionsPageControlMouseDown(
  9050. TObject * /*Sender*/, TMouseButton Button, TShiftState /*Shift*/, int X, int Y)
  9051. {
  9052. int Index = SessionsPageControl->IndexOfTabAt(X, Y);
  9053. if (Index >= 0)
  9054. {
  9055. if (Button == mbLeft)
  9056. {
  9057. // "Mouse down" is raised only after tab is switched.
  9058. // If switching tab (switching session) takes long enough for user
  9059. // to actually release the button, "mouse down" is still raised,
  9060. // but we do not get "mouse up" event, so dragging is not cancelled,
  9061. // prevent that by not beginning dragging in the first place.
  9062. if (IsKeyPressed(VK_LBUTTON))
  9063. {
  9064. // when user clicks the "+", we get mouse down only after the session
  9065. // is closed, when new session tab is already on X:Y, so dragging
  9066. // starts, prevent that
  9067. if (MilliSecondsBetween(Now(), FSessionsPageControlNewSessionTime) > 500)
  9068. {
  9069. TTerminal * Terminal = GetSessionTabTerminal(SessionsPageControl->Pages[Index]);
  9070. if (Terminal != NULL)
  9071. {
  9072. SessionsPageControl->BeginDrag(false);
  9073. }
  9074. }
  9075. }
  9076. }
  9077. else if (Button == mbMiddle)
  9078. {
  9079. // ignore middle-click for "New session tab"
  9080. TTerminal * Terminal = GetSessionTabTerminal(SessionsPageControl->Pages[Index]);
  9081. if (Terminal != NULL)
  9082. {
  9083. CloseSessionTab(Index);
  9084. }
  9085. }
  9086. }
  9087. }
  9088. //---------------------------------------------------------------------------
  9089. void __fastcall TCustomScpExplorerForm::SessionsPageControlDragDrop(
  9090. TObject * /*Sender*/, TObject * /*Source*/, int X, int Y)
  9091. {
  9092. int Index = SessionsPageControl->IndexOfTabAt(X, Y);
  9093. // do not allow dropping on the "+" tab
  9094. TTerminal * TargetTerminal = GetSessionTabTerminal(SessionsPageControl->Pages[Index]);
  9095. if ((TargetTerminal != NULL) &&
  9096. (SessionsPageControl->ActivePage->PageIndex != Index))
  9097. {
  9098. Configuration->Usage->Inc(L"SessionTabMoves");
  9099. // this is almost redundant as we would recreate tabs in DoTerminalListChanged,
  9100. // but we want to actually prevent that to avoid flicker
  9101. SessionsPageControl->ActivePage->PageIndex = Index;
  9102. TTerminal * Terminal = GetSessionTabTerminal(SessionsPageControl->ActivePage);
  9103. TTerminalManager::Instance()->Move(Terminal, TargetTerminal);
  9104. }
  9105. }
  9106. //---------------------------------------------------------------------------
  9107. void __fastcall TCustomScpExplorerForm::SessionsPageControlDragOver(
  9108. TObject * Sender, TObject * Source, int X, int Y,
  9109. TDragState /*State*/, bool & Accept)
  9110. {
  9111. Accept = (Sender == Source);
  9112. if (Accept)
  9113. {
  9114. int Index = SessionsPageControl->IndexOfTabAt(X, Y);
  9115. TTerminal * Terminal = GetSessionTabTerminal(SessionsPageControl->Pages[Index]);
  9116. // do not allow dragging to the "+" tab
  9117. Accept = (Terminal != NULL);
  9118. }
  9119. }
  9120. //---------------------------------------------------------------------------
  9121. void __fastcall TCustomScpExplorerForm::SessionsDDDragOver(int /*KeyState*/,
  9122. const TPoint & Point, int & Effect)
  9123. {
  9124. int Index = SessionsPageControl->IndexOfTabAt(Point.X, Point.Y);
  9125. if (Index < 0)
  9126. {
  9127. Effect = DROPEFFECT_NONE;
  9128. }
  9129. else
  9130. {
  9131. TTerminal * TargetTerminal = GetSessionTabTerminal(SessionsPageControl->Pages[Index]);
  9132. // do not allow dropping on the "+" tab
  9133. if (TargetTerminal == NULL)
  9134. {
  9135. Effect = DROPEFFECT_NONE;
  9136. }
  9137. else if ((TargetTerminal != Terminal) && (Effect == DROPEFFECT_MOVE))
  9138. {
  9139. Effect = DROPEFFECT_COPY;
  9140. }
  9141. }
  9142. }
  9143. //---------------------------------------------------------------------------
  9144. void __fastcall TCustomScpExplorerForm::SessionsDDProcessDropped(
  9145. TObject * /*Sender*/, int /*KeyState*/, const TPoint & Point, int Effect)
  9146. {
  9147. int Index = SessionsPageControl->IndexOfTabAt(Point.X, Point.Y);
  9148. // do not allow dropping on the "+" tab
  9149. TManagedTerminal * TargetTerminal = GetSessionTabTerminal(SessionsPageControl->Pages[Index]);
  9150. if (TargetTerminal != NULL)
  9151. {
  9152. DebugAssert(!IsFileControl(DropSourceControl, osRemote));
  9153. if (!IsFileControl(DropSourceControl, osRemote))
  9154. {
  9155. TTerminalManager::Instance()->ActiveTerminal = TargetTerminal;
  9156. RemoteFileControlDragDropFileOperation(SessionsPageControl, Effect,
  9157. // Why don't we use Terminal->CurrentDirectory directly?
  9158. TTerminalManager::Instance()->ActiveTerminal->CurrentDirectory,
  9159. // do not force queue, drag drop
  9160. false, true);
  9161. }
  9162. }
  9163. }
  9164. //---------------------------------------------------------------------------
  9165. void __fastcall TCustomScpExplorerForm::QueueDDProcessDropped(
  9166. TObject * /*Sender*/, int /*KeyState*/, const TPoint & /*Point*/, int Effect)
  9167. {
  9168. // Downloads are handled in RemoteFileControlDDEnd
  9169. if (!IsFileControl(DropSourceControl, osRemote))
  9170. {
  9171. RemoteFileControlDragDropFileOperation(QueueView3, Effect,
  9172. Terminal->CurrentDirectory,
  9173. // force queue, drag drop
  9174. true, true);
  9175. }
  9176. }
  9177. //---------------------------------------------------------------------------
  9178. void __fastcall TCustomScpExplorerForm::FormClose(TObject * /*Sender*/, TCloseAction & /*Action*/)
  9179. {
  9180. FShowing = false;
  9181. // Do not save empty workspace
  9182. if (WinConfiguration->AutoSaveWorkspace && (Terminal != NULL))
  9183. {
  9184. std::unique_ptr<TObjectList> DataList(DoCollectWorkspace());
  9185. UnicodeString Name = WorkspaceName();
  9186. DoSaveWorkspace(
  9187. Name, DataList.get(),
  9188. !Configuration->DisablePasswordStoring &&
  9189. WinConfiguration->AutoSaveWorkspacePasswords, false);
  9190. WinConfiguration->LastStoredSession = Name;
  9191. }
  9192. }
  9193. //---------------------------------------------------------------------------
  9194. void __fastcall TCustomScpExplorerForm::RemoteDirViewRead(TObject * /*Sender*/)
  9195. {
  9196. TManagedTerminal * ManagedTerminal =
  9197. dynamic_cast<TManagedTerminal *>(RemoteDirView->Terminal);
  9198. if (ManagedTerminal != NULL)
  9199. {
  9200. ManagedTerminal->DirectoryLoaded = Now();
  9201. }
  9202. }
  9203. //---------------------------------------------------------------------------
  9204. void __fastcall TCustomScpExplorerForm::DirViewSelectItem(TObject * Sender,
  9205. TListItem * /*Item*/, bool /*Selected*/)
  9206. {
  9207. TCustomDirView * DirView = reinterpret_cast<TCustomDirView *>(Sender);
  9208. switch (DirView->LastSelectMethod)
  9209. {
  9210. case smKeyboard:
  9211. Configuration->Usage->Inc(L"KeyboardSelections");
  9212. break;
  9213. case smMouse:
  9214. Configuration->Usage->Inc(L"MouseSelections");
  9215. break;
  9216. }
  9217. }
  9218. //---------------------------------------------------------------------------
  9219. int __fastcall TCustomScpExplorerForm::AddSessionColor(TColor Color)
  9220. {
  9221. if (Color != 0)
  9222. {
  9223. return GetSessionColorImage(FSessionColors, Color, FSessionColorMaskImageIndex);
  9224. }
  9225. else
  9226. {
  9227. return FSessionTabImageIndex;
  9228. }
  9229. }
  9230. //---------------------------------------------------------------------------
  9231. int __fastcall TCustomScpExplorerForm::AddFixedSessionImage(int GlyphsSourceIndex)
  9232. {
  9233. TPngImageCollectionItem * Item =
  9234. GlyphsModule->ExplorerImages->PngImages->Items[GlyphsSourceIndex];
  9235. FSessionColors->AddPng(Item->PngImage);
  9236. return FSessionColors->Count - 1;
  9237. }
  9238. //---------------------------------------------------------------------------
  9239. void __fastcall TCustomScpExplorerForm::AddFixedSessionImages()
  9240. {
  9241. FSessionColors->SetSize(GlyphsModule->ExplorerImages->Width, GlyphsModule->ExplorerImages->Height);
  9242. FNewSessionTabImageIndex = AddFixedSessionImage(NonVisualDataModule->NewSessionAction->ImageIndex);
  9243. FSessionTabImageIndex = AddFixedSessionImage(SiteImageIndex);
  9244. FSessionColorMaskImageIndex = AddFixedSessionImage(SiteColorMaskImageIndex);
  9245. }
  9246. //---------------------------------------------------------------------------
  9247. void __fastcall TCustomScpExplorerForm::CollectItemsWithTextDisplayMode(TWinControl * Control)
  9248. {
  9249. for (int Index = 0; Index < Control->ControlCount; Index++)
  9250. {
  9251. TControl * AControl = Control->Controls[Index];
  9252. TWinControl * WinControl = dynamic_cast<TWinControl *>(AControl);
  9253. if (WinControl != NULL)
  9254. {
  9255. CollectItemsWithTextDisplayMode(WinControl);
  9256. }
  9257. TTBCustomToolbar * Toolbar = dynamic_cast<TTBCustomToolbar *>(AControl);
  9258. if (Toolbar != NULL)
  9259. {
  9260. // we care for top-level items only
  9261. TTBCustomItem * Items = Toolbar->Items;
  9262. for (int ItemIndex = 0; ItemIndex < Items->Count; ItemIndex++)
  9263. {
  9264. TTBCustomItem * Item = Items->Items[ItemIndex];
  9265. if (((Item->DisplayMode == nbdmImageAndText) ||
  9266. (dynamic_cast<TTBXLabelItem *>(Item) != NULL)) &&
  9267. EligibleForImageDisplayMode(Item))
  9268. {
  9269. FItemsWithTextDisplayMode.insert(Item);
  9270. }
  9271. }
  9272. }
  9273. }
  9274. }
  9275. //---------------------------------------------------------------------------
  9276. bool __fastcall TCustomScpExplorerForm::EligibleForImageDisplayMode(TTBCustomItem * /*Item*/)
  9277. {
  9278. return true;
  9279. }
  9280. //---------------------------------------------------------------------------
  9281. bool __fastcall TCustomScpExplorerForm::UpdateToolbarDisplayMode()
  9282. {
  9283. bool SelectiveToolbarText = WinConfiguration->SelectiveToolbarText;
  9284. TTBItemDisplayMode DisplayMode = (SelectiveToolbarText ? nbdmImageAndText : nbdmDefault);
  9285. typedef std::set<TTBCustomItem *> TItemsWithTextDisplayMode;
  9286. TItemsWithTextDisplayMode::iterator i = FItemsWithTextDisplayMode.begin();
  9287. bool Result = true;
  9288. while (Result && (i != FItemsWithTextDisplayMode.end()))
  9289. {
  9290. TTBCustomItem * Item = *i;
  9291. TTBXLabelItem * Label = dynamic_cast<TTBXLabelItem *>(Item);
  9292. if (Label != NULL)
  9293. {
  9294. // optimization
  9295. if (Label->Visible == SelectiveToolbarText)
  9296. {
  9297. Result = false;
  9298. }
  9299. else
  9300. {
  9301. Label->Visible = SelectiveToolbarText;
  9302. }
  9303. }
  9304. else
  9305. {
  9306. // optimization
  9307. if (Item->DisplayMode == DisplayMode)
  9308. {
  9309. Result = false;
  9310. }
  9311. else
  9312. {
  9313. Item->DisplayMode = DisplayMode;
  9314. }
  9315. }
  9316. ++i;
  9317. }
  9318. if (Result)
  9319. {
  9320. UpdateNewSessionTab();
  9321. }
  9322. return Result;
  9323. }
  9324. //---------------------------------------------------------------------------
  9325. void __fastcall TCustomScpExplorerForm::DisplaySystemContextMenu()
  9326. {
  9327. DebugFail();
  9328. }
  9329. //---------------------------------------------------------------------------
  9330. bool __fastcall TCustomScpExplorerForm::IsBusy()
  9331. {
  9332. // Among other, a lock level is non zero, while directory is being loaded,
  9333. // even when it happens as a result of dirview navigation,
  9334. // i.e. when TNonVisualDataModule is NOT FBusy.
  9335. // That's why the TNonVisualDataModule::GetBusy calls this method.
  9336. // Among other this prevents a panel auto update to occur while
  9337. // directory is changing.
  9338. return (FLockLevel > 0) || DirView(osCurrent)->IsEditing();
  9339. }
  9340. //---------------------------------------------------------------------------
  9341. Boolean __fastcall TCustomScpExplorerForm::AllowedAction(TAction * /*Action*/, TActionAllowed Allowed)
  9342. {
  9343. // While the window is disabled, we still seem to process menu shortcuts at least,
  9344. // so stop it at least here.
  9345. // See also TCustomScpExplorerForm::RemoteDirViewBusy
  9346. return
  9347. (Allowed == aaUpdate) ||
  9348. !NonVisualDataModule->Busy;
  9349. }
  9350. //---------------------------------------------------------------------------
  9351. void __fastcall TCustomScpExplorerForm::EditMenuItemPopup(TTBCustomItem * Sender, bool FromLink)
  9352. {
  9353. NonVisualDataModule->EditMenuItemPopup(Sender, FromLink);
  9354. }
  9355. //---------------------------------------------------------------------------
  9356. void __fastcall TCustomScpExplorerForm::DirViewBusy(TObject * /*Sender*/, int Busy, bool & State)
  9357. {
  9358. // This is somewhat redundant to LockWindow() call from
  9359. // TTerminalManager::TerminalReadDirectoryProgress.
  9360. // But disabling window is known not to block keyboard shorcuts
  9361. // (see TCustomScpExplorerForm::AllowedAction), this hopefully works.
  9362. if (Busy > 0)
  9363. {
  9364. if (NonVisualDataModule->Busy)
  9365. {
  9366. State = false;
  9367. }
  9368. else
  9369. {
  9370. NonVisualDataModule->StartBusy();
  9371. // Will be countered for TCustomDirView.DoExecute in DoDirViewExecFile
  9372. LockWindow();
  9373. }
  9374. }
  9375. else if (Busy < 0)
  9376. {
  9377. UnlockWindow();
  9378. NonVisualDataModule->EndBusy();
  9379. }
  9380. else
  9381. {
  9382. State = NonVisualDataModule->Busy;
  9383. }
  9384. }
  9385. //---------------------------------------------------------------------------
  9386. void __fastcall TCustomScpExplorerForm::SessionsPageControlContextPopup(TObject * /*Sender*/, TPoint & MousePos, bool & Handled)
  9387. {
  9388. int Index = SessionsPageControl->IndexOfTabAt(MousePos.X, MousePos.Y);
  9389. // no context menu for "New session tab"
  9390. if ((Index >= 0) && (GetSessionTabTerminal(SessionsPageControl->Pages[Index]) != NULL))
  9391. {
  9392. SessionsPageControl->ActivePageIndex = Index;
  9393. if (DebugAlwaysTrue(SessionTabSwitched()))
  9394. {
  9395. // copied from TControl.WMContextMenu
  9396. SendCancelMode(SessionsPageControl);
  9397. // explicit popup instead of using PopupMenu property
  9398. // to avoid menu to popup somewhere within SessionTabSwitched above,
  9399. // while connecting yet not-connected session and hence
  9400. // allowing an access to commands over not-completelly connected session
  9401. TPoint Point = SessionsPageControl->ClientToScreen(MousePos);
  9402. TPopupMenu * PopupMenu = NonVisualDataModule->SessionsPopup;
  9403. PopupMenu->PopupComponent = SessionsPageControl;
  9404. PopupMenu->Popup(Point.x, Point.y);
  9405. }
  9406. }
  9407. Handled = true;
  9408. }
  9409. //---------------------------------------------------------------------------
  9410. bool __fastcall TCustomScpExplorerForm::CanChangePassword()
  9411. {
  9412. return (Terminal != NULL) && Terminal->Active && Terminal->IsCapable[fcChangePassword];
  9413. }
  9414. //---------------------------------------------------------------------------
  9415. void __fastcall TCustomScpExplorerForm::ChangePassword()
  9416. {
  9417. Configuration->Usage->Inc(L"PasswordChanges");
  9418. std::unique_ptr<TSessionData> ChangePasswordData(Terminal->SessionData->Clone());
  9419. ChangePasswordData->DisableAuthentationsExceptPassword();
  9420. ChangePasswordData->ChangePassword = true;
  9421. std::unique_ptr<TTerminal> ChangePasswordSession(
  9422. Terminal->CreateSecondarySession(L"ChangePassword", ChangePasswordData.get()));
  9423. ChangePasswordSession->AutoReadDirectory = false;
  9424. if (TTerminalManager::Instance()->ConnectTerminal(ChangePasswordSession.get()))
  9425. {
  9426. MessageDialog(MainInstructions(LoadStr(PASSWORD_CHANGED)), qtInformation, qaOK, HELP_CHANGE_PASSWORD);
  9427. }
  9428. else
  9429. {
  9430. Configuration->Usage->Inc(L"PasswordChangesFailed");
  9431. }
  9432. }
  9433. //---------------------------------------------------------------------------
  9434. bool __fastcall TCustomScpExplorerForm::CanPrivateKeyUpload()
  9435. {
  9436. // No nice way to assert SSH2
  9437. return (Terminal != NULL) && Terminal->Active && (Terminal->FSProtocol == cfsSFTP);
  9438. }
  9439. //---------------------------------------------------------------------------
  9440. void __fastcall TCustomScpExplorerForm::PrivateKeyUpload()
  9441. {
  9442. TOperationVisualizer OperationVisualizer;
  9443. UnicodeString FileName = Terminal->SessionData->PublicKeyFile;
  9444. TTerminalManager::Instance()->UploadPublicKey(Terminal, NULL, FileName);
  9445. }
  9446. //---------------------------------------------------------------------------
  9447. void __fastcall TCustomScpExplorerForm::ChangeScale(int M, int D)
  9448. {
  9449. TForm::ChangeScale(M, D);
  9450. int APixelsPerInch = GetControlPixelsPerInch(this);
  9451. HANDLE ResourceModule = GUIConfiguration->ChangeToDefaultResourceModule();
  9452. try
  9453. {
  9454. GlyphsModule->SetPixelsPerInch(APixelsPerInch);
  9455. }
  9456. __finally
  9457. {
  9458. GUIConfiguration->ChangeResourceModule(ResourceModule);
  9459. }
  9460. }
  9461. //---------------------------------------------------------------------------
  9462. void __fastcall TCustomScpExplorerForm::DockContextPopup(TObject * Sender, TPoint & MousePos, bool & /*Handled*/)
  9463. {
  9464. NonVisualDataModule->ControlContextPopup(Sender, MousePos);
  9465. }
  9466. //---------------------------------------------------------------------
  9467. class TPublicControl : public TControl
  9468. {
  9469. friend class TCustomScpExplorerForm;
  9470. };
  9471. //---------------------------------------------------------------------------
  9472. void __fastcall TCustomScpExplorerForm::CopyPopup(TControl * DestControl, TControl * SourceControl)
  9473. {
  9474. static_cast<TPublicControl *>(DestControl)->PopupMenu = static_cast<TPublicControl *>(SourceControl)->PopupMenu;
  9475. static_cast<TPublicControl *>(DestControl)->OnContextPopup = static_cast<TPublicControl *>(SourceControl)->OnContextPopup;
  9476. }
  9477. //---------------------------------------------------------------------------
  9478. void __fastcall TCustomScpExplorerForm::DoBookmarkClick(TOperationSide Side, TObject * Sender)
  9479. {
  9480. TBookmark * Bookmark = DebugNotNull(reinterpret_cast<TBookmark *>(dynamic_cast<TTBCustomItem *>(Sender)->Tag));
  9481. OpenBookmark(Side, Bookmark);
  9482. }
  9483. //---------------------------------------------------------------------------
  9484. void __fastcall TCustomScpExplorerForm::LocalBookmarkClick(TObject * Sender)
  9485. {
  9486. DoBookmarkClick(osLocal, Sender);
  9487. }
  9488. //---------------------------------------------------------------------------
  9489. void __fastcall TCustomScpExplorerForm::RemoteBookmarkClick(TObject * Sender)
  9490. {
  9491. DoBookmarkClick(osRemote, Sender);
  9492. }
  9493. //---------------------------------------------------------------------------
  9494. void __fastcall TCustomScpExplorerForm::CreateOpenDirMenuList(
  9495. TTBCustomItem * Menu, TOperationSide Side, TBookmarkList * BookmarkList)
  9496. {
  9497. // TODO
  9498. if (BookmarkList != NULL)
  9499. {
  9500. TNotifyEvent OnBookmarkClick = (Side == osLocal) ? &LocalBookmarkClick : &RemoteBookmarkClick;
  9501. if (!WinConfiguration->UseLocationProfiles)
  9502. {
  9503. std::unique_ptr<TStringList> Directories(new TStringList());
  9504. Directories->CaseSensitive = (Side == osRemote);
  9505. for (int Index = 0; Index < BookmarkList->Count; Index++)
  9506. {
  9507. TBookmark * Bookmark = BookmarkList->Bookmarks[Index];
  9508. UnicodeString Directory = Bookmark->GetSideDirectory(Side);
  9509. if (!Directory.IsEmpty() && (Directories->IndexOf(Directory) < 0))
  9510. {
  9511. std::unique_ptr<TTBCustomItem> Item(new TTBXItem(Owner));
  9512. Item->Caption = EscapeHotkey(Directory);
  9513. Item->ShortCut = Bookmark->ShortCut;
  9514. Item->OnClick = OnBookmarkClick;
  9515. Item->Tag = reinterpret_cast<int>(Bookmark);
  9516. Directories->Add(Directory);
  9517. Menu->Add(Item.release());
  9518. }
  9519. }
  9520. }
  9521. else
  9522. {
  9523. std::unique_ptr<TStrings> Folders(CreateSortedStringList());
  9524. for (int Index = 0; Index < BookmarkList->Count; Index++)
  9525. {
  9526. TBookmark * Bookmark = BookmarkList->Bookmarks[Index];
  9527. if (!Bookmark->Node.IsEmpty())
  9528. {
  9529. Folders->Add(Bookmark->Node);
  9530. }
  9531. }
  9532. for (int Index = 0; Index < Folders->Count; Index++)
  9533. {
  9534. std::unique_ptr<TTBCustomItem> Item(new TTBXSubmenuItem(Owner));
  9535. Item->Caption = Folders->Strings[Index];
  9536. Item->ImageIndex = NonVisualDataModule->RemoteChangePathAction->ImageIndex;
  9537. Folders->Objects[Index] = Item.get();
  9538. Menu->Add(Item.release());
  9539. }
  9540. for (int Index = 0; Index < BookmarkList->Count; Index++)
  9541. {
  9542. TBookmark * Bookmark = BookmarkList->Bookmarks[Index];
  9543. TTBCustomItem * Parent;
  9544. if (!Bookmark->Node.IsEmpty())
  9545. {
  9546. DebugAssert(Folders->IndexOf(Bookmark->Node) >= 0);
  9547. Parent = dynamic_cast<TTBCustomItem *>(Folders->Objects[Folders->IndexOf(Bookmark->Node)]);
  9548. }
  9549. else
  9550. {
  9551. Parent = Menu;
  9552. }
  9553. std::unique_ptr<TTBCustomItem> Item(new TTBXItem(Owner));
  9554. Item->Caption = Bookmark->Name;
  9555. Item->ShortCut = Bookmark->ShortCut;
  9556. Item->OnClick = OnBookmarkClick;
  9557. Item->Tag = reinterpret_cast<int>(Bookmark);
  9558. if (((Bookmark->Name == Bookmark->Local) || (Bookmark->Name == Bookmark->Remote)) &&
  9559. (Bookmark->Local.IsEmpty() || Bookmark->Remote.IsEmpty()))
  9560. {
  9561. // No hint for location profiles that are actually mere "bookmarks"
  9562. }
  9563. else
  9564. {
  9565. UnicodeString Hint = FORMAT(LoadStrPart(LOCATION_PROFILE_HINT, 1), (Bookmark->Name));
  9566. UnicodeString LongHint;
  9567. if (!Bookmark->Local.IsEmpty())
  9568. {
  9569. AddToList(LongHint, FORMAT(LoadStrPart(LOCATION_PROFILE_HINT, 2), (Bookmark->Local)), L"\n");
  9570. }
  9571. if (!Bookmark->Remote.IsEmpty())
  9572. {
  9573. AddToList(LongHint, FORMAT(LoadStrPart(LOCATION_PROFILE_HINT, 3), (Bookmark->Remote)), L"\n");
  9574. }
  9575. AddToList(Hint, LongHint, L"|");
  9576. Item->Hint = Hint;
  9577. }
  9578. Parent->Add(Item.release());
  9579. }
  9580. }
  9581. AddMenuSeparator(Menu);
  9582. }
  9583. }
  9584. //---------------------------------------------------------------------------
  9585. void __fastcall TCustomScpExplorerForm::CreateOpenDirMenu(TTBCustomItem * Menu, TOperationSide Side)
  9586. {
  9587. Menu->Clear();
  9588. std::unique_ptr<TTBCustomItem> Item;
  9589. Item.reset(new TTBXItem(Owner));
  9590. Item->Action = (Side == osLocal) ? NonVisualDataModule->LocalOpenDirAction : NonVisualDataModule->RemoteOpenDirAction;
  9591. Item->Options = Item->Options << tboDefault;
  9592. Menu->Add(Item.release());
  9593. AddMenuSeparator(Menu);
  9594. if (Terminal != NULL)
  9595. {
  9596. CreateOpenDirMenuList(Menu, Side, WinConfiguration->Bookmarks[Terminal->SessionData->SessionKey]);
  9597. }
  9598. CreateOpenDirMenuList(Menu, Side, WinConfiguration->SharedBookmarks);
  9599. Item.reset(new TTBXItem(Owner));
  9600. Item->Action = (Side == osLocal) ? NonVisualDataModule->LocalAddBookmarkAction : NonVisualDataModule->RemoteAddBookmarkAction;
  9601. Menu->Add(Item.release());
  9602. }
  9603. //---------------------------------------------------------------------------
  9604. bool __fastcall TCustomScpExplorerForm::TryOpenDirectory(TOperationSide Side, const UnicodeString & Path)
  9605. {
  9606. bool Result = true;
  9607. try
  9608. {
  9609. if (Side == osRemote)
  9610. {
  9611. Terminal->ExceptionOnFail = true;
  9612. }
  9613. try
  9614. {
  9615. DirView(Side)->Path = Path;
  9616. }
  9617. __finally
  9618. {
  9619. if (Side == osRemote)
  9620. {
  9621. Terminal->ExceptionOnFail = false;
  9622. }
  9623. }
  9624. }
  9625. catch (Exception & E)
  9626. {
  9627. if ((Side != osRemote) || Terminal->Active)
  9628. {
  9629. ShowExtendedExceptionEx(Terminal, &E);
  9630. Result = false;
  9631. }
  9632. else
  9633. {
  9634. throw;
  9635. }
  9636. }
  9637. return Result;
  9638. }
  9639. //---------------------------------------------------------------------------
  9640. void __fastcall TCustomScpExplorerForm::ReloadDirectory(TOperationSide Side)
  9641. {
  9642. // Make sure there some feedback even when loading tiny local folders
  9643. TInstantOperationVisualizer Visualizer;
  9644. DirView(Side)->ReloadDirectory();
  9645. }
  9646. //---------------------------------------------------------------------------
  9647. void __fastcall TCustomScpExplorerForm::CloseSessionTab(int Index)
  9648. {
  9649. if (Index == SessionsPageControl->TabIndex)
  9650. {
  9651. CloseSession();
  9652. }
  9653. else
  9654. {
  9655. TTabSheet * TabSheet = SessionsPageControl->Pages[Index];
  9656. TTerminal * Terminal = GetSessionTabTerminal(TabSheet);
  9657. TTerminalManager * Manager = TTerminalManager::Instance();
  9658. if (!Terminal->Active)
  9659. {
  9660. Manager->FreeTerminal(Terminal);
  9661. }
  9662. else
  9663. {
  9664. TTerminalQueue * Queue = Manager->FindQueueForTerminal(Terminal);
  9665. if (CanCloseQueue(Queue))
  9666. {
  9667. Manager->FreeTerminal(Terminal);
  9668. }
  9669. }
  9670. }
  9671. }
  9672. //---------------------------------------------------------------------------
  9673. void __fastcall TCustomScpExplorerForm::SessionsPageControlCloseButtonClick(TPageControl * /*Sender*/, int Index)
  9674. {
  9675. CloseSessionTab(Index);
  9676. }
  9677. //---------------------------------------------------------------------------
  9678. void __fastcall TCustomScpExplorerForm::CopyFilesToClipboard(TOperationSide Side, bool OnFocused)
  9679. {
  9680. if (DebugAlwaysTrue(!IsSideLocalBrowser(Side)))
  9681. {
  9682. TInstantOperationVisualizer Visualizer;
  9683. // To trigger ClipboardDataObjectRelease (in case the clipboard already contains our data)
  9684. // before we create a new data object
  9685. ClipboardClear();
  9686. DebugAssert(FClipboardFakeDirectory.IsEmpty());
  9687. DebugAssert(FClipboardFakeMonitors.get() == NULL);
  9688. FClipboardFakeDirectory = CreateFakeTransferDirectory();
  9689. // Intermediate variable to WORKAROUND a crash in compiler
  9690. TObjectList * ClipboardFakeMonitors =
  9691. StartCreationDirectoryMonitorsOnEachDrive(FILE_NOTIFY_CHANGE_DIR_NAME, ClipboardFakeCreated);
  9692. FClipboardFakeMonitors.reset(ClipboardFakeMonitors);
  9693. if (FClipboardDragDropFilesEx.get() == NULL)
  9694. {
  9695. FClipboardDragDropFilesEx.reset(new TDragDropFilesEx(this));
  9696. FClipboardDragDropFilesEx->OnDataObjectRelease = ClipboardDataObjectRelease;
  9697. }
  9698. FClipboardDragDropFilesEx->FileList->Clear();
  9699. FClipboardDragDropFilesEx->FileList->AddItem(NULL, FClipboardFakeDirectory);
  9700. FClipboardDragDropFilesEx->CopyToClipboard();
  9701. FClipboardTerminal = FTerminal;
  9702. // Need full paths, as cwd can be different once files are pasted
  9703. FClipboardFileList.reset(TRemoteFileList::CloneStrings(RemoteDirView->CreateFileList(OnFocused, true)));
  9704. for (int Index = 0; Index < FClipboardFileList->Count; Index++)
  9705. {
  9706. FClipboardFileList->Strings[Index] = UnixExcludeTrailingBackslash(FClipboardFileList->Strings[Index]);
  9707. }
  9708. }
  9709. }
  9710. //---------------------------------------------------------------------------
  9711. void __fastcall TCustomScpExplorerForm::ClipboardClear()
  9712. {
  9713. if (OpenClipboard(0))
  9714. {
  9715. EmptyClipboard(); // Calls ClipboardDataObjectRelease, if clipboard contains our data
  9716. CloseClipboard();
  9717. }
  9718. }
  9719. //---------------------------------------------------------------------------
  9720. bool __fastcall TCustomScpExplorerForm::DoesClipboardContainOurFiles()
  9721. {
  9722. return !FClipboardFakeDirectory.IsEmpty();
  9723. }
  9724. //---------------------------------------------------------------------------
  9725. void __fastcall TCustomScpExplorerForm::ClipboardStop()
  9726. {
  9727. RemoveDir(ApiPath(FClipboardFakeDirectory));
  9728. FClipboardFakeDirectory = UnicodeString();
  9729. FClipboardTerminal = NULL;
  9730. // Also called in Idle()
  9731. FClipboardFakeMonitors.reset(NULL);
  9732. }
  9733. //---------------------------------------------------------------------------
  9734. void __fastcall TCustomScpExplorerForm::ClipboardDataObjectRelease(TObject * /*Sender*/)
  9735. {
  9736. if (DebugAlwaysTrue(!FClipboardFakeDirectory.IsEmpty()))
  9737. {
  9738. ClipboardStop();
  9739. }
  9740. }
  9741. //---------------------------------------------------------------------------
  9742. void __fastcall TCustomScpExplorerForm::ClipboardDownload(const UnicodeString & TargetDirectory, bool NoConfirmation, bool DragDrop)
  9743. {
  9744. TTerminalManager * Manager = TTerminalManager::Instance();
  9745. Manager->ActiveTerminal = FClipboardTerminal;
  9746. TTransferOperationParam Params;
  9747. Params.TargetDirectory = TargetDirectory;
  9748. Params.DragDrop = DragDrop;
  9749. ExecuteFileOperation(foCopy, osRemote, FClipboardFileList.get(), NoConfirmation, &Params);
  9750. }
  9751. //---------------------------------------------------------------------------
  9752. void __fastcall TCustomScpExplorerForm::ClipboardFakeCreated(TObject * /*Sender*/, const UnicodeString FileName)
  9753. {
  9754. // It can actually rarelly happen that some random file is created, while we are shuttting down the monitor
  9755. // (as it pumps a Windows message queue while being shutted down)
  9756. if (DebugAlwaysTrue(!FClipboardFakeDirectory.IsEmpty()) &&
  9757. SameText(ExtractFileName(FileName), ExtractFileName(FClipboardFakeDirectory)))
  9758. {
  9759. // Can fail as it can be e.g. locked by AV, so we retry that later
  9760. bool Removed = RemoveDir(ApiPath(FileName));
  9761. try
  9762. {
  9763. if (!NonVisualDataModule->Busy)
  9764. {
  9765. bool NoConfirmation = (WinConfiguration->DDTransferConfirmation == asOff);
  9766. ClipboardDownload(ExtractFilePath(FileName), NoConfirmation, true);
  9767. }
  9768. }
  9769. __finally
  9770. {
  9771. if (!Removed)
  9772. {
  9773. RemoveDir(ApiPath(FileName));
  9774. }
  9775. }
  9776. }
  9777. }
  9778. //---------------------------------------------------------------------------
  9779. void __fastcall TCustomScpExplorerForm::DirViewGetItemColor(
  9780. TObject * Sender, UnicodeString FileName, bool Directory, __int64 Size, TDateTime Modification, TColor & Color)
  9781. {
  9782. TFileMasks::TParams MaskParams;
  9783. MaskParams.Size = Size;
  9784. MaskParams.Modification = Modification;
  9785. TCustomDirView * DirView = DebugNotNull(dynamic_cast<TCustomDirView *>(Sender));
  9786. for (TFileColorData::TList::const_iterator Iter = FFileColors.begin(); Iter != FFileColors.end(); Iter++)
  9787. {
  9788. bool ImplicitMatch;
  9789. if (Iter->FileMask.Matches(FileName, Directory, DirView->PathName, &MaskParams, false, ImplicitMatch) &&
  9790. !ImplicitMatch)
  9791. {
  9792. Color = Iter->Color;
  9793. break;
  9794. }
  9795. }
  9796. }
  9797. //---------------------------------------------------------------------------
  9798. void __fastcall TCustomScpExplorerForm::DirViewKeyDown(TObject * Sender, WORD & Key, TShiftState)
  9799. {
  9800. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  9801. if (!DirView->IsEditing())
  9802. {
  9803. // Handled here, instead of RemoteDirViewKeyPress (as on Login dialog),
  9804. // as VK_BACK is handled by TCustomDirView.KeyDown and we can swallow it here,
  9805. // while searching
  9806. if (Key == VK_BACK)
  9807. {
  9808. if (!FIncrementalSearch.IsEmpty())
  9809. {
  9810. if (FIncrementalSearch.Length() == 1)
  9811. {
  9812. ResetIncrementalSearch();
  9813. }
  9814. else
  9815. {
  9816. UnicodeString NewText = FIncrementalSearch.SubString(1, FIncrementalSearch.Length() - 1);
  9817. IncrementalSearch(NewText, false, false);
  9818. }
  9819. Key = 0;
  9820. }
  9821. }
  9822. }
  9823. }
  9824. //---------------------------------------------------------------------------
  9825. void __fastcall TCustomScpExplorerForm::DirViewKeyPress(TObject * Sender, wchar_t & Key)
  9826. {
  9827. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  9828. if (!DirView->IsEditing() && (WinConfiguration->PanelSearch != isOff))
  9829. {
  9830. // Filter control sequences.
  9831. // When not searching yet, prefer use of the space for toggling file selection
  9832. // (so we cannot incrementally search for a string starting with a space).
  9833. if ((Key > VK_SPACE) ||
  9834. ((Key == VK_SPACE) && (GetKeyState(VK_CONTROL) >= 0) && !FIncrementalSearch.IsEmpty()))
  9835. {
  9836. IncrementalSearch(FIncrementalSearch + Key, false, false);
  9837. Key = 0;
  9838. }
  9839. }
  9840. }
  9841. //---------------------------------------------------------------------------
  9842. void __fastcall TCustomScpExplorerForm::ResetIncrementalSearch()
  9843. {
  9844. if (!FIncrementalSearch.IsEmpty())
  9845. {
  9846. FIncrementalSearch = L"";
  9847. DirView(osCurrent)->UpdateStatusBar();
  9848. }
  9849. }
  9850. //---------------------------------------------------------------------------
  9851. void __fastcall TCustomScpExplorerForm::IncrementalSearch(const UnicodeString & Text, bool SkipCurrent, bool Reverse)
  9852. {
  9853. TListItem * Item = SearchFile(Text, SkipCurrent, Reverse);
  9854. if (Item == NULL)
  9855. {
  9856. MessageBeep(MB_ICONHAND);
  9857. }
  9858. else
  9859. {
  9860. TCustomDirView * ADirView = DirView(osCurrent);
  9861. {
  9862. TAutoNestingCounter Guard(FIncrementalSearching);
  9863. ADirView->FocusItem(Item);
  9864. if (ADirView->NortonLike == nlOff)
  9865. {
  9866. if ((ADirView->Selected != Item) ||
  9867. (ADirView->SelCount > 1))
  9868. {
  9869. ADirView->ClearSelection();
  9870. ADirView->Selected = Item;
  9871. }
  9872. }
  9873. }
  9874. FIncrementalSearch = Text;
  9875. Item->MakeVisible(false);
  9876. TListItem * NextItem = SearchFile(Text, true, Reverse);
  9877. FIncrementalSearchHaveNext = (NextItem != NULL) && (NextItem != Item);
  9878. ADirView->UpdateStatusBar();
  9879. }
  9880. }
  9881. //---------------------------------------------------------------------------
  9882. TListItem * __fastcall TCustomScpExplorerForm::GetNextFile(TListItem * Item, bool Reverse)
  9883. {
  9884. int Index = Item->Index;
  9885. if (!Reverse)
  9886. {
  9887. Index++;
  9888. if (Index >= Item->Owner->Count)
  9889. {
  9890. Index = 0;
  9891. }
  9892. }
  9893. else
  9894. {
  9895. Index--;
  9896. if (Index < 0)
  9897. {
  9898. Index = Item->Owner->Count - 1;
  9899. }
  9900. }
  9901. return Item->Owner->Item[Index];
  9902. }
  9903. //---------------------------------------------------------------------------
  9904. TListItem * __fastcall TCustomScpExplorerForm::SearchFile(const UnicodeString & Text, bool SkipCurrent, bool Reverse)
  9905. {
  9906. TCustomDirView * ADirView = DirView(osCurrent);
  9907. if (ADirView->Items->Count == 0)
  9908. {
  9909. return NULL;
  9910. }
  9911. else
  9912. {
  9913. TListItem * CurrentItem = (ADirView->ItemFocused != NULL) ? ADirView->ItemFocused : ADirView->Items->Item[0];
  9914. TListItem * Item = CurrentItem;
  9915. if (SkipCurrent)
  9916. {
  9917. Item = DebugNotNull(GetNextFile(Item, Reverse));
  9918. }
  9919. while (true)
  9920. {
  9921. bool Matches = false;
  9922. switch (WinConfiguration->PanelSearch)
  9923. {
  9924. case isNameStartOnly:
  9925. Matches = ContainsTextSemiCaseSensitive(Item->Caption.SubString(1, Text.Length()), Text);
  9926. break;
  9927. case isName:
  9928. Matches = ContainsTextSemiCaseSensitive(Item->Caption, Text);
  9929. break;
  9930. case isAll:
  9931. int ColCount = (ADirView->ViewStyle == vsReport) ? ADirView->ColProperties->Count : 1;
  9932. int Index = 0;
  9933. while ((Index < ColCount) && !Matches)
  9934. {
  9935. bool Visible = (ADirView->ViewStyle == vsReport) ? ADirView->ColProperties->Visible[Index] : true;
  9936. if (Visible)
  9937. {
  9938. Matches = ContainsTextSemiCaseSensitive(ADirView->GetColumnText(Item, Index), Text);
  9939. }
  9940. Index++;
  9941. }
  9942. break;
  9943. }
  9944. if (Matches)
  9945. {
  9946. return Item;
  9947. }
  9948. Item = GetNextFile(Item, Reverse);
  9949. if (Item == CurrentItem)
  9950. {
  9951. return NULL;
  9952. }
  9953. }
  9954. }
  9955. }
  9956. //---------------------------------------------------------------------------
  9957. void __fastcall TCustomScpExplorerForm::DirViewExit(TObject *)
  9958. {
  9959. ResetIncrementalSearch();
  9960. }
  9961. //---------------------------------------------------------------------------
  9962. void __fastcall TCustomScpExplorerForm::CMDialogKey(TWMKeyDown & Message)
  9963. {
  9964. if (Message.CharCode == VK_TAB)
  9965. {
  9966. if (!FIncrementalSearch.IsEmpty())
  9967. {
  9968. TShiftState Shift = KeyDataToShiftState(Message.KeyData);
  9969. bool Reverse = Shift.Contains(ssShift);
  9970. IncrementalSearch(FIncrementalSearch, true, Reverse);
  9971. Message.Result = 1;
  9972. return;
  9973. }
  9974. }
  9975. else if (Message.CharCode == VK_ESCAPE)
  9976. {
  9977. if (!FIncrementalSearch.IsEmpty())
  9978. {
  9979. ResetIncrementalSearch();
  9980. Message.Result = 1;
  9981. return;
  9982. }
  9983. }
  9984. TForm::Dispatch(&Message);
  9985. }
  9986. //---------------------------------------------------------------------------
  9987. void __fastcall TCustomScpExplorerForm::Deactivate()
  9988. {
  9989. TForm::Deactivate();
  9990. // E.g. when switching to an internal editor window
  9991. ResetIncrementalSearch();
  9992. }
  9993. //---------------------------------------------------------------------------
  9994. void __fastcall TCustomScpExplorerForm::ApplicationEventsDeactivate(TObject *)
  9995. {
  9996. // When switching to another application
  9997. ResetIncrementalSearch();
  9998. }
  9999. //---------------------------------------------------------------------------
  10000. void __fastcall TCustomScpExplorerForm::ApplicationEventsModalBegin(TObject *)
  10001. {
  10002. ResetIncrementalSearch();
  10003. }
  10004. //---------------------------------------------------------------------------
  10005. void __fastcall TCustomScpExplorerForm::DirViewChangeFocus(TObject *, TListItem *)
  10006. {
  10007. if (FIncrementalSearching <= 0)
  10008. {
  10009. ResetIncrementalSearch();
  10010. }
  10011. }
  10012. //---------------------------------------------------------------------------
  10013. void __fastcall TCustomScpExplorerForm::RemoteStatusBarMouseDown(TObject *, TMouseButton, TShiftState, int, int)
  10014. {
  10015. CountClicksForWindowPrint(this);
  10016. }
  10017. //---------------------------------------------------------------------------
  10018. void __fastcall TCustomScpExplorerForm::RemoteDirViewResize(TObject *)
  10019. {
  10020. CenterReconnectToolbar();
  10021. }
  10022. //---------------------------------------------------------------------------