CustomScpExplorer.cpp 416 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426
  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 "NonVisual.h"
  22. #include "Glyphs.h"
  23. #include "Tools.h"
  24. #include "WinConfiguration.h"
  25. #include "TerminalManager.h"
  26. #include "EditorManager.h"
  27. #include "ProgParams.h"
  28. #include "Setup.h"
  29. #include <Consts.hpp>
  30. #include <DateUtils.hpp>
  31. #include <TB2Common.hpp>
  32. #include <DirectoryMonitor.hpp>
  33. #include <System.IOUtils.hpp>
  34. #include <System.StrUtils.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. FQueueFileList.reset(new TQueueFileList());
  204. FProgressSide = osCurrent;
  205. FCalculateSizeOperation = NULL;
  206. FDownloadingFromClipboard = false;
  207. FClipboardFakeMonitorsPendingReset = false;
  208. FHiContrastTheme = NULL;
  209. InitializeRemoteThumbnailMask();
  210. FEditorManager = new TEditorManager();
  211. FEditorManager->OnFileChange = ExecutedFileChanged;
  212. FEditorManager->OnFileReload = ExecutedFileReload;
  213. FEditorManager->OnFileEarlyClosed = ExecutedFileEarlyClosed;
  214. FEditorManager->OnFileUploadComplete = ExecutedFileUploadComplete;
  215. FLocalEditors = new TList();
  216. FQueueStatus = NULL;
  217. FQueueStatusSection = new TCriticalSection();
  218. FQueueStatusInvalidated = false;
  219. FQueueItemInvalidated = false;
  220. FQueueStatusUpdating = false;
  221. FQueueActedItem = NULL;
  222. FQueueController = new TQueueController(QueueView3);
  223. FQueueScrollOnDragOver = new TListViewScrollOnDragOver(QueueView3, true);
  224. FUserActionTimer = new TTimer(this);
  225. FUserActionTimer->Enabled = false;
  226. FUserActionTimer->Interval = 10;
  227. FUserActionTimer->OnTimer = UserActionTimer;
  228. FNotes = new TStringList();
  229. FNoteTimer = new TTimer(this);
  230. FNoteTimer->Enabled = false;
  231. FNoteTimer->OnTimer = NoteTimer;
  232. FOnNoteClick = NULL;
  233. FOle32Library = LoadLibrary(L"Ole32.dll");
  234. FDragMoveCursor = FOle32Library != NULL ?
  235. LoadCursor(FOle32Library, MAKEINTRESOURCE(2)) : NULL;
  236. UseSystemSettings(this);
  237. TTBXStringList * TransferList = dynamic_cast<TTBXStringList*>(
  238. static_cast<TObject*>(GetComponent(fcTransferList)));
  239. DebugAssert(TransferList != NULL);
  240. FTransferListHoverIndex = -1;
  241. TransferList->OnChange = TransferListChange;
  242. TransferList->OnDrawItem = TransferListDrawItem;
  243. SetSubmenu(dynamic_cast<TTBXCustomItem *>(static_cast<TObject *>(GetComponent(fcColorMenu))), true);
  244. SetSubmenu(NonVisualDataModule->ColorMenuItem, true);
  245. UseDesktopFont(SessionsPageControl);
  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. // Needed for Explorer (Commander calls it implicitly on its own)
  266. UpdateSessionsPageControlHeight();
  267. UpdateQueueLabel();
  268. CheckStoreTransition();
  269. CreateHiddenWindow();
  270. StartUpdates();
  271. // Start loading drive data, so that they are ready once we need them for StartCreationDirectoryMonitorsOnEachDrive
  272. // (necessary for "explorer" only, as "commander" loads it for its drive menu)
  273. DriveInfo->NeedData();
  274. }
  275. //---------------------------------------------------------------------------
  276. __fastcall TCustomScpExplorerForm::~TCustomScpExplorerForm()
  277. {
  278. AppLog(L"Destroying main window");
  279. FInvalid = true;
  280. ReleaseHiContrastTheme();
  281. if (FClipboardTerminal != NULL)
  282. {
  283. ClipboardClear(); // implies ClipboardStop
  284. }
  285. // this has to be one of the very first things to do
  286. StopUpdateThread();
  287. // This is needed when shuting down Windows only. Otherwise it's already set NULL from Execute()
  288. TTerminalManager::Instance()->ScpExplorer = NULL;
  289. delete FCustomCommandLocalFileList;
  290. delete FCustomCommandRemoteFileList;
  291. delete FCustomCommandMenu;
  292. delete FTrayIcon;
  293. FTrayIcon = NULL;
  294. FEditorManager->CloseInternalEditors(ForceCloseInternalEditor);
  295. delete FEditorManager;
  296. ForceCloseLocalEditors();
  297. delete FLocalEditors;
  298. HideFileFindDialog();
  299. if (FDelayedDeletionTimer)
  300. {
  301. DoDelayedDeletion(NULL);
  302. SAFE_DESTROY(FDelayedDeletionTimer);
  303. }
  304. SAFE_DESTROY(FDelayedDeletionList);
  305. // sometimes we do not get DDEnd so the list is not released
  306. SAFE_DESTROY(FDDFileList);
  307. DebugAssert(FSynchronizeController == NULL);
  308. CloseHandle(FDDExtMutex);
  309. FDDExtMutex = NULL;
  310. FreeLibrary(FOle32Library);
  311. FOle32Library = NULL;
  312. FDragMoveCursor = NULL;
  313. DebugAssert(!FErrorList);
  314. if (FEverShown)
  315. {
  316. // when window is never shown (like when running command-line operation),
  317. // particularly window site is not restored correctly (BoundsRect value set
  318. // in RestoreForm gets lost during handle allocation), so we do not want
  319. // it to be stored
  320. AppLog(L"Storing main window configuration");
  321. StoreParams();
  322. }
  323. ManagedSession = NULL;
  324. Queue = NULL;
  325. DebugAssert(NonVisualDataModule && (NonVisualDataModule->ScpExplorer == this));
  326. NonVisualDataModule->ScpExplorer = NULL;
  327. delete FQueueScrollOnDragOver;
  328. FQueueScrollOnDragOver = NULL;
  329. delete FQueueController;
  330. FQueueController = NULL;
  331. delete FQueueStatusSection;
  332. FQueueStatusSection = NULL;
  333. delete FQueueStatus;
  334. FQueueStatus = NULL;
  335. delete FUserActionTimer;
  336. FUserActionTimer = NULL;
  337. delete FNoteTimer;
  338. delete FNotes;
  339. delete FNoteData;
  340. SAFE_DESTROY(FDocks);
  341. SessionsPageControl->Images = NULL;
  342. // only after clearing Terminal (after SessionListChanged => ... => AddSessionColor is called)
  343. SAFE_DESTROY(FSessionColors);
  344. SAFE_DESTROY(FSessionsDragDropFilesEx);
  345. SAFE_DESTROY(FHistoryMenu[0][0]);
  346. SAFE_DESTROY(FHistoryMenu[0][1]);
  347. SAFE_DESTROY(FHistoryMenu[1][0]);
  348. SAFE_DESTROY(FHistoryMenu[1][1]);
  349. if (FHiddenWindow != NULL)
  350. {
  351. DestroyWindow(FHiddenWindow);
  352. FHiddenWindow = NULL;
  353. }
  354. AppLog(L"Main window destroyed");
  355. }
  356. //---------------------------------------------------------------------------
  357. void TCustomScpExplorerForm::ReleaseHiContrastTheme()
  358. {
  359. if (FHiContrastTheme != NULL)
  360. {
  361. ReleaseTBXTheme(FHiContrastTheme);
  362. FHiContrastTheme = NULL;
  363. }
  364. }
  365. //---------------------------------------------------------------------------
  366. void __fastcall TCustomScpExplorerForm::RefreshPanel(const UnicodeString & Session, const UnicodeString & Path)
  367. {
  368. std::unique_ptr<TSessionData> Data;
  369. if (!Session.IsEmpty())
  370. {
  371. bool DefaultsOnly;
  372. Data.reset(StoredSessions->ParseUrl(Session, NULL, DefaultsOnly));
  373. }
  374. TTerminalManager * Manager = TTerminalManager::Instance();
  375. for (int Index = 0; Index < Manager->Count; Index++)
  376. {
  377. TTerminal * Terminal = Manager->Sessions[Index];
  378. if (Session.IsEmpty() ||
  379. Manager->IsActiveTerminalForSite(Terminal, Data.get()))
  380. {
  381. if (Path.IsEmpty())
  382. {
  383. Terminal->ClearCaches();
  384. }
  385. else
  386. {
  387. Terminal->DirectoryModified(Path, true);
  388. }
  389. }
  390. }
  391. // We should flag a pending refresh for the background terminals or busy foreground terminals
  392. if (HasActiveTerminal() &&
  393. CanCommandLineFromAnotherInstance() &&
  394. (Session.IsEmpty() || Manager->IsActiveTerminalForSite(Terminal, Data.get())) &&
  395. (Path.IsEmpty() || UnixIsChildPath(Path, Terminal->CurrentDirectory)))
  396. {
  397. Terminal->ReloadDirectory();
  398. }
  399. }
  400. //---------------------------------------------------------------------------
  401. void __fastcall TCustomScpExplorerForm::WMCopyData(TMessage & Message)
  402. {
  403. PCOPYDATASTRUCT CopyData = reinterpret_cast<PCOPYDATASTRUCT>(Message.LParam);
  404. AppLogFmt(L"Received copy data message with size %d", (int(CopyData->cbData)));
  405. size_t MessageSize = sizeof(TCopyDataMessage);
  406. bool Result = DebugAlwaysTrue(CopyData->cbData == MessageSize);
  407. if (Result)
  408. {
  409. const TCopyDataMessage & Message = *reinterpret_cast<const TCopyDataMessage *>(CopyData->lpData);
  410. Result = (Message.Version == TCopyDataMessage::Version1);
  411. if (Result)
  412. {
  413. switch (Message.Command)
  414. {
  415. case TCopyDataMessage::CommandCanCommandLine:
  416. Result = CanCommandLineFromAnotherInstance();
  417. if (Result)
  418. {
  419. AppLog(L"Can process command-line from other instance");
  420. }
  421. else
  422. {
  423. AppLog(L"Cannot process command-line from other instance");
  424. }
  425. break;
  426. case TCopyDataMessage::CommandCommandLine:
  427. {
  428. UnicodeString CommandLine(Message.CommandLine);
  429. Result = CommandLineFromAnotherInstance(CommandLine);
  430. }
  431. break;
  432. case TCopyDataMessage::RefreshPanel:
  433. RefreshPanel(Message.Refresh.Session, Message.Refresh.Path);
  434. break;
  435. case TCopyDataMessage::MainWindowCheck:
  436. Result = true;
  437. break;
  438. default:
  439. Result = false;
  440. break;
  441. }
  442. }
  443. }
  444. Message.Result = Result ? 1 : 0;
  445. }
  446. //---------------------------------------------------------------------------
  447. void __fastcall TCustomScpExplorerForm::CreateHiddenWindow()
  448. {
  449. WNDCLASS WindowClass = {0};
  450. WindowClass.lpfnWndProc = DefWindowProc;
  451. WindowClass.hInstance = HInstance;
  452. WindowClass.lpszClassName = HIDDEN_WINDOW_NAME;
  453. FHiddenWindow = NULL;
  454. if (RegisterClass(&WindowClass))
  455. {
  456. FHiddenWindow = CreateWindow(HIDDEN_WINDOW_NAME, L"",
  457. WS_POPUP, 0, 0, 0, 0, 0, 0, HInstance, NULL);
  458. }
  459. }
  460. //---------------------------------------------------------------------------
  461. bool __fastcall TCustomScpExplorerForm::CanConsole()
  462. {
  463. return
  464. HasActiveTerminal() &&
  465. (Terminal->IsCapable[fcAnyCommand] || Terminal->IsCapable[fcSecondaryShell]);
  466. }
  467. //---------------------------------------------------------------------------
  468. bool __fastcall TCustomScpExplorerForm::CanCommandLineFromAnotherInstance()
  469. {
  470. bool Result = Visible && !NonVisualDataModule->Busy;
  471. return Result;
  472. }
  473. //---------------------------------------------------------------------------
  474. bool __fastcall TCustomScpExplorerForm::CommandLineFromAnotherInstance(
  475. const UnicodeString & CommandLine)
  476. {
  477. TProgramParams Params(CommandLine);
  478. bool Result = CanCommandLineFromAnotherInstance() && DebugAlwaysTrue(Params.ParamCount > 0);
  479. if (Result)
  480. {
  481. NonVisualDataModule->StartBusy();
  482. try
  483. {
  484. // this action is initiated from another process,
  485. // so it's likely that our window is not visible,
  486. // and user won't see what is going on
  487. Application->BringToFront();
  488. // reload sessions as we may be asked to open a session
  489. // just stored by another instance
  490. StoredSessions->Reload();
  491. UnicodeString SessionName = Params.Param[1];
  492. std::unique_ptr<TObjectList> DataList(new TObjectList());
  493. try
  494. {
  495. UnicodeString DownloadFile; // unused
  496. int Flags = GetCommandLineParseUrlFlags(&Params);
  497. GetLoginData(SessionName, &Params, DataList.get(), DownloadFile, true, this, Flags);
  498. }
  499. catch (EAbort &)
  500. {
  501. // Happens when opening session in PuTTY and in other situations,
  502. // in which we do not want to fail WM_COPYDATA, as that would cause master instance
  503. // to process the command-line on its own (or to pass it to yet another instance)
  504. }
  505. if (DataList->Count > 0)
  506. {
  507. TTerminalManager * Manager = TTerminalManager::Instance();
  508. Manager->ActiveSession = Manager->NewSessions(DataList.get());
  509. }
  510. }
  511. __finally
  512. {
  513. NonVisualDataModule->EndBusy();
  514. }
  515. }
  516. return Result;
  517. }
  518. //---------------------------------------------------------------------------
  519. TManagedTerminal * TCustomScpExplorerForm::GetTerminal()
  520. {
  521. TManagedTerminal * Result;
  522. if ((FManagedSession != NULL) && !FManagedSession->LocalBrowser)
  523. {
  524. Result = FManagedSession;
  525. }
  526. else
  527. {
  528. Result = NULL;
  529. }
  530. return Result;
  531. }
  532. //---------------------------------------------------------------------------
  533. void __fastcall TCustomScpExplorerForm::SetManagedSession(TManagedTerminal * value)
  534. {
  535. if (FManagedSession != value)
  536. {
  537. TWindowLock Lock(this);
  538. SessionChanging();
  539. DoSetManagedSession(value, false);
  540. }
  541. }
  542. //---------------------------------------------------------------------------
  543. void __fastcall TCustomScpExplorerForm::DoSetManagedSession(TManagedTerminal * value, bool Replace)
  544. {
  545. DebugAssert(!Replace || ((value != NULL) && !value->LocalBrowser));
  546. FManagedSession = value;
  547. {
  548. TValueRestorer<bool> AllowTransferPresetAutoSelectRestorer(FAllowTransferPresetAutoSelect, false);
  549. TAutoFlag SessionChangingFlag(FSessionChanging);
  550. SessionChanged(Replace);
  551. }
  552. if (Terminal != NULL)
  553. {
  554. TransferPresetAutoSelect();
  555. }
  556. // Update app and tab titles, prevented above by FSessionChanging.
  557. UpdateControls();
  558. }
  559. //---------------------------------------------------------------------------
  560. void __fastcall TCustomScpExplorerForm::ReplaceTerminal(TManagedTerminal * value)
  561. {
  562. FEditorManager->ProcessFiles(FileTerminalReplaced, value);
  563. DoSetManagedSession(value, true);
  564. }
  565. //---------------------------------------------------------------------------
  566. void __fastcall TCustomScpExplorerForm::SessionChanging()
  567. {
  568. if (ManagedSession != NULL)
  569. {
  570. UpdateSession(ManagedSession);
  571. }
  572. }
  573. //---------------------------------------------------------------------------
  574. void __fastcall TCustomScpExplorerForm::SessionChanged(bool Replaced)
  575. {
  576. if (ManagedSession != NULL)
  577. {
  578. UpdateSessionColor(static_cast<TColor>(ManagedSession->StateData->Color));
  579. }
  580. if (!TTerminalManager::Instance()->Updating)
  581. {
  582. SessionListChanged();
  583. }
  584. if (Terminal != NULL)
  585. {
  586. TObject * State = NULL;
  587. if (WinConfiguration->PreservePanelState)
  588. {
  589. State = Terminal->RemoteExplorerState;
  590. DirView(osRemote)->AnnounceState(State);
  591. }
  592. try
  593. {
  594. if (Replaced)
  595. {
  596. RemoteDirView->ReplaceTerminal(Terminal);
  597. }
  598. else
  599. {
  600. RemoteDirView->Terminal = Terminal;
  601. }
  602. if (Terminal->Active)
  603. {
  604. Terminal->RefreshDirectory();
  605. }
  606. if (State != NULL)
  607. {
  608. DirView(osRemote)->RestoreState(State);
  609. }
  610. }
  611. __finally
  612. {
  613. DirView(osRemote)->AnnounceState(NULL);
  614. }
  615. }
  616. else
  617. {
  618. RemoteDirView->Terminal = NULL;
  619. }
  620. InitStatusBar();
  621. UpdateTransferList();
  622. // Update panels Enable state before refreshing the labels
  623. UpdateControls();
  624. }
  625. //---------------------------------------------------------------------------
  626. void __fastcall TCustomScpExplorerForm::SetQueue(TTerminalQueue * value)
  627. {
  628. if (Queue != value)
  629. {
  630. if (FQueue != NULL)
  631. {
  632. FQueue->OnListUpdate = NULL;
  633. FQueue->OnQueueItemUpdate = NULL;
  634. }
  635. FQueue = value;
  636. if (FQueue != NULL)
  637. {
  638. DebugAssert(FQueue->OnListUpdate == NULL);
  639. FQueue->OnListUpdate = QueueListUpdate;
  640. DebugAssert(FQueue->OnQueueItemUpdate == NULL);
  641. FQueue->OnQueueItemUpdate = QueueItemUpdate;
  642. }
  643. QueueChanged();
  644. }
  645. }
  646. //---------------------------------------------------------------------------
  647. void __fastcall TCustomScpExplorerForm::QueueView3Deletion(TObject * /*Sender*/,
  648. TListItem * Item)
  649. {
  650. if (FQueueActedItem == Item)
  651. {
  652. FQueueActedItem = NULL;
  653. if ((QueueView3->PopupMenu != NULL) &&
  654. (QueueView3->PopupMenu->PopupComponent == QueueView3))
  655. {
  656. // rather "trick", suggested by Jordan on jrsoftware.toolbar2000
  657. ReleaseCapture();
  658. }
  659. }
  660. if (Item->Data == FPendingQueueActionItem)
  661. {
  662. FPendingQueueActionItem = NULL;
  663. }
  664. }
  665. //---------------------------------------------------------------------------
  666. bool TCustomScpExplorerForm::IsAnythingQueued()
  667. {
  668. return (FQueueStatus != NULL) && (FQueueStatus->ActiveAndPendingPrimaryCount > 0);
  669. }
  670. //---------------------------------------------------------------------------
  671. void __fastcall TCustomScpExplorerForm::UpdateQueueStatus(bool QueueChanging)
  672. {
  673. {
  674. TGuard Guard(FQueueStatusSection);
  675. FQueueStatusInvalidated = false;
  676. if (FQueue != NULL)
  677. {
  678. FQueueStatus = FQueue->CreateStatus(FQueueStatus);
  679. }
  680. }
  681. if ((FQueueStatus != NULL) && (FQueueStatus->Count > FMaxQueueLength))
  682. {
  683. FMaxQueueLength = FQueueStatus->Count;
  684. Configuration->Usage->SetMax(L"MaxQueueLength", FMaxQueueLength);
  685. }
  686. {
  687. TAutoFlag Flag(FQueueStatusUpdating);
  688. FQueueController->UpdateQueueStatus(FQueueStatus);
  689. }
  690. UpdateQueueFileList();
  691. SetQueueProgress();
  692. UpdateQueueView();
  693. bool IsEmpty = !IsAnythingQueued();
  694. if (IsEmpty && (Terminal != NULL))
  695. {
  696. TOnceDoneOperation OnceDoneOperation =
  697. NonVisualDataModule->CurrentQueueOnceEmptyOperation();
  698. NonVisualDataModule->ResetQueueOnceEmptyOperation();
  699. if ((FQueue != NULL) && !WinConfiguration->EnableQueueByDefault && !QueueChanging)
  700. {
  701. FQueue->Enabled = false;
  702. }
  703. if ((OnceDoneOperation != odoIdle) && !NonVisualDataModule->Busy)
  704. {
  705. Terminal->CloseOnCompletion(OnceDoneOperation, LoadStr(CLOSED_ON_QUEUE_EMPTY));
  706. }
  707. }
  708. }
  709. //---------------------------------------------------------------------------
  710. UnicodeString __fastcall TCustomScpExplorerForm::GetQueueProgressTitle()
  711. {
  712. UnicodeString Result;
  713. if (FQueueStatus != NULL)
  714. {
  715. if (FQueueStatus->IsOnlyOneActiveAndNoPending())
  716. {
  717. TFileOperationProgressType * ProgressData =
  718. FQueueStatus->Items[FQueueStatus->DoneCount]->ProgressData;
  719. if ((ProgressData != NULL) && ProgressData->InProgress)
  720. {
  721. Result = TProgressForm::ProgressStr(NULL, ProgressData);
  722. }
  723. }
  724. else if (FQueueStatus->ActiveAndPendingPrimaryCount > 1)
  725. {
  726. Result = FMTLOAD(PROGRESS_IN_QUEUE, (FQueueStatus->ActiveAndPendingPrimaryCount));
  727. }
  728. }
  729. return Result;
  730. }
  731. //---------------------------------------------------------------------------
  732. void __fastcall TCustomScpExplorerForm::UpdateQueueView()
  733. {
  734. ComponentVisible[fcQueueView] =
  735. !IsLocalBrowserMode() &&
  736. ((WinConfiguration->QueueView.Show == qvShow) ||
  737. ((WinConfiguration->QueueView.Show == qvHideWhenEmpty) &&
  738. (FQueueStatus != NULL) && (FQueueStatus->Count > 0)));
  739. }
  740. //---------------------------------------------------------------------------
  741. void __fastcall TCustomScpExplorerForm::QueueChanged()
  742. {
  743. if (FQueueStatus != NULL)
  744. {
  745. // UpdateFileList implementation relies on the status being recreated when session changes
  746. delete FQueueStatus;
  747. FQueueStatus = NULL;
  748. }
  749. UpdateQueueStatus(true);
  750. }
  751. //---------------------------------------------------------------------------
  752. void __fastcall TCustomScpExplorerForm::QueueListUpdate(TTerminalQueue * Queue)
  753. {
  754. if (FQueue == Queue)
  755. {
  756. FQueueStatusInvalidated = true;
  757. }
  758. }
  759. //---------------------------------------------------------------------------
  760. void __fastcall TCustomScpExplorerForm::QueueItemUpdate(TTerminalQueue * Queue,
  761. TQueueItem * Item)
  762. {
  763. if (FQueue == Queue)
  764. {
  765. TGuard Guard(FQueueStatusSection);
  766. // this may be running in parallel with QueueChanged
  767. if (FQueueStatus != NULL)
  768. {
  769. TQueueItemProxy * QueueItem = FQueueStatus->FindByQueueItem(Item);
  770. if ((Item->Status == TQueueItem::qsDone) && (Terminal != NULL))
  771. {
  772. FRefreshLocalDirectory = (QueueItem == NULL) ||
  773. (!QueueItem->Info->ModifiedLocal.IsEmpty());
  774. FRefreshRemoteDirectory = (QueueItem == NULL) ||
  775. (!QueueItem->Info->ModifiedRemote.IsEmpty());
  776. }
  777. if (QueueItem != NULL)
  778. {
  779. QueueItem->UserData = (void*)true;
  780. FQueueItemInvalidated = true;
  781. }
  782. }
  783. }
  784. }
  785. //---------------------------------------------------------------------------
  786. bool __fastcall TCustomScpExplorerForm::IsQueueAutoPopup()
  787. {
  788. // during standalone editing, we have no way to see/control queue,
  789. // so we have to always popup prompts automatically
  790. return StandaloneOperation || GUIConfiguration->QueueAutoPopup;
  791. }
  792. //---------------------------------------------------------------------------
  793. void __fastcall TCustomScpExplorerForm::RefreshQueueItems()
  794. {
  795. if (FQueueStatus != NULL)
  796. {
  797. bool QueueAutoPopup = IsQueueAutoPopup();
  798. bool NeedRefresh = FQueueController->NeedRefresh();
  799. bool Refresh = FQueueItemInvalidated || NeedRefresh;
  800. FQueueItemInvalidated = false;
  801. int Limit = Refresh ? FQueueStatus->Count : FQueueStatus->DoneAndActiveCount;
  802. bool Updated = false;
  803. TQueueItemProxy * QueueItem;
  804. bool WasUserAction;
  805. for (int Index = 0; Index < Limit; Index++)
  806. {
  807. bool Update = false;
  808. QueueItem = FQueueStatus->Items[Index];
  809. WasUserAction = TQueueItem::IsUserActionStatus(QueueItem->Status);
  810. if (!NonVisualDataModule->Busy && QueueAutoPopup && WasUserAction &&
  811. (FPendingQueueActionItem == NULL))
  812. {
  813. FPendingQueueActionItem = QueueItem;
  814. FUserActionTimer->Enabled = true;
  815. }
  816. if ((bool)QueueItem->UserData)
  817. {
  818. QueueItem->UserData = (void*)false;
  819. QueueItem->Update();
  820. Updated = true;
  821. Update = true;
  822. }
  823. else if (FQueueController->QueueItemNeedsFrequentRefresh(QueueItem) || NeedRefresh)
  824. {
  825. Update = true;
  826. }
  827. if (Update)
  828. {
  829. FQueueController->RefreshQueueItem(QueueItem);
  830. }
  831. }
  832. if (Updated)
  833. {
  834. NonVisualDataModule->UpdateNonVisibleActions();
  835. SetQueueProgress();
  836. UpdateQueueFileList();
  837. }
  838. }
  839. }
  840. //---------------------------------------------------------------------------
  841. void __fastcall TCustomScpExplorerForm::SetTaskbarListProgressState(TBPFLAG Flags)
  842. {
  843. if (FTaskbarList != NULL)
  844. {
  845. // Could now use Application->MainFormHandle, now that we implement Application->OnGetMainFormHandle
  846. FTaskbarList->SetProgressState(GetMainForm()->Handle, Flags);
  847. }
  848. }
  849. //---------------------------------------------------------------------------
  850. void __fastcall TCustomScpExplorerForm::SetTaskbarListProgressValue(int Progress)
  851. {
  852. if (Progress >= 0)
  853. {
  854. if (FTaskbarList != NULL)
  855. {
  856. FTaskbarList->SetProgressValue(GetMainForm()->Handle, Progress, 100);
  857. }
  858. }
  859. else
  860. {
  861. SetTaskbarListProgressState(TBPF_INDETERMINATE);
  862. }
  863. }
  864. //---------------------------------------------------------------------------
  865. void __fastcall TCustomScpExplorerForm::SetTaskbarListProgressValue(TFileOperationProgressType * ProgressData)
  866. {
  867. int OverallProgress;
  868. // FProgressForm is null when this is called from SetQueueProgress
  869. if ((FProgressForm != NULL) && (FProgressForm->SynchronizeProgress != NULL))
  870. {
  871. OverallProgress = FProgressForm->SynchronizeProgress->Progress(ProgressData);
  872. }
  873. else if (!ProgressData->IsIndeterminate())
  874. {
  875. OverallProgress = ProgressData->OverallProgress();
  876. }
  877. else
  878. {
  879. OverallProgress = -1;
  880. }
  881. SetTaskbarListProgressValue(OverallProgress);
  882. }
  883. //---------------------------------------------------------------------------
  884. void __fastcall TCustomScpExplorerForm::SetQueueProgress()
  885. {
  886. TTerminalManager::Instance()->QueueStatusUpdated();
  887. if ((FTaskbarList != NULL) && (FProgressForm == NULL))
  888. {
  889. // Do not animate task bar button, when items are queued, but processing is disabled
  890. if ((FQueueStatus != NULL) && (FQueueStatus->ActivePrimaryCount > 0))
  891. {
  892. if (FQueueStatus->IsOnlyOneActiveAndNoPending())
  893. {
  894. TFileOperationProgressType * ProgressData = NULL;
  895. if (FQueueStatus->Items[FQueueStatus->DoneCount] != NULL)
  896. {
  897. ProgressData = FQueueStatus->Items[FQueueStatus->DoneCount]->ProgressData;
  898. }
  899. if ((ProgressData != NULL) &&
  900. ProgressData->InProgress)
  901. {
  902. SetTaskbarListProgressValue(ProgressData);
  903. }
  904. else
  905. {
  906. SetTaskbarListProgressState(TBPF_NOPROGRESS);
  907. }
  908. }
  909. else
  910. {
  911. SetTaskbarListProgressState(TBPF_INDETERMINATE);
  912. }
  913. }
  914. else
  915. {
  916. SetTaskbarListProgressState(TBPF_NOPROGRESS);
  917. }
  918. }
  919. UpdateQueueLabel();
  920. }
  921. //---------------------------------------------------------------------------
  922. void __fastcall TCustomScpExplorerForm::UpdateQueueLabel()
  923. {
  924. UnicodeString Caption = LoadStr(QUEUE_CAPTION);
  925. if (FQueueStatus != NULL)
  926. {
  927. int ActiveAndPendingPrimaryCount = FQueueStatus->ActiveAndPendingPrimaryCount;
  928. if (ActiveAndPendingPrimaryCount > 0)
  929. {
  930. Caption = FORMAT("%s (%d)", (Caption, ActiveAndPendingPrimaryCount));
  931. }
  932. }
  933. QueueLabel->Caption = Caption;
  934. }
  935. //---------------------------------------------------------------------------
  936. void TCustomScpExplorerForm::CheckStoreTransition()
  937. {
  938. if (WinConfiguration->StoreTransition == stInit)
  939. {
  940. if (IsUWP())
  941. {
  942. WinConfiguration->StoreTransition = stStoreFresh;
  943. AppLog(L"Fresh UWP/store installation");
  944. }
  945. else
  946. {
  947. WinConfiguration->StoreTransition = stStandard;
  948. AppLog(L"Fresh standard installation");
  949. }
  950. }
  951. else if ((WinConfiguration->StoreTransition == stStandard) && IsUWP())
  952. {
  953. WinConfiguration->StoreTransition = stStoreMigrated;
  954. WinConfiguration->FirstRun = WinConfiguration->FirstRun + L"=>" + StandardDatestamp();
  955. AppLog(L"Standard installation migrated to store installation");
  956. }
  957. if (WinConfiguration->StoreTransition == stStoreMigrated)
  958. {
  959. TTBXToolbar * Toolbar = new TTBXToolbar(MessageDock);
  960. Toolbar->Parent = MessageDock;
  961. Toolbar->FullSize = true;
  962. Toolbar->Color = Application->HintColor;
  963. TTBXLabelItem * LabelItem;
  964. LabelItem = new TTBXLabelItem(Toolbar);
  965. // Convert to explicit RGB "black" color, to make sure it is contrasting even in Dark theme
  966. // (TTBXOfficeXPTheme.GetStandardColor would convert clWindow to "white" in dark theme)
  967. LabelItem->FontSettings->Color = static_cast<TColor>(GetSysColor(clWindow));
  968. LabelItem->Margin = ScaleByTextHeight(MessageDock, 2); // Is this automatically rescaled?
  969. LabelItem->Caption = LoadStr(STORE_MIGRATION_THANKS);
  970. Toolbar->Items->Add(LabelItem);
  971. TTBXCustomItem * LinkItem = new TTBXItem(Toolbar);
  972. LinkItem->FontSettings->Color = LinkColor;
  973. LinkItem->Caption = LoadStr(STORE_MIGRATION_LEARN);
  974. LinkItem->OnClick = StoreTransitionLinkClick;
  975. Toolbar->Items->Add(LinkItem);
  976. AddMenuSeparator(Toolbar->Items);
  977. TTBXCustomItem * CloseItem = new TTBXItem(Toolbar);
  978. CloseItem->FontSettings->Color = static_cast<TColor>(GetSysColor(clWindow));
  979. CloseItem->Caption = LoadStr(MESSAGE_DISMISS);
  980. CloseItem->OnClick = StoreTransitionCloseClick;
  981. Toolbar->Items->Add(CloseItem);
  982. GiveTBItemPriority(CloseItem);
  983. }
  984. }
  985. //---------------------------------------------------------------------------
  986. void __fastcall TCustomScpExplorerForm::StoreTransitionLinkClick(TObject *)
  987. {
  988. ShowHelp(HELP_STORE_TRANSITION);
  989. }
  990. //---------------------------------------------------------------------------
  991. void __fastcall TCustomScpExplorerForm::StoreTransitionCloseClick(TObject * Sender)
  992. {
  993. TTBCustomItem * Item = DebugNotNull(dynamic_cast<TTBCustomItem *>(Sender));
  994. TTBXToolbar * Toolbar = DebugNotNull(dynamic_cast<TTBXToolbar *>(DebugNotNull(DebugNotNull(Item->Parent)->ParentComponent)));
  995. delete Toolbar;
  996. WinConfiguration->StoreTransition = stStoreAcknowledged;
  997. UpdateControls();
  998. }
  999. //---------------------------------------------------------------------------
  1000. void __fastcall TCustomScpExplorerForm::UpdateTransferList()
  1001. {
  1002. TTBXStringList * TransferList = dynamic_cast<TTBXStringList*>(
  1003. static_cast<TComponent*>(GetComponent(fcTransferList)));
  1004. TTBXDropDownItem * TransferDropDown = dynamic_cast<TTBXDropDownItem*>(
  1005. static_cast<TComponent*>(GetComponent(fcTransferDropDown)));
  1006. TransferList->Strings->BeginUpdate();
  1007. try
  1008. {
  1009. TransferList->Strings->Assign(GUIConfiguration->CopyParamList->NameList);
  1010. TransferList->Strings->Insert(0, StripHotkey(LoadStr(COPY_PARAM_DEFAULT)));
  1011. TransferList->ItemIndex = GUIConfiguration->CopyParamIndex + 1;
  1012. if (FTransferDropDownHint.IsEmpty())
  1013. {
  1014. FTransferDropDownHint = TransferDropDown->Hint;
  1015. }
  1016. // this way we get name for "default" settings (COPY_PARAM_DEFAULT)
  1017. UnicodeString Name = TransferList->Strings->Strings[TransferList->ItemIndex];
  1018. TransferDropDown->Text = StripHotkey(Name);
  1019. TransferDropDown->Hint = FORMAT(L"%s|%s:\n%s",
  1020. (FTransferDropDownHint, StripHotkey(Name),
  1021. GUIConfiguration->CurrentCopyParam.GetInfoStr(L"; ",
  1022. FLAGMASK(HasActiveTerminal(), Terminal->UsableCopyParamAttrs(0).General))));
  1023. // update the label, otherwise when it is updated only on the first draw
  1024. // of the list, it is drawn "bold" for some reason
  1025. FTransferListHoverIndex = TransferList->ItemIndex;
  1026. UpdateTransferLabel();
  1027. }
  1028. __finally
  1029. {
  1030. TransferList->Strings->EndUpdate();
  1031. }
  1032. }
  1033. //---------------------------------------------------------------------------
  1034. void __fastcall TCustomScpExplorerForm::UpdateCustomCommandsToolbar()
  1035. {
  1036. TTBXToolbar * Toolbar = dynamic_cast<TTBXToolbar *>(GetComponent(fcCustomCommandsBand));
  1037. DebugAssert(Toolbar != NULL);
  1038. NonVisualDataModule->UpdateCustomCommandsToolbar(Toolbar);
  1039. }
  1040. //---------------------------------------------------------------------------
  1041. void __fastcall TCustomScpExplorerForm::UpdateActions()
  1042. {
  1043. TForm::UpdateActions();
  1044. if (ComponentVisible[fcCustomCommandsBand])
  1045. {
  1046. UpdateCustomCommandsToolbar();
  1047. }
  1048. }
  1049. //---------------------------------------------------------------------------
  1050. void __fastcall TCustomScpExplorerForm::UpdateSessionsPageControlHeight()
  1051. {
  1052. SessionsPageControl->Height = SessionsPageControl->GetTabsHeight();
  1053. }
  1054. //---------------------------------------------------------------------------
  1055. void __fastcall TCustomScpExplorerForm::UpdateRowSelect(TCustomDirView * DirView)
  1056. {
  1057. if (DirView->RowSelect != WinConfiguration->FullRowSelect)
  1058. {
  1059. DirView->RowSelect = WinConfiguration->FullRowSelect;
  1060. // Without this, the panel turns gray and unused part of header turns black
  1061. DirView->Perform(CM_COLORCHANGED, 0, 0);
  1062. }
  1063. }
  1064. //---------------------------------------------------------------------------
  1065. void __fastcall TCustomScpExplorerForm::ConfigurationChanged()
  1066. {
  1067. Color = GetBtnFaceColor();
  1068. DebugAssert(Configuration && RemoteDirView);
  1069. RemoteDirView->DDAllowMove = !WinConfiguration->DDDisableMove;
  1070. RemoteDirView->DimmHiddenFiles = WinConfiguration->DimmHiddenFiles;
  1071. RemoteDirView->ShowHiddenFiles = WinConfiguration->ShowHiddenFiles;
  1072. RemoteDirView->FormatSizeBytes = WinConfiguration->FormatSizeBytes;
  1073. RemoteDirView->ShowInaccesibleDirectories = WinConfiguration->ShowInaccesibleDirectories;
  1074. RemoteDirView->NaturalOrderNumericalSorting = WinConfiguration->NaturalOrderNumericalSorting;
  1075. RemoteDirView->AlwaysSortDirectoriesByName = WinConfiguration->AlwaysSortDirectoriesByName;
  1076. UpdateRowSelect(RemoteDirView);
  1077. RemoteDriveView->DDAllowMove = !WinConfiguration->DDDisableMove;
  1078. RemoteDriveView->DimmHiddenDirs = WinConfiguration->DimmHiddenFiles;
  1079. RemoteDriveView->ShowHiddenDirs = WinConfiguration->ShowHiddenFiles;
  1080. RemoteDriveView->ShowInaccesibleDirectories = WinConfiguration->ShowInaccesibleDirectories;
  1081. RemoteDriveView->NaturalOrderNumericalSorting = WinConfiguration->NaturalOrderNumericalSorting;
  1082. UpdateSessionsPageControlHeight();
  1083. SetDockAllowDrag(!WinConfiguration->LockToolbars);
  1084. UpdateToolbarDisplayMode();
  1085. UpdateTransferList();
  1086. if (Terminal != NULL)
  1087. {
  1088. TransferPresetAutoSelect();
  1089. }
  1090. UpdateQueueView();
  1091. UpdateControls();
  1092. // this can be called even before constuctor finishes.
  1093. if (FEditorManager != NULL)
  1094. {
  1095. FEditorManager->ProcessFiles(FileConfigurationChanged, NULL);
  1096. }
  1097. // this can be called even before constuctor finishes.
  1098. if (FLocalEditors != NULL)
  1099. {
  1100. for (int Index = 0; Index < FLocalEditors->Count; Index++)
  1101. {
  1102. ReconfigureEditorForm(static_cast<TForm *>(FLocalEditors->Items[Index]));
  1103. }
  1104. }
  1105. if (ComponentVisible[fcCustomCommandsBand])
  1106. {
  1107. UpdateCustomCommandsToolbar();
  1108. }
  1109. // show only when keeping queue items forever,
  1110. // otherwise, it's enough to have it in the context menu
  1111. QueueDeleteAllDoneQueueToolbarItem->Visible =
  1112. WinConfiguration->QueueKeepDoneItems && (WinConfiguration->QueueKeepDoneItemsFor < 0);
  1113. if (FFileColorsCurrent != WinConfiguration->FileColors)
  1114. {
  1115. TFileColorData::LoadList(WinConfiguration->FileColors, FFileColors);
  1116. FFileColorsCurrent = WinConfiguration->FileColors;
  1117. FileColorsChanged();
  1118. }
  1119. if ((GetNewTabTab()->CaptionTruncation != GetNewTabTabCaptionTruncation()) &&
  1120. (FSessionColors != NULL))
  1121. {
  1122. SessionListChanged();
  1123. }
  1124. if (GlyphsModule->LargerToolbar != WinConfiguration->LargerToolbar)
  1125. {
  1126. GlyphsModule->LargerToolbar = WinConfiguration->LargerToolbar;
  1127. if (FSessionColors != NULL)
  1128. {
  1129. RegenerateSessionColorsImageList();
  1130. UpdateSessionsPageControlHeight();
  1131. }
  1132. }
  1133. }
  1134. //---------------------------------------------------------------------------
  1135. void __fastcall TCustomScpExplorerForm::DoFileColorsChanged(TCustomDirView * DirView)
  1136. {
  1137. DirView->OnGetItemColor = FFileColors.empty() ? TDirViewGetItemColorEvent(NULL) : TDirViewGetItemColorEvent(&DirViewGetItemColor);
  1138. DirView->Invalidate();
  1139. }
  1140. //---------------------------------------------------------------------------
  1141. void __fastcall TCustomScpExplorerForm::FileColorsChanged()
  1142. {
  1143. DoFileColorsChanged(RemoteDirView);
  1144. }
  1145. //---------------------------------------------------------------------------
  1146. void __fastcall TCustomScpExplorerForm::FileConfigurationChanged(
  1147. const UnicodeString FileName, TEditedFileData * /*Data*/, TObject * Token,
  1148. void * /*Arg*/)
  1149. {
  1150. if (Token != NULL)
  1151. {
  1152. TForm * Editor = dynamic_cast<TForm*>(Token);
  1153. DebugAssert(Editor != NULL);
  1154. ReconfigureEditorForm(Editor);
  1155. }
  1156. }
  1157. //---------------------------------------------------------------------------
  1158. void __fastcall TCustomScpExplorerForm::EnableDDTransferConfirmation(TObject * /*Sender*/)
  1159. {
  1160. WinConfiguration->DDTransferConfirmation = asOn;
  1161. }
  1162. //---------------------------------------------------------------------------
  1163. void __fastcall TCustomScpExplorerForm::CopyParamDialogAfter(
  1164. TTransferDirection /*Direction*/, bool /*Temp*/, const UnicodeString & /*TargetDirectory*/)
  1165. {
  1166. // noop
  1167. }
  1168. //---------------------------------------------------------------------------
  1169. bool TCustomScpExplorerForm::GetDoNotShowCopyDialogDefault(bool DragDrop)
  1170. {
  1171. return DragDrop && (WinConfiguration->DDTransferConfirmation == asAuto);
  1172. }
  1173. //---------------------------------------------------------------------------
  1174. void TCustomScpExplorerForm::HandleDoNotShowCopyDialogAgain(bool DragDrop, bool DoNotShowAgain)
  1175. {
  1176. if (DoNotShowAgain)
  1177. {
  1178. if (DragDrop)
  1179. {
  1180. if (WinConfiguration->DDTransferConfirmation == asAuto)
  1181. {
  1182. PopupTrayBalloon(
  1183. NULL, LoadStr(DD_TRANSFER_CONFIRM_OFF2), qtInformation, NULL, 0, EnableDDTransferConfirmation, NULL);
  1184. }
  1185. WinConfiguration->DDTransferConfirmation = asOff;
  1186. }
  1187. else
  1188. {
  1189. WinConfiguration->ConfirmTransferring = false;
  1190. }
  1191. }
  1192. else
  1193. {
  1194. // User explicitly unchecked "do not show again",
  1195. // so show him the dialog the next time
  1196. if (DragDrop && (WinConfiguration->DDTransferConfirmation == asAuto))
  1197. {
  1198. WinConfiguration->DDTransferConfirmation = asOn;
  1199. }
  1200. }
  1201. }
  1202. //---------------------------------------------------------------------------
  1203. bool __fastcall TCustomScpExplorerForm::CopyParamDialog(
  1204. TTransferDirection Direction, TTransferType Type, bool Temp,
  1205. TStrings * FileList, UnicodeString & TargetDirectory, TGUICopyParamType & CopyParam,
  1206. bool Confirm, bool DragDrop, int Options)
  1207. {
  1208. bool Result = true;
  1209. DebugAssert(HasActiveTerminal());
  1210. // these parameters are known in advance
  1211. int Params =
  1212. FLAGMASK(Type == ttMove, cpDelete);
  1213. bool ToTemp = (Temp && (Direction == tdToLocal));
  1214. if (Result && Confirm && WinConfiguration->ConfirmTransferring)
  1215. {
  1216. bool DisableNewerOnly =
  1217. (!Terminal->IsCapable[fcNewerOnlyUpload] && (Direction == tdToRemote)) ||
  1218. ToTemp;
  1219. Options |=
  1220. FLAGMASK(ToTemp, coTemp) |
  1221. FLAGMASK(!Terminal->IsCapable[fcBackgroundTransfers], coDisableQueue) |
  1222. coDoNotShowAgain;
  1223. TUsableCopyParamAttrs UsableCopyParamAttrs = Terminal->UsableCopyParamAttrs(Params);
  1224. int CopyParamAttrs = (Direction == tdToRemote ?
  1225. UsableCopyParamAttrs.Upload : UsableCopyParamAttrs.Download) |
  1226. FLAGMASK(DisableNewerOnly, cpaNoNewerOnly);
  1227. int OutputOptions = FLAGMASK(GetDoNotShowCopyDialogDefault(DragDrop), cooDoNotShowAgain);
  1228. std::unique_ptr<TSessionData> SessionData(SessionDataForCode());
  1229. FlashOnBackground(); // Particularly when called from ClipboardFakeCreated
  1230. Result = DoCopyDialog(Direction == tdToRemote, Type == ttMove,
  1231. FileList, TargetDirectory, &CopyParam, Options, CopyParamAttrs, SessionData.get(), &OutputOptions, -1);
  1232. if (Result)
  1233. {
  1234. HandleDoNotShowCopyDialogAgain(DragDrop, FLAGSET(OutputOptions, cooDoNotShowAgain));
  1235. CopyParamDialogAfter(Direction, Temp, TargetDirectory);
  1236. }
  1237. }
  1238. if (Result && CopyParam.Queue && !ToTemp && Terminal->IsCapable[fcBackgroundTransfers])
  1239. {
  1240. Configuration->Usage->Inc(L"TransfersOnBackground");
  1241. // these parameter are known only after transfer dialog
  1242. Params |=
  1243. (CopyParam.QueueNoConfirmation ? cpNoConfirmation : 0);
  1244. AddQueueItem(Queue, Direction, FileList, TargetDirectory, CopyParam, Params);
  1245. Result = false;
  1246. ClearTransferSourceSelection(Direction);
  1247. }
  1248. return Result;
  1249. }
  1250. //---------------------------------------------------------------------------
  1251. void TCustomScpExplorerForm::ClearOperationSelection(TOperationSide Side)
  1252. {
  1253. if (FOnFileOperationFinished != NULL)
  1254. {
  1255. FOnFileOperationFinished(Side, UnicodeString(), true, true);
  1256. }
  1257. else
  1258. {
  1259. if (HasDirView[Side])
  1260. {
  1261. DirView(Side)->SelectAll(smNone);
  1262. }
  1263. }
  1264. }
  1265. //---------------------------------------------------------------------------
  1266. void __fastcall TCustomScpExplorerForm::ClearTransferSourceSelection(TTransferDirection Direction)
  1267. {
  1268. TOperationSide Side = ((Direction == tdToRemote) ? osLocal : osRemote);
  1269. ClearOperationSelection(Side);
  1270. }
  1271. //---------------------------------------------------------------------------
  1272. void __fastcall TCustomScpExplorerForm::AddQueueItem(
  1273. TTerminalQueue * Queue, TTransferDirection Direction, TStrings * FileList,
  1274. const UnicodeString TargetDirectory, TGUICopyParamType & CopyParam,
  1275. int Params)
  1276. {
  1277. DebugAssert(Queue != NULL);
  1278. TQueueItem * QueueItem;
  1279. if (Direction == tdToRemote)
  1280. {
  1281. CopyParam.IncludeFileMask.SetRoots(FileList, TargetDirectory);
  1282. QueueItem =
  1283. new TUploadQueueItem(
  1284. Terminal, FileList, TargetDirectory, &CopyParam, Params, CopyParam.QueueParallel);
  1285. }
  1286. else
  1287. {
  1288. CopyParam.IncludeFileMask.SetRoots(TargetDirectory, FileList);
  1289. QueueItem =
  1290. new TDownloadQueueItem(
  1291. Terminal, FileList, TargetDirectory, &CopyParam, Params, CopyParam.QueueParallel);
  1292. }
  1293. AddQueueItem(Queue, QueueItem, Terminal);
  1294. }
  1295. //---------------------------------------------------------------------------
  1296. void __fastcall TCustomScpExplorerForm::AddQueueItem(TTerminalQueue * Queue, TQueueItem * QueueItem, TManagedTerminal * Terminal)
  1297. {
  1298. if (Queue->IsEmpty)
  1299. {
  1300. Terminal->QueueOperationStart = Now();
  1301. }
  1302. Queue->AddItem(QueueItem);
  1303. }
  1304. //---------------------------------------------------------------------------
  1305. void __fastcall TCustomScpExplorerForm::RestoreFormParams()
  1306. {
  1307. }
  1308. //---------------------------------------------------------------------------
  1309. void __fastcall TCustomScpExplorerForm::InitControls()
  1310. {
  1311. DebugAssert(FDocks != NULL);
  1312. for (int Index = 0; Index < ComponentCount; Index++)
  1313. {
  1314. TTBDock * Dock = dynamic_cast<TTBDock *>(Components[Index]);
  1315. if ((Dock != NULL) && (Dock->Tag == 0))
  1316. {
  1317. FDocks->Add(Dock);
  1318. }
  1319. }
  1320. CollectItemsWithTextDisplayMode(this);
  1321. }
  1322. //---------------------------------------------------------------------------
  1323. void __fastcall TCustomScpExplorerForm::RestoreParams()
  1324. {
  1325. ConfigurationChanged();
  1326. QueuePanel->Height =
  1327. LoadDimension(WinConfiguration->QueueView.Height, WinConfiguration->QueueView.HeightPixelsPerInch, this);
  1328. LoadListViewStr(QueueView3, WinConfiguration->QueueView.Layout);
  1329. QueueDock->Visible = WinConfiguration->QueueView.ToolBar;
  1330. QueueLabel->Visible = WinConfiguration->QueueView.Label;
  1331. QueueFileList->Visible = WinConfiguration->QueueView.FileList;
  1332. QueueFileList->Height =
  1333. LoadDimension(WinConfiguration->QueueView.FileListHeight, WinConfiguration->QueueView.FileListHeightPixelsPerInch, this);
  1334. if (QueueFileList->Visible)
  1335. {
  1336. AdjustQueueLayout();
  1337. }
  1338. UpdateCustomCommandsToolbar();
  1339. }
  1340. //---------------------------------------------------------------------------
  1341. void __fastcall TCustomScpExplorerForm::StoreParams()
  1342. {
  1343. int APixelsPerInch = GetControlPixelsPerInch(this);
  1344. WinConfiguration->QueueView.Height = QueuePanel->Height;
  1345. WinConfiguration->QueueView.HeightPixelsPerInch = APixelsPerInch;
  1346. WinConfiguration->QueueView.Layout = GetListViewStr(QueueView3);
  1347. WinConfiguration->QueueView.ToolBar = QueueDock->Visible;
  1348. WinConfiguration->QueueView.Label = QueueLabel->Visible;
  1349. WinConfiguration->QueueView.FileList = QueueFileList->Visible;
  1350. WinConfiguration->QueueView.FileListHeight = QueueFileList->Height;
  1351. WinConfiguration->QueueView.FileListHeightPixelsPerInch = APixelsPerInch;
  1352. }
  1353. //---------------------------------------------------------------------------
  1354. void __fastcall TCustomScpExplorerForm::Loaded()
  1355. {
  1356. // Loaded is called in more deterministic moment than previously used CreateParams
  1357. if (!FFormRestored)
  1358. {
  1359. FFormRestored = true;
  1360. RestoreFormParams();
  1361. }
  1362. TForm::Loaded();
  1363. }
  1364. //---------------------------------------------------------------------------
  1365. void __fastcall TCustomScpExplorerForm::SetDockAllowDrag(bool value)
  1366. {
  1367. DebugAssert(FDocks != NULL);
  1368. for (int Index = 0; Index < FDocks->Count; Index++)
  1369. {
  1370. static_cast<TTBDock*>(FDocks->Items[Index])->AllowDrag = value;
  1371. }
  1372. }
  1373. //---------------------------------------------------------------------------
  1374. void __fastcall TCustomScpExplorerForm::LoadToolbarsLayoutStr(UnicodeString LayoutStr, UnicodeString ButtonsStr)
  1375. {
  1376. SetDockAllowDrag(true);
  1377. while (!ButtonsStr.IsEmpty())
  1378. {
  1379. UnicodeString S = CutToChar(ButtonsStr, L';', true);
  1380. UnicodeString Name = CutToChar(S, L'=', true);
  1381. for (int ToolbarIndex = 0; ToolbarIndex < ComponentCount; ToolbarIndex++)
  1382. {
  1383. TTBCustomToolbar * Toolbar = dynamic_cast<TTBCustomToolbar *>(Components[ToolbarIndex]);
  1384. if ((Toolbar != NULL) && SameText(Name, GetToolbarKey(Toolbar->Name)))
  1385. {
  1386. while (!S.IsEmpty())
  1387. {
  1388. UnicodeString S2 = CutToChar(S, L',', true);
  1389. UnicodeString Name = CutToChar(S2, L':', true);
  1390. for (int ItemIndex = 0; ItemIndex < Toolbar->Items->Count; ItemIndex++)
  1391. {
  1392. TTBCustomItem * Item = Toolbar->Items->Items[ItemIndex];
  1393. if (SameText(GetToolbarItemName(Item), Name))
  1394. {
  1395. // forward compatibility
  1396. UnicodeString S3 = CutToChar(S2, L':', true);
  1397. bool Visible = StrToIntDef(S3, 0);
  1398. Item->Visible = Visible;
  1399. }
  1400. }
  1401. }
  1402. }
  1403. }
  1404. }
  1405. ::LoadToolbarsLayoutStr(this, LayoutStr);
  1406. SetDockAllowDrag(!WinConfiguration->LockToolbars);
  1407. }
  1408. //---------------------------------------------------------------------------
  1409. UnicodeString __fastcall TCustomScpExplorerForm::GetToolbarsLayoutStr()
  1410. {
  1411. UnicodeString Result;
  1412. SetDockAllowDrag(true);
  1413. Result = ::GetToolbarsLayoutStr(this);
  1414. SetDockAllowDrag(!WinConfiguration->LockToolbars);
  1415. return Result;
  1416. }
  1417. //---------------------------------------------------------------------------
  1418. UnicodeString __fastcall TCustomScpExplorerForm::GetToolbarItemName(TTBCustomItem * Item)
  1419. {
  1420. UnicodeString Result;
  1421. if (Item->Action != NULL)
  1422. {
  1423. Result = Item->Action->Name;
  1424. Result = RemoveSuffix(Result, L"Action", true);
  1425. }
  1426. else
  1427. {
  1428. Result = Item->Name;
  1429. Result = RemoveSuffix(Result, L"SubmenuItem", true);
  1430. Result = RemoveSuffix(Result, L"Item", true);
  1431. }
  1432. return Result;
  1433. }
  1434. //---------------------------------------------------------------------------
  1435. UnicodeString __fastcall TCustomScpExplorerForm::GetToolbarsButtonsStr()
  1436. {
  1437. UnicodeString Result;
  1438. for (int ToolbarIndex = 0; ToolbarIndex < ComponentCount; ToolbarIndex++)
  1439. {
  1440. UnicodeString ToolbarStr;
  1441. TTBCustomToolbar * Toolbar = dynamic_cast<TTBCustomToolbar *>(Components[ToolbarIndex]);
  1442. if ((Toolbar != NULL) && (Toolbar != QueueToolbar))
  1443. {
  1444. for (int ItemIndex = 0; ItemIndex < Toolbar->Items->Count; ItemIndex++)
  1445. {
  1446. TTBCustomItem * Item = Toolbar->Items->Items[ItemIndex];
  1447. // Currently all buttons are visible by default, so we can safely remember all hidden buttons.
  1448. // Once we introduce any buttons that are hidden by default, we would have to remember their initial state
  1449. // and save the changes here only.
  1450. if (NonVisualDataModule->IsCustomizableToolbarItem(Item) && !Item->Visible)
  1451. {
  1452. UnicodeString Name = GetToolbarItemName(Item);
  1453. DebugAssert(Name.Pos(L"TBX") == 0);
  1454. AddToList(ToolbarStr, FORMAT(L"%s:0", (Name)), L",");
  1455. }
  1456. }
  1457. }
  1458. if (!ToolbarStr.IsEmpty())
  1459. {
  1460. AddToList(Result, FORMAT(L"%s=%s", (GetToolbarKey(Toolbar->Name), ToolbarStr)), L";");
  1461. }
  1462. }
  1463. return Result;
  1464. }
  1465. //---------------------------------------------------------------------------
  1466. void __fastcall TCustomScpExplorerForm::CreateProgressForm(TSynchronizeProgress * SynchronizeProgress)
  1467. {
  1468. DebugAssert(FProgressForm == NULL);
  1469. bool AllowSkip = HasActiveTerminal() ? Terminal->IsCapable[fcSkipTransfer] : false;
  1470. bool AllowMoveToQueue = (FTransferResumeList != NULL) || (FDeletedFiles != NULL);
  1471. FProgressForm = new TProgressForm(Application, AllowMoveToQueue, AllowSkip, SynchronizeProgress);
  1472. FProgressForm->DeleteLocalToRecycleBin =
  1473. (WinConfiguration->DeleteToRecycleBin != FAlternativeDelete);
  1474. FProgressForm->DeleteRemoteToRecycleBin =
  1475. HasActiveTerminal() &&
  1476. (Terminal->SessionData->DeleteToRecycleBin != FAlternativeDelete) &&
  1477. !Terminal->SessionData->RecycleBinPath.IsEmpty();
  1478. // As progress window has delayed show now, we need to lock ourselves, (at least) until then
  1479. LockWindow();
  1480. // Actually, do not know what hides the progress once the operation finishes
  1481. // (it is possibly the SetQueueProgress - and we should not rely on that)
  1482. SetTaskbarListProgressState(TBPF_NORMAL);
  1483. }
  1484. //---------------------------------------------------------------------------
  1485. void __fastcall TCustomScpExplorerForm::DestroyProgressForm()
  1486. {
  1487. UnlockWindow();
  1488. SAFE_DESTROY(FProgressForm);
  1489. SetQueueProgress();
  1490. }
  1491. //---------------------------------------------------------------------------
  1492. void __fastcall TCustomScpExplorerForm::FileOperationProgress(
  1493. TFileOperationProgressType & ProgressData)
  1494. {
  1495. // Download to temporary local directory
  1496. if (FPendingTempSpaceWarn && ProgressData.InProgress && ProgressData.TotalSizeSet)
  1497. {
  1498. bool Continue = true;
  1499. FPendingTempSpaceWarn = false;
  1500. DoWarnLackOfTempSpace(ProgressData.Directory, ProgressData.TotalSize, Continue);
  1501. if (!Continue)
  1502. {
  1503. Abort();
  1504. }
  1505. }
  1506. // operation is being executed and we still didn't show up progress form
  1507. if (ProgressData.InProgress && !FProgressForm)
  1508. {
  1509. CreateProgressForm(NULL);
  1510. DebugAssert(FProgressForm->OnceDoneOperation == odoIdle);
  1511. FProgressForm->OnceDoneOperation = ProgressData.InitialOnceDoneOperation;
  1512. }
  1513. // operation is finished (or terminated), so we hide progress form
  1514. else if (!ProgressData.InProgress && (FProgressForm != NULL) && (FProgressForm->SynchronizeProgress == NULL))
  1515. {
  1516. DestroyProgressForm();
  1517. if (ProgressData.Operation == foCalculateSize)
  1518. {
  1519. // When calculating size before transfer, the abort caused by
  1520. // cancel flag set due to "MoveToQueue" below,
  1521. // is not propagated back to us in ExecuteFileOperation,
  1522. // as it is silently swallowed.
  1523. // So we explicitly (re)throw it here.
  1524. if (FMoveToQueue)
  1525. {
  1526. Abort();
  1527. }
  1528. }
  1529. else
  1530. {
  1531. if ((ProgressData.Cancel == csContinue) &&
  1532. !FAutoOperation)
  1533. {
  1534. OperationComplete(ProgressData.StartTime);
  1535. }
  1536. }
  1537. }
  1538. if (FProgressForm &&
  1539. // Can happen only during synchronization, when ending one of the operations
  1540. ProgressData.InProgress)
  1541. {
  1542. FProgressForm->SetProgressData(ProgressData);
  1543. DebugAssert(ProgressData.InProgress);
  1544. SetTaskbarListProgressValue(&ProgressData);
  1545. if (FProgressForm->Cancel > csContinue)
  1546. {
  1547. ProgressData.SetCancelAtLeast(FProgressForm->Cancel);
  1548. if (FProgressForm->Cancel == csCancelFile)
  1549. {
  1550. FProgressForm->ClearCancel();
  1551. }
  1552. // cancel cancels even the move
  1553. FMoveToQueue = false;
  1554. }
  1555. else if (FProgressForm->MoveToQueue)
  1556. {
  1557. FMoveToQueue = true;
  1558. ProgressData.SetCancelAtLeast(csCancel);
  1559. }
  1560. if ((FTransferResumeList != NULL) &&
  1561. ProgressData.InProgress &&
  1562. ProgressData.IsTransfer() &&
  1563. !ProgressData.FullFileName.IsEmpty())
  1564. {
  1565. if ((FTransferResumeList->Count == 0) ||
  1566. (FTransferResumeList->Strings[FTransferResumeList->Count - 1] != ProgressData.FullFileName))
  1567. {
  1568. // note that we do not recognize directories from files here
  1569. FTransferResumeList->Add(ProgressData.FullFileName);
  1570. }
  1571. }
  1572. }
  1573. }
  1574. //---------------------------------------------------------------------------
  1575. void __fastcall TCustomScpExplorerForm::OperationComplete(
  1576. const TDateTime & StartTime)
  1577. {
  1578. if (GUIConfiguration->BeepOnFinish &&
  1579. (Now() - StartTime > GUIConfiguration->BeepOnFinishAfter))
  1580. {
  1581. UnicodeString BeepSound = GUIConfiguration->BeepSound;
  1582. DWORD Sound;
  1583. if (!ExtractFileExt(BeepSound).IsEmpty())
  1584. {
  1585. Sound = SND_FILENAME;
  1586. }
  1587. else
  1588. {
  1589. Sound = SND_ALIAS;
  1590. }
  1591. PlaySound(BeepSound.c_str(), NULL, Sound | SND_ASYNC);
  1592. }
  1593. }
  1594. //---------------------------------------------------------------------------
  1595. void __fastcall TCustomScpExplorerForm::OperationProgress(
  1596. TFileOperationProgressType & ProgressData)
  1597. {
  1598. // For example, when calling TFileOperationProgressType::Suspend in TTerminal::QueryReopen,
  1599. // we cannot recurse back to TTerminal::Idle as that may detect another error, calling "suspend" again.
  1600. TAutoNestingCounter Counter(FDoNotIdleCurrentTerminal);
  1601. FileOperationProgress(ProgressData);
  1602. }
  1603. //---------------------------------------------------------------------------
  1604. UnicodeString __fastcall TCustomScpExplorerForm::GetProgressTitle()
  1605. {
  1606. UnicodeString Result;
  1607. if (FProgressForm != NULL)
  1608. {
  1609. Result = FProgressForm->ProgressStr();
  1610. }
  1611. return Result;
  1612. }
  1613. //---------------------------------------------------------------------------
  1614. bool __fastcall TCustomScpExplorerForm::PanelOperation(TOperationSide /*Side*/,
  1615. bool DragDrop)
  1616. {
  1617. return (!DragDrop && (DropSourceControl == NULL)) ||
  1618. (DropSourceControl == DirView(osOther));
  1619. }
  1620. //---------------------------------------------------------------------------
  1621. TListItem * TCustomScpExplorerForm::VisualiseOperationFinished(TOperationSide Side, const UnicodeString & FileName, bool Unselect)
  1622. {
  1623. TCustomDirView * ADirView = DirView(Side);
  1624. UnicodeString FileNameOnly = ExtractFileName(FileName, !IsSideLocalBrowser(Side));
  1625. TListItem * Item = ADirView->FindFileItem(FileNameOnly);
  1626. // this can happen when local drive is unplugged in the middle of the operation
  1627. if (Item != NULL)
  1628. {
  1629. if (Unselect)
  1630. {
  1631. Item->Selected = false;
  1632. }
  1633. ADirView->MakeProgressVisible(Item);
  1634. }
  1635. return Item;
  1636. }
  1637. //---------------------------------------------------------------------------
  1638. void __fastcall TCustomScpExplorerForm::DoOperationFinished(
  1639. TFileOperation Operation, TOperationSide Side,
  1640. bool /*Temp*/, const UnicodeString & FileName, bool Success, bool NotCancelled,
  1641. TOnceDoneOperation & OnceDoneOperation)
  1642. {
  1643. if (!FAutoOperation)
  1644. {
  1645. // no selection on "/upload", form serves only as event handler
  1646. // (it is not displayed)
  1647. if (PanelOperation(Side, FDragDropOperation) && Visible &&
  1648. ((Operation == FPrimaryOperation) ||
  1649. ((Operation != foCalculateSize) && (Operation != foGetProperties) && (Operation != foCalculateChecksum))))
  1650. {
  1651. TOperationSide DViewSide;
  1652. if ((FProgressSide != osCurrent) &&
  1653. IsLocalBrowserMode() && (Side == osLocal)) // Only to limit the impact
  1654. {
  1655. // assume the operation is over the focused panel
  1656. DViewSide = FProgressSide;
  1657. }
  1658. else
  1659. {
  1660. DViewSide = Side;
  1661. }
  1662. if (FOnFileOperationFinished != NULL)
  1663. {
  1664. FOnFileOperationFinished(DViewSide, FileName, Success, NotCancelled);
  1665. }
  1666. else
  1667. {
  1668. VisualiseOperationFinished(DViewSide, FileName, Success && NotCancelled);
  1669. }
  1670. }
  1671. if (TFileOperationProgressType::IsTransferOperation(Operation))
  1672. {
  1673. if (DebugAlwaysTrue(!IsLocalBrowserMode()))
  1674. {
  1675. if (Side == osLocal)
  1676. {
  1677. Configuration->Usage->Inc(L"UploadedFiles");
  1678. }
  1679. else
  1680. {
  1681. Configuration->Usage->Inc(L"DownloadedFiles");
  1682. }
  1683. }
  1684. }
  1685. }
  1686. if (Success && (FSynchronizeController != NULL))
  1687. {
  1688. DebugAssert(!IsSideLocalBrowser(osRemote));
  1689. if (Operation == foCopy)
  1690. {
  1691. DebugAssert(Side == osLocal);
  1692. FSynchronizeController->LogOperation(soUpload, FileName);
  1693. }
  1694. else if (Operation == foDelete)
  1695. {
  1696. DebugAssert(Side == osRemote);
  1697. FSynchronizeController->LogOperation(soDelete, FileName);
  1698. }
  1699. }
  1700. if (FProgressForm)
  1701. {
  1702. OnceDoneOperation = FProgressForm->OnceDoneOperation;
  1703. }
  1704. }
  1705. //---------------------------------------------------------------------------
  1706. void __fastcall TCustomScpExplorerForm::OperationFinished(
  1707. TFileOperation Operation, TOperationSide Side,
  1708. bool Temp, const UnicodeString & FileName, bool Success, bool NotCancelled,
  1709. TOnceDoneOperation & OnceDoneOperation)
  1710. {
  1711. TAutoNestingCounter Counter(FDoNotIdleCurrentTerminal);
  1712. DoOperationFinished(Operation, Side, Temp, FileName, Success, NotCancelled, OnceDoneOperation);
  1713. }
  1714. //---------------------------------------------------------------------------
  1715. bool TCustomScpExplorerForm::IsLocalBrowserMode()
  1716. {
  1717. return false;
  1718. }
  1719. //---------------------------------------------------------------------------
  1720. bool TCustomScpExplorerForm::SupportsLocalBrowser()
  1721. {
  1722. return false;
  1723. }
  1724. //---------------------------------------------------------------------------
  1725. bool TCustomScpExplorerForm::IsSideLocalBrowser(TOperationSide)
  1726. {
  1727. return false;
  1728. }
  1729. //---------------------------------------------------------------------------
  1730. TCustomDirView * TCustomScpExplorerForm::GetCurrentLocalBrowser()
  1731. {
  1732. return NULL;
  1733. }
  1734. //---------------------------------------------------------------------------
  1735. TCustomDirView * __fastcall TCustomScpExplorerForm::DirView(TOperationSide Side)
  1736. {
  1737. DebugAssert(GetSide(Side) == osRemote);
  1738. DebugUsedParam(Side);
  1739. return RemoteDirView;
  1740. }
  1741. //---------------------------------------------------------------------------
  1742. TCustomDriveView * __fastcall TCustomScpExplorerForm::DriveView(TOperationSide Side)
  1743. {
  1744. DebugAssert(GetSide(Side) == osRemote);
  1745. DebugUsedParam(Side);
  1746. return RemoteDriveView;
  1747. }
  1748. //---------------------------------------------------------------------------
  1749. bool __fastcall TCustomScpExplorerForm::DirViewEnabled(TOperationSide Side)
  1750. {
  1751. DebugAssert(GetSide(Side) == osRemote);
  1752. DebugUsedParam(Side);
  1753. return HasActiveTerminal();
  1754. }
  1755. //---------------------------------------------------------------------------
  1756. bool __fastcall TCustomScpExplorerForm::GetEnableFocusedOperation(
  1757. TOperationSide Side, int FilesOnly)
  1758. {
  1759. return DirView(Side)->AnyFileSelected(true, (FilesOnly != 0), true);
  1760. }
  1761. //---------------------------------------------------------------------------
  1762. bool __fastcall TCustomScpExplorerForm::GetEnableSelectedOperation(
  1763. TOperationSide Side, int FilesOnly)
  1764. {
  1765. return DirView(Side)->AnyFileSelected(false, (FilesOnly != 0), true);
  1766. }
  1767. //---------------------------------------------------------------------------
  1768. struct THistoryItemData
  1769. {
  1770. short int Side;
  1771. short int Index;
  1772. };
  1773. //---------------------------------------------------------------------------
  1774. void __fastcall TCustomScpExplorerForm::HistoryGo(TOperationSide Side, int Index)
  1775. {
  1776. DirView(Side)->HistoryGo(Index);
  1777. }
  1778. //---------------------------------------------------------------------------
  1779. void __fastcall TCustomScpExplorerForm::HistoryItemClick(System::TObject* Sender)
  1780. {
  1781. TTBCustomItem * Item = dynamic_cast<TTBCustomItem *>(Sender);
  1782. THistoryItemData Data = *reinterpret_cast<THistoryItemData*>(&(Item->Tag));
  1783. HistoryGo((TOperationSide)Data.Side, Data.Index);
  1784. }
  1785. //---------------------------------------------------------------------------
  1786. void __fastcall TCustomScpExplorerForm::UpdateHistoryMenu(TOperationSide Side,
  1787. bool Back)
  1788. {
  1789. if (FHistoryMenu[Side == osLocal][Back] != NULL)
  1790. {
  1791. TCustomDirView * DView = DirView(Side);
  1792. TTBXPopupMenu * Menu = FHistoryMenu[Side == osLocal][Back];
  1793. int ICount = Back ? DView->BackCount : DView->ForwardCount;
  1794. if (ICount > 10)
  1795. {
  1796. ICount = 10;
  1797. }
  1798. Menu->Items->Clear();
  1799. THistoryItemData Data;
  1800. Data.Side = Side;
  1801. for (short int i = 1; i <= ICount; i++)
  1802. {
  1803. TTBCustomItem * Item = new TTBXItem(Menu);
  1804. Data.Index = static_cast<short int>(i * (Back ? -1 : 1));
  1805. Item->Caption = MinimizeName(DView->HistoryPath[Data.Index], 50, !IsSideLocalBrowser(Side));
  1806. Item->Hint = DView->HistoryPath[Data.Index];
  1807. DebugAssert(sizeof(int) == sizeof(THistoryItemData));
  1808. Item->Tag = *reinterpret_cast<int*>(&Data);
  1809. Item->OnClick = HistoryItemClick;
  1810. Menu->Items->Add(Item);
  1811. }
  1812. }
  1813. }
  1814. //---------------------------------------------------------------------------
  1815. TTBXPopupMenu * __fastcall TCustomScpExplorerForm::HistoryMenu(
  1816. TOperationSide Side, bool Back)
  1817. {
  1818. if (FHistoryMenu[Side == osLocal][Back] == NULL)
  1819. {
  1820. // workaround
  1821. // In Pascal the size of TTBXPopupMenu is 132, in C++ 136,
  1822. // operator new allocates memory in Pascal code, but calls inline
  1823. // constructor in C++, leading in problems, the function does
  1824. // both in Pascal code
  1825. FHistoryMenu[Side == osLocal][Back] = CreateTBXPopupMenu(this);
  1826. UpdateHistoryMenu(Side, Back);
  1827. }
  1828. return FHistoryMenu[Side == osLocal][Back];
  1829. }
  1830. //---------------------------------------------------------------------------
  1831. void __fastcall TCustomScpExplorerForm::DirViewHistoryChange(
  1832. TCustomDirView *Sender)
  1833. {
  1834. TOperationSide Side = (Sender == DirView(osOther) ? osOther : osLocal);
  1835. UpdateHistoryMenu(Side, true);
  1836. UpdateHistoryMenu(Side, false);
  1837. }
  1838. //---------------------------------------------------------------------------
  1839. bool __fastcall TCustomScpExplorerForm::CustomCommandRemoteAllowed()
  1840. {
  1841. // remote custom commands can be executed only if the server supports shell commands
  1842. // or have secondary shell
  1843. return HasActiveTerminal() && (Terminal->IsCapable[fcSecondaryShell] || Terminal->IsCapable[fcShellAnyCommand]);
  1844. }
  1845. //---------------------------------------------------------------------------
  1846. int __fastcall TCustomScpExplorerForm::CustomCommandState(
  1847. const TCustomCommandType & Command, bool OnFocused, TCustomCommandListType ListType)
  1848. {
  1849. // -1 = hidden, 0 = disabled, 1 = enabled
  1850. int Result;
  1851. TFileCustomCommand RemoteCustomCommand;
  1852. TLocalCustomCommand LocalCustomCommand;
  1853. TFileCustomCommand * NonInteractiveCustomCommand =
  1854. FLAGCLEAR(Command.Params, ccLocal) ? &RemoteCustomCommand : &LocalCustomCommand;
  1855. TInteractiveCustomCommand InteractiveCustomCommand(NonInteractiveCustomCommand);
  1856. UnicodeString Cmd = InteractiveCustomCommand.Complete(Command.Command, false);
  1857. if (FLAGCLEAR(Command.Params, ccLocal))
  1858. {
  1859. int AllowedState = CustomCommandRemoteAllowed() ? 1 : 0;
  1860. // custom command that does not operate with files can be executed anytime ...
  1861. if (!NonInteractiveCustomCommand->IsFileCommand(Cmd))
  1862. {
  1863. // ... but do not show such command in remote file menu
  1864. if ((ListType == ccltAll) || (ListType == ccltNonFile))
  1865. {
  1866. Result = AllowedState;
  1867. }
  1868. else
  1869. {
  1870. Result = -1;
  1871. }
  1872. }
  1873. else
  1874. {
  1875. if ((ListType == ccltAll) || ((ListType == ccltFile) && !IsSideLocalBrowser(FCurrentSide)))
  1876. {
  1877. Result = (DirView(FCurrentSide)->AnyFileSelected(OnFocused, false, true)) ? AllowedState : 0;
  1878. }
  1879. else
  1880. {
  1881. Result = -1;
  1882. }
  1883. }
  1884. }
  1885. else
  1886. {
  1887. // custom command that does not operate with files can be executed anytime
  1888. if (!NonInteractiveCustomCommand->IsFileCommand(Cmd))
  1889. {
  1890. Result = ((ListType == ccltAll) || (ListType == ccltNonFile)) ? 1 : -1;
  1891. }
  1892. else if (LocalCustomCommand.HasLocalFileName(Cmd))
  1893. {
  1894. if ((ListType == ccltAll) || (ListType == ccltFile))
  1895. {
  1896. // special case is "diff"-style command that can be executed over any side,
  1897. // if we have both sides
  1898. Result =
  1899. // Cannot have focus on both panels, so we have to call AnyFileSelected
  1900. // directly (instead of EnableSelectedOperation) to pass
  1901. // false to FocusedFileOnlyWhenFocused when panel is inactive.
  1902. ((HasDirView[osLocal] && !IsSideLocalBrowser(osRemote) &&
  1903. DirView(osLocal)->AnyFileSelected(false, false, (FCurrentSide == osLocal))) &&
  1904. DirView(osRemote)->AnyFileSelected(false, false, (FCurrentSide == osRemote))) ? 1 : 0;
  1905. }
  1906. else if (ListType == ccltBoth)
  1907. {
  1908. DebugAssert(!IsLocalBrowserMode());
  1909. Result = 1;
  1910. }
  1911. else
  1912. {
  1913. Result = -1;
  1914. }
  1915. }
  1916. else
  1917. {
  1918. if ((ListType == ccltAll) ||
  1919. ((ListType == ccltFile) && (!IsSideLocalBrowser(FCurrentSide) || FLAGCLEAR(Command.Params, ccRemoteFiles))))
  1920. {
  1921. Result = DirView(FCurrentSide)->AnyFileSelected(OnFocused, false, true) ? 1 : 0;
  1922. }
  1923. else
  1924. {
  1925. Result = -1;
  1926. }
  1927. }
  1928. if ((Result > 0) &&
  1929. LocalCustomCommand.IsSessionCommand(Cmd) &&
  1930. !HasActiveTerminal())
  1931. {
  1932. Result = 0;
  1933. }
  1934. }
  1935. return Result;
  1936. }
  1937. //---------------------------------------------------------------------------
  1938. void __fastcall TCustomScpExplorerForm::RemoteCustomCommand(
  1939. TStrings * FileList, const TCustomCommandType & ACommand,
  1940. const TCustomCommandData & Data, const UnicodeString & CommandCommand)
  1941. {
  1942. if (EnsureCommandSessionFallback(fcShellAnyCommand))
  1943. {
  1944. TRemoteCustomCommand RemoteCustomCommand(Data, Terminal->CurrentDirectory);
  1945. TWinInteractiveCustomCommand InteractiveCustomCommand(
  1946. &RemoteCustomCommand, ACommand.Name, ACommand.HomePage);
  1947. UnicodeString Command = InteractiveCustomCommand.Complete(CommandCommand, false);
  1948. Configuration->Usage->Inc(L"RemoteCustomCommandRuns2");
  1949. bool Capture =
  1950. FLAGSET(ACommand.Params, ccShowResults) ||
  1951. FLAGSET(ACommand.Params, ccCopyResults) ||
  1952. FLAGSET(ACommand.Params, ccShowResultsInMsgBox);
  1953. TCaptureOutputEvent OutputEvent = NULL;
  1954. DebugAssert(FCapturedLog == NULL);
  1955. if (Capture)
  1956. {
  1957. FCapturedLog = new TStringList();
  1958. OutputEvent = TerminalCaptureLog;
  1959. }
  1960. try
  1961. {
  1962. if (!RemoteCustomCommand.IsFileCommand(Command))
  1963. {
  1964. Terminal->AnyCommand(RemoteCustomCommand.Complete(Command, true),
  1965. OutputEvent);
  1966. }
  1967. else
  1968. {
  1969. Terminal->CustomCommandOnFiles(Command, ACommand.Params, FileList, OutputEvent);
  1970. }
  1971. if ((FCapturedLog != NULL) && (FCapturedLog->Count > 0))
  1972. {
  1973. if (FLAGSET(ACommand.Params, ccCopyResults))
  1974. {
  1975. CopyToClipboard(FCapturedLog);
  1976. }
  1977. if (FLAGSET(ACommand.Params, ccShowResults))
  1978. {
  1979. DoConsoleDialog(Terminal, L"", FCapturedLog);
  1980. }
  1981. else if (FLAGSET(ACommand.Params, ccShowResultsInMsgBox))
  1982. {
  1983. MessageDialog(FCapturedLog->Text, qtInformation, qaOK);
  1984. }
  1985. }
  1986. }
  1987. __finally
  1988. {
  1989. SAFE_DESTROY(FCapturedLog);
  1990. }
  1991. }
  1992. }
  1993. //---------------------------------------------------------------------------
  1994. void __fastcall TCustomScpExplorerForm::LocalCustomCommandPure(
  1995. TStrings * FileList, const TCustomCommandType & ACommand, const UnicodeString & Command, TStrings * ALocalFileList,
  1996. const TCustomCommandData & Data, bool LocalFileCommand, bool FileListCommand, UnicodeString * POutput)
  1997. {
  1998. DebugAssert(!IsLocalBrowserMode());
  1999. TStrings * LocalFileList = NULL;
  2000. TStrings * RemoteFileList = NULL;
  2001. TStrings * RemoteFileListFull = NULL;
  2002. try
  2003. {
  2004. if (LocalFileCommand)
  2005. {
  2006. if (ALocalFileList == NULL)
  2007. {
  2008. DebugAssert(HasDirView[osLocal]);
  2009. // Cannot have focus on both panels, so we have to call AnyFileSelected
  2010. // directly (instead of EnableSelectedOperation) to pass
  2011. // false to FocusedFileOnlyWhenFocused
  2012. DebugAssert(DirView(osLocal)->AnyFileSelected(false, false, false));
  2013. LocalFileList = DirView(osLocal)->CreateFileList(false, true, NULL);
  2014. }
  2015. else
  2016. {
  2017. LocalFileList = ALocalFileList;
  2018. }
  2019. if (FileListCommand)
  2020. {
  2021. if (LocalFileList->Count != 1)
  2022. {
  2023. throw Exception(LoadStr(CUSTOM_COMMAND_SELECTED_UNMATCH1));
  2024. }
  2025. }
  2026. else
  2027. {
  2028. if ((LocalFileList->Count != 1) &&
  2029. (FileList->Count != 1) &&
  2030. (LocalFileList->Count != FileList->Count))
  2031. {
  2032. throw Exception(LoadStr(CUSTOM_COMMAND_SELECTED_UNMATCH));
  2033. }
  2034. }
  2035. }
  2036. UnicodeString RootTempDir;
  2037. UnicodeString TempDir;
  2038. bool RemoteFiles = FLAGSET(ACommand.Params, ccRemoteFiles);
  2039. if (!RemoteFiles)
  2040. {
  2041. TemporarilyDownloadFiles(FileList, false, RootTempDir, TempDir, false, true, true);
  2042. }
  2043. try
  2044. {
  2045. TDateTimes RemoteFileTimes;
  2046. if (RemoteFiles)
  2047. {
  2048. RemoteFileList = FileList;
  2049. }
  2050. else
  2051. {
  2052. RemoteFileList = new TStringList();
  2053. TMakeLocalFileListParams MakeFileListParam;
  2054. MakeFileListParam.FileList = RemoteFileList;
  2055. MakeFileListParam.FileTimes = &RemoteFileTimes;
  2056. MakeFileListParam.IncludeDirs = FLAGSET(ACommand.Params, ccApplyToDirectories);
  2057. MakeFileListParam.Recursive =
  2058. FLAGSET(ACommand.Params, ccRecursive) && !FileListCommand;
  2059. ProcessLocalDirectory(TempDir, Terminal->MakeLocalFileList, &MakeFileListParam);
  2060. if (!MakeFileListParam.Recursive)
  2061. {
  2062. RemoteFileTimes.clear();
  2063. RemoteFileListFull = new TStringList();
  2064. MakeFileListParam.FileList = RemoteFileListFull;
  2065. MakeFileListParam.Recursive = true;
  2066. MakeFileListParam.IncludeDirs = false;
  2067. ProcessLocalDirectory(TempDir, Terminal->MakeLocalFileList, &MakeFileListParam);
  2068. }
  2069. else
  2070. {
  2071. RemoteFileListFull = RemoteFileList;
  2072. }
  2073. }
  2074. bool NonBlocking = FileListCommand && RemoteFiles && !POutput;
  2075. TFileOperationProgressType Progress(&OperationProgress, &OperationFinished);
  2076. if (!NonBlocking)
  2077. {
  2078. int Count;
  2079. if (FileListCommand)
  2080. {
  2081. Count = 1;
  2082. }
  2083. else if (LocalFileCommand)
  2084. {
  2085. Count = std::max(LocalFileList->Count, RemoteFileList->Count);
  2086. }
  2087. else
  2088. {
  2089. Count = RemoteFileList->Count;
  2090. }
  2091. Progress.Start(foCustomCommand, osRemote, Count);
  2092. DebugAssert(FProgressForm != NULL);
  2093. FProgressForm->ReadOnly = true;
  2094. }
  2095. try
  2096. {
  2097. if (FileListCommand)
  2098. {
  2099. UnicodeString LocalFile;
  2100. // MakeFileList does not delimit filenames
  2101. UnicodeString FileList = MakeFileList(RemoteFileList);
  2102. if (LocalFileCommand)
  2103. {
  2104. DebugAssert(LocalFileList->Count == 1);
  2105. LocalFile = LocalFileList->Strings[0];
  2106. }
  2107. TLocalCustomCommand CustomCommand(Data,
  2108. Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(), L"", LocalFile, FileList);
  2109. UnicodeString ShellCommand = CustomCommand.Complete(Command, true);
  2110. if (NonBlocking)
  2111. {
  2112. DebugAssert(!POutput);
  2113. ExecuteShellChecked(ShellCommand);
  2114. }
  2115. else
  2116. {
  2117. ExecuteProcessCheckedAndWait(ShellCommand, HelpKeyword, POutput);
  2118. }
  2119. }
  2120. else if (LocalFileCommand)
  2121. {
  2122. if (LocalFileList->Count == 1)
  2123. {
  2124. UnicodeString LocalFile = LocalFileList->Strings[0];
  2125. for (int Index = 0; Index < RemoteFileList->Count; Index++)
  2126. {
  2127. UnicodeString FileName = RemoteFileList->Strings[Index];
  2128. UnicodeString FileNameForProgress = RemoteFiles ? FileName : ExtractFileName(FileName);
  2129. Progress.SetFile(FileNameForProgress);
  2130. TLocalCustomCommand CustomCommand(Data,
  2131. Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(), FileName, LocalFile, L"");
  2132. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  2133. TOnceDoneOperation OnceDoneOperation;
  2134. // Do not unselect anything, as it won't work with two panels anyway
  2135. Progress.Finish(UnicodeString(), true, OnceDoneOperation);
  2136. }
  2137. }
  2138. else if (RemoteFileList->Count == 1)
  2139. {
  2140. UnicodeString FileName = RemoteFileList->Strings[0];
  2141. for (int Index = 0; Index < LocalFileList->Count; Index++)
  2142. {
  2143. UnicodeString LocalFileName = LocalFileList->Strings[Index];
  2144. UnicodeString LocalFileNameOnly = ExtractFileName(LocalFileName);
  2145. Progress.SetFile(LocalFileNameOnly);
  2146. TLocalCustomCommand CustomCommand(
  2147. Data, Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(),
  2148. FileName, LocalFileName, L"");
  2149. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  2150. TOnceDoneOperation OnceDoneOperation;
  2151. // Do not unselect anything, as it won't work with two panels anyway
  2152. Progress.Finish(UnicodeString(), true, OnceDoneOperation);
  2153. }
  2154. }
  2155. else
  2156. {
  2157. if (LocalFileList->Count != RemoteFileList->Count)
  2158. {
  2159. throw Exception(LoadStr(CUSTOM_COMMAND_PAIRS_DOWNLOAD_FAILED));
  2160. }
  2161. for (int Index = 0; Index < LocalFileList->Count; Index++)
  2162. {
  2163. UnicodeString FileName = RemoteFileList->Strings[Index];
  2164. UnicodeString LocalFileName = LocalFileList->Strings[Index];
  2165. UnicodeString LocalFileNameOnly = ExtractFileName(LocalFileName);
  2166. Progress.SetFile(LocalFileNameOnly); // sic
  2167. TLocalCustomCommand CustomCommand(
  2168. Data, Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(),
  2169. FileName, LocalFileName, L"");
  2170. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  2171. TOnceDoneOperation OnceDoneOperation;
  2172. // Do not unselect anything, as it won't work with two panels anyway
  2173. Progress.Finish(UnicodeString(), true, OnceDoneOperation);
  2174. }
  2175. }
  2176. }
  2177. else
  2178. {
  2179. for (int Index = 0; Index < RemoteFileList->Count; Index++)
  2180. {
  2181. UnicodeString FileName = RemoteFileList->Strings[Index];
  2182. UnicodeString FileNameForProgress = RemoteFiles ? FileName : ExtractFileName(FileName);
  2183. Progress.SetFile(FileNameForProgress);
  2184. TLocalCustomCommand CustomCommand(Data,
  2185. Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(),
  2186. FileName, L"", L"");
  2187. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  2188. TOnceDoneOperation OnceDoneOperation;
  2189. Progress.Finish(FileNameForProgress, true, OnceDoneOperation);
  2190. }
  2191. }
  2192. }
  2193. __finally
  2194. {
  2195. if (!NonBlocking)
  2196. {
  2197. Progress.Stop();
  2198. }
  2199. }
  2200. DebugAssert(!FAutoOperation);
  2201. if (!RemoteFiles)
  2202. {
  2203. TempDir = IncludeTrailingBackslash(TempDir);
  2204. for (int Index = 0; Index < RemoteFileListFull->Count; Index++)
  2205. {
  2206. UnicodeString FileName = RemoteFileListFull->Strings[Index];
  2207. if (DebugAlwaysTrue(SameText(TempDir, FileName.SubString(1, TempDir.Length()))) &&
  2208. // Skip directories, process only nested files.
  2209. // The check is redundant as FileAge fails for directories anyway.
  2210. !DirectoryExists(FileName))
  2211. {
  2212. UnicodeString RemoteDir =
  2213. UnixExtractFileDir(
  2214. UnixIncludeTrailingBackslash(Terminal->CurrentDirectory) +
  2215. ToUnixPath(FileName.SubString(TempDir.Length() + 1, FileName.Length() - TempDir.Length())));
  2216. TSearchRecSmart SearchRec;
  2217. if (FileSearchRec(FileName, SearchRec) &&
  2218. (SearchRec.GetLastWriteTime() != RemoteFileTimes[Index]))
  2219. {
  2220. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  2221. TemporaryFileCopyParam(CopyParam);
  2222. CopyParam.FileMask = L"";
  2223. FAutoOperation = true;
  2224. FMoveToQueue = false;
  2225. std::unique_ptr<TStrings> TemporaryFilesList(new TStringList());
  2226. TemporaryFilesList->Add(FileName);
  2227. Terminal->CopyToRemote(TemporaryFilesList.get(), RemoteDir, &CopyParam, cpTemporary, NULL);
  2228. }
  2229. }
  2230. }
  2231. }
  2232. }
  2233. __finally
  2234. {
  2235. FAutoOperation = false;
  2236. if (!RootTempDir.IsEmpty() && DebugAlwaysTrue(!RemoteFiles))
  2237. {
  2238. RecursiveDeleteFile(ExcludeTrailingBackslash(RootTempDir));
  2239. }
  2240. }
  2241. }
  2242. __finally
  2243. {
  2244. if (RemoteFileList != FileList)
  2245. {
  2246. delete RemoteFileList;
  2247. }
  2248. if (LocalFileList != ALocalFileList)
  2249. {
  2250. delete LocalFileList;
  2251. }
  2252. if (RemoteFileListFull != RemoteFileList)
  2253. {
  2254. delete RemoteFileListFull;
  2255. }
  2256. }
  2257. }
  2258. //---------------------------------------------------------------------------
  2259. void __fastcall TCustomScpExplorerForm::LocalCustomCommandWithLocalFiles(
  2260. const TCustomCommandType & ACommand, const UnicodeString & Command, const TCustomCommandData & Data,
  2261. bool FileListCommand, UnicodeString * POutput)
  2262. {
  2263. std::unique_ptr<TStrings> SelectedFileList(DirView(osCurrent)->CreateFileList(false, true, NULL));
  2264. std::unique_ptr<TStrings> LocalFileList(new TStringList());
  2265. for (int Index = 0; Index < SelectedFileList->Count; Index++)
  2266. {
  2267. UnicodeString FileName = SelectedFileList->Strings[Index];
  2268. if (DirectoryExists(FileName))
  2269. {
  2270. if (FLAGSET(ACommand.Params, ccApplyToDirectories))
  2271. {
  2272. LocalFileList->Add(FileName);
  2273. }
  2274. if (FLAGSET(ACommand.Params, ccRecursive))
  2275. {
  2276. TMakeLocalFileListParams MakeFileListParam;
  2277. MakeFileListParam.FileList = LocalFileList.get();
  2278. MakeFileListParam.FileTimes = NULL;
  2279. MakeFileListParam.IncludeDirs = FLAGSET(ACommand.Params, ccApplyToDirectories);
  2280. MakeFileListParam.Recursive = true;
  2281. ProcessLocalDirectory(FileName, Terminal->MakeLocalFileList, &MakeFileListParam);
  2282. }
  2283. }
  2284. else
  2285. {
  2286. LocalFileList->Add(FileName);
  2287. }
  2288. }
  2289. UnicodeString RemotePath;
  2290. if (Terminal != NULL)
  2291. {
  2292. RemotePath = Terminal->CurrentDirectory;
  2293. }
  2294. if (FileListCommand)
  2295. {
  2296. UnicodeString FileList = MakeFileList(LocalFileList.get());
  2297. TLocalCustomCommand CustomCommand(
  2298. Data, RemotePath, DefaultDownloadTargetDirectory(),
  2299. L"", L"", FileList);
  2300. ExecuteProcessChecked(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  2301. }
  2302. else
  2303. {
  2304. TFileOperationProgressType Progress(&OperationProgress, &OperationFinished);
  2305. Progress.Start(foCustomCommand, osLocal, LocalFileList->Count);
  2306. DebugAssert(FProgressForm != NULL);
  2307. FProgressForm->ReadOnly = true;
  2308. try
  2309. {
  2310. for (int Index = 0; Index < LocalFileList->Count; Index++)
  2311. {
  2312. UnicodeString FileName = LocalFileList->Strings[Index];
  2313. Progress.SetFile(FileName);
  2314. TLocalCustomCommand CustomCommand(
  2315. Data, RemotePath, DefaultDownloadTargetDirectory(),
  2316. FileName, L"", L"");
  2317. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  2318. TOnceDoneOperation OnceDoneOperation;
  2319. Progress.Finish(FileName, true, OnceDoneOperation);
  2320. }
  2321. }
  2322. __finally
  2323. {
  2324. Progress.Stop();
  2325. }
  2326. }
  2327. }
  2328. //---------------------------------------------------------------------------
  2329. void __fastcall TCustomScpExplorerForm::LocalCustomCommand(TStrings * FileList,
  2330. const TCustomCommandType & ACommand, TStrings * ALocalFileList,
  2331. const TCustomCommandData & Data, const UnicodeString & CommandCommand)
  2332. {
  2333. UnicodeString RemotePath = (Terminal != NULL) ? Terminal->CurrentDirectory : UnicodeString();
  2334. TLocalCustomCommand LocalCustomCommand(Data, RemotePath, DefaultDownloadTargetDirectory());
  2335. TWinInteractiveCustomCommand InteractiveCustomCommand(
  2336. &LocalCustomCommand, ACommand.Name, ACommand.HomePage);
  2337. UnicodeString Command = InteractiveCustomCommand.Complete(CommandCommand, false);
  2338. bool FileListCommand = LocalCustomCommand.IsFileListCommand(Command);
  2339. bool LocalFileCommand = LocalCustomCommand.HasLocalFileName(Command);
  2340. Configuration->Usage->Inc(L"LocalCustomCommandRuns2");
  2341. std::unique_ptr<UnicodeString> POutput;
  2342. if (FLAGSET(ACommand.Params, ccShowResultsInMsgBox) ||
  2343. FLAGSET(ACommand.Params, ccCopyResults))
  2344. {
  2345. POutput.reset(new UnicodeString());
  2346. }
  2347. TValueRestorer<TOperationSide> ProgressSideRestorer(FProgressSide, FCurrentSide);
  2348. if (!LocalCustomCommand.IsFileCommand(Command))
  2349. {
  2350. ExecuteProcessChecked(LocalCustomCommand.Complete(Command, true), HelpKeyword, POutput.get());
  2351. }
  2352. // remote files?
  2353. else if (!IsSideLocalBrowser(FCurrentSide) || LocalFileCommand)
  2354. {
  2355. LocalCustomCommandPure(FileList, ACommand, Command, ALocalFileList, Data, LocalFileCommand, FileListCommand, POutput.get());
  2356. }
  2357. // local files
  2358. else
  2359. {
  2360. LocalCustomCommandWithLocalFiles(ACommand, Command, Data, FileListCommand, POutput.get());
  2361. }
  2362. if (POutput.get() != NULL)
  2363. {
  2364. // If the output is single-line, we do not want the trailing CRLF, as the CopyToClipboard(TStrings *) does
  2365. int P = POutput->Pos(sLineBreak);
  2366. if (P == POutput->Length() - static_cast<int>(strlen(sLineBreak)) + 1)
  2367. {
  2368. POutput->SetLength(P - 1);
  2369. }
  2370. // Copy even empty output to clipboard
  2371. if (FLAGSET(ACommand.Params, ccCopyResults))
  2372. {
  2373. CopyToClipboard(*POutput);
  2374. }
  2375. // But do not show an empty message box.
  2376. // This way the ShowResultsInMsgBox can be used to suppress a console window of commands with no output
  2377. if (FLAGSET(ACommand.Params, ccShowResultsInMsgBox) &&
  2378. !POutput->IsEmpty())
  2379. {
  2380. TClipboardHandler ClipboardHandler;
  2381. ClipboardHandler.Text = *POutput;
  2382. TMessageParams Params;
  2383. TQueryButtonAlias Aliases[1];
  2384. Aliases[0].Button = qaRetry;
  2385. Aliases[0].Alias = LoadStr(EDIT_COPY);
  2386. Aliases[0].OnSubmit = &ClipboardHandler.Copy;
  2387. Params.Aliases = Aliases;
  2388. Params.AliasesCount = LENOF(Aliases);
  2389. MessageDialog(*POutput, qtInformation, qaOK | qaRetry, HelpKeyword, &Params);
  2390. }
  2391. }
  2392. }
  2393. //---------------------------------------------------------------------------
  2394. void __fastcall TCustomScpExplorerForm::CustomCommand(TStrings * FileList,
  2395. const TCustomCommandType & ACommand, TStrings * ALocalFileList)
  2396. {
  2397. TCustomCommandData Data;
  2398. UnicodeString Site;
  2399. UnicodeString RemotePath;
  2400. if (HasActiveTerminal())
  2401. {
  2402. std::unique_ptr<TSessionData> SessionData(SessionDataForCode());
  2403. Data = TCustomCommandData(SessionData.get());
  2404. Site = Terminal->SessionData->SessionKey;
  2405. RemotePath = Terminal->CurrentDirectory;
  2406. }
  2407. UnicodeString HelpKeyword = ACommand.HomePage;
  2408. std::unique_ptr<TStrings> CustomCommandOptions(CloneStrings(WinConfiguration->CustomCommandOptions));
  2409. if (ACommand.AnyOptionWithFlag(TCustomCommandType::ofRun))
  2410. {
  2411. std::unique_ptr<TCustomCommand> CustomCommandForOptions;
  2412. if (FLAGCLEAR(ACommand.Params, ccLocal))
  2413. {
  2414. CustomCommandForOptions.reset(new TRemoteCustomCommand(Data, RemotePath));
  2415. }
  2416. else
  2417. {
  2418. CustomCommandForOptions.reset(new TLocalCustomCommand(Data, RemotePath, DefaultDownloadTargetDirectory()));
  2419. }
  2420. if (!DoCustomCommandOptionsDialog(
  2421. &ACommand, CustomCommandOptions.get(), NULL, TCustomCommandType::ofRun, CustomCommandForOptions.get(), Site, NULL))
  2422. {
  2423. Abort();
  2424. }
  2425. }
  2426. UnicodeString CommandCommand = ACommand.GetCommandWithExpandedOptions(CustomCommandOptions.get(), Site);
  2427. if (FLAGCLEAR(ACommand.Params, ccLocal))
  2428. {
  2429. RemoteCustomCommand(FileList, ACommand, Data, CommandCommand);
  2430. }
  2431. else
  2432. {
  2433. LocalCustomCommand(FileList, ACommand, ALocalFileList, Data, CommandCommand);
  2434. }
  2435. }
  2436. //---------------------------------------------------------------------------
  2437. void __fastcall TCustomScpExplorerForm::BothCustomCommand(
  2438. const TCustomCommandType & Command)
  2439. {
  2440. DebugAssert(FCustomCommandLocalFileList != NULL);
  2441. DebugAssert(FCustomCommandRemoteFileList != NULL);
  2442. DebugAssert(FCustomCommandLocalFileList->Count == FCustomCommandRemoteFileList->Count);
  2443. TStrings * LocalFileList = new TStringList();
  2444. TStrings * RemoteFileList = new TStringList();
  2445. try
  2446. {
  2447. for (int Index = 0; Index < FCustomCommandLocalFileList->Count; Index++)
  2448. {
  2449. LocalFileList->Clear();
  2450. LocalFileList->AddObject(
  2451. FCustomCommandLocalFileList->Strings[Index],
  2452. FCustomCommandLocalFileList->Objects[Index]);
  2453. RemoteFileList->Clear();
  2454. RemoteFileList->AddObject(
  2455. FCustomCommandRemoteFileList->Strings[Index],
  2456. FCustomCommandRemoteFileList->Objects[Index]);
  2457. CustomCommand(RemoteFileList, Command, LocalFileList);
  2458. }
  2459. }
  2460. __finally
  2461. {
  2462. delete LocalFileList;
  2463. delete RemoteFileList;
  2464. }
  2465. }
  2466. //---------------------------------------------------------------------------
  2467. void __fastcall TCustomScpExplorerForm::CustomCommandMenu(
  2468. TAction * Action, TStrings * LocalFileList, TStrings * RemoteFileList)
  2469. {
  2470. delete FCustomCommandLocalFileList;
  2471. delete FCustomCommandRemoteFileList;
  2472. // takeover ownership,
  2473. // the lists must survive the MenuPopup as OnClick occurs only after it exits
  2474. FCustomCommandLocalFileList = LocalFileList;
  2475. FCustomCommandRemoteFileList = RemoteFileList;
  2476. TButton * Button = dynamic_cast<TButton *>(Action->ActionComponent);
  2477. if (Button != NULL)
  2478. {
  2479. FCustomCommandMenu->Items->Clear();
  2480. NonVisualDataModule->CreateCustomCommandsMenu(FCustomCommandMenu->Items, false, false, ccltBoth, NULL);
  2481. MenuPopup(FCustomCommandMenu, Button);
  2482. }
  2483. else
  2484. {
  2485. NonVisualDataModule->CreateCustomCommandsMenu(Action, false, ccltBoth);
  2486. }
  2487. }
  2488. //---------------------------------------------------------------------------
  2489. void __fastcall TCustomScpExplorerForm::TerminalCaptureLog(
  2490. const UnicodeString & AddedLine, TCaptureOutputType OutputType)
  2491. {
  2492. DebugAssert(FCapturedLog != NULL);
  2493. if ((OutputType == cotOutput) || (OutputType == cotError))
  2494. {
  2495. FCapturedLog->Add(AddedLine);
  2496. }
  2497. }
  2498. //---------------------------------------------------------------------------
  2499. bool __fastcall TCustomScpExplorerForm::IsFileControl(TObject * Control,
  2500. TOperationSide Side)
  2501. {
  2502. return
  2503. (Side == osOther) &&
  2504. ((Control == DirView(osOther)) || (Control == DriveView(osOther)));
  2505. }
  2506. //---------------------------------------------------------------------------
  2507. void __fastcall TCustomScpExplorerForm::DirViewContextPopupDefaultItem(
  2508. TOperationSide Side, TTBXCustomItem * Item,
  2509. TResolvedDoubleClickAction DoubleClickAction1, TResolvedDoubleClickAction DoubleClickAction2)
  2510. {
  2511. TCustomDirView * DView = DirView(Side);
  2512. bool IsDefault = false;
  2513. if (DView->ItemFocused != NULL)
  2514. {
  2515. TTerminal * ATerminal = !IsSideLocalBrowser(Side) ? Terminal : NULL;
  2516. TResolvedDoubleClickAction DefaultAction =
  2517. WinConfiguration->ResolveDoubleClickAction(DView->ItemIsDirectory(DView->ItemFocused), ATerminal);
  2518. IsDefault = (DefaultAction == DoubleClickAction1) || (DefaultAction == DoubleClickAction2);
  2519. }
  2520. TTBItemOptions O = Item->Options;
  2521. if (IsDefault)
  2522. {
  2523. Item->Options = O << tboDefault;
  2524. }
  2525. else
  2526. {
  2527. Item->Options = O >> tboDefault;
  2528. }
  2529. }
  2530. //---------------------------------------------------------------------------
  2531. void __fastcall TCustomScpExplorerForm::DirViewContextPopup(
  2532. TOperationSide Side, Byte PopupComponent, const TPoint &MousePos)
  2533. {
  2534. TCustomDirView * DView = DirView(Side);
  2535. TListItem * Item = DView->ItemFocused;
  2536. if ((DView->GetItemAt(MousePos.x, MousePos.y) == Item) &&
  2537. EnableFocusedOperation[Side])
  2538. {
  2539. TPoint ClientPoint;
  2540. ClientPoint = ((MousePos.x < 0) && (MousePos.y < 0)) ?
  2541. TPoint(0, 0) : MousePos;
  2542. FLastContextPopupScreenPoint = DView->ClientToScreen(ClientPoint);
  2543. reinterpret_cast<TPopupMenu*>(GetComponent(PopupComponent))->Popup(
  2544. FLastContextPopupScreenPoint.x, FLastContextPopupScreenPoint.y);
  2545. }
  2546. }
  2547. //---------------------------------------------------------------------------
  2548. void __fastcall TCustomScpExplorerForm::RemoteDirViewContextPopup(
  2549. TObject * /*Sender*/, const TPoint &MousePos, bool &Handled)
  2550. {
  2551. DirViewContextPopupDefaultItem(osRemote, NonVisualDataModule->RemoteOpenMenuItem, rdcaChangeDir, rdcaOpen);
  2552. DirViewContextPopupDefaultItem(osRemote, NonVisualDataModule->RemoteEditMenuItem, rdcaEdit, rdcaNone);
  2553. DirViewContextPopupDefaultItem(osRemote, NonVisualDataModule->RemoteCopyMenuItem, rdcaCopy, rdcaNone);
  2554. DirViewContextPopup(osRemote, fcRemotePopup, MousePos);
  2555. Handled = true;
  2556. }
  2557. //---------------------------------------------------------------------------
  2558. void __fastcall TCustomScpExplorerForm::ReloadLocalDirectory(const UnicodeString Directory)
  2559. {
  2560. }
  2561. //---------------------------------------------------------------------------
  2562. void __fastcall TCustomScpExplorerForm::BatchStart(void *& /*Storage*/)
  2563. {
  2564. DebugAssert(FErrorList == NULL);
  2565. if (WinConfiguration->ContinueOnError)
  2566. {
  2567. FErrorList = new TStringList();
  2568. Configuration->Usage->Inc(L"ContinuationsOnError");
  2569. }
  2570. NonVisualDataModule->StartBusy();
  2571. }
  2572. //---------------------------------------------------------------------------
  2573. void __fastcall TCustomScpExplorerForm::BatchEnd(void * /*Storage*/)
  2574. {
  2575. NonVisualDataModule->EndBusy();
  2576. if (FErrorList)
  2577. {
  2578. HandleErrorList(FErrorList);
  2579. }
  2580. }
  2581. //---------------------------------------------------------------------------
  2582. void __fastcall TCustomScpExplorerForm::UpdateCopyParamCounters(
  2583. const TCopyParamType & CopyParam)
  2584. {
  2585. if (!CopyParam.IncludeFileMask.Masks.IsEmpty())
  2586. {
  2587. Configuration->Usage->Inc(L"FileMaskUses");
  2588. }
  2589. if (IsEffectiveFileNameMask(CopyParam.FileMask))
  2590. {
  2591. Configuration->Usage->Inc(L"OperationMaskUses");
  2592. }
  2593. }
  2594. //---------------------------------------------------------------------------
  2595. bool __fastcall TCustomScpExplorerForm::ExecuteCopyMoveFileOperation(
  2596. TFileOperation Operation, TOperationSide Side, TStrings * FileList, bool NoConfirmation, void * AParam)
  2597. {
  2598. DebugAssert(!IsLocalBrowserMode());
  2599. TTransferDirection Direction = (Side == osLocal ? tdToRemote : tdToLocal);
  2600. TTransferType Type = (Operation == foCopy ? ttCopy : ttMove);
  2601. TTransferOperationParam DefaultParam;
  2602. TTransferOperationParam & Param = (AParam != NULL) ? *static_cast<TTransferOperationParam *>(AParam) : DefaultParam;
  2603. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  2604. switch (Param.Queue)
  2605. {
  2606. case asOn:
  2607. CopyParam.Queue = true;
  2608. break;
  2609. case asOff:
  2610. CopyParam.Queue = false;
  2611. break;
  2612. case asAuto:
  2613. default:
  2614. // keep default
  2615. break;
  2616. }
  2617. bool Result =
  2618. CopyParamDialog(Direction, Type, Param.Temp, FileList, Param.TargetDirectory, CopyParam, !NoConfirmation, Param.DragDrop, Param.Options);
  2619. if (Result)
  2620. {
  2621. DebugAssert(Terminal);
  2622. bool SelectionRestored = false;
  2623. TCustomDirView * DView = NULL;
  2624. if (HasDirView[Side])
  2625. {
  2626. DView = DirView(Side);
  2627. DView->SaveSelection();
  2628. DView->SaveSelectedNames();
  2629. }
  2630. UpdateCopyParamCounters(CopyParam);
  2631. std::unique_ptr<TStringList> TransferResumeList(new TStringList());
  2632. DebugAssert(FTransferResumeList == NULL);
  2633. FTransferResumeList = Terminal->IsCapable[fcMoveToQueue] ? TransferResumeList.get() : NULL;
  2634. FMoveToQueue = false;
  2635. int Params = FLAGMASK(Operation == foMove, cpDelete);
  2636. try
  2637. {
  2638. TStrings * PermanentFileList;
  2639. std::unique_ptr<TStrings> PermanentFileListOwner;
  2640. try
  2641. {
  2642. if (Side == osLocal)
  2643. {
  2644. PermanentFileList = FileList;
  2645. Params |= FLAGMASK(Param.Temp, cpTemporary);
  2646. CopyParam.IncludeFileMask.SetRoots(FileList, Param.TargetDirectory);
  2647. Terminal->CopyToRemote(FileList, Param.TargetDirectory, &CopyParam, Params, NULL);
  2648. if (Operation == foMove)
  2649. {
  2650. ReloadLocalDirectory();
  2651. if (DView != NULL)
  2652. {
  2653. DView->RestoreSelection();
  2654. }
  2655. SelectionRestored = true;
  2656. }
  2657. }
  2658. else
  2659. {
  2660. // Clone the file list as it may refer to current directory files,
  2661. // which get destroyed, when the source directory is reloaded after foMove operation.
  2662. // We should actually clone the file list for whole ExecuteFileOperation to protect against reloads.
  2663. // But for a hotfix, we are not going to do such a big change.
  2664. PermanentFileListOwner.reset(TRemoteFileList::CloneStrings(FileList));
  2665. PermanentFileList = PermanentFileListOwner.get();
  2666. try
  2667. {
  2668. CopyParam.IncludeFileMask.SetRoots(Param.TargetDirectory, FileList);
  2669. Terminal->CopyToLocal(FileList, Param.TargetDirectory, &CopyParam, Params, NULL);
  2670. }
  2671. __finally
  2672. {
  2673. if (Operation == foMove)
  2674. {
  2675. if (DView != NULL)
  2676. {
  2677. DView->RestoreSelection();
  2678. }
  2679. SelectionRestored = true;
  2680. }
  2681. ReloadLocalDirectory(Param.TargetDirectory);
  2682. }
  2683. }
  2684. // When the transfer batch is aborted between individual file transfers, the Abort is not called.
  2685. if (FMoveToQueue)
  2686. {
  2687. Abort();
  2688. }
  2689. }
  2690. catch (EAbort &)
  2691. {
  2692. if (FMoveToQueue)
  2693. {
  2694. Params |= (CopyParam.QueueNoConfirmation ? cpNoConfirmation : 0);
  2695. DebugAssert(CopyParam.TransferSkipList == NULL);
  2696. DebugAssert(CopyParam.TransferResumeFile.IsEmpty());
  2697. if (TransferResumeList->Count > 0)
  2698. {
  2699. CopyParam.TransferResumeFile = TransferResumeList->Strings[TransferResumeList->Count - 1];
  2700. TransferResumeList->Delete(TransferResumeList->Count - 1);
  2701. }
  2702. CopyParam.TransferSkipList = TransferResumeList.release();
  2703. // not really needed, just to keep it consistent with TransferResumeList
  2704. FTransferResumeList = NULL;
  2705. FMoveToQueue = false;
  2706. Configuration->Usage->Inc("MovesToBackground");
  2707. AddQueueItem(Queue, Direction, PermanentFileList, Param.TargetDirectory, CopyParam, Params);
  2708. ClearTransferSourceSelection(Direction);
  2709. }
  2710. throw;
  2711. }
  2712. }
  2713. __finally
  2714. {
  2715. if (!SelectionRestored && (DView != NULL))
  2716. {
  2717. DView->DiscardSavedSelection();
  2718. }
  2719. FTransferResumeList = NULL;
  2720. }
  2721. }
  2722. return Result;
  2723. }
  2724. //---------------------------------------------------------------------------
  2725. bool __fastcall TCustomScpExplorerForm::ExecuteDeleteFileOperation(
  2726. TOperationSide Side, TStrings * FileList, void * Param)
  2727. {
  2728. DebugAssert(FileList->Count);
  2729. // We deliberately do not toggle alternative flag (Param), but use OR,
  2730. // because the Param is set only when command is invoked using Shift-Del/F8 keyboard
  2731. // shortcut of CurrentDeleteAlternativeAction
  2732. bool Alternative =
  2733. bool(Param) || UseAlternativeFunction();
  2734. bool Recycle;
  2735. if (IsSideLocalBrowser(Side))
  2736. {
  2737. Recycle = (WinConfiguration->DeleteToRecycleBin != Alternative);
  2738. }
  2739. else
  2740. {
  2741. Recycle =
  2742. (Terminal->SessionData->DeleteToRecycleBin != Alternative) &&
  2743. !Terminal->SessionData->RecycleBinPath.IsEmpty() &&
  2744. !Terminal->IsRecycledFile(FileList->Strings[0]);
  2745. }
  2746. bool Result = !(Recycle ? WinConfiguration->ConfirmRecycling : WinConfiguration->ConfirmDeleting);
  2747. if (!Result)
  2748. {
  2749. UnicodeString Query;
  2750. if (FileList->Count == 1)
  2751. {
  2752. if (IsSideLocalBrowser(Side))
  2753. {
  2754. Query = ExtractFileName(FileList->Strings[0]);
  2755. }
  2756. else
  2757. {
  2758. Query = UnixExtractFileName(FileList->Strings[0]);
  2759. }
  2760. Query = FMTLOAD((Recycle ? CONFIRM_RECYCLE_FILE : CONFIRM_DELETE_FILE), (Query));
  2761. }
  2762. else
  2763. {
  2764. Query = FMTLOAD((Recycle ? CONFIRM_RECYCLE_FILES : CONFIRM_DELETE_FILES), (FileList->Count));
  2765. }
  2766. TMessageParams Params(mpNeverAskAgainCheck);
  2767. Params.ImageName = L"Delete file";
  2768. unsigned int Answer =
  2769. MessageDialog(MainInstructions(Query), qtConfirmation, qaOK | qaCancel, HELP_DELETE_FILE, &Params);
  2770. if (Answer == qaNeverAskAgain)
  2771. {
  2772. Result = true;
  2773. if (Recycle)
  2774. {
  2775. WinConfiguration->ConfirmRecycling = false;
  2776. }
  2777. else
  2778. {
  2779. WinConfiguration->ConfirmDeleting = false;
  2780. }
  2781. }
  2782. else
  2783. {
  2784. Result = (Answer == qaOK);
  2785. }
  2786. }
  2787. if (Result)
  2788. {
  2789. DeleteFiles(Side, FileList, FLAGMASK(Alternative, dfAlternative));
  2790. }
  2791. return Result;
  2792. }
  2793. //---------------------------------------------------------------------------
  2794. bool __fastcall TCustomScpExplorerForm::ExecuteFileOperation(TFileOperation Operation,
  2795. TOperationSide Side, TStrings * FileList, bool NoConfirmation, void * Param)
  2796. {
  2797. TAutoBatch AutoBatch(this);
  2798. bool Result;
  2799. if (TFileOperationProgressType::IsTransferOperation(Operation))
  2800. {
  2801. Result = ExecuteCopyMoveFileOperation(Operation, Side, FileList, NoConfirmation, Param);
  2802. }
  2803. else if (Operation == foRename)
  2804. {
  2805. DebugAssert(DirView(Side)->ItemFocused);
  2806. DirView(Side)->ItemFocused->EditCaption();
  2807. Result = true;
  2808. }
  2809. else if (Operation == foDelete)
  2810. {
  2811. Result = ExecuteDeleteFileOperation(Side, FileList, Param);
  2812. }
  2813. else if (Operation == foSetProperties)
  2814. {
  2815. DirView(osRemote)->SaveSelectedNames();
  2816. Result = SetProperties(Side, FileList);
  2817. }
  2818. else if (Operation == foCustomCommand)
  2819. {
  2820. DebugAssert(Param);
  2821. DebugAssert(Side == osRemote); // Side carries no meaning for foCustomCommand
  2822. DirView(osRemote)->SaveSelectedNames();
  2823. const TCustomCommandType * Command = static_cast<const TCustomCommandType*>(Param);
  2824. CustomCommand(FileList, *Command, NULL);
  2825. Result = true;
  2826. }
  2827. else if ((Operation == foRemoteMove) || (Operation == foRemoteCopy))
  2828. {
  2829. DebugAssert(!IsSideLocalBrowser(Side));
  2830. Result = RemoteTransferFiles(FileList, NoConfirmation,
  2831. (Operation == foRemoteMove), reinterpret_cast<TManagedTerminal *>(Param));
  2832. }
  2833. else if (Operation == foLock)
  2834. {
  2835. DebugAssert(!IsSideLocalBrowser(Side));
  2836. LockFiles(FileList, true);
  2837. Result = true;
  2838. }
  2839. else if (Operation == foUnlock)
  2840. {
  2841. DebugAssert(!IsSideLocalBrowser(Side));
  2842. LockFiles(FileList, false);
  2843. Result = true;
  2844. }
  2845. else
  2846. {
  2847. DebugFail();
  2848. }
  2849. return Result;
  2850. }
  2851. //---------------------------------------------------------------------------
  2852. TOperationSide __fastcall TCustomScpExplorerForm::GetSide(TOperationSide Side)
  2853. {
  2854. if (Side == osCurrent)
  2855. {
  2856. Side = FCurrentSide;
  2857. }
  2858. return Side;
  2859. }
  2860. //---------------------------------------------------------------------------
  2861. TOperationSide TCustomScpExplorerForm::GetOtherSide(TOperationSide Side)
  2862. {
  2863. TOperationSide Result;
  2864. switch (GetSide(Side))
  2865. {
  2866. case osLocal:
  2867. Result = osOther;
  2868. break;
  2869. case osOther:
  2870. Result = osLocal;
  2871. break;
  2872. default:
  2873. DebugFail();
  2874. Abort();
  2875. }
  2876. return Result;
  2877. }
  2878. //---------------------------------------------------------------------------
  2879. bool __fastcall TCustomScpExplorerForm::ExecuteFileOperation(TFileOperation Operation,
  2880. TOperationSide Side, bool OnFocused, bool NoConfirmation, void * Param)
  2881. {
  2882. Side = GetSide(Side);
  2883. bool FullPath = IsSideLocalBrowser(Side);
  2884. TCustomDirView * ADirView = DirView(Side);
  2885. std::unique_ptr<TStringList> FileList(new TStringList());
  2886. ADirView->CreateFileList(OnFocused, FullPath, FileList.get());
  2887. bool ReloadProperties =
  2888. !IsSideLocalBrowser(Side) && (Operation == foSetProperties) &&
  2889. DebugAlwaysTrue(HasActiveTerminal()) && Terminal->IsCapable[fcLoadingAdditionalProperties];
  2890. bool Result = ExecuteFileOperation(Operation, Side, FileList.get(), NoConfirmation, Param);
  2891. if (Result && ReloadProperties)
  2892. {
  2893. std::unique_ptr<TStringList> FileList2(new TStringList());
  2894. FileList->CaseSensitive = true;
  2895. FileList->Sort();
  2896. for (int Index = 0; Index < ADirView->Items->Count; Index++)
  2897. {
  2898. TListItem * Item = ADirView->Items->Item[Index];
  2899. UnicodeString FileName = ADirView->ItemFileName(Item);
  2900. int Index2;
  2901. if (FileList->Find(FileName, Index2))
  2902. {
  2903. FileList2->AddObject(FileName, ADirView->ItemData(Item));
  2904. }
  2905. }
  2906. LoadFilesProperties(FileList2.get());
  2907. }
  2908. return Result;
  2909. }
  2910. //---------------------------------------------------------------------------
  2911. void __fastcall TCustomScpExplorerForm::ExecuteFileOperationCommand(
  2912. TFileOperation Operation, TOperationSide Side, bool OnFocused,
  2913. bool NoConfirmation, void * Param)
  2914. {
  2915. if (ExecuteFileOperation(Operation, Side, OnFocused, NoConfirmation, Param))
  2916. {
  2917. if (TFileOperationProgressType::IsTransferOperation(Operation))
  2918. {
  2919. DebugAssert(!IsLocalBrowserMode());
  2920. if (GetSide(Side) == osLocal)
  2921. {
  2922. Configuration->Usage->Inc(L"UploadsCommand");
  2923. }
  2924. else
  2925. {
  2926. Configuration->Usage->Inc(L"DownloadsCommand");
  2927. }
  2928. }
  2929. }
  2930. }
  2931. //---------------------------------------------------------------------------
  2932. void __fastcall TCustomScpExplorerForm::ExecuteCopyOperationCommand(
  2933. TOperationSide Side, bool OnFocused, unsigned int Flags)
  2934. {
  2935. TTransferOperationParam Param;
  2936. if ((WinConfiguration->Interface != ifCommander) ||
  2937. WinConfiguration->ScpCommander.ExplorerKeyboardShortcuts)
  2938. {
  2939. Flags &= ~cocShortCutHint;
  2940. }
  2941. TCustomDirView * DView = DirView(Side);
  2942. Param.Options =
  2943. FLAGMASK(FLAGSET(Flags, cocShortCutHint), coShortCutHint) |
  2944. FLAGMASK(SelectedAllFilesInDirView(DView), coAllFiles);
  2945. if (FLAGSET(Flags, cocQueue))
  2946. {
  2947. Param.Queue = asOn;
  2948. }
  2949. else if (FLAGSET(Flags, cocNonQueue))
  2950. {
  2951. Param.Queue = asOff;
  2952. }
  2953. ExecuteFileOperationCommand(foCopy, Side, OnFocused, false, &Param);
  2954. }
  2955. //---------------------------------------------------------------------------
  2956. void __fastcall TCustomScpExplorerForm::HandleErrorList(TStringList *& ErrorList)
  2957. {
  2958. int Count = ErrorList->Count;
  2959. try
  2960. {
  2961. if (ErrorList->Count)
  2962. {
  2963. UnicodeString Message = MainInstructions(FMTLOAD(ERROR_LIST_COUNT, (ErrorList->Count)));
  2964. if (MessageDialog(Message, qtError,
  2965. qaOK | qaCancel, HELP_NONE) == qaOK)
  2966. {
  2967. unsigned int Answer;
  2968. int Index = 0;
  2969. do
  2970. {
  2971. DebugAssert(Index >= 0 && Index < Count);
  2972. TQueryButtonAlias Aliases[2];
  2973. Aliases[0].Button = qaYes;
  2974. Aliases[0].Alias = LoadStr(PREV_BUTTON);
  2975. Aliases[1].Button = qaNo;
  2976. Aliases[1].Alias = LoadStr(NEXT_BUTTON);
  2977. TMessageParams Params;
  2978. Params.Aliases = Aliases;
  2979. Params.AliasesCount = LENOF(Aliases);
  2980. int No = Index + 1;
  2981. UnicodeString Message = ErrorList->Strings[Index];
  2982. UnicodeString AMainInstructions;
  2983. UnicodeString Details;
  2984. if (ExtractMainInstructions(Message, AMainInstructions))
  2985. {
  2986. Details = Message;
  2987. Message = AMainInstructions;
  2988. }
  2989. Message = MainInstructions(FMTLOAD(ERROR_LIST_NUMBER, (No, Count, Message))) + Details;
  2990. Answer = MoreMessageDialog(
  2991. Message,
  2992. dynamic_cast<TStrings *>(ErrorList->Objects[Index]), qtError,
  2993. (Index ? qaYes : 0) | (Index < Count - 1 ? qaNo : 0) |
  2994. qaOK, HELP_NONE, &Params);
  2995. if (Answer == qaNo)
  2996. {
  2997. Index++;
  2998. }
  2999. if (Answer == qaYes)
  3000. {
  3001. Index--;
  3002. }
  3003. }
  3004. while (Answer != qaOK);
  3005. }
  3006. }
  3007. }
  3008. __finally
  3009. {
  3010. TStrings * List = ErrorList;
  3011. ErrorList = NULL;
  3012. for (int i = 0; i < Count; i++)
  3013. {
  3014. delete List->Objects[i];
  3015. }
  3016. delete List;
  3017. }
  3018. }
  3019. //---------------------------------------------------------------------------
  3020. void __fastcall TCustomScpExplorerForm::ExecuteRemoteFile(
  3021. const UnicodeString & FullFileName, TRemoteFile * File, TExecuteFileBy ExecuteFileBy)
  3022. {
  3023. // needed for checking filemasks, as there's no directory object
  3024. // associated with the file object
  3025. File->FullFileName = FullFileName;
  3026. TFileMasks::TParams MaskParams;
  3027. MaskParams.Size = File->Size;
  3028. MaskParams.Modification = File->Modification;
  3029. ExecuteFile(osRemote, ExecuteFileBy, NULL, FullFileName, File, MaskParams);
  3030. }
  3031. //---------------------------------------------------------------------------
  3032. void __fastcall TCustomScpExplorerForm::EditNew(TOperationSide Side)
  3033. {
  3034. DebugAssert(!WinConfiguration->DisableOpenEdit);
  3035. Side = GetSide(Side);
  3036. TCustomDirView * DView = DirView(Side);
  3037. TListItem * FocusedItem = DView->ItemFocused;
  3038. UnicodeString Name;
  3039. if ((FocusedItem != NULL) && !DView->ItemIsDirectory(FocusedItem))
  3040. {
  3041. Name = DView->ItemFileName(FocusedItem);
  3042. }
  3043. else
  3044. {
  3045. Name = LoadStr(NEW_FILE);
  3046. }
  3047. UnicodeString Names = Name;
  3048. std::unique_ptr<TStrings> History(CloneStrings(CustomWinConfiguration->History[L"EditFile"]));
  3049. if (InputDialog(LoadStr(EDIT_FILE_CAPTION), LoadStr(EDIT_FILE_PROMPT), Names,
  3050. HELP_EDIT_NEW, History.get(), true, NULL, true, 444))
  3051. {
  3052. while (!Names.IsEmpty())
  3053. {
  3054. Name = CutToChar(Names, FileMasksDelimiters[1], false);
  3055. CustomWinConfiguration->History[L"EditFile"] = History.get();
  3056. UnicodeString TargetFileName;
  3057. UnicodeString LocalFileName;
  3058. UnicodeString RootTempDir;
  3059. UnicodeString TempDir;
  3060. UnicodeString RemoteDirectory;
  3061. bool ExistingFile = false;
  3062. if (!IsSideLocalBrowser(Side))
  3063. {
  3064. Name = AbsolutePath(Terminal->CurrentDirectory, Name);
  3065. std::unique_ptr<TRemoteFile> File(Terminal->TryReadFile(Name));
  3066. if ((File.get() != NULL) &&
  3067. !File->IsDirectory)
  3068. {
  3069. ExecuteRemoteFile(Name, File.get(), efDefaultEditor);
  3070. ExistingFile = true;
  3071. }
  3072. File.reset(NULL);
  3073. if (!ExistingFile)
  3074. {
  3075. RemoteDirectory = UnixExtractFilePath(Name);
  3076. TemporaryDirectoryForRemoteFiles(
  3077. RemoteDirectory, GUIConfiguration->CurrentCopyParam, false, TempDir, RootTempDir);
  3078. TargetFileName = UnixExtractFileName(Name);
  3079. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  3080. LocalFileName = TempDir +
  3081. // We probably do not want to trim the VMS version here
  3082. Terminal->ChangeFileName(&CopyParam, TargetFileName, osRemote, false);
  3083. }
  3084. }
  3085. else
  3086. {
  3087. if (ExtractFilePath(Name).IsEmpty())
  3088. {
  3089. LocalFileName = IncludeTrailingBackslash(DView->PathName) + Name;
  3090. }
  3091. else
  3092. {
  3093. LocalFileName = ExpandFileName(Name);
  3094. }
  3095. TargetFileName = ExtractFileName(Name);
  3096. }
  3097. if (!ExistingFile)
  3098. {
  3099. bool NewFile = !FileExists(ApiPath(LocalFileName));
  3100. if (NewFile)
  3101. {
  3102. int File = FileCreate(ApiPath(LocalFileName));
  3103. if (File < 0)
  3104. {
  3105. if (!RootTempDir.IsEmpty())
  3106. {
  3107. RecursiveDeleteFile(ExcludeTrailingBackslash(RootTempDir));
  3108. }
  3109. throw Exception(FMTLOAD(CREATE_FILE_ERROR, (LocalFileName)));
  3110. }
  3111. else
  3112. {
  3113. FileClose(File);
  3114. }
  3115. }
  3116. TExecuteFileBy ExecuteFileBy = efDefaultEditor;
  3117. const TEditorData * ExternalEditor = NULL;
  3118. TFileMasks::TParams MaskParams; // size not known
  3119. ExecuteFileNormalize(ExecuteFileBy, ExternalEditor, TargetFileName,
  3120. false, MaskParams);
  3121. CustomExecuteFile(Side, ExecuteFileBy, LocalFileName, TargetFileName,
  3122. ExternalEditor, RootTempDir, RemoteDirectory, NewFile, TDateTime());
  3123. }
  3124. }
  3125. }
  3126. }
  3127. //---------------------------------------------------------------------------
  3128. bool __fastcall TCustomScpExplorerForm::RemoteExecuteForceText(
  3129. TExecuteFileBy ExecuteFileBy, const TEditorData * ExternalEditor)
  3130. {
  3131. DebugAssert((ExecuteFileBy == efExternalEditor) ==
  3132. ((ExternalEditor != NULL) && (ExternalEditor->Editor == edExternal)));
  3133. DebugAssert(ExecuteFileBy != efDefaultEditor);
  3134. return
  3135. ((ExecuteFileBy == efInternalEditor)) ||
  3136. ((ExecuteFileBy == efExternalEditor) && ExternalEditor->ExternalEditorText);
  3137. }
  3138. //---------------------------------------------------------------------------
  3139. void __fastcall TCustomScpExplorerForm::CustomExecuteFile(TOperationSide Side,
  3140. TExecuteFileBy ExecuteFileBy, UnicodeString FileName, UnicodeString OriginalFileName,
  3141. const TEditorData * ExternalEditor, UnicodeString LocalRootDirectory,
  3142. UnicodeString RemoteDirectory, bool NewFile, const TDateTime & SourceTimestamp)
  3143. {
  3144. DebugAssert(!WinConfiguration->DisableOpenEdit);
  3145. DebugAssert((ExecuteFileBy == efExternalEditor) ==
  3146. ((ExternalEditor != NULL) && (ExternalEditor->Editor == edExternal)));
  3147. DebugAssert(ExecuteFileBy != efDefaultEditor);
  3148. Side = GetSide(Side);
  3149. std::unique_ptr<TEditedFileData> Data(new TEditedFileData);
  3150. if (!IsSideLocalBrowser(Side))
  3151. {
  3152. Data->Terminal = Terminal;
  3153. Data->SessionData = CloneCurrentSessionData();
  3154. Data->ForceText = RemoteExecuteForceText(ExecuteFileBy, ExternalEditor);
  3155. Data->RemoteDirectory = RemoteDirectory;
  3156. Data->SessionName = Terminal->SessionData->SessionName;
  3157. Data->LocalRootDirectory = LocalRootDirectory;
  3158. Data->OriginalFileName = OriginalFileName;
  3159. Data->Command = L""; // will be changed later for external editor
  3160. // Empty SourceTimestamp indicates that checking was disabled when the file was opened
  3161. if (WinConfiguration->EditorCheckNotModified)
  3162. {
  3163. Data->SourceTimestamp = SourceTimestamp;
  3164. }
  3165. }
  3166. if (ExecuteFileBy == efInternalEditor)
  3167. {
  3168. if (!IsSideLocalBrowser(Side))
  3169. {
  3170. UnicodeString RemotePath = UnixCombinePaths(RemoteDirectory, OriginalFileName);
  3171. UnicodeString Caption = RemotePath + TitleSeparator + Terminal->SessionData->SessionName;
  3172. TForm * Editor;
  3173. try
  3174. {
  3175. Editor =
  3176. ShowEditorForm(
  3177. FileName, this,
  3178. FEditorManager->FileChanged, FEditorManager->FileReload, FEditorManager->FileClosed,
  3179. SaveAllInternalEditors, AnyInternalEditorModified,
  3180. Caption, StandaloneOperation, SessionColor, Terminal->SessionData->InternalEditorEncoding, NewFile);
  3181. }
  3182. catch(...)
  3183. {
  3184. if (!LocalRootDirectory.IsEmpty())
  3185. {
  3186. RecursiveDeleteFile(ExcludeTrailingBackslash(LocalRootDirectory));
  3187. }
  3188. throw;
  3189. }
  3190. FEditorManager->AddFileInternal(FileName, Data.release(), Editor);
  3191. AppLogFmt(L"Opened internal editor with \"%s\"", (FileName));
  3192. }
  3193. else
  3194. {
  3195. DebugAssert(!StandaloneOperation);
  3196. TForm * Editor =
  3197. ShowEditorForm(FileName, this, NULL, NULL, LocalEditorClosed,
  3198. SaveAllInternalEditors, AnyInternalEditorModified,
  3199. L"", false, SessionColor, -1, NewFile);
  3200. FLocalEditors->Add(Editor);
  3201. }
  3202. }
  3203. else
  3204. {
  3205. HANDLE Process;
  3206. if (ExecuteFileBy == efExternalEditor)
  3207. {
  3208. UnicodeString Program, Params, Dir;
  3209. Data->Command = ExternalEditor->ExternalEditor;
  3210. ReformatFileNameCommand(Data->Command);
  3211. SplitCommand(Data->Command, Program, Params, Dir);
  3212. Params = ExpandFileNameCommand(Params, FileName);
  3213. Program = ExpandEnvironmentVariables(Program);
  3214. if (!ExecuteShell(Program, Params, Process))
  3215. {
  3216. throw EOSExtException(FMTLOAD(EDITOR_ERROR, (Program)));
  3217. }
  3218. }
  3219. else
  3220. {
  3221. DebugAssert(!IsSideLocalBrowser(Side));
  3222. if (!ExecuteShell(FileName, L"", Process))
  3223. {
  3224. throw EOSExtException(FMTLOAD(EXECUTE_FILE_ERROR, (FileName)));
  3225. }
  3226. }
  3227. if (IsSideLocalBrowser(Side) ||
  3228. ((ExecuteFileBy == efShell) &&
  3229. !WinConfiguration->Editor.SDIShellEditor) ||
  3230. ((ExecuteFileBy == efExternalEditor) &&
  3231. !ExternalEditor->SDIExternalEditor))
  3232. {
  3233. // no need for handle
  3234. if (Process != NULL)
  3235. {
  3236. DebugCheck(CloseHandle(Process));
  3237. }
  3238. Process = INVALID_HANDLE_VALUE;
  3239. }
  3240. else
  3241. {
  3242. if (Process == NULL)
  3243. {
  3244. throw ExtException(LoadStr(OPEN_FILE_NO_PROCESS2), L"", HELP_OPEN_FILE_NO_PROCESS);
  3245. }
  3246. }
  3247. if (!IsSideLocalBrowser(Side))
  3248. {
  3249. FEditorManager->AddFileExternal(FileName, Data.release(), Process);
  3250. AppLogFmt(L"Opened external editor with remote file \"%s\".", (FileName));
  3251. }
  3252. }
  3253. }
  3254. //---------------------------------------------------------------------------
  3255. void __fastcall TCustomScpExplorerForm::SaveInternalEditor(
  3256. const UnicodeString /*FileName*/, TEditedFileData * /*Data*/, TObject * Token,
  3257. void * /*Arg*/)
  3258. {
  3259. if (Token != NULL)
  3260. {
  3261. EditorFormFileSave(static_cast<TForm *>(Token));
  3262. }
  3263. }
  3264. //---------------------------------------------------------------------------
  3265. void __fastcall TCustomScpExplorerForm::SaveAllInternalEditors(TObject * /*Sender*/)
  3266. {
  3267. for (int Index = 0; Index < FLocalEditors->Count; Index++)
  3268. {
  3269. EditorFormFileSave(static_cast<TForm *>(FLocalEditors->Items[Index]));
  3270. }
  3271. FEditorManager->ProcessFiles(SaveInternalEditor, NULL);
  3272. }
  3273. //---------------------------------------------------------------------------
  3274. void __fastcall TCustomScpExplorerForm::InternalEditorModified(
  3275. const UnicodeString /*FileName*/, TEditedFileData * /*Data*/, TObject * Token,
  3276. void * Arg)
  3277. {
  3278. if ((Token != NULL) &&
  3279. IsEditorFormModified(static_cast<TForm *>(Token)))
  3280. {
  3281. *static_cast<bool *>(Arg) = true;
  3282. }
  3283. }
  3284. //---------------------------------------------------------------------------
  3285. void __fastcall TCustomScpExplorerForm::AnyInternalEditorModified(
  3286. TObject * /*Sender*/, bool & Modified)
  3287. {
  3288. for (int Index = 0; !Modified && (Index < FLocalEditors->Count); Index++)
  3289. {
  3290. if (IsEditorFormModified(static_cast<TForm *>(FLocalEditors->Items[Index])))
  3291. {
  3292. Modified = true;
  3293. }
  3294. }
  3295. if (!Modified)
  3296. {
  3297. FEditorManager->ProcessFiles(InternalEditorModified, &Modified);
  3298. }
  3299. }
  3300. //---------------------------------------------------------------------------
  3301. void __fastcall TCustomScpExplorerForm::LocalEditorClosed(TObject * Sender, bool /*Forced*/)
  3302. {
  3303. DebugCheck(FLocalEditors->Extract(Sender) >= 0);
  3304. }
  3305. //---------------------------------------------------------------------------
  3306. void __fastcall TCustomScpExplorerForm::TemporaryDirectoryForRemoteFiles(
  3307. const UnicodeString & RemoteDirectory, const TCopyParamType & CopyParam, bool Simple,
  3308. UnicodeString & Result, UnicodeString & RootDirectory)
  3309. {
  3310. if (!WinConfiguration->TemporaryDirectoryDeterministic || Simple)
  3311. {
  3312. RootDirectory = IncludeTrailingBackslash(WinConfiguration->TemporaryDir());
  3313. Result = RootDirectory;
  3314. }
  3315. else
  3316. {
  3317. RootDirectory = L"";
  3318. Result = WinConfiguration->ExpandedTemporaryDirectory();
  3319. Result = IncludeTrailingBackslash(Result);
  3320. }
  3321. if (WinConfiguration->TemporaryDirectoryAppendSession && !Simple)
  3322. {
  3323. Result = IncludeTrailingBackslash(Result + MakeValidFileName(Terminal->SessionData->SessionName));
  3324. }
  3325. if (WinConfiguration->TemporaryDirectoryAppendPath && !Simple)
  3326. {
  3327. UnicodeString ARemoteDirectory = RemoteDirectory;
  3328. if (!ARemoteDirectory.IsEmpty() && (ARemoteDirectory[1] == L'/'))
  3329. {
  3330. ARemoteDirectory.Delete(1, 1);
  3331. }
  3332. Result = IncludeTrailingBackslash(Result + CopyParam.ValidLocalPath(FromUnixPath(ARemoteDirectory)));
  3333. }
  3334. if (!ForceDirectories(ApiPath(Result)))
  3335. {
  3336. throw EOSExtException(FMTLOAD(CREATE_TEMP_DIR_ERROR, (Result)));
  3337. }
  3338. }
  3339. //---------------------------------------------------------------------------
  3340. void __fastcall TCustomScpExplorerForm::TemporarilyDownloadFiles(
  3341. TStrings * FileList, bool ForceText, UnicodeString & RootTempDir, UnicodeString & TempDir,
  3342. bool GetTargetNames, bool AutoOperation, bool SimpleTempDir)
  3343. {
  3344. DebugAssert(!IsLocalBrowserMode());
  3345. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  3346. if (ForceText)
  3347. {
  3348. CopyParam.TransferMode = tmAscii;
  3349. }
  3350. TemporaryFileCopyParam(CopyParam);
  3351. if (TempDir.IsEmpty())
  3352. {
  3353. TemporaryDirectoryForRemoteFiles(Terminal->CurrentDirectory, CopyParam, SimpleTempDir, TempDir, RootTempDir);
  3354. }
  3355. DebugAssert(!FAutoOperation);
  3356. FAutoOperation = AutoOperation;
  3357. FMoveToQueue = false;
  3358. Terminal->ExceptionOnFail = true;
  3359. try
  3360. {
  3361. try
  3362. {
  3363. // turn off confirmations, as for MDI editors we may possibly download
  3364. // the same file over
  3365. Terminal->CopyToLocal(
  3366. FileList, TempDir, &CopyParam, cpNoConfirmation | cpTemporary, NULL);
  3367. if (GetTargetNames)
  3368. {
  3369. for (int i = 0; i < FileList->Count; i++)
  3370. {
  3371. FileList->Strings[i] =
  3372. Terminal->ChangeFileName(&CopyParam, UnixExtractFileName(FileList->Strings[i]), osRemote, false);
  3373. }
  3374. }
  3375. }
  3376. catch(...)
  3377. {
  3378. if (!RootTempDir.IsEmpty())
  3379. {
  3380. RecursiveDeleteFile(ExcludeTrailingBackslash(RootTempDir));
  3381. }
  3382. throw;
  3383. }
  3384. }
  3385. __finally
  3386. {
  3387. FAutoOperation = false;
  3388. Terminal->ExceptionOnFail = false;
  3389. }
  3390. }
  3391. //---------------------------------------------------------------------------
  3392. void __fastcall TCustomScpExplorerForm::EditorAutoConfig()
  3393. {
  3394. // Do not waste time checking for default editor list the next time,
  3395. // or testing if default editor is not notepad.
  3396. bool TryNextTime = false;
  3397. UnicodeString UsageState;
  3398. if (!WinConfiguration->EditorList->IsDefaultList())
  3399. {
  3400. UsageState = "H";
  3401. }
  3402. else
  3403. {
  3404. UnicodeString Executable;
  3405. UnicodeString ExecutableDescription;
  3406. if (DetectSystemExternalEditor(false, Executable, ExecutableDescription, UsageState, TryNextTime))
  3407. {
  3408. UnicodeString Message =
  3409. FMTLOAD(EDITOR_AUTO_CONFIG2, (ExecutableDescription, ExecutableDescription));
  3410. unsigned int Answer =
  3411. MessageDialog(Message, qtConfirmation, qaOK | qaCancel, HELP_EDITOR_AUTO_CONFIG);
  3412. if (Answer != qaOK)
  3413. {
  3414. UsageState = "R";
  3415. }
  3416. else
  3417. {
  3418. UsageState = "A";
  3419. TEditorData EditorData;
  3420. EditorData.Editor = edExternal;
  3421. EditorData.ExternalEditor = FormatCommand(Executable, L"");
  3422. EditorData.ExternalEditorOptionsAutodetect();
  3423. TEditorList EditorList;
  3424. EditorList = *WinConfiguration->EditorList;
  3425. EditorList.Insert(0, new TEditorPreferences(EditorData));
  3426. WinConfiguration->EditorList = &EditorList;
  3427. }
  3428. }
  3429. }
  3430. WinConfiguration->OfferedEditorAutoConfig = !TryNextTime;
  3431. WinConfiguration->Usage->Set(L"EditorAutoConfig", UsageState);
  3432. }
  3433. //---------------------------------------------------------------------------
  3434. void __fastcall TCustomScpExplorerForm::ExecuteFileNormalize(
  3435. TExecuteFileBy & ExecuteFileBy, const TEditorData *& ExternalEditor,
  3436. const UnicodeString & FileName, bool Local, const TFileMasks::TParams & MaskParams)
  3437. {
  3438. if (ExecuteFileBy == efDefaultEditor)
  3439. {
  3440. if (!WinConfiguration->OfferedEditorAutoConfig)
  3441. {
  3442. EditorAutoConfig();
  3443. }
  3444. const TEditorPreferences * Editor =
  3445. WinConfiguration->DefaultEditorForFile(FileName, Local, MaskParams);
  3446. if ((Editor == NULL) || (Editor->Data->Editor == edInternal))
  3447. {
  3448. ExecuteFileBy = efInternalEditor;
  3449. ExternalEditor = NULL;
  3450. }
  3451. else if (Editor->Data->Editor == edOpen)
  3452. {
  3453. ExecuteFileBy = efShell;
  3454. ExternalEditor = NULL;
  3455. }
  3456. else
  3457. {
  3458. ExecuteFileBy = efExternalEditor;
  3459. ExternalEditor = Editor->Data;
  3460. }
  3461. }
  3462. }
  3463. //---------------------------------------------------------------------------
  3464. void __fastcall TCustomScpExplorerForm::ExecuteFile(TOperationSide Side,
  3465. TExecuteFileBy ExecuteFileBy, const TEditorData * ExternalEditor,
  3466. UnicodeString FullFileName, TObject * Object, const TFileMasks::TParams & MaskParams)
  3467. {
  3468. UnicodeString OriginalFileName;
  3469. UnicodeString LocalRootDirectory;
  3470. UnicodeString RemoteDirectory;
  3471. ExecuteFileNormalize(ExecuteFileBy, ExternalEditor, FullFileName,
  3472. IsSideLocalBrowser(Side), MaskParams);
  3473. UnicodeString Counter;
  3474. UnicodeString LocalFileName;
  3475. bool Handled = false;
  3476. if (!IsSideLocalBrowser(Side))
  3477. {
  3478. // We need to trim VMS version here, so that we use name without version
  3479. // when uploading back to create a new version of the file
  3480. OriginalFileName = Terminal->GetBaseFileName(UnixExtractFileName(FullFileName));
  3481. RemoteDirectory = UnixExtractFilePath(FullFileName);
  3482. TObject * Token = NULL;
  3483. UnicodeString LocalDirectory;
  3484. if (!FEditorManager->CanAddFile(RemoteDirectory, OriginalFileName,
  3485. Terminal->SessionData->SessionName, Token, LocalRootDirectory,
  3486. LocalDirectory))
  3487. {
  3488. if (Token != NULL)
  3489. {
  3490. TForm * Form = dynamic_cast<TForm *>(Token);
  3491. if (Form->WindowState == wsMinimized)
  3492. {
  3493. ShowWindow(Form->Handle, SW_RESTORE);
  3494. }
  3495. else
  3496. {
  3497. Form->SetFocus();
  3498. }
  3499. Handled = true;
  3500. }
  3501. else
  3502. {
  3503. throw Exception(FMTLOAD(ALREADY_EDITED_EXTERNALLY_OR_UPLOADED, (OriginalFileName)));
  3504. }
  3505. }
  3506. if (!Handled)
  3507. {
  3508. TStringList * FileList1 = new TStringList();
  3509. try
  3510. {
  3511. FileList1->AddObject(FullFileName, Object);
  3512. TemporarilyDownloadFiles(FileList1,
  3513. RemoteExecuteForceText(ExecuteFileBy, ExternalEditor),
  3514. LocalRootDirectory, LocalDirectory, true, true, false);
  3515. LocalFileName = LocalDirectory + FileList1->Strings[0];
  3516. }
  3517. __finally
  3518. {
  3519. delete FileList1;
  3520. }
  3521. switch (ExecuteFileBy)
  3522. {
  3523. case efShell:
  3524. Counter = "RemoteFilesExecuted";
  3525. break;
  3526. case efInternalEditor:
  3527. Counter = "RemoteFilesOpenedInInternalEditor";
  3528. break;
  3529. case efExternalEditor:
  3530. Counter = "RemoteFilesOpenedInExternalEditor";
  3531. break;
  3532. default:
  3533. DebugFail();
  3534. }
  3535. }
  3536. }
  3537. else
  3538. {
  3539. LocalFileName = FullFileName;
  3540. OriginalFileName = ExtractFileName(FullFileName);
  3541. switch (ExecuteFileBy)
  3542. {
  3543. case efShell:
  3544. Counter = "LocalFilesExecuted";
  3545. break;
  3546. case efInternalEditor:
  3547. Counter = "LocalFilesOpenedInInternalEditor";
  3548. break;
  3549. case efExternalEditor:
  3550. Counter = "LocalFilesOpenedInExternalEditor";
  3551. break;
  3552. default:
  3553. DebugFail();
  3554. }
  3555. }
  3556. if (!Handled)
  3557. {
  3558. Configuration->Usage->Inc(Counter);
  3559. CustomExecuteFile(Side, ExecuteFileBy, LocalFileName, OriginalFileName,
  3560. ExternalEditor, LocalRootDirectory, RemoteDirectory, false, MaskParams.Modification);
  3561. }
  3562. }
  3563. //---------------------------------------------------------------------------
  3564. void __fastcall TCustomScpExplorerForm::ExecuteFile(TOperationSide Side,
  3565. TExecuteFileBy ExecuteFileBy, const TEditorData * ExternalEditor,
  3566. bool AllSelected, bool OnFocused)
  3567. {
  3568. DebugAssert(!WinConfiguration->DisableOpenEdit);
  3569. DebugAssert((ExecuteFileBy == efExternalEditor) ==
  3570. ((ExternalEditor != NULL) && (ExternalEditor->Editor == edExternal)));
  3571. Side = GetSide(Side);
  3572. TCustomDirView * DView = DirView(Side);
  3573. TStrings * FileList =
  3574. AllSelected ?
  3575. DView->CreateFileList(OnFocused, IsSideLocalBrowser(Side)) :
  3576. DView->CreateFocusedFileList(IsSideLocalBrowser(Side));
  3577. try
  3578. {
  3579. DebugAssert(AllSelected || (FileList->Count == 1));
  3580. for (int i = 0; i < FileList->Count; i++)
  3581. {
  3582. UnicodeString ListFileName = FileList->Strings[i];
  3583. UnicodeString FileNameOnly =
  3584. !IsSideLocalBrowser(Side) ? UnixExtractFileName(ListFileName) : ExtractFileName(ListFileName);
  3585. TListItem * Item = DView->FindFileItem(FileNameOnly);
  3586. if (!DView->ItemIsDirectory(Item))
  3587. {
  3588. UnicodeString FullFileName;
  3589. if (!IsSideLocalBrowser(Side))
  3590. {
  3591. FullFileName = DirView(Side)->Path + ListFileName;
  3592. }
  3593. else
  3594. {
  3595. FullFileName = ListFileName;
  3596. }
  3597. TObject * Object = FileList->Objects[i];
  3598. TFileMasks::TParams MaskParams;
  3599. MaskParams.Size = DView->ItemFileSize(Item);
  3600. TDateTimePrecision Precision;
  3601. MaskParams.Modification = DView->ItemFileTime(Item, Precision);
  3602. std::unique_ptr<TRemoteFile> File;
  3603. if (!IsSideLocalBrowser(Side))
  3604. {
  3605. File.reset(Terminal->TryReadFile(FullFileName));
  3606. if (File.get() != NULL)
  3607. {
  3608. File->FullFileName = FullFileName;
  3609. Object = File.get();
  3610. MaskParams.Size = File->Size;
  3611. MaskParams.Modification = File->Modification;
  3612. }
  3613. }
  3614. ExecuteFile(Side, ExecuteFileBy, ExternalEditor, FullFileName, Object, MaskParams);
  3615. }
  3616. }
  3617. }
  3618. __finally
  3619. {
  3620. delete FileList;
  3621. }
  3622. }
  3623. //---------------------------------------------------------------------------
  3624. void __fastcall TCustomScpExplorerForm::TemporaryFileCopyParam(TCopyParamType & CopyParam)
  3625. {
  3626. CopyParam.FileNameCase = ncNoChange;
  3627. CopyParam.PreserveRights = false;
  3628. CopyParam.PreserveReadOnly = false;
  3629. CopyParam.ReplaceInvalidChars = true;
  3630. CopyParam.IncludeFileMask = TFileMasks();
  3631. CopyParam.NewerOnly = false;
  3632. CopyParam.FileMask = L"";
  3633. }
  3634. //---------------------------------------------------------------------------
  3635. bool TCustomScpExplorerForm::EditorCheckNotModified(const TEditedFileData * Data)
  3636. {
  3637. return WinConfiguration->EditorCheckNotModified && (Data->SourceTimestamp != TDateTime());
  3638. }
  3639. //---------------------------------------------------------------------------
  3640. class TEditorUploadQueueItem : public TUploadQueueItem
  3641. {
  3642. public:
  3643. __fastcall TEditorUploadQueueItem(
  3644. TTerminal * Terminal, TStrings * FilesToCopy, const UnicodeString & TargetDir,
  3645. const TCopyParamType * CopyParam, int Params) :
  3646. TUploadQueueItem(Terminal, FilesToCopy, TargetDir, CopyParam, Params, false)
  3647. {
  3648. }
  3649. protected:
  3650. virtual void __fastcall DoTransferExecute(TTerminal * Terminal, TParallelOperation * ParallelOperation)
  3651. {
  3652. TUploadQueueItem::DoTransferExecute(Terminal, ParallelOperation);
  3653. TTerminalManager::Instance()->ScpExplorer->EditedFileUploaded(Terminal, CompleteEvent);
  3654. }
  3655. };
  3656. //---------------------------------------------------------------------------
  3657. void TCustomScpExplorerForm::EditedFileUploaded(TTerminal * ATerminal, HANDLE UploadCompleteEvent)
  3658. {
  3659. if (WinConfiguration->EditorCheckNotModified) // optimization
  3660. {
  3661. UnicodeString RemoteFilePath;
  3662. {
  3663. TGuard Guard(FEditorManager->Section);
  3664. TEditedFileData * Data = FEditorManager->FindByUploadCompleteEvent(UploadCompleteEvent);
  3665. if (DebugAlwaysTrue(Data != NULL) &&
  3666. EditorCheckNotModified(Data))
  3667. {
  3668. RemoteFilePath = UnixCombinePaths(Data->RemoteDirectory, Data->OriginalFileName);
  3669. }
  3670. }
  3671. if (!RemoteFilePath.IsEmpty())
  3672. {
  3673. std::unique_ptr<TRemoteFile> File(ATerminal->TryReadFile(RemoteFilePath));
  3674. if (File.get() != NULL)
  3675. {
  3676. TGuard Guard(FEditorManager->Section);
  3677. TEditedFileData * Data = FEditorManager->FindByUploadCompleteEvent(UploadCompleteEvent);
  3678. if (DebugAlwaysTrue(Data != NULL))
  3679. {
  3680. Data->SourceTimestamp = File->Modification;
  3681. AppLogFmt(L"Uploaded edited remote file timestamp: %s", (StandardTimestamp(Data->SourceTimestamp)));
  3682. }
  3683. }
  3684. }
  3685. }
  3686. }
  3687. //---------------------------------------------------------------------------
  3688. void __fastcall TCustomScpExplorerForm::ExecutedFileChanged(
  3689. const UnicodeString & FileName, const TDateTime & Timestamp, TEditedFileData * Data,
  3690. HANDLE UploadCompleteEvent, bool & Retry)
  3691. {
  3692. TTerminalManager * Manager = TTerminalManager::Instance();
  3693. bool IsInactive = !IsActiveTerminal(Data->Terminal);
  3694. if ((Manager->ActiveTerminal == Data->Terminal) &&
  3695. Manager->ScheduleTerminalReconnnect(Data->Terminal))
  3696. {
  3697. AppLog(L"Scheduled session reconnect, will retry the upload later");
  3698. Retry = true;
  3699. }
  3700. else if (Manager->IsReconnectingTerminal(Data->Terminal))
  3701. {
  3702. AppLog(L"Session is being reconnected, will retry the upload later");
  3703. Retry = true;
  3704. }
  3705. else if (IsInactive &&
  3706. GUIConfiguration->SessionReopenAutoInactive &&
  3707. (Data->Terminal->NextInactiveReconnect != TDateTime()))
  3708. {
  3709. if (Data->Terminal->LastInactiveSave != Timestamp)
  3710. {
  3711. AppLog(L"Inactive session reconnect is pending, rescheduling it for as soon as possible to retry the upload");
  3712. Data->Terminal->NextInactiveReconnect = Now();
  3713. Data->Terminal->LastInactiveSave = Timestamp;
  3714. }
  3715. else
  3716. {
  3717. AppLog(L"Inactive session reconnect is pending, will retry the upload later");
  3718. }
  3719. Retry = true;
  3720. }
  3721. else if (IsInactive)
  3722. {
  3723. if (!NonVisualDataModule->Busy)
  3724. {
  3725. UnicodeString FileNameOnly = ExtractFileName(FileName);
  3726. UnicodeString EditInactiveSessionReopenAcceptedCounter = L"EditInactiveSessionReopenAccepted";
  3727. UnicodeString EditInactiveSessionReopenRejectedCounter = L"EditInactiveSessionReopenRejected";
  3728. if (Data->Terminal == NULL)
  3729. {
  3730. TManagedTerminal * SameSiteTerminal = Manager->FindActiveTerminalForSite(Data->SessionData);
  3731. if (SameSiteTerminal != NULL)
  3732. {
  3733. UnicodeString Message =
  3734. FMTLOAD(EDIT_SESSION_REATTACH,
  3735. (FileNameOnly, Data->SessionName, FileNameOnly));
  3736. if (MessageDialog(Message, qtConfirmation, qaOK | qaCancel) == qaOK)
  3737. {
  3738. Data->Terminal = SameSiteTerminal;
  3739. Data->SessionName = SameSiteTerminal->SessionData->SessionName;
  3740. // We might also overwrite session data
  3741. Configuration->Usage->Inc(EditInactiveSessionReopenAcceptedCounter);
  3742. }
  3743. else
  3744. {
  3745. Configuration->Usage->Inc(EditInactiveSessionReopenRejectedCounter);
  3746. Abort();
  3747. }
  3748. }
  3749. }
  3750. // Foreground session should reconnect itself (except for those disconnected explicitly by the user)
  3751. else if (DebugAlwaysTrue(Terminal->Disconnected || (Terminal != Data->Terminal)))
  3752. {
  3753. UnicodeString Message =
  3754. MainInstructions(
  3755. FMTLOAD(EDIT_SESSION_RECONNECT, (Data->SessionName, FileNameOnly)));
  3756. TMessageParams Params(FLAGMASK(!GUIConfiguration->SessionReopenAutoInactive, mpNeverAskAgainCheck));
  3757. Params.NeverAskAgainTitle = LoadStr(ALWAYS_RECONNECT);
  3758. unsigned int Answer = MessageDialog(Message, qtConfirmation, qaOK | qaCancel, HELP_NONE, &Params);
  3759. if (Answer == qaNeverAskAgain)
  3760. {
  3761. GUIConfiguration->SessionReopenAutoInactive = true;
  3762. Answer = qaOK;
  3763. }
  3764. if (Answer == qaOK)
  3765. {
  3766. if (Terminal->Disconnected)
  3767. {
  3768. Manager->ActiveSession = Terminal;
  3769. ReconnectSession();
  3770. // Was it reconnected? (if not, Data->Terminal should be nulled now)
  3771. if (!IsActiveTerminal(Data->Terminal) || (Terminal != Data->Terminal))
  3772. {
  3773. Abort();
  3774. }
  3775. }
  3776. else
  3777. {
  3778. Manager->SetActiveTerminalWithAutoReconnect(Data->Terminal);
  3779. }
  3780. Configuration->Usage->Inc(EditInactiveSessionReopenAcceptedCounter);
  3781. }
  3782. else
  3783. {
  3784. Configuration->Usage->Inc(EditInactiveSessionReopenRejectedCounter);
  3785. Abort();
  3786. }
  3787. }
  3788. }
  3789. if (!IsActiveTerminal(Data->Terminal))
  3790. {
  3791. Configuration->Usage->Inc(L"EditInactiveSession");
  3792. // Prevent this when not idle (!NonVisualDataModule->Busy)?
  3793. throw Exception(FMTLOAD(EDIT_SESSION_CLOSED2,
  3794. (ExtractFileName(FileName), Data->SessionName)));
  3795. }
  3796. }
  3797. if (!Retry && EditorCheckNotModified(Data))
  3798. {
  3799. if (NonVisualDataModule->Busy)
  3800. {
  3801. Retry = true;
  3802. }
  3803. else
  3804. {
  3805. UnicodeString RemoteFilePath = UnixCombinePaths(Data->RemoteDirectory, Data->OriginalFileName);
  3806. std::unique_ptr<TRemoteFile> File(Terminal->TryReadFile(RemoteFilePath));
  3807. if (File.get() != NULL)
  3808. {
  3809. AppLogFmt(L"Edited remote file timestamp: %s, Original timestamp: %s", (StandardTimestamp(File->Modification), StandardTimestamp(Data->SourceTimestamp)));
  3810. if (File->Modification != Data->SourceTimestamp)
  3811. {
  3812. UnicodeString Message = MainInstructions(LoadStr(EDIT_CHANGED_EXTERNALLY));
  3813. if (MessageDialog(Message, qtConfirmation, qaOK | qaCancel) != qaOK)
  3814. {
  3815. Abort();
  3816. }
  3817. }
  3818. }
  3819. }
  3820. }
  3821. if (!Retry)
  3822. {
  3823. TStrings * FileList = new TStringList();
  3824. try
  3825. {
  3826. FileList->Add(FileName);
  3827. // Consider using the same settings (preset) as when the file was downloaded.
  3828. // More over this does not reflect the actual session that will do the upload.
  3829. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  3830. TemporaryFileCopyParam(CopyParam);
  3831. if (Data->ForceText)
  3832. {
  3833. CopyParam.TransferMode = tmAscii;
  3834. }
  3835. // so i do not need to worry if masking algorithm works in all cases
  3836. // ("" means "copy file name", no masking is actually done)
  3837. if (ExtractFileName(FileName) == Data->OriginalFileName)
  3838. {
  3839. CopyParam.FileMask = L"";
  3840. }
  3841. else
  3842. {
  3843. CopyParam.FileMask = DelimitFileNameMask(Data->OriginalFileName);
  3844. }
  3845. int Params = cpNoConfirmation | cpTemporary;
  3846. if (Data->Terminal->IsCapable[fcBackgroundTransfers])
  3847. {
  3848. TTerminalQueue * AQueue = Manager->FindQueueForTerminal(Data->Terminal);
  3849. TQueueItem * QueueItem =
  3850. new TEditorUploadQueueItem(Data->Terminal, FileList, Data->RemoteDirectory, &CopyParam, Params);
  3851. QueueItem->CompleteEvent = UploadCompleteEvent;
  3852. AddQueueItem(AQueue, QueueItem, Data->Terminal);
  3853. }
  3854. else
  3855. {
  3856. if (NonVisualDataModule->Busy)
  3857. {
  3858. Retry = true;
  3859. }
  3860. else
  3861. {
  3862. FMoveToQueue = false;
  3863. Data->Terminal->CopyToRemote(FileList, Data->RemoteDirectory, &CopyParam, Params, NULL);
  3864. SetEvent(UploadCompleteEvent);
  3865. }
  3866. }
  3867. }
  3868. __finally
  3869. {
  3870. delete FileList;
  3871. }
  3872. }
  3873. }
  3874. //---------------------------------------------------------------------------
  3875. void __fastcall TCustomScpExplorerForm::ExecutedFileReload(
  3876. const UnicodeString & FileName, TEditedFileData * Data)
  3877. {
  3878. // Sanity check, we should not be busy otherwise user would not be able to click Reload button.
  3879. DebugAssert(!NonVisualDataModule->Busy);
  3880. if (!IsActiveTerminal(Data->Terminal))
  3881. {
  3882. throw Exception(FMTLOAD(EDIT_SESSION_CLOSED_RELOAD,
  3883. (ExtractFileName(FileName), Data->SessionName)));
  3884. }
  3885. TManagedTerminal * PrevSession = TTerminalManager::Instance()->ActiveSession;
  3886. TTerminalManager::Instance()->ActiveSession = Data->Terminal;
  3887. NonVisualDataModule->StartBusy();
  3888. try
  3889. {
  3890. std::unique_ptr<TRemoteFile> File;
  3891. UnicodeString RemoteFileName =
  3892. UnixIncludeTrailingBackslash(Data->RemoteDirectory) + Data->OriginalFileName;
  3893. Terminal->ExceptionOnFail = true;
  3894. try
  3895. {
  3896. File.reset(Terminal->ReadFile(RemoteFileName));
  3897. if (!File->HaveFullFileName)
  3898. {
  3899. File->FullFileName = RemoteFileName;
  3900. }
  3901. }
  3902. __finally
  3903. {
  3904. Terminal->ExceptionOnFail = false;
  3905. }
  3906. std::unique_ptr<TStrings> FileList(new TStringList());
  3907. FileList->AddObject(RemoteFileName, File.get());
  3908. UnicodeString RootTempDir = Data->LocalRootDirectory;
  3909. UnicodeString TempDir = ExtractFilePath(FileName);
  3910. TemporarilyDownloadFiles(FileList.get(), Data->ForceText, RootTempDir, TempDir, true, true, false);
  3911. if (EditorCheckNotModified(Data))
  3912. {
  3913. Data->SourceTimestamp = File->Modification;
  3914. }
  3915. // sanity check, the target file name should be still the same
  3916. DebugAssert(ExtractFileName(FileName) == FileList->Strings[0]);
  3917. }
  3918. __finally
  3919. {
  3920. NonVisualDataModule->EndBusy();
  3921. // it actually may not exist anymore...
  3922. TTerminalManager::Instance()->ActiveSession = PrevSession;
  3923. }
  3924. }
  3925. //---------------------------------------------------------------------------
  3926. void __fastcall TCustomScpExplorerForm::ExecutedFileEarlyClosed(
  3927. const TEditedFileData * Data, bool & KeepOpen)
  3928. {
  3929. // Command is set for external editors only (not for "shell" open).
  3930. if (!Data->Command.IsEmpty())
  3931. {
  3932. bool AnyFound = false;
  3933. bool AnyMDI = false;
  3934. bool AnyNonMDI = false;
  3935. bool AnyDetect = false;
  3936. TEditorList * EditorList = new TEditorList();
  3937. try
  3938. {
  3939. *EditorList = *WinConfiguration->EditorList;
  3940. for (int i = 0; i < EditorList->Count; i++)
  3941. {
  3942. const TEditorPreferences * Editor = EditorList->Editors[i];
  3943. if ((Editor->Data->Editor == edExternal) &&
  3944. (Editor->Data->ExternalEditor == Data->Command))
  3945. {
  3946. AnyFound = true;
  3947. if (Editor->Data->SDIExternalEditor)
  3948. {
  3949. AnyNonMDI = true;
  3950. if (Editor->Data->DetectMDIExternalEditor)
  3951. {
  3952. AnyDetect = true;
  3953. }
  3954. }
  3955. else
  3956. {
  3957. AnyMDI = true;
  3958. }
  3959. }
  3960. }
  3961. bool EnableMDI = false;
  3962. bool DisableDetect = false;
  3963. if (AnyMDI)
  3964. {
  3965. KeepOpen = true;
  3966. if (AnyNonMDI)
  3967. {
  3968. // there is at least one instance of the editor with MDI support enabled,
  3969. // and one with disabled, enable it for all instances
  3970. EnableMDI = true;
  3971. }
  3972. }
  3973. else if (AnyFound && !AnyDetect)
  3974. {
  3975. // at least once instance found but all have MDI autodetection disabled
  3976. // => close the file (default action)
  3977. }
  3978. else
  3979. {
  3980. // no instance of the editor has MDI support enabled
  3981. TMessageParams Params;
  3982. if (AnyFound)
  3983. {
  3984. // there is at least one instance of the editor
  3985. Params.Params |= mpNeverAskAgainCheck;
  3986. }
  3987. unsigned int Answer = MessageDialog(FMTLOAD(EDITOR_EARLY_CLOSED2, (Data->OriginalFileName)), qtWarning,
  3988. qaYes | qaNo, HELP_EDITOR_EARLY_CLOSED, &Params);
  3989. switch (Answer)
  3990. {
  3991. case qaNeverAskAgain:
  3992. DisableDetect = true;
  3993. break;
  3994. case qaNo:
  3995. EnableMDI = true;
  3996. KeepOpen = true;
  3997. break;
  3998. }
  3999. }
  4000. if (AnyFound && (EnableMDI || DisableDetect))
  4001. {
  4002. bool Changed = false;
  4003. for (int i = 0; i < EditorList->Count; i++)
  4004. {
  4005. const TEditorPreferences * Editor = EditorList->Editors[i];
  4006. if ((Editor->Data->Editor == edExternal) &&
  4007. (Editor->Data->ExternalEditor == Data->Command) &&
  4008. ((EnableMDI && Editor->Data->SDIExternalEditor) ||
  4009. (DisableDetect && Editor->Data->DetectMDIExternalEditor)))
  4010. {
  4011. Changed = true;
  4012. TEditorPreferences * UpdatedEditor = new TEditorPreferences(*Editor);
  4013. if (EnableMDI)
  4014. {
  4015. UpdatedEditor->GetData()->SDIExternalEditor = false;
  4016. }
  4017. if (DisableDetect)
  4018. {
  4019. UpdatedEditor->GetData()->DetectMDIExternalEditor = false;
  4020. }
  4021. EditorList->Change(i, UpdatedEditor);
  4022. }
  4023. }
  4024. if (Changed)
  4025. {
  4026. WinConfiguration->EditorList = EditorList;
  4027. }
  4028. }
  4029. }
  4030. __finally
  4031. {
  4032. delete EditorList;
  4033. }
  4034. }
  4035. else
  4036. {
  4037. // "open" case
  4038. MessageDialog(FMTLOAD(APP_EARLY_CLOSED, (Data->OriginalFileName)), qtWarning,
  4039. qaOK, HELP_APP_EARLY_CLOSED);
  4040. }
  4041. }
  4042. //---------------------------------------------------------------------------
  4043. void __fastcall TCustomScpExplorerForm::ExecutedFileUploadComplete(TObject * Sender)
  4044. {
  4045. EditorFormFileUploadComplete(DebugNotNull(dynamic_cast<TForm *>(Sender)));
  4046. }
  4047. //---------------------------------------------------------------------------
  4048. void __fastcall TCustomScpExplorerForm::RemoteDirViewEnter(TObject * /*Sender*/)
  4049. {
  4050. SideEnter(osRemote);
  4051. }
  4052. //---------------------------------------------------------------------------
  4053. void __fastcall TCustomScpExplorerForm::RemoteDriveViewEnter(TObject * /*Sender*/)
  4054. {
  4055. MakeNextInTabOrder(RemoteDirPanel, RemoteDrivePanel);
  4056. SideEnter(osRemote);
  4057. }
  4058. //---------------------------------------------------------------------------
  4059. void __fastcall TCustomScpExplorerForm::SideEnter(TOperationSide Side)
  4060. {
  4061. FCurrentSide = Side;
  4062. if (Visible)
  4063. {
  4064. UpdateControls();
  4065. }
  4066. }
  4067. //---------------------------------------------------------------------------
  4068. void __fastcall TCustomScpExplorerForm::FileDeleted(
  4069. TOperationSide Side, const UnicodeString & FileName, bool Success, bool NotCancelled)
  4070. {
  4071. VisualiseOperationFinished(Side, FileName, Success && NotCancelled);
  4072. if (DebugAlwaysTrue(FDeletedFiles != NULL) && Success)
  4073. {
  4074. FDeletedFiles->Add(FileName);
  4075. }
  4076. }
  4077. //---------------------------------------------------------------------------
  4078. void __fastcall TCustomScpExplorerForm::DeleteFiles(TOperationSide Side,
  4079. TStrings * FileList, bool Alternative)
  4080. {
  4081. TCustomDirView * DView = DirView(Side);
  4082. DView->SaveSelection();
  4083. DView->SaveSelectedNames();
  4084. DebugAssert(!FAlternativeDelete);
  4085. FAlternativeDelete = Alternative;
  4086. try
  4087. {
  4088. int Params = FLAGMASK(Alternative, dfAlternative);
  4089. bool Remote = !IsSideLocalBrowser(Side);
  4090. std::unique_ptr<TStringList> DeletedFiles;
  4091. std::unique_ptr<TStrings> PermanentFileList;
  4092. bool AllowMoveToQueue = (Terminal != NULL);
  4093. if (AllowMoveToQueue)
  4094. {
  4095. DeletedFiles.reset(new TStringList());
  4096. DeletedFiles->CaseSensitive = Remote;
  4097. }
  4098. try
  4099. {
  4100. DebugAssert(FDeletedFiles == NULL);
  4101. TValueRestorer<TStrings *> DeletedFilesRestorer(FDeletedFiles, DeletedFiles.get());
  4102. TValueRestorer<TFileOperationFinishedEvent> OnFileOperationFinishedRestorer(FOnFileOperationFinished);
  4103. if (AllowMoveToQueue)
  4104. {
  4105. FOnFileOperationFinished = FileDeleted;
  4106. }
  4107. FMoveToQueue = false;
  4108. if (Remote)
  4109. {
  4110. DebugAssert(AllowMoveToQueue);
  4111. // Clone the file list as it may refer to current directory files, which get destroyed, when the directory is reloaded after the operation
  4112. PermanentFileList.reset(TRemoteFileList::CloneStrings(FileList));
  4113. DebugAssert(Terminal != NULL);
  4114. Terminal->DeleteFiles(FileList, Params);
  4115. }
  4116. else
  4117. {
  4118. if (AllowMoveToQueue) // optimization)
  4119. {
  4120. PermanentFileList.reset(CloneStrings(FileList));
  4121. }
  4122. if (IsLocalBrowserMode())
  4123. {
  4124. Configuration->Usage->Inc(L"LocalLocalDeletes");
  4125. }
  4126. TValueRestorer<TOperationSide> ProgressSideRestorer(FProgressSide, FCurrentSide);
  4127. try
  4128. {
  4129. TTerminalManager::Instance()->LocalTerminal->DeleteLocalFiles(FileList, Params);
  4130. }
  4131. __finally
  4132. {
  4133. ReloadLocalDirectory();
  4134. }
  4135. }
  4136. // Probably not needed for deleting, just for consistency with transfer code
  4137. if (FMoveToQueue)
  4138. {
  4139. Abort();
  4140. }
  4141. }
  4142. catch (EAbort &)
  4143. {
  4144. if (FMoveToQueue && DebugAlwaysTrue(AllowMoveToQueue) && DebugAlwaysTrue(Terminal != NULL))
  4145. {
  4146. FMoveToQueue = false;
  4147. DeletedFiles->Sorted = true;
  4148. for (int Index = 0; Index < PermanentFileList->Count; Index++)
  4149. {
  4150. if (DeletedFiles->IndexOf(PermanentFileList->Strings[Index]) >= 0)
  4151. {
  4152. // We should always be deleting the first item => what can be used to optimize this code, if needed
  4153. DebugAssert(Index == 0);
  4154. PermanentFileList->Delete(Index);
  4155. Index--;
  4156. }
  4157. }
  4158. if (Remote)
  4159. {
  4160. Configuration->Usage->Inc("MovesToBackgroundDelete");
  4161. TQueueItem * QueueItem = new TRemoteDeleteQueueItem(Terminal, PermanentFileList.get(), Params);
  4162. AddQueueItem(Queue, QueueItem, Terminal);
  4163. }
  4164. else
  4165. {
  4166. Configuration->Usage->Inc("MovesToBackgroundDeleteLocal");
  4167. TQueueItem * QueueItem = new TLocalDeleteQueueItem(PermanentFileList.get(), Params);
  4168. AddQueueItem(Queue, QueueItem, Terminal);
  4169. }
  4170. ClearOperationSelection(Side);
  4171. }
  4172. }
  4173. FAlternativeDelete = false;
  4174. }
  4175. catch(...)
  4176. {
  4177. FAlternativeDelete = false;
  4178. DView->DiscardSavedSelection();
  4179. throw;
  4180. }
  4181. DView->RestoreSelection();
  4182. }
  4183. //---------------------------------------------------------------------------
  4184. void __fastcall TCustomScpExplorerForm::LockFiles(TStrings * FileList, bool Lock)
  4185. {
  4186. DebugAssert(Terminal);
  4187. DebugAssert(!IsLocalBrowserMode());
  4188. RemoteDirView->SaveSelection();
  4189. RemoteDirView->SaveSelectedNames();
  4190. try
  4191. {
  4192. if (Lock)
  4193. {
  4194. Terminal->LockFiles(FileList);
  4195. }
  4196. else
  4197. {
  4198. Terminal->UnlockFiles(FileList);
  4199. }
  4200. }
  4201. catch(...)
  4202. {
  4203. RemoteDirView->DiscardSavedSelection();
  4204. throw;
  4205. }
  4206. RemoteDirView->RestoreSelection();
  4207. }
  4208. //---------------------------------------------------------------------------
  4209. bool TCustomScpExplorerForm::DoDirectoryExists(void * Session, const UnicodeString & Directory)
  4210. {
  4211. TTerminal * ATerminal = (Session == NULL) ? Terminal : reinterpret_cast<TTerminal *>(Session);
  4212. bool Result =
  4213. IsActiveTerminal(ATerminal) &&
  4214. ATerminal->DirectoryExists(Directory);
  4215. return Result;
  4216. }
  4217. //---------------------------------------------------------------------------
  4218. bool TCustomScpExplorerForm::NeedSecondarySessionForRemoteCopy(TStrings * FileList)
  4219. {
  4220. bool CopyDirsOnSecondarySession = Terminal->IsCapable[fcSecondaryShell];
  4221. return
  4222. !Terminal->IsCapable[fcRemoteCopy] ||
  4223. (CopyDirsOnSecondarySession && TRemoteFileList::AnyDirectory(FileList));
  4224. }
  4225. //---------------------------------------------------------------------------
  4226. bool __fastcall TCustomScpExplorerForm::RemoteTransferDialog(TManagedTerminal *& Session,
  4227. TStrings * FileList, UnicodeString & Target, UnicodeString & FileMask, bool & DirectCopy,
  4228. bool NoConfirmation, bool Move)
  4229. {
  4230. DebugAssert(Terminal != NULL);
  4231. DebugAssert(Terminal == TTerminalManager::Instance()->ActiveTerminal);
  4232. DebugAssert(!IsLocalBrowserMode());
  4233. if (Session == NULL)
  4234. {
  4235. Session = Terminal;
  4236. }
  4237. bool TargetConfirmed = false;
  4238. if (Session == Terminal)
  4239. {
  4240. if (RemoteDriveView->DropTarget != NULL)
  4241. {
  4242. Target = RemoteDriveView->NodePathName(RemoteDriveView->DropTarget);
  4243. TargetConfirmed = true;
  4244. }
  4245. else if (RemoteDirView->DropTarget != NULL)
  4246. {
  4247. DebugAssert(RemoteDirView->ItemIsDirectory(RemoteDirView->DropTarget));
  4248. Target = RemoteDirView->ItemFullFileName(RemoteDirView->DropTarget);
  4249. TargetConfirmed = true;
  4250. }
  4251. else
  4252. {
  4253. Target = RemoteDirView->Path;
  4254. }
  4255. }
  4256. else
  4257. {
  4258. Target = GetSessionPath(Session, osRemote);
  4259. }
  4260. Target = UnixIncludeTrailingBackslash(Target);
  4261. if (FileList->Count == 1)
  4262. {
  4263. FileMask = DelimitFileNameMask(UnixExtractFileName(FileList->Strings[0]));
  4264. }
  4265. else
  4266. {
  4267. FileMask = AnyMask;
  4268. }
  4269. DirectCopy = Terminal->IsCapable[fcRemoteCopy] || Terminal->IsCapable[fcSecondaryShell];
  4270. bool Result = true;
  4271. if (!NoConfirmation)
  4272. {
  4273. bool Multi = (FileList->Count > 1);
  4274. if (Move)
  4275. {
  4276. Result = DoRemoteMoveDialog(Multi, Target, FileMask, DoDirectoryExists);
  4277. }
  4278. else
  4279. {
  4280. std::unique_ptr<TStrings> Sessions(new TStringList);
  4281. std::unique_ptr<TStrings> Directories(new TStringList);
  4282. TStrings * SessionList = TTerminalManager::Instance()->SessionList;
  4283. for (int Index = 0; Index < SessionList->Count; Index++)
  4284. {
  4285. TManagedTerminal * ASession = DebugNotNull(dynamic_cast<TManagedTerminal *>(SessionList->Objects[Index]));
  4286. if (IsActiveTerminal(ASession) && DebugAlwaysTrue(!ASession->LocalBrowser))
  4287. {
  4288. Sessions->AddObject(SessionList->Strings[Index], ASession);
  4289. Directories->Add(GetSessionPath(ASession, osRemote));
  4290. }
  4291. }
  4292. bool AnyDirectory = false;
  4293. bool CopyDirsOnSecondarySession = Terminal->IsCapable[fcSecondaryShell];
  4294. if (CopyDirsOnSecondarySession && !Terminal->CommandSessionOpened) // optimization
  4295. {
  4296. for (int Index = 0; !AnyDirectory && (Index < FileList->Count); Index++)
  4297. {
  4298. AnyDirectory = DebugNotNull(dynamic_cast<TRemoteFile *>(FileList->Objects[Index]))->IsDirectory;
  4299. }
  4300. }
  4301. TDirectRemoteCopy AllowDirectCopy;
  4302. if (Terminal->CommandSessionOpened || !NeedSecondarySessionForRemoteCopy(FileList))
  4303. {
  4304. DebugAssert(DirectCopy);
  4305. AllowDirectCopy = drcAllow;
  4306. }
  4307. else if (Terminal->IsCapable[fcSecondaryShell])
  4308. {
  4309. DebugAssert(DirectCopy);
  4310. AllowDirectCopy = Terminal->IsCapable[fcRemoteCopy] ? drcConfirmCommandSessionDirs : drcConfirmCommandSession;
  4311. }
  4312. else
  4313. {
  4314. DebugAssert(!DirectCopy);
  4315. AllowDirectCopy = drcDisallow;
  4316. }
  4317. void * ASession = Session;
  4318. Result = DoRemoteCopyDialog(
  4319. Sessions.get(), Directories.get(), AllowDirectCopy, Multi, ASession, Target, FileMask, DirectCopy, Terminal,
  4320. DoDirectoryExists, TargetConfirmed);
  4321. Session = static_cast<TManagedTerminal *>(ASession);
  4322. }
  4323. }
  4324. return Result;
  4325. }
  4326. //---------------------------------------------------------------------------
  4327. bool __fastcall TCustomScpExplorerForm::RemoteTransferFiles(
  4328. TStrings * FileList, bool NoConfirmation, bool Move, TManagedTerminal * Session)
  4329. {
  4330. DebugAssert(!IsLocalBrowserMode());
  4331. bool DirectCopy;
  4332. UnicodeString Target, FileMask;
  4333. bool Result = RemoteTransferDialog(Session, FileList, Target, FileMask, DirectCopy, NoConfirmation, Move);
  4334. if (Result)
  4335. {
  4336. if (!Move && !DirectCopy)
  4337. {
  4338. Configuration->Usage->Inc("RemoteCopyTemp");
  4339. UnicodeString RootTempDir;
  4340. UnicodeString TempDir;
  4341. TemporarilyDownloadFiles(FileList, false, RootTempDir, TempDir, false, false, true);
  4342. TStrings * TemporaryFilesList = new TStringList();
  4343. try
  4344. {
  4345. TMakeLocalFileListParams MakeFileListParam;
  4346. MakeFileListParam.FileList = TemporaryFilesList;
  4347. MakeFileListParam.FileTimes = NULL;
  4348. MakeFileListParam.IncludeDirs = true;
  4349. MakeFileListParam.Recursive = false;
  4350. ProcessLocalDirectory(TempDir, Terminal->MakeLocalFileList, &MakeFileListParam);
  4351. TTerminalManager::Instance()->ActiveSession = Session;
  4352. if (TemporaryFilesList->Count > 0)
  4353. {
  4354. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  4355. CopyParam.FileMask = FileMask;
  4356. DebugAssert(!FAutoOperation);
  4357. FAutoOperation = true;
  4358. FMoveToQueue = false;
  4359. Terminal->CopyToRemote(TemporaryFilesList, Target, &CopyParam, cpTemporary, NULL);
  4360. }
  4361. }
  4362. __finally
  4363. {
  4364. delete TemporaryFilesList;
  4365. FAutoOperation = false;
  4366. if (!RootTempDir.IsEmpty())
  4367. {
  4368. RecursiveDeleteFile(ExcludeTrailingBackslash(RootTempDir));
  4369. }
  4370. }
  4371. }
  4372. else
  4373. {
  4374. RemoteDirView->SaveSelection();
  4375. RemoteDirView->SaveSelectedNames();
  4376. try
  4377. {
  4378. if (Move)
  4379. {
  4380. Configuration->Usage->Inc("RemoteMove");
  4381. Terminal->MoveFiles(FileList, Target, FileMask, false);
  4382. }
  4383. else
  4384. {
  4385. Configuration->Usage->Inc("RemoteCopyDirect");
  4386. DebugAssert(DirectCopy);
  4387. DebugAssert(Session == Terminal);
  4388. if (!NeedSecondarySessionForRemoteCopy(FileList) ||
  4389. Terminal->CommandSessionOpened ||
  4390. CommandSessionFallback())
  4391. {
  4392. Terminal->CopyFiles(FileList, Target, FileMask, false);
  4393. }
  4394. }
  4395. }
  4396. catch(...)
  4397. {
  4398. RemoteDirView->DiscardSavedSelection();
  4399. throw;
  4400. }
  4401. RemoteDirView->RestoreSelection();
  4402. }
  4403. }
  4404. return Result;
  4405. }
  4406. //---------------------------------------------------------------------------
  4407. void __fastcall TCustomScpExplorerForm::CreateRemoteDirectory(
  4408. const UnicodeString & Path, TRemoteProperties & Properties)
  4409. {
  4410. DebugAssert(!IsLocalBrowserMode());
  4411. Properties.Valid = Properties.Valid << vpEncrypt;
  4412. Properties.Encrypt = GUIConfiguration->CurrentCopyParam.EncryptNewFiles;
  4413. RemoteDirView->CreateDirectoryEx(Path, &Properties);
  4414. }
  4415. //---------------------------------------------------------------------------
  4416. void __fastcall TCustomScpExplorerForm::CreateDirectory(TOperationSide Side)
  4417. {
  4418. Side = GetSide(Side);
  4419. TRemoteProperties Properties = GUIConfiguration->NewDirectoryProperties;
  4420. TRemoteProperties * AProperties = (!IsSideLocalBrowser(Side) ? &Properties : NULL);
  4421. UnicodeString Name = LoadStr(NEW_FOLDER);
  4422. int AllowedChanges =
  4423. FLAGMASK(!IsSideLocalBrowser(Side) && Terminal->IsCapable[fcModeChanging], cpMode);
  4424. bool SaveSettings = false;
  4425. if (DoCreateDirectoryDialog(Name, AProperties, AllowedChanges, SaveSettings))
  4426. {
  4427. TWindowLock Lock(this);
  4428. if (!IsSideLocalBrowser(Side))
  4429. {
  4430. if (SaveSettings)
  4431. {
  4432. GUIConfiguration->NewDirectoryProperties = Properties;
  4433. }
  4434. CreateRemoteDirectory(Name, Properties);
  4435. }
  4436. else
  4437. {
  4438. DirView(Side)->CreateDirectory(Name);
  4439. }
  4440. }
  4441. }
  4442. //---------------------------------------------------------------------------
  4443. void __fastcall TCustomScpExplorerForm::HomeDirectory(TOperationSide Side)
  4444. {
  4445. TWindowLock Lock(this);
  4446. DirView(Side)->ExecuteHomeDirectory();
  4447. }
  4448. //---------------------------------------------------------------------------
  4449. void __fastcall TCustomScpExplorerForm::OpenDirectory(TOperationSide Side)
  4450. {
  4451. DoOpenDirectoryDialog(odBrowse, Side);
  4452. }
  4453. //---------------------------------------------------------------------------
  4454. bool __fastcall TCustomScpExplorerForm::OpenBookmark(TOperationSide Side, TBookmark * Bookmark)
  4455. {
  4456. TOperationSide BookmarkSide = IsLocalBrowserMode() ? osLocal : Side;
  4457. UnicodeString Path = Bookmark->GetSideDirectory(BookmarkSide);
  4458. bool Result = !Path.IsEmpty();
  4459. if (Result)
  4460. {
  4461. TryOpenDirectory(Side, Path);
  4462. }
  4463. return Result;
  4464. }
  4465. //---------------------------------------------------------------------------
  4466. void __fastcall TCustomScpExplorerForm::CalculateSize(
  4467. TStrings * FileList, __int64 & Size, TCalculateSizeStats & Stats,
  4468. bool & Close)
  4469. {
  4470. // terminal can be already closed (e.g. dropped connection)
  4471. if (Terminal != NULL)
  4472. {
  4473. try
  4474. {
  4475. TCalculateSizeParams Params;
  4476. Params.Stats = &Stats;
  4477. Terminal->CalculateFilesSize(FileList, Size, Params);
  4478. }
  4479. catch(...)
  4480. {
  4481. if (!Terminal->Active)
  4482. {
  4483. Close = true;
  4484. }
  4485. throw;
  4486. }
  4487. }
  4488. }
  4489. //---------------------------------------------------------------------------
  4490. void __fastcall TCustomScpExplorerForm::CalculateChecksum(const UnicodeString & Alg,
  4491. TStrings * FileList, TCalculatedChecksumEvent OnCalculatedChecksum,
  4492. bool & Close)
  4493. {
  4494. if ((Terminal != NULL) && // terminal can be already closed (e.g. dropped connection)
  4495. EnsureCommandSessionFallback(fcCalculatingChecksum))
  4496. {
  4497. Configuration->Usage->Inc(L"ChecksumCalculated");
  4498. try
  4499. {
  4500. Terminal->CalculateFilesChecksum(Alg, FileList, OnCalculatedChecksum);
  4501. }
  4502. catch(...)
  4503. {
  4504. if (!Terminal->Active)
  4505. {
  4506. Close = true;
  4507. }
  4508. throw;
  4509. }
  4510. }
  4511. }
  4512. //---------------------------------------------------------------------------
  4513. void TCustomScpExplorerForm::LoadFilesProperties(TStrings * FileList)
  4514. {
  4515. if (Terminal->LoadFilesProperties(FileList))
  4516. {
  4517. RemoteDirView->Invalidate();
  4518. }
  4519. }
  4520. //---------------------------------------------------------------------------
  4521. bool TCustomScpExplorerForm::CanCalculateChecksum()
  4522. {
  4523. return
  4524. Terminal->IsCapable[fcCalculatingChecksum] ||
  4525. (Terminal->IsCapable[fcSecondaryShell] && !Terminal->IsEncryptingFiles());
  4526. }
  4527. //---------------------------------------------------------------------------
  4528. bool __fastcall TCustomScpExplorerForm::SetProperties(TOperationSide Side, TStrings * FileList)
  4529. {
  4530. bool Result;
  4531. if (!IsSideLocalBrowser(Side))
  4532. {
  4533. TRemoteTokenList * GroupList = NULL;
  4534. TRemoteTokenList * UserList = NULL;
  4535. try
  4536. {
  4537. TRemoteProperties CurrentProperties;
  4538. LoadFilesProperties(FileList);
  4539. bool CapableGroupChanging = Terminal->IsCapable[fcGroupChanging];
  4540. bool CapableOwnerChanging = Terminal->IsCapable[fcOwnerChanging];
  4541. if (CapableGroupChanging || CapableOwnerChanging)
  4542. {
  4543. if (CapableGroupChanging)
  4544. {
  4545. GroupList = Terminal->Groups->Duplicate();
  4546. }
  4547. if (CapableOwnerChanging)
  4548. {
  4549. UserList = Terminal->Users->Duplicate();
  4550. }
  4551. TRemoteDirectory * Files = Terminal->Files;
  4552. int Count = Files->Count;
  4553. if (Count > 100)
  4554. {
  4555. Count = 100;
  4556. }
  4557. for (int Index = 0; Index < Count; Index++)
  4558. {
  4559. TRemoteFile * File = Files->Files[Index];
  4560. if (CapableGroupChanging)
  4561. {
  4562. GroupList->AddUnique(File->Group);
  4563. }
  4564. if (CapableOwnerChanging)
  4565. {
  4566. UserList->AddUnique(File->Owner);
  4567. }
  4568. }
  4569. // if we haven't collected tokens for all files in current directory,
  4570. // make sure we collect them at least for all selected files.
  4571. // (note that so far the files in FileList has to be from current direcotry)
  4572. if (Count < Files->Count)
  4573. {
  4574. for (int Index = 0; Index < FileList->Count; Index++)
  4575. {
  4576. TRemoteFile * File = (TRemoteFile *)(FileList->Objects[Index]);
  4577. if (CapableGroupChanging)
  4578. {
  4579. GroupList->AddUnique(File->Group);
  4580. }
  4581. if (CapableOwnerChanging)
  4582. {
  4583. UserList->AddUnique(File->Owner);
  4584. }
  4585. }
  4586. }
  4587. }
  4588. CurrentProperties = TRemoteProperties::CommonProperties(FileList);
  4589. bool CapableAclChanging = Terminal->IsCapable[fcAclChangingFiles];
  4590. if (CapableAclChanging && TRemoteFileList::AnyDirectory(FileList))
  4591. {
  4592. CapableAclChanging = false;
  4593. CurrentProperties.Valid = CurrentProperties.Valid >> vpRights;
  4594. }
  4595. int Flags = 0;
  4596. if (Terminal->IsCapable[fcModeChanging]) Flags |= cpMode;
  4597. if (CapableAclChanging) Flags |= cpAcl;
  4598. if (CapableOwnerChanging) Flags |= cpOwner;
  4599. if (CapableGroupChanging) Flags |= cpGroup;
  4600. TCalculateChecksumEvent CalculateChecksumEvent = NULL;
  4601. if (CanCalculateChecksum())
  4602. {
  4603. CalculateChecksumEvent = CalculateChecksum;
  4604. }
  4605. std::unique_ptr<TStrings> ChecksumAlgs(new TStringList());
  4606. Terminal->GetSupportedChecksumAlgs(ChecksumAlgs.get());
  4607. int Options =
  4608. FLAGMASK(Terminal->IsCapable[fcGroupOwnerChangingByID], poUserGroupByID) |
  4609. FLAGMASK(Terminal->IsCapable[fcTags], poTags);
  4610. TRemoteProperties NewProperties = CurrentProperties;
  4611. Result =
  4612. DoPropertiesDialog(FileList, RemoteDirView->PathName,
  4613. GroupList, UserList, ChecksumAlgs.get(), &NewProperties, Flags, Options,
  4614. CalculateSize, CalculateChecksumEvent);
  4615. if (Result)
  4616. {
  4617. NewProperties = TRemoteProperties::ChangedProperties(CurrentProperties, NewProperties);
  4618. Terminal->ChangeFilesProperties(FileList, &NewProperties);
  4619. }
  4620. }
  4621. __finally
  4622. {
  4623. delete GroupList;
  4624. delete UserList;
  4625. }
  4626. }
  4627. else
  4628. {
  4629. DirView(Side)->DisplayPropertiesMenu();
  4630. Result = true;
  4631. }
  4632. return Result;
  4633. }
  4634. //---------------------------------------------------------------------------
  4635. void __fastcall TCustomScpExplorerForm::KeyProcessed(Word & Key, TShiftState Shift)
  4636. {
  4637. if (Shift * AllKeyShiftStates() == (TShiftState() << ssAlt))
  4638. {
  4639. FIgnoreNextDialogChar = Key;
  4640. }
  4641. Key = 0;
  4642. }
  4643. //---------------------------------------------------------------------------
  4644. void __fastcall TCustomScpExplorerForm::CheckCustomCommandShortCut(
  4645. TCustomCommandList * List, Word & Key, Classes::TShiftState Shift, TShortCut KeyShortCut)
  4646. {
  4647. const TCustomCommandType * Command = List->Find(KeyShortCut);
  4648. if (Command != NULL)
  4649. {
  4650. KeyProcessed(Key, Shift);
  4651. if (CustomCommandState(*Command, false, ccltAll) > 0)
  4652. {
  4653. TAutoFlag DontCopyCommandToClipboardFlag(DontCopyCommandToClipboard);
  4654. ExecuteFileOperationCommand(foCustomCommand, osRemote,
  4655. false, false, const_cast<TCustomCommandType *>(Command));
  4656. }
  4657. }
  4658. }
  4659. //---------------------------------------------------------------------------
  4660. void __fastcall TCustomScpExplorerForm::KeyDown(Word & Key, Classes::TShiftState Shift)
  4661. {
  4662. if (!NonVisualDataModule->Busy)
  4663. {
  4664. if (QueueView3->Focused() && (QueueView3->OnKeyDown != NULL))
  4665. {
  4666. QueueView3->OnKeyDown(QueueView3, Key, Shift);
  4667. }
  4668. if ((Key != 0) && !DirView(osCurrent)->IsEditing())
  4669. {
  4670. TShortCut KeyShortCut = ShortCut(Key, Shift);
  4671. for (int Index = 0; Index < NonVisualDataModule->ExplorerActions->ActionCount; Index++)
  4672. {
  4673. // Note that handling shortcuts on panel (current control) popup menu has precendence over this.
  4674. // But so far it's not a problem, as it does what we want.
  4675. TAction * Action = (TAction *)NonVisualDataModule->ExplorerActions->Actions[Index];
  4676. if (((Action->ShortCut == KeyShortCut) ||
  4677. (Action->SecondaryShortCuts->IndexOfShortCut(KeyShortCut) >= 0)) &&
  4678. AllowedAction(Action, aaShortCut))
  4679. {
  4680. // Has to be called before the action as the dialog char is already in queue.
  4681. // So when the action consumes message queue, we already need to have the
  4682. // FIgnoreNextDialogChar set
  4683. KeyProcessed(Key, Shift);
  4684. // Reset reference to previous component (when menu/toolbar was clicked).
  4685. // Needed to detect that action was invoked by keyboard shortcut
  4686. // in TNonVisualDataModule::ExplorerActionsExecute
  4687. Action->ActionComponent = NULL;
  4688. Action->Execute();
  4689. return;
  4690. }
  4691. }
  4692. for (int i = 0; i < TTerminalManager::Instance()->Count; i++)
  4693. {
  4694. if (NonVisualDataModule->OpenSessionShortCut(i) == KeyShortCut)
  4695. {
  4696. KeyProcessed(Key, Shift);
  4697. TTerminalManager::Instance()->ActiveSessionIndex = i;
  4698. return;
  4699. }
  4700. }
  4701. if (Key == VK_TAB && Shift.Contains(ssCtrl))
  4702. {
  4703. KeyProcessed(Key, Shift);
  4704. TTerminalManager::Instance()->CycleTerminals(!Shift.Contains(ssShift));
  4705. }
  4706. TShortCut CustomShortCut = NormalizeCustomShortCut(KeyShortCut);
  4707. if (IsCustomShortCut(CustomShortCut))
  4708. {
  4709. CheckCustomCommandShortCut(WinConfiguration->CustomCommandList, Key, Shift, CustomShortCut);
  4710. CheckCustomCommandShortCut(WinConfiguration->ExtensionList, Key, Shift, CustomShortCut);
  4711. if (WinConfiguration->SharedBookmarks != NULL)
  4712. {
  4713. TBookmark * Bookmark = WinConfiguration->SharedBookmarks->FindByShortCut(CustomShortCut);
  4714. if ((Bookmark != NULL) &&
  4715. OpenBookmark(FCurrentSide, Bookmark))
  4716. {
  4717. KeyProcessed(Key, Shift);
  4718. }
  4719. }
  4720. }
  4721. }
  4722. }
  4723. TForm::KeyDown(Key, Shift);
  4724. }
  4725. //---------------------------------------------------------------------------
  4726. void __fastcall TCustomScpExplorerForm::InitStatusBar()
  4727. {
  4728. bool OpenedTerminal = false;
  4729. UnicodeString ProtocolName;
  4730. UnicodeString ProtocolNameHint;
  4731. UnicodeString SecurityProtocolName;
  4732. UnicodeString DurationHint;
  4733. // See the comment in TScpCommanderForm::UpdateControls()
  4734. TTerminal * ActiveTerminal = TTerminalManager::Instance()->ActiveTerminal;
  4735. if (ActiveTerminal != NULL)
  4736. {
  4737. // Active is not enough here
  4738. if (ActiveTerminal->Status == ssOpened)
  4739. {
  4740. const TSessionInfo & SessionInfo = ActiveTerminal->GetSessionInfo();
  4741. const TFileSystemInfo & FileSystemInfo = ActiveTerminal->GetFileSystemInfo();
  4742. if (FileSystemInfo.ProtocolName.IsEmpty())
  4743. {
  4744. ProtocolName = SessionInfo.ProtocolName;
  4745. }
  4746. else
  4747. {
  4748. ProtocolName = FileSystemInfo.ProtocolName;
  4749. }
  4750. ProtocolNameHint = LoadStr(STATUS_PROTOCOL_HINT);
  4751. SecurityProtocolName = SessionInfo.SecurityProtocolName;
  4752. DurationHint = LoadStr(STATUS_DURATION_HINT);
  4753. OpenedTerminal = true;
  4754. }
  4755. else if (ActiveTerminal->Status > ssClosed)
  4756. {
  4757. ProtocolName = LoadStr(STATUS_CONNECTING);
  4758. }
  4759. else
  4760. {
  4761. ProtocolName = LoadStr(STATUS_NOT_CONNECTED2);
  4762. }
  4763. }
  4764. TTBXStatusBar * SessionStatusBar = (TTBXStatusBar *)GetComponent(fcStatusBar);
  4765. int Offset = SessionStatusBar->Panels->Count - SessionPanelCount;
  4766. TTBXStatusPanel * ProtocolStatusPanel = SessionStatusBar->Panels->Items[Offset + 0];
  4767. ProtocolStatusPanel->ViewPriority = (ActiveTerminal != NULL) ? 100 : 0;
  4768. if (ProtocolStatusPanel->Caption != ProtocolName)
  4769. {
  4770. ProtocolStatusPanel->Caption = ProtocolName;
  4771. std::unique_ptr<TCanvas> Canvas(CreateControlCanvas(SessionStatusBar));
  4772. ProtocolStatusPanel->Size = Canvas->TextWidth(ProtocolName) + ScaleByTextHeight(SessionStatusBar, 32);
  4773. }
  4774. ProtocolStatusPanel->Hint = ProtocolNameHint;
  4775. TTBXStatusPanel * SecurityStatusPanel = SessionStatusBar->Panels->Items[Offset + 1];
  4776. SecurityStatusPanel->ViewPriority = OpenedTerminal ? 98 : 0;
  4777. bool SecurityEnabled = !SecurityProtocolName.IsEmpty();
  4778. SecurityStatusPanel->Enabled = SecurityEnabled;
  4779. UnicodeString SecurityProtocolHint;
  4780. if (SecurityEnabled)
  4781. {
  4782. SecurityProtocolHint = FMTLOAD(STATUS_SECURE, (SecurityProtocolName));
  4783. }
  4784. else
  4785. {
  4786. SecurityProtocolHint = LoadStr(STATUS_INSECURE);
  4787. }
  4788. SecurityStatusPanel->Hint = SecurityProtocolHint;
  4789. TTBXStatusPanel * DurationStatusPanel = SessionStatusBar->Panels->Items[Offset + 2];
  4790. DurationStatusPanel->ViewPriority = OpenedTerminal ? 99 : 0;
  4791. DurationStatusPanel->Hint = DurationHint;
  4792. UpdateStatusBar();
  4793. }
  4794. //---------------------------------------------------------------------------
  4795. void __fastcall TCustomScpExplorerForm::UpdateStatusBar()
  4796. {
  4797. TTBXStatusBar * SessionStatusBar = (TTBXStatusBar *)GetComponent(fcStatusBar);
  4798. DebugAssert(SessionStatusBar != NULL);
  4799. TTBXStatusPanel * NoteStatusPanel = SessionStatusBar->Panels->Items[0];
  4800. if (!FNote.IsEmpty())
  4801. {
  4802. NoteStatusPanel->Caption = FNote;
  4803. }
  4804. else
  4805. {
  4806. UpdateStatusPanelText(NoteStatusPanel);
  4807. }
  4808. NoteStatusPanel->Hint = FNoteHints;
  4809. UnicodeString LoginTime;
  4810. // See the comment in TScpCommanderForm::UpdateControls()
  4811. TTerminal * ActiveTerminal = TTerminalManager::Instance()->ActiveTerminal;
  4812. // Active is not enough here
  4813. if ((ActiveTerminal != NULL) && (ActiveTerminal->Status == ssOpened))
  4814. {
  4815. const TSessionInfo & SessionInfo = ActiveTerminal->GetSessionInfo();
  4816. LoginTime = FormatDateTimeSpan(Now() - SessionInfo.LoginTime);
  4817. }
  4818. TTBXStatusPanel * DurationStatusPanel = SessionStatusBar->Panels->Items[SessionStatusBar->Panels->Count - 1];
  4819. DurationStatusPanel->Caption = LoginTime;
  4820. }
  4821. //---------------------------------------------------------------------------
  4822. void __fastcall TCustomScpExplorerForm::UpdateStatusPanelText(TTBXStatusPanel * Panel)
  4823. {
  4824. Panel->Caption = L"";
  4825. }
  4826. //---------------------------------------------------------------------------
  4827. void __fastcall TCustomScpExplorerForm::Idle()
  4828. {
  4829. if (FShowing ||
  4830. // Particularly for
  4831. // - Standalone editing
  4832. // - Detecting closed connection and automatically reconnect it while waiting for changes
  4833. // while "keeping remote directory up to date".
  4834. StandaloneOperation)
  4835. {
  4836. FEditorManager->Check();
  4837. // make sure that Idle is called before update queue, as it may invoke QueueEvent
  4838. // that needs to know if queue view is visible (and it may be closed after queue update)
  4839. TTerminalManager::Instance()->Idle(FDoNotIdleCurrentTerminal > 0);
  4840. }
  4841. if (FShowing)
  4842. {
  4843. if (!NonVisualDataModule->Busy &&
  4844. // Menu is opened or drag&drop is going on
  4845. (Mouse->Capture == NULL))
  4846. {
  4847. if (FRefreshRemoteDirectory)
  4848. {
  4849. if ((Terminal != NULL) && (Terminal->Status == ssOpened))
  4850. {
  4851. Terminal->RefreshDirectory();
  4852. }
  4853. FRefreshRemoteDirectory = false;
  4854. }
  4855. if (FRefreshLocalDirectory)
  4856. {
  4857. ReloadLocalDirectory();
  4858. FRefreshLocalDirectory = false;
  4859. }
  4860. if (WinConfiguration->RefreshRemotePanel)
  4861. {
  4862. if ((Terminal != NULL) && (Terminal->Status == ssOpened) &&
  4863. (Now() - Terminal->DirectoryLoaded > WinConfiguration->RefreshRemotePanelInterval))
  4864. {
  4865. RemoteDirView->ReloadDirectory();
  4866. }
  4867. }
  4868. }
  4869. // TB2 shows accels on Alt key down, but hides it only after menu modal loop finishes.
  4870. // But if Alt is used to switch apps or for keyboard shortcut, the menu modal loop does not happen and accelerators stay displayed.
  4871. // It's difficult to fix this in TB2, as there's no "up" equivalent to CM_DIALOGKEY, which shows the accels.
  4872. // 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.
  4873. TTBCustomToolbar * MenuToolbar = DebugNotNull(dynamic_cast<TTBCustomToolbar *>(GetComponent(fcMenu)));
  4874. MenuToolbar->Idle();
  4875. }
  4876. if (FShowing || StandaloneOperation)
  4877. {
  4878. if (FQueueStatusInvalidated)
  4879. {
  4880. UpdateQueueStatus(false);
  4881. }
  4882. RefreshQueueItems();
  4883. }
  4884. if (FShowing)
  4885. {
  4886. UpdateStatusBar();
  4887. }
  4888. if (FClipboardFakeMonitorsPendingReset && !FDownloadingFromClipboard)
  4889. {
  4890. FClipboardFakeMonitorsPendingReset = false;
  4891. FClipboardFakeMonitors.reset(NULL);
  4892. }
  4893. FIgnoreNextDialogChar = 0;
  4894. }
  4895. //---------------------------------------------------------------------------
  4896. void __fastcall TCustomScpExplorerForm::UserActionTimer(TObject * /*Sender*/)
  4897. {
  4898. try
  4899. {
  4900. FUserActionTimer->Enabled = false;
  4901. if (IsQueueAutoPopup() && (FPendingQueueActionItem != NULL))
  4902. {
  4903. if (TQueueItem::IsUserActionStatus(FPendingQueueActionItem->Status))
  4904. {
  4905. FPendingQueueActionItem->ProcessUserAction();
  4906. }
  4907. }
  4908. }
  4909. __finally
  4910. {
  4911. FPendingQueueActionItem = NULL;
  4912. }
  4913. }
  4914. //---------------------------------------------------------------------------
  4915. void __fastcall TCustomScpExplorerForm::ApplicationMinimize(TObject * /*Sender*/)
  4916. {
  4917. if (WinConfiguration->MinimizeToTray)
  4918. {
  4919. UpdateTrayIcon();
  4920. FTrayIcon->Visible = true;
  4921. if (Visible)
  4922. {
  4923. ShowWindow(Handle, SW_HIDE);
  4924. }
  4925. }
  4926. }
  4927. //---------------------------------------------------------------------------
  4928. void __fastcall TCustomScpExplorerForm::ApplicationRestore(TObject * /*Sender*/)
  4929. {
  4930. // WORKAROUND
  4931. // When restoring maximized window from minimization,
  4932. // rarely some controls do not align properly.
  4933. // Two instances seen (both for Commander):
  4934. // - When restoring, window is temporarily narrower (not maximized),
  4935. // causing toolbars on TopDock to wrap and dock to expand horizontally.
  4936. // Once maximized already, top dock shinks back, but the session PageControl,
  4937. // do not align up, leaving space between TopDock and PageControl.
  4938. // - Similar issue seen with LocalDirView not aligning down to status bar.
  4939. for (int Index = 0; Index < ControlCount; Index++)
  4940. {
  4941. RealignControl(Controls[Index]);
  4942. }
  4943. if (FTrayIcon->Visible)
  4944. {
  4945. FTrayIcon->Visible = false;
  4946. if (Visible)
  4947. {
  4948. ShowWindow(Handle, SW_SHOW);
  4949. }
  4950. }
  4951. if (FNeedSession && DebugAlwaysTrue(Terminal == NULL))
  4952. {
  4953. FNeedSession = false;
  4954. NonVisualDataModule->StartBusy();
  4955. try
  4956. {
  4957. NeedSession(true);
  4958. }
  4959. __finally
  4960. {
  4961. NonVisualDataModule->EndBusy();
  4962. }
  4963. }
  4964. }
  4965. //---------------------------------------------------------------------------
  4966. void __fastcall TCustomScpExplorerForm::UpdateTrayIcon()
  4967. {
  4968. FTrayIcon->Hint = GetMainForm()->Caption;
  4969. }
  4970. //---------------------------------------------------------------------------
  4971. void __fastcall TCustomScpExplorerForm::ApplicationTitleChanged()
  4972. {
  4973. UpdateTrayIcon();
  4974. }
  4975. //---------------------------------------------------------------------------
  4976. void __fastcall TCustomScpExplorerForm::RestoreApp()
  4977. {
  4978. // workaround
  4979. // TApplication.WndProc sets TApplication.FAppIconic to false,
  4980. // on WM_ACTIVATEAPP, what renders TApplication.Restore no-op function.
  4981. // But WM_ACTIVATEAPP message can be received even when
  4982. // the main window is minimized to the tray and internal editor window is focused
  4983. // (after another application was previously active)
  4984. if (::IsIconic(Handle))
  4985. {
  4986. if (!IsAppIconic())
  4987. {
  4988. SetAppIconic(true);
  4989. }
  4990. }
  4991. ::ApplicationRestore();
  4992. Application->BringToFront();
  4993. }
  4994. //---------------------------------------------------------------------------
  4995. void __fastcall TCustomScpExplorerForm::TrayIconClick(TObject * /*Sender*/)
  4996. {
  4997. RestoreApp();
  4998. }
  4999. //---------------------------------------------------------------------------
  5000. void __fastcall TCustomScpExplorerForm::NewSession(const UnicodeString & SessionUrl)
  5001. {
  5002. if (OpenInNewWindow())
  5003. {
  5004. ExecuteNewInstance(SessionUrl);
  5005. }
  5006. else
  5007. {
  5008. TTerminalManager::Instance()->NewSession(SessionUrl);
  5009. }
  5010. }
  5011. //---------------------------------------------------------------------------
  5012. void TCustomScpExplorerForm::NewTab(TOperationSide, bool)
  5013. {
  5014. NewSession();
  5015. }
  5016. //---------------------------------------------------------------------------
  5017. UnicodeString __fastcall TCustomScpExplorerForm::SaveHiddenDuplicateSession(TSessionData * SessionData)
  5018. {
  5019. UnicodeString SessionName = StoredSessions->HiddenPrefix + ManagedSession->SessionData->SessionName;
  5020. StoredSessions->NewSession(SessionName, SessionData);
  5021. // modified only, explicit
  5022. StoredSessions->Save(false, true);
  5023. // encode session name because of slashes in hierarchical sessions
  5024. return EncodeUrlString(SessionName);
  5025. }
  5026. //---------------------------------------------------------------------------
  5027. UnicodeString __fastcall TCustomScpExplorerForm::CreateHiddenDuplicateSession()
  5028. {
  5029. // current working directories become defaults here, what is not right
  5030. std::unique_ptr<TSessionData> SessionData(CloneCurrentSessionData());
  5031. return SaveHiddenDuplicateSession(SessionData.get());
  5032. }
  5033. //---------------------------------------------------------------------------
  5034. void __fastcall TCustomScpExplorerForm::DuplicateTab()
  5035. {
  5036. if (OpenInNewWindow())
  5037. {
  5038. ExecuteNewInstance(CreateHiddenDuplicateSession());
  5039. }
  5040. else
  5041. {
  5042. // current working directories become defaults here, what is not right
  5043. std::unique_ptr<TSessionData> SessionData(CloneCurrentSessionData());
  5044. TTerminalManager * Manager = TTerminalManager::Instance();
  5045. TManagedTerminal * ASession = Manager->NewManagedTerminal(SessionData.get());
  5046. DebugAssert(ASession->LocalBrowser == ManagedSession->LocalBrowser);
  5047. // We definitely want these
  5048. ASession->Disconnected = ManagedSession->Disconnected;
  5049. // Not sure about these two
  5050. ASession->Permanent = ManagedSession->Permanent;
  5051. ASession->DisconnectedTemporarily = ManagedSession->DisconnectedTemporarily;
  5052. Manager->ActiveSession = ASession;
  5053. }
  5054. }
  5055. //---------------------------------------------------------------------------
  5056. void __fastcall TCustomScpExplorerForm::RenameTab()
  5057. {
  5058. UnicodeString Name = ManagedSession->SessionData->SessionName;
  5059. if (InputDialog(LoadStr(RENAME_TAB_TITLE), LoadStr(RENAME_TAB_PROMPT), Name, HELP_TAB_RENAME) &&
  5060. // When submitting the default name, do not fix it
  5061. (Name != ManagedSession->SessionData->SessionName))
  5062. {
  5063. // Checks for a slash only, so it's not that big deal that we do the check after submitting the dialog only.
  5064. TSessionData::ValidateName(Name);
  5065. // This is inconsistent with how color (for example) is handled.
  5066. ManagedSession->SessionData->Name = Name;
  5067. UpdateControls();
  5068. // Add/Remove distinguishing paths from sessions of the same name.
  5069. SessionListChanged();
  5070. }
  5071. }
  5072. //---------------------------------------------------------------------------
  5073. bool __fastcall TCustomScpExplorerForm::CanCloseQueue(TTerminalQueue * Queue)
  5074. {
  5075. DebugAssert(Queue != NULL);
  5076. bool Result = Queue->IsEmpty;
  5077. if (!Result)
  5078. {
  5079. SetFocus();
  5080. Result = (MessageDialog(LoadStr(PENDING_QUEUE_ITEMS2), qtWarning, qaOK | qaCancel, HELP_NONE) == qaOK);
  5081. }
  5082. return Result;
  5083. }
  5084. //---------------------------------------------------------------------------
  5085. bool __fastcall TCustomScpExplorerForm::CanCloseQueue()
  5086. {
  5087. return CanCloseQueue(FQueue);
  5088. }
  5089. //---------------------------------------------------------------------------
  5090. void __fastcall TCustomScpExplorerForm::CloseTab()
  5091. {
  5092. if (CanCloseQueue())
  5093. {
  5094. TTerminalManager::Instance()->FreeActiveTerminal();
  5095. }
  5096. }
  5097. //---------------------------------------------------------------------------
  5098. void __fastcall TCustomScpExplorerForm::DisconnectSession()
  5099. {
  5100. if (CanCloseQueue())
  5101. {
  5102. TTerminalManager::Instance()->DisconnectActiveTerminal();
  5103. }
  5104. }
  5105. //---------------------------------------------------------------------------
  5106. void __fastcall TCustomScpExplorerForm::TerminalDisconnected()
  5107. {
  5108. DetachTerminal(Terminal);
  5109. RemoteDirView->Terminal = Terminal;
  5110. UpdateRemotePathComboBox(false);
  5111. InitStatusBar();
  5112. UpdateControls();
  5113. }
  5114. //---------------------------------------------------------------------------
  5115. void __fastcall TCustomScpExplorerForm::TerminalConnecting()
  5116. {
  5117. InitStatusBar();
  5118. }
  5119. //---------------------------------------------------------------------------
  5120. void __fastcall TCustomScpExplorerForm::ReconnectSession()
  5121. {
  5122. if (DebugAlwaysTrue(!Terminal->Active))
  5123. {
  5124. TTerminalManager::Instance()->ReconnectActiveTerminal();
  5125. }
  5126. }
  5127. //---------------------------------------------------------------------------
  5128. void __fastcall TCustomScpExplorerForm::OpenStoredSession(TSessionData * Data)
  5129. {
  5130. if (OpenInNewWindow())
  5131. {
  5132. // encode session name because of slashes in hierarchical sessions
  5133. ExecuteNewInstance(EncodeUrlString(Data->Name));
  5134. }
  5135. else
  5136. {
  5137. TTerminalManager * Manager = TTerminalManager::Instance();
  5138. TManagedTerminal * Terminal = Manager->NewManagedTerminal(Data);
  5139. Manager->ActiveSession = Terminal;
  5140. }
  5141. }
  5142. //---------------------------------------------------------------------------
  5143. void TCustomScpExplorerForm::DoOpenFolderOrWorkspace(
  5144. const UnicodeString & Name, bool ConnectFirstTerminal, bool CheckMaxSessions)
  5145. {
  5146. TTerminalManager * Manager = TTerminalManager::Instance();
  5147. std::unique_ptr<TObjectList> DataList(new TObjectList());
  5148. // This asks for a master password. Maybe we should postpone asking until a connection is actually opened.
  5149. StoredSessions->GetFolderOrWorkspace(Name, DataList.get());
  5150. if (!CheckMaxSessions || (DataList->Count <= WinConfiguration->MaxSessions))
  5151. {
  5152. TManagedTerminal * FirstSession = Manager->NewSessions(DataList.get());
  5153. // FirstSession can be null, if none of the workspace sites exist anymore
  5154. // or if all workspace sessions are not suported by the interface
  5155. // (when trying to open all local-local workspace in "explorer" interface)
  5156. if (FirstSession != NULL)
  5157. {
  5158. if (!ConnectFirstTerminal)
  5159. {
  5160. FirstSession->Disconnected = true;
  5161. FirstSession->DisconnectedTemporarily = true;
  5162. }
  5163. Manager->ActiveSession = FirstSession;
  5164. }
  5165. }
  5166. }
  5167. //---------------------------------------------------------------------------
  5168. void __fastcall TCustomScpExplorerForm::OpenFolderOrWorkspace(const UnicodeString & Name)
  5169. {
  5170. if (OpenInNewWindow())
  5171. {
  5172. ExecuteNewInstance(Name);
  5173. }
  5174. else
  5175. {
  5176. DoOpenFolderOrWorkspace(Name, true, false);
  5177. }
  5178. }
  5179. //---------------------------------------------------------------------------
  5180. void __fastcall TCustomScpExplorerForm::FormCloseQuery(TObject * /*Sender*/,
  5181. bool &CanClose)
  5182. {
  5183. AppLog(L"Windows close query");
  5184. // See the comment in CloseApp()
  5185. if (NonVisualDataModule->Busy)
  5186. {
  5187. CanClose = false;
  5188. }
  5189. else
  5190. {
  5191. TTerminalManager * Manager = TTerminalManager::Instance();
  5192. int ActiveSessions = 0;
  5193. for (int Index = 0; Index < Manager->Count; Index++)
  5194. {
  5195. if (Manager->Sessions[Index]->Active)
  5196. {
  5197. ActiveSessions++;
  5198. }
  5199. }
  5200. // Confirm, when there's at least one active remote session or
  5201. // when there are multiple (inactive or local) sessions and auto workspace saving is not enabled.
  5202. if (((ActiveSessions > 0) || ((Manager->Count > 1) && !WinConfiguration->AutoSaveWorkspace)) &&
  5203. WinConfiguration->ConfirmClosingSession)
  5204. {
  5205. unsigned int Result;
  5206. TMessageParams Params(mpNeverAskAgainCheck);
  5207. UnicodeString Message;
  5208. int Answers = qaOK | qaCancel;
  5209. // The current session is the only active session
  5210. if ((ActiveSessions == 1) && (Terminal != NULL) && Terminal->Active)
  5211. {
  5212. if (!WinConfiguration->AutoSaveWorkspace)
  5213. {
  5214. Message = LoadStr(CLOSE_SESSION_WORKSPACE);
  5215. }
  5216. else
  5217. {
  5218. Message = LoadStr(CLOSE_SESSION);
  5219. }
  5220. Message = FORMAT(Message, (Terminal->SessionData->SessionName));
  5221. }
  5222. // Multiple active sessions or one active session, but it's not the current one
  5223. else if (ActiveSessions > 0)
  5224. {
  5225. if (!WinConfiguration->AutoSaveWorkspace)
  5226. {
  5227. Message = LoadStr(CLOSE_SESSIONS_WORKSPACE3);
  5228. }
  5229. else
  5230. {
  5231. Message = LoadStr(CLOSE_SESSIONS);
  5232. }
  5233. }
  5234. // Multiple tabs, but all are inactive or local, and saving of worspace is not enabled
  5235. else
  5236. {
  5237. DebugAlwaysTrue(Manager->Count > 1);
  5238. DebugAlwaysTrue(!WinConfiguration->AutoSaveWorkspace);
  5239. Message = LoadStr(CLOSE_WORKSPACE);
  5240. }
  5241. UnicodeString Note;
  5242. if (WinConfiguration->AutoSaveWorkspace)
  5243. {
  5244. Note = FMTLOAD(AUTO_WORKSPACE, (WorkspaceName()));
  5245. }
  5246. else
  5247. {
  5248. Note = LoadStr(AUTO_WORKSPACE_ENABLE);
  5249. Answers = qaYes | qaNo | qaCancel;
  5250. }
  5251. Message = FORMAT("%s\n\n%s", (MainInstructions(Message), Note));
  5252. SetFocus();
  5253. Result = MessageDialog(Message, qtConfirmation,
  5254. Answers, HELP_CLOSE_SESSION_WORKSPACE, &Params);
  5255. if (Result == qaNeverAskAgain)
  5256. {
  5257. WinConfiguration->ConfirmClosingSession = false;
  5258. }
  5259. if (Result == qaNo)
  5260. {
  5261. CanClose = SaveWorkspace(true);
  5262. // note that the workspace will be saved redundatly again from FormClose
  5263. }
  5264. else
  5265. {
  5266. CanClose =
  5267. (Result == qaOK) ||
  5268. (Result == qaYes) || // CLOSE_SESSIONS_WORKSPACE variant
  5269. (Result == qaNeverAskAgain);
  5270. }
  5271. }
  5272. if (CanClose && HasActiveTerminal())
  5273. {
  5274. CanClose = CanCloseQueue();
  5275. }
  5276. }
  5277. if (CanClose)
  5278. {
  5279. CanClose =
  5280. FEditorManager->CloseInternalEditors(CloseInternalEditor) &&
  5281. FEditorManager->CloseExternalFilesWithoutProcess();
  5282. if (CanClose)
  5283. {
  5284. while (CanClose && (FLocalEditors->Count > 0))
  5285. {
  5286. int PrevCount = FLocalEditors->Count;
  5287. static_cast<TForm *>(FLocalEditors->Items[0])->Close();
  5288. CanClose = (FLocalEditors->Count < PrevCount);
  5289. }
  5290. if (CanClose)
  5291. {
  5292. CanClose = FEditorManager->Empty(true);
  5293. if (!CanClose)
  5294. {
  5295. SetFocus();
  5296. CanClose =
  5297. (MessageDialog(
  5298. LoadStr(PENDING_EDITORS), qtWarning, qaIgnore | qaCancel, HELP_NONE) == qaIgnore);
  5299. }
  5300. }
  5301. }
  5302. }
  5303. }
  5304. //---------------------------------------------------------------------------
  5305. void __fastcall TCustomScpExplorerForm::CloseInternalEditor(TObject * Sender)
  5306. {
  5307. TForm * Form = dynamic_cast<TForm *>(Sender);
  5308. DebugAssert(Form != NULL);
  5309. Form->Close();
  5310. }
  5311. //---------------------------------------------------------------------------
  5312. void __fastcall TCustomScpExplorerForm::ForceCloseInternalEditor(TObject * Sender)
  5313. {
  5314. TForm * Form = dynamic_cast<TForm *>(Sender);
  5315. delete Form;
  5316. }
  5317. //---------------------------------------------------------------------------
  5318. void __fastcall TCustomScpExplorerForm::ForceCloseLocalEditors()
  5319. {
  5320. while (FLocalEditors->Count > 0)
  5321. {
  5322. delete static_cast<TForm *>(FLocalEditors->Items[0]);
  5323. }
  5324. }
  5325. //---------------------------------------------------------------------------
  5326. void __fastcall TCustomScpExplorerForm::RemoteDirViewDisplayProperties(
  5327. TObject *Sender)
  5328. {
  5329. TStrings *FileList = ((TUnixDirView*)Sender)->CreateFileList(True, False, NULL);
  5330. try
  5331. {
  5332. SetProperties(osRemote, FileList);
  5333. }
  5334. __finally
  5335. {
  5336. delete FileList;
  5337. }
  5338. }
  5339. //---------------------------------------------------------------------------
  5340. void __fastcall TCustomScpExplorerForm::ComponentShowing(Byte Component, bool value)
  5341. {
  5342. if (value)
  5343. {
  5344. if (Component == fcCustomCommandsBand)
  5345. {
  5346. UpdateCustomCommandsToolbar();
  5347. }
  5348. if (Component == fcQueueFileList)
  5349. {
  5350. DebugAssert(!QueueFileListSplitter->Visible);
  5351. AdjustQueueLayout();
  5352. UpdateQueueFileList();
  5353. }
  5354. }
  5355. }
  5356. //---------------------------------------------------------------------------
  5357. void __fastcall TCustomScpExplorerForm::AdjustQueueLayout()
  5358. {
  5359. int MinHeightLimit =
  5360. GetStaticQueuePanelComponentsHeight() +
  5361. QueueFileListSplitter->Height +
  5362. QueueFileList->Height +
  5363. GetMinQueueViewHeight();
  5364. if (QueuePanel->ClientHeight < MinHeightLimit)
  5365. {
  5366. QueuePanel->ClientHeight = MinHeightLimit;
  5367. }
  5368. }
  5369. //---------------------------------------------------------------------------
  5370. void __fastcall TCustomScpExplorerForm::SetComponentVisible(Byte Component, Boolean value)
  5371. {
  5372. TControl * Control = GetComponent(Component);
  5373. DebugAssert(Control);
  5374. bool Changed = (Control->Visible != value);
  5375. if (Changed)
  5376. {
  5377. ComponentShowing(Component, value);
  5378. TWinControl * WinControl = dynamic_cast<TWinControl*>(Control);
  5379. bool WasFocused = (WinControl != NULL) && (ActiveControl != NULL) &&
  5380. ((ActiveControl == WinControl) || (ActiveControl->Parent == WinControl));
  5381. if (value)
  5382. {
  5383. int RemainingHeight = Control->Parent->ClientHeight;
  5384. int RemainingWidth = Control->Parent->ClientWidth;
  5385. for (int i = 0; i < Control->Parent->ControlCount; i++)
  5386. {
  5387. TControl * ChildControl = Control->Parent->Controls[i];
  5388. if (ChildControl->Visible)
  5389. {
  5390. switch (ChildControl->Align)
  5391. {
  5392. case alTop:
  5393. case alBottom:
  5394. RemainingHeight -= ChildControl->Height;
  5395. break;
  5396. case alLeft:
  5397. case alRight:
  5398. RemainingWidth -= ChildControl->Width;
  5399. break;
  5400. }
  5401. }
  5402. }
  5403. int Reserve = ScaleByTextHeight(this, 32);
  5404. // queue in explorer, trees in commander
  5405. if (Control->Height > RemainingHeight - Reserve)
  5406. {
  5407. Control->Height = RemainingHeight / 2;
  5408. }
  5409. if (Control->Width > RemainingWidth - Reserve)
  5410. {
  5411. Control->Width = RemainingWidth / 2;
  5412. }
  5413. }
  5414. Control->Visible = value;
  5415. if (WasFocused && Visible)
  5416. {
  5417. DirView(osCurrent)->SetFocus();
  5418. }
  5419. FixControlsPlacement();
  5420. }
  5421. }
  5422. //---------------------------------------------------------------------------
  5423. bool __fastcall TCustomScpExplorerForm::GetComponentVisible(Byte Component)
  5424. {
  5425. TControl * Control = GetComponent(Component);
  5426. if (Control == NULL)
  5427. {
  5428. return false;
  5429. }
  5430. else
  5431. {
  5432. return Control->Visible;
  5433. }
  5434. }
  5435. //---------------------------------------------------------------------------
  5436. bool __fastcall TCustomScpExplorerForm::IsComponentPossible(Byte Component)
  5437. {
  5438. bool Result;
  5439. if ((Component == fcExplorerUpdatesBand) || (Component == fcCommanderUpdatesBand))
  5440. {
  5441. Result = !IsUWP();
  5442. }
  5443. else
  5444. {
  5445. Result = true;
  5446. }
  5447. return Result;
  5448. }
  5449. //---------------------------------------------------------------------------
  5450. void __fastcall TCustomScpExplorerForm::MakeFocusedItemVisible(TCustomDirView * DirView)
  5451. {
  5452. if (DirView->ItemFocused != NULL)
  5453. {
  5454. DirView->ItemFocused->MakeVisible(false);
  5455. }
  5456. }
  5457. //---------------------------------------------------------------------------
  5458. void __fastcall TCustomScpExplorerForm::FixControlsPlacement()
  5459. {
  5460. MakeFocusedItemVisible(RemoteDirView);
  5461. QueueSplitter->Visible = QueuePanel->Visible;
  5462. QueueFileListSplitter->Visible = QueueFileList->Visible;
  5463. RemotePanelSplitter->Visible = RemoteDrivePanel->Visible;
  5464. TControl * TopControlsOrder[] =
  5465. { TopDock, MessageDock, SessionsPageControl };
  5466. SetVerticalControlsOrder(TopControlsOrder, LENOF(TopControlsOrder));
  5467. TControl * QueueControlsOrder[] =
  5468. { QueueDock, QueueView3, QueueFileListSplitter, QueueFileList };
  5469. SetVerticalControlsOrder(QueueControlsOrder, LENOF(QueueControlsOrder));
  5470. }
  5471. //---------------------------------------------------------------------------
  5472. TControl * __fastcall TCustomScpExplorerForm::GetComponent(Byte Component)
  5473. {
  5474. switch (Component) {
  5475. case fcStatusBar: return RemoteStatusBar;
  5476. case fcRemotePopup: return reinterpret_cast<TControl *>(NonVisualDataModule->RemoteFilePopup);
  5477. case fcQueueView: return QueuePanel;
  5478. case fcQueueToolbar: return QueueDock;
  5479. case fcRemoteTree: return RemoteDrivePanel;
  5480. case fcSessionsTabs: return SessionsPageControl;
  5481. case fcQueueFileList: return QueueFileList;
  5482. default: return NULL;
  5483. }
  5484. }
  5485. //---------------------------------------------------------------------------
  5486. void __fastcall TCustomScpExplorerForm::DirViewColumnRightClick(
  5487. TObject *Sender, TListColumn *Column, TPoint &Point)
  5488. {
  5489. DebugAssert(NonVisualDataModule && Column && Sender);
  5490. NonVisualDataModule->ListColumn = Column;
  5491. TPopupMenu * DirViewColumnMenu;
  5492. if (Sender == DirView(osOther))
  5493. {
  5494. DirViewColumnMenu = NonVisualDataModule->RemoteDirViewColumnPopup;
  5495. NonVisualDataModule->RemoteSortByExtColumnPopupItem->Visible = (Column->Index == uvName);
  5496. bool SizeColumn = (Column->Index == uvSize);
  5497. NonVisualDataModule->RemoteFormatSizeBytesPopupItem->Visible = SizeColumn;
  5498. NonVisualDataModule->RemoteCalculateDirectorySizesPopupItem->Visible = SizeColumn;
  5499. }
  5500. else
  5501. {
  5502. DirViewColumnMenu = NonVisualDataModule->LocalDirViewColumnPopup;
  5503. NonVisualDataModule->LocalSortByExtColumnPopupItem->Visible = (Column->Index == dvName);
  5504. bool SizeColumn = (Column->Index == dvSize);
  5505. NonVisualDataModule->LocalFormatSizeBytesPopupItem->Visible = SizeColumn;
  5506. NonVisualDataModule->LocalCalculateDirectorySizesPopupItem->Visible = SizeColumn;
  5507. }
  5508. TCustomListView * ListView = DebugNotNull(dynamic_cast<TCustomListView *>(Sender));
  5509. TPoint ScreenPoint = ListView->ClientToScreen(Point);
  5510. ScreenPoint.x -= GetScrollPos(ListView->Handle, SB_HORZ);
  5511. DirViewColumnMenu->Popup(ScreenPoint.x, ScreenPoint.y);
  5512. }
  5513. //---------------------------------------------------------------------------
  5514. void __fastcall TCustomScpExplorerForm::DirViewExecFile(
  5515. TObject *Sender, TListItem *Item, bool &AllowExec)
  5516. {
  5517. DoDirViewExecFile(Sender, Item, AllowExec);
  5518. }
  5519. //---------------------------------------------------------------------------
  5520. void __fastcall TCustomScpExplorerForm::DoDirViewExecFile(TObject * Sender,
  5521. TListItem * Item, bool & AllowExec)
  5522. {
  5523. DebugAssert(Sender && Item && Configuration);
  5524. DebugAssert(AllowExec);
  5525. TCustomDirView * ADirView = (TCustomDirView *)Sender;
  5526. TOperationSide Side = ((ADirView == DirView(osRemote)) ? osRemote : osLocal);
  5527. bool Remote = !IsSideLocalBrowser(Side);
  5528. DebugAssert(Item == ADirView->ItemFocused);
  5529. bool IsDirectory = ADirView->ItemIsDirectory(Item);
  5530. TResolvedDoubleClickAction Action;
  5531. if (FForceExecution)
  5532. {
  5533. Action = IsDirectory ? rdcaChangeDir : rdcaOpen;
  5534. }
  5535. else
  5536. {
  5537. TTerminal * ATerminal = !IsSideLocalBrowser(Side) ? Terminal : NULL;
  5538. Action = WinConfiguration->ResolveDoubleClickAction(IsDirectory, ATerminal);
  5539. }
  5540. if (Action == rdcaCopy)
  5541. {
  5542. // To counter the lock in DirViewBusy (called from TCustomDirView.DoExecute)
  5543. UnlockWindow();
  5544. try
  5545. {
  5546. if (IsLocalBrowserMode())
  5547. {
  5548. LocalLocalCopy(foCopy, Side, true, !WinConfiguration->CopyOnDoubleClickConfirmation, false, 0);
  5549. }
  5550. else
  5551. {
  5552. if (HasActiveTerminal())
  5553. {
  5554. ExecuteFileOperation(foCopy, Side, true, !WinConfiguration->CopyOnDoubleClickConfirmation);
  5555. }
  5556. }
  5557. }
  5558. __finally
  5559. {
  5560. LockWindow();
  5561. }
  5562. AllowExec = false;
  5563. }
  5564. else if (Action == rdcaEdit)
  5565. {
  5566. if (!Remote || !WinConfiguration->DisableOpenEdit)
  5567. {
  5568. ExecuteFile(Side, efDefaultEditor);
  5569. AllowExec = false;
  5570. }
  5571. }
  5572. else if (Action == rdcaOpen)
  5573. {
  5574. if (DebugAlwaysTrue(!IsDirectory) && Remote && !WinConfiguration->DisableOpenEdit)
  5575. {
  5576. ExecuteFile(osRemote, efShell);
  5577. AllowExec = false;
  5578. }
  5579. }
  5580. }
  5581. //---------------------------------------------------------------------------
  5582. bool __fastcall TCustomScpExplorerForm::GetHasDirView(TOperationSide Side)
  5583. {
  5584. return ((Side == osRemote) || (Side == osCurrent));
  5585. }
  5586. //---------------------------------------------------------------------------
  5587. void __fastcall TCustomScpExplorerForm::CompareDirectories()
  5588. {
  5589. DebugFail();
  5590. }
  5591. //---------------------------------------------------------------------------
  5592. void __fastcall TCustomScpExplorerForm::SynchronizeDirectories()
  5593. {
  5594. DebugFail();
  5595. }
  5596. //---------------------------------------------------------------------------
  5597. bool __fastcall TCustomScpExplorerForm::DoSynchronizeDirectories(
  5598. UnicodeString & LocalDirectory, UnicodeString & RemoteDirectory, int UseDefaults)
  5599. {
  5600. TSynchronizeParamType Params;
  5601. Params.LocalDirectory = LocalDirectory;
  5602. Params.RemoteDirectory = RemoteDirectory;
  5603. int UnusedParams =
  5604. (GUIConfiguration->SynchronizeParams &
  5605. (TTerminal::spPreviewChanges | TTerminal::spTimestamp | TTerminal::spNotByTime | TTerminal::spBySize |
  5606. TTerminal::spCaseSensitive | TTerminal::spMirror | TTerminal::spByChecksum));
  5607. Params.Params = GUIConfiguration->SynchronizeParams & ~UnusedParams;
  5608. Params.Options = GUIConfiguration->SynchronizeOptions;
  5609. bool SaveSettings = false;
  5610. TSynchronizeController Controller(&DoSynchronize, &DoSynchronizeInvalid,
  5611. &DoSynchronizeTooManyDirectories);
  5612. DebugAssert(FSynchronizeController == NULL);
  5613. FSynchronizeController = &Controller;
  5614. bool Result;
  5615. try
  5616. {
  5617. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  5618. int CopyParamAttrs = Terminal->UsableCopyParamAttrs(0).Upload;
  5619. int Options =
  5620. FLAGMASK(SynchronizeAllowSelectedOnly(), soAllowSelectedOnly);
  5621. DebugAssert(FOnFeedSynchronizeError == NULL);
  5622. DebugAssert(FOnSynchronizeAbort == NULL);
  5623. DebugAssert(FSynchronizeTerminal == NULL);
  5624. FSynchronizeTerminal = Terminal;
  5625. Result = DoSynchronizeDialog(Params, &CopyParam, Controller.StartStop,
  5626. SaveSettings, Options, CopyParamAttrs, GetSynchronizeOptions, SynchronizeSessionLog,
  5627. FOnFeedSynchronizeError, FOnSynchronizeAbort, SynchronizeInNewWindow, UseDefaults);
  5628. if (Result)
  5629. {
  5630. if (SaveSettings)
  5631. {
  5632. GUIConfiguration->SynchronizeParams = Params.Params | UnusedParams;
  5633. GUIConfiguration->SynchronizeOptions = Params.Options;
  5634. }
  5635. else
  5636. {
  5637. if (FLAGSET(GUIConfiguration->SynchronizeOptions, soSynchronizeAsk) &&
  5638. FLAGCLEAR(Params.Options, soSynchronizeAsk) &&
  5639. FLAGSET(Params.Options, soSynchronize))
  5640. {
  5641. GUIConfiguration->SynchronizeOptions =
  5642. (GUIConfiguration->SynchronizeOptions & ~soSynchronizeAsk) |
  5643. soSynchronize;
  5644. }
  5645. }
  5646. LocalDirectory = Params.LocalDirectory;
  5647. RemoteDirectory = Params.RemoteDirectory;
  5648. }
  5649. }
  5650. __finally
  5651. {
  5652. FSynchronizeController = NULL;
  5653. DebugAssert(FOnFeedSynchronizeError == NULL);
  5654. FOnFeedSynchronizeError = NULL;
  5655. DebugAssert(FOnSynchronizeAbort == NULL);
  5656. FOnSynchronizeAbort = NULL;
  5657. FSynchronizeTerminal = NULL;
  5658. }
  5659. return Result;
  5660. }
  5661. //---------------------------------------------------------------------------
  5662. void __fastcall TCustomScpExplorerForm::DoSynchronize(
  5663. TSynchronizeController * /*Sender*/, const UnicodeString LocalDirectory,
  5664. const UnicodeString RemoteDirectory, const TCopyParamType & CopyParam,
  5665. const TSynchronizeParamType & Params, TSynchronizeChecklist ** Checklist,
  5666. TSynchronizeOptions * Options, bool Full)
  5667. {
  5668. // Keep-up-to-date mode
  5669. if (Terminal->Status == ssOpened)
  5670. {
  5671. try
  5672. {
  5673. int PParams = Params.Params;
  5674. if (!Full)
  5675. {
  5676. PParams |= TTerminal::spNoRecurse | TTerminal::spUseCache | TTerminal::spDelayProgress;
  5677. if (FLAGSET(Params.Options, soRecurse))
  5678. {
  5679. PParams |= TTerminal::spSubDirs;
  5680. }
  5681. }
  5682. else
  5683. {
  5684. // if keepuptodate is non-recursive,
  5685. // full sync before has to be non-recursive as well
  5686. if (FLAGCLEAR(Params.Options, soRecurse))
  5687. {
  5688. PParams |= TTerminal::spNoRecurse;
  5689. }
  5690. }
  5691. Synchronize(LocalDirectory, RemoteDirectory, smRemote, CopyParam,
  5692. PParams, Checklist, Options);
  5693. }
  5694. catch (Exception & E)
  5695. {
  5696. if (!Terminal->Active)
  5697. {
  5698. ShowExtendedExceptionEx(Terminal, &E);
  5699. // Do not abort the "keep up to date" when the session was succesfully reconnected.
  5700. if (!Terminal->Active)
  5701. {
  5702. throw;
  5703. }
  5704. }
  5705. else
  5706. {
  5707. if (FLAGSET(Params.Options, soContinueOnError) &&
  5708. DebugAlwaysTrue(FOnFeedSynchronizeError != NULL))
  5709. {
  5710. // noop, already logged in MoreMessageDialog
  5711. }
  5712. else
  5713. {
  5714. // We get mostly EAbort here, so this is noop.
  5715. // Exception is an error when listing directories while looking for differences.
  5716. ShowExtendedExceptionEx(Terminal, &E);
  5717. throw;
  5718. }
  5719. }
  5720. }
  5721. }
  5722. }
  5723. //---------------------------------------------------------------------------
  5724. void __fastcall TCustomScpExplorerForm::DoSynchronizeInvalid(
  5725. TSynchronizeController * /*Sender*/, const UnicodeString Directory,
  5726. const UnicodeString ErrorStr)
  5727. {
  5728. if (!Directory.IsEmpty())
  5729. {
  5730. SimpleErrorDialog(FMTLOAD(WATCH_ERROR_DIRECTORY, (Directory)), ErrorStr);
  5731. }
  5732. else
  5733. {
  5734. SimpleErrorDialog(LoadStr(WATCH_ERROR_GENERAL), ErrorStr);
  5735. }
  5736. }
  5737. //---------------------------------------------------------------------------
  5738. void __fastcall TCustomScpExplorerForm::DoSynchronizeTooManyDirectories(
  5739. TSynchronizeController * /*Sender*/, int & MaxDirectories)
  5740. {
  5741. if (MaxDirectories < GUIConfiguration->MaxWatchDirectories)
  5742. {
  5743. MaxDirectories = GUIConfiguration->MaxWatchDirectories;
  5744. }
  5745. else
  5746. {
  5747. TMessageParams Params(mpNeverAskAgainCheck);
  5748. unsigned int Result = MessageDialog(
  5749. FMTLOAD(TOO_MANY_WATCH_DIRECTORIES, (MaxDirectories, MaxDirectories)),
  5750. qtConfirmation, qaYes | qaNo, HELP_TOO_MANY_WATCH_DIRECTORIES, &Params);
  5751. if ((Result == qaYes) || (Result == qaNeverAskAgain))
  5752. {
  5753. MaxDirectories *= 2;
  5754. if (Result == qaNeverAskAgain)
  5755. {
  5756. GUIConfiguration->MaxWatchDirectories = MaxDirectories;
  5757. }
  5758. }
  5759. else
  5760. {
  5761. Abort();
  5762. }
  5763. }
  5764. }
  5765. //---------------------------------------------------------------------------
  5766. void __fastcall TCustomScpExplorerForm::Synchronize(const UnicodeString LocalDirectory,
  5767. const UnicodeString RemoteDirectory, TSynchronizeMode Mode,
  5768. const TCopyParamType & CopyParam, int Params, TSynchronizeChecklist ** Checklist,
  5769. TSynchronizeOptions * Options)
  5770. {
  5771. // Keep-up-to-date mode
  5772. DebugAssert(!FAutoOperation);
  5773. void * BatchStorage;
  5774. BatchStart(BatchStorage);
  5775. FAutoOperation = true;
  5776. bool AnyOperation = false;
  5777. TDateTime StartTime = Now();
  5778. TSynchronizeChecklist * AChecklist = NULL;
  5779. try
  5780. {
  5781. FSynchronizeProgressForm = new TSynchronizeProgressForm(Application, true, -1);
  5782. if (FLAGCLEAR(Params, TTerminal::spDelayProgress))
  5783. {
  5784. FSynchronizeProgressForm->Start();
  5785. }
  5786. AChecklist = Terminal->SynchronizeCollect(LocalDirectory, RemoteDirectory,
  5787. static_cast<TTerminal::TSynchronizeMode>(Mode),
  5788. &CopyParam, Params | TTerminal::spNoConfirmation, TerminalSynchronizeDirectory,
  5789. Options);
  5790. SetTaskbarListProgressState(TBPF_NOPROGRESS);
  5791. SAFE_DESTROY(FSynchronizeProgressForm);
  5792. AnyOperation = (AChecklist->CheckedCount > 0);
  5793. if (AnyOperation)
  5794. {
  5795. TSynchronizeProgress SynchronizeProgress(AChecklist);
  5796. CreateProgressForm(&SynchronizeProgress);
  5797. try
  5798. {
  5799. Terminal->SynchronizeApply(
  5800. AChecklist, &CopyParam, Params | TTerminal::spNoConfirmation, TerminalSynchronizeDirectory,
  5801. SynchronizeProcessedItem, NULL, NULL, NULL);
  5802. }
  5803. __finally
  5804. {
  5805. DestroyProgressForm();
  5806. }
  5807. }
  5808. }
  5809. __finally
  5810. {
  5811. if (Checklist == NULL)
  5812. {
  5813. delete AChecklist;
  5814. }
  5815. else
  5816. {
  5817. *Checklist = AChecklist;
  5818. }
  5819. FAutoOperation = false;
  5820. SAFE_DESTROY(FSynchronizeProgressForm);
  5821. BatchEnd(BatchStorage);
  5822. ReloadLocalDirectory();
  5823. if (AnyOperation)
  5824. {
  5825. OperationComplete(StartTime);
  5826. }
  5827. }
  5828. }
  5829. //---------------------------------------------------------------------------
  5830. bool __fastcall TCustomScpExplorerForm::SynchronizeAllowSelectedOnly()
  5831. {
  5832. // can be called from command line
  5833. return Visible &&
  5834. ((DirView(osRemote)->SelCount > 0) ||
  5835. (HasDirView[osLocal] && (DirView(osLocal)->SelCount > 0)));
  5836. }
  5837. //---------------------------------------------------------------------------
  5838. void __fastcall TCustomScpExplorerForm::SynchronizeSessionLog(const UnicodeString & Message)
  5839. {
  5840. LogSynchronizeEvent(Terminal, Message);
  5841. }
  5842. //---------------------------------------------------------------------------
  5843. void __fastcall TCustomScpExplorerForm::GetSynchronizeOptions(
  5844. int Params, TSynchronizeOptions & Options)
  5845. {
  5846. DebugAssert(!IsLocalBrowserMode());
  5847. if (FLAGSET(Params, TTerminal::spSelectedOnly) && SynchronizeAllowSelectedOnly())
  5848. {
  5849. Options.Filter = new TStringList();
  5850. Options.Filter->CaseSensitive = false;
  5851. Options.Filter->Duplicates = Types::dupAccept;
  5852. if (DirView(osRemote)->SelCount > 0)
  5853. {
  5854. DirView(osRemote)->CreateFileList(false, false, Options.Filter);
  5855. }
  5856. if (HasDirView[osLocal] && (DirView(osLocal)->SelCount > 0))
  5857. {
  5858. DirView(osLocal)->CreateFileList(false, false, Options.Filter);
  5859. }
  5860. Options.Filter->Sort();
  5861. }
  5862. }
  5863. //---------------------------------------------------------------------------
  5864. UnicodeString __fastcall TCustomScpExplorerForm::SerializeCopyParamForCommandLine(const TCopyParamType * CopyParams)
  5865. {
  5866. TCopyParamType Defaults;
  5867. std::unique_ptr<THierarchicalStorage> ConfigStorage(Configuration->CreateConfigStorage());
  5868. ConfigStorage->AccessMode = smRead;
  5869. if (ConfigStorage->OpenSubKey(Configuration->ConfigurationSubKey, false))
  5870. {
  5871. GUIConfiguration->LoadCopyParam(ConfigStorage.get(), &Defaults);
  5872. }
  5873. ConfigStorage.reset(NULL);
  5874. std::unique_ptr<TStringList> Options(new TStringList());
  5875. std::unique_ptr<TOptionsStorage> OptionsStorage(new TOptionsStorage(Options.get(), true));
  5876. CopyParams->Save(OptionsStorage.get(), &Defaults);
  5877. UnicodeString Result;
  5878. if (Options->Count > 0)
  5879. {
  5880. Result =
  5881. FORMAT(L" %s%s", (TProgramParams::FormatSwitch(RAWTRANSFERSETTINGS_SWITCH), StringsToParams(Options.get())));
  5882. }
  5883. return Result;
  5884. }
  5885. //---------------------------------------------------------------------------
  5886. void __fastcall TCustomScpExplorerForm::SynchronizeInNewWindow(
  5887. const TSynchronizeParamType & Params, const TCopyParamType * CopyParams)
  5888. {
  5889. UnicodeString SessionName = CreateHiddenDuplicateSession();
  5890. UnicodeString AdditionalParams =
  5891. FORMAT(L"%s%s %s // \"%s\" \"%s\" %d %d", (
  5892. TProgramParams::FormatSwitch(DEFAULTS_SWITCH),
  5893. SerializeCopyParamForCommandLine(CopyParams),
  5894. TProgramParams::FormatSwitch(KEEP_UP_TO_DATE_SWITCH),
  5895. Params.LocalDirectory, Params.RemoteDirectory, Params.Params, Params.Options));
  5896. ExecuteNewInstance(SessionName, AdditionalParams);
  5897. }
  5898. //---------------------------------------------------------------------------
  5899. struct TSynchronizeParams
  5900. {
  5901. UnicodeString LocalDirectory;
  5902. UnicodeString RemoteDirectory;
  5903. TSynchronizeMode Mode;
  5904. TSynchronizeChecklist * Checklist;
  5905. int Params;
  5906. TCopyParamType * CopyParam;
  5907. TDateTime CollectElapsed;
  5908. TDateTime * StartTime;
  5909. TProcessedSynchronizationChecklistItem OnProcessedItem;
  5910. };
  5911. //---------------------------------------------------------------------------
  5912. void __fastcall TCustomScpExplorerForm::FullSynchronize(
  5913. TSynchronizeParams & Params, TProcessedSynchronizationChecklistItem OnProcessedItem,
  5914. TUpdatedSynchronizationChecklistItems OnUpdatedSynchronizationChecklistItems)
  5915. {
  5916. DebugAssert(!FAutoOperation);
  5917. void * BatchStorage;
  5918. BatchStart(BatchStorage);
  5919. FAutoOperation = true;
  5920. TFileOperationStatistics Statistics;
  5921. TDateTime Start = Now();
  5922. try
  5923. {
  5924. Params.OnProcessedItem = OnProcessedItem;
  5925. TSynchronizeProgress SynchronizeProgress(Params.Checklist);
  5926. CreateProgressForm(&SynchronizeProgress);
  5927. Terminal->SynchronizeApply(
  5928. Params.Checklist, Params.CopyParam, Params.Params | TTerminal::spNoConfirmation,
  5929. TerminalSynchronizeDirectory, SynchronizeProcessedItem, OnUpdatedSynchronizationChecklistItems, &Params,
  5930. &Statistics);
  5931. }
  5932. __finally
  5933. {
  5934. Params.OnProcessedItem = NULL;
  5935. FAutoOperation = false;
  5936. DestroyProgressForm();
  5937. BatchEnd(BatchStorage);
  5938. ReloadLocalDirectory();
  5939. }
  5940. if (WinConfiguration->SynchronizeSummary)
  5941. {
  5942. UnicodeString Message = MainInstructions(LoadStr(SYNCHRONIZE_COMPLETE)) + L"\n";
  5943. // The statistics should be 0 anyway in this case
  5944. if (FLAGCLEAR(Params.Params, TTerminal::spTimestamp))
  5945. {
  5946. Message += L"\n";
  5947. if (Statistics.FilesUploaded > 0)
  5948. {
  5949. Message += FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 1), (FormatNumber(Statistics.FilesUploaded), FormatBytes(Statistics.TotalUploaded))) + L"\n";
  5950. }
  5951. if (Statistics.FilesDownloaded > 0)
  5952. {
  5953. Message += FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 2), (FormatNumber(Statistics.FilesDownloaded), FormatBytes(Statistics.TotalDownloaded))) + L"\n";
  5954. }
  5955. if (Statistics.FilesDeletedLocal > 0)
  5956. {
  5957. Message += FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 3), (FormatNumber(Statistics.FilesDeletedLocal))) + L"\n";
  5958. }
  5959. if (Statistics.FilesDeletedRemote > 0)
  5960. {
  5961. Message += FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 4), (FormatNumber(Statistics.FilesDeletedRemote))) + L"\n";
  5962. }
  5963. }
  5964. TDateTime Elapsed = (Now() - Start);
  5965. Message +=
  5966. L"\n" +
  5967. FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 5), (FormatDateTimeSpan(Params.CollectElapsed))) + L"\n" +
  5968. FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 6), (FormatDateTimeSpan(Elapsed)));
  5969. TMessageParams Params(mpNeverAskAgainCheck);
  5970. unsigned int Result = MessageDialog(Message, qtInformation, qaOK, HELP_NONE, &Params);
  5971. if (Result == qaNeverAskAgain)
  5972. {
  5973. WinConfiguration->SynchronizeSummary = false;
  5974. }
  5975. }
  5976. }
  5977. //---------------------------------------------------------------------------
  5978. void __fastcall TCustomScpExplorerForm::SynchronizeProcessedItem(void * Token, const TSynchronizeChecklist::TItem * ChecklistItem)
  5979. {
  5980. if (DebugAlwaysTrue(FProgressForm != NULL) && DebugAlwaysTrue(FProgressForm->SynchronizeProgress != NULL))
  5981. {
  5982. FProgressForm->SynchronizeProgress->ItemProcessed(ChecklistItem);
  5983. }
  5984. // Not set in keep-up-to-date mode - Synchronize() method
  5985. if (Token != NULL)
  5986. {
  5987. TSynchronizeParams & Params = *static_cast<TSynchronizeParams *>(Token);
  5988. if (Params.OnProcessedItem != NULL)
  5989. {
  5990. Params.OnProcessedItem(NULL, ChecklistItem);
  5991. }
  5992. }
  5993. }
  5994. //---------------------------------------------------------------------------
  5995. void __fastcall TCustomScpExplorerForm::DoFullSynchronize(
  5996. void * Token, TProcessedSynchronizationChecklistItem OnProcessedItem,
  5997. TUpdatedSynchronizationChecklistItems OnUpdatedSynchronizationChecklistItems)
  5998. {
  5999. TSynchronizeParams & Params = *static_cast<TSynchronizeParams *>(Token);
  6000. *Params.StartTime = Now();
  6001. FullSynchronize(Params, OnProcessedItem, OnUpdatedSynchronizationChecklistItems);
  6002. }
  6003. //---------------------------------------------------------------------------
  6004. void TCustomScpExplorerForm::DoQueueSynchronize(void * Token)
  6005. {
  6006. TSynchronizeParams & Params = *static_cast<TSynchronizeParams *>(Token);
  6007. int CopyParams = TTerminal::GetSynchronizeCopyParams(Params.Params);
  6008. TCopyParamType SyncCopyParam = TTerminal::GetSynchronizeCopyParam(Params.CopyParam, Params.Params);
  6009. bool Parallel = GUIConfiguration->CurrentCopyParam.QueueParallel;
  6010. int Index = 0;
  6011. const TSynchronizeChecklist::TItem * ChecklistItem;
  6012. while (Params.Checklist->GetNextChecked(Index, ChecklistItem))
  6013. {
  6014. TQueueItem * QueueItem = Terminal->SynchronizeToQueue(ChecklistItem, &SyncCopyParam, CopyParams, Parallel);
  6015. AddQueueItem(Queue, QueueItem, Terminal);
  6016. }
  6017. }
  6018. //---------------------------------------------------------------------------
  6019. void __fastcall TCustomScpExplorerForm::DoSynchronizeChecklistCalculateSize(
  6020. TSynchronizeChecklist * Checklist, const TSynchronizeChecklist::TItemList & Items, void * Token)
  6021. {
  6022. // terminal can be already closed (e.g. dropped connection)
  6023. if (Terminal != NULL)
  6024. {
  6025. TSynchronizeParams & Params = *static_cast<TSynchronizeParams *>(Token);
  6026. Terminal->SynchronizeChecklistCalculateSize(Checklist, Items, Params.CopyParam);
  6027. }
  6028. }
  6029. //---------------------------------------------------------------------------
  6030. void __fastcall TCustomScpExplorerForm::DoSynchronizeMove(
  6031. TOperationSide Side, TStrings * FileList, const UnicodeString & NewFileName, bool TargetIsDirectory, void * Token)
  6032. {
  6033. DebugAssert(!IsLocalBrowserMode());
  6034. TAutoBatch AutoBatch(this);
  6035. TAutoFlag AutoOperationFlag(FAutoOperation);
  6036. TSynchronizeParams & Params = *static_cast<TSynchronizeParams *>(Token);
  6037. if (Side == osRemote)
  6038. {
  6039. UnicodeString Target;
  6040. UnicodeString FileMask;
  6041. if (TargetIsDirectory)
  6042. {
  6043. Target = NewFileName;
  6044. Terminal->CreateTargetDirectory(Target, faDirectory, Params.CopyParam);
  6045. }
  6046. else
  6047. {
  6048. Target = UnixExtractFileDir(NewFileName);
  6049. FileMask = DelimitFileNameMask(UnixExtractFileName(NewFileName));
  6050. }
  6051. RemoteDirView->SaveSelection();
  6052. RemoteDirView->SaveSelectedNames();
  6053. try
  6054. {
  6055. Terminal->MoveFiles(FileList, Target, FileMask, false);
  6056. }
  6057. catch(...)
  6058. {
  6059. RemoteDirView->DiscardSavedSelection();
  6060. throw;
  6061. }
  6062. RemoteDirView->RestoreSelection();
  6063. }
  6064. else if (DebugAlwaysTrue(Side == osLocal))
  6065. {
  6066. std::unique_ptr<TStrings> Directories(CreateSortedStringList());
  6067. if (TargetIsDirectory)
  6068. {
  6069. if (!ForceDirectories(ApiPath(NewFileName)))
  6070. {
  6071. throw EOSExtException(FMTLOAD(CREATE_DIR_ERROR, (NewFileName)));
  6072. }
  6073. Directories->Add(NewFileName);
  6074. }
  6075. else
  6076. {
  6077. Directories->Add(ExtractFileDir(NewFileName));
  6078. }
  6079. for (int Index = 0; Index < FileList->Count; Index++)
  6080. {
  6081. UnicodeString FileName = FileList->Strings[Index];
  6082. UnicodeString Directory = ExtractFileDir(FileName);
  6083. Directories->Add(Directory);
  6084. UnicodeString NewFilePath = NewFileName;
  6085. if (TargetIsDirectory)
  6086. {
  6087. NewFilePath = TPath::Combine(NewFilePath, ExtractFileName(FileName));
  6088. }
  6089. if (!MoveFile(ApiPath(FileName).c_str(), ApiPath(NewFilePath).c_str()))
  6090. {
  6091. throw EOSExtException(FMTLOAD(RENAME_FILE_ERROR, (FileName, NewFilePath)));
  6092. }
  6093. }
  6094. for (int Index = 0; Index < Directories->Count; Index++)
  6095. {
  6096. ReloadLocalDirectory(Directories->Strings[Index]);
  6097. }
  6098. }
  6099. }
  6100. //---------------------------------------------------------------------------
  6101. void __fastcall TCustomScpExplorerForm::DoSynchronizeExplore(TOperationSide Side, TSynchronizeChecklist::TAction Action, const TSynchronizeChecklist::TItem * Item)
  6102. {
  6103. UnicodeString LocalPath = ExcludeTrailingBackslash(Item->Local.Directory);
  6104. if (Side == osLocal)
  6105. {
  6106. if (Action == TSynchronizeChecklist::saDownloadNew)
  6107. {
  6108. OpenFolderInExplorer(LocalPath);
  6109. }
  6110. else
  6111. {
  6112. OpenFileInExplorer(Item->ForceGetLocalPath());
  6113. }
  6114. }
  6115. else if (DebugAlwaysTrue(Side == osRemote))
  6116. {
  6117. // Similar to CreateHiddenDuplicateSession, except that it modifies the initial directories
  6118. std::unique_ptr<TSessionData> SessionData(CloneCurrentSessionData());
  6119. SessionData->RemoteDirectory = UnixExcludeTrailingBackslash(Item->Remote.Directory);
  6120. if (!LocalPath.IsEmpty())
  6121. {
  6122. SessionData->LocalDirectory = LocalPath;
  6123. }
  6124. UnicodeString SessionName = SaveHiddenDuplicateSession(SessionData.get());
  6125. ExecuteNewInstance(SessionName, FORMAT(L"%s=\"%s\"", (TProgramParams::FormatSwitch(EXPLORE_SWITCH), Item->GetFileName())));
  6126. }
  6127. }
  6128. //---------------------------------------------------------------------------
  6129. void __fastcall TCustomScpExplorerForm::FullSynchronizeInNewWindow(
  6130. TSynchronizeMode Mode, int Params, const UnicodeString & LocalDirectory, const UnicodeString & RemoteDirectory,
  6131. const TCopyParamType * CopyParams)
  6132. {
  6133. UnicodeString SessionName = CreateHiddenDuplicateSession();
  6134. UnicodeString AdditionalParams =
  6135. FORMAT(L"%s%s %s // \"%s\" \"%s\" %d %d", (
  6136. TProgramParams::FormatSwitch(DEFAULTS_SWITCH),
  6137. SerializeCopyParamForCommandLine(CopyParams),
  6138. TProgramParams::FormatSwitch(SYNCHRONIZE_SWITCH),
  6139. LocalDirectory, RemoteDirectory, Mode, Params));
  6140. ExecuteNewInstance(SessionName, AdditionalParams);
  6141. }
  6142. //---------------------------------------------------------------------------
  6143. int __fastcall TCustomScpExplorerForm::DoFullSynchronizeDirectories(
  6144. UnicodeString & LocalDirectory, UnicodeString & RemoteDirectory,
  6145. TSynchronizeMode & Mode, int Params, bool & SaveMode, int UseDefaults)
  6146. {
  6147. int Result;
  6148. bool SaveSettings = false;
  6149. int Options =
  6150. FLAGMASK(!Terminal->IsCapable[fcTimestampChanging], fsoDisableTimestamp) |
  6151. FLAGMASK(!CanCalculateChecksum(), fsoDisableByChecksum) |
  6152. FLAGMASK(SynchronizeAllowSelectedOnly(), fsoAllowSelectedOnly);
  6153. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  6154. TUsableCopyParamAttrs CopyParamAttrs = Terminal->UsableCopyParamAttrs(0);
  6155. bool Continue =
  6156. ((UseDefaults == 0) ||
  6157. DoFullSynchronizeDialog(Mode, Params, LocalDirectory, RemoteDirectory,
  6158. &CopyParam, SaveSettings, SaveMode, Options, CopyParamAttrs, FullSynchronizeInNewWindow, UseDefaults)) &&
  6159. (FLAGCLEAR(Params, TTerminal::spByChecksum) || EnsureCommandSessionFallback(fcCalculatingChecksum));
  6160. if (Continue)
  6161. {
  6162. Configuration->Usage->Inc(L"Synchronizations");
  6163. CopyParam.IncludeFileMask.SetRoots(LocalDirectory, RemoteDirectory);
  6164. UpdateCopyParamCounters(CopyParam);
  6165. TSynchronizeOptions SynchronizeOptions;
  6166. GetSynchronizeOptions(Params, SynchronizeOptions);
  6167. if (SaveSettings)
  6168. {
  6169. GUIConfiguration->SynchronizeParams = Params;
  6170. }
  6171. else
  6172. {
  6173. SaveMode = false;
  6174. }
  6175. TDateTime StartTime = Now();
  6176. TSynchronizeChecklist * Checklist = NULL;
  6177. try
  6178. {
  6179. DebugAssert(!FAutoOperation);
  6180. FAutoOperation = true;
  6181. try
  6182. {
  6183. UnicodeString SessionKey = Terminal->SessionData->SessionKey;
  6184. std::unique_ptr<TStrings> DataList(Configuration->LoadDirectoryStatisticsCache(SessionKey, RemoteDirectory, CopyParam));
  6185. int Files = -1;
  6186. if (DataList->Count >= 1)
  6187. {
  6188. Files = StrToIntDef(DataList->Strings[0], Files);
  6189. }
  6190. else
  6191. {
  6192. DataList->Add(UnicodeString());
  6193. }
  6194. FSynchronizeProgressForm = new TSynchronizeProgressForm(Application, true, Files);
  6195. FSynchronizeProgressForm->Start();
  6196. Checklist = Terminal->SynchronizeCollect(LocalDirectory, RemoteDirectory,
  6197. static_cast<TTerminal::TSynchronizeMode>(Mode),
  6198. &CopyParam, Params | TTerminal::spNoConfirmation, TerminalSynchronizeDirectory,
  6199. &SynchronizeOptions);
  6200. if (Terminal->SessionData->CacheDirectories)
  6201. {
  6202. DataList->Strings[0] = IntToStr(SynchronizeOptions.Files);
  6203. Configuration->SaveDirectoryStatisticsCache(SessionKey, RemoteDirectory, CopyParam, DataList.get());
  6204. }
  6205. }
  6206. __finally
  6207. {
  6208. FAutoOperation = false;
  6209. SetTaskbarListProgressState(TBPF_NOPROGRESS);
  6210. SAFE_DESTROY(FSynchronizeProgressForm);
  6211. }
  6212. TSynchronizeParams SynchronizeParams;
  6213. SynchronizeParams.LocalDirectory = LocalDirectory;
  6214. SynchronizeParams.RemoteDirectory = RemoteDirectory;
  6215. SynchronizeParams.Mode = Mode;
  6216. SynchronizeParams.CopyParam = &CopyParam;
  6217. SynchronizeParams.Params = Params;
  6218. SynchronizeParams.Checklist = Checklist;
  6219. SynchronizeParams.CollectElapsed = (Now() - StartTime);
  6220. SynchronizeParams.StartTime = &StartTime;
  6221. SynchronizeParams.OnProcessedItem = NULL;
  6222. Result = Checklist->Count;
  6223. if (Checklist->Count > 0)
  6224. {
  6225. if (FLAGSET(Params, TTerminal::spPreviewChanges))
  6226. {
  6227. TQueueSynchronizeEvent OnQueueSynchronize = NULL;
  6228. if (Visible && FLAGCLEAR(Params, TTerminal::spTimestamp))
  6229. {
  6230. OnQueueSynchronize = DoQueueSynchronize;
  6231. }
  6232. if (!DoSynchronizeChecklistDialog(
  6233. Checklist, Mode, Params, LocalDirectory, RemoteDirectory, CustomCommandMenu, DoFullSynchronize,
  6234. OnQueueSynchronize, DoSynchronizeChecklistCalculateSize, DoSynchronizeMove, DoSynchronizeExplore,
  6235. &SynchronizeParams))
  6236. {
  6237. Result = -1;
  6238. }
  6239. }
  6240. else
  6241. {
  6242. FullSynchronize(SynchronizeParams, NULL, NULL);
  6243. }
  6244. }
  6245. else
  6246. {
  6247. UnicodeString Message = MainInstructions(LoadStr(COMPARE_NO_DIFFERENCES));
  6248. MessageDialog(Message, qtInformation, qaOK, HELP_SYNCHRONIZE_NO_DIFFERENCES);
  6249. }
  6250. }
  6251. __finally
  6252. {
  6253. delete Checklist;
  6254. }
  6255. OperationComplete(StartTime);
  6256. }
  6257. else
  6258. {
  6259. Result = -1;
  6260. }
  6261. return Result;
  6262. }
  6263. //---------------------------------------------------------------------------
  6264. void __fastcall TCustomScpExplorerForm::TerminalSynchronizeDirectory(
  6265. const UnicodeString & LocalDirectory, const UnicodeString & RemoteDirectory,
  6266. bool & Continue, bool Collect, const TSynchronizeOptions * Options)
  6267. {
  6268. if (Collect)
  6269. {
  6270. DebugAssert(FSynchronizeProgressForm != NULL);
  6271. if (!FSynchronizeProgressForm->Started)
  6272. {
  6273. FSynchronizeProgressForm->Start();
  6274. }
  6275. int CompareProgress = FSynchronizeProgressForm->SetData(LocalDirectory, RemoteDirectory, Options->Files, Continue);
  6276. SetTaskbarListProgressValue(CompareProgress);
  6277. }
  6278. else
  6279. {
  6280. DebugAssert(FSynchronizeProgressForm == NULL);
  6281. DebugAssert(FProgressForm != NULL);
  6282. }
  6283. }
  6284. //---------------------------------------------------------------------------
  6285. void __fastcall TCustomScpExplorerForm::StandaloneEdit(const UnicodeString & FileName)
  6286. {
  6287. UnicodeString FullFileName = AbsolutePath(Terminal->CurrentDirectory, FileName);
  6288. std::unique_ptr<TRemoteFile> File(Terminal->ReadFile(FullFileName));
  6289. if (File.get() != NULL)
  6290. {
  6291. ExecuteRemoteFile(FullFileName, File.get(), efInternalEditor);
  6292. Application->ShowMainForm = false;
  6293. Application->Run();
  6294. }
  6295. }
  6296. //---------------------------------------------------------------------------
  6297. void __fastcall TCustomScpExplorerForm::ExploreLocalDirectory(TOperationSide)
  6298. {
  6299. DebugFail();
  6300. }
  6301. //---------------------------------------------------------------------------
  6302. TSessionData * __fastcall TCustomScpExplorerForm::CloneCurrentSessionData()
  6303. {
  6304. std::unique_ptr<TSessionData> SessionData(new TSessionData(L""));
  6305. SessionData->Assign(ManagedSession->SessionData);
  6306. UpdateSessionData(SessionData.get());
  6307. DebugAssert(SessionData->IsLocalBrowser == IsLocalBrowserMode());
  6308. if (IsActiveTerminal(ManagedSession))
  6309. {
  6310. ManagedSession->UpdateSessionCredentials(SessionData.get());
  6311. }
  6312. if (!ManagedSession->SessionData->HasSessionName())
  6313. {
  6314. // Particularly for "Workspace/XXXX" name, we need to reset it, as it would become user-visible
  6315. // once IsWorkspace is cleared
  6316. SessionData->Name = UnicodeString();
  6317. }
  6318. // Most uses of this method ends with saving the session, and we do not want to store the IsWorkspace flag.
  6319. // Particularly, with SaveHiddenDuplicateSession, it won't work as TSessionData::ParseUrl explicitly ignored workspace sessions.
  6320. SessionData->IsWorkspace = false;
  6321. return SessionData.release();
  6322. }
  6323. //---------------------------------------------------------------------------
  6324. void __fastcall TCustomScpExplorerForm::SaveCurrentSession()
  6325. {
  6326. TSessionData * SessionData = CloneCurrentSessionData();
  6327. try
  6328. {
  6329. TSessionData * EditingSessionData = StoredSessions->FindSame(SessionData);
  6330. DoSaveSession(SessionData, EditingSessionData, true, NULL);
  6331. }
  6332. __finally
  6333. {
  6334. delete SessionData;
  6335. }
  6336. }
  6337. //---------------------------------------------------------------------------
  6338. TObjectList * __fastcall TCustomScpExplorerForm::DoCollectWorkspace()
  6339. {
  6340. TTerminalManager * Manager = TTerminalManager::Instance();
  6341. std::unique_ptr<TObjectList> DataList(new TObjectList());
  6342. if (DebugAlwaysTrue(ManagedSession != NULL))
  6343. {
  6344. // Update ManagedSession->StateData
  6345. UpdateSession(ManagedSession);
  6346. }
  6347. Manager->SaveWorkspace(DataList.get());
  6348. return DataList.release();
  6349. }
  6350. //---------------------------------------------------------------------------
  6351. void __fastcall TCustomScpExplorerForm::DoSaveWorkspace(
  6352. const UnicodeString & Name, TObjectList * DataList, bool SavePasswords, bool Explicit)
  6353. {
  6354. WinConfiguration->LastWorkspace = Name;
  6355. if (!SavePasswords)
  6356. {
  6357. for (int Index = 0; Index < DataList->Count; Index++)
  6358. {
  6359. TSessionData * SessionData = dynamic_cast<TSessionData *>(DataList->Items[Index]);
  6360. if (SessionData->Link.IsEmpty())
  6361. {
  6362. SessionData->ClearSessionPasswords();
  6363. }
  6364. }
  6365. }
  6366. StoredSessions->NewWorkspace(Name, DataList);
  6367. // modified only
  6368. StoredSessions->Save(false, Explicit);
  6369. }
  6370. //---------------------------------------------------------------------------
  6371. UnicodeString __fastcall TCustomScpExplorerForm::WorkspaceName()
  6372. {
  6373. return
  6374. DefaultStr(WinConfiguration->LastWorkspace,
  6375. DefaultStr(WinConfiguration->AutoWorkspace, LoadStr(NEW_WORKSPACE)));
  6376. }
  6377. //---------------------------------------------------------------------------
  6378. bool __fastcall TCustomScpExplorerForm::SaveWorkspace(bool EnableAutoSave)
  6379. {
  6380. std::unique_ptr<TObjectList> DataList(DoCollectWorkspace());
  6381. bool AnyNonStoredSessionWithPassword = false;
  6382. bool AnyNonStoredNonWorkspaceSessionWithPassword = false;
  6383. bool AllNonStoredSessionsAnonymous = true;
  6384. TTerminalManager * Manager = TTerminalManager::Instance();
  6385. for (int Index = 0; Index < DataList->Count; Index++)
  6386. {
  6387. TSessionData * SessionData = dynamic_cast<TSessionData *>(DataList->Items[Index]);
  6388. if (SessionData->Link.IsEmpty())
  6389. {
  6390. if (SessionData->HasAnySessionPassword())
  6391. {
  6392. AnyNonStoredSessionWithPassword = true;
  6393. if (!Manager->Sessions[Index]->SessionData->IsWorkspace)
  6394. {
  6395. AnyNonStoredNonWorkspaceSessionWithPassword = true;
  6396. }
  6397. }
  6398. if (!SameText(SessionData->UserName, AnonymousUserName))
  6399. {
  6400. AllNonStoredSessionsAnonymous = false;
  6401. }
  6402. }
  6403. }
  6404. bool SavePasswords;
  6405. bool * PSavePasswords;
  6406. bool NotRecommendedSavingPasswords =
  6407. !CustomWinConfiguration->UseMasterPassword &&
  6408. !AllNonStoredSessionsAnonymous;
  6409. if (Configuration->DisablePasswordStoring ||
  6410. !AnyNonStoredSessionWithPassword)
  6411. {
  6412. PSavePasswords = NULL;
  6413. SavePasswords = false;
  6414. }
  6415. else
  6416. {
  6417. PSavePasswords = &SavePasswords;
  6418. SavePasswords =
  6419. !AnyNonStoredNonWorkspaceSessionWithPassword ||
  6420. !NotRecommendedSavingPasswords;
  6421. }
  6422. UnicodeString Name = WorkspaceName();
  6423. bool CreateShortcut = false;
  6424. bool Result =
  6425. DoSaveWorkspaceDialog(
  6426. Name, PSavePasswords, NotRecommendedSavingPasswords, CreateShortcut,
  6427. EnableAutoSave);
  6428. if (Result)
  6429. {
  6430. DoSaveWorkspace(Name, DataList.get(), SavePasswords, true);
  6431. if (CreateShortcut)
  6432. {
  6433. TOperationVisualizer Visualizer;
  6434. UnicodeString AdditionalParams = TProgramParams::FormatSwitch(DESKTOP_SWITCH);
  6435. CreateDesktopSessionShortCut(Name, L"", AdditionalParams, -1, WORKSPACE_ICON);
  6436. }
  6437. if (EnableAutoSave)
  6438. {
  6439. WinConfiguration->AutoSaveWorkspace = true;
  6440. WinConfiguration->AutoWorkspace = Name;
  6441. if (PSavePasswords != NULL)
  6442. {
  6443. WinConfiguration->AutoSaveWorkspacePasswords = SavePasswords;
  6444. }
  6445. }
  6446. }
  6447. return Result;
  6448. }
  6449. //---------------------------------------------------------------------------
  6450. void __fastcall TCustomScpExplorerForm::UpdateSession(TManagedTerminal * Session)
  6451. {
  6452. DebugAssert(IsLocalBrowserMode() == Session->LocalBrowser);
  6453. DebugAssert(Session == ManagedSession);
  6454. if (!IsLocalBrowserMode())
  6455. {
  6456. SAFE_DESTROY(Session->RemoteExplorerState);
  6457. if (WinConfiguration->PreservePanelState)
  6458. {
  6459. Session->RemoteExplorerState = RemoteDirView->SaveState();
  6460. }
  6461. }
  6462. UpdateSessionData(Session->StateData);
  6463. }
  6464. //---------------------------------------------------------------------------
  6465. void __fastcall TCustomScpExplorerForm::UpdateSessionData(TSessionData * Data)
  6466. {
  6467. // Keep in sync with TSessionData::CopyStateData
  6468. DebugAssert(Data != NULL);
  6469. // This is inconsistent with how color (for example) is handled.
  6470. Data->Name = ManagedSession->SessionData->Name;
  6471. if (!IsLocalBrowserMode())
  6472. {
  6473. // cannot use RemoteDirView->Path, because it is empty if connection
  6474. // was already closed
  6475. // also only peek, we may not be connected at all atm
  6476. // so make sure we do not try retrieving current directory from the server
  6477. // (particularly with FTP)
  6478. UnicodeString ACurrentDirectory = Terminal->PeekCurrentDirectory();
  6479. if (!ACurrentDirectory.IsEmpty())
  6480. {
  6481. Data->RemoteDirectory = ACurrentDirectory;
  6482. }
  6483. }
  6484. Data->Color = SessionColor;
  6485. }
  6486. //---------------------------------------------------------------------------
  6487. void __fastcall TCustomScpExplorerForm::ToolBarResize(TObject *Sender)
  6488. {
  6489. TTBXToolbar * Toolbar = dynamic_cast<TTBXToolbar*>(Sender);
  6490. DebugAssert(Toolbar != NULL);
  6491. for (int i = 0; i < Toolbar->Items->Count; i++)
  6492. {
  6493. TTBXCustomDropDownItem * DropDownItem;
  6494. DropDownItem = dynamic_cast<TTBXCustomDropDownItem *>(Toolbar->Items->Items[i]);
  6495. if (DropDownItem != NULL)
  6496. {
  6497. ToolbarItemResize(DropDownItem,
  6498. Toolbar->Width - (Toolbar->View->BaseSize.x - DropDownItem->EditWidth) -
  6499. Toolbar->NonClientWidth);
  6500. break;
  6501. }
  6502. }
  6503. }
  6504. //---------------------------------------------------------------------------
  6505. void __fastcall TCustomScpExplorerForm::ToolbarItemResize(TTBXCustomDropDownItem * Item, int Width)
  6506. {
  6507. Item->EditWidth = Width;
  6508. }
  6509. //---------------------------------------------------------------------------
  6510. void __fastcall TCustomScpExplorerForm::ToolbarGetBaseSize(
  6511. TTBCustomToolbar * Toolbar, TPoint & ASize)
  6512. {
  6513. for (int i = 0; i < Toolbar->Items->Count; i++)
  6514. {
  6515. TTBXCustomDropDownItem * DropDownItem;
  6516. DropDownItem = dynamic_cast<TTBXCustomDropDownItem *>(Toolbar->Items->Items[i]);
  6517. if (DropDownItem != NULL)
  6518. {
  6519. ASize.x -= DropDownItem->EditWidth;
  6520. TControl * Control = this;
  6521. ASize.x += ScaleByTextHeight(Control, 50) /* minimal combo width */;
  6522. }
  6523. }
  6524. }
  6525. //---------------------------------------------------------------------------
  6526. void __fastcall TCustomScpExplorerForm::DoWarnLackOfTempSpace(
  6527. const UnicodeString Path, __int64 RequiredSpace, bool & Continue)
  6528. {
  6529. if (WinConfiguration->DDWarnLackOfTempSpace)
  6530. {
  6531. UnicodeString ADrive = ExtractFileDrive(ExpandFileName(Path));
  6532. if (!ADrive.IsEmpty())
  6533. {
  6534. __int64 FreeSpace = DiskFree((Byte)(ADrive[1]-'A'+1));
  6535. DebugAssert(RequiredSpace >= 0);
  6536. __int64 RequiredWithReserve;
  6537. RequiredWithReserve = (__int64)(RequiredSpace * WinConfiguration->DDWarnLackOfTempSpaceRatio);
  6538. if (FreeSpace < RequiredWithReserve)
  6539. {
  6540. unsigned int Result;
  6541. TMessageParams Params(mpNeverAskAgainCheck);
  6542. Result = MessageDialog(FMTLOAD(DD_WARN_LACK_OF_TEMP_SPACE, (Path,
  6543. FormatBytes(FreeSpace), FormatBytes(RequiredSpace))),
  6544. qtWarning, qaYes | qaNo, HELP_DD_WARN_LACK_OF_TEMP_SPACE, &Params);
  6545. if (Result == qaNeverAskAgain)
  6546. {
  6547. WinConfiguration->DDWarnLackOfTempSpace = false;
  6548. }
  6549. Continue = (Result == qaYes || Result == qaNeverAskAgain);
  6550. }
  6551. }
  6552. }
  6553. }
  6554. //---------------------------------------------------------------------------
  6555. void __fastcall TCustomScpExplorerForm::AddBookmark(TOperationSide Side)
  6556. {
  6557. DoOpenDirectoryDialog(odAddBookmark, Side);
  6558. }
  6559. //---------------------------------------------------------------------------
  6560. TStrings * __fastcall TCustomScpExplorerForm::CreateVisitedDirectories(TOperationSide Side)
  6561. {
  6562. // we should better use TCustomDirView::FCaseSensitive, but it is private
  6563. TStringList * VisitedDirectories = CreateSortedStringList(!IsSideLocalBrowser(Side));
  6564. try
  6565. {
  6566. TCustomDirView * DView = DirView(Side);
  6567. for (int Index = -DView->BackCount; Index <= DView->ForwardCount; Index++)
  6568. {
  6569. VisitedDirectories->Add(DView->HistoryPath[Index]);
  6570. }
  6571. }
  6572. catch (...)
  6573. {
  6574. delete VisitedDirectories;
  6575. throw;
  6576. }
  6577. return VisitedDirectories;
  6578. }
  6579. //---------------------------------------------------------------------------
  6580. void __fastcall TCustomScpExplorerForm::DoOpenDirectoryDialog(
  6581. TOpenDirectoryMode Mode, TOperationSide Side)
  6582. {
  6583. bool Continue = true;
  6584. if (Mode == odAddBookmark)
  6585. {
  6586. TMessageParams Params(mpNeverAskAgainCheck);
  6587. Params.NeverAskAgainTitle = LoadStr(ADD_BOOKMARK_SHARED);
  6588. Params.NeverAskAgainCheckedInitially = WinConfiguration->UseSharedBookmarks;
  6589. UnicodeString Message = MainInstructions(FMTLOAD(ADD_BOOKMARK_CONFIRM, (DirView(Side)->PathName)));
  6590. unsigned int Answer =
  6591. MessageDialog(Message,
  6592. qtConfirmation, qaOK | qaCancel, HELP_ADD_BOOKMARK_CONFIRM, &Params);
  6593. if (Answer == qaNeverAskAgain)
  6594. {
  6595. Continue = true;
  6596. WinConfiguration->UseSharedBookmarks = true;
  6597. }
  6598. else if (Answer == qaOK)
  6599. {
  6600. Continue = true;
  6601. WinConfiguration->UseSharedBookmarks = false;
  6602. }
  6603. else
  6604. {
  6605. Continue = false;
  6606. }
  6607. }
  6608. if (Continue)
  6609. {
  6610. TStrings * VisitedDirectories = CreateVisitedDirectories(Side);
  6611. try
  6612. {
  6613. UnicodeString Name = DirView(Side)->PathName;
  6614. bool Repeat;
  6615. do
  6616. {
  6617. Repeat = false;
  6618. TOperationSide BookmarkSide = IsSideLocalBrowser(Side) ? osLocal : osRemote;
  6619. if (::DoOpenDirectoryDialog(Mode, BookmarkSide, Name, VisitedDirectories, Terminal,
  6620. // do not allow switching to location profiles,
  6621. // if we are not connected
  6622. HasDirView[osLocal] && IsActiveTerminal(Terminal)))
  6623. {
  6624. TWindowLock Lock(this);
  6625. if (!TryOpenDirectory(Side, Name))
  6626. {
  6627. Repeat = true;
  6628. Mode = odBrowse;
  6629. }
  6630. }
  6631. }
  6632. while (Repeat);
  6633. }
  6634. __finally
  6635. {
  6636. delete VisitedDirectories;
  6637. }
  6638. }
  6639. }
  6640. //---------------------------------------------------------------------------
  6641. bool __fastcall TCustomScpExplorerForm::CommandSessionFallback()
  6642. {
  6643. DebugAssert(!Terminal->CommandSessionOpened);
  6644. return TTerminalManager::Instance()->ConnectTerminal(Terminal->CommandSession);
  6645. }
  6646. //---------------------------------------------------------------------------
  6647. bool __fastcall TCustomScpExplorerForm::EnsureCommandSessionFallback(TFSCapability Capability)
  6648. {
  6649. bool Result = Terminal->IsCapable[Capability] ||
  6650. Terminal->CommandSessionOpened;
  6651. if (!Result)
  6652. {
  6653. // We might get here for example when "checksum" synchronization is requested from command-line
  6654. // on server that supports neither checksum nor shell
  6655. if (!Terminal->IsCapable[fcSecondaryShell])
  6656. {
  6657. NotSupported();
  6658. }
  6659. else if (!GUIConfiguration->ConfirmCommandSession)
  6660. {
  6661. Result = true;
  6662. }
  6663. else
  6664. {
  6665. TMessageParams Params(mpNeverAskAgainCheck);
  6666. const TFileSystemInfo & FileSystemInfo = Terminal->GetFileSystemInfo();
  6667. unsigned int Answer = MessageDialog(FMTLOAD(PERFORM_ON_COMMAND_SESSION2,
  6668. (FileSystemInfo.ProtocolName, FileSystemInfo.ProtocolName)), qtConfirmation,
  6669. qaOK | qaCancel, HELP_PERFORM_ON_COMMAND_SESSION, &Params);
  6670. if (Answer == qaNeverAskAgain)
  6671. {
  6672. GUIConfiguration->ConfirmCommandSession = false;
  6673. Result = true;
  6674. }
  6675. else if (Answer == qaOK)
  6676. {
  6677. Result = true;
  6678. }
  6679. }
  6680. if (Result)
  6681. {
  6682. Result = CommandSessionFallback();
  6683. }
  6684. }
  6685. return Result;
  6686. }
  6687. //---------------------------------------------------------------------------
  6688. void __fastcall TCustomScpExplorerForm::OpenConsole(UnicodeString Command)
  6689. {
  6690. if (EnsureCommandSessionFallback(fcAnyCommand))
  6691. {
  6692. DoConsoleDialog(Terminal, Command);
  6693. }
  6694. }
  6695. //---------------------------------------------------------------------------
  6696. void __fastcall TCustomScpExplorerForm::FileControlDDDragEnter(
  6697. TObject *Sender, _di_IDataObject /*DataObj*/, int /*grfKeyState*/,
  6698. const TPoint & /*Point*/, int & /*dwEffect*/, bool & Accept)
  6699. {
  6700. if (!IsSideLocalBrowser(osRemote) &&
  6701. IsFileControl(DropSourceControl, osRemote) &&
  6702. (FDDExtMapFile != NULL))
  6703. {
  6704. Accept = true;
  6705. }
  6706. FDDTargetControl = dynamic_cast<TControl*>(Sender);
  6707. DebugAssert(FDDTargetControl != NULL);
  6708. }
  6709. //---------------------------------------------------------------------------
  6710. void __fastcall TCustomScpExplorerForm::SessionsDDDragEnter(
  6711. _di_IDataObject DataObj, int KeyState,
  6712. const TPoint & Point, int & Effect, bool & Accept)
  6713. {
  6714. FileControlDDDragEnter(SessionsPageControl, DataObj, KeyState, Point, Effect, Accept);
  6715. }
  6716. //---------------------------------------------------------------------------
  6717. void __fastcall TCustomScpExplorerForm::QueueDDDragEnter(
  6718. _di_IDataObject DataObj, int KeyState,
  6719. const TPoint & Point, int & Effect, bool & Accept)
  6720. {
  6721. FileControlDDDragEnter(QueueView3, DataObj, KeyState, Point, Effect, Accept);
  6722. }
  6723. //---------------------------------------------------------------------------
  6724. void __fastcall TCustomScpExplorerForm::FileControlDDDragLeave(
  6725. TObject * Sender)
  6726. {
  6727. DebugUsedParam(Sender);
  6728. DebugAssert(FDDTargetControl == Sender);
  6729. FDDTargetControl = NULL;
  6730. }
  6731. //---------------------------------------------------------------------------
  6732. void __fastcall TCustomScpExplorerForm::SessionsDDDragLeave(int)
  6733. {
  6734. FileControlDDDragLeave(SessionsPageControl);
  6735. }
  6736. //---------------------------------------------------------------------------
  6737. void __fastcall TCustomScpExplorerForm::QueueDDDragLeave(int)
  6738. {
  6739. FileControlDDDragLeave(QueueView3);
  6740. }
  6741. //---------------------------------------------------------------------------
  6742. void __fastcall TCustomScpExplorerForm::AddEditLink(TOperationSide Side, bool Add)
  6743. {
  6744. DebugAssert(!IsSideLocalBrowser(Side));
  6745. DebugUsedParam(Side);
  6746. bool Edit = false;
  6747. TRemoteFile * File = NULL;
  6748. UnicodeString FileName;
  6749. UnicodeString PointTo;
  6750. bool SymbolicLink = true;
  6751. if (RemoteDirView->ItemFocused)
  6752. {
  6753. DebugAssert(RemoteDirView->ItemFocused->Data);
  6754. File = (TRemoteFile *)RemoteDirView->ItemFocused->Data;
  6755. Edit = !Add && File->IsSymLink && Terminal->SessionData->ResolveSymlinks;
  6756. if (Edit)
  6757. {
  6758. FileName = File->FileName;
  6759. PointTo = File->LinkTo;
  6760. }
  6761. else if (!File->IsParentDirectory)
  6762. {
  6763. PointTo = File->FileName;
  6764. }
  6765. }
  6766. if (DoSymlinkDialog(FileName, PointTo, osRemote, SymbolicLink, Edit,
  6767. Terminal->IsCapable[fcHardLink]))
  6768. {
  6769. Configuration->Usage->Inc(L"RemoteLinksCreated");
  6770. if (Edit)
  6771. {
  6772. DebugAssert(File->FileName == FileName);
  6773. int Params = dfNoRecursive;
  6774. Terminal->ExceptionOnFail = true;
  6775. try
  6776. {
  6777. Terminal->DeleteFile(L"", File, &Params);
  6778. }
  6779. __finally
  6780. {
  6781. Terminal->ExceptionOnFail = false;
  6782. }
  6783. }
  6784. Terminal->CreateLink(FileName, PointTo, SymbolicLink);
  6785. }
  6786. }
  6787. //---------------------------------------------------------------------------
  6788. bool __fastcall TCustomScpExplorerForm::CanAddEditLink(TOperationSide Side)
  6789. {
  6790. return
  6791. (IsSideLocalBrowser(Side) ||
  6792. (HasActiveTerminal() &&
  6793. Terminal->ResolvingSymlinks &&
  6794. Terminal->IsCapable[fcSymbolicLink]));
  6795. }
  6796. //---------------------------------------------------------------------------
  6797. bool __fastcall TCustomScpExplorerForm::LinkFocused()
  6798. {
  6799. return
  6800. !IsSideLocalBrowser(FCurrentSide) &&
  6801. (RemoteDirView->ItemFocused != NULL) &&
  6802. ((TRemoteFile *)RemoteDirView->ItemFocused->Data)->IsSymLink &&
  6803. Terminal->SessionData->ResolveSymlinks;
  6804. }
  6805. //---------------------------------------------------------------------------
  6806. void __fastcall TCustomScpExplorerForm::ExecuteCurrentFile()
  6807. {
  6808. DebugAssert(!WinConfiguration->DisableOpenEdit);
  6809. FForceExecution = true;
  6810. try
  6811. {
  6812. DirView(osCurrent)->ExecuteCurrentFile();
  6813. }
  6814. __finally
  6815. {
  6816. FForceExecution = false;
  6817. }
  6818. }
  6819. //---------------------------------------------------------------------------
  6820. void __fastcall TCustomScpExplorerForm::ExecuteCurrentFileWith(bool OnFocused)
  6821. {
  6822. TEditorData ExternalEditor;
  6823. ExternalEditor.Editor = edExternal;
  6824. bool Remember = false;
  6825. if (DoEditorPreferencesDialog(&ExternalEditor, Remember, epmAdHoc, !IsSideLocalBrowser(osCurrent)))
  6826. {
  6827. if (Remember)
  6828. {
  6829. TEditorList * EditorList = new TEditorList;
  6830. try
  6831. {
  6832. *EditorList = *WinConfiguration->EditorList;
  6833. bool Found = false;
  6834. int i = 0;
  6835. while (!Found && (i < EditorList->Count))
  6836. {
  6837. const TEditorPreferences * Editor = EditorList->Editors[i];
  6838. if ((Editor->Data->Editor == edExternal) &&
  6839. (Editor->Data->ExternalEditor == ExternalEditor.ExternalEditor))
  6840. {
  6841. Found = true;
  6842. }
  6843. i++;
  6844. }
  6845. if (!Found)
  6846. {
  6847. EditorList->Add(new TEditorPreferences(ExternalEditor));
  6848. WinConfiguration->EditorList = EditorList;
  6849. }
  6850. }
  6851. __finally
  6852. {
  6853. delete EditorList;
  6854. }
  6855. }
  6856. ExecuteFile(osCurrent, efExternalEditor, &ExternalEditor, true, OnFocused);
  6857. }
  6858. }
  6859. //---------------------------------------------------------------------------
  6860. void __fastcall TCustomScpExplorerForm::DetachTerminal(TObject * ATerminal)
  6861. {
  6862. if (FFileFindTerminal == ATerminal)
  6863. {
  6864. FFileFindTerminal = NULL;
  6865. HideFileFindDialog();
  6866. }
  6867. if (FClipboardTerminal == ATerminal)
  6868. {
  6869. ClipboardClear(); // implies ClipboardStop
  6870. }
  6871. if ((FOnSynchronizeAbort != NULL) &&
  6872. (ATerminal == FManagedSession))
  6873. {
  6874. FOnSynchronizeAbort(NULL);
  6875. }
  6876. }
  6877. //---------------------------------------------------------------------------
  6878. void __fastcall TCustomScpExplorerForm::TerminalRemoved(TObject * Sender)
  6879. {
  6880. FEditorManager->ProcessFiles(FileTerminalRemoved, Sender);
  6881. DetachTerminal(Sender);
  6882. }
  6883. //---------------------------------------------------------------------------
  6884. void __fastcall TCustomScpExplorerForm::FileTerminalReplaced(
  6885. const UnicodeString FileName, TEditedFileData * Data, TObject * /*Token*/, void * Arg)
  6886. {
  6887. TManagedTerminal * ATerminal = static_cast<TManagedTerminal *>(Arg);
  6888. if (Data->Terminal == Terminal)
  6889. {
  6890. Data->Terminal = ATerminal;
  6891. }
  6892. }
  6893. //---------------------------------------------------------------------------
  6894. void __fastcall TCustomScpExplorerForm::FileTerminalRemoved(const UnicodeString FileName,
  6895. TEditedFileData * Data, TObject * /*Token*/, void * Arg)
  6896. {
  6897. TTerminal * Terminal = static_cast<TTerminal *>(Arg);
  6898. DebugAssert(Terminal != NULL);
  6899. if (Data->Terminal == Terminal)
  6900. {
  6901. Data->Terminal = NULL;
  6902. }
  6903. }
  6904. //---------------------------------------------------------------------------
  6905. TManagedTerminal * TCustomScpExplorerForm::GetReplacementForLastSession()
  6906. {
  6907. return NULL;
  6908. }
  6909. //---------------------------------------------------------------------------
  6910. void __fastcall TCustomScpExplorerForm::LastTerminalClosed()
  6911. {
  6912. UpdateControls();
  6913. SessionColor = TColor(0);
  6914. UpdateRemotePathComboBox(false);
  6915. NeedSession(false);
  6916. }
  6917. //---------------------------------------------------------------------------
  6918. void __fastcall TCustomScpExplorerForm::NeedSession(bool Startup)
  6919. {
  6920. try
  6921. {
  6922. // Cache, as the login dialog can change its value.
  6923. // See also DoShow. Also see TTerminalManager::NewSession.
  6924. bool ShowLogin = WinConfiguration->ShowLoginWhenNoSession;
  6925. try
  6926. {
  6927. if (ShowLogin)
  6928. {
  6929. bool ReloadSessions = !Startup;
  6930. TTerminalManager::Instance()->NewSession(L"", ReloadSessions, this, true);
  6931. }
  6932. }
  6933. __finally
  6934. {
  6935. // Do not terminate, if we are only starting up and we are not showing Login dialog
  6936. // (so there was no chance for the user to open any session yet)
  6937. if ((ShowLogin || !Startup) &&
  6938. !WinConfiguration->KeepOpenWhenNoSession &&
  6939. // this rules out the implicit Commander's local-local browser
  6940. ((Terminal == NULL) || (!Terminal->Active && !Terminal->Permanent)))
  6941. {
  6942. TerminateApplication();
  6943. }
  6944. }
  6945. }
  6946. catch (EAbort &)
  6947. {
  6948. // swallow
  6949. // The TTerminalManager does not expect the OnLastTerminalClose to throw without trying to connect.
  6950. // Also when called from TWinControl.UpdateShowing => CMShowingChanged, the showing is aborted on exception
  6951. }
  6952. }
  6953. //---------------------------------------------------------------------------
  6954. void __fastcall TCustomScpExplorerForm::SessionListChanged(bool ForceTruncationUpdate)
  6955. {
  6956. TTerminalManager * Manager = TTerminalManager::Instance();
  6957. DebugAssert(!Manager->Updating);
  6958. TAutoFlag UpdatingSessionTabsFlag(FUpdatingSessionTabs);
  6959. int ActiveSessionIndex = Manager->ActiveSessionIndex;
  6960. Configuration->Usage->SetMax(L"MaxOpenedSessions", Manager->Count);
  6961. SessionsPageControl->LockDrawing();
  6962. try
  6963. {
  6964. int PrevTabsWidth = SessionsPageControl->TotalTabsWidth();
  6965. while (SessionsPageControl->PageCount > Manager->Count + 1)
  6966. {
  6967. delete SessionsPageControl->Pages[SessionsPageControl->PageCount - 1];
  6968. }
  6969. for (int Index = 0; Index <= Manager->Count; Index++)
  6970. {
  6971. TThemeTabSheet * TabSheet;
  6972. if (Index >= SessionsPageControl->PageCount)
  6973. {
  6974. TabSheet = new TThemeTabSheet(SessionsPageControl);
  6975. TabSheet->PageControl = SessionsPageControl;
  6976. }
  6977. else
  6978. {
  6979. TabSheet = SessionsPageControl->Pages[Index];
  6980. }
  6981. bool IsSessionTab = (Index < Manager->Count);
  6982. if (IsSessionTab)
  6983. {
  6984. TTerminal * Terminal = Manager->Sessions[Index];
  6985. TabSheet->Tag = reinterpret_cast<int>(Terminal);
  6986. TabSheet->CaptionTruncation = WinConfiguration->SessionTabCaptionTruncation ? tttEllipsis : tttNone;
  6987. UpdateSessionTab(TabSheet);
  6988. }
  6989. else
  6990. {
  6991. TabSheet->Tag = 0; // not really needed
  6992. TabSheet->Shadowed = false;
  6993. TabSheet->Button = SupportsLocalBrowser() ? ttbDropDown : ttbNone;
  6994. TabSheet->CaptionTruncation = GetNewTabTabCaptionTruncation();
  6995. // 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)
  6996. UpdateNewTabTab();
  6997. }
  6998. }
  6999. // Prevent flicker on the right side where scrolling buttons would be, unless really needed
  7000. if (ForceTruncationUpdate || (SessionsPageControl->TotalTabsWidth() != PrevTabsWidth))
  7001. {
  7002. SessionsPageControl->UpdateTabsCaptionTruncation();
  7003. }
  7004. }
  7005. __finally
  7006. {
  7007. SessionsPageControl->UnlockDrawing();
  7008. }
  7009. SessionsPageControl->ActivePageIndex = ActiveSessionIndex;
  7010. }
  7011. //---------------------------------------------------------------------------
  7012. TThemeTabCaptionTruncation TCustomScpExplorerForm::GetNewTabTabCaptionTruncation()
  7013. {
  7014. return WinConfiguration->SessionTabCaptionTruncation ? tttNoText : tttNone;
  7015. }
  7016. //---------------------------------------------------------------------------
  7017. UnicodeString TCustomScpExplorerForm::GetNewTabTabCaption()
  7018. {
  7019. return StripHotkey(StripTrailingPunctuation(NonVisualDataModule->NewTabAction->Caption));
  7020. }
  7021. //---------------------------------------------------------------------------
  7022. TThemeTabSheet * TCustomScpExplorerForm::GetNewTabTab()
  7023. {
  7024. return SessionsPageControl->Pages[SessionsPageControl->PageCount - 1];
  7025. }
  7026. //---------------------------------------------------------------------------
  7027. void __fastcall TCustomScpExplorerForm::UpdateNewTabTab()
  7028. {
  7029. TThemeTabSheet * TabSheet = GetNewTabTab();
  7030. UnicodeString TabCaption;
  7031. if (WinConfiguration->SelectiveToolbarText)
  7032. {
  7033. TabCaption = GetNewTabTabCaption();
  7034. }
  7035. TabSheet->BaseCaption = TabCaption;
  7036. TabSheet->ImageIndex = GetNewTabTabImageIndex(osCurrent);
  7037. }
  7038. //---------------------------------------------------------------------------
  7039. TManagedTerminal * __fastcall TCustomScpExplorerForm::GetSessionTabSession(TTabSheet * TabSheet)
  7040. {
  7041. return reinterpret_cast<TManagedTerminal *>(TabSheet->Tag);
  7042. }
  7043. //---------------------------------------------------------------------------
  7044. UnicodeString TCustomScpExplorerForm::GetLocalBrowserSessionTitle(TManagedTerminal *)
  7045. {
  7046. DebugFail();
  7047. return UnicodeString();
  7048. }
  7049. //---------------------------------------------------------------------------
  7050. void TCustomScpExplorerForm::UpdateSessionTab(TThemeTabSheet * TabSheet)
  7051. {
  7052. if (DebugAlwaysTrue(TabSheet != NULL))
  7053. {
  7054. TManagedTerminal * ASession = GetSessionTabSession(TabSheet);
  7055. if (DebugAlwaysTrue(ASession != NULL))
  7056. {
  7057. TColor Color = (ASession == Terminal) ? FSessionColor : ASession->StateData->Color;
  7058. if (ASession->LocalBrowser)
  7059. {
  7060. TabSheet->ImageIndex = FLocalBrowserTabImageIndex;
  7061. }
  7062. else
  7063. {
  7064. TabSheet->ImageIndex = AddSessionColor(Color);
  7065. }
  7066. TTerminalManager * Manager = TTerminalManager::Instance();
  7067. TabSheet->Shadowed = !ASession->Active && !ASession->LocalBrowser;
  7068. TabSheet->Button = CanCloseSession(ASession) ? ttbClose : ttbNone;
  7069. TabSheet->BaseCaption = Manager->GetSessionTitle(ASession, true);
  7070. }
  7071. }
  7072. }
  7073. //---------------------------------------------------------------------------
  7074. bool TCustomScpExplorerForm::CanCloseSession(TManagedTerminal * Session)
  7075. {
  7076. return !Session->LocalBrowser || (TTerminalManager::Instance()->Count > 1);
  7077. }
  7078. //---------------------------------------------------------------------------
  7079. bool __fastcall TCustomScpExplorerForm::SessionTabSwitched()
  7080. {
  7081. DebugAssert(SessionsPageControl->ActivePage != NULL);
  7082. TManagedTerminal * Session = GetSessionTabSession(SessionsPageControl->ActivePage);
  7083. bool Result = (Session != NULL);
  7084. if (Result)
  7085. {
  7086. TTerminalManager::Instance()->ActiveSession = Session;
  7087. }
  7088. else
  7089. {
  7090. try
  7091. {
  7092. NewTab();
  7093. }
  7094. __finally
  7095. {
  7096. SessionListChanged();
  7097. }
  7098. FSessionsPageControlNewTabTime = Now();
  7099. }
  7100. return Result;
  7101. }
  7102. //---------------------------------------------------------------------------
  7103. void __fastcall TCustomScpExplorerForm::SessionsPageControlChange(TObject * /*Sender*/)
  7104. {
  7105. TWindowLock Lock(this);
  7106. SessionTabSwitched();
  7107. }
  7108. //---------------------------------------------------------------------------
  7109. void __fastcall TCustomScpExplorerForm::TransferListChange(TObject * Sender)
  7110. {
  7111. TTBXStringList * TransferList = dynamic_cast<TTBXStringList *>(Sender);
  7112. DebugAssert(TransferList != NULL);
  7113. UnicodeString Name;
  7114. if (TransferList->ItemIndex <= 0)
  7115. {
  7116. Name = L"";
  7117. }
  7118. else
  7119. {
  7120. Name = GUIConfiguration->CopyParamList->Names[TransferList->ItemIndex - 1];
  7121. }
  7122. if (FCopyParamAutoSelected.IsEmpty())
  7123. {
  7124. // if previous preset was not autoselected, make new preset the "default"
  7125. FCopyParamDefault = Name;
  7126. }
  7127. GUIConfiguration->CopyParamCurrent = Name;
  7128. }
  7129. //---------------------------------------------------------------------------
  7130. void __fastcall TCustomScpExplorerForm::UpdateTransferLabel()
  7131. {
  7132. // sanity check
  7133. bool ExistingPreset =
  7134. (FTransferListHoverIndex >= 0) &&
  7135. (FTransferListHoverIndex < 1 + GUIConfiguration->CopyParamList->Count);
  7136. DebugAssert(ExistingPreset);
  7137. if (ExistingPreset)
  7138. {
  7139. HDC DC = GetDC(0);
  7140. TCanvas * Canvas = new TCanvas();
  7141. try
  7142. {
  7143. Canvas->Handle = DC;
  7144. Canvas->Font = GetToolbarFont(this);
  7145. UnicodeString Name;
  7146. if (FTransferListHoverIndex == 0)
  7147. {
  7148. Name = L"";
  7149. }
  7150. else
  7151. {
  7152. Name = GUIConfiguration->CopyParamList->Names[FTransferListHoverIndex - 1];
  7153. }
  7154. TTBXLabelItem * TransferLabel = dynamic_cast<TTBXLabelItem*>(
  7155. static_cast<TComponent*>(GetComponent(fcTransferLabel)));
  7156. TTBXStringList * TransferList = dynamic_cast<TTBXStringList*>(
  7157. static_cast<TObject*>(GetComponent(fcTransferList)));
  7158. UnicodeString InfoStr =
  7159. GUIConfiguration->CopyParamPreset[Name].
  7160. GetInfoStr(L"; ",
  7161. FLAGMASK(HasActiveTerminal(), Terminal->UsableCopyParamAttrs(0).General));
  7162. int MaxWidth = TransferList->MinWidth - (2 * TransferLabel->Margin) - ScaleByTextHeight(this, 10);
  7163. if (Canvas->TextExtent(InfoStr).cx > MaxWidth)
  7164. {
  7165. while (Canvas->TextExtent(InfoStr + Ellipsis).cx > MaxWidth)
  7166. {
  7167. InfoStr.SetLength(InfoStr.Length() - 1);
  7168. }
  7169. InfoStr += Ellipsis;
  7170. }
  7171. // UpdateCaption does not cause invalidation of whole submenu, while
  7172. // setting Caption property does.
  7173. // also it probably does not resize the label, even if necessary
  7174. // (we do not want that anyway)
  7175. TransferLabel->UpdateCaption(InfoStr);
  7176. }
  7177. __finally
  7178. {
  7179. Canvas->Handle = NULL;
  7180. ReleaseDC(0, DC);
  7181. delete Canvas;
  7182. }
  7183. }
  7184. }
  7185. //---------------------------------------------------------------------------
  7186. void __fastcall TCustomScpExplorerForm::TransferListDrawItem(
  7187. TTBXCustomList * /*Sender*/, TCanvas * /*ACanvas*/, const TRect & /*ARect*/,
  7188. int /*AIndex*/, int AHoverIndex, bool & /*DrawDefault*/)
  7189. {
  7190. if (FTransferListHoverIndex != AHoverIndex)
  7191. {
  7192. FTransferListHoverIndex = AHoverIndex;
  7193. UpdateTransferLabel();
  7194. }
  7195. }
  7196. //---------------------------------------------------------------------------
  7197. void __fastcall TCustomScpExplorerForm::WMAppCommand(TMessage & Message)
  7198. {
  7199. int Command = GET_APPCOMMAND_LPARAM(Message.LParam);
  7200. TShiftState Shift = KeyDataToShiftState(GET_KEYSTATE_LPARAM(Message.LParam));
  7201. if ((Shift * (TShiftState() << ssShift << ssAlt << ssCtrl)).Empty())
  7202. {
  7203. if (Command == APPCOMMAND_BROWSER_FAVORITES)
  7204. {
  7205. if (!NonVisualDataModule->Busy)
  7206. {
  7207. OpenDirectory(GetSide(osCurrent));
  7208. }
  7209. Message.Result = 1;
  7210. }
  7211. else
  7212. {
  7213. TForm::Dispatch(&Message);
  7214. }
  7215. }
  7216. else
  7217. {
  7218. TForm::Dispatch(&Message);
  7219. }
  7220. }
  7221. //---------------------------------------------------------------------------
  7222. void __fastcall TCustomScpExplorerForm::CMDialogChar(TMessage & AMessage)
  7223. {
  7224. TCMDialogChar & Message = reinterpret_cast<TCMDialogChar &>(AMessage);
  7225. if ((FIgnoreNextDialogChar != 0) &&
  7226. (towupper(Message.CharCode) == towupper(FIgnoreNextDialogChar)))
  7227. {
  7228. Message.Result = 1;
  7229. }
  7230. else
  7231. {
  7232. TForm::Dispatch(&Message);
  7233. }
  7234. FIgnoreNextDialogChar = 0;
  7235. }
  7236. //---------------------------------------------------------------------------
  7237. void __fastcall TCustomScpExplorerForm::WMSysCommand(TMessage & Message)
  7238. {
  7239. // The four low-order bits are used internally by Windows
  7240. unsigned int Cmd = (Message.WParam & 0xFFF0);
  7241. // SC_RESTORE, SC_MAXIMIZE, SC_MINIMIZE - buttons on windows title
  7242. // SC_DEFAULT - double click on windows title (does not work, at least on WinXP)
  7243. // 61730 - restore thru double click - undocumented
  7244. // 61490 - maximize thru double click - undocumented
  7245. if ((Cmd == SC_RESTORE) || (Cmd == SC_MAXIMIZE) ||
  7246. (Cmd == SC_MINIMIZE) || (Cmd == SC_DEFAULT))
  7247. {
  7248. SysResizing(Cmd);
  7249. }
  7250. TForm::Dispatch(&Message);
  7251. }
  7252. //---------------------------------------------------------------------------
  7253. void __fastcall TCustomScpExplorerForm::WMQueryEndSession(TMessage & Message)
  7254. {
  7255. AppLog(L"Windows session end query");
  7256. // We were actually never able to make ENDSESSION_CRITICAL happen.
  7257. // Also there no point returning TRUE as we are not able to
  7258. // handle the abrupt termination caused by subsequent WM_ENDSESSION cleanly.
  7259. // Hence the process termination might be safer :)
  7260. if ((Message.LParam != ENDSESSION_CRITICAL) &&
  7261. (((FQueue != NULL) && !FQueue->IsEmpty) || (FProgressForm != NULL)))
  7262. {
  7263. Message.Result = FALSE;
  7264. }
  7265. else
  7266. {
  7267. Message.Result = TRUE;
  7268. }
  7269. // Do not call default handling as that triggers OnCloseQuery,
  7270. // where our implementation will popup confirmation dialogs, what we do not want,
  7271. // as per Vista guidelines:
  7272. // https://learn.microsoft.com/en-us/windows/win32/shutdown/shutting-down
  7273. }
  7274. //---------------------------------------------------------------------------
  7275. void __fastcall TCustomScpExplorerForm::WMEndSession(TWMEndSession & Message)
  7276. {
  7277. AppLog(L"Windows session end");
  7278. if (Message.EndSession && IsApplicationMinimized())
  7279. {
  7280. // WORKAROUND
  7281. // TApplication.WndProc() calls Application.Terminate() before Halt(),
  7282. // when it receives WM_ENDSESSION,
  7283. // but that sometimes (particularly when application is minimized) cause crashes.
  7284. // Crash popups message that blocks log off.
  7285. // Obviously application cannot shutdown cleanly after WM_ENDSESSION,
  7286. // so we call ExitProcess() immediately, not even trying to cleanup.
  7287. // It still causes beep, so there's likely some popup, but it does not block
  7288. // log off.
  7289. ExitProcess(0);
  7290. }
  7291. TForm::Dispatch(&Message);
  7292. }
  7293. #ifdef _DEBUG
  7294. //---------------------------------------------------------------------------
  7295. void __fastcall TCustomScpExplorerForm::WMWindowPosChanged(TWMWindowPosMsg & Message)
  7296. {
  7297. TForm::Dispatch(&Message);
  7298. }
  7299. #endif
  7300. //---------------------------------------------------------------------------
  7301. void __fastcall TCustomScpExplorerForm::SysResizing(unsigned int /*Cmd*/)
  7302. {
  7303. }
  7304. //---------------------------------------------------------------------------
  7305. void __fastcall TCustomScpExplorerForm::DoShow()
  7306. {
  7307. // Called from TCustomScpExplorerForm::CMShowingChanged
  7308. // only now are the controls resized finally, so the size constraints
  7309. // will not conflict with possibly very small window size
  7310. RestoreFormParams();
  7311. // This indicates that the form was rescaled after parameters were loaded and
  7312. // as most probably the parameters were actually saved with the current form scaling
  7313. // (windows was the last time likely closed on the same monitor, where it is starting now),
  7314. // rescaling happened twice already. Reload the parameters, as now they likely won't need any scaling
  7315. // and no rounding problems will occur.
  7316. // See also RestoreForm.
  7317. // (we should be safe to call RestoreParams unconditionally)
  7318. if (PixelsPerInch != Screen->PixelsPerInch)
  7319. {
  7320. RestoreParams();
  7321. }
  7322. FixControlsPlacement();
  7323. if (Position == poDefaultPosOnly)
  7324. {
  7325. CutFormToDesktop(this);
  7326. }
  7327. TForm::DoShow();
  7328. FSessionsDragDropFilesEx->DragDropControl = SessionsPageControl;
  7329. FQueueDragDropFilesEx->DragDropControl = QueueView3;
  7330. // This was previously in NeedSession, where other related code is.
  7331. // But that is called from CMShowingChanged, when the window is already visible and the local-local window state
  7332. // unpleasantly shows briefly.
  7333. if ((ManagedSession == NULL) &&
  7334. !WinConfiguration->ShowLoginWhenNoSession &&
  7335. WinConfiguration->AutoSaveWorkspace && !WinConfiguration->AutoWorkspace.IsEmpty() &&
  7336. // This detects if workspace was saved the last time the main widow was closed
  7337. SameText(WinConfiguration->LastStoredSession, WinConfiguration->AutoWorkspace))
  7338. {
  7339. DoOpenFolderOrWorkspace(WinConfiguration->AutoWorkspace, false, true);
  7340. Configuration->Usage->Inc(L"OpenedWorkspacesAuto");
  7341. }
  7342. // Before main window displays
  7343. if (ManagedSession == NULL)
  7344. {
  7345. StartingWithoutSession();
  7346. }
  7347. UpdatePixelsPerInchMainWindowCounter();
  7348. FShowing = true;
  7349. }
  7350. //---------------------------------------------------------------------------
  7351. void __fastcall TCustomScpExplorerForm::UpdatePixelsPerInchMainWindowCounter()
  7352. {
  7353. Configuration->Usage->Set(L"PixelsPerInchMainWindow", PixelsPerInch);
  7354. }
  7355. //---------------------------------------------------------------------------
  7356. void __fastcall TCustomScpExplorerForm::StartingWithoutSession()
  7357. {
  7358. SessionListChanged();
  7359. InitStatusBar();
  7360. UpdateControls();
  7361. }
  7362. //---------------------------------------------------------------------------
  7363. void __fastcall TCustomScpExplorerForm::PopupTrayBalloon(TTerminal * Terminal,
  7364. const UnicodeString & Str, TQueryType Type, Exception * E, unsigned int Seconds,
  7365. TNotifyEvent OnBalloonClick, TObject * UserData)
  7366. {
  7367. bool Do;
  7368. UnicodeString Message;
  7369. if (E == NULL)
  7370. {
  7371. Message = Str;
  7372. Do = true;
  7373. }
  7374. else
  7375. {
  7376. Do = ExceptionMessage(E, Message);
  7377. }
  7378. if (Do && WinConfiguration->BalloonNotifications)
  7379. {
  7380. UnicodeString Title;
  7381. if ((Terminal == NULL) && (Type == qtInformation) && ExtractMainInstructions(Message, Title))
  7382. {
  7383. Message = TrimLeft(Message);
  7384. }
  7385. else
  7386. {
  7387. Message = UnformatMessage(Message);
  7388. const ResourceString * Captions[] = { &_SMsgDlgConfirm, &_SMsgDlgWarning,
  7389. &_SMsgDlgError, &_SMsgDlgInformation, NULL };
  7390. Title = LoadResourceString(Captions[Type]);
  7391. if (Terminal != NULL)
  7392. {
  7393. TManagedTerminal * Session = DebugNotNull(dynamic_cast<TManagedTerminal *>(Terminal->PrimaryTerminal));
  7394. Title = TTerminalManager::Instance()->GetSessionTitle(Session, true) + TitleSeparator + Title;
  7395. }
  7396. }
  7397. if (Seconds == 0)
  7398. {
  7399. Seconds = WinConfiguration->NotificationsTimeout;
  7400. }
  7401. FTrayIcon->PopupBalloon(Title, Message, Type, Seconds * MSecsPerSec, OnBalloonClick, UserData);
  7402. }
  7403. }
  7404. //---------------------------------------------------------------------------
  7405. unsigned int __fastcall TCustomScpExplorerForm::MoreMessageDialog(const UnicodeString Message,
  7406. TStrings * MoreMessages, TQueryType Type, unsigned int Answers,
  7407. UnicodeString HelpKeyword, const TMessageParams * Params,
  7408. TTerminal * Terminal)
  7409. {
  7410. if (((WinConfiguration->ContinueOnError && (FErrorList != NULL)) ||
  7411. (FOnFeedSynchronizeError != NULL)) &&
  7412. (Params != NULL) && (Params->Params & mpAllowContinueOnError) )
  7413. {
  7414. if (FOnFeedSynchronizeError != NULL)
  7415. {
  7416. FOnFeedSynchronizeError(Message, MoreMessages, Type, HelpKeyword);
  7417. }
  7418. else
  7419. {
  7420. DebugAssert(FErrorList != NULL);
  7421. TStringList * MoreMessagesCopy = NULL;
  7422. if (MoreMessages)
  7423. {
  7424. MoreMessagesCopy = new TStringList();
  7425. MoreMessagesCopy->Assign(MoreMessages);
  7426. }
  7427. FErrorList->AddObject(Message, MoreMessagesCopy);
  7428. }
  7429. PopupTrayBalloon(Terminal, Message, Type);
  7430. return ContinueAnswer(Answers);
  7431. }
  7432. else
  7433. {
  7434. bool UseBalloon = FTrayIcon->Visible;
  7435. if (UseBalloon)
  7436. {
  7437. PopupTrayBalloon(Terminal, Message, Type);
  7438. }
  7439. unsigned int Result;
  7440. try
  7441. {
  7442. Result = ::MoreMessageDialog(Message, MoreMessages, Type, Answers, HelpKeyword, Params);
  7443. }
  7444. __finally
  7445. {
  7446. // cancel only balloon we popped up, otherwise we may cancel notification
  7447. // balloon that was there before the message dialog
  7448. // (such as "dd confirmation opt in balloon)
  7449. if (UseBalloon)
  7450. {
  7451. FTrayIcon->CancelBalloon();
  7452. }
  7453. }
  7454. return Result;
  7455. }
  7456. }
  7457. //---------------------------------------------------------------------------
  7458. void __fastcall TCustomScpExplorerForm::ShowExtendedException(
  7459. TTerminal * Terminal, Exception * E)
  7460. {
  7461. if (FTrayIcon->Visible)
  7462. {
  7463. PopupTrayBalloon(Terminal, L"", qtError, E);
  7464. }
  7465. // particularly prevent opening new session from jump list,
  7466. // while exception is shown
  7467. NonVisualDataModule->StartBusy();
  7468. try
  7469. {
  7470. ShowExtendedExceptionEx(Terminal, E);
  7471. }
  7472. __finally
  7473. {
  7474. NonVisualDataModule->EndBusy();
  7475. FTrayIcon->CancelBalloon();
  7476. }
  7477. }
  7478. //---------------------------------------------------------------------------
  7479. void __fastcall TCustomScpExplorerForm::SessionReady()
  7480. {
  7481. InitStatusBar();
  7482. // cannot rely on active page being page for active terminal,
  7483. // as it can happen that active page is the "new session" page
  7484. // (e.g. when reconnecting active terminal, while login dialog
  7485. // invoked from "new session" page is modal)
  7486. int ActiveSessionIndex = TTerminalManager::Instance()->ActiveSessionIndex;
  7487. UpdateSessionTab(SessionsPageControl->Pages[ActiveSessionIndex]);
  7488. }
  7489. //---------------------------------------------------------------------------
  7490. void TCustomScpExplorerForm::InactiveTerminalNotify(
  7491. TManagedTerminal * Terminal, const UnicodeString & Message, TQueryType Type, Exception * E)
  7492. {
  7493. Notify(Terminal, Message, Type, false, NULL, NULL, E);
  7494. int Index = TTerminalManager::Instance()->IndexOf(Terminal);
  7495. if (DebugAlwaysTrue((Index >= 0) && (Index < SessionsPageControl->PageCount)))
  7496. {
  7497. UpdateSessionTab(SessionsPageControl->Pages[Index]);
  7498. }
  7499. }
  7500. //---------------------------------------------------------------------------
  7501. void TCustomScpExplorerForm::Notify(
  7502. TManagedTerminal * Terminal, const UnicodeString & AMessage, TQueryType Type,
  7503. bool Important, TNotifyEvent OnClick, TObject * UserData, Exception * E)
  7504. {
  7505. UnicodeString Message = AMessage;
  7506. if ((E == NULL) ||
  7507. ExceptionMessage(E, Message))
  7508. {
  7509. unsigned int Seconds = WinConfiguration->NotificationsTimeout;
  7510. if (Important)
  7511. {
  7512. Seconds *= 5;
  7513. }
  7514. UnicodeString NoteMessage(UnformatMessage(Message));
  7515. if (Terminal != NULL)
  7516. {
  7517. NoteMessage = TTerminalManager::Instance()->FormatTerminalNoteMessage(Terminal, NoteMessage);
  7518. }
  7519. if (WinConfiguration->BalloonNotifications)
  7520. {
  7521. AddNote(NoteMessage);
  7522. PopupTrayBalloon(Terminal, Message, Type, NULL, Seconds, OnClick, UserData);
  7523. }
  7524. else
  7525. {
  7526. FlashOnBackground();
  7527. PostNote(NoteMessage, Seconds, OnClick, UserData);
  7528. }
  7529. }
  7530. }
  7531. //---------------------------------------------------------------------------
  7532. void __fastcall TCustomScpExplorerForm::QueueEmptyNoteClicked(TObject * Sender)
  7533. {
  7534. RestoreApp();
  7535. TTerminalNoteData * TerminalNoteData = dynamic_cast<TTerminalNoteData *>(Sender);
  7536. if (DebugAlwaysTrue(TerminalNoteData != NULL) &&
  7537. !NonVisualDataModule->Busy)
  7538. {
  7539. TManagedTerminal * Terminal = TerminalNoteData->Terminal;
  7540. TTerminalManager::Instance()->ActiveSession = Terminal;
  7541. if (!ComponentVisible[fcQueueView])
  7542. {
  7543. ToggleQueueVisibility();
  7544. GoToQueue();
  7545. }
  7546. }
  7547. }
  7548. //---------------------------------------------------------------------------
  7549. void __fastcall TCustomScpExplorerForm::QueueEvent(TManagedTerminal * ATerminal,
  7550. TTerminalQueue * /*Queue*/, TQueueEvent Event)
  7551. {
  7552. UnicodeString Message;
  7553. TNotifyEvent OnClick = NULL;
  7554. TObject * UserData = NULL;
  7555. bool QueueInvisible = !ComponentVisible[fcQueueView] || IsApplicationMinimized();
  7556. switch (Event)
  7557. {
  7558. case qeEmptyButMonitored:
  7559. if ((ATerminal != Terminal) || QueueInvisible)
  7560. {
  7561. Message = LoadStr(BALLOON_QUEUE_EMPTY);
  7562. OnClick = QueueEmptyNoteClicked;
  7563. TTerminalNoteData * TerminalNoteData = new TTerminalNoteData();
  7564. TerminalNoteData->Terminal = ATerminal;
  7565. UserData = TerminalNoteData;
  7566. }
  7567. break;
  7568. case qeEmpty:
  7569. OperationComplete(ATerminal->QueueOperationStart);
  7570. break;
  7571. case qePendingUserAction:
  7572. if ((ATerminal != Terminal) ||
  7573. (QueueInvisible && !IsQueueAutoPopup()))
  7574. {
  7575. Message = LoadStr(BALLOON_QUEUE_USER_ACTION);
  7576. }
  7577. break;
  7578. default:
  7579. DebugFail();
  7580. }
  7581. if (!Message.IsEmpty())
  7582. {
  7583. Notify(ATerminal, Message, qtInformation, false, OnClick, UserData);
  7584. }
  7585. }
  7586. //---------------------------------------------------------------------------
  7587. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDCreateDragFileList(
  7588. TObject * /*Sender*/, TFileList * FileList, bool & Created)
  7589. {
  7590. if (FDDExtMapFile != NULL)
  7591. {
  7592. CloseHandle(FDDExtMapFile);
  7593. FDDExtMapFile = NULL;
  7594. }
  7595. if (WinConfiguration->DDFakeFile)
  7596. {
  7597. DDFakeFileInitDrag(FileList, Created);
  7598. }
  7599. }
  7600. //---------------------------------------------------------------------------
  7601. void __fastcall TCustomScpExplorerForm::DDFakeCreated(TObject * /*Sender*/, const UnicodeString FileName)
  7602. {
  7603. if (DebugAlwaysTrue(!FDragFakeDirectory.IsEmpty()) &&
  7604. SameText(ExtractFileName(FileName), ExtractFileName(FDragFakeDirectory)))
  7605. {
  7606. FFakeFileDropTarget = FileName;
  7607. }
  7608. }
  7609. //---------------------------------------------------------------------------
  7610. UnicodeString __fastcall TCustomScpExplorerForm::CreateFakeTransferDirectory()
  7611. {
  7612. UnicodeString Result = ExcludeTrailingBackslash(WinConfiguration->TemporaryDir());
  7613. if (!ForceDirectories(ApiPath(Result)))
  7614. {
  7615. throw Exception(FMTLOAD(CREATE_TEMP_DIR_ERROR, (Result)));
  7616. }
  7617. FileSetAttr(ApiPath(Result), faHidden);
  7618. return Result;
  7619. }
  7620. //---------------------------------------------------------------------------
  7621. void __fastcall TCustomScpExplorerForm::DDFakeFileInitDrag(TFileList * FileList,
  7622. bool & Created)
  7623. {
  7624. FFakeFileDropTarget = UnicodeString();
  7625. FDragFakeDirectory = CreateFakeTransferDirectory();
  7626. FileList->AddItem(NULL, FDragFakeDirectory);
  7627. Created = true;
  7628. if (!WinConfiguration->IsDDExtRunning() || WinConfiguration->IsDDExtBroken())
  7629. {
  7630. FDragFakeMonitors = StartCreationDirectoryMonitorsOnEachDrive(FILE_NOTIFY_CHANGE_DIR_NAME, DDFakeCreated);
  7631. }
  7632. FDDExtMapFile = CreateFileMappingA((HANDLE)0xFFFFFFFF, NULL, PAGE_READWRITE,
  7633. 0, sizeof(TDragExtCommStruct), AnsiString(DRAG_EXT_MAPPING).c_str());
  7634. {
  7635. TMutexGuard Guard(FDDExtMutex, DRAGEXT_MUTEX_RELEASE_TIMEOUT);
  7636. TDragExtCommStruct* CommStruct;
  7637. CommStruct = static_cast<TDragExtCommStruct*>(MapViewOfFile(FDDExtMapFile,
  7638. FILE_MAP_ALL_ACCESS, 0, 0, 0));
  7639. DebugAssert(CommStruct != NULL);
  7640. CommStruct->Version = TDragExtCommStruct::CurrentVersion;
  7641. CommStruct->Dragging = true;
  7642. wcsncpy(CommStruct->DropDest, FDragFakeDirectory.c_str(),
  7643. LENOF(CommStruct->DropDest));
  7644. NULL_TERMINATE(CommStruct->DropDest);
  7645. UnmapViewOfFile(CommStruct);
  7646. }
  7647. }
  7648. //---------------------------------------------------------------------------
  7649. bool __fastcall TCustomScpExplorerForm::RemoteFileControlFileOperation(
  7650. TObject * Sender, TFileOperation Operation, bool NoConfirmation, void * Param)
  7651. {
  7652. bool Result;
  7653. DebugAssert(!IsLocalBrowserMode());
  7654. if (Sender == RemoteDirView)
  7655. {
  7656. Result = ExecuteFileOperation(Operation, osRemote, true, NoConfirmation, Param);
  7657. }
  7658. else
  7659. {
  7660. DebugAssert(Sender == RemoteDriveView);
  7661. TStrings * FileList = RemoteDriveView->DragFileList();
  7662. try
  7663. {
  7664. Result = ExecuteFileOperation(Operation, osRemote, FileList, NoConfirmation, Param);
  7665. }
  7666. __finally
  7667. {
  7668. delete FileList;
  7669. }
  7670. }
  7671. if (FDDTargetControl == RemoteDriveView)
  7672. {
  7673. RemoteDriveView->UpdateDropTarget();
  7674. }
  7675. // foRemoteMove happens when file/dir is dragged within the remote tree view
  7676. // or from tree view to dir view.
  7677. // foMove happens when file/dir is dragged from remote tree view outside of
  7678. // application via dragex
  7679. if (((Operation == foRemoteMove) || (Operation == foMove)) &&
  7680. (DropSourceControl == RemoteDriveView))
  7681. {
  7682. RemoteDriveView->UpdateDropSource();
  7683. }
  7684. return Result;
  7685. }
  7686. //---------------------------------------------------------------------------
  7687. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDEnd(TObject * Sender)
  7688. {
  7689. DebugAssert(!IsLocalBrowserMode());
  7690. // This also handles drops of remote files to queue.
  7691. // Drops of local files (uploads) are handled in QueueDDProcessDropped.
  7692. SAFE_DESTROY(FDDFileList);
  7693. if (!FFakeFileDropTarget.IsEmpty())
  7694. {
  7695. RemoveDir(ApiPath(FFakeFileDropTarget));
  7696. }
  7697. if ((FDDExtMapFile != NULL) || (FDDTargetControl == QueueView3))
  7698. {
  7699. try
  7700. {
  7701. TDragResult DDResult = (Sender == RemoteDirView) ?
  7702. RemoteDirView->LastDDResult : RemoteDriveView->LastDDResult;
  7703. // Focus is moved to the target application,
  7704. // but as we are going to present the UI, we need to steal the focus back.
  7705. // This most likely won't work though (windows does not allow application
  7706. // to steal focus most of the time)
  7707. Application->BringToFront();
  7708. // On older version of Windows we never got drMove here, see also comment below.
  7709. // On Windows 10, we get the "move".
  7710. if ((DDResult == drCopy) || (DDResult == drMove) || (DDResult == drInvalid))
  7711. {
  7712. UnicodeString TargetDirectory;
  7713. TFileOperation Operation;
  7714. // drInvalid may mean drMove, see comment below
  7715. switch (DDResult)
  7716. {
  7717. case drCopy:
  7718. Operation = foCopy;
  7719. break;
  7720. case drMove:
  7721. Operation = foMove;
  7722. break;
  7723. default:
  7724. DebugFail();
  7725. case drInvalid:
  7726. // prefer "copy" for safety
  7727. Operation = FLAGSET(FLastDropEffect, DROPEFFECT_MOVE) ? foMove : foCopy;
  7728. break;
  7729. }
  7730. TTransferOperationParam Param;
  7731. UnicodeString CounterName;
  7732. bool ForceQueue;
  7733. if (!DDGetTarget(Param.TargetDirectory, ForceQueue, CounterName))
  7734. {
  7735. // we get drInvalid both if d&d was intercepted by ddext,
  7736. // and when users drops on no-drop location.
  7737. // we tell the difference by existence of response from ddext,
  7738. // so we ignore absence of response in this case
  7739. if (DDResult != drInvalid)
  7740. {
  7741. // here we know that the extension is installed,
  7742. // as it is checked as soon as drag&drop starts from
  7743. // RemoteFileControlDDCreateDragFileList
  7744. Configuration->Usage->Inc(L"DownloadsDragDropExternalExtTargetUnknown");
  7745. if (!WinConfiguration->DDExtInstalled || WinConfiguration->IsDDExtBroken())
  7746. {
  7747. throw ExtException(NULL, LoadStr(DD_TARGET_UNKNOWN), HELP_DD_TARGET_UNKNOWN);
  7748. }
  7749. else
  7750. {
  7751. throw ExtException(NULL, LoadStr(DRAGEXT_TARGET_UNKNOWN2), HELP_DRAGEXT_TARGET_UNKNOWN);
  7752. }
  7753. }
  7754. }
  7755. else
  7756. {
  7757. // download using fake file
  7758. Param.Temp = false;
  7759. Param.DragDrop = true;
  7760. if (ForceQueue)
  7761. {
  7762. Param.Queue = asOn;
  7763. }
  7764. if (Sender == RemoteDirView)
  7765. {
  7766. Param.Options = FLAGMASK(SelectedAllFilesInDirView(RemoteDirView), coAllFiles);
  7767. }
  7768. if (RemoteFileControlFileOperation(Sender, Operation,
  7769. (WinConfiguration->DDTransferConfirmation == asOff), &Param))
  7770. {
  7771. Configuration->Usage->Inc(CounterName);
  7772. }
  7773. }
  7774. }
  7775. }
  7776. __finally
  7777. {
  7778. CloseHandle(FDDExtMapFile);
  7779. FDDExtMapFile = NULL;
  7780. delete FDragFakeMonitors;
  7781. FDragFakeMonitors = NULL;
  7782. RemoveDir(ApiPath(FDragFakeDirectory));
  7783. FDragFakeDirectory = L"";
  7784. FFakeFileDropTarget = UnicodeString();
  7785. }
  7786. }
  7787. if (FDragDropSshTerminate.get() != NULL)
  7788. {
  7789. FDragDropSshTerminate->Rethrow();
  7790. }
  7791. }
  7792. //---------------------------------------------------------------------------
  7793. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDGiveFeedback(
  7794. TObject * /*Sender*/, int dwEffect, HRESULT & /*Result*/)
  7795. {
  7796. // Remember drop effect so we know (when user drops files), if we copy or move
  7797. FLastDropEffect = dwEffect;
  7798. }
  7799. //---------------------------------------------------------------------------
  7800. bool __fastcall TCustomScpExplorerForm::DDGetTarget(
  7801. UnicodeString & Directory, bool & ForceQueue, UnicodeString & CounterName)
  7802. {
  7803. bool Result;
  7804. if (FDDTargetControl == QueueView3)
  7805. {
  7806. Directory = DefaultDownloadTargetDirectory();
  7807. Result = true;
  7808. CounterName = L"DownloadsDragDropQueue";
  7809. ForceQueue = true;
  7810. }
  7811. else if (!FFakeFileDropTarget.IsEmpty())
  7812. {
  7813. Directory = ExcludeTrailingBackslash(ExtractFilePath(FFakeFileDropTarget));
  7814. Result = true;
  7815. ForceQueue = false;
  7816. CounterName = L"DownloadsDragDropFakeFile";
  7817. }
  7818. else
  7819. {
  7820. ForceQueue = false;
  7821. Enabled = false;
  7822. try
  7823. {
  7824. int Timer = 0;
  7825. Result = false;
  7826. while (!Result && (Timer < WinConfiguration->DDExtTimeout))
  7827. {
  7828. {
  7829. TMutexGuard Guard(FDDExtMutex, DRAGEXT_MUTEX_RELEASE_TIMEOUT);
  7830. TDragExtCommStruct* CommStruct;
  7831. CommStruct = static_cast<TDragExtCommStruct*>(MapViewOfFile(FDDExtMapFile,
  7832. FILE_MAP_ALL_ACCESS, 0, 0, 0));
  7833. DebugAssert(CommStruct != NULL);
  7834. Result = !CommStruct->Dragging;
  7835. if (Result)
  7836. {
  7837. Directory = ExtractFilePath(CommStruct->DropDest);
  7838. CounterName = L"DownloadsDragDropExternalExt";
  7839. }
  7840. UnmapViewOfFile(CommStruct);
  7841. }
  7842. if (!Result)
  7843. {
  7844. Sleep(50);
  7845. Timer += 50;
  7846. Application->ProcessMessages();
  7847. }
  7848. }
  7849. }
  7850. __finally
  7851. {
  7852. Enabled = true;
  7853. }
  7854. }
  7855. return Result;
  7856. }
  7857. //---------------------------------------------------------------------------
  7858. void __fastcall TCustomScpExplorerForm::AddDelayedDirectoryDeletion(
  7859. const UnicodeString TempDir, int SecDelay)
  7860. {
  7861. TDateTime Alarm = IncSecond(Now(), SecDelay);
  7862. FDelayedDeletionList->AddObject(TempDir, reinterpret_cast<TObject*>(Alarm.FileDate()));
  7863. if (FDelayedDeletionTimer == NULL)
  7864. {
  7865. DebugAssert(HandleAllocated());
  7866. FDelayedDeletionTimer = new TTimer(this);
  7867. FDelayedDeletionTimer->Interval = 10000;
  7868. FDelayedDeletionTimer->OnTimer = DoDelayedDeletion;
  7869. }
  7870. else
  7871. {
  7872. FDelayedDeletionTimer->Enabled = true;
  7873. }
  7874. }
  7875. //---------------------------------------------------------------------------
  7876. void __fastcall TCustomScpExplorerForm::DoDelayedDeletion(TObject * Sender)
  7877. {
  7878. DebugAssert(FDelayedDeletionList != NULL);
  7879. TDateTime N = Now();
  7880. TDateTime Alert;
  7881. UnicodeString Directory;
  7882. for (int Index = FDelayedDeletionList->Count-1; Index >= 0; Index--)
  7883. {
  7884. Alert = FileDateToDateTime(reinterpret_cast<int>(FDelayedDeletionList->Objects[Index]));
  7885. if ((N >= Alert) || (Sender == NULL))
  7886. {
  7887. Directory = FDelayedDeletionList->Strings[Index];
  7888. if (RecursiveDeleteFile(ExcludeTrailingBackslash(Directory)))
  7889. {
  7890. FDelayedDeletionList->Delete(Index);
  7891. }
  7892. }
  7893. }
  7894. if (FDelayedDeletionList->Count == 0)
  7895. {
  7896. FDelayedDeletionTimer->Enabled = false;
  7897. }
  7898. }
  7899. //---------------------------------------------------------------------------
  7900. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDTargetDrop()
  7901. {
  7902. DebugAssert(!IsLocalBrowserMode());
  7903. if (IsFileControl(FDDTargetControl, osRemote) ||
  7904. (FDDTargetControl == SessionsPageControl))
  7905. {
  7906. TManagedTerminal * TargetTerminal = NULL;
  7907. TFileOperation Operation = foNone;
  7908. if (FDDTargetControl == SessionsPageControl)
  7909. {
  7910. TPoint Point = SessionsPageControl->ScreenToClient(Mouse->CursorPos);
  7911. int Index = SessionsPageControl->IndexOfTabAt(Point.X, Point.Y);
  7912. // do not allow dropping on local-local, disconnected and the "+" tab (see also SessionsDDProcessDropped)
  7913. TargetTerminal = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  7914. if (IsActiveTerminal(TargetTerminal) && DebugAlwaysTrue(!TargetTerminal->LocalBrowser))
  7915. {
  7916. if ((FLastDropEffect == DROPEFFECT_MOVE) &&
  7917. (TargetTerminal == TTerminalManager::Instance()->ActiveTerminal))
  7918. {
  7919. Operation = foRemoteMove;
  7920. }
  7921. else
  7922. {
  7923. Operation = foRemoteCopy;
  7924. }
  7925. }
  7926. }
  7927. else
  7928. {
  7929. if (FLastDropEffect == DROPEFFECT_MOVE)
  7930. {
  7931. Operation = foRemoteMove;
  7932. }
  7933. else if (FLastDropEffect == DROPEFFECT_COPY)
  7934. {
  7935. Operation = foRemoteCopy;
  7936. }
  7937. }
  7938. if (Operation != foNone)
  7939. {
  7940. RemoteFileControlFileOperation(DropSourceControl,
  7941. Operation, (WinConfiguration->DDTransferConfirmation == asOff), TargetTerminal);
  7942. }
  7943. // abort drag&drop
  7944. Abort();
  7945. }
  7946. else if ((FDDExtMapFile == NULL) && (FLastDropEffect != DROPEFFECT_NONE) &&
  7947. // Drops of remote files to queue are handled in RemoteFileControlDDEnd
  7948. (FDDTargetControl != QueueView3))
  7949. {
  7950. DebugAssert(!FDragTempDir.IsEmpty());
  7951. TTransferType Type;
  7952. UnicodeString TempDir = FDragTempDir;
  7953. // We clear FDragTempDir before calling
  7954. // just in case it fail (raises exception)
  7955. FDragTempDir = L"";
  7956. Type = (FLastDropEffect & DROPEFFECT_MOVE ? ttMove : Type = ttCopy);
  7957. TGUICopyParamType CopyParams = GUIConfiguration->CurrentCopyParam;
  7958. // empty directory parameter means temp directory -> don't display it!
  7959. UnicodeString TargetDir = L"";
  7960. int Options = 0;
  7961. if (!CopyParamDialog(tdToLocal, Type, true, FDDFileList,
  7962. TargetDir, CopyParams, (WinConfiguration->DDTransferConfirmation != asOff), true, Options))
  7963. {
  7964. Abort();
  7965. }
  7966. // TargetDir is set when dropped on local file control
  7967. // (this was workaround for legacy dirview event handling, now it should be
  7968. // made prettier)
  7969. if (TargetDir.IsEmpty())
  7970. {
  7971. TargetDir = TempDir;
  7972. if (ForceDirectories(ApiPath(TargetDir)))
  7973. {
  7974. DebugAssert(Terminal && !TargetDir.IsEmpty());
  7975. FPendingTempSpaceWarn = true;
  7976. try
  7977. {
  7978. FDragDropOperation = true;
  7979. int Params = cpTemporary |
  7980. (Type == ttMove ? cpDelete : 0);
  7981. DDDownload(FDDFileList, TargetDir, &CopyParams, Params);
  7982. Configuration->Usage->Inc(L"DownloadsDragDropExternalTemp");
  7983. }
  7984. __finally
  7985. {
  7986. FDragDropOperation = false;
  7987. FPendingTempSpaceWarn = false;
  7988. AddDelayedDirectoryDeletion(TargetDir, WinConfiguration->DDDeleteDelay);
  7989. }
  7990. }
  7991. else
  7992. {
  7993. throw Exception(FMTLOAD(CREATE_TEMP_DIR_ERROR, (TargetDir)));
  7994. }
  7995. }
  7996. }
  7997. }
  7998. //---------------------------------------------------------------------------
  7999. void __fastcall TCustomScpExplorerForm::DDDownload(
  8000. TStrings * FilesToCopy, const UnicodeString & TargetDir, TCopyParamType * CopyParam, int Params)
  8001. {
  8002. DebugAssert(!IsLocalBrowserMode());
  8003. TAutoBatch AutoBatch(this);
  8004. CopyParam->IncludeFileMask.SetRoots(TargetDir, FilesToCopy);
  8005. UpdateCopyParamCounters(*CopyParam);
  8006. FMoveToQueue = false;
  8007. Terminal->CopyToLocal(FilesToCopy, TargetDir, CopyParam, Params, NULL);
  8008. if (FLAGSET(Params, cpDelete) && (DropSourceControl == RemoteDriveView))
  8009. {
  8010. RemoteDriveView->UpdateDropSource();
  8011. }
  8012. }
  8013. //---------------------------------------------------------------------------
  8014. class TFakeDataObjectFilesEx : public TDataObjectFilesEx
  8015. {
  8016. public:
  8017. __fastcall TFakeDataObjectFilesEx(TFileList * AFileList, bool RenderPIDL,
  8018. bool RenderFilename) : TDataObjectFilesEx(AFileList, RenderPIDL, RenderFilename, true)
  8019. {
  8020. }
  8021. virtual bool __fastcall AllowData(const tagFORMATETC & FormatEtc)
  8022. {
  8023. return (FormatEtc.cfFormat == CF_HDROP) ? false :
  8024. TDataObjectFilesEx::AllowData(FormatEtc);
  8025. }
  8026. };
  8027. //---------------------------------------------------------------------------
  8028. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDCreateDataObject(
  8029. TObject * Sender, TDataObject *& DataObject)
  8030. {
  8031. if (FDDExtMapFile != NULL)
  8032. {
  8033. TFileList * FileList = DragDropFiles(Sender)->FileList;
  8034. if (!FileList->RenderPIDLs() || !FileList->RenderNames())
  8035. {
  8036. Abort();
  8037. }
  8038. if (FileList->Count > 0)
  8039. {
  8040. TDataObjectFilesEx * FilesObject = new TFakeDataObjectFilesEx(FileList, true, true);
  8041. if (!FilesObject->IsValid(true, true))
  8042. {
  8043. FilesObject->_Release();
  8044. }
  8045. else
  8046. {
  8047. DataObject = FilesObject;
  8048. }
  8049. }
  8050. }
  8051. }
  8052. //---------------------------------------------------------------------------
  8053. void __fastcall TCustomScpExplorerForm::GoToCommandLine()
  8054. {
  8055. DebugFail();
  8056. }
  8057. //---------------------------------------------------------------------------
  8058. void __fastcall TCustomScpExplorerForm::GoToTree()
  8059. {
  8060. ComponentVisible[fcRemoteTree] = true;
  8061. DriveView(osRemote)->SetFocus();
  8062. }
  8063. //---------------------------------------------------------------------------
  8064. TStrings * __fastcall TCustomScpExplorerForm::PanelExport(TOperationSide Side,
  8065. TPanelExport Export)
  8066. {
  8067. TCustomDirView * DirView = this->DirView(Side);
  8068. std::unique_ptr<TStrings> ExportData(new TStringList());
  8069. switch (Export)
  8070. {
  8071. case pePath:
  8072. ExportData->Add(DirView->PathName);
  8073. break;
  8074. case peFileList:
  8075. case peFullFileList:
  8076. {
  8077. bool FullPath = (Export == peFullFileList);
  8078. DirView->CreateFileList(false, FullPath, ExportData.get());
  8079. for (int Index = 0; Index < ExportData->Count; Index++)
  8080. {
  8081. if (ExportData->Strings[Index].Pos(L" ") > 0)
  8082. {
  8083. ExportData->Strings[Index] = FORMAT(L"\"%s\"", (ExportData->Strings[Index]));
  8084. }
  8085. }
  8086. }
  8087. break;
  8088. default:
  8089. DebugFail();
  8090. }
  8091. return ExportData.release();
  8092. }
  8093. //---------------------------------------------------------------------------
  8094. void __fastcall TCustomScpExplorerForm::PanelExport(TOperationSide Side,
  8095. TPanelExport Export, TPanelExportDestination Destination)
  8096. {
  8097. std::unique_ptr<TStrings> ExportData(PanelExport(Side, Export));
  8098. PanelExportStore(Side, Export, Destination, ExportData.get());
  8099. }
  8100. //---------------------------------------------------------------------------
  8101. void __fastcall TCustomScpExplorerForm::PanelExportStore(TOperationSide /*Side*/,
  8102. TPanelExport /*Export*/, TPanelExportDestination Destination,
  8103. TStrings * ExportData)
  8104. {
  8105. if (Destination == pedClipboard)
  8106. {
  8107. TInstantOperationVisualizer Visualizer;
  8108. CopyToClipboard(ExportData);
  8109. }
  8110. else
  8111. {
  8112. DebugFail();
  8113. }
  8114. }
  8115. //---------------------------------------------------------------------------
  8116. void __fastcall TCustomScpExplorerForm::Filter(TOperationSide Side)
  8117. {
  8118. TCustomDirView * DirView = this->DirView(Side);
  8119. UnicodeString Mask = DirView->Mask;
  8120. if (DoFilterMaskDialog(DirView, Mask))
  8121. {
  8122. DirView->Mask = TFileMasks::NormalizeMask(Mask);
  8123. Configuration->Usage->Inc(L"Filters");
  8124. }
  8125. }
  8126. //---------------------------------------------------------------------------
  8127. TQueueOperation __fastcall TCustomScpExplorerForm::DefaultQueueOperation()
  8128. {
  8129. return FQueueController->DefaultOperation();
  8130. }
  8131. //---------------------------------------------------------------------------
  8132. bool __fastcall TCustomScpExplorerForm::AllowQueueOperation(
  8133. TQueueOperation Operation, void ** Param)
  8134. {
  8135. switch (Operation)
  8136. {
  8137. case qoPreferences:
  8138. return true;
  8139. case qoGoTo:
  8140. return ComponentVisible[fcQueueView] && QueueView3->Enabled;
  8141. case qoOnceEmpty:
  8142. return IsAnythingQueued();
  8143. default:
  8144. return FQueueController->AllowOperation(Operation, Param);
  8145. }
  8146. }
  8147. //---------------------------------------------------------------------------
  8148. void __fastcall TCustomScpExplorerForm::GoToQueue()
  8149. {
  8150. if (DebugAlwaysTrue(QueueView3->Visible))
  8151. {
  8152. QueueView3->SetFocus();
  8153. }
  8154. }
  8155. //---------------------------------------------------------------------------
  8156. void __fastcall TCustomScpExplorerForm::ExecuteQueueOperation(
  8157. TQueueOperation Operation, void * Param)
  8158. {
  8159. if (Operation == qoGoTo)
  8160. {
  8161. GoToQueue();
  8162. }
  8163. else if (Operation == qoPreferences)
  8164. {
  8165. PreferencesDialog(pmQueue);
  8166. }
  8167. else
  8168. {
  8169. FQueueController->ExecuteOperation(Operation, Param);
  8170. }
  8171. }
  8172. //---------------------------------------------------------------------------
  8173. bool __fastcall TCustomScpExplorerForm::GetQueueEnabled()
  8174. {
  8175. return (Queue != NULL) && Queue->Enabled;
  8176. }
  8177. //---------------------------------------------------------------------------
  8178. void __fastcall TCustomScpExplorerForm::ToggleQueueEnabled()
  8179. {
  8180. DebugAssert(Queue != NULL);
  8181. if (Queue != NULL)
  8182. {
  8183. Queue->Enabled = !Queue->Enabled;
  8184. }
  8185. }
  8186. //---------------------------------------------------------------------------
  8187. void __fastcall TCustomScpExplorerForm::QueueView3ContextPopup(
  8188. TObject * /*Sender*/, TPoint & /*MousePos*/, bool & /*Handled*/)
  8189. {
  8190. FQueueActedItem = QueueView3->ItemFocused;
  8191. }
  8192. //---------------------------------------------------------------------------
  8193. /*virtual*/ int __fastcall TCustomScpExplorerForm::GetStaticComponentsHeight()
  8194. {
  8195. return TopDock->Height + (MessageDock->Visible ? MessageDock->Height : 0) + QueueSplitter->Height;
  8196. }
  8197. //---------------------------------------------------------------------------
  8198. int __fastcall TCustomScpExplorerForm::GetStaticQueuePanelComponentsHeight()
  8199. {
  8200. return
  8201. (QueueFileListSplitter->Visible ? QueueFileListSplitter->Height : 0) +
  8202. (QueueDock->Visible ? QueueDock->Height : 0) +
  8203. (QueueLabel->Visible ? QueueLabel->Height : 0);
  8204. }
  8205. //---------------------------------------------------------------------------
  8206. int __fastcall TCustomScpExplorerForm::GetMinQueueViewHeight()
  8207. {
  8208. return ScaleByTextHeight(this, 48);
  8209. }
  8210. //---------------------------------------------------------------------------
  8211. void __fastcall TCustomScpExplorerForm::QueueSplitterCanResize(
  8212. TObject * /*Sender*/, int & NewSize, bool & Accept)
  8213. {
  8214. // when queue is hidden by double-clicking splitter, stray attempt to
  8215. // resize the panel with strange value arrives, make sure it is ignored
  8216. if (ComponentVisible[fcQueueView])
  8217. {
  8218. int MaxHeightLimit = ClientHeight - GetStaticComponentsHeight() - RemotePanel->Constraints->MinHeight;
  8219. if (NewSize > MaxHeightLimit)
  8220. {
  8221. NewSize = MaxHeightLimit;
  8222. }
  8223. int MinHeightLimit =
  8224. GetStaticQueuePanelComponentsHeight() +
  8225. (QueueFileList->Visible ? QueueFileList->Height : 0) +
  8226. GetMinQueueViewHeight();
  8227. if (NewSize < MinHeightLimit)
  8228. {
  8229. NewSize = MinHeightLimit;
  8230. }
  8231. }
  8232. else
  8233. {
  8234. Accept = false;
  8235. }
  8236. }
  8237. //---------------------------------------------------------------------------
  8238. void __fastcall TCustomScpExplorerForm::QueueFileListSplitterCanResize(TObject *, int & NewSize, bool &)
  8239. {
  8240. int TotalHeight = GetStaticQueuePanelComponentsHeight() + NewSize;
  8241. int QueueViewHeight = QueuePanel->ClientHeight - TotalHeight;
  8242. if (QueueViewHeight < GetMinQueueViewHeight())
  8243. {
  8244. NewSize -= (GetMinQueueViewHeight() - QueueViewHeight);
  8245. }
  8246. }
  8247. //---------------------------------------------------------------------------
  8248. void __fastcall TCustomScpExplorerForm::QueueView3StartDrag(TObject * /*Sender*/,
  8249. TDragObject *& /*DragObject*/)
  8250. {
  8251. FQueueActedItem = QueueView3->ItemFocused;
  8252. FQueueScrollOnDragOver->StartDrag();
  8253. }
  8254. //---------------------------------------------------------------------------
  8255. void __fastcall TCustomScpExplorerForm::QueueView3EndDrag(
  8256. TObject *, TObject * DebugUsedArg(Target), int DebugUsedArg(X), int DebugUsedArg(Y))
  8257. {
  8258. FQueueScrollOnDragOver->EndDrag();
  8259. }
  8260. //---------------------------------------------------------------------------
  8261. void __fastcall TCustomScpExplorerForm::QueueView3DragOver(TObject * /*Sender*/,
  8262. TObject * Source, int X, int Y, TDragState /*State*/, bool & Accept)
  8263. {
  8264. Accept = true;
  8265. if (Source == QueueView3)
  8266. {
  8267. TListItem * DropTarget = QueueView3->GetItemAt(X, Y);
  8268. Accept = (DropTarget != NULL) && (FQueueActedItem != NULL);
  8269. if (Accept)
  8270. {
  8271. TQueueItemProxy * QueueItem;
  8272. TQueueItemProxy * DestQueueItem;
  8273. QueueItem = static_cast<TQueueItemProxy *>(FQueueActedItem->Data);
  8274. DestQueueItem = static_cast<TQueueItemProxy *>(DropTarget->Data);
  8275. Accept = (QueueItem != DestQueueItem) &&
  8276. (QueueItem->Status == TQueueItem::qsPending) &&
  8277. (DestQueueItem->Status == TQueueItem::qsPending);
  8278. }
  8279. FQueueScrollOnDragOver->DragOver(TPoint(X, Y));
  8280. }
  8281. }
  8282. //---------------------------------------------------------------------------
  8283. void __fastcall TCustomScpExplorerForm::QueueView3DragDrop(TObject * /*Sender*/,
  8284. TObject * /*Source*/, int /*X*/, int /*Y*/)
  8285. {
  8286. if ((FQueueActedItem != NULL) && (QueueView3->DropTarget != NULL))
  8287. {
  8288. TQueueItemProxy * QueueItem;
  8289. TQueueItemProxy * DestQueueItem;
  8290. QueueItem = static_cast<TQueueItemProxy *>(FQueueActedItem->Data);
  8291. DestQueueItem = static_cast<TQueueItemProxy *>(QueueView3->DropTarget->Data);
  8292. QueueItem->Move(DestQueueItem);
  8293. }
  8294. }
  8295. //---------------------------------------------------------------------------
  8296. void __fastcall TCustomScpExplorerForm::QueueView3Enter(TObject * /*Sender*/)
  8297. {
  8298. if ((QueueView3->ItemFocused == NULL) &&
  8299. (QueueView3->Items->Count > 0))
  8300. {
  8301. QueueView3->ItemFocused = QueueView3->Items->Item[0];
  8302. }
  8303. QueueLabelUpdateStatus();
  8304. }
  8305. //---------------------------------------------------------------------------
  8306. void __fastcall TCustomScpExplorerForm::QueueView3Exit(TObject * /*Sender*/)
  8307. {
  8308. QueueLabelUpdateStatus();
  8309. }
  8310. //---------------------------------------------------------------------------
  8311. void __fastcall TCustomScpExplorerForm::QueueLabelUpdateStatus()
  8312. {
  8313. QueueLabel->UpdateStatus();
  8314. }
  8315. //---------------------------------------------------------------------------
  8316. void __fastcall TCustomScpExplorerForm::QueueView3SelectItem(
  8317. TObject * /*Sender*/, TListItem * /*Item*/, bool Selected)
  8318. {
  8319. if (Selected)
  8320. {
  8321. NonVisualDataModule->UpdateNonVisibleActions();
  8322. }
  8323. }
  8324. //---------------------------------------------------------------------------
  8325. TDragDropFilesEx * __fastcall TCustomScpExplorerForm::DragDropFiles(TObject * Sender)
  8326. {
  8327. TDragDropFilesEx * Result = NULL;
  8328. if (Sender == SessionsPageControl)
  8329. {
  8330. Result = FSessionsDragDropFilesEx;
  8331. }
  8332. else if (Sender == QueueView3)
  8333. {
  8334. Result = FQueueDragDropFilesEx;
  8335. }
  8336. else
  8337. {
  8338. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  8339. if (DirView != NULL)
  8340. {
  8341. Result = DirView->DragDropFilesEx;
  8342. }
  8343. else
  8344. {
  8345. TCustomDriveView * DriveView = dynamic_cast<TCustomDriveView *>(Sender);
  8346. if (DriveView != NULL)
  8347. {
  8348. Result = DriveView->DragDropFilesEx;
  8349. }
  8350. }
  8351. }
  8352. DebugAssert(Result != NULL);
  8353. return Result;
  8354. }
  8355. //---------------------------------------------------------------------------
  8356. bool __fastcall TCustomScpExplorerForm::SelectedAllFilesInDirView(TCustomDirView * DView)
  8357. {
  8358. return (DView->SelCount == DView->FilesCount);
  8359. }
  8360. //---------------------------------------------------------------------------
  8361. bool __fastcall TCustomScpExplorerForm::DraggingAllFilesFromDirView(TOperationSide Side, TStrings * FileList)
  8362. {
  8363. return HasDirView[Side] && (DropSourceControl == DirView(Side)) && (FileList->Count == DirView(Side)->FilesCount);
  8364. }
  8365. //---------------------------------------------------------------------------
  8366. void __fastcall TCustomScpExplorerForm::RemoteFileControlDragDropFileOperation(
  8367. TObject * Sender, int Effect, UnicodeString TargetPath, bool ForceQueue, bool DragDrop)
  8368. {
  8369. DebugAssert(!IsLocalBrowserMode());
  8370. TFileOperation Operation;
  8371. switch (Effect)
  8372. {
  8373. case DROPEFFECT_MOVE:
  8374. Operation = foMove;
  8375. break;
  8376. case DROPEFFECT_COPY:
  8377. // occures on WinXP (reported by user)
  8378. default:
  8379. Operation = foCopy;
  8380. break;
  8381. };
  8382. TDragDropFilesEx * DragDropFilesEx = DragDropFiles(Sender);
  8383. // see a comment in TUnixDirView::PerformItemDragDropOperation
  8384. if (DragDropFilesEx->FileList->Count > 0)
  8385. {
  8386. TStrings * FileList = new TStringList();
  8387. try
  8388. {
  8389. for (int Index = 0; Index < DragDropFilesEx->FileList->Count; Index++)
  8390. {
  8391. FileList->Add(DragDropFilesEx->FileList->Items[Index]->Name);
  8392. }
  8393. FDragDropOperation = true;
  8394. TTransferOperationParam Param;
  8395. Param.TargetDirectory = TargetPath;
  8396. // upload, no temp dirs
  8397. Param.Temp = false;
  8398. Param.DragDrop = DragDrop;
  8399. Param.Options =
  8400. FLAGMASK(DraggingAllFilesFromDirView(osLocal, FileList), coAllFiles);
  8401. if (ForceQueue)
  8402. {
  8403. Param.Queue = asOn;
  8404. }
  8405. bool NoConfirmation = DragDrop ? (WinConfiguration->DDTransferConfirmation == asOff) : false;
  8406. if (ExecuteFileOperation(Operation, osLocal, FileList, NoConfirmation, &Param))
  8407. {
  8408. if (IsFileControl(DropSourceControl, osLocal))
  8409. {
  8410. Configuration->Usage->Inc(L"UploadsDragDropInternal");
  8411. }
  8412. else
  8413. {
  8414. Configuration->Usage->Inc(L"UploadsDragDropExternal");
  8415. }
  8416. }
  8417. }
  8418. __finally
  8419. {
  8420. FDragDropOperation = false;
  8421. delete FileList;
  8422. }
  8423. }
  8424. }
  8425. //---------------------------------------------------------------------------
  8426. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDFileOperation(
  8427. TObject * Sender, int Effect, UnicodeString /*SourcePath*/,
  8428. UnicodeString TargetPath, bool Paste, bool & /*DoOperation*/)
  8429. {
  8430. RemoteFileControlDragDropFileOperation(Sender, Effect, TargetPath, false, !Paste);
  8431. }
  8432. //---------------------------------------------------------------------------
  8433. void __fastcall TCustomScpExplorerForm::RemoteFileContolDDChooseEffect(
  8434. TObject * Sender, int grfKeyState, int & dwEffect)
  8435. {
  8436. DebugAssert(!IsLocalBrowserMode());
  8437. // if any drop effect is allowed at all (e.g. no drop to self and drop to parent)
  8438. if ((dwEffect != DROPEFFECT_NONE) &&
  8439. IsFileControl(DropSourceControl, osRemote))
  8440. {
  8441. // do not allow drop on remote panel (on free space, still allow drop on directories)
  8442. if ((Sender == RemoteDirView) && (DropSourceControl == RemoteDirView) &&
  8443. (RemoteDirView->DropTarget == NULL))
  8444. {
  8445. dwEffect = DROPEFFECT_NONE;
  8446. }
  8447. else
  8448. {
  8449. if (dwEffect == DROPEFFECT_COPY)
  8450. {
  8451. bool MoveCapable = Terminal->IsCapable[fcRemoteMove];
  8452. // currently we support copying always (at least via temporary directory);
  8453. // remove associated checks once this all proves stable and working
  8454. bool CopyCapable = true;
  8455. // if we do not support neither of operations, there's no discussion
  8456. if (!MoveCapable && !CopyCapable)
  8457. {
  8458. dwEffect = DROPEFFECT_NONE;
  8459. }
  8460. else
  8461. {
  8462. // The default effect inside remote panel is move,
  8463. // unless we do not support it, but support copy
  8464. if (FLAGCLEAR(grfKeyState, MK_CONTROL))
  8465. {
  8466. dwEffect = MoveCapable ? DROPEFFECT_MOVE : DROPEFFECT_COPY;
  8467. }
  8468. else
  8469. {
  8470. // with ctrl-down, we want copy unless it is not supported
  8471. dwEffect = CopyCapable ? DROPEFFECT_COPY : DROPEFFECT_NONE;
  8472. }
  8473. }
  8474. }
  8475. }
  8476. }
  8477. }
  8478. //---------------------------------------------------------------------------
  8479. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDDragFileName(
  8480. TObject * Sender, TRemoteFile * File, UnicodeString & FileName)
  8481. {
  8482. DebugAssert(!IsLocalBrowserMode());
  8483. if (FDDTotalSize >= 0)
  8484. {
  8485. if (File->IsDirectory)
  8486. {
  8487. FDDTotalSize = -1;
  8488. }
  8489. else
  8490. {
  8491. FDDTotalSize += File->Size;
  8492. }
  8493. }
  8494. DebugAssert(!FDragTempDir.IsEmpty());
  8495. // TODO: this is quite ineffective
  8496. // TODO: what if invalid character replacement is disabled?
  8497. FileName = FDragTempDir + GUIConfiguration->CurrentCopyParam.ValidLocalFileName(File->FileName);
  8498. UnicodeString TransferFileName;
  8499. if (Sender == RemoteDriveView)
  8500. {
  8501. TransferFileName = UnixExcludeTrailingBackslash(File->FullFileName);
  8502. }
  8503. else
  8504. {
  8505. TransferFileName = File->FileName;
  8506. }
  8507. FDDFileList->AddObject(TransferFileName, File);
  8508. }
  8509. //---------------------------------------------------------------------------
  8510. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDDragDetect(
  8511. TObject * /*Sender*/, int /*grfKeyState*/, const TPoint & /*DetectStart*/,
  8512. const TPoint & /*Point*/, TDragDetectStatus /*DragStatus*/)
  8513. {
  8514. // sometimes we do not get DDEnd so the list is not released
  8515. SAFE_DESTROY(FDDFileList);
  8516. FDDFileList = new TStringList();
  8517. FDragTempDir = WinConfiguration->TemporaryDir();
  8518. FDDTotalSize = 0;
  8519. FDragDropSshTerminate.reset(NULL);
  8520. }
  8521. //---------------------------------------------------------------------------
  8522. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDQueryContinueDrag(
  8523. TObject * /*Sender*/, LongBool /*FEscapePressed*/, int /*grfKeyState*/,
  8524. HRESULT & Result)
  8525. {
  8526. if (Result == DRAGDROP_S_DROP)
  8527. {
  8528. try
  8529. {
  8530. GlobalDragImageList->HideDragImage();
  8531. try
  8532. {
  8533. RemoteFileControlDDTargetDrop();
  8534. }
  8535. catch(ESshTerminate & E)
  8536. {
  8537. FDragDropSshTerminate.reset(E.Clone());
  8538. }
  8539. }
  8540. catch (Exception &E)
  8541. {
  8542. // If downloading fails we need to cancel drag&drop, otherwise
  8543. // Explorer shows error
  8544. // But by the way exception probably never reach this point as
  8545. // it's catched on way.
  8546. // Fatal exceptions get here (like when opening a secondary shell extension for file duplication fails).
  8547. Result = DRAGDROP_S_CANCEL;
  8548. ShowExtendedException(Terminal, &E);
  8549. }
  8550. }
  8551. }
  8552. //---------------------------------------------------------------------------
  8553. void __fastcall TCustomScpExplorerForm::DirViewMatchMask(
  8554. TObject * /*Sender*/, UnicodeString FileName, bool Directory, __int64 Size,
  8555. TDateTime Modification, UnicodeString Masks, bool & Matches, bool AllowImplicitMatches)
  8556. {
  8557. TFileMasks::TParams MaskParams;
  8558. MaskParams.Size = Size;
  8559. MaskParams.Modification = Modification;
  8560. // this does not re-parse the mask if it is the same as the last time
  8561. FDirViewMatchMask = Masks;
  8562. bool ImplicitMatch;
  8563. Matches =
  8564. // Local and RecurseInclude parameters have no effect as there's no path.
  8565. // This should use potential MatchesFileName overload with ImplicitMatch argument.
  8566. FDirViewMatchMask.Matches(FileName, false, Directory, &MaskParams, true, ImplicitMatch) &&
  8567. (AllowImplicitMatches || !ImplicitMatch);
  8568. }
  8569. //---------------------------------------------------------------------------
  8570. void __fastcall TCustomScpExplorerForm::DirViewGetOverlay(
  8571. TObject * Sender, TListItem * Item, WORD & Indexes)
  8572. {
  8573. TCustomDirView * DirView = reinterpret_cast<TCustomDirView *>(Sender);
  8574. if (GetPartialFileExtLen(DirView->ItemFileName(Item)) > 0)
  8575. {
  8576. Indexes |= oiPartial;
  8577. }
  8578. }
  8579. //---------------------------------------------------------------------------
  8580. bool __fastcall TCustomScpExplorerForm::CanPasteToDirViewFromClipBoard()
  8581. {
  8582. return
  8583. DirViewEnabled(osCurrent) &&
  8584. DirView(osCurrent)->CanPasteFromClipBoard();
  8585. }
  8586. //---------------------------------------------------------------------------
  8587. bool __fastcall TCustomScpExplorerForm::CanPasteFromClipBoard()
  8588. {
  8589. bool Result = false;
  8590. if (CanPasteToDirViewFromClipBoard())
  8591. {
  8592. Result = true;
  8593. }
  8594. else
  8595. {
  8596. UnicodeString ClipboardText;
  8597. if (NonEmptyTextFromClipboard(ClipboardText) &&
  8598. (ClipboardText.Pos(L"\n") == 0)) // it's already trimmed
  8599. {
  8600. if (StoredSessions->IsUrl(ClipboardText))
  8601. {
  8602. Result = true;
  8603. }
  8604. else
  8605. {
  8606. Result = DirViewEnabled(osCurrent);
  8607. }
  8608. }
  8609. }
  8610. return Result;
  8611. }
  8612. //---------------------------------------------------------------------------
  8613. void __fastcall TCustomScpExplorerForm::PasteFromClipBoard()
  8614. {
  8615. // Pasting to any local panel is handled in TScpCommanderForm::PasteFromClipBoard()
  8616. if (DoesClipboardContainOurFiles())
  8617. {
  8618. if (DebugAlwaysTrue(CanPasteToDirViewFromClipBoard()))
  8619. {
  8620. DebugAssert(!IsLocalBrowserMode());
  8621. TTerminalManager * Manager = TTerminalManager::Instance();
  8622. TTerminal * TargetTerminal = Manager->ActiveTerminal;
  8623. Manager->ActiveSession = FClipboardTerminal;
  8624. ExecuteFileOperation(foRemoteCopy, osRemote, FClipboardFileList.get(), false, TargetTerminal);
  8625. }
  8626. }
  8627. else if (CanPasteToDirViewFromClipBoard())
  8628. {
  8629. DirView(osCurrent)->PasteFromClipBoard();
  8630. }
  8631. else
  8632. {
  8633. UnicodeString ClipboardText;
  8634. if (NonEmptyTextFromClipboard(ClipboardText))
  8635. {
  8636. if (StoredSessions->IsUrl(ClipboardText))
  8637. {
  8638. NewSession(ClipboardText);
  8639. }
  8640. else
  8641. {
  8642. DirView(osCurrent)->Path = ClipboardText;
  8643. }
  8644. }
  8645. }
  8646. }
  8647. //---------------------------------------------------------------------------
  8648. void __fastcall TCustomScpExplorerForm::FileListFromClipboard()
  8649. {
  8650. // TBD
  8651. }
  8652. //---------------------------------------------------------------------------
  8653. void __fastcall TCustomScpExplorerForm::SelectAll(TOperationSide Side, TSelectMode Mode)
  8654. {
  8655. TCustomDirView * ADirView = DirView(Side);
  8656. ADirView->SelectAll(Mode);
  8657. ADirView->SetFocus();
  8658. }
  8659. //---------------------------------------------------------------------------
  8660. void __fastcall TCustomScpExplorerForm::SelectByMask(TOperationSide Side, bool Select)
  8661. {
  8662. TCustomDirView * ADirView = DirView(Side);
  8663. TFileFilter Filter;
  8664. DefaultFileFilter(Filter);
  8665. if (DoSelectMaskDialog(ADirView, Select, Filter))
  8666. {
  8667. Configuration->Usage->Inc(L"MaskSelections");
  8668. ADirView->SelectFiles(Filter, Select);
  8669. ADirView->SetFocus();
  8670. }
  8671. }
  8672. //---------------------------------------------------------------------------
  8673. void __fastcall TCustomScpExplorerForm::RestoreSelectedNames(TOperationSide Side)
  8674. {
  8675. TCustomDirView * ADirView = DirView(Side);
  8676. ADirView->RestoreSelectedNames();
  8677. ADirView->SetFocus();
  8678. }
  8679. //---------------------------------------------------------------------------
  8680. void __fastcall TCustomScpExplorerForm::SelectSameExt(bool Select)
  8681. {
  8682. TCustomDirView * CurrentDirView = DirView(osCurrent);
  8683. if (DebugAlwaysTrue(CurrentDirView->ItemFocused != NULL))
  8684. {
  8685. UnicodeString FileName = CurrentDirView->ItemFileName(CurrentDirView->ItemFocused);
  8686. UnicodeString Ext;
  8687. if (!IsSideLocalBrowser(osCurrent))
  8688. {
  8689. Ext = UnixExtractFileExt(FileName);
  8690. }
  8691. else
  8692. {
  8693. Ext = ExtractFileExt(FileName);
  8694. }
  8695. if (Ext.IsEmpty())
  8696. {
  8697. Ext = L".";
  8698. }
  8699. TFileFilter Filter;
  8700. Filter.Masks = FORMAT(L"*%s", (Ext));
  8701. Filter.Directories = false;
  8702. CurrentDirView->SelectFiles(Filter, Select);
  8703. }
  8704. }
  8705. //---------------------------------------------------------------------------
  8706. UnicodeString __fastcall TCustomScpExplorerForm::FileStatusBarText(
  8707. const TStatusFileInfo & FileInfo, TOperationSide Side)
  8708. {
  8709. UnicodeString Result;
  8710. if (FIncrementalSearchState.Searching && (Side == GetSide(osCurrent)))
  8711. {
  8712. Result = FormatIncrementalSearchStatus(FIncrementalSearchState);
  8713. }
  8714. else if (!IsSideLocalBrowser(Side) && ((Terminal == NULL) || Terminal->Disconnected))
  8715. {
  8716. // noop
  8717. }
  8718. else
  8719. {
  8720. Result =
  8721. FMTLOAD(FILE_INFO_FORMAT,
  8722. (FormatBytes(FileInfo.SelectedSize),
  8723. FormatBytes(FileInfo.FilesSize),
  8724. FormatNumber(FileInfo.SelectedCount),
  8725. FormatNumber(FileInfo.FilesCount)));
  8726. }
  8727. return Result;
  8728. }
  8729. //---------------------------------------------------------------------------
  8730. void __fastcall TCustomScpExplorerForm::FileStatusBarPanelClick(
  8731. TTBXStatusPanel * Panel, TOperationSide Side)
  8732. {
  8733. if (Panel->Index == 1)
  8734. {
  8735. ToggleShowHiddenFiles();
  8736. }
  8737. else if (Panel->Index == 2)
  8738. {
  8739. Filter(Side);
  8740. }
  8741. }
  8742. //---------------------------------------------------------------------------
  8743. void __fastcall TCustomScpExplorerForm::UpdateFileStatusBar(
  8744. TTBXStatusBar * StatusBar, const TStatusFileInfo & FileInfo, TOperationSide Side)
  8745. {
  8746. DebugAssert(!StatusBar->SimplePanel);
  8747. StatusBar->Panels->Items[0]->Caption = FileStatusBarText(FileInfo, Side);
  8748. UpdateFileStatusExtendedPanels(StatusBar, FileInfo);
  8749. }
  8750. //---------------------------------------------------------------------------
  8751. void __fastcall TCustomScpExplorerForm::UpdateFileStatusExtendedPanels(
  8752. TTBXStatusBar * StatusBar, const TStatusFileInfo & FileInfo)
  8753. {
  8754. DebugAssert(StatusBar->Panels->Count >= 3);
  8755. TTBXStatusPanel * HiddenFilesPanel = StatusBar->Panels->Items[1];
  8756. if (FileInfo.HiddenCount > 0)
  8757. {
  8758. HiddenFilesPanel->Caption = FMTLOAD(FILE_INFO_HIDDEN2, (FormatNumber(FileInfo.HiddenCount)));
  8759. HiddenFilesPanel->ViewPriority = 90; // <100 allows hiding panel when it does not fit
  8760. }
  8761. else
  8762. {
  8763. HiddenFilesPanel->ViewPriority = 0;
  8764. // not really necessary, just to cleanup no-longer-valid data
  8765. HiddenFilesPanel->Caption = L"";
  8766. }
  8767. TTBXStatusPanel * FilteredFilesPanel = StatusBar->Panels->Items[2];
  8768. if (FileInfo.FilteredCount > 0)
  8769. {
  8770. FilteredFilesPanel->Caption = FMTLOAD(FILE_INFO_FILTERED2, (FormatNumber(FileInfo.FilteredCount)));
  8771. FilteredFilesPanel->ViewPriority = 90; // <100 allows hiding panel when it does not fit
  8772. }
  8773. else
  8774. {
  8775. FilteredFilesPanel->ViewPriority = 0;
  8776. // not really necessary, just to cleanup no-longer-valid data
  8777. FilteredFilesPanel->Caption = L"";
  8778. }
  8779. }
  8780. //---------------------------------------------------------------------------
  8781. void __fastcall TCustomScpExplorerForm::RemoteStatusBarClick(
  8782. TObject * /*Sender*/)
  8783. {
  8784. if (DirView(osOther)->Enabled)
  8785. {
  8786. DirView(osOther)->SetFocus();
  8787. }
  8788. }
  8789. //---------------------------------------------------------------------------
  8790. void __fastcall TCustomScpExplorerForm::ToggleQueueVisibility()
  8791. {
  8792. TQueueViewConfiguration Config = WinConfiguration->QueueView;
  8793. switch (Config.Show)
  8794. {
  8795. case qvShow:
  8796. if ((FQueueStatus != NULL) && (FQueueStatus->Count > 0))
  8797. {
  8798. Config.Show = qvHide;
  8799. }
  8800. else
  8801. {
  8802. Config.Show = Config.LastHideShow;
  8803. }
  8804. break;
  8805. case qvHideWhenEmpty:
  8806. if (ComponentVisible[fcQueueView])
  8807. {
  8808. Config.Show = qvHide;
  8809. }
  8810. else
  8811. {
  8812. Config.LastHideShow = Config.Show;
  8813. Config.Show = qvShow;
  8814. }
  8815. break;
  8816. case qvHide:
  8817. Config.LastHideShow = Config.Show;
  8818. Config.Show = qvShow;
  8819. break;
  8820. }
  8821. WinConfiguration->QueueView = Config;
  8822. }
  8823. //---------------------------------------------------------------------------
  8824. UnicodeString __fastcall TCustomScpExplorerForm::PathForCaption()
  8825. {
  8826. UnicodeString Result;
  8827. if (Terminal != NULL)
  8828. {
  8829. switch (WinConfiguration->PathInCaption)
  8830. {
  8831. case picShort:
  8832. Result = ExtractShortName(Terminal->CurrentDirectory, true);
  8833. break;
  8834. case picFull:
  8835. Result = Terminal->CurrentDirectory;
  8836. break;
  8837. }
  8838. }
  8839. return Result;
  8840. }
  8841. //---------------------------------------------------------------------------
  8842. TColor __fastcall TCustomScpExplorerForm::PanelColor()
  8843. {
  8844. TColor Result = GetWindowColor(FSessionColor);
  8845. return Result;
  8846. }
  8847. //---------------------------------------------------------------------------
  8848. TColor __fastcall TCustomScpExplorerForm::DisabledPanelColor()
  8849. {
  8850. TColor Result = (WinConfiguration->UseDarkTheme() ? static_cast<TColor>(RGB(0x40, 0x40, 0x40)) : clBtnFace);
  8851. return Result;
  8852. }
  8853. //---------------------------------------------------------------------------
  8854. void TCustomScpExplorerForm::UpdatePanelControls(TCustomDirView * ADirView, TCustomDriveView * ADriveView)
  8855. {
  8856. bool UseDarkTheme = WinConfiguration->UseDarkTheme();
  8857. ADirView->DarkMode = UseDarkTheme;
  8858. ADriveView->DarkMode = UseDarkTheme;
  8859. }
  8860. //---------------------------------------------------------------------------
  8861. void __fastcall TCustomScpExplorerForm::UpdateControls()
  8862. {
  8863. if (!FSessionChanging)
  8864. {
  8865. TTerminalManager::Instance()->UpdateAppTitle();
  8866. }
  8867. // WORKAROUND: Disabling list view when it is not showing yet does not set its
  8868. // background to gray on Windows 7 (works on Windows 10).
  8869. // See also EnableControl
  8870. if (Showing)
  8871. {
  8872. if (ManagedSession != NULL)
  8873. {
  8874. // Update path when it changes
  8875. if ((SessionsPageControl->ActivePage != NULL) &&
  8876. (GetSessionTabSession(SessionsPageControl->ActivePage) == ManagedSession))
  8877. {
  8878. UpdateSessionTab(SessionsPageControl->ActivePage);
  8879. }
  8880. }
  8881. NonVisualDataModule->ReconnectSessionAction->Update();
  8882. ReconnectToolbar->Visible = NonVisualDataModule->ReconnectSessionAction->Visible;
  8883. // ReconnectSessionAction is hidden when disabled, so enabling it actualy resizes the toolbar
  8884. CenterReconnectToolbar();
  8885. bool HasTerminal = HasActiveTerminal();
  8886. RemoteDirView->Enabled = HasTerminal;
  8887. RemoteDirView->Color = HasTerminal ? PanelColor() : DisabledPanelColor();
  8888. RemoteDirView->Font->Color = GetWindowTextColor(RemoteDirView->Color);
  8889. RemoteDriveView->Enabled = RemoteDirView->Enabled;
  8890. RemoteDriveView->Color = RemoteDirView->Color;
  8891. RemoteDriveView->Font->Color = RemoteDirView->Font->Color;
  8892. QueueView3->Enabled = HasTerminal && Terminal->IsCapable[fcBackgroundTransfers];
  8893. QueueView3->Color = QueueView3->Enabled ? GetWindowColor() : DisabledPanelColor();
  8894. QueueView3->Font->Color = GetWindowTextColor(QueueView3->Color);
  8895. QueueFileList->Enabled = QueueView3->Enabled;
  8896. QueueFileList->Color = QueueView3->Color;
  8897. QueueFileList->Font->Color = QueueView3->Font->Color;
  8898. QueueLabelUpdateStatus();
  8899. UpdatePanelControls(RemoteDirView, RemoteDriveView);
  8900. bool UseDarkTheme = WinConfiguration->UseDarkTheme();
  8901. if (FImmersiveDarkMode != UseDarkTheme)
  8902. {
  8903. UpdateDarkMode();
  8904. }
  8905. SessionsPageControl->TabTheme = UseDarkTheme ? CurrentTheme : NULL;
  8906. UnicodeString CurrentHiContrastThemeName = (FHiContrastTheme != NULL) ? FHiContrastTheme->Name : EmptyStr;
  8907. bool HiContrast = WinConfiguration->HiContrast;
  8908. UnicodeString NewHiContrastThemeName = HiContrast ? GetThemeName(!UseDarkTheme) : EmptyStr;
  8909. if (CurrentHiContrastThemeName != NewHiContrastThemeName)
  8910. {
  8911. ReleaseHiContrastTheme();
  8912. if (!NewHiContrastThemeName.IsEmpty())
  8913. {
  8914. FHiContrastTheme = GetTBXTheme(NewHiContrastThemeName);
  8915. }
  8916. }
  8917. SessionsPageControl->ActiveTabTheme = HiContrast ? DebugNotNull(FHiContrastTheme) : NULL;
  8918. reinterpret_cast<TTBCustomItem *>(GetComponent(fcRemotePathComboBox))->Enabled = HasTerminal || IsLocalBrowserMode();
  8919. MessageDock->Visible = (MessageDock->ToolbarCount > 0);
  8920. }
  8921. }
  8922. //---------------------------------------------------------------------------
  8923. void __fastcall TCustomScpExplorerForm::DoDirViewLoaded(TCustomDirView * /*Sender*/)
  8924. {
  8925. UpdateControls();
  8926. }
  8927. //---------------------------------------------------------------------------
  8928. void __fastcall TCustomScpExplorerForm::DirViewLoaded(
  8929. TObject * Sender)
  8930. {
  8931. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  8932. DebugAssert(DirView != NULL);
  8933. DoDirViewLoaded(DirView);
  8934. TransferPresetAutoSelect();
  8935. TUnixDirView * UnixDirView = dynamic_cast<TUnixDirView *>(DirView);
  8936. if (UnixDirView != NULL)
  8937. {
  8938. TManagedTerminal * ATerminal = DebugNotNull(dynamic_cast<TManagedTerminal *>(UnixDirView->Terminal));
  8939. TTerminalManager::Instance()->TerminalLoadedDirectory(ATerminal);
  8940. }
  8941. }
  8942. //---------------------------------------------------------------------------
  8943. void __fastcall TCustomScpExplorerForm::StartUpdates()
  8944. {
  8945. TUpdatesConfiguration Updates = WinConfiguration->Updates;
  8946. // first run after installation
  8947. if (double(Updates.LastCheck) == 0)
  8948. {
  8949. AppLog(L"First run, scheduling updates check for the next run");
  8950. // make sure next time there will be an update (if enabled)
  8951. Updates.LastCheck = TDateTime(1);
  8952. WinConfiguration->Updates = Updates;
  8953. }
  8954. else
  8955. {
  8956. TDateTime Period;
  8957. if (IsUWP() && !IsOfficialPackage())
  8958. {
  8959. Period = DefaultUpdatesPeriod;
  8960. AppLog(L"Thirdparty UWP package, using default updates check period");
  8961. }
  8962. else
  8963. {
  8964. Period = Updates.Period;
  8965. }
  8966. AppLogFmt(L"Updates check period: %.2f", (double(Period)));
  8967. if (double(Period) > 0)
  8968. {
  8969. TDateTime Interval = Now() - Updates.LastCheck;
  8970. AppLogFmt(L"Interval since the last updates check: %.2f", (double(Interval)));
  8971. if (Interval >= Period)
  8972. {
  8973. StartUpdateThread(UpdatesChecked);
  8974. }
  8975. }
  8976. }
  8977. }
  8978. //---------------------------------------------------------------------------
  8979. void __fastcall TCustomScpExplorerForm::UpdatesChecked()
  8980. {
  8981. AppLog(L"Updates check done");
  8982. UnicodeString Message;
  8983. bool New;
  8984. TQueryType Type;
  8985. GetUpdatesMessage(Message, New, Type, false);
  8986. if (!Message.IsEmpty())
  8987. {
  8988. AppLogFmt(L"Updates check message: %s", (Message));
  8989. if (New)
  8990. {
  8991. AppLog(L"New version detected");
  8992. Message = FMTLOAD(NEW_VERSION_CLICK, (Message));
  8993. }
  8994. if (!New && (Type != qtWarning))
  8995. {
  8996. AppLog(L"Posting non-critical updates note");
  8997. PostNote(UnformatMessage(Message), 0, UpdatesNoteClicked);
  8998. }
  8999. else
  9000. {
  9001. AppLog(L"Posting new version or critical message notification");
  9002. Notify(NULL, Message, Type, true, UpdatesNoteClicked);
  9003. }
  9004. if (New)
  9005. {
  9006. Configuration->Usage->Inc(L"UpdateNotifications");
  9007. }
  9008. }
  9009. }
  9010. //---------------------------------------------------------------------------
  9011. void __fastcall TCustomScpExplorerForm::UpdatesNoteClicked(TObject * /*Sender*/)
  9012. {
  9013. RestoreApp();
  9014. bool CanDisplay = !NonVisualDataModule->Busy;
  9015. if (!CanDisplay && (Screen->ActiveForm != NULL))
  9016. {
  9017. CanDisplay = (Screen->ActiveForm->Perform(WM_CAN_DISPLAY_UPDATES, 0, 0) != 0);
  9018. }
  9019. if (CanDisplay)
  9020. {
  9021. Configuration->Usage->Inc(L"UpdateNotificationsClicked");
  9022. // Probably not needed as CheckForUpdates is blocking since we started using neon for HTTP implementation.
  9023. NonVisualDataModule->StartBusy();
  9024. try
  9025. {
  9026. CheckForUpdates(true);
  9027. }
  9028. __finally
  9029. {
  9030. NonVisualDataModule->EndBusy();
  9031. }
  9032. }
  9033. else
  9034. {
  9035. Configuration->Usage->Inc(L"UpdateNotificationsBusy");
  9036. }
  9037. }
  9038. //---------------------------------------------------------------------------
  9039. void __fastcall TCustomScpExplorerForm::GetTransferPresetAutoSelectData(
  9040. TCopyParamRuleData & Data)
  9041. {
  9042. DebugAssert(Terminal != NULL);
  9043. Data.HostName = Terminal->SessionData->HostNameExpanded;
  9044. Data.UserName = Terminal->SessionData->UserNameExpanded;
  9045. Data.RemoteDirectory = RemoteDirView->PathName;
  9046. }
  9047. //---------------------------------------------------------------------------
  9048. void __fastcall TCustomScpExplorerForm::TransferPresetAutoSelect()
  9049. {
  9050. // Terminal can be null when we are changing local directory implicitly,
  9051. // because it has been deleted, while no session is connected
  9052. // (Login dialog is open)
  9053. if (FAllowTransferPresetAutoSelect && (Terminal != NULL))
  9054. {
  9055. DebugAssert(!IsLocalBrowserMode());
  9056. TCopyParamRuleData Data;
  9057. GetTransferPresetAutoSelectData(Data);
  9058. int CopyParamIndex = GUIConfiguration->CopyParamList->Find(Data);
  9059. UnicodeString CopyParamCurrent = GUIConfiguration->CopyParamCurrent;
  9060. if (CopyParamIndex < 0)
  9061. {
  9062. // there is no preset that matches autoselection
  9063. // set preset that we consider "default"
  9064. FCopyParamAutoSelected = L""; // forget last autoselected preset
  9065. GUIConfiguration->CopyParamCurrent = FCopyParamDefault;
  9066. }
  9067. else
  9068. {
  9069. // there is preset matching autoselection
  9070. UnicodeString CopyParamName = GUIConfiguration->CopyParamList->Names[CopyParamIndex];
  9071. if (CopyParamName == FCopyParamAutoSelected)
  9072. {
  9073. // autoselected the same preset as the last time
  9074. // make no change (i.e. preserve custom user preset, if any)
  9075. }
  9076. else
  9077. {
  9078. // autoselected the different preset then the last time (or there
  9079. // was default preset set)
  9080. FCopyParamAutoSelected = CopyParamName; // remember autoselection
  9081. GUIConfiguration->CopyParamCurrent = CopyParamName;
  9082. }
  9083. }
  9084. if (GUIConfiguration->CopyParamCurrent != CopyParamCurrent)
  9085. {
  9086. if (CopyParamIndex >= 0)
  9087. {
  9088. Configuration->Usage->Inc(L"CopyParamAutoSelects");
  9089. }
  9090. TTransferPresetNoteData * Data = new TTransferPresetNoteData;
  9091. try
  9092. {
  9093. int Fmt =
  9094. (CopyParamIndex < 0) ?
  9095. (GUIConfiguration->CopyParamIndex < 0 ? COPY_PARAM_DEFAULT_NORM : COPY_PARAM_DEFAULT_CUSTOM) :
  9096. COPY_PARAM_AUTOSELECTED;
  9097. UnicodeString Message = FMTLOAD(Fmt, (StripHotkey(GUIConfiguration->CopyParamCurrent)));
  9098. Data->Message = MainInstructions(Message);
  9099. int CopyParamAttrs = Terminal->UsableCopyParamAttrs(0).General;
  9100. UnicodeString Info = GUIConfiguration->CurrentCopyParam.GetInfoStr(L"\n",
  9101. CopyParamAttrs);
  9102. if (CopyParamIndex >= 0)
  9103. {
  9104. DebugAssert(GUIConfiguration->CopyParamList->Rules[CopyParamIndex] != NULL);
  9105. Info = FORMAT(L"%s\n \n%s", (Info,
  9106. FMTLOAD(COPY_PARAM_RULE,
  9107. (GUIConfiguration->CopyParamList->Rules[CopyParamIndex]->GetInfoStr(L"\n")))));
  9108. }
  9109. Data->Message += L"\n\n" + Info;
  9110. if (WinConfiguration->CopyParamAutoSelectNotice)
  9111. {
  9112. TransferPresetNoteMessage(Data, true);
  9113. }
  9114. else
  9115. {
  9116. PostNote(Message, 0, TransferPresetNoteClicked, Data);
  9117. Data = NULL; // ownership passed
  9118. }
  9119. }
  9120. __finally
  9121. {
  9122. delete Data;
  9123. }
  9124. }
  9125. }
  9126. }
  9127. //---------------------------------------------------------------------------
  9128. void __fastcall TCustomScpExplorerForm::TransferPresetNoteMessage(
  9129. TTransferPresetNoteData * NoteData, bool AllowNeverAskAgain)
  9130. {
  9131. DebugAssert(NoteData != NULL);
  9132. DebugAssert(!IsLocalBrowserMode());
  9133. TMessageParams Params(AllowNeverAskAgain ? mpNeverAskAgainCheck : 0);
  9134. TQueryButtonAlias Aliases[1];
  9135. Aliases[0].Button = qaIgnore; // "ignore" is after "ok"
  9136. Aliases[0].Alias = LoadStr(CONFIGURE_BUTTON);
  9137. Params.Aliases = Aliases;
  9138. Params.AliasesCount = LENOF(Aliases);
  9139. unsigned int Result =
  9140. MoreMessageDialog(NoteData->Message, NULL, qtInformation,
  9141. qaOK | qaIgnore, HELP_COPY_PARAM_AUTOSELECTED, &Params);
  9142. switch (Result)
  9143. {
  9144. case qaNeverAskAgain:
  9145. DebugAssert(AllowNeverAskAgain);
  9146. WinConfiguration->CopyParamAutoSelectNotice = false;
  9147. break;
  9148. case qaIgnore:
  9149. PreferencesDialog(pmPresets);
  9150. break;
  9151. }
  9152. }
  9153. //---------------------------------------------------------------------------
  9154. void __fastcall TCustomScpExplorerForm::TransferPresetNoteClicked(TObject * Sender)
  9155. {
  9156. // as of now useless, as this is used for notes only, never for balloons, ...
  9157. RestoreApp();
  9158. // .. and we should never be busy here
  9159. if (DebugAlwaysTrue(!NonVisualDataModule->Busy))
  9160. {
  9161. TransferPresetNoteMessage(DebugNotNull(dynamic_cast<TTransferPresetNoteData *>(Sender)), false);
  9162. }
  9163. }
  9164. //---------------------------------------------------------------------------
  9165. void __fastcall TCustomScpExplorerForm::PreferencesDialog(
  9166. TPreferencesMode APreferencesMode)
  9167. {
  9168. std::unique_ptr<TPreferencesDialogData> PreferencesData;
  9169. TCopyParamRuleData Data;
  9170. if (Terminal != NULL)
  9171. {
  9172. PreferencesData.reset(new TPreferencesDialogData());
  9173. GetTransferPresetAutoSelectData(Data);
  9174. PreferencesData->CopyParamRuleData = &Data;
  9175. }
  9176. DoPreferencesDialog(APreferencesMode, PreferencesData.get());
  9177. }
  9178. //---------------------------------------------------------------------------
  9179. void __fastcall TCustomScpExplorerForm::AdHocCustomCommandValidate(
  9180. const TCustomCommandType & Command)
  9181. {
  9182. if (CustomCommandState(Command, FEditingFocusedAdHocCommand, ccltAll) <= 0)
  9183. {
  9184. throw Exception(FMTLOAD(CUSTOM_COMMAND_IMPOSSIBLE2, (Command.Command)));
  9185. }
  9186. }
  9187. //---------------------------------------------------------------------------
  9188. void __fastcall TCustomScpExplorerForm::AdHocCustomCommand(bool OnFocused)
  9189. {
  9190. bool RemoteAllowed = CustomCommandRemoteAllowed();
  9191. TCustomCommandType Command;
  9192. // make sure we use local custom command when remote are not supported
  9193. if (RemoteAllowed || FLAGSET(FLastCustomCommand.Params, ccLocal))
  9194. {
  9195. Command = FLastCustomCommand;
  9196. }
  9197. else
  9198. {
  9199. Command.Params = Command.Params | ccLocal;
  9200. }
  9201. Command.Name = LoadStr(CUSTOM_COMMAND_AD_HOC_NAME);
  9202. FEditingFocusedAdHocCommand = OnFocused;
  9203. bool LocalSide = IsSideLocalBrowser(FCurrentSide);
  9204. int Options =
  9205. FLAGMASK((!RemoteAllowed || LocalSide), ccoDisableRemote) |
  9206. FLAGMASK(LocalSide, ccoDisableRemoteFiles);
  9207. if (DoCustomCommandDialog(Command, WinConfiguration->CustomCommandList,
  9208. ccmAdHoc, Options, AdHocCustomCommandValidate, NULL))
  9209. {
  9210. FLastCustomCommand = Command;
  9211. UpdateCustomCommandsToolbar();
  9212. ExecuteFileOperation(foCustomCommand, osRemote, OnFocused, false, &Command);
  9213. }
  9214. }
  9215. //---------------------------------------------------------------------------
  9216. void __fastcall TCustomScpExplorerForm::LastCustomCommand(bool OnFocused)
  9217. {
  9218. DebugAssert(!FLastCustomCommand.Command.IsEmpty());
  9219. int State = CustomCommandState(FLastCustomCommand, OnFocused, ccltAll);
  9220. DebugAssert(State > 0);
  9221. if (State <= 0)
  9222. {
  9223. throw Exception(FMTLOAD(CUSTOM_COMMAND_IMPOSSIBLE2, (FLastCustomCommand.Command)));
  9224. }
  9225. ExecuteFileOperation(foCustomCommand, osRemote, OnFocused, false, &FLastCustomCommand);
  9226. }
  9227. //---------------------------------------------------------------------------
  9228. bool __fastcall TCustomScpExplorerForm::GetLastCustomCommand(bool OnFocused,
  9229. TCustomCommandType & Command, int & State)
  9230. {
  9231. bool Result = !FLastCustomCommand.Command.IsEmpty();
  9232. if (Result)
  9233. {
  9234. Command = FLastCustomCommand;
  9235. State = CustomCommandState(FLastCustomCommand, OnFocused, ccltAll);
  9236. }
  9237. return Result;
  9238. }
  9239. //---------------------------------------------------------------------------
  9240. void __fastcall TCustomScpExplorerForm::BeforeAction()
  9241. {
  9242. ResetIncrementalSearch();
  9243. }
  9244. //---------------------------------------------------------------------------
  9245. void __fastcall TCustomScpExplorerForm::PostComponentHide(Byte Component)
  9246. {
  9247. DebugAssert(ComponentVisible[Component]);
  9248. PostMessage(Handle, WM_COMPONENT_HIDE, Component, 0);
  9249. }
  9250. //---------------------------------------------------------------------------
  9251. void __fastcall TCustomScpExplorerForm::QueueSplitterDblClick(TObject * /*Sender*/)
  9252. {
  9253. // when queue panel is resized here directly, the status bar is stretched
  9254. // over whole space the panel occupied
  9255. PostComponentHide(fcQueueView);
  9256. }
  9257. //---------------------------------------------------------------------------
  9258. void __fastcall TCustomScpExplorerForm::QueueFileListSplitterDblClick(TObject *)
  9259. {
  9260. ComponentVisible[fcQueueFileList] = false;
  9261. }
  9262. //---------------------------------------------------------------------------
  9263. void __fastcall TCustomScpExplorerForm::ThemeChanged()
  9264. {
  9265. // We hoped this will refresh scrollbar colors, but it does not have any effect here.
  9266. RefreshColorMode();
  9267. ResetSysDarkTheme();
  9268. ConfigurationChanged();
  9269. ConfigureInterface();
  9270. // To update references to recreated/destroyed themes in SessionsPageControl
  9271. UpdateControls();
  9272. // Should be called for all controls
  9273. RemoteDirView->Perform(WM_THEMECHANGED, 0, 0);
  9274. }
  9275. //---------------------------------------------------------------------------
  9276. void __fastcall TCustomScpExplorerForm::WMSettingChange(TMessage & Message)
  9277. {
  9278. // Do not handle, when shutting down anyway (maybe also when not setup completely yet?)
  9279. if (!FInvalid &&
  9280. (Message.LParam != 0) &&
  9281. (wcscmp(reinterpret_cast<LPCWCH>(Message.LParam), L"ImmersiveColorSet") == 0))
  9282. {
  9283. ThemeChanged();
  9284. }
  9285. TForm::Dispatch(&Message);
  9286. }
  9287. //---------------------------------------------------------------------------
  9288. void __fastcall TCustomScpExplorerForm::Dispatch(void * Message)
  9289. {
  9290. TMessage * M = static_cast<TMessage*>(Message);
  9291. switch (M->Msg)
  9292. {
  9293. case CM_DIALOGCHAR:
  9294. CMDialogChar(*M);
  9295. break;
  9296. case WM_APPCOMMAND:
  9297. WMAppCommand(*M);
  9298. break;
  9299. case WM_SYSCOMMAND:
  9300. WMSysCommand(*M);
  9301. break;
  9302. case WM_QUERYENDSESSION:
  9303. WMQueryEndSession(*M);
  9304. break;
  9305. case WM_ENDSESSION:
  9306. WMEndSession(*reinterpret_cast<TWMEndSession *>(M));
  9307. break;
  9308. #ifdef _DEBUG
  9309. case WM_WINDOWPOSCHANGED:
  9310. WMWindowPosChanged(*reinterpret_cast<TWMWindowPosMsg *>(M));
  9311. break;
  9312. case CM_CUSTOMSTYLECHANGED:
  9313. TForm::Dispatch(Message);
  9314. break;
  9315. #endif
  9316. case WM_COMPONENT_HIDE:
  9317. {
  9318. Byte Component = static_cast<Byte>(M->WParam);
  9319. // sanity check
  9320. if (ComponentVisible[Component])
  9321. {
  9322. // special treatment
  9323. if (Component == fcQueueView)
  9324. {
  9325. ToggleQueueVisibility();
  9326. DebugAssert(!ComponentVisible[fcQueueView]);
  9327. }
  9328. else
  9329. {
  9330. ComponentVisible[Component] = false;
  9331. }
  9332. }
  9333. }
  9334. break;
  9335. case WM_COPYDATA:
  9336. WMCopyData(*M);
  9337. break;
  9338. case WM_MANAGES_CAPTION:
  9339. // caption managed in TTerminalManager::UpdateAppTitle()
  9340. M->Result = 1;
  9341. break;
  9342. case WM_WANTS_MOUSEWHEEL:
  9343. M->Result = 1;
  9344. break;
  9345. case CM_SHOWINGCHANGED:
  9346. CMShowingChanged(*M);
  9347. break;
  9348. case WM_CLOSE:
  9349. WMClose(*M);
  9350. break;
  9351. case WM_DPICHANGED:
  9352. WMDpiChanged(*M);
  9353. break;
  9354. case WM_SETTINGCHANGE:
  9355. WMSettingChange(*M);
  9356. break;
  9357. case CM_DIALOGKEY:
  9358. CMDialogKey(*reinterpret_cast<TWMKeyDown *>(M));
  9359. break;
  9360. case WM_PASTE_FILES:
  9361. PasteFiles();
  9362. break;
  9363. case WM_IS_HIDDEN:
  9364. // To treat master password prompt as a main window while showing it when workspace is loading while
  9365. // the exporer is showing up (Visible is true already)
  9366. M->Result = !FShowing;
  9367. break;
  9368. case WM_SHOWWINDOW:
  9369. if (M->WParam)
  9370. {
  9371. AddStartupSequence(L"J");
  9372. }
  9373. TForm::Dispatch(Message);
  9374. break;
  9375. case WM_QUEUE_CALLBACK:
  9376. WMQueueCallback(*M);
  9377. break;
  9378. default:
  9379. TForm::Dispatch(Message);
  9380. break;
  9381. }
  9382. }
  9383. //---------------------------------------------------------------------------
  9384. void __fastcall TCustomScpExplorerForm::UpdateImages()
  9385. {
  9386. // noop
  9387. }
  9388. //---------------------------------------------------------------------------
  9389. void TCustomScpExplorerForm::RegenerateSessionColorsImageList()
  9390. {
  9391. FSessionColors->Clear();
  9392. AddFixedSessionImages();
  9393. ::RegenerateSessionColorsImageList(FSessionColors, FSessionColorMaskImageIndex);
  9394. UpdateImages();
  9395. }
  9396. //---------------------------------------------------------------------------
  9397. void __fastcall TCustomScpExplorerForm::WMDpiChanged(TMessage & Message)
  9398. {
  9399. TForm::Dispatch(&Message);
  9400. Configuration->Usage->Inc(L"PixelsPerInchChanged");
  9401. UpdatePixelsPerInchMainWindowCounter();
  9402. }
  9403. //---------------------------------------------------------------------------
  9404. void __fastcall TCustomScpExplorerForm::WMClose(TMessage & Message)
  9405. {
  9406. AppLog(L"Main window WM_CLOSE");
  9407. // Cannot close window while we are busy.
  9408. // We cannot test this in FormCloseQuery as that is called also from
  9409. // Close(), which is called by CloseApplicationAction. So we can be busy
  9410. // there already even, when it is legitimate to close the application.
  9411. // Possibly a better place to handle this would be WMSysCommand.
  9412. if (NonVisualDataModule->Busy)
  9413. {
  9414. Message.Result = 1;
  9415. }
  9416. else
  9417. {
  9418. TForm::Dispatch(&Message);
  9419. }
  9420. }
  9421. //---------------------------------------------------------------------------
  9422. void __fastcall TCustomScpExplorerForm::CMShowingChanged(TMessage & Message)
  9423. {
  9424. if (!FShowingChanged)
  9425. {
  9426. AddStartupSequence(L"D");
  9427. FShowingChanged = true;
  9428. }
  9429. // Ignoring implicit local-local browser of Commander, opened from DoShow
  9430. bool NoSession = (ManagedSession == NULL);
  9431. // This calls TCustomScpExplorerForm::DoShow
  9432. TForm::Dispatch(&Message);
  9433. // Now the window is visible (TForm::Dispatch is what usually takes long, like when loading a "local" network directory)
  9434. if (Showing && !FStarted)
  9435. {
  9436. FStarted = true;
  9437. InterfaceStarted();
  9438. }
  9439. if (Showing && NoSession)
  9440. {
  9441. // When we are starting minimized (i.e. from an installer),
  9442. // postpone showing Login dialog until we get restored.
  9443. // Otherwise the Login dialog (and Authentication window) show restored
  9444. // over invisible (minimized) main window.
  9445. if (WindowState == wsMinimized)
  9446. {
  9447. FNeedSession = true;
  9448. }
  9449. else
  9450. {
  9451. // This happens before application ever goes idle, so the toolbars would
  9452. // stay enabled (initial state) until the Login dialog is dismissed.
  9453. UpdateActions();
  9454. NonVisualDataModule->StartBusy();
  9455. try
  9456. {
  9457. // Need to process WM_ACTIVATEAPP before showing the Login dialog,
  9458. // otherwise the dialog does not receive focus.
  9459. // With Commander interface the ProcessMessages is called already
  9460. // by TDriveView, but with Explorer interface, we need to call it explicily
  9461. Application->ProcessMessages();
  9462. // do not reload sessions, they have been loaded just now (optimization)
  9463. NeedSession(true);
  9464. }
  9465. __finally
  9466. {
  9467. NonVisualDataModule->EndBusy();
  9468. }
  9469. }
  9470. }
  9471. }
  9472. //---------------------------------------------------------------------------
  9473. void __fastcall TCustomScpExplorerForm::CenterReconnectToolbar()
  9474. {
  9475. ReconnectToolbar->Left = (ReconnectToolbar->Parent->ClientWidth - ReconnectToolbar->Width) / 2;
  9476. ReconnectToolbar->Top = (ReconnectToolbar->Parent->ClientHeight - ReconnectToolbar->Height) / 2;
  9477. }
  9478. //---------------------------------------------------------------------------
  9479. void __fastcall TCustomScpExplorerForm::FormConstrainedResize(
  9480. TObject * /*Sender*/, int & MinWidth, int & MinHeight, int & MaxWidth,
  9481. int & MaxHeight)
  9482. {
  9483. // workaround for bug in TWinControl.CalcConstraints
  9484. // Check for empty rect (restore from iconic state) is done there only after
  9485. // call to AdjustClientRect, which enlarges the rect (for forms).
  9486. TRect R = GetClientRect();
  9487. // when restoring from iconic state, place no restrictions
  9488. if (IsRectEmpty(R))
  9489. {
  9490. MinWidth = 0;
  9491. MinHeight = 0;
  9492. MaxWidth = 32000;
  9493. MaxHeight = 32000;
  9494. }
  9495. }
  9496. //---------------------------------------------------------------------------
  9497. void __fastcall TCustomScpExplorerForm::GetSpaceAvailable(const UnicodeString Path,
  9498. TSpaceAvailable & ASpaceAvailable, bool & Close)
  9499. {
  9500. // terminal can be already closed (e.g. dropped connection)
  9501. if ((Terminal != NULL) && Terminal->IsCapable[fcCheckingSpaceAvailable])
  9502. {
  9503. Configuration->Usage->Inc(L"SpaceAvailableChecks");
  9504. try
  9505. {
  9506. Terminal->SpaceAvailable(Path, ASpaceAvailable);
  9507. }
  9508. catch(...)
  9509. {
  9510. if (!Terminal->Active)
  9511. {
  9512. Close = true;
  9513. }
  9514. throw;
  9515. }
  9516. }
  9517. }
  9518. //---------------------------------------------------------------------------
  9519. void __fastcall TCustomScpExplorerForm::FileSystemInfo()
  9520. {
  9521. const TSessionInfo & SessionInfo = Terminal->GetSessionInfo();
  9522. const TFileSystemInfo & FileSystemInfo = Terminal->GetFileSystemInfo(true);
  9523. TGetSpaceAvailable OnGetSpaceAvailable = NULL;
  9524. if (Terminal->IsCapable[fcCheckingSpaceAvailable])
  9525. {
  9526. OnGetSpaceAvailable = GetSpaceAvailable;
  9527. }
  9528. DoFileSystemInfoDialog(SessionInfo, FileSystemInfo, Terminal->CurrentDirectory,
  9529. OnGetSpaceAvailable);
  9530. }
  9531. //---------------------------------------------------------------------------
  9532. TSessionData * __fastcall TCustomScpExplorerForm::SessionDataForCode()
  9533. {
  9534. std::unique_ptr<TSessionData> Data(CloneCurrentSessionData());
  9535. Terminal->FillSessionDataForCode(Data.get());
  9536. return Data.release();
  9537. }
  9538. //---------------------------------------------------------------------------
  9539. void __fastcall TCustomScpExplorerForm::GenerateUrl(TStrings * Paths)
  9540. {
  9541. std::unique_ptr<TSessionData> Data(SessionDataForCode());
  9542. DoGenerateUrlDialog(Data.get(), Paths);
  9543. }
  9544. //---------------------------------------------------------------------------
  9545. void __fastcall TCustomScpExplorerForm::SessionGenerateUrl()
  9546. {
  9547. GenerateUrl(NULL);
  9548. }
  9549. //---------------------------------------------------------------------------
  9550. void __fastcall TCustomScpExplorerForm::FileGenerateUrl()
  9551. {
  9552. std::unique_ptr<TStrings> Paths(new TStringList());
  9553. DirView(osCurrent)->CreateFileList(false, true, Paths.get());
  9554. GenerateUrl(Paths.get());
  9555. }
  9556. //---------------------------------------------------------------------------
  9557. void __fastcall TCustomScpExplorerForm::UpdateSessionColor(TColor value)
  9558. {
  9559. FSessionColor = value;
  9560. TColor C = (value != 0 ? value : Vcl::Graphics::clNone);
  9561. TTBXColorItem * ColorItem = dynamic_cast<TTBXColorItem *>(
  9562. static_cast<TObject *>(GetComponent(fcColorMenu)));
  9563. DebugAssert(ColorItem != NULL);
  9564. ColorItem->Color = C;
  9565. NonVisualDataModule->ColorMenuItem->Color = C;
  9566. UpdateControls();
  9567. }
  9568. //---------------------------------------------------------------------------
  9569. void __fastcall TCustomScpExplorerForm::SetSessionColor(TColor value)
  9570. {
  9571. if (value != FSessionColor)
  9572. {
  9573. UpdateSessionColor(value);
  9574. // Is null when called from LastTerminalClosed
  9575. if (Terminal != NULL)
  9576. {
  9577. SessionsPageControl->ActivePage->ImageIndex = AddSessionColor(value);
  9578. }
  9579. }
  9580. }
  9581. //---------------------------------------------------------------------------
  9582. bool __fastcall TCustomScpExplorerForm::CancelNote(bool Force)
  9583. {
  9584. bool Result = FNoteTimer->Enabled;
  9585. if (Result)
  9586. {
  9587. // cannot cancel note too early
  9588. bool NotEarly =
  9589. (Now() - FNoteShown >
  9590. EncodeTimeVerbose(0, 0, (unsigned short)(WinConfiguration->NotificationsStickTime), 0));
  9591. if (Force || NotEarly)
  9592. {
  9593. FNoteTimer->Enabled = false;
  9594. FNote = L"";
  9595. SAFE_DESTROY(FNoteData);
  9596. FOnNoteClick = NULL;
  9597. FNoteHints = FNotes->Text;
  9598. FNoteHints.Delete(FNoteHints.Length() - 1, 2);
  9599. UpdateStatusBar();
  9600. }
  9601. }
  9602. return Result;
  9603. }
  9604. //---------------------------------------------------------------------------
  9605. void TCustomScpExplorerForm::CancelNote(const UnicodeString & Note)
  9606. {
  9607. if (FNote == Note)
  9608. {
  9609. CancelNote(true);
  9610. }
  9611. }
  9612. //---------------------------------------------------------------------------
  9613. void __fastcall TCustomScpExplorerForm::NoteTimer(TObject * /*Sender*/)
  9614. {
  9615. DebugAssert(FNoteTimer->Enabled);
  9616. CancelNote(true);
  9617. }
  9618. //---------------------------------------------------------------------------
  9619. void __fastcall TCustomScpExplorerForm::UpdateNoteHints()
  9620. {
  9621. FNoteHints = FNotes->Text.TrimRight();
  9622. }
  9623. //---------------------------------------------------------------------------
  9624. void __fastcall TCustomScpExplorerForm::AddNote(UnicodeString Note, bool UpdateNow)
  9625. {
  9626. int P = Note.Pos(L"\n");
  9627. if (P > 0)
  9628. {
  9629. Note.SetLength(P - 1);
  9630. }
  9631. FNotes->Add(FORMAT(L"[%s] %s",
  9632. (FormatDateTime(Configuration->TimeFormat, Now()), Note)));
  9633. while (FNotes->Count > 10)
  9634. {
  9635. FNotes->Delete(0);
  9636. }
  9637. if (UpdateNow)
  9638. {
  9639. UpdateNoteHints();
  9640. UpdateStatusBar();
  9641. }
  9642. }
  9643. //---------------------------------------------------------------------------
  9644. void TCustomScpExplorerForm::PostNote(
  9645. const UnicodeString & ANote, unsigned int Seconds, TNotifyEvent OnNoteClick, TObject * NoteData)
  9646. {
  9647. UnicodeString Note = ANote;
  9648. int P = Note.Pos(L"\n");
  9649. if (P > 0)
  9650. {
  9651. Note.SetLength(P - 1);
  9652. }
  9653. UpdateNoteHints();
  9654. FNote = Note;
  9655. SAFE_DESTROY(FNoteData);
  9656. FNoteData = NoteData;
  9657. FOnNoteClick = OnNoteClick;
  9658. AddNote(Note, false);
  9659. UpdateStatusBar();
  9660. FNoteShown = Now();
  9661. FNoteTimer->Enabled = false;
  9662. if (Seconds == 0)
  9663. {
  9664. Seconds = WinConfiguration->NotificationsTimeout;
  9665. }
  9666. FNoteTimer->Interval = Seconds * MSecsPerSec;
  9667. FNoteTimer->Enabled = true;
  9668. }
  9669. //---------------------------------------------------------------------------
  9670. void __fastcall TCustomScpExplorerForm::ReadDirectoryCancelled()
  9671. {
  9672. PostNote(LoadStr(DIRECTORY_READING_CANCELLED));
  9673. }
  9674. //---------------------------------------------------------------------------
  9675. void __fastcall TCustomScpExplorerForm::SynchronizeBrowsingChanged()
  9676. {
  9677. if (NonVisualDataModule->SynchronizeBrowsingAction2->Checked)
  9678. {
  9679. Configuration->Usage->Inc(L"SynchronizeBrowsingEnabled");
  9680. }
  9681. PostNote(FORMAT(LoadStrPart(SYNC_DIR_BROWSE_TOGGLE, 1),
  9682. (LoadStrPart(SYNC_DIR_BROWSE_TOGGLE,
  9683. (NonVisualDataModule->SynchronizeBrowsingAction2->Checked ? 2 : 3)))));
  9684. }
  9685. //---------------------------------------------------------------------------
  9686. void __fastcall TCustomScpExplorerForm::ToggleShowHiddenFiles()
  9687. {
  9688. bool NewShowHiddenFiles = !WinConfiguration->ShowHiddenFiles;
  9689. UnicodeString Note =
  9690. FORMAT(LoadStrPart(SHOW_HIDDEN_FILES_TOGGLE, 1), (LoadStrPart(SHOW_HIDDEN_FILES_TOGGLE, (NewShowHiddenFiles ? 2 : 3))));
  9691. PostNote(Note);
  9692. GetComponent(fcStatusBar)->Repaint(); // toggling ShowHiddenFiles takes time, force repaint beforehand
  9693. WinConfiguration->ShowHiddenFiles = NewShowHiddenFiles;
  9694. }
  9695. //---------------------------------------------------------------------------
  9696. void __fastcall TCustomScpExplorerForm::SetFormatSizeBytes(TFormatBytesStyle Style)
  9697. {
  9698. WinConfiguration->FormatSizeBytes = Style;
  9699. }
  9700. //---------------------------------------------------------------------------
  9701. void __fastcall TCustomScpExplorerForm::ToggleAutoReadDirectoryAfterOp()
  9702. {
  9703. Configuration->AutoReadDirectoryAfterOp = !Configuration->AutoReadDirectoryAfterOp;
  9704. PostNote(FORMAT(LoadStrPart(AUTO_READ_DIRECTORY_TOGGLE, 1),
  9705. (LoadStrPart(AUTO_READ_DIRECTORY_TOGGLE,
  9706. (Configuration->AutoReadDirectoryAfterOp ? 2 : 3)))));
  9707. }
  9708. //---------------------------------------------------------------------------
  9709. void __fastcall TCustomScpExplorerForm::StatusBarPanelDblClick(
  9710. TTBXCustomStatusBar * Sender, TTBXStatusPanel * Panel)
  9711. {
  9712. if (Panel->Index == 0)
  9713. {
  9714. if (FOnNoteClick != NULL)
  9715. {
  9716. // prevent the user data from being freed by possible call
  9717. // to CancelNote or PostNote during call to OnNoteClick
  9718. std::unique_ptr<TObject> NoteData(FNoteData);
  9719. TNotifyEvent OnNoteClick = FOnNoteClick;
  9720. FNoteData = NULL;
  9721. // need to cancel the note as we are going to delete its user data
  9722. CancelNote(true);
  9723. OnNoteClick(NoteData.get());
  9724. }
  9725. }
  9726. if (HasActiveTerminal() &&
  9727. (Panel->Index >= Sender->Panels->Count - SessionPanelCount))
  9728. {
  9729. FileSystemInfo();
  9730. }
  9731. }
  9732. //---------------------------------------------------------------------------
  9733. void __fastcall TCustomScpExplorerForm::LockWindow(bool Force)
  9734. {
  9735. // workaround:
  9736. // 1) for unknown reason, disabling window, while minimized,
  9737. // prevents it from restoring, even if it was enabled again meanwhile
  9738. // 2) when disabling the main window, while another has focus
  9739. // minimize is no longer possible
  9740. // ("keep up to date" dialog - though does not seem to be the case with "find file" -
  9741. // so it has probably something to do with the semi-modalness of the "keep up to date" dialog)
  9742. // Shouldn't we use IsApplicationMinimized() here?
  9743. if ((FLockSuspendLevel == 0) && !IsIconic(Application->Handle) && (Force || (Screen->ActiveForm == this)))
  9744. {
  9745. Enabled = false;
  9746. }
  9747. FLockLevel++;
  9748. }
  9749. //---------------------------------------------------------------------------
  9750. void __fastcall TCustomScpExplorerForm::UnlockWindow()
  9751. {
  9752. DebugAssert(FLockLevel > 0);
  9753. FLockLevel--;
  9754. if (FLockLevel == 0)
  9755. {
  9756. DebugAssert(FLockSuspendLevel == 0);
  9757. Enabled = true;
  9758. // VCL_COPY (TCustomForm.SetWindowFocus)
  9759. if (Active && (ActiveControl != NULL))
  9760. {
  9761. ::SetFocus(ActiveControl->Handle);
  9762. if (GetFocus() == ActiveControl->Handle)
  9763. {
  9764. ActiveControl->Perform(CM_UIACTIVATE, 0, 0);
  9765. }
  9766. }
  9767. }
  9768. }
  9769. //---------------------------------------------------------------------------
  9770. void __fastcall TCustomScpExplorerForm::SuspendWindowLock()
  9771. {
  9772. // We need to make sure that window is enabled when the last modal window closes
  9773. // otherwise focus is not restored correctly.
  9774. // So we re-enable the window when modal window opens and
  9775. // disable it back after is closes
  9776. if (FLockLevel > 0)
  9777. {
  9778. // while we have nesting counter, we know that we never be called
  9779. // recursivelly as Application->OnModalBegin is called only
  9780. // for the top-level modal window
  9781. if (DebugAlwaysTrue(FLockSuspendLevel == 0))
  9782. {
  9783. // won't be disabled when conditions in LockWindow() were not satisfied
  9784. FDisabledOnLockSuspend = !Enabled;
  9785. // When minimized to tray (or actually when set to SW_HIDE),
  9786. // setting Enabled makes the window focusable even when there's
  9787. // modal window over it
  9788. if (!FTrayIcon->Visible)
  9789. {
  9790. Enabled = true;
  9791. }
  9792. }
  9793. FLockSuspendLevel++;
  9794. }
  9795. }
  9796. //---------------------------------------------------------------------------
  9797. void __fastcall TCustomScpExplorerForm::ResumeWindowLock()
  9798. {
  9799. if (FLockSuspendLevel > 0)
  9800. {
  9801. DebugAssert(FLockLevel > 0);
  9802. FLockSuspendLevel--;
  9803. // see comment in SuspendWindowLock
  9804. if (DebugAlwaysTrue(FLockSuspendLevel == 0))
  9805. {
  9806. // Note that window can be enabled here, when we were minized to tray when
  9807. // was SuspendWindowLock() called.
  9808. // We should possibly do the same check as in LockWindow(),
  9809. // if it is ever possible that the consitions change between
  9810. // SuspendWindowLock() and ResumeWindowLock()
  9811. Enabled = !FDisabledOnLockSuspend;
  9812. }
  9813. }
  9814. }
  9815. //---------------------------------------------------------------------------
  9816. void __fastcall TCustomScpExplorerForm::UpdateRemotePathComboBox(bool TextOnly)
  9817. {
  9818. TTBXComboBoxItem * RemotePathComboBox =
  9819. reinterpret_cast<TTBXComboBoxItem *>(GetComponent(fcRemotePathComboBox));
  9820. if (!TextOnly)
  9821. {
  9822. TStrings * Items = RemotePathComboBox->Strings;
  9823. Items->BeginUpdate();
  9824. try
  9825. {
  9826. Items->Clear();
  9827. if ((Terminal != NULL) && !RemoteDirView->Path.IsEmpty())
  9828. {
  9829. UnicodeString APath = UnixExcludeTrailingBackslash(RemoteDirView->Path);
  9830. while (!IsUnixRootPath(APath))
  9831. {
  9832. int P = APath.LastDelimiter(L'/');
  9833. DebugAssert(P >= 0);
  9834. Items->Insert(0, APath.SubString(P + 1, APath.Length() - P));
  9835. APath.SetLength(P - 1);
  9836. }
  9837. Items->Insert(0, Customunixdirview_SUnixDefaultRootName);
  9838. }
  9839. }
  9840. __finally
  9841. {
  9842. Items->EndUpdate();
  9843. }
  9844. }
  9845. RemotePathComboBox->ItemIndex = RemotePathComboBox->Strings->Count - 1;
  9846. // Setting ItemIndex to -1 does not reset its text
  9847. if (RemotePathComboBox->Strings->Count == 0)
  9848. {
  9849. RemotePathComboBox->Text = L"";
  9850. }
  9851. }
  9852. //---------------------------------------------------------------------------
  9853. void __fastcall TCustomScpExplorerForm::DoRemotePathComboBoxAdjustImageIndex(
  9854. TTBXComboBoxItem * Sender, const UnicodeString DebugUsedArg(AText), int AIndex, int & ImageIndex)
  9855. {
  9856. if (AIndex < 0)
  9857. {
  9858. AIndex = Sender->ItemIndex;
  9859. }
  9860. ImageIndex = (AIndex < Sender->Strings->Count - 1 ? StdDirIcon : StdDirSelIcon);
  9861. }
  9862. //---------------------------------------------------------------------------
  9863. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxAdjustImageIndex(
  9864. TTBXComboBoxItem * Sender, const UnicodeString AText, int AIndex,
  9865. int & ImageIndex)
  9866. {
  9867. DoRemotePathComboBoxAdjustImageIndex(Sender, AText, AIndex, ImageIndex);
  9868. }
  9869. //---------------------------------------------------------------------------
  9870. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxDrawItem(
  9871. TTBXCustomList * /*Sender*/, TCanvas * /*ACanvas*/, TRect & ARect, int AIndex,
  9872. int /*AHoverIndex*/, bool & /*DrawDefault*/)
  9873. {
  9874. if (!IsLocalBrowserMode())
  9875. {
  9876. ARect.Left += (10 * AIndex);
  9877. }
  9878. }
  9879. //---------------------------------------------------------------------------
  9880. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxMeasureWidth(
  9881. TTBXCustomList * /*Sender*/, TCanvas * /*ACanvas*/, int AIndex, int &AWidth)
  9882. {
  9883. AWidth += (10 * AIndex);
  9884. }
  9885. //---------------------------------------------------------------------------
  9886. void __fastcall TCustomScpExplorerForm::DoRemotePathComboBoxItemClick(TObject * Sender)
  9887. {
  9888. TTBXComboBoxItem * RemotePathComboBox = dynamic_cast<TTBXComboBoxItem*>(Sender);
  9889. UnicodeString APath = UnixExcludeTrailingBackslash(RemoteDirView->Path);
  9890. int Index = RemotePathComboBox->ItemIndex;
  9891. while (Index < RemotePathComboBox->Strings->Count - 1)
  9892. {
  9893. APath = UnixExtractFileDir(APath);
  9894. Index++;
  9895. }
  9896. // VanDyke style paths
  9897. if (APath.IsEmpty())
  9898. {
  9899. DebugAssert(RemotePathComboBox->ItemIndex == 0);
  9900. APath = ROOTDIRECTORY;
  9901. }
  9902. if (RemoteDirView->Path != APath)
  9903. {
  9904. RemoteDirView->Path = APath;
  9905. }
  9906. }
  9907. //---------------------------------------------------------------------------
  9908. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxItemClick(TObject * Sender)
  9909. {
  9910. DoRemotePathComboBoxItemClick(Sender);
  9911. }
  9912. //---------------------------------------------------------------------------
  9913. void __fastcall TCustomScpExplorerForm::DoRemotePathComboBoxCancel(TObject * Sender)
  9914. {
  9915. DebugAssert(Sender == GetComponent(fcRemotePathComboBox));
  9916. DebugUsedParam(Sender);
  9917. UpdateRemotePathComboBox(true);
  9918. }
  9919. //---------------------------------------------------------------------------
  9920. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxCancel(TObject * Sender)
  9921. {
  9922. DoRemotePathComboBoxCancel(Sender);
  9923. }
  9924. //---------------------------------------------------------------------------
  9925. void __fastcall TCustomScpExplorerForm::DirViewEditing(
  9926. TObject * Sender, TListItem * Item, bool & /*AllowEdit*/)
  9927. {
  9928. ResetIncrementalSearch();
  9929. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  9930. DebugAssert(DirView != NULL);
  9931. if (!WinConfiguration->RenameWholeName && !DirView->ItemIsDirectory(Item))
  9932. {
  9933. HWND Edit = ListView_GetEditControl(DirView->Handle);
  9934. // OnEditing is called also from TCustomListView::CanEdit
  9935. if (Edit != NULL)
  9936. {
  9937. EditSelectBaseName(Edit);
  9938. }
  9939. }
  9940. }
  9941. //---------------------------------------------------------------------------
  9942. //---------------------------------------------------------------------------
  9943. TDragDropFilesEx * __fastcall TCustomScpExplorerForm::CreateDragDropFilesEx()
  9944. {
  9945. TDragDropFilesEx * Result = new TDragDropFilesEx(this);
  9946. Result->AutoDetectDnD = false;
  9947. Result->NeedValid = TFileExMustDnDSet() << nvFilename;
  9948. Result->RenderDataOn = rdoEnterAndDropSync;
  9949. Result->TargetEffects = TDropEffectSet() << deCopy << deMove;
  9950. return Result;
  9951. }
  9952. //---------------------------------------------------------------------------
  9953. void __fastcall TCustomScpExplorerForm::UpdateDarkMode()
  9954. {
  9955. if (IsWin10Build(19041))
  9956. {
  9957. FImmersiveDarkMode = WinConfiguration->UseDarkTheme();
  9958. BOOL DarkMode = FImmersiveDarkMode ? TRUE : FALSE;
  9959. const int DWMWA_USE_IMMERSIVE_DARK_MODE = 20;
  9960. DwmSetWindowAttribute(Handle, DWMWA_USE_IMMERSIVE_DARK_MODE, &DarkMode, sizeof(DarkMode));
  9961. }
  9962. }
  9963. //---------------------------------------------------------------------------
  9964. void __fastcall TCustomScpExplorerForm::CreateWnd()
  9965. {
  9966. TForm::CreateWnd();
  9967. // win32-darkmode calls AllowDarkModeForWindow(this, true) here, but it does not seem to have any effect
  9968. UpdateDarkMode();
  9969. if (FSessionsDragDropFilesEx == NULL)
  9970. {
  9971. FSessionsDragDropFilesEx = CreateDragDropFilesEx();
  9972. FSessionsDragDropFilesEx->OnDragOver = SessionsDDDragOver;
  9973. FSessionsDragDropFilesEx->OnProcessDropped = SessionsDDProcessDropped;
  9974. FSessionsDragDropFilesEx->OnDragEnter = SessionsDDDragEnter;
  9975. FSessionsDragDropFilesEx->OnDragLeave = SessionsDDDragLeave;
  9976. }
  9977. if (FQueueDragDropFilesEx == NULL)
  9978. {
  9979. FQueueDragDropFilesEx = CreateDragDropFilesEx();
  9980. // No need to set OnDragOver as we do not have any restrictions
  9981. FQueueDragDropFilesEx->OnProcessDropped = QueueDDProcessDropped;
  9982. FQueueDragDropFilesEx->OnDragEnter = QueueDDDragEnter;
  9983. FQueueDragDropFilesEx->OnDragLeave = QueueDDDragLeave;
  9984. }
  9985. }
  9986. //---------------------------------------------------------------------------
  9987. void __fastcall TCustomScpExplorerForm::DestroyWnd()
  9988. {
  9989. TForm::DestroyWnd();
  9990. FSessionsDragDropFilesEx->DragDropControl = NULL;
  9991. FQueueDragDropFilesEx->DragDropControl = NULL;
  9992. }
  9993. //---------------------------------------------------------------------------
  9994. void __fastcall TCustomScpExplorerForm::FormShow(TObject * /*Sender*/)
  9995. {
  9996. SideEnter(FCurrentSide);
  9997. FEverShown = true;
  9998. }
  9999. //---------------------------------------------------------------------------
  10000. void __fastcall TCustomScpExplorerForm::DoFindFiles(
  10001. TTerminal * ATerminal, UnicodeString Directory, const TFileMasks & FileMask,
  10002. TFileFoundEvent OnFileFound, TFindingFileEvent OnFindingFile)
  10003. {
  10004. if (!NonVisualDataModule->Busy)
  10005. {
  10006. TTerminalManager::Instance()->ActiveSession = DebugNotNull(dynamic_cast<TManagedTerminal *>(ATerminal));
  10007. Configuration->Usage->Inc(L"FileFinds");
  10008. LockWindow(true);
  10009. NonVisualDataModule->StartBusy();
  10010. WinConfiguration->LockedInterface = true;
  10011. try
  10012. {
  10013. Terminal->FilesFind(Directory, FileMask, OnFileFound, OnFindingFile);
  10014. }
  10015. __finally
  10016. {
  10017. WinConfiguration->LockedInterface = false;
  10018. NonVisualDataModule->EndBusy();
  10019. UnlockWindow();
  10020. }
  10021. }
  10022. }
  10023. //---------------------------------------------------------------------------
  10024. void __fastcall TCustomScpExplorerForm::DoFocusRemotePath(TTerminal * ATerminal, const UnicodeString & Path)
  10025. {
  10026. if (!NonVisualDataModule->Busy)
  10027. {
  10028. TTerminalManager::Instance()->ActiveSession = DebugNotNull(dynamic_cast<TManagedTerminal *>(ATerminal));
  10029. SetFocus();
  10030. RemoteDirView->Path = UnixExtractFilePath(Path);
  10031. UnicodeString FileName = UnixExtractFileName(Path);
  10032. TListItem * Item = RemoteDirView->FindFileItem(FileName);
  10033. // If not found, it can because we have loaded directory from cache and the file is new, try again after reload.
  10034. // (tbd: we should not even try this, if we have not loaded the directory from the cache)
  10035. if (Item == NULL)
  10036. {
  10037. ReloadDirectory(osRemote);
  10038. Item = RemoteDirView->FindFileItem(FileName);
  10039. }
  10040. if (Item != NULL)
  10041. {
  10042. RemoteDirView->ItemFocused = Item;
  10043. RemoteDirView->ItemFocused->MakeVisible(false);
  10044. RemoteDirView->SetFocus();
  10045. }
  10046. }
  10047. }
  10048. //---------------------------------------------------------------------------
  10049. bool __fastcall TCustomScpExplorerForm::CanOperateOnFoundFiles(TTerminal * ATerminal)
  10050. {
  10051. bool Result = !NonVisualDataModule->Busy;
  10052. if (Result)
  10053. {
  10054. TTerminalManager::Instance()->ActiveSession = DebugNotNull(dynamic_cast<TManagedTerminal *>(ATerminal));
  10055. }
  10056. return Result;
  10057. }
  10058. //---------------------------------------------------------------------------
  10059. void __fastcall TCustomScpExplorerForm::DoOperationOnFoundFiles(
  10060. TFileOperation Operation, TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  10061. {
  10062. if (CanOperateOnFoundFiles(ATerminal))
  10063. {
  10064. TValueRestorer<TFileOperationFinishedEvent> OnFileOperationFinishedRestorer(FOnFileOperationFinished, OnFileOperationFinished);
  10065. ExecuteFileOperation(Operation, osRemote, FileList, false, NULL);
  10066. }
  10067. }
  10068. //---------------------------------------------------------------------------
  10069. void __fastcall TCustomScpExplorerForm::DoDeleteFoundFiles(
  10070. TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  10071. {
  10072. DoOperationOnFoundFiles(foDelete, ATerminal, FileList, OnFileOperationFinished);
  10073. }
  10074. //---------------------------------------------------------------------------
  10075. void __fastcall TCustomScpExplorerForm::DoDownloadFoundFiles(
  10076. TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  10077. {
  10078. DoOperationOnFoundFiles(foCopy, ATerminal, FileList, OnFileOperationFinished);
  10079. }
  10080. //---------------------------------------------------------------------------
  10081. void __fastcall TCustomScpExplorerForm::DoEditFoundFiles(
  10082. TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  10083. {
  10084. if (CanOperateOnFoundFiles(ATerminal))
  10085. {
  10086. for (int Index = 0; Index < FileList->Count; Index++)
  10087. {
  10088. UnicodeString FileName = FileList->Strings[Index];
  10089. TRemoteFile * File = static_cast<TRemoteFile *>(FileList->Objects[Index]);
  10090. ExecuteRemoteFile(FileName, File, efDefaultEditor);
  10091. OnFileOperationFinished(osRemote, FileName, true, true);
  10092. }
  10093. }
  10094. }
  10095. //---------------------------------------------------------------------------
  10096. void __fastcall TCustomScpExplorerForm::RemoteFindFiles()
  10097. {
  10098. FFileFindTerminal = Terminal;
  10099. ShowFileFindDialog(
  10100. Terminal, RemoteDirView->Path, DoFindFiles, DoFocusRemotePath, DoDeleteFoundFiles,
  10101. DoDownloadFoundFiles, DoEditFoundFiles);
  10102. }
  10103. //---------------------------------------------------------------------------
  10104. void __fastcall TCustomScpExplorerForm::UpdateTaskbarList(ITaskbarList3 * TaskbarList)
  10105. {
  10106. FTaskbarList = TaskbarList;
  10107. }
  10108. //---------------------------------------------------------------------------
  10109. void __fastcall TCustomScpExplorerForm::SessionsPageControlMouseDown(
  10110. TObject * /*Sender*/, TMouseButton Button, TShiftState /*Shift*/, int X, int Y)
  10111. {
  10112. int Index = SessionsPageControl->IndexOfTabAt(X, Y);
  10113. if (Index >= 0)
  10114. {
  10115. if (Button == mbLeft)
  10116. {
  10117. // WORKAROUND: "Mouse down" is raised only after tab is switched.
  10118. // If switching tab (switching session) takes long enough for user
  10119. // to actually release the button, "mouse down" is still raised,
  10120. // but we do not get "mouse up" event, so dragging is not cancelled,
  10121. // prevent that by not beginning dragging in the first place.
  10122. int Key = ((GetSystemMetrics(SM_SWAPBUTTON) == 0) ? VK_LBUTTON : VK_RBUTTON);
  10123. if (IsKeyPressed(Key))
  10124. {
  10125. // when user clicks the "+", we get mouse down only after the session
  10126. // is closed, when new session tab is already on X:Y, so dragging
  10127. // starts, prevent that
  10128. if (MilliSecondsBetween(Now(), FSessionsPageControlNewTabTime) > 500)
  10129. {
  10130. TTerminal * Terminal = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  10131. if (Terminal != NULL)
  10132. {
  10133. SessionsPageControl->BeginDrag(false);
  10134. }
  10135. }
  10136. }
  10137. }
  10138. else if (Button == mbMiddle)
  10139. {
  10140. // ignore middle-click for "New session tab" and the last local-local tab
  10141. TManagedTerminal * Session = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  10142. if ((Session != NULL) && CanCloseSession(Session))
  10143. {
  10144. CloseSessionTab(Index);
  10145. }
  10146. }
  10147. }
  10148. }
  10149. //---------------------------------------------------------------------------
  10150. void __fastcall TCustomScpExplorerForm::SessionsPageControlDragDrop(
  10151. TObject * /*Sender*/, TObject * /*Source*/, int X, int Y)
  10152. {
  10153. int Index = SessionsPageControl->IndexOfTabAt(X, Y);
  10154. // do not allow dropping on the "+" tab
  10155. TTerminal * TargetTerminal = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  10156. if ((TargetTerminal != NULL) &&
  10157. (SessionsPageControl->ActivePage->PageIndex != Index))
  10158. {
  10159. Configuration->Usage->Inc(L"SessionTabMoves");
  10160. // this is almost redundant as we would recreate tabs in SessionListChanged,
  10161. // but we want to actually prevent that to avoid flicker
  10162. SessionsPageControl->ActivePage->PageIndex = Index;
  10163. TTerminal * Terminal = GetSessionTabSession(SessionsPageControl->ActivePage);
  10164. TTerminalManager::Instance()->Move(Terminal, TargetTerminal);
  10165. }
  10166. }
  10167. //---------------------------------------------------------------------------
  10168. void __fastcall TCustomScpExplorerForm::SessionsPageControlDragOver(
  10169. TObject * Sender, TObject * Source, int X, int Y,
  10170. TDragState /*State*/, bool & Accept)
  10171. {
  10172. Accept = (Sender == Source);
  10173. if (Accept)
  10174. {
  10175. int Index = SessionsPageControl->IndexOfTabAt(X, Y);
  10176. TTerminal * Terminal = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  10177. // do not allow dragging to the "+" tab
  10178. Accept = (Terminal != NULL);
  10179. }
  10180. }
  10181. //---------------------------------------------------------------------------
  10182. void __fastcall TCustomScpExplorerForm::SessionsDDDragOver(
  10183. int DebugUsedArg(KeyState), const TPoint & Point, int & Effect, int DebugUsedArg(PreferredEffect))
  10184. {
  10185. int Index = SessionsPageControl->IndexOfTabAt(Point.X, Point.Y);
  10186. if (Index < 0)
  10187. {
  10188. Effect = DROPEFFECT_NONE;
  10189. }
  10190. else
  10191. {
  10192. TManagedTerminal * TargetSession = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  10193. // do not allow dropping on the "+" tab or on disconnected or local-local tabs
  10194. if (!IsActiveTerminal(TargetSession) || DebugAlwaysFalse(TargetSession->LocalBrowser))
  10195. {
  10196. Effect = DROPEFFECT_NONE;
  10197. }
  10198. else if ((TargetSession != Terminal) && (Effect == DROPEFFECT_MOVE))
  10199. {
  10200. Effect = DROPEFFECT_COPY;
  10201. }
  10202. }
  10203. }
  10204. //---------------------------------------------------------------------------
  10205. void __fastcall TCustomScpExplorerForm::SessionsDDProcessDropped(
  10206. TObject * /*Sender*/, int /*KeyState*/, const TPoint & Point, int Effect)
  10207. {
  10208. int Index = SessionsPageControl->IndexOfTabAt(Point.X, Point.Y);
  10209. // do not allow dropping on local-local, disconnected and the "+" tab
  10210. // (though we do not seem to get here in that case anyway, contrary to RemoteFileControlDDTargetDrop)
  10211. TManagedTerminal * TargetTerminal = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  10212. if (DebugAlwaysTrue(IsActiveTerminal(TargetTerminal) && !TargetTerminal->LocalBrowser))
  10213. {
  10214. DebugAssert(!IsFileControl(DropSourceControl, osRemote));
  10215. if (!IsFileControl(DropSourceControl, osRemote))
  10216. {
  10217. TTerminalManager::Instance()->ActiveSession = TargetTerminal;
  10218. RemoteFileControlDragDropFileOperation(SessionsPageControl, Effect,
  10219. // Why don't we use Terminal->CurrentDirectory directly?
  10220. TTerminalManager::Instance()->ActiveTerminal->CurrentDirectory,
  10221. // do not force queue, drag drop
  10222. false, true);
  10223. }
  10224. }
  10225. }
  10226. //---------------------------------------------------------------------------
  10227. void __fastcall TCustomScpExplorerForm::QueueDDProcessDropped(
  10228. TObject * /*Sender*/, int /*KeyState*/, const TPoint & /*Point*/, int Effect)
  10229. {
  10230. // Downloads are handled in RemoteFileControlDDEnd
  10231. if (!IsFileControl(DropSourceControl, osRemote))
  10232. {
  10233. RemoteFileControlDragDropFileOperation(QueueView3, Effect,
  10234. Terminal->CurrentDirectory,
  10235. // force queue, drag drop
  10236. true, true);
  10237. }
  10238. }
  10239. //---------------------------------------------------------------------------
  10240. void __fastcall TCustomScpExplorerForm::FormClose(TObject * /*Sender*/, TCloseAction & /*Action*/)
  10241. {
  10242. FShowing = false;
  10243. // Do not save empty workspace
  10244. if (WinConfiguration->AutoSaveWorkspace && (ManagedSession != NULL))
  10245. {
  10246. std::unique_ptr<TObjectList> DataList(DoCollectWorkspace());
  10247. UnicodeString Name = WorkspaceName();
  10248. DoSaveWorkspace(
  10249. Name, DataList.get(),
  10250. !Configuration->DisablePasswordStoring &&
  10251. WinConfiguration->AutoSaveWorkspacePasswords, false);
  10252. WinConfiguration->LastStoredSession = Name;
  10253. }
  10254. AppLog(L"Closing main window");
  10255. }
  10256. //---------------------------------------------------------------------------
  10257. void __fastcall TCustomScpExplorerForm::RemoteDirViewRead(TObject * /*Sender*/)
  10258. {
  10259. TManagedTerminal * ManagedTerminal =
  10260. dynamic_cast<TManagedTerminal *>(RemoteDirView->Terminal);
  10261. if (ManagedTerminal != NULL)
  10262. {
  10263. ManagedTerminal->DirectoryLoaded = Now();
  10264. }
  10265. }
  10266. //---------------------------------------------------------------------------
  10267. void __fastcall TCustomScpExplorerForm::DirViewSelectItem(TObject * Sender,
  10268. TListItem * /*Item*/, bool /*Selected*/)
  10269. {
  10270. TCustomDirView * DirView = reinterpret_cast<TCustomDirView *>(Sender);
  10271. switch (DirView->LastSelectMethod)
  10272. {
  10273. case smKeyboard:
  10274. Configuration->Usage->Inc(L"KeyboardSelections");
  10275. break;
  10276. case smMouse:
  10277. Configuration->Usage->Inc(L"MouseSelections");
  10278. break;
  10279. }
  10280. }
  10281. //---------------------------------------------------------------------------
  10282. int __fastcall TCustomScpExplorerForm::AddSessionColor(TColor Color)
  10283. {
  10284. if (Color != 0)
  10285. {
  10286. return GetSessionColorImage(FSessionColors, Color, FSessionColorMaskImageIndex);
  10287. }
  10288. else
  10289. {
  10290. return FSessionTabImageIndex;
  10291. }
  10292. }
  10293. //---------------------------------------------------------------------------
  10294. int __fastcall TCustomScpExplorerForm::AddFixedSessionImage(int GlyphsSourceIndex)
  10295. {
  10296. TPngImageCollectionItem * Item =
  10297. GlyphsModule->ExplorerImages->PngImages->Items[GlyphsSourceIndex];
  10298. FSessionColors->AddPng(Item->PngImage);
  10299. return FSessionColors->Count - 1;
  10300. }
  10301. //---------------------------------------------------------------------------
  10302. void __fastcall TCustomScpExplorerForm::AddFixedSessionImages()
  10303. {
  10304. FSessionColors->SetSize(GlyphsModule->ExplorerImages->Width, GlyphsModule->ExplorerImages->Height);
  10305. FNewRemoteTabTabImageIndex = AddFixedSessionImage(NonVisualDataModule->NewRemoteTabAction->ImageIndex);
  10306. FNewLocalTabTabImageIndex = AddFixedSessionImage(NonVisualDataModule->NewLocalTabAction->ImageIndex);
  10307. FSessionTabImageIndex = AddFixedSessionImage(SiteImageIndex);
  10308. FSessionColorMaskImageIndex = AddFixedSessionImage(SiteColorMaskImageIndex);
  10309. FLocalBrowserTabImageIndex = AddFixedSessionImage(LocalBrowserImageIndex);
  10310. }
  10311. //---------------------------------------------------------------------------
  10312. int TCustomScpExplorerForm::GetNewTabActionImageIndex()
  10313. {
  10314. return NonVisualDataModule->NewRemoteTabAction->ImageIndex;
  10315. }
  10316. //---------------------------------------------------------------------------
  10317. int TCustomScpExplorerForm::GetNewTabTabImageIndex(TOperationSide Side)
  10318. {
  10319. switch (Side)
  10320. {
  10321. case osLocal:
  10322. return FNewLocalTabTabImageIndex;
  10323. case osRemote:
  10324. case osCurrent:
  10325. return FNewRemoteTabTabImageIndex;
  10326. default:
  10327. DebugFail();
  10328. return -1;
  10329. }
  10330. }
  10331. //---------------------------------------------------------------------------
  10332. void __fastcall TCustomScpExplorerForm::CollectItemsWithTextDisplayMode(TWinControl * Control)
  10333. {
  10334. for (int Index = 0; Index < Control->ControlCount; Index++)
  10335. {
  10336. TControl * AControl = Control->Controls[Index];
  10337. TWinControl * WinControl = dynamic_cast<TWinControl *>(AControl);
  10338. if (WinControl != NULL)
  10339. {
  10340. CollectItemsWithTextDisplayMode(WinControl);
  10341. }
  10342. TTBCustomToolbar * Toolbar = dynamic_cast<TTBCustomToolbar *>(AControl);
  10343. if (Toolbar != NULL)
  10344. {
  10345. // we care for top-level items only
  10346. TTBCustomItem * Items = Toolbar->Items;
  10347. for (int ItemIndex = 0; ItemIndex < Items->Count; ItemIndex++)
  10348. {
  10349. TTBCustomItem * Item = Items->Items[ItemIndex];
  10350. if (((Item->DisplayMode == nbdmImageAndText) ||
  10351. (dynamic_cast<TTBXLabelItem *>(Item) != NULL)) &&
  10352. EligibleForImageDisplayMode(Item))
  10353. {
  10354. FItemsWithTextDisplayMode.insert(Item);
  10355. }
  10356. }
  10357. }
  10358. }
  10359. }
  10360. //---------------------------------------------------------------------------
  10361. bool __fastcall TCustomScpExplorerForm::EligibleForImageDisplayMode(TTBCustomItem * /*Item*/)
  10362. {
  10363. return true;
  10364. }
  10365. //---------------------------------------------------------------------------
  10366. bool __fastcall TCustomScpExplorerForm::UpdateToolbarDisplayMode()
  10367. {
  10368. bool SelectiveToolbarText = WinConfiguration->SelectiveToolbarText;
  10369. TTBItemDisplayMode DisplayMode = (SelectiveToolbarText ? nbdmImageAndText : nbdmDefault);
  10370. typedef std::set<TTBCustomItem *> TItemsWithTextDisplayMode;
  10371. TItemsWithTextDisplayMode::iterator i = FItemsWithTextDisplayMode.begin();
  10372. bool Result = true;
  10373. while (Result && (i != FItemsWithTextDisplayMode.end()))
  10374. {
  10375. TTBCustomItem * Item = *i;
  10376. TTBXLabelItem * Label = dynamic_cast<TTBXLabelItem *>(Item);
  10377. if (Label != NULL)
  10378. {
  10379. // optimization
  10380. if (Label->Visible == SelectiveToolbarText)
  10381. {
  10382. Result = false;
  10383. }
  10384. else
  10385. {
  10386. Label->Visible = SelectiveToolbarText;
  10387. }
  10388. }
  10389. else
  10390. {
  10391. // optimization
  10392. if (Item->DisplayMode == DisplayMode)
  10393. {
  10394. Result = false;
  10395. }
  10396. else
  10397. {
  10398. Item->DisplayMode = DisplayMode;
  10399. }
  10400. }
  10401. ++i;
  10402. }
  10403. UpdateNewTabTab();
  10404. return Result;
  10405. }
  10406. //---------------------------------------------------------------------------
  10407. void __fastcall TCustomScpExplorerForm::DisplaySystemContextMenu()
  10408. {
  10409. DebugFail();
  10410. }
  10411. //---------------------------------------------------------------------------
  10412. bool __fastcall TCustomScpExplorerForm::IsBusy()
  10413. {
  10414. // Among other, a lock level is non zero, while directory is being loaded,
  10415. // even when it happens as a result of dirview navigation,
  10416. // i.e. when TNonVisualDataModule is NOT FBusy.
  10417. // That's why the TNonVisualDataModule::GetBusy calls this method.
  10418. // Among other this prevents a panel auto update to occur while
  10419. // directory is changing.
  10420. return (FLockLevel > 0) || DirView(osCurrent)->IsEditing();
  10421. }
  10422. //---------------------------------------------------------------------------
  10423. Boolean __fastcall TCustomScpExplorerForm::AllowedAction(TAction * Action, TActionAllowed Allowed)
  10424. {
  10425. // While the window is disabled, we still seem to process menu shortcuts at least,
  10426. // so stop it at least here.
  10427. // See also TCustomScpExplorerForm::RemoteDirViewBusy
  10428. bool Result;
  10429. if (Allowed == aaUpdate)
  10430. {
  10431. Result = true;
  10432. }
  10433. else
  10434. {
  10435. Result = !NonVisualDataModule->Busy;
  10436. if (Result && (Allowed == aaShortCut))
  10437. {
  10438. // Otherwise F5 for local-local will be swallowed by disabled local<>remote actions in TCustomScpExplorerForm::KeyDown
  10439. Action->Update();
  10440. Result = Action->Enabled;
  10441. }
  10442. }
  10443. return Result;
  10444. }
  10445. //---------------------------------------------------------------------------
  10446. void __fastcall TCustomScpExplorerForm::EditMenuItemPopup(TTBCustomItem * Sender, bool FromLink)
  10447. {
  10448. NonVisualDataModule->EditMenuItemPopup(Sender, FromLink);
  10449. }
  10450. //---------------------------------------------------------------------------
  10451. void __fastcall TCustomScpExplorerForm::DirViewBusy(TObject * /*Sender*/, int Busy, bool & State)
  10452. {
  10453. // This is somewhat redundant to LockWindow() call from
  10454. // TTerminalManager::TerminalReadDirectoryProgress.
  10455. // But disabling window is known not to block keyboard shorcuts
  10456. // (see TCustomScpExplorerForm::AllowedAction), this hopefully works.
  10457. if (Busy > 0)
  10458. {
  10459. if (NonVisualDataModule->Busy)
  10460. {
  10461. State = false;
  10462. }
  10463. else
  10464. {
  10465. NonVisualDataModule->StartBusy();
  10466. // Will be countered for TCustomDirView.DoExecute in DoDirViewExecFile
  10467. LockWindow();
  10468. }
  10469. }
  10470. else if (Busy < 0)
  10471. {
  10472. UnlockWindow();
  10473. NonVisualDataModule->EndBusy();
  10474. }
  10475. else
  10476. {
  10477. State = NonVisualDataModule->Busy;
  10478. }
  10479. }
  10480. //---------------------------------------------------------------------------
  10481. void __fastcall TCustomScpExplorerForm::SessionsPageControlContextPopup(TObject * /*Sender*/, TPoint & MousePos, bool & Handled)
  10482. {
  10483. int Index = SessionsPageControl->IndexOfTabAt(MousePos.X, MousePos.Y);
  10484. if (Index >= 0)
  10485. {
  10486. TPopupMenu * PopupMenu = NULL;
  10487. TManagedTerminal * Session = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  10488. if (Session != NULL)
  10489. {
  10490. SessionsPageControl->ActivePageIndex = Index;
  10491. if (DebugAlwaysTrue(SessionTabSwitched()))
  10492. {
  10493. // copied from TControl.WMContextMenu
  10494. SendCancelMode(SessionsPageControl);
  10495. // explicit popup instead of using PopupMenu property
  10496. // to avoid menu to popup somewhere within SessionTabSwitched above,
  10497. // while connecting yet not-connected session and hence
  10498. // allowing an access to commands over not-completely connected session
  10499. PopupMenu = Session->LocalBrowser ? NonVisualDataModule->LocalBrowserPopup : NonVisualDataModule->SessionsPopup;
  10500. }
  10501. }
  10502. else
  10503. {
  10504. if (SupportsLocalBrowser())
  10505. {
  10506. PopupMenu = NonVisualDataModule->NewTabPopup;
  10507. }
  10508. }
  10509. if (PopupMenu != NULL)
  10510. {
  10511. TPoint Point = SessionsPageControl->ClientToScreen(MousePos);
  10512. PopupMenu->PopupComponent = SessionsPageControl;
  10513. PopupMenu->Popup(Point.x, Point.y);
  10514. }
  10515. }
  10516. Handled = true;
  10517. }
  10518. //---------------------------------------------------------------------------
  10519. bool __fastcall TCustomScpExplorerForm::CanChangePassword()
  10520. {
  10521. return HasActiveTerminal() && Terminal->IsCapable[fcChangePassword];
  10522. }
  10523. //---------------------------------------------------------------------------
  10524. void __fastcall TCustomScpExplorerForm::ChangePassword()
  10525. {
  10526. Configuration->Usage->Inc(L"PasswordChanges");
  10527. std::unique_ptr<TSessionData> ChangePasswordData(Terminal->SessionData->Clone());
  10528. ChangePasswordData->DisableAuthentationsExceptPassword();
  10529. ChangePasswordData->ChangePassword = true;
  10530. std::unique_ptr<TTerminal> ChangePasswordSession(
  10531. Terminal->CreateSecondarySession(L"ChangePassword", ChangePasswordData.get()));
  10532. ChangePasswordSession->AutoReadDirectory = false;
  10533. if (TTerminalManager::Instance()->ConnectTerminal(ChangePasswordSession.get()))
  10534. {
  10535. MessageDialog(MainInstructions(LoadStr(PASSWORD_CHANGED)), qtInformation, qaOK, HELP_CHANGE_PASSWORD);
  10536. }
  10537. else
  10538. {
  10539. Configuration->Usage->Inc(L"PasswordChangesFailed");
  10540. }
  10541. }
  10542. //---------------------------------------------------------------------------
  10543. bool __fastcall TCustomScpExplorerForm::CanPrivateKeyUpload()
  10544. {
  10545. // No nice way to assert SSH2
  10546. return HasActiveTerminal() && (Terminal->FSProtocol == cfsSFTP);
  10547. }
  10548. //---------------------------------------------------------------------------
  10549. void __fastcall TCustomScpExplorerForm::PrivateKeyUpload()
  10550. {
  10551. TOperationVisualizer OperationVisualizer;
  10552. UnicodeString FileName = Terminal->SessionData->PublicKeyFile;
  10553. TTerminalManager::Instance()->UploadPublicKey(Terminal, NULL, FileName);
  10554. }
  10555. //---------------------------------------------------------------------------
  10556. void __fastcall TCustomScpExplorerForm::FormAfterMonitorDpiChanged(TObject *, int OldDPI, int NewDPI)
  10557. {
  10558. DebugUsedParam(OldDPI);
  10559. DebugAssert(NewDPI == GetControlPixelsPerInch(this));
  10560. GlyphsModule->PixelsPerInch = NewDPI;
  10561. // Can be called too early when primary monitor has non-system dpi
  10562. if (FSessionColors != NULL)
  10563. {
  10564. RegenerateSessionColorsImageList();
  10565. }
  10566. UpdateTabsSize();
  10567. UpdateSessionsPageControlHeight();
  10568. QueueFileListColumnAutoSize();
  10569. }
  10570. //---------------------------------------------------------------------------
  10571. void __fastcall TCustomScpExplorerForm::DockContextPopup(TObject * Sender, TPoint & MousePos, bool & /*Handled*/)
  10572. {
  10573. NonVisualDataModule->ControlContextPopup(Sender, MousePos);
  10574. }
  10575. //---------------------------------------------------------------------
  10576. class TPublicControl : public TControl
  10577. {
  10578. friend class TCustomScpExplorerForm;
  10579. };
  10580. //---------------------------------------------------------------------------
  10581. void __fastcall TCustomScpExplorerForm::CopyPopup(TControl * DestControl, TControl * SourceControl)
  10582. {
  10583. static_cast<TPublicControl *>(DestControl)->PopupMenu = static_cast<TPublicControl *>(SourceControl)->PopupMenu;
  10584. static_cast<TPublicControl *>(DestControl)->OnContextPopup = static_cast<TPublicControl *>(SourceControl)->OnContextPopup;
  10585. }
  10586. //---------------------------------------------------------------------------
  10587. void __fastcall TCustomScpExplorerForm::DoBookmarkClick(TOperationSide Side, TObject * Sender)
  10588. {
  10589. TBookmark * Bookmark = DebugNotNull(reinterpret_cast<TBookmark *>(dynamic_cast<TTBCustomItem *>(Sender)->Tag));
  10590. OpenBookmark(Side, Bookmark);
  10591. }
  10592. //---------------------------------------------------------------------------
  10593. void __fastcall TCustomScpExplorerForm::LocalBookmarkClick(TObject * Sender)
  10594. {
  10595. DoBookmarkClick(osLocal, Sender);
  10596. }
  10597. //---------------------------------------------------------------------------
  10598. void __fastcall TCustomScpExplorerForm::RemoteBookmarkClick(TObject * Sender)
  10599. {
  10600. DoBookmarkClick(osRemote, Sender);
  10601. }
  10602. //---------------------------------------------------------------------------
  10603. void __fastcall TCustomScpExplorerForm::CreateOpenDirMenuList(
  10604. TTBCustomItem * Menu, TOperationSide Side, TBookmarkList * BookmarkList)
  10605. {
  10606. if (BookmarkList != NULL)
  10607. {
  10608. TNotifyEvent OnBookmarkClick = (Side == osLocal) ? &LocalBookmarkClick : &RemoteBookmarkClick;
  10609. if (IsLocalBrowserMode())
  10610. {
  10611. Side = osLocal;
  10612. }
  10613. if (!WinConfiguration->UseLocationProfiles)
  10614. {
  10615. std::unique_ptr<TStringList> Directories(new TStringList());
  10616. Directories->CaseSensitive = (Side == osRemote);
  10617. for (int Index = 0; Index < BookmarkList->Count; Index++)
  10618. {
  10619. TBookmark * Bookmark = BookmarkList->Bookmarks[Index];
  10620. UnicodeString Directory = Bookmark->GetSideDirectory(Side);
  10621. if (!Directory.IsEmpty() && (Directories->IndexOf(Directory) < 0))
  10622. {
  10623. std::unique_ptr<TTBCustomItem> Item(new TTBXItem(Owner));
  10624. Item->Caption = EscapeHotkey(Directory);
  10625. Item->ShortCut = Bookmark->ShortCut;
  10626. Item->OnClick = OnBookmarkClick;
  10627. Item->Tag = reinterpret_cast<int>(Bookmark);
  10628. Directories->Add(Directory);
  10629. Menu->Add(Item.release());
  10630. }
  10631. }
  10632. }
  10633. else
  10634. {
  10635. std::unique_ptr<TStrings> Folders(CreateSortedStringList());
  10636. for (int Index = 0; Index < BookmarkList->Count; Index++)
  10637. {
  10638. TBookmark * Bookmark = BookmarkList->Bookmarks[Index];
  10639. if (!Bookmark->Node.IsEmpty())
  10640. {
  10641. Folders->Add(Bookmark->Node);
  10642. }
  10643. }
  10644. for (int Index = 0; Index < Folders->Count; Index++)
  10645. {
  10646. std::unique_ptr<TTBCustomItem> Item(new TTBXSubmenuItem(Owner));
  10647. Item->Caption = Folders->Strings[Index];
  10648. Item->ImageIndex = NonVisualDataModule->RemoteChangePathAction2->ImageIndex;
  10649. Folders->Objects[Index] = Item.get();
  10650. Menu->Add(Item.release());
  10651. }
  10652. for (int Index = 0; Index < BookmarkList->Count; Index++)
  10653. {
  10654. TBookmark * Bookmark = BookmarkList->Bookmarks[Index];
  10655. TTBCustomItem * Parent;
  10656. if (!Bookmark->Node.IsEmpty())
  10657. {
  10658. DebugAssert(Folders->IndexOf(Bookmark->Node) >= 0);
  10659. Parent = dynamic_cast<TTBCustomItem *>(Folders->Objects[Folders->IndexOf(Bookmark->Node)]);
  10660. }
  10661. else
  10662. {
  10663. Parent = Menu;
  10664. }
  10665. std::unique_ptr<TTBCustomItem> Item(new TTBXItem(Owner));
  10666. Item->Caption = Bookmark->Name;
  10667. Item->ShortCut = Bookmark->ShortCut;
  10668. Item->OnClick = OnBookmarkClick;
  10669. Item->Tag = reinterpret_cast<int>(Bookmark);
  10670. if (((Bookmark->Name == Bookmark->Local) || (Bookmark->Name == Bookmark->Remote)) &&
  10671. (Bookmark->Local.IsEmpty() || Bookmark->Remote.IsEmpty()))
  10672. {
  10673. // No hint for location profiles that are actually mere "bookmarks"
  10674. }
  10675. else
  10676. {
  10677. UnicodeString Hint = FORMAT(LoadStrPart(LOCATION_PROFILE_HINT, 1), (Bookmark->Name));
  10678. UnicodeString LongHint;
  10679. if (!Bookmark->Local.IsEmpty())
  10680. {
  10681. AddToList(LongHint, FORMAT(LoadStrPart(LOCATION_PROFILE_HINT, 2), (Bookmark->Local)), L"\n");
  10682. }
  10683. if (!Bookmark->Remote.IsEmpty())
  10684. {
  10685. AddToList(LongHint, FORMAT(LoadStrPart(LOCATION_PROFILE_HINT, 3), (Bookmark->Remote)), L"\n");
  10686. }
  10687. AddToList(Hint, LongHint, L"|");
  10688. Item->Hint = Hint;
  10689. }
  10690. Parent->Add(Item.release());
  10691. }
  10692. }
  10693. AddMenuSeparator(Menu);
  10694. }
  10695. }
  10696. //---------------------------------------------------------------------------
  10697. void __fastcall TCustomScpExplorerForm::CreateOpenDirMenu(TTBCustomItem * Menu, TOperationSide Side)
  10698. {
  10699. Menu->Clear();
  10700. std::unique_ptr<TTBCustomItem> Item;
  10701. Item.reset(new TTBXItem(Owner));
  10702. Item->Action = (Side == osLocal) ? NonVisualDataModule->LocalOpenDirAction : NonVisualDataModule->RemoteOpenDirAction;
  10703. Item->Options = Item->Options << tboDefault;
  10704. Menu->Add(Item.release());
  10705. AddMenuSeparator(Menu);
  10706. if (Terminal != NULL)
  10707. {
  10708. CreateOpenDirMenuList(Menu, Side, WinConfiguration->Bookmarks[Terminal->SessionData->SessionKey]);
  10709. }
  10710. CreateOpenDirMenuList(Menu, Side, WinConfiguration->SharedBookmarks);
  10711. Item.reset(new TTBXItem(Owner));
  10712. Item->Action = (Side == osLocal) ? NonVisualDataModule->LocalAddBookmarkAction2 : NonVisualDataModule->RemoteAddBookmarkAction2;
  10713. Menu->Add(Item.release());
  10714. }
  10715. //---------------------------------------------------------------------------
  10716. bool __fastcall TCustomScpExplorerForm::TryOpenDirectory(TOperationSide Side, const UnicodeString & Path)
  10717. {
  10718. bool Result = true;
  10719. bool Remote = !IsSideLocalBrowser(Side);
  10720. try
  10721. {
  10722. UnicodeString APath = Path;
  10723. if (Remote)
  10724. {
  10725. Terminal->ExceptionOnFail = true;
  10726. }
  10727. else
  10728. {
  10729. APath = ExpandEnvironmentVariables(Path);
  10730. }
  10731. try
  10732. {
  10733. DirView(Side)->Path = APath;
  10734. }
  10735. __finally
  10736. {
  10737. if (Remote)
  10738. {
  10739. Terminal->ExceptionOnFail = false;
  10740. }
  10741. }
  10742. }
  10743. catch (Exception & E)
  10744. {
  10745. if (!Remote || Terminal->Active)
  10746. {
  10747. ShowExtendedExceptionEx(Terminal, &E);
  10748. Result = false;
  10749. }
  10750. else
  10751. {
  10752. throw;
  10753. }
  10754. }
  10755. return Result;
  10756. }
  10757. //---------------------------------------------------------------------------
  10758. void __fastcall TCustomScpExplorerForm::ReloadDirectory(TOperationSide Side)
  10759. {
  10760. // Make sure there some feedback even when loading tiny local folders
  10761. TInstantOperationVisualizer Visualizer;
  10762. DirView(Side)->ReloadDirectory();
  10763. }
  10764. //---------------------------------------------------------------------------
  10765. void __fastcall TCustomScpExplorerForm::CloseSessionTab(int Index)
  10766. {
  10767. if (Index == SessionsPageControl->TabIndex)
  10768. {
  10769. CloseTab();
  10770. }
  10771. else
  10772. {
  10773. TTabSheet * TabSheet = SessionsPageControl->Pages[Index];
  10774. TTerminal * Terminal = GetSessionTabSession(TabSheet);
  10775. TTerminalManager * Manager = TTerminalManager::Instance();
  10776. if (!Terminal->Active)
  10777. {
  10778. Manager->FreeTerminal(Terminal);
  10779. }
  10780. else
  10781. {
  10782. TTerminalQueue * Queue = Manager->FindQueueForTerminal(Terminal);
  10783. if (CanCloseQueue(Queue))
  10784. {
  10785. Manager->FreeTerminal(Terminal);
  10786. }
  10787. }
  10788. }
  10789. }
  10790. //---------------------------------------------------------------------------
  10791. void __fastcall TCustomScpExplorerForm::SessionsPageControlTabButtonClick(TPageControl *, int Index)
  10792. {
  10793. TManagedTerminal * Session = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  10794. if (Session == NULL)
  10795. {
  10796. TRect ButtonRect = SessionsPageControl->TabButtonRect(Index);
  10797. ButtonRect = TRect(SessionsPageControl->ClientToScreen(ButtonRect.TopLeft()), SessionsPageControl->ClientToScreen(ButtonRect.BottomRight()));
  10798. //TPoint P = TPoint(ButtonRect.Left, ButtonRect.Bottom);
  10799. //P = SessionsPageControl->ClientToScreen(P);
  10800. MenuPopup(NonVisualDataModule->NewTabPopup, ButtonRect, SessionsPageControl);
  10801. }
  10802. else
  10803. {
  10804. CloseSessionTab(Index);
  10805. }
  10806. }
  10807. //---------------------------------------------------------------------------
  10808. void __fastcall TCustomScpExplorerForm::CopyFilesToClipboard(TOperationSide Side, bool OnFocused)
  10809. {
  10810. if (DebugAlwaysTrue(!IsSideLocalBrowser(Side)))
  10811. {
  10812. TInstantOperationVisualizer Visualizer;
  10813. // To trigger ClipboardDataObjectRelease (in case the clipboard already contains our data)
  10814. // before we create a new data object
  10815. ClipboardClear();
  10816. DebugAssert(FClipboardFakeDirectory.IsEmpty());
  10817. DebugAssert(FClipboardFakeMonitors.get() == NULL);
  10818. FClipboardFakeDirectory = CreateFakeTransferDirectory();
  10819. // Intermediate variable to WORKAROUND a crash in compiler
  10820. TObjectList * ClipboardFakeMonitors =
  10821. StartCreationDirectoryMonitorsOnEachDrive(FILE_NOTIFY_CHANGE_DIR_NAME, ClipboardFakeCreated);
  10822. FClipboardFakeMonitors.reset(ClipboardFakeMonitors);
  10823. if (FClipboardDragDropFilesEx.get() == NULL)
  10824. {
  10825. FClipboardDragDropFilesEx.reset(new TDragDropFilesEx(this));
  10826. FClipboardDragDropFilesEx->OnDataObjectRelease = ClipboardDataObjectRelease;
  10827. }
  10828. FClipboardDragDropFilesEx->FileList->Clear();
  10829. FClipboardDragDropFilesEx->FileList->AddItem(NULL, FClipboardFakeDirectory);
  10830. FClipboardDragDropFilesEx->CopyToClipboard();
  10831. FClipboardTerminal = Terminal;
  10832. // Need full paths, as cwd can be different once files are pasted
  10833. FClipboardFileList.reset(TRemoteFileList::CloneStrings(RemoteDirView->CreateFileList(OnFocused, true)));
  10834. for (int Index = 0; Index < FClipboardFileList->Count; Index++)
  10835. {
  10836. FClipboardFileList->Strings[Index] = UnixExcludeTrailingBackslash(FClipboardFileList->Strings[Index]);
  10837. }
  10838. AppLogFmt(L"Created fake clipboard directory \"%s\" and started drive monitors", (FClipboardFakeDirectory));
  10839. }
  10840. }
  10841. //---------------------------------------------------------------------------
  10842. void __fastcall TCustomScpExplorerForm::ClipboardClear()
  10843. {
  10844. if (OpenClipboard(0))
  10845. {
  10846. AppLog(L"Clearing clipboard");
  10847. EmptyClipboard(); // Calls ClipboardDataObjectRelease, if clipboard contains our data
  10848. CloseClipboard();
  10849. }
  10850. }
  10851. //---------------------------------------------------------------------------
  10852. bool __fastcall TCustomScpExplorerForm::DoesClipboardContainOurFiles()
  10853. {
  10854. return !FClipboardFakeDirectory.IsEmpty();
  10855. }
  10856. //---------------------------------------------------------------------------
  10857. void __fastcall TCustomScpExplorerForm::ClipboardStop()
  10858. {
  10859. bool Deleted = RemoveDir(ApiPath(FClipboardFakeDirectory));
  10860. AppLogFmt(L"Clearing clipboard, deleted clipboard temporary folder \"%s\" - %d", (FClipboardFakeDirectory, int(Deleted)));
  10861. FClipboardFakeDirectory = UnicodeString();
  10862. FClipboardTerminal = NULL;
  10863. if (FDownloadingFromClipboard)
  10864. {
  10865. // We are called by the monitor, so attempt to release it would deadlock
  10866. FClipboardFakeMonitorsPendingReset = true;
  10867. }
  10868. else
  10869. {
  10870. FClipboardFakeMonitors.reset(NULL);
  10871. }
  10872. }
  10873. //---------------------------------------------------------------------------
  10874. void __fastcall TCustomScpExplorerForm::ClipboardDataObjectRelease(TObject * /*Sender*/)
  10875. {
  10876. if (DebugAlwaysTrue(!FClipboardFakeDirectory.IsEmpty()))
  10877. {
  10878. ClipboardStop();
  10879. }
  10880. }
  10881. //---------------------------------------------------------------------------
  10882. void __fastcall TCustomScpExplorerForm::ClipboardDownload(const UnicodeString & TargetDirectory, bool NoConfirmation, bool DragDrop)
  10883. {
  10884. TTerminalManager * Manager = TTerminalManager::Instance();
  10885. Manager->ActiveSession = FClipboardTerminal;
  10886. TTransferOperationParam Params;
  10887. Params.TargetDirectory = TargetDirectory;
  10888. Params.DragDrop = DragDrop;
  10889. ExecuteFileOperation(foCopy, osRemote, FClipboardFileList.get(), NoConfirmation, &Params);
  10890. }
  10891. //---------------------------------------------------------------------------
  10892. void TCustomScpExplorerForm::PasteFiles()
  10893. {
  10894. // Guard against possible race conditions
  10895. if (!FClipboardPasteTarget.IsEmpty())
  10896. {
  10897. // ClipboardFakeCreated might be called while we are here, so make sure we finish operating on the same path we started with
  10898. UnicodeString Target = FClipboardPasteTarget;
  10899. FClipboardPasteTarget = EmptyStr;
  10900. // Can fail as it can be e.g. locked by AV, so we retry that later
  10901. bool Removed = RemoveDir(ApiPath(Target));
  10902. AppLogFmt(L"First attempt to delete pasted fake clipboard directory \"%s\" - %d", (Target, int(Removed)));
  10903. try
  10904. {
  10905. if (NonVisualDataModule->Busy)
  10906. {
  10907. AppLog(L"Is busy, cannot paste");
  10908. }
  10909. else
  10910. {
  10911. DebugAssert(!FClipboardFakeMonitorsPendingReset);
  10912. FClipboardFakeMonitorsPendingReset = false;
  10913. bool NoConfirmation = (WinConfiguration->DDTransferConfirmation == asOff);
  10914. TAutoFlag Flag(FDownloadingFromClipboard);
  10915. AppLog(L"Downloading to paste target...");
  10916. ClipboardDownload(ExtractFilePath(Target), NoConfirmation, true);
  10917. AppLog(L"Downloaded to paste target...");
  10918. }
  10919. }
  10920. __finally
  10921. {
  10922. if (!Removed)
  10923. {
  10924. Removed = RemoveDir(ApiPath(Target));
  10925. AppLogFmt(L"Second attempt to delete pasted fake clipboard directory \"%s\" - %d", (Target, int(Removed)));
  10926. }
  10927. }
  10928. }
  10929. }
  10930. //---------------------------------------------------------------------------
  10931. void __fastcall TCustomScpExplorerForm::ClipboardFakeCreated(TObject * /*Sender*/, const UnicodeString FileName)
  10932. {
  10933. // It can actually rarelly happen that some random file is created, while we are shutting down the monitor
  10934. // (as it pumps a Windows message queue while being shutted down)
  10935. if (DebugAlwaysTrue(!FClipboardFakeDirectory.IsEmpty()) &&
  10936. // Is can happen that creation of our temporary directory is detected (even though we are creating it before starting the monitors).
  10937. // It tent do happen with later copy-pastes, probably because everything is cached already and monitors start quickly.
  10938. !IsPathToSameFile(FClipboardFakeDirectory, FileName) &&
  10939. SameText(ExtractFileName(FileName), ExtractFileName(FClipboardFakeDirectory)))
  10940. {
  10941. AppLogFmt(L"Fake clipboard directory pasted to \"%s\"", (FileName));
  10942. FClipboardPasteTarget = FileName;
  10943. // Particularly when pasting to external HDD, the fake directory is strangely recreated if removed here in the monitor callback.
  10944. // Postponing removing (or actually whole pasting operation) seems to help.
  10945. PostMessage(Handle, WM_PASTE_FILES, 0, 0);
  10946. }
  10947. }
  10948. //---------------------------------------------------------------------------
  10949. void __fastcall TCustomScpExplorerForm::DirViewGetItemColor(
  10950. TObject * Sender, UnicodeString FileName, bool Directory, __int64 Size, TDateTime Modification, TColor & Color)
  10951. {
  10952. TFileMasks::TParams MaskParams;
  10953. MaskParams.Size = Size;
  10954. MaskParams.Modification = Modification;
  10955. TCustomDirView * DirView = DebugNotNull(dynamic_cast<TCustomDirView *>(Sender));
  10956. bool Local = (dynamic_cast<TUnixDirView *>(DirView) == NULL);
  10957. // TUnixDirView::ItemFullFileName returns trailing backslash for directories, what filemask cannot deal with
  10958. if (!Local && Directory)
  10959. {
  10960. FileName = UnixExcludeTrailingBackslash(FileName);
  10961. }
  10962. for (TFileColorData::TList::const_iterator Iter = FFileColors.begin(); Iter != FFileColors.end(); Iter++)
  10963. {
  10964. bool ImplicitMatch;
  10965. if (Iter->FileMask.Matches(FileName, Local, Directory, &MaskParams, false, ImplicitMatch) &&
  10966. !ImplicitMatch)
  10967. {
  10968. Color = Iter->Color;
  10969. break;
  10970. }
  10971. }
  10972. }
  10973. //---------------------------------------------------------------------------
  10974. void __fastcall TCustomScpExplorerForm::DirViewKeyDown(TObject * Sender, WORD & Key, TShiftState)
  10975. {
  10976. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  10977. if (!DirView->IsEditing())
  10978. {
  10979. // Handled here, instead of RemoteDirViewKeyPress (as on Login dialog),
  10980. // as VK_BACK is handled by TCustomDirView.KeyDown and we can swallow it here,
  10981. // while searching
  10982. if (Key == VK_BACK)
  10983. {
  10984. if (FIncrementalSearchState.Searching)
  10985. {
  10986. if (FIncrementalSearchState.Text.Length() <= 1)
  10987. {
  10988. ResetIncrementalSearch();
  10989. }
  10990. else
  10991. {
  10992. UnicodeString NewText = LeftStr(FIncrementalSearchState.Text, FIncrementalSearchState.Text.Length() - 1);
  10993. IncrementalSearch(NewText, false, false);
  10994. }
  10995. Key = 0;
  10996. }
  10997. }
  10998. }
  10999. }
  11000. //---------------------------------------------------------------------------
  11001. void __fastcall TCustomScpExplorerForm::DirViewKeyPress(TObject * Sender, wchar_t & Key)
  11002. {
  11003. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  11004. if (!DirView->IsEditing() && (WinConfiguration->PanelSearch != isOff))
  11005. {
  11006. // Filter control sequences.
  11007. // When not searching yet, prefer use of the space for toggling file selection
  11008. // (so we cannot incrementally search for a string starting with a space).
  11009. if ((Key > VK_SPACE) ||
  11010. ((Key == VK_SPACE) && (GetKeyState(VK_CONTROL) >= 0) && FIncrementalSearchState.Searching))
  11011. {
  11012. IncrementalSearch(FIncrementalSearchState.Text + Key, false, false);
  11013. Key = 0;
  11014. }
  11015. }
  11016. }
  11017. //---------------------------------------------------------------------------
  11018. void __fastcall TCustomScpExplorerForm::ResetIncrementalSearch()
  11019. {
  11020. if (FIncrementalSearchState.Searching)
  11021. {
  11022. FIncrementalSearchState.Reset();
  11023. DirView(osCurrent)->UpdateStatusBar();
  11024. }
  11025. }
  11026. //---------------------------------------------------------------------------
  11027. void __fastcall TCustomScpExplorerForm::IncrementalSearch(const UnicodeString & Text, bool SkipCurrent, bool Reverse)
  11028. {
  11029. TListItem * Item = SearchFile(Text, SkipCurrent, Reverse);
  11030. if (Item == NULL)
  11031. {
  11032. MessageBeep(MB_ICONHAND);
  11033. }
  11034. else
  11035. {
  11036. TCustomDirView * ADirView = DirView(osCurrent);
  11037. {
  11038. TAutoNestingCounter Guard(FIncrementalSearching);
  11039. ADirView->FocusItem(Item);
  11040. if (ADirView->NortonLike == nlOff)
  11041. {
  11042. if ((ADirView->Selected != Item) ||
  11043. (ADirView->SelCount > 1))
  11044. {
  11045. ADirView->ClearSelection();
  11046. ADirView->Selected = Item;
  11047. }
  11048. }
  11049. }
  11050. FIncrementalSearchState.Searching = true;
  11051. FIncrementalSearchState.Text = Text;
  11052. Item->MakeVisible(false);
  11053. TListItem * NextItem = SearchFile(Text, true, Reverse);
  11054. FIncrementalSearchState.HaveNext = (NextItem != NULL) && (NextItem != Item);
  11055. ADirView->UpdateStatusBar();
  11056. }
  11057. }
  11058. //---------------------------------------------------------------------------
  11059. TListItem * __fastcall TCustomScpExplorerForm::GetNextFile(TListItem * Item, bool Reverse)
  11060. {
  11061. int Index = Item->Index;
  11062. if (!Reverse)
  11063. {
  11064. Index++;
  11065. if (Index >= Item->Owner->Count)
  11066. {
  11067. Index = 0;
  11068. }
  11069. }
  11070. else
  11071. {
  11072. Index--;
  11073. if (Index < 0)
  11074. {
  11075. Index = Item->Owner->Count - 1;
  11076. }
  11077. }
  11078. return Item->Owner->Item[Index];
  11079. }
  11080. //---------------------------------------------------------------------------
  11081. TListItem * __fastcall TCustomScpExplorerForm::SearchFile(const UnicodeString & Text, bool SkipCurrent, bool Reverse)
  11082. {
  11083. TCustomDirView * ADirView = DirView(osCurrent);
  11084. if (ADirView->Items->Count == 0)
  11085. {
  11086. return NULL;
  11087. }
  11088. else
  11089. {
  11090. TListItem * CurrentItem = (ADirView->ItemFocused != NULL) ? ADirView->ItemFocused : ADirView->Items->Item[0];
  11091. TListItem * Item = CurrentItem;
  11092. if (SkipCurrent)
  11093. {
  11094. Item = DebugNotNull(GetNextFile(Item, Reverse));
  11095. }
  11096. while (true)
  11097. {
  11098. bool Matches = false;
  11099. switch (WinConfiguration->PanelSearch)
  11100. {
  11101. case isNameStartOnly:
  11102. Matches = ContainsTextSemiCaseSensitive(ADirView->ItemFileName(Item).SubString(1, Text.Length()), Text);
  11103. break;
  11104. case isName:
  11105. Matches = ContainsTextSemiCaseSensitive(ADirView->ItemFileName(Item), Text);
  11106. break;
  11107. case isAll:
  11108. int ColCount = (ADirView->DirViewStyle == dvsReport) ? ADirView->ColProperties->Count : 1;
  11109. int Index = 0;
  11110. while ((Index < ColCount) && !Matches)
  11111. {
  11112. bool Visible = (ADirView->DirViewStyle == dvsReport) ? ADirView->ColProperties->Visible[Index] : true;
  11113. if (Visible)
  11114. {
  11115. Matches = ContainsTextSemiCaseSensitive(ADirView->GetColumnText(Item, Index), Text);
  11116. }
  11117. Index++;
  11118. }
  11119. break;
  11120. }
  11121. if (Matches)
  11122. {
  11123. return Item;
  11124. }
  11125. Item = GetNextFile(Item, Reverse);
  11126. if (Item == CurrentItem)
  11127. {
  11128. return NULL;
  11129. }
  11130. }
  11131. }
  11132. }
  11133. //---------------------------------------------------------------------------
  11134. void __fastcall TCustomScpExplorerForm::DirViewExit(TObject *)
  11135. {
  11136. ResetIncrementalSearch();
  11137. }
  11138. //---------------------------------------------------------------------------
  11139. void __fastcall TCustomScpExplorerForm::CMDialogKey(TWMKeyDown & Message)
  11140. {
  11141. if (Message.CharCode == VK_TAB)
  11142. {
  11143. if (!FIncrementalSearchState.Text.IsEmpty())
  11144. {
  11145. TShiftState Shift = KeyDataToShiftState(Message.KeyData);
  11146. bool Reverse = Shift.Contains(ssShift);
  11147. IncrementalSearch(FIncrementalSearchState.Text, true, Reverse);
  11148. Message.Result = 1;
  11149. return;
  11150. }
  11151. }
  11152. else if (Message.CharCode == VK_ESCAPE)
  11153. {
  11154. if (FIncrementalSearchState.Searching)
  11155. {
  11156. ResetIncrementalSearch();
  11157. Message.Result = 1;
  11158. return;
  11159. }
  11160. }
  11161. TForm::Dispatch(&Message);
  11162. }
  11163. //---------------------------------------------------------------------------
  11164. void __fastcall TCustomScpExplorerForm::Deactivate()
  11165. {
  11166. TForm::Deactivate();
  11167. // E.g. when switching to an internal editor window
  11168. ResetIncrementalSearch();
  11169. }
  11170. //---------------------------------------------------------------------------
  11171. void __fastcall TCustomScpExplorerForm::ApplicationEventsDeactivate(TObject *)
  11172. {
  11173. // When switching to another application
  11174. ResetIncrementalSearch();
  11175. }
  11176. //---------------------------------------------------------------------------
  11177. void __fastcall TCustomScpExplorerForm::ApplicationEventsModalBegin(TObject *)
  11178. {
  11179. ResetIncrementalSearch();
  11180. }
  11181. //---------------------------------------------------------------------------
  11182. void __fastcall TCustomScpExplorerForm::DirViewChangeFocus(TObject *, TListItem *)
  11183. {
  11184. if (FIncrementalSearching <= 0)
  11185. {
  11186. ResetIncrementalSearch();
  11187. }
  11188. }
  11189. //---------------------------------------------------------------------------
  11190. void __fastcall TCustomScpExplorerForm::RemoteStatusBarMouseDown(TObject *, TMouseButton, TShiftState, int, int)
  11191. {
  11192. CountClicksForWindowPrint(this);
  11193. }
  11194. //---------------------------------------------------------------------------
  11195. void __fastcall TCustomScpExplorerForm::RemoteDirViewResize(TObject *)
  11196. {
  11197. CenterReconnectToolbar();
  11198. }
  11199. //---------------------------------------------------------------------------
  11200. void TCustomScpExplorerForm::DoExploreFile(TCustomDirView * DirView, const UnicodeString & FileName)
  11201. {
  11202. if (!FileName.IsEmpty())
  11203. {
  11204. DirView->FocusByName(FileName);
  11205. if (DirView->ItemFocused != NULL)
  11206. {
  11207. DirView->ItemFocused->Selected = true;
  11208. }
  11209. }
  11210. }
  11211. //---------------------------------------------------------------------------
  11212. void __fastcall TCustomScpExplorerForm::ExploreFile(const UnicodeString & FileName)
  11213. {
  11214. DoExploreFile(RemoteDirView, FileName);
  11215. }
  11216. //---------------------------------------------------------------------------
  11217. void __fastcall TCustomScpExplorerForm::UpdateQueueFileList()
  11218. {
  11219. if (!FQueueStatusUpdating)
  11220. {
  11221. bool Refresh;
  11222. if ((FQueueStatus != NULL) && QueueFileList->Visible)
  11223. {
  11224. Refresh = FQueueStatus->UpdateFileList(FQueueController->GetFocusedPrimaryItem(), FQueueFileList.get());
  11225. }
  11226. else
  11227. {
  11228. if (FQueueFileList->GetCount() > 0)
  11229. {
  11230. FQueueFileList->Clear();
  11231. Refresh = true;
  11232. }
  11233. }
  11234. int Count = FQueueFileList->GetCount();
  11235. if (QueueFileList->Items->Count != Count)
  11236. {
  11237. DebugAssert(Refresh);
  11238. QueueFileList->Items->Count = Count;
  11239. }
  11240. if (Refresh)
  11241. {
  11242. QueueFileList->Invalidate();
  11243. }
  11244. DebugAssert(QueueFileList->Items->Count == FQueueFileList->GetCount());
  11245. }
  11246. }
  11247. //---------------------------------------------------------------------------
  11248. void __fastcall TCustomScpExplorerForm::QueueFileListData(TObject *, TListItem * Item)
  11249. {
  11250. Item->Caption = FQueueFileList->GetFileName(Item->Index);
  11251. }
  11252. //---------------------------------------------------------------------------
  11253. void __fastcall TCustomScpExplorerForm::QueueView3Change(TObject *, TListItem *, TItemChange)
  11254. {
  11255. // Should be improved, once we do not refresh the list when switching between details of the same batch.
  11256. // See TSynchronizeChecklistDialog::UpdateTimer
  11257. if (QueueFileList->Items->Count > 0)
  11258. {
  11259. QueueFileList->Items->Item[0]->MakeVisible(false);
  11260. }
  11261. UpdateQueueFileList();
  11262. }
  11263. //---------------------------------------------------------------------------
  11264. void __fastcall TCustomScpExplorerForm::QueueLabelGetStatus(TCustomPathLabel *, bool & Active)
  11265. {
  11266. Active = QueueView3->Focused() || QueueFileList->Focused();
  11267. }
  11268. //---------------------------------------------------------------------------
  11269. void __fastcall TCustomScpExplorerForm::QueueFileListEnterExit(TObject *)
  11270. {
  11271. QueueLabelUpdateStatus();
  11272. }
  11273. //---------------------------------------------------------------------------
  11274. void __fastcall TCustomScpExplorerForm::QueueFileListCustomDrawItem(
  11275. TCustomListView * Sender, TListItem * Item, TCustomDrawState, bool & DebugUsedArg(DefaultDraw))
  11276. {
  11277. int State = FQueueFileList->GetState(Item->Index);
  11278. if (State == qfsProcessed)
  11279. {
  11280. Sender->Canvas->Font->Color = clGrayText;
  11281. }
  11282. }
  11283. //---------------------------------------------------------------------------
  11284. void __fastcall TCustomScpExplorerForm::QueueFileListColumnAutoSize()
  11285. {
  11286. // We do not really need whole width, so we always preemptivelly keep free space for the vertical scrollbar
  11287. QueueFileList->Column[0]->Width =
  11288. QueueFileList->ClientWidth - GetSystemMetricsForControl(QueueFileList, SM_CXVSCROLL) - ScaleByTextHeight(QueueFileList, 8);
  11289. }
  11290. //---------------------------------------------------------------------------
  11291. void __fastcall TCustomScpExplorerForm::QueueFileListResize(TObject *)
  11292. {
  11293. QueueFileListColumnAutoSize();
  11294. }
  11295. //---------------------------------------------------------------------------
  11296. void __fastcall TCustomScpExplorerForm::CloseApp()
  11297. {
  11298. // Called from TNonVisualDataModule::ExplorerActionsExecute, which sets busy flag.
  11299. // FormCloseQuery must check busy state, so that the application cannot be closed while busy by Alt+F4 and X button.
  11300. // So we clear the flag here.
  11301. DebugAssert(NonVisualDataModule->Busy);
  11302. NonVisualDataModule->EndBusy();
  11303. try
  11304. {
  11305. Close();
  11306. }
  11307. __finally
  11308. {
  11309. NonVisualDataModule->StartBusy();
  11310. }
  11311. }
  11312. //---------------------------------------------------------------------------
  11313. bool __fastcall TCustomScpExplorerForm::IsActiveTerminal(TTerminal * Terminal)
  11314. {
  11315. return TTerminalManager::Instance()->IsActiveTerminal(Terminal);
  11316. }
  11317. //---------------------------------------------------------------------------
  11318. bool __fastcall TCustomScpExplorerForm::HasManagedSession()
  11319. {
  11320. return (ManagedSession != NULL);
  11321. }
  11322. //---------------------------------------------------------------------------
  11323. bool __fastcall TCustomScpExplorerForm::HasActiveTerminal()
  11324. {
  11325. return IsActiveTerminal(Terminal);
  11326. }
  11327. //---------------------------------------------------------------------------
  11328. void TCustomScpExplorerForm::LocalLocalCopy(
  11329. TFileOperation, TOperationSide, bool DebugUsedArg(OnFocused), bool DebugUsedArg(NoConfirmation),
  11330. bool DebugUsedArg(DragDrop), unsigned int DebugUsedArg(Flags))
  11331. {
  11332. DebugFail();
  11333. }
  11334. //---------------------------------------------------------------------------
  11335. void TCustomScpExplorerForm::LocalLocalCopyCommand(TFileOperation Operation, TOperationSide Side, bool OnFocused, unsigned int Flags)
  11336. {
  11337. LocalLocalCopy(Operation, Side, OnFocused, !WinConfiguration->ConfirmTransferring, false, Flags);
  11338. }
  11339. //---------------------------------------------------------------------------
  11340. void TCustomScpExplorerForm::UpdateTabsSize()
  11341. {
  11342. // 1) Changing tab list triggers the OnResize.
  11343. // 2) Is called in TForm constructor, when we are not ready to call SessionListChanged (particularly when starting with colored session)
  11344. if (!FUpdatingSessionTabs && (FSessionColors != NULL))
  11345. {
  11346. // UpdateTabsCaptionTruncation should be enough
  11347. SessionListChanged(true);
  11348. }
  11349. }
  11350. //---------------------------------------------------------------------------
  11351. void __fastcall TCustomScpExplorerForm::SessionsPageControlResize(TObject *)
  11352. {
  11353. UpdateTabsSize();
  11354. }
  11355. //---------------------------------------------------------------------------
  11356. UnicodeString TCustomScpExplorerForm::GetSessionPath(TManagedTerminal * ASession, TOperationSide Side)
  11357. {
  11358. if (ASession == ManagedSession)
  11359. {
  11360. UpdateSession(ASession);
  11361. }
  11362. TSessionData * Data = ASession->StateData;
  11363. UnicodeString Result;
  11364. if (Side == osLocal)
  11365. {
  11366. Result = Data->LocalDirectory;
  11367. }
  11368. else if (DebugAlwaysTrue(Side == osOther))
  11369. {
  11370. Result = ASession->LocalBrowser ? Data->OtherLocalDirectory : Data->RemoteDirectory;
  11371. }
  11372. return Result;
  11373. }
  11374. //---------------------------------------------------------------------------
  11375. UnicodeString TCustomScpExplorerForm::GetTabHintSessionDetails(TManagedTerminal * ASession)
  11376. {
  11377. UnicodeString Title = TTerminalManager::Instance()->GetSessionTitle(ASession, false);
  11378. UnicodeString Result = ASession->SessionData->DefaultSessionName;
  11379. if (Title == Result)
  11380. {
  11381. Result = EmptyStr;
  11382. }
  11383. else
  11384. {
  11385. Result += L"\n";
  11386. }
  11387. return Result;
  11388. }
  11389. //---------------------------------------------------------------------------
  11390. UnicodeString TCustomScpExplorerForm::GetTabHintDetails(TManagedTerminal * ASession)
  11391. {
  11392. UnicodeString Result = GetTabHintSessionDetails(ASession);
  11393. if (!ASession->Active)
  11394. {
  11395. Result += LoadStr(STATUS_NOT_CONNECTED2);
  11396. }
  11397. else
  11398. {
  11399. Result += GetSessionPath(ASession, osRemote);
  11400. }
  11401. return Result;
  11402. }
  11403. //---------------------------------------------------------------------------
  11404. UnicodeString TCustomScpExplorerForm::GetNewTabHintDetails()
  11405. {
  11406. return LoadStr(NEW_REMOTE_TAB_HINT);
  11407. }
  11408. //---------------------------------------------------------------------------
  11409. void __fastcall TCustomScpExplorerForm::SessionsPageControlTabHint(TPageControl *, int Index, UnicodeString & Hint)
  11410. {
  11411. if (Index >= 0)
  11412. {
  11413. TThemeTabSheet * TabSheet = SessionsPageControl->Pages[Index];
  11414. TManagedTerminal * TabSession = GetSessionTabSession(TabSheet);
  11415. if (TabSession == NULL)
  11416. {
  11417. UnicodeString NewTabTabCaption = GetNewTabTabCaption();
  11418. if (NewTabTabCaption != TabSheet->Caption)
  11419. {
  11420. Hint = NewTabTabCaption + L"|" + GetNewTabHintDetails();
  11421. }
  11422. }
  11423. else
  11424. {
  11425. TTerminalManager * Manager = TTerminalManager::Instance();
  11426. UnicodeString TabCaption = Manager->GetSessionTitle(TabSession, true);
  11427. Hint = TabCaption + L"|" + GetTabHintDetails(TabSession);
  11428. }
  11429. }
  11430. }
  11431. //---------------------------------------------------------------------------
  11432. typedef std::vector<TListItem *> TListItemsVector;
  11433. struct TCalculateSizeOperation
  11434. {
  11435. TCalculateSizeStats Stats;
  11436. TListItemsVector ListItems;
  11437. size_t Index;
  11438. };
  11439. //---------------------------------------------------------------------------
  11440. void TCustomScpExplorerForm::CalculateDirectorySizes(TOperationSide Side)
  11441. {
  11442. TCalculateSizeOperation CalculateSizeOperation;
  11443. Side = GetSide(Side);
  11444. TCustomDirView * ADirView = DirView(Side);
  11445. bool LocalSide = IsSideLocalBrowser(Side);
  11446. std::unique_ptr<TStrings> AllFileList(ADirView->CreateFileList(false, LocalSide, NULL, true));
  11447. std::unique_ptr<TStrings> FileList(new TStringList());
  11448. for (int Index = 0; Index < AllFileList->Count; Index++)
  11449. {
  11450. TListItem * Item = DebugNotNull(dynamic_cast<TListItem *>(AllFileList->Objects[Index]));
  11451. if (ADirView->ItemIsDirectory(Item))
  11452. {
  11453. FileList->AddObject(AllFileList->Strings[Index], ADirView->ItemData(Item));
  11454. CalculateSizeOperation.ListItems.push_back(Item);
  11455. }
  11456. }
  11457. CalculateSizeOperation.Index = 0;
  11458. __int64 Size = 0;
  11459. TValueRestorer<TCalculateSizeOperation *> DirectorySizeOperationRestorer(FCalculateSizeOperation, &CalculateSizeOperation);
  11460. TValueRestorer<TFileOperationFinishedEvent> OnFileOperationFinishedRestorer(FOnFileOperationFinished);
  11461. FOnFileOperationFinished = DirectorySizeCalculated;
  11462. TValueRestorer<TFileOperation> PrimaryOperationRestorer(FPrimaryOperation, foCalculateSize);
  11463. TCalculatedSizes CalculatedSizes;
  11464. CalculateSizeOperation.Stats.CalculatedSizes = &CalculatedSizes;
  11465. if (LocalSide)
  11466. {
  11467. TValueRestorer<TOperationSide> ProgressSideRestorer(FProgressSide, Side);
  11468. ManagedSession->CalculateLocalFilesSize(FileList.get(), Size, NULL, true, NULL, &CalculatedSizes);
  11469. }
  11470. else
  11471. {
  11472. TCalculateSizeParams Params;
  11473. Params.Stats = &CalculateSizeOperation.Stats;
  11474. Params.UseCache = true;
  11475. Terminal->CalculateFilesSize(FileList.get(), Size, Params);
  11476. }
  11477. }
  11478. //---------------------------------------------------------------------------
  11479. void __fastcall TCustomScpExplorerForm::DirectorySizeCalculated(
  11480. TOperationSide Side, const UnicodeString & FileName, bool Success, bool NotCancelled)
  11481. {
  11482. DebugAlwaysTrue(FCalculateSizeOperation != NULL);
  11483. TListItem * Item = VisualiseOperationFinished(Side, FileName, false);
  11484. TCustomDirView * ADirView = dynamic_cast<TCustomDirView *>(Item->Owner->Owner);
  11485. // optimization
  11486. bool GotExpectedItem =
  11487. (FCalculateSizeOperation->Index < FCalculateSizeOperation->ListItems.size()) &&
  11488. (FCalculateSizeOperation->ListItems[FCalculateSizeOperation->Index] == Item);
  11489. int Index = -1;
  11490. if (DebugAlwaysTrue(GotExpectedItem))
  11491. {
  11492. Index = FCalculateSizeOperation->Index;
  11493. FCalculateSizeOperation->Index++;
  11494. }
  11495. else
  11496. {
  11497. TListItemsVector::iterator I =
  11498. std::find(FCalculateSizeOperation->ListItems.begin(), FCalculateSizeOperation->ListItems.end(), Item);
  11499. if (DebugAlwaysTrue(I != FCalculateSizeOperation->ListItems.end()))
  11500. {
  11501. Index = I - FCalculateSizeOperation->ListItems.begin();
  11502. }
  11503. }
  11504. if (DebugAlwaysTrue(Index >= 0) && Success && NotCancelled)
  11505. {
  11506. __int64 Size = (*FCalculateSizeOperation->Stats.CalculatedSizes)[Index];
  11507. ADirView->SetItemCalculatedSize(Item, Size);
  11508. }
  11509. }
  11510. //---------------------------------------------------------------------------
  11511. void __fastcall TCustomScpExplorerForm::MessageDockRequestDock(TObject *, TTBCustomDockableWindow *, bool & Accept)
  11512. {
  11513. Accept = false;
  11514. }
  11515. //---------------------------------------------------------------------------
  11516. void TCustomScpExplorerForm::AutoSizeColumns(TOperationSide Side)
  11517. {
  11518. // Send Ctrl+Gray+ to the list view
  11519. TKeyboardState KeyState;
  11520. GetKeyboardState(KeyState);
  11521. TKeyboardState NewKeyState;
  11522. memset(NewKeyState, 0, sizeof(NewKeyState));
  11523. NewKeyState[VK_CONTROL] = 0x81;
  11524. SetKeyboardState(NewKeyState);
  11525. SendMessage(DirView(Side)->Handle, WM_KEYDOWN, VK_ADD, 0);
  11526. SetKeyboardState(KeyState);
  11527. }
  11528. //---------------------------------------------------------------------------
  11529. void TCustomScpExplorerForm::QueueResetLayoutColumns()
  11530. {
  11531. LoadListViewStr(QueueView3, QueueViewLayoutDefault);
  11532. }
  11533. //---------------------------------------------------------------------------
  11534. void * TCustomScpExplorerForm::SaveFocus()
  11535. {
  11536. return NULL;
  11537. }
  11538. //---------------------------------------------------------------------------
  11539. void TCustomScpExplorerForm::RestoreFocus(void * Focus)
  11540. {
  11541. DebugUsedParam(Focus);
  11542. DebugAssert(Focus == NULL);
  11543. }
  11544. //---------------------------------------------------------------------------
  11545. void TCustomScpExplorerForm::IncrementalSearchStart()
  11546. {
  11547. TCustomDirView * ADirView = DirView(osCurrent);
  11548. ADirView->SetFocus();
  11549. if (!FIncrementalSearchState.Searching)
  11550. {
  11551. FIncrementalSearchState.Searching = true;
  11552. FIncrementalSearchState.HaveNext = false;
  11553. DebugAssert(FIncrementalSearchState.Text.IsEmpty());
  11554. ADirView->UpdateStatusBar();
  11555. }
  11556. }
  11557. //---------------------------------------------------------------------------
  11558. void __fastcall TCustomScpExplorerForm::RemoteDirViewThumbnailNeeded(
  11559. TUnixDirView *, TListItem * Item, TRemoteFile * File, const TSize & Size, TBitmap *& Bitmap)
  11560. {
  11561. // though when downloading to temporary folder, encryption should not be a problem
  11562. if (Terminal->IsCapable[fcBackgroundTransfers] &&
  11563. !File->IsDirectory)
  11564. {
  11565. UnicodeString FileName = File->FullFileName;
  11566. TFileMasks::TParams MaskParams;
  11567. MaskParams.Size = File->Size;
  11568. MaskParams.Modification = File->Modification;
  11569. if ((File->Size <= static_cast<__int64>(WinConfiguration->RemoteThumbnailSizeLimit) * 1024) &&
  11570. FRemoteThumbnailMask.Matches(FileName, false, File->IsDirectory, &MaskParams))
  11571. {
  11572. Bitmap = TTerminalManager::Instance()->ThumbnailNeeded(Terminal, Item->Index, File, Size);
  11573. }
  11574. }
  11575. }
  11576. //---------------------------------------------------------------------------
  11577. TThumbnailDownloadQueueItem * TCustomScpExplorerForm::AddThumbnailDownloadQueueItem(TManagedTerminal * ATerminal)
  11578. {
  11579. DebugAssert(ATerminal == Terminal);
  11580. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  11581. TemporaryFileCopyParam(CopyParam);
  11582. CopyParam.TransferMode = tmBinary;
  11583. UnicodeString SourceDir = ATerminal->CurrentDirectory;
  11584. UnicodeString TempDir, RootTempDir;
  11585. TemporaryDirectoryForRemoteFiles(SourceDir, CopyParam, true, TempDir, RootTempDir);
  11586. TTerminalQueue * AQueue = TTerminalManager::Instance()->FindQueueForTerminal(ATerminal);
  11587. DebugAssert((Queue == NULL) || (AQueue == Queue)); // we might get here before queue is set
  11588. TThumbnailDownloadQueueItem * QueueItem =
  11589. new TThumbnailDownloadQueueItem(this, Terminal, SourceDir, TempDir, &CopyParam);
  11590. AddQueueItem(AQueue, QueueItem, ATerminal);
  11591. return QueueItem;
  11592. }
  11593. //---------------------------------------------------------------------------
  11594. enum { qccRemoteItemVisible, qccRemoteItemRedraw };
  11595. typedef std::pair<int, UnicodeString> TRemoteItemVisibleData;
  11596. //---------------------------------------------------------------------------
  11597. void TCustomScpExplorerForm::PostThumbnailVisibleQueueQuery(int Index, const UnicodeString & FileName)
  11598. {
  11599. // The queue might be destroyed before the WM_QUEUE_CALLBACK is processed
  11600. TRemoteItemVisibleData * Data = new TRemoteItemVisibleData();
  11601. Data->first = Index;
  11602. Data->second = FileName;
  11603. PostMessage(Handle, WM_QUEUE_CALLBACK, qccRemoteItemVisible, reinterpret_cast<WPARAM>(Data));
  11604. }
  11605. //---------------------------------------------------------------------------
  11606. void TCustomScpExplorerForm::PostThumbnailDrawRequest(int Index)
  11607. {
  11608. PostMessage(Handle, WM_QUEUE_CALLBACK, qccRemoteItemRedraw, Index);
  11609. }
  11610. //---------------------------------------------------------------------------
  11611. void TCustomScpExplorerForm::WMQueueCallback(TMessage & Message)
  11612. {
  11613. int Command = Message.WParam;
  11614. if (Command == qccRemoteItemVisible)
  11615. {
  11616. TRemoteItemVisibleData * Data = reinterpret_cast<TRemoteItemVisibleData *>(Message.LParam);
  11617. int Index = Data->first;
  11618. UnicodeString FileName = Data->second;
  11619. delete Data;
  11620. TGuard Guard(Terminal->ThumbnailsSection.get());
  11621. bool Visible = False;
  11622. if (Terminal->ThumbnailsEnabled &&
  11623. // though we should better also disable thumbnails once we switch to different view style
  11624. (RemoteDirView->DirViewStyle == dvsThumbnail))
  11625. {
  11626. // The message might be processed, while we are already loading a different directory
  11627. if ((Index >= 0) &&
  11628. (Index < RemoteDirView->Items->Count))
  11629. {
  11630. TListItem * Item = RemoteDirView->Items->Item[Index];
  11631. Visible =
  11632. (RemoteDirView->ItemFullFileName(Item) == FileName) &&
  11633. ListView_IsItemVisible(RemoteDirView->Handle, Index);
  11634. }
  11635. }
  11636. Terminal->ThumbnailVisible(Index, FileName, Visible);
  11637. }
  11638. else if (Command == qccRemoteItemRedraw)
  11639. {
  11640. int Index = Message.LParam;
  11641. if ((Index >= 0) &&
  11642. (Index < RemoteDirView->Items->Count))
  11643. {
  11644. RemoteDirView->InvalidateItem(RemoteDirView->Items->Item[Index]);
  11645. }
  11646. }
  11647. }
  11648. //---------------------------------------------------------------------------
  11649. void TCustomScpExplorerForm::InitializeRemoteThumbnailMask()
  11650. {
  11651. UnicodeString DefaultRemoteThumbnailMask = L"*.jpg;*.jpeg;*.gif;*.png;*.svg;*.bmp;*.raw;*.ico;*.heic;*.webp";
  11652. UnicodeString RemoteThumbnailMask = WinConfiguration->RemoteThumbnailMask;
  11653. if (RemoteThumbnailMask.IsEmpty() ||
  11654. StartsStr(L"+", RemoteThumbnailMask) || StartsStr(L"-", RemoteThumbnailMask) || StartsStr(L"*", RemoteThumbnailMask))
  11655. {
  11656. std::unique_ptr<TStrings> RemoteThumbnailMasks(new TStringList());
  11657. while (!DefaultRemoteThumbnailMask.IsEmpty())
  11658. {
  11659. RemoteThumbnailMasks->Add(CutToChar(DefaultRemoteThumbnailMask, L';', true));
  11660. }
  11661. RemoteThumbnailMasks.reset(ProcessFeatures(RemoteThumbnailMasks.get(), RemoteThumbnailMask));
  11662. RemoteThumbnailMask = EmptyStr;
  11663. for (int Index = 0; Index < RemoteThumbnailMasks->Count; Index++)
  11664. {
  11665. AddToList(RemoteThumbnailMask, RemoteThumbnailMasks->Strings[Index], L";");
  11666. }
  11667. }
  11668. FRemoteThumbnailMask.Masks = RemoteThumbnailMask;
  11669. }
  11670. //---------------------------------------------------------------------------
  11671. void __fastcall TCustomScpExplorerForm::RemoteDirViewStartLoading(TObject *)
  11672. {
  11673. Terminal->StartLoadingDirectory();
  11674. }
  11675. //---------------------------------------------------------------------------
  11676. void __fastcall TCustomScpExplorerForm::RemoteDirViewStartReading(TObject *)
  11677. {
  11678. Terminal->DisableThumbnails();
  11679. }
  11680. //---------------------------------------------------------------------------
  11681. void TCustomScpExplorerForm::ChangeDirViewStyle(TOperationSide Side, TDirViewStyle DirViewStyle)
  11682. {
  11683. TCustomDirView * ADirView = DirView(Side);
  11684. if (ADirView->DirViewStyle != DirViewStyle)
  11685. {
  11686. if (DirViewStyle == dvsThumbnail)
  11687. {
  11688. if (dynamic_cast<TUnixDirView *>(ADirView) != NULL)
  11689. {
  11690. Configuration->Usage->Inc(L"ThumbnailsRemote");
  11691. }
  11692. else
  11693. {
  11694. Configuration->Usage->Inc(L"ThumbnailsLocal");
  11695. }
  11696. }
  11697. ADirView->DirViewStyle = DirViewStyle;
  11698. UpdateControls();
  11699. }
  11700. }