CustomScpExplorer.cpp 399 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976
  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 <algorithm>
  8. #include <Bookmarks.h>
  9. #include <Interface.h>
  10. #include <Exceptions.h>
  11. #include <CoreMain.h>
  12. #include <FileSystems.h>
  13. #include <TextsCore.h>
  14. #include <TextsWin.h>
  15. #include <HelpWin.h>
  16. #include <VCLCommon.h>
  17. #include <Progress.h>
  18. #include <SynchronizeProgress.h>
  19. #include <DragExt.h>
  20. #include <WinApi.h>
  21. #include "GUITools.h"
  22. #include "NonVisual.h"
  23. #include "Glyphs.h"
  24. #include "Tools.h"
  25. #include "WinConfiguration.h"
  26. #include "TerminalManager.h"
  27. #include "EditorManager.h"
  28. #include "ProgParams.h"
  29. #include "Setup.h"
  30. #include <Consts.hpp>
  31. #include <DateUtils.hpp>
  32. #include <TB2Common.hpp>
  33. #include <DirectoryMonitor.hpp>
  34. #include <System.IOUtils.hpp>
  35. //---------------------------------------------------------------------------
  36. #pragma package(smart_init)
  37. #pragma link "CustomDirView"
  38. #pragma link "CustomUnixDirView"
  39. #pragma link "IEListView"
  40. #pragma link "NortonLikeListView"
  41. #pragma link "UnixDirView"
  42. #pragma link "CustomDriveView"
  43. #pragma link "UnixDriveView"
  44. #pragma link "CustomDriveView"
  45. #pragma link "TB2Dock"
  46. #pragma link "TBXStatusBars"
  47. #pragma link "TB2Item"
  48. #pragma link "TB2Toolbar"
  49. #pragma link "ThemePageControl"
  50. #pragma link "PathLabel"
  51. #pragma resource "*.dfm"
  52. //---------------------------------------------------------------------------
  53. #define WM_COMPONENT_HIDE (WM_WINSCP_USER + 4)
  54. #define WM_PASTE_FILES (WM_WINSCP_USER + 14)
  55. static const int SessionPanelCount = 3;
  56. //---------------------------------------------------------------------------
  57. class TMutexGuard
  58. {
  59. public:
  60. TMutexGuard(HANDLE AMutex, int Message = MUTEX_RELEASE_TIMEOUT,
  61. int Timeout = 5000)
  62. {
  63. FMutex = NULL;
  64. unsigned long WaitResult = WaitForSingleObject(AMutex, Timeout);
  65. if (WaitResult == WAIT_TIMEOUT)
  66. {
  67. throw Exception(LoadStr(MUTEX_RELEASE_TIMEOUT));
  68. }
  69. else
  70. {
  71. FMutex = AMutex;
  72. }
  73. }
  74. ~TMutexGuard()
  75. {
  76. if (FMutex != NULL)
  77. {
  78. ReleaseMutex(FMutex);
  79. }
  80. }
  81. private:
  82. HANDLE FMutex;
  83. };
  84. //---------------------------------------------------------------------------
  85. class TWindowLock
  86. {
  87. public:
  88. TWindowLock(TCustomScpExplorerForm * Form) :
  89. FForm(Form)
  90. {
  91. FForm->LockWindow();
  92. }
  93. ~TWindowLock()
  94. {
  95. FForm->UnlockWindow();
  96. }
  97. private:
  98. TCustomScpExplorerForm * FForm;
  99. };
  100. //---------------------------------------------------------------------------
  101. TAutoBatch::TAutoBatch(TCustomScpExplorerForm * Form) :
  102. FForm(Form)
  103. {
  104. FForm->BatchStart(FBatchStorage);
  105. }
  106. //---------------------------------------------------------------------------
  107. TAutoBatch::~TAutoBatch()
  108. {
  109. FForm->BatchEnd(FBatchStorage);
  110. }
  111. //---------------------------------------------------------------------------
  112. struct TTransferOperationParam
  113. {
  114. TTransferOperationParam();
  115. UnicodeString TargetDirectory;
  116. bool Temp;
  117. bool DragDrop;
  118. int Options;
  119. TAutoSwitch Queue;
  120. };
  121. //---------------------------------------------------------------------------
  122. TTransferOperationParam::TTransferOperationParam()
  123. {
  124. Temp = false;
  125. DragDrop = false;
  126. Options = 0;
  127. Queue = asAuto;
  128. }
  129. //---------------------------------------------------------------------------
  130. class TTransferPresetNoteData : public TObject
  131. {
  132. public:
  133. UnicodeString Message;
  134. };
  135. //---------------------------------------------------------------------------
  136. class TTerminalNoteData : public TObject
  137. {
  138. public:
  139. TManagedTerminal * Terminal;
  140. };
  141. //---------------------------------------------------------------------------
  142. __fastcall TCustomScpExplorerForm::TCustomScpExplorerForm(TComponent* Owner):
  143. FFormRestored(false),
  144. TForm(Owner)
  145. {
  146. AddStartupSequence(L"F");
  147. FInvalid = false;
  148. FCurrentSide = osRemote;
  149. FEverShown = false;
  150. FDocks = new TList();
  151. InitControls();
  152. DebugAssert(NonVisualDataModule && !NonVisualDataModule->ScpExplorer);
  153. // So that UpdateCustomCommandsToolbar called from RestoreParams works
  154. NonVisualDataModule->ScpExplorer = this;
  155. RestoreParams();
  156. ConfigurationChanged();
  157. RemoteDirView->Invalidate();
  158. FAutoOperation = false;
  159. FOnFileOperationFinished = NULL;
  160. FPrimaryOperation = foNone;
  161. FForceExecution = false;
  162. FIgnoreNextDialogChar = 0;
  163. FErrorList = NULL;
  164. FSynchronizeProgressForm = NULL;
  165. FProgressForm = NULL;
  166. FRefreshLocalDirectory = false;
  167. FRefreshRemoteDirectory = false;
  168. FDDExtMapFile = NULL;
  169. // CreateMutexW keeps failing with ERROR_NOACCESS
  170. FDDExtMutex = CreateMutexA(NULL, false, AnsiString(DRAG_EXT_MUTEX).c_str());
  171. DebugAssert(FDDExtMutex != NULL);
  172. FDDTargetControl = NULL;
  173. FDelayedDeletionTimer = NULL;
  174. FDelayedDeletionList = new TStringList();
  175. FDDFileList = NULL;
  176. FPendingTempSpaceWarn = false;
  177. FCapturedLog = NULL;
  178. FDragDropOperation = false;
  179. memset(&FHistoryMenu, 0, sizeof(FHistoryMenu));
  180. FAllowTransferPresetAutoSelect = true;
  181. FSessionChanging = false;
  182. FCopyParamDefault = L"";
  183. FSynchronizeController = NULL;
  184. FPendingQueueActionItem = NULL;
  185. FLockLevel = 0;
  186. FLockSuspendLevel = 0;
  187. FDisabledOnLockSuspend = false;
  188. FAlternativeDelete = false;
  189. FTrayIcon = new ::TTrayIcon(0);
  190. FTrayIcon->OnClick = TrayIconClick;
  191. FMaxQueueLength = 0;
  192. FLastContextPopupScreenPoint = TPoint(-1, -1);
  193. FTransferResumeList = NULL;
  194. FDeletedFiles = NULL;
  195. FMoveToQueue = false;
  196. StandaloneOperation = false;
  197. FOnFeedSynchronizeError = NULL;
  198. FOnSynchronizeAbort = NULL;
  199. FSynchronizeTerminal = NULL;
  200. FNeedSession = false;
  201. FDoNotIdleCurrentTerminal = 0;
  202. FIncrementalSearching = 0;
  203. FIncrementalSearchHaveNext = false;
  204. FQueueFileList.reset(new TQueueFileList());
  205. FProgressSide = osCurrent;
  206. FCalculateSizeOperation = NULL;
  207. FDownloadingFromClipboard = false;
  208. FClipboardFakeMonitorsPendingReset = false;
  209. FEditorManager = new TEditorManager();
  210. FEditorManager->OnFileChange = ExecutedFileChanged;
  211. FEditorManager->OnFileReload = ExecutedFileReload;
  212. FEditorManager->OnFileEarlyClosed = ExecutedFileEarlyClosed;
  213. FEditorManager->OnFileUploadComplete = ExecutedFileUploadComplete;
  214. FLocalEditors = new TList();
  215. FQueueStatus = NULL;
  216. FQueueStatusSection = new TCriticalSection();
  217. FQueueStatusInvalidated = false;
  218. FQueueItemInvalidated = false;
  219. FQueueStatusUpdating = false;
  220. FQueueActedItem = NULL;
  221. FQueueController = new TQueueController(QueueView3);
  222. FQueueScrollOnDragOver = new TListViewScrollOnDragOver(QueueView3, true);
  223. FUserActionTimer = new TTimer(this);
  224. FUserActionTimer->Enabled = false;
  225. FUserActionTimer->Interval = 10;
  226. FUserActionTimer->OnTimer = UserActionTimer;
  227. FNotes = new TStringList();
  228. FNoteTimer = new TTimer(this);
  229. FNoteTimer->Enabled = false;
  230. FNoteTimer->OnTimer = NoteTimer;
  231. FOnNoteClick = NULL;
  232. FOle32Library = LoadLibrary(L"Ole32.dll");
  233. FDragMoveCursor = FOle32Library != NULL ?
  234. LoadCursor(FOle32Library, MAKEINTRESOURCE(2)) : NULL;
  235. UseSystemSettings(this);
  236. TTBXStringList * TransferList = dynamic_cast<TTBXStringList*>(
  237. static_cast<TObject*>(GetComponent(fcTransferList)));
  238. DebugAssert(TransferList != NULL);
  239. FTransferListHoverIndex = -1;
  240. TransferList->OnChange = TransferListChange;
  241. TransferList->OnDrawItem = TransferListDrawItem;
  242. SetSubmenu(dynamic_cast<TTBXCustomItem *>(static_cast<TObject *>(GetComponent(fcColorMenu))), true);
  243. SetSubmenu(NonVisualDataModule->ColorMenuItem, true);
  244. UseDesktopFont(SessionsPageControl);
  245. UpdateSessionsPageControlHeight();
  246. UseDesktopFont(RemoteDirView);
  247. UseDesktopFont(RemoteDriveView);
  248. UseDesktopFont(QueueView3);
  249. UseDesktopFont(QueueFileList);
  250. UseDesktopFont(QueueLabel);
  251. UseDesktopFont(RemoteStatusBar);
  252. reinterpret_cast<TLabel*>(QueueSplitter)->OnDblClick = QueueSplitterDblClick;
  253. QueueSplitter->ShowHint = true;
  254. reinterpret_cast<TLabel*>(QueueFileListSplitter)->OnDblClick = QueueFileListSplitterDblClick;
  255. QueueFileListSplitter->ShowHint = true;
  256. RemotePanelSplitter->ShowHint = true;
  257. UpdateImages();
  258. FCustomCommandMenu = CreateTBXPopupMenu(this);
  259. FCustomCommandLocalFileList = NULL;
  260. FCustomCommandRemoteFileList = NULL;
  261. FSessionColors = new TPngImageList(this);
  262. FSessionColors->ColorDepth = cd32Bit;
  263. AddFixedSessionImages();
  264. SessionsPageControl->Images = FSessionColors;
  265. UpdateQueueLabel();
  266. CheckStoreTransition();
  267. CreateHiddenWindow();
  268. StartUpdates();
  269. // Start loading drive data, so that they are ready once we need them for StartCreationDirectoryMonitorsOnEachDrive
  270. // (necessary for "explorer" only, as "commander" loads it for its drive menu)
  271. DriveInfo->NeedData();
  272. }
  273. //---------------------------------------------------------------------------
  274. __fastcall TCustomScpExplorerForm::~TCustomScpExplorerForm()
  275. {
  276. FInvalid = true;
  277. if (FClipboardTerminal != NULL)
  278. {
  279. ClipboardClear(); // implies ClipboardStop
  280. }
  281. // this has to be one of the very first things to do
  282. StopUpdateThread();
  283. // This is needed when shuting down Windows only. Otherwise it's already set NULL from Execute()
  284. TTerminalManager::Instance()->ScpExplorer = NULL;
  285. delete FCustomCommandLocalFileList;
  286. delete FCustomCommandRemoteFileList;
  287. delete FCustomCommandMenu;
  288. delete FTrayIcon;
  289. FTrayIcon = NULL;
  290. FEditorManager->CloseInternalEditors(ForceCloseInternalEditor);
  291. delete FEditorManager;
  292. ForceCloseLocalEditors();
  293. delete FLocalEditors;
  294. HideFileFindDialog();
  295. if (FDelayedDeletionTimer)
  296. {
  297. DoDelayedDeletion(NULL);
  298. SAFE_DESTROY(FDelayedDeletionTimer);
  299. }
  300. SAFE_DESTROY(FDelayedDeletionList);
  301. // sometimes we do not get DDEnd so the list is not released
  302. SAFE_DESTROY(FDDFileList);
  303. DebugAssert(FSynchronizeController == NULL);
  304. CloseHandle(FDDExtMutex);
  305. FDDExtMutex = NULL;
  306. FreeLibrary(FOle32Library);
  307. FOle32Library = NULL;
  308. FDragMoveCursor = NULL;
  309. DebugAssert(!FErrorList);
  310. if (FEverShown)
  311. {
  312. // when window is never shown (like when running command-line operation),
  313. // particularly window site is not restored correctly (BoundsRect value set
  314. // in RestoreForm gets lost during handle allocation), so we do not want
  315. // it to be stored
  316. StoreParams();
  317. }
  318. ManagedSession = NULL;
  319. Queue = NULL;
  320. DebugAssert(NonVisualDataModule && (NonVisualDataModule->ScpExplorer == this));
  321. NonVisualDataModule->ScpExplorer = NULL;
  322. delete FQueueScrollOnDragOver;
  323. FQueueScrollOnDragOver = NULL;
  324. delete FQueueController;
  325. FQueueController = NULL;
  326. delete FQueueStatusSection;
  327. FQueueStatusSection = NULL;
  328. delete FQueueStatus;
  329. FQueueStatus = NULL;
  330. delete FUserActionTimer;
  331. FUserActionTimer = NULL;
  332. delete FNoteTimer;
  333. delete FNotes;
  334. delete FNoteData;
  335. SAFE_DESTROY(FDocks);
  336. SessionsPageControl->Images = NULL;
  337. // only after clearing Terminal (after SessionListChanged => ... => AddSessionColor is called)
  338. SAFE_DESTROY(FSessionColors);
  339. SAFE_DESTROY(FSessionsDragDropFilesEx);
  340. SAFE_DESTROY(FHistoryMenu[0][0]);
  341. SAFE_DESTROY(FHistoryMenu[0][1]);
  342. SAFE_DESTROY(FHistoryMenu[1][0]);
  343. SAFE_DESTROY(FHistoryMenu[1][1]);
  344. if (FHiddenWindow != NULL)
  345. {
  346. DestroyWindow(FHiddenWindow);
  347. FHiddenWindow = NULL;
  348. }
  349. }
  350. //---------------------------------------------------------------------------
  351. void __fastcall TCustomScpExplorerForm::RefreshPanel(const UnicodeString & Session, const UnicodeString & Path)
  352. {
  353. std::unique_ptr<TSessionData> Data;
  354. if (!Session.IsEmpty())
  355. {
  356. bool DefaultsOnly;
  357. Data.reset(StoredSessions->ParseUrl(Session, NULL, DefaultsOnly));
  358. }
  359. TTerminalManager * Manager = TTerminalManager::Instance();
  360. for (int Index = 0; Index < Manager->Count; Index++)
  361. {
  362. TTerminal * Terminal = Manager->Sessions[Index];
  363. if (Session.IsEmpty() ||
  364. Manager->IsActiveTerminalForSite(Terminal, Data.get()))
  365. {
  366. if (Path.IsEmpty())
  367. {
  368. Terminal->ClearCaches();
  369. }
  370. else
  371. {
  372. Terminal->DirectoryModified(Path, true);
  373. }
  374. }
  375. }
  376. // We should flag a pending refresh for the background terminals or busy foreground terminals
  377. if (HasActiveTerminal() &&
  378. CanCommandLineFromAnotherInstance() &&
  379. (Session.IsEmpty() || Manager->IsActiveTerminalForSite(Terminal, Data.get())) &&
  380. (Path.IsEmpty() || UnixIsChildPath(Path, Terminal->CurrentDirectory)))
  381. {
  382. Terminal->ReloadDirectory();
  383. }
  384. }
  385. //---------------------------------------------------------------------------
  386. void __fastcall TCustomScpExplorerForm::WMCopyData(TMessage & Message)
  387. {
  388. PCOPYDATASTRUCT CopyData = reinterpret_cast<PCOPYDATASTRUCT>(Message.LParam);
  389. AppLogFmt(L"Received copy data message with size %d", (int(CopyData->cbData)));
  390. size_t MessageSize = sizeof(TCopyDataMessage);
  391. bool Result = DebugAlwaysTrue(CopyData->cbData == MessageSize);
  392. if (Result)
  393. {
  394. const TCopyDataMessage & Message = *reinterpret_cast<const TCopyDataMessage *>(CopyData->lpData);
  395. Result = (Message.Version == TCopyDataMessage::Version1);
  396. if (Result)
  397. {
  398. switch (Message.Command)
  399. {
  400. case TCopyDataMessage::CommandCanCommandLine:
  401. Result = CanCommandLineFromAnotherInstance();
  402. if (Result)
  403. {
  404. AppLog(L"Can process command-line from other instance");
  405. }
  406. else
  407. {
  408. AppLog(L"Cannot process command-line from other instance");
  409. }
  410. break;
  411. case TCopyDataMessage::CommandCommandLine:
  412. {
  413. UnicodeString CommandLine(Message.CommandLine);
  414. Result = CommandLineFromAnotherInstance(CommandLine);
  415. }
  416. break;
  417. case TCopyDataMessage::RefreshPanel:
  418. RefreshPanel(Message.Refresh.Session, Message.Refresh.Path);
  419. break;
  420. case TCopyDataMessage::MainWindowCheck:
  421. Result = true;
  422. break;
  423. default:
  424. Result = false;
  425. break;
  426. }
  427. }
  428. }
  429. Message.Result = Result ? 1 : 0;
  430. }
  431. //---------------------------------------------------------------------------
  432. void __fastcall TCustomScpExplorerForm::CreateHiddenWindow()
  433. {
  434. WNDCLASS WindowClass = {0};
  435. WindowClass.lpfnWndProc = DefWindowProc;
  436. WindowClass.hInstance = HInstance;
  437. WindowClass.lpszClassName = HIDDEN_WINDOW_NAME;
  438. FHiddenWindow = NULL;
  439. if (RegisterClass(&WindowClass))
  440. {
  441. FHiddenWindow = CreateWindow(HIDDEN_WINDOW_NAME, L"",
  442. WS_POPUP, 0, 0, 0, 0, 0, 0, HInstance, NULL);
  443. }
  444. }
  445. //---------------------------------------------------------------------------
  446. bool __fastcall TCustomScpExplorerForm::CanConsole()
  447. {
  448. return
  449. HasActiveTerminal() &&
  450. (Terminal->IsCapable[fcAnyCommand] || Terminal->IsCapable[fcSecondaryShell]);
  451. }
  452. //---------------------------------------------------------------------------
  453. bool __fastcall TCustomScpExplorerForm::CanCommandLineFromAnotherInstance()
  454. {
  455. bool Result = Visible && !NonVisualDataModule->Busy;
  456. return Result;
  457. }
  458. //---------------------------------------------------------------------------
  459. bool __fastcall TCustomScpExplorerForm::CommandLineFromAnotherInstance(
  460. const UnicodeString & CommandLine)
  461. {
  462. TProgramParams Params(CommandLine);
  463. bool Result = CanCommandLineFromAnotherInstance() && DebugAlwaysTrue(Params.ParamCount > 0);
  464. if (Result)
  465. {
  466. NonVisualDataModule->StartBusy();
  467. try
  468. {
  469. // this action is initiated from another process,
  470. // so it's likely that our window is not visible,
  471. // and user won't see what is going on
  472. Application->BringToFront();
  473. // reload sessions as we may be asked to open a session
  474. // just stored by another instance
  475. StoredSessions->Reload();
  476. UnicodeString SessionName = Params.Param[1];
  477. std::unique_ptr<TObjectList> DataList(new TObjectList());
  478. try
  479. {
  480. UnicodeString DownloadFile; // unused
  481. int Flags = GetCommandLineParseUrlFlags(&Params);
  482. GetLoginData(SessionName, &Params, DataList.get(), DownloadFile, true, this, Flags);
  483. }
  484. catch (EAbort &)
  485. {
  486. // Happens when opening session in PuTTY and in other situations,
  487. // in which we do not want to fail WM_COPYDATA, as that would cause master instance
  488. // to process the command-line on its own (or to pass it to yet another instance)
  489. }
  490. if (DataList->Count > 0)
  491. {
  492. TTerminalManager * Manager = TTerminalManager::Instance();
  493. Manager->ActiveSession = Manager->NewSessions(DataList.get());
  494. }
  495. }
  496. __finally
  497. {
  498. NonVisualDataModule->EndBusy();
  499. }
  500. }
  501. return Result;
  502. }
  503. //---------------------------------------------------------------------------
  504. TManagedTerminal * TCustomScpExplorerForm::GetTerminal()
  505. {
  506. TManagedTerminal * Result;
  507. if ((FManagedSession != NULL) && !FManagedSession->LocalBrowser)
  508. {
  509. Result = FManagedSession;
  510. }
  511. else
  512. {
  513. Result = NULL;
  514. }
  515. return Result;
  516. }
  517. //---------------------------------------------------------------------------
  518. void __fastcall TCustomScpExplorerForm::SetManagedSession(TManagedTerminal * value)
  519. {
  520. if (FManagedSession != value)
  521. {
  522. SessionChanging();
  523. DoSetManagedSession(value, false);
  524. }
  525. }
  526. //---------------------------------------------------------------------------
  527. void __fastcall TCustomScpExplorerForm::DoSetManagedSession(TManagedTerminal * value, bool Replace)
  528. {
  529. DebugAssert(!Replace || ((value != NULL) && !value->LocalBrowser));
  530. FManagedSession = value;
  531. {
  532. TValueRestorer<bool> AllowTransferPresetAutoSelectRestorer(FAllowTransferPresetAutoSelect);
  533. FAllowTransferPresetAutoSelect = false;
  534. TAutoFlag SessionChangingFlag(FSessionChanging);
  535. SessionChanged(Replace);
  536. }
  537. if (Terminal != NULL)
  538. {
  539. TransferPresetAutoSelect();
  540. }
  541. // Update app and tab titles, prevented above by FSessionChanging.
  542. UpdateControls();
  543. }
  544. //---------------------------------------------------------------------------
  545. void __fastcall TCustomScpExplorerForm::ReplaceTerminal(TManagedTerminal * value)
  546. {
  547. DoSetManagedSession(value, true);
  548. }
  549. //---------------------------------------------------------------------------
  550. void __fastcall TCustomScpExplorerForm::SessionChanging()
  551. {
  552. if (ManagedSession != NULL)
  553. {
  554. UpdateSession(ManagedSession);
  555. }
  556. }
  557. //---------------------------------------------------------------------------
  558. void __fastcall TCustomScpExplorerForm::SessionChanged(bool Replaced)
  559. {
  560. if (ManagedSession != NULL)
  561. {
  562. UpdateSessionColor(static_cast<TColor>(ManagedSession->StateData->Color));
  563. }
  564. if (!TTerminalManager::Instance()->Updating)
  565. {
  566. SessionListChanged();
  567. }
  568. if (Terminal != NULL)
  569. {
  570. TObject * State = NULL;
  571. if (WinConfiguration->PreservePanelState)
  572. {
  573. State = Terminal->RemoteExplorerState;
  574. DirView(osRemote)->AnnounceState(State);
  575. }
  576. try
  577. {
  578. if (Replaced)
  579. {
  580. RemoteDirView->ReplaceTerminal(Terminal);
  581. }
  582. else
  583. {
  584. RemoteDirView->Terminal = Terminal;
  585. }
  586. if (Terminal->Active)
  587. {
  588. Terminal->RefreshDirectory();
  589. }
  590. if (State != NULL)
  591. {
  592. DirView(osRemote)->RestoreState(State);
  593. }
  594. }
  595. __finally
  596. {
  597. DirView(osRemote)->AnnounceState(NULL);
  598. }
  599. }
  600. else
  601. {
  602. RemoteDirView->Terminal = NULL;
  603. }
  604. InitStatusBar();
  605. UpdateTransferList();
  606. // Update panels Enable state before refreshing the labels
  607. UpdateControls();
  608. }
  609. //---------------------------------------------------------------------------
  610. void __fastcall TCustomScpExplorerForm::SetQueue(TTerminalQueue * value)
  611. {
  612. if (Queue != value)
  613. {
  614. if (FQueue != NULL)
  615. {
  616. FQueue->OnListUpdate = NULL;
  617. FQueue->OnQueueItemUpdate = NULL;
  618. }
  619. FQueue = value;
  620. if (FQueue != NULL)
  621. {
  622. DebugAssert(FQueue->OnListUpdate == NULL);
  623. FQueue->OnListUpdate = QueueListUpdate;
  624. DebugAssert(FQueue->OnQueueItemUpdate == NULL);
  625. FQueue->OnQueueItemUpdate = QueueItemUpdate;
  626. }
  627. QueueChanged();
  628. }
  629. }
  630. //---------------------------------------------------------------------------
  631. void __fastcall TCustomScpExplorerForm::QueueView3Deletion(TObject * /*Sender*/,
  632. TListItem * Item)
  633. {
  634. if (FQueueActedItem == Item)
  635. {
  636. FQueueActedItem = NULL;
  637. if ((QueueView3->PopupMenu != NULL) &&
  638. (QueueView3->PopupMenu->PopupComponent == QueueView3))
  639. {
  640. // rather "trick", suggested by Jordan on jrsoftware.toolbar2000
  641. ReleaseCapture();
  642. }
  643. }
  644. if (Item->Data == FPendingQueueActionItem)
  645. {
  646. FPendingQueueActionItem = NULL;
  647. }
  648. }
  649. //---------------------------------------------------------------------------
  650. bool TCustomScpExplorerForm::IsAnythingQueued()
  651. {
  652. return (FQueueStatus != NULL) && (FQueueStatus->ActiveAndPendingPrimaryCount > 0);
  653. }
  654. //---------------------------------------------------------------------------
  655. void __fastcall TCustomScpExplorerForm::UpdateQueueStatus(bool QueueChanging)
  656. {
  657. {
  658. TGuard Guard(FQueueStatusSection);
  659. FQueueStatusInvalidated = false;
  660. if (FQueue != NULL)
  661. {
  662. FQueueStatus = FQueue->CreateStatus(FQueueStatus);
  663. }
  664. }
  665. if ((FQueueStatus != NULL) && (FQueueStatus->Count > FMaxQueueLength))
  666. {
  667. FMaxQueueLength = FQueueStatus->Count;
  668. Configuration->Usage->SetMax(L"MaxQueueLength", FMaxQueueLength);
  669. }
  670. {
  671. TAutoFlag Flag(FQueueStatusUpdating);
  672. FQueueController->UpdateQueueStatus(FQueueStatus);
  673. }
  674. UpdateQueueFileList();
  675. SetQueueProgress();
  676. UpdateQueueView();
  677. bool IsEmpty = !IsAnythingQueued();
  678. if (IsEmpty && (Terminal != NULL))
  679. {
  680. TOnceDoneOperation OnceDoneOperation =
  681. NonVisualDataModule->CurrentQueueOnceEmptyOperation();
  682. NonVisualDataModule->ResetQueueOnceEmptyOperation();
  683. if ((FQueue != NULL) && !WinConfiguration->EnableQueueByDefault && !QueueChanging)
  684. {
  685. FQueue->Enabled = false;
  686. }
  687. if ((OnceDoneOperation != odoIdle) && !NonVisualDataModule->Busy)
  688. {
  689. Terminal->CloseOnCompletion(OnceDoneOperation, LoadStr(CLOSED_ON_QUEUE_EMPTY));
  690. }
  691. }
  692. }
  693. //---------------------------------------------------------------------------
  694. UnicodeString __fastcall TCustomScpExplorerForm::GetQueueProgressTitle()
  695. {
  696. UnicodeString Result;
  697. if (FQueueStatus != NULL)
  698. {
  699. if (FQueueStatus->IsOnlyOneActiveAndNoPending())
  700. {
  701. TFileOperationProgressType * ProgressData =
  702. FQueueStatus->Items[FQueueStatus->DoneCount]->ProgressData;
  703. if ((ProgressData != NULL) && ProgressData->InProgress)
  704. {
  705. Result = TProgressForm::ProgressStr(NULL, ProgressData);
  706. }
  707. }
  708. else if (FQueueStatus->ActiveAndPendingPrimaryCount > 1)
  709. {
  710. Result = FMTLOAD(PROGRESS_IN_QUEUE, (FQueueStatus->ActiveAndPendingPrimaryCount));
  711. }
  712. }
  713. return Result;
  714. }
  715. //---------------------------------------------------------------------------
  716. void __fastcall TCustomScpExplorerForm::UpdateQueueView()
  717. {
  718. ComponentVisible[fcQueueView] =
  719. !IsLocalBrowserMode() &&
  720. ((WinConfiguration->QueueView.Show == qvShow) ||
  721. ((WinConfiguration->QueueView.Show == qvHideWhenEmpty) &&
  722. (FQueueStatus != NULL) && (FQueueStatus->Count > 0)));
  723. }
  724. //---------------------------------------------------------------------------
  725. void __fastcall TCustomScpExplorerForm::QueueChanged()
  726. {
  727. if (FQueueStatus != NULL)
  728. {
  729. // UpdateFileList implementation relies on the status being recreated when session changes
  730. delete FQueueStatus;
  731. FQueueStatus = NULL;
  732. }
  733. UpdateQueueStatus(true);
  734. }
  735. //---------------------------------------------------------------------------
  736. void __fastcall TCustomScpExplorerForm::QueueListUpdate(TTerminalQueue * Queue)
  737. {
  738. if (FQueue == Queue)
  739. {
  740. FQueueStatusInvalidated = true;
  741. }
  742. }
  743. //---------------------------------------------------------------------------
  744. void __fastcall TCustomScpExplorerForm::QueueItemUpdate(TTerminalQueue * Queue,
  745. TQueueItem * Item)
  746. {
  747. if (FQueue == Queue)
  748. {
  749. TGuard Guard(FQueueStatusSection);
  750. // this may be running in parallel with QueueChanged
  751. if (FQueueStatus != NULL)
  752. {
  753. TQueueItemProxy * QueueItem = FQueueStatus->FindByQueueItem(Item);
  754. if ((Item->Status == TQueueItem::qsDone) && (Terminal != NULL))
  755. {
  756. FRefreshLocalDirectory = (QueueItem == NULL) ||
  757. (!QueueItem->Info->ModifiedLocal.IsEmpty());
  758. FRefreshRemoteDirectory = (QueueItem == NULL) ||
  759. (!QueueItem->Info->ModifiedRemote.IsEmpty());
  760. }
  761. if (QueueItem != NULL)
  762. {
  763. QueueItem->UserData = (void*)true;
  764. FQueueItemInvalidated = true;
  765. }
  766. }
  767. }
  768. }
  769. //---------------------------------------------------------------------------
  770. bool __fastcall TCustomScpExplorerForm::IsQueueAutoPopup()
  771. {
  772. // during standalone editing, we have no way to see/control queue,
  773. // so we have to always popup prompts automatically
  774. return StandaloneOperation || GUIConfiguration->QueueAutoPopup;
  775. }
  776. //---------------------------------------------------------------------------
  777. void __fastcall TCustomScpExplorerForm::RefreshQueueItems()
  778. {
  779. if (FQueueStatus != NULL)
  780. {
  781. bool QueueAutoPopup = IsQueueAutoPopup();
  782. bool NeedRefresh = FQueueController->NeedRefresh();
  783. bool Refresh = FQueueItemInvalidated || NeedRefresh;
  784. FQueueItemInvalidated = false;
  785. int Limit = Refresh ? FQueueStatus->Count : FQueueStatus->DoneAndActiveCount;
  786. bool Updated = false;
  787. TQueueItemProxy * QueueItem;
  788. bool WasUserAction;
  789. for (int Index = 0; Index < Limit; Index++)
  790. {
  791. bool Update = false;
  792. QueueItem = FQueueStatus->Items[Index];
  793. WasUserAction = TQueueItem::IsUserActionStatus(QueueItem->Status);
  794. if (!NonVisualDataModule->Busy && QueueAutoPopup && WasUserAction &&
  795. (FPendingQueueActionItem == NULL))
  796. {
  797. FPendingQueueActionItem = QueueItem;
  798. FUserActionTimer->Enabled = true;
  799. }
  800. if ((bool)QueueItem->UserData)
  801. {
  802. QueueItem->UserData = (void*)false;
  803. QueueItem->Update();
  804. Updated = true;
  805. Update = true;
  806. }
  807. else if (FQueueController->QueueItemNeedsFrequentRefresh(QueueItem) || NeedRefresh)
  808. {
  809. Update = true;
  810. }
  811. if (Update)
  812. {
  813. FQueueController->RefreshQueueItem(QueueItem);
  814. }
  815. }
  816. if (Updated)
  817. {
  818. NonVisualDataModule->UpdateNonVisibleActions();
  819. SetQueueProgress();
  820. UpdateQueueFileList();
  821. }
  822. }
  823. }
  824. //---------------------------------------------------------------------------
  825. void __fastcall TCustomScpExplorerForm::SetTaskbarListProgressState(TBPFLAG Flags)
  826. {
  827. if (FTaskbarList != NULL)
  828. {
  829. // Could now use Application->MainFormHandle, now that we implement Application->OnGetMainFormHandle
  830. FTaskbarList->SetProgressState(GetMainForm()->Handle, Flags);
  831. }
  832. }
  833. //---------------------------------------------------------------------------
  834. void __fastcall TCustomScpExplorerForm::SetTaskbarListProgressValue(int Progress)
  835. {
  836. if (Progress >= 0)
  837. {
  838. if (FTaskbarList != NULL)
  839. {
  840. FTaskbarList->SetProgressValue(GetMainForm()->Handle, Progress, 100);
  841. }
  842. }
  843. else
  844. {
  845. SetTaskbarListProgressState(TBPF_INDETERMINATE);
  846. }
  847. }
  848. //---------------------------------------------------------------------------
  849. void __fastcall TCustomScpExplorerForm::SetTaskbarListProgressValue(TFileOperationProgressType * ProgressData)
  850. {
  851. int OverallProgress;
  852. // FProgressForm is null when this is called from SetQueueProgress
  853. if ((FProgressForm != NULL) && (FProgressForm->SynchronizeProgress != NULL))
  854. {
  855. OverallProgress = FProgressForm->SynchronizeProgress->Progress(ProgressData);
  856. }
  857. else if (!ProgressData->IsIndeterminate())
  858. {
  859. OverallProgress = ProgressData->OverallProgress();
  860. }
  861. else
  862. {
  863. OverallProgress = -1;
  864. }
  865. SetTaskbarListProgressValue(OverallProgress);
  866. }
  867. //---------------------------------------------------------------------------
  868. void __fastcall TCustomScpExplorerForm::SetQueueProgress()
  869. {
  870. TTerminalManager::Instance()->QueueStatusUpdated();
  871. if ((FTaskbarList != NULL) && (FProgressForm == NULL))
  872. {
  873. // Do not animate task bar button, when items are queued, but processing is disabled
  874. if ((FQueueStatus != NULL) && (FQueueStatus->ActivePrimaryCount > 0))
  875. {
  876. if (FQueueStatus->IsOnlyOneActiveAndNoPending())
  877. {
  878. TFileOperationProgressType * ProgressData = NULL;
  879. if (FQueueStatus->Items[FQueueStatus->DoneCount] != NULL)
  880. {
  881. ProgressData = FQueueStatus->Items[FQueueStatus->DoneCount]->ProgressData;
  882. }
  883. if ((ProgressData != NULL) &&
  884. ProgressData->InProgress)
  885. {
  886. SetTaskbarListProgressValue(ProgressData);
  887. }
  888. else
  889. {
  890. SetTaskbarListProgressState(TBPF_NOPROGRESS);
  891. }
  892. }
  893. else
  894. {
  895. SetTaskbarListProgressState(TBPF_INDETERMINATE);
  896. }
  897. }
  898. else
  899. {
  900. SetTaskbarListProgressState(TBPF_NOPROGRESS);
  901. }
  902. }
  903. UpdateQueueLabel();
  904. }
  905. //---------------------------------------------------------------------------
  906. void __fastcall TCustomScpExplorerForm::UpdateQueueLabel()
  907. {
  908. UnicodeString Caption = LoadStr(QUEUE_CAPTION);
  909. if (FQueueStatus != NULL)
  910. {
  911. int ActiveAndPendingPrimaryCount = FQueueStatus->ActiveAndPendingPrimaryCount;
  912. if (ActiveAndPendingPrimaryCount > 0)
  913. {
  914. Caption = FORMAT("%s (%d)", (Caption, ActiveAndPendingPrimaryCount));
  915. }
  916. }
  917. QueueLabel->Caption = Caption;
  918. }
  919. //---------------------------------------------------------------------------
  920. void TCustomScpExplorerForm::CheckStoreTransition()
  921. {
  922. if (WinConfiguration->StoreTransition == stInit)
  923. {
  924. if (IsUWP())
  925. {
  926. WinConfiguration->StoreTransition = stStoreFresh;
  927. AppLog(L"Fresh UWP/store installation");
  928. }
  929. else
  930. {
  931. WinConfiguration->StoreTransition = stStandard;
  932. AppLog(L"Fresh standard installation");
  933. }
  934. }
  935. else if ((WinConfiguration->StoreTransition == stStandard) && IsUWP())
  936. {
  937. WinConfiguration->StoreTransition = stStoreMigrated;
  938. WinConfiguration->FirstRun = WinConfiguration->FirstRun + L"=>" + StandardDatestamp();
  939. AppLog(L"Standard installation migrated to store installation");
  940. }
  941. if (WinConfiguration->StoreTransition == stStoreMigrated)
  942. {
  943. TTBXToolbar * Toolbar = new TTBXToolbar(MessageDock);
  944. Toolbar->Parent = MessageDock;
  945. Toolbar->FullSize = true;
  946. Toolbar->Color = Application->HintColor;
  947. TTBXLabelItem * LabelItem;
  948. LabelItem = new TTBXLabelItem(Toolbar);
  949. // Convert to explicit RGB "black" color, to make sure it is contrasting even in Dark theme
  950. // (TTBXOfficeXPTheme.GetStandardColor would convert clWindow to "white" in dark theme)
  951. LabelItem->FontSettings->Color = static_cast<TColor>(GetSysColor(clWindow));
  952. LabelItem->Margin = ScaleByTextHeight(MessageDock, 2); // Is this automatically rescaled?
  953. LabelItem->Caption = LoadStr(STORE_MIGRATION_THANKS);
  954. Toolbar->Items->Add(LabelItem);
  955. TTBXCustomItem * LinkItem = new TTBXItem(Toolbar);
  956. LinkItem->FontSettings->Color = LinkColor;
  957. LinkItem->Caption = LoadStr(STORE_MIGRATION_LEARN);
  958. LinkItem->OnClick = StoreTransitionLinkClick;
  959. Toolbar->Items->Add(LinkItem);
  960. AddMenuSeparator(Toolbar->Items);
  961. TTBXCustomItem * CloseItem = new TTBXItem(Toolbar);
  962. CloseItem->FontSettings->Color = static_cast<TColor>(GetSysColor(clWindow));
  963. CloseItem->Caption = LoadStr(MESSAGE_DISMISS);
  964. CloseItem->OnClick = StoreTransitionCloseClick;
  965. Toolbar->Items->Add(CloseItem);
  966. GiveTBItemPriority(CloseItem);
  967. }
  968. }
  969. //---------------------------------------------------------------------------
  970. void __fastcall TCustomScpExplorerForm::StoreTransitionLinkClick(TObject *)
  971. {
  972. ShowHelp(HELP_STORE_TRANSITION);
  973. }
  974. //---------------------------------------------------------------------------
  975. void __fastcall TCustomScpExplorerForm::StoreTransitionCloseClick(TObject * Sender)
  976. {
  977. TTBCustomItem * Item = DebugNotNull(dynamic_cast<TTBCustomItem *>(Sender));
  978. TTBXToolbar * Toolbar = DebugNotNull(dynamic_cast<TTBXToolbar *>(DebugNotNull(DebugNotNull(Item->Parent)->ParentComponent)));
  979. delete Toolbar;
  980. WinConfiguration->StoreTransition = stStoreAcknowledged;
  981. UpdateControls();
  982. }
  983. //---------------------------------------------------------------------------
  984. void __fastcall TCustomScpExplorerForm::UpdateTransferList()
  985. {
  986. TTBXStringList * TransferList = dynamic_cast<TTBXStringList*>(
  987. static_cast<TComponent*>(GetComponent(fcTransferList)));
  988. TTBXDropDownItem * TransferDropDown = dynamic_cast<TTBXDropDownItem*>(
  989. static_cast<TComponent*>(GetComponent(fcTransferDropDown)));
  990. TransferList->Strings->BeginUpdate();
  991. try
  992. {
  993. TransferList->Strings->Assign(GUIConfiguration->CopyParamList->NameList);
  994. TransferList->Strings->Insert(0, StripHotkey(LoadStr(COPY_PARAM_DEFAULT)));
  995. TransferList->ItemIndex = GUIConfiguration->CopyParamIndex + 1;
  996. if (FTransferDropDownHint.IsEmpty())
  997. {
  998. FTransferDropDownHint = TransferDropDown->Hint;
  999. }
  1000. // this way we get name for "default" settings (COPY_PARAM_DEFAULT)
  1001. UnicodeString Name = TransferList->Strings->Strings[TransferList->ItemIndex];
  1002. TransferDropDown->Text = StripHotkey(Name);
  1003. TransferDropDown->Hint = FORMAT(L"%s|%s:\n%s",
  1004. (FTransferDropDownHint, StripHotkey(Name),
  1005. GUIConfiguration->CurrentCopyParam.GetInfoStr(L"; ",
  1006. FLAGMASK(HasActiveTerminal(), Terminal->UsableCopyParamAttrs(0).General))));
  1007. // update the label, otherwise when it is updated only on the first draw
  1008. // of the list, it is drawn "bold" for some reason
  1009. FTransferListHoverIndex = TransferList->ItemIndex;
  1010. UpdateTransferLabel();
  1011. }
  1012. __finally
  1013. {
  1014. TransferList->Strings->EndUpdate();
  1015. }
  1016. }
  1017. //---------------------------------------------------------------------------
  1018. void __fastcall TCustomScpExplorerForm::UpdateCustomCommandsToolbar()
  1019. {
  1020. TTBXToolbar * Toolbar = dynamic_cast<TTBXToolbar *>(GetComponent(fcCustomCommandsBand));
  1021. DebugAssert(Toolbar != NULL);
  1022. NonVisualDataModule->UpdateCustomCommandsToolbar(Toolbar);
  1023. }
  1024. //---------------------------------------------------------------------------
  1025. void __fastcall TCustomScpExplorerForm::UpdateActions()
  1026. {
  1027. TForm::UpdateActions();
  1028. if (ComponentVisible[fcCustomCommandsBand])
  1029. {
  1030. UpdateCustomCommandsToolbar();
  1031. }
  1032. }
  1033. //---------------------------------------------------------------------------
  1034. void __fastcall TCustomScpExplorerForm::UpdateSessionsPageControlHeight()
  1035. {
  1036. SessionsPageControl->Height = SessionsPageControl->GetTabsHeight();
  1037. }
  1038. //---------------------------------------------------------------------------
  1039. void __fastcall TCustomScpExplorerForm::UpdateRowSelect(TCustomDirView * DirView)
  1040. {
  1041. if (DirView->RowSelect != WinConfiguration->FullRowSelect)
  1042. {
  1043. DirView->RowSelect = WinConfiguration->FullRowSelect;
  1044. // Without this, the panel turns gray and unused part of header turns black
  1045. DirView->Perform(CM_COLORCHANGED, 0, 0);
  1046. }
  1047. }
  1048. //---------------------------------------------------------------------------
  1049. void __fastcall TCustomScpExplorerForm::ConfigurationChanged()
  1050. {
  1051. Color = GetBtnFaceColor();
  1052. DebugAssert(Configuration && RemoteDirView);
  1053. RemoteDirView->DDAllowMove = !WinConfiguration->DDDisableMove;
  1054. RemoteDirView->DimmHiddenFiles = WinConfiguration->DimmHiddenFiles;
  1055. RemoteDirView->ShowHiddenFiles = WinConfiguration->ShowHiddenFiles;
  1056. RemoteDirView->FormatSizeBytes = WinConfiguration->FormatSizeBytes;
  1057. RemoteDirView->ShowInaccesibleDirectories = WinConfiguration->ShowInaccesibleDirectories;
  1058. RemoteDirView->NaturalOrderNumericalSorting = WinConfiguration->NaturalOrderNumericalSorting;
  1059. RemoteDirView->AlwaysSortDirectoriesByName = WinConfiguration->AlwaysSortDirectoriesByName;
  1060. UpdateRowSelect(RemoteDirView);
  1061. RemoteDriveView->DDAllowMove = !WinConfiguration->DDDisableMove;
  1062. RemoteDriveView->DimmHiddenDirs = WinConfiguration->DimmHiddenFiles;
  1063. RemoteDriveView->ShowHiddenDirs = WinConfiguration->ShowHiddenFiles;
  1064. RemoteDriveView->ShowInaccesibleDirectories = WinConfiguration->ShowInaccesibleDirectories;
  1065. RemoteDriveView->NaturalOrderNumericalSorting = WinConfiguration->NaturalOrderNumericalSorting;
  1066. UpdateSessionsPageControlHeight();
  1067. SetDockAllowDrag(!WinConfiguration->LockToolbars);
  1068. UpdateToolbarDisplayMode();
  1069. UpdateTransferList();
  1070. if (Terminal != NULL)
  1071. {
  1072. TransferPresetAutoSelect();
  1073. }
  1074. UpdateQueueView();
  1075. UpdateControls();
  1076. // this can be called even before constuctor finishes.
  1077. if (FEditorManager != NULL)
  1078. {
  1079. FEditorManager->ProcessFiles(FileConfigurationChanged, NULL);
  1080. }
  1081. // this can be called even before constuctor finishes.
  1082. if (FLocalEditors != NULL)
  1083. {
  1084. for (int Index = 0; Index < FLocalEditors->Count; Index++)
  1085. {
  1086. ReconfigureEditorForm(static_cast<TForm *>(FLocalEditors->Items[Index]));
  1087. }
  1088. }
  1089. if (ComponentVisible[fcCustomCommandsBand])
  1090. {
  1091. UpdateCustomCommandsToolbar();
  1092. }
  1093. // show only when keeping queue items forever,
  1094. // otherwise, it's enough to have it in the context menu
  1095. QueueDeleteAllDoneQueueToolbarItem->Visible =
  1096. WinConfiguration->QueueKeepDoneItems && (WinConfiguration->QueueKeepDoneItemsFor < 0);
  1097. if (FFileColorsCurrent != WinConfiguration->FileColors)
  1098. {
  1099. TFileColorData::LoadList(WinConfiguration->FileColors, FFileColors);
  1100. FFileColorsCurrent = WinConfiguration->FileColors;
  1101. FileColorsChanged();
  1102. }
  1103. }
  1104. //---------------------------------------------------------------------------
  1105. void __fastcall TCustomScpExplorerForm::DoFileColorsChanged(TCustomDirView * DirView)
  1106. {
  1107. DirView->OnGetItemColor = FFileColors.empty() ? TDirViewGetItemColorEvent(NULL) : TDirViewGetItemColorEvent(&DirViewGetItemColor);
  1108. DirView->Invalidate();
  1109. }
  1110. //---------------------------------------------------------------------------
  1111. void __fastcall TCustomScpExplorerForm::FileColorsChanged()
  1112. {
  1113. DoFileColorsChanged(RemoteDirView);
  1114. }
  1115. //---------------------------------------------------------------------------
  1116. void __fastcall TCustomScpExplorerForm::FileConfigurationChanged(
  1117. const UnicodeString FileName, TEditedFileData * /*Data*/, TObject * Token,
  1118. void * /*Arg*/)
  1119. {
  1120. if (Token != NULL)
  1121. {
  1122. TForm * Editor = dynamic_cast<TForm*>(Token);
  1123. DebugAssert(Editor != NULL);
  1124. ReconfigureEditorForm(Editor);
  1125. }
  1126. }
  1127. //---------------------------------------------------------------------------
  1128. void __fastcall TCustomScpExplorerForm::EnableDDTransferConfirmation(TObject * /*Sender*/)
  1129. {
  1130. WinConfiguration->DDTransferConfirmation = asOn;
  1131. }
  1132. //---------------------------------------------------------------------------
  1133. void __fastcall TCustomScpExplorerForm::CopyParamDialogAfter(
  1134. TTransferDirection /*Direction*/, bool /*Temp*/, const UnicodeString & /*TargetDirectory*/)
  1135. {
  1136. // noop
  1137. }
  1138. //---------------------------------------------------------------------------
  1139. bool TCustomScpExplorerForm::GetDoNotShowCopyDialogDefault(bool DragDrop)
  1140. {
  1141. return DragDrop && (WinConfiguration->DDTransferConfirmation == asAuto);
  1142. }
  1143. //---------------------------------------------------------------------------
  1144. void TCustomScpExplorerForm::HandleDoNotShowCopyDialogAgain(bool DragDrop, bool DoNotShowAgain)
  1145. {
  1146. if (DoNotShowAgain)
  1147. {
  1148. if (DragDrop)
  1149. {
  1150. if (WinConfiguration->DDTransferConfirmation == asAuto)
  1151. {
  1152. PopupTrayBalloon(
  1153. NULL, LoadStr(DD_TRANSFER_CONFIRM_OFF2), qtInformation, NULL, 0, EnableDDTransferConfirmation, NULL);
  1154. }
  1155. WinConfiguration->DDTransferConfirmation = asOff;
  1156. }
  1157. else
  1158. {
  1159. WinConfiguration->ConfirmTransferring = false;
  1160. }
  1161. }
  1162. else
  1163. {
  1164. // User explicitly unchecked "do not show again",
  1165. // so show him the dialog the next time
  1166. if (DragDrop && (WinConfiguration->DDTransferConfirmation == asAuto))
  1167. {
  1168. WinConfiguration->DDTransferConfirmation = asOn;
  1169. }
  1170. }
  1171. }
  1172. //---------------------------------------------------------------------------
  1173. bool __fastcall TCustomScpExplorerForm::CopyParamDialog(
  1174. TTransferDirection Direction, TTransferType Type, bool Temp,
  1175. TStrings * FileList, UnicodeString & TargetDirectory, TGUICopyParamType & CopyParam,
  1176. bool Confirm, bool DragDrop, int Options)
  1177. {
  1178. bool Result = true;
  1179. DebugAssert(HasActiveTerminal());
  1180. // these parameters are known in advance
  1181. int Params =
  1182. FLAGMASK(Type == ttMove, cpDelete);
  1183. bool ToTemp = (Temp && (Direction == tdToLocal));
  1184. if (Result && Confirm && WinConfiguration->ConfirmTransferring)
  1185. {
  1186. bool DisableNewerOnly =
  1187. (!Terminal->IsCapable[fcNewerOnlyUpload] && (Direction == tdToRemote)) ||
  1188. ToTemp;
  1189. Options |=
  1190. FLAGMASK(ToTemp, coTemp) |
  1191. FLAGMASK(!Terminal->IsCapable[fcBackgroundTransfers], coDisableQueue) |
  1192. coDoNotShowAgain;
  1193. TUsableCopyParamAttrs UsableCopyParamAttrs = Terminal->UsableCopyParamAttrs(Params);
  1194. int CopyParamAttrs = (Direction == tdToRemote ?
  1195. UsableCopyParamAttrs.Upload : UsableCopyParamAttrs.Download) |
  1196. FLAGMASK(DisableNewerOnly, cpaNoNewerOnly);
  1197. int OutputOptions = FLAGMASK(GetDoNotShowCopyDialogDefault(DragDrop), cooDoNotShowAgain);
  1198. std::unique_ptr<TSessionData> SessionData(SessionDataForCode());
  1199. FlashOnBackground(); // Particularly when called from ClipboardFakeCreated
  1200. Result = DoCopyDialog(Direction == tdToRemote, Type == ttMove,
  1201. FileList, TargetDirectory, &CopyParam, Options, CopyParamAttrs, SessionData.get(), &OutputOptions, -1);
  1202. if (Result)
  1203. {
  1204. HandleDoNotShowCopyDialogAgain(DragDrop, FLAGSET(OutputOptions, cooDoNotShowAgain));
  1205. CopyParamDialogAfter(Direction, Temp, TargetDirectory);
  1206. }
  1207. }
  1208. if (Result && CopyParam.Queue && !ToTemp && Terminal->IsCapable[fcBackgroundTransfers])
  1209. {
  1210. Configuration->Usage->Inc(L"TransfersOnBackground");
  1211. // these parameter are known only after transfer dialog
  1212. Params |=
  1213. (CopyParam.QueueNoConfirmation ? cpNoConfirmation : 0);
  1214. AddQueueItem(Queue, Direction, FileList, TargetDirectory, CopyParam, Params);
  1215. Result = false;
  1216. ClearTransferSourceSelection(Direction);
  1217. }
  1218. return Result;
  1219. }
  1220. //---------------------------------------------------------------------------
  1221. void TCustomScpExplorerForm::ClearOperationSelection(TOperationSide Side)
  1222. {
  1223. if (FOnFileOperationFinished != NULL)
  1224. {
  1225. FOnFileOperationFinished(Side, UnicodeString(), true);
  1226. }
  1227. else
  1228. {
  1229. if (HasDirView[Side])
  1230. {
  1231. DirView(Side)->SelectAll(smNone);
  1232. }
  1233. }
  1234. }
  1235. //---------------------------------------------------------------------------
  1236. void __fastcall TCustomScpExplorerForm::ClearTransferSourceSelection(TTransferDirection Direction)
  1237. {
  1238. TOperationSide Side = ((Direction == tdToRemote) ? osLocal : osRemote);
  1239. ClearOperationSelection(Side);
  1240. }
  1241. //---------------------------------------------------------------------------
  1242. void __fastcall TCustomScpExplorerForm::AddQueueItem(
  1243. TTerminalQueue * Queue, TTransferDirection Direction, TStrings * FileList,
  1244. const UnicodeString TargetDirectory, TGUICopyParamType & CopyParam,
  1245. int Params)
  1246. {
  1247. DebugAssert(Queue != NULL);
  1248. bool SingleFile = false;
  1249. if (FileList->Count == 1)
  1250. {
  1251. if (Direction == tdToRemote)
  1252. {
  1253. UnicodeString FileName = FileList->Strings[0];
  1254. SingleFile = FileExists(ApiPath(FileName));
  1255. }
  1256. else
  1257. {
  1258. TRemoteFile * File = static_cast<TRemoteFile *>(FileList->Objects[0]);
  1259. SingleFile = !File->IsDirectory;
  1260. }
  1261. }
  1262. TQueueItem * QueueItem;
  1263. if (Direction == tdToRemote)
  1264. {
  1265. CopyParam.IncludeFileMask.SetRoots(FileList, TargetDirectory);
  1266. QueueItem = new TUploadQueueItem(Terminal, FileList, TargetDirectory,
  1267. &CopyParam, Params, SingleFile, CopyParam.QueueParallel);
  1268. }
  1269. else
  1270. {
  1271. CopyParam.IncludeFileMask.SetRoots(TargetDirectory, FileList);
  1272. QueueItem = new TDownloadQueueItem(Terminal, FileList, TargetDirectory,
  1273. &CopyParam, Params, SingleFile, CopyParam.QueueParallel);
  1274. }
  1275. AddQueueItem(Queue, QueueItem, Terminal);
  1276. }
  1277. //---------------------------------------------------------------------------
  1278. void __fastcall TCustomScpExplorerForm::AddQueueItem(TTerminalQueue * Queue, TQueueItem * QueueItem, TManagedTerminal * Terminal)
  1279. {
  1280. if (Queue->IsEmpty)
  1281. {
  1282. Terminal->QueueOperationStart = Now();
  1283. }
  1284. Queue->AddItem(QueueItem);
  1285. }
  1286. //---------------------------------------------------------------------------
  1287. void __fastcall TCustomScpExplorerForm::RestoreFormParams()
  1288. {
  1289. }
  1290. //---------------------------------------------------------------------------
  1291. void __fastcall TCustomScpExplorerForm::InitControls()
  1292. {
  1293. DebugAssert(FDocks != NULL);
  1294. for (int Index = 0; Index < ComponentCount; Index++)
  1295. {
  1296. TTBDock * Dock = dynamic_cast<TTBDock *>(Components[Index]);
  1297. if ((Dock != NULL) && (Dock->Tag == 0))
  1298. {
  1299. FDocks->Add(Dock);
  1300. }
  1301. }
  1302. CollectItemsWithTextDisplayMode(this);
  1303. }
  1304. //---------------------------------------------------------------------------
  1305. void __fastcall TCustomScpExplorerForm::RestoreParams()
  1306. {
  1307. ConfigurationChanged();
  1308. QueuePanel->Height =
  1309. LoadDimension(WinConfiguration->QueueView.Height, WinConfiguration->QueueView.HeightPixelsPerInch, this);
  1310. LoadListViewStr(QueueView3, WinConfiguration->QueueView.Layout);
  1311. QueueDock->Visible = WinConfiguration->QueueView.ToolBar;
  1312. QueueLabel->Visible = WinConfiguration->QueueView.Label;
  1313. QueueFileList->Visible = WinConfiguration->QueueView.FileList;
  1314. QueueFileList->Height =
  1315. LoadDimension(WinConfiguration->QueueView.FileListHeight, WinConfiguration->QueueView.FileListHeightPixelsPerInch, this);
  1316. if (QueueFileList->Visible)
  1317. {
  1318. AdjustQueueLayout();
  1319. }
  1320. UpdateCustomCommandsToolbar();
  1321. }
  1322. //---------------------------------------------------------------------------
  1323. void __fastcall TCustomScpExplorerForm::StoreParams()
  1324. {
  1325. int APixelsPerInch = GetControlPixelsPerInch(this);
  1326. WinConfiguration->QueueView.Height = QueuePanel->Height;
  1327. WinConfiguration->QueueView.HeightPixelsPerInch = APixelsPerInch;
  1328. WinConfiguration->QueueView.Layout = GetListViewStr(QueueView3);
  1329. WinConfiguration->QueueView.ToolBar = QueueDock->Visible;
  1330. WinConfiguration->QueueView.Label = QueueLabel->Visible;
  1331. WinConfiguration->QueueView.FileList = QueueFileList->Visible;
  1332. WinConfiguration->QueueView.FileListHeight = QueueFileList->Height;
  1333. WinConfiguration->QueueView.FileListHeightPixelsPerInch = APixelsPerInch;
  1334. }
  1335. //---------------------------------------------------------------------------
  1336. void __fastcall TCustomScpExplorerForm::CreateParams(TCreateParams & Params)
  1337. {
  1338. if (!FFormRestored)
  1339. {
  1340. FFormRestored = true;
  1341. RestoreFormParams();
  1342. }
  1343. TForm::CreateParams(Params);
  1344. }
  1345. //---------------------------------------------------------------------------
  1346. void __fastcall TCustomScpExplorerForm::SetDockAllowDrag(bool value)
  1347. {
  1348. DebugAssert(FDocks != NULL);
  1349. for (int Index = 0; Index < FDocks->Count; Index++)
  1350. {
  1351. static_cast<TTBDock*>(FDocks->Items[Index])->AllowDrag = value;
  1352. }
  1353. }
  1354. //---------------------------------------------------------------------------
  1355. void __fastcall TCustomScpExplorerForm::LoadToolbarsLayoutStr(UnicodeString LayoutStr, UnicodeString ButtonsStr)
  1356. {
  1357. SetDockAllowDrag(true);
  1358. while (!ButtonsStr.IsEmpty())
  1359. {
  1360. UnicodeString S = CutToChar(ButtonsStr, L';', true);
  1361. UnicodeString Name = CutToChar(S, L'=', true);
  1362. for (int ToolbarIndex = 0; ToolbarIndex < ComponentCount; ToolbarIndex++)
  1363. {
  1364. TTBCustomToolbar * Toolbar = dynamic_cast<TTBCustomToolbar *>(Components[ToolbarIndex]);
  1365. if ((Toolbar != NULL) && SameText(Name, GetToolbarKey(Toolbar->Name)))
  1366. {
  1367. while (!S.IsEmpty())
  1368. {
  1369. UnicodeString S2 = CutToChar(S, L',', true);
  1370. UnicodeString Name = CutToChar(S2, L':', true);
  1371. for (int ItemIndex = 0; ItemIndex < Toolbar->Items->Count; ItemIndex++)
  1372. {
  1373. TTBCustomItem * Item = Toolbar->Items->Items[ItemIndex];
  1374. if (SameText(GetToolbarItemName(Item), Name))
  1375. {
  1376. // forward compatibility
  1377. UnicodeString S3 = CutToChar(S2, L':', true);
  1378. bool Visible = StrToIntDef(S3, 0);
  1379. Item->Visible = Visible;
  1380. }
  1381. }
  1382. }
  1383. }
  1384. }
  1385. }
  1386. ::LoadToolbarsLayoutStr(this, LayoutStr);
  1387. SetDockAllowDrag(!WinConfiguration->LockToolbars);
  1388. }
  1389. //---------------------------------------------------------------------------
  1390. UnicodeString __fastcall TCustomScpExplorerForm::GetToolbarsLayoutStr()
  1391. {
  1392. UnicodeString Result;
  1393. SetDockAllowDrag(true);
  1394. Result = ::GetToolbarsLayoutStr(this);
  1395. SetDockAllowDrag(!WinConfiguration->LockToolbars);
  1396. return Result;
  1397. }
  1398. //---------------------------------------------------------------------------
  1399. UnicodeString __fastcall TCustomScpExplorerForm::GetToolbarItemName(TTBCustomItem * Item)
  1400. {
  1401. UnicodeString Result;
  1402. if (Item->Action != NULL)
  1403. {
  1404. Result = Item->Action->Name;
  1405. Result = RemoveSuffix(Result, L"Action", true);
  1406. }
  1407. else
  1408. {
  1409. Result = Item->Name;
  1410. Result = RemoveSuffix(Result, L"SubmenuItem", true);
  1411. Result = RemoveSuffix(Result, L"Item", true);
  1412. }
  1413. return Result;
  1414. }
  1415. //---------------------------------------------------------------------------
  1416. UnicodeString __fastcall TCustomScpExplorerForm::GetToolbarsButtonsStr()
  1417. {
  1418. UnicodeString Result;
  1419. for (int ToolbarIndex = 0; ToolbarIndex < ComponentCount; ToolbarIndex++)
  1420. {
  1421. UnicodeString ToolbarStr;
  1422. TTBCustomToolbar * Toolbar = dynamic_cast<TTBCustomToolbar *>(Components[ToolbarIndex]);
  1423. if ((Toolbar != NULL) && (Toolbar != QueueToolbar))
  1424. {
  1425. for (int ItemIndex = 0; ItemIndex < Toolbar->Items->Count; ItemIndex++)
  1426. {
  1427. TTBCustomItem * Item = Toolbar->Items->Items[ItemIndex];
  1428. // Currently all buttons are visible by default, so we can safely remember all hidden buttons.
  1429. // Once we introduce any buttons that are hidden by default, we would have to remember their initial state
  1430. // and save the changes here only.
  1431. if (NonVisualDataModule->IsCustomizableToolbarItem(Item) && !Item->Visible)
  1432. {
  1433. UnicodeString Name = GetToolbarItemName(Item);
  1434. DebugAssert(Name.Pos(L"TBX") == 0);
  1435. AddToList(ToolbarStr, FORMAT(L"%s:0", (Name)), L",");
  1436. }
  1437. }
  1438. }
  1439. if (!ToolbarStr.IsEmpty())
  1440. {
  1441. AddToList(Result, FORMAT(L"%s=%s", (GetToolbarKey(Toolbar->Name), ToolbarStr)), L";");
  1442. }
  1443. }
  1444. return Result;
  1445. }
  1446. //---------------------------------------------------------------------------
  1447. void __fastcall TCustomScpExplorerForm::CreateProgressForm(TSynchronizeProgress * SynchronizeProgress)
  1448. {
  1449. DebugAssert(FProgressForm == NULL);
  1450. bool AllowSkip = HasActiveTerminal() ? Terminal->IsCapable[fcSkipTransfer] : false;
  1451. bool AllowMoveToQueue = (FTransferResumeList != NULL) || (FDeletedFiles != NULL);
  1452. FProgressForm = new TProgressForm(Application, AllowMoveToQueue, AllowSkip, SynchronizeProgress);
  1453. FProgressForm->DeleteLocalToRecycleBin =
  1454. (WinConfiguration->DeleteToRecycleBin != FAlternativeDelete);
  1455. FProgressForm->DeleteRemoteToRecycleBin =
  1456. HasActiveTerminal() &&
  1457. (Terminal->SessionData->DeleteToRecycleBin != FAlternativeDelete) &&
  1458. !Terminal->SessionData->RecycleBinPath.IsEmpty();
  1459. // As progress window has delayed show now, we need to lock ourselves, (at least) until then
  1460. LockWindow();
  1461. // Actually, do not know what hides the progress once the operation finishes
  1462. // (it is possibly the SetQueueProgress - and we should not rely on that)
  1463. SetTaskbarListProgressState(TBPF_NORMAL);
  1464. }
  1465. //---------------------------------------------------------------------------
  1466. void __fastcall TCustomScpExplorerForm::DestroyProgressForm()
  1467. {
  1468. SAFE_DESTROY(FProgressForm);
  1469. UnlockWindow();
  1470. SetQueueProgress();
  1471. }
  1472. //---------------------------------------------------------------------------
  1473. void __fastcall TCustomScpExplorerForm::FileOperationProgress(
  1474. TFileOperationProgressType & ProgressData)
  1475. {
  1476. // Download to temporary local directory
  1477. if (FPendingTempSpaceWarn && ProgressData.InProgress && ProgressData.TotalSizeSet)
  1478. {
  1479. bool Continue = true;
  1480. FPendingTempSpaceWarn = false;
  1481. DoWarnLackOfTempSpace(ProgressData.Directory, ProgressData.TotalSize, Continue);
  1482. if (!Continue)
  1483. {
  1484. Abort();
  1485. }
  1486. }
  1487. // operation is being executed and we still didn't show up progress form
  1488. if (ProgressData.InProgress && !FProgressForm)
  1489. {
  1490. CreateProgressForm(NULL);
  1491. DebugAssert(FProgressForm->OnceDoneOperation == odoIdle);
  1492. FProgressForm->OnceDoneOperation = ProgressData.InitialOnceDoneOperation;
  1493. }
  1494. // operation is finished (or terminated), so we hide progress form
  1495. else if (!ProgressData.InProgress && (FProgressForm != NULL) && (FProgressForm->SynchronizeProgress == NULL))
  1496. {
  1497. DestroyProgressForm();
  1498. if (ProgressData.Operation == foCalculateSize)
  1499. {
  1500. // When calculating size before transfer, the abort caused by
  1501. // cancel flag set due to "MoveToQueue" below,
  1502. // is not propagated back to us in ExecuteFileOperation,
  1503. // as it is silently swallowed.
  1504. // So we explicitly (re)throw it here.
  1505. if (FMoveToQueue)
  1506. {
  1507. Abort();
  1508. }
  1509. }
  1510. else
  1511. {
  1512. if ((ProgressData.Cancel == csContinue) &&
  1513. !FAutoOperation)
  1514. {
  1515. OperationComplete(ProgressData.StartTime);
  1516. }
  1517. }
  1518. }
  1519. if (FProgressForm &&
  1520. // Can happen only during synchronization, when ending one of the operations
  1521. ProgressData.InProgress)
  1522. {
  1523. FProgressForm->SetProgressData(ProgressData);
  1524. DebugAssert(ProgressData.InProgress);
  1525. SetTaskbarListProgressValue(&ProgressData);
  1526. if (FProgressForm->Cancel > csContinue)
  1527. {
  1528. ProgressData.SetCancelAtLeast(FProgressForm->Cancel);
  1529. if (FProgressForm->Cancel == csCancelFile)
  1530. {
  1531. FProgressForm->ClearCancel();
  1532. }
  1533. // cancel cancels even the move
  1534. FMoveToQueue = false;
  1535. }
  1536. else if (FProgressForm->MoveToQueue)
  1537. {
  1538. FMoveToQueue = true;
  1539. ProgressData.SetCancelAtLeast(csCancel);
  1540. }
  1541. if ((FTransferResumeList != NULL) &&
  1542. ProgressData.InProgress &&
  1543. ProgressData.IsTransfer() &&
  1544. !ProgressData.FullFileName.IsEmpty())
  1545. {
  1546. if ((FTransferResumeList->Count == 0) ||
  1547. (FTransferResumeList->Strings[FTransferResumeList->Count - 1] != ProgressData.FullFileName))
  1548. {
  1549. // note that we do not recognize directories from files here
  1550. FTransferResumeList->Add(ProgressData.FullFileName);
  1551. }
  1552. }
  1553. }
  1554. }
  1555. //---------------------------------------------------------------------------
  1556. void __fastcall TCustomScpExplorerForm::OperationComplete(
  1557. const TDateTime & StartTime)
  1558. {
  1559. if (GUIConfiguration->BeepOnFinish &&
  1560. (Now() - StartTime > GUIConfiguration->BeepOnFinishAfter))
  1561. {
  1562. UnicodeString BeepSound = GUIConfiguration->BeepSound;
  1563. DWORD Sound;
  1564. if (!ExtractFileExt(BeepSound).IsEmpty())
  1565. {
  1566. Sound = SND_FILENAME;
  1567. }
  1568. else
  1569. {
  1570. Sound = SND_ALIAS;
  1571. }
  1572. PlaySound(BeepSound.c_str(), NULL, Sound | SND_ASYNC);
  1573. }
  1574. }
  1575. //---------------------------------------------------------------------------
  1576. void __fastcall TCustomScpExplorerForm::OperationProgress(
  1577. TFileOperationProgressType & ProgressData)
  1578. {
  1579. // For example, when calling TFileOperationProgressType::Suspend in TTerminal::QueryReopen,
  1580. // we cannot recurse back to TTerminal::Idle as that may detect another error, calling "suspend" again.
  1581. TAutoNestingCounter Counter(FDoNotIdleCurrentTerminal);
  1582. FileOperationProgress(ProgressData);
  1583. }
  1584. //---------------------------------------------------------------------------
  1585. UnicodeString __fastcall TCustomScpExplorerForm::GetProgressTitle()
  1586. {
  1587. UnicodeString Result;
  1588. if (FProgressForm != NULL)
  1589. {
  1590. Result = FProgressForm->ProgressStr();
  1591. }
  1592. return Result;
  1593. }
  1594. //---------------------------------------------------------------------------
  1595. bool __fastcall TCustomScpExplorerForm::PanelOperation(TOperationSide /*Side*/,
  1596. bool DragDrop)
  1597. {
  1598. return (!DragDrop && (DropSourceControl == NULL)) ||
  1599. (DropSourceControl == DirView(osOther));
  1600. }
  1601. //---------------------------------------------------------------------------
  1602. TListItem * TCustomScpExplorerForm::VisualiseOperationFinished(TOperationSide Side, const UnicodeString & FileName, bool Unselect)
  1603. {
  1604. TCustomDirView * ADirView = DirView(Side);
  1605. UnicodeString FileNameOnly = ExtractFileName(FileName, !IsSideLocalBrowser(Side));
  1606. TListItem * Item = ADirView->FindFileItem(FileNameOnly);
  1607. // this can happen when local drive is unplugged in the middle of the operation
  1608. if (Item != NULL)
  1609. {
  1610. if (Unselect)
  1611. {
  1612. Item->Selected = false;
  1613. }
  1614. ADirView->MakeProgressVisible(Item);
  1615. }
  1616. return Item;
  1617. }
  1618. //---------------------------------------------------------------------------
  1619. void __fastcall TCustomScpExplorerForm::DoOperationFinished(
  1620. TFileOperation Operation, TOperationSide Side,
  1621. bool /*Temp*/, const UnicodeString & FileName, bool Success,
  1622. TOnceDoneOperation & OnceDoneOperation)
  1623. {
  1624. if (!FAutoOperation)
  1625. {
  1626. // no selection on "/upload", form serves only as event handler
  1627. // (it is not displayed)
  1628. if (PanelOperation(Side, FDragDropOperation) && Visible &&
  1629. ((Operation == FPrimaryOperation) ||
  1630. ((Operation != foCalculateSize) && (Operation != foGetProperties) && (Operation != foCalculateChecksum))))
  1631. {
  1632. TOperationSide DViewSide;
  1633. if ((FProgressSide != osCurrent) &&
  1634. IsLocalBrowserMode() && (Side == osLocal)) // Only to limit the impact
  1635. {
  1636. // assume the operation is over the focused panel
  1637. DViewSide = FProgressSide;
  1638. }
  1639. else
  1640. {
  1641. DViewSide = Side;
  1642. }
  1643. if (FOnFileOperationFinished != NULL)
  1644. {
  1645. FOnFileOperationFinished(DViewSide, FileName, Success);
  1646. }
  1647. else
  1648. {
  1649. VisualiseOperationFinished(DViewSide, FileName, Success);
  1650. }
  1651. }
  1652. if (TFileOperationProgressType::IsTransferOperation(Operation))
  1653. {
  1654. if (DebugAlwaysTrue(!IsLocalBrowserMode()))
  1655. {
  1656. if (Side == osLocal)
  1657. {
  1658. Configuration->Usage->Inc(L"UploadedFiles");
  1659. }
  1660. else
  1661. {
  1662. Configuration->Usage->Inc(L"DownloadedFiles");
  1663. }
  1664. }
  1665. }
  1666. }
  1667. if (Success && (FSynchronizeController != NULL))
  1668. {
  1669. DebugAssert(!IsSideLocalBrowser(osRemote));
  1670. if (Operation == foCopy)
  1671. {
  1672. DebugAssert(Side == osLocal);
  1673. FSynchronizeController->LogOperation(soUpload, FileName);
  1674. }
  1675. else if (Operation == foDelete)
  1676. {
  1677. DebugAssert(Side == osRemote);
  1678. FSynchronizeController->LogOperation(soDelete, FileName);
  1679. }
  1680. }
  1681. if (FProgressForm)
  1682. {
  1683. OnceDoneOperation = FProgressForm->OnceDoneOperation;
  1684. }
  1685. }
  1686. //---------------------------------------------------------------------------
  1687. void __fastcall TCustomScpExplorerForm::OperationFinished(
  1688. TFileOperation Operation, TOperationSide Side,
  1689. bool Temp, const UnicodeString & FileName, Boolean Success,
  1690. TOnceDoneOperation & OnceDoneOperation)
  1691. {
  1692. TAutoNestingCounter Counter(FDoNotIdleCurrentTerminal);
  1693. DoOperationFinished(Operation, Side, Temp, FileName, Success,
  1694. OnceDoneOperation);
  1695. }
  1696. //---------------------------------------------------------------------------
  1697. bool TCustomScpExplorerForm::IsLocalBrowserMode()
  1698. {
  1699. return false;
  1700. }
  1701. //---------------------------------------------------------------------------
  1702. bool TCustomScpExplorerForm::SupportsLocalBrowser()
  1703. {
  1704. return false;
  1705. }
  1706. //---------------------------------------------------------------------------
  1707. bool TCustomScpExplorerForm::IsSideLocalBrowser(TOperationSide)
  1708. {
  1709. return false;
  1710. }
  1711. //---------------------------------------------------------------------------
  1712. TCustomDirView * TCustomScpExplorerForm::GetCurrentLocalBrowser()
  1713. {
  1714. return NULL;
  1715. }
  1716. //---------------------------------------------------------------------------
  1717. TCustomDirView * __fastcall TCustomScpExplorerForm::DirView(TOperationSide Side)
  1718. {
  1719. DebugAssert(GetSide(Side) == osRemote);
  1720. DebugUsedParam(Side);
  1721. return RemoteDirView;
  1722. }
  1723. //---------------------------------------------------------------------------
  1724. TCustomDriveView * __fastcall TCustomScpExplorerForm::DriveView(TOperationSide Side)
  1725. {
  1726. DebugAssert(GetSide(Side) == osRemote);
  1727. DebugUsedParam(Side);
  1728. return RemoteDriveView;
  1729. }
  1730. //---------------------------------------------------------------------------
  1731. bool __fastcall TCustomScpExplorerForm::DirViewEnabled(TOperationSide Side)
  1732. {
  1733. DebugAssert(GetSide(Side) == osRemote);
  1734. DebugUsedParam(Side);
  1735. return HasActiveTerminal();
  1736. }
  1737. //---------------------------------------------------------------------------
  1738. bool __fastcall TCustomScpExplorerForm::GetEnableFocusedOperation(
  1739. TOperationSide Side, int FilesOnly)
  1740. {
  1741. return DirView(Side)->AnyFileSelected(true, (FilesOnly != 0), true);
  1742. }
  1743. //---------------------------------------------------------------------------
  1744. bool __fastcall TCustomScpExplorerForm::GetEnableSelectedOperation(
  1745. TOperationSide Side, int FilesOnly)
  1746. {
  1747. return DirView(Side)->AnyFileSelected(false, (FilesOnly != 0), true);
  1748. }
  1749. //---------------------------------------------------------------------------
  1750. struct THistoryItemData
  1751. {
  1752. short int Side;
  1753. short int Index;
  1754. };
  1755. //---------------------------------------------------------------------------
  1756. void __fastcall TCustomScpExplorerForm::HistoryGo(TOperationSide Side, int Index)
  1757. {
  1758. DirView(Side)->HistoryGo(Index);
  1759. }
  1760. //---------------------------------------------------------------------------
  1761. void __fastcall TCustomScpExplorerForm::HistoryItemClick(System::TObject* Sender)
  1762. {
  1763. TTBCustomItem * Item = dynamic_cast<TTBCustomItem *>(Sender);
  1764. THistoryItemData Data = *reinterpret_cast<THistoryItemData*>(&(Item->Tag));
  1765. HistoryGo((TOperationSide)Data.Side, Data.Index);
  1766. }
  1767. //---------------------------------------------------------------------------
  1768. void __fastcall TCustomScpExplorerForm::UpdateHistoryMenu(TOperationSide Side,
  1769. bool Back)
  1770. {
  1771. if (FHistoryMenu[Side == osLocal][Back] != NULL)
  1772. {
  1773. TCustomDirView * DView = DirView(Side);
  1774. TTBXPopupMenu * Menu = FHistoryMenu[Side == osLocal][Back];
  1775. int ICount = Back ? DView->BackCount : DView->ForwardCount;
  1776. if (ICount > 10)
  1777. {
  1778. ICount = 10;
  1779. }
  1780. Menu->Items->Clear();
  1781. THistoryItemData Data;
  1782. Data.Side = Side;
  1783. for (short int i = 1; i <= ICount; i++)
  1784. {
  1785. TTBCustomItem * Item = new TTBXItem(Menu);
  1786. Data.Index = static_cast<short int>(i * (Back ? -1 : 1));
  1787. Item->Caption = MinimizeName(DView->HistoryPath[Data.Index], 50, !IsSideLocalBrowser(Side));
  1788. Item->Hint = DView->HistoryPath[Data.Index];
  1789. DebugAssert(sizeof(int) == sizeof(THistoryItemData));
  1790. Item->Tag = *reinterpret_cast<int*>(&Data);
  1791. Item->OnClick = HistoryItemClick;
  1792. Menu->Items->Add(Item);
  1793. }
  1794. }
  1795. }
  1796. //---------------------------------------------------------------------------
  1797. TTBXPopupMenu * __fastcall TCustomScpExplorerForm::HistoryMenu(
  1798. TOperationSide Side, bool Back)
  1799. {
  1800. if (FHistoryMenu[Side == osLocal][Back] == NULL)
  1801. {
  1802. // workaround
  1803. // In Pascal the size of TTBXPopupMenu is 132, in C++ 136,
  1804. // operator new allocates memory in Pascal code, but calls inline
  1805. // contructor in C++, leading in problems, the function does
  1806. // both in Pascal code
  1807. FHistoryMenu[Side == osLocal][Back] = CreateTBXPopupMenu(this);
  1808. UpdateHistoryMenu(Side, Back);
  1809. }
  1810. return FHistoryMenu[Side == osLocal][Back];
  1811. }
  1812. //---------------------------------------------------------------------------
  1813. void __fastcall TCustomScpExplorerForm::DirViewHistoryChange(
  1814. TCustomDirView *Sender)
  1815. {
  1816. TOperationSide Side = (Sender == DirView(osOther) ? osOther : osLocal);
  1817. UpdateHistoryMenu(Side, true);
  1818. UpdateHistoryMenu(Side, false);
  1819. }
  1820. //---------------------------------------------------------------------------
  1821. bool __fastcall TCustomScpExplorerForm::CustomCommandRemoteAllowed()
  1822. {
  1823. // remote custom commands can be executed only if the server supports shell commands
  1824. // or have secondary shell
  1825. return HasActiveTerminal() && (Terminal->IsCapable[fcSecondaryShell] || Terminal->IsCapable[fcShellAnyCommand]);
  1826. }
  1827. //---------------------------------------------------------------------------
  1828. int __fastcall TCustomScpExplorerForm::CustomCommandState(
  1829. const TCustomCommandType & Command, bool OnFocused, TCustomCommandListType ListType)
  1830. {
  1831. // -1 = hidden, 0 = disabled, 1 = enabled
  1832. int Result;
  1833. TFileCustomCommand RemoteCustomCommand;
  1834. TLocalCustomCommand LocalCustomCommand;
  1835. TFileCustomCommand * NonInteractiveCustomCommand =
  1836. FLAGCLEAR(Command.Params, ccLocal) ? &RemoteCustomCommand : &LocalCustomCommand;
  1837. TInteractiveCustomCommand InteractiveCustomCommand(NonInteractiveCustomCommand);
  1838. UnicodeString Cmd = InteractiveCustomCommand.Complete(Command.Command, false);
  1839. if (FLAGCLEAR(Command.Params, ccLocal))
  1840. {
  1841. int AllowedState = CustomCommandRemoteAllowed() ? 1 : 0;
  1842. // custom command that does not operate with files can be executed anytime ...
  1843. if (!NonInteractiveCustomCommand->IsFileCommand(Cmd))
  1844. {
  1845. // ... but do not show such command in remote file menu
  1846. if ((ListType == ccltAll) || (ListType == ccltNonFile))
  1847. {
  1848. Result = AllowedState;
  1849. }
  1850. else
  1851. {
  1852. Result = -1;
  1853. }
  1854. }
  1855. else
  1856. {
  1857. if ((ListType == ccltAll) || ((ListType == ccltFile) && !IsSideLocalBrowser(FCurrentSide)))
  1858. {
  1859. Result = (DirView(FCurrentSide)->AnyFileSelected(OnFocused, false, true)) ? AllowedState : 0;
  1860. }
  1861. else
  1862. {
  1863. Result = -1;
  1864. }
  1865. }
  1866. }
  1867. else
  1868. {
  1869. // custom command that does not operate with files can be executed anytime
  1870. if (!NonInteractiveCustomCommand->IsFileCommand(Cmd))
  1871. {
  1872. Result = ((ListType == ccltAll) || (ListType == ccltNonFile)) ? 1 : -1;
  1873. }
  1874. else if (LocalCustomCommand.HasLocalFileName(Cmd))
  1875. {
  1876. if ((ListType == ccltAll) || (ListType == ccltFile))
  1877. {
  1878. // special case is "diff"-style command that can be executed over any side,
  1879. // if we have both sides
  1880. Result =
  1881. // Cannot have focus on both panels, so we have to call AnyFileSelected
  1882. // directly (instead of EnableSelectedOperation) to pass
  1883. // false to FocusedFileOnlyWhenFocused when panel is inactive.
  1884. ((HasDirView[osLocal] && !IsSideLocalBrowser(osRemote) &&
  1885. DirView(osLocal)->AnyFileSelected(false, false, (FCurrentSide == osLocal))) &&
  1886. DirView(osRemote)->AnyFileSelected(false, false, (FCurrentSide == osRemote))) ? 1 : 0;
  1887. }
  1888. else if (ListType == ccltBoth)
  1889. {
  1890. DebugAssert(!IsLocalBrowserMode());
  1891. Result = 1;
  1892. }
  1893. else
  1894. {
  1895. Result = -1;
  1896. }
  1897. }
  1898. else
  1899. {
  1900. if ((ListType == ccltAll) ||
  1901. ((ListType == ccltFile) && (!IsSideLocalBrowser(FCurrentSide) || FLAGCLEAR(Command.Params, ccRemoteFiles))))
  1902. {
  1903. Result = DirView(FCurrentSide)->AnyFileSelected(OnFocused, false, true) ? 1 : 0;
  1904. }
  1905. else
  1906. {
  1907. Result = -1;
  1908. }
  1909. }
  1910. if ((Result > 0) &&
  1911. LocalCustomCommand.IsSessionCommand(Cmd) &&
  1912. !HasActiveTerminal())
  1913. {
  1914. Result = 0;
  1915. }
  1916. }
  1917. return Result;
  1918. }
  1919. //---------------------------------------------------------------------------
  1920. void __fastcall TCustomScpExplorerForm::RemoteCustomCommand(
  1921. TStrings * FileList, const TCustomCommandType & ACommand,
  1922. const TCustomCommandData & Data, const UnicodeString & CommandCommand)
  1923. {
  1924. if (EnsureCommandSessionFallback(fcShellAnyCommand))
  1925. {
  1926. TRemoteCustomCommand RemoteCustomCommand(Data, Terminal->CurrentDirectory);
  1927. TWinInteractiveCustomCommand InteractiveCustomCommand(
  1928. &RemoteCustomCommand, ACommand.Name, ACommand.HomePage);
  1929. UnicodeString Command = InteractiveCustomCommand.Complete(CommandCommand, false);
  1930. Configuration->Usage->Inc(L"RemoteCustomCommandRuns2");
  1931. bool Capture =
  1932. FLAGSET(ACommand.Params, ccShowResults) ||
  1933. FLAGSET(ACommand.Params, ccCopyResults) ||
  1934. FLAGSET(ACommand.Params, ccShowResultsInMsgBox);
  1935. TCaptureOutputEvent OutputEvent = NULL;
  1936. DebugAssert(FCapturedLog == NULL);
  1937. if (Capture)
  1938. {
  1939. FCapturedLog = new TStringList();
  1940. OutputEvent = TerminalCaptureLog;
  1941. }
  1942. try
  1943. {
  1944. if (!RemoteCustomCommand.IsFileCommand(Command))
  1945. {
  1946. Terminal->AnyCommand(RemoteCustomCommand.Complete(Command, true),
  1947. OutputEvent);
  1948. }
  1949. else
  1950. {
  1951. Terminal->CustomCommandOnFiles(Command, ACommand.Params, FileList, OutputEvent);
  1952. }
  1953. if ((FCapturedLog != NULL) && (FCapturedLog->Count > 0))
  1954. {
  1955. if (FLAGSET(ACommand.Params, ccCopyResults))
  1956. {
  1957. CopyToClipboard(FCapturedLog);
  1958. }
  1959. if (FLAGSET(ACommand.Params, ccShowResults))
  1960. {
  1961. DoConsoleDialog(Terminal, L"", FCapturedLog);
  1962. }
  1963. else if (FLAGSET(ACommand.Params, ccShowResultsInMsgBox))
  1964. {
  1965. MessageDialog(FCapturedLog->Text, qtInformation, qaOK);
  1966. }
  1967. }
  1968. }
  1969. __finally
  1970. {
  1971. SAFE_DESTROY(FCapturedLog);
  1972. }
  1973. }
  1974. }
  1975. //---------------------------------------------------------------------------
  1976. void __fastcall TCustomScpExplorerForm::LocalCustomCommandPure(
  1977. TStrings * FileList, const TCustomCommandType & ACommand, const UnicodeString & Command, TStrings * ALocalFileList,
  1978. const TCustomCommandData & Data, bool LocalFileCommand, bool FileListCommand, UnicodeString * POutput)
  1979. {
  1980. DebugAssert(!IsLocalBrowserMode());
  1981. TStrings * LocalFileList = NULL;
  1982. TStrings * RemoteFileList = NULL;
  1983. TStrings * RemoteFileListFull = NULL;
  1984. try
  1985. {
  1986. if (LocalFileCommand)
  1987. {
  1988. if (ALocalFileList == NULL)
  1989. {
  1990. DebugAssert(HasDirView[osLocal]);
  1991. // Cannot have focus on both panels, so we have to call AnyFileSelected
  1992. // directly (instead of EnableSelectedOperation) to pass
  1993. // false to FocusedFileOnlyWhenFocused
  1994. DebugAssert(DirView(osLocal)->AnyFileSelected(false, false, false));
  1995. LocalFileList = DirView(osLocal)->CreateFileList(false, true, NULL);
  1996. }
  1997. else
  1998. {
  1999. LocalFileList = ALocalFileList;
  2000. }
  2001. if (FileListCommand)
  2002. {
  2003. if (LocalFileList->Count != 1)
  2004. {
  2005. throw Exception(LoadStr(CUSTOM_COMMAND_SELECTED_UNMATCH1));
  2006. }
  2007. }
  2008. else
  2009. {
  2010. if ((LocalFileList->Count != 1) &&
  2011. (FileList->Count != 1) &&
  2012. (LocalFileList->Count != FileList->Count))
  2013. {
  2014. throw Exception(LoadStr(CUSTOM_COMMAND_SELECTED_UNMATCH));
  2015. }
  2016. }
  2017. }
  2018. UnicodeString RootTempDir;
  2019. UnicodeString TempDir;
  2020. bool RemoteFiles = FLAGSET(ACommand.Params, ccRemoteFiles);
  2021. if (!RemoteFiles)
  2022. {
  2023. TemporarilyDownloadFiles(FileList, false, RootTempDir, TempDir, false, true, true);
  2024. }
  2025. try
  2026. {
  2027. TDateTimes RemoteFileTimes;
  2028. if (RemoteFiles)
  2029. {
  2030. RemoteFileList = FileList;
  2031. }
  2032. else
  2033. {
  2034. RemoteFileList = new TStringList();
  2035. TMakeLocalFileListParams MakeFileListParam;
  2036. MakeFileListParam.FileList = RemoteFileList;
  2037. MakeFileListParam.FileTimes = &RemoteFileTimes;
  2038. MakeFileListParam.IncludeDirs = FLAGSET(ACommand.Params, ccApplyToDirectories);
  2039. MakeFileListParam.Recursive =
  2040. FLAGSET(ACommand.Params, ccRecursive) && !FileListCommand;
  2041. ProcessLocalDirectory(TempDir, Terminal->MakeLocalFileList, &MakeFileListParam);
  2042. if (!MakeFileListParam.Recursive)
  2043. {
  2044. RemoteFileTimes.clear();
  2045. RemoteFileListFull = new TStringList();
  2046. MakeFileListParam.FileList = RemoteFileListFull;
  2047. MakeFileListParam.Recursive = true;
  2048. MakeFileListParam.IncludeDirs = false;
  2049. ProcessLocalDirectory(TempDir, Terminal->MakeLocalFileList, &MakeFileListParam);
  2050. }
  2051. else
  2052. {
  2053. RemoteFileListFull = RemoteFileList;
  2054. }
  2055. }
  2056. bool NonBlocking = FileListCommand && RemoteFiles && !POutput;
  2057. TFileOperationProgressType Progress(&OperationProgress, &OperationFinished);
  2058. if (!NonBlocking)
  2059. {
  2060. int Count;
  2061. if (FileListCommand)
  2062. {
  2063. Count = 1;
  2064. }
  2065. else if (LocalFileCommand)
  2066. {
  2067. Count = std::max(LocalFileList->Count, RemoteFileList->Count);
  2068. }
  2069. else
  2070. {
  2071. Count = RemoteFileList->Count;
  2072. }
  2073. Progress.Start(foCustomCommand, osRemote, Count);
  2074. DebugAssert(FProgressForm != NULL);
  2075. FProgressForm->ReadOnly = true;
  2076. }
  2077. try
  2078. {
  2079. if (FileListCommand)
  2080. {
  2081. UnicodeString LocalFile;
  2082. // MakeFileList does not delimit filenames
  2083. UnicodeString FileList = MakeFileList(RemoteFileList);
  2084. if (LocalFileCommand)
  2085. {
  2086. DebugAssert(LocalFileList->Count == 1);
  2087. LocalFile = LocalFileList->Strings[0];
  2088. }
  2089. TLocalCustomCommand CustomCommand(Data,
  2090. Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(), L"", LocalFile, FileList);
  2091. UnicodeString ShellCommand = CustomCommand.Complete(Command, true);
  2092. if (NonBlocking)
  2093. {
  2094. DebugAssert(!POutput);
  2095. ExecuteShellChecked(ShellCommand);
  2096. }
  2097. else
  2098. {
  2099. ExecuteProcessCheckedAndWait(ShellCommand, HelpKeyword, POutput);
  2100. }
  2101. }
  2102. else if (LocalFileCommand)
  2103. {
  2104. if (LocalFileList->Count == 1)
  2105. {
  2106. UnicodeString LocalFile = LocalFileList->Strings[0];
  2107. for (int Index = 0; Index < RemoteFileList->Count; Index++)
  2108. {
  2109. UnicodeString FileName = RemoteFileList->Strings[Index];
  2110. UnicodeString FileNameForProgress = RemoteFiles ? FileName : ExtractFileName(FileName);
  2111. Progress.SetFile(FileNameForProgress);
  2112. TLocalCustomCommand CustomCommand(Data,
  2113. Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(), FileName, LocalFile, L"");
  2114. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  2115. TOnceDoneOperation OnceDoneOperation;
  2116. // Do not unselect anything, as it won't work with two panels anyway
  2117. Progress.Finish(UnicodeString(), true, OnceDoneOperation);
  2118. }
  2119. }
  2120. else if (RemoteFileList->Count == 1)
  2121. {
  2122. UnicodeString FileName = RemoteFileList->Strings[0];
  2123. for (int Index = 0; Index < LocalFileList->Count; Index++)
  2124. {
  2125. UnicodeString LocalFileName = LocalFileList->Strings[Index];
  2126. UnicodeString LocalFileNameOnly = ExtractFileName(LocalFileName);
  2127. Progress.SetFile(LocalFileNameOnly);
  2128. TLocalCustomCommand CustomCommand(
  2129. Data, Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(),
  2130. FileName, LocalFileName, L"");
  2131. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  2132. TOnceDoneOperation OnceDoneOperation;
  2133. // Do not unselect anything, as it won't work with two panels anyway
  2134. Progress.Finish(UnicodeString(), true, OnceDoneOperation);
  2135. }
  2136. }
  2137. else
  2138. {
  2139. if (LocalFileList->Count != RemoteFileList->Count)
  2140. {
  2141. throw Exception(LoadStr(CUSTOM_COMMAND_PAIRS_DOWNLOAD_FAILED));
  2142. }
  2143. for (int Index = 0; Index < LocalFileList->Count; Index++)
  2144. {
  2145. UnicodeString FileName = RemoteFileList->Strings[Index];
  2146. UnicodeString LocalFileName = LocalFileList->Strings[Index];
  2147. UnicodeString LocalFileNameOnly = ExtractFileName(LocalFileName);
  2148. Progress.SetFile(LocalFileNameOnly); // sic
  2149. TLocalCustomCommand CustomCommand(
  2150. Data, Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(),
  2151. FileName, LocalFileName, L"");
  2152. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  2153. TOnceDoneOperation OnceDoneOperation;
  2154. // Do not unselect anything, as it won't work with two panels anyway
  2155. Progress.Finish(UnicodeString(), true, OnceDoneOperation);
  2156. }
  2157. }
  2158. }
  2159. else
  2160. {
  2161. for (int Index = 0; Index < RemoteFileList->Count; Index++)
  2162. {
  2163. UnicodeString FileName = RemoteFileList->Strings[Index];
  2164. UnicodeString FileNameForProgress = RemoteFiles ? FileName : ExtractFileName(FileName);
  2165. Progress.SetFile(FileNameForProgress);
  2166. TLocalCustomCommand CustomCommand(Data,
  2167. Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(),
  2168. FileName, L"", L"");
  2169. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  2170. TOnceDoneOperation OnceDoneOperation;
  2171. Progress.Finish(FileNameForProgress, true, OnceDoneOperation);
  2172. }
  2173. }
  2174. }
  2175. __finally
  2176. {
  2177. if (!NonBlocking)
  2178. {
  2179. Progress.Stop();
  2180. }
  2181. }
  2182. DebugAssert(!FAutoOperation);
  2183. if (!RemoteFiles)
  2184. {
  2185. TempDir = IncludeTrailingBackslash(TempDir);
  2186. for (int Index = 0; Index < RemoteFileListFull->Count; Index++)
  2187. {
  2188. UnicodeString FileName = RemoteFileListFull->Strings[Index];
  2189. if (DebugAlwaysTrue(SameText(TempDir, FileName.SubString(1, TempDir.Length()))) &&
  2190. // Skip directories, process only nested files.
  2191. // The check is redundant as FileAge fails for directories anyway.
  2192. !DirectoryExists(FileName))
  2193. {
  2194. UnicodeString RemoteDir =
  2195. UnixExtractFileDir(
  2196. UnixIncludeTrailingBackslash(Terminal->CurrentDirectory) +
  2197. ToUnixPath(FileName.SubString(TempDir.Length() + 1, FileName.Length() - TempDir.Length())));
  2198. TDateTime NewTime;
  2199. if (FileAge(FileName, NewTime) &&
  2200. (NewTime != RemoteFileTimes[Index]))
  2201. {
  2202. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  2203. TemporaryFileCopyParam(CopyParam);
  2204. CopyParam.FileMask = L"";
  2205. FAutoOperation = true;
  2206. std::unique_ptr<TStrings> TemporaryFilesList(new TStringList());
  2207. TemporaryFilesList->Add(FileName);
  2208. Terminal->CopyToRemote(TemporaryFilesList.get(), RemoteDir, &CopyParam, cpTemporary, NULL);
  2209. }
  2210. }
  2211. }
  2212. }
  2213. }
  2214. __finally
  2215. {
  2216. FAutoOperation = false;
  2217. if (!RootTempDir.IsEmpty() && DebugAlwaysTrue(!RemoteFiles))
  2218. {
  2219. RecursiveDeleteFile(ExcludeTrailingBackslash(RootTempDir));
  2220. }
  2221. }
  2222. }
  2223. __finally
  2224. {
  2225. if (RemoteFileList != FileList)
  2226. {
  2227. delete RemoteFileList;
  2228. }
  2229. if (LocalFileList != ALocalFileList)
  2230. {
  2231. delete LocalFileList;
  2232. }
  2233. if (RemoteFileListFull != RemoteFileList)
  2234. {
  2235. delete RemoteFileListFull;
  2236. }
  2237. }
  2238. }
  2239. //---------------------------------------------------------------------------
  2240. void __fastcall TCustomScpExplorerForm::LocalCustomCommandWithLocalFiles(
  2241. const TCustomCommandType & ACommand, const UnicodeString & Command, const TCustomCommandData & Data,
  2242. bool FileListCommand, UnicodeString * POutput)
  2243. {
  2244. std::unique_ptr<TStrings> SelectedFileList(DirView(osCurrent)->CreateFileList(false, true, NULL));
  2245. std::unique_ptr<TStrings> LocalFileList(new TStringList());
  2246. for (int Index = 0; Index < SelectedFileList->Count; Index++)
  2247. {
  2248. UnicodeString FileName = SelectedFileList->Strings[Index];
  2249. if (DirectoryExists(FileName))
  2250. {
  2251. if (FLAGSET(ACommand.Params, ccApplyToDirectories))
  2252. {
  2253. LocalFileList->Add(FileName);
  2254. }
  2255. if (FLAGSET(ACommand.Params, ccRecursive))
  2256. {
  2257. TMakeLocalFileListParams MakeFileListParam;
  2258. MakeFileListParam.FileList = LocalFileList.get();
  2259. MakeFileListParam.FileTimes = NULL;
  2260. MakeFileListParam.IncludeDirs = FLAGSET(ACommand.Params, ccApplyToDirectories);
  2261. MakeFileListParam.Recursive = true;
  2262. ProcessLocalDirectory(FileName, Terminal->MakeLocalFileList, &MakeFileListParam);
  2263. }
  2264. }
  2265. else
  2266. {
  2267. LocalFileList->Add(FileName);
  2268. }
  2269. }
  2270. UnicodeString RemotePath;
  2271. if (Terminal != NULL)
  2272. {
  2273. RemotePath = Terminal->CurrentDirectory;
  2274. }
  2275. if (FileListCommand)
  2276. {
  2277. UnicodeString FileList = MakeFileList(LocalFileList.get());
  2278. TLocalCustomCommand CustomCommand(
  2279. Data, RemotePath, DefaultDownloadTargetDirectory(),
  2280. L"", L"", FileList);
  2281. ExecuteProcessChecked(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  2282. }
  2283. else
  2284. {
  2285. TFileOperationProgressType Progress(&OperationProgress, &OperationFinished);
  2286. Progress.Start(foCustomCommand, osLocal, LocalFileList->Count);
  2287. DebugAssert(FProgressForm != NULL);
  2288. FProgressForm->ReadOnly = true;
  2289. try
  2290. {
  2291. for (int Index = 0; Index < LocalFileList->Count; Index++)
  2292. {
  2293. UnicodeString FileName = LocalFileList->Strings[Index];
  2294. Progress.SetFile(FileName);
  2295. TLocalCustomCommand CustomCommand(
  2296. Data, RemotePath, DefaultDownloadTargetDirectory(),
  2297. FileName, L"", L"");
  2298. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  2299. TOnceDoneOperation OnceDoneOperation;
  2300. Progress.Finish(FileName, true, OnceDoneOperation);
  2301. }
  2302. }
  2303. __finally
  2304. {
  2305. Progress.Stop();
  2306. }
  2307. }
  2308. }
  2309. //---------------------------------------------------------------------------
  2310. void __fastcall TCustomScpExplorerForm::LocalCustomCommand(TStrings * FileList,
  2311. const TCustomCommandType & ACommand, TStrings * ALocalFileList,
  2312. const TCustomCommandData & Data, const UnicodeString & CommandCommand)
  2313. {
  2314. UnicodeString RemotePath = (Terminal != NULL) ? Terminal->CurrentDirectory : UnicodeString();
  2315. TLocalCustomCommand LocalCustomCommand(Data, RemotePath, DefaultDownloadTargetDirectory());
  2316. TWinInteractiveCustomCommand InteractiveCustomCommand(
  2317. &LocalCustomCommand, ACommand.Name, ACommand.HomePage);
  2318. UnicodeString Command = InteractiveCustomCommand.Complete(CommandCommand, false);
  2319. bool FileListCommand = LocalCustomCommand.IsFileListCommand(Command);
  2320. bool LocalFileCommand = LocalCustomCommand.HasLocalFileName(Command);
  2321. Configuration->Usage->Inc(L"LocalCustomCommandRuns2");
  2322. std::unique_ptr<UnicodeString> POutput;
  2323. if (FLAGSET(ACommand.Params, ccShowResultsInMsgBox) ||
  2324. FLAGSET(ACommand.Params, ccCopyResults))
  2325. {
  2326. POutput.reset(new UnicodeString());
  2327. }
  2328. TValueRestorer<TOperationSide> ProgressSideRestorer(FProgressSide);
  2329. FProgressSide = FCurrentSide;
  2330. if (!LocalCustomCommand.IsFileCommand(Command))
  2331. {
  2332. ExecuteProcessChecked(LocalCustomCommand.Complete(Command, true), HelpKeyword, POutput.get());
  2333. }
  2334. // remote files?
  2335. else if (!IsSideLocalBrowser(FCurrentSide) || LocalFileCommand)
  2336. {
  2337. LocalCustomCommandPure(FileList, ACommand, Command, ALocalFileList, Data, LocalFileCommand, FileListCommand, POutput.get());
  2338. }
  2339. // local files
  2340. else
  2341. {
  2342. LocalCustomCommandWithLocalFiles(ACommand, Command, Data, FileListCommand, POutput.get());
  2343. }
  2344. if (POutput.get() != NULL)
  2345. {
  2346. // If the output is single-line, we do not want the trailing CRLF, as the CopyToClipboard(TStrings *) does
  2347. int P = POutput->Pos(sLineBreak);
  2348. if (P == POutput->Length() - static_cast<int>(strlen(sLineBreak)) + 1)
  2349. {
  2350. POutput->SetLength(P - 1);
  2351. }
  2352. // Copy even empty output to clipboard
  2353. if (FLAGSET(ACommand.Params, ccCopyResults))
  2354. {
  2355. CopyToClipboard(*POutput);
  2356. }
  2357. // But do not show an empty message box.
  2358. // This way the ShowResultsInMsgBox can be used to suppress a console window of commands with no output
  2359. if (FLAGSET(ACommand.Params, ccShowResultsInMsgBox) &&
  2360. !POutput->IsEmpty())
  2361. {
  2362. TClipboardHandler ClipboardHandler;
  2363. ClipboardHandler.Text = *POutput;
  2364. TMessageParams Params;
  2365. TQueryButtonAlias Aliases[1];
  2366. Aliases[0].Button = qaRetry;
  2367. Aliases[0].Alias = LoadStr(EDIT_COPY);
  2368. Aliases[0].OnSubmit = &ClipboardHandler.Copy;
  2369. Params.Aliases = Aliases;
  2370. Params.AliasesCount = LENOF(Aliases);
  2371. MessageDialog(*POutput, qtInformation, qaOK | qaRetry, HelpKeyword, &Params);
  2372. }
  2373. }
  2374. }
  2375. //---------------------------------------------------------------------------
  2376. void __fastcall TCustomScpExplorerForm::CustomCommand(TStrings * FileList,
  2377. const TCustomCommandType & ACommand, TStrings * ALocalFileList)
  2378. {
  2379. TCustomCommandData Data;
  2380. UnicodeString Site;
  2381. UnicodeString RemotePath;
  2382. if (HasActiveTerminal())
  2383. {
  2384. std::unique_ptr<TSessionData> SessionData(SessionDataForCode());
  2385. Data = TCustomCommandData(SessionData.get());
  2386. Site = Terminal->SessionData->SessionKey;
  2387. RemotePath = Terminal->CurrentDirectory;
  2388. }
  2389. UnicodeString HelpKeyword = ACommand.HomePage;
  2390. std::unique_ptr<TStrings> CustomCommandOptions(CloneStrings(WinConfiguration->CustomCommandOptions));
  2391. if (ACommand.AnyOptionWithFlag(TCustomCommandType::ofRun))
  2392. {
  2393. std::unique_ptr<TCustomCommand> CustomCommandForOptions;
  2394. if (FLAGCLEAR(ACommand.Params, ccLocal))
  2395. {
  2396. CustomCommandForOptions.reset(new TRemoteCustomCommand(Data, RemotePath));
  2397. }
  2398. else
  2399. {
  2400. CustomCommandForOptions.reset(new TLocalCustomCommand(Data, RemotePath, DefaultDownloadTargetDirectory()));
  2401. }
  2402. if (!DoCustomCommandOptionsDialog(
  2403. &ACommand, CustomCommandOptions.get(), NULL, TCustomCommandType::ofRun, CustomCommandForOptions.get(), Site, NULL))
  2404. {
  2405. Abort();
  2406. }
  2407. }
  2408. UnicodeString CommandCommand = ACommand.GetCommandWithExpandedOptions(CustomCommandOptions.get(), Site);
  2409. if (FLAGCLEAR(ACommand.Params, ccLocal))
  2410. {
  2411. RemoteCustomCommand(FileList, ACommand, Data, CommandCommand);
  2412. }
  2413. else
  2414. {
  2415. LocalCustomCommand(FileList, ACommand, ALocalFileList, Data, CommandCommand);
  2416. }
  2417. }
  2418. //---------------------------------------------------------------------------
  2419. void __fastcall TCustomScpExplorerForm::BothCustomCommand(
  2420. const TCustomCommandType & Command)
  2421. {
  2422. DebugAssert(FCustomCommandLocalFileList != NULL);
  2423. DebugAssert(FCustomCommandRemoteFileList != NULL);
  2424. DebugAssert(FCustomCommandLocalFileList->Count == FCustomCommandRemoteFileList->Count);
  2425. TStrings * LocalFileList = new TStringList();
  2426. TStrings * RemoteFileList = new TStringList();
  2427. try
  2428. {
  2429. for (int Index = 0; Index < FCustomCommandLocalFileList->Count; Index++)
  2430. {
  2431. LocalFileList->Clear();
  2432. LocalFileList->AddObject(
  2433. FCustomCommandLocalFileList->Strings[Index],
  2434. FCustomCommandLocalFileList->Objects[Index]);
  2435. RemoteFileList->Clear();
  2436. RemoteFileList->AddObject(
  2437. FCustomCommandRemoteFileList->Strings[Index],
  2438. FCustomCommandRemoteFileList->Objects[Index]);
  2439. CustomCommand(RemoteFileList, Command, LocalFileList);
  2440. }
  2441. }
  2442. __finally
  2443. {
  2444. delete LocalFileList;
  2445. delete RemoteFileList;
  2446. }
  2447. }
  2448. //---------------------------------------------------------------------------
  2449. void __fastcall TCustomScpExplorerForm::CustomCommandMenu(
  2450. TAction * Action, TStrings * LocalFileList, TStrings * RemoteFileList)
  2451. {
  2452. delete FCustomCommandLocalFileList;
  2453. delete FCustomCommandRemoteFileList;
  2454. // takeover ownership,
  2455. // the lists must survive the MenuPopup as OnClick occurs only after it exits
  2456. FCustomCommandLocalFileList = LocalFileList;
  2457. FCustomCommandRemoteFileList = RemoteFileList;
  2458. TButton * Button = dynamic_cast<TButton *>(Action->ActionComponent);
  2459. if (Button != NULL)
  2460. {
  2461. FCustomCommandMenu->Items->Clear();
  2462. NonVisualDataModule->CreateCustomCommandsMenu(FCustomCommandMenu->Items, false, false, ccltBoth, NULL);
  2463. MenuPopup(FCustomCommandMenu, Button);
  2464. }
  2465. else
  2466. {
  2467. NonVisualDataModule->CreateCustomCommandsMenu(Action, false, ccltBoth);
  2468. }
  2469. }
  2470. //---------------------------------------------------------------------------
  2471. void __fastcall TCustomScpExplorerForm::TerminalCaptureLog(
  2472. const UnicodeString & AddedLine, TCaptureOutputType OutputType)
  2473. {
  2474. DebugAssert(FCapturedLog != NULL);
  2475. if ((OutputType == cotOutput) || (OutputType == cotError))
  2476. {
  2477. FCapturedLog->Add(AddedLine);
  2478. }
  2479. }
  2480. //---------------------------------------------------------------------------
  2481. bool __fastcall TCustomScpExplorerForm::IsFileControl(TObject * Control,
  2482. TOperationSide Side)
  2483. {
  2484. return
  2485. (Side == osOther) &&
  2486. ((Control == DirView(osOther)) || (Control == DriveView(osOther)));
  2487. }
  2488. //---------------------------------------------------------------------------
  2489. void __fastcall TCustomScpExplorerForm::DirViewContextPopupDefaultItem(
  2490. TOperationSide Side, TTBXCustomItem * Item,
  2491. TResolvedDoubleClickAction DoubleClickAction1, TResolvedDoubleClickAction DoubleClickAction2)
  2492. {
  2493. TCustomDirView * DView = DirView(Side);
  2494. bool IsDefault = false;
  2495. if (DView->ItemFocused != NULL)
  2496. {
  2497. TTerminal * ATerminal = !IsSideLocalBrowser(Side) ? Terminal : NULL;
  2498. TResolvedDoubleClickAction DefaultAction =
  2499. WinConfiguration->ResolveDoubleClickAction(DView->ItemIsDirectory(DView->ItemFocused), ATerminal);
  2500. IsDefault = (DefaultAction == DoubleClickAction1) || (DefaultAction == DoubleClickAction2);
  2501. }
  2502. TTBItemOptions O = Item->Options;
  2503. if (IsDefault)
  2504. {
  2505. Item->Options = O << tboDefault;
  2506. }
  2507. else
  2508. {
  2509. Item->Options = O >> tboDefault;
  2510. }
  2511. }
  2512. //---------------------------------------------------------------------------
  2513. void __fastcall TCustomScpExplorerForm::DirViewContextPopup(
  2514. TOperationSide Side, Byte PopupComponent, const TPoint &MousePos)
  2515. {
  2516. TCustomDirView * DView = DirView(Side);
  2517. TListItem * Item = DView->ItemFocused;
  2518. if ((DView->GetItemAt(MousePos.x, MousePos.y) == Item) &&
  2519. EnableFocusedOperation[Side])
  2520. {
  2521. TPoint ClientPoint;
  2522. ClientPoint = ((MousePos.x < 0) && (MousePos.y < 0)) ?
  2523. TPoint(0, 0) : MousePos;
  2524. FLastContextPopupScreenPoint = DView->ClientToScreen(ClientPoint);
  2525. reinterpret_cast<TPopupMenu*>(GetComponent(PopupComponent))->Popup(
  2526. FLastContextPopupScreenPoint.x, FLastContextPopupScreenPoint.y);
  2527. }
  2528. }
  2529. //---------------------------------------------------------------------------
  2530. void __fastcall TCustomScpExplorerForm::RemoteDirViewContextPopup(
  2531. TObject * /*Sender*/, const TPoint &MousePos, bool &Handled)
  2532. {
  2533. DirViewContextPopupDefaultItem(osRemote, NonVisualDataModule->RemoteOpenMenuItem, rdcaChangeDir, rdcaOpen);
  2534. DirViewContextPopupDefaultItem(osRemote, NonVisualDataModule->RemoteEditMenuItem, rdcaEdit, rdcaNone);
  2535. DirViewContextPopupDefaultItem(osRemote, NonVisualDataModule->RemoteCopyMenuItem, rdcaCopy, rdcaNone);
  2536. DirViewContextPopup(osRemote, fcRemotePopup, MousePos);
  2537. Handled = true;
  2538. }
  2539. //---------------------------------------------------------------------------
  2540. void __fastcall TCustomScpExplorerForm::ReloadLocalDirectory(const UnicodeString Directory)
  2541. {
  2542. }
  2543. //---------------------------------------------------------------------------
  2544. void __fastcall TCustomScpExplorerForm::BatchStart(void *& /*Storage*/)
  2545. {
  2546. DebugAssert(FErrorList == NULL);
  2547. if (WinConfiguration->ContinueOnError)
  2548. {
  2549. FErrorList = new TStringList();
  2550. Configuration->Usage->Inc(L"ContinuationsOnError");
  2551. }
  2552. NonVisualDataModule->StartBusy();
  2553. }
  2554. //---------------------------------------------------------------------------
  2555. void __fastcall TCustomScpExplorerForm::BatchEnd(void * /*Storage*/)
  2556. {
  2557. NonVisualDataModule->EndBusy();
  2558. if (FErrorList)
  2559. {
  2560. HandleErrorList(FErrorList);
  2561. }
  2562. }
  2563. //---------------------------------------------------------------------------
  2564. void __fastcall TCustomScpExplorerForm::UpdateCopyParamCounters(
  2565. const TCopyParamType & CopyParam)
  2566. {
  2567. if (!CopyParam.IncludeFileMask.Masks.IsEmpty())
  2568. {
  2569. Configuration->Usage->Inc(L"FileMaskUses");
  2570. }
  2571. if (IsEffectiveFileNameMask(CopyParam.FileMask))
  2572. {
  2573. Configuration->Usage->Inc(L"OperationMaskUses");
  2574. }
  2575. }
  2576. //---------------------------------------------------------------------------
  2577. bool __fastcall TCustomScpExplorerForm::ExecuteCopyMoveFileOperation(
  2578. TFileOperation Operation, TOperationSide Side, TStrings * FileList, bool NoConfirmation, void * AParam)
  2579. {
  2580. DebugAssert(!IsLocalBrowserMode());
  2581. TTransferDirection Direction = (Side == osLocal ? tdToRemote : tdToLocal);
  2582. TTransferType Type = (Operation == foCopy ? ttCopy : ttMove);
  2583. TTransferOperationParam DefaultParam;
  2584. TTransferOperationParam & Param = (AParam != NULL) ? *static_cast<TTransferOperationParam *>(AParam) : DefaultParam;
  2585. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  2586. switch (Param.Queue)
  2587. {
  2588. case asOn:
  2589. CopyParam.Queue = true;
  2590. break;
  2591. case asOff:
  2592. CopyParam.Queue = false;
  2593. break;
  2594. case asAuto:
  2595. default:
  2596. // keep default
  2597. break;
  2598. }
  2599. bool Result =
  2600. CopyParamDialog(Direction, Type, Param.Temp, FileList, Param.TargetDirectory, CopyParam, !NoConfirmation, Param.DragDrop, Param.Options);
  2601. if (Result)
  2602. {
  2603. DebugAssert(Terminal);
  2604. bool SelectionRestored = false;
  2605. TCustomDirView * DView = NULL;
  2606. if (HasDirView[Side])
  2607. {
  2608. DView = DirView(Side);
  2609. DView->SaveSelection();
  2610. DView->SaveSelectedNames();
  2611. }
  2612. UpdateCopyParamCounters(CopyParam);
  2613. std::unique_ptr<TStringList> TransferResumeList(new TStringList());
  2614. DebugAssert(FTransferResumeList == NULL);
  2615. FTransferResumeList = Terminal->IsCapable[fcMoveToQueue] ? TransferResumeList.get() : NULL;
  2616. FMoveToQueue = false;
  2617. int Params = FLAGMASK(Operation == foMove, cpDelete);
  2618. try
  2619. {
  2620. TStrings * PermanentFileList;
  2621. std::unique_ptr<TStrings> PermanentFileListOwner;
  2622. try
  2623. {
  2624. if (Side == osLocal)
  2625. {
  2626. PermanentFileList = FileList;
  2627. Params |= FLAGMASK(Param.Temp, cpTemporary);
  2628. CopyParam.IncludeFileMask.SetRoots(FileList, Param.TargetDirectory);
  2629. Terminal->CopyToRemote(FileList, Param.TargetDirectory, &CopyParam, Params, NULL);
  2630. if (Operation == foMove)
  2631. {
  2632. ReloadLocalDirectory();
  2633. if (DView != NULL)
  2634. {
  2635. DView->RestoreSelection();
  2636. }
  2637. SelectionRestored = true;
  2638. }
  2639. }
  2640. else
  2641. {
  2642. // Clone the file list as it may refer to current directory files,
  2643. // which get destroyed, when the source directory is reloaded after foMove operation.
  2644. // We should actually clone the file list for whole ExecuteFileOperation to protect against reloads.
  2645. // But for a hotfix, we are not going to do such a big change.
  2646. PermanentFileListOwner.reset(TRemoteFileList::CloneStrings(FileList));
  2647. PermanentFileList = PermanentFileListOwner.get();
  2648. try
  2649. {
  2650. CopyParam.IncludeFileMask.SetRoots(Param.TargetDirectory, FileList);
  2651. Terminal->CopyToLocal(FileList, Param.TargetDirectory, &CopyParam, Params, NULL);
  2652. }
  2653. __finally
  2654. {
  2655. if (Operation == foMove)
  2656. {
  2657. if (DView != NULL)
  2658. {
  2659. DView->RestoreSelection();
  2660. }
  2661. SelectionRestored = true;
  2662. }
  2663. ReloadLocalDirectory(Param.TargetDirectory);
  2664. }
  2665. }
  2666. // When the transfer batch is aborted between individual file transfers, the Abort is not called.
  2667. if (FMoveToQueue)
  2668. {
  2669. Abort();
  2670. }
  2671. }
  2672. catch (EAbort &)
  2673. {
  2674. if (FMoveToQueue)
  2675. {
  2676. Params |= (CopyParam.QueueNoConfirmation ? cpNoConfirmation : 0);
  2677. DebugAssert(CopyParam.TransferSkipList == NULL);
  2678. DebugAssert(CopyParam.TransferResumeFile.IsEmpty());
  2679. if (TransferResumeList->Count > 0)
  2680. {
  2681. CopyParam.TransferResumeFile = TransferResumeList->Strings[TransferResumeList->Count - 1];
  2682. TransferResumeList->Delete(TransferResumeList->Count - 1);
  2683. }
  2684. CopyParam.TransferSkipList = TransferResumeList.release();
  2685. // not really needed, just to keep it consistent with TransferResumeList
  2686. FTransferResumeList = NULL;
  2687. FMoveToQueue = false;
  2688. Configuration->Usage->Inc("MovesToBackground");
  2689. AddQueueItem(Queue, Direction, PermanentFileList, Param.TargetDirectory, CopyParam, Params);
  2690. ClearTransferSourceSelection(Direction);
  2691. }
  2692. throw;
  2693. }
  2694. }
  2695. __finally
  2696. {
  2697. if (!SelectionRestored && (DView != NULL))
  2698. {
  2699. DView->DiscardSavedSelection();
  2700. }
  2701. FTransferResumeList = NULL;
  2702. }
  2703. }
  2704. return Result;
  2705. }
  2706. //---------------------------------------------------------------------------
  2707. bool __fastcall TCustomScpExplorerForm::ExecuteDeleteFileOperation(
  2708. TOperationSide Side, TStrings * FileList, void * Param)
  2709. {
  2710. DebugAssert(FileList->Count);
  2711. // We deliberately do not toggle alternative flag (Param), but use OR,
  2712. // because the Param is set only when command is invoked using Shift-Del/F8 keyboard
  2713. // shortcut of CurrentDeleteAlternativeAction
  2714. bool Alternative =
  2715. bool(Param) || UseAlternativeFunction();
  2716. bool Recycle;
  2717. if (IsSideLocalBrowser(Side))
  2718. {
  2719. Recycle = (WinConfiguration->DeleteToRecycleBin != Alternative);
  2720. }
  2721. else
  2722. {
  2723. Recycle =
  2724. (Terminal->SessionData->DeleteToRecycleBin != Alternative) &&
  2725. !Terminal->SessionData->RecycleBinPath.IsEmpty() &&
  2726. !Terminal->IsRecycledFile(FileList->Strings[0]);
  2727. }
  2728. bool Result = !(Recycle ? WinConfiguration->ConfirmRecycling : WinConfiguration->ConfirmDeleting);
  2729. if (!Result)
  2730. {
  2731. UnicodeString Query;
  2732. if (FileList->Count == 1)
  2733. {
  2734. if (IsSideLocalBrowser(Side))
  2735. {
  2736. Query = ExtractFileName(FileList->Strings[0]);
  2737. }
  2738. else
  2739. {
  2740. Query = UnixExtractFileName(FileList->Strings[0]);
  2741. }
  2742. Query = FMTLOAD((Recycle ? CONFIRM_RECYCLE_FILE : CONFIRM_DELETE_FILE), (Query));
  2743. }
  2744. else
  2745. {
  2746. Query = FMTLOAD((Recycle ? CONFIRM_RECYCLE_FILES : CONFIRM_DELETE_FILES), (FileList->Count));
  2747. }
  2748. TMessageParams Params(mpNeverAskAgainCheck);
  2749. Params.ImageName = L"Delete file";
  2750. unsigned int Answer =
  2751. MessageDialog(MainInstructions(Query), qtConfirmation, qaOK | qaCancel, HELP_DELETE_FILE, &Params);
  2752. if (Answer == qaNeverAskAgain)
  2753. {
  2754. Result = true;
  2755. if (Recycle)
  2756. {
  2757. WinConfiguration->ConfirmRecycling = false;
  2758. }
  2759. else
  2760. {
  2761. WinConfiguration->ConfirmDeleting = false;
  2762. }
  2763. }
  2764. else
  2765. {
  2766. Result = (Answer == qaOK);
  2767. }
  2768. }
  2769. if (Result)
  2770. {
  2771. DeleteFiles(Side, FileList, FLAGMASK(Alternative, dfAlternative));
  2772. }
  2773. return Result;
  2774. }
  2775. //---------------------------------------------------------------------------
  2776. bool __fastcall TCustomScpExplorerForm::ExecuteFileOperation(TFileOperation Operation,
  2777. TOperationSide Side, TStrings * FileList, bool NoConfirmation, void * Param)
  2778. {
  2779. TAutoBatch AutoBatch(this);
  2780. bool Result;
  2781. if (TFileOperationProgressType::IsTransferOperation(Operation))
  2782. {
  2783. Result = ExecuteCopyMoveFileOperation(Operation, Side, FileList, NoConfirmation, Param);
  2784. }
  2785. else if (Operation == foRename)
  2786. {
  2787. DebugAssert(DirView(Side)->ItemFocused);
  2788. DirView(Side)->ItemFocused->EditCaption();
  2789. Result = true;
  2790. }
  2791. else if (Operation == foDelete)
  2792. {
  2793. Result = ExecuteDeleteFileOperation(Side, FileList, Param);
  2794. }
  2795. else if (Operation == foSetProperties)
  2796. {
  2797. DirView(osRemote)->SaveSelectedNames();
  2798. Result = SetProperties(Side, FileList);
  2799. }
  2800. else if (Operation == foCustomCommand)
  2801. {
  2802. DebugAssert(Param);
  2803. DebugAssert(Side == osRemote); // Side carries no meaning for foCustomCommand
  2804. DirView(osRemote)->SaveSelectedNames();
  2805. const TCustomCommandType * Command = static_cast<const TCustomCommandType*>(Param);
  2806. CustomCommand(FileList, *Command, NULL);
  2807. Result = true;
  2808. }
  2809. else if ((Operation == foRemoteMove) || (Operation == foRemoteCopy))
  2810. {
  2811. DebugAssert(!IsSideLocalBrowser(Side));
  2812. Result = RemoteTransferFiles(FileList, NoConfirmation,
  2813. (Operation == foRemoteMove), reinterpret_cast<TManagedTerminal *>(Param));
  2814. }
  2815. else if (Operation == foLock)
  2816. {
  2817. DebugAssert(!IsSideLocalBrowser(Side));
  2818. LockFiles(FileList, true);
  2819. Result = true;
  2820. }
  2821. else if (Operation == foUnlock)
  2822. {
  2823. DebugAssert(!IsSideLocalBrowser(Side));
  2824. LockFiles(FileList, false);
  2825. Result = true;
  2826. }
  2827. else
  2828. {
  2829. DebugFail();
  2830. }
  2831. return Result;
  2832. }
  2833. //---------------------------------------------------------------------------
  2834. TOperationSide __fastcall TCustomScpExplorerForm::GetSide(TOperationSide Side)
  2835. {
  2836. if (Side == osCurrent)
  2837. {
  2838. Side = FCurrentSide;
  2839. }
  2840. return Side;
  2841. }
  2842. //---------------------------------------------------------------------------
  2843. TOperationSide TCustomScpExplorerForm::GetOtherSide(TOperationSide Side)
  2844. {
  2845. TOperationSide Result;
  2846. switch (GetSide(Side))
  2847. {
  2848. case osLocal:
  2849. Result = osOther;
  2850. break;
  2851. case osOther:
  2852. Result = osLocal;
  2853. break;
  2854. default:
  2855. DebugFail();
  2856. Abort();
  2857. }
  2858. return Result;
  2859. }
  2860. //---------------------------------------------------------------------------
  2861. bool __fastcall TCustomScpExplorerForm::ExecuteFileOperation(TFileOperation Operation,
  2862. TOperationSide Side, bool OnFocused, bool NoConfirmation, void * Param)
  2863. {
  2864. Side = GetSide(Side);
  2865. bool FullPath = IsSideLocalBrowser(Side);
  2866. TCustomDirView * ADirView = DirView(Side);
  2867. std::unique_ptr<TStringList> FileList(new TStringList());
  2868. ADirView->CreateFileList(OnFocused, FullPath, FileList.get());
  2869. bool ReloadProperties =
  2870. !IsSideLocalBrowser(Side) && (Operation == foSetProperties) &&
  2871. DebugAlwaysTrue(HasActiveTerminal()) && Terminal->IsCapable[fcLoadingAdditionalProperties];
  2872. bool Result = ExecuteFileOperation(Operation, Side, FileList.get(), NoConfirmation, Param);
  2873. if (Result && ReloadProperties)
  2874. {
  2875. std::unique_ptr<TStringList> FileList2(new TStringList());
  2876. FileList->CaseSensitive = true;
  2877. FileList->Sort();
  2878. for (int Index = 0; Index < ADirView->Items->Count; Index++)
  2879. {
  2880. TListItem * Item = ADirView->Items->Item[Index];
  2881. UnicodeString FileName = ADirView->ItemFileName(Item);
  2882. int Index2;
  2883. if (FileList->Find(FileName, Index2))
  2884. {
  2885. FileList2->AddObject(FileName, ADirView->ItemData(Item));
  2886. }
  2887. }
  2888. LoadFilesProperties(FileList2.get());
  2889. }
  2890. return Result;
  2891. }
  2892. //---------------------------------------------------------------------------
  2893. void __fastcall TCustomScpExplorerForm::ExecuteFileOperationCommand(
  2894. TFileOperation Operation, TOperationSide Side, bool OnFocused,
  2895. bool NoConfirmation, void * Param)
  2896. {
  2897. if (ExecuteFileOperation(Operation, Side, OnFocused, NoConfirmation, Param))
  2898. {
  2899. if (TFileOperationProgressType::IsTransferOperation(Operation))
  2900. {
  2901. DebugAssert(!IsLocalBrowserMode());
  2902. if (GetSide(Side) == osLocal)
  2903. {
  2904. Configuration->Usage->Inc(L"UploadsCommand");
  2905. }
  2906. else
  2907. {
  2908. Configuration->Usage->Inc(L"DownloadsCommand");
  2909. }
  2910. }
  2911. }
  2912. }
  2913. //---------------------------------------------------------------------------
  2914. void __fastcall TCustomScpExplorerForm::ExecuteCopyOperationCommand(
  2915. TOperationSide Side, bool OnFocused, unsigned int Flags)
  2916. {
  2917. TTransferOperationParam Param;
  2918. if ((WinConfiguration->Interface != ifCommander) ||
  2919. WinConfiguration->ScpCommander.ExplorerKeyboardShortcuts)
  2920. {
  2921. Flags &= ~cocShortCutHint;
  2922. }
  2923. TCustomDirView * DView = DirView(Side);
  2924. Param.Options =
  2925. FLAGMASK(FLAGSET(Flags, cocShortCutHint), coShortCutHint) |
  2926. FLAGMASK(SelectedAllFilesInDirView(DView), coAllFiles);
  2927. if (FLAGSET(Flags, cocQueue))
  2928. {
  2929. Param.Queue = asOn;
  2930. }
  2931. else if (FLAGSET(Flags, cocNonQueue))
  2932. {
  2933. Param.Queue = asOff;
  2934. }
  2935. ExecuteFileOperationCommand(foCopy, Side, OnFocused, false, &Param);
  2936. }
  2937. //---------------------------------------------------------------------------
  2938. void __fastcall TCustomScpExplorerForm::HandleErrorList(TStringList *& ErrorList)
  2939. {
  2940. int Count = ErrorList->Count;
  2941. try
  2942. {
  2943. if (ErrorList->Count)
  2944. {
  2945. UnicodeString Message = MainInstructions(FMTLOAD(ERROR_LIST_COUNT, (ErrorList->Count)));
  2946. if (MessageDialog(Message, qtError,
  2947. qaOK | qaCancel, HELP_NONE) == qaOK)
  2948. {
  2949. unsigned int Answer;
  2950. int Index = 0;
  2951. do
  2952. {
  2953. DebugAssert(Index >= 0 && Index < Count);
  2954. TQueryButtonAlias Aliases[2];
  2955. Aliases[0].Button = qaYes;
  2956. Aliases[0].Alias = LoadStr(PREV_BUTTON);
  2957. Aliases[1].Button = qaNo;
  2958. Aliases[1].Alias = LoadStr(NEXT_BUTTON);
  2959. TMessageParams Params;
  2960. Params.Aliases = Aliases;
  2961. Params.AliasesCount = LENOF(Aliases);
  2962. int No = Index + 1;
  2963. UnicodeString Message = ErrorList->Strings[Index];
  2964. UnicodeString AMainInstructions;
  2965. UnicodeString Details;
  2966. if (ExtractMainInstructions(Message, AMainInstructions))
  2967. {
  2968. Details = Message;
  2969. Message = AMainInstructions;
  2970. }
  2971. Message = MainInstructions(FMTLOAD(ERROR_LIST_NUMBER, (No, Count, Message))) + Details;
  2972. Answer = MoreMessageDialog(
  2973. Message,
  2974. dynamic_cast<TStrings *>(ErrorList->Objects[Index]), qtError,
  2975. (Index ? qaYes : 0) | (Index < Count - 1 ? qaNo : 0) |
  2976. qaOK, HELP_NONE, &Params);
  2977. if (Answer == qaNo)
  2978. {
  2979. Index++;
  2980. }
  2981. if (Answer == qaYes)
  2982. {
  2983. Index--;
  2984. }
  2985. }
  2986. while (Answer != qaOK);
  2987. }
  2988. }
  2989. }
  2990. __finally
  2991. {
  2992. TStrings * List = ErrorList;
  2993. ErrorList = NULL;
  2994. for (int i = 0; i < Count; i++)
  2995. {
  2996. delete List->Objects[i];
  2997. }
  2998. delete List;
  2999. }
  3000. }
  3001. //---------------------------------------------------------------------------
  3002. void __fastcall TCustomScpExplorerForm::ExecuteRemoteFile(
  3003. const UnicodeString & FullFileName, TRemoteFile * File, TExecuteFileBy ExecuteFileBy)
  3004. {
  3005. // needed for checking filemasks, as there's no directory object
  3006. // associated with the file object
  3007. File->FullFileName = FullFileName;
  3008. TFileMasks::TParams MaskParams;
  3009. MaskParams.Size = File->Size;
  3010. MaskParams.Modification = File->Modification;
  3011. ExecuteFile(osRemote, ExecuteFileBy, NULL, FullFileName, File, MaskParams);
  3012. }
  3013. //---------------------------------------------------------------------------
  3014. void __fastcall TCustomScpExplorerForm::EditNew(TOperationSide Side)
  3015. {
  3016. DebugAssert(!WinConfiguration->DisableOpenEdit);
  3017. Side = GetSide(Side);
  3018. TCustomDirView * DView = DirView(Side);
  3019. TListItem * FocusedItem = DView->ItemFocused;
  3020. UnicodeString Name;
  3021. if ((FocusedItem != NULL) && !DView->ItemIsDirectory(FocusedItem))
  3022. {
  3023. Name = DView->ItemFileName(FocusedItem);
  3024. }
  3025. else
  3026. {
  3027. Name = LoadStr(NEW_FILE);
  3028. }
  3029. UnicodeString Names = Name;
  3030. std::unique_ptr<TStrings> History(CloneStrings(CustomWinConfiguration->History[L"EditFile"]));
  3031. if (InputDialog(LoadStr(EDIT_FILE_CAPTION), LoadStr(EDIT_FILE_PROMPT), Names,
  3032. HELP_EDIT_NEW, History.get(), true, NULL, true, 400))
  3033. {
  3034. while (!Names.IsEmpty())
  3035. {
  3036. Name = CutToChar(Names, FileMasksDelimiters[1], false);
  3037. CustomWinConfiguration->History[L"EditFile"] = History.get();
  3038. UnicodeString TargetFileName;
  3039. UnicodeString LocalFileName;
  3040. UnicodeString RootTempDir;
  3041. UnicodeString TempDir;
  3042. UnicodeString RemoteDirectory;
  3043. bool ExistingFile = false;
  3044. if (!IsSideLocalBrowser(Side))
  3045. {
  3046. Name = AbsolutePath(Terminal->CurrentDirectory, Name);
  3047. std::unique_ptr<TRemoteFile> File(Terminal->TryReadFile(Name));
  3048. if ((File.get() != NULL) &&
  3049. !File->IsDirectory)
  3050. {
  3051. ExecuteRemoteFile(Name, File.get(), efDefaultEditor);
  3052. ExistingFile = true;
  3053. }
  3054. File.reset(NULL);
  3055. if (!ExistingFile)
  3056. {
  3057. RemoteDirectory = UnixExtractFilePath(Name);
  3058. TemporaryDirectoryForRemoteFiles(
  3059. RemoteDirectory, GUIConfiguration->CurrentCopyParam, false, TempDir, RootTempDir);
  3060. TargetFileName = UnixExtractFileName(Name);
  3061. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  3062. LocalFileName = TempDir +
  3063. // We probably do not want to trim the VMS version here
  3064. Terminal->ChangeFileName(&CopyParam, TargetFileName, osRemote, false);
  3065. }
  3066. }
  3067. else
  3068. {
  3069. if (ExtractFilePath(Name).IsEmpty())
  3070. {
  3071. LocalFileName = IncludeTrailingBackslash(DView->PathName) + Name;
  3072. }
  3073. else
  3074. {
  3075. LocalFileName = ExpandFileName(Name);
  3076. }
  3077. TargetFileName = ExtractFileName(Name);
  3078. }
  3079. if (!ExistingFile)
  3080. {
  3081. bool NewFile = !FileExists(ApiPath(LocalFileName));
  3082. if (NewFile)
  3083. {
  3084. int File = FileCreate(ApiPath(LocalFileName));
  3085. if (File < 0)
  3086. {
  3087. if (!RootTempDir.IsEmpty())
  3088. {
  3089. RecursiveDeleteFile(ExcludeTrailingBackslash(RootTempDir));
  3090. }
  3091. throw Exception(FMTLOAD(CREATE_FILE_ERROR, (LocalFileName)));
  3092. }
  3093. else
  3094. {
  3095. FileClose(File);
  3096. }
  3097. }
  3098. TExecuteFileBy ExecuteFileBy = efDefaultEditor;
  3099. const TEditorData * ExternalEditor = NULL;
  3100. TFileMasks::TParams MaskParams; // size not known
  3101. ExecuteFileNormalize(ExecuteFileBy, ExternalEditor, TargetFileName,
  3102. false, MaskParams);
  3103. CustomExecuteFile(Side, ExecuteFileBy, LocalFileName, TargetFileName,
  3104. ExternalEditor, RootTempDir, RemoteDirectory, NewFile, TDateTime());
  3105. }
  3106. }
  3107. }
  3108. }
  3109. //---------------------------------------------------------------------------
  3110. bool __fastcall TCustomScpExplorerForm::RemoteExecuteForceText(
  3111. TExecuteFileBy ExecuteFileBy, const TEditorData * ExternalEditor)
  3112. {
  3113. DebugAssert((ExecuteFileBy == efExternalEditor) ==
  3114. ((ExternalEditor != NULL) && (ExternalEditor->Editor == edExternal)));
  3115. DebugAssert(ExecuteFileBy != efDefaultEditor);
  3116. return
  3117. ((ExecuteFileBy == efInternalEditor)) ||
  3118. ((ExecuteFileBy == efExternalEditor) && ExternalEditor->ExternalEditorText);
  3119. }
  3120. //---------------------------------------------------------------------------
  3121. void __fastcall TCustomScpExplorerForm::CustomExecuteFile(TOperationSide Side,
  3122. TExecuteFileBy ExecuteFileBy, UnicodeString FileName, UnicodeString OriginalFileName,
  3123. const TEditorData * ExternalEditor, UnicodeString LocalRootDirectory,
  3124. UnicodeString RemoteDirectory, bool NewFile, const TDateTime & SourceTimestamp)
  3125. {
  3126. DebugAssert(!WinConfiguration->DisableOpenEdit);
  3127. DebugAssert((ExecuteFileBy == efExternalEditor) ==
  3128. ((ExternalEditor != NULL) && (ExternalEditor->Editor == edExternal)));
  3129. DebugAssert(ExecuteFileBy != efDefaultEditor);
  3130. Side = GetSide(Side);
  3131. std::unique_ptr<TEditedFileData> Data(new TEditedFileData);
  3132. if (!IsSideLocalBrowser(Side))
  3133. {
  3134. Data->Terminal = Terminal;
  3135. Data->Queue = Queue;
  3136. Data->SessionData = CloneCurrentSessionData();
  3137. Data->ForceText = RemoteExecuteForceText(ExecuteFileBy, ExternalEditor);
  3138. Data->RemoteDirectory = RemoteDirectory;
  3139. Data->SessionName = Terminal->SessionData->SessionName;
  3140. Data->LocalRootDirectory = LocalRootDirectory;
  3141. Data->OriginalFileName = OriginalFileName;
  3142. Data->Command = L""; // will be changed later for external editor
  3143. // Empty SourceTimestamp indicates that checking was disabled when the file was opened
  3144. if (WinConfiguration->EditorCheckNotModified)
  3145. {
  3146. Data->SourceTimestamp = SourceTimestamp;
  3147. }
  3148. }
  3149. if (ExecuteFileBy == efInternalEditor)
  3150. {
  3151. if (!IsSideLocalBrowser(Side))
  3152. {
  3153. UnicodeString RemotePath = UnixCombinePaths(RemoteDirectory, OriginalFileName);
  3154. UnicodeString Caption = RemotePath + TitleSeparator + Terminal->SessionData->SessionName;
  3155. TForm * Editor;
  3156. try
  3157. {
  3158. Editor =
  3159. ShowEditorForm(
  3160. FileName, this,
  3161. FEditorManager->FileChanged, FEditorManager->FileReload, FEditorManager->FileClosed,
  3162. SaveAllInternalEditors, AnyInternalEditorModified,
  3163. Caption, StandaloneOperation, SessionColor, Terminal->SessionData->InternalEditorEncoding, NewFile);
  3164. }
  3165. catch(...)
  3166. {
  3167. if (!LocalRootDirectory.IsEmpty())
  3168. {
  3169. RecursiveDeleteFile(ExcludeTrailingBackslash(LocalRootDirectory));
  3170. }
  3171. throw;
  3172. }
  3173. FEditorManager->AddFileInternal(FileName, Data.release(), Editor);
  3174. AppLogFmt(L"Opened internal editor with \"%s\"", (FileName));
  3175. }
  3176. else
  3177. {
  3178. DebugAssert(!StandaloneOperation);
  3179. TForm * Editor =
  3180. ShowEditorForm(FileName, this, NULL, NULL, LocalEditorClosed,
  3181. SaveAllInternalEditors, AnyInternalEditorModified,
  3182. L"", false, SessionColor, -1, NewFile);
  3183. FLocalEditors->Add(Editor);
  3184. }
  3185. }
  3186. else
  3187. {
  3188. HANDLE Process;
  3189. if (ExecuteFileBy == efExternalEditor)
  3190. {
  3191. UnicodeString Program, Params, Dir;
  3192. Data->Command = ExternalEditor->ExternalEditor;
  3193. ReformatFileNameCommand(Data->Command);
  3194. SplitCommand(Data->Command, Program, Params, Dir);
  3195. Params = ExpandFileNameCommand(Params, FileName);
  3196. Program = ExpandEnvironmentVariables(Program);
  3197. if (!ExecuteShell(Program, Params, Process))
  3198. {
  3199. throw EOSExtException(FMTLOAD(EDITOR_ERROR, (Program)));
  3200. }
  3201. }
  3202. else
  3203. {
  3204. DebugAssert(!IsSideLocalBrowser(Side));
  3205. if (!ExecuteShell(FileName, L"", Process))
  3206. {
  3207. throw EOSExtException(FMTLOAD(EXECUTE_FILE_ERROR, (FileName)));
  3208. }
  3209. }
  3210. if (IsSideLocalBrowser(Side) ||
  3211. ((ExecuteFileBy == efShell) &&
  3212. !WinConfiguration->Editor.SDIShellEditor) ||
  3213. ((ExecuteFileBy == efExternalEditor) &&
  3214. !ExternalEditor->SDIExternalEditor))
  3215. {
  3216. // no need for handle
  3217. if (Process != NULL)
  3218. {
  3219. DebugCheck(CloseHandle(Process));
  3220. }
  3221. Process = INVALID_HANDLE_VALUE;
  3222. }
  3223. else
  3224. {
  3225. if (Process == NULL)
  3226. {
  3227. throw ExtException(LoadStr(OPEN_FILE_NO_PROCESS2), L"", HELP_OPEN_FILE_NO_PROCESS);
  3228. }
  3229. }
  3230. if (!IsSideLocalBrowser(Side))
  3231. {
  3232. FEditorManager->AddFileExternal(FileName, Data.release(), Process);
  3233. AppLogFmt(L"Opened external editor with remote file \"%s\".", (FileName));
  3234. }
  3235. }
  3236. }
  3237. //---------------------------------------------------------------------------
  3238. void __fastcall TCustomScpExplorerForm::SaveInternalEditor(
  3239. const UnicodeString /*FileName*/, TEditedFileData * /*Data*/, TObject * Token,
  3240. void * /*Arg*/)
  3241. {
  3242. if (Token != NULL)
  3243. {
  3244. EditorFormFileSave(static_cast<TForm *>(Token));
  3245. }
  3246. }
  3247. //---------------------------------------------------------------------------
  3248. void __fastcall TCustomScpExplorerForm::SaveAllInternalEditors(TObject * /*Sender*/)
  3249. {
  3250. for (int Index = 0; Index < FLocalEditors->Count; Index++)
  3251. {
  3252. EditorFormFileSave(static_cast<TForm *>(FLocalEditors->Items[Index]));
  3253. }
  3254. FEditorManager->ProcessFiles(SaveInternalEditor, NULL);
  3255. }
  3256. //---------------------------------------------------------------------------
  3257. void __fastcall TCustomScpExplorerForm::InternalEditorModified(
  3258. const UnicodeString /*FileName*/, TEditedFileData * /*Data*/, TObject * Token,
  3259. void * Arg)
  3260. {
  3261. if ((Token != NULL) &&
  3262. IsEditorFormModified(static_cast<TForm *>(Token)))
  3263. {
  3264. *static_cast<bool *>(Arg) = true;
  3265. }
  3266. }
  3267. //---------------------------------------------------------------------------
  3268. void __fastcall TCustomScpExplorerForm::AnyInternalEditorModified(
  3269. TObject * /*Sender*/, bool & Modified)
  3270. {
  3271. for (int Index = 0; !Modified && (Index < FLocalEditors->Count); Index++)
  3272. {
  3273. if (IsEditorFormModified(static_cast<TForm *>(FLocalEditors->Items[Index])))
  3274. {
  3275. Modified = true;
  3276. }
  3277. }
  3278. if (!Modified)
  3279. {
  3280. FEditorManager->ProcessFiles(InternalEditorModified, &Modified);
  3281. }
  3282. }
  3283. //---------------------------------------------------------------------------
  3284. void __fastcall TCustomScpExplorerForm::LocalEditorClosed(TObject * Sender, bool /*Forced*/)
  3285. {
  3286. DebugCheck(FLocalEditors->Extract(Sender) >= 0);
  3287. }
  3288. //---------------------------------------------------------------------------
  3289. void __fastcall TCustomScpExplorerForm::TemporaryDirectoryForRemoteFiles(
  3290. const UnicodeString & RemoteDirectory, const TCopyParamType & CopyParam, bool Simple,
  3291. UnicodeString & Result, UnicodeString & RootDirectory)
  3292. {
  3293. if (!WinConfiguration->TemporaryDirectoryDeterministic || Simple)
  3294. {
  3295. RootDirectory = IncludeTrailingBackslash(WinConfiguration->TemporaryDir());
  3296. Result = RootDirectory;
  3297. }
  3298. else
  3299. {
  3300. RootDirectory = L"";
  3301. Result = WinConfiguration->ExpandedTemporaryDirectory();
  3302. Result = IncludeTrailingBackslash(Result);
  3303. }
  3304. if (WinConfiguration->TemporaryDirectoryAppendSession && !Simple)
  3305. {
  3306. Result = IncludeTrailingBackslash(Result + MakeValidFileName(Terminal->SessionData->SessionName));
  3307. }
  3308. if (WinConfiguration->TemporaryDirectoryAppendPath && !Simple)
  3309. {
  3310. UnicodeString ARemoteDirectory = RemoteDirectory;
  3311. if (!ARemoteDirectory.IsEmpty() && (ARemoteDirectory[1] == L'/'))
  3312. {
  3313. ARemoteDirectory.Delete(1, 1);
  3314. }
  3315. Result = IncludeTrailingBackslash(Result + CopyParam.ValidLocalPath(FromUnixPath(ARemoteDirectory)));
  3316. }
  3317. if (!ForceDirectories(ApiPath(Result)))
  3318. {
  3319. throw EOSExtException(FMTLOAD(CREATE_TEMP_DIR_ERROR, (Result)));
  3320. }
  3321. }
  3322. //---------------------------------------------------------------------------
  3323. void __fastcall TCustomScpExplorerForm::TemporarilyDownloadFiles(
  3324. TStrings * FileList, bool ForceText, UnicodeString & RootTempDir, UnicodeString & TempDir,
  3325. bool GetTargetNames, bool AutoOperation, bool SimpleTempDir)
  3326. {
  3327. DebugAssert(!IsLocalBrowserMode());
  3328. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  3329. if (ForceText)
  3330. {
  3331. CopyParam.TransferMode = tmAscii;
  3332. }
  3333. TemporaryFileCopyParam(CopyParam);
  3334. if (TempDir.IsEmpty())
  3335. {
  3336. TemporaryDirectoryForRemoteFiles(Terminal->CurrentDirectory, CopyParam, SimpleTempDir, TempDir, RootTempDir);
  3337. }
  3338. DebugAssert(!FAutoOperation);
  3339. FAutoOperation = AutoOperation;
  3340. Terminal->ExceptionOnFail = true;
  3341. try
  3342. {
  3343. try
  3344. {
  3345. // turn off confirmations, as for MDI editors we may possibly download
  3346. // the same file over
  3347. Terminal->CopyToLocal(
  3348. FileList, TempDir, &CopyParam, cpNoConfirmation | cpTemporary, NULL);
  3349. if (GetTargetNames)
  3350. {
  3351. for (int i = 0; i < FileList->Count; i++)
  3352. {
  3353. FileList->Strings[i] =
  3354. Terminal->ChangeFileName(&CopyParam, UnixExtractFileName(FileList->Strings[i]), osRemote, false);
  3355. }
  3356. }
  3357. }
  3358. catch(...)
  3359. {
  3360. if (!RootTempDir.IsEmpty())
  3361. {
  3362. RecursiveDeleteFile(ExcludeTrailingBackslash(RootTempDir));
  3363. }
  3364. throw;
  3365. }
  3366. }
  3367. __finally
  3368. {
  3369. FAutoOperation = false;
  3370. Terminal->ExceptionOnFail = false;
  3371. }
  3372. }
  3373. //---------------------------------------------------------------------------
  3374. void __fastcall TCustomScpExplorerForm::EditorAutoConfig()
  3375. {
  3376. // Do not waste time checking for default editor list the next time,
  3377. // or testing if default editor is not notepad.
  3378. bool TryNextTime = false;
  3379. UnicodeString UsageState;
  3380. if (!WinConfiguration->EditorList->IsDefaultList())
  3381. {
  3382. UsageState = "H";
  3383. }
  3384. else
  3385. {
  3386. UnicodeString Executable;
  3387. UnicodeString ExecutableDescription;
  3388. if (DetectSystemExternalEditor(false, Executable, ExecutableDescription, UsageState, TryNextTime))
  3389. {
  3390. UnicodeString Message =
  3391. FMTLOAD(EDITOR_AUTO_CONFIG2, (ExecutableDescription, ExecutableDescription));
  3392. unsigned int Answer =
  3393. MessageDialog(Message, qtConfirmation, qaOK | qaCancel, HELP_EDITOR_AUTO_CONFIG);
  3394. if (Answer != qaOK)
  3395. {
  3396. UsageState = "R";
  3397. }
  3398. else
  3399. {
  3400. UsageState = "A";
  3401. TEditorData EditorData;
  3402. EditorData.Editor = edExternal;
  3403. EditorData.ExternalEditor = FormatCommand(Executable, L"");
  3404. EditorData.ExternalEditorOptionsAutodetect();
  3405. TEditorList EditorList;
  3406. EditorList = *WinConfiguration->EditorList;
  3407. EditorList.Insert(0, new TEditorPreferences(EditorData));
  3408. WinConfiguration->EditorList = &EditorList;
  3409. }
  3410. }
  3411. }
  3412. WinConfiguration->OfferedEditorAutoConfig = !TryNextTime;
  3413. WinConfiguration->Usage->Set(L"EditorAutoConfig", UsageState);
  3414. }
  3415. //---------------------------------------------------------------------------
  3416. void __fastcall TCustomScpExplorerForm::ExecuteFileNormalize(
  3417. TExecuteFileBy & ExecuteFileBy, const TEditorData *& ExternalEditor,
  3418. const UnicodeString & FileName, bool Local, const TFileMasks::TParams & MaskParams)
  3419. {
  3420. if (ExecuteFileBy == efDefaultEditor)
  3421. {
  3422. if (!WinConfiguration->OfferedEditorAutoConfig)
  3423. {
  3424. EditorAutoConfig();
  3425. }
  3426. const TEditorPreferences * Editor =
  3427. WinConfiguration->DefaultEditorForFile(FileName, Local, MaskParams);
  3428. if ((Editor == NULL) || (Editor->Data->Editor == edInternal))
  3429. {
  3430. ExecuteFileBy = efInternalEditor;
  3431. ExternalEditor = NULL;
  3432. }
  3433. else if (Editor->Data->Editor == edOpen)
  3434. {
  3435. ExecuteFileBy = efShell;
  3436. ExternalEditor = NULL;
  3437. }
  3438. else
  3439. {
  3440. ExecuteFileBy = efExternalEditor;
  3441. ExternalEditor = Editor->Data;
  3442. }
  3443. }
  3444. }
  3445. //---------------------------------------------------------------------------
  3446. void __fastcall TCustomScpExplorerForm::ExecuteFile(TOperationSide Side,
  3447. TExecuteFileBy ExecuteFileBy, const TEditorData * ExternalEditor,
  3448. UnicodeString FullFileName, TObject * Object, const TFileMasks::TParams & MaskParams)
  3449. {
  3450. UnicodeString OriginalFileName;
  3451. UnicodeString LocalRootDirectory;
  3452. UnicodeString RemoteDirectory;
  3453. ExecuteFileNormalize(ExecuteFileBy, ExternalEditor, FullFileName,
  3454. IsSideLocalBrowser(Side), MaskParams);
  3455. UnicodeString Counter;
  3456. UnicodeString LocalFileName;
  3457. bool Handled = false;
  3458. if (!IsSideLocalBrowser(Side))
  3459. {
  3460. // We need to trim VMS version here, so that we use name without version
  3461. // when uploading back to create a new version of the file
  3462. OriginalFileName = Terminal->GetBaseFileName(UnixExtractFileName(FullFileName));
  3463. RemoteDirectory = UnixExtractFilePath(FullFileName);
  3464. TObject * Token = NULL;
  3465. UnicodeString LocalDirectory;
  3466. if (!FEditorManager->CanAddFile(RemoteDirectory, OriginalFileName,
  3467. Terminal->SessionData->SessionName, Token, LocalRootDirectory,
  3468. LocalDirectory))
  3469. {
  3470. if (Token != NULL)
  3471. {
  3472. TForm * Form = dynamic_cast<TForm *>(Token);
  3473. if (Form->WindowState == wsMinimized)
  3474. {
  3475. ShowWindow(Form->Handle, SW_RESTORE);
  3476. }
  3477. else
  3478. {
  3479. Form->SetFocus();
  3480. }
  3481. Handled = true;
  3482. }
  3483. else
  3484. {
  3485. throw Exception(FMTLOAD(ALREADY_EDITED_EXTERNALLY_OR_UPLOADED, (OriginalFileName)));
  3486. }
  3487. }
  3488. if (!Handled)
  3489. {
  3490. TStringList * FileList1 = new TStringList();
  3491. try
  3492. {
  3493. FileList1->AddObject(FullFileName, Object);
  3494. TemporarilyDownloadFiles(FileList1,
  3495. RemoteExecuteForceText(ExecuteFileBy, ExternalEditor),
  3496. LocalRootDirectory, LocalDirectory, true, true, false);
  3497. LocalFileName = LocalDirectory + FileList1->Strings[0];
  3498. }
  3499. __finally
  3500. {
  3501. delete FileList1;
  3502. }
  3503. switch (ExecuteFileBy)
  3504. {
  3505. case efShell:
  3506. Counter = "RemoteFilesExecuted";
  3507. break;
  3508. case efInternalEditor:
  3509. Counter = "RemoteFilesOpenedInInternalEditor";
  3510. break;
  3511. case efExternalEditor:
  3512. Counter = "RemoteFilesOpenedInExternalEditor";
  3513. break;
  3514. default:
  3515. DebugFail();
  3516. }
  3517. }
  3518. }
  3519. else
  3520. {
  3521. LocalFileName = FullFileName;
  3522. OriginalFileName = ExtractFileName(FullFileName);
  3523. switch (ExecuteFileBy)
  3524. {
  3525. case efShell:
  3526. Counter = "LocalFilesExecuted";
  3527. break;
  3528. case efInternalEditor:
  3529. Counter = "LocalFilesOpenedInInternalEditor";
  3530. break;
  3531. case efExternalEditor:
  3532. Counter = "LocalFilesOpenedInExternalEditor";
  3533. break;
  3534. default:
  3535. DebugFail();
  3536. }
  3537. }
  3538. if (!Handled)
  3539. {
  3540. Configuration->Usage->Inc(Counter);
  3541. CustomExecuteFile(Side, ExecuteFileBy, LocalFileName, OriginalFileName,
  3542. ExternalEditor, LocalRootDirectory, RemoteDirectory, false, MaskParams.Modification);
  3543. }
  3544. }
  3545. //---------------------------------------------------------------------------
  3546. void __fastcall TCustomScpExplorerForm::ExecuteFile(TOperationSide Side,
  3547. TExecuteFileBy ExecuteFileBy, const TEditorData * ExternalEditor,
  3548. bool AllSelected, bool OnFocused)
  3549. {
  3550. DebugAssert(!WinConfiguration->DisableOpenEdit);
  3551. DebugAssert((ExecuteFileBy == efExternalEditor) ==
  3552. ((ExternalEditor != NULL) && (ExternalEditor->Editor == edExternal)));
  3553. Side = GetSide(Side);
  3554. TCustomDirView * DView = DirView(Side);
  3555. TStrings * FileList =
  3556. AllSelected ?
  3557. DView->CreateFileList(OnFocused, IsSideLocalBrowser(Side)) :
  3558. DView->CreateFocusedFileList(IsSideLocalBrowser(Side));
  3559. try
  3560. {
  3561. DebugAssert(AllSelected || (FileList->Count == 1));
  3562. for (int i = 0; i < FileList->Count; i++)
  3563. {
  3564. UnicodeString ListFileName = FileList->Strings[i];
  3565. UnicodeString FileNameOnly =
  3566. !IsSideLocalBrowser(Side) ? UnixExtractFileName(ListFileName) : ExtractFileName(ListFileName);
  3567. TListItem * Item = DView->FindFileItem(FileNameOnly);
  3568. if (!DView->ItemIsDirectory(Item))
  3569. {
  3570. UnicodeString FullFileName;
  3571. if (!IsSideLocalBrowser(Side))
  3572. {
  3573. FullFileName = DirView(Side)->Path + ListFileName;
  3574. }
  3575. else
  3576. {
  3577. FullFileName = ListFileName;
  3578. }
  3579. TObject * Object = FileList->Objects[i];
  3580. TFileMasks::TParams MaskParams;
  3581. MaskParams.Size = DView->ItemFileSize(Item);
  3582. TDateTimePrecision Precision;
  3583. MaskParams.Modification = DView->ItemFileTime(Item, Precision);
  3584. std::unique_ptr<TRemoteFile> File;
  3585. if (!IsSideLocalBrowser(Side))
  3586. {
  3587. File.reset(Terminal->TryReadFile(FullFileName));
  3588. if (File.get() != NULL)
  3589. {
  3590. File->FullFileName = FullFileName;
  3591. Object = File.get();
  3592. MaskParams.Size = File->Size;
  3593. MaskParams.Modification = File->Modification;
  3594. }
  3595. }
  3596. ExecuteFile(Side, ExecuteFileBy, ExternalEditor, FullFileName, Object, MaskParams);
  3597. }
  3598. }
  3599. }
  3600. __finally
  3601. {
  3602. delete FileList;
  3603. }
  3604. }
  3605. //---------------------------------------------------------------------------
  3606. void __fastcall TCustomScpExplorerForm::TemporaryFileCopyParam(TCopyParamType & CopyParam)
  3607. {
  3608. CopyParam.FileNameCase = ncNoChange;
  3609. CopyParam.PreserveRights = false;
  3610. CopyParam.PreserveReadOnly = false;
  3611. CopyParam.ReplaceInvalidChars = true;
  3612. CopyParam.IncludeFileMask = TFileMasks();
  3613. CopyParam.NewerOnly = false;
  3614. CopyParam.FileMask = L"";
  3615. }
  3616. //---------------------------------------------------------------------------
  3617. bool TCustomScpExplorerForm::EditorCheckNotModified(const TEditedFileData * Data)
  3618. {
  3619. return WinConfiguration->EditorCheckNotModified && (Data->SourceTimestamp != TDateTime());
  3620. }
  3621. //---------------------------------------------------------------------------
  3622. class TEditorUploadQueueItem : public TUploadQueueItem
  3623. {
  3624. public:
  3625. __fastcall TEditorUploadQueueItem(
  3626. TTerminal * Terminal, TStrings * FilesToCopy, const UnicodeString & TargetDir,
  3627. const TCopyParamType * CopyParam, int Params) :
  3628. TUploadQueueItem(Terminal, FilesToCopy, TargetDir, CopyParam, Params, true, false)
  3629. {
  3630. }
  3631. protected:
  3632. virtual void __fastcall DoTransferExecute(TTerminal * Terminal, TParallelOperation * ParallelOperation)
  3633. {
  3634. TUploadQueueItem::DoTransferExecute(Terminal, ParallelOperation);
  3635. TTerminalManager::Instance()->ScpExplorer->EditedFileUploaded(Terminal, CompleteEvent);
  3636. }
  3637. };
  3638. //---------------------------------------------------------------------------
  3639. void TCustomScpExplorerForm::EditedFileUploaded(TTerminal * ATerminal, HANDLE UploadCompleteEvent)
  3640. {
  3641. if (WinConfiguration->EditorCheckNotModified) // optimization
  3642. {
  3643. UnicodeString RemoteFilePath;
  3644. {
  3645. TGuard Guard(FEditorManager->Section);
  3646. TEditedFileData * Data = FEditorManager->FindByUploadCompleteEvent(UploadCompleteEvent);
  3647. if (DebugAlwaysTrue(Data != NULL) &&
  3648. EditorCheckNotModified(Data))
  3649. {
  3650. RemoteFilePath = UnixCombinePaths(Data->RemoteDirectory, Data->OriginalFileName);
  3651. }
  3652. }
  3653. if (!RemoteFilePath.IsEmpty())
  3654. {
  3655. std::unique_ptr<TRemoteFile> File(ATerminal->TryReadFile(RemoteFilePath));
  3656. if (File.get() != NULL)
  3657. {
  3658. TGuard Guard(FEditorManager->Section);
  3659. TEditedFileData * Data = FEditorManager->FindByUploadCompleteEvent(UploadCompleteEvent);
  3660. if (DebugAlwaysTrue(Data != NULL))
  3661. {
  3662. Data->SourceTimestamp = File->Modification;
  3663. AppLogFmt(L"Uploaded edited remote file timestamp: %s: ", (StandardTimestamp(Data->SourceTimestamp)));
  3664. }
  3665. }
  3666. }
  3667. }
  3668. }
  3669. //---------------------------------------------------------------------------
  3670. void __fastcall TCustomScpExplorerForm::ExecutedFileChanged(
  3671. const UnicodeString & FileName, TEditedFileData * Data, HANDLE UploadCompleteEvent, bool & Retry)
  3672. {
  3673. TTerminalManager * Manager = TTerminalManager::Instance();
  3674. if ((Manager->ActiveTerminal == Data->Terminal) &&
  3675. Manager->ScheduleTerminalReconnnect(Data->Terminal))
  3676. {
  3677. AppLog(L"Scheduled session reconnect, will retry the upload later");
  3678. Retry = true;
  3679. }
  3680. else if (!IsActiveTerminal(Data->Terminal))
  3681. {
  3682. if (!NonVisualDataModule->Busy)
  3683. {
  3684. UnicodeString FileNameOnly = ExtractFileName(FileName);
  3685. UnicodeString EditInactiveSessionReopenAcceptedCounter = L"EditInactiveSessionReopenAccepted";
  3686. UnicodeString EditInactiveSessionReopenRejectedCounter = L"EditInactiveSessionReopenRejected";
  3687. if (Data->Terminal == NULL)
  3688. {
  3689. TManagedTerminal * SameSiteTerminal = Manager->FindActiveTerminalForSite(Data->SessionData);
  3690. if (SameSiteTerminal != NULL)
  3691. {
  3692. UnicodeString Message =
  3693. FMTLOAD(EDIT_SESSION_REATTACH,
  3694. (FileNameOnly, Data->SessionName, FileNameOnly));
  3695. if (MessageDialog(Message, qtConfirmation, qaOK | qaCancel) == qaOK)
  3696. {
  3697. Data->Terminal = SameSiteTerminal;
  3698. Data->Queue = Manager->FindQueueForTerminal(SameSiteTerminal);
  3699. Data->SessionName = SameSiteTerminal->SessionData->SessionName;
  3700. // We might also overwrite session data
  3701. Configuration->Usage->Inc(EditInactiveSessionReopenAcceptedCounter);
  3702. }
  3703. else
  3704. {
  3705. Configuration->Usage->Inc(EditInactiveSessionReopenRejectedCounter);
  3706. Abort();
  3707. }
  3708. }
  3709. }
  3710. // Foreground session should reconnect itself (except for those disconnected explicitly by the user)
  3711. else if (DebugAlwaysTrue(Terminal->Disconnected || (Terminal != Data->Terminal)))
  3712. {
  3713. UnicodeString Message =
  3714. MainInstructions(
  3715. FMTLOAD(EDIT_SESSION_RECONNECT, (Data->SessionName, FileNameOnly)));
  3716. if (MessageDialog(Message, qtConfirmation, qaOK | qaCancel) == qaOK)
  3717. {
  3718. if (Terminal->Disconnected)
  3719. {
  3720. Manager->ActiveSession = Terminal;
  3721. ReconnectSession();
  3722. DebugAssert(Terminal == Data->Terminal);
  3723. Data->Queue = Manager->FindQueueForTerminal(Terminal);
  3724. }
  3725. else
  3726. {
  3727. Manager->SetActiveTerminalWithAutoReconnect(Data->Terminal);
  3728. }
  3729. Configuration->Usage->Inc(EditInactiveSessionReopenAcceptedCounter);
  3730. }
  3731. else
  3732. {
  3733. Configuration->Usage->Inc(EditInactiveSessionReopenRejectedCounter);
  3734. Abort();
  3735. }
  3736. }
  3737. }
  3738. if (!IsActiveTerminal(Data->Terminal))
  3739. {
  3740. Configuration->Usage->Inc(L"EditInactiveSession");
  3741. // Prevent this when not idle (!NonVisualDataModule->Busy)?
  3742. throw Exception(FMTLOAD(EDIT_SESSION_CLOSED2,
  3743. (ExtractFileName(FileName), Data->SessionName)));
  3744. }
  3745. }
  3746. if (!Retry && EditorCheckNotModified(Data))
  3747. {
  3748. if (NonVisualDataModule->Busy)
  3749. {
  3750. Retry = true;
  3751. }
  3752. else
  3753. {
  3754. UnicodeString RemoteFilePath = UnixCombinePaths(Data->RemoteDirectory, Data->OriginalFileName);
  3755. std::unique_ptr<TRemoteFile> File(Terminal->TryReadFile(RemoteFilePath));
  3756. if (File.get() != NULL)
  3757. {
  3758. AppLogFmt(L"Edited remote file timestamp: %s, Original timestamp: %s", (StandardTimestamp(File->Modification), StandardTimestamp(Data->SourceTimestamp)));
  3759. if (File->Modification != Data->SourceTimestamp)
  3760. {
  3761. UnicodeString Message = MainInstructions(LoadStr(EDIT_CHANGED_EXTERNALLY));
  3762. if (MessageDialog(Message, qtConfirmation, qaOK | qaCancel) != qaOK)
  3763. {
  3764. Abort();
  3765. }
  3766. }
  3767. }
  3768. }
  3769. }
  3770. if (!Retry)
  3771. {
  3772. TStrings * FileList = new TStringList();
  3773. try
  3774. {
  3775. FileList->Add(FileName);
  3776. // Consider using the same settings (preset) as when the file was downloaded.
  3777. // More over this does not reflect the actual session that will do the upload.
  3778. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  3779. TemporaryFileCopyParam(CopyParam);
  3780. if (Data->ForceText)
  3781. {
  3782. CopyParam.TransferMode = tmAscii;
  3783. }
  3784. // so i do not need to worry if masking algorithm works in all cases
  3785. // ("" means "copy file name", no masking is actually done)
  3786. if (ExtractFileName(FileName) == Data->OriginalFileName)
  3787. {
  3788. CopyParam.FileMask = L"";
  3789. }
  3790. else
  3791. {
  3792. CopyParam.FileMask = DelimitFileNameMask(Data->OriginalFileName);
  3793. }
  3794. int Params = cpNoConfirmation | cpTemporary;
  3795. if (Data->Terminal->IsCapable[fcBackgroundTransfers])
  3796. {
  3797. DebugAssert(Data->Queue != NULL);
  3798. TQueueItem * QueueItem =
  3799. new TEditorUploadQueueItem(Data->Terminal, FileList, Data->RemoteDirectory, &CopyParam, Params);
  3800. QueueItem->CompleteEvent = UploadCompleteEvent;
  3801. AddQueueItem(Data->Queue, QueueItem, Data->Terminal);
  3802. }
  3803. else
  3804. {
  3805. if (NonVisualDataModule->Busy)
  3806. {
  3807. Retry = true;
  3808. }
  3809. else
  3810. {
  3811. Data->Terminal->CopyToRemote(FileList, Data->RemoteDirectory, &CopyParam, Params, NULL);
  3812. SetEvent(UploadCompleteEvent);
  3813. }
  3814. }
  3815. }
  3816. __finally
  3817. {
  3818. delete FileList;
  3819. }
  3820. }
  3821. }
  3822. //---------------------------------------------------------------------------
  3823. void __fastcall TCustomScpExplorerForm::ExecutedFileReload(
  3824. const UnicodeString & FileName, TEditedFileData * Data)
  3825. {
  3826. // Sanity check, we should not be busy otherwise user would not be able to click Reload button.
  3827. DebugAssert(!NonVisualDataModule->Busy);
  3828. if (!IsActiveTerminal(Data->Terminal))
  3829. {
  3830. throw Exception(FMTLOAD(EDIT_SESSION_CLOSED_RELOAD,
  3831. (ExtractFileName(FileName), Data->SessionName)));
  3832. }
  3833. TManagedTerminal * PrevSession = TTerminalManager::Instance()->ActiveSession;
  3834. TTerminalManager::Instance()->ActiveSession = Data->Terminal;
  3835. NonVisualDataModule->StartBusy();
  3836. try
  3837. {
  3838. std::unique_ptr<TRemoteFile> File;
  3839. UnicodeString RemoteFileName =
  3840. UnixIncludeTrailingBackslash(Data->RemoteDirectory) + Data->OriginalFileName;
  3841. Terminal->ExceptionOnFail = true;
  3842. try
  3843. {
  3844. File.reset(Terminal->ReadFile(RemoteFileName));
  3845. if (!File->HaveFullFileName)
  3846. {
  3847. File->FullFileName = RemoteFileName;
  3848. }
  3849. }
  3850. __finally
  3851. {
  3852. Terminal->ExceptionOnFail = false;
  3853. }
  3854. std::unique_ptr<TStrings> FileList(new TStringList());
  3855. FileList->AddObject(RemoteFileName, File.get());
  3856. UnicodeString RootTempDir = Data->LocalRootDirectory;
  3857. UnicodeString TempDir = ExtractFilePath(FileName);
  3858. TemporarilyDownloadFiles(FileList.get(), Data->ForceText, RootTempDir, TempDir, true, true, false);
  3859. if (EditorCheckNotModified(Data))
  3860. {
  3861. Data->SourceTimestamp = File->Modification;
  3862. }
  3863. // sanity check, the target file name should be still the same
  3864. DebugAssert(ExtractFileName(FileName) == FileList->Strings[0]);
  3865. }
  3866. __finally
  3867. {
  3868. NonVisualDataModule->EndBusy();
  3869. // it actually may not exist anymore...
  3870. TTerminalManager::Instance()->ActiveSession = PrevSession;
  3871. }
  3872. }
  3873. //---------------------------------------------------------------------------
  3874. void __fastcall TCustomScpExplorerForm::ExecutedFileEarlyClosed(
  3875. const TEditedFileData * Data, bool & KeepOpen)
  3876. {
  3877. // Command is set for external editors only (not for "shell" open).
  3878. if (!Data->Command.IsEmpty())
  3879. {
  3880. bool AnyFound = false;
  3881. bool AnyMDI = false;
  3882. bool AnyNonMDI = false;
  3883. bool AnyDetect = false;
  3884. TEditorList * EditorList = new TEditorList();
  3885. try
  3886. {
  3887. *EditorList = *WinConfiguration->EditorList;
  3888. for (int i = 0; i < EditorList->Count; i++)
  3889. {
  3890. const TEditorPreferences * Editor = EditorList->Editors[i];
  3891. if ((Editor->Data->Editor == edExternal) &&
  3892. (Editor->Data->ExternalEditor == Data->Command))
  3893. {
  3894. AnyFound = true;
  3895. if (Editor->Data->SDIExternalEditor)
  3896. {
  3897. AnyNonMDI = true;
  3898. if (Editor->Data->DetectMDIExternalEditor)
  3899. {
  3900. AnyDetect = true;
  3901. }
  3902. }
  3903. else
  3904. {
  3905. AnyMDI = true;
  3906. }
  3907. }
  3908. }
  3909. bool EnableMDI = false;
  3910. bool DisableDetect = false;
  3911. if (AnyMDI)
  3912. {
  3913. KeepOpen = true;
  3914. if (AnyNonMDI)
  3915. {
  3916. // there is at least one instance of the editor with MDI support enabled,
  3917. // and one with disabled, enable it for all instances
  3918. EnableMDI = true;
  3919. }
  3920. }
  3921. else if (AnyFound && !AnyDetect)
  3922. {
  3923. // at least once instance found but all have MDI autodetection disabled
  3924. // => close the file (default action)
  3925. }
  3926. else
  3927. {
  3928. // no instance of the editor has MDI support enabled
  3929. TMessageParams Params;
  3930. if (AnyFound)
  3931. {
  3932. // there is at least one instance of the editor
  3933. Params.Params |= mpNeverAskAgainCheck;
  3934. }
  3935. unsigned int Answer = MessageDialog(FMTLOAD(EDITOR_EARLY_CLOSED2, (Data->OriginalFileName)), qtWarning,
  3936. qaYes | qaNo, HELP_EDITOR_EARLY_CLOSED, &Params);
  3937. switch (Answer)
  3938. {
  3939. case qaNeverAskAgain:
  3940. DisableDetect = true;
  3941. break;
  3942. case qaNo:
  3943. EnableMDI = true;
  3944. KeepOpen = true;
  3945. break;
  3946. }
  3947. }
  3948. if (AnyFound && (EnableMDI || DisableDetect))
  3949. {
  3950. bool Changed = false;
  3951. for (int i = 0; i < EditorList->Count; i++)
  3952. {
  3953. const TEditorPreferences * Editor = EditorList->Editors[i];
  3954. if ((Editor->Data->Editor == edExternal) &&
  3955. (Editor->Data->ExternalEditor == Data->Command) &&
  3956. ((EnableMDI && Editor->Data->SDIExternalEditor) ||
  3957. (DisableDetect && Editor->Data->DetectMDIExternalEditor)))
  3958. {
  3959. Changed = true;
  3960. TEditorPreferences * UpdatedEditor = new TEditorPreferences(*Editor);
  3961. if (EnableMDI)
  3962. {
  3963. UpdatedEditor->GetData()->SDIExternalEditor = false;
  3964. }
  3965. if (DisableDetect)
  3966. {
  3967. UpdatedEditor->GetData()->DetectMDIExternalEditor = false;
  3968. }
  3969. EditorList->Change(i, UpdatedEditor);
  3970. }
  3971. }
  3972. if (Changed)
  3973. {
  3974. WinConfiguration->EditorList = EditorList;
  3975. }
  3976. }
  3977. }
  3978. __finally
  3979. {
  3980. delete EditorList;
  3981. }
  3982. }
  3983. else
  3984. {
  3985. // "open" case
  3986. MessageDialog(FMTLOAD(APP_EARLY_CLOSED, (Data->OriginalFileName)), qtWarning,
  3987. qaOK, HELP_APP_EARLY_CLOSED);
  3988. }
  3989. }
  3990. //---------------------------------------------------------------------------
  3991. void __fastcall TCustomScpExplorerForm::ExecutedFileUploadComplete(TObject * Sender)
  3992. {
  3993. EditorFormFileUploadComplete(DebugNotNull(dynamic_cast<TForm *>(Sender)));
  3994. }
  3995. //---------------------------------------------------------------------------
  3996. void __fastcall TCustomScpExplorerForm::RemoteDirViewEnter(TObject * /*Sender*/)
  3997. {
  3998. SideEnter(osRemote);
  3999. }
  4000. //---------------------------------------------------------------------------
  4001. void __fastcall TCustomScpExplorerForm::RemoteDriveViewEnter(TObject * /*Sender*/)
  4002. {
  4003. MakeNextInTabOrder(RemoteDirPanel, RemoteDrivePanel);
  4004. SideEnter(osRemote);
  4005. }
  4006. //---------------------------------------------------------------------------
  4007. void __fastcall TCustomScpExplorerForm::SideEnter(TOperationSide Side)
  4008. {
  4009. FCurrentSide = Side;
  4010. if (Visible)
  4011. {
  4012. UpdateControls();
  4013. }
  4014. }
  4015. //---------------------------------------------------------------------------
  4016. void __fastcall TCustomScpExplorerForm::FileDeleted(TOperationSide Side, const UnicodeString & FileName, bool Success)
  4017. {
  4018. VisualiseOperationFinished(Side, FileName, Success);
  4019. if (DebugAlwaysTrue(FDeletedFiles != NULL) && Success)
  4020. {
  4021. FDeletedFiles->Add(FileName);
  4022. }
  4023. }
  4024. //---------------------------------------------------------------------------
  4025. void __fastcall TCustomScpExplorerForm::DeleteFiles(TOperationSide Side,
  4026. TStrings * FileList, bool Alternative)
  4027. {
  4028. TCustomDirView * DView = DirView(Side);
  4029. DView->SaveSelection();
  4030. DView->SaveSelectedNames();
  4031. DebugAssert(!FAlternativeDelete);
  4032. FAlternativeDelete = Alternative;
  4033. try
  4034. {
  4035. if (!IsSideLocalBrowser(Side))
  4036. {
  4037. DebugAssert(Terminal != NULL);
  4038. // Clone the file list as it may refer to current directory files, which get destroyed, when the directory is reloaded after the operation
  4039. std::unique_ptr<TStrings> PermanentFileList(TRemoteFileList::CloneStrings(FileList));
  4040. std::unique_ptr<TStringList> DeletedFiles(new TStringList());
  4041. DeletedFiles->CaseSensitive = DebugAlwaysTrue(!IsSideLocalBrowser(Side));
  4042. int Params = FLAGMASK(Alternative, dfAlternative);
  4043. try
  4044. {
  4045. TValueRestorer<TStrings *> DeletedFilesRestorer(FDeletedFiles);
  4046. DebugAssert(FDeletedFiles == NULL);
  4047. FDeletedFiles = DeletedFiles.get();
  4048. TValueRestorer<TFileOperationFinishedEvent> OnFileOperationFinishedRestorer(FOnFileOperationFinished);
  4049. FOnFileOperationFinished = FileDeleted;
  4050. FMoveToQueue = false;
  4051. Terminal->DeleteFiles(FileList, Params);
  4052. // Probably not needed for deleting, just for consistency with transfer code
  4053. if (FMoveToQueue)
  4054. {
  4055. Abort();
  4056. }
  4057. }
  4058. catch (EAbort &)
  4059. {
  4060. if (FMoveToQueue)
  4061. {
  4062. DeletedFiles->Sorted = true;
  4063. for (int Index = 0; Index < PermanentFileList->Count; Index++)
  4064. {
  4065. if (DeletedFiles->IndexOf(PermanentFileList->Strings[Index]) >= 0)
  4066. {
  4067. // We should always be deleting the first item => what can be used to optimize this code, if needed
  4068. DebugAssert(Index == 0);
  4069. PermanentFileList->Delete(Index);
  4070. Index--;
  4071. }
  4072. }
  4073. Configuration->Usage->Inc("MovesToBackgroundDelete");
  4074. TQueueItem * QueueItem = new TDeleteQueueItem(Terminal, PermanentFileList.get(), Params);
  4075. AddQueueItem(Queue, QueueItem, Terminal);
  4076. ClearOperationSelection(osRemote);
  4077. }
  4078. }
  4079. }
  4080. else
  4081. {
  4082. if (IsLocalBrowserMode())
  4083. {
  4084. Configuration->Usage->Inc(L"LocalLocalDeletes");
  4085. }
  4086. TValueRestorer<TOperationSide> ProgressSideRestorer(FProgressSide);
  4087. FProgressSide = FCurrentSide;
  4088. try
  4089. {
  4090. TTerminalManager::Instance()->LocalTerminal->DeleteLocalFiles(FileList, FLAGMASK(Alternative, dfAlternative));
  4091. }
  4092. __finally
  4093. {
  4094. ReloadLocalDirectory();
  4095. }
  4096. }
  4097. FAlternativeDelete = false;
  4098. }
  4099. catch(...)
  4100. {
  4101. FAlternativeDelete = false;
  4102. DView->DiscardSavedSelection();
  4103. throw;
  4104. }
  4105. DView->RestoreSelection();
  4106. }
  4107. //---------------------------------------------------------------------------
  4108. void __fastcall TCustomScpExplorerForm::LockFiles(TStrings * FileList, bool Lock)
  4109. {
  4110. DebugAssert(Terminal);
  4111. DebugAssert(!IsLocalBrowserMode());
  4112. RemoteDirView->SaveSelection();
  4113. RemoteDirView->SaveSelectedNames();
  4114. try
  4115. {
  4116. if (Lock)
  4117. {
  4118. Terminal->LockFiles(FileList);
  4119. }
  4120. else
  4121. {
  4122. Terminal->UnlockFiles(FileList);
  4123. }
  4124. }
  4125. catch(...)
  4126. {
  4127. RemoteDirView->DiscardSavedSelection();
  4128. throw;
  4129. }
  4130. RemoteDirView->RestoreSelection();
  4131. }
  4132. //---------------------------------------------------------------------------
  4133. bool TCustomScpExplorerForm::DoDirectoryExists(void * Session, const UnicodeString & Directory)
  4134. {
  4135. bool Result = false;
  4136. TTerminal * ATerminal = (Session == NULL) ? Terminal : reinterpret_cast<TTerminal *>(Session);
  4137. if (IsActiveTerminal(ATerminal))
  4138. {
  4139. std::unique_ptr<TRemoteFile> File(ATerminal->TryReadFile(Directory));
  4140. Result = File && File->IsDirectory;
  4141. }
  4142. return Result;
  4143. }
  4144. //---------------------------------------------------------------------------
  4145. bool __fastcall TCustomScpExplorerForm::RemoteTransferDialog(TManagedTerminal *& Session,
  4146. TStrings * FileList, UnicodeString & Target, UnicodeString & FileMask, bool & DirectCopy,
  4147. bool NoConfirmation, bool Move)
  4148. {
  4149. DebugAssert(Terminal != NULL);
  4150. DebugAssert(Terminal == TTerminalManager::Instance()->ActiveTerminal);
  4151. DebugAssert(!IsLocalBrowserMode());
  4152. if (Session == NULL)
  4153. {
  4154. Session = Terminal;
  4155. }
  4156. if (Session == Terminal)
  4157. {
  4158. if (RemoteDriveView->DropTarget != NULL)
  4159. {
  4160. Target = RemoteDriveView->NodePathName(RemoteDriveView->DropTarget);
  4161. }
  4162. else if (RemoteDirView->DropTarget != NULL)
  4163. {
  4164. DebugAssert(RemoteDirView->ItemIsDirectory(RemoteDirView->DropTarget));
  4165. Target = RemoteDirView->ItemFullFileName(RemoteDirView->DropTarget);
  4166. }
  4167. else
  4168. {
  4169. Target = RemoteDirView->Path;
  4170. }
  4171. }
  4172. else
  4173. {
  4174. Target = GetSessionPath(Session, osRemote);
  4175. }
  4176. Target = UnixIncludeTrailingBackslash(Target);
  4177. if (FileList->Count == 1)
  4178. {
  4179. FileMask = DelimitFileNameMask(UnixExtractFileName(FileList->Strings[0]));
  4180. }
  4181. else
  4182. {
  4183. FileMask = AnyMask;
  4184. }
  4185. DirectCopy = Terminal->IsCapable[fcRemoteCopy] || Terminal->IsCapable[fcSecondaryShell];
  4186. bool Result = true;
  4187. if (!NoConfirmation)
  4188. {
  4189. bool Multi = (FileList->Count > 1);
  4190. if (Move)
  4191. {
  4192. Result = DoRemoteMoveDialog(Multi, Target, FileMask, DoDirectoryExists);
  4193. }
  4194. else
  4195. {
  4196. std::unique_ptr<TStrings> Sessions(new TStringList);
  4197. std::unique_ptr<TStrings> Directories(new TStringList);
  4198. TStrings * SessionList = TTerminalManager::Instance()->SessionList;
  4199. for (int Index = 0; Index < SessionList->Count; Index++)
  4200. {
  4201. TManagedTerminal * ASession = DebugNotNull(dynamic_cast<TManagedTerminal *>(SessionList->Objects[Index]));
  4202. if (IsActiveTerminal(ASession) && DebugAlwaysTrue(!ASession->LocalBrowser))
  4203. {
  4204. Sessions->AddObject(SessionList->Strings[Index], ASession);
  4205. Directories->Add(GetSessionPath(ASession, osRemote));
  4206. }
  4207. }
  4208. TDirectRemoteCopy AllowDirectCopy;
  4209. if (Terminal->IsCapable[fcRemoteCopy] || Terminal->CommandSessionOpened)
  4210. {
  4211. DebugAssert(DirectCopy);
  4212. AllowDirectCopy = drcAllow;
  4213. }
  4214. else if (Terminal->IsCapable[fcSecondaryShell])
  4215. {
  4216. DebugAssert(DirectCopy);
  4217. AllowDirectCopy = drcConfirmCommandSession;
  4218. }
  4219. else
  4220. {
  4221. DebugAssert(!DirectCopy);
  4222. AllowDirectCopy = drcDisallow;
  4223. }
  4224. void * ASession = Session;
  4225. Result = DoRemoteCopyDialog(
  4226. Sessions.get(), Directories.get(), AllowDirectCopy, Multi, ASession, Target, FileMask, DirectCopy, Terminal,
  4227. DoDirectoryExists);
  4228. Session = static_cast<TManagedTerminal *>(ASession);
  4229. }
  4230. }
  4231. return Result;
  4232. }
  4233. //---------------------------------------------------------------------------
  4234. bool __fastcall TCustomScpExplorerForm::RemoteTransferFiles(
  4235. TStrings * FileList, bool NoConfirmation, bool Move, TManagedTerminal * Session)
  4236. {
  4237. DebugAssert(!IsLocalBrowserMode());
  4238. bool DirectCopy;
  4239. UnicodeString Target, FileMask;
  4240. bool Result = RemoteTransferDialog(Session, FileList, Target, FileMask, DirectCopy, NoConfirmation, Move);
  4241. if (Result)
  4242. {
  4243. if (!Move && !DirectCopy)
  4244. {
  4245. Configuration->Usage->Inc("RemoteCopyTemp");
  4246. UnicodeString RootTempDir;
  4247. UnicodeString TempDir;
  4248. TemporarilyDownloadFiles(FileList, false, RootTempDir, TempDir, false, false, true);
  4249. TStrings * TemporaryFilesList = new TStringList();
  4250. try
  4251. {
  4252. TMakeLocalFileListParams MakeFileListParam;
  4253. MakeFileListParam.FileList = TemporaryFilesList;
  4254. MakeFileListParam.FileTimes = NULL;
  4255. MakeFileListParam.IncludeDirs = true;
  4256. MakeFileListParam.Recursive = false;
  4257. ProcessLocalDirectory(TempDir, Terminal->MakeLocalFileList, &MakeFileListParam);
  4258. TTerminalManager::Instance()->ActiveSession = Session;
  4259. if (TemporaryFilesList->Count > 0)
  4260. {
  4261. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  4262. CopyParam.FileMask = FileMask;
  4263. DebugAssert(!FAutoOperation);
  4264. FAutoOperation = true;
  4265. Terminal->CopyToRemote(TemporaryFilesList, Target, &CopyParam, cpTemporary, NULL);
  4266. }
  4267. }
  4268. __finally
  4269. {
  4270. delete TemporaryFilesList;
  4271. FAutoOperation = false;
  4272. if (!RootTempDir.IsEmpty())
  4273. {
  4274. RecursiveDeleteFile(ExcludeTrailingBackslash(RootTempDir));
  4275. }
  4276. }
  4277. }
  4278. else
  4279. {
  4280. RemoteDirView->SaveSelection();
  4281. RemoteDirView->SaveSelectedNames();
  4282. try
  4283. {
  4284. if (Move)
  4285. {
  4286. Configuration->Usage->Inc("RemoteMove");
  4287. Terminal->MoveFiles(FileList, Target, FileMask, false);
  4288. }
  4289. else
  4290. {
  4291. Configuration->Usage->Inc("RemoteCopyDirect");
  4292. DebugAssert(DirectCopy);
  4293. DebugAssert(Session == Terminal);
  4294. if (Terminal->IsCapable[fcRemoteCopy] ||
  4295. Terminal->CommandSessionOpened ||
  4296. CommandSessionFallback())
  4297. {
  4298. Terminal->CopyFiles(FileList, Target, FileMask);
  4299. }
  4300. }
  4301. }
  4302. catch(...)
  4303. {
  4304. RemoteDirView->DiscardSavedSelection();
  4305. throw;
  4306. }
  4307. RemoteDirView->RestoreSelection();
  4308. }
  4309. }
  4310. return Result;
  4311. }
  4312. //---------------------------------------------------------------------------
  4313. void __fastcall TCustomScpExplorerForm::CreateRemoteDirectory(
  4314. const UnicodeString & Path, TRemoteProperties & Properties)
  4315. {
  4316. DebugAssert(!IsLocalBrowserMode());
  4317. Properties.Valid = Properties.Valid << vpEncrypt;
  4318. Properties.Encrypt = GUIConfiguration->CurrentCopyParam.EncryptNewFiles;
  4319. RemoteDirView->CreateDirectoryEx(Path, &Properties);
  4320. }
  4321. //---------------------------------------------------------------------------
  4322. void __fastcall TCustomScpExplorerForm::CreateDirectory(TOperationSide Side)
  4323. {
  4324. Side = GetSide(Side);
  4325. TRemoteProperties Properties = GUIConfiguration->NewDirectoryProperties;
  4326. TRemoteProperties * AProperties = (!IsSideLocalBrowser(Side) ? &Properties : NULL);
  4327. UnicodeString Name = LoadStr(NEW_FOLDER);
  4328. int AllowedChanges =
  4329. FLAGMASK(!IsSideLocalBrowser(Side) && Terminal->IsCapable[fcModeChanging], cpMode);
  4330. bool SaveSettings = false;
  4331. if (DoCreateDirectoryDialog(Name, AProperties, AllowedChanges, SaveSettings))
  4332. {
  4333. TWindowLock Lock(this);
  4334. if (!IsSideLocalBrowser(Side))
  4335. {
  4336. if (SaveSettings)
  4337. {
  4338. GUIConfiguration->NewDirectoryProperties = Properties;
  4339. }
  4340. CreateRemoteDirectory(Name, Properties);
  4341. }
  4342. else
  4343. {
  4344. DirView(Side)->CreateDirectory(Name);
  4345. }
  4346. }
  4347. }
  4348. //---------------------------------------------------------------------------
  4349. void __fastcall TCustomScpExplorerForm::HomeDirectory(TOperationSide Side)
  4350. {
  4351. TWindowLock Lock(this);
  4352. DirView(Side)->ExecuteHomeDirectory();
  4353. }
  4354. //---------------------------------------------------------------------------
  4355. void __fastcall TCustomScpExplorerForm::OpenDirectory(TOperationSide Side)
  4356. {
  4357. DoOpenDirectoryDialog(odBrowse, Side);
  4358. }
  4359. //---------------------------------------------------------------------------
  4360. bool __fastcall TCustomScpExplorerForm::OpenBookmark(TOperationSide Side, TBookmark * Bookmark)
  4361. {
  4362. TOperationSide BookmarkSide = IsLocalBrowserMode() ? osLocal : Side;
  4363. UnicodeString Path = Bookmark->GetSideDirectory(BookmarkSide);
  4364. bool Result = !Path.IsEmpty();
  4365. if (Result)
  4366. {
  4367. TryOpenDirectory(Side, Path);
  4368. }
  4369. return Result;
  4370. }
  4371. //---------------------------------------------------------------------------
  4372. void __fastcall TCustomScpExplorerForm::CalculateSize(
  4373. TStrings * FileList, __int64 & Size, TCalculateSizeStats & Stats,
  4374. bool & Close)
  4375. {
  4376. // terminal can be already closed (e.g. dropped connection)
  4377. if (Terminal != NULL)
  4378. {
  4379. try
  4380. {
  4381. TCalculateSizeParams Params;
  4382. Params.Stats = &Stats;
  4383. Terminal->CalculateFilesSize(FileList, Size, Params);
  4384. }
  4385. catch(...)
  4386. {
  4387. if (!Terminal->Active)
  4388. {
  4389. Close = true;
  4390. }
  4391. throw;
  4392. }
  4393. }
  4394. }
  4395. //---------------------------------------------------------------------------
  4396. void __fastcall TCustomScpExplorerForm::CalculateChecksum(const UnicodeString & Alg,
  4397. TStrings * FileList, TCalculatedChecksumEvent OnCalculatedChecksum,
  4398. bool & Close)
  4399. {
  4400. if ((Terminal != NULL) && // terminal can be already closed (e.g. dropped connection)
  4401. EnsureCommandSessionFallback(fcCalculatingChecksum))
  4402. {
  4403. Configuration->Usage->Inc(L"ChecksumCalculated");
  4404. try
  4405. {
  4406. Terminal->CalculateFilesChecksum(Alg, FileList, OnCalculatedChecksum);
  4407. }
  4408. catch(...)
  4409. {
  4410. if (!Terminal->Active)
  4411. {
  4412. Close = true;
  4413. }
  4414. throw;
  4415. }
  4416. }
  4417. }
  4418. //---------------------------------------------------------------------------
  4419. void TCustomScpExplorerForm::LoadFilesProperties(TStrings * FileList)
  4420. {
  4421. if (Terminal->LoadFilesProperties(FileList))
  4422. {
  4423. RemoteDirView->Invalidate();
  4424. }
  4425. }
  4426. //---------------------------------------------------------------------------
  4427. bool __fastcall TCustomScpExplorerForm::SetProperties(TOperationSide Side, TStrings * FileList)
  4428. {
  4429. bool Result;
  4430. if (!IsSideLocalBrowser(Side))
  4431. {
  4432. TRemoteTokenList * GroupList = NULL;
  4433. TRemoteTokenList * UserList = NULL;
  4434. try
  4435. {
  4436. TRemoteProperties CurrentProperties;
  4437. LoadFilesProperties(FileList);
  4438. bool CapableGroupChanging = Terminal->IsCapable[fcGroupChanging];
  4439. bool CapableOwnerChanging = Terminal->IsCapable[fcOwnerChanging];
  4440. if (CapableGroupChanging || CapableOwnerChanging)
  4441. {
  4442. if (CapableGroupChanging)
  4443. {
  4444. GroupList = Terminal->Groups->Duplicate();
  4445. }
  4446. if (CapableOwnerChanging)
  4447. {
  4448. UserList = Terminal->Users->Duplicate();
  4449. }
  4450. TRemoteDirectory * Files = Terminal->Files;
  4451. int Count = Files->Count;
  4452. if (Count > 100)
  4453. {
  4454. Count = 100;
  4455. }
  4456. for (int Index = 0; Index < Count; Index++)
  4457. {
  4458. TRemoteFile * File = Files->Files[Index];
  4459. if (CapableGroupChanging)
  4460. {
  4461. GroupList->AddUnique(File->Group);
  4462. }
  4463. if (CapableOwnerChanging)
  4464. {
  4465. UserList->AddUnique(File->Owner);
  4466. }
  4467. }
  4468. // if we haven't collected tokens for all files in current directory,
  4469. // make sure we collect them at least for all selected files.
  4470. // (note that so far the files in FileList has to be from current direcotry)
  4471. if (Count < Files->Count)
  4472. {
  4473. for (int Index = 0; Index < FileList->Count; Index++)
  4474. {
  4475. TRemoteFile * File = (TRemoteFile *)(FileList->Objects[Index]);
  4476. if (CapableGroupChanging)
  4477. {
  4478. GroupList->AddUnique(File->Group);
  4479. }
  4480. if (CapableOwnerChanging)
  4481. {
  4482. UserList->AddUnique(File->Owner);
  4483. }
  4484. }
  4485. }
  4486. }
  4487. CurrentProperties = TRemoteProperties::CommonProperties(FileList);
  4488. bool CapableAclChanging = Terminal->IsCapable[fcAclChangingFiles];
  4489. for (int Index = 0; (Index < FileList->Count) && CapableAclChanging; Index++)
  4490. {
  4491. if (dynamic_cast<TRemoteFile *>(FileList->Objects[Index])->IsDirectory)
  4492. {
  4493. CapableAclChanging = false;
  4494. CurrentProperties.Valid = CurrentProperties.Valid >> vpRights;
  4495. }
  4496. }
  4497. int Flags = 0;
  4498. if (Terminal->IsCapable[fcModeChanging]) Flags |= cpMode;
  4499. if (CapableAclChanging) Flags |= cpAcl;
  4500. if (CapableOwnerChanging) Flags |= cpOwner;
  4501. if (CapableGroupChanging) Flags |= cpGroup;
  4502. TCalculateChecksumEvent CalculateChecksumEvent = NULL;
  4503. if (Terminal->IsCapable[fcCalculatingChecksum] ||
  4504. (Terminal->IsCapable[fcSecondaryShell] && !Terminal->IsEncryptingFiles()))
  4505. {
  4506. CalculateChecksumEvent = CalculateChecksum;
  4507. }
  4508. std::unique_ptr<TStrings> ChecksumAlgs(new TStringList());
  4509. Terminal->GetSupportedChecksumAlgs(ChecksumAlgs.get());
  4510. TRemoteProperties NewProperties = CurrentProperties;
  4511. Result =
  4512. DoPropertiesDialog(FileList, RemoteDirView->PathName,
  4513. GroupList, UserList, ChecksumAlgs.get(), &NewProperties, Flags,
  4514. Terminal->IsCapable[fcGroupOwnerChangingByID],
  4515. CalculateSize, CalculateChecksumEvent);
  4516. if (Result)
  4517. {
  4518. NewProperties = TRemoteProperties::ChangedProperties(CurrentProperties, NewProperties);
  4519. Terminal->ChangeFilesProperties(FileList, &NewProperties);
  4520. }
  4521. }
  4522. __finally
  4523. {
  4524. delete GroupList;
  4525. delete UserList;
  4526. }
  4527. }
  4528. else
  4529. {
  4530. DirView(Side)->DisplayPropertiesMenu();
  4531. Result = true;
  4532. }
  4533. return Result;
  4534. }
  4535. //---------------------------------------------------------------------------
  4536. void __fastcall TCustomScpExplorerForm::KeyProcessed(Word & Key, TShiftState Shift)
  4537. {
  4538. if (Shift * AllKeyShiftStates() == (TShiftState() << ssAlt))
  4539. {
  4540. FIgnoreNextDialogChar = Key;
  4541. }
  4542. Key = 0;
  4543. }
  4544. //---------------------------------------------------------------------------
  4545. void __fastcall TCustomScpExplorerForm::CheckCustomCommandShortCut(
  4546. TCustomCommandList * List, Word & Key, Classes::TShiftState Shift, TShortCut KeyShortCut)
  4547. {
  4548. const TCustomCommandType * Command = List->Find(KeyShortCut);
  4549. if (Command != NULL)
  4550. {
  4551. KeyProcessed(Key, Shift);
  4552. if (CustomCommandState(*Command, false, ccltAll) > 0)
  4553. {
  4554. TAutoFlag DontCopyCommandToClipboardFlag(DontCopyCommandToClipboard);
  4555. ExecuteFileOperationCommand(foCustomCommand, osRemote,
  4556. false, false, const_cast<TCustomCommandType *>(Command));
  4557. }
  4558. }
  4559. }
  4560. //---------------------------------------------------------------------------
  4561. void __fastcall TCustomScpExplorerForm::KeyDown(Word & Key, Classes::TShiftState Shift)
  4562. {
  4563. if (!NonVisualDataModule->Busy)
  4564. {
  4565. if (QueueView3->Focused() && (QueueView3->OnKeyDown != NULL))
  4566. {
  4567. QueueView3->OnKeyDown(QueueView3, Key, Shift);
  4568. }
  4569. if ((Key != 0) && !DirView(osCurrent)->IsEditing())
  4570. {
  4571. TShortCut KeyShortCut = ShortCut(Key, Shift);
  4572. for (int Index = 0; Index < NonVisualDataModule->ExplorerActions->ActionCount; Index++)
  4573. {
  4574. // Note that handling shortcuts on panel (current control) popup menu has precendence over this.
  4575. // But so far it's not a problem, as it does what we want.
  4576. TAction * Action = (TAction *)NonVisualDataModule->ExplorerActions->Actions[Index];
  4577. if (((Action->ShortCut == KeyShortCut) ||
  4578. (Action->SecondaryShortCuts->IndexOfShortCut(KeyShortCut) >= 0)) &&
  4579. AllowedAction(Action, aaShortCut))
  4580. {
  4581. // Has to be called before the action as the dialog char is already in queue.
  4582. // So when the action consumes message queue, we already need to have the
  4583. // FIgnoreNextDialogChar set
  4584. KeyProcessed(Key, Shift);
  4585. // Reset reference to previous component (when menu/toolbar was clicked).
  4586. // Needed to detect that action was invoked by keyboard shortcut
  4587. // in TNonVisualDataModule::ExplorerActionsExecute
  4588. Action->ActionComponent = NULL;
  4589. Action->Execute();
  4590. return;
  4591. }
  4592. }
  4593. for (int i = 0; i < TTerminalManager::Instance()->Count; i++)
  4594. {
  4595. if (NonVisualDataModule->OpenSessionShortCut(i) == KeyShortCut)
  4596. {
  4597. KeyProcessed(Key, Shift);
  4598. TTerminalManager::Instance()->ActiveSessionIndex = i;
  4599. return;
  4600. }
  4601. }
  4602. if (Key == VK_TAB && Shift.Contains(ssCtrl))
  4603. {
  4604. KeyProcessed(Key, Shift);
  4605. TTerminalManager::Instance()->CycleTerminals(!Shift.Contains(ssShift));
  4606. }
  4607. TShortCut CustomShortCut = NormalizeCustomShortCut(KeyShortCut);
  4608. if (IsCustomShortCut(CustomShortCut))
  4609. {
  4610. CheckCustomCommandShortCut(WinConfiguration->CustomCommandList, Key, Shift, CustomShortCut);
  4611. CheckCustomCommandShortCut(WinConfiguration->ExtensionList, Key, Shift, CustomShortCut);
  4612. if (WinConfiguration->SharedBookmarks != NULL)
  4613. {
  4614. TBookmark * Bookmark = WinConfiguration->SharedBookmarks->FindByShortCut(CustomShortCut);
  4615. if ((Bookmark != NULL) &&
  4616. OpenBookmark(FCurrentSide, Bookmark))
  4617. {
  4618. KeyProcessed(Key, Shift);
  4619. }
  4620. }
  4621. }
  4622. }
  4623. }
  4624. TForm::KeyDown(Key, Shift);
  4625. }
  4626. //---------------------------------------------------------------------------
  4627. void __fastcall TCustomScpExplorerForm::InitStatusBar()
  4628. {
  4629. bool OpenedTerminal = false;
  4630. UnicodeString ProtocolName;
  4631. UnicodeString ProtocolNameHint;
  4632. UnicodeString SecurityProtocolName;
  4633. UnicodeString DurationHint;
  4634. // See the comment in TScpCommanderForm::UpdateControls()
  4635. TTerminal * ActiveTerminal = TTerminalManager::Instance()->ActiveTerminal;
  4636. if (ActiveTerminal != NULL)
  4637. {
  4638. // Active is not enough here
  4639. if (ActiveTerminal->Status == ssOpened)
  4640. {
  4641. const TSessionInfo & SessionInfo = ActiveTerminal->GetSessionInfo();
  4642. const TFileSystemInfo & FileSystemInfo = ActiveTerminal->GetFileSystemInfo();
  4643. if (FileSystemInfo.ProtocolName.IsEmpty())
  4644. {
  4645. ProtocolName = SessionInfo.ProtocolName;
  4646. }
  4647. else
  4648. {
  4649. ProtocolName = FileSystemInfo.ProtocolName;
  4650. }
  4651. ProtocolNameHint = LoadStr(STATUS_PROTOCOL_HINT);
  4652. SecurityProtocolName = SessionInfo.SecurityProtocolName;
  4653. DurationHint = LoadStr(STATUS_DURATION_HINT);
  4654. OpenedTerminal = true;
  4655. }
  4656. else if (ActiveTerminal->Status > ssClosed)
  4657. {
  4658. ProtocolName = LoadStr(STATUS_CONNECTING);
  4659. }
  4660. else
  4661. {
  4662. ProtocolName = LoadStr(STATUS_NOT_CONNECTED2);
  4663. }
  4664. }
  4665. TTBXStatusBar * SessionStatusBar = (TTBXStatusBar *)GetComponent(fcStatusBar);
  4666. int Offset = SessionStatusBar->Panels->Count - SessionPanelCount;
  4667. TTBXStatusPanel * ProtocolStatusPanel = SessionStatusBar->Panels->Items[Offset + 0];
  4668. ProtocolStatusPanel->ViewPriority = (ActiveTerminal != NULL) ? 100 : 0;
  4669. if (ProtocolStatusPanel->Caption != ProtocolName)
  4670. {
  4671. ProtocolStatusPanel->Caption = ProtocolName;
  4672. std::unique_ptr<TCanvas> Canvas(CreateControlCanvas(SessionStatusBar));
  4673. ProtocolStatusPanel->Size = Canvas->TextWidth(ProtocolName) + ScaleByTextHeight(SessionStatusBar, 32);
  4674. }
  4675. ProtocolStatusPanel->Hint = ProtocolNameHint;
  4676. TTBXStatusPanel * SecurityStatusPanel = SessionStatusBar->Panels->Items[Offset + 1];
  4677. SecurityStatusPanel->ViewPriority = OpenedTerminal ? 98 : 0;
  4678. bool SecurityEnabled = !SecurityProtocolName.IsEmpty();
  4679. SecurityStatusPanel->Enabled = SecurityEnabled;
  4680. UnicodeString SecurityProtocolHint;
  4681. if (SecurityEnabled)
  4682. {
  4683. SecurityProtocolHint = FMTLOAD(STATUS_SECURE, (SecurityProtocolName));
  4684. }
  4685. else
  4686. {
  4687. SecurityProtocolHint = LoadStr(STATUS_INSECURE);
  4688. }
  4689. SecurityStatusPanel->Hint = SecurityProtocolHint;
  4690. TTBXStatusPanel * DurationStatusPanel = SessionStatusBar->Panels->Items[Offset + 2];
  4691. DurationStatusPanel->ViewPriority = OpenedTerminal ? 99 : 0;
  4692. DurationStatusPanel->Hint = DurationHint;
  4693. UpdateStatusBar();
  4694. }
  4695. //---------------------------------------------------------------------------
  4696. void __fastcall TCustomScpExplorerForm::UpdateStatusBar()
  4697. {
  4698. TTBXStatusBar * SessionStatusBar = (TTBXStatusBar *)GetComponent(fcStatusBar);
  4699. DebugAssert(SessionStatusBar != NULL);
  4700. TTBXStatusPanel * NoteStatusPanel = SessionStatusBar->Panels->Items[0];
  4701. if (!FNote.IsEmpty())
  4702. {
  4703. NoteStatusPanel->Caption = FNote;
  4704. }
  4705. else
  4706. {
  4707. UpdateStatusPanelText(NoteStatusPanel);
  4708. }
  4709. NoteStatusPanel->Hint = FNoteHints;
  4710. UnicodeString LoginTime;
  4711. // See the comment in TScpCommanderForm::UpdateControls()
  4712. TTerminal * ActiveTerminal = TTerminalManager::Instance()->ActiveTerminal;
  4713. // Active is not enough here
  4714. if ((ActiveTerminal != NULL) && (ActiveTerminal->Status == ssOpened))
  4715. {
  4716. const TSessionInfo & SessionInfo = ActiveTerminal->GetSessionInfo();
  4717. LoginTime = FormatDateTimeSpan(Configuration->TimeFormat, Now() - SessionInfo.LoginTime);
  4718. }
  4719. TTBXStatusPanel * DurationStatusPanel = SessionStatusBar->Panels->Items[SessionStatusBar->Panels->Count - 1];
  4720. DurationStatusPanel->Caption = LoginTime;
  4721. }
  4722. //---------------------------------------------------------------------------
  4723. void __fastcall TCustomScpExplorerForm::UpdateStatusPanelText(TTBXStatusPanel * Panel)
  4724. {
  4725. Panel->Caption = L"";
  4726. }
  4727. //---------------------------------------------------------------------------
  4728. void __fastcall TCustomScpExplorerForm::Idle()
  4729. {
  4730. if (FShowing ||
  4731. // Particularly for
  4732. // - Standalone editing
  4733. // - Detecting closed connection and automatically reconnect it while waiting for changes
  4734. // while "keeping remote directory up to date".
  4735. StandaloneOperation)
  4736. {
  4737. FEditorManager->Check();
  4738. // make sure that Idle is called before update queue, as it may invoke QueueEvent
  4739. // that needs to know if queue view is visible (and it may be closed after queue update)
  4740. TTerminalManager::Instance()->Idle(FDoNotIdleCurrentTerminal > 0);
  4741. }
  4742. if (FShowing)
  4743. {
  4744. if (!NonVisualDataModule->Busy &&
  4745. // Menu is opened or drag&drop is going on
  4746. (Mouse->Capture == NULL))
  4747. {
  4748. if (FRefreshRemoteDirectory)
  4749. {
  4750. if ((Terminal != NULL) && (Terminal->Status == ssOpened))
  4751. {
  4752. Terminal->RefreshDirectory();
  4753. }
  4754. FRefreshRemoteDirectory = false;
  4755. }
  4756. if (FRefreshLocalDirectory)
  4757. {
  4758. ReloadLocalDirectory();
  4759. FRefreshLocalDirectory = false;
  4760. }
  4761. if (WinConfiguration->RefreshRemotePanel)
  4762. {
  4763. if ((Terminal != NULL) && (Terminal->Status == ssOpened) &&
  4764. (Now() - Terminal->DirectoryLoaded > WinConfiguration->RefreshRemotePanelInterval))
  4765. {
  4766. RemoteDirView->ReloadDirectory();
  4767. }
  4768. }
  4769. }
  4770. // TB2 shows accels on Alt key down, but hides it only after menu modal loop finishes.
  4771. // But if Alt is used to switch apps or for keyboard shortcut, the menu modal loop does not happen and accelerators stay displayed.
  4772. // It's difficult to fix this in TB2, as there's no "up" equivalent to CM_DIALOGKEY, which shows the accels.
  4773. // And the Alt key can be released even when other window or application is active, so handling the key up in explorer window won't do either.
  4774. TTBCustomToolbar * MenuToolbar = DebugNotNull(dynamic_cast<TTBCustomToolbar *>(GetComponent(fcMenu)));
  4775. MenuToolbar->Idle();
  4776. }
  4777. if (FShowing || StandaloneOperation)
  4778. {
  4779. if (FQueueStatusInvalidated)
  4780. {
  4781. UpdateQueueStatus(false);
  4782. }
  4783. RefreshQueueItems();
  4784. }
  4785. if (FShowing)
  4786. {
  4787. UpdateStatusBar();
  4788. }
  4789. if (FClipboardFakeMonitorsPendingReset && !FDownloadingFromClipboard)
  4790. {
  4791. FClipboardFakeMonitorsPendingReset = false;
  4792. FClipboardFakeMonitors.reset(NULL);
  4793. }
  4794. FIgnoreNextDialogChar = 0;
  4795. }
  4796. //---------------------------------------------------------------------------
  4797. void __fastcall TCustomScpExplorerForm::UserActionTimer(TObject * /*Sender*/)
  4798. {
  4799. try
  4800. {
  4801. FUserActionTimer->Enabled = false;
  4802. if (IsQueueAutoPopup() && (FPendingQueueActionItem != NULL))
  4803. {
  4804. if (TQueueItem::IsUserActionStatus(FPendingQueueActionItem->Status))
  4805. {
  4806. FPendingQueueActionItem->ProcessUserAction();
  4807. }
  4808. }
  4809. }
  4810. __finally
  4811. {
  4812. FPendingQueueActionItem = NULL;
  4813. }
  4814. }
  4815. //---------------------------------------------------------------------------
  4816. void __fastcall TCustomScpExplorerForm::ApplicationMinimize(TObject * /*Sender*/)
  4817. {
  4818. if (WinConfiguration->MinimizeToTray)
  4819. {
  4820. UpdateTrayIcon();
  4821. FTrayIcon->Visible = true;
  4822. if (Visible)
  4823. {
  4824. ShowWindow(Handle, SW_HIDE);
  4825. }
  4826. }
  4827. }
  4828. //---------------------------------------------------------------------------
  4829. void __fastcall TCustomScpExplorerForm::ApplicationRestore(TObject * /*Sender*/)
  4830. {
  4831. // WORKAROUND
  4832. // When restoring maximized window from minimization,
  4833. // rarely some controls do not align properly.
  4834. // Two instances seen (both for Commander):
  4835. // - When restoring, window is temporarily narrower (not maximized),
  4836. // causing toolbars on TopDock to wrap and dock to expand horizontally.
  4837. // Once maximized already, top dock shinks back, but the session PageControl,
  4838. // do not align up, leaving space between TopDock and PageControl.
  4839. // - Similar issue seen with LocalDirView not aligning down to status bar.
  4840. for (int Index = 0; Index < ControlCount; Index++)
  4841. {
  4842. RealignControl(Controls[Index]);
  4843. }
  4844. if (FTrayIcon->Visible)
  4845. {
  4846. FTrayIcon->Visible = false;
  4847. if (Visible)
  4848. {
  4849. ShowWindow(Handle, SW_SHOW);
  4850. }
  4851. }
  4852. if (FNeedSession && DebugAlwaysTrue(Terminal == NULL))
  4853. {
  4854. FNeedSession = false;
  4855. NonVisualDataModule->StartBusy();
  4856. try
  4857. {
  4858. NeedSession(true);
  4859. }
  4860. __finally
  4861. {
  4862. NonVisualDataModule->EndBusy();
  4863. }
  4864. }
  4865. }
  4866. //---------------------------------------------------------------------------
  4867. void __fastcall TCustomScpExplorerForm::UpdateTrayIcon()
  4868. {
  4869. FTrayIcon->Hint = GetMainForm()->Caption;
  4870. }
  4871. //---------------------------------------------------------------------------
  4872. void __fastcall TCustomScpExplorerForm::ApplicationTitleChanged()
  4873. {
  4874. UpdateTrayIcon();
  4875. }
  4876. //---------------------------------------------------------------------------
  4877. void __fastcall TCustomScpExplorerForm::RestoreApp()
  4878. {
  4879. // workaround
  4880. // TApplication.WndProc sets TApplication.FAppIconic to false,
  4881. // on WM_ACTIVATEAPP, what renders TApplication.Restore no-op function.
  4882. // But WM_ACTIVATEAPP message can be received even when
  4883. // the main window is minimized to the tray and internal editor window is focused
  4884. // (after another application was previously active)
  4885. if (::IsIconic(Handle))
  4886. {
  4887. if (!IsAppIconic())
  4888. {
  4889. SetAppIconic(true);
  4890. }
  4891. }
  4892. ::ApplicationRestore();
  4893. Application->BringToFront();
  4894. }
  4895. //---------------------------------------------------------------------------
  4896. void __fastcall TCustomScpExplorerForm::TrayIconClick(TObject * /*Sender*/)
  4897. {
  4898. RestoreApp();
  4899. }
  4900. //---------------------------------------------------------------------------
  4901. void __fastcall TCustomScpExplorerForm::NewSession(const UnicodeString & SessionUrl)
  4902. {
  4903. if (OpenInNewWindow())
  4904. {
  4905. ExecuteNewInstance(SessionUrl);
  4906. }
  4907. else
  4908. {
  4909. TTerminalManager::Instance()->NewSession(SessionUrl);
  4910. }
  4911. }
  4912. //---------------------------------------------------------------------------
  4913. void TCustomScpExplorerForm::NewTab(TOperationSide, bool)
  4914. {
  4915. NewSession();
  4916. }
  4917. //---------------------------------------------------------------------------
  4918. UnicodeString __fastcall TCustomScpExplorerForm::SaveHiddenDuplicateSession(TSessionData * SessionData)
  4919. {
  4920. UnicodeString SessionName = StoredSessions->HiddenPrefix + ManagedSession->SessionData->SessionName;
  4921. StoredSessions->NewSession(SessionName, SessionData);
  4922. // modified only, explicit
  4923. StoredSessions->Save(false, true);
  4924. // encode session name because of slashes in hierarchical sessions
  4925. return EncodeUrlString(SessionName);
  4926. }
  4927. //---------------------------------------------------------------------------
  4928. UnicodeString __fastcall TCustomScpExplorerForm::CreateHiddenDuplicateSession()
  4929. {
  4930. // current working directories become defaults here, what is not right
  4931. std::unique_ptr<TSessionData> SessionData(CloneCurrentSessionData());
  4932. return SaveHiddenDuplicateSession(SessionData.get());
  4933. }
  4934. //---------------------------------------------------------------------------
  4935. void __fastcall TCustomScpExplorerForm::DuplicateTab()
  4936. {
  4937. if (OpenInNewWindow())
  4938. {
  4939. ExecuteNewInstance(CreateHiddenDuplicateSession());
  4940. }
  4941. else
  4942. {
  4943. // current working directories become defaults here, what is not right
  4944. std::unique_ptr<TSessionData> SessionData(CloneCurrentSessionData());
  4945. TTerminalManager * Manager = TTerminalManager::Instance();
  4946. TManagedTerminal * ASession = Manager->NewManagedTerminal(SessionData.get());
  4947. DebugAssert(ASession->LocalBrowser == ManagedSession->LocalBrowser);
  4948. // We definitelly want these
  4949. ASession->Disconnected = ManagedSession->Disconnected;
  4950. // Not sure about these two
  4951. ASession->Permanent = ManagedSession->Permanent;
  4952. ASession->DisconnectedTemporarily = ManagedSession->DisconnectedTemporarily;
  4953. Manager->ActiveSession = ASession;
  4954. }
  4955. }
  4956. //---------------------------------------------------------------------------
  4957. void __fastcall TCustomScpExplorerForm::RenameTab()
  4958. {
  4959. UnicodeString Name = ManagedSession->SessionData->SessionName;
  4960. if (InputDialog(LoadStr(RENAME_TAB_TITLE), LoadStr(RENAME_TAB_PROMPT), Name, HELP_TAB_RENAME) &&
  4961. // When submitting the default name, do not fix it
  4962. (Name != ManagedSession->SessionData->SessionName))
  4963. {
  4964. // Checks for a slash only, so it's not that big deal that we do the check after submitting the dialog only.
  4965. TSessionData::ValidateName(Name);
  4966. // This is inconsistent with how color (for example) is handled.
  4967. ManagedSession->SessionData->Name = Name;
  4968. UpdateControls();
  4969. // Add/Remove distinguishing paths from sessions of the same name.
  4970. SessionListChanged();
  4971. }
  4972. }
  4973. //---------------------------------------------------------------------------
  4974. bool __fastcall TCustomScpExplorerForm::CanCloseQueue(TTerminalQueue * Queue)
  4975. {
  4976. DebugAssert(Queue != NULL);
  4977. bool Result = Queue->IsEmpty;
  4978. if (!Result)
  4979. {
  4980. SetFocus();
  4981. Result = (MessageDialog(LoadStr(PENDING_QUEUE_ITEMS2), qtWarning, qaOK | qaCancel, HELP_NONE) == qaOK);
  4982. }
  4983. return Result;
  4984. }
  4985. //---------------------------------------------------------------------------
  4986. bool __fastcall TCustomScpExplorerForm::CanCloseQueue()
  4987. {
  4988. return CanCloseQueue(FQueue);
  4989. }
  4990. //---------------------------------------------------------------------------
  4991. void __fastcall TCustomScpExplorerForm::CloseTab()
  4992. {
  4993. if (CanCloseQueue())
  4994. {
  4995. TTerminalManager::Instance()->FreeActiveTerminal();
  4996. }
  4997. }
  4998. //---------------------------------------------------------------------------
  4999. void __fastcall TCustomScpExplorerForm::DisconnectSession()
  5000. {
  5001. if (CanCloseQueue())
  5002. {
  5003. TTerminalManager::Instance()->DisconnectActiveTerminal();
  5004. }
  5005. }
  5006. //---------------------------------------------------------------------------
  5007. void __fastcall TCustomScpExplorerForm::TerminalDisconnected()
  5008. {
  5009. DetachTerminal(Terminal);
  5010. RemoteDirView->Terminal = Terminal;
  5011. UpdateRemotePathComboBox(false);
  5012. InitStatusBar();
  5013. UpdateControls();
  5014. }
  5015. //---------------------------------------------------------------------------
  5016. void __fastcall TCustomScpExplorerForm::TerminalConnecting()
  5017. {
  5018. InitStatusBar();
  5019. }
  5020. //---------------------------------------------------------------------------
  5021. void __fastcall TCustomScpExplorerForm::ReconnectSession()
  5022. {
  5023. if (DebugAlwaysTrue(!Terminal->Active))
  5024. {
  5025. TTerminalManager::Instance()->ReconnectActiveTerminal();
  5026. }
  5027. }
  5028. //---------------------------------------------------------------------------
  5029. void __fastcall TCustomScpExplorerForm::OpenStoredSession(TSessionData * Data)
  5030. {
  5031. if (OpenInNewWindow())
  5032. {
  5033. // encode session name because of slashes in hierarchical sessions
  5034. ExecuteNewInstance(EncodeUrlString(Data->Name));
  5035. }
  5036. else
  5037. {
  5038. TTerminalManager * Manager = TTerminalManager::Instance();
  5039. TManagedTerminal * Terminal = Manager->NewManagedTerminal(Data);
  5040. Manager->ActiveSession = Terminal;
  5041. }
  5042. }
  5043. //---------------------------------------------------------------------------
  5044. void TCustomScpExplorerForm::DoOpenFolderOrWorkspace(
  5045. const UnicodeString & Name, bool ConnectFirstTerminal, bool CheckMaxSessions)
  5046. {
  5047. TTerminalManager * Manager = TTerminalManager::Instance();
  5048. std::unique_ptr<TObjectList> DataList(new TObjectList());
  5049. // This asks for a master password. Maybe we should postpone asking until a connection is actually opened.
  5050. StoredSessions->GetFolderOrWorkspace(Name, DataList.get());
  5051. if (!CheckMaxSessions || (DataList->Count <= WinConfiguration->MaxSessions))
  5052. {
  5053. TManagedTerminal * FirstSession = Manager->NewSessions(DataList.get());
  5054. // FirstSession can be null, if none of the workspace sites exist anymore
  5055. // or if all workspace sessions are not suported by the interface
  5056. // (when trying to open all local-local workspace in "explorer" interface)
  5057. if (FirstSession != NULL)
  5058. {
  5059. if (!ConnectFirstTerminal)
  5060. {
  5061. FirstSession->Disconnected = true;
  5062. FirstSession->DisconnectedTemporarily = true;
  5063. }
  5064. Manager->ActiveSession = FirstSession;
  5065. }
  5066. }
  5067. }
  5068. //---------------------------------------------------------------------------
  5069. void __fastcall TCustomScpExplorerForm::OpenFolderOrWorkspace(const UnicodeString & Name)
  5070. {
  5071. if (OpenInNewWindow())
  5072. {
  5073. ExecuteNewInstance(Name);
  5074. }
  5075. else
  5076. {
  5077. DoOpenFolderOrWorkspace(Name, true, false);
  5078. }
  5079. }
  5080. //---------------------------------------------------------------------------
  5081. void __fastcall TCustomScpExplorerForm::FormCloseQuery(TObject * /*Sender*/,
  5082. bool &CanClose)
  5083. {
  5084. // See the comment in CloseApp()
  5085. if (NonVisualDataModule->Busy)
  5086. {
  5087. CanClose = false;
  5088. }
  5089. else
  5090. {
  5091. TTerminalManager * Manager = TTerminalManager::Instance();
  5092. int ActiveSessions = 0;
  5093. for (int Index = 0; Index < Manager->Count; Index++)
  5094. {
  5095. if (Manager->Sessions[Index]->Active)
  5096. {
  5097. ActiveSessions++;
  5098. }
  5099. }
  5100. // Confirm, when there's at least one active remote session or
  5101. // when there are multiple (inactive or local) sessions and auto workspace saving is not enabled.
  5102. if (((ActiveSessions > 0) || ((Manager->Count > 1) && !WinConfiguration->AutoSaveWorkspace)) &&
  5103. WinConfiguration->ConfirmClosingSession)
  5104. {
  5105. unsigned int Result;
  5106. TMessageParams Params(mpNeverAskAgainCheck);
  5107. UnicodeString Message;
  5108. int Answers = qaOK | qaCancel;
  5109. // The current session is the only active session
  5110. if ((ActiveSessions == 1) && (Terminal != NULL) && Terminal->Active)
  5111. {
  5112. if (!WinConfiguration->AutoSaveWorkspace)
  5113. {
  5114. Message = LoadStr(CLOSE_SESSION_WORKSPACE);
  5115. }
  5116. else
  5117. {
  5118. Message = LoadStr(CLOSE_SESSION);
  5119. }
  5120. Message = FORMAT(Message, (Terminal->SessionData->SessionName));
  5121. }
  5122. // Multiple active sessions or one active session, but it's not the current one
  5123. else if (ActiveSessions > 0)
  5124. {
  5125. if (!WinConfiguration->AutoSaveWorkspace)
  5126. {
  5127. Message = LoadStr(CLOSE_SESSIONS_WORKSPACE3);
  5128. }
  5129. else
  5130. {
  5131. Message = LoadStr(CLOSE_SESSIONS);
  5132. }
  5133. }
  5134. // Multiple tabs, but all are inactive or local, and saving of worspace is not enabled
  5135. else
  5136. {
  5137. DebugAlwaysTrue(Manager->Count > 1);
  5138. DebugAlwaysTrue(!WinConfiguration->AutoSaveWorkspace);
  5139. Message = LoadStr(CLOSE_WORKSPACE);
  5140. }
  5141. UnicodeString Note;
  5142. if (WinConfiguration->AutoSaveWorkspace)
  5143. {
  5144. Note = FMTLOAD(AUTO_WORKSPACE, (WorkspaceName()));
  5145. }
  5146. else
  5147. {
  5148. Note = LoadStr(AUTO_WORKSPACE_ENABLE);
  5149. Answers = qaYes | qaNo | qaCancel;
  5150. }
  5151. Message = FORMAT("%s\n\n%s", (MainInstructions(Message), Note));
  5152. SetFocus();
  5153. Result = MessageDialog(Message, qtConfirmation,
  5154. Answers, HELP_CLOSE_SESSION_WORKSPACE, &Params);
  5155. if (Result == qaNeverAskAgain)
  5156. {
  5157. WinConfiguration->ConfirmClosingSession = false;
  5158. }
  5159. if (Result == qaNo)
  5160. {
  5161. CanClose = SaveWorkspace(true);
  5162. // note that the workspace will be saved redundatly again from FormClose
  5163. }
  5164. else
  5165. {
  5166. CanClose =
  5167. (Result == qaOK) ||
  5168. (Result == qaYes) || // CLOSE_SESSIONS_WORKSPACE variant
  5169. (Result == qaNeverAskAgain);
  5170. }
  5171. }
  5172. if (CanClose && HasActiveTerminal())
  5173. {
  5174. CanClose = CanCloseQueue();
  5175. }
  5176. }
  5177. if (CanClose)
  5178. {
  5179. CanClose =
  5180. FEditorManager->CloseInternalEditors(CloseInternalEditor) &&
  5181. FEditorManager->CloseExternalFilesWithoutProcess();
  5182. if (CanClose)
  5183. {
  5184. while (CanClose && (FLocalEditors->Count > 0))
  5185. {
  5186. int PrevCount = FLocalEditors->Count;
  5187. static_cast<TForm *>(FLocalEditors->Items[0])->Close();
  5188. CanClose = (FLocalEditors->Count < PrevCount);
  5189. }
  5190. if (CanClose)
  5191. {
  5192. CanClose = FEditorManager->Empty(true);
  5193. if (!CanClose)
  5194. {
  5195. SetFocus();
  5196. CanClose =
  5197. (MessageDialog(
  5198. LoadStr(PENDING_EDITORS), qtWarning, qaIgnore | qaCancel, HELP_NONE) == qaIgnore);
  5199. }
  5200. }
  5201. }
  5202. }
  5203. }
  5204. //---------------------------------------------------------------------------
  5205. void __fastcall TCustomScpExplorerForm::CloseInternalEditor(TObject * Sender)
  5206. {
  5207. TForm * Form = dynamic_cast<TForm *>(Sender);
  5208. DebugAssert(Form != NULL);
  5209. Form->Close();
  5210. }
  5211. //---------------------------------------------------------------------------
  5212. void __fastcall TCustomScpExplorerForm::ForceCloseInternalEditor(TObject * Sender)
  5213. {
  5214. TForm * Form = dynamic_cast<TForm *>(Sender);
  5215. delete Form;
  5216. }
  5217. //---------------------------------------------------------------------------
  5218. void __fastcall TCustomScpExplorerForm::ForceCloseLocalEditors()
  5219. {
  5220. while (FLocalEditors->Count > 0)
  5221. {
  5222. delete static_cast<TForm *>(FLocalEditors->Items[0]);
  5223. }
  5224. }
  5225. //---------------------------------------------------------------------------
  5226. void __fastcall TCustomScpExplorerForm::RemoteDirViewDisplayProperties(
  5227. TObject *Sender)
  5228. {
  5229. TStrings *FileList = ((TUnixDirView*)Sender)->CreateFileList(True, False, NULL);
  5230. try
  5231. {
  5232. SetProperties(osRemote, FileList);
  5233. }
  5234. __finally
  5235. {
  5236. delete FileList;
  5237. }
  5238. }
  5239. //---------------------------------------------------------------------------
  5240. void __fastcall TCustomScpExplorerForm::ComponentShowing(Byte Component, bool value)
  5241. {
  5242. if (value)
  5243. {
  5244. if (Component == fcCustomCommandsBand)
  5245. {
  5246. UpdateCustomCommandsToolbar();
  5247. }
  5248. if (Component == fcQueueFileList)
  5249. {
  5250. DebugAssert(!QueueFileListSplitter->Visible);
  5251. AdjustQueueLayout();
  5252. UpdateQueueFileList();
  5253. }
  5254. }
  5255. }
  5256. //---------------------------------------------------------------------------
  5257. void __fastcall TCustomScpExplorerForm::AdjustQueueLayout()
  5258. {
  5259. int MinHeightLimit =
  5260. GetStaticQueuePanelComponentsHeight() +
  5261. QueueFileListSplitter->Height +
  5262. QueueFileList->Height +
  5263. GetMinQueueViewHeight();
  5264. if (QueuePanel->ClientHeight < MinHeightLimit)
  5265. {
  5266. QueuePanel->ClientHeight = MinHeightLimit;
  5267. }
  5268. }
  5269. //---------------------------------------------------------------------------
  5270. void __fastcall TCustomScpExplorerForm::SetComponentVisible(Byte Component, Boolean value)
  5271. {
  5272. TControl * Control = GetComponent(Component);
  5273. DebugAssert(Control);
  5274. bool Changed = (Control->Visible != value);
  5275. if (Changed)
  5276. {
  5277. ComponentShowing(Component, value);
  5278. TWinControl * WinControl = dynamic_cast<TWinControl*>(Control);
  5279. bool WasFocused = (WinControl != NULL) && (ActiveControl != NULL) &&
  5280. ((ActiveControl == WinControl) || (ActiveControl->Parent == WinControl));
  5281. if (value)
  5282. {
  5283. int RemainingHeight = Control->Parent->ClientHeight;
  5284. int RemainingWidth = Control->Parent->ClientWidth;
  5285. for (int i = 0; i < Control->Parent->ControlCount; i++)
  5286. {
  5287. TControl * ChildControl = Control->Parent->Controls[i];
  5288. if (ChildControl->Visible)
  5289. {
  5290. switch (ChildControl->Align)
  5291. {
  5292. case alTop:
  5293. case alBottom:
  5294. RemainingHeight -= ChildControl->Height;
  5295. break;
  5296. case alLeft:
  5297. case alRight:
  5298. RemainingWidth -= ChildControl->Width;
  5299. break;
  5300. }
  5301. }
  5302. }
  5303. int Reserve = ScaleByTextHeight(this, 32);
  5304. // queue in explorer, trees in commander
  5305. if (Control->Height > RemainingHeight - Reserve)
  5306. {
  5307. Control->Height = RemainingHeight / 2;
  5308. }
  5309. if (Control->Width > RemainingWidth - Reserve)
  5310. {
  5311. Control->Width = RemainingWidth / 2;
  5312. }
  5313. }
  5314. Control->Visible = value;
  5315. if (WasFocused && Visible)
  5316. {
  5317. DirView(osCurrent)->SetFocus();
  5318. }
  5319. FixControlsPlacement();
  5320. }
  5321. }
  5322. //---------------------------------------------------------------------------
  5323. bool __fastcall TCustomScpExplorerForm::GetComponentVisible(Byte Component)
  5324. {
  5325. TControl * Control = GetComponent(Component);
  5326. if (Control == NULL)
  5327. {
  5328. return false;
  5329. }
  5330. else
  5331. {
  5332. return Control->Visible;
  5333. }
  5334. }
  5335. //---------------------------------------------------------------------------
  5336. bool __fastcall TCustomScpExplorerForm::IsComponentPossible(Byte Component)
  5337. {
  5338. bool Result;
  5339. if ((Component == fcExplorerUpdatesBand) || (Component == fcCommanderUpdatesBand))
  5340. {
  5341. Result = !IsUWP();
  5342. }
  5343. else
  5344. {
  5345. Result = true;
  5346. }
  5347. return Result;
  5348. }
  5349. //---------------------------------------------------------------------------
  5350. void __fastcall TCustomScpExplorerForm::MakeFocusedItemVisible(TCustomDirView * DirView)
  5351. {
  5352. if (DirView->ItemFocused != NULL)
  5353. {
  5354. DirView->ItemFocused->MakeVisible(false);
  5355. }
  5356. }
  5357. //---------------------------------------------------------------------------
  5358. void __fastcall TCustomScpExplorerForm::FixControlsPlacement()
  5359. {
  5360. MakeFocusedItemVisible(RemoteDirView);
  5361. QueueSplitter->Visible = QueuePanel->Visible;
  5362. QueueFileListSplitter->Visible = QueueFileList->Visible;
  5363. RemotePanelSplitter->Visible = RemoteDrivePanel->Visible;
  5364. TControl * TopControlsOrder[] =
  5365. { TopDock, MessageDock, SessionsPageControl };
  5366. SetVerticalControlsOrder(TopControlsOrder, LENOF(TopControlsOrder));
  5367. TControl * QueueControlsOrder[] =
  5368. { QueueDock, QueueView3, QueueFileListSplitter, QueueFileList };
  5369. SetVerticalControlsOrder(QueueControlsOrder, LENOF(QueueControlsOrder));
  5370. }
  5371. //---------------------------------------------------------------------------
  5372. TControl * __fastcall TCustomScpExplorerForm::GetComponent(Byte Component)
  5373. {
  5374. switch (Component) {
  5375. case fcStatusBar: return RemoteStatusBar;
  5376. case fcRemotePopup: return reinterpret_cast<TControl *>(NonVisualDataModule->RemoteFilePopup);
  5377. case fcQueueView: return QueuePanel;
  5378. case fcQueueToolbar: return QueueDock;
  5379. case fcRemoteTree: return RemoteDrivePanel;
  5380. case fcSessionsTabs: return SessionsPageControl;
  5381. case fcQueueFileList: return QueueFileList;
  5382. default: return NULL;
  5383. }
  5384. }
  5385. //---------------------------------------------------------------------------
  5386. void __fastcall TCustomScpExplorerForm::DirViewColumnRightClick(
  5387. TObject *Sender, TListColumn *Column, TPoint &Point)
  5388. {
  5389. DebugAssert(NonVisualDataModule && Column && Sender);
  5390. NonVisualDataModule->ListColumn = Column;
  5391. TPopupMenu * DirViewColumnMenu;
  5392. if (Sender == DirView(osOther))
  5393. {
  5394. DirViewColumnMenu = NonVisualDataModule->RemoteDirViewColumnPopup;
  5395. NonVisualDataModule->RemoteSortByExtColumnPopupItem->Visible = (Column->Index == uvName);
  5396. bool SizeColumn = (Column->Index == uvSize);
  5397. NonVisualDataModule->RemoteFormatSizeBytesPopupItem->Visible = SizeColumn;
  5398. NonVisualDataModule->RemoteCalculateDirectorySizesPopupItem->Visible = SizeColumn;
  5399. }
  5400. else
  5401. {
  5402. DirViewColumnMenu = NonVisualDataModule->LocalDirViewColumnPopup;
  5403. NonVisualDataModule->LocalSortByExtColumnPopupItem->Visible = (Column->Index == dvName);
  5404. bool SizeColumn = (Column->Index == dvSize);
  5405. NonVisualDataModule->LocalFormatSizeBytesPopupItem->Visible = SizeColumn;
  5406. NonVisualDataModule->LocalCalculateDirectorySizesPopupItem->Visible = SizeColumn;
  5407. }
  5408. TCustomListView * ListView = DebugNotNull(dynamic_cast<TCustomListView *>(Sender));
  5409. TPoint ScreenPoint = ListView->ClientToScreen(Point);
  5410. ScreenPoint.x -= GetScrollPos(ListView->Handle, SB_HORZ);
  5411. DirViewColumnMenu->Popup(ScreenPoint.x, ScreenPoint.y);
  5412. }
  5413. //---------------------------------------------------------------------------
  5414. void __fastcall TCustomScpExplorerForm::DirViewExecFile(
  5415. TObject *Sender, TListItem *Item, bool &AllowExec)
  5416. {
  5417. DoDirViewExecFile(Sender, Item, AllowExec);
  5418. }
  5419. //---------------------------------------------------------------------------
  5420. void __fastcall TCustomScpExplorerForm::DoDirViewExecFile(TObject * Sender,
  5421. TListItem * Item, bool & AllowExec)
  5422. {
  5423. DebugAssert(Sender && Item && Configuration);
  5424. DebugAssert(AllowExec);
  5425. TCustomDirView * ADirView = (TCustomDirView *)Sender;
  5426. TOperationSide Side = ((ADirView == DirView(osRemote)) ? osRemote : osLocal);
  5427. bool Remote = !IsSideLocalBrowser(Side);
  5428. DebugAssert(Item == ADirView->ItemFocused);
  5429. bool IsDirectory = ADirView->ItemIsDirectory(Item);
  5430. TResolvedDoubleClickAction Action;
  5431. if (FForceExecution)
  5432. {
  5433. Action = IsDirectory ? rdcaChangeDir : rdcaOpen;
  5434. }
  5435. else
  5436. {
  5437. TTerminal * ATerminal = !IsSideLocalBrowser(Side) ? Terminal : NULL;
  5438. Action = WinConfiguration->ResolveDoubleClickAction(IsDirectory, ATerminal);
  5439. }
  5440. if (Action == rdcaCopy)
  5441. {
  5442. // To counter the lock in DirViewBusy (called from TCustomDirView.DoExecute)
  5443. UnlockWindow();
  5444. try
  5445. {
  5446. if (IsLocalBrowserMode())
  5447. {
  5448. LocalLocalCopy(foCopy, Side, true, !WinConfiguration->CopyOnDoubleClickConfirmation, false, 0);
  5449. }
  5450. else
  5451. {
  5452. ExecuteFileOperation(foCopy, Side, true, !WinConfiguration->CopyOnDoubleClickConfirmation);
  5453. }
  5454. }
  5455. __finally
  5456. {
  5457. LockWindow();
  5458. }
  5459. AllowExec = false;
  5460. }
  5461. else if (Action == rdcaEdit)
  5462. {
  5463. if (!Remote || !WinConfiguration->DisableOpenEdit)
  5464. {
  5465. ExecuteFile(Side, efDefaultEditor);
  5466. AllowExec = false;
  5467. }
  5468. }
  5469. else if (Action == rdcaOpen)
  5470. {
  5471. if (DebugAlwaysTrue(!IsDirectory) && Remote && !WinConfiguration->DisableOpenEdit)
  5472. {
  5473. ExecuteFile(osRemote, efShell);
  5474. AllowExec = false;
  5475. }
  5476. }
  5477. }
  5478. //---------------------------------------------------------------------------
  5479. bool __fastcall TCustomScpExplorerForm::GetHasDirView(TOperationSide Side)
  5480. {
  5481. return ((Side == osRemote) || (Side == osCurrent));
  5482. }
  5483. //---------------------------------------------------------------------------
  5484. void __fastcall TCustomScpExplorerForm::CompareDirectories()
  5485. {
  5486. DebugFail();
  5487. }
  5488. //---------------------------------------------------------------------------
  5489. void __fastcall TCustomScpExplorerForm::SynchronizeDirectories()
  5490. {
  5491. DebugFail();
  5492. }
  5493. //---------------------------------------------------------------------------
  5494. bool __fastcall TCustomScpExplorerForm::DoSynchronizeDirectories(
  5495. UnicodeString & LocalDirectory, UnicodeString & RemoteDirectory, int UseDefaults)
  5496. {
  5497. TSynchronizeParamType Params;
  5498. Params.LocalDirectory = LocalDirectory;
  5499. Params.RemoteDirectory = RemoteDirectory;
  5500. int UnusedParams =
  5501. (GUIConfiguration->SynchronizeParams &
  5502. (TTerminal::spPreviewChanges | TTerminal::spTimestamp | TTerminal::spNotByTime | TTerminal::spBySize |
  5503. TTerminal::spCaseSensitive));
  5504. Params.Params = GUIConfiguration->SynchronizeParams & ~UnusedParams;
  5505. Params.Options = GUIConfiguration->SynchronizeOptions;
  5506. bool SaveSettings = false;
  5507. TSynchronizeController Controller(&DoSynchronize, &DoSynchronizeInvalid,
  5508. &DoSynchronizeTooManyDirectories);
  5509. DebugAssert(FSynchronizeController == NULL);
  5510. FSynchronizeController = &Controller;
  5511. bool Result;
  5512. try
  5513. {
  5514. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  5515. int CopyParamAttrs = Terminal->UsableCopyParamAttrs(0).Upload;
  5516. int Options =
  5517. FLAGMASK(SynchronizeAllowSelectedOnly(), soAllowSelectedOnly);
  5518. DebugAssert(FOnFeedSynchronizeError == NULL);
  5519. DebugAssert(FOnSynchronizeAbort == NULL);
  5520. DebugAssert(FSynchronizeTerminal == NULL);
  5521. FSynchronizeTerminal = Terminal;
  5522. Result = DoSynchronizeDialog(Params, &CopyParam, Controller.StartStop,
  5523. SaveSettings, Options, CopyParamAttrs, GetSynchronizeOptions, SynchronizeSessionLog,
  5524. FOnFeedSynchronizeError, FOnSynchronizeAbort, SynchronizeInNewWindow, UseDefaults);
  5525. if (Result)
  5526. {
  5527. if (SaveSettings)
  5528. {
  5529. GUIConfiguration->SynchronizeParams = Params.Params | UnusedParams;
  5530. GUIConfiguration->SynchronizeOptions = Params.Options;
  5531. }
  5532. else
  5533. {
  5534. if (FLAGSET(GUIConfiguration->SynchronizeOptions, soSynchronizeAsk) &&
  5535. FLAGCLEAR(Params.Options, soSynchronizeAsk) &&
  5536. FLAGSET(Params.Options, soSynchronize))
  5537. {
  5538. GUIConfiguration->SynchronizeOptions =
  5539. (GUIConfiguration->SynchronizeOptions & ~soSynchronizeAsk) |
  5540. soSynchronize;
  5541. }
  5542. }
  5543. LocalDirectory = Params.LocalDirectory;
  5544. RemoteDirectory = Params.RemoteDirectory;
  5545. }
  5546. }
  5547. __finally
  5548. {
  5549. FSynchronizeController = NULL;
  5550. DebugAssert(FOnFeedSynchronizeError == NULL);
  5551. FOnFeedSynchronizeError = NULL;
  5552. DebugAssert(FOnSynchronizeAbort == NULL);
  5553. FOnSynchronizeAbort = NULL;
  5554. FSynchronizeTerminal = NULL;
  5555. }
  5556. return Result;
  5557. }
  5558. //---------------------------------------------------------------------------
  5559. void __fastcall TCustomScpExplorerForm::DoSynchronize(
  5560. TSynchronizeController * /*Sender*/, const UnicodeString LocalDirectory,
  5561. const UnicodeString RemoteDirectory, const TCopyParamType & CopyParam,
  5562. const TSynchronizeParamType & Params, TSynchronizeChecklist ** Checklist,
  5563. TSynchronizeOptions * Options, bool Full)
  5564. {
  5565. // Keep-up-to-date mode
  5566. if (Terminal->Status == ssOpened)
  5567. {
  5568. try
  5569. {
  5570. int PParams = Params.Params;
  5571. if (!Full)
  5572. {
  5573. PParams |= TTerminal::spNoRecurse | TTerminal::spUseCache | TTerminal::spDelayProgress;
  5574. if (FLAGSET(Params.Options, soRecurse))
  5575. {
  5576. PParams |= TTerminal::spSubDirs;
  5577. }
  5578. }
  5579. else
  5580. {
  5581. // if keepuptodate is non-recursive,
  5582. // full sync before has to be non-recursive as well
  5583. if (FLAGCLEAR(Params.Options, soRecurse))
  5584. {
  5585. PParams |= TTerminal::spNoRecurse;
  5586. }
  5587. }
  5588. Synchronize(LocalDirectory, RemoteDirectory, smRemote, CopyParam,
  5589. PParams, Checklist, Options);
  5590. }
  5591. catch (Exception & E)
  5592. {
  5593. if (!Terminal->Active)
  5594. {
  5595. ShowExtendedExceptionEx(Terminal, &E);
  5596. // Do not abort the "keep up to date" when the session was succesfully reconnected.
  5597. if (!Terminal->Active)
  5598. {
  5599. throw;
  5600. }
  5601. }
  5602. else
  5603. {
  5604. if (FLAGSET(Params.Options, soContinueOnError) &&
  5605. DebugAlwaysTrue(FOnFeedSynchronizeError != NULL))
  5606. {
  5607. // noop, already logged in MoreMessageDialog
  5608. }
  5609. else
  5610. {
  5611. // We get mostly EAbort here, so this is noop.
  5612. // Exception is an error when listing directories while looking for differences.
  5613. ShowExtendedExceptionEx(Terminal, &E);
  5614. throw;
  5615. }
  5616. }
  5617. }
  5618. }
  5619. }
  5620. //---------------------------------------------------------------------------
  5621. void __fastcall TCustomScpExplorerForm::DoSynchronizeInvalid(
  5622. TSynchronizeController * /*Sender*/, const UnicodeString Directory,
  5623. const UnicodeString ErrorStr)
  5624. {
  5625. if (!Directory.IsEmpty())
  5626. {
  5627. SimpleErrorDialog(FMTLOAD(WATCH_ERROR_DIRECTORY, (Directory)), ErrorStr);
  5628. }
  5629. else
  5630. {
  5631. SimpleErrorDialog(LoadStr(WATCH_ERROR_GENERAL), ErrorStr);
  5632. }
  5633. }
  5634. //---------------------------------------------------------------------------
  5635. void __fastcall TCustomScpExplorerForm::DoSynchronizeTooManyDirectories(
  5636. TSynchronizeController * /*Sender*/, int & MaxDirectories)
  5637. {
  5638. if (MaxDirectories < GUIConfiguration->MaxWatchDirectories)
  5639. {
  5640. MaxDirectories = GUIConfiguration->MaxWatchDirectories;
  5641. }
  5642. else
  5643. {
  5644. TMessageParams Params(mpNeverAskAgainCheck);
  5645. unsigned int Result = MessageDialog(
  5646. FMTLOAD(TOO_MANY_WATCH_DIRECTORIES, (MaxDirectories, MaxDirectories)),
  5647. qtConfirmation, qaYes | qaNo, HELP_TOO_MANY_WATCH_DIRECTORIES, &Params);
  5648. if ((Result == qaYes) || (Result == qaNeverAskAgain))
  5649. {
  5650. MaxDirectories *= 2;
  5651. if (Result == qaNeverAskAgain)
  5652. {
  5653. GUIConfiguration->MaxWatchDirectories = MaxDirectories;
  5654. }
  5655. }
  5656. else
  5657. {
  5658. Abort();
  5659. }
  5660. }
  5661. }
  5662. //---------------------------------------------------------------------------
  5663. void __fastcall TCustomScpExplorerForm::Synchronize(const UnicodeString LocalDirectory,
  5664. const UnicodeString RemoteDirectory, TSynchronizeMode Mode,
  5665. const TCopyParamType & CopyParam, int Params, TSynchronizeChecklist ** Checklist,
  5666. TSynchronizeOptions * Options)
  5667. {
  5668. // Keep-up-to-date mode
  5669. DebugAssert(!FAutoOperation);
  5670. void * BatchStorage;
  5671. BatchStart(BatchStorage);
  5672. FAutoOperation = true;
  5673. bool AnyOperation = false;
  5674. TDateTime StartTime = Now();
  5675. TSynchronizeChecklist * AChecklist = NULL;
  5676. try
  5677. {
  5678. FSynchronizeProgressForm = new TSynchronizeProgressForm(Application, true, -1);
  5679. if (FLAGCLEAR(Params, TTerminal::spDelayProgress))
  5680. {
  5681. FSynchronizeProgressForm->Start();
  5682. }
  5683. AChecklist = Terminal->SynchronizeCollect(LocalDirectory, RemoteDirectory,
  5684. static_cast<TTerminal::TSynchronizeMode>(Mode),
  5685. &CopyParam, Params | TTerminal::spNoConfirmation, TerminalSynchronizeDirectory,
  5686. Options);
  5687. SetTaskbarListProgressState(TBPF_NOPROGRESS);
  5688. SAFE_DESTROY(FSynchronizeProgressForm);
  5689. AnyOperation = (AChecklist->CheckedCount > 0);
  5690. if (AnyOperation)
  5691. {
  5692. TSynchronizeProgress SynchronizeProgress(AChecklist);
  5693. CreateProgressForm(&SynchronizeProgress);
  5694. try
  5695. {
  5696. Terminal->SynchronizeApply(
  5697. AChecklist, &CopyParam, Params | TTerminal::spNoConfirmation, TerminalSynchronizeDirectory,
  5698. SynchronizeProcessedItem, NULL, NULL, NULL);
  5699. }
  5700. __finally
  5701. {
  5702. DestroyProgressForm();
  5703. }
  5704. }
  5705. }
  5706. __finally
  5707. {
  5708. if (Checklist == NULL)
  5709. {
  5710. delete AChecklist;
  5711. }
  5712. else
  5713. {
  5714. *Checklist = AChecklist;
  5715. }
  5716. FAutoOperation = false;
  5717. SAFE_DESTROY(FSynchronizeProgressForm);
  5718. BatchEnd(BatchStorage);
  5719. ReloadLocalDirectory();
  5720. if (AnyOperation)
  5721. {
  5722. OperationComplete(StartTime);
  5723. }
  5724. }
  5725. }
  5726. //---------------------------------------------------------------------------
  5727. bool __fastcall TCustomScpExplorerForm::SynchronizeAllowSelectedOnly()
  5728. {
  5729. // can be called from command line
  5730. return Visible &&
  5731. ((DirView(osRemote)->SelCount > 0) ||
  5732. (HasDirView[osLocal] && (DirView(osLocal)->SelCount > 0)));
  5733. }
  5734. //---------------------------------------------------------------------------
  5735. void __fastcall TCustomScpExplorerForm::SynchronizeSessionLog(const UnicodeString & Message)
  5736. {
  5737. LogSynchronizeEvent(Terminal, Message);
  5738. }
  5739. //---------------------------------------------------------------------------
  5740. void __fastcall TCustomScpExplorerForm::GetSynchronizeOptions(
  5741. int Params, TSynchronizeOptions & Options)
  5742. {
  5743. DebugAssert(!IsLocalBrowserMode());
  5744. if (FLAGSET(Params, TTerminal::spSelectedOnly) && SynchronizeAllowSelectedOnly())
  5745. {
  5746. Options.Filter = new TStringList();
  5747. Options.Filter->CaseSensitive = false;
  5748. Options.Filter->Duplicates = Types::dupAccept;
  5749. if (DirView(osRemote)->SelCount > 0)
  5750. {
  5751. DirView(osRemote)->CreateFileList(false, false, Options.Filter);
  5752. }
  5753. if (HasDirView[osLocal] && (DirView(osLocal)->SelCount > 0))
  5754. {
  5755. DirView(osLocal)->CreateFileList(false, false, Options.Filter);
  5756. }
  5757. Options.Filter->Sort();
  5758. }
  5759. }
  5760. //---------------------------------------------------------------------------
  5761. UnicodeString __fastcall TCustomScpExplorerForm::SerializeCopyParamForCommandLine(const TCopyParamType * CopyParams)
  5762. {
  5763. TCopyParamType Defaults;
  5764. std::unique_ptr<THierarchicalStorage> ConfigStorage(Configuration->CreateConfigStorage());
  5765. ConfigStorage->AccessMode = smRead;
  5766. if (ConfigStorage->OpenSubKey(Configuration->ConfigurationSubKey, false))
  5767. {
  5768. GUIConfiguration->LoadCopyParam(ConfigStorage.get(), &Defaults);
  5769. }
  5770. ConfigStorage.reset(NULL);
  5771. std::unique_ptr<TStringList> Options(new TStringList());
  5772. std::unique_ptr<TOptionsStorage> OptionsStorage(new TOptionsStorage(Options.get(), true));
  5773. CopyParams->Save(OptionsStorage.get(), &Defaults);
  5774. UnicodeString Result;
  5775. if (Options->Count > 0)
  5776. {
  5777. Result =
  5778. FORMAT(L" %s%s", (TProgramParams::FormatSwitch(RAWTRANSFERSETTINGS_SWITCH), StringsToParams(Options.get())));
  5779. }
  5780. return Result;
  5781. }
  5782. //---------------------------------------------------------------------------
  5783. void __fastcall TCustomScpExplorerForm::SynchronizeInNewWindow(
  5784. const TSynchronizeParamType & Params, const TCopyParamType * CopyParams)
  5785. {
  5786. UnicodeString SessionName = CreateHiddenDuplicateSession();
  5787. UnicodeString AdditionalParams =
  5788. FORMAT(L"%s%s %s // \"%s\" \"%s\" %d %d", (
  5789. TProgramParams::FormatSwitch(DEFAULTS_SWITCH),
  5790. SerializeCopyParamForCommandLine(CopyParams),
  5791. TProgramParams::FormatSwitch(KEEP_UP_TO_DATE_SWITCH),
  5792. Params.LocalDirectory, Params.RemoteDirectory, Params.Params, Params.Options));
  5793. ExecuteNewInstance(SessionName, AdditionalParams);
  5794. }
  5795. //---------------------------------------------------------------------------
  5796. struct TSynchronizeParams
  5797. {
  5798. UnicodeString LocalDirectory;
  5799. UnicodeString RemoteDirectory;
  5800. TSynchronizeMode Mode;
  5801. TSynchronizeChecklist * Checklist;
  5802. int Params;
  5803. TCopyParamType * CopyParam;
  5804. TDateTime CollectElapsed;
  5805. TDateTime * StartTime;
  5806. TProcessedSynchronizationChecklistItem OnProcessedItem;
  5807. };
  5808. //---------------------------------------------------------------------------
  5809. void __fastcall TCustomScpExplorerForm::FullSynchronize(
  5810. TSynchronizeParams & Params, TProcessedSynchronizationChecklistItem OnProcessedItem,
  5811. TUpdatedSynchronizationChecklistItems OnUpdatedSynchronizationChecklistItems)
  5812. {
  5813. DebugAssert(!FAutoOperation);
  5814. void * BatchStorage;
  5815. BatchStart(BatchStorage);
  5816. FAutoOperation = true;
  5817. TFileOperationStatistics Statistics;
  5818. TDateTime Start = Now();
  5819. try
  5820. {
  5821. Params.OnProcessedItem = OnProcessedItem;
  5822. TSynchronizeProgress SynchronizeProgress(Params.Checklist);
  5823. CreateProgressForm(&SynchronizeProgress);
  5824. Terminal->SynchronizeApply(
  5825. Params.Checklist, Params.CopyParam, Params.Params | TTerminal::spNoConfirmation,
  5826. TerminalSynchronizeDirectory, SynchronizeProcessedItem, OnUpdatedSynchronizationChecklistItems, &Params,
  5827. &Statistics);
  5828. }
  5829. __finally
  5830. {
  5831. Params.OnProcessedItem = NULL;
  5832. FAutoOperation = false;
  5833. DestroyProgressForm();
  5834. BatchEnd(BatchStorage);
  5835. ReloadLocalDirectory();
  5836. }
  5837. if (WinConfiguration->SynchronizeSummary)
  5838. {
  5839. UnicodeString Message = MainInstructions(LoadStr(SYNCHRONIZE_COMPLETE)) + L"\n";
  5840. // The statistics should be 0 anyway in this case
  5841. if (FLAGCLEAR(Params.Params, TTerminal::spTimestamp))
  5842. {
  5843. Message += L"\n";
  5844. if (Statistics.FilesUploaded > 0)
  5845. {
  5846. Message += FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 1), (FormatNumber(Statistics.FilesUploaded), FormatBytes(Statistics.TotalUploaded))) + L"\n";
  5847. }
  5848. if (Statistics.FilesDownloaded > 0)
  5849. {
  5850. Message += FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 2), (FormatNumber(Statistics.FilesDownloaded), FormatBytes(Statistics.TotalDownloaded))) + L"\n";
  5851. }
  5852. if (Statistics.FilesDeletedLocal > 0)
  5853. {
  5854. Message += FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 3), (FormatNumber(Statistics.FilesDeletedLocal))) + L"\n";
  5855. }
  5856. if (Statistics.FilesDeletedRemote > 0)
  5857. {
  5858. Message += FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 4), (FormatNumber(Statistics.FilesDeletedRemote))) + L"\n";
  5859. }
  5860. }
  5861. TDateTime Elapsed = (Now() - Start);
  5862. Message +=
  5863. L"\n" +
  5864. FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 5), (FormatDateTimeSpan(Configuration->TimeFormat, Params.CollectElapsed))) + L"\n" +
  5865. FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 6), (FormatDateTimeSpan(Configuration->TimeFormat, Elapsed)));
  5866. TMessageParams Params(mpNeverAskAgainCheck);
  5867. unsigned int Result = MessageDialog(Message, qtInformation, qaOK, HELP_NONE, &Params);
  5868. if (Result == qaNeverAskAgain)
  5869. {
  5870. WinConfiguration->SynchronizeSummary = false;
  5871. }
  5872. }
  5873. }
  5874. //---------------------------------------------------------------------------
  5875. void __fastcall TCustomScpExplorerForm::SynchronizeProcessedItem(void * Token, const TSynchronizeChecklist::TItem * ChecklistItem)
  5876. {
  5877. if (DebugAlwaysTrue(FProgressForm != NULL) && DebugAlwaysTrue(FProgressForm->SynchronizeProgress != NULL))
  5878. {
  5879. FProgressForm->SynchronizeProgress->ItemProcessed(ChecklistItem);
  5880. }
  5881. // Not set in keep-up-to-date mode - Synchronize() method
  5882. if (Token != NULL)
  5883. {
  5884. TSynchronizeParams & Params = *static_cast<TSynchronizeParams *>(Token);
  5885. if (Params.OnProcessedItem != NULL)
  5886. {
  5887. Params.OnProcessedItem(NULL, ChecklistItem);
  5888. }
  5889. }
  5890. }
  5891. //---------------------------------------------------------------------------
  5892. void __fastcall TCustomScpExplorerForm::DoFullSynchronize(
  5893. void * Token, TProcessedSynchronizationChecklistItem OnProcessedItem,
  5894. TUpdatedSynchronizationChecklistItems OnUpdatedSynchronizationChecklistItems)
  5895. {
  5896. TSynchronizeParams & Params = *static_cast<TSynchronizeParams *>(Token);
  5897. *Params.StartTime = Now();
  5898. FullSynchronize(Params, OnProcessedItem, OnUpdatedSynchronizationChecklistItems);
  5899. }
  5900. //---------------------------------------------------------------------------
  5901. void __fastcall TCustomScpExplorerForm::DoSynchronizeChecklistCalculateSize(
  5902. TSynchronizeChecklist * Checklist, const TSynchronizeChecklist::TItemList & Items, void * Token)
  5903. {
  5904. // terminal can be already closed (e.g. dropped connection)
  5905. if (Terminal != NULL)
  5906. {
  5907. TSynchronizeParams & Params = *static_cast<TSynchronizeParams *>(Token);
  5908. Terminal->SynchronizeChecklistCalculateSize(Checklist, Items, Params.CopyParam);
  5909. }
  5910. }
  5911. //---------------------------------------------------------------------------
  5912. void __fastcall TCustomScpExplorerForm::DoSynchronizeMove(
  5913. TOperationSide Side, const UnicodeString & FileName, const UnicodeString & NewFileName, TRemoteFile * RemoteFile)
  5914. {
  5915. DebugAssert(!IsLocalBrowserMode());
  5916. TAutoBatch AutoBatch(this);
  5917. TAutoFlag AutoOperationFlag(FAutoOperation);
  5918. if (Side == osRemote)
  5919. {
  5920. std::unique_ptr<TStrings> FileList(new TStringList());
  5921. FileList->AddObject(FileName, RemoteFile);
  5922. UnicodeString Target = UnixExtractFileDir(NewFileName);
  5923. UnicodeString FileMask = DelimitFileNameMask(UnixExtractFileName(NewFileName));
  5924. RemoteDirView->SaveSelection();
  5925. RemoteDirView->SaveSelectedNames();
  5926. try
  5927. {
  5928. Terminal->MoveFiles(FileList.get(), Target, FileMask, false);
  5929. }
  5930. catch(...)
  5931. {
  5932. RemoteDirView->DiscardSavedSelection();
  5933. throw;
  5934. }
  5935. RemoteDirView->RestoreSelection();
  5936. }
  5937. else if (DebugAlwaysTrue(Side == osLocal))
  5938. {
  5939. if (!MoveFile(FileName.c_str(), NewFileName.c_str()))
  5940. {
  5941. throw EOSExtException(FMTLOAD(RENAME_FILE_ERROR, (FileName, NewFileName)));
  5942. }
  5943. UnicodeString Directory = ExtractFileDir(FileName);
  5944. ReloadLocalDirectory(Directory);
  5945. UnicodeString NewDirectory = ExtractFileDir(NewFileName);
  5946. if (!SamePaths(Directory, NewDirectory))
  5947. {
  5948. ReloadLocalDirectory(NewDirectory);
  5949. }
  5950. }
  5951. }
  5952. //---------------------------------------------------------------------------
  5953. void __fastcall TCustomScpExplorerForm::DoSynchronizeBrowse(TOperationSide Side, TSynchronizeChecklist::TAction Action, const TSynchronizeChecklist::TItem * Item)
  5954. {
  5955. UnicodeString LocalPath = ExcludeTrailingBackslash(Item->Local.Directory);
  5956. if (Side == osLocal)
  5957. {
  5958. if (Action == TSynchronizeChecklist::saDownloadNew)
  5959. {
  5960. OpenFolderInExplorer(LocalPath);
  5961. }
  5962. else
  5963. {
  5964. OpenFileInExplorer(TPath::Combine(LocalPath, Item->GetFileName()));
  5965. }
  5966. }
  5967. else if (DebugAlwaysTrue(Side == osRemote))
  5968. {
  5969. // Similar to CreateHiddenDuplicateSession, except that it modifies the initial directories
  5970. std::unique_ptr<TSessionData> SessionData(CloneCurrentSessionData());
  5971. SessionData->RemoteDirectory = UnixExcludeTrailingBackslash(Item->Remote.Directory);
  5972. if (!LocalPath.IsEmpty())
  5973. {
  5974. SessionData->LocalDirectory = LocalPath;
  5975. }
  5976. UnicodeString SessionName = SaveHiddenDuplicateSession(SessionData.get());
  5977. ExecuteNewInstance(SessionName, FORMAT(L"%s=\"%s\"", (TProgramParams::FormatSwitch(BROWSE_SWITCH), Item->GetFileName())));
  5978. }
  5979. }
  5980. //---------------------------------------------------------------------------
  5981. void __fastcall TCustomScpExplorerForm::FullSynchronizeInNewWindow(
  5982. TSynchronizeMode Mode, int Params, const UnicodeString & LocalDirectory, const UnicodeString & RemoteDirectory,
  5983. const TCopyParamType * CopyParams)
  5984. {
  5985. UnicodeString SessionName = CreateHiddenDuplicateSession();
  5986. UnicodeString AdditionalParams =
  5987. FORMAT(L"%s%s %s // \"%s\" \"%s\" %d %d", (
  5988. TProgramParams::FormatSwitch(DEFAULTS_SWITCH),
  5989. SerializeCopyParamForCommandLine(CopyParams),
  5990. TProgramParams::FormatSwitch(SYNCHRONIZE_SWITCH),
  5991. LocalDirectory, RemoteDirectory, Mode, Params));
  5992. ExecuteNewInstance(SessionName, AdditionalParams);
  5993. }
  5994. //---------------------------------------------------------------------------
  5995. int __fastcall TCustomScpExplorerForm::DoFullSynchronizeDirectories(
  5996. UnicodeString & LocalDirectory, UnicodeString & RemoteDirectory,
  5997. TSynchronizeMode & Mode, int Params, bool & SaveMode, int UseDefaults)
  5998. {
  5999. int Result;
  6000. bool SaveSettings = false;
  6001. int Options =
  6002. FLAGMASK(!Terminal->IsCapable[fcTimestampChanging], fsoDisableTimestamp) |
  6003. FLAGMASK(SynchronizeAllowSelectedOnly(), fsoAllowSelectedOnly);
  6004. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  6005. TUsableCopyParamAttrs CopyParamAttrs = Terminal->UsableCopyParamAttrs(0);
  6006. bool Continue =
  6007. (UseDefaults == 0) ||
  6008. DoFullSynchronizeDialog(Mode, Params, LocalDirectory, RemoteDirectory,
  6009. &CopyParam, SaveSettings, SaveMode, Options, CopyParamAttrs, FullSynchronizeInNewWindow, UseDefaults);
  6010. if (Continue)
  6011. {
  6012. Configuration->Usage->Inc(L"Synchronizations");
  6013. CopyParam.IncludeFileMask.SetRoots(LocalDirectory, RemoteDirectory);
  6014. UpdateCopyParamCounters(CopyParam);
  6015. TSynchronizeOptions SynchronizeOptions;
  6016. GetSynchronizeOptions(Params, SynchronizeOptions);
  6017. if (SaveSettings)
  6018. {
  6019. GUIConfiguration->SynchronizeParams = Params;
  6020. }
  6021. else
  6022. {
  6023. SaveMode = false;
  6024. }
  6025. TDateTime StartTime = Now();
  6026. TSynchronizeChecklist * Checklist = NULL;
  6027. try
  6028. {
  6029. DebugAssert(!FAutoOperation);
  6030. FAutoOperation = true;
  6031. try
  6032. {
  6033. UnicodeString SessionKey = Terminal->SessionData->SessionKey;
  6034. std::unique_ptr<TStrings> DataList(Configuration->LoadDirectoryStatisticsCache(SessionKey, RemoteDirectory, CopyParam));
  6035. int Files = -1;
  6036. if (DataList->Count >= 1)
  6037. {
  6038. Files = StrToIntDef(DataList->Strings[0], Files);
  6039. }
  6040. else
  6041. {
  6042. DataList->Add(UnicodeString());
  6043. }
  6044. FSynchronizeProgressForm = new TSynchronizeProgressForm(Application, true, Files);
  6045. FSynchronizeProgressForm->Start();
  6046. Checklist = Terminal->SynchronizeCollect(LocalDirectory, RemoteDirectory,
  6047. static_cast<TTerminal::TSynchronizeMode>(Mode),
  6048. &CopyParam, Params | TTerminal::spNoConfirmation, TerminalSynchronizeDirectory,
  6049. &SynchronizeOptions);
  6050. if (Terminal->SessionData->CacheDirectories)
  6051. {
  6052. DataList->Strings[0] = IntToStr(SynchronizeOptions.Files);
  6053. Configuration->SaveDirectoryStatisticsCache(SessionKey, RemoteDirectory, CopyParam, DataList.get());
  6054. }
  6055. }
  6056. __finally
  6057. {
  6058. FAutoOperation = false;
  6059. SetTaskbarListProgressState(TBPF_NOPROGRESS);
  6060. SAFE_DESTROY(FSynchronizeProgressForm);
  6061. }
  6062. TSynchronizeParams SynchronizeParams;
  6063. SynchronizeParams.LocalDirectory = LocalDirectory;
  6064. SynchronizeParams.RemoteDirectory = RemoteDirectory;
  6065. SynchronizeParams.Mode = Mode;
  6066. SynchronizeParams.CopyParam = &CopyParam;
  6067. SynchronizeParams.Params = Params;
  6068. SynchronizeParams.Checklist = Checklist;
  6069. SynchronizeParams.CollectElapsed = (Now() - StartTime);
  6070. SynchronizeParams.StartTime = &StartTime;
  6071. SynchronizeParams.OnProcessedItem = NULL;
  6072. Result = Checklist->Count;
  6073. if (Checklist->Count > 0)
  6074. {
  6075. if (FLAGSET(Params, TTerminal::spPreviewChanges))
  6076. {
  6077. if (!DoSynchronizeChecklistDialog(
  6078. Checklist, Mode, Params, LocalDirectory, RemoteDirectory, CustomCommandMenu, DoFullSynchronize,
  6079. DoSynchronizeChecklistCalculateSize, DoSynchronizeMove, DoSynchronizeBrowse, &SynchronizeParams))
  6080. {
  6081. Result = -1;
  6082. }
  6083. }
  6084. else
  6085. {
  6086. FullSynchronize(SynchronizeParams, NULL, NULL);
  6087. }
  6088. }
  6089. else
  6090. {
  6091. UnicodeString Message = MainInstructions(LoadStr(COMPARE_NO_DIFFERENCES));
  6092. MessageDialog(Message, qtInformation, qaOK, HELP_SYNCHRONIZE_NO_DIFFERENCES);
  6093. }
  6094. }
  6095. __finally
  6096. {
  6097. delete Checklist;
  6098. }
  6099. OperationComplete(StartTime);
  6100. }
  6101. else
  6102. {
  6103. Result = -1;
  6104. }
  6105. return Result;
  6106. }
  6107. //---------------------------------------------------------------------------
  6108. void __fastcall TCustomScpExplorerForm::TerminalSynchronizeDirectory(
  6109. const UnicodeString & LocalDirectory, const UnicodeString & RemoteDirectory,
  6110. bool & Continue, bool Collect, const TSynchronizeOptions * Options)
  6111. {
  6112. if (Collect)
  6113. {
  6114. DebugAssert(FSynchronizeProgressForm != NULL);
  6115. if (!FSynchronizeProgressForm->Started)
  6116. {
  6117. FSynchronizeProgressForm->Start();
  6118. }
  6119. int CompareProgress = FSynchronizeProgressForm->SetData(LocalDirectory, RemoteDirectory, Options->Files, Continue);
  6120. SetTaskbarListProgressValue(CompareProgress);
  6121. }
  6122. else
  6123. {
  6124. DebugAssert(FSynchronizeProgressForm == NULL);
  6125. DebugAssert(FProgressForm != NULL);
  6126. }
  6127. }
  6128. //---------------------------------------------------------------------------
  6129. void __fastcall TCustomScpExplorerForm::StandaloneEdit(const UnicodeString & FileName)
  6130. {
  6131. UnicodeString FullFileName = AbsolutePath(Terminal->CurrentDirectory, FileName);
  6132. std::unique_ptr<TRemoteFile> File(Terminal->ReadFile(FullFileName));
  6133. if (File.get() != NULL)
  6134. {
  6135. ExecuteRemoteFile(FullFileName, File.get(), efInternalEditor);
  6136. Application->ShowMainForm = false;
  6137. Application->Run();
  6138. }
  6139. }
  6140. //---------------------------------------------------------------------------
  6141. void __fastcall TCustomScpExplorerForm::ExploreLocalDirectory(TOperationSide)
  6142. {
  6143. DebugFail();
  6144. }
  6145. //---------------------------------------------------------------------------
  6146. TSessionData * __fastcall TCustomScpExplorerForm::CloneCurrentSessionData()
  6147. {
  6148. std::unique_ptr<TSessionData> SessionData(new TSessionData(L""));
  6149. SessionData->Assign(ManagedSession->SessionData);
  6150. UpdateSessionData(SessionData.get());
  6151. DebugAssert(SessionData->IsLocalBrowser == IsLocalBrowserMode());
  6152. if (IsActiveTerminal(ManagedSession))
  6153. {
  6154. ManagedSession->UpdateSessionCredentials(SessionData.get());
  6155. }
  6156. if (!ManagedSession->SessionData->HasSessionName())
  6157. {
  6158. // Particularly for "Workspace/XXXX" name, we need to reset it, as it would become user-visible
  6159. // once IsWorkspace is cleared
  6160. SessionData->Name = UnicodeString();
  6161. }
  6162. // Most uses of this method ends with saving the session, and we do not want to store the IsWorkspace flag.
  6163. // Particularly, with SaveHiddenDuplicateSession, it won't work as TSessionData::ParseUrl explicitly ignored workspace sessions.
  6164. SessionData->IsWorkspace = false;
  6165. return SessionData.release();
  6166. }
  6167. //---------------------------------------------------------------------------
  6168. void __fastcall TCustomScpExplorerForm::SaveCurrentSession()
  6169. {
  6170. TSessionData * SessionData = CloneCurrentSessionData();
  6171. try
  6172. {
  6173. TSessionData * EditingSessionData = StoredSessions->FindSame(SessionData);
  6174. DoSaveSession(SessionData, EditingSessionData, true, NULL);
  6175. }
  6176. __finally
  6177. {
  6178. delete SessionData;
  6179. }
  6180. }
  6181. //---------------------------------------------------------------------------
  6182. TObjectList * __fastcall TCustomScpExplorerForm::DoCollectWorkspace()
  6183. {
  6184. TTerminalManager * Manager = TTerminalManager::Instance();
  6185. std::unique_ptr<TObjectList> DataList(new TObjectList());
  6186. if (DebugAlwaysTrue(ManagedSession != NULL))
  6187. {
  6188. // Update ManagedSession->StateData
  6189. UpdateSession(ManagedSession);
  6190. }
  6191. Manager->SaveWorkspace(DataList.get());
  6192. return DataList.release();
  6193. }
  6194. //---------------------------------------------------------------------------
  6195. void __fastcall TCustomScpExplorerForm::DoSaveWorkspace(
  6196. const UnicodeString & Name, TObjectList * DataList, bool SavePasswords, bool Explicit)
  6197. {
  6198. WinConfiguration->LastWorkspace = Name;
  6199. if (!SavePasswords)
  6200. {
  6201. for (int Index = 0; Index < DataList->Count; Index++)
  6202. {
  6203. TSessionData * SessionData = dynamic_cast<TSessionData *>(DataList->Items[Index]);
  6204. if (SessionData->Link.IsEmpty())
  6205. {
  6206. SessionData->ClearSessionPasswords();
  6207. }
  6208. }
  6209. }
  6210. StoredSessions->NewWorkspace(Name, DataList);
  6211. // modified only
  6212. StoredSessions->Save(false, Explicit);
  6213. }
  6214. //---------------------------------------------------------------------------
  6215. UnicodeString __fastcall TCustomScpExplorerForm::WorkspaceName()
  6216. {
  6217. return
  6218. DefaultStr(WinConfiguration->LastWorkspace,
  6219. DefaultStr(WinConfiguration->AutoWorkspace, LoadStr(NEW_WORKSPACE)));
  6220. }
  6221. //---------------------------------------------------------------------------
  6222. bool __fastcall TCustomScpExplorerForm::SaveWorkspace(bool EnableAutoSave)
  6223. {
  6224. std::unique_ptr<TObjectList> DataList(DoCollectWorkspace());
  6225. bool AnyNonStoredSessionWithPassword = false;
  6226. bool AnyNonStoredNonWorkspaceSessionWithPassword = false;
  6227. bool AllNonStoredSessionsAnonymous = true;
  6228. TTerminalManager * Manager = TTerminalManager::Instance();
  6229. for (int Index = 0; Index < DataList->Count; Index++)
  6230. {
  6231. TSessionData * SessionData = dynamic_cast<TSessionData *>(DataList->Items[Index]);
  6232. if (SessionData->Link.IsEmpty())
  6233. {
  6234. if (SessionData->HasAnySessionPassword())
  6235. {
  6236. AnyNonStoredSessionWithPassword = true;
  6237. if (!Manager->Sessions[Index]->SessionData->IsWorkspace)
  6238. {
  6239. AnyNonStoredNonWorkspaceSessionWithPassword = true;
  6240. }
  6241. }
  6242. if (!SameText(SessionData->UserName, AnonymousUserName))
  6243. {
  6244. AllNonStoredSessionsAnonymous = false;
  6245. }
  6246. }
  6247. }
  6248. bool SavePasswords;
  6249. bool * PSavePasswords;
  6250. bool NotRecommendedSavingPasswords =
  6251. !CustomWinConfiguration->UseMasterPassword &&
  6252. !AllNonStoredSessionsAnonymous;
  6253. if (Configuration->DisablePasswordStoring ||
  6254. !AnyNonStoredSessionWithPassword)
  6255. {
  6256. PSavePasswords = NULL;
  6257. SavePasswords = false;
  6258. }
  6259. else
  6260. {
  6261. PSavePasswords = &SavePasswords;
  6262. SavePasswords =
  6263. !AnyNonStoredNonWorkspaceSessionWithPassword ||
  6264. !NotRecommendedSavingPasswords;
  6265. }
  6266. UnicodeString Name = WorkspaceName();
  6267. bool CreateShortcut = false;
  6268. bool Result =
  6269. DoSaveWorkspaceDialog(
  6270. Name, PSavePasswords, NotRecommendedSavingPasswords, CreateShortcut,
  6271. EnableAutoSave);
  6272. if (Result)
  6273. {
  6274. DoSaveWorkspace(Name, DataList.get(), SavePasswords, true);
  6275. if (CreateShortcut)
  6276. {
  6277. TOperationVisualizer Visualizer;
  6278. UnicodeString AdditionalParams = TProgramParams::FormatSwitch(DESKTOP_SWITCH);
  6279. CreateDesktopSessionShortCut(Name, L"", AdditionalParams, -1, WORKSPACE_ICON);
  6280. }
  6281. if (EnableAutoSave)
  6282. {
  6283. WinConfiguration->AutoSaveWorkspace = true;
  6284. WinConfiguration->AutoWorkspace = Name;
  6285. if (PSavePasswords != NULL)
  6286. {
  6287. WinConfiguration->AutoSaveWorkspacePasswords = SavePasswords;
  6288. }
  6289. }
  6290. }
  6291. return Result;
  6292. }
  6293. //---------------------------------------------------------------------------
  6294. void __fastcall TCustomScpExplorerForm::UpdateSession(TManagedTerminal * Session)
  6295. {
  6296. DebugAssert(IsLocalBrowserMode() == Session->LocalBrowser);
  6297. DebugAssert(Session == ManagedSession);
  6298. if (!IsLocalBrowserMode())
  6299. {
  6300. SAFE_DESTROY(Session->RemoteExplorerState);
  6301. if (WinConfiguration->PreservePanelState)
  6302. {
  6303. Session->RemoteExplorerState = RemoteDirView->SaveState();
  6304. }
  6305. }
  6306. UpdateSessionData(Session->StateData);
  6307. }
  6308. //---------------------------------------------------------------------------
  6309. void __fastcall TCustomScpExplorerForm::UpdateSessionData(TSessionData * Data)
  6310. {
  6311. // Keep in sync with TSessionData::CopyStateData
  6312. DebugAssert(Data != NULL);
  6313. // This is inconsistent with how color (for example) is handled.
  6314. Data->Name = ManagedSession->SessionData->Name;
  6315. if (!IsLocalBrowserMode())
  6316. {
  6317. // cannot use RemoteDirView->Path, because it is empty if connection
  6318. // was already closed
  6319. // also only peek, we may not be connected at all atm
  6320. // so make sure we do not try retrieving current directory from the server
  6321. // (particularly with FTP)
  6322. UnicodeString ACurrentDirectory = Terminal->PeekCurrentDirectory();
  6323. if (!ACurrentDirectory.IsEmpty())
  6324. {
  6325. Data->RemoteDirectory = ACurrentDirectory;
  6326. }
  6327. }
  6328. Data->Color = SessionColor;
  6329. }
  6330. //---------------------------------------------------------------------------
  6331. void __fastcall TCustomScpExplorerForm::ToolBarResize(TObject *Sender)
  6332. {
  6333. TTBXToolbar * Toolbar = dynamic_cast<TTBXToolbar*>(Sender);
  6334. DebugAssert(Toolbar != NULL);
  6335. for (int i = 0; i < Toolbar->Items->Count; i++)
  6336. {
  6337. TTBXCustomDropDownItem * DropDownItem;
  6338. DropDownItem = dynamic_cast<TTBXCustomDropDownItem *>(Toolbar->Items->Items[i]);
  6339. if (DropDownItem != NULL)
  6340. {
  6341. ToolbarItemResize(DropDownItem,
  6342. Toolbar->Width - (Toolbar->View->BaseSize.x - DropDownItem->EditWidth) -
  6343. Toolbar->NonClientWidth);
  6344. break;
  6345. }
  6346. }
  6347. }
  6348. //---------------------------------------------------------------------------
  6349. void __fastcall TCustomScpExplorerForm::ToolbarItemResize(TTBXCustomDropDownItem * Item, int Width)
  6350. {
  6351. Item->EditWidth = Width;
  6352. }
  6353. //---------------------------------------------------------------------------
  6354. void __fastcall TCustomScpExplorerForm::ToolbarGetBaseSize(
  6355. TTBCustomToolbar * Toolbar, TPoint & ASize)
  6356. {
  6357. for (int i = 0; i < Toolbar->Items->Count; i++)
  6358. {
  6359. TTBXCustomDropDownItem * DropDownItem;
  6360. DropDownItem = dynamic_cast<TTBXCustomDropDownItem *>(Toolbar->Items->Items[i]);
  6361. if (DropDownItem != NULL)
  6362. {
  6363. ASize.x -= DropDownItem->EditWidth;
  6364. ASize.x += ScaleByTextHeight(this, 50) /* minimal combo width */;
  6365. }
  6366. }
  6367. }
  6368. //---------------------------------------------------------------------------
  6369. void __fastcall TCustomScpExplorerForm::DoWarnLackOfTempSpace(
  6370. const UnicodeString Path, __int64 RequiredSpace, bool & Continue)
  6371. {
  6372. if (WinConfiguration->DDWarnLackOfTempSpace)
  6373. {
  6374. UnicodeString ADrive = ExtractFileDrive(ExpandFileName(Path));
  6375. if (!ADrive.IsEmpty())
  6376. {
  6377. __int64 FreeSpace = DiskFree((Byte)(ADrive[1]-'A'+1));
  6378. DebugAssert(RequiredSpace >= 0);
  6379. __int64 RequiredWithReserve;
  6380. RequiredWithReserve = (__int64)(RequiredSpace * WinConfiguration->DDWarnLackOfTempSpaceRatio);
  6381. if (FreeSpace < RequiredWithReserve)
  6382. {
  6383. unsigned int Result;
  6384. TMessageParams Params(mpNeverAskAgainCheck);
  6385. Result = MessageDialog(FMTLOAD(DD_WARN_LACK_OF_TEMP_SPACE, (Path,
  6386. FormatBytes(FreeSpace), FormatBytes(RequiredSpace))),
  6387. qtWarning, qaYes | qaNo, HELP_DD_WARN_LACK_OF_TEMP_SPACE, &Params);
  6388. if (Result == qaNeverAskAgain)
  6389. {
  6390. WinConfiguration->DDWarnLackOfTempSpace = false;
  6391. }
  6392. Continue = (Result == qaYes || Result == qaNeverAskAgain);
  6393. }
  6394. }
  6395. }
  6396. }
  6397. //---------------------------------------------------------------------------
  6398. void __fastcall TCustomScpExplorerForm::AddBookmark(TOperationSide Side)
  6399. {
  6400. DoOpenDirectoryDialog(odAddBookmark, Side);
  6401. }
  6402. //---------------------------------------------------------------------------
  6403. TStrings * __fastcall TCustomScpExplorerForm::CreateVisitedDirectories(TOperationSide Side)
  6404. {
  6405. // we should better use TCustomDirView::FCaseSensitive, but it is private
  6406. TStringList * VisitedDirectories = CreateSortedStringList(!IsSideLocalBrowser(Side));
  6407. try
  6408. {
  6409. TCustomDirView * DView = DirView(Side);
  6410. for (int Index = -DView->BackCount; Index <= DView->ForwardCount; Index++)
  6411. {
  6412. VisitedDirectories->Add(DView->HistoryPath[Index]);
  6413. }
  6414. }
  6415. catch (...)
  6416. {
  6417. delete VisitedDirectories;
  6418. throw;
  6419. }
  6420. return VisitedDirectories;
  6421. }
  6422. //---------------------------------------------------------------------------
  6423. void __fastcall TCustomScpExplorerForm::DoOpenDirectoryDialog(
  6424. TOpenDirectoryMode Mode, TOperationSide Side)
  6425. {
  6426. bool Continue = true;
  6427. if (Mode == odAddBookmark)
  6428. {
  6429. TMessageParams Params(mpNeverAskAgainCheck);
  6430. Params.NeverAskAgainTitle = LoadStr(ADD_BOOKMARK_SHARED);
  6431. Params.NeverAskAgainCheckedInitially = WinConfiguration->UseSharedBookmarks;
  6432. UnicodeString Message = MainInstructions(FMTLOAD(ADD_BOOKMARK_CONFIRM, (DirView(Side)->PathName)));
  6433. unsigned int Answer =
  6434. MessageDialog(Message,
  6435. qtConfirmation, qaOK | qaCancel, HELP_ADD_BOOKMARK_CONFIRM, &Params);
  6436. if (Answer == qaNeverAskAgain)
  6437. {
  6438. Continue = true;
  6439. WinConfiguration->UseSharedBookmarks = true;
  6440. }
  6441. else if (Answer == qaOK)
  6442. {
  6443. Continue = true;
  6444. WinConfiguration->UseSharedBookmarks = false;
  6445. }
  6446. else
  6447. {
  6448. Continue = false;
  6449. }
  6450. }
  6451. if (Continue)
  6452. {
  6453. TStrings * VisitedDirectories = CreateVisitedDirectories(Side);
  6454. try
  6455. {
  6456. UnicodeString Name = DirView(Side)->PathName;
  6457. bool Repeat;
  6458. do
  6459. {
  6460. Repeat = false;
  6461. TOperationSide BookmarkSide = IsSideLocalBrowser(Side) ? osLocal : osRemote;
  6462. if (::DoOpenDirectoryDialog(Mode, BookmarkSide, Name, VisitedDirectories, Terminal,
  6463. // do not allow switching to location profiles,
  6464. // if we are not connected
  6465. HasDirView[osLocal] && (Terminal != NULL)))
  6466. {
  6467. TWindowLock Lock(this);
  6468. if (!TryOpenDirectory(Side, Name))
  6469. {
  6470. Repeat = true;
  6471. Mode = odBrowse;
  6472. }
  6473. }
  6474. }
  6475. while (Repeat);
  6476. }
  6477. __finally
  6478. {
  6479. delete VisitedDirectories;
  6480. }
  6481. }
  6482. }
  6483. //---------------------------------------------------------------------------
  6484. bool __fastcall TCustomScpExplorerForm::CommandSessionFallback()
  6485. {
  6486. DebugAssert(!Terminal->CommandSessionOpened);
  6487. return TTerminalManager::Instance()->ConnectTerminal(Terminal->CommandSession);
  6488. }
  6489. //---------------------------------------------------------------------------
  6490. bool __fastcall TCustomScpExplorerForm::EnsureCommandSessionFallback(TFSCapability Capability)
  6491. {
  6492. bool Result = Terminal->IsCapable[Capability] ||
  6493. Terminal->CommandSessionOpened;
  6494. if (!Result)
  6495. {
  6496. DebugAssert(Terminal->IsCapable[fcSecondaryShell]);
  6497. if (!GUIConfiguration->ConfirmCommandSession)
  6498. {
  6499. Result = true;
  6500. }
  6501. else
  6502. {
  6503. TMessageParams Params(mpNeverAskAgainCheck);
  6504. const TFileSystemInfo & FileSystemInfo = Terminal->GetFileSystemInfo();
  6505. unsigned int Answer = MessageDialog(FMTLOAD(PERFORM_ON_COMMAND_SESSION2,
  6506. (FileSystemInfo.ProtocolName, FileSystemInfo.ProtocolName)), qtConfirmation,
  6507. qaOK | qaCancel, HELP_PERFORM_ON_COMMAND_SESSION, &Params);
  6508. if (Answer == qaNeverAskAgain)
  6509. {
  6510. GUIConfiguration->ConfirmCommandSession = false;
  6511. Result = true;
  6512. }
  6513. else if (Answer == qaOK)
  6514. {
  6515. Result = true;
  6516. }
  6517. }
  6518. if (Result)
  6519. {
  6520. Result = CommandSessionFallback();
  6521. }
  6522. }
  6523. return Result;
  6524. }
  6525. //---------------------------------------------------------------------------
  6526. void __fastcall TCustomScpExplorerForm::OpenConsole(UnicodeString Command)
  6527. {
  6528. if (EnsureCommandSessionFallback(fcAnyCommand))
  6529. {
  6530. DoConsoleDialog(Terminal, Command);
  6531. }
  6532. }
  6533. //---------------------------------------------------------------------------
  6534. void __fastcall TCustomScpExplorerForm::FileControlDDDragEnter(
  6535. TObject *Sender, _di_IDataObject /*DataObj*/, int /*grfKeyState*/,
  6536. const TPoint & /*Point*/, int & /*dwEffect*/, bool & Accept)
  6537. {
  6538. if (!IsSideLocalBrowser(osRemote) &&
  6539. IsFileControl(DropSourceControl, osRemote) &&
  6540. (FDDExtMapFile != NULL))
  6541. {
  6542. Accept = true;
  6543. }
  6544. FDDTargetControl = dynamic_cast<TControl*>(Sender);
  6545. DebugAssert(FDDTargetControl != NULL);
  6546. }
  6547. //---------------------------------------------------------------------------
  6548. void __fastcall TCustomScpExplorerForm::SessionsDDDragEnter(
  6549. _di_IDataObject DataObj, int KeyState,
  6550. const TPoint & Point, int & Effect, bool & Accept)
  6551. {
  6552. FileControlDDDragEnter(SessionsPageControl, DataObj, KeyState, Point, Effect, Accept);
  6553. }
  6554. //---------------------------------------------------------------------------
  6555. void __fastcall TCustomScpExplorerForm::QueueDDDragEnter(
  6556. _di_IDataObject DataObj, int KeyState,
  6557. const TPoint & Point, int & Effect, bool & Accept)
  6558. {
  6559. FileControlDDDragEnter(QueueView3, DataObj, KeyState, Point, Effect, Accept);
  6560. }
  6561. //---------------------------------------------------------------------------
  6562. void __fastcall TCustomScpExplorerForm::FileControlDDDragLeave(
  6563. TObject * Sender)
  6564. {
  6565. DebugUsedParam(Sender);
  6566. DebugAssert(FDDTargetControl == Sender);
  6567. FDDTargetControl = NULL;
  6568. }
  6569. //---------------------------------------------------------------------------
  6570. void __fastcall TCustomScpExplorerForm::SessionsDDDragLeave()
  6571. {
  6572. FileControlDDDragLeave(SessionsPageControl);
  6573. }
  6574. //---------------------------------------------------------------------------
  6575. void __fastcall TCustomScpExplorerForm::QueueDDDragLeave()
  6576. {
  6577. FileControlDDDragLeave(QueueView3);
  6578. }
  6579. //---------------------------------------------------------------------------
  6580. void __fastcall TCustomScpExplorerForm::AddEditLink(TOperationSide Side, bool Add)
  6581. {
  6582. DebugAssert(!IsSideLocalBrowser(Side));
  6583. DebugUsedParam(Side);
  6584. bool Edit = false;
  6585. TRemoteFile * File = NULL;
  6586. UnicodeString FileName;
  6587. UnicodeString PointTo;
  6588. bool SymbolicLink = true;
  6589. if (RemoteDirView->ItemFocused)
  6590. {
  6591. DebugAssert(RemoteDirView->ItemFocused->Data);
  6592. File = (TRemoteFile *)RemoteDirView->ItemFocused->Data;
  6593. Edit = !Add && File->IsSymLink && Terminal->SessionData->ResolveSymlinks;
  6594. if (Edit)
  6595. {
  6596. FileName = File->FileName;
  6597. PointTo = File->LinkTo;
  6598. }
  6599. else if (!File->IsParentDirectory)
  6600. {
  6601. PointTo = File->FileName;
  6602. }
  6603. }
  6604. if (DoSymlinkDialog(FileName, PointTo, osRemote, SymbolicLink, Edit,
  6605. Terminal->IsCapable[fcHardLink]))
  6606. {
  6607. Configuration->Usage->Inc(L"RemoteLinksCreated");
  6608. if (Edit)
  6609. {
  6610. DebugAssert(File->FileName == FileName);
  6611. int Params = dfNoRecursive;
  6612. Terminal->ExceptionOnFail = true;
  6613. try
  6614. {
  6615. Terminal->DeleteFile(L"", File, &Params);
  6616. }
  6617. __finally
  6618. {
  6619. Terminal->ExceptionOnFail = false;
  6620. }
  6621. }
  6622. Terminal->CreateLink(FileName, PointTo, SymbolicLink);
  6623. }
  6624. }
  6625. //---------------------------------------------------------------------------
  6626. bool __fastcall TCustomScpExplorerForm::CanAddEditLink(TOperationSide Side)
  6627. {
  6628. return
  6629. (IsSideLocalBrowser(Side) ||
  6630. (HasActiveTerminal() &&
  6631. Terminal->ResolvingSymlinks &&
  6632. Terminal->IsCapable[fcSymbolicLink]));
  6633. }
  6634. //---------------------------------------------------------------------------
  6635. bool __fastcall TCustomScpExplorerForm::LinkFocused()
  6636. {
  6637. return
  6638. !IsSideLocalBrowser(FCurrentSide) &&
  6639. (RemoteDirView->ItemFocused != NULL) &&
  6640. ((TRemoteFile *)RemoteDirView->ItemFocused->Data)->IsSymLink &&
  6641. Terminal->SessionData->ResolveSymlinks;
  6642. }
  6643. //---------------------------------------------------------------------------
  6644. void __fastcall TCustomScpExplorerForm::ExecuteCurrentFile()
  6645. {
  6646. DebugAssert(!WinConfiguration->DisableOpenEdit);
  6647. FForceExecution = true;
  6648. try
  6649. {
  6650. DirView(osCurrent)->ExecuteCurrentFile();
  6651. }
  6652. __finally
  6653. {
  6654. FForceExecution = false;
  6655. }
  6656. }
  6657. //---------------------------------------------------------------------------
  6658. void __fastcall TCustomScpExplorerForm::ExecuteCurrentFileWith(bool OnFocused)
  6659. {
  6660. TEditorData ExternalEditor;
  6661. ExternalEditor.Editor = edExternal;
  6662. bool Remember = false;
  6663. if (DoEditorPreferencesDialog(&ExternalEditor, Remember, epmAdHoc, !IsSideLocalBrowser(osCurrent)))
  6664. {
  6665. if (Remember)
  6666. {
  6667. TEditorList * EditorList = new TEditorList;
  6668. try
  6669. {
  6670. *EditorList = *WinConfiguration->EditorList;
  6671. bool Found = false;
  6672. int i = 0;
  6673. while (!Found && (i < EditorList->Count))
  6674. {
  6675. const TEditorPreferences * Editor = EditorList->Editors[i];
  6676. if ((Editor->Data->Editor == edExternal) &&
  6677. (Editor->Data->ExternalEditor == ExternalEditor.ExternalEditor))
  6678. {
  6679. Found = true;
  6680. }
  6681. i++;
  6682. }
  6683. if (!Found)
  6684. {
  6685. EditorList->Add(new TEditorPreferences(ExternalEditor));
  6686. WinConfiguration->EditorList = EditorList;
  6687. }
  6688. }
  6689. __finally
  6690. {
  6691. delete EditorList;
  6692. }
  6693. }
  6694. ExecuteFile(osCurrent, efExternalEditor, &ExternalEditor, true, OnFocused);
  6695. }
  6696. }
  6697. //---------------------------------------------------------------------------
  6698. void __fastcall TCustomScpExplorerForm::DetachTerminal(TObject * ATerminal)
  6699. {
  6700. if (FFileFindTerminal == ATerminal)
  6701. {
  6702. FFileFindTerminal = NULL;
  6703. HideFileFindDialog();
  6704. }
  6705. if (FClipboardTerminal == ATerminal)
  6706. {
  6707. ClipboardClear(); // implies ClipboardStop
  6708. }
  6709. if ((FOnSynchronizeAbort != NULL) &&
  6710. (ATerminal == FManagedSession))
  6711. {
  6712. FOnSynchronizeAbort(NULL);
  6713. }
  6714. }
  6715. //---------------------------------------------------------------------------
  6716. void __fastcall TCustomScpExplorerForm::TerminalRemoved(TObject * Sender)
  6717. {
  6718. FEditorManager->ProcessFiles(FileTerminalRemoved, Sender);
  6719. DetachTerminal(Sender);
  6720. }
  6721. //---------------------------------------------------------------------------
  6722. void __fastcall TCustomScpExplorerForm::FileTerminalRemoved(const UnicodeString FileName,
  6723. TEditedFileData * Data, TObject * /*Token*/, void * Arg)
  6724. {
  6725. TTerminal * Terminal = static_cast<TTerminal *>(Arg);
  6726. DebugAssert(Terminal != NULL);
  6727. if (Data->Terminal == Terminal)
  6728. {
  6729. Data->Terminal = NULL;
  6730. }
  6731. }
  6732. //---------------------------------------------------------------------------
  6733. TManagedTerminal * TCustomScpExplorerForm::GetReplacementForLastSession()
  6734. {
  6735. return NULL;
  6736. }
  6737. //---------------------------------------------------------------------------
  6738. void __fastcall TCustomScpExplorerForm::LastTerminalClosed()
  6739. {
  6740. UpdateControls();
  6741. SessionColor = TColor(0);
  6742. UpdateRemotePathComboBox(false);
  6743. NeedSession(false);
  6744. }
  6745. //---------------------------------------------------------------------------
  6746. void __fastcall TCustomScpExplorerForm::NeedSession(bool Startup)
  6747. {
  6748. try
  6749. {
  6750. // Cache, as the login dialog can change its value.
  6751. // See also DoShow. Also see TTerminalManager::NewSession.
  6752. bool ShowLogin = WinConfiguration->ShowLoginWhenNoSession;
  6753. try
  6754. {
  6755. if (ShowLogin)
  6756. {
  6757. bool ReloadSessions = !Startup;
  6758. TTerminalManager::Instance()->NewSession(L"", ReloadSessions, this, true);
  6759. }
  6760. }
  6761. __finally
  6762. {
  6763. // Do not terminate, if we are only starting up and we are not showing Login dialog
  6764. // (so there was no chance for the user to open any session yet)
  6765. if ((ShowLogin || !Startup) &&
  6766. !WinConfiguration->KeepOpenWhenNoSession &&
  6767. // this rules out the implicit Commander's local-local browser
  6768. ((Terminal == NULL) || (!Terminal->Active && !Terminal->Permanent)))
  6769. {
  6770. TerminateApplication();
  6771. }
  6772. }
  6773. }
  6774. catch (EAbort &)
  6775. {
  6776. // swallow
  6777. // The TTerminalManager does not expect the OnLastTerminalClose to throw without trying to connect.
  6778. // Also when called from TWinControl.UpdateShowing => CMShowingChanged, the showing is aborted on exception
  6779. }
  6780. }
  6781. //---------------------------------------------------------------------------
  6782. void __fastcall TCustomScpExplorerForm::SessionListChanged(bool ForceTruncationUpdate)
  6783. {
  6784. TTerminalManager * Manager = TTerminalManager::Instance();
  6785. DebugAssert(!Manager->Updating);
  6786. TAutoFlag UpdatingSessionTabsFlag(FUpdatingSessionTabs);
  6787. int ActiveSessionIndex = Manager->ActiveSessionIndex;
  6788. Configuration->Usage->SetMax(L"MaxOpenedSessions", Manager->Count);
  6789. SendMessage(SessionsPageControl->Handle, WM_SETREDRAW, 0, 0);
  6790. try
  6791. {
  6792. int PrevTabsWidth = SessionsPageControl->TotalTabsWidth();
  6793. while (SessionsPageControl->PageCount > Manager->Count + 1)
  6794. {
  6795. delete SessionsPageControl->Pages[SessionsPageControl->PageCount - 1];
  6796. }
  6797. for (int Index = 0; Index <= Manager->Count; Index++)
  6798. {
  6799. TThemeTabSheet * TabSheet;
  6800. if (Index >= SessionsPageControl->PageCount)
  6801. {
  6802. TabSheet = new TThemeTabSheet(SessionsPageControl);
  6803. TabSheet->PageControl = SessionsPageControl;
  6804. }
  6805. else
  6806. {
  6807. TabSheet = SessionsPageControl->Pages[Index];
  6808. }
  6809. bool IsSessionTab = (Index < Manager->Count);
  6810. if (IsSessionTab)
  6811. {
  6812. TTerminal * Terminal = Manager->Sessions[Index];
  6813. TabSheet->Tag = reinterpret_cast<int>(Terminal);
  6814. TabSheet->CaptionTruncation = tttEllipsis;
  6815. UpdateSessionTab(TabSheet);
  6816. }
  6817. else
  6818. {
  6819. TabSheet->Tag = 0; // not really needed
  6820. TabSheet->Shadowed = false;
  6821. TabSheet->Button = SupportsLocalBrowser() ? ttbDropDown : ttbNone;
  6822. TabSheet->CaptionTruncation = tttNoText;
  6823. // 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)
  6824. UpdateNewTabTab();
  6825. }
  6826. }
  6827. // Prevent flicker on the right side where scrolling buttons would be, unless really needed
  6828. if (ForceTruncationUpdate || (SessionsPageControl->TotalTabsWidth() != PrevTabsWidth))
  6829. {
  6830. SessionsPageControl->UpdateTabsCaptionTruncation();
  6831. }
  6832. }
  6833. __finally
  6834. {
  6835. SendMessage(SessionsPageControl->Handle, WM_SETREDRAW, 1, 0);
  6836. }
  6837. SessionsPageControl->ActivePageIndex = ActiveSessionIndex;
  6838. }
  6839. //---------------------------------------------------------------------------
  6840. UnicodeString TCustomScpExplorerForm::GetNewTabTabCaption()
  6841. {
  6842. return StripHotkey(StripTrailingPunctuation(NonVisualDataModule->NewTabAction->Caption));
  6843. }
  6844. //---------------------------------------------------------------------------
  6845. void __fastcall TCustomScpExplorerForm::UpdateNewTabTab()
  6846. {
  6847. TThemeTabSheet * TabSheet = SessionsPageControl->Pages[SessionsPageControl->PageCount - 1];
  6848. UnicodeString TabCaption;
  6849. if (WinConfiguration->SelectiveToolbarText)
  6850. {
  6851. TabCaption = GetNewTabTabCaption();
  6852. }
  6853. TabSheet->BaseCaption = TabCaption;
  6854. TabSheet->ImageIndex = GetNewTabTabImageIndex(osCurrent);
  6855. }
  6856. //---------------------------------------------------------------------------
  6857. TManagedTerminal * __fastcall TCustomScpExplorerForm::GetSessionTabSession(TTabSheet * TabSheet)
  6858. {
  6859. return reinterpret_cast<TManagedTerminal *>(TabSheet->Tag);
  6860. }
  6861. //---------------------------------------------------------------------------
  6862. UnicodeString TCustomScpExplorerForm::GetLocalBrowserSessionTitle(TManagedTerminal *)
  6863. {
  6864. DebugFail();
  6865. return UnicodeString();
  6866. }
  6867. //---------------------------------------------------------------------------
  6868. void TCustomScpExplorerForm::UpdateSessionTab(TThemeTabSheet * TabSheet)
  6869. {
  6870. if (DebugAlwaysTrue(TabSheet != NULL))
  6871. {
  6872. TManagedTerminal * ASession = GetSessionTabSession(TabSheet);
  6873. if (DebugAlwaysTrue(ASession != NULL))
  6874. {
  6875. TColor Color = (ASession == Terminal) ? FSessionColor : ASession->StateData->Color;
  6876. if (ASession->LocalBrowser)
  6877. {
  6878. TabSheet->ImageIndex = FLocalBrowserTabImageIndex;
  6879. }
  6880. else
  6881. {
  6882. TabSheet->ImageIndex = AddSessionColor(Color);
  6883. }
  6884. TTerminalManager * Manager = TTerminalManager::Instance();
  6885. TabSheet->Shadowed = !ASession->Active && !ASession->LocalBrowser;
  6886. TabSheet->Button = CanCloseSession(ASession) ? ttbClose : ttbNone;
  6887. TabSheet->BaseCaption = Manager->GetSessionTitle(ASession, true);
  6888. }
  6889. }
  6890. }
  6891. //---------------------------------------------------------------------------
  6892. bool TCustomScpExplorerForm::CanCloseSession(TManagedTerminal * Session)
  6893. {
  6894. return !Session->LocalBrowser || (TTerminalManager::Instance()->Count > 1);
  6895. }
  6896. //---------------------------------------------------------------------------
  6897. bool __fastcall TCustomScpExplorerForm::SessionTabSwitched()
  6898. {
  6899. DebugAssert(SessionsPageControl->ActivePage != NULL);
  6900. TManagedTerminal * Session = GetSessionTabSession(SessionsPageControl->ActivePage);
  6901. bool Result = (Session != NULL);
  6902. if (Result)
  6903. {
  6904. TTerminalManager::Instance()->ActiveSession = Session;
  6905. }
  6906. else
  6907. {
  6908. try
  6909. {
  6910. NewTab();
  6911. }
  6912. __finally
  6913. {
  6914. SessionListChanged();
  6915. }
  6916. FSessionsPageControlNewTabTime = Now();
  6917. }
  6918. return Result;
  6919. }
  6920. //---------------------------------------------------------------------------
  6921. void __fastcall TCustomScpExplorerForm::SessionsPageControlChange(TObject * /*Sender*/)
  6922. {
  6923. TWindowLock Lock(this);
  6924. SessionTabSwitched();
  6925. }
  6926. //---------------------------------------------------------------------------
  6927. void __fastcall TCustomScpExplorerForm::TransferListChange(TObject * Sender)
  6928. {
  6929. TTBXStringList * TransferList = dynamic_cast<TTBXStringList *>(Sender);
  6930. DebugAssert(TransferList != NULL);
  6931. UnicodeString Name;
  6932. if (TransferList->ItemIndex <= 0)
  6933. {
  6934. Name = L"";
  6935. }
  6936. else
  6937. {
  6938. Name = GUIConfiguration->CopyParamList->Names[TransferList->ItemIndex - 1];
  6939. }
  6940. if (FCopyParamAutoSelected.IsEmpty())
  6941. {
  6942. // if previous preset was not autoselected, make new preset the "default"
  6943. FCopyParamDefault = Name;
  6944. }
  6945. GUIConfiguration->CopyParamCurrent = Name;
  6946. }
  6947. //---------------------------------------------------------------------------
  6948. void __fastcall TCustomScpExplorerForm::UpdateTransferLabel()
  6949. {
  6950. // sanity check
  6951. bool ExistingPreset =
  6952. (FTransferListHoverIndex >= 0) &&
  6953. (FTransferListHoverIndex < 1 + GUIConfiguration->CopyParamList->Count);
  6954. DebugAssert(ExistingPreset);
  6955. if (ExistingPreset)
  6956. {
  6957. HDC DC = GetDC(0);
  6958. TCanvas * Canvas = new TCanvas();
  6959. try
  6960. {
  6961. Canvas->Handle = DC;
  6962. Canvas->Font = GetToolbarFont(this);
  6963. UnicodeString Name;
  6964. if (FTransferListHoverIndex == 0)
  6965. {
  6966. Name = L"";
  6967. }
  6968. else
  6969. {
  6970. Name = GUIConfiguration->CopyParamList->Names[FTransferListHoverIndex - 1];
  6971. }
  6972. TTBXLabelItem * TransferLabel = dynamic_cast<TTBXLabelItem*>(
  6973. static_cast<TComponent*>(GetComponent(fcTransferLabel)));
  6974. TTBXStringList * TransferList = dynamic_cast<TTBXStringList*>(
  6975. static_cast<TObject*>(GetComponent(fcTransferList)));
  6976. UnicodeString InfoStr =
  6977. GUIConfiguration->CopyParamPreset[Name].
  6978. GetInfoStr(L"; ",
  6979. FLAGMASK(HasActiveTerminal(), Terminal->UsableCopyParamAttrs(0).General));
  6980. int MaxWidth = TransferList->MinWidth - (2 * TransferLabel->Margin) - ScaleByTextHeight(this, 10);
  6981. if (Canvas->TextExtent(InfoStr).cx > MaxWidth)
  6982. {
  6983. while (Canvas->TextExtent(InfoStr + Ellipsis).cx > MaxWidth)
  6984. {
  6985. InfoStr.SetLength(InfoStr.Length() - 1);
  6986. }
  6987. InfoStr += Ellipsis;
  6988. }
  6989. // UpdateCaption does not cause invalidation of whole submenu, while
  6990. // setting Caption property does.
  6991. // also it probably does not resize the label, even if necessary
  6992. // (we do not want that anyway)
  6993. TransferLabel->UpdateCaption(InfoStr);
  6994. }
  6995. __finally
  6996. {
  6997. Canvas->Handle = NULL;
  6998. ReleaseDC(0, DC);
  6999. delete Canvas;
  7000. }
  7001. }
  7002. }
  7003. //---------------------------------------------------------------------------
  7004. void __fastcall TCustomScpExplorerForm::TransferListDrawItem(
  7005. TTBXCustomList * /*Sender*/, TCanvas * /*ACanvas*/, const TRect & /*ARect*/,
  7006. int /*AIndex*/, int AHoverIndex, bool & /*DrawDefault*/)
  7007. {
  7008. if (FTransferListHoverIndex != AHoverIndex)
  7009. {
  7010. FTransferListHoverIndex = AHoverIndex;
  7011. UpdateTransferLabel();
  7012. }
  7013. }
  7014. //---------------------------------------------------------------------------
  7015. void __fastcall TCustomScpExplorerForm::WMAppCommand(TMessage & Message)
  7016. {
  7017. int Command = GET_APPCOMMAND_LPARAM(Message.LParam);
  7018. TShiftState Shift = KeyDataToShiftState(GET_KEYSTATE_LPARAM(Message.LParam));
  7019. if ((Shift * (TShiftState() << ssShift << ssAlt << ssCtrl)).Empty())
  7020. {
  7021. if (Command == APPCOMMAND_BROWSER_FAVORITES)
  7022. {
  7023. if (!NonVisualDataModule->Busy)
  7024. {
  7025. OpenDirectory(GetSide(osCurrent));
  7026. }
  7027. Message.Result = 1;
  7028. }
  7029. else
  7030. {
  7031. TForm::Dispatch(&Message);
  7032. }
  7033. }
  7034. else
  7035. {
  7036. TForm::Dispatch(&Message);
  7037. }
  7038. }
  7039. //---------------------------------------------------------------------------
  7040. void __fastcall TCustomScpExplorerForm::CMDialogChar(TMessage & AMessage)
  7041. {
  7042. TCMDialogChar & Message = reinterpret_cast<TCMDialogChar &>(AMessage);
  7043. if ((FIgnoreNextDialogChar != 0) &&
  7044. (toupper(Message.CharCode) == toupper(FIgnoreNextDialogChar)))
  7045. {
  7046. Message.Result = 1;
  7047. }
  7048. else
  7049. {
  7050. TForm::Dispatch(&Message);
  7051. }
  7052. FIgnoreNextDialogChar = 0;
  7053. }
  7054. //---------------------------------------------------------------------------
  7055. void __fastcall TCustomScpExplorerForm::WMSysCommand(TMessage & Message)
  7056. {
  7057. // The four low-order bits are used internally by Windows
  7058. unsigned int Cmd = (Message.WParam & 0xFFF0);
  7059. // SC_RESTORE, SC_MAXIMIZE, SC_MINIMIZE - buttons on windows title
  7060. // SC_DEFAULT - double click on windows title (does not work, at least on WinXP)
  7061. // 61730 - restore thru double click - undocumented
  7062. // 61490 - maximize thru double click - undocumented
  7063. if ((Cmd == SC_RESTORE) || (Cmd == SC_MAXIMIZE) ||
  7064. (Cmd == SC_MINIMIZE) || (Cmd == SC_DEFAULT))
  7065. {
  7066. SysResizing(Cmd);
  7067. }
  7068. TForm::Dispatch(&Message);
  7069. }
  7070. //---------------------------------------------------------------------------
  7071. void __fastcall TCustomScpExplorerForm::WMQueryEndSession(TMessage & Message)
  7072. {
  7073. // We were actually never able to make ENDSESSION_CRITICAL happen.
  7074. // Also there no point returning TRUE as we are not able to
  7075. // handle the abrupt termination caused by subsequent WM_ENDSESSION cleanly.
  7076. // Hence the process termination might be safer :)
  7077. if ((Message.LParam != ENDSESSION_CRITICAL) &&
  7078. (((FQueue != NULL) && !FQueue->IsEmpty) || (FProgressForm != NULL)))
  7079. {
  7080. Message.Result = FALSE;
  7081. }
  7082. else
  7083. {
  7084. Message.Result = TRUE;
  7085. }
  7086. // Do not call default handling as that triggers OnCloseQuery,
  7087. // where our implementation will popup confirmation dialogs, what we do not want,
  7088. // as per Vista guidelines:
  7089. // https://learn.microsoft.com/en-us/windows/win32/shutdown/shutting-down
  7090. }
  7091. //---------------------------------------------------------------------------
  7092. void __fastcall TCustomScpExplorerForm::WMEndSession(TWMEndSession & Message)
  7093. {
  7094. if (Message.EndSession && IsApplicationMinimized())
  7095. {
  7096. // WORKAROUND
  7097. // TApplication.WndProc() calls Application.Terminate() before Halt(),
  7098. // when it receives WM_ENDSESSION,
  7099. // but that sometimes (particularly when application is minimized) cause crashes.
  7100. // Crash popups message that blocks log off.
  7101. // Obviously application cannot shutdown cleanly after WM_ENDSESSION,
  7102. // so we call ExitProcess() immediately, not even trying to cleanup.
  7103. // It still causes beep, so there's likely some popup, but it does not block
  7104. // log off.
  7105. ExitProcess(0);
  7106. }
  7107. TForm::Dispatch(&Message);
  7108. }
  7109. #ifdef _DEBUG
  7110. //---------------------------------------------------------------------------
  7111. void __fastcall TCustomScpExplorerForm::WMWindowPosChanged(TWMWindowPosMsg & Message)
  7112. {
  7113. TForm::Dispatch(&Message);
  7114. }
  7115. #endif
  7116. //---------------------------------------------------------------------------
  7117. void __fastcall TCustomScpExplorerForm::SysResizing(unsigned int /*Cmd*/)
  7118. {
  7119. }
  7120. //---------------------------------------------------------------------------
  7121. void __fastcall TCustomScpExplorerForm::DoShow()
  7122. {
  7123. // Called from TCustomScpExplorerForm::CMShowingChanged
  7124. // only now are the controls resized finally, so the size constraints
  7125. // will not conflict with possibly very small window size
  7126. RestoreFormParams();
  7127. // This indicates that the form was rescaled after parameters were loaded and
  7128. // as most probably the parameters were actually saved with the current form scaling
  7129. // (windows was the last time likely closed on the same monitor, where it is starting now),
  7130. // rescaling happened twice already. Reload the parameters, as now they likely won't need any scaling
  7131. // and no rounding problems will occur.
  7132. // See also RestoreForm.
  7133. // (we should be safe to call RestoreParams unconditionally)
  7134. if (PixelsPerInch != Screen->PixelsPerInch)
  7135. {
  7136. RestoreParams();
  7137. }
  7138. FixControlsPlacement();
  7139. if (Position == poDefaultPosOnly)
  7140. {
  7141. CutFormToDesktop(this);
  7142. }
  7143. TForm::DoShow();
  7144. FSessionsDragDropFilesEx->DragDropControl = SessionsPageControl;
  7145. FQueueDragDropFilesEx->DragDropControl = QueueView3;
  7146. // This was previously in NeedSession, where other related code is.
  7147. // But that is called from CMShowingChanged, when the window is already visible and the local-local window state
  7148. // unpleasantly shows briefly.
  7149. if ((ManagedSession == NULL) &&
  7150. !WinConfiguration->ShowLoginWhenNoSession &&
  7151. WinConfiguration->AutoSaveWorkspace && !WinConfiguration->AutoWorkspace.IsEmpty() &&
  7152. // This detects if workspace was saved the last time the main widow was closed
  7153. SameText(WinConfiguration->LastStoredSession, WinConfiguration->AutoWorkspace))
  7154. {
  7155. DoOpenFolderOrWorkspace(WinConfiguration->AutoWorkspace, false, true);
  7156. Configuration->Usage->Inc(L"OpenedWorkspacesAuto");
  7157. }
  7158. // Before main window displays
  7159. if (ManagedSession == NULL)
  7160. {
  7161. StartingWithoutSession();
  7162. }
  7163. UpdatePixelsPerInchMainWindowCounter();
  7164. FShowing = true;
  7165. }
  7166. //---------------------------------------------------------------------------
  7167. void __fastcall TCustomScpExplorerForm::UpdatePixelsPerInchMainWindowCounter()
  7168. {
  7169. Configuration->Usage->Set(L"PixelsPerInchMainWindow", PixelsPerInch);
  7170. }
  7171. //---------------------------------------------------------------------------
  7172. void __fastcall TCustomScpExplorerForm::StartingWithoutSession()
  7173. {
  7174. SessionListChanged();
  7175. InitStatusBar();
  7176. UpdateControls();
  7177. }
  7178. //---------------------------------------------------------------------------
  7179. void __fastcall TCustomScpExplorerForm::PopupTrayBalloon(TTerminal * Terminal,
  7180. const UnicodeString & Str, TQueryType Type, Exception * E, unsigned int Seconds,
  7181. TNotifyEvent OnBalloonClick, TObject * UserData)
  7182. {
  7183. bool Do;
  7184. UnicodeString Message;
  7185. if (E == NULL)
  7186. {
  7187. Message = Str;
  7188. Do = true;
  7189. }
  7190. else
  7191. {
  7192. Do = ExceptionMessage(E, Message);
  7193. }
  7194. if (Do && WinConfiguration->BalloonNotifications)
  7195. {
  7196. UnicodeString Title;
  7197. if ((Terminal == NULL) && (Type == qtInformation) && ExtractMainInstructions(Message, Title))
  7198. {
  7199. Message = TrimLeft(Message);
  7200. }
  7201. else
  7202. {
  7203. Message = UnformatMessage(Message);
  7204. const ResourceString * Captions[] = { &_SMsgDlgConfirm, &_SMsgDlgWarning,
  7205. &_SMsgDlgError, &_SMsgDlgInformation, NULL };
  7206. Title = LoadResourceString(Captions[Type]);
  7207. if (Terminal != NULL)
  7208. {
  7209. TManagedTerminal * Session = DebugNotNull(dynamic_cast<TManagedTerminal *>(Terminal));
  7210. Title = TTerminalManager::Instance()->GetSessionTitle(Session, true) + TitleSeparator + Title;
  7211. }
  7212. }
  7213. if (Seconds == 0)
  7214. {
  7215. Seconds = WinConfiguration->NotificationsTimeout;
  7216. }
  7217. FTrayIcon->PopupBalloon(Title, Message, Type, Seconds * MSecsPerSec, OnBalloonClick, UserData);
  7218. }
  7219. }
  7220. //---------------------------------------------------------------------------
  7221. unsigned int __fastcall TCustomScpExplorerForm::MoreMessageDialog(const UnicodeString Message,
  7222. TStrings * MoreMessages, TQueryType Type, unsigned int Answers,
  7223. UnicodeString HelpKeyword, const TMessageParams * Params,
  7224. TTerminal * Terminal)
  7225. {
  7226. if (((WinConfiguration->ContinueOnError && (FErrorList != NULL)) ||
  7227. (FOnFeedSynchronizeError != NULL)) &&
  7228. (Params != NULL) && (Params->Params & mpAllowContinueOnError) )
  7229. {
  7230. if (FOnFeedSynchronizeError != NULL)
  7231. {
  7232. FOnFeedSynchronizeError(Message, MoreMessages, Type, HelpKeyword);
  7233. }
  7234. else
  7235. {
  7236. DebugAssert(FErrorList != NULL);
  7237. TStringList * MoreMessagesCopy = NULL;
  7238. if (MoreMessages)
  7239. {
  7240. MoreMessagesCopy = new TStringList();
  7241. MoreMessagesCopy->Assign(MoreMessages);
  7242. }
  7243. FErrorList->AddObject(Message, MoreMessagesCopy);
  7244. }
  7245. PopupTrayBalloon(Terminal, Message, Type);
  7246. return ContinueAnswer(Answers);
  7247. }
  7248. else
  7249. {
  7250. bool UseBalloon = FTrayIcon->Visible;
  7251. if (UseBalloon)
  7252. {
  7253. PopupTrayBalloon(Terminal, Message, Type);
  7254. }
  7255. unsigned int Result;
  7256. try
  7257. {
  7258. Result = ::MoreMessageDialog(Message, MoreMessages, Type, Answers, HelpKeyword, Params);
  7259. }
  7260. __finally
  7261. {
  7262. // cancel only balloon we popped up, otherwise we may cancel notification
  7263. // balloon that was there before the message dialog
  7264. // (such as "dd confirmation opt in balloon)
  7265. if (UseBalloon)
  7266. {
  7267. FTrayIcon->CancelBalloon();
  7268. }
  7269. }
  7270. return Result;
  7271. }
  7272. }
  7273. //---------------------------------------------------------------------------
  7274. void __fastcall TCustomScpExplorerForm::ShowExtendedException(
  7275. TTerminal * Terminal, Exception * E)
  7276. {
  7277. if (FTrayIcon->Visible)
  7278. {
  7279. PopupTrayBalloon(Terminal, L"", qtError, E);
  7280. }
  7281. // particularly prevent opening new session from jump list,
  7282. // while exception is shown
  7283. NonVisualDataModule->StartBusy();
  7284. try
  7285. {
  7286. ShowExtendedExceptionEx(Terminal, E);
  7287. }
  7288. __finally
  7289. {
  7290. NonVisualDataModule->EndBusy();
  7291. FTrayIcon->CancelBalloon();
  7292. }
  7293. }
  7294. //---------------------------------------------------------------------------
  7295. void __fastcall TCustomScpExplorerForm::SessionReady()
  7296. {
  7297. InitStatusBar();
  7298. // cannot rely on active page being page for active terminal,
  7299. // as it can happen that active page is the "new session" page
  7300. // (e.g. when reconnecting active terminal, while login dialog
  7301. // invoked from "new session" page is modal)
  7302. int ActiveSessionIndex = TTerminalManager::Instance()->ActiveSessionIndex;
  7303. UpdateSessionTab(SessionsPageControl->Pages[ActiveSessionIndex]);
  7304. }
  7305. //---------------------------------------------------------------------------
  7306. void __fastcall TCustomScpExplorerForm::InactiveTerminalException(
  7307. TTerminal * Terminal, Exception * E)
  7308. {
  7309. Notify(Terminal, L"", qtError, false, NULL, NULL, E);
  7310. if (!Terminal->Active)
  7311. {
  7312. int Index = TTerminalManager::Instance()->IndexOf(Terminal);
  7313. if (DebugAlwaysTrue((Index >= 0) && (Index < SessionsPageControl->PageCount)))
  7314. {
  7315. UpdateSessionTab(SessionsPageControl->Pages[Index]);
  7316. }
  7317. }
  7318. }
  7319. //---------------------------------------------------------------------------
  7320. void __fastcall TCustomScpExplorerForm::Notify(TTerminal * Terminal,
  7321. UnicodeString Message, TQueryType Type,
  7322. bool Important, TNotifyEvent OnClick, TObject * UserData, Exception * E)
  7323. {
  7324. if ((E == NULL) ||
  7325. ExceptionMessage(E, Message))
  7326. {
  7327. unsigned int Seconds = WinConfiguration->NotificationsTimeout;
  7328. if (Important)
  7329. {
  7330. Seconds *= 5;
  7331. }
  7332. UnicodeString NoteMessage(UnformatMessage(Message));
  7333. if (Terminal != NULL)
  7334. {
  7335. TManagedTerminal * Session = DebugNotNull(dynamic_cast<TManagedTerminal *>(Terminal));
  7336. NoteMessage = FORMAT(L"%s: %s",
  7337. (TTerminalManager::Instance()->GetSessionTitle(Session, true), NoteMessage));
  7338. }
  7339. if (WinConfiguration->BalloonNotifications)
  7340. {
  7341. AddNote(NoteMessage);
  7342. PopupTrayBalloon(Terminal, Message, Type, NULL, Seconds, OnClick, UserData);
  7343. }
  7344. else
  7345. {
  7346. FlashOnBackground();
  7347. PostNote(NoteMessage, Seconds, OnClick, UserData);
  7348. }
  7349. }
  7350. }
  7351. //---------------------------------------------------------------------------
  7352. void __fastcall TCustomScpExplorerForm::QueueEmptyNoteClicked(TObject * Sender)
  7353. {
  7354. RestoreApp();
  7355. TTerminalNoteData * TerminalNoteData = dynamic_cast<TTerminalNoteData *>(Sender);
  7356. if (DebugAlwaysTrue(TerminalNoteData != NULL) &&
  7357. !NonVisualDataModule->Busy)
  7358. {
  7359. TManagedTerminal * Terminal = TerminalNoteData->Terminal;
  7360. TTerminalManager::Instance()->ActiveSession = Terminal;
  7361. if (!ComponentVisible[fcQueueView])
  7362. {
  7363. ToggleQueueVisibility();
  7364. GoToQueue();
  7365. }
  7366. }
  7367. }
  7368. //---------------------------------------------------------------------------
  7369. void __fastcall TCustomScpExplorerForm::QueueEvent(TManagedTerminal * ATerminal,
  7370. TTerminalQueue * /*Queue*/, TQueueEvent Event)
  7371. {
  7372. UnicodeString Message;
  7373. TNotifyEvent OnClick = NULL;
  7374. TObject * UserData = NULL;
  7375. bool QueueInvisible = !ComponentVisible[fcQueueView] || IsApplicationMinimized();
  7376. switch (Event)
  7377. {
  7378. case qeEmptyButMonitored:
  7379. if ((ATerminal != Terminal) || QueueInvisible)
  7380. {
  7381. Message = LoadStr(BALLOON_QUEUE_EMPTY);
  7382. OnClick = QueueEmptyNoteClicked;
  7383. TTerminalNoteData * TerminalNoteData = new TTerminalNoteData();
  7384. TerminalNoteData->Terminal = ATerminal;
  7385. UserData = TerminalNoteData;
  7386. }
  7387. break;
  7388. case qeEmpty:
  7389. OperationComplete(ATerminal->QueueOperationStart);
  7390. break;
  7391. case qePendingUserAction:
  7392. if ((ATerminal != Terminal) ||
  7393. (QueueInvisible && !IsQueueAutoPopup()))
  7394. {
  7395. Message = LoadStr(BALLOON_QUEUE_USER_ACTION);
  7396. }
  7397. break;
  7398. default:
  7399. DebugFail();
  7400. }
  7401. if (!Message.IsEmpty())
  7402. {
  7403. Notify(ATerminal, Message, qtInformation, false, OnClick, UserData);
  7404. }
  7405. }
  7406. //---------------------------------------------------------------------------
  7407. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDCreateDragFileList(
  7408. TObject * /*Sender*/, TFileList * FileList, bool & Created)
  7409. {
  7410. if (FDDExtMapFile != NULL)
  7411. {
  7412. CloseHandle(FDDExtMapFile);
  7413. FDDExtMapFile = NULL;
  7414. }
  7415. if (WinConfiguration->DDFakeFile)
  7416. {
  7417. DDFakeFileInitDrag(FileList, Created);
  7418. }
  7419. }
  7420. //---------------------------------------------------------------------------
  7421. void __fastcall TCustomScpExplorerForm::DDFakeCreated(TObject * /*Sender*/, const UnicodeString FileName)
  7422. {
  7423. if (DebugAlwaysTrue(!FDragFakeDirectory.IsEmpty()) &&
  7424. SameText(ExtractFileName(FileName), ExtractFileName(FDragFakeDirectory)))
  7425. {
  7426. FFakeFileDropTarget = FileName;
  7427. }
  7428. }
  7429. //---------------------------------------------------------------------------
  7430. UnicodeString __fastcall TCustomScpExplorerForm::CreateFakeTransferDirectory()
  7431. {
  7432. UnicodeString Result = ExcludeTrailingBackslash(WinConfiguration->TemporaryDir());
  7433. if (!ForceDirectories(ApiPath(Result)))
  7434. {
  7435. throw Exception(FMTLOAD(CREATE_TEMP_DIR_ERROR, (Result)));
  7436. }
  7437. FileSetAttr(ApiPath(Result), faHidden);
  7438. return Result;
  7439. }
  7440. //---------------------------------------------------------------------------
  7441. void __fastcall TCustomScpExplorerForm::DDFakeFileInitDrag(TFileList * FileList,
  7442. bool & Created)
  7443. {
  7444. FFakeFileDropTarget = UnicodeString();
  7445. FDragFakeDirectory = CreateFakeTransferDirectory();
  7446. FileList->AddItem(NULL, FDragFakeDirectory);
  7447. Created = true;
  7448. if (!WinConfiguration->IsDDExtRunning() || WinConfiguration->IsDDExtBroken())
  7449. {
  7450. FDragFakeMonitors = StartCreationDirectoryMonitorsOnEachDrive(FILE_NOTIFY_CHANGE_DIR_NAME, DDFakeCreated);
  7451. }
  7452. FDDExtMapFile = CreateFileMappingA((HANDLE)0xFFFFFFFF, NULL, PAGE_READWRITE,
  7453. 0, sizeof(TDragExtCommStruct), AnsiString(DRAG_EXT_MAPPING).c_str());
  7454. {
  7455. TMutexGuard Guard(FDDExtMutex, DRAGEXT_MUTEX_RELEASE_TIMEOUT);
  7456. TDragExtCommStruct* CommStruct;
  7457. CommStruct = static_cast<TDragExtCommStruct*>(MapViewOfFile(FDDExtMapFile,
  7458. FILE_MAP_ALL_ACCESS, 0, 0, 0));
  7459. DebugAssert(CommStruct != NULL);
  7460. CommStruct->Version = TDragExtCommStruct::CurrentVersion;
  7461. CommStruct->Dragging = true;
  7462. wcsncpy(CommStruct->DropDest, FDragFakeDirectory.c_str(),
  7463. LENOF(CommStruct->DropDest));
  7464. NULL_TERMINATE(CommStruct->DropDest);
  7465. UnmapViewOfFile(CommStruct);
  7466. }
  7467. }
  7468. //---------------------------------------------------------------------------
  7469. bool __fastcall TCustomScpExplorerForm::RemoteFileControlFileOperation(
  7470. TObject * Sender, TFileOperation Operation, bool NoConfirmation, void * Param)
  7471. {
  7472. bool Result;
  7473. DebugAssert(!IsLocalBrowserMode());
  7474. if (Sender == RemoteDirView)
  7475. {
  7476. Result = ExecuteFileOperation(Operation, osRemote, true, NoConfirmation, Param);
  7477. }
  7478. else
  7479. {
  7480. DebugAssert(Sender == RemoteDriveView);
  7481. TStrings * FileList = RemoteDriveView->DragFileList();
  7482. try
  7483. {
  7484. Result = ExecuteFileOperation(Operation, osRemote, FileList, NoConfirmation, Param);
  7485. }
  7486. __finally
  7487. {
  7488. delete FileList;
  7489. }
  7490. }
  7491. if (FDDTargetControl == RemoteDriveView)
  7492. {
  7493. RemoteDriveView->UpdateDropTarget();
  7494. }
  7495. // foRemoteMove happens when file/dir is dragged within the remote tree view
  7496. // or from tree view to dir view.
  7497. // foMove happens when file/dir is dragged from remote tree view outside of
  7498. // application via dragex
  7499. if (((Operation == foRemoteMove) || (Operation == foMove)) &&
  7500. (DropSourceControl == RemoteDriveView))
  7501. {
  7502. RemoteDriveView->UpdateDropSource();
  7503. }
  7504. return Result;
  7505. }
  7506. //---------------------------------------------------------------------------
  7507. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDEnd(TObject * Sender)
  7508. {
  7509. DebugAssert(!IsLocalBrowserMode());
  7510. // This also handles drops of remote files to queue.
  7511. // Drops of local files (uploads) are handled in QueueDDProcessDropped.
  7512. SAFE_DESTROY(FDDFileList);
  7513. if (!FFakeFileDropTarget.IsEmpty())
  7514. {
  7515. RemoveDir(ApiPath(FFakeFileDropTarget));
  7516. }
  7517. if ((FDDExtMapFile != NULL) || (FDDTargetControl == QueueView3))
  7518. {
  7519. try
  7520. {
  7521. TDragResult DDResult = (Sender == RemoteDirView) ?
  7522. RemoteDirView->LastDDResult : RemoteDriveView->LastDDResult;
  7523. // Focus is moved to the target application,
  7524. // but as we are going to present the UI, we need to steal the focus back.
  7525. // This most likely won't work though (windows does not allow application
  7526. // to steal focus most of the time)
  7527. Application->BringToFront();
  7528. // On older version of Windows we never got drMove here, see also comment below.
  7529. // On Windows 10, we get the "move".
  7530. if ((DDResult == drCopy) || (DDResult == drMove) || (DDResult == drInvalid))
  7531. {
  7532. UnicodeString TargetDirectory;
  7533. TFileOperation Operation;
  7534. // drInvalid may mean drMove, see comment below
  7535. switch (DDResult)
  7536. {
  7537. case drCopy:
  7538. Operation = foCopy;
  7539. break;
  7540. case drMove:
  7541. Operation = foMove;
  7542. break;
  7543. default:
  7544. DebugFail();
  7545. case drInvalid:
  7546. // prefer "copy" for safety
  7547. Operation = FLAGSET(FLastDropEffect, DROPEFFECT_MOVE) ? foMove : foCopy;
  7548. break;
  7549. }
  7550. TTransferOperationParam Param;
  7551. UnicodeString CounterName;
  7552. bool ForceQueue;
  7553. if (!DDGetTarget(Param.TargetDirectory, ForceQueue, CounterName))
  7554. {
  7555. // we get drInvalid both if d&d was intercepted by ddext,
  7556. // and when users drops on no-drop location.
  7557. // we tell the difference by existence of response from ddext,
  7558. // so we ignore absence of response in this case
  7559. if (DDResult != drInvalid)
  7560. {
  7561. // here we know that the extension is installed,
  7562. // as it is checked as soon as drag&drop starts from
  7563. // RemoteFileControlDDCreateDragFileList
  7564. Configuration->Usage->Inc(L"DownloadsDragDropExternalExtTargetUnknown");
  7565. if (!WinConfiguration->DDExtInstalled || WinConfiguration->IsDDExtBroken())
  7566. {
  7567. throw ExtException(NULL, LoadStr(DD_TARGET_UNKNOWN), HELP_DD_TARGET_UNKNOWN);
  7568. }
  7569. else
  7570. {
  7571. throw ExtException(NULL, LoadStr(DRAGEXT_TARGET_UNKNOWN2), HELP_DRAGEXT_TARGET_UNKNOWN);
  7572. }
  7573. }
  7574. }
  7575. else
  7576. {
  7577. // download using fake file
  7578. Param.Temp = false;
  7579. Param.DragDrop = true;
  7580. if (ForceQueue)
  7581. {
  7582. Param.Queue = asOn;
  7583. }
  7584. if (Sender == RemoteDirView)
  7585. {
  7586. Param.Options = FLAGMASK(SelectedAllFilesInDirView(RemoteDirView), coAllFiles);
  7587. }
  7588. if (RemoteFileControlFileOperation(Sender, Operation,
  7589. (WinConfiguration->DDTransferConfirmation == asOff), &Param))
  7590. {
  7591. Configuration->Usage->Inc(CounterName);
  7592. }
  7593. }
  7594. }
  7595. }
  7596. __finally
  7597. {
  7598. CloseHandle(FDDExtMapFile);
  7599. FDDExtMapFile = NULL;
  7600. delete FDragFakeMonitors;
  7601. FDragFakeMonitors = NULL;
  7602. RemoveDir(ApiPath(FDragFakeDirectory));
  7603. FDragFakeDirectory = L"";
  7604. FFakeFileDropTarget = UnicodeString();
  7605. }
  7606. }
  7607. if (FDragDropSshTerminate.get() != NULL)
  7608. {
  7609. FDragDropSshTerminate->Rethrow();
  7610. }
  7611. }
  7612. //---------------------------------------------------------------------------
  7613. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDGiveFeedback(
  7614. TObject * /*Sender*/, int dwEffect, HRESULT & /*Result*/)
  7615. {
  7616. // Remember drop effect so we know (when user drops files), if we copy or move
  7617. FLastDropEffect = dwEffect;
  7618. }
  7619. //---------------------------------------------------------------------------
  7620. bool __fastcall TCustomScpExplorerForm::DDGetTarget(
  7621. UnicodeString & Directory, bool & ForceQueue, UnicodeString & CounterName)
  7622. {
  7623. bool Result;
  7624. if (FDDTargetControl == QueueView3)
  7625. {
  7626. Directory = DefaultDownloadTargetDirectory();
  7627. Result = true;
  7628. CounterName = L"DownloadsDragDropQueue";
  7629. ForceQueue = true;
  7630. }
  7631. else if (!FFakeFileDropTarget.IsEmpty())
  7632. {
  7633. Directory = ExcludeTrailingBackslash(ExtractFilePath(FFakeFileDropTarget));
  7634. Result = true;
  7635. ForceQueue = false;
  7636. CounterName = L"DownloadsDragDropFakeFile";
  7637. }
  7638. else
  7639. {
  7640. ForceQueue = false;
  7641. Enabled = false;
  7642. try
  7643. {
  7644. int Timer = 0;
  7645. Result = false;
  7646. while (!Result && (Timer < WinConfiguration->DDExtTimeout))
  7647. {
  7648. {
  7649. TMutexGuard Guard(FDDExtMutex, DRAGEXT_MUTEX_RELEASE_TIMEOUT);
  7650. TDragExtCommStruct* CommStruct;
  7651. CommStruct = static_cast<TDragExtCommStruct*>(MapViewOfFile(FDDExtMapFile,
  7652. FILE_MAP_ALL_ACCESS, 0, 0, 0));
  7653. DebugAssert(CommStruct != NULL);
  7654. Result = !CommStruct->Dragging;
  7655. if (Result)
  7656. {
  7657. Directory = ExtractFilePath(CommStruct->DropDest);
  7658. CounterName = L"DownloadsDragDropExternalExt";
  7659. }
  7660. UnmapViewOfFile(CommStruct);
  7661. }
  7662. if (!Result)
  7663. {
  7664. Sleep(50);
  7665. Timer += 50;
  7666. Application->ProcessMessages();
  7667. }
  7668. }
  7669. }
  7670. __finally
  7671. {
  7672. Enabled = true;
  7673. }
  7674. }
  7675. return Result;
  7676. }
  7677. //---------------------------------------------------------------------------
  7678. void __fastcall TCustomScpExplorerForm::AddDelayedDirectoryDeletion(
  7679. const UnicodeString TempDir, int SecDelay)
  7680. {
  7681. TDateTime Alarm = IncSecond(Now(), SecDelay);
  7682. FDelayedDeletionList->AddObject(TempDir, reinterpret_cast<TObject*>(Alarm.FileDate()));
  7683. if (FDelayedDeletionTimer == NULL)
  7684. {
  7685. DebugAssert(HandleAllocated());
  7686. FDelayedDeletionTimer = new TTimer(this);
  7687. FDelayedDeletionTimer->Interval = 10000;
  7688. FDelayedDeletionTimer->OnTimer = DoDelayedDeletion;
  7689. }
  7690. else
  7691. {
  7692. FDelayedDeletionTimer->Enabled = true;
  7693. }
  7694. }
  7695. //---------------------------------------------------------------------------
  7696. void __fastcall TCustomScpExplorerForm::DoDelayedDeletion(TObject * Sender)
  7697. {
  7698. DebugAssert(FDelayedDeletionList != NULL);
  7699. TDateTime N = Now();
  7700. TDateTime Alert;
  7701. UnicodeString Directory;
  7702. for (int Index = FDelayedDeletionList->Count-1; Index >= 0; Index--)
  7703. {
  7704. Alert = FileDateToDateTime(reinterpret_cast<int>(FDelayedDeletionList->Objects[Index]));
  7705. if ((N >= Alert) || (Sender == NULL))
  7706. {
  7707. Directory = FDelayedDeletionList->Strings[Index];
  7708. if (RecursiveDeleteFile(ExcludeTrailingBackslash(Directory)))
  7709. {
  7710. FDelayedDeletionList->Delete(Index);
  7711. }
  7712. }
  7713. }
  7714. if (FDelayedDeletionList->Count == 0)
  7715. {
  7716. FDelayedDeletionTimer->Enabled = false;
  7717. }
  7718. }
  7719. //---------------------------------------------------------------------------
  7720. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDTargetDrop()
  7721. {
  7722. DebugAssert(!IsLocalBrowserMode());
  7723. if (IsFileControl(FDDTargetControl, osRemote) ||
  7724. (FDDTargetControl == SessionsPageControl))
  7725. {
  7726. TManagedTerminal * TargetTerminal = NULL;
  7727. TFileOperation Operation = foNone;
  7728. if (FDDTargetControl == SessionsPageControl)
  7729. {
  7730. TPoint Point = SessionsPageControl->ScreenToClient(Mouse->CursorPos);
  7731. int Index = SessionsPageControl->IndexOfTabAt(Point.X, Point.Y);
  7732. // do not allow dropping on local-local, disconnected and the "+" tab (see also SessionsDDProcessDropped)
  7733. TargetTerminal = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  7734. if (IsActiveTerminal(TargetTerminal) && DebugAlwaysTrue(!TargetTerminal->LocalBrowser))
  7735. {
  7736. if ((FLastDropEffect == DROPEFFECT_MOVE) &&
  7737. (TargetTerminal == TTerminalManager::Instance()->ActiveTerminal))
  7738. {
  7739. Operation = foRemoteMove;
  7740. }
  7741. else
  7742. {
  7743. Operation = foRemoteCopy;
  7744. }
  7745. }
  7746. }
  7747. else
  7748. {
  7749. if (FLastDropEffect == DROPEFFECT_MOVE)
  7750. {
  7751. Operation = foRemoteMove;
  7752. }
  7753. else if (FLastDropEffect == DROPEFFECT_COPY)
  7754. {
  7755. Operation = foRemoteCopy;
  7756. }
  7757. }
  7758. if (Operation != foNone)
  7759. {
  7760. RemoteFileControlFileOperation(DropSourceControl,
  7761. Operation, (WinConfiguration->DDTransferConfirmation == asOff), TargetTerminal);
  7762. }
  7763. // abort drag&drop
  7764. Abort();
  7765. }
  7766. else if ((FDDExtMapFile == NULL) && (FLastDropEffect != DROPEFFECT_NONE) &&
  7767. // Drops of remote files to queue are handled in RemoteFileControlDDEnd
  7768. (FDDTargetControl != QueueView3))
  7769. {
  7770. DebugAssert(!FDragTempDir.IsEmpty());
  7771. TTransferType Type;
  7772. UnicodeString TempDir = FDragTempDir;
  7773. // We clear FDragTempDir before calling
  7774. // just in case it fail (raises exception)
  7775. FDragTempDir = L"";
  7776. Type = (FLastDropEffect & DROPEFFECT_MOVE ? ttMove : Type = ttCopy);
  7777. TGUICopyParamType CopyParams = GUIConfiguration->CurrentCopyParam;
  7778. // empty directory parameter means temp directory -> don't display it!
  7779. UnicodeString TargetDir = L"";
  7780. int Options = 0;
  7781. if (!CopyParamDialog(tdToLocal, Type, true, FDDFileList,
  7782. TargetDir, CopyParams, (WinConfiguration->DDTransferConfirmation != asOff), true, Options))
  7783. {
  7784. Abort();
  7785. }
  7786. // TargetDir is set when dropped on local file control
  7787. // (this was workaround for legacy dirview event handling, now it should be
  7788. // made prettier)
  7789. if (TargetDir.IsEmpty())
  7790. {
  7791. TargetDir = TempDir;
  7792. if (ForceDirectories(ApiPath(TargetDir)))
  7793. {
  7794. DebugAssert(Terminal && !TargetDir.IsEmpty());
  7795. FPendingTempSpaceWarn = true;
  7796. try
  7797. {
  7798. FDragDropOperation = true;
  7799. int Params = cpTemporary |
  7800. (Type == ttMove ? cpDelete : 0);
  7801. DDDownload(FDDFileList, TargetDir, &CopyParams, Params);
  7802. Configuration->Usage->Inc(L"DownloadsDragDropExternalTemp");
  7803. }
  7804. __finally
  7805. {
  7806. FDragDropOperation = false;
  7807. FPendingTempSpaceWarn = false;
  7808. AddDelayedDirectoryDeletion(TargetDir, WinConfiguration->DDDeleteDelay);
  7809. }
  7810. }
  7811. else
  7812. {
  7813. throw Exception(FMTLOAD(CREATE_TEMP_DIR_ERROR, (TargetDir)));
  7814. }
  7815. }
  7816. }
  7817. }
  7818. //---------------------------------------------------------------------------
  7819. void __fastcall TCustomScpExplorerForm::DDDownload(
  7820. TStrings * FilesToCopy, const UnicodeString & TargetDir, TCopyParamType * CopyParam, int Params)
  7821. {
  7822. DebugAssert(!IsLocalBrowserMode());
  7823. TAutoBatch AutoBatch(this);
  7824. CopyParam->IncludeFileMask.SetRoots(TargetDir, FilesToCopy);
  7825. UpdateCopyParamCounters(*CopyParam);
  7826. Terminal->CopyToLocal(FilesToCopy, TargetDir, CopyParam, Params, NULL);
  7827. if (FLAGSET(Params, cpDelete) && (DropSourceControl == RemoteDriveView))
  7828. {
  7829. RemoteDriveView->UpdateDropSource();
  7830. }
  7831. }
  7832. //---------------------------------------------------------------------------
  7833. class TFakeDataObjectFilesEx : public TDataObjectFilesEx
  7834. {
  7835. public:
  7836. __fastcall TFakeDataObjectFilesEx(TFileList * AFileList, bool RenderPIDL,
  7837. bool RenderFilename) : TDataObjectFilesEx(AFileList, RenderPIDL, RenderFilename, true)
  7838. {
  7839. }
  7840. virtual bool __fastcall AllowData(const tagFORMATETC & FormatEtc)
  7841. {
  7842. return (FormatEtc.cfFormat == CF_HDROP) ? false :
  7843. TDataObjectFilesEx::AllowData(FormatEtc);
  7844. }
  7845. };
  7846. //---------------------------------------------------------------------------
  7847. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDCreateDataObject(
  7848. TObject * Sender, TDataObject *& DataObject)
  7849. {
  7850. if (FDDExtMapFile != NULL)
  7851. {
  7852. TFileList * FileList = DragDropFiles(Sender)->FileList;
  7853. if (!FileList->RenderPIDLs() || !FileList->RenderNames())
  7854. {
  7855. Abort();
  7856. }
  7857. if (FileList->Count > 0)
  7858. {
  7859. TDataObjectFilesEx * FilesObject = new TFakeDataObjectFilesEx(FileList, true, true);
  7860. if (!FilesObject->IsValid(true, true))
  7861. {
  7862. FilesObject->_Release();
  7863. }
  7864. else
  7865. {
  7866. DataObject = FilesObject;
  7867. }
  7868. }
  7869. }
  7870. }
  7871. //---------------------------------------------------------------------------
  7872. void __fastcall TCustomScpExplorerForm::GoToCommandLine()
  7873. {
  7874. DebugFail();
  7875. }
  7876. //---------------------------------------------------------------------------
  7877. void __fastcall TCustomScpExplorerForm::GoToTree()
  7878. {
  7879. ComponentVisible[fcRemoteTree] = true;
  7880. DriveView(osRemote)->SetFocus();
  7881. }
  7882. //---------------------------------------------------------------------------
  7883. TStrings * __fastcall TCustomScpExplorerForm::PanelExport(TOperationSide Side,
  7884. TPanelExport Export)
  7885. {
  7886. TCustomDirView * DirView = this->DirView(Side);
  7887. std::unique_ptr<TStrings> ExportData(new TStringList());
  7888. switch (Export)
  7889. {
  7890. case pePath:
  7891. ExportData->Add(DirView->PathName);
  7892. break;
  7893. case peFileList:
  7894. case peFullFileList:
  7895. {
  7896. bool FullPath = (Export == peFullFileList);
  7897. DirView->CreateFileList(false, FullPath, ExportData.get());
  7898. for (int Index = 0; Index < ExportData->Count; Index++)
  7899. {
  7900. if (ExportData->Strings[Index].Pos(L" ") > 0)
  7901. {
  7902. ExportData->Strings[Index] = FORMAT(L"\"%s\"", (ExportData->Strings[Index]));
  7903. }
  7904. }
  7905. }
  7906. break;
  7907. default:
  7908. DebugFail();
  7909. }
  7910. return ExportData.release();
  7911. }
  7912. //---------------------------------------------------------------------------
  7913. void __fastcall TCustomScpExplorerForm::PanelExport(TOperationSide Side,
  7914. TPanelExport Export, TPanelExportDestination Destination)
  7915. {
  7916. std::unique_ptr<TStrings> ExportData(PanelExport(Side, Export));
  7917. PanelExportStore(Side, Export, Destination, ExportData.get());
  7918. }
  7919. //---------------------------------------------------------------------------
  7920. void __fastcall TCustomScpExplorerForm::PanelExportStore(TOperationSide /*Side*/,
  7921. TPanelExport /*Export*/, TPanelExportDestination Destination,
  7922. TStrings * ExportData)
  7923. {
  7924. if (Destination == pedClipboard)
  7925. {
  7926. TInstantOperationVisualizer Visualizer;
  7927. CopyToClipboard(ExportData);
  7928. }
  7929. else
  7930. {
  7931. DebugFail();
  7932. }
  7933. }
  7934. //---------------------------------------------------------------------------
  7935. void __fastcall TCustomScpExplorerForm::Filter(TOperationSide Side)
  7936. {
  7937. TCustomDirView * DirView = this->DirView(Side);
  7938. UnicodeString Mask = DirView->Mask;
  7939. if (DoFilterMaskDialog(DirView, Mask))
  7940. {
  7941. DirView->Mask = TFileMasks::NormalizeMask(Mask);
  7942. Configuration->Usage->Inc(L"Filters");
  7943. }
  7944. }
  7945. //---------------------------------------------------------------------------
  7946. TQueueOperation __fastcall TCustomScpExplorerForm::DefaultQueueOperation()
  7947. {
  7948. return FQueueController->DefaultOperation();
  7949. }
  7950. //---------------------------------------------------------------------------
  7951. bool __fastcall TCustomScpExplorerForm::AllowQueueOperation(
  7952. TQueueOperation Operation, void ** Param)
  7953. {
  7954. switch (Operation)
  7955. {
  7956. case qoPreferences:
  7957. return true;
  7958. case qoGoTo:
  7959. return ComponentVisible[fcQueueView] && QueueView3->Enabled;
  7960. case qoOnceEmpty:
  7961. return IsAnythingQueued();
  7962. default:
  7963. return FQueueController->AllowOperation(Operation, Param);
  7964. }
  7965. }
  7966. //---------------------------------------------------------------------------
  7967. void __fastcall TCustomScpExplorerForm::GoToQueue()
  7968. {
  7969. if (DebugAlwaysTrue(QueueView3->Visible))
  7970. {
  7971. QueueView3->SetFocus();
  7972. }
  7973. }
  7974. //---------------------------------------------------------------------------
  7975. void __fastcall TCustomScpExplorerForm::ExecuteQueueOperation(
  7976. TQueueOperation Operation, void * Param)
  7977. {
  7978. if (Operation == qoGoTo)
  7979. {
  7980. GoToQueue();
  7981. }
  7982. else if (Operation == qoPreferences)
  7983. {
  7984. PreferencesDialog(pmQueue);
  7985. }
  7986. else
  7987. {
  7988. FQueueController->ExecuteOperation(Operation, Param);
  7989. }
  7990. }
  7991. //---------------------------------------------------------------------------
  7992. bool __fastcall TCustomScpExplorerForm::GetQueueEnabled()
  7993. {
  7994. return (Queue != NULL) && Queue->Enabled;
  7995. }
  7996. //---------------------------------------------------------------------------
  7997. void __fastcall TCustomScpExplorerForm::ToggleQueueEnabled()
  7998. {
  7999. DebugAssert(Queue != NULL);
  8000. if (Queue != NULL)
  8001. {
  8002. Queue->Enabled = !Queue->Enabled;
  8003. }
  8004. }
  8005. //---------------------------------------------------------------------------
  8006. void __fastcall TCustomScpExplorerForm::QueueView3ContextPopup(
  8007. TObject * /*Sender*/, TPoint & /*MousePos*/, bool & /*Handled*/)
  8008. {
  8009. FQueueActedItem = QueueView3->ItemFocused;
  8010. }
  8011. //---------------------------------------------------------------------------
  8012. /*virtual*/ int __fastcall TCustomScpExplorerForm::GetStaticComponentsHeight()
  8013. {
  8014. return TopDock->Height + (MessageDock->Visible ? MessageDock->Height : 0) + QueueSplitter->Height;
  8015. }
  8016. //---------------------------------------------------------------------------
  8017. int __fastcall TCustomScpExplorerForm::GetStaticQueuePanelComponentsHeight()
  8018. {
  8019. return
  8020. (QueueFileListSplitter->Visible ? QueueFileListSplitter->Height : 0) +
  8021. (QueueDock->Visible ? QueueDock->Height : 0) +
  8022. (QueueLabel->Visible ? QueueLabel->Height : 0);
  8023. }
  8024. //---------------------------------------------------------------------------
  8025. int __fastcall TCustomScpExplorerForm::GetMinQueueViewHeight()
  8026. {
  8027. return ScaleByTextHeight(this, 48);
  8028. }
  8029. //---------------------------------------------------------------------------
  8030. void __fastcall TCustomScpExplorerForm::QueueSplitterCanResize(
  8031. TObject * /*Sender*/, int & NewSize, bool & Accept)
  8032. {
  8033. // when queue is hidden by double-clicking splitter, stray attempt to
  8034. // resize the panel with strange value arrives, make sure it is ignored
  8035. if (ComponentVisible[fcQueueView])
  8036. {
  8037. int MaxHeightLimit = ClientHeight - GetStaticComponentsHeight() - RemotePanel->Constraints->MinHeight;
  8038. if (NewSize > MaxHeightLimit)
  8039. {
  8040. NewSize = MaxHeightLimit;
  8041. }
  8042. int MinHeightLimit =
  8043. GetStaticQueuePanelComponentsHeight() +
  8044. (QueueFileList->Visible ? QueueFileList->Height : 0) +
  8045. GetMinQueueViewHeight();
  8046. if (NewSize < MinHeightLimit)
  8047. {
  8048. NewSize = MinHeightLimit;
  8049. }
  8050. }
  8051. else
  8052. {
  8053. Accept = false;
  8054. }
  8055. }
  8056. //---------------------------------------------------------------------------
  8057. void __fastcall TCustomScpExplorerForm::QueueFileListSplitterCanResize(TObject *, int & NewSize, bool &)
  8058. {
  8059. int TotalHeight = GetStaticQueuePanelComponentsHeight() + NewSize;
  8060. int QueueViewHeight = QueuePanel->ClientHeight - TotalHeight;
  8061. if (QueueViewHeight < GetMinQueueViewHeight())
  8062. {
  8063. NewSize -= (GetMinQueueViewHeight() - QueueViewHeight);
  8064. }
  8065. }
  8066. //---------------------------------------------------------------------------
  8067. void __fastcall TCustomScpExplorerForm::QueueView3StartDrag(TObject * /*Sender*/,
  8068. TDragObject *& /*DragObject*/)
  8069. {
  8070. FQueueActedItem = QueueView3->ItemFocused;
  8071. FQueueScrollOnDragOver->StartDrag();
  8072. }
  8073. //---------------------------------------------------------------------------
  8074. void __fastcall TCustomScpExplorerForm::QueueView3EndDrag(
  8075. TObject *, TObject * DebugUsedArg(Target), int DebugUsedArg(X), int DebugUsedArg(Y))
  8076. {
  8077. FQueueScrollOnDragOver->EndDrag();
  8078. }
  8079. //---------------------------------------------------------------------------
  8080. void __fastcall TCustomScpExplorerForm::QueueView3DragOver(TObject * /*Sender*/,
  8081. TObject * Source, int X, int Y, TDragState /*State*/, bool & Accept)
  8082. {
  8083. Accept = true;
  8084. if (Source == QueueView3)
  8085. {
  8086. TListItem * DropTarget = QueueView3->GetItemAt(X, Y);
  8087. Accept = (DropTarget != NULL) && (FQueueActedItem != NULL);
  8088. if (Accept)
  8089. {
  8090. TQueueItemProxy * QueueItem;
  8091. TQueueItemProxy * DestQueueItem;
  8092. QueueItem = static_cast<TQueueItemProxy *>(FQueueActedItem->Data);
  8093. DestQueueItem = static_cast<TQueueItemProxy *>(DropTarget->Data);
  8094. Accept = (QueueItem != DestQueueItem) &&
  8095. (QueueItem->Status == TQueueItem::qsPending) &&
  8096. (DestQueueItem->Status == TQueueItem::qsPending);
  8097. }
  8098. FQueueScrollOnDragOver->DragOver(TPoint(X, Y));
  8099. }
  8100. }
  8101. //---------------------------------------------------------------------------
  8102. void __fastcall TCustomScpExplorerForm::QueueView3DragDrop(TObject * /*Sender*/,
  8103. TObject * /*Source*/, int /*X*/, int /*Y*/)
  8104. {
  8105. if ((FQueueActedItem != NULL) && (QueueView3->DropTarget != NULL))
  8106. {
  8107. TQueueItemProxy * QueueItem;
  8108. TQueueItemProxy * DestQueueItem;
  8109. QueueItem = static_cast<TQueueItemProxy *>(FQueueActedItem->Data);
  8110. DestQueueItem = static_cast<TQueueItemProxy *>(QueueView3->DropTarget->Data);
  8111. QueueItem->Move(DestQueueItem);
  8112. }
  8113. }
  8114. //---------------------------------------------------------------------------
  8115. void __fastcall TCustomScpExplorerForm::QueueView3Enter(TObject * /*Sender*/)
  8116. {
  8117. if ((QueueView3->ItemFocused == NULL) &&
  8118. (QueueView3->Items->Count > 0))
  8119. {
  8120. QueueView3->ItemFocused = QueueView3->Items->Item[0];
  8121. }
  8122. QueueLabelUpdateStatus();
  8123. }
  8124. //---------------------------------------------------------------------------
  8125. void __fastcall TCustomScpExplorerForm::QueueView3Exit(TObject * /*Sender*/)
  8126. {
  8127. QueueLabelUpdateStatus();
  8128. }
  8129. //---------------------------------------------------------------------------
  8130. void __fastcall TCustomScpExplorerForm::QueueLabelUpdateStatus()
  8131. {
  8132. QueueLabel->UpdateStatus();
  8133. }
  8134. //---------------------------------------------------------------------------
  8135. void __fastcall TCustomScpExplorerForm::QueueView3SelectItem(
  8136. TObject * /*Sender*/, TListItem * /*Item*/, bool Selected)
  8137. {
  8138. if (Selected)
  8139. {
  8140. NonVisualDataModule->UpdateNonVisibleActions();
  8141. }
  8142. }
  8143. //---------------------------------------------------------------------------
  8144. TDragDropFilesEx * __fastcall TCustomScpExplorerForm::DragDropFiles(TObject * Sender)
  8145. {
  8146. TDragDropFilesEx * Result = NULL;
  8147. if (Sender == SessionsPageControl)
  8148. {
  8149. Result = FSessionsDragDropFilesEx;
  8150. }
  8151. else if (Sender == QueueView3)
  8152. {
  8153. Result = FQueueDragDropFilesEx;
  8154. }
  8155. else
  8156. {
  8157. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  8158. if (DirView != NULL)
  8159. {
  8160. Result = DirView->DragDropFilesEx;
  8161. }
  8162. else
  8163. {
  8164. TCustomDriveView * DriveView = dynamic_cast<TCustomDriveView *>(Sender);
  8165. if (DriveView != NULL)
  8166. {
  8167. Result = DriveView->DragDropFilesEx;
  8168. }
  8169. }
  8170. }
  8171. DebugAssert(Result != NULL);
  8172. return Result;
  8173. }
  8174. //---------------------------------------------------------------------------
  8175. bool __fastcall TCustomScpExplorerForm::SelectedAllFilesInDirView(TCustomDirView * DView)
  8176. {
  8177. return (DView->SelCount == DView->FilesCount);
  8178. }
  8179. //---------------------------------------------------------------------------
  8180. bool __fastcall TCustomScpExplorerForm::DraggingAllFilesFromDirView(TOperationSide Side, TStrings * FileList)
  8181. {
  8182. return HasDirView[Side] && (DropSourceControl == DirView(Side)) && (FileList->Count == DirView(Side)->FilesCount);
  8183. }
  8184. //---------------------------------------------------------------------------
  8185. void __fastcall TCustomScpExplorerForm::RemoteFileControlDragDropFileOperation(
  8186. TObject * Sender, int Effect, UnicodeString TargetPath, bool ForceQueue, bool DragDrop)
  8187. {
  8188. DebugAssert(!IsLocalBrowserMode());
  8189. TFileOperation Operation;
  8190. switch (Effect)
  8191. {
  8192. case DROPEFFECT_MOVE:
  8193. Operation = foMove;
  8194. break;
  8195. case DROPEFFECT_COPY:
  8196. // occures on WinXP (reported by user)
  8197. default:
  8198. Operation = foCopy;
  8199. break;
  8200. };
  8201. TDragDropFilesEx * DragDropFilesEx = DragDropFiles(Sender);
  8202. // see a comment in TUnixDirView::PerformItemDragDropOperation
  8203. if (DragDropFilesEx->FileList->Count > 0)
  8204. {
  8205. TStrings * FileList = new TStringList();
  8206. try
  8207. {
  8208. for (int Index = 0; Index < DragDropFilesEx->FileList->Count; Index++)
  8209. {
  8210. FileList->Add(DragDropFilesEx->FileList->Items[Index]->Name);
  8211. }
  8212. FDragDropOperation = true;
  8213. TTransferOperationParam Param;
  8214. Param.TargetDirectory = TargetPath;
  8215. // upload, no temp dirs
  8216. Param.Temp = false;
  8217. Param.DragDrop = DragDrop;
  8218. Param.Options =
  8219. FLAGMASK(DraggingAllFilesFromDirView(osLocal, FileList), coAllFiles);
  8220. if (ForceQueue)
  8221. {
  8222. Param.Queue = asOn;
  8223. }
  8224. bool NoConfirmation = DragDrop ? (WinConfiguration->DDTransferConfirmation == asOff) : false;
  8225. if (ExecuteFileOperation(Operation, osLocal, FileList, NoConfirmation, &Param))
  8226. {
  8227. if (IsFileControl(DropSourceControl, osLocal))
  8228. {
  8229. Configuration->Usage->Inc(L"UploadsDragDropInternal");
  8230. }
  8231. else
  8232. {
  8233. Configuration->Usage->Inc(L"UploadsDragDropExternal");
  8234. }
  8235. }
  8236. }
  8237. __finally
  8238. {
  8239. FDragDropOperation = false;
  8240. delete FileList;
  8241. }
  8242. }
  8243. }
  8244. //---------------------------------------------------------------------------
  8245. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDFileOperation(
  8246. TObject * Sender, int Effect, UnicodeString /*SourcePath*/,
  8247. UnicodeString TargetPath, bool Paste, bool & /*DoOperation*/)
  8248. {
  8249. RemoteFileControlDragDropFileOperation(Sender, Effect, TargetPath, false, !Paste);
  8250. }
  8251. //---------------------------------------------------------------------------
  8252. void __fastcall TCustomScpExplorerForm::RemoteFileContolDDChooseEffect(
  8253. TObject * Sender, int grfKeyState, int & dwEffect)
  8254. {
  8255. DebugAssert(!IsLocalBrowserMode());
  8256. // if any drop effect is allowed at all (e.g. no drop to self and drop to parent)
  8257. if ((dwEffect != DROPEFFECT_NONE) &&
  8258. IsFileControl(DropSourceControl, osRemote))
  8259. {
  8260. // do not allow drop on remote panel (on free space, still allow drop on directories)
  8261. if ((Sender == RemoteDirView) && (DropSourceControl == RemoteDirView) &&
  8262. (RemoteDirView->DropTarget == NULL))
  8263. {
  8264. dwEffect = DROPEFFECT_NONE;
  8265. }
  8266. else
  8267. {
  8268. if (dwEffect == DROPEFFECT_COPY)
  8269. {
  8270. bool MoveCapable = Terminal->IsCapable[fcRemoteMove];
  8271. // currently we support copying always (at least via temporary directory);
  8272. // remove associated checks once this all proves stable and working
  8273. bool CopyCapable = true;
  8274. // if we do not support neither of operations, there's no discussion
  8275. if (!MoveCapable && !CopyCapable)
  8276. {
  8277. dwEffect = DROPEFFECT_NONE;
  8278. }
  8279. else
  8280. {
  8281. // The default effect inside remote panel is move,
  8282. // unless we do not support it, but support copy
  8283. if (FLAGCLEAR(grfKeyState, MK_CONTROL))
  8284. {
  8285. dwEffect = MoveCapable ? DROPEFFECT_MOVE : DROPEFFECT_COPY;
  8286. }
  8287. else
  8288. {
  8289. // with ctrl-down, we want copy unless it is not supported
  8290. dwEffect = CopyCapable ? DROPEFFECT_COPY : DROPEFFECT_NONE;
  8291. }
  8292. }
  8293. }
  8294. }
  8295. }
  8296. }
  8297. //---------------------------------------------------------------------------
  8298. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDDragFileName(
  8299. TObject * Sender, TRemoteFile * File, UnicodeString & FileName)
  8300. {
  8301. DebugAssert(!IsLocalBrowserMode());
  8302. if (FDDTotalSize >= 0)
  8303. {
  8304. if (File->IsDirectory)
  8305. {
  8306. FDDTotalSize = -1;
  8307. }
  8308. else
  8309. {
  8310. FDDTotalSize += File->Size;
  8311. }
  8312. }
  8313. DebugAssert(!FDragTempDir.IsEmpty());
  8314. // TODO: this is quite ineffective
  8315. // TODO: what if invalid character replacement is disabled?
  8316. FileName = FDragTempDir + GUIConfiguration->CurrentCopyParam.ValidLocalFileName(File->FileName);
  8317. UnicodeString TransferFileName;
  8318. if (Sender == RemoteDriveView)
  8319. {
  8320. TransferFileName = UnixExcludeTrailingBackslash(File->FullFileName);
  8321. }
  8322. else
  8323. {
  8324. TransferFileName = File->FileName;
  8325. }
  8326. FDDFileList->AddObject(TransferFileName, File);
  8327. }
  8328. //---------------------------------------------------------------------------
  8329. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDDragDetect(
  8330. TObject * /*Sender*/, int /*grfKeyState*/, const TPoint & /*DetectStart*/,
  8331. const TPoint & /*Point*/, TDragDetectStatus /*DragStatus*/)
  8332. {
  8333. // sometimes we do not get DDEnd so the list is not released
  8334. SAFE_DESTROY(FDDFileList);
  8335. FDDFileList = new TStringList();
  8336. FDragTempDir = WinConfiguration->TemporaryDir();
  8337. FDDTotalSize = 0;
  8338. FDragDropSshTerminate.reset(NULL);
  8339. }
  8340. //---------------------------------------------------------------------------
  8341. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDQueryContinueDrag(
  8342. TObject * /*Sender*/, BOOL /*FEscapePressed*/, int /*grfKeyState*/,
  8343. HRESULT & Result)
  8344. {
  8345. if (Result == DRAGDROP_S_DROP)
  8346. {
  8347. try
  8348. {
  8349. GlobalDragImageList->HideDragImage();
  8350. try
  8351. {
  8352. RemoteFileControlDDTargetDrop();
  8353. }
  8354. catch(ESshTerminate & E)
  8355. {
  8356. FDragDropSshTerminate.reset(E.Clone());
  8357. }
  8358. }
  8359. catch (Exception &E)
  8360. {
  8361. // If downloading fails we need to cancel drag&drop, otherwise
  8362. // Explorer shows error
  8363. // But by the way exception probably never reach this point as
  8364. // it's catched on way.
  8365. // Fatal exceptions get here (like when opening a secondary shell extension for file duplication fails).
  8366. Result = DRAGDROP_S_CANCEL;
  8367. ShowExtendedException(Terminal, &E);
  8368. }
  8369. }
  8370. }
  8371. //---------------------------------------------------------------------------
  8372. void __fastcall TCustomScpExplorerForm::DirViewMatchMask(
  8373. TObject * /*Sender*/, UnicodeString FileName, bool Directory, __int64 Size,
  8374. TDateTime Modification, UnicodeString Masks, bool & Matches, bool AllowImplicitMatches)
  8375. {
  8376. TFileMasks::TParams MaskParams;
  8377. MaskParams.Size = Size;
  8378. MaskParams.Modification = Modification;
  8379. // this does not re-parse the mask if it is the same as the last time
  8380. FDirViewMatchMask = Masks;
  8381. bool ImplicitMatch;
  8382. Matches =
  8383. // Local and RecurseInclude parameters have no effect as there's no path.
  8384. // This should use potential MatchesFileName overload with ImplicitMatch argument.
  8385. FDirViewMatchMask.Matches(FileName, false, Directory, &MaskParams, true, ImplicitMatch) &&
  8386. (AllowImplicitMatches || !ImplicitMatch);
  8387. }
  8388. //---------------------------------------------------------------------------
  8389. void __fastcall TCustomScpExplorerForm::DirViewGetOverlay(
  8390. TObject * Sender, TListItem * Item, WORD & Indexes)
  8391. {
  8392. TCustomDirView * DirView = reinterpret_cast<TCustomDirView *>(Sender);
  8393. if (GetPartialFileExtLen(DirView->ItemFileName(Item)) > 0)
  8394. {
  8395. Indexes |= oiPartial;
  8396. }
  8397. }
  8398. //---------------------------------------------------------------------------
  8399. bool __fastcall TCustomScpExplorerForm::CanPasteToDirViewFromClipBoard()
  8400. {
  8401. return
  8402. DirViewEnabled(osCurrent) &&
  8403. DirView(osCurrent)->CanPasteFromClipBoard();
  8404. }
  8405. //---------------------------------------------------------------------------
  8406. bool __fastcall TCustomScpExplorerForm::CanPasteFromClipBoard()
  8407. {
  8408. bool Result = false;
  8409. if (CanPasteToDirViewFromClipBoard())
  8410. {
  8411. Result = true;
  8412. }
  8413. else
  8414. {
  8415. UnicodeString ClipboardText;
  8416. if (NonEmptyTextFromClipboard(ClipboardText) &&
  8417. (ClipboardText.Pos(L"\n") == 0)) // it's already trimmed
  8418. {
  8419. if (StoredSessions->IsUrl(ClipboardText))
  8420. {
  8421. Result = true;
  8422. }
  8423. else
  8424. {
  8425. Result = DirViewEnabled(osCurrent);
  8426. }
  8427. }
  8428. }
  8429. return Result;
  8430. }
  8431. //---------------------------------------------------------------------------
  8432. void __fastcall TCustomScpExplorerForm::PasteFromClipBoard()
  8433. {
  8434. // Pasting to any local panel is handled in TScpCommanderForm::PasteFromClipBoard()
  8435. if (DoesClipboardContainOurFiles())
  8436. {
  8437. if (DebugAlwaysTrue(CanPasteToDirViewFromClipBoard()))
  8438. {
  8439. DebugAssert(!IsLocalBrowserMode());
  8440. TTerminalManager * Manager = TTerminalManager::Instance();
  8441. TTerminal * TargetTerminal = Manager->ActiveTerminal;
  8442. Manager->ActiveSession = FClipboardTerminal;
  8443. ExecuteFileOperation(foRemoteCopy, osRemote, FClipboardFileList.get(), false, TargetTerminal);
  8444. }
  8445. }
  8446. else if (CanPasteToDirViewFromClipBoard())
  8447. {
  8448. DirView(osCurrent)->PasteFromClipBoard();
  8449. }
  8450. else
  8451. {
  8452. UnicodeString ClipboardText;
  8453. if (NonEmptyTextFromClipboard(ClipboardText))
  8454. {
  8455. if (StoredSessions->IsUrl(ClipboardText))
  8456. {
  8457. NewSession(ClipboardText);
  8458. }
  8459. else
  8460. {
  8461. DirView(osCurrent)->Path = ClipboardText;
  8462. }
  8463. }
  8464. }
  8465. }
  8466. //---------------------------------------------------------------------------
  8467. void __fastcall TCustomScpExplorerForm::FileListFromClipboard()
  8468. {
  8469. // TBD
  8470. }
  8471. //---------------------------------------------------------------------------
  8472. void __fastcall TCustomScpExplorerForm::SelectAll(TOperationSide Side, TSelectMode Mode)
  8473. {
  8474. TCustomDirView * ADirView = DirView(Side);
  8475. ADirView->SelectAll(Mode);
  8476. ADirView->SetFocus();
  8477. }
  8478. //---------------------------------------------------------------------------
  8479. void __fastcall TCustomScpExplorerForm::SelectByMask(TOperationSide Side, bool Select)
  8480. {
  8481. TCustomDirView * ADirView = DirView(Side);
  8482. TFileFilter Filter;
  8483. DefaultFileFilter(Filter);
  8484. if (DoSelectMaskDialog(ADirView, Select, Filter))
  8485. {
  8486. Configuration->Usage->Inc(L"MaskSelections");
  8487. ADirView->SelectFiles(Filter, Select);
  8488. ADirView->SetFocus();
  8489. }
  8490. }
  8491. //---------------------------------------------------------------------------
  8492. void __fastcall TCustomScpExplorerForm::RestoreSelectedNames(TOperationSide Side)
  8493. {
  8494. TCustomDirView * ADirView = DirView(Side);
  8495. ADirView->RestoreSelectedNames();
  8496. ADirView->SetFocus();
  8497. }
  8498. //---------------------------------------------------------------------------
  8499. void __fastcall TCustomScpExplorerForm::SelectSameExt(bool Select)
  8500. {
  8501. TCustomDirView * CurrentDirView = DirView(osCurrent);
  8502. if (DebugAlwaysTrue(CurrentDirView->ItemFocused != NULL))
  8503. {
  8504. UnicodeString FileName = CurrentDirView->ItemFileName(CurrentDirView->ItemFocused);
  8505. UnicodeString Ext;
  8506. if (!IsSideLocalBrowser(osCurrent))
  8507. {
  8508. Ext = UnixExtractFileExt(FileName);
  8509. }
  8510. else
  8511. {
  8512. Ext = ExtractFileExt(FileName);
  8513. }
  8514. if (Ext.IsEmpty())
  8515. {
  8516. Ext = L".";
  8517. }
  8518. TFileFilter Filter;
  8519. Filter.Masks = FORMAT(L"*%s", (Ext));
  8520. Filter.Directories = false;
  8521. CurrentDirView->SelectFiles(Filter, Select);
  8522. }
  8523. }
  8524. //---------------------------------------------------------------------------
  8525. UnicodeString __fastcall TCustomScpExplorerForm::FileStatusBarText(
  8526. const TStatusFileInfo & FileInfo, TOperationSide Side)
  8527. {
  8528. UnicodeString Result;
  8529. if (!FIncrementalSearch.IsEmpty() && (Side == GetSide(osCurrent)))
  8530. {
  8531. Result = FormatIncrementalSearchStatus(FIncrementalSearch, FIncrementalSearchHaveNext);
  8532. }
  8533. else if (!IsSideLocalBrowser(Side) && ((Terminal == NULL) || Terminal->Disconnected))
  8534. {
  8535. // noop
  8536. }
  8537. else
  8538. {
  8539. Result =
  8540. FMTLOAD(FILE_INFO_FORMAT,
  8541. (FormatBytes(FileInfo.SelectedSize),
  8542. FormatBytes(FileInfo.FilesSize),
  8543. FormatNumber(FileInfo.SelectedCount),
  8544. FormatNumber(FileInfo.FilesCount)));
  8545. }
  8546. return Result;
  8547. }
  8548. //---------------------------------------------------------------------------
  8549. void __fastcall TCustomScpExplorerForm::FileStatusBarPanelClick(
  8550. TTBXStatusPanel * Panel, TOperationSide Side)
  8551. {
  8552. if (Panel->Index == 1)
  8553. {
  8554. ToggleShowHiddenFiles();
  8555. }
  8556. else if (Panel->Index == 2)
  8557. {
  8558. Filter(Side);
  8559. }
  8560. }
  8561. //---------------------------------------------------------------------------
  8562. void __fastcall TCustomScpExplorerForm::UpdateFileStatusBar(
  8563. TTBXStatusBar * StatusBar, const TStatusFileInfo & FileInfo, TOperationSide Side)
  8564. {
  8565. DebugAssert(!StatusBar->SimplePanel);
  8566. StatusBar->Panels->Items[0]->Caption = FileStatusBarText(FileInfo, Side);
  8567. UpdateFileStatusExtendedPanels(StatusBar, FileInfo);
  8568. }
  8569. //---------------------------------------------------------------------------
  8570. void __fastcall TCustomScpExplorerForm::UpdateFileStatusExtendedPanels(
  8571. TTBXStatusBar * StatusBar, const TStatusFileInfo & FileInfo)
  8572. {
  8573. DebugAssert(StatusBar->Panels->Count >= 3);
  8574. TTBXStatusPanel * HiddenFilesPanel = StatusBar->Panels->Items[1];
  8575. if (FileInfo.HiddenCount > 0)
  8576. {
  8577. HiddenFilesPanel->Caption = FMTLOAD(FILE_INFO_HIDDEN2, (FormatNumber(FileInfo.HiddenCount)));
  8578. HiddenFilesPanel->ViewPriority = 90; // <100 allows hiding panel when it does not fit
  8579. }
  8580. else
  8581. {
  8582. HiddenFilesPanel->ViewPriority = 0;
  8583. // not really necessary, just to cleanup no-longer-valid data
  8584. HiddenFilesPanel->Caption = L"";
  8585. }
  8586. TTBXStatusPanel * FilteredFilesPanel = StatusBar->Panels->Items[2];
  8587. if (FileInfo.FilteredCount > 0)
  8588. {
  8589. FilteredFilesPanel->Caption = FMTLOAD(FILE_INFO_FILTERED2, (FormatNumber(FileInfo.FilteredCount)));
  8590. FilteredFilesPanel->ViewPriority = 90; // <100 allows hiding panel when it does not fit
  8591. }
  8592. else
  8593. {
  8594. FilteredFilesPanel->ViewPriority = 0;
  8595. // not really necessary, just to cleanup no-longer-valid data
  8596. FilteredFilesPanel->Caption = L"";
  8597. }
  8598. }
  8599. //---------------------------------------------------------------------------
  8600. void __fastcall TCustomScpExplorerForm::RemoteStatusBarClick(
  8601. TObject * /*Sender*/)
  8602. {
  8603. if (DirView(osOther)->Enabled)
  8604. {
  8605. DirView(osOther)->SetFocus();
  8606. }
  8607. }
  8608. //---------------------------------------------------------------------------
  8609. void __fastcall TCustomScpExplorerForm::ToggleQueueVisibility()
  8610. {
  8611. TQueueViewConfiguration Config = WinConfiguration->QueueView;
  8612. switch (Config.Show)
  8613. {
  8614. case qvShow:
  8615. if ((FQueueStatus != NULL) && (FQueueStatus->Count > 0))
  8616. {
  8617. Config.Show = qvHide;
  8618. }
  8619. else
  8620. {
  8621. Config.Show = Config.LastHideShow;
  8622. }
  8623. break;
  8624. case qvHideWhenEmpty:
  8625. if (ComponentVisible[fcQueueView])
  8626. {
  8627. Config.Show = qvHide;
  8628. }
  8629. else
  8630. {
  8631. Config.LastHideShow = Config.Show;
  8632. Config.Show = qvShow;
  8633. }
  8634. break;
  8635. case qvHide:
  8636. Config.LastHideShow = Config.Show;
  8637. Config.Show = qvShow;
  8638. break;
  8639. }
  8640. WinConfiguration->QueueView = Config;
  8641. }
  8642. //---------------------------------------------------------------------------
  8643. UnicodeString __fastcall TCustomScpExplorerForm::PathForCaption()
  8644. {
  8645. UnicodeString Result;
  8646. if (Terminal != NULL)
  8647. {
  8648. switch (WinConfiguration->PathInCaption)
  8649. {
  8650. case picShort:
  8651. Result = ExtractShortName(Terminal->CurrentDirectory, true);
  8652. break;
  8653. case picFull:
  8654. Result = Terminal->CurrentDirectory;
  8655. break;
  8656. }
  8657. }
  8658. return Result;
  8659. }
  8660. //---------------------------------------------------------------------------
  8661. TColor __fastcall TCustomScpExplorerForm::PanelColor()
  8662. {
  8663. TColor Result = GetWindowColor(FSessionColor);
  8664. return Result;
  8665. }
  8666. //---------------------------------------------------------------------------
  8667. TColor __fastcall TCustomScpExplorerForm::DisabledPanelColor()
  8668. {
  8669. TColor Result = (WinConfiguration->UseDarkTheme() ? static_cast<TColor>(RGB(0x40, 0x40, 0x40)) : clBtnFace);
  8670. return Result;
  8671. }
  8672. //---------------------------------------------------------------------------
  8673. void __fastcall TCustomScpExplorerForm::UpdateControls()
  8674. {
  8675. if (!FSessionChanging)
  8676. {
  8677. TTerminalManager::Instance()->UpdateAppTitle();
  8678. }
  8679. // WORKAROUND: Disabling list view when it is not showing yet does not set its
  8680. // background to gray on Windows 7 (works on Windows 10).
  8681. // See also EnableControl
  8682. if (Showing)
  8683. {
  8684. if (ManagedSession != NULL)
  8685. {
  8686. // Update path when it changes
  8687. if ((SessionsPageControl->ActivePage != NULL) &&
  8688. (GetSessionTabSession(SessionsPageControl->ActivePage) == ManagedSession))
  8689. {
  8690. UpdateSessionTab(SessionsPageControl->ActivePage);
  8691. }
  8692. }
  8693. NonVisualDataModule->ReconnectSessionAction->Update();
  8694. ReconnectToolbar->Visible = NonVisualDataModule->ReconnectSessionAction->Visible;
  8695. // ReconnectSessionAction is hidden when disabled, so enabling it actualy resizes the toolbar
  8696. CenterReconnectToolbar();
  8697. bool HasTerminal = HasActiveTerminal();
  8698. RemoteDirView->Enabled = HasTerminal;
  8699. RemoteDirView->Color = HasTerminal ? PanelColor() : DisabledPanelColor();
  8700. RemoteDirView->Font->Color = GetWindowTextColor(RemoteDirView->Color);
  8701. RemoteDriveView->Enabled = RemoteDirView->Enabled;
  8702. RemoteDriveView->Color = RemoteDirView->Color;
  8703. RemoteDriveView->Font->Color = RemoteDirView->Font->Color;
  8704. QueueView3->Enabled = HasTerminal && Terminal->IsCapable[fcBackgroundTransfers];
  8705. QueueView3->Color = QueueView3->Enabled ? GetWindowColor() : DisabledPanelColor();
  8706. QueueView3->Font->Color = GetWindowTextColor(QueueView3->Color);
  8707. QueueFileList->Enabled = QueueView3->Enabled;
  8708. QueueFileList->Color = QueueView3->Color;
  8709. QueueFileList->Font->Color = QueueView3->Font->Color;
  8710. QueueLabelUpdateStatus();
  8711. RemoteDirView->DarkMode = WinConfiguration->UseDarkTheme();
  8712. RemoteDriveView->DarkMode = RemoteDirView->DarkMode;
  8713. if (FImmersiveDarkMode != WinConfiguration->UseDarkTheme())
  8714. {
  8715. UpdateDarkMode();
  8716. }
  8717. reinterpret_cast<TTBCustomItem *>(GetComponent(fcRemotePathComboBox))->Enabled = HasTerminal || IsLocalBrowserMode();
  8718. MessageDock->Visible = (MessageDock->ToolbarCount > 0);
  8719. }
  8720. }
  8721. //---------------------------------------------------------------------------
  8722. void __fastcall TCustomScpExplorerForm::DoDirViewLoaded(TCustomDirView * /*Sender*/)
  8723. {
  8724. UpdateControls();
  8725. }
  8726. //---------------------------------------------------------------------------
  8727. void __fastcall TCustomScpExplorerForm::DirViewLoaded(
  8728. TObject * Sender)
  8729. {
  8730. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  8731. DebugAssert(DirView != NULL);
  8732. DoDirViewLoaded(DirView);
  8733. TransferPresetAutoSelect();
  8734. }
  8735. //---------------------------------------------------------------------------
  8736. void __fastcall TCustomScpExplorerForm::StartUpdates()
  8737. {
  8738. TUpdatesConfiguration Updates = WinConfiguration->Updates;
  8739. // first run after installation
  8740. if (double(Updates.LastCheck) == 0)
  8741. {
  8742. AppLog(L"First run, scheduling updates check for the next run");
  8743. // make sure next time there will be an update (if enabled)
  8744. Updates.LastCheck = TDateTime(1);
  8745. WinConfiguration->Updates = Updates;
  8746. }
  8747. else
  8748. {
  8749. TDateTime Period;
  8750. if (IsUWP() && !IsOfficialPackage())
  8751. {
  8752. Period = DefaultUpdatesPeriod;
  8753. AppLog(L"Thirdparty UWP package, using default updates check period");
  8754. }
  8755. else
  8756. {
  8757. Period = Updates.Period;
  8758. }
  8759. AppLogFmt(L"Updates check period: %.2f", (double(Period)));
  8760. if (double(Period) > 0)
  8761. {
  8762. TDateTime Interval = Now() - Updates.LastCheck;
  8763. AppLogFmt(L"Interval since the last updates check: %.2f", (double(Interval)));
  8764. if (Interval >= Period)
  8765. {
  8766. StartUpdateThread(UpdatesChecked);
  8767. }
  8768. }
  8769. }
  8770. }
  8771. //---------------------------------------------------------------------------
  8772. void __fastcall TCustomScpExplorerForm::UpdatesChecked()
  8773. {
  8774. AppLog(L"Updates check done");
  8775. UnicodeString Message;
  8776. bool New;
  8777. TQueryType Type;
  8778. GetUpdatesMessage(Message, New, Type, false);
  8779. if (!Message.IsEmpty())
  8780. {
  8781. AppLogFmt(L"Updates check message: %s", (Message));
  8782. if (New)
  8783. {
  8784. AppLog(L"New version detected");
  8785. Message = FMTLOAD(NEW_VERSION_CLICK, (Message));
  8786. }
  8787. if (!New && (Type != qtWarning))
  8788. {
  8789. AppLog(L"Posting non-critical updates note");
  8790. PostNote(UnformatMessage(Message), 0, UpdatesNoteClicked, NULL);
  8791. }
  8792. else
  8793. {
  8794. AppLog(L"Posting new version or critical message notification");
  8795. Notify(NULL, Message, Type, true, UpdatesNoteClicked);
  8796. }
  8797. if (New)
  8798. {
  8799. Configuration->Usage->Inc(L"UpdateNotifications");
  8800. }
  8801. }
  8802. }
  8803. //---------------------------------------------------------------------------
  8804. void __fastcall TCustomScpExplorerForm::UpdatesNoteClicked(TObject * /*Sender*/)
  8805. {
  8806. RestoreApp();
  8807. bool CanDisplay = !NonVisualDataModule->Busy;
  8808. if (!CanDisplay && (Screen->ActiveForm != NULL))
  8809. {
  8810. CanDisplay = (Screen->ActiveForm->Perform(WM_CAN_DISPLAY_UPDATES, 0, 0) != 0);
  8811. }
  8812. if (CanDisplay)
  8813. {
  8814. Configuration->Usage->Inc(L"UpdateNotificationsClicked");
  8815. // Probably not needed as CheckForUpdates is blocking since we started using neon for HTTP implementation.
  8816. NonVisualDataModule->StartBusy();
  8817. try
  8818. {
  8819. CheckForUpdates(true);
  8820. }
  8821. __finally
  8822. {
  8823. NonVisualDataModule->EndBusy();
  8824. }
  8825. }
  8826. else
  8827. {
  8828. Configuration->Usage->Inc(L"UpdateNotificationsBusy");
  8829. }
  8830. }
  8831. //---------------------------------------------------------------------------
  8832. void __fastcall TCustomScpExplorerForm::GetTransferPresetAutoSelectData(
  8833. TCopyParamRuleData & Data)
  8834. {
  8835. DebugAssert(Terminal != NULL);
  8836. Data.HostName = Terminal->SessionData->HostNameExpanded;
  8837. Data.UserName = Terminal->SessionData->UserNameExpanded;
  8838. Data.RemoteDirectory = RemoteDirView->PathName;
  8839. }
  8840. //---------------------------------------------------------------------------
  8841. void __fastcall TCustomScpExplorerForm::TransferPresetAutoSelect()
  8842. {
  8843. // Terminal can be null when we are changing local directory implicitly,
  8844. // because it has been deleted, while no session is connected
  8845. // (Login dialog is open)
  8846. if (FAllowTransferPresetAutoSelect && (Terminal != NULL))
  8847. {
  8848. DebugAssert(!IsLocalBrowserMode());
  8849. TCopyParamRuleData Data;
  8850. GetTransferPresetAutoSelectData(Data);
  8851. int CopyParamIndex = GUIConfiguration->CopyParamList->Find(Data);
  8852. UnicodeString CopyParamCurrent = GUIConfiguration->CopyParamCurrent;
  8853. if (CopyParamIndex < 0)
  8854. {
  8855. // there is no preset that matches autoselection
  8856. // set preset that we consider "default"
  8857. FCopyParamAutoSelected = L""; // forget last autoselected preset
  8858. GUIConfiguration->CopyParamCurrent = FCopyParamDefault;
  8859. }
  8860. else
  8861. {
  8862. // there is preset matching autoselection
  8863. UnicodeString CopyParamName = GUIConfiguration->CopyParamList->Names[CopyParamIndex];
  8864. if (CopyParamName == FCopyParamAutoSelected)
  8865. {
  8866. // autoselected the same preset as the last time
  8867. // make no change (i.e. preserve custom user preset, if any)
  8868. }
  8869. else
  8870. {
  8871. // autoselected the different preset then the last time (or there
  8872. // was default preset set)
  8873. FCopyParamAutoSelected = CopyParamName; // remember autoselection
  8874. GUIConfiguration->CopyParamCurrent = CopyParamName;
  8875. }
  8876. }
  8877. if (GUIConfiguration->CopyParamCurrent != CopyParamCurrent)
  8878. {
  8879. if (CopyParamIndex >= 0)
  8880. {
  8881. Configuration->Usage->Inc(L"CopyParamAutoSelects");
  8882. }
  8883. TTransferPresetNoteData * Data = new TTransferPresetNoteData;
  8884. try
  8885. {
  8886. int Fmt =
  8887. (CopyParamIndex < 0) ?
  8888. (GUIConfiguration->CopyParamIndex < 0 ? COPY_PARAM_DEFAULT_NORM : COPY_PARAM_DEFAULT_CUSTOM) :
  8889. COPY_PARAM_AUTOSELECTED;
  8890. UnicodeString Message = FMTLOAD(Fmt, (StripHotkey(GUIConfiguration->CopyParamCurrent)));
  8891. Data->Message = MainInstructions(Message);
  8892. int CopyParamAttrs = Terminal->UsableCopyParamAttrs(0).General;
  8893. UnicodeString Info = GUIConfiguration->CurrentCopyParam.GetInfoStr(L"\n",
  8894. CopyParamAttrs);
  8895. if (CopyParamIndex >= 0)
  8896. {
  8897. DebugAssert(GUIConfiguration->CopyParamList->Rules[CopyParamIndex] != NULL);
  8898. Info = FORMAT(L"%s\n \n%s", (Info,
  8899. FMTLOAD(COPY_PARAM_RULE,
  8900. (GUIConfiguration->CopyParamList->Rules[CopyParamIndex]->GetInfoStr(L"\n")))));
  8901. }
  8902. Data->Message += L"\n\n" + Info;
  8903. if (WinConfiguration->CopyParamAutoSelectNotice)
  8904. {
  8905. TransferPresetNoteMessage(Data, true);
  8906. }
  8907. else
  8908. {
  8909. PostNote(Message, 0, TransferPresetNoteClicked, Data);
  8910. Data = NULL; // ownership passed
  8911. }
  8912. }
  8913. __finally
  8914. {
  8915. delete Data;
  8916. }
  8917. }
  8918. }
  8919. }
  8920. //---------------------------------------------------------------------------
  8921. void __fastcall TCustomScpExplorerForm::TransferPresetNoteMessage(
  8922. TTransferPresetNoteData * NoteData, bool AllowNeverAskAgain)
  8923. {
  8924. DebugAssert(NoteData != NULL);
  8925. DebugAssert(!IsLocalBrowserMode());
  8926. TMessageParams Params(AllowNeverAskAgain ? mpNeverAskAgainCheck : 0);
  8927. TQueryButtonAlias Aliases[1];
  8928. Aliases[0].Button = qaIgnore; // "ignore" is after "ok"
  8929. Aliases[0].Alias = LoadStr(CONFIGURE_BUTTON);
  8930. Params.Aliases = Aliases;
  8931. Params.AliasesCount = LENOF(Aliases);
  8932. unsigned int Result =
  8933. MoreMessageDialog(NoteData->Message, NULL, qtInformation,
  8934. qaOK | qaIgnore, HELP_COPY_PARAM_AUTOSELECTED, &Params);
  8935. switch (Result)
  8936. {
  8937. case qaNeverAskAgain:
  8938. DebugAssert(AllowNeverAskAgain);
  8939. WinConfiguration->CopyParamAutoSelectNotice = false;
  8940. break;
  8941. case qaIgnore:
  8942. PreferencesDialog(pmPresets);
  8943. break;
  8944. }
  8945. }
  8946. //---------------------------------------------------------------------------
  8947. void __fastcall TCustomScpExplorerForm::TransferPresetNoteClicked(TObject * Sender)
  8948. {
  8949. // as of now useless, as this is used for notes only, never for balloons, ...
  8950. RestoreApp();
  8951. // .. and we should never be busy here
  8952. if (DebugAlwaysTrue(!NonVisualDataModule->Busy))
  8953. {
  8954. TransferPresetNoteMessage(DebugNotNull(dynamic_cast<TTransferPresetNoteData *>(Sender)), false);
  8955. }
  8956. }
  8957. //---------------------------------------------------------------------------
  8958. void __fastcall TCustomScpExplorerForm::PreferencesDialog(
  8959. TPreferencesMode APreferencesMode)
  8960. {
  8961. std::unique_ptr<TPreferencesDialogData> PreferencesData;
  8962. TCopyParamRuleData Data;
  8963. if (Terminal != NULL)
  8964. {
  8965. PreferencesData.reset(new TPreferencesDialogData());
  8966. GetTransferPresetAutoSelectData(Data);
  8967. PreferencesData->CopyParamRuleData = &Data;
  8968. }
  8969. DoPreferencesDialog(APreferencesMode, PreferencesData.get());
  8970. }
  8971. //---------------------------------------------------------------------------
  8972. void __fastcall TCustomScpExplorerForm::AdHocCustomCommandValidate(
  8973. const TCustomCommandType & Command)
  8974. {
  8975. if (CustomCommandState(Command, FEditingFocusedAdHocCommand, ccltAll) <= 0)
  8976. {
  8977. throw Exception(FMTLOAD(CUSTOM_COMMAND_IMPOSSIBLE2, (Command.Command)));
  8978. }
  8979. }
  8980. //---------------------------------------------------------------------------
  8981. void __fastcall TCustomScpExplorerForm::AdHocCustomCommand(bool OnFocused)
  8982. {
  8983. bool RemoteAllowed = CustomCommandRemoteAllowed();
  8984. TCustomCommandType Command;
  8985. // make sure we use local custom command when remote are not supported
  8986. if (RemoteAllowed || FLAGSET(FLastCustomCommand.Params, ccLocal))
  8987. {
  8988. Command = FLastCustomCommand;
  8989. }
  8990. else
  8991. {
  8992. Command.Params = Command.Params | ccLocal;
  8993. }
  8994. Command.Name = LoadStr(CUSTOM_COMMAND_AD_HOC_NAME);
  8995. FEditingFocusedAdHocCommand = OnFocused;
  8996. bool LocalSide = IsSideLocalBrowser(FCurrentSide);
  8997. int Options =
  8998. FLAGMASK((!RemoteAllowed || LocalSide), ccoDisableRemote) |
  8999. FLAGMASK(LocalSide, ccoDisableRemoteFiles);
  9000. if (DoCustomCommandDialog(Command, WinConfiguration->CustomCommandList,
  9001. ccmAdHoc, Options, AdHocCustomCommandValidate, NULL))
  9002. {
  9003. FLastCustomCommand = Command;
  9004. UpdateCustomCommandsToolbar();
  9005. ExecuteFileOperation(foCustomCommand, osRemote, OnFocused, false, &Command);
  9006. }
  9007. }
  9008. //---------------------------------------------------------------------------
  9009. void __fastcall TCustomScpExplorerForm::LastCustomCommand(bool OnFocused)
  9010. {
  9011. DebugAssert(!FLastCustomCommand.Command.IsEmpty());
  9012. int State = CustomCommandState(FLastCustomCommand, OnFocused, ccltAll);
  9013. DebugAssert(State > 0);
  9014. if (State <= 0)
  9015. {
  9016. throw Exception(FMTLOAD(CUSTOM_COMMAND_IMPOSSIBLE2, (FLastCustomCommand.Command)));
  9017. }
  9018. ExecuteFileOperation(foCustomCommand, osRemote, OnFocused, false, &FLastCustomCommand);
  9019. }
  9020. //---------------------------------------------------------------------------
  9021. bool __fastcall TCustomScpExplorerForm::GetLastCustomCommand(bool OnFocused,
  9022. TCustomCommandType & Command, int & State)
  9023. {
  9024. bool Result = !FLastCustomCommand.Command.IsEmpty();
  9025. if (Result)
  9026. {
  9027. Command = FLastCustomCommand;
  9028. State = CustomCommandState(FLastCustomCommand, OnFocused, ccltAll);
  9029. }
  9030. return Result;
  9031. }
  9032. //---------------------------------------------------------------------------
  9033. void __fastcall TCustomScpExplorerForm::BeforeAction()
  9034. {
  9035. ResetIncrementalSearch();
  9036. }
  9037. //---------------------------------------------------------------------------
  9038. void __fastcall TCustomScpExplorerForm::PostComponentHide(Byte Component)
  9039. {
  9040. DebugAssert(ComponentVisible[Component]);
  9041. PostMessage(Handle, WM_COMPONENT_HIDE, Component, 0);
  9042. }
  9043. //---------------------------------------------------------------------------
  9044. void __fastcall TCustomScpExplorerForm::QueueSplitterDblClick(TObject * /*Sender*/)
  9045. {
  9046. // when queue panel is resized here directly, the status bar is stretched
  9047. // over whole space the panel occupied
  9048. PostComponentHide(fcQueueView);
  9049. }
  9050. //---------------------------------------------------------------------------
  9051. void __fastcall TCustomScpExplorerForm::QueueFileListSplitterDblClick(TObject *)
  9052. {
  9053. ComponentVisible[fcQueueFileList] = false;
  9054. }
  9055. //---------------------------------------------------------------------------
  9056. void __fastcall TCustomScpExplorerForm::ThemeChanged()
  9057. {
  9058. // We hoped this will refresh scrollbar colors, but it does not have any effect here.
  9059. RefreshColorMode();
  9060. ResetSysDarkTheme();
  9061. ConfigurationChanged();
  9062. ConfigureInterface();
  9063. // Should be called for all controls
  9064. RemoteDirView->Perform(WM_THEMECHANGED, 0, 0);
  9065. }
  9066. //---------------------------------------------------------------------------
  9067. void __fastcall TCustomScpExplorerForm::WMSettingChange(TMessage & Message)
  9068. {
  9069. // Do not handle, when shutting down anyway (maybe also when not setup completely yet?)
  9070. if (!FInvalid &&
  9071. (Message.LParam != 0) &&
  9072. (wcscmp(reinterpret_cast<LPCWCH>(Message.LParam), L"ImmersiveColorSet") == 0))
  9073. {
  9074. ThemeChanged();
  9075. }
  9076. TForm::Dispatch(&Message);
  9077. }
  9078. //---------------------------------------------------------------------------
  9079. void __fastcall TCustomScpExplorerForm::Dispatch(void * Message)
  9080. {
  9081. TMessage * M = static_cast<TMessage*>(Message);
  9082. switch (M->Msg)
  9083. {
  9084. case CM_DIALOGCHAR:
  9085. CMDialogChar(*M);
  9086. break;
  9087. case WM_APPCOMMAND:
  9088. WMAppCommand(*M);
  9089. break;
  9090. case WM_SYSCOMMAND:
  9091. WMSysCommand(*M);
  9092. break;
  9093. case WM_QUERYENDSESSION:
  9094. WMQueryEndSession(*M);
  9095. break;
  9096. case WM_ENDSESSION:
  9097. WMEndSession(*reinterpret_cast<TWMEndSession *>(M));
  9098. break;
  9099. #ifdef _DEBUG
  9100. case WM_WINDOWPOSCHANGED:
  9101. WMWindowPosChanged(*reinterpret_cast<TWMWindowPosMsg *>(M));
  9102. break;
  9103. case CM_CUSTOMSTYLECHANGED:
  9104. TForm::Dispatch(Message);
  9105. break;
  9106. #endif
  9107. case WM_COMPONENT_HIDE:
  9108. {
  9109. Byte Component = static_cast<Byte>(M->WParam);
  9110. // sanity check
  9111. if (ComponentVisible[Component])
  9112. {
  9113. // special treatment
  9114. if (Component == fcQueueView)
  9115. {
  9116. ToggleQueueVisibility();
  9117. DebugAssert(!ComponentVisible[fcQueueView]);
  9118. }
  9119. else
  9120. {
  9121. ComponentVisible[Component] = false;
  9122. }
  9123. }
  9124. }
  9125. break;
  9126. case WM_COPYDATA:
  9127. WMCopyData(*M);
  9128. break;
  9129. case WM_MANAGES_CAPTION:
  9130. // caption managed in TTerminalManager::UpdateAppTitle()
  9131. M->Result = 1;
  9132. break;
  9133. case WM_WANTS_MOUSEWHEEL:
  9134. M->Result = 1;
  9135. break;
  9136. case CM_SHOWINGCHANGED:
  9137. CMShowingChanged(*M);
  9138. break;
  9139. case WM_CLOSE:
  9140. WMClose(*M);
  9141. break;
  9142. case WM_DPICHANGED:
  9143. WMDpiChanged(*M);
  9144. break;
  9145. case CM_DPICHANGED:
  9146. CMDpiChanged(*M);
  9147. break;
  9148. case WM_SETTINGCHANGE:
  9149. WMSettingChange(*M);
  9150. break;
  9151. case CM_DIALOGKEY:
  9152. CMDialogKey(*reinterpret_cast<TWMKeyDown *>(M));
  9153. break;
  9154. case WM_PASTE_FILES:
  9155. PasteFiles();
  9156. break;
  9157. case WM_IS_HIDDEN:
  9158. // To treat master password prompt as a main window while showing it when workspace is loading while
  9159. // the exporer is showing up (Visible is true already)
  9160. M->Result = !FShowing;
  9161. break;
  9162. default:
  9163. TForm::Dispatch(Message);
  9164. break;
  9165. }
  9166. }
  9167. //---------------------------------------------------------------------------
  9168. void __fastcall TCustomScpExplorerForm::UpdateImages()
  9169. {
  9170. // noop
  9171. }
  9172. //---------------------------------------------------------------------------
  9173. void __fastcall TCustomScpExplorerForm::CMDpiChanged(TMessage & Message)
  9174. {
  9175. TForm::Dispatch(&Message);
  9176. FSessionColors->Clear();
  9177. AddFixedSessionImages();
  9178. RegenerateSessionColorsImageList(FSessionColors, FSessionColorMaskImageIndex);
  9179. UpdateImages();
  9180. }
  9181. //---------------------------------------------------------------------------
  9182. void __fastcall TCustomScpExplorerForm::WMDpiChanged(TMessage & Message)
  9183. {
  9184. TForm::Dispatch(&Message);
  9185. Configuration->Usage->Inc(L"PixelsPerInchChanged");
  9186. UpdatePixelsPerInchMainWindowCounter();
  9187. }
  9188. //---------------------------------------------------------------------------
  9189. void __fastcall TCustomScpExplorerForm::WMClose(TMessage & Message)
  9190. {
  9191. // Cannot close window while we are busy.
  9192. // We cannot test this in FormCloseQuery as that is called also from
  9193. // Close(), which is called by CloseApplicationAction. So we can be busy
  9194. // there already even, when it is legitimate to close the application.
  9195. // Possibly a better place to handle this would be WMSysCommand.
  9196. if (NonVisualDataModule->Busy)
  9197. {
  9198. Message.Result = 1;
  9199. }
  9200. else
  9201. {
  9202. TForm::Dispatch(&Message);
  9203. }
  9204. }
  9205. //---------------------------------------------------------------------------
  9206. void __fastcall TCustomScpExplorerForm::CMShowingChanged(TMessage & Message)
  9207. {
  9208. if (!FShowingChanged)
  9209. {
  9210. AddStartupSequence(L"D");
  9211. FShowingChanged = true;
  9212. }
  9213. // Ignoring implicit local-local browser of Commander, opened from DoShow
  9214. bool NoSession = (ManagedSession == NULL);
  9215. // This calls TCustomScpExplorerForm::DoShow
  9216. TForm::Dispatch(&Message);
  9217. // Now the window is visible (TForm::Dispatch is what usually takes long, like when loading a "local" network directory)
  9218. if (Showing && !FStarted)
  9219. {
  9220. FStarted = true;
  9221. InterfaceStarted();
  9222. }
  9223. if (Showing && NoSession)
  9224. {
  9225. // When we are starting minimized (i.e. from an installer),
  9226. // postpone showing Login dialog until we get restored.
  9227. // Otherwise the Login dialog (and Authentication window) show restored
  9228. // over invisible (minimized) main window.
  9229. if (WindowState == wsMinimized)
  9230. {
  9231. FNeedSession = true;
  9232. }
  9233. else
  9234. {
  9235. // This happens before application ever goes idle, so the toolbars would
  9236. // stay enabled (initial state) until the Login dialog is dismissed.
  9237. UpdateActions();
  9238. NonVisualDataModule->StartBusy();
  9239. try
  9240. {
  9241. // Need to process WM_ACTIVATEAPP before showing the Login dialog,
  9242. // otherwise the dialog does not receive focus.
  9243. // With Commander interface the ProcessMessages is called already
  9244. // by TDriveView, but with Explorer interface, we need to call it explicily
  9245. Application->ProcessMessages();
  9246. // do not reload sessions, they have been loaded just now (optimization)
  9247. NeedSession(true);
  9248. }
  9249. __finally
  9250. {
  9251. NonVisualDataModule->EndBusy();
  9252. }
  9253. }
  9254. }
  9255. }
  9256. //---------------------------------------------------------------------------
  9257. void __fastcall TCustomScpExplorerForm::CenterReconnectToolbar()
  9258. {
  9259. ReconnectToolbar->Left = (ReconnectToolbar->Parent->ClientWidth - ReconnectToolbar->Width) / 2;
  9260. ReconnectToolbar->Top = (ReconnectToolbar->Parent->ClientHeight - ReconnectToolbar->Height) / 2;
  9261. }
  9262. //---------------------------------------------------------------------------
  9263. void __fastcall TCustomScpExplorerForm::FormConstrainedResize(
  9264. TObject * /*Sender*/, int & MinWidth, int & MinHeight, int & MaxWidth,
  9265. int & MaxHeight)
  9266. {
  9267. // workaround for bug in TWinControl.CalcConstraints
  9268. // Check for empty rect (restore from iconic state) is done there only after
  9269. // call to AdjustClientRect, which enlarges the rect (for forms).
  9270. TRect R = GetClientRect();
  9271. // when restoring from iconic state, place no restrictions
  9272. if (IsRectEmpty(R))
  9273. {
  9274. MinWidth = 0;
  9275. MinHeight = 0;
  9276. MaxWidth = 32000;
  9277. MaxHeight = 32000;
  9278. }
  9279. }
  9280. //---------------------------------------------------------------------------
  9281. void __fastcall TCustomScpExplorerForm::GetSpaceAvailable(const UnicodeString Path,
  9282. TSpaceAvailable & ASpaceAvailable, bool & Close)
  9283. {
  9284. // terminal can be already closed (e.g. dropped connection)
  9285. if ((Terminal != NULL) && Terminal->IsCapable[fcCheckingSpaceAvailable])
  9286. {
  9287. Configuration->Usage->Inc(L"SpaceAvailableChecks");
  9288. try
  9289. {
  9290. Terminal->SpaceAvailable(Path, ASpaceAvailable);
  9291. }
  9292. catch(...)
  9293. {
  9294. if (!Terminal->Active)
  9295. {
  9296. Close = true;
  9297. }
  9298. throw;
  9299. }
  9300. }
  9301. }
  9302. //---------------------------------------------------------------------------
  9303. void __fastcall TCustomScpExplorerForm::FileSystemInfo()
  9304. {
  9305. const TSessionInfo & SessionInfo = Terminal->GetSessionInfo();
  9306. const TFileSystemInfo & FileSystemInfo = Terminal->GetFileSystemInfo(true);
  9307. TGetSpaceAvailable OnGetSpaceAvailable = NULL;
  9308. if (Terminal->IsCapable[fcCheckingSpaceAvailable])
  9309. {
  9310. OnGetSpaceAvailable = GetSpaceAvailable;
  9311. }
  9312. DoFileSystemInfoDialog(SessionInfo, FileSystemInfo, Terminal->CurrentDirectory,
  9313. OnGetSpaceAvailable);
  9314. }
  9315. //---------------------------------------------------------------------------
  9316. TSessionData * __fastcall TCustomScpExplorerForm::SessionDataForCode()
  9317. {
  9318. std::unique_ptr<TSessionData> Data(CloneCurrentSessionData());
  9319. Terminal->FillSessionDataForCode(Data.get());
  9320. return Data.release();
  9321. }
  9322. //---------------------------------------------------------------------------
  9323. void __fastcall TCustomScpExplorerForm::GenerateUrl(TStrings * Paths)
  9324. {
  9325. std::unique_ptr<TSessionData> Data(SessionDataForCode());
  9326. DoGenerateUrlDialog(Data.get(), Paths);
  9327. }
  9328. //---------------------------------------------------------------------------
  9329. void __fastcall TCustomScpExplorerForm::SessionGenerateUrl()
  9330. {
  9331. GenerateUrl(NULL);
  9332. }
  9333. //---------------------------------------------------------------------------
  9334. void __fastcall TCustomScpExplorerForm::FileGenerateUrl()
  9335. {
  9336. std::unique_ptr<TStrings> Paths(new TStringList());
  9337. DirView(osCurrent)->CreateFileList(false, true, Paths.get());
  9338. GenerateUrl(Paths.get());
  9339. }
  9340. //---------------------------------------------------------------------------
  9341. void __fastcall TCustomScpExplorerForm::UpdateSessionColor(TColor value)
  9342. {
  9343. FSessionColor = value;
  9344. TColor C = (value != 0 ? value : Vcl::Graphics::clNone);
  9345. TTBXColorItem * ColorItem = dynamic_cast<TTBXColorItem *>(
  9346. static_cast<TObject *>(GetComponent(fcColorMenu)));
  9347. DebugAssert(ColorItem != NULL);
  9348. ColorItem->Color = C;
  9349. NonVisualDataModule->ColorMenuItem->Color = C;
  9350. UpdateControls();
  9351. }
  9352. //---------------------------------------------------------------------------
  9353. void __fastcall TCustomScpExplorerForm::SetSessionColor(TColor value)
  9354. {
  9355. if (value != FSessionColor)
  9356. {
  9357. UpdateSessionColor(value);
  9358. // Is null when called from LastTerminalClosed
  9359. if (Terminal != NULL)
  9360. {
  9361. SessionsPageControl->ActivePage->ImageIndex = AddSessionColor(value);
  9362. }
  9363. }
  9364. }
  9365. //---------------------------------------------------------------------------
  9366. bool __fastcall TCustomScpExplorerForm::CancelNote(bool Force)
  9367. {
  9368. bool Result = FNoteTimer->Enabled;
  9369. if (Result)
  9370. {
  9371. // cannot cancel note too early
  9372. bool NotEarly =
  9373. (Now() - FNoteShown >
  9374. EncodeTimeVerbose(0, 0, (unsigned short)(WinConfiguration->NotificationsStickTime), 0));
  9375. if (Force || NotEarly)
  9376. {
  9377. FNoteTimer->Enabled = false;
  9378. FNote = L"";
  9379. SAFE_DESTROY(FNoteData);
  9380. FOnNoteClick = NULL;
  9381. FNoteHints = FNotes->Text;
  9382. FNoteHints.Delete(FNoteHints.Length() - 1, 2);
  9383. UpdateStatusBar();
  9384. }
  9385. }
  9386. return Result;
  9387. }
  9388. //---------------------------------------------------------------------------
  9389. void __fastcall TCustomScpExplorerForm::NoteTimer(TObject * /*Sender*/)
  9390. {
  9391. DebugAssert(FNoteTimer->Enabled);
  9392. CancelNote(true);
  9393. }
  9394. //---------------------------------------------------------------------------
  9395. void __fastcall TCustomScpExplorerForm::UpdateNoteHints()
  9396. {
  9397. FNoteHints = FNotes->Text.TrimRight();
  9398. }
  9399. //---------------------------------------------------------------------------
  9400. void __fastcall TCustomScpExplorerForm::AddNote(UnicodeString Note, bool UpdateNow)
  9401. {
  9402. int P = Note.Pos(L"\n");
  9403. if (P > 0)
  9404. {
  9405. Note.SetLength(P - 1);
  9406. }
  9407. FNotes->Add(FORMAT(L"[%s] %s",
  9408. (FormatDateTime(Configuration->TimeFormat, Now()), Note)));
  9409. while (FNotes->Count > 10)
  9410. {
  9411. FNotes->Delete(0);
  9412. }
  9413. if (UpdateNow)
  9414. {
  9415. UpdateNoteHints();
  9416. UpdateStatusBar();
  9417. }
  9418. }
  9419. //---------------------------------------------------------------------------
  9420. void __fastcall TCustomScpExplorerForm::PostNote(UnicodeString Note,
  9421. unsigned int Seconds, TNotifyEvent OnNoteClick, TObject * NoteData)
  9422. {
  9423. int P = Note.Pos(L"\n");
  9424. if (P > 0)
  9425. {
  9426. Note.SetLength(P - 1);
  9427. }
  9428. UpdateNoteHints();
  9429. FNote = Note;
  9430. SAFE_DESTROY(FNoteData);
  9431. FNoteData = NoteData;
  9432. FOnNoteClick = OnNoteClick;
  9433. AddNote(Note, false);
  9434. UpdateStatusBar();
  9435. FNoteShown = Now();
  9436. FNoteTimer->Enabled = false;
  9437. if (Seconds == 0)
  9438. {
  9439. Seconds = WinConfiguration->NotificationsTimeout;
  9440. }
  9441. FNoteTimer->Interval = Seconds * MSecsPerSec;
  9442. FNoteTimer->Enabled = true;
  9443. }
  9444. //---------------------------------------------------------------------------
  9445. void __fastcall TCustomScpExplorerForm::ReadDirectoryCancelled()
  9446. {
  9447. PostNote(LoadStr(DIRECTORY_READING_CANCELLED), 0, NULL, NULL);
  9448. }
  9449. //---------------------------------------------------------------------------
  9450. void __fastcall TCustomScpExplorerForm::SynchronizeBrowsingChanged()
  9451. {
  9452. if (NonVisualDataModule->SynchronizeBrowsingAction2->Checked)
  9453. {
  9454. Configuration->Usage->Inc(L"SynchronizeBrowsingEnabled");
  9455. }
  9456. PostNote(FORMAT(LoadStrPart(SYNC_DIR_BROWSE_TOGGLE, 1),
  9457. (LoadStrPart(SYNC_DIR_BROWSE_TOGGLE,
  9458. (NonVisualDataModule->SynchronizeBrowsingAction2->Checked ? 2 : 3)))),
  9459. 0, NULL, NULL);
  9460. }
  9461. //---------------------------------------------------------------------------
  9462. void __fastcall TCustomScpExplorerForm::ToggleShowHiddenFiles()
  9463. {
  9464. bool NewShowHiddenFiles = !WinConfiguration->ShowHiddenFiles;
  9465. UnicodeString Note =
  9466. FORMAT(LoadStrPart(SHOW_HIDDEN_FILES_TOGGLE, 1), (LoadStrPart(SHOW_HIDDEN_FILES_TOGGLE, (NewShowHiddenFiles ? 2 : 3))));
  9467. PostNote(Note, 0, NULL, NULL);
  9468. GetComponent(fcStatusBar)->Repaint(); // toggling ShowHiddenFiles takes time, force repaint beforehand
  9469. WinConfiguration->ShowHiddenFiles = NewShowHiddenFiles;
  9470. }
  9471. //---------------------------------------------------------------------------
  9472. void __fastcall TCustomScpExplorerForm::SetFormatSizeBytes(TFormatBytesStyle Style)
  9473. {
  9474. WinConfiguration->FormatSizeBytes = Style;
  9475. }
  9476. //---------------------------------------------------------------------------
  9477. void __fastcall TCustomScpExplorerForm::ToggleAutoReadDirectoryAfterOp()
  9478. {
  9479. Configuration->AutoReadDirectoryAfterOp = !Configuration->AutoReadDirectoryAfterOp;
  9480. PostNote(FORMAT(LoadStrPart(AUTO_READ_DIRECTORY_TOGGLE, 1),
  9481. (LoadStrPart(AUTO_READ_DIRECTORY_TOGGLE,
  9482. (Configuration->AutoReadDirectoryAfterOp ? 2 : 3)))), 0, NULL, NULL);
  9483. }
  9484. //---------------------------------------------------------------------------
  9485. void __fastcall TCustomScpExplorerForm::StatusBarPanelDblClick(
  9486. TTBXCustomStatusBar * Sender, TTBXStatusPanel * Panel)
  9487. {
  9488. if (Panel->Index == 0)
  9489. {
  9490. if (FOnNoteClick != NULL)
  9491. {
  9492. // prevent the user data from being freed by possible call
  9493. // to CancelNote or PostNote during call to OnNoteClick
  9494. std::unique_ptr<TObject> NoteData(FNoteData);
  9495. TNotifyEvent OnNoteClick = FOnNoteClick;
  9496. FNoteData = NULL;
  9497. // need to cancel the note as we are going to delete its user data
  9498. CancelNote(true);
  9499. OnNoteClick(NoteData.get());
  9500. }
  9501. }
  9502. if (HasActiveTerminal() &&
  9503. (Panel->Index >= Sender->Panels->Count - SessionPanelCount))
  9504. {
  9505. FileSystemInfo();
  9506. }
  9507. }
  9508. //---------------------------------------------------------------------------
  9509. void __fastcall TCustomScpExplorerForm::LockWindow(bool Force)
  9510. {
  9511. // workaround:
  9512. // 1) for unknown reason, disabling window, while minimized,
  9513. // prevents it from restoring, even if it was enabled again meanwhile
  9514. // 2) when disabling the main window, while another has focus
  9515. // minimize is no longer possible
  9516. // ("keep up to date" dialog - though does not seem to be the case with "find file" -
  9517. // so it has probably something to do with the semi-modalness of the "keep up to date" dialog)
  9518. // Shouldn't we use IsApplicationMinimized() here?
  9519. if ((FLockSuspendLevel == 0) && !IsIconic(Application->Handle) && (Force || (Screen->ActiveForm == this)))
  9520. {
  9521. Enabled = false;
  9522. }
  9523. FLockLevel++;
  9524. }
  9525. //---------------------------------------------------------------------------
  9526. void __fastcall TCustomScpExplorerForm::UnlockWindow()
  9527. {
  9528. DebugAssert(FLockLevel > 0);
  9529. FLockLevel--;
  9530. if (FLockLevel == 0)
  9531. {
  9532. DebugAssert(FLockSuspendLevel == 0);
  9533. Enabled = true;
  9534. }
  9535. }
  9536. //---------------------------------------------------------------------------
  9537. void __fastcall TCustomScpExplorerForm::SuspendWindowLock()
  9538. {
  9539. // We need to make sure that window is enabled when the last modal window closes
  9540. // otherwise focus is not restored correctly.
  9541. // So we re-enable the window when modal window opens and
  9542. // disable it back after is closes
  9543. if (FLockLevel > 0)
  9544. {
  9545. // while we have nesting counter, we know that we never be called
  9546. // recursivelly as Application->OnModalBegin is called only
  9547. // for the top-level modal window
  9548. if (DebugAlwaysTrue(FLockSuspendLevel == 0))
  9549. {
  9550. // won't be disabled when conditions in LockWindow() were not satisfied
  9551. FDisabledOnLockSuspend = !Enabled;
  9552. // When minimized to tray (or actually when set to SW_HIDE),
  9553. // setting Enabled makes the window focusable even when there's
  9554. // modal window over it
  9555. if (!FTrayIcon->Visible)
  9556. {
  9557. Enabled = true;
  9558. }
  9559. }
  9560. FLockSuspendLevel++;
  9561. }
  9562. }
  9563. //---------------------------------------------------------------------------
  9564. void __fastcall TCustomScpExplorerForm::ResumeWindowLock()
  9565. {
  9566. if (FLockSuspendLevel > 0)
  9567. {
  9568. DebugAssert(FLockLevel > 0);
  9569. FLockSuspendLevel--;
  9570. // see comment in SuspendWindowLock
  9571. if (DebugAlwaysTrue(FLockSuspendLevel == 0))
  9572. {
  9573. // Note that window can be enabled here, when we were minized to tray when
  9574. // was SuspendWindowLock() called.
  9575. // We should possibly do the same check as in LockWindow(),
  9576. // if it is ever possible that the consitions change between
  9577. // SuspendWindowLock() and ResumeWindowLock()
  9578. Enabled = !FDisabledOnLockSuspend;
  9579. }
  9580. }
  9581. }
  9582. //---------------------------------------------------------------------------
  9583. void __fastcall TCustomScpExplorerForm::UpdateRemotePathComboBox(bool TextOnly)
  9584. {
  9585. TTBXComboBoxItem * RemotePathComboBox =
  9586. reinterpret_cast<TTBXComboBoxItem *>(GetComponent(fcRemotePathComboBox));
  9587. if (!TextOnly)
  9588. {
  9589. TStrings * Items = RemotePathComboBox->Strings;
  9590. Items->BeginUpdate();
  9591. try
  9592. {
  9593. Items->Clear();
  9594. if ((Terminal != NULL) && !RemoteDirView->Path.IsEmpty())
  9595. {
  9596. UnicodeString APath = UnixExcludeTrailingBackslash(RemoteDirView->Path);
  9597. while (!IsUnixRootPath(APath))
  9598. {
  9599. int P = APath.LastDelimiter(L'/');
  9600. DebugAssert(P >= 0);
  9601. Items->Insert(0, APath.SubString(P + 1, APath.Length() - P));
  9602. APath.SetLength(P - 1);
  9603. }
  9604. Items->Insert(0, Customunixdirview_SUnixDefaultRootName);
  9605. }
  9606. }
  9607. __finally
  9608. {
  9609. Items->EndUpdate();
  9610. }
  9611. }
  9612. RemotePathComboBox->ItemIndex = RemotePathComboBox->Strings->Count - 1;
  9613. // Setting ItemIndex to -1 does not reset its text
  9614. if (RemotePathComboBox->Strings->Count == 0)
  9615. {
  9616. RemotePathComboBox->Text = L"";
  9617. }
  9618. }
  9619. //---------------------------------------------------------------------------
  9620. void __fastcall TCustomScpExplorerForm::DoRemotePathComboBoxAdjustImageIndex(
  9621. TTBXComboBoxItem * Sender, const UnicodeString DebugUsedArg(AText), int AIndex, int & ImageIndex)
  9622. {
  9623. if (AIndex < 0)
  9624. {
  9625. AIndex = Sender->ItemIndex;
  9626. }
  9627. ImageIndex = (AIndex < Sender->Strings->Count - 1 ? StdDirIcon : StdDirSelIcon);
  9628. }
  9629. //---------------------------------------------------------------------------
  9630. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxAdjustImageIndex(
  9631. TTBXComboBoxItem * Sender, const UnicodeString AText, int AIndex,
  9632. int & ImageIndex)
  9633. {
  9634. DoRemotePathComboBoxAdjustImageIndex(Sender, AText, AIndex, ImageIndex);
  9635. }
  9636. //---------------------------------------------------------------------------
  9637. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxDrawItem(
  9638. TTBXCustomList * /*Sender*/, TCanvas * /*ACanvas*/, TRect & ARect, int AIndex,
  9639. int /*AHoverIndex*/, bool & /*DrawDefault*/)
  9640. {
  9641. if (!IsLocalBrowserMode())
  9642. {
  9643. ARect.Left += (10 * AIndex);
  9644. }
  9645. }
  9646. //---------------------------------------------------------------------------
  9647. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxMeasureWidth(
  9648. TTBXCustomList * /*Sender*/, TCanvas * /*ACanvas*/, int AIndex, int &AWidth)
  9649. {
  9650. AWidth += (10 * AIndex);
  9651. }
  9652. //---------------------------------------------------------------------------
  9653. void __fastcall TCustomScpExplorerForm::DoRemotePathComboBoxItemClick(TObject * Sender)
  9654. {
  9655. TTBXComboBoxItem * RemotePathComboBox = dynamic_cast<TTBXComboBoxItem*>(Sender);
  9656. UnicodeString APath = UnixExcludeTrailingBackslash(RemoteDirView->Path);
  9657. int Index = RemotePathComboBox->ItemIndex;
  9658. while (Index < RemotePathComboBox->Strings->Count - 1)
  9659. {
  9660. APath = UnixExtractFileDir(APath);
  9661. Index++;
  9662. }
  9663. // VanDyke style paths
  9664. if (APath.IsEmpty())
  9665. {
  9666. DebugAssert(RemotePathComboBox->ItemIndex == 0);
  9667. APath = ROOTDIRECTORY;
  9668. }
  9669. if (RemoteDirView->Path != APath)
  9670. {
  9671. RemoteDirView->Path = APath;
  9672. }
  9673. }
  9674. //---------------------------------------------------------------------------
  9675. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxItemClick(TObject * Sender)
  9676. {
  9677. DoRemotePathComboBoxItemClick(Sender);
  9678. }
  9679. //---------------------------------------------------------------------------
  9680. void __fastcall TCustomScpExplorerForm::DoRemotePathComboBoxCancel(TObject * Sender)
  9681. {
  9682. DebugAssert(Sender == GetComponent(fcRemotePathComboBox));
  9683. DebugUsedParam(Sender);
  9684. UpdateRemotePathComboBox(true);
  9685. }
  9686. //---------------------------------------------------------------------------
  9687. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxCancel(TObject * Sender)
  9688. {
  9689. DoRemotePathComboBoxCancel(Sender);
  9690. }
  9691. //---------------------------------------------------------------------------
  9692. void __fastcall TCustomScpExplorerForm::DirViewEditing(
  9693. TObject * Sender, TListItem * Item, bool & /*AllowEdit*/)
  9694. {
  9695. ResetIncrementalSearch();
  9696. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  9697. DebugAssert(DirView != NULL);
  9698. if (!WinConfiguration->RenameWholeName && !DirView->ItemIsDirectory(Item))
  9699. {
  9700. HWND Edit = ListView_GetEditControl(DirView->Handle);
  9701. // OnEditing is called also from TCustomListView::CanEdit
  9702. if (Edit != NULL)
  9703. {
  9704. EditSelectBaseName(Edit);
  9705. }
  9706. }
  9707. }
  9708. //---------------------------------------------------------------------------
  9709. //---------------------------------------------------------------------------
  9710. TDragDropFilesEx * __fastcall TCustomScpExplorerForm::CreateDragDropFilesEx()
  9711. {
  9712. TDragDropFilesEx * Result = new TDragDropFilesEx(this);
  9713. Result->AutoDetectDnD = false;
  9714. Result->NeedValid = TFileExMustDnDSet() << nvFilename;
  9715. Result->RenderDataOn = rdoEnterAndDropSync;
  9716. Result->TargetEffects = TDropEffectSet() << deCopy << deMove;
  9717. return Result;
  9718. }
  9719. //---------------------------------------------------------------------------
  9720. void __fastcall TCustomScpExplorerForm::UpdateDarkMode()
  9721. {
  9722. if (IsWin10Build(19041))
  9723. {
  9724. FImmersiveDarkMode = WinConfiguration->UseDarkTheme();
  9725. BOOL DarkMode = FImmersiveDarkMode ? TRUE : FALSE;
  9726. const int DWMWA_USE_IMMERSIVE_DARK_MODE = 20;
  9727. DwmSetWindowAttribute(Handle, DWMWA_USE_IMMERSIVE_DARK_MODE, &DarkMode, sizeof(DarkMode));
  9728. }
  9729. }
  9730. //---------------------------------------------------------------------------
  9731. void __fastcall TCustomScpExplorerForm::CreateWnd()
  9732. {
  9733. TForm::CreateWnd();
  9734. // win32-darkmode calls AllowDarkModeForWindow(this, true) here, but it does not seem to have any effect
  9735. UpdateDarkMode();
  9736. if (FSessionsDragDropFilesEx == NULL)
  9737. {
  9738. FSessionsDragDropFilesEx = CreateDragDropFilesEx();
  9739. FSessionsDragDropFilesEx->OnDragOver = SessionsDDDragOver;
  9740. FSessionsDragDropFilesEx->OnProcessDropped = SessionsDDProcessDropped;
  9741. FSessionsDragDropFilesEx->OnDragEnter = SessionsDDDragEnter;
  9742. FSessionsDragDropFilesEx->OnDragLeave = SessionsDDDragLeave;
  9743. }
  9744. if (FQueueDragDropFilesEx == NULL)
  9745. {
  9746. FQueueDragDropFilesEx = CreateDragDropFilesEx();
  9747. // No need to set OnDragOver as we do not have any restrictions
  9748. FQueueDragDropFilesEx->OnProcessDropped = QueueDDProcessDropped;
  9749. FQueueDragDropFilesEx->OnDragEnter = QueueDDDragEnter;
  9750. FQueueDragDropFilesEx->OnDragLeave = QueueDDDragLeave;
  9751. }
  9752. }
  9753. //---------------------------------------------------------------------------
  9754. void __fastcall TCustomScpExplorerForm::DestroyWnd()
  9755. {
  9756. TForm::DestroyWnd();
  9757. FSessionsDragDropFilesEx->DragDropControl = NULL;
  9758. FQueueDragDropFilesEx->DragDropControl = NULL;
  9759. }
  9760. //---------------------------------------------------------------------------
  9761. void __fastcall TCustomScpExplorerForm::FormShow(TObject * /*Sender*/)
  9762. {
  9763. SideEnter(FCurrentSide);
  9764. FEverShown = true;
  9765. }
  9766. //---------------------------------------------------------------------------
  9767. void __fastcall TCustomScpExplorerForm::DoFindFiles(
  9768. TTerminal * ATerminal, UnicodeString Directory, const TFileMasks & FileMask,
  9769. TFileFoundEvent OnFileFound, TFindingFileEvent OnFindingFile)
  9770. {
  9771. if (!NonVisualDataModule->Busy)
  9772. {
  9773. TTerminalManager::Instance()->ActiveSession = DebugNotNull(dynamic_cast<TManagedTerminal *>(ATerminal));
  9774. Configuration->Usage->Inc(L"FileFinds");
  9775. LockWindow(true);
  9776. NonVisualDataModule->StartBusy();
  9777. WinConfiguration->LockedInterface = true;
  9778. try
  9779. {
  9780. Terminal->FilesFind(Directory, FileMask, OnFileFound, OnFindingFile);
  9781. }
  9782. __finally
  9783. {
  9784. WinConfiguration->LockedInterface = false;
  9785. NonVisualDataModule->EndBusy();
  9786. UnlockWindow();
  9787. }
  9788. }
  9789. }
  9790. //---------------------------------------------------------------------------
  9791. void __fastcall TCustomScpExplorerForm::DoFocusRemotePath(TTerminal * ATerminal, const UnicodeString & Path)
  9792. {
  9793. if (!NonVisualDataModule->Busy)
  9794. {
  9795. TTerminalManager::Instance()->ActiveSession = DebugNotNull(dynamic_cast<TManagedTerminal *>(ATerminal));
  9796. SetFocus();
  9797. RemoteDirView->Path = UnixExtractFilePath(Path);
  9798. UnicodeString FileName = UnixExtractFileName(Path);
  9799. TListItem * Item = RemoteDirView->FindFileItem(FileName);
  9800. // If not found, it can because we have loaded directory from cache and the file is new, try again after reload.
  9801. // (tbd: we should not even try this, if we have not loaded the directory from the cache)
  9802. if (Item == NULL)
  9803. {
  9804. ReloadDirectory(osRemote);
  9805. Item = RemoteDirView->FindFileItem(FileName);
  9806. }
  9807. if (Item != NULL)
  9808. {
  9809. RemoteDirView->ItemFocused = Item;
  9810. RemoteDirView->ItemFocused->MakeVisible(false);
  9811. RemoteDirView->SetFocus();
  9812. }
  9813. }
  9814. }
  9815. //---------------------------------------------------------------------------
  9816. bool __fastcall TCustomScpExplorerForm::CanOperateOnFoundFiles(TTerminal * ATerminal)
  9817. {
  9818. bool Result = !NonVisualDataModule->Busy;
  9819. if (Result)
  9820. {
  9821. TTerminalManager::Instance()->ActiveSession = DebugNotNull(dynamic_cast<TManagedTerminal *>(ATerminal));
  9822. }
  9823. return Result;
  9824. }
  9825. //---------------------------------------------------------------------------
  9826. void __fastcall TCustomScpExplorerForm::DoOperationOnFoundFiles(
  9827. TFileOperation Operation, TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  9828. {
  9829. if (CanOperateOnFoundFiles(ATerminal))
  9830. {
  9831. TValueRestorer<TFileOperationFinishedEvent> OnFileOperationFinishedRestorer(FOnFileOperationFinished);
  9832. FOnFileOperationFinished = OnFileOperationFinished;
  9833. ExecuteFileOperation(Operation, osRemote, FileList, false, NULL);
  9834. }
  9835. }
  9836. //---------------------------------------------------------------------------
  9837. void __fastcall TCustomScpExplorerForm::DoDeleteFoundFiles(
  9838. TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  9839. {
  9840. DoOperationOnFoundFiles(foDelete, ATerminal, FileList, OnFileOperationFinished);
  9841. }
  9842. //---------------------------------------------------------------------------
  9843. void __fastcall TCustomScpExplorerForm::DoDownloadFoundFiles(
  9844. TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  9845. {
  9846. DoOperationOnFoundFiles(foCopy, ATerminal, FileList, OnFileOperationFinished);
  9847. }
  9848. //---------------------------------------------------------------------------
  9849. void __fastcall TCustomScpExplorerForm::DoEditFoundFiles(
  9850. TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  9851. {
  9852. if (CanOperateOnFoundFiles(ATerminal))
  9853. {
  9854. for (int Index = 0; Index < FileList->Count; Index++)
  9855. {
  9856. UnicodeString FileName = FileList->Strings[Index];
  9857. TRemoteFile * File = static_cast<TRemoteFile *>(FileList->Objects[Index]);
  9858. ExecuteRemoteFile(FileName, File, efDefaultEditor);
  9859. OnFileOperationFinished(osRemote, FileName, true);
  9860. }
  9861. }
  9862. }
  9863. //---------------------------------------------------------------------------
  9864. void __fastcall TCustomScpExplorerForm::RemoteFindFiles()
  9865. {
  9866. FFileFindTerminal = Terminal;
  9867. ShowFileFindDialog(
  9868. Terminal, RemoteDirView->Path, DoFindFiles, DoFocusRemotePath, DoDeleteFoundFiles,
  9869. DoDownloadFoundFiles, DoEditFoundFiles);
  9870. }
  9871. //---------------------------------------------------------------------------
  9872. void __fastcall TCustomScpExplorerForm::UpdateTaskbarList(ITaskbarList3 * TaskbarList)
  9873. {
  9874. FTaskbarList = TaskbarList;
  9875. }
  9876. //---------------------------------------------------------------------------
  9877. void __fastcall TCustomScpExplorerForm::SessionsPageControlMouseDown(
  9878. TObject * /*Sender*/, TMouseButton Button, TShiftState /*Shift*/, int X, int Y)
  9879. {
  9880. int Index = SessionsPageControl->IndexOfTabAt(X, Y);
  9881. if (Index >= 0)
  9882. {
  9883. if (Button == mbLeft)
  9884. {
  9885. // WORKAROUND: "Mouse down" is raised only after tab is switched.
  9886. // If switching tab (switching session) takes long enough for user
  9887. // to actually release the button, "mouse down" is still raised,
  9888. // but we do not get "mouse up" event, so dragging is not cancelled,
  9889. // prevent that by not beginning dragging in the first place.
  9890. int Key = ((GetSystemMetrics(SM_SWAPBUTTON) == 0) ? VK_LBUTTON : VK_RBUTTON);
  9891. if (IsKeyPressed(Key))
  9892. {
  9893. // when user clicks the "+", we get mouse down only after the session
  9894. // is closed, when new session tab is already on X:Y, so dragging
  9895. // starts, prevent that
  9896. if (MilliSecondsBetween(Now(), FSessionsPageControlNewTabTime) > 500)
  9897. {
  9898. TTerminal * Terminal = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  9899. if (Terminal != NULL)
  9900. {
  9901. SessionsPageControl->BeginDrag(false);
  9902. }
  9903. }
  9904. }
  9905. }
  9906. else if (Button == mbMiddle)
  9907. {
  9908. // ignore middle-click for "New session tab" and the last local-local tab
  9909. TManagedTerminal * Session = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  9910. if ((Session != NULL) && CanCloseSession(Session))
  9911. {
  9912. CloseSessionTab(Index);
  9913. }
  9914. }
  9915. }
  9916. }
  9917. //---------------------------------------------------------------------------
  9918. void __fastcall TCustomScpExplorerForm::SessionsPageControlDragDrop(
  9919. TObject * /*Sender*/, TObject * /*Source*/, int X, int Y)
  9920. {
  9921. int Index = SessionsPageControl->IndexOfTabAt(X, Y);
  9922. // do not allow dropping on the "+" tab
  9923. TTerminal * TargetTerminal = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  9924. if ((TargetTerminal != NULL) &&
  9925. (SessionsPageControl->ActivePage->PageIndex != Index))
  9926. {
  9927. Configuration->Usage->Inc(L"SessionTabMoves");
  9928. // this is almost redundant as we would recreate tabs in SessionListChanged,
  9929. // but we want to actually prevent that to avoid flicker
  9930. SessionsPageControl->ActivePage->PageIndex = Index;
  9931. TTerminal * Terminal = GetSessionTabSession(SessionsPageControl->ActivePage);
  9932. TTerminalManager::Instance()->Move(Terminal, TargetTerminal);
  9933. }
  9934. }
  9935. //---------------------------------------------------------------------------
  9936. void __fastcall TCustomScpExplorerForm::SessionsPageControlDragOver(
  9937. TObject * Sender, TObject * Source, int X, int Y,
  9938. TDragState /*State*/, bool & Accept)
  9939. {
  9940. Accept = (Sender == Source);
  9941. if (Accept)
  9942. {
  9943. int Index = SessionsPageControl->IndexOfTabAt(X, Y);
  9944. TTerminal * Terminal = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  9945. // do not allow dragging to the "+" tab
  9946. Accept = (Terminal != NULL);
  9947. }
  9948. }
  9949. //---------------------------------------------------------------------------
  9950. void __fastcall TCustomScpExplorerForm::SessionsDDDragOver(
  9951. int DebugUsedArg(KeyState), const TPoint & Point, int & Effect, int DebugUsedArg(PreferredEffect))
  9952. {
  9953. int Index = SessionsPageControl->IndexOfTabAt(Point.X, Point.Y);
  9954. if (Index < 0)
  9955. {
  9956. Effect = DROPEFFECT_NONE;
  9957. }
  9958. else
  9959. {
  9960. TManagedTerminal * TargetSession = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  9961. // do not allow dropping on the "+" tab or on disconnected or local-local tabs
  9962. if (!IsActiveTerminal(TargetSession) || DebugAlwaysFalse(TargetSession->LocalBrowser))
  9963. {
  9964. Effect = DROPEFFECT_NONE;
  9965. }
  9966. else if ((TargetSession != Terminal) && (Effect == DROPEFFECT_MOVE))
  9967. {
  9968. Effect = DROPEFFECT_COPY;
  9969. }
  9970. }
  9971. }
  9972. //---------------------------------------------------------------------------
  9973. void __fastcall TCustomScpExplorerForm::SessionsDDProcessDropped(
  9974. TObject * /*Sender*/, int /*KeyState*/, const TPoint & Point, int Effect)
  9975. {
  9976. int Index = SessionsPageControl->IndexOfTabAt(Point.X, Point.Y);
  9977. // do not allow dropping on local-local, disconnected and the "+" tab
  9978. // (though we do not seem to get here in that case anyway, contrary to RemoteFileControlDDTargetDrop)
  9979. TManagedTerminal * TargetTerminal = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  9980. if (DebugAlwaysTrue(IsActiveTerminal(TargetTerminal) && !TargetTerminal->LocalBrowser))
  9981. {
  9982. DebugAssert(!IsFileControl(DropSourceControl, osRemote));
  9983. if (!IsFileControl(DropSourceControl, osRemote))
  9984. {
  9985. TTerminalManager::Instance()->ActiveSession = TargetTerminal;
  9986. RemoteFileControlDragDropFileOperation(SessionsPageControl, Effect,
  9987. // Why don't we use Terminal->CurrentDirectory directly?
  9988. TTerminalManager::Instance()->ActiveTerminal->CurrentDirectory,
  9989. // do not force queue, drag drop
  9990. false, true);
  9991. }
  9992. }
  9993. }
  9994. //---------------------------------------------------------------------------
  9995. void __fastcall TCustomScpExplorerForm::QueueDDProcessDropped(
  9996. TObject * /*Sender*/, int /*KeyState*/, const TPoint & /*Point*/, int Effect)
  9997. {
  9998. // Downloads are handled in RemoteFileControlDDEnd
  9999. if (!IsFileControl(DropSourceControl, osRemote))
  10000. {
  10001. RemoteFileControlDragDropFileOperation(QueueView3, Effect,
  10002. Terminal->CurrentDirectory,
  10003. // force queue, drag drop
  10004. true, true);
  10005. }
  10006. }
  10007. //---------------------------------------------------------------------------
  10008. void __fastcall TCustomScpExplorerForm::FormClose(TObject * /*Sender*/, TCloseAction & /*Action*/)
  10009. {
  10010. FShowing = false;
  10011. // Do not save empty workspace
  10012. if (WinConfiguration->AutoSaveWorkspace && (ManagedSession != NULL))
  10013. {
  10014. std::unique_ptr<TObjectList> DataList(DoCollectWorkspace());
  10015. UnicodeString Name = WorkspaceName();
  10016. DoSaveWorkspace(
  10017. Name, DataList.get(),
  10018. !Configuration->DisablePasswordStoring &&
  10019. WinConfiguration->AutoSaveWorkspacePasswords, false);
  10020. WinConfiguration->LastStoredSession = Name;
  10021. }
  10022. }
  10023. //---------------------------------------------------------------------------
  10024. void __fastcall TCustomScpExplorerForm::RemoteDirViewRead(TObject * /*Sender*/)
  10025. {
  10026. TManagedTerminal * ManagedTerminal =
  10027. dynamic_cast<TManagedTerminal *>(RemoteDirView->Terminal);
  10028. if (ManagedTerminal != NULL)
  10029. {
  10030. ManagedTerminal->DirectoryLoaded = Now();
  10031. }
  10032. }
  10033. //---------------------------------------------------------------------------
  10034. void __fastcall TCustomScpExplorerForm::DirViewSelectItem(TObject * Sender,
  10035. TListItem * /*Item*/, bool /*Selected*/)
  10036. {
  10037. TCustomDirView * DirView = reinterpret_cast<TCustomDirView *>(Sender);
  10038. switch (DirView->LastSelectMethod)
  10039. {
  10040. case smKeyboard:
  10041. Configuration->Usage->Inc(L"KeyboardSelections");
  10042. break;
  10043. case smMouse:
  10044. Configuration->Usage->Inc(L"MouseSelections");
  10045. break;
  10046. }
  10047. }
  10048. //---------------------------------------------------------------------------
  10049. int __fastcall TCustomScpExplorerForm::AddSessionColor(TColor Color)
  10050. {
  10051. if (Color != 0)
  10052. {
  10053. return GetSessionColorImage(FSessionColors, Color, FSessionColorMaskImageIndex);
  10054. }
  10055. else
  10056. {
  10057. return FSessionTabImageIndex;
  10058. }
  10059. }
  10060. //---------------------------------------------------------------------------
  10061. int __fastcall TCustomScpExplorerForm::AddFixedSessionImage(int GlyphsSourceIndex)
  10062. {
  10063. TPngImageCollectionItem * Item =
  10064. GlyphsModule->ExplorerImages->PngImages->Items[GlyphsSourceIndex];
  10065. FSessionColors->AddPng(Item->PngImage);
  10066. return FSessionColors->Count - 1;
  10067. }
  10068. //---------------------------------------------------------------------------
  10069. void __fastcall TCustomScpExplorerForm::AddFixedSessionImages()
  10070. {
  10071. FSessionColors->SetSize(GlyphsModule->ExplorerImages->Width, GlyphsModule->ExplorerImages->Height);
  10072. FNewRemoteTabTabImageIndex = AddFixedSessionImage(NonVisualDataModule->NewRemoteTabAction->ImageIndex);
  10073. FNewLocalTabTabImageIndex = AddFixedSessionImage(NonVisualDataModule->NewLocalTabAction->ImageIndex);
  10074. FSessionTabImageIndex = AddFixedSessionImage(SiteImageIndex);
  10075. FSessionColorMaskImageIndex = AddFixedSessionImage(SiteColorMaskImageIndex);
  10076. FLocalBrowserTabImageIndex = AddFixedSessionImage(LocalBrowserImageIndex);
  10077. }
  10078. //---------------------------------------------------------------------------
  10079. int TCustomScpExplorerForm::GetNewTabActionImageIndex()
  10080. {
  10081. return NonVisualDataModule->NewRemoteTabAction->ImageIndex;
  10082. }
  10083. //---------------------------------------------------------------------------
  10084. int TCustomScpExplorerForm::GetNewTabTabImageIndex(TOperationSide Side)
  10085. {
  10086. switch (Side)
  10087. {
  10088. case osLocal:
  10089. return FNewLocalTabTabImageIndex;
  10090. case osRemote:
  10091. case osCurrent:
  10092. return FNewRemoteTabTabImageIndex;
  10093. default:
  10094. DebugFail();
  10095. return -1;
  10096. }
  10097. }
  10098. //---------------------------------------------------------------------------
  10099. void __fastcall TCustomScpExplorerForm::CollectItemsWithTextDisplayMode(TWinControl * Control)
  10100. {
  10101. for (int Index = 0; Index < Control->ControlCount; Index++)
  10102. {
  10103. TControl * AControl = Control->Controls[Index];
  10104. TWinControl * WinControl = dynamic_cast<TWinControl *>(AControl);
  10105. if (WinControl != NULL)
  10106. {
  10107. CollectItemsWithTextDisplayMode(WinControl);
  10108. }
  10109. TTBCustomToolbar * Toolbar = dynamic_cast<TTBCustomToolbar *>(AControl);
  10110. if (Toolbar != NULL)
  10111. {
  10112. // we care for top-level items only
  10113. TTBCustomItem * Items = Toolbar->Items;
  10114. for (int ItemIndex = 0; ItemIndex < Items->Count; ItemIndex++)
  10115. {
  10116. TTBCustomItem * Item = Items->Items[ItemIndex];
  10117. if (((Item->DisplayMode == nbdmImageAndText) ||
  10118. (dynamic_cast<TTBXLabelItem *>(Item) != NULL)) &&
  10119. EligibleForImageDisplayMode(Item))
  10120. {
  10121. FItemsWithTextDisplayMode.insert(Item);
  10122. }
  10123. }
  10124. }
  10125. }
  10126. }
  10127. //---------------------------------------------------------------------------
  10128. bool __fastcall TCustomScpExplorerForm::EligibleForImageDisplayMode(TTBCustomItem * /*Item*/)
  10129. {
  10130. return true;
  10131. }
  10132. //---------------------------------------------------------------------------
  10133. bool __fastcall TCustomScpExplorerForm::UpdateToolbarDisplayMode()
  10134. {
  10135. bool SelectiveToolbarText = WinConfiguration->SelectiveToolbarText;
  10136. TTBItemDisplayMode DisplayMode = (SelectiveToolbarText ? nbdmImageAndText : nbdmDefault);
  10137. typedef std::set<TTBCustomItem *> TItemsWithTextDisplayMode;
  10138. TItemsWithTextDisplayMode::iterator i = FItemsWithTextDisplayMode.begin();
  10139. bool Result = true;
  10140. while (Result && (i != FItemsWithTextDisplayMode.end()))
  10141. {
  10142. TTBCustomItem * Item = *i;
  10143. TTBXLabelItem * Label = dynamic_cast<TTBXLabelItem *>(Item);
  10144. if (Label != NULL)
  10145. {
  10146. // optimization
  10147. if (Label->Visible == SelectiveToolbarText)
  10148. {
  10149. Result = false;
  10150. }
  10151. else
  10152. {
  10153. Label->Visible = SelectiveToolbarText;
  10154. }
  10155. }
  10156. else
  10157. {
  10158. // optimization
  10159. if (Item->DisplayMode == DisplayMode)
  10160. {
  10161. Result = false;
  10162. }
  10163. else
  10164. {
  10165. Item->DisplayMode = DisplayMode;
  10166. }
  10167. }
  10168. ++i;
  10169. }
  10170. UpdateNewTabTab();
  10171. return Result;
  10172. }
  10173. //---------------------------------------------------------------------------
  10174. void __fastcall TCustomScpExplorerForm::DisplaySystemContextMenu()
  10175. {
  10176. DebugFail();
  10177. }
  10178. //---------------------------------------------------------------------------
  10179. bool __fastcall TCustomScpExplorerForm::IsBusy()
  10180. {
  10181. // Among other, a lock level is non zero, while directory is being loaded,
  10182. // even when it happens as a result of dirview navigation,
  10183. // i.e. when TNonVisualDataModule is NOT FBusy.
  10184. // That's why the TNonVisualDataModule::GetBusy calls this method.
  10185. // Among other this prevents a panel auto update to occur while
  10186. // directory is changing.
  10187. return (FLockLevel > 0) || DirView(osCurrent)->IsEditing();
  10188. }
  10189. //---------------------------------------------------------------------------
  10190. Boolean __fastcall TCustomScpExplorerForm::AllowedAction(TAction * Action, TActionAllowed Allowed)
  10191. {
  10192. // While the window is disabled, we still seem to process menu shortcuts at least,
  10193. // so stop it at least here.
  10194. // See also TCustomScpExplorerForm::RemoteDirViewBusy
  10195. bool Result;
  10196. if (Allowed == aaUpdate)
  10197. {
  10198. Result = true;
  10199. }
  10200. else
  10201. {
  10202. Result = !NonVisualDataModule->Busy;
  10203. if (Result && (Allowed == aaShortCut))
  10204. {
  10205. // Otherwise F5 for local-local will be swallowed by disabled local<>remote actions in TCustomScpExplorerForm::KeyDown
  10206. Action->Update();
  10207. Result = Action->Enabled;
  10208. }
  10209. }
  10210. return Result;
  10211. }
  10212. //---------------------------------------------------------------------------
  10213. void __fastcall TCustomScpExplorerForm::EditMenuItemPopup(TTBCustomItem * Sender, bool FromLink)
  10214. {
  10215. NonVisualDataModule->EditMenuItemPopup(Sender, FromLink);
  10216. }
  10217. //---------------------------------------------------------------------------
  10218. void __fastcall TCustomScpExplorerForm::DirViewBusy(TObject * /*Sender*/, int Busy, bool & State)
  10219. {
  10220. // This is somewhat redundant to LockWindow() call from
  10221. // TTerminalManager::TerminalReadDirectoryProgress.
  10222. // But disabling window is known not to block keyboard shorcuts
  10223. // (see TCustomScpExplorerForm::AllowedAction), this hopefully works.
  10224. if (Busy > 0)
  10225. {
  10226. if (NonVisualDataModule->Busy)
  10227. {
  10228. State = false;
  10229. }
  10230. else
  10231. {
  10232. NonVisualDataModule->StartBusy();
  10233. // Will be countered for TCustomDirView.DoExecute in DoDirViewExecFile
  10234. LockWindow();
  10235. }
  10236. }
  10237. else if (Busy < 0)
  10238. {
  10239. UnlockWindow();
  10240. NonVisualDataModule->EndBusy();
  10241. }
  10242. else
  10243. {
  10244. State = NonVisualDataModule->Busy;
  10245. }
  10246. }
  10247. //---------------------------------------------------------------------------
  10248. void __fastcall TCustomScpExplorerForm::SessionsPageControlContextPopup(TObject * /*Sender*/, TPoint & MousePos, bool & Handled)
  10249. {
  10250. int Index = SessionsPageControl->IndexOfTabAt(MousePos.X, MousePos.Y);
  10251. if (Index >= 0)
  10252. {
  10253. TPopupMenu * PopupMenu = NULL;
  10254. TManagedTerminal * Session = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  10255. if (Session != NULL)
  10256. {
  10257. SessionsPageControl->ActivePageIndex = Index;
  10258. if (DebugAlwaysTrue(SessionTabSwitched()))
  10259. {
  10260. // copied from TControl.WMContextMenu
  10261. SendCancelMode(SessionsPageControl);
  10262. // explicit popup instead of using PopupMenu property
  10263. // to avoid menu to popup somewhere within SessionTabSwitched above,
  10264. // while connecting yet not-connected session and hence
  10265. // allowing an access to commands over not-completely connected session
  10266. PopupMenu = Session->LocalBrowser ? NonVisualDataModule->LocalBrowserPopup : NonVisualDataModule->SessionsPopup;
  10267. }
  10268. }
  10269. else
  10270. {
  10271. if (SupportsLocalBrowser())
  10272. {
  10273. PopupMenu = NonVisualDataModule->NewTabPopup;
  10274. }
  10275. }
  10276. if (PopupMenu != NULL)
  10277. {
  10278. TPoint Point = SessionsPageControl->ClientToScreen(MousePos);
  10279. PopupMenu->PopupComponent = SessionsPageControl;
  10280. PopupMenu->Popup(Point.x, Point.y);
  10281. }
  10282. }
  10283. Handled = true;
  10284. }
  10285. //---------------------------------------------------------------------------
  10286. bool __fastcall TCustomScpExplorerForm::CanChangePassword()
  10287. {
  10288. return HasActiveTerminal() && Terminal->IsCapable[fcChangePassword];
  10289. }
  10290. //---------------------------------------------------------------------------
  10291. void __fastcall TCustomScpExplorerForm::ChangePassword()
  10292. {
  10293. Configuration->Usage->Inc(L"PasswordChanges");
  10294. std::unique_ptr<TSessionData> ChangePasswordData(Terminal->SessionData->Clone());
  10295. ChangePasswordData->DisableAuthentationsExceptPassword();
  10296. ChangePasswordData->ChangePassword = true;
  10297. std::unique_ptr<TTerminal> ChangePasswordSession(
  10298. Terminal->CreateSecondarySession(L"ChangePassword", ChangePasswordData.get()));
  10299. ChangePasswordSession->AutoReadDirectory = false;
  10300. if (TTerminalManager::Instance()->ConnectTerminal(ChangePasswordSession.get()))
  10301. {
  10302. MessageDialog(MainInstructions(LoadStr(PASSWORD_CHANGED)), qtInformation, qaOK, HELP_CHANGE_PASSWORD);
  10303. }
  10304. else
  10305. {
  10306. Configuration->Usage->Inc(L"PasswordChangesFailed");
  10307. }
  10308. }
  10309. //---------------------------------------------------------------------------
  10310. bool __fastcall TCustomScpExplorerForm::CanPrivateKeyUpload()
  10311. {
  10312. // No nice way to assert SSH2
  10313. return HasActiveTerminal() && (Terminal->FSProtocol == cfsSFTP);
  10314. }
  10315. //---------------------------------------------------------------------------
  10316. void __fastcall TCustomScpExplorerForm::PrivateKeyUpload()
  10317. {
  10318. TOperationVisualizer OperationVisualizer;
  10319. UnicodeString FileName = Terminal->SessionData->PublicKeyFile;
  10320. TTerminalManager::Instance()->UploadPublicKey(Terminal, NULL, FileName);
  10321. }
  10322. //---------------------------------------------------------------------------
  10323. void __fastcall TCustomScpExplorerForm::ChangeScale(int M, int D)
  10324. {
  10325. TForm::ChangeScale(M, D);
  10326. int APixelsPerInch = GetControlPixelsPerInch(this);
  10327. HANDLE ResourceModule = GUIConfiguration->ChangeToDefaultResourceModule();
  10328. try
  10329. {
  10330. GlyphsModule->SetPixelsPerInch(APixelsPerInch);
  10331. }
  10332. __finally
  10333. {
  10334. GUIConfiguration->ChangeResourceModule(ResourceModule);
  10335. }
  10336. }
  10337. //---------------------------------------------------------------------------
  10338. void __fastcall TCustomScpExplorerForm::DockContextPopup(TObject * Sender, TPoint & MousePos, bool & /*Handled*/)
  10339. {
  10340. NonVisualDataModule->ControlContextPopup(Sender, MousePos);
  10341. }
  10342. //---------------------------------------------------------------------
  10343. class TPublicControl : public TControl
  10344. {
  10345. friend class TCustomScpExplorerForm;
  10346. };
  10347. //---------------------------------------------------------------------------
  10348. void __fastcall TCustomScpExplorerForm::CopyPopup(TControl * DestControl, TControl * SourceControl)
  10349. {
  10350. static_cast<TPublicControl *>(DestControl)->PopupMenu = static_cast<TPublicControl *>(SourceControl)->PopupMenu;
  10351. static_cast<TPublicControl *>(DestControl)->OnContextPopup = static_cast<TPublicControl *>(SourceControl)->OnContextPopup;
  10352. }
  10353. //---------------------------------------------------------------------------
  10354. void __fastcall TCustomScpExplorerForm::DoBookmarkClick(TOperationSide Side, TObject * Sender)
  10355. {
  10356. TBookmark * Bookmark = DebugNotNull(reinterpret_cast<TBookmark *>(dynamic_cast<TTBCustomItem *>(Sender)->Tag));
  10357. OpenBookmark(Side, Bookmark);
  10358. }
  10359. //---------------------------------------------------------------------------
  10360. void __fastcall TCustomScpExplorerForm::LocalBookmarkClick(TObject * Sender)
  10361. {
  10362. DoBookmarkClick(osLocal, Sender);
  10363. }
  10364. //---------------------------------------------------------------------------
  10365. void __fastcall TCustomScpExplorerForm::RemoteBookmarkClick(TObject * Sender)
  10366. {
  10367. DoBookmarkClick(osRemote, Sender);
  10368. }
  10369. //---------------------------------------------------------------------------
  10370. void __fastcall TCustomScpExplorerForm::CreateOpenDirMenuList(
  10371. TTBCustomItem * Menu, TOperationSide Side, TBookmarkList * BookmarkList)
  10372. {
  10373. if (BookmarkList != NULL)
  10374. {
  10375. TNotifyEvent OnBookmarkClick = (Side == osLocal) ? &LocalBookmarkClick : &RemoteBookmarkClick;
  10376. if (IsLocalBrowserMode())
  10377. {
  10378. Side = osLocal;
  10379. }
  10380. if (!WinConfiguration->UseLocationProfiles)
  10381. {
  10382. std::unique_ptr<TStringList> Directories(new TStringList());
  10383. Directories->CaseSensitive = (Side == osRemote);
  10384. for (int Index = 0; Index < BookmarkList->Count; Index++)
  10385. {
  10386. TBookmark * Bookmark = BookmarkList->Bookmarks[Index];
  10387. UnicodeString Directory = Bookmark->GetSideDirectory(Side);
  10388. if (!Directory.IsEmpty() && (Directories->IndexOf(Directory) < 0))
  10389. {
  10390. std::unique_ptr<TTBCustomItem> Item(new TTBXItem(Owner));
  10391. Item->Caption = EscapeHotkey(Directory);
  10392. Item->ShortCut = Bookmark->ShortCut;
  10393. Item->OnClick = OnBookmarkClick;
  10394. Item->Tag = reinterpret_cast<int>(Bookmark);
  10395. Directories->Add(Directory);
  10396. Menu->Add(Item.release());
  10397. }
  10398. }
  10399. }
  10400. else
  10401. {
  10402. std::unique_ptr<TStrings> Folders(CreateSortedStringList());
  10403. for (int Index = 0; Index < BookmarkList->Count; Index++)
  10404. {
  10405. TBookmark * Bookmark = BookmarkList->Bookmarks[Index];
  10406. if (!Bookmark->Node.IsEmpty())
  10407. {
  10408. Folders->Add(Bookmark->Node);
  10409. }
  10410. }
  10411. for (int Index = 0; Index < Folders->Count; Index++)
  10412. {
  10413. std::unique_ptr<TTBCustomItem> Item(new TTBXSubmenuItem(Owner));
  10414. Item->Caption = Folders->Strings[Index];
  10415. Item->ImageIndex = NonVisualDataModule->RemoteChangePathAction2->ImageIndex;
  10416. Folders->Objects[Index] = Item.get();
  10417. Menu->Add(Item.release());
  10418. }
  10419. for (int Index = 0; Index < BookmarkList->Count; Index++)
  10420. {
  10421. TBookmark * Bookmark = BookmarkList->Bookmarks[Index];
  10422. TTBCustomItem * Parent;
  10423. if (!Bookmark->Node.IsEmpty())
  10424. {
  10425. DebugAssert(Folders->IndexOf(Bookmark->Node) >= 0);
  10426. Parent = dynamic_cast<TTBCustomItem *>(Folders->Objects[Folders->IndexOf(Bookmark->Node)]);
  10427. }
  10428. else
  10429. {
  10430. Parent = Menu;
  10431. }
  10432. std::unique_ptr<TTBCustomItem> Item(new TTBXItem(Owner));
  10433. Item->Caption = Bookmark->Name;
  10434. Item->ShortCut = Bookmark->ShortCut;
  10435. Item->OnClick = OnBookmarkClick;
  10436. Item->Tag = reinterpret_cast<int>(Bookmark);
  10437. if (((Bookmark->Name == Bookmark->Local) || (Bookmark->Name == Bookmark->Remote)) &&
  10438. (Bookmark->Local.IsEmpty() || Bookmark->Remote.IsEmpty()))
  10439. {
  10440. // No hint for location profiles that are actually mere "bookmarks"
  10441. }
  10442. else
  10443. {
  10444. UnicodeString Hint = FORMAT(LoadStrPart(LOCATION_PROFILE_HINT, 1), (Bookmark->Name));
  10445. UnicodeString LongHint;
  10446. if (!Bookmark->Local.IsEmpty())
  10447. {
  10448. AddToList(LongHint, FORMAT(LoadStrPart(LOCATION_PROFILE_HINT, 2), (Bookmark->Local)), L"\n");
  10449. }
  10450. if (!Bookmark->Remote.IsEmpty())
  10451. {
  10452. AddToList(LongHint, FORMAT(LoadStrPart(LOCATION_PROFILE_HINT, 3), (Bookmark->Remote)), L"\n");
  10453. }
  10454. AddToList(Hint, LongHint, L"|");
  10455. Item->Hint = Hint;
  10456. }
  10457. Parent->Add(Item.release());
  10458. }
  10459. }
  10460. AddMenuSeparator(Menu);
  10461. }
  10462. }
  10463. //---------------------------------------------------------------------------
  10464. void __fastcall TCustomScpExplorerForm::CreateOpenDirMenu(TTBCustomItem * Menu, TOperationSide Side)
  10465. {
  10466. Menu->Clear();
  10467. std::unique_ptr<TTBCustomItem> Item;
  10468. Item.reset(new TTBXItem(Owner));
  10469. Item->Action = (Side == osLocal) ? NonVisualDataModule->LocalOpenDirAction : NonVisualDataModule->RemoteOpenDirAction;
  10470. Item->Options = Item->Options << tboDefault;
  10471. Menu->Add(Item.release());
  10472. AddMenuSeparator(Menu);
  10473. if (Terminal != NULL)
  10474. {
  10475. CreateOpenDirMenuList(Menu, Side, WinConfiguration->Bookmarks[Terminal->SessionData->SessionKey]);
  10476. }
  10477. CreateOpenDirMenuList(Menu, Side, WinConfiguration->SharedBookmarks);
  10478. Item.reset(new TTBXItem(Owner));
  10479. Item->Action = (Side == osLocal) ? NonVisualDataModule->LocalAddBookmarkAction2 : NonVisualDataModule->RemoteAddBookmarkAction2;
  10480. Menu->Add(Item.release());
  10481. }
  10482. //---------------------------------------------------------------------------
  10483. bool __fastcall TCustomScpExplorerForm::TryOpenDirectory(TOperationSide Side, const UnicodeString & Path)
  10484. {
  10485. bool Result = true;
  10486. bool Remote = !IsSideLocalBrowser(Side);
  10487. try
  10488. {
  10489. UnicodeString APath = Path;
  10490. if (Remote)
  10491. {
  10492. Terminal->ExceptionOnFail = true;
  10493. }
  10494. else
  10495. {
  10496. APath = ExpandEnvironmentVariables(Path);
  10497. }
  10498. try
  10499. {
  10500. DirView(Side)->Path = APath;
  10501. }
  10502. __finally
  10503. {
  10504. if (Remote)
  10505. {
  10506. Terminal->ExceptionOnFail = false;
  10507. }
  10508. }
  10509. }
  10510. catch (Exception & E)
  10511. {
  10512. if (!Remote || Terminal->Active)
  10513. {
  10514. ShowExtendedExceptionEx(Terminal, &E);
  10515. Result = false;
  10516. }
  10517. else
  10518. {
  10519. throw;
  10520. }
  10521. }
  10522. return Result;
  10523. }
  10524. //---------------------------------------------------------------------------
  10525. void __fastcall TCustomScpExplorerForm::ReloadDirectory(TOperationSide Side)
  10526. {
  10527. // Make sure there some feedback even when loading tiny local folders
  10528. TInstantOperationVisualizer Visualizer;
  10529. DirView(Side)->ReloadDirectory();
  10530. }
  10531. //---------------------------------------------------------------------------
  10532. void __fastcall TCustomScpExplorerForm::CloseSessionTab(int Index)
  10533. {
  10534. if (Index == SessionsPageControl->TabIndex)
  10535. {
  10536. CloseTab();
  10537. }
  10538. else
  10539. {
  10540. TTabSheet * TabSheet = SessionsPageControl->Pages[Index];
  10541. TTerminal * Terminal = GetSessionTabSession(TabSheet);
  10542. TTerminalManager * Manager = TTerminalManager::Instance();
  10543. if (!Terminal->Active)
  10544. {
  10545. Manager->FreeTerminal(Terminal);
  10546. }
  10547. else
  10548. {
  10549. TTerminalQueue * Queue = Manager->FindQueueForTerminal(Terminal);
  10550. if (CanCloseQueue(Queue))
  10551. {
  10552. Manager->FreeTerminal(Terminal);
  10553. }
  10554. }
  10555. }
  10556. }
  10557. //---------------------------------------------------------------------------
  10558. void __fastcall TCustomScpExplorerForm::SessionsPageControlTabButtonClick(TPageControl *, int Index)
  10559. {
  10560. TManagedTerminal * Session = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  10561. if (Session == NULL)
  10562. {
  10563. TRect ButtonRect = SessionsPageControl->TabButtonRect(Index);
  10564. ButtonRect = TRect(SessionsPageControl->ClientToScreen(ButtonRect.TopLeft()), SessionsPageControl->ClientToScreen(ButtonRect.BottomRight()));
  10565. //TPoint P = TPoint(ButtonRect.Left, ButtonRect.Bottom);
  10566. //P = SessionsPageControl->ClientToScreen(P);
  10567. MenuPopup(NonVisualDataModule->NewTabPopup, ButtonRect, SessionsPageControl);
  10568. }
  10569. else
  10570. {
  10571. CloseSessionTab(Index);
  10572. }
  10573. }
  10574. //---------------------------------------------------------------------------
  10575. void __fastcall TCustomScpExplorerForm::CopyFilesToClipboard(TOperationSide Side, bool OnFocused)
  10576. {
  10577. if (DebugAlwaysTrue(!IsSideLocalBrowser(Side)))
  10578. {
  10579. TInstantOperationVisualizer Visualizer;
  10580. // To trigger ClipboardDataObjectRelease (in case the clipboard already contains our data)
  10581. // before we create a new data object
  10582. ClipboardClear();
  10583. DebugAssert(FClipboardFakeDirectory.IsEmpty());
  10584. DebugAssert(FClipboardFakeMonitors.get() == NULL);
  10585. FClipboardFakeDirectory = CreateFakeTransferDirectory();
  10586. // Intermediate variable to WORKAROUND a crash in compiler
  10587. TObjectList * ClipboardFakeMonitors =
  10588. StartCreationDirectoryMonitorsOnEachDrive(FILE_NOTIFY_CHANGE_DIR_NAME, ClipboardFakeCreated);
  10589. FClipboardFakeMonitors.reset(ClipboardFakeMonitors);
  10590. if (FClipboardDragDropFilesEx.get() == NULL)
  10591. {
  10592. FClipboardDragDropFilesEx.reset(new TDragDropFilesEx(this));
  10593. FClipboardDragDropFilesEx->OnDataObjectRelease = ClipboardDataObjectRelease;
  10594. }
  10595. FClipboardDragDropFilesEx->FileList->Clear();
  10596. FClipboardDragDropFilesEx->FileList->AddItem(NULL, FClipboardFakeDirectory);
  10597. FClipboardDragDropFilesEx->CopyToClipboard();
  10598. FClipboardTerminal = Terminal;
  10599. // Need full paths, as cwd can be different once files are pasted
  10600. FClipboardFileList.reset(TRemoteFileList::CloneStrings(RemoteDirView->CreateFileList(OnFocused, true)));
  10601. for (int Index = 0; Index < FClipboardFileList->Count; Index++)
  10602. {
  10603. FClipboardFileList->Strings[Index] = UnixExcludeTrailingBackslash(FClipboardFileList->Strings[Index]);
  10604. }
  10605. }
  10606. }
  10607. //---------------------------------------------------------------------------
  10608. void __fastcall TCustomScpExplorerForm::ClipboardClear()
  10609. {
  10610. if (OpenClipboard(0))
  10611. {
  10612. EmptyClipboard(); // Calls ClipboardDataObjectRelease, if clipboard contains our data
  10613. CloseClipboard();
  10614. }
  10615. }
  10616. //---------------------------------------------------------------------------
  10617. bool __fastcall TCustomScpExplorerForm::DoesClipboardContainOurFiles()
  10618. {
  10619. return !FClipboardFakeDirectory.IsEmpty();
  10620. }
  10621. //---------------------------------------------------------------------------
  10622. void __fastcall TCustomScpExplorerForm::ClipboardStop()
  10623. {
  10624. RemoveDir(ApiPath(FClipboardFakeDirectory));
  10625. FClipboardFakeDirectory = UnicodeString();
  10626. FClipboardTerminal = NULL;
  10627. if (FDownloadingFromClipboard)
  10628. {
  10629. // We are called by the monitor, so attempt to release it would deadlock
  10630. FClipboardFakeMonitorsPendingReset = true;
  10631. }
  10632. else
  10633. {
  10634. FClipboardFakeMonitors.reset(NULL);
  10635. }
  10636. }
  10637. //---------------------------------------------------------------------------
  10638. void __fastcall TCustomScpExplorerForm::ClipboardDataObjectRelease(TObject * /*Sender*/)
  10639. {
  10640. if (DebugAlwaysTrue(!FClipboardFakeDirectory.IsEmpty()))
  10641. {
  10642. ClipboardStop();
  10643. }
  10644. }
  10645. //---------------------------------------------------------------------------
  10646. void __fastcall TCustomScpExplorerForm::ClipboardDownload(const UnicodeString & TargetDirectory, bool NoConfirmation, bool DragDrop)
  10647. {
  10648. TTerminalManager * Manager = TTerminalManager::Instance();
  10649. Manager->ActiveSession = FClipboardTerminal;
  10650. TTransferOperationParam Params;
  10651. Params.TargetDirectory = TargetDirectory;
  10652. Params.DragDrop = DragDrop;
  10653. ExecuteFileOperation(foCopy, osRemote, FClipboardFileList.get(), NoConfirmation, &Params);
  10654. }
  10655. //---------------------------------------------------------------------------
  10656. void TCustomScpExplorerForm::PasteFiles()
  10657. {
  10658. // Guard against possible race conditions
  10659. if (!FClipboardPasteTarget.IsEmpty())
  10660. {
  10661. // ClipboardFakeCreated might be called while we are here, so make sure we finish operating on the same path we started with
  10662. UnicodeString Target = FClipboardPasteTarget;
  10663. FClipboardPasteTarget = EmptyStr;
  10664. // Can fail as it can be e.g. locked by AV, so we retry that later
  10665. bool Removed = RemoveDir(ApiPath(Target));
  10666. try
  10667. {
  10668. if (!NonVisualDataModule->Busy)
  10669. {
  10670. DebugAssert(!FClipboardFakeMonitorsPendingReset);
  10671. FClipboardFakeMonitorsPendingReset = false;
  10672. bool NoConfirmation = (WinConfiguration->DDTransferConfirmation == asOff);
  10673. TAutoFlag Flag(FDownloadingFromClipboard);
  10674. ClipboardDownload(ExtractFilePath(Target), NoConfirmation, true);
  10675. }
  10676. }
  10677. __finally
  10678. {
  10679. if (!Removed)
  10680. {
  10681. RemoveDir(ApiPath(Target));
  10682. }
  10683. }
  10684. }
  10685. }
  10686. //---------------------------------------------------------------------------
  10687. void __fastcall TCustomScpExplorerForm::ClipboardFakeCreated(TObject * /*Sender*/, const UnicodeString FileName)
  10688. {
  10689. // It can actually rarelly happen that some random file is created, while we are shutting down the monitor
  10690. // (as it pumps a Windows message queue while being shutted down)
  10691. if (DebugAlwaysTrue(!FClipboardFakeDirectory.IsEmpty()) &&
  10692. SameText(ExtractFileName(FileName), ExtractFileName(FClipboardFakeDirectory)))
  10693. {
  10694. AppLogFmt(L"Fake clipboard directory pasted to \"%s\"", (FileName));
  10695. FClipboardPasteTarget = FileName;
  10696. // Particularly when pasting to external HDD, the fake directory is strangely recreated if removed here in the monitor callback.
  10697. // Postponing removing (or actually whole pasting operation) seems to help.
  10698. PostMessage(Handle, WM_PASTE_FILES, 0, 0);
  10699. }
  10700. }
  10701. //---------------------------------------------------------------------------
  10702. void __fastcall TCustomScpExplorerForm::DirViewGetItemColor(
  10703. TObject * Sender, UnicodeString FileName, bool Directory, __int64 Size, TDateTime Modification, TColor & Color)
  10704. {
  10705. TFileMasks::TParams MaskParams;
  10706. MaskParams.Size = Size;
  10707. MaskParams.Modification = Modification;
  10708. TCustomDirView * DirView = DebugNotNull(dynamic_cast<TCustomDirView *>(Sender));
  10709. bool Local = (dynamic_cast<TUnixDirView *>(DirView) == NULL);
  10710. // TUnixDirView::ItemFullFileName returns trailing backslash for directories, what filemask cannot deal with
  10711. if (!Local && Directory)
  10712. {
  10713. FileName = UnixExcludeTrailingBackslash(FileName);
  10714. }
  10715. for (TFileColorData::TList::const_iterator Iter = FFileColors.begin(); Iter != FFileColors.end(); Iter++)
  10716. {
  10717. bool ImplicitMatch;
  10718. if (Iter->FileMask.Matches(FileName, Local, Directory, &MaskParams, false, ImplicitMatch) &&
  10719. !ImplicitMatch)
  10720. {
  10721. Color = Iter->Color;
  10722. break;
  10723. }
  10724. }
  10725. }
  10726. //---------------------------------------------------------------------------
  10727. void __fastcall TCustomScpExplorerForm::DirViewKeyDown(TObject * Sender, WORD & Key, TShiftState)
  10728. {
  10729. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  10730. if (!DirView->IsEditing())
  10731. {
  10732. // Handled here, instead of RemoteDirViewKeyPress (as on Login dialog),
  10733. // as VK_BACK is handled by TCustomDirView.KeyDown and we can swallow it here,
  10734. // while searching
  10735. if (Key == VK_BACK)
  10736. {
  10737. if (!FIncrementalSearch.IsEmpty())
  10738. {
  10739. if (FIncrementalSearch.Length() == 1)
  10740. {
  10741. ResetIncrementalSearch();
  10742. }
  10743. else
  10744. {
  10745. UnicodeString NewText = FIncrementalSearch.SubString(1, FIncrementalSearch.Length() - 1);
  10746. IncrementalSearch(NewText, false, false);
  10747. }
  10748. Key = 0;
  10749. }
  10750. }
  10751. }
  10752. }
  10753. //---------------------------------------------------------------------------
  10754. void __fastcall TCustomScpExplorerForm::DirViewKeyPress(TObject * Sender, wchar_t & Key)
  10755. {
  10756. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  10757. if (!DirView->IsEditing() && (WinConfiguration->PanelSearch != isOff))
  10758. {
  10759. // Filter control sequences.
  10760. // When not searching yet, prefer use of the space for toggling file selection
  10761. // (so we cannot incrementally search for a string starting with a space).
  10762. if ((Key > VK_SPACE) ||
  10763. ((Key == VK_SPACE) && (GetKeyState(VK_CONTROL) >= 0) && !FIncrementalSearch.IsEmpty()))
  10764. {
  10765. IncrementalSearch(FIncrementalSearch + Key, false, false);
  10766. Key = 0;
  10767. }
  10768. }
  10769. }
  10770. //---------------------------------------------------------------------------
  10771. void __fastcall TCustomScpExplorerForm::ResetIncrementalSearch()
  10772. {
  10773. if (!FIncrementalSearch.IsEmpty())
  10774. {
  10775. FIncrementalSearch = L"";
  10776. DirView(osCurrent)->UpdateStatusBar();
  10777. }
  10778. }
  10779. //---------------------------------------------------------------------------
  10780. void __fastcall TCustomScpExplorerForm::IncrementalSearch(const UnicodeString & Text, bool SkipCurrent, bool Reverse)
  10781. {
  10782. TListItem * Item = SearchFile(Text, SkipCurrent, Reverse);
  10783. if (Item == NULL)
  10784. {
  10785. MessageBeep(MB_ICONHAND);
  10786. }
  10787. else
  10788. {
  10789. TCustomDirView * ADirView = DirView(osCurrent);
  10790. {
  10791. TAutoNestingCounter Guard(FIncrementalSearching);
  10792. ADirView->FocusItem(Item);
  10793. if (ADirView->NortonLike == nlOff)
  10794. {
  10795. if ((ADirView->Selected != Item) ||
  10796. (ADirView->SelCount > 1))
  10797. {
  10798. ADirView->ClearSelection();
  10799. ADirView->Selected = Item;
  10800. }
  10801. }
  10802. }
  10803. FIncrementalSearch = Text;
  10804. Item->MakeVisible(false);
  10805. TListItem * NextItem = SearchFile(Text, true, Reverse);
  10806. FIncrementalSearchHaveNext = (NextItem != NULL) && (NextItem != Item);
  10807. ADirView->UpdateStatusBar();
  10808. }
  10809. }
  10810. //---------------------------------------------------------------------------
  10811. TListItem * __fastcall TCustomScpExplorerForm::GetNextFile(TListItem * Item, bool Reverse)
  10812. {
  10813. int Index = Item->Index;
  10814. if (!Reverse)
  10815. {
  10816. Index++;
  10817. if (Index >= Item->Owner->Count)
  10818. {
  10819. Index = 0;
  10820. }
  10821. }
  10822. else
  10823. {
  10824. Index--;
  10825. if (Index < 0)
  10826. {
  10827. Index = Item->Owner->Count - 1;
  10828. }
  10829. }
  10830. return Item->Owner->Item[Index];
  10831. }
  10832. //---------------------------------------------------------------------------
  10833. TListItem * __fastcall TCustomScpExplorerForm::SearchFile(const UnicodeString & Text, bool SkipCurrent, bool Reverse)
  10834. {
  10835. TCustomDirView * ADirView = DirView(osCurrent);
  10836. if (ADirView->Items->Count == 0)
  10837. {
  10838. return NULL;
  10839. }
  10840. else
  10841. {
  10842. TListItem * CurrentItem = (ADirView->ItemFocused != NULL) ? ADirView->ItemFocused : ADirView->Items->Item[0];
  10843. TListItem * Item = CurrentItem;
  10844. if (SkipCurrent)
  10845. {
  10846. Item = DebugNotNull(GetNextFile(Item, Reverse));
  10847. }
  10848. while (true)
  10849. {
  10850. bool Matches = false;
  10851. switch (WinConfiguration->PanelSearch)
  10852. {
  10853. case isNameStartOnly:
  10854. Matches = ContainsTextSemiCaseSensitive(ADirView->ItemFileName(Item).SubString(1, Text.Length()), Text);
  10855. break;
  10856. case isName:
  10857. Matches = ContainsTextSemiCaseSensitive(ADirView->ItemFileName(Item), Text);
  10858. break;
  10859. case isAll:
  10860. int ColCount = (ADirView->ViewStyle == vsReport) ? ADirView->ColProperties->Count : 1;
  10861. int Index = 0;
  10862. while ((Index < ColCount) && !Matches)
  10863. {
  10864. bool Visible = (ADirView->ViewStyle == vsReport) ? ADirView->ColProperties->Visible[Index] : true;
  10865. if (Visible)
  10866. {
  10867. Matches = ContainsTextSemiCaseSensitive(ADirView->GetColumnText(Item, Index), Text);
  10868. }
  10869. Index++;
  10870. }
  10871. break;
  10872. }
  10873. if (Matches)
  10874. {
  10875. return Item;
  10876. }
  10877. Item = GetNextFile(Item, Reverse);
  10878. if (Item == CurrentItem)
  10879. {
  10880. return NULL;
  10881. }
  10882. }
  10883. }
  10884. }
  10885. //---------------------------------------------------------------------------
  10886. void __fastcall TCustomScpExplorerForm::DirViewExit(TObject *)
  10887. {
  10888. ResetIncrementalSearch();
  10889. }
  10890. //---------------------------------------------------------------------------
  10891. void __fastcall TCustomScpExplorerForm::CMDialogKey(TWMKeyDown & Message)
  10892. {
  10893. if (Message.CharCode == VK_TAB)
  10894. {
  10895. if (!FIncrementalSearch.IsEmpty())
  10896. {
  10897. TShiftState Shift = KeyDataToShiftState(Message.KeyData);
  10898. bool Reverse = Shift.Contains(ssShift);
  10899. IncrementalSearch(FIncrementalSearch, true, Reverse);
  10900. Message.Result = 1;
  10901. return;
  10902. }
  10903. }
  10904. else if (Message.CharCode == VK_ESCAPE)
  10905. {
  10906. if (!FIncrementalSearch.IsEmpty())
  10907. {
  10908. ResetIncrementalSearch();
  10909. Message.Result = 1;
  10910. return;
  10911. }
  10912. }
  10913. TForm::Dispatch(&Message);
  10914. }
  10915. //---------------------------------------------------------------------------
  10916. void __fastcall TCustomScpExplorerForm::Deactivate()
  10917. {
  10918. TForm::Deactivate();
  10919. // E.g. when switching to an internal editor window
  10920. ResetIncrementalSearch();
  10921. }
  10922. //---------------------------------------------------------------------------
  10923. void __fastcall TCustomScpExplorerForm::ApplicationEventsDeactivate(TObject *)
  10924. {
  10925. // When switching to another application
  10926. ResetIncrementalSearch();
  10927. }
  10928. //---------------------------------------------------------------------------
  10929. void __fastcall TCustomScpExplorerForm::ApplicationEventsModalBegin(TObject *)
  10930. {
  10931. ResetIncrementalSearch();
  10932. }
  10933. //---------------------------------------------------------------------------
  10934. void __fastcall TCustomScpExplorerForm::DirViewChangeFocus(TObject *, TListItem *)
  10935. {
  10936. if (FIncrementalSearching <= 0)
  10937. {
  10938. ResetIncrementalSearch();
  10939. }
  10940. }
  10941. //---------------------------------------------------------------------------
  10942. void __fastcall TCustomScpExplorerForm::RemoteStatusBarMouseDown(TObject *, TMouseButton, TShiftState, int, int)
  10943. {
  10944. CountClicksForWindowPrint(this);
  10945. }
  10946. //---------------------------------------------------------------------------
  10947. void __fastcall TCustomScpExplorerForm::RemoteDirViewResize(TObject *)
  10948. {
  10949. CenterReconnectToolbar();
  10950. }
  10951. //---------------------------------------------------------------------------
  10952. void __fastcall TCustomScpExplorerForm::BrowseFile()
  10953. {
  10954. if (RemoteDirView->ItemFocused != NULL)
  10955. {
  10956. RemoteDirView->ItemFocused->Selected = true;
  10957. }
  10958. }
  10959. //---------------------------------------------------------------------------
  10960. void __fastcall TCustomScpExplorerForm::UpdateQueueFileList()
  10961. {
  10962. if (!FQueueStatusUpdating)
  10963. {
  10964. bool Refresh;
  10965. if ((FQueueStatus != NULL) && QueueFileList->Visible)
  10966. {
  10967. Refresh = FQueueStatus->UpdateFileList(FQueueController->GetFocusedPrimaryItem(), FQueueFileList.get());
  10968. }
  10969. else
  10970. {
  10971. if (FQueueFileList->GetCount() > 0)
  10972. {
  10973. FQueueFileList->Clear();
  10974. Refresh = true;
  10975. }
  10976. }
  10977. int Count = FQueueFileList->GetCount();
  10978. if (QueueFileList->Items->Count != Count)
  10979. {
  10980. DebugAssert(Refresh);
  10981. QueueFileList->Items->Count = Count;
  10982. }
  10983. if (Refresh)
  10984. {
  10985. QueueFileList->Invalidate();
  10986. }
  10987. DebugAssert(QueueFileList->Items->Count == FQueueFileList->GetCount());
  10988. }
  10989. }
  10990. //---------------------------------------------------------------------------
  10991. void __fastcall TCustomScpExplorerForm::QueueFileListData(TObject *, TListItem * Item)
  10992. {
  10993. Item->Caption = FQueueFileList->GetFileName(Item->Index);
  10994. }
  10995. //---------------------------------------------------------------------------
  10996. void __fastcall TCustomScpExplorerForm::QueueView3Change(TObject *, TListItem *, TItemChange)
  10997. {
  10998. // Should be improved, once we do not refresh the list when switching between details of the same batch.
  10999. // See TSynchronizeChecklistDialog::UpdateTimer
  11000. if (QueueFileList->Items->Count > 0)
  11001. {
  11002. QueueFileList->Items->Item[0]->MakeVisible(false);
  11003. }
  11004. UpdateQueueFileList();
  11005. }
  11006. //---------------------------------------------------------------------------
  11007. void __fastcall TCustomScpExplorerForm::QueueLabelGetStatus(TCustomPathLabel *, bool & Active)
  11008. {
  11009. Active = QueueView3->Focused() || QueueFileList->Focused();
  11010. }
  11011. //---------------------------------------------------------------------------
  11012. void __fastcall TCustomScpExplorerForm::QueueFileListEnterExit(TObject *)
  11013. {
  11014. QueueLabelUpdateStatus();
  11015. }
  11016. //---------------------------------------------------------------------------
  11017. void __fastcall TCustomScpExplorerForm::QueueFileListCustomDrawItem(
  11018. TCustomListView * Sender, TListItem * Item, TCustomDrawState, bool & DebugUsedArg(DefaultDraw))
  11019. {
  11020. int State = FQueueFileList->GetState(Item->Index);
  11021. if (State == qfsProcessed)
  11022. {
  11023. Sender->Canvas->Font->Color = clGrayText;
  11024. }
  11025. }
  11026. //---------------------------------------------------------------------------
  11027. void __fastcall TCustomScpExplorerForm::QueueFileListColumnAutoSize()
  11028. {
  11029. // We do not really need whole width, so we always preemptivelly keep free space for the vertical scrollbar
  11030. QueueFileList->Column[0]->Width =
  11031. QueueFileList->ClientWidth - GetSystemMetricsForControl(QueueFileList, SM_CXVSCROLL) - ScaleByTextHeight(QueueFileList, 8);
  11032. }
  11033. //---------------------------------------------------------------------------
  11034. void __fastcall TCustomScpExplorerForm::QueueFileListResize(TObject *)
  11035. {
  11036. QueueFileListColumnAutoSize();
  11037. }
  11038. //---------------------------------------------------------------------------
  11039. void __fastcall TCustomScpExplorerForm::CloseApp()
  11040. {
  11041. // Called from TNonVisualDataModule::ExplorerActionsExecute, which sets busy flag.
  11042. // FormCloseQuery must check busy state, so that the application cannot be closed while busy by Alt+F4 and X button.
  11043. // So we clear the flag here.
  11044. DebugAssert(NonVisualDataModule->Busy);
  11045. NonVisualDataModule->EndBusy();
  11046. try
  11047. {
  11048. Close();
  11049. }
  11050. __finally
  11051. {
  11052. NonVisualDataModule->StartBusy();
  11053. }
  11054. }
  11055. //---------------------------------------------------------------------------
  11056. bool __fastcall TCustomScpExplorerForm::IsActiveTerminal(TTerminal * Terminal)
  11057. {
  11058. return (Terminal != NULL) && Terminal->Active;
  11059. }
  11060. //---------------------------------------------------------------------------
  11061. bool __fastcall TCustomScpExplorerForm::HasManagedSession()
  11062. {
  11063. return (ManagedSession != NULL);
  11064. }
  11065. //---------------------------------------------------------------------------
  11066. bool __fastcall TCustomScpExplorerForm::HasActiveTerminal()
  11067. {
  11068. return IsActiveTerminal(Terminal);
  11069. }
  11070. //---------------------------------------------------------------------------
  11071. void TCustomScpExplorerForm::LocalLocalCopy(
  11072. TFileOperation, TOperationSide, bool DebugUsedArg(OnFocused), bool DebugUsedArg(NoConfirmation),
  11073. bool DebugUsedArg(DragDrop), unsigned int DebugUsedArg(Flags))
  11074. {
  11075. DebugFail();
  11076. }
  11077. //---------------------------------------------------------------------------
  11078. void TCustomScpExplorerForm::LocalLocalCopyCommand(TFileOperation Operation, TOperationSide Side, bool OnFocused, unsigned int Flags)
  11079. {
  11080. LocalLocalCopy(Operation, Side, OnFocused, !WinConfiguration->ConfirmTransferring, false, Flags);
  11081. }
  11082. //---------------------------------------------------------------------------
  11083. void __fastcall TCustomScpExplorerForm::SessionsPageControlResize(TObject *)
  11084. {
  11085. // 1) Changing tab list triggers the OnResize.
  11086. // 2) Is called in TForm constructor, when we are not ready to call SessionListChanged (particularly when starting with colored session)
  11087. if (!FUpdatingSessionTabs && (FSessionColors != NULL))
  11088. {
  11089. // UpdateTabsCaptionTruncation should be enough
  11090. SessionListChanged(true);
  11091. }
  11092. }
  11093. //---------------------------------------------------------------------------
  11094. UnicodeString TCustomScpExplorerForm::GetSessionPath(TManagedTerminal * ASession, TOperationSide Side)
  11095. {
  11096. if (ASession == ManagedSession)
  11097. {
  11098. UpdateSession(ASession);
  11099. }
  11100. TSessionData * Data = ASession->StateData;
  11101. UnicodeString Result;
  11102. if (Side == osLocal)
  11103. {
  11104. Result = Data->LocalDirectory;
  11105. }
  11106. else if (DebugAlwaysTrue(Side == osOther))
  11107. {
  11108. Result = ASession->LocalBrowser ? Data->OtherLocalDirectory : Data->RemoteDirectory;
  11109. }
  11110. return Result;
  11111. }
  11112. //---------------------------------------------------------------------------
  11113. UnicodeString TCustomScpExplorerForm::GetTabHintSessionDetails(TManagedTerminal * ASession)
  11114. {
  11115. UnicodeString Title = TTerminalManager::Instance()->GetSessionTitle(ASession, false);
  11116. UnicodeString Result = ASession->SessionData->DefaultSessionName;
  11117. if (Title == Result)
  11118. {
  11119. Result = EmptyStr;
  11120. }
  11121. else
  11122. {
  11123. Result += L"\n";
  11124. }
  11125. return Result;
  11126. }
  11127. //---------------------------------------------------------------------------
  11128. UnicodeString TCustomScpExplorerForm::GetTabHintDetails(TManagedTerminal * ASession)
  11129. {
  11130. UnicodeString Result = GetTabHintSessionDetails(ASession);
  11131. if (!ASession->Active)
  11132. {
  11133. Result += LoadStr(STATUS_NOT_CONNECTED2);
  11134. }
  11135. else
  11136. {
  11137. Result += GetSessionPath(ASession, osRemote);
  11138. }
  11139. return Result;
  11140. }
  11141. //---------------------------------------------------------------------------
  11142. UnicodeString TCustomScpExplorerForm::GetNewTabHintDetails()
  11143. {
  11144. return LoadStr(NEW_REMOTE_TAB_HINT);
  11145. }
  11146. //---------------------------------------------------------------------------
  11147. void __fastcall TCustomScpExplorerForm::SessionsPageControlTabHint(TPageControl *, int Index, UnicodeString & Hint)
  11148. {
  11149. if (Index >= 0)
  11150. {
  11151. TThemeTabSheet * TabSheet = SessionsPageControl->Pages[Index];
  11152. TManagedTerminal * TabSession = GetSessionTabSession(TabSheet);
  11153. if (TabSession == NULL)
  11154. {
  11155. UnicodeString NewTabTabCaption = GetNewTabTabCaption();
  11156. if (NewTabTabCaption != TabSheet->Caption)
  11157. {
  11158. Hint = NewTabTabCaption + L"|" + GetNewTabHintDetails();
  11159. }
  11160. }
  11161. else
  11162. {
  11163. TTerminalManager * Manager = TTerminalManager::Instance();
  11164. UnicodeString TabCaption = Manager->GetSessionTitle(TabSession, true);
  11165. Hint = TabCaption + L"|" + GetTabHintDetails(TabSession);
  11166. }
  11167. }
  11168. }
  11169. //---------------------------------------------------------------------------
  11170. typedef std::vector<TListItem *> TListItemsVector;
  11171. struct TCalculateSizeOperation
  11172. {
  11173. TCalculateSizeStats Stats;
  11174. TListItemsVector ListItems;
  11175. size_t Index;
  11176. };
  11177. //---------------------------------------------------------------------------
  11178. void TCustomScpExplorerForm::CalculateDirectorySizes(TOperationSide Side)
  11179. {
  11180. TCalculateSizeOperation CalculateSizeOperation;
  11181. Side = GetSide(Side);
  11182. TCustomDirView * ADirView = DirView(Side);
  11183. bool LocalSide = IsSideLocalBrowser(Side);
  11184. std::unique_ptr<TStrings> AllFileList(ADirView->CreateFileList(false, LocalSide, NULL, true));
  11185. std::unique_ptr<TStrings> FileList(new TStringList());
  11186. for (int Index = 0; Index < AllFileList->Count; Index++)
  11187. {
  11188. TListItem * Item = DebugNotNull(dynamic_cast<TListItem *>(AllFileList->Objects[Index]));
  11189. if (ADirView->ItemIsDirectory(Item))
  11190. {
  11191. FileList->AddObject(AllFileList->Strings[Index], ADirView->ItemData(Item));
  11192. CalculateSizeOperation.ListItems.push_back(Item);
  11193. }
  11194. }
  11195. CalculateSizeOperation.Index = 0;
  11196. __int64 Size = 0;
  11197. TValueRestorer<TCalculateSizeOperation *> DirectorySizeOperationRestorer(FCalculateSizeOperation);
  11198. FCalculateSizeOperation = &CalculateSizeOperation;
  11199. TValueRestorer<TFileOperationFinishedEvent> OnFileOperationFinishedRestorer(FOnFileOperationFinished);
  11200. FOnFileOperationFinished = DirectorySizeCalculated;
  11201. TValueRestorer<TFileOperation> PrimaryOperationRestorer(FPrimaryOperation);
  11202. FPrimaryOperation = foCalculateSize;
  11203. TCalculatedSizes CalculatedSizes;
  11204. CalculateSizeOperation.Stats.CalculatedSizes = &CalculatedSizes;
  11205. if (LocalSide)
  11206. {
  11207. TValueRestorer<TOperationSide> ProgressSideRestorer(FProgressSide);
  11208. FProgressSide = Side;
  11209. ManagedSession->CalculateLocalFilesSize(FileList.get(), Size, NULL, true, NULL, &CalculatedSizes);
  11210. }
  11211. else
  11212. {
  11213. TCalculateSizeParams Params;
  11214. Params.Stats = &CalculateSizeOperation.Stats;
  11215. Params.UseCache = true;
  11216. Terminal->CalculateFilesSize(FileList.get(), Size, Params);
  11217. }
  11218. }
  11219. //---------------------------------------------------------------------------
  11220. void __fastcall TCustomScpExplorerForm::DirectorySizeCalculated(TOperationSide Side, const UnicodeString & FileName, bool Success)
  11221. {
  11222. DebugAlwaysTrue(FCalculateSizeOperation != NULL);
  11223. TListItem * Item = VisualiseOperationFinished(Side, FileName, false);
  11224. TCustomDirView * ADirView = dynamic_cast<TCustomDirView *>(Item->Owner->Owner);
  11225. // optimization
  11226. bool GotExpectedItem =
  11227. (FCalculateSizeOperation->Index < FCalculateSizeOperation->ListItems.size()) &&
  11228. (FCalculateSizeOperation->ListItems[FCalculateSizeOperation->Index] == Item);
  11229. int Index = -1;
  11230. if (DebugAlwaysTrue(GotExpectedItem))
  11231. {
  11232. Index = FCalculateSizeOperation->Index;
  11233. FCalculateSizeOperation->Index++;
  11234. }
  11235. else
  11236. {
  11237. TListItemsVector::iterator I =
  11238. std::find(FCalculateSizeOperation->ListItems.begin(), FCalculateSizeOperation->ListItems.end(), Item);
  11239. if (DebugAlwaysTrue(I != FCalculateSizeOperation->ListItems.end()))
  11240. {
  11241. Index = I - FCalculateSizeOperation->ListItems.begin();
  11242. }
  11243. }
  11244. if (DebugAlwaysTrue(Index >= 0) && Success)
  11245. {
  11246. __int64 Size = (*FCalculateSizeOperation->Stats.CalculatedSizes)[Index];
  11247. ADirView->SetItemCalculatedSize(Item, Size);
  11248. }
  11249. }
  11250. //---------------------------------------------------------------------------
  11251. void __fastcall TCustomScpExplorerForm::MessageDockRequestDock(TObject *, TTBCustomDockableWindow *, bool & Accept)
  11252. {
  11253. Accept = false;
  11254. }
  11255. //---------------------------------------------------------------------------
  11256. void TCustomScpExplorerForm::AutoSizeColumns(TOperationSide Side)
  11257. {
  11258. // Send Ctrl+Gray+ to the list view
  11259. TKeyboardState KeyState;
  11260. GetKeyboardState(KeyState);
  11261. TKeyboardState NewKeyState;
  11262. memset(NewKeyState, 0, sizeof(NewKeyState));
  11263. NewKeyState[VK_CONTROL] = 0x81;
  11264. SetKeyboardState(NewKeyState);
  11265. SendMessage(DirView(Side)->Handle, WM_KEYDOWN, VK_ADD, 0);
  11266. SetKeyboardState(KeyState);
  11267. }
  11268. //---------------------------------------------------------------------------
  11269. void TCustomScpExplorerForm::QueueResetLayoutColumns()
  11270. {
  11271. LoadListViewStr(QueueView3, QueueViewLayoutDefault);
  11272. }