CustomScpExplorer.cpp 351 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707
  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. #include <System.IOUtils.hpp>
  34. //---------------------------------------------------------------------------
  35. #pragma package(smart_init)
  36. #pragma link "CustomDirView"
  37. #pragma link "CustomUnixDirView"
  38. #pragma link "IEListView"
  39. #pragma link "NortonLikeListView"
  40. #pragma link "UnixDirView"
  41. #pragma link "CustomDriveView"
  42. #pragma link "UnixDriveView"
  43. #pragma link "CustomDriveView"
  44. #pragma link "TB2Dock"
  45. #pragma link "TBXStatusBars"
  46. #pragma link "TB2Item"
  47. #pragma link "TB2Toolbar"
  48. #pragma link "ThemePageControl"
  49. #pragma link "PathLabel"
  50. #ifndef NO_RESOURCES
  51. #pragma resource "*.dfm"
  52. #endif
  53. //---------------------------------------------------------------------------
  54. #define WM_COMPONENT_HIDE (WM_WINSCP_USER + 4)
  55. static const int SessionPanelCount = 4;
  56. //---------------------------------------------------------------------------
  57. class TMutexGuard
  58. {
  59. public:
  60. TMutexGuard(HANDLE AMutex, int Message = MUTEX_RELEASE_TIMEOUT,
  61. int Timeout = 5000)
  62. {
  63. FMutex = NULL;
  64. unsigned long WaitResult = WaitForSingleObject(AMutex, Timeout);
  65. if (WaitResult == WAIT_TIMEOUT)
  66. {
  67. throw Exception(LoadStr(MUTEX_RELEASE_TIMEOUT));
  68. }
  69. else
  70. {
  71. FMutex = AMutex;
  72. }
  73. }
  74. ~TMutexGuard()
  75. {
  76. if (FMutex != NULL)
  77. {
  78. ReleaseMutex(FMutex);
  79. }
  80. }
  81. private:
  82. HANDLE FMutex;
  83. };
  84. //---------------------------------------------------------------------------
  85. class TWindowLock
  86. {
  87. public:
  88. TWindowLock(TCustomScpExplorerForm * Form) :
  89. FForm(Form)
  90. {
  91. FForm->LockWindow();
  92. }
  93. ~TWindowLock()
  94. {
  95. FForm->UnlockWindow();
  96. }
  97. private:
  98. TCustomScpExplorerForm * FForm;
  99. };
  100. //---------------------------------------------------------------------------
  101. class TAutoBatch
  102. {
  103. public:
  104. TAutoBatch(TCustomScpExplorerForm * Form) :
  105. FForm(Form)
  106. {
  107. FForm->BatchStart(FBatchStorage);
  108. }
  109. ~TAutoBatch()
  110. {
  111. FForm->BatchEnd(FBatchStorage);
  112. }
  113. private:
  114. TCustomScpExplorerForm * FForm;
  115. void * FBatchStorage;
  116. };
  117. //---------------------------------------------------------------------------
  118. struct TTransferOperationParam
  119. {
  120. TTransferOperationParam();
  121. UnicodeString TargetDirectory;
  122. bool Temp;
  123. bool DragDrop;
  124. int Options;
  125. TAutoSwitch Queue;
  126. };
  127. //---------------------------------------------------------------------------
  128. TTransferOperationParam::TTransferOperationParam()
  129. {
  130. Temp = false;
  131. DragDrop = false;
  132. Options = 0;
  133. Queue = asAuto;
  134. }
  135. //---------------------------------------------------------------------------
  136. class TTransferPresetNoteData : public TObject
  137. {
  138. public:
  139. UnicodeString Message;
  140. };
  141. //---------------------------------------------------------------------------
  142. class TTerminalNoteData : public TObject
  143. {
  144. public:
  145. TManagedTerminal * Terminal;
  146. };
  147. //---------------------------------------------------------------------------
  148. __fastcall TCustomScpExplorerForm::TCustomScpExplorerForm(TComponent* Owner):
  149. FFormRestored(false),
  150. TForm(Owner)
  151. {
  152. FCurrentSide = osRemote;
  153. FEverShown = false;
  154. FDocks = new TList();
  155. InitControls();
  156. RestoreParams();
  157. ConfigurationChanged();
  158. RemoteDirView->Invalidate();
  159. DebugAssert(NonVisualDataModule && !NonVisualDataModule->ScpExplorer);
  160. NonVisualDataModule->ScpExplorer = this;
  161. FAutoOperation = false;
  162. FOnFileOperationFinished = NULL;
  163. FForceExecution = false;
  164. FIgnoreNextDialogChar = 0;
  165. FErrorList = NULL;
  166. FSynchronizeProgressForm = NULL;
  167. FProgressForm = NULL;
  168. FRefreshLocalDirectory = false;
  169. FRefreshRemoteDirectory = false;
  170. FDDExtMapFile = NULL;
  171. // CreateMutexW keeps failing with ERROR_NOACCESS
  172. FDDExtMutex = CreateMutexA(NULL, false, AnsiString(DRAG_EXT_MUTEX).c_str());
  173. DebugAssert(FDDExtMutex != NULL);
  174. FDDTargetControl = NULL;
  175. FDelayedDeletionTimer = NULL;
  176. FDelayedDeletionList = new TStringList();
  177. FDDFileList = NULL;
  178. FPendingTempSpaceWarn = false;
  179. FCapturedLog = NULL;
  180. FDragDropOperation = false;
  181. memset(&FHistoryMenu, 0, sizeof(FHistoryMenu));
  182. FAllowTransferPresetAutoSelect = true;
  183. FCopyParamDefault = L"";
  184. FSynchronizeController = NULL;
  185. FPendingQueueActionItem = NULL;
  186. FLockLevel = 0;
  187. FLockSuspendLevel = 0;
  188. FDisabledOnLockSuspend = false;
  189. FAlternativeDelete = false;
  190. FTrayIcon = new ::TTrayIcon(0);
  191. FTrayIcon->OnClick = TrayIconClick;
  192. FMaxQueueLength = 0;
  193. FLastContextPopupScreenPoint = TPoint(-1, -1);
  194. FTransferResumeList = NULL;
  195. FMoveToQueue = false;
  196. FStandaloneEditing = false;
  197. FOnFeedSynchronizeError = NULL;
  198. FNeedSession = false;
  199. FDoNotIdleCurrentTerminal = 0;
  200. FIncrementalSearching = 0;
  201. FIncrementalSearchHaveNext = false;
  202. FEditorManager = new TEditorManager();
  203. FEditorManager->OnFileChange = ExecutedFileChanged;
  204. FEditorManager->OnFileReload = ExecutedFileReload;
  205. FEditorManager->OnFileEarlyClosed = ExecutedFileEarlyClosed;
  206. FEditorManager->OnFileUploadComplete = ExecutedFileUploadComplete;
  207. FLocalEditors = new TList();
  208. FQueueStatus = NULL;
  209. FQueueStatusSection = new TCriticalSection();
  210. FQueueStatusInvalidated = false;
  211. FQueueItemInvalidated = false;
  212. FQueueActedItem = NULL;
  213. FQueueController = new TQueueController(QueueView3);
  214. FUserActionTimer = new TTimer(this);
  215. FUserActionTimer->Enabled = false;
  216. FUserActionTimer->Interval = 10;
  217. FUserActionTimer->OnTimer = UserActionTimer;
  218. FNotes = new TStringList();
  219. FNoteTimer = new TTimer(this);
  220. FNoteTimer->Enabled = false;
  221. FNoteTimer->OnTimer = NoteTimer;
  222. FOnNoteClick = NULL;
  223. FOle32Library = LoadLibrary(L"Ole32.dll");
  224. FDragMoveCursor = FOle32Library != NULL ?
  225. LoadCursor(FOle32Library, MAKEINTRESOURCE(2)) : NULL;
  226. UseSystemSettings(this);
  227. TTBXStringList * TransferList = dynamic_cast<TTBXStringList*>(
  228. static_cast<TObject*>(GetComponent(fcTransferList)));
  229. DebugAssert(TransferList != NULL);
  230. FTransferListHoverIndex = -1;
  231. TransferList->OnChange = TransferListChange;
  232. TransferList->OnDrawItem = TransferListDrawItem;
  233. SetSubmenu(dynamic_cast<TTBXCustomItem *>(static_cast<TObject *>(GetComponent(fcColorMenu))));
  234. SetSubmenu(NonVisualDataModule->ColorMenuItem);
  235. UseDesktopFont(SessionsPageControl);
  236. UpdateSessionsPageControlHeight();
  237. UseDesktopFont(RemoteDirView);
  238. UseDesktopFont(RemoteDriveView);
  239. UseDesktopFont(QueueView3);
  240. UseDesktopFont(QueueLabel);
  241. UseDesktopFont(RemoteStatusBar);
  242. reinterpret_cast<TLabel*>(QueueSplitter)->OnDblClick = QueueSplitterDblClick;
  243. QueueSplitter->ShowHint = true;
  244. RemotePanelSplitter->ShowHint = true;
  245. UpdateImages();
  246. FCustomCommandMenu = CreateTBXPopupMenu(this);
  247. FCustomCommandLocalFileList = NULL;
  248. FCustomCommandRemoteFileList = NULL;
  249. FSessionColors = new TPngImageList(this);
  250. FSessionColors->ColorDepth = cd32Bit;
  251. AddFixedSessionImages();
  252. SessionsPageControl->Images = FSessionColors;
  253. QueueLabel->FocusControl = QueueView3;
  254. UpdateQueueLabel();
  255. FRemoteDirViewWasFocused = true;
  256. CreateHiddenWindow();
  257. StartUpdates();
  258. }
  259. //---------------------------------------------------------------------------
  260. __fastcall TCustomScpExplorerForm::~TCustomScpExplorerForm()
  261. {
  262. // this has to be one of the very first things to do
  263. StopUpdateThread();
  264. // This is needed when shuting down Windows only. Otherwise it's already set NULL from Execute()
  265. TTerminalManager::Instance()->ScpExplorer = NULL;
  266. delete FCustomCommandLocalFileList;
  267. delete FCustomCommandRemoteFileList;
  268. delete FCustomCommandMenu;
  269. delete FTrayIcon;
  270. FTrayIcon = NULL;
  271. FEditorManager->CloseInternalEditors(ForceCloseInternalEditor);
  272. delete FEditorManager;
  273. ForceCloseLocalEditors();
  274. delete FLocalEditors;
  275. HideFileFindDialog();
  276. if (FDelayedDeletionTimer)
  277. {
  278. DoDelayedDeletion(NULL);
  279. SAFE_DESTROY(FDelayedDeletionTimer);
  280. }
  281. SAFE_DESTROY(FDelayedDeletionList);
  282. // sometimes we do not get DDEnd so the list is not released
  283. SAFE_DESTROY(FDDFileList);
  284. DebugAssert(FSynchronizeController == NULL);
  285. CloseHandle(FDDExtMutex);
  286. FDDExtMutex = NULL;
  287. FreeLibrary(FOle32Library);
  288. FOle32Library = NULL;
  289. FDragMoveCursor = NULL;
  290. DebugAssert(!FErrorList);
  291. if (FEverShown)
  292. {
  293. // when window is never shown (like when running command-line operation),
  294. // particularly window site is not restored correctly (BoundsRect value set
  295. // in RestoreForm gets lost during handle allocation), so we do not want
  296. // it to be stored
  297. StoreParams();
  298. }
  299. Terminal = NULL;
  300. Queue = NULL;
  301. DebugAssert(NonVisualDataModule && (NonVisualDataModule->ScpExplorer == this));
  302. NonVisualDataModule->ScpExplorer = NULL;
  303. delete FQueueController;
  304. FQueueController = NULL;
  305. delete FQueueStatusSection;
  306. FQueueStatusSection = NULL;
  307. delete FQueueStatus;
  308. FQueueStatus = NULL;
  309. delete FUserActionTimer;
  310. FUserActionTimer = NULL;
  311. delete FNoteTimer;
  312. delete FNotes;
  313. delete FNoteData;
  314. SAFE_DESTROY(FDocks);
  315. SessionsPageControl->Images = NULL;
  316. // only after clearing Terminal (after DoTerminalListChanged => ... => AddSessionColor is called)
  317. SAFE_DESTROY(FSessionColors);
  318. SAFE_DESTROY(FSessionsDragDropFilesEx);
  319. SAFE_DESTROY(FHistoryMenu[0][0]);
  320. SAFE_DESTROY(FHistoryMenu[0][1]);
  321. SAFE_DESTROY(FHistoryMenu[1][0]);
  322. SAFE_DESTROY(FHistoryMenu[1][1]);
  323. if (FHiddenWindow != NULL)
  324. {
  325. DestroyWindow(FHiddenWindow);
  326. FHiddenWindow = NULL;
  327. }
  328. }
  329. //---------------------------------------------------------------------------
  330. void __fastcall TCustomScpExplorerForm::RefreshPanel(const UnicodeString & Session, const UnicodeString & Path)
  331. {
  332. std::unique_ptr<TSessionData> Data;
  333. if (!Session.IsEmpty())
  334. {
  335. bool DefaultsOnly;
  336. Data.reset(StoredSessions->ParseUrl(Session, NULL, DefaultsOnly));
  337. }
  338. TTerminalManager * Manager = TTerminalManager::Instance();
  339. for (int Index = 0; Index < Manager->Count; Index++)
  340. {
  341. TTerminal * Terminal = Manager->Terminals[Index];
  342. if (Session.IsEmpty() ||
  343. Terminal->SessionData->IsSameSite(Data.get()))
  344. {
  345. if (Path.IsEmpty())
  346. {
  347. Terminal->ClearCaches();
  348. }
  349. else
  350. {
  351. Terminal->DirectoryModified(Path, true);
  352. }
  353. }
  354. }
  355. // We should flag a pending refresh for the background terminals or busy foreground terminals
  356. if ((Terminal != NULL) && Terminal->Active &&
  357. CanCommandLineFromAnotherInstance() &&
  358. (Session.IsEmpty() || Terminal->SessionData->IsSameSite(Data.get())) &&
  359. (Path.IsEmpty() || UnixIsChildPath(Path, Terminal->CurrentDirectory)))
  360. {
  361. Terminal->ReloadDirectory();
  362. }
  363. }
  364. //---------------------------------------------------------------------------
  365. void __fastcall TCustomScpExplorerForm::WMCopyData(TMessage & Message)
  366. {
  367. PCOPYDATASTRUCT CopyData = reinterpret_cast<PCOPYDATASTRUCT>(Message.LParam);
  368. size_t MessageSize = sizeof(TCopyDataMessage);
  369. bool Result = DebugAlwaysTrue(CopyData->cbData == MessageSize);
  370. if (Result)
  371. {
  372. const TCopyDataMessage & Message = *reinterpret_cast<const TCopyDataMessage *>(CopyData->lpData);
  373. Result = (Message.Version == TCopyDataMessage::Version1);
  374. if (Result)
  375. {
  376. switch (Message.Command)
  377. {
  378. case TCopyDataMessage::CommandCanCommandLine:
  379. Result = CanCommandLineFromAnotherInstance();
  380. break;
  381. case TCopyDataMessage::CommandCommandLine:
  382. {
  383. UnicodeString CommandLine(Message.CommandLine);
  384. Result = CommandLineFromAnotherInstance(CommandLine);
  385. }
  386. break;
  387. case TCopyDataMessage::RefreshPanel:
  388. RefreshPanel(Message.Refresh.Session, Message.Refresh.Path);
  389. break;
  390. case TCopyDataMessage::MainWindowCheck:
  391. Result = true;
  392. break;
  393. default:
  394. Result = false;
  395. break;
  396. }
  397. }
  398. }
  399. Message.Result = Result ? 1 : 0;
  400. }
  401. //---------------------------------------------------------------------------
  402. void __fastcall TCustomScpExplorerForm::CreateHiddenWindow()
  403. {
  404. WNDCLASS WindowClass = {0};
  405. WindowClass.lpfnWndProc = DefWindowProc;
  406. WindowClass.hInstance = HInstance;
  407. WindowClass.lpszClassName = HIDDEN_WINDOW_NAME;
  408. FHiddenWindow = NULL;
  409. if (RegisterClass(&WindowClass))
  410. {
  411. FHiddenWindow = CreateWindow(HIDDEN_WINDOW_NAME, L"",
  412. WS_POPUP, 0, 0, 0, 0, 0, 0, HInstance, NULL);
  413. }
  414. }
  415. //---------------------------------------------------------------------------
  416. bool __fastcall TCustomScpExplorerForm::CanConsole()
  417. {
  418. return
  419. (Terminal != NULL) &&
  420. Terminal->Active &&
  421. (Terminal->IsCapable[fcAnyCommand] || Terminal->IsCapable[fcSecondaryShell]);
  422. }
  423. //---------------------------------------------------------------------------
  424. bool __fastcall TCustomScpExplorerForm::CanCommandLineFromAnotherInstance()
  425. {
  426. bool Result = Visible && !NonVisualDataModule->Busy;
  427. return Result;
  428. }
  429. //---------------------------------------------------------------------------
  430. bool __fastcall TCustomScpExplorerForm::CommandLineFromAnotherInstance(
  431. const UnicodeString & CommandLine)
  432. {
  433. TProgramParams Params(CommandLine);
  434. bool Result = CanCommandLineFromAnotherInstance() && DebugAlwaysTrue(Params.ParamCount > 0);
  435. if (Result)
  436. {
  437. NonVisualDataModule->StartBusy();
  438. try
  439. {
  440. // this action is initiated from another process,
  441. // so it's likely that our window is not visible,
  442. // and user won't see what is going on
  443. Application->BringToFront();
  444. // reload sessions as we may be asked to open a session
  445. // just stored by another instance
  446. StoredSessions->Reload();
  447. UnicodeString SessionName = Params.Param[1];
  448. std::unique_ptr<TObjectList> DataList(new TObjectList());
  449. UnicodeString DownloadFile; // unused
  450. try
  451. {
  452. GetLoginData(SessionName, &Params, DataList.get(), DownloadFile, true, this);
  453. }
  454. catch (EAbort &)
  455. {
  456. // Happens when opening session in PuTTY and in other situations,
  457. // in which we do not want to fail WM_COPYDATA, as that would cause master instance
  458. // to process the command-line on its own (or to pass it to yet another instance)
  459. }
  460. if (DataList->Count > 0)
  461. {
  462. TTerminalManager * Manager = TTerminalManager::Instance();
  463. Manager->ActiveTerminal = Manager->NewTerminals(DataList.get());
  464. }
  465. }
  466. __finally
  467. {
  468. NonVisualDataModule->EndBusy();
  469. }
  470. }
  471. return Result;
  472. }
  473. //---------------------------------------------------------------------------
  474. void __fastcall TCustomScpExplorerForm::SetTerminal(TManagedTerminal * value)
  475. {
  476. if (FTerminal != value)
  477. {
  478. TerminalChanging();
  479. DoSetTerminal(value, false);
  480. }
  481. }
  482. //---------------------------------------------------------------------------
  483. void __fastcall TCustomScpExplorerForm::DoSetTerminal(TManagedTerminal * value, bool Replace)
  484. {
  485. FTerminal = value;
  486. bool PrevAllowTransferPresetAutoSelect = FAllowTransferPresetAutoSelect;
  487. FAllowTransferPresetAutoSelect = false;
  488. try
  489. {
  490. TerminalChanged(Replace);
  491. }
  492. __finally
  493. {
  494. FAllowTransferPresetAutoSelect = PrevAllowTransferPresetAutoSelect;
  495. }
  496. if (Terminal != NULL)
  497. {
  498. TransferPresetAutoSelect();
  499. }
  500. }
  501. //---------------------------------------------------------------------------
  502. void __fastcall TCustomScpExplorerForm::ReplaceTerminal(TManagedTerminal * value)
  503. {
  504. DoSetTerminal(value, true);
  505. }
  506. //---------------------------------------------------------------------------
  507. void __fastcall TCustomScpExplorerForm::TerminalChanging()
  508. {
  509. if (FTerminal != NULL)
  510. {
  511. UpdateTerminal(Terminal);
  512. }
  513. }
  514. //---------------------------------------------------------------------------
  515. void __fastcall TCustomScpExplorerForm::TerminalChanged(bool Replaced)
  516. {
  517. if (Terminal != NULL)
  518. {
  519. UpdateSessionColor((TColor)Terminal->StateData->Color);
  520. }
  521. DoTerminalListChanged();
  522. DebugAssert(!IsLocalBrowserMode()); // TODO
  523. if (Replaced)
  524. {
  525. RemoteDirView->ReplaceTerminal(Terminal);
  526. }
  527. else
  528. {
  529. RemoteDirView->Terminal = Terminal;
  530. }
  531. NonVisualDataModule->ResetQueueOnceEmptyOperation();
  532. if (Terminal != NULL)
  533. {
  534. if (Terminal->Active)
  535. {
  536. Terminal->RefreshDirectory();
  537. }
  538. if (WinConfiguration->PreservePanelState)
  539. {
  540. if (Terminal->RemoteExplorerState != NULL)
  541. {
  542. DirView(osRemote)->RestoreState(Terminal->RemoteExplorerState);
  543. }
  544. else
  545. {
  546. DirView(osRemote)->ClearState();
  547. }
  548. }
  549. if (!Replaced && Terminal->Active)
  550. {
  551. InitStatusBar();
  552. }
  553. }
  554. UpdateTransferList();
  555. // Update panels Enable state before refreshing the labels
  556. UpdateControls();
  557. }
  558. //---------------------------------------------------------------------------
  559. void __fastcall TCustomScpExplorerForm::SetQueue(TTerminalQueue * value)
  560. {
  561. if (Queue != value)
  562. {
  563. if (FQueue != NULL)
  564. {
  565. FQueue->OnListUpdate = NULL;
  566. FQueue->OnQueueItemUpdate = NULL;
  567. }
  568. FQueue = value;
  569. if (FQueue != NULL)
  570. {
  571. DebugAssert(FQueue->OnListUpdate == NULL);
  572. FQueue->OnListUpdate = QueueListUpdate;
  573. DebugAssert(FQueue->OnQueueItemUpdate == NULL);
  574. FQueue->OnQueueItemUpdate = QueueItemUpdate;
  575. }
  576. QueueChanged();
  577. }
  578. }
  579. //---------------------------------------------------------------------------
  580. void __fastcall TCustomScpExplorerForm::QueueView3Deletion(TObject * /*Sender*/,
  581. TListItem * Item)
  582. {
  583. if (FQueueActedItem == Item)
  584. {
  585. FQueueActedItem = NULL;
  586. if ((QueueView3->PopupMenu != NULL) &&
  587. (QueueView3->PopupMenu->PopupComponent == QueueView3))
  588. {
  589. // rather "trick", suggested by Jordan on jrsoftware.toolbar2000
  590. ReleaseCapture();
  591. }
  592. }
  593. if (Item->Data == FPendingQueueActionItem)
  594. {
  595. FPendingQueueActionItem = NULL;
  596. }
  597. }
  598. //---------------------------------------------------------------------------
  599. void __fastcall TCustomScpExplorerForm::UpdateQueueStatus(bool QueueChanging)
  600. {
  601. {
  602. TGuard Guard(FQueueStatusSection);
  603. FQueueStatusInvalidated = false;
  604. if (FQueue != NULL)
  605. {
  606. FQueueStatus = FQueue->CreateStatus(FQueueStatus);
  607. }
  608. }
  609. if ((FQueueStatus != NULL) && (FQueueStatus->Count > FMaxQueueLength))
  610. {
  611. FMaxQueueLength = FQueueStatus->Count;
  612. Configuration->Usage->SetMax(L"MaxQueueLength", FMaxQueueLength);
  613. }
  614. FQueueController->UpdateQueueStatus(FQueueStatus);
  615. SetQueueProgress();
  616. UpdateQueueView();
  617. bool IsEmpty = (FQueueStatus == NULL) || (FQueueStatus->Count == 0);
  618. if (IsEmpty && (Terminal != NULL))
  619. {
  620. TOnceDoneOperation OnceDoneOperation =
  621. NonVisualDataModule->CurrentQueueOnceEmptyOperation();
  622. NonVisualDataModule->ResetQueueOnceEmptyOperation();
  623. if ((FQueue != NULL) && !WinConfiguration->EnableQueueByDefault && !QueueChanging)
  624. {
  625. FQueue->Enabled = false;
  626. }
  627. if ((OnceDoneOperation != odoIdle) && !NonVisualDataModule->Busy)
  628. {
  629. Terminal->CloseOnCompletion(OnceDoneOperation, LoadStr(CLOSED_ON_QUEUE_EMPTY));
  630. }
  631. }
  632. }
  633. //---------------------------------------------------------------------------
  634. UnicodeString __fastcall TCustomScpExplorerForm::GetQueueProgressTitle()
  635. {
  636. UnicodeString Result;
  637. if (FQueueStatus != NULL)
  638. {
  639. if (FQueueStatus->IsOnlyOneActiveAndNoPending())
  640. {
  641. TFileOperationProgressType * ProgressData =
  642. FQueueStatus->Items[FQueueStatus->DoneCount]->ProgressData;
  643. if ((ProgressData != NULL) && ProgressData->InProgress)
  644. {
  645. Result = TProgressForm::ProgressStr(NULL, ProgressData);
  646. }
  647. }
  648. else if (FQueueStatus->ActiveAndPendingPrimaryCount > 1)
  649. {
  650. Result = FMTLOAD(PROGRESS_IN_QUEUE, (FQueueStatus->ActiveAndPendingPrimaryCount));
  651. }
  652. }
  653. return Result;
  654. }
  655. //---------------------------------------------------------------------------
  656. void __fastcall TCustomScpExplorerForm::UpdateQueueView()
  657. {
  658. ComponentVisible[fcQueueView] =
  659. (WinConfiguration->QueueView.Show == qvShow) ||
  660. ((WinConfiguration->QueueView.Show == qvHideWhenEmpty) &&
  661. (FQueueStatus != NULL) && (FQueueStatus->Count > 0));
  662. }
  663. //---------------------------------------------------------------------------
  664. void __fastcall TCustomScpExplorerForm::QueueChanged()
  665. {
  666. if (FQueueStatus != NULL)
  667. {
  668. delete FQueueStatus;
  669. FQueueStatus = NULL;
  670. }
  671. UpdateQueueStatus(true);
  672. }
  673. //---------------------------------------------------------------------------
  674. void __fastcall TCustomScpExplorerForm::QueueListUpdate(TTerminalQueue * Queue)
  675. {
  676. if (FQueue == Queue)
  677. {
  678. FQueueStatusInvalidated = true;
  679. }
  680. }
  681. //---------------------------------------------------------------------------
  682. void __fastcall TCustomScpExplorerForm::QueueItemUpdate(TTerminalQueue * Queue,
  683. TQueueItem * Item)
  684. {
  685. if (FQueue == Queue)
  686. {
  687. TGuard Guard(FQueueStatusSection);
  688. // this may be running in parallel with QueueChanged
  689. if (FQueueStatus != NULL)
  690. {
  691. TQueueItemProxy * QueueItem = FQueueStatus->FindByQueueItem(Item);
  692. if ((Item->Status == TQueueItem::qsDone) && (Terminal != NULL))
  693. {
  694. FRefreshLocalDirectory = (QueueItem == NULL) ||
  695. (!QueueItem->Info->ModifiedLocal.IsEmpty());
  696. FRefreshRemoteDirectory = (QueueItem == NULL) ||
  697. (!QueueItem->Info->ModifiedRemote.IsEmpty());
  698. }
  699. if (QueueItem != NULL)
  700. {
  701. QueueItem->UserData = (void*)true;
  702. FQueueItemInvalidated = true;
  703. }
  704. }
  705. }
  706. }
  707. //---------------------------------------------------------------------------
  708. bool __fastcall TCustomScpExplorerForm::IsQueueAutoPopup()
  709. {
  710. // during standalone editing, we have no way to see/control queue,
  711. // so we have to always popup prompts automatically
  712. return FStandaloneEditing || GUIConfiguration->QueueAutoPopup;
  713. }
  714. //---------------------------------------------------------------------------
  715. void __fastcall TCustomScpExplorerForm::RefreshQueueItems()
  716. {
  717. if (FQueueStatus != NULL)
  718. {
  719. bool QueueAutoPopup = IsQueueAutoPopup();
  720. bool NeedRefresh = FQueueController->NeedRefresh();
  721. bool Refresh = FQueueItemInvalidated || NeedRefresh;
  722. FQueueItemInvalidated = false;
  723. int Limit = Refresh ? FQueueStatus->Count : FQueueStatus->DoneAndActiveCount;
  724. bool Updated = false;
  725. TQueueItemProxy * QueueItem;
  726. bool WasUserAction;
  727. for (int Index = 0; Index < Limit; Index++)
  728. {
  729. bool Update = false;
  730. QueueItem = FQueueStatus->Items[Index];
  731. WasUserAction = TQueueItem::IsUserActionStatus(QueueItem->Status);
  732. if (!NonVisualDataModule->Busy && QueueAutoPopup && WasUserAction &&
  733. (FPendingQueueActionItem == NULL))
  734. {
  735. FPendingQueueActionItem = QueueItem;
  736. FUserActionTimer->Enabled = true;
  737. }
  738. if ((bool)QueueItem->UserData)
  739. {
  740. QueueItem->UserData = (void*)false;
  741. QueueItem->Update();
  742. Updated = true;
  743. Update = true;
  744. }
  745. else if (FQueueController->QueueItemNeedsFrequentRefresh(QueueItem) || NeedRefresh)
  746. {
  747. Update = true;
  748. }
  749. if (Update)
  750. {
  751. FQueueController->RefreshQueueItem(QueueItem);
  752. }
  753. }
  754. if (Updated)
  755. {
  756. NonVisualDataModule->UpdateNonVisibleActions();
  757. SetQueueProgress();
  758. }
  759. }
  760. }
  761. //---------------------------------------------------------------------------
  762. void __fastcall TCustomScpExplorerForm::SetTaskbarListProgressState(TBPFLAG Flags)
  763. {
  764. // Could now use Application->MainFormHandle, now that we implement Application->OnGetMainFormHandle
  765. FTaskbarList->SetProgressState(GetMainForm()->Handle, Flags);
  766. }
  767. //---------------------------------------------------------------------------
  768. void __fastcall TCustomScpExplorerForm::SetTaskbarListProgressValue(int Progress)
  769. {
  770. if (Progress >= 0)
  771. {
  772. FTaskbarList->SetProgressValue(GetMainForm()->Handle, Progress, 100);
  773. }
  774. else
  775. {
  776. SetTaskbarListProgressState(TBPF_INDETERMINATE);
  777. }
  778. }
  779. //---------------------------------------------------------------------------
  780. void __fastcall TCustomScpExplorerForm::SetTaskbarListProgressValue(TFileOperationProgressType * ProgressData)
  781. {
  782. int OverallProgress;
  783. // FProgressForm is null when this is called from SetQueueProgress
  784. if ((FProgressForm != NULL) && (FProgressForm->SynchronizeProgress != NULL))
  785. {
  786. OverallProgress = FProgressForm->SynchronizeProgress->Progress(ProgressData);
  787. }
  788. else if (!TFileOperationProgressType::IsIndeterminateOperation(ProgressData->Operation))
  789. {
  790. OverallProgress = ProgressData->OverallProgress();
  791. }
  792. else
  793. {
  794. OverallProgress = -1;
  795. }
  796. SetTaskbarListProgressValue(OverallProgress);
  797. }
  798. //---------------------------------------------------------------------------
  799. void __fastcall TCustomScpExplorerForm::SetQueueProgress()
  800. {
  801. TTerminalManager::Instance()->QueueStatusUpdated();
  802. if ((FTaskbarList != NULL) && (FProgressForm == NULL))
  803. {
  804. if ((FQueueStatus != NULL) && (FQueueStatus->ActiveAndPendingPrimaryCount > 0))
  805. {
  806. if (FQueueStatus->IsOnlyOneActiveAndNoPending())
  807. {
  808. TFileOperationProgressType * ProgressData = NULL;
  809. if (FQueueStatus->Items[FQueueStatus->DoneCount] != NULL)
  810. {
  811. ProgressData = FQueueStatus->Items[FQueueStatus->DoneCount]->ProgressData;
  812. }
  813. if ((ProgressData != NULL) &&
  814. ProgressData->InProgress)
  815. {
  816. SetTaskbarListProgressValue(ProgressData);
  817. }
  818. else
  819. {
  820. SetTaskbarListProgressState(TBPF_NOPROGRESS);
  821. }
  822. }
  823. else
  824. {
  825. SetTaskbarListProgressState(TBPF_INDETERMINATE);
  826. }
  827. }
  828. else
  829. {
  830. SetTaskbarListProgressState(TBPF_NOPROGRESS);
  831. }
  832. }
  833. UpdateQueueLabel();
  834. }
  835. //---------------------------------------------------------------------------
  836. void __fastcall TCustomScpExplorerForm::UpdateQueueLabel()
  837. {
  838. UnicodeString Caption = LoadStr(QUEUE_CAPTION);
  839. if (FQueueStatus != NULL)
  840. {
  841. int ActiveAndPendingPrimaryCount = FQueueStatus->ActiveAndPendingPrimaryCount;
  842. if (ActiveAndPendingPrimaryCount > 0)
  843. {
  844. Caption = FORMAT("%s (%d)", (Caption, ActiveAndPendingPrimaryCount));
  845. }
  846. }
  847. QueueLabel->Caption = Caption;
  848. }
  849. //---------------------------------------------------------------------------
  850. void __fastcall TCustomScpExplorerForm::UpdateTransferList()
  851. {
  852. TTBXStringList * TransferList = dynamic_cast<TTBXStringList*>(
  853. static_cast<TComponent*>(GetComponent(fcTransferList)));
  854. TTBXDropDownItem * TransferDropDown = dynamic_cast<TTBXDropDownItem*>(
  855. static_cast<TComponent*>(GetComponent(fcTransferDropDown)));
  856. TransferList->Strings->BeginUpdate();
  857. try
  858. {
  859. TransferList->Strings->Assign(GUIConfiguration->CopyParamList->NameList);
  860. TransferList->Strings->Insert(0, StripHotkey(LoadStr(COPY_PARAM_DEFAULT)));
  861. TransferList->ItemIndex = GUIConfiguration->CopyParamIndex + 1;
  862. if (FTransferDropDownHint.IsEmpty())
  863. {
  864. FTransferDropDownHint = TransferDropDown->Hint;
  865. }
  866. // this way we get name for "default" settings (COPY_PARAM_DEFAULT)
  867. UnicodeString Name = TransferList->Strings->Strings[TransferList->ItemIndex];
  868. TransferDropDown->Text = StripHotkey(Name);
  869. TransferDropDown->Hint = FORMAT(L"%s|%s:\n%s",
  870. (FTransferDropDownHint, StripHotkey(Name),
  871. GUIConfiguration->CurrentCopyParam.GetInfoStr(L"; ",
  872. FLAGMASK(((Terminal != NULL) && Terminal->Active), Terminal->UsableCopyParamAttrs(0).General))));
  873. // update the label, otherwise when it is updated only on the first draw
  874. // of the list, it is drawn "bold" for some reason
  875. FTransferListHoverIndex = TransferList->ItemIndex;
  876. UpdateTransferLabel();
  877. }
  878. __finally
  879. {
  880. TransferList->Strings->EndUpdate();
  881. }
  882. }
  883. //---------------------------------------------------------------------------
  884. void __fastcall TCustomScpExplorerForm::UpdateCustomCommandsToolbar()
  885. {
  886. TTBXToolbar * Toolbar = dynamic_cast<TTBXToolbar *>(GetComponent(fcCustomCommandsBand));
  887. DebugAssert(Toolbar != NULL);
  888. NonVisualDataModule->UpdateCustomCommandsToolbar(Toolbar);
  889. }
  890. //---------------------------------------------------------------------------
  891. void __fastcall TCustomScpExplorerForm::UpdateActions()
  892. {
  893. TForm::UpdateActions();
  894. if (ComponentVisible[fcCustomCommandsBand])
  895. {
  896. UpdateCustomCommandsToolbar();
  897. }
  898. }
  899. //---------------------------------------------------------------------------
  900. void __fastcall TCustomScpExplorerForm::UpdateSessionsPageControlHeight()
  901. {
  902. SessionsPageControl->Height = SessionsPageControl->GetTabsHeight();
  903. }
  904. //---------------------------------------------------------------------------
  905. void __fastcall TCustomScpExplorerForm::ConfigurationChanged()
  906. {
  907. Color = GetBtnFaceColor();
  908. DebugAssert(Configuration && RemoteDirView);
  909. RemoteDirView->DimmHiddenFiles = WinConfiguration->DimmHiddenFiles;
  910. RemoteDirView->ShowHiddenFiles = WinConfiguration->ShowHiddenFiles;
  911. RemoteDirView->FormatSizeBytes = WinConfiguration->FormatSizeBytes;
  912. RemoteDirView->ShowInaccesibleDirectories = WinConfiguration->ShowInaccesibleDirectories;
  913. RemoteDirView->NaturalOrderNumericalSorting = WinConfiguration->NaturalOrderNumericalSorting;
  914. if (RemoteDirView->RowSelect != WinConfiguration->FullRowSelect)
  915. {
  916. RemoteDirView->RowSelect = WinConfiguration->FullRowSelect;
  917. // selection is not redrawn automatically when RowSelect changes
  918. RemoteDirView->Invalidate();
  919. }
  920. RemoteDriveView->DimmHiddenDirs = WinConfiguration->DimmHiddenFiles;
  921. RemoteDriveView->ShowHiddenDirs = WinConfiguration->ShowHiddenFiles;
  922. RemoteDriveView->ShowInaccesibleDirectories = WinConfiguration->ShowInaccesibleDirectories;
  923. RemoteDriveView->NaturalOrderNumericalSorting = WinConfiguration->NaturalOrderNumericalSorting;
  924. UpdateSessionsPageControlHeight();
  925. SetDockAllowDrag(!WinConfiguration->LockToolbars);
  926. UpdateToolbarDisplayMode();
  927. UpdateTransferList();
  928. if (Terminal != NULL)
  929. {
  930. TransferPresetAutoSelect();
  931. }
  932. UpdateQueueView();
  933. UpdateControls();
  934. // this can be called even before constuctor finishes.
  935. if (FEditorManager != NULL)
  936. {
  937. FEditorManager->ProcessFiles(FileConfigurationChanged, NULL);
  938. }
  939. // this can be called even before constuctor finishes.
  940. if (FLocalEditors != NULL)
  941. {
  942. for (int Index = 0; Index < FLocalEditors->Count; Index++)
  943. {
  944. ReconfigureEditorForm(static_cast<TForm *>(FLocalEditors->Items[Index]));
  945. }
  946. }
  947. if (ComponentVisible[fcCustomCommandsBand])
  948. {
  949. UpdateCustomCommandsToolbar();
  950. }
  951. // show only when keeping queue items forever,
  952. // otherwise, its enough to have in in the context menu
  953. QueueDeleteAllDoneQueueToolbarItem->Visible =
  954. WinConfiguration->QueueKeepDoneItems && (WinConfiguration->QueueKeepDoneItemsFor < 0);
  955. if (FFileColorsCurrent != WinConfiguration->FileColors)
  956. {
  957. TFileColorData::LoadList(WinConfiguration->FileColors, FFileColors);
  958. FFileColorsCurrent = WinConfiguration->FileColors;
  959. FileColorsChanged();
  960. }
  961. }
  962. //---------------------------------------------------------------------------
  963. void __fastcall TCustomScpExplorerForm::DoFileColorsChanged(TCustomDirView * DirView)
  964. {
  965. DirView->OnGetItemColor = FFileColors.empty() ? TDirViewGetItemColorEvent(NULL) : TDirViewGetItemColorEvent(&DirViewGetItemColor);
  966. DirView->Invalidate();
  967. }
  968. //---------------------------------------------------------------------------
  969. void __fastcall TCustomScpExplorerForm::FileColorsChanged()
  970. {
  971. DoFileColorsChanged(RemoteDirView);
  972. }
  973. //---------------------------------------------------------------------------
  974. void __fastcall TCustomScpExplorerForm::FileConfigurationChanged(
  975. const UnicodeString FileName, TEditedFileData * /*Data*/, TObject * Token,
  976. void * /*Arg*/)
  977. {
  978. if (Token != NULL)
  979. {
  980. TForm * Editor = dynamic_cast<TForm*>(Token);
  981. DebugAssert(Editor != NULL);
  982. ReconfigureEditorForm(Editor);
  983. }
  984. }
  985. //---------------------------------------------------------------------------
  986. void __fastcall TCustomScpExplorerForm::EnableDDTransferConfirmation(TObject * /*Sender*/)
  987. {
  988. WinConfiguration->DDTransferConfirmation = asOn;
  989. }
  990. //---------------------------------------------------------------------------
  991. void __fastcall TCustomScpExplorerForm::CopyParamDialogAfter(
  992. TTransferDirection /*Direction*/, bool /*Temp*/, const UnicodeString & /*TargetDirectory*/)
  993. {
  994. // noop
  995. }
  996. //---------------------------------------------------------------------------
  997. bool __fastcall TCustomScpExplorerForm::CopyParamDialog(
  998. TTransferDirection Direction, TTransferType Type, bool Temp,
  999. TStrings * FileList, UnicodeString & TargetDirectory, TGUICopyParamType & CopyParam,
  1000. bool Confirm, bool DragDrop, int Options)
  1001. {
  1002. bool Result = true;
  1003. DebugAssert(Terminal && Terminal->Active);
  1004. // these parameters are known in advance
  1005. int Params =
  1006. FLAGMASK(Type == ttMove, cpDelete);
  1007. bool ToTemp = (Temp && (Direction == tdToLocal));
  1008. if (Result && Confirm && WinConfiguration->ConfirmTransferring)
  1009. {
  1010. bool DisableNewerOnly =
  1011. (!Terminal->IsCapable[fcNewerOnlyUpload] && (Direction == tdToRemote)) ||
  1012. ToTemp;
  1013. Options |=
  1014. FLAGMASK(ToTemp, coTemp) |
  1015. FLAGMASK(!Terminal->IsCapable[fsBackgroundTransfers], coDisableQueue) |
  1016. coDoNotShowAgain;
  1017. TUsableCopyParamAttrs UsableCopyParamAttrs = Terminal->UsableCopyParamAttrs(Params);
  1018. int CopyParamAttrs = (Direction == tdToRemote ?
  1019. UsableCopyParamAttrs.Upload : UsableCopyParamAttrs.Download) |
  1020. FLAGMASK(DisableNewerOnly, cpaNoNewerOnly);
  1021. int OutputOptions =
  1022. FLAGMASK(DragDrop && (WinConfiguration->DDTransferConfirmation == asAuto),
  1023. cooDoNotShowAgain);
  1024. std::unique_ptr<TSessionData> SessionData(SessionDataForCode());
  1025. FlashOnBackground(); // Particularly when called from ClipboardFakeCreated
  1026. Result = DoCopyDialog(Direction == tdToRemote, Type == ttMove,
  1027. FileList, TargetDirectory, &CopyParam, Options, CopyParamAttrs, SessionData.get(), &OutputOptions, -1);
  1028. if (Result)
  1029. {
  1030. if (FLAGSET(OutputOptions, cooDoNotShowAgain))
  1031. {
  1032. if (DragDrop)
  1033. {
  1034. if (WinConfiguration->DDTransferConfirmation == asAuto)
  1035. {
  1036. PopupTrayBalloon(NULL, LoadStr(DD_TRANSFER_CONFIRM_OFF2), qtInformation,
  1037. NULL, 0, EnableDDTransferConfirmation, NULL);
  1038. }
  1039. WinConfiguration->DDTransferConfirmation = asOff;
  1040. }
  1041. else
  1042. {
  1043. WinConfiguration->ConfirmTransferring = false;
  1044. }
  1045. }
  1046. else
  1047. {
  1048. // User explicitly unchecked "do not show again",
  1049. // so show him the dialog the next time
  1050. if (DragDrop && (WinConfiguration->DDTransferConfirmation == asAuto))
  1051. {
  1052. WinConfiguration->DDTransferConfirmation = asOn;
  1053. }
  1054. }
  1055. CopyParamDialogAfter(Direction, Temp, TargetDirectory);
  1056. }
  1057. }
  1058. if (Result && CopyParam.Queue && !ToTemp && Terminal->IsCapable[fsBackgroundTransfers])
  1059. {
  1060. Configuration->Usage->Inc(L"TransfersOnBackground");
  1061. // these parameter are known only after transfer dialog
  1062. Params |=
  1063. (CopyParam.QueueNoConfirmation ? cpNoConfirmation : 0);
  1064. AddQueueItem(Queue, Direction, FileList, TargetDirectory, CopyParam, Params);
  1065. Result = false;
  1066. ClearTransferSourceSelection(Direction);
  1067. }
  1068. return Result;
  1069. }
  1070. //---------------------------------------------------------------------------
  1071. void __fastcall TCustomScpExplorerForm::ClearTransferSourceSelection(TTransferDirection Direction)
  1072. {
  1073. if (FOnFileOperationFinished != NULL)
  1074. {
  1075. FOnFileOperationFinished(UnicodeString(), true);
  1076. }
  1077. else
  1078. {
  1079. TOperationSide Side = ((Direction == tdToRemote) ? osLocal : osRemote);
  1080. if (HasDirView[Side])
  1081. {
  1082. DirView(Side)->SelectAll(smNone);
  1083. }
  1084. }
  1085. }
  1086. //---------------------------------------------------------------------------
  1087. void __fastcall TCustomScpExplorerForm::AddQueueItem(
  1088. TTerminalQueue * Queue, TTransferDirection Direction, TStrings * FileList,
  1089. const UnicodeString TargetDirectory, const TGUICopyParamType & CopyParam,
  1090. int Params)
  1091. {
  1092. DebugAssert(Queue != NULL);
  1093. bool SingleFile = false;
  1094. if (FileList->Count == 1)
  1095. {
  1096. if (Direction == tdToRemote)
  1097. {
  1098. UnicodeString FileName = FileList->Strings[0];
  1099. SingleFile = FileExists(ApiPath(FileName));
  1100. }
  1101. else
  1102. {
  1103. TRemoteFile * File = static_cast<TRemoteFile *>(FileList->Objects[0]);
  1104. SingleFile = !File->IsDirectory;
  1105. }
  1106. }
  1107. TQueueItem * QueueItem;
  1108. if (Direction == tdToRemote)
  1109. {
  1110. QueueItem = new TUploadQueueItem(Terminal, FileList, TargetDirectory,
  1111. &CopyParam, Params, SingleFile, CopyParam.QueueParallel);
  1112. }
  1113. else
  1114. {
  1115. QueueItem = new TDownloadQueueItem(Terminal, FileList, TargetDirectory,
  1116. &CopyParam, Params, SingleFile, CopyParam.QueueParallel);
  1117. }
  1118. AddQueueItem(Queue, QueueItem, Terminal);
  1119. }
  1120. //---------------------------------------------------------------------------
  1121. void __fastcall TCustomScpExplorerForm::AddQueueItem(TTerminalQueue * Queue, TQueueItem * QueueItem, TManagedTerminal * Terminal)
  1122. {
  1123. if (Queue->IsEmpty)
  1124. {
  1125. Terminal->QueueOperationStart = Now();
  1126. }
  1127. Queue->AddItem(QueueItem);
  1128. }
  1129. //---------------------------------------------------------------------------
  1130. void __fastcall TCustomScpExplorerForm::RestoreFormParams()
  1131. {
  1132. }
  1133. //---------------------------------------------------------------------------
  1134. void __fastcall TCustomScpExplorerForm::InitControls()
  1135. {
  1136. DebugAssert(FDocks != NULL);
  1137. for (int Index = 0; Index < ComponentCount; Index++)
  1138. {
  1139. TTBDock * Dock = dynamic_cast<TTBDock *>(Components[Index]);
  1140. if ((Dock != NULL) && (Dock->Tag == 0))
  1141. {
  1142. FDocks->Add(Dock);
  1143. }
  1144. }
  1145. CollectItemsWithTextDisplayMode(this);
  1146. }
  1147. //---------------------------------------------------------------------------
  1148. void __fastcall TCustomScpExplorerForm::RestoreParams()
  1149. {
  1150. ConfigurationChanged();
  1151. QueuePanel->Height =
  1152. LoadDimension(WinConfiguration->QueueView.Height, WinConfiguration->QueueView.HeightPixelsPerInch, this);
  1153. LoadListViewStr(QueueView3, WinConfiguration->QueueView.Layout);
  1154. QueueDock->Visible = WinConfiguration->QueueView.ToolBar;
  1155. QueueLabel->Visible = WinConfiguration->QueueView.Label;
  1156. }
  1157. //---------------------------------------------------------------------------
  1158. void __fastcall TCustomScpExplorerForm::StoreParams()
  1159. {
  1160. WinConfiguration->QueueView.Height = QueuePanel->Height;
  1161. WinConfiguration->QueueView.HeightPixelsPerInch = GetControlPixelsPerInch(this);
  1162. WinConfiguration->QueueView.Layout = GetListViewStr(QueueView3);
  1163. WinConfiguration->QueueView.ToolBar = QueueDock->Visible;
  1164. WinConfiguration->QueueView.Label = QueueLabel->Visible;
  1165. }
  1166. //---------------------------------------------------------------------------
  1167. void __fastcall TCustomScpExplorerForm::CreateParams(TCreateParams & Params)
  1168. {
  1169. if (!FFormRestored)
  1170. {
  1171. FFormRestored = true;
  1172. RestoreFormParams();
  1173. }
  1174. TForm::CreateParams(Params);
  1175. }
  1176. //---------------------------------------------------------------------------
  1177. void __fastcall TCustomScpExplorerForm::SetDockAllowDrag(bool value)
  1178. {
  1179. DebugAssert(FDocks != NULL);
  1180. for (int Index = 0; Index < FDocks->Count; Index++)
  1181. {
  1182. static_cast<TTBDock*>(FDocks->Items[Index])->AllowDrag = value;
  1183. }
  1184. }
  1185. //---------------------------------------------------------------------------
  1186. void __fastcall TCustomScpExplorerForm::LoadToolbarsLayoutStr(UnicodeString LayoutStr, UnicodeString ButtonsStr)
  1187. {
  1188. SetDockAllowDrag(true);
  1189. while (!ButtonsStr.IsEmpty())
  1190. {
  1191. UnicodeString S = CutToChar(ButtonsStr, L';', true);
  1192. UnicodeString Name = CutToChar(S, L'=', true);
  1193. for (int ToolbarIndex = 0; ToolbarIndex < ComponentCount; ToolbarIndex++)
  1194. {
  1195. TTBCustomToolbar * Toolbar = dynamic_cast<TTBCustomToolbar *>(Components[ToolbarIndex]);
  1196. if ((Toolbar != NULL) && SameText(Name, GetToolbarKey(Toolbar->Name)))
  1197. {
  1198. while (!S.IsEmpty())
  1199. {
  1200. UnicodeString S2 = CutToChar(S, L',', true);
  1201. UnicodeString Name = CutToChar(S2, L':', true);
  1202. for (int ItemIndex = 0; ItemIndex < Toolbar->Items->Count; ItemIndex++)
  1203. {
  1204. TTBCustomItem * Item = Toolbar->Items->Items[ItemIndex];
  1205. if (SameText(GetToolbarItemName(Item), Name))
  1206. {
  1207. // forward compatibility
  1208. UnicodeString S3 = CutToChar(S2, L':', true);
  1209. bool Visible = StrToIntDef(S3, 0);
  1210. Item->Visible = Visible;
  1211. }
  1212. }
  1213. }
  1214. }
  1215. }
  1216. }
  1217. ::LoadToolbarsLayoutStr(this, LayoutStr);
  1218. SetDockAllowDrag(!WinConfiguration->LockToolbars);
  1219. }
  1220. //---------------------------------------------------------------------------
  1221. UnicodeString __fastcall TCustomScpExplorerForm::GetToolbarsLayoutStr()
  1222. {
  1223. UnicodeString Result;
  1224. SetDockAllowDrag(true);
  1225. Result = ::GetToolbarsLayoutStr(this);
  1226. SetDockAllowDrag(!WinConfiguration->LockToolbars);
  1227. return Result;
  1228. }
  1229. //---------------------------------------------------------------------------
  1230. UnicodeString __fastcall TCustomScpExplorerForm::GetToolbarItemName(TTBCustomItem * Item)
  1231. {
  1232. UnicodeString Result;
  1233. if (Item->Action != NULL)
  1234. {
  1235. Result = Item->Action->Name;
  1236. Result = RemoveSuffix(Result, L"Action");
  1237. }
  1238. else
  1239. {
  1240. Result = Item->Name;
  1241. Result = RemoveSuffix(Result, L"SubmenuItem");
  1242. Result = RemoveSuffix(Result, L"Item");
  1243. }
  1244. return Result;
  1245. }
  1246. //---------------------------------------------------------------------------
  1247. UnicodeString __fastcall TCustomScpExplorerForm::GetToolbarsButtonsStr()
  1248. {
  1249. UnicodeString Result;
  1250. for (int ToolbarIndex = 0; ToolbarIndex < ComponentCount; ToolbarIndex++)
  1251. {
  1252. UnicodeString ToolbarStr;
  1253. TTBCustomToolbar * Toolbar = dynamic_cast<TTBCustomToolbar *>(Components[ToolbarIndex]);
  1254. if ((Toolbar != NULL) && (Toolbar != QueueToolbar))
  1255. {
  1256. for (int ItemIndex = 0; ItemIndex < Toolbar->Items->Count; ItemIndex++)
  1257. {
  1258. TTBCustomItem * Item = Toolbar->Items->Items[ItemIndex];
  1259. // Currently all buttons are visible by default, so we can safely remember all hidden buttons.
  1260. // Once we introduce any buttons that aer hidden by default, we would have to remember their initial state
  1261. // and save the changes here only.
  1262. if (!Item->Visible)
  1263. {
  1264. UnicodeString Name = GetToolbarItemName(Item);
  1265. DebugAssert(Name.Pos(L"TBX") == 0);
  1266. AddToList(ToolbarStr, FORMAT(L"%s:0", (Name)), L",");
  1267. }
  1268. }
  1269. }
  1270. if (!ToolbarStr.IsEmpty())
  1271. {
  1272. AddToList(Result, FORMAT(L"%s=%s", (GetToolbarKey(Toolbar->Name), ToolbarStr)), L";");
  1273. }
  1274. }
  1275. return Result;
  1276. }
  1277. //---------------------------------------------------------------------------
  1278. void __fastcall TCustomScpExplorerForm::CreateProgressForm(TSynchronizeProgress * SynchronizeProgress)
  1279. {
  1280. DebugAssert(FProgressForm == NULL);
  1281. bool AllowSkip = (Terminal != NULL) ? Terminal->IsCapable[fsSkipTransfer] : false;
  1282. FProgressForm = new TProgressForm(Application, (FTransferResumeList != NULL), AllowSkip, SynchronizeProgress);
  1283. FProgressForm->DeleteLocalToRecycleBin =
  1284. (WinConfiguration->DeleteToRecycleBin != FAlternativeDelete);
  1285. FProgressForm->DeleteRemoteToRecycleBin =
  1286. (Terminal != NULL) &&
  1287. (Terminal->SessionData->DeleteToRecycleBin != FAlternativeDelete) &&
  1288. !Terminal->SessionData->RecycleBinPath.IsEmpty();
  1289. // As progress window has delayed show now, we need to lock ourselves, (at least) until then
  1290. LockWindow();
  1291. if (FTaskbarList != NULL)
  1292. {
  1293. // Actually, do not know what hides the progress once the operation finishes
  1294. SetTaskbarListProgressState(TBPF_NORMAL);
  1295. }
  1296. }
  1297. //---------------------------------------------------------------------------
  1298. void __fastcall TCustomScpExplorerForm::DestroyProgressForm()
  1299. {
  1300. UnlockWindow();
  1301. SAFE_DESTROY(FProgressForm);
  1302. SetQueueProgress();
  1303. }
  1304. //---------------------------------------------------------------------------
  1305. void __fastcall TCustomScpExplorerForm::FileOperationProgress(
  1306. TFileOperationProgressType & ProgressData)
  1307. {
  1308. // Download to temporary local directory
  1309. if (FPendingTempSpaceWarn && ProgressData.InProgress && ProgressData.TotalSizeSet)
  1310. {
  1311. bool Continue = true;
  1312. FPendingTempSpaceWarn = false;
  1313. DoWarnLackOfTempSpace(ProgressData.Directory, ProgressData.TotalSize, Continue);
  1314. if (!Continue)
  1315. {
  1316. Abort();
  1317. }
  1318. }
  1319. // operation is being executed and we still didn't show up progress form
  1320. if (ProgressData.InProgress && !FProgressForm)
  1321. {
  1322. CreateProgressForm(NULL);
  1323. DebugAssert(FProgressForm->OnceDoneOperation == odoIdle);
  1324. FProgressForm->OnceDoneOperation = ProgressData.InitialOnceDoneOperation;
  1325. }
  1326. // operation is finished (or terminated), so we hide progress form
  1327. else if (!ProgressData.InProgress && (FProgressForm != NULL) && (FProgressForm->SynchronizeProgress == NULL))
  1328. {
  1329. DestroyProgressForm();
  1330. if (ProgressData.Operation == foCalculateSize)
  1331. {
  1332. // When calculating size before transfer, the abort caused by
  1333. // cancel flag set due to "MoveToQueue" below,
  1334. // is not propagated back to us in ExecuteFileOperation,
  1335. // as it is silently swallowed.
  1336. // So we explicitly (re)throw it here.
  1337. if (FMoveToQueue)
  1338. {
  1339. Abort();
  1340. }
  1341. }
  1342. else
  1343. {
  1344. if ((ProgressData.Cancel == csContinue) &&
  1345. !FAutoOperation)
  1346. {
  1347. OperationComplete(ProgressData.StartTime);
  1348. }
  1349. }
  1350. }
  1351. if (FProgressForm &&
  1352. // Can happen only during synchronization, when ending one of the operations
  1353. ProgressData.InProgress)
  1354. {
  1355. FProgressForm->SetProgressData(ProgressData);
  1356. if (FTaskbarList != NULL)
  1357. {
  1358. DebugAssert(ProgressData.InProgress);
  1359. SetTaskbarListProgressValue(&ProgressData);
  1360. }
  1361. if (FProgressForm->Cancel > csContinue)
  1362. {
  1363. ProgressData.SetCancelAtLeast(FProgressForm->Cancel);
  1364. if (FProgressForm->Cancel == csCancelFile)
  1365. {
  1366. FProgressForm->ClearCancel();
  1367. }
  1368. // cancel cancels even the move
  1369. FMoveToQueue = false;
  1370. }
  1371. else if (FProgressForm->MoveToQueue)
  1372. {
  1373. FMoveToQueue = true;
  1374. ProgressData.SetCancelAtLeast(csCancel);
  1375. }
  1376. if ((FTransferResumeList != NULL) &&
  1377. ProgressData.InProgress &&
  1378. ((ProgressData.Operation == foCopy) || (ProgressData.Operation == foMove)) &&
  1379. !ProgressData.FullFileName.IsEmpty())
  1380. {
  1381. if ((FTransferResumeList->Count == 0) ||
  1382. (FTransferResumeList->Strings[FTransferResumeList->Count - 1] != ProgressData.FullFileName))
  1383. {
  1384. // note that we do not recognize directories from files here
  1385. FTransferResumeList->Add(ProgressData.FullFileName);
  1386. }
  1387. }
  1388. }
  1389. }
  1390. //---------------------------------------------------------------------------
  1391. void __fastcall TCustomScpExplorerForm::OperationComplete(
  1392. const TDateTime & StartTime)
  1393. {
  1394. if (GUIConfiguration->BeepOnFinish &&
  1395. (Now() - StartTime > GUIConfiguration->BeepOnFinishAfter))
  1396. {
  1397. UnicodeString BeepSound = GUIConfiguration->BeepSound;
  1398. DWORD Sound;
  1399. if (!ExtractFileExt(BeepSound).IsEmpty())
  1400. {
  1401. Sound = SND_FILENAME;
  1402. }
  1403. else
  1404. {
  1405. Sound = SND_ALIAS;
  1406. }
  1407. PlaySound(BeepSound.c_str(), NULL, Sound | SND_ASYNC);
  1408. }
  1409. }
  1410. //---------------------------------------------------------------------------
  1411. void __fastcall TCustomScpExplorerForm::OperationProgress(
  1412. TFileOperationProgressType & ProgressData)
  1413. {
  1414. // For example, when calling TFileOperationProgressType::Suspend in TTerminal::QueryReopen,
  1415. // we cannot recurse back to TTerminal::Idle as that may detect another error, calling "suspend" again.
  1416. TAutoNestingCounter Counter(FDoNotIdleCurrentTerminal);
  1417. FileOperationProgress(ProgressData);
  1418. }
  1419. //---------------------------------------------------------------------------
  1420. UnicodeString __fastcall TCustomScpExplorerForm::GetProgressTitle()
  1421. {
  1422. UnicodeString Result;
  1423. if (FProgressForm != NULL)
  1424. {
  1425. Result = FProgressForm->ProgressStr();
  1426. }
  1427. return Result;
  1428. }
  1429. //---------------------------------------------------------------------------
  1430. bool __fastcall TCustomScpExplorerForm::PanelOperation(TOperationSide /*Side*/,
  1431. bool DragDrop)
  1432. {
  1433. return (!DragDrop && (DropSourceControl == NULL)) ||
  1434. (DropSourceControl == DirView(osOther));
  1435. }
  1436. //---------------------------------------------------------------------------
  1437. void __fastcall TCustomScpExplorerForm::DoOperationFinished(
  1438. TFileOperation Operation, TOperationSide Side,
  1439. bool /*Temp*/, const UnicodeString & FileName, bool Success,
  1440. TOnceDoneOperation & OnceDoneOperation)
  1441. {
  1442. if (!FAutoOperation)
  1443. {
  1444. // no selection on "/upload", form servers only as event handler
  1445. // (it is not displayed)
  1446. if (PanelOperation(Side, FDragDropOperation) &&
  1447. Visible && (Operation != foCalculateSize) &&
  1448. (Operation != foGetProperties) && (Operation != foCalculateChecksum))
  1449. {
  1450. if (FOnFileOperationFinished != NULL)
  1451. {
  1452. FOnFileOperationFinished(FileName, Success);
  1453. }
  1454. else
  1455. {
  1456. TCustomDirView * DView = DirView(Side);
  1457. UnicodeString FileNameOnly = ExtractFileName(FileName, !IsSideLocalBrowser(Side));
  1458. TListItem *Item = DView->FindFileItem(FileNameOnly);
  1459. // this can happen when local drive is unplugged in the middle of the operation
  1460. if (Item != NULL)
  1461. {
  1462. if (Success) Item->Selected = false;
  1463. DView->MakeProgressVisible(Item);
  1464. }
  1465. }
  1466. }
  1467. if ((Operation == foCopy) || (Operation == foMove))
  1468. {
  1469. DebugAssert(!IsLocalBrowserMode()); // TODO
  1470. if (Side == osLocal)
  1471. {
  1472. Configuration->Usage->Inc(L"UploadedFiles");
  1473. }
  1474. else
  1475. {
  1476. Configuration->Usage->Inc(L"DownloadedFiles");
  1477. }
  1478. }
  1479. }
  1480. if (Success && (FSynchronizeController != NULL))
  1481. {
  1482. DebugAssert(!IsSideLocalBrowser(Side));
  1483. if (Operation == foCopy)
  1484. {
  1485. DebugAssert(Side == osLocal);
  1486. FSynchronizeController->LogOperation(soUpload, FileName);
  1487. }
  1488. else if (Operation == foDelete)
  1489. {
  1490. DebugAssert(Side == osRemote);
  1491. FSynchronizeController->LogOperation(soDelete, FileName);
  1492. }
  1493. }
  1494. if (FProgressForm)
  1495. {
  1496. OnceDoneOperation = FProgressForm->OnceDoneOperation;
  1497. }
  1498. }
  1499. //---------------------------------------------------------------------------
  1500. void __fastcall TCustomScpExplorerForm::OperationFinished(
  1501. TFileOperation Operation, TOperationSide Side,
  1502. bool Temp, const UnicodeString & FileName, Boolean Success,
  1503. TOnceDoneOperation & OnceDoneOperation)
  1504. {
  1505. TAutoNestingCounter Counter(FDoNotIdleCurrentTerminal);
  1506. DoOperationFinished(Operation, Side, Temp, FileName, Success,
  1507. OnceDoneOperation);
  1508. }
  1509. //---------------------------------------------------------------------------
  1510. bool TCustomScpExplorerForm::IsLocalBrowserMode()
  1511. {
  1512. return false;
  1513. }
  1514. //---------------------------------------------------------------------------
  1515. bool TCustomScpExplorerForm::IsSideLocalBrowser(TOperationSide)
  1516. {
  1517. return false;
  1518. }
  1519. //---------------------------------------------------------------------------
  1520. TCustomDirView * TCustomScpExplorerForm::GetCurrentLocalBrowser()
  1521. {
  1522. return NULL;
  1523. }
  1524. //---------------------------------------------------------------------------
  1525. TCustomDirView * __fastcall TCustomScpExplorerForm::DirView(TOperationSide Side)
  1526. {
  1527. DebugAssert(GetSide(Side) == osRemote);
  1528. DebugUsedParam(Side);
  1529. return RemoteDirView;
  1530. }
  1531. //---------------------------------------------------------------------------
  1532. TCustomDriveView * __fastcall TCustomScpExplorerForm::DriveView(TOperationSide Side)
  1533. {
  1534. DebugAssert(GetSide(Side) == osRemote);
  1535. DebugUsedParam(Side);
  1536. return RemoteDriveView;
  1537. }
  1538. //---------------------------------------------------------------------------
  1539. bool __fastcall TCustomScpExplorerForm::DirViewEnabled(TOperationSide Side)
  1540. {
  1541. DebugAssert(GetSide(Side) == osRemote);
  1542. DebugUsedParam(Side);
  1543. return (Terminal != NULL) && Terminal->Active;
  1544. }
  1545. //---------------------------------------------------------------------------
  1546. bool __fastcall TCustomScpExplorerForm::GetEnableFocusedOperation(
  1547. TOperationSide Side, int FilesOnly)
  1548. {
  1549. return DirView(Side)->AnyFileSelected(true, (FilesOnly != 0), true);
  1550. }
  1551. //---------------------------------------------------------------------------
  1552. bool __fastcall TCustomScpExplorerForm::GetEnableSelectedOperation(
  1553. TOperationSide Side, int FilesOnly)
  1554. {
  1555. return DirView(Side)->AnyFileSelected(false, (FilesOnly != 0), true);
  1556. }
  1557. //---------------------------------------------------------------------------
  1558. struct THistoryItemData
  1559. {
  1560. short int Side;
  1561. short int Index;
  1562. };
  1563. //---------------------------------------------------------------------------
  1564. void __fastcall TCustomScpExplorerForm::HistoryGo(TOperationSide Side, int Index)
  1565. {
  1566. DirView(Side)->HistoryGo(Index);
  1567. }
  1568. //---------------------------------------------------------------------------
  1569. void __fastcall TCustomScpExplorerForm::HistoryItemClick(System::TObject* Sender)
  1570. {
  1571. TTBCustomItem * Item = dynamic_cast<TTBCustomItem *>(Sender);
  1572. THistoryItemData Data = *reinterpret_cast<THistoryItemData*>(&(Item->Tag));
  1573. HistoryGo((TOperationSide)Data.Side, Data.Index);
  1574. }
  1575. //---------------------------------------------------------------------------
  1576. void __fastcall TCustomScpExplorerForm::UpdateHistoryMenu(TOperationSide Side,
  1577. bool Back)
  1578. {
  1579. if (FHistoryMenu[Side == osLocal][Back] != NULL)
  1580. {
  1581. TCustomDirView * DView = DirView(Side);
  1582. TTBXPopupMenu * Menu = FHistoryMenu[Side == osLocal][Back];
  1583. int ICount = Back ? DView->BackCount : DView->ForwardCount;
  1584. if (ICount > 10)
  1585. {
  1586. ICount = 10;
  1587. }
  1588. Menu->Items->Clear();
  1589. THistoryItemData Data;
  1590. Data.Side = Side;
  1591. for (short int i = 1; i <= ICount; i++)
  1592. {
  1593. TTBCustomItem * Item = new TTBXItem(Menu);
  1594. Data.Index = static_cast<short int>(i * (Back ? -1 : 1));
  1595. Item->Caption = MinimizeName(DView->HistoryPath[Data.Index], 50, !IsSideLocalBrowser(Side));
  1596. Item->Hint = DView->HistoryPath[Data.Index];
  1597. DebugAssert(sizeof(int) == sizeof(THistoryItemData));
  1598. Item->Tag = *reinterpret_cast<int*>(&Data);
  1599. Item->OnClick = HistoryItemClick;
  1600. Menu->Items->Add(Item);
  1601. }
  1602. }
  1603. }
  1604. //---------------------------------------------------------------------------
  1605. TTBXPopupMenu * __fastcall TCustomScpExplorerForm::HistoryMenu(
  1606. TOperationSide Side, bool Back)
  1607. {
  1608. if (FHistoryMenu[Side == osLocal][Back] == NULL)
  1609. {
  1610. // workaround
  1611. // In Pascal the size of TTBXPopupMenu is 132, in C++ 136,
  1612. // operator new allocates memory in Pascal code, but calls inline
  1613. // contructor in C++, leading in problems, the function does
  1614. // both in Pascal code
  1615. FHistoryMenu[Side == osLocal][Back] = CreateTBXPopupMenu(this);
  1616. UpdateHistoryMenu(Side, Back);
  1617. }
  1618. return FHistoryMenu[Side == osLocal][Back];
  1619. }
  1620. //---------------------------------------------------------------------------
  1621. void __fastcall TCustomScpExplorerForm::DirViewHistoryChange(
  1622. TCustomDirView *Sender)
  1623. {
  1624. TOperationSide Side = (Sender == DirView(osOther) ? osOther : osLocal);
  1625. UpdateHistoryMenu(Side, true);
  1626. UpdateHistoryMenu(Side, false);
  1627. }
  1628. //---------------------------------------------------------------------------
  1629. bool __fastcall TCustomScpExplorerForm::CustomCommandRemoteAllowed()
  1630. {
  1631. // remote custom commands can be executed only if the server supports shell commands
  1632. // or have secondary shell
  1633. return (FTerminal != NULL) && FTerminal->Active && (FTerminal->IsCapable[fcSecondaryShell] || FTerminal->IsCapable[fcShellAnyCommand]);
  1634. }
  1635. //---------------------------------------------------------------------------
  1636. int __fastcall TCustomScpExplorerForm::CustomCommandState(
  1637. const TCustomCommandType & Command, bool OnFocused, TCustomCommandListType ListType)
  1638. {
  1639. int Result;
  1640. TFileCustomCommand RemoteCustomCommand;
  1641. TLocalCustomCommand LocalCustomCommand;
  1642. TFileCustomCommand * NonInteractiveCustomCommand =
  1643. FLAGCLEAR(Command.Params, ccLocal) ? &RemoteCustomCommand : &LocalCustomCommand;
  1644. TInteractiveCustomCommand InteractiveCustomCommand(NonInteractiveCustomCommand);
  1645. UnicodeString Cmd = InteractiveCustomCommand.Complete(Command.Command, false);
  1646. if (FLAGCLEAR(Command.Params, ccLocal))
  1647. {
  1648. int AllowedState = CustomCommandRemoteAllowed() ? 1 : 0;
  1649. // custom command that does not operate with files can be executed anytime ...
  1650. if (!NonInteractiveCustomCommand->IsFileCommand(Cmd))
  1651. {
  1652. // ... but do not show such command in remote file menu
  1653. if ((ListType == ccltAll) || (ListType == ccltNonFile))
  1654. {
  1655. Result = AllowedState;
  1656. }
  1657. else
  1658. {
  1659. Result = -1;
  1660. }
  1661. }
  1662. else
  1663. {
  1664. if ((ListType == ccltAll) || ((ListType == ccltFile) && !IsSideLocalBrowser(FCurrentSide)))
  1665. {
  1666. Result = (DirView(FCurrentSide)->AnyFileSelected(OnFocused, false, true)) ? AllowedState : 0;
  1667. }
  1668. else
  1669. {
  1670. Result = -1;
  1671. }
  1672. }
  1673. }
  1674. else
  1675. {
  1676. // custom command that does not operate with files can be executed anytime
  1677. if (!NonInteractiveCustomCommand->IsFileCommand(Cmd))
  1678. {
  1679. Result = ((ListType == ccltAll) || (ListType == ccltNonFile)) ? 1 : -1;
  1680. }
  1681. else if (LocalCustomCommand.HasLocalFileName(Cmd))
  1682. {
  1683. if ((ListType == ccltAll) || (ListType == ccltFile))
  1684. {
  1685. // special case is "diff"-style command that can be executed over any side,
  1686. // if we have both sides
  1687. Result =
  1688. // Cannot have focus on both panels, so we have to call AnyFileSelected
  1689. // directly (instead of EnableSelectedOperation) to pass
  1690. // false to FocusedFileOnlyWhenFocused when panel is inactive.
  1691. ((HasDirView[osLocal] && !IsSideLocalBrowser(osRemote) &&
  1692. DirView(osLocal)->AnyFileSelected(false, false, (FCurrentSide == osLocal))) &&
  1693. DirView(osRemote)->AnyFileSelected(false, false, (FCurrentSide == osRemote))) ? 1 : 0;
  1694. }
  1695. else if (ListType == ccltBoth)
  1696. {
  1697. DebugAssert(!IsLocalBrowserMode());
  1698. Result = 1;
  1699. }
  1700. else
  1701. {
  1702. Result = -1;
  1703. }
  1704. }
  1705. else
  1706. {
  1707. if ((ListType == ccltAll) ||
  1708. ((ListType == ccltFile) && (!IsSideLocalBrowser(FCurrentSide) || FLAGCLEAR(Command.Params, ccRemoteFiles))))
  1709. {
  1710. Result = DirView(FCurrentSide)->AnyFileSelected(OnFocused, false, true) ? 1 : 0;
  1711. }
  1712. else
  1713. {
  1714. Result = -1;
  1715. }
  1716. }
  1717. }
  1718. return Result;
  1719. }
  1720. //---------------------------------------------------------------------------
  1721. void __fastcall TCustomScpExplorerForm::RemoteCustomCommand(
  1722. TStrings * FileList, const TCustomCommandType & ACommand,
  1723. const TCustomCommandData & Data, const UnicodeString & CommandCommand)
  1724. {
  1725. if (EnsureCommandSessionFallback(fcShellAnyCommand))
  1726. {
  1727. TRemoteCustomCommand RemoteCustomCommand(Data, Terminal->CurrentDirectory);
  1728. TWinInteractiveCustomCommand InteractiveCustomCommand(
  1729. &RemoteCustomCommand, ACommand.Name, ACommand.HomePage);
  1730. UnicodeString Command = InteractiveCustomCommand.Complete(CommandCommand, false);
  1731. Configuration->Usage->Inc(L"RemoteCustomCommandRuns2");
  1732. bool Capture =
  1733. FLAGSET(ACommand.Params, ccShowResults) ||
  1734. FLAGSET(ACommand.Params, ccCopyResults) ||
  1735. FLAGSET(ACommand.Params, ccShowResultsInMsgBox);
  1736. TCaptureOutputEvent OutputEvent = NULL;
  1737. DebugAssert(FCapturedLog == NULL);
  1738. if (Capture)
  1739. {
  1740. FCapturedLog = new TStringList();
  1741. OutputEvent = TerminalCaptureLog;
  1742. }
  1743. try
  1744. {
  1745. if (!RemoteCustomCommand.IsFileCommand(Command))
  1746. {
  1747. Terminal->AnyCommand(RemoteCustomCommand.Complete(Command, true),
  1748. OutputEvent);
  1749. }
  1750. else
  1751. {
  1752. Terminal->CustomCommandOnFiles(Command, ACommand.Params, FileList, OutputEvent);
  1753. }
  1754. if ((FCapturedLog != NULL) && (FCapturedLog->Count > 0))
  1755. {
  1756. if (FLAGSET(ACommand.Params, ccCopyResults))
  1757. {
  1758. CopyToClipboard(FCapturedLog);
  1759. }
  1760. if (FLAGSET(ACommand.Params, ccShowResults))
  1761. {
  1762. DoConsoleDialog(Terminal, L"", FCapturedLog);
  1763. }
  1764. else if (FLAGSET(ACommand.Params, ccShowResultsInMsgBox))
  1765. {
  1766. MessageDialog(FCapturedLog->Text, qtInformation, qaOK);
  1767. }
  1768. }
  1769. }
  1770. __finally
  1771. {
  1772. SAFE_DESTROY(FCapturedLog);
  1773. }
  1774. }
  1775. }
  1776. //---------------------------------------------------------------------------
  1777. void __fastcall TCustomScpExplorerForm::LocalCustomCommandPure(
  1778. TStrings * FileList, const TCustomCommandType & ACommand, const UnicodeString & Command, TStrings * ALocalFileList,
  1779. const TCustomCommandData & Data, bool LocalFileCommand, bool FileListCommand, UnicodeString * POutput)
  1780. {
  1781. DebugAssert(!IsLocalBrowserMode());
  1782. TStrings * LocalFileList = NULL;
  1783. TStrings * RemoteFileList = NULL;
  1784. TStrings * RemoteFileListFull = NULL;
  1785. try
  1786. {
  1787. if (LocalFileCommand)
  1788. {
  1789. if (ALocalFileList == NULL)
  1790. {
  1791. DebugAssert(HasDirView[osLocal]);
  1792. // Cannot have focus on both panels, so we have to call AnyFileSelected
  1793. // directly (instead of EnableSelectedOperation) to pass
  1794. // false to FocusedFileOnlyWhenFocused
  1795. DebugAssert(DirView(osLocal)->AnyFileSelected(false, false, false));
  1796. LocalFileList = DirView(osLocal)->CreateFileList(false, true, NULL);
  1797. }
  1798. else
  1799. {
  1800. LocalFileList = ALocalFileList;
  1801. }
  1802. if (FileListCommand)
  1803. {
  1804. if (LocalFileList->Count != 1)
  1805. {
  1806. throw Exception(LoadStr(CUSTOM_COMMAND_SELECTED_UNMATCH1));
  1807. }
  1808. }
  1809. else
  1810. {
  1811. if ((LocalFileList->Count != 1) &&
  1812. (FileList->Count != 1) &&
  1813. (LocalFileList->Count != FileList->Count))
  1814. {
  1815. throw Exception(LoadStr(CUSTOM_COMMAND_SELECTED_UNMATCH));
  1816. }
  1817. }
  1818. }
  1819. UnicodeString RootTempDir;
  1820. UnicodeString TempDir;
  1821. bool RemoteFiles = FLAGSET(ACommand.Params, ccRemoteFiles);
  1822. if (!RemoteFiles)
  1823. {
  1824. TemporarilyDownloadFiles(FileList, false, RootTempDir, TempDir, false, false, true);
  1825. }
  1826. try
  1827. {
  1828. TDateTimes RemoteFileTimes;
  1829. if (RemoteFiles)
  1830. {
  1831. RemoteFileList = FileList;
  1832. }
  1833. else
  1834. {
  1835. RemoteFileList = new TStringList();
  1836. TMakeLocalFileListParams MakeFileListParam;
  1837. MakeFileListParam.FileList = RemoteFileList;
  1838. MakeFileListParam.FileTimes = &RemoteFileTimes;
  1839. MakeFileListParam.IncludeDirs = FLAGSET(ACommand.Params, ccApplyToDirectories);
  1840. MakeFileListParam.Recursive =
  1841. FLAGSET(ACommand.Params, ccRecursive) && !FileListCommand;
  1842. ProcessLocalDirectory(TempDir, Terminal->MakeLocalFileList, &MakeFileListParam);
  1843. if (!MakeFileListParam.Recursive)
  1844. {
  1845. RemoteFileTimes.clear();
  1846. RemoteFileListFull = new TStringList();
  1847. MakeFileListParam.FileList = RemoteFileListFull;
  1848. MakeFileListParam.Recursive = true;
  1849. MakeFileListParam.IncludeDirs = false;
  1850. ProcessLocalDirectory(TempDir, Terminal->MakeLocalFileList, &MakeFileListParam);
  1851. }
  1852. else
  1853. {
  1854. RemoteFileListFull = RemoteFileList;
  1855. }
  1856. }
  1857. bool NonBlocking = FileListCommand && RemoteFiles && !POutput;
  1858. TFileOperationProgressType Progress(&OperationProgress, &OperationFinished);
  1859. if (!NonBlocking)
  1860. {
  1861. int Count;
  1862. if (FileListCommand)
  1863. {
  1864. Count = 1;
  1865. }
  1866. else if (LocalFileCommand)
  1867. {
  1868. Count = std::max(LocalFileList->Count, RemoteFileList->Count);
  1869. }
  1870. else
  1871. {
  1872. Count = RemoteFileList->Count;
  1873. }
  1874. Progress.Start(foCustomCommand, osRemote, Count);
  1875. DebugAssert(FProgressForm != NULL);
  1876. FProgressForm->ReadOnly = true;
  1877. }
  1878. try
  1879. {
  1880. if (FileListCommand)
  1881. {
  1882. UnicodeString LocalFile;
  1883. // MakeFileList does not delimit filenames
  1884. UnicodeString FileList = MakeFileList(RemoteFileList);
  1885. if (LocalFileCommand)
  1886. {
  1887. DebugAssert(LocalFileList->Count == 1);
  1888. LocalFile = LocalFileList->Strings[0];
  1889. }
  1890. TLocalCustomCommand CustomCommand(Data,
  1891. Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(), L"", LocalFile, FileList);
  1892. UnicodeString ShellCommand = CustomCommand.Complete(Command, true);
  1893. if (NonBlocking)
  1894. {
  1895. DebugAssert(!POutput);
  1896. ExecuteShellChecked(ShellCommand);
  1897. }
  1898. else
  1899. {
  1900. ExecuteProcessCheckedAndWait(ShellCommand, HelpKeyword, POutput);
  1901. }
  1902. }
  1903. else if (LocalFileCommand)
  1904. {
  1905. if (LocalFileList->Count == 1)
  1906. {
  1907. UnicodeString LocalFile = LocalFileList->Strings[0];
  1908. for (int Index = 0; Index < RemoteFileList->Count; Index++)
  1909. {
  1910. UnicodeString FileName = RemoteFileList->Strings[Index];
  1911. UnicodeString FileNameForProgress = RemoteFiles ? FileName : ExtractFileName(FileName);
  1912. Progress.SetFile(FileNameForProgress);
  1913. TLocalCustomCommand CustomCommand(Data,
  1914. Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(), FileName, LocalFile, L"");
  1915. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  1916. TOnceDoneOperation OnceDoneOperation;
  1917. // Do not unselect anything, as it won't work with two panels anyway
  1918. Progress.Finish(UnicodeString(), true, OnceDoneOperation);
  1919. }
  1920. }
  1921. else if (RemoteFileList->Count == 1)
  1922. {
  1923. UnicodeString FileName = RemoteFileList->Strings[0];
  1924. for (int Index = 0; Index < LocalFileList->Count; Index++)
  1925. {
  1926. UnicodeString LocalFileName = LocalFileList->Strings[Index];
  1927. UnicodeString LocalFileNameOnly = ExtractFileName(LocalFileName);
  1928. Progress.SetFile(LocalFileNameOnly);
  1929. TLocalCustomCommand CustomCommand(
  1930. Data, Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(),
  1931. FileName, LocalFileName, L"");
  1932. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  1933. TOnceDoneOperation OnceDoneOperation;
  1934. // Do not unselect anything, as it won't work with two panels anyway
  1935. Progress.Finish(UnicodeString(), true, OnceDoneOperation);
  1936. }
  1937. }
  1938. else
  1939. {
  1940. if (LocalFileList->Count != RemoteFileList->Count)
  1941. {
  1942. throw Exception(LoadStr(CUSTOM_COMMAND_PAIRS_DOWNLOAD_FAILED));
  1943. }
  1944. for (int Index = 0; Index < LocalFileList->Count; Index++)
  1945. {
  1946. UnicodeString FileName = RemoteFileList->Strings[Index];
  1947. UnicodeString LocalFileName = LocalFileList->Strings[Index];
  1948. UnicodeString LocalFileNameOnly = ExtractFileName(LocalFileName);
  1949. Progress.SetFile(LocalFileNameOnly); // sic
  1950. TLocalCustomCommand CustomCommand(
  1951. Data, Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(),
  1952. FileName, LocalFileName, L"");
  1953. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  1954. TOnceDoneOperation OnceDoneOperation;
  1955. // Do not unselect anything, as it won't work with two panels anyway
  1956. Progress.Finish(UnicodeString(), true, OnceDoneOperation);
  1957. }
  1958. }
  1959. }
  1960. else
  1961. {
  1962. for (int Index = 0; Index < RemoteFileList->Count; Index++)
  1963. {
  1964. UnicodeString FileName = RemoteFileList->Strings[Index];
  1965. UnicodeString FileNameForProgress = RemoteFiles ? FileName : ExtractFileName(FileName);
  1966. Progress.SetFile(FileNameForProgress);
  1967. TLocalCustomCommand CustomCommand(Data,
  1968. Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(),
  1969. FileName, L"", L"");
  1970. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  1971. TOnceDoneOperation OnceDoneOperation;
  1972. Progress.Finish(FileNameForProgress, true, OnceDoneOperation);
  1973. }
  1974. }
  1975. }
  1976. __finally
  1977. {
  1978. if (!NonBlocking)
  1979. {
  1980. Progress.Stop();
  1981. }
  1982. }
  1983. DebugAssert(!FAutoOperation);
  1984. if (!RemoteFiles)
  1985. {
  1986. TempDir = IncludeTrailingBackslash(TempDir);
  1987. for (int Index = 0; Index < RemoteFileListFull->Count; Index++)
  1988. {
  1989. UnicodeString FileName = RemoteFileListFull->Strings[Index];
  1990. if (DebugAlwaysTrue(SameText(TempDir, FileName.SubString(1, TempDir.Length()))) &&
  1991. // Skip directories, process only nested files.
  1992. // The check is redundant as FileAge fails for directories anyway.
  1993. !DirectoryExists(FileName))
  1994. {
  1995. UnicodeString RemoteDir =
  1996. UnixExtractFileDir(
  1997. UnixIncludeTrailingBackslash(FTerminal->CurrentDirectory) +
  1998. ToUnixPath(FileName.SubString(TempDir.Length() + 1, FileName.Length() - TempDir.Length())));
  1999. TDateTime NewTime;
  2000. if (FileAge(FileName, NewTime) &&
  2001. (NewTime != RemoteFileTimes[Index]))
  2002. {
  2003. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  2004. TemporaryFileCopyParam(CopyParam);
  2005. CopyParam.FileMask = L"";
  2006. FAutoOperation = true;
  2007. std::unique_ptr<TStrings> TemporaryFilesList(new TStringList());
  2008. TemporaryFilesList->Add(FileName);
  2009. FTerminal->CopyToRemote(TemporaryFilesList.get(), RemoteDir, &CopyParam, cpTemporary, NULL);
  2010. }
  2011. }
  2012. }
  2013. }
  2014. }
  2015. __finally
  2016. {
  2017. FAutoOperation = false;
  2018. if (!RootTempDir.IsEmpty() && DebugAlwaysTrue(!RemoteFiles))
  2019. {
  2020. RecursiveDeleteFile(ExcludeTrailingBackslash(RootTempDir), false);
  2021. }
  2022. }
  2023. }
  2024. __finally
  2025. {
  2026. if (RemoteFileList != FileList)
  2027. {
  2028. delete RemoteFileList;
  2029. }
  2030. if (LocalFileList != ALocalFileList)
  2031. {
  2032. delete LocalFileList;
  2033. }
  2034. if (RemoteFileListFull != RemoteFileList)
  2035. {
  2036. delete RemoteFileListFull;
  2037. }
  2038. }
  2039. }
  2040. //---------------------------------------------------------------------------
  2041. void __fastcall TCustomScpExplorerForm::LocalCustomCommandWithLocalFiles(
  2042. const TCustomCommandType & ACommand, const UnicodeString & Command, const TCustomCommandData & Data,
  2043. bool FileListCommand, UnicodeString * POutput)
  2044. {
  2045. std::unique_ptr<TStrings> SelectedFileList(DirView(osLocal)->CreateFileList(false, true, NULL));
  2046. std::unique_ptr<TStrings> LocalFileList(new TStringList());
  2047. for (int Index = 0; Index < SelectedFileList->Count; Index++)
  2048. {
  2049. UnicodeString FileName = SelectedFileList->Strings[Index];
  2050. if (DirectoryExists(FileName))
  2051. {
  2052. if (FLAGSET(ACommand.Params, ccApplyToDirectories))
  2053. {
  2054. LocalFileList->Add(FileName);
  2055. }
  2056. if (FLAGSET(ACommand.Params, ccRecursive))
  2057. {
  2058. TMakeLocalFileListParams MakeFileListParam;
  2059. MakeFileListParam.FileList = LocalFileList.get();
  2060. MakeFileListParam.FileTimes = NULL;
  2061. MakeFileListParam.IncludeDirs = FLAGSET(ACommand.Params, ccApplyToDirectories);
  2062. MakeFileListParam.Recursive = true;
  2063. ProcessLocalDirectory(FileName, Terminal->MakeLocalFileList, &MakeFileListParam);
  2064. }
  2065. }
  2066. else
  2067. {
  2068. LocalFileList->Add(FileName);
  2069. }
  2070. }
  2071. UnicodeString RemotePath;
  2072. if (Terminal != NULL)
  2073. {
  2074. RemotePath = Terminal->CurrentDirectory;
  2075. }
  2076. if (FileListCommand)
  2077. {
  2078. UnicodeString FileList = MakeFileList(LocalFileList.get());
  2079. TLocalCustomCommand CustomCommand(
  2080. Data, RemotePath, DefaultDownloadTargetDirectory(),
  2081. L"", L"", FileList);
  2082. ExecuteProcessChecked(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  2083. }
  2084. else
  2085. {
  2086. TFileOperationProgressType Progress(&OperationProgress, &OperationFinished);
  2087. Progress.Start(foCustomCommand, osLocal, LocalFileList->Count);
  2088. DebugAssert(FProgressForm != NULL);
  2089. FProgressForm->ReadOnly = true;
  2090. try
  2091. {
  2092. for (int Index = 0; Index < LocalFileList->Count; Index++)
  2093. {
  2094. UnicodeString FileName = LocalFileList->Strings[Index];
  2095. Progress.SetFile(FileName);
  2096. TLocalCustomCommand CustomCommand(
  2097. Data, RemotePath, DefaultDownloadTargetDirectory(),
  2098. FileName, L"", L"");
  2099. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  2100. TOnceDoneOperation OnceDoneOperation;
  2101. Progress.Finish(FileName, true, OnceDoneOperation);
  2102. }
  2103. }
  2104. __finally
  2105. {
  2106. Progress.Stop();
  2107. }
  2108. }
  2109. }
  2110. //---------------------------------------------------------------------------
  2111. void __fastcall TCustomScpExplorerForm::LocalCustomCommand(TStrings * FileList,
  2112. const TCustomCommandType & ACommand, TStrings * ALocalFileList,
  2113. const TCustomCommandData & Data, const UnicodeString & CommandCommand)
  2114. {
  2115. UnicodeString RemotePath = (Terminal != NULL) ? Terminal->CurrentDirectory : UnicodeString();
  2116. TLocalCustomCommand LocalCustomCommand(Data, RemotePath, DefaultDownloadTargetDirectory());
  2117. TWinInteractiveCustomCommand InteractiveCustomCommand(
  2118. &LocalCustomCommand, ACommand.Name, ACommand.HomePage);
  2119. UnicodeString Command = InteractiveCustomCommand.Complete(CommandCommand, false);
  2120. bool FileListCommand = LocalCustomCommand.IsFileListCommand(Command);
  2121. bool LocalFileCommand = LocalCustomCommand.HasLocalFileName(Command);
  2122. Configuration->Usage->Inc(L"LocalCustomCommandRuns2");
  2123. std::unique_ptr<UnicodeString> POutput;
  2124. if (FLAGSET(ACommand.Params, ccShowResultsInMsgBox) ||
  2125. FLAGSET(ACommand.Params, ccCopyResults))
  2126. {
  2127. POutput.reset(new UnicodeString());
  2128. }
  2129. if (!LocalCustomCommand.IsFileCommand(Command))
  2130. {
  2131. ExecuteProcessChecked(LocalCustomCommand.Complete(Command, true), HelpKeyword, POutput.get());
  2132. }
  2133. // remote files?
  2134. else if ((FCurrentSide == osRemote) || LocalFileCommand)
  2135. {
  2136. LocalCustomCommandPure(FileList, ACommand, Command, ALocalFileList, Data, LocalFileCommand, FileListCommand, POutput.get());
  2137. }
  2138. // local files
  2139. else
  2140. {
  2141. LocalCustomCommandWithLocalFiles(ACommand, Command, Data, FileListCommand, POutput.get());
  2142. }
  2143. if (POutput.get() != NULL)
  2144. {
  2145. // If the output is single-line, we do not want the trailing CRLF, as the CopyToClipboard(TStrings *) does
  2146. int P = POutput->Pos(sLineBreak);
  2147. if (P == POutput->Length() - static_cast<int>(strlen(sLineBreak)) + 1)
  2148. {
  2149. POutput->SetLength(P - 1);
  2150. }
  2151. // Copy even empty output to clipboard
  2152. if (FLAGSET(ACommand.Params, ccCopyResults))
  2153. {
  2154. CopyToClipboard(*POutput);
  2155. }
  2156. // But do not show an empty message box.
  2157. // This way the ShowResultsInMsgBox can be used to suppress a console window of commands with no output
  2158. if (FLAGSET(ACommand.Params, ccShowResultsInMsgBox) &&
  2159. !POutput->IsEmpty())
  2160. {
  2161. TClipboardHandler ClipboardHandler;
  2162. ClipboardHandler.Text = *POutput;
  2163. TMessageParams Params;
  2164. TQueryButtonAlias Aliases[1];
  2165. Aliases[0].Button = qaRetry;
  2166. Aliases[0].Alias = LoadStr(EDIT_COPY);
  2167. Aliases[0].OnSubmit = &ClipboardHandler.Copy;
  2168. Params.Aliases = Aliases;
  2169. Params.AliasesCount = LENOF(Aliases);
  2170. MessageDialog(*POutput, qtInformation, qaOK | qaRetry, HelpKeyword, &Params);
  2171. }
  2172. }
  2173. }
  2174. //---------------------------------------------------------------------------
  2175. void __fastcall TCustomScpExplorerForm::CustomCommand(TStrings * FileList,
  2176. const TCustomCommandType & ACommand, TStrings * ALocalFileList)
  2177. {
  2178. TCustomCommandData Data;
  2179. UnicodeString Site;
  2180. UnicodeString RemotePath;
  2181. if (Terminal != NULL)
  2182. {
  2183. Data = TCustomCommandData(Terminal);
  2184. Site = Terminal->SessionData->SessionKey;
  2185. RemotePath = Terminal->CurrentDirectory;
  2186. }
  2187. UnicodeString HelpKeyword = ACommand.HomePage;
  2188. std::unique_ptr<TStrings> CustomCommandOptions(CloneStrings(WinConfiguration->CustomCommandOptions));
  2189. if (ACommand.AnyOptionWithFlag(TCustomCommandType::ofRun))
  2190. {
  2191. std::unique_ptr<TCustomCommand> CustomCommandForOptions;
  2192. if (FLAGCLEAR(ACommand.Params, ccLocal))
  2193. {
  2194. CustomCommandForOptions.reset(new TRemoteCustomCommand(Data, RemotePath));
  2195. }
  2196. else
  2197. {
  2198. CustomCommandForOptions.reset(new TLocalCustomCommand(Data, RemotePath, DefaultDownloadTargetDirectory()));
  2199. }
  2200. if (!DoCustomCommandOptionsDialog(
  2201. &ACommand, CustomCommandOptions.get(), NULL, TCustomCommandType::ofRun, CustomCommandForOptions.get(), Site, NULL))
  2202. {
  2203. Abort();
  2204. }
  2205. }
  2206. UnicodeString CommandCommand = ACommand.GetCommandWithExpandedOptions(CustomCommandOptions.get(), Site);
  2207. if (FLAGCLEAR(ACommand.Params, ccLocal))
  2208. {
  2209. RemoteCustomCommand(FileList, ACommand, Data, CommandCommand);
  2210. }
  2211. else
  2212. {
  2213. LocalCustomCommand(FileList, ACommand, ALocalFileList, Data, CommandCommand);
  2214. }
  2215. }
  2216. //---------------------------------------------------------------------------
  2217. void __fastcall TCustomScpExplorerForm::BothCustomCommand(
  2218. const TCustomCommandType & Command)
  2219. {
  2220. DebugAssert(FCustomCommandLocalFileList != NULL);
  2221. DebugAssert(FCustomCommandRemoteFileList != NULL);
  2222. DebugAssert(FCustomCommandLocalFileList->Count == FCustomCommandRemoteFileList->Count);
  2223. TStrings * LocalFileList = new TStringList();
  2224. TStrings * RemoteFileList = new TStringList();
  2225. try
  2226. {
  2227. for (int Index = 0; Index < FCustomCommandLocalFileList->Count; Index++)
  2228. {
  2229. LocalFileList->Clear();
  2230. LocalFileList->AddObject(
  2231. FCustomCommandLocalFileList->Strings[Index],
  2232. FCustomCommandLocalFileList->Objects[Index]);
  2233. RemoteFileList->Clear();
  2234. RemoteFileList->AddObject(
  2235. FCustomCommandRemoteFileList->Strings[Index],
  2236. FCustomCommandRemoteFileList->Objects[Index]);
  2237. CustomCommand(RemoteFileList, Command, LocalFileList);
  2238. }
  2239. }
  2240. __finally
  2241. {
  2242. delete LocalFileList;
  2243. delete RemoteFileList;
  2244. }
  2245. }
  2246. //---------------------------------------------------------------------------
  2247. void __fastcall TCustomScpExplorerForm::CustomCommandMenu(
  2248. TAction * Action, TStrings * LocalFileList, TStrings * RemoteFileList)
  2249. {
  2250. delete FCustomCommandLocalFileList;
  2251. delete FCustomCommandRemoteFileList;
  2252. // takeover ownership,
  2253. // the lists must survive the MenuPopup as OnClick occurs only after it exits
  2254. FCustomCommandLocalFileList = LocalFileList;
  2255. FCustomCommandRemoteFileList = RemoteFileList;
  2256. TButton * Button = dynamic_cast<TButton *>(Action->ActionComponent);
  2257. if (Button != NULL)
  2258. {
  2259. FCustomCommandMenu->Items->Clear();
  2260. NonVisualDataModule->CreateCustomCommandsMenu(FCustomCommandMenu->Items, false, false, ccltBoth);
  2261. MenuPopup(FCustomCommandMenu, Button);
  2262. }
  2263. else
  2264. {
  2265. NonVisualDataModule->CreateCustomCommandsMenu(Action, false, ccltBoth);
  2266. }
  2267. }
  2268. //---------------------------------------------------------------------------
  2269. void __fastcall TCustomScpExplorerForm::TerminalCaptureLog(
  2270. const UnicodeString & AddedLine, TCaptureOutputType OutputType)
  2271. {
  2272. DebugAssert(FCapturedLog != NULL);
  2273. if ((OutputType == cotOutput) || (OutputType == cotError))
  2274. {
  2275. FCapturedLog->Add(AddedLine);
  2276. }
  2277. }
  2278. //---------------------------------------------------------------------------
  2279. bool __fastcall TCustomScpExplorerForm::IsFileControl(TObject * Control,
  2280. TOperationSide Side)
  2281. {
  2282. return
  2283. (Side == osOther) &&
  2284. ((Control == DirView(osOther)) || (Control == DriveView(osOther)));
  2285. }
  2286. //---------------------------------------------------------------------------
  2287. void __fastcall TCustomScpExplorerForm::DirViewContextPopupDefaultItem(
  2288. TOperationSide Side, TTBXCustomItem * Item, TDoubleClickAction DoubleClickAction)
  2289. {
  2290. TTBItemOptions O;
  2291. O = Item->Options;
  2292. TCustomDirView * DView = DirView(Side);
  2293. if ((DView->ItemFocused != NULL) &&
  2294. (WinConfiguration->DoubleClickAction == DoubleClickAction) &&
  2295. // when resolving links is disabled, default action is to enter the directory,
  2296. // no matter what DoubleClickAction is configured to
  2297. (IsSideLocalBrowser(Side) || Terminal->ResolvingSymlinks || Terminal->IsEncryptingFiles()) &&
  2298. // Can only Edit files, but can Open/Copy even directories
  2299. ((DoubleClickAction != dcaEdit) ||
  2300. !DView->ItemIsDirectory(DView->ItemFocused)))
  2301. {
  2302. Item->Options = O << tboDefault;
  2303. }
  2304. else
  2305. {
  2306. Item->Options = O >> tboDefault;
  2307. }
  2308. }
  2309. //---------------------------------------------------------------------------
  2310. void __fastcall TCustomScpExplorerForm::DirViewContextPopup(
  2311. TOperationSide Side, Byte PopupComponent, const TPoint &MousePos)
  2312. {
  2313. TCustomDirView * DView = DirView(Side);
  2314. TListItem * Item = DView->ItemFocused;
  2315. if ((DView->GetItemAt(MousePos.x, MousePos.y) == Item) &&
  2316. EnableFocusedOperation[Side])
  2317. {
  2318. TPoint ClientPoint;
  2319. ClientPoint = ((MousePos.x < 0) && (MousePos.y < 0)) ?
  2320. TPoint(0, 0) : MousePos;
  2321. FLastContextPopupScreenPoint = DView->ClientToScreen(ClientPoint);
  2322. reinterpret_cast<TPopupMenu*>(GetComponent(PopupComponent))->Popup(
  2323. FLastContextPopupScreenPoint.x, FLastContextPopupScreenPoint.y);
  2324. }
  2325. }
  2326. //---------------------------------------------------------------------------
  2327. void __fastcall TCustomScpExplorerForm::RemoteDirViewContextPopup(
  2328. TObject * /*Sender*/, const TPoint &MousePos, bool &Handled)
  2329. {
  2330. DirViewContextPopupDefaultItem(osRemote, NonVisualDataModule->RemoteOpenMenuItem, dcaOpen);
  2331. DirViewContextPopupDefaultItem(osRemote, NonVisualDataModule->RemoteEditMenuItem, dcaEdit);
  2332. DirViewContextPopupDefaultItem(osRemote, NonVisualDataModule->RemoteCopyMenuItem, dcaCopy);
  2333. DirViewContextPopup(osRemote, fcRemotePopup, MousePos);
  2334. Handled = true;
  2335. }
  2336. //---------------------------------------------------------------------------
  2337. void __fastcall TCustomScpExplorerForm::ReloadLocalDirectory(const UnicodeString Directory)
  2338. {
  2339. }
  2340. //---------------------------------------------------------------------------
  2341. void __fastcall TCustomScpExplorerForm::BatchStart(void *& /*Storage*/)
  2342. {
  2343. DebugAssert(FErrorList == NULL);
  2344. if (WinConfiguration->ContinueOnError)
  2345. {
  2346. FErrorList = new TStringList();
  2347. Configuration->Usage->Inc(L"ContinuationsOnError");
  2348. }
  2349. NonVisualDataModule->StartBusy();
  2350. }
  2351. //---------------------------------------------------------------------------
  2352. void __fastcall TCustomScpExplorerForm::BatchEnd(void * /*Storage*/)
  2353. {
  2354. NonVisualDataModule->EndBusy();
  2355. if (FErrorList)
  2356. {
  2357. HandleErrorList(FErrorList);
  2358. }
  2359. }
  2360. //---------------------------------------------------------------------------
  2361. void __fastcall TCustomScpExplorerForm::UpdateCopyParamCounters(
  2362. const TCopyParamType & CopyParam)
  2363. {
  2364. if (!CopyParam.IncludeFileMask.Masks.IsEmpty())
  2365. {
  2366. Configuration->Usage->Inc(L"FileMaskUses");
  2367. }
  2368. if (IsEffectiveFileNameMask(CopyParam.FileMask))
  2369. {
  2370. Configuration->Usage->Inc(L"OperationMaskUses");
  2371. }
  2372. }
  2373. //---------------------------------------------------------------------------
  2374. bool __fastcall TCustomScpExplorerForm::ExecuteCopyMoveFileOperation(
  2375. TFileOperation Operation, TOperationSide Side, TStrings * FileList, bool NoConfirmation, void * AParam)
  2376. {
  2377. DebugAssert(!IsLocalBrowserMode());
  2378. TTransferDirection Direction = (Side == osLocal ? tdToRemote : tdToLocal);
  2379. TTransferType Type = (Operation == foCopy ? ttCopy : ttMove);
  2380. TTransferOperationParam DefaultParam;
  2381. TTransferOperationParam & Param = (AParam != NULL) ? *static_cast<TTransferOperationParam *>(AParam) : DefaultParam;
  2382. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  2383. switch (Param.Queue)
  2384. {
  2385. case asOn:
  2386. CopyParam.Queue = true;
  2387. break;
  2388. case asOff:
  2389. CopyParam.Queue = false;
  2390. break;
  2391. case asAuto:
  2392. default:
  2393. // keep default
  2394. break;
  2395. }
  2396. bool Result =
  2397. CopyParamDialog(Direction, Type, Param.Temp, FileList, Param.TargetDirectory, CopyParam, !NoConfirmation, Param.DragDrop, Param.Options);
  2398. if (Result)
  2399. {
  2400. DebugAssert(Terminal);
  2401. bool SelectionRestored = false;
  2402. TCustomDirView * DView = NULL;
  2403. if (HasDirView[Side])
  2404. {
  2405. DView = DirView(Side);
  2406. DView->SaveSelection();
  2407. DView->SaveSelectedNames();
  2408. }
  2409. UpdateCopyParamCounters(CopyParam);
  2410. std::unique_ptr<TStringList> TransferResumeList(new TStringList());
  2411. DebugAssert(FTransferResumeList == NULL);
  2412. FTransferResumeList = Terminal->IsCapable[fcMoveToQueue] ? TransferResumeList.get() : NULL;
  2413. FMoveToQueue = false;
  2414. int Params = FLAGMASK(Operation == foMove, cpDelete);
  2415. try
  2416. {
  2417. TStrings * PermanentFileList;
  2418. std::unique_ptr<TStrings> PermanentFileListOwner;
  2419. try
  2420. {
  2421. if (Side == osLocal)
  2422. {
  2423. PermanentFileList = FileList;
  2424. Params |= FLAGMASK(Param.Temp, cpTemporary);
  2425. Terminal->CopyToRemote(FileList, Param.TargetDirectory, &CopyParam, Params, NULL);
  2426. if (Operation == foMove)
  2427. {
  2428. ReloadLocalDirectory();
  2429. if (DView != NULL)
  2430. {
  2431. DView->RestoreSelection();
  2432. }
  2433. SelectionRestored = true;
  2434. }
  2435. }
  2436. else
  2437. {
  2438. // Clone the file list as it may refer to current directory files,
  2439. // which get destroyed, when the source directory is reloaded after foMove operation.
  2440. // We should actually clone the file list for whole ExecuteFileOperation to protect against reloads.
  2441. // But for a hotfix, we are not going to do such a big change.
  2442. PermanentFileListOwner.reset(TRemoteFileList::CloneStrings(FileList));
  2443. PermanentFileList = PermanentFileListOwner.get();
  2444. try
  2445. {
  2446. Terminal->CopyToLocal(FileList, Param.TargetDirectory, &CopyParam, Params, NULL);
  2447. }
  2448. __finally
  2449. {
  2450. if (Operation == foMove)
  2451. {
  2452. if (DView != NULL)
  2453. {
  2454. DView->RestoreSelection();
  2455. }
  2456. SelectionRestored = true;
  2457. }
  2458. ReloadLocalDirectory(Param.TargetDirectory);
  2459. }
  2460. }
  2461. }
  2462. catch (EAbort &)
  2463. {
  2464. if (FMoveToQueue)
  2465. {
  2466. Params |= (CopyParam.QueueNoConfirmation ? cpNoConfirmation : 0);
  2467. DebugAssert(CopyParam.TransferSkipList == NULL);
  2468. DebugAssert(CopyParam.TransferResumeFile.IsEmpty());
  2469. if (TransferResumeList->Count > 0)
  2470. {
  2471. CopyParam.TransferResumeFile = TransferResumeList->Strings[TransferResumeList->Count - 1];
  2472. TransferResumeList->Delete(TransferResumeList->Count - 1);
  2473. }
  2474. CopyParam.TransferSkipList = TransferResumeList.release();
  2475. // not really needed, just to keep it consistent with TransferResumeList
  2476. FTransferResumeList = NULL;
  2477. FMoveToQueue = false;
  2478. Configuration->Usage->Inc("MovesToBackground");
  2479. AddQueueItem(Queue, Direction, PermanentFileList, Param.TargetDirectory, CopyParam, Params);
  2480. ClearTransferSourceSelection(Direction);
  2481. }
  2482. throw;
  2483. }
  2484. }
  2485. __finally
  2486. {
  2487. if (!SelectionRestored && (DView != NULL))
  2488. {
  2489. DView->DiscardSavedSelection();
  2490. }
  2491. FTransferResumeList = NULL;
  2492. }
  2493. }
  2494. return Result;
  2495. }
  2496. //---------------------------------------------------------------------------
  2497. bool __fastcall TCustomScpExplorerForm::ExecuteDeleteFileOperation(
  2498. TOperationSide Side, TStrings * FileList, void * Param)
  2499. {
  2500. DebugAssert(FileList->Count);
  2501. // We deliberately do not toggle alternative flag (Param), but use OR,
  2502. // because the Param is set only when command is invoked using Shift-Del/F8 keyboard
  2503. // shortcut of CurrentDeleteAlternativeAction
  2504. bool Alternative =
  2505. bool(Param) || UseAlternativeFunction();
  2506. bool Recycle;
  2507. if (IsSideLocalBrowser(Side))
  2508. {
  2509. Recycle = (WinConfiguration->DeleteToRecycleBin != Alternative);
  2510. }
  2511. else
  2512. {
  2513. Recycle =
  2514. (Terminal->SessionData->DeleteToRecycleBin != Alternative) &&
  2515. !Terminal->SessionData->RecycleBinPath.IsEmpty() &&
  2516. !Terminal->IsRecycledFile(FileList->Strings[0]);
  2517. }
  2518. bool Result = !(Recycle ? WinConfiguration->ConfirmRecycling : WinConfiguration->ConfirmDeleting);
  2519. if (!Result)
  2520. {
  2521. UnicodeString Query;
  2522. if (FileList->Count == 1)
  2523. {
  2524. if (IsSideLocalBrowser(Side))
  2525. {
  2526. Query = ExtractFileName(FileList->Strings[0]);
  2527. }
  2528. else
  2529. {
  2530. Query = UnixExtractFileName(FileList->Strings[0]);
  2531. }
  2532. Query = FMTLOAD((Recycle ? CONFIRM_RECYCLE_FILE : CONFIRM_DELETE_FILE), (Query));
  2533. }
  2534. else
  2535. {
  2536. Query = FMTLOAD((Recycle ? CONFIRM_RECYCLE_FILES : CONFIRM_DELETE_FILES), (FileList->Count));
  2537. }
  2538. TMessageParams Params(mpNeverAskAgainCheck);
  2539. Params.ImageName = L"Delete file";
  2540. unsigned int Answer =
  2541. MessageDialog(MainInstructions(Query), qtConfirmation, qaOK | qaCancel, HELP_DELETE_FILE, &Params);
  2542. if (Answer == qaNeverAskAgain)
  2543. {
  2544. Result = true;
  2545. if (Recycle)
  2546. {
  2547. WinConfiguration->ConfirmRecycling = false;
  2548. }
  2549. else
  2550. {
  2551. WinConfiguration->ConfirmDeleting = false;
  2552. }
  2553. }
  2554. else
  2555. {
  2556. Result = (Answer == qaOK);
  2557. }
  2558. }
  2559. if (Result)
  2560. {
  2561. DeleteFiles(Side, FileList, FLAGMASK(Alternative, dfAlternative));
  2562. }
  2563. return Result;
  2564. }
  2565. //---------------------------------------------------------------------------
  2566. bool __fastcall TCustomScpExplorerForm::ExecuteFileOperation(TFileOperation Operation,
  2567. TOperationSide Side, TStrings * FileList, bool NoConfirmation, void * Param)
  2568. {
  2569. TAutoBatch AutoBatch(this);
  2570. bool Result;
  2571. if ((Operation == foCopy) || (Operation == foMove))
  2572. {
  2573. Result = ExecuteCopyMoveFileOperation(Operation, Side, FileList, NoConfirmation, Param);
  2574. }
  2575. else if (Operation == foRename)
  2576. {
  2577. DebugAssert(DirView(Side)->ItemFocused);
  2578. DirView(Side)->ItemFocused->EditCaption();
  2579. Result = true;
  2580. }
  2581. else if (Operation == foDelete)
  2582. {
  2583. Result = ExecuteDeleteFileOperation(Side, FileList, Param);
  2584. }
  2585. else if (Operation == foSetProperties)
  2586. {
  2587. DirView(osRemote)->SaveSelectedNames();
  2588. Result = SetProperties(Side, FileList);
  2589. }
  2590. else if (Operation == foCustomCommand)
  2591. {
  2592. DebugAssert(Param);
  2593. DebugAssert(Side == osRemote); // Side carries no meaning for foCustomCommand
  2594. DirView(osRemote)->SaveSelectedNames();
  2595. const TCustomCommandType * Command = static_cast<const TCustomCommandType*>(Param);
  2596. CustomCommand(FileList, *Command, NULL);
  2597. Result = true;
  2598. }
  2599. else if ((Operation == foRemoteMove) || (Operation == foRemoteCopy))
  2600. {
  2601. DebugAssert(!IsSideLocalBrowser(Side));
  2602. Result = RemoteTransferFiles(FileList, NoConfirmation,
  2603. (Operation == foRemoteMove), reinterpret_cast<TManagedTerminal *>(Param));
  2604. }
  2605. else if (Operation == foLock)
  2606. {
  2607. DebugAssert(!IsSideLocalBrowser(Side));
  2608. LockFiles(FileList, true);
  2609. Result = true;
  2610. }
  2611. else if (Operation == foUnlock)
  2612. {
  2613. DebugAssert(!IsSideLocalBrowser(Side));
  2614. LockFiles(FileList, false);
  2615. Result = true;
  2616. }
  2617. else
  2618. {
  2619. DebugFail();
  2620. }
  2621. return Result;
  2622. }
  2623. //---------------------------------------------------------------------------
  2624. TOperationSide __fastcall TCustomScpExplorerForm::GetSide(TOperationSide Side)
  2625. {
  2626. if (Side == osCurrent)
  2627. {
  2628. Side = FCurrentSide;
  2629. }
  2630. return Side;
  2631. }
  2632. //---------------------------------------------------------------------------
  2633. bool __fastcall TCustomScpExplorerForm::ExecuteFileOperation(TFileOperation Operation,
  2634. TOperationSide Side, bool OnFocused, bool NoConfirmation, void * Param)
  2635. {
  2636. Side = GetSide(Side);
  2637. bool Result;
  2638. TStrings * FileList = DirView(Side)->CreateFileList(OnFocused, IsSideLocalBrowser(Side), NULL);
  2639. try
  2640. {
  2641. Result = ExecuteFileOperation(Operation, Side, FileList, NoConfirmation, Param);
  2642. }
  2643. __finally
  2644. {
  2645. delete FileList;
  2646. }
  2647. return Result;
  2648. }
  2649. //---------------------------------------------------------------------------
  2650. void __fastcall TCustomScpExplorerForm::ExecuteFileOperationCommand(
  2651. TFileOperation Operation, TOperationSide Side, bool OnFocused,
  2652. bool NoConfirmation, void * Param)
  2653. {
  2654. if (ExecuteFileOperation(Operation, Side, OnFocused, NoConfirmation, Param))
  2655. {
  2656. if ((Operation == foCopy) || (Operation == foMove))
  2657. {
  2658. DebugAssert(!IsLocalBrowserMode());
  2659. if (GetSide(Side) == osLocal)
  2660. {
  2661. Configuration->Usage->Inc(L"UploadsCommand");
  2662. }
  2663. else
  2664. {
  2665. Configuration->Usage->Inc(L"DownloadsCommand");
  2666. }
  2667. }
  2668. }
  2669. }
  2670. //---------------------------------------------------------------------------
  2671. void __fastcall TCustomScpExplorerForm::ExecuteCopyOperationCommand(
  2672. TOperationSide Side, bool OnFocused, unsigned int Flags)
  2673. {
  2674. TTransferOperationParam Param;
  2675. if ((WinConfiguration->Interface != ifCommander) ||
  2676. WinConfiguration->ScpCommander.ExplorerKeyboardShortcuts)
  2677. {
  2678. Flags &= ~cocShortCutHint;
  2679. }
  2680. TCustomDirView * DView = DirView(Side);
  2681. Param.Options =
  2682. FLAGMASK(FLAGSET(Flags, cocShortCutHint), coShortCutHint) |
  2683. FLAGMASK(SelectedAllFilesInDirView(DView), coAllFiles);
  2684. if (FLAGSET(Flags, cocQueue))
  2685. {
  2686. Param.Queue = asOn;
  2687. }
  2688. else if (FLAGSET(Flags, cocNonQueue))
  2689. {
  2690. Param.Queue = asOff;
  2691. }
  2692. ExecuteFileOperationCommand(foCopy, Side, OnFocused, false, &Param);
  2693. }
  2694. //---------------------------------------------------------------------------
  2695. void __fastcall TCustomScpExplorerForm::HandleErrorList(TStringList *& ErrorList)
  2696. {
  2697. try
  2698. {
  2699. if (ErrorList->Count)
  2700. {
  2701. UnicodeString Message = MainInstructions(FMTLOAD(ERROR_LIST_COUNT, (ErrorList->Count)));
  2702. if (MessageDialog(Message, qtError,
  2703. qaOK | qaCancel, HELP_NONE) == qaOK)
  2704. {
  2705. unsigned int Answer;
  2706. int Index = 0;
  2707. do
  2708. {
  2709. DebugAssert(Index >= 0 && Index < ErrorList->Count);
  2710. TQueryButtonAlias Aliases[2];
  2711. Aliases[0].Button = qaYes;
  2712. Aliases[0].Alias = LoadStr(PREV_BUTTON);
  2713. Aliases[1].Button = qaNo;
  2714. Aliases[1].Alias = LoadStr(NEXT_BUTTON);
  2715. TMessageParams Params;
  2716. Params.Aliases = Aliases;
  2717. Params.AliasesCount = LENOF(Aliases);
  2718. Answer = MoreMessageDialog(
  2719. FMTLOAD(ERROR_LIST_NUMBER, (Index+1, ErrorList->Count, ErrorList->Strings[Index])),
  2720. dynamic_cast<TStrings *>(ErrorList->Objects[Index]), qtError,
  2721. (Index ? qaYes : 0) | (Index < ErrorList->Count - 1 ? qaNo : 0) |
  2722. qaOK, HELP_NONE, &Params);
  2723. if (Answer == qaNo)
  2724. {
  2725. Index++;
  2726. }
  2727. if (Answer == qaYes)
  2728. {
  2729. Index--;
  2730. }
  2731. }
  2732. while (Answer != qaOK);
  2733. }
  2734. }
  2735. }
  2736. __finally
  2737. {
  2738. TStrings * List = ErrorList;
  2739. ErrorList = NULL;
  2740. for (int i = 0; i < List->Count; i++)
  2741. {
  2742. delete List->Objects[i];
  2743. }
  2744. delete List;
  2745. }
  2746. }
  2747. //---------------------------------------------------------------------------
  2748. void __fastcall TCustomScpExplorerForm::ExecuteRemoteFile(
  2749. const UnicodeString & FullFileName, TRemoteFile * File, TExecuteFileBy ExecuteFileBy)
  2750. {
  2751. // needed for checking filemasks, as there's no directory object
  2752. // associated with the file object
  2753. File->FullFileName = FullFileName;
  2754. TFileMasks::TParams MaskParams;
  2755. MaskParams.Size = File->Size;
  2756. MaskParams.Modification = File->Modification;
  2757. ExecuteFile(osRemote, ExecuteFileBy, NULL, FullFileName, File, MaskParams);
  2758. }
  2759. //---------------------------------------------------------------------------
  2760. void __fastcall TCustomScpExplorerForm::EditNew(TOperationSide Side)
  2761. {
  2762. DebugAssert(!WinConfiguration->DisableOpenEdit);
  2763. Side = GetSide(Side);
  2764. TCustomDirView * DView = DirView(Side);
  2765. TListItem * FocusedItem = DView->ItemFocused;
  2766. UnicodeString Name;
  2767. if ((FocusedItem != NULL) && !DView->ItemIsDirectory(FocusedItem))
  2768. {
  2769. Name = DView->ItemFileName(FocusedItem);
  2770. }
  2771. else
  2772. {
  2773. Name = LoadStr(NEW_FILE);
  2774. }
  2775. UnicodeString Names = Name;
  2776. std::unique_ptr<TStrings> History(CloneStrings(CustomWinConfiguration->History[L"EditFile"]));
  2777. if (InputDialog(LoadStr(EDIT_FILE_CAPTION), LoadStr(EDIT_FILE_PROMPT), Names,
  2778. HELP_EDIT_NEW, History.get(), true))
  2779. {
  2780. while (!Names.IsEmpty())
  2781. {
  2782. Name = CutToChar(Names, FileMasksDelimiters[1], false);
  2783. CustomWinConfiguration->History[L"EditFile"] = History.get();
  2784. UnicodeString TargetFileName;
  2785. UnicodeString LocalFileName;
  2786. UnicodeString RootTempDir;
  2787. UnicodeString TempDir;
  2788. UnicodeString RemoteDirectory;
  2789. bool ExistingFile = false;
  2790. if (!IsSideLocalBrowser(Side))
  2791. {
  2792. Name = AbsolutePath(FTerminal->CurrentDirectory, Name);
  2793. TRemoteFile * File = NULL;
  2794. if (FTerminal->FileExists(Name, &File) &&
  2795. !File->IsDirectory)
  2796. {
  2797. try
  2798. {
  2799. ExecuteRemoteFile(Name, File, efDefaultEditor);
  2800. ExistingFile = true;
  2801. }
  2802. __finally
  2803. {
  2804. delete File;
  2805. }
  2806. }
  2807. if (!ExistingFile)
  2808. {
  2809. RemoteDirectory = UnixExtractFilePath(Name);
  2810. TemporaryDirectoryForRemoteFiles(
  2811. RemoteDirectory, GUIConfiguration->CurrentCopyParam, TempDir, RootTempDir);
  2812. TargetFileName = UnixExtractFileName(Name);
  2813. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  2814. LocalFileName = TempDir +
  2815. // We probably do not want to trim the VMS version here
  2816. FTerminal->ChangeFileName(&CopyParam, TargetFileName, osRemote, false);
  2817. }
  2818. }
  2819. else
  2820. {
  2821. if (ExtractFilePath(Name).IsEmpty())
  2822. {
  2823. LocalFileName = IncludeTrailingBackslash(DView->PathName) + Name;
  2824. }
  2825. else
  2826. {
  2827. LocalFileName = ExpandFileName(Name);
  2828. }
  2829. TargetFileName = ExtractFileName(Name);
  2830. }
  2831. if (!ExistingFile)
  2832. {
  2833. bool NewFile = !FileExists(ApiPath(LocalFileName));
  2834. if (NewFile)
  2835. {
  2836. int File = FileCreate(ApiPath(LocalFileName));
  2837. if (File < 0)
  2838. {
  2839. if (!RootTempDir.IsEmpty())
  2840. {
  2841. RecursiveDeleteFile(ExcludeTrailingBackslash(RootTempDir), false);
  2842. }
  2843. throw Exception(FMTLOAD(CREATE_FILE_ERROR, (LocalFileName)));
  2844. }
  2845. else
  2846. {
  2847. FileClose(File);
  2848. }
  2849. }
  2850. TExecuteFileBy ExecuteFileBy = efDefaultEditor;
  2851. const TEditorData * ExternalEditor = NULL;
  2852. TFileMasks::TParams MaskParams; // size not known
  2853. ExecuteFileNormalize(ExecuteFileBy, ExternalEditor, TargetFileName,
  2854. false, MaskParams);
  2855. CustomExecuteFile(Side, ExecuteFileBy, LocalFileName, TargetFileName,
  2856. ExternalEditor, RootTempDir, RemoteDirectory, NewFile);
  2857. }
  2858. }
  2859. }
  2860. }
  2861. //---------------------------------------------------------------------------
  2862. bool __fastcall TCustomScpExplorerForm::RemoteExecuteForceText(
  2863. TExecuteFileBy ExecuteFileBy, const TEditorData * ExternalEditor)
  2864. {
  2865. DebugAssert((ExecuteFileBy == efExternalEditor) ==
  2866. ((ExternalEditor != NULL) && (ExternalEditor->Editor == edExternal)));
  2867. DebugAssert(ExecuteFileBy != efDefaultEditor);
  2868. return
  2869. ((ExecuteFileBy == efInternalEditor)) ||
  2870. ((ExecuteFileBy == efExternalEditor) && ExternalEditor->ExternalEditorText);
  2871. }
  2872. //---------------------------------------------------------------------------
  2873. void __fastcall TCustomScpExplorerForm::CustomExecuteFile(TOperationSide Side,
  2874. TExecuteFileBy ExecuteFileBy, UnicodeString FileName, UnicodeString OriginalFileName,
  2875. const TEditorData * ExternalEditor, UnicodeString LocalRootDirectory,
  2876. UnicodeString RemoteDirectory, bool NewFile)
  2877. {
  2878. DebugAssert(!WinConfiguration->DisableOpenEdit);
  2879. DebugAssert((ExecuteFileBy == efExternalEditor) ==
  2880. ((ExternalEditor != NULL) && (ExternalEditor->Editor == edExternal)));
  2881. DebugAssert(ExecuteFileBy != efDefaultEditor);
  2882. Side = GetSide(Side);
  2883. std::unique_ptr<TEditedFileData> Data(new TEditedFileData);
  2884. if (!IsSideLocalBrowser(Side))
  2885. {
  2886. Data->Terminal = Terminal;
  2887. Data->Queue = Queue;
  2888. Data->SessionData = CloneCurrentSessionData();
  2889. Data->ForceText = RemoteExecuteForceText(ExecuteFileBy, ExternalEditor);
  2890. Data->RemoteDirectory = RemoteDirectory;
  2891. Data->SessionName = Terminal->SessionData->SessionName;
  2892. Data->LocalRootDirectory = LocalRootDirectory;
  2893. Data->OriginalFileName = OriginalFileName;
  2894. Data->Command = L""; // will be changed later for external editor
  2895. }
  2896. if (ExecuteFileBy == efInternalEditor)
  2897. {
  2898. if (!IsSideLocalBrowser(Side))
  2899. {
  2900. UnicodeString Caption = UnixIncludeTrailingBackslash(RemoteDirectory) + OriginalFileName +
  2901. L" - " + Terminal->SessionData->SessionName;
  2902. TForm * Editor;
  2903. try
  2904. {
  2905. TNotifyEvent OnFileChanged = NULL;
  2906. TNotifyEvent OnSaveAll = NULL;
  2907. TAnyModifiedEvent OnAnyModified = NULL;
  2908. // Edited files are uploaded in background queue, what we do not support with encrypted files
  2909. if (Terminal->IsCapable[fsBackgroundTransfers])
  2910. {
  2911. OnFileChanged = FEditorManager->FileChanged;
  2912. OnSaveAll = SaveAllInternalEditors;
  2913. OnAnyModified = AnyInternalEditorModified;
  2914. }
  2915. Editor = ShowEditorForm(FileName, this, OnFileChanged,
  2916. FEditorManager->FileReload, FEditorManager->FileClosed,
  2917. OnSaveAll, OnAnyModified,
  2918. Caption, FStandaloneEditing, SessionColor, Terminal->SessionData->InternalEditorEncoding, NewFile);
  2919. }
  2920. catch(...)
  2921. {
  2922. if (!LocalRootDirectory.IsEmpty())
  2923. {
  2924. RecursiveDeleteFile(ExcludeTrailingBackslash(LocalRootDirectory), false);
  2925. }
  2926. throw;
  2927. }
  2928. FEditorManager->AddFileInternal(FileName, Data.release(), Editor);
  2929. }
  2930. else
  2931. {
  2932. DebugAssert(!FStandaloneEditing);
  2933. TForm * Editor =
  2934. ShowEditorForm(FileName, this, NULL, NULL, LocalEditorClosed,
  2935. SaveAllInternalEditors, AnyInternalEditorModified,
  2936. L"", false, SessionColor, -1, NewFile);
  2937. FLocalEditors->Add(Editor);
  2938. }
  2939. }
  2940. else
  2941. {
  2942. HANDLE Process;
  2943. if (ExecuteFileBy == efExternalEditor)
  2944. {
  2945. UnicodeString Program, Params, Dir;
  2946. Data->Command = ExternalEditor->ExternalEditor;
  2947. ReformatFileNameCommand(Data->Command);
  2948. SplitCommand(Data->Command, Program, Params, Dir);
  2949. Params = ExpandFileNameCommand(Params, FileName);
  2950. Program = ExpandEnvironmentVariables(Program);
  2951. if (!ExecuteShell(Program, Params, Process))
  2952. {
  2953. throw EOSExtException(FMTLOAD(EDITOR_ERROR, (Program)));
  2954. }
  2955. }
  2956. else
  2957. {
  2958. DebugAssert(IsSideLocalBrowser(Side));
  2959. if (!ExecuteShell(FileName, L"", Process))
  2960. {
  2961. throw EOSExtException(FMTLOAD(EXECUTE_FILE_ERROR, (FileName)));
  2962. }
  2963. }
  2964. if (IsSideLocalBrowser(Side) ||
  2965. ((ExecuteFileBy == efShell) &&
  2966. !WinConfiguration->Editor.SDIShellEditor) ||
  2967. ((ExecuteFileBy == efExternalEditor) &&
  2968. !ExternalEditor->SDIExternalEditor))
  2969. {
  2970. // no need for handle
  2971. if (Process != NULL)
  2972. {
  2973. DebugCheck(CloseHandle(Process));
  2974. }
  2975. Process = INVALID_HANDLE_VALUE;
  2976. }
  2977. else
  2978. {
  2979. if (Process == NULL)
  2980. {
  2981. throw ExtException(LoadStr(OPEN_FILE_NO_PROCESS2), L"", HELP_OPEN_FILE_NO_PROCESS);
  2982. }
  2983. }
  2984. if (!IsSideLocalBrowser(Side))
  2985. {
  2986. FEditorManager->AddFileExternal(FileName, Data.release(), Process);
  2987. }
  2988. }
  2989. }
  2990. //---------------------------------------------------------------------------
  2991. void __fastcall TCustomScpExplorerForm::SaveInternalEditor(
  2992. const UnicodeString /*FileName*/, TEditedFileData * /*Data*/, TObject * Token,
  2993. void * /*Arg*/)
  2994. {
  2995. if (Token != NULL)
  2996. {
  2997. EditorFormFileSave(static_cast<TForm *>(Token));
  2998. }
  2999. }
  3000. //---------------------------------------------------------------------------
  3001. void __fastcall TCustomScpExplorerForm::SaveAllInternalEditors(TObject * /*Sender*/)
  3002. {
  3003. for (int Index = 0; Index < FLocalEditors->Count; Index++)
  3004. {
  3005. EditorFormFileSave(static_cast<TForm *>(FLocalEditors->Items[Index]));
  3006. }
  3007. FEditorManager->ProcessFiles(SaveInternalEditor, NULL);
  3008. }
  3009. //---------------------------------------------------------------------------
  3010. void __fastcall TCustomScpExplorerForm::InternalEditorModified(
  3011. const UnicodeString /*FileName*/, TEditedFileData * /*Data*/, TObject * Token,
  3012. void * Arg)
  3013. {
  3014. if ((Token != NULL) &&
  3015. IsEditorFormModified(static_cast<TForm *>(Token)))
  3016. {
  3017. *static_cast<bool *>(Arg) = true;
  3018. }
  3019. }
  3020. //---------------------------------------------------------------------------
  3021. void __fastcall TCustomScpExplorerForm::AnyInternalEditorModified(
  3022. TObject * /*Sender*/, bool & Modified)
  3023. {
  3024. for (int Index = 0; !Modified && (Index < FLocalEditors->Count); Index++)
  3025. {
  3026. if (IsEditorFormModified(static_cast<TForm *>(FLocalEditors->Items[Index])))
  3027. {
  3028. Modified = true;
  3029. }
  3030. }
  3031. if (!Modified)
  3032. {
  3033. FEditorManager->ProcessFiles(InternalEditorModified, &Modified);
  3034. }
  3035. }
  3036. //---------------------------------------------------------------------------
  3037. void __fastcall TCustomScpExplorerForm::LocalEditorClosed(TObject * Sender, bool /*Forced*/)
  3038. {
  3039. DebugCheck(FLocalEditors->Extract(Sender) >= 0);
  3040. }
  3041. //---------------------------------------------------------------------------
  3042. void __fastcall TCustomScpExplorerForm::TemporaryDirectoryForRemoteFiles(
  3043. UnicodeString RemoteDirectory, TCopyParamType CopyParam,
  3044. UnicodeString & Result, UnicodeString & RootDirectory)
  3045. {
  3046. if (!WinConfiguration->TemporaryDirectoryDeterministic)
  3047. {
  3048. RootDirectory = IncludeTrailingBackslash(WinConfiguration->TemporaryDir());
  3049. Result = RootDirectory;
  3050. }
  3051. else
  3052. {
  3053. RootDirectory = L"";
  3054. Result = WinConfiguration->ExpandedTemporaryDirectory();
  3055. Result = IncludeTrailingBackslash(Result);
  3056. }
  3057. if (WinConfiguration->TemporaryDirectoryAppendSession)
  3058. {
  3059. Result = IncludeTrailingBackslash(Result + MakeValidFileName(Terminal->SessionData->SessionName));
  3060. }
  3061. if (WinConfiguration->TemporaryDirectoryAppendPath)
  3062. {
  3063. if (!RemoteDirectory.IsEmpty() && (RemoteDirectory[1] == L'/'))
  3064. {
  3065. RemoteDirectory.Delete(1, 1);
  3066. }
  3067. Result = IncludeTrailingBackslash(Result + CopyParam.ValidLocalPath(FromUnixPath(RemoteDirectory)));
  3068. }
  3069. if (!ForceDirectories(ApiPath(Result)))
  3070. {
  3071. throw EOSExtException(FMTLOAD(CREATE_TEMP_DIR_ERROR, (Result)));
  3072. }
  3073. }
  3074. //---------------------------------------------------------------------------
  3075. void __fastcall TCustomScpExplorerForm::TemporarilyDownloadFiles(
  3076. TStrings * FileList, bool ForceText, UnicodeString & RootTempDir, UnicodeString & TempDir,
  3077. bool AllFiles, bool GetTargetNames, bool AutoOperation)
  3078. {
  3079. DebugAssert(!IsLocalBrowserMode());
  3080. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  3081. if (ForceText)
  3082. {
  3083. CopyParam.TransferMode = tmAscii;
  3084. }
  3085. TemporaryFileCopyParam(CopyParam);
  3086. if (AllFiles)
  3087. {
  3088. CopyParam.IncludeFileMask = TFileMasks();
  3089. }
  3090. if (TempDir.IsEmpty())
  3091. {
  3092. TemporaryDirectoryForRemoteFiles(FTerminal->CurrentDirectory, CopyParam, TempDir, RootTempDir);
  3093. }
  3094. DebugAssert(!FAutoOperation);
  3095. FAutoOperation = AutoOperation;
  3096. Terminal->ExceptionOnFail = true;
  3097. try
  3098. {
  3099. try
  3100. {
  3101. // turn off confirmations, as for MDI editors we may possibly download
  3102. // the same file over
  3103. Terminal->CopyToLocal(
  3104. FileList, TempDir, &CopyParam, cpNoConfirmation | cpTemporary, NULL);
  3105. if (GetTargetNames)
  3106. {
  3107. for (int i = 0; i < FileList->Count; i++)
  3108. {
  3109. FileList->Strings[i] =
  3110. Terminal->ChangeFileName(&CopyParam, UnixExtractFileName(FileList->Strings[i]), osRemote, false);
  3111. }
  3112. }
  3113. }
  3114. catch(...)
  3115. {
  3116. if (!RootTempDir.IsEmpty())
  3117. {
  3118. RecursiveDeleteFile(ExcludeTrailingBackslash(RootTempDir), false);
  3119. }
  3120. throw;
  3121. }
  3122. }
  3123. __finally
  3124. {
  3125. FAutoOperation = false;
  3126. Terminal->ExceptionOnFail = false;
  3127. }
  3128. }
  3129. //---------------------------------------------------------------------------
  3130. void __fastcall TCustomScpExplorerForm::EditorAutoConfig()
  3131. {
  3132. // Do not waste time checking for default editor list the next time,
  3133. // or testing if default editor is not notepad.
  3134. bool TryNextTime = false;
  3135. UnicodeString UsageState;
  3136. if (!WinConfiguration->EditorList->IsDefaultList())
  3137. {
  3138. UsageState = "H";
  3139. }
  3140. else
  3141. {
  3142. UnicodeString Executable;
  3143. UnicodeString ExecutableDescription;
  3144. if (DetectSystemExternalEditor(false, Executable, ExecutableDescription, UsageState, TryNextTime))
  3145. {
  3146. UnicodeString Message =
  3147. FMTLOAD(EDITOR_AUTO_CONFIG2, (ExecutableDescription, ExecutableDescription));
  3148. unsigned int Answer =
  3149. MessageDialog(Message, qtConfirmation, qaOK | qaCancel, HELP_EDITOR_AUTO_CONFIG);
  3150. if (Answer != qaOK)
  3151. {
  3152. UsageState = "R";
  3153. }
  3154. else
  3155. {
  3156. UsageState = "A";
  3157. TEditorData EditorData;
  3158. EditorData.Editor = edExternal;
  3159. EditorData.ExternalEditor = FormatCommand(Executable, L"");
  3160. EditorData.DecideExternalEditorText();
  3161. TEditorList EditorList;
  3162. EditorList = *WinConfiguration->EditorList;
  3163. EditorList.Insert(0, new TEditorPreferences(EditorData));
  3164. WinConfiguration->EditorList = &EditorList;
  3165. }
  3166. }
  3167. }
  3168. WinConfiguration->OfferedEditorAutoConfig = !TryNextTime;
  3169. WinConfiguration->Usage->Set(L"EditorAutoConfig", UsageState);
  3170. }
  3171. //---------------------------------------------------------------------------
  3172. void __fastcall TCustomScpExplorerForm::ExecuteFileNormalize(
  3173. TExecuteFileBy & ExecuteFileBy, const TEditorData *& ExternalEditor,
  3174. const UnicodeString & FileName, bool Local, const TFileMasks::TParams & MaskParams)
  3175. {
  3176. if (ExecuteFileBy == efDefaultEditor)
  3177. {
  3178. if (!WinConfiguration->OfferedEditorAutoConfig)
  3179. {
  3180. EditorAutoConfig();
  3181. }
  3182. const TEditorPreferences * Editor =
  3183. WinConfiguration->DefaultEditorForFile(FileName, Local, MaskParams);
  3184. if ((Editor == NULL) || (Editor->Data->Editor == edInternal))
  3185. {
  3186. ExecuteFileBy = efInternalEditor;
  3187. ExternalEditor = NULL;
  3188. }
  3189. else if (Editor->Data->Editor == edOpen)
  3190. {
  3191. ExecuteFileBy = efShell;
  3192. ExternalEditor = NULL;
  3193. }
  3194. else
  3195. {
  3196. ExecuteFileBy = efExternalEditor;
  3197. ExternalEditor = Editor->Data;
  3198. }
  3199. }
  3200. }
  3201. //---------------------------------------------------------------------------
  3202. void __fastcall TCustomScpExplorerForm::ExecuteFile(TOperationSide Side,
  3203. TExecuteFileBy ExecuteFileBy, const TEditorData * ExternalEditor,
  3204. UnicodeString FullFileName, TObject * Object, const TFileMasks::TParams & MaskParams)
  3205. {
  3206. UnicodeString OriginalFileName;
  3207. UnicodeString LocalRootDirectory;
  3208. UnicodeString RemoteDirectory;
  3209. ExecuteFileNormalize(ExecuteFileBy, ExternalEditor, FullFileName,
  3210. IsSideLocalBrowser(Side), MaskParams);
  3211. UnicodeString Counter;
  3212. UnicodeString LocalFileName;
  3213. bool Handled = false;
  3214. if (!IsSideLocalBrowser(Side))
  3215. {
  3216. // We need to trim VMS version here, so that we use name without version
  3217. // when uploading back to create a new version of the file
  3218. OriginalFileName = FTerminal->GetBaseFileName(UnixExtractFileName(FullFileName));
  3219. RemoteDirectory = UnixExtractFilePath(FullFileName);
  3220. TObject * Token = NULL;
  3221. UnicodeString LocalDirectory;
  3222. if (!FEditorManager->CanAddFile(RemoteDirectory, OriginalFileName,
  3223. Terminal->SessionData->SessionName, Token, LocalRootDirectory,
  3224. LocalDirectory))
  3225. {
  3226. if (Token != NULL)
  3227. {
  3228. TForm * Form = dynamic_cast<TForm *>(Token);
  3229. if (Form->WindowState == wsMinimized)
  3230. {
  3231. ShowWindow(Form->Handle, SW_RESTORE);
  3232. }
  3233. else
  3234. {
  3235. Form->SetFocus();
  3236. }
  3237. Handled = true;
  3238. }
  3239. else
  3240. {
  3241. throw Exception(FMTLOAD(ALREADY_EDITED_EXTERNALLY_OR_UPLOADED, (OriginalFileName)));
  3242. }
  3243. }
  3244. if (!Handled)
  3245. {
  3246. TStringList * FileList1 = new TStringList();
  3247. try
  3248. {
  3249. FileList1->AddObject(FullFileName, Object);
  3250. TemporarilyDownloadFiles(FileList1,
  3251. RemoteExecuteForceText(ExecuteFileBy, ExternalEditor),
  3252. LocalRootDirectory, LocalDirectory, true, true, true);
  3253. LocalFileName = LocalDirectory + FileList1->Strings[0];
  3254. }
  3255. __finally
  3256. {
  3257. delete FileList1;
  3258. }
  3259. switch (ExecuteFileBy)
  3260. {
  3261. case efShell:
  3262. Counter = "RemoteFilesExecuted";
  3263. break;
  3264. case efInternalEditor:
  3265. Counter = "RemoteFilesOpenedInInternalEditor";
  3266. break;
  3267. case efExternalEditor:
  3268. Counter = "RemoteFilesOpenedInExternalEditor";
  3269. break;
  3270. default:
  3271. DebugFail();
  3272. }
  3273. }
  3274. }
  3275. else
  3276. {
  3277. LocalFileName = FullFileName;
  3278. OriginalFileName = ExtractFileName(FullFileName);
  3279. switch (ExecuteFileBy)
  3280. {
  3281. case efShell:
  3282. Counter = "LocalFilesExecuted";
  3283. break;
  3284. case efInternalEditor:
  3285. Counter = "LocalFilesOpenedInInternalEditor";
  3286. break;
  3287. case efExternalEditor:
  3288. Counter = "LocalFilesOpenedInExternalEditor";
  3289. break;
  3290. default:
  3291. DebugFail();
  3292. }
  3293. }
  3294. if (!Handled)
  3295. {
  3296. Configuration->Usage->Inc(Counter);
  3297. CustomExecuteFile(Side, ExecuteFileBy, LocalFileName, OriginalFileName,
  3298. ExternalEditor, LocalRootDirectory, RemoteDirectory, false);
  3299. }
  3300. }
  3301. //---------------------------------------------------------------------------
  3302. void __fastcall TCustomScpExplorerForm::ExecuteFile(TOperationSide Side,
  3303. TExecuteFileBy ExecuteFileBy, const TEditorData * ExternalEditor,
  3304. bool AllSelected, bool OnFocused)
  3305. {
  3306. DebugAssert(!WinConfiguration->DisableOpenEdit);
  3307. DebugAssert((ExecuteFileBy == efExternalEditor) ==
  3308. ((ExternalEditor != NULL) && (ExternalEditor->Editor == edExternal)));
  3309. Side = GetSide(Side);
  3310. TCustomDirView * DView = DirView(Side);
  3311. TStrings * FileList =
  3312. AllSelected ?
  3313. DView->CreateFileList(OnFocused, IsSideLocalBrowser(Side)) :
  3314. DView->CreateFocusedFileList(IsSideLocalBrowser(Side));
  3315. try
  3316. {
  3317. DebugAssert(AllSelected || (FileList->Count == 1));
  3318. for (int i = 0; i < FileList->Count; i++)
  3319. {
  3320. UnicodeString ListFileName = FileList->Strings[i];
  3321. UnicodeString FileNameOnly =
  3322. !IsSideLocalBrowser(Side) ? UnixExtractFileName(ListFileName) : ExtractFileName(ListFileName);
  3323. TListItem * Item = DView->FindFileItem(FileNameOnly);
  3324. if (!DView->ItemIsDirectory(Item))
  3325. {
  3326. UnicodeString FullFileName;
  3327. if (!IsSideLocalBrowser(Side))
  3328. {
  3329. FullFileName = DirView(Side)->Path + ListFileName;
  3330. }
  3331. else
  3332. {
  3333. FullFileName = ListFileName;
  3334. }
  3335. TFileMasks::TParams MaskParams;
  3336. MaskParams.Size = DView->ItemFileSize(Item);
  3337. TDateTimePrecision Precision;
  3338. MaskParams.Modification = DView->ItemFileTime(Item, Precision);
  3339. ExecuteFile(Side, ExecuteFileBy, ExternalEditor, FullFileName,
  3340. FileList->Objects[i], MaskParams);
  3341. }
  3342. }
  3343. }
  3344. __finally
  3345. {
  3346. delete FileList;
  3347. }
  3348. }
  3349. //---------------------------------------------------------------------------
  3350. void __fastcall TCustomScpExplorerForm::TemporaryFileCopyParam(TCopyParamType & CopyParam)
  3351. {
  3352. // do not forget to add additional options to TemporarilyDownloadFiles, and AS
  3353. CopyParam.FileNameCase = ncNoChange;
  3354. CopyParam.PreserveRights = false;
  3355. CopyParam.PreserveReadOnly = false;
  3356. CopyParam.ReplaceInvalidChars = true;
  3357. CopyParam.IncludeFileMask = TFileMasks();
  3358. CopyParam.NewerOnly = false;
  3359. CopyParam.FileMask = L"";
  3360. }
  3361. //---------------------------------------------------------------------------
  3362. void __fastcall TCustomScpExplorerForm::ExecutedFileChanged(const UnicodeString FileName,
  3363. TEditedFileData * Data, HANDLE UploadCompleteEvent)
  3364. {
  3365. TTerminalManager * Manager = TTerminalManager::Instance();
  3366. if ((Data->Terminal == NULL) || !Data->Terminal->Active)
  3367. {
  3368. if (!NonVisualDataModule->Busy)
  3369. {
  3370. UnicodeString FileNameOnly = ExtractFileName(FileName);
  3371. UnicodeString EditInactiveSessionReopenAcceptedCounter = L"EditInactiveSessionReopenAccepted";
  3372. UnicodeString EditInactiveSessionReopenRejectedCounter = L"EditInactiveSessionReopenRejected";
  3373. if (Data->Terminal == NULL)
  3374. {
  3375. TTerminal * SameSiteTerminal = Manager->FindActiveTerminalForSite(Data->SessionData);
  3376. if (SameSiteTerminal != NULL)
  3377. {
  3378. UnicodeString Message =
  3379. FMTLOAD(EDIT_SESSION_REATTACH,
  3380. (FileNameOnly, Data->SessionName, FileNameOnly));
  3381. if (MessageDialog(Message, qtConfirmation, qaOK | qaCancel) == qaOK)
  3382. {
  3383. Data->Terminal = Terminal;
  3384. Data->Queue = Manager->FindQueueForTerminal(Terminal);
  3385. Data->SessionName = Terminal->SessionData->SessionName;
  3386. // We might also overwrite session data
  3387. Configuration->Usage->Inc(EditInactiveSessionReopenAcceptedCounter);
  3388. }
  3389. else
  3390. {
  3391. Configuration->Usage->Inc(EditInactiveSessionReopenRejectedCounter);
  3392. Abort();
  3393. }
  3394. }
  3395. }
  3396. // foreground session should reconnect itself
  3397. else if (Terminal != Data->Terminal)
  3398. {
  3399. UnicodeString Message =
  3400. MainInstructions(
  3401. FMTLOAD(EDIT_SESSION_RECONNECT, (Data->SessionName, FileNameOnly)));
  3402. if (MessageDialog(Message, qtConfirmation, qaOK | qaCancel) == qaOK)
  3403. {
  3404. Manager->SetActiveTerminalWithAutoReconnect(Data->Terminal);
  3405. Configuration->Usage->Inc(EditInactiveSessionReopenAcceptedCounter);
  3406. }
  3407. else
  3408. {
  3409. Configuration->Usage->Inc(EditInactiveSessionReopenRejectedCounter);
  3410. Abort();
  3411. }
  3412. }
  3413. }
  3414. if ((Data->Terminal == NULL) || !Data->Terminal->Active)
  3415. {
  3416. Configuration->Usage->Inc(L"EditInactiveSession");
  3417. // Prevent this when not idle (!NonVisualDataModule->Busy)?
  3418. throw Exception(FMTLOAD(EDIT_SESSION_CLOSED2,
  3419. (ExtractFileName(FileName), Data->SessionName)));
  3420. }
  3421. }
  3422. TStrings * FileList = new TStringList();
  3423. try
  3424. {
  3425. FileList->Add(FileName);
  3426. // consider using the same settings (preset) as when the file was downloaded
  3427. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  3428. TemporaryFileCopyParam(CopyParam);
  3429. if (Data->ForceText)
  3430. {
  3431. CopyParam.TransferMode = tmAscii;
  3432. }
  3433. // so i do not need to worry if masking algorithm works in all cases
  3434. // ("" means "copy file name", no masking is actually done)
  3435. if (ExtractFileName(FileName) == Data->OriginalFileName)
  3436. {
  3437. CopyParam.FileMask = L"";
  3438. }
  3439. else
  3440. {
  3441. CopyParam.FileMask = DelimitFileNameMask(Data->OriginalFileName);
  3442. }
  3443. DebugAssert(Data->Queue != NULL);
  3444. int Params = cpNoConfirmation | cpTemporary;
  3445. TQueueItem * QueueItem = new TUploadQueueItem(Data->Terminal, FileList,
  3446. Data->RemoteDirectory, &CopyParam, Params, true, false);
  3447. QueueItem->CompleteEvent = UploadCompleteEvent;
  3448. AddQueueItem(Data->Queue, QueueItem, Data->Terminal);
  3449. }
  3450. __finally
  3451. {
  3452. delete FileList;
  3453. }
  3454. }
  3455. //---------------------------------------------------------------------------
  3456. void __fastcall TCustomScpExplorerForm::ExecutedFileReload(
  3457. const UnicodeString FileName, const TEditedFileData * Data)
  3458. {
  3459. // Sanity check, we should not be busy otherwise user would not be able to click Reload button.
  3460. DebugAssert(!NonVisualDataModule->Busy);
  3461. if ((Data->Terminal == NULL) || !Data->Terminal->Active)
  3462. {
  3463. throw Exception(FMTLOAD(EDIT_SESSION_CLOSED_RELOAD,
  3464. (ExtractFileName(FileName), Data->SessionName)));
  3465. }
  3466. TManagedTerminal * PrevTerminal = TTerminalManager::Instance()->ActiveTerminal;
  3467. TTerminalManager::Instance()->ActiveTerminal = Data->Terminal;
  3468. NonVisualDataModule->StartBusy();
  3469. try
  3470. {
  3471. std::unique_ptr<TRemoteFile> File;
  3472. UnicodeString RemoteFileName =
  3473. UnixIncludeTrailingBackslash(Data->RemoteDirectory) + Data->OriginalFileName;
  3474. FTerminal->ExceptionOnFail = true;
  3475. try
  3476. {
  3477. TRemoteFile * AFile = NULL;
  3478. FTerminal->ReadFile(RemoteFileName, AFile);
  3479. File.reset(AFile);
  3480. if (!File->HaveFullFileName)
  3481. {
  3482. File->FullFileName = RemoteFileName;
  3483. }
  3484. }
  3485. __finally
  3486. {
  3487. FTerminal->ExceptionOnFail = false;
  3488. }
  3489. std::unique_ptr<TStrings> FileList(new TStringList());
  3490. FileList->AddObject(RemoteFileName, File.get());
  3491. UnicodeString RootTempDir = Data->LocalRootDirectory;
  3492. UnicodeString TempDir = ExtractFilePath(FileName);
  3493. TemporarilyDownloadFiles(FileList.get(), Data->ForceText, RootTempDir,
  3494. TempDir, true, true, true);
  3495. // sanity check, the target file name should be still the same
  3496. DebugAssert(ExtractFileName(FileName) == FileList->Strings[0]);
  3497. }
  3498. __finally
  3499. {
  3500. NonVisualDataModule->EndBusy();
  3501. // it actually may not exist anymore...
  3502. TTerminalManager::Instance()->ActiveTerminal = PrevTerminal;
  3503. }
  3504. }
  3505. //---------------------------------------------------------------------------
  3506. void __fastcall TCustomScpExplorerForm::ExecutedFileEarlyClosed(
  3507. const TEditedFileData * Data, bool & KeepOpen)
  3508. {
  3509. // Command is set for external editors only (not for "shell" open).
  3510. if (!Data->Command.IsEmpty())
  3511. {
  3512. bool AnyFound = false;
  3513. bool AnyMDI = false;
  3514. bool AnyNonMDI = false;
  3515. bool AnyDetect = false;
  3516. TEditorList * EditorList = new TEditorList();
  3517. try
  3518. {
  3519. *EditorList = *WinConfiguration->EditorList;
  3520. for (int i = 0; i < EditorList->Count; i++)
  3521. {
  3522. const TEditorPreferences * Editor = EditorList->Editors[i];
  3523. if ((Editor->Data->Editor == edExternal) &&
  3524. (Editor->Data->ExternalEditor == Data->Command))
  3525. {
  3526. AnyFound = true;
  3527. if (Editor->Data->SDIExternalEditor)
  3528. {
  3529. AnyNonMDI = true;
  3530. if (Editor->Data->DetectMDIExternalEditor)
  3531. {
  3532. AnyDetect = true;
  3533. }
  3534. }
  3535. else
  3536. {
  3537. AnyMDI = true;
  3538. }
  3539. }
  3540. }
  3541. bool EnableMDI = false;
  3542. bool DisableDetect = false;
  3543. if (AnyMDI)
  3544. {
  3545. KeepOpen = true;
  3546. if (AnyNonMDI)
  3547. {
  3548. // there is at least one instance of the editor with MDI support enabled,
  3549. // and one with disabled, enable it for all instances
  3550. EnableMDI = true;
  3551. }
  3552. }
  3553. else if (AnyFound && !AnyDetect)
  3554. {
  3555. // at least once instance found but all have MDI autodetection disabled
  3556. // => close the file (default action)
  3557. }
  3558. else
  3559. {
  3560. // no instance of the editor has MDI support enabled
  3561. TMessageParams Params;
  3562. if (AnyFound)
  3563. {
  3564. // there is at least one instance of the editor
  3565. Params.Params |= mpNeverAskAgainCheck;
  3566. }
  3567. unsigned int Answer = MessageDialog(FMTLOAD(EDITOR_EARLY_CLOSED2, (Data->OriginalFileName)), qtWarning,
  3568. qaYes | qaNo, HELP_EDITOR_EARLY_CLOSED, &Params);
  3569. switch (Answer)
  3570. {
  3571. case qaNeverAskAgain:
  3572. DisableDetect = true;
  3573. break;
  3574. case qaNo:
  3575. EnableMDI = true;
  3576. KeepOpen = true;
  3577. break;
  3578. }
  3579. }
  3580. if (AnyFound && (EnableMDI || DisableDetect))
  3581. {
  3582. bool Changed = false;
  3583. for (int i = 0; i < EditorList->Count; i++)
  3584. {
  3585. const TEditorPreferences * Editor = EditorList->Editors[i];
  3586. if ((Editor->Data->Editor == edExternal) &&
  3587. (Editor->Data->ExternalEditor == Data->Command) &&
  3588. ((EnableMDI && Editor->Data->SDIExternalEditor) ||
  3589. (DisableDetect && Editor->Data->DetectMDIExternalEditor)))
  3590. {
  3591. Changed = true;
  3592. TEditorPreferences * UpdatedEditor = new TEditorPreferences(*Editor);
  3593. if (EnableMDI)
  3594. {
  3595. UpdatedEditor->GetData()->SDIExternalEditor = false;
  3596. }
  3597. if (DisableDetect)
  3598. {
  3599. UpdatedEditor->GetData()->DetectMDIExternalEditor = false;
  3600. }
  3601. EditorList->Change(i, UpdatedEditor);
  3602. }
  3603. }
  3604. if (Changed)
  3605. {
  3606. WinConfiguration->EditorList = EditorList;
  3607. }
  3608. }
  3609. }
  3610. __finally
  3611. {
  3612. delete EditorList;
  3613. }
  3614. }
  3615. else
  3616. {
  3617. // "open" case
  3618. MessageDialog(FMTLOAD(APP_EARLY_CLOSED, (Data->OriginalFileName)), qtWarning,
  3619. qaOK, HELP_APP_EARLY_CLOSED);
  3620. }
  3621. }
  3622. //---------------------------------------------------------------------------
  3623. void __fastcall TCustomScpExplorerForm::ExecutedFileUploadComplete(TObject * Sender)
  3624. {
  3625. EditorFormFileUploadComplete(DebugNotNull(dynamic_cast<TForm *>(Sender)));
  3626. }
  3627. //---------------------------------------------------------------------------
  3628. void __fastcall TCustomScpExplorerForm::RemoteDirViewEnter(TObject * /*Sender*/)
  3629. {
  3630. SideEnter(osRemote);
  3631. }
  3632. //---------------------------------------------------------------------------
  3633. void __fastcall TCustomScpExplorerForm::RemoteDriveViewEnter(TObject * /*Sender*/)
  3634. {
  3635. MakeNextInTabOrder(RemoteDirPanel, RemoteDrivePanel);
  3636. SideEnter(osRemote);
  3637. }
  3638. //---------------------------------------------------------------------------
  3639. void __fastcall TCustomScpExplorerForm::SideEnter(TOperationSide Side)
  3640. {
  3641. FCurrentSide = Side;
  3642. if (Visible)
  3643. {
  3644. UpdateControls();
  3645. }
  3646. }
  3647. //---------------------------------------------------------------------------
  3648. void __fastcall TCustomScpExplorerForm::DeleteFiles(TOperationSide Side,
  3649. TStrings * FileList, bool Alternative)
  3650. {
  3651. TCustomDirView * DView = DirView(Side);
  3652. DView->SaveSelection();
  3653. DView->SaveSelectedNames();
  3654. DebugAssert(!FAlternativeDelete);
  3655. FAlternativeDelete = Alternative;
  3656. try
  3657. {
  3658. if (!IsSideLocalBrowser(Side))
  3659. {
  3660. DebugAssert(Terminal != NULL);
  3661. Terminal->DeleteFiles(FileList, FLAGMASK(Alternative, dfAlternative));
  3662. }
  3663. else
  3664. {
  3665. try
  3666. {
  3667. TTerminalManager::Instance()->LocalTerminal->DeleteLocalFiles(FileList, FLAGMASK(Alternative, dfAlternative));
  3668. }
  3669. __finally
  3670. {
  3671. ReloadLocalDirectory();
  3672. }
  3673. }
  3674. FAlternativeDelete = false;
  3675. }
  3676. catch(...)
  3677. {
  3678. FAlternativeDelete = false;
  3679. DView->DiscardSavedSelection();
  3680. throw;
  3681. }
  3682. DView->RestoreSelection();
  3683. }
  3684. //---------------------------------------------------------------------------
  3685. void __fastcall TCustomScpExplorerForm::LockFiles(TStrings * FileList, bool Lock)
  3686. {
  3687. DebugAssert(Terminal);
  3688. DebugAssert(!IsLocalBrowserMode());
  3689. RemoteDirView->SaveSelection();
  3690. RemoteDirView->SaveSelectedNames();
  3691. try
  3692. {
  3693. if (Lock)
  3694. {
  3695. Terminal->LockFiles(FileList);
  3696. }
  3697. else
  3698. {
  3699. Terminal->UnlockFiles(FileList);
  3700. }
  3701. }
  3702. catch(...)
  3703. {
  3704. RemoteDirView->DiscardSavedSelection();
  3705. throw;
  3706. }
  3707. RemoteDirView->RestoreSelection();
  3708. }
  3709. //---------------------------------------------------------------------------
  3710. bool __fastcall TCustomScpExplorerForm::RemoteTransferDialog(TManagedTerminal *& Session,
  3711. TStrings * FileList, UnicodeString & Target, UnicodeString & FileMask, bool & DirectCopy,
  3712. bool NoConfirmation, bool Move)
  3713. {
  3714. DebugAssert(Terminal != NULL);
  3715. DebugAssert(!IsLocalBrowserMode());
  3716. // update Terminal->StateData->RemoteDirectory
  3717. UpdateTerminal(Terminal);
  3718. if (Session == NULL)
  3719. {
  3720. Session = TTerminalManager::Instance()->ActiveTerminal;
  3721. }
  3722. if (Session == TTerminalManager::Instance()->ActiveTerminal)
  3723. {
  3724. if (RemoteDriveView->DropTarget != NULL)
  3725. {
  3726. Target = RemoteDriveView->NodePathName(RemoteDriveView->DropTarget);
  3727. }
  3728. else if (RemoteDirView->DropTarget != NULL)
  3729. {
  3730. DebugAssert(RemoteDirView->ItemIsDirectory(RemoteDirView->DropTarget));
  3731. Target = RemoteDirView->ItemFullFileName(RemoteDirView->DropTarget);
  3732. }
  3733. else
  3734. {
  3735. Target = RemoteDirView->Path;
  3736. }
  3737. }
  3738. else
  3739. {
  3740. Target = Session->StateData->RemoteDirectory;
  3741. }
  3742. Target = UnixIncludeTrailingBackslash(Target);
  3743. if (FileList->Count == 1)
  3744. {
  3745. FileMask = DelimitFileNameMask(UnixExtractFileName(FileList->Strings[0]));
  3746. }
  3747. else
  3748. {
  3749. FileMask = AnyMask;
  3750. }
  3751. DirectCopy = FTerminal->IsCapable[fcRemoteCopy] || FTerminal->IsCapable[fcSecondaryShell];
  3752. bool Result = true;
  3753. if (!NoConfirmation)
  3754. {
  3755. bool Multi = (FileList->Count > 1);
  3756. if (Move)
  3757. {
  3758. Result = DoRemoteMoveDialog(Multi, Target, FileMask);
  3759. }
  3760. else
  3761. {
  3762. TStrings * Sessions = TTerminalManager::Instance()->TerminalList;
  3763. TStrings * Directories = new TStringList;
  3764. try
  3765. {
  3766. for (int Index = 0; Index < Sessions->Count; Index++)
  3767. {
  3768. TManagedTerminal * Terminal =
  3769. dynamic_cast<TManagedTerminal *>(Sessions->Objects[Index]);
  3770. Directories->Add(Terminal->StateData->RemoteDirectory);
  3771. }
  3772. TDirectRemoteCopy AllowDirectCopy;
  3773. if (FTerminal->IsCapable[fcRemoteCopy] || FTerminal->CommandSessionOpened)
  3774. {
  3775. DebugAssert(DirectCopy);
  3776. AllowDirectCopy = drcAllow;
  3777. }
  3778. else if (FTerminal->IsCapable[fcSecondaryShell])
  3779. {
  3780. DebugAssert(DirectCopy);
  3781. AllowDirectCopy = drcConfirmCommandSession;
  3782. }
  3783. else
  3784. {
  3785. DebugAssert(!DirectCopy);
  3786. AllowDirectCopy = drcDisallow;
  3787. }
  3788. void * ASession = Session;
  3789. Result = DoRemoteCopyDialog(Sessions, Directories, AllowDirectCopy,
  3790. Multi, ASession, Target, FileMask, DirectCopy, TTerminalManager::Instance()->ActiveTerminal);
  3791. Session = static_cast<TManagedTerminal *>(ASession);
  3792. }
  3793. __finally
  3794. {
  3795. delete Directories;
  3796. }
  3797. }
  3798. }
  3799. return Result;
  3800. }
  3801. //---------------------------------------------------------------------------
  3802. bool __fastcall TCustomScpExplorerForm::RemoteTransferFiles(
  3803. TStrings * FileList, bool NoConfirmation, bool Move, TManagedTerminal * Session)
  3804. {
  3805. DebugAssert(!IsLocalBrowserMode());
  3806. bool DirectCopy;
  3807. UnicodeString Target, FileMask;
  3808. bool Result = RemoteTransferDialog(Session, FileList, Target, FileMask, DirectCopy, NoConfirmation, Move);
  3809. if (Result)
  3810. {
  3811. if (!Move && !DirectCopy)
  3812. {
  3813. Configuration->Usage->Inc("RemoteCopyTemp");
  3814. UnicodeString RootTempDir;
  3815. UnicodeString TempDir;
  3816. TemporarilyDownloadFiles(FileList, false, RootTempDir, TempDir, false, false, false);
  3817. TStrings * TemporaryFilesList = new TStringList();
  3818. try
  3819. {
  3820. TMakeLocalFileListParams MakeFileListParam;
  3821. MakeFileListParam.FileList = TemporaryFilesList;
  3822. MakeFileListParam.FileTimes = NULL;
  3823. MakeFileListParam.IncludeDirs = true;
  3824. MakeFileListParam.Recursive = false;
  3825. ProcessLocalDirectory(TempDir, FTerminal->MakeLocalFileList, &MakeFileListParam);
  3826. TTerminalManager::Instance()->ActiveTerminal = Session;
  3827. if (TemporaryFilesList->Count > 0)
  3828. {
  3829. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  3830. CopyParam.FileMask = FileMask;
  3831. DebugAssert(!FAutoOperation);
  3832. FAutoOperation = true;
  3833. FTerminal->CopyToRemote(TemporaryFilesList, Target, &CopyParam, cpTemporary, NULL);
  3834. }
  3835. }
  3836. __finally
  3837. {
  3838. delete TemporaryFilesList;
  3839. FAutoOperation = false;
  3840. if (!RootTempDir.IsEmpty())
  3841. {
  3842. RecursiveDeleteFile(ExcludeTrailingBackslash(RootTempDir), false);
  3843. }
  3844. }
  3845. }
  3846. else
  3847. {
  3848. RemoteDirView->SaveSelection();
  3849. RemoteDirView->SaveSelectedNames();
  3850. try
  3851. {
  3852. if (Move)
  3853. {
  3854. Configuration->Usage->Inc("RemoteMove");
  3855. Terminal->MoveFiles(FileList, Target, FileMask);
  3856. }
  3857. else
  3858. {
  3859. Configuration->Usage->Inc("RemoteCopyDirect");
  3860. DebugAssert(DirectCopy);
  3861. DebugAssert(Session == FTerminal);
  3862. if (FTerminal->IsCapable[fcRemoteCopy] ||
  3863. FTerminal->CommandSessionOpened ||
  3864. CommandSessionFallback())
  3865. {
  3866. Terminal->CopyFiles(FileList, Target, FileMask);
  3867. }
  3868. }
  3869. }
  3870. catch(...)
  3871. {
  3872. RemoteDirView->DiscardSavedSelection();
  3873. throw;
  3874. }
  3875. RemoteDirView->RestoreSelection();
  3876. }
  3877. }
  3878. return Result;
  3879. }
  3880. //---------------------------------------------------------------------------
  3881. void __fastcall TCustomScpExplorerForm::CreateRemoteDirectory(
  3882. const UnicodeString & Path, TRemoteProperties & Properties)
  3883. {
  3884. DebugAssert(!IsLocalBrowserMode());
  3885. Properties.Valid = Properties.Valid << vpEncrypt;
  3886. Properties.Encrypt = GUIConfiguration->CurrentCopyParam.EncryptNewFiles;
  3887. RemoteDirView->CreateDirectoryEx(Path, &Properties);
  3888. }
  3889. //---------------------------------------------------------------------------
  3890. void __fastcall TCustomScpExplorerForm::CreateDirectory(TOperationSide Side)
  3891. {
  3892. Side = GetSide(Side);
  3893. TRemoteProperties Properties = GUIConfiguration->NewDirectoryProperties;
  3894. TRemoteProperties * AProperties = (!IsSideLocalBrowser(Side) ? &Properties : NULL);
  3895. UnicodeString Name = LoadStr(NEW_FOLDER);
  3896. int AllowedChanges =
  3897. FLAGMASK(!IsSideLocalBrowser(Side) && Terminal->IsCapable[fcModeChanging], cpMode);
  3898. bool SaveSettings = false;
  3899. if (DoCreateDirectoryDialog(Name, AProperties, AllowedChanges, SaveSettings))
  3900. {
  3901. TWindowLock Lock(this);
  3902. if (!IsSideLocalBrowser(Side))
  3903. {
  3904. if (SaveSettings)
  3905. {
  3906. GUIConfiguration->NewDirectoryProperties = Properties;
  3907. }
  3908. CreateRemoteDirectory(Name, Properties);
  3909. }
  3910. else
  3911. {
  3912. DirView(Side)->CreateDirectory(Name);
  3913. }
  3914. }
  3915. }
  3916. //---------------------------------------------------------------------------
  3917. void __fastcall TCustomScpExplorerForm::HomeDirectory(TOperationSide Side)
  3918. {
  3919. TWindowLock Lock(this);
  3920. DirView(Side)->ExecuteHomeDirectory();
  3921. }
  3922. //---------------------------------------------------------------------------
  3923. void __fastcall TCustomScpExplorerForm::OpenDirectory(TOperationSide Side)
  3924. {
  3925. DoOpenDirectoryDialog(odBrowse, Side);
  3926. }
  3927. //---------------------------------------------------------------------------
  3928. bool __fastcall TCustomScpExplorerForm::OpenBookmark(TOperationSide Side, TBookmark * Bookmark)
  3929. {
  3930. UnicodeString Path = Bookmark->GetSideDirectory(Side);
  3931. bool Result = !Path.IsEmpty();
  3932. if (Result)
  3933. {
  3934. // While we might get here when the session is closed (from location profiles),
  3935. // it's not a problem as the Path setter is noop then
  3936. DirView(Side)->Path = Path;
  3937. }
  3938. return Result;
  3939. }
  3940. //---------------------------------------------------------------------------
  3941. void __fastcall TCustomScpExplorerForm::CalculateSize(
  3942. TStrings * FileList, __int64 & Size, TCalculateSizeStats & Stats,
  3943. bool & Close)
  3944. {
  3945. // terminal can be already closed (e.g. dropped connection)
  3946. if (Terminal != NULL)
  3947. {
  3948. try
  3949. {
  3950. Terminal->CalculateFilesSize(FileList, Size, 0, NULL, true, Stats);
  3951. }
  3952. catch(...)
  3953. {
  3954. if (!Terminal->Active)
  3955. {
  3956. Close = true;
  3957. }
  3958. throw;
  3959. }
  3960. }
  3961. }
  3962. //---------------------------------------------------------------------------
  3963. void __fastcall TCustomScpExplorerForm::CalculateChecksum(const UnicodeString & Alg,
  3964. TStrings * FileList, TCalculatedChecksumEvent OnCalculatedChecksum,
  3965. bool & Close)
  3966. {
  3967. // terminal can be already closed (e.g. dropped connection)
  3968. if (Terminal != NULL)
  3969. {
  3970. Configuration->Usage->Inc(L"ChecksumCalculated");
  3971. try
  3972. {
  3973. Terminal->CalculateFilesChecksum(Alg, FileList, NULL, OnCalculatedChecksum);
  3974. }
  3975. catch(...)
  3976. {
  3977. if (!Terminal->Active)
  3978. {
  3979. Close = true;
  3980. }
  3981. throw;
  3982. }
  3983. }
  3984. }
  3985. //---------------------------------------------------------------------------
  3986. bool __fastcall TCustomScpExplorerForm::SetProperties(TOperationSide Side, TStrings * FileList)
  3987. {
  3988. bool Result;
  3989. if (!IsSideLocalBrowser(Side))
  3990. {
  3991. TRemoteTokenList * GroupList = NULL;
  3992. TRemoteTokenList * UserList = NULL;
  3993. try
  3994. {
  3995. TRemoteProperties CurrentProperties;
  3996. if (Terminal->LoadFilesProperties(FileList))
  3997. {
  3998. RemoteDirView->Invalidate();
  3999. }
  4000. bool CapableGroupChanging = Terminal->IsCapable[fcGroupChanging];
  4001. bool CapableOwnerChanging = Terminal->IsCapable[fcOwnerChanging];
  4002. if (CapableGroupChanging || CapableOwnerChanging)
  4003. {
  4004. if (CapableGroupChanging)
  4005. {
  4006. GroupList = Terminal->Groups->Duplicate();
  4007. }
  4008. if (CapableOwnerChanging)
  4009. {
  4010. UserList = Terminal->Users->Duplicate();
  4011. }
  4012. TRemoteDirectory * Files = Terminal->Files;
  4013. int Count = Files->Count;
  4014. if (Count > 100)
  4015. {
  4016. Count = 100;
  4017. }
  4018. for (int Index = 0; Index < Count; Index++)
  4019. {
  4020. TRemoteFile * File = Files->Files[Index];
  4021. if (CapableGroupChanging)
  4022. {
  4023. GroupList->AddUnique(File->Group);
  4024. }
  4025. if (CapableOwnerChanging)
  4026. {
  4027. UserList->AddUnique(File->Owner);
  4028. }
  4029. }
  4030. // if we haven't collected tokens for all files in current directory,
  4031. // make sure we collect them at least for all selected files.
  4032. // (note that so far the files in FileList has to be from current direcotry)
  4033. if (Count < Files->Count)
  4034. {
  4035. for (int Index = 0; Index < FileList->Count; Index++)
  4036. {
  4037. TRemoteFile * File = (TRemoteFile *)(FileList->Objects[Index]);
  4038. if (CapableGroupChanging)
  4039. {
  4040. GroupList->AddUnique(File->Group);
  4041. }
  4042. if (CapableOwnerChanging)
  4043. {
  4044. UserList->AddUnique(File->Owner);
  4045. }
  4046. }
  4047. }
  4048. }
  4049. CurrentProperties = TRemoteProperties::CommonProperties(FileList);
  4050. int Flags = 0;
  4051. if (Terminal->IsCapable[fcModeChanging]) Flags |= cpMode;
  4052. if (CapableOwnerChanging) Flags |= cpOwner;
  4053. if (CapableGroupChanging) Flags |= cpGroup;
  4054. TCalculateChecksumEvent CalculateChecksumEvent = NULL;
  4055. if (Terminal->IsCapable[fcCalculatingChecksum])
  4056. {
  4057. CalculateChecksumEvent = CalculateChecksum;
  4058. }
  4059. std::unique_ptr<TStrings> ChecksumAlgs(new TStringList());
  4060. Terminal->GetSupportedChecksumAlgs(ChecksumAlgs.get());
  4061. TRemoteProperties NewProperties = CurrentProperties;
  4062. Result =
  4063. DoPropertiesDialog(FileList, RemoteDirView->PathName,
  4064. GroupList, UserList, ChecksumAlgs.get(), &NewProperties, Flags,
  4065. Terminal->IsCapable[fcGroupOwnerChangingByID],
  4066. CalculateSize, CalculateChecksumEvent);
  4067. if (Result)
  4068. {
  4069. NewProperties = TRemoteProperties::ChangedProperties(CurrentProperties, NewProperties);
  4070. Terminal->ChangeFilesProperties(FileList, &NewProperties);
  4071. }
  4072. }
  4073. __finally
  4074. {
  4075. delete GroupList;
  4076. delete UserList;
  4077. }
  4078. }
  4079. else
  4080. {
  4081. DirView(Side)->DisplayPropertiesMenu();
  4082. Result = true;
  4083. }
  4084. return Result;
  4085. }
  4086. //---------------------------------------------------------------------------
  4087. void __fastcall TCustomScpExplorerForm::KeyProcessed(Word & Key, TShiftState Shift)
  4088. {
  4089. if (Shift * AllKeyShiftStates() == (TShiftState() << ssAlt))
  4090. {
  4091. FIgnoreNextDialogChar = Key;
  4092. }
  4093. Key = 0;
  4094. }
  4095. //---------------------------------------------------------------------------
  4096. void __fastcall TCustomScpExplorerForm::CheckCustomCommandShortCut(
  4097. TCustomCommandList * List, Word & Key, Classes::TShiftState Shift, TShortCut KeyShortCut)
  4098. {
  4099. const TCustomCommandType * Command = List->Find(KeyShortCut);
  4100. if (Command != NULL)
  4101. {
  4102. KeyProcessed(Key, Shift);
  4103. if (CustomCommandState(*Command, false, ccltAll) > 0)
  4104. {
  4105. TAutoFlag DontCopyCommandToClipboardFlag(DontCopyCommandToClipboard);
  4106. ExecuteFileOperationCommand(foCustomCommand, osRemote,
  4107. false, false, const_cast<TCustomCommandType *>(Command));
  4108. }
  4109. }
  4110. }
  4111. //---------------------------------------------------------------------------
  4112. void __fastcall TCustomScpExplorerForm::KeyDown(Word & Key, Classes::TShiftState Shift)
  4113. {
  4114. if (QueueView3->Focused() && (QueueView3->OnKeyDown != NULL))
  4115. {
  4116. QueueView3->OnKeyDown(QueueView3, Key, Shift);
  4117. }
  4118. if (!DirView(osCurrent)->IsEditing())
  4119. {
  4120. TShortCut KeyShortCut = ShortCut(Key, Shift);
  4121. for (int Index = 0; Index < NonVisualDataModule->ExplorerActions->ActionCount; Index++)
  4122. {
  4123. TAction * Action = (TAction *)NonVisualDataModule->ExplorerActions->Actions[Index];
  4124. if (((Action->ShortCut == KeyShortCut) ||
  4125. (Action->SecondaryShortCuts->IndexOfShortCut(KeyShortCut) >= 0)) &&
  4126. AllowedAction(Action, aaShortCut))
  4127. {
  4128. // Has to be called before the action as the dialog char is already in queue.
  4129. // So when the action consumes message queue, we already need to have the
  4130. // FIgnoreNextDialogChar set
  4131. KeyProcessed(Key, Shift);
  4132. // Reset reference to previous component (when menu/toolbar was clicked).
  4133. // Needed to detect that action was invoked by keyboard shortcut
  4134. // in TNonVisualDataModule::ExplorerActionsExecute
  4135. Action->ActionComponent = NULL;
  4136. Action->Execute();
  4137. return;
  4138. }
  4139. }
  4140. for (int i = 0; i < TTerminalManager::Instance()->Count; i++)
  4141. {
  4142. if (NonVisualDataModule->OpenSessionShortCut(i) == KeyShortCut)
  4143. {
  4144. KeyProcessed(Key, Shift);
  4145. TTerminalManager::Instance()->ActiveTerminalIndex = i;
  4146. return;
  4147. }
  4148. }
  4149. if (Key == VK_TAB && Shift.Contains(ssCtrl))
  4150. {
  4151. KeyProcessed(Key, Shift);
  4152. TTerminalManager::Instance()->CycleTerminals(!Shift.Contains(ssShift));
  4153. }
  4154. TShortCut CustomShortCut = NormalizeCustomShortCut(KeyShortCut);
  4155. if (IsCustomShortCut(CustomShortCut))
  4156. {
  4157. CheckCustomCommandShortCut(WinConfiguration->CustomCommandList, Key, Shift, CustomShortCut);
  4158. CheckCustomCommandShortCut(WinConfiguration->ExtensionList, Key, Shift, CustomShortCut);
  4159. if (WinConfiguration->SharedBookmarks != NULL)
  4160. {
  4161. TBookmark * Bookmark = WinConfiguration->SharedBookmarks->FindByShortCut(CustomShortCut);
  4162. if ((Bookmark != NULL) &&
  4163. OpenBookmark(FCurrentSide, Bookmark))
  4164. {
  4165. KeyProcessed(Key, Shift);
  4166. }
  4167. }
  4168. }
  4169. }
  4170. TForm::KeyDown(Key, Shift);
  4171. }
  4172. //---------------------------------------------------------------------------
  4173. void __fastcall TCustomScpExplorerForm::InitStatusBar()
  4174. {
  4175. DebugAssert(Terminal != NULL);
  4176. const TSessionInfo & SessionInfo = Terminal->GetSessionInfo();
  4177. const TFileSystemInfo & FileSystemInfo = Terminal->GetFileSystemInfo();
  4178. TTBXStatusBar * SessionStatusBar = (TTBXStatusBar *)GetComponent(fcStatusBar);
  4179. int Offset = SessionStatusBar->Panels->Count - SessionPanelCount;
  4180. bool SecurityEnabled = !SessionInfo.SecurityProtocolName.IsEmpty();
  4181. SessionStatusBar->Panels->Items[Offset + 0]->Enabled = SecurityEnabled;
  4182. // expanded from ?: to avoid memory leaks
  4183. if (SecurityEnabled)
  4184. {
  4185. SessionStatusBar->Panels->Items[Offset + 0]->Hint =
  4186. FMTLOAD(STATUS_SECURE, (SessionInfo.SecurityProtocolName));
  4187. }
  4188. else
  4189. {
  4190. SessionStatusBar->Panels->Items[Offset + 0]->Hint = LoadStr(STATUS_INSECURE);
  4191. }
  4192. if (FileSystemInfo.ProtocolName.IsEmpty())
  4193. {
  4194. SessionStatusBar->Panels->Items[Offset + 1]->Caption = SessionInfo.ProtocolName;
  4195. }
  4196. else
  4197. {
  4198. SessionStatusBar->Panels->Items[Offset + 1]->Caption = FileSystemInfo.ProtocolName;
  4199. }
  4200. SessionStatusBar->Panels->Items[Offset + 1]->Hint = LoadStr(STATUS_PROTOCOL_HINT);
  4201. SessionStatusBar->Panels->Items[Offset + 2]->Enabled =
  4202. (!SessionInfo.CSCompression.IsEmpty() || !SessionInfo.SCCompression.IsEmpty());
  4203. UnicodeString CSCompressionStr = DefaultStr(SessionInfo.CSCompression, LoadStr(NO_STR));
  4204. UnicodeString SCCompressionStr = DefaultStr(SessionInfo.SCCompression, LoadStr(NO_STR));
  4205. UnicodeString CompressionStr = CSCompressionStr;
  4206. if (CSCompressionStr != SCCompressionStr)
  4207. {
  4208. CompressionStr += UnicodeString(L"/") + SCCompressionStr;
  4209. }
  4210. SessionStatusBar->Panels->Items[Offset + 2]->Hint = FMTLOAD(STATUS_COMPRESSION_HINT, (CompressionStr));
  4211. SessionStatusBar->Panels->Items[Offset + 3]->Hint = LoadStr(STATUS_DURATION_HINT);
  4212. UpdateStatusBar();
  4213. }
  4214. //---------------------------------------------------------------------------
  4215. void __fastcall TCustomScpExplorerForm::UpdateStatusBar()
  4216. {
  4217. TTBXStatusBar * SessionStatusBar = (TTBXStatusBar *)GetComponent(fcStatusBar);
  4218. DebugAssert(SessionStatusBar != NULL);
  4219. if (!Terminal || !Terminal->Active || Terminal->Status < ssOpened)
  4220. {
  4221. // note: (Terminal->Status < sshReady) currently never happens here,
  4222. // so STATUS_CONNECTING is never used
  4223. SessionStatusBar->SimplePanel = true;
  4224. SessionStatusBar->SimpleText = LoadStr(
  4225. !Terminal || !Terminal->Active ? STATUS_NOT_CONNECTED : STATUS_CONNECTING);
  4226. }
  4227. else
  4228. {
  4229. DebugAssert(Terminal);
  4230. SessionStatusBar->SimplePanel = false;
  4231. const TSessionInfo & SessionInfo = Terminal->GetSessionInfo();
  4232. if (!FNote.IsEmpty())
  4233. {
  4234. SessionStatusBar->Panels->Items[0]->Caption = FNote;
  4235. }
  4236. else
  4237. {
  4238. UpdateStatusPanelText(SessionStatusBar->Panels->Items[0]);
  4239. }
  4240. SessionStatusBar->Panels->Items[0]->Hint = FNoteHints;
  4241. SessionStatusBar->Panels->Items[SessionStatusBar->Panels->Count - 1]->Caption =
  4242. FormatDateTimeSpan(Configuration->TimeFormat, Now() - SessionInfo.LoginTime);
  4243. }
  4244. }
  4245. //---------------------------------------------------------------------------
  4246. void __fastcall TCustomScpExplorerForm::UpdateStatusPanelText(TTBXStatusPanel * Panel)
  4247. {
  4248. Panel->Caption = L"";
  4249. }
  4250. //---------------------------------------------------------------------------
  4251. void __fastcall TCustomScpExplorerForm::Idle()
  4252. {
  4253. if (FShowing || FStandaloneEditing)
  4254. {
  4255. FEditorManager->Check();
  4256. // make sure that Idle is called before update queue, as it may invoke QueueEvent
  4257. // that needs to know if queue view is visible (and it may be closed after queue update)
  4258. TTerminalManager::Instance()->Idle(FDoNotIdleCurrentTerminal > 0);
  4259. }
  4260. if (FShowing)
  4261. {
  4262. if (!NonVisualDataModule->Busy &&
  4263. // Menu is opened or drag&drop is going on
  4264. (Mouse->Capture == NULL))
  4265. {
  4266. if (FRefreshRemoteDirectory)
  4267. {
  4268. if ((Terminal != NULL) && (Terminal->Status == ssOpened))
  4269. {
  4270. Terminal->RefreshDirectory();
  4271. }
  4272. FRefreshRemoteDirectory = false;
  4273. }
  4274. if (FRefreshLocalDirectory)
  4275. {
  4276. ReloadLocalDirectory();
  4277. FRefreshLocalDirectory = false;
  4278. }
  4279. if (WinConfiguration->RefreshRemotePanel)
  4280. {
  4281. if ((Terminal != NULL) && (Terminal->Status == ssOpened) &&
  4282. (Now() - Terminal->DirectoryLoaded > WinConfiguration->RefreshRemotePanelInterval))
  4283. {
  4284. RemoteDirView->ReloadDirectory();
  4285. }
  4286. }
  4287. }
  4288. }
  4289. if (FShowing || FStandaloneEditing)
  4290. {
  4291. if (FQueueStatusInvalidated)
  4292. {
  4293. UpdateQueueStatus(false);
  4294. }
  4295. RefreshQueueItems();
  4296. }
  4297. if (FShowing)
  4298. {
  4299. UpdateStatusBar();
  4300. }
  4301. FIgnoreNextDialogChar = 0;
  4302. }
  4303. //---------------------------------------------------------------------------
  4304. void __fastcall TCustomScpExplorerForm::UserActionTimer(TObject * /*Sender*/)
  4305. {
  4306. try
  4307. {
  4308. FUserActionTimer->Enabled = false;
  4309. if (IsQueueAutoPopup() && (FPendingQueueActionItem != NULL))
  4310. {
  4311. if (TQueueItem::IsUserActionStatus(FPendingQueueActionItem->Status))
  4312. {
  4313. FPendingQueueActionItem->ProcessUserAction();
  4314. }
  4315. }
  4316. }
  4317. __finally
  4318. {
  4319. FPendingQueueActionItem = NULL;
  4320. }
  4321. }
  4322. //---------------------------------------------------------------------------
  4323. void __fastcall TCustomScpExplorerForm::ApplicationMinimize(TObject * /*Sender*/)
  4324. {
  4325. if (WinConfiguration->MinimizeToTray)
  4326. {
  4327. UpdateTrayIcon();
  4328. FTrayIcon->Visible = true;
  4329. if (Visible)
  4330. {
  4331. ShowWindow(Handle, SW_HIDE);
  4332. }
  4333. }
  4334. }
  4335. //---------------------------------------------------------------------------
  4336. void __fastcall TCustomScpExplorerForm::ApplicationRestore(TObject * /*Sender*/)
  4337. {
  4338. // WORKAROUND
  4339. // When restoring maximized window from minimization,
  4340. // rarely some controls do not align properly.
  4341. // Two instances seen (both for Commander):
  4342. // - When restoring, window is temporarily narrower (not maximized),
  4343. // causing toolbars on TopDock to wrap and dock to expand horizontally.
  4344. // Once maximized already, top dock shinks back, but the session PageControl,
  4345. // do not align up, leaving space between TopDock and PageControl.
  4346. // - Similar issue seen with LocalDirView not aligning down to status bar.
  4347. for (int Index = 0; Index < ControlCount; Index++)
  4348. {
  4349. RealignControl(Controls[Index]);
  4350. }
  4351. if (FTrayIcon->Visible)
  4352. {
  4353. FTrayIcon->Visible = false;
  4354. if (Visible)
  4355. {
  4356. ShowWindow(Handle, SW_SHOW);
  4357. }
  4358. }
  4359. if (FNeedSession && DebugAlwaysTrue(Terminal == NULL))
  4360. {
  4361. FNeedSession = false;
  4362. NonVisualDataModule->StartBusy();
  4363. try
  4364. {
  4365. NeedSession(true);
  4366. }
  4367. __finally
  4368. {
  4369. NonVisualDataModule->EndBusy();
  4370. }
  4371. }
  4372. }
  4373. //---------------------------------------------------------------------------
  4374. void __fastcall TCustomScpExplorerForm::UpdateTrayIcon()
  4375. {
  4376. FTrayIcon->Hint = GetMainForm()->Caption;
  4377. }
  4378. //---------------------------------------------------------------------------
  4379. void __fastcall TCustomScpExplorerForm::ApplicationTitleChanged()
  4380. {
  4381. UpdateTrayIcon();
  4382. }
  4383. //---------------------------------------------------------------------------
  4384. void __fastcall TCustomScpExplorerForm::RestoreApp()
  4385. {
  4386. // workaround
  4387. // TApplication.WndProc sets TApplication.FAppIconic to false,
  4388. // on WM_ACTIVATEAPP, what renders TApplication.Restore no-op function.
  4389. // But WM_ACTIVATEAPP message can be received even when
  4390. // the main window is minimized to the tray and internal editor window is focused
  4391. // (after another application was previously active)
  4392. if (::IsIconic(Handle))
  4393. {
  4394. if (!IsAppIconic())
  4395. {
  4396. SetAppIconic(true);
  4397. }
  4398. }
  4399. ::ApplicationRestore();
  4400. Application->BringToFront();
  4401. }
  4402. //---------------------------------------------------------------------------
  4403. void __fastcall TCustomScpExplorerForm::TrayIconClick(TObject * /*Sender*/)
  4404. {
  4405. RestoreApp();
  4406. }
  4407. //---------------------------------------------------------------------------
  4408. void __fastcall TCustomScpExplorerForm::NewSession(bool FromSite, const UnicodeString & SessionUrl)
  4409. {
  4410. if (OpenInNewWindow())
  4411. {
  4412. // todo: Pass FromSite
  4413. ExecuteNewInstance(SessionUrl);
  4414. }
  4415. else
  4416. {
  4417. TTerminalManager::Instance()->NewSession(FromSite, SessionUrl);
  4418. }
  4419. }
  4420. //---------------------------------------------------------------------------
  4421. UnicodeString __fastcall TCustomScpExplorerForm::SaveHiddenDuplicateSession(TSessionData * SessionData)
  4422. {
  4423. UnicodeString SessionName = StoredSessions->HiddenPrefix + Terminal->SessionData->SessionName;
  4424. StoredSessions->NewSession(SessionName, SessionData);
  4425. // modified only, explicit
  4426. StoredSessions->Save(false, true);
  4427. // encode session name because of slashes in hierarchical sessions
  4428. return EncodeUrlString(SessionName);
  4429. }
  4430. //---------------------------------------------------------------------------
  4431. UnicodeString __fastcall TCustomScpExplorerForm::CreateHiddenDuplicateSession()
  4432. {
  4433. // current working directories become defaults here, what is not right
  4434. std::unique_ptr<TSessionData> SessionData(CloneCurrentSessionData());
  4435. return SaveHiddenDuplicateSession(SessionData.get());
  4436. }
  4437. //---------------------------------------------------------------------------
  4438. void __fastcall TCustomScpExplorerForm::DuplicateSession()
  4439. {
  4440. if (OpenInNewWindow())
  4441. {
  4442. ExecuteNewInstance(CreateHiddenDuplicateSession());
  4443. }
  4444. else
  4445. {
  4446. // current working directories become defaults here, what is not right
  4447. std::unique_ptr<TSessionData> SessionData(CloneCurrentSessionData());
  4448. TTerminalManager * Manager = TTerminalManager::Instance();
  4449. TManagedTerminal * Terminal = Manager->NewManagedTerminal(SessionData.get());
  4450. Manager->ActiveTerminal = Terminal;
  4451. }
  4452. }
  4453. //---------------------------------------------------------------------------
  4454. void __fastcall TCustomScpExplorerForm::RenameSession()
  4455. {
  4456. UnicodeString Name = Terminal->SessionData->SessionName;
  4457. if (InputDialog(LoadStr(RENAME_SESSION_TITLE), LoadStr(RENAME_SESSION_PROMPT), Name, HELP_SESSION_RENAME))
  4458. {
  4459. // Checks for a slash only, so it's not that big deal that we do the check after submitting the dialog only.
  4460. TSessionData::ValidateName(Name);
  4461. // This is inconsistent with how color (for example) is handled.
  4462. Terminal->SessionData->Name = Name;
  4463. UpdateControls();
  4464. // Add/Remove distinguishing paths from sessions of the same name.
  4465. DoTerminalListChanged();
  4466. }
  4467. }
  4468. //---------------------------------------------------------------------------
  4469. bool __fastcall TCustomScpExplorerForm::CanCloseQueue(TTerminalQueue * Queue)
  4470. {
  4471. DebugAssert(Queue != NULL);
  4472. bool Result = Queue->IsEmpty;
  4473. if (!Result)
  4474. {
  4475. SetFocus();
  4476. Result = (MessageDialog(LoadStr(PENDING_QUEUE_ITEMS2), qtWarning, qaOK | qaCancel, HELP_NONE) == qaOK);
  4477. }
  4478. return Result;
  4479. }
  4480. //---------------------------------------------------------------------------
  4481. bool __fastcall TCustomScpExplorerForm::CanCloseQueue()
  4482. {
  4483. return CanCloseQueue(FQueue);
  4484. }
  4485. //---------------------------------------------------------------------------
  4486. void __fastcall TCustomScpExplorerForm::CloseSession()
  4487. {
  4488. if (CanCloseQueue())
  4489. {
  4490. TTerminalManager::Instance()->FreeActiveTerminal();
  4491. }
  4492. }
  4493. //---------------------------------------------------------------------------
  4494. void __fastcall TCustomScpExplorerForm::DisconnectSession()
  4495. {
  4496. if (CanCloseQueue())
  4497. {
  4498. TTerminalManager::Instance()->DisconnectActiveTerminal();
  4499. }
  4500. }
  4501. //---------------------------------------------------------------------------
  4502. void __fastcall TCustomScpExplorerForm::TerminalDisconnected()
  4503. {
  4504. RemoteDirView->Terminal = Terminal;
  4505. UpdateRemotePathComboBox(false);
  4506. UpdateControls();
  4507. }
  4508. //---------------------------------------------------------------------------
  4509. void __fastcall TCustomScpExplorerForm::ReconnectSession()
  4510. {
  4511. if (DebugAlwaysTrue(!Terminal->Active))
  4512. {
  4513. TTerminalManager::Instance()->ReconnectActiveTerminal();
  4514. }
  4515. }
  4516. //---------------------------------------------------------------------------
  4517. void __fastcall TCustomScpExplorerForm::OpenStoredSession(TSessionData * Data)
  4518. {
  4519. if (OpenInNewWindow())
  4520. {
  4521. // encode session name because of slashes in hierarchical sessions
  4522. ExecuteNewInstance(EncodeUrlString(Data->Name));
  4523. }
  4524. else
  4525. {
  4526. TTerminalManager * Manager = TTerminalManager::Instance();
  4527. TManagedTerminal * Terminal = Manager->NewManagedTerminal(Data);
  4528. Manager->ActiveTerminal = Terminal;
  4529. }
  4530. }
  4531. //---------------------------------------------------------------------------
  4532. void __fastcall TCustomScpExplorerForm::DoOpenFolderOrWorkspace(const UnicodeString & Name, bool ConnectFirstTerminal)
  4533. {
  4534. TTerminalManager * Manager = TTerminalManager::Instance();
  4535. std::unique_ptr<TObjectList> DataList(new TObjectList());
  4536. StoredSessions->GetFolderOrWorkspace(Name, DataList.get());
  4537. TManagedTerminal * FirstTerminal = Manager->NewTerminals(DataList.get());
  4538. if (!ConnectFirstTerminal)
  4539. {
  4540. FirstTerminal->Disconnected = true;
  4541. FirstTerminal->DisconnectedTemporarily = true;
  4542. }
  4543. Manager->ActiveTerminal = FirstTerminal;
  4544. }
  4545. //---------------------------------------------------------------------------
  4546. void __fastcall TCustomScpExplorerForm::OpenFolderOrWorkspace(const UnicodeString & Name)
  4547. {
  4548. if (OpenInNewWindow())
  4549. {
  4550. ExecuteNewInstance(Name);
  4551. }
  4552. else
  4553. {
  4554. DoOpenFolderOrWorkspace(Name, true);
  4555. }
  4556. }
  4557. //---------------------------------------------------------------------------
  4558. void __fastcall TCustomScpExplorerForm::FormCloseQuery(TObject * /*Sender*/,
  4559. bool &CanClose)
  4560. {
  4561. if (Terminal != NULL)
  4562. {
  4563. if (Terminal->Active && WinConfiguration->ConfirmClosingSession)
  4564. {
  4565. unsigned int Result;
  4566. TMessageParams Params(mpNeverAskAgainCheck);
  4567. UnicodeString Message;
  4568. int Answers = qaOK | qaCancel;
  4569. if (TTerminalManager::Instance()->Count > 1)
  4570. {
  4571. if (!WinConfiguration->AutoSaveWorkspace)
  4572. {
  4573. Message = LoadStr(CLOSE_SESSIONS_WORKSPACE3);
  4574. }
  4575. else
  4576. {
  4577. Message = LoadStr(CLOSE_SESSIONS);
  4578. }
  4579. }
  4580. else
  4581. {
  4582. if (!WinConfiguration->AutoSaveWorkspace)
  4583. {
  4584. Message = LoadStr(CLOSE_SESSION_WORKSPACE);
  4585. }
  4586. else
  4587. {
  4588. Message = LoadStr(CLOSE_SESSION);
  4589. }
  4590. Message = FORMAT(Message, (Terminal->SessionData->SessionName));
  4591. }
  4592. UnicodeString Note;
  4593. if (WinConfiguration->AutoSaveWorkspace)
  4594. {
  4595. Note = FMTLOAD(AUTO_WORKSPACE, (WorkspaceName()));
  4596. }
  4597. else
  4598. {
  4599. Note = LoadStr(AUTO_WORKSPACE_ENABLE);
  4600. Answers = qaYes | qaNo | qaCancel;
  4601. }
  4602. Message = FORMAT("%s\n\n%s", (MainInstructions(Message), Note));
  4603. SetFocus();
  4604. Result = MessageDialog(Message, qtConfirmation,
  4605. Answers, HELP_NONE, &Params);
  4606. if (Result == qaNeverAskAgain)
  4607. {
  4608. WinConfiguration->ConfirmClosingSession = false;
  4609. }
  4610. if (Result == qaNo)
  4611. {
  4612. CanClose = SaveWorkspace(true);
  4613. // note that the workspace will be saved redundatly again from FormClose
  4614. }
  4615. else
  4616. {
  4617. CanClose =
  4618. (Result == qaOK) ||
  4619. (Result == qaYes) || // CLOSE_SESSIONS_WORKSPACE variant
  4620. (Result == qaNeverAskAgain);
  4621. }
  4622. }
  4623. if (CanClose)
  4624. {
  4625. CanClose = CanCloseQueue();
  4626. }
  4627. }
  4628. if (CanClose)
  4629. {
  4630. CanClose =
  4631. FEditorManager->CloseInternalEditors(CloseInternalEditor) &&
  4632. FEditorManager->CloseExternalFilesWithoutProcess();
  4633. if (CanClose)
  4634. {
  4635. while (CanClose && (FLocalEditors->Count > 0))
  4636. {
  4637. int PrevCount = FLocalEditors->Count;
  4638. static_cast<TForm *>(FLocalEditors->Items[0])->Close();
  4639. CanClose = (FLocalEditors->Count < PrevCount);
  4640. }
  4641. if (CanClose)
  4642. {
  4643. CanClose = FEditorManager->Empty(true);
  4644. if (!CanClose)
  4645. {
  4646. SetFocus();
  4647. CanClose =
  4648. (MessageDialog(
  4649. LoadStr(PENDING_EDITORS), qtWarning, qaIgnore | qaCancel, HELP_NONE) == qaIgnore);
  4650. }
  4651. }
  4652. }
  4653. }
  4654. }
  4655. //---------------------------------------------------------------------------
  4656. void __fastcall TCustomScpExplorerForm::CloseInternalEditor(TObject * Sender)
  4657. {
  4658. TForm * Form = dynamic_cast<TForm *>(Sender);
  4659. DebugAssert(Form != NULL);
  4660. Form->Close();
  4661. }
  4662. //---------------------------------------------------------------------------
  4663. void __fastcall TCustomScpExplorerForm::ForceCloseInternalEditor(TObject * Sender)
  4664. {
  4665. TForm * Form = dynamic_cast<TForm *>(Sender);
  4666. delete Form;
  4667. }
  4668. //---------------------------------------------------------------------------
  4669. void __fastcall TCustomScpExplorerForm::ForceCloseLocalEditors()
  4670. {
  4671. while (FLocalEditors->Count > 0)
  4672. {
  4673. delete static_cast<TForm *>(FLocalEditors->Items[0]);
  4674. }
  4675. }
  4676. //---------------------------------------------------------------------------
  4677. void __fastcall TCustomScpExplorerForm::RemoteDirViewDisplayProperties(
  4678. TObject *Sender)
  4679. {
  4680. TStrings *FileList = ((TUnixDirView*)Sender)->CreateFileList(True, False, NULL);
  4681. try
  4682. {
  4683. SetProperties(osRemote, FileList);
  4684. }
  4685. __finally
  4686. {
  4687. delete FileList;
  4688. }
  4689. }
  4690. //---------------------------------------------------------------------------
  4691. void __fastcall TCustomScpExplorerForm::ComponentShowing(Byte Component, bool value)
  4692. {
  4693. if (value)
  4694. {
  4695. if (Component == fcCustomCommandsBand)
  4696. {
  4697. UpdateCustomCommandsToolbar();
  4698. }
  4699. }
  4700. }
  4701. //---------------------------------------------------------------------------
  4702. void __fastcall TCustomScpExplorerForm::SetComponentVisible(Byte Component, Boolean value)
  4703. {
  4704. TControl * Control = GetComponent(Component);
  4705. DebugAssert(Control);
  4706. bool Changed = (Control->Visible != value);
  4707. if (Changed)
  4708. {
  4709. ComponentShowing(Component, value);
  4710. TWinControl * WinControl = dynamic_cast<TWinControl*>(Control);
  4711. bool WasFocused = (WinControl != NULL) && (ActiveControl != NULL) &&
  4712. ((ActiveControl == WinControl) || (ActiveControl->Parent == WinControl));
  4713. if (value)
  4714. {
  4715. int RemainingHeight = Control->Parent->ClientHeight;
  4716. int RemainingWidth = Control->Parent->ClientWidth;
  4717. for (int i = 0; i < Control->Parent->ControlCount; i++)
  4718. {
  4719. TControl * ChildControl = Control->Parent->Controls[i];
  4720. if (ChildControl->Visible)
  4721. {
  4722. switch (ChildControl->Align)
  4723. {
  4724. case alTop:
  4725. case alBottom:
  4726. RemainingHeight -= ChildControl->Height;
  4727. break;
  4728. case alLeft:
  4729. case alRight:
  4730. RemainingWidth -= ChildControl->Width;
  4731. break;
  4732. }
  4733. }
  4734. }
  4735. int Reserve = ScaleByTextHeight(this, 32);
  4736. // queue in explorer, trees in commander
  4737. if (Control->Height > RemainingHeight - Reserve)
  4738. {
  4739. Control->Height = RemainingHeight / 2;
  4740. }
  4741. if (Control->Width > RemainingWidth - Reserve)
  4742. {
  4743. Control->Width = RemainingWidth / 2;
  4744. }
  4745. }
  4746. Control->Visible = value;
  4747. if (WasFocused && Visible)
  4748. {
  4749. DirView(osCurrent)->SetFocus();
  4750. }
  4751. FixControlsPlacement();
  4752. }
  4753. }
  4754. //---------------------------------------------------------------------------
  4755. bool __fastcall TCustomScpExplorerForm::GetComponentVisible(Byte Component)
  4756. {
  4757. TControl * Control = GetComponent(Component);
  4758. if (Control == NULL)
  4759. {
  4760. return false;
  4761. }
  4762. else
  4763. {
  4764. return Control->Visible;
  4765. }
  4766. }
  4767. //---------------------------------------------------------------------------
  4768. bool __fastcall TCustomScpExplorerForm::IsComponentPossible(Byte Component)
  4769. {
  4770. bool Result;
  4771. if ((Component == fcExplorerUpdatesBand) || (Component == fcCommanderUpdatesBand))
  4772. {
  4773. Result = !IsUWP();
  4774. }
  4775. else
  4776. {
  4777. Result = true;
  4778. }
  4779. return Result;
  4780. }
  4781. //---------------------------------------------------------------------------
  4782. void __fastcall TCustomScpExplorerForm::FixControlsPlacement()
  4783. {
  4784. if (DirView(osOther)->ItemFocused != NULL)
  4785. {
  4786. DirView(osOther)->ItemFocused->MakeVisible(false);
  4787. }
  4788. QueueSplitter->Visible = QueuePanel->Visible;
  4789. RemotePanelSplitter->Visible = RemoteDrivePanel->Visible;
  4790. }
  4791. //---------------------------------------------------------------------------
  4792. TControl * __fastcall TCustomScpExplorerForm::GetComponent(Byte Component)
  4793. {
  4794. switch (Component) {
  4795. case fcStatusBar: return RemoteStatusBar;
  4796. case fcRemotePopup: return reinterpret_cast<TControl *>(NonVisualDataModule->RemoteFilePopup);
  4797. case fcQueueView: return QueuePanel;
  4798. case fcQueueToolbar: return QueueDock;
  4799. case fcRemoteTree: return RemoteDrivePanel;
  4800. case fcSessionsTabs: return SessionsPageControl;
  4801. default: return NULL;
  4802. }
  4803. }
  4804. //---------------------------------------------------------------------------
  4805. void __fastcall TCustomScpExplorerForm::DirViewColumnRightClick(
  4806. TObject *Sender, TListColumn *Column, TPoint &Point)
  4807. {
  4808. DebugAssert(NonVisualDataModule && Column && Sender);
  4809. NonVisualDataModule->ListColumn = Column;
  4810. TPopupMenu * DirViewColumnMenu;
  4811. if (dynamic_cast<TUnixDirView *>(Sender) != NULL)
  4812. {
  4813. DirViewColumnMenu = NonVisualDataModule->RemoteDirViewColumnPopup;
  4814. NonVisualDataModule->RemoteSortByExtColumnPopupItem->Visible =
  4815. (Column->Index == uvName);
  4816. NonVisualDataModule->RemoteFormatSizeBytesPopupItem->Visible =
  4817. (Column->Index == uvSize);
  4818. }
  4819. else
  4820. {
  4821. DirViewColumnMenu = NonVisualDataModule->LocalDirViewColumnPopup;
  4822. NonVisualDataModule->LocalSortByExtColumnPopupItem->Visible =
  4823. (Column->Index == dvName);
  4824. NonVisualDataModule->LocalFormatSizeBytesPopupItem->Visible =
  4825. (Column->Index == dvSize);
  4826. }
  4827. TCustomListView * ListView = DebugNotNull(dynamic_cast<TCustomListView *>(Sender));
  4828. TPoint ScreenPoint = ListView->ClientToScreen(Point);
  4829. ScreenPoint.x -= GetScrollPos(ListView->Handle, SB_HORZ);
  4830. DirViewColumnMenu->Popup(ScreenPoint.x, ScreenPoint.y);
  4831. }
  4832. //---------------------------------------------------------------------------
  4833. void __fastcall TCustomScpExplorerForm::DirViewExecFile(
  4834. TObject *Sender, TListItem *Item, bool &AllowExec)
  4835. {
  4836. DoDirViewExecFile(Sender, Item, AllowExec);
  4837. }
  4838. //---------------------------------------------------------------------------
  4839. void __fastcall TCustomScpExplorerForm::DoDirViewExecFile(TObject * Sender,
  4840. TListItem * Item, bool & AllowExec)
  4841. {
  4842. DebugAssert(Sender && Item && Configuration);
  4843. DebugAssert(AllowExec);
  4844. TCustomDirView * ADirView = (TCustomDirView *)Sender;
  4845. bool Remote = (ADirView == DirView(osRemote)) && !IsSideLocalBrowser(osRemote);
  4846. bool ResolvedSymlinks = !Remote || Terminal->ResolvingSymlinks;
  4847. TOperationSide Side = (Remote ? osRemote : osLocal); // TODO
  4848. // Anything special is done on double click only (not on "open" indicated by FForceExecution),
  4849. // on files only (not directories)
  4850. // and only when symlinks are resolved (apply to remote panel only)
  4851. if (!ADirView->ItemIsDirectory(Item) &&
  4852. (ResolvedSymlinks || FForceExecution || Terminal->IsEncryptingFiles()))
  4853. {
  4854. if ((WinConfiguration->DoubleClickAction != dcaOpen) &&
  4855. !FForceExecution &&
  4856. (ResolvedSymlinks || Terminal->IsEncryptingFiles()))
  4857. {
  4858. if (WinConfiguration->DoubleClickAction == dcaCopy)
  4859. {
  4860. // To counter the lock in DirViewBusy (called from TCustomDirView.DoExecute)
  4861. UnlockWindow();
  4862. try
  4863. {
  4864. ExecuteFileOperation(
  4865. foCopy, Side, true, !WinConfiguration->CopyOnDoubleClickConfirmation);
  4866. }
  4867. __finally
  4868. {
  4869. LockWindow();
  4870. }
  4871. AllowExec = false;
  4872. }
  4873. else if (WinConfiguration->DoubleClickAction == dcaEdit)
  4874. {
  4875. if (!Remote || !WinConfiguration->DisableOpenEdit)
  4876. {
  4877. ExecuteFile(Side, efDefaultEditor);
  4878. AllowExec = false;
  4879. }
  4880. }
  4881. else
  4882. {
  4883. DebugFail();
  4884. }
  4885. }
  4886. // if we have not done anything special, fall back to default behavior
  4887. if (AllowExec)
  4888. {
  4889. if (Remote && !WinConfiguration->DisableOpenEdit)
  4890. {
  4891. ExecuteFile(osRemote, efShell);
  4892. AllowExec = false;
  4893. }
  4894. }
  4895. }
  4896. }
  4897. //---------------------------------------------------------------------------
  4898. bool __fastcall TCustomScpExplorerForm::GetHasDirView(TOperationSide Side)
  4899. {
  4900. return ((Side == osRemote) || (Side == osCurrent));
  4901. }
  4902. //---------------------------------------------------------------------------
  4903. void __fastcall TCustomScpExplorerForm::CompareDirectories()
  4904. {
  4905. DebugFail();
  4906. }
  4907. //---------------------------------------------------------------------------
  4908. void __fastcall TCustomScpExplorerForm::SynchronizeDirectories()
  4909. {
  4910. DebugFail();
  4911. }
  4912. //---------------------------------------------------------------------------
  4913. bool __fastcall TCustomScpExplorerForm::DoSynchronizeDirectories(
  4914. UnicodeString & LocalDirectory, UnicodeString & RemoteDirectory, int UseDefaults)
  4915. {
  4916. TSynchronizeParamType Params;
  4917. Params.LocalDirectory = LocalDirectory;
  4918. Params.RemoteDirectory = RemoteDirectory;
  4919. int UnusedParams =
  4920. (GUIConfiguration->SynchronizeParams &
  4921. (TTerminal::spPreviewChanges | TTerminal::spTimestamp | TTerminal::spNotByTime | TTerminal::spBySize));
  4922. Params.Params = GUIConfiguration->SynchronizeParams & ~UnusedParams;
  4923. Params.Options = GUIConfiguration->SynchronizeOptions;
  4924. bool SaveSettings = false;
  4925. TSynchronizeController Controller(&DoSynchronize, &DoSynchronizeInvalid,
  4926. &DoSynchronizeTooManyDirectories);
  4927. DebugAssert(FSynchronizeController == NULL);
  4928. FSynchronizeController = &Controller;
  4929. bool Result;
  4930. try
  4931. {
  4932. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  4933. int CopyParamAttrs = Terminal->UsableCopyParamAttrs(0).Upload;
  4934. int Options =
  4935. FLAGMASK(SynchronizeAllowSelectedOnly(), soAllowSelectedOnly);
  4936. DebugAssert(FOnFeedSynchronizeError == NULL);
  4937. Result = DoSynchronizeDialog(Params, &CopyParam, Controller.StartStop,
  4938. SaveSettings, Options, CopyParamAttrs, GetSynchronizeOptions, SynchronizeSessionLog,
  4939. FOnFeedSynchronizeError, SynchronizeInNewWindow, UseDefaults);
  4940. if (Result)
  4941. {
  4942. if (SaveSettings)
  4943. {
  4944. GUIConfiguration->SynchronizeParams = Params.Params | UnusedParams;
  4945. GUIConfiguration->SynchronizeOptions = Params.Options;
  4946. }
  4947. else
  4948. {
  4949. if (FLAGSET(GUIConfiguration->SynchronizeOptions, soSynchronizeAsk) &&
  4950. FLAGCLEAR(Params.Options, soSynchronizeAsk) &&
  4951. FLAGSET(Params.Options, soSynchronize))
  4952. {
  4953. GUIConfiguration->SynchronizeOptions =
  4954. (GUIConfiguration->SynchronizeOptions & ~soSynchronizeAsk) |
  4955. soSynchronize;
  4956. }
  4957. }
  4958. LocalDirectory = Params.LocalDirectory;
  4959. RemoteDirectory = Params.RemoteDirectory;
  4960. }
  4961. }
  4962. __finally
  4963. {
  4964. FSynchronizeController = NULL;
  4965. DebugAssert(FOnFeedSynchronizeError == NULL);
  4966. FOnFeedSynchronizeError = NULL;
  4967. }
  4968. return Result;
  4969. }
  4970. //---------------------------------------------------------------------------
  4971. void __fastcall TCustomScpExplorerForm::DoSynchronize(
  4972. TSynchronizeController * /*Sender*/, const UnicodeString LocalDirectory,
  4973. const UnicodeString RemoteDirectory, const TCopyParamType & CopyParam,
  4974. const TSynchronizeParamType & Params, TSynchronizeChecklist ** Checklist,
  4975. TSynchronizeOptions * Options, bool Full)
  4976. {
  4977. // Keep-up-to-date mode
  4978. if (Terminal->Status == ssOpened)
  4979. {
  4980. try
  4981. {
  4982. int PParams = Params.Params;
  4983. if (!Full)
  4984. {
  4985. PParams |= TTerminal::spNoRecurse | TTerminal::spUseCache | TTerminal::spDelayProgress;
  4986. if (FLAGSET(Params.Options, soRecurse))
  4987. {
  4988. PParams |= TTerminal::spSubDirs;
  4989. }
  4990. }
  4991. else
  4992. {
  4993. // if keepuptodate is non-recursive,
  4994. // full sync before has to be non-recursive as well
  4995. if (FLAGCLEAR(Params.Options, soRecurse))
  4996. {
  4997. PParams |= TTerminal::spNoRecurse;
  4998. }
  4999. }
  5000. Synchronize(LocalDirectory, RemoteDirectory, smRemote, CopyParam,
  5001. PParams, Checklist, Options);
  5002. }
  5003. catch (Exception & E)
  5004. {
  5005. if (FLAGSET(Params.Options, soContinueOnError) && Terminal->Active &&
  5006. DebugAlwaysTrue(FOnFeedSynchronizeError != NULL))
  5007. {
  5008. // noop, already logged in MoreMessageDialog
  5009. }
  5010. else
  5011. {
  5012. ShowExtendedExceptionEx(Terminal, &E);
  5013. throw;
  5014. }
  5015. }
  5016. }
  5017. }
  5018. //---------------------------------------------------------------------------
  5019. void __fastcall TCustomScpExplorerForm::DoSynchronizeInvalid(
  5020. TSynchronizeController * /*Sender*/, const UnicodeString Directory,
  5021. const UnicodeString ErrorStr)
  5022. {
  5023. if (!Directory.IsEmpty())
  5024. {
  5025. SimpleErrorDialog(FMTLOAD(WATCH_ERROR_DIRECTORY, (Directory)), ErrorStr);
  5026. }
  5027. else
  5028. {
  5029. SimpleErrorDialog(LoadStr(WATCH_ERROR_GENERAL), ErrorStr);
  5030. }
  5031. }
  5032. //---------------------------------------------------------------------------
  5033. void __fastcall TCustomScpExplorerForm::DoSynchronizeTooManyDirectories(
  5034. TSynchronizeController * /*Sender*/, int & MaxDirectories)
  5035. {
  5036. if (MaxDirectories < GUIConfiguration->MaxWatchDirectories)
  5037. {
  5038. MaxDirectories = GUIConfiguration->MaxWatchDirectories;
  5039. }
  5040. else
  5041. {
  5042. TMessageParams Params(mpNeverAskAgainCheck);
  5043. unsigned int Result = MessageDialog(
  5044. FMTLOAD(TOO_MANY_WATCH_DIRECTORIES, (MaxDirectories, MaxDirectories)),
  5045. qtConfirmation, qaYes | qaNo, HELP_TOO_MANY_WATCH_DIRECTORIES, &Params);
  5046. if ((Result == qaYes) || (Result == qaNeverAskAgain))
  5047. {
  5048. MaxDirectories *= 2;
  5049. if (Result == qaNeverAskAgain)
  5050. {
  5051. GUIConfiguration->MaxWatchDirectories = MaxDirectories;
  5052. }
  5053. }
  5054. else
  5055. {
  5056. Abort();
  5057. }
  5058. }
  5059. }
  5060. //---------------------------------------------------------------------------
  5061. void __fastcall TCustomScpExplorerForm::Synchronize(const UnicodeString LocalDirectory,
  5062. const UnicodeString RemoteDirectory, TSynchronizeMode Mode,
  5063. const TCopyParamType & CopyParam, int Params, TSynchronizeChecklist ** Checklist,
  5064. TSynchronizeOptions * Options)
  5065. {
  5066. // Keep-up-to-date mode
  5067. DebugAssert(!FAutoOperation);
  5068. void * BatchStorage;
  5069. BatchStart(BatchStorage);
  5070. FAutoOperation = true;
  5071. bool AnyOperation = false;
  5072. TDateTime StartTime = Now();
  5073. TSynchronizeChecklist * AChecklist = NULL;
  5074. try
  5075. {
  5076. FSynchronizeProgressForm = new TSynchronizeProgressForm(Application, true, -1);
  5077. if (FLAGCLEAR(Params, TTerminal::spDelayProgress))
  5078. {
  5079. FSynchronizeProgressForm->Start();
  5080. }
  5081. AChecklist = Terminal->SynchronizeCollect(LocalDirectory, RemoteDirectory,
  5082. static_cast<TTerminal::TSynchronizeMode>(Mode),
  5083. &CopyParam, Params | TTerminal::spNoConfirmation, TerminalSynchronizeDirectory,
  5084. Options);
  5085. SAFE_DESTROY(FSynchronizeProgressForm);
  5086. AnyOperation = (AChecklist->CheckedCount > 0);
  5087. if (AnyOperation)
  5088. {
  5089. TSynchronizeProgress SynchronizeProgress(AChecklist);
  5090. CreateProgressForm(&SynchronizeProgress);
  5091. try
  5092. {
  5093. Terminal->SynchronizeApply(
  5094. AChecklist, &CopyParam, Params | TTerminal::spNoConfirmation, TerminalSynchronizeDirectory,
  5095. SynchronizeProcessedItem, NULL, NULL, NULL);
  5096. }
  5097. __finally
  5098. {
  5099. DestroyProgressForm();
  5100. }
  5101. }
  5102. }
  5103. __finally
  5104. {
  5105. if (Checklist == NULL)
  5106. {
  5107. delete AChecklist;
  5108. }
  5109. else
  5110. {
  5111. *Checklist = AChecklist;
  5112. }
  5113. FAutoOperation = false;
  5114. SAFE_DESTROY(FSynchronizeProgressForm);
  5115. BatchEnd(BatchStorage);
  5116. ReloadLocalDirectory();
  5117. if (AnyOperation)
  5118. {
  5119. OperationComplete(StartTime);
  5120. }
  5121. }
  5122. }
  5123. //---------------------------------------------------------------------------
  5124. bool __fastcall TCustomScpExplorerForm::SynchronizeAllowSelectedOnly()
  5125. {
  5126. // can be called from command line
  5127. return Visible &&
  5128. ((DirView(osOther)->SelCount > 0) ||
  5129. (HasDirView[osLocal] && (DirView(osLocal)->SelCount > 0)));
  5130. }
  5131. //---------------------------------------------------------------------------
  5132. void __fastcall TCustomScpExplorerForm::SynchronizeSessionLog(const UnicodeString & Message)
  5133. {
  5134. LogSynchronizeEvent(Terminal, Message);
  5135. }
  5136. //---------------------------------------------------------------------------
  5137. void __fastcall TCustomScpExplorerForm::GetSynchronizeOptions(
  5138. int Params, TSynchronizeOptions & Options)
  5139. {
  5140. DebugAssert(!IsLocalBrowserMode());
  5141. if (FLAGSET(Params, TTerminal::spSelectedOnly) && SynchronizeAllowSelectedOnly())
  5142. {
  5143. Options.Filter = new TStringList();
  5144. Options.Filter->CaseSensitive = false;
  5145. Options.Filter->Duplicates = Types::dupAccept;
  5146. if (DirView(osRemote)->SelCount > 0)
  5147. {
  5148. DirView(osRemote)->CreateFileList(false, false, Options.Filter);
  5149. }
  5150. if (HasDirView[osLocal] && (DirView(osLocal)->SelCount > 0))
  5151. {
  5152. DirView(osLocal)->CreateFileList(false, false, Options.Filter);
  5153. }
  5154. Options.Filter->Sort();
  5155. }
  5156. }
  5157. //---------------------------------------------------------------------------
  5158. UnicodeString __fastcall TCustomScpExplorerForm::SerializeCopyParamForCommandLine(const TCopyParamType * CopyParams)
  5159. {
  5160. TCopyParamType Defaults;
  5161. std::unique_ptr<THierarchicalStorage> ConfigStorage(Configuration->CreateConfigStorage());
  5162. ConfigStorage->AccessMode = smRead;
  5163. if (ConfigStorage->OpenSubKey(Configuration->ConfigurationSubKey, false))
  5164. {
  5165. GUIConfiguration->LoadCopyParam(ConfigStorage.get(), &Defaults);
  5166. }
  5167. ConfigStorage.reset(NULL);
  5168. std::unique_ptr<TStringList> Options(new TStringList());
  5169. std::unique_ptr<TOptionsStorage> OptionsStorage(new TOptionsStorage(Options.get(), true));
  5170. CopyParams->Save(OptionsStorage.get(), &Defaults);
  5171. UnicodeString Result;
  5172. if (Options->Count > 0)
  5173. {
  5174. Result =
  5175. FORMAT(L" %s%s", (TProgramParams::FormatSwitch(RAWTRANSFERSETTINGS_SWITCH), StringsToParams(Options.get())));
  5176. }
  5177. return Result;
  5178. }
  5179. //---------------------------------------------------------------------------
  5180. void __fastcall TCustomScpExplorerForm::SynchronizeInNewWindow(
  5181. const TSynchronizeParamType & Params, const TCopyParamType * CopyParams)
  5182. {
  5183. UnicodeString SessionName = CreateHiddenDuplicateSession();
  5184. UnicodeString AdditionalParams =
  5185. FORMAT(L"%s \"%s\" \"%s\" %d %d %s%s", (
  5186. TProgramParams::FormatSwitch(KEEP_UP_TO_DATE_SWITCH),
  5187. Params.LocalDirectory, Params.RemoteDirectory, Params.Params, Params.Options,
  5188. TProgramParams::FormatSwitch(DEFAULTS_SWITCH),
  5189. SerializeCopyParamForCommandLine(CopyParams)));
  5190. ExecuteNewInstance(SessionName, AdditionalParams);
  5191. }
  5192. //---------------------------------------------------------------------------
  5193. struct TSynchronizeParams
  5194. {
  5195. UnicodeString LocalDirectory;
  5196. UnicodeString RemoteDirectory;
  5197. TSynchronizeMode Mode;
  5198. TSynchronizeChecklist * Checklist;
  5199. int Params;
  5200. TCopyParamType * CopyParam;
  5201. TDateTime CollectElapsed;
  5202. TDateTime * StartTime;
  5203. TProcessedSynchronizationChecklistItem OnProcessedItem;
  5204. };
  5205. //---------------------------------------------------------------------------
  5206. void __fastcall TCustomScpExplorerForm::FullSynchronize(
  5207. TSynchronizeParams & Params, TProcessedSynchronizationChecklistItem OnProcessedItem,
  5208. TUpdatedSynchronizationChecklistItems OnUpdatedSynchronizationChecklistItems)
  5209. {
  5210. DebugAssert(!FAutoOperation);
  5211. void * BatchStorage;
  5212. BatchStart(BatchStorage);
  5213. FAutoOperation = true;
  5214. TFileOperationStatistics Statistics;
  5215. TDateTime Start = Now();
  5216. try
  5217. {
  5218. Params.OnProcessedItem = OnProcessedItem;
  5219. TSynchronizeProgress SynchronizeProgress(Params.Checklist);
  5220. CreateProgressForm(&SynchronizeProgress);
  5221. Terminal->SynchronizeApply(
  5222. Params.Checklist, Params.CopyParam, Params.Params | TTerminal::spNoConfirmation,
  5223. TerminalSynchronizeDirectory, SynchronizeProcessedItem, OnUpdatedSynchronizationChecklistItems, &Params,
  5224. &Statistics);
  5225. }
  5226. __finally
  5227. {
  5228. Params.OnProcessedItem = NULL;
  5229. FAutoOperation = false;
  5230. DestroyProgressForm();
  5231. BatchEnd(BatchStorage);
  5232. ReloadLocalDirectory();
  5233. }
  5234. if (WinConfiguration->SynchronizeSummary)
  5235. {
  5236. UnicodeString Message = MainInstructions(LoadStr(SYNCHRONIZE_COMPLETE)) + L"\n";
  5237. // The statistics should be 0 anyway in this case
  5238. if (FLAGCLEAR(Params.Params, TTerminal::spTimestamp))
  5239. {
  5240. Message += L"\n";
  5241. if (Statistics.FilesUploaded > 0)
  5242. {
  5243. Message += FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 1), (FormatNumber(Statistics.FilesUploaded), FormatBytes(Statistics.TotalUploaded))) + L"\n";
  5244. }
  5245. if (Statistics.FilesDownloaded > 0)
  5246. {
  5247. Message += FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 2), (FormatNumber(Statistics.FilesDownloaded), FormatBytes(Statistics.TotalDownloaded))) + L"\n";
  5248. }
  5249. if (Statistics.FilesDeletedLocal > 0)
  5250. {
  5251. Message += FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 3), (FormatNumber(Statistics.FilesDeletedLocal))) + L"\n";
  5252. }
  5253. if (Statistics.FilesDeletedRemote > 0)
  5254. {
  5255. Message += FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 4), (FormatNumber(Statistics.FilesDeletedRemote))) + L"\n";
  5256. }
  5257. }
  5258. TDateTime Elapsed = (Now() - Start);
  5259. Message +=
  5260. L"\n" +
  5261. FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 5), (FormatDateTimeSpan(Configuration->TimeFormat, Params.CollectElapsed))) + L"\n" +
  5262. FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 6), (FormatDateTimeSpan(Configuration->TimeFormat, Elapsed)));
  5263. TMessageParams Params(mpNeverAskAgainCheck);
  5264. unsigned int Result = MessageDialog(Message, qtInformation, qaOK, HELP_NONE, &Params);
  5265. if (Result == qaNeverAskAgain)
  5266. {
  5267. WinConfiguration->SynchronizeSummary = false;
  5268. }
  5269. }
  5270. }
  5271. //---------------------------------------------------------------------------
  5272. void __fastcall TCustomScpExplorerForm::SynchronizeProcessedItem(void * Token, const TSynchronizeChecklist::TItem * ChecklistItem)
  5273. {
  5274. if (DebugAlwaysTrue(FProgressForm != NULL) && DebugAlwaysTrue(FProgressForm->SynchronizeProgress != NULL))
  5275. {
  5276. FProgressForm->SynchronizeProgress->ItemProcessed(ChecklistItem);
  5277. }
  5278. // Not set in keep-up-to-date mode - Synchronize() method
  5279. if (Token != NULL)
  5280. {
  5281. TSynchronizeParams & Params = *static_cast<TSynchronizeParams *>(Token);
  5282. if (Params.OnProcessedItem != NULL)
  5283. {
  5284. Params.OnProcessedItem(NULL, ChecklistItem);
  5285. }
  5286. }
  5287. }
  5288. //---------------------------------------------------------------------------
  5289. void __fastcall TCustomScpExplorerForm::DoFullSynchronize(
  5290. void * Token, TProcessedSynchronizationChecklistItem OnProcessedItem,
  5291. TUpdatedSynchronizationChecklistItems OnUpdatedSynchronizationChecklistItems)
  5292. {
  5293. TSynchronizeParams & Params = *static_cast<TSynchronizeParams *>(Token);
  5294. *Params.StartTime = Now();
  5295. FullSynchronize(Params, OnProcessedItem, OnUpdatedSynchronizationChecklistItems);
  5296. }
  5297. //---------------------------------------------------------------------------
  5298. void __fastcall TCustomScpExplorerForm::DoSynchronizeChecklistCalculateSize(
  5299. TSynchronizeChecklist * Checklist, const TSynchronizeChecklist::TItemList & Items, void * Token)
  5300. {
  5301. // terminal can be already closed (e.g. dropped connection)
  5302. if (Terminal != NULL)
  5303. {
  5304. TSynchronizeParams & Params = *static_cast<TSynchronizeParams *>(Token);
  5305. Terminal->SynchronizeChecklistCalculateSize(Checklist, Items, Params.CopyParam);
  5306. }
  5307. }
  5308. //---------------------------------------------------------------------------
  5309. void __fastcall TCustomScpExplorerForm::DoSynchronizeMove(
  5310. TOperationSide Side, const UnicodeString & FileName, const UnicodeString & NewFileName, TRemoteFile * RemoteFile)
  5311. {
  5312. DebugAssert(!IsLocalBrowserMode());
  5313. TAutoBatch AutoBatch(this);
  5314. TAutoFlag AutoOperationFlag(FAutoOperation);
  5315. if (Side == osRemote)
  5316. {
  5317. std::unique_ptr<TStrings> FileList(new TStringList());
  5318. FileList->AddObject(FileName, RemoteFile);
  5319. UnicodeString Target = UnixExtractFileDir(NewFileName);
  5320. UnicodeString FileMask = DelimitFileNameMask(UnixExtractFileName(NewFileName));
  5321. RemoteDirView->SaveSelection();
  5322. RemoteDirView->SaveSelectedNames();
  5323. try
  5324. {
  5325. Terminal->MoveFiles(FileList.get(), Target, FileMask);
  5326. }
  5327. catch(...)
  5328. {
  5329. RemoteDirView->DiscardSavedSelection();
  5330. throw;
  5331. }
  5332. RemoteDirView->RestoreSelection();
  5333. }
  5334. else if (DebugAlwaysTrue(Side == osLocal))
  5335. {
  5336. if (!MoveFile(FileName.c_str(), NewFileName.c_str()))
  5337. {
  5338. throw EOSExtException(FMTLOAD(RENAME_FILE_ERROR, (FileName, NewFileName)));
  5339. }
  5340. UnicodeString Directory = ExtractFileDir(FileName);
  5341. ReloadLocalDirectory(Directory);
  5342. UnicodeString NewDirectory = ExtractFileDir(NewFileName);
  5343. if (!SamePaths(Directory, NewDirectory))
  5344. {
  5345. ReloadLocalDirectory(NewDirectory);
  5346. }
  5347. }
  5348. }
  5349. //---------------------------------------------------------------------------
  5350. void __fastcall TCustomScpExplorerForm::DoSynchronizeBrowse(TOperationSide Side, TSynchronizeChecklist::TAction Action, const TSynchronizeChecklist::TItem * Item)
  5351. {
  5352. UnicodeString LocalPath = ExcludeTrailingBackslash(Item->Local.Directory);
  5353. if (Side == osLocal)
  5354. {
  5355. if (Action == TSynchronizeChecklist::saDownloadNew)
  5356. {
  5357. OpenFolderInExplorer(LocalPath);
  5358. }
  5359. else
  5360. {
  5361. OpenFileInExplorer(TPath::Combine(LocalPath, Item->GetFileName()));
  5362. }
  5363. }
  5364. else if (DebugAlwaysTrue(Side == osRemote))
  5365. {
  5366. // Similar to CreateHiddenDuplicateSession, except that it modifies the initial directories
  5367. std::unique_ptr<TSessionData> SessionData(CloneCurrentSessionData());
  5368. SessionData->RemoteDirectory = UnixExcludeTrailingBackslash(Item->Remote.Directory);
  5369. if (!LocalPath.IsEmpty())
  5370. {
  5371. SessionData->LocalDirectory = LocalPath;
  5372. }
  5373. UnicodeString SessionName = SaveHiddenDuplicateSession(SessionData.get());
  5374. ExecuteNewInstance(SessionName, FORMAT(L"%s=%s", (TProgramParams::FormatSwitch(BROWSE_SWITCH), Item->GetFileName())));
  5375. }
  5376. }
  5377. //---------------------------------------------------------------------------
  5378. void __fastcall TCustomScpExplorerForm::FullSynchronizeInNewWindow(
  5379. TSynchronizeMode Mode, int Params, const UnicodeString & LocalDirectory, const UnicodeString & RemoteDirectory,
  5380. const TCopyParamType * CopyParams)
  5381. {
  5382. UnicodeString SessionName = CreateHiddenDuplicateSession();
  5383. UnicodeString AdditionalParams =
  5384. FORMAT(L"%s \"%s\" \"%s\" %d %d %s%s", (
  5385. TProgramParams::FormatSwitch(SYNCHRONIZE_SWITCH),
  5386. LocalDirectory, RemoteDirectory, Mode, Params,
  5387. TProgramParams::FormatSwitch(DEFAULTS_SWITCH),
  5388. SerializeCopyParamForCommandLine(CopyParams)));
  5389. ExecuteNewInstance(SessionName, AdditionalParams);
  5390. }
  5391. //---------------------------------------------------------------------------
  5392. int __fastcall TCustomScpExplorerForm::DoFullSynchronizeDirectories(
  5393. UnicodeString & LocalDirectory, UnicodeString & RemoteDirectory,
  5394. TSynchronizeMode & Mode, int Params, bool & SaveMode, int UseDefaults)
  5395. {
  5396. int Result;
  5397. bool SaveSettings = false;
  5398. int Options =
  5399. FLAGMASK(!Terminal->IsCapable[fcTimestampChanging], fsoDisableTimestamp) |
  5400. FLAGMASK(SynchronizeAllowSelectedOnly(), fsoAllowSelectedOnly);
  5401. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  5402. TUsableCopyParamAttrs CopyParamAttrs = Terminal->UsableCopyParamAttrs(0);
  5403. bool Continue =
  5404. (UseDefaults == 0) ||
  5405. DoFullSynchronizeDialog(Mode, Params, LocalDirectory, RemoteDirectory,
  5406. &CopyParam, SaveSettings, SaveMode, Options, CopyParamAttrs, FullSynchronizeInNewWindow, UseDefaults);
  5407. if (Continue)
  5408. {
  5409. Configuration->Usage->Inc(L"Synchronizations");
  5410. UpdateCopyParamCounters(CopyParam);
  5411. TSynchronizeOptions SynchronizeOptions;
  5412. GetSynchronizeOptions(Params, SynchronizeOptions);
  5413. if (SaveSettings)
  5414. {
  5415. GUIConfiguration->SynchronizeParams = Params;
  5416. }
  5417. else
  5418. {
  5419. SaveMode = false;
  5420. }
  5421. TDateTime StartTime = Now();
  5422. TSynchronizeChecklist * Checklist = NULL;
  5423. try
  5424. {
  5425. DebugAssert(!FAutoOperation);
  5426. FAutoOperation = true;
  5427. try
  5428. {
  5429. UnicodeString SessionKey = Terminal->SessionData->SessionKey;
  5430. std::unique_ptr<TStrings> DataList(Configuration->LoadDirectoryStatisticsCache(SessionKey, RemoteDirectory, CopyParam));
  5431. int Files = -1;
  5432. if (DataList->Count >= 1)
  5433. {
  5434. Files = StrToIntDef(DataList->Strings[0], Files);
  5435. }
  5436. else
  5437. {
  5438. DataList->Add(UnicodeString());
  5439. }
  5440. FSynchronizeProgressForm = new TSynchronizeProgressForm(Application, true, Files);
  5441. FSynchronizeProgressForm->Start();
  5442. Checklist = Terminal->SynchronizeCollect(LocalDirectory, RemoteDirectory,
  5443. static_cast<TTerminal::TSynchronizeMode>(Mode),
  5444. &CopyParam, Params | TTerminal::spNoConfirmation, TerminalSynchronizeDirectory,
  5445. &SynchronizeOptions);
  5446. if (Terminal->SessionData->CacheDirectories)
  5447. {
  5448. DataList->Strings[0] = IntToStr(SynchronizeOptions.Files);
  5449. Configuration->SaveDirectoryStatisticsCache(SessionKey, RemoteDirectory, CopyParam, DataList.get());
  5450. }
  5451. }
  5452. __finally
  5453. {
  5454. FAutoOperation = false;
  5455. SAFE_DESTROY(FSynchronizeProgressForm);
  5456. }
  5457. TSynchronizeParams SynchronizeParams;
  5458. SynchronizeParams.LocalDirectory = LocalDirectory;
  5459. SynchronizeParams.RemoteDirectory = RemoteDirectory;
  5460. SynchronizeParams.Mode = Mode;
  5461. SynchronizeParams.CopyParam = &CopyParam;
  5462. SynchronizeParams.Params = Params;
  5463. SynchronizeParams.Checklist = Checklist;
  5464. SynchronizeParams.CollectElapsed = (Now() - StartTime);
  5465. SynchronizeParams.StartTime = &StartTime;
  5466. SynchronizeParams.OnProcessedItem = NULL;
  5467. Result = Checklist->Count;
  5468. if (Checklist->Count > 0)
  5469. {
  5470. if (FLAGSET(Params, TTerminal::spPreviewChanges))
  5471. {
  5472. if (!DoSynchronizeChecklistDialog(
  5473. Checklist, Mode, Params, LocalDirectory, RemoteDirectory, CustomCommandMenu, DoFullSynchronize,
  5474. DoSynchronizeChecklistCalculateSize, DoSynchronizeMove, DoSynchronizeBrowse, &SynchronizeParams))
  5475. {
  5476. Result = -1;
  5477. }
  5478. }
  5479. else
  5480. {
  5481. FullSynchronize(SynchronizeParams, NULL, NULL);
  5482. }
  5483. }
  5484. else
  5485. {
  5486. UnicodeString Message = MainInstructions(LoadStr(COMPARE_NO_DIFFERENCES));
  5487. MessageDialog(Message, qtInformation, qaOK, HELP_SYNCHRONIZE_NO_DIFFERENCES);
  5488. }
  5489. }
  5490. __finally
  5491. {
  5492. delete Checklist;
  5493. }
  5494. OperationComplete(StartTime);
  5495. }
  5496. else
  5497. {
  5498. Result = -1;
  5499. }
  5500. return Result;
  5501. }
  5502. //---------------------------------------------------------------------------
  5503. void __fastcall TCustomScpExplorerForm::TerminalSynchronizeDirectory(
  5504. const UnicodeString & LocalDirectory, const UnicodeString & RemoteDirectory,
  5505. bool & Continue, bool Collect, const TSynchronizeOptions * Options)
  5506. {
  5507. if (Collect)
  5508. {
  5509. DebugAssert(FSynchronizeProgressForm != NULL);
  5510. if (!FSynchronizeProgressForm->Started)
  5511. {
  5512. FSynchronizeProgressForm->Start();
  5513. }
  5514. int CompareProgress = FSynchronizeProgressForm->SetData(LocalDirectory, RemoteDirectory, Options->Files, Continue);
  5515. SetTaskbarListProgressValue(CompareProgress);
  5516. }
  5517. else
  5518. {
  5519. DebugAssert(FSynchronizeProgressForm == NULL);
  5520. DebugAssert(FProgressForm != NULL);
  5521. }
  5522. }
  5523. //---------------------------------------------------------------------------
  5524. void __fastcall TCustomScpExplorerForm::StandaloneEdit(const UnicodeString & FileName)
  5525. {
  5526. UnicodeString FullFileName = AbsolutePath(FTerminal->CurrentDirectory, FileName);
  5527. TRemoteFile * File = NULL;
  5528. Terminal->ReadFile(FullFileName, File);
  5529. if (File != NULL)
  5530. {
  5531. std::unique_ptr<TRemoteFile> FileOwner(File);
  5532. TAutoFlag Flag(FStandaloneEditing);
  5533. ExecuteRemoteFile(FullFileName, File, efInternalEditor);
  5534. Application->ShowMainForm = false;
  5535. Application->Run();
  5536. }
  5537. }
  5538. //---------------------------------------------------------------------------
  5539. void __fastcall TCustomScpExplorerForm::ExploreLocalDirectory()
  5540. {
  5541. DebugFail();
  5542. }
  5543. //---------------------------------------------------------------------------
  5544. TSessionData * __fastcall TCustomScpExplorerForm::CloneCurrentSessionData()
  5545. {
  5546. std::unique_ptr<TSessionData> SessionData(new TSessionData(L""));
  5547. SessionData->Assign(Terminal->SessionData);
  5548. UpdateSessionData(SessionData.get());
  5549. Terminal->UpdateSessionCredentials(SessionData.get());
  5550. if (!Terminal->SessionData->HasSessionName())
  5551. {
  5552. // Particularly for "Workspace/XXXX" name, we need to reset it, as it would become user-visible
  5553. // once IsWorkspace is cleared
  5554. SessionData->Name = UnicodeString();
  5555. SessionData->IsWorkspace = false;
  5556. }
  5557. return SessionData.release();
  5558. }
  5559. //---------------------------------------------------------------------------
  5560. void __fastcall TCustomScpExplorerForm::SaveCurrentSession()
  5561. {
  5562. TSessionData * SessionData = CloneCurrentSessionData();
  5563. try
  5564. {
  5565. TSessionData * EditingSessionData = StoredSessions->FindSame(SessionData);
  5566. DoSaveSession(SessionData, EditingSessionData, true, NULL);
  5567. }
  5568. __finally
  5569. {
  5570. delete SessionData;
  5571. }
  5572. }
  5573. //---------------------------------------------------------------------------
  5574. TObjectList * __fastcall TCustomScpExplorerForm::DoCollectWorkspace()
  5575. {
  5576. TTerminalManager * Manager = TTerminalManager::Instance();
  5577. std::unique_ptr<TObjectList> DataList(new TObjectList());
  5578. if (DebugAlwaysTrue(Terminal != NULL))
  5579. {
  5580. // Update (Managed)Terminal->StateData
  5581. UpdateTerminal(Terminal);
  5582. }
  5583. Manager->SaveWorkspace(DataList.get());
  5584. return DataList.release();
  5585. }
  5586. //---------------------------------------------------------------------------
  5587. void __fastcall TCustomScpExplorerForm::DoSaveWorkspace(
  5588. const UnicodeString & Name, TObjectList * DataList, bool SavePasswords, bool Explicit)
  5589. {
  5590. WinConfiguration->LastWorkspace = Name;
  5591. if (!SavePasswords)
  5592. {
  5593. for (int Index = 0; Index < DataList->Count; Index++)
  5594. {
  5595. TSessionData * SessionData = dynamic_cast<TSessionData *>(DataList->Items[Index]);
  5596. if (SessionData->Link.IsEmpty())
  5597. {
  5598. SessionData->ClearSessionPasswords();
  5599. }
  5600. }
  5601. }
  5602. StoredSessions->NewWorkspace(Name, DataList);
  5603. // modified only
  5604. StoredSessions->Save(false, Explicit);
  5605. }
  5606. //---------------------------------------------------------------------------
  5607. UnicodeString __fastcall TCustomScpExplorerForm::WorkspaceName()
  5608. {
  5609. return
  5610. DefaultStr(WinConfiguration->LastWorkspace,
  5611. DefaultStr(WinConfiguration->AutoWorkspace, LoadStr(NEW_WORKSPACE)));
  5612. }
  5613. //---------------------------------------------------------------------------
  5614. bool __fastcall TCustomScpExplorerForm::SaveWorkspace(bool EnableAutoSave)
  5615. {
  5616. std::unique_ptr<TObjectList> DataList(DoCollectWorkspace());
  5617. bool AnyNonStoredSessionWithPassword = false;
  5618. bool AnyNonStoredNonWorkspaceSessionWithPassword = false;
  5619. bool AllNonStoredSessionsAnonymous = true;
  5620. TTerminalManager * Manager = TTerminalManager::Instance();
  5621. for (int Index = 0; Index < DataList->Count; Index++)
  5622. {
  5623. TSessionData * SessionData = dynamic_cast<TSessionData *>(DataList->Items[Index]);
  5624. if (SessionData->Link.IsEmpty())
  5625. {
  5626. if (SessionData->HasAnySessionPassword())
  5627. {
  5628. AnyNonStoredSessionWithPassword = true;
  5629. if (!Manager->Terminals[Index]->SessionData->IsWorkspace)
  5630. {
  5631. AnyNonStoredNonWorkspaceSessionWithPassword = true;
  5632. }
  5633. }
  5634. if (!SameText(SessionData->UserName, AnonymousUserName))
  5635. {
  5636. AllNonStoredSessionsAnonymous = false;
  5637. }
  5638. }
  5639. }
  5640. bool SavePasswords;
  5641. bool * PSavePasswords;
  5642. bool NotRecommendedSavingPasswords =
  5643. !CustomWinConfiguration->UseMasterPassword &&
  5644. !AllNonStoredSessionsAnonymous;
  5645. if (Configuration->DisablePasswordStoring ||
  5646. !AnyNonStoredSessionWithPassword)
  5647. {
  5648. PSavePasswords = NULL;
  5649. SavePasswords = false;
  5650. }
  5651. else
  5652. {
  5653. PSavePasswords = &SavePasswords;
  5654. SavePasswords =
  5655. !AnyNonStoredNonWorkspaceSessionWithPassword ||
  5656. !NotRecommendedSavingPasswords;
  5657. }
  5658. UnicodeString Name = WorkspaceName();
  5659. bool CreateShortcut = false;
  5660. bool Result =
  5661. DoSaveWorkspaceDialog(
  5662. Name, PSavePasswords, NotRecommendedSavingPasswords, CreateShortcut,
  5663. EnableAutoSave);
  5664. if (Result)
  5665. {
  5666. DoSaveWorkspace(Name, DataList.get(), SavePasswords, true);
  5667. if (CreateShortcut)
  5668. {
  5669. TOperationVisualizer Visualizer;
  5670. UnicodeString AdditionalParams = TProgramParams::FormatSwitch(DESKTOP_SWITCH);
  5671. CreateDesktopSessionShortCut(Name, L"", AdditionalParams, -1, WORKSPACE_ICON);
  5672. }
  5673. if (EnableAutoSave)
  5674. {
  5675. WinConfiguration->AutoSaveWorkspace = true;
  5676. WinConfiguration->AutoWorkspace = Name;
  5677. if (PSavePasswords != NULL)
  5678. {
  5679. WinConfiguration->AutoSaveWorkspacePasswords = SavePasswords;
  5680. }
  5681. }
  5682. }
  5683. return Result;
  5684. }
  5685. //---------------------------------------------------------------------------
  5686. void __fastcall TCustomScpExplorerForm::UpdateTerminal(TManagedTerminal * Terminal)
  5687. {
  5688. DebugAssert(!IsLocalBrowserMode());
  5689. SAFE_DESTROY(Terminal->RemoteExplorerState);
  5690. if (WinConfiguration->PreservePanelState)
  5691. {
  5692. Terminal->RemoteExplorerState = RemoteDirView->SaveState();
  5693. }
  5694. UpdateSessionData(Terminal->StateData);
  5695. }
  5696. //---------------------------------------------------------------------------
  5697. void __fastcall TCustomScpExplorerForm::UpdateSessionData(TSessionData * Data)
  5698. {
  5699. // Keep in sync with TSessionData::CopyStateData
  5700. DebugAssert(!IsLocalBrowserMode());
  5701. DebugAssert(Data != NULL);
  5702. // This is inconsistent with how color (for example) is handled.
  5703. Data->Name = Terminal->SessionData->Name;
  5704. // cannot use RemoteDirView->Path, because it is empty if connection
  5705. // was already closed
  5706. // also only peek, we may not be connected at all atm
  5707. // so make sure we do not try retrieving current directory from the server
  5708. // (particularly with FTP)
  5709. UnicodeString ACurrentDirectory = Terminal->PeekCurrentDirectory();
  5710. if (!ACurrentDirectory.IsEmpty())
  5711. {
  5712. Data->RemoteDirectory = ACurrentDirectory;
  5713. }
  5714. Data->Color = SessionColor;
  5715. }
  5716. //---------------------------------------------------------------------------
  5717. void __fastcall TCustomScpExplorerForm::ToolBarResize(TObject *Sender)
  5718. {
  5719. TTBXToolbar * Toolbar = dynamic_cast<TTBXToolbar*>(Sender);
  5720. DebugAssert(Toolbar != NULL);
  5721. for (int i = 0; i < Toolbar->Items->Count; i++)
  5722. {
  5723. TTBXCustomDropDownItem * DropDownItem;
  5724. DropDownItem = dynamic_cast<TTBXCustomDropDownItem *>(Toolbar->Items->Items[i]);
  5725. if (DropDownItem != NULL)
  5726. {
  5727. ToolbarItemResize(DropDownItem,
  5728. Toolbar->Width - (Toolbar->View->BaseSize.x - DropDownItem->EditWidth) -
  5729. Toolbar->NonClientWidth);
  5730. break;
  5731. }
  5732. }
  5733. }
  5734. //---------------------------------------------------------------------------
  5735. void __fastcall TCustomScpExplorerForm::ToolbarItemResize(TTBXCustomDropDownItem * Item, int Width)
  5736. {
  5737. Item->EditWidth = Width;
  5738. }
  5739. //---------------------------------------------------------------------------
  5740. void __fastcall TCustomScpExplorerForm::ToolbarGetBaseSize(
  5741. TTBCustomToolbar * Toolbar, TPoint & ASize)
  5742. {
  5743. for (int i = 0; i < Toolbar->Items->Count; i++)
  5744. {
  5745. TTBXCustomDropDownItem * DropDownItem;
  5746. DropDownItem = dynamic_cast<TTBXCustomDropDownItem *>(Toolbar->Items->Items[i]);
  5747. if (DropDownItem != NULL)
  5748. {
  5749. ASize.x -= DropDownItem->EditWidth;
  5750. ASize.x += ScaleByTextHeight(this, 50) /* minimal combo width */;
  5751. }
  5752. }
  5753. }
  5754. //---------------------------------------------------------------------------
  5755. void __fastcall TCustomScpExplorerForm::DoWarnLackOfTempSpace(
  5756. const UnicodeString Path, __int64 RequiredSpace, bool & Continue)
  5757. {
  5758. if (WinConfiguration->DDWarnLackOfTempSpace)
  5759. {
  5760. UnicodeString ADrive = ExtractFileDrive(ExpandFileName(Path));
  5761. if (!ADrive.IsEmpty())
  5762. {
  5763. __int64 FreeSpace = DiskFree((Byte)(ADrive[1]-'A'+1));
  5764. DebugAssert(RequiredSpace >= 0);
  5765. __int64 RequiredWithReserve;
  5766. RequiredWithReserve = (__int64)(RequiredSpace * WinConfiguration->DDWarnLackOfTempSpaceRatio);
  5767. if (FreeSpace < RequiredWithReserve)
  5768. {
  5769. unsigned int Result;
  5770. TMessageParams Params(mpNeverAskAgainCheck);
  5771. Result = MessageDialog(FMTLOAD(DD_WARN_LACK_OF_TEMP_SPACE, (Path,
  5772. FormatBytes(FreeSpace), FormatBytes(RequiredSpace))),
  5773. qtWarning, qaYes | qaNo, HELP_DD_WARN_LACK_OF_TEMP_SPACE, &Params);
  5774. if (Result == qaNeverAskAgain)
  5775. {
  5776. WinConfiguration->DDWarnLackOfTempSpace = false;
  5777. }
  5778. Continue = (Result == qaYes || Result == qaNeverAskAgain);
  5779. }
  5780. }
  5781. }
  5782. }
  5783. //---------------------------------------------------------------------------
  5784. void __fastcall TCustomScpExplorerForm::AddBookmark(TOperationSide Side)
  5785. {
  5786. DoOpenDirectoryDialog(odAddBookmark, Side);
  5787. }
  5788. //---------------------------------------------------------------------------
  5789. TStrings * __fastcall TCustomScpExplorerForm::CreateVisitedDirectories(TOperationSide Side)
  5790. {
  5791. // we should better use TCustomDirView::FCaseSensitive, but it is private
  5792. TStringList * VisitedDirectories = CreateSortedStringList(!IsSideLocalBrowser(Side));
  5793. try
  5794. {
  5795. TCustomDirView * DView = DirView(Side);
  5796. for (int Index = -DView->BackCount; Index <= DView->ForwardCount; Index++)
  5797. {
  5798. VisitedDirectories->Add(DView->HistoryPath[Index]);
  5799. }
  5800. }
  5801. catch (...)
  5802. {
  5803. delete VisitedDirectories;
  5804. throw;
  5805. }
  5806. return VisitedDirectories;
  5807. }
  5808. //---------------------------------------------------------------------------
  5809. void __fastcall TCustomScpExplorerForm::DoOpenDirectoryDialog(
  5810. TOpenDirectoryMode Mode, TOperationSide Side)
  5811. {
  5812. bool Continue = true;
  5813. if (Mode == odAddBookmark)
  5814. {
  5815. TMessageParams Params(mpNeverAskAgainCheck);
  5816. Params.NeverAskAgainTitle = LoadStr(ADD_BOOKMARK_SHARED);
  5817. Params.NeverAskAgainCheckedInitially = WinConfiguration->UseSharedBookmarks;
  5818. UnicodeString Message = MainInstructions(FMTLOAD(ADD_BOOKMARK_CONFIRM, (DirView(Side)->PathName)));
  5819. unsigned int Answer =
  5820. MessageDialog(Message,
  5821. qtConfirmation, qaOK | qaCancel, HELP_ADD_BOOKMARK_CONFIRM, &Params);
  5822. if (Answer == qaNeverAskAgain)
  5823. {
  5824. Continue = true;
  5825. WinConfiguration->UseSharedBookmarks = true;
  5826. }
  5827. else if (Answer == qaOK)
  5828. {
  5829. Continue = true;
  5830. WinConfiguration->UseSharedBookmarks = false;
  5831. }
  5832. else
  5833. {
  5834. Continue = false;
  5835. }
  5836. }
  5837. if (Continue)
  5838. {
  5839. TStrings * VisitedDirectories = CreateVisitedDirectories(Side);
  5840. try
  5841. {
  5842. UnicodeString Name = DirView(Side)->PathName;
  5843. bool Repeat;
  5844. do
  5845. {
  5846. Repeat = false;
  5847. if (::DoOpenDirectoryDialog(Mode, Side, Name, VisitedDirectories, Terminal,
  5848. // do not allow switching to location profiles,
  5849. // if we are not connected
  5850. HasDirView[osLocal] && (Terminal != NULL)))
  5851. {
  5852. TWindowLock Lock(this);
  5853. if (!TryOpenDirectory(Side, Name))
  5854. {
  5855. Repeat = true;
  5856. Mode = odBrowse;
  5857. }
  5858. }
  5859. }
  5860. while (Repeat);
  5861. }
  5862. __finally
  5863. {
  5864. delete VisitedDirectories;
  5865. }
  5866. }
  5867. }
  5868. //---------------------------------------------------------------------------
  5869. bool __fastcall TCustomScpExplorerForm::CommandSessionFallback()
  5870. {
  5871. DebugAssert(!FTerminal->CommandSessionOpened);
  5872. return TTerminalManager::Instance()->ConnectTerminal(FTerminal->CommandSession);
  5873. }
  5874. //---------------------------------------------------------------------------
  5875. bool __fastcall TCustomScpExplorerForm::EnsureCommandSessionFallback(TFSCapability Capability)
  5876. {
  5877. bool Result = FTerminal->IsCapable[Capability] ||
  5878. FTerminal->CommandSessionOpened;
  5879. if (!Result)
  5880. {
  5881. DebugAssert(FTerminal->IsCapable[fcSecondaryShell]);
  5882. if (!GUIConfiguration->ConfirmCommandSession)
  5883. {
  5884. Result = true;
  5885. }
  5886. else
  5887. {
  5888. TMessageParams Params(mpNeverAskAgainCheck);
  5889. const TFileSystemInfo & FileSystemInfo = Terminal->GetFileSystemInfo();
  5890. unsigned int Answer = MessageDialog(FMTLOAD(PERFORM_ON_COMMAND_SESSION2,
  5891. (FileSystemInfo.ProtocolName, FileSystemInfo.ProtocolName)), qtConfirmation,
  5892. qaOK | qaCancel, HELP_PERFORM_ON_COMMAND_SESSION, &Params);
  5893. if (Answer == qaNeverAskAgain)
  5894. {
  5895. GUIConfiguration->ConfirmCommandSession = false;
  5896. Result = true;
  5897. }
  5898. else if (Answer == qaOK)
  5899. {
  5900. Result = true;
  5901. }
  5902. }
  5903. if (Result)
  5904. {
  5905. Result = CommandSessionFallback();
  5906. }
  5907. }
  5908. return Result;
  5909. }
  5910. //---------------------------------------------------------------------------
  5911. void __fastcall TCustomScpExplorerForm::OpenConsole(UnicodeString Command)
  5912. {
  5913. if (EnsureCommandSessionFallback(fcAnyCommand))
  5914. {
  5915. DoConsoleDialog(Terminal, Command);
  5916. }
  5917. }
  5918. //---------------------------------------------------------------------------
  5919. void __fastcall TCustomScpExplorerForm::FileControlDDDragEnter(
  5920. TObject *Sender, _di_IDataObject /*DataObj*/, int /*grfKeyState*/,
  5921. const TPoint & /*Point*/, int & /*dwEffect*/, bool & Accept)
  5922. {
  5923. if (!IsSideLocalBrowser(osRemote) &&
  5924. IsFileControl(DropSourceControl, osRemote) &&
  5925. (FDDExtMapFile != NULL))
  5926. {
  5927. Accept = true;
  5928. }
  5929. FDDTargetControl = dynamic_cast<TControl*>(Sender);
  5930. DebugAssert(FDDTargetControl != NULL);
  5931. }
  5932. //---------------------------------------------------------------------------
  5933. void __fastcall TCustomScpExplorerForm::SessionsDDDragEnter(
  5934. _di_IDataObject DataObj, int KeyState,
  5935. const TPoint & Point, int & Effect, bool & Accept)
  5936. {
  5937. FileControlDDDragEnter(SessionsPageControl, DataObj, KeyState, Point, Effect, Accept);
  5938. }
  5939. //---------------------------------------------------------------------------
  5940. void __fastcall TCustomScpExplorerForm::QueueDDDragEnter(
  5941. _di_IDataObject DataObj, int KeyState,
  5942. const TPoint & Point, int & Effect, bool & Accept)
  5943. {
  5944. FileControlDDDragEnter(QueueView3, DataObj, KeyState, Point, Effect, Accept);
  5945. }
  5946. //---------------------------------------------------------------------------
  5947. void __fastcall TCustomScpExplorerForm::FileControlDDDragLeave(
  5948. TObject * Sender)
  5949. {
  5950. DebugUsedParam(Sender);
  5951. DebugAssert(FDDTargetControl == Sender);
  5952. FDDTargetControl = NULL;
  5953. }
  5954. //---------------------------------------------------------------------------
  5955. void __fastcall TCustomScpExplorerForm::SessionsDDDragLeave()
  5956. {
  5957. FileControlDDDragLeave(SessionsPageControl);
  5958. }
  5959. //---------------------------------------------------------------------------
  5960. void __fastcall TCustomScpExplorerForm::QueueDDDragLeave()
  5961. {
  5962. FileControlDDDragLeave(QueueView3);
  5963. }
  5964. //---------------------------------------------------------------------------
  5965. void __fastcall TCustomScpExplorerForm::AddEditLink(TOperationSide Side, bool Add)
  5966. {
  5967. DebugAssert(!IsSideLocalBrowser(Side));
  5968. DebugUsedParam(Side);
  5969. bool Edit = false;
  5970. TRemoteFile * File = NULL;
  5971. UnicodeString FileName;
  5972. UnicodeString PointTo;
  5973. bool SymbolicLink = true;
  5974. if (RemoteDirView->ItemFocused)
  5975. {
  5976. DebugAssert(RemoteDirView->ItemFocused->Data);
  5977. File = (TRemoteFile *)RemoteDirView->ItemFocused->Data;
  5978. Edit = !Add && File->IsSymLink && Terminal->SessionData->ResolveSymlinks;
  5979. if (Edit)
  5980. {
  5981. FileName = File->FileName;
  5982. PointTo = File->LinkTo;
  5983. }
  5984. else
  5985. {
  5986. PointTo = File->FileName;
  5987. }
  5988. }
  5989. if (DoSymlinkDialog(FileName, PointTo, osRemote, SymbolicLink, Edit,
  5990. Terminal->IsCapable[fcHardLink]))
  5991. {
  5992. Configuration->Usage->Inc(L"RemoteLinksCreated");
  5993. if (Edit)
  5994. {
  5995. DebugAssert(File->FileName == FileName);
  5996. int Params = dfNoRecursive;
  5997. Terminal->ExceptionOnFail = true;
  5998. try
  5999. {
  6000. Terminal->DeleteFile(L"", File, &Params);
  6001. }
  6002. __finally
  6003. {
  6004. Terminal->ExceptionOnFail = false;
  6005. }
  6006. }
  6007. Terminal->CreateLink(FileName, PointTo, SymbolicLink);
  6008. }
  6009. }
  6010. //---------------------------------------------------------------------------
  6011. bool __fastcall TCustomScpExplorerForm::CanAddEditLink(TOperationSide Side)
  6012. {
  6013. return
  6014. (IsSideLocalBrowser(Side) ||
  6015. ((Terminal != NULL) &&
  6016. Terminal->ResolvingSymlinks &&
  6017. Terminal->IsCapable[fcSymbolicLink]));
  6018. }
  6019. //---------------------------------------------------------------------------
  6020. bool __fastcall TCustomScpExplorerForm::LinkFocused()
  6021. {
  6022. return
  6023. !IsSideLocalBrowser(FCurrentSide) &&
  6024. (RemoteDirView->ItemFocused != NULL) &&
  6025. ((TRemoteFile *)RemoteDirView->ItemFocused->Data)->IsSymLink &&
  6026. Terminal->SessionData->ResolveSymlinks;
  6027. }
  6028. //---------------------------------------------------------------------------
  6029. void __fastcall TCustomScpExplorerForm::ExecuteCurrentFile()
  6030. {
  6031. DebugAssert(!WinConfiguration->DisableOpenEdit);
  6032. FForceExecution = true;
  6033. try
  6034. {
  6035. DirView(osCurrent)->ExecuteCurrentFile();
  6036. }
  6037. __finally
  6038. {
  6039. FForceExecution = false;
  6040. }
  6041. }
  6042. //---------------------------------------------------------------------------
  6043. void __fastcall TCustomScpExplorerForm::ExecuteCurrentFileWith(bool OnFocused)
  6044. {
  6045. TEditorData ExternalEditor;
  6046. ExternalEditor.Editor = edExternal;
  6047. bool Remember = false;
  6048. if (DoEditorPreferencesDialog(&ExternalEditor, Remember, epmAdHoc, !IsSideLocalBrowser(osCurrent)))
  6049. {
  6050. if (Remember)
  6051. {
  6052. TEditorList * EditorList = new TEditorList;
  6053. try
  6054. {
  6055. *EditorList = *WinConfiguration->EditorList;
  6056. bool Found = false;
  6057. int i = 0;
  6058. while (!Found && (i < EditorList->Count))
  6059. {
  6060. const TEditorPreferences * Editor = EditorList->Editors[i];
  6061. if ((Editor->Data->Editor == edExternal) &&
  6062. (Editor->Data->ExternalEditor == ExternalEditor.ExternalEditor))
  6063. {
  6064. Found = true;
  6065. }
  6066. i++;
  6067. }
  6068. if (!Found)
  6069. {
  6070. EditorList->Add(new TEditorPreferences(ExternalEditor));
  6071. WinConfiguration->EditorList = EditorList;
  6072. }
  6073. }
  6074. __finally
  6075. {
  6076. delete EditorList;
  6077. }
  6078. }
  6079. ExecuteFile(osCurrent, efExternalEditor, &ExternalEditor, true, OnFocused);
  6080. }
  6081. }
  6082. //---------------------------------------------------------------------------
  6083. void __fastcall TCustomScpExplorerForm::TerminalRemoved(TObject * Sender)
  6084. {
  6085. FEditorManager->ProcessFiles(FileTerminalRemoved, Sender);
  6086. if (FFileFindTerminal == Sender)
  6087. {
  6088. FFileFindTerminal = NULL;
  6089. HideFileFindDialog();
  6090. }
  6091. if (FClipboardTerminal == Sender)
  6092. {
  6093. ClipboardClear(); // implies ClipboardStop
  6094. }
  6095. }
  6096. //---------------------------------------------------------------------------
  6097. void __fastcall TCustomScpExplorerForm::FileTerminalRemoved(const UnicodeString FileName,
  6098. TEditedFileData * Data, TObject * /*Token*/, void * Arg)
  6099. {
  6100. TTerminal * Terminal = static_cast<TTerminal *>(Arg);
  6101. DebugAssert(Terminal != NULL);
  6102. if (Data->Terminal == Terminal)
  6103. {
  6104. Data->Terminal = NULL;
  6105. }
  6106. }
  6107. //---------------------------------------------------------------------------
  6108. void __fastcall TCustomScpExplorerForm::LastTerminalClosed(TObject * /*Sender*/)
  6109. {
  6110. UpdateControls();
  6111. SessionColor = TColor(0);
  6112. UpdateRemotePathComboBox(false);
  6113. NeedSession(false);
  6114. }
  6115. //---------------------------------------------------------------------------
  6116. void __fastcall TCustomScpExplorerForm::NeedSession(bool Startup)
  6117. {
  6118. try
  6119. {
  6120. // Cache, as the login dialog can change its value
  6121. bool ShowLogin = WinConfiguration->ShowLoginWhenNoSession;
  6122. try
  6123. {
  6124. if (ShowLogin)
  6125. {
  6126. bool ReloadSessions = !Startup;
  6127. TTerminalManager::Instance()->NewSession(false, L"", ReloadSessions, this);
  6128. }
  6129. else if (Startup && WinConfiguration->AutoSaveWorkspace && !WinConfiguration->AutoWorkspace.IsEmpty() &&
  6130. // This detects if workspace was saved the last time the main widow was closed
  6131. SameText(WinConfiguration->LastStoredSession, WinConfiguration->AutoWorkspace))
  6132. {
  6133. DoOpenFolderOrWorkspace(WinConfiguration->AutoWorkspace, false);
  6134. Configuration->Usage->Inc(L"OpenedWorkspacesAuto");
  6135. }
  6136. }
  6137. __finally
  6138. {
  6139. // Do not terminate, if we are only starting up and we are not showing Login dialog
  6140. // (so there was no chance for the user to open any session yet)
  6141. if ((ShowLogin || !Startup) &&
  6142. !WinConfiguration->KeepOpenWhenNoSession &&
  6143. ((Terminal == NULL) || (!Terminal->Active && !Terminal->Permanent)))
  6144. {
  6145. TerminateApplication();
  6146. }
  6147. }
  6148. }
  6149. catch (EAbort &)
  6150. {
  6151. // swallow
  6152. // The TTerminalManager does not expect the OnLastTerminalClose to throw without trying to connect.
  6153. // Also when called from TWinControl.UpdateShowing => CMShowingChanged, the showing is aborted on exception
  6154. }
  6155. }
  6156. //---------------------------------------------------------------------------
  6157. void __fastcall TCustomScpExplorerForm::DoTerminalListChanged()
  6158. {
  6159. TStrings * TerminalList = TTerminalManager::Instance()->TerminalList;
  6160. int ActiveTerminalIndex = TTerminalManager::Instance()->ActiveTerminalIndex;
  6161. Configuration->Usage->SetMax(L"MaxOpenedSessions", TerminalList->Count);
  6162. SendMessage(SessionsPageControl->Handle, WM_SETREDRAW, 0, 0);
  6163. try
  6164. {
  6165. while ((SessionsPageControl->PageCount > TerminalList->Count + 1) ||
  6166. // Clear the design time unthemed tab
  6167. ((SessionsPageControl->PageCount > 0) &&
  6168. (dynamic_cast<TThemeTabSheet *>(SessionsPageControl->Pages[SessionsPageControl->PageCount - 1]) == NULL)))
  6169. {
  6170. delete SessionsPageControl->Pages[SessionsPageControl->PageCount - 1];
  6171. }
  6172. for (int Index = 0; Index <= TerminalList->Count; Index++)
  6173. {
  6174. TThemeTabSheet * TabSheet;
  6175. if (Index >= SessionsPageControl->PageCount)
  6176. {
  6177. TabSheet = new TThemeTabSheet(SessionsPageControl);
  6178. TabSheet->PageControl = SessionsPageControl;
  6179. }
  6180. else
  6181. {
  6182. TabSheet = DebugNotNull(dynamic_cast<TThemeTabSheet *>(SessionsPageControl->Pages[Index]));
  6183. }
  6184. bool IsSessionTab = (Index < TerminalList->Count);
  6185. if (IsSessionTab)
  6186. {
  6187. TTerminal * Terminal = dynamic_cast<TTerminal *>(TerminalList->Objects[Index]);
  6188. TabSheet->Tag = reinterpret_cast<int>(Terminal);
  6189. UpdateSessionTab(TabSheet);
  6190. }
  6191. else
  6192. {
  6193. TabSheet->ImageIndex = FNewSessionTabImageIndex;
  6194. TabSheet->Tag = 0; // not really needed
  6195. TabSheet->Shadowed = false;
  6196. // 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)
  6197. UpdateNewSessionTab();
  6198. }
  6199. TabSheet->ShowCloseButton = IsSessionTab;
  6200. }
  6201. }
  6202. __finally
  6203. {
  6204. SendMessage(SessionsPageControl->Handle, WM_SETREDRAW, 1, 0);
  6205. }
  6206. SessionsPageControl->ActivePageIndex = ActiveTerminalIndex;
  6207. }
  6208. //---------------------------------------------------------------------------
  6209. void __fastcall TCustomScpExplorerForm::TerminalListChanged(TObject * /*Sender*/)
  6210. {
  6211. DoTerminalListChanged();
  6212. }
  6213. //---------------------------------------------------------------------------
  6214. void __fastcall TCustomScpExplorerForm::UpdateNewSessionTab()
  6215. {
  6216. TTabSheet * TabSheet = SessionsPageControl->Pages[SessionsPageControl->PageCount - 1];
  6217. DebugAssert(TabSheet->ImageIndex == 0);
  6218. TabSheet->Caption =
  6219. WinConfiguration->SelectiveToolbarText ?
  6220. StripHotkey(StripTrailingPunctuation(NonVisualDataModule->NewSessionAction->Caption)) :
  6221. UnicodeString();
  6222. }
  6223. //---------------------------------------------------------------------------
  6224. TManagedTerminal * __fastcall TCustomScpExplorerForm::GetSessionTabTerminal(TTabSheet * TabSheet)
  6225. {
  6226. return reinterpret_cast<TManagedTerminal *>(TabSheet->Tag);
  6227. }
  6228. //---------------------------------------------------------------------------
  6229. void __fastcall TCustomScpExplorerForm::UpdateSessionTab(TTabSheet * TabSheet)
  6230. {
  6231. if (DebugAlwaysTrue(TabSheet != NULL))
  6232. {
  6233. TManagedTerminal * ATerminal = GetSessionTabTerminal(TabSheet);
  6234. if (DebugAlwaysTrue(ATerminal != NULL))
  6235. {
  6236. TColor Color = (ATerminal == FTerminal) ? FSessionColor : ATerminal->StateData->Color;
  6237. TabSheet->ImageIndex = AddSessionColor(Color);
  6238. UnicodeString TabCaption = TTerminalManager::Instance()->GetTerminalTitle(ATerminal, true);
  6239. TabSheet->Caption = SessionsPageControl->FormatCaptionWithCloseButton(TabCaption);
  6240. TThemeTabSheet * ThemeTabSheet = dynamic_cast<TThemeTabSheet *>(TabSheet);
  6241. if (DebugAlwaysTrue(ThemeTabSheet != NULL))
  6242. {
  6243. ThemeTabSheet->Shadowed = !ATerminal->Active;
  6244. }
  6245. }
  6246. }
  6247. }
  6248. //---------------------------------------------------------------------------
  6249. bool __fastcall TCustomScpExplorerForm::SessionTabSwitched()
  6250. {
  6251. DebugAssert(SessionsPageControl->ActivePage != NULL);
  6252. TManagedTerminal * Terminal = GetSessionTabTerminal(SessionsPageControl->ActivePage);
  6253. bool Result = (Terminal != NULL);
  6254. if (Result)
  6255. {
  6256. TTerminalManager::Instance()->ActiveTerminal = Terminal;
  6257. }
  6258. else
  6259. {
  6260. try
  6261. {
  6262. NewSession(false);
  6263. }
  6264. __finally
  6265. {
  6266. DoTerminalListChanged();
  6267. }
  6268. FSessionsPageControlNewSessionTime = Now();
  6269. }
  6270. return Result;
  6271. }
  6272. //---------------------------------------------------------------------------
  6273. void __fastcall TCustomScpExplorerForm::SessionsPageControlChange(TObject * /*Sender*/)
  6274. {
  6275. SessionTabSwitched();
  6276. }
  6277. //---------------------------------------------------------------------------
  6278. void __fastcall TCustomScpExplorerForm::TransferListChange(TObject * Sender)
  6279. {
  6280. TTBXStringList * TransferList = dynamic_cast<TTBXStringList *>(Sender);
  6281. DebugAssert(TransferList != NULL);
  6282. UnicodeString Name;
  6283. if (TransferList->ItemIndex <= 0)
  6284. {
  6285. Name = L"";
  6286. }
  6287. else
  6288. {
  6289. Name = GUIConfiguration->CopyParamList->Names[TransferList->ItemIndex - 1];
  6290. }
  6291. if (FCopyParamAutoSelected.IsEmpty())
  6292. {
  6293. // if previous preset was not autoselected, make new preset the "default"
  6294. FCopyParamDefault = Name;
  6295. }
  6296. GUIConfiguration->CopyParamCurrent = Name;
  6297. }
  6298. //---------------------------------------------------------------------------
  6299. void __fastcall TCustomScpExplorerForm::UpdateTransferLabel()
  6300. {
  6301. // sanity check
  6302. bool ExistingPreset =
  6303. (FTransferListHoverIndex >= 0) &&
  6304. (FTransferListHoverIndex < 1 + GUIConfiguration->CopyParamList->Count);
  6305. DebugAssert(ExistingPreset);
  6306. if (ExistingPreset)
  6307. {
  6308. HDC DC = GetDC(0);
  6309. TCanvas * Canvas = new TCanvas();
  6310. try
  6311. {
  6312. Canvas->Handle = DC;
  6313. Canvas->Font = GetToolbarFont(this);
  6314. UnicodeString Name;
  6315. if (FTransferListHoverIndex == 0)
  6316. {
  6317. Name = L"";
  6318. }
  6319. else
  6320. {
  6321. Name = GUIConfiguration->CopyParamList->Names[FTransferListHoverIndex - 1];
  6322. }
  6323. TTBXLabelItem * TransferLabel = dynamic_cast<TTBXLabelItem*>(
  6324. static_cast<TComponent*>(GetComponent(fcTransferLabel)));
  6325. TTBXStringList * TransferList = dynamic_cast<TTBXStringList*>(
  6326. static_cast<TObject*>(GetComponent(fcTransferList)));
  6327. UnicodeString InfoStr =
  6328. GUIConfiguration->CopyParamPreset[Name].
  6329. GetInfoStr(L"; ",
  6330. FLAGMASK(((Terminal != NULL) && Terminal->Active), Terminal->UsableCopyParamAttrs(0).General));
  6331. int MaxWidth = TransferList->MinWidth - (2 * TransferLabel->Margin) - ScaleByTextHeight(this, 10);
  6332. if (Canvas->TextExtent(InfoStr).cx > MaxWidth)
  6333. {
  6334. while (Canvas->TextExtent(InfoStr + Ellipsis).cx > MaxWidth)
  6335. {
  6336. InfoStr.SetLength(InfoStr.Length() - 1);
  6337. }
  6338. InfoStr += Ellipsis;
  6339. }
  6340. // UpdateCaption does not cause invalidation of whole submenu, while
  6341. // setting Caption property does.
  6342. // also it probably does not resize the label, even if necessary
  6343. // (we do not want that anyway)
  6344. TransferLabel->UpdateCaption(InfoStr);
  6345. }
  6346. __finally
  6347. {
  6348. Canvas->Handle = NULL;
  6349. ReleaseDC(0, DC);
  6350. delete Canvas;
  6351. }
  6352. }
  6353. }
  6354. //---------------------------------------------------------------------------
  6355. void __fastcall TCustomScpExplorerForm::TransferListDrawItem(
  6356. TTBXCustomList * /*Sender*/, TCanvas * /*ACanvas*/, const TRect & /*ARect*/,
  6357. int /*AIndex*/, int AHoverIndex, bool & /*DrawDefault*/)
  6358. {
  6359. if (FTransferListHoverIndex != AHoverIndex)
  6360. {
  6361. FTransferListHoverIndex = AHoverIndex;
  6362. UpdateTransferLabel();
  6363. }
  6364. }
  6365. //---------------------------------------------------------------------------
  6366. void __fastcall TCustomScpExplorerForm::WMAppCommand(TMessage & Message)
  6367. {
  6368. int Command = GET_APPCOMMAND_LPARAM(Message.LParam);
  6369. TShiftState Shift = KeyDataToShiftState(GET_KEYSTATE_LPARAM(Message.LParam));
  6370. if ((Shift * (TShiftState() << ssShift << ssAlt << ssCtrl)).Empty())
  6371. {
  6372. if (Command == APPCOMMAND_BROWSER_FAVORITES)
  6373. {
  6374. OpenDirectory(GetSide(osCurrent));
  6375. Message.Result = 1;
  6376. }
  6377. else
  6378. {
  6379. TForm::Dispatch(&Message);
  6380. }
  6381. }
  6382. else
  6383. {
  6384. TForm::Dispatch(&Message);
  6385. }
  6386. }
  6387. //---------------------------------------------------------------------------
  6388. void __fastcall TCustomScpExplorerForm::CMDialogChar(TMessage & AMessage)
  6389. {
  6390. TCMDialogChar & Message = reinterpret_cast<TCMDialogChar &>(AMessage);
  6391. if ((FIgnoreNextDialogChar != 0) &&
  6392. (toupper(Message.CharCode) == toupper(FIgnoreNextDialogChar)))
  6393. {
  6394. Message.Result = 1;
  6395. }
  6396. else
  6397. {
  6398. TForm::Dispatch(&Message);
  6399. }
  6400. FIgnoreNextDialogChar = 0;
  6401. }
  6402. //---------------------------------------------------------------------------
  6403. void __fastcall TCustomScpExplorerForm::WMSysCommand(TMessage & Message)
  6404. {
  6405. // The four low-order bits are used internally by Windows
  6406. unsigned int Cmd = (Message.WParam & 0xFFF0);
  6407. // SC_RESTORE, SC_MAXIMIZE, SC_MINIMIZE - buttons on windows title
  6408. // SC_DEFAULT - double click on windows title (does not work, at least on WinXP)
  6409. // 61730 - restore thru double click - undocumented
  6410. // 61490 - maximize thru double click - undocumented
  6411. if ((Cmd == SC_RESTORE) || (Cmd == SC_MAXIMIZE) ||
  6412. (Cmd == SC_MINIMIZE) || (Cmd == SC_DEFAULT))
  6413. {
  6414. SysResizing(Cmd);
  6415. }
  6416. TForm::Dispatch(&Message);
  6417. }
  6418. //---------------------------------------------------------------------------
  6419. void __fastcall TCustomScpExplorerForm::WMQueryEndSession(TMessage & Message)
  6420. {
  6421. // We were actually never able to make ENDSESSION_CRITICAL happen.
  6422. // Also there no point returning TRUE as we are not able to
  6423. // handle the abrupt termination caused by subsequent WM_ENDSESSION cleanly.
  6424. // Hence the process termination might be safer :)
  6425. if ((Message.LParam != ENDSESSION_CRITICAL) &&
  6426. (((FQueue != NULL) && !FQueue->IsEmpty) || (FProgressForm != NULL)))
  6427. {
  6428. Message.Result = FALSE;
  6429. }
  6430. else
  6431. {
  6432. Message.Result = TRUE;
  6433. }
  6434. // Do not call default handling as that triggers OnCloseQuery,
  6435. // where our implementation will popup confirmation dialogs, what we do not want,
  6436. // as per Vista guidelines:
  6437. // https://docs.microsoft.com/en-us/windows/win32/shutdown/shutting-down
  6438. }
  6439. //---------------------------------------------------------------------------
  6440. void __fastcall TCustomScpExplorerForm::WMEndSession(TWMEndSession & Message)
  6441. {
  6442. if (Message.EndSession && IsApplicationMinimized())
  6443. {
  6444. // WORKAROUND
  6445. // TApplication.WndProc() calls Application.Terminate() before Halt(),
  6446. // when it receives WM_ENDSESSION,
  6447. // but that sometimes (particularly when application is minimized) cause crashes.
  6448. // Crash popups message that blocks log off.
  6449. // Obviously application cannot shutdown cleanly after WM_ENDSESSION,
  6450. // so we call ExitProcess() immediately, not even trying to cleanup.
  6451. // It still causes beep, so there's likely some popup, but it does not block
  6452. // log off.
  6453. ExitProcess(0);
  6454. }
  6455. TForm::Dispatch(&Message);
  6456. }
  6457. #ifdef _DEBUG
  6458. //---------------------------------------------------------------------------
  6459. void __fastcall TCustomScpExplorerForm::WMWindowPosChanged(TWMWindowPosMsg & Message)
  6460. {
  6461. TForm::Dispatch(&Message);
  6462. }
  6463. #endif
  6464. //---------------------------------------------------------------------------
  6465. void __fastcall TCustomScpExplorerForm::SysResizing(unsigned int /*Cmd*/)
  6466. {
  6467. }
  6468. //---------------------------------------------------------------------------
  6469. void __fastcall TCustomScpExplorerForm::DoShow()
  6470. {
  6471. // only now are the controls resized finally, so the size constraints
  6472. // will not conflict with possibly very small window size
  6473. RestoreFormParams();
  6474. // This indicates that the form was rescaled after parameters were loaded and
  6475. // as most probably the parameters were actually saved with the current form scaling
  6476. // (windows was the last time likely closed on the same monitor, where it is starting now),
  6477. // rescaling happened twice already. Reload the parameters, as now they likely won't need any scaling
  6478. // and no rounding problems will occur.
  6479. // See also RestoreForm.
  6480. // (we should be safe to call RestoreParams unconditionally)
  6481. if (PixelsPerInch != Screen->PixelsPerInch)
  6482. {
  6483. RestoreParams();
  6484. }
  6485. FixControlsPlacement();
  6486. if (Position == poDefaultPosOnly)
  6487. {
  6488. CutFormToDesktop(this);
  6489. }
  6490. TForm::DoShow();
  6491. FSessionsDragDropFilesEx->DragDropControl = SessionsPageControl;
  6492. FQueueDragDropFilesEx->DragDropControl = QueueView3;
  6493. if (Terminal == NULL)
  6494. {
  6495. StartingDisconnected();
  6496. }
  6497. UpdatePixelsPerInchMainWindowCounter();
  6498. FShowing = true;
  6499. }
  6500. //---------------------------------------------------------------------------
  6501. void __fastcall TCustomScpExplorerForm::UpdatePixelsPerInchMainWindowCounter()
  6502. {
  6503. Configuration->Usage->Set(L"PixelsPerInchMainWindow", PixelsPerInch);
  6504. }
  6505. //---------------------------------------------------------------------------
  6506. void __fastcall TCustomScpExplorerForm::StartingDisconnected()
  6507. {
  6508. DoTerminalListChanged();
  6509. }
  6510. //---------------------------------------------------------------------------
  6511. void __fastcall TCustomScpExplorerForm::PopupTrayBalloon(TTerminal * Terminal,
  6512. const UnicodeString & Str, TQueryType Type, Exception * E, unsigned int Seconds,
  6513. TNotifyEvent OnBalloonClick, TObject * UserData)
  6514. {
  6515. bool Do;
  6516. UnicodeString Message;
  6517. if (E == NULL)
  6518. {
  6519. Message = Str;
  6520. Do = true;
  6521. }
  6522. else
  6523. {
  6524. Do = ExceptionMessage(E, Message);
  6525. }
  6526. if (Do && WinConfiguration->BalloonNotifications)
  6527. {
  6528. UnicodeString Title;
  6529. if ((Terminal == NULL) && (Type == qtInformation) && ExtractMainInstructions(Message, Title))
  6530. {
  6531. Message = TrimLeft(Message);
  6532. }
  6533. else
  6534. {
  6535. Message = UnformatMessage(Message);
  6536. const ResourceString * Captions[] = { &_SMsgDlgConfirm, &_SMsgDlgWarning,
  6537. &_SMsgDlgError, &_SMsgDlgInformation, NULL };
  6538. Title = LoadResourceString(Captions[Type]);
  6539. if (Terminal != NULL)
  6540. {
  6541. Title = FORMAT(L"%s - %s",
  6542. (TTerminalManager::Instance()->GetTerminalTitle(Terminal, true), Title));
  6543. }
  6544. }
  6545. if (Seconds == 0)
  6546. {
  6547. Seconds = WinConfiguration->NotificationsTimeout;
  6548. }
  6549. FTrayIcon->PopupBalloon(Title, Message, Type, Seconds * MSecsPerSec, OnBalloonClick, UserData);
  6550. }
  6551. }
  6552. //---------------------------------------------------------------------------
  6553. unsigned int __fastcall TCustomScpExplorerForm::MoreMessageDialog(const UnicodeString Message,
  6554. TStrings * MoreMessages, TQueryType Type, unsigned int Answers,
  6555. UnicodeString HelpKeyword, const TMessageParams * Params,
  6556. TTerminal * Terminal)
  6557. {
  6558. if (((WinConfiguration->ContinueOnError && (FErrorList != NULL)) ||
  6559. (FOnFeedSynchronizeError != NULL)) &&
  6560. (Params != NULL) && (Params->Params & mpAllowContinueOnError) )
  6561. {
  6562. if (FOnFeedSynchronizeError != NULL)
  6563. {
  6564. FOnFeedSynchronizeError(Message, MoreMessages, Type, HelpKeyword);
  6565. }
  6566. else
  6567. {
  6568. DebugAssert(FErrorList != NULL);
  6569. TStringList * MoreMessagesCopy = NULL;
  6570. if (MoreMessages)
  6571. {
  6572. MoreMessagesCopy = new TStringList();
  6573. MoreMessagesCopy->Assign(MoreMessages);
  6574. }
  6575. FErrorList->AddObject(Message, MoreMessagesCopy);
  6576. }
  6577. PopupTrayBalloon(Terminal, Message, Type);
  6578. return ContinueAnswer(Answers);
  6579. }
  6580. else
  6581. {
  6582. bool UseBalloon = FTrayIcon->Visible;
  6583. if (UseBalloon)
  6584. {
  6585. PopupTrayBalloon(Terminal, Message, Type);
  6586. }
  6587. unsigned int Result;
  6588. try
  6589. {
  6590. Result = ::MoreMessageDialog(Message, MoreMessages, Type, Answers, HelpKeyword, Params);
  6591. }
  6592. __finally
  6593. {
  6594. // cancel only balloon we popped up, otherwise we may cancel notification
  6595. // balloon that was there before the message dialog
  6596. // (such as "dd confirmation opt in balloon)
  6597. if (UseBalloon)
  6598. {
  6599. FTrayIcon->CancelBalloon();
  6600. }
  6601. }
  6602. return Result;
  6603. }
  6604. }
  6605. //---------------------------------------------------------------------------
  6606. void __fastcall TCustomScpExplorerForm::ShowExtendedException(
  6607. TTerminal * Terminal, Exception * E)
  6608. {
  6609. if (FTrayIcon->Visible)
  6610. {
  6611. PopupTrayBalloon(Terminal, L"", qtError, E);
  6612. }
  6613. // particularly prevent opening new session from jump list,
  6614. // while exception is shown
  6615. NonVisualDataModule->StartBusy();
  6616. try
  6617. {
  6618. ShowExtendedExceptionEx(Terminal, E);
  6619. }
  6620. __finally
  6621. {
  6622. NonVisualDataModule->EndBusy();
  6623. FTrayIcon->CancelBalloon();
  6624. }
  6625. }
  6626. //---------------------------------------------------------------------------
  6627. void __fastcall TCustomScpExplorerForm::TerminalReady()
  6628. {
  6629. if (Terminal->Active)
  6630. {
  6631. InitStatusBar();
  6632. }
  6633. // cannot rely on active page being page for active terminal,
  6634. // as it can happen that active page is the "new session" page
  6635. // (e.g. when reconnecting active terminal, while login dialog
  6636. // invoked from "new session" page is modal)
  6637. int ActiveTerminalIndex = TTerminalManager::Instance()->ActiveTerminalIndex;
  6638. UpdateSessionTab(SessionsPageControl->Pages[ActiveTerminalIndex]);
  6639. }
  6640. //---------------------------------------------------------------------------
  6641. void __fastcall TCustomScpExplorerForm::InactiveTerminalException(
  6642. TTerminal * Terminal, Exception * E)
  6643. {
  6644. Notify(Terminal, L"", qtError, false, NULL, NULL, E);
  6645. if (!Terminal->Active)
  6646. {
  6647. int Index = TTerminalManager::Instance()->IndexOf(Terminal);
  6648. if (DebugAlwaysTrue((Index >= 0) && (Index < SessionsPageControl->PageCount)))
  6649. {
  6650. UpdateSessionTab(SessionsPageControl->Pages[Index]);
  6651. }
  6652. }
  6653. }
  6654. //---------------------------------------------------------------------------
  6655. void __fastcall TCustomScpExplorerForm::Notify(TTerminal * Terminal,
  6656. UnicodeString Message, TQueryType Type,
  6657. bool Important, TNotifyEvent OnClick, TObject * UserData, Exception * E)
  6658. {
  6659. if ((E == NULL) ||
  6660. ExceptionMessage(E, Message))
  6661. {
  6662. unsigned int Seconds = WinConfiguration->NotificationsTimeout;
  6663. if (Important)
  6664. {
  6665. Seconds *= 5;
  6666. }
  6667. UnicodeString NoteMessage(UnformatMessage(Message));
  6668. if (Terminal != NULL)
  6669. {
  6670. NoteMessage = FORMAT(L"%s: %s",
  6671. (TTerminalManager::Instance()->GetTerminalTitle(Terminal, true), NoteMessage));
  6672. }
  6673. if (WinConfiguration->BalloonNotifications)
  6674. {
  6675. AddNote(NoteMessage);
  6676. PopupTrayBalloon(Terminal, Message, Type, NULL, Seconds, OnClick, UserData);
  6677. }
  6678. else
  6679. {
  6680. FlashOnBackground();
  6681. PostNote(NoteMessage, Seconds, OnClick, UserData);
  6682. }
  6683. }
  6684. }
  6685. //---------------------------------------------------------------------------
  6686. void __fastcall TCustomScpExplorerForm::QueueEmptyNoteClicked(TObject * Sender)
  6687. {
  6688. RestoreApp();
  6689. TTerminalNoteData * TerminalNoteData = dynamic_cast<TTerminalNoteData *>(Sender);
  6690. if (DebugAlwaysTrue(TerminalNoteData != NULL) &&
  6691. !NonVisualDataModule->Busy)
  6692. {
  6693. TManagedTerminal * Terminal = TerminalNoteData->Terminal;
  6694. TTerminalManager::Instance()->ActiveTerminal = Terminal;
  6695. if (!ComponentVisible[fcQueueView])
  6696. {
  6697. ToggleQueueVisibility();
  6698. GoToQueue();
  6699. }
  6700. }
  6701. }
  6702. //---------------------------------------------------------------------------
  6703. void __fastcall TCustomScpExplorerForm::QueueEvent(TManagedTerminal * ATerminal,
  6704. TTerminalQueue * /*Queue*/, TQueueEvent Event)
  6705. {
  6706. UnicodeString Message;
  6707. TNotifyEvent OnClick = NULL;
  6708. TObject * UserData = NULL;
  6709. bool QueueInvisible = !ComponentVisible[fcQueueView] || IsApplicationMinimized();
  6710. switch (Event)
  6711. {
  6712. case qeEmptyButMonitored:
  6713. if ((ATerminal != Terminal) || QueueInvisible)
  6714. {
  6715. Message = LoadStr(BALLOON_QUEUE_EMPTY);
  6716. OnClick = QueueEmptyNoteClicked;
  6717. TTerminalNoteData * TerminalNoteData = new TTerminalNoteData();
  6718. TerminalNoteData->Terminal = ATerminal;
  6719. UserData = TerminalNoteData;
  6720. }
  6721. break;
  6722. case qeEmpty:
  6723. OperationComplete(ATerminal->QueueOperationStart);
  6724. break;
  6725. case qePendingUserAction:
  6726. if ((ATerminal != Terminal) ||
  6727. (QueueInvisible && !IsQueueAutoPopup()))
  6728. {
  6729. Message = LoadStr(BALLOON_QUEUE_USER_ACTION);
  6730. }
  6731. break;
  6732. default:
  6733. DebugFail();
  6734. }
  6735. if (!Message.IsEmpty())
  6736. {
  6737. Notify(ATerminal, Message, qtInformation, false, OnClick, UserData);
  6738. }
  6739. }
  6740. //---------------------------------------------------------------------------
  6741. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDCreateDragFileList(
  6742. TObject * /*Sender*/, TFileList * FileList, bool & Created)
  6743. {
  6744. if (FDDExtMapFile != NULL)
  6745. {
  6746. CloseHandle(FDDExtMapFile);
  6747. FDDExtMapFile = NULL;
  6748. }
  6749. if (WinConfiguration->DDFakeFile)
  6750. {
  6751. DDFakeFileInitDrag(FileList, Created);
  6752. }
  6753. }
  6754. //---------------------------------------------------------------------------
  6755. void __fastcall TCustomScpExplorerForm::DDFakeCreated(TObject * /*Sender*/, const UnicodeString FileName)
  6756. {
  6757. if (DebugAlwaysTrue(!FDragFakeDirectory.IsEmpty()) &&
  6758. SameText(ExtractFileName(FileName), ExtractFileName(FDragFakeDirectory)))
  6759. {
  6760. FFakeFileDropTarget = FileName;
  6761. }
  6762. }
  6763. //---------------------------------------------------------------------------
  6764. UnicodeString __fastcall TCustomScpExplorerForm::CreateFakeTransferDirectory()
  6765. {
  6766. UnicodeString Result = ExcludeTrailingBackslash(WinConfiguration->TemporaryDir());
  6767. if (!ForceDirectories(ApiPath(Result)))
  6768. {
  6769. throw Exception(FMTLOAD(CREATE_TEMP_DIR_ERROR, (Result)));
  6770. }
  6771. FileSetAttr(ApiPath(Result), faHidden);
  6772. return Result;
  6773. }
  6774. //---------------------------------------------------------------------------
  6775. void __fastcall TCustomScpExplorerForm::DDFakeFileInitDrag(TFileList * FileList,
  6776. bool & Created)
  6777. {
  6778. FFakeFileDropTarget = UnicodeString();
  6779. FDragFakeDirectory = CreateFakeTransferDirectory();
  6780. FileList->AddItem(NULL, FDragFakeDirectory);
  6781. Created = true;
  6782. if (!WinConfiguration->IsDDExtRunning() || WinConfiguration->IsDDExtBroken())
  6783. {
  6784. FDragFakeMonitors = StartCreationDirectoryMonitorsOnEachDrive(FILE_NOTIFY_CHANGE_DIR_NAME, DDFakeCreated);
  6785. }
  6786. FDDExtMapFile = CreateFileMappingA((HANDLE)0xFFFFFFFF, NULL, PAGE_READWRITE,
  6787. 0, sizeof(TDragExtCommStruct), AnsiString(DRAG_EXT_MAPPING).c_str());
  6788. {
  6789. TMutexGuard Guard(FDDExtMutex, DRAGEXT_MUTEX_RELEASE_TIMEOUT);
  6790. TDragExtCommStruct* CommStruct;
  6791. CommStruct = static_cast<TDragExtCommStruct*>(MapViewOfFile(FDDExtMapFile,
  6792. FILE_MAP_ALL_ACCESS, 0, 0, 0));
  6793. DebugAssert(CommStruct != NULL);
  6794. CommStruct->Version = TDragExtCommStruct::CurrentVersion;
  6795. CommStruct->Dragging = true;
  6796. wcsncpy(CommStruct->DropDest, FDragFakeDirectory.c_str(),
  6797. LENOF(CommStruct->DropDest));
  6798. NULL_TERMINATE(CommStruct->DropDest);
  6799. UnmapViewOfFile(CommStruct);
  6800. }
  6801. }
  6802. //---------------------------------------------------------------------------
  6803. bool __fastcall TCustomScpExplorerForm::RemoteFileControlFileOperation(
  6804. TObject * Sender, TFileOperation Operation, bool NoConfirmation, void * Param)
  6805. {
  6806. bool Result;
  6807. DebugAssert(!IsLocalBrowserMode());
  6808. if (Sender == RemoteDirView)
  6809. {
  6810. Result = ExecuteFileOperation(Operation, osRemote, true, NoConfirmation, Param);
  6811. }
  6812. else
  6813. {
  6814. DebugAssert(Sender == RemoteDriveView);
  6815. TStrings * FileList = RemoteDriveView->DragFileList();
  6816. try
  6817. {
  6818. Result = ExecuteFileOperation(Operation, osRemote, FileList, NoConfirmation, Param);
  6819. }
  6820. __finally
  6821. {
  6822. delete FileList;
  6823. }
  6824. }
  6825. if (FDDTargetControl == RemoteDriveView)
  6826. {
  6827. RemoteDriveView->UpdateDropTarget();
  6828. }
  6829. // foRemoteMove happens when file/dir is dragged within the remote tree view
  6830. // or from tree view to dir view.
  6831. // foMove happens when file/dir is dragged from remote tree view outside of
  6832. // application via dragex
  6833. if (((Operation == foRemoteMove) || (Operation == foMove)) &&
  6834. (DropSourceControl == RemoteDriveView))
  6835. {
  6836. RemoteDriveView->UpdateDropSource();
  6837. }
  6838. return Result;
  6839. }
  6840. //---------------------------------------------------------------------------
  6841. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDEnd(TObject * Sender)
  6842. {
  6843. DebugAssert(!IsLocalBrowserMode());
  6844. // This also handles drops of remote files to queue.
  6845. // Drops of local files (uploads) are handled in QueueDDProcessDropped.
  6846. SAFE_DESTROY(FDDFileList);
  6847. if (!FFakeFileDropTarget.IsEmpty())
  6848. {
  6849. RemoveDir(ApiPath(FFakeFileDropTarget));
  6850. }
  6851. if ((FDDExtMapFile != NULL) || (FDDTargetControl == QueueView3))
  6852. {
  6853. try
  6854. {
  6855. TDragResult DDResult = (Sender == RemoteDirView) ?
  6856. RemoteDirView->LastDDResult : RemoteDriveView->LastDDResult;
  6857. // Focus is moved to the target application,
  6858. // but as we are going to present the UI, we need to steal the focus back.
  6859. // This most likely won't work though (windows does not allow application
  6860. // to steal focus most of the time)
  6861. Application->BringToFront();
  6862. // On older version of Windows we never got drMove here, see also comment below.
  6863. // On Windows 10, we get the "move".
  6864. if ((DDResult == drCopy) || (DDResult == drMove) || (DDResult == drInvalid))
  6865. {
  6866. UnicodeString TargetDirectory;
  6867. TFileOperation Operation;
  6868. // drInvalid may mean drMove, see comment below
  6869. switch (DDResult)
  6870. {
  6871. case drCopy:
  6872. Operation = foCopy;
  6873. break;
  6874. case drMove:
  6875. Operation = foMove;
  6876. break;
  6877. default:
  6878. DebugFail();
  6879. case drInvalid:
  6880. // prefer "copy" for safety
  6881. Operation = FLAGSET(FLastDropEffect, DROPEFFECT_MOVE) ? foMove : foCopy;
  6882. break;
  6883. }
  6884. TTransferOperationParam Param;
  6885. UnicodeString CounterName;
  6886. bool ForceQueue;
  6887. if (!DDGetTarget(Param.TargetDirectory, ForceQueue, CounterName))
  6888. {
  6889. // we get drInvalid both if d&d was intercepted by ddext,
  6890. // and when users drops on no-drop location.
  6891. // we tell the difference by existence of response from ddext,
  6892. // so we ignore absence of response in this case
  6893. if (DDResult != drInvalid)
  6894. {
  6895. // here we know that the extension is installed,
  6896. // as it is checked as soon as drag&drop starts from
  6897. // RemoteFileControlDDCreateDragFileList
  6898. Configuration->Usage->Inc(L"DownloadsDragDropExternalExtTargetUnknown");
  6899. if (!WinConfiguration->DDExtInstalled || WinConfiguration->IsDDExtBroken())
  6900. {
  6901. throw ExtException(NULL, LoadStr(DD_TARGET_UNKNOWN), HELP_DD_TARGET_UNKNOWN);
  6902. }
  6903. else
  6904. {
  6905. throw ExtException(NULL, LoadStr(DRAGEXT_TARGET_UNKNOWN2), HELP_DRAGEXT_TARGET_UNKNOWN);
  6906. }
  6907. }
  6908. }
  6909. else
  6910. {
  6911. // download using fake file
  6912. Param.Temp = false;
  6913. Param.DragDrop = true;
  6914. if (ForceQueue)
  6915. {
  6916. Param.Queue = asOn;
  6917. }
  6918. if (Sender == RemoteDirView)
  6919. {
  6920. Param.Options = FLAGMASK(SelectedAllFilesInDirView(RemoteDirView), coAllFiles);
  6921. }
  6922. if (RemoteFileControlFileOperation(Sender, Operation,
  6923. (WinConfiguration->DDTransferConfirmation == asOff), &Param))
  6924. {
  6925. Configuration->Usage->Inc(CounterName);
  6926. }
  6927. }
  6928. }
  6929. }
  6930. __finally
  6931. {
  6932. CloseHandle(FDDExtMapFile);
  6933. FDDExtMapFile = NULL;
  6934. delete FDragFakeMonitors;
  6935. FDragFakeMonitors = NULL;
  6936. RemoveDir(ApiPath(FDragFakeDirectory));
  6937. FDragFakeDirectory = L"";
  6938. FFakeFileDropTarget = UnicodeString();
  6939. }
  6940. }
  6941. if (FDragDropSshTerminate.get() != NULL)
  6942. {
  6943. FDragDropSshTerminate->Rethrow();
  6944. }
  6945. }
  6946. //---------------------------------------------------------------------------
  6947. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDGiveFeedback(
  6948. TObject * /*Sender*/, int dwEffect, HRESULT & /*Result*/)
  6949. {
  6950. // Remember drop effect so we know (when user drops files), if we copy or move
  6951. FLastDropEffect = dwEffect;
  6952. }
  6953. //---------------------------------------------------------------------------
  6954. bool __fastcall TCustomScpExplorerForm::DDGetTarget(
  6955. UnicodeString & Directory, bool & ForceQueue, UnicodeString & CounterName)
  6956. {
  6957. bool Result;
  6958. if (FDDTargetControl == QueueView3)
  6959. {
  6960. Directory = DefaultDownloadTargetDirectory();
  6961. Result = true;
  6962. CounterName = L"DownloadsDragDropQueue";
  6963. ForceQueue = true;
  6964. }
  6965. else if (!FFakeFileDropTarget.IsEmpty())
  6966. {
  6967. Directory = ExcludeTrailingBackslash(ExtractFilePath(FFakeFileDropTarget));
  6968. Result = true;
  6969. ForceQueue = false;
  6970. CounterName = L"DownloadsDragDropFakeFile";
  6971. }
  6972. else
  6973. {
  6974. ForceQueue = false;
  6975. Enabled = false;
  6976. try
  6977. {
  6978. int Timer = 0;
  6979. Result = false;
  6980. while (!Result && (Timer < WinConfiguration->DDExtTimeout))
  6981. {
  6982. {
  6983. TMutexGuard Guard(FDDExtMutex, DRAGEXT_MUTEX_RELEASE_TIMEOUT);
  6984. TDragExtCommStruct* CommStruct;
  6985. CommStruct = static_cast<TDragExtCommStruct*>(MapViewOfFile(FDDExtMapFile,
  6986. FILE_MAP_ALL_ACCESS, 0, 0, 0));
  6987. DebugAssert(CommStruct != NULL);
  6988. Result = !CommStruct->Dragging;
  6989. if (Result)
  6990. {
  6991. Directory = ExtractFilePath(CommStruct->DropDest);
  6992. CounterName = L"DownloadsDragDropExternalExt";
  6993. }
  6994. UnmapViewOfFile(CommStruct);
  6995. }
  6996. if (!Result)
  6997. {
  6998. Sleep(50);
  6999. Timer += 50;
  7000. Application->ProcessMessages();
  7001. }
  7002. }
  7003. }
  7004. __finally
  7005. {
  7006. Enabled = true;
  7007. }
  7008. }
  7009. return Result;
  7010. }
  7011. //---------------------------------------------------------------------------
  7012. void __fastcall TCustomScpExplorerForm::AddDelayedDirectoryDeletion(
  7013. const UnicodeString TempDir, int SecDelay)
  7014. {
  7015. TDateTime Alarm = Now() + (double)((double)SecDelay*OneMillisecond);
  7016. FDelayedDeletionList->AddObject(TempDir, reinterpret_cast<TObject*>(Alarm.FileDate()));
  7017. if (FDelayedDeletionTimer == NULL)
  7018. {
  7019. DebugAssert(HandleAllocated());
  7020. FDelayedDeletionTimer = new TTimer(this);
  7021. FDelayedDeletionTimer->Interval = 10000;
  7022. FDelayedDeletionTimer->OnTimer = DoDelayedDeletion;
  7023. }
  7024. else
  7025. {
  7026. FDelayedDeletionTimer->Enabled = true;
  7027. }
  7028. }
  7029. //---------------------------------------------------------------------------
  7030. void __fastcall TCustomScpExplorerForm::DoDelayedDeletion(TObject * Sender)
  7031. {
  7032. DebugAssert(FDelayedDeletionList != NULL);
  7033. TDateTime N = Now();
  7034. TDateTime Alert;
  7035. UnicodeString Directory;
  7036. for (int Index = FDelayedDeletionList->Count-1; Index >= 0; Index--)
  7037. {
  7038. Alert = FileDateToDateTime(reinterpret_cast<int>(FDelayedDeletionList->Objects[Index]));
  7039. if ((N >= Alert) || (Sender == NULL))
  7040. {
  7041. Directory = FDelayedDeletionList->Strings[Index];
  7042. if (DeleteDirectory(ExcludeTrailingBackslash(Directory)))
  7043. {
  7044. FDelayedDeletionList->Delete(Index);
  7045. }
  7046. }
  7047. }
  7048. if (FDelayedDeletionList->Count == 0)
  7049. {
  7050. FDelayedDeletionTimer->Enabled = false;
  7051. }
  7052. }
  7053. //---------------------------------------------------------------------------
  7054. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDTargetDrop()
  7055. {
  7056. DebugAssert(!IsLocalBrowserMode());
  7057. if (IsFileControl(FDDTargetControl, osRemote) ||
  7058. (FDDTargetControl == SessionsPageControl))
  7059. {
  7060. TTerminal * TargetTerminal = NULL;
  7061. TFileOperation Operation = foNone;
  7062. if (FDDTargetControl == SessionsPageControl)
  7063. {
  7064. TPoint Point = SessionsPageControl->ScreenToClient(Mouse->CursorPos);
  7065. int Index = SessionsPageControl->IndexOfTabAt(Point.X, Point.Y);
  7066. // do not allow dropping on the "+" tab
  7067. TargetTerminal = GetSessionTabTerminal(SessionsPageControl->Pages[Index]);
  7068. if (TargetTerminal != NULL)
  7069. {
  7070. if ((FLastDropEffect == DROPEFFECT_MOVE) &&
  7071. (TargetTerminal == TTerminalManager::Instance()->ActiveTerminal))
  7072. {
  7073. Operation = foRemoteMove;
  7074. }
  7075. else
  7076. {
  7077. Operation = foRemoteCopy;
  7078. }
  7079. }
  7080. }
  7081. else
  7082. {
  7083. if (FLastDropEffect == DROPEFFECT_MOVE)
  7084. {
  7085. Operation = foRemoteMove;
  7086. }
  7087. else if (FLastDropEffect == DROPEFFECT_COPY)
  7088. {
  7089. Operation = foRemoteCopy;
  7090. }
  7091. }
  7092. if (Operation != foNone)
  7093. {
  7094. RemoteFileControlFileOperation(DropSourceControl,
  7095. Operation, (WinConfiguration->DDTransferConfirmation == asOff), TargetTerminal);
  7096. }
  7097. // abort drag&drop
  7098. Abort();
  7099. }
  7100. else if ((FDDExtMapFile == NULL) && (FLastDropEffect != DROPEFFECT_NONE) &&
  7101. // Drops of remote files to queue are handled in RemoteFileControlDDEnd
  7102. (FDDTargetControl != QueueView3))
  7103. {
  7104. DebugAssert(!FDragTempDir.IsEmpty());
  7105. TTransferType Type;
  7106. UnicodeString TempDir = FDragTempDir;
  7107. // We clear FDragTempDir before calling
  7108. // just in case it fail (raises exception)
  7109. FDragTempDir = L"";
  7110. Type = (FLastDropEffect & DROPEFFECT_MOVE ? ttMove : Type = ttCopy);
  7111. TGUICopyParamType CopyParams = GUIConfiguration->CurrentCopyParam;
  7112. // empty directory parameter means temp directory -> don't display it!
  7113. UnicodeString TargetDir = L"";
  7114. int Options = 0;
  7115. if (!CopyParamDialog(tdToLocal, Type, true, FDDFileList,
  7116. TargetDir, CopyParams, (WinConfiguration->DDTransferConfirmation != asOff), true, Options))
  7117. {
  7118. Abort();
  7119. }
  7120. // TargetDir is set when dropped on local file control
  7121. // (this was workaround for legacy dirview event handling, now it should be
  7122. // made prettier)
  7123. if (TargetDir.IsEmpty())
  7124. {
  7125. TargetDir = TempDir;
  7126. if (ForceDirectories(ApiPath(TargetDir)))
  7127. {
  7128. DebugAssert(Terminal && !TargetDir.IsEmpty());
  7129. FPendingTempSpaceWarn = true;
  7130. try
  7131. {
  7132. FDragDropOperation = true;
  7133. int Params = cpTemporary |
  7134. (Type == ttMove ? cpDelete : 0);
  7135. DDDownload(FDDFileList, TargetDir, &CopyParams, Params);
  7136. Configuration->Usage->Inc(L"DownloadsDragDropExternalTemp");
  7137. }
  7138. __finally
  7139. {
  7140. FDragDropOperation = false;
  7141. FPendingTempSpaceWarn = false;
  7142. AddDelayedDirectoryDeletion(TargetDir, WinConfiguration->DDDeleteDelay);
  7143. }
  7144. }
  7145. else
  7146. {
  7147. throw Exception(FMTLOAD(CREATE_TEMP_DIR_ERROR, (TargetDir)));
  7148. }
  7149. }
  7150. }
  7151. }
  7152. //---------------------------------------------------------------------------
  7153. void __fastcall TCustomScpExplorerForm::DDDownload(
  7154. TStrings * FilesToCopy, const UnicodeString & TargetDir, const TCopyParamType * CopyParam, int Params)
  7155. {
  7156. DebugAssert(!IsLocalBrowserMode());
  7157. TAutoBatch AutoBatch(this);
  7158. UpdateCopyParamCounters(*CopyParam);
  7159. Terminal->CopyToLocal(FilesToCopy, TargetDir, CopyParam, Params, NULL);
  7160. if (FLAGSET(Params, cpDelete) && (DropSourceControl == RemoteDriveView))
  7161. {
  7162. RemoteDriveView->UpdateDropSource();
  7163. }
  7164. }
  7165. //---------------------------------------------------------------------------
  7166. class TFakeDataObjectFilesEx : public TDataObjectFilesEx
  7167. {
  7168. public:
  7169. __fastcall TFakeDataObjectFilesEx(TFileList * AFileList, bool RenderPIDL,
  7170. bool RenderFilename) : TDataObjectFilesEx(AFileList, RenderPIDL, RenderFilename, true)
  7171. {
  7172. }
  7173. virtual bool __fastcall AllowData(const tagFORMATETC & FormatEtc)
  7174. {
  7175. return (FormatEtc.cfFormat == CF_HDROP) ? false :
  7176. TDataObjectFilesEx::AllowData(FormatEtc);
  7177. }
  7178. };
  7179. //---------------------------------------------------------------------------
  7180. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDCreateDataObject(
  7181. TObject * Sender, TDataObject *& DataObject)
  7182. {
  7183. if (FDDExtMapFile != NULL)
  7184. {
  7185. TFileList * FileList = DragDropFiles(Sender)->FileList;
  7186. if (!FileList->RenderPIDLs() || !FileList->RenderNames())
  7187. {
  7188. Abort();
  7189. }
  7190. if (FileList->Count > 0)
  7191. {
  7192. TDataObjectFilesEx * FilesObject = new TFakeDataObjectFilesEx(FileList, true, true);
  7193. if (!FilesObject->IsValid(true, true))
  7194. {
  7195. FilesObject->_Release();
  7196. }
  7197. else
  7198. {
  7199. DataObject = FilesObject;
  7200. }
  7201. }
  7202. }
  7203. }
  7204. //---------------------------------------------------------------------------
  7205. void __fastcall TCustomScpExplorerForm::GoToCommandLine()
  7206. {
  7207. DebugFail();
  7208. }
  7209. //---------------------------------------------------------------------------
  7210. void __fastcall TCustomScpExplorerForm::GoToTree()
  7211. {
  7212. ComponentVisible[fcRemoteTree] = true;
  7213. DriveView(osRemote)->SetFocus();
  7214. }
  7215. //---------------------------------------------------------------------------
  7216. TStrings * __fastcall TCustomScpExplorerForm::PanelExport(TOperationSide Side,
  7217. TPanelExport Export)
  7218. {
  7219. TCustomDirView * DirView = this->DirView(Side);
  7220. std::unique_ptr<TStrings> ExportData(new TStringList());
  7221. switch (Export)
  7222. {
  7223. case pePath:
  7224. ExportData->Add(DirView->PathName);
  7225. break;
  7226. case peFileList:
  7227. case peFullFileList:
  7228. {
  7229. bool FullPath = (Export == peFullFileList);
  7230. DirView->CreateFileList(false, FullPath, ExportData.get());
  7231. for (int Index = 0; Index < ExportData->Count; Index++)
  7232. {
  7233. if (ExportData->Strings[Index].Pos(L" ") > 0)
  7234. {
  7235. ExportData->Strings[Index] = FORMAT(L"\"%s\"", (ExportData->Strings[Index]));
  7236. }
  7237. }
  7238. }
  7239. break;
  7240. default:
  7241. DebugFail();
  7242. }
  7243. return ExportData.release();
  7244. }
  7245. //---------------------------------------------------------------------------
  7246. void __fastcall TCustomScpExplorerForm::PanelExport(TOperationSide Side,
  7247. TPanelExport Export, TPanelExportDestination Destination)
  7248. {
  7249. std::unique_ptr<TStrings> ExportData(PanelExport(Side, Export));
  7250. PanelExportStore(Side, Export, Destination, ExportData.get());
  7251. }
  7252. //---------------------------------------------------------------------------
  7253. void __fastcall TCustomScpExplorerForm::PanelExportStore(TOperationSide /*Side*/,
  7254. TPanelExport /*Export*/, TPanelExportDestination Destination,
  7255. TStrings * ExportData)
  7256. {
  7257. if (Destination == pedClipboard)
  7258. {
  7259. TInstantOperationVisualizer Visualizer;
  7260. CopyToClipboard(ExportData);
  7261. }
  7262. else
  7263. {
  7264. DebugFail();
  7265. }
  7266. }
  7267. //---------------------------------------------------------------------------
  7268. void __fastcall TCustomScpExplorerForm::Filter(TOperationSide Side)
  7269. {
  7270. TCustomDirView * DirView = this->DirView(Side);
  7271. UnicodeString Mask = DirView->Mask;
  7272. if (DoFilterMaskDialog(DirView, Mask))
  7273. {
  7274. DirView->Mask = TFileMasks::NormalizeMask(Mask);
  7275. Configuration->Usage->Inc(L"Filters");
  7276. }
  7277. }
  7278. //---------------------------------------------------------------------------
  7279. TQueueOperation __fastcall TCustomScpExplorerForm::DefaultQueueOperation()
  7280. {
  7281. return FQueueController->DefaultOperation();
  7282. }
  7283. //---------------------------------------------------------------------------
  7284. bool __fastcall TCustomScpExplorerForm::AllowQueueOperation(
  7285. TQueueOperation Operation, void ** Param)
  7286. {
  7287. switch (Operation)
  7288. {
  7289. case qoPreferences:
  7290. return true;
  7291. case qoGoTo:
  7292. return ComponentVisible[fcQueueView] && QueueView3->Enabled;
  7293. case qoOnceEmpty:
  7294. return !FQueueController->Empty;
  7295. default:
  7296. return FQueueController->AllowOperation(Operation, Param);
  7297. }
  7298. }
  7299. //---------------------------------------------------------------------------
  7300. void __fastcall TCustomScpExplorerForm::GoToQueue()
  7301. {
  7302. if (DebugAlwaysTrue(QueueView3->Visible))
  7303. {
  7304. QueueView3->SetFocus();
  7305. }
  7306. }
  7307. //---------------------------------------------------------------------------
  7308. void __fastcall TCustomScpExplorerForm::ExecuteQueueOperation(
  7309. TQueueOperation Operation, void * Param)
  7310. {
  7311. if (Operation == qoGoTo)
  7312. {
  7313. GoToQueue();
  7314. }
  7315. else if (Operation == qoPreferences)
  7316. {
  7317. PreferencesDialog(pmQueue);
  7318. }
  7319. else
  7320. {
  7321. FQueueController->ExecuteOperation(Operation, Param);
  7322. }
  7323. }
  7324. //---------------------------------------------------------------------------
  7325. bool __fastcall TCustomScpExplorerForm::GetQueueEnabled()
  7326. {
  7327. return (Queue != NULL) && Queue->Enabled;
  7328. }
  7329. //---------------------------------------------------------------------------
  7330. void __fastcall TCustomScpExplorerForm::ToggleQueueEnabled()
  7331. {
  7332. DebugAssert(Queue != NULL);
  7333. if (Queue != NULL)
  7334. {
  7335. Queue->Enabled = !Queue->Enabled;
  7336. }
  7337. }
  7338. //---------------------------------------------------------------------------
  7339. void __fastcall TCustomScpExplorerForm::QueueView3ContextPopup(
  7340. TObject * /*Sender*/, TPoint & /*MousePos*/, bool & /*Handled*/)
  7341. {
  7342. FQueueActedItem = QueueView3->ItemFocused;
  7343. }
  7344. //---------------------------------------------------------------------------
  7345. /*virtual*/ int __fastcall TCustomScpExplorerForm::GetStaticComponentsHeight()
  7346. {
  7347. return TopDock->Height + QueueSplitter->Height;
  7348. }
  7349. //---------------------------------------------------------------------------
  7350. void __fastcall TCustomScpExplorerForm::QueueSplitterCanResize(
  7351. TObject * /*Sender*/, int & NewSize, bool & Accept)
  7352. {
  7353. // when queue is hidden by double-clicking splitter, stray attempt to
  7354. // resize the panel with strange value arrives, make sure it is ignored
  7355. if (ComponentVisible[fcQueueView])
  7356. {
  7357. int HeightLimit = ClientHeight - GetStaticComponentsHeight() -
  7358. RemotePanel->Constraints->MinHeight;
  7359. if (NewSize > HeightLimit)
  7360. {
  7361. NewSize = HeightLimit;
  7362. }
  7363. }
  7364. else
  7365. {
  7366. Accept = false;
  7367. }
  7368. }
  7369. //---------------------------------------------------------------------------
  7370. void __fastcall TCustomScpExplorerForm::QueueView3StartDrag(TObject * /*Sender*/,
  7371. TDragObject *& /*DragObject*/)
  7372. {
  7373. FQueueActedItem = QueueView3->ItemFocused;
  7374. }
  7375. //---------------------------------------------------------------------------
  7376. void __fastcall TCustomScpExplorerForm::QueueView3DragOver(TObject * /*Sender*/,
  7377. TObject * Source, int X, int Y, TDragState /*State*/, bool & Accept)
  7378. {
  7379. Accept = true;
  7380. if (Source == QueueView3)
  7381. {
  7382. TListItem * DropTarget = QueueView3->GetItemAt(X, Y);
  7383. Accept = (DropTarget != NULL) && (FQueueActedItem != NULL);
  7384. if (Accept)
  7385. {
  7386. TQueueItemProxy * QueueItem;
  7387. TQueueItemProxy * DestQueueItem;
  7388. QueueItem = static_cast<TQueueItemProxy *>(FQueueActedItem->Data);
  7389. DestQueueItem = static_cast<TQueueItemProxy *>(DropTarget->Data);
  7390. Accept = (QueueItem != DestQueueItem) &&
  7391. (QueueItem->Status == TQueueItem::qsPending) &&
  7392. (DestQueueItem->Status == TQueueItem::qsPending);
  7393. }
  7394. }
  7395. }
  7396. //---------------------------------------------------------------------------
  7397. void __fastcall TCustomScpExplorerForm::QueueView3DragDrop(TObject * /*Sender*/,
  7398. TObject * /*Source*/, int /*X*/, int /*Y*/)
  7399. {
  7400. if ((FQueueActedItem != NULL) && (QueueView3->DropTarget != NULL))
  7401. {
  7402. TQueueItemProxy * QueueItem;
  7403. TQueueItemProxy * DestQueueItem;
  7404. QueueItem = static_cast<TQueueItemProxy *>(FQueueActedItem->Data);
  7405. DestQueueItem = static_cast<TQueueItemProxy *>(QueueView3->DropTarget->Data);
  7406. QueueItem->Move(DestQueueItem);
  7407. }
  7408. }
  7409. //---------------------------------------------------------------------------
  7410. void __fastcall TCustomScpExplorerForm::QueueView3Enter(TObject * /*Sender*/)
  7411. {
  7412. if ((QueueView3->ItemFocused == NULL) &&
  7413. (QueueView3->Items->Count > 0))
  7414. {
  7415. QueueView3->ItemFocused = QueueView3->Items->Item[0];
  7416. }
  7417. QueueLabelUpdateStatus();
  7418. }
  7419. //---------------------------------------------------------------------------
  7420. void __fastcall TCustomScpExplorerForm::QueueView3Exit(TObject * /*Sender*/)
  7421. {
  7422. QueueLabelUpdateStatus();
  7423. }
  7424. //---------------------------------------------------------------------------
  7425. void __fastcall TCustomScpExplorerForm::QueueLabelUpdateStatus()
  7426. {
  7427. QueueLabel->UpdateStatus();
  7428. }
  7429. //---------------------------------------------------------------------------
  7430. void __fastcall TCustomScpExplorerForm::QueueView3SelectItem(
  7431. TObject * /*Sender*/, TListItem * /*Item*/, bool Selected)
  7432. {
  7433. if (Selected)
  7434. {
  7435. NonVisualDataModule->UpdateNonVisibleActions();
  7436. }
  7437. }
  7438. //---------------------------------------------------------------------------
  7439. TDragDropFilesEx * __fastcall TCustomScpExplorerForm::DragDropFiles(TObject * Sender)
  7440. {
  7441. TDragDropFilesEx * Result = NULL;
  7442. if (Sender == SessionsPageControl)
  7443. {
  7444. Result = FSessionsDragDropFilesEx;
  7445. }
  7446. else if (Sender == QueueView3)
  7447. {
  7448. Result = FQueueDragDropFilesEx;
  7449. }
  7450. else
  7451. {
  7452. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  7453. if (DirView != NULL)
  7454. {
  7455. Result = DirView->DragDropFilesEx;
  7456. }
  7457. else
  7458. {
  7459. TCustomDriveView * DriveView = dynamic_cast<TCustomDriveView *>(Sender);
  7460. if (DriveView != NULL)
  7461. {
  7462. Result = DriveView->DragDropFilesEx;
  7463. }
  7464. }
  7465. }
  7466. DebugAssert(Result != NULL);
  7467. return Result;
  7468. }
  7469. //---------------------------------------------------------------------------
  7470. bool __fastcall TCustomScpExplorerForm::SelectedAllFilesInDirView(TCustomDirView * DView)
  7471. {
  7472. return (DView->SelCount == DView->FilesCount);
  7473. }
  7474. //---------------------------------------------------------------------------
  7475. bool __fastcall TCustomScpExplorerForm::DraggingAllFilesFromDirView(TOperationSide Side, TStrings * FileList)
  7476. {
  7477. return HasDirView[Side] && (DropSourceControl == DirView(Side)) && (FileList->Count == DirView(Side)->FilesCount);
  7478. }
  7479. //---------------------------------------------------------------------------
  7480. void __fastcall TCustomScpExplorerForm::RemoteFileControlDragDropFileOperation(
  7481. TObject * Sender, int Effect, UnicodeString TargetPath, bool ForceQueue, bool DragDrop)
  7482. {
  7483. DebugAssert(!IsLocalBrowserMode());
  7484. TFileOperation Operation;
  7485. switch (Effect)
  7486. {
  7487. case DROPEFFECT_MOVE:
  7488. Operation = foMove;
  7489. break;
  7490. case DROPEFFECT_COPY:
  7491. // occures on WinXP (reported by user)
  7492. default:
  7493. Operation = foCopy;
  7494. break;
  7495. };
  7496. TDragDropFilesEx * DragDropFilesEx = DragDropFiles(Sender);
  7497. // see a comment in TUnixDirView::PerformItemDragDropOperation
  7498. if (DragDropFilesEx->FileList->Count > 0)
  7499. {
  7500. TStrings * FileList = new TStringList();
  7501. try
  7502. {
  7503. for (int Index = 0; Index < DragDropFilesEx->FileList->Count; Index++)
  7504. {
  7505. FileList->Add(DragDropFilesEx->FileList->Items[Index]->Name);
  7506. }
  7507. FDragDropOperation = true;
  7508. TTransferOperationParam Param;
  7509. Param.TargetDirectory = TargetPath;
  7510. // upload, no temp dirs
  7511. Param.Temp = false;
  7512. Param.DragDrop = DragDrop;
  7513. Param.Options =
  7514. FLAGMASK(DraggingAllFilesFromDirView(osLocal, FileList), coAllFiles);
  7515. if (ForceQueue)
  7516. {
  7517. Param.Queue = asOn;
  7518. }
  7519. bool NoConfirmation = DragDrop ? (WinConfiguration->DDTransferConfirmation == asOff) : false;
  7520. if (ExecuteFileOperation(Operation, osLocal, FileList, NoConfirmation, &Param))
  7521. {
  7522. if (IsFileControl(DropSourceControl, osLocal))
  7523. {
  7524. Configuration->Usage->Inc(L"UploadsDragDropInternal");
  7525. }
  7526. else
  7527. {
  7528. Configuration->Usage->Inc(L"UploadsDragDropExternal");
  7529. }
  7530. }
  7531. }
  7532. __finally
  7533. {
  7534. FDragDropOperation = false;
  7535. delete FileList;
  7536. }
  7537. }
  7538. }
  7539. //---------------------------------------------------------------------------
  7540. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDFileOperation(
  7541. TObject * Sender, int Effect, UnicodeString /*SourcePath*/,
  7542. UnicodeString TargetPath, bool Paste, bool & /*DoOperation*/)
  7543. {
  7544. RemoteFileControlDragDropFileOperation(Sender, Effect, TargetPath, false, !Paste);
  7545. }
  7546. //---------------------------------------------------------------------------
  7547. void __fastcall TCustomScpExplorerForm::RemoteFileContolDDChooseEffect(
  7548. TObject * Sender, int grfKeyState, int & dwEffect)
  7549. {
  7550. DebugAssert(!IsLocalBrowserMode());
  7551. // if any drop effect is allowed at all (e.g. no drop to self and drop to parent)
  7552. if ((dwEffect != DROPEFFECT_NONE) &&
  7553. IsFileControl(DropSourceControl, osRemote))
  7554. {
  7555. // do not allow drop on remote panel (on free space, still allow drop on directories)
  7556. if ((Sender == RemoteDirView) && (DropSourceControl == RemoteDirView) &&
  7557. (RemoteDirView->DropTarget == NULL))
  7558. {
  7559. dwEffect = DROPEFFECT_NONE;
  7560. }
  7561. else
  7562. {
  7563. if (dwEffect == DROPEFFECT_COPY)
  7564. {
  7565. bool MoveCapable = FTerminal->IsCapable[fcRemoteMove];
  7566. // currently we support copying always (at least via temporary directory);
  7567. // remove associated checks once this all proves stable and working
  7568. bool CopyCapable = true;
  7569. // if we do not support neither of operations, there's no discussion
  7570. if (!MoveCapable && !CopyCapable)
  7571. {
  7572. dwEffect = DROPEFFECT_NONE;
  7573. }
  7574. else
  7575. {
  7576. // The default effect inside remote panel is move,
  7577. // unless we do not support it, but support copy
  7578. if (FLAGCLEAR(grfKeyState, MK_CONTROL))
  7579. {
  7580. dwEffect = MoveCapable ? DROPEFFECT_MOVE : DROPEFFECT_COPY;
  7581. }
  7582. else
  7583. {
  7584. // with ctrl-down, we want copy unless it is not supported
  7585. dwEffect = CopyCapable ? DROPEFFECT_COPY : DROPEFFECT_NONE;
  7586. }
  7587. }
  7588. }
  7589. }
  7590. }
  7591. }
  7592. //---------------------------------------------------------------------------
  7593. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDDragFileName(
  7594. TObject * Sender, TRemoteFile * File, UnicodeString & FileName)
  7595. {
  7596. DebugAssert(!IsLocalBrowserMode());
  7597. if (FDDTotalSize >= 0)
  7598. {
  7599. if (File->IsDirectory)
  7600. {
  7601. FDDTotalSize = -1;
  7602. }
  7603. else
  7604. {
  7605. FDDTotalSize += File->Size;
  7606. }
  7607. }
  7608. DebugAssert(!FDragTempDir.IsEmpty());
  7609. // TODO: this is quite ineffective
  7610. // TODO: what if invalid character replacement is disabled?
  7611. FileName = FDragTempDir + GUIConfiguration->CurrentCopyParam.ValidLocalFileName(File->FileName);
  7612. UnicodeString TransferFileName;
  7613. if (Sender == RemoteDriveView)
  7614. {
  7615. TransferFileName = UnixExcludeTrailingBackslash(File->FullFileName);
  7616. }
  7617. else
  7618. {
  7619. TransferFileName = File->FileName;
  7620. }
  7621. FDDFileList->AddObject(TransferFileName, File);
  7622. }
  7623. //---------------------------------------------------------------------------
  7624. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDDragDetect(
  7625. TObject * /*Sender*/, int /*grfKeyState*/, const TPoint & /*DetectStart*/,
  7626. const TPoint & /*Point*/, TDragDetectStatus /*DragStatus*/)
  7627. {
  7628. // sometimes we do not get DDEnd so the list is not released
  7629. SAFE_DESTROY(FDDFileList);
  7630. FDDFileList = new TStringList();
  7631. FDragTempDir = WinConfiguration->TemporaryDir();
  7632. FDDTotalSize = 0;
  7633. FDragDropSshTerminate.reset(NULL);
  7634. }
  7635. //---------------------------------------------------------------------------
  7636. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDQueryContinueDrag(
  7637. TObject * /*Sender*/, BOOL /*FEscapePressed*/, int /*grfKeyState*/,
  7638. HRESULT & Result)
  7639. {
  7640. if (Result == DRAGDROP_S_DROP)
  7641. {
  7642. try
  7643. {
  7644. GlobalDragImageList->HideDragImage();
  7645. try
  7646. {
  7647. RemoteFileControlDDTargetDrop();
  7648. }
  7649. catch(ESshTerminate & E)
  7650. {
  7651. FDragDropSshTerminate.reset(E.Clone());
  7652. }
  7653. }
  7654. catch (Exception &E)
  7655. {
  7656. // If downloading fails we need to cancel drag&drop, otherwise
  7657. // Explorer shows error
  7658. // But by the way exception probably never reach this point as
  7659. // it's catched on way.
  7660. // Fatal exceptions get here (like when opening a secondary shell extension for file duplication fails).
  7661. Result = DRAGDROP_S_CANCEL;
  7662. ShowExtendedException(Terminal, &E);
  7663. }
  7664. }
  7665. }
  7666. //---------------------------------------------------------------------------
  7667. void __fastcall TCustomScpExplorerForm::DirViewMatchMask(
  7668. TObject * /*Sender*/, UnicodeString FileName, bool Directory, __int64 Size,
  7669. TDateTime Modification, UnicodeString Masks, bool & Matches, bool AllowImplicitMatches)
  7670. {
  7671. TFileMasks::TParams MaskParams;
  7672. MaskParams.Size = Size;
  7673. MaskParams.Modification = Modification;
  7674. // this does not re-parse the mask if it is the same as the last time
  7675. FDirViewMatchMask = Masks;
  7676. bool ImplicitMatch;
  7677. Matches =
  7678. // RecurseInclude parameter has no effect as the Path is empty
  7679. FDirViewMatchMask.Matches(FileName, Directory, UnicodeString(L""), &MaskParams, true, ImplicitMatch) &&
  7680. (AllowImplicitMatches || !ImplicitMatch);
  7681. }
  7682. //---------------------------------------------------------------------------
  7683. void __fastcall TCustomScpExplorerForm::DirViewGetOverlay(
  7684. TObject * Sender, TListItem * Item, WORD & Indexes)
  7685. {
  7686. TCustomDirView * DirView = reinterpret_cast<TCustomDirView *>(Sender);
  7687. UnicodeString Ext;
  7688. if (dynamic_cast<TUnixDirView *>(DirView) != NULL)
  7689. {
  7690. Ext = UnixExtractFileExt(DirView->ItemFileName(Item));
  7691. }
  7692. else
  7693. {
  7694. Ext = ExtractFileExt(DirView->ItemFileName(Item));
  7695. }
  7696. if (SameText(Ext, Configuration->PartialExt))
  7697. {
  7698. Indexes |= oiPartial;
  7699. }
  7700. }
  7701. //---------------------------------------------------------------------------
  7702. bool __fastcall TCustomScpExplorerForm::CanPasteToDirViewFromClipBoard()
  7703. {
  7704. return
  7705. DirViewEnabled(osCurrent) &&
  7706. DirView(osCurrent)->CanPasteFromClipBoard();
  7707. }
  7708. //---------------------------------------------------------------------------
  7709. bool __fastcall TCustomScpExplorerForm::CanPasteFromClipBoard()
  7710. {
  7711. bool Result = false;
  7712. if (CanPasteToDirViewFromClipBoard())
  7713. {
  7714. Result = true;
  7715. }
  7716. else
  7717. {
  7718. UnicodeString ClipboardText;
  7719. if (NonEmptyTextFromClipboard(ClipboardText))
  7720. {
  7721. if (StoredSessions->IsUrl(ClipboardText))
  7722. {
  7723. Result = true;
  7724. }
  7725. else
  7726. {
  7727. Result = DirViewEnabled(osCurrent);
  7728. }
  7729. }
  7730. }
  7731. return Result;
  7732. }
  7733. //---------------------------------------------------------------------------
  7734. void __fastcall TCustomScpExplorerForm::PasteFromClipBoard()
  7735. {
  7736. if (DoesClipboardContainOurFiles())
  7737. {
  7738. if (DebugAlwaysTrue(CanPasteToDirViewFromClipBoard()))
  7739. {
  7740. DebugAssert(!IsLocalBrowserMode());
  7741. TTerminalManager * Manager = TTerminalManager::Instance();
  7742. TTerminal * TergetTerminal = Manager->ActiveTerminal;
  7743. Manager->ActiveTerminal = FClipboardTerminal;
  7744. ExecuteFileOperation(foRemoteCopy, osRemote, FClipboardFileList.get(), false, TergetTerminal);
  7745. }
  7746. }
  7747. else if (CanPasteToDirViewFromClipBoard())
  7748. {
  7749. DirView(osCurrent)->PasteFromClipBoard();
  7750. }
  7751. else
  7752. {
  7753. UnicodeString ClipboardText;
  7754. if (NonEmptyTextFromClipboard(ClipboardText))
  7755. {
  7756. if (StoredSessions->IsUrl(ClipboardText))
  7757. {
  7758. NewSession(false, ClipboardText);
  7759. }
  7760. else
  7761. {
  7762. DirView(osCurrent)->Path = ClipboardText;
  7763. }
  7764. }
  7765. }
  7766. }
  7767. //---------------------------------------------------------------------------
  7768. void __fastcall TCustomScpExplorerForm::FileListFromClipboard()
  7769. {
  7770. // TBD
  7771. }
  7772. //---------------------------------------------------------------------------
  7773. void __fastcall TCustomScpExplorerForm::SelectAll(TOperationSide Side, TSelectMode Mode)
  7774. {
  7775. TCustomDirView * ADirView = DirView(Side);
  7776. ADirView->SelectAll(Mode);
  7777. ADirView->SetFocus();
  7778. }
  7779. //---------------------------------------------------------------------------
  7780. void __fastcall TCustomScpExplorerForm::SelectByMask(TOperationSide Side, bool Select)
  7781. {
  7782. TCustomDirView * ADirView = DirView(Side);
  7783. TFileFilter Filter;
  7784. DefaultFileFilter(Filter);
  7785. if (DoSelectMaskDialog(ADirView, Select, Filter))
  7786. {
  7787. Configuration->Usage->Inc(L"MaskSelections");
  7788. ADirView->SelectFiles(Filter, Select);
  7789. ADirView->SetFocus();
  7790. }
  7791. }
  7792. //---------------------------------------------------------------------------
  7793. void __fastcall TCustomScpExplorerForm::RestoreSelectedNames(TOperationSide Side)
  7794. {
  7795. TCustomDirView * ADirView = DirView(Side);
  7796. ADirView->RestoreSelectedNames();
  7797. ADirView->SetFocus();
  7798. }
  7799. //---------------------------------------------------------------------------
  7800. void __fastcall TCustomScpExplorerForm::SelectSameExt(bool Select)
  7801. {
  7802. TCustomDirView * CurrentDirView = DirView(osCurrent);
  7803. if (DebugAlwaysTrue(CurrentDirView->ItemFocused != NULL))
  7804. {
  7805. UnicodeString FileName = CurrentDirView->ItemFileName(CurrentDirView->ItemFocused);
  7806. UnicodeString Ext;
  7807. if (!IsSideLocalBrowser(osCurrent))
  7808. {
  7809. Ext = UnixExtractFileExt(FileName);
  7810. }
  7811. else
  7812. {
  7813. Ext = ExtractFileExt(FileName);
  7814. }
  7815. if (Ext.IsEmpty())
  7816. {
  7817. Ext = L".";
  7818. }
  7819. TFileFilter Filter;
  7820. Filter.Masks = FORMAT(L"*%s", (Ext));
  7821. Filter.Directories = false;
  7822. CurrentDirView->SelectFiles(Filter, Select);
  7823. }
  7824. }
  7825. //---------------------------------------------------------------------------
  7826. UnicodeString __fastcall TCustomScpExplorerForm::FileStatusBarText(
  7827. const TStatusFileInfo & FileInfo, TOperationSide Side)
  7828. {
  7829. UnicodeString Result;
  7830. if (!FIncrementalSearch.IsEmpty() && (Side == GetSide(osCurrent)))
  7831. {
  7832. Result = FormatIncrementalSearchStatus(FIncrementalSearch, FIncrementalSearchHaveNext);
  7833. }
  7834. else if (!IsSideLocalBrowser(Side) && ((Terminal == NULL) || Terminal->Disconnected))
  7835. {
  7836. // noop
  7837. }
  7838. else
  7839. {
  7840. Result =
  7841. FMTLOAD(FILE_INFO_FORMAT,
  7842. (FormatBytes(FileInfo.SelectedSize),
  7843. FormatBytes(FileInfo.FilesSize),
  7844. FormatNumber(FileInfo.SelectedCount),
  7845. FormatNumber(FileInfo.FilesCount)));
  7846. }
  7847. return Result;
  7848. }
  7849. //---------------------------------------------------------------------------
  7850. void __fastcall TCustomScpExplorerForm::FileStatusBarPanelClick(
  7851. TTBXStatusPanel * Panel, TOperationSide Side)
  7852. {
  7853. if (Panel->Index == 1)
  7854. {
  7855. ToggleShowHiddenFiles();
  7856. }
  7857. else if (Panel->Index == 2)
  7858. {
  7859. Filter(Side);
  7860. }
  7861. }
  7862. //---------------------------------------------------------------------------
  7863. void __fastcall TCustomScpExplorerForm::UpdateFileStatusBar(
  7864. TTBXStatusBar * StatusBar, const TStatusFileInfo & FileInfo, TOperationSide Side)
  7865. {
  7866. DebugAssert(!StatusBar->SimplePanel);
  7867. StatusBar->Panels->Items[0]->Caption = FileStatusBarText(FileInfo, Side);
  7868. UpdateFileStatusExtendedPanels(StatusBar, FileInfo);
  7869. }
  7870. //---------------------------------------------------------------------------
  7871. void __fastcall TCustomScpExplorerForm::UpdateFileStatusExtendedPanels(
  7872. TTBXStatusBar * StatusBar, const TStatusFileInfo & FileInfo)
  7873. {
  7874. DebugAssert(StatusBar->Panels->Count >= 3);
  7875. TTBXStatusPanel * HiddenFilesPanel = StatusBar->Panels->Items[1];
  7876. if (FileInfo.HiddenCount > 0)
  7877. {
  7878. HiddenFilesPanel->Caption = FMTLOAD(FILE_INFO_HIDDEN2, (FormatNumber(FileInfo.HiddenCount)));
  7879. HiddenFilesPanel->ViewPriority = 90; // <100 allows hiding panel when it does not fit
  7880. }
  7881. else
  7882. {
  7883. HiddenFilesPanel->ViewPriority = 0;
  7884. // not really necessary, just to cleanup no-longer-valid data
  7885. HiddenFilesPanel->Caption = L"";
  7886. }
  7887. TTBXStatusPanel * FilteredFilesPanel = StatusBar->Panels->Items[2];
  7888. if (FileInfo.FilteredCount > 0)
  7889. {
  7890. FilteredFilesPanel->Caption = FMTLOAD(FILE_INFO_FILTERED2, (FormatNumber(FileInfo.FilteredCount)));
  7891. FilteredFilesPanel->ViewPriority = 90; // <100 allows hiding panel when it does not fit
  7892. }
  7893. else
  7894. {
  7895. FilteredFilesPanel->ViewPriority = 0;
  7896. // not really necessary, just to cleanup no-longer-valid data
  7897. FilteredFilesPanel->Caption = L"";
  7898. }
  7899. }
  7900. //---------------------------------------------------------------------------
  7901. void __fastcall TCustomScpExplorerForm::RemoteStatusBarClick(
  7902. TObject * /*Sender*/)
  7903. {
  7904. if (DirView(osRemote)->Enabled)
  7905. {
  7906. DirView(osRemote)->SetFocus();
  7907. }
  7908. }
  7909. //---------------------------------------------------------------------------
  7910. void __fastcall TCustomScpExplorerForm::ToggleQueueVisibility()
  7911. {
  7912. TQueueViewConfiguration Config = WinConfiguration->QueueView;
  7913. switch (Config.Show)
  7914. {
  7915. case qvShow:
  7916. if ((FQueueStatus != NULL) && (FQueueStatus->Count > 0))
  7917. {
  7918. Config.Show = qvHide;
  7919. }
  7920. else
  7921. {
  7922. Config.Show = Config.LastHideShow;
  7923. }
  7924. break;
  7925. case qvHideWhenEmpty:
  7926. if (ComponentVisible[fcQueueView])
  7927. {
  7928. Config.Show = qvHide;
  7929. }
  7930. else
  7931. {
  7932. Config.LastHideShow = Config.Show;
  7933. Config.Show = qvShow;
  7934. }
  7935. break;
  7936. case qvHide:
  7937. Config.LastHideShow = Config.Show;
  7938. Config.Show = qvShow;
  7939. break;
  7940. }
  7941. WinConfiguration->QueueView = Config;
  7942. }
  7943. //---------------------------------------------------------------------------
  7944. UnicodeString __fastcall TCustomScpExplorerForm::PathForCaption()
  7945. {
  7946. UnicodeString Result;
  7947. if (FTerminal != NULL)
  7948. {
  7949. switch (WinConfiguration->PathInCaption)
  7950. {
  7951. case picShort:
  7952. Result = TTerminalManager::Instance()->GetTerminalShortPath(FTerminal);
  7953. break;
  7954. case picFull:
  7955. Result = FTerminal->CurrentDirectory;
  7956. break;
  7957. }
  7958. }
  7959. return Result;
  7960. }
  7961. //---------------------------------------------------------------------------
  7962. TColor __fastcall TCustomScpExplorerForm::PanelColor()
  7963. {
  7964. TColor Result = GetWindowColor(FSessionColor);
  7965. return Result;
  7966. }
  7967. //---------------------------------------------------------------------------
  7968. TColor __fastcall TCustomScpExplorerForm::DisabledPanelColor()
  7969. {
  7970. TColor Result = (WinConfiguration->UseDarkTheme() ? static_cast<TColor>(RGB(0x40, 0x40, 0x40)) : clBtnFace);
  7971. return Result;
  7972. }
  7973. //---------------------------------------------------------------------------
  7974. void __fastcall TCustomScpExplorerForm::UpdateControls()
  7975. {
  7976. TTerminalManager::Instance()->UpdateAppTitle();
  7977. // WORAKRDOUND: Disabling list view when it is not showing yet does not set its
  7978. // background to gray on Windows 7 (works on Windows 10).
  7979. // See also EnableControl
  7980. if (Showing)
  7981. {
  7982. if (Terminal != NULL)
  7983. {
  7984. // Update path when it changes
  7985. if ((SessionsPageControl->ActivePage != NULL) && (GetSessionTabTerminal(SessionsPageControl->ActivePage) == Terminal))
  7986. {
  7987. UpdateSessionTab(SessionsPageControl->ActivePage);
  7988. }
  7989. }
  7990. NonVisualDataModule->ReconnectSessionAction->Update();
  7991. ReconnectToolbar->Visible = NonVisualDataModule->ReconnectSessionAction->Visible;
  7992. // ReconnectSessionAction is hidden when disabled, so enabling it actualy resizes the toolbar
  7993. CenterReconnectToolbar();
  7994. bool HasTerminal = (Terminal != NULL) && Terminal->Active;
  7995. if (HasTerminal)
  7996. {
  7997. // TODO needed yet with second local browser?
  7998. if (!RemoteDirView->Enabled)
  7999. {
  8000. RemoteDirView->Enabled = true;
  8001. if (FRemoteDirViewWasFocused)
  8002. {
  8003. ActiveControl = RemoteDirView;
  8004. }
  8005. }
  8006. RemoteDirView->Color = PanelColor();
  8007. }
  8008. else
  8009. {
  8010. if (RemoteDirView->Enabled)
  8011. {
  8012. // This is first called when the form is being constructed
  8013. // (not anymore due to Showing test above)
  8014. // but the false is overriden in the constructor later.
  8015. // An even later in TScpCommanderForm::DoShow()
  8016. FRemoteDirViewWasFocused = (ActiveControl == RemoteDirView);
  8017. RemoteDirView->Enabled = false;
  8018. RemoteDirView->Color = DisabledPanelColor();
  8019. }
  8020. }
  8021. RemoteDirView->Font->Color = GetWindowTextColor(RemoteDirView->Color);
  8022. RemoteDriveView->Enabled = RemoteDirView->Enabled;
  8023. RemoteDriveView->Color = RemoteDirView->Color;
  8024. RemoteDriveView->Font->Color = RemoteDirView->Font->Color;
  8025. QueueView3->Enabled = HasTerminal && Terminal->IsCapable[fsBackgroundTransfers];
  8026. QueueView3->Color = QueueView3->Enabled ? GetWindowColor() : DisabledPanelColor();
  8027. QueueView3->Font->Color = GetWindowTextColor(QueueView3->Color);
  8028. QueueLabelUpdateStatus();
  8029. RemoteDirView->DarkMode = WinConfiguration->UseDarkTheme();
  8030. RemoteDriveView->DarkMode = RemoteDirView->DarkMode;
  8031. reinterpret_cast<TTBCustomItem *>(GetComponent(fcRemotePathComboBox))->Enabled = HasTerminal;
  8032. }
  8033. }
  8034. //---------------------------------------------------------------------------
  8035. void __fastcall TCustomScpExplorerForm::DoDirViewLoaded(TCustomDirView * /*Sender*/)
  8036. {
  8037. UpdateControls();
  8038. }
  8039. //---------------------------------------------------------------------------
  8040. void __fastcall TCustomScpExplorerForm::DirViewLoaded(
  8041. TObject * Sender)
  8042. {
  8043. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  8044. DebugAssert(DirView != NULL);
  8045. DoDirViewLoaded(DirView);
  8046. TransferPresetAutoSelect();
  8047. }
  8048. //---------------------------------------------------------------------------
  8049. void __fastcall TCustomScpExplorerForm::StartUpdates()
  8050. {
  8051. TUpdatesConfiguration Updates = WinConfiguration->Updates;
  8052. // first run after installation
  8053. if (double(Updates.LastCheck) == 0)
  8054. {
  8055. // make sure next time there will be an update (if enabled)
  8056. Updates.LastCheck = TDateTime(1);
  8057. WinConfiguration->Updates = Updates;
  8058. }
  8059. else if ((double(Updates.Period) > 0) &&
  8060. (Now() - Updates.LastCheck >= Updates.Period))
  8061. {
  8062. TThreadMethod OnUpdatesChecked = NULL;
  8063. if (!IsUWP())
  8064. {
  8065. OnUpdatesChecked = UpdatesChecked;
  8066. }
  8067. StartUpdateThread(OnUpdatesChecked);
  8068. }
  8069. }
  8070. //---------------------------------------------------------------------------
  8071. void __fastcall TCustomScpExplorerForm::UpdatesChecked()
  8072. {
  8073. UnicodeString Message;
  8074. bool New;
  8075. TQueryType Type;
  8076. GetUpdatesMessage(Message, New, Type, false);
  8077. if (!Message.IsEmpty())
  8078. {
  8079. if (New)
  8080. {
  8081. Message = FMTLOAD(NEW_VERSION_CLICK, (Message));
  8082. }
  8083. if (!New && (Type != qtWarning))
  8084. {
  8085. PostNote(UnformatMessage(Message), 0, UpdatesNoteClicked, NULL);
  8086. }
  8087. else
  8088. {
  8089. Notify(NULL, Message, Type, true, UpdatesNoteClicked);
  8090. }
  8091. if (New)
  8092. {
  8093. Configuration->Usage->Inc(L"UpdateNotifications");
  8094. }
  8095. }
  8096. }
  8097. //---------------------------------------------------------------------------
  8098. void __fastcall TCustomScpExplorerForm::UpdatesNoteClicked(TObject * /*Sender*/)
  8099. {
  8100. RestoreApp();
  8101. bool CanDisplay = !NonVisualDataModule->Busy;
  8102. if (!CanDisplay && (Screen->ActiveForm != NULL))
  8103. {
  8104. CanDisplay = (Screen->ActiveForm->Perform(WM_CAN_DISPLAY_UPDATES, 0, 0) != 0);
  8105. }
  8106. if (CanDisplay)
  8107. {
  8108. Configuration->Usage->Inc(L"UpdateNotificationsClicked");
  8109. CheckForUpdates(true);
  8110. }
  8111. else
  8112. {
  8113. Configuration->Usage->Inc(L"UpdateNotificationsBusy");
  8114. }
  8115. }
  8116. //---------------------------------------------------------------------------
  8117. void __fastcall TCustomScpExplorerForm::GetTransferPresetAutoSelectData(
  8118. TCopyParamRuleData & Data)
  8119. {
  8120. DebugAssert(Terminal != NULL);
  8121. Data.HostName = Terminal->SessionData->HostNameExpanded;
  8122. Data.UserName = Terminal->SessionData->UserNameExpanded;
  8123. Data.RemoteDirectory = RemoteDirView->PathName;
  8124. }
  8125. //---------------------------------------------------------------------------
  8126. void __fastcall TCustomScpExplorerForm::TransferPresetAutoSelect()
  8127. {
  8128. // Terminal can be null when we are changing local directory implicitly,
  8129. // because it has been deleted, while no session is connected
  8130. // (Login dialog is open)
  8131. if (FAllowTransferPresetAutoSelect && (Terminal != NULL))
  8132. {
  8133. DebugAssert(!IsLocalBrowserMode());
  8134. TCopyParamRuleData Data;
  8135. GetTransferPresetAutoSelectData(Data);
  8136. int CopyParamIndex = GUIConfiguration->CopyParamList->Find(Data);
  8137. UnicodeString CopyParamCurrent = GUIConfiguration->CopyParamCurrent;
  8138. if (CopyParamIndex < 0)
  8139. {
  8140. // there is no preset that matches autoselection
  8141. // set preset that we consider "default"
  8142. FCopyParamAutoSelected = L""; // forget last autoselected preset
  8143. GUIConfiguration->CopyParamCurrent = FCopyParamDefault;
  8144. }
  8145. else
  8146. {
  8147. // there is preset matching autoselection
  8148. UnicodeString CopyParamName = GUIConfiguration->CopyParamList->Names[CopyParamIndex];
  8149. if (CopyParamName == FCopyParamAutoSelected)
  8150. {
  8151. // autoselected the same preset as the last time
  8152. // make no change (i.e. preserve custom user preset, if any)
  8153. }
  8154. else
  8155. {
  8156. // autoselected the different preset then the last time (or there
  8157. // was default preset set)
  8158. FCopyParamAutoSelected = CopyParamName; // remember autoselection
  8159. GUIConfiguration->CopyParamCurrent = CopyParamName;
  8160. }
  8161. }
  8162. if (GUIConfiguration->CopyParamCurrent != CopyParamCurrent)
  8163. {
  8164. if (CopyParamIndex >= 0)
  8165. {
  8166. Configuration->Usage->Inc(L"CopyParamAutoSelects");
  8167. }
  8168. TTransferPresetNoteData * Data = new TTransferPresetNoteData;
  8169. try
  8170. {
  8171. int Fmt =
  8172. (CopyParamIndex < 0) ?
  8173. (GUIConfiguration->CopyParamIndex < 0 ? COPY_PARAM_DEFAULT_NORM : COPY_PARAM_DEFAULT_CUSTOM) :
  8174. COPY_PARAM_AUTOSELECTED;
  8175. UnicodeString Message = FMTLOAD(Fmt, (StripHotkey(GUIConfiguration->CopyParamCurrent)));
  8176. Data->Message = MainInstructions(Message);
  8177. int CopyParamAttrs = Terminal->UsableCopyParamAttrs(0).General;
  8178. UnicodeString Info = GUIConfiguration->CurrentCopyParam.GetInfoStr(L"\n",
  8179. CopyParamAttrs);
  8180. if (CopyParamIndex >= 0)
  8181. {
  8182. DebugAssert(GUIConfiguration->CopyParamList->Rules[CopyParamIndex] != NULL);
  8183. Info = FORMAT(L"%s\n \n%s", (Info,
  8184. FMTLOAD(COPY_PARAM_RULE,
  8185. (GUIConfiguration->CopyParamList->Rules[CopyParamIndex]->GetInfoStr(L"\n")))));
  8186. }
  8187. Data->Message += L"\n\n" + Info;
  8188. if (WinConfiguration->CopyParamAutoSelectNotice)
  8189. {
  8190. TransferPresetNoteMessage(Data, true);
  8191. }
  8192. else
  8193. {
  8194. PostNote(Message, 0, TransferPresetNoteClicked, Data);
  8195. Data = NULL; // ownership passed
  8196. }
  8197. }
  8198. __finally
  8199. {
  8200. delete Data;
  8201. }
  8202. }
  8203. }
  8204. }
  8205. //---------------------------------------------------------------------------
  8206. void __fastcall TCustomScpExplorerForm::TransferPresetNoteMessage(
  8207. TTransferPresetNoteData * NoteData, bool AllowNeverAskAgain)
  8208. {
  8209. DebugAssert(NoteData != NULL);
  8210. DebugAssert(!IsLocalBrowserMode());
  8211. TMessageParams Params(AllowNeverAskAgain ? mpNeverAskAgainCheck : 0);
  8212. TQueryButtonAlias Aliases[1];
  8213. Aliases[0].Button = qaIgnore; // "ignore" is after "ok"
  8214. Aliases[0].Alias = LoadStr(CONFIGURE_BUTTON);
  8215. Params.Aliases = Aliases;
  8216. Params.AliasesCount = LENOF(Aliases);
  8217. unsigned int Result =
  8218. MoreMessageDialog(NoteData->Message, NULL, qtInformation,
  8219. qaOK | qaIgnore, HELP_COPY_PARAM_AUTOSELECTED, &Params);
  8220. switch (Result)
  8221. {
  8222. case qaNeverAskAgain:
  8223. DebugAssert(AllowNeverAskAgain);
  8224. WinConfiguration->CopyParamAutoSelectNotice = false;
  8225. break;
  8226. case qaIgnore:
  8227. PreferencesDialog(pmPresets);
  8228. break;
  8229. }
  8230. }
  8231. //---------------------------------------------------------------------------
  8232. void __fastcall TCustomScpExplorerForm::TransferPresetNoteClicked(TObject * Sender)
  8233. {
  8234. // as of now useless, as this is used for notes only, never for balloons, ...
  8235. RestoreApp();
  8236. // .. and we should never be busy here
  8237. if (DebugAlwaysTrue(!NonVisualDataModule->Busy))
  8238. {
  8239. TransferPresetNoteMessage(DebugNotNull(dynamic_cast<TTransferPresetNoteData *>(Sender)), false);
  8240. }
  8241. }
  8242. //---------------------------------------------------------------------------
  8243. void __fastcall TCustomScpExplorerForm::PreferencesDialog(
  8244. TPreferencesMode APreferencesMode)
  8245. {
  8246. std::unique_ptr<TPreferencesDialogData> PreferencesData;
  8247. TCopyParamRuleData Data;
  8248. if (Terminal != NULL)
  8249. {
  8250. PreferencesData.reset(new TPreferencesDialogData());
  8251. GetTransferPresetAutoSelectData(Data);
  8252. PreferencesData->CopyParamRuleData = &Data;
  8253. }
  8254. DoPreferencesDialog(APreferencesMode, PreferencesData.get());
  8255. }
  8256. //---------------------------------------------------------------------------
  8257. void __fastcall TCustomScpExplorerForm::AdHocCustomCommandValidate(
  8258. const TCustomCommandType & Command)
  8259. {
  8260. if (CustomCommandState(Command, FEditingFocusedAdHocCommand, ccltAll) <= 0)
  8261. {
  8262. throw Exception(FMTLOAD(CUSTOM_COMMAND_IMPOSSIBLE, (Command.Command)));
  8263. }
  8264. }
  8265. //---------------------------------------------------------------------------
  8266. void __fastcall TCustomScpExplorerForm::AdHocCustomCommand(bool OnFocused)
  8267. {
  8268. bool RemoteAllowed = CustomCommandRemoteAllowed();
  8269. TCustomCommandType Command;
  8270. // make sure we use local custom command when remote are not supported
  8271. if (RemoteAllowed || FLAGSET(FLastCustomCommand.Params, ccLocal))
  8272. {
  8273. Command = FLastCustomCommand;
  8274. }
  8275. else
  8276. {
  8277. Command.Params = Command.Params | ccLocal;
  8278. }
  8279. Command.Name = LoadStr(CUSTOM_COMMAND_AD_HOC_NAME);
  8280. FEditingFocusedAdHocCommand = OnFocused;
  8281. bool LocalSide = IsSideLocalBrowser(FCurrentSide);
  8282. int Options =
  8283. FLAGMASK((!RemoteAllowed || LocalSide), ccoDisableRemote) |
  8284. FLAGMASK(LocalSide, ccoDisableRemoteFiles);
  8285. if (DoCustomCommandDialog(Command, WinConfiguration->CustomCommandList,
  8286. ccmAdHoc, Options, AdHocCustomCommandValidate, NULL))
  8287. {
  8288. FLastCustomCommand = Command;
  8289. UpdateCustomCommandsToolbar();
  8290. ExecuteFileOperation(foCustomCommand, osRemote, OnFocused, false, &Command);
  8291. }
  8292. }
  8293. //---------------------------------------------------------------------------
  8294. void __fastcall TCustomScpExplorerForm::LastCustomCommand(bool OnFocused)
  8295. {
  8296. DebugAssert(!FLastCustomCommand.Command.IsEmpty());
  8297. int State = CustomCommandState(FLastCustomCommand, OnFocused, ccltAll);
  8298. DebugAssert(State > 0);
  8299. if (State <= 0)
  8300. {
  8301. throw Exception(FMTLOAD(CUSTOM_COMMAND_IMPOSSIBLE, (FLastCustomCommand.Command)));
  8302. }
  8303. ExecuteFileOperation(foCustomCommand, osRemote, OnFocused, false, &FLastCustomCommand);
  8304. }
  8305. //---------------------------------------------------------------------------
  8306. bool __fastcall TCustomScpExplorerForm::GetLastCustomCommand(bool OnFocused,
  8307. TCustomCommandType & Command, int & State)
  8308. {
  8309. bool Result = !FLastCustomCommand.Command.IsEmpty();
  8310. if (Result)
  8311. {
  8312. Command = FLastCustomCommand;
  8313. State = CustomCommandState(FLastCustomCommand, OnFocused, ccltAll);
  8314. }
  8315. return Result;
  8316. }
  8317. //---------------------------------------------------------------------------
  8318. void __fastcall TCustomScpExplorerForm::BeforeAction()
  8319. {
  8320. ResetIncrementalSearch();
  8321. }
  8322. //---------------------------------------------------------------------------
  8323. void __fastcall TCustomScpExplorerForm::PostComponentHide(Byte Component)
  8324. {
  8325. DebugAssert(ComponentVisible[Component]);
  8326. PostMessage(Handle, WM_COMPONENT_HIDE, Component, 0);
  8327. }
  8328. //---------------------------------------------------------------------------
  8329. void __fastcall TCustomScpExplorerForm::QueueSplitterDblClick(TObject * /*Sender*/)
  8330. {
  8331. // when queue panel is resized here directly, the status bar is stretched
  8332. // over whole space the panel occupied
  8333. PostComponentHide(fcQueueView);
  8334. }
  8335. //---------------------------------------------------------------------------
  8336. void __fastcall TCustomScpExplorerForm::WMWinIniChange(TMessage & Message)
  8337. {
  8338. WinConfiguration->ResetSysDarkTheme();
  8339. ConfigurationChanged();
  8340. ConfigureInterface();
  8341. TForm::Dispatch(&Message);
  8342. }
  8343. //---------------------------------------------------------------------------
  8344. void __fastcall TCustomScpExplorerForm::Dispatch(void * Message)
  8345. {
  8346. TMessage * M = static_cast<TMessage*>(Message);
  8347. switch (M->Msg)
  8348. {
  8349. case CM_DIALOGCHAR:
  8350. CMDialogChar(*M);
  8351. break;
  8352. case WM_APPCOMMAND:
  8353. WMAppCommand(*M);
  8354. break;
  8355. case WM_SYSCOMMAND:
  8356. WMSysCommand(*M);
  8357. break;
  8358. case WM_QUERYENDSESSION:
  8359. WMQueryEndSession(*M);
  8360. break;
  8361. case WM_ENDSESSION:
  8362. WMEndSession(*reinterpret_cast<TWMEndSession *>(M));
  8363. break;
  8364. #ifdef _DEBUG
  8365. case WM_WINDOWPOSCHANGED:
  8366. WMWindowPosChanged(*reinterpret_cast<TWMWindowPosMsg *>(M));
  8367. break;
  8368. case CM_CUSTOMSTYLECHANGED:
  8369. TForm::Dispatch(Message);
  8370. break;
  8371. #endif
  8372. case WM_COMPONENT_HIDE:
  8373. {
  8374. Byte Component = static_cast<Byte>(M->WParam);
  8375. // sanity check
  8376. if (ComponentVisible[Component])
  8377. {
  8378. // special treatment
  8379. if (Component == fcQueueView)
  8380. {
  8381. ToggleQueueVisibility();
  8382. DebugAssert(!ComponentVisible[fcQueueView]);
  8383. }
  8384. else
  8385. {
  8386. ComponentVisible[Component] = false;
  8387. }
  8388. }
  8389. }
  8390. break;
  8391. case WM_COPYDATA:
  8392. WMCopyData(*M);
  8393. break;
  8394. case WM_MANAGES_CAPTION:
  8395. // caption managed in TTerminalManager::UpdateAppTitle()
  8396. M->Result = 1;
  8397. break;
  8398. case WM_WANTS_MOUSEWHEEL:
  8399. M->Result = 1;
  8400. break;
  8401. case CM_SHOWINGCHANGED:
  8402. CMShowingChanged(*M);
  8403. break;
  8404. case WM_CLOSE:
  8405. WMClose(*M);
  8406. break;
  8407. case WM_DPICHANGED:
  8408. WMDpiChanged(*M);
  8409. break;
  8410. case CM_DPICHANGED:
  8411. CMDpiChanged(*M);
  8412. break;
  8413. case WM_WININICHANGE:
  8414. WMWinIniChange(*M);
  8415. break;
  8416. case CM_DIALOGKEY:
  8417. CMDialogKey(*reinterpret_cast<TWMKeyDown *>(M));
  8418. break;
  8419. default:
  8420. TForm::Dispatch(Message);
  8421. break;
  8422. }
  8423. }
  8424. //---------------------------------------------------------------------------
  8425. void __fastcall TCustomScpExplorerForm::UpdateImages()
  8426. {
  8427. // noop
  8428. }
  8429. //---------------------------------------------------------------------------
  8430. void __fastcall TCustomScpExplorerForm::CMDpiChanged(TMessage & Message)
  8431. {
  8432. TForm::Dispatch(&Message);
  8433. FSessionColors->Clear();
  8434. AddFixedSessionImages();
  8435. RegenerateSessionColorsImageList(FSessionColors, FSessionColorMaskImageIndex);
  8436. UpdateImages();
  8437. }
  8438. //---------------------------------------------------------------------------
  8439. void __fastcall TCustomScpExplorerForm::WMDpiChanged(TMessage & Message)
  8440. {
  8441. TForm::Dispatch(&Message);
  8442. Configuration->Usage->Inc(L"PixelsPerInchChanged");
  8443. UpdatePixelsPerInchMainWindowCounter();
  8444. }
  8445. //---------------------------------------------------------------------------
  8446. void __fastcall TCustomScpExplorerForm::WMClose(TMessage & Message)
  8447. {
  8448. // Cannot close window while we are busy.
  8449. // We cannot test this in FormCloseQuery as that is called also from
  8450. // Close(), which is called by CloseApplicationAction. So we can be busy
  8451. // there already even, when it is legitimate to close the application.
  8452. // Possibly a better place to handle this would be WMSysCommand.
  8453. if (NonVisualDataModule->Busy)
  8454. {
  8455. Message.Result = 1;
  8456. }
  8457. else
  8458. {
  8459. TForm::Dispatch(&Message);
  8460. }
  8461. }
  8462. //---------------------------------------------------------------------------
  8463. void __fastcall TCustomScpExplorerForm::CMShowingChanged(TMessage & Message)
  8464. {
  8465. TForm::Dispatch(&Message);
  8466. if (Showing && (Terminal == NULL))
  8467. {
  8468. // When we are starting minimized (i.e. from an installer),
  8469. // postpone showing Login dialog until we get restored.
  8470. // Otherwise the Login dialog (and Authentication window) show restored
  8471. // over invisible (minimized) main window.
  8472. if (WindowState == wsMinimized)
  8473. {
  8474. FNeedSession = true;
  8475. }
  8476. else
  8477. {
  8478. // This happens before application ever goes idle, so the toolbars would
  8479. // stay enabled (initial state) until the Login dialog is dismissed.
  8480. UpdateActions();
  8481. NonVisualDataModule->StartBusy();
  8482. try
  8483. {
  8484. // Need to process WM_ACTIVATEAPP before showing the Login dialog,
  8485. // otherwise the dialog does not receive focus.
  8486. // With Commander interface the ProcessMessages is called already
  8487. // by TDriveView, but with Explorer interface, we need to call it explicily
  8488. Application->ProcessMessages();
  8489. // do not reload sessions, they have been loaded just now (optimization)
  8490. NeedSession(true);
  8491. }
  8492. __finally
  8493. {
  8494. NonVisualDataModule->EndBusy();
  8495. }
  8496. }
  8497. }
  8498. }
  8499. //---------------------------------------------------------------------------
  8500. void __fastcall TCustomScpExplorerForm::CenterReconnectToolbar()
  8501. {
  8502. ReconnectToolbar->Left = (ReconnectToolbar->Parent->ClientWidth - ReconnectToolbar->Width) / 2;
  8503. ReconnectToolbar->Top = (ReconnectToolbar->Parent->ClientHeight - ReconnectToolbar->Height) / 2;
  8504. }
  8505. //---------------------------------------------------------------------------
  8506. void __fastcall TCustomScpExplorerForm::FormConstrainedResize(
  8507. TObject * /*Sender*/, int & MinWidth, int & MinHeight, int & MaxWidth,
  8508. int & MaxHeight)
  8509. {
  8510. // workaround for bug in TWinControl.CalcConstraints
  8511. // Check for empty rect (restore from iconic state) is done there only after
  8512. // call to AdjustClientRect, which enlarges the rect (for forms).
  8513. TRect R = GetClientRect();
  8514. // when restoring from iconic state, place no restrictions
  8515. if (IsRectEmpty(R))
  8516. {
  8517. MinWidth = 0;
  8518. MinHeight = 0;
  8519. MaxWidth = 32000;
  8520. MaxHeight = 32000;
  8521. }
  8522. }
  8523. //---------------------------------------------------------------------------
  8524. void __fastcall TCustomScpExplorerForm::GetSpaceAvailable(const UnicodeString Path,
  8525. TSpaceAvailable & ASpaceAvailable, bool & Close)
  8526. {
  8527. // terminal can be already closed (e.g. dropped connection)
  8528. if ((Terminal != NULL) && Terminal->IsCapable[fcCheckingSpaceAvailable])
  8529. {
  8530. Configuration->Usage->Inc(L"SpaceAvailableChecks");
  8531. try
  8532. {
  8533. Terminal->SpaceAvailable(Path, ASpaceAvailable);
  8534. }
  8535. catch(...)
  8536. {
  8537. if (!Terminal->Active)
  8538. {
  8539. Close = true;
  8540. }
  8541. throw;
  8542. }
  8543. }
  8544. }
  8545. //---------------------------------------------------------------------------
  8546. void __fastcall TCustomScpExplorerForm::FileSystemInfo()
  8547. {
  8548. const TSessionInfo & SessionInfo = Terminal->GetSessionInfo();
  8549. const TFileSystemInfo & FileSystemInfo = Terminal->GetFileSystemInfo(true);
  8550. TGetSpaceAvailable OnGetSpaceAvailable = NULL;
  8551. if (Terminal->IsCapable[fcCheckingSpaceAvailable])
  8552. {
  8553. OnGetSpaceAvailable = GetSpaceAvailable;
  8554. }
  8555. DoFileSystemInfoDialog(SessionInfo, FileSystemInfo, Terminal->CurrentDirectory,
  8556. OnGetSpaceAvailable);
  8557. }
  8558. //---------------------------------------------------------------------------
  8559. TSessionData * __fastcall TCustomScpExplorerForm::SessionDataForCode()
  8560. {
  8561. std::unique_ptr<TSessionData> Data(CloneCurrentSessionData());
  8562. Terminal->FillSessionDataForCode(Data.get());
  8563. return Data.release();
  8564. }
  8565. //---------------------------------------------------------------------------
  8566. void __fastcall TCustomScpExplorerForm::GenerateUrl(TStrings * Paths)
  8567. {
  8568. std::unique_ptr<TSessionData> Data(SessionDataForCode());
  8569. DoGenerateUrlDialog(Data.get(), Paths);
  8570. }
  8571. //---------------------------------------------------------------------------
  8572. void __fastcall TCustomScpExplorerForm::SessionGenerateUrl()
  8573. {
  8574. GenerateUrl(NULL);
  8575. }
  8576. //---------------------------------------------------------------------------
  8577. void __fastcall TCustomScpExplorerForm::FileGenerateUrl()
  8578. {
  8579. std::unique_ptr<TStrings> Paths(new TStringList());
  8580. DirView(osCurrent)->CreateFileList(false, true, Paths.get());
  8581. GenerateUrl(Paths.get());
  8582. }
  8583. //---------------------------------------------------------------------------
  8584. void __fastcall TCustomScpExplorerForm::UpdateSessionColor(TColor value)
  8585. {
  8586. FSessionColor = value;
  8587. TColor C = (value != 0 ? value : Vcl::Graphics::clNone);
  8588. TTBXColorItem * ColorItem = dynamic_cast<TTBXColorItem *>(
  8589. static_cast<TObject *>(GetComponent(fcColorMenu)));
  8590. DebugAssert(ColorItem != NULL);
  8591. ColorItem->Color = C;
  8592. NonVisualDataModule->ColorMenuItem->Color = C;
  8593. UpdateControls();
  8594. }
  8595. //---------------------------------------------------------------------------
  8596. void __fastcall TCustomScpExplorerForm::SetSessionColor(TColor value)
  8597. {
  8598. if (value != FSessionColor)
  8599. {
  8600. UpdateSessionColor(value);
  8601. // Is null when called from LastTerminalClosed
  8602. if (Terminal != NULL)
  8603. {
  8604. SessionsPageControl->ActivePage->ImageIndex = AddSessionColor(value);
  8605. }
  8606. }
  8607. }
  8608. //---------------------------------------------------------------------------
  8609. bool __fastcall TCustomScpExplorerForm::CancelNote(bool Force)
  8610. {
  8611. bool Result = FNoteTimer->Enabled;
  8612. if (Result)
  8613. {
  8614. // cannot cancel note too early
  8615. bool NotEarly =
  8616. (Now() - FNoteShown >
  8617. EncodeTimeVerbose(0, 0, (unsigned short)(WinConfiguration->NotificationsStickTime), 0));
  8618. if (Force || NotEarly)
  8619. {
  8620. FNoteTimer->Enabled = false;
  8621. FNote = L"";
  8622. SAFE_DESTROY(FNoteData);
  8623. FOnNoteClick = NULL;
  8624. FNoteHints = FNotes->Text;
  8625. FNoteHints.Delete(FNoteHints.Length() - 1, 2);
  8626. UpdateStatusBar();
  8627. }
  8628. }
  8629. return Result;
  8630. }
  8631. //---------------------------------------------------------------------------
  8632. void __fastcall TCustomScpExplorerForm::NoteTimer(TObject * /*Sender*/)
  8633. {
  8634. DebugAssert(FNoteTimer->Enabled);
  8635. CancelNote(true);
  8636. }
  8637. //---------------------------------------------------------------------------
  8638. void __fastcall TCustomScpExplorerForm::AddNote(UnicodeString Note, bool UpdateNow)
  8639. {
  8640. int P = Note.Pos(L"\n");
  8641. if (P > 0)
  8642. {
  8643. Note.SetLength(P - 1);
  8644. }
  8645. FNotes->Add(FORMAT(L"[%s] %s",
  8646. (FormatDateTime(Configuration->TimeFormat, Now()), Note)));
  8647. while (FNotes->Count > 10)
  8648. {
  8649. FNotes->Delete(0);
  8650. }
  8651. if (UpdateNow)
  8652. {
  8653. FNoteHints = FNotes->Text;
  8654. FNoteHints.Delete(FNoteHints.Length() - 1, 2);
  8655. UpdateStatusBar();
  8656. }
  8657. }
  8658. //---------------------------------------------------------------------------
  8659. void __fastcall TCustomScpExplorerForm::PostNote(UnicodeString Note,
  8660. unsigned int Seconds, TNotifyEvent OnNoteClick, TObject * NoteData)
  8661. {
  8662. int P = Note.Pos(L"\n");
  8663. if (P > 0)
  8664. {
  8665. Note.SetLength(P - 1);
  8666. }
  8667. FNoteHints = FNotes->Text;
  8668. FNoteHints.Delete(FNoteHints.Length() - 1, 2);
  8669. FNote = Note;
  8670. SAFE_DESTROY(FNoteData);
  8671. FNoteData = NoteData;
  8672. FOnNoteClick = OnNoteClick;
  8673. AddNote(Note, false);
  8674. UpdateStatusBar();
  8675. FNoteShown = Now();
  8676. FNoteTimer->Enabled = false;
  8677. if (Seconds == 0)
  8678. {
  8679. Seconds = WinConfiguration->NotificationsTimeout;
  8680. }
  8681. FNoteTimer->Interval = Seconds * MSecsPerSec;
  8682. FNoteTimer->Enabled = true;
  8683. }
  8684. //---------------------------------------------------------------------------
  8685. void __fastcall TCustomScpExplorerForm::ReadDirectoryCancelled()
  8686. {
  8687. PostNote(LoadStr(DIRECTORY_READING_CANCELLED), 0, NULL, NULL);
  8688. }
  8689. //---------------------------------------------------------------------------
  8690. void __fastcall TCustomScpExplorerForm::SynchronizeBrowsingChanged()
  8691. {
  8692. if (NonVisualDataModule->SynchronizeBrowsingAction->Checked)
  8693. {
  8694. Configuration->Usage->Inc(L"SynchronizeBrowsingEnabled");
  8695. }
  8696. PostNote(FORMAT(LoadStrPart(SYNC_DIR_BROWSE_TOGGLE, 1),
  8697. (LoadStrPart(SYNC_DIR_BROWSE_TOGGLE,
  8698. (NonVisualDataModule->SynchronizeBrowsingAction->Checked ? 2 : 3)))),
  8699. 0, NULL, NULL);
  8700. }
  8701. //---------------------------------------------------------------------------
  8702. void __fastcall TCustomScpExplorerForm::ToggleShowHiddenFiles()
  8703. {
  8704. WinConfiguration->ShowHiddenFiles = !WinConfiguration->ShowHiddenFiles;
  8705. PostNote(FORMAT(LoadStrPart(SHOW_HIDDEN_FILES_TOGGLE, 1),
  8706. (LoadStrPart(SHOW_HIDDEN_FILES_TOGGLE,
  8707. (WinConfiguration->ShowHiddenFiles ? 2 : 3)))), 0, NULL, NULL);
  8708. }
  8709. //---------------------------------------------------------------------------
  8710. void __fastcall TCustomScpExplorerForm::SetFormatSizeBytes(TFormatBytesStyle Style)
  8711. {
  8712. WinConfiguration->FormatSizeBytes = Style;
  8713. }
  8714. //---------------------------------------------------------------------------
  8715. void __fastcall TCustomScpExplorerForm::ToggleAutoReadDirectoryAfterOp()
  8716. {
  8717. Configuration->AutoReadDirectoryAfterOp = !Configuration->AutoReadDirectoryAfterOp;
  8718. PostNote(FORMAT(LoadStrPart(AUTO_READ_DIRECTORY_TOGGLE, 1),
  8719. (LoadStrPart(AUTO_READ_DIRECTORY_TOGGLE,
  8720. (Configuration->AutoReadDirectoryAfterOp ? 2 : 3)))), 0, NULL, NULL);
  8721. }
  8722. //---------------------------------------------------------------------------
  8723. void __fastcall TCustomScpExplorerForm::StatusBarPanelDblClick(
  8724. TTBXCustomStatusBar * Sender, TTBXStatusPanel * Panel)
  8725. {
  8726. if (Panel->Index == 0)
  8727. {
  8728. if (FOnNoteClick != NULL)
  8729. {
  8730. // prevent the user data from being freed by possible call
  8731. // to CancelNote or PostNote during call to OnNoteClick
  8732. std::unique_ptr<TObject> NoteData(FNoteData);
  8733. TNotifyEvent OnNoteClick = FOnNoteClick;
  8734. FNoteData = NULL;
  8735. // need to cancel the note as we are going to delete its user data
  8736. CancelNote(true);
  8737. OnNoteClick(NoteData.get());
  8738. }
  8739. }
  8740. if (Panel->Index >= Sender->Panels->Count - SessionPanelCount)
  8741. {
  8742. FileSystemInfo();
  8743. }
  8744. }
  8745. //---------------------------------------------------------------------------
  8746. void __fastcall TCustomScpExplorerForm::LockWindow(bool Force)
  8747. {
  8748. // workaround:
  8749. // 1) for unknown reason, disabling window, while minimized,
  8750. // prevents it from restoring, even if it was enabled again meanwhile
  8751. // 2) when disabling the main window, while another has focus
  8752. // minimize is no longer possible
  8753. // ("keep up to date" dialog - though does not seem to be the case with "find file" -
  8754. // so it has probably something to do with the semi-modalness of the "keep up to date" dialog)
  8755. // Shouldn't we use IsApplicationMinimized() here?
  8756. if ((FLockSuspendLevel == 0) && !IsIconic(Application->Handle) && (Force || (Screen->ActiveForm == this)))
  8757. {
  8758. Enabled = false;
  8759. }
  8760. FLockLevel++;
  8761. }
  8762. //---------------------------------------------------------------------------
  8763. void __fastcall TCustomScpExplorerForm::UnlockWindow()
  8764. {
  8765. DebugAssert(FLockLevel > 0);
  8766. FLockLevel--;
  8767. if (FLockLevel == 0)
  8768. {
  8769. DebugAssert(FLockSuspendLevel == 0);
  8770. Enabled = true;
  8771. }
  8772. }
  8773. //---------------------------------------------------------------------------
  8774. void __fastcall TCustomScpExplorerForm::SuspendWindowLock()
  8775. {
  8776. // We need to make sure that window is enabled when the last modal window closes
  8777. // otherwise focus is not restored correctly.
  8778. // So we re-enable the window when modal window opens and
  8779. // disable it back after is closes
  8780. if (FLockLevel > 0)
  8781. {
  8782. // while we have nesting counter, we know that we never be called
  8783. // recursivelly as Application->OnModalBegin is called only
  8784. // for the top-level modal window
  8785. if (DebugAlwaysTrue(FLockSuspendLevel == 0))
  8786. {
  8787. // won't be disabled when conditions in LockWindow() were not satisfied
  8788. FDisabledOnLockSuspend = !Enabled;
  8789. // When minimized to tray (or actually when set to SW_HIDE),
  8790. // setting Enabled makes the window focusable even when there's
  8791. // modal window over it
  8792. if (!FTrayIcon->Visible)
  8793. {
  8794. Enabled = true;
  8795. }
  8796. }
  8797. FLockSuspendLevel++;
  8798. }
  8799. }
  8800. //---------------------------------------------------------------------------
  8801. void __fastcall TCustomScpExplorerForm::ResumeWindowLock()
  8802. {
  8803. if (FLockSuspendLevel > 0)
  8804. {
  8805. DebugAssert(FLockLevel > 0);
  8806. FLockSuspendLevel--;
  8807. // see comment in SuspendWindowLock
  8808. if (DebugAlwaysTrue(FLockSuspendLevel == 0))
  8809. {
  8810. // Note that window can be enabled here, when we were minized to tray when
  8811. // was SuspendWindowLock() called.
  8812. // We should possibly do the same check as in LockWindow(),
  8813. // if it is ever possible that the consitions change between
  8814. // SuspendWindowLock() and ResumeWindowLock()
  8815. Enabled = !FDisabledOnLockSuspend;
  8816. }
  8817. }
  8818. }
  8819. //---------------------------------------------------------------------------
  8820. void __fastcall TCustomScpExplorerForm::UpdateRemotePathComboBox(bool TextOnly)
  8821. {
  8822. if (!TextOnly)
  8823. {
  8824. TTBXComboBoxItem * RemotePathComboBox =
  8825. reinterpret_cast<TTBXComboBoxItem *>(GetComponent(fcRemotePathComboBox));
  8826. TStrings * Items = RemotePathComboBox->Strings;
  8827. Items->BeginUpdate();
  8828. try
  8829. {
  8830. Items->Clear();
  8831. if ((Terminal != NULL) && !RemoteDirView->Path.IsEmpty())
  8832. {
  8833. UnicodeString APath = UnixExcludeTrailingBackslash(RemoteDirView->Path);
  8834. while (!IsUnixRootPath(APath))
  8835. {
  8836. int P = APath.LastDelimiter(L'/');
  8837. DebugAssert(P >= 0);
  8838. Items->Insert(0, APath.SubString(P + 1, APath.Length() - P));
  8839. APath.SetLength(P - 1);
  8840. }
  8841. Items->Insert(0, Customunixdirview_SUnixDefaultRootName);
  8842. }
  8843. }
  8844. __finally
  8845. {
  8846. RemotePathComboBox->ItemIndex = Items->Count - 1;
  8847. // Setting ItemIndex to -1 does not reset its text
  8848. if (Items->Count == 0)
  8849. {
  8850. RemotePathComboBox->Text = L"";
  8851. }
  8852. Items->EndUpdate();
  8853. }
  8854. }
  8855. }
  8856. //---------------------------------------------------------------------------
  8857. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxAdjustImageIndex(
  8858. TTBXComboBoxItem * Sender, const UnicodeString /*AText*/, int AIndex,
  8859. int & ImageIndex)
  8860. {
  8861. if (AIndex < 0)
  8862. {
  8863. AIndex = Sender->ItemIndex;
  8864. }
  8865. ImageIndex = (AIndex < Sender->Strings->Count - 1 ? StdDirIcon : StdDirSelIcon);
  8866. }
  8867. //---------------------------------------------------------------------------
  8868. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxDrawItem(
  8869. TTBXCustomList * /*Sender*/, TCanvas * /*ACanvas*/, TRect & ARect, int AIndex,
  8870. int /*AHoverIndex*/, bool & /*DrawDefault*/)
  8871. {
  8872. ARect.Left += (10 * AIndex);
  8873. }
  8874. //---------------------------------------------------------------------------
  8875. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxMeasureWidth(
  8876. TTBXCustomList * /*Sender*/, TCanvas * /*ACanvas*/, int AIndex, int &AWidth)
  8877. {
  8878. AWidth += (10 * AIndex);
  8879. }
  8880. //---------------------------------------------------------------------------
  8881. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxItemClick(
  8882. TObject * Sender)
  8883. {
  8884. TTBXComboBoxItem * RemotePathComboBox = dynamic_cast<TTBXComboBoxItem*>(Sender);
  8885. UnicodeString APath = UnixExcludeTrailingBackslash(RemoteDirView->Path);
  8886. int Index = RemotePathComboBox->ItemIndex;
  8887. while (Index < RemotePathComboBox->Strings->Count - 1)
  8888. {
  8889. APath = UnixExtractFileDir(APath);
  8890. Index++;
  8891. }
  8892. // VanDyke style paths
  8893. if (APath.IsEmpty())
  8894. {
  8895. DebugAssert(RemotePathComboBox->ItemIndex == 0);
  8896. APath = ROOTDIRECTORY;
  8897. }
  8898. if (RemoteDirView->Path != APath)
  8899. {
  8900. RemoteDirView->Path = APath;
  8901. }
  8902. }
  8903. //---------------------------------------------------------------------------
  8904. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxCancel(TObject * Sender)
  8905. {
  8906. DebugAssert(Sender == GetComponent(fcRemotePathComboBox));
  8907. DebugUsedParam(Sender);
  8908. UpdateRemotePathComboBox(true);
  8909. }
  8910. //---------------------------------------------------------------------------
  8911. void __fastcall TCustomScpExplorerForm::DirViewEditing(
  8912. TObject * Sender, TListItem * Item, bool & /*AllowEdit*/)
  8913. {
  8914. ResetIncrementalSearch();
  8915. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  8916. DebugAssert(DirView != NULL);
  8917. if (!WinConfiguration->RenameWholeName && !DirView->ItemIsDirectory(Item))
  8918. {
  8919. HWND Edit = ListView_GetEditControl(DirView->Handle);
  8920. // OnEditing is called also from TCustomListView::CanEdit
  8921. if (Edit != NULL)
  8922. {
  8923. EditSelectBaseName(Edit);
  8924. }
  8925. }
  8926. }
  8927. //---------------------------------------------------------------------------
  8928. //---------------------------------------------------------------------------
  8929. TDragDropFilesEx * __fastcall TCustomScpExplorerForm::CreateDragDropFilesEx()
  8930. {
  8931. TDragDropFilesEx * Result = new TDragDropFilesEx(this);
  8932. Result->AutoDetectDnD = false;
  8933. Result->NeedValid = TFileExMustDnDSet() << nvFilename;
  8934. Result->RenderDataOn = rdoEnterAndDropSync;
  8935. Result->TargetEffects = TDropEffectSet() << deCopy << deMove;
  8936. return Result;
  8937. }
  8938. //---------------------------------------------------------------------------
  8939. void __fastcall TCustomScpExplorerForm::CreateWnd()
  8940. {
  8941. TForm::CreateWnd();
  8942. if (FSessionsDragDropFilesEx == NULL)
  8943. {
  8944. FSessionsDragDropFilesEx = CreateDragDropFilesEx();
  8945. FSessionsDragDropFilesEx->OnDragOver = SessionsDDDragOver;
  8946. FSessionsDragDropFilesEx->OnProcessDropped = SessionsDDProcessDropped;
  8947. FSessionsDragDropFilesEx->OnDragEnter = SessionsDDDragEnter;
  8948. FSessionsDragDropFilesEx->OnDragLeave = SessionsDDDragLeave;
  8949. }
  8950. if (FQueueDragDropFilesEx == NULL)
  8951. {
  8952. FQueueDragDropFilesEx = CreateDragDropFilesEx();
  8953. // No need to set OnDragOver as we do not have any restrictions
  8954. FQueueDragDropFilesEx->OnProcessDropped = QueueDDProcessDropped;
  8955. FQueueDragDropFilesEx->OnDragEnter = QueueDDDragEnter;
  8956. FQueueDragDropFilesEx->OnDragLeave = QueueDDDragLeave;
  8957. }
  8958. }
  8959. //---------------------------------------------------------------------------
  8960. void __fastcall TCustomScpExplorerForm::DestroyWnd()
  8961. {
  8962. TForm::DestroyWnd();
  8963. FSessionsDragDropFilesEx->DragDropControl = NULL;
  8964. FQueueDragDropFilesEx->DragDropControl = NULL;
  8965. }
  8966. //---------------------------------------------------------------------------
  8967. void __fastcall TCustomScpExplorerForm::FormShow(TObject * /*Sender*/)
  8968. {
  8969. SideEnter(FCurrentSide);
  8970. FEverShown = true;
  8971. }
  8972. //---------------------------------------------------------------------------
  8973. void __fastcall TCustomScpExplorerForm::DoFindFiles(
  8974. TTerminal * ATerminal, UnicodeString Directory, const TFileMasks & FileMask,
  8975. TFileFoundEvent OnFileFound, TFindingFileEvent OnFindingFile)
  8976. {
  8977. if (!NonVisualDataModule->Busy)
  8978. {
  8979. TTerminalManager::Instance()->ActiveTerminal = DebugNotNull(dynamic_cast<TManagedTerminal *>(ATerminal));
  8980. Configuration->Usage->Inc(L"FileFinds");
  8981. LockWindow(true);
  8982. NonVisualDataModule->StartBusy();
  8983. WinConfiguration->LockedInterface = true;
  8984. try
  8985. {
  8986. FTerminal->FilesFind(Directory, FileMask, OnFileFound, OnFindingFile);
  8987. }
  8988. __finally
  8989. {
  8990. WinConfiguration->LockedInterface = false;
  8991. NonVisualDataModule->EndBusy();
  8992. UnlockWindow();
  8993. }
  8994. }
  8995. }
  8996. //---------------------------------------------------------------------------
  8997. void __fastcall TCustomScpExplorerForm::DoFocusRemotePath(TTerminal * ATerminal, const UnicodeString & Path)
  8998. {
  8999. if (!NonVisualDataModule->Busy)
  9000. {
  9001. TTerminalManager::Instance()->ActiveTerminal = DebugNotNull(dynamic_cast<TManagedTerminal *>(ATerminal));
  9002. SetFocus();
  9003. RemoteDirView->Path = UnixExtractFilePath(Path);
  9004. UnicodeString FileName = UnixExtractFileName(Path);
  9005. TListItem * Item = RemoteDirView->FindFileItem(FileName);
  9006. // If not found, it can because we have loaded directory from cache and the file is new, try again after reload.
  9007. // (tbd: we should not even try this, if we have not loaded the directory from the cache)
  9008. if (Item == NULL)
  9009. {
  9010. ReloadDirectory(osRemote);
  9011. Item = RemoteDirView->FindFileItem(FileName);
  9012. }
  9013. if (Item != NULL)
  9014. {
  9015. RemoteDirView->ItemFocused = Item;
  9016. RemoteDirView->ItemFocused->MakeVisible(false);
  9017. RemoteDirView->SetFocus();
  9018. }
  9019. }
  9020. }
  9021. //---------------------------------------------------------------------------
  9022. bool __fastcall TCustomScpExplorerForm::CanOperateOnFoundFiles(TTerminal * ATerminal)
  9023. {
  9024. bool Result = !NonVisualDataModule->Busy;
  9025. if (Result)
  9026. {
  9027. TTerminalManager::Instance()->ActiveTerminal = DebugNotNull(dynamic_cast<TManagedTerminal *>(ATerminal));
  9028. }
  9029. return Result;
  9030. }
  9031. //---------------------------------------------------------------------------
  9032. void __fastcall TCustomScpExplorerForm::DoOperationOnFoundFiles(
  9033. TFileOperation Operation, TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  9034. {
  9035. if (CanOperateOnFoundFiles(ATerminal))
  9036. {
  9037. TValueRestorer<TFileOperationFinishedEvent> OnFileOperationFinishedRestorer(FOnFileOperationFinished);
  9038. FOnFileOperationFinished = OnFileOperationFinished;
  9039. ExecuteFileOperation(Operation, osRemote, FileList, false, NULL);
  9040. }
  9041. }
  9042. //---------------------------------------------------------------------------
  9043. void __fastcall TCustomScpExplorerForm::DoDeleteFoundFiles(
  9044. TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  9045. {
  9046. DoOperationOnFoundFiles(foDelete, ATerminal, FileList, OnFileOperationFinished);
  9047. }
  9048. //---------------------------------------------------------------------------
  9049. void __fastcall TCustomScpExplorerForm::DoDownloadFoundFiles(
  9050. TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  9051. {
  9052. DoOperationOnFoundFiles(foCopy, ATerminal, FileList, OnFileOperationFinished);
  9053. }
  9054. //---------------------------------------------------------------------------
  9055. void __fastcall TCustomScpExplorerForm::DoEditFoundFiles(
  9056. TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  9057. {
  9058. if (CanOperateOnFoundFiles(ATerminal))
  9059. {
  9060. for (int Index = 0; Index < FileList->Count; Index++)
  9061. {
  9062. UnicodeString FileName = FileList->Strings[Index];
  9063. TRemoteFile * File = static_cast<TRemoteFile *>(FileList->Objects[Index]);
  9064. ExecuteRemoteFile(FileName, File, efDefaultEditor);
  9065. OnFileOperationFinished(FileName, true);
  9066. }
  9067. }
  9068. }
  9069. //---------------------------------------------------------------------------
  9070. void __fastcall TCustomScpExplorerForm::RemoteFindFiles()
  9071. {
  9072. FFileFindTerminal = Terminal;
  9073. ShowFileFindDialog(
  9074. Terminal, RemoteDirView->Path, DoFindFiles, DoFocusRemotePath, DoDeleteFoundFiles,
  9075. DoDownloadFoundFiles, DoEditFoundFiles);
  9076. }
  9077. //---------------------------------------------------------------------------
  9078. void __fastcall TCustomScpExplorerForm::UpdateTaskbarList(ITaskbarList3 * TaskbarList)
  9079. {
  9080. FTaskbarList = TaskbarList;
  9081. }
  9082. //---------------------------------------------------------------------------
  9083. void __fastcall TCustomScpExplorerForm::SessionsPageControlMouseDown(
  9084. TObject * /*Sender*/, TMouseButton Button, TShiftState /*Shift*/, int X, int Y)
  9085. {
  9086. int Index = SessionsPageControl->IndexOfTabAt(X, Y);
  9087. if (Index >= 0)
  9088. {
  9089. if (Button == mbLeft)
  9090. {
  9091. // "Mouse down" is raised only after tab is switched.
  9092. // If switching tab (switching session) takes long enough for user
  9093. // to actually release the button, "mouse down" is still raised,
  9094. // but we do not get "mouse up" event, so dragging is not cancelled,
  9095. // prevent that by not beginning dragging in the first place.
  9096. if (IsKeyPressed(VK_LBUTTON))
  9097. {
  9098. // when user clicks the "+", we get mouse down only after the session
  9099. // is closed, when new session tab is already on X:Y, so dragging
  9100. // starts, prevent that
  9101. if (MilliSecondsBetween(Now(), FSessionsPageControlNewSessionTime) > 500)
  9102. {
  9103. TTerminal * Terminal = GetSessionTabTerminal(SessionsPageControl->Pages[Index]);
  9104. if (Terminal != NULL)
  9105. {
  9106. SessionsPageControl->BeginDrag(false);
  9107. }
  9108. }
  9109. }
  9110. }
  9111. else if (Button == mbMiddle)
  9112. {
  9113. // ignore middle-click for "New session tab"
  9114. TTerminal * Terminal = GetSessionTabTerminal(SessionsPageControl->Pages[Index]);
  9115. if (Terminal != NULL)
  9116. {
  9117. CloseSessionTab(Index);
  9118. }
  9119. }
  9120. }
  9121. }
  9122. //---------------------------------------------------------------------------
  9123. void __fastcall TCustomScpExplorerForm::SessionsPageControlDragDrop(
  9124. TObject * /*Sender*/, TObject * /*Source*/, int X, int Y)
  9125. {
  9126. int Index = SessionsPageControl->IndexOfTabAt(X, Y);
  9127. // do not allow dropping on the "+" tab
  9128. TTerminal * TargetTerminal = GetSessionTabTerminal(SessionsPageControl->Pages[Index]);
  9129. if ((TargetTerminal != NULL) &&
  9130. (SessionsPageControl->ActivePage->PageIndex != Index))
  9131. {
  9132. Configuration->Usage->Inc(L"SessionTabMoves");
  9133. // this is almost redundant as we would recreate tabs in DoTerminalListChanged,
  9134. // but we want to actually prevent that to avoid flicker
  9135. SessionsPageControl->ActivePage->PageIndex = Index;
  9136. TTerminal * Terminal = GetSessionTabTerminal(SessionsPageControl->ActivePage);
  9137. TTerminalManager::Instance()->Move(Terminal, TargetTerminal);
  9138. }
  9139. }
  9140. //---------------------------------------------------------------------------
  9141. void __fastcall TCustomScpExplorerForm::SessionsPageControlDragOver(
  9142. TObject * Sender, TObject * Source, int X, int Y,
  9143. TDragState /*State*/, bool & Accept)
  9144. {
  9145. Accept = (Sender == Source);
  9146. if (Accept)
  9147. {
  9148. int Index = SessionsPageControl->IndexOfTabAt(X, Y);
  9149. TTerminal * Terminal = GetSessionTabTerminal(SessionsPageControl->Pages[Index]);
  9150. // do not allow dragging to the "+" tab
  9151. Accept = (Terminal != NULL);
  9152. }
  9153. }
  9154. //---------------------------------------------------------------------------
  9155. void __fastcall TCustomScpExplorerForm::SessionsDDDragOver(int /*KeyState*/,
  9156. const TPoint & Point, int & Effect)
  9157. {
  9158. int Index = SessionsPageControl->IndexOfTabAt(Point.X, Point.Y);
  9159. if (Index < 0)
  9160. {
  9161. Effect = DROPEFFECT_NONE;
  9162. }
  9163. else
  9164. {
  9165. TTerminal * TargetTerminal = GetSessionTabTerminal(SessionsPageControl->Pages[Index]);
  9166. // do not allow dropping on the "+" tab
  9167. if (TargetTerminal == NULL)
  9168. {
  9169. Effect = DROPEFFECT_NONE;
  9170. }
  9171. else if ((TargetTerminal != Terminal) && (Effect == DROPEFFECT_MOVE))
  9172. {
  9173. Effect = DROPEFFECT_COPY;
  9174. }
  9175. }
  9176. }
  9177. //---------------------------------------------------------------------------
  9178. void __fastcall TCustomScpExplorerForm::SessionsDDProcessDropped(
  9179. TObject * /*Sender*/, int /*KeyState*/, const TPoint & Point, int Effect)
  9180. {
  9181. int Index = SessionsPageControl->IndexOfTabAt(Point.X, Point.Y);
  9182. // do not allow dropping on the "+" tab
  9183. TManagedTerminal * TargetTerminal = GetSessionTabTerminal(SessionsPageControl->Pages[Index]);
  9184. if (TargetTerminal != NULL)
  9185. {
  9186. DebugAssert(!IsFileControl(DropSourceControl, osRemote));
  9187. if (!IsFileControl(DropSourceControl, osRemote))
  9188. {
  9189. TTerminalManager::Instance()->ActiveTerminal = TargetTerminal;
  9190. RemoteFileControlDragDropFileOperation(SessionsPageControl, Effect,
  9191. // Why don't we use Terminal->CurrentDirectory directly?
  9192. TTerminalManager::Instance()->ActiveTerminal->CurrentDirectory,
  9193. // do not force queue, drag drop
  9194. false, true);
  9195. }
  9196. }
  9197. }
  9198. //---------------------------------------------------------------------------
  9199. void __fastcall TCustomScpExplorerForm::QueueDDProcessDropped(
  9200. TObject * /*Sender*/, int /*KeyState*/, const TPoint & /*Point*/, int Effect)
  9201. {
  9202. // Downloads are handled in RemoteFileControlDDEnd
  9203. if (!IsFileControl(DropSourceControl, osRemote))
  9204. {
  9205. RemoteFileControlDragDropFileOperation(QueueView3, Effect,
  9206. Terminal->CurrentDirectory,
  9207. // force queue, drag drop
  9208. true, true);
  9209. }
  9210. }
  9211. //---------------------------------------------------------------------------
  9212. void __fastcall TCustomScpExplorerForm::FormClose(TObject * /*Sender*/, TCloseAction & /*Action*/)
  9213. {
  9214. FShowing = false;
  9215. // Do not save empty workspace
  9216. if (WinConfiguration->AutoSaveWorkspace && (Terminal != NULL))
  9217. {
  9218. std::unique_ptr<TObjectList> DataList(DoCollectWorkspace());
  9219. UnicodeString Name = WorkspaceName();
  9220. DoSaveWorkspace(
  9221. Name, DataList.get(),
  9222. !Configuration->DisablePasswordStoring &&
  9223. WinConfiguration->AutoSaveWorkspacePasswords, false);
  9224. WinConfiguration->LastStoredSession = Name;
  9225. }
  9226. }
  9227. //---------------------------------------------------------------------------
  9228. void __fastcall TCustomScpExplorerForm::RemoteDirViewRead(TObject * /*Sender*/)
  9229. {
  9230. TManagedTerminal * ManagedTerminal =
  9231. dynamic_cast<TManagedTerminal *>(RemoteDirView->Terminal);
  9232. if (ManagedTerminal != NULL)
  9233. {
  9234. ManagedTerminal->DirectoryLoaded = Now();
  9235. }
  9236. }
  9237. //---------------------------------------------------------------------------
  9238. void __fastcall TCustomScpExplorerForm::DirViewSelectItem(TObject * Sender,
  9239. TListItem * /*Item*/, bool /*Selected*/)
  9240. {
  9241. TCustomDirView * DirView = reinterpret_cast<TCustomDirView *>(Sender);
  9242. switch (DirView->LastSelectMethod)
  9243. {
  9244. case smKeyboard:
  9245. Configuration->Usage->Inc(L"KeyboardSelections");
  9246. break;
  9247. case smMouse:
  9248. Configuration->Usage->Inc(L"MouseSelections");
  9249. break;
  9250. }
  9251. }
  9252. //---------------------------------------------------------------------------
  9253. int __fastcall TCustomScpExplorerForm::AddSessionColor(TColor Color)
  9254. {
  9255. if (Color != 0)
  9256. {
  9257. return GetSessionColorImage(FSessionColors, Color, FSessionColorMaskImageIndex);
  9258. }
  9259. else
  9260. {
  9261. return FSessionTabImageIndex;
  9262. }
  9263. }
  9264. //---------------------------------------------------------------------------
  9265. int __fastcall TCustomScpExplorerForm::AddFixedSessionImage(int GlyphsSourceIndex)
  9266. {
  9267. TPngImageCollectionItem * Item =
  9268. GlyphsModule->ExplorerImages->PngImages->Items[GlyphsSourceIndex];
  9269. FSessionColors->AddPng(Item->PngImage);
  9270. return FSessionColors->Count - 1;
  9271. }
  9272. //---------------------------------------------------------------------------
  9273. void __fastcall TCustomScpExplorerForm::AddFixedSessionImages()
  9274. {
  9275. FSessionColors->SetSize(GlyphsModule->ExplorerImages->Width, GlyphsModule->ExplorerImages->Height);
  9276. FNewSessionTabImageIndex = AddFixedSessionImage(NonVisualDataModule->NewSessionAction->ImageIndex);
  9277. FSessionTabImageIndex = AddFixedSessionImage(SiteImageIndex);
  9278. FSessionColorMaskImageIndex = AddFixedSessionImage(SiteColorMaskImageIndex);
  9279. }
  9280. //---------------------------------------------------------------------------
  9281. void __fastcall TCustomScpExplorerForm::CollectItemsWithTextDisplayMode(TWinControl * Control)
  9282. {
  9283. for (int Index = 0; Index < Control->ControlCount; Index++)
  9284. {
  9285. TControl * AControl = Control->Controls[Index];
  9286. TWinControl * WinControl = dynamic_cast<TWinControl *>(AControl);
  9287. if (WinControl != NULL)
  9288. {
  9289. CollectItemsWithTextDisplayMode(WinControl);
  9290. }
  9291. TTBCustomToolbar * Toolbar = dynamic_cast<TTBCustomToolbar *>(AControl);
  9292. if (Toolbar != NULL)
  9293. {
  9294. // we care for top-level items only
  9295. TTBCustomItem * Items = Toolbar->Items;
  9296. for (int ItemIndex = 0; ItemIndex < Items->Count; ItemIndex++)
  9297. {
  9298. TTBCustomItem * Item = Items->Items[ItemIndex];
  9299. if (((Item->DisplayMode == nbdmImageAndText) ||
  9300. (dynamic_cast<TTBXLabelItem *>(Item) != NULL)) &&
  9301. EligibleForImageDisplayMode(Item))
  9302. {
  9303. FItemsWithTextDisplayMode.insert(Item);
  9304. }
  9305. }
  9306. }
  9307. }
  9308. }
  9309. //---------------------------------------------------------------------------
  9310. bool __fastcall TCustomScpExplorerForm::EligibleForImageDisplayMode(TTBCustomItem * /*Item*/)
  9311. {
  9312. return true;
  9313. }
  9314. //---------------------------------------------------------------------------
  9315. bool __fastcall TCustomScpExplorerForm::UpdateToolbarDisplayMode()
  9316. {
  9317. bool SelectiveToolbarText = WinConfiguration->SelectiveToolbarText;
  9318. TTBItemDisplayMode DisplayMode = (SelectiveToolbarText ? nbdmImageAndText : nbdmDefault);
  9319. typedef std::set<TTBCustomItem *> TItemsWithTextDisplayMode;
  9320. TItemsWithTextDisplayMode::iterator i = FItemsWithTextDisplayMode.begin();
  9321. bool Result = true;
  9322. while (Result && (i != FItemsWithTextDisplayMode.end()))
  9323. {
  9324. TTBCustomItem * Item = *i;
  9325. TTBXLabelItem * Label = dynamic_cast<TTBXLabelItem *>(Item);
  9326. if (Label != NULL)
  9327. {
  9328. // optimization
  9329. if (Label->Visible == SelectiveToolbarText)
  9330. {
  9331. Result = false;
  9332. }
  9333. else
  9334. {
  9335. Label->Visible = SelectiveToolbarText;
  9336. }
  9337. }
  9338. else
  9339. {
  9340. // optimization
  9341. if (Item->DisplayMode == DisplayMode)
  9342. {
  9343. Result = false;
  9344. }
  9345. else
  9346. {
  9347. Item->DisplayMode = DisplayMode;
  9348. }
  9349. }
  9350. ++i;
  9351. }
  9352. if (Result)
  9353. {
  9354. UpdateNewSessionTab();
  9355. }
  9356. return Result;
  9357. }
  9358. //---------------------------------------------------------------------------
  9359. void __fastcall TCustomScpExplorerForm::DisplaySystemContextMenu()
  9360. {
  9361. DebugFail();
  9362. }
  9363. //---------------------------------------------------------------------------
  9364. bool __fastcall TCustomScpExplorerForm::IsBusy()
  9365. {
  9366. // Among other, a lock level is non zero, while directory is being loaded,
  9367. // even when it happens as a result of dirview navigation,
  9368. // i.e. when TNonVisualDataModule is NOT FBusy.
  9369. // That's why the TNonVisualDataModule::GetBusy calls this method.
  9370. // Among other this prevents a panel auto update to occur while
  9371. // directory is changing.
  9372. return (FLockLevel > 0) || DirView(osCurrent)->IsEditing();
  9373. }
  9374. //---------------------------------------------------------------------------
  9375. Boolean __fastcall TCustomScpExplorerForm::AllowedAction(TAction * /*Action*/, TActionAllowed Allowed)
  9376. {
  9377. // While the window is disabled, we still seem to process menu shortcuts at least,
  9378. // so stop it at least here.
  9379. // See also TCustomScpExplorerForm::RemoteDirViewBusy
  9380. return
  9381. (Allowed == aaUpdate) ||
  9382. !NonVisualDataModule->Busy;
  9383. }
  9384. //---------------------------------------------------------------------------
  9385. void __fastcall TCustomScpExplorerForm::EditMenuItemPopup(TTBCustomItem * Sender, bool FromLink)
  9386. {
  9387. NonVisualDataModule->EditMenuItemPopup(Sender, FromLink);
  9388. }
  9389. //---------------------------------------------------------------------------
  9390. void __fastcall TCustomScpExplorerForm::DirViewBusy(TObject * /*Sender*/, int Busy, bool & State)
  9391. {
  9392. // This is somewhat redundant to LockWindow() call from
  9393. // TTerminalManager::TerminalReadDirectoryProgress.
  9394. // But disabling window is known not to block keyboard shorcuts
  9395. // (see TCustomScpExplorerForm::AllowedAction), this hopefully works.
  9396. if (Busy > 0)
  9397. {
  9398. if (NonVisualDataModule->Busy)
  9399. {
  9400. State = false;
  9401. }
  9402. else
  9403. {
  9404. NonVisualDataModule->StartBusy();
  9405. // Will be countered for TCustomDirView.DoExecute in DoDirViewExecFile
  9406. LockWindow();
  9407. }
  9408. }
  9409. else if (Busy < 0)
  9410. {
  9411. UnlockWindow();
  9412. NonVisualDataModule->EndBusy();
  9413. }
  9414. else
  9415. {
  9416. State = NonVisualDataModule->Busy;
  9417. }
  9418. }
  9419. //---------------------------------------------------------------------------
  9420. void __fastcall TCustomScpExplorerForm::SessionsPageControlContextPopup(TObject * /*Sender*/, TPoint & MousePos, bool & Handled)
  9421. {
  9422. int Index = SessionsPageControl->IndexOfTabAt(MousePos.X, MousePos.Y);
  9423. // no context menu for "New session tab"
  9424. if ((Index >= 0) && (GetSessionTabTerminal(SessionsPageControl->Pages[Index]) != NULL))
  9425. {
  9426. SessionsPageControl->ActivePageIndex = Index;
  9427. if (DebugAlwaysTrue(SessionTabSwitched()))
  9428. {
  9429. // copied from TControl.WMContextMenu
  9430. SendCancelMode(SessionsPageControl);
  9431. // explicit popup instead of using PopupMenu property
  9432. // to avoid menu to popup somewhere within SessionTabSwitched above,
  9433. // while connecting yet not-connected session and hence
  9434. // allowing an access to commands over not-completelly connected session
  9435. TPoint Point = SessionsPageControl->ClientToScreen(MousePos);
  9436. TPopupMenu * PopupMenu = NonVisualDataModule->SessionsPopup;
  9437. PopupMenu->PopupComponent = SessionsPageControl;
  9438. PopupMenu->Popup(Point.x, Point.y);
  9439. }
  9440. }
  9441. Handled = true;
  9442. }
  9443. //---------------------------------------------------------------------------
  9444. bool __fastcall TCustomScpExplorerForm::CanChangePassword()
  9445. {
  9446. return (Terminal != NULL) && Terminal->Active && Terminal->IsCapable[fcChangePassword];
  9447. }
  9448. //---------------------------------------------------------------------------
  9449. void __fastcall TCustomScpExplorerForm::ChangePassword()
  9450. {
  9451. Configuration->Usage->Inc(L"PasswordChanges");
  9452. std::unique_ptr<TSessionData> ChangePasswordData(Terminal->SessionData->Clone());
  9453. ChangePasswordData->DisableAuthentationsExceptPassword();
  9454. ChangePasswordData->ChangePassword = true;
  9455. std::unique_ptr<TTerminal> ChangePasswordSession(
  9456. Terminal->CreateSecondarySession(L"ChangePassword", ChangePasswordData.get()));
  9457. ChangePasswordSession->AutoReadDirectory = false;
  9458. if (TTerminalManager::Instance()->ConnectTerminal(ChangePasswordSession.get()))
  9459. {
  9460. MessageDialog(MainInstructions(LoadStr(PASSWORD_CHANGED)), qtInformation, qaOK, HELP_CHANGE_PASSWORD);
  9461. }
  9462. else
  9463. {
  9464. Configuration->Usage->Inc(L"PasswordChangesFailed");
  9465. }
  9466. }
  9467. //---------------------------------------------------------------------------
  9468. bool __fastcall TCustomScpExplorerForm::CanPrivateKeyUpload()
  9469. {
  9470. // No nice way to assert SSH2
  9471. return (Terminal != NULL) && Terminal->Active && (Terminal->FSProtocol == cfsSFTP);
  9472. }
  9473. //---------------------------------------------------------------------------
  9474. void __fastcall TCustomScpExplorerForm::PrivateKeyUpload()
  9475. {
  9476. TOperationVisualizer OperationVisualizer;
  9477. UnicodeString FileName = Terminal->SessionData->PublicKeyFile;
  9478. TTerminalManager::Instance()->UploadPublicKey(Terminal, NULL, FileName);
  9479. }
  9480. //---------------------------------------------------------------------------
  9481. void __fastcall TCustomScpExplorerForm::ChangeScale(int M, int D)
  9482. {
  9483. TForm::ChangeScale(M, D);
  9484. int APixelsPerInch = GetControlPixelsPerInch(this);
  9485. HANDLE ResourceModule = GUIConfiguration->ChangeToDefaultResourceModule();
  9486. try
  9487. {
  9488. GlyphsModule->SetPixelsPerInch(APixelsPerInch);
  9489. }
  9490. __finally
  9491. {
  9492. GUIConfiguration->ChangeResourceModule(ResourceModule);
  9493. }
  9494. }
  9495. //---------------------------------------------------------------------------
  9496. void __fastcall TCustomScpExplorerForm::DockContextPopup(TObject * Sender, TPoint & MousePos, bool & /*Handled*/)
  9497. {
  9498. NonVisualDataModule->ControlContextPopup(Sender, MousePos);
  9499. }
  9500. //---------------------------------------------------------------------
  9501. class TPublicControl : public TControl
  9502. {
  9503. friend class TCustomScpExplorerForm;
  9504. };
  9505. //---------------------------------------------------------------------------
  9506. void __fastcall TCustomScpExplorerForm::CopyPopup(TControl * DestControl, TControl * SourceControl)
  9507. {
  9508. static_cast<TPublicControl *>(DestControl)->PopupMenu = static_cast<TPublicControl *>(SourceControl)->PopupMenu;
  9509. static_cast<TPublicControl *>(DestControl)->OnContextPopup = static_cast<TPublicControl *>(SourceControl)->OnContextPopup;
  9510. }
  9511. //---------------------------------------------------------------------------
  9512. void __fastcall TCustomScpExplorerForm::DoBookmarkClick(TOperationSide Side, TObject * Sender)
  9513. {
  9514. TBookmark * Bookmark = DebugNotNull(reinterpret_cast<TBookmark *>(dynamic_cast<TTBCustomItem *>(Sender)->Tag));
  9515. OpenBookmark(Side, Bookmark);
  9516. }
  9517. //---------------------------------------------------------------------------
  9518. void __fastcall TCustomScpExplorerForm::LocalBookmarkClick(TObject * Sender)
  9519. {
  9520. DoBookmarkClick(osLocal, Sender);
  9521. }
  9522. //---------------------------------------------------------------------------
  9523. void __fastcall TCustomScpExplorerForm::RemoteBookmarkClick(TObject * Sender)
  9524. {
  9525. DoBookmarkClick(osRemote, Sender);
  9526. }
  9527. //---------------------------------------------------------------------------
  9528. void __fastcall TCustomScpExplorerForm::CreateOpenDirMenuList(
  9529. TTBCustomItem * Menu, TOperationSide Side, TBookmarkList * BookmarkList)
  9530. {
  9531. // TODO
  9532. if (BookmarkList != NULL)
  9533. {
  9534. TNotifyEvent OnBookmarkClick = (Side == osLocal) ? &LocalBookmarkClick : &RemoteBookmarkClick;
  9535. if (!WinConfiguration->UseLocationProfiles)
  9536. {
  9537. std::unique_ptr<TStringList> Directories(new TStringList());
  9538. Directories->CaseSensitive = (Side == osRemote);
  9539. for (int Index = 0; Index < BookmarkList->Count; Index++)
  9540. {
  9541. TBookmark * Bookmark = BookmarkList->Bookmarks[Index];
  9542. UnicodeString Directory = Bookmark->GetSideDirectory(Side);
  9543. if (!Directory.IsEmpty() && (Directories->IndexOf(Directory) < 0))
  9544. {
  9545. std::unique_ptr<TTBCustomItem> Item(new TTBXItem(Owner));
  9546. Item->Caption = EscapeHotkey(Directory);
  9547. Item->ShortCut = Bookmark->ShortCut;
  9548. Item->OnClick = OnBookmarkClick;
  9549. Item->Tag = reinterpret_cast<int>(Bookmark);
  9550. Directories->Add(Directory);
  9551. Menu->Add(Item.release());
  9552. }
  9553. }
  9554. }
  9555. else
  9556. {
  9557. std::unique_ptr<TStrings> Folders(CreateSortedStringList());
  9558. for (int Index = 0; Index < BookmarkList->Count; Index++)
  9559. {
  9560. TBookmark * Bookmark = BookmarkList->Bookmarks[Index];
  9561. if (!Bookmark->Node.IsEmpty())
  9562. {
  9563. Folders->Add(Bookmark->Node);
  9564. }
  9565. }
  9566. for (int Index = 0; Index < Folders->Count; Index++)
  9567. {
  9568. std::unique_ptr<TTBCustomItem> Item(new TTBXSubmenuItem(Owner));
  9569. Item->Caption = Folders->Strings[Index];
  9570. Item->ImageIndex = NonVisualDataModule->RemoteChangePathAction->ImageIndex;
  9571. Folders->Objects[Index] = Item.get();
  9572. Menu->Add(Item.release());
  9573. }
  9574. for (int Index = 0; Index < BookmarkList->Count; Index++)
  9575. {
  9576. TBookmark * Bookmark = BookmarkList->Bookmarks[Index];
  9577. TTBCustomItem * Parent;
  9578. if (!Bookmark->Node.IsEmpty())
  9579. {
  9580. DebugAssert(Folders->IndexOf(Bookmark->Node) >= 0);
  9581. Parent = dynamic_cast<TTBCustomItem *>(Folders->Objects[Folders->IndexOf(Bookmark->Node)]);
  9582. }
  9583. else
  9584. {
  9585. Parent = Menu;
  9586. }
  9587. std::unique_ptr<TTBCustomItem> Item(new TTBXItem(Owner));
  9588. Item->Caption = Bookmark->Name;
  9589. Item->ShortCut = Bookmark->ShortCut;
  9590. Item->OnClick = OnBookmarkClick;
  9591. Item->Tag = reinterpret_cast<int>(Bookmark);
  9592. if (((Bookmark->Name == Bookmark->Local) || (Bookmark->Name == Bookmark->Remote)) &&
  9593. (Bookmark->Local.IsEmpty() || Bookmark->Remote.IsEmpty()))
  9594. {
  9595. // No hint for location profiles that are actually mere "bookmarks"
  9596. }
  9597. else
  9598. {
  9599. UnicodeString Hint = FORMAT(LoadStrPart(LOCATION_PROFILE_HINT, 1), (Bookmark->Name));
  9600. UnicodeString LongHint;
  9601. if (!Bookmark->Local.IsEmpty())
  9602. {
  9603. AddToList(LongHint, FORMAT(LoadStrPart(LOCATION_PROFILE_HINT, 2), (Bookmark->Local)), L"\n");
  9604. }
  9605. if (!Bookmark->Remote.IsEmpty())
  9606. {
  9607. AddToList(LongHint, FORMAT(LoadStrPart(LOCATION_PROFILE_HINT, 3), (Bookmark->Remote)), L"\n");
  9608. }
  9609. AddToList(Hint, LongHint, L"|");
  9610. Item->Hint = Hint;
  9611. }
  9612. Parent->Add(Item.release());
  9613. }
  9614. }
  9615. AddMenuSeparator(Menu);
  9616. }
  9617. }
  9618. //---------------------------------------------------------------------------
  9619. void __fastcall TCustomScpExplorerForm::CreateOpenDirMenu(TTBCustomItem * Menu, TOperationSide Side)
  9620. {
  9621. Menu->Clear();
  9622. std::unique_ptr<TTBCustomItem> Item;
  9623. Item.reset(new TTBXItem(Owner));
  9624. Item->Action = (Side == osLocal) ? NonVisualDataModule->LocalOpenDirAction : NonVisualDataModule->RemoteOpenDirAction;
  9625. Item->Options = Item->Options << tboDefault;
  9626. Menu->Add(Item.release());
  9627. AddMenuSeparator(Menu);
  9628. if (Terminal != NULL)
  9629. {
  9630. CreateOpenDirMenuList(Menu, Side, WinConfiguration->Bookmarks[Terminal->SessionData->SessionKey]);
  9631. }
  9632. CreateOpenDirMenuList(Menu, Side, WinConfiguration->SharedBookmarks);
  9633. Item.reset(new TTBXItem(Owner));
  9634. Item->Action = (Side == osLocal) ? NonVisualDataModule->LocalAddBookmarkAction : NonVisualDataModule->RemoteAddBookmarkAction;
  9635. Menu->Add(Item.release());
  9636. }
  9637. //---------------------------------------------------------------------------
  9638. bool __fastcall TCustomScpExplorerForm::TryOpenDirectory(TOperationSide Side, const UnicodeString & Path)
  9639. {
  9640. bool Result = true;
  9641. try
  9642. {
  9643. if (Side == osRemote)
  9644. {
  9645. Terminal->ExceptionOnFail = true;
  9646. }
  9647. try
  9648. {
  9649. DirView(Side)->Path = Path;
  9650. }
  9651. __finally
  9652. {
  9653. if (Side == osRemote)
  9654. {
  9655. Terminal->ExceptionOnFail = false;
  9656. }
  9657. }
  9658. }
  9659. catch (Exception & E)
  9660. {
  9661. if ((Side != osRemote) || Terminal->Active)
  9662. {
  9663. ShowExtendedExceptionEx(Terminal, &E);
  9664. Result = false;
  9665. }
  9666. else
  9667. {
  9668. throw;
  9669. }
  9670. }
  9671. return Result;
  9672. }
  9673. //---------------------------------------------------------------------------
  9674. void __fastcall TCustomScpExplorerForm::ReloadDirectory(TOperationSide Side)
  9675. {
  9676. // Make sure there some feedback even when loading tiny local folders
  9677. TInstantOperationVisualizer Visualizer;
  9678. DirView(Side)->ReloadDirectory();
  9679. }
  9680. //---------------------------------------------------------------------------
  9681. void __fastcall TCustomScpExplorerForm::CloseSessionTab(int Index)
  9682. {
  9683. if (Index == SessionsPageControl->TabIndex)
  9684. {
  9685. CloseSession();
  9686. }
  9687. else
  9688. {
  9689. TTabSheet * TabSheet = SessionsPageControl->Pages[Index];
  9690. TTerminal * Terminal = GetSessionTabTerminal(TabSheet);
  9691. TTerminalManager * Manager = TTerminalManager::Instance();
  9692. if (!Terminal->Active)
  9693. {
  9694. Manager->FreeTerminal(Terminal);
  9695. }
  9696. else
  9697. {
  9698. TTerminalQueue * Queue = Manager->FindQueueForTerminal(Terminal);
  9699. if (CanCloseQueue(Queue))
  9700. {
  9701. Manager->FreeTerminal(Terminal);
  9702. }
  9703. }
  9704. }
  9705. }
  9706. //---------------------------------------------------------------------------
  9707. void __fastcall TCustomScpExplorerForm::SessionsPageControlCloseButtonClick(TPageControl * /*Sender*/, int Index)
  9708. {
  9709. CloseSessionTab(Index);
  9710. }
  9711. //---------------------------------------------------------------------------
  9712. void __fastcall TCustomScpExplorerForm::CopyFilesToClipboard(TOperationSide Side, bool OnFocused)
  9713. {
  9714. if (DebugAlwaysTrue(!IsSideLocalBrowser(Side)))
  9715. {
  9716. TInstantOperationVisualizer Visualizer;
  9717. // To trigger ClipboardDataObjectRelease (in case the clipboard already contains our data)
  9718. // before we create a new data object
  9719. ClipboardClear();
  9720. DebugAssert(FClipboardFakeDirectory.IsEmpty());
  9721. DebugAssert(FClipboardFakeMonitors.get() == NULL);
  9722. FClipboardFakeDirectory = CreateFakeTransferDirectory();
  9723. // Intermediate variable to WORKAROUND a crash in compiler
  9724. TObjectList * ClipboardFakeMonitors =
  9725. StartCreationDirectoryMonitorsOnEachDrive(FILE_NOTIFY_CHANGE_DIR_NAME, ClipboardFakeCreated);
  9726. FClipboardFakeMonitors.reset(ClipboardFakeMonitors);
  9727. if (FClipboardDragDropFilesEx.get() == NULL)
  9728. {
  9729. FClipboardDragDropFilesEx.reset(new TDragDropFilesEx(this));
  9730. FClipboardDragDropFilesEx->OnDataObjectRelease = ClipboardDataObjectRelease;
  9731. }
  9732. FClipboardDragDropFilesEx->FileList->Clear();
  9733. FClipboardDragDropFilesEx->FileList->AddItem(NULL, FClipboardFakeDirectory);
  9734. FClipboardDragDropFilesEx->CopyToClipboard();
  9735. FClipboardTerminal = FTerminal;
  9736. // Need full paths, as cwd can be different once files are pasted
  9737. FClipboardFileList.reset(TRemoteFileList::CloneStrings(RemoteDirView->CreateFileList(OnFocused, true)));
  9738. for (int Index = 0; Index < FClipboardFileList->Count; Index++)
  9739. {
  9740. FClipboardFileList->Strings[Index] = UnixExcludeTrailingBackslash(FClipboardFileList->Strings[Index]);
  9741. }
  9742. }
  9743. }
  9744. //---------------------------------------------------------------------------
  9745. void __fastcall TCustomScpExplorerForm::ClipboardClear()
  9746. {
  9747. if (OpenClipboard(0))
  9748. {
  9749. EmptyClipboard(); // Calls ClipboardDataObjectRelease, if clipboard contains our data
  9750. CloseClipboard();
  9751. }
  9752. }
  9753. //---------------------------------------------------------------------------
  9754. bool __fastcall TCustomScpExplorerForm::DoesClipboardContainOurFiles()
  9755. {
  9756. return !FClipboardFakeDirectory.IsEmpty();
  9757. }
  9758. //---------------------------------------------------------------------------
  9759. void __fastcall TCustomScpExplorerForm::ClipboardStop()
  9760. {
  9761. RemoveDir(ApiPath(FClipboardFakeDirectory));
  9762. FClipboardFakeDirectory = UnicodeString();
  9763. FClipboardTerminal = NULL;
  9764. // Also called in Idle()
  9765. FClipboardFakeMonitors.reset(NULL);
  9766. }
  9767. //---------------------------------------------------------------------------
  9768. void __fastcall TCustomScpExplorerForm::ClipboardDataObjectRelease(TObject * /*Sender*/)
  9769. {
  9770. if (DebugAlwaysTrue(!FClipboardFakeDirectory.IsEmpty()))
  9771. {
  9772. ClipboardStop();
  9773. }
  9774. }
  9775. //---------------------------------------------------------------------------
  9776. void __fastcall TCustomScpExplorerForm::ClipboardDownload(const UnicodeString & TargetDirectory, bool NoConfirmation, bool DragDrop)
  9777. {
  9778. TTerminalManager * Manager = TTerminalManager::Instance();
  9779. Manager->ActiveTerminal = FClipboardTerminal;
  9780. TTransferOperationParam Params;
  9781. Params.TargetDirectory = TargetDirectory;
  9782. Params.DragDrop = DragDrop;
  9783. ExecuteFileOperation(foCopy, osRemote, FClipboardFileList.get(), NoConfirmation, &Params);
  9784. }
  9785. //---------------------------------------------------------------------------
  9786. void __fastcall TCustomScpExplorerForm::ClipboardFakeCreated(TObject * /*Sender*/, const UnicodeString FileName)
  9787. {
  9788. // It can actually rarelly happen that some random file is created, while we are shuttting down the monitor
  9789. // (as it pumps a Windows message queue while being shutted down)
  9790. if (DebugAlwaysTrue(!FClipboardFakeDirectory.IsEmpty()) &&
  9791. SameText(ExtractFileName(FileName), ExtractFileName(FClipboardFakeDirectory)))
  9792. {
  9793. // Can fail as it can be e.g. locked by AV, so we retry that later
  9794. bool Removed = RemoveDir(ApiPath(FileName));
  9795. try
  9796. {
  9797. if (!NonVisualDataModule->Busy)
  9798. {
  9799. bool NoConfirmation = (WinConfiguration->DDTransferConfirmation == asOff);
  9800. ClipboardDownload(ExtractFilePath(FileName), NoConfirmation, true);
  9801. }
  9802. }
  9803. __finally
  9804. {
  9805. if (!Removed)
  9806. {
  9807. RemoveDir(ApiPath(FileName));
  9808. }
  9809. }
  9810. }
  9811. }
  9812. //---------------------------------------------------------------------------
  9813. void __fastcall TCustomScpExplorerForm::DirViewGetItemColor(
  9814. TObject * Sender, UnicodeString FileName, bool Directory, __int64 Size, TDateTime Modification, TColor & Color)
  9815. {
  9816. TFileMasks::TParams MaskParams;
  9817. MaskParams.Size = Size;
  9818. MaskParams.Modification = Modification;
  9819. TCustomDirView * DirView = DebugNotNull(dynamic_cast<TCustomDirView *>(Sender));
  9820. for (TFileColorData::TList::const_iterator Iter = FFileColors.begin(); Iter != FFileColors.end(); Iter++)
  9821. {
  9822. bool ImplicitMatch;
  9823. if (Iter->FileMask.Matches(FileName, Directory, DirView->PathName, &MaskParams, false, ImplicitMatch) &&
  9824. !ImplicitMatch)
  9825. {
  9826. Color = Iter->Color;
  9827. break;
  9828. }
  9829. }
  9830. }
  9831. //---------------------------------------------------------------------------
  9832. void __fastcall TCustomScpExplorerForm::DirViewKeyDown(TObject * Sender, WORD & Key, TShiftState)
  9833. {
  9834. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  9835. if (!DirView->IsEditing())
  9836. {
  9837. // Handled here, instead of RemoteDirViewKeyPress (as on Login dialog),
  9838. // as VK_BACK is handled by TCustomDirView.KeyDown and we can swallow it here,
  9839. // while searching
  9840. if (Key == VK_BACK)
  9841. {
  9842. if (!FIncrementalSearch.IsEmpty())
  9843. {
  9844. if (FIncrementalSearch.Length() == 1)
  9845. {
  9846. ResetIncrementalSearch();
  9847. }
  9848. else
  9849. {
  9850. UnicodeString NewText = FIncrementalSearch.SubString(1, FIncrementalSearch.Length() - 1);
  9851. IncrementalSearch(NewText, false, false);
  9852. }
  9853. Key = 0;
  9854. }
  9855. }
  9856. }
  9857. }
  9858. //---------------------------------------------------------------------------
  9859. void __fastcall TCustomScpExplorerForm::DirViewKeyPress(TObject * Sender, wchar_t & Key)
  9860. {
  9861. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  9862. if (!DirView->IsEditing() && (WinConfiguration->PanelSearch != isOff))
  9863. {
  9864. // Filter control sequences.
  9865. // When not searching yet, prefer use of the space for toggling file selection
  9866. // (so we cannot incrementally search for a string starting with a space).
  9867. if ((Key > VK_SPACE) ||
  9868. ((Key == VK_SPACE) && (GetKeyState(VK_CONTROL) >= 0) && !FIncrementalSearch.IsEmpty()))
  9869. {
  9870. IncrementalSearch(FIncrementalSearch + Key, false, false);
  9871. Key = 0;
  9872. }
  9873. }
  9874. }
  9875. //---------------------------------------------------------------------------
  9876. void __fastcall TCustomScpExplorerForm::ResetIncrementalSearch()
  9877. {
  9878. if (!FIncrementalSearch.IsEmpty())
  9879. {
  9880. FIncrementalSearch = L"";
  9881. DirView(osCurrent)->UpdateStatusBar();
  9882. }
  9883. }
  9884. //---------------------------------------------------------------------------
  9885. void __fastcall TCustomScpExplorerForm::IncrementalSearch(const UnicodeString & Text, bool SkipCurrent, bool Reverse)
  9886. {
  9887. TListItem * Item = SearchFile(Text, SkipCurrent, Reverse);
  9888. if (Item == NULL)
  9889. {
  9890. MessageBeep(MB_ICONHAND);
  9891. }
  9892. else
  9893. {
  9894. TCustomDirView * ADirView = DirView(osCurrent);
  9895. {
  9896. TAutoNestingCounter Guard(FIncrementalSearching);
  9897. ADirView->FocusItem(Item);
  9898. if (ADirView->NortonLike == nlOff)
  9899. {
  9900. if ((ADirView->Selected != Item) ||
  9901. (ADirView->SelCount > 1))
  9902. {
  9903. ADirView->ClearSelection();
  9904. ADirView->Selected = Item;
  9905. }
  9906. }
  9907. }
  9908. FIncrementalSearch = Text;
  9909. Item->MakeVisible(false);
  9910. TListItem * NextItem = SearchFile(Text, true, Reverse);
  9911. FIncrementalSearchHaveNext = (NextItem != NULL) && (NextItem != Item);
  9912. ADirView->UpdateStatusBar();
  9913. }
  9914. }
  9915. //---------------------------------------------------------------------------
  9916. TListItem * __fastcall TCustomScpExplorerForm::GetNextFile(TListItem * Item, bool Reverse)
  9917. {
  9918. int Index = Item->Index;
  9919. if (!Reverse)
  9920. {
  9921. Index++;
  9922. if (Index >= Item->Owner->Count)
  9923. {
  9924. Index = 0;
  9925. }
  9926. }
  9927. else
  9928. {
  9929. Index--;
  9930. if (Index < 0)
  9931. {
  9932. Index = Item->Owner->Count - 1;
  9933. }
  9934. }
  9935. return Item->Owner->Item[Index];
  9936. }
  9937. //---------------------------------------------------------------------------
  9938. TListItem * __fastcall TCustomScpExplorerForm::SearchFile(const UnicodeString & Text, bool SkipCurrent, bool Reverse)
  9939. {
  9940. TCustomDirView * ADirView = DirView(osCurrent);
  9941. if (ADirView->Items->Count == 0)
  9942. {
  9943. return NULL;
  9944. }
  9945. else
  9946. {
  9947. TListItem * CurrentItem = (ADirView->ItemFocused != NULL) ? ADirView->ItemFocused : ADirView->Items->Item[0];
  9948. TListItem * Item = CurrentItem;
  9949. if (SkipCurrent)
  9950. {
  9951. Item = DebugNotNull(GetNextFile(Item, Reverse));
  9952. }
  9953. while (true)
  9954. {
  9955. bool Matches = false;
  9956. switch (WinConfiguration->PanelSearch)
  9957. {
  9958. case isNameStartOnly:
  9959. Matches = ContainsTextSemiCaseSensitive(Item->Caption.SubString(1, Text.Length()), Text);
  9960. break;
  9961. case isName:
  9962. Matches = ContainsTextSemiCaseSensitive(Item->Caption, Text);
  9963. break;
  9964. case isAll:
  9965. int ColCount = (ADirView->ViewStyle == vsReport) ? ADirView->ColProperties->Count : 1;
  9966. int Index = 0;
  9967. while ((Index < ColCount) && !Matches)
  9968. {
  9969. bool Visible = (ADirView->ViewStyle == vsReport) ? ADirView->ColProperties->Visible[Index] : true;
  9970. if (Visible)
  9971. {
  9972. Matches = ContainsTextSemiCaseSensitive(ADirView->GetColumnText(Item, Index), Text);
  9973. }
  9974. Index++;
  9975. }
  9976. break;
  9977. }
  9978. if (Matches)
  9979. {
  9980. return Item;
  9981. }
  9982. Item = GetNextFile(Item, Reverse);
  9983. if (Item == CurrentItem)
  9984. {
  9985. return NULL;
  9986. }
  9987. }
  9988. }
  9989. }
  9990. //---------------------------------------------------------------------------
  9991. void __fastcall TCustomScpExplorerForm::DirViewExit(TObject *)
  9992. {
  9993. ResetIncrementalSearch();
  9994. }
  9995. //---------------------------------------------------------------------------
  9996. void __fastcall TCustomScpExplorerForm::CMDialogKey(TWMKeyDown & Message)
  9997. {
  9998. if (Message.CharCode == VK_TAB)
  9999. {
  10000. if (!FIncrementalSearch.IsEmpty())
  10001. {
  10002. TShiftState Shift = KeyDataToShiftState(Message.KeyData);
  10003. bool Reverse = Shift.Contains(ssShift);
  10004. IncrementalSearch(FIncrementalSearch, true, Reverse);
  10005. Message.Result = 1;
  10006. return;
  10007. }
  10008. }
  10009. else if (Message.CharCode == VK_ESCAPE)
  10010. {
  10011. if (!FIncrementalSearch.IsEmpty())
  10012. {
  10013. ResetIncrementalSearch();
  10014. Message.Result = 1;
  10015. return;
  10016. }
  10017. }
  10018. TForm::Dispatch(&Message);
  10019. }
  10020. //---------------------------------------------------------------------------
  10021. void __fastcall TCustomScpExplorerForm::Deactivate()
  10022. {
  10023. TForm::Deactivate();
  10024. // E.g. when switching to an internal editor window
  10025. ResetIncrementalSearch();
  10026. }
  10027. //---------------------------------------------------------------------------
  10028. void __fastcall TCustomScpExplorerForm::ApplicationEventsDeactivate(TObject *)
  10029. {
  10030. // When switching to another application
  10031. ResetIncrementalSearch();
  10032. }
  10033. //---------------------------------------------------------------------------
  10034. void __fastcall TCustomScpExplorerForm::ApplicationEventsModalBegin(TObject *)
  10035. {
  10036. ResetIncrementalSearch();
  10037. }
  10038. //---------------------------------------------------------------------------
  10039. void __fastcall TCustomScpExplorerForm::DirViewChangeFocus(TObject *, TListItem *)
  10040. {
  10041. if (FIncrementalSearching <= 0)
  10042. {
  10043. ResetIncrementalSearch();
  10044. }
  10045. }
  10046. //---------------------------------------------------------------------------
  10047. void __fastcall TCustomScpExplorerForm::RemoteStatusBarMouseDown(TObject *, TMouseButton, TShiftState, int, int)
  10048. {
  10049. CountClicksForWindowPrint(this);
  10050. }
  10051. //---------------------------------------------------------------------------
  10052. void __fastcall TCustomScpExplorerForm::RemoteDirViewResize(TObject *)
  10053. {
  10054. CenterReconnectToolbar();
  10055. }
  10056. //---------------------------------------------------------------------------
  10057. void __fastcall TCustomScpExplorerForm::BrowseFile()
  10058. {
  10059. if (RemoteDirView->ItemFocused != NULL)
  10060. {
  10061. RemoteDirView->ItemFocused->Selected = true;
  10062. }
  10063. }
  10064. //---------------------------------------------------------------------------