CustomScpExplorer.cpp 367 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139
  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. #pragma resource "*.dfm"
  51. //---------------------------------------------------------------------------
  52. #define WM_COMPONENT_HIDE (WM_WINSCP_USER + 4)
  53. static const int SessionPanelCount = 4;
  54. //---------------------------------------------------------------------------
  55. class TMutexGuard
  56. {
  57. public:
  58. TMutexGuard(HANDLE AMutex, int Message = MUTEX_RELEASE_TIMEOUT,
  59. int Timeout = 5000)
  60. {
  61. FMutex = NULL;
  62. unsigned long WaitResult = WaitForSingleObject(AMutex, Timeout);
  63. if (WaitResult == WAIT_TIMEOUT)
  64. {
  65. throw Exception(LoadStr(MUTEX_RELEASE_TIMEOUT));
  66. }
  67. else
  68. {
  69. FMutex = AMutex;
  70. }
  71. }
  72. ~TMutexGuard()
  73. {
  74. if (FMutex != NULL)
  75. {
  76. ReleaseMutex(FMutex);
  77. }
  78. }
  79. private:
  80. HANDLE FMutex;
  81. };
  82. //---------------------------------------------------------------------------
  83. class TWindowLock
  84. {
  85. public:
  86. TWindowLock(TCustomScpExplorerForm * Form) :
  87. FForm(Form)
  88. {
  89. FForm->LockWindow();
  90. }
  91. ~TWindowLock()
  92. {
  93. FForm->UnlockWindow();
  94. }
  95. private:
  96. TCustomScpExplorerForm * FForm;
  97. };
  98. //---------------------------------------------------------------------------
  99. class TAutoBatch
  100. {
  101. public:
  102. TAutoBatch(TCustomScpExplorerForm * Form) :
  103. FForm(Form)
  104. {
  105. FForm->BatchStart(FBatchStorage);
  106. }
  107. ~TAutoBatch()
  108. {
  109. FForm->BatchEnd(FBatchStorage);
  110. }
  111. private:
  112. TCustomScpExplorerForm * FForm;
  113. void * FBatchStorage;
  114. };
  115. //---------------------------------------------------------------------------
  116. struct TTransferOperationParam
  117. {
  118. TTransferOperationParam();
  119. UnicodeString TargetDirectory;
  120. bool Temp;
  121. bool DragDrop;
  122. int Options;
  123. TAutoSwitch Queue;
  124. };
  125. //---------------------------------------------------------------------------
  126. TTransferOperationParam::TTransferOperationParam()
  127. {
  128. Temp = false;
  129. DragDrop = false;
  130. Options = 0;
  131. Queue = asAuto;
  132. }
  133. //---------------------------------------------------------------------------
  134. class TTransferPresetNoteData : public TObject
  135. {
  136. public:
  137. UnicodeString Message;
  138. };
  139. //---------------------------------------------------------------------------
  140. class TTerminalNoteData : public TObject
  141. {
  142. public:
  143. TManagedTerminal * Terminal;
  144. };
  145. //---------------------------------------------------------------------------
  146. __fastcall TCustomScpExplorerForm::TCustomScpExplorerForm(TComponent* Owner):
  147. FFormRestored(false),
  148. TForm(Owner)
  149. {
  150. AddStartupSequence(L"F");
  151. FInvalid = false;
  152. FCurrentSide = osRemote;
  153. FEverShown = false;
  154. FDocks = new TList();
  155. InitControls();
  156. DebugAssert(NonVisualDataModule && !NonVisualDataModule->ScpExplorer);
  157. // So that UpdateCustomCommandsToolbar called from RestoreParams works
  158. NonVisualDataModule->ScpExplorer = this;
  159. RestoreParams();
  160. ConfigurationChanged();
  161. RemoteDirView->Invalidate();
  162. FAutoOperation = false;
  163. FOnFileOperationFinished = NULL;
  164. FForceExecution = false;
  165. FIgnoreNextDialogChar = 0;
  166. FErrorList = NULL;
  167. FSynchronizeProgressForm = NULL;
  168. FProgressForm = NULL;
  169. FRefreshLocalDirectory = false;
  170. FRefreshRemoteDirectory = false;
  171. FDDExtMapFile = NULL;
  172. // CreateMutexW keeps failing with ERROR_NOACCESS
  173. FDDExtMutex = CreateMutexA(NULL, false, AnsiString(DRAG_EXT_MUTEX).c_str());
  174. DebugAssert(FDDExtMutex != NULL);
  175. FDDTargetControl = NULL;
  176. FDelayedDeletionTimer = NULL;
  177. FDelayedDeletionList = new TStringList();
  178. FDDFileList = NULL;
  179. FPendingTempSpaceWarn = false;
  180. FCapturedLog = NULL;
  181. FDragDropOperation = false;
  182. memset(&FHistoryMenu, 0, sizeof(FHistoryMenu));
  183. FAllowTransferPresetAutoSelect = true;
  184. FCopyParamDefault = L"";
  185. FSynchronizeController = NULL;
  186. FPendingQueueActionItem = NULL;
  187. FLockLevel = 0;
  188. FLockSuspendLevel = 0;
  189. FDisabledOnLockSuspend = false;
  190. FAlternativeDelete = false;
  191. FTrayIcon = new ::TTrayIcon(0);
  192. FTrayIcon->OnClick = TrayIconClick;
  193. FMaxQueueLength = 0;
  194. FLastContextPopupScreenPoint = TPoint(-1, -1);
  195. FTransferResumeList = NULL;
  196. FMoveToQueue = false;
  197. StandaloneOperation = false;
  198. FOnFeedSynchronizeError = NULL;
  199. FOnSynchronizeAbort = NULL;
  200. FSynchronizeTerminal = NULL;
  201. FNeedSession = false;
  202. FDoNotIdleCurrentTerminal = 0;
  203. FIncrementalSearching = 0;
  204. FIncrementalSearchHaveNext = false;
  205. FQueueFileList.reset(new TQueueFileList());
  206. FDownloadingFromClipboard = false;
  207. FClipboardFakeMonitorsPendingReset = false;
  208. FEditorManager = new TEditorManager();
  209. FEditorManager->OnFileChange = ExecutedFileChanged;
  210. FEditorManager->OnFileReload = ExecutedFileReload;
  211. FEditorManager->OnFileEarlyClosed = ExecutedFileEarlyClosed;
  212. FEditorManager->OnFileUploadComplete = ExecutedFileUploadComplete;
  213. FLocalEditors = new TList();
  214. FQueueStatus = NULL;
  215. FQueueStatusSection = new TCriticalSection();
  216. FQueueStatusInvalidated = false;
  217. FQueueItemInvalidated = false;
  218. FQueueStatusUpdating = false;
  219. FQueueActedItem = NULL;
  220. FQueueController = new TQueueController(QueueView3);
  221. FUserActionTimer = new TTimer(this);
  222. FUserActionTimer->Enabled = false;
  223. FUserActionTimer->Interval = 10;
  224. FUserActionTimer->OnTimer = UserActionTimer;
  225. FNotes = new TStringList();
  226. FNoteTimer = new TTimer(this);
  227. FNoteTimer->Enabled = false;
  228. FNoteTimer->OnTimer = NoteTimer;
  229. FOnNoteClick = NULL;
  230. FOle32Library = LoadLibrary(L"Ole32.dll");
  231. FDragMoveCursor = FOle32Library != NULL ?
  232. LoadCursor(FOle32Library, MAKEINTRESOURCE(2)) : NULL;
  233. UseSystemSettings(this);
  234. TTBXStringList * TransferList = dynamic_cast<TTBXStringList*>(
  235. static_cast<TObject*>(GetComponent(fcTransferList)));
  236. DebugAssert(TransferList != NULL);
  237. FTransferListHoverIndex = -1;
  238. TransferList->OnChange = TransferListChange;
  239. TransferList->OnDrawItem = TransferListDrawItem;
  240. SetSubmenu(dynamic_cast<TTBXCustomItem *>(static_cast<TObject *>(GetComponent(fcColorMenu))));
  241. SetSubmenu(NonVisualDataModule->ColorMenuItem);
  242. UseDesktopFont(SessionsPageControl);
  243. UpdateSessionsPageControlHeight();
  244. UseDesktopFont(RemoteDirView);
  245. UseDesktopFont(RemoteDriveView);
  246. UseDesktopFont(QueueView3);
  247. UseDesktopFont(QueueFileList);
  248. UseDesktopFont(QueueLabel);
  249. UseDesktopFont(RemoteStatusBar);
  250. reinterpret_cast<TLabel*>(QueueSplitter)->OnDblClick = QueueSplitterDblClick;
  251. QueueSplitter->ShowHint = true;
  252. reinterpret_cast<TLabel*>(QueueFileListSplitter)->OnDblClick = QueueFileListSplitterDblClick;
  253. QueueFileListSplitter->ShowHint = true;
  254. RemotePanelSplitter->ShowHint = true;
  255. UpdateImages();
  256. FCustomCommandMenu = CreateTBXPopupMenu(this);
  257. FCustomCommandLocalFileList = NULL;
  258. FCustomCommandRemoteFileList = NULL;
  259. FSessionColors = new TPngImageList(this);
  260. FSessionColors->ColorDepth = cd32Bit;
  261. AddFixedSessionImages();
  262. SessionsPageControl->Images = FSessionColors;
  263. UpdateQueueLabel();
  264. FRemoteDirViewWasFocused = true;
  265. CreateHiddenWindow();
  266. StartUpdates();
  267. }
  268. //---------------------------------------------------------------------------
  269. __fastcall TCustomScpExplorerForm::~TCustomScpExplorerForm()
  270. {
  271. FInvalid = true;
  272. // this has to be one of the very first things to do
  273. StopUpdateThread();
  274. // This is needed when shuting down Windows only. Otherwise it's already set NULL from Execute()
  275. TTerminalManager::Instance()->ScpExplorer = NULL;
  276. delete FCustomCommandLocalFileList;
  277. delete FCustomCommandRemoteFileList;
  278. delete FCustomCommandMenu;
  279. delete FTrayIcon;
  280. FTrayIcon = NULL;
  281. FEditorManager->CloseInternalEditors(ForceCloseInternalEditor);
  282. delete FEditorManager;
  283. ForceCloseLocalEditors();
  284. delete FLocalEditors;
  285. HideFileFindDialog();
  286. if (FDelayedDeletionTimer)
  287. {
  288. DoDelayedDeletion(NULL);
  289. SAFE_DESTROY(FDelayedDeletionTimer);
  290. }
  291. SAFE_DESTROY(FDelayedDeletionList);
  292. // sometimes we do not get DDEnd so the list is not released
  293. SAFE_DESTROY(FDDFileList);
  294. DebugAssert(FSynchronizeController == NULL);
  295. CloseHandle(FDDExtMutex);
  296. FDDExtMutex = NULL;
  297. FreeLibrary(FOle32Library);
  298. FOle32Library = NULL;
  299. FDragMoveCursor = NULL;
  300. DebugAssert(!FErrorList);
  301. if (FEverShown)
  302. {
  303. // when window is never shown (like when running command-line operation),
  304. // particularly window site is not restored correctly (BoundsRect value set
  305. // in RestoreForm gets lost during handle allocation), so we do not want
  306. // it to be stored
  307. StoreParams();
  308. }
  309. Terminal = NULL;
  310. Queue = NULL;
  311. DebugAssert(NonVisualDataModule && (NonVisualDataModule->ScpExplorer == this));
  312. NonVisualDataModule->ScpExplorer = NULL;
  313. delete FQueueController;
  314. FQueueController = NULL;
  315. delete FQueueStatusSection;
  316. FQueueStatusSection = NULL;
  317. delete FQueueStatus;
  318. FQueueStatus = NULL;
  319. delete FUserActionTimer;
  320. FUserActionTimer = NULL;
  321. delete FNoteTimer;
  322. delete FNotes;
  323. delete FNoteData;
  324. SAFE_DESTROY(FDocks);
  325. SessionsPageControl->Images = NULL;
  326. // only after clearing Terminal (after DoTerminalListChanged => ... => AddSessionColor is called)
  327. SAFE_DESTROY(FSessionColors);
  328. SAFE_DESTROY(FSessionsDragDropFilesEx);
  329. SAFE_DESTROY(FHistoryMenu[0][0]);
  330. SAFE_DESTROY(FHistoryMenu[0][1]);
  331. SAFE_DESTROY(FHistoryMenu[1][0]);
  332. SAFE_DESTROY(FHistoryMenu[1][1]);
  333. if (FHiddenWindow != NULL)
  334. {
  335. DestroyWindow(FHiddenWindow);
  336. FHiddenWindow = NULL;
  337. }
  338. }
  339. //---------------------------------------------------------------------------
  340. void __fastcall TCustomScpExplorerForm::RefreshPanel(const UnicodeString & Session, const UnicodeString & Path)
  341. {
  342. std::unique_ptr<TSessionData> Data;
  343. if (!Session.IsEmpty())
  344. {
  345. bool DefaultsOnly;
  346. Data.reset(StoredSessions->ParseUrl(Session, NULL, DefaultsOnly));
  347. }
  348. TTerminalManager * Manager = TTerminalManager::Instance();
  349. for (int Index = 0; Index < Manager->Count; Index++)
  350. {
  351. TTerminal * Terminal = Manager->Terminals[Index];
  352. if (Session.IsEmpty() ||
  353. Manager->IsActiveTerminalForSite(Terminal, Data.get()))
  354. {
  355. if (Path.IsEmpty())
  356. {
  357. Terminal->ClearCaches();
  358. }
  359. else
  360. {
  361. Terminal->DirectoryModified(Path, true);
  362. }
  363. }
  364. }
  365. // We should flag a pending refresh for the background terminals or busy foreground terminals
  366. if (HasActiveTerminal() &&
  367. CanCommandLineFromAnotherInstance() &&
  368. (Session.IsEmpty() || Manager->IsActiveTerminalForSite(Terminal, Data.get())) &&
  369. (Path.IsEmpty() || UnixIsChildPath(Path, Terminal->CurrentDirectory)))
  370. {
  371. Terminal->ReloadDirectory();
  372. }
  373. }
  374. //---------------------------------------------------------------------------
  375. void __fastcall TCustomScpExplorerForm::WMCopyData(TMessage & Message)
  376. {
  377. PCOPYDATASTRUCT CopyData = reinterpret_cast<PCOPYDATASTRUCT>(Message.LParam);
  378. size_t MessageSize = sizeof(TCopyDataMessage);
  379. bool Result = DebugAlwaysTrue(CopyData->cbData == MessageSize);
  380. if (Result)
  381. {
  382. const TCopyDataMessage & Message = *reinterpret_cast<const TCopyDataMessage *>(CopyData->lpData);
  383. Result = (Message.Version == TCopyDataMessage::Version1);
  384. if (Result)
  385. {
  386. switch (Message.Command)
  387. {
  388. case TCopyDataMessage::CommandCanCommandLine:
  389. Result = CanCommandLineFromAnotherInstance();
  390. break;
  391. case TCopyDataMessage::CommandCommandLine:
  392. {
  393. UnicodeString CommandLine(Message.CommandLine);
  394. Result = CommandLineFromAnotherInstance(CommandLine);
  395. }
  396. break;
  397. case TCopyDataMessage::RefreshPanel:
  398. RefreshPanel(Message.Refresh.Session, Message.Refresh.Path);
  399. break;
  400. case TCopyDataMessage::MainWindowCheck:
  401. Result = true;
  402. break;
  403. default:
  404. Result = false;
  405. break;
  406. }
  407. }
  408. }
  409. Message.Result = Result ? 1 : 0;
  410. }
  411. //---------------------------------------------------------------------------
  412. void __fastcall TCustomScpExplorerForm::CreateHiddenWindow()
  413. {
  414. WNDCLASS WindowClass = {0};
  415. WindowClass.lpfnWndProc = DefWindowProc;
  416. WindowClass.hInstance = HInstance;
  417. WindowClass.lpszClassName = HIDDEN_WINDOW_NAME;
  418. FHiddenWindow = NULL;
  419. if (RegisterClass(&WindowClass))
  420. {
  421. FHiddenWindow = CreateWindow(HIDDEN_WINDOW_NAME, L"",
  422. WS_POPUP, 0, 0, 0, 0, 0, 0, HInstance, NULL);
  423. }
  424. }
  425. //---------------------------------------------------------------------------
  426. bool __fastcall TCustomScpExplorerForm::CanConsole()
  427. {
  428. return
  429. HasActiveTerminal() &&
  430. (Terminal->IsCapable[fcAnyCommand] || Terminal->IsCapable[fcSecondaryShell]);
  431. }
  432. //---------------------------------------------------------------------------
  433. bool __fastcall TCustomScpExplorerForm::CanCommandLineFromAnotherInstance()
  434. {
  435. bool Result = Visible && !NonVisualDataModule->Busy;
  436. return Result;
  437. }
  438. //---------------------------------------------------------------------------
  439. bool __fastcall TCustomScpExplorerForm::CommandLineFromAnotherInstance(
  440. const UnicodeString & CommandLine)
  441. {
  442. TProgramParams Params(CommandLine);
  443. bool Result = CanCommandLineFromAnotherInstance() && DebugAlwaysTrue(Params.ParamCount > 0);
  444. if (Result)
  445. {
  446. NonVisualDataModule->StartBusy();
  447. try
  448. {
  449. // this action is initiated from another process,
  450. // so it's likely that our window is not visible,
  451. // and user won't see what is going on
  452. Application->BringToFront();
  453. // reload sessions as we may be asked to open a session
  454. // just stored by another instance
  455. StoredSessions->Reload();
  456. UnicodeString SessionName = Params.Param[1];
  457. std::unique_ptr<TObjectList> DataList(new TObjectList());
  458. try
  459. {
  460. UnicodeString DownloadFile; // unused
  461. int Flags = GetCommandLineParseUrlFlags(&Params);
  462. GetLoginData(SessionName, &Params, DataList.get(), DownloadFile, true, this, Flags);
  463. }
  464. catch (EAbort &)
  465. {
  466. // Happens when opening session in PuTTY and in other situations,
  467. // in which we do not want to fail WM_COPYDATA, as that would cause master instance
  468. // to process the command-line on its own (or to pass it to yet another instance)
  469. }
  470. if (DataList->Count > 0)
  471. {
  472. TTerminalManager * Manager = TTerminalManager::Instance();
  473. Manager->ActiveTerminal = Manager->NewTerminals(DataList.get());
  474. }
  475. }
  476. __finally
  477. {
  478. NonVisualDataModule->EndBusy();
  479. }
  480. }
  481. return Result;
  482. }
  483. //---------------------------------------------------------------------------
  484. void __fastcall TCustomScpExplorerForm::SetTerminal(TManagedTerminal * value)
  485. {
  486. if (FTerminal != value)
  487. {
  488. TerminalChanging();
  489. DoSetTerminal(value, false);
  490. }
  491. }
  492. //---------------------------------------------------------------------------
  493. void __fastcall TCustomScpExplorerForm::DoSetTerminal(TManagedTerminal * value, bool Replace)
  494. {
  495. FTerminal = value;
  496. bool PrevAllowTransferPresetAutoSelect = FAllowTransferPresetAutoSelect;
  497. FAllowTransferPresetAutoSelect = false;
  498. try
  499. {
  500. TerminalChanged(Replace);
  501. }
  502. __finally
  503. {
  504. FAllowTransferPresetAutoSelect = PrevAllowTransferPresetAutoSelect;
  505. }
  506. if (Terminal != NULL)
  507. {
  508. TransferPresetAutoSelect();
  509. }
  510. }
  511. //---------------------------------------------------------------------------
  512. void __fastcall TCustomScpExplorerForm::ReplaceTerminal(TManagedTerminal * value)
  513. {
  514. DoSetTerminal(value, true);
  515. }
  516. //---------------------------------------------------------------------------
  517. void __fastcall TCustomScpExplorerForm::TerminalChanging()
  518. {
  519. if (FTerminal != NULL)
  520. {
  521. UpdateTerminal(Terminal);
  522. }
  523. }
  524. //---------------------------------------------------------------------------
  525. void __fastcall TCustomScpExplorerForm::TerminalChanged(bool Replaced)
  526. {
  527. if (Terminal != NULL)
  528. {
  529. UpdateSessionColor((TColor)Terminal->StateData->Color);
  530. }
  531. DoTerminalListChanged();
  532. DebugAssert(!IsLocalBrowserMode());
  533. if (Replaced)
  534. {
  535. RemoteDirView->ReplaceTerminal(Terminal);
  536. }
  537. else
  538. {
  539. RemoteDirView->Terminal = Terminal;
  540. }
  541. NonVisualDataModule->ResetQueueOnceEmptyOperation();
  542. if (Terminal != NULL)
  543. {
  544. if (Terminal->Active)
  545. {
  546. Terminal->RefreshDirectory();
  547. }
  548. if (WinConfiguration->PreservePanelState)
  549. {
  550. if (Terminal->RemoteExplorerState != NULL)
  551. {
  552. DirView(osRemote)->RestoreState(Terminal->RemoteExplorerState);
  553. }
  554. else
  555. {
  556. DirView(osRemote)->ClearState();
  557. }
  558. }
  559. if (!Replaced && Terminal->Active)
  560. {
  561. InitStatusBar();
  562. }
  563. }
  564. UpdateTransferList();
  565. // Update panels Enable state before refreshing the labels
  566. UpdateControls();
  567. }
  568. //---------------------------------------------------------------------------
  569. void __fastcall TCustomScpExplorerForm::SetQueue(TTerminalQueue * value)
  570. {
  571. if (Queue != value)
  572. {
  573. if (FQueue != NULL)
  574. {
  575. FQueue->OnListUpdate = NULL;
  576. FQueue->OnQueueItemUpdate = NULL;
  577. }
  578. FQueue = value;
  579. if (FQueue != NULL)
  580. {
  581. DebugAssert(FQueue->OnListUpdate == NULL);
  582. FQueue->OnListUpdate = QueueListUpdate;
  583. DebugAssert(FQueue->OnQueueItemUpdate == NULL);
  584. FQueue->OnQueueItemUpdate = QueueItemUpdate;
  585. }
  586. QueueChanged();
  587. }
  588. }
  589. //---------------------------------------------------------------------------
  590. void __fastcall TCustomScpExplorerForm::QueueView3Deletion(TObject * /*Sender*/,
  591. TListItem * Item)
  592. {
  593. if (FQueueActedItem == Item)
  594. {
  595. FQueueActedItem = NULL;
  596. if ((QueueView3->PopupMenu != NULL) &&
  597. (QueueView3->PopupMenu->PopupComponent == QueueView3))
  598. {
  599. // rather "trick", suggested by Jordan on jrsoftware.toolbar2000
  600. ReleaseCapture();
  601. }
  602. }
  603. if (Item->Data == FPendingQueueActionItem)
  604. {
  605. FPendingQueueActionItem = NULL;
  606. }
  607. }
  608. //---------------------------------------------------------------------------
  609. bool TCustomScpExplorerForm::IsAnythingQueued()
  610. {
  611. return (FQueueStatus != NULL) && (FQueueStatus->ActiveAndPendingPrimaryCount > 0);
  612. }
  613. //---------------------------------------------------------------------------
  614. void __fastcall TCustomScpExplorerForm::UpdateQueueStatus(bool QueueChanging)
  615. {
  616. {
  617. TGuard Guard(FQueueStatusSection);
  618. FQueueStatusInvalidated = false;
  619. if (FQueue != NULL)
  620. {
  621. FQueueStatus = FQueue->CreateStatus(FQueueStatus);
  622. }
  623. }
  624. if ((FQueueStatus != NULL) && (FQueueStatus->Count > FMaxQueueLength))
  625. {
  626. FMaxQueueLength = FQueueStatus->Count;
  627. Configuration->Usage->SetMax(L"MaxQueueLength", FMaxQueueLength);
  628. }
  629. {
  630. TAutoFlag Flag(FQueueStatusUpdating);
  631. FQueueController->UpdateQueueStatus(FQueueStatus);
  632. }
  633. UpdateQueueFileList();
  634. SetQueueProgress();
  635. UpdateQueueView();
  636. bool IsEmpty = !IsAnythingQueued();
  637. if (IsEmpty && (Terminal != NULL))
  638. {
  639. TOnceDoneOperation OnceDoneOperation =
  640. NonVisualDataModule->CurrentQueueOnceEmptyOperation();
  641. NonVisualDataModule->ResetQueueOnceEmptyOperation();
  642. if ((FQueue != NULL) && !WinConfiguration->EnableQueueByDefault && !QueueChanging)
  643. {
  644. FQueue->Enabled = false;
  645. }
  646. if ((OnceDoneOperation != odoIdle) && !NonVisualDataModule->Busy)
  647. {
  648. Terminal->CloseOnCompletion(OnceDoneOperation, LoadStr(CLOSED_ON_QUEUE_EMPTY));
  649. }
  650. }
  651. }
  652. //---------------------------------------------------------------------------
  653. UnicodeString __fastcall TCustomScpExplorerForm::GetQueueProgressTitle()
  654. {
  655. UnicodeString Result;
  656. if (FQueueStatus != NULL)
  657. {
  658. if (FQueueStatus->IsOnlyOneActiveAndNoPending())
  659. {
  660. TFileOperationProgressType * ProgressData =
  661. FQueueStatus->Items[FQueueStatus->DoneCount]->ProgressData;
  662. if ((ProgressData != NULL) && ProgressData->InProgress)
  663. {
  664. Result = TProgressForm::ProgressStr(NULL, ProgressData);
  665. }
  666. }
  667. else if (FQueueStatus->ActiveAndPendingPrimaryCount > 1)
  668. {
  669. Result = FMTLOAD(PROGRESS_IN_QUEUE, (FQueueStatus->ActiveAndPendingPrimaryCount));
  670. }
  671. }
  672. return Result;
  673. }
  674. //---------------------------------------------------------------------------
  675. void __fastcall TCustomScpExplorerForm::UpdateQueueView()
  676. {
  677. ComponentVisible[fcQueueView] =
  678. (WinConfiguration->QueueView.Show == qvShow) ||
  679. ((WinConfiguration->QueueView.Show == qvHideWhenEmpty) &&
  680. (FQueueStatus != NULL) && (FQueueStatus->Count > 0));
  681. }
  682. //---------------------------------------------------------------------------
  683. void __fastcall TCustomScpExplorerForm::QueueChanged()
  684. {
  685. if (FQueueStatus != NULL)
  686. {
  687. // UpdateFileList implementation relies on the status being recreated when session changes
  688. delete FQueueStatus;
  689. FQueueStatus = NULL;
  690. }
  691. UpdateQueueStatus(true);
  692. }
  693. //---------------------------------------------------------------------------
  694. void __fastcall TCustomScpExplorerForm::QueueListUpdate(TTerminalQueue * Queue)
  695. {
  696. if (FQueue == Queue)
  697. {
  698. FQueueStatusInvalidated = true;
  699. }
  700. }
  701. //---------------------------------------------------------------------------
  702. void __fastcall TCustomScpExplorerForm::QueueItemUpdate(TTerminalQueue * Queue,
  703. TQueueItem * Item)
  704. {
  705. if (FQueue == Queue)
  706. {
  707. TGuard Guard(FQueueStatusSection);
  708. // this may be running in parallel with QueueChanged
  709. if (FQueueStatus != NULL)
  710. {
  711. TQueueItemProxy * QueueItem = FQueueStatus->FindByQueueItem(Item);
  712. if ((Item->Status == TQueueItem::qsDone) && (Terminal != NULL))
  713. {
  714. FRefreshLocalDirectory = (QueueItem == NULL) ||
  715. (!QueueItem->Info->ModifiedLocal.IsEmpty());
  716. FRefreshRemoteDirectory = (QueueItem == NULL) ||
  717. (!QueueItem->Info->ModifiedRemote.IsEmpty());
  718. }
  719. if (QueueItem != NULL)
  720. {
  721. QueueItem->UserData = (void*)true;
  722. FQueueItemInvalidated = true;
  723. }
  724. }
  725. }
  726. }
  727. //---------------------------------------------------------------------------
  728. bool __fastcall TCustomScpExplorerForm::IsQueueAutoPopup()
  729. {
  730. // during standalone editing, we have no way to see/control queue,
  731. // so we have to always popup prompts automatically
  732. return StandaloneOperation || GUIConfiguration->QueueAutoPopup;
  733. }
  734. //---------------------------------------------------------------------------
  735. void __fastcall TCustomScpExplorerForm::RefreshQueueItems()
  736. {
  737. if (FQueueStatus != NULL)
  738. {
  739. bool QueueAutoPopup = IsQueueAutoPopup();
  740. bool NeedRefresh = FQueueController->NeedRefresh();
  741. bool Refresh = FQueueItemInvalidated || NeedRefresh;
  742. FQueueItemInvalidated = false;
  743. int Limit = Refresh ? FQueueStatus->Count : FQueueStatus->DoneAndActiveCount;
  744. bool Updated = false;
  745. TQueueItemProxy * QueueItem;
  746. bool WasUserAction;
  747. for (int Index = 0; Index < Limit; Index++)
  748. {
  749. bool Update = false;
  750. QueueItem = FQueueStatus->Items[Index];
  751. WasUserAction = TQueueItem::IsUserActionStatus(QueueItem->Status);
  752. if (!NonVisualDataModule->Busy && QueueAutoPopup && WasUserAction &&
  753. (FPendingQueueActionItem == NULL))
  754. {
  755. FPendingQueueActionItem = QueueItem;
  756. FUserActionTimer->Enabled = true;
  757. }
  758. if ((bool)QueueItem->UserData)
  759. {
  760. QueueItem->UserData = (void*)false;
  761. QueueItem->Update();
  762. Updated = true;
  763. Update = true;
  764. }
  765. else if (FQueueController->QueueItemNeedsFrequentRefresh(QueueItem) || NeedRefresh)
  766. {
  767. Update = true;
  768. }
  769. if (Update)
  770. {
  771. FQueueController->RefreshQueueItem(QueueItem);
  772. }
  773. }
  774. if (Updated)
  775. {
  776. NonVisualDataModule->UpdateNonVisibleActions();
  777. SetQueueProgress();
  778. UpdateQueueFileList();
  779. }
  780. }
  781. }
  782. //---------------------------------------------------------------------------
  783. void __fastcall TCustomScpExplorerForm::SetTaskbarListProgressState(TBPFLAG Flags)
  784. {
  785. if (FTaskbarList != NULL)
  786. {
  787. // Could now use Application->MainFormHandle, now that we implement Application->OnGetMainFormHandle
  788. FTaskbarList->SetProgressState(GetMainForm()->Handle, Flags);
  789. }
  790. }
  791. //---------------------------------------------------------------------------
  792. void __fastcall TCustomScpExplorerForm::SetTaskbarListProgressValue(int Progress)
  793. {
  794. if (Progress >= 0)
  795. {
  796. if (FTaskbarList != NULL)
  797. {
  798. FTaskbarList->SetProgressValue(GetMainForm()->Handle, Progress, 100);
  799. }
  800. }
  801. else
  802. {
  803. SetTaskbarListProgressState(TBPF_INDETERMINATE);
  804. }
  805. }
  806. //---------------------------------------------------------------------------
  807. void __fastcall TCustomScpExplorerForm::SetTaskbarListProgressValue(TFileOperationProgressType * ProgressData)
  808. {
  809. int OverallProgress;
  810. // FProgressForm is null when this is called from SetQueueProgress
  811. if ((FProgressForm != NULL) && (FProgressForm->SynchronizeProgress != NULL))
  812. {
  813. OverallProgress = FProgressForm->SynchronizeProgress->Progress(ProgressData);
  814. }
  815. else if (!ProgressData->IsIndeterminate())
  816. {
  817. OverallProgress = ProgressData->OverallProgress();
  818. }
  819. else
  820. {
  821. OverallProgress = -1;
  822. }
  823. SetTaskbarListProgressValue(OverallProgress);
  824. }
  825. //---------------------------------------------------------------------------
  826. void __fastcall TCustomScpExplorerForm::SetQueueProgress()
  827. {
  828. TTerminalManager::Instance()->QueueStatusUpdated();
  829. if ((FTaskbarList != NULL) && (FProgressForm == NULL))
  830. {
  831. if ((FQueueStatus != NULL) && (FQueueStatus->ActiveAndPendingPrimaryCount > 0))
  832. {
  833. if (FQueueStatus->IsOnlyOneActiveAndNoPending())
  834. {
  835. TFileOperationProgressType * ProgressData = NULL;
  836. if (FQueueStatus->Items[FQueueStatus->DoneCount] != NULL)
  837. {
  838. ProgressData = FQueueStatus->Items[FQueueStatus->DoneCount]->ProgressData;
  839. }
  840. if ((ProgressData != NULL) &&
  841. ProgressData->InProgress)
  842. {
  843. SetTaskbarListProgressValue(ProgressData);
  844. }
  845. else
  846. {
  847. SetTaskbarListProgressState(TBPF_NOPROGRESS);
  848. }
  849. }
  850. else
  851. {
  852. SetTaskbarListProgressState(TBPF_INDETERMINATE);
  853. }
  854. }
  855. else
  856. {
  857. SetTaskbarListProgressState(TBPF_NOPROGRESS);
  858. }
  859. }
  860. UpdateQueueLabel();
  861. }
  862. //---------------------------------------------------------------------------
  863. void __fastcall TCustomScpExplorerForm::UpdateQueueLabel()
  864. {
  865. UnicodeString Caption = LoadStr(QUEUE_CAPTION);
  866. if (FQueueStatus != NULL)
  867. {
  868. int ActiveAndPendingPrimaryCount = FQueueStatus->ActiveAndPendingPrimaryCount;
  869. if (ActiveAndPendingPrimaryCount > 0)
  870. {
  871. Caption = FORMAT("%s (%d)", (Caption, ActiveAndPendingPrimaryCount));
  872. }
  873. }
  874. QueueLabel->Caption = Caption;
  875. }
  876. //---------------------------------------------------------------------------
  877. void __fastcall TCustomScpExplorerForm::UpdateTransferList()
  878. {
  879. TTBXStringList * TransferList = dynamic_cast<TTBXStringList*>(
  880. static_cast<TComponent*>(GetComponent(fcTransferList)));
  881. TTBXDropDownItem * TransferDropDown = dynamic_cast<TTBXDropDownItem*>(
  882. static_cast<TComponent*>(GetComponent(fcTransferDropDown)));
  883. TransferList->Strings->BeginUpdate();
  884. try
  885. {
  886. TransferList->Strings->Assign(GUIConfiguration->CopyParamList->NameList);
  887. TransferList->Strings->Insert(0, StripHotkey(LoadStr(COPY_PARAM_DEFAULT)));
  888. TransferList->ItemIndex = GUIConfiguration->CopyParamIndex + 1;
  889. if (FTransferDropDownHint.IsEmpty())
  890. {
  891. FTransferDropDownHint = TransferDropDown->Hint;
  892. }
  893. // this way we get name for "default" settings (COPY_PARAM_DEFAULT)
  894. UnicodeString Name = TransferList->Strings->Strings[TransferList->ItemIndex];
  895. TransferDropDown->Text = StripHotkey(Name);
  896. TransferDropDown->Hint = FORMAT(L"%s|%s:\n%s",
  897. (FTransferDropDownHint, StripHotkey(Name),
  898. GUIConfiguration->CurrentCopyParam.GetInfoStr(L"; ",
  899. FLAGMASK(HasActiveTerminal(), Terminal->UsableCopyParamAttrs(0).General))));
  900. // update the label, otherwise when it is updated only on the first draw
  901. // of the list, it is drawn "bold" for some reason
  902. FTransferListHoverIndex = TransferList->ItemIndex;
  903. UpdateTransferLabel();
  904. }
  905. __finally
  906. {
  907. TransferList->Strings->EndUpdate();
  908. }
  909. }
  910. //---------------------------------------------------------------------------
  911. void __fastcall TCustomScpExplorerForm::UpdateCustomCommandsToolbar()
  912. {
  913. TTBXToolbar * Toolbar = dynamic_cast<TTBXToolbar *>(GetComponent(fcCustomCommandsBand));
  914. DebugAssert(Toolbar != NULL);
  915. NonVisualDataModule->UpdateCustomCommandsToolbar(Toolbar);
  916. }
  917. //---------------------------------------------------------------------------
  918. void __fastcall TCustomScpExplorerForm::UpdateActions()
  919. {
  920. TForm::UpdateActions();
  921. if (ComponentVisible[fcCustomCommandsBand])
  922. {
  923. UpdateCustomCommandsToolbar();
  924. }
  925. }
  926. //---------------------------------------------------------------------------
  927. void __fastcall TCustomScpExplorerForm::UpdateSessionsPageControlHeight()
  928. {
  929. SessionsPageControl->Height = SessionsPageControl->GetTabsHeight();
  930. }
  931. //---------------------------------------------------------------------------
  932. void __fastcall TCustomScpExplorerForm::ConfigurationChanged()
  933. {
  934. Color = GetBtnFaceColor();
  935. DebugAssert(Configuration && RemoteDirView);
  936. RemoteDirView->DDAllowMove = !WinConfiguration->DDDisableMove;
  937. RemoteDirView->DimmHiddenFiles = WinConfiguration->DimmHiddenFiles;
  938. RemoteDirView->ShowHiddenFiles = WinConfiguration->ShowHiddenFiles;
  939. RemoteDirView->FormatSizeBytes = WinConfiguration->FormatSizeBytes;
  940. RemoteDirView->ShowInaccesibleDirectories = WinConfiguration->ShowInaccesibleDirectories;
  941. RemoteDirView->NaturalOrderNumericalSorting = WinConfiguration->NaturalOrderNumericalSorting;
  942. if (RemoteDirView->RowSelect != WinConfiguration->FullRowSelect)
  943. {
  944. RemoteDirView->RowSelect = WinConfiguration->FullRowSelect;
  945. // Without this, the panel turns gray and unused part of header turns black
  946. RemoteDirView->Perform(CM_COLORCHANGED, 0, 0);
  947. }
  948. RemoteDriveView->DDAllowMove = !WinConfiguration->DDDisableMove;
  949. RemoteDriveView->DimmHiddenDirs = WinConfiguration->DimmHiddenFiles;
  950. RemoteDriveView->ShowHiddenDirs = WinConfiguration->ShowHiddenFiles;
  951. RemoteDriveView->ShowInaccesibleDirectories = WinConfiguration->ShowInaccesibleDirectories;
  952. RemoteDriveView->NaturalOrderNumericalSorting = WinConfiguration->NaturalOrderNumericalSorting;
  953. UpdateSessionsPageControlHeight();
  954. SetDockAllowDrag(!WinConfiguration->LockToolbars);
  955. UpdateToolbarDisplayMode();
  956. UpdateTransferList();
  957. if (Terminal != NULL)
  958. {
  959. TransferPresetAutoSelect();
  960. }
  961. UpdateQueueView();
  962. UpdateControls();
  963. // this can be called even before constuctor finishes.
  964. if (FEditorManager != NULL)
  965. {
  966. FEditorManager->ProcessFiles(FileConfigurationChanged, NULL);
  967. }
  968. // this can be called even before constuctor finishes.
  969. if (FLocalEditors != NULL)
  970. {
  971. for (int Index = 0; Index < FLocalEditors->Count; Index++)
  972. {
  973. ReconfigureEditorForm(static_cast<TForm *>(FLocalEditors->Items[Index]));
  974. }
  975. }
  976. if (ComponentVisible[fcCustomCommandsBand])
  977. {
  978. UpdateCustomCommandsToolbar();
  979. }
  980. // show only when keeping queue items forever,
  981. // otherwise, it's enough to have it in the context menu
  982. QueueDeleteAllDoneQueueToolbarItem->Visible =
  983. WinConfiguration->QueueKeepDoneItems && (WinConfiguration->QueueKeepDoneItemsFor < 0);
  984. if (FFileColorsCurrent != WinConfiguration->FileColors)
  985. {
  986. TFileColorData::LoadList(WinConfiguration->FileColors, FFileColors);
  987. FFileColorsCurrent = WinConfiguration->FileColors;
  988. FileColorsChanged();
  989. }
  990. }
  991. //---------------------------------------------------------------------------
  992. void __fastcall TCustomScpExplorerForm::DoFileColorsChanged(TCustomDirView * DirView)
  993. {
  994. DirView->OnGetItemColor = FFileColors.empty() ? TDirViewGetItemColorEvent(NULL) : TDirViewGetItemColorEvent(&DirViewGetItemColor);
  995. DirView->Invalidate();
  996. }
  997. //---------------------------------------------------------------------------
  998. void __fastcall TCustomScpExplorerForm::FileColorsChanged()
  999. {
  1000. DoFileColorsChanged(RemoteDirView);
  1001. }
  1002. //---------------------------------------------------------------------------
  1003. void __fastcall TCustomScpExplorerForm::FileConfigurationChanged(
  1004. const UnicodeString FileName, TEditedFileData * /*Data*/, TObject * Token,
  1005. void * /*Arg*/)
  1006. {
  1007. if (Token != NULL)
  1008. {
  1009. TForm * Editor = dynamic_cast<TForm*>(Token);
  1010. DebugAssert(Editor != NULL);
  1011. ReconfigureEditorForm(Editor);
  1012. }
  1013. }
  1014. //---------------------------------------------------------------------------
  1015. void __fastcall TCustomScpExplorerForm::EnableDDTransferConfirmation(TObject * /*Sender*/)
  1016. {
  1017. WinConfiguration->DDTransferConfirmation = asOn;
  1018. }
  1019. //---------------------------------------------------------------------------
  1020. void __fastcall TCustomScpExplorerForm::CopyParamDialogAfter(
  1021. TTransferDirection /*Direction*/, bool /*Temp*/, const UnicodeString & /*TargetDirectory*/)
  1022. {
  1023. // noop
  1024. }
  1025. //---------------------------------------------------------------------------
  1026. bool __fastcall TCustomScpExplorerForm::CopyParamDialog(
  1027. TTransferDirection Direction, TTransferType Type, bool Temp,
  1028. TStrings * FileList, UnicodeString & TargetDirectory, TGUICopyParamType & CopyParam,
  1029. bool Confirm, bool DragDrop, int Options)
  1030. {
  1031. bool Result = true;
  1032. DebugAssert(HasActiveTerminal());
  1033. // these parameters are known in advance
  1034. int Params =
  1035. FLAGMASK(Type == ttMove, cpDelete);
  1036. bool ToTemp = (Temp && (Direction == tdToLocal));
  1037. if (Result && Confirm && WinConfiguration->ConfirmTransferring)
  1038. {
  1039. bool DisableNewerOnly =
  1040. (!Terminal->IsCapable[fcNewerOnlyUpload] && (Direction == tdToRemote)) ||
  1041. ToTemp;
  1042. Options |=
  1043. FLAGMASK(ToTemp, coTemp) |
  1044. FLAGMASK(!Terminal->IsCapable[fcBackgroundTransfers], coDisableQueue) |
  1045. coDoNotShowAgain;
  1046. TUsableCopyParamAttrs UsableCopyParamAttrs = Terminal->UsableCopyParamAttrs(Params);
  1047. int CopyParamAttrs = (Direction == tdToRemote ?
  1048. UsableCopyParamAttrs.Upload : UsableCopyParamAttrs.Download) |
  1049. FLAGMASK(DisableNewerOnly, cpaNoNewerOnly);
  1050. int OutputOptions =
  1051. FLAGMASK(DragDrop && (WinConfiguration->DDTransferConfirmation == asAuto),
  1052. cooDoNotShowAgain);
  1053. std::unique_ptr<TSessionData> SessionData(SessionDataForCode());
  1054. FlashOnBackground(); // Particularly when called from ClipboardFakeCreated
  1055. Result = DoCopyDialog(Direction == tdToRemote, Type == ttMove,
  1056. FileList, TargetDirectory, &CopyParam, Options, CopyParamAttrs, SessionData.get(), &OutputOptions, -1);
  1057. if (Result)
  1058. {
  1059. if (FLAGSET(OutputOptions, cooDoNotShowAgain))
  1060. {
  1061. if (DragDrop)
  1062. {
  1063. if (WinConfiguration->DDTransferConfirmation == asAuto)
  1064. {
  1065. PopupTrayBalloon(NULL, LoadStr(DD_TRANSFER_CONFIRM_OFF2), qtInformation,
  1066. NULL, 0, EnableDDTransferConfirmation, NULL);
  1067. }
  1068. WinConfiguration->DDTransferConfirmation = asOff;
  1069. }
  1070. else
  1071. {
  1072. WinConfiguration->ConfirmTransferring = false;
  1073. }
  1074. }
  1075. else
  1076. {
  1077. // User explicitly unchecked "do not show again",
  1078. // so show him the dialog the next time
  1079. if (DragDrop && (WinConfiguration->DDTransferConfirmation == asAuto))
  1080. {
  1081. WinConfiguration->DDTransferConfirmation = asOn;
  1082. }
  1083. }
  1084. CopyParamDialogAfter(Direction, Temp, TargetDirectory);
  1085. }
  1086. }
  1087. if (Result && CopyParam.Queue && !ToTemp && Terminal->IsCapable[fcBackgroundTransfers])
  1088. {
  1089. Configuration->Usage->Inc(L"TransfersOnBackground");
  1090. // these parameter are known only after transfer dialog
  1091. Params |=
  1092. (CopyParam.QueueNoConfirmation ? cpNoConfirmation : 0);
  1093. AddQueueItem(Queue, Direction, FileList, TargetDirectory, CopyParam, Params);
  1094. Result = false;
  1095. ClearTransferSourceSelection(Direction);
  1096. }
  1097. return Result;
  1098. }
  1099. //---------------------------------------------------------------------------
  1100. void __fastcall TCustomScpExplorerForm::ClearTransferSourceSelection(TTransferDirection Direction)
  1101. {
  1102. if (FOnFileOperationFinished != NULL)
  1103. {
  1104. FOnFileOperationFinished(UnicodeString(), true);
  1105. }
  1106. else
  1107. {
  1108. TOperationSide Side = ((Direction == tdToRemote) ? osLocal : osRemote);
  1109. if (HasDirView[Side])
  1110. {
  1111. DirView(Side)->SelectAll(smNone);
  1112. }
  1113. }
  1114. }
  1115. //---------------------------------------------------------------------------
  1116. void __fastcall TCustomScpExplorerForm::AddQueueItem(
  1117. TTerminalQueue * Queue, TTransferDirection Direction, TStrings * FileList,
  1118. const UnicodeString TargetDirectory, TGUICopyParamType & CopyParam,
  1119. int Params)
  1120. {
  1121. DebugAssert(Queue != NULL);
  1122. bool SingleFile = false;
  1123. if (FileList->Count == 1)
  1124. {
  1125. if (Direction == tdToRemote)
  1126. {
  1127. UnicodeString FileName = FileList->Strings[0];
  1128. SingleFile = FileExists(ApiPath(FileName));
  1129. }
  1130. else
  1131. {
  1132. TRemoteFile * File = static_cast<TRemoteFile *>(FileList->Objects[0]);
  1133. SingleFile = !File->IsDirectory;
  1134. }
  1135. }
  1136. TQueueItem * QueueItem;
  1137. if (Direction == tdToRemote)
  1138. {
  1139. CopyParam.IncludeFileMask.SetRoots(FileList, TargetDirectory);
  1140. QueueItem = new TUploadQueueItem(Terminal, FileList, TargetDirectory,
  1141. &CopyParam, Params, SingleFile, CopyParam.QueueParallel);
  1142. }
  1143. else
  1144. {
  1145. CopyParam.IncludeFileMask.SetRoots(TargetDirectory, FileList);
  1146. QueueItem = new TDownloadQueueItem(Terminal, FileList, TargetDirectory,
  1147. &CopyParam, Params, SingleFile, CopyParam.QueueParallel);
  1148. }
  1149. AddQueueItem(Queue, QueueItem, Terminal);
  1150. }
  1151. //---------------------------------------------------------------------------
  1152. void __fastcall TCustomScpExplorerForm::AddQueueItem(TTerminalQueue * Queue, TQueueItem * QueueItem, TManagedTerminal * Terminal)
  1153. {
  1154. if (Queue->IsEmpty)
  1155. {
  1156. Terminal->QueueOperationStart = Now();
  1157. }
  1158. Queue->AddItem(QueueItem);
  1159. }
  1160. //---------------------------------------------------------------------------
  1161. void __fastcall TCustomScpExplorerForm::RestoreFormParams()
  1162. {
  1163. }
  1164. //---------------------------------------------------------------------------
  1165. void __fastcall TCustomScpExplorerForm::InitControls()
  1166. {
  1167. DebugAssert(FDocks != NULL);
  1168. for (int Index = 0; Index < ComponentCount; Index++)
  1169. {
  1170. TTBDock * Dock = dynamic_cast<TTBDock *>(Components[Index]);
  1171. if ((Dock != NULL) && (Dock->Tag == 0))
  1172. {
  1173. FDocks->Add(Dock);
  1174. }
  1175. }
  1176. CollectItemsWithTextDisplayMode(this);
  1177. }
  1178. //---------------------------------------------------------------------------
  1179. void __fastcall TCustomScpExplorerForm::RestoreParams()
  1180. {
  1181. ConfigurationChanged();
  1182. QueuePanel->Height =
  1183. LoadDimension(WinConfiguration->QueueView.Height, WinConfiguration->QueueView.HeightPixelsPerInch, this);
  1184. LoadListViewStr(QueueView3, WinConfiguration->QueueView.Layout);
  1185. QueueDock->Visible = WinConfiguration->QueueView.ToolBar;
  1186. QueueLabel->Visible = WinConfiguration->QueueView.Label;
  1187. QueueFileList->Visible = WinConfiguration->QueueView.FileList;
  1188. QueueFileList->Height =
  1189. LoadDimension(WinConfiguration->QueueView.FileListHeight, WinConfiguration->QueueView.FileListHeightPixelsPerInch, this);
  1190. if (QueueFileList->Visible)
  1191. {
  1192. AdjustQueueLayout();
  1193. }
  1194. UpdateCustomCommandsToolbar();
  1195. }
  1196. //---------------------------------------------------------------------------
  1197. void __fastcall TCustomScpExplorerForm::StoreParams()
  1198. {
  1199. int APixelsPerInch = GetControlPixelsPerInch(this);
  1200. WinConfiguration->QueueView.Height = QueuePanel->Height;
  1201. WinConfiguration->QueueView.HeightPixelsPerInch = APixelsPerInch;
  1202. WinConfiguration->QueueView.Layout = GetListViewStr(QueueView3);
  1203. WinConfiguration->QueueView.ToolBar = QueueDock->Visible;
  1204. WinConfiguration->QueueView.Label = QueueLabel->Visible;
  1205. WinConfiguration->QueueView.FileList = QueueFileList->Visible;
  1206. WinConfiguration->QueueView.FileListHeight = QueueFileList->Height;
  1207. WinConfiguration->QueueView.FileListHeightPixelsPerInch = APixelsPerInch;
  1208. }
  1209. //---------------------------------------------------------------------------
  1210. void __fastcall TCustomScpExplorerForm::CreateParams(TCreateParams & Params)
  1211. {
  1212. if (!FFormRestored)
  1213. {
  1214. FFormRestored = true;
  1215. RestoreFormParams();
  1216. }
  1217. TForm::CreateParams(Params);
  1218. }
  1219. //---------------------------------------------------------------------------
  1220. void __fastcall TCustomScpExplorerForm::SetDockAllowDrag(bool value)
  1221. {
  1222. DebugAssert(FDocks != NULL);
  1223. for (int Index = 0; Index < FDocks->Count; Index++)
  1224. {
  1225. static_cast<TTBDock*>(FDocks->Items[Index])->AllowDrag = value;
  1226. }
  1227. }
  1228. //---------------------------------------------------------------------------
  1229. void __fastcall TCustomScpExplorerForm::LoadToolbarsLayoutStr(UnicodeString LayoutStr, UnicodeString ButtonsStr)
  1230. {
  1231. SetDockAllowDrag(true);
  1232. while (!ButtonsStr.IsEmpty())
  1233. {
  1234. UnicodeString S = CutToChar(ButtonsStr, L';', true);
  1235. UnicodeString Name = CutToChar(S, L'=', true);
  1236. for (int ToolbarIndex = 0; ToolbarIndex < ComponentCount; ToolbarIndex++)
  1237. {
  1238. TTBCustomToolbar * Toolbar = dynamic_cast<TTBCustomToolbar *>(Components[ToolbarIndex]);
  1239. if ((Toolbar != NULL) && SameText(Name, GetToolbarKey(Toolbar->Name)))
  1240. {
  1241. while (!S.IsEmpty())
  1242. {
  1243. UnicodeString S2 = CutToChar(S, L',', true);
  1244. UnicodeString Name = CutToChar(S2, L':', true);
  1245. for (int ItemIndex = 0; ItemIndex < Toolbar->Items->Count; ItemIndex++)
  1246. {
  1247. TTBCustomItem * Item = Toolbar->Items->Items[ItemIndex];
  1248. if (SameText(GetToolbarItemName(Item), Name))
  1249. {
  1250. // forward compatibility
  1251. UnicodeString S3 = CutToChar(S2, L':', true);
  1252. bool Visible = StrToIntDef(S3, 0);
  1253. Item->Visible = Visible;
  1254. }
  1255. }
  1256. }
  1257. }
  1258. }
  1259. }
  1260. ::LoadToolbarsLayoutStr(this, LayoutStr);
  1261. SetDockAllowDrag(!WinConfiguration->LockToolbars);
  1262. }
  1263. //---------------------------------------------------------------------------
  1264. UnicodeString __fastcall TCustomScpExplorerForm::GetToolbarsLayoutStr()
  1265. {
  1266. UnicodeString Result;
  1267. SetDockAllowDrag(true);
  1268. Result = ::GetToolbarsLayoutStr(this);
  1269. SetDockAllowDrag(!WinConfiguration->LockToolbars);
  1270. return Result;
  1271. }
  1272. //---------------------------------------------------------------------------
  1273. UnicodeString __fastcall TCustomScpExplorerForm::GetToolbarItemName(TTBCustomItem * Item)
  1274. {
  1275. UnicodeString Result;
  1276. if (Item->Action != NULL)
  1277. {
  1278. Result = Item->Action->Name;
  1279. Result = RemoveSuffix(Result, L"Action", true);
  1280. }
  1281. else
  1282. {
  1283. Result = Item->Name;
  1284. Result = RemoveSuffix(Result, L"SubmenuItem", true);
  1285. Result = RemoveSuffix(Result, L"Item", true);
  1286. }
  1287. return Result;
  1288. }
  1289. //---------------------------------------------------------------------------
  1290. UnicodeString __fastcall TCustomScpExplorerForm::GetToolbarsButtonsStr()
  1291. {
  1292. UnicodeString Result;
  1293. for (int ToolbarIndex = 0; ToolbarIndex < ComponentCount; ToolbarIndex++)
  1294. {
  1295. UnicodeString ToolbarStr;
  1296. TTBCustomToolbar * Toolbar = dynamic_cast<TTBCustomToolbar *>(Components[ToolbarIndex]);
  1297. if ((Toolbar != NULL) && (Toolbar != QueueToolbar))
  1298. {
  1299. for (int ItemIndex = 0; ItemIndex < Toolbar->Items->Count; ItemIndex++)
  1300. {
  1301. TTBCustomItem * Item = Toolbar->Items->Items[ItemIndex];
  1302. // Currently all buttons are visible by default, so we can safely remember all hidden buttons.
  1303. // Once we introduce any buttons that are hidden by default, we would have to remember their initial state
  1304. // and save the changes here only.
  1305. if (NonVisualDataModule->IsCustomizableToolbarItem(Item) && !Item->Visible)
  1306. {
  1307. UnicodeString Name = GetToolbarItemName(Item);
  1308. DebugAssert(Name.Pos(L"TBX") == 0);
  1309. AddToList(ToolbarStr, FORMAT(L"%s:0", (Name)), L",");
  1310. }
  1311. }
  1312. }
  1313. if (!ToolbarStr.IsEmpty())
  1314. {
  1315. AddToList(Result, FORMAT(L"%s=%s", (GetToolbarKey(Toolbar->Name), ToolbarStr)), L";");
  1316. }
  1317. }
  1318. return Result;
  1319. }
  1320. //---------------------------------------------------------------------------
  1321. void __fastcall TCustomScpExplorerForm::CreateProgressForm(TSynchronizeProgress * SynchronizeProgress)
  1322. {
  1323. DebugAssert(FProgressForm == NULL);
  1324. bool AllowSkip = (Terminal != NULL) ? Terminal->IsCapable[fcSkipTransfer] : false;
  1325. FProgressForm = new TProgressForm(Application, (FTransferResumeList != NULL), AllowSkip, SynchronizeProgress);
  1326. FProgressForm->DeleteLocalToRecycleBin =
  1327. (WinConfiguration->DeleteToRecycleBin != FAlternativeDelete);
  1328. FProgressForm->DeleteRemoteToRecycleBin =
  1329. (Terminal != NULL) &&
  1330. (Terminal->SessionData->DeleteToRecycleBin != FAlternativeDelete) &&
  1331. !Terminal->SessionData->RecycleBinPath.IsEmpty();
  1332. // As progress window has delayed show now, we need to lock ourselves, (at least) until then
  1333. LockWindow();
  1334. // Actually, do not know what hides the progress once the operation finishes
  1335. // (it is possibly the SetQueueProgress - and we should not rely on that)
  1336. SetTaskbarListProgressState(TBPF_NORMAL);
  1337. }
  1338. //---------------------------------------------------------------------------
  1339. void __fastcall TCustomScpExplorerForm::DestroyProgressForm()
  1340. {
  1341. UnlockWindow();
  1342. SAFE_DESTROY(FProgressForm);
  1343. SetQueueProgress();
  1344. }
  1345. //---------------------------------------------------------------------------
  1346. void __fastcall TCustomScpExplorerForm::FileOperationProgress(
  1347. TFileOperationProgressType & ProgressData)
  1348. {
  1349. // Download to temporary local directory
  1350. if (FPendingTempSpaceWarn && ProgressData.InProgress && ProgressData.TotalSizeSet)
  1351. {
  1352. bool Continue = true;
  1353. FPendingTempSpaceWarn = false;
  1354. DoWarnLackOfTempSpace(ProgressData.Directory, ProgressData.TotalSize, Continue);
  1355. if (!Continue)
  1356. {
  1357. Abort();
  1358. }
  1359. }
  1360. // operation is being executed and we still didn't show up progress form
  1361. if (ProgressData.InProgress && !FProgressForm)
  1362. {
  1363. CreateProgressForm(NULL);
  1364. DebugAssert(FProgressForm->OnceDoneOperation == odoIdle);
  1365. FProgressForm->OnceDoneOperation = ProgressData.InitialOnceDoneOperation;
  1366. }
  1367. // operation is finished (or terminated), so we hide progress form
  1368. else if (!ProgressData.InProgress && (FProgressForm != NULL) && (FProgressForm->SynchronizeProgress == NULL))
  1369. {
  1370. DestroyProgressForm();
  1371. if (ProgressData.Operation == foCalculateSize)
  1372. {
  1373. // When calculating size before transfer, the abort caused by
  1374. // cancel flag set due to "MoveToQueue" below,
  1375. // is not propagated back to us in ExecuteFileOperation,
  1376. // as it is silently swallowed.
  1377. // So we explicitly (re)throw it here.
  1378. if (FMoveToQueue)
  1379. {
  1380. Abort();
  1381. }
  1382. }
  1383. else
  1384. {
  1385. if ((ProgressData.Cancel == csContinue) &&
  1386. !FAutoOperation)
  1387. {
  1388. OperationComplete(ProgressData.StartTime);
  1389. }
  1390. }
  1391. }
  1392. if (FProgressForm &&
  1393. // Can happen only during synchronization, when ending one of the operations
  1394. ProgressData.InProgress)
  1395. {
  1396. FProgressForm->SetProgressData(ProgressData);
  1397. DebugAssert(ProgressData.InProgress);
  1398. SetTaskbarListProgressValue(&ProgressData);
  1399. if (FProgressForm->Cancel > csContinue)
  1400. {
  1401. ProgressData.SetCancelAtLeast(FProgressForm->Cancel);
  1402. if (FProgressForm->Cancel == csCancelFile)
  1403. {
  1404. FProgressForm->ClearCancel();
  1405. }
  1406. // cancel cancels even the move
  1407. FMoveToQueue = false;
  1408. }
  1409. else if (FProgressForm->MoveToQueue)
  1410. {
  1411. FMoveToQueue = true;
  1412. ProgressData.SetCancelAtLeast(csCancel);
  1413. }
  1414. if ((FTransferResumeList != NULL) &&
  1415. ProgressData.InProgress &&
  1416. ProgressData.IsTransfer() &&
  1417. !ProgressData.FullFileName.IsEmpty())
  1418. {
  1419. if ((FTransferResumeList->Count == 0) ||
  1420. (FTransferResumeList->Strings[FTransferResumeList->Count - 1] != ProgressData.FullFileName))
  1421. {
  1422. // note that we do not recognize directories from files here
  1423. FTransferResumeList->Add(ProgressData.FullFileName);
  1424. }
  1425. }
  1426. }
  1427. }
  1428. //---------------------------------------------------------------------------
  1429. void __fastcall TCustomScpExplorerForm::OperationComplete(
  1430. const TDateTime & StartTime)
  1431. {
  1432. if (GUIConfiguration->BeepOnFinish &&
  1433. (Now() - StartTime > GUIConfiguration->BeepOnFinishAfter))
  1434. {
  1435. UnicodeString BeepSound = GUIConfiguration->BeepSound;
  1436. DWORD Sound;
  1437. if (!ExtractFileExt(BeepSound).IsEmpty())
  1438. {
  1439. Sound = SND_FILENAME;
  1440. }
  1441. else
  1442. {
  1443. Sound = SND_ALIAS;
  1444. }
  1445. PlaySound(BeepSound.c_str(), NULL, Sound | SND_ASYNC);
  1446. }
  1447. }
  1448. //---------------------------------------------------------------------------
  1449. void __fastcall TCustomScpExplorerForm::OperationProgress(
  1450. TFileOperationProgressType & ProgressData)
  1451. {
  1452. // For example, when calling TFileOperationProgressType::Suspend in TTerminal::QueryReopen,
  1453. // we cannot recurse back to TTerminal::Idle as that may detect another error, calling "suspend" again.
  1454. TAutoNestingCounter Counter(FDoNotIdleCurrentTerminal);
  1455. FileOperationProgress(ProgressData);
  1456. }
  1457. //---------------------------------------------------------------------------
  1458. UnicodeString __fastcall TCustomScpExplorerForm::GetProgressTitle()
  1459. {
  1460. UnicodeString Result;
  1461. if (FProgressForm != NULL)
  1462. {
  1463. Result = FProgressForm->ProgressStr();
  1464. }
  1465. return Result;
  1466. }
  1467. //---------------------------------------------------------------------------
  1468. bool __fastcall TCustomScpExplorerForm::PanelOperation(TOperationSide /*Side*/,
  1469. bool DragDrop)
  1470. {
  1471. return (!DragDrop && (DropSourceControl == NULL)) ||
  1472. (DropSourceControl == DirView(osOther));
  1473. }
  1474. //---------------------------------------------------------------------------
  1475. void __fastcall TCustomScpExplorerForm::DoOperationFinished(
  1476. TFileOperation Operation, TOperationSide Side,
  1477. bool /*Temp*/, const UnicodeString & FileName, bool Success,
  1478. TOnceDoneOperation & OnceDoneOperation)
  1479. {
  1480. if (!FAutoOperation)
  1481. {
  1482. // no selection on "/upload", form serves only as event handler
  1483. // (it is not displayed)
  1484. if (PanelOperation(Side, FDragDropOperation) &&
  1485. Visible && (Operation != foCalculateSize) &&
  1486. (Operation != foGetProperties) && (Operation != foCalculateChecksum))
  1487. {
  1488. if (FOnFileOperationFinished != NULL)
  1489. {
  1490. FOnFileOperationFinished(FileName, Success);
  1491. }
  1492. else
  1493. {
  1494. TCustomDirView * DView = DirView(Side);
  1495. UnicodeString FileNameOnly = ExtractFileName(FileName, !IsSideLocalBrowser(Side));
  1496. TListItem *Item = DView->FindFileItem(FileNameOnly);
  1497. // this can happen when local drive is unplugged in the middle of the operation
  1498. if (Item != NULL)
  1499. {
  1500. if (Success) Item->Selected = false;
  1501. DView->MakeProgressVisible(Item);
  1502. }
  1503. }
  1504. }
  1505. if (TFileOperationProgressType::IsTransferOperation(Operation))
  1506. {
  1507. DebugAssert(!IsLocalBrowserMode());
  1508. if (Side == osLocal)
  1509. {
  1510. Configuration->Usage->Inc(L"UploadedFiles");
  1511. }
  1512. else
  1513. {
  1514. Configuration->Usage->Inc(L"DownloadedFiles");
  1515. }
  1516. }
  1517. }
  1518. if (Success && (FSynchronizeController != NULL))
  1519. {
  1520. DebugAssert(!IsSideLocalBrowser(osRemote));
  1521. if (Operation == foCopy)
  1522. {
  1523. DebugAssert(Side == osLocal);
  1524. FSynchronizeController->LogOperation(soUpload, FileName);
  1525. }
  1526. else if (Operation == foDelete)
  1527. {
  1528. DebugAssert(Side == osRemote);
  1529. FSynchronizeController->LogOperation(soDelete, FileName);
  1530. }
  1531. }
  1532. if (FProgressForm)
  1533. {
  1534. OnceDoneOperation = FProgressForm->OnceDoneOperation;
  1535. }
  1536. }
  1537. //---------------------------------------------------------------------------
  1538. void __fastcall TCustomScpExplorerForm::OperationFinished(
  1539. TFileOperation Operation, TOperationSide Side,
  1540. bool Temp, const UnicodeString & FileName, Boolean Success,
  1541. TOnceDoneOperation & OnceDoneOperation)
  1542. {
  1543. TAutoNestingCounter Counter(FDoNotIdleCurrentTerminal);
  1544. DoOperationFinished(Operation, Side, Temp, FileName, Success,
  1545. OnceDoneOperation);
  1546. }
  1547. //---------------------------------------------------------------------------
  1548. bool TCustomScpExplorerForm::IsLocalBrowserMode()
  1549. {
  1550. return false;
  1551. }
  1552. //---------------------------------------------------------------------------
  1553. bool TCustomScpExplorerForm::IsSideLocalBrowser(TOperationSide)
  1554. {
  1555. return false;
  1556. }
  1557. //---------------------------------------------------------------------------
  1558. TCustomDirView * TCustomScpExplorerForm::GetCurrentLocalBrowser()
  1559. {
  1560. return NULL;
  1561. }
  1562. //---------------------------------------------------------------------------
  1563. TCustomDirView * __fastcall TCustomScpExplorerForm::DirView(TOperationSide Side)
  1564. {
  1565. DebugAssert(GetSide(Side) == osRemote);
  1566. DebugUsedParam(Side);
  1567. return RemoteDirView;
  1568. }
  1569. //---------------------------------------------------------------------------
  1570. TCustomDriveView * __fastcall TCustomScpExplorerForm::DriveView(TOperationSide Side)
  1571. {
  1572. DebugAssert(GetSide(Side) == osRemote);
  1573. DebugUsedParam(Side);
  1574. return RemoteDriveView;
  1575. }
  1576. //---------------------------------------------------------------------------
  1577. bool __fastcall TCustomScpExplorerForm::DirViewEnabled(TOperationSide Side)
  1578. {
  1579. DebugAssert(GetSide(Side) == osRemote);
  1580. DebugUsedParam(Side);
  1581. return HasActiveTerminal();
  1582. }
  1583. //---------------------------------------------------------------------------
  1584. bool __fastcall TCustomScpExplorerForm::GetEnableFocusedOperation(
  1585. TOperationSide Side, int FilesOnly)
  1586. {
  1587. return DirView(Side)->AnyFileSelected(true, (FilesOnly != 0), true);
  1588. }
  1589. //---------------------------------------------------------------------------
  1590. bool __fastcall TCustomScpExplorerForm::GetEnableSelectedOperation(
  1591. TOperationSide Side, int FilesOnly)
  1592. {
  1593. return DirView(Side)->AnyFileSelected(false, (FilesOnly != 0), true);
  1594. }
  1595. //---------------------------------------------------------------------------
  1596. struct THistoryItemData
  1597. {
  1598. short int Side;
  1599. short int Index;
  1600. };
  1601. //---------------------------------------------------------------------------
  1602. void __fastcall TCustomScpExplorerForm::HistoryGo(TOperationSide Side, int Index)
  1603. {
  1604. DirView(Side)->HistoryGo(Index);
  1605. }
  1606. //---------------------------------------------------------------------------
  1607. void __fastcall TCustomScpExplorerForm::HistoryItemClick(System::TObject* Sender)
  1608. {
  1609. TTBCustomItem * Item = dynamic_cast<TTBCustomItem *>(Sender);
  1610. THistoryItemData Data = *reinterpret_cast<THistoryItemData*>(&(Item->Tag));
  1611. HistoryGo((TOperationSide)Data.Side, Data.Index);
  1612. }
  1613. //---------------------------------------------------------------------------
  1614. void __fastcall TCustomScpExplorerForm::UpdateHistoryMenu(TOperationSide Side,
  1615. bool Back)
  1616. {
  1617. if (FHistoryMenu[Side == osLocal][Back] != NULL)
  1618. {
  1619. TCustomDirView * DView = DirView(Side);
  1620. TTBXPopupMenu * Menu = FHistoryMenu[Side == osLocal][Back];
  1621. int ICount = Back ? DView->BackCount : DView->ForwardCount;
  1622. if (ICount > 10)
  1623. {
  1624. ICount = 10;
  1625. }
  1626. Menu->Items->Clear();
  1627. THistoryItemData Data;
  1628. Data.Side = Side;
  1629. for (short int i = 1; i <= ICount; i++)
  1630. {
  1631. TTBCustomItem * Item = new TTBXItem(Menu);
  1632. Data.Index = static_cast<short int>(i * (Back ? -1 : 1));
  1633. Item->Caption = MinimizeName(DView->HistoryPath[Data.Index], 50, !IsSideLocalBrowser(Side));
  1634. Item->Hint = DView->HistoryPath[Data.Index];
  1635. DebugAssert(sizeof(int) == sizeof(THistoryItemData));
  1636. Item->Tag = *reinterpret_cast<int*>(&Data);
  1637. Item->OnClick = HistoryItemClick;
  1638. Menu->Items->Add(Item);
  1639. }
  1640. }
  1641. }
  1642. //---------------------------------------------------------------------------
  1643. TTBXPopupMenu * __fastcall TCustomScpExplorerForm::HistoryMenu(
  1644. TOperationSide Side, bool Back)
  1645. {
  1646. if (FHistoryMenu[Side == osLocal][Back] == NULL)
  1647. {
  1648. // workaround
  1649. // In Pascal the size of TTBXPopupMenu is 132, in C++ 136,
  1650. // operator new allocates memory in Pascal code, but calls inline
  1651. // contructor in C++, leading in problems, the function does
  1652. // both in Pascal code
  1653. FHistoryMenu[Side == osLocal][Back] = CreateTBXPopupMenu(this);
  1654. UpdateHistoryMenu(Side, Back);
  1655. }
  1656. return FHistoryMenu[Side == osLocal][Back];
  1657. }
  1658. //---------------------------------------------------------------------------
  1659. void __fastcall TCustomScpExplorerForm::DirViewHistoryChange(
  1660. TCustomDirView *Sender)
  1661. {
  1662. TOperationSide Side = (Sender == DirView(osOther) ? osOther : osLocal);
  1663. UpdateHistoryMenu(Side, true);
  1664. UpdateHistoryMenu(Side, false);
  1665. }
  1666. //---------------------------------------------------------------------------
  1667. bool __fastcall TCustomScpExplorerForm::CustomCommandRemoteAllowed()
  1668. {
  1669. // remote custom commands can be executed only if the server supports shell commands
  1670. // or have secondary shell
  1671. return HasActiveTerminal() && (FTerminal->IsCapable[fcSecondaryShell] || FTerminal->IsCapable[fcShellAnyCommand]);
  1672. }
  1673. //---------------------------------------------------------------------------
  1674. int __fastcall TCustomScpExplorerForm::CustomCommandState(
  1675. const TCustomCommandType & Command, bool OnFocused, TCustomCommandListType ListType)
  1676. {
  1677. // -1 = hidden, 0 = disabled, 1 = enabled
  1678. int Result;
  1679. TFileCustomCommand RemoteCustomCommand;
  1680. TLocalCustomCommand LocalCustomCommand;
  1681. TFileCustomCommand * NonInteractiveCustomCommand =
  1682. FLAGCLEAR(Command.Params, ccLocal) ? &RemoteCustomCommand : &LocalCustomCommand;
  1683. TInteractiveCustomCommand InteractiveCustomCommand(NonInteractiveCustomCommand);
  1684. UnicodeString Cmd = InteractiveCustomCommand.Complete(Command.Command, false);
  1685. if (FLAGCLEAR(Command.Params, ccLocal))
  1686. {
  1687. int AllowedState = CustomCommandRemoteAllowed() ? 1 : 0;
  1688. // custom command that does not operate with files can be executed anytime ...
  1689. if (!NonInteractiveCustomCommand->IsFileCommand(Cmd))
  1690. {
  1691. // ... but do not show such command in remote file menu
  1692. if ((ListType == ccltAll) || (ListType == ccltNonFile))
  1693. {
  1694. Result = AllowedState;
  1695. }
  1696. else
  1697. {
  1698. Result = -1;
  1699. }
  1700. }
  1701. else
  1702. {
  1703. if ((ListType == ccltAll) || ((ListType == ccltFile) && !IsSideLocalBrowser(FCurrentSide)))
  1704. {
  1705. Result = (DirView(FCurrentSide)->AnyFileSelected(OnFocused, false, true)) ? AllowedState : 0;
  1706. }
  1707. else
  1708. {
  1709. Result = -1;
  1710. }
  1711. }
  1712. }
  1713. else
  1714. {
  1715. // custom command that does not operate with files can be executed anytime
  1716. if (!NonInteractiveCustomCommand->IsFileCommand(Cmd))
  1717. {
  1718. Result = ((ListType == ccltAll) || (ListType == ccltNonFile)) ? 1 : -1;
  1719. }
  1720. else if (LocalCustomCommand.HasLocalFileName(Cmd))
  1721. {
  1722. if ((ListType == ccltAll) || (ListType == ccltFile))
  1723. {
  1724. // special case is "diff"-style command that can be executed over any side,
  1725. // if we have both sides
  1726. Result =
  1727. // Cannot have focus on both panels, so we have to call AnyFileSelected
  1728. // directly (instead of EnableSelectedOperation) to pass
  1729. // false to FocusedFileOnlyWhenFocused when panel is inactive.
  1730. ((HasDirView[osLocal] && !IsSideLocalBrowser(osRemote) &&
  1731. DirView(osLocal)->AnyFileSelected(false, false, (FCurrentSide == osLocal))) &&
  1732. DirView(osRemote)->AnyFileSelected(false, false, (FCurrentSide == osRemote))) ? 1 : 0;
  1733. }
  1734. else if (ListType == ccltBoth)
  1735. {
  1736. DebugAssert(!IsLocalBrowserMode());
  1737. Result = 1;
  1738. }
  1739. else
  1740. {
  1741. Result = -1;
  1742. }
  1743. }
  1744. else
  1745. {
  1746. if ((ListType == ccltAll) ||
  1747. ((ListType == ccltFile) && (!IsSideLocalBrowser(FCurrentSide) || FLAGCLEAR(Command.Params, ccRemoteFiles))))
  1748. {
  1749. Result = DirView(FCurrentSide)->AnyFileSelected(OnFocused, false, true) ? 1 : 0;
  1750. }
  1751. else
  1752. {
  1753. Result = -1;
  1754. }
  1755. }
  1756. if ((Result > 0) &&
  1757. LocalCustomCommand.IsSessionCommand(Cmd) &&
  1758. !HasActiveTerminal())
  1759. {
  1760. Result = 0;
  1761. }
  1762. }
  1763. return Result;
  1764. }
  1765. //---------------------------------------------------------------------------
  1766. void __fastcall TCustomScpExplorerForm::RemoteCustomCommand(
  1767. TStrings * FileList, const TCustomCommandType & ACommand,
  1768. const TCustomCommandData & Data, const UnicodeString & CommandCommand)
  1769. {
  1770. if (EnsureCommandSessionFallback(fcShellAnyCommand))
  1771. {
  1772. TRemoteCustomCommand RemoteCustomCommand(Data, Terminal->CurrentDirectory);
  1773. TWinInteractiveCustomCommand InteractiveCustomCommand(
  1774. &RemoteCustomCommand, ACommand.Name, ACommand.HomePage);
  1775. UnicodeString Command = InteractiveCustomCommand.Complete(CommandCommand, false);
  1776. Configuration->Usage->Inc(L"RemoteCustomCommandRuns2");
  1777. bool Capture =
  1778. FLAGSET(ACommand.Params, ccShowResults) ||
  1779. FLAGSET(ACommand.Params, ccCopyResults) ||
  1780. FLAGSET(ACommand.Params, ccShowResultsInMsgBox);
  1781. TCaptureOutputEvent OutputEvent = NULL;
  1782. DebugAssert(FCapturedLog == NULL);
  1783. if (Capture)
  1784. {
  1785. FCapturedLog = new TStringList();
  1786. OutputEvent = TerminalCaptureLog;
  1787. }
  1788. try
  1789. {
  1790. if (!RemoteCustomCommand.IsFileCommand(Command))
  1791. {
  1792. Terminal->AnyCommand(RemoteCustomCommand.Complete(Command, true),
  1793. OutputEvent);
  1794. }
  1795. else
  1796. {
  1797. Terminal->CustomCommandOnFiles(Command, ACommand.Params, FileList, OutputEvent);
  1798. }
  1799. if ((FCapturedLog != NULL) && (FCapturedLog->Count > 0))
  1800. {
  1801. if (FLAGSET(ACommand.Params, ccCopyResults))
  1802. {
  1803. CopyToClipboard(FCapturedLog);
  1804. }
  1805. if (FLAGSET(ACommand.Params, ccShowResults))
  1806. {
  1807. DoConsoleDialog(Terminal, L"", FCapturedLog);
  1808. }
  1809. else if (FLAGSET(ACommand.Params, ccShowResultsInMsgBox))
  1810. {
  1811. MessageDialog(FCapturedLog->Text, qtInformation, qaOK);
  1812. }
  1813. }
  1814. }
  1815. __finally
  1816. {
  1817. SAFE_DESTROY(FCapturedLog);
  1818. }
  1819. }
  1820. }
  1821. //---------------------------------------------------------------------------
  1822. void __fastcall TCustomScpExplorerForm::LocalCustomCommandPure(
  1823. TStrings * FileList, const TCustomCommandType & ACommand, const UnicodeString & Command, TStrings * ALocalFileList,
  1824. const TCustomCommandData & Data, bool LocalFileCommand, bool FileListCommand, UnicodeString * POutput)
  1825. {
  1826. DebugAssert(!IsLocalBrowserMode());
  1827. TStrings * LocalFileList = NULL;
  1828. TStrings * RemoteFileList = NULL;
  1829. TStrings * RemoteFileListFull = NULL;
  1830. try
  1831. {
  1832. if (LocalFileCommand)
  1833. {
  1834. if (ALocalFileList == NULL)
  1835. {
  1836. DebugAssert(HasDirView[osLocal]);
  1837. // Cannot have focus on both panels, so we have to call AnyFileSelected
  1838. // directly (instead of EnableSelectedOperation) to pass
  1839. // false to FocusedFileOnlyWhenFocused
  1840. DebugAssert(DirView(osLocal)->AnyFileSelected(false, false, false));
  1841. LocalFileList = DirView(osLocal)->CreateFileList(false, true, NULL);
  1842. }
  1843. else
  1844. {
  1845. LocalFileList = ALocalFileList;
  1846. }
  1847. if (FileListCommand)
  1848. {
  1849. if (LocalFileList->Count != 1)
  1850. {
  1851. throw Exception(LoadStr(CUSTOM_COMMAND_SELECTED_UNMATCH1));
  1852. }
  1853. }
  1854. else
  1855. {
  1856. if ((LocalFileList->Count != 1) &&
  1857. (FileList->Count != 1) &&
  1858. (LocalFileList->Count != FileList->Count))
  1859. {
  1860. throw Exception(LoadStr(CUSTOM_COMMAND_SELECTED_UNMATCH));
  1861. }
  1862. }
  1863. }
  1864. UnicodeString RootTempDir;
  1865. UnicodeString TempDir;
  1866. bool RemoteFiles = FLAGSET(ACommand.Params, ccRemoteFiles);
  1867. if (!RemoteFiles)
  1868. {
  1869. TemporarilyDownloadFiles(FileList, false, RootTempDir, TempDir, false, true);
  1870. }
  1871. try
  1872. {
  1873. TDateTimes RemoteFileTimes;
  1874. if (RemoteFiles)
  1875. {
  1876. RemoteFileList = FileList;
  1877. }
  1878. else
  1879. {
  1880. RemoteFileList = new TStringList();
  1881. TMakeLocalFileListParams MakeFileListParam;
  1882. MakeFileListParam.FileList = RemoteFileList;
  1883. MakeFileListParam.FileTimes = &RemoteFileTimes;
  1884. MakeFileListParam.IncludeDirs = FLAGSET(ACommand.Params, ccApplyToDirectories);
  1885. MakeFileListParam.Recursive =
  1886. FLAGSET(ACommand.Params, ccRecursive) && !FileListCommand;
  1887. ProcessLocalDirectory(TempDir, Terminal->MakeLocalFileList, &MakeFileListParam);
  1888. if (!MakeFileListParam.Recursive)
  1889. {
  1890. RemoteFileTimes.clear();
  1891. RemoteFileListFull = new TStringList();
  1892. MakeFileListParam.FileList = RemoteFileListFull;
  1893. MakeFileListParam.Recursive = true;
  1894. MakeFileListParam.IncludeDirs = false;
  1895. ProcessLocalDirectory(TempDir, Terminal->MakeLocalFileList, &MakeFileListParam);
  1896. }
  1897. else
  1898. {
  1899. RemoteFileListFull = RemoteFileList;
  1900. }
  1901. }
  1902. bool NonBlocking = FileListCommand && RemoteFiles && !POutput;
  1903. TFileOperationProgressType Progress(&OperationProgress, &OperationFinished);
  1904. if (!NonBlocking)
  1905. {
  1906. int Count;
  1907. if (FileListCommand)
  1908. {
  1909. Count = 1;
  1910. }
  1911. else if (LocalFileCommand)
  1912. {
  1913. Count = std::max(LocalFileList->Count, RemoteFileList->Count);
  1914. }
  1915. else
  1916. {
  1917. Count = RemoteFileList->Count;
  1918. }
  1919. Progress.Start(foCustomCommand, osRemote, Count);
  1920. DebugAssert(FProgressForm != NULL);
  1921. FProgressForm->ReadOnly = true;
  1922. }
  1923. try
  1924. {
  1925. if (FileListCommand)
  1926. {
  1927. UnicodeString LocalFile;
  1928. // MakeFileList does not delimit filenames
  1929. UnicodeString FileList = MakeFileList(RemoteFileList);
  1930. if (LocalFileCommand)
  1931. {
  1932. DebugAssert(LocalFileList->Count == 1);
  1933. LocalFile = LocalFileList->Strings[0];
  1934. }
  1935. TLocalCustomCommand CustomCommand(Data,
  1936. Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(), L"", LocalFile, FileList);
  1937. UnicodeString ShellCommand = CustomCommand.Complete(Command, true);
  1938. if (NonBlocking)
  1939. {
  1940. DebugAssert(!POutput);
  1941. ExecuteShellChecked(ShellCommand);
  1942. }
  1943. else
  1944. {
  1945. ExecuteProcessCheckedAndWait(ShellCommand, HelpKeyword, POutput);
  1946. }
  1947. }
  1948. else if (LocalFileCommand)
  1949. {
  1950. if (LocalFileList->Count == 1)
  1951. {
  1952. UnicodeString LocalFile = LocalFileList->Strings[0];
  1953. for (int Index = 0; Index < RemoteFileList->Count; Index++)
  1954. {
  1955. UnicodeString FileName = RemoteFileList->Strings[Index];
  1956. UnicodeString FileNameForProgress = RemoteFiles ? FileName : ExtractFileName(FileName);
  1957. Progress.SetFile(FileNameForProgress);
  1958. TLocalCustomCommand CustomCommand(Data,
  1959. Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(), FileName, LocalFile, L"");
  1960. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  1961. TOnceDoneOperation OnceDoneOperation;
  1962. // Do not unselect anything, as it won't work with two panels anyway
  1963. Progress.Finish(UnicodeString(), true, OnceDoneOperation);
  1964. }
  1965. }
  1966. else if (RemoteFileList->Count == 1)
  1967. {
  1968. UnicodeString FileName = RemoteFileList->Strings[0];
  1969. for (int Index = 0; Index < LocalFileList->Count; Index++)
  1970. {
  1971. UnicodeString LocalFileName = LocalFileList->Strings[Index];
  1972. UnicodeString LocalFileNameOnly = ExtractFileName(LocalFileName);
  1973. Progress.SetFile(LocalFileNameOnly);
  1974. TLocalCustomCommand CustomCommand(
  1975. Data, Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(),
  1976. FileName, LocalFileName, L"");
  1977. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  1978. TOnceDoneOperation OnceDoneOperation;
  1979. // Do not unselect anything, as it won't work with two panels anyway
  1980. Progress.Finish(UnicodeString(), true, OnceDoneOperation);
  1981. }
  1982. }
  1983. else
  1984. {
  1985. if (LocalFileList->Count != RemoteFileList->Count)
  1986. {
  1987. throw Exception(LoadStr(CUSTOM_COMMAND_PAIRS_DOWNLOAD_FAILED));
  1988. }
  1989. for (int Index = 0; Index < LocalFileList->Count; Index++)
  1990. {
  1991. UnicodeString FileName = RemoteFileList->Strings[Index];
  1992. UnicodeString LocalFileName = LocalFileList->Strings[Index];
  1993. UnicodeString LocalFileNameOnly = ExtractFileName(LocalFileName);
  1994. Progress.SetFile(LocalFileNameOnly); // sic
  1995. TLocalCustomCommand CustomCommand(
  1996. Data, Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(),
  1997. FileName, LocalFileName, L"");
  1998. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  1999. TOnceDoneOperation OnceDoneOperation;
  2000. // Do not unselect anything, as it won't work with two panels anyway
  2001. Progress.Finish(UnicodeString(), true, OnceDoneOperation);
  2002. }
  2003. }
  2004. }
  2005. else
  2006. {
  2007. for (int Index = 0; Index < RemoteFileList->Count; Index++)
  2008. {
  2009. UnicodeString FileName = RemoteFileList->Strings[Index];
  2010. UnicodeString FileNameForProgress = RemoteFiles ? FileName : ExtractFileName(FileName);
  2011. Progress.SetFile(FileNameForProgress);
  2012. TLocalCustomCommand CustomCommand(Data,
  2013. Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(),
  2014. FileName, L"", L"");
  2015. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  2016. TOnceDoneOperation OnceDoneOperation;
  2017. Progress.Finish(FileNameForProgress, true, OnceDoneOperation);
  2018. }
  2019. }
  2020. }
  2021. __finally
  2022. {
  2023. if (!NonBlocking)
  2024. {
  2025. Progress.Stop();
  2026. }
  2027. }
  2028. DebugAssert(!FAutoOperation);
  2029. if (!RemoteFiles)
  2030. {
  2031. TempDir = IncludeTrailingBackslash(TempDir);
  2032. for (int Index = 0; Index < RemoteFileListFull->Count; Index++)
  2033. {
  2034. UnicodeString FileName = RemoteFileListFull->Strings[Index];
  2035. if (DebugAlwaysTrue(SameText(TempDir, FileName.SubString(1, TempDir.Length()))) &&
  2036. // Skip directories, process only nested files.
  2037. // The check is redundant as FileAge fails for directories anyway.
  2038. !DirectoryExists(FileName))
  2039. {
  2040. UnicodeString RemoteDir =
  2041. UnixExtractFileDir(
  2042. UnixIncludeTrailingBackslash(FTerminal->CurrentDirectory) +
  2043. ToUnixPath(FileName.SubString(TempDir.Length() + 1, FileName.Length() - TempDir.Length())));
  2044. TDateTime NewTime;
  2045. if (FileAge(FileName, NewTime) &&
  2046. (NewTime != RemoteFileTimes[Index]))
  2047. {
  2048. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  2049. TemporaryFileCopyParam(CopyParam);
  2050. CopyParam.FileMask = L"";
  2051. FAutoOperation = true;
  2052. std::unique_ptr<TStrings> TemporaryFilesList(new TStringList());
  2053. TemporaryFilesList->Add(FileName);
  2054. FTerminal->CopyToRemote(TemporaryFilesList.get(), RemoteDir, &CopyParam, cpTemporary, NULL);
  2055. }
  2056. }
  2057. }
  2058. }
  2059. }
  2060. __finally
  2061. {
  2062. FAutoOperation = false;
  2063. if (!RootTempDir.IsEmpty() && DebugAlwaysTrue(!RemoteFiles))
  2064. {
  2065. RecursiveDeleteFile(ExcludeTrailingBackslash(RootTempDir), false);
  2066. }
  2067. }
  2068. }
  2069. __finally
  2070. {
  2071. if (RemoteFileList != FileList)
  2072. {
  2073. delete RemoteFileList;
  2074. }
  2075. if (LocalFileList != ALocalFileList)
  2076. {
  2077. delete LocalFileList;
  2078. }
  2079. if (RemoteFileListFull != RemoteFileList)
  2080. {
  2081. delete RemoteFileListFull;
  2082. }
  2083. }
  2084. }
  2085. //---------------------------------------------------------------------------
  2086. void __fastcall TCustomScpExplorerForm::LocalCustomCommandWithLocalFiles(
  2087. const TCustomCommandType & ACommand, const UnicodeString & Command, const TCustomCommandData & Data,
  2088. bool FileListCommand, UnicodeString * POutput)
  2089. {
  2090. std::unique_ptr<TStrings> SelectedFileList(DirView(osLocal)->CreateFileList(false, true, NULL));
  2091. std::unique_ptr<TStrings> LocalFileList(new TStringList());
  2092. for (int Index = 0; Index < SelectedFileList->Count; Index++)
  2093. {
  2094. UnicodeString FileName = SelectedFileList->Strings[Index];
  2095. if (DirectoryExists(FileName))
  2096. {
  2097. if (FLAGSET(ACommand.Params, ccApplyToDirectories))
  2098. {
  2099. LocalFileList->Add(FileName);
  2100. }
  2101. if (FLAGSET(ACommand.Params, ccRecursive))
  2102. {
  2103. TMakeLocalFileListParams MakeFileListParam;
  2104. MakeFileListParam.FileList = LocalFileList.get();
  2105. MakeFileListParam.FileTimes = NULL;
  2106. MakeFileListParam.IncludeDirs = FLAGSET(ACommand.Params, ccApplyToDirectories);
  2107. MakeFileListParam.Recursive = true;
  2108. ProcessLocalDirectory(FileName, Terminal->MakeLocalFileList, &MakeFileListParam);
  2109. }
  2110. }
  2111. else
  2112. {
  2113. LocalFileList->Add(FileName);
  2114. }
  2115. }
  2116. UnicodeString RemotePath;
  2117. if (Terminal != NULL)
  2118. {
  2119. RemotePath = Terminal->CurrentDirectory;
  2120. }
  2121. if (FileListCommand)
  2122. {
  2123. UnicodeString FileList = MakeFileList(LocalFileList.get());
  2124. TLocalCustomCommand CustomCommand(
  2125. Data, RemotePath, DefaultDownloadTargetDirectory(),
  2126. L"", L"", FileList);
  2127. ExecuteProcessChecked(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  2128. }
  2129. else
  2130. {
  2131. TFileOperationProgressType Progress(&OperationProgress, &OperationFinished);
  2132. Progress.Start(foCustomCommand, osLocal, LocalFileList->Count);
  2133. DebugAssert(FProgressForm != NULL);
  2134. FProgressForm->ReadOnly = true;
  2135. try
  2136. {
  2137. for (int Index = 0; Index < LocalFileList->Count; Index++)
  2138. {
  2139. UnicodeString FileName = LocalFileList->Strings[Index];
  2140. Progress.SetFile(FileName);
  2141. TLocalCustomCommand CustomCommand(
  2142. Data, RemotePath, DefaultDownloadTargetDirectory(),
  2143. FileName, L"", L"");
  2144. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  2145. TOnceDoneOperation OnceDoneOperation;
  2146. Progress.Finish(FileName, true, OnceDoneOperation);
  2147. }
  2148. }
  2149. __finally
  2150. {
  2151. Progress.Stop();
  2152. }
  2153. }
  2154. }
  2155. //---------------------------------------------------------------------------
  2156. void __fastcall TCustomScpExplorerForm::LocalCustomCommand(TStrings * FileList,
  2157. const TCustomCommandType & ACommand, TStrings * ALocalFileList,
  2158. const TCustomCommandData & Data, const UnicodeString & CommandCommand)
  2159. {
  2160. UnicodeString RemotePath = (Terminal != NULL) ? Terminal->CurrentDirectory : UnicodeString();
  2161. TLocalCustomCommand LocalCustomCommand(Data, RemotePath, DefaultDownloadTargetDirectory());
  2162. TWinInteractiveCustomCommand InteractiveCustomCommand(
  2163. &LocalCustomCommand, ACommand.Name, ACommand.HomePage);
  2164. UnicodeString Command = InteractiveCustomCommand.Complete(CommandCommand, false);
  2165. bool FileListCommand = LocalCustomCommand.IsFileListCommand(Command);
  2166. bool LocalFileCommand = LocalCustomCommand.HasLocalFileName(Command);
  2167. Configuration->Usage->Inc(L"LocalCustomCommandRuns2");
  2168. std::unique_ptr<UnicodeString> POutput;
  2169. if (FLAGSET(ACommand.Params, ccShowResultsInMsgBox) ||
  2170. FLAGSET(ACommand.Params, ccCopyResults))
  2171. {
  2172. POutput.reset(new UnicodeString());
  2173. }
  2174. if (!LocalCustomCommand.IsFileCommand(Command))
  2175. {
  2176. ExecuteProcessChecked(LocalCustomCommand.Complete(Command, true), HelpKeyword, POutput.get());
  2177. }
  2178. // remote files?
  2179. else if ((FCurrentSide == osRemote) || LocalFileCommand)
  2180. {
  2181. LocalCustomCommandPure(FileList, ACommand, Command, ALocalFileList, Data, LocalFileCommand, FileListCommand, POutput.get());
  2182. }
  2183. // local files
  2184. else
  2185. {
  2186. LocalCustomCommandWithLocalFiles(ACommand, Command, Data, FileListCommand, POutput.get());
  2187. }
  2188. if (POutput.get() != NULL)
  2189. {
  2190. // If the output is single-line, we do not want the trailing CRLF, as the CopyToClipboard(TStrings *) does
  2191. int P = POutput->Pos(sLineBreak);
  2192. if (P == POutput->Length() - static_cast<int>(strlen(sLineBreak)) + 1)
  2193. {
  2194. POutput->SetLength(P - 1);
  2195. }
  2196. // Copy even empty output to clipboard
  2197. if (FLAGSET(ACommand.Params, ccCopyResults))
  2198. {
  2199. CopyToClipboard(*POutput);
  2200. }
  2201. // But do not show an empty message box.
  2202. // This way the ShowResultsInMsgBox can be used to suppress a console window of commands with no output
  2203. if (FLAGSET(ACommand.Params, ccShowResultsInMsgBox) &&
  2204. !POutput->IsEmpty())
  2205. {
  2206. TClipboardHandler ClipboardHandler;
  2207. ClipboardHandler.Text = *POutput;
  2208. TMessageParams Params;
  2209. TQueryButtonAlias Aliases[1];
  2210. Aliases[0].Button = qaRetry;
  2211. Aliases[0].Alias = LoadStr(EDIT_COPY);
  2212. Aliases[0].OnSubmit = &ClipboardHandler.Copy;
  2213. Params.Aliases = Aliases;
  2214. Params.AliasesCount = LENOF(Aliases);
  2215. MessageDialog(*POutput, qtInformation, qaOK | qaRetry, HelpKeyword, &Params);
  2216. }
  2217. }
  2218. }
  2219. //---------------------------------------------------------------------------
  2220. void __fastcall TCustomScpExplorerForm::CustomCommand(TStrings * FileList,
  2221. const TCustomCommandType & ACommand, TStrings * ALocalFileList)
  2222. {
  2223. TCustomCommandData Data;
  2224. UnicodeString Site;
  2225. UnicodeString RemotePath;
  2226. if (HasActiveTerminal())
  2227. {
  2228. std::unique_ptr<TSessionData> SessionData(SessionDataForCode());
  2229. Data = TCustomCommandData(SessionData.get());
  2230. Site = Terminal->SessionData->SessionKey;
  2231. RemotePath = Terminal->CurrentDirectory;
  2232. }
  2233. UnicodeString HelpKeyword = ACommand.HomePage;
  2234. std::unique_ptr<TStrings> CustomCommandOptions(CloneStrings(WinConfiguration->CustomCommandOptions));
  2235. if (ACommand.AnyOptionWithFlag(TCustomCommandType::ofRun))
  2236. {
  2237. std::unique_ptr<TCustomCommand> CustomCommandForOptions;
  2238. if (FLAGCLEAR(ACommand.Params, ccLocal))
  2239. {
  2240. CustomCommandForOptions.reset(new TRemoteCustomCommand(Data, RemotePath));
  2241. }
  2242. else
  2243. {
  2244. CustomCommandForOptions.reset(new TLocalCustomCommand(Data, RemotePath, DefaultDownloadTargetDirectory()));
  2245. }
  2246. if (!DoCustomCommandOptionsDialog(
  2247. &ACommand, CustomCommandOptions.get(), NULL, TCustomCommandType::ofRun, CustomCommandForOptions.get(), Site, NULL))
  2248. {
  2249. Abort();
  2250. }
  2251. }
  2252. UnicodeString CommandCommand = ACommand.GetCommandWithExpandedOptions(CustomCommandOptions.get(), Site);
  2253. if (FLAGCLEAR(ACommand.Params, ccLocal))
  2254. {
  2255. RemoteCustomCommand(FileList, ACommand, Data, CommandCommand);
  2256. }
  2257. else
  2258. {
  2259. LocalCustomCommand(FileList, ACommand, ALocalFileList, Data, CommandCommand);
  2260. }
  2261. }
  2262. //---------------------------------------------------------------------------
  2263. void __fastcall TCustomScpExplorerForm::BothCustomCommand(
  2264. const TCustomCommandType & Command)
  2265. {
  2266. DebugAssert(FCustomCommandLocalFileList != NULL);
  2267. DebugAssert(FCustomCommandRemoteFileList != NULL);
  2268. DebugAssert(FCustomCommandLocalFileList->Count == FCustomCommandRemoteFileList->Count);
  2269. TStrings * LocalFileList = new TStringList();
  2270. TStrings * RemoteFileList = new TStringList();
  2271. try
  2272. {
  2273. for (int Index = 0; Index < FCustomCommandLocalFileList->Count; Index++)
  2274. {
  2275. LocalFileList->Clear();
  2276. LocalFileList->AddObject(
  2277. FCustomCommandLocalFileList->Strings[Index],
  2278. FCustomCommandLocalFileList->Objects[Index]);
  2279. RemoteFileList->Clear();
  2280. RemoteFileList->AddObject(
  2281. FCustomCommandRemoteFileList->Strings[Index],
  2282. FCustomCommandRemoteFileList->Objects[Index]);
  2283. CustomCommand(RemoteFileList, Command, LocalFileList);
  2284. }
  2285. }
  2286. __finally
  2287. {
  2288. delete LocalFileList;
  2289. delete RemoteFileList;
  2290. }
  2291. }
  2292. //---------------------------------------------------------------------------
  2293. void __fastcall TCustomScpExplorerForm::CustomCommandMenu(
  2294. TAction * Action, TStrings * LocalFileList, TStrings * RemoteFileList)
  2295. {
  2296. delete FCustomCommandLocalFileList;
  2297. delete FCustomCommandRemoteFileList;
  2298. // takeover ownership,
  2299. // the lists must survive the MenuPopup as OnClick occurs only after it exits
  2300. FCustomCommandLocalFileList = LocalFileList;
  2301. FCustomCommandRemoteFileList = RemoteFileList;
  2302. TButton * Button = dynamic_cast<TButton *>(Action->ActionComponent);
  2303. if (Button != NULL)
  2304. {
  2305. FCustomCommandMenu->Items->Clear();
  2306. NonVisualDataModule->CreateCustomCommandsMenu(FCustomCommandMenu->Items, false, false, ccltBoth, NULL);
  2307. MenuPopup(FCustomCommandMenu, Button);
  2308. }
  2309. else
  2310. {
  2311. NonVisualDataModule->CreateCustomCommandsMenu(Action, false, ccltBoth);
  2312. }
  2313. }
  2314. //---------------------------------------------------------------------------
  2315. void __fastcall TCustomScpExplorerForm::TerminalCaptureLog(
  2316. const UnicodeString & AddedLine, TCaptureOutputType OutputType)
  2317. {
  2318. DebugAssert(FCapturedLog != NULL);
  2319. if ((OutputType == cotOutput) || (OutputType == cotError))
  2320. {
  2321. FCapturedLog->Add(AddedLine);
  2322. }
  2323. }
  2324. //---------------------------------------------------------------------------
  2325. bool __fastcall TCustomScpExplorerForm::IsFileControl(TObject * Control,
  2326. TOperationSide Side)
  2327. {
  2328. return
  2329. (Side == osOther) &&
  2330. ((Control == DirView(osOther)) || (Control == DriveView(osOther)));
  2331. }
  2332. //---------------------------------------------------------------------------
  2333. void __fastcall TCustomScpExplorerForm::DirViewContextPopupDefaultItem(
  2334. TOperationSide Side, TTBXCustomItem * Item, TDoubleClickAction DoubleClickAction)
  2335. {
  2336. TTBItemOptions O;
  2337. O = Item->Options;
  2338. TCustomDirView * DView = DirView(Side);
  2339. if ((DView->ItemFocused != NULL) &&
  2340. (WinConfiguration->DoubleClickAction == DoubleClickAction) &&
  2341. // when resolving links is disabled, default action is to enter the directory,
  2342. // no matter what DoubleClickAction is configured to
  2343. (IsSideLocalBrowser(Side) || Terminal->ResolvingSymlinks || Terminal->IsEncryptingFiles()) &&
  2344. // Can only Edit files, but can Open/Copy even directories
  2345. ((DoubleClickAction != dcaEdit) ||
  2346. !DView->ItemIsDirectory(DView->ItemFocused)))
  2347. {
  2348. Item->Options = O << tboDefault;
  2349. }
  2350. else
  2351. {
  2352. Item->Options = O >> tboDefault;
  2353. }
  2354. }
  2355. //---------------------------------------------------------------------------
  2356. void __fastcall TCustomScpExplorerForm::DirViewContextPopup(
  2357. TOperationSide Side, Byte PopupComponent, const TPoint &MousePos)
  2358. {
  2359. TCustomDirView * DView = DirView(Side);
  2360. TListItem * Item = DView->ItemFocused;
  2361. if ((DView->GetItemAt(MousePos.x, MousePos.y) == Item) &&
  2362. EnableFocusedOperation[Side])
  2363. {
  2364. TPoint ClientPoint;
  2365. ClientPoint = ((MousePos.x < 0) && (MousePos.y < 0)) ?
  2366. TPoint(0, 0) : MousePos;
  2367. FLastContextPopupScreenPoint = DView->ClientToScreen(ClientPoint);
  2368. reinterpret_cast<TPopupMenu*>(GetComponent(PopupComponent))->Popup(
  2369. FLastContextPopupScreenPoint.x, FLastContextPopupScreenPoint.y);
  2370. }
  2371. }
  2372. //---------------------------------------------------------------------------
  2373. void __fastcall TCustomScpExplorerForm::RemoteDirViewContextPopup(
  2374. TObject * /*Sender*/, const TPoint &MousePos, bool &Handled)
  2375. {
  2376. DirViewContextPopupDefaultItem(osRemote, NonVisualDataModule->RemoteOpenMenuItem, dcaOpen);
  2377. DirViewContextPopupDefaultItem(osRemote, NonVisualDataModule->RemoteEditMenuItem, dcaEdit);
  2378. DirViewContextPopupDefaultItem(osRemote, NonVisualDataModule->RemoteCopyMenuItem, dcaCopy);
  2379. DirViewContextPopup(osRemote, fcRemotePopup, MousePos);
  2380. Handled = true;
  2381. }
  2382. //---------------------------------------------------------------------------
  2383. void __fastcall TCustomScpExplorerForm::ReloadLocalDirectory(const UnicodeString Directory)
  2384. {
  2385. }
  2386. //---------------------------------------------------------------------------
  2387. void __fastcall TCustomScpExplorerForm::BatchStart(void *& /*Storage*/)
  2388. {
  2389. DebugAssert(FErrorList == NULL);
  2390. if (WinConfiguration->ContinueOnError)
  2391. {
  2392. FErrorList = new TStringList();
  2393. Configuration->Usage->Inc(L"ContinuationsOnError");
  2394. }
  2395. NonVisualDataModule->StartBusy();
  2396. }
  2397. //---------------------------------------------------------------------------
  2398. void __fastcall TCustomScpExplorerForm::BatchEnd(void * /*Storage*/)
  2399. {
  2400. NonVisualDataModule->EndBusy();
  2401. if (FErrorList)
  2402. {
  2403. HandleErrorList(FErrorList);
  2404. }
  2405. }
  2406. //---------------------------------------------------------------------------
  2407. void __fastcall TCustomScpExplorerForm::UpdateCopyParamCounters(
  2408. const TCopyParamType & CopyParam)
  2409. {
  2410. if (!CopyParam.IncludeFileMask.Masks.IsEmpty())
  2411. {
  2412. Configuration->Usage->Inc(L"FileMaskUses");
  2413. }
  2414. if (IsEffectiveFileNameMask(CopyParam.FileMask))
  2415. {
  2416. Configuration->Usage->Inc(L"OperationMaskUses");
  2417. }
  2418. }
  2419. //---------------------------------------------------------------------------
  2420. bool __fastcall TCustomScpExplorerForm::ExecuteCopyMoveFileOperation(
  2421. TFileOperation Operation, TOperationSide Side, TStrings * FileList, bool NoConfirmation, void * AParam)
  2422. {
  2423. DebugAssert(!IsLocalBrowserMode());
  2424. TTransferDirection Direction = (Side == osLocal ? tdToRemote : tdToLocal);
  2425. TTransferType Type = (Operation == foCopy ? ttCopy : ttMove);
  2426. TTransferOperationParam DefaultParam;
  2427. TTransferOperationParam & Param = (AParam != NULL) ? *static_cast<TTransferOperationParam *>(AParam) : DefaultParam;
  2428. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  2429. switch (Param.Queue)
  2430. {
  2431. case asOn:
  2432. CopyParam.Queue = true;
  2433. break;
  2434. case asOff:
  2435. CopyParam.Queue = false;
  2436. break;
  2437. case asAuto:
  2438. default:
  2439. // keep default
  2440. break;
  2441. }
  2442. bool Result =
  2443. CopyParamDialog(Direction, Type, Param.Temp, FileList, Param.TargetDirectory, CopyParam, !NoConfirmation, Param.DragDrop, Param.Options);
  2444. if (Result)
  2445. {
  2446. DebugAssert(Terminal);
  2447. bool SelectionRestored = false;
  2448. TCustomDirView * DView = NULL;
  2449. if (HasDirView[Side])
  2450. {
  2451. DView = DirView(Side);
  2452. DView->SaveSelection();
  2453. DView->SaveSelectedNames();
  2454. }
  2455. UpdateCopyParamCounters(CopyParam);
  2456. std::unique_ptr<TStringList> TransferResumeList(new TStringList());
  2457. DebugAssert(FTransferResumeList == NULL);
  2458. FTransferResumeList = Terminal->IsCapable[fcMoveToQueue] ? TransferResumeList.get() : NULL;
  2459. FMoveToQueue = false;
  2460. int Params = FLAGMASK(Operation == foMove, cpDelete);
  2461. try
  2462. {
  2463. TStrings * PermanentFileList;
  2464. std::unique_ptr<TStrings> PermanentFileListOwner;
  2465. try
  2466. {
  2467. if (Side == osLocal)
  2468. {
  2469. PermanentFileList = FileList;
  2470. Params |= FLAGMASK(Param.Temp, cpTemporary);
  2471. CopyParam.IncludeFileMask.SetRoots(FileList, Param.TargetDirectory);
  2472. Terminal->CopyToRemote(FileList, Param.TargetDirectory, &CopyParam, Params, NULL);
  2473. if (Operation == foMove)
  2474. {
  2475. ReloadLocalDirectory();
  2476. if (DView != NULL)
  2477. {
  2478. DView->RestoreSelection();
  2479. }
  2480. SelectionRestored = true;
  2481. }
  2482. }
  2483. else
  2484. {
  2485. // Clone the file list as it may refer to current directory files,
  2486. // which get destroyed, when the source directory is reloaded after foMove operation.
  2487. // We should actually clone the file list for whole ExecuteFileOperation to protect against reloads.
  2488. // But for a hotfix, we are not going to do such a big change.
  2489. PermanentFileListOwner.reset(TRemoteFileList::CloneStrings(FileList));
  2490. PermanentFileList = PermanentFileListOwner.get();
  2491. try
  2492. {
  2493. CopyParam.IncludeFileMask.SetRoots(Param.TargetDirectory, FileList);
  2494. Terminal->CopyToLocal(FileList, Param.TargetDirectory, &CopyParam, Params, NULL);
  2495. }
  2496. __finally
  2497. {
  2498. if (Operation == foMove)
  2499. {
  2500. if (DView != NULL)
  2501. {
  2502. DView->RestoreSelection();
  2503. }
  2504. SelectionRestored = true;
  2505. }
  2506. ReloadLocalDirectory(Param.TargetDirectory);
  2507. }
  2508. }
  2509. // When the transfer batch is aborted between individual file transfers, the Abort is not called.
  2510. if (FMoveToQueue)
  2511. {
  2512. Abort();
  2513. }
  2514. }
  2515. catch (EAbort &)
  2516. {
  2517. if (FMoveToQueue)
  2518. {
  2519. Params |= (CopyParam.QueueNoConfirmation ? cpNoConfirmation : 0);
  2520. DebugAssert(CopyParam.TransferSkipList == NULL);
  2521. DebugAssert(CopyParam.TransferResumeFile.IsEmpty());
  2522. if (TransferResumeList->Count > 0)
  2523. {
  2524. CopyParam.TransferResumeFile = TransferResumeList->Strings[TransferResumeList->Count - 1];
  2525. TransferResumeList->Delete(TransferResumeList->Count - 1);
  2526. }
  2527. CopyParam.TransferSkipList = TransferResumeList.release();
  2528. // not really needed, just to keep it consistent with TransferResumeList
  2529. FTransferResumeList = NULL;
  2530. FMoveToQueue = false;
  2531. Configuration->Usage->Inc("MovesToBackground");
  2532. AddQueueItem(Queue, Direction, PermanentFileList, Param.TargetDirectory, CopyParam, Params);
  2533. ClearTransferSourceSelection(Direction);
  2534. }
  2535. throw;
  2536. }
  2537. }
  2538. __finally
  2539. {
  2540. if (!SelectionRestored && (DView != NULL))
  2541. {
  2542. DView->DiscardSavedSelection();
  2543. }
  2544. FTransferResumeList = NULL;
  2545. }
  2546. }
  2547. return Result;
  2548. }
  2549. //---------------------------------------------------------------------------
  2550. bool __fastcall TCustomScpExplorerForm::ExecuteDeleteFileOperation(
  2551. TOperationSide Side, TStrings * FileList, void * Param)
  2552. {
  2553. DebugAssert(FileList->Count);
  2554. // We deliberately do not toggle alternative flag (Param), but use OR,
  2555. // because the Param is set only when command is invoked using Shift-Del/F8 keyboard
  2556. // shortcut of CurrentDeleteAlternativeAction
  2557. bool Alternative =
  2558. bool(Param) || UseAlternativeFunction();
  2559. bool Recycle;
  2560. if (IsSideLocalBrowser(Side))
  2561. {
  2562. Recycle = (WinConfiguration->DeleteToRecycleBin != Alternative);
  2563. }
  2564. else
  2565. {
  2566. Recycle =
  2567. (Terminal->SessionData->DeleteToRecycleBin != Alternative) &&
  2568. !Terminal->SessionData->RecycleBinPath.IsEmpty() &&
  2569. !Terminal->IsRecycledFile(FileList->Strings[0]);
  2570. }
  2571. bool Result = !(Recycle ? WinConfiguration->ConfirmRecycling : WinConfiguration->ConfirmDeleting);
  2572. if (!Result)
  2573. {
  2574. UnicodeString Query;
  2575. if (FileList->Count == 1)
  2576. {
  2577. if (IsSideLocalBrowser(Side))
  2578. {
  2579. Query = ExtractFileName(FileList->Strings[0]);
  2580. }
  2581. else
  2582. {
  2583. Query = UnixExtractFileName(FileList->Strings[0]);
  2584. }
  2585. Query = FMTLOAD((Recycle ? CONFIRM_RECYCLE_FILE : CONFIRM_DELETE_FILE), (Query));
  2586. }
  2587. else
  2588. {
  2589. Query = FMTLOAD((Recycle ? CONFIRM_RECYCLE_FILES : CONFIRM_DELETE_FILES), (FileList->Count));
  2590. }
  2591. TMessageParams Params(mpNeverAskAgainCheck);
  2592. Params.ImageName = L"Delete file";
  2593. unsigned int Answer =
  2594. MessageDialog(MainInstructions(Query), qtConfirmation, qaOK | qaCancel, HELP_DELETE_FILE, &Params);
  2595. if (Answer == qaNeverAskAgain)
  2596. {
  2597. Result = true;
  2598. if (Recycle)
  2599. {
  2600. WinConfiguration->ConfirmRecycling = false;
  2601. }
  2602. else
  2603. {
  2604. WinConfiguration->ConfirmDeleting = false;
  2605. }
  2606. }
  2607. else
  2608. {
  2609. Result = (Answer == qaOK);
  2610. }
  2611. }
  2612. if (Result)
  2613. {
  2614. DeleteFiles(Side, FileList, FLAGMASK(Alternative, dfAlternative));
  2615. }
  2616. return Result;
  2617. }
  2618. //---------------------------------------------------------------------------
  2619. bool __fastcall TCustomScpExplorerForm::ExecuteFileOperation(TFileOperation Operation,
  2620. TOperationSide Side, TStrings * FileList, bool NoConfirmation, void * Param)
  2621. {
  2622. TAutoBatch AutoBatch(this);
  2623. bool Result;
  2624. if (TFileOperationProgressType::IsTransferOperation(Operation))
  2625. {
  2626. Result = ExecuteCopyMoveFileOperation(Operation, Side, FileList, NoConfirmation, Param);
  2627. }
  2628. else if (Operation == foRename)
  2629. {
  2630. DebugAssert(DirView(Side)->ItemFocused);
  2631. DirView(Side)->ItemFocused->EditCaption();
  2632. Result = true;
  2633. }
  2634. else if (Operation == foDelete)
  2635. {
  2636. Result = ExecuteDeleteFileOperation(Side, FileList, Param);
  2637. }
  2638. else if (Operation == foSetProperties)
  2639. {
  2640. DirView(osRemote)->SaveSelectedNames();
  2641. Result = SetProperties(Side, FileList);
  2642. }
  2643. else if (Operation == foCustomCommand)
  2644. {
  2645. DebugAssert(Param);
  2646. DebugAssert(Side == osRemote); // Side carries no meaning for foCustomCommand
  2647. DirView(osRemote)->SaveSelectedNames();
  2648. const TCustomCommandType * Command = static_cast<const TCustomCommandType*>(Param);
  2649. CustomCommand(FileList, *Command, NULL);
  2650. Result = true;
  2651. }
  2652. else if ((Operation == foRemoteMove) || (Operation == foRemoteCopy))
  2653. {
  2654. DebugAssert(!IsSideLocalBrowser(Side));
  2655. Result = RemoteTransferFiles(FileList, NoConfirmation,
  2656. (Operation == foRemoteMove), reinterpret_cast<TManagedTerminal *>(Param));
  2657. }
  2658. else if (Operation == foLock)
  2659. {
  2660. DebugAssert(!IsSideLocalBrowser(Side));
  2661. LockFiles(FileList, true);
  2662. Result = true;
  2663. }
  2664. else if (Operation == foUnlock)
  2665. {
  2666. DebugAssert(!IsSideLocalBrowser(Side));
  2667. LockFiles(FileList, false);
  2668. Result = true;
  2669. }
  2670. else
  2671. {
  2672. DebugFail();
  2673. }
  2674. return Result;
  2675. }
  2676. //---------------------------------------------------------------------------
  2677. TOperationSide __fastcall TCustomScpExplorerForm::GetSide(TOperationSide Side)
  2678. {
  2679. if (Side == osCurrent)
  2680. {
  2681. Side = FCurrentSide;
  2682. }
  2683. return Side;
  2684. }
  2685. //---------------------------------------------------------------------------
  2686. bool __fastcall TCustomScpExplorerForm::ExecuteFileOperation(TFileOperation Operation,
  2687. TOperationSide Side, bool OnFocused, bool NoConfirmation, void * Param)
  2688. {
  2689. Side = GetSide(Side);
  2690. bool FullPath = IsSideLocalBrowser(Side);
  2691. TCustomDirView * ADirView = DirView(Side);
  2692. std::unique_ptr<TStringList> FileList(new TStringList());
  2693. ADirView->CreateFileList(OnFocused, FullPath, FileList.get());
  2694. bool ReloadProperties =
  2695. (Side == osRemote) && (Operation == foSetProperties) &&
  2696. DebugAlwaysTrue(HasActiveTerminal()) && Terminal->IsCapable[fcLoadingAdditionalProperties];
  2697. bool Result = ExecuteFileOperation(Operation, Side, FileList.get(), NoConfirmation, Param);
  2698. if (Result && ReloadProperties)
  2699. {
  2700. std::unique_ptr<TStringList> FileList2(new TStringList());
  2701. FileList->CaseSensitive = true;
  2702. FileList->Sort();
  2703. for (int Index = 0; Index < ADirView->Items->Count; Index++)
  2704. {
  2705. TListItem * Item = ADirView->Items->Item[Index];
  2706. UnicodeString FileName = ADirView->ItemFileName(Item);
  2707. int Index2;
  2708. if (FileList->Find(FileName, Index2))
  2709. {
  2710. FileList2->AddObject(FileName, ADirView->ItemData(Item));
  2711. }
  2712. }
  2713. LoadFilesProperties(FileList2.get());
  2714. }
  2715. return Result;
  2716. }
  2717. //---------------------------------------------------------------------------
  2718. void __fastcall TCustomScpExplorerForm::ExecuteFileOperationCommand(
  2719. TFileOperation Operation, TOperationSide Side, bool OnFocused,
  2720. bool NoConfirmation, void * Param)
  2721. {
  2722. if (ExecuteFileOperation(Operation, Side, OnFocused, NoConfirmation, Param))
  2723. {
  2724. if (TFileOperationProgressType::IsTransferOperation(Operation))
  2725. {
  2726. DebugAssert(!IsLocalBrowserMode());
  2727. if (GetSide(Side) == osLocal)
  2728. {
  2729. Configuration->Usage->Inc(L"UploadsCommand");
  2730. }
  2731. else
  2732. {
  2733. Configuration->Usage->Inc(L"DownloadsCommand");
  2734. }
  2735. }
  2736. }
  2737. }
  2738. //---------------------------------------------------------------------------
  2739. void __fastcall TCustomScpExplorerForm::ExecuteCopyOperationCommand(
  2740. TOperationSide Side, bool OnFocused, unsigned int Flags)
  2741. {
  2742. TTransferOperationParam Param;
  2743. if ((WinConfiguration->Interface != ifCommander) ||
  2744. WinConfiguration->ScpCommander.ExplorerKeyboardShortcuts)
  2745. {
  2746. Flags &= ~cocShortCutHint;
  2747. }
  2748. TCustomDirView * DView = DirView(Side);
  2749. Param.Options =
  2750. FLAGMASK(FLAGSET(Flags, cocShortCutHint), coShortCutHint) |
  2751. FLAGMASK(SelectedAllFilesInDirView(DView), coAllFiles);
  2752. if (FLAGSET(Flags, cocQueue))
  2753. {
  2754. Param.Queue = asOn;
  2755. }
  2756. else if (FLAGSET(Flags, cocNonQueue))
  2757. {
  2758. Param.Queue = asOff;
  2759. }
  2760. ExecuteFileOperationCommand(foCopy, Side, OnFocused, false, &Param);
  2761. }
  2762. //---------------------------------------------------------------------------
  2763. void __fastcall TCustomScpExplorerForm::HandleErrorList(TStringList *& ErrorList)
  2764. {
  2765. int Count = ErrorList->Count;
  2766. try
  2767. {
  2768. if (ErrorList->Count)
  2769. {
  2770. UnicodeString Message = MainInstructions(FMTLOAD(ERROR_LIST_COUNT, (ErrorList->Count)));
  2771. if (MessageDialog(Message, qtError,
  2772. qaOK | qaCancel, HELP_NONE) == qaOK)
  2773. {
  2774. unsigned int Answer;
  2775. int Index = 0;
  2776. do
  2777. {
  2778. DebugAssert(Index >= 0 && Index < Count);
  2779. TQueryButtonAlias Aliases[2];
  2780. Aliases[0].Button = qaYes;
  2781. Aliases[0].Alias = LoadStr(PREV_BUTTON);
  2782. Aliases[1].Button = qaNo;
  2783. Aliases[1].Alias = LoadStr(NEXT_BUTTON);
  2784. TMessageParams Params;
  2785. Params.Aliases = Aliases;
  2786. Params.AliasesCount = LENOF(Aliases);
  2787. int No = Index + 1;
  2788. UnicodeString Message = ErrorList->Strings[Index];
  2789. UnicodeString AMainInstructions;
  2790. UnicodeString Details;
  2791. if (ExtractMainInstructions(Message, AMainInstructions))
  2792. {
  2793. Details = Message;
  2794. Message = AMainInstructions;
  2795. }
  2796. Message = MainInstructions(FMTLOAD(ERROR_LIST_NUMBER, (No, Count, Message))) + Details;
  2797. Answer = MoreMessageDialog(
  2798. Message,
  2799. dynamic_cast<TStrings *>(ErrorList->Objects[Index]), qtError,
  2800. (Index ? qaYes : 0) | (Index < Count - 1 ? qaNo : 0) |
  2801. qaOK, HELP_NONE, &Params);
  2802. if (Answer == qaNo)
  2803. {
  2804. Index++;
  2805. }
  2806. if (Answer == qaYes)
  2807. {
  2808. Index--;
  2809. }
  2810. }
  2811. while (Answer != qaOK);
  2812. }
  2813. }
  2814. }
  2815. __finally
  2816. {
  2817. TStrings * List = ErrorList;
  2818. ErrorList = NULL;
  2819. for (int i = 0; i < Count; i++)
  2820. {
  2821. delete List->Objects[i];
  2822. }
  2823. delete List;
  2824. }
  2825. }
  2826. //---------------------------------------------------------------------------
  2827. void __fastcall TCustomScpExplorerForm::ExecuteRemoteFile(
  2828. const UnicodeString & FullFileName, TRemoteFile * File, TExecuteFileBy ExecuteFileBy)
  2829. {
  2830. // needed for checking filemasks, as there's no directory object
  2831. // associated with the file object
  2832. File->FullFileName = FullFileName;
  2833. TFileMasks::TParams MaskParams;
  2834. MaskParams.Size = File->Size;
  2835. MaskParams.Modification = File->Modification;
  2836. ExecuteFile(osRemote, ExecuteFileBy, NULL, FullFileName, File, MaskParams);
  2837. }
  2838. //---------------------------------------------------------------------------
  2839. void __fastcall TCustomScpExplorerForm::EditNew(TOperationSide Side)
  2840. {
  2841. DebugAssert(!WinConfiguration->DisableOpenEdit);
  2842. Side = GetSide(Side);
  2843. TCustomDirView * DView = DirView(Side);
  2844. TListItem * FocusedItem = DView->ItemFocused;
  2845. UnicodeString Name;
  2846. if ((FocusedItem != NULL) && !DView->ItemIsDirectory(FocusedItem))
  2847. {
  2848. Name = DView->ItemFileName(FocusedItem);
  2849. }
  2850. else
  2851. {
  2852. Name = LoadStr(NEW_FILE);
  2853. }
  2854. UnicodeString Names = Name;
  2855. std::unique_ptr<TStrings> History(CloneStrings(CustomWinConfiguration->History[L"EditFile"]));
  2856. if (InputDialog(LoadStr(EDIT_FILE_CAPTION), LoadStr(EDIT_FILE_PROMPT), Names,
  2857. HELP_EDIT_NEW, History.get(), true, NULL, true, 400))
  2858. {
  2859. while (!Names.IsEmpty())
  2860. {
  2861. Name = CutToChar(Names, FileMasksDelimiters[1], false);
  2862. CustomWinConfiguration->History[L"EditFile"] = History.get();
  2863. UnicodeString TargetFileName;
  2864. UnicodeString LocalFileName;
  2865. UnicodeString RootTempDir;
  2866. UnicodeString TempDir;
  2867. UnicodeString RemoteDirectory;
  2868. bool ExistingFile = false;
  2869. if (!IsSideLocalBrowser(Side))
  2870. {
  2871. Name = AbsolutePath(FTerminal->CurrentDirectory, Name);
  2872. TRemoteFile * File = NULL;
  2873. if (FTerminal->FileExists(Name, &File) &&
  2874. !File->IsDirectory)
  2875. {
  2876. try
  2877. {
  2878. ExecuteRemoteFile(Name, File, efDefaultEditor);
  2879. ExistingFile = true;
  2880. }
  2881. __finally
  2882. {
  2883. delete File;
  2884. }
  2885. }
  2886. if (!ExistingFile)
  2887. {
  2888. RemoteDirectory = UnixExtractFilePath(Name);
  2889. TemporaryDirectoryForRemoteFiles(
  2890. RemoteDirectory, GUIConfiguration->CurrentCopyParam, TempDir, RootTempDir);
  2891. TargetFileName = UnixExtractFileName(Name);
  2892. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  2893. LocalFileName = TempDir +
  2894. // We probably do not want to trim the VMS version here
  2895. FTerminal->ChangeFileName(&CopyParam, TargetFileName, osRemote, false);
  2896. }
  2897. }
  2898. else
  2899. {
  2900. if (ExtractFilePath(Name).IsEmpty())
  2901. {
  2902. LocalFileName = IncludeTrailingBackslash(DView->PathName) + Name;
  2903. }
  2904. else
  2905. {
  2906. LocalFileName = ExpandFileName(Name);
  2907. }
  2908. TargetFileName = ExtractFileName(Name);
  2909. }
  2910. if (!ExistingFile)
  2911. {
  2912. bool NewFile = !FileExists(ApiPath(LocalFileName));
  2913. if (NewFile)
  2914. {
  2915. int File = FileCreate(ApiPath(LocalFileName));
  2916. if (File < 0)
  2917. {
  2918. if (!RootTempDir.IsEmpty())
  2919. {
  2920. RecursiveDeleteFile(ExcludeTrailingBackslash(RootTempDir), false);
  2921. }
  2922. throw Exception(FMTLOAD(CREATE_FILE_ERROR, (LocalFileName)));
  2923. }
  2924. else
  2925. {
  2926. FileClose(File);
  2927. }
  2928. }
  2929. TExecuteFileBy ExecuteFileBy = efDefaultEditor;
  2930. const TEditorData * ExternalEditor = NULL;
  2931. TFileMasks::TParams MaskParams; // size not known
  2932. ExecuteFileNormalize(ExecuteFileBy, ExternalEditor, TargetFileName,
  2933. false, MaskParams);
  2934. CustomExecuteFile(Side, ExecuteFileBy, LocalFileName, TargetFileName,
  2935. ExternalEditor, RootTempDir, RemoteDirectory, NewFile);
  2936. }
  2937. }
  2938. }
  2939. }
  2940. //---------------------------------------------------------------------------
  2941. bool __fastcall TCustomScpExplorerForm::RemoteExecuteForceText(
  2942. TExecuteFileBy ExecuteFileBy, const TEditorData * ExternalEditor)
  2943. {
  2944. DebugAssert((ExecuteFileBy == efExternalEditor) ==
  2945. ((ExternalEditor != NULL) && (ExternalEditor->Editor == edExternal)));
  2946. DebugAssert(ExecuteFileBy != efDefaultEditor);
  2947. return
  2948. ((ExecuteFileBy == efInternalEditor)) ||
  2949. ((ExecuteFileBy == efExternalEditor) && ExternalEditor->ExternalEditorText);
  2950. }
  2951. //---------------------------------------------------------------------------
  2952. void __fastcall TCustomScpExplorerForm::CustomExecuteFile(TOperationSide Side,
  2953. TExecuteFileBy ExecuteFileBy, UnicodeString FileName, UnicodeString OriginalFileName,
  2954. const TEditorData * ExternalEditor, UnicodeString LocalRootDirectory,
  2955. UnicodeString RemoteDirectory, bool NewFile)
  2956. {
  2957. DebugAssert(!WinConfiguration->DisableOpenEdit);
  2958. DebugAssert((ExecuteFileBy == efExternalEditor) ==
  2959. ((ExternalEditor != NULL) && (ExternalEditor->Editor == edExternal)));
  2960. DebugAssert(ExecuteFileBy != efDefaultEditor);
  2961. Side = GetSide(Side);
  2962. std::unique_ptr<TEditedFileData> Data(new TEditedFileData);
  2963. if (!IsSideLocalBrowser(Side))
  2964. {
  2965. Data->Terminal = Terminal;
  2966. Data->Queue = Queue;
  2967. Data->SessionData = CloneCurrentSessionData();
  2968. Data->ForceText = RemoteExecuteForceText(ExecuteFileBy, ExternalEditor);
  2969. Data->RemoteDirectory = RemoteDirectory;
  2970. Data->SessionName = Terminal->SessionData->SessionName;
  2971. Data->LocalRootDirectory = LocalRootDirectory;
  2972. Data->OriginalFileName = OriginalFileName;
  2973. Data->Command = L""; // will be changed later for external editor
  2974. }
  2975. if (ExecuteFileBy == efInternalEditor)
  2976. {
  2977. if (!IsSideLocalBrowser(Side))
  2978. {
  2979. UnicodeString Caption = UnixIncludeTrailingBackslash(RemoteDirectory) + OriginalFileName +
  2980. L" - " + Terminal->SessionData->SessionName;
  2981. TForm * Editor;
  2982. try
  2983. {
  2984. Editor =
  2985. ShowEditorForm(
  2986. FileName, this,
  2987. FEditorManager->FileChanged, FEditorManager->FileReload, FEditorManager->FileClosed,
  2988. SaveAllInternalEditors, AnyInternalEditorModified,
  2989. Caption, StandaloneOperation, SessionColor, Terminal->SessionData->InternalEditorEncoding, NewFile);
  2990. }
  2991. catch(...)
  2992. {
  2993. if (!LocalRootDirectory.IsEmpty())
  2994. {
  2995. RecursiveDeleteFile(ExcludeTrailingBackslash(LocalRootDirectory), false);
  2996. }
  2997. throw;
  2998. }
  2999. FEditorManager->AddFileInternal(FileName, Data.release(), Editor);
  3000. }
  3001. else
  3002. {
  3003. DebugAssert(!StandaloneOperation);
  3004. TForm * Editor =
  3005. ShowEditorForm(FileName, this, NULL, NULL, LocalEditorClosed,
  3006. SaveAllInternalEditors, AnyInternalEditorModified,
  3007. L"", false, SessionColor, -1, NewFile);
  3008. FLocalEditors->Add(Editor);
  3009. }
  3010. }
  3011. else
  3012. {
  3013. HANDLE Process;
  3014. if (ExecuteFileBy == efExternalEditor)
  3015. {
  3016. UnicodeString Program, Params, Dir;
  3017. Data->Command = ExternalEditor->ExternalEditor;
  3018. ReformatFileNameCommand(Data->Command);
  3019. SplitCommand(Data->Command, Program, Params, Dir);
  3020. Params = ExpandFileNameCommand(Params, FileName);
  3021. Program = ExpandEnvironmentVariables(Program);
  3022. if (!ExecuteShell(Program, Params, Process))
  3023. {
  3024. throw EOSExtException(FMTLOAD(EDITOR_ERROR, (Program)));
  3025. }
  3026. }
  3027. else
  3028. {
  3029. DebugAssert(!IsSideLocalBrowser(Side));
  3030. if (!ExecuteShell(FileName, L"", Process))
  3031. {
  3032. throw EOSExtException(FMTLOAD(EXECUTE_FILE_ERROR, (FileName)));
  3033. }
  3034. }
  3035. if (IsSideLocalBrowser(Side) ||
  3036. ((ExecuteFileBy == efShell) &&
  3037. !WinConfiguration->Editor.SDIShellEditor) ||
  3038. ((ExecuteFileBy == efExternalEditor) &&
  3039. !ExternalEditor->SDIExternalEditor))
  3040. {
  3041. // no need for handle
  3042. if (Process != NULL)
  3043. {
  3044. DebugCheck(CloseHandle(Process));
  3045. }
  3046. Process = INVALID_HANDLE_VALUE;
  3047. }
  3048. else
  3049. {
  3050. if (Process == NULL)
  3051. {
  3052. throw ExtException(LoadStr(OPEN_FILE_NO_PROCESS2), L"", HELP_OPEN_FILE_NO_PROCESS);
  3053. }
  3054. }
  3055. if (!IsSideLocalBrowser(Side))
  3056. {
  3057. FEditorManager->AddFileExternal(FileName, Data.release(), Process);
  3058. }
  3059. }
  3060. }
  3061. //---------------------------------------------------------------------------
  3062. void __fastcall TCustomScpExplorerForm::SaveInternalEditor(
  3063. const UnicodeString /*FileName*/, TEditedFileData * /*Data*/, TObject * Token,
  3064. void * /*Arg*/)
  3065. {
  3066. if (Token != NULL)
  3067. {
  3068. EditorFormFileSave(static_cast<TForm *>(Token));
  3069. }
  3070. }
  3071. //---------------------------------------------------------------------------
  3072. void __fastcall TCustomScpExplorerForm::SaveAllInternalEditors(TObject * /*Sender*/)
  3073. {
  3074. for (int Index = 0; Index < FLocalEditors->Count; Index++)
  3075. {
  3076. EditorFormFileSave(static_cast<TForm *>(FLocalEditors->Items[Index]));
  3077. }
  3078. FEditorManager->ProcessFiles(SaveInternalEditor, NULL);
  3079. }
  3080. //---------------------------------------------------------------------------
  3081. void __fastcall TCustomScpExplorerForm::InternalEditorModified(
  3082. const UnicodeString /*FileName*/, TEditedFileData * /*Data*/, TObject * Token,
  3083. void * Arg)
  3084. {
  3085. if ((Token != NULL) &&
  3086. IsEditorFormModified(static_cast<TForm *>(Token)))
  3087. {
  3088. *static_cast<bool *>(Arg) = true;
  3089. }
  3090. }
  3091. //---------------------------------------------------------------------------
  3092. void __fastcall TCustomScpExplorerForm::AnyInternalEditorModified(
  3093. TObject * /*Sender*/, bool & Modified)
  3094. {
  3095. for (int Index = 0; !Modified && (Index < FLocalEditors->Count); Index++)
  3096. {
  3097. if (IsEditorFormModified(static_cast<TForm *>(FLocalEditors->Items[Index])))
  3098. {
  3099. Modified = true;
  3100. }
  3101. }
  3102. if (!Modified)
  3103. {
  3104. FEditorManager->ProcessFiles(InternalEditorModified, &Modified);
  3105. }
  3106. }
  3107. //---------------------------------------------------------------------------
  3108. void __fastcall TCustomScpExplorerForm::LocalEditorClosed(TObject * Sender, bool /*Forced*/)
  3109. {
  3110. DebugCheck(FLocalEditors->Extract(Sender) >= 0);
  3111. }
  3112. //---------------------------------------------------------------------------
  3113. void __fastcall TCustomScpExplorerForm::TemporaryDirectoryForRemoteFiles(
  3114. UnicodeString RemoteDirectory, TCopyParamType CopyParam,
  3115. UnicodeString & Result, UnicodeString & RootDirectory)
  3116. {
  3117. if (!WinConfiguration->TemporaryDirectoryDeterministic)
  3118. {
  3119. RootDirectory = IncludeTrailingBackslash(WinConfiguration->TemporaryDir());
  3120. Result = RootDirectory;
  3121. }
  3122. else
  3123. {
  3124. RootDirectory = L"";
  3125. Result = WinConfiguration->ExpandedTemporaryDirectory();
  3126. Result = IncludeTrailingBackslash(Result);
  3127. }
  3128. if (WinConfiguration->TemporaryDirectoryAppendSession)
  3129. {
  3130. Result = IncludeTrailingBackslash(Result + MakeValidFileName(Terminal->SessionData->SessionName));
  3131. }
  3132. if (WinConfiguration->TemporaryDirectoryAppendPath)
  3133. {
  3134. if (!RemoteDirectory.IsEmpty() && (RemoteDirectory[1] == L'/'))
  3135. {
  3136. RemoteDirectory.Delete(1, 1);
  3137. }
  3138. Result = IncludeTrailingBackslash(Result + CopyParam.ValidLocalPath(FromUnixPath(RemoteDirectory)));
  3139. }
  3140. if (!ForceDirectories(ApiPath(Result)))
  3141. {
  3142. throw EOSExtException(FMTLOAD(CREATE_TEMP_DIR_ERROR, (Result)));
  3143. }
  3144. }
  3145. //---------------------------------------------------------------------------
  3146. void __fastcall TCustomScpExplorerForm::TemporarilyDownloadFiles(
  3147. TStrings * FileList, bool ForceText, UnicodeString & RootTempDir, UnicodeString & TempDir,
  3148. bool GetTargetNames, bool AutoOperation)
  3149. {
  3150. DebugAssert(!IsLocalBrowserMode());
  3151. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  3152. if (ForceText)
  3153. {
  3154. CopyParam.TransferMode = tmAscii;
  3155. }
  3156. TemporaryFileCopyParam(CopyParam);
  3157. if (TempDir.IsEmpty())
  3158. {
  3159. TemporaryDirectoryForRemoteFiles(FTerminal->CurrentDirectory, CopyParam, TempDir, RootTempDir);
  3160. }
  3161. DebugAssert(!FAutoOperation);
  3162. FAutoOperation = AutoOperation;
  3163. Terminal->ExceptionOnFail = true;
  3164. try
  3165. {
  3166. try
  3167. {
  3168. // turn off confirmations, as for MDI editors we may possibly download
  3169. // the same file over
  3170. Terminal->CopyToLocal(
  3171. FileList, TempDir, &CopyParam, cpNoConfirmation | cpTemporary, NULL);
  3172. if (GetTargetNames)
  3173. {
  3174. for (int i = 0; i < FileList->Count; i++)
  3175. {
  3176. FileList->Strings[i] =
  3177. Terminal->ChangeFileName(&CopyParam, UnixExtractFileName(FileList->Strings[i]), osRemote, false);
  3178. }
  3179. }
  3180. }
  3181. catch(...)
  3182. {
  3183. if (!RootTempDir.IsEmpty())
  3184. {
  3185. RecursiveDeleteFile(ExcludeTrailingBackslash(RootTempDir), false);
  3186. }
  3187. throw;
  3188. }
  3189. }
  3190. __finally
  3191. {
  3192. FAutoOperation = false;
  3193. Terminal->ExceptionOnFail = false;
  3194. }
  3195. }
  3196. //---------------------------------------------------------------------------
  3197. void __fastcall TCustomScpExplorerForm::EditorAutoConfig()
  3198. {
  3199. // Do not waste time checking for default editor list the next time,
  3200. // or testing if default editor is not notepad.
  3201. bool TryNextTime = false;
  3202. UnicodeString UsageState;
  3203. if (!WinConfiguration->EditorList->IsDefaultList())
  3204. {
  3205. UsageState = "H";
  3206. }
  3207. else
  3208. {
  3209. UnicodeString Executable;
  3210. UnicodeString ExecutableDescription;
  3211. if (DetectSystemExternalEditor(false, Executable, ExecutableDescription, UsageState, TryNextTime))
  3212. {
  3213. UnicodeString Message =
  3214. FMTLOAD(EDITOR_AUTO_CONFIG2, (ExecutableDescription, ExecutableDescription));
  3215. unsigned int Answer =
  3216. MessageDialog(Message, qtConfirmation, qaOK | qaCancel, HELP_EDITOR_AUTO_CONFIG);
  3217. if (Answer != qaOK)
  3218. {
  3219. UsageState = "R";
  3220. }
  3221. else
  3222. {
  3223. UsageState = "A";
  3224. TEditorData EditorData;
  3225. EditorData.Editor = edExternal;
  3226. EditorData.ExternalEditor = FormatCommand(Executable, L"");
  3227. EditorData.ExternalEditorOptionsAutodetect();
  3228. TEditorList EditorList;
  3229. EditorList = *WinConfiguration->EditorList;
  3230. EditorList.Insert(0, new TEditorPreferences(EditorData));
  3231. WinConfiguration->EditorList = &EditorList;
  3232. }
  3233. }
  3234. }
  3235. WinConfiguration->OfferedEditorAutoConfig = !TryNextTime;
  3236. WinConfiguration->Usage->Set(L"EditorAutoConfig", UsageState);
  3237. }
  3238. //---------------------------------------------------------------------------
  3239. void __fastcall TCustomScpExplorerForm::ExecuteFileNormalize(
  3240. TExecuteFileBy & ExecuteFileBy, const TEditorData *& ExternalEditor,
  3241. const UnicodeString & FileName, bool Local, const TFileMasks::TParams & MaskParams)
  3242. {
  3243. if (ExecuteFileBy == efDefaultEditor)
  3244. {
  3245. if (!WinConfiguration->OfferedEditorAutoConfig)
  3246. {
  3247. EditorAutoConfig();
  3248. }
  3249. const TEditorPreferences * Editor =
  3250. WinConfiguration->DefaultEditorForFile(FileName, Local, MaskParams);
  3251. if ((Editor == NULL) || (Editor->Data->Editor == edInternal))
  3252. {
  3253. ExecuteFileBy = efInternalEditor;
  3254. ExternalEditor = NULL;
  3255. }
  3256. else if (Editor->Data->Editor == edOpen)
  3257. {
  3258. ExecuteFileBy = efShell;
  3259. ExternalEditor = NULL;
  3260. }
  3261. else
  3262. {
  3263. ExecuteFileBy = efExternalEditor;
  3264. ExternalEditor = Editor->Data;
  3265. }
  3266. }
  3267. }
  3268. //---------------------------------------------------------------------------
  3269. void __fastcall TCustomScpExplorerForm::ExecuteFile(TOperationSide Side,
  3270. TExecuteFileBy ExecuteFileBy, const TEditorData * ExternalEditor,
  3271. UnicodeString FullFileName, TObject * Object, const TFileMasks::TParams & MaskParams)
  3272. {
  3273. UnicodeString OriginalFileName;
  3274. UnicodeString LocalRootDirectory;
  3275. UnicodeString RemoteDirectory;
  3276. ExecuteFileNormalize(ExecuteFileBy, ExternalEditor, FullFileName,
  3277. IsSideLocalBrowser(Side), MaskParams);
  3278. UnicodeString Counter;
  3279. UnicodeString LocalFileName;
  3280. bool Handled = false;
  3281. if (!IsSideLocalBrowser(Side))
  3282. {
  3283. // We need to trim VMS version here, so that we use name without version
  3284. // when uploading back to create a new version of the file
  3285. OriginalFileName = FTerminal->GetBaseFileName(UnixExtractFileName(FullFileName));
  3286. RemoteDirectory = UnixExtractFilePath(FullFileName);
  3287. TObject * Token = NULL;
  3288. UnicodeString LocalDirectory;
  3289. if (!FEditorManager->CanAddFile(RemoteDirectory, OriginalFileName,
  3290. Terminal->SessionData->SessionName, Token, LocalRootDirectory,
  3291. LocalDirectory))
  3292. {
  3293. if (Token != NULL)
  3294. {
  3295. TForm * Form = dynamic_cast<TForm *>(Token);
  3296. if (Form->WindowState == wsMinimized)
  3297. {
  3298. ShowWindow(Form->Handle, SW_RESTORE);
  3299. }
  3300. else
  3301. {
  3302. Form->SetFocus();
  3303. }
  3304. Handled = true;
  3305. }
  3306. else
  3307. {
  3308. throw Exception(FMTLOAD(ALREADY_EDITED_EXTERNALLY_OR_UPLOADED, (OriginalFileName)));
  3309. }
  3310. }
  3311. if (!Handled)
  3312. {
  3313. TStringList * FileList1 = new TStringList();
  3314. try
  3315. {
  3316. FileList1->AddObject(FullFileName, Object);
  3317. TemporarilyDownloadFiles(FileList1,
  3318. RemoteExecuteForceText(ExecuteFileBy, ExternalEditor),
  3319. LocalRootDirectory, LocalDirectory, true, true);
  3320. LocalFileName = LocalDirectory + FileList1->Strings[0];
  3321. }
  3322. __finally
  3323. {
  3324. delete FileList1;
  3325. }
  3326. switch (ExecuteFileBy)
  3327. {
  3328. case efShell:
  3329. Counter = "RemoteFilesExecuted";
  3330. break;
  3331. case efInternalEditor:
  3332. Counter = "RemoteFilesOpenedInInternalEditor";
  3333. break;
  3334. case efExternalEditor:
  3335. Counter = "RemoteFilesOpenedInExternalEditor";
  3336. break;
  3337. default:
  3338. DebugFail();
  3339. }
  3340. }
  3341. }
  3342. else
  3343. {
  3344. LocalFileName = FullFileName;
  3345. OriginalFileName = ExtractFileName(FullFileName);
  3346. switch (ExecuteFileBy)
  3347. {
  3348. case efShell:
  3349. Counter = "LocalFilesExecuted";
  3350. break;
  3351. case efInternalEditor:
  3352. Counter = "LocalFilesOpenedInInternalEditor";
  3353. break;
  3354. case efExternalEditor:
  3355. Counter = "LocalFilesOpenedInExternalEditor";
  3356. break;
  3357. default:
  3358. DebugFail();
  3359. }
  3360. }
  3361. if (!Handled)
  3362. {
  3363. Configuration->Usage->Inc(Counter);
  3364. CustomExecuteFile(Side, ExecuteFileBy, LocalFileName, OriginalFileName,
  3365. ExternalEditor, LocalRootDirectory, RemoteDirectory, false);
  3366. }
  3367. }
  3368. //---------------------------------------------------------------------------
  3369. void __fastcall TCustomScpExplorerForm::ExecuteFile(TOperationSide Side,
  3370. TExecuteFileBy ExecuteFileBy, const TEditorData * ExternalEditor,
  3371. bool AllSelected, bool OnFocused)
  3372. {
  3373. DebugAssert(!WinConfiguration->DisableOpenEdit);
  3374. DebugAssert((ExecuteFileBy == efExternalEditor) ==
  3375. ((ExternalEditor != NULL) && (ExternalEditor->Editor == edExternal)));
  3376. Side = GetSide(Side);
  3377. TCustomDirView * DView = DirView(Side);
  3378. TStrings * FileList =
  3379. AllSelected ?
  3380. DView->CreateFileList(OnFocused, IsSideLocalBrowser(Side)) :
  3381. DView->CreateFocusedFileList(IsSideLocalBrowser(Side));
  3382. try
  3383. {
  3384. DebugAssert(AllSelected || (FileList->Count == 1));
  3385. for (int i = 0; i < FileList->Count; i++)
  3386. {
  3387. UnicodeString ListFileName = FileList->Strings[i];
  3388. UnicodeString FileNameOnly =
  3389. !IsSideLocalBrowser(Side) ? UnixExtractFileName(ListFileName) : ExtractFileName(ListFileName);
  3390. TListItem * Item = DView->FindFileItem(FileNameOnly);
  3391. if (!DView->ItemIsDirectory(Item))
  3392. {
  3393. UnicodeString FullFileName;
  3394. if (!IsSideLocalBrowser(Side))
  3395. {
  3396. FullFileName = DirView(Side)->Path + ListFileName;
  3397. }
  3398. else
  3399. {
  3400. FullFileName = ListFileName;
  3401. }
  3402. TFileMasks::TParams MaskParams;
  3403. MaskParams.Size = DView->ItemFileSize(Item);
  3404. TDateTimePrecision Precision;
  3405. MaskParams.Modification = DView->ItemFileTime(Item, Precision);
  3406. ExecuteFile(Side, ExecuteFileBy, ExternalEditor, FullFileName,
  3407. FileList->Objects[i], MaskParams);
  3408. }
  3409. }
  3410. }
  3411. __finally
  3412. {
  3413. delete FileList;
  3414. }
  3415. }
  3416. //---------------------------------------------------------------------------
  3417. void __fastcall TCustomScpExplorerForm::TemporaryFileCopyParam(TCopyParamType & CopyParam)
  3418. {
  3419. CopyParam.FileNameCase = ncNoChange;
  3420. CopyParam.PreserveRights = false;
  3421. CopyParam.PreserveReadOnly = false;
  3422. CopyParam.ReplaceInvalidChars = true;
  3423. CopyParam.IncludeFileMask = TFileMasks();
  3424. CopyParam.NewerOnly = false;
  3425. CopyParam.FileMask = L"";
  3426. }
  3427. //---------------------------------------------------------------------------
  3428. void __fastcall TCustomScpExplorerForm::ExecutedFileChanged(
  3429. const UnicodeString & FileName, TEditedFileData * Data, HANDLE UploadCompleteEvent, bool & Retry)
  3430. {
  3431. TTerminalManager * Manager = TTerminalManager::Instance();
  3432. if (!IsActiveTerminal(Data->Terminal))
  3433. {
  3434. if (!NonVisualDataModule->Busy)
  3435. {
  3436. UnicodeString FileNameOnly = ExtractFileName(FileName);
  3437. UnicodeString EditInactiveSessionReopenAcceptedCounter = L"EditInactiveSessionReopenAccepted";
  3438. UnicodeString EditInactiveSessionReopenRejectedCounter = L"EditInactiveSessionReopenRejected";
  3439. if (Data->Terminal == NULL)
  3440. {
  3441. TTerminal * SameSiteTerminal = Manager->FindActiveTerminalForSite(Data->SessionData);
  3442. if (SameSiteTerminal != NULL)
  3443. {
  3444. UnicodeString Message =
  3445. FMTLOAD(EDIT_SESSION_REATTACH,
  3446. (FileNameOnly, Data->SessionName, FileNameOnly));
  3447. if (MessageDialog(Message, qtConfirmation, qaOK | qaCancel) == qaOK)
  3448. {
  3449. Data->Terminal = Terminal;
  3450. Data->Queue = Manager->FindQueueForTerminal(Terminal);
  3451. Data->SessionName = Terminal->SessionData->SessionName;
  3452. // We might also overwrite session data
  3453. Configuration->Usage->Inc(EditInactiveSessionReopenAcceptedCounter);
  3454. }
  3455. else
  3456. {
  3457. Configuration->Usage->Inc(EditInactiveSessionReopenRejectedCounter);
  3458. Abort();
  3459. }
  3460. }
  3461. }
  3462. // foreground session should reconnect itself
  3463. else if (Terminal != Data->Terminal)
  3464. {
  3465. UnicodeString Message =
  3466. MainInstructions(
  3467. FMTLOAD(EDIT_SESSION_RECONNECT, (Data->SessionName, FileNameOnly)));
  3468. if (MessageDialog(Message, qtConfirmation, qaOK | qaCancel) == qaOK)
  3469. {
  3470. Manager->SetActiveTerminalWithAutoReconnect(Data->Terminal);
  3471. Configuration->Usage->Inc(EditInactiveSessionReopenAcceptedCounter);
  3472. }
  3473. else
  3474. {
  3475. Configuration->Usage->Inc(EditInactiveSessionReopenRejectedCounter);
  3476. Abort();
  3477. }
  3478. }
  3479. }
  3480. if (!IsActiveTerminal(Data->Terminal))
  3481. {
  3482. Configuration->Usage->Inc(L"EditInactiveSession");
  3483. // Prevent this when not idle (!NonVisualDataModule->Busy)?
  3484. throw Exception(FMTLOAD(EDIT_SESSION_CLOSED2,
  3485. (ExtractFileName(FileName), Data->SessionName)));
  3486. }
  3487. }
  3488. TStrings * FileList = new TStringList();
  3489. try
  3490. {
  3491. FileList->Add(FileName);
  3492. // consider using the same settings (preset) as when the file was downloaded
  3493. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  3494. TemporaryFileCopyParam(CopyParam);
  3495. if (Data->ForceText)
  3496. {
  3497. CopyParam.TransferMode = tmAscii;
  3498. }
  3499. // so i do not need to worry if masking algorithm works in all cases
  3500. // ("" means "copy file name", no masking is actually done)
  3501. if (ExtractFileName(FileName) == Data->OriginalFileName)
  3502. {
  3503. CopyParam.FileMask = L"";
  3504. }
  3505. else
  3506. {
  3507. CopyParam.FileMask = DelimitFileNameMask(Data->OriginalFileName);
  3508. }
  3509. int Params = cpNoConfirmation | cpTemporary;
  3510. if (Terminal->IsCapable[fcBackgroundTransfers])
  3511. {
  3512. DebugAssert(Data->Queue != NULL);
  3513. TQueueItem * QueueItem = new TUploadQueueItem(Data->Terminal, FileList,
  3514. Data->RemoteDirectory, &CopyParam, Params, true, false);
  3515. QueueItem->CompleteEvent = UploadCompleteEvent;
  3516. AddQueueItem(Data->Queue, QueueItem, Data->Terminal);
  3517. }
  3518. else
  3519. {
  3520. if (NonVisualDataModule->Busy)
  3521. {
  3522. Retry = true;
  3523. }
  3524. else
  3525. {
  3526. Terminal->CopyToRemote(FileList, Data->RemoteDirectory, &CopyParam, Params, NULL);
  3527. SetEvent(UploadCompleteEvent);
  3528. }
  3529. }
  3530. }
  3531. __finally
  3532. {
  3533. delete FileList;
  3534. }
  3535. }
  3536. //---------------------------------------------------------------------------
  3537. void __fastcall TCustomScpExplorerForm::ExecutedFileReload(
  3538. const UnicodeString FileName, const TEditedFileData * Data)
  3539. {
  3540. // Sanity check, we should not be busy otherwise user would not be able to click Reload button.
  3541. DebugAssert(!NonVisualDataModule->Busy);
  3542. if (!IsActiveTerminal(Data->Terminal))
  3543. {
  3544. throw Exception(FMTLOAD(EDIT_SESSION_CLOSED_RELOAD,
  3545. (ExtractFileName(FileName), Data->SessionName)));
  3546. }
  3547. TManagedTerminal * PrevTerminal = TTerminalManager::Instance()->ActiveTerminal;
  3548. TTerminalManager::Instance()->ActiveTerminal = Data->Terminal;
  3549. NonVisualDataModule->StartBusy();
  3550. try
  3551. {
  3552. std::unique_ptr<TRemoteFile> File;
  3553. UnicodeString RemoteFileName =
  3554. UnixIncludeTrailingBackslash(Data->RemoteDirectory) + Data->OriginalFileName;
  3555. FTerminal->ExceptionOnFail = true;
  3556. try
  3557. {
  3558. TRemoteFile * AFile = NULL;
  3559. FTerminal->ReadFile(RemoteFileName, AFile);
  3560. File.reset(AFile);
  3561. if (!File->HaveFullFileName)
  3562. {
  3563. File->FullFileName = RemoteFileName;
  3564. }
  3565. }
  3566. __finally
  3567. {
  3568. FTerminal->ExceptionOnFail = false;
  3569. }
  3570. std::unique_ptr<TStrings> FileList(new TStringList());
  3571. FileList->AddObject(RemoteFileName, File.get());
  3572. UnicodeString RootTempDir = Data->LocalRootDirectory;
  3573. UnicodeString TempDir = ExtractFilePath(FileName);
  3574. TemporarilyDownloadFiles(FileList.get(), Data->ForceText, RootTempDir, TempDir, true, true);
  3575. // sanity check, the target file name should be still the same
  3576. DebugAssert(ExtractFileName(FileName) == FileList->Strings[0]);
  3577. }
  3578. __finally
  3579. {
  3580. NonVisualDataModule->EndBusy();
  3581. // it actually may not exist anymore...
  3582. TTerminalManager::Instance()->ActiveTerminal = PrevTerminal;
  3583. }
  3584. }
  3585. //---------------------------------------------------------------------------
  3586. void __fastcall TCustomScpExplorerForm::ExecutedFileEarlyClosed(
  3587. const TEditedFileData * Data, bool & KeepOpen)
  3588. {
  3589. // Command is set for external editors only (not for "shell" open).
  3590. if (!Data->Command.IsEmpty())
  3591. {
  3592. bool AnyFound = false;
  3593. bool AnyMDI = false;
  3594. bool AnyNonMDI = false;
  3595. bool AnyDetect = false;
  3596. TEditorList * EditorList = new TEditorList();
  3597. try
  3598. {
  3599. *EditorList = *WinConfiguration->EditorList;
  3600. for (int i = 0; i < EditorList->Count; i++)
  3601. {
  3602. const TEditorPreferences * Editor = EditorList->Editors[i];
  3603. if ((Editor->Data->Editor == edExternal) &&
  3604. (Editor->Data->ExternalEditor == Data->Command))
  3605. {
  3606. AnyFound = true;
  3607. if (Editor->Data->SDIExternalEditor)
  3608. {
  3609. AnyNonMDI = true;
  3610. if (Editor->Data->DetectMDIExternalEditor)
  3611. {
  3612. AnyDetect = true;
  3613. }
  3614. }
  3615. else
  3616. {
  3617. AnyMDI = true;
  3618. }
  3619. }
  3620. }
  3621. bool EnableMDI = false;
  3622. bool DisableDetect = false;
  3623. if (AnyMDI)
  3624. {
  3625. KeepOpen = true;
  3626. if (AnyNonMDI)
  3627. {
  3628. // there is at least one instance of the editor with MDI support enabled,
  3629. // and one with disabled, enable it for all instances
  3630. EnableMDI = true;
  3631. }
  3632. }
  3633. else if (AnyFound && !AnyDetect)
  3634. {
  3635. // at least once instance found but all have MDI autodetection disabled
  3636. // => close the file (default action)
  3637. }
  3638. else
  3639. {
  3640. // no instance of the editor has MDI support enabled
  3641. TMessageParams Params;
  3642. if (AnyFound)
  3643. {
  3644. // there is at least one instance of the editor
  3645. Params.Params |= mpNeverAskAgainCheck;
  3646. }
  3647. unsigned int Answer = MessageDialog(FMTLOAD(EDITOR_EARLY_CLOSED2, (Data->OriginalFileName)), qtWarning,
  3648. qaYes | qaNo, HELP_EDITOR_EARLY_CLOSED, &Params);
  3649. switch (Answer)
  3650. {
  3651. case qaNeverAskAgain:
  3652. DisableDetect = true;
  3653. break;
  3654. case qaNo:
  3655. EnableMDI = true;
  3656. KeepOpen = true;
  3657. break;
  3658. }
  3659. }
  3660. if (AnyFound && (EnableMDI || DisableDetect))
  3661. {
  3662. bool Changed = false;
  3663. for (int i = 0; i < EditorList->Count; i++)
  3664. {
  3665. const TEditorPreferences * Editor = EditorList->Editors[i];
  3666. if ((Editor->Data->Editor == edExternal) &&
  3667. (Editor->Data->ExternalEditor == Data->Command) &&
  3668. ((EnableMDI && Editor->Data->SDIExternalEditor) ||
  3669. (DisableDetect && Editor->Data->DetectMDIExternalEditor)))
  3670. {
  3671. Changed = true;
  3672. TEditorPreferences * UpdatedEditor = new TEditorPreferences(*Editor);
  3673. if (EnableMDI)
  3674. {
  3675. UpdatedEditor->GetData()->SDIExternalEditor = false;
  3676. }
  3677. if (DisableDetect)
  3678. {
  3679. UpdatedEditor->GetData()->DetectMDIExternalEditor = false;
  3680. }
  3681. EditorList->Change(i, UpdatedEditor);
  3682. }
  3683. }
  3684. if (Changed)
  3685. {
  3686. WinConfiguration->EditorList = EditorList;
  3687. }
  3688. }
  3689. }
  3690. __finally
  3691. {
  3692. delete EditorList;
  3693. }
  3694. }
  3695. else
  3696. {
  3697. // "open" case
  3698. MessageDialog(FMTLOAD(APP_EARLY_CLOSED, (Data->OriginalFileName)), qtWarning,
  3699. qaOK, HELP_APP_EARLY_CLOSED);
  3700. }
  3701. }
  3702. //---------------------------------------------------------------------------
  3703. void __fastcall TCustomScpExplorerForm::ExecutedFileUploadComplete(TObject * Sender)
  3704. {
  3705. EditorFormFileUploadComplete(DebugNotNull(dynamic_cast<TForm *>(Sender)));
  3706. }
  3707. //---------------------------------------------------------------------------
  3708. void __fastcall TCustomScpExplorerForm::RemoteDirViewEnter(TObject * /*Sender*/)
  3709. {
  3710. SideEnter(osRemote);
  3711. }
  3712. //---------------------------------------------------------------------------
  3713. void __fastcall TCustomScpExplorerForm::RemoteDriveViewEnter(TObject * /*Sender*/)
  3714. {
  3715. MakeNextInTabOrder(RemoteDirPanel, RemoteDrivePanel);
  3716. SideEnter(osRemote);
  3717. }
  3718. //---------------------------------------------------------------------------
  3719. void __fastcall TCustomScpExplorerForm::SideEnter(TOperationSide Side)
  3720. {
  3721. FCurrentSide = Side;
  3722. if (Visible)
  3723. {
  3724. UpdateControls();
  3725. }
  3726. }
  3727. //---------------------------------------------------------------------------
  3728. void __fastcall TCustomScpExplorerForm::DeleteFiles(TOperationSide Side,
  3729. TStrings * FileList, bool Alternative)
  3730. {
  3731. TCustomDirView * DView = DirView(Side);
  3732. DView->SaveSelection();
  3733. DView->SaveSelectedNames();
  3734. DebugAssert(!FAlternativeDelete);
  3735. FAlternativeDelete = Alternative;
  3736. try
  3737. {
  3738. if (!IsSideLocalBrowser(Side))
  3739. {
  3740. DebugAssert(Terminal != NULL);
  3741. Terminal->DeleteFiles(FileList, FLAGMASK(Alternative, dfAlternative));
  3742. }
  3743. else
  3744. {
  3745. try
  3746. {
  3747. TTerminalManager::Instance()->LocalTerminal->DeleteLocalFiles(FileList, FLAGMASK(Alternative, dfAlternative));
  3748. }
  3749. __finally
  3750. {
  3751. ReloadLocalDirectory();
  3752. }
  3753. }
  3754. FAlternativeDelete = false;
  3755. }
  3756. catch(...)
  3757. {
  3758. FAlternativeDelete = false;
  3759. DView->DiscardSavedSelection();
  3760. throw;
  3761. }
  3762. DView->RestoreSelection();
  3763. }
  3764. //---------------------------------------------------------------------------
  3765. void __fastcall TCustomScpExplorerForm::LockFiles(TStrings * FileList, bool Lock)
  3766. {
  3767. DebugAssert(Terminal);
  3768. DebugAssert(!IsLocalBrowserMode());
  3769. RemoteDirView->SaveSelection();
  3770. RemoteDirView->SaveSelectedNames();
  3771. try
  3772. {
  3773. if (Lock)
  3774. {
  3775. Terminal->LockFiles(FileList);
  3776. }
  3777. else
  3778. {
  3779. Terminal->UnlockFiles(FileList);
  3780. }
  3781. }
  3782. catch(...)
  3783. {
  3784. RemoteDirView->DiscardSavedSelection();
  3785. throw;
  3786. }
  3787. RemoteDirView->RestoreSelection();
  3788. }
  3789. //---------------------------------------------------------------------------
  3790. bool __fastcall TCustomScpExplorerForm::RemoteTransferDialog(TManagedTerminal *& Session,
  3791. TStrings * FileList, UnicodeString & Target, UnicodeString & FileMask, bool & DirectCopy,
  3792. bool NoConfirmation, bool Move)
  3793. {
  3794. DebugAssert(Terminal != NULL);
  3795. DebugAssert(!IsLocalBrowserMode());
  3796. // update Terminal->StateData->RemoteDirectory
  3797. UpdateTerminal(Terminal);
  3798. if (Session == NULL)
  3799. {
  3800. Session = TTerminalManager::Instance()->ActiveTerminal;
  3801. }
  3802. if (Session == TTerminalManager::Instance()->ActiveTerminal)
  3803. {
  3804. if (RemoteDriveView->DropTarget != NULL)
  3805. {
  3806. Target = RemoteDriveView->NodePathName(RemoteDriveView->DropTarget);
  3807. }
  3808. else if (RemoteDirView->DropTarget != NULL)
  3809. {
  3810. DebugAssert(RemoteDirView->ItemIsDirectory(RemoteDirView->DropTarget));
  3811. Target = RemoteDirView->ItemFullFileName(RemoteDirView->DropTarget);
  3812. }
  3813. else
  3814. {
  3815. Target = RemoteDirView->Path;
  3816. }
  3817. }
  3818. else
  3819. {
  3820. Target = Session->StateData->RemoteDirectory;
  3821. }
  3822. Target = UnixIncludeTrailingBackslash(Target);
  3823. if (FileList->Count == 1)
  3824. {
  3825. FileMask = DelimitFileNameMask(UnixExtractFileName(FileList->Strings[0]));
  3826. }
  3827. else
  3828. {
  3829. FileMask = AnyMask;
  3830. }
  3831. DirectCopy = FTerminal->IsCapable[fcRemoteCopy] || FTerminal->IsCapable[fcSecondaryShell];
  3832. bool Result = true;
  3833. if (!NoConfirmation)
  3834. {
  3835. bool Multi = (FileList->Count > 1);
  3836. if (Move)
  3837. {
  3838. Result = DoRemoteMoveDialog(Multi, Target, FileMask);
  3839. }
  3840. else
  3841. {
  3842. std::unique_ptr<TStrings> Sessions(new TStringList);
  3843. std::unique_ptr<TStrings> Directories(new TStringList);
  3844. TStrings * SessionList = TTerminalManager::Instance()->TerminalList;
  3845. for (int Index = 0; Index < SessionList->Count; Index++)
  3846. {
  3847. TManagedTerminal * ASession = DebugNotNull(dynamic_cast<TManagedTerminal *>(SessionList->Objects[Index]));
  3848. if (IsActiveTerminal(ASession))
  3849. {
  3850. Sessions->AddObject(SessionList->Strings[Index], ASession);
  3851. Directories->Add(ASession->StateData->RemoteDirectory);
  3852. }
  3853. }
  3854. TDirectRemoteCopy AllowDirectCopy;
  3855. if (FTerminal->IsCapable[fcRemoteCopy] || FTerminal->CommandSessionOpened)
  3856. {
  3857. DebugAssert(DirectCopy);
  3858. AllowDirectCopy = drcAllow;
  3859. }
  3860. else if (FTerminal->IsCapable[fcSecondaryShell])
  3861. {
  3862. DebugAssert(DirectCopy);
  3863. AllowDirectCopy = drcConfirmCommandSession;
  3864. }
  3865. else
  3866. {
  3867. DebugAssert(!DirectCopy);
  3868. AllowDirectCopy = drcDisallow;
  3869. }
  3870. void * ASession = Session;
  3871. Result = DoRemoteCopyDialog(
  3872. Sessions.get(), Directories.get(), AllowDirectCopy, Multi, ASession, Target, FileMask, DirectCopy, TTerminalManager::Instance()->ActiveTerminal);
  3873. Session = static_cast<TManagedTerminal *>(ASession);
  3874. }
  3875. }
  3876. return Result;
  3877. }
  3878. //---------------------------------------------------------------------------
  3879. bool __fastcall TCustomScpExplorerForm::RemoteTransferFiles(
  3880. TStrings * FileList, bool NoConfirmation, bool Move, TManagedTerminal * Session)
  3881. {
  3882. DebugAssert(!IsLocalBrowserMode());
  3883. bool DirectCopy;
  3884. UnicodeString Target, FileMask;
  3885. bool Result = RemoteTransferDialog(Session, FileList, Target, FileMask, DirectCopy, NoConfirmation, Move);
  3886. if (Result)
  3887. {
  3888. if (!Move && !DirectCopy)
  3889. {
  3890. Configuration->Usage->Inc("RemoteCopyTemp");
  3891. UnicodeString RootTempDir;
  3892. UnicodeString TempDir;
  3893. TemporarilyDownloadFiles(FileList, false, RootTempDir, TempDir, false, false);
  3894. TStrings * TemporaryFilesList = new TStringList();
  3895. try
  3896. {
  3897. TMakeLocalFileListParams MakeFileListParam;
  3898. MakeFileListParam.FileList = TemporaryFilesList;
  3899. MakeFileListParam.FileTimes = NULL;
  3900. MakeFileListParam.IncludeDirs = true;
  3901. MakeFileListParam.Recursive = false;
  3902. ProcessLocalDirectory(TempDir, FTerminal->MakeLocalFileList, &MakeFileListParam);
  3903. TTerminalManager::Instance()->ActiveTerminal = Session;
  3904. if (TemporaryFilesList->Count > 0)
  3905. {
  3906. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  3907. CopyParam.FileMask = FileMask;
  3908. DebugAssert(!FAutoOperation);
  3909. FAutoOperation = true;
  3910. FTerminal->CopyToRemote(TemporaryFilesList, Target, &CopyParam, cpTemporary, NULL);
  3911. }
  3912. }
  3913. __finally
  3914. {
  3915. delete TemporaryFilesList;
  3916. FAutoOperation = false;
  3917. if (!RootTempDir.IsEmpty())
  3918. {
  3919. RecursiveDeleteFile(ExcludeTrailingBackslash(RootTempDir), false);
  3920. }
  3921. }
  3922. }
  3923. else
  3924. {
  3925. RemoteDirView->SaveSelection();
  3926. RemoteDirView->SaveSelectedNames();
  3927. try
  3928. {
  3929. if (Move)
  3930. {
  3931. Configuration->Usage->Inc("RemoteMove");
  3932. Terminal->MoveFiles(FileList, Target, FileMask);
  3933. }
  3934. else
  3935. {
  3936. Configuration->Usage->Inc("RemoteCopyDirect");
  3937. DebugAssert(DirectCopy);
  3938. DebugAssert(Session == FTerminal);
  3939. if (FTerminal->IsCapable[fcRemoteCopy] ||
  3940. FTerminal->CommandSessionOpened ||
  3941. CommandSessionFallback())
  3942. {
  3943. Terminal->CopyFiles(FileList, Target, FileMask);
  3944. }
  3945. }
  3946. }
  3947. catch(...)
  3948. {
  3949. RemoteDirView->DiscardSavedSelection();
  3950. throw;
  3951. }
  3952. RemoteDirView->RestoreSelection();
  3953. }
  3954. }
  3955. return Result;
  3956. }
  3957. //---------------------------------------------------------------------------
  3958. void __fastcall TCustomScpExplorerForm::CreateRemoteDirectory(
  3959. const UnicodeString & Path, TRemoteProperties & Properties)
  3960. {
  3961. DebugAssert(!IsLocalBrowserMode());
  3962. Properties.Valid = Properties.Valid << vpEncrypt;
  3963. Properties.Encrypt = GUIConfiguration->CurrentCopyParam.EncryptNewFiles;
  3964. RemoteDirView->CreateDirectoryEx(Path, &Properties);
  3965. }
  3966. //---------------------------------------------------------------------------
  3967. void __fastcall TCustomScpExplorerForm::CreateDirectory(TOperationSide Side)
  3968. {
  3969. Side = GetSide(Side);
  3970. TRemoteProperties Properties = GUIConfiguration->NewDirectoryProperties;
  3971. TRemoteProperties * AProperties = (!IsSideLocalBrowser(Side) ? &Properties : NULL);
  3972. UnicodeString Name = LoadStr(NEW_FOLDER);
  3973. int AllowedChanges =
  3974. FLAGMASK(!IsSideLocalBrowser(Side) && Terminal->IsCapable[fcModeChanging], cpMode);
  3975. bool SaveSettings = false;
  3976. if (DoCreateDirectoryDialog(Name, AProperties, AllowedChanges, SaveSettings))
  3977. {
  3978. TWindowLock Lock(this);
  3979. if (!IsSideLocalBrowser(Side))
  3980. {
  3981. if (SaveSettings)
  3982. {
  3983. GUIConfiguration->NewDirectoryProperties = Properties;
  3984. }
  3985. CreateRemoteDirectory(Name, Properties);
  3986. }
  3987. else
  3988. {
  3989. DirView(Side)->CreateDirectory(Name);
  3990. }
  3991. }
  3992. }
  3993. //---------------------------------------------------------------------------
  3994. void __fastcall TCustomScpExplorerForm::HomeDirectory(TOperationSide Side)
  3995. {
  3996. TWindowLock Lock(this);
  3997. DirView(Side)->ExecuteHomeDirectory();
  3998. }
  3999. //---------------------------------------------------------------------------
  4000. void __fastcall TCustomScpExplorerForm::OpenDirectory(TOperationSide Side)
  4001. {
  4002. DoOpenDirectoryDialog(odBrowse, Side);
  4003. }
  4004. //---------------------------------------------------------------------------
  4005. bool __fastcall TCustomScpExplorerForm::OpenBookmark(TOperationSide Side, TBookmark * Bookmark)
  4006. {
  4007. UnicodeString Path = Bookmark->GetSideDirectory(Side);
  4008. bool Result = !Path.IsEmpty();
  4009. if (Result)
  4010. {
  4011. TryOpenDirectory(Side, Path);
  4012. }
  4013. return Result;
  4014. }
  4015. //---------------------------------------------------------------------------
  4016. void __fastcall TCustomScpExplorerForm::CalculateSize(
  4017. TStrings * FileList, __int64 & Size, TCalculateSizeStats & Stats,
  4018. bool & Close)
  4019. {
  4020. // terminal can be already closed (e.g. dropped connection)
  4021. if (Terminal != NULL)
  4022. {
  4023. try
  4024. {
  4025. Terminal->CalculateFilesSize(FileList, Size, 0, NULL, true, Stats);
  4026. }
  4027. catch(...)
  4028. {
  4029. if (!Terminal->Active)
  4030. {
  4031. Close = true;
  4032. }
  4033. throw;
  4034. }
  4035. }
  4036. }
  4037. //---------------------------------------------------------------------------
  4038. void __fastcall TCustomScpExplorerForm::CalculateChecksum(const UnicodeString & Alg,
  4039. TStrings * FileList, TCalculatedChecksumEvent OnCalculatedChecksum,
  4040. bool & Close)
  4041. {
  4042. // terminal can be already closed (e.g. dropped connection)
  4043. if (Terminal != NULL)
  4044. {
  4045. Configuration->Usage->Inc(L"ChecksumCalculated");
  4046. try
  4047. {
  4048. Terminal->CalculateFilesChecksum(Alg, FileList, NULL, OnCalculatedChecksum);
  4049. }
  4050. catch(...)
  4051. {
  4052. if (!Terminal->Active)
  4053. {
  4054. Close = true;
  4055. }
  4056. throw;
  4057. }
  4058. }
  4059. }
  4060. //---------------------------------------------------------------------------
  4061. void TCustomScpExplorerForm::LoadFilesProperties(TStrings * FileList)
  4062. {
  4063. if (Terminal->LoadFilesProperties(FileList))
  4064. {
  4065. RemoteDirView->Invalidate();
  4066. }
  4067. }
  4068. //---------------------------------------------------------------------------
  4069. bool __fastcall TCustomScpExplorerForm::SetProperties(TOperationSide Side, TStrings * FileList)
  4070. {
  4071. bool Result;
  4072. if (!IsSideLocalBrowser(Side))
  4073. {
  4074. TRemoteTokenList * GroupList = NULL;
  4075. TRemoteTokenList * UserList = NULL;
  4076. try
  4077. {
  4078. TRemoteProperties CurrentProperties;
  4079. LoadFilesProperties(FileList);
  4080. bool CapableGroupChanging = Terminal->IsCapable[fcGroupChanging];
  4081. bool CapableOwnerChanging = Terminal->IsCapable[fcOwnerChanging];
  4082. if (CapableGroupChanging || CapableOwnerChanging)
  4083. {
  4084. if (CapableGroupChanging)
  4085. {
  4086. GroupList = Terminal->Groups->Duplicate();
  4087. }
  4088. if (CapableOwnerChanging)
  4089. {
  4090. UserList = Terminal->Users->Duplicate();
  4091. }
  4092. TRemoteDirectory * Files = Terminal->Files;
  4093. int Count = Files->Count;
  4094. if (Count > 100)
  4095. {
  4096. Count = 100;
  4097. }
  4098. for (int Index = 0; Index < Count; Index++)
  4099. {
  4100. TRemoteFile * File = Files->Files[Index];
  4101. if (CapableGroupChanging)
  4102. {
  4103. GroupList->AddUnique(File->Group);
  4104. }
  4105. if (CapableOwnerChanging)
  4106. {
  4107. UserList->AddUnique(File->Owner);
  4108. }
  4109. }
  4110. // if we haven't collected tokens for all files in current directory,
  4111. // make sure we collect them at least for all selected files.
  4112. // (note that so far the files in FileList has to be from current direcotry)
  4113. if (Count < Files->Count)
  4114. {
  4115. for (int Index = 0; Index < FileList->Count; Index++)
  4116. {
  4117. TRemoteFile * File = (TRemoteFile *)(FileList->Objects[Index]);
  4118. if (CapableGroupChanging)
  4119. {
  4120. GroupList->AddUnique(File->Group);
  4121. }
  4122. if (CapableOwnerChanging)
  4123. {
  4124. UserList->AddUnique(File->Owner);
  4125. }
  4126. }
  4127. }
  4128. }
  4129. CurrentProperties = TRemoteProperties::CommonProperties(FileList);
  4130. bool CapableAclChanging = Terminal->IsCapable[fcAclChangingFiles];
  4131. for (int Index = 0; (Index < FileList->Count) && CapableAclChanging; Index++)
  4132. {
  4133. if (dynamic_cast<TRemoteFile *>(FileList->Objects[Index])->IsDirectory)
  4134. {
  4135. CapableAclChanging = false;
  4136. CurrentProperties.Valid = CurrentProperties.Valid >> vpRights;
  4137. }
  4138. }
  4139. int Flags = 0;
  4140. if (Terminal->IsCapable[fcModeChanging]) Flags |= cpMode;
  4141. if (CapableAclChanging) Flags |= cpAcl;
  4142. if (CapableOwnerChanging) Flags |= cpOwner;
  4143. if (CapableGroupChanging) Flags |= cpGroup;
  4144. TCalculateChecksumEvent CalculateChecksumEvent = NULL;
  4145. if (Terminal->IsCapable[fcCalculatingChecksum])
  4146. {
  4147. CalculateChecksumEvent = CalculateChecksum;
  4148. }
  4149. std::unique_ptr<TStrings> ChecksumAlgs(new TStringList());
  4150. Terminal->GetSupportedChecksumAlgs(ChecksumAlgs.get());
  4151. TRemoteProperties NewProperties = CurrentProperties;
  4152. Result =
  4153. DoPropertiesDialog(FileList, RemoteDirView->PathName,
  4154. GroupList, UserList, ChecksumAlgs.get(), &NewProperties, Flags,
  4155. Terminal->IsCapable[fcGroupOwnerChangingByID],
  4156. CalculateSize, CalculateChecksumEvent);
  4157. if (Result)
  4158. {
  4159. NewProperties = TRemoteProperties::ChangedProperties(CurrentProperties, NewProperties);
  4160. Terminal->ChangeFilesProperties(FileList, &NewProperties);
  4161. }
  4162. }
  4163. __finally
  4164. {
  4165. delete GroupList;
  4166. delete UserList;
  4167. }
  4168. }
  4169. else
  4170. {
  4171. DirView(Side)->DisplayPropertiesMenu();
  4172. Result = true;
  4173. }
  4174. return Result;
  4175. }
  4176. //---------------------------------------------------------------------------
  4177. void __fastcall TCustomScpExplorerForm::KeyProcessed(Word & Key, TShiftState Shift)
  4178. {
  4179. if (Shift * AllKeyShiftStates() == (TShiftState() << ssAlt))
  4180. {
  4181. FIgnoreNextDialogChar = Key;
  4182. }
  4183. Key = 0;
  4184. }
  4185. //---------------------------------------------------------------------------
  4186. void __fastcall TCustomScpExplorerForm::CheckCustomCommandShortCut(
  4187. TCustomCommandList * List, Word & Key, Classes::TShiftState Shift, TShortCut KeyShortCut)
  4188. {
  4189. const TCustomCommandType * Command = List->Find(KeyShortCut);
  4190. if (Command != NULL)
  4191. {
  4192. KeyProcessed(Key, Shift);
  4193. if (CustomCommandState(*Command, false, ccltAll) > 0)
  4194. {
  4195. TAutoFlag DontCopyCommandToClipboardFlag(DontCopyCommandToClipboard);
  4196. ExecuteFileOperationCommand(foCustomCommand, osRemote,
  4197. false, false, const_cast<TCustomCommandType *>(Command));
  4198. }
  4199. }
  4200. }
  4201. //---------------------------------------------------------------------------
  4202. void __fastcall TCustomScpExplorerForm::KeyDown(Word & Key, Classes::TShiftState Shift)
  4203. {
  4204. if (!NonVisualDataModule->Busy)
  4205. {
  4206. if (QueueView3->Focused() && (QueueView3->OnKeyDown != NULL))
  4207. {
  4208. QueueView3->OnKeyDown(QueueView3, Key, Shift);
  4209. }
  4210. if ((Key != 0) && !DirView(osCurrent)->IsEditing())
  4211. {
  4212. TShortCut KeyShortCut = ShortCut(Key, Shift);
  4213. for (int Index = 0; Index < NonVisualDataModule->ExplorerActions->ActionCount; Index++)
  4214. {
  4215. TAction * Action = (TAction *)NonVisualDataModule->ExplorerActions->Actions[Index];
  4216. if (((Action->ShortCut == KeyShortCut) ||
  4217. (Action->SecondaryShortCuts->IndexOfShortCut(KeyShortCut) >= 0)) &&
  4218. AllowedAction(Action, aaShortCut))
  4219. {
  4220. // Has to be called before the action as the dialog char is already in queue.
  4221. // So when the action consumes message queue, we already need to have the
  4222. // FIgnoreNextDialogChar set
  4223. KeyProcessed(Key, Shift);
  4224. // Reset reference to previous component (when menu/toolbar was clicked).
  4225. // Needed to detect that action was invoked by keyboard shortcut
  4226. // in TNonVisualDataModule::ExplorerActionsExecute
  4227. Action->ActionComponent = NULL;
  4228. Action->Execute();
  4229. return;
  4230. }
  4231. }
  4232. for (int i = 0; i < TTerminalManager::Instance()->Count; i++)
  4233. {
  4234. if (NonVisualDataModule->OpenSessionShortCut(i) == KeyShortCut)
  4235. {
  4236. KeyProcessed(Key, Shift);
  4237. TTerminalManager::Instance()->ActiveTerminalIndex = i;
  4238. return;
  4239. }
  4240. }
  4241. if (Key == VK_TAB && Shift.Contains(ssCtrl))
  4242. {
  4243. KeyProcessed(Key, Shift);
  4244. TTerminalManager::Instance()->CycleTerminals(!Shift.Contains(ssShift));
  4245. }
  4246. TShortCut CustomShortCut = NormalizeCustomShortCut(KeyShortCut);
  4247. if (IsCustomShortCut(CustomShortCut))
  4248. {
  4249. CheckCustomCommandShortCut(WinConfiguration->CustomCommandList, Key, Shift, CustomShortCut);
  4250. CheckCustomCommandShortCut(WinConfiguration->ExtensionList, Key, Shift, CustomShortCut);
  4251. if (WinConfiguration->SharedBookmarks != NULL)
  4252. {
  4253. TBookmark * Bookmark = WinConfiguration->SharedBookmarks->FindByShortCut(CustomShortCut);
  4254. if ((Bookmark != NULL) &&
  4255. OpenBookmark(FCurrentSide, Bookmark))
  4256. {
  4257. KeyProcessed(Key, Shift);
  4258. }
  4259. }
  4260. }
  4261. }
  4262. }
  4263. TForm::KeyDown(Key, Shift);
  4264. }
  4265. //---------------------------------------------------------------------------
  4266. void __fastcall TCustomScpExplorerForm::InitStatusBar()
  4267. {
  4268. DebugAssert(Terminal != NULL);
  4269. const TSessionInfo & SessionInfo = Terminal->GetSessionInfo();
  4270. const TFileSystemInfo & FileSystemInfo = Terminal->GetFileSystemInfo();
  4271. TTBXStatusBar * SessionStatusBar = (TTBXStatusBar *)GetComponent(fcStatusBar);
  4272. int Offset = SessionStatusBar->Panels->Count - SessionPanelCount;
  4273. bool SecurityEnabled = !SessionInfo.SecurityProtocolName.IsEmpty();
  4274. SessionStatusBar->Panels->Items[Offset + 0]->Enabled = SecurityEnabled;
  4275. // expanded from ?: to avoid memory leaks
  4276. if (SecurityEnabled)
  4277. {
  4278. SessionStatusBar->Panels->Items[Offset + 0]->Hint =
  4279. FMTLOAD(STATUS_SECURE, (SessionInfo.SecurityProtocolName));
  4280. }
  4281. else
  4282. {
  4283. SessionStatusBar->Panels->Items[Offset + 0]->Hint = LoadStr(STATUS_INSECURE);
  4284. }
  4285. if (FileSystemInfo.ProtocolName.IsEmpty())
  4286. {
  4287. SessionStatusBar->Panels->Items[Offset + 1]->Caption = SessionInfo.ProtocolName;
  4288. }
  4289. else
  4290. {
  4291. SessionStatusBar->Panels->Items[Offset + 1]->Caption = FileSystemInfo.ProtocolName;
  4292. }
  4293. SessionStatusBar->Panels->Items[Offset + 1]->Hint = LoadStr(STATUS_PROTOCOL_HINT);
  4294. SessionStatusBar->Panels->Items[Offset + 2]->Enabled =
  4295. (!SessionInfo.CSCompression.IsEmpty() || !SessionInfo.SCCompression.IsEmpty());
  4296. UnicodeString CSCompressionStr = DefaultStr(SessionInfo.CSCompression, LoadStr(NO_STR));
  4297. UnicodeString SCCompressionStr = DefaultStr(SessionInfo.SCCompression, LoadStr(NO_STR));
  4298. UnicodeString CompressionStr = CSCompressionStr;
  4299. if (CSCompressionStr != SCCompressionStr)
  4300. {
  4301. CompressionStr += UnicodeString(L"/") + SCCompressionStr;
  4302. }
  4303. SessionStatusBar->Panels->Items[Offset + 2]->Hint = FMTLOAD(STATUS_COMPRESSION_HINT, (CompressionStr));
  4304. SessionStatusBar->Panels->Items[Offset + 3]->Hint = LoadStr(STATUS_DURATION_HINT);
  4305. UpdateStatusBar();
  4306. }
  4307. //---------------------------------------------------------------------------
  4308. void __fastcall TCustomScpExplorerForm::UpdateStatusBar()
  4309. {
  4310. TTBXStatusBar * SessionStatusBar = (TTBXStatusBar *)GetComponent(fcStatusBar);
  4311. DebugAssert(SessionStatusBar != NULL);
  4312. if (!HasActiveTerminal() || (Terminal->Status < ssOpened))
  4313. {
  4314. // note: (Terminal->Status < sshReady) currently never happens here,
  4315. // so STATUS_CONNECTING is never used
  4316. SessionStatusBar->SimplePanel = true;
  4317. SessionStatusBar->SimpleText = LoadStr(
  4318. !HasActiveTerminal() ? STATUS_NOT_CONNECTED : STATUS_CONNECTING);
  4319. }
  4320. else
  4321. {
  4322. DebugAssert(Terminal);
  4323. SessionStatusBar->SimplePanel = false;
  4324. const TSessionInfo & SessionInfo = Terminal->GetSessionInfo();
  4325. if (!FNote.IsEmpty())
  4326. {
  4327. SessionStatusBar->Panels->Items[0]->Caption = FNote;
  4328. }
  4329. else
  4330. {
  4331. UpdateStatusPanelText(SessionStatusBar->Panels->Items[0]);
  4332. }
  4333. SessionStatusBar->Panels->Items[SessionStatusBar->Panels->Count - 1]->Caption =
  4334. FormatDateTimeSpan(Configuration->TimeFormat, Now() - SessionInfo.LoginTime);
  4335. }
  4336. SessionStatusBar->Panels->Items[0]->Hint = FNoteHints;
  4337. }
  4338. //---------------------------------------------------------------------------
  4339. void __fastcall TCustomScpExplorerForm::UpdateStatusPanelText(TTBXStatusPanel * Panel)
  4340. {
  4341. Panel->Caption = L"";
  4342. }
  4343. //---------------------------------------------------------------------------
  4344. void __fastcall TCustomScpExplorerForm::Idle()
  4345. {
  4346. if (FShowing ||
  4347. // Particularly for
  4348. // - Standalone editing
  4349. // - Detecting closed connection and automatically reconnect it while waiting for changes
  4350. // while "keeping remote directory up to date".
  4351. StandaloneOperation)
  4352. {
  4353. FEditorManager->Check();
  4354. // make sure that Idle is called before update queue, as it may invoke QueueEvent
  4355. // that needs to know if queue view is visible (and it may be closed after queue update)
  4356. TTerminalManager::Instance()->Idle(FDoNotIdleCurrentTerminal > 0);
  4357. }
  4358. if (FShowing)
  4359. {
  4360. if (!NonVisualDataModule->Busy &&
  4361. // Menu is opened or drag&drop is going on
  4362. (Mouse->Capture == NULL))
  4363. {
  4364. if (FRefreshRemoteDirectory)
  4365. {
  4366. if ((Terminal != NULL) && (Terminal->Status == ssOpened))
  4367. {
  4368. Terminal->RefreshDirectory();
  4369. }
  4370. FRefreshRemoteDirectory = false;
  4371. }
  4372. if (FRefreshLocalDirectory)
  4373. {
  4374. ReloadLocalDirectory();
  4375. FRefreshLocalDirectory = false;
  4376. }
  4377. if (WinConfiguration->RefreshRemotePanel)
  4378. {
  4379. if ((Terminal != NULL) && (Terminal->Status == ssOpened) &&
  4380. (Now() - Terminal->DirectoryLoaded > WinConfiguration->RefreshRemotePanelInterval))
  4381. {
  4382. RemoteDirView->ReloadDirectory();
  4383. }
  4384. }
  4385. }
  4386. }
  4387. if (FShowing || StandaloneOperation)
  4388. {
  4389. if (FQueueStatusInvalidated)
  4390. {
  4391. UpdateQueueStatus(false);
  4392. }
  4393. RefreshQueueItems();
  4394. }
  4395. if (FShowing)
  4396. {
  4397. UpdateStatusBar();
  4398. }
  4399. if (FClipboardFakeMonitorsPendingReset && !FDownloadingFromClipboard)
  4400. {
  4401. FClipboardFakeMonitorsPendingReset = false;
  4402. FClipboardFakeMonitors.reset(NULL);
  4403. }
  4404. FIgnoreNextDialogChar = 0;
  4405. }
  4406. //---------------------------------------------------------------------------
  4407. void __fastcall TCustomScpExplorerForm::UserActionTimer(TObject * /*Sender*/)
  4408. {
  4409. try
  4410. {
  4411. FUserActionTimer->Enabled = false;
  4412. if (IsQueueAutoPopup() && (FPendingQueueActionItem != NULL))
  4413. {
  4414. if (TQueueItem::IsUserActionStatus(FPendingQueueActionItem->Status))
  4415. {
  4416. FPendingQueueActionItem->ProcessUserAction();
  4417. }
  4418. }
  4419. }
  4420. __finally
  4421. {
  4422. FPendingQueueActionItem = NULL;
  4423. }
  4424. }
  4425. //---------------------------------------------------------------------------
  4426. void __fastcall TCustomScpExplorerForm::ApplicationMinimize(TObject * /*Sender*/)
  4427. {
  4428. if (WinConfiguration->MinimizeToTray)
  4429. {
  4430. UpdateTrayIcon();
  4431. FTrayIcon->Visible = true;
  4432. if (Visible)
  4433. {
  4434. ShowWindow(Handle, SW_HIDE);
  4435. }
  4436. }
  4437. }
  4438. //---------------------------------------------------------------------------
  4439. void __fastcall TCustomScpExplorerForm::ApplicationRestore(TObject * /*Sender*/)
  4440. {
  4441. // WORKAROUND
  4442. // When restoring maximized window from minimization,
  4443. // rarely some controls do not align properly.
  4444. // Two instances seen (both for Commander):
  4445. // - When restoring, window is temporarily narrower (not maximized),
  4446. // causing toolbars on TopDock to wrap and dock to expand horizontally.
  4447. // Once maximized already, top dock shinks back, but the session PageControl,
  4448. // do not align up, leaving space between TopDock and PageControl.
  4449. // - Similar issue seen with LocalDirView not aligning down to status bar.
  4450. for (int Index = 0; Index < ControlCount; Index++)
  4451. {
  4452. RealignControl(Controls[Index]);
  4453. }
  4454. if (FTrayIcon->Visible)
  4455. {
  4456. FTrayIcon->Visible = false;
  4457. if (Visible)
  4458. {
  4459. ShowWindow(Handle, SW_SHOW);
  4460. }
  4461. }
  4462. if (FNeedSession && DebugAlwaysTrue(Terminal == NULL))
  4463. {
  4464. FNeedSession = false;
  4465. NonVisualDataModule->StartBusy();
  4466. try
  4467. {
  4468. NeedSession(true);
  4469. }
  4470. __finally
  4471. {
  4472. NonVisualDataModule->EndBusy();
  4473. }
  4474. }
  4475. }
  4476. //---------------------------------------------------------------------------
  4477. void __fastcall TCustomScpExplorerForm::UpdateTrayIcon()
  4478. {
  4479. FTrayIcon->Hint = GetMainForm()->Caption;
  4480. }
  4481. //---------------------------------------------------------------------------
  4482. void __fastcall TCustomScpExplorerForm::ApplicationTitleChanged()
  4483. {
  4484. UpdateTrayIcon();
  4485. }
  4486. //---------------------------------------------------------------------------
  4487. void __fastcall TCustomScpExplorerForm::RestoreApp()
  4488. {
  4489. // workaround
  4490. // TApplication.WndProc sets TApplication.FAppIconic to false,
  4491. // on WM_ACTIVATEAPP, what renders TApplication.Restore no-op function.
  4492. // But WM_ACTIVATEAPP message can be received even when
  4493. // the main window is minimized to the tray and internal editor window is focused
  4494. // (after another application was previously active)
  4495. if (::IsIconic(Handle))
  4496. {
  4497. if (!IsAppIconic())
  4498. {
  4499. SetAppIconic(true);
  4500. }
  4501. }
  4502. ::ApplicationRestore();
  4503. Application->BringToFront();
  4504. }
  4505. //---------------------------------------------------------------------------
  4506. void __fastcall TCustomScpExplorerForm::TrayIconClick(TObject * /*Sender*/)
  4507. {
  4508. RestoreApp();
  4509. }
  4510. //---------------------------------------------------------------------------
  4511. void __fastcall TCustomScpExplorerForm::NewSession(const UnicodeString & SessionUrl)
  4512. {
  4513. if (OpenInNewWindow())
  4514. {
  4515. ExecuteNewInstance(SessionUrl);
  4516. }
  4517. else
  4518. {
  4519. TTerminalManager::Instance()->NewSession(SessionUrl);
  4520. }
  4521. }
  4522. //---------------------------------------------------------------------------
  4523. UnicodeString __fastcall TCustomScpExplorerForm::SaveHiddenDuplicateSession(TSessionData * SessionData)
  4524. {
  4525. UnicodeString SessionName = StoredSessions->HiddenPrefix + Terminal->SessionData->SessionName;
  4526. StoredSessions->NewSession(SessionName, SessionData);
  4527. // modified only, explicit
  4528. StoredSessions->Save(false, true);
  4529. // encode session name because of slashes in hierarchical sessions
  4530. return EncodeUrlString(SessionName);
  4531. }
  4532. //---------------------------------------------------------------------------
  4533. UnicodeString __fastcall TCustomScpExplorerForm::CreateHiddenDuplicateSession()
  4534. {
  4535. // current working directories become defaults here, what is not right
  4536. std::unique_ptr<TSessionData> SessionData(CloneCurrentSessionData());
  4537. return SaveHiddenDuplicateSession(SessionData.get());
  4538. }
  4539. //---------------------------------------------------------------------------
  4540. void __fastcall TCustomScpExplorerForm::DuplicateSession()
  4541. {
  4542. if (OpenInNewWindow())
  4543. {
  4544. ExecuteNewInstance(CreateHiddenDuplicateSession());
  4545. }
  4546. else
  4547. {
  4548. // current working directories become defaults here, what is not right
  4549. std::unique_ptr<TSessionData> SessionData(CloneCurrentSessionData());
  4550. TTerminalManager * Manager = TTerminalManager::Instance();
  4551. TManagedTerminal * ATerminal = Manager->NewManagedTerminal(SessionData.get());
  4552. // We definitelly want this
  4553. ATerminal->Disconnected = Terminal->Disconnected;
  4554. // Not sure about these two
  4555. ATerminal->Permanent = Terminal->Permanent;
  4556. ATerminal->DisconnectedTemporarily = Terminal->DisconnectedTemporarily;
  4557. Manager->ActiveTerminal = ATerminal;
  4558. }
  4559. }
  4560. //---------------------------------------------------------------------------
  4561. void __fastcall TCustomScpExplorerForm::RenameSession()
  4562. {
  4563. UnicodeString Name = Terminal->SessionData->SessionName;
  4564. if (InputDialog(LoadStr(RENAME_SESSION_TITLE), LoadStr(RENAME_SESSION_PROMPT), Name, HELP_SESSION_RENAME))
  4565. {
  4566. // Checks for a slash only, so it's not that big deal that we do the check after submitting the dialog only.
  4567. TSessionData::ValidateName(Name);
  4568. // This is inconsistent with how color (for example) is handled.
  4569. Terminal->SessionData->Name = Name;
  4570. UpdateControls();
  4571. // Add/Remove distinguishing paths from sessions of the same name.
  4572. DoTerminalListChanged();
  4573. }
  4574. }
  4575. //---------------------------------------------------------------------------
  4576. bool __fastcall TCustomScpExplorerForm::CanCloseQueue(TTerminalQueue * Queue)
  4577. {
  4578. DebugAssert(Queue != NULL);
  4579. bool Result = Queue->IsEmpty;
  4580. if (!Result)
  4581. {
  4582. SetFocus();
  4583. Result = (MessageDialog(LoadStr(PENDING_QUEUE_ITEMS2), qtWarning, qaOK | qaCancel, HELP_NONE) == qaOK);
  4584. }
  4585. return Result;
  4586. }
  4587. //---------------------------------------------------------------------------
  4588. bool __fastcall TCustomScpExplorerForm::CanCloseQueue()
  4589. {
  4590. return CanCloseQueue(FQueue);
  4591. }
  4592. //---------------------------------------------------------------------------
  4593. void __fastcall TCustomScpExplorerForm::CloseSession()
  4594. {
  4595. if (CanCloseQueue())
  4596. {
  4597. TTerminalManager::Instance()->FreeActiveTerminal();
  4598. }
  4599. }
  4600. //---------------------------------------------------------------------------
  4601. void __fastcall TCustomScpExplorerForm::DisconnectSession()
  4602. {
  4603. if (CanCloseQueue())
  4604. {
  4605. TTerminalManager::Instance()->DisconnectActiveTerminal();
  4606. }
  4607. }
  4608. //---------------------------------------------------------------------------
  4609. void __fastcall TCustomScpExplorerForm::TerminalDisconnected()
  4610. {
  4611. DetachTerminal(Terminal);
  4612. RemoteDirView->Terminal = Terminal;
  4613. UpdateRemotePathComboBox(false);
  4614. UpdateControls();
  4615. }
  4616. //---------------------------------------------------------------------------
  4617. void __fastcall TCustomScpExplorerForm::ReconnectSession()
  4618. {
  4619. if (DebugAlwaysTrue(!Terminal->Active))
  4620. {
  4621. TTerminalManager::Instance()->ReconnectActiveTerminal();
  4622. }
  4623. }
  4624. //---------------------------------------------------------------------------
  4625. void __fastcall TCustomScpExplorerForm::OpenStoredSession(TSessionData * Data)
  4626. {
  4627. if (OpenInNewWindow())
  4628. {
  4629. // encode session name because of slashes in hierarchical sessions
  4630. ExecuteNewInstance(EncodeUrlString(Data->Name));
  4631. }
  4632. else
  4633. {
  4634. TTerminalManager * Manager = TTerminalManager::Instance();
  4635. TManagedTerminal * Terminal = Manager->NewManagedTerminal(Data);
  4636. Manager->ActiveTerminal = Terminal;
  4637. }
  4638. }
  4639. //---------------------------------------------------------------------------
  4640. void TCustomScpExplorerForm::DoOpenFolderOrWorkspace(
  4641. const UnicodeString & Name, bool ConnectFirstTerminal, bool CheckMaxSessions)
  4642. {
  4643. TTerminalManager * Manager = TTerminalManager::Instance();
  4644. std::unique_ptr<TObjectList> DataList(new TObjectList());
  4645. StoredSessions->GetFolderOrWorkspace(Name, DataList.get());
  4646. if (!CheckMaxSessions || (DataList->Count <= WinConfiguration->MaxSessions))
  4647. {
  4648. TManagedTerminal * FirstTerminal = Manager->NewTerminals(DataList.get());
  4649. // FirstTerminal can be null, if some of the
  4650. if (!ConnectFirstTerminal && (FirstTerminal != NULL))
  4651. {
  4652. FirstTerminal->Disconnected = true;
  4653. FirstTerminal->DisconnectedTemporarily = true;
  4654. }
  4655. Manager->ActiveTerminal = FirstTerminal;
  4656. }
  4657. }
  4658. //---------------------------------------------------------------------------
  4659. void __fastcall TCustomScpExplorerForm::OpenFolderOrWorkspace(const UnicodeString & Name)
  4660. {
  4661. if (OpenInNewWindow())
  4662. {
  4663. ExecuteNewInstance(Name);
  4664. }
  4665. else
  4666. {
  4667. DoOpenFolderOrWorkspace(Name, true, false);
  4668. }
  4669. }
  4670. //---------------------------------------------------------------------------
  4671. void __fastcall TCustomScpExplorerForm::FormCloseQuery(TObject * /*Sender*/,
  4672. bool &CanClose)
  4673. {
  4674. // See the comment in CloseApp()
  4675. if (NonVisualDataModule->Busy)
  4676. {
  4677. CanClose = false;
  4678. }
  4679. else
  4680. {
  4681. TTerminalManager * Manager = TTerminalManager::Instance();
  4682. int ActiveSessions = 0;
  4683. for (int Index = 0; Index < Manager->Count; Index++)
  4684. {
  4685. if (Manager->Terminals[Index]->Active)
  4686. {
  4687. ActiveSessions++;
  4688. }
  4689. }
  4690. // Confirm, when there's at least one active remote session or
  4691. // when there are multiple (inactive or local) sessions and auto workspace saving is not enabled.
  4692. if (((ActiveSessions > 0) || ((Manager->Count > 1) && !WinConfiguration->AutoSaveWorkspace)) &&
  4693. WinConfiguration->ConfirmClosingSession)
  4694. {
  4695. unsigned int Result;
  4696. TMessageParams Params(mpNeverAskAgainCheck);
  4697. UnicodeString Message;
  4698. int Answers = qaOK | qaCancel;
  4699. // The current session is the only active session
  4700. if ((ActiveSessions == 1) && (Terminal != NULL) && Terminal->Active)
  4701. {
  4702. if (!WinConfiguration->AutoSaveWorkspace)
  4703. {
  4704. Message = LoadStr(CLOSE_SESSION_WORKSPACE);
  4705. }
  4706. else
  4707. {
  4708. Message = LoadStr(CLOSE_SESSION);
  4709. }
  4710. Message = FORMAT(Message, (Terminal->SessionData->SessionName));
  4711. }
  4712. // Multiple active sessions or one active session, but it's not the current one
  4713. else if (ActiveSessions > 0)
  4714. {
  4715. if (!WinConfiguration->AutoSaveWorkspace)
  4716. {
  4717. Message = LoadStr(CLOSE_SESSIONS_WORKSPACE3);
  4718. }
  4719. else
  4720. {
  4721. Message = LoadStr(CLOSE_SESSIONS);
  4722. }
  4723. }
  4724. // Multiple tabs, but all are inactive or local, and saving of worspace is not enabled
  4725. else
  4726. {
  4727. DebugAlwaysTrue(Manager->Count > 1);
  4728. DebugAlwaysTrue(!WinConfiguration->AutoSaveWorkspace);
  4729. Message = LoadStr(CLOSE_WORKSPACE);
  4730. }
  4731. UnicodeString Note;
  4732. if (WinConfiguration->AutoSaveWorkspace)
  4733. {
  4734. Note = FMTLOAD(AUTO_WORKSPACE, (WorkspaceName()));
  4735. }
  4736. else
  4737. {
  4738. Note = LoadStr(AUTO_WORKSPACE_ENABLE);
  4739. Answers = qaYes | qaNo | qaCancel;
  4740. }
  4741. Message = FORMAT("%s\n\n%s", (MainInstructions(Message), Note));
  4742. SetFocus();
  4743. Result = MessageDialog(Message, qtConfirmation,
  4744. Answers, HELP_CLOSE_SESSION_WORKSPACE, &Params);
  4745. if (Result == qaNeverAskAgain)
  4746. {
  4747. WinConfiguration->ConfirmClosingSession = false;
  4748. }
  4749. if (Result == qaNo)
  4750. {
  4751. CanClose = SaveWorkspace(true);
  4752. // note that the workspace will be saved redundatly again from FormClose
  4753. }
  4754. else
  4755. {
  4756. CanClose =
  4757. (Result == qaOK) ||
  4758. (Result == qaYes) || // CLOSE_SESSIONS_WORKSPACE variant
  4759. (Result == qaNeverAskAgain);
  4760. }
  4761. }
  4762. if (CanClose && HasActiveTerminal())
  4763. {
  4764. CanClose = CanCloseQueue();
  4765. }
  4766. }
  4767. if (CanClose)
  4768. {
  4769. CanClose =
  4770. FEditorManager->CloseInternalEditors(CloseInternalEditor) &&
  4771. FEditorManager->CloseExternalFilesWithoutProcess();
  4772. if (CanClose)
  4773. {
  4774. while (CanClose && (FLocalEditors->Count > 0))
  4775. {
  4776. int PrevCount = FLocalEditors->Count;
  4777. static_cast<TForm *>(FLocalEditors->Items[0])->Close();
  4778. CanClose = (FLocalEditors->Count < PrevCount);
  4779. }
  4780. if (CanClose)
  4781. {
  4782. CanClose = FEditorManager->Empty(true);
  4783. if (!CanClose)
  4784. {
  4785. SetFocus();
  4786. CanClose =
  4787. (MessageDialog(
  4788. LoadStr(PENDING_EDITORS), qtWarning, qaIgnore | qaCancel, HELP_NONE) == qaIgnore);
  4789. }
  4790. }
  4791. }
  4792. }
  4793. }
  4794. //---------------------------------------------------------------------------
  4795. void __fastcall TCustomScpExplorerForm::CloseInternalEditor(TObject * Sender)
  4796. {
  4797. TForm * Form = dynamic_cast<TForm *>(Sender);
  4798. DebugAssert(Form != NULL);
  4799. Form->Close();
  4800. }
  4801. //---------------------------------------------------------------------------
  4802. void __fastcall TCustomScpExplorerForm::ForceCloseInternalEditor(TObject * Sender)
  4803. {
  4804. TForm * Form = dynamic_cast<TForm *>(Sender);
  4805. delete Form;
  4806. }
  4807. //---------------------------------------------------------------------------
  4808. void __fastcall TCustomScpExplorerForm::ForceCloseLocalEditors()
  4809. {
  4810. while (FLocalEditors->Count > 0)
  4811. {
  4812. delete static_cast<TForm *>(FLocalEditors->Items[0]);
  4813. }
  4814. }
  4815. //---------------------------------------------------------------------------
  4816. void __fastcall TCustomScpExplorerForm::RemoteDirViewDisplayProperties(
  4817. TObject *Sender)
  4818. {
  4819. TStrings *FileList = ((TUnixDirView*)Sender)->CreateFileList(True, False, NULL);
  4820. try
  4821. {
  4822. SetProperties(osRemote, FileList);
  4823. }
  4824. __finally
  4825. {
  4826. delete FileList;
  4827. }
  4828. }
  4829. //---------------------------------------------------------------------------
  4830. void __fastcall TCustomScpExplorerForm::ComponentShowing(Byte Component, bool value)
  4831. {
  4832. if (value)
  4833. {
  4834. if (Component == fcCustomCommandsBand)
  4835. {
  4836. UpdateCustomCommandsToolbar();
  4837. }
  4838. if (Component == fcQueueFileList)
  4839. {
  4840. DebugAssert(!QueueFileListSplitter->Visible);
  4841. AdjustQueueLayout();
  4842. UpdateQueueFileList();
  4843. }
  4844. }
  4845. }
  4846. //---------------------------------------------------------------------------
  4847. void __fastcall TCustomScpExplorerForm::AdjustQueueLayout()
  4848. {
  4849. int MinHeightLimit =
  4850. GetStaticQueuePanelComponentsHeight() +
  4851. QueueFileListSplitter->Height +
  4852. QueueFileList->Height +
  4853. GetMinQueueViewHeight();
  4854. if (QueuePanel->ClientHeight < MinHeightLimit)
  4855. {
  4856. QueuePanel->ClientHeight = MinHeightLimit;
  4857. }
  4858. }
  4859. //---------------------------------------------------------------------------
  4860. void __fastcall TCustomScpExplorerForm::SetComponentVisible(Byte Component, Boolean value)
  4861. {
  4862. TControl * Control = GetComponent(Component);
  4863. DebugAssert(Control);
  4864. bool Changed = (Control->Visible != value);
  4865. if (Changed)
  4866. {
  4867. ComponentShowing(Component, value);
  4868. TWinControl * WinControl = dynamic_cast<TWinControl*>(Control);
  4869. bool WasFocused = (WinControl != NULL) && (ActiveControl != NULL) &&
  4870. ((ActiveControl == WinControl) || (ActiveControl->Parent == WinControl));
  4871. if (value)
  4872. {
  4873. int RemainingHeight = Control->Parent->ClientHeight;
  4874. int RemainingWidth = Control->Parent->ClientWidth;
  4875. for (int i = 0; i < Control->Parent->ControlCount; i++)
  4876. {
  4877. TControl * ChildControl = Control->Parent->Controls[i];
  4878. if (ChildControl->Visible)
  4879. {
  4880. switch (ChildControl->Align)
  4881. {
  4882. case alTop:
  4883. case alBottom:
  4884. RemainingHeight -= ChildControl->Height;
  4885. break;
  4886. case alLeft:
  4887. case alRight:
  4888. RemainingWidth -= ChildControl->Width;
  4889. break;
  4890. }
  4891. }
  4892. }
  4893. int Reserve = ScaleByTextHeight(this, 32);
  4894. // queue in explorer, trees in commander
  4895. if (Control->Height > RemainingHeight - Reserve)
  4896. {
  4897. Control->Height = RemainingHeight / 2;
  4898. }
  4899. if (Control->Width > RemainingWidth - Reserve)
  4900. {
  4901. Control->Width = RemainingWidth / 2;
  4902. }
  4903. }
  4904. Control->Visible = value;
  4905. if (WasFocused && Visible)
  4906. {
  4907. DirView(osCurrent)->SetFocus();
  4908. }
  4909. FixControlsPlacement();
  4910. }
  4911. }
  4912. //---------------------------------------------------------------------------
  4913. bool __fastcall TCustomScpExplorerForm::GetComponentVisible(Byte Component)
  4914. {
  4915. TControl * Control = GetComponent(Component);
  4916. if (Control == NULL)
  4917. {
  4918. return false;
  4919. }
  4920. else
  4921. {
  4922. return Control->Visible;
  4923. }
  4924. }
  4925. //---------------------------------------------------------------------------
  4926. bool __fastcall TCustomScpExplorerForm::IsComponentPossible(Byte Component)
  4927. {
  4928. bool Result;
  4929. if ((Component == fcExplorerUpdatesBand) || (Component == fcCommanderUpdatesBand))
  4930. {
  4931. Result = !IsUWP();
  4932. }
  4933. else
  4934. {
  4935. Result = true;
  4936. }
  4937. return Result;
  4938. }
  4939. //---------------------------------------------------------------------------
  4940. void __fastcall TCustomScpExplorerForm::FixControlsPlacement()
  4941. {
  4942. if (DirView(osOther)->ItemFocused != NULL)
  4943. {
  4944. DirView(osOther)->ItemFocused->MakeVisible(false);
  4945. }
  4946. QueueSplitter->Visible = QueuePanel->Visible;
  4947. QueueFileListSplitter->Visible = QueueFileList->Visible;
  4948. RemotePanelSplitter->Visible = RemoteDrivePanel->Visible;
  4949. TControl * QueueControlsOrder[] =
  4950. { QueueDock, QueueView3, QueueFileListSplitter, QueueFileList };
  4951. SetVerticalControlsOrder(QueueControlsOrder, LENOF(QueueControlsOrder));
  4952. }
  4953. //---------------------------------------------------------------------------
  4954. TControl * __fastcall TCustomScpExplorerForm::GetComponent(Byte Component)
  4955. {
  4956. switch (Component) {
  4957. case fcStatusBar: return RemoteStatusBar;
  4958. case fcRemotePopup: return reinterpret_cast<TControl *>(NonVisualDataModule->RemoteFilePopup);
  4959. case fcQueueView: return QueuePanel;
  4960. case fcQueueToolbar: return QueueDock;
  4961. case fcRemoteTree: return RemoteDrivePanel;
  4962. case fcSessionsTabs: return SessionsPageControl;
  4963. case fcQueueFileList: return QueueFileList;
  4964. default: return NULL;
  4965. }
  4966. }
  4967. //---------------------------------------------------------------------------
  4968. void __fastcall TCustomScpExplorerForm::DirViewColumnRightClick(
  4969. TObject *Sender, TListColumn *Column, TPoint &Point)
  4970. {
  4971. DebugAssert(NonVisualDataModule && Column && Sender);
  4972. NonVisualDataModule->ListColumn = Column;
  4973. TPopupMenu * DirViewColumnMenu;
  4974. if (dynamic_cast<TUnixDirView *>(Sender) != NULL)
  4975. {
  4976. DirViewColumnMenu = NonVisualDataModule->RemoteDirViewColumnPopup;
  4977. NonVisualDataModule->RemoteSortByExtColumnPopupItem->Visible =
  4978. (Column->Index == uvName);
  4979. NonVisualDataModule->RemoteFormatSizeBytesPopupItem->Visible =
  4980. (Column->Index == uvSize);
  4981. }
  4982. else
  4983. {
  4984. DirViewColumnMenu = NonVisualDataModule->LocalDirViewColumnPopup;
  4985. NonVisualDataModule->LocalSortByExtColumnPopupItem->Visible =
  4986. (Column->Index == dvName);
  4987. NonVisualDataModule->LocalFormatSizeBytesPopupItem->Visible =
  4988. (Column->Index == dvSize);
  4989. }
  4990. TCustomListView * ListView = DebugNotNull(dynamic_cast<TCustomListView *>(Sender));
  4991. TPoint ScreenPoint = ListView->ClientToScreen(Point);
  4992. ScreenPoint.x -= GetScrollPos(ListView->Handle, SB_HORZ);
  4993. DirViewColumnMenu->Popup(ScreenPoint.x, ScreenPoint.y);
  4994. }
  4995. //---------------------------------------------------------------------------
  4996. void __fastcall TCustomScpExplorerForm::DirViewExecFile(
  4997. TObject *Sender, TListItem *Item, bool &AllowExec)
  4998. {
  4999. DoDirViewExecFile(Sender, Item, AllowExec);
  5000. }
  5001. //---------------------------------------------------------------------------
  5002. void __fastcall TCustomScpExplorerForm::DoDirViewExecFile(TObject * Sender,
  5003. TListItem * Item, bool & AllowExec)
  5004. {
  5005. DebugAssert(Sender && Item && Configuration);
  5006. DebugAssert(AllowExec);
  5007. TCustomDirView * ADirView = (TCustomDirView *)Sender;
  5008. bool Remote = (ADirView == DirView(osRemote)) && !IsSideLocalBrowser(osRemote);
  5009. bool ResolvedSymlinks = !Remote || Terminal->ResolvingSymlinks || Terminal->IsEncryptingFiles();
  5010. TOperationSide Side = (Remote ? osRemote : osLocal);
  5011. // Anything special is done on double click only (not on "open" indicated by FForceExecution),
  5012. // on files only (not directories)
  5013. // and only when symlinks are resolved (apply to remote panel only)
  5014. if (!ADirView->ItemIsDirectory(Item) &&
  5015. (ResolvedSymlinks || FForceExecution))
  5016. {
  5017. if ((WinConfiguration->DoubleClickAction != dcaOpen) &&
  5018. !FForceExecution && ResolvedSymlinks)
  5019. {
  5020. if (WinConfiguration->DoubleClickAction == dcaCopy)
  5021. {
  5022. // To counter the lock in DirViewBusy (called from TCustomDirView.DoExecute)
  5023. UnlockWindow();
  5024. try
  5025. {
  5026. ExecuteFileOperation(
  5027. foCopy, Side, true, !WinConfiguration->CopyOnDoubleClickConfirmation);
  5028. }
  5029. __finally
  5030. {
  5031. LockWindow();
  5032. }
  5033. AllowExec = false;
  5034. }
  5035. else if (WinConfiguration->DoubleClickAction == dcaEdit)
  5036. {
  5037. if (!Remote || !WinConfiguration->DisableOpenEdit)
  5038. {
  5039. ExecuteFile(Side, efDefaultEditor);
  5040. AllowExec = false;
  5041. }
  5042. }
  5043. else
  5044. {
  5045. DebugFail();
  5046. }
  5047. }
  5048. // if we have not done anything special, fall back to default behavior
  5049. if (AllowExec)
  5050. {
  5051. if (Remote && !WinConfiguration->DisableOpenEdit)
  5052. {
  5053. ExecuteFile(osRemote, efShell);
  5054. AllowExec = false;
  5055. }
  5056. }
  5057. }
  5058. }
  5059. //---------------------------------------------------------------------------
  5060. bool __fastcall TCustomScpExplorerForm::GetHasDirView(TOperationSide Side)
  5061. {
  5062. return ((Side == osRemote) || (Side == osCurrent));
  5063. }
  5064. //---------------------------------------------------------------------------
  5065. void __fastcall TCustomScpExplorerForm::CompareDirectories()
  5066. {
  5067. DebugFail();
  5068. }
  5069. //---------------------------------------------------------------------------
  5070. void __fastcall TCustomScpExplorerForm::SynchronizeDirectories()
  5071. {
  5072. DebugFail();
  5073. }
  5074. //---------------------------------------------------------------------------
  5075. bool __fastcall TCustomScpExplorerForm::DoSynchronizeDirectories(
  5076. UnicodeString & LocalDirectory, UnicodeString & RemoteDirectory, int UseDefaults)
  5077. {
  5078. TSynchronizeParamType Params;
  5079. Params.LocalDirectory = LocalDirectory;
  5080. Params.RemoteDirectory = RemoteDirectory;
  5081. int UnusedParams =
  5082. (GUIConfiguration->SynchronizeParams &
  5083. (TTerminal::spPreviewChanges | TTerminal::spTimestamp | TTerminal::spNotByTime | TTerminal::spBySize |
  5084. TTerminal::spCaseSensitive));
  5085. Params.Params = GUIConfiguration->SynchronizeParams & ~UnusedParams;
  5086. Params.Options = GUIConfiguration->SynchronizeOptions;
  5087. bool SaveSettings = false;
  5088. TSynchronizeController Controller(&DoSynchronize, &DoSynchronizeInvalid,
  5089. &DoSynchronizeTooManyDirectories);
  5090. DebugAssert(FSynchronizeController == NULL);
  5091. FSynchronizeController = &Controller;
  5092. bool Result;
  5093. try
  5094. {
  5095. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  5096. int CopyParamAttrs = Terminal->UsableCopyParamAttrs(0).Upload;
  5097. int Options =
  5098. FLAGMASK(SynchronizeAllowSelectedOnly(), soAllowSelectedOnly);
  5099. DebugAssert(FOnFeedSynchronizeError == NULL);
  5100. DebugAssert(FOnSynchronizeAbort == NULL);
  5101. DebugAssert(FSynchronizeTerminal == NULL);
  5102. FSynchronizeTerminal = Terminal;
  5103. Result = DoSynchronizeDialog(Params, &CopyParam, Controller.StartStop,
  5104. SaveSettings, Options, CopyParamAttrs, GetSynchronizeOptions, SynchronizeSessionLog,
  5105. FOnFeedSynchronizeError, FOnSynchronizeAbort, SynchronizeInNewWindow, UseDefaults);
  5106. if (Result)
  5107. {
  5108. if (SaveSettings)
  5109. {
  5110. GUIConfiguration->SynchronizeParams = Params.Params | UnusedParams;
  5111. GUIConfiguration->SynchronizeOptions = Params.Options;
  5112. }
  5113. else
  5114. {
  5115. if (FLAGSET(GUIConfiguration->SynchronizeOptions, soSynchronizeAsk) &&
  5116. FLAGCLEAR(Params.Options, soSynchronizeAsk) &&
  5117. FLAGSET(Params.Options, soSynchronize))
  5118. {
  5119. GUIConfiguration->SynchronizeOptions =
  5120. (GUIConfiguration->SynchronizeOptions & ~soSynchronizeAsk) |
  5121. soSynchronize;
  5122. }
  5123. }
  5124. LocalDirectory = Params.LocalDirectory;
  5125. RemoteDirectory = Params.RemoteDirectory;
  5126. }
  5127. }
  5128. __finally
  5129. {
  5130. FSynchronizeController = NULL;
  5131. DebugAssert(FOnFeedSynchronizeError == NULL);
  5132. FOnFeedSynchronizeError = NULL;
  5133. DebugAssert(FOnSynchronizeAbort == NULL);
  5134. FOnSynchronizeAbort = NULL;
  5135. FSynchronizeTerminal = NULL;
  5136. }
  5137. return Result;
  5138. }
  5139. //---------------------------------------------------------------------------
  5140. void __fastcall TCustomScpExplorerForm::DoSynchronize(
  5141. TSynchronizeController * /*Sender*/, const UnicodeString LocalDirectory,
  5142. const UnicodeString RemoteDirectory, const TCopyParamType & CopyParam,
  5143. const TSynchronizeParamType & Params, TSynchronizeChecklist ** Checklist,
  5144. TSynchronizeOptions * Options, bool Full)
  5145. {
  5146. // Keep-up-to-date mode
  5147. if (Terminal->Status == ssOpened)
  5148. {
  5149. try
  5150. {
  5151. int PParams = Params.Params;
  5152. if (!Full)
  5153. {
  5154. PParams |= TTerminal::spNoRecurse | TTerminal::spUseCache | TTerminal::spDelayProgress;
  5155. if (FLAGSET(Params.Options, soRecurse))
  5156. {
  5157. PParams |= TTerminal::spSubDirs;
  5158. }
  5159. }
  5160. else
  5161. {
  5162. // if keepuptodate is non-recursive,
  5163. // full sync before has to be non-recursive as well
  5164. if (FLAGCLEAR(Params.Options, soRecurse))
  5165. {
  5166. PParams |= TTerminal::spNoRecurse;
  5167. }
  5168. }
  5169. Synchronize(LocalDirectory, RemoteDirectory, smRemote, CopyParam,
  5170. PParams, Checklist, Options);
  5171. }
  5172. catch (Exception & E)
  5173. {
  5174. if (!Terminal->Active)
  5175. {
  5176. ShowExtendedExceptionEx(Terminal, &E);
  5177. // Do not abort the "keep up to date" when the session was succesfully reconnected.
  5178. if (!Terminal->Active)
  5179. {
  5180. throw;
  5181. }
  5182. }
  5183. else
  5184. {
  5185. if (FLAGSET(Params.Options, soContinueOnError) &&
  5186. DebugAlwaysTrue(FOnFeedSynchronizeError != NULL))
  5187. {
  5188. // noop, already logged in MoreMessageDialog
  5189. }
  5190. else
  5191. {
  5192. // We get mostly EAbort here, so this is noop.
  5193. // Exception is an error when listing directories while looking for differences.
  5194. ShowExtendedExceptionEx(Terminal, &E);
  5195. throw;
  5196. }
  5197. }
  5198. }
  5199. }
  5200. }
  5201. //---------------------------------------------------------------------------
  5202. void __fastcall TCustomScpExplorerForm::DoSynchronizeInvalid(
  5203. TSynchronizeController * /*Sender*/, const UnicodeString Directory,
  5204. const UnicodeString ErrorStr)
  5205. {
  5206. if (!Directory.IsEmpty())
  5207. {
  5208. SimpleErrorDialog(FMTLOAD(WATCH_ERROR_DIRECTORY, (Directory)), ErrorStr);
  5209. }
  5210. else
  5211. {
  5212. SimpleErrorDialog(LoadStr(WATCH_ERROR_GENERAL), ErrorStr);
  5213. }
  5214. }
  5215. //---------------------------------------------------------------------------
  5216. void __fastcall TCustomScpExplorerForm::DoSynchronizeTooManyDirectories(
  5217. TSynchronizeController * /*Sender*/, int & MaxDirectories)
  5218. {
  5219. if (MaxDirectories < GUIConfiguration->MaxWatchDirectories)
  5220. {
  5221. MaxDirectories = GUIConfiguration->MaxWatchDirectories;
  5222. }
  5223. else
  5224. {
  5225. TMessageParams Params(mpNeverAskAgainCheck);
  5226. unsigned int Result = MessageDialog(
  5227. FMTLOAD(TOO_MANY_WATCH_DIRECTORIES, (MaxDirectories, MaxDirectories)),
  5228. qtConfirmation, qaYes | qaNo, HELP_TOO_MANY_WATCH_DIRECTORIES, &Params);
  5229. if ((Result == qaYes) || (Result == qaNeverAskAgain))
  5230. {
  5231. MaxDirectories *= 2;
  5232. if (Result == qaNeverAskAgain)
  5233. {
  5234. GUIConfiguration->MaxWatchDirectories = MaxDirectories;
  5235. }
  5236. }
  5237. else
  5238. {
  5239. Abort();
  5240. }
  5241. }
  5242. }
  5243. //---------------------------------------------------------------------------
  5244. void __fastcall TCustomScpExplorerForm::Synchronize(const UnicodeString LocalDirectory,
  5245. const UnicodeString RemoteDirectory, TSynchronizeMode Mode,
  5246. const TCopyParamType & CopyParam, int Params, TSynchronizeChecklist ** Checklist,
  5247. TSynchronizeOptions * Options)
  5248. {
  5249. // Keep-up-to-date mode
  5250. DebugAssert(!FAutoOperation);
  5251. void * BatchStorage;
  5252. BatchStart(BatchStorage);
  5253. FAutoOperation = true;
  5254. bool AnyOperation = false;
  5255. TDateTime StartTime = Now();
  5256. TSynchronizeChecklist * AChecklist = NULL;
  5257. try
  5258. {
  5259. FSynchronizeProgressForm = new TSynchronizeProgressForm(Application, true, -1);
  5260. if (FLAGCLEAR(Params, TTerminal::spDelayProgress))
  5261. {
  5262. FSynchronizeProgressForm->Start();
  5263. }
  5264. AChecklist = Terminal->SynchronizeCollect(LocalDirectory, RemoteDirectory,
  5265. static_cast<TTerminal::TSynchronizeMode>(Mode),
  5266. &CopyParam, Params | TTerminal::spNoConfirmation, TerminalSynchronizeDirectory,
  5267. Options);
  5268. SetTaskbarListProgressState(TBPF_NOPROGRESS);
  5269. SAFE_DESTROY(FSynchronizeProgressForm);
  5270. AnyOperation = (AChecklist->CheckedCount > 0);
  5271. if (AnyOperation)
  5272. {
  5273. TSynchronizeProgress SynchronizeProgress(AChecklist);
  5274. CreateProgressForm(&SynchronizeProgress);
  5275. try
  5276. {
  5277. Terminal->SynchronizeApply(
  5278. AChecklist, &CopyParam, Params | TTerminal::spNoConfirmation, TerminalSynchronizeDirectory,
  5279. SynchronizeProcessedItem, NULL, NULL, NULL);
  5280. }
  5281. __finally
  5282. {
  5283. DestroyProgressForm();
  5284. }
  5285. }
  5286. }
  5287. __finally
  5288. {
  5289. if (Checklist == NULL)
  5290. {
  5291. delete AChecklist;
  5292. }
  5293. else
  5294. {
  5295. *Checklist = AChecklist;
  5296. }
  5297. FAutoOperation = false;
  5298. SAFE_DESTROY(FSynchronizeProgressForm);
  5299. BatchEnd(BatchStorage);
  5300. ReloadLocalDirectory();
  5301. if (AnyOperation)
  5302. {
  5303. OperationComplete(StartTime);
  5304. }
  5305. }
  5306. }
  5307. //---------------------------------------------------------------------------
  5308. bool __fastcall TCustomScpExplorerForm::SynchronizeAllowSelectedOnly()
  5309. {
  5310. // can be called from command line
  5311. return Visible &&
  5312. ((DirView(osRemote)->SelCount > 0) ||
  5313. (HasDirView[osLocal] && (DirView(osLocal)->SelCount > 0)));
  5314. }
  5315. //---------------------------------------------------------------------------
  5316. void __fastcall TCustomScpExplorerForm::SynchronizeSessionLog(const UnicodeString & Message)
  5317. {
  5318. LogSynchronizeEvent(Terminal, Message);
  5319. }
  5320. //---------------------------------------------------------------------------
  5321. void __fastcall TCustomScpExplorerForm::GetSynchronizeOptions(
  5322. int Params, TSynchronizeOptions & Options)
  5323. {
  5324. DebugAssert(!IsLocalBrowserMode());
  5325. if (FLAGSET(Params, TTerminal::spSelectedOnly) && SynchronizeAllowSelectedOnly())
  5326. {
  5327. Options.Filter = new TStringList();
  5328. Options.Filter->CaseSensitive = false;
  5329. Options.Filter->Duplicates = Types::dupAccept;
  5330. if (DirView(osRemote)->SelCount > 0)
  5331. {
  5332. DirView(osRemote)->CreateFileList(false, false, Options.Filter);
  5333. }
  5334. if (HasDirView[osLocal] && (DirView(osLocal)->SelCount > 0))
  5335. {
  5336. DirView(osLocal)->CreateFileList(false, false, Options.Filter);
  5337. }
  5338. Options.Filter->Sort();
  5339. }
  5340. }
  5341. //---------------------------------------------------------------------------
  5342. UnicodeString __fastcall TCustomScpExplorerForm::SerializeCopyParamForCommandLine(const TCopyParamType * CopyParams)
  5343. {
  5344. TCopyParamType Defaults;
  5345. std::unique_ptr<THierarchicalStorage> ConfigStorage(Configuration->CreateConfigStorage());
  5346. ConfigStorage->AccessMode = smRead;
  5347. if (ConfigStorage->OpenSubKey(Configuration->ConfigurationSubKey, false))
  5348. {
  5349. GUIConfiguration->LoadCopyParam(ConfigStorage.get(), &Defaults);
  5350. }
  5351. ConfigStorage.reset(NULL);
  5352. std::unique_ptr<TStringList> Options(new TStringList());
  5353. std::unique_ptr<TOptionsStorage> OptionsStorage(new TOptionsStorage(Options.get(), true));
  5354. CopyParams->Save(OptionsStorage.get(), &Defaults);
  5355. UnicodeString Result;
  5356. if (Options->Count > 0)
  5357. {
  5358. Result =
  5359. FORMAT(L" %s%s", (TProgramParams::FormatSwitch(RAWTRANSFERSETTINGS_SWITCH), StringsToParams(Options.get())));
  5360. }
  5361. return Result;
  5362. }
  5363. //---------------------------------------------------------------------------
  5364. void __fastcall TCustomScpExplorerForm::SynchronizeInNewWindow(
  5365. const TSynchronizeParamType & Params, const TCopyParamType * CopyParams)
  5366. {
  5367. UnicodeString SessionName = CreateHiddenDuplicateSession();
  5368. UnicodeString AdditionalParams =
  5369. FORMAT(L"%s%s %s // \"%s\" \"%s\" %d %d", (
  5370. TProgramParams::FormatSwitch(DEFAULTS_SWITCH),
  5371. SerializeCopyParamForCommandLine(CopyParams),
  5372. TProgramParams::FormatSwitch(KEEP_UP_TO_DATE_SWITCH),
  5373. Params.LocalDirectory, Params.RemoteDirectory, Params.Params, Params.Options));
  5374. ExecuteNewInstance(SessionName, AdditionalParams);
  5375. }
  5376. //---------------------------------------------------------------------------
  5377. struct TSynchronizeParams
  5378. {
  5379. UnicodeString LocalDirectory;
  5380. UnicodeString RemoteDirectory;
  5381. TSynchronizeMode Mode;
  5382. TSynchronizeChecklist * Checklist;
  5383. int Params;
  5384. TCopyParamType * CopyParam;
  5385. TDateTime CollectElapsed;
  5386. TDateTime * StartTime;
  5387. TProcessedSynchronizationChecklistItem OnProcessedItem;
  5388. };
  5389. //---------------------------------------------------------------------------
  5390. void __fastcall TCustomScpExplorerForm::FullSynchronize(
  5391. TSynchronizeParams & Params, TProcessedSynchronizationChecklistItem OnProcessedItem,
  5392. TUpdatedSynchronizationChecklistItems OnUpdatedSynchronizationChecklistItems)
  5393. {
  5394. DebugAssert(!FAutoOperation);
  5395. void * BatchStorage;
  5396. BatchStart(BatchStorage);
  5397. FAutoOperation = true;
  5398. TFileOperationStatistics Statistics;
  5399. TDateTime Start = Now();
  5400. try
  5401. {
  5402. Params.OnProcessedItem = OnProcessedItem;
  5403. TSynchronizeProgress SynchronizeProgress(Params.Checklist);
  5404. CreateProgressForm(&SynchronizeProgress);
  5405. Terminal->SynchronizeApply(
  5406. Params.Checklist, Params.CopyParam, Params.Params | TTerminal::spNoConfirmation,
  5407. TerminalSynchronizeDirectory, SynchronizeProcessedItem, OnUpdatedSynchronizationChecklistItems, &Params,
  5408. &Statistics);
  5409. }
  5410. __finally
  5411. {
  5412. Params.OnProcessedItem = NULL;
  5413. FAutoOperation = false;
  5414. DestroyProgressForm();
  5415. BatchEnd(BatchStorage);
  5416. ReloadLocalDirectory();
  5417. }
  5418. if (WinConfiguration->SynchronizeSummary)
  5419. {
  5420. UnicodeString Message = MainInstructions(LoadStr(SYNCHRONIZE_COMPLETE)) + L"\n";
  5421. // The statistics should be 0 anyway in this case
  5422. if (FLAGCLEAR(Params.Params, TTerminal::spTimestamp))
  5423. {
  5424. Message += L"\n";
  5425. if (Statistics.FilesUploaded > 0)
  5426. {
  5427. Message += FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 1), (FormatNumber(Statistics.FilesUploaded), FormatBytes(Statistics.TotalUploaded))) + L"\n";
  5428. }
  5429. if (Statistics.FilesDownloaded > 0)
  5430. {
  5431. Message += FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 2), (FormatNumber(Statistics.FilesDownloaded), FormatBytes(Statistics.TotalDownloaded))) + L"\n";
  5432. }
  5433. if (Statistics.FilesDeletedLocal > 0)
  5434. {
  5435. Message += FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 3), (FormatNumber(Statistics.FilesDeletedLocal))) + L"\n";
  5436. }
  5437. if (Statistics.FilesDeletedRemote > 0)
  5438. {
  5439. Message += FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 4), (FormatNumber(Statistics.FilesDeletedRemote))) + L"\n";
  5440. }
  5441. }
  5442. TDateTime Elapsed = (Now() - Start);
  5443. Message +=
  5444. L"\n" +
  5445. FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 5), (FormatDateTimeSpan(Configuration->TimeFormat, Params.CollectElapsed))) + L"\n" +
  5446. FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 6), (FormatDateTimeSpan(Configuration->TimeFormat, Elapsed)));
  5447. TMessageParams Params(mpNeverAskAgainCheck);
  5448. unsigned int Result = MessageDialog(Message, qtInformation, qaOK, HELP_NONE, &Params);
  5449. if (Result == qaNeverAskAgain)
  5450. {
  5451. WinConfiguration->SynchronizeSummary = false;
  5452. }
  5453. }
  5454. }
  5455. //---------------------------------------------------------------------------
  5456. void __fastcall TCustomScpExplorerForm::SynchronizeProcessedItem(void * Token, const TSynchronizeChecklist::TItem * ChecklistItem)
  5457. {
  5458. if (DebugAlwaysTrue(FProgressForm != NULL) && DebugAlwaysTrue(FProgressForm->SynchronizeProgress != NULL))
  5459. {
  5460. FProgressForm->SynchronizeProgress->ItemProcessed(ChecklistItem);
  5461. }
  5462. // Not set in keep-up-to-date mode - Synchronize() method
  5463. if (Token != NULL)
  5464. {
  5465. TSynchronizeParams & Params = *static_cast<TSynchronizeParams *>(Token);
  5466. if (Params.OnProcessedItem != NULL)
  5467. {
  5468. Params.OnProcessedItem(NULL, ChecklistItem);
  5469. }
  5470. }
  5471. }
  5472. //---------------------------------------------------------------------------
  5473. void __fastcall TCustomScpExplorerForm::DoFullSynchronize(
  5474. void * Token, TProcessedSynchronizationChecklistItem OnProcessedItem,
  5475. TUpdatedSynchronizationChecklistItems OnUpdatedSynchronizationChecklistItems)
  5476. {
  5477. TSynchronizeParams & Params = *static_cast<TSynchronizeParams *>(Token);
  5478. *Params.StartTime = Now();
  5479. FullSynchronize(Params, OnProcessedItem, OnUpdatedSynchronizationChecklistItems);
  5480. }
  5481. //---------------------------------------------------------------------------
  5482. void __fastcall TCustomScpExplorerForm::DoSynchronizeChecklistCalculateSize(
  5483. TSynchronizeChecklist * Checklist, const TSynchronizeChecklist::TItemList & Items, void * Token)
  5484. {
  5485. // terminal can be already closed (e.g. dropped connection)
  5486. if (Terminal != NULL)
  5487. {
  5488. TSynchronizeParams & Params = *static_cast<TSynchronizeParams *>(Token);
  5489. Terminal->SynchronizeChecklistCalculateSize(Checklist, Items, Params.CopyParam);
  5490. }
  5491. }
  5492. //---------------------------------------------------------------------------
  5493. void __fastcall TCustomScpExplorerForm::DoSynchronizeMove(
  5494. TOperationSide Side, const UnicodeString & FileName, const UnicodeString & NewFileName, TRemoteFile * RemoteFile)
  5495. {
  5496. DebugAssert(!IsLocalBrowserMode());
  5497. TAutoBatch AutoBatch(this);
  5498. TAutoFlag AutoOperationFlag(FAutoOperation);
  5499. if (Side == osRemote)
  5500. {
  5501. std::unique_ptr<TStrings> FileList(new TStringList());
  5502. FileList->AddObject(FileName, RemoteFile);
  5503. UnicodeString Target = UnixExtractFileDir(NewFileName);
  5504. UnicodeString FileMask = DelimitFileNameMask(UnixExtractFileName(NewFileName));
  5505. RemoteDirView->SaveSelection();
  5506. RemoteDirView->SaveSelectedNames();
  5507. try
  5508. {
  5509. Terminal->MoveFiles(FileList.get(), Target, FileMask);
  5510. }
  5511. catch(...)
  5512. {
  5513. RemoteDirView->DiscardSavedSelection();
  5514. throw;
  5515. }
  5516. RemoteDirView->RestoreSelection();
  5517. }
  5518. else if (DebugAlwaysTrue(Side == osLocal))
  5519. {
  5520. if (!MoveFile(FileName.c_str(), NewFileName.c_str()))
  5521. {
  5522. throw EOSExtException(FMTLOAD(RENAME_FILE_ERROR, (FileName, NewFileName)));
  5523. }
  5524. UnicodeString Directory = ExtractFileDir(FileName);
  5525. ReloadLocalDirectory(Directory);
  5526. UnicodeString NewDirectory = ExtractFileDir(NewFileName);
  5527. if (!SamePaths(Directory, NewDirectory))
  5528. {
  5529. ReloadLocalDirectory(NewDirectory);
  5530. }
  5531. }
  5532. }
  5533. //---------------------------------------------------------------------------
  5534. void __fastcall TCustomScpExplorerForm::DoSynchronizeBrowse(TOperationSide Side, TSynchronizeChecklist::TAction Action, const TSynchronizeChecklist::TItem * Item)
  5535. {
  5536. UnicodeString LocalPath = ExcludeTrailingBackslash(Item->Local.Directory);
  5537. if (Side == osLocal)
  5538. {
  5539. if (Action == TSynchronizeChecklist::saDownloadNew)
  5540. {
  5541. OpenFolderInExplorer(LocalPath);
  5542. }
  5543. else
  5544. {
  5545. OpenFileInExplorer(TPath::Combine(LocalPath, Item->GetFileName()));
  5546. }
  5547. }
  5548. else if (DebugAlwaysTrue(Side == osRemote))
  5549. {
  5550. // Similar to CreateHiddenDuplicateSession, except that it modifies the initial directories
  5551. std::unique_ptr<TSessionData> SessionData(CloneCurrentSessionData());
  5552. SessionData->RemoteDirectory = UnixExcludeTrailingBackslash(Item->Remote.Directory);
  5553. if (!LocalPath.IsEmpty())
  5554. {
  5555. SessionData->LocalDirectory = LocalPath;
  5556. }
  5557. UnicodeString SessionName = SaveHiddenDuplicateSession(SessionData.get());
  5558. ExecuteNewInstance(SessionName, FORMAT(L"%s=\"%s\"", (TProgramParams::FormatSwitch(BROWSE_SWITCH), Item->GetFileName())));
  5559. }
  5560. }
  5561. //---------------------------------------------------------------------------
  5562. void __fastcall TCustomScpExplorerForm::FullSynchronizeInNewWindow(
  5563. TSynchronizeMode Mode, int Params, const UnicodeString & LocalDirectory, const UnicodeString & RemoteDirectory,
  5564. const TCopyParamType * CopyParams)
  5565. {
  5566. UnicodeString SessionName = CreateHiddenDuplicateSession();
  5567. UnicodeString AdditionalParams =
  5568. FORMAT(L"%s%s %s // \"%s\" \"%s\" %d %d", (
  5569. TProgramParams::FormatSwitch(DEFAULTS_SWITCH),
  5570. SerializeCopyParamForCommandLine(CopyParams),
  5571. TProgramParams::FormatSwitch(SYNCHRONIZE_SWITCH),
  5572. LocalDirectory, RemoteDirectory, Mode, Params));
  5573. ExecuteNewInstance(SessionName, AdditionalParams);
  5574. }
  5575. //---------------------------------------------------------------------------
  5576. int __fastcall TCustomScpExplorerForm::DoFullSynchronizeDirectories(
  5577. UnicodeString & LocalDirectory, UnicodeString & RemoteDirectory,
  5578. TSynchronizeMode & Mode, int Params, bool & SaveMode, int UseDefaults)
  5579. {
  5580. int Result;
  5581. bool SaveSettings = false;
  5582. int Options =
  5583. FLAGMASK(!Terminal->IsCapable[fcTimestampChanging], fsoDisableTimestamp) |
  5584. FLAGMASK(SynchronizeAllowSelectedOnly(), fsoAllowSelectedOnly);
  5585. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  5586. TUsableCopyParamAttrs CopyParamAttrs = Terminal->UsableCopyParamAttrs(0);
  5587. bool Continue =
  5588. (UseDefaults == 0) ||
  5589. DoFullSynchronizeDialog(Mode, Params, LocalDirectory, RemoteDirectory,
  5590. &CopyParam, SaveSettings, SaveMode, Options, CopyParamAttrs, FullSynchronizeInNewWindow, UseDefaults);
  5591. if (Continue)
  5592. {
  5593. Configuration->Usage->Inc(L"Synchronizations");
  5594. CopyParam.IncludeFileMask.SetRoots(LocalDirectory, RemoteDirectory);
  5595. UpdateCopyParamCounters(CopyParam);
  5596. TSynchronizeOptions SynchronizeOptions;
  5597. GetSynchronizeOptions(Params, SynchronizeOptions);
  5598. if (SaveSettings)
  5599. {
  5600. GUIConfiguration->SynchronizeParams = Params;
  5601. }
  5602. else
  5603. {
  5604. SaveMode = false;
  5605. }
  5606. TDateTime StartTime = Now();
  5607. TSynchronizeChecklist * Checklist = NULL;
  5608. try
  5609. {
  5610. DebugAssert(!FAutoOperation);
  5611. FAutoOperation = true;
  5612. try
  5613. {
  5614. UnicodeString SessionKey = Terminal->SessionData->SessionKey;
  5615. std::unique_ptr<TStrings> DataList(Configuration->LoadDirectoryStatisticsCache(SessionKey, RemoteDirectory, CopyParam));
  5616. int Files = -1;
  5617. if (DataList->Count >= 1)
  5618. {
  5619. Files = StrToIntDef(DataList->Strings[0], Files);
  5620. }
  5621. else
  5622. {
  5623. DataList->Add(UnicodeString());
  5624. }
  5625. FSynchronizeProgressForm = new TSynchronizeProgressForm(Application, true, Files);
  5626. FSynchronizeProgressForm->Start();
  5627. Checklist = Terminal->SynchronizeCollect(LocalDirectory, RemoteDirectory,
  5628. static_cast<TTerminal::TSynchronizeMode>(Mode),
  5629. &CopyParam, Params | TTerminal::spNoConfirmation, TerminalSynchronizeDirectory,
  5630. &SynchronizeOptions);
  5631. if (Terminal->SessionData->CacheDirectories)
  5632. {
  5633. DataList->Strings[0] = IntToStr(SynchronizeOptions.Files);
  5634. Configuration->SaveDirectoryStatisticsCache(SessionKey, RemoteDirectory, CopyParam, DataList.get());
  5635. }
  5636. }
  5637. __finally
  5638. {
  5639. FAutoOperation = false;
  5640. SetTaskbarListProgressState(TBPF_NOPROGRESS);
  5641. SAFE_DESTROY(FSynchronizeProgressForm);
  5642. }
  5643. TSynchronizeParams SynchronizeParams;
  5644. SynchronizeParams.LocalDirectory = LocalDirectory;
  5645. SynchronizeParams.RemoteDirectory = RemoteDirectory;
  5646. SynchronizeParams.Mode = Mode;
  5647. SynchronizeParams.CopyParam = &CopyParam;
  5648. SynchronizeParams.Params = Params;
  5649. SynchronizeParams.Checklist = Checklist;
  5650. SynchronizeParams.CollectElapsed = (Now() - StartTime);
  5651. SynchronizeParams.StartTime = &StartTime;
  5652. SynchronizeParams.OnProcessedItem = NULL;
  5653. Result = Checklist->Count;
  5654. if (Checklist->Count > 0)
  5655. {
  5656. if (FLAGSET(Params, TTerminal::spPreviewChanges))
  5657. {
  5658. if (!DoSynchronizeChecklistDialog(
  5659. Checklist, Mode, Params, LocalDirectory, RemoteDirectory, CustomCommandMenu, DoFullSynchronize,
  5660. DoSynchronizeChecklistCalculateSize, DoSynchronizeMove, DoSynchronizeBrowse, &SynchronizeParams))
  5661. {
  5662. Result = -1;
  5663. }
  5664. }
  5665. else
  5666. {
  5667. FullSynchronize(SynchronizeParams, NULL, NULL);
  5668. }
  5669. }
  5670. else
  5671. {
  5672. UnicodeString Message = MainInstructions(LoadStr(COMPARE_NO_DIFFERENCES));
  5673. MessageDialog(Message, qtInformation, qaOK, HELP_SYNCHRONIZE_NO_DIFFERENCES);
  5674. }
  5675. }
  5676. __finally
  5677. {
  5678. delete Checklist;
  5679. }
  5680. OperationComplete(StartTime);
  5681. }
  5682. else
  5683. {
  5684. Result = -1;
  5685. }
  5686. return Result;
  5687. }
  5688. //---------------------------------------------------------------------------
  5689. void __fastcall TCustomScpExplorerForm::TerminalSynchronizeDirectory(
  5690. const UnicodeString & LocalDirectory, const UnicodeString & RemoteDirectory,
  5691. bool & Continue, bool Collect, const TSynchronizeOptions * Options)
  5692. {
  5693. if (Collect)
  5694. {
  5695. DebugAssert(FSynchronizeProgressForm != NULL);
  5696. if (!FSynchronizeProgressForm->Started)
  5697. {
  5698. FSynchronizeProgressForm->Start();
  5699. }
  5700. int CompareProgress = FSynchronizeProgressForm->SetData(LocalDirectory, RemoteDirectory, Options->Files, Continue);
  5701. SetTaskbarListProgressValue(CompareProgress);
  5702. }
  5703. else
  5704. {
  5705. DebugAssert(FSynchronizeProgressForm == NULL);
  5706. DebugAssert(FProgressForm != NULL);
  5707. }
  5708. }
  5709. //---------------------------------------------------------------------------
  5710. void __fastcall TCustomScpExplorerForm::StandaloneEdit(const UnicodeString & FileName)
  5711. {
  5712. UnicodeString FullFileName = AbsolutePath(FTerminal->CurrentDirectory, FileName);
  5713. TRemoteFile * File = NULL;
  5714. Terminal->ReadFile(FullFileName, File);
  5715. if (File != NULL)
  5716. {
  5717. std::unique_ptr<TRemoteFile> FileOwner(File);
  5718. ExecuteRemoteFile(FullFileName, File, efInternalEditor);
  5719. Application->ShowMainForm = false;
  5720. Application->Run();
  5721. }
  5722. }
  5723. //---------------------------------------------------------------------------
  5724. void __fastcall TCustomScpExplorerForm::ExploreLocalDirectory()
  5725. {
  5726. DebugFail();
  5727. }
  5728. //---------------------------------------------------------------------------
  5729. TSessionData * __fastcall TCustomScpExplorerForm::CloneCurrentSessionData()
  5730. {
  5731. std::unique_ptr<TSessionData> SessionData(new TSessionData(L""));
  5732. SessionData->Assign(Terminal->SessionData);
  5733. UpdateSessionData(SessionData.get());
  5734. if (Terminal->Active)
  5735. {
  5736. Terminal->UpdateSessionCredentials(SessionData.get());
  5737. }
  5738. if (!Terminal->SessionData->HasSessionName())
  5739. {
  5740. // Particularly for "Workspace/XXXX" name, we need to reset it, as it would become user-visible
  5741. // once IsWorkspace is cleared
  5742. SessionData->Name = UnicodeString();
  5743. }
  5744. // Most uses of this method ends with saving the session, and we do not want to store the IsWorkspace flag.
  5745. // Particularly, with SaveHiddenDuplicateSession, it won't work as TSessionData::ParseUrl explicitly ignored workspace sessions.
  5746. SessionData->IsWorkspace = false;
  5747. return SessionData.release();
  5748. }
  5749. //---------------------------------------------------------------------------
  5750. void __fastcall TCustomScpExplorerForm::SaveCurrentSession()
  5751. {
  5752. TSessionData * SessionData = CloneCurrentSessionData();
  5753. try
  5754. {
  5755. TSessionData * EditingSessionData = StoredSessions->FindSame(SessionData);
  5756. DoSaveSession(SessionData, EditingSessionData, true, NULL);
  5757. }
  5758. __finally
  5759. {
  5760. delete SessionData;
  5761. }
  5762. }
  5763. //---------------------------------------------------------------------------
  5764. TObjectList * __fastcall TCustomScpExplorerForm::DoCollectWorkspace()
  5765. {
  5766. TTerminalManager * Manager = TTerminalManager::Instance();
  5767. std::unique_ptr<TObjectList> DataList(new TObjectList());
  5768. if (DebugAlwaysTrue(Terminal != NULL))
  5769. {
  5770. // Update (Managed)Terminal->StateData
  5771. UpdateTerminal(Terminal);
  5772. }
  5773. Manager->SaveWorkspace(DataList.get());
  5774. return DataList.release();
  5775. }
  5776. //---------------------------------------------------------------------------
  5777. void __fastcall TCustomScpExplorerForm::DoSaveWorkspace(
  5778. const UnicodeString & Name, TObjectList * DataList, bool SavePasswords, bool Explicit)
  5779. {
  5780. WinConfiguration->LastWorkspace = Name;
  5781. if (!SavePasswords)
  5782. {
  5783. for (int Index = 0; Index < DataList->Count; Index++)
  5784. {
  5785. TSessionData * SessionData = dynamic_cast<TSessionData *>(DataList->Items[Index]);
  5786. if (SessionData->Link.IsEmpty())
  5787. {
  5788. SessionData->ClearSessionPasswords();
  5789. }
  5790. }
  5791. }
  5792. StoredSessions->NewWorkspace(Name, DataList);
  5793. // modified only
  5794. StoredSessions->Save(false, Explicit);
  5795. }
  5796. //---------------------------------------------------------------------------
  5797. UnicodeString __fastcall TCustomScpExplorerForm::WorkspaceName()
  5798. {
  5799. return
  5800. DefaultStr(WinConfiguration->LastWorkspace,
  5801. DefaultStr(WinConfiguration->AutoWorkspace, LoadStr(NEW_WORKSPACE)));
  5802. }
  5803. //---------------------------------------------------------------------------
  5804. bool __fastcall TCustomScpExplorerForm::SaveWorkspace(bool EnableAutoSave)
  5805. {
  5806. std::unique_ptr<TObjectList> DataList(DoCollectWorkspace());
  5807. bool AnyNonStoredSessionWithPassword = false;
  5808. bool AnyNonStoredNonWorkspaceSessionWithPassword = false;
  5809. bool AllNonStoredSessionsAnonymous = true;
  5810. TTerminalManager * Manager = TTerminalManager::Instance();
  5811. for (int Index = 0; Index < DataList->Count; Index++)
  5812. {
  5813. TSessionData * SessionData = dynamic_cast<TSessionData *>(DataList->Items[Index]);
  5814. if (SessionData->Link.IsEmpty())
  5815. {
  5816. if (SessionData->HasAnySessionPassword())
  5817. {
  5818. AnyNonStoredSessionWithPassword = true;
  5819. if (!Manager->Terminals[Index]->SessionData->IsWorkspace)
  5820. {
  5821. AnyNonStoredNonWorkspaceSessionWithPassword = true;
  5822. }
  5823. }
  5824. if (!SameText(SessionData->UserName, AnonymousUserName))
  5825. {
  5826. AllNonStoredSessionsAnonymous = false;
  5827. }
  5828. }
  5829. }
  5830. bool SavePasswords;
  5831. bool * PSavePasswords;
  5832. bool NotRecommendedSavingPasswords =
  5833. !CustomWinConfiguration->UseMasterPassword &&
  5834. !AllNonStoredSessionsAnonymous;
  5835. if (Configuration->DisablePasswordStoring ||
  5836. !AnyNonStoredSessionWithPassword)
  5837. {
  5838. PSavePasswords = NULL;
  5839. SavePasswords = false;
  5840. }
  5841. else
  5842. {
  5843. PSavePasswords = &SavePasswords;
  5844. SavePasswords =
  5845. !AnyNonStoredNonWorkspaceSessionWithPassword ||
  5846. !NotRecommendedSavingPasswords;
  5847. }
  5848. UnicodeString Name = WorkspaceName();
  5849. bool CreateShortcut = false;
  5850. bool Result =
  5851. DoSaveWorkspaceDialog(
  5852. Name, PSavePasswords, NotRecommendedSavingPasswords, CreateShortcut,
  5853. EnableAutoSave);
  5854. if (Result)
  5855. {
  5856. DoSaveWorkspace(Name, DataList.get(), SavePasswords, true);
  5857. if (CreateShortcut)
  5858. {
  5859. TOperationVisualizer Visualizer;
  5860. UnicodeString AdditionalParams = TProgramParams::FormatSwitch(DESKTOP_SWITCH);
  5861. CreateDesktopSessionShortCut(Name, L"", AdditionalParams, -1, WORKSPACE_ICON);
  5862. }
  5863. if (EnableAutoSave)
  5864. {
  5865. WinConfiguration->AutoSaveWorkspace = true;
  5866. WinConfiguration->AutoWorkspace = Name;
  5867. if (PSavePasswords != NULL)
  5868. {
  5869. WinConfiguration->AutoSaveWorkspacePasswords = SavePasswords;
  5870. }
  5871. }
  5872. }
  5873. return Result;
  5874. }
  5875. //---------------------------------------------------------------------------
  5876. void __fastcall TCustomScpExplorerForm::UpdateTerminal(TManagedTerminal * Terminal)
  5877. {
  5878. DebugAssert(!IsLocalBrowserMode());
  5879. SAFE_DESTROY(Terminal->RemoteExplorerState);
  5880. if (WinConfiguration->PreservePanelState)
  5881. {
  5882. Terminal->RemoteExplorerState = RemoteDirView->SaveState();
  5883. }
  5884. UpdateSessionData(Terminal->StateData);
  5885. }
  5886. //---------------------------------------------------------------------------
  5887. void __fastcall TCustomScpExplorerForm::UpdateSessionData(TSessionData * Data)
  5888. {
  5889. // Keep in sync with TSessionData::CopyStateData
  5890. DebugAssert(!IsLocalBrowserMode());
  5891. DebugAssert(Data != NULL);
  5892. // This is inconsistent with how color (for example) is handled.
  5893. Data->Name = Terminal->SessionData->Name;
  5894. // cannot use RemoteDirView->Path, because it is empty if connection
  5895. // was already closed
  5896. // also only peek, we may not be connected at all atm
  5897. // so make sure we do not try retrieving current directory from the server
  5898. // (particularly with FTP)
  5899. UnicodeString ACurrentDirectory = Terminal->PeekCurrentDirectory();
  5900. if (!ACurrentDirectory.IsEmpty())
  5901. {
  5902. Data->RemoteDirectory = ACurrentDirectory;
  5903. }
  5904. Data->Color = SessionColor;
  5905. }
  5906. //---------------------------------------------------------------------------
  5907. void __fastcall TCustomScpExplorerForm::ToolBarResize(TObject *Sender)
  5908. {
  5909. TTBXToolbar * Toolbar = dynamic_cast<TTBXToolbar*>(Sender);
  5910. DebugAssert(Toolbar != NULL);
  5911. for (int i = 0; i < Toolbar->Items->Count; i++)
  5912. {
  5913. TTBXCustomDropDownItem * DropDownItem;
  5914. DropDownItem = dynamic_cast<TTBXCustomDropDownItem *>(Toolbar->Items->Items[i]);
  5915. if (DropDownItem != NULL)
  5916. {
  5917. ToolbarItemResize(DropDownItem,
  5918. Toolbar->Width - (Toolbar->View->BaseSize.x - DropDownItem->EditWidth) -
  5919. Toolbar->NonClientWidth);
  5920. break;
  5921. }
  5922. }
  5923. }
  5924. //---------------------------------------------------------------------------
  5925. void __fastcall TCustomScpExplorerForm::ToolbarItemResize(TTBXCustomDropDownItem * Item, int Width)
  5926. {
  5927. Item->EditWidth = Width;
  5928. }
  5929. //---------------------------------------------------------------------------
  5930. void __fastcall TCustomScpExplorerForm::ToolbarGetBaseSize(
  5931. TTBCustomToolbar * Toolbar, TPoint & ASize)
  5932. {
  5933. for (int i = 0; i < Toolbar->Items->Count; i++)
  5934. {
  5935. TTBXCustomDropDownItem * DropDownItem;
  5936. DropDownItem = dynamic_cast<TTBXCustomDropDownItem *>(Toolbar->Items->Items[i]);
  5937. if (DropDownItem != NULL)
  5938. {
  5939. ASize.x -= DropDownItem->EditWidth;
  5940. ASize.x += ScaleByTextHeight(this, 50) /* minimal combo width */;
  5941. }
  5942. }
  5943. }
  5944. //---------------------------------------------------------------------------
  5945. void __fastcall TCustomScpExplorerForm::DoWarnLackOfTempSpace(
  5946. const UnicodeString Path, __int64 RequiredSpace, bool & Continue)
  5947. {
  5948. if (WinConfiguration->DDWarnLackOfTempSpace)
  5949. {
  5950. UnicodeString ADrive = ExtractFileDrive(ExpandFileName(Path));
  5951. if (!ADrive.IsEmpty())
  5952. {
  5953. __int64 FreeSpace = DiskFree((Byte)(ADrive[1]-'A'+1));
  5954. DebugAssert(RequiredSpace >= 0);
  5955. __int64 RequiredWithReserve;
  5956. RequiredWithReserve = (__int64)(RequiredSpace * WinConfiguration->DDWarnLackOfTempSpaceRatio);
  5957. if (FreeSpace < RequiredWithReserve)
  5958. {
  5959. unsigned int Result;
  5960. TMessageParams Params(mpNeverAskAgainCheck);
  5961. Result = MessageDialog(FMTLOAD(DD_WARN_LACK_OF_TEMP_SPACE, (Path,
  5962. FormatBytes(FreeSpace), FormatBytes(RequiredSpace))),
  5963. qtWarning, qaYes | qaNo, HELP_DD_WARN_LACK_OF_TEMP_SPACE, &Params);
  5964. if (Result == qaNeverAskAgain)
  5965. {
  5966. WinConfiguration->DDWarnLackOfTempSpace = false;
  5967. }
  5968. Continue = (Result == qaYes || Result == qaNeverAskAgain);
  5969. }
  5970. }
  5971. }
  5972. }
  5973. //---------------------------------------------------------------------------
  5974. void __fastcall TCustomScpExplorerForm::AddBookmark(TOperationSide Side)
  5975. {
  5976. DoOpenDirectoryDialog(odAddBookmark, Side);
  5977. }
  5978. //---------------------------------------------------------------------------
  5979. TStrings * __fastcall TCustomScpExplorerForm::CreateVisitedDirectories(TOperationSide Side)
  5980. {
  5981. // we should better use TCustomDirView::FCaseSensitive, but it is private
  5982. TStringList * VisitedDirectories = CreateSortedStringList(!IsSideLocalBrowser(Side));
  5983. try
  5984. {
  5985. TCustomDirView * DView = DirView(Side);
  5986. for (int Index = -DView->BackCount; Index <= DView->ForwardCount; Index++)
  5987. {
  5988. VisitedDirectories->Add(DView->HistoryPath[Index]);
  5989. }
  5990. }
  5991. catch (...)
  5992. {
  5993. delete VisitedDirectories;
  5994. throw;
  5995. }
  5996. return VisitedDirectories;
  5997. }
  5998. //---------------------------------------------------------------------------
  5999. void __fastcall TCustomScpExplorerForm::DoOpenDirectoryDialog(
  6000. TOpenDirectoryMode Mode, TOperationSide Side)
  6001. {
  6002. bool Continue = true;
  6003. if (Mode == odAddBookmark)
  6004. {
  6005. TMessageParams Params(mpNeverAskAgainCheck);
  6006. Params.NeverAskAgainTitle = LoadStr(ADD_BOOKMARK_SHARED);
  6007. Params.NeverAskAgainCheckedInitially = WinConfiguration->UseSharedBookmarks;
  6008. UnicodeString Message = MainInstructions(FMTLOAD(ADD_BOOKMARK_CONFIRM, (DirView(Side)->PathName)));
  6009. unsigned int Answer =
  6010. MessageDialog(Message,
  6011. qtConfirmation, qaOK | qaCancel, HELP_ADD_BOOKMARK_CONFIRM, &Params);
  6012. if (Answer == qaNeverAskAgain)
  6013. {
  6014. Continue = true;
  6015. WinConfiguration->UseSharedBookmarks = true;
  6016. }
  6017. else if (Answer == qaOK)
  6018. {
  6019. Continue = true;
  6020. WinConfiguration->UseSharedBookmarks = false;
  6021. }
  6022. else
  6023. {
  6024. Continue = false;
  6025. }
  6026. }
  6027. if (Continue)
  6028. {
  6029. TStrings * VisitedDirectories = CreateVisitedDirectories(Side);
  6030. try
  6031. {
  6032. UnicodeString Name = DirView(Side)->PathName;
  6033. bool Repeat;
  6034. do
  6035. {
  6036. Repeat = false;
  6037. if (::DoOpenDirectoryDialog(Mode, Side, Name, VisitedDirectories, Terminal,
  6038. // do not allow switching to location profiles,
  6039. // if we are not connected
  6040. HasDirView[osLocal] && (Terminal != NULL)))
  6041. {
  6042. TWindowLock Lock(this);
  6043. if (!TryOpenDirectory(Side, Name))
  6044. {
  6045. Repeat = true;
  6046. Mode = odBrowse;
  6047. }
  6048. }
  6049. }
  6050. while (Repeat);
  6051. }
  6052. __finally
  6053. {
  6054. delete VisitedDirectories;
  6055. }
  6056. }
  6057. }
  6058. //---------------------------------------------------------------------------
  6059. bool __fastcall TCustomScpExplorerForm::CommandSessionFallback()
  6060. {
  6061. DebugAssert(!FTerminal->CommandSessionOpened);
  6062. return TTerminalManager::Instance()->ConnectTerminal(FTerminal->CommandSession);
  6063. }
  6064. //---------------------------------------------------------------------------
  6065. bool __fastcall TCustomScpExplorerForm::EnsureCommandSessionFallback(TFSCapability Capability)
  6066. {
  6067. bool Result = FTerminal->IsCapable[Capability] ||
  6068. FTerminal->CommandSessionOpened;
  6069. if (!Result)
  6070. {
  6071. DebugAssert(FTerminal->IsCapable[fcSecondaryShell]);
  6072. if (!GUIConfiguration->ConfirmCommandSession)
  6073. {
  6074. Result = true;
  6075. }
  6076. else
  6077. {
  6078. TMessageParams Params(mpNeverAskAgainCheck);
  6079. const TFileSystemInfo & FileSystemInfo = Terminal->GetFileSystemInfo();
  6080. unsigned int Answer = MessageDialog(FMTLOAD(PERFORM_ON_COMMAND_SESSION2,
  6081. (FileSystemInfo.ProtocolName, FileSystemInfo.ProtocolName)), qtConfirmation,
  6082. qaOK | qaCancel, HELP_PERFORM_ON_COMMAND_SESSION, &Params);
  6083. if (Answer == qaNeverAskAgain)
  6084. {
  6085. GUIConfiguration->ConfirmCommandSession = false;
  6086. Result = true;
  6087. }
  6088. else if (Answer == qaOK)
  6089. {
  6090. Result = true;
  6091. }
  6092. }
  6093. if (Result)
  6094. {
  6095. Result = CommandSessionFallback();
  6096. }
  6097. }
  6098. return Result;
  6099. }
  6100. //---------------------------------------------------------------------------
  6101. void __fastcall TCustomScpExplorerForm::OpenConsole(UnicodeString Command)
  6102. {
  6103. if (EnsureCommandSessionFallback(fcAnyCommand))
  6104. {
  6105. DoConsoleDialog(Terminal, Command);
  6106. }
  6107. }
  6108. //---------------------------------------------------------------------------
  6109. void __fastcall TCustomScpExplorerForm::FileControlDDDragEnter(
  6110. TObject *Sender, _di_IDataObject /*DataObj*/, int /*grfKeyState*/,
  6111. const TPoint & /*Point*/, int & /*dwEffect*/, bool & Accept)
  6112. {
  6113. if (!IsSideLocalBrowser(osRemote) &&
  6114. IsFileControl(DropSourceControl, osRemote) &&
  6115. (FDDExtMapFile != NULL))
  6116. {
  6117. Accept = true;
  6118. }
  6119. FDDTargetControl = dynamic_cast<TControl*>(Sender);
  6120. DebugAssert(FDDTargetControl != NULL);
  6121. }
  6122. //---------------------------------------------------------------------------
  6123. void __fastcall TCustomScpExplorerForm::SessionsDDDragEnter(
  6124. _di_IDataObject DataObj, int KeyState,
  6125. const TPoint & Point, int & Effect, bool & Accept)
  6126. {
  6127. FileControlDDDragEnter(SessionsPageControl, DataObj, KeyState, Point, Effect, Accept);
  6128. }
  6129. //---------------------------------------------------------------------------
  6130. void __fastcall TCustomScpExplorerForm::QueueDDDragEnter(
  6131. _di_IDataObject DataObj, int KeyState,
  6132. const TPoint & Point, int & Effect, bool & Accept)
  6133. {
  6134. FileControlDDDragEnter(QueueView3, DataObj, KeyState, Point, Effect, Accept);
  6135. }
  6136. //---------------------------------------------------------------------------
  6137. void __fastcall TCustomScpExplorerForm::FileControlDDDragLeave(
  6138. TObject * Sender)
  6139. {
  6140. DebugUsedParam(Sender);
  6141. DebugAssert(FDDTargetControl == Sender);
  6142. FDDTargetControl = NULL;
  6143. }
  6144. //---------------------------------------------------------------------------
  6145. void __fastcall TCustomScpExplorerForm::SessionsDDDragLeave()
  6146. {
  6147. FileControlDDDragLeave(SessionsPageControl);
  6148. }
  6149. //---------------------------------------------------------------------------
  6150. void __fastcall TCustomScpExplorerForm::QueueDDDragLeave()
  6151. {
  6152. FileControlDDDragLeave(QueueView3);
  6153. }
  6154. //---------------------------------------------------------------------------
  6155. void __fastcall TCustomScpExplorerForm::AddEditLink(TOperationSide Side, bool Add)
  6156. {
  6157. DebugAssert(!IsSideLocalBrowser(Side));
  6158. DebugUsedParam(Side);
  6159. bool Edit = false;
  6160. TRemoteFile * File = NULL;
  6161. UnicodeString FileName;
  6162. UnicodeString PointTo;
  6163. bool SymbolicLink = true;
  6164. if (RemoteDirView->ItemFocused)
  6165. {
  6166. DebugAssert(RemoteDirView->ItemFocused->Data);
  6167. File = (TRemoteFile *)RemoteDirView->ItemFocused->Data;
  6168. Edit = !Add && File->IsSymLink && Terminal->SessionData->ResolveSymlinks;
  6169. if (Edit)
  6170. {
  6171. FileName = File->FileName;
  6172. PointTo = File->LinkTo;
  6173. }
  6174. else
  6175. {
  6176. PointTo = File->FileName;
  6177. }
  6178. }
  6179. if (DoSymlinkDialog(FileName, PointTo, osRemote, SymbolicLink, Edit,
  6180. Terminal->IsCapable[fcHardLink]))
  6181. {
  6182. Configuration->Usage->Inc(L"RemoteLinksCreated");
  6183. if (Edit)
  6184. {
  6185. DebugAssert(File->FileName == FileName);
  6186. int Params = dfNoRecursive;
  6187. Terminal->ExceptionOnFail = true;
  6188. try
  6189. {
  6190. Terminal->DeleteFile(L"", File, &Params);
  6191. }
  6192. __finally
  6193. {
  6194. Terminal->ExceptionOnFail = false;
  6195. }
  6196. }
  6197. Terminal->CreateLink(FileName, PointTo, SymbolicLink);
  6198. }
  6199. }
  6200. //---------------------------------------------------------------------------
  6201. bool __fastcall TCustomScpExplorerForm::CanAddEditLink(TOperationSide Side)
  6202. {
  6203. return
  6204. (IsSideLocalBrowser(Side) ||
  6205. (HasActiveTerminal() &&
  6206. Terminal->ResolvingSymlinks &&
  6207. Terminal->IsCapable[fcSymbolicLink]));
  6208. }
  6209. //---------------------------------------------------------------------------
  6210. bool __fastcall TCustomScpExplorerForm::LinkFocused()
  6211. {
  6212. return
  6213. !IsSideLocalBrowser(FCurrentSide) &&
  6214. (RemoteDirView->ItemFocused != NULL) &&
  6215. ((TRemoteFile *)RemoteDirView->ItemFocused->Data)->IsSymLink &&
  6216. Terminal->SessionData->ResolveSymlinks;
  6217. }
  6218. //---------------------------------------------------------------------------
  6219. void __fastcall TCustomScpExplorerForm::ExecuteCurrentFile()
  6220. {
  6221. DebugAssert(!WinConfiguration->DisableOpenEdit);
  6222. FForceExecution = true;
  6223. try
  6224. {
  6225. DirView(osCurrent)->ExecuteCurrentFile();
  6226. }
  6227. __finally
  6228. {
  6229. FForceExecution = false;
  6230. }
  6231. }
  6232. //---------------------------------------------------------------------------
  6233. void __fastcall TCustomScpExplorerForm::ExecuteCurrentFileWith(bool OnFocused)
  6234. {
  6235. TEditorData ExternalEditor;
  6236. ExternalEditor.Editor = edExternal;
  6237. bool Remember = false;
  6238. if (DoEditorPreferencesDialog(&ExternalEditor, Remember, epmAdHoc, !IsSideLocalBrowser(osCurrent)))
  6239. {
  6240. if (Remember)
  6241. {
  6242. TEditorList * EditorList = new TEditorList;
  6243. try
  6244. {
  6245. *EditorList = *WinConfiguration->EditorList;
  6246. bool Found = false;
  6247. int i = 0;
  6248. while (!Found && (i < EditorList->Count))
  6249. {
  6250. const TEditorPreferences * Editor = EditorList->Editors[i];
  6251. if ((Editor->Data->Editor == edExternal) &&
  6252. (Editor->Data->ExternalEditor == ExternalEditor.ExternalEditor))
  6253. {
  6254. Found = true;
  6255. }
  6256. i++;
  6257. }
  6258. if (!Found)
  6259. {
  6260. EditorList->Add(new TEditorPreferences(ExternalEditor));
  6261. WinConfiguration->EditorList = EditorList;
  6262. }
  6263. }
  6264. __finally
  6265. {
  6266. delete EditorList;
  6267. }
  6268. }
  6269. ExecuteFile(osCurrent, efExternalEditor, &ExternalEditor, true, OnFocused);
  6270. }
  6271. }
  6272. //---------------------------------------------------------------------------
  6273. void __fastcall TCustomScpExplorerForm::DetachTerminal(TObject * ATerminal)
  6274. {
  6275. FEditorManager->ProcessFiles(FileTerminalRemoved, ATerminal);
  6276. if (FFileFindTerminal == ATerminal)
  6277. {
  6278. FFileFindTerminal = NULL;
  6279. HideFileFindDialog();
  6280. }
  6281. if (FClipboardTerminal == ATerminal)
  6282. {
  6283. ClipboardClear(); // implies ClipboardStop
  6284. }
  6285. if ((FOnSynchronizeAbort != NULL) &&
  6286. (ATerminal == FTerminal))
  6287. {
  6288. FOnSynchronizeAbort(NULL);
  6289. }
  6290. }
  6291. //---------------------------------------------------------------------------
  6292. void __fastcall TCustomScpExplorerForm::TerminalRemoved(TObject * Sender)
  6293. {
  6294. DetachTerminal(Sender);
  6295. }
  6296. //---------------------------------------------------------------------------
  6297. void __fastcall TCustomScpExplorerForm::FileTerminalRemoved(const UnicodeString FileName,
  6298. TEditedFileData * Data, TObject * /*Token*/, void * Arg)
  6299. {
  6300. TTerminal * Terminal = static_cast<TTerminal *>(Arg);
  6301. DebugAssert(Terminal != NULL);
  6302. if (Data->Terminal == Terminal)
  6303. {
  6304. Data->Terminal = NULL;
  6305. }
  6306. }
  6307. //---------------------------------------------------------------------------
  6308. void __fastcall TCustomScpExplorerForm::LastTerminalClosed(TObject * /*Sender*/)
  6309. {
  6310. UpdateControls();
  6311. SessionColor = TColor(0);
  6312. UpdateRemotePathComboBox(false);
  6313. NeedSession(false);
  6314. }
  6315. //---------------------------------------------------------------------------
  6316. void __fastcall TCustomScpExplorerForm::NeedSession(bool Startup)
  6317. {
  6318. try
  6319. {
  6320. // Cache, as the login dialog can change its value
  6321. // See also DoShow. Also see TTerminalManager::NewSession.
  6322. bool ShowLogin = WinConfiguration->ShowLoginWhenNoSession;
  6323. try
  6324. {
  6325. if (ShowLogin)
  6326. {
  6327. bool ReloadSessions = !Startup;
  6328. TTerminalManager::Instance()->NewSession(L"", ReloadSessions, this);
  6329. }
  6330. else if (Startup && WinConfiguration->AutoSaveWorkspace && !WinConfiguration->AutoWorkspace.IsEmpty() &&
  6331. // This detects if workspace was saved the last time the main widow was closed
  6332. SameText(WinConfiguration->LastStoredSession, WinConfiguration->AutoWorkspace))
  6333. {
  6334. DoOpenFolderOrWorkspace(WinConfiguration->AutoWorkspace, false, true);
  6335. Configuration->Usage->Inc(L"OpenedWorkspacesAuto");
  6336. }
  6337. }
  6338. __finally
  6339. {
  6340. // Do not terminate, if we are only starting up and we are not showing Login dialog
  6341. // (so there was no chance for the user to open any session yet)
  6342. if ((ShowLogin || !Startup) &&
  6343. !WinConfiguration->KeepOpenWhenNoSession &&
  6344. ((Terminal == NULL) || (!Terminal->Active && !Terminal->Permanent)))
  6345. {
  6346. TerminateApplication();
  6347. }
  6348. }
  6349. }
  6350. catch (EAbort &)
  6351. {
  6352. // swallow
  6353. // The TTerminalManager does not expect the OnLastTerminalClose to throw without trying to connect.
  6354. // Also when called from TWinControl.UpdateShowing => CMShowingChanged, the showing is aborted on exception
  6355. }
  6356. }
  6357. //---------------------------------------------------------------------------
  6358. void __fastcall TCustomScpExplorerForm::DoTerminalListChanged()
  6359. {
  6360. TStrings * TerminalList = TTerminalManager::Instance()->TerminalList;
  6361. int ActiveTerminalIndex = TTerminalManager::Instance()->ActiveTerminalIndex;
  6362. Configuration->Usage->SetMax(L"MaxOpenedSessions", TerminalList->Count);
  6363. SendMessage(SessionsPageControl->Handle, WM_SETREDRAW, 0, 0);
  6364. try
  6365. {
  6366. while ((SessionsPageControl->PageCount > TerminalList->Count + 1) ||
  6367. // Clear the design time unthemed tab
  6368. ((SessionsPageControl->PageCount > 0) &&
  6369. (dynamic_cast<TThemeTabSheet *>(SessionsPageControl->Pages[SessionsPageControl->PageCount - 1]) == NULL)))
  6370. {
  6371. delete SessionsPageControl->Pages[SessionsPageControl->PageCount - 1];
  6372. }
  6373. for (int Index = 0; Index <= TerminalList->Count; Index++)
  6374. {
  6375. TThemeTabSheet * TabSheet;
  6376. if (Index >= SessionsPageControl->PageCount)
  6377. {
  6378. TabSheet = new TThemeTabSheet(SessionsPageControl);
  6379. TabSheet->PageControl = SessionsPageControl;
  6380. }
  6381. else
  6382. {
  6383. TabSheet = DebugNotNull(dynamic_cast<TThemeTabSheet *>(SessionsPageControl->Pages[Index]));
  6384. }
  6385. bool IsSessionTab = (Index < TerminalList->Count);
  6386. if (IsSessionTab)
  6387. {
  6388. TTerminal * Terminal = dynamic_cast<TTerminal *>(TerminalList->Objects[Index]);
  6389. TabSheet->Tag = reinterpret_cast<int>(Terminal);
  6390. UpdateSessionTab(TabSheet);
  6391. }
  6392. else
  6393. {
  6394. TabSheet->ImageIndex = FNewSessionTabImageIndex;
  6395. TabSheet->Tag = 0; // not really needed
  6396. TabSheet->Shadowed = false;
  6397. // 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)
  6398. UpdateNewSessionTab();
  6399. }
  6400. TabSheet->ShowCloseButton = IsSessionTab;
  6401. }
  6402. }
  6403. __finally
  6404. {
  6405. SendMessage(SessionsPageControl->Handle, WM_SETREDRAW, 1, 0);
  6406. }
  6407. SessionsPageControl->ActivePageIndex = ActiveTerminalIndex;
  6408. }
  6409. //---------------------------------------------------------------------------
  6410. void __fastcall TCustomScpExplorerForm::TerminalListChanged(TObject * /*Sender*/)
  6411. {
  6412. DoTerminalListChanged();
  6413. }
  6414. //---------------------------------------------------------------------------
  6415. void __fastcall TCustomScpExplorerForm::UpdateNewSessionTab()
  6416. {
  6417. TTabSheet * TabSheet = SessionsPageControl->Pages[SessionsPageControl->PageCount - 1];
  6418. DebugAssert(TabSheet->ImageIndex == 0);
  6419. TabSheet->Caption =
  6420. WinConfiguration->SelectiveToolbarText ?
  6421. StripHotkey(StripTrailingPunctuation(NonVisualDataModule->NewSessionAction->Caption)) :
  6422. UnicodeString();
  6423. }
  6424. //---------------------------------------------------------------------------
  6425. TManagedTerminal * __fastcall TCustomScpExplorerForm::GetSessionTabTerminal(TTabSheet * TabSheet)
  6426. {
  6427. return reinterpret_cast<TManagedTerminal *>(TabSheet->Tag);
  6428. }
  6429. //---------------------------------------------------------------------------
  6430. void __fastcall TCustomScpExplorerForm::UpdateSessionTab(TTabSheet * TabSheet)
  6431. {
  6432. if (DebugAlwaysTrue(TabSheet != NULL))
  6433. {
  6434. TManagedTerminal * ATerminal = GetSessionTabTerminal(TabSheet);
  6435. if (DebugAlwaysTrue(ATerminal != NULL))
  6436. {
  6437. TColor Color = (ATerminal == FTerminal) ? FSessionColor : ATerminal->StateData->Color;
  6438. TabSheet->ImageIndex = AddSessionColor(Color);
  6439. UnicodeString TabCaption = TTerminalManager::Instance()->GetTerminalTitle(ATerminal, true);
  6440. TabSheet->Caption = SessionsPageControl->FormatCaptionWithCloseButton(TabCaption);
  6441. TThemeTabSheet * ThemeTabSheet = dynamic_cast<TThemeTabSheet *>(TabSheet);
  6442. if (DebugAlwaysTrue(ThemeTabSheet != NULL))
  6443. {
  6444. ThemeTabSheet->Shadowed = !ATerminal->Active;
  6445. }
  6446. }
  6447. }
  6448. }
  6449. //---------------------------------------------------------------------------
  6450. bool __fastcall TCustomScpExplorerForm::SessionTabSwitched()
  6451. {
  6452. DebugAssert(SessionsPageControl->ActivePage != NULL);
  6453. TManagedTerminal * Terminal = GetSessionTabTerminal(SessionsPageControl->ActivePage);
  6454. bool Result = (Terminal != NULL);
  6455. if (Result)
  6456. {
  6457. TTerminalManager::Instance()->ActiveTerminal = Terminal;
  6458. }
  6459. else
  6460. {
  6461. try
  6462. {
  6463. NewSession();
  6464. }
  6465. __finally
  6466. {
  6467. DoTerminalListChanged();
  6468. }
  6469. FSessionsPageControlNewSessionTime = Now();
  6470. }
  6471. return Result;
  6472. }
  6473. //---------------------------------------------------------------------------
  6474. void __fastcall TCustomScpExplorerForm::SessionsPageControlChange(TObject * /*Sender*/)
  6475. {
  6476. SessionTabSwitched();
  6477. }
  6478. //---------------------------------------------------------------------------
  6479. void __fastcall TCustomScpExplorerForm::TransferListChange(TObject * Sender)
  6480. {
  6481. TTBXStringList * TransferList = dynamic_cast<TTBXStringList *>(Sender);
  6482. DebugAssert(TransferList != NULL);
  6483. UnicodeString Name;
  6484. if (TransferList->ItemIndex <= 0)
  6485. {
  6486. Name = L"";
  6487. }
  6488. else
  6489. {
  6490. Name = GUIConfiguration->CopyParamList->Names[TransferList->ItemIndex - 1];
  6491. }
  6492. if (FCopyParamAutoSelected.IsEmpty())
  6493. {
  6494. // if previous preset was not autoselected, make new preset the "default"
  6495. FCopyParamDefault = Name;
  6496. }
  6497. GUIConfiguration->CopyParamCurrent = Name;
  6498. }
  6499. //---------------------------------------------------------------------------
  6500. void __fastcall TCustomScpExplorerForm::UpdateTransferLabel()
  6501. {
  6502. // sanity check
  6503. bool ExistingPreset =
  6504. (FTransferListHoverIndex >= 0) &&
  6505. (FTransferListHoverIndex < 1 + GUIConfiguration->CopyParamList->Count);
  6506. DebugAssert(ExistingPreset);
  6507. if (ExistingPreset)
  6508. {
  6509. HDC DC = GetDC(0);
  6510. TCanvas * Canvas = new TCanvas();
  6511. try
  6512. {
  6513. Canvas->Handle = DC;
  6514. Canvas->Font = GetToolbarFont(this);
  6515. UnicodeString Name;
  6516. if (FTransferListHoverIndex == 0)
  6517. {
  6518. Name = L"";
  6519. }
  6520. else
  6521. {
  6522. Name = GUIConfiguration->CopyParamList->Names[FTransferListHoverIndex - 1];
  6523. }
  6524. TTBXLabelItem * TransferLabel = dynamic_cast<TTBXLabelItem*>(
  6525. static_cast<TComponent*>(GetComponent(fcTransferLabel)));
  6526. TTBXStringList * TransferList = dynamic_cast<TTBXStringList*>(
  6527. static_cast<TObject*>(GetComponent(fcTransferList)));
  6528. UnicodeString InfoStr =
  6529. GUIConfiguration->CopyParamPreset[Name].
  6530. GetInfoStr(L"; ",
  6531. FLAGMASK(HasActiveTerminal(), Terminal->UsableCopyParamAttrs(0).General));
  6532. int MaxWidth = TransferList->MinWidth - (2 * TransferLabel->Margin) - ScaleByTextHeight(this, 10);
  6533. if (Canvas->TextExtent(InfoStr).cx > MaxWidth)
  6534. {
  6535. while (Canvas->TextExtent(InfoStr + Ellipsis).cx > MaxWidth)
  6536. {
  6537. InfoStr.SetLength(InfoStr.Length() - 1);
  6538. }
  6539. InfoStr += Ellipsis;
  6540. }
  6541. // UpdateCaption does not cause invalidation of whole submenu, while
  6542. // setting Caption property does.
  6543. // also it probably does not resize the label, even if necessary
  6544. // (we do not want that anyway)
  6545. TransferLabel->UpdateCaption(InfoStr);
  6546. }
  6547. __finally
  6548. {
  6549. Canvas->Handle = NULL;
  6550. ReleaseDC(0, DC);
  6551. delete Canvas;
  6552. }
  6553. }
  6554. }
  6555. //---------------------------------------------------------------------------
  6556. void __fastcall TCustomScpExplorerForm::TransferListDrawItem(
  6557. TTBXCustomList * /*Sender*/, TCanvas * /*ACanvas*/, const TRect & /*ARect*/,
  6558. int /*AIndex*/, int AHoverIndex, bool & /*DrawDefault*/)
  6559. {
  6560. if (FTransferListHoverIndex != AHoverIndex)
  6561. {
  6562. FTransferListHoverIndex = AHoverIndex;
  6563. UpdateTransferLabel();
  6564. }
  6565. }
  6566. //---------------------------------------------------------------------------
  6567. void __fastcall TCustomScpExplorerForm::WMAppCommand(TMessage & Message)
  6568. {
  6569. int Command = GET_APPCOMMAND_LPARAM(Message.LParam);
  6570. TShiftState Shift = KeyDataToShiftState(GET_KEYSTATE_LPARAM(Message.LParam));
  6571. if ((Shift * (TShiftState() << ssShift << ssAlt << ssCtrl)).Empty())
  6572. {
  6573. if (Command == APPCOMMAND_BROWSER_FAVORITES)
  6574. {
  6575. if (!NonVisualDataModule->Busy)
  6576. {
  6577. OpenDirectory(GetSide(osCurrent));
  6578. }
  6579. Message.Result = 1;
  6580. }
  6581. else
  6582. {
  6583. TForm::Dispatch(&Message);
  6584. }
  6585. }
  6586. else
  6587. {
  6588. TForm::Dispatch(&Message);
  6589. }
  6590. }
  6591. //---------------------------------------------------------------------------
  6592. void __fastcall TCustomScpExplorerForm::CMDialogChar(TMessage & AMessage)
  6593. {
  6594. TCMDialogChar & Message = reinterpret_cast<TCMDialogChar &>(AMessage);
  6595. if ((FIgnoreNextDialogChar != 0) &&
  6596. (toupper(Message.CharCode) == toupper(FIgnoreNextDialogChar)))
  6597. {
  6598. Message.Result = 1;
  6599. }
  6600. else
  6601. {
  6602. TForm::Dispatch(&Message);
  6603. }
  6604. FIgnoreNextDialogChar = 0;
  6605. }
  6606. //---------------------------------------------------------------------------
  6607. void __fastcall TCustomScpExplorerForm::WMSysCommand(TMessage & Message)
  6608. {
  6609. // The four low-order bits are used internally by Windows
  6610. unsigned int Cmd = (Message.WParam & 0xFFF0);
  6611. // SC_RESTORE, SC_MAXIMIZE, SC_MINIMIZE - buttons on windows title
  6612. // SC_DEFAULT - double click on windows title (does not work, at least on WinXP)
  6613. // 61730 - restore thru double click - undocumented
  6614. // 61490 - maximize thru double click - undocumented
  6615. if ((Cmd == SC_RESTORE) || (Cmd == SC_MAXIMIZE) ||
  6616. (Cmd == SC_MINIMIZE) || (Cmd == SC_DEFAULT))
  6617. {
  6618. SysResizing(Cmd);
  6619. }
  6620. TForm::Dispatch(&Message);
  6621. }
  6622. //---------------------------------------------------------------------------
  6623. void __fastcall TCustomScpExplorerForm::WMQueryEndSession(TMessage & Message)
  6624. {
  6625. // We were actually never able to make ENDSESSION_CRITICAL happen.
  6626. // Also there no point returning TRUE as we are not able to
  6627. // handle the abrupt termination caused by subsequent WM_ENDSESSION cleanly.
  6628. // Hence the process termination might be safer :)
  6629. if ((Message.LParam != ENDSESSION_CRITICAL) &&
  6630. (((FQueue != NULL) && !FQueue->IsEmpty) || (FProgressForm != NULL)))
  6631. {
  6632. Message.Result = FALSE;
  6633. }
  6634. else
  6635. {
  6636. Message.Result = TRUE;
  6637. }
  6638. // Do not call default handling as that triggers OnCloseQuery,
  6639. // where our implementation will popup confirmation dialogs, what we do not want,
  6640. // as per Vista guidelines:
  6641. // https://docs.microsoft.com/en-us/windows/win32/shutdown/shutting-down
  6642. }
  6643. //---------------------------------------------------------------------------
  6644. void __fastcall TCustomScpExplorerForm::WMEndSession(TWMEndSession & Message)
  6645. {
  6646. if (Message.EndSession && IsApplicationMinimized())
  6647. {
  6648. // WORKAROUND
  6649. // TApplication.WndProc() calls Application.Terminate() before Halt(),
  6650. // when it receives WM_ENDSESSION,
  6651. // but that sometimes (particularly when application is minimized) cause crashes.
  6652. // Crash popups message that blocks log off.
  6653. // Obviously application cannot shutdown cleanly after WM_ENDSESSION,
  6654. // so we call ExitProcess() immediately, not even trying to cleanup.
  6655. // It still causes beep, so there's likely some popup, but it does not block
  6656. // log off.
  6657. ExitProcess(0);
  6658. }
  6659. TForm::Dispatch(&Message);
  6660. }
  6661. #ifdef _DEBUG
  6662. //---------------------------------------------------------------------------
  6663. void __fastcall TCustomScpExplorerForm::WMWindowPosChanged(TWMWindowPosMsg & Message)
  6664. {
  6665. TForm::Dispatch(&Message);
  6666. }
  6667. #endif
  6668. //---------------------------------------------------------------------------
  6669. void __fastcall TCustomScpExplorerForm::SysResizing(unsigned int /*Cmd*/)
  6670. {
  6671. }
  6672. //---------------------------------------------------------------------------
  6673. void __fastcall TCustomScpExplorerForm::DoShow()
  6674. {
  6675. // only now are the controls resized finally, so the size constraints
  6676. // will not conflict with possibly very small window size
  6677. RestoreFormParams();
  6678. // This indicates that the form was rescaled after parameters were loaded and
  6679. // as most probably the parameters were actually saved with the current form scaling
  6680. // (windows was the last time likely closed on the same monitor, where it is starting now),
  6681. // rescaling happened twice already. Reload the parameters, as now they likely won't need any scaling
  6682. // and no rounding problems will occur.
  6683. // See also RestoreForm.
  6684. // (we should be safe to call RestoreParams unconditionally)
  6685. if (PixelsPerInch != Screen->PixelsPerInch)
  6686. {
  6687. RestoreParams();
  6688. }
  6689. FixControlsPlacement();
  6690. if (Position == poDefaultPosOnly)
  6691. {
  6692. CutFormToDesktop(this);
  6693. }
  6694. TForm::DoShow();
  6695. FSessionsDragDropFilesEx->DragDropControl = SessionsPageControl;
  6696. FQueueDragDropFilesEx->DragDropControl = QueueView3;
  6697. if (Terminal == NULL)
  6698. {
  6699. StartingDisconnected();
  6700. }
  6701. UpdatePixelsPerInchMainWindowCounter();
  6702. FShowing = true;
  6703. }
  6704. //---------------------------------------------------------------------------
  6705. void __fastcall TCustomScpExplorerForm::UpdatePixelsPerInchMainWindowCounter()
  6706. {
  6707. Configuration->Usage->Set(L"PixelsPerInchMainWindow", PixelsPerInch);
  6708. }
  6709. //---------------------------------------------------------------------------
  6710. void __fastcall TCustomScpExplorerForm::StartingDisconnected()
  6711. {
  6712. DoTerminalListChanged();
  6713. }
  6714. //---------------------------------------------------------------------------
  6715. void __fastcall TCustomScpExplorerForm::PopupTrayBalloon(TTerminal * Terminal,
  6716. const UnicodeString & Str, TQueryType Type, Exception * E, unsigned int Seconds,
  6717. TNotifyEvent OnBalloonClick, TObject * UserData)
  6718. {
  6719. bool Do;
  6720. UnicodeString Message;
  6721. if (E == NULL)
  6722. {
  6723. Message = Str;
  6724. Do = true;
  6725. }
  6726. else
  6727. {
  6728. Do = ExceptionMessage(E, Message);
  6729. }
  6730. if (Do && WinConfiguration->BalloonNotifications)
  6731. {
  6732. UnicodeString Title;
  6733. if ((Terminal == NULL) && (Type == qtInformation) && ExtractMainInstructions(Message, Title))
  6734. {
  6735. Message = TrimLeft(Message);
  6736. }
  6737. else
  6738. {
  6739. Message = UnformatMessage(Message);
  6740. const ResourceString * Captions[] = { &_SMsgDlgConfirm, &_SMsgDlgWarning,
  6741. &_SMsgDlgError, &_SMsgDlgInformation, NULL };
  6742. Title = LoadResourceString(Captions[Type]);
  6743. if (Terminal != NULL)
  6744. {
  6745. Title = FORMAT(L"%s - %s",
  6746. (TTerminalManager::Instance()->GetTerminalTitle(Terminal, true), Title));
  6747. }
  6748. }
  6749. if (Seconds == 0)
  6750. {
  6751. Seconds = WinConfiguration->NotificationsTimeout;
  6752. }
  6753. FTrayIcon->PopupBalloon(Title, Message, Type, Seconds * MSecsPerSec, OnBalloonClick, UserData);
  6754. }
  6755. }
  6756. //---------------------------------------------------------------------------
  6757. unsigned int __fastcall TCustomScpExplorerForm::MoreMessageDialog(const UnicodeString Message,
  6758. TStrings * MoreMessages, TQueryType Type, unsigned int Answers,
  6759. UnicodeString HelpKeyword, const TMessageParams * Params,
  6760. TTerminal * Terminal)
  6761. {
  6762. if (((WinConfiguration->ContinueOnError && (FErrorList != NULL)) ||
  6763. (FOnFeedSynchronizeError != NULL)) &&
  6764. (Params != NULL) && (Params->Params & mpAllowContinueOnError) )
  6765. {
  6766. if (FOnFeedSynchronizeError != NULL)
  6767. {
  6768. FOnFeedSynchronizeError(Message, MoreMessages, Type, HelpKeyword);
  6769. }
  6770. else
  6771. {
  6772. DebugAssert(FErrorList != NULL);
  6773. TStringList * MoreMessagesCopy = NULL;
  6774. if (MoreMessages)
  6775. {
  6776. MoreMessagesCopy = new TStringList();
  6777. MoreMessagesCopy->Assign(MoreMessages);
  6778. }
  6779. FErrorList->AddObject(Message, MoreMessagesCopy);
  6780. }
  6781. PopupTrayBalloon(Terminal, Message, Type);
  6782. return ContinueAnswer(Answers);
  6783. }
  6784. else
  6785. {
  6786. bool UseBalloon = FTrayIcon->Visible;
  6787. if (UseBalloon)
  6788. {
  6789. PopupTrayBalloon(Terminal, Message, Type);
  6790. }
  6791. unsigned int Result;
  6792. try
  6793. {
  6794. Result = ::MoreMessageDialog(Message, MoreMessages, Type, Answers, HelpKeyword, Params);
  6795. }
  6796. __finally
  6797. {
  6798. // cancel only balloon we popped up, otherwise we may cancel notification
  6799. // balloon that was there before the message dialog
  6800. // (such as "dd confirmation opt in balloon)
  6801. if (UseBalloon)
  6802. {
  6803. FTrayIcon->CancelBalloon();
  6804. }
  6805. }
  6806. return Result;
  6807. }
  6808. }
  6809. //---------------------------------------------------------------------------
  6810. void __fastcall TCustomScpExplorerForm::ShowExtendedException(
  6811. TTerminal * Terminal, Exception * E)
  6812. {
  6813. if (FTrayIcon->Visible)
  6814. {
  6815. PopupTrayBalloon(Terminal, L"", qtError, E);
  6816. }
  6817. // particularly prevent opening new session from jump list,
  6818. // while exception is shown
  6819. NonVisualDataModule->StartBusy();
  6820. try
  6821. {
  6822. ShowExtendedExceptionEx(Terminal, E);
  6823. }
  6824. __finally
  6825. {
  6826. NonVisualDataModule->EndBusy();
  6827. FTrayIcon->CancelBalloon();
  6828. }
  6829. }
  6830. //---------------------------------------------------------------------------
  6831. void __fastcall TCustomScpExplorerForm::TerminalReady()
  6832. {
  6833. if (Terminal->Active)
  6834. {
  6835. InitStatusBar();
  6836. }
  6837. // cannot rely on active page being page for active terminal,
  6838. // as it can happen that active page is the "new session" page
  6839. // (e.g. when reconnecting active terminal, while login dialog
  6840. // invoked from "new session" page is modal)
  6841. int ActiveTerminalIndex = TTerminalManager::Instance()->ActiveTerminalIndex;
  6842. UpdateSessionTab(SessionsPageControl->Pages[ActiveTerminalIndex]);
  6843. }
  6844. //---------------------------------------------------------------------------
  6845. void __fastcall TCustomScpExplorerForm::InactiveTerminalException(
  6846. TTerminal * Terminal, Exception * E)
  6847. {
  6848. Notify(Terminal, L"", qtError, false, NULL, NULL, E);
  6849. if (!Terminal->Active)
  6850. {
  6851. int Index = TTerminalManager::Instance()->IndexOf(Terminal);
  6852. if (DebugAlwaysTrue((Index >= 0) && (Index < SessionsPageControl->PageCount)))
  6853. {
  6854. UpdateSessionTab(SessionsPageControl->Pages[Index]);
  6855. }
  6856. }
  6857. }
  6858. //---------------------------------------------------------------------------
  6859. void __fastcall TCustomScpExplorerForm::Notify(TTerminal * Terminal,
  6860. UnicodeString Message, TQueryType Type,
  6861. bool Important, TNotifyEvent OnClick, TObject * UserData, Exception * E)
  6862. {
  6863. if ((E == NULL) ||
  6864. ExceptionMessage(E, Message))
  6865. {
  6866. unsigned int Seconds = WinConfiguration->NotificationsTimeout;
  6867. if (Important)
  6868. {
  6869. Seconds *= 5;
  6870. }
  6871. UnicodeString NoteMessage(UnformatMessage(Message));
  6872. if (Terminal != NULL)
  6873. {
  6874. NoteMessage = FORMAT(L"%s: %s",
  6875. (TTerminalManager::Instance()->GetTerminalTitle(Terminal, true), NoteMessage));
  6876. }
  6877. if (WinConfiguration->BalloonNotifications)
  6878. {
  6879. AddNote(NoteMessage);
  6880. PopupTrayBalloon(Terminal, Message, Type, NULL, Seconds, OnClick, UserData);
  6881. }
  6882. else
  6883. {
  6884. FlashOnBackground();
  6885. PostNote(NoteMessage, Seconds, OnClick, UserData);
  6886. }
  6887. }
  6888. }
  6889. //---------------------------------------------------------------------------
  6890. void __fastcall TCustomScpExplorerForm::QueueEmptyNoteClicked(TObject * Sender)
  6891. {
  6892. RestoreApp();
  6893. TTerminalNoteData * TerminalNoteData = dynamic_cast<TTerminalNoteData *>(Sender);
  6894. if (DebugAlwaysTrue(TerminalNoteData != NULL) &&
  6895. !NonVisualDataModule->Busy)
  6896. {
  6897. TManagedTerminal * Terminal = TerminalNoteData->Terminal;
  6898. TTerminalManager::Instance()->ActiveTerminal = Terminal;
  6899. if (!ComponentVisible[fcQueueView])
  6900. {
  6901. ToggleQueueVisibility();
  6902. GoToQueue();
  6903. }
  6904. }
  6905. }
  6906. //---------------------------------------------------------------------------
  6907. void __fastcall TCustomScpExplorerForm::QueueEvent(TManagedTerminal * ATerminal,
  6908. TTerminalQueue * /*Queue*/, TQueueEvent Event)
  6909. {
  6910. UnicodeString Message;
  6911. TNotifyEvent OnClick = NULL;
  6912. TObject * UserData = NULL;
  6913. bool QueueInvisible = !ComponentVisible[fcQueueView] || IsApplicationMinimized();
  6914. switch (Event)
  6915. {
  6916. case qeEmptyButMonitored:
  6917. if ((ATerminal != Terminal) || QueueInvisible)
  6918. {
  6919. Message = LoadStr(BALLOON_QUEUE_EMPTY);
  6920. OnClick = QueueEmptyNoteClicked;
  6921. TTerminalNoteData * TerminalNoteData = new TTerminalNoteData();
  6922. TerminalNoteData->Terminal = ATerminal;
  6923. UserData = TerminalNoteData;
  6924. }
  6925. break;
  6926. case qeEmpty:
  6927. OperationComplete(ATerminal->QueueOperationStart);
  6928. break;
  6929. case qePendingUserAction:
  6930. if ((ATerminal != Terminal) ||
  6931. (QueueInvisible && !IsQueueAutoPopup()))
  6932. {
  6933. Message = LoadStr(BALLOON_QUEUE_USER_ACTION);
  6934. }
  6935. break;
  6936. default:
  6937. DebugFail();
  6938. }
  6939. if (!Message.IsEmpty())
  6940. {
  6941. Notify(ATerminal, Message, qtInformation, false, OnClick, UserData);
  6942. }
  6943. }
  6944. //---------------------------------------------------------------------------
  6945. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDCreateDragFileList(
  6946. TObject * /*Sender*/, TFileList * FileList, bool & Created)
  6947. {
  6948. if (FDDExtMapFile != NULL)
  6949. {
  6950. CloseHandle(FDDExtMapFile);
  6951. FDDExtMapFile = NULL;
  6952. }
  6953. if (WinConfiguration->DDFakeFile)
  6954. {
  6955. DDFakeFileInitDrag(FileList, Created);
  6956. }
  6957. }
  6958. //---------------------------------------------------------------------------
  6959. void __fastcall TCustomScpExplorerForm::DDFakeCreated(TObject * /*Sender*/, const UnicodeString FileName)
  6960. {
  6961. if (DebugAlwaysTrue(!FDragFakeDirectory.IsEmpty()) &&
  6962. SameText(ExtractFileName(FileName), ExtractFileName(FDragFakeDirectory)))
  6963. {
  6964. FFakeFileDropTarget = FileName;
  6965. }
  6966. }
  6967. //---------------------------------------------------------------------------
  6968. UnicodeString __fastcall TCustomScpExplorerForm::CreateFakeTransferDirectory()
  6969. {
  6970. UnicodeString Result = ExcludeTrailingBackslash(WinConfiguration->TemporaryDir());
  6971. if (!ForceDirectories(ApiPath(Result)))
  6972. {
  6973. throw Exception(FMTLOAD(CREATE_TEMP_DIR_ERROR, (Result)));
  6974. }
  6975. FileSetAttr(ApiPath(Result), faHidden);
  6976. return Result;
  6977. }
  6978. //---------------------------------------------------------------------------
  6979. void __fastcall TCustomScpExplorerForm::DDFakeFileInitDrag(TFileList * FileList,
  6980. bool & Created)
  6981. {
  6982. FFakeFileDropTarget = UnicodeString();
  6983. FDragFakeDirectory = CreateFakeTransferDirectory();
  6984. FileList->AddItem(NULL, FDragFakeDirectory);
  6985. Created = true;
  6986. if (!WinConfiguration->IsDDExtRunning() || WinConfiguration->IsDDExtBroken())
  6987. {
  6988. FDragFakeMonitors = StartCreationDirectoryMonitorsOnEachDrive(FILE_NOTIFY_CHANGE_DIR_NAME, DDFakeCreated);
  6989. }
  6990. FDDExtMapFile = CreateFileMappingA((HANDLE)0xFFFFFFFF, NULL, PAGE_READWRITE,
  6991. 0, sizeof(TDragExtCommStruct), AnsiString(DRAG_EXT_MAPPING).c_str());
  6992. {
  6993. TMutexGuard Guard(FDDExtMutex, DRAGEXT_MUTEX_RELEASE_TIMEOUT);
  6994. TDragExtCommStruct* CommStruct;
  6995. CommStruct = static_cast<TDragExtCommStruct*>(MapViewOfFile(FDDExtMapFile,
  6996. FILE_MAP_ALL_ACCESS, 0, 0, 0));
  6997. DebugAssert(CommStruct != NULL);
  6998. CommStruct->Version = TDragExtCommStruct::CurrentVersion;
  6999. CommStruct->Dragging = true;
  7000. wcsncpy(CommStruct->DropDest, FDragFakeDirectory.c_str(),
  7001. LENOF(CommStruct->DropDest));
  7002. NULL_TERMINATE(CommStruct->DropDest);
  7003. UnmapViewOfFile(CommStruct);
  7004. }
  7005. }
  7006. //---------------------------------------------------------------------------
  7007. bool __fastcall TCustomScpExplorerForm::RemoteFileControlFileOperation(
  7008. TObject * Sender, TFileOperation Operation, bool NoConfirmation, void * Param)
  7009. {
  7010. bool Result;
  7011. DebugAssert(!IsLocalBrowserMode());
  7012. if (Sender == RemoteDirView)
  7013. {
  7014. Result = ExecuteFileOperation(Operation, osRemote, true, NoConfirmation, Param);
  7015. }
  7016. else
  7017. {
  7018. DebugAssert(Sender == RemoteDriveView);
  7019. TStrings * FileList = RemoteDriveView->DragFileList();
  7020. try
  7021. {
  7022. Result = ExecuteFileOperation(Operation, osRemote, FileList, NoConfirmation, Param);
  7023. }
  7024. __finally
  7025. {
  7026. delete FileList;
  7027. }
  7028. }
  7029. if (FDDTargetControl == RemoteDriveView)
  7030. {
  7031. RemoteDriveView->UpdateDropTarget();
  7032. }
  7033. // foRemoteMove happens when file/dir is dragged within the remote tree view
  7034. // or from tree view to dir view.
  7035. // foMove happens when file/dir is dragged from remote tree view outside of
  7036. // application via dragex
  7037. if (((Operation == foRemoteMove) || (Operation == foMove)) &&
  7038. (DropSourceControl == RemoteDriveView))
  7039. {
  7040. RemoteDriveView->UpdateDropSource();
  7041. }
  7042. return Result;
  7043. }
  7044. //---------------------------------------------------------------------------
  7045. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDEnd(TObject * Sender)
  7046. {
  7047. DebugAssert(!IsLocalBrowserMode());
  7048. // This also handles drops of remote files to queue.
  7049. // Drops of local files (uploads) are handled in QueueDDProcessDropped.
  7050. SAFE_DESTROY(FDDFileList);
  7051. if (!FFakeFileDropTarget.IsEmpty())
  7052. {
  7053. RemoveDir(ApiPath(FFakeFileDropTarget));
  7054. }
  7055. if ((FDDExtMapFile != NULL) || (FDDTargetControl == QueueView3))
  7056. {
  7057. try
  7058. {
  7059. TDragResult DDResult = (Sender == RemoteDirView) ?
  7060. RemoteDirView->LastDDResult : RemoteDriveView->LastDDResult;
  7061. // Focus is moved to the target application,
  7062. // but as we are going to present the UI, we need to steal the focus back.
  7063. // This most likely won't work though (windows does not allow application
  7064. // to steal focus most of the time)
  7065. Application->BringToFront();
  7066. // On older version of Windows we never got drMove here, see also comment below.
  7067. // On Windows 10, we get the "move".
  7068. if ((DDResult == drCopy) || (DDResult == drMove) || (DDResult == drInvalid))
  7069. {
  7070. UnicodeString TargetDirectory;
  7071. TFileOperation Operation;
  7072. // drInvalid may mean drMove, see comment below
  7073. switch (DDResult)
  7074. {
  7075. case drCopy:
  7076. Operation = foCopy;
  7077. break;
  7078. case drMove:
  7079. Operation = foMove;
  7080. break;
  7081. default:
  7082. DebugFail();
  7083. case drInvalid:
  7084. // prefer "copy" for safety
  7085. Operation = FLAGSET(FLastDropEffect, DROPEFFECT_MOVE) ? foMove : foCopy;
  7086. break;
  7087. }
  7088. TTransferOperationParam Param;
  7089. UnicodeString CounterName;
  7090. bool ForceQueue;
  7091. if (!DDGetTarget(Param.TargetDirectory, ForceQueue, CounterName))
  7092. {
  7093. // we get drInvalid both if d&d was intercepted by ddext,
  7094. // and when users drops on no-drop location.
  7095. // we tell the difference by existence of response from ddext,
  7096. // so we ignore absence of response in this case
  7097. if (DDResult != drInvalid)
  7098. {
  7099. // here we know that the extension is installed,
  7100. // as it is checked as soon as drag&drop starts from
  7101. // RemoteFileControlDDCreateDragFileList
  7102. Configuration->Usage->Inc(L"DownloadsDragDropExternalExtTargetUnknown");
  7103. if (!WinConfiguration->DDExtInstalled || WinConfiguration->IsDDExtBroken())
  7104. {
  7105. throw ExtException(NULL, LoadStr(DD_TARGET_UNKNOWN), HELP_DD_TARGET_UNKNOWN);
  7106. }
  7107. else
  7108. {
  7109. throw ExtException(NULL, LoadStr(DRAGEXT_TARGET_UNKNOWN2), HELP_DRAGEXT_TARGET_UNKNOWN);
  7110. }
  7111. }
  7112. }
  7113. else
  7114. {
  7115. // download using fake file
  7116. Param.Temp = false;
  7117. Param.DragDrop = true;
  7118. if (ForceQueue)
  7119. {
  7120. Param.Queue = asOn;
  7121. }
  7122. if (Sender == RemoteDirView)
  7123. {
  7124. Param.Options = FLAGMASK(SelectedAllFilesInDirView(RemoteDirView), coAllFiles);
  7125. }
  7126. if (RemoteFileControlFileOperation(Sender, Operation,
  7127. (WinConfiguration->DDTransferConfirmation == asOff), &Param))
  7128. {
  7129. Configuration->Usage->Inc(CounterName);
  7130. }
  7131. }
  7132. }
  7133. }
  7134. __finally
  7135. {
  7136. CloseHandle(FDDExtMapFile);
  7137. FDDExtMapFile = NULL;
  7138. delete FDragFakeMonitors;
  7139. FDragFakeMonitors = NULL;
  7140. RemoveDir(ApiPath(FDragFakeDirectory));
  7141. FDragFakeDirectory = L"";
  7142. FFakeFileDropTarget = UnicodeString();
  7143. }
  7144. }
  7145. if (FDragDropSshTerminate.get() != NULL)
  7146. {
  7147. FDragDropSshTerminate->Rethrow();
  7148. }
  7149. }
  7150. //---------------------------------------------------------------------------
  7151. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDGiveFeedback(
  7152. TObject * /*Sender*/, int dwEffect, HRESULT & /*Result*/)
  7153. {
  7154. // Remember drop effect so we know (when user drops files), if we copy or move
  7155. FLastDropEffect = dwEffect;
  7156. }
  7157. //---------------------------------------------------------------------------
  7158. bool __fastcall TCustomScpExplorerForm::DDGetTarget(
  7159. UnicodeString & Directory, bool & ForceQueue, UnicodeString & CounterName)
  7160. {
  7161. bool Result;
  7162. if (FDDTargetControl == QueueView3)
  7163. {
  7164. Directory = DefaultDownloadTargetDirectory();
  7165. Result = true;
  7166. CounterName = L"DownloadsDragDropQueue";
  7167. ForceQueue = true;
  7168. }
  7169. else if (!FFakeFileDropTarget.IsEmpty())
  7170. {
  7171. Directory = ExcludeTrailingBackslash(ExtractFilePath(FFakeFileDropTarget));
  7172. Result = true;
  7173. ForceQueue = false;
  7174. CounterName = L"DownloadsDragDropFakeFile";
  7175. }
  7176. else
  7177. {
  7178. ForceQueue = false;
  7179. Enabled = false;
  7180. try
  7181. {
  7182. int Timer = 0;
  7183. Result = false;
  7184. while (!Result && (Timer < WinConfiguration->DDExtTimeout))
  7185. {
  7186. {
  7187. TMutexGuard Guard(FDDExtMutex, DRAGEXT_MUTEX_RELEASE_TIMEOUT);
  7188. TDragExtCommStruct* CommStruct;
  7189. CommStruct = static_cast<TDragExtCommStruct*>(MapViewOfFile(FDDExtMapFile,
  7190. FILE_MAP_ALL_ACCESS, 0, 0, 0));
  7191. DebugAssert(CommStruct != NULL);
  7192. Result = !CommStruct->Dragging;
  7193. if (Result)
  7194. {
  7195. Directory = ExtractFilePath(CommStruct->DropDest);
  7196. CounterName = L"DownloadsDragDropExternalExt";
  7197. }
  7198. UnmapViewOfFile(CommStruct);
  7199. }
  7200. if (!Result)
  7201. {
  7202. Sleep(50);
  7203. Timer += 50;
  7204. Application->ProcessMessages();
  7205. }
  7206. }
  7207. }
  7208. __finally
  7209. {
  7210. Enabled = true;
  7211. }
  7212. }
  7213. return Result;
  7214. }
  7215. //---------------------------------------------------------------------------
  7216. void __fastcall TCustomScpExplorerForm::AddDelayedDirectoryDeletion(
  7217. const UnicodeString TempDir, int SecDelay)
  7218. {
  7219. TDateTime Alarm = IncSecond(Now(), SecDelay);
  7220. FDelayedDeletionList->AddObject(TempDir, reinterpret_cast<TObject*>(Alarm.FileDate()));
  7221. if (FDelayedDeletionTimer == NULL)
  7222. {
  7223. DebugAssert(HandleAllocated());
  7224. FDelayedDeletionTimer = new TTimer(this);
  7225. FDelayedDeletionTimer->Interval = 10000;
  7226. FDelayedDeletionTimer->OnTimer = DoDelayedDeletion;
  7227. }
  7228. else
  7229. {
  7230. FDelayedDeletionTimer->Enabled = true;
  7231. }
  7232. }
  7233. //---------------------------------------------------------------------------
  7234. void __fastcall TCustomScpExplorerForm::DoDelayedDeletion(TObject * Sender)
  7235. {
  7236. DebugAssert(FDelayedDeletionList != NULL);
  7237. TDateTime N = Now();
  7238. TDateTime Alert;
  7239. UnicodeString Directory;
  7240. for (int Index = FDelayedDeletionList->Count-1; Index >= 0; Index--)
  7241. {
  7242. Alert = FileDateToDateTime(reinterpret_cast<int>(FDelayedDeletionList->Objects[Index]));
  7243. if ((N >= Alert) || (Sender == NULL))
  7244. {
  7245. Directory = FDelayedDeletionList->Strings[Index];
  7246. if (DeleteDirectory(ExcludeTrailingBackslash(Directory)))
  7247. {
  7248. FDelayedDeletionList->Delete(Index);
  7249. }
  7250. }
  7251. }
  7252. if (FDelayedDeletionList->Count == 0)
  7253. {
  7254. FDelayedDeletionTimer->Enabled = false;
  7255. }
  7256. }
  7257. //---------------------------------------------------------------------------
  7258. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDTargetDrop()
  7259. {
  7260. DebugAssert(!IsLocalBrowserMode());
  7261. if (IsFileControl(FDDTargetControl, osRemote) ||
  7262. (FDDTargetControl == SessionsPageControl))
  7263. {
  7264. TTerminal * TargetTerminal = NULL;
  7265. TFileOperation Operation = foNone;
  7266. if (FDDTargetControl == SessionsPageControl)
  7267. {
  7268. TPoint Point = SessionsPageControl->ScreenToClient(Mouse->CursorPos);
  7269. int Index = SessionsPageControl->IndexOfTabAt(Point.X, Point.Y);
  7270. // do not allow dropping on the "+" and disconnected tabs
  7271. TargetTerminal = GetSessionTabTerminal(SessionsPageControl->Pages[Index]);
  7272. if (IsActiveTerminal(TargetTerminal))
  7273. {
  7274. if ((FLastDropEffect == DROPEFFECT_MOVE) &&
  7275. (TargetTerminal == TTerminalManager::Instance()->ActiveTerminal))
  7276. {
  7277. Operation = foRemoteMove;
  7278. }
  7279. else
  7280. {
  7281. Operation = foRemoteCopy;
  7282. }
  7283. }
  7284. }
  7285. else
  7286. {
  7287. if (FLastDropEffect == DROPEFFECT_MOVE)
  7288. {
  7289. Operation = foRemoteMove;
  7290. }
  7291. else if (FLastDropEffect == DROPEFFECT_COPY)
  7292. {
  7293. Operation = foRemoteCopy;
  7294. }
  7295. }
  7296. if (Operation != foNone)
  7297. {
  7298. RemoteFileControlFileOperation(DropSourceControl,
  7299. Operation, (WinConfiguration->DDTransferConfirmation == asOff), TargetTerminal);
  7300. }
  7301. // abort drag&drop
  7302. Abort();
  7303. }
  7304. else if ((FDDExtMapFile == NULL) && (FLastDropEffect != DROPEFFECT_NONE) &&
  7305. // Drops of remote files to queue are handled in RemoteFileControlDDEnd
  7306. (FDDTargetControl != QueueView3))
  7307. {
  7308. DebugAssert(!FDragTempDir.IsEmpty());
  7309. TTransferType Type;
  7310. UnicodeString TempDir = FDragTempDir;
  7311. // We clear FDragTempDir before calling
  7312. // just in case it fail (raises exception)
  7313. FDragTempDir = L"";
  7314. Type = (FLastDropEffect & DROPEFFECT_MOVE ? ttMove : Type = ttCopy);
  7315. TGUICopyParamType CopyParams = GUIConfiguration->CurrentCopyParam;
  7316. // empty directory parameter means temp directory -> don't display it!
  7317. UnicodeString TargetDir = L"";
  7318. int Options = 0;
  7319. if (!CopyParamDialog(tdToLocal, Type, true, FDDFileList,
  7320. TargetDir, CopyParams, (WinConfiguration->DDTransferConfirmation != asOff), true, Options))
  7321. {
  7322. Abort();
  7323. }
  7324. // TargetDir is set when dropped on local file control
  7325. // (this was workaround for legacy dirview event handling, now it should be
  7326. // made prettier)
  7327. if (TargetDir.IsEmpty())
  7328. {
  7329. TargetDir = TempDir;
  7330. if (ForceDirectories(ApiPath(TargetDir)))
  7331. {
  7332. DebugAssert(Terminal && !TargetDir.IsEmpty());
  7333. FPendingTempSpaceWarn = true;
  7334. try
  7335. {
  7336. FDragDropOperation = true;
  7337. int Params = cpTemporary |
  7338. (Type == ttMove ? cpDelete : 0);
  7339. DDDownload(FDDFileList, TargetDir, &CopyParams, Params);
  7340. Configuration->Usage->Inc(L"DownloadsDragDropExternalTemp");
  7341. }
  7342. __finally
  7343. {
  7344. FDragDropOperation = false;
  7345. FPendingTempSpaceWarn = false;
  7346. AddDelayedDirectoryDeletion(TargetDir, WinConfiguration->DDDeleteDelay);
  7347. }
  7348. }
  7349. else
  7350. {
  7351. throw Exception(FMTLOAD(CREATE_TEMP_DIR_ERROR, (TargetDir)));
  7352. }
  7353. }
  7354. }
  7355. }
  7356. //---------------------------------------------------------------------------
  7357. void __fastcall TCustomScpExplorerForm::DDDownload(
  7358. TStrings * FilesToCopy, const UnicodeString & TargetDir, TCopyParamType * CopyParam, int Params)
  7359. {
  7360. DebugAssert(!IsLocalBrowserMode());
  7361. TAutoBatch AutoBatch(this);
  7362. CopyParam->IncludeFileMask.SetRoots(TargetDir, FilesToCopy);
  7363. UpdateCopyParamCounters(*CopyParam);
  7364. Terminal->CopyToLocal(FilesToCopy, TargetDir, CopyParam, Params, NULL);
  7365. if (FLAGSET(Params, cpDelete) && (DropSourceControl == RemoteDriveView))
  7366. {
  7367. RemoteDriveView->UpdateDropSource();
  7368. }
  7369. }
  7370. //---------------------------------------------------------------------------
  7371. class TFakeDataObjectFilesEx : public TDataObjectFilesEx
  7372. {
  7373. public:
  7374. __fastcall TFakeDataObjectFilesEx(TFileList * AFileList, bool RenderPIDL,
  7375. bool RenderFilename) : TDataObjectFilesEx(AFileList, RenderPIDL, RenderFilename, true)
  7376. {
  7377. }
  7378. virtual bool __fastcall AllowData(const tagFORMATETC & FormatEtc)
  7379. {
  7380. return (FormatEtc.cfFormat == CF_HDROP) ? false :
  7381. TDataObjectFilesEx::AllowData(FormatEtc);
  7382. }
  7383. };
  7384. //---------------------------------------------------------------------------
  7385. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDCreateDataObject(
  7386. TObject * Sender, TDataObject *& DataObject)
  7387. {
  7388. if (FDDExtMapFile != NULL)
  7389. {
  7390. TFileList * FileList = DragDropFiles(Sender)->FileList;
  7391. if (!FileList->RenderPIDLs() || !FileList->RenderNames())
  7392. {
  7393. Abort();
  7394. }
  7395. if (FileList->Count > 0)
  7396. {
  7397. TDataObjectFilesEx * FilesObject = new TFakeDataObjectFilesEx(FileList, true, true);
  7398. if (!FilesObject->IsValid(true, true))
  7399. {
  7400. FilesObject->_Release();
  7401. }
  7402. else
  7403. {
  7404. DataObject = FilesObject;
  7405. }
  7406. }
  7407. }
  7408. }
  7409. //---------------------------------------------------------------------------
  7410. void __fastcall TCustomScpExplorerForm::GoToCommandLine()
  7411. {
  7412. DebugFail();
  7413. }
  7414. //---------------------------------------------------------------------------
  7415. void __fastcall TCustomScpExplorerForm::GoToTree()
  7416. {
  7417. ComponentVisible[fcRemoteTree] = true;
  7418. DriveView(osRemote)->SetFocus();
  7419. }
  7420. //---------------------------------------------------------------------------
  7421. TStrings * __fastcall TCustomScpExplorerForm::PanelExport(TOperationSide Side,
  7422. TPanelExport Export)
  7423. {
  7424. TCustomDirView * DirView = this->DirView(Side);
  7425. std::unique_ptr<TStrings> ExportData(new TStringList());
  7426. switch (Export)
  7427. {
  7428. case pePath:
  7429. ExportData->Add(DirView->PathName);
  7430. break;
  7431. case peFileList:
  7432. case peFullFileList:
  7433. {
  7434. bool FullPath = (Export == peFullFileList);
  7435. DirView->CreateFileList(false, FullPath, ExportData.get());
  7436. for (int Index = 0; Index < ExportData->Count; Index++)
  7437. {
  7438. if (ExportData->Strings[Index].Pos(L" ") > 0)
  7439. {
  7440. ExportData->Strings[Index] = FORMAT(L"\"%s\"", (ExportData->Strings[Index]));
  7441. }
  7442. }
  7443. }
  7444. break;
  7445. default:
  7446. DebugFail();
  7447. }
  7448. return ExportData.release();
  7449. }
  7450. //---------------------------------------------------------------------------
  7451. void __fastcall TCustomScpExplorerForm::PanelExport(TOperationSide Side,
  7452. TPanelExport Export, TPanelExportDestination Destination)
  7453. {
  7454. std::unique_ptr<TStrings> ExportData(PanelExport(Side, Export));
  7455. PanelExportStore(Side, Export, Destination, ExportData.get());
  7456. }
  7457. //---------------------------------------------------------------------------
  7458. void __fastcall TCustomScpExplorerForm::PanelExportStore(TOperationSide /*Side*/,
  7459. TPanelExport /*Export*/, TPanelExportDestination Destination,
  7460. TStrings * ExportData)
  7461. {
  7462. if (Destination == pedClipboard)
  7463. {
  7464. TInstantOperationVisualizer Visualizer;
  7465. CopyToClipboard(ExportData);
  7466. }
  7467. else
  7468. {
  7469. DebugFail();
  7470. }
  7471. }
  7472. //---------------------------------------------------------------------------
  7473. void __fastcall TCustomScpExplorerForm::Filter(TOperationSide Side)
  7474. {
  7475. TCustomDirView * DirView = this->DirView(Side);
  7476. UnicodeString Mask = DirView->Mask;
  7477. if (DoFilterMaskDialog(DirView, Mask))
  7478. {
  7479. DirView->Mask = TFileMasks::NormalizeMask(Mask);
  7480. Configuration->Usage->Inc(L"Filters");
  7481. }
  7482. }
  7483. //---------------------------------------------------------------------------
  7484. TQueueOperation __fastcall TCustomScpExplorerForm::DefaultQueueOperation()
  7485. {
  7486. return FQueueController->DefaultOperation();
  7487. }
  7488. //---------------------------------------------------------------------------
  7489. bool __fastcall TCustomScpExplorerForm::AllowQueueOperation(
  7490. TQueueOperation Operation, void ** Param)
  7491. {
  7492. switch (Operation)
  7493. {
  7494. case qoPreferences:
  7495. return true;
  7496. case qoGoTo:
  7497. return ComponentVisible[fcQueueView] && QueueView3->Enabled;
  7498. case qoOnceEmpty:
  7499. return IsAnythingQueued();
  7500. default:
  7501. return FQueueController->AllowOperation(Operation, Param);
  7502. }
  7503. }
  7504. //---------------------------------------------------------------------------
  7505. void __fastcall TCustomScpExplorerForm::GoToQueue()
  7506. {
  7507. if (DebugAlwaysTrue(QueueView3->Visible))
  7508. {
  7509. QueueView3->SetFocus();
  7510. }
  7511. }
  7512. //---------------------------------------------------------------------------
  7513. void __fastcall TCustomScpExplorerForm::ExecuteQueueOperation(
  7514. TQueueOperation Operation, void * Param)
  7515. {
  7516. if (Operation == qoGoTo)
  7517. {
  7518. GoToQueue();
  7519. }
  7520. else if (Operation == qoPreferences)
  7521. {
  7522. PreferencesDialog(pmQueue);
  7523. }
  7524. else
  7525. {
  7526. FQueueController->ExecuteOperation(Operation, Param);
  7527. }
  7528. }
  7529. //---------------------------------------------------------------------------
  7530. bool __fastcall TCustomScpExplorerForm::GetQueueEnabled()
  7531. {
  7532. return (Queue != NULL) && Queue->Enabled;
  7533. }
  7534. //---------------------------------------------------------------------------
  7535. void __fastcall TCustomScpExplorerForm::ToggleQueueEnabled()
  7536. {
  7537. DebugAssert(Queue != NULL);
  7538. if (Queue != NULL)
  7539. {
  7540. Queue->Enabled = !Queue->Enabled;
  7541. }
  7542. }
  7543. //---------------------------------------------------------------------------
  7544. void __fastcall TCustomScpExplorerForm::QueueView3ContextPopup(
  7545. TObject * /*Sender*/, TPoint & /*MousePos*/, bool & /*Handled*/)
  7546. {
  7547. FQueueActedItem = QueueView3->ItemFocused;
  7548. }
  7549. //---------------------------------------------------------------------------
  7550. /*virtual*/ int __fastcall TCustomScpExplorerForm::GetStaticComponentsHeight()
  7551. {
  7552. return TopDock->Height + QueueSplitter->Height;
  7553. }
  7554. //---------------------------------------------------------------------------
  7555. int __fastcall TCustomScpExplorerForm::GetStaticQueuePanelComponentsHeight()
  7556. {
  7557. return
  7558. (QueueFileListSplitter->Visible ? QueueFileListSplitter->Height : 0) +
  7559. (QueueDock->Visible ? QueueDock->Height : 0) +
  7560. (QueueLabel->Visible ? QueueLabel->Height : 0);
  7561. }
  7562. //---------------------------------------------------------------------------
  7563. int __fastcall TCustomScpExplorerForm::GetMinQueueViewHeight()
  7564. {
  7565. return ScaleByTextHeight(this, 48);
  7566. }
  7567. //---------------------------------------------------------------------------
  7568. void __fastcall TCustomScpExplorerForm::QueueSplitterCanResize(
  7569. TObject * /*Sender*/, int & NewSize, bool & Accept)
  7570. {
  7571. // when queue is hidden by double-clicking splitter, stray attempt to
  7572. // resize the panel with strange value arrives, make sure it is ignored
  7573. if (ComponentVisible[fcQueueView])
  7574. {
  7575. int MaxHeightLimit = ClientHeight - GetStaticComponentsHeight() - RemotePanel->Constraints->MinHeight;
  7576. if (NewSize > MaxHeightLimit)
  7577. {
  7578. NewSize = MaxHeightLimit;
  7579. }
  7580. int MinHeightLimit =
  7581. GetStaticQueuePanelComponentsHeight() +
  7582. (QueueFileList->Visible ? QueueFileList->Height : 0) +
  7583. GetMinQueueViewHeight();
  7584. if (NewSize < MinHeightLimit)
  7585. {
  7586. NewSize = MinHeightLimit;
  7587. }
  7588. }
  7589. else
  7590. {
  7591. Accept = false;
  7592. }
  7593. }
  7594. //---------------------------------------------------------------------------
  7595. void __fastcall TCustomScpExplorerForm::QueueFileListSplitterCanResize(TObject *, int & NewSize, bool &)
  7596. {
  7597. int TotalHeight = GetStaticQueuePanelComponentsHeight() + NewSize;
  7598. int QueueViewHeight = QueuePanel->ClientHeight - TotalHeight;
  7599. if (QueueViewHeight < GetMinQueueViewHeight())
  7600. {
  7601. NewSize -= (GetMinQueueViewHeight() - QueueViewHeight);
  7602. }
  7603. }
  7604. //---------------------------------------------------------------------------
  7605. void __fastcall TCustomScpExplorerForm::QueueView3StartDrag(TObject * /*Sender*/,
  7606. TDragObject *& /*DragObject*/)
  7607. {
  7608. FQueueActedItem = QueueView3->ItemFocused;
  7609. }
  7610. //---------------------------------------------------------------------------
  7611. void __fastcall TCustomScpExplorerForm::QueueView3DragOver(TObject * /*Sender*/,
  7612. TObject * Source, int X, int Y, TDragState /*State*/, bool & Accept)
  7613. {
  7614. Accept = true;
  7615. if (Source == QueueView3)
  7616. {
  7617. TListItem * DropTarget = QueueView3->GetItemAt(X, Y);
  7618. Accept = (DropTarget != NULL) && (FQueueActedItem != NULL);
  7619. if (Accept)
  7620. {
  7621. TQueueItemProxy * QueueItem;
  7622. TQueueItemProxy * DestQueueItem;
  7623. QueueItem = static_cast<TQueueItemProxy *>(FQueueActedItem->Data);
  7624. DestQueueItem = static_cast<TQueueItemProxy *>(DropTarget->Data);
  7625. Accept = (QueueItem != DestQueueItem) &&
  7626. (QueueItem->Status == TQueueItem::qsPending) &&
  7627. (DestQueueItem->Status == TQueueItem::qsPending);
  7628. }
  7629. }
  7630. }
  7631. //---------------------------------------------------------------------------
  7632. void __fastcall TCustomScpExplorerForm::QueueView3DragDrop(TObject * /*Sender*/,
  7633. TObject * /*Source*/, int /*X*/, int /*Y*/)
  7634. {
  7635. if ((FQueueActedItem != NULL) && (QueueView3->DropTarget != NULL))
  7636. {
  7637. TQueueItemProxy * QueueItem;
  7638. TQueueItemProxy * DestQueueItem;
  7639. QueueItem = static_cast<TQueueItemProxy *>(FQueueActedItem->Data);
  7640. DestQueueItem = static_cast<TQueueItemProxy *>(QueueView3->DropTarget->Data);
  7641. QueueItem->Move(DestQueueItem);
  7642. }
  7643. }
  7644. //---------------------------------------------------------------------------
  7645. void __fastcall TCustomScpExplorerForm::QueueView3Enter(TObject * /*Sender*/)
  7646. {
  7647. if ((QueueView3->ItemFocused == NULL) &&
  7648. (QueueView3->Items->Count > 0))
  7649. {
  7650. QueueView3->ItemFocused = QueueView3->Items->Item[0];
  7651. }
  7652. QueueLabelUpdateStatus();
  7653. }
  7654. //---------------------------------------------------------------------------
  7655. void __fastcall TCustomScpExplorerForm::QueueView3Exit(TObject * /*Sender*/)
  7656. {
  7657. QueueLabelUpdateStatus();
  7658. }
  7659. //---------------------------------------------------------------------------
  7660. void __fastcall TCustomScpExplorerForm::QueueLabelUpdateStatus()
  7661. {
  7662. QueueLabel->UpdateStatus();
  7663. }
  7664. //---------------------------------------------------------------------------
  7665. void __fastcall TCustomScpExplorerForm::QueueView3SelectItem(
  7666. TObject * /*Sender*/, TListItem * /*Item*/, bool Selected)
  7667. {
  7668. if (Selected)
  7669. {
  7670. NonVisualDataModule->UpdateNonVisibleActions();
  7671. }
  7672. }
  7673. //---------------------------------------------------------------------------
  7674. TDragDropFilesEx * __fastcall TCustomScpExplorerForm::DragDropFiles(TObject * Sender)
  7675. {
  7676. TDragDropFilesEx * Result = NULL;
  7677. if (Sender == SessionsPageControl)
  7678. {
  7679. Result = FSessionsDragDropFilesEx;
  7680. }
  7681. else if (Sender == QueueView3)
  7682. {
  7683. Result = FQueueDragDropFilesEx;
  7684. }
  7685. else
  7686. {
  7687. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  7688. if (DirView != NULL)
  7689. {
  7690. Result = DirView->DragDropFilesEx;
  7691. }
  7692. else
  7693. {
  7694. TCustomDriveView * DriveView = dynamic_cast<TCustomDriveView *>(Sender);
  7695. if (DriveView != NULL)
  7696. {
  7697. Result = DriveView->DragDropFilesEx;
  7698. }
  7699. }
  7700. }
  7701. DebugAssert(Result != NULL);
  7702. return Result;
  7703. }
  7704. //---------------------------------------------------------------------------
  7705. bool __fastcall TCustomScpExplorerForm::SelectedAllFilesInDirView(TCustomDirView * DView)
  7706. {
  7707. return (DView->SelCount == DView->FilesCount);
  7708. }
  7709. //---------------------------------------------------------------------------
  7710. bool __fastcall TCustomScpExplorerForm::DraggingAllFilesFromDirView(TOperationSide Side, TStrings * FileList)
  7711. {
  7712. return HasDirView[Side] && (DropSourceControl == DirView(Side)) && (FileList->Count == DirView(Side)->FilesCount);
  7713. }
  7714. //---------------------------------------------------------------------------
  7715. void __fastcall TCustomScpExplorerForm::RemoteFileControlDragDropFileOperation(
  7716. TObject * Sender, int Effect, UnicodeString TargetPath, bool ForceQueue, bool DragDrop)
  7717. {
  7718. DebugAssert(!IsLocalBrowserMode());
  7719. TFileOperation Operation;
  7720. switch (Effect)
  7721. {
  7722. case DROPEFFECT_MOVE:
  7723. Operation = foMove;
  7724. break;
  7725. case DROPEFFECT_COPY:
  7726. // occures on WinXP (reported by user)
  7727. default:
  7728. Operation = foCopy;
  7729. break;
  7730. };
  7731. TDragDropFilesEx * DragDropFilesEx = DragDropFiles(Sender);
  7732. // see a comment in TUnixDirView::PerformItemDragDropOperation
  7733. if (DragDropFilesEx->FileList->Count > 0)
  7734. {
  7735. TStrings * FileList = new TStringList();
  7736. try
  7737. {
  7738. for (int Index = 0; Index < DragDropFilesEx->FileList->Count; Index++)
  7739. {
  7740. FileList->Add(DragDropFilesEx->FileList->Items[Index]->Name);
  7741. }
  7742. FDragDropOperation = true;
  7743. TTransferOperationParam Param;
  7744. Param.TargetDirectory = TargetPath;
  7745. // upload, no temp dirs
  7746. Param.Temp = false;
  7747. Param.DragDrop = DragDrop;
  7748. Param.Options =
  7749. FLAGMASK(DraggingAllFilesFromDirView(osLocal, FileList), coAllFiles);
  7750. if (ForceQueue)
  7751. {
  7752. Param.Queue = asOn;
  7753. }
  7754. bool NoConfirmation = DragDrop ? (WinConfiguration->DDTransferConfirmation == asOff) : false;
  7755. if (ExecuteFileOperation(Operation, osLocal, FileList, NoConfirmation, &Param))
  7756. {
  7757. if (IsFileControl(DropSourceControl, osLocal))
  7758. {
  7759. Configuration->Usage->Inc(L"UploadsDragDropInternal");
  7760. }
  7761. else
  7762. {
  7763. Configuration->Usage->Inc(L"UploadsDragDropExternal");
  7764. }
  7765. }
  7766. }
  7767. __finally
  7768. {
  7769. FDragDropOperation = false;
  7770. delete FileList;
  7771. }
  7772. }
  7773. }
  7774. //---------------------------------------------------------------------------
  7775. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDFileOperation(
  7776. TObject * Sender, int Effect, UnicodeString /*SourcePath*/,
  7777. UnicodeString TargetPath, bool Paste, bool & /*DoOperation*/)
  7778. {
  7779. RemoteFileControlDragDropFileOperation(Sender, Effect, TargetPath, false, !Paste);
  7780. }
  7781. //---------------------------------------------------------------------------
  7782. void __fastcall TCustomScpExplorerForm::RemoteFileContolDDChooseEffect(
  7783. TObject * Sender, int grfKeyState, int & dwEffect)
  7784. {
  7785. DebugAssert(!IsLocalBrowserMode());
  7786. // if any drop effect is allowed at all (e.g. no drop to self and drop to parent)
  7787. if ((dwEffect != DROPEFFECT_NONE) &&
  7788. IsFileControl(DropSourceControl, osRemote))
  7789. {
  7790. // do not allow drop on remote panel (on free space, still allow drop on directories)
  7791. if ((Sender == RemoteDirView) && (DropSourceControl == RemoteDirView) &&
  7792. (RemoteDirView->DropTarget == NULL))
  7793. {
  7794. dwEffect = DROPEFFECT_NONE;
  7795. }
  7796. else
  7797. {
  7798. if (dwEffect == DROPEFFECT_COPY)
  7799. {
  7800. bool MoveCapable = FTerminal->IsCapable[fcRemoteMove];
  7801. // currently we support copying always (at least via temporary directory);
  7802. // remove associated checks once this all proves stable and working
  7803. bool CopyCapable = true;
  7804. // if we do not support neither of operations, there's no discussion
  7805. if (!MoveCapable && !CopyCapable)
  7806. {
  7807. dwEffect = DROPEFFECT_NONE;
  7808. }
  7809. else
  7810. {
  7811. // The default effect inside remote panel is move,
  7812. // unless we do not support it, but support copy
  7813. if (FLAGCLEAR(grfKeyState, MK_CONTROL))
  7814. {
  7815. dwEffect = MoveCapable ? DROPEFFECT_MOVE : DROPEFFECT_COPY;
  7816. }
  7817. else
  7818. {
  7819. // with ctrl-down, we want copy unless it is not supported
  7820. dwEffect = CopyCapable ? DROPEFFECT_COPY : DROPEFFECT_NONE;
  7821. }
  7822. }
  7823. }
  7824. }
  7825. }
  7826. }
  7827. //---------------------------------------------------------------------------
  7828. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDDragFileName(
  7829. TObject * Sender, TRemoteFile * File, UnicodeString & FileName)
  7830. {
  7831. DebugAssert(!IsLocalBrowserMode());
  7832. if (FDDTotalSize >= 0)
  7833. {
  7834. if (File->IsDirectory)
  7835. {
  7836. FDDTotalSize = -1;
  7837. }
  7838. else
  7839. {
  7840. FDDTotalSize += File->Size;
  7841. }
  7842. }
  7843. DebugAssert(!FDragTempDir.IsEmpty());
  7844. // TODO: this is quite ineffective
  7845. // TODO: what if invalid character replacement is disabled?
  7846. FileName = FDragTempDir + GUIConfiguration->CurrentCopyParam.ValidLocalFileName(File->FileName);
  7847. UnicodeString TransferFileName;
  7848. if (Sender == RemoteDriveView)
  7849. {
  7850. TransferFileName = UnixExcludeTrailingBackslash(File->FullFileName);
  7851. }
  7852. else
  7853. {
  7854. TransferFileName = File->FileName;
  7855. }
  7856. FDDFileList->AddObject(TransferFileName, File);
  7857. }
  7858. //---------------------------------------------------------------------------
  7859. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDDragDetect(
  7860. TObject * /*Sender*/, int /*grfKeyState*/, const TPoint & /*DetectStart*/,
  7861. const TPoint & /*Point*/, TDragDetectStatus /*DragStatus*/)
  7862. {
  7863. // sometimes we do not get DDEnd so the list is not released
  7864. SAFE_DESTROY(FDDFileList);
  7865. FDDFileList = new TStringList();
  7866. FDragTempDir = WinConfiguration->TemporaryDir();
  7867. FDDTotalSize = 0;
  7868. FDragDropSshTerminate.reset(NULL);
  7869. }
  7870. //---------------------------------------------------------------------------
  7871. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDQueryContinueDrag(
  7872. TObject * /*Sender*/, BOOL /*FEscapePressed*/, int /*grfKeyState*/,
  7873. HRESULT & Result)
  7874. {
  7875. if (Result == DRAGDROP_S_DROP)
  7876. {
  7877. try
  7878. {
  7879. GlobalDragImageList->HideDragImage();
  7880. try
  7881. {
  7882. RemoteFileControlDDTargetDrop();
  7883. }
  7884. catch(ESshTerminate & E)
  7885. {
  7886. FDragDropSshTerminate.reset(E.Clone());
  7887. }
  7888. }
  7889. catch (Exception &E)
  7890. {
  7891. // If downloading fails we need to cancel drag&drop, otherwise
  7892. // Explorer shows error
  7893. // But by the way exception probably never reach this point as
  7894. // it's catched on way.
  7895. // Fatal exceptions get here (like when opening a secondary shell extension for file duplication fails).
  7896. Result = DRAGDROP_S_CANCEL;
  7897. ShowExtendedException(Terminal, &E);
  7898. }
  7899. }
  7900. }
  7901. //---------------------------------------------------------------------------
  7902. void __fastcall TCustomScpExplorerForm::DirViewMatchMask(
  7903. TObject * /*Sender*/, UnicodeString FileName, bool Directory, __int64 Size,
  7904. TDateTime Modification, UnicodeString Masks, bool & Matches, bool AllowImplicitMatches)
  7905. {
  7906. TFileMasks::TParams MaskParams;
  7907. MaskParams.Size = Size;
  7908. MaskParams.Modification = Modification;
  7909. // this does not re-parse the mask if it is the same as the last time
  7910. FDirViewMatchMask = Masks;
  7911. bool ImplicitMatch;
  7912. Matches =
  7913. // Local and RecurseInclude parameters have no effect as there's no path.
  7914. // This should use potential MatchesFileName overload with ImplicitMatch argument.
  7915. FDirViewMatchMask.Matches(FileName, false, Directory, &MaskParams, true, ImplicitMatch) &&
  7916. (AllowImplicitMatches || !ImplicitMatch);
  7917. }
  7918. //---------------------------------------------------------------------------
  7919. void __fastcall TCustomScpExplorerForm::DirViewGetOverlay(
  7920. TObject * Sender, TListItem * Item, WORD & Indexes)
  7921. {
  7922. TCustomDirView * DirView = reinterpret_cast<TCustomDirView *>(Sender);
  7923. UnicodeString Ext;
  7924. if (dynamic_cast<TUnixDirView *>(DirView) != NULL)
  7925. {
  7926. Ext = UnixExtractFileExt(DirView->ItemFileName(Item));
  7927. }
  7928. else
  7929. {
  7930. Ext = ExtractFileExt(DirView->ItemFileName(Item));
  7931. }
  7932. if (SameText(Ext, Configuration->PartialExt))
  7933. {
  7934. Indexes |= oiPartial;
  7935. }
  7936. }
  7937. //---------------------------------------------------------------------------
  7938. bool __fastcall TCustomScpExplorerForm::CanPasteToDirViewFromClipBoard()
  7939. {
  7940. return
  7941. DirViewEnabled(osCurrent) &&
  7942. DirView(osCurrent)->CanPasteFromClipBoard();
  7943. }
  7944. //---------------------------------------------------------------------------
  7945. bool __fastcall TCustomScpExplorerForm::CanPasteFromClipBoard()
  7946. {
  7947. bool Result = false;
  7948. if (CanPasteToDirViewFromClipBoard())
  7949. {
  7950. Result = true;
  7951. }
  7952. else
  7953. {
  7954. UnicodeString ClipboardText;
  7955. if (NonEmptyTextFromClipboard(ClipboardText) &&
  7956. (ClipboardText.Pos(L"\n") == 0)) // it's already trimmed
  7957. {
  7958. if (StoredSessions->IsUrl(ClipboardText))
  7959. {
  7960. Result = true;
  7961. }
  7962. else
  7963. {
  7964. Result = DirViewEnabled(osCurrent);
  7965. }
  7966. }
  7967. }
  7968. return Result;
  7969. }
  7970. //---------------------------------------------------------------------------
  7971. void __fastcall TCustomScpExplorerForm::PasteFromClipBoard()
  7972. {
  7973. if (DoesClipboardContainOurFiles())
  7974. {
  7975. if (DebugAlwaysTrue(CanPasteToDirViewFromClipBoard()))
  7976. {
  7977. DebugAssert(!IsLocalBrowserMode());
  7978. TTerminalManager * Manager = TTerminalManager::Instance();
  7979. TTerminal * TargetTerminal = Manager->ActiveTerminal;
  7980. Manager->ActiveTerminal = FClipboardTerminal;
  7981. ExecuteFileOperation(foRemoteCopy, osRemote, FClipboardFileList.get(), false, TargetTerminal);
  7982. }
  7983. }
  7984. else if (CanPasteToDirViewFromClipBoard())
  7985. {
  7986. DirView(osCurrent)->PasteFromClipBoard();
  7987. }
  7988. else
  7989. {
  7990. UnicodeString ClipboardText;
  7991. if (NonEmptyTextFromClipboard(ClipboardText))
  7992. {
  7993. if (StoredSessions->IsUrl(ClipboardText))
  7994. {
  7995. NewSession(ClipboardText);
  7996. }
  7997. else
  7998. {
  7999. DirView(osCurrent)->Path = ClipboardText;
  8000. }
  8001. }
  8002. }
  8003. }
  8004. //---------------------------------------------------------------------------
  8005. void __fastcall TCustomScpExplorerForm::FileListFromClipboard()
  8006. {
  8007. // TBD
  8008. }
  8009. //---------------------------------------------------------------------------
  8010. void __fastcall TCustomScpExplorerForm::SelectAll(TOperationSide Side, TSelectMode Mode)
  8011. {
  8012. TCustomDirView * ADirView = DirView(Side);
  8013. ADirView->SelectAll(Mode);
  8014. ADirView->SetFocus();
  8015. }
  8016. //---------------------------------------------------------------------------
  8017. void __fastcall TCustomScpExplorerForm::SelectByMask(TOperationSide Side, bool Select)
  8018. {
  8019. TCustomDirView * ADirView = DirView(Side);
  8020. TFileFilter Filter;
  8021. DefaultFileFilter(Filter);
  8022. if (DoSelectMaskDialog(ADirView, Select, Filter))
  8023. {
  8024. Configuration->Usage->Inc(L"MaskSelections");
  8025. ADirView->SelectFiles(Filter, Select);
  8026. ADirView->SetFocus();
  8027. }
  8028. }
  8029. //---------------------------------------------------------------------------
  8030. void __fastcall TCustomScpExplorerForm::RestoreSelectedNames(TOperationSide Side)
  8031. {
  8032. TCustomDirView * ADirView = DirView(Side);
  8033. ADirView->RestoreSelectedNames();
  8034. ADirView->SetFocus();
  8035. }
  8036. //---------------------------------------------------------------------------
  8037. void __fastcall TCustomScpExplorerForm::SelectSameExt(bool Select)
  8038. {
  8039. TCustomDirView * CurrentDirView = DirView(osCurrent);
  8040. if (DebugAlwaysTrue(CurrentDirView->ItemFocused != NULL))
  8041. {
  8042. UnicodeString FileName = CurrentDirView->ItemFileName(CurrentDirView->ItemFocused);
  8043. UnicodeString Ext;
  8044. if (!IsSideLocalBrowser(osCurrent))
  8045. {
  8046. Ext = UnixExtractFileExt(FileName);
  8047. }
  8048. else
  8049. {
  8050. Ext = ExtractFileExt(FileName);
  8051. }
  8052. if (Ext.IsEmpty())
  8053. {
  8054. Ext = L".";
  8055. }
  8056. TFileFilter Filter;
  8057. Filter.Masks = FORMAT(L"*%s", (Ext));
  8058. Filter.Directories = false;
  8059. CurrentDirView->SelectFiles(Filter, Select);
  8060. }
  8061. }
  8062. //---------------------------------------------------------------------------
  8063. UnicodeString __fastcall TCustomScpExplorerForm::FileStatusBarText(
  8064. const TStatusFileInfo & FileInfo, TOperationSide Side)
  8065. {
  8066. UnicodeString Result;
  8067. if (!FIncrementalSearch.IsEmpty() && (Side == GetSide(osCurrent)))
  8068. {
  8069. Result = FormatIncrementalSearchStatus(FIncrementalSearch, FIncrementalSearchHaveNext);
  8070. }
  8071. else if (!IsSideLocalBrowser(Side) && ((Terminal == NULL) || Terminal->Disconnected))
  8072. {
  8073. // noop
  8074. }
  8075. else
  8076. {
  8077. Result =
  8078. FMTLOAD(FILE_INFO_FORMAT,
  8079. (FormatBytes(FileInfo.SelectedSize),
  8080. FormatBytes(FileInfo.FilesSize),
  8081. FormatNumber(FileInfo.SelectedCount),
  8082. FormatNumber(FileInfo.FilesCount)));
  8083. }
  8084. return Result;
  8085. }
  8086. //---------------------------------------------------------------------------
  8087. void __fastcall TCustomScpExplorerForm::FileStatusBarPanelClick(
  8088. TTBXStatusPanel * Panel, TOperationSide Side)
  8089. {
  8090. if (Panel->Index == 1)
  8091. {
  8092. ToggleShowHiddenFiles();
  8093. }
  8094. else if (Panel->Index == 2)
  8095. {
  8096. Filter(Side);
  8097. }
  8098. }
  8099. //---------------------------------------------------------------------------
  8100. void __fastcall TCustomScpExplorerForm::UpdateFileStatusBar(
  8101. TTBXStatusBar * StatusBar, const TStatusFileInfo & FileInfo, TOperationSide Side)
  8102. {
  8103. DebugAssert(!StatusBar->SimplePanel);
  8104. StatusBar->Panels->Items[0]->Caption = FileStatusBarText(FileInfo, Side);
  8105. UpdateFileStatusExtendedPanels(StatusBar, FileInfo);
  8106. }
  8107. //---------------------------------------------------------------------------
  8108. void __fastcall TCustomScpExplorerForm::UpdateFileStatusExtendedPanels(
  8109. TTBXStatusBar * StatusBar, const TStatusFileInfo & FileInfo)
  8110. {
  8111. DebugAssert(StatusBar->Panels->Count >= 3);
  8112. TTBXStatusPanel * HiddenFilesPanel = StatusBar->Panels->Items[1];
  8113. if (FileInfo.HiddenCount > 0)
  8114. {
  8115. HiddenFilesPanel->Caption = FMTLOAD(FILE_INFO_HIDDEN2, (FormatNumber(FileInfo.HiddenCount)));
  8116. HiddenFilesPanel->ViewPriority = 90; // <100 allows hiding panel when it does not fit
  8117. }
  8118. else
  8119. {
  8120. HiddenFilesPanel->ViewPriority = 0;
  8121. // not really necessary, just to cleanup no-longer-valid data
  8122. HiddenFilesPanel->Caption = L"";
  8123. }
  8124. TTBXStatusPanel * FilteredFilesPanel = StatusBar->Panels->Items[2];
  8125. if (FileInfo.FilteredCount > 0)
  8126. {
  8127. FilteredFilesPanel->Caption = FMTLOAD(FILE_INFO_FILTERED2, (FormatNumber(FileInfo.FilteredCount)));
  8128. FilteredFilesPanel->ViewPriority = 90; // <100 allows hiding panel when it does not fit
  8129. }
  8130. else
  8131. {
  8132. FilteredFilesPanel->ViewPriority = 0;
  8133. // not really necessary, just to cleanup no-longer-valid data
  8134. FilteredFilesPanel->Caption = L"";
  8135. }
  8136. }
  8137. //---------------------------------------------------------------------------
  8138. void __fastcall TCustomScpExplorerForm::RemoteStatusBarClick(
  8139. TObject * /*Sender*/)
  8140. {
  8141. if (DirView(osRemote)->Enabled)
  8142. {
  8143. DirView(osRemote)->SetFocus();
  8144. }
  8145. }
  8146. //---------------------------------------------------------------------------
  8147. void __fastcall TCustomScpExplorerForm::ToggleQueueVisibility()
  8148. {
  8149. TQueueViewConfiguration Config = WinConfiguration->QueueView;
  8150. switch (Config.Show)
  8151. {
  8152. case qvShow:
  8153. if ((FQueueStatus != NULL) && (FQueueStatus->Count > 0))
  8154. {
  8155. Config.Show = qvHide;
  8156. }
  8157. else
  8158. {
  8159. Config.Show = Config.LastHideShow;
  8160. }
  8161. break;
  8162. case qvHideWhenEmpty:
  8163. if (ComponentVisible[fcQueueView])
  8164. {
  8165. Config.Show = qvHide;
  8166. }
  8167. else
  8168. {
  8169. Config.LastHideShow = Config.Show;
  8170. Config.Show = qvShow;
  8171. }
  8172. break;
  8173. case qvHide:
  8174. Config.LastHideShow = Config.Show;
  8175. Config.Show = qvShow;
  8176. break;
  8177. }
  8178. WinConfiguration->QueueView = Config;
  8179. }
  8180. //---------------------------------------------------------------------------
  8181. UnicodeString __fastcall TCustomScpExplorerForm::PathForCaption()
  8182. {
  8183. UnicodeString Result;
  8184. if (FTerminal != NULL)
  8185. {
  8186. switch (WinConfiguration->PathInCaption)
  8187. {
  8188. case picShort:
  8189. Result = TTerminalManager::Instance()->GetTerminalShortPath(FTerminal);
  8190. break;
  8191. case picFull:
  8192. Result = FTerminal->CurrentDirectory;
  8193. break;
  8194. }
  8195. }
  8196. return Result;
  8197. }
  8198. //---------------------------------------------------------------------------
  8199. TColor __fastcall TCustomScpExplorerForm::PanelColor()
  8200. {
  8201. TColor Result = GetWindowColor(FSessionColor);
  8202. return Result;
  8203. }
  8204. //---------------------------------------------------------------------------
  8205. TColor __fastcall TCustomScpExplorerForm::DisabledPanelColor()
  8206. {
  8207. TColor Result = (WinConfiguration->UseDarkTheme() ? static_cast<TColor>(RGB(0x40, 0x40, 0x40)) : clBtnFace);
  8208. return Result;
  8209. }
  8210. //---------------------------------------------------------------------------
  8211. void __fastcall TCustomScpExplorerForm::UpdateControls()
  8212. {
  8213. TTerminalManager::Instance()->UpdateAppTitle();
  8214. // WORKAROUND: Disabling list view when it is not showing yet does not set its
  8215. // background to gray on Windows 7 (works on Windows 10).
  8216. // See also EnableControl
  8217. if (Showing)
  8218. {
  8219. if (Terminal != NULL)
  8220. {
  8221. // Update path when it changes
  8222. if ((SessionsPageControl->ActivePage != NULL) && (GetSessionTabTerminal(SessionsPageControl->ActivePage) == Terminal))
  8223. {
  8224. UpdateSessionTab(SessionsPageControl->ActivePage);
  8225. }
  8226. }
  8227. NonVisualDataModule->ReconnectSessionAction->Update();
  8228. ReconnectToolbar->Visible = NonVisualDataModule->ReconnectSessionAction->Visible;
  8229. // ReconnectSessionAction is hidden when disabled, so enabling it actualy resizes the toolbar
  8230. CenterReconnectToolbar();
  8231. bool HasTerminal = HasActiveTerminal();
  8232. if (HasTerminal)
  8233. {
  8234. if (!RemoteDirView->Enabled)
  8235. {
  8236. RemoteDirView->Enabled = true;
  8237. if (FRemoteDirViewWasFocused)
  8238. {
  8239. ActiveControl = RemoteDirView;
  8240. }
  8241. }
  8242. RemoteDirView->Color = PanelColor();
  8243. }
  8244. else
  8245. {
  8246. if (RemoteDirView->Enabled)
  8247. {
  8248. // This is first called when the form is being constructed
  8249. // (not anymore due to Showing test above)
  8250. // but the false is overriden in the constructor later.
  8251. // An even later in TScpCommanderForm::DoShow()
  8252. FRemoteDirViewWasFocused = (ActiveControl == RemoteDirView);
  8253. RemoteDirView->Enabled = false;
  8254. }
  8255. RemoteDirView->Color = DisabledPanelColor();
  8256. }
  8257. RemoteDirView->Font->Color = GetWindowTextColor(RemoteDirView->Color);
  8258. RemoteDriveView->Enabled = RemoteDirView->Enabled;
  8259. RemoteDriveView->Color = RemoteDirView->Color;
  8260. RemoteDriveView->Font->Color = RemoteDirView->Font->Color;
  8261. QueueView3->Enabled = HasTerminal && Terminal->IsCapable[fcBackgroundTransfers];
  8262. QueueView3->Color = QueueView3->Enabled ? GetWindowColor() : DisabledPanelColor();
  8263. QueueView3->Font->Color = GetWindowTextColor(QueueView3->Color);
  8264. QueueFileList->Enabled = QueueView3->Enabled;
  8265. QueueFileList->Color = QueueView3->Color;
  8266. QueueFileList->Font->Color = QueueView3->Font->Color;
  8267. QueueLabelUpdateStatus();
  8268. RemoteDirView->DarkMode = WinConfiguration->UseDarkTheme();
  8269. RemoteDriveView->DarkMode = RemoteDirView->DarkMode;
  8270. if (FImmersiveDarkMode != WinConfiguration->UseDarkTheme())
  8271. {
  8272. UpdateDarkMode();
  8273. }
  8274. reinterpret_cast<TTBCustomItem *>(GetComponent(fcRemotePathComboBox))->Enabled = HasTerminal;
  8275. }
  8276. }
  8277. //---------------------------------------------------------------------------
  8278. void __fastcall TCustomScpExplorerForm::DoDirViewLoaded(TCustomDirView * /*Sender*/)
  8279. {
  8280. UpdateControls();
  8281. }
  8282. //---------------------------------------------------------------------------
  8283. void __fastcall TCustomScpExplorerForm::DirViewLoaded(
  8284. TObject * Sender)
  8285. {
  8286. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  8287. DebugAssert(DirView != NULL);
  8288. DoDirViewLoaded(DirView);
  8289. TransferPresetAutoSelect();
  8290. }
  8291. //---------------------------------------------------------------------------
  8292. void __fastcall TCustomScpExplorerForm::StartUpdates()
  8293. {
  8294. TUpdatesConfiguration Updates = WinConfiguration->Updates;
  8295. // first run after installation
  8296. if (double(Updates.LastCheck) == 0)
  8297. {
  8298. // make sure next time there will be an update (if enabled)
  8299. Updates.LastCheck = TDateTime(1);
  8300. WinConfiguration->Updates = Updates;
  8301. }
  8302. else if ((double(Updates.Period) > 0) &&
  8303. (Now() - Updates.LastCheck >= Updates.Period))
  8304. {
  8305. TThreadMethod OnUpdatesChecked = NULL;
  8306. if (!IsUWP())
  8307. {
  8308. OnUpdatesChecked = UpdatesChecked;
  8309. }
  8310. StartUpdateThread(OnUpdatesChecked);
  8311. }
  8312. }
  8313. //---------------------------------------------------------------------------
  8314. void __fastcall TCustomScpExplorerForm::UpdatesChecked()
  8315. {
  8316. UnicodeString Message;
  8317. bool New;
  8318. TQueryType Type;
  8319. GetUpdatesMessage(Message, New, Type, false);
  8320. if (!Message.IsEmpty())
  8321. {
  8322. if (New)
  8323. {
  8324. Message = FMTLOAD(NEW_VERSION_CLICK, (Message));
  8325. }
  8326. if (!New && (Type != qtWarning))
  8327. {
  8328. PostNote(UnformatMessage(Message), 0, UpdatesNoteClicked, NULL);
  8329. }
  8330. else
  8331. {
  8332. Notify(NULL, Message, Type, true, UpdatesNoteClicked);
  8333. }
  8334. if (New)
  8335. {
  8336. Configuration->Usage->Inc(L"UpdateNotifications");
  8337. }
  8338. }
  8339. }
  8340. //---------------------------------------------------------------------------
  8341. void __fastcall TCustomScpExplorerForm::UpdatesNoteClicked(TObject * /*Sender*/)
  8342. {
  8343. RestoreApp();
  8344. bool CanDisplay = !NonVisualDataModule->Busy;
  8345. if (!CanDisplay && (Screen->ActiveForm != NULL))
  8346. {
  8347. CanDisplay = (Screen->ActiveForm->Perform(WM_CAN_DISPLAY_UPDATES, 0, 0) != 0);
  8348. }
  8349. if (CanDisplay)
  8350. {
  8351. Configuration->Usage->Inc(L"UpdateNotificationsClicked");
  8352. CheckForUpdates(true);
  8353. }
  8354. else
  8355. {
  8356. Configuration->Usage->Inc(L"UpdateNotificationsBusy");
  8357. }
  8358. }
  8359. //---------------------------------------------------------------------------
  8360. void __fastcall TCustomScpExplorerForm::GetTransferPresetAutoSelectData(
  8361. TCopyParamRuleData & Data)
  8362. {
  8363. DebugAssert(Terminal != NULL);
  8364. Data.HostName = Terminal->SessionData->HostNameExpanded;
  8365. Data.UserName = Terminal->SessionData->UserNameExpanded;
  8366. Data.RemoteDirectory = RemoteDirView->PathName;
  8367. }
  8368. //---------------------------------------------------------------------------
  8369. void __fastcall TCustomScpExplorerForm::TransferPresetAutoSelect()
  8370. {
  8371. // Terminal can be null when we are changing local directory implicitly,
  8372. // because it has been deleted, while no session is connected
  8373. // (Login dialog is open)
  8374. if (FAllowTransferPresetAutoSelect && (Terminal != NULL))
  8375. {
  8376. DebugAssert(!IsLocalBrowserMode());
  8377. TCopyParamRuleData Data;
  8378. GetTransferPresetAutoSelectData(Data);
  8379. int CopyParamIndex = GUIConfiguration->CopyParamList->Find(Data);
  8380. UnicodeString CopyParamCurrent = GUIConfiguration->CopyParamCurrent;
  8381. if (CopyParamIndex < 0)
  8382. {
  8383. // there is no preset that matches autoselection
  8384. // set preset that we consider "default"
  8385. FCopyParamAutoSelected = L""; // forget last autoselected preset
  8386. GUIConfiguration->CopyParamCurrent = FCopyParamDefault;
  8387. }
  8388. else
  8389. {
  8390. // there is preset matching autoselection
  8391. UnicodeString CopyParamName = GUIConfiguration->CopyParamList->Names[CopyParamIndex];
  8392. if (CopyParamName == FCopyParamAutoSelected)
  8393. {
  8394. // autoselected the same preset as the last time
  8395. // make no change (i.e. preserve custom user preset, if any)
  8396. }
  8397. else
  8398. {
  8399. // autoselected the different preset then the last time (or there
  8400. // was default preset set)
  8401. FCopyParamAutoSelected = CopyParamName; // remember autoselection
  8402. GUIConfiguration->CopyParamCurrent = CopyParamName;
  8403. }
  8404. }
  8405. if (GUIConfiguration->CopyParamCurrent != CopyParamCurrent)
  8406. {
  8407. if (CopyParamIndex >= 0)
  8408. {
  8409. Configuration->Usage->Inc(L"CopyParamAutoSelects");
  8410. }
  8411. TTransferPresetNoteData * Data = new TTransferPresetNoteData;
  8412. try
  8413. {
  8414. int Fmt =
  8415. (CopyParamIndex < 0) ?
  8416. (GUIConfiguration->CopyParamIndex < 0 ? COPY_PARAM_DEFAULT_NORM : COPY_PARAM_DEFAULT_CUSTOM) :
  8417. COPY_PARAM_AUTOSELECTED;
  8418. UnicodeString Message = FMTLOAD(Fmt, (StripHotkey(GUIConfiguration->CopyParamCurrent)));
  8419. Data->Message = MainInstructions(Message);
  8420. int CopyParamAttrs = Terminal->UsableCopyParamAttrs(0).General;
  8421. UnicodeString Info = GUIConfiguration->CurrentCopyParam.GetInfoStr(L"\n",
  8422. CopyParamAttrs);
  8423. if (CopyParamIndex >= 0)
  8424. {
  8425. DebugAssert(GUIConfiguration->CopyParamList->Rules[CopyParamIndex] != NULL);
  8426. Info = FORMAT(L"%s\n \n%s", (Info,
  8427. FMTLOAD(COPY_PARAM_RULE,
  8428. (GUIConfiguration->CopyParamList->Rules[CopyParamIndex]->GetInfoStr(L"\n")))));
  8429. }
  8430. Data->Message += L"\n\n" + Info;
  8431. if (WinConfiguration->CopyParamAutoSelectNotice)
  8432. {
  8433. TransferPresetNoteMessage(Data, true);
  8434. }
  8435. else
  8436. {
  8437. PostNote(Message, 0, TransferPresetNoteClicked, Data);
  8438. Data = NULL; // ownership passed
  8439. }
  8440. }
  8441. __finally
  8442. {
  8443. delete Data;
  8444. }
  8445. }
  8446. }
  8447. }
  8448. //---------------------------------------------------------------------------
  8449. void __fastcall TCustomScpExplorerForm::TransferPresetNoteMessage(
  8450. TTransferPresetNoteData * NoteData, bool AllowNeverAskAgain)
  8451. {
  8452. DebugAssert(NoteData != NULL);
  8453. DebugAssert(!IsLocalBrowserMode());
  8454. TMessageParams Params(AllowNeverAskAgain ? mpNeverAskAgainCheck : 0);
  8455. TQueryButtonAlias Aliases[1];
  8456. Aliases[0].Button = qaIgnore; // "ignore" is after "ok"
  8457. Aliases[0].Alias = LoadStr(CONFIGURE_BUTTON);
  8458. Params.Aliases = Aliases;
  8459. Params.AliasesCount = LENOF(Aliases);
  8460. unsigned int Result =
  8461. MoreMessageDialog(NoteData->Message, NULL, qtInformation,
  8462. qaOK | qaIgnore, HELP_COPY_PARAM_AUTOSELECTED, &Params);
  8463. switch (Result)
  8464. {
  8465. case qaNeverAskAgain:
  8466. DebugAssert(AllowNeverAskAgain);
  8467. WinConfiguration->CopyParamAutoSelectNotice = false;
  8468. break;
  8469. case qaIgnore:
  8470. PreferencesDialog(pmPresets);
  8471. break;
  8472. }
  8473. }
  8474. //---------------------------------------------------------------------------
  8475. void __fastcall TCustomScpExplorerForm::TransferPresetNoteClicked(TObject * Sender)
  8476. {
  8477. // as of now useless, as this is used for notes only, never for balloons, ...
  8478. RestoreApp();
  8479. // .. and we should never be busy here
  8480. if (DebugAlwaysTrue(!NonVisualDataModule->Busy))
  8481. {
  8482. TransferPresetNoteMessage(DebugNotNull(dynamic_cast<TTransferPresetNoteData *>(Sender)), false);
  8483. }
  8484. }
  8485. //---------------------------------------------------------------------------
  8486. void __fastcall TCustomScpExplorerForm::PreferencesDialog(
  8487. TPreferencesMode APreferencesMode)
  8488. {
  8489. std::unique_ptr<TPreferencesDialogData> PreferencesData;
  8490. TCopyParamRuleData Data;
  8491. if (Terminal != NULL)
  8492. {
  8493. PreferencesData.reset(new TPreferencesDialogData());
  8494. GetTransferPresetAutoSelectData(Data);
  8495. PreferencesData->CopyParamRuleData = &Data;
  8496. }
  8497. DoPreferencesDialog(APreferencesMode, PreferencesData.get());
  8498. }
  8499. //---------------------------------------------------------------------------
  8500. void __fastcall TCustomScpExplorerForm::AdHocCustomCommandValidate(
  8501. const TCustomCommandType & Command)
  8502. {
  8503. if (CustomCommandState(Command, FEditingFocusedAdHocCommand, ccltAll) <= 0)
  8504. {
  8505. throw Exception(FMTLOAD(CUSTOM_COMMAND_IMPOSSIBLE2, (Command.Command)));
  8506. }
  8507. }
  8508. //---------------------------------------------------------------------------
  8509. void __fastcall TCustomScpExplorerForm::AdHocCustomCommand(bool OnFocused)
  8510. {
  8511. bool RemoteAllowed = CustomCommandRemoteAllowed();
  8512. TCustomCommandType Command;
  8513. // make sure we use local custom command when remote are not supported
  8514. if (RemoteAllowed || FLAGSET(FLastCustomCommand.Params, ccLocal))
  8515. {
  8516. Command = FLastCustomCommand;
  8517. }
  8518. else
  8519. {
  8520. Command.Params = Command.Params | ccLocal;
  8521. }
  8522. Command.Name = LoadStr(CUSTOM_COMMAND_AD_HOC_NAME);
  8523. FEditingFocusedAdHocCommand = OnFocused;
  8524. bool LocalSide = IsSideLocalBrowser(FCurrentSide);
  8525. int Options =
  8526. FLAGMASK((!RemoteAllowed || LocalSide), ccoDisableRemote) |
  8527. FLAGMASK(LocalSide, ccoDisableRemoteFiles);
  8528. if (DoCustomCommandDialog(Command, WinConfiguration->CustomCommandList,
  8529. ccmAdHoc, Options, AdHocCustomCommandValidate, NULL))
  8530. {
  8531. FLastCustomCommand = Command;
  8532. UpdateCustomCommandsToolbar();
  8533. ExecuteFileOperation(foCustomCommand, osRemote, OnFocused, false, &Command);
  8534. }
  8535. }
  8536. //---------------------------------------------------------------------------
  8537. void __fastcall TCustomScpExplorerForm::LastCustomCommand(bool OnFocused)
  8538. {
  8539. DebugAssert(!FLastCustomCommand.Command.IsEmpty());
  8540. int State = CustomCommandState(FLastCustomCommand, OnFocused, ccltAll);
  8541. DebugAssert(State > 0);
  8542. if (State <= 0)
  8543. {
  8544. throw Exception(FMTLOAD(CUSTOM_COMMAND_IMPOSSIBLE2, (FLastCustomCommand.Command)));
  8545. }
  8546. ExecuteFileOperation(foCustomCommand, osRemote, OnFocused, false, &FLastCustomCommand);
  8547. }
  8548. //---------------------------------------------------------------------------
  8549. bool __fastcall TCustomScpExplorerForm::GetLastCustomCommand(bool OnFocused,
  8550. TCustomCommandType & Command, int & State)
  8551. {
  8552. bool Result = !FLastCustomCommand.Command.IsEmpty();
  8553. if (Result)
  8554. {
  8555. Command = FLastCustomCommand;
  8556. State = CustomCommandState(FLastCustomCommand, OnFocused, ccltAll);
  8557. }
  8558. return Result;
  8559. }
  8560. //---------------------------------------------------------------------------
  8561. void __fastcall TCustomScpExplorerForm::BeforeAction()
  8562. {
  8563. ResetIncrementalSearch();
  8564. }
  8565. //---------------------------------------------------------------------------
  8566. void __fastcall TCustomScpExplorerForm::PostComponentHide(Byte Component)
  8567. {
  8568. DebugAssert(ComponentVisible[Component]);
  8569. PostMessage(Handle, WM_COMPONENT_HIDE, Component, 0);
  8570. }
  8571. //---------------------------------------------------------------------------
  8572. void __fastcall TCustomScpExplorerForm::QueueSplitterDblClick(TObject * /*Sender*/)
  8573. {
  8574. // when queue panel is resized here directly, the status bar is stretched
  8575. // over whole space the panel occupied
  8576. PostComponentHide(fcQueueView);
  8577. }
  8578. //---------------------------------------------------------------------------
  8579. void __fastcall TCustomScpExplorerForm::QueueFileListSplitterDblClick(TObject *)
  8580. {
  8581. ComponentVisible[fcQueueFileList] = false;
  8582. }
  8583. //---------------------------------------------------------------------------
  8584. void __fastcall TCustomScpExplorerForm::ThemeChanged()
  8585. {
  8586. // We hoped this will refresh scrollbar colors, but it does not have any effect here.
  8587. RefreshColorMode();
  8588. ResetSysDarkTheme();
  8589. ConfigurationChanged();
  8590. ConfigureInterface();
  8591. // Should be called for all controls
  8592. RemoteDirView->Perform(WM_THEMECHANGED, 0, 0);
  8593. }
  8594. //---------------------------------------------------------------------------
  8595. void __fastcall TCustomScpExplorerForm::WMSettingChange(TMessage & Message)
  8596. {
  8597. // Do not handle, when shutting down anyway (maybe also when not setup completelly yet?)
  8598. if (!FInvalid &&
  8599. (Message.LParam != 0) &&
  8600. (wcscmp(reinterpret_cast<LPCWCH>(Message.LParam), L"ImmersiveColorSet") == 0))
  8601. {
  8602. ThemeChanged();
  8603. }
  8604. TForm::Dispatch(&Message);
  8605. }
  8606. //---------------------------------------------------------------------------
  8607. void __fastcall TCustomScpExplorerForm::Dispatch(void * Message)
  8608. {
  8609. TMessage * M = static_cast<TMessage*>(Message);
  8610. switch (M->Msg)
  8611. {
  8612. case CM_DIALOGCHAR:
  8613. CMDialogChar(*M);
  8614. break;
  8615. case WM_APPCOMMAND:
  8616. WMAppCommand(*M);
  8617. break;
  8618. case WM_SYSCOMMAND:
  8619. WMSysCommand(*M);
  8620. break;
  8621. case WM_QUERYENDSESSION:
  8622. WMQueryEndSession(*M);
  8623. break;
  8624. case WM_ENDSESSION:
  8625. WMEndSession(*reinterpret_cast<TWMEndSession *>(M));
  8626. break;
  8627. #ifdef _DEBUG
  8628. case WM_WINDOWPOSCHANGED:
  8629. WMWindowPosChanged(*reinterpret_cast<TWMWindowPosMsg *>(M));
  8630. break;
  8631. case CM_CUSTOMSTYLECHANGED:
  8632. TForm::Dispatch(Message);
  8633. break;
  8634. #endif
  8635. case WM_COMPONENT_HIDE:
  8636. {
  8637. Byte Component = static_cast<Byte>(M->WParam);
  8638. // sanity check
  8639. if (ComponentVisible[Component])
  8640. {
  8641. // special treatment
  8642. if (Component == fcQueueView)
  8643. {
  8644. ToggleQueueVisibility();
  8645. DebugAssert(!ComponentVisible[fcQueueView]);
  8646. }
  8647. else
  8648. {
  8649. ComponentVisible[Component] = false;
  8650. }
  8651. }
  8652. }
  8653. break;
  8654. case WM_COPYDATA:
  8655. WMCopyData(*M);
  8656. break;
  8657. case WM_MANAGES_CAPTION:
  8658. // caption managed in TTerminalManager::UpdateAppTitle()
  8659. M->Result = 1;
  8660. break;
  8661. case WM_WANTS_MOUSEWHEEL:
  8662. M->Result = 1;
  8663. break;
  8664. case CM_SHOWINGCHANGED:
  8665. CMShowingChanged(*M);
  8666. break;
  8667. case WM_CLOSE:
  8668. WMClose(*M);
  8669. break;
  8670. case WM_DPICHANGED:
  8671. WMDpiChanged(*M);
  8672. break;
  8673. case CM_DPICHANGED:
  8674. CMDpiChanged(*M);
  8675. break;
  8676. case WM_SETTINGCHANGE:
  8677. WMSettingChange(*M);
  8678. break;
  8679. case CM_DIALOGKEY:
  8680. CMDialogKey(*reinterpret_cast<TWMKeyDown *>(M));
  8681. break;
  8682. default:
  8683. TForm::Dispatch(Message);
  8684. break;
  8685. }
  8686. }
  8687. //---------------------------------------------------------------------------
  8688. void __fastcall TCustomScpExplorerForm::UpdateImages()
  8689. {
  8690. // noop
  8691. }
  8692. //---------------------------------------------------------------------------
  8693. void __fastcall TCustomScpExplorerForm::CMDpiChanged(TMessage & Message)
  8694. {
  8695. TForm::Dispatch(&Message);
  8696. FSessionColors->Clear();
  8697. AddFixedSessionImages();
  8698. RegenerateSessionColorsImageList(FSessionColors, FSessionColorMaskImageIndex);
  8699. UpdateImages();
  8700. }
  8701. //---------------------------------------------------------------------------
  8702. void __fastcall TCustomScpExplorerForm::WMDpiChanged(TMessage & Message)
  8703. {
  8704. TForm::Dispatch(&Message);
  8705. Configuration->Usage->Inc(L"PixelsPerInchChanged");
  8706. UpdatePixelsPerInchMainWindowCounter();
  8707. }
  8708. //---------------------------------------------------------------------------
  8709. void __fastcall TCustomScpExplorerForm::WMClose(TMessage & Message)
  8710. {
  8711. // Cannot close window while we are busy.
  8712. // We cannot test this in FormCloseQuery as that is called also from
  8713. // Close(), which is called by CloseApplicationAction. So we can be busy
  8714. // there already even, when it is legitimate to close the application.
  8715. // Possibly a better place to handle this would be WMSysCommand.
  8716. if (NonVisualDataModule->Busy)
  8717. {
  8718. Message.Result = 1;
  8719. }
  8720. else
  8721. {
  8722. TForm::Dispatch(&Message);
  8723. }
  8724. }
  8725. //---------------------------------------------------------------------------
  8726. void __fastcall TCustomScpExplorerForm::CMShowingChanged(TMessage & Message)
  8727. {
  8728. if (!FShowingChanged)
  8729. {
  8730. AddStartupSequence(L"D");
  8731. FShowingChanged = true;
  8732. }
  8733. TForm::Dispatch(&Message);
  8734. // Now the window is visible (TForm::Dispatch is what usually takes long, like when loading a "local" network directory)
  8735. if (Showing && !FStarted)
  8736. {
  8737. FStarted = true;
  8738. InterfaceStarted();
  8739. }
  8740. if (Showing && (Terminal == NULL))
  8741. {
  8742. // When we are starting minimized (i.e. from an installer),
  8743. // postpone showing Login dialog until we get restored.
  8744. // Otherwise the Login dialog (and Authentication window) show restored
  8745. // over invisible (minimized) main window.
  8746. if (WindowState == wsMinimized)
  8747. {
  8748. FNeedSession = true;
  8749. }
  8750. else
  8751. {
  8752. // This happens before application ever goes idle, so the toolbars would
  8753. // stay enabled (initial state) until the Login dialog is dismissed.
  8754. UpdateActions();
  8755. NonVisualDataModule->StartBusy();
  8756. try
  8757. {
  8758. // Need to process WM_ACTIVATEAPP before showing the Login dialog,
  8759. // otherwise the dialog does not receive focus.
  8760. // With Commander interface the ProcessMessages is called already
  8761. // by TDriveView, but with Explorer interface, we need to call it explicily
  8762. Application->ProcessMessages();
  8763. // do not reload sessions, they have been loaded just now (optimization)
  8764. NeedSession(true);
  8765. }
  8766. __finally
  8767. {
  8768. NonVisualDataModule->EndBusy();
  8769. }
  8770. }
  8771. }
  8772. }
  8773. //---------------------------------------------------------------------------
  8774. void __fastcall TCustomScpExplorerForm::CenterReconnectToolbar()
  8775. {
  8776. ReconnectToolbar->Left = (ReconnectToolbar->Parent->ClientWidth - ReconnectToolbar->Width) / 2;
  8777. ReconnectToolbar->Top = (ReconnectToolbar->Parent->ClientHeight - ReconnectToolbar->Height) / 2;
  8778. }
  8779. //---------------------------------------------------------------------------
  8780. void __fastcall TCustomScpExplorerForm::FormConstrainedResize(
  8781. TObject * /*Sender*/, int & MinWidth, int & MinHeight, int & MaxWidth,
  8782. int & MaxHeight)
  8783. {
  8784. // workaround for bug in TWinControl.CalcConstraints
  8785. // Check for empty rect (restore from iconic state) is done there only after
  8786. // call to AdjustClientRect, which enlarges the rect (for forms).
  8787. TRect R = GetClientRect();
  8788. // when restoring from iconic state, place no restrictions
  8789. if (IsRectEmpty(R))
  8790. {
  8791. MinWidth = 0;
  8792. MinHeight = 0;
  8793. MaxWidth = 32000;
  8794. MaxHeight = 32000;
  8795. }
  8796. }
  8797. //---------------------------------------------------------------------------
  8798. void __fastcall TCustomScpExplorerForm::GetSpaceAvailable(const UnicodeString Path,
  8799. TSpaceAvailable & ASpaceAvailable, bool & Close)
  8800. {
  8801. // terminal can be already closed (e.g. dropped connection)
  8802. if ((Terminal != NULL) && Terminal->IsCapable[fcCheckingSpaceAvailable])
  8803. {
  8804. Configuration->Usage->Inc(L"SpaceAvailableChecks");
  8805. try
  8806. {
  8807. Terminal->SpaceAvailable(Path, ASpaceAvailable);
  8808. }
  8809. catch(...)
  8810. {
  8811. if (!Terminal->Active)
  8812. {
  8813. Close = true;
  8814. }
  8815. throw;
  8816. }
  8817. }
  8818. }
  8819. //---------------------------------------------------------------------------
  8820. void __fastcall TCustomScpExplorerForm::FileSystemInfo()
  8821. {
  8822. const TSessionInfo & SessionInfo = Terminal->GetSessionInfo();
  8823. const TFileSystemInfo & FileSystemInfo = Terminal->GetFileSystemInfo(true);
  8824. TGetSpaceAvailable OnGetSpaceAvailable = NULL;
  8825. if (Terminal->IsCapable[fcCheckingSpaceAvailable])
  8826. {
  8827. OnGetSpaceAvailable = GetSpaceAvailable;
  8828. }
  8829. DoFileSystemInfoDialog(SessionInfo, FileSystemInfo, Terminal->CurrentDirectory,
  8830. OnGetSpaceAvailable);
  8831. }
  8832. //---------------------------------------------------------------------------
  8833. TSessionData * __fastcall TCustomScpExplorerForm::SessionDataForCode()
  8834. {
  8835. std::unique_ptr<TSessionData> Data(CloneCurrentSessionData());
  8836. Terminal->FillSessionDataForCode(Data.get());
  8837. return Data.release();
  8838. }
  8839. //---------------------------------------------------------------------------
  8840. void __fastcall TCustomScpExplorerForm::GenerateUrl(TStrings * Paths)
  8841. {
  8842. std::unique_ptr<TSessionData> Data(SessionDataForCode());
  8843. DoGenerateUrlDialog(Data.get(), Paths);
  8844. }
  8845. //---------------------------------------------------------------------------
  8846. void __fastcall TCustomScpExplorerForm::SessionGenerateUrl()
  8847. {
  8848. GenerateUrl(NULL);
  8849. }
  8850. //---------------------------------------------------------------------------
  8851. void __fastcall TCustomScpExplorerForm::FileGenerateUrl()
  8852. {
  8853. std::unique_ptr<TStrings> Paths(new TStringList());
  8854. DirView(osCurrent)->CreateFileList(false, true, Paths.get());
  8855. GenerateUrl(Paths.get());
  8856. }
  8857. //---------------------------------------------------------------------------
  8858. void __fastcall TCustomScpExplorerForm::UpdateSessionColor(TColor value)
  8859. {
  8860. FSessionColor = value;
  8861. TColor C = (value != 0 ? value : Vcl::Graphics::clNone);
  8862. TTBXColorItem * ColorItem = dynamic_cast<TTBXColorItem *>(
  8863. static_cast<TObject *>(GetComponent(fcColorMenu)));
  8864. DebugAssert(ColorItem != NULL);
  8865. ColorItem->Color = C;
  8866. NonVisualDataModule->ColorMenuItem->Color = C;
  8867. UpdateControls();
  8868. }
  8869. //---------------------------------------------------------------------------
  8870. void __fastcall TCustomScpExplorerForm::SetSessionColor(TColor value)
  8871. {
  8872. if (value != FSessionColor)
  8873. {
  8874. UpdateSessionColor(value);
  8875. // Is null when called from LastTerminalClosed
  8876. if (Terminal != NULL)
  8877. {
  8878. SessionsPageControl->ActivePage->ImageIndex = AddSessionColor(value);
  8879. }
  8880. }
  8881. }
  8882. //---------------------------------------------------------------------------
  8883. bool __fastcall TCustomScpExplorerForm::CancelNote(bool Force)
  8884. {
  8885. bool Result = FNoteTimer->Enabled;
  8886. if (Result)
  8887. {
  8888. // cannot cancel note too early
  8889. bool NotEarly =
  8890. (Now() - FNoteShown >
  8891. EncodeTimeVerbose(0, 0, (unsigned short)(WinConfiguration->NotificationsStickTime), 0));
  8892. if (Force || NotEarly)
  8893. {
  8894. FNoteTimer->Enabled = false;
  8895. FNote = L"";
  8896. SAFE_DESTROY(FNoteData);
  8897. FOnNoteClick = NULL;
  8898. FNoteHints = FNotes->Text;
  8899. FNoteHints.Delete(FNoteHints.Length() - 1, 2);
  8900. UpdateStatusBar();
  8901. }
  8902. }
  8903. return Result;
  8904. }
  8905. //---------------------------------------------------------------------------
  8906. void __fastcall TCustomScpExplorerForm::NoteTimer(TObject * /*Sender*/)
  8907. {
  8908. DebugAssert(FNoteTimer->Enabled);
  8909. CancelNote(true);
  8910. }
  8911. //---------------------------------------------------------------------------
  8912. void __fastcall TCustomScpExplorerForm::UpdateNoteHints()
  8913. {
  8914. FNoteHints = FNotes->Text.TrimRight();
  8915. }
  8916. //---------------------------------------------------------------------------
  8917. void __fastcall TCustomScpExplorerForm::AddNote(UnicodeString Note, bool UpdateNow)
  8918. {
  8919. int P = Note.Pos(L"\n");
  8920. if (P > 0)
  8921. {
  8922. Note.SetLength(P - 1);
  8923. }
  8924. FNotes->Add(FORMAT(L"[%s] %s",
  8925. (FormatDateTime(Configuration->TimeFormat, Now()), Note)));
  8926. while (FNotes->Count > 10)
  8927. {
  8928. FNotes->Delete(0);
  8929. }
  8930. if (UpdateNow)
  8931. {
  8932. UpdateNoteHints();
  8933. UpdateStatusBar();
  8934. }
  8935. }
  8936. //---------------------------------------------------------------------------
  8937. void __fastcall TCustomScpExplorerForm::PostNote(UnicodeString Note,
  8938. unsigned int Seconds, TNotifyEvent OnNoteClick, TObject * NoteData)
  8939. {
  8940. int P = Note.Pos(L"\n");
  8941. if (P > 0)
  8942. {
  8943. Note.SetLength(P - 1);
  8944. }
  8945. UpdateNoteHints();
  8946. FNote = Note;
  8947. SAFE_DESTROY(FNoteData);
  8948. FNoteData = NoteData;
  8949. FOnNoteClick = OnNoteClick;
  8950. AddNote(Note, false);
  8951. UpdateStatusBar();
  8952. FNoteShown = Now();
  8953. FNoteTimer->Enabled = false;
  8954. if (Seconds == 0)
  8955. {
  8956. Seconds = WinConfiguration->NotificationsTimeout;
  8957. }
  8958. FNoteTimer->Interval = Seconds * MSecsPerSec;
  8959. FNoteTimer->Enabled = true;
  8960. }
  8961. //---------------------------------------------------------------------------
  8962. void __fastcall TCustomScpExplorerForm::ReadDirectoryCancelled()
  8963. {
  8964. PostNote(LoadStr(DIRECTORY_READING_CANCELLED), 0, NULL, NULL);
  8965. }
  8966. //---------------------------------------------------------------------------
  8967. void __fastcall TCustomScpExplorerForm::SynchronizeBrowsingChanged()
  8968. {
  8969. if (NonVisualDataModule->SynchronizeBrowsingAction->Checked)
  8970. {
  8971. Configuration->Usage->Inc(L"SynchronizeBrowsingEnabled");
  8972. }
  8973. PostNote(FORMAT(LoadStrPart(SYNC_DIR_BROWSE_TOGGLE, 1),
  8974. (LoadStrPart(SYNC_DIR_BROWSE_TOGGLE,
  8975. (NonVisualDataModule->SynchronizeBrowsingAction->Checked ? 2 : 3)))),
  8976. 0, NULL, NULL);
  8977. }
  8978. //---------------------------------------------------------------------------
  8979. void __fastcall TCustomScpExplorerForm::ToggleShowHiddenFiles()
  8980. {
  8981. PostNote(FORMAT(LoadStrPart(SHOW_HIDDEN_FILES_TOGGLE, 1),
  8982. (LoadStrPart(SHOW_HIDDEN_FILES_TOGGLE,
  8983. (WinConfiguration->ShowHiddenFiles ? 2 : 3)))), 0, NULL, NULL);
  8984. GetComponent(fcStatusBar)->Repaint(); // toggling ShowHiddenFiles takes time, force repaint beforehand
  8985. WinConfiguration->ShowHiddenFiles = !WinConfiguration->ShowHiddenFiles;
  8986. }
  8987. //---------------------------------------------------------------------------
  8988. void __fastcall TCustomScpExplorerForm::SetFormatSizeBytes(TFormatBytesStyle Style)
  8989. {
  8990. WinConfiguration->FormatSizeBytes = Style;
  8991. }
  8992. //---------------------------------------------------------------------------
  8993. void __fastcall TCustomScpExplorerForm::ToggleAutoReadDirectoryAfterOp()
  8994. {
  8995. Configuration->AutoReadDirectoryAfterOp = !Configuration->AutoReadDirectoryAfterOp;
  8996. PostNote(FORMAT(LoadStrPart(AUTO_READ_DIRECTORY_TOGGLE, 1),
  8997. (LoadStrPart(AUTO_READ_DIRECTORY_TOGGLE,
  8998. (Configuration->AutoReadDirectoryAfterOp ? 2 : 3)))), 0, NULL, NULL);
  8999. }
  9000. //---------------------------------------------------------------------------
  9001. void __fastcall TCustomScpExplorerForm::StatusBarPanelDblClick(
  9002. TTBXCustomStatusBar * Sender, TTBXStatusPanel * Panel)
  9003. {
  9004. if (Panel->Index == 0)
  9005. {
  9006. if (FOnNoteClick != NULL)
  9007. {
  9008. // prevent the user data from being freed by possible call
  9009. // to CancelNote or PostNote during call to OnNoteClick
  9010. std::unique_ptr<TObject> NoteData(FNoteData);
  9011. TNotifyEvent OnNoteClick = FOnNoteClick;
  9012. FNoteData = NULL;
  9013. // need to cancel the note as we are going to delete its user data
  9014. CancelNote(true);
  9015. OnNoteClick(NoteData.get());
  9016. }
  9017. }
  9018. if (Panel->Index >= Sender->Panels->Count - SessionPanelCount)
  9019. {
  9020. FileSystemInfo();
  9021. }
  9022. }
  9023. //---------------------------------------------------------------------------
  9024. void __fastcall TCustomScpExplorerForm::LockWindow(bool Force)
  9025. {
  9026. // workaround:
  9027. // 1) for unknown reason, disabling window, while minimized,
  9028. // prevents it from restoring, even if it was enabled again meanwhile
  9029. // 2) when disabling the main window, while another has focus
  9030. // minimize is no longer possible
  9031. // ("keep up to date" dialog - though does not seem to be the case with "find file" -
  9032. // so it has probably something to do with the semi-modalness of the "keep up to date" dialog)
  9033. // Shouldn't we use IsApplicationMinimized() here?
  9034. if ((FLockSuspendLevel == 0) && !IsIconic(Application->Handle) && (Force || (Screen->ActiveForm == this)))
  9035. {
  9036. Enabled = false;
  9037. }
  9038. FLockLevel++;
  9039. }
  9040. //---------------------------------------------------------------------------
  9041. void __fastcall TCustomScpExplorerForm::UnlockWindow()
  9042. {
  9043. DebugAssert(FLockLevel > 0);
  9044. FLockLevel--;
  9045. if (FLockLevel == 0)
  9046. {
  9047. DebugAssert(FLockSuspendLevel == 0);
  9048. Enabled = true;
  9049. }
  9050. }
  9051. //---------------------------------------------------------------------------
  9052. void __fastcall TCustomScpExplorerForm::SuspendWindowLock()
  9053. {
  9054. // We need to make sure that window is enabled when the last modal window closes
  9055. // otherwise focus is not restored correctly.
  9056. // So we re-enable the window when modal window opens and
  9057. // disable it back after is closes
  9058. if (FLockLevel > 0)
  9059. {
  9060. // while we have nesting counter, we know that we never be called
  9061. // recursivelly as Application->OnModalBegin is called only
  9062. // for the top-level modal window
  9063. if (DebugAlwaysTrue(FLockSuspendLevel == 0))
  9064. {
  9065. // won't be disabled when conditions in LockWindow() were not satisfied
  9066. FDisabledOnLockSuspend = !Enabled;
  9067. // When minimized to tray (or actually when set to SW_HIDE),
  9068. // setting Enabled makes the window focusable even when there's
  9069. // modal window over it
  9070. if (!FTrayIcon->Visible)
  9071. {
  9072. Enabled = true;
  9073. }
  9074. }
  9075. FLockSuspendLevel++;
  9076. }
  9077. }
  9078. //---------------------------------------------------------------------------
  9079. void __fastcall TCustomScpExplorerForm::ResumeWindowLock()
  9080. {
  9081. if (FLockSuspendLevel > 0)
  9082. {
  9083. DebugAssert(FLockLevel > 0);
  9084. FLockSuspendLevel--;
  9085. // see comment in SuspendWindowLock
  9086. if (DebugAlwaysTrue(FLockSuspendLevel == 0))
  9087. {
  9088. // Note that window can be enabled here, when we were minized to tray when
  9089. // was SuspendWindowLock() called.
  9090. // We should possibly do the same check as in LockWindow(),
  9091. // if it is ever possible that the consitions change between
  9092. // SuspendWindowLock() and ResumeWindowLock()
  9093. Enabled = !FDisabledOnLockSuspend;
  9094. }
  9095. }
  9096. }
  9097. //---------------------------------------------------------------------------
  9098. void __fastcall TCustomScpExplorerForm::UpdateRemotePathComboBox(bool TextOnly)
  9099. {
  9100. TTBXComboBoxItem * RemotePathComboBox =
  9101. reinterpret_cast<TTBXComboBoxItem *>(GetComponent(fcRemotePathComboBox));
  9102. if (!TextOnly)
  9103. {
  9104. TStrings * Items = RemotePathComboBox->Strings;
  9105. Items->BeginUpdate();
  9106. try
  9107. {
  9108. Items->Clear();
  9109. if ((Terminal != NULL) && !RemoteDirView->Path.IsEmpty())
  9110. {
  9111. UnicodeString APath = UnixExcludeTrailingBackslash(RemoteDirView->Path);
  9112. while (!IsUnixRootPath(APath))
  9113. {
  9114. int P = APath.LastDelimiter(L'/');
  9115. DebugAssert(P >= 0);
  9116. Items->Insert(0, APath.SubString(P + 1, APath.Length() - P));
  9117. APath.SetLength(P - 1);
  9118. }
  9119. Items->Insert(0, Customunixdirview_SUnixDefaultRootName);
  9120. }
  9121. }
  9122. __finally
  9123. {
  9124. Items->EndUpdate();
  9125. }
  9126. }
  9127. RemotePathComboBox->ItemIndex = RemotePathComboBox->Strings->Count - 1;
  9128. // Setting ItemIndex to -1 does not reset its text
  9129. if (RemotePathComboBox->Strings->Count == 0)
  9130. {
  9131. RemotePathComboBox->Text = L"";
  9132. }
  9133. }
  9134. //---------------------------------------------------------------------------
  9135. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxAdjustImageIndex(
  9136. TTBXComboBoxItem * Sender, const UnicodeString /*AText*/, int AIndex,
  9137. int & ImageIndex)
  9138. {
  9139. if (AIndex < 0)
  9140. {
  9141. AIndex = Sender->ItemIndex;
  9142. }
  9143. ImageIndex = (AIndex < Sender->Strings->Count - 1 ? StdDirIcon : StdDirSelIcon);
  9144. }
  9145. //---------------------------------------------------------------------------
  9146. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxDrawItem(
  9147. TTBXCustomList * /*Sender*/, TCanvas * /*ACanvas*/, TRect & ARect, int AIndex,
  9148. int /*AHoverIndex*/, bool & /*DrawDefault*/)
  9149. {
  9150. ARect.Left += (10 * AIndex);
  9151. }
  9152. //---------------------------------------------------------------------------
  9153. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxMeasureWidth(
  9154. TTBXCustomList * /*Sender*/, TCanvas * /*ACanvas*/, int AIndex, int &AWidth)
  9155. {
  9156. AWidth += (10 * AIndex);
  9157. }
  9158. //---------------------------------------------------------------------------
  9159. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxItemClick(
  9160. TObject * Sender)
  9161. {
  9162. TTBXComboBoxItem * RemotePathComboBox = dynamic_cast<TTBXComboBoxItem*>(Sender);
  9163. UnicodeString APath = UnixExcludeTrailingBackslash(RemoteDirView->Path);
  9164. int Index = RemotePathComboBox->ItemIndex;
  9165. while (Index < RemotePathComboBox->Strings->Count - 1)
  9166. {
  9167. APath = UnixExtractFileDir(APath);
  9168. Index++;
  9169. }
  9170. // VanDyke style paths
  9171. if (APath.IsEmpty())
  9172. {
  9173. DebugAssert(RemotePathComboBox->ItemIndex == 0);
  9174. APath = ROOTDIRECTORY;
  9175. }
  9176. if (RemoteDirView->Path != APath)
  9177. {
  9178. RemoteDirView->Path = APath;
  9179. }
  9180. }
  9181. //---------------------------------------------------------------------------
  9182. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxCancel(TObject * Sender)
  9183. {
  9184. DebugAssert(Sender == GetComponent(fcRemotePathComboBox));
  9185. DebugUsedParam(Sender);
  9186. UpdateRemotePathComboBox(true);
  9187. }
  9188. //---------------------------------------------------------------------------
  9189. void __fastcall TCustomScpExplorerForm::DirViewEditing(
  9190. TObject * Sender, TListItem * Item, bool & /*AllowEdit*/)
  9191. {
  9192. ResetIncrementalSearch();
  9193. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  9194. DebugAssert(DirView != NULL);
  9195. if (!WinConfiguration->RenameWholeName && !DirView->ItemIsDirectory(Item))
  9196. {
  9197. HWND Edit = ListView_GetEditControl(DirView->Handle);
  9198. // OnEditing is called also from TCustomListView::CanEdit
  9199. if (Edit != NULL)
  9200. {
  9201. EditSelectBaseName(Edit);
  9202. }
  9203. }
  9204. }
  9205. //---------------------------------------------------------------------------
  9206. //---------------------------------------------------------------------------
  9207. TDragDropFilesEx * __fastcall TCustomScpExplorerForm::CreateDragDropFilesEx()
  9208. {
  9209. TDragDropFilesEx * Result = new TDragDropFilesEx(this);
  9210. Result->AutoDetectDnD = false;
  9211. Result->NeedValid = TFileExMustDnDSet() << nvFilename;
  9212. Result->RenderDataOn = rdoEnterAndDropSync;
  9213. Result->TargetEffects = TDropEffectSet() << deCopy << deMove;
  9214. return Result;
  9215. }
  9216. //---------------------------------------------------------------------------
  9217. void __fastcall TCustomScpExplorerForm::UpdateDarkMode()
  9218. {
  9219. if (IsWin10Build(2004))
  9220. {
  9221. FImmersiveDarkMode = WinConfiguration->UseDarkTheme();
  9222. BOOL DarkMode = FImmersiveDarkMode ? TRUE : FALSE;
  9223. const int DWMWA_USE_IMMERSIVE_DARK_MODE = 20;
  9224. DwmSetWindowAttribute(Handle, DWMWA_USE_IMMERSIVE_DARK_MODE, &DarkMode, sizeof(DarkMode));
  9225. }
  9226. }
  9227. //---------------------------------------------------------------------------
  9228. void __fastcall TCustomScpExplorerForm::CreateWnd()
  9229. {
  9230. TForm::CreateWnd();
  9231. // win32-darkmode calls AllowDarkModeForWindow(this, true) here, but it does not seem to have any effect
  9232. UpdateDarkMode();
  9233. if (FSessionsDragDropFilesEx == NULL)
  9234. {
  9235. FSessionsDragDropFilesEx = CreateDragDropFilesEx();
  9236. FSessionsDragDropFilesEx->OnDragOver = SessionsDDDragOver;
  9237. FSessionsDragDropFilesEx->OnProcessDropped = SessionsDDProcessDropped;
  9238. FSessionsDragDropFilesEx->OnDragEnter = SessionsDDDragEnter;
  9239. FSessionsDragDropFilesEx->OnDragLeave = SessionsDDDragLeave;
  9240. }
  9241. if (FQueueDragDropFilesEx == NULL)
  9242. {
  9243. FQueueDragDropFilesEx = CreateDragDropFilesEx();
  9244. // No need to set OnDragOver as we do not have any restrictions
  9245. FQueueDragDropFilesEx->OnProcessDropped = QueueDDProcessDropped;
  9246. FQueueDragDropFilesEx->OnDragEnter = QueueDDDragEnter;
  9247. FQueueDragDropFilesEx->OnDragLeave = QueueDDDragLeave;
  9248. }
  9249. }
  9250. //---------------------------------------------------------------------------
  9251. void __fastcall TCustomScpExplorerForm::DestroyWnd()
  9252. {
  9253. TForm::DestroyWnd();
  9254. FSessionsDragDropFilesEx->DragDropControl = NULL;
  9255. FQueueDragDropFilesEx->DragDropControl = NULL;
  9256. }
  9257. //---------------------------------------------------------------------------
  9258. void __fastcall TCustomScpExplorerForm::FormShow(TObject * /*Sender*/)
  9259. {
  9260. SideEnter(FCurrentSide);
  9261. FEverShown = true;
  9262. }
  9263. //---------------------------------------------------------------------------
  9264. void __fastcall TCustomScpExplorerForm::DoFindFiles(
  9265. TTerminal * ATerminal, UnicodeString Directory, const TFileMasks & FileMask,
  9266. TFileFoundEvent OnFileFound, TFindingFileEvent OnFindingFile)
  9267. {
  9268. if (!NonVisualDataModule->Busy)
  9269. {
  9270. TTerminalManager::Instance()->ActiveTerminal = DebugNotNull(dynamic_cast<TManagedTerminal *>(ATerminal));
  9271. Configuration->Usage->Inc(L"FileFinds");
  9272. LockWindow(true);
  9273. NonVisualDataModule->StartBusy();
  9274. WinConfiguration->LockedInterface = true;
  9275. try
  9276. {
  9277. FTerminal->FilesFind(Directory, FileMask, OnFileFound, OnFindingFile);
  9278. }
  9279. __finally
  9280. {
  9281. WinConfiguration->LockedInterface = false;
  9282. NonVisualDataModule->EndBusy();
  9283. UnlockWindow();
  9284. }
  9285. }
  9286. }
  9287. //---------------------------------------------------------------------------
  9288. void __fastcall TCustomScpExplorerForm::DoFocusRemotePath(TTerminal * ATerminal, const UnicodeString & Path)
  9289. {
  9290. if (!NonVisualDataModule->Busy)
  9291. {
  9292. TTerminalManager::Instance()->ActiveTerminal = DebugNotNull(dynamic_cast<TManagedTerminal *>(ATerminal));
  9293. SetFocus();
  9294. RemoteDirView->Path = UnixExtractFilePath(Path);
  9295. UnicodeString FileName = UnixExtractFileName(Path);
  9296. TListItem * Item = RemoteDirView->FindFileItem(FileName);
  9297. // If not found, it can because we have loaded directory from cache and the file is new, try again after reload.
  9298. // (tbd: we should not even try this, if we have not loaded the directory from the cache)
  9299. if (Item == NULL)
  9300. {
  9301. ReloadDirectory(osRemote);
  9302. Item = RemoteDirView->FindFileItem(FileName);
  9303. }
  9304. if (Item != NULL)
  9305. {
  9306. RemoteDirView->ItemFocused = Item;
  9307. RemoteDirView->ItemFocused->MakeVisible(false);
  9308. RemoteDirView->SetFocus();
  9309. }
  9310. }
  9311. }
  9312. //---------------------------------------------------------------------------
  9313. bool __fastcall TCustomScpExplorerForm::CanOperateOnFoundFiles(TTerminal * ATerminal)
  9314. {
  9315. bool Result = !NonVisualDataModule->Busy;
  9316. if (Result)
  9317. {
  9318. TTerminalManager::Instance()->ActiveTerminal = DebugNotNull(dynamic_cast<TManagedTerminal *>(ATerminal));
  9319. }
  9320. return Result;
  9321. }
  9322. //---------------------------------------------------------------------------
  9323. void __fastcall TCustomScpExplorerForm::DoOperationOnFoundFiles(
  9324. TFileOperation Operation, TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  9325. {
  9326. if (CanOperateOnFoundFiles(ATerminal))
  9327. {
  9328. TValueRestorer<TFileOperationFinishedEvent> OnFileOperationFinishedRestorer(FOnFileOperationFinished);
  9329. FOnFileOperationFinished = OnFileOperationFinished;
  9330. ExecuteFileOperation(Operation, osRemote, FileList, false, NULL);
  9331. }
  9332. }
  9333. //---------------------------------------------------------------------------
  9334. void __fastcall TCustomScpExplorerForm::DoDeleteFoundFiles(
  9335. TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  9336. {
  9337. DoOperationOnFoundFiles(foDelete, ATerminal, FileList, OnFileOperationFinished);
  9338. }
  9339. //---------------------------------------------------------------------------
  9340. void __fastcall TCustomScpExplorerForm::DoDownloadFoundFiles(
  9341. TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  9342. {
  9343. DoOperationOnFoundFiles(foCopy, ATerminal, FileList, OnFileOperationFinished);
  9344. }
  9345. //---------------------------------------------------------------------------
  9346. void __fastcall TCustomScpExplorerForm::DoEditFoundFiles(
  9347. TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  9348. {
  9349. if (CanOperateOnFoundFiles(ATerminal))
  9350. {
  9351. for (int Index = 0; Index < FileList->Count; Index++)
  9352. {
  9353. UnicodeString FileName = FileList->Strings[Index];
  9354. TRemoteFile * File = static_cast<TRemoteFile *>(FileList->Objects[Index]);
  9355. ExecuteRemoteFile(FileName, File, efDefaultEditor);
  9356. OnFileOperationFinished(FileName, true);
  9357. }
  9358. }
  9359. }
  9360. //---------------------------------------------------------------------------
  9361. void __fastcall TCustomScpExplorerForm::RemoteFindFiles()
  9362. {
  9363. FFileFindTerminal = Terminal;
  9364. ShowFileFindDialog(
  9365. Terminal, RemoteDirView->Path, DoFindFiles, DoFocusRemotePath, DoDeleteFoundFiles,
  9366. DoDownloadFoundFiles, DoEditFoundFiles);
  9367. }
  9368. //---------------------------------------------------------------------------
  9369. void __fastcall TCustomScpExplorerForm::UpdateTaskbarList(ITaskbarList3 * TaskbarList)
  9370. {
  9371. FTaskbarList = TaskbarList;
  9372. }
  9373. //---------------------------------------------------------------------------
  9374. void __fastcall TCustomScpExplorerForm::SessionsPageControlMouseDown(
  9375. TObject * /*Sender*/, TMouseButton Button, TShiftState /*Shift*/, int X, int Y)
  9376. {
  9377. int Index = SessionsPageControl->IndexOfTabAt(X, Y);
  9378. if (Index >= 0)
  9379. {
  9380. if (Button == mbLeft)
  9381. {
  9382. // WORKAROUND: "Mouse down" is raised only after tab is switched.
  9383. // If switching tab (switching session) takes long enough for user
  9384. // to actually release the button, "mouse down" is still raised,
  9385. // but we do not get "mouse up" event, so dragging is not cancelled,
  9386. // prevent that by not beginning dragging in the first place.
  9387. if (IsKeyPressed(VK_LBUTTON))
  9388. {
  9389. // when user clicks the "+", we get mouse down only after the session
  9390. // is closed, when new session tab is already on X:Y, so dragging
  9391. // starts, prevent that
  9392. if (MilliSecondsBetween(Now(), FSessionsPageControlNewSessionTime) > 500)
  9393. {
  9394. TTerminal * Terminal = GetSessionTabTerminal(SessionsPageControl->Pages[Index]);
  9395. if (Terminal != NULL)
  9396. {
  9397. SessionsPageControl->BeginDrag(false);
  9398. }
  9399. }
  9400. }
  9401. }
  9402. else if (Button == mbMiddle)
  9403. {
  9404. // ignore middle-click for "New session tab"
  9405. TTerminal * Terminal = GetSessionTabTerminal(SessionsPageControl->Pages[Index]);
  9406. if (Terminal != NULL)
  9407. {
  9408. CloseSessionTab(Index);
  9409. }
  9410. }
  9411. }
  9412. }
  9413. //---------------------------------------------------------------------------
  9414. void __fastcall TCustomScpExplorerForm::SessionsPageControlDragDrop(
  9415. TObject * /*Sender*/, TObject * /*Source*/, int X, int Y)
  9416. {
  9417. int Index = SessionsPageControl->IndexOfTabAt(X, Y);
  9418. // do not allow dropping on the "+" tab
  9419. TTerminal * TargetTerminal = GetSessionTabTerminal(SessionsPageControl->Pages[Index]);
  9420. if ((TargetTerminal != NULL) &&
  9421. (SessionsPageControl->ActivePage->PageIndex != Index))
  9422. {
  9423. Configuration->Usage->Inc(L"SessionTabMoves");
  9424. // this is almost redundant as we would recreate tabs in DoTerminalListChanged,
  9425. // but we want to actually prevent that to avoid flicker
  9426. SessionsPageControl->ActivePage->PageIndex = Index;
  9427. TTerminal * Terminal = GetSessionTabTerminal(SessionsPageControl->ActivePage);
  9428. TTerminalManager::Instance()->Move(Terminal, TargetTerminal);
  9429. }
  9430. }
  9431. //---------------------------------------------------------------------------
  9432. void __fastcall TCustomScpExplorerForm::SessionsPageControlDragOver(
  9433. TObject * Sender, TObject * Source, int X, int Y,
  9434. TDragState /*State*/, bool & Accept)
  9435. {
  9436. Accept = (Sender == Source);
  9437. if (Accept)
  9438. {
  9439. int Index = SessionsPageControl->IndexOfTabAt(X, Y);
  9440. TTerminal * Terminal = GetSessionTabTerminal(SessionsPageControl->Pages[Index]);
  9441. // do not allow dragging to the "+" tab
  9442. Accept = (Terminal != NULL);
  9443. }
  9444. }
  9445. //---------------------------------------------------------------------------
  9446. void __fastcall TCustomScpExplorerForm::SessionsDDDragOver(
  9447. int DebugUsedArg(KeyState), const TPoint & Point, int & Effect, int DebugUsedArg(PreferredEffect))
  9448. {
  9449. int Index = SessionsPageControl->IndexOfTabAt(Point.X, Point.Y);
  9450. if (Index < 0)
  9451. {
  9452. Effect = DROPEFFECT_NONE;
  9453. }
  9454. else
  9455. {
  9456. TTerminal * TargetTerminal = GetSessionTabTerminal(SessionsPageControl->Pages[Index]);
  9457. // do not allow dropping on the "+" and disconnected tabs
  9458. if (!IsActiveTerminal(TargetTerminal))
  9459. {
  9460. Effect = DROPEFFECT_NONE;
  9461. }
  9462. else if ((TargetTerminal != Terminal) && (Effect == DROPEFFECT_MOVE))
  9463. {
  9464. Effect = DROPEFFECT_COPY;
  9465. }
  9466. }
  9467. }
  9468. //---------------------------------------------------------------------------
  9469. void __fastcall TCustomScpExplorerForm::SessionsDDProcessDropped(
  9470. TObject * /*Sender*/, int /*KeyState*/, const TPoint & Point, int Effect)
  9471. {
  9472. int Index = SessionsPageControl->IndexOfTabAt(Point.X, Point.Y);
  9473. // do not allow dropping on the "+" and disconnected tabs
  9474. TManagedTerminal * TargetTerminal = GetSessionTabTerminal(SessionsPageControl->Pages[Index]);
  9475. if (IsActiveTerminal(TargetTerminal))
  9476. {
  9477. DebugAssert(!IsFileControl(DropSourceControl, osRemote));
  9478. if (!IsFileControl(DropSourceControl, osRemote))
  9479. {
  9480. TTerminalManager::Instance()->ActiveTerminal = TargetTerminal;
  9481. RemoteFileControlDragDropFileOperation(SessionsPageControl, Effect,
  9482. // Why don't we use Terminal->CurrentDirectory directly?
  9483. TTerminalManager::Instance()->ActiveTerminal->CurrentDirectory,
  9484. // do not force queue, drag drop
  9485. false, true);
  9486. }
  9487. }
  9488. }
  9489. //---------------------------------------------------------------------------
  9490. void __fastcall TCustomScpExplorerForm::QueueDDProcessDropped(
  9491. TObject * /*Sender*/, int /*KeyState*/, const TPoint & /*Point*/, int Effect)
  9492. {
  9493. // Downloads are handled in RemoteFileControlDDEnd
  9494. if (!IsFileControl(DropSourceControl, osRemote))
  9495. {
  9496. RemoteFileControlDragDropFileOperation(QueueView3, Effect,
  9497. Terminal->CurrentDirectory,
  9498. // force queue, drag drop
  9499. true, true);
  9500. }
  9501. }
  9502. //---------------------------------------------------------------------------
  9503. void __fastcall TCustomScpExplorerForm::FormClose(TObject * /*Sender*/, TCloseAction & /*Action*/)
  9504. {
  9505. FShowing = false;
  9506. // Do not save empty workspace
  9507. if (WinConfiguration->AutoSaveWorkspace && (Terminal != NULL))
  9508. {
  9509. std::unique_ptr<TObjectList> DataList(DoCollectWorkspace());
  9510. UnicodeString Name = WorkspaceName();
  9511. DoSaveWorkspace(
  9512. Name, DataList.get(),
  9513. !Configuration->DisablePasswordStoring &&
  9514. WinConfiguration->AutoSaveWorkspacePasswords, false);
  9515. WinConfiguration->LastStoredSession = Name;
  9516. }
  9517. }
  9518. //---------------------------------------------------------------------------
  9519. void __fastcall TCustomScpExplorerForm::RemoteDirViewRead(TObject * /*Sender*/)
  9520. {
  9521. TManagedTerminal * ManagedTerminal =
  9522. dynamic_cast<TManagedTerminal *>(RemoteDirView->Terminal);
  9523. if (ManagedTerminal != NULL)
  9524. {
  9525. ManagedTerminal->DirectoryLoaded = Now();
  9526. }
  9527. }
  9528. //---------------------------------------------------------------------------
  9529. void __fastcall TCustomScpExplorerForm::DirViewSelectItem(TObject * Sender,
  9530. TListItem * /*Item*/, bool /*Selected*/)
  9531. {
  9532. TCustomDirView * DirView = reinterpret_cast<TCustomDirView *>(Sender);
  9533. switch (DirView->LastSelectMethod)
  9534. {
  9535. case smKeyboard:
  9536. Configuration->Usage->Inc(L"KeyboardSelections");
  9537. break;
  9538. case smMouse:
  9539. Configuration->Usage->Inc(L"MouseSelections");
  9540. break;
  9541. }
  9542. }
  9543. //---------------------------------------------------------------------------
  9544. int __fastcall TCustomScpExplorerForm::AddSessionColor(TColor Color)
  9545. {
  9546. if (Color != 0)
  9547. {
  9548. return GetSessionColorImage(FSessionColors, Color, FSessionColorMaskImageIndex);
  9549. }
  9550. else
  9551. {
  9552. return FSessionTabImageIndex;
  9553. }
  9554. }
  9555. //---------------------------------------------------------------------------
  9556. int __fastcall TCustomScpExplorerForm::AddFixedSessionImage(int GlyphsSourceIndex)
  9557. {
  9558. TPngImageCollectionItem * Item =
  9559. GlyphsModule->ExplorerImages->PngImages->Items[GlyphsSourceIndex];
  9560. FSessionColors->AddPng(Item->PngImage);
  9561. return FSessionColors->Count - 1;
  9562. }
  9563. //---------------------------------------------------------------------------
  9564. void __fastcall TCustomScpExplorerForm::AddFixedSessionImages()
  9565. {
  9566. FSessionColors->SetSize(GlyphsModule->ExplorerImages->Width, GlyphsModule->ExplorerImages->Height);
  9567. FNewSessionTabImageIndex = AddFixedSessionImage(NonVisualDataModule->NewSessionAction->ImageIndex);
  9568. FSessionTabImageIndex = AddFixedSessionImage(SiteImageIndex);
  9569. FSessionColorMaskImageIndex = AddFixedSessionImage(SiteColorMaskImageIndex);
  9570. }
  9571. //---------------------------------------------------------------------------
  9572. void __fastcall TCustomScpExplorerForm::CollectItemsWithTextDisplayMode(TWinControl * Control)
  9573. {
  9574. for (int Index = 0; Index < Control->ControlCount; Index++)
  9575. {
  9576. TControl * AControl = Control->Controls[Index];
  9577. TWinControl * WinControl = dynamic_cast<TWinControl *>(AControl);
  9578. if (WinControl != NULL)
  9579. {
  9580. CollectItemsWithTextDisplayMode(WinControl);
  9581. }
  9582. TTBCustomToolbar * Toolbar = dynamic_cast<TTBCustomToolbar *>(AControl);
  9583. if (Toolbar != NULL)
  9584. {
  9585. // we care for top-level items only
  9586. TTBCustomItem * Items = Toolbar->Items;
  9587. for (int ItemIndex = 0; ItemIndex < Items->Count; ItemIndex++)
  9588. {
  9589. TTBCustomItem * Item = Items->Items[ItemIndex];
  9590. if (((Item->DisplayMode == nbdmImageAndText) ||
  9591. (dynamic_cast<TTBXLabelItem *>(Item) != NULL)) &&
  9592. EligibleForImageDisplayMode(Item))
  9593. {
  9594. FItemsWithTextDisplayMode.insert(Item);
  9595. }
  9596. }
  9597. }
  9598. }
  9599. }
  9600. //---------------------------------------------------------------------------
  9601. bool __fastcall TCustomScpExplorerForm::EligibleForImageDisplayMode(TTBCustomItem * /*Item*/)
  9602. {
  9603. return true;
  9604. }
  9605. //---------------------------------------------------------------------------
  9606. bool __fastcall TCustomScpExplorerForm::UpdateToolbarDisplayMode()
  9607. {
  9608. bool SelectiveToolbarText = WinConfiguration->SelectiveToolbarText;
  9609. TTBItemDisplayMode DisplayMode = (SelectiveToolbarText ? nbdmImageAndText : nbdmDefault);
  9610. typedef std::set<TTBCustomItem *> TItemsWithTextDisplayMode;
  9611. TItemsWithTextDisplayMode::iterator i = FItemsWithTextDisplayMode.begin();
  9612. bool Result = true;
  9613. while (Result && (i != FItemsWithTextDisplayMode.end()))
  9614. {
  9615. TTBCustomItem * Item = *i;
  9616. TTBXLabelItem * Label = dynamic_cast<TTBXLabelItem *>(Item);
  9617. if (Label != NULL)
  9618. {
  9619. // optimization
  9620. if (Label->Visible == SelectiveToolbarText)
  9621. {
  9622. Result = false;
  9623. }
  9624. else
  9625. {
  9626. Label->Visible = SelectiveToolbarText;
  9627. }
  9628. }
  9629. else
  9630. {
  9631. // optimization
  9632. if (Item->DisplayMode == DisplayMode)
  9633. {
  9634. Result = false;
  9635. }
  9636. else
  9637. {
  9638. Item->DisplayMode = DisplayMode;
  9639. }
  9640. }
  9641. ++i;
  9642. }
  9643. if (Result)
  9644. {
  9645. UpdateNewSessionTab();
  9646. }
  9647. return Result;
  9648. }
  9649. //---------------------------------------------------------------------------
  9650. void __fastcall TCustomScpExplorerForm::DisplaySystemContextMenu()
  9651. {
  9652. DebugFail();
  9653. }
  9654. //---------------------------------------------------------------------------
  9655. bool __fastcall TCustomScpExplorerForm::IsBusy()
  9656. {
  9657. // Among other, a lock level is non zero, while directory is being loaded,
  9658. // even when it happens as a result of dirview navigation,
  9659. // i.e. when TNonVisualDataModule is NOT FBusy.
  9660. // That's why the TNonVisualDataModule::GetBusy calls this method.
  9661. // Among other this prevents a panel auto update to occur while
  9662. // directory is changing.
  9663. return (FLockLevel > 0) || DirView(osCurrent)->IsEditing();
  9664. }
  9665. //---------------------------------------------------------------------------
  9666. Boolean __fastcall TCustomScpExplorerForm::AllowedAction(TAction * /*Action*/, TActionAllowed Allowed)
  9667. {
  9668. // While the window is disabled, we still seem to process menu shortcuts at least,
  9669. // so stop it at least here.
  9670. // See also TCustomScpExplorerForm::RemoteDirViewBusy
  9671. return
  9672. (Allowed == aaUpdate) ||
  9673. !NonVisualDataModule->Busy;
  9674. }
  9675. //---------------------------------------------------------------------------
  9676. void __fastcall TCustomScpExplorerForm::EditMenuItemPopup(TTBCustomItem * Sender, bool FromLink)
  9677. {
  9678. NonVisualDataModule->EditMenuItemPopup(Sender, FromLink);
  9679. }
  9680. //---------------------------------------------------------------------------
  9681. void __fastcall TCustomScpExplorerForm::DirViewBusy(TObject * /*Sender*/, int Busy, bool & State)
  9682. {
  9683. // This is somewhat redundant to LockWindow() call from
  9684. // TTerminalManager::TerminalReadDirectoryProgress.
  9685. // But disabling window is known not to block keyboard shorcuts
  9686. // (see TCustomScpExplorerForm::AllowedAction), this hopefully works.
  9687. if (Busy > 0)
  9688. {
  9689. if (NonVisualDataModule->Busy)
  9690. {
  9691. State = false;
  9692. }
  9693. else
  9694. {
  9695. NonVisualDataModule->StartBusy();
  9696. // Will be countered for TCustomDirView.DoExecute in DoDirViewExecFile
  9697. LockWindow();
  9698. }
  9699. }
  9700. else if (Busy < 0)
  9701. {
  9702. UnlockWindow();
  9703. NonVisualDataModule->EndBusy();
  9704. }
  9705. else
  9706. {
  9707. State = NonVisualDataModule->Busy;
  9708. }
  9709. }
  9710. //---------------------------------------------------------------------------
  9711. void __fastcall TCustomScpExplorerForm::SessionsPageControlContextPopup(TObject * /*Sender*/, TPoint & MousePos, bool & Handled)
  9712. {
  9713. int Index = SessionsPageControl->IndexOfTabAt(MousePos.X, MousePos.Y);
  9714. // no context menu for "New session tab"
  9715. if ((Index >= 0) && (GetSessionTabTerminal(SessionsPageControl->Pages[Index]) != NULL))
  9716. {
  9717. SessionsPageControl->ActivePageIndex = Index;
  9718. if (DebugAlwaysTrue(SessionTabSwitched()))
  9719. {
  9720. // copied from TControl.WMContextMenu
  9721. SendCancelMode(SessionsPageControl);
  9722. // explicit popup instead of using PopupMenu property
  9723. // to avoid menu to popup somewhere within SessionTabSwitched above,
  9724. // while connecting yet not-connected session and hence
  9725. // allowing an access to commands over not-completelly connected session
  9726. TPoint Point = SessionsPageControl->ClientToScreen(MousePos);
  9727. TPopupMenu * PopupMenu = NonVisualDataModule->SessionsPopup;
  9728. PopupMenu->PopupComponent = SessionsPageControl;
  9729. PopupMenu->Popup(Point.x, Point.y);
  9730. }
  9731. }
  9732. Handled = true;
  9733. }
  9734. //---------------------------------------------------------------------------
  9735. bool __fastcall TCustomScpExplorerForm::CanChangePassword()
  9736. {
  9737. return HasActiveTerminal() && Terminal->IsCapable[fcChangePassword];
  9738. }
  9739. //---------------------------------------------------------------------------
  9740. void __fastcall TCustomScpExplorerForm::ChangePassword()
  9741. {
  9742. Configuration->Usage->Inc(L"PasswordChanges");
  9743. std::unique_ptr<TSessionData> ChangePasswordData(Terminal->SessionData->Clone());
  9744. ChangePasswordData->DisableAuthentationsExceptPassword();
  9745. ChangePasswordData->ChangePassword = true;
  9746. std::unique_ptr<TTerminal> ChangePasswordSession(
  9747. Terminal->CreateSecondarySession(L"ChangePassword", ChangePasswordData.get()));
  9748. ChangePasswordSession->AutoReadDirectory = false;
  9749. if (TTerminalManager::Instance()->ConnectTerminal(ChangePasswordSession.get()))
  9750. {
  9751. MessageDialog(MainInstructions(LoadStr(PASSWORD_CHANGED)), qtInformation, qaOK, HELP_CHANGE_PASSWORD);
  9752. }
  9753. else
  9754. {
  9755. Configuration->Usage->Inc(L"PasswordChangesFailed");
  9756. }
  9757. }
  9758. //---------------------------------------------------------------------------
  9759. bool __fastcall TCustomScpExplorerForm::CanPrivateKeyUpload()
  9760. {
  9761. // No nice way to assert SSH2
  9762. return HasActiveTerminal() && (Terminal->FSProtocol == cfsSFTP);
  9763. }
  9764. //---------------------------------------------------------------------------
  9765. void __fastcall TCustomScpExplorerForm::PrivateKeyUpload()
  9766. {
  9767. TOperationVisualizer OperationVisualizer;
  9768. UnicodeString FileName = Terminal->SessionData->PublicKeyFile;
  9769. TTerminalManager::Instance()->UploadPublicKey(Terminal, NULL, FileName);
  9770. }
  9771. //---------------------------------------------------------------------------
  9772. void __fastcall TCustomScpExplorerForm::ChangeScale(int M, int D)
  9773. {
  9774. TForm::ChangeScale(M, D);
  9775. int APixelsPerInch = GetControlPixelsPerInch(this);
  9776. HANDLE ResourceModule = GUIConfiguration->ChangeToDefaultResourceModule();
  9777. try
  9778. {
  9779. GlyphsModule->SetPixelsPerInch(APixelsPerInch);
  9780. }
  9781. __finally
  9782. {
  9783. GUIConfiguration->ChangeResourceModule(ResourceModule);
  9784. }
  9785. }
  9786. //---------------------------------------------------------------------------
  9787. void __fastcall TCustomScpExplorerForm::DockContextPopup(TObject * Sender, TPoint & MousePos, bool & /*Handled*/)
  9788. {
  9789. NonVisualDataModule->ControlContextPopup(Sender, MousePos);
  9790. }
  9791. //---------------------------------------------------------------------
  9792. class TPublicControl : public TControl
  9793. {
  9794. friend class TCustomScpExplorerForm;
  9795. };
  9796. //---------------------------------------------------------------------------
  9797. void __fastcall TCustomScpExplorerForm::CopyPopup(TControl * DestControl, TControl * SourceControl)
  9798. {
  9799. static_cast<TPublicControl *>(DestControl)->PopupMenu = static_cast<TPublicControl *>(SourceControl)->PopupMenu;
  9800. static_cast<TPublicControl *>(DestControl)->OnContextPopup = static_cast<TPublicControl *>(SourceControl)->OnContextPopup;
  9801. }
  9802. //---------------------------------------------------------------------------
  9803. void __fastcall TCustomScpExplorerForm::DoBookmarkClick(TOperationSide Side, TObject * Sender)
  9804. {
  9805. TBookmark * Bookmark = DebugNotNull(reinterpret_cast<TBookmark *>(dynamic_cast<TTBCustomItem *>(Sender)->Tag));
  9806. OpenBookmark(Side, Bookmark);
  9807. }
  9808. //---------------------------------------------------------------------------
  9809. void __fastcall TCustomScpExplorerForm::LocalBookmarkClick(TObject * Sender)
  9810. {
  9811. DoBookmarkClick(osLocal, Sender);
  9812. }
  9813. //---------------------------------------------------------------------------
  9814. void __fastcall TCustomScpExplorerForm::RemoteBookmarkClick(TObject * Sender)
  9815. {
  9816. DoBookmarkClick(osRemote, Sender);
  9817. }
  9818. //---------------------------------------------------------------------------
  9819. void __fastcall TCustomScpExplorerForm::CreateOpenDirMenuList(
  9820. TTBCustomItem * Menu, TOperationSide Side, TBookmarkList * BookmarkList)
  9821. {
  9822. if (BookmarkList != NULL)
  9823. {
  9824. TNotifyEvent OnBookmarkClick = (Side == osLocal) ? &LocalBookmarkClick : &RemoteBookmarkClick;
  9825. if (!WinConfiguration->UseLocationProfiles)
  9826. {
  9827. std::unique_ptr<TStringList> Directories(new TStringList());
  9828. Directories->CaseSensitive = (Side == osRemote);
  9829. for (int Index = 0; Index < BookmarkList->Count; Index++)
  9830. {
  9831. TBookmark * Bookmark = BookmarkList->Bookmarks[Index];
  9832. UnicodeString Directory = Bookmark->GetSideDirectory(Side);
  9833. if (!Directory.IsEmpty() && (Directories->IndexOf(Directory) < 0))
  9834. {
  9835. std::unique_ptr<TTBCustomItem> Item(new TTBXItem(Owner));
  9836. Item->Caption = EscapeHotkey(Directory);
  9837. Item->ShortCut = Bookmark->ShortCut;
  9838. Item->OnClick = OnBookmarkClick;
  9839. Item->Tag = reinterpret_cast<int>(Bookmark);
  9840. Directories->Add(Directory);
  9841. Menu->Add(Item.release());
  9842. }
  9843. }
  9844. }
  9845. else
  9846. {
  9847. std::unique_ptr<TStrings> Folders(CreateSortedStringList());
  9848. for (int Index = 0; Index < BookmarkList->Count; Index++)
  9849. {
  9850. TBookmark * Bookmark = BookmarkList->Bookmarks[Index];
  9851. if (!Bookmark->Node.IsEmpty())
  9852. {
  9853. Folders->Add(Bookmark->Node);
  9854. }
  9855. }
  9856. for (int Index = 0; Index < Folders->Count; Index++)
  9857. {
  9858. std::unique_ptr<TTBCustomItem> Item(new TTBXSubmenuItem(Owner));
  9859. Item->Caption = Folders->Strings[Index];
  9860. Item->ImageIndex = NonVisualDataModule->RemoteChangePathAction->ImageIndex;
  9861. Folders->Objects[Index] = Item.get();
  9862. Menu->Add(Item.release());
  9863. }
  9864. for (int Index = 0; Index < BookmarkList->Count; Index++)
  9865. {
  9866. TBookmark * Bookmark = BookmarkList->Bookmarks[Index];
  9867. TTBCustomItem * Parent;
  9868. if (!Bookmark->Node.IsEmpty())
  9869. {
  9870. DebugAssert(Folders->IndexOf(Bookmark->Node) >= 0);
  9871. Parent = dynamic_cast<TTBCustomItem *>(Folders->Objects[Folders->IndexOf(Bookmark->Node)]);
  9872. }
  9873. else
  9874. {
  9875. Parent = Menu;
  9876. }
  9877. std::unique_ptr<TTBCustomItem> Item(new TTBXItem(Owner));
  9878. Item->Caption = Bookmark->Name;
  9879. Item->ShortCut = Bookmark->ShortCut;
  9880. Item->OnClick = OnBookmarkClick;
  9881. Item->Tag = reinterpret_cast<int>(Bookmark);
  9882. if (((Bookmark->Name == Bookmark->Local) || (Bookmark->Name == Bookmark->Remote)) &&
  9883. (Bookmark->Local.IsEmpty() || Bookmark->Remote.IsEmpty()))
  9884. {
  9885. // No hint for location profiles that are actually mere "bookmarks"
  9886. }
  9887. else
  9888. {
  9889. UnicodeString Hint = FORMAT(LoadStrPart(LOCATION_PROFILE_HINT, 1), (Bookmark->Name));
  9890. UnicodeString LongHint;
  9891. if (!Bookmark->Local.IsEmpty())
  9892. {
  9893. AddToList(LongHint, FORMAT(LoadStrPart(LOCATION_PROFILE_HINT, 2), (Bookmark->Local)), L"\n");
  9894. }
  9895. if (!Bookmark->Remote.IsEmpty())
  9896. {
  9897. AddToList(LongHint, FORMAT(LoadStrPart(LOCATION_PROFILE_HINT, 3), (Bookmark->Remote)), L"\n");
  9898. }
  9899. AddToList(Hint, LongHint, L"|");
  9900. Item->Hint = Hint;
  9901. }
  9902. Parent->Add(Item.release());
  9903. }
  9904. }
  9905. AddMenuSeparator(Menu);
  9906. }
  9907. }
  9908. //---------------------------------------------------------------------------
  9909. void __fastcall TCustomScpExplorerForm::CreateOpenDirMenu(TTBCustomItem * Menu, TOperationSide Side)
  9910. {
  9911. Menu->Clear();
  9912. std::unique_ptr<TTBCustomItem> Item;
  9913. Item.reset(new TTBXItem(Owner));
  9914. Item->Action = (Side == osLocal) ? NonVisualDataModule->LocalOpenDirAction : NonVisualDataModule->RemoteOpenDirAction;
  9915. Item->Options = Item->Options << tboDefault;
  9916. Menu->Add(Item.release());
  9917. AddMenuSeparator(Menu);
  9918. if (Terminal != NULL)
  9919. {
  9920. CreateOpenDirMenuList(Menu, Side, WinConfiguration->Bookmarks[Terminal->SessionData->SessionKey]);
  9921. }
  9922. CreateOpenDirMenuList(Menu, Side, WinConfiguration->SharedBookmarks);
  9923. Item.reset(new TTBXItem(Owner));
  9924. Item->Action = (Side == osLocal) ? NonVisualDataModule->LocalAddBookmarkAction : NonVisualDataModule->RemoteAddBookmarkAction;
  9925. Menu->Add(Item.release());
  9926. }
  9927. //---------------------------------------------------------------------------
  9928. bool __fastcall TCustomScpExplorerForm::TryOpenDirectory(TOperationSide Side, const UnicodeString & Path)
  9929. {
  9930. bool Result = true;
  9931. try
  9932. {
  9933. UnicodeString APath = Path;
  9934. if (Side == osRemote)
  9935. {
  9936. Terminal->ExceptionOnFail = true;
  9937. }
  9938. else
  9939. {
  9940. APath = ExpandEnvironmentVariables(Path);
  9941. }
  9942. try
  9943. {
  9944. DirView(Side)->Path = APath;
  9945. }
  9946. __finally
  9947. {
  9948. if (Side == osRemote)
  9949. {
  9950. Terminal->ExceptionOnFail = false;
  9951. }
  9952. }
  9953. }
  9954. catch (Exception & E)
  9955. {
  9956. if ((Side != osRemote) || Terminal->Active)
  9957. {
  9958. ShowExtendedExceptionEx(Terminal, &E);
  9959. Result = false;
  9960. }
  9961. else
  9962. {
  9963. throw;
  9964. }
  9965. }
  9966. return Result;
  9967. }
  9968. //---------------------------------------------------------------------------
  9969. void __fastcall TCustomScpExplorerForm::ReloadDirectory(TOperationSide Side)
  9970. {
  9971. // Make sure there some feedback even when loading tiny local folders
  9972. TInstantOperationVisualizer Visualizer;
  9973. DirView(Side)->ReloadDirectory();
  9974. }
  9975. //---------------------------------------------------------------------------
  9976. void __fastcall TCustomScpExplorerForm::CloseSessionTab(int Index)
  9977. {
  9978. if (Index == SessionsPageControl->TabIndex)
  9979. {
  9980. CloseSession();
  9981. }
  9982. else
  9983. {
  9984. TTabSheet * TabSheet = SessionsPageControl->Pages[Index];
  9985. TTerminal * Terminal = GetSessionTabTerminal(TabSheet);
  9986. TTerminalManager * Manager = TTerminalManager::Instance();
  9987. if (!Terminal->Active)
  9988. {
  9989. Manager->FreeTerminal(Terminal);
  9990. }
  9991. else
  9992. {
  9993. TTerminalQueue * Queue = Manager->FindQueueForTerminal(Terminal);
  9994. if (CanCloseQueue(Queue))
  9995. {
  9996. Manager->FreeTerminal(Terminal);
  9997. }
  9998. }
  9999. }
  10000. }
  10001. //---------------------------------------------------------------------------
  10002. void __fastcall TCustomScpExplorerForm::SessionsPageControlCloseButtonClick(TPageControl * /*Sender*/, int Index)
  10003. {
  10004. CloseSessionTab(Index);
  10005. }
  10006. //---------------------------------------------------------------------------
  10007. void __fastcall TCustomScpExplorerForm::CopyFilesToClipboard(TOperationSide Side, bool OnFocused)
  10008. {
  10009. if (DebugAlwaysTrue(!IsSideLocalBrowser(Side)))
  10010. {
  10011. TInstantOperationVisualizer Visualizer;
  10012. // To trigger ClipboardDataObjectRelease (in case the clipboard already contains our data)
  10013. // before we create a new data object
  10014. ClipboardClear();
  10015. DebugAssert(FClipboardFakeDirectory.IsEmpty());
  10016. DebugAssert(FClipboardFakeMonitors.get() == NULL);
  10017. FClipboardFakeDirectory = CreateFakeTransferDirectory();
  10018. // Intermediate variable to WORKAROUND a crash in compiler
  10019. TObjectList * ClipboardFakeMonitors =
  10020. StartCreationDirectoryMonitorsOnEachDrive(FILE_NOTIFY_CHANGE_DIR_NAME, ClipboardFakeCreated);
  10021. FClipboardFakeMonitors.reset(ClipboardFakeMonitors);
  10022. if (FClipboardDragDropFilesEx.get() == NULL)
  10023. {
  10024. FClipboardDragDropFilesEx.reset(new TDragDropFilesEx(this));
  10025. FClipboardDragDropFilesEx->OnDataObjectRelease = ClipboardDataObjectRelease;
  10026. }
  10027. FClipboardDragDropFilesEx->FileList->Clear();
  10028. FClipboardDragDropFilesEx->FileList->AddItem(NULL, FClipboardFakeDirectory);
  10029. FClipboardDragDropFilesEx->CopyToClipboard();
  10030. FClipboardTerminal = FTerminal;
  10031. // Need full paths, as cwd can be different once files are pasted
  10032. FClipboardFileList.reset(TRemoteFileList::CloneStrings(RemoteDirView->CreateFileList(OnFocused, true)));
  10033. for (int Index = 0; Index < FClipboardFileList->Count; Index++)
  10034. {
  10035. FClipboardFileList->Strings[Index] = UnixExcludeTrailingBackslash(FClipboardFileList->Strings[Index]);
  10036. }
  10037. }
  10038. }
  10039. //---------------------------------------------------------------------------
  10040. void __fastcall TCustomScpExplorerForm::ClipboardClear()
  10041. {
  10042. if (OpenClipboard(0))
  10043. {
  10044. EmptyClipboard(); // Calls ClipboardDataObjectRelease, if clipboard contains our data
  10045. CloseClipboard();
  10046. }
  10047. }
  10048. //---------------------------------------------------------------------------
  10049. bool __fastcall TCustomScpExplorerForm::DoesClipboardContainOurFiles()
  10050. {
  10051. return !FClipboardFakeDirectory.IsEmpty();
  10052. }
  10053. //---------------------------------------------------------------------------
  10054. void __fastcall TCustomScpExplorerForm::ClipboardStop()
  10055. {
  10056. RemoveDir(ApiPath(FClipboardFakeDirectory));
  10057. FClipboardFakeDirectory = UnicodeString();
  10058. FClipboardTerminal = NULL;
  10059. if (FDownloadingFromClipboard)
  10060. {
  10061. // We are called by the monitor, so attempt to release it would deadlonk
  10062. FClipboardFakeMonitorsPendingReset = true;
  10063. }
  10064. else
  10065. {
  10066. FClipboardFakeMonitors.reset(NULL);
  10067. }
  10068. }
  10069. //---------------------------------------------------------------------------
  10070. void __fastcall TCustomScpExplorerForm::ClipboardDataObjectRelease(TObject * /*Sender*/)
  10071. {
  10072. if (DebugAlwaysTrue(!FClipboardFakeDirectory.IsEmpty()))
  10073. {
  10074. ClipboardStop();
  10075. }
  10076. }
  10077. //---------------------------------------------------------------------------
  10078. void __fastcall TCustomScpExplorerForm::ClipboardDownload(const UnicodeString & TargetDirectory, bool NoConfirmation, bool DragDrop)
  10079. {
  10080. TTerminalManager * Manager = TTerminalManager::Instance();
  10081. Manager->ActiveTerminal = FClipboardTerminal;
  10082. TTransferOperationParam Params;
  10083. Params.TargetDirectory = TargetDirectory;
  10084. Params.DragDrop = DragDrop;
  10085. ExecuteFileOperation(foCopy, osRemote, FClipboardFileList.get(), NoConfirmation, &Params);
  10086. }
  10087. //---------------------------------------------------------------------------
  10088. void __fastcall TCustomScpExplorerForm::ClipboardFakeCreated(TObject * /*Sender*/, const UnicodeString FileName)
  10089. {
  10090. // It can actually rarelly happen that some random file is created, while we are shutting down the monitor
  10091. // (as it pumps a Windows message queue while being shutted down)
  10092. if (DebugAlwaysTrue(!FClipboardFakeDirectory.IsEmpty()) &&
  10093. SameText(ExtractFileName(FileName), ExtractFileName(FClipboardFakeDirectory)))
  10094. {
  10095. // Can fail as it can be e.g. locked by AV, so we retry that later
  10096. bool Removed = RemoveDir(ApiPath(FileName));
  10097. try
  10098. {
  10099. if (!NonVisualDataModule->Busy)
  10100. {
  10101. DebugAssert(!FClipboardFakeMonitorsPendingReset);
  10102. FClipboardFakeMonitorsPendingReset = false;
  10103. bool NoConfirmation = (WinConfiguration->DDTransferConfirmation == asOff);
  10104. TAutoFlag Flag(FDownloadingFromClipboard);
  10105. ClipboardDownload(ExtractFilePath(FileName), NoConfirmation, true);
  10106. }
  10107. }
  10108. __finally
  10109. {
  10110. if (!Removed)
  10111. {
  10112. RemoveDir(ApiPath(FileName));
  10113. }
  10114. }
  10115. }
  10116. }
  10117. //---------------------------------------------------------------------------
  10118. void __fastcall TCustomScpExplorerForm::DirViewGetItemColor(
  10119. TObject * Sender, UnicodeString FileName, bool Directory, __int64 Size, TDateTime Modification, TColor & Color)
  10120. {
  10121. TFileMasks::TParams MaskParams;
  10122. MaskParams.Size = Size;
  10123. MaskParams.Modification = Modification;
  10124. TCustomDirView * DirView = DebugNotNull(dynamic_cast<TCustomDirView *>(Sender));
  10125. bool Local = (dynamic_cast<TUnixDirView *>(DirView) == NULL);
  10126. for (TFileColorData::TList::const_iterator Iter = FFileColors.begin(); Iter != FFileColors.end(); Iter++)
  10127. {
  10128. bool ImplicitMatch;
  10129. if (Iter->FileMask.Matches(FileName, Local, Directory, &MaskParams, false, ImplicitMatch) &&
  10130. !ImplicitMatch)
  10131. {
  10132. Color = Iter->Color;
  10133. break;
  10134. }
  10135. }
  10136. }
  10137. //---------------------------------------------------------------------------
  10138. void __fastcall TCustomScpExplorerForm::DirViewKeyDown(TObject * Sender, WORD & Key, TShiftState)
  10139. {
  10140. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  10141. if (!DirView->IsEditing())
  10142. {
  10143. // Handled here, instead of RemoteDirViewKeyPress (as on Login dialog),
  10144. // as VK_BACK is handled by TCustomDirView.KeyDown and we can swallow it here,
  10145. // while searching
  10146. if (Key == VK_BACK)
  10147. {
  10148. if (!FIncrementalSearch.IsEmpty())
  10149. {
  10150. if (FIncrementalSearch.Length() == 1)
  10151. {
  10152. ResetIncrementalSearch();
  10153. }
  10154. else
  10155. {
  10156. UnicodeString NewText = FIncrementalSearch.SubString(1, FIncrementalSearch.Length() - 1);
  10157. IncrementalSearch(NewText, false, false);
  10158. }
  10159. Key = 0;
  10160. }
  10161. }
  10162. }
  10163. }
  10164. //---------------------------------------------------------------------------
  10165. void __fastcall TCustomScpExplorerForm::DirViewKeyPress(TObject * Sender, wchar_t & Key)
  10166. {
  10167. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  10168. if (!DirView->IsEditing() && (WinConfiguration->PanelSearch != isOff))
  10169. {
  10170. // Filter control sequences.
  10171. // When not searching yet, prefer use of the space for toggling file selection
  10172. // (so we cannot incrementally search for a string starting with a space).
  10173. if ((Key > VK_SPACE) ||
  10174. ((Key == VK_SPACE) && (GetKeyState(VK_CONTROL) >= 0) && !FIncrementalSearch.IsEmpty()))
  10175. {
  10176. IncrementalSearch(FIncrementalSearch + Key, false, false);
  10177. Key = 0;
  10178. }
  10179. }
  10180. }
  10181. //---------------------------------------------------------------------------
  10182. void __fastcall TCustomScpExplorerForm::ResetIncrementalSearch()
  10183. {
  10184. if (!FIncrementalSearch.IsEmpty())
  10185. {
  10186. FIncrementalSearch = L"";
  10187. DirView(osCurrent)->UpdateStatusBar();
  10188. }
  10189. }
  10190. //---------------------------------------------------------------------------
  10191. void __fastcall TCustomScpExplorerForm::IncrementalSearch(const UnicodeString & Text, bool SkipCurrent, bool Reverse)
  10192. {
  10193. TListItem * Item = SearchFile(Text, SkipCurrent, Reverse);
  10194. if (Item == NULL)
  10195. {
  10196. MessageBeep(MB_ICONHAND);
  10197. }
  10198. else
  10199. {
  10200. TCustomDirView * ADirView = DirView(osCurrent);
  10201. {
  10202. TAutoNestingCounter Guard(FIncrementalSearching);
  10203. ADirView->FocusItem(Item);
  10204. if (ADirView->NortonLike == nlOff)
  10205. {
  10206. if ((ADirView->Selected != Item) ||
  10207. (ADirView->SelCount > 1))
  10208. {
  10209. ADirView->ClearSelection();
  10210. ADirView->Selected = Item;
  10211. }
  10212. }
  10213. }
  10214. FIncrementalSearch = Text;
  10215. Item->MakeVisible(false);
  10216. TListItem * NextItem = SearchFile(Text, true, Reverse);
  10217. FIncrementalSearchHaveNext = (NextItem != NULL) && (NextItem != Item);
  10218. ADirView->UpdateStatusBar();
  10219. }
  10220. }
  10221. //---------------------------------------------------------------------------
  10222. TListItem * __fastcall TCustomScpExplorerForm::GetNextFile(TListItem * Item, bool Reverse)
  10223. {
  10224. int Index = Item->Index;
  10225. if (!Reverse)
  10226. {
  10227. Index++;
  10228. if (Index >= Item->Owner->Count)
  10229. {
  10230. Index = 0;
  10231. }
  10232. }
  10233. else
  10234. {
  10235. Index--;
  10236. if (Index < 0)
  10237. {
  10238. Index = Item->Owner->Count - 1;
  10239. }
  10240. }
  10241. return Item->Owner->Item[Index];
  10242. }
  10243. //---------------------------------------------------------------------------
  10244. TListItem * __fastcall TCustomScpExplorerForm::SearchFile(const UnicodeString & Text, bool SkipCurrent, bool Reverse)
  10245. {
  10246. TCustomDirView * ADirView = DirView(osCurrent);
  10247. if (ADirView->Items->Count == 0)
  10248. {
  10249. return NULL;
  10250. }
  10251. else
  10252. {
  10253. TListItem * CurrentItem = (ADirView->ItemFocused != NULL) ? ADirView->ItemFocused : ADirView->Items->Item[0];
  10254. TListItem * Item = CurrentItem;
  10255. if (SkipCurrent)
  10256. {
  10257. Item = DebugNotNull(GetNextFile(Item, Reverse));
  10258. }
  10259. while (true)
  10260. {
  10261. bool Matches = false;
  10262. switch (WinConfiguration->PanelSearch)
  10263. {
  10264. case isNameStartOnly:
  10265. Matches = ContainsTextSemiCaseSensitive(ADirView->ItemFileName(Item).SubString(1, Text.Length()), Text);
  10266. break;
  10267. case isName:
  10268. Matches = ContainsTextSemiCaseSensitive(ADirView->ItemFileName(Item), Text);
  10269. break;
  10270. case isAll:
  10271. int ColCount = (ADirView->ViewStyle == vsReport) ? ADirView->ColProperties->Count : 1;
  10272. int Index = 0;
  10273. while ((Index < ColCount) && !Matches)
  10274. {
  10275. bool Visible = (ADirView->ViewStyle == vsReport) ? ADirView->ColProperties->Visible[Index] : true;
  10276. if (Visible)
  10277. {
  10278. Matches = ContainsTextSemiCaseSensitive(ADirView->GetColumnText(Item, Index), Text);
  10279. }
  10280. Index++;
  10281. }
  10282. break;
  10283. }
  10284. if (Matches)
  10285. {
  10286. return Item;
  10287. }
  10288. Item = GetNextFile(Item, Reverse);
  10289. if (Item == CurrentItem)
  10290. {
  10291. return NULL;
  10292. }
  10293. }
  10294. }
  10295. }
  10296. //---------------------------------------------------------------------------
  10297. void __fastcall TCustomScpExplorerForm::DirViewExit(TObject *)
  10298. {
  10299. ResetIncrementalSearch();
  10300. }
  10301. //---------------------------------------------------------------------------
  10302. void __fastcall TCustomScpExplorerForm::CMDialogKey(TWMKeyDown & Message)
  10303. {
  10304. if (Message.CharCode == VK_TAB)
  10305. {
  10306. if (!FIncrementalSearch.IsEmpty())
  10307. {
  10308. TShiftState Shift = KeyDataToShiftState(Message.KeyData);
  10309. bool Reverse = Shift.Contains(ssShift);
  10310. IncrementalSearch(FIncrementalSearch, true, Reverse);
  10311. Message.Result = 1;
  10312. return;
  10313. }
  10314. }
  10315. else if (Message.CharCode == VK_ESCAPE)
  10316. {
  10317. if (!FIncrementalSearch.IsEmpty())
  10318. {
  10319. ResetIncrementalSearch();
  10320. Message.Result = 1;
  10321. return;
  10322. }
  10323. }
  10324. TForm::Dispatch(&Message);
  10325. }
  10326. //---------------------------------------------------------------------------
  10327. void __fastcall TCustomScpExplorerForm::Deactivate()
  10328. {
  10329. TForm::Deactivate();
  10330. // E.g. when switching to an internal editor window
  10331. ResetIncrementalSearch();
  10332. }
  10333. //---------------------------------------------------------------------------
  10334. void __fastcall TCustomScpExplorerForm::ApplicationEventsDeactivate(TObject *)
  10335. {
  10336. // When switching to another application
  10337. ResetIncrementalSearch();
  10338. }
  10339. //---------------------------------------------------------------------------
  10340. void __fastcall TCustomScpExplorerForm::ApplicationEventsModalBegin(TObject *)
  10341. {
  10342. ResetIncrementalSearch();
  10343. }
  10344. //---------------------------------------------------------------------------
  10345. void __fastcall TCustomScpExplorerForm::DirViewChangeFocus(TObject *, TListItem *)
  10346. {
  10347. if (FIncrementalSearching <= 0)
  10348. {
  10349. ResetIncrementalSearch();
  10350. }
  10351. }
  10352. //---------------------------------------------------------------------------
  10353. void __fastcall TCustomScpExplorerForm::RemoteStatusBarMouseDown(TObject *, TMouseButton, TShiftState, int, int)
  10354. {
  10355. CountClicksForWindowPrint(this);
  10356. }
  10357. //---------------------------------------------------------------------------
  10358. void __fastcall TCustomScpExplorerForm::RemoteDirViewResize(TObject *)
  10359. {
  10360. CenterReconnectToolbar();
  10361. }
  10362. //---------------------------------------------------------------------------
  10363. void __fastcall TCustomScpExplorerForm::BrowseFile()
  10364. {
  10365. if (RemoteDirView->ItemFocused != NULL)
  10366. {
  10367. RemoteDirView->ItemFocused->Selected = true;
  10368. }
  10369. }
  10370. //---------------------------------------------------------------------------
  10371. void __fastcall TCustomScpExplorerForm::UpdateQueueFileList()
  10372. {
  10373. if (!FQueueStatusUpdating)
  10374. {
  10375. bool Refresh;
  10376. if ((FQueueStatus != NULL) && QueueFileList->Visible)
  10377. {
  10378. Refresh = FQueueStatus->UpdateFileList(FQueueController->GetFocusedPrimaryItem(), FQueueFileList.get());
  10379. }
  10380. else
  10381. {
  10382. if (FQueueFileList->GetCount() > 0)
  10383. {
  10384. FQueueFileList->Clear();
  10385. Refresh = true;
  10386. }
  10387. }
  10388. int Count = FQueueFileList->GetCount();
  10389. if (QueueFileList->Items->Count != Count)
  10390. {
  10391. DebugAssert(Refresh);
  10392. QueueFileList->Items->Count = Count;
  10393. }
  10394. if (Refresh)
  10395. {
  10396. QueueFileList->Invalidate();
  10397. }
  10398. DebugAssert(QueueFileList->Items->Count == FQueueFileList->GetCount());
  10399. }
  10400. }
  10401. //---------------------------------------------------------------------------
  10402. void __fastcall TCustomScpExplorerForm::QueueFileListData(TObject *, TListItem * Item)
  10403. {
  10404. Item->Caption = FQueueFileList->GetFileName(Item->Index);
  10405. }
  10406. //---------------------------------------------------------------------------
  10407. void __fastcall TCustomScpExplorerForm::QueueView3Change(TObject *, TListItem *, TItemChange)
  10408. {
  10409. // Should be improved, once we do not refresh the list when switching between details of the same batch.
  10410. // See TSynchronizeChecklistDialog::UpdateTimer
  10411. if (QueueFileList->Items->Count > 0)
  10412. {
  10413. QueueFileList->Items->Item[0]->MakeVisible(false);
  10414. }
  10415. UpdateQueueFileList();
  10416. }
  10417. //---------------------------------------------------------------------------
  10418. void __fastcall TCustomScpExplorerForm::QueueLabelGetStatus(TCustomPathLabel *, bool & Active)
  10419. {
  10420. Active = QueueView3->Focused() || QueueFileList->Focused();
  10421. }
  10422. //---------------------------------------------------------------------------
  10423. void __fastcall TCustomScpExplorerForm::QueueFileListEnterExit(TObject *)
  10424. {
  10425. QueueLabelUpdateStatus();
  10426. }
  10427. //---------------------------------------------------------------------------
  10428. void __fastcall TCustomScpExplorerForm::QueueFileListCustomDrawItem(
  10429. TCustomListView * Sender, TListItem * Item, TCustomDrawState, bool & DebugUsedArg(DefaultDraw))
  10430. {
  10431. int State = FQueueFileList->GetState(Item->Index);
  10432. if (State == qfsProcessed)
  10433. {
  10434. Sender->Canvas->Font->Color = clGrayText;
  10435. }
  10436. }
  10437. //---------------------------------------------------------------------------
  10438. void __fastcall TCustomScpExplorerForm::QueueFileListColumnAutoSize()
  10439. {
  10440. // We do not really need whole width, so we always preemptivelly keep free space for the vertical scrollbar
  10441. QueueFileList->Column[0]->Width =
  10442. QueueFileList->ClientWidth - GetSystemMetricsForControl(QueueFileList, SM_CXVSCROLL) - ScaleByTextHeight(QueueFileList, 8);
  10443. }
  10444. //---------------------------------------------------------------------------
  10445. void __fastcall TCustomScpExplorerForm::QueueFileListResize(TObject *)
  10446. {
  10447. QueueFileListColumnAutoSize();
  10448. }
  10449. //---------------------------------------------------------------------------
  10450. void __fastcall TCustomScpExplorerForm::CloseApp()
  10451. {
  10452. // Called from TNonVisualDataModule::ExplorerActionsExecute, which sets busy flag.
  10453. // FormCloseQuery must check busy state, so that the application cannot be closed while busy by Alt+F4 and X button.
  10454. // So we clear the flag here.
  10455. DebugAssert(NonVisualDataModule->Busy);
  10456. NonVisualDataModule->EndBusy();
  10457. try
  10458. {
  10459. Close();
  10460. }
  10461. __finally
  10462. {
  10463. NonVisualDataModule->StartBusy();
  10464. }
  10465. }
  10466. //---------------------------------------------------------------------------
  10467. bool __fastcall TCustomScpExplorerForm::IsActiveTerminal(TTerminal * Terminal)
  10468. {
  10469. return (Terminal != NULL) && Terminal->Active;
  10470. }
  10471. //---------------------------------------------------------------------------
  10472. bool __fastcall TCustomScpExplorerForm::HasActiveTerminal()
  10473. {
  10474. return IsActiveTerminal(Terminal);
  10475. }