CustomScpExplorer.cpp 317 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765
  1. //---------------------------------------------------------------------------
  2. #define NO_WIN32_LEAN_AND_MEAN
  3. #include <vcl.h>
  4. #pragma hdrstop
  5. #include <Common.h>
  6. #include "CustomScpExplorer.h"
  7. #include <Bookmarks.h>
  8. #include <Interface.h>
  9. #include <Exceptions.h>
  10. #include <CoreMain.h>
  11. #include <FileSystems.h>
  12. #include <TextsCore.h>
  13. #include <TextsWin.h>
  14. #include <HelpWin.h>
  15. #include <VCLCommon.h>
  16. #include <Progress.h>
  17. #include <SynchronizeProgress.h>
  18. #include <DragExt.h>
  19. #include <WinApi.h>
  20. #include "GUITools.h"
  21. #include "NonVisual.h"
  22. #include "Glyphs.h"
  23. #include "Tools.h"
  24. #include "WinConfiguration.h"
  25. #include "TerminalManager.h"
  26. #include "EditorManager.h"
  27. #include "ProgParams.h"
  28. #include "Setup.h"
  29. #include <Consts.hpp>
  30. #include <DateUtils.hpp>
  31. #include <TB2Common.hpp>
  32. #include <DirectoryMonitor.hpp>
  33. //---------------------------------------------------------------------------
  34. #pragma package(smart_init)
  35. #pragma link "CustomDirView"
  36. #pragma link "CustomUnixDirView"
  37. #pragma link "IEListView"
  38. #pragma link "NortonLikeListView"
  39. #pragma link "UnixDirView"
  40. #pragma link "CustomDriveView"
  41. #pragma link "UnixDriveView"
  42. #pragma link "CustomDriveView"
  43. #pragma link "TB2Dock"
  44. #pragma link "TBXStatusBars"
  45. #pragma link "TB2Item"
  46. #pragma link "TB2Toolbar"
  47. #pragma link "ThemePageControl"
  48. #pragma link "PathLabel"
  49. #ifndef NO_RESOURCES
  50. #pragma resource "*.dfm"
  51. #endif
  52. //---------------------------------------------------------------------------
  53. #define SAVE_SELECTION(DIRVIEW) \
  54. UnicodeString FocusFile = L""; \
  55. UnicodeString LastFocusedFile = L""; \
  56. if (DIRVIEW->ItemFocused) LastFocusedFile = DIRVIEW->ItemFocused->Caption; \
  57. { TListItem * ClosestUnselected = DIRVIEW->ClosestUnselected(DIRVIEW->ItemFocused); \
  58. if (ClosestUnselected) FocusFile = ClosestUnselected->Caption; }
  59. #define RESTORE_SELECTION(DIRVIEW) \
  60. if (!LastFocusedFile.IsEmpty() && \
  61. (!DIRVIEW->ItemFocused || (DIRVIEW->ItemFocused->Caption != LastFocusedFile))) \
  62. { \
  63. TListItem *ItemToSelect = DIRVIEW->FindFileItem(FocusFile); \
  64. if (ItemToSelect) \
  65. { \
  66. DIRVIEW->ItemFocused = ItemToSelect; \
  67. DIRVIEW->ItemFocused->MakeVisible(False); \
  68. } \
  69. }
  70. //---------------------------------------------------------------------------
  71. #define WM_COMPONENT_HIDE (WM_WINSCP_USER + 4)
  72. static const int SessionPanelCount = 4;
  73. //---------------------------------------------------------------------------
  74. class TMutexGuard
  75. {
  76. public:
  77. TMutexGuard(HANDLE AMutex, int Message = MUTEX_RELEASE_TIMEOUT,
  78. int Timeout = 5000)
  79. {
  80. FMutex = NULL;
  81. unsigned long WaitResult = WaitForSingleObject(AMutex, Timeout);
  82. if (WaitResult == WAIT_TIMEOUT)
  83. {
  84. throw Exception(LoadStr(MUTEX_RELEASE_TIMEOUT));
  85. }
  86. else
  87. {
  88. FMutex = AMutex;
  89. }
  90. }
  91. ~TMutexGuard()
  92. {
  93. if (FMutex != NULL)
  94. {
  95. ReleaseMutex(FMutex);
  96. }
  97. }
  98. private:
  99. HANDLE FMutex;
  100. };
  101. //---------------------------------------------------------------------------
  102. class TWindowLock
  103. {
  104. public:
  105. TWindowLock(TCustomScpExplorerForm * Form) :
  106. FForm(Form)
  107. {
  108. FForm->LockWindow();
  109. }
  110. ~TWindowLock()
  111. {
  112. FForm->UnlockWindow();
  113. }
  114. private:
  115. TCustomScpExplorerForm * FForm;
  116. };
  117. //---------------------------------------------------------------------------
  118. struct TTransferOperationParam
  119. {
  120. TTransferOperationParam();
  121. UnicodeString TargetDirectory;
  122. bool Temp;
  123. bool DragDrop;
  124. int Options;
  125. TAutoSwitch Queue;
  126. };
  127. //---------------------------------------------------------------------------
  128. TTransferOperationParam::TTransferOperationParam()
  129. {
  130. Temp = false;
  131. DragDrop = false;
  132. Options = 0;
  133. Queue = asAuto;
  134. }
  135. //---------------------------------------------------------------------------
  136. class TTransferPresetNoteData : public TObject
  137. {
  138. public:
  139. UnicodeString Message;
  140. };
  141. //---------------------------------------------------------------------------
  142. class TTerminalNoteData : public TObject
  143. {
  144. public:
  145. TTerminal * Terminal;
  146. };
  147. //---------------------------------------------------------------------------
  148. __fastcall TCustomScpExplorerForm::TCustomScpExplorerForm(TComponent* Owner):
  149. FFormRestored(false),
  150. TForm(Owner)
  151. {
  152. FCurrentSide = osRemote;
  153. FEverShown = false;
  154. FDocks = new TList();
  155. RestoreParams();
  156. ConfigurationChanged();
  157. RemoteDirView->Invalidate();
  158. DebugAssert(NonVisualDataModule && !NonVisualDataModule->ScpExplorer);
  159. NonVisualDataModule->ScpExplorer = this;
  160. FAutoOperation = false;
  161. FOnFileOperationFinished = NULL;
  162. FForceExecution = false;
  163. FIgnoreNextDialogChar = 0;
  164. FErrorList = NULL;
  165. FSynchronizeProgressForm = NULL;
  166. FProgressForm = NULL;
  167. FRefreshLocalDirectory = false;
  168. FRefreshRemoteDirectory = false;
  169. FDDMoveSlipped = false;
  170. FDDExtMapFile = NULL;
  171. // CreateMutexW keeps failing with ERROR_NOACCESS
  172. FDDExtMutex = CreateMutexA(NULL, false, AnsiString(DRAG_EXT_MUTEX).c_str());
  173. DebugAssert(FDDExtMutex != NULL);
  174. FDDTargetControl = NULL;
  175. FDelayedDeletionTimer = NULL;
  176. FDelayedDeletionList = new TStringList();
  177. FDDFileList = NULL;
  178. FPendingTempSpaceWarn = false;
  179. FCapturedLog = NULL;
  180. FDragDropOperation = false;
  181. memset(&FHistoryMenu, 0, sizeof(FHistoryMenu));
  182. FAllowTransferPresetAutoSelect = true;
  183. FCopyParamDefault = L"";
  184. FSynchronizeController = NULL;
  185. FPendingQueueActionItem = NULL;
  186. FLockLevel = 0;
  187. FLockSuspendLevel = 0;
  188. FDisabledOnLockSuspend = false;
  189. FAlternativeDelete = false;
  190. FTrayIcon = new ::TTrayIcon(0);
  191. FTrayIcon->OnClick = TrayIconClick;
  192. FMaxQueueLength = 0;
  193. FLastContextPopupScreenPoint = TPoint(-1, -1);
  194. FTransferResumeList = NULL;
  195. FMoveToQueue = false;
  196. FStandaloneEditing = false;
  197. FOnFeedSynchronizeError = NULL;
  198. FNeedSession = false;
  199. FDoNotIdleCurrentTerminal = 0;
  200. FEditorManager = new TEditorManager();
  201. FEditorManager->OnFileChange = ExecutedFileChanged;
  202. FEditorManager->OnFileReload = ExecutedFileReload;
  203. FEditorManager->OnFileEarlyClosed = ExecutedFileEarlyClosed;
  204. FEditorManager->OnFileUploadComplete = ExecutedFileUploadComplete;
  205. FLocalEditors = new TList();
  206. FQueueStatus = NULL;
  207. FQueueStatusSection = new TCriticalSection();
  208. FQueueStatusInvalidated = false;
  209. FQueueItemInvalidated = false;
  210. FQueueActedItem = NULL;
  211. FQueueController = new TQueueController(QueueView3);
  212. FUserActionTimer = new TTimer(this);
  213. FUserActionTimer->Enabled = false;
  214. FUserActionTimer->Interval = 10;
  215. FUserActionTimer->OnTimer = UserActionTimer;
  216. FNotes = new TStringList();
  217. FNoteTimer = new TTimer(this);
  218. FNoteTimer->Enabled = false;
  219. FNoteTimer->OnTimer = NoteTimer;
  220. FOnNoteClick = NULL;
  221. FOle32Library = LoadLibrary(L"Ole32.dll");
  222. FDragMoveCursor = FOle32Library != NULL ?
  223. LoadCursor(FOle32Library, MAKEINTRESOURCE(2)) : NULL;
  224. UseSystemSettings(this);
  225. TTBXStringList * TransferList = dynamic_cast<TTBXStringList*>(
  226. static_cast<TObject*>(GetComponent(fcTransferList)));
  227. DebugAssert(TransferList != NULL);
  228. FTransferListHoverIndex = -1;
  229. TransferList->OnChange = TransferListChange;
  230. TransferList->OnDrawItem = TransferListDrawItem;
  231. SetSubmenu(dynamic_cast<TTBXCustomItem *>(static_cast<TObject *>(GetComponent(fcColorMenu))));
  232. SetSubmenu(NonVisualDataModule->ColorMenuItem);
  233. UseDesktopFont(SessionsPageControl);
  234. UpdateSessionsPageControlHeight();
  235. UseDesktopFont(RemoteDirView);
  236. UseDesktopFont(RemoteDriveView);
  237. UseDesktopFont(QueueView3);
  238. UseDesktopFont(QueueLabel);
  239. UseDesktopFont(RemoteStatusBar);
  240. reinterpret_cast<TLabel*>(QueueSplitter)->OnDblClick = QueueSplitterDblClick;
  241. QueueSplitter->ShowHint = true;
  242. RemotePanelSplitter->ShowHint = true;
  243. UpdateImages();
  244. FCustomCommandMenu = CreateTBXPopupMenu(this);
  245. FCustomCommandLocalFileList = NULL;
  246. FCustomCommandRemoteFileList = NULL;
  247. FSessionColors = new TPngImageList(this);
  248. FSessionColors->ColorDepth = cd32Bit;
  249. AddFixedSessionImages();
  250. SessionsPageControl->Images = FSessionColors;
  251. QueueLabel->FocusControl = QueueView3;
  252. UpdateQueueLabel();
  253. FRemoteDirViewWasFocused = true;
  254. CreateHiddenWindow();
  255. StartUpdates();
  256. }
  257. //---------------------------------------------------------------------------
  258. __fastcall TCustomScpExplorerForm::~TCustomScpExplorerForm()
  259. {
  260. // this has to be one of the very first things to do
  261. StopUpdateThread();
  262. SessionsPageControl->Images = NULL;
  263. SAFE_DESTROY(FSessionColors);
  264. SAFE_DESTROY(FSessionsDragDropFilesEx);
  265. delete FCustomCommandLocalFileList;
  266. delete FCustomCommandRemoteFileList;
  267. delete FCustomCommandMenu;
  268. delete FTrayIcon;
  269. FTrayIcon = NULL;
  270. FEditorManager->CloseInternalEditors(ForceCloseInternalEditor);
  271. delete FEditorManager;
  272. ForceCloseLocalEditors();
  273. delete FLocalEditors;
  274. HideFileFindDialog();
  275. if (FDelayedDeletionTimer)
  276. {
  277. DoDelayedDeletion(NULL);
  278. SAFE_DESTROY(FDelayedDeletionTimer);
  279. }
  280. SAFE_DESTROY(FDelayedDeletionList);
  281. // sometimes we do not get DDEnd so the list is not released
  282. SAFE_DESTROY(FDDFileList);
  283. DebugAssert(FSynchronizeController == NULL);
  284. CloseHandle(FDDExtMutex);
  285. FDDExtMutex = NULL;
  286. FreeLibrary(FOle32Library);
  287. FOle32Library = NULL;
  288. FDragMoveCursor = NULL;
  289. DebugAssert(!FErrorList);
  290. if (FEverShown)
  291. {
  292. // when window is never shown (like when running command-line operation),
  293. // particularly window site is not restored correctly (BoundsRect value set
  294. // in RestoreForm gets lost during handle allocation), so we do not want
  295. // it to be stored
  296. StoreParams();
  297. }
  298. Terminal = NULL;
  299. Queue = NULL;
  300. DebugAssert(NonVisualDataModule && (NonVisualDataModule->ScpExplorer == this));
  301. NonVisualDataModule->ScpExplorer = NULL;
  302. delete FQueueController;
  303. FQueueController = NULL;
  304. delete FQueueStatusSection;
  305. FQueueStatusSection = NULL;
  306. delete FQueueStatus;
  307. FQueueStatus = NULL;
  308. delete FUserActionTimer;
  309. FUserActionTimer = NULL;
  310. delete FNoteTimer;
  311. delete FNotes;
  312. delete FNoteData;
  313. SAFE_DESTROY(FDocks);
  314. SAFE_DESTROY(FHistoryMenu[0][0]);
  315. SAFE_DESTROY(FHistoryMenu[0][1]);
  316. SAFE_DESTROY(FHistoryMenu[1][0]);
  317. SAFE_DESTROY(FHistoryMenu[1][1]);
  318. if (FHiddenWindow != NULL)
  319. {
  320. DestroyWindow(FHiddenWindow);
  321. FHiddenWindow = NULL;
  322. }
  323. }
  324. //---------------------------------------------------------------------------
  325. void __fastcall TCustomScpExplorerForm::RefreshPanel(const UnicodeString & Session, const UnicodeString & Path)
  326. {
  327. std::unique_ptr<TSessionData> Data;
  328. if (!Session.IsEmpty())
  329. {
  330. bool DefaultsOnly;
  331. Data.reset(StoredSessions->ParseUrl(Session, NULL, DefaultsOnly));
  332. }
  333. TTerminalManager * Manager = TTerminalManager::Instance();
  334. for (int Index = 0; Index < Manager->Count; Index++)
  335. {
  336. TTerminal * Terminal = Manager->Terminals[Index];
  337. if (Session.IsEmpty() ||
  338. Terminal->SessionData->IsSameSite(Data.get()))
  339. {
  340. if (Path.IsEmpty())
  341. {
  342. Terminal->ClearCaches();
  343. }
  344. else
  345. {
  346. Terminal->DirectoryModified(Path, true);
  347. }
  348. }
  349. }
  350. // We should flag a pending refresh for the background terminals or busy foreground terminals
  351. if ((Terminal != NULL) && Terminal->Active &&
  352. CanCommandLineFromAnotherInstance() &&
  353. (Session.IsEmpty() || Terminal->SessionData->IsSameSite(Data.get())) &&
  354. (Path.IsEmpty() || UnixIsChildPath(Path, Terminal->CurrentDirectory)))
  355. {
  356. Terminal->ReloadDirectory();
  357. }
  358. }
  359. //---------------------------------------------------------------------------
  360. void __fastcall TCustomScpExplorerForm::WMCopyData(TMessage & Message)
  361. {
  362. PCOPYDATASTRUCT CopyData = reinterpret_cast<PCOPYDATASTRUCT>(Message.LParam);
  363. size_t MessageSize = sizeof(TCopyDataMessage);
  364. bool Result = DebugAlwaysTrue(CopyData->cbData == MessageSize);
  365. if (Result)
  366. {
  367. const TCopyDataMessage & Message = *reinterpret_cast<const TCopyDataMessage *>(CopyData->lpData);
  368. Result = (Message.Version == TCopyDataMessage::Version1);
  369. if (Result)
  370. {
  371. switch (Message.Command)
  372. {
  373. case TCopyDataMessage::CommandCanCommandLine:
  374. Result = CanCommandLineFromAnotherInstance();
  375. break;
  376. case TCopyDataMessage::CommandCommandLine:
  377. {
  378. UnicodeString CommandLine(Message.CommandLine);
  379. Result = CommandLineFromAnotherInstance(CommandLine);
  380. }
  381. break;
  382. case TCopyDataMessage::RefreshPanel:
  383. RefreshPanel(Message.Refresh.Session, Message.Refresh.Path);
  384. break;
  385. case TCopyDataMessage::MainWindowCheck:
  386. Result = true;
  387. break;
  388. default:
  389. Result = false;
  390. break;
  391. }
  392. }
  393. }
  394. Message.Result = Result ? 1 : 0;
  395. }
  396. //---------------------------------------------------------------------------
  397. void __fastcall TCustomScpExplorerForm::CreateHiddenWindow()
  398. {
  399. WNDCLASS WindowClass = {0};
  400. WindowClass.lpfnWndProc = DefWindowProc;
  401. WindowClass.hInstance = HInstance;
  402. WindowClass.lpszClassName = HIDDEN_WINDOW_NAME;
  403. FHiddenWindow = NULL;
  404. if (RegisterClass(&WindowClass))
  405. {
  406. FHiddenWindow = CreateWindow(HIDDEN_WINDOW_NAME, L"",
  407. WS_POPUP, 0, 0, 0, 0, 0, 0, HInstance, NULL);
  408. }
  409. }
  410. //---------------------------------------------------------------------------
  411. bool __fastcall TCustomScpExplorerForm::CanConsole()
  412. {
  413. return (Terminal != NULL) && (Terminal->IsCapable[fcAnyCommand] || Terminal->IsCapable[fcSecondaryShell]);
  414. }
  415. //---------------------------------------------------------------------------
  416. bool __fastcall TCustomScpExplorerForm::CanCommandLineFromAnotherInstance()
  417. {
  418. bool Result = Visible && !NonVisualDataModule->Busy;
  419. return Result;
  420. }
  421. //---------------------------------------------------------------------------
  422. bool __fastcall TCustomScpExplorerForm::CommandLineFromAnotherInstance(
  423. const UnicodeString & CommandLine)
  424. {
  425. TProgramParams Params(CommandLine);
  426. bool Result = CanCommandLineFromAnotherInstance() && DebugAlwaysTrue(Params.ParamCount > 0);
  427. if (Result)
  428. {
  429. NonVisualDataModule->StartBusy();
  430. try
  431. {
  432. // this action is initiated from another process,
  433. // so it's likely that our window is not visible,
  434. // and user won't see what is going on
  435. Application->BringToFront();
  436. // reload sessions as we may be asked to open a session
  437. // just stored by another instance
  438. StoredSessions->Reload();
  439. UnicodeString SessionName = Params.Param[1];
  440. std::unique_ptr<TObjectList> DataList(new TObjectList());
  441. UnicodeString DownloadFile; // unused
  442. try
  443. {
  444. GetLoginData(SessionName, &Params, DataList.get(), DownloadFile, true, this);
  445. }
  446. catch (EAbort &)
  447. {
  448. // Happens when opening session in PuTTY and in other situations,
  449. // in which we do not want to fail WM_COPYDATA, as that would cause master instance
  450. // to process the command-line on its own (or to pass it to yet another instance)
  451. }
  452. if (DataList->Count > 0)
  453. {
  454. TTerminalManager * Manager = TTerminalManager::Instance();
  455. Manager->ActiveTerminal = Manager->NewTerminals(DataList.get());
  456. }
  457. }
  458. __finally
  459. {
  460. NonVisualDataModule->EndBusy();
  461. }
  462. }
  463. return Result;
  464. }
  465. //---------------------------------------------------------------------------
  466. void __fastcall TCustomScpExplorerForm::SetTerminal(TTerminal * value)
  467. {
  468. if (FTerminal != value)
  469. {
  470. TerminalChanging();
  471. FTerminal = value;
  472. bool PrevAllowTransferPresetAutoSelect = FAllowTransferPresetAutoSelect;
  473. FAllowTransferPresetAutoSelect = false;
  474. try
  475. {
  476. TerminalChanged();
  477. }
  478. __finally
  479. {
  480. FAllowTransferPresetAutoSelect = PrevAllowTransferPresetAutoSelect;
  481. }
  482. if (Terminal != NULL)
  483. {
  484. TransferPresetAutoSelect();
  485. }
  486. }
  487. }
  488. //---------------------------------------------------------------------------
  489. void __fastcall TCustomScpExplorerForm::TerminalChanging()
  490. {
  491. if (FTerminal != NULL)
  492. {
  493. UpdateTerminal(Terminal);
  494. }
  495. }
  496. //---------------------------------------------------------------------------
  497. void __fastcall TCustomScpExplorerForm::TerminalChanged()
  498. {
  499. RemoteDirView->Terminal = Terminal;
  500. NonVisualDataModule->ResetQueueOnceEmptyOperation();
  501. TManagedTerminal * ManagedTerminal = NULL;
  502. if (Terminal != NULL)
  503. {
  504. if (Terminal->Active)
  505. {
  506. Terminal->RefreshDirectory();
  507. }
  508. ManagedTerminal = dynamic_cast<TManagedTerminal *>(Terminal);
  509. DebugAssert(ManagedTerminal != NULL);
  510. if (WinConfiguration->PreservePanelState)
  511. {
  512. if (ManagedTerminal->RemoteExplorerState != NULL)
  513. {
  514. RemoteDirView->RestoreState(ManagedTerminal->RemoteExplorerState);
  515. }
  516. else
  517. {
  518. RemoteDirView->ClearState();
  519. }
  520. }
  521. InitStatusBar();
  522. }
  523. DoTerminalListChanged(false);
  524. if (ManagedTerminal != NULL)
  525. {
  526. // this has to be set only after the tab is switched from DoTerminalListChanged,
  527. // otherwise we are changing color of wrong tab
  528. SessionColor = (TColor)ManagedTerminal->StateData->Color;
  529. }
  530. UpdateTransferList();
  531. // Update panels Enable state before refreshing the labels
  532. UpdateControls();
  533. }
  534. //---------------------------------------------------------------------------
  535. void __fastcall TCustomScpExplorerForm::SetQueue(TTerminalQueue * value)
  536. {
  537. if (Queue != value)
  538. {
  539. if (FQueue != NULL)
  540. {
  541. FQueue->OnListUpdate = NULL;
  542. FQueue->OnQueueItemUpdate = NULL;
  543. }
  544. FQueue = value;
  545. if (FQueue != NULL)
  546. {
  547. DebugAssert(FQueue->OnListUpdate == NULL);
  548. FQueue->OnListUpdate = QueueListUpdate;
  549. DebugAssert(FQueue->OnQueueItemUpdate == NULL);
  550. FQueue->OnQueueItemUpdate = QueueItemUpdate;
  551. }
  552. QueueChanged();
  553. }
  554. }
  555. //---------------------------------------------------------------------------
  556. void __fastcall TCustomScpExplorerForm::QueueView3Deletion(TObject * /*Sender*/,
  557. TListItem * Item)
  558. {
  559. if (FQueueActedItem == Item)
  560. {
  561. FQueueActedItem = NULL;
  562. if ((QueueView3->PopupMenu != NULL) &&
  563. (QueueView3->PopupMenu->PopupComponent == QueueView3))
  564. {
  565. // rather "trick", suggested by Jordan on jrsoftware.toolbar2000
  566. ReleaseCapture();
  567. }
  568. }
  569. if (Item->Data == FPendingQueueActionItem)
  570. {
  571. FPendingQueueActionItem = NULL;
  572. }
  573. }
  574. //---------------------------------------------------------------------------
  575. void __fastcall TCustomScpExplorerForm::UpdateQueueStatus(bool QueueChanging)
  576. {
  577. {
  578. TGuard Guard(FQueueStatusSection);
  579. FQueueStatusInvalidated = false;
  580. if (FQueue != NULL)
  581. {
  582. FQueueStatus = FQueue->CreateStatus(FQueueStatus);
  583. }
  584. }
  585. if ((FQueueStatus != NULL) && (FQueueStatus->Count > FMaxQueueLength))
  586. {
  587. FMaxQueueLength = FQueueStatus->Count;
  588. Configuration->Usage->SetMax(L"MaxQueueLength", FMaxQueueLength);
  589. }
  590. FQueueController->UpdateQueueStatus(FQueueStatus);
  591. SetQueueProgress();
  592. UpdateQueueView();
  593. bool IsEmpty = (FQueueStatus == NULL) || (FQueueStatus->Count == 0);
  594. if (IsEmpty && (Terminal != NULL))
  595. {
  596. TOnceDoneOperation OnceDoneOperation =
  597. NonVisualDataModule->CurrentQueueOnceEmptyOperation();
  598. NonVisualDataModule->ResetQueueOnceEmptyOperation();
  599. if ((FQueue != NULL) && !WinConfiguration->EnableQueueByDefault && !QueueChanging)
  600. {
  601. FQueue->Enabled = false;
  602. }
  603. if ((OnceDoneOperation != odoIdle) && !NonVisualDataModule->Busy)
  604. {
  605. Terminal->CloseOnCompletion(OnceDoneOperation, LoadStr(CLOSED_ON_QUEUE_EMPTY));
  606. }
  607. }
  608. }
  609. //---------------------------------------------------------------------------
  610. UnicodeString __fastcall TCustomScpExplorerForm::GetQueueProgressTitle()
  611. {
  612. UnicodeString Result;
  613. if (FQueueStatus != NULL)
  614. {
  615. if (FQueueStatus->IsOnlyOneActiveAndNoPending())
  616. {
  617. TFileOperationProgressType * ProgressData =
  618. FQueueStatus->Items[FQueueStatus->DoneCount]->ProgressData;
  619. if ((ProgressData != NULL) && ProgressData->InProgress)
  620. {
  621. Result = TProgressForm::ProgressStr(NULL, ProgressData);
  622. }
  623. }
  624. else if (FQueueStatus->ActiveAndPendingPrimaryCount > 1)
  625. {
  626. Result = FMTLOAD(PROGRESS_IN_QUEUE, (FQueueStatus->ActiveAndPendingPrimaryCount));
  627. }
  628. }
  629. return Result;
  630. }
  631. //---------------------------------------------------------------------------
  632. void __fastcall TCustomScpExplorerForm::UpdateQueueView()
  633. {
  634. ComponentVisible[fcQueueView] =
  635. (WinConfiguration->QueueView.Show == qvShow) ||
  636. ((WinConfiguration->QueueView.Show == qvHideWhenEmpty) &&
  637. (FQueueStatus != NULL) && (FQueueStatus->Count > 0));
  638. }
  639. //---------------------------------------------------------------------------
  640. void __fastcall TCustomScpExplorerForm::QueueChanged()
  641. {
  642. if (FQueueStatus != NULL)
  643. {
  644. delete FQueueStatus;
  645. FQueueStatus = NULL;
  646. }
  647. UpdateQueueStatus(true);
  648. }
  649. //---------------------------------------------------------------------------
  650. void __fastcall TCustomScpExplorerForm::QueueListUpdate(TTerminalQueue * Queue)
  651. {
  652. if (FQueue == Queue)
  653. {
  654. FQueueStatusInvalidated = true;
  655. }
  656. }
  657. //---------------------------------------------------------------------------
  658. void __fastcall TCustomScpExplorerForm::QueueItemUpdate(TTerminalQueue * Queue,
  659. TQueueItem * Item)
  660. {
  661. if (FQueue == Queue)
  662. {
  663. TGuard Guard(FQueueStatusSection);
  664. // this may be running in parallel with QueueChanged
  665. if (FQueueStatus != NULL)
  666. {
  667. TQueueItemProxy * QueueItem = FQueueStatus->FindByQueueItem(Item);
  668. if ((Item->Status == TQueueItem::qsDone) && (Terminal != NULL))
  669. {
  670. FRefreshLocalDirectory = (QueueItem == NULL) ||
  671. (!QueueItem->Info->ModifiedLocal.IsEmpty());
  672. FRefreshRemoteDirectory = (QueueItem == NULL) ||
  673. (!QueueItem->Info->ModifiedRemote.IsEmpty());
  674. }
  675. if (QueueItem != NULL)
  676. {
  677. QueueItem->UserData = (void*)true;
  678. FQueueItemInvalidated = true;
  679. }
  680. }
  681. }
  682. }
  683. //---------------------------------------------------------------------------
  684. bool __fastcall TCustomScpExplorerForm::IsQueueAutoPopup()
  685. {
  686. // during standalone editing, we have no way to see/control queue,
  687. // so we have to always popup prompts automatically
  688. return FStandaloneEditing || GUIConfiguration->QueueAutoPopup;
  689. }
  690. //---------------------------------------------------------------------------
  691. void __fastcall TCustomScpExplorerForm::RefreshQueueItems()
  692. {
  693. if (FQueueStatus != NULL)
  694. {
  695. bool QueueAutoPopup = IsQueueAutoPopup();
  696. bool NeedRefresh = FQueueController->NeedRefresh();
  697. bool Refresh = FQueueItemInvalidated || NeedRefresh;
  698. FQueueItemInvalidated = false;
  699. int Limit = Refresh ? FQueueStatus->Count : FQueueStatus->DoneAndActiveCount;
  700. bool Updated = false;
  701. TQueueItemProxy * QueueItem;
  702. bool WasUserAction;
  703. for (int Index = 0; Index < Limit; Index++)
  704. {
  705. bool Update = false;
  706. QueueItem = FQueueStatus->Items[Index];
  707. WasUserAction = TQueueItem::IsUserActionStatus(QueueItem->Status);
  708. if (!NonVisualDataModule->Busy && QueueAutoPopup && WasUserAction &&
  709. (FPendingQueueActionItem == NULL))
  710. {
  711. FPendingQueueActionItem = QueueItem;
  712. FUserActionTimer->Enabled = true;
  713. }
  714. if ((bool)QueueItem->UserData)
  715. {
  716. QueueItem->UserData = (void*)false;
  717. QueueItem->Update();
  718. Updated = true;
  719. Update = true;
  720. }
  721. else if (FQueueController->QueueItemNeedsFrequentRefresh(QueueItem) || NeedRefresh)
  722. {
  723. Update = true;
  724. }
  725. if (Update)
  726. {
  727. FQueueController->RefreshQueueItem(QueueItem);
  728. }
  729. }
  730. if (Updated)
  731. {
  732. NonVisualDataModule->UpdateNonVisibleActions();
  733. SetQueueProgress();
  734. }
  735. }
  736. }
  737. //---------------------------------------------------------------------------
  738. void __fastcall TCustomScpExplorerForm::SetTaskbarListProgressState(TBPFLAG Flags)
  739. {
  740. // Could now use Application->MainFormHandle, now that we implement Application->OnGetMainFormHandle
  741. FTaskbarList->SetProgressState(GetMainForm()->Handle, Flags);
  742. }
  743. //---------------------------------------------------------------------------
  744. void __fastcall TCustomScpExplorerForm::SetTaskbarListProgressValue(TFileOperationProgressType * ProgressData)
  745. {
  746. if (ProgressData->Operation != foCalculateSize)
  747. {
  748. // implies TBPF_NORMAL
  749. FTaskbarList->SetProgressValue(GetMainForm()->Handle, ProgressData->OverallProgress(), 100);
  750. }
  751. else
  752. {
  753. SetTaskbarListProgressState(TBPF_INDETERMINATE);
  754. }
  755. }
  756. //---------------------------------------------------------------------------
  757. void __fastcall TCustomScpExplorerForm::SetQueueProgress()
  758. {
  759. TTerminalManager::Instance()->QueueStatusUpdated();
  760. if ((FTaskbarList != NULL) && (FProgressForm == NULL))
  761. {
  762. if ((FQueueStatus != NULL) && (FQueueStatus->ActiveAndPendingPrimaryCount > 0))
  763. {
  764. if (FQueueStatus->IsOnlyOneActiveAndNoPending())
  765. {
  766. TFileOperationProgressType * ProgressData = NULL;
  767. if (FQueueStatus->Items[FQueueStatus->DoneCount] != NULL)
  768. {
  769. ProgressData = FQueueStatus->Items[FQueueStatus->DoneCount]->ProgressData;
  770. }
  771. if ((ProgressData != NULL) &&
  772. ProgressData->InProgress)
  773. {
  774. SetTaskbarListProgressValue(ProgressData);
  775. }
  776. else
  777. {
  778. SetTaskbarListProgressState(TBPF_NOPROGRESS);
  779. }
  780. }
  781. else
  782. {
  783. SetTaskbarListProgressState(TBPF_INDETERMINATE);
  784. }
  785. }
  786. else
  787. {
  788. SetTaskbarListProgressState(TBPF_NOPROGRESS);
  789. }
  790. }
  791. UpdateQueueLabel();
  792. }
  793. //---------------------------------------------------------------------------
  794. void __fastcall TCustomScpExplorerForm::UpdateQueueLabel()
  795. {
  796. UnicodeString Caption = LoadStr(QUEUE_CAPTION);
  797. if (FQueueStatus != NULL)
  798. {
  799. int ActiveAndPendingPrimaryCount = FQueueStatus->ActiveAndPendingPrimaryCount;
  800. if (ActiveAndPendingPrimaryCount > 0)
  801. {
  802. Caption = FORMAT("%s (%d)", (Caption, ActiveAndPendingPrimaryCount));
  803. }
  804. }
  805. QueueLabel->Caption = Caption;
  806. }
  807. //---------------------------------------------------------------------------
  808. void __fastcall TCustomScpExplorerForm::UpdateTransferList()
  809. {
  810. TTBXStringList * TransferList = dynamic_cast<TTBXStringList*>(
  811. static_cast<TComponent*>(GetComponent(fcTransferList)));
  812. TTBXDropDownItem * TransferDropDown = dynamic_cast<TTBXDropDownItem*>(
  813. static_cast<TComponent*>(GetComponent(fcTransferDropDown)));
  814. TransferList->Strings->BeginUpdate();
  815. try
  816. {
  817. TransferList->Strings->Assign(GUIConfiguration->CopyParamList->NameList);
  818. TransferList->Strings->Insert(0, StripHotkey(LoadStr(COPY_PARAM_DEFAULT)));
  819. TransferList->ItemIndex = GUIConfiguration->CopyParamIndex + 1;
  820. if (FTransferDropDownHint.IsEmpty())
  821. {
  822. FTransferDropDownHint = TransferDropDown->Hint;
  823. }
  824. // this way we get name for "default" settings (COPY_PARAM_DEFAULT)
  825. UnicodeString Name = TransferList->Strings->Strings[TransferList->ItemIndex];
  826. TransferDropDown->Text = StripHotkey(Name);
  827. TransferDropDown->Hint = FORMAT(L"%s|%s:\n%s",
  828. (FTransferDropDownHint, StripHotkey(Name),
  829. GUIConfiguration->CurrentCopyParam.GetInfoStr(L"; ",
  830. FLAGMASK(Terminal != NULL, Terminal->UsableCopyParamAttrs(0).General))));
  831. // update the label, otherwise when it is updated only on the first draw
  832. // of the list, it is drawn "bold" for some reason
  833. FTransferListHoverIndex = TransferList->ItemIndex;
  834. UpdateTransferLabel();
  835. }
  836. __finally
  837. {
  838. TransferList->Strings->EndUpdate();
  839. }
  840. }
  841. //---------------------------------------------------------------------------
  842. void __fastcall TCustomScpExplorerForm::UpdateCustomCommandsToolbar()
  843. {
  844. TTBXToolbar * Toolbar = dynamic_cast<TTBXToolbar *>(GetComponent(fcCustomCommandsBand));
  845. DebugAssert(Toolbar != NULL);
  846. NonVisualDataModule->UpdateCustomCommandsToolbar(Toolbar);
  847. }
  848. //---------------------------------------------------------------------------
  849. void __fastcall TCustomScpExplorerForm::UpdateActions()
  850. {
  851. TForm::UpdateActions();
  852. if (ComponentVisible[fcCustomCommandsBand])
  853. {
  854. UpdateCustomCommandsToolbar();
  855. }
  856. }
  857. //---------------------------------------------------------------------------
  858. void __fastcall TCustomScpExplorerForm::UpdateSessionsPageControlHeight()
  859. {
  860. SessionsPageControl->Height = SessionsPageControl->GetTabsHeight();
  861. }
  862. //---------------------------------------------------------------------------
  863. void __fastcall TCustomScpExplorerForm::ConfigurationChanged()
  864. {
  865. DebugAssert(Configuration && RemoteDirView);
  866. RemoteDirView->DDAllowMove = WinConfiguration->DDAllowMoveInit;
  867. RemoteDirView->DimmHiddenFiles = WinConfiguration->DimmHiddenFiles;
  868. RemoteDirView->ShowHiddenFiles = WinConfiguration->ShowHiddenFiles;
  869. RemoteDirView->FormatSizeBytes = WinConfiguration->FormatSizeBytes;
  870. RemoteDirView->ShowInaccesibleDirectories = WinConfiguration->ShowInaccesibleDirectories;
  871. RemoteDirView->NaturalOrderNumericalSorting = WinConfiguration->NaturalOrderNumericalSorting;
  872. if (RemoteDirView->RowSelect != WinConfiguration->FullRowSelect)
  873. {
  874. RemoteDirView->RowSelect = WinConfiguration->FullRowSelect;
  875. // selection is not redrawn automatically when RowSelect changes
  876. RemoteDirView->Invalidate();
  877. }
  878. RemoteDriveView->DDAllowMove = WinConfiguration->DDAllowMoveInit;
  879. RemoteDriveView->DimmHiddenDirs = WinConfiguration->DimmHiddenFiles;
  880. RemoteDriveView->ShowHiddenDirs = WinConfiguration->ShowHiddenFiles;
  881. RemoteDriveView->ShowInaccesibleDirectories = WinConfiguration->ShowInaccesibleDirectories;
  882. UpdateSessionsPageControlHeight();
  883. SetDockAllowDrag(!WinConfiguration->LockToolbars);
  884. UpdateToolbarDisplayMode();
  885. UpdateTransferList();
  886. if (Terminal != NULL)
  887. {
  888. TransferPresetAutoSelect();
  889. }
  890. UpdateQueueView();
  891. UpdateControls();
  892. // this can be called even before constuctor finishes.
  893. if (FEditorManager != NULL)
  894. {
  895. FEditorManager->ProcessFiles(FileConfigurationChanged, NULL);
  896. }
  897. // this can be called even before constuctor finishes.
  898. if (FLocalEditors != NULL)
  899. {
  900. for (int Index = 0; Index < FLocalEditors->Count; Index++)
  901. {
  902. ReconfigureEditorForm(static_cast<TForm *>(FLocalEditors->Items[Index]));
  903. }
  904. }
  905. if (ComponentVisible[fcCustomCommandsBand])
  906. {
  907. UpdateCustomCommandsToolbar();
  908. }
  909. // show only when keeping queue items forever,
  910. // otherwise, its enough to have in in the context menu
  911. QueueDeleteAllDoneQueueToolbarItem->Visible =
  912. WinConfiguration->QueueKeepDoneItems && (WinConfiguration->QueueKeepDoneItemsFor < 0);
  913. }
  914. //---------------------------------------------------------------------------
  915. void __fastcall TCustomScpExplorerForm::FileConfigurationChanged(
  916. const UnicodeString FileName, TEditedFileData * /*Data*/, TObject * Token,
  917. void * /*Arg*/)
  918. {
  919. if (Token != NULL)
  920. {
  921. TForm * Editor = dynamic_cast<TForm*>(Token);
  922. DebugAssert(Editor != NULL);
  923. ReconfigureEditorForm(Editor);
  924. }
  925. }
  926. //---------------------------------------------------------------------------
  927. void __fastcall TCustomScpExplorerForm::EnableDDTransferConfirmation(TObject * /*Sender*/)
  928. {
  929. WinConfiguration->DDTransferConfirmation = asOn;
  930. }
  931. //---------------------------------------------------------------------------
  932. void __fastcall TCustomScpExplorerForm::CopyParamDialogAfter(
  933. TTransferDirection /*Direction*/, bool /*Temp*/, const UnicodeString & /*TargetDirectory*/)
  934. {
  935. // noop
  936. }
  937. //---------------------------------------------------------------------------
  938. bool __fastcall TCustomScpExplorerForm::CopyParamDialog(
  939. TTransferDirection Direction, TTransferType Type, bool Temp,
  940. TStrings * FileList, UnicodeString & TargetDirectory, TGUICopyParamType & CopyParam,
  941. bool Confirm, bool DragDrop, int Options)
  942. {
  943. bool Result = true;
  944. DebugAssert(Terminal && Terminal->Active);
  945. // Temp means d&d here so far, may change in future!
  946. if (Temp && (Direction == tdToLocal) && (Type == ttMove) &&
  947. !WinConfiguration->DDAllowMove)
  948. {
  949. TMessageParams Params(mpNeverAskAgainCheck);
  950. unsigned int Answer = MessageDialog(LoadStr(DND_DOWNLOAD_MOVE_WARNING), qtWarning,
  951. qaOK | qaCancel, HELP_DND_DOWNLOAD_MOVE_WARNING, &Params);
  952. if (Answer == qaNeverAskAgain)
  953. {
  954. WinConfiguration->DDAllowMove = true;
  955. }
  956. else if (Answer == qaCancel)
  957. {
  958. Result = false;
  959. }
  960. }
  961. // these parameters are known in advance
  962. int Params =
  963. FLAGMASK(Type == ttMove, cpDelete);
  964. bool ToTemp = (Temp && (Direction == tdToLocal));
  965. if (Result && Confirm && WinConfiguration->ConfirmTransferring)
  966. {
  967. bool DisableNewerOnly =
  968. (!Terminal->IsCapable[fcNewerOnlyUpload] && (Direction == tdToRemote)) ||
  969. ToTemp;
  970. Options |=
  971. FLAGMASK(ToTemp, coTemp) |
  972. FLAGMASK(!Terminal->IsCapable[fsBackgroundTransfers], coDisableQueue) |
  973. coDoNotShowAgain;
  974. TUsableCopyParamAttrs UsableCopyParamAttrs = Terminal->UsableCopyParamAttrs(Params);
  975. int CopyParamAttrs = (Direction == tdToRemote ?
  976. UsableCopyParamAttrs.Upload : UsableCopyParamAttrs.Download) |
  977. FLAGMASK(DisableNewerOnly, cpaNoNewerOnly);
  978. int OutputOptions =
  979. FLAGMASK(DragDrop && (WinConfiguration->DDTransferConfirmation == asAuto),
  980. cooDoNotShowAgain);
  981. std::unique_ptr<TSessionData> SessionData(SessionDataForCode());
  982. Result = DoCopyDialog(Direction == tdToRemote, Type == ttMove,
  983. FileList, TargetDirectory, &CopyParam, Options, CopyParamAttrs, SessionData.get(), &OutputOptions);
  984. if (Result)
  985. {
  986. if (FLAGSET(OutputOptions, cooDoNotShowAgain))
  987. {
  988. if (DragDrop)
  989. {
  990. if (WinConfiguration->DDTransferConfirmation == asAuto)
  991. {
  992. PopupTrayBalloon(NULL, LoadStr(DD_TRANSFER_CONFIRM_OFF2), qtInformation,
  993. NULL, 0, EnableDDTransferConfirmation, NULL);
  994. }
  995. WinConfiguration->DDTransferConfirmation = asOff;
  996. }
  997. else
  998. {
  999. WinConfiguration->ConfirmTransferring = false;
  1000. }
  1001. }
  1002. else
  1003. {
  1004. // User exclicitly unchecked "do not show again",
  1005. // so show him the dialog the next time
  1006. if (DragDrop && (WinConfiguration->DDTransferConfirmation == asAuto))
  1007. {
  1008. WinConfiguration->DDTransferConfirmation = asOn;
  1009. }
  1010. }
  1011. CopyParamDialogAfter(Direction, Temp, TargetDirectory);
  1012. }
  1013. }
  1014. if (Result && CopyParam.Queue && !ToTemp && Terminal->IsCapable[fsBackgroundTransfers])
  1015. {
  1016. Configuration->Usage->Inc(L"TransfersOnBackground");
  1017. // these parameter are known only after transfer dialog
  1018. Params |=
  1019. (CopyParam.QueueNoConfirmation ? cpNoConfirmation : 0);
  1020. AddQueueItem(Queue, Direction, FileList, TargetDirectory, CopyParam, Params);
  1021. Result = false;
  1022. ClearTransferSourceSelection(Direction);
  1023. }
  1024. return Result;
  1025. }
  1026. //---------------------------------------------------------------------------
  1027. void __fastcall TCustomScpExplorerForm::ClearTransferSourceSelection(TTransferDirection Direction)
  1028. {
  1029. if (FOnFileOperationFinished != NULL)
  1030. {
  1031. FOnFileOperationFinished(UnicodeString(), true);
  1032. }
  1033. else
  1034. {
  1035. TOperationSide Side = ((Direction == tdToRemote) ? osLocal : osRemote);
  1036. if (HasDirView[Side])
  1037. {
  1038. DirView(Side)->SelectAll(smNone);
  1039. }
  1040. }
  1041. }
  1042. //---------------------------------------------------------------------------
  1043. void __fastcall TCustomScpExplorerForm::AddQueueItem(
  1044. TTerminalQueue * Queue, TTransferDirection Direction, TStrings * FileList,
  1045. const UnicodeString TargetDirectory, const TGUICopyParamType & CopyParam,
  1046. int Params)
  1047. {
  1048. DebugAssert(Queue != NULL);
  1049. bool SingleFile = false;
  1050. if (FileList->Count == 1)
  1051. {
  1052. if (Direction == tdToRemote)
  1053. {
  1054. UnicodeString FileName = FileList->Strings[0];
  1055. SingleFile = FileExists(ApiPath(FileName));
  1056. }
  1057. else
  1058. {
  1059. TRemoteFile * File = static_cast<TRemoteFile *>(FileList->Objects[0]);
  1060. SingleFile = !File->IsDirectory;
  1061. }
  1062. }
  1063. TQueueItem * QueueItem;
  1064. if (Direction == tdToRemote)
  1065. {
  1066. QueueItem = new TUploadQueueItem(Terminal, FileList, TargetDirectory,
  1067. &CopyParam, Params, SingleFile, CopyParam.QueueParallel);
  1068. }
  1069. else
  1070. {
  1071. QueueItem = new TDownloadQueueItem(Terminal, FileList, TargetDirectory,
  1072. &CopyParam, Params, SingleFile, CopyParam.QueueParallel);
  1073. }
  1074. AddQueueItem(Queue, QueueItem, Terminal);
  1075. }
  1076. //---------------------------------------------------------------------------
  1077. void __fastcall TCustomScpExplorerForm::AddQueueItem(TTerminalQueue * Queue, TQueueItem * QueueItem, TTerminal * Terminal)
  1078. {
  1079. if (Queue->IsEmpty)
  1080. {
  1081. TManagedTerminal * ManagedTerminal = DebugNotNull(dynamic_cast<TManagedTerminal *>(Terminal));
  1082. ManagedTerminal->QueueOperationStart = Now();
  1083. }
  1084. Queue->AddItem(QueueItem);
  1085. }
  1086. //---------------------------------------------------------------------------
  1087. void __fastcall TCustomScpExplorerForm::RestoreFormParams()
  1088. {
  1089. }
  1090. //---------------------------------------------------------------------------
  1091. void __fastcall TCustomScpExplorerForm::RestoreParams()
  1092. {
  1093. DebugAssert(FDocks != NULL);
  1094. for (int Index = 0; Index < ComponentCount; Index++)
  1095. {
  1096. TTBDock * Dock = dynamic_cast<TTBDock *>(Components[Index]);
  1097. if ((Dock != NULL) && (Dock->Tag == 0))
  1098. {
  1099. FDocks->Add(Dock);
  1100. }
  1101. }
  1102. CollectItemsWithTextDisplayMode(this);
  1103. ConfigurationChanged();
  1104. QueuePanel->Height =
  1105. LoadDimension(WinConfiguration->QueueView.Height, WinConfiguration->QueueView.HeightPixelsPerInch, this);
  1106. LoadListViewStr(QueueView3, WinConfiguration->QueueView.Layout);
  1107. QueueDock->Visible = WinConfiguration->QueueView.ToolBar;
  1108. QueueLabel->Visible = WinConfiguration->QueueView.Label;
  1109. }
  1110. //---------------------------------------------------------------------------
  1111. void __fastcall TCustomScpExplorerForm::StoreParams()
  1112. {
  1113. WinConfiguration->QueueView.Height = QueuePanel->Height;
  1114. WinConfiguration->QueueView.HeightPixelsPerInch = GetControlPixelsPerInch(this);
  1115. WinConfiguration->QueueView.Layout = GetListViewStr(QueueView3);
  1116. WinConfiguration->QueueView.ToolBar = QueueDock->Visible;
  1117. WinConfiguration->QueueView.Label = QueueLabel->Visible;
  1118. }
  1119. //---------------------------------------------------------------------------
  1120. void __fastcall TCustomScpExplorerForm::CreateParams(TCreateParams & Params)
  1121. {
  1122. if (!FFormRestored)
  1123. {
  1124. FFormRestored = true;
  1125. RestoreFormParams();
  1126. }
  1127. TForm::CreateParams(Params);
  1128. }
  1129. //---------------------------------------------------------------------------
  1130. void __fastcall TCustomScpExplorerForm::SetDockAllowDrag(bool value)
  1131. {
  1132. DebugAssert(FDocks != NULL);
  1133. for (int Index = 0; Index < FDocks->Count; Index++)
  1134. {
  1135. static_cast<TTBDock*>(FDocks->Items[Index])->AllowDrag = value;
  1136. }
  1137. }
  1138. //---------------------------------------------------------------------------
  1139. void __fastcall TCustomScpExplorerForm::LoadToolbarsLayoutStr(UnicodeString LayoutStr, UnicodeString ButtonsStr)
  1140. {
  1141. SetDockAllowDrag(true);
  1142. while (!ButtonsStr.IsEmpty())
  1143. {
  1144. UnicodeString S = CutToChar(ButtonsStr, L';', true);
  1145. UnicodeString Name = CutToChar(S, L'=', true);
  1146. for (int ToolbarIndex = 0; ToolbarIndex < ComponentCount; ToolbarIndex++)
  1147. {
  1148. TTBCustomToolbar * Toolbar = dynamic_cast<TTBCustomToolbar *>(Components[ToolbarIndex]);
  1149. if ((Toolbar != NULL) && SameText(Name, GetToolbarKey(Toolbar->Name)))
  1150. {
  1151. while (!S.IsEmpty())
  1152. {
  1153. UnicodeString S2 = CutToChar(S, L',', true);
  1154. UnicodeString Name = CutToChar(S2, L':', true);
  1155. for (int ItemIndex = 0; ItemIndex < Toolbar->Items->Count; ItemIndex++)
  1156. {
  1157. TTBCustomItem * Item = Toolbar->Items->Items[ItemIndex];
  1158. if (SameText(GetToolbarItemName(Item), Name))
  1159. {
  1160. // forward compatibility
  1161. UnicodeString S3 = CutToChar(S2, L':', true);
  1162. bool Visible = StrToIntDef(S3, 0);
  1163. Item->Visible = Visible;
  1164. }
  1165. }
  1166. }
  1167. }
  1168. }
  1169. }
  1170. ::LoadToolbarsLayoutStr(this, LayoutStr);
  1171. SetDockAllowDrag(!WinConfiguration->LockToolbars);
  1172. }
  1173. //---------------------------------------------------------------------------
  1174. UnicodeString __fastcall TCustomScpExplorerForm::GetToolbarsLayoutStr()
  1175. {
  1176. UnicodeString Result;
  1177. SetDockAllowDrag(true);
  1178. Result = ::GetToolbarsLayoutStr(this);
  1179. SetDockAllowDrag(!WinConfiguration->LockToolbars);
  1180. return Result;
  1181. }
  1182. //---------------------------------------------------------------------------
  1183. UnicodeString __fastcall TCustomScpExplorerForm::GetToolbarItemName(TTBCustomItem * Item)
  1184. {
  1185. UnicodeString Result;
  1186. if (Item->Action != NULL)
  1187. {
  1188. Result = Item->Action->Name;
  1189. Result = RemoveSuffix(Result, L"Action");
  1190. }
  1191. else
  1192. {
  1193. Result = Item->Name;
  1194. Result = RemoveSuffix(Result, L"SubmenuItem");
  1195. Result = RemoveSuffix(Result, L"Item");
  1196. }
  1197. return Result;
  1198. }
  1199. //---------------------------------------------------------------------------
  1200. UnicodeString __fastcall TCustomScpExplorerForm::GetToolbarsButtonsStr()
  1201. {
  1202. UnicodeString Result;
  1203. for (int ToolbarIndex = 0; ToolbarIndex < ComponentCount; ToolbarIndex++)
  1204. {
  1205. UnicodeString ToolbarStr;
  1206. TTBCustomToolbar * Toolbar = dynamic_cast<TTBCustomToolbar *>(Components[ToolbarIndex]);
  1207. if ((Toolbar != NULL) && (Toolbar != QueueToolbar))
  1208. {
  1209. for (int ItemIndex = 0; ItemIndex < Toolbar->Items->Count; ItemIndex++)
  1210. {
  1211. TTBCustomItem * Item = Toolbar->Items->Items[ItemIndex];
  1212. // Currently all buttons are visible by default, so we can safely remember all hidden buttons.
  1213. // Once we introduce any buttons that aer hidden by default, we would have to remember their initial state
  1214. // and save the changes here only.
  1215. if (!Item->Visible)
  1216. {
  1217. UnicodeString Name = GetToolbarItemName(Item);
  1218. DebugAssert(Name.Pos(L"TBX") == 0);
  1219. AddToList(ToolbarStr, FORMAT(L"%s:0", (Name)), L",");
  1220. }
  1221. }
  1222. }
  1223. if (!ToolbarStr.IsEmpty())
  1224. {
  1225. AddToList(Result, FORMAT(L"%s=%s", (GetToolbarKey(Toolbar->Name), ToolbarStr)), L";");
  1226. }
  1227. }
  1228. return Result;
  1229. }
  1230. //---------------------------------------------------------------------------
  1231. void __fastcall TCustomScpExplorerForm::CreateProgressForm(const TSynchronizeChecklist * SynchronizeChecklist)
  1232. {
  1233. DebugAssert(FProgressForm == NULL);
  1234. FProgressForm =
  1235. new TProgressForm(Application, (FTransferResumeList != NULL), Terminal->IsCapable[fsSkipTransfer], SynchronizeChecklist);
  1236. FProgressForm->DeleteLocalToRecycleBin =
  1237. (WinConfiguration->DeleteToRecycleBin != FAlternativeDelete);
  1238. FProgressForm->DeleteRemoteToRecycleBin =
  1239. (Terminal->SessionData->DeleteToRecycleBin != FAlternativeDelete) &&
  1240. !Terminal->SessionData->RecycleBinPath.IsEmpty();
  1241. // As progress window has delayed show now, we need to lock ourselves, (at least) until then
  1242. LockWindow();
  1243. if (FTaskbarList != NULL)
  1244. {
  1245. // Actually, do not know what hides the progress once the operation finishes
  1246. SetTaskbarListProgressState(TBPF_NORMAL);
  1247. }
  1248. }
  1249. //---------------------------------------------------------------------------
  1250. void __fastcall TCustomScpExplorerForm::DestroyProgressForm()
  1251. {
  1252. UnlockWindow();
  1253. SAFE_DESTROY(FProgressForm);
  1254. SetQueueProgress();
  1255. }
  1256. //---------------------------------------------------------------------------
  1257. void __fastcall TCustomScpExplorerForm::FileOperationProgress(
  1258. TFileOperationProgressType & ProgressData)
  1259. {
  1260. // Download to temporary local directory
  1261. if (FPendingTempSpaceWarn && ProgressData.InProgress && ProgressData.TotalSizeSet)
  1262. {
  1263. bool Continue = true;
  1264. FPendingTempSpaceWarn = false;
  1265. DoWarnLackOfTempSpace(ProgressData.Directory, ProgressData.TotalSize, Continue);
  1266. if (!Continue)
  1267. {
  1268. Abort();
  1269. }
  1270. }
  1271. // operation is being executed and we still didn't show up progress form
  1272. if (ProgressData.InProgress && !FProgressForm)
  1273. {
  1274. CreateProgressForm(NULL);
  1275. // When main window is hidden, synchronisation form does not exist (we do not get here during synchronization - only comparing),
  1276. // we suppose "/upload" or URL download mode
  1277. if (!Visible && (ProgressData.Operation != foCalculateSize) &&
  1278. (ProgressData.Operation != foCalculateChecksum) &&
  1279. (FSynchronizeProgressForm == NULL) &&
  1280. !FStandaloneEditing)
  1281. {
  1282. FProgressForm->OnceDoneOperation = odoDisconnect;
  1283. }
  1284. }
  1285. // operation is finished (or terminated), so we hide progress form
  1286. else if (!ProgressData.InProgress && (FProgressForm != NULL) && (FProgressForm->SynchronizeChecklist == NULL))
  1287. {
  1288. DestroyProgressForm();
  1289. if (ProgressData.Operation == foCalculateSize)
  1290. {
  1291. // When calculating size before transfer, the abort caused by
  1292. // cancel flag set due to "MoveToQueue" below,
  1293. // is not propagated back to us in ExecuteFileOperation,
  1294. // as it is silently swallowed.
  1295. // So we explicitly (re)throw it here.
  1296. if (FMoveToQueue)
  1297. {
  1298. Abort();
  1299. }
  1300. }
  1301. else
  1302. {
  1303. if ((ProgressData.Cancel == csContinue) &&
  1304. !FAutoOperation)
  1305. {
  1306. OperationComplete(ProgressData.StartTime);
  1307. }
  1308. }
  1309. }
  1310. if (FProgressForm &&
  1311. // Can happen only during synchronization, when ending one of the operations
  1312. ProgressData.InProgress)
  1313. {
  1314. FProgressForm->SetProgressData(ProgressData);
  1315. if (FTaskbarList != NULL)
  1316. {
  1317. DebugAssert(ProgressData.InProgress);
  1318. SetTaskbarListProgressValue(&ProgressData);
  1319. }
  1320. if (FProgressForm->Cancel > csContinue)
  1321. {
  1322. ProgressData.SetCancelAtLeast(FProgressForm->Cancel);
  1323. if (FProgressForm->Cancel == csCancelFile)
  1324. {
  1325. FProgressForm->ClearCancel();
  1326. }
  1327. // cancel cancels even the move
  1328. FMoveToQueue = false;
  1329. }
  1330. else if (FProgressForm->MoveToQueue)
  1331. {
  1332. FMoveToQueue = true;
  1333. ProgressData.SetCancelAtLeast(csCancel);
  1334. }
  1335. if ((FTransferResumeList != NULL) &&
  1336. ProgressData.InProgress &&
  1337. ((ProgressData.Operation == foCopy) || (ProgressData.Operation == foMove)) &&
  1338. !ProgressData.FullFileName.IsEmpty())
  1339. {
  1340. if ((FTransferResumeList->Count == 0) ||
  1341. (FTransferResumeList->Strings[FTransferResumeList->Count - 1] != ProgressData.FullFileName))
  1342. {
  1343. // note that we do not recognize directories from files here
  1344. FTransferResumeList->Add(ProgressData.FullFileName);
  1345. }
  1346. }
  1347. }
  1348. }
  1349. //---------------------------------------------------------------------------
  1350. void __fastcall TCustomScpExplorerForm::OperationComplete(
  1351. const TDateTime & StartTime)
  1352. {
  1353. if (GUIConfiguration->BeepOnFinish &&
  1354. (Now() - StartTime > GUIConfiguration->BeepOnFinishAfter))
  1355. {
  1356. UnicodeString BeepSound = GUIConfiguration->BeepSound;
  1357. DWORD Sound;
  1358. if (!ExtractFileExt(BeepSound).IsEmpty())
  1359. {
  1360. Sound = SND_FILENAME;
  1361. }
  1362. else
  1363. {
  1364. Sound = SND_ALIAS;
  1365. }
  1366. PlaySound(BeepSound.c_str(), NULL, Sound | SND_ASYNC);
  1367. }
  1368. }
  1369. //---------------------------------------------------------------------------
  1370. void __fastcall TCustomScpExplorerForm::OperationProgress(
  1371. TFileOperationProgressType & ProgressData)
  1372. {
  1373. // For example, when calling TFileOperationProgressType::Suspend in TTerminal::QueryReopen,
  1374. // we cannot recurse back to TTerminal::Idle as that may detect another error, calling "suspend" again.
  1375. TAutoNestingCounter Counter(FDoNotIdleCurrentTerminal);
  1376. FileOperationProgress(ProgressData);
  1377. }
  1378. //---------------------------------------------------------------------------
  1379. UnicodeString __fastcall TCustomScpExplorerForm::GetProgressTitle(const TFileOperationProgressType & ProgressData)
  1380. {
  1381. UnicodeString Result;
  1382. if (ProgressData.InProgress)
  1383. {
  1384. const TSynchronizeChecklist * SynchronizeChecklist = NULL;
  1385. if (FProgressForm != NULL)
  1386. {
  1387. SynchronizeChecklist = FProgressForm->SynchronizeChecklist;
  1388. }
  1389. Result = TProgressForm::ProgressStr(SynchronizeChecklist, &ProgressData);
  1390. }
  1391. return Result;
  1392. }
  1393. //---------------------------------------------------------------------------
  1394. bool __fastcall TCustomScpExplorerForm::PanelOperation(TOperationSide /*Side*/,
  1395. bool DragDrop)
  1396. {
  1397. return (!DragDrop && (DropSourceControl == NULL)) ||
  1398. (DropSourceControl == RemoteDirView);
  1399. }
  1400. //---------------------------------------------------------------------------
  1401. void __fastcall TCustomScpExplorerForm::DoOperationFinished(
  1402. TFileOperation Operation, TOperationSide Side,
  1403. bool /*Temp*/, const UnicodeString & FileName, bool Success,
  1404. TOnceDoneOperation & OnceDoneOperation)
  1405. {
  1406. if (!FAutoOperation)
  1407. {
  1408. // no selection on "/upload", form servers only as event handler
  1409. // (it is not displayed)
  1410. if (PanelOperation(Side, FDragDropOperation) &&
  1411. Visible && (Operation != foCalculateSize) &&
  1412. (Operation != foGetProperties) && (Operation != foCalculateChecksum))
  1413. {
  1414. if (FOnFileOperationFinished != NULL)
  1415. {
  1416. FOnFileOperationFinished(FileName, Success);
  1417. }
  1418. else
  1419. {
  1420. TCustomDirView * DView = DirView(Side);
  1421. UnicodeString FileNameOnly = ExtractFileName(FileName, (Side == osRemote));
  1422. TListItem *Item = DView->FindFileItem(FileNameOnly);
  1423. // this can happen when local drive is unplugged in the middle of the operation
  1424. if (Item != NULL)
  1425. {
  1426. if (Success) Item->Selected = false;
  1427. DView->MakeProgressVisible(Item);
  1428. }
  1429. }
  1430. }
  1431. if ((Operation == foCopy) || (Operation == foMove))
  1432. {
  1433. if (Side == osLocal)
  1434. {
  1435. Configuration->Usage->Inc(L"UploadedFiles");
  1436. }
  1437. else
  1438. {
  1439. Configuration->Usage->Inc(L"DownloadedFiles");
  1440. }
  1441. }
  1442. }
  1443. if (Success && (FSynchronizeController != NULL))
  1444. {
  1445. if (Operation == foCopy)
  1446. {
  1447. DebugAssert(Side == osLocal);
  1448. FSynchronizeController->LogOperation(soUpload, FileName);
  1449. }
  1450. else if (Operation == foDelete)
  1451. {
  1452. DebugAssert(Side == osRemote);
  1453. FSynchronizeController->LogOperation(soDelete, FileName);
  1454. }
  1455. }
  1456. if (FProgressForm)
  1457. {
  1458. OnceDoneOperation = FProgressForm->OnceDoneOperation;
  1459. }
  1460. }
  1461. //---------------------------------------------------------------------------
  1462. void __fastcall TCustomScpExplorerForm::OperationFinished(
  1463. TFileOperation Operation, TOperationSide Side,
  1464. bool Temp, const UnicodeString & FileName, Boolean Success,
  1465. TOnceDoneOperation & OnceDoneOperation)
  1466. {
  1467. TAutoNestingCounter Counter(FDoNotIdleCurrentTerminal);
  1468. DoOperationFinished(Operation, Side, Temp, FileName, Success,
  1469. OnceDoneOperation);
  1470. }
  1471. //---------------------------------------------------------------------------
  1472. TCustomDirView * __fastcall TCustomScpExplorerForm::DirView(TOperationSide Side)
  1473. {
  1474. DebugAssert(GetSide(Side) == osRemote);
  1475. DebugUsedParam(Side);
  1476. return RemoteDirView;
  1477. }
  1478. //---------------------------------------------------------------------------
  1479. bool __fastcall TCustomScpExplorerForm::DirViewEnabled(TOperationSide Side)
  1480. {
  1481. DebugAssert(GetSide(Side) == osRemote);
  1482. DebugUsedParam(Side);
  1483. return (Terminal != NULL);
  1484. }
  1485. //---------------------------------------------------------------------------
  1486. bool __fastcall TCustomScpExplorerForm::GetEnableFocusedOperation(
  1487. TOperationSide Side, int FilesOnly)
  1488. {
  1489. return DirView(Side)->AnyFileSelected(true, (FilesOnly != 0), true);
  1490. }
  1491. //---------------------------------------------------------------------------
  1492. bool __fastcall TCustomScpExplorerForm::GetEnableSelectedOperation(
  1493. TOperationSide Side, int FilesOnly)
  1494. {
  1495. return DirView(Side)->AnyFileSelected(false, (FilesOnly != 0), true);
  1496. }
  1497. //---------------------------------------------------------------------------
  1498. struct THistoryItemData
  1499. {
  1500. short int Side;
  1501. short int Index;
  1502. };
  1503. //---------------------------------------------------------------------------
  1504. void __fastcall TCustomScpExplorerForm::HistoryGo(TOperationSide Side, int Index)
  1505. {
  1506. DirView(Side)->HistoryGo(Index);
  1507. }
  1508. //---------------------------------------------------------------------------
  1509. void __fastcall TCustomScpExplorerForm::HistoryItemClick(System::TObject* Sender)
  1510. {
  1511. TTBCustomItem * Item = dynamic_cast<TTBCustomItem *>(Sender);
  1512. THistoryItemData Data = *reinterpret_cast<THistoryItemData*>(&(Item->Tag));
  1513. HistoryGo((TOperationSide)Data.Side, Data.Index);
  1514. }
  1515. //---------------------------------------------------------------------------
  1516. void __fastcall TCustomScpExplorerForm::UpdateHistoryMenu(TOperationSide Side,
  1517. bool Back)
  1518. {
  1519. if (FHistoryMenu[Side == osLocal][Back] != NULL)
  1520. {
  1521. TCustomDirView * DView = DirView(Side);
  1522. TTBXPopupMenu * Menu = FHistoryMenu[Side == osLocal][Back];
  1523. int ICount = Back ? DView->BackCount : DView->ForwardCount;
  1524. if (ICount > 10)
  1525. {
  1526. ICount = 10;
  1527. }
  1528. Menu->Items->Clear();
  1529. THistoryItemData Data;
  1530. Data.Side = Side;
  1531. for (short int i = 1; i <= ICount; i++)
  1532. {
  1533. TTBCustomItem * Item = new TTBXItem(Menu);
  1534. Data.Index = static_cast<short int>(i * (Back ? -1 : 1));
  1535. Item->Caption = MinimizeName(DView->HistoryPath[Data.Index], 50, (Side == osRemote));
  1536. Item->Hint = DView->HistoryPath[Data.Index];
  1537. DebugAssert(sizeof(int) == sizeof(THistoryItemData));
  1538. Item->Tag = *reinterpret_cast<int*>(&Data);
  1539. Item->OnClick = HistoryItemClick;
  1540. Menu->Items->Add(Item);
  1541. }
  1542. }
  1543. }
  1544. //---------------------------------------------------------------------------
  1545. TTBXPopupMenu * __fastcall TCustomScpExplorerForm::HistoryMenu(
  1546. TOperationSide Side, bool Back)
  1547. {
  1548. if (FHistoryMenu[Side == osLocal][Back] == NULL)
  1549. {
  1550. // workaround
  1551. // In Pascal the size of TTBXPopupMenu is 132, in C++ 136,
  1552. // operator new allocates memory in Pascal code, but calls inline
  1553. // contructor in C++, leading in problems, the function does
  1554. // both in Pascal code
  1555. FHistoryMenu[Side == osLocal][Back] = CreateTBXPopupMenu(this);
  1556. UpdateHistoryMenu(Side, Back);
  1557. }
  1558. return FHistoryMenu[Side == osLocal][Back];
  1559. }
  1560. //---------------------------------------------------------------------------
  1561. void __fastcall TCustomScpExplorerForm::DirViewHistoryChange(
  1562. TCustomDirView *Sender)
  1563. {
  1564. TOperationSide Side = (Sender == DirView(osRemote) ? osRemote : osLocal);
  1565. UpdateHistoryMenu(Side, true);
  1566. UpdateHistoryMenu(Side, false);
  1567. }
  1568. //---------------------------------------------------------------------------
  1569. bool __fastcall TCustomScpExplorerForm::CustomCommandRemoteAllowed()
  1570. {
  1571. // remote custom commands can be executed only if the server supports shell commands
  1572. // or have secondary shell
  1573. return (FTerminal != NULL) && (FTerminal->IsCapable[fcSecondaryShell] || FTerminal->IsCapable[fcShellAnyCommand]);
  1574. }
  1575. //---------------------------------------------------------------------------
  1576. int __fastcall TCustomScpExplorerForm::CustomCommandState(
  1577. const TCustomCommandType & Command, bool OnFocused, TCustomCommandListType ListType)
  1578. {
  1579. int Result;
  1580. TFileCustomCommand RemoteCustomCommand;
  1581. TLocalCustomCommand LocalCustomCommand;
  1582. TFileCustomCommand * NonInteractiveCustomCommand =
  1583. FLAGCLEAR(Command.Params, ccLocal) ? &RemoteCustomCommand : &LocalCustomCommand;
  1584. TInteractiveCustomCommand InteractiveCustomCommand(NonInteractiveCustomCommand);
  1585. UnicodeString Cmd = InteractiveCustomCommand.Complete(Command.Command, false);
  1586. if (FLAGCLEAR(Command.Params, ccLocal))
  1587. {
  1588. int AllowedState = CustomCommandRemoteAllowed() ? 1 : 0;
  1589. // custom command that does not operate with files can be executed anytime ...
  1590. if (!NonInteractiveCustomCommand->IsFileCommand(Cmd))
  1591. {
  1592. if ((ListType == ccltAll) || (ListType == ccltNonFile))
  1593. {
  1594. // ... but do not show such command in remote file menu (TODO)
  1595. Result = AllowedState;
  1596. }
  1597. else
  1598. {
  1599. Result = -1;
  1600. }
  1601. }
  1602. else
  1603. {
  1604. if ((ListType == ccltAll) || (ListType == ccltFile))
  1605. {
  1606. Result = ((FCurrentSide == osRemote) && DirView(osRemote)->AnyFileSelected(OnFocused, false, true)) ? AllowedState : 0;
  1607. }
  1608. else
  1609. {
  1610. Result = -1;
  1611. }
  1612. }
  1613. }
  1614. else
  1615. {
  1616. // custom command that does not operate with files can be executed anytime
  1617. if (!NonInteractiveCustomCommand->IsFileCommand(Cmd))
  1618. {
  1619. Result = ((ListType == ccltAll) || (ListType == ccltNonFile)) ? 1 : -1;
  1620. }
  1621. else if (LocalCustomCommand.HasLocalFileName(Cmd))
  1622. {
  1623. if ((ListType == ccltAll) || (ListType == ccltFile))
  1624. {
  1625. // special case is "diff"-style command that can be executed over any side,
  1626. // if we have both sides
  1627. Result =
  1628. // Cannot have focus on both panels, so we have to call AnyFileSelected
  1629. // directly (instead of EnableSelectedOperation) to pass
  1630. // false to FocusedFileOnlyWhenFocused when panel is inactive.
  1631. ((HasDirView[osLocal] && DirView(osLocal)->AnyFileSelected(false, false, (FCurrentSide == osLocal))) &&
  1632. DirView(osRemote)->AnyFileSelected(false, false, (FCurrentSide == osRemote))) ? 1 : 0;
  1633. }
  1634. else if (ListType == ccltBoth)
  1635. {
  1636. Result = 1;
  1637. }
  1638. else
  1639. {
  1640. Result = -1;
  1641. }
  1642. }
  1643. else
  1644. {
  1645. if ((ListType == ccltAll) || (ListType == ccltFile))
  1646. {
  1647. Result = DirView(FCurrentSide)->AnyFileSelected(OnFocused, false, true) ? 1 : 0;
  1648. }
  1649. else
  1650. {
  1651. Result = -1;
  1652. }
  1653. }
  1654. }
  1655. return Result;
  1656. }
  1657. //---------------------------------------------------------------------------
  1658. void __fastcall TCustomScpExplorerForm::RemoteCustomCommand(
  1659. TStrings * FileList, const TCustomCommandType & ACommand,
  1660. const TCustomCommandData & Data, const UnicodeString & CommandCommand)
  1661. {
  1662. if (EnsureCommandSessionFallback(fcShellAnyCommand))
  1663. {
  1664. TRemoteCustomCommand RemoteCustomCommand(Data, Terminal->CurrentDirectory);
  1665. TWinInteractiveCustomCommand InteractiveCustomCommand(
  1666. &RemoteCustomCommand, ACommand.Name, ACommand.HomePage);
  1667. UnicodeString Command = InteractiveCustomCommand.Complete(CommandCommand, false);
  1668. Configuration->Usage->Inc(L"RemoteCustomCommandRuns2");
  1669. bool Capture =
  1670. FLAGSET(ACommand.Params, ccShowResults) ||
  1671. FLAGSET(ACommand.Params, ccCopyResults) ||
  1672. FLAGSET(ACommand.Params, ccShowResultsInMsgBox);
  1673. TCaptureOutputEvent OutputEvent = NULL;
  1674. DebugAssert(FCapturedLog == NULL);
  1675. if (Capture)
  1676. {
  1677. FCapturedLog = new TStringList();
  1678. OutputEvent = TerminalCaptureLog;
  1679. }
  1680. try
  1681. {
  1682. if (!RemoteCustomCommand.IsFileCommand(Command))
  1683. {
  1684. Terminal->AnyCommand(RemoteCustomCommand.Complete(Command, true),
  1685. OutputEvent);
  1686. }
  1687. else
  1688. {
  1689. Terminal->CustomCommandOnFiles(Command, ACommand.Params, FileList, OutputEvent);
  1690. }
  1691. if ((FCapturedLog != NULL) && (FCapturedLog->Count > 0))
  1692. {
  1693. if (FLAGSET(ACommand.Params, ccCopyResults))
  1694. {
  1695. CopyToClipboard(FCapturedLog);
  1696. }
  1697. if (FLAGSET(ACommand.Params, ccShowResults))
  1698. {
  1699. DoConsoleDialog(Terminal, L"", FCapturedLog);
  1700. }
  1701. else if (FLAGSET(ACommand.Params, ccShowResultsInMsgBox))
  1702. {
  1703. MessageDialog(FCapturedLog->Text, qtInformation, qaOK);
  1704. }
  1705. }
  1706. }
  1707. __finally
  1708. {
  1709. SAFE_DESTROY(FCapturedLog);
  1710. }
  1711. }
  1712. }
  1713. //---------------------------------------------------------------------------
  1714. void __fastcall TCustomScpExplorerForm::LocalCustomCommandPure(
  1715. TStrings * FileList, const TCustomCommandType & ACommand, const UnicodeString & Command, TStrings * ALocalFileList,
  1716. const TCustomCommandData & Data, bool LocalFileCommand, bool FileListCommand, UnicodeString * POutput)
  1717. {
  1718. TStrings * LocalFileList = NULL;
  1719. TStrings * RemoteFileList = NULL;
  1720. TStrings * RemoteFileListFull = NULL;
  1721. try
  1722. {
  1723. if (LocalFileCommand)
  1724. {
  1725. if (ALocalFileList == NULL)
  1726. {
  1727. DebugAssert(HasDirView[osLocal]);
  1728. // Cannot have focus on both panels, so we have to call AnyFileSelected
  1729. // directly (instead of EnableSelectedOperation) to pass
  1730. // false to FocusedFileOnlyWhenFocused
  1731. DebugAssert(DirView(osLocal)->AnyFileSelected(false, false, false));
  1732. LocalFileList = DirView(osLocal)->CreateFileList(false, true, NULL);
  1733. }
  1734. else
  1735. {
  1736. LocalFileList = ALocalFileList;
  1737. }
  1738. if (FileListCommand)
  1739. {
  1740. if (LocalFileList->Count != 1)
  1741. {
  1742. throw Exception(LoadStr(CUSTOM_COMMAND_SELECTED_UNMATCH1));
  1743. }
  1744. }
  1745. else
  1746. {
  1747. if ((LocalFileList->Count != 1) &&
  1748. (FileList->Count != 1) &&
  1749. (LocalFileList->Count != FileList->Count))
  1750. {
  1751. throw Exception(LoadStr(CUSTOM_COMMAND_SELECTED_UNMATCH));
  1752. }
  1753. }
  1754. }
  1755. UnicodeString RootTempDir;
  1756. UnicodeString TempDir;
  1757. bool RemoteFiles = FLAGSET(ACommand.Params, ccRemoteFiles);
  1758. if (!RemoteFiles)
  1759. {
  1760. TemporarilyDownloadFiles(FileList, false, RootTempDir, TempDir, false, false, true);
  1761. }
  1762. try
  1763. {
  1764. TDateTimes RemoteFileTimes;
  1765. if (RemoteFiles)
  1766. {
  1767. RemoteFileList = FileList;
  1768. }
  1769. else
  1770. {
  1771. RemoteFileList = new TStringList();
  1772. TMakeLocalFileListParams MakeFileListParam;
  1773. MakeFileListParam.FileList = RemoteFileList;
  1774. MakeFileListParam.FileTimes = &RemoteFileTimes;
  1775. MakeFileListParam.IncludeDirs = FLAGSET(ACommand.Params, ccApplyToDirectories);
  1776. MakeFileListParam.Recursive =
  1777. FLAGSET(ACommand.Params, ccRecursive) && !FileListCommand;
  1778. ProcessLocalDirectory(TempDir, Terminal->MakeLocalFileList, &MakeFileListParam);
  1779. if (!MakeFileListParam.Recursive)
  1780. {
  1781. RemoteFileTimes.clear();
  1782. RemoteFileListFull = new TStringList();
  1783. MakeFileListParam.FileList = RemoteFileListFull;
  1784. MakeFileListParam.Recursive = true;
  1785. MakeFileListParam.IncludeDirs = false;
  1786. ProcessLocalDirectory(TempDir, Terminal->MakeLocalFileList, &MakeFileListParam);
  1787. }
  1788. else
  1789. {
  1790. RemoteFileListFull = RemoteFileList;
  1791. }
  1792. }
  1793. bool NonBlocking = FileListCommand && RemoteFiles && !POutput;
  1794. TFileOperationProgressType Progress(&OperationProgress, &OperationFinished);
  1795. if (!NonBlocking)
  1796. {
  1797. Progress.Start(foCustomCommand, osRemote, FileListCommand ? 1 : FileList->Count);
  1798. DebugAssert(FProgressForm != NULL);
  1799. FProgressForm->ReadOnly = true;
  1800. }
  1801. try
  1802. {
  1803. if (FileListCommand)
  1804. {
  1805. UnicodeString LocalFile;
  1806. // MakeFileList does not delimit filenames
  1807. UnicodeString FileList = MakeFileList(RemoteFileList);
  1808. if (LocalFileCommand)
  1809. {
  1810. DebugAssert(LocalFileList->Count == 1);
  1811. LocalFile = LocalFileList->Strings[0];
  1812. }
  1813. TLocalCustomCommand CustomCommand(Data,
  1814. Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(), L"", LocalFile, FileList);
  1815. UnicodeString ShellCommand = CustomCommand.Complete(Command, true);
  1816. if (NonBlocking)
  1817. {
  1818. DebugAssert(!POutput);
  1819. ExecuteShellChecked(ShellCommand);
  1820. }
  1821. else
  1822. {
  1823. ExecuteProcessCheckedAndWait(ShellCommand, HelpKeyword, POutput);
  1824. }
  1825. }
  1826. else if (LocalFileCommand)
  1827. {
  1828. if (LocalFileList->Count == 1)
  1829. {
  1830. UnicodeString LocalFile = LocalFileList->Strings[0];
  1831. for (int Index = 0; Index < RemoteFileList->Count; Index++)
  1832. {
  1833. UnicodeString FileName = RemoteFileList->Strings[Index];
  1834. TLocalCustomCommand CustomCommand(Data,
  1835. Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(), FileName, LocalFile, L"");
  1836. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  1837. }
  1838. }
  1839. else if (RemoteFileList->Count == 1)
  1840. {
  1841. UnicodeString FileName = RemoteFileList->Strings[0];
  1842. for (int Index = 0; Index < LocalFileList->Count; Index++)
  1843. {
  1844. TLocalCustomCommand CustomCommand(
  1845. Data, Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(),
  1846. FileName, LocalFileList->Strings[Index], L"");
  1847. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  1848. }
  1849. }
  1850. else
  1851. {
  1852. if (LocalFileList->Count != RemoteFileList->Count)
  1853. {
  1854. throw Exception(LoadStr(CUSTOM_COMMAND_PAIRS_DOWNLOAD_FAILED));
  1855. }
  1856. for (int Index = 0; Index < LocalFileList->Count; Index++)
  1857. {
  1858. UnicodeString FileName = RemoteFileList->Strings[Index];
  1859. TLocalCustomCommand CustomCommand(
  1860. Data, Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(),
  1861. FileName, LocalFileList->Strings[Index], L"");
  1862. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  1863. }
  1864. }
  1865. }
  1866. else
  1867. {
  1868. for (int Index = 0; Index < RemoteFileList->Count; Index++)
  1869. {
  1870. TLocalCustomCommand CustomCommand(Data,
  1871. Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(),
  1872. RemoteFileList->Strings[Index], L"", L"");
  1873. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  1874. }
  1875. }
  1876. }
  1877. __finally
  1878. {
  1879. if (!NonBlocking)
  1880. {
  1881. Progress.Stop();
  1882. }
  1883. }
  1884. DebugAssert(!FAutoOperation);
  1885. if (!RemoteFiles)
  1886. {
  1887. TempDir = IncludeTrailingBackslash(TempDir);
  1888. for (int Index = 0; Index < RemoteFileListFull->Count; Index++)
  1889. {
  1890. UnicodeString FileName = RemoteFileListFull->Strings[Index];
  1891. if (DebugAlwaysTrue(SameText(TempDir, FileName.SubString(1, TempDir.Length()))) &&
  1892. // Skip directories, process only nested files.
  1893. // The check is redundant as FileAge fails for directories anyway.
  1894. !DirectoryExists(FileName))
  1895. {
  1896. UnicodeString RemoteDir =
  1897. UnixExtractFileDir(
  1898. UnixIncludeTrailingBackslash(FTerminal->CurrentDirectory) +
  1899. ToUnixPath(FileName.SubString(TempDir.Length() + 1, FileName.Length() - TempDir.Length())));
  1900. TDateTime NewTime;
  1901. if (FileAge(FileName, NewTime) &&
  1902. (NewTime != RemoteFileTimes[Index]))
  1903. {
  1904. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  1905. TemporaryFileCopyParam(CopyParam);
  1906. CopyParam.FileMask = L"";
  1907. FAutoOperation = true;
  1908. std::unique_ptr<TStrings> TemporaryFilesList(new TStringList());
  1909. TemporaryFilesList->Add(FileName);
  1910. FTerminal->CopyToRemote(TemporaryFilesList.get(), RemoteDir, &CopyParam, cpTemporary, NULL);
  1911. }
  1912. }
  1913. }
  1914. }
  1915. }
  1916. __finally
  1917. {
  1918. FAutoOperation = false;
  1919. if (!RootTempDir.IsEmpty() && DebugAlwaysTrue(!RemoteFiles))
  1920. {
  1921. RecursiveDeleteFile(ExcludeTrailingBackslash(RootTempDir), false);
  1922. }
  1923. }
  1924. }
  1925. __finally
  1926. {
  1927. if (RemoteFileList != FileList)
  1928. {
  1929. delete RemoteFileList;
  1930. }
  1931. if (LocalFileList != ALocalFileList)
  1932. {
  1933. delete LocalFileList;
  1934. }
  1935. if (RemoteFileListFull != RemoteFileList)
  1936. {
  1937. delete RemoteFileListFull;
  1938. }
  1939. }
  1940. }
  1941. //---------------------------------------------------------------------------
  1942. void __fastcall TCustomScpExplorerForm::LocalCustomCommandWithRemoteFiles(
  1943. const TCustomCommandType & ACommand, const UnicodeString & Command, const TCustomCommandData & Data,
  1944. bool FileListCommand, UnicodeString * POutput)
  1945. {
  1946. std::unique_ptr<TStrings> SelectedFileList(DirView(osLocal)->CreateFileList(false, true, NULL));
  1947. std::unique_ptr<TStrings> LocalFileList(new TStringList());
  1948. for (int Index = 0; Index < SelectedFileList->Count; Index++)
  1949. {
  1950. UnicodeString FileName = SelectedFileList->Strings[Index];
  1951. if (DirectoryExists(FileName))
  1952. {
  1953. if (FLAGSET(ACommand.Params, ccApplyToDirectories))
  1954. {
  1955. LocalFileList->Add(FileName);
  1956. }
  1957. if (FLAGSET(ACommand.Params, ccRecursive))
  1958. {
  1959. TMakeLocalFileListParams MakeFileListParam;
  1960. MakeFileListParam.FileList = LocalFileList.get();
  1961. MakeFileListParam.FileTimes = NULL;
  1962. MakeFileListParam.IncludeDirs = FLAGSET(ACommand.Params, ccApplyToDirectories);
  1963. MakeFileListParam.Recursive = true;
  1964. ProcessLocalDirectory(FileName, Terminal->MakeLocalFileList, &MakeFileListParam);
  1965. }
  1966. }
  1967. else
  1968. {
  1969. LocalFileList->Add(FileName);
  1970. }
  1971. }
  1972. if (FileListCommand)
  1973. {
  1974. UnicodeString FileList = MakeFileList(LocalFileList.get());
  1975. TLocalCustomCommand CustomCommand(
  1976. Data, Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(),
  1977. L"", L"", FileList);
  1978. ExecuteProcessChecked(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  1979. }
  1980. else
  1981. {
  1982. TFileOperationProgressType Progress(&OperationProgress, &OperationFinished);
  1983. Progress.Start(foCustomCommand, osRemote, FileListCommand ? 1 : LocalFileList->Count);
  1984. DebugAssert(FProgressForm != NULL);
  1985. FProgressForm->ReadOnly = true;
  1986. try
  1987. {
  1988. for (int Index = 0; Index < LocalFileList->Count; Index++)
  1989. {
  1990. UnicodeString FileName = LocalFileList->Strings[Index];
  1991. TLocalCustomCommand CustomCommand(
  1992. Data, Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(),
  1993. FileName, L"", L"");
  1994. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  1995. }
  1996. }
  1997. __finally
  1998. {
  1999. Progress.Stop();
  2000. }
  2001. }
  2002. }
  2003. //---------------------------------------------------------------------------
  2004. void __fastcall TCustomScpExplorerForm::LocalCustomCommand(TStrings * FileList,
  2005. const TCustomCommandType & ACommand, TStrings * ALocalFileList,
  2006. const TCustomCommandData & Data, const UnicodeString & CommandCommand)
  2007. {
  2008. TLocalCustomCommand LocalCustomCommand(Data, Terminal->CurrentDirectory, DefaultDownloadTargetDirectory());
  2009. TWinInteractiveCustomCommand InteractiveCustomCommand(
  2010. &LocalCustomCommand, ACommand.Name, ACommand.HomePage);
  2011. UnicodeString Command = InteractiveCustomCommand.Complete(CommandCommand, false);
  2012. bool FileListCommand = LocalCustomCommand.IsFileListCommand(Command);
  2013. bool LocalFileCommand = LocalCustomCommand.HasLocalFileName(Command);
  2014. Configuration->Usage->Inc(L"LocalCustomCommandRuns2");
  2015. std::unique_ptr<UnicodeString> POutput;
  2016. if (FLAGSET(ACommand.Params, ccShowResultsInMsgBox) ||
  2017. FLAGSET(ACommand.Params, ccCopyResults))
  2018. {
  2019. POutput.reset(new UnicodeString());
  2020. }
  2021. if (!LocalCustomCommand.IsFileCommand(Command))
  2022. {
  2023. ExecuteProcessChecked(LocalCustomCommand.Complete(Command, true), HelpKeyword, POutput.get());
  2024. }
  2025. // remote files?
  2026. else if ((FCurrentSide == osRemote) || LocalFileCommand)
  2027. {
  2028. LocalCustomCommandPure(FileList, ACommand, Command, ALocalFileList, Data, LocalFileCommand, FileListCommand, POutput.get());
  2029. }
  2030. // local files
  2031. else
  2032. {
  2033. LocalCustomCommandWithRemoteFiles(ACommand, Command, Data, FileListCommand, POutput.get());
  2034. }
  2035. if (POutput.get() != NULL)
  2036. {
  2037. // If the output is single-line, we do not want the trailing CRLF, as the CopyToClipboard(TStrings *) does
  2038. int P = POutput->Pos(sLineBreak);
  2039. if (P == POutput->Length() - static_cast<int>(strlen(sLineBreak)) + 1)
  2040. {
  2041. POutput->SetLength(P - 1);
  2042. }
  2043. // Copy even empty output to clipboard
  2044. if (FLAGSET(ACommand.Params, ccCopyResults))
  2045. {
  2046. CopyToClipboard(*POutput);
  2047. }
  2048. // But do not show an empty message box.
  2049. // This way the ShowResultsInMsgBox can be used to suppress a console window of commands with no output
  2050. if (FLAGSET(ACommand.Params, ccShowResultsInMsgBox) &&
  2051. !POutput->IsEmpty())
  2052. {
  2053. TClipboardHandler ClipboardHandler;
  2054. ClipboardHandler.Text = *POutput;
  2055. TMessageParams Params;
  2056. TQueryButtonAlias Aliases[1];
  2057. Aliases[0].Button = qaRetry;
  2058. Aliases[0].Alias = LoadStr(EDIT_COPY);
  2059. Aliases[0].OnSubmit = &ClipboardHandler.Copy;
  2060. Params.Aliases = Aliases;
  2061. Params.AliasesCount = LENOF(Aliases);
  2062. MessageDialog(*POutput, qtInformation, qaOK | qaRetry, HelpKeyword, &Params);
  2063. }
  2064. }
  2065. }
  2066. //---------------------------------------------------------------------------
  2067. void __fastcall TCustomScpExplorerForm::CustomCommand(TStrings * FileList,
  2068. const TCustomCommandType & ACommand, TStrings * ALocalFileList)
  2069. {
  2070. TCustomCommandData Data(Terminal);
  2071. UnicodeString Site = Terminal->SessionData->SessionKey;
  2072. UnicodeString HelpKeyword = ACommand.HomePage;
  2073. std::unique_ptr<TStrings> CustomCommandOptions(CloneStrings(WinConfiguration->CustomCommandOptions));
  2074. if (ACommand.AnyOptionWithFlag(TCustomCommandType::ofRun))
  2075. {
  2076. std::unique_ptr<TCustomCommand> CustomCommandForOptions;
  2077. if (FLAGCLEAR(ACommand.Params, ccLocal))
  2078. {
  2079. CustomCommandForOptions.reset(new TRemoteCustomCommand(Data, Terminal->CurrentDirectory));
  2080. }
  2081. else
  2082. {
  2083. CustomCommandForOptions.reset(new TLocalCustomCommand(Data, Terminal->CurrentDirectory, DefaultDownloadTargetDirectory()));
  2084. }
  2085. if (!DoCustomCommandOptionsDialog(
  2086. &ACommand, CustomCommandOptions.get(), NULL, TCustomCommandType::ofRun, CustomCommandForOptions.get(), Site, NULL))
  2087. {
  2088. Abort();
  2089. }
  2090. }
  2091. UnicodeString CommandCommand = ACommand.GetCommandWithExpandedOptions(CustomCommandOptions.get(), Site);
  2092. if (FLAGCLEAR(ACommand.Params, ccLocal))
  2093. {
  2094. RemoteCustomCommand(FileList, ACommand, Data, CommandCommand);
  2095. }
  2096. else
  2097. {
  2098. LocalCustomCommand(FileList, ACommand, ALocalFileList, Data, CommandCommand);
  2099. }
  2100. }
  2101. //---------------------------------------------------------------------------
  2102. void __fastcall TCustomScpExplorerForm::BothCustomCommand(
  2103. const TCustomCommandType & Command)
  2104. {
  2105. DebugAssert(FCustomCommandLocalFileList != NULL);
  2106. DebugAssert(FCustomCommandRemoteFileList != NULL);
  2107. DebugAssert(FCustomCommandLocalFileList->Count == FCustomCommandRemoteFileList->Count);
  2108. TStrings * LocalFileList = new TStringList();
  2109. TStrings * RemoteFileList = new TStringList();
  2110. try
  2111. {
  2112. for (int Index = 0; Index < FCustomCommandLocalFileList->Count; Index++)
  2113. {
  2114. LocalFileList->Clear();
  2115. LocalFileList->AddObject(
  2116. FCustomCommandLocalFileList->Strings[Index],
  2117. FCustomCommandLocalFileList->Objects[Index]);
  2118. RemoteFileList->Clear();
  2119. RemoteFileList->AddObject(
  2120. FCustomCommandRemoteFileList->Strings[Index],
  2121. FCustomCommandRemoteFileList->Objects[Index]);
  2122. CustomCommand(RemoteFileList, Command, LocalFileList);
  2123. }
  2124. }
  2125. __finally
  2126. {
  2127. delete LocalFileList;
  2128. delete RemoteFileList;
  2129. }
  2130. }
  2131. //---------------------------------------------------------------------------
  2132. void __fastcall TCustomScpExplorerForm::CustomCommandMenu(
  2133. TAction * Action, TStrings * LocalFileList, TStrings * RemoteFileList)
  2134. {
  2135. delete FCustomCommandLocalFileList;
  2136. delete FCustomCommandRemoteFileList;
  2137. // takeover ownership,
  2138. // the lists must survive the MenuPopup as OnClick occurs only after it exits
  2139. FCustomCommandLocalFileList = LocalFileList;
  2140. FCustomCommandRemoteFileList = RemoteFileList;
  2141. TButton * Button = dynamic_cast<TButton *>(Action->ActionComponent);
  2142. if (Button != NULL)
  2143. {
  2144. FCustomCommandMenu->Items->Clear();
  2145. NonVisualDataModule->CreateCustomCommandsMenu(FCustomCommandMenu->Items, false, false, ccltBoth);
  2146. MenuPopup(FCustomCommandMenu, Button);
  2147. }
  2148. else
  2149. {
  2150. NonVisualDataModule->CreateCustomCommandsMenu(Action, false, ccltBoth);
  2151. }
  2152. }
  2153. //---------------------------------------------------------------------------
  2154. void __fastcall TCustomScpExplorerForm::TerminalCaptureLog(
  2155. const UnicodeString & AddedLine, TCaptureOutputType OutputType)
  2156. {
  2157. DebugAssert(FCapturedLog != NULL);
  2158. if ((OutputType == cotOutput) || (OutputType == cotError))
  2159. {
  2160. FCapturedLog->Add(AddedLine);
  2161. }
  2162. }
  2163. //---------------------------------------------------------------------------
  2164. bool __fastcall TCustomScpExplorerForm::IsFileControl(TObject * Control,
  2165. TOperationSide Side)
  2166. {
  2167. return (Side == osRemote) &&
  2168. ((Control == RemoteDirView) || (Control == RemoteDriveView));
  2169. }
  2170. //---------------------------------------------------------------------------
  2171. void __fastcall TCustomScpExplorerForm::DirViewContextPopupDefaultItem(
  2172. TOperationSide Side, TTBXCustomItem * Item, TDoubleClickAction DoubleClickAction)
  2173. {
  2174. TTBItemOptions O;
  2175. O = Item->Options;
  2176. TCustomDirView * DView = DirView(Side);
  2177. if ((DView->ItemFocused != NULL) &&
  2178. (WinConfiguration->DoubleClickAction == DoubleClickAction) &&
  2179. // when resolving links is disabled, default action is to enter the directory,
  2180. // no matter what DoubleClickAction is configured to
  2181. ((Side != osRemote) || Terminal->ResolvingSymlinks || Terminal->IsEncryptingFiles()) &&
  2182. // Can only Edit files, but can Open/Copy even directories
  2183. ((DoubleClickAction != dcaEdit) ||
  2184. !DView->ItemIsDirectory(DView->ItemFocused)))
  2185. {
  2186. Item->Options = O << tboDefault;
  2187. }
  2188. else
  2189. {
  2190. Item->Options = O >> tboDefault;
  2191. }
  2192. }
  2193. //---------------------------------------------------------------------------
  2194. void __fastcall TCustomScpExplorerForm::DirViewContextPopup(
  2195. TOperationSide Side, Byte PopupComponent, const TPoint &MousePos)
  2196. {
  2197. TCustomDirView * DView = DirView(Side);
  2198. TListItem * Item = DView->ItemFocused;
  2199. if ((DView->GetItemAt(MousePos.x, MousePos.y) == Item) &&
  2200. EnableFocusedOperation[Side])
  2201. {
  2202. TPoint ClientPoint;
  2203. ClientPoint = ((MousePos.x < 0) && (MousePos.y < 0)) ?
  2204. TPoint(0, 0) : MousePos;
  2205. FLastContextPopupScreenPoint = DView->ClientToScreen(ClientPoint);
  2206. reinterpret_cast<TPopupMenu*>(GetComponent(PopupComponent))->Popup(
  2207. FLastContextPopupScreenPoint.x, FLastContextPopupScreenPoint.y);
  2208. }
  2209. }
  2210. //---------------------------------------------------------------------------
  2211. void __fastcall TCustomScpExplorerForm::RemoteDirViewContextPopup(
  2212. TObject * /*Sender*/, const TPoint &MousePos, bool &Handled)
  2213. {
  2214. DirViewContextPopupDefaultItem(osRemote, NonVisualDataModule->RemoteOpenMenuItem, dcaOpen);
  2215. DirViewContextPopupDefaultItem(osRemote, NonVisualDataModule->RemoteEditMenuItem, dcaEdit);
  2216. DirViewContextPopupDefaultItem(osRemote, NonVisualDataModule->RemoteCopyMenuItem, dcaCopy);
  2217. DirViewContextPopup(osRemote, fcRemotePopup, MousePos);
  2218. Handled = true;
  2219. }
  2220. //---------------------------------------------------------------------------
  2221. void __fastcall TCustomScpExplorerForm::ReloadLocalDirectory(const UnicodeString Directory)
  2222. {
  2223. }
  2224. //---------------------------------------------------------------------------
  2225. void __fastcall TCustomScpExplorerForm::BatchStart(void *& /*Storage*/)
  2226. {
  2227. DebugAssert(FErrorList == NULL);
  2228. if (WinConfiguration->ContinueOnError)
  2229. {
  2230. FErrorList = new TStringList();
  2231. Configuration->Usage->Inc(L"ContinuationsOnError");
  2232. }
  2233. NonVisualDataModule->StartBusy();
  2234. }
  2235. //---------------------------------------------------------------------------
  2236. void __fastcall TCustomScpExplorerForm::BatchEnd(void * /*Storage*/)
  2237. {
  2238. NonVisualDataModule->EndBusy();
  2239. if (FErrorList)
  2240. {
  2241. HandleErrorList(FErrorList);
  2242. }
  2243. }
  2244. //---------------------------------------------------------------------------
  2245. void __fastcall TCustomScpExplorerForm::UpdateCopyParamCounters(
  2246. const TCopyParamType & CopyParam)
  2247. {
  2248. if (!CopyParam.IncludeFileMask.Masks.IsEmpty())
  2249. {
  2250. Configuration->Usage->Inc(L"FileMaskUses");
  2251. }
  2252. if (IsEffectiveFileNameMask(CopyParam.FileMask))
  2253. {
  2254. Configuration->Usage->Inc(L"OperationMaskUses");
  2255. }
  2256. }
  2257. //---------------------------------------------------------------------------
  2258. bool __fastcall TCustomScpExplorerForm::ExecuteFileOperation(TFileOperation Operation,
  2259. TOperationSide Side, TStrings * FileList, bool NoConfirmation, void * Param)
  2260. {
  2261. void * BatchStorage;
  2262. BatchStart(BatchStorage);
  2263. bool Result;
  2264. try
  2265. {
  2266. if ((Operation == foCopy) || (Operation == foMove))
  2267. {
  2268. TTransferDirection Direction = (Side == osLocal ? tdToRemote : tdToLocal);
  2269. TTransferType Type = (Operation == foCopy ? ttCopy : ttMove);
  2270. UnicodeString TargetDirectory;
  2271. bool Temp = false;
  2272. bool DragDrop = false;
  2273. int Options = 0;
  2274. TAutoSwitch UseQueue = asAuto;
  2275. if (Param != NULL)
  2276. {
  2277. TTransferOperationParam& TParam =
  2278. *static_cast<TTransferOperationParam*>(Param);
  2279. TargetDirectory = TParam.TargetDirectory;
  2280. Temp = TParam.Temp;
  2281. DragDrop = TParam.DragDrop;
  2282. Options = TParam.Options;
  2283. UseQueue = TParam.Queue;
  2284. }
  2285. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  2286. switch (UseQueue)
  2287. {
  2288. case asOn:
  2289. CopyParam.Queue = true;
  2290. break;
  2291. case asOff:
  2292. CopyParam.Queue = false;
  2293. break;
  2294. case asAuto:
  2295. default:
  2296. // keep default
  2297. break;
  2298. }
  2299. Result =
  2300. CopyParamDialog(Direction, Type, Temp, FileList, TargetDirectory,
  2301. CopyParam, !NoConfirmation, DragDrop, Options);
  2302. if (Result)
  2303. {
  2304. DebugAssert(Terminal);
  2305. bool SelectionRestored = false;
  2306. TCustomDirView * DView = NULL;
  2307. if (HasDirView[Side])
  2308. {
  2309. DView = DirView(Side);
  2310. DView->SaveSelection();
  2311. DView->SaveSelectedNames();
  2312. }
  2313. UpdateCopyParamCounters(CopyParam);
  2314. std::unique_ptr<TStringList> TransferResumeList(new TStringList());
  2315. DebugAssert(FTransferResumeList == NULL);
  2316. FTransferResumeList =
  2317. Terminal->IsCapable[fcMoveToQueue] ? TransferResumeList.get() : NULL;
  2318. FMoveToQueue = false;
  2319. int Params = FLAGMASK(Operation == foMove, cpDelete);
  2320. try
  2321. {
  2322. TStrings * PermanentFileList;
  2323. std::unique_ptr<TStrings> PermanentFileListOwner;
  2324. try
  2325. {
  2326. if (Side == osLocal)
  2327. {
  2328. PermanentFileList = FileList;
  2329. Params |= FLAGMASK(Temp, cpTemporary);
  2330. Terminal->CopyToRemote(FileList, TargetDirectory, &CopyParam, Params, NULL);
  2331. if (Operation == foMove)
  2332. {
  2333. ReloadLocalDirectory();
  2334. if (DView != NULL)
  2335. {
  2336. DView->RestoreSelection();
  2337. }
  2338. SelectionRestored = true;
  2339. }
  2340. }
  2341. else
  2342. {
  2343. // Clone the file list as it may refer to current directory files,
  2344. // which get destroyed, when the source directory is reloaded after foMove operation.
  2345. // We should actually clone the file list for whole ExecuteFileOperation to protect against reloads.
  2346. // But for a hotfix, we are not going to do such a big change.
  2347. PermanentFileListOwner.reset(TRemoteFileList::CloneStrings(FileList));
  2348. PermanentFileList = PermanentFileListOwner.get();
  2349. try
  2350. {
  2351. Terminal->CopyToLocal(
  2352. FileList, TargetDirectory, &CopyParam, Params, NULL);
  2353. }
  2354. __finally
  2355. {
  2356. if (Operation == foMove)
  2357. {
  2358. if (DView != NULL)
  2359. {
  2360. DView->RestoreSelection();
  2361. }
  2362. SelectionRestored = true;
  2363. }
  2364. ReloadLocalDirectory(TargetDirectory);
  2365. }
  2366. }
  2367. }
  2368. catch (EAbort &)
  2369. {
  2370. if (FMoveToQueue)
  2371. {
  2372. Params |=
  2373. (CopyParam.QueueNoConfirmation ? cpNoConfirmation : 0);
  2374. DebugAssert(CopyParam.TransferSkipList == NULL);
  2375. DebugAssert(CopyParam.TransferResumeFile.IsEmpty());
  2376. if (TransferResumeList->Count > 0)
  2377. {
  2378. CopyParam.TransferResumeFile = TransferResumeList->Strings[TransferResumeList->Count - 1];
  2379. TransferResumeList->Delete(TransferResumeList->Count - 1);
  2380. }
  2381. CopyParam.TransferSkipList = TransferResumeList.release();
  2382. // not really needed, just to keep it consistent with TransferResumeList
  2383. FTransferResumeList = NULL;
  2384. FMoveToQueue = false;
  2385. Configuration->Usage->Inc("MovesToBackground");
  2386. AddQueueItem(Queue, Direction, PermanentFileList, TargetDirectory, CopyParam, Params);
  2387. ClearTransferSourceSelection(Direction);
  2388. }
  2389. throw;
  2390. }
  2391. }
  2392. __finally
  2393. {
  2394. if (!SelectionRestored && (DView != NULL))
  2395. {
  2396. DView->DiscardSavedSelection();
  2397. }
  2398. FTransferResumeList = NULL;
  2399. }
  2400. }
  2401. }
  2402. else if (Operation == foRename)
  2403. {
  2404. DebugAssert(DirView(Side)->ItemFocused);
  2405. DirView(Side)->ItemFocused->EditCaption();
  2406. Result = true;
  2407. }
  2408. else if (Operation == foDelete)
  2409. {
  2410. DebugAssert(FileList->Count);
  2411. // We deliberately do not toggle alternative flag (Param), but use OR,
  2412. // because the Param is set only when command is invoked using Shift-Del/F8 keyboard
  2413. // shortcut of CurrentDeleteAlternativeAction
  2414. bool Alternative =
  2415. bool(Param) || UseAlternativeFunction();
  2416. bool Recycle;
  2417. if (Side == osLocal)
  2418. {
  2419. Recycle = (WinConfiguration->DeleteToRecycleBin != Alternative);
  2420. }
  2421. else
  2422. {
  2423. Recycle = (Terminal->SessionData->DeleteToRecycleBin != Alternative) &&
  2424. !Terminal->SessionData->RecycleBinPath.IsEmpty() &&
  2425. !Terminal->IsRecycledFile(FileList->Strings[0]);
  2426. }
  2427. Result =
  2428. !(Recycle ? WinConfiguration->ConfirmRecycling : WinConfiguration->ConfirmDeleting);
  2429. if (!Result)
  2430. {
  2431. UnicodeString Query;
  2432. if (FileList->Count == 1)
  2433. {
  2434. if (Side == osLocal)
  2435. {
  2436. Query = ExtractFileName(FileList->Strings[0]);
  2437. }
  2438. else
  2439. {
  2440. Query = UnixExtractFileName(FileList->Strings[0]);
  2441. }
  2442. Query = FMTLOAD(
  2443. (Recycle ? CONFIRM_RECYCLE_FILE : CONFIRM_DELETE_FILE), (Query));
  2444. }
  2445. else
  2446. {
  2447. Query = FMTLOAD(
  2448. (Recycle ? CONFIRM_RECYCLE_FILES : CONFIRM_DELETE_FILES), (FileList->Count));
  2449. }
  2450. TMessageParams Params(mpNeverAskAgainCheck);
  2451. Params.ImageName = L"Delete file";
  2452. unsigned int Answer = MessageDialog(MainInstructions(Query), qtConfirmation,
  2453. qaOK | qaCancel, HELP_DELETE_FILE, &Params);
  2454. if (Answer == qaNeverAskAgain)
  2455. {
  2456. Result = true;
  2457. if (Recycle)
  2458. {
  2459. WinConfiguration->ConfirmRecycling = false;
  2460. }
  2461. else
  2462. {
  2463. WinConfiguration->ConfirmDeleting = false;
  2464. }
  2465. }
  2466. else
  2467. {
  2468. Result = (Answer == qaOK);
  2469. }
  2470. }
  2471. if (Result)
  2472. {
  2473. DeleteFiles(Side, FileList, FLAGMASK(Alternative, dfAlternative));
  2474. }
  2475. }
  2476. else if (Operation == foSetProperties)
  2477. {
  2478. RemoteDirView->SaveSelectedNames();
  2479. Result = SetProperties(Side, FileList);
  2480. }
  2481. else if (Operation == foCustomCommand)
  2482. {
  2483. DebugAssert(Param);
  2484. DebugAssert(Side == osRemote);
  2485. RemoteDirView->SaveSelectedNames();
  2486. const TCustomCommandType * Command = static_cast<const TCustomCommandType*>(Param);
  2487. CustomCommand(FileList, *Command, NULL);
  2488. Result = true;
  2489. }
  2490. else if ((Operation == foRemoteMove) || (Operation == foRemoteCopy))
  2491. {
  2492. DebugAssert(Side == osRemote);
  2493. Result = RemoteTransferFiles(FileList, NoConfirmation,
  2494. (Operation == foRemoteMove), reinterpret_cast<TTerminal *>(Param));
  2495. }
  2496. else if (Operation == foLock)
  2497. {
  2498. DebugAssert(Side == osRemote);
  2499. LockFiles(FileList, true);
  2500. Result = true;
  2501. }
  2502. else if (Operation == foUnlock)
  2503. {
  2504. DebugAssert(Side == osRemote);
  2505. LockFiles(FileList, false);
  2506. Result = true;
  2507. }
  2508. else
  2509. {
  2510. DebugFail();
  2511. }
  2512. }
  2513. __finally
  2514. {
  2515. BatchEnd(BatchStorage);
  2516. }
  2517. return Result;
  2518. }
  2519. //---------------------------------------------------------------------------
  2520. TOperationSide __fastcall TCustomScpExplorerForm::GetSide(TOperationSide Side)
  2521. {
  2522. if (Side == osCurrent)
  2523. {
  2524. Side = FCurrentSide;
  2525. }
  2526. return Side;
  2527. }
  2528. //---------------------------------------------------------------------------
  2529. bool __fastcall TCustomScpExplorerForm::ExecuteFileOperation(TFileOperation Operation,
  2530. TOperationSide Side, bool OnFocused, bool NoConfirmation, void * Param)
  2531. {
  2532. Side = GetSide(Side);
  2533. bool Result;
  2534. TStrings * FileList = DirView(Side)->CreateFileList(OnFocused, (Side == osLocal), NULL);
  2535. try
  2536. {
  2537. Result = ExecuteFileOperation(Operation, Side, FileList, NoConfirmation, Param);
  2538. }
  2539. __finally
  2540. {
  2541. delete FileList;
  2542. }
  2543. return Result;
  2544. }
  2545. //---------------------------------------------------------------------------
  2546. void __fastcall TCustomScpExplorerForm::ExecuteFileOperationCommand(
  2547. TFileOperation Operation, TOperationSide Side, bool OnFocused,
  2548. bool NoConfirmation, void * Param)
  2549. {
  2550. if (ExecuteFileOperation(Operation, Side, OnFocused, NoConfirmation, Param))
  2551. {
  2552. if ((Operation == foCopy) || (Operation == foMove))
  2553. {
  2554. if (GetSide(Side) == osLocal)
  2555. {
  2556. Configuration->Usage->Inc(L"UploadsCommand");
  2557. }
  2558. else
  2559. {
  2560. Configuration->Usage->Inc(L"DownloadsCommand");
  2561. }
  2562. }
  2563. }
  2564. }
  2565. //---------------------------------------------------------------------------
  2566. void __fastcall TCustomScpExplorerForm::ExecuteCopyOperationCommand(
  2567. TOperationSide Side, bool OnFocused, unsigned int Flags)
  2568. {
  2569. TTransferOperationParam Param;
  2570. if ((WinConfiguration->Interface != ifCommander) ||
  2571. WinConfiguration->ScpCommander.ExplorerKeyboardShortcuts)
  2572. {
  2573. Flags &= ~cocShortCutHint;
  2574. }
  2575. TCustomDirView * DView = DirView(Side);
  2576. Param.Options =
  2577. FLAGMASK(FLAGSET(Flags, cocShortCutHint), coShortCutHint) |
  2578. FLAGMASK(SelectedAllFilesInDirView(DView), coAllFiles);
  2579. if (FLAGSET(Flags, cocQueue))
  2580. {
  2581. Param.Queue = asOn;
  2582. }
  2583. else if (FLAGSET(Flags, cocNonQueue))
  2584. {
  2585. Param.Queue = asOff;
  2586. }
  2587. ExecuteFileOperationCommand(foCopy, Side, OnFocused, false, &Param);
  2588. }
  2589. //---------------------------------------------------------------------------
  2590. void __fastcall TCustomScpExplorerForm::HandleErrorList(TStringList *& ErrorList)
  2591. {
  2592. try
  2593. {
  2594. if (ErrorList->Count)
  2595. {
  2596. UnicodeString Message = MainInstructions(FMTLOAD(ERROR_LIST_COUNT, (ErrorList->Count)));
  2597. if (MessageDialog(Message, qtError,
  2598. qaOK | qaCancel, HELP_NONE) == qaOK)
  2599. {
  2600. unsigned int Answer;
  2601. int Index = 0;
  2602. do
  2603. {
  2604. DebugAssert(Index >= 0 && Index < ErrorList->Count);
  2605. TQueryButtonAlias Aliases[2];
  2606. Aliases[0].Button = qaYes;
  2607. Aliases[0].Alias = LoadStr(PREV_BUTTON);
  2608. Aliases[1].Button = qaNo;
  2609. Aliases[1].Alias = LoadStr(NEXT_BUTTON);
  2610. TMessageParams Params;
  2611. Params.Aliases = Aliases;
  2612. Params.AliasesCount = LENOF(Aliases);
  2613. Answer = MoreMessageDialog(
  2614. FMTLOAD(ERROR_LIST_NUMBER, (Index+1, ErrorList->Count, ErrorList->Strings[Index])),
  2615. dynamic_cast<TStrings *>(ErrorList->Objects[Index]), qtError,
  2616. (Index ? qaYes : 0) | (Index < ErrorList->Count - 1 ? qaNo : 0) |
  2617. qaOK, HELP_NONE, &Params);
  2618. if (Answer == qaNo)
  2619. {
  2620. Index++;
  2621. }
  2622. if (Answer == qaYes)
  2623. {
  2624. Index--;
  2625. }
  2626. }
  2627. while (Answer != qaOK);
  2628. }
  2629. }
  2630. }
  2631. __finally
  2632. {
  2633. TStrings * List = ErrorList;
  2634. ErrorList = NULL;
  2635. for (int i = 0; i < List->Count; i++)
  2636. {
  2637. delete List->Objects[i];
  2638. }
  2639. delete List;
  2640. }
  2641. }
  2642. //---------------------------------------------------------------------------
  2643. void __fastcall TCustomScpExplorerForm::ExecuteRemoteFile(
  2644. const UnicodeString & FullFileName, TRemoteFile * File, TExecuteFileBy ExecuteFileBy)
  2645. {
  2646. // needed for checking filemasks, as there's no directory object
  2647. // associated with the file object
  2648. File->FullFileName = FullFileName;
  2649. TFileMasks::TParams MaskParams;
  2650. MaskParams.Size = File->Size;
  2651. MaskParams.Modification = File->Modification;
  2652. ExecuteFile(osRemote, ExecuteFileBy, NULL, FullFileName, File, MaskParams);
  2653. }
  2654. //---------------------------------------------------------------------------
  2655. void __fastcall TCustomScpExplorerForm::EditNew(TOperationSide Side)
  2656. {
  2657. DebugAssert(!WinConfiguration->DisableOpenEdit);
  2658. Side = GetSide(Side);
  2659. TCustomDirView * DView = DirView(Side);
  2660. TListItem * FocusedItem = DView->ItemFocused;
  2661. UnicodeString Name;
  2662. if ((FocusedItem != NULL) && !DView->ItemIsParentDirectory(FocusedItem))
  2663. {
  2664. Name = DView->ItemFileName(FocusedItem);
  2665. }
  2666. else
  2667. {
  2668. Name = LoadStr(NEW_FILE);
  2669. }
  2670. UnicodeString Names = Name;
  2671. std::unique_ptr<TStrings> History(CloneStrings(CustomWinConfiguration->History[L"EditFile"]));
  2672. if (InputDialog(LoadStr(EDIT_FILE_CAPTION), LoadStr(EDIT_FILE_PROMPT), Names,
  2673. HELP_EDIT_NEW, History.get(), true))
  2674. {
  2675. while (!Names.IsEmpty())
  2676. {
  2677. Name = CutToChar(Names, FileMasksDelimiters[1], false);
  2678. CustomWinConfiguration->History[L"EditFile"] = History.get();
  2679. UnicodeString TargetFileName;
  2680. UnicodeString LocalFileName;
  2681. UnicodeString RootTempDir;
  2682. UnicodeString TempDir;
  2683. UnicodeString RemoteDirectory;
  2684. bool ExistingFile = false;
  2685. if (Side == osRemote)
  2686. {
  2687. Name = AbsolutePath(FTerminal->CurrentDirectory, Name);
  2688. TRemoteFile * File = NULL;
  2689. if (FTerminal->FileExists(Name, &File))
  2690. {
  2691. try
  2692. {
  2693. ExecuteRemoteFile(Name, File, efDefaultEditor);
  2694. ExistingFile = true;
  2695. }
  2696. __finally
  2697. {
  2698. delete File;
  2699. }
  2700. }
  2701. if (!ExistingFile)
  2702. {
  2703. RemoteDirectory = UnixExtractFilePath(Name);
  2704. TemporaryDirectoryForRemoteFiles(
  2705. RemoteDirectory, GUIConfiguration->CurrentCopyParam, TempDir, RootTempDir);
  2706. TargetFileName = UnixExtractFileName(Name);
  2707. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  2708. LocalFileName = TempDir +
  2709. // We probably do not want to trim the VMS version here
  2710. FTerminal->ChangeFileName(&CopyParam, TargetFileName, osRemote, false);
  2711. }
  2712. }
  2713. else
  2714. {
  2715. if (ExtractFilePath(Name).IsEmpty())
  2716. {
  2717. LocalFileName = IncludeTrailingBackslash(DView->PathName) + Name;
  2718. }
  2719. else
  2720. {
  2721. LocalFileName = ExpandFileName(Name);
  2722. }
  2723. TargetFileName = ExtractFileName(Name);
  2724. }
  2725. if (!ExistingFile)
  2726. {
  2727. if (!FileExists(ApiPath(LocalFileName)))
  2728. {
  2729. int File = FileCreate(ApiPath(LocalFileName));
  2730. if (File < 0)
  2731. {
  2732. if (!RootTempDir.IsEmpty())
  2733. {
  2734. RecursiveDeleteFile(ExcludeTrailingBackslash(RootTempDir), false);
  2735. }
  2736. throw Exception(FMTLOAD(CREATE_FILE_ERROR, (LocalFileName)));
  2737. }
  2738. else
  2739. {
  2740. FileClose(File);
  2741. }
  2742. }
  2743. TExecuteFileBy ExecuteFileBy = efDefaultEditor;
  2744. const TEditorData * ExternalEditor = NULL;
  2745. TFileMasks::TParams MaskParams; // size not known
  2746. ExecuteFileNormalize(ExecuteFileBy, ExternalEditor, TargetFileName,
  2747. false, MaskParams);
  2748. CustomExecuteFile(Side, ExecuteFileBy, LocalFileName, TargetFileName,
  2749. ExternalEditor, RootTempDir, RemoteDirectory);
  2750. }
  2751. }
  2752. }
  2753. }
  2754. //---------------------------------------------------------------------------
  2755. bool __fastcall TCustomScpExplorerForm::RemoteExecuteForceText(
  2756. TExecuteFileBy ExecuteFileBy, const TEditorData * ExternalEditor)
  2757. {
  2758. DebugAssert((ExecuteFileBy == efExternalEditor) ==
  2759. ((ExternalEditor != NULL) && (ExternalEditor->Editor == edExternal)));
  2760. DebugAssert(ExecuteFileBy != efDefaultEditor);
  2761. return
  2762. ((ExecuteFileBy == efInternalEditor)) ||
  2763. ((ExecuteFileBy == efExternalEditor) && ExternalEditor->ExternalEditorText);
  2764. }
  2765. //---------------------------------------------------------------------------
  2766. void __fastcall TCustomScpExplorerForm::CustomExecuteFile(TOperationSide Side,
  2767. TExecuteFileBy ExecuteFileBy, UnicodeString FileName, UnicodeString OriginalFileName,
  2768. const TEditorData * ExternalEditor, UnicodeString LocalRootDirectory,
  2769. UnicodeString RemoteDirectory)
  2770. {
  2771. DebugAssert(!WinConfiguration->DisableOpenEdit);
  2772. DebugAssert((ExecuteFileBy == efExternalEditor) ==
  2773. ((ExternalEditor != NULL) && (ExternalEditor->Editor == edExternal)));
  2774. DebugAssert(ExecuteFileBy != efDefaultEditor);
  2775. Side = GetSide(Side);
  2776. std::unique_ptr<TEditedFileData> Data(new TEditedFileData);
  2777. if (Side == osRemote)
  2778. {
  2779. Data->Terminal = Terminal;
  2780. Data->Queue = Queue;
  2781. Data->SessionData = CloneCurrentSessionData();
  2782. Data->ForceText = RemoteExecuteForceText(ExecuteFileBy, ExternalEditor);
  2783. Data->RemoteDirectory = RemoteDirectory;
  2784. Data->SessionName = Terminal->SessionData->SessionName;
  2785. Data->LocalRootDirectory = LocalRootDirectory;
  2786. Data->OriginalFileName = OriginalFileName;
  2787. Data->Command = L""; // will be changed later for external editor
  2788. }
  2789. if (ExecuteFileBy == efInternalEditor)
  2790. {
  2791. if (Side == osRemote)
  2792. {
  2793. UnicodeString Caption = UnixIncludeTrailingBackslash(RemoteDirectory) + OriginalFileName +
  2794. L" - " + Terminal->SessionData->SessionName;
  2795. TForm * Editor;
  2796. try
  2797. {
  2798. TNotifyEvent OnFileChanged = NULL;
  2799. TNotifyEvent OnSaveAll = NULL;
  2800. TAnyModifiedEvent OnAnyModified = NULL;
  2801. // Edited files are uploaded in background queue, what we do not support with encrypted files
  2802. if (Terminal->IsCapable[fsBackgroundTransfers])
  2803. {
  2804. OnFileChanged = FEditorManager->FileChanged;
  2805. OnSaveAll = SaveAllInternalEditors;
  2806. OnAnyModified = AnyInternalEditorModified;
  2807. }
  2808. Editor = ShowEditorForm(FileName, this, OnFileChanged,
  2809. FEditorManager->FileReload, FEditorManager->FileClosed,
  2810. OnSaveAll, OnAnyModified,
  2811. Caption, FStandaloneEditing, SessionColor, Terminal->SessionData->InternalEditorEncoding);
  2812. }
  2813. catch(...)
  2814. {
  2815. if (!LocalRootDirectory.IsEmpty())
  2816. {
  2817. RecursiveDeleteFile(ExcludeTrailingBackslash(LocalRootDirectory), false);
  2818. }
  2819. throw;
  2820. }
  2821. FEditorManager->AddFileInternal(FileName, Data.release(), Editor);
  2822. }
  2823. else
  2824. {
  2825. DebugAssert(!FStandaloneEditing);
  2826. TForm * Editor =
  2827. ShowEditorForm(FileName, this, NULL, NULL, LocalEditorClosed,
  2828. SaveAllInternalEditors, AnyInternalEditorModified,
  2829. L"", false, SessionColor, -1);
  2830. FLocalEditors->Add(Editor);
  2831. }
  2832. }
  2833. else
  2834. {
  2835. HANDLE Process;
  2836. if (ExecuteFileBy == efExternalEditor)
  2837. {
  2838. UnicodeString Program, Params, Dir;
  2839. Data->Command = ExternalEditor->ExternalEditor;
  2840. ReformatFileNameCommand(Data->Command);
  2841. SplitCommand(Data->Command, Program, Params, Dir);
  2842. Params = ExpandFileNameCommand(Params, FileName);
  2843. Program = ExpandEnvironmentVariables(Program);
  2844. if (!ExecuteShell(Program, Params, Process))
  2845. {
  2846. throw EOSExtException(FMTLOAD(EDITOR_ERROR, (Program)));
  2847. }
  2848. }
  2849. else
  2850. {
  2851. DebugAssert(Side == osRemote);
  2852. if (!ExecuteShell(FileName, L"", Process))
  2853. {
  2854. throw EOSExtException(FMTLOAD(EXECUTE_FILE_ERROR, (FileName)));
  2855. }
  2856. }
  2857. if ((Side == osLocal) ||
  2858. ((ExecuteFileBy == efShell) &&
  2859. !WinConfiguration->Editor.SDIShellEditor) ||
  2860. ((ExecuteFileBy == efExternalEditor) &&
  2861. !ExternalEditor->SDIExternalEditor))
  2862. {
  2863. // no need for handle
  2864. if (Process != NULL)
  2865. {
  2866. DebugCheck(CloseHandle(Process));
  2867. }
  2868. Process = INVALID_HANDLE_VALUE;
  2869. }
  2870. else
  2871. {
  2872. if (Process == NULL)
  2873. {
  2874. throw ExtException(LoadStr(OPEN_FILE_NO_PROCESS2), L"", HELP_OPEN_FILE_NO_PROCESS);
  2875. }
  2876. }
  2877. if (Side == osRemote)
  2878. {
  2879. FEditorManager->AddFileExternal(FileName, Data.release(), Process);
  2880. }
  2881. }
  2882. }
  2883. //---------------------------------------------------------------------------
  2884. void __fastcall TCustomScpExplorerForm::SaveInternalEditor(
  2885. const UnicodeString /*FileName*/, TEditedFileData * /*Data*/, TObject * Token,
  2886. void * /*Arg*/)
  2887. {
  2888. if (Token != NULL)
  2889. {
  2890. EditorFormFileSave(static_cast<TForm *>(Token));
  2891. }
  2892. }
  2893. //---------------------------------------------------------------------------
  2894. void __fastcall TCustomScpExplorerForm::SaveAllInternalEditors(TObject * /*Sender*/)
  2895. {
  2896. for (int Index = 0; Index < FLocalEditors->Count; Index++)
  2897. {
  2898. EditorFormFileSave(static_cast<TForm *>(FLocalEditors->Items[Index]));
  2899. }
  2900. FEditorManager->ProcessFiles(SaveInternalEditor, NULL);
  2901. }
  2902. //---------------------------------------------------------------------------
  2903. void __fastcall TCustomScpExplorerForm::InternalEditorModified(
  2904. const UnicodeString /*FileName*/, TEditedFileData * /*Data*/, TObject * Token,
  2905. void * Arg)
  2906. {
  2907. if ((Token != NULL) &&
  2908. IsEditorFormModified(static_cast<TForm *>(Token)))
  2909. {
  2910. *static_cast<bool *>(Arg) = true;
  2911. }
  2912. }
  2913. //---------------------------------------------------------------------------
  2914. void __fastcall TCustomScpExplorerForm::AnyInternalEditorModified(
  2915. TObject * /*Sender*/, bool & Modified)
  2916. {
  2917. for (int Index = 0; !Modified && (Index < FLocalEditors->Count); Index++)
  2918. {
  2919. if (IsEditorFormModified(static_cast<TForm *>(FLocalEditors->Items[Index])))
  2920. {
  2921. Modified = true;
  2922. }
  2923. }
  2924. if (!Modified)
  2925. {
  2926. FEditorManager->ProcessFiles(InternalEditorModified, &Modified);
  2927. }
  2928. }
  2929. //---------------------------------------------------------------------------
  2930. void __fastcall TCustomScpExplorerForm::LocalEditorClosed(TObject * Sender, bool /*Forced*/)
  2931. {
  2932. DebugCheck(FLocalEditors->Extract(Sender) >= 0);
  2933. }
  2934. //---------------------------------------------------------------------------
  2935. void __fastcall TCustomScpExplorerForm::TemporaryDirectoryForRemoteFiles(
  2936. UnicodeString RemoteDirectory, TCopyParamType CopyParam,
  2937. UnicodeString & Result, UnicodeString & RootDirectory)
  2938. {
  2939. if (!WinConfiguration->TemporaryDirectoryDeterministic)
  2940. {
  2941. RootDirectory = IncludeTrailingBackslash(WinConfiguration->TemporaryDir());
  2942. Result = RootDirectory;
  2943. }
  2944. else
  2945. {
  2946. RootDirectory = L"";
  2947. Result = WinConfiguration->ExpandedTemporaryDirectory();
  2948. Result = IncludeTrailingBackslash(Result);
  2949. }
  2950. if (WinConfiguration->TemporaryDirectoryAppendSession)
  2951. {
  2952. Result = IncludeTrailingBackslash(Result + MakeValidFileName(Terminal->SessionData->SessionName));
  2953. }
  2954. if (WinConfiguration->TemporaryDirectoryAppendPath)
  2955. {
  2956. if (!RemoteDirectory.IsEmpty() && (RemoteDirectory[1] == L'/'))
  2957. {
  2958. RemoteDirectory.Delete(1, 1);
  2959. }
  2960. Result = IncludeTrailingBackslash(Result + CopyParam.ValidLocalPath(FromUnixPath(RemoteDirectory)));
  2961. }
  2962. if (!ForceDirectories(ApiPath(Result)))
  2963. {
  2964. throw EOSExtException(FMTLOAD(CREATE_TEMP_DIR_ERROR, (Result)));
  2965. }
  2966. }
  2967. //---------------------------------------------------------------------------
  2968. void __fastcall TCustomScpExplorerForm::TemporarilyDownloadFiles(
  2969. TStrings * FileList, bool ForceText, UnicodeString & RootTempDir, UnicodeString & TempDir,
  2970. bool AllFiles, bool GetTargetNames, bool AutoOperation)
  2971. {
  2972. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  2973. if (ForceText)
  2974. {
  2975. CopyParam.TransferMode = tmAscii;
  2976. }
  2977. TemporaryFileCopyParam(CopyParam);
  2978. if (AllFiles)
  2979. {
  2980. CopyParam.IncludeFileMask = TFileMasks();
  2981. }
  2982. if (TempDir.IsEmpty())
  2983. {
  2984. TemporaryDirectoryForRemoteFiles(FTerminal->CurrentDirectory, CopyParam, TempDir, RootTempDir);
  2985. }
  2986. DebugAssert(!FAutoOperation);
  2987. FAutoOperation = AutoOperation;
  2988. Terminal->ExceptionOnFail = true;
  2989. try
  2990. {
  2991. try
  2992. {
  2993. // turn off confirmations, as for MDI editors we may possibly download
  2994. // the same file over
  2995. Terminal->CopyToLocal(
  2996. FileList, TempDir, &CopyParam, cpNoConfirmation | cpTemporary, NULL);
  2997. if (GetTargetNames)
  2998. {
  2999. for (int i = 0; i < FileList->Count; i++)
  3000. {
  3001. FileList->Strings[i] =
  3002. Terminal->ChangeFileName(&CopyParam, UnixExtractFileName(FileList->Strings[i]), osRemote, false);
  3003. }
  3004. }
  3005. }
  3006. catch(...)
  3007. {
  3008. if (!RootTempDir.IsEmpty())
  3009. {
  3010. RecursiveDeleteFile(ExcludeTrailingBackslash(RootTempDir), false);
  3011. }
  3012. throw;
  3013. }
  3014. }
  3015. __finally
  3016. {
  3017. FAutoOperation = false;
  3018. Terminal->ExceptionOnFail = false;
  3019. }
  3020. }
  3021. //---------------------------------------------------------------------------
  3022. void __fastcall TCustomScpExplorerForm::EditorAutoConfig()
  3023. {
  3024. // Do not waste time checking for default editor list the next time,
  3025. // or testing if default editor is not notepad.
  3026. bool TryNextTime = false;
  3027. UnicodeString UsageState;
  3028. if (!WinConfiguration->EditorList->IsDefaultList())
  3029. {
  3030. UsageState = "H";
  3031. }
  3032. else
  3033. {
  3034. UnicodeString Executable;
  3035. UnicodeString ExecutableDescription;
  3036. if (DetectSystemExternalEditor(false, Executable, ExecutableDescription, UsageState, TryNextTime))
  3037. {
  3038. UnicodeString Message =
  3039. FMTLOAD(EDITOR_AUTO_CONFIG2, (ExecutableDescription, ExecutableDescription));
  3040. unsigned int Answer =
  3041. MessageDialog(Message, qtConfirmation, qaOK | qaCancel, HELP_EDITOR_AUTO_CONFIG);
  3042. if (Answer != qaOK)
  3043. {
  3044. UsageState = "R";
  3045. }
  3046. else
  3047. {
  3048. UsageState = "A";
  3049. TEditorData EditorData;
  3050. EditorData.Editor = edExternal;
  3051. EditorData.ExternalEditor = FormatCommand(Executable, L"");
  3052. EditorData.DecideExternalEditorText();
  3053. TEditorList EditorList;
  3054. EditorList = *WinConfiguration->EditorList;
  3055. EditorList.Insert(0, new TEditorPreferences(EditorData));
  3056. WinConfiguration->EditorList = &EditorList;
  3057. }
  3058. }
  3059. }
  3060. WinConfiguration->OfferedEditorAutoConfig = !TryNextTime;
  3061. WinConfiguration->Usage->Set(L"EditorAutoConfig", UsageState);
  3062. }
  3063. //---------------------------------------------------------------------------
  3064. void __fastcall TCustomScpExplorerForm::ExecuteFileNormalize(
  3065. TExecuteFileBy & ExecuteFileBy, const TEditorData *& ExternalEditor,
  3066. const UnicodeString & FileName, bool Local, const TFileMasks::TParams & MaskParams)
  3067. {
  3068. if (ExecuteFileBy == efDefaultEditor)
  3069. {
  3070. if (!WinConfiguration->OfferedEditorAutoConfig)
  3071. {
  3072. EditorAutoConfig();
  3073. }
  3074. const TEditorPreferences * Editor =
  3075. WinConfiguration->DefaultEditorForFile(FileName, Local, MaskParams);
  3076. if ((Editor == NULL) || (Editor->Data->Editor == edInternal))
  3077. {
  3078. ExecuteFileBy = efInternalEditor;
  3079. ExternalEditor = NULL;
  3080. }
  3081. else if (Editor->Data->Editor == edOpen)
  3082. {
  3083. ExecuteFileBy = efShell;
  3084. ExternalEditor = NULL;
  3085. }
  3086. else
  3087. {
  3088. ExecuteFileBy = efExternalEditor;
  3089. ExternalEditor = Editor->Data;
  3090. }
  3091. }
  3092. }
  3093. //---------------------------------------------------------------------------
  3094. void __fastcall TCustomScpExplorerForm::ExecuteFile(TOperationSide Side,
  3095. TExecuteFileBy ExecuteFileBy, const TEditorData * ExternalEditor,
  3096. UnicodeString FullFileName, TObject * Object, const TFileMasks::TParams & MaskParams)
  3097. {
  3098. UnicodeString OriginalFileName;
  3099. UnicodeString LocalRootDirectory;
  3100. UnicodeString RemoteDirectory;
  3101. ExecuteFileNormalize(ExecuteFileBy, ExternalEditor, FullFileName,
  3102. (Side == osLocal), MaskParams);
  3103. UnicodeString Counter;
  3104. UnicodeString LocalFileName;
  3105. bool Handled = false;
  3106. if (Side == osRemote)
  3107. {
  3108. // We need to trim VMS version here, so that we use name without version
  3109. // when uploading back to create a new version of the file
  3110. OriginalFileName = FTerminal->GetBaseFileName(UnixExtractFileName(FullFileName));
  3111. RemoteDirectory = UnixExtractFilePath(FullFileName);
  3112. TObject * Token = NULL;
  3113. UnicodeString LocalDirectory;
  3114. if (!FEditorManager->CanAddFile(RemoteDirectory, OriginalFileName,
  3115. Terminal->SessionData->SessionName, Token, LocalRootDirectory,
  3116. LocalDirectory))
  3117. {
  3118. if (Token != NULL)
  3119. {
  3120. TForm * Form = dynamic_cast<TForm *>(Token);
  3121. if (Form->WindowState == wsMinimized)
  3122. {
  3123. ShowWindow(Form->Handle, SW_RESTORE);
  3124. }
  3125. else
  3126. {
  3127. Form->SetFocus();
  3128. }
  3129. Handled = true;
  3130. }
  3131. else
  3132. {
  3133. throw Exception(FMTLOAD(ALREADY_EDITED_EXTERNALLY_OR_UPLOADED, (OriginalFileName)));
  3134. }
  3135. }
  3136. if (!Handled)
  3137. {
  3138. TStringList * FileList1 = new TStringList();
  3139. try
  3140. {
  3141. FileList1->AddObject(FullFileName, Object);
  3142. TemporarilyDownloadFiles(FileList1,
  3143. RemoteExecuteForceText(ExecuteFileBy, ExternalEditor),
  3144. LocalRootDirectory, LocalDirectory, true, true, true);
  3145. LocalFileName = LocalDirectory + FileList1->Strings[0];
  3146. }
  3147. __finally
  3148. {
  3149. delete FileList1;
  3150. }
  3151. switch (ExecuteFileBy)
  3152. {
  3153. case efShell:
  3154. Counter = "RemoteFilesExecuted";
  3155. break;
  3156. case efInternalEditor:
  3157. Counter = "RemoteFilesOpenedInInternalEditor";
  3158. break;
  3159. case efExternalEditor:
  3160. Counter = "RemoteFilesOpenedInExternalEditor";
  3161. break;
  3162. default:
  3163. DebugFail();
  3164. }
  3165. }
  3166. }
  3167. else
  3168. {
  3169. LocalFileName = FullFileName;
  3170. OriginalFileName = ExtractFileName(FullFileName);
  3171. switch (ExecuteFileBy)
  3172. {
  3173. case efShell:
  3174. Counter = "LocalFilesExecuted";
  3175. break;
  3176. case efInternalEditor:
  3177. Counter = "LocalFilesOpenedInInternalEditor";
  3178. break;
  3179. case efExternalEditor:
  3180. Counter = "LocalFilesOpenedInExternalEditor";
  3181. break;
  3182. default:
  3183. DebugFail();
  3184. }
  3185. }
  3186. if (!Handled)
  3187. {
  3188. Configuration->Usage->Inc(Counter);
  3189. CustomExecuteFile(Side, ExecuteFileBy, LocalFileName, OriginalFileName,
  3190. ExternalEditor, LocalRootDirectory, RemoteDirectory);
  3191. }
  3192. }
  3193. //---------------------------------------------------------------------------
  3194. void __fastcall TCustomScpExplorerForm::ExecuteFile(TOperationSide Side,
  3195. TExecuteFileBy ExecuteFileBy, const TEditorData * ExternalEditor,
  3196. bool AllSelected, bool OnFocused)
  3197. {
  3198. DebugAssert(!WinConfiguration->DisableOpenEdit);
  3199. DebugAssert((ExecuteFileBy == efExternalEditor) ==
  3200. ((ExternalEditor != NULL) && (ExternalEditor->Editor == edExternal)));
  3201. Side = GetSide(Side);
  3202. TCustomDirView * DView = DirView(Side);
  3203. TStrings * FileList = AllSelected ?
  3204. DView->CreateFileList(OnFocused, Side == osLocal) :
  3205. DView->CreateFocusedFileList(Side == osLocal);
  3206. try
  3207. {
  3208. DebugAssert(AllSelected || (FileList->Count == 1));
  3209. for (int i = 0; i < FileList->Count; i++)
  3210. {
  3211. UnicodeString ListFileName = FileList->Strings[i];
  3212. UnicodeString FileNameOnly = (Side == osRemote) ?
  3213. UnixExtractFileName(ListFileName) : ExtractFileName(ListFileName);
  3214. TListItem * Item = DView->FindFileItem(FileNameOnly);
  3215. if (!DView->ItemIsDirectory(Item))
  3216. {
  3217. UnicodeString FullFileName;
  3218. if (Side == osRemote)
  3219. {
  3220. FullFileName = RemoteDirView->Path + ListFileName;
  3221. }
  3222. else
  3223. {
  3224. FullFileName = ListFileName;
  3225. }
  3226. TFileMasks::TParams MaskParams;
  3227. MaskParams.Size = DView->ItemFileSize(Item);
  3228. TDateTimePrecision Precision;
  3229. MaskParams.Modification = DView->ItemFileTime(Item, Precision);
  3230. ExecuteFile(Side, ExecuteFileBy, ExternalEditor, FullFileName,
  3231. FileList->Objects[i], MaskParams);
  3232. }
  3233. }
  3234. }
  3235. __finally
  3236. {
  3237. delete FileList;
  3238. }
  3239. }
  3240. //---------------------------------------------------------------------------
  3241. void __fastcall TCustomScpExplorerForm::TemporaryFileCopyParam(TCopyParamType & CopyParam)
  3242. {
  3243. // do not forget to add additional options to TemporarilyDownloadFiles, and AS
  3244. CopyParam.FileNameCase = ncNoChange;
  3245. CopyParam.PreserveRights = false;
  3246. CopyParam.PreserveReadOnly = false;
  3247. CopyParam.ReplaceInvalidChars = true;
  3248. CopyParam.IncludeFileMask = TFileMasks();
  3249. CopyParam.NewerOnly = false;
  3250. CopyParam.FileMask = L"";
  3251. }
  3252. //---------------------------------------------------------------------------
  3253. void __fastcall TCustomScpExplorerForm::ExecutedFileChanged(const UnicodeString FileName,
  3254. TEditedFileData * Data, HANDLE UploadCompleteEvent)
  3255. {
  3256. TTerminalManager * Manager = TTerminalManager::Instance();
  3257. if ((Data->Terminal == NULL) || !Data->Terminal->Active)
  3258. {
  3259. if (!NonVisualDataModule->Busy)
  3260. {
  3261. UnicodeString FileNameOnly = ExtractFileName(FileName);
  3262. UnicodeString EditInactiveSessionReopenAcceptedCounter = L"EditInactiveSessionReopenAccepted";
  3263. UnicodeString EditInactiveSessionReopenRejectedCounter = L"EditInactiveSessionReopenRejected";
  3264. if (Data->Terminal == NULL)
  3265. {
  3266. TTerminal * SameSiteTerminal = Manager->FindActiveTerminalForSite(Data->SessionData);
  3267. if (SameSiteTerminal != NULL)
  3268. {
  3269. UnicodeString Message =
  3270. FMTLOAD(EDIT_SESSION_REATTACH,
  3271. (FileNameOnly, Data->SessionName, FileNameOnly));
  3272. if (MessageDialog(Message, qtConfirmation, qaOK | qaCancel) == qaOK)
  3273. {
  3274. Data->Terminal = Terminal;
  3275. Data->Queue = Manager->FindQueueForTerminal(Terminal);
  3276. Data->SessionName = Terminal->SessionData->SessionName;
  3277. // We might also overwrite session data
  3278. Configuration->Usage->Inc(EditInactiveSessionReopenAcceptedCounter);
  3279. }
  3280. else
  3281. {
  3282. Configuration->Usage->Inc(EditInactiveSessionReopenRejectedCounter);
  3283. Abort();
  3284. }
  3285. }
  3286. }
  3287. // foreground session should reconnect itself
  3288. else if (Terminal != Data->Terminal)
  3289. {
  3290. UnicodeString Message =
  3291. MainInstructions(
  3292. FMTLOAD(EDIT_SESSION_RECONNECT, (Data->SessionName, FileNameOnly)));
  3293. if (MessageDialog(Message, qtConfirmation, qaOK | qaCancel) == qaOK)
  3294. {
  3295. Manager->SetActiveTerminalWithAutoReconnect(Data->Terminal);
  3296. Configuration->Usage->Inc(EditInactiveSessionReopenAcceptedCounter);
  3297. }
  3298. else
  3299. {
  3300. Configuration->Usage->Inc(EditInactiveSessionReopenRejectedCounter);
  3301. Abort();
  3302. }
  3303. }
  3304. }
  3305. if ((Data->Terminal == NULL) || !Data->Terminal->Active)
  3306. {
  3307. Configuration->Usage->Inc(L"EditInactiveSession");
  3308. // Prevent this when not idle (!NonVisualDataModule->Busy)?
  3309. throw Exception(FMTLOAD(EDIT_SESSION_CLOSED2,
  3310. (ExtractFileName(FileName), Data->SessionName)));
  3311. }
  3312. }
  3313. TStrings * FileList = new TStringList();
  3314. try
  3315. {
  3316. FileList->Add(FileName);
  3317. // consider using the same settings (preset) as when the file was downloaded
  3318. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  3319. TemporaryFileCopyParam(CopyParam);
  3320. if (Data->ForceText)
  3321. {
  3322. CopyParam.TransferMode = tmAscii;
  3323. }
  3324. // so i do not need to worry if masking algorithm works in all cases
  3325. // ("" means "copy file name", no masking is actually done)
  3326. if (ExtractFileName(FileName) == Data->OriginalFileName)
  3327. {
  3328. CopyParam.FileMask = L"";
  3329. }
  3330. else
  3331. {
  3332. CopyParam.FileMask = DelimitFileNameMask(Data->OriginalFileName);
  3333. }
  3334. DebugAssert(Data->Queue != NULL);
  3335. int Params = cpNoConfirmation | cpTemporary;
  3336. TQueueItem * QueueItem = new TUploadQueueItem(Data->Terminal, FileList,
  3337. Data->RemoteDirectory, &CopyParam, Params, true, false);
  3338. QueueItem->CompleteEvent = UploadCompleteEvent;
  3339. AddQueueItem(Data->Queue, QueueItem, Data->Terminal);
  3340. }
  3341. __finally
  3342. {
  3343. delete FileList;
  3344. }
  3345. }
  3346. //---------------------------------------------------------------------------
  3347. void __fastcall TCustomScpExplorerForm::ExecutedFileReload(
  3348. const UnicodeString FileName, const TEditedFileData * Data)
  3349. {
  3350. // Sanity check, we should not be busy otherwise user would not be able to click Reload button.
  3351. DebugAssert(!NonVisualDataModule->Busy);
  3352. if ((Data->Terminal == NULL) || !Data->Terminal->Active)
  3353. {
  3354. throw Exception(FMTLOAD(EDIT_SESSION_CLOSED_RELOAD,
  3355. (ExtractFileName(FileName), Data->SessionName)));
  3356. }
  3357. TTerminal * PrevTerminal = TTerminalManager::Instance()->ActiveTerminal;
  3358. TTerminalManager::Instance()->ActiveTerminal = Data->Terminal;
  3359. NonVisualDataModule->StartBusy();
  3360. try
  3361. {
  3362. std::unique_ptr<TRemoteFile> File;
  3363. UnicodeString RemoteFileName =
  3364. UnixIncludeTrailingBackslash(Data->RemoteDirectory) + Data->OriginalFileName;
  3365. FTerminal->ExceptionOnFail = true;
  3366. try
  3367. {
  3368. TRemoteFile * AFile = NULL;
  3369. FTerminal->ReadFile(RemoteFileName, AFile);
  3370. File.reset(AFile);
  3371. if (!File->HaveFullFileName)
  3372. {
  3373. File->FullFileName = RemoteFileName;
  3374. }
  3375. }
  3376. __finally
  3377. {
  3378. FTerminal->ExceptionOnFail = false;
  3379. }
  3380. std::unique_ptr<TStrings> FileList(new TStringList());
  3381. FileList->AddObject(RemoteFileName, File.get());
  3382. UnicodeString RootTempDir = Data->LocalRootDirectory;
  3383. UnicodeString TempDir = ExtractFilePath(FileName);
  3384. TemporarilyDownloadFiles(FileList.get(), Data->ForceText, RootTempDir,
  3385. TempDir, true, true, true);
  3386. // sanity check, the target file name should be still the same
  3387. DebugAssert(ExtractFileName(FileName) == FileList->Strings[0]);
  3388. }
  3389. __finally
  3390. {
  3391. NonVisualDataModule->EndBusy();
  3392. // it actually may not exist anymore...
  3393. TTerminalManager::Instance()->ActiveTerminal = PrevTerminal;
  3394. }
  3395. }
  3396. //---------------------------------------------------------------------------
  3397. void __fastcall TCustomScpExplorerForm::ExecutedFileEarlyClosed(
  3398. const TEditedFileData * Data, bool & KeepOpen)
  3399. {
  3400. // Command is set for external editors only (not for "shell" open).
  3401. if (!Data->Command.IsEmpty())
  3402. {
  3403. bool AnyFound = false;
  3404. bool AnyMDI = false;
  3405. bool AnyNonMDI = false;
  3406. bool AnyDetect = false;
  3407. TEditorList * EditorList = new TEditorList();
  3408. try
  3409. {
  3410. *EditorList = *WinConfiguration->EditorList;
  3411. for (int i = 0; i < EditorList->Count; i++)
  3412. {
  3413. const TEditorPreferences * Editor = EditorList->Editors[i];
  3414. if ((Editor->Data->Editor == edExternal) &&
  3415. (Editor->Data->ExternalEditor == Data->Command))
  3416. {
  3417. AnyFound = true;
  3418. if (Editor->Data->SDIExternalEditor)
  3419. {
  3420. AnyNonMDI = true;
  3421. if (Editor->Data->DetectMDIExternalEditor)
  3422. {
  3423. AnyDetect = true;
  3424. }
  3425. }
  3426. else
  3427. {
  3428. AnyMDI = true;
  3429. }
  3430. }
  3431. }
  3432. bool EnableMDI = false;
  3433. bool DisableDetect = false;
  3434. if (AnyMDI)
  3435. {
  3436. KeepOpen = true;
  3437. if (AnyNonMDI)
  3438. {
  3439. // there is at least one instance of the editor with MDI support enabled,
  3440. // and one with disabled, enable it for all instances
  3441. EnableMDI = true;
  3442. }
  3443. }
  3444. else if (AnyFound && !AnyDetect)
  3445. {
  3446. // at least once instance found but all have MDI autodetection disabled
  3447. // => close the file (default action)
  3448. }
  3449. else
  3450. {
  3451. // no instance of the editor has MDI support enabled
  3452. TMessageParams Params;
  3453. if (AnyFound)
  3454. {
  3455. // there is at least one instance of the editor
  3456. Params.Params |= mpNeverAskAgainCheck;
  3457. }
  3458. unsigned int Answer = MessageDialog(FMTLOAD(EDITOR_EARLY_CLOSED2, (Data->OriginalFileName)), qtWarning,
  3459. qaYes | qaNo, HELP_EDITOR_EARLY_CLOSED, &Params);
  3460. switch (Answer)
  3461. {
  3462. case qaNeverAskAgain:
  3463. DisableDetect = true;
  3464. break;
  3465. case qaNo:
  3466. EnableMDI = true;
  3467. KeepOpen = true;
  3468. break;
  3469. }
  3470. }
  3471. if (AnyFound && (EnableMDI || DisableDetect))
  3472. {
  3473. bool Changed = false;
  3474. for (int i = 0; i < EditorList->Count; i++)
  3475. {
  3476. const TEditorPreferences * Editor = EditorList->Editors[i];
  3477. if ((Editor->Data->Editor == edExternal) &&
  3478. (Editor->Data->ExternalEditor == Data->Command) &&
  3479. ((EnableMDI && Editor->Data->SDIExternalEditor) ||
  3480. (DisableDetect && Editor->Data->DetectMDIExternalEditor)))
  3481. {
  3482. Changed = true;
  3483. TEditorPreferences * UpdatedEditor = new TEditorPreferences(*Editor);
  3484. if (EnableMDI)
  3485. {
  3486. UpdatedEditor->GetData()->SDIExternalEditor = false;
  3487. }
  3488. if (DisableDetect)
  3489. {
  3490. UpdatedEditor->GetData()->DetectMDIExternalEditor = false;
  3491. }
  3492. EditorList->Change(i, UpdatedEditor);
  3493. }
  3494. }
  3495. if (Changed)
  3496. {
  3497. WinConfiguration->EditorList = EditorList;
  3498. }
  3499. }
  3500. }
  3501. __finally
  3502. {
  3503. delete EditorList;
  3504. }
  3505. }
  3506. else
  3507. {
  3508. // "open" case
  3509. MessageDialog(FMTLOAD(APP_EARLY_CLOSED, (Data->OriginalFileName)), qtWarning,
  3510. qaOK, HELP_APP_EARLY_CLOSED);
  3511. }
  3512. }
  3513. //---------------------------------------------------------------------------
  3514. void __fastcall TCustomScpExplorerForm::ExecutedFileUploadComplete(TObject * Sender)
  3515. {
  3516. EditorFormFileUploadComplete(DebugNotNull(dynamic_cast<TForm *>(Sender)));
  3517. }
  3518. //---------------------------------------------------------------------------
  3519. void __fastcall TCustomScpExplorerForm::RemoteDirViewEnter(TObject * /*Sender*/)
  3520. {
  3521. SideEnter(osRemote);
  3522. }
  3523. //---------------------------------------------------------------------------
  3524. void __fastcall TCustomScpExplorerForm::RemoteDriveViewEnter(TObject * /*Sender*/)
  3525. {
  3526. MakeNextInTabOrder(RemoteDirView, RemoteDriveView);
  3527. SideEnter(osRemote);
  3528. }
  3529. //---------------------------------------------------------------------------
  3530. void __fastcall TCustomScpExplorerForm::SideEnter(TOperationSide Side)
  3531. {
  3532. FCurrentSide = Side;
  3533. if (Visible)
  3534. {
  3535. UpdateControls();
  3536. }
  3537. }
  3538. //---------------------------------------------------------------------------
  3539. void __fastcall TCustomScpExplorerForm::DeleteFiles(TOperationSide Side,
  3540. TStrings * FileList, bool Alternative)
  3541. {
  3542. TCustomDirView * DView = DirView(Side);
  3543. DView->SaveSelection();
  3544. DView->SaveSelectedNames();
  3545. DebugAssert(!FAlternativeDelete);
  3546. FAlternativeDelete = Alternative;
  3547. try
  3548. {
  3549. if (Side == osRemote)
  3550. {
  3551. DebugAssert(Terminal != NULL);
  3552. Terminal->DeleteFiles(FileList, FLAGMASK(Alternative, dfAlternative));
  3553. }
  3554. else
  3555. {
  3556. try
  3557. {
  3558. TTerminalManager::Instance()->LocalTerminal->DeleteLocalFiles(FileList, FLAGMASK(Alternative, dfAlternative));
  3559. }
  3560. __finally
  3561. {
  3562. ReloadLocalDirectory();
  3563. }
  3564. }
  3565. FAlternativeDelete = false;
  3566. }
  3567. catch(...)
  3568. {
  3569. FAlternativeDelete = false;
  3570. DView->DiscardSavedSelection();
  3571. throw;
  3572. }
  3573. DView->RestoreSelection();
  3574. }
  3575. //---------------------------------------------------------------------------
  3576. void __fastcall TCustomScpExplorerForm::LockFiles(TStrings * FileList, bool Lock)
  3577. {
  3578. DebugAssert(Terminal);
  3579. RemoteDirView->SaveSelection();
  3580. RemoteDirView->SaveSelectedNames();
  3581. try
  3582. {
  3583. if (Lock)
  3584. {
  3585. Terminal->LockFiles(FileList);
  3586. }
  3587. else
  3588. {
  3589. Terminal->UnlockFiles(FileList);
  3590. }
  3591. }
  3592. catch(...)
  3593. {
  3594. RemoteDirView->DiscardSavedSelection();
  3595. throw;
  3596. }
  3597. RemoteDirView->RestoreSelection();
  3598. }
  3599. //---------------------------------------------------------------------------
  3600. bool __fastcall TCustomScpExplorerForm::RemoteTransferDialog(TTerminal *& Session,
  3601. TStrings * FileList, UnicodeString & Target, UnicodeString & FileMask, bool & DirectCopy,
  3602. bool NoConfirmation, bool Move)
  3603. {
  3604. if (RemoteDriveView->DropTarget != NULL)
  3605. {
  3606. Target = RemoteDriveView->NodePathName(RemoteDriveView->DropTarget);
  3607. }
  3608. else if (RemoteDirView->DropTarget != NULL)
  3609. {
  3610. DebugAssert(RemoteDirView->ItemIsDirectory(RemoteDirView->DropTarget));
  3611. Target = RemoteDirView->ItemFullFileName(RemoteDirView->DropTarget);
  3612. }
  3613. else
  3614. {
  3615. Target = RemoteDirView->Path;
  3616. }
  3617. if (Session == NULL)
  3618. {
  3619. Session = TTerminalManager::Instance()->ActiveTerminal;
  3620. }
  3621. Target = UnixIncludeTrailingBackslash(Target);
  3622. if (FileList->Count == 1)
  3623. {
  3624. FileMask = DelimitFileNameMask(UnixExtractFileName(FileList->Strings[0]));
  3625. }
  3626. else
  3627. {
  3628. FileMask = AnyMask;
  3629. }
  3630. DirectCopy = FTerminal->IsCapable[fcRemoteCopy] || FTerminal->IsCapable[fcSecondaryShell];
  3631. bool Result = true;
  3632. if (!NoConfirmation)
  3633. {
  3634. bool Multi = (FileList->Count > 1);
  3635. if (Move)
  3636. {
  3637. Result = DoRemoteMoveDialog(Multi, Target, FileMask);
  3638. }
  3639. else
  3640. {
  3641. DebugAssert(Terminal != NULL);
  3642. // update Terminal->StateData->RemoteDirectory
  3643. UpdateTerminal(Terminal);
  3644. TStrings * Sessions = TTerminalManager::Instance()->TerminalList;
  3645. TStrings * Directories = new TStringList;
  3646. try
  3647. {
  3648. for (int Index = 0; Index < Sessions->Count; Index++)
  3649. {
  3650. TManagedTerminal * Terminal =
  3651. dynamic_cast<TManagedTerminal *>(Sessions->Objects[Index]);
  3652. Directories->Add(Terminal->StateData->RemoteDirectory);
  3653. }
  3654. TDirectRemoteCopy AllowDirectCopy;
  3655. if (FTerminal->IsCapable[fcRemoteCopy] || FTerminal->CommandSessionOpened)
  3656. {
  3657. DebugAssert(DirectCopy);
  3658. AllowDirectCopy = drcAllow;
  3659. }
  3660. else if (FTerminal->IsCapable[fcSecondaryShell])
  3661. {
  3662. DebugAssert(DirectCopy);
  3663. AllowDirectCopy = drcConfirmCommandSession;
  3664. }
  3665. else
  3666. {
  3667. DebugAssert(!DirectCopy);
  3668. AllowDirectCopy = drcDisallow;
  3669. }
  3670. void * ASession = Session;
  3671. Result = DoRemoteCopyDialog(Sessions, Directories, AllowDirectCopy,
  3672. Multi, ASession, Target, FileMask, DirectCopy, TTerminalManager::Instance()->ActiveTerminal);
  3673. Session = static_cast<TTerminal *>(ASession);
  3674. }
  3675. __finally
  3676. {
  3677. delete Directories;
  3678. }
  3679. }
  3680. }
  3681. return Result;
  3682. }
  3683. //---------------------------------------------------------------------------
  3684. bool __fastcall TCustomScpExplorerForm::RemoteTransferFiles(
  3685. TStrings * FileList, bool NoConfirmation, bool Move, TTerminal * Session)
  3686. {
  3687. bool DirectCopy;
  3688. UnicodeString Target, FileMask;
  3689. bool Result = RemoteTransferDialog(Session, FileList, Target, FileMask, DirectCopy, NoConfirmation, Move);
  3690. if (Result)
  3691. {
  3692. if (!Move && !DirectCopy)
  3693. {
  3694. Configuration->Usage->Inc("RemoteCopyTemp");
  3695. UnicodeString RootTempDir;
  3696. UnicodeString TempDir;
  3697. TemporarilyDownloadFiles(FileList, false, RootTempDir, TempDir, false, false, false);
  3698. TStrings * TemporaryFilesList = new TStringList();
  3699. try
  3700. {
  3701. TMakeLocalFileListParams MakeFileListParam;
  3702. MakeFileListParam.FileList = TemporaryFilesList;
  3703. MakeFileListParam.FileTimes = NULL;
  3704. MakeFileListParam.IncludeDirs = true;
  3705. MakeFileListParam.Recursive = false;
  3706. ProcessLocalDirectory(TempDir, FTerminal->MakeLocalFileList, &MakeFileListParam);
  3707. TTerminalManager::Instance()->ActiveTerminal = Session;
  3708. if (TemporaryFilesList->Count > 0)
  3709. {
  3710. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  3711. CopyParam.FileMask = FileMask;
  3712. DebugAssert(!FAutoOperation);
  3713. FAutoOperation = true;
  3714. FTerminal->CopyToRemote(TemporaryFilesList, Target, &CopyParam, cpTemporary, NULL);
  3715. }
  3716. }
  3717. __finally
  3718. {
  3719. delete TemporaryFilesList;
  3720. FAutoOperation = false;
  3721. if (!RootTempDir.IsEmpty())
  3722. {
  3723. RecursiveDeleteFile(ExcludeTrailingBackslash(RootTempDir), false);
  3724. }
  3725. }
  3726. }
  3727. else
  3728. {
  3729. RemoteDirView->SaveSelection();
  3730. RemoteDirView->SaveSelectedNames();
  3731. try
  3732. {
  3733. if (Move)
  3734. {
  3735. Configuration->Usage->Inc("RemoteMove");
  3736. Terminal->MoveFiles(FileList, Target, FileMask);
  3737. }
  3738. else
  3739. {
  3740. Configuration->Usage->Inc("RemoteCopyDirect");
  3741. DebugAssert(DirectCopy);
  3742. DebugAssert(Session == FTerminal);
  3743. if (FTerminal->IsCapable[fcRemoteCopy] ||
  3744. FTerminal->CommandSessionOpened ||
  3745. CommandSessionFallback())
  3746. {
  3747. Terminal->CopyFiles(FileList, Target, FileMask);
  3748. }
  3749. }
  3750. }
  3751. catch(...)
  3752. {
  3753. RemoteDirView->DiscardSavedSelection();
  3754. throw;
  3755. }
  3756. RemoteDirView->RestoreSelection();
  3757. }
  3758. }
  3759. return Result;
  3760. }
  3761. //---------------------------------------------------------------------------
  3762. void __fastcall TCustomScpExplorerForm::CreateRemoteDirectory(
  3763. const UnicodeString & Path, TRemoteProperties & Properties)
  3764. {
  3765. Properties.Valid = Properties.Valid << vpEncrypt;
  3766. Properties.Encrypt = GUIConfiguration->CurrentCopyParam.EncryptNewFiles;
  3767. RemoteDirView->CreateDirectoryEx(Path, &Properties);
  3768. }
  3769. //---------------------------------------------------------------------------
  3770. void __fastcall TCustomScpExplorerForm::CreateDirectory(TOperationSide Side)
  3771. {
  3772. Side = GetSide(Side);
  3773. TRemoteProperties Properties = GUIConfiguration->NewDirectoryProperties;
  3774. TRemoteProperties * AProperties = (Side == osRemote ? &Properties : NULL);
  3775. UnicodeString Name = LoadStr(NEW_FOLDER);
  3776. int AllowedChanges =
  3777. FLAGMASK((Side == osRemote) && Terminal->IsCapable[fcModeChanging], cpMode);
  3778. bool SaveSettings = false;
  3779. if (DoCreateDirectoryDialog(Name, AProperties, AllowedChanges, SaveSettings))
  3780. {
  3781. TWindowLock Lock(this);
  3782. if (Side == osRemote)
  3783. {
  3784. if (SaveSettings)
  3785. {
  3786. GUIConfiguration->NewDirectoryProperties = Properties;
  3787. }
  3788. CreateRemoteDirectory(Name, Properties);
  3789. }
  3790. else
  3791. {
  3792. DirView(Side)->CreateDirectory(Name);
  3793. }
  3794. }
  3795. }
  3796. //---------------------------------------------------------------------------
  3797. void __fastcall TCustomScpExplorerForm::HomeDirectory(TOperationSide Side)
  3798. {
  3799. TWindowLock Lock(this);
  3800. DirView(Side)->ExecuteHomeDirectory();
  3801. }
  3802. //---------------------------------------------------------------------------
  3803. void __fastcall TCustomScpExplorerForm::OpenDirectory(TOperationSide Side)
  3804. {
  3805. DoOpenDirectoryDialog(odBrowse, Side);
  3806. }
  3807. //---------------------------------------------------------------------------
  3808. bool __fastcall TCustomScpExplorerForm::OpenBookmark(TOperationSide Side, TBookmark * Bookmark)
  3809. {
  3810. UnicodeString Path = Bookmark->GetSideDirectory(Side);
  3811. bool Result = !Path.IsEmpty();
  3812. if (Result)
  3813. {
  3814. // While we might get here when the session is closed (from location profiles),
  3815. // it's not a problem as the Path setter is noop then
  3816. DirView(Side)->Path = Path;
  3817. }
  3818. return Result;
  3819. }
  3820. //---------------------------------------------------------------------------
  3821. void __fastcall TCustomScpExplorerForm::CalculateSize(
  3822. TStrings * FileList, __int64 & Size, TCalculateSizeStats & Stats,
  3823. bool & Close)
  3824. {
  3825. // terminal can be already closed (e.g. dropped connection)
  3826. if (Terminal != NULL)
  3827. {
  3828. try
  3829. {
  3830. Terminal->CalculateFilesSize(FileList, Size, 0, NULL, true, Stats);
  3831. }
  3832. catch(...)
  3833. {
  3834. if (!Terminal->Active)
  3835. {
  3836. Close = true;
  3837. }
  3838. throw;
  3839. }
  3840. }
  3841. }
  3842. //---------------------------------------------------------------------------
  3843. void __fastcall TCustomScpExplorerForm::CalculateChecksum(const UnicodeString & Alg,
  3844. TStrings * FileList, TCalculatedChecksumEvent OnCalculatedChecksum,
  3845. bool & Close)
  3846. {
  3847. // terminal can be already closed (e.g. dropped connection)
  3848. if (Terminal != NULL)
  3849. {
  3850. Configuration->Usage->Inc(L"ChecksumCalculated");
  3851. try
  3852. {
  3853. Terminal->CalculateFilesChecksum(Alg, FileList, NULL, OnCalculatedChecksum);
  3854. }
  3855. catch(...)
  3856. {
  3857. if (!Terminal->Active)
  3858. {
  3859. Close = true;
  3860. }
  3861. throw;
  3862. }
  3863. }
  3864. }
  3865. //---------------------------------------------------------------------------
  3866. bool __fastcall TCustomScpExplorerForm::SetProperties(TOperationSide Side, TStrings * FileList)
  3867. {
  3868. bool Result;
  3869. if (Side == osRemote)
  3870. {
  3871. TRemoteTokenList * GroupList = NULL;
  3872. TRemoteTokenList * UserList = NULL;
  3873. try
  3874. {
  3875. TRemoteProperties CurrentProperties;
  3876. if (Terminal->LoadFilesProperties(FileList))
  3877. {
  3878. RemoteDirView->Invalidate();
  3879. }
  3880. bool CapableGroupChanging = Terminal->IsCapable[fcGroupChanging];
  3881. bool CapableOwnerChanging = Terminal->IsCapable[fcOwnerChanging];
  3882. if (CapableGroupChanging || CapableOwnerChanging)
  3883. {
  3884. if (CapableGroupChanging)
  3885. {
  3886. GroupList = Terminal->Groups->Duplicate();
  3887. }
  3888. if (CapableOwnerChanging)
  3889. {
  3890. UserList = Terminal->Users->Duplicate();
  3891. }
  3892. TRemoteDirectory * Files = Terminal->Files;
  3893. int Count = Files->Count;
  3894. if (Count > 100)
  3895. {
  3896. Count = 100;
  3897. }
  3898. for (int Index = 0; Index < Count; Index++)
  3899. {
  3900. TRemoteFile * File = Files->Files[Index];
  3901. if (CapableGroupChanging)
  3902. {
  3903. GroupList->AddUnique(File->Group);
  3904. }
  3905. if (CapableOwnerChanging)
  3906. {
  3907. UserList->AddUnique(File->Owner);
  3908. }
  3909. }
  3910. // if we haven't collected tokens for all files in current directory,
  3911. // make sure we collect them at least for all selected files.
  3912. // (note that so far the files in FileList has to be from current direcotry)
  3913. if (Count < Files->Count)
  3914. {
  3915. for (int Index = 0; Index < FileList->Count; Index++)
  3916. {
  3917. TRemoteFile * File = (TRemoteFile *)(FileList->Objects[Index]);
  3918. if (CapableGroupChanging)
  3919. {
  3920. GroupList->AddUnique(File->Group);
  3921. }
  3922. if (CapableOwnerChanging)
  3923. {
  3924. UserList->AddUnique(File->Owner);
  3925. }
  3926. }
  3927. }
  3928. }
  3929. CurrentProperties = TRemoteProperties::CommonProperties(FileList);
  3930. int Flags = 0;
  3931. if (Terminal->IsCapable[fcModeChanging]) Flags |= cpMode;
  3932. if (CapableOwnerChanging) Flags |= cpOwner;
  3933. if (CapableGroupChanging) Flags |= cpGroup;
  3934. TCalculateChecksumEvent CalculateChecksumEvent = NULL;
  3935. if (Terminal->IsCapable[fcCalculatingChecksum])
  3936. {
  3937. CalculateChecksumEvent = CalculateChecksum;
  3938. }
  3939. std::unique_ptr<TStrings> ChecksumAlgs(new TStringList());
  3940. Terminal->GetSupportedChecksumAlgs(ChecksumAlgs.get());
  3941. TRemoteProperties NewProperties = CurrentProperties;
  3942. Result =
  3943. DoPropertiesDialog(FileList, RemoteDirView->PathName,
  3944. GroupList, UserList, ChecksumAlgs.get(), &NewProperties, Flags,
  3945. Terminal->IsCapable[fcGroupOwnerChangingByID],
  3946. CalculateSize, CalculateChecksumEvent);
  3947. if (Result)
  3948. {
  3949. NewProperties = TRemoteProperties::ChangedProperties(CurrentProperties, NewProperties);
  3950. Terminal->ChangeFilesProperties(FileList, &NewProperties);
  3951. }
  3952. }
  3953. __finally
  3954. {
  3955. delete GroupList;
  3956. delete UserList;
  3957. }
  3958. }
  3959. else
  3960. {
  3961. DirView(Side)->DisplayPropertiesMenu();
  3962. Result = true;
  3963. }
  3964. return Result;
  3965. }
  3966. //---------------------------------------------------------------------------
  3967. void __fastcall TCustomScpExplorerForm::KeyProcessed(Word & Key, TShiftState Shift)
  3968. {
  3969. if (Shift * AllKeyShiftStates() == (TShiftState() << ssAlt))
  3970. {
  3971. FIgnoreNextDialogChar = Key;
  3972. }
  3973. Key = 0;
  3974. }
  3975. //---------------------------------------------------------------------------
  3976. void __fastcall TCustomScpExplorerForm::CheckCustomCommandShortCut(
  3977. TCustomCommandList * List, Word & Key, Classes::TShiftState Shift, TShortCut KeyShortCut)
  3978. {
  3979. const TCustomCommandType * Command = List->Find(KeyShortCut);
  3980. if (Command != NULL)
  3981. {
  3982. KeyProcessed(Key, Shift);
  3983. if (CustomCommandState(*Command, false, ccltAll) > 0)
  3984. {
  3985. ExecuteFileOperationCommand(foCustomCommand, osRemote,
  3986. false, false, const_cast<TCustomCommandType *>(Command));
  3987. }
  3988. }
  3989. }
  3990. //---------------------------------------------------------------------------
  3991. void __fastcall TCustomScpExplorerForm::KeyDown(Word & Key, Classes::TShiftState Shift)
  3992. {
  3993. if (QueueView3->Focused() && (QueueView3->OnKeyDown != NULL))
  3994. {
  3995. QueueView3->OnKeyDown(QueueView3, Key, Shift);
  3996. }
  3997. if (!DirView(osCurrent)->IsEditing())
  3998. {
  3999. TShortCut KeyShortCut = ShortCut(Key, Shift);
  4000. for (int Index = 0; Index < NonVisualDataModule->ExplorerActions->ActionCount; Index++)
  4001. {
  4002. TAction * Action = (TAction *)NonVisualDataModule->ExplorerActions->Actions[Index];
  4003. if (((Action->ShortCut == KeyShortCut) ||
  4004. (Action->SecondaryShortCuts->IndexOfShortCut(KeyShortCut) >= 0)) &&
  4005. AllowedAction(Action, aaShortCut))
  4006. {
  4007. // Has to be called before the action as the dialog char is already in queue.
  4008. // So when the action consumes message queue, we already need to have the
  4009. // FIgnoreNextDialogChar set
  4010. KeyProcessed(Key, Shift);
  4011. // Reset reference to previous component (when menu/toolbar was clicked).
  4012. // Needed to detect that action was invoked by keyboard shortcut
  4013. // in TNonVisualDataModule::ExplorerActionsExecute
  4014. Action->ActionComponent = NULL;
  4015. Action->Execute();
  4016. return;
  4017. }
  4018. }
  4019. for (int i = 0; i < TTerminalManager::Instance()->Count; i++)
  4020. {
  4021. if (NonVisualDataModule->OpenSessionShortCut(i) == KeyShortCut)
  4022. {
  4023. KeyProcessed(Key, Shift);
  4024. TTerminalManager::Instance()->ActiveTerminalIndex = i;
  4025. return;
  4026. }
  4027. }
  4028. if (Key == VK_TAB && Shift.Contains(ssCtrl))
  4029. {
  4030. KeyProcessed(Key, Shift);
  4031. TTerminalManager::Instance()->CycleTerminals(!Shift.Contains(ssShift));
  4032. }
  4033. TShortCut CustomShortCut = NormalizeCustomShortCut(KeyShortCut);
  4034. if (IsCustomShortCut(CustomShortCut))
  4035. {
  4036. CheckCustomCommandShortCut(WinConfiguration->CustomCommandList, Key, Shift, CustomShortCut);
  4037. CheckCustomCommandShortCut(WinConfiguration->ExtensionList, Key, Shift, CustomShortCut);
  4038. if (WinConfiguration->SharedBookmarks != NULL)
  4039. {
  4040. TBookmark * Bookmark = WinConfiguration->SharedBookmarks->FindByShortCut(CustomShortCut);
  4041. if ((Bookmark != NULL) &&
  4042. OpenBookmark(FCurrentSide, Bookmark))
  4043. {
  4044. KeyProcessed(Key, Shift);
  4045. }
  4046. }
  4047. }
  4048. }
  4049. TForm::KeyDown(Key, Shift);
  4050. }
  4051. //---------------------------------------------------------------------------
  4052. void __fastcall TCustomScpExplorerForm::InitStatusBar()
  4053. {
  4054. const TSessionInfo & SessionInfo = Terminal->GetSessionInfo();
  4055. const TFileSystemInfo & FileSystemInfo = Terminal->GetFileSystemInfo();
  4056. TTBXStatusBar * SessionStatusBar = (TTBXStatusBar *)GetComponent(fcStatusBar);
  4057. DebugAssert(Terminal);
  4058. int Offset = SessionStatusBar->Panels->Count - SessionPanelCount;
  4059. bool SecurityEnabled = !SessionInfo.SecurityProtocolName.IsEmpty();
  4060. SessionStatusBar->Panels->Items[Offset + 0]->Enabled = SecurityEnabled;
  4061. // expanded from ?: to avoid memory leaks
  4062. if (SecurityEnabled)
  4063. {
  4064. SessionStatusBar->Panels->Items[Offset + 0]->Hint =
  4065. FMTLOAD(STATUS_SECURE, (SessionInfo.SecurityProtocolName));
  4066. }
  4067. else
  4068. {
  4069. SessionStatusBar->Panels->Items[Offset + 0]->Hint = LoadStr(STATUS_INSECURE);
  4070. }
  4071. if (FileSystemInfo.ProtocolName.IsEmpty())
  4072. {
  4073. SessionStatusBar->Panels->Items[Offset + 1]->Caption = SessionInfo.ProtocolName;
  4074. }
  4075. else
  4076. {
  4077. SessionStatusBar->Panels->Items[Offset + 1]->Caption = FileSystemInfo.ProtocolName;
  4078. }
  4079. SessionStatusBar->Panels->Items[Offset + 1]->Hint = LoadStr(STATUS_PROTOCOL_HINT);
  4080. SessionStatusBar->Panels->Items[Offset + 2]->Enabled =
  4081. (!SessionInfo.CSCompression.IsEmpty() || !SessionInfo.SCCompression.IsEmpty());
  4082. UnicodeString CSCompressionStr = DefaultStr(SessionInfo.CSCompression, LoadStr(NO_STR));
  4083. UnicodeString SCCompressionStr = DefaultStr(SessionInfo.SCCompression, LoadStr(NO_STR));
  4084. UnicodeString CompressionStr = CSCompressionStr;
  4085. if (CSCompressionStr != SCCompressionStr)
  4086. {
  4087. CompressionStr += UnicodeString(L"/") + SCCompressionStr;
  4088. }
  4089. SessionStatusBar->Panels->Items[Offset + 2]->Hint = FMTLOAD(STATUS_COMPRESSION_HINT, (CompressionStr));
  4090. SessionStatusBar->Panels->Items[Offset + 3]->Hint = LoadStr(STATUS_DURATION_HINT);
  4091. UpdateStatusBar();
  4092. }
  4093. //---------------------------------------------------------------------------
  4094. void __fastcall TCustomScpExplorerForm::UpdateStatusBar()
  4095. {
  4096. TTBXStatusBar * SessionStatusBar = (TTBXStatusBar *)GetComponent(fcStatusBar);
  4097. DebugAssert(SessionStatusBar != NULL);
  4098. if (!Terminal || !Terminal->Active || Terminal->Status < ssOpened)
  4099. {
  4100. // note: (Terminal->Status < sshReady) currently never happens here,
  4101. // so STATUS_CONNECTING is never used
  4102. SessionStatusBar->SimplePanel = true;
  4103. SessionStatusBar->SimpleText = LoadStr(
  4104. !Terminal || !Terminal->Active ? STATUS_NOT_CONNECTED : STATUS_CONNECTING);
  4105. }
  4106. else
  4107. {
  4108. DebugAssert(Terminal);
  4109. SessionStatusBar->SimplePanel = false;
  4110. const TSessionInfo & SessionInfo = Terminal->GetSessionInfo();
  4111. if (!FNote.IsEmpty())
  4112. {
  4113. SessionStatusBar->Panels->Items[0]->Caption = FNote;
  4114. }
  4115. else
  4116. {
  4117. UpdateStatusPanelText(SessionStatusBar->Panels->Items[0]);
  4118. }
  4119. SessionStatusBar->Panels->Items[0]->Hint = FNoteHints;
  4120. SessionStatusBar->Panels->Items[SessionStatusBar->Panels->Count - 1]->Caption =
  4121. FormatDateTimeSpan(Configuration->TimeFormat, Now() - SessionInfo.LoginTime);
  4122. }
  4123. }
  4124. //---------------------------------------------------------------------------
  4125. void __fastcall TCustomScpExplorerForm::UpdateStatusPanelText(TTBXStatusPanel * Panel)
  4126. {
  4127. Panel->Caption = L"";
  4128. }
  4129. //---------------------------------------------------------------------------
  4130. void __fastcall TCustomScpExplorerForm::Idle()
  4131. {
  4132. if (FShowing || FStandaloneEditing)
  4133. {
  4134. FEditorManager->Check();
  4135. // make sure that Idle is called before update queue, as it may invoke QueueEvent
  4136. // that needs to know if queue view is visible (and it may be closed after queue update)
  4137. TTerminalManager::Instance()->Idle(FDoNotIdleCurrentTerminal > 0);
  4138. }
  4139. if (FShowing)
  4140. {
  4141. if (!NonVisualDataModule->Busy &&
  4142. // Menu is opened or drag&drop is going on
  4143. (Mouse->Capture == NULL))
  4144. {
  4145. if (FRefreshRemoteDirectory)
  4146. {
  4147. if ((Terminal != NULL) && (Terminal->Status == ssOpened))
  4148. {
  4149. Terminal->RefreshDirectory();
  4150. }
  4151. FRefreshRemoteDirectory = false;
  4152. }
  4153. if (FRefreshLocalDirectory)
  4154. {
  4155. ReloadLocalDirectory();
  4156. FRefreshLocalDirectory = false;
  4157. }
  4158. if (WinConfiguration->RefreshRemotePanel)
  4159. {
  4160. TManagedTerminal * ManagedTerminal =
  4161. dynamic_cast<TManagedTerminal *>(Terminal);
  4162. if ((ManagedTerminal != NULL) && (Terminal->Status == ssOpened) &&
  4163. (Now() - ManagedTerminal->DirectoryLoaded >
  4164. WinConfiguration->RefreshRemotePanelInterval))
  4165. {
  4166. RemoteDirView->ReloadDirectory();
  4167. }
  4168. }
  4169. }
  4170. }
  4171. if (FShowing || FStandaloneEditing)
  4172. {
  4173. if (FQueueStatusInvalidated)
  4174. {
  4175. UpdateQueueStatus(false);
  4176. }
  4177. RefreshQueueItems();
  4178. }
  4179. if (FShowing)
  4180. {
  4181. UpdateStatusBar();
  4182. }
  4183. FIgnoreNextDialogChar = 0;
  4184. }
  4185. //---------------------------------------------------------------------------
  4186. void __fastcall TCustomScpExplorerForm::UserActionTimer(TObject * /*Sender*/)
  4187. {
  4188. try
  4189. {
  4190. FUserActionTimer->Enabled = false;
  4191. if (IsQueueAutoPopup() && (FPendingQueueActionItem != NULL))
  4192. {
  4193. if (TQueueItem::IsUserActionStatus(FPendingQueueActionItem->Status))
  4194. {
  4195. FPendingQueueActionItem->ProcessUserAction();
  4196. }
  4197. }
  4198. }
  4199. __finally
  4200. {
  4201. FPendingQueueActionItem = NULL;
  4202. }
  4203. }
  4204. //---------------------------------------------------------------------------
  4205. void __fastcall TCustomScpExplorerForm::ApplicationMinimize(TObject * /*Sender*/)
  4206. {
  4207. if (WinConfiguration->MinimizeToTray)
  4208. {
  4209. UpdateTrayIcon();
  4210. FTrayIcon->Visible = true;
  4211. if (Visible)
  4212. {
  4213. ShowWindow(Handle, SW_HIDE);
  4214. }
  4215. }
  4216. }
  4217. //---------------------------------------------------------------------------
  4218. void __fastcall TCustomScpExplorerForm::ApplicationRestore(TObject * /*Sender*/)
  4219. {
  4220. // WORKAROUND
  4221. // When restoring maximized window from minimization,
  4222. // rarely some controls do not align properly.
  4223. // Two instances seen (both for Commander):
  4224. // - When restoring, window is temporarily narrower (not maximized),
  4225. // causing toolbars on TopDock to wrap and dock to expand horizontally.
  4226. // Once maximized already, top dock shinks back, but the session PageControl,
  4227. // do not align up, leaving space between TopDock and PageControl.
  4228. // - Similar issue seen with LocalDirView not aligning down to status bar.
  4229. for (int Index = 0; Index < ControlCount; Index++)
  4230. {
  4231. RealignControl(Controls[Index]);
  4232. }
  4233. if (FTrayIcon->Visible)
  4234. {
  4235. FTrayIcon->Visible = false;
  4236. if (Visible)
  4237. {
  4238. ShowWindow(Handle, SW_SHOW);
  4239. }
  4240. }
  4241. if (FNeedSession && DebugAlwaysTrue(Terminal == NULL))
  4242. {
  4243. FNeedSession = false;
  4244. NonVisualDataModule->StartBusy();
  4245. try
  4246. {
  4247. NeedSession(false);
  4248. }
  4249. __finally
  4250. {
  4251. NonVisualDataModule->EndBusy();
  4252. }
  4253. }
  4254. }
  4255. //---------------------------------------------------------------------------
  4256. void __fastcall TCustomScpExplorerForm::UpdateTrayIcon()
  4257. {
  4258. FTrayIcon->Hint = Caption;
  4259. }
  4260. //---------------------------------------------------------------------------
  4261. void __fastcall TCustomScpExplorerForm::ApplicationTitleChanged()
  4262. {
  4263. UpdateTrayIcon();
  4264. }
  4265. //---------------------------------------------------------------------------
  4266. void __fastcall TCustomScpExplorerForm::RestoreApp()
  4267. {
  4268. // workaround
  4269. // TApplication.WndProc sets TApplication.FAppIconic to false,
  4270. // on WM_ACTIVATEAPP, what renders TApplication.Restore no-op function.
  4271. // But WM_ACTIVATEAPP message can be received even when
  4272. // the main window is minimized to the tray and internal editor window is focused
  4273. // (after another application was previously active)
  4274. if (::IsIconic(Handle))
  4275. {
  4276. if (!IsAppIconic())
  4277. {
  4278. SetAppIconic(true);
  4279. }
  4280. }
  4281. ::ApplicationRestore();
  4282. Application->BringToFront();
  4283. }
  4284. //---------------------------------------------------------------------------
  4285. void __fastcall TCustomScpExplorerForm::TrayIconClick(TObject * /*Sender*/)
  4286. {
  4287. RestoreApp();
  4288. }
  4289. //---------------------------------------------------------------------------
  4290. void __fastcall TCustomScpExplorerForm::NewSession(bool FromSite, const UnicodeString & SessionUrl)
  4291. {
  4292. if (OpenInNewWindow())
  4293. {
  4294. // todo: Pass FromSite
  4295. ExecuteNewInstance(SessionUrl);
  4296. }
  4297. else
  4298. {
  4299. TTerminalManager::Instance()->NewSession(FromSite, SessionUrl);
  4300. }
  4301. }
  4302. //---------------------------------------------------------------------------
  4303. UnicodeString __fastcall TCustomScpExplorerForm::CreateHiddenDuplicateSession()
  4304. {
  4305. UnicodeString SessionName = StoredSessions->HiddenPrefix + Terminal->SessionData->SessionName;
  4306. // current working directories become defaults here, what is not right
  4307. std::unique_ptr<TSessionData> SessionData(CloneCurrentSessionData());
  4308. StoredSessions->NewSession(SessionName, SessionData.get());
  4309. // modified only, explicit
  4310. StoredSessions->Save(false, true);
  4311. // encode session name because of slashes in hierarchical sessions
  4312. return EncodeUrlString(SessionName);
  4313. }
  4314. //---------------------------------------------------------------------------
  4315. void __fastcall TCustomScpExplorerForm::DuplicateSession()
  4316. {
  4317. if (OpenInNewWindow())
  4318. {
  4319. ExecuteNewInstance(CreateHiddenDuplicateSession());
  4320. }
  4321. else
  4322. {
  4323. // current working directories become defaults here, what is not right
  4324. std::unique_ptr<TSessionData> SessionData(CloneCurrentSessionData());
  4325. TTerminalManager * Manager = TTerminalManager::Instance();
  4326. TTerminal * Terminal = Manager->NewTerminal(SessionData.get());
  4327. Manager->ActiveTerminal = Terminal;
  4328. }
  4329. }
  4330. //---------------------------------------------------------------------------
  4331. bool __fastcall TCustomScpExplorerForm::CanCloseQueue()
  4332. {
  4333. DebugAssert(FQueue != NULL);
  4334. bool Result = FQueue->IsEmpty;
  4335. if (!Result)
  4336. {
  4337. SetFocus();
  4338. Result = (MessageDialog(LoadStr(PENDING_QUEUE_ITEMS2), qtWarning, qaOK | qaCancel, HELP_NONE) == qaOK);
  4339. }
  4340. return Result;
  4341. }
  4342. //---------------------------------------------------------------------------
  4343. void __fastcall TCustomScpExplorerForm::CloseSession()
  4344. {
  4345. if (CanCloseQueue())
  4346. {
  4347. TTerminalManager::Instance()->FreeActiveTerminal();
  4348. }
  4349. }
  4350. //---------------------------------------------------------------------------
  4351. void __fastcall TCustomScpExplorerForm::OpenStoredSession(TSessionData * Data)
  4352. {
  4353. if (OpenInNewWindow())
  4354. {
  4355. // encode session name because of slashes in hierarchical sessions
  4356. ExecuteNewInstance(EncodeUrlString(Data->Name));
  4357. }
  4358. else
  4359. {
  4360. TTerminalManager * Manager = TTerminalManager::Instance();
  4361. TTerminal * Terminal = Manager->NewTerminal(Data);
  4362. Manager->ActiveTerminal = Terminal;
  4363. }
  4364. }
  4365. //---------------------------------------------------------------------------
  4366. void __fastcall TCustomScpExplorerForm::OpenFolderOrWorkspace(const UnicodeString & Name)
  4367. {
  4368. if (OpenInNewWindow())
  4369. {
  4370. ExecuteNewInstance(Name);
  4371. }
  4372. else
  4373. {
  4374. TTerminalManager * Manager = TTerminalManager::Instance();
  4375. std::unique_ptr<TObjectList> DataList(new TObjectList());
  4376. StoredSessions->GetFolderOrWorkspace(Name, DataList.get());
  4377. Manager->ActiveTerminal = Manager->NewTerminals(DataList.get());
  4378. }
  4379. }
  4380. //---------------------------------------------------------------------------
  4381. void __fastcall TCustomScpExplorerForm::FormCloseQuery(TObject * /*Sender*/,
  4382. bool &CanClose)
  4383. {
  4384. if (Terminal != NULL)
  4385. {
  4386. if (Terminal->Active && WinConfiguration->ConfirmClosingSession)
  4387. {
  4388. unsigned int Result;
  4389. TMessageParams Params(mpNeverAskAgainCheck);
  4390. UnicodeString Message;
  4391. int Answers = qaOK | qaCancel;
  4392. if (TTerminalManager::Instance()->Count > 1)
  4393. {
  4394. if (!WinConfiguration->AutoSaveWorkspace)
  4395. {
  4396. Message = LoadStr(CLOSE_SESSIONS_WORKSPACE2);
  4397. Answers = qaYes | qaNo | qaCancel;
  4398. }
  4399. else
  4400. {
  4401. Message = MainInstructions(LoadStr(CLOSE_SESSIONS));
  4402. }
  4403. }
  4404. else
  4405. {
  4406. Message = MainInstructions(FMTLOAD(CLOSE_SESSION, (Terminal->SessionData->SessionName)));
  4407. }
  4408. if (WinConfiguration->AutoSaveWorkspace)
  4409. {
  4410. Message =
  4411. FORMAT("%s\n\n%s", (Message,
  4412. FMTLOAD(AUTO_WORKSPACE, (WorkspaceName()))));
  4413. }
  4414. SetFocus();
  4415. Result = MessageDialog(Message, qtConfirmation,
  4416. Answers, HELP_NONE, &Params);
  4417. if (Result == qaNeverAskAgain)
  4418. {
  4419. WinConfiguration->ConfirmClosingSession = false;
  4420. }
  4421. if (Result == qaNo)
  4422. {
  4423. CanClose = SaveWorkspace(true);
  4424. // note that the workspace will be saved redundatly again from FormClose
  4425. }
  4426. else
  4427. {
  4428. CanClose =
  4429. (Result == qaOK) ||
  4430. (Result == qaYes) || // CLOSE_SESSIONS_WORKSPACE variant
  4431. (Result == qaNeverAskAgain);
  4432. }
  4433. }
  4434. if (CanClose)
  4435. {
  4436. CanClose = CanCloseQueue();
  4437. }
  4438. }
  4439. if (CanClose)
  4440. {
  4441. CanClose =
  4442. FEditorManager->CloseInternalEditors(CloseInternalEditor) &&
  4443. FEditorManager->CloseExternalFilesWithoutProcess();
  4444. if (CanClose)
  4445. {
  4446. while (CanClose && (FLocalEditors->Count > 0))
  4447. {
  4448. int PrevCount = FLocalEditors->Count;
  4449. static_cast<TForm *>(FLocalEditors->Items[0])->Close();
  4450. CanClose = (FLocalEditors->Count < PrevCount);
  4451. }
  4452. if (CanClose)
  4453. {
  4454. CanClose = FEditorManager->Empty(true);
  4455. if (!CanClose)
  4456. {
  4457. SetFocus();
  4458. CanClose =
  4459. (MessageDialog(
  4460. LoadStr(PENDING_EDITORS), qtWarning, qaIgnore | qaCancel, HELP_NONE) == qaIgnore);
  4461. }
  4462. }
  4463. }
  4464. }
  4465. }
  4466. //---------------------------------------------------------------------------
  4467. void __fastcall TCustomScpExplorerForm::CloseInternalEditor(TObject * Sender)
  4468. {
  4469. TForm * Form = dynamic_cast<TForm *>(Sender);
  4470. DebugAssert(Form != NULL);
  4471. Form->Close();
  4472. }
  4473. //---------------------------------------------------------------------------
  4474. void __fastcall TCustomScpExplorerForm::ForceCloseInternalEditor(TObject * Sender)
  4475. {
  4476. TForm * Form = dynamic_cast<TForm *>(Sender);
  4477. delete Form;
  4478. }
  4479. //---------------------------------------------------------------------------
  4480. void __fastcall TCustomScpExplorerForm::ForceCloseLocalEditors()
  4481. {
  4482. while (FLocalEditors->Count > 0)
  4483. {
  4484. delete static_cast<TForm *>(FLocalEditors->Items[0]);
  4485. }
  4486. }
  4487. //---------------------------------------------------------------------------
  4488. void __fastcall TCustomScpExplorerForm::RemoteDirViewDisplayProperties(
  4489. TObject *Sender)
  4490. {
  4491. TStrings *FileList = ((TUnixDirView*)Sender)->CreateFileList(True, False, NULL);
  4492. try
  4493. {
  4494. SetProperties(osRemote, FileList);
  4495. }
  4496. __finally
  4497. {
  4498. delete FileList;
  4499. }
  4500. }
  4501. //---------------------------------------------------------------------------
  4502. void __fastcall TCustomScpExplorerForm::ComponentShowing(Byte Component, bool value)
  4503. {
  4504. if (value)
  4505. {
  4506. if (Component == fcCustomCommandsBand)
  4507. {
  4508. UpdateCustomCommandsToolbar();
  4509. }
  4510. }
  4511. }
  4512. //---------------------------------------------------------------------------
  4513. void __fastcall TCustomScpExplorerForm::SetComponentVisible(Byte Component, Boolean value)
  4514. {
  4515. TControl * Control = GetComponent(Component);
  4516. DebugAssert(Control);
  4517. bool Changed = (Control->Visible != value);
  4518. if (Changed)
  4519. {
  4520. ComponentShowing(Component, value);
  4521. TWinControl * WinControl = dynamic_cast<TWinControl*>(Control);
  4522. bool WasFocused = (WinControl != NULL) && (ActiveControl != NULL) &&
  4523. ((ActiveControl == WinControl) || (ActiveControl->Parent == WinControl));
  4524. if (value)
  4525. {
  4526. int RemainingHeight = Control->Parent->ClientHeight;
  4527. int RemainingWidth = Control->Parent->ClientWidth;
  4528. for (int i = 0; i < Control->Parent->ControlCount; i++)
  4529. {
  4530. TControl * ChildControl = Control->Parent->Controls[i];
  4531. if (ChildControl->Visible)
  4532. {
  4533. switch (ChildControl->Align)
  4534. {
  4535. case alTop:
  4536. case alBottom:
  4537. RemainingHeight -= ChildControl->Height;
  4538. break;
  4539. case alLeft:
  4540. case alRight:
  4541. RemainingWidth -= ChildControl->Width;
  4542. break;
  4543. }
  4544. }
  4545. }
  4546. int Reserve = ScaleByTextHeight(this, 32);
  4547. // queue in explorer, trees in commander
  4548. if (Control->Height > RemainingHeight - Reserve)
  4549. {
  4550. Control->Height = RemainingHeight / 2;
  4551. }
  4552. if (Control->Width > RemainingWidth - Reserve)
  4553. {
  4554. Control->Width = RemainingWidth / 2;
  4555. }
  4556. }
  4557. Control->Visible = value;
  4558. if (WasFocused && Visible)
  4559. {
  4560. DirView(osCurrent)->SetFocus();
  4561. }
  4562. FixControlsPlacement();
  4563. }
  4564. }
  4565. //---------------------------------------------------------------------------
  4566. bool __fastcall TCustomScpExplorerForm::GetComponentVisible(Byte Component)
  4567. {
  4568. TControl * Control = GetComponent(Component);
  4569. if (Control == NULL)
  4570. {
  4571. return false;
  4572. }
  4573. else
  4574. {
  4575. return Control->Visible;
  4576. }
  4577. }
  4578. //---------------------------------------------------------------------------
  4579. bool __fastcall TCustomScpExplorerForm::IsComponentPossible(Byte Component)
  4580. {
  4581. bool Result;
  4582. if ((Component == fcExplorerUpdatesBand) || (Component == fcCommanderUpdatesBand))
  4583. {
  4584. Result = !IsUWP();
  4585. }
  4586. else
  4587. {
  4588. Result = true;
  4589. }
  4590. return Result;
  4591. }
  4592. //---------------------------------------------------------------------------
  4593. void __fastcall TCustomScpExplorerForm::FixControlsPlacement()
  4594. {
  4595. if (RemoteDirView->ItemFocused != NULL)
  4596. {
  4597. RemoteDirView->ItemFocused->MakeVisible(false);
  4598. }
  4599. QueueSplitter->Visible = QueuePanel->Visible;
  4600. RemotePanelSplitter->Visible = RemoteDriveView->Visible;
  4601. }
  4602. //---------------------------------------------------------------------------
  4603. TControl * __fastcall TCustomScpExplorerForm::GetComponent(Byte Component)
  4604. {
  4605. switch (Component) {
  4606. case fcStatusBar: return RemoteStatusBar;
  4607. case fcRemotePopup: return reinterpret_cast<TControl *>(NonVisualDataModule->RemoteFilePopup);
  4608. case fcQueueView: return QueuePanel;
  4609. case fcQueueToolbar: return QueueDock;
  4610. case fcRemoteTree: return RemoteDriveView;
  4611. case fcSessionsTabs: return SessionsPageControl;
  4612. default: return NULL;
  4613. }
  4614. }
  4615. //---------------------------------------------------------------------------
  4616. void __fastcall TCustomScpExplorerForm::DirViewColumnRightClick(
  4617. TObject *Sender, TListColumn *Column, TPoint &Point)
  4618. {
  4619. DebugAssert(NonVisualDataModule && Column && Sender);
  4620. NonVisualDataModule->ListColumn = Column;
  4621. TPopupMenu * DirViewColumnMenu;
  4622. if (Sender == RemoteDirView)
  4623. {
  4624. DirViewColumnMenu = NonVisualDataModule->RemoteDirViewColumnPopup;
  4625. NonVisualDataModule->RemoteSortByExtColumnPopupItem->Visible =
  4626. (Column->Index == uvName);
  4627. NonVisualDataModule->RemoteFormatSizeBytesPopupItem->Visible =
  4628. (Column->Index == uvSize);
  4629. }
  4630. else
  4631. {
  4632. DirViewColumnMenu = NonVisualDataModule->LocalDirViewColumnPopup;
  4633. NonVisualDataModule->LocalSortByExtColumnPopupItem->Visible =
  4634. (Column->Index == dvName);
  4635. NonVisualDataModule->LocalFormatSizeBytesPopupItem->Visible =
  4636. (Column->Index == dvSize);
  4637. }
  4638. TCustomListView * ListView = DebugNotNull(dynamic_cast<TCustomListView *>(Sender));
  4639. TPoint ScreenPoint = ListView->ClientToScreen(Point);
  4640. ScreenPoint.x -= GetScrollPos(ListView->Handle, SB_HORZ);
  4641. DirViewColumnMenu->Popup(ScreenPoint.x, ScreenPoint.y);
  4642. }
  4643. //---------------------------------------------------------------------------
  4644. void __fastcall TCustomScpExplorerForm::DirViewExecFile(
  4645. TObject *Sender, TListItem *Item, bool &AllowExec)
  4646. {
  4647. DoDirViewExecFile(Sender, Item, AllowExec);
  4648. }
  4649. //---------------------------------------------------------------------------
  4650. void __fastcall TCustomScpExplorerForm::DoDirViewExecFile(TObject * Sender,
  4651. TListItem * Item, bool & AllowExec)
  4652. {
  4653. DebugAssert(Sender && Item && Configuration);
  4654. DebugAssert(AllowExec);
  4655. TCustomDirView * ADirView = (TCustomDirView *)Sender;
  4656. bool Remote = (ADirView == DirView(osRemote));
  4657. bool ResolvedSymlinks = !Remote || Terminal->ResolvingSymlinks;
  4658. TOperationSide Side = (Remote ? osRemote : osLocal);
  4659. // Anything special is done on double click only (not on "open" indicated by FForceExecution),
  4660. // on files only (not directories)
  4661. // and only when symlinks are resolved (apply to remote panel only)
  4662. if (!ADirView->ItemIsDirectory(Item) &&
  4663. (ResolvedSymlinks || FForceExecution || Terminal->IsEncryptingFiles()))
  4664. {
  4665. if ((WinConfiguration->DoubleClickAction != dcaOpen) &&
  4666. !FForceExecution &&
  4667. (ResolvedSymlinks || Terminal->IsEncryptingFiles()))
  4668. {
  4669. if (WinConfiguration->DoubleClickAction == dcaCopy)
  4670. {
  4671. // To counter the lock in DirViewBusy (called from TCustomDirView.DoExecute)
  4672. UnlockWindow();
  4673. try
  4674. {
  4675. ExecuteFileOperation(
  4676. foCopy, Side, true, !WinConfiguration->CopyOnDoubleClickConfirmation);
  4677. }
  4678. __finally
  4679. {
  4680. LockWindow();
  4681. }
  4682. AllowExec = false;
  4683. }
  4684. else if (WinConfiguration->DoubleClickAction == dcaEdit)
  4685. {
  4686. if (!Remote || !WinConfiguration->DisableOpenEdit)
  4687. {
  4688. ExecuteFile(Side, efDefaultEditor);
  4689. AllowExec = false;
  4690. }
  4691. }
  4692. else
  4693. {
  4694. DebugFail();
  4695. }
  4696. }
  4697. // if we have not done anything special, fall back to default behavior
  4698. if (AllowExec)
  4699. {
  4700. if (Remote && !WinConfiguration->DisableOpenEdit)
  4701. {
  4702. ExecuteFile(osRemote, efShell);
  4703. AllowExec = false;
  4704. }
  4705. }
  4706. }
  4707. }
  4708. //---------------------------------------------------------------------------
  4709. bool __fastcall TCustomScpExplorerForm::GetHasDirView(TOperationSide Side)
  4710. {
  4711. return ((Side == osRemote) || (Side == osCurrent));
  4712. }
  4713. //---------------------------------------------------------------------------
  4714. void __fastcall TCustomScpExplorerForm::CompareDirectories()
  4715. {
  4716. DebugFail();
  4717. }
  4718. //---------------------------------------------------------------------------
  4719. void __fastcall TCustomScpExplorerForm::SynchronizeDirectories()
  4720. {
  4721. DebugFail();
  4722. }
  4723. //---------------------------------------------------------------------------
  4724. bool __fastcall TCustomScpExplorerForm::DoSynchronizeDirectories(
  4725. UnicodeString & LocalDirectory, UnicodeString & RemoteDirectory, bool UseDefaults)
  4726. {
  4727. TSynchronizeParamType Params;
  4728. Params.LocalDirectory = LocalDirectory;
  4729. Params.RemoteDirectory = RemoteDirectory;
  4730. int UnusedParams =
  4731. (GUIConfiguration->SynchronizeParams &
  4732. (TTerminal::spPreviewChanges | TTerminal::spTimestamp | TTerminal::spNotByTime | TTerminal::spBySize));
  4733. Params.Params = GUIConfiguration->SynchronizeParams & ~UnusedParams;
  4734. Params.Options = GUIConfiguration->SynchronizeOptions;
  4735. bool SaveSettings = false;
  4736. TSynchronizeController Controller(&DoSynchronize, &DoSynchronizeInvalid,
  4737. &DoSynchronizeTooManyDirectories);
  4738. DebugAssert(FSynchronizeController == NULL);
  4739. FSynchronizeController = &Controller;
  4740. bool Result;
  4741. try
  4742. {
  4743. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  4744. int CopyParamAttrs = Terminal->UsableCopyParamAttrs(0).Upload;
  4745. int Options =
  4746. FLAGMASK(SynchronizeAllowSelectedOnly(), soAllowSelectedOnly);
  4747. DebugAssert(FOnFeedSynchronizeError == NULL);
  4748. Result = DoSynchronizeDialog(Params, &CopyParam, Controller.StartStop,
  4749. SaveSettings, Options, CopyParamAttrs, GetSynchronizeOptions, SynchronizeSessionLog,
  4750. FOnFeedSynchronizeError, SynchronizeInNewWindow, UseDefaults);
  4751. if (Result)
  4752. {
  4753. if (SaveSettings)
  4754. {
  4755. GUIConfiguration->SynchronizeParams = Params.Params | UnusedParams;
  4756. GUIConfiguration->SynchronizeOptions = Params.Options;
  4757. }
  4758. else
  4759. {
  4760. if (FLAGSET(GUIConfiguration->SynchronizeOptions, soSynchronizeAsk) &&
  4761. FLAGCLEAR(Params.Options, soSynchronizeAsk) &&
  4762. FLAGSET(Params.Options, soSynchronize))
  4763. {
  4764. GUIConfiguration->SynchronizeOptions =
  4765. (GUIConfiguration->SynchronizeOptions & ~soSynchronizeAsk) |
  4766. soSynchronize;
  4767. }
  4768. }
  4769. LocalDirectory = Params.LocalDirectory;
  4770. RemoteDirectory = Params.RemoteDirectory;
  4771. }
  4772. }
  4773. __finally
  4774. {
  4775. FSynchronizeController = NULL;
  4776. DebugAssert(FOnFeedSynchronizeError == NULL);
  4777. FOnFeedSynchronizeError = NULL;
  4778. }
  4779. return Result;
  4780. }
  4781. //---------------------------------------------------------------------------
  4782. void __fastcall TCustomScpExplorerForm::DoSynchronize(
  4783. TSynchronizeController * /*Sender*/, const UnicodeString LocalDirectory,
  4784. const UnicodeString RemoteDirectory, const TCopyParamType & CopyParam,
  4785. const TSynchronizeParamType & Params, TSynchronizeChecklist ** Checklist,
  4786. TSynchronizeOptions * Options, bool Full)
  4787. {
  4788. if (Terminal->Status == ssOpened)
  4789. {
  4790. try
  4791. {
  4792. int PParams = Params.Params;
  4793. if (!Full)
  4794. {
  4795. PParams |= TTerminal::spNoRecurse | TTerminal::spUseCache | TTerminal::spDelayProgress;
  4796. if (FLAGSET(Params.Options, soRecurse))
  4797. {
  4798. PParams |= TTerminal::spSubDirs;
  4799. }
  4800. }
  4801. else
  4802. {
  4803. // if keepuptodate is non-recursive,
  4804. // full sync before has to be non-recursive as well
  4805. if (FLAGCLEAR(Params.Options, soRecurse))
  4806. {
  4807. PParams |= TTerminal::spNoRecurse;
  4808. }
  4809. }
  4810. Synchronize(LocalDirectory, RemoteDirectory, smRemote, CopyParam,
  4811. PParams, Checklist, Options);
  4812. }
  4813. catch (Exception & E)
  4814. {
  4815. if (FLAGSET(Params.Options, soContinueOnError) && Terminal->Active &&
  4816. DebugAlwaysTrue(FOnFeedSynchronizeError != NULL))
  4817. {
  4818. // noop, already logged in MoreMessageDialog
  4819. }
  4820. else
  4821. {
  4822. ShowExtendedExceptionEx(Terminal, &E);
  4823. throw;
  4824. }
  4825. }
  4826. }
  4827. }
  4828. //---------------------------------------------------------------------------
  4829. void __fastcall TCustomScpExplorerForm::DoSynchronizeInvalid(
  4830. TSynchronizeController * /*Sender*/, const UnicodeString Directory,
  4831. const UnicodeString ErrorStr)
  4832. {
  4833. if (!Directory.IsEmpty())
  4834. {
  4835. SimpleErrorDialog(FMTLOAD(WATCH_ERROR_DIRECTORY, (Directory)), ErrorStr);
  4836. }
  4837. else
  4838. {
  4839. SimpleErrorDialog(LoadStr(WATCH_ERROR_GENERAL), ErrorStr);
  4840. }
  4841. }
  4842. //---------------------------------------------------------------------------
  4843. void __fastcall TCustomScpExplorerForm::DoSynchronizeTooManyDirectories(
  4844. TSynchronizeController * /*Sender*/, int & MaxDirectories)
  4845. {
  4846. if (MaxDirectories < GUIConfiguration->MaxWatchDirectories)
  4847. {
  4848. MaxDirectories = GUIConfiguration->MaxWatchDirectories;
  4849. }
  4850. else
  4851. {
  4852. TMessageParams Params(mpNeverAskAgainCheck);
  4853. unsigned int Result = MessageDialog(
  4854. FMTLOAD(TOO_MANY_WATCH_DIRECTORIES, (MaxDirectories, MaxDirectories)),
  4855. qtConfirmation, qaYes | qaNo, HELP_TOO_MANY_WATCH_DIRECTORIES, &Params);
  4856. if ((Result == qaYes) || (Result == qaNeverAskAgain))
  4857. {
  4858. MaxDirectories *= 2;
  4859. if (Result == qaNeverAskAgain)
  4860. {
  4861. GUIConfiguration->MaxWatchDirectories = MaxDirectories;
  4862. }
  4863. }
  4864. else
  4865. {
  4866. Abort();
  4867. }
  4868. }
  4869. }
  4870. //---------------------------------------------------------------------------
  4871. void __fastcall TCustomScpExplorerForm::Synchronize(const UnicodeString LocalDirectory,
  4872. const UnicodeString RemoteDirectory, TSynchronizeMode Mode,
  4873. const TCopyParamType & CopyParam, int Params, TSynchronizeChecklist ** Checklist,
  4874. TSynchronizeOptions * Options)
  4875. {
  4876. DebugAssert(!FAutoOperation);
  4877. void * BatchStorage;
  4878. BatchStart(BatchStorage);
  4879. FAutoOperation = true;
  4880. bool AnyOperation = false;
  4881. TDateTime StartTime = Now();
  4882. TSynchronizeChecklist * AChecklist = NULL;
  4883. try
  4884. {
  4885. FSynchronizeProgressForm = new TSynchronizeProgressForm(Application, true, true);
  4886. if (FLAGCLEAR(Params, TTerminal::spDelayProgress))
  4887. {
  4888. FSynchronizeProgressForm->Start();
  4889. }
  4890. AChecklist = Terminal->SynchronizeCollect(LocalDirectory, RemoteDirectory,
  4891. static_cast<TTerminal::TSynchronizeMode>(Mode),
  4892. &CopyParam, Params | TTerminal::spNoConfirmation, TerminalSynchronizeDirectory,
  4893. Options);
  4894. SAFE_DESTROY(FSynchronizeProgressForm);
  4895. AnyOperation = (AChecklist->CheckedCount > 0);
  4896. if (AnyOperation)
  4897. {
  4898. CreateProgressForm(AChecklist);
  4899. try
  4900. {
  4901. Terminal->SynchronizeApply(AChecklist, &CopyParam, Params | TTerminal::spNoConfirmation, TerminalSynchronizeDirectory, NULL, NULL);
  4902. }
  4903. __finally
  4904. {
  4905. DestroyProgressForm();
  4906. }
  4907. }
  4908. }
  4909. __finally
  4910. {
  4911. if (Checklist == NULL)
  4912. {
  4913. delete AChecklist;
  4914. }
  4915. else
  4916. {
  4917. *Checklist = AChecklist;
  4918. }
  4919. FAutoOperation = false;
  4920. SAFE_DESTROY(FSynchronizeProgressForm);
  4921. BatchEnd(BatchStorage);
  4922. ReloadLocalDirectory();
  4923. if (AnyOperation)
  4924. {
  4925. OperationComplete(StartTime);
  4926. }
  4927. }
  4928. }
  4929. //---------------------------------------------------------------------------
  4930. bool __fastcall TCustomScpExplorerForm::SynchronizeAllowSelectedOnly()
  4931. {
  4932. // can be called from command line
  4933. return Visible &&
  4934. ((DirView(osRemote)->SelCount > 0) ||
  4935. (HasDirView[osLocal] && (DirView(osLocal)->SelCount > 0)));
  4936. }
  4937. //---------------------------------------------------------------------------
  4938. void __fastcall TCustomScpExplorerForm::SynchronizeSessionLog(const UnicodeString & Message)
  4939. {
  4940. LogSynchronizeEvent(Terminal, Message);
  4941. }
  4942. //---------------------------------------------------------------------------
  4943. void __fastcall TCustomScpExplorerForm::GetSynchronizeOptions(
  4944. int Params, TSynchronizeOptions & Options)
  4945. {
  4946. if (FLAGSET(Params, TTerminal::spSelectedOnly) && SynchronizeAllowSelectedOnly())
  4947. {
  4948. Options.Filter = new TStringList();
  4949. Options.Filter->CaseSensitive = false;
  4950. Options.Filter->Duplicates = Types::dupAccept;
  4951. if (DirView(osRemote)->SelCount > 0)
  4952. {
  4953. DirView(osRemote)->CreateFileList(false, false, Options.Filter);
  4954. }
  4955. if (HasDirView[osLocal] && (DirView(osLocal)->SelCount > 0))
  4956. {
  4957. DirView(osLocal)->CreateFileList(false, false, Options.Filter);
  4958. }
  4959. Options.Filter->Sort();
  4960. }
  4961. }
  4962. //---------------------------------------------------------------------------
  4963. void __fastcall TCustomScpExplorerForm::SynchronizeInNewWindow(
  4964. const TSynchronizeParamType & Params, const TCopyParamType * CopyParams)
  4965. {
  4966. UnicodeString SessionName = CreateHiddenDuplicateSession();
  4967. UnicodeString AdditionalParams =
  4968. FORMAT(L"%s \"%s\" \"%s\" %d %d %s", (
  4969. TProgramParams::FormatSwitch(KEEP_UP_TO_DATE_SWITCH),
  4970. Params.LocalDirectory, Params.RemoteDirectory, Params.Params, Params.Options,
  4971. TProgramParams::FormatSwitch(DEFAULTS_SWITCH)));
  4972. TCopyParamType Defaults;
  4973. std::unique_ptr<THierarchicalStorage> ConfigStorage(Configuration->CreateConfigStorage());
  4974. ConfigStorage->AccessMode = smRead;
  4975. if (ConfigStorage->OpenSubKey(Configuration->ConfigurationSubKey, false, false))
  4976. {
  4977. GUIConfiguration->LoadCopyParam(ConfigStorage.get(), &Defaults);
  4978. }
  4979. ConfigStorage.reset(NULL);
  4980. std::unique_ptr<TStringList> Options(new TStringList());
  4981. std::unique_ptr<TOptionsStorage> OptionsStorage(new TOptionsStorage(Options.get(), true));
  4982. GUIConfiguration->SaveCopyParam(OptionsStorage.get(), CopyParams, &Defaults);
  4983. if (Options->Count > 0)
  4984. {
  4985. AdditionalParams +=
  4986. FORMAT(L" %s%s", (TProgramParams::FormatSwitch(RAW_CONFIG_SWITCH), StringsToParams(Options.get())));
  4987. }
  4988. ExecuteNewInstance(SessionName, AdditionalParams);
  4989. }
  4990. //---------------------------------------------------------------------------
  4991. struct TSynchronizeParams
  4992. {
  4993. UnicodeString LocalDirectory;
  4994. UnicodeString RemoteDirectory;
  4995. TSynchronizeMode Mode;
  4996. TSynchronizeChecklist * Checklist;
  4997. int Params;
  4998. TCopyParamType * CopyParam;
  4999. TDateTime * StartTime;
  5000. };
  5001. //---------------------------------------------------------------------------
  5002. void __fastcall TCustomScpExplorerForm::FullSynchronize(
  5003. TSynchronizeParams & Params, TProcessedSynchronizationChecklistItem OnProcessedItem,
  5004. TUpdatedSynchronizationChecklistItems OnUpdatedSynchronizationChecklistItems)
  5005. {
  5006. DebugAssert(!FAutoOperation);
  5007. void * BatchStorage;
  5008. BatchStart(BatchStorage);
  5009. FAutoOperation = true;
  5010. try
  5011. {
  5012. CreateProgressForm(Params.Checklist);
  5013. Terminal->SynchronizeApply(
  5014. Params.Checklist, Params.CopyParam, Params.Params | TTerminal::spNoConfirmation,
  5015. TerminalSynchronizeDirectory, OnProcessedItem, OnUpdatedSynchronizationChecklistItems);
  5016. }
  5017. __finally
  5018. {
  5019. FAutoOperation = false;
  5020. DestroyProgressForm();
  5021. BatchEnd(BatchStorage);
  5022. ReloadLocalDirectory();
  5023. }
  5024. }
  5025. //---------------------------------------------------------------------------
  5026. void __fastcall TCustomScpExplorerForm::DoFullSynchronize(
  5027. void * Token, TProcessedSynchronizationChecklistItem OnProcessedItem,
  5028. TUpdatedSynchronizationChecklistItems OnUpdatedSynchronizationChecklistItems)
  5029. {
  5030. TSynchronizeParams & Params = *static_cast<TSynchronizeParams *>(Token);
  5031. *Params.StartTime = Now();
  5032. FullSynchronize(Params, OnProcessedItem, OnUpdatedSynchronizationChecklistItems);
  5033. }
  5034. //---------------------------------------------------------------------------
  5035. void __fastcall TCustomScpExplorerForm::DoSynchronizeChecklistCalculateSize(
  5036. TSynchronizeChecklist * Checklist, const TSynchronizeChecklist::TItemList & Items, void * Token)
  5037. {
  5038. // terminal can be already closed (e.g. dropped connection)
  5039. if (Terminal != NULL)
  5040. {
  5041. TSynchronizeParams & Params = *static_cast<TSynchronizeParams *>(Token);
  5042. Terminal->SynchronizeChecklistCalculateSize(Checklist, Items, Params.CopyParam);
  5043. }
  5044. }
  5045. //---------------------------------------------------------------------------
  5046. void __fastcall TCustomScpExplorerForm::SynchronizeNoDifferences()
  5047. {
  5048. UnicodeString Message = MainInstructions(LoadStr(COMPARE_NO_DIFFERENCES));
  5049. MessageDialog(Message, qtInformation, qaOK, HELP_SYNCHRONIZE_NO_DIFFERENCES);
  5050. }
  5051. //---------------------------------------------------------------------------
  5052. bool __fastcall TCustomScpExplorerForm::DoFullSynchronizeDirectories(
  5053. UnicodeString & LocalDirectory, UnicodeString & RemoteDirectory,
  5054. TSynchronizeMode & Mode, bool & SaveMode, bool UseDefaults)
  5055. {
  5056. bool Result;
  5057. int Params = GUIConfiguration->SynchronizeParams;
  5058. bool SaveSettings = false;
  5059. int Options =
  5060. FLAGMASK(!Terminal->IsCapable[fcTimestampChanging], fsoDisableTimestamp) |
  5061. FLAGMASK(SynchronizeAllowSelectedOnly(), fsoAllowSelectedOnly);
  5062. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  5063. TUsableCopyParamAttrs CopyParamAttrs = Terminal->UsableCopyParamAttrs(0);
  5064. Result = UseDefaults ||
  5065. DoFullSynchronizeDialog(Mode, Params, LocalDirectory, RemoteDirectory,
  5066. &CopyParam, SaveSettings, SaveMode, Options, CopyParamAttrs);
  5067. if (Result)
  5068. {
  5069. Configuration->Usage->Inc(L"Synchronizations");
  5070. UpdateCopyParamCounters(CopyParam);
  5071. TSynchronizeOptions SynchronizeOptions;
  5072. GetSynchronizeOptions(Params, SynchronizeOptions);
  5073. if (SaveSettings)
  5074. {
  5075. GUIConfiguration->SynchronizeParams = Params;
  5076. }
  5077. else
  5078. {
  5079. SaveMode = false;
  5080. }
  5081. TDateTime StartTime = Now();
  5082. TSynchronizeChecklist * Checklist = NULL;
  5083. try
  5084. {
  5085. DebugAssert(!FAutoOperation);
  5086. FAutoOperation = true;
  5087. try
  5088. {
  5089. FSynchronizeProgressForm = new TSynchronizeProgressForm(Application, true, true);
  5090. FSynchronizeProgressForm->Start();
  5091. Checklist = Terminal->SynchronizeCollect(LocalDirectory, RemoteDirectory,
  5092. static_cast<TTerminal::TSynchronizeMode>(Mode),
  5093. &CopyParam, Params | TTerminal::spNoConfirmation, TerminalSynchronizeDirectory,
  5094. &SynchronizeOptions);
  5095. }
  5096. __finally
  5097. {
  5098. FAutoOperation = false;
  5099. SAFE_DESTROY(FSynchronizeProgressForm);
  5100. }
  5101. TSynchronizeParams SynchronizeParams;
  5102. SynchronizeParams.LocalDirectory = LocalDirectory;
  5103. SynchronizeParams.RemoteDirectory = RemoteDirectory;
  5104. SynchronizeParams.Mode = Mode;
  5105. SynchronizeParams.CopyParam = &CopyParam;
  5106. SynchronizeParams.Params = Params;
  5107. SynchronizeParams.Checklist = Checklist;
  5108. SynchronizeParams.StartTime = &StartTime;
  5109. if (FLAGSET(Params, TTerminal::spPreviewChanges))
  5110. {
  5111. if (Checklist->Count > 0)
  5112. {
  5113. Result =
  5114. DoSynchronizeChecklistDialog(
  5115. Checklist, Mode, Params, LocalDirectory, RemoteDirectory, CustomCommandMenu, DoFullSynchronize,
  5116. DoSynchronizeChecklistCalculateSize, &SynchronizeParams);
  5117. }
  5118. else
  5119. {
  5120. SynchronizeNoDifferences();
  5121. }
  5122. }
  5123. else
  5124. {
  5125. if (Checklist->CheckedCount > 0)
  5126. {
  5127. FullSynchronize(SynchronizeParams, NULL, NULL);
  5128. }
  5129. else
  5130. {
  5131. SynchronizeNoDifferences();
  5132. }
  5133. }
  5134. }
  5135. __finally
  5136. {
  5137. delete Checklist;
  5138. }
  5139. OperationComplete(StartTime);
  5140. }
  5141. return Result;
  5142. }
  5143. //---------------------------------------------------------------------------
  5144. void __fastcall TCustomScpExplorerForm::TerminalSynchronizeDirectory(
  5145. const UnicodeString LocalDirectory, const UnicodeString RemoteDirectory,
  5146. bool & Continue, bool Collect)
  5147. {
  5148. if (Collect)
  5149. {
  5150. DebugAssert(FSynchronizeProgressForm != NULL);
  5151. if (!FSynchronizeProgressForm->Started)
  5152. {
  5153. FSynchronizeProgressForm->Start();
  5154. }
  5155. FSynchronizeProgressForm->SetData(LocalDirectory, RemoteDirectory, Continue);
  5156. }
  5157. else
  5158. {
  5159. DebugAssert(FSynchronizeProgressForm == NULL);
  5160. DebugAssert(FProgressForm != NULL);
  5161. }
  5162. }
  5163. //---------------------------------------------------------------------------
  5164. void __fastcall TCustomScpExplorerForm::StandaloneEdit(const UnicodeString & FileName)
  5165. {
  5166. UnicodeString FullFileName = AbsolutePath(FTerminal->CurrentDirectory, FileName);
  5167. TRemoteFile * File = NULL;
  5168. Terminal->ReadFile(FullFileName, File);
  5169. if (File != NULL)
  5170. {
  5171. std::unique_ptr<TRemoteFile> FileOwner(File);
  5172. TAutoFlag Flag(FStandaloneEditing);
  5173. ExecuteRemoteFile(FullFileName, File, efInternalEditor);
  5174. Application->ShowMainForm = false;
  5175. Application->Run();
  5176. }
  5177. }
  5178. //---------------------------------------------------------------------------
  5179. void __fastcall TCustomScpExplorerForm::ExploreLocalDirectory()
  5180. {
  5181. DebugFail();
  5182. }
  5183. //---------------------------------------------------------------------------
  5184. TSessionData * __fastcall TCustomScpExplorerForm::CloneCurrentSessionData()
  5185. {
  5186. std::unique_ptr<TSessionData> SessionData(new TSessionData(L""));
  5187. SessionData->Assign(Terminal->SessionData);
  5188. UpdateSessionData(SessionData.get());
  5189. TTerminalManager::Instance()->UpdateSessionCredentials(SessionData.get());
  5190. if (Terminal->SessionData->IsWorkspace)
  5191. {
  5192. // Have to reset the "Workspace/XXX" name which would become user-visible
  5193. // once IsWorkspace is cleared
  5194. SessionData->Name = UnicodeString();
  5195. SessionData->IsWorkspace = false;
  5196. }
  5197. return SessionData.release();
  5198. }
  5199. //---------------------------------------------------------------------------
  5200. void __fastcall TCustomScpExplorerForm::SaveCurrentSession()
  5201. {
  5202. TSessionData * SessionData = CloneCurrentSessionData();
  5203. try
  5204. {
  5205. TSessionData * EditingSessionData = StoredSessions->FindSame(SessionData);
  5206. DoSaveSession(SessionData, EditingSessionData, true, NULL);
  5207. }
  5208. __finally
  5209. {
  5210. delete SessionData;
  5211. }
  5212. }
  5213. //---------------------------------------------------------------------------
  5214. TObjectList * __fastcall TCustomScpExplorerForm::DoCollectWorkspace()
  5215. {
  5216. TTerminalManager * Manager = TTerminalManager::Instance();
  5217. std::unique_ptr<TObjectList> DataList(new TObjectList());
  5218. if (DebugAlwaysTrue(Terminal != NULL))
  5219. {
  5220. // Update (Managed)Terminal->StateData
  5221. UpdateTerminal(Terminal);
  5222. }
  5223. Manager->SaveWorkspace(DataList.get());
  5224. return DataList.release();
  5225. }
  5226. //---------------------------------------------------------------------------
  5227. void __fastcall TCustomScpExplorerForm::DoSaveWorkspace(
  5228. const UnicodeString & Name, TObjectList * DataList, bool SavePasswords, bool Explicit)
  5229. {
  5230. WinConfiguration->LastWorkspace = Name;
  5231. if (!SavePasswords)
  5232. {
  5233. for (int Index = 0; Index < DataList->Count; Index++)
  5234. {
  5235. TSessionData * SessionData = dynamic_cast<TSessionData *>(DataList->Items[Index]);
  5236. if (SessionData->Link.IsEmpty())
  5237. {
  5238. SessionData->ClearSessionPasswords();
  5239. }
  5240. }
  5241. }
  5242. StoredSessions->NewWorkspace(Name, DataList);
  5243. // modified only
  5244. StoredSessions->Save(false, Explicit);
  5245. }
  5246. //---------------------------------------------------------------------------
  5247. UnicodeString __fastcall TCustomScpExplorerForm::WorkspaceName()
  5248. {
  5249. return
  5250. DefaultStr(WinConfiguration->LastWorkspace,
  5251. DefaultStr(WinConfiguration->AutoWorkspace, LoadStr(NEW_WORKSPACE)));
  5252. }
  5253. //---------------------------------------------------------------------------
  5254. bool __fastcall TCustomScpExplorerForm::SaveWorkspace(bool EnableAutoSave)
  5255. {
  5256. std::unique_ptr<TObjectList> DataList(DoCollectWorkspace());
  5257. bool AnyNonStoredSessionWithPassword = false;
  5258. bool AnyNonStoredNonWorkspaceSessionWithPassword = false;
  5259. bool AllNonStoredSessionsAnonymous = true;
  5260. TTerminalManager * Manager = TTerminalManager::Instance();
  5261. for (int Index = 0; Index < DataList->Count; Index++)
  5262. {
  5263. TSessionData * SessionData = dynamic_cast<TSessionData *>(DataList->Items[Index]);
  5264. if (SessionData->Link.IsEmpty())
  5265. {
  5266. if (SessionData->HasAnySessionPassword())
  5267. {
  5268. AnyNonStoredSessionWithPassword = true;
  5269. if (!Manager->Terminals[Index]->SessionData->IsWorkspace)
  5270. {
  5271. AnyNonStoredNonWorkspaceSessionWithPassword = true;
  5272. }
  5273. }
  5274. if (!SameText(SessionData->UserName, AnonymousUserName))
  5275. {
  5276. AllNonStoredSessionsAnonymous = false;
  5277. }
  5278. }
  5279. }
  5280. bool SavePasswords;
  5281. bool * PSavePasswords;
  5282. bool NotRecommendedSavingPasswords =
  5283. !CustomWinConfiguration->UseMasterPassword &&
  5284. !AllNonStoredSessionsAnonymous;
  5285. if (Configuration->DisablePasswordStoring ||
  5286. !AnyNonStoredSessionWithPassword)
  5287. {
  5288. PSavePasswords = NULL;
  5289. SavePasswords = false;
  5290. }
  5291. else
  5292. {
  5293. PSavePasswords = &SavePasswords;
  5294. SavePasswords =
  5295. !AnyNonStoredNonWorkspaceSessionWithPassword ||
  5296. !NotRecommendedSavingPasswords;
  5297. }
  5298. UnicodeString Name = WorkspaceName();
  5299. bool CreateShortcut = false;
  5300. bool Result =
  5301. DoSaveWorkspaceDialog(
  5302. Name, PSavePasswords, NotRecommendedSavingPasswords, CreateShortcut,
  5303. EnableAutoSave);
  5304. if (Result)
  5305. {
  5306. DoSaveWorkspace(Name, DataList.get(), SavePasswords, true);
  5307. if (CreateShortcut)
  5308. {
  5309. TOperationVisualizer Visualizer;
  5310. UnicodeString AdditionalParams = TProgramParams::FormatSwitch(DESKTOP_SWITCH);
  5311. CreateDesktopSessionShortCut(Name, L"", AdditionalParams, -1, WORKSPACE_ICON);
  5312. }
  5313. if (EnableAutoSave)
  5314. {
  5315. WinConfiguration->AutoSaveWorkspace = true;
  5316. WinConfiguration->AutoWorkspace = Name;
  5317. if (PSavePasswords != NULL)
  5318. {
  5319. WinConfiguration->AutoSaveWorkspacePasswords = SavePasswords;
  5320. }
  5321. }
  5322. }
  5323. return Result;
  5324. }
  5325. //---------------------------------------------------------------------------
  5326. void __fastcall TCustomScpExplorerForm::UpdateTerminal(TTerminal * Terminal)
  5327. {
  5328. TManagedTerminal * ManagedTerminal = dynamic_cast<TManagedTerminal *>(Terminal);
  5329. DebugAssert(ManagedTerminal != NULL);
  5330. SAFE_DESTROY(ManagedTerminal->RemoteExplorerState);
  5331. if (WinConfiguration->PreservePanelState)
  5332. {
  5333. ManagedTerminal->RemoteExplorerState = RemoteDirView->SaveState();
  5334. }
  5335. UpdateSessionData(ManagedTerminal->StateData);
  5336. }
  5337. //---------------------------------------------------------------------------
  5338. void __fastcall TCustomScpExplorerForm::UpdateSessionData(TSessionData * Data)
  5339. {
  5340. // Keep in sync with TSessionData::CopyStateData
  5341. DebugAssert(Data != NULL);
  5342. // cannot use RemoteDirView->Path, because it is empty if connection
  5343. // was already closed
  5344. // also only peek, we may not be connected at all atm
  5345. // so make sure we do not try retrieving current directory from the server
  5346. // (particularly with FTP)
  5347. UnicodeString ACurrentDirectory = Terminal->PeekCurrentDirectory();
  5348. if (!ACurrentDirectory.IsEmpty())
  5349. {
  5350. Data->RemoteDirectory = ACurrentDirectory;
  5351. }
  5352. Data->Color = SessionColor;
  5353. }
  5354. //---------------------------------------------------------------------------
  5355. void __fastcall TCustomScpExplorerForm::ToolBarResize(TObject *Sender)
  5356. {
  5357. TTBXToolbar * Toolbar = dynamic_cast<TTBXToolbar*>(Sender);
  5358. DebugAssert(Toolbar != NULL);
  5359. for (int i = 0; i < Toolbar->Items->Count; i++)
  5360. {
  5361. TTBXCustomDropDownItem * DropDownItem;
  5362. DropDownItem = dynamic_cast<TTBXCustomDropDownItem *>(Toolbar->Items->Items[i]);
  5363. if (DropDownItem != NULL)
  5364. {
  5365. ToolbarItemResize(DropDownItem,
  5366. Toolbar->Width - (Toolbar->View->BaseSize.x - DropDownItem->EditWidth) -
  5367. Toolbar->NonClientWidth);
  5368. break;
  5369. }
  5370. }
  5371. }
  5372. //---------------------------------------------------------------------------
  5373. void __fastcall TCustomScpExplorerForm::ToolbarItemResize(TTBXCustomDropDownItem * Item, int Width)
  5374. {
  5375. Item->EditWidth = Width;
  5376. }
  5377. //---------------------------------------------------------------------------
  5378. void __fastcall TCustomScpExplorerForm::ToolbarGetBaseSize(
  5379. TTBCustomToolbar * Toolbar, TPoint & ASize)
  5380. {
  5381. for (int i = 0; i < Toolbar->Items->Count; i++)
  5382. {
  5383. TTBXCustomDropDownItem * DropDownItem;
  5384. DropDownItem = dynamic_cast<TTBXCustomDropDownItem *>(Toolbar->Items->Items[i]);
  5385. if (DropDownItem != NULL)
  5386. {
  5387. ASize.x -= DropDownItem->EditWidth;
  5388. ASize.x += ScaleByTextHeight(this, 50) /* minimal combo width */;
  5389. }
  5390. }
  5391. }
  5392. //---------------------------------------------------------------------------
  5393. void __fastcall TCustomScpExplorerForm::DoWarnLackOfTempSpace(
  5394. const UnicodeString Path, __int64 RequiredSpace, bool & Continue)
  5395. {
  5396. if (WinConfiguration->DDWarnLackOfTempSpace)
  5397. {
  5398. UnicodeString ADrive = ExtractFileDrive(ExpandFileName(Path));
  5399. if (!ADrive.IsEmpty())
  5400. {
  5401. __int64 FreeSpace = DiskFree((Byte)(ADrive[1]-'A'+1));
  5402. DebugAssert(RequiredSpace >= 0);
  5403. __int64 RequiredWithReserve;
  5404. RequiredWithReserve = (__int64)(RequiredSpace * WinConfiguration->DDWarnLackOfTempSpaceRatio);
  5405. if (FreeSpace < RequiredWithReserve)
  5406. {
  5407. unsigned int Result;
  5408. TMessageParams Params(mpNeverAskAgainCheck);
  5409. Result = MessageDialog(FMTLOAD(DD_WARN_LACK_OF_TEMP_SPACE, (Path,
  5410. FormatBytes(FreeSpace), FormatBytes(RequiredSpace))),
  5411. qtWarning, qaYes | qaNo, HELP_DD_WARN_LACK_OF_TEMP_SPACE, &Params);
  5412. if (Result == qaNeverAskAgain)
  5413. {
  5414. WinConfiguration->DDWarnLackOfTempSpace = false;
  5415. }
  5416. Continue = (Result == qaYes || Result == qaNeverAskAgain);
  5417. }
  5418. }
  5419. }
  5420. }
  5421. //---------------------------------------------------------------------------
  5422. void __fastcall TCustomScpExplorerForm::AddBookmark(TOperationSide Side)
  5423. {
  5424. DoOpenDirectoryDialog(odAddBookmark, Side);
  5425. }
  5426. //---------------------------------------------------------------------------
  5427. TStrings * __fastcall TCustomScpExplorerForm::CreateVisitedDirectories(TOperationSide Side)
  5428. {
  5429. // we should better use TCustomDirView::FCaseSensitive, but it is private
  5430. TStringList * VisitedDirectories = CreateSortedStringList((Side == osRemote));
  5431. try
  5432. {
  5433. TCustomDirView * DView = DirView(Side);
  5434. for (int Index = -DView->BackCount; Index <= DView->ForwardCount; Index++)
  5435. {
  5436. VisitedDirectories->Add(DView->HistoryPath[Index]);
  5437. }
  5438. }
  5439. catch (...)
  5440. {
  5441. delete VisitedDirectories;
  5442. throw;
  5443. }
  5444. return VisitedDirectories;
  5445. }
  5446. //---------------------------------------------------------------------------
  5447. void __fastcall TCustomScpExplorerForm::DoOpenDirectoryDialog(
  5448. TOpenDirectoryMode Mode, TOperationSide Side)
  5449. {
  5450. bool Continue = true;
  5451. if (Mode == odAddBookmark)
  5452. {
  5453. TMessageParams Params(mpNeverAskAgainCheck);
  5454. Params.NeverAskAgainTitle = LoadStr(ADD_BOOKMARK_SHARED);
  5455. Params.NeverAskAgainCheckedInitially = WinConfiguration->UseSharedBookmarks;
  5456. UnicodeString Message = MainInstructions(FMTLOAD(ADD_BOOKMARK_CONFIRM, (DirView(Side)->PathName)));
  5457. unsigned int Answer =
  5458. MessageDialog(Message,
  5459. qtConfirmation, qaOK | qaCancel, HELP_ADD_BOOKMARK_CONFIRM, &Params);
  5460. if (Answer == qaNeverAskAgain)
  5461. {
  5462. Continue = true;
  5463. WinConfiguration->UseSharedBookmarks = true;
  5464. }
  5465. else if (Answer == qaOK)
  5466. {
  5467. Continue = true;
  5468. WinConfiguration->UseSharedBookmarks = false;
  5469. }
  5470. else
  5471. {
  5472. Continue = false;
  5473. }
  5474. }
  5475. if (Continue)
  5476. {
  5477. TStrings * VisitedDirectories = CreateVisitedDirectories(Side);
  5478. try
  5479. {
  5480. UnicodeString Name = DirView(Side)->PathName;
  5481. if (::DoOpenDirectoryDialog(Mode, Side, Name, VisitedDirectories, Terminal,
  5482. // do not allow switching to location profiles,
  5483. // if we are not connected
  5484. HasDirView[osLocal] && (Terminal != NULL)))
  5485. {
  5486. TWindowLock Lock(this);
  5487. DirView(Side)->Path = Name;
  5488. }
  5489. }
  5490. __finally
  5491. {
  5492. delete VisitedDirectories;
  5493. }
  5494. }
  5495. }
  5496. //---------------------------------------------------------------------------
  5497. bool __fastcall TCustomScpExplorerForm::CommandSessionFallback()
  5498. {
  5499. DebugAssert(!FTerminal->CommandSessionOpened);
  5500. return TTerminalManager::Instance()->ConnectTerminal(FTerminal->CommandSession);
  5501. }
  5502. //---------------------------------------------------------------------------
  5503. bool __fastcall TCustomScpExplorerForm::EnsureCommandSessionFallback(TFSCapability Capability)
  5504. {
  5505. bool Result = FTerminal->IsCapable[Capability] ||
  5506. FTerminal->CommandSessionOpened;
  5507. if (!Result)
  5508. {
  5509. DebugAssert(FTerminal->IsCapable[fcSecondaryShell]);
  5510. if (!GUIConfiguration->ConfirmCommandSession)
  5511. {
  5512. Result = true;
  5513. }
  5514. else
  5515. {
  5516. TMessageParams Params(mpNeverAskAgainCheck);
  5517. const TFileSystemInfo & FileSystemInfo = Terminal->GetFileSystemInfo();
  5518. unsigned int Answer = MessageDialog(FMTLOAD(PERFORM_ON_COMMAND_SESSION2,
  5519. (FileSystemInfo.ProtocolName, FileSystemInfo.ProtocolName)), qtConfirmation,
  5520. qaOK | qaCancel, HELP_PERFORM_ON_COMMAND_SESSION, &Params);
  5521. if (Answer == qaNeverAskAgain)
  5522. {
  5523. GUIConfiguration->ConfirmCommandSession = false;
  5524. Result = true;
  5525. }
  5526. else if (Answer == qaOK)
  5527. {
  5528. Result = true;
  5529. }
  5530. }
  5531. if (Result)
  5532. {
  5533. Result = CommandSessionFallback();
  5534. }
  5535. }
  5536. return Result;
  5537. }
  5538. //---------------------------------------------------------------------------
  5539. void __fastcall TCustomScpExplorerForm::OpenConsole(UnicodeString Command)
  5540. {
  5541. if (EnsureCommandSessionFallback(fcAnyCommand))
  5542. {
  5543. DoConsoleDialog(Terminal, Command);
  5544. }
  5545. }
  5546. //---------------------------------------------------------------------------
  5547. void __fastcall TCustomScpExplorerForm::FileControlDDDragEnter(
  5548. TObject *Sender, _di_IDataObject /*DataObj*/, int /*grfKeyState*/,
  5549. const TPoint & /*Point*/, int & /*dwEffect*/, bool & Accept)
  5550. {
  5551. if (IsFileControl(DropSourceControl, osRemote) &&
  5552. (FDDExtMapFile != NULL))
  5553. {
  5554. Accept = true;
  5555. }
  5556. FDDTargetControl = dynamic_cast<TControl*>(Sender);
  5557. DebugAssert(FDDTargetControl != NULL);
  5558. }
  5559. //---------------------------------------------------------------------------
  5560. void __fastcall TCustomScpExplorerForm::SessionsDDDragEnter(
  5561. _di_IDataObject DataObj, int KeyState,
  5562. const TPoint & Point, int & Effect, bool & Accept)
  5563. {
  5564. FileControlDDDragEnter(SessionsPageControl, DataObj, KeyState, Point, Effect, Accept);
  5565. }
  5566. //---------------------------------------------------------------------------
  5567. void __fastcall TCustomScpExplorerForm::QueueDDDragEnter(
  5568. _di_IDataObject DataObj, int KeyState,
  5569. const TPoint & Point, int & Effect, bool & Accept)
  5570. {
  5571. FileControlDDDragEnter(QueueView3, DataObj, KeyState, Point, Effect, Accept);
  5572. }
  5573. //---------------------------------------------------------------------------
  5574. void __fastcall TCustomScpExplorerForm::FileControlDDDragLeave(
  5575. TObject * Sender)
  5576. {
  5577. DebugUsedParam(Sender);
  5578. DebugAssert(FDDTargetControl == Sender);
  5579. FDDTargetControl = NULL;
  5580. }
  5581. //---------------------------------------------------------------------------
  5582. void __fastcall TCustomScpExplorerForm::SessionsDDDragLeave()
  5583. {
  5584. FileControlDDDragLeave(SessionsPageControl);
  5585. }
  5586. //---------------------------------------------------------------------------
  5587. void __fastcall TCustomScpExplorerForm::QueueDDDragLeave()
  5588. {
  5589. FileControlDDDragLeave(QueueView3);
  5590. }
  5591. //---------------------------------------------------------------------------
  5592. void __fastcall TCustomScpExplorerForm::AddEditLink(TOperationSide Side, bool Add)
  5593. {
  5594. DebugAssert(GetSide(Side) == osRemote);
  5595. DebugUsedParam(Side);
  5596. bool Edit = false;
  5597. TRemoteFile * File = NULL;
  5598. UnicodeString FileName;
  5599. UnicodeString PointTo;
  5600. bool SymbolicLink = true;
  5601. if (RemoteDirView->ItemFocused)
  5602. {
  5603. DebugAssert(RemoteDirView->ItemFocused->Data);
  5604. File = (TRemoteFile *)RemoteDirView->ItemFocused->Data;
  5605. Edit = !Add && File->IsSymLink && Terminal->SessionData->ResolveSymlinks;
  5606. if (Edit)
  5607. {
  5608. FileName = File->FileName;
  5609. PointTo = File->LinkTo;
  5610. }
  5611. else
  5612. {
  5613. PointTo = File->FileName;
  5614. }
  5615. }
  5616. if (DoSymlinkDialog(FileName, PointTo, osRemote, SymbolicLink, Edit,
  5617. Terminal->IsCapable[fcHardLink]))
  5618. {
  5619. Configuration->Usage->Inc(L"RemoteLinksCreated");
  5620. if (Edit)
  5621. {
  5622. DebugAssert(File->FileName == FileName);
  5623. int Params = dfNoRecursive;
  5624. Terminal->ExceptionOnFail = true;
  5625. try
  5626. {
  5627. Terminal->DeleteFile(L"", File, &Params);
  5628. }
  5629. __finally
  5630. {
  5631. Terminal->ExceptionOnFail = false;
  5632. }
  5633. }
  5634. Terminal->CreateLink(FileName, PointTo, SymbolicLink);
  5635. }
  5636. }
  5637. //---------------------------------------------------------------------------
  5638. bool __fastcall TCustomScpExplorerForm::CanAddEditLink(TOperationSide Side)
  5639. {
  5640. return
  5641. (Terminal != NULL) &&
  5642. ((GetSide(Side) != osRemote) ||
  5643. (Terminal->ResolvingSymlinks &&
  5644. Terminal->IsCapable[fcSymbolicLink]));
  5645. }
  5646. //---------------------------------------------------------------------------
  5647. bool __fastcall TCustomScpExplorerForm::LinkFocused()
  5648. {
  5649. return
  5650. (FCurrentSide == osRemote) &&
  5651. (RemoteDirView->ItemFocused != NULL) &&
  5652. ((TRemoteFile *)RemoteDirView->ItemFocused->Data)->IsSymLink &&
  5653. Terminal->SessionData->ResolveSymlinks;
  5654. }
  5655. //---------------------------------------------------------------------------
  5656. void __fastcall TCustomScpExplorerForm::ExecuteCurrentFile()
  5657. {
  5658. DebugAssert(!WinConfiguration->DisableOpenEdit);
  5659. FForceExecution = true;
  5660. try
  5661. {
  5662. DirView(osCurrent)->ExecuteCurrentFile();
  5663. }
  5664. __finally
  5665. {
  5666. FForceExecution = false;
  5667. }
  5668. }
  5669. //---------------------------------------------------------------------------
  5670. void __fastcall TCustomScpExplorerForm::ExecuteCurrentFileWith(bool OnFocused)
  5671. {
  5672. TEditorData ExternalEditor;
  5673. ExternalEditor.Editor = edExternal;
  5674. bool Remember = false;
  5675. if (DoEditorPreferencesDialog(&ExternalEditor, Remember, epmAdHoc,
  5676. (GetSide(osCurrent) == osRemote)))
  5677. {
  5678. if (Remember)
  5679. {
  5680. TEditorList * EditorList = new TEditorList;
  5681. try
  5682. {
  5683. *EditorList = *WinConfiguration->EditorList;
  5684. bool Found = false;
  5685. int i = 0;
  5686. while (!Found && (i < EditorList->Count))
  5687. {
  5688. const TEditorPreferences * Editor = EditorList->Editors[i];
  5689. if ((Editor->Data->Editor == edExternal) &&
  5690. (Editor->Data->ExternalEditor == ExternalEditor.ExternalEditor))
  5691. {
  5692. Found = true;
  5693. }
  5694. i++;
  5695. }
  5696. if (!Found)
  5697. {
  5698. EditorList->Add(new TEditorPreferences(ExternalEditor));
  5699. WinConfiguration->EditorList = EditorList;
  5700. }
  5701. }
  5702. __finally
  5703. {
  5704. delete EditorList;
  5705. }
  5706. }
  5707. ExecuteFile(osCurrent, efExternalEditor, &ExternalEditor, true, OnFocused);
  5708. }
  5709. }
  5710. //---------------------------------------------------------------------------
  5711. void __fastcall TCustomScpExplorerForm::TerminalRemoved(TObject * Sender)
  5712. {
  5713. FEditorManager->ProcessFiles(FileTerminalRemoved, Sender);
  5714. if (FFileFindTerminal == Sender)
  5715. {
  5716. FFileFindTerminal = NULL;
  5717. HideFileFindDialog();
  5718. }
  5719. }
  5720. //---------------------------------------------------------------------------
  5721. void __fastcall TCustomScpExplorerForm::FileTerminalRemoved(const UnicodeString FileName,
  5722. TEditedFileData * Data, TObject * /*Token*/, void * Arg)
  5723. {
  5724. TTerminal * Terminal = static_cast<TTerminal *>(Arg);
  5725. DebugAssert(Terminal != NULL);
  5726. if (Data->Terminal == Terminal)
  5727. {
  5728. Data->Terminal = NULL;
  5729. }
  5730. }
  5731. //---------------------------------------------------------------------------
  5732. void __fastcall TCustomScpExplorerForm::LastTerminalClosed(TObject * /*Sender*/)
  5733. {
  5734. UpdateControls();
  5735. SessionColor = TColor(0);
  5736. UpdateRemotePathComboBox(false);
  5737. NeedSession(true);
  5738. }
  5739. //---------------------------------------------------------------------------
  5740. void __fastcall TCustomScpExplorerForm::NeedSession(bool ReloadSessions)
  5741. {
  5742. try
  5743. {
  5744. try
  5745. {
  5746. TTerminalManager::Instance()->NewSession(false, L"", ReloadSessions, this);
  5747. }
  5748. __finally
  5749. {
  5750. if (!WinConfiguration->KeepOpenWhenNoSession &&
  5751. (!Terminal || !Terminal->Active))
  5752. {
  5753. TerminateApplication();
  5754. }
  5755. }
  5756. }
  5757. catch (EAbort &)
  5758. {
  5759. // swallow
  5760. // The TTerminalManager does not expect the OnLastTerminalClose to throw without trying to connect.
  5761. // Also when called from TWinControl.UpdateShowing => CMShowingChanged, the showing is aborted on exception
  5762. }
  5763. }
  5764. //---------------------------------------------------------------------------
  5765. void __fastcall TCustomScpExplorerForm::DoTerminalListChanged(bool Force)
  5766. {
  5767. TStrings * TerminalList = TTerminalManager::Instance()->TerminalList;
  5768. int ActiveTerminalIndex = TTerminalManager::Instance()->ActiveTerminalIndex;
  5769. Configuration->Usage->SetMax(L"MaxOpenedSessions", TerminalList->Count);
  5770. bool ListChanged = Force || (TerminalList->Count + 1 != SessionsPageControl->PageCount);
  5771. if (!ListChanged)
  5772. {
  5773. int Index = 0;
  5774. while (!ListChanged && (Index < TerminalList->Count))
  5775. {
  5776. ListChanged =
  5777. (GetSessionTabTerminal(SessionsPageControl->Pages[Index]) != TerminalList->Objects[Index]) ||
  5778. (SessionsPageControl->Pages[Index]->Caption != TerminalList->Strings[Index]);
  5779. Index++;
  5780. }
  5781. }
  5782. if (ListChanged)
  5783. {
  5784. SendMessage(SessionsPageControl->Handle, WM_SETREDRAW, 0, 0);
  5785. try
  5786. {
  5787. FSessionColors->Clear();
  5788. AddFixedSessionImages();
  5789. while (SessionsPageControl->PageCount > 0)
  5790. {
  5791. delete SessionsPageControl->Pages[0];
  5792. }
  5793. for (int Index = 0; Index < TerminalList->Count; Index++)
  5794. {
  5795. TThemeTabSheet * TabSheet = new TThemeTabSheet(SessionsPageControl);
  5796. TabSheet->Caption = TerminalList->Strings[Index];
  5797. TTerminal * Terminal = dynamic_cast<TTerminal *>(TerminalList->Objects[Index]);
  5798. TabSheet->Tag = reinterpret_cast<int>(Terminal);
  5799. TabSheet->PageControl = SessionsPageControl;
  5800. UpdateSessionTab(TabSheet);
  5801. }
  5802. TTabSheet * TabSheet = new TTabSheet(SessionsPageControl);
  5803. TabSheet->PageControl = SessionsPageControl;
  5804. TabSheet->ImageIndex = FNewSessionTabImageIndex;
  5805. UpdateNewSessionTab();
  5806. }
  5807. __finally
  5808. {
  5809. SendMessage(SessionsPageControl->Handle, WM_SETREDRAW, 1, 0);
  5810. }
  5811. }
  5812. SessionsPageControl->ActivePageIndex = ActiveTerminalIndex;
  5813. }
  5814. //---------------------------------------------------------------------------
  5815. void __fastcall TCustomScpExplorerForm::TerminalListChanged(TObject * /*Sender*/)
  5816. {
  5817. DoTerminalListChanged(false);
  5818. }
  5819. //---------------------------------------------------------------------------
  5820. void __fastcall TCustomScpExplorerForm::UpdateNewSessionTab()
  5821. {
  5822. TTabSheet * TabSheet = SessionsPageControl->Pages[SessionsPageControl->PageCount - 1];
  5823. DebugAssert(TabSheet->ImageIndex == 0);
  5824. TabSheet->Caption =
  5825. WinConfiguration->SelectiveToolbarText ?
  5826. StripHotkey(StripTrailingPunctuation(NonVisualDataModule->NewSessionAction->Caption)) :
  5827. UnicodeString();
  5828. }
  5829. //---------------------------------------------------------------------------
  5830. TTerminal * __fastcall TCustomScpExplorerForm::GetSessionTabTerminal(TTabSheet * TabSheet)
  5831. {
  5832. return reinterpret_cast<TTerminal *>(TabSheet->Tag);
  5833. }
  5834. //---------------------------------------------------------------------------
  5835. void __fastcall TCustomScpExplorerForm::UpdateSessionTab(TTabSheet * TabSheet)
  5836. {
  5837. if (DebugAlwaysTrue(TabSheet != NULL))
  5838. {
  5839. TManagedTerminal * ManagedTerminal =
  5840. dynamic_cast<TManagedTerminal *>(GetSessionTabTerminal(TabSheet));
  5841. if (DebugAlwaysTrue(ManagedTerminal != NULL))
  5842. {
  5843. TColor Color =
  5844. (ManagedTerminal == FTerminal) ? FSessionColor : ManagedTerminal->StateData->Color;
  5845. TabSheet->ImageIndex = AddSessionColor(Color);
  5846. TabSheet->Caption = TTerminalManager::Instance()->GetTerminalTitle(ManagedTerminal, true);
  5847. TThemeTabSheet * ThemeTabSheet = dynamic_cast<TThemeTabSheet *>(TabSheet);
  5848. if (DebugAlwaysTrue(ThemeTabSheet != NULL))
  5849. {
  5850. ThemeTabSheet->Shadowed = !ManagedTerminal->Active;
  5851. }
  5852. }
  5853. }
  5854. }
  5855. //---------------------------------------------------------------------------
  5856. bool __fastcall TCustomScpExplorerForm::SessionTabSwitched()
  5857. {
  5858. DebugAssert(SessionsPageControl->ActivePage != NULL);
  5859. TTerminal * Terminal = GetSessionTabTerminal(SessionsPageControl->ActivePage);
  5860. bool Result = (Terminal != NULL);
  5861. if (Result)
  5862. {
  5863. TTerminalManager::Instance()->ActiveTerminal = Terminal;
  5864. }
  5865. else
  5866. {
  5867. try
  5868. {
  5869. NewSession(false);
  5870. }
  5871. __finally
  5872. {
  5873. DoTerminalListChanged(false);
  5874. }
  5875. FSessionsPageControlNewSessionTime = Now();
  5876. }
  5877. return Result;
  5878. }
  5879. //---------------------------------------------------------------------------
  5880. void __fastcall TCustomScpExplorerForm::SessionsPageControlChange(TObject * /*Sender*/)
  5881. {
  5882. SessionTabSwitched();
  5883. }
  5884. //---------------------------------------------------------------------------
  5885. void __fastcall TCustomScpExplorerForm::TransferListChange(TObject * Sender)
  5886. {
  5887. TTBXStringList * TransferList = dynamic_cast<TTBXStringList *>(Sender);
  5888. DebugAssert(TransferList != NULL);
  5889. UnicodeString Name;
  5890. if (TransferList->ItemIndex <= 0)
  5891. {
  5892. Name = L"";
  5893. }
  5894. else
  5895. {
  5896. Name = GUIConfiguration->CopyParamList->Names[TransferList->ItemIndex - 1];
  5897. }
  5898. if (FCopyParamAutoSelected.IsEmpty())
  5899. {
  5900. // if previous preset was not autoselected, make new preset the "default"
  5901. FCopyParamDefault = Name;
  5902. }
  5903. GUIConfiguration->CopyParamCurrent = Name;
  5904. }
  5905. //---------------------------------------------------------------------------
  5906. void __fastcall TCustomScpExplorerForm::UpdateTransferLabel()
  5907. {
  5908. // sanity check
  5909. bool ExistingPreset =
  5910. (FTransferListHoverIndex >= 0) &&
  5911. (FTransferListHoverIndex < 1 + GUIConfiguration->CopyParamList->Count);
  5912. DebugAssert(ExistingPreset);
  5913. if (ExistingPreset)
  5914. {
  5915. HDC DC = GetDC(0);
  5916. TCanvas * Canvas = new TCanvas();
  5917. try
  5918. {
  5919. Canvas->Handle = DC;
  5920. Canvas->Font = GetToolbarFont(this);
  5921. UnicodeString Name;
  5922. if (FTransferListHoverIndex == 0)
  5923. {
  5924. Name = L"";
  5925. }
  5926. else
  5927. {
  5928. Name = GUIConfiguration->CopyParamList->Names[FTransferListHoverIndex - 1];
  5929. }
  5930. TTBXLabelItem * TransferLabel = dynamic_cast<TTBXLabelItem*>(
  5931. static_cast<TComponent*>(GetComponent(fcTransferLabel)));
  5932. TTBXStringList * TransferList = dynamic_cast<TTBXStringList*>(
  5933. static_cast<TObject*>(GetComponent(fcTransferList)));
  5934. UnicodeString InfoStr =
  5935. GUIConfiguration->CopyParamPreset[Name].
  5936. GetInfoStr(L"; ",
  5937. FLAGMASK(Terminal != NULL, Terminal->UsableCopyParamAttrs(0).General));
  5938. int MaxWidth = TransferList->MinWidth - (2 * TransferLabel->Margin) - ScaleByTextHeight(this, 10);
  5939. if (Canvas->TextExtent(InfoStr).cx > MaxWidth)
  5940. {
  5941. while (Canvas->TextExtent(InfoStr + Ellipsis).cx > MaxWidth)
  5942. {
  5943. InfoStr.SetLength(InfoStr.Length() - 1);
  5944. }
  5945. InfoStr += Ellipsis;
  5946. }
  5947. // UpdateCaption does not cause invalidation of whole submenu, while
  5948. // setting Caption property does.
  5949. // also it probably does not resize the label, even if necessary
  5950. // (we do not want that anyway)
  5951. TransferLabel->UpdateCaption(InfoStr);
  5952. }
  5953. __finally
  5954. {
  5955. Canvas->Handle = NULL;
  5956. ReleaseDC(0, DC);
  5957. delete Canvas;
  5958. }
  5959. }
  5960. }
  5961. //---------------------------------------------------------------------------
  5962. void __fastcall TCustomScpExplorerForm::TransferListDrawItem(
  5963. TTBXCustomList * /*Sender*/, TCanvas * /*ACanvas*/, const TRect & /*ARect*/,
  5964. int /*AIndex*/, int AHoverIndex, bool & /*DrawDefault*/)
  5965. {
  5966. if (FTransferListHoverIndex != AHoverIndex)
  5967. {
  5968. FTransferListHoverIndex = AHoverIndex;
  5969. UpdateTransferLabel();
  5970. }
  5971. }
  5972. //---------------------------------------------------------------------------
  5973. void __fastcall TCustomScpExplorerForm::WMAppCommand(TMessage & Message)
  5974. {
  5975. int Command = GET_APPCOMMAND_LPARAM(Message.LParam);
  5976. TShiftState Shift = KeyDataToShiftState(GET_KEYSTATE_LPARAM(Message.LParam));
  5977. if ((Shift * (TShiftState() << ssShift << ssAlt << ssCtrl)).Empty())
  5978. {
  5979. if (Command == APPCOMMAND_BROWSER_FAVORITES)
  5980. {
  5981. OpenDirectory(GetSide(osCurrent));
  5982. Message.Result = 1;
  5983. }
  5984. else
  5985. {
  5986. TForm::Dispatch(&Message);
  5987. }
  5988. }
  5989. else
  5990. {
  5991. TForm::Dispatch(&Message);
  5992. }
  5993. }
  5994. //---------------------------------------------------------------------------
  5995. void __fastcall TCustomScpExplorerForm::CMDialogChar(TMessage & AMessage)
  5996. {
  5997. TCMDialogChar & Message = reinterpret_cast<TCMDialogChar &>(AMessage);
  5998. if ((FIgnoreNextDialogChar != 0) &&
  5999. (toupper(Message.CharCode) == toupper(FIgnoreNextDialogChar)))
  6000. {
  6001. Message.Result = 1;
  6002. }
  6003. else
  6004. {
  6005. TForm::Dispatch(&Message);
  6006. }
  6007. FIgnoreNextDialogChar = 0;
  6008. }
  6009. //---------------------------------------------------------------------------
  6010. void __fastcall TCustomScpExplorerForm::WMSysCommand(TMessage & Message)
  6011. {
  6012. // The four low-order bits are used internally by Windows
  6013. unsigned int Cmd = (Message.WParam & 0xFFF0);
  6014. // SC_RESTORE, SC_MAXIMIZE, SC_MINIMIZE - buttons on windows title
  6015. // SC_DEFAULT - double click on windows title (does not work, at least on WinXP)
  6016. // 61730 - restore thru double click - undocumented
  6017. // 61490 - maximize thru double click - undocumented
  6018. if ((Cmd == SC_RESTORE) || (Cmd == SC_MAXIMIZE) ||
  6019. (Cmd == SC_MINIMIZE) || (Cmd == SC_DEFAULT))
  6020. {
  6021. SysResizing(Cmd);
  6022. }
  6023. TForm::Dispatch(&Message);
  6024. }
  6025. //---------------------------------------------------------------------------
  6026. void __fastcall TCustomScpExplorerForm::WMQueryEndSession(TMessage & Message)
  6027. {
  6028. // We were actually never able to make ENDSESSION_CRITICAL happen.
  6029. // Also there no point returning TRUE as we are not able to
  6030. // handle the abrupt termination caused by subsequent WM_ENDSESSION cleanly.
  6031. // Hence the process termination might be safer :)
  6032. if ((Message.LParam != ENDSESSION_CRITICAL) &&
  6033. (((FQueue != NULL) && !FQueue->IsEmpty) || (FProgressForm != NULL)))
  6034. {
  6035. Message.Result = FALSE;
  6036. }
  6037. else
  6038. {
  6039. Message.Result = TRUE;
  6040. }
  6041. // Do not call default handling as that triggers OnCloseQuery,
  6042. // where our implementation will popup confirmation dialogs, what we do not want,
  6043. // as per Vista guidelines:
  6044. // https://docs.microsoft.com/en-us/windows/desktop/shutdown/shutting-down
  6045. }
  6046. //---------------------------------------------------------------------------
  6047. void __fastcall TCustomScpExplorerForm::WMEndSession(TWMEndSession & Message)
  6048. {
  6049. if (Message.EndSession && IsApplicationMinimized())
  6050. {
  6051. // WORKAROUND
  6052. // TApplication.WndProc() calls Application.Terminate() before Halt(),
  6053. // when it receives WM_ENDSESSION,
  6054. // but that sometimes (particularly when application is minimized) cause crashes.
  6055. // Crash popups message that blocks log off.
  6056. // Obviously application cannot shutdown cleanly after WM_ENDSESSION,
  6057. // so we call ExitProcess() immediately, not even trying to cleanup.
  6058. // It still causes beep, so there's likely some popup, but it does not block
  6059. // log off.
  6060. ExitProcess(0);
  6061. }
  6062. TForm::Dispatch(&Message);
  6063. }
  6064. #ifdef _DEBUG
  6065. //---------------------------------------------------------------------------
  6066. void __fastcall TCustomScpExplorerForm::WMWindowPosChanged(TWMWindowPosMsg & Message)
  6067. {
  6068. TForm::Dispatch(&Message);
  6069. }
  6070. #endif
  6071. //---------------------------------------------------------------------------
  6072. void __fastcall TCustomScpExplorerForm::SysResizing(unsigned int /*Cmd*/)
  6073. {
  6074. }
  6075. //---------------------------------------------------------------------------
  6076. void __fastcall TCustomScpExplorerForm::DoShow()
  6077. {
  6078. // only now are the controls resized finally, so the size constraints
  6079. // will not conflict with possibly very small window size
  6080. RestoreFormParams();
  6081. FixControlsPlacement();
  6082. if (Position == poDefaultPosOnly)
  6083. {
  6084. CutFormToDesktop(this);
  6085. }
  6086. TForm::DoShow();
  6087. FSessionsDragDropFilesEx->DragDropControl = SessionsPageControl;
  6088. FQueueDragDropFilesEx->DragDropControl = QueueView3;
  6089. if (Terminal == NULL)
  6090. {
  6091. StartingDisconnected();
  6092. }
  6093. UpdatePixelsPerInchMainWindowCounter();
  6094. FShowing = true;
  6095. }
  6096. //---------------------------------------------------------------------------
  6097. void __fastcall TCustomScpExplorerForm::UpdatePixelsPerInchMainWindowCounter()
  6098. {
  6099. Configuration->Usage->Set(L"PixelsPerInchMainWindow", PixelsPerInch);
  6100. }
  6101. //---------------------------------------------------------------------------
  6102. void __fastcall TCustomScpExplorerForm::StartingDisconnected()
  6103. {
  6104. DoTerminalListChanged(true);
  6105. }
  6106. //---------------------------------------------------------------------------
  6107. void __fastcall TCustomScpExplorerForm::PopupTrayBalloon(TTerminal * Terminal,
  6108. const UnicodeString & Str, TQueryType Type, Exception * E, unsigned int Seconds,
  6109. TNotifyEvent OnBalloonClick, TObject * UserData)
  6110. {
  6111. bool Do;
  6112. UnicodeString Message;
  6113. if (E == NULL)
  6114. {
  6115. Message = Str;
  6116. Do = true;
  6117. }
  6118. else
  6119. {
  6120. Do = ExceptionMessage(E, Message);
  6121. }
  6122. if (Do && WinConfiguration->BalloonNotifications)
  6123. {
  6124. UnicodeString Title;
  6125. if ((Terminal == NULL) && (Type == qtInformation) && ExtractMainInstructions(Message, Title))
  6126. {
  6127. Message = TrimLeft(Message);
  6128. }
  6129. else
  6130. {
  6131. Message = UnformatMessage(Message);
  6132. const ResourceString * Captions[] = { &_SMsgDlgConfirm, &_SMsgDlgWarning,
  6133. &_SMsgDlgError, &_SMsgDlgInformation, NULL };
  6134. Title = LoadResourceString(Captions[Type]);
  6135. if (Terminal != NULL)
  6136. {
  6137. Title = FORMAT(L"%s - %s",
  6138. (TTerminalManager::Instance()->GetTerminalTitle(Terminal, true), Title));
  6139. }
  6140. }
  6141. if (Seconds == 0)
  6142. {
  6143. Seconds = WinConfiguration->NotificationsTimeout;
  6144. }
  6145. FTrayIcon->PopupBalloon(Title, Message, Type, Seconds * MSecsPerSec, OnBalloonClick, UserData);
  6146. }
  6147. }
  6148. //---------------------------------------------------------------------------
  6149. unsigned int __fastcall TCustomScpExplorerForm::MoreMessageDialog(const UnicodeString Message,
  6150. TStrings * MoreMessages, TQueryType Type, unsigned int Answers,
  6151. UnicodeString HelpKeyword, const TMessageParams * Params,
  6152. TTerminal * Terminal)
  6153. {
  6154. if (((WinConfiguration->ContinueOnError && (FErrorList != NULL)) ||
  6155. (FOnFeedSynchronizeError != NULL)) &&
  6156. (Params != NULL) && (Params->Params & mpAllowContinueOnError) )
  6157. {
  6158. if (FOnFeedSynchronizeError != NULL)
  6159. {
  6160. FOnFeedSynchronizeError(Message, MoreMessages, Type, HelpKeyword);
  6161. }
  6162. else
  6163. {
  6164. DebugAssert(FErrorList != NULL);
  6165. TStringList * MoreMessagesCopy = NULL;
  6166. if (MoreMessages)
  6167. {
  6168. MoreMessagesCopy = new TStringList();
  6169. MoreMessagesCopy->Assign(MoreMessages);
  6170. }
  6171. FErrorList->AddObject(Message, MoreMessagesCopy);
  6172. }
  6173. PopupTrayBalloon(Terminal, Message, Type);
  6174. return ContinueAnswer(Answers);
  6175. }
  6176. else
  6177. {
  6178. bool UseBalloon = FTrayIcon->Visible;
  6179. if (UseBalloon)
  6180. {
  6181. PopupTrayBalloon(Terminal, Message, Type);
  6182. }
  6183. unsigned int Result;
  6184. try
  6185. {
  6186. Result = ::MoreMessageDialog(Message, MoreMessages, Type, Answers, HelpKeyword, Params);
  6187. }
  6188. __finally
  6189. {
  6190. // cancel only balloon we popped up, otherwise we may cancel notification
  6191. // balloon that was there before the message dialog
  6192. // (such as "dd confirmation opt in balloon)
  6193. if (UseBalloon)
  6194. {
  6195. FTrayIcon->CancelBalloon();
  6196. }
  6197. }
  6198. return Result;
  6199. }
  6200. }
  6201. //---------------------------------------------------------------------------
  6202. void __fastcall TCustomScpExplorerForm::ShowExtendedException(
  6203. TTerminal * Terminal, Exception * E)
  6204. {
  6205. if (FTrayIcon->Visible)
  6206. {
  6207. PopupTrayBalloon(Terminal, L"", qtError, E);
  6208. }
  6209. // particularly prevent opening new session from jump list,
  6210. // while exception is shown
  6211. NonVisualDataModule->StartBusy();
  6212. try
  6213. {
  6214. ShowExtendedExceptionEx(Terminal, E);
  6215. }
  6216. __finally
  6217. {
  6218. NonVisualDataModule->EndBusy();
  6219. FTrayIcon->CancelBalloon();
  6220. }
  6221. }
  6222. //---------------------------------------------------------------------------
  6223. void __fastcall TCustomScpExplorerForm::TerminalReady()
  6224. {
  6225. // cannot rely on active page being page for active terminal,
  6226. // as it can happen that active page is the "new session" page
  6227. // (e.g. when reconnecting active terminal, while login dialog
  6228. // invoked from "new session" page is modal)
  6229. int ActiveTerminalIndex = TTerminalManager::Instance()->ActiveTerminalIndex;
  6230. UpdateSessionTab(SessionsPageControl->Pages[ActiveTerminalIndex]);
  6231. }
  6232. //---------------------------------------------------------------------------
  6233. void __fastcall TCustomScpExplorerForm::InactiveTerminalException(
  6234. TTerminal * Terminal, Exception * E)
  6235. {
  6236. Notify(Terminal, L"", qtError, false, NULL, NULL, E);
  6237. if (!Terminal->Active)
  6238. {
  6239. int Index = TTerminalManager::Instance()->IndexOf(Terminal);
  6240. if (DebugAlwaysTrue((Index >= 0) && (Index < SessionsPageControl->PageCount)))
  6241. {
  6242. UpdateSessionTab(SessionsPageControl->Pages[Index]);
  6243. }
  6244. }
  6245. }
  6246. //---------------------------------------------------------------------------
  6247. void __fastcall TCustomScpExplorerForm::Notify(TTerminal * Terminal,
  6248. UnicodeString Message, TQueryType Type,
  6249. bool Important, TNotifyEvent OnClick, TObject * UserData, Exception * E)
  6250. {
  6251. if ((E == NULL) ||
  6252. ExceptionMessage(E, Message))
  6253. {
  6254. unsigned int Seconds = WinConfiguration->NotificationsTimeout;
  6255. if (Important)
  6256. {
  6257. Seconds *= 5;
  6258. }
  6259. UnicodeString NoteMessage(UnformatMessage(Message));
  6260. if (Terminal != NULL)
  6261. {
  6262. NoteMessage = FORMAT(L"%s: %s",
  6263. (TTerminalManager::Instance()->GetTerminalTitle(Terminal, true), NoteMessage));
  6264. }
  6265. if (WinConfiguration->BalloonNotifications)
  6266. {
  6267. AddNote(NoteMessage);
  6268. PopupTrayBalloon(Terminal, Message, Type, NULL, Seconds, OnClick, UserData);
  6269. }
  6270. else
  6271. {
  6272. FlashOnBackground();
  6273. PostNote(NoteMessage, Seconds, OnClick, UserData);
  6274. }
  6275. }
  6276. }
  6277. //---------------------------------------------------------------------------
  6278. void __fastcall TCustomScpExplorerForm::QueueEmptyNoteClicked(TObject * Sender)
  6279. {
  6280. RestoreApp();
  6281. TTerminalNoteData * TerminalNoteData = dynamic_cast<TTerminalNoteData *>(Sender);
  6282. if (DebugAlwaysTrue(TerminalNoteData != NULL) &&
  6283. !NonVisualDataModule->Busy)
  6284. {
  6285. TTerminal * Terminal = TerminalNoteData->Terminal;
  6286. TTerminalManager::Instance()->ActiveTerminal = Terminal;
  6287. if (!ComponentVisible[fcQueueView])
  6288. {
  6289. ToggleQueueVisibility();
  6290. GoToQueue();
  6291. }
  6292. }
  6293. }
  6294. //---------------------------------------------------------------------------
  6295. void __fastcall TCustomScpExplorerForm::QueueEvent(TTerminal * ATerminal,
  6296. TTerminalQueue * /*Queue*/, TQueueEvent Event)
  6297. {
  6298. TManagedTerminal * ManagedTerminal = DebugNotNull(dynamic_cast<TManagedTerminal *>(ATerminal));
  6299. UnicodeString Message;
  6300. TNotifyEvent OnClick = NULL;
  6301. TObject * UserData = NULL;
  6302. bool QueueInvisible = !ComponentVisible[fcQueueView] || IsApplicationMinimized();
  6303. switch (Event)
  6304. {
  6305. case qeEmptyButMonitored:
  6306. if ((ATerminal != Terminal) || QueueInvisible)
  6307. {
  6308. Message = LoadStr(BALLOON_QUEUE_EMPTY);
  6309. OnClick = QueueEmptyNoteClicked;
  6310. TTerminalNoteData * TerminalNoteData = new TTerminalNoteData();
  6311. TerminalNoteData->Terminal = ATerminal;
  6312. UserData = TerminalNoteData;
  6313. }
  6314. break;
  6315. case qeEmpty:
  6316. OperationComplete(ManagedTerminal->QueueOperationStart);
  6317. break;
  6318. case qePendingUserAction:
  6319. if ((ATerminal != Terminal) ||
  6320. (QueueInvisible && !IsQueueAutoPopup()))
  6321. {
  6322. Message = LoadStr(BALLOON_QUEUE_USER_ACTION);
  6323. }
  6324. break;
  6325. default:
  6326. DebugFail();
  6327. }
  6328. if (!Message.IsEmpty())
  6329. {
  6330. Notify(ATerminal, Message, qtInformation, false, OnClick, UserData);
  6331. }
  6332. }
  6333. //---------------------------------------------------------------------------
  6334. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDCreateDragFileList(
  6335. TObject * /*Sender*/, TFileList * FileList, bool & Created)
  6336. {
  6337. if (FDDExtMapFile != NULL)
  6338. {
  6339. CloseHandle(FDDExtMapFile);
  6340. FDDExtMapFile = NULL;
  6341. }
  6342. if (WinConfiguration->DDFakeFile)
  6343. {
  6344. DDFakeFileInitDrag(FileList, Created);
  6345. }
  6346. }
  6347. //---------------------------------------------------------------------------
  6348. void __fastcall TCustomScpExplorerForm::DDFakeCreated(TObject * /*Sender*/, const UnicodeString FileName)
  6349. {
  6350. if (DebugAlwaysTrue(!FDragFakeDirectory.IsEmpty()) &&
  6351. SameText(ExtractFileName(FileName), ExtractFileName(FDragFakeDirectory)))
  6352. {
  6353. FFakeFileDropTarget = FileName;
  6354. }
  6355. }
  6356. //---------------------------------------------------------------------------
  6357. void __fastcall TCustomScpExplorerForm::DDFakeFileInitDrag(TFileList * FileList,
  6358. bool & Created)
  6359. {
  6360. FFakeFileDropTarget = UnicodeString();
  6361. FDragFakeDirectory =
  6362. ExcludeTrailingBackslash(WinConfiguration->TemporaryDir());
  6363. if (!ForceDirectories(ApiPath(FDragFakeDirectory)))
  6364. {
  6365. throw Exception(FMTLOAD(CREATE_TEMP_DIR_ERROR, (FDragFakeDirectory)));
  6366. }
  6367. FileSetAttr(ApiPath(FDragFakeDirectory), faHidden);
  6368. FileList->AddItem(NULL, FDragFakeDirectory);
  6369. Created = true;
  6370. if (!WinConfiguration->IsDDExtRunning() || (GetWindowsBuild() >= 17134))
  6371. {
  6372. FDragFakeMonitors = StartCreationDirectoryMonitorsOnEachDrive(FILE_NOTIFY_CHANGE_DIR_NAME, DDFakeCreated);
  6373. }
  6374. FDDExtMapFile = CreateFileMappingA((HANDLE)0xFFFFFFFF, NULL, PAGE_READWRITE,
  6375. 0, sizeof(TDragExtCommStruct), AnsiString(DRAG_EXT_MAPPING).c_str());
  6376. {
  6377. TMutexGuard Guard(FDDExtMutex, DRAGEXT_MUTEX_RELEASE_TIMEOUT);
  6378. TDragExtCommStruct* CommStruct;
  6379. CommStruct = static_cast<TDragExtCommStruct*>(MapViewOfFile(FDDExtMapFile,
  6380. FILE_MAP_ALL_ACCESS, 0, 0, 0));
  6381. DebugAssert(CommStruct != NULL);
  6382. CommStruct->Version = TDragExtCommStruct::CurrentVersion;
  6383. CommStruct->Dragging = true;
  6384. wcsncpy(CommStruct->DropDest, FDragFakeDirectory.c_str(),
  6385. LENOF(CommStruct->DropDest));
  6386. NULL_TERMINATE(CommStruct->DropDest);
  6387. UnmapViewOfFile(CommStruct);
  6388. }
  6389. FDDMoveSlipped = false;
  6390. }
  6391. //---------------------------------------------------------------------------
  6392. bool __fastcall TCustomScpExplorerForm::RemoteFileControlFileOperation(
  6393. TObject * Sender, TFileOperation Operation, bool NoConfirmation, void * Param)
  6394. {
  6395. bool Result;
  6396. if (Sender == RemoteDirView)
  6397. {
  6398. Result = ExecuteFileOperation(Operation, osRemote, true, NoConfirmation, Param);
  6399. }
  6400. else
  6401. {
  6402. DebugAssert(Sender == RemoteDriveView);
  6403. TStrings * FileList = RemoteDriveView->DragFileList();
  6404. try
  6405. {
  6406. Result = ExecuteFileOperation(Operation, osRemote, FileList, NoConfirmation, Param);
  6407. }
  6408. __finally
  6409. {
  6410. delete FileList;
  6411. }
  6412. }
  6413. if (FDDTargetControl == RemoteDriveView)
  6414. {
  6415. RemoteDriveView->UpdateDropTarget();
  6416. }
  6417. // foRemoteMove happens when file/dir is dragged within the remote tree view
  6418. // or from tree view to dir view.
  6419. // foMove happens when file/dir is dragged from remote tree view outside of
  6420. // application via dragex
  6421. if (((Operation == foRemoteMove) || (Operation == foMove)) &&
  6422. (DropSourceControl == RemoteDriveView))
  6423. {
  6424. RemoteDriveView->UpdateDropSource();
  6425. }
  6426. return Result;
  6427. }
  6428. //---------------------------------------------------------------------------
  6429. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDEnd(TObject * Sender)
  6430. {
  6431. // This also handles drops of remote files to queue.
  6432. // Drops of local files (uploads) are handled in QueueDDProcessDropped.
  6433. SAFE_DESTROY(FDDFileList);
  6434. if (!FFakeFileDropTarget.IsEmpty())
  6435. {
  6436. RemoveDir(ApiPath(FFakeFileDropTarget));
  6437. }
  6438. if ((FDDExtMapFile != NULL) || (FDDTargetControl == QueueView3))
  6439. {
  6440. try
  6441. {
  6442. TDragResult DDResult = (Sender == RemoteDirView) ?
  6443. RemoteDirView->LastDDResult : RemoteDriveView->LastDDResult;
  6444. // Focus is moved to the target application,
  6445. // but as we are going to present the UI, we need to steal the focus back.
  6446. // This most likely won't work though (windows does not allow application
  6447. // to steal focus most of the time)
  6448. Application->BringToFront();
  6449. // On older version of Windows we never got drMove here, see also comment below.
  6450. // On Windows 10, we get the "move".
  6451. if ((DDResult == drCopy) || (DDResult == drMove) || (DDResult == drInvalid))
  6452. {
  6453. UnicodeString TargetDirectory;
  6454. TFileOperation Operation;
  6455. // drInvalid may mean drMove, see comment below
  6456. switch (DDResult)
  6457. {
  6458. case drCopy:
  6459. Operation = foCopy;
  6460. break;
  6461. case drMove:
  6462. Operation = foMove;
  6463. break;
  6464. default:
  6465. DebugFail();
  6466. case drInvalid:
  6467. // prefer "copy" for safety
  6468. Operation = FLAGSET(FLastDropEffect, DROPEFFECT_MOVE) ? foMove : foCopy;
  6469. break;
  6470. }
  6471. if (FDDMoveSlipped)
  6472. {
  6473. Operation = foMove;
  6474. }
  6475. TTransferOperationParam Param;
  6476. UnicodeString CounterName;
  6477. bool ForceQueue;
  6478. if (!DDGetTarget(Param.TargetDirectory, ForceQueue, CounterName))
  6479. {
  6480. // we get drInvalid both if d&d was intercepted by ddext,
  6481. // and when users drops on no-drop location.
  6482. // we tell the difference by existence of response from ddext,
  6483. // so we ignore absence of response in this case
  6484. if (DDResult != drInvalid)
  6485. {
  6486. // here we know that the extension is installed,
  6487. // as it is checked as soon as drag&drop starts from
  6488. // RemoteFileControlDDCreateDragFileList
  6489. Configuration->Usage->Inc(L"DownloadsDragDropExternalExtTargetUnknown");
  6490. throw ExtException(NULL, LoadStr(DRAGEXT_TARGET_UNKNOWN2), HELP_DRAGEXT_TARGET_UNKNOWN);
  6491. }
  6492. }
  6493. else
  6494. {
  6495. // download using fake file
  6496. Param.Temp = false;
  6497. Param.DragDrop = true;
  6498. if (ForceQueue)
  6499. {
  6500. Param.Queue = asOn;
  6501. }
  6502. if (Sender == RemoteDirView)
  6503. {
  6504. Param.Options = FLAGMASK(SelectedAllFilesInDirView(RemoteDirView), coAllFiles);
  6505. }
  6506. if (RemoteFileControlFileOperation(Sender, Operation,
  6507. (WinConfiguration->DDTransferConfirmation == asOff), &Param))
  6508. {
  6509. Configuration->Usage->Inc(CounterName);
  6510. }
  6511. }
  6512. }
  6513. }
  6514. __finally
  6515. {
  6516. CloseHandle(FDDExtMapFile);
  6517. FDDExtMapFile = NULL;
  6518. delete FDragFakeMonitors;
  6519. FDragFakeMonitors = NULL;
  6520. RemoveDir(ApiPath(FDragFakeDirectory));
  6521. FDragFakeDirectory = L"";
  6522. FFakeFileDropTarget = UnicodeString();
  6523. }
  6524. }
  6525. if (FDragDropSshTerminate.get() != NULL)
  6526. {
  6527. FDragDropSshTerminate->Rethrow();
  6528. }
  6529. }
  6530. //---------------------------------------------------------------------------
  6531. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDGiveFeedback(
  6532. TObject * Sender, int dwEffect, HRESULT & /*Result*/)
  6533. {
  6534. HCURSOR SlippedCopyCursor;
  6535. FDDMoveSlipped =
  6536. (FDragMoveCursor != NULL) &&
  6537. (!WinConfiguration->DDAllowMoveInit) && (dwEffect == DROPEFFECT_Copy) &&
  6538. ((IsFileControl(FDDTargetControl, osRemote) && (GetKeyState(VK_CONTROL) >= 0) &&
  6539. FTerminal->IsCapable[fcRemoteMove]) ||
  6540. (IsFileControl(FDDTargetControl, osLocal) && (GetKeyState(VK_SHIFT) < 0)));
  6541. SlippedCopyCursor = FDDMoveSlipped ? FDragMoveCursor : Dragdrop::DefaultCursor;
  6542. DragDropFiles(Sender)->CHCopy = SlippedCopyCursor;
  6543. DragDropFiles(Sender)->CHScrollCopy = SlippedCopyCursor;
  6544. // Remember drop effect so we know (when user drops files), if we copy or move
  6545. FLastDropEffect = dwEffect;
  6546. }
  6547. //---------------------------------------------------------------------------
  6548. bool __fastcall TCustomScpExplorerForm::DDGetTarget(
  6549. UnicodeString & Directory, bool & ForceQueue, UnicodeString & CounterName)
  6550. {
  6551. bool Result;
  6552. if (FDDTargetControl == QueueView3)
  6553. {
  6554. Directory = DefaultDownloadTargetDirectory();
  6555. Result = true;
  6556. CounterName = L"DownloadsDragDropQueue";
  6557. ForceQueue = true;
  6558. }
  6559. else if (!FFakeFileDropTarget.IsEmpty())
  6560. {
  6561. Directory = ExcludeTrailingBackslash(ExtractFilePath(FFakeFileDropTarget));
  6562. Result = true;
  6563. ForceQueue = false;
  6564. CounterName = L"DownloadsDragDropFakeFile";
  6565. }
  6566. else
  6567. {
  6568. ForceQueue = false;
  6569. Enabled = false;
  6570. try
  6571. {
  6572. int Timer = 0;
  6573. Result = false;
  6574. while (!Result && (Timer < WinConfiguration->DDExtTimeout))
  6575. {
  6576. {
  6577. TMutexGuard Guard(FDDExtMutex, DRAGEXT_MUTEX_RELEASE_TIMEOUT);
  6578. TDragExtCommStruct* CommStruct;
  6579. CommStruct = static_cast<TDragExtCommStruct*>(MapViewOfFile(FDDExtMapFile,
  6580. FILE_MAP_ALL_ACCESS, 0, 0, 0));
  6581. DebugAssert(CommStruct != NULL);
  6582. Result = !CommStruct->Dragging;
  6583. if (Result)
  6584. {
  6585. Directory = ExtractFilePath(CommStruct->DropDest);
  6586. CounterName = L"DownloadsDragDropExternalExt";
  6587. }
  6588. UnmapViewOfFile(CommStruct);
  6589. }
  6590. if (!Result)
  6591. {
  6592. Sleep(50);
  6593. Timer += 50;
  6594. Application->ProcessMessages();
  6595. }
  6596. }
  6597. }
  6598. __finally
  6599. {
  6600. Enabled = true;
  6601. }
  6602. }
  6603. return Result;
  6604. }
  6605. //---------------------------------------------------------------------------
  6606. void __fastcall TCustomScpExplorerForm::AddDelayedDirectoryDeletion(
  6607. const UnicodeString TempDir, int SecDelay)
  6608. {
  6609. TDateTime Alarm = Now() + (double)((double)SecDelay*OneMillisecond);
  6610. FDelayedDeletionList->AddObject(TempDir, reinterpret_cast<TObject*>(Alarm.FileDate()));
  6611. if (FDelayedDeletionTimer == NULL)
  6612. {
  6613. DebugAssert(HandleAllocated());
  6614. FDelayedDeletionTimer = new TTimer(this);
  6615. FDelayedDeletionTimer->Interval = 10000;
  6616. FDelayedDeletionTimer->OnTimer = DoDelayedDeletion;
  6617. }
  6618. else
  6619. {
  6620. FDelayedDeletionTimer->Enabled = true;
  6621. }
  6622. }
  6623. //---------------------------------------------------------------------------
  6624. void __fastcall TCustomScpExplorerForm::DoDelayedDeletion(TObject * Sender)
  6625. {
  6626. DebugAssert(FDelayedDeletionList != NULL);
  6627. TDateTime N = Now();
  6628. TDateTime Alert;
  6629. UnicodeString Directory;
  6630. for (int Index = FDelayedDeletionList->Count-1; Index >= 0; Index--)
  6631. {
  6632. Alert = FileDateToDateTime(reinterpret_cast<int>(FDelayedDeletionList->Objects[Index]));
  6633. if ((N >= Alert) || (Sender == NULL))
  6634. {
  6635. Directory = FDelayedDeletionList->Strings[Index];
  6636. if (DeleteDirectory(ExcludeTrailingBackslash(Directory)))
  6637. {
  6638. FDelayedDeletionList->Delete(Index);
  6639. }
  6640. }
  6641. }
  6642. if (FDelayedDeletionList->Count == 0)
  6643. {
  6644. FDelayedDeletionTimer->Enabled = false;
  6645. }
  6646. }
  6647. //---------------------------------------------------------------------------
  6648. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDTargetDrop()
  6649. {
  6650. if (IsFileControl(FDDTargetControl, osRemote) ||
  6651. (FDDTargetControl == SessionsPageControl))
  6652. {
  6653. TTerminal * TargetTerminal = NULL;
  6654. TFileOperation Operation = foNone;
  6655. if (FDDTargetControl == SessionsPageControl)
  6656. {
  6657. TPoint Point = SessionsPageControl->ScreenToClient(Mouse->CursorPos);
  6658. int Index = SessionsPageControl->IndexOfTabAt(Point.X, Point.Y);
  6659. // do not allow dropping on the "+" tab
  6660. TargetTerminal = GetSessionTabTerminal(SessionsPageControl->Pages[Index]);
  6661. if (TargetTerminal != NULL)
  6662. {
  6663. if ((FLastDropEffect == DROPEFFECT_MOVE) &&
  6664. (TargetTerminal == TTerminalManager::Instance()->ActiveTerminal))
  6665. {
  6666. Operation = foRemoteMove;
  6667. }
  6668. else
  6669. {
  6670. Operation = foRemoteCopy;
  6671. }
  6672. }
  6673. }
  6674. else
  6675. {
  6676. // when move from remote side is disabled, we allow copying inside the remote
  6677. // panel, but we interpret is as moving (we also slip in the move cursor)
  6678. if ((FLastDropEffect == DROPEFFECT_MOVE) ||
  6679. (!WinConfiguration->DDAllowMoveInit && (FLastDropEffect == DROPEFFECT_COPY) &&
  6680. FDDMoveSlipped))
  6681. {
  6682. Operation = foRemoteMove;
  6683. }
  6684. else if (FLastDropEffect == DROPEFFECT_COPY)
  6685. {
  6686. Operation = foRemoteCopy;
  6687. }
  6688. }
  6689. if (Operation != foNone)
  6690. {
  6691. RemoteFileControlFileOperation(DropSourceControl,
  6692. Operation, (WinConfiguration->DDTransferConfirmation == asOff), TargetTerminal);
  6693. }
  6694. // abort drag&drop
  6695. Abort();
  6696. }
  6697. else if ((FDDExtMapFile == NULL) && (FLastDropEffect != DROPEFFECT_NONE) &&
  6698. // Drops of remote files to queue are handled in RemoteFileControlDDEnd
  6699. (FDDTargetControl != QueueView3))
  6700. {
  6701. DebugAssert(!FDragTempDir.IsEmpty());
  6702. TTransferType Type;
  6703. UnicodeString TempDir = FDragTempDir;
  6704. // We clear FDragTempDir before calling
  6705. // just in case it fail (raises exception)
  6706. FDragTempDir = L"";
  6707. Type = (FLastDropEffect & DROPEFFECT_MOVE ? ttMove : Type = ttCopy);
  6708. TGUICopyParamType CopyParams = GUIConfiguration->CurrentCopyParam;
  6709. // empty directory parameter means temp directory -> don't display it!
  6710. UnicodeString TargetDir = L"";
  6711. int Options = 0;
  6712. if (!CopyParamDialog(tdToLocal, Type, true, FDDFileList,
  6713. TargetDir, CopyParams, (WinConfiguration->DDTransferConfirmation != asOff), true, Options))
  6714. {
  6715. Abort();
  6716. }
  6717. // TargetDir is set when dropped on local file control
  6718. // (this was workaround for legacy dirview event handling, now it should be
  6719. // made prettier)
  6720. if (TargetDir.IsEmpty())
  6721. {
  6722. TargetDir = TempDir;
  6723. if (ForceDirectories(ApiPath(TargetDir)))
  6724. {
  6725. DebugAssert(Terminal && !TargetDir.IsEmpty());
  6726. FPendingTempSpaceWarn = true;
  6727. try
  6728. {
  6729. FDragDropOperation = true;
  6730. int Params = cpTemporary |
  6731. (Type == ttMove ? cpDelete : 0);
  6732. DDDownload(FDDFileList, TargetDir, &CopyParams, Params);
  6733. Configuration->Usage->Inc(L"DownloadsDragDropExternalTemp");
  6734. }
  6735. __finally
  6736. {
  6737. FDragDropOperation = false;
  6738. FPendingTempSpaceWarn = false;
  6739. AddDelayedDirectoryDeletion(TargetDir, WinConfiguration->DDDeleteDelay);
  6740. }
  6741. }
  6742. else
  6743. {
  6744. throw Exception(FMTLOAD(CREATE_TEMP_DIR_ERROR, (TargetDir)));
  6745. }
  6746. }
  6747. }
  6748. }
  6749. //---------------------------------------------------------------------------
  6750. void __fastcall TCustomScpExplorerForm::DDDownload(
  6751. TStrings * FilesToCopy, const UnicodeString & TargetDir, const TCopyParamType * CopyParam, int Params)
  6752. {
  6753. void * BatchStorage;
  6754. BatchStart(BatchStorage);
  6755. try
  6756. {
  6757. UpdateCopyParamCounters(*CopyParam);
  6758. Terminal->CopyToLocal(FilesToCopy, TargetDir, CopyParam, Params, NULL);
  6759. if (FLAGSET(Params, cpDelete) && (DropSourceControl == RemoteDriveView))
  6760. {
  6761. RemoteDriveView->UpdateDropSource();
  6762. }
  6763. }
  6764. __finally
  6765. {
  6766. BatchEnd(BatchStorage);
  6767. }
  6768. }
  6769. //---------------------------------------------------------------------------
  6770. class TFakeDataObjectFilesEx : public TDataObjectFilesEx
  6771. {
  6772. public:
  6773. __fastcall TFakeDataObjectFilesEx(TFileList * AFileList, bool RenderPIDL,
  6774. bool RenderFilename) : TDataObjectFilesEx(AFileList, RenderPIDL, RenderFilename)
  6775. {
  6776. }
  6777. virtual bool __fastcall AllowData(const tagFORMATETC & FormatEtc)
  6778. {
  6779. return (FormatEtc.cfFormat == CF_HDROP) ? false :
  6780. TDataObjectFilesEx::AllowData(FormatEtc);
  6781. }
  6782. };
  6783. //---------------------------------------------------------------------------
  6784. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDCreateDataObject(
  6785. TObject * Sender, TDataObject *& DataObject)
  6786. {
  6787. if (FDDExtMapFile != NULL)
  6788. {
  6789. TFileList * FileList = DragDropFiles(Sender)->FileList;
  6790. if (!FileList->RenderPIDLs() || !FileList->RenderNames())
  6791. {
  6792. Abort();
  6793. }
  6794. if (FileList->Count > 0)
  6795. {
  6796. TDataObjectFilesEx * FilesObject = new TFakeDataObjectFilesEx(FileList, true, true);
  6797. if (!FilesObject->IsValid(true, true))
  6798. {
  6799. FilesObject->_Release();
  6800. }
  6801. else
  6802. {
  6803. DataObject = FilesObject;
  6804. }
  6805. }
  6806. }
  6807. }
  6808. //---------------------------------------------------------------------------
  6809. void __fastcall TCustomScpExplorerForm::GoToCommandLine()
  6810. {
  6811. DebugFail();
  6812. }
  6813. //---------------------------------------------------------------------------
  6814. void __fastcall TCustomScpExplorerForm::GoToTree()
  6815. {
  6816. ComponentVisible[fcRemoteTree] = true;
  6817. RemoteDriveView->SetFocus();
  6818. }
  6819. //---------------------------------------------------------------------------
  6820. TStrings * __fastcall TCustomScpExplorerForm::PanelExport(TOperationSide Side,
  6821. TPanelExport Export)
  6822. {
  6823. TCustomDirView * DirView = this->DirView(Side);
  6824. std::unique_ptr<TStrings> ExportData(new TStringList());
  6825. switch (Export)
  6826. {
  6827. case pePath:
  6828. ExportData->Add(DirView->PathName);
  6829. break;
  6830. case peFileList:
  6831. case peFullFileList:
  6832. {
  6833. bool FullPath = (Export == peFullFileList);
  6834. DirView->CreateFileList(false, FullPath, ExportData.get());
  6835. for (int Index = 0; Index < ExportData->Count; Index++)
  6836. {
  6837. if (ExportData->Strings[Index].Pos(L" ") > 0)
  6838. {
  6839. ExportData->Strings[Index] = FORMAT(L"\"%s\"", (ExportData->Strings[Index]));
  6840. }
  6841. }
  6842. }
  6843. break;
  6844. default:
  6845. DebugFail();
  6846. }
  6847. return ExportData.release();
  6848. }
  6849. //---------------------------------------------------------------------------
  6850. void __fastcall TCustomScpExplorerForm::PanelExport(TOperationSide Side,
  6851. TPanelExport Export, TPanelExportDestination Destination)
  6852. {
  6853. std::unique_ptr<TStrings> ExportData(PanelExport(Side, Export));
  6854. PanelExportStore(Side, Export, Destination, ExportData.get());
  6855. }
  6856. //---------------------------------------------------------------------------
  6857. void __fastcall TCustomScpExplorerForm::PanelExportStore(TOperationSide /*Side*/,
  6858. TPanelExport /*Export*/, TPanelExportDestination Destination,
  6859. TStrings * ExportData)
  6860. {
  6861. if (Destination == pedClipboard)
  6862. {
  6863. TInstantOperationVisualizer Visualizer;
  6864. CopyToClipboard(ExportData);
  6865. }
  6866. else
  6867. {
  6868. DebugFail();
  6869. }
  6870. }
  6871. //---------------------------------------------------------------------------
  6872. void __fastcall TCustomScpExplorerForm::Filter(TOperationSide Side)
  6873. {
  6874. TCustomDirView * DirView = this->DirView(Side);
  6875. TFileFilter Filter;
  6876. DefaultFileFilter(Filter);
  6877. Filter.Masks = DirView->Mask;
  6878. if (DoFilterMaskDialog(DirView, &Filter))
  6879. {
  6880. DirView->Mask = TFileMasks::NormalizeMask(Filter.Masks);
  6881. Configuration->Usage->Inc(L"Filters");
  6882. }
  6883. }
  6884. //---------------------------------------------------------------------------
  6885. TQueueOperation __fastcall TCustomScpExplorerForm::DefaultQueueOperation()
  6886. {
  6887. return FQueueController->DefaultOperation();
  6888. }
  6889. //---------------------------------------------------------------------------
  6890. bool __fastcall TCustomScpExplorerForm::AllowQueueOperation(
  6891. TQueueOperation Operation, void ** Param)
  6892. {
  6893. switch (Operation)
  6894. {
  6895. case qoPreferences:
  6896. return true;
  6897. case qoGoTo:
  6898. return ComponentVisible[fcQueueView] && QueueView3->Enabled;
  6899. case qoOnceEmpty:
  6900. return !FQueueController->Empty;
  6901. default:
  6902. return FQueueController->AllowOperation(Operation, Param);
  6903. }
  6904. }
  6905. //---------------------------------------------------------------------------
  6906. void __fastcall TCustomScpExplorerForm::GoToQueue()
  6907. {
  6908. if (DebugAlwaysTrue(QueueView3->Visible))
  6909. {
  6910. QueueView3->SetFocus();
  6911. }
  6912. }
  6913. //---------------------------------------------------------------------------
  6914. void __fastcall TCustomScpExplorerForm::ExecuteQueueOperation(
  6915. TQueueOperation Operation, void * Param)
  6916. {
  6917. if (Operation == qoGoTo)
  6918. {
  6919. GoToQueue();
  6920. }
  6921. else if (Operation == qoPreferences)
  6922. {
  6923. PreferencesDialog(pmQueue);
  6924. }
  6925. else
  6926. {
  6927. FQueueController->ExecuteOperation(Operation, Param);
  6928. }
  6929. }
  6930. //---------------------------------------------------------------------------
  6931. bool __fastcall TCustomScpExplorerForm::GetQueueEnabled()
  6932. {
  6933. return (Queue != NULL) && Queue->Enabled;
  6934. }
  6935. //---------------------------------------------------------------------------
  6936. void __fastcall TCustomScpExplorerForm::ToggleQueueEnabled()
  6937. {
  6938. DebugAssert(Queue != NULL);
  6939. if (Queue != NULL)
  6940. {
  6941. Queue->Enabled = !Queue->Enabled;
  6942. }
  6943. }
  6944. //---------------------------------------------------------------------------
  6945. void __fastcall TCustomScpExplorerForm::QueueView3ContextPopup(
  6946. TObject * /*Sender*/, TPoint & /*MousePos*/, bool & /*Handled*/)
  6947. {
  6948. FQueueActedItem = QueueView3->ItemFocused;
  6949. }
  6950. //---------------------------------------------------------------------------
  6951. /*virtual*/ int __fastcall TCustomScpExplorerForm::GetStaticComponentsHeight()
  6952. {
  6953. return TopDock->Height + QueueSplitter->Height;
  6954. }
  6955. //---------------------------------------------------------------------------
  6956. void __fastcall TCustomScpExplorerForm::QueueSplitterCanResize(
  6957. TObject * /*Sender*/, int & NewSize, bool & Accept)
  6958. {
  6959. // when queue is hidden by double-clicking splitter, stray attempt to
  6960. // resize the panel with strange value arrives, make sure it is ignored
  6961. if (ComponentVisible[fcQueueView])
  6962. {
  6963. int HeightLimit = ClientHeight - GetStaticComponentsHeight() -
  6964. RemotePanel->Constraints->MinHeight;
  6965. if (NewSize > HeightLimit)
  6966. {
  6967. NewSize = HeightLimit;
  6968. }
  6969. }
  6970. else
  6971. {
  6972. Accept = false;
  6973. }
  6974. }
  6975. //---------------------------------------------------------------------------
  6976. void __fastcall TCustomScpExplorerForm::QueueView3StartDrag(TObject * /*Sender*/,
  6977. TDragObject *& /*DragObject*/)
  6978. {
  6979. FQueueActedItem = QueueView3->ItemFocused;
  6980. }
  6981. //---------------------------------------------------------------------------
  6982. void __fastcall TCustomScpExplorerForm::QueueView3DragOver(TObject * /*Sender*/,
  6983. TObject * Source, int X, int Y, TDragState /*State*/, bool & Accept)
  6984. {
  6985. Accept = true;
  6986. if (Source == QueueView3)
  6987. {
  6988. TListItem * DropTarget = QueueView3->GetItemAt(X, Y);
  6989. Accept = (DropTarget != NULL) && (FQueueActedItem != NULL);
  6990. if (Accept)
  6991. {
  6992. TQueueItemProxy * QueueItem;
  6993. TQueueItemProxy * DestQueueItem;
  6994. QueueItem = static_cast<TQueueItemProxy *>(FQueueActedItem->Data);
  6995. DestQueueItem = static_cast<TQueueItemProxy *>(DropTarget->Data);
  6996. Accept = (QueueItem != DestQueueItem) &&
  6997. (QueueItem->Status == TQueueItem::qsPending) &&
  6998. (DestQueueItem->Status == TQueueItem::qsPending);
  6999. }
  7000. }
  7001. }
  7002. //---------------------------------------------------------------------------
  7003. void __fastcall TCustomScpExplorerForm::QueueView3DragDrop(TObject * /*Sender*/,
  7004. TObject * /*Source*/, int /*X*/, int /*Y*/)
  7005. {
  7006. if ((FQueueActedItem != NULL) && (QueueView3->DropTarget != NULL))
  7007. {
  7008. TQueueItemProxy * QueueItem;
  7009. TQueueItemProxy * DestQueueItem;
  7010. QueueItem = static_cast<TQueueItemProxy *>(FQueueActedItem->Data);
  7011. DestQueueItem = static_cast<TQueueItemProxy *>(QueueView3->DropTarget->Data);
  7012. QueueItem->Move(DestQueueItem);
  7013. }
  7014. }
  7015. //---------------------------------------------------------------------------
  7016. void __fastcall TCustomScpExplorerForm::QueueView3Enter(TObject * /*Sender*/)
  7017. {
  7018. if ((QueueView3->ItemFocused == NULL) &&
  7019. (QueueView3->Items->Count > 0))
  7020. {
  7021. QueueView3->ItemFocused = QueueView3->Items->Item[0];
  7022. }
  7023. QueueLabelUpdateStatus();
  7024. }
  7025. //---------------------------------------------------------------------------
  7026. void __fastcall TCustomScpExplorerForm::QueueView3Exit(TObject * /*Sender*/)
  7027. {
  7028. QueueLabelUpdateStatus();
  7029. }
  7030. //---------------------------------------------------------------------------
  7031. void __fastcall TCustomScpExplorerForm::QueueLabelUpdateStatus()
  7032. {
  7033. QueueLabel->UpdateStatus();
  7034. }
  7035. //---------------------------------------------------------------------------
  7036. void __fastcall TCustomScpExplorerForm::QueueView3SelectItem(
  7037. TObject * /*Sender*/, TListItem * /*Item*/, bool Selected)
  7038. {
  7039. if (Selected)
  7040. {
  7041. NonVisualDataModule->UpdateNonVisibleActions();
  7042. }
  7043. }
  7044. //---------------------------------------------------------------------------
  7045. TDragDropFilesEx * __fastcall TCustomScpExplorerForm::DragDropFiles(TObject * Sender)
  7046. {
  7047. TDragDropFilesEx * Result = NULL;
  7048. if (Sender == SessionsPageControl)
  7049. {
  7050. Result = FSessionsDragDropFilesEx;
  7051. }
  7052. else if (Sender == QueueView3)
  7053. {
  7054. Result = FQueueDragDropFilesEx;
  7055. }
  7056. else
  7057. {
  7058. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  7059. if (DirView != NULL)
  7060. {
  7061. Result = DirView->DragDropFilesEx;
  7062. }
  7063. else
  7064. {
  7065. TCustomDriveView * DriveView = dynamic_cast<TCustomDriveView *>(Sender);
  7066. if (DriveView != NULL)
  7067. {
  7068. Result = DriveView->DragDropFilesEx;
  7069. }
  7070. }
  7071. }
  7072. DebugAssert(Result != NULL);
  7073. return Result;
  7074. }
  7075. //---------------------------------------------------------------------------
  7076. bool __fastcall TCustomScpExplorerForm::SelectedAllFilesInDirView(TCustomDirView * DView)
  7077. {
  7078. return (DView->SelCount == DView->FilesCount);
  7079. }
  7080. //---------------------------------------------------------------------------
  7081. bool __fastcall TCustomScpExplorerForm::DraggingAllFilesFromDirView(TOperationSide Side, TStrings * FileList)
  7082. {
  7083. return HasDirView[Side] && (DropSourceControl == DirView(Side)) && (FileList->Count == DirView(Side)->FilesCount);
  7084. }
  7085. //---------------------------------------------------------------------------
  7086. void __fastcall TCustomScpExplorerForm::RemoteFileControlDragDropFileOperation(
  7087. TObject * Sender, int Effect, UnicodeString TargetPath, bool ForceQueue)
  7088. {
  7089. TFileOperation Operation;
  7090. switch (Effect)
  7091. {
  7092. case DROPEFFECT_MOVE:
  7093. Operation = foMove;
  7094. break;
  7095. case DROPEFFECT_COPY:
  7096. // occures on WinXP (reported by user)
  7097. default:
  7098. Operation = foCopy;
  7099. break;
  7100. };
  7101. TDragDropFilesEx * DragDropFilesEx = DragDropFiles(Sender);
  7102. // see a comment in TUnixDirView::PerformItemDragDropOperation
  7103. if (DragDropFilesEx->FileList->Count > 0)
  7104. {
  7105. TStrings * FileList = new TStringList();
  7106. try
  7107. {
  7108. for (int Index = 0; Index < DragDropFilesEx->FileList->Count; Index++)
  7109. {
  7110. FileList->Add(DragDropFilesEx->FileList->Items[Index]->Name);
  7111. }
  7112. FDragDropOperation = true;
  7113. TTransferOperationParam Param;
  7114. Param.TargetDirectory = TargetPath;
  7115. // upload, no temp dirs
  7116. Param.Temp = false;
  7117. Param.DragDrop = true;
  7118. Param.Options =
  7119. FLAGMASK(DraggingAllFilesFromDirView(osLocal, FileList), coAllFiles);
  7120. if (ForceQueue)
  7121. {
  7122. Param.Queue = asOn;
  7123. }
  7124. if (ExecuteFileOperation(Operation, osLocal, FileList,
  7125. (WinConfiguration->DDTransferConfirmation == asOff), &Param))
  7126. {
  7127. if (IsFileControl(DropSourceControl, osLocal))
  7128. {
  7129. Configuration->Usage->Inc(L"UploadsDragDropInternal");
  7130. }
  7131. else
  7132. {
  7133. Configuration->Usage->Inc(L"UploadsDragDropExternal");
  7134. }
  7135. }
  7136. }
  7137. __finally
  7138. {
  7139. FDragDropOperation = false;
  7140. delete FileList;
  7141. }
  7142. }
  7143. }
  7144. //---------------------------------------------------------------------------
  7145. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDFileOperation(
  7146. TObject * Sender, int Effect, UnicodeString /*SourcePath*/,
  7147. UnicodeString TargetPath, bool & /*DoOperation*/)
  7148. {
  7149. RemoteFileControlDragDropFileOperation(Sender, Effect, TargetPath, false);
  7150. }
  7151. //---------------------------------------------------------------------------
  7152. void __fastcall TCustomScpExplorerForm::RemoteFileContolDDChooseEffect(
  7153. TObject * Sender, int grfKeyState, int & dwEffect)
  7154. {
  7155. // if any drop effect is allowed at all (e.g. no drop to self and drop to parent)
  7156. if ((dwEffect != DROPEFFECT_None) &&
  7157. IsFileControl(DropSourceControl, osRemote))
  7158. {
  7159. // do not allow drop on remote panel (on free space, still allow drop on directories)
  7160. if ((Sender == RemoteDirView) && (DropSourceControl == RemoteDirView) &&
  7161. (RemoteDirView->DropTarget == NULL))
  7162. {
  7163. dwEffect = DROPEFFECT_None;
  7164. }
  7165. else
  7166. {
  7167. if (dwEffect == DROPEFFECT_Copy)
  7168. {
  7169. bool MoveCapable = FTerminal->IsCapable[fcRemoteMove];
  7170. // currently we support copying always (at least via temporary directory);
  7171. // remove associated checks once this all proves stable and working
  7172. bool CopyCapable = true;
  7173. // if we do not support neither of operations, there's no discussion
  7174. if (!MoveCapable && !CopyCapable)
  7175. {
  7176. dwEffect = DROPEFFECT_None;
  7177. }
  7178. // when moving is disabled, we need to keep effect to "copy",
  7179. // which will be later interpretted as move (with slipped-in cursor)
  7180. else if (!WinConfiguration->DDAllowMoveInit && FLAGCLEAR(grfKeyState, MK_CONTROL))
  7181. {
  7182. // no-op, keep copy
  7183. }
  7184. else
  7185. {
  7186. // The default effect inside remote panel is move,
  7187. // unless we do not support it, but support copy
  7188. if (FLAGCLEAR(grfKeyState, MK_CONTROL))
  7189. {
  7190. dwEffect = MoveCapable ? DROPEFFECT_Move : DROPEFFECT_Copy;
  7191. }
  7192. else
  7193. {
  7194. // with ctrl-down, we want copy unless it is not supported
  7195. dwEffect = CopyCapable ? DROPEFFECT_Copy : DROPEFFECT_None;
  7196. }
  7197. }
  7198. }
  7199. }
  7200. }
  7201. }
  7202. //---------------------------------------------------------------------------
  7203. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDDragFileName(
  7204. TObject * Sender, TRemoteFile * File, UnicodeString & FileName)
  7205. {
  7206. if (FDDTotalSize >= 0)
  7207. {
  7208. if (File->IsDirectory)
  7209. {
  7210. FDDTotalSize = -1;
  7211. }
  7212. else
  7213. {
  7214. FDDTotalSize += File->Size;
  7215. }
  7216. }
  7217. DebugAssert(!FDragTempDir.IsEmpty());
  7218. // TODO: this is quite ineffective
  7219. // TODO: what if invalid character replacement is disabled?
  7220. FileName = FDragTempDir + GUIConfiguration->CurrentCopyParam.ValidLocalFileName(File->FileName);
  7221. UnicodeString TransferFileName;
  7222. if (Sender == RemoteDriveView)
  7223. {
  7224. TransferFileName = UnixExcludeTrailingBackslash(File->FullFileName);
  7225. }
  7226. else
  7227. {
  7228. TransferFileName = File->FileName;
  7229. }
  7230. FDDFileList->AddObject(TransferFileName, File);
  7231. }
  7232. //---------------------------------------------------------------------------
  7233. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDDragDetect(
  7234. TObject * /*Sender*/, int /*grfKeyState*/, const TPoint & /*DetectStart*/,
  7235. const TPoint & /*Point*/, TDragDetectStatus /*DragStatus*/)
  7236. {
  7237. // sometimes we do not get DDEnd so the list is not released
  7238. SAFE_DESTROY(FDDFileList);
  7239. FDDFileList = new TStringList();
  7240. FDragTempDir = WinConfiguration->TemporaryDir();
  7241. FDDTotalSize = 0;
  7242. FDragDropSshTerminate.reset(NULL);
  7243. }
  7244. //---------------------------------------------------------------------------
  7245. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDQueryContinueDrag(
  7246. TObject * /*Sender*/, BOOL /*FEscapePressed*/, int /*grfKeyState*/,
  7247. HRESULT & Result)
  7248. {
  7249. if (Result == DRAGDROP_S_DROP)
  7250. {
  7251. try
  7252. {
  7253. GlobalDragImageList->HideDragImage();
  7254. try
  7255. {
  7256. RemoteFileControlDDTargetDrop();
  7257. }
  7258. catch(ESshTerminate & E)
  7259. {
  7260. FDragDropSshTerminate.reset(E.Clone());
  7261. }
  7262. }
  7263. catch (Exception &E)
  7264. {
  7265. // If downloading fails we need to cancel drag&drop, otherwise
  7266. // Explorer shows error
  7267. // But by the way exception probably never reach this point as
  7268. // it's catched on way.
  7269. // Fatal exceptions get here (like when opening a secondary shell extension for file duplication fails).
  7270. Result = DRAGDROP_S_CANCEL;
  7271. ShowExtendedException(Terminal, &E);
  7272. }
  7273. }
  7274. }
  7275. //---------------------------------------------------------------------------
  7276. void __fastcall TCustomScpExplorerForm::DirViewMatchMask(
  7277. TObject * /*Sender*/, UnicodeString FileName, bool Directory, __int64 Size,
  7278. TDateTime Modification, UnicodeString Masks, bool & Matches, bool AllowImplicitMatches)
  7279. {
  7280. TFileMasks::TParams MaskParams;
  7281. MaskParams.Size = Size;
  7282. MaskParams.Modification = Modification;
  7283. // this does not re-parse the mask if it is the same as the last time
  7284. FDirViewMatchMask = Masks;
  7285. bool ImplicitMatch;
  7286. Matches =
  7287. // RecurseInclude parameter has no effect as the Path is empty
  7288. FDirViewMatchMask.Matches(FileName, Directory, UnicodeString(L""), &MaskParams, true, ImplicitMatch) &&
  7289. (AllowImplicitMatches || !ImplicitMatch);
  7290. }
  7291. //---------------------------------------------------------------------------
  7292. void __fastcall TCustomScpExplorerForm::DirViewGetOverlay(
  7293. TObject * Sender, TListItem * Item, WORD & Indexes)
  7294. {
  7295. TCustomDirView * DirView = reinterpret_cast<TCustomDirView *>(Sender);
  7296. UnicodeString Ext;
  7297. if (DirView == RemoteDirView)
  7298. {
  7299. Ext = UnixExtractFileExt(DirView->ItemFileName(Item));
  7300. }
  7301. else
  7302. {
  7303. Ext = ExtractFileExt(DirView->ItemFileName(Item));
  7304. }
  7305. if (SameText(Ext, Configuration->PartialExt))
  7306. {
  7307. Indexes |= oiPartial;
  7308. }
  7309. }
  7310. //---------------------------------------------------------------------------
  7311. bool __fastcall TCustomScpExplorerForm::CanPasteToDirViewFromClipBoard()
  7312. {
  7313. return
  7314. DirViewEnabled(osCurrent) &&
  7315. DirView(osCurrent)->CanPasteFromClipBoard();
  7316. }
  7317. //---------------------------------------------------------------------------
  7318. bool __fastcall TCustomScpExplorerForm::CanPasteFromClipBoard()
  7319. {
  7320. bool Result = false;
  7321. if (CanPasteToDirViewFromClipBoard())
  7322. {
  7323. Result = true;
  7324. }
  7325. else
  7326. {
  7327. UnicodeString ClipboardText;
  7328. if (NonEmptyTextFromClipboard(ClipboardText))
  7329. {
  7330. if (StoredSessions->IsUrl(ClipboardText))
  7331. {
  7332. Result = true;
  7333. }
  7334. else
  7335. {
  7336. Result = DirViewEnabled(osCurrent);
  7337. }
  7338. }
  7339. }
  7340. return Result;
  7341. }
  7342. //---------------------------------------------------------------------------
  7343. void __fastcall TCustomScpExplorerForm::PasteFromClipBoard()
  7344. {
  7345. if (CanPasteToDirViewFromClipBoard())
  7346. {
  7347. DirView(osCurrent)->PasteFromClipBoard();
  7348. }
  7349. else
  7350. {
  7351. UnicodeString ClipboardText;
  7352. if (NonEmptyTextFromClipboard(ClipboardText))
  7353. {
  7354. if (StoredSessions->IsUrl(ClipboardText))
  7355. {
  7356. NewSession(false, ClipboardText);
  7357. }
  7358. else
  7359. {
  7360. DirView(osCurrent)->Path = ClipboardText;
  7361. }
  7362. }
  7363. }
  7364. }
  7365. //---------------------------------------------------------------------------
  7366. void __fastcall TCustomScpExplorerForm::FileListFromClipboard()
  7367. {
  7368. // TBD
  7369. }
  7370. //---------------------------------------------------------------------------
  7371. void __fastcall TCustomScpExplorerForm::SelectAll(TOperationSide Side, TSelectMode Mode)
  7372. {
  7373. TCustomDirView * ADirView = DirView(Side);
  7374. ADirView->SelectAll(Mode);
  7375. ADirView->SetFocus();
  7376. }
  7377. //---------------------------------------------------------------------------
  7378. void __fastcall TCustomScpExplorerForm::SelectByMask(TOperationSide Side, bool Select)
  7379. {
  7380. TCustomDirView * ADirView = DirView(Side);
  7381. TFileFilter Filter;
  7382. DefaultFileFilter(Filter);
  7383. if (DoSelectMaskDialog(ADirView, Select, &Filter, Configuration))
  7384. {
  7385. Configuration->Usage->Inc(L"MaskSelections");
  7386. ADirView->SelectFiles(Filter, Select);
  7387. ADirView->SetFocus();
  7388. }
  7389. }
  7390. //---------------------------------------------------------------------------
  7391. void __fastcall TCustomScpExplorerForm::RestoreSelectedNames(TOperationSide Side)
  7392. {
  7393. TCustomDirView * ADirView = DirView(Side);
  7394. ADirView->RestoreSelectedNames();
  7395. ADirView->SetFocus();
  7396. }
  7397. //---------------------------------------------------------------------------
  7398. void __fastcall TCustomScpExplorerForm::SelectSameExt(bool Select)
  7399. {
  7400. TCustomDirView * CurrentDirView = DirView(osCurrent);
  7401. if (DebugAlwaysTrue(CurrentDirView->ItemFocused != NULL))
  7402. {
  7403. UnicodeString FileName = CurrentDirView->ItemFileName(CurrentDirView->ItemFocused);
  7404. UnicodeString Ext;
  7405. if (GetSide(osCurrent) == osRemote)
  7406. {
  7407. Ext = UnixExtractFileExt(FileName);
  7408. }
  7409. else
  7410. {
  7411. Ext = ExtractFileExt(FileName);
  7412. }
  7413. if (Ext.IsEmpty())
  7414. {
  7415. Ext = L".";
  7416. }
  7417. TFileFilter Filter;
  7418. Filter.Masks = FORMAT(L"*%s", (Ext));
  7419. Filter.Directories = false;
  7420. CurrentDirView->SelectFiles(Filter, Select);
  7421. }
  7422. }
  7423. //---------------------------------------------------------------------------
  7424. UnicodeString __fastcall TCustomScpExplorerForm::FileStatusBarText(
  7425. const TStatusFileInfo & FileInfo, TOperationSide Side)
  7426. {
  7427. UnicodeString Result;
  7428. if ((Side == osRemote) && (Terminal == NULL))
  7429. {
  7430. // noop
  7431. }
  7432. else
  7433. {
  7434. Result =
  7435. FMTLOAD(FILE_INFO_FORMAT,
  7436. (FormatBytes(FileInfo.SelectedSize),
  7437. FormatBytes(FileInfo.FilesSize),
  7438. FormatNumber(FileInfo.SelectedCount),
  7439. FormatNumber(FileInfo.FilesCount)));
  7440. }
  7441. return Result;
  7442. }
  7443. //---------------------------------------------------------------------------
  7444. void __fastcall TCustomScpExplorerForm::FileStatusBarPanelClick(
  7445. TTBXStatusPanel * Panel, TOperationSide Side)
  7446. {
  7447. if (Panel->Index == 1)
  7448. {
  7449. ToggleShowHiddenFiles();
  7450. }
  7451. else if (Panel->Index == 2)
  7452. {
  7453. Filter(Side);
  7454. }
  7455. }
  7456. //---------------------------------------------------------------------------
  7457. void __fastcall TCustomScpExplorerForm::UpdateFileStatusBar(
  7458. TTBXStatusBar * StatusBar, const TStatusFileInfo & FileInfo, TOperationSide Side)
  7459. {
  7460. DebugAssert(!StatusBar->SimplePanel);
  7461. StatusBar->Panels->Items[0]->Caption = FileStatusBarText(FileInfo, Side);
  7462. UpdateFileStatusExtendedPanels(StatusBar, FileInfo);
  7463. }
  7464. //---------------------------------------------------------------------------
  7465. void __fastcall TCustomScpExplorerForm::UpdateFileStatusExtendedPanels(
  7466. TTBXStatusBar * StatusBar, const TStatusFileInfo & FileInfo)
  7467. {
  7468. DebugAssert(StatusBar->Panels->Count >= 3);
  7469. TTBXStatusPanel * HiddenFilesPanel = StatusBar->Panels->Items[1];
  7470. if (FileInfo.HiddenCount > 0)
  7471. {
  7472. HiddenFilesPanel->Caption = FMTLOAD(FILE_INFO_HIDDEN2, (FormatNumber(FileInfo.HiddenCount)));
  7473. HiddenFilesPanel->ViewPriority = 90; // <100 allows hiding panel when it does not fit
  7474. }
  7475. else
  7476. {
  7477. HiddenFilesPanel->ViewPriority = 0;
  7478. // not really necessary, just to cleanup no-longer-valid data
  7479. HiddenFilesPanel->Caption = L"";
  7480. }
  7481. TTBXStatusPanel * FilteredFilesPanel = StatusBar->Panels->Items[2];
  7482. if (FileInfo.FilteredCount > 0)
  7483. {
  7484. FilteredFilesPanel->Caption = FMTLOAD(FILE_INFO_FILTERED2, (FormatNumber(FileInfo.FilteredCount)));
  7485. FilteredFilesPanel->ViewPriority = 90; // <100 allows hiding panel when it does not fit
  7486. }
  7487. else
  7488. {
  7489. FilteredFilesPanel->ViewPriority = 0;
  7490. // not really necessary, just to cleanup no-longer-valid data
  7491. FilteredFilesPanel->Caption = L"";
  7492. }
  7493. }
  7494. //---------------------------------------------------------------------------
  7495. void __fastcall TCustomScpExplorerForm::RemoteStatusBarClick(
  7496. TObject * /*Sender*/)
  7497. {
  7498. if (RemoteDirView->Enabled)
  7499. {
  7500. RemoteDirView->SetFocus();
  7501. }
  7502. }
  7503. //---------------------------------------------------------------------------
  7504. void __fastcall TCustomScpExplorerForm::ToggleQueueVisibility()
  7505. {
  7506. TQueueViewConfiguration Config = WinConfiguration->QueueView;
  7507. switch (Config.Show)
  7508. {
  7509. case qvShow:
  7510. if ((FQueueStatus != NULL) && (FQueueStatus->Count > 0))
  7511. {
  7512. Config.Show = qvHide;
  7513. }
  7514. else
  7515. {
  7516. Config.Show = Config.LastHideShow;
  7517. }
  7518. break;
  7519. case qvHideWhenEmpty:
  7520. if (ComponentVisible[fcQueueView])
  7521. {
  7522. Config.Show = qvHide;
  7523. }
  7524. else
  7525. {
  7526. Config.LastHideShow = Config.Show;
  7527. Config.Show = qvShow;
  7528. }
  7529. break;
  7530. case qvHide:
  7531. Config.LastHideShow = Config.Show;
  7532. Config.Show = qvShow;
  7533. break;
  7534. }
  7535. WinConfiguration->QueueView = Config;
  7536. }
  7537. //---------------------------------------------------------------------------
  7538. UnicodeString __fastcall TCustomScpExplorerForm::PathForCaption()
  7539. {
  7540. UnicodeString Result;
  7541. if (FTerminal != NULL)
  7542. {
  7543. switch (WinConfiguration->PathInCaption)
  7544. {
  7545. case picShort:
  7546. Result = TTerminalManager::Instance()->GetTerminalShortPath(FTerminal);
  7547. break;
  7548. case picFull:
  7549. Result = FTerminal->CurrentDirectory;
  7550. break;
  7551. }
  7552. }
  7553. return Result;
  7554. }
  7555. //---------------------------------------------------------------------------
  7556. void __fastcall TCustomScpExplorerForm::UpdateControls()
  7557. {
  7558. TTerminalManager::Instance()->UpdateAppTitle();
  7559. // WORAKRDOUND: Disabling list view when it is not showing yet does not set its
  7560. // background to gray on Windows 7 (works on Windows 10).
  7561. // See also EnableControl
  7562. if (Showing)
  7563. {
  7564. bool HasTerminal = (Terminal != NULL);
  7565. if (HasTerminal)
  7566. {
  7567. // Update path when it changes
  7568. if ((SessionsPageControl->ActivePage != NULL) && (GetSessionTabTerminal(SessionsPageControl->ActivePage) == Terminal))
  7569. {
  7570. UpdateSessionTab(SessionsPageControl->ActivePage);
  7571. }
  7572. if (!RemoteDirView->Enabled)
  7573. {
  7574. RemoteDirView->Enabled = true;
  7575. if (FRemoteDirViewWasFocused)
  7576. {
  7577. ActiveControl = RemoteDirView;
  7578. }
  7579. }
  7580. RemoteDriveView->Enabled = true;
  7581. RemoteDirView->Color = (FSessionColor != 0 ? FSessionColor : clWindow);
  7582. RemoteDriveView->Color = RemoteDirView->Color;
  7583. }
  7584. else
  7585. {
  7586. if (RemoteDirView->Enabled)
  7587. {
  7588. // This is first called when the form is being constructed
  7589. // (not anymore due to Showing test above)
  7590. // but the false is overriden in the constructor later.
  7591. // An even later in TScpCommanderForm::DoShow()
  7592. FRemoteDirViewWasFocused = (ActiveControl == RemoteDirView);
  7593. EnableControl(RemoteDirView, false);
  7594. }
  7595. EnableControl(RemoteDriveView, false);
  7596. }
  7597. EnableControl(QueueView3, HasTerminal && Terminal->IsCapable[fsBackgroundTransfers]);
  7598. QueueLabelUpdateStatus();
  7599. reinterpret_cast<TTBCustomItem *>(GetComponent(fcRemotePathComboBox))->Enabled = HasTerminal;
  7600. }
  7601. }
  7602. //---------------------------------------------------------------------------
  7603. void __fastcall TCustomScpExplorerForm::DoDirViewLoaded(TCustomDirView * /*Sender*/)
  7604. {
  7605. UpdateControls();
  7606. }
  7607. //---------------------------------------------------------------------------
  7608. void __fastcall TCustomScpExplorerForm::DirViewLoaded(
  7609. TObject * Sender)
  7610. {
  7611. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  7612. DebugAssert(DirView != NULL);
  7613. DoDirViewLoaded(DirView);
  7614. TransferPresetAutoSelect();
  7615. }
  7616. //---------------------------------------------------------------------------
  7617. void __fastcall TCustomScpExplorerForm::StartUpdates()
  7618. {
  7619. TUpdatesConfiguration Updates = WinConfiguration->Updates;
  7620. // first run after installation
  7621. if (double(Updates.LastCheck) == 0)
  7622. {
  7623. // make sure next time there will be an update (if enabled)
  7624. Updates.LastCheck = TDateTime(1);
  7625. WinConfiguration->Updates = Updates;
  7626. }
  7627. else if ((double(Updates.Period) > 0) &&
  7628. (Now() - Updates.LastCheck >= Updates.Period))
  7629. {
  7630. TThreadMethod OnUpdatesChecked = NULL;
  7631. if (!IsUWP())
  7632. {
  7633. OnUpdatesChecked = UpdatesChecked;
  7634. }
  7635. StartUpdateThread(OnUpdatesChecked);
  7636. }
  7637. }
  7638. //---------------------------------------------------------------------------
  7639. void __fastcall TCustomScpExplorerForm::UpdatesChecked()
  7640. {
  7641. UnicodeString Message;
  7642. bool New;
  7643. TQueryType Type;
  7644. GetUpdatesMessage(Message, New, Type, false);
  7645. if (!Message.IsEmpty())
  7646. {
  7647. if (New)
  7648. {
  7649. Message = FMTLOAD(NEW_VERSION_CLICK, (Message));
  7650. }
  7651. if (!New && (Type != qtWarning))
  7652. {
  7653. PostNote(UnformatMessage(Message), 0, UpdatesNoteClicked, NULL);
  7654. }
  7655. else
  7656. {
  7657. Notify(NULL, Message, Type, true, UpdatesNoteClicked);
  7658. }
  7659. if (New)
  7660. {
  7661. Configuration->Usage->Inc(L"UpdateNotifications");
  7662. }
  7663. }
  7664. }
  7665. //---------------------------------------------------------------------------
  7666. void __fastcall TCustomScpExplorerForm::UpdatesNoteClicked(TObject * /*Sender*/)
  7667. {
  7668. RestoreApp();
  7669. bool CanDisplay = !NonVisualDataModule->Busy;
  7670. if (!CanDisplay && (Screen->ActiveForm != NULL))
  7671. {
  7672. CanDisplay = (Screen->ActiveForm->Perform(WM_CAN_DISPLAY_UPDATES, 0, 0) != 0);
  7673. }
  7674. if (CanDisplay)
  7675. {
  7676. Configuration->Usage->Inc(L"UpdateNotificationsClicked");
  7677. CheckForUpdates(true);
  7678. }
  7679. else
  7680. {
  7681. Configuration->Usage->Inc(L"UpdateNotificationsBusy");
  7682. }
  7683. }
  7684. //---------------------------------------------------------------------------
  7685. void __fastcall TCustomScpExplorerForm::GetTransferPresetAutoSelectData(
  7686. TCopyParamRuleData & Data)
  7687. {
  7688. DebugAssert(Terminal != NULL);
  7689. Data.HostName = Terminal->SessionData->HostNameExpanded;
  7690. Data.UserName = Terminal->SessionData->UserNameExpanded;
  7691. Data.RemoteDirectory = RemoteDirView->PathName;
  7692. }
  7693. //---------------------------------------------------------------------------
  7694. void __fastcall TCustomScpExplorerForm::TransferPresetAutoSelect()
  7695. {
  7696. // Terminal can be null when we are changing local directory implicitly,
  7697. // because it has been deleted, while no session is connected
  7698. // (Login dialog is open)
  7699. if (FAllowTransferPresetAutoSelect && (Terminal != NULL))
  7700. {
  7701. TCopyParamRuleData Data;
  7702. GetTransferPresetAutoSelectData(Data);
  7703. int CopyParamIndex = GUIConfiguration->CopyParamList->Find(Data);
  7704. UnicodeString CopyParamCurrent = GUIConfiguration->CopyParamCurrent;
  7705. if (CopyParamIndex < 0)
  7706. {
  7707. // there is no preset that matches autoselection
  7708. // set preset that we consider "default"
  7709. FCopyParamAutoSelected = L""; // forget last autoselected preset
  7710. GUIConfiguration->CopyParamCurrent = FCopyParamDefault;
  7711. }
  7712. else
  7713. {
  7714. // there is preset matching autoselection
  7715. UnicodeString CopyParamName = GUIConfiguration->CopyParamList->Names[CopyParamIndex];
  7716. if (CopyParamName == FCopyParamAutoSelected)
  7717. {
  7718. // autoselected the same preset as the last time
  7719. // make no change (i.e. preserve custom user preset, if any)
  7720. }
  7721. else
  7722. {
  7723. // autoselected the different preset then the last time (or there
  7724. // was default preset set)
  7725. FCopyParamAutoSelected = CopyParamName; // remember autoselection
  7726. GUIConfiguration->CopyParamCurrent = CopyParamName;
  7727. }
  7728. }
  7729. if (GUIConfiguration->CopyParamCurrent != CopyParamCurrent)
  7730. {
  7731. if (CopyParamIndex >= 0)
  7732. {
  7733. Configuration->Usage->Inc(L"CopyParamAutoSelects");
  7734. }
  7735. TTransferPresetNoteData * Data = new TTransferPresetNoteData;
  7736. try
  7737. {
  7738. int Fmt =
  7739. (CopyParamIndex < 0) ?
  7740. (GUIConfiguration->CopyParamIndex < 0 ? COPY_PARAM_DEFAULT_NORM : COPY_PARAM_DEFAULT_CUSTOM) :
  7741. COPY_PARAM_AUTOSELECTED;
  7742. UnicodeString Message = FMTLOAD(Fmt, (StripHotkey(GUIConfiguration->CopyParamCurrent)));
  7743. Data->Message = MainInstructions(Message);
  7744. int CopyParamAttrs = Terminal->UsableCopyParamAttrs(0).General;
  7745. UnicodeString Info = GUIConfiguration->CurrentCopyParam.GetInfoStr(L"\n",
  7746. CopyParamAttrs);
  7747. if (CopyParamIndex >= 0)
  7748. {
  7749. DebugAssert(GUIConfiguration->CopyParamList->Rules[CopyParamIndex] != NULL);
  7750. Info = FORMAT(L"%s\n \n%s", (Info,
  7751. FMTLOAD(COPY_PARAM_RULE,
  7752. (GUIConfiguration->CopyParamList->Rules[CopyParamIndex]->GetInfoStr(L"\n")))));
  7753. }
  7754. Data->Message += L"\n\n" + Info;
  7755. if (WinConfiguration->CopyParamAutoSelectNotice)
  7756. {
  7757. TransferPresetNoteMessage(Data, true);
  7758. }
  7759. else
  7760. {
  7761. PostNote(Message, 0, TransferPresetNoteClicked, Data);
  7762. Data = NULL; // ownership passed
  7763. }
  7764. }
  7765. __finally
  7766. {
  7767. delete Data;
  7768. }
  7769. }
  7770. }
  7771. }
  7772. //---------------------------------------------------------------------------
  7773. void __fastcall TCustomScpExplorerForm::TransferPresetNoteMessage(
  7774. TTransferPresetNoteData * NoteData, bool AllowNeverAskAgain)
  7775. {
  7776. DebugAssert(NoteData != NULL);
  7777. TMessageParams Params(AllowNeverAskAgain ? mpNeverAskAgainCheck : 0);
  7778. TQueryButtonAlias Aliases[1];
  7779. Aliases[0].Button = qaIgnore; // "ignore" is after "ok"
  7780. Aliases[0].Alias = LoadStr(CONFIGURE_BUTTON);
  7781. Params.Aliases = Aliases;
  7782. Params.AliasesCount = LENOF(Aliases);
  7783. unsigned int Result =
  7784. MoreMessageDialog(NoteData->Message, NULL, qtInformation,
  7785. qaOK | qaIgnore, HELP_COPY_PARAM_AUTOSELECTED, &Params);
  7786. switch (Result)
  7787. {
  7788. case qaNeverAskAgain:
  7789. DebugAssert(AllowNeverAskAgain);
  7790. WinConfiguration->CopyParamAutoSelectNotice = false;
  7791. break;
  7792. case qaIgnore:
  7793. PreferencesDialog(pmPresets);
  7794. break;
  7795. }
  7796. }
  7797. //---------------------------------------------------------------------------
  7798. void __fastcall TCustomScpExplorerForm::TransferPresetNoteClicked(TObject * Sender)
  7799. {
  7800. // as of now useless, as this is used for notes only, never for balloons, ...
  7801. RestoreApp();
  7802. // .. and we should never be busy here
  7803. if (DebugAlwaysTrue(!NonVisualDataModule->Busy))
  7804. {
  7805. TransferPresetNoteMessage(DebugNotNull(dynamic_cast<TTransferPresetNoteData *>(Sender)), false);
  7806. }
  7807. }
  7808. //---------------------------------------------------------------------------
  7809. void __fastcall TCustomScpExplorerForm::PreferencesDialog(
  7810. TPreferencesMode APreferencesMode)
  7811. {
  7812. std::unique_ptr<TPreferencesDialogData> PreferencesData;
  7813. TCopyParamRuleData Data;
  7814. if (Terminal != NULL)
  7815. {
  7816. PreferencesData.reset(new TPreferencesDialogData());
  7817. GetTransferPresetAutoSelectData(Data);
  7818. PreferencesData->CopyParamRuleData = &Data;
  7819. }
  7820. DoPreferencesDialog(APreferencesMode, PreferencesData.get());
  7821. }
  7822. //---------------------------------------------------------------------------
  7823. void __fastcall TCustomScpExplorerForm::AdHocCustomCommandValidate(
  7824. const TCustomCommandType & Command)
  7825. {
  7826. if (CustomCommandState(Command, FEditingFocusedAdHocCommand, ccltAll) <= 0)
  7827. {
  7828. throw Exception(FMTLOAD(CUSTOM_COMMAND_IMPOSSIBLE, (Command.Command)));
  7829. }
  7830. }
  7831. //---------------------------------------------------------------------------
  7832. void __fastcall TCustomScpExplorerForm::AdHocCustomCommand(bool OnFocused)
  7833. {
  7834. bool RemoteAllowed = CustomCommandRemoteAllowed();
  7835. TCustomCommandType Command;
  7836. // make sure we use local custom command when remote are not supported
  7837. if (RemoteAllowed || FLAGSET(FLastCustomCommand.Params, ccLocal))
  7838. {
  7839. Command = FLastCustomCommand;
  7840. }
  7841. else
  7842. {
  7843. Command.Params = Command.Params | ccLocal;
  7844. }
  7845. Command.Name = LoadStr(CUSTOM_COMMAND_AD_HOC_NAME);
  7846. FEditingFocusedAdHocCommand = OnFocused;
  7847. bool LocalSide = (FCurrentSide == osLocal);
  7848. int Options =
  7849. FLAGMASK((!RemoteAllowed || LocalSide), ccoDisableRemote) |
  7850. FLAGMASK(LocalSide, ccoDisableRemoteFiles);
  7851. if (DoCustomCommandDialog(Command, WinConfiguration->CustomCommandList,
  7852. ccmAdHoc, Options, AdHocCustomCommandValidate, NULL))
  7853. {
  7854. FLastCustomCommand = Command;
  7855. UpdateCustomCommandsToolbar();
  7856. ExecuteFileOperation(foCustomCommand, osRemote, OnFocused, false, &Command);
  7857. }
  7858. }
  7859. //---------------------------------------------------------------------------
  7860. void __fastcall TCustomScpExplorerForm::LastCustomCommand(bool OnFocused)
  7861. {
  7862. DebugAssert(!FLastCustomCommand.Command.IsEmpty());
  7863. int State = CustomCommandState(FLastCustomCommand, OnFocused, ccltAll);
  7864. DebugAssert(State > 0);
  7865. if (State <= 0)
  7866. {
  7867. throw Exception(FMTLOAD(CUSTOM_COMMAND_IMPOSSIBLE, (FLastCustomCommand.Command)));
  7868. }
  7869. ExecuteFileOperation(foCustomCommand, osRemote, OnFocused, false, &FLastCustomCommand);
  7870. }
  7871. //---------------------------------------------------------------------------
  7872. bool __fastcall TCustomScpExplorerForm::GetLastCustomCommand(bool OnFocused,
  7873. TCustomCommandType & Command, int & State)
  7874. {
  7875. bool Result = !FLastCustomCommand.Command.IsEmpty();
  7876. if (Result)
  7877. {
  7878. Command = FLastCustomCommand;
  7879. State = CustomCommandState(FLastCustomCommand, OnFocused, ccltAll);
  7880. }
  7881. return Result;
  7882. }
  7883. //---------------------------------------------------------------------------
  7884. void __fastcall TCustomScpExplorerForm::BeforeAction()
  7885. {
  7886. if (RemoteDirView->ItemFocused != NULL)
  7887. {
  7888. RemoteDirView->ItemFocused->CancelEdit();
  7889. }
  7890. }
  7891. //---------------------------------------------------------------------------
  7892. void __fastcall TCustomScpExplorerForm::PostComponentHide(Byte Component)
  7893. {
  7894. DebugAssert(ComponentVisible[Component]);
  7895. PostMessage(Handle, WM_COMPONENT_HIDE, Component, 0);
  7896. }
  7897. //---------------------------------------------------------------------------
  7898. void __fastcall TCustomScpExplorerForm::QueueSplitterDblClick(TObject * /*Sender*/)
  7899. {
  7900. // when queue panel is resized here directly, the status bar is stretched
  7901. // over whole space the panel occupied
  7902. PostComponentHide(fcQueueView);
  7903. }
  7904. //---------------------------------------------------------------------------
  7905. void __fastcall TCustomScpExplorerForm::Dispatch(void * Message)
  7906. {
  7907. TMessage * M = static_cast<TMessage*>(Message);
  7908. switch (M->Msg)
  7909. {
  7910. case CM_DIALOGCHAR:
  7911. CMDialogChar(*M);
  7912. break;
  7913. case WM_APPCOMMAND:
  7914. WMAppCommand(*M);
  7915. break;
  7916. case WM_SYSCOMMAND:
  7917. WMSysCommand(*M);
  7918. break;
  7919. case WM_QUERYENDSESSION:
  7920. WMQueryEndSession(*M);
  7921. break;
  7922. case WM_ENDSESSION:
  7923. WMEndSession(*reinterpret_cast<TWMEndSession *>(M));
  7924. break;
  7925. #ifdef _DEBUG
  7926. case WM_WINDOWPOSCHANGED:
  7927. WMWindowPosChanged(*reinterpret_cast<TWMWindowPosMsg *>(M));
  7928. break;
  7929. case CM_CUSTOMSTYLECHANGED:
  7930. TForm::Dispatch(Message);
  7931. break;
  7932. #endif
  7933. case WM_COMPONENT_HIDE:
  7934. {
  7935. Byte Component = static_cast<Byte>(M->WParam);
  7936. // sanity check
  7937. if (ComponentVisible[Component])
  7938. {
  7939. // special treatment
  7940. if (Component == fcQueueView)
  7941. {
  7942. ToggleQueueVisibility();
  7943. DebugAssert(!ComponentVisible[fcQueueView]);
  7944. }
  7945. else
  7946. {
  7947. ComponentVisible[Component] = false;
  7948. }
  7949. }
  7950. }
  7951. break;
  7952. case WM_COPYDATA:
  7953. WMCopyData(*M);
  7954. break;
  7955. case WM_MANAGES_CAPTION:
  7956. // caption managed in TTerminalManager::UpdateAppTitle()
  7957. M->Result = 1;
  7958. break;
  7959. case WM_WANTS_MOUSEWHEEL:
  7960. M->Result = 1;
  7961. break;
  7962. case CM_SHOWINGCHANGED:
  7963. CMShowingChanged(*M);
  7964. break;
  7965. case WM_CLOSE:
  7966. WMClose(*M);
  7967. break;
  7968. case WM_DPICHANGED:
  7969. WMDpiChanged(*M);
  7970. break;
  7971. case CM_DPICHANGED:
  7972. CMDpiChanged(*M);
  7973. break;
  7974. default:
  7975. TForm::Dispatch(Message);
  7976. break;
  7977. }
  7978. }
  7979. //---------------------------------------------------------------------------
  7980. void __fastcall TCustomScpExplorerForm::UpdateImages()
  7981. {
  7982. // noop
  7983. }
  7984. //---------------------------------------------------------------------------
  7985. void __fastcall TCustomScpExplorerForm::CMDpiChanged(TMessage & Message)
  7986. {
  7987. TForm::Dispatch(&Message);
  7988. // regenerate session images
  7989. DoTerminalListChanged(true);
  7990. UpdateImages();
  7991. }
  7992. //---------------------------------------------------------------------------
  7993. void __fastcall TCustomScpExplorerForm::WMDpiChanged(TMessage & Message)
  7994. {
  7995. TForm::Dispatch(&Message);
  7996. Configuration->Usage->Inc(L"PixelsPerInchChanged");
  7997. UpdatePixelsPerInchMainWindowCounter();
  7998. }
  7999. //---------------------------------------------------------------------------
  8000. void __fastcall TCustomScpExplorerForm::WMClose(TMessage & Message)
  8001. {
  8002. // Cannot close window while we are busy.
  8003. // We cannot test this in FormCloseQuery as that is called also from
  8004. // Close(), which is called by CloseApplicationAction. So we can be busy
  8005. // there already even, when it is legitimate to close the application.
  8006. // Possibly a better place to handle this would be WMSysCommand.
  8007. if (NonVisualDataModule->Busy)
  8008. {
  8009. Message.Result = 1;
  8010. }
  8011. else
  8012. {
  8013. TForm::Dispatch(&Message);
  8014. }
  8015. }
  8016. //---------------------------------------------------------------------------
  8017. void __fastcall TCustomScpExplorerForm::CMShowingChanged(TMessage & Message)
  8018. {
  8019. TForm::Dispatch(&Message);
  8020. if (Showing && (Terminal == NULL))
  8021. {
  8022. // When we are starting minimized (i.e. from an installer),
  8023. // postpone showing Login dialog until we get restored.
  8024. // Otherwise the Login dialog (and Authentication window) show restored
  8025. // over invidible (minimized) main window.
  8026. if (WindowState == wsMinimized)
  8027. {
  8028. FNeedSession = true;
  8029. }
  8030. else
  8031. {
  8032. // This happens before application ever goes idle, so the toolbars would
  8033. // stay enabled (initial state) until the Login dialog is dismissed.
  8034. UpdateActions();
  8035. NonVisualDataModule->StartBusy();
  8036. try
  8037. {
  8038. // Need to process WM_ACTIVATEAPP before showing the Login dialog,
  8039. // otherwise the dialog does not receive focus.
  8040. // With Commander interface the ProcessMessages is called already
  8041. // by TDriveView, but with Explorer interface, we need to call it explicily
  8042. Application->ProcessMessages();
  8043. // do not reload sessions, they have been loaded just now (optimization)
  8044. NeedSession(false);
  8045. }
  8046. __finally
  8047. {
  8048. NonVisualDataModule->EndBusy();
  8049. }
  8050. }
  8051. }
  8052. }
  8053. //---------------------------------------------------------------------------
  8054. void __fastcall TCustomScpExplorerForm::FormConstrainedResize(
  8055. TObject * /*Sender*/, int & MinWidth, int & MinHeight, int & MaxWidth,
  8056. int & MaxHeight)
  8057. {
  8058. // workaround for bug in TWinControl.CalcConstraints
  8059. // Check for empty rect (restore from iconic state) is done there only after
  8060. // call to AdjustClientRect, which enlarges the rect (for forms).
  8061. TRect R = GetClientRect();
  8062. // when restoring from iconic state, place no restrictions
  8063. if (IsRectEmpty(R))
  8064. {
  8065. MinWidth = 0;
  8066. MinHeight = 0;
  8067. MaxWidth = 32000;
  8068. MaxHeight = 32000;
  8069. }
  8070. }
  8071. //---------------------------------------------------------------------------
  8072. void __fastcall TCustomScpExplorerForm::GetSpaceAvailable(const UnicodeString Path,
  8073. TSpaceAvailable & ASpaceAvailable, bool & Close)
  8074. {
  8075. // terminal can be already closed (e.g. dropped connection)
  8076. if ((Terminal != NULL) && Terminal->IsCapable[fcCheckingSpaceAvailable])
  8077. {
  8078. Configuration->Usage->Inc(L"SpaceAvailableChecks");
  8079. try
  8080. {
  8081. Terminal->SpaceAvailable(Path, ASpaceAvailable);
  8082. }
  8083. catch(...)
  8084. {
  8085. if (!Terminal->Active)
  8086. {
  8087. Close = true;
  8088. }
  8089. throw;
  8090. }
  8091. }
  8092. }
  8093. //---------------------------------------------------------------------------
  8094. void __fastcall TCustomScpExplorerForm::FileSystemInfo()
  8095. {
  8096. const TSessionInfo & SessionInfo = Terminal->GetSessionInfo();
  8097. const TFileSystemInfo & FileSystemInfo = Terminal->GetFileSystemInfo(true);
  8098. TGetSpaceAvailable OnGetSpaceAvailable = NULL;
  8099. if (Terminal->IsCapable[fcCheckingSpaceAvailable])
  8100. {
  8101. OnGetSpaceAvailable = GetSpaceAvailable;
  8102. }
  8103. DoFileSystemInfoDialog(SessionInfo, FileSystemInfo, Terminal->CurrentDirectory,
  8104. OnGetSpaceAvailable);
  8105. }
  8106. //---------------------------------------------------------------------------
  8107. TSessionData * __fastcall TCustomScpExplorerForm::SessionDataForCode()
  8108. {
  8109. std::unique_ptr<TSessionData> Data(CloneCurrentSessionData());
  8110. Terminal->FillSessionDataForCode(Data.get());
  8111. return Data.release();
  8112. }
  8113. //---------------------------------------------------------------------------
  8114. void __fastcall TCustomScpExplorerForm::GenerateUrl(TStrings * Paths)
  8115. {
  8116. std::unique_ptr<TSessionData> Data(SessionDataForCode());
  8117. DoGenerateUrlDialog(Data.get(), Paths);
  8118. }
  8119. //---------------------------------------------------------------------------
  8120. void __fastcall TCustomScpExplorerForm::SessionGenerateUrl()
  8121. {
  8122. GenerateUrl(NULL);
  8123. }
  8124. //---------------------------------------------------------------------------
  8125. void __fastcall TCustomScpExplorerForm::FileGenerateUrl()
  8126. {
  8127. std::unique_ptr<TStrings> Paths(new TStringList());
  8128. DirView(osCurrent)->CreateFileList(false, true, Paths.get());
  8129. GenerateUrl(Paths.get());
  8130. }
  8131. //---------------------------------------------------------------------------
  8132. void __fastcall TCustomScpExplorerForm::SetSessionColor(TColor value)
  8133. {
  8134. if (value != FSessionColor)
  8135. {
  8136. FSessionColor = value;
  8137. TColor C = (value != 0 ? value : Vcl::Graphics::clNone);
  8138. TTBXColorItem * ColorItem = dynamic_cast<TTBXColorItem *>(
  8139. static_cast<TObject *>(GetComponent(fcColorMenu)));
  8140. DebugAssert(ColorItem != NULL);
  8141. ColorItem->Color = C;
  8142. NonVisualDataModule->ColorMenuItem->Color = C;
  8143. // Is null when called from LastTerminalClosed
  8144. if (Terminal != NULL)
  8145. {
  8146. SessionsPageControl->ActivePage->ImageIndex = AddSessionColor(value);
  8147. }
  8148. UpdateControls();
  8149. }
  8150. }
  8151. //---------------------------------------------------------------------------
  8152. bool __fastcall TCustomScpExplorerForm::CancelNote(bool Force)
  8153. {
  8154. bool Result = FNoteTimer->Enabled;
  8155. if (Result)
  8156. {
  8157. // cannot cancel note too early
  8158. bool NotEarly =
  8159. (Now() - FNoteShown >
  8160. EncodeTimeVerbose(0, 0, (unsigned short)(WinConfiguration->NotificationsStickTime), 0));
  8161. if (Force || NotEarly)
  8162. {
  8163. FNoteTimer->Enabled = false;
  8164. FNote = L"";
  8165. SAFE_DESTROY(FNoteData);
  8166. FOnNoteClick = NULL;
  8167. FNoteHints = FNotes->Text;
  8168. FNoteHints.Delete(FNoteHints.Length() - 1, 2);
  8169. UpdateStatusBar();
  8170. }
  8171. }
  8172. return Result;
  8173. }
  8174. //---------------------------------------------------------------------------
  8175. void __fastcall TCustomScpExplorerForm::NoteTimer(TObject * /*Sender*/)
  8176. {
  8177. DebugAssert(FNoteTimer->Enabled);
  8178. CancelNote(true);
  8179. }
  8180. //---------------------------------------------------------------------------
  8181. void __fastcall TCustomScpExplorerForm::AddNote(UnicodeString Note, bool UpdateNow)
  8182. {
  8183. int P = Note.Pos(L"\n");
  8184. if (P > 0)
  8185. {
  8186. Note.SetLength(P - 1);
  8187. }
  8188. FNotes->Add(FORMAT(L"[%s] %s",
  8189. (FormatDateTime(Configuration->TimeFormat, Now()), Note)));
  8190. while (FNotes->Count > 10)
  8191. {
  8192. FNotes->Delete(0);
  8193. }
  8194. if (UpdateNow)
  8195. {
  8196. FNoteHints = FNotes->Text;
  8197. FNoteHints.Delete(FNoteHints.Length() - 1, 2);
  8198. UpdateStatusBar();
  8199. }
  8200. }
  8201. //---------------------------------------------------------------------------
  8202. void __fastcall TCustomScpExplorerForm::PostNote(UnicodeString Note,
  8203. unsigned int Seconds, TNotifyEvent OnNoteClick, TObject * NoteData)
  8204. {
  8205. int P = Note.Pos(L"\n");
  8206. if (P > 0)
  8207. {
  8208. Note.SetLength(P - 1);
  8209. }
  8210. FNoteHints = FNotes->Text;
  8211. FNoteHints.Delete(FNoteHints.Length() - 1, 2);
  8212. FNote = Note;
  8213. SAFE_DESTROY(FNoteData);
  8214. FNoteData = NoteData;
  8215. FOnNoteClick = OnNoteClick;
  8216. AddNote(Note, false);
  8217. UpdateStatusBar();
  8218. FNoteShown = Now();
  8219. FNoteTimer->Enabled = false;
  8220. if (Seconds == 0)
  8221. {
  8222. Seconds = WinConfiguration->NotificationsTimeout;
  8223. }
  8224. FNoteTimer->Interval = Seconds * MSecsPerSec;
  8225. FNoteTimer->Enabled = true;
  8226. }
  8227. //---------------------------------------------------------------------------
  8228. void __fastcall TCustomScpExplorerForm::ReadDirectoryCancelled()
  8229. {
  8230. PostNote(LoadStr(DIRECTORY_READING_CANCELLED), 0, NULL, NULL);
  8231. }
  8232. //---------------------------------------------------------------------------
  8233. void __fastcall TCustomScpExplorerForm::SynchronizeBrowsingChanged()
  8234. {
  8235. if (NonVisualDataModule->SynchronizeBrowsingAction->Checked)
  8236. {
  8237. Configuration->Usage->Inc(L"SynchronizeBrowsingEnabled");
  8238. }
  8239. PostNote(FORMAT(LoadStrPart(SYNC_DIR_BROWSE_TOGGLE, 1),
  8240. (LoadStrPart(SYNC_DIR_BROWSE_TOGGLE,
  8241. (NonVisualDataModule->SynchronizeBrowsingAction->Checked ? 2 : 3)))),
  8242. 0, NULL, NULL);
  8243. }
  8244. //---------------------------------------------------------------------------
  8245. void __fastcall TCustomScpExplorerForm::ToggleShowHiddenFiles()
  8246. {
  8247. WinConfiguration->ShowHiddenFiles = !WinConfiguration->ShowHiddenFiles;
  8248. PostNote(FORMAT(LoadStrPart(SHOW_HIDDEN_FILES_TOGGLE, 1),
  8249. (LoadStrPart(SHOW_HIDDEN_FILES_TOGGLE,
  8250. (WinConfiguration->ShowHiddenFiles ? 2 : 3)))), 0, NULL, NULL);
  8251. }
  8252. //---------------------------------------------------------------------------
  8253. void __fastcall TCustomScpExplorerForm::SetFormatSizeBytes(TFormatBytesStyle Style)
  8254. {
  8255. WinConfiguration->FormatSizeBytes = Style;
  8256. }
  8257. //---------------------------------------------------------------------------
  8258. void __fastcall TCustomScpExplorerForm::ToggleAutoReadDirectoryAfterOp()
  8259. {
  8260. Configuration->AutoReadDirectoryAfterOp = !Configuration->AutoReadDirectoryAfterOp;
  8261. PostNote(FORMAT(LoadStrPart(AUTO_READ_DIRECTORY_TOGGLE, 1),
  8262. (LoadStrPart(AUTO_READ_DIRECTORY_TOGGLE,
  8263. (Configuration->AutoReadDirectoryAfterOp ? 2 : 3)))), 0, NULL, NULL);
  8264. }
  8265. //---------------------------------------------------------------------------
  8266. void __fastcall TCustomScpExplorerForm::StatusBarPanelDblClick(
  8267. TTBXCustomStatusBar * Sender, TTBXStatusPanel * Panel)
  8268. {
  8269. if (Panel->Index == 0)
  8270. {
  8271. if (FOnNoteClick != NULL)
  8272. {
  8273. // prevent the user data from being freed by possible call
  8274. // to CancelNote or PostNote during call to OnNoteClick
  8275. std::unique_ptr<TObject> NoteData(FNoteData);
  8276. TNotifyEvent OnNoteClick = FOnNoteClick;
  8277. FNoteData = NULL;
  8278. // need to cancel the note as we are going to delete its user data
  8279. CancelNote(true);
  8280. OnNoteClick(NoteData.get());
  8281. }
  8282. }
  8283. if (Panel->Index >= Sender->Panels->Count - SessionPanelCount)
  8284. {
  8285. FileSystemInfo();
  8286. }
  8287. }
  8288. //---------------------------------------------------------------------------
  8289. void __fastcall TCustomScpExplorerForm::LockWindow(bool Force)
  8290. {
  8291. // workaround:
  8292. // 1) for unknown reason, disabling window, while minimized,
  8293. // prevents it from restoring, even if it was enabled again meanwhile
  8294. // 2) when disabling the main window, while another has focus
  8295. // minimize is no longer possible
  8296. // ("keep up to date" dialog - though does not seem to be the case with "find file" -
  8297. // so it has probably something to do with the semi-modalness of the "keep up to date" dialog)
  8298. // Shouldn't we use IsApplicationMinimized() here?
  8299. if ((FLockSuspendLevel == 0) && !IsIconic(Application->Handle) && (Force || (Screen->ActiveForm == this)))
  8300. {
  8301. Enabled = false;
  8302. }
  8303. FLockLevel++;
  8304. }
  8305. //---------------------------------------------------------------------------
  8306. void __fastcall TCustomScpExplorerForm::UnlockWindow()
  8307. {
  8308. DebugAssert(FLockLevel > 0);
  8309. FLockLevel--;
  8310. if (FLockLevel == 0)
  8311. {
  8312. DebugAssert(FLockSuspendLevel == 0);
  8313. Enabled = true;
  8314. }
  8315. }
  8316. //---------------------------------------------------------------------------
  8317. void __fastcall TCustomScpExplorerForm::SuspendWindowLock()
  8318. {
  8319. // We need to make sure that window is enabled when the last modal window closes
  8320. // otherwise focus is not restored correctly.
  8321. // So we re-enable the window when modal window opens and
  8322. // disable it back after is closes
  8323. if (FLockLevel > 0)
  8324. {
  8325. // while we have nesting counter, we know that we never be called
  8326. // recursivelly as Application->OnModalBegin is called only
  8327. // for the top-level modal window
  8328. if (DebugAlwaysTrue(FLockSuspendLevel == 0))
  8329. {
  8330. // won't be disabled when conditions in LockWindow() were not satisfied
  8331. FDisabledOnLockSuspend = !Enabled;
  8332. // When minimized to tray (or actually when set to SW_HIDE),
  8333. // setting Enabled makes the window focusable even when there's
  8334. // modal window over it
  8335. if (!FTrayIcon->Visible)
  8336. {
  8337. Enabled = true;
  8338. }
  8339. }
  8340. FLockSuspendLevel++;
  8341. }
  8342. }
  8343. //---------------------------------------------------------------------------
  8344. void __fastcall TCustomScpExplorerForm::ResumeWindowLock()
  8345. {
  8346. if (FLockSuspendLevel > 0)
  8347. {
  8348. DebugAssert(FLockLevel > 0);
  8349. FLockSuspendLevel--;
  8350. // see comment in SuspendWindowLock
  8351. if (DebugAlwaysTrue(FLockSuspendLevel == 0))
  8352. {
  8353. // Note that window can be enabled here, when we were minized to tray when
  8354. // was SuspendWindowLock() called.
  8355. // We should possibly do the same check as in LockWindow(),
  8356. // if it is ever possible that the consitions change between
  8357. // SuspendWindowLock() and ResumeWindowLock()
  8358. Enabled = !FDisabledOnLockSuspend;
  8359. }
  8360. }
  8361. }
  8362. //---------------------------------------------------------------------------
  8363. void __fastcall TCustomScpExplorerForm::UpdateRemotePathComboBox(bool TextOnly)
  8364. {
  8365. if (!TextOnly)
  8366. {
  8367. TTBXComboBoxItem * RemotePathComboBox =
  8368. reinterpret_cast<TTBXComboBoxItem *>(GetComponent(fcRemotePathComboBox));
  8369. TStrings * Items = RemotePathComboBox->Strings;
  8370. Items->BeginUpdate();
  8371. try
  8372. {
  8373. Items->Clear();
  8374. if (Terminal != NULL)
  8375. {
  8376. UnicodeString APath = UnixExcludeTrailingBackslash(RemoteDirView->Path);
  8377. while (!IsUnixRootPath(APath))
  8378. {
  8379. int P = APath.LastDelimiter(L'/');
  8380. DebugAssert(P >= 0);
  8381. Items->Insert(0, APath.SubString(P + 1, APath.Length() - P));
  8382. APath.SetLength(P - 1);
  8383. }
  8384. Items->Insert(0, Customunixdirview_SUnixDefaultRootName);
  8385. }
  8386. }
  8387. __finally
  8388. {
  8389. RemotePathComboBox->ItemIndex = Items->Count - 1;
  8390. // Setting ItemIndex to -1 does not reset its text
  8391. if (Items->Count == 0)
  8392. {
  8393. RemotePathComboBox->Text = L"";
  8394. }
  8395. Items->EndUpdate();
  8396. }
  8397. }
  8398. }
  8399. //---------------------------------------------------------------------------
  8400. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxAdjustImageIndex(
  8401. TTBXComboBoxItem * Sender, const UnicodeString /*AText*/, int AIndex,
  8402. int & ImageIndex)
  8403. {
  8404. if (AIndex < 0)
  8405. {
  8406. AIndex = Sender->ItemIndex;
  8407. }
  8408. ImageIndex = (AIndex < Sender->Strings->Count - 1 ? StdDirIcon : StdDirSelIcon);
  8409. }
  8410. //---------------------------------------------------------------------------
  8411. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxDrawItem(
  8412. TTBXCustomList * /*Sender*/, TCanvas * /*ACanvas*/, TRect & ARect, int AIndex,
  8413. int /*AHoverIndex*/, bool & /*DrawDefault*/)
  8414. {
  8415. ARect.Left += (10 * AIndex);
  8416. }
  8417. //---------------------------------------------------------------------------
  8418. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxMeasureWidth(
  8419. TTBXCustomList * /*Sender*/, TCanvas * /*ACanvas*/, int AIndex, int &AWidth)
  8420. {
  8421. AWidth += (10 * AIndex);
  8422. }
  8423. //---------------------------------------------------------------------------
  8424. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxItemClick(
  8425. TObject * Sender)
  8426. {
  8427. TTBXComboBoxItem * RemotePathComboBox = dynamic_cast<TTBXComboBoxItem*>(Sender);
  8428. UnicodeString APath = UnixExcludeTrailingBackslash(RemoteDirView->Path);
  8429. int Index = RemotePathComboBox->ItemIndex;
  8430. while (Index < RemotePathComboBox->Strings->Count - 1)
  8431. {
  8432. APath = UnixExtractFileDir(APath);
  8433. Index++;
  8434. }
  8435. // VanDyke style paths
  8436. if (APath.IsEmpty())
  8437. {
  8438. DebugAssert(RemotePathComboBox->ItemIndex == 0);
  8439. APath = ROOTDIRECTORY;
  8440. }
  8441. if (RemoteDirView->Path != APath)
  8442. {
  8443. RemoteDirView->Path = APath;
  8444. }
  8445. }
  8446. //---------------------------------------------------------------------------
  8447. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxCancel(TObject * Sender)
  8448. {
  8449. DebugAssert(Sender == GetComponent(fcRemotePathComboBox));
  8450. DebugUsedParam(Sender);
  8451. UpdateRemotePathComboBox(true);
  8452. }
  8453. //---------------------------------------------------------------------------
  8454. void __fastcall TCustomScpExplorerForm::DirViewEditing(
  8455. TObject * Sender, TListItem * Item, bool & /*AllowEdit*/)
  8456. {
  8457. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  8458. DebugAssert(DirView != NULL);
  8459. if (!WinConfiguration->RenameWholeName && !DirView->ItemIsDirectory(Item))
  8460. {
  8461. HWND Edit = ListView_GetEditControl(DirView->Handle);
  8462. // OnEditing is called also from TCustomListView::CanEdit
  8463. if (Edit != NULL)
  8464. {
  8465. EditSelectBaseName(Edit);
  8466. }
  8467. }
  8468. }
  8469. //---------------------------------------------------------------------------
  8470. //---------------------------------------------------------------------------
  8471. TDragDropFilesEx * __fastcall TCustomScpExplorerForm::CreateDragDropFilesEx()
  8472. {
  8473. TDragDropFilesEx * Result = new TDragDropFilesEx(this);
  8474. Result->AutoDetectDnD = false;
  8475. Result->NeedValid = TFileExMustDnDSet() << nvFilename;
  8476. Result->RenderDataOn = rdoEnterAndDropSync;
  8477. Result->TargetEffects = TDropEffectSet() << deCopy << deMove;
  8478. return Result;
  8479. }
  8480. //---------------------------------------------------------------------------
  8481. void __fastcall TCustomScpExplorerForm::CreateWnd()
  8482. {
  8483. TForm::CreateWnd();
  8484. if (FSessionsDragDropFilesEx == NULL)
  8485. {
  8486. FSessionsDragDropFilesEx = CreateDragDropFilesEx();
  8487. FSessionsDragDropFilesEx->OnDragOver = SessionsDDDragOver;
  8488. FSessionsDragDropFilesEx->OnProcessDropped = SessionsDDProcessDropped;
  8489. FSessionsDragDropFilesEx->OnDragEnter = SessionsDDDragEnter;
  8490. FSessionsDragDropFilesEx->OnDragLeave = SessionsDDDragLeave;
  8491. }
  8492. if (FQueueDragDropFilesEx == NULL)
  8493. {
  8494. FQueueDragDropFilesEx = CreateDragDropFilesEx();
  8495. // No need to set OnDragOver as we do not have any restrictions
  8496. FQueueDragDropFilesEx->OnProcessDropped = QueueDDProcessDropped;
  8497. FQueueDragDropFilesEx->OnDragEnter = QueueDDDragEnter;
  8498. FQueueDragDropFilesEx->OnDragLeave = QueueDDDragLeave;
  8499. }
  8500. }
  8501. //---------------------------------------------------------------------------
  8502. void __fastcall TCustomScpExplorerForm::DestroyWnd()
  8503. {
  8504. TForm::DestroyWnd();
  8505. FSessionsDragDropFilesEx->DragDropControl = NULL;
  8506. FQueueDragDropFilesEx->DragDropControl = NULL;
  8507. }
  8508. //---------------------------------------------------------------------------
  8509. void __fastcall TCustomScpExplorerForm::FormShow(TObject * /*Sender*/)
  8510. {
  8511. SideEnter(FCurrentSide);
  8512. FEverShown = true;
  8513. }
  8514. //---------------------------------------------------------------------------
  8515. void __fastcall TCustomScpExplorerForm::DoFindFiles(
  8516. TTerminal * ATerminal, UnicodeString Directory, const TFileMasks & FileMask,
  8517. TFileFoundEvent OnFileFound, TFindingFileEvent OnFindingFile)
  8518. {
  8519. if (!NonVisualDataModule->Busy)
  8520. {
  8521. TTerminalManager::Instance()->ActiveTerminal = ATerminal;
  8522. Configuration->Usage->Inc(L"FileFinds");
  8523. LockWindow(true);
  8524. NonVisualDataModule->StartBusy();
  8525. WinConfiguration->LockedInterface = true;
  8526. try
  8527. {
  8528. FTerminal->FilesFind(Directory, FileMask, OnFileFound, OnFindingFile);
  8529. }
  8530. __finally
  8531. {
  8532. WinConfiguration->LockedInterface = false;
  8533. NonVisualDataModule->EndBusy();
  8534. UnlockWindow();
  8535. }
  8536. }
  8537. }
  8538. //---------------------------------------------------------------------------
  8539. void __fastcall TCustomScpExplorerForm::DoFocusRemotePath(TTerminal * ATerminal, const UnicodeString & Path)
  8540. {
  8541. if (!NonVisualDataModule->Busy)
  8542. {
  8543. TTerminalManager::Instance()->ActiveTerminal = ATerminal;
  8544. SetFocus();
  8545. RemoteDirView->Path = UnixExtractFilePath(Path);
  8546. TListItem * Item = RemoteDirView->FindFileItem(UnixExtractFileName(Path));
  8547. if (Item != NULL)
  8548. {
  8549. RemoteDirView->ItemFocused = Item;
  8550. RemoteDirView->ItemFocused->MakeVisible(false);
  8551. RemoteDirView->SetFocus();
  8552. }
  8553. }
  8554. }
  8555. //---------------------------------------------------------------------------
  8556. bool __fastcall TCustomScpExplorerForm::CanOperateOnFoundFiles(TTerminal * ATerminal)
  8557. {
  8558. bool Result = !NonVisualDataModule->Busy;
  8559. if (Result)
  8560. {
  8561. TTerminalManager::Instance()->ActiveTerminal = ATerminal;
  8562. }
  8563. return Result;
  8564. }
  8565. //---------------------------------------------------------------------------
  8566. void __fastcall TCustomScpExplorerForm::DoOperationOnFoundFiles(
  8567. TFileOperation Operation, TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  8568. {
  8569. if (CanOperateOnFoundFiles(ATerminal))
  8570. {
  8571. TValueRestorer<TFileOperationFinishedEvent> OnFileOperationFinishedRestorer(FOnFileOperationFinished);
  8572. FOnFileOperationFinished = OnFileOperationFinished;
  8573. ExecuteFileOperation(Operation, osRemote, FileList, false, NULL);
  8574. }
  8575. }
  8576. //---------------------------------------------------------------------------
  8577. void __fastcall TCustomScpExplorerForm::DoDeleteFoundFiles(
  8578. TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  8579. {
  8580. DoOperationOnFoundFiles(foDelete, ATerminal, FileList, OnFileOperationFinished);
  8581. }
  8582. //---------------------------------------------------------------------------
  8583. void __fastcall TCustomScpExplorerForm::DoDownloadFoundFiles(
  8584. TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  8585. {
  8586. DoOperationOnFoundFiles(foCopy, ATerminal, FileList, OnFileOperationFinished);
  8587. }
  8588. //---------------------------------------------------------------------------
  8589. void __fastcall TCustomScpExplorerForm::DoEditFoundFiles(
  8590. TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  8591. {
  8592. if (CanOperateOnFoundFiles(ATerminal))
  8593. {
  8594. for (int Index = 0; Index < FileList->Count; Index++)
  8595. {
  8596. UnicodeString FileName = FileList->Strings[Index];
  8597. TRemoteFile * File = static_cast<TRemoteFile *>(FileList->Objects[Index]);
  8598. ExecuteRemoteFile(FileName, File, efDefaultEditor);
  8599. OnFileOperationFinished(FileName, true);
  8600. }
  8601. }
  8602. }
  8603. //---------------------------------------------------------------------------
  8604. void __fastcall TCustomScpExplorerForm::RemoteFindFiles()
  8605. {
  8606. FFileFindTerminal = Terminal;
  8607. ShowFileFindDialog(
  8608. Terminal, RemoteDirView->Path, DoFindFiles, DoFocusRemotePath, DoDeleteFoundFiles,
  8609. DoDownloadFoundFiles, DoEditFoundFiles);
  8610. }
  8611. //---------------------------------------------------------------------------
  8612. void __fastcall TCustomScpExplorerForm::UpdateTaskbarList(ITaskbarList3 * TaskbarList)
  8613. {
  8614. FTaskbarList = TaskbarList;
  8615. }
  8616. //---------------------------------------------------------------------------
  8617. void __fastcall TCustomScpExplorerForm::SessionsPageControlMouseDown(
  8618. TObject * /*Sender*/, TMouseButton Button, TShiftState /*Shift*/, int X, int Y)
  8619. {
  8620. int Index = SessionsPageControl->IndexOfTabAt(X, Y);
  8621. if (Index >= 0)
  8622. {
  8623. if (Button == mbLeft)
  8624. {
  8625. // "Mouse down" is raised only after tab is switched.
  8626. // If switching tab (switching session) takes long enough for user
  8627. // to actually release the button, "mouse down" is still raised,
  8628. // but we do not get "mouse up" event, so dragging is not cancelled,
  8629. // prevent that by not beginning dragging in the first place.
  8630. if (IsKeyPressed(VK_LBUTTON))
  8631. {
  8632. // when user clicks the "+", we get mouse down only after the session
  8633. // is closed, when new session tab is already on X:Y, so dragging
  8634. // starts, prevent that
  8635. if (MilliSecondsBetween(Now(), FSessionsPageControlNewSessionTime) > 500)
  8636. {
  8637. TTerminal * Terminal = GetSessionTabTerminal(SessionsPageControl->Pages[Index]);
  8638. if (Terminal != NULL)
  8639. {
  8640. SessionsPageControl->BeginDrag(false);
  8641. }
  8642. }
  8643. }
  8644. }
  8645. else if (Button == mbMiddle)
  8646. {
  8647. // ignore middle-click for "New session tab"
  8648. TTerminal * Terminal = GetSessionTabTerminal(SessionsPageControl->Pages[Index]);
  8649. if (Terminal != NULL)
  8650. {
  8651. SessionsPageControl->ActivePageIndex = Index;
  8652. // Switch to session tab (has to be session tab, due to previous check)
  8653. if (DebugAlwaysTrue(SessionTabSwitched()))
  8654. {
  8655. CloseSession();
  8656. }
  8657. }
  8658. }
  8659. }
  8660. }
  8661. //---------------------------------------------------------------------------
  8662. void __fastcall TCustomScpExplorerForm::SessionsPageControlDragDrop(
  8663. TObject * /*Sender*/, TObject * /*Source*/, int X, int Y)
  8664. {
  8665. int Index = SessionsPageControl->IndexOfTabAt(X, Y);
  8666. // do not allow dropping on the "+" tab
  8667. TTerminal * TargetTerminal = GetSessionTabTerminal(SessionsPageControl->Pages[Index]);
  8668. if ((TargetTerminal != NULL) &&
  8669. (SessionsPageControl->ActivePage->PageIndex != Index))
  8670. {
  8671. Configuration->Usage->Inc(L"SessionTabMoves");
  8672. // this is almost redundant as we would recreate tabs in DoTerminalListChanged,
  8673. // but we want to actually prevent that to avoid flicker
  8674. SessionsPageControl->ActivePage->PageIndex = Index;
  8675. TTerminal * Terminal = GetSessionTabTerminal(SessionsPageControl->ActivePage);
  8676. TTerminalManager::Instance()->Move(Terminal, TargetTerminal);
  8677. }
  8678. }
  8679. //---------------------------------------------------------------------------
  8680. void __fastcall TCustomScpExplorerForm::SessionsPageControlDragOver(
  8681. TObject * Sender, TObject * Source, int X, int Y,
  8682. TDragState /*State*/, bool & Accept)
  8683. {
  8684. Accept = (Sender == Source);
  8685. if (Accept)
  8686. {
  8687. int Index = SessionsPageControl->IndexOfTabAt(X, Y);
  8688. TTerminal * Terminal = GetSessionTabTerminal(SessionsPageControl->Pages[Index]);
  8689. // do not allow dragging to the "+" tab
  8690. Accept = (Terminal != NULL);
  8691. }
  8692. }
  8693. //---------------------------------------------------------------------------
  8694. void __fastcall TCustomScpExplorerForm::SessionsDDDragOver(int /*KeyState*/,
  8695. const TPoint & Point, int & Effect)
  8696. {
  8697. int Index = SessionsPageControl->IndexOfTabAt(Point.X, Point.Y);
  8698. if (Index < 0)
  8699. {
  8700. Effect = DROPEFFECT_None;
  8701. }
  8702. else
  8703. {
  8704. TTerminal * TargetTerminal = GetSessionTabTerminal(SessionsPageControl->Pages[Index]);
  8705. // do not allow dropping on the "+" tab
  8706. if (TargetTerminal == NULL)
  8707. {
  8708. Effect = DROPEFFECT_None;
  8709. }
  8710. }
  8711. }
  8712. //---------------------------------------------------------------------------
  8713. void __fastcall TCustomScpExplorerForm::SessionsDDProcessDropped(
  8714. TObject * /*Sender*/, int /*KeyState*/, const TPoint & Point, int Effect)
  8715. {
  8716. int Index = SessionsPageControl->IndexOfTabAt(Point.X, Point.Y);
  8717. // do not allow dropping on the "+" tab
  8718. TTerminal * TargetTerminal = GetSessionTabTerminal(SessionsPageControl->Pages[Index]);
  8719. if (TargetTerminal != NULL)
  8720. {
  8721. DebugAssert(!IsFileControl(DropSourceControl, osRemote));
  8722. if (!IsFileControl(DropSourceControl, osRemote))
  8723. {
  8724. TTerminalManager::Instance()->ActiveTerminal = TargetTerminal;
  8725. RemoteFileControlDragDropFileOperation(SessionsPageControl, Effect,
  8726. // Why don't we use Terminal->CurrentDirectory directly?
  8727. TTerminalManager::Instance()->ActiveTerminal->CurrentDirectory, false);
  8728. }
  8729. }
  8730. }
  8731. //---------------------------------------------------------------------------
  8732. void __fastcall TCustomScpExplorerForm::QueueDDProcessDropped(
  8733. TObject * /*Sender*/, int /*KeyState*/, const TPoint & /*Point*/, int Effect)
  8734. {
  8735. // Downloads are handled in RemoteFileControlDDEnd
  8736. if (!IsFileControl(DropSourceControl, osRemote))
  8737. {
  8738. RemoteFileControlDragDropFileOperation(QueueView3, Effect,
  8739. Terminal->CurrentDirectory,
  8740. // force queue
  8741. true);
  8742. }
  8743. }
  8744. //---------------------------------------------------------------------------
  8745. void __fastcall TCustomScpExplorerForm::FormClose(TObject * /*Sender*/, TCloseAction & /*Action*/)
  8746. {
  8747. FShowing = false;
  8748. // Do not save empty workspace
  8749. if (WinConfiguration->AutoSaveWorkspace && (Terminal != NULL))
  8750. {
  8751. std::unique_ptr<TObjectList> DataList(DoCollectWorkspace());
  8752. UnicodeString Name = WorkspaceName();
  8753. DoSaveWorkspace(
  8754. Name, DataList.get(),
  8755. !Configuration->DisablePasswordStoring &&
  8756. WinConfiguration->AutoSaveWorkspacePasswords, false);
  8757. WinConfiguration->LastStoredSession = Name;
  8758. }
  8759. }
  8760. //---------------------------------------------------------------------------
  8761. void __fastcall TCustomScpExplorerForm::RemoteDirViewRead(TObject * /*Sender*/)
  8762. {
  8763. TManagedTerminal * ManagedTerminal =
  8764. dynamic_cast<TManagedTerminal *>(RemoteDirView->Terminal);
  8765. if (ManagedTerminal != NULL)
  8766. {
  8767. ManagedTerminal->DirectoryLoaded = Now();
  8768. }
  8769. }
  8770. //---------------------------------------------------------------------------
  8771. void __fastcall TCustomScpExplorerForm::DirViewSelectItem(TObject * Sender,
  8772. TListItem * /*Item*/, bool /*Selected*/)
  8773. {
  8774. TCustomDirView * DirView = reinterpret_cast<TCustomDirView *>(Sender);
  8775. switch (DirView->LastSelectMethod)
  8776. {
  8777. case smKeyboard:
  8778. Configuration->Usage->Inc(L"KeyboardSelections");
  8779. break;
  8780. case smMouse:
  8781. Configuration->Usage->Inc(L"MouseSelections");
  8782. break;
  8783. }
  8784. }
  8785. //---------------------------------------------------------------------------
  8786. int __fastcall TCustomScpExplorerForm::AddSessionColor(TColor Color)
  8787. {
  8788. if (Color != 0)
  8789. {
  8790. AddSessionColorImage(FSessionColors, Color, FSessionColorMaskImageIndex);
  8791. return FSessionColors->Count - 1;
  8792. }
  8793. else
  8794. {
  8795. return FSessionTabImageIndex;
  8796. }
  8797. }
  8798. //---------------------------------------------------------------------------
  8799. int __fastcall TCustomScpExplorerForm::AddFixedSessionImage(int GlyphsSourceIndex)
  8800. {
  8801. TPngImageCollectionItem * Item =
  8802. GlyphsModule->ExplorerImages->PngImages->Items[GlyphsSourceIndex];
  8803. FSessionColors->AddPng(Item->PngImage);
  8804. return FSessionColors->Count - 1;
  8805. }
  8806. //---------------------------------------------------------------------------
  8807. void __fastcall TCustomScpExplorerForm::AddFixedSessionImages()
  8808. {
  8809. FSessionColors->SetSize(GlyphsModule->ExplorerImages->Width, GlyphsModule->ExplorerImages->Height);
  8810. FNewSessionTabImageIndex = AddFixedSessionImage(NonVisualDataModule->NewSessionAction->ImageIndex);
  8811. FSessionTabImageIndex = AddFixedSessionImage(SiteImageIndex);
  8812. FSessionColorMaskImageIndex = AddFixedSessionImage(SiteColorMaskImageIndex);
  8813. }
  8814. //---------------------------------------------------------------------------
  8815. void __fastcall TCustomScpExplorerForm::CollectItemsWithTextDisplayMode(TWinControl * Control)
  8816. {
  8817. for (int Index = 0; Index < Control->ControlCount; Index++)
  8818. {
  8819. TControl * AControl = Control->Controls[Index];
  8820. TWinControl * WinControl = dynamic_cast<TWinControl *>(AControl);
  8821. if (WinControl != NULL)
  8822. {
  8823. CollectItemsWithTextDisplayMode(WinControl);
  8824. }
  8825. TTBCustomToolbar * Toolbar = dynamic_cast<TTBCustomToolbar *>(AControl);
  8826. if (Toolbar != NULL)
  8827. {
  8828. // we care for top-level items only
  8829. TTBCustomItem * Items = Toolbar->Items;
  8830. for (int ItemIndex = 0; ItemIndex < Items->Count; ItemIndex++)
  8831. {
  8832. TTBCustomItem * Item = Items->Items[ItemIndex];
  8833. if (((Item->DisplayMode == nbdmImageAndText) ||
  8834. (dynamic_cast<TTBXLabelItem *>(Item) != NULL)) &&
  8835. EligibleForImageDisplayMode(Item))
  8836. {
  8837. FItemsWithTextDisplayMode.insert(Item);
  8838. }
  8839. }
  8840. }
  8841. }
  8842. }
  8843. //---------------------------------------------------------------------------
  8844. bool __fastcall TCustomScpExplorerForm::EligibleForImageDisplayMode(TTBCustomItem * /*Item*/)
  8845. {
  8846. return true;
  8847. }
  8848. //---------------------------------------------------------------------------
  8849. bool __fastcall TCustomScpExplorerForm::UpdateToolbarDisplayMode()
  8850. {
  8851. bool SelectiveToolbarText = WinConfiguration->SelectiveToolbarText;
  8852. TTBItemDisplayMode DisplayMode = (SelectiveToolbarText ? nbdmImageAndText : nbdmDefault);
  8853. typedef std::set<TTBCustomItem *> TItemsWithTextDisplayMode;
  8854. TItemsWithTextDisplayMode::iterator i = FItemsWithTextDisplayMode.begin();
  8855. bool Result = true;
  8856. while (Result && (i != FItemsWithTextDisplayMode.end()))
  8857. {
  8858. TTBCustomItem * Item = *i;
  8859. TTBXLabelItem * Label = dynamic_cast<TTBXLabelItem *>(Item);
  8860. if (Label != NULL)
  8861. {
  8862. // optimization
  8863. if (Label->Visible == SelectiveToolbarText)
  8864. {
  8865. Result = false;
  8866. }
  8867. else
  8868. {
  8869. Label->Visible = SelectiveToolbarText;
  8870. }
  8871. }
  8872. else
  8873. {
  8874. // optimization
  8875. if (Item->DisplayMode == DisplayMode)
  8876. {
  8877. Result = false;
  8878. }
  8879. else
  8880. {
  8881. Item->DisplayMode = DisplayMode;
  8882. }
  8883. }
  8884. ++i;
  8885. }
  8886. if (Result)
  8887. {
  8888. UpdateNewSessionTab();
  8889. }
  8890. return Result;
  8891. }
  8892. //---------------------------------------------------------------------------
  8893. void __fastcall TCustomScpExplorerForm::DisplaySystemContextMenu()
  8894. {
  8895. DebugFail();
  8896. }
  8897. //---------------------------------------------------------------------------
  8898. bool __fastcall TCustomScpExplorerForm::IsBusy()
  8899. {
  8900. // Among other, a lock level is non zero, while directory is being loaded,
  8901. // even when it happens as a result of dirview navigation,
  8902. // i.e. when TNonVisualDataModule is NOT FBusy.
  8903. // That's why the TNonVisualDataModule::GetBusy calls this method.
  8904. // Among other this prevents a panel auto update to occur while
  8905. // directory is changing.
  8906. return (FLockLevel > 0) || DirView(osCurrent)->IsEditing();
  8907. }
  8908. //---------------------------------------------------------------------------
  8909. Boolean __fastcall TCustomScpExplorerForm::AllowedAction(TAction * /*Action*/, TActionAllowed Allowed)
  8910. {
  8911. // While the window is disabled, we still seem to process menu shortcuts at least,
  8912. // so stop it at least here.
  8913. // See also TCustomScpExplorerForm::RemoteDirViewBusy
  8914. return
  8915. (Allowed == aaUpdate) ||
  8916. !NonVisualDataModule->Busy;
  8917. }
  8918. //---------------------------------------------------------------------------
  8919. void __fastcall TCustomScpExplorerForm::EditMenuItemPopup(TTBCustomItem * Sender, bool FromLink)
  8920. {
  8921. NonVisualDataModule->EditMenuItemPopup(Sender, FromLink);
  8922. }
  8923. //---------------------------------------------------------------------------
  8924. void __fastcall TCustomScpExplorerForm::DirViewBusy(TObject * /*Sender*/, int Busy, bool & State)
  8925. {
  8926. // This is somewhat redundant to LockWindow() call from
  8927. // TTerminalManager::TerminalReadDirectoryProgress.
  8928. // But disabling window is known not to block keyboard shorcuts
  8929. // (see TCustomScpExplorerForm::AllowedAction), this hopefully works.
  8930. if (Busy > 0)
  8931. {
  8932. if (NonVisualDataModule->Busy)
  8933. {
  8934. State = false;
  8935. }
  8936. else
  8937. {
  8938. NonVisualDataModule->StartBusy();
  8939. // Will be countered for TCustomDirView.DoExecute in DoDirViewExecFile
  8940. LockWindow();
  8941. }
  8942. }
  8943. else if (Busy < 0)
  8944. {
  8945. UnlockWindow();
  8946. NonVisualDataModule->EndBusy();
  8947. }
  8948. else
  8949. {
  8950. State = NonVisualDataModule->Busy;
  8951. }
  8952. }
  8953. //---------------------------------------------------------------------------
  8954. void __fastcall TCustomScpExplorerForm::SessionsPageControlContextPopup(TObject * /*Sender*/, TPoint & MousePos, bool & Handled)
  8955. {
  8956. int Index = SessionsPageControl->IndexOfTabAt(MousePos.X, MousePos.Y);
  8957. // no context menu for "New session tab"
  8958. if ((Index >= 0) && (GetSessionTabTerminal(SessionsPageControl->Pages[Index]) != NULL))
  8959. {
  8960. SessionsPageControl->ActivePageIndex = Index;
  8961. if (DebugAlwaysTrue(SessionTabSwitched()))
  8962. {
  8963. // copied from TControl.WMContextMenu
  8964. SendCancelMode(SessionsPageControl);
  8965. // explicit popup instead of using PopupMenu property
  8966. // to avoid menu to popup somewhere within SessionTabSwitched above,
  8967. // while connecting yet not-connected session and hence
  8968. // allowing an access to commands over not-completelly connected session
  8969. TPoint Point = SessionsPageControl->ClientToScreen(MousePos);
  8970. TPopupMenu * PopupMenu = NonVisualDataModule->SessionsPopup;
  8971. PopupMenu->PopupComponent = SessionsPageControl;
  8972. PopupMenu->Popup(Point.x, Point.y);
  8973. }
  8974. }
  8975. Handled = true;
  8976. }
  8977. //---------------------------------------------------------------------------
  8978. bool __fastcall TCustomScpExplorerForm::CanChangePassword()
  8979. {
  8980. return (Terminal != NULL) && Terminal->Active && Terminal->IsCapable[fcChangePassword];
  8981. }
  8982. //---------------------------------------------------------------------------
  8983. void __fastcall TCustomScpExplorerForm::ChangePassword()
  8984. {
  8985. Configuration->Usage->Inc(L"PasswordChanges");
  8986. std::unique_ptr<TSessionData> ChangePasswordData(Terminal->SessionData->Clone());
  8987. ChangePasswordData->DisableAuthentationsExceptPassword();
  8988. ChangePasswordData->ChangePassword = true;
  8989. std::unique_ptr<TTerminal> ChangePasswordSession(
  8990. Terminal->CreateSecondarySession(L"ChangePassword", ChangePasswordData.get()));
  8991. ChangePasswordSession->AutoReadDirectory = false;
  8992. if (TTerminalManager::Instance()->ConnectTerminal(ChangePasswordSession.get()))
  8993. {
  8994. MessageDialog(MainInstructions(LoadStr(PASSWORD_CHANGED)), qtInformation, qaOK, HELP_CHANGE_PASSWORD);
  8995. }
  8996. else
  8997. {
  8998. Configuration->Usage->Inc(L"PasswordChangesFailed");
  8999. }
  9000. }
  9001. //---------------------------------------------------------------------------
  9002. bool __fastcall TCustomScpExplorerForm::CanPrivateKeyUpload()
  9003. {
  9004. // No nice way to assert SSH2
  9005. return (Terminal != NULL) && Terminal->Active && (Terminal->FSProtocol == cfsSFTP);
  9006. }
  9007. //---------------------------------------------------------------------------
  9008. void __fastcall TCustomScpExplorerForm::PrivateKeyUpload()
  9009. {
  9010. TOperationVisualizer OperationVisualizer;
  9011. UnicodeString FileName = Terminal->SessionData->PublicKeyFile;
  9012. TTerminalManager::Instance()->UploadPublicKey(Terminal, NULL, FileName);
  9013. }
  9014. //---------------------------------------------------------------------------
  9015. void __fastcall TCustomScpExplorerForm::ChangeScale(int M, int D)
  9016. {
  9017. TForm::ChangeScale(M, D);
  9018. int APixelsPerInch = GetControlPixelsPerInch(this);
  9019. HANDLE ResourceModule = GUIConfiguration->ChangeToDefaultResourceModule();
  9020. try
  9021. {
  9022. GlyphsModule->SetPixelsPerInch(APixelsPerInch);
  9023. }
  9024. __finally
  9025. {
  9026. GUIConfiguration->ChangeResourceModule(ResourceModule);
  9027. }
  9028. }
  9029. //---------------------------------------------------------------------------
  9030. void __fastcall TCustomScpExplorerForm::DockContextPopup(TObject * Sender, TPoint & MousePos, bool & /*Handled*/)
  9031. {
  9032. NonVisualDataModule->ControlContextPopup(Sender, MousePos);
  9033. }
  9034. //---------------------------------------------------------------------
  9035. class TPublicControl : public TControl
  9036. {
  9037. friend class TCustomScpExplorerForm;
  9038. };
  9039. //---------------------------------------------------------------------------
  9040. void __fastcall TCustomScpExplorerForm::CopyPopup(TControl * DestControl, TControl * SourceControl)
  9041. {
  9042. static_cast<TPublicControl *>(DestControl)->PopupMenu = static_cast<TPublicControl *>(SourceControl)->PopupMenu;
  9043. static_cast<TPublicControl *>(DestControl)->OnContextPopup = static_cast<TPublicControl *>(SourceControl)->OnContextPopup;
  9044. }
  9045. //---------------------------------------------------------------------------
  9046. void __fastcall TCustomScpExplorerForm::DoBookmarkClick(TOperationSide Side, TObject * Sender)
  9047. {
  9048. TBookmark * Bookmark = DebugNotNull(reinterpret_cast<TBookmark *>(dynamic_cast<TTBCustomItem *>(Sender)->Tag));
  9049. OpenBookmark(Side, Bookmark);
  9050. }
  9051. //---------------------------------------------------------------------------
  9052. void __fastcall TCustomScpExplorerForm::LocalBookmarkClick(TObject * Sender)
  9053. {
  9054. DoBookmarkClick(osLocal, Sender);
  9055. }
  9056. //---------------------------------------------------------------------------
  9057. void __fastcall TCustomScpExplorerForm::RemoteBookmarkClick(TObject * Sender)
  9058. {
  9059. DoBookmarkClick(osRemote, Sender);
  9060. }
  9061. //---------------------------------------------------------------------------
  9062. void __fastcall TCustomScpExplorerForm::CreateOpenDirMenuList(
  9063. TTBCustomItem * Menu, TOperationSide Side, TBookmarkList * BookmarkList)
  9064. {
  9065. TNotifyEvent OnBookmarkClick = (Side == osLocal) ? &LocalBookmarkClick : &RemoteBookmarkClick;
  9066. if (!WinConfiguration->UseLocationProfiles)
  9067. {
  9068. std::unique_ptr<TStringList> Directories(new TStringList());
  9069. Directories->CaseSensitive = (Side == osRemote);
  9070. for (int Index = 0; Index < BookmarkList->Count; Index++)
  9071. {
  9072. TBookmark * Bookmark = BookmarkList->Bookmarks[Index];
  9073. UnicodeString Directory = Bookmark->GetSideDirectory(Side);
  9074. if (!Directory.IsEmpty() && (Directories->IndexOf(Directory) < 0))
  9075. {
  9076. std::unique_ptr<TTBCustomItem> Item(new TTBXItem(Owner));
  9077. Item->Caption = EscapeHotkey(Directory);
  9078. Item->ShortCut = Bookmark->ShortCut;
  9079. Item->OnClick = OnBookmarkClick;
  9080. Item->Tag = reinterpret_cast<int>(Bookmark);
  9081. Directories->Add(Directory);
  9082. Menu->Add(Item.release());
  9083. }
  9084. }
  9085. }
  9086. else
  9087. {
  9088. std::unique_ptr<TStrings> Folders(CreateSortedStringList());
  9089. for (int Index = 0; Index < BookmarkList->Count; Index++)
  9090. {
  9091. TBookmark * Bookmark = BookmarkList->Bookmarks[Index];
  9092. if (!Bookmark->Node.IsEmpty())
  9093. {
  9094. Folders->Add(Bookmark->Node);
  9095. }
  9096. }
  9097. for (int Index = 0; Index < Folders->Count; Index++)
  9098. {
  9099. std::unique_ptr<TTBCustomItem> Item(new TTBXSubmenuItem(Owner));
  9100. Item->Caption = Folders->Strings[Index];
  9101. Item->ImageIndex = NonVisualDataModule->RemoteChangePathAction->ImageIndex;
  9102. Folders->Objects[Index] = Item.get();
  9103. Menu->Add(Item.release());
  9104. }
  9105. for (int Index = 0; Index < BookmarkList->Count; Index++)
  9106. {
  9107. TBookmark * Bookmark = BookmarkList->Bookmarks[Index];
  9108. TTBCustomItem * Parent;
  9109. if (!Bookmark->Node.IsEmpty())
  9110. {
  9111. DebugAssert(Folders->IndexOf(Bookmark->Node) >= 0);
  9112. Parent = dynamic_cast<TTBCustomItem *>(Folders->Objects[Folders->IndexOf(Bookmark->Node)]);
  9113. }
  9114. else
  9115. {
  9116. Parent = Menu;
  9117. }
  9118. std::unique_ptr<TTBCustomItem> Item(new TTBXItem(Owner));
  9119. Item->Caption = Bookmark->Name;
  9120. Item->ShortCut = Bookmark->ShortCut;
  9121. Item->OnClick = OnBookmarkClick;
  9122. Item->Tag = reinterpret_cast<int>(Bookmark);
  9123. if (((Bookmark->Name == Bookmark->Local) || (Bookmark->Name == Bookmark->Remote)) &&
  9124. (Bookmark->Local.IsEmpty() || Bookmark->Remote.IsEmpty()))
  9125. {
  9126. // No hint for location profiles that are actually mere "bookmarks"
  9127. }
  9128. else
  9129. {
  9130. UnicodeString Hint = FORMAT(LoadStrPart(LOCATION_PROFILE_HINT, 1), (Bookmark->Name));
  9131. UnicodeString LongHint;
  9132. if (!Bookmark->Local.IsEmpty())
  9133. {
  9134. AddToList(LongHint, FORMAT(LoadStrPart(LOCATION_PROFILE_HINT, 2), (Bookmark->Local)), L"\n");
  9135. }
  9136. if (!Bookmark->Remote.IsEmpty())
  9137. {
  9138. AddToList(LongHint, FORMAT(LoadStrPart(LOCATION_PROFILE_HINT, 3), (Bookmark->Remote)), L"\n");
  9139. }
  9140. AddToList(Hint, LongHint, L"|");
  9141. Item->Hint = Hint;
  9142. }
  9143. Parent->Add(Item.release());
  9144. }
  9145. }
  9146. }
  9147. //---------------------------------------------------------------------------
  9148. void __fastcall TCustomScpExplorerForm::CreateOpenDirMenu(TTBCustomItem * Menu, TOperationSide Side)
  9149. {
  9150. Menu->Clear();
  9151. CreateOpenDirMenuList(Menu, Side, WinConfiguration->Bookmarks[Terminal->SessionData->SessionKey]);
  9152. AddMenuSeparator(Menu);
  9153. CreateOpenDirMenuList(Menu, Side, WinConfiguration->SharedBookmarks);
  9154. AddMenuSeparator(Menu);
  9155. std::unique_ptr<TTBCustomItem> Item(new TTBXItem(Owner));
  9156. Item->Action = (Side == osLocal) ? NonVisualDataModule->LocalAddBookmarkAction : NonVisualDataModule->RemoteAddBookmarkAction;
  9157. Menu->Add(Item.release());
  9158. }
  9159. //---------------------------------------------------------------------------