CustomScpExplorer.cpp 331 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143
  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. class TAutoBatch
  119. {
  120. public:
  121. TAutoBatch(TCustomScpExplorerForm * Form) :
  122. FForm(Form)
  123. {
  124. FForm->BatchStart(FBatchStorage);
  125. }
  126. ~TAutoBatch()
  127. {
  128. FForm->BatchEnd(FBatchStorage);
  129. }
  130. private:
  131. TCustomScpExplorerForm * FForm;
  132. void * FBatchStorage;
  133. };
  134. //---------------------------------------------------------------------------
  135. struct TTransferOperationParam
  136. {
  137. TTransferOperationParam();
  138. UnicodeString TargetDirectory;
  139. bool Temp;
  140. bool DragDrop;
  141. int Options;
  142. TAutoSwitch Queue;
  143. };
  144. //---------------------------------------------------------------------------
  145. TTransferOperationParam::TTransferOperationParam()
  146. {
  147. Temp = false;
  148. DragDrop = false;
  149. Options = 0;
  150. Queue = asAuto;
  151. }
  152. //---------------------------------------------------------------------------
  153. class TTransferPresetNoteData : public TObject
  154. {
  155. public:
  156. UnicodeString Message;
  157. };
  158. //---------------------------------------------------------------------------
  159. class TTerminalNoteData : public TObject
  160. {
  161. public:
  162. TTerminal * Terminal;
  163. };
  164. //---------------------------------------------------------------------------
  165. __fastcall TCustomScpExplorerForm::TCustomScpExplorerForm(TComponent* Owner):
  166. FFormRestored(false),
  167. TForm(Owner)
  168. {
  169. FCurrentSide = osRemote;
  170. FEverShown = false;
  171. FDocks = new TList();
  172. RestoreParams();
  173. ConfigurationChanged();
  174. RemoteDirView->Invalidate();
  175. DebugAssert(NonVisualDataModule && !NonVisualDataModule->ScpExplorer);
  176. NonVisualDataModule->ScpExplorer = this;
  177. FAutoOperation = false;
  178. FOnFileOperationFinished = NULL;
  179. FForceExecution = false;
  180. FIgnoreNextDialogChar = 0;
  181. FErrorList = NULL;
  182. FSynchronizeProgressForm = NULL;
  183. FProgressForm = NULL;
  184. FRefreshLocalDirectory = false;
  185. FRefreshRemoteDirectory = false;
  186. FDDExtMapFile = NULL;
  187. // CreateMutexW keeps failing with ERROR_NOACCESS
  188. FDDExtMutex = CreateMutexA(NULL, false, AnsiString(DRAG_EXT_MUTEX).c_str());
  189. DebugAssert(FDDExtMutex != NULL);
  190. FDDTargetControl = NULL;
  191. FDelayedDeletionTimer = NULL;
  192. FDelayedDeletionList = new TStringList();
  193. FDDFileList = NULL;
  194. FPendingTempSpaceWarn = false;
  195. FCapturedLog = NULL;
  196. FDragDropOperation = false;
  197. memset(&FHistoryMenu, 0, sizeof(FHistoryMenu));
  198. FAllowTransferPresetAutoSelect = true;
  199. FCopyParamDefault = L"";
  200. FSynchronizeController = NULL;
  201. FPendingQueueActionItem = NULL;
  202. FLockLevel = 0;
  203. FLockSuspendLevel = 0;
  204. FDisabledOnLockSuspend = false;
  205. FAlternativeDelete = false;
  206. FTrayIcon = new ::TTrayIcon(0);
  207. FTrayIcon->OnClick = TrayIconClick;
  208. FMaxQueueLength = 0;
  209. FLastContextPopupScreenPoint = TPoint(-1, -1);
  210. FTransferResumeList = NULL;
  211. FMoveToQueue = false;
  212. FStandaloneEditing = false;
  213. FOnFeedSynchronizeError = NULL;
  214. FNeedSession = false;
  215. FDoNotIdleCurrentTerminal = 0;
  216. FEditorManager = new TEditorManager();
  217. FEditorManager->OnFileChange = ExecutedFileChanged;
  218. FEditorManager->OnFileReload = ExecutedFileReload;
  219. FEditorManager->OnFileEarlyClosed = ExecutedFileEarlyClosed;
  220. FEditorManager->OnFileUploadComplete = ExecutedFileUploadComplete;
  221. FLocalEditors = new TList();
  222. FQueueStatus = NULL;
  223. FQueueStatusSection = new TCriticalSection();
  224. FQueueStatusInvalidated = false;
  225. FQueueItemInvalidated = false;
  226. FQueueActedItem = NULL;
  227. FQueueController = new TQueueController(QueueView3);
  228. FUserActionTimer = new TTimer(this);
  229. FUserActionTimer->Enabled = false;
  230. FUserActionTimer->Interval = 10;
  231. FUserActionTimer->OnTimer = UserActionTimer;
  232. FNotes = new TStringList();
  233. FNoteTimer = new TTimer(this);
  234. FNoteTimer->Enabled = false;
  235. FNoteTimer->OnTimer = NoteTimer;
  236. FOnNoteClick = NULL;
  237. FOle32Library = LoadLibrary(L"Ole32.dll");
  238. FDragMoveCursor = FOle32Library != NULL ?
  239. LoadCursor(FOle32Library, MAKEINTRESOURCE(2)) : NULL;
  240. UseSystemSettings(this);
  241. TTBXStringList * TransferList = dynamic_cast<TTBXStringList*>(
  242. static_cast<TObject*>(GetComponent(fcTransferList)));
  243. DebugAssert(TransferList != NULL);
  244. FTransferListHoverIndex = -1;
  245. TransferList->OnChange = TransferListChange;
  246. TransferList->OnDrawItem = TransferListDrawItem;
  247. SetSubmenu(dynamic_cast<TTBXCustomItem *>(static_cast<TObject *>(GetComponent(fcColorMenu))));
  248. SetSubmenu(NonVisualDataModule->ColorMenuItem);
  249. UseDesktopFont(SessionsPageControl);
  250. UpdateSessionsPageControlHeight();
  251. UseDesktopFont(RemoteDirView);
  252. UseDesktopFont(RemoteDriveView);
  253. UseDesktopFont(QueueView3);
  254. UseDesktopFont(QueueLabel);
  255. UseDesktopFont(RemoteStatusBar);
  256. reinterpret_cast<TLabel*>(QueueSplitter)->OnDblClick = QueueSplitterDblClick;
  257. QueueSplitter->ShowHint = true;
  258. RemotePanelSplitter->ShowHint = true;
  259. UpdateImages();
  260. FCustomCommandMenu = CreateTBXPopupMenu(this);
  261. FCustomCommandLocalFileList = NULL;
  262. FCustomCommandRemoteFileList = NULL;
  263. FSessionColors = new TPngImageList(this);
  264. FSessionColors->ColorDepth = cd32Bit;
  265. AddFixedSessionImages();
  266. SessionsPageControl->Images = FSessionColors;
  267. QueueLabel->FocusControl = QueueView3;
  268. UpdateQueueLabel();
  269. FRemoteDirViewWasFocused = true;
  270. CreateHiddenWindow();
  271. StartUpdates();
  272. }
  273. //---------------------------------------------------------------------------
  274. __fastcall TCustomScpExplorerForm::~TCustomScpExplorerForm()
  275. {
  276. // this has to be one of the very first things to do
  277. StopUpdateThread();
  278. delete FCustomCommandLocalFileList;
  279. delete FCustomCommandRemoteFileList;
  280. delete FCustomCommandMenu;
  281. delete FTrayIcon;
  282. FTrayIcon = NULL;
  283. FEditorManager->CloseInternalEditors(ForceCloseInternalEditor);
  284. delete FEditorManager;
  285. ForceCloseLocalEditors();
  286. delete FLocalEditors;
  287. HideFileFindDialog();
  288. if (FDelayedDeletionTimer)
  289. {
  290. DoDelayedDeletion(NULL);
  291. SAFE_DESTROY(FDelayedDeletionTimer);
  292. }
  293. SAFE_DESTROY(FDelayedDeletionList);
  294. // sometimes we do not get DDEnd so the list is not released
  295. SAFE_DESTROY(FDDFileList);
  296. DebugAssert(FSynchronizeController == NULL);
  297. CloseHandle(FDDExtMutex);
  298. FDDExtMutex = NULL;
  299. FreeLibrary(FOle32Library);
  300. FOle32Library = NULL;
  301. FDragMoveCursor = NULL;
  302. DebugAssert(!FErrorList);
  303. if (FEverShown)
  304. {
  305. // when window is never shown (like when running command-line operation),
  306. // particularly window site is not restored correctly (BoundsRect value set
  307. // in RestoreForm gets lost during handle allocation), so we do not want
  308. // it to be stored
  309. StoreParams();
  310. }
  311. Terminal = NULL;
  312. Queue = NULL;
  313. DebugAssert(NonVisualDataModule && (NonVisualDataModule->ScpExplorer == this));
  314. NonVisualDataModule->ScpExplorer = NULL;
  315. delete FQueueController;
  316. FQueueController = NULL;
  317. delete FQueueStatusSection;
  318. FQueueStatusSection = NULL;
  319. delete FQueueStatus;
  320. FQueueStatus = NULL;
  321. delete FUserActionTimer;
  322. FUserActionTimer = NULL;
  323. delete FNoteTimer;
  324. delete FNotes;
  325. delete FNoteData;
  326. SAFE_DESTROY(FDocks);
  327. SessionsPageControl->Images = NULL;
  328. // only after clearing Terminal (after DoTerminalListChanged => ... => AddSessionColor is called)
  329. SAFE_DESTROY(FSessionColors);
  330. SAFE_DESTROY(FSessionsDragDropFilesEx);
  331. SAFE_DESTROY(FHistoryMenu[0][0]);
  332. SAFE_DESTROY(FHistoryMenu[0][1]);
  333. SAFE_DESTROY(FHistoryMenu[1][0]);
  334. SAFE_DESTROY(FHistoryMenu[1][1]);
  335. if (FHiddenWindow != NULL)
  336. {
  337. DestroyWindow(FHiddenWindow);
  338. FHiddenWindow = NULL;
  339. }
  340. }
  341. //---------------------------------------------------------------------------
  342. void __fastcall TCustomScpExplorerForm::RefreshPanel(const UnicodeString & Session, const UnicodeString & Path)
  343. {
  344. std::unique_ptr<TSessionData> Data;
  345. if (!Session.IsEmpty())
  346. {
  347. bool DefaultsOnly;
  348. Data.reset(StoredSessions->ParseUrl(Session, NULL, DefaultsOnly));
  349. }
  350. TTerminalManager * Manager = TTerminalManager::Instance();
  351. for (int Index = 0; Index < Manager->Count; Index++)
  352. {
  353. TTerminal * Terminal = Manager->Terminals[Index];
  354. if (Session.IsEmpty() ||
  355. Terminal->SessionData->IsSameSite(Data.get()))
  356. {
  357. if (Path.IsEmpty())
  358. {
  359. Terminal->ClearCaches();
  360. }
  361. else
  362. {
  363. Terminal->DirectoryModified(Path, true);
  364. }
  365. }
  366. }
  367. // We should flag a pending refresh for the background terminals or busy foreground terminals
  368. if ((Terminal != NULL) && Terminal->Active &&
  369. CanCommandLineFromAnotherInstance() &&
  370. (Session.IsEmpty() || Terminal->SessionData->IsSameSite(Data.get())) &&
  371. (Path.IsEmpty() || UnixIsChildPath(Path, Terminal->CurrentDirectory)))
  372. {
  373. Terminal->ReloadDirectory();
  374. }
  375. }
  376. //---------------------------------------------------------------------------
  377. void __fastcall TCustomScpExplorerForm::WMCopyData(TMessage & Message)
  378. {
  379. PCOPYDATASTRUCT CopyData = reinterpret_cast<PCOPYDATASTRUCT>(Message.LParam);
  380. size_t MessageSize = sizeof(TCopyDataMessage);
  381. bool Result = DebugAlwaysTrue(CopyData->cbData == MessageSize);
  382. if (Result)
  383. {
  384. const TCopyDataMessage & Message = *reinterpret_cast<const TCopyDataMessage *>(CopyData->lpData);
  385. Result = (Message.Version == TCopyDataMessage::Version1);
  386. if (Result)
  387. {
  388. switch (Message.Command)
  389. {
  390. case TCopyDataMessage::CommandCanCommandLine:
  391. Result = CanCommandLineFromAnotherInstance();
  392. break;
  393. case TCopyDataMessage::CommandCommandLine:
  394. {
  395. UnicodeString CommandLine(Message.CommandLine);
  396. Result = CommandLineFromAnotherInstance(CommandLine);
  397. }
  398. break;
  399. case TCopyDataMessage::RefreshPanel:
  400. RefreshPanel(Message.Refresh.Session, Message.Refresh.Path);
  401. break;
  402. case TCopyDataMessage::MainWindowCheck:
  403. Result = true;
  404. break;
  405. default:
  406. Result = false;
  407. break;
  408. }
  409. }
  410. }
  411. Message.Result = Result ? 1 : 0;
  412. }
  413. //---------------------------------------------------------------------------
  414. void __fastcall TCustomScpExplorerForm::CreateHiddenWindow()
  415. {
  416. WNDCLASS WindowClass = {0};
  417. WindowClass.lpfnWndProc = DefWindowProc;
  418. WindowClass.hInstance = HInstance;
  419. WindowClass.lpszClassName = HIDDEN_WINDOW_NAME;
  420. FHiddenWindow = NULL;
  421. if (RegisterClass(&WindowClass))
  422. {
  423. FHiddenWindow = CreateWindow(HIDDEN_WINDOW_NAME, L"",
  424. WS_POPUP, 0, 0, 0, 0, 0, 0, HInstance, NULL);
  425. }
  426. }
  427. //---------------------------------------------------------------------------
  428. bool __fastcall TCustomScpExplorerForm::CanConsole()
  429. {
  430. return (Terminal != NULL) && (Terminal->IsCapable[fcAnyCommand] || Terminal->IsCapable[fcSecondaryShell]);
  431. }
  432. //---------------------------------------------------------------------------
  433. bool __fastcall TCustomScpExplorerForm::CanCommandLineFromAnotherInstance()
  434. {
  435. bool Result = Visible && !NonVisualDataModule->Busy;
  436. return Result;
  437. }
  438. //---------------------------------------------------------------------------
  439. bool __fastcall TCustomScpExplorerForm::CommandLineFromAnotherInstance(
  440. const UnicodeString & CommandLine)
  441. {
  442. TProgramParams Params(CommandLine);
  443. bool Result = CanCommandLineFromAnotherInstance() && DebugAlwaysTrue(Params.ParamCount > 0);
  444. if (Result)
  445. {
  446. NonVisualDataModule->StartBusy();
  447. try
  448. {
  449. // this action is initiated from another process,
  450. // so it's likely that our window is not visible,
  451. // and user won't see what is going on
  452. Application->BringToFront();
  453. // reload sessions as we may be asked to open a session
  454. // just stored by another instance
  455. StoredSessions->Reload();
  456. UnicodeString SessionName = Params.Param[1];
  457. std::unique_ptr<TObjectList> DataList(new TObjectList());
  458. UnicodeString DownloadFile; // unused
  459. try
  460. {
  461. GetLoginData(SessionName, &Params, DataList.get(), DownloadFile, true, this);
  462. }
  463. catch (EAbort &)
  464. {
  465. // Happens when opening session in PuTTY and in other situations,
  466. // in which we do not want to fail WM_COPYDATA, as that would cause master instance
  467. // to process the command-line on its own (or to pass it to yet another instance)
  468. }
  469. if (DataList->Count > 0)
  470. {
  471. TTerminalManager * Manager = TTerminalManager::Instance();
  472. Manager->ActiveTerminal = Manager->NewTerminals(DataList.get());
  473. }
  474. }
  475. __finally
  476. {
  477. NonVisualDataModule->EndBusy();
  478. }
  479. }
  480. return Result;
  481. }
  482. //---------------------------------------------------------------------------
  483. void __fastcall TCustomScpExplorerForm::SetTerminal(TTerminal * value)
  484. {
  485. if (FTerminal != value)
  486. {
  487. TerminalChanging();
  488. DoSetTerminal(value, false);
  489. }
  490. }
  491. //---------------------------------------------------------------------------
  492. void __fastcall TCustomScpExplorerForm::DoSetTerminal(TTerminal * value, bool Replace)
  493. {
  494. FTerminal = value;
  495. bool PrevAllowTransferPresetAutoSelect = FAllowTransferPresetAutoSelect;
  496. FAllowTransferPresetAutoSelect = false;
  497. try
  498. {
  499. TerminalChanged(Replace);
  500. }
  501. __finally
  502. {
  503. FAllowTransferPresetAutoSelect = PrevAllowTransferPresetAutoSelect;
  504. }
  505. if (Terminal != NULL)
  506. {
  507. TransferPresetAutoSelect();
  508. }
  509. }
  510. //---------------------------------------------------------------------------
  511. void __fastcall TCustomScpExplorerForm::ReplaceTerminal(TTerminal * value)
  512. {
  513. DoSetTerminal(value, true);
  514. }
  515. //---------------------------------------------------------------------------
  516. void __fastcall TCustomScpExplorerForm::TerminalChanging()
  517. {
  518. if (FTerminal != NULL)
  519. {
  520. UpdateTerminal(Terminal);
  521. }
  522. }
  523. //---------------------------------------------------------------------------
  524. void __fastcall TCustomScpExplorerForm::TerminalChanged(bool Replaced)
  525. {
  526. if (Replaced)
  527. {
  528. RemoteDirView->ReplaceTerminal(Terminal);
  529. }
  530. else
  531. {
  532. RemoteDirView->Terminal = Terminal;
  533. }
  534. NonVisualDataModule->ResetQueueOnceEmptyOperation();
  535. TManagedTerminal * ManagedTerminal = NULL;
  536. if (Terminal != NULL)
  537. {
  538. if (Terminal->Active)
  539. {
  540. Terminal->RefreshDirectory();
  541. }
  542. ManagedTerminal = dynamic_cast<TManagedTerminal *>(Terminal);
  543. DebugAssert(ManagedTerminal != NULL);
  544. if (WinConfiguration->PreservePanelState)
  545. {
  546. if (ManagedTerminal->RemoteExplorerState != NULL)
  547. {
  548. RemoteDirView->RestoreState(ManagedTerminal->RemoteExplorerState);
  549. }
  550. else
  551. {
  552. RemoteDirView->ClearState();
  553. }
  554. }
  555. if (!Replaced)
  556. {
  557. InitStatusBar();
  558. }
  559. }
  560. DoTerminalListChanged();
  561. if (ManagedTerminal != NULL)
  562. {
  563. // this has to be set only after the tab is switched from DoTerminalListChanged,
  564. // otherwise we are changing color of wrong tab
  565. SessionColor = (TColor)ManagedTerminal->StateData->Color;
  566. }
  567. UpdateTransferList();
  568. // Update panels Enable state before refreshing the labels
  569. UpdateControls();
  570. }
  571. //---------------------------------------------------------------------------
  572. void __fastcall TCustomScpExplorerForm::SetQueue(TTerminalQueue * value)
  573. {
  574. if (Queue != value)
  575. {
  576. if (FQueue != NULL)
  577. {
  578. FQueue->OnListUpdate = NULL;
  579. FQueue->OnQueueItemUpdate = NULL;
  580. }
  581. FQueue = value;
  582. if (FQueue != NULL)
  583. {
  584. DebugAssert(FQueue->OnListUpdate == NULL);
  585. FQueue->OnListUpdate = QueueListUpdate;
  586. DebugAssert(FQueue->OnQueueItemUpdate == NULL);
  587. FQueue->OnQueueItemUpdate = QueueItemUpdate;
  588. }
  589. QueueChanged();
  590. }
  591. }
  592. //---------------------------------------------------------------------------
  593. void __fastcall TCustomScpExplorerForm::QueueView3Deletion(TObject * /*Sender*/,
  594. TListItem * Item)
  595. {
  596. if (FQueueActedItem == Item)
  597. {
  598. FQueueActedItem = NULL;
  599. if ((QueueView3->PopupMenu != NULL) &&
  600. (QueueView3->PopupMenu->PopupComponent == QueueView3))
  601. {
  602. // rather "trick", suggested by Jordan on jrsoftware.toolbar2000
  603. ReleaseCapture();
  604. }
  605. }
  606. if (Item->Data == FPendingQueueActionItem)
  607. {
  608. FPendingQueueActionItem = NULL;
  609. }
  610. }
  611. //---------------------------------------------------------------------------
  612. void __fastcall TCustomScpExplorerForm::UpdateQueueStatus(bool QueueChanging)
  613. {
  614. {
  615. TGuard Guard(FQueueStatusSection);
  616. FQueueStatusInvalidated = false;
  617. if (FQueue != NULL)
  618. {
  619. FQueueStatus = FQueue->CreateStatus(FQueueStatus);
  620. }
  621. }
  622. if ((FQueueStatus != NULL) && (FQueueStatus->Count > FMaxQueueLength))
  623. {
  624. FMaxQueueLength = FQueueStatus->Count;
  625. Configuration->Usage->SetMax(L"MaxQueueLength", FMaxQueueLength);
  626. }
  627. FQueueController->UpdateQueueStatus(FQueueStatus);
  628. SetQueueProgress();
  629. UpdateQueueView();
  630. bool IsEmpty = (FQueueStatus == NULL) || (FQueueStatus->Count == 0);
  631. if (IsEmpty && (Terminal != NULL))
  632. {
  633. TOnceDoneOperation OnceDoneOperation =
  634. NonVisualDataModule->CurrentQueueOnceEmptyOperation();
  635. NonVisualDataModule->ResetQueueOnceEmptyOperation();
  636. if ((FQueue != NULL) && !WinConfiguration->EnableQueueByDefault && !QueueChanging)
  637. {
  638. FQueue->Enabled = false;
  639. }
  640. if ((OnceDoneOperation != odoIdle) && !NonVisualDataModule->Busy)
  641. {
  642. Terminal->CloseOnCompletion(OnceDoneOperation, LoadStr(CLOSED_ON_QUEUE_EMPTY));
  643. }
  644. }
  645. }
  646. //---------------------------------------------------------------------------
  647. UnicodeString __fastcall TCustomScpExplorerForm::GetQueueProgressTitle()
  648. {
  649. UnicodeString Result;
  650. if (FQueueStatus != NULL)
  651. {
  652. if (FQueueStatus->IsOnlyOneActiveAndNoPending())
  653. {
  654. TFileOperationProgressType * ProgressData =
  655. FQueueStatus->Items[FQueueStatus->DoneCount]->ProgressData;
  656. if ((ProgressData != NULL) && ProgressData->InProgress)
  657. {
  658. Result = TProgressForm::ProgressStr(NULL, ProgressData);
  659. }
  660. }
  661. else if (FQueueStatus->ActiveAndPendingPrimaryCount > 1)
  662. {
  663. Result = FMTLOAD(PROGRESS_IN_QUEUE, (FQueueStatus->ActiveAndPendingPrimaryCount));
  664. }
  665. }
  666. return Result;
  667. }
  668. //---------------------------------------------------------------------------
  669. void __fastcall TCustomScpExplorerForm::UpdateQueueView()
  670. {
  671. ComponentVisible[fcQueueView] =
  672. (WinConfiguration->QueueView.Show == qvShow) ||
  673. ((WinConfiguration->QueueView.Show == qvHideWhenEmpty) &&
  674. (FQueueStatus != NULL) && (FQueueStatus->Count > 0));
  675. }
  676. //---------------------------------------------------------------------------
  677. void __fastcall TCustomScpExplorerForm::QueueChanged()
  678. {
  679. if (FQueueStatus != NULL)
  680. {
  681. delete FQueueStatus;
  682. FQueueStatus = NULL;
  683. }
  684. UpdateQueueStatus(true);
  685. }
  686. //---------------------------------------------------------------------------
  687. void __fastcall TCustomScpExplorerForm::QueueListUpdate(TTerminalQueue * Queue)
  688. {
  689. if (FQueue == Queue)
  690. {
  691. FQueueStatusInvalidated = true;
  692. }
  693. }
  694. //---------------------------------------------------------------------------
  695. void __fastcall TCustomScpExplorerForm::QueueItemUpdate(TTerminalQueue * Queue,
  696. TQueueItem * Item)
  697. {
  698. if (FQueue == Queue)
  699. {
  700. TGuard Guard(FQueueStatusSection);
  701. // this may be running in parallel with QueueChanged
  702. if (FQueueStatus != NULL)
  703. {
  704. TQueueItemProxy * QueueItem = FQueueStatus->FindByQueueItem(Item);
  705. if ((Item->Status == TQueueItem::qsDone) && (Terminal != NULL))
  706. {
  707. FRefreshLocalDirectory = (QueueItem == NULL) ||
  708. (!QueueItem->Info->ModifiedLocal.IsEmpty());
  709. FRefreshRemoteDirectory = (QueueItem == NULL) ||
  710. (!QueueItem->Info->ModifiedRemote.IsEmpty());
  711. }
  712. if (QueueItem != NULL)
  713. {
  714. QueueItem->UserData = (void*)true;
  715. FQueueItemInvalidated = true;
  716. }
  717. }
  718. }
  719. }
  720. //---------------------------------------------------------------------------
  721. bool __fastcall TCustomScpExplorerForm::IsQueueAutoPopup()
  722. {
  723. // during standalone editing, we have no way to see/control queue,
  724. // so we have to always popup prompts automatically
  725. return FStandaloneEditing || GUIConfiguration->QueueAutoPopup;
  726. }
  727. //---------------------------------------------------------------------------
  728. void __fastcall TCustomScpExplorerForm::RefreshQueueItems()
  729. {
  730. if (FQueueStatus != NULL)
  731. {
  732. bool QueueAutoPopup = IsQueueAutoPopup();
  733. bool NeedRefresh = FQueueController->NeedRefresh();
  734. bool Refresh = FQueueItemInvalidated || NeedRefresh;
  735. FQueueItemInvalidated = false;
  736. int Limit = Refresh ? FQueueStatus->Count : FQueueStatus->DoneAndActiveCount;
  737. bool Updated = false;
  738. TQueueItemProxy * QueueItem;
  739. bool WasUserAction;
  740. for (int Index = 0; Index < Limit; Index++)
  741. {
  742. bool Update = false;
  743. QueueItem = FQueueStatus->Items[Index];
  744. WasUserAction = TQueueItem::IsUserActionStatus(QueueItem->Status);
  745. if (!NonVisualDataModule->Busy && QueueAutoPopup && WasUserAction &&
  746. (FPendingQueueActionItem == NULL))
  747. {
  748. FPendingQueueActionItem = QueueItem;
  749. FUserActionTimer->Enabled = true;
  750. }
  751. if ((bool)QueueItem->UserData)
  752. {
  753. QueueItem->UserData = (void*)false;
  754. QueueItem->Update();
  755. Updated = true;
  756. Update = true;
  757. }
  758. else if (FQueueController->QueueItemNeedsFrequentRefresh(QueueItem) || NeedRefresh)
  759. {
  760. Update = true;
  761. }
  762. if (Update)
  763. {
  764. FQueueController->RefreshQueueItem(QueueItem);
  765. }
  766. }
  767. if (Updated)
  768. {
  769. NonVisualDataModule->UpdateNonVisibleActions();
  770. SetQueueProgress();
  771. }
  772. }
  773. }
  774. //---------------------------------------------------------------------------
  775. void __fastcall TCustomScpExplorerForm::SetTaskbarListProgressState(TBPFLAG Flags)
  776. {
  777. // Could now use Application->MainFormHandle, now that we implement Application->OnGetMainFormHandle
  778. FTaskbarList->SetProgressState(GetMainForm()->Handle, Flags);
  779. }
  780. //---------------------------------------------------------------------------
  781. void __fastcall TCustomScpExplorerForm::SetTaskbarListProgressValue(TFileOperationProgressType * ProgressData)
  782. {
  783. if (ProgressData->Operation != foCalculateSize)
  784. {
  785. int OverallProgress;
  786. // FProgressForm is null when this is called from SetQueueProgress
  787. if ((FProgressForm != NULL) && (FProgressForm->SynchronizeProgress != NULL))
  788. {
  789. OverallProgress = FProgressForm->SynchronizeProgress->Progress(ProgressData);
  790. }
  791. else
  792. {
  793. OverallProgress = ProgressData->OverallProgress();
  794. }
  795. // implies TBPF_NORMAL
  796. FTaskbarList->SetProgressValue(GetMainForm()->Handle, OverallProgress, 100);
  797. }
  798. else
  799. {
  800. SetTaskbarListProgressState(TBPF_INDETERMINATE);
  801. }
  802. }
  803. //---------------------------------------------------------------------------
  804. void __fastcall TCustomScpExplorerForm::SetQueueProgress()
  805. {
  806. TTerminalManager::Instance()->QueueStatusUpdated();
  807. if ((FTaskbarList != NULL) && (FProgressForm == NULL))
  808. {
  809. if ((FQueueStatus != NULL) && (FQueueStatus->ActiveAndPendingPrimaryCount > 0))
  810. {
  811. if (FQueueStatus->IsOnlyOneActiveAndNoPending())
  812. {
  813. TFileOperationProgressType * ProgressData = NULL;
  814. if (FQueueStatus->Items[FQueueStatus->DoneCount] != NULL)
  815. {
  816. ProgressData = FQueueStatus->Items[FQueueStatus->DoneCount]->ProgressData;
  817. }
  818. if ((ProgressData != NULL) &&
  819. ProgressData->InProgress)
  820. {
  821. SetTaskbarListProgressValue(ProgressData);
  822. }
  823. else
  824. {
  825. SetTaskbarListProgressState(TBPF_NOPROGRESS);
  826. }
  827. }
  828. else
  829. {
  830. SetTaskbarListProgressState(TBPF_INDETERMINATE);
  831. }
  832. }
  833. else
  834. {
  835. SetTaskbarListProgressState(TBPF_NOPROGRESS);
  836. }
  837. }
  838. UpdateQueueLabel();
  839. }
  840. //---------------------------------------------------------------------------
  841. void __fastcall TCustomScpExplorerForm::UpdateQueueLabel()
  842. {
  843. UnicodeString Caption = LoadStr(QUEUE_CAPTION);
  844. if (FQueueStatus != NULL)
  845. {
  846. int ActiveAndPendingPrimaryCount = FQueueStatus->ActiveAndPendingPrimaryCount;
  847. if (ActiveAndPendingPrimaryCount > 0)
  848. {
  849. Caption = FORMAT("%s (%d)", (Caption, ActiveAndPendingPrimaryCount));
  850. }
  851. }
  852. QueueLabel->Caption = Caption;
  853. }
  854. //---------------------------------------------------------------------------
  855. void __fastcall TCustomScpExplorerForm::UpdateTransferList()
  856. {
  857. TTBXStringList * TransferList = dynamic_cast<TTBXStringList*>(
  858. static_cast<TComponent*>(GetComponent(fcTransferList)));
  859. TTBXDropDownItem * TransferDropDown = dynamic_cast<TTBXDropDownItem*>(
  860. static_cast<TComponent*>(GetComponent(fcTransferDropDown)));
  861. TransferList->Strings->BeginUpdate();
  862. try
  863. {
  864. TransferList->Strings->Assign(GUIConfiguration->CopyParamList->NameList);
  865. TransferList->Strings->Insert(0, StripHotkey(LoadStr(COPY_PARAM_DEFAULT)));
  866. TransferList->ItemIndex = GUIConfiguration->CopyParamIndex + 1;
  867. if (FTransferDropDownHint.IsEmpty())
  868. {
  869. FTransferDropDownHint = TransferDropDown->Hint;
  870. }
  871. // this way we get name for "default" settings (COPY_PARAM_DEFAULT)
  872. UnicodeString Name = TransferList->Strings->Strings[TransferList->ItemIndex];
  873. TransferDropDown->Text = StripHotkey(Name);
  874. TransferDropDown->Hint = FORMAT(L"%s|%s:\n%s",
  875. (FTransferDropDownHint, StripHotkey(Name),
  876. GUIConfiguration->CurrentCopyParam.GetInfoStr(L"; ",
  877. FLAGMASK(((Terminal != NULL) && Terminal->Active), Terminal->UsableCopyParamAttrs(0).General))));
  878. // update the label, otherwise when it is updated only on the first draw
  879. // of the list, it is drawn "bold" for some reason
  880. FTransferListHoverIndex = TransferList->ItemIndex;
  881. UpdateTransferLabel();
  882. }
  883. __finally
  884. {
  885. TransferList->Strings->EndUpdate();
  886. }
  887. }
  888. //---------------------------------------------------------------------------
  889. void __fastcall TCustomScpExplorerForm::UpdateCustomCommandsToolbar()
  890. {
  891. TTBXToolbar * Toolbar = dynamic_cast<TTBXToolbar *>(GetComponent(fcCustomCommandsBand));
  892. DebugAssert(Toolbar != NULL);
  893. NonVisualDataModule->UpdateCustomCommandsToolbar(Toolbar);
  894. }
  895. //---------------------------------------------------------------------------
  896. void __fastcall TCustomScpExplorerForm::UpdateActions()
  897. {
  898. TForm::UpdateActions();
  899. if (ComponentVisible[fcCustomCommandsBand])
  900. {
  901. UpdateCustomCommandsToolbar();
  902. }
  903. }
  904. //---------------------------------------------------------------------------
  905. void __fastcall TCustomScpExplorerForm::UpdateSessionsPageControlHeight()
  906. {
  907. SessionsPageControl->Height = SessionsPageControl->GetTabsHeight();
  908. }
  909. //---------------------------------------------------------------------------
  910. void __fastcall TCustomScpExplorerForm::ConfigurationChanged()
  911. {
  912. DebugAssert(Configuration && RemoteDirView);
  913. RemoteDirView->DimmHiddenFiles = WinConfiguration->DimmHiddenFiles;
  914. RemoteDirView->ShowHiddenFiles = WinConfiguration->ShowHiddenFiles;
  915. RemoteDirView->FormatSizeBytes = WinConfiguration->FormatSizeBytes;
  916. RemoteDirView->ShowInaccesibleDirectories = WinConfiguration->ShowInaccesibleDirectories;
  917. RemoteDirView->NaturalOrderNumericalSorting = WinConfiguration->NaturalOrderNumericalSorting;
  918. if (RemoteDirView->RowSelect != WinConfiguration->FullRowSelect)
  919. {
  920. RemoteDirView->RowSelect = WinConfiguration->FullRowSelect;
  921. // selection is not redrawn automatically when RowSelect changes
  922. RemoteDirView->Invalidate();
  923. }
  924. RemoteDriveView->DimmHiddenDirs = WinConfiguration->DimmHiddenFiles;
  925. RemoteDriveView->ShowHiddenDirs = WinConfiguration->ShowHiddenFiles;
  926. RemoteDriveView->ShowInaccesibleDirectories = WinConfiguration->ShowInaccesibleDirectories;
  927. RemoteDriveView->NaturalOrderNumericalSorting = WinConfiguration->NaturalOrderNumericalSorting;
  928. UpdateSessionsPageControlHeight();
  929. SetDockAllowDrag(!WinConfiguration->LockToolbars);
  930. UpdateToolbarDisplayMode();
  931. UpdateTransferList();
  932. if (Terminal != NULL)
  933. {
  934. TransferPresetAutoSelect();
  935. }
  936. UpdateQueueView();
  937. UpdateControls();
  938. // this can be called even before constuctor finishes.
  939. if (FEditorManager != NULL)
  940. {
  941. FEditorManager->ProcessFiles(FileConfigurationChanged, NULL);
  942. }
  943. // this can be called even before constuctor finishes.
  944. if (FLocalEditors != NULL)
  945. {
  946. for (int Index = 0; Index < FLocalEditors->Count; Index++)
  947. {
  948. ReconfigureEditorForm(static_cast<TForm *>(FLocalEditors->Items[Index]));
  949. }
  950. }
  951. if (ComponentVisible[fcCustomCommandsBand])
  952. {
  953. UpdateCustomCommandsToolbar();
  954. }
  955. // show only when keeping queue items forever,
  956. // otherwise, its enough to have in in the context menu
  957. QueueDeleteAllDoneQueueToolbarItem->Visible =
  958. WinConfiguration->QueueKeepDoneItems && (WinConfiguration->QueueKeepDoneItemsFor < 0);
  959. }
  960. //---------------------------------------------------------------------------
  961. void __fastcall TCustomScpExplorerForm::FileConfigurationChanged(
  962. const UnicodeString FileName, TEditedFileData * /*Data*/, TObject * Token,
  963. void * /*Arg*/)
  964. {
  965. if (Token != NULL)
  966. {
  967. TForm * Editor = dynamic_cast<TForm*>(Token);
  968. DebugAssert(Editor != NULL);
  969. ReconfigureEditorForm(Editor);
  970. }
  971. }
  972. //---------------------------------------------------------------------------
  973. void __fastcall TCustomScpExplorerForm::EnableDDTransferConfirmation(TObject * /*Sender*/)
  974. {
  975. WinConfiguration->DDTransferConfirmation = asOn;
  976. }
  977. //---------------------------------------------------------------------------
  978. void __fastcall TCustomScpExplorerForm::CopyParamDialogAfter(
  979. TTransferDirection /*Direction*/, bool /*Temp*/, const UnicodeString & /*TargetDirectory*/)
  980. {
  981. // noop
  982. }
  983. //---------------------------------------------------------------------------
  984. bool __fastcall TCustomScpExplorerForm::CopyParamDialog(
  985. TTransferDirection Direction, TTransferType Type, bool Temp,
  986. TStrings * FileList, UnicodeString & TargetDirectory, TGUICopyParamType & CopyParam,
  987. bool Confirm, bool DragDrop, int Options)
  988. {
  989. bool Result = true;
  990. DebugAssert(Terminal && Terminal->Active);
  991. // these parameters are known in advance
  992. int Params =
  993. FLAGMASK(Type == ttMove, cpDelete);
  994. bool ToTemp = (Temp && (Direction == tdToLocal));
  995. if (Result && Confirm && WinConfiguration->ConfirmTransferring)
  996. {
  997. bool DisableNewerOnly =
  998. (!Terminal->IsCapable[fcNewerOnlyUpload] && (Direction == tdToRemote)) ||
  999. ToTemp;
  1000. Options |=
  1001. FLAGMASK(ToTemp, coTemp) |
  1002. FLAGMASK(!Terminal->IsCapable[fsBackgroundTransfers], coDisableQueue) |
  1003. coDoNotShowAgain;
  1004. TUsableCopyParamAttrs UsableCopyParamAttrs = Terminal->UsableCopyParamAttrs(Params);
  1005. int CopyParamAttrs = (Direction == tdToRemote ?
  1006. UsableCopyParamAttrs.Upload : UsableCopyParamAttrs.Download) |
  1007. FLAGMASK(DisableNewerOnly, cpaNoNewerOnly);
  1008. int OutputOptions =
  1009. FLAGMASK(DragDrop && (WinConfiguration->DDTransferConfirmation == asAuto),
  1010. cooDoNotShowAgain);
  1011. std::unique_ptr<TSessionData> SessionData(SessionDataForCode());
  1012. FlashOnBackground(); // Particularly when called from ClipboardFakeCreated
  1013. Result = DoCopyDialog(Direction == tdToRemote, Type == ttMove,
  1014. FileList, TargetDirectory, &CopyParam, Options, CopyParamAttrs, SessionData.get(), &OutputOptions);
  1015. if (Result)
  1016. {
  1017. if (FLAGSET(OutputOptions, cooDoNotShowAgain))
  1018. {
  1019. if (DragDrop)
  1020. {
  1021. if (WinConfiguration->DDTransferConfirmation == asAuto)
  1022. {
  1023. PopupTrayBalloon(NULL, LoadStr(DD_TRANSFER_CONFIRM_OFF2), qtInformation,
  1024. NULL, 0, EnableDDTransferConfirmation, NULL);
  1025. }
  1026. WinConfiguration->DDTransferConfirmation = asOff;
  1027. }
  1028. else
  1029. {
  1030. WinConfiguration->ConfirmTransferring = false;
  1031. }
  1032. }
  1033. else
  1034. {
  1035. // User explicitly unchecked "do not show again",
  1036. // so show him the dialog the next time
  1037. if (DragDrop && (WinConfiguration->DDTransferConfirmation == asAuto))
  1038. {
  1039. WinConfiguration->DDTransferConfirmation = asOn;
  1040. }
  1041. }
  1042. CopyParamDialogAfter(Direction, Temp, TargetDirectory);
  1043. }
  1044. }
  1045. if (Result && CopyParam.Queue && !ToTemp && Terminal->IsCapable[fsBackgroundTransfers])
  1046. {
  1047. Configuration->Usage->Inc(L"TransfersOnBackground");
  1048. // these parameter are known only after transfer dialog
  1049. Params |=
  1050. (CopyParam.QueueNoConfirmation ? cpNoConfirmation : 0);
  1051. AddQueueItem(Queue, Direction, FileList, TargetDirectory, CopyParam, Params);
  1052. Result = false;
  1053. ClearTransferSourceSelection(Direction);
  1054. }
  1055. return Result;
  1056. }
  1057. //---------------------------------------------------------------------------
  1058. void __fastcall TCustomScpExplorerForm::ClearTransferSourceSelection(TTransferDirection Direction)
  1059. {
  1060. if (FOnFileOperationFinished != NULL)
  1061. {
  1062. FOnFileOperationFinished(UnicodeString(), true);
  1063. }
  1064. else
  1065. {
  1066. TOperationSide Side = ((Direction == tdToRemote) ? osLocal : osRemote);
  1067. if (HasDirView[Side])
  1068. {
  1069. DirView(Side)->SelectAll(smNone);
  1070. }
  1071. }
  1072. }
  1073. //---------------------------------------------------------------------------
  1074. void __fastcall TCustomScpExplorerForm::AddQueueItem(
  1075. TTerminalQueue * Queue, TTransferDirection Direction, TStrings * FileList,
  1076. const UnicodeString TargetDirectory, const TGUICopyParamType & CopyParam,
  1077. int Params)
  1078. {
  1079. DebugAssert(Queue != NULL);
  1080. bool SingleFile = false;
  1081. if (FileList->Count == 1)
  1082. {
  1083. if (Direction == tdToRemote)
  1084. {
  1085. UnicodeString FileName = FileList->Strings[0];
  1086. SingleFile = FileExists(ApiPath(FileName));
  1087. }
  1088. else
  1089. {
  1090. TRemoteFile * File = static_cast<TRemoteFile *>(FileList->Objects[0]);
  1091. SingleFile = !File->IsDirectory;
  1092. }
  1093. }
  1094. TQueueItem * QueueItem;
  1095. if (Direction == tdToRemote)
  1096. {
  1097. QueueItem = new TUploadQueueItem(Terminal, FileList, TargetDirectory,
  1098. &CopyParam, Params, SingleFile, CopyParam.QueueParallel);
  1099. }
  1100. else
  1101. {
  1102. QueueItem = new TDownloadQueueItem(Terminal, FileList, TargetDirectory,
  1103. &CopyParam, Params, SingleFile, CopyParam.QueueParallel);
  1104. }
  1105. AddQueueItem(Queue, QueueItem, Terminal);
  1106. }
  1107. //---------------------------------------------------------------------------
  1108. void __fastcall TCustomScpExplorerForm::AddQueueItem(TTerminalQueue * Queue, TQueueItem * QueueItem, TTerminal * Terminal)
  1109. {
  1110. if (Queue->IsEmpty)
  1111. {
  1112. TManagedTerminal * ManagedTerminal = DebugNotNull(dynamic_cast<TManagedTerminal *>(Terminal));
  1113. ManagedTerminal->QueueOperationStart = Now();
  1114. }
  1115. Queue->AddItem(QueueItem);
  1116. }
  1117. //---------------------------------------------------------------------------
  1118. void __fastcall TCustomScpExplorerForm::RestoreFormParams()
  1119. {
  1120. }
  1121. //---------------------------------------------------------------------------
  1122. void __fastcall TCustomScpExplorerForm::RestoreParams()
  1123. {
  1124. DebugAssert(FDocks != NULL);
  1125. for (int Index = 0; Index < ComponentCount; Index++)
  1126. {
  1127. TTBDock * Dock = dynamic_cast<TTBDock *>(Components[Index]);
  1128. if ((Dock != NULL) && (Dock->Tag == 0))
  1129. {
  1130. FDocks->Add(Dock);
  1131. }
  1132. }
  1133. CollectItemsWithTextDisplayMode(this);
  1134. ConfigurationChanged();
  1135. QueuePanel->Height =
  1136. LoadDimension(WinConfiguration->QueueView.Height, WinConfiguration->QueueView.HeightPixelsPerInch, this);
  1137. LoadListViewStr(QueueView3, WinConfiguration->QueueView.Layout);
  1138. QueueDock->Visible = WinConfiguration->QueueView.ToolBar;
  1139. QueueLabel->Visible = WinConfiguration->QueueView.Label;
  1140. }
  1141. //---------------------------------------------------------------------------
  1142. void __fastcall TCustomScpExplorerForm::StoreParams()
  1143. {
  1144. WinConfiguration->QueueView.Height = QueuePanel->Height;
  1145. WinConfiguration->QueueView.HeightPixelsPerInch = GetControlPixelsPerInch(this);
  1146. WinConfiguration->QueueView.Layout = GetListViewStr(QueueView3);
  1147. WinConfiguration->QueueView.ToolBar = QueueDock->Visible;
  1148. WinConfiguration->QueueView.Label = QueueLabel->Visible;
  1149. }
  1150. //---------------------------------------------------------------------------
  1151. void __fastcall TCustomScpExplorerForm::CreateParams(TCreateParams & Params)
  1152. {
  1153. if (!FFormRestored)
  1154. {
  1155. FFormRestored = true;
  1156. RestoreFormParams();
  1157. }
  1158. TForm::CreateParams(Params);
  1159. }
  1160. //---------------------------------------------------------------------------
  1161. void __fastcall TCustomScpExplorerForm::SetDockAllowDrag(bool value)
  1162. {
  1163. DebugAssert(FDocks != NULL);
  1164. for (int Index = 0; Index < FDocks->Count; Index++)
  1165. {
  1166. static_cast<TTBDock*>(FDocks->Items[Index])->AllowDrag = value;
  1167. }
  1168. }
  1169. //---------------------------------------------------------------------------
  1170. void __fastcall TCustomScpExplorerForm::LoadToolbarsLayoutStr(UnicodeString LayoutStr, UnicodeString ButtonsStr)
  1171. {
  1172. SetDockAllowDrag(true);
  1173. while (!ButtonsStr.IsEmpty())
  1174. {
  1175. UnicodeString S = CutToChar(ButtonsStr, L';', true);
  1176. UnicodeString Name = CutToChar(S, L'=', true);
  1177. for (int ToolbarIndex = 0; ToolbarIndex < ComponentCount; ToolbarIndex++)
  1178. {
  1179. TTBCustomToolbar * Toolbar = dynamic_cast<TTBCustomToolbar *>(Components[ToolbarIndex]);
  1180. if ((Toolbar != NULL) && SameText(Name, GetToolbarKey(Toolbar->Name)))
  1181. {
  1182. while (!S.IsEmpty())
  1183. {
  1184. UnicodeString S2 = CutToChar(S, L',', true);
  1185. UnicodeString Name = CutToChar(S2, L':', true);
  1186. for (int ItemIndex = 0; ItemIndex < Toolbar->Items->Count; ItemIndex++)
  1187. {
  1188. TTBCustomItem * Item = Toolbar->Items->Items[ItemIndex];
  1189. if (SameText(GetToolbarItemName(Item), Name))
  1190. {
  1191. // forward compatibility
  1192. UnicodeString S3 = CutToChar(S2, L':', true);
  1193. bool Visible = StrToIntDef(S3, 0);
  1194. Item->Visible = Visible;
  1195. }
  1196. }
  1197. }
  1198. }
  1199. }
  1200. }
  1201. ::LoadToolbarsLayoutStr(this, LayoutStr);
  1202. SetDockAllowDrag(!WinConfiguration->LockToolbars);
  1203. }
  1204. //---------------------------------------------------------------------------
  1205. UnicodeString __fastcall TCustomScpExplorerForm::GetToolbarsLayoutStr()
  1206. {
  1207. UnicodeString Result;
  1208. SetDockAllowDrag(true);
  1209. Result = ::GetToolbarsLayoutStr(this);
  1210. SetDockAllowDrag(!WinConfiguration->LockToolbars);
  1211. return Result;
  1212. }
  1213. //---------------------------------------------------------------------------
  1214. UnicodeString __fastcall TCustomScpExplorerForm::GetToolbarItemName(TTBCustomItem * Item)
  1215. {
  1216. UnicodeString Result;
  1217. if (Item->Action != NULL)
  1218. {
  1219. Result = Item->Action->Name;
  1220. Result = RemoveSuffix(Result, L"Action");
  1221. }
  1222. else
  1223. {
  1224. Result = Item->Name;
  1225. Result = RemoveSuffix(Result, L"SubmenuItem");
  1226. Result = RemoveSuffix(Result, L"Item");
  1227. }
  1228. return Result;
  1229. }
  1230. //---------------------------------------------------------------------------
  1231. UnicodeString __fastcall TCustomScpExplorerForm::GetToolbarsButtonsStr()
  1232. {
  1233. UnicodeString Result;
  1234. for (int ToolbarIndex = 0; ToolbarIndex < ComponentCount; ToolbarIndex++)
  1235. {
  1236. UnicodeString ToolbarStr;
  1237. TTBCustomToolbar * Toolbar = dynamic_cast<TTBCustomToolbar *>(Components[ToolbarIndex]);
  1238. if ((Toolbar != NULL) && (Toolbar != QueueToolbar))
  1239. {
  1240. for (int ItemIndex = 0; ItemIndex < Toolbar->Items->Count; ItemIndex++)
  1241. {
  1242. TTBCustomItem * Item = Toolbar->Items->Items[ItemIndex];
  1243. // Currently all buttons are visible by default, so we can safely remember all hidden buttons.
  1244. // Once we introduce any buttons that aer hidden by default, we would have to remember their initial state
  1245. // and save the changes here only.
  1246. if (!Item->Visible)
  1247. {
  1248. UnicodeString Name = GetToolbarItemName(Item);
  1249. DebugAssert(Name.Pos(L"TBX") == 0);
  1250. AddToList(ToolbarStr, FORMAT(L"%s:0", (Name)), L",");
  1251. }
  1252. }
  1253. }
  1254. if (!ToolbarStr.IsEmpty())
  1255. {
  1256. AddToList(Result, FORMAT(L"%s=%s", (GetToolbarKey(Toolbar->Name), ToolbarStr)), L";");
  1257. }
  1258. }
  1259. return Result;
  1260. }
  1261. //---------------------------------------------------------------------------
  1262. void __fastcall TCustomScpExplorerForm::CreateProgressForm(TSynchronizeProgress * SynchronizeProgress)
  1263. {
  1264. DebugAssert(FProgressForm == NULL);
  1265. FProgressForm =
  1266. new TProgressForm(Application, (FTransferResumeList != NULL), Terminal->IsCapable[fsSkipTransfer], SynchronizeProgress);
  1267. FProgressForm->DeleteLocalToRecycleBin =
  1268. (WinConfiguration->DeleteToRecycleBin != FAlternativeDelete);
  1269. FProgressForm->DeleteRemoteToRecycleBin =
  1270. (Terminal->SessionData->DeleteToRecycleBin != FAlternativeDelete) &&
  1271. !Terminal->SessionData->RecycleBinPath.IsEmpty();
  1272. // As progress window has delayed show now, we need to lock ourselves, (at least) until then
  1273. LockWindow();
  1274. if (FTaskbarList != NULL)
  1275. {
  1276. // Actually, do not know what hides the progress once the operation finishes
  1277. SetTaskbarListProgressState(TBPF_NORMAL);
  1278. }
  1279. }
  1280. //---------------------------------------------------------------------------
  1281. void __fastcall TCustomScpExplorerForm::DestroyProgressForm()
  1282. {
  1283. UnlockWindow();
  1284. SAFE_DESTROY(FProgressForm);
  1285. SetQueueProgress();
  1286. }
  1287. //---------------------------------------------------------------------------
  1288. void __fastcall TCustomScpExplorerForm::FileOperationProgress(
  1289. TFileOperationProgressType & ProgressData)
  1290. {
  1291. // Download to temporary local directory
  1292. if (FPendingTempSpaceWarn && ProgressData.InProgress && ProgressData.TotalSizeSet)
  1293. {
  1294. bool Continue = true;
  1295. FPendingTempSpaceWarn = false;
  1296. DoWarnLackOfTempSpace(ProgressData.Directory, ProgressData.TotalSize, Continue);
  1297. if (!Continue)
  1298. {
  1299. Abort();
  1300. }
  1301. }
  1302. // operation is being executed and we still didn't show up progress form
  1303. if (ProgressData.InProgress && !FProgressForm)
  1304. {
  1305. CreateProgressForm(NULL);
  1306. // When main window is hidden, synchronisation form does not exist (we do not get here during synchronization - only comparing),
  1307. // we suppose "/upload" or URL download mode
  1308. if (!Visible && (ProgressData.Operation != foCalculateSize) &&
  1309. (ProgressData.Operation != foCalculateChecksum) &&
  1310. // Hack to prevent renames from DoSynchronizeMove (synchronize checklist) closing the connection
  1311. (ProgressData.Operation != foRemoteMove) &&
  1312. (FSynchronizeProgressForm == NULL) &&
  1313. !FStandaloneEditing)
  1314. {
  1315. FProgressForm->OnceDoneOperation = odoDisconnect;
  1316. }
  1317. }
  1318. // operation is finished (or terminated), so we hide progress form
  1319. else if (!ProgressData.InProgress && (FProgressForm != NULL) && (FProgressForm->SynchronizeProgress == NULL))
  1320. {
  1321. DestroyProgressForm();
  1322. if (ProgressData.Operation == foCalculateSize)
  1323. {
  1324. // When calculating size before transfer, the abort caused by
  1325. // cancel flag set due to "MoveToQueue" below,
  1326. // is not propagated back to us in ExecuteFileOperation,
  1327. // as it is silently swallowed.
  1328. // So we explicitly (re)throw it here.
  1329. if (FMoveToQueue)
  1330. {
  1331. Abort();
  1332. }
  1333. }
  1334. else
  1335. {
  1336. if ((ProgressData.Cancel == csContinue) &&
  1337. !FAutoOperation)
  1338. {
  1339. OperationComplete(ProgressData.StartTime);
  1340. }
  1341. }
  1342. }
  1343. if (FProgressForm &&
  1344. // Can happen only during synchronization, when ending one of the operations
  1345. ProgressData.InProgress)
  1346. {
  1347. FProgressForm->SetProgressData(ProgressData);
  1348. if (FTaskbarList != NULL)
  1349. {
  1350. DebugAssert(ProgressData.InProgress);
  1351. SetTaskbarListProgressValue(&ProgressData);
  1352. }
  1353. if (FProgressForm->Cancel > csContinue)
  1354. {
  1355. ProgressData.SetCancelAtLeast(FProgressForm->Cancel);
  1356. if (FProgressForm->Cancel == csCancelFile)
  1357. {
  1358. FProgressForm->ClearCancel();
  1359. }
  1360. // cancel cancels even the move
  1361. FMoveToQueue = false;
  1362. }
  1363. else if (FProgressForm->MoveToQueue)
  1364. {
  1365. FMoveToQueue = true;
  1366. ProgressData.SetCancelAtLeast(csCancel);
  1367. }
  1368. if ((FTransferResumeList != NULL) &&
  1369. ProgressData.InProgress &&
  1370. ((ProgressData.Operation == foCopy) || (ProgressData.Operation == foMove)) &&
  1371. !ProgressData.FullFileName.IsEmpty())
  1372. {
  1373. if ((FTransferResumeList->Count == 0) ||
  1374. (FTransferResumeList->Strings[FTransferResumeList->Count - 1] != ProgressData.FullFileName))
  1375. {
  1376. // note that we do not recognize directories from files here
  1377. FTransferResumeList->Add(ProgressData.FullFileName);
  1378. }
  1379. }
  1380. }
  1381. }
  1382. //---------------------------------------------------------------------------
  1383. void __fastcall TCustomScpExplorerForm::OperationComplete(
  1384. const TDateTime & StartTime)
  1385. {
  1386. if (GUIConfiguration->BeepOnFinish &&
  1387. (Now() - StartTime > GUIConfiguration->BeepOnFinishAfter))
  1388. {
  1389. UnicodeString BeepSound = GUIConfiguration->BeepSound;
  1390. DWORD Sound;
  1391. if (!ExtractFileExt(BeepSound).IsEmpty())
  1392. {
  1393. Sound = SND_FILENAME;
  1394. }
  1395. else
  1396. {
  1397. Sound = SND_ALIAS;
  1398. }
  1399. PlaySound(BeepSound.c_str(), NULL, Sound | SND_ASYNC);
  1400. }
  1401. }
  1402. //---------------------------------------------------------------------------
  1403. void __fastcall TCustomScpExplorerForm::OperationProgress(
  1404. TFileOperationProgressType & ProgressData)
  1405. {
  1406. // For example, when calling TFileOperationProgressType::Suspend in TTerminal::QueryReopen,
  1407. // we cannot recurse back to TTerminal::Idle as that may detect another error, calling "suspend" again.
  1408. TAutoNestingCounter Counter(FDoNotIdleCurrentTerminal);
  1409. FileOperationProgress(ProgressData);
  1410. }
  1411. //---------------------------------------------------------------------------
  1412. UnicodeString __fastcall TCustomScpExplorerForm::GetProgressTitle()
  1413. {
  1414. UnicodeString Result;
  1415. if (FProgressForm != NULL)
  1416. {
  1417. Result = FProgressForm->ProgressStr();
  1418. }
  1419. return Result;
  1420. }
  1421. //---------------------------------------------------------------------------
  1422. bool __fastcall TCustomScpExplorerForm::PanelOperation(TOperationSide /*Side*/,
  1423. bool DragDrop)
  1424. {
  1425. return (!DragDrop && (DropSourceControl == NULL)) ||
  1426. (DropSourceControl == RemoteDirView);
  1427. }
  1428. //---------------------------------------------------------------------------
  1429. void __fastcall TCustomScpExplorerForm::DoOperationFinished(
  1430. TFileOperation Operation, TOperationSide Side,
  1431. bool /*Temp*/, const UnicodeString & FileName, bool Success,
  1432. TOnceDoneOperation & OnceDoneOperation)
  1433. {
  1434. if (!FAutoOperation)
  1435. {
  1436. // no selection on "/upload", form servers only as event handler
  1437. // (it is not displayed)
  1438. if (PanelOperation(Side, FDragDropOperation) &&
  1439. Visible && (Operation != foCalculateSize) &&
  1440. (Operation != foGetProperties) && (Operation != foCalculateChecksum))
  1441. {
  1442. if (FOnFileOperationFinished != NULL)
  1443. {
  1444. FOnFileOperationFinished(FileName, Success);
  1445. }
  1446. else
  1447. {
  1448. TCustomDirView * DView = DirView(Side);
  1449. UnicodeString FileNameOnly = ExtractFileName(FileName, (Side == osRemote));
  1450. TListItem *Item = DView->FindFileItem(FileNameOnly);
  1451. // this can happen when local drive is unplugged in the middle of the operation
  1452. if (Item != NULL)
  1453. {
  1454. if (Success) Item->Selected = false;
  1455. DView->MakeProgressVisible(Item);
  1456. }
  1457. }
  1458. }
  1459. if ((Operation == foCopy) || (Operation == foMove))
  1460. {
  1461. if (Side == osLocal)
  1462. {
  1463. Configuration->Usage->Inc(L"UploadedFiles");
  1464. }
  1465. else
  1466. {
  1467. Configuration->Usage->Inc(L"DownloadedFiles");
  1468. }
  1469. }
  1470. }
  1471. if (Success && (FSynchronizeController != NULL))
  1472. {
  1473. if (Operation == foCopy)
  1474. {
  1475. DebugAssert(Side == osLocal);
  1476. FSynchronizeController->LogOperation(soUpload, FileName);
  1477. }
  1478. else if (Operation == foDelete)
  1479. {
  1480. DebugAssert(Side == osRemote);
  1481. FSynchronizeController->LogOperation(soDelete, FileName);
  1482. }
  1483. }
  1484. if (FProgressForm)
  1485. {
  1486. OnceDoneOperation = FProgressForm->OnceDoneOperation;
  1487. }
  1488. }
  1489. //---------------------------------------------------------------------------
  1490. void __fastcall TCustomScpExplorerForm::OperationFinished(
  1491. TFileOperation Operation, TOperationSide Side,
  1492. bool Temp, const UnicodeString & FileName, Boolean Success,
  1493. TOnceDoneOperation & OnceDoneOperation)
  1494. {
  1495. TAutoNestingCounter Counter(FDoNotIdleCurrentTerminal);
  1496. DoOperationFinished(Operation, Side, Temp, FileName, Success,
  1497. OnceDoneOperation);
  1498. }
  1499. //---------------------------------------------------------------------------
  1500. TCustomDirView * __fastcall TCustomScpExplorerForm::DirView(TOperationSide Side)
  1501. {
  1502. DebugAssert(GetSide(Side) == osRemote);
  1503. DebugUsedParam(Side);
  1504. return RemoteDirView;
  1505. }
  1506. //---------------------------------------------------------------------------
  1507. bool __fastcall TCustomScpExplorerForm::DirViewEnabled(TOperationSide Side)
  1508. {
  1509. DebugAssert(GetSide(Side) == osRemote);
  1510. DebugUsedParam(Side);
  1511. return (Terminal != NULL);
  1512. }
  1513. //---------------------------------------------------------------------------
  1514. bool __fastcall TCustomScpExplorerForm::GetEnableFocusedOperation(
  1515. TOperationSide Side, int FilesOnly)
  1516. {
  1517. return DirView(Side)->AnyFileSelected(true, (FilesOnly != 0), true);
  1518. }
  1519. //---------------------------------------------------------------------------
  1520. bool __fastcall TCustomScpExplorerForm::GetEnableSelectedOperation(
  1521. TOperationSide Side, int FilesOnly)
  1522. {
  1523. return DirView(Side)->AnyFileSelected(false, (FilesOnly != 0), true);
  1524. }
  1525. //---------------------------------------------------------------------------
  1526. struct THistoryItemData
  1527. {
  1528. short int Side;
  1529. short int Index;
  1530. };
  1531. //---------------------------------------------------------------------------
  1532. void __fastcall TCustomScpExplorerForm::HistoryGo(TOperationSide Side, int Index)
  1533. {
  1534. DirView(Side)->HistoryGo(Index);
  1535. }
  1536. //---------------------------------------------------------------------------
  1537. void __fastcall TCustomScpExplorerForm::HistoryItemClick(System::TObject* Sender)
  1538. {
  1539. TTBCustomItem * Item = dynamic_cast<TTBCustomItem *>(Sender);
  1540. THistoryItemData Data = *reinterpret_cast<THistoryItemData*>(&(Item->Tag));
  1541. HistoryGo((TOperationSide)Data.Side, Data.Index);
  1542. }
  1543. //---------------------------------------------------------------------------
  1544. void __fastcall TCustomScpExplorerForm::UpdateHistoryMenu(TOperationSide Side,
  1545. bool Back)
  1546. {
  1547. if (FHistoryMenu[Side == osLocal][Back] != NULL)
  1548. {
  1549. TCustomDirView * DView = DirView(Side);
  1550. TTBXPopupMenu * Menu = FHistoryMenu[Side == osLocal][Back];
  1551. int ICount = Back ? DView->BackCount : DView->ForwardCount;
  1552. if (ICount > 10)
  1553. {
  1554. ICount = 10;
  1555. }
  1556. Menu->Items->Clear();
  1557. THistoryItemData Data;
  1558. Data.Side = Side;
  1559. for (short int i = 1; i <= ICount; i++)
  1560. {
  1561. TTBCustomItem * Item = new TTBXItem(Menu);
  1562. Data.Index = static_cast<short int>(i * (Back ? -1 : 1));
  1563. Item->Caption = MinimizeName(DView->HistoryPath[Data.Index], 50, (Side == osRemote));
  1564. Item->Hint = DView->HistoryPath[Data.Index];
  1565. DebugAssert(sizeof(int) == sizeof(THistoryItemData));
  1566. Item->Tag = *reinterpret_cast<int*>(&Data);
  1567. Item->OnClick = HistoryItemClick;
  1568. Menu->Items->Add(Item);
  1569. }
  1570. }
  1571. }
  1572. //---------------------------------------------------------------------------
  1573. TTBXPopupMenu * __fastcall TCustomScpExplorerForm::HistoryMenu(
  1574. TOperationSide Side, bool Back)
  1575. {
  1576. if (FHistoryMenu[Side == osLocal][Back] == NULL)
  1577. {
  1578. // workaround
  1579. // In Pascal the size of TTBXPopupMenu is 132, in C++ 136,
  1580. // operator new allocates memory in Pascal code, but calls inline
  1581. // contructor in C++, leading in problems, the function does
  1582. // both in Pascal code
  1583. FHistoryMenu[Side == osLocal][Back] = CreateTBXPopupMenu(this);
  1584. UpdateHistoryMenu(Side, Back);
  1585. }
  1586. return FHistoryMenu[Side == osLocal][Back];
  1587. }
  1588. //---------------------------------------------------------------------------
  1589. void __fastcall TCustomScpExplorerForm::DirViewHistoryChange(
  1590. TCustomDirView *Sender)
  1591. {
  1592. TOperationSide Side = (Sender == DirView(osRemote) ? osRemote : osLocal);
  1593. UpdateHistoryMenu(Side, true);
  1594. UpdateHistoryMenu(Side, false);
  1595. }
  1596. //---------------------------------------------------------------------------
  1597. bool __fastcall TCustomScpExplorerForm::CustomCommandRemoteAllowed()
  1598. {
  1599. // remote custom commands can be executed only if the server supports shell commands
  1600. // or have secondary shell
  1601. return (FTerminal != NULL) && (FTerminal->IsCapable[fcSecondaryShell] || FTerminal->IsCapable[fcShellAnyCommand]);
  1602. }
  1603. //---------------------------------------------------------------------------
  1604. int __fastcall TCustomScpExplorerForm::CustomCommandState(
  1605. const TCustomCommandType & Command, bool OnFocused, TCustomCommandListType ListType)
  1606. {
  1607. int Result;
  1608. TFileCustomCommand RemoteCustomCommand;
  1609. TLocalCustomCommand LocalCustomCommand;
  1610. TFileCustomCommand * NonInteractiveCustomCommand =
  1611. FLAGCLEAR(Command.Params, ccLocal) ? &RemoteCustomCommand : &LocalCustomCommand;
  1612. TInteractiveCustomCommand InteractiveCustomCommand(NonInteractiveCustomCommand);
  1613. UnicodeString Cmd = InteractiveCustomCommand.Complete(Command.Command, false);
  1614. if (FLAGCLEAR(Command.Params, ccLocal))
  1615. {
  1616. int AllowedState = CustomCommandRemoteAllowed() ? 1 : 0;
  1617. // custom command that does not operate with files can be executed anytime ...
  1618. if (!NonInteractiveCustomCommand->IsFileCommand(Cmd))
  1619. {
  1620. if ((ListType == ccltAll) || (ListType == ccltNonFile))
  1621. {
  1622. // ... but do not show such command in remote file menu (TODO)
  1623. Result = AllowedState;
  1624. }
  1625. else
  1626. {
  1627. Result = -1;
  1628. }
  1629. }
  1630. else
  1631. {
  1632. if ((ListType == ccltAll) || (ListType == ccltFile))
  1633. {
  1634. Result = ((FCurrentSide == osRemote) && DirView(osRemote)->AnyFileSelected(OnFocused, false, true)) ? AllowedState : 0;
  1635. }
  1636. else
  1637. {
  1638. Result = -1;
  1639. }
  1640. }
  1641. }
  1642. else
  1643. {
  1644. // custom command that does not operate with files can be executed anytime
  1645. if (!NonInteractiveCustomCommand->IsFileCommand(Cmd))
  1646. {
  1647. Result = ((ListType == ccltAll) || (ListType == ccltNonFile)) ? 1 : -1;
  1648. }
  1649. else if (LocalCustomCommand.HasLocalFileName(Cmd))
  1650. {
  1651. if ((ListType == ccltAll) || (ListType == ccltFile))
  1652. {
  1653. // special case is "diff"-style command that can be executed over any side,
  1654. // if we have both sides
  1655. Result =
  1656. // Cannot have focus on both panels, so we have to call AnyFileSelected
  1657. // directly (instead of EnableSelectedOperation) to pass
  1658. // false to FocusedFileOnlyWhenFocused when panel is inactive.
  1659. ((HasDirView[osLocal] && DirView(osLocal)->AnyFileSelected(false, false, (FCurrentSide == osLocal))) &&
  1660. DirView(osRemote)->AnyFileSelected(false, false, (FCurrentSide == osRemote))) ? 1 : 0;
  1661. }
  1662. else if (ListType == ccltBoth)
  1663. {
  1664. Result = 1;
  1665. }
  1666. else
  1667. {
  1668. Result = -1;
  1669. }
  1670. }
  1671. else
  1672. {
  1673. if ((ListType == ccltAll) || (ListType == ccltFile))
  1674. {
  1675. Result = DirView(FCurrentSide)->AnyFileSelected(OnFocused, false, true) ? 1 : 0;
  1676. }
  1677. else
  1678. {
  1679. Result = -1;
  1680. }
  1681. }
  1682. }
  1683. return Result;
  1684. }
  1685. //---------------------------------------------------------------------------
  1686. void __fastcall TCustomScpExplorerForm::RemoteCustomCommand(
  1687. TStrings * FileList, const TCustomCommandType & ACommand,
  1688. const TCustomCommandData & Data, const UnicodeString & CommandCommand)
  1689. {
  1690. if (EnsureCommandSessionFallback(fcShellAnyCommand))
  1691. {
  1692. TRemoteCustomCommand RemoteCustomCommand(Data, Terminal->CurrentDirectory);
  1693. TWinInteractiveCustomCommand InteractiveCustomCommand(
  1694. &RemoteCustomCommand, ACommand.Name, ACommand.HomePage);
  1695. UnicodeString Command = InteractiveCustomCommand.Complete(CommandCommand, false);
  1696. Configuration->Usage->Inc(L"RemoteCustomCommandRuns2");
  1697. bool Capture =
  1698. FLAGSET(ACommand.Params, ccShowResults) ||
  1699. FLAGSET(ACommand.Params, ccCopyResults) ||
  1700. FLAGSET(ACommand.Params, ccShowResultsInMsgBox);
  1701. TCaptureOutputEvent OutputEvent = NULL;
  1702. DebugAssert(FCapturedLog == NULL);
  1703. if (Capture)
  1704. {
  1705. FCapturedLog = new TStringList();
  1706. OutputEvent = TerminalCaptureLog;
  1707. }
  1708. try
  1709. {
  1710. if (!RemoteCustomCommand.IsFileCommand(Command))
  1711. {
  1712. Terminal->AnyCommand(RemoteCustomCommand.Complete(Command, true),
  1713. OutputEvent);
  1714. }
  1715. else
  1716. {
  1717. Terminal->CustomCommandOnFiles(Command, ACommand.Params, FileList, OutputEvent);
  1718. }
  1719. if ((FCapturedLog != NULL) && (FCapturedLog->Count > 0))
  1720. {
  1721. if (FLAGSET(ACommand.Params, ccCopyResults))
  1722. {
  1723. CopyToClipboard(FCapturedLog);
  1724. }
  1725. if (FLAGSET(ACommand.Params, ccShowResults))
  1726. {
  1727. DoConsoleDialog(Terminal, L"", FCapturedLog);
  1728. }
  1729. else if (FLAGSET(ACommand.Params, ccShowResultsInMsgBox))
  1730. {
  1731. MessageDialog(FCapturedLog->Text, qtInformation, qaOK);
  1732. }
  1733. }
  1734. }
  1735. __finally
  1736. {
  1737. SAFE_DESTROY(FCapturedLog);
  1738. }
  1739. }
  1740. }
  1741. //---------------------------------------------------------------------------
  1742. void __fastcall TCustomScpExplorerForm::LocalCustomCommandPure(
  1743. TStrings * FileList, const TCustomCommandType & ACommand, const UnicodeString & Command, TStrings * ALocalFileList,
  1744. const TCustomCommandData & Data, bool LocalFileCommand, bool FileListCommand, UnicodeString * POutput)
  1745. {
  1746. TStrings * LocalFileList = NULL;
  1747. TStrings * RemoteFileList = NULL;
  1748. TStrings * RemoteFileListFull = NULL;
  1749. try
  1750. {
  1751. if (LocalFileCommand)
  1752. {
  1753. if (ALocalFileList == NULL)
  1754. {
  1755. DebugAssert(HasDirView[osLocal]);
  1756. // Cannot have focus on both panels, so we have to call AnyFileSelected
  1757. // directly (instead of EnableSelectedOperation) to pass
  1758. // false to FocusedFileOnlyWhenFocused
  1759. DebugAssert(DirView(osLocal)->AnyFileSelected(false, false, false));
  1760. LocalFileList = DirView(osLocal)->CreateFileList(false, true, NULL);
  1761. }
  1762. else
  1763. {
  1764. LocalFileList = ALocalFileList;
  1765. }
  1766. if (FileListCommand)
  1767. {
  1768. if (LocalFileList->Count != 1)
  1769. {
  1770. throw Exception(LoadStr(CUSTOM_COMMAND_SELECTED_UNMATCH1));
  1771. }
  1772. }
  1773. else
  1774. {
  1775. if ((LocalFileList->Count != 1) &&
  1776. (FileList->Count != 1) &&
  1777. (LocalFileList->Count != FileList->Count))
  1778. {
  1779. throw Exception(LoadStr(CUSTOM_COMMAND_SELECTED_UNMATCH));
  1780. }
  1781. }
  1782. }
  1783. UnicodeString RootTempDir;
  1784. UnicodeString TempDir;
  1785. bool RemoteFiles = FLAGSET(ACommand.Params, ccRemoteFiles);
  1786. if (!RemoteFiles)
  1787. {
  1788. TemporarilyDownloadFiles(FileList, false, RootTempDir, TempDir, false, false, true);
  1789. }
  1790. try
  1791. {
  1792. TDateTimes RemoteFileTimes;
  1793. if (RemoteFiles)
  1794. {
  1795. RemoteFileList = FileList;
  1796. }
  1797. else
  1798. {
  1799. RemoteFileList = new TStringList();
  1800. TMakeLocalFileListParams MakeFileListParam;
  1801. MakeFileListParam.FileList = RemoteFileList;
  1802. MakeFileListParam.FileTimes = &RemoteFileTimes;
  1803. MakeFileListParam.IncludeDirs = FLAGSET(ACommand.Params, ccApplyToDirectories);
  1804. MakeFileListParam.Recursive =
  1805. FLAGSET(ACommand.Params, ccRecursive) && !FileListCommand;
  1806. ProcessLocalDirectory(TempDir, Terminal->MakeLocalFileList, &MakeFileListParam);
  1807. if (!MakeFileListParam.Recursive)
  1808. {
  1809. RemoteFileTimes.clear();
  1810. RemoteFileListFull = new TStringList();
  1811. MakeFileListParam.FileList = RemoteFileListFull;
  1812. MakeFileListParam.Recursive = true;
  1813. MakeFileListParam.IncludeDirs = false;
  1814. ProcessLocalDirectory(TempDir, Terminal->MakeLocalFileList, &MakeFileListParam);
  1815. }
  1816. else
  1817. {
  1818. RemoteFileListFull = RemoteFileList;
  1819. }
  1820. }
  1821. bool NonBlocking = FileListCommand && RemoteFiles && !POutput;
  1822. TFileOperationProgressType Progress(&OperationProgress, &OperationFinished);
  1823. if (!NonBlocking)
  1824. {
  1825. int Count;
  1826. if (FileListCommand)
  1827. {
  1828. Count = 1;
  1829. }
  1830. else if (LocalFileCommand)
  1831. {
  1832. Count = std::max(LocalFileList->Count, RemoteFileList->Count);
  1833. }
  1834. else
  1835. {
  1836. Count = RemoteFileList->Count;
  1837. }
  1838. Progress.Start(foCustomCommand, osRemote, Count);
  1839. DebugAssert(FProgressForm != NULL);
  1840. FProgressForm->ReadOnly = true;
  1841. }
  1842. try
  1843. {
  1844. if (FileListCommand)
  1845. {
  1846. UnicodeString LocalFile;
  1847. // MakeFileList does not delimit filenames
  1848. UnicodeString FileList = MakeFileList(RemoteFileList);
  1849. if (LocalFileCommand)
  1850. {
  1851. DebugAssert(LocalFileList->Count == 1);
  1852. LocalFile = LocalFileList->Strings[0];
  1853. }
  1854. TLocalCustomCommand CustomCommand(Data,
  1855. Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(), L"", LocalFile, FileList);
  1856. UnicodeString ShellCommand = CustomCommand.Complete(Command, true);
  1857. if (NonBlocking)
  1858. {
  1859. DebugAssert(!POutput);
  1860. ExecuteShellChecked(ShellCommand);
  1861. }
  1862. else
  1863. {
  1864. ExecuteProcessCheckedAndWait(ShellCommand, HelpKeyword, POutput);
  1865. }
  1866. }
  1867. else if (LocalFileCommand)
  1868. {
  1869. if (LocalFileList->Count == 1)
  1870. {
  1871. UnicodeString LocalFile = LocalFileList->Strings[0];
  1872. for (int Index = 0; Index < RemoteFileList->Count; Index++)
  1873. {
  1874. UnicodeString FileName = RemoteFileList->Strings[Index];
  1875. UnicodeString FileNameForProgress = RemoteFiles ? FileName : ExtractFileName(FileName);
  1876. Progress.SetFile(FileNameForProgress);
  1877. TLocalCustomCommand CustomCommand(Data,
  1878. Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(), FileName, LocalFile, L"");
  1879. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  1880. TOnceDoneOperation OnceDoneOperation;
  1881. // Do not unselect anything, as it won't work with two panels anyway
  1882. Progress.Finish(UnicodeString(), true, OnceDoneOperation);
  1883. }
  1884. }
  1885. else if (RemoteFileList->Count == 1)
  1886. {
  1887. UnicodeString FileName = RemoteFileList->Strings[0];
  1888. for (int Index = 0; Index < LocalFileList->Count; Index++)
  1889. {
  1890. UnicodeString LocalFileName = LocalFileList->Strings[Index];
  1891. UnicodeString LocalFileNameOnly = ExtractFileName(LocalFileName);
  1892. Progress.SetFile(LocalFileNameOnly);
  1893. TLocalCustomCommand CustomCommand(
  1894. Data, Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(),
  1895. FileName, LocalFileName, L"");
  1896. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  1897. TOnceDoneOperation OnceDoneOperation;
  1898. // Do not unselect anything, as it won't work with two panels anyway
  1899. Progress.Finish(UnicodeString(), true, OnceDoneOperation);
  1900. }
  1901. }
  1902. else
  1903. {
  1904. if (LocalFileList->Count != RemoteFileList->Count)
  1905. {
  1906. throw Exception(LoadStr(CUSTOM_COMMAND_PAIRS_DOWNLOAD_FAILED));
  1907. }
  1908. for (int Index = 0; Index < LocalFileList->Count; Index++)
  1909. {
  1910. UnicodeString FileName = RemoteFileList->Strings[Index];
  1911. UnicodeString LocalFileName = LocalFileList->Strings[Index];
  1912. UnicodeString LocalFileNameOnly = ExtractFileName(LocalFileName);
  1913. Progress.SetFile(LocalFileNameOnly); // sic
  1914. TLocalCustomCommand CustomCommand(
  1915. Data, Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(),
  1916. FileName, LocalFileName, L"");
  1917. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  1918. TOnceDoneOperation OnceDoneOperation;
  1919. // Do not unselect anything, as it won't work with two panels anyway
  1920. Progress.Finish(UnicodeString(), true, OnceDoneOperation);
  1921. }
  1922. }
  1923. }
  1924. else
  1925. {
  1926. for (int Index = 0; Index < RemoteFileList->Count; Index++)
  1927. {
  1928. UnicodeString FileName = RemoteFileList->Strings[Index];
  1929. UnicodeString FileNameForProgress = RemoteFiles ? FileName : ExtractFileName(FileName);
  1930. Progress.SetFile(FileNameForProgress);
  1931. TLocalCustomCommand CustomCommand(Data,
  1932. Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(),
  1933. FileName, L"", L"");
  1934. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  1935. TOnceDoneOperation OnceDoneOperation;
  1936. Progress.Finish(FileNameForProgress, true, OnceDoneOperation);
  1937. }
  1938. }
  1939. }
  1940. __finally
  1941. {
  1942. if (!NonBlocking)
  1943. {
  1944. Progress.Stop();
  1945. }
  1946. }
  1947. DebugAssert(!FAutoOperation);
  1948. if (!RemoteFiles)
  1949. {
  1950. TempDir = IncludeTrailingBackslash(TempDir);
  1951. for (int Index = 0; Index < RemoteFileListFull->Count; Index++)
  1952. {
  1953. UnicodeString FileName = RemoteFileListFull->Strings[Index];
  1954. if (DebugAlwaysTrue(SameText(TempDir, FileName.SubString(1, TempDir.Length()))) &&
  1955. // Skip directories, process only nested files.
  1956. // The check is redundant as FileAge fails for directories anyway.
  1957. !DirectoryExists(FileName))
  1958. {
  1959. UnicodeString RemoteDir =
  1960. UnixExtractFileDir(
  1961. UnixIncludeTrailingBackslash(FTerminal->CurrentDirectory) +
  1962. ToUnixPath(FileName.SubString(TempDir.Length() + 1, FileName.Length() - TempDir.Length())));
  1963. TDateTime NewTime;
  1964. if (FileAge(FileName, NewTime) &&
  1965. (NewTime != RemoteFileTimes[Index]))
  1966. {
  1967. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  1968. TemporaryFileCopyParam(CopyParam);
  1969. CopyParam.FileMask = L"";
  1970. FAutoOperation = true;
  1971. std::unique_ptr<TStrings> TemporaryFilesList(new TStringList());
  1972. TemporaryFilesList->Add(FileName);
  1973. FTerminal->CopyToRemote(TemporaryFilesList.get(), RemoteDir, &CopyParam, cpTemporary, NULL);
  1974. }
  1975. }
  1976. }
  1977. }
  1978. }
  1979. __finally
  1980. {
  1981. FAutoOperation = false;
  1982. if (!RootTempDir.IsEmpty() && DebugAlwaysTrue(!RemoteFiles))
  1983. {
  1984. RecursiveDeleteFile(ExcludeTrailingBackslash(RootTempDir), false);
  1985. }
  1986. }
  1987. }
  1988. __finally
  1989. {
  1990. if (RemoteFileList != FileList)
  1991. {
  1992. delete RemoteFileList;
  1993. }
  1994. if (LocalFileList != ALocalFileList)
  1995. {
  1996. delete LocalFileList;
  1997. }
  1998. if (RemoteFileListFull != RemoteFileList)
  1999. {
  2000. delete RemoteFileListFull;
  2001. }
  2002. }
  2003. }
  2004. //---------------------------------------------------------------------------
  2005. void __fastcall TCustomScpExplorerForm::LocalCustomCommandWithLocalFiles(
  2006. const TCustomCommandType & ACommand, const UnicodeString & Command, const TCustomCommandData & Data,
  2007. bool FileListCommand, UnicodeString * POutput)
  2008. {
  2009. std::unique_ptr<TStrings> SelectedFileList(DirView(osLocal)->CreateFileList(false, true, NULL));
  2010. std::unique_ptr<TStrings> LocalFileList(new TStringList());
  2011. for (int Index = 0; Index < SelectedFileList->Count; Index++)
  2012. {
  2013. UnicodeString FileName = SelectedFileList->Strings[Index];
  2014. if (DirectoryExists(FileName))
  2015. {
  2016. if (FLAGSET(ACommand.Params, ccApplyToDirectories))
  2017. {
  2018. LocalFileList->Add(FileName);
  2019. }
  2020. if (FLAGSET(ACommand.Params, ccRecursive))
  2021. {
  2022. TMakeLocalFileListParams MakeFileListParam;
  2023. MakeFileListParam.FileList = LocalFileList.get();
  2024. MakeFileListParam.FileTimes = NULL;
  2025. MakeFileListParam.IncludeDirs = FLAGSET(ACommand.Params, ccApplyToDirectories);
  2026. MakeFileListParam.Recursive = true;
  2027. ProcessLocalDirectory(FileName, Terminal->MakeLocalFileList, &MakeFileListParam);
  2028. }
  2029. }
  2030. else
  2031. {
  2032. LocalFileList->Add(FileName);
  2033. }
  2034. }
  2035. if (FileListCommand)
  2036. {
  2037. UnicodeString FileList = MakeFileList(LocalFileList.get());
  2038. TLocalCustomCommand CustomCommand(
  2039. Data, Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(),
  2040. L"", L"", FileList);
  2041. ExecuteProcessChecked(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  2042. }
  2043. else
  2044. {
  2045. TFileOperationProgressType Progress(&OperationProgress, &OperationFinished);
  2046. Progress.Start(foCustomCommand, osLocal, LocalFileList->Count);
  2047. DebugAssert(FProgressForm != NULL);
  2048. FProgressForm->ReadOnly = true;
  2049. try
  2050. {
  2051. for (int Index = 0; Index < LocalFileList->Count; Index++)
  2052. {
  2053. UnicodeString FileName = LocalFileList->Strings[Index];
  2054. Progress.SetFile(FileName);
  2055. TLocalCustomCommand CustomCommand(
  2056. Data, Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(),
  2057. FileName, L"", L"");
  2058. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  2059. TOnceDoneOperation OnceDoneOperation;
  2060. Progress.Finish(FileName, true, OnceDoneOperation);
  2061. }
  2062. }
  2063. __finally
  2064. {
  2065. Progress.Stop();
  2066. }
  2067. }
  2068. }
  2069. //---------------------------------------------------------------------------
  2070. void __fastcall TCustomScpExplorerForm::LocalCustomCommand(TStrings * FileList,
  2071. const TCustomCommandType & ACommand, TStrings * ALocalFileList,
  2072. const TCustomCommandData & Data, const UnicodeString & CommandCommand)
  2073. {
  2074. TLocalCustomCommand LocalCustomCommand(Data, Terminal->CurrentDirectory, DefaultDownloadTargetDirectory());
  2075. TWinInteractiveCustomCommand InteractiveCustomCommand(
  2076. &LocalCustomCommand, ACommand.Name, ACommand.HomePage);
  2077. UnicodeString Command = InteractiveCustomCommand.Complete(CommandCommand, false);
  2078. bool FileListCommand = LocalCustomCommand.IsFileListCommand(Command);
  2079. bool LocalFileCommand = LocalCustomCommand.HasLocalFileName(Command);
  2080. Configuration->Usage->Inc(L"LocalCustomCommandRuns2");
  2081. std::unique_ptr<UnicodeString> POutput;
  2082. if (FLAGSET(ACommand.Params, ccShowResultsInMsgBox) ||
  2083. FLAGSET(ACommand.Params, ccCopyResults))
  2084. {
  2085. POutput.reset(new UnicodeString());
  2086. }
  2087. if (!LocalCustomCommand.IsFileCommand(Command))
  2088. {
  2089. ExecuteProcessChecked(LocalCustomCommand.Complete(Command, true), HelpKeyword, POutput.get());
  2090. }
  2091. // remote files?
  2092. else if ((FCurrentSide == osRemote) || LocalFileCommand)
  2093. {
  2094. LocalCustomCommandPure(FileList, ACommand, Command, ALocalFileList, Data, LocalFileCommand, FileListCommand, POutput.get());
  2095. }
  2096. // local files
  2097. else
  2098. {
  2099. LocalCustomCommandWithLocalFiles(ACommand, Command, Data, FileListCommand, POutput.get());
  2100. }
  2101. if (POutput.get() != NULL)
  2102. {
  2103. // If the output is single-line, we do not want the trailing CRLF, as the CopyToClipboard(TStrings *) does
  2104. int P = POutput->Pos(sLineBreak);
  2105. if (P == POutput->Length() - static_cast<int>(strlen(sLineBreak)) + 1)
  2106. {
  2107. POutput->SetLength(P - 1);
  2108. }
  2109. // Copy even empty output to clipboard
  2110. if (FLAGSET(ACommand.Params, ccCopyResults))
  2111. {
  2112. CopyToClipboard(*POutput);
  2113. }
  2114. // But do not show an empty message box.
  2115. // This way the ShowResultsInMsgBox can be used to suppress a console window of commands with no output
  2116. if (FLAGSET(ACommand.Params, ccShowResultsInMsgBox) &&
  2117. !POutput->IsEmpty())
  2118. {
  2119. TClipboardHandler ClipboardHandler;
  2120. ClipboardHandler.Text = *POutput;
  2121. TMessageParams Params;
  2122. TQueryButtonAlias Aliases[1];
  2123. Aliases[0].Button = qaRetry;
  2124. Aliases[0].Alias = LoadStr(EDIT_COPY);
  2125. Aliases[0].OnSubmit = &ClipboardHandler.Copy;
  2126. Params.Aliases = Aliases;
  2127. Params.AliasesCount = LENOF(Aliases);
  2128. MessageDialog(*POutput, qtInformation, qaOK | qaRetry, HelpKeyword, &Params);
  2129. }
  2130. }
  2131. }
  2132. //---------------------------------------------------------------------------
  2133. void __fastcall TCustomScpExplorerForm::CustomCommand(TStrings * FileList,
  2134. const TCustomCommandType & ACommand, TStrings * ALocalFileList)
  2135. {
  2136. TCustomCommandData Data(Terminal);
  2137. UnicodeString Site = Terminal->SessionData->SessionKey;
  2138. UnicodeString HelpKeyword = ACommand.HomePage;
  2139. std::unique_ptr<TStrings> CustomCommandOptions(CloneStrings(WinConfiguration->CustomCommandOptions));
  2140. if (ACommand.AnyOptionWithFlag(TCustomCommandType::ofRun))
  2141. {
  2142. std::unique_ptr<TCustomCommand> CustomCommandForOptions;
  2143. if (FLAGCLEAR(ACommand.Params, ccLocal))
  2144. {
  2145. CustomCommandForOptions.reset(new TRemoteCustomCommand(Data, Terminal->CurrentDirectory));
  2146. }
  2147. else
  2148. {
  2149. CustomCommandForOptions.reset(new TLocalCustomCommand(Data, Terminal->CurrentDirectory, DefaultDownloadTargetDirectory()));
  2150. }
  2151. if (!DoCustomCommandOptionsDialog(
  2152. &ACommand, CustomCommandOptions.get(), NULL, TCustomCommandType::ofRun, CustomCommandForOptions.get(), Site, NULL))
  2153. {
  2154. Abort();
  2155. }
  2156. }
  2157. UnicodeString CommandCommand = ACommand.GetCommandWithExpandedOptions(CustomCommandOptions.get(), Site);
  2158. if (FLAGCLEAR(ACommand.Params, ccLocal))
  2159. {
  2160. RemoteCustomCommand(FileList, ACommand, Data, CommandCommand);
  2161. }
  2162. else
  2163. {
  2164. LocalCustomCommand(FileList, ACommand, ALocalFileList, Data, CommandCommand);
  2165. }
  2166. }
  2167. //---------------------------------------------------------------------------
  2168. void __fastcall TCustomScpExplorerForm::BothCustomCommand(
  2169. const TCustomCommandType & Command)
  2170. {
  2171. DebugAssert(FCustomCommandLocalFileList != NULL);
  2172. DebugAssert(FCustomCommandRemoteFileList != NULL);
  2173. DebugAssert(FCustomCommandLocalFileList->Count == FCustomCommandRemoteFileList->Count);
  2174. TStrings * LocalFileList = new TStringList();
  2175. TStrings * RemoteFileList = new TStringList();
  2176. try
  2177. {
  2178. for (int Index = 0; Index < FCustomCommandLocalFileList->Count; Index++)
  2179. {
  2180. LocalFileList->Clear();
  2181. LocalFileList->AddObject(
  2182. FCustomCommandLocalFileList->Strings[Index],
  2183. FCustomCommandLocalFileList->Objects[Index]);
  2184. RemoteFileList->Clear();
  2185. RemoteFileList->AddObject(
  2186. FCustomCommandRemoteFileList->Strings[Index],
  2187. FCustomCommandRemoteFileList->Objects[Index]);
  2188. CustomCommand(RemoteFileList, Command, LocalFileList);
  2189. }
  2190. }
  2191. __finally
  2192. {
  2193. delete LocalFileList;
  2194. delete RemoteFileList;
  2195. }
  2196. }
  2197. //---------------------------------------------------------------------------
  2198. void __fastcall TCustomScpExplorerForm::CustomCommandMenu(
  2199. TAction * Action, TStrings * LocalFileList, TStrings * RemoteFileList)
  2200. {
  2201. delete FCustomCommandLocalFileList;
  2202. delete FCustomCommandRemoteFileList;
  2203. // takeover ownership,
  2204. // the lists must survive the MenuPopup as OnClick occurs only after it exits
  2205. FCustomCommandLocalFileList = LocalFileList;
  2206. FCustomCommandRemoteFileList = RemoteFileList;
  2207. TButton * Button = dynamic_cast<TButton *>(Action->ActionComponent);
  2208. if (Button != NULL)
  2209. {
  2210. FCustomCommandMenu->Items->Clear();
  2211. NonVisualDataModule->CreateCustomCommandsMenu(FCustomCommandMenu->Items, false, false, ccltBoth);
  2212. MenuPopup(FCustomCommandMenu, Button);
  2213. }
  2214. else
  2215. {
  2216. NonVisualDataModule->CreateCustomCommandsMenu(Action, false, ccltBoth);
  2217. }
  2218. }
  2219. //---------------------------------------------------------------------------
  2220. void __fastcall TCustomScpExplorerForm::TerminalCaptureLog(
  2221. const UnicodeString & AddedLine, TCaptureOutputType OutputType)
  2222. {
  2223. DebugAssert(FCapturedLog != NULL);
  2224. if ((OutputType == cotOutput) || (OutputType == cotError))
  2225. {
  2226. FCapturedLog->Add(AddedLine);
  2227. }
  2228. }
  2229. //---------------------------------------------------------------------------
  2230. bool __fastcall TCustomScpExplorerForm::IsFileControl(TObject * Control,
  2231. TOperationSide Side)
  2232. {
  2233. return (Side == osRemote) &&
  2234. ((Control == RemoteDirView) || (Control == RemoteDriveView));
  2235. }
  2236. //---------------------------------------------------------------------------
  2237. void __fastcall TCustomScpExplorerForm::DirViewContextPopupDefaultItem(
  2238. TOperationSide Side, TTBXCustomItem * Item, TDoubleClickAction DoubleClickAction)
  2239. {
  2240. TTBItemOptions O;
  2241. O = Item->Options;
  2242. TCustomDirView * DView = DirView(Side);
  2243. if ((DView->ItemFocused != NULL) &&
  2244. (WinConfiguration->DoubleClickAction == DoubleClickAction) &&
  2245. // when resolving links is disabled, default action is to enter the directory,
  2246. // no matter what DoubleClickAction is configured to
  2247. ((Side != osRemote) || Terminal->ResolvingSymlinks || Terminal->IsEncryptingFiles()) &&
  2248. // Can only Edit files, but can Open/Copy even directories
  2249. ((DoubleClickAction != dcaEdit) ||
  2250. !DView->ItemIsDirectory(DView->ItemFocused)))
  2251. {
  2252. Item->Options = O << tboDefault;
  2253. }
  2254. else
  2255. {
  2256. Item->Options = O >> tboDefault;
  2257. }
  2258. }
  2259. //---------------------------------------------------------------------------
  2260. void __fastcall TCustomScpExplorerForm::DirViewContextPopup(
  2261. TOperationSide Side, Byte PopupComponent, const TPoint &MousePos)
  2262. {
  2263. TCustomDirView * DView = DirView(Side);
  2264. TListItem * Item = DView->ItemFocused;
  2265. if ((DView->GetItemAt(MousePos.x, MousePos.y) == Item) &&
  2266. EnableFocusedOperation[Side])
  2267. {
  2268. TPoint ClientPoint;
  2269. ClientPoint = ((MousePos.x < 0) && (MousePos.y < 0)) ?
  2270. TPoint(0, 0) : MousePos;
  2271. FLastContextPopupScreenPoint = DView->ClientToScreen(ClientPoint);
  2272. reinterpret_cast<TPopupMenu*>(GetComponent(PopupComponent))->Popup(
  2273. FLastContextPopupScreenPoint.x, FLastContextPopupScreenPoint.y);
  2274. }
  2275. }
  2276. //---------------------------------------------------------------------------
  2277. void __fastcall TCustomScpExplorerForm::RemoteDirViewContextPopup(
  2278. TObject * /*Sender*/, const TPoint &MousePos, bool &Handled)
  2279. {
  2280. DirViewContextPopupDefaultItem(osRemote, NonVisualDataModule->RemoteOpenMenuItem, dcaOpen);
  2281. DirViewContextPopupDefaultItem(osRemote, NonVisualDataModule->RemoteEditMenuItem, dcaEdit);
  2282. DirViewContextPopupDefaultItem(osRemote, NonVisualDataModule->RemoteCopyMenuItem, dcaCopy);
  2283. DirViewContextPopup(osRemote, fcRemotePopup, MousePos);
  2284. Handled = true;
  2285. }
  2286. //---------------------------------------------------------------------------
  2287. void __fastcall TCustomScpExplorerForm::ReloadLocalDirectory(const UnicodeString Directory)
  2288. {
  2289. }
  2290. //---------------------------------------------------------------------------
  2291. void __fastcall TCustomScpExplorerForm::BatchStart(void *& /*Storage*/)
  2292. {
  2293. DebugAssert(FErrorList == NULL);
  2294. if (WinConfiguration->ContinueOnError)
  2295. {
  2296. FErrorList = new TStringList();
  2297. Configuration->Usage->Inc(L"ContinuationsOnError");
  2298. }
  2299. NonVisualDataModule->StartBusy();
  2300. }
  2301. //---------------------------------------------------------------------------
  2302. void __fastcall TCustomScpExplorerForm::BatchEnd(void * /*Storage*/)
  2303. {
  2304. NonVisualDataModule->EndBusy();
  2305. if (FErrorList)
  2306. {
  2307. HandleErrorList(FErrorList);
  2308. }
  2309. }
  2310. //---------------------------------------------------------------------------
  2311. void __fastcall TCustomScpExplorerForm::UpdateCopyParamCounters(
  2312. const TCopyParamType & CopyParam)
  2313. {
  2314. if (!CopyParam.IncludeFileMask.Masks.IsEmpty())
  2315. {
  2316. Configuration->Usage->Inc(L"FileMaskUses");
  2317. }
  2318. if (IsEffectiveFileNameMask(CopyParam.FileMask))
  2319. {
  2320. Configuration->Usage->Inc(L"OperationMaskUses");
  2321. }
  2322. }
  2323. //---------------------------------------------------------------------------
  2324. bool __fastcall TCustomScpExplorerForm::ExecuteCopyMoveFileOperation(
  2325. TFileOperation Operation, TOperationSide Side, TStrings * FileList, bool NoConfirmation, void * AParam)
  2326. {
  2327. TTransferDirection Direction = (Side == osLocal ? tdToRemote : tdToLocal);
  2328. TTransferType Type = (Operation == foCopy ? ttCopy : ttMove);
  2329. TTransferOperationParam DefaultParam;
  2330. TTransferOperationParam & Param = (AParam != NULL) ? *static_cast<TTransferOperationParam *>(AParam) : DefaultParam;
  2331. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  2332. switch (Param.Queue)
  2333. {
  2334. case asOn:
  2335. CopyParam.Queue = true;
  2336. break;
  2337. case asOff:
  2338. CopyParam.Queue = false;
  2339. break;
  2340. case asAuto:
  2341. default:
  2342. // keep default
  2343. break;
  2344. }
  2345. bool Result =
  2346. CopyParamDialog(Direction, Type, Param.Temp, FileList, Param.TargetDirectory, CopyParam, !NoConfirmation, Param.DragDrop, Param.Options);
  2347. if (Result)
  2348. {
  2349. DebugAssert(Terminal);
  2350. bool SelectionRestored = false;
  2351. TCustomDirView * DView = NULL;
  2352. if (HasDirView[Side])
  2353. {
  2354. DView = DirView(Side);
  2355. DView->SaveSelection();
  2356. DView->SaveSelectedNames();
  2357. }
  2358. UpdateCopyParamCounters(CopyParam);
  2359. std::unique_ptr<TStringList> TransferResumeList(new TStringList());
  2360. DebugAssert(FTransferResumeList == NULL);
  2361. FTransferResumeList = Terminal->IsCapable[fcMoveToQueue] ? TransferResumeList.get() : NULL;
  2362. FMoveToQueue = false;
  2363. int Params = FLAGMASK(Operation == foMove, cpDelete);
  2364. try
  2365. {
  2366. TStrings * PermanentFileList;
  2367. std::unique_ptr<TStrings> PermanentFileListOwner;
  2368. try
  2369. {
  2370. if (Side == osLocal)
  2371. {
  2372. PermanentFileList = FileList;
  2373. Params |= FLAGMASK(Param.Temp, cpTemporary);
  2374. Terminal->CopyToRemote(FileList, Param.TargetDirectory, &CopyParam, Params, NULL);
  2375. if (Operation == foMove)
  2376. {
  2377. ReloadLocalDirectory();
  2378. if (DView != NULL)
  2379. {
  2380. DView->RestoreSelection();
  2381. }
  2382. SelectionRestored = true;
  2383. }
  2384. }
  2385. else
  2386. {
  2387. // Clone the file list as it may refer to current directory files,
  2388. // which get destroyed, when the source directory is reloaded after foMove operation.
  2389. // We should actually clone the file list for whole ExecuteFileOperation to protect against reloads.
  2390. // But for a hotfix, we are not going to do such a big change.
  2391. PermanentFileListOwner.reset(TRemoteFileList::CloneStrings(FileList));
  2392. PermanentFileList = PermanentFileListOwner.get();
  2393. try
  2394. {
  2395. Terminal->CopyToLocal(FileList, Param.TargetDirectory, &CopyParam, Params, NULL);
  2396. }
  2397. __finally
  2398. {
  2399. if (Operation == foMove)
  2400. {
  2401. if (DView != NULL)
  2402. {
  2403. DView->RestoreSelection();
  2404. }
  2405. SelectionRestored = true;
  2406. }
  2407. ReloadLocalDirectory(Param.TargetDirectory);
  2408. }
  2409. }
  2410. }
  2411. catch (EAbort &)
  2412. {
  2413. if (FMoveToQueue)
  2414. {
  2415. Params |= (CopyParam.QueueNoConfirmation ? cpNoConfirmation : 0);
  2416. DebugAssert(CopyParam.TransferSkipList == NULL);
  2417. DebugAssert(CopyParam.TransferResumeFile.IsEmpty());
  2418. if (TransferResumeList->Count > 0)
  2419. {
  2420. CopyParam.TransferResumeFile = TransferResumeList->Strings[TransferResumeList->Count - 1];
  2421. TransferResumeList->Delete(TransferResumeList->Count - 1);
  2422. }
  2423. CopyParam.TransferSkipList = TransferResumeList.release();
  2424. // not really needed, just to keep it consistent with TransferResumeList
  2425. FTransferResumeList = NULL;
  2426. FMoveToQueue = false;
  2427. Configuration->Usage->Inc("MovesToBackground");
  2428. AddQueueItem(Queue, Direction, PermanentFileList, Param.TargetDirectory, CopyParam, Params);
  2429. ClearTransferSourceSelection(Direction);
  2430. }
  2431. throw;
  2432. }
  2433. }
  2434. __finally
  2435. {
  2436. if (!SelectionRestored && (DView != NULL))
  2437. {
  2438. DView->DiscardSavedSelection();
  2439. }
  2440. FTransferResumeList = NULL;
  2441. }
  2442. }
  2443. return Result;
  2444. }
  2445. //---------------------------------------------------------------------------
  2446. bool __fastcall TCustomScpExplorerForm::ExecuteDeleteFileOperation(
  2447. TOperationSide Side, TStrings * FileList, void * Param)
  2448. {
  2449. DebugAssert(FileList->Count);
  2450. // We deliberately do not toggle alternative flag (Param), but use OR,
  2451. // because the Param is set only when command is invoked using Shift-Del/F8 keyboard
  2452. // shortcut of CurrentDeleteAlternativeAction
  2453. bool Alternative =
  2454. bool(Param) || UseAlternativeFunction();
  2455. bool Recycle;
  2456. if (Side == osLocal)
  2457. {
  2458. Recycle = (WinConfiguration->DeleteToRecycleBin != Alternative);
  2459. }
  2460. else
  2461. {
  2462. Recycle =
  2463. (Terminal->SessionData->DeleteToRecycleBin != Alternative) &&
  2464. !Terminal->SessionData->RecycleBinPath.IsEmpty() &&
  2465. !Terminal->IsRecycledFile(FileList->Strings[0]);
  2466. }
  2467. bool Result = !(Recycle ? WinConfiguration->ConfirmRecycling : WinConfiguration->ConfirmDeleting);
  2468. if (!Result)
  2469. {
  2470. UnicodeString Query;
  2471. if (FileList->Count == 1)
  2472. {
  2473. if (Side == osLocal)
  2474. {
  2475. Query = ExtractFileName(FileList->Strings[0]);
  2476. }
  2477. else
  2478. {
  2479. Query = UnixExtractFileName(FileList->Strings[0]);
  2480. }
  2481. Query = FMTLOAD((Recycle ? CONFIRM_RECYCLE_FILE : CONFIRM_DELETE_FILE), (Query));
  2482. }
  2483. else
  2484. {
  2485. Query = FMTLOAD((Recycle ? CONFIRM_RECYCLE_FILES : CONFIRM_DELETE_FILES), (FileList->Count));
  2486. }
  2487. TMessageParams Params(mpNeverAskAgainCheck);
  2488. Params.ImageName = L"Delete file";
  2489. unsigned int Answer =
  2490. MessageDialog(MainInstructions(Query), qtConfirmation, qaOK | qaCancel, HELP_DELETE_FILE, &Params);
  2491. if (Answer == qaNeverAskAgain)
  2492. {
  2493. Result = true;
  2494. if (Recycle)
  2495. {
  2496. WinConfiguration->ConfirmRecycling = false;
  2497. }
  2498. else
  2499. {
  2500. WinConfiguration->ConfirmDeleting = false;
  2501. }
  2502. }
  2503. else
  2504. {
  2505. Result = (Answer == qaOK);
  2506. }
  2507. }
  2508. if (Result)
  2509. {
  2510. DeleteFiles(Side, FileList, FLAGMASK(Alternative, dfAlternative));
  2511. }
  2512. return Result;
  2513. }
  2514. //---------------------------------------------------------------------------
  2515. bool __fastcall TCustomScpExplorerForm::ExecuteFileOperation(TFileOperation Operation,
  2516. TOperationSide Side, TStrings * FileList, bool NoConfirmation, void * Param)
  2517. {
  2518. TAutoBatch AutoBatch(this);
  2519. bool Result;
  2520. if ((Operation == foCopy) || (Operation == foMove))
  2521. {
  2522. Result = ExecuteCopyMoveFileOperation(Operation, Side, FileList, NoConfirmation, Param);
  2523. }
  2524. else if (Operation == foRename)
  2525. {
  2526. DebugAssert(DirView(Side)->ItemFocused);
  2527. DirView(Side)->ItemFocused->EditCaption();
  2528. Result = true;
  2529. }
  2530. else if (Operation == foDelete)
  2531. {
  2532. Result = ExecuteDeleteFileOperation(Side, FileList, Param);
  2533. }
  2534. else if (Operation == foSetProperties)
  2535. {
  2536. RemoteDirView->SaveSelectedNames();
  2537. Result = SetProperties(Side, FileList);
  2538. }
  2539. else if (Operation == foCustomCommand)
  2540. {
  2541. DebugAssert(Param);
  2542. DebugAssert(Side == osRemote);
  2543. RemoteDirView->SaveSelectedNames();
  2544. const TCustomCommandType * Command = static_cast<const TCustomCommandType*>(Param);
  2545. CustomCommand(FileList, *Command, NULL);
  2546. Result = true;
  2547. }
  2548. else if ((Operation == foRemoteMove) || (Operation == foRemoteCopy))
  2549. {
  2550. DebugAssert(Side == osRemote);
  2551. Result = RemoteTransferFiles(FileList, NoConfirmation,
  2552. (Operation == foRemoteMove), reinterpret_cast<TTerminal *>(Param));
  2553. }
  2554. else if (Operation == foLock)
  2555. {
  2556. DebugAssert(Side == osRemote);
  2557. LockFiles(FileList, true);
  2558. Result = true;
  2559. }
  2560. else if (Operation == foUnlock)
  2561. {
  2562. DebugAssert(Side == osRemote);
  2563. LockFiles(FileList, false);
  2564. Result = true;
  2565. }
  2566. else
  2567. {
  2568. DebugFail();
  2569. }
  2570. return Result;
  2571. }
  2572. //---------------------------------------------------------------------------
  2573. TOperationSide __fastcall TCustomScpExplorerForm::GetSide(TOperationSide Side)
  2574. {
  2575. if (Side == osCurrent)
  2576. {
  2577. Side = FCurrentSide;
  2578. }
  2579. return Side;
  2580. }
  2581. //---------------------------------------------------------------------------
  2582. bool __fastcall TCustomScpExplorerForm::ExecuteFileOperation(TFileOperation Operation,
  2583. TOperationSide Side, bool OnFocused, bool NoConfirmation, void * Param)
  2584. {
  2585. Side = GetSide(Side);
  2586. bool Result;
  2587. TStrings * FileList = DirView(Side)->CreateFileList(OnFocused, (Side == osLocal), NULL);
  2588. try
  2589. {
  2590. Result = ExecuteFileOperation(Operation, Side, FileList, NoConfirmation, Param);
  2591. }
  2592. __finally
  2593. {
  2594. delete FileList;
  2595. }
  2596. return Result;
  2597. }
  2598. //---------------------------------------------------------------------------
  2599. void __fastcall TCustomScpExplorerForm::ExecuteFileOperationCommand(
  2600. TFileOperation Operation, TOperationSide Side, bool OnFocused,
  2601. bool NoConfirmation, void * Param)
  2602. {
  2603. if (ExecuteFileOperation(Operation, Side, OnFocused, NoConfirmation, Param))
  2604. {
  2605. if ((Operation == foCopy) || (Operation == foMove))
  2606. {
  2607. if (GetSide(Side) == osLocal)
  2608. {
  2609. Configuration->Usage->Inc(L"UploadsCommand");
  2610. }
  2611. else
  2612. {
  2613. Configuration->Usage->Inc(L"DownloadsCommand");
  2614. }
  2615. }
  2616. }
  2617. }
  2618. //---------------------------------------------------------------------------
  2619. void __fastcall TCustomScpExplorerForm::ExecuteCopyOperationCommand(
  2620. TOperationSide Side, bool OnFocused, unsigned int Flags)
  2621. {
  2622. TTransferOperationParam Param;
  2623. if ((WinConfiguration->Interface != ifCommander) ||
  2624. WinConfiguration->ScpCommander.ExplorerKeyboardShortcuts)
  2625. {
  2626. Flags &= ~cocShortCutHint;
  2627. }
  2628. TCustomDirView * DView = DirView(Side);
  2629. Param.Options =
  2630. FLAGMASK(FLAGSET(Flags, cocShortCutHint), coShortCutHint) |
  2631. FLAGMASK(SelectedAllFilesInDirView(DView), coAllFiles);
  2632. if (FLAGSET(Flags, cocQueue))
  2633. {
  2634. Param.Queue = asOn;
  2635. }
  2636. else if (FLAGSET(Flags, cocNonQueue))
  2637. {
  2638. Param.Queue = asOff;
  2639. }
  2640. ExecuteFileOperationCommand(foCopy, Side, OnFocused, false, &Param);
  2641. }
  2642. //---------------------------------------------------------------------------
  2643. void __fastcall TCustomScpExplorerForm::HandleErrorList(TStringList *& ErrorList)
  2644. {
  2645. try
  2646. {
  2647. if (ErrorList->Count)
  2648. {
  2649. UnicodeString Message = MainInstructions(FMTLOAD(ERROR_LIST_COUNT, (ErrorList->Count)));
  2650. if (MessageDialog(Message, qtError,
  2651. qaOK | qaCancel, HELP_NONE) == qaOK)
  2652. {
  2653. unsigned int Answer;
  2654. int Index = 0;
  2655. do
  2656. {
  2657. DebugAssert(Index >= 0 && Index < ErrorList->Count);
  2658. TQueryButtonAlias Aliases[2];
  2659. Aliases[0].Button = qaYes;
  2660. Aliases[0].Alias = LoadStr(PREV_BUTTON);
  2661. Aliases[1].Button = qaNo;
  2662. Aliases[1].Alias = LoadStr(NEXT_BUTTON);
  2663. TMessageParams Params;
  2664. Params.Aliases = Aliases;
  2665. Params.AliasesCount = LENOF(Aliases);
  2666. Answer = MoreMessageDialog(
  2667. FMTLOAD(ERROR_LIST_NUMBER, (Index+1, ErrorList->Count, ErrorList->Strings[Index])),
  2668. dynamic_cast<TStrings *>(ErrorList->Objects[Index]), qtError,
  2669. (Index ? qaYes : 0) | (Index < ErrorList->Count - 1 ? qaNo : 0) |
  2670. qaOK, HELP_NONE, &Params);
  2671. if (Answer == qaNo)
  2672. {
  2673. Index++;
  2674. }
  2675. if (Answer == qaYes)
  2676. {
  2677. Index--;
  2678. }
  2679. }
  2680. while (Answer != qaOK);
  2681. }
  2682. }
  2683. }
  2684. __finally
  2685. {
  2686. TStrings * List = ErrorList;
  2687. ErrorList = NULL;
  2688. for (int i = 0; i < List->Count; i++)
  2689. {
  2690. delete List->Objects[i];
  2691. }
  2692. delete List;
  2693. }
  2694. }
  2695. //---------------------------------------------------------------------------
  2696. void __fastcall TCustomScpExplorerForm::ExecuteRemoteFile(
  2697. const UnicodeString & FullFileName, TRemoteFile * File, TExecuteFileBy ExecuteFileBy)
  2698. {
  2699. // needed for checking filemasks, as there's no directory object
  2700. // associated with the file object
  2701. File->FullFileName = FullFileName;
  2702. TFileMasks::TParams MaskParams;
  2703. MaskParams.Size = File->Size;
  2704. MaskParams.Modification = File->Modification;
  2705. ExecuteFile(osRemote, ExecuteFileBy, NULL, FullFileName, File, MaskParams);
  2706. }
  2707. //---------------------------------------------------------------------------
  2708. void __fastcall TCustomScpExplorerForm::EditNew(TOperationSide Side)
  2709. {
  2710. DebugAssert(!WinConfiguration->DisableOpenEdit);
  2711. Side = GetSide(Side);
  2712. TCustomDirView * DView = DirView(Side);
  2713. TListItem * FocusedItem = DView->ItemFocused;
  2714. UnicodeString Name;
  2715. if ((FocusedItem != NULL) && !DView->ItemIsParentDirectory(FocusedItem))
  2716. {
  2717. Name = DView->ItemFileName(FocusedItem);
  2718. }
  2719. else
  2720. {
  2721. Name = LoadStr(NEW_FILE);
  2722. }
  2723. UnicodeString Names = Name;
  2724. std::unique_ptr<TStrings> History(CloneStrings(CustomWinConfiguration->History[L"EditFile"]));
  2725. if (InputDialog(LoadStr(EDIT_FILE_CAPTION), LoadStr(EDIT_FILE_PROMPT), Names,
  2726. HELP_EDIT_NEW, History.get(), true))
  2727. {
  2728. while (!Names.IsEmpty())
  2729. {
  2730. Name = CutToChar(Names, FileMasksDelimiters[1], false);
  2731. CustomWinConfiguration->History[L"EditFile"] = History.get();
  2732. UnicodeString TargetFileName;
  2733. UnicodeString LocalFileName;
  2734. UnicodeString RootTempDir;
  2735. UnicodeString TempDir;
  2736. UnicodeString RemoteDirectory;
  2737. bool ExistingFile = false;
  2738. if (Side == osRemote)
  2739. {
  2740. Name = AbsolutePath(FTerminal->CurrentDirectory, Name);
  2741. TRemoteFile * File = NULL;
  2742. if (FTerminal->FileExists(Name, &File))
  2743. {
  2744. try
  2745. {
  2746. ExecuteRemoteFile(Name, File, efDefaultEditor);
  2747. ExistingFile = true;
  2748. }
  2749. __finally
  2750. {
  2751. delete File;
  2752. }
  2753. }
  2754. if (!ExistingFile)
  2755. {
  2756. RemoteDirectory = UnixExtractFilePath(Name);
  2757. TemporaryDirectoryForRemoteFiles(
  2758. RemoteDirectory, GUIConfiguration->CurrentCopyParam, TempDir, RootTempDir);
  2759. TargetFileName = UnixExtractFileName(Name);
  2760. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  2761. LocalFileName = TempDir +
  2762. // We probably do not want to trim the VMS version here
  2763. FTerminal->ChangeFileName(&CopyParam, TargetFileName, osRemote, false);
  2764. }
  2765. }
  2766. else
  2767. {
  2768. if (ExtractFilePath(Name).IsEmpty())
  2769. {
  2770. LocalFileName = IncludeTrailingBackslash(DView->PathName) + Name;
  2771. }
  2772. else
  2773. {
  2774. LocalFileName = ExpandFileName(Name);
  2775. }
  2776. TargetFileName = ExtractFileName(Name);
  2777. }
  2778. if (!ExistingFile)
  2779. {
  2780. if (!FileExists(ApiPath(LocalFileName)))
  2781. {
  2782. int File = FileCreate(ApiPath(LocalFileName));
  2783. if (File < 0)
  2784. {
  2785. if (!RootTempDir.IsEmpty())
  2786. {
  2787. RecursiveDeleteFile(ExcludeTrailingBackslash(RootTempDir), false);
  2788. }
  2789. throw Exception(FMTLOAD(CREATE_FILE_ERROR, (LocalFileName)));
  2790. }
  2791. else
  2792. {
  2793. FileClose(File);
  2794. }
  2795. }
  2796. TExecuteFileBy ExecuteFileBy = efDefaultEditor;
  2797. const TEditorData * ExternalEditor = NULL;
  2798. TFileMasks::TParams MaskParams; // size not known
  2799. ExecuteFileNormalize(ExecuteFileBy, ExternalEditor, TargetFileName,
  2800. false, MaskParams);
  2801. CustomExecuteFile(Side, ExecuteFileBy, LocalFileName, TargetFileName,
  2802. ExternalEditor, RootTempDir, RemoteDirectory);
  2803. }
  2804. }
  2805. }
  2806. }
  2807. //---------------------------------------------------------------------------
  2808. bool __fastcall TCustomScpExplorerForm::RemoteExecuteForceText(
  2809. TExecuteFileBy ExecuteFileBy, const TEditorData * ExternalEditor)
  2810. {
  2811. DebugAssert((ExecuteFileBy == efExternalEditor) ==
  2812. ((ExternalEditor != NULL) && (ExternalEditor->Editor == edExternal)));
  2813. DebugAssert(ExecuteFileBy != efDefaultEditor);
  2814. return
  2815. ((ExecuteFileBy == efInternalEditor)) ||
  2816. ((ExecuteFileBy == efExternalEditor) && ExternalEditor->ExternalEditorText);
  2817. }
  2818. //---------------------------------------------------------------------------
  2819. void __fastcall TCustomScpExplorerForm::CustomExecuteFile(TOperationSide Side,
  2820. TExecuteFileBy ExecuteFileBy, UnicodeString FileName, UnicodeString OriginalFileName,
  2821. const TEditorData * ExternalEditor, UnicodeString LocalRootDirectory,
  2822. UnicodeString RemoteDirectory)
  2823. {
  2824. DebugAssert(!WinConfiguration->DisableOpenEdit);
  2825. DebugAssert((ExecuteFileBy == efExternalEditor) ==
  2826. ((ExternalEditor != NULL) && (ExternalEditor->Editor == edExternal)));
  2827. DebugAssert(ExecuteFileBy != efDefaultEditor);
  2828. Side = GetSide(Side);
  2829. std::unique_ptr<TEditedFileData> Data(new TEditedFileData);
  2830. if (Side == osRemote)
  2831. {
  2832. Data->Terminal = Terminal;
  2833. Data->Queue = Queue;
  2834. Data->SessionData = CloneCurrentSessionData();
  2835. Data->ForceText = RemoteExecuteForceText(ExecuteFileBy, ExternalEditor);
  2836. Data->RemoteDirectory = RemoteDirectory;
  2837. Data->SessionName = Terminal->SessionData->SessionName;
  2838. Data->LocalRootDirectory = LocalRootDirectory;
  2839. Data->OriginalFileName = OriginalFileName;
  2840. Data->Command = L""; // will be changed later for external editor
  2841. }
  2842. if (ExecuteFileBy == efInternalEditor)
  2843. {
  2844. if (Side == osRemote)
  2845. {
  2846. UnicodeString Caption = UnixIncludeTrailingBackslash(RemoteDirectory) + OriginalFileName +
  2847. L" - " + Terminal->SessionData->SessionName;
  2848. TForm * Editor;
  2849. try
  2850. {
  2851. TNotifyEvent OnFileChanged = NULL;
  2852. TNotifyEvent OnSaveAll = NULL;
  2853. TAnyModifiedEvent OnAnyModified = NULL;
  2854. // Edited files are uploaded in background queue, what we do not support with encrypted files
  2855. if (Terminal->IsCapable[fsBackgroundTransfers])
  2856. {
  2857. OnFileChanged = FEditorManager->FileChanged;
  2858. OnSaveAll = SaveAllInternalEditors;
  2859. OnAnyModified = AnyInternalEditorModified;
  2860. }
  2861. Editor = ShowEditorForm(FileName, this, OnFileChanged,
  2862. FEditorManager->FileReload, FEditorManager->FileClosed,
  2863. OnSaveAll, OnAnyModified,
  2864. Caption, FStandaloneEditing, SessionColor, Terminal->SessionData->InternalEditorEncoding);
  2865. }
  2866. catch(...)
  2867. {
  2868. if (!LocalRootDirectory.IsEmpty())
  2869. {
  2870. RecursiveDeleteFile(ExcludeTrailingBackslash(LocalRootDirectory), false);
  2871. }
  2872. throw;
  2873. }
  2874. FEditorManager->AddFileInternal(FileName, Data.release(), Editor);
  2875. }
  2876. else
  2877. {
  2878. DebugAssert(!FStandaloneEditing);
  2879. TForm * Editor =
  2880. ShowEditorForm(FileName, this, NULL, NULL, LocalEditorClosed,
  2881. SaveAllInternalEditors, AnyInternalEditorModified,
  2882. L"", false, SessionColor, -1);
  2883. FLocalEditors->Add(Editor);
  2884. }
  2885. }
  2886. else
  2887. {
  2888. HANDLE Process;
  2889. if (ExecuteFileBy == efExternalEditor)
  2890. {
  2891. UnicodeString Program, Params, Dir;
  2892. Data->Command = ExternalEditor->ExternalEditor;
  2893. ReformatFileNameCommand(Data->Command);
  2894. SplitCommand(Data->Command, Program, Params, Dir);
  2895. Params = ExpandFileNameCommand(Params, FileName);
  2896. Program = ExpandEnvironmentVariables(Program);
  2897. if (!ExecuteShell(Program, Params, Process))
  2898. {
  2899. throw EOSExtException(FMTLOAD(EDITOR_ERROR, (Program)));
  2900. }
  2901. }
  2902. else
  2903. {
  2904. DebugAssert(Side == osRemote);
  2905. if (!ExecuteShell(FileName, L"", Process))
  2906. {
  2907. throw EOSExtException(FMTLOAD(EXECUTE_FILE_ERROR, (FileName)));
  2908. }
  2909. }
  2910. if ((Side == osLocal) ||
  2911. ((ExecuteFileBy == efShell) &&
  2912. !WinConfiguration->Editor.SDIShellEditor) ||
  2913. ((ExecuteFileBy == efExternalEditor) &&
  2914. !ExternalEditor->SDIExternalEditor))
  2915. {
  2916. // no need for handle
  2917. if (Process != NULL)
  2918. {
  2919. DebugCheck(CloseHandle(Process));
  2920. }
  2921. Process = INVALID_HANDLE_VALUE;
  2922. }
  2923. else
  2924. {
  2925. if (Process == NULL)
  2926. {
  2927. throw ExtException(LoadStr(OPEN_FILE_NO_PROCESS2), L"", HELP_OPEN_FILE_NO_PROCESS);
  2928. }
  2929. }
  2930. if (Side == osRemote)
  2931. {
  2932. FEditorManager->AddFileExternal(FileName, Data.release(), Process);
  2933. }
  2934. }
  2935. }
  2936. //---------------------------------------------------------------------------
  2937. void __fastcall TCustomScpExplorerForm::SaveInternalEditor(
  2938. const UnicodeString /*FileName*/, TEditedFileData * /*Data*/, TObject * Token,
  2939. void * /*Arg*/)
  2940. {
  2941. if (Token != NULL)
  2942. {
  2943. EditorFormFileSave(static_cast<TForm *>(Token));
  2944. }
  2945. }
  2946. //---------------------------------------------------------------------------
  2947. void __fastcall TCustomScpExplorerForm::SaveAllInternalEditors(TObject * /*Sender*/)
  2948. {
  2949. for (int Index = 0; Index < FLocalEditors->Count; Index++)
  2950. {
  2951. EditorFormFileSave(static_cast<TForm *>(FLocalEditors->Items[Index]));
  2952. }
  2953. FEditorManager->ProcessFiles(SaveInternalEditor, NULL);
  2954. }
  2955. //---------------------------------------------------------------------------
  2956. void __fastcall TCustomScpExplorerForm::InternalEditorModified(
  2957. const UnicodeString /*FileName*/, TEditedFileData * /*Data*/, TObject * Token,
  2958. void * Arg)
  2959. {
  2960. if ((Token != NULL) &&
  2961. IsEditorFormModified(static_cast<TForm *>(Token)))
  2962. {
  2963. *static_cast<bool *>(Arg) = true;
  2964. }
  2965. }
  2966. //---------------------------------------------------------------------------
  2967. void __fastcall TCustomScpExplorerForm::AnyInternalEditorModified(
  2968. TObject * /*Sender*/, bool & Modified)
  2969. {
  2970. for (int Index = 0; !Modified && (Index < FLocalEditors->Count); Index++)
  2971. {
  2972. if (IsEditorFormModified(static_cast<TForm *>(FLocalEditors->Items[Index])))
  2973. {
  2974. Modified = true;
  2975. }
  2976. }
  2977. if (!Modified)
  2978. {
  2979. FEditorManager->ProcessFiles(InternalEditorModified, &Modified);
  2980. }
  2981. }
  2982. //---------------------------------------------------------------------------
  2983. void __fastcall TCustomScpExplorerForm::LocalEditorClosed(TObject * Sender, bool /*Forced*/)
  2984. {
  2985. DebugCheck(FLocalEditors->Extract(Sender) >= 0);
  2986. }
  2987. //---------------------------------------------------------------------------
  2988. void __fastcall TCustomScpExplorerForm::TemporaryDirectoryForRemoteFiles(
  2989. UnicodeString RemoteDirectory, TCopyParamType CopyParam,
  2990. UnicodeString & Result, UnicodeString & RootDirectory)
  2991. {
  2992. if (!WinConfiguration->TemporaryDirectoryDeterministic)
  2993. {
  2994. RootDirectory = IncludeTrailingBackslash(WinConfiguration->TemporaryDir());
  2995. Result = RootDirectory;
  2996. }
  2997. else
  2998. {
  2999. RootDirectory = L"";
  3000. Result = WinConfiguration->ExpandedTemporaryDirectory();
  3001. Result = IncludeTrailingBackslash(Result);
  3002. }
  3003. if (WinConfiguration->TemporaryDirectoryAppendSession)
  3004. {
  3005. Result = IncludeTrailingBackslash(Result + MakeValidFileName(Terminal->SessionData->SessionName));
  3006. }
  3007. if (WinConfiguration->TemporaryDirectoryAppendPath)
  3008. {
  3009. if (!RemoteDirectory.IsEmpty() && (RemoteDirectory[1] == L'/'))
  3010. {
  3011. RemoteDirectory.Delete(1, 1);
  3012. }
  3013. Result = IncludeTrailingBackslash(Result + CopyParam.ValidLocalPath(FromUnixPath(RemoteDirectory)));
  3014. }
  3015. if (!ForceDirectories(ApiPath(Result)))
  3016. {
  3017. throw EOSExtException(FMTLOAD(CREATE_TEMP_DIR_ERROR, (Result)));
  3018. }
  3019. }
  3020. //---------------------------------------------------------------------------
  3021. void __fastcall TCustomScpExplorerForm::TemporarilyDownloadFiles(
  3022. TStrings * FileList, bool ForceText, UnicodeString & RootTempDir, UnicodeString & TempDir,
  3023. bool AllFiles, bool GetTargetNames, bool AutoOperation)
  3024. {
  3025. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  3026. if (ForceText)
  3027. {
  3028. CopyParam.TransferMode = tmAscii;
  3029. }
  3030. TemporaryFileCopyParam(CopyParam);
  3031. if (AllFiles)
  3032. {
  3033. CopyParam.IncludeFileMask = TFileMasks();
  3034. }
  3035. if (TempDir.IsEmpty())
  3036. {
  3037. TemporaryDirectoryForRemoteFiles(FTerminal->CurrentDirectory, CopyParam, TempDir, RootTempDir);
  3038. }
  3039. DebugAssert(!FAutoOperation);
  3040. FAutoOperation = AutoOperation;
  3041. Terminal->ExceptionOnFail = true;
  3042. try
  3043. {
  3044. try
  3045. {
  3046. // turn off confirmations, as for MDI editors we may possibly download
  3047. // the same file over
  3048. Terminal->CopyToLocal(
  3049. FileList, TempDir, &CopyParam, cpNoConfirmation | cpTemporary, NULL);
  3050. if (GetTargetNames)
  3051. {
  3052. for (int i = 0; i < FileList->Count; i++)
  3053. {
  3054. FileList->Strings[i] =
  3055. Terminal->ChangeFileName(&CopyParam, UnixExtractFileName(FileList->Strings[i]), osRemote, false);
  3056. }
  3057. }
  3058. }
  3059. catch(...)
  3060. {
  3061. if (!RootTempDir.IsEmpty())
  3062. {
  3063. RecursiveDeleteFile(ExcludeTrailingBackslash(RootTempDir), false);
  3064. }
  3065. throw;
  3066. }
  3067. }
  3068. __finally
  3069. {
  3070. FAutoOperation = false;
  3071. Terminal->ExceptionOnFail = false;
  3072. }
  3073. }
  3074. //---------------------------------------------------------------------------
  3075. void __fastcall TCustomScpExplorerForm::EditorAutoConfig()
  3076. {
  3077. // Do not waste time checking for default editor list the next time,
  3078. // or testing if default editor is not notepad.
  3079. bool TryNextTime = false;
  3080. UnicodeString UsageState;
  3081. if (!WinConfiguration->EditorList->IsDefaultList())
  3082. {
  3083. UsageState = "H";
  3084. }
  3085. else
  3086. {
  3087. UnicodeString Executable;
  3088. UnicodeString ExecutableDescription;
  3089. if (DetectSystemExternalEditor(false, Executable, ExecutableDescription, UsageState, TryNextTime))
  3090. {
  3091. UnicodeString Message =
  3092. FMTLOAD(EDITOR_AUTO_CONFIG2, (ExecutableDescription, ExecutableDescription));
  3093. unsigned int Answer =
  3094. MessageDialog(Message, qtConfirmation, qaOK | qaCancel, HELP_EDITOR_AUTO_CONFIG);
  3095. if (Answer != qaOK)
  3096. {
  3097. UsageState = "R";
  3098. }
  3099. else
  3100. {
  3101. UsageState = "A";
  3102. TEditorData EditorData;
  3103. EditorData.Editor = edExternal;
  3104. EditorData.ExternalEditor = FormatCommand(Executable, L"");
  3105. EditorData.DecideExternalEditorText();
  3106. TEditorList EditorList;
  3107. EditorList = *WinConfiguration->EditorList;
  3108. EditorList.Insert(0, new TEditorPreferences(EditorData));
  3109. WinConfiguration->EditorList = &EditorList;
  3110. }
  3111. }
  3112. }
  3113. WinConfiguration->OfferedEditorAutoConfig = !TryNextTime;
  3114. WinConfiguration->Usage->Set(L"EditorAutoConfig", UsageState);
  3115. }
  3116. //---------------------------------------------------------------------------
  3117. void __fastcall TCustomScpExplorerForm::ExecuteFileNormalize(
  3118. TExecuteFileBy & ExecuteFileBy, const TEditorData *& ExternalEditor,
  3119. const UnicodeString & FileName, bool Local, const TFileMasks::TParams & MaskParams)
  3120. {
  3121. if (ExecuteFileBy == efDefaultEditor)
  3122. {
  3123. if (!WinConfiguration->OfferedEditorAutoConfig)
  3124. {
  3125. EditorAutoConfig();
  3126. }
  3127. const TEditorPreferences * Editor =
  3128. WinConfiguration->DefaultEditorForFile(FileName, Local, MaskParams);
  3129. if ((Editor == NULL) || (Editor->Data->Editor == edInternal))
  3130. {
  3131. ExecuteFileBy = efInternalEditor;
  3132. ExternalEditor = NULL;
  3133. }
  3134. else if (Editor->Data->Editor == edOpen)
  3135. {
  3136. ExecuteFileBy = efShell;
  3137. ExternalEditor = NULL;
  3138. }
  3139. else
  3140. {
  3141. ExecuteFileBy = efExternalEditor;
  3142. ExternalEditor = Editor->Data;
  3143. }
  3144. }
  3145. }
  3146. //---------------------------------------------------------------------------
  3147. void __fastcall TCustomScpExplorerForm::ExecuteFile(TOperationSide Side,
  3148. TExecuteFileBy ExecuteFileBy, const TEditorData * ExternalEditor,
  3149. UnicodeString FullFileName, TObject * Object, const TFileMasks::TParams & MaskParams)
  3150. {
  3151. UnicodeString OriginalFileName;
  3152. UnicodeString LocalRootDirectory;
  3153. UnicodeString RemoteDirectory;
  3154. ExecuteFileNormalize(ExecuteFileBy, ExternalEditor, FullFileName,
  3155. (Side == osLocal), MaskParams);
  3156. UnicodeString Counter;
  3157. UnicodeString LocalFileName;
  3158. bool Handled = false;
  3159. if (Side == osRemote)
  3160. {
  3161. // We need to trim VMS version here, so that we use name without version
  3162. // when uploading back to create a new version of the file
  3163. OriginalFileName = FTerminal->GetBaseFileName(UnixExtractFileName(FullFileName));
  3164. RemoteDirectory = UnixExtractFilePath(FullFileName);
  3165. TObject * Token = NULL;
  3166. UnicodeString LocalDirectory;
  3167. if (!FEditorManager->CanAddFile(RemoteDirectory, OriginalFileName,
  3168. Terminal->SessionData->SessionName, Token, LocalRootDirectory,
  3169. LocalDirectory))
  3170. {
  3171. if (Token != NULL)
  3172. {
  3173. TForm * Form = dynamic_cast<TForm *>(Token);
  3174. if (Form->WindowState == wsMinimized)
  3175. {
  3176. ShowWindow(Form->Handle, SW_RESTORE);
  3177. }
  3178. else
  3179. {
  3180. Form->SetFocus();
  3181. }
  3182. Handled = true;
  3183. }
  3184. else
  3185. {
  3186. throw Exception(FMTLOAD(ALREADY_EDITED_EXTERNALLY_OR_UPLOADED, (OriginalFileName)));
  3187. }
  3188. }
  3189. if (!Handled)
  3190. {
  3191. TStringList * FileList1 = new TStringList();
  3192. try
  3193. {
  3194. FileList1->AddObject(FullFileName, Object);
  3195. TemporarilyDownloadFiles(FileList1,
  3196. RemoteExecuteForceText(ExecuteFileBy, ExternalEditor),
  3197. LocalRootDirectory, LocalDirectory, true, true, true);
  3198. LocalFileName = LocalDirectory + FileList1->Strings[0];
  3199. }
  3200. __finally
  3201. {
  3202. delete FileList1;
  3203. }
  3204. switch (ExecuteFileBy)
  3205. {
  3206. case efShell:
  3207. Counter = "RemoteFilesExecuted";
  3208. break;
  3209. case efInternalEditor:
  3210. Counter = "RemoteFilesOpenedInInternalEditor";
  3211. break;
  3212. case efExternalEditor:
  3213. Counter = "RemoteFilesOpenedInExternalEditor";
  3214. break;
  3215. default:
  3216. DebugFail();
  3217. }
  3218. }
  3219. }
  3220. else
  3221. {
  3222. LocalFileName = FullFileName;
  3223. OriginalFileName = ExtractFileName(FullFileName);
  3224. switch (ExecuteFileBy)
  3225. {
  3226. case efShell:
  3227. Counter = "LocalFilesExecuted";
  3228. break;
  3229. case efInternalEditor:
  3230. Counter = "LocalFilesOpenedInInternalEditor";
  3231. break;
  3232. case efExternalEditor:
  3233. Counter = "LocalFilesOpenedInExternalEditor";
  3234. break;
  3235. default:
  3236. DebugFail();
  3237. }
  3238. }
  3239. if (!Handled)
  3240. {
  3241. Configuration->Usage->Inc(Counter);
  3242. CustomExecuteFile(Side, ExecuteFileBy, LocalFileName, OriginalFileName,
  3243. ExternalEditor, LocalRootDirectory, RemoteDirectory);
  3244. }
  3245. }
  3246. //---------------------------------------------------------------------------
  3247. void __fastcall TCustomScpExplorerForm::ExecuteFile(TOperationSide Side,
  3248. TExecuteFileBy ExecuteFileBy, const TEditorData * ExternalEditor,
  3249. bool AllSelected, bool OnFocused)
  3250. {
  3251. DebugAssert(!WinConfiguration->DisableOpenEdit);
  3252. DebugAssert((ExecuteFileBy == efExternalEditor) ==
  3253. ((ExternalEditor != NULL) && (ExternalEditor->Editor == edExternal)));
  3254. Side = GetSide(Side);
  3255. TCustomDirView * DView = DirView(Side);
  3256. TStrings * FileList = AllSelected ?
  3257. DView->CreateFileList(OnFocused, Side == osLocal) :
  3258. DView->CreateFocusedFileList(Side == osLocal);
  3259. try
  3260. {
  3261. DebugAssert(AllSelected || (FileList->Count == 1));
  3262. for (int i = 0; i < FileList->Count; i++)
  3263. {
  3264. UnicodeString ListFileName = FileList->Strings[i];
  3265. UnicodeString FileNameOnly = (Side == osRemote) ?
  3266. UnixExtractFileName(ListFileName) : ExtractFileName(ListFileName);
  3267. TListItem * Item = DView->FindFileItem(FileNameOnly);
  3268. if (!DView->ItemIsDirectory(Item))
  3269. {
  3270. UnicodeString FullFileName;
  3271. if (Side == osRemote)
  3272. {
  3273. FullFileName = RemoteDirView->Path + ListFileName;
  3274. }
  3275. else
  3276. {
  3277. FullFileName = ListFileName;
  3278. }
  3279. TFileMasks::TParams MaskParams;
  3280. MaskParams.Size = DView->ItemFileSize(Item);
  3281. TDateTimePrecision Precision;
  3282. MaskParams.Modification = DView->ItemFileTime(Item, Precision);
  3283. ExecuteFile(Side, ExecuteFileBy, ExternalEditor, FullFileName,
  3284. FileList->Objects[i], MaskParams);
  3285. }
  3286. }
  3287. }
  3288. __finally
  3289. {
  3290. delete FileList;
  3291. }
  3292. }
  3293. //---------------------------------------------------------------------------
  3294. void __fastcall TCustomScpExplorerForm::TemporaryFileCopyParam(TCopyParamType & CopyParam)
  3295. {
  3296. // do not forget to add additional options to TemporarilyDownloadFiles, and AS
  3297. CopyParam.FileNameCase = ncNoChange;
  3298. CopyParam.PreserveRights = false;
  3299. CopyParam.PreserveReadOnly = false;
  3300. CopyParam.ReplaceInvalidChars = true;
  3301. CopyParam.IncludeFileMask = TFileMasks();
  3302. CopyParam.NewerOnly = false;
  3303. CopyParam.FileMask = L"";
  3304. }
  3305. //---------------------------------------------------------------------------
  3306. void __fastcall TCustomScpExplorerForm::ExecutedFileChanged(const UnicodeString FileName,
  3307. TEditedFileData * Data, HANDLE UploadCompleteEvent)
  3308. {
  3309. TTerminalManager * Manager = TTerminalManager::Instance();
  3310. if ((Data->Terminal == NULL) || !Data->Terminal->Active)
  3311. {
  3312. if (!NonVisualDataModule->Busy)
  3313. {
  3314. UnicodeString FileNameOnly = ExtractFileName(FileName);
  3315. UnicodeString EditInactiveSessionReopenAcceptedCounter = L"EditInactiveSessionReopenAccepted";
  3316. UnicodeString EditInactiveSessionReopenRejectedCounter = L"EditInactiveSessionReopenRejected";
  3317. if (Data->Terminal == NULL)
  3318. {
  3319. TTerminal * SameSiteTerminal = Manager->FindActiveTerminalForSite(Data->SessionData);
  3320. if (SameSiteTerminal != NULL)
  3321. {
  3322. UnicodeString Message =
  3323. FMTLOAD(EDIT_SESSION_REATTACH,
  3324. (FileNameOnly, Data->SessionName, FileNameOnly));
  3325. if (MessageDialog(Message, qtConfirmation, qaOK | qaCancel) == qaOK)
  3326. {
  3327. Data->Terminal = Terminal;
  3328. Data->Queue = Manager->FindQueueForTerminal(Terminal);
  3329. Data->SessionName = Terminal->SessionData->SessionName;
  3330. // We might also overwrite session data
  3331. Configuration->Usage->Inc(EditInactiveSessionReopenAcceptedCounter);
  3332. }
  3333. else
  3334. {
  3335. Configuration->Usage->Inc(EditInactiveSessionReopenRejectedCounter);
  3336. Abort();
  3337. }
  3338. }
  3339. }
  3340. // foreground session should reconnect itself
  3341. else if (Terminal != Data->Terminal)
  3342. {
  3343. UnicodeString Message =
  3344. MainInstructions(
  3345. FMTLOAD(EDIT_SESSION_RECONNECT, (Data->SessionName, FileNameOnly)));
  3346. if (MessageDialog(Message, qtConfirmation, qaOK | qaCancel) == qaOK)
  3347. {
  3348. Manager->SetActiveTerminalWithAutoReconnect(Data->Terminal);
  3349. Configuration->Usage->Inc(EditInactiveSessionReopenAcceptedCounter);
  3350. }
  3351. else
  3352. {
  3353. Configuration->Usage->Inc(EditInactiveSessionReopenRejectedCounter);
  3354. Abort();
  3355. }
  3356. }
  3357. }
  3358. if ((Data->Terminal == NULL) || !Data->Terminal->Active)
  3359. {
  3360. Configuration->Usage->Inc(L"EditInactiveSession");
  3361. // Prevent this when not idle (!NonVisualDataModule->Busy)?
  3362. throw Exception(FMTLOAD(EDIT_SESSION_CLOSED2,
  3363. (ExtractFileName(FileName), Data->SessionName)));
  3364. }
  3365. }
  3366. TStrings * FileList = new TStringList();
  3367. try
  3368. {
  3369. FileList->Add(FileName);
  3370. // consider using the same settings (preset) as when the file was downloaded
  3371. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  3372. TemporaryFileCopyParam(CopyParam);
  3373. if (Data->ForceText)
  3374. {
  3375. CopyParam.TransferMode = tmAscii;
  3376. }
  3377. // so i do not need to worry if masking algorithm works in all cases
  3378. // ("" means "copy file name", no masking is actually done)
  3379. if (ExtractFileName(FileName) == Data->OriginalFileName)
  3380. {
  3381. CopyParam.FileMask = L"";
  3382. }
  3383. else
  3384. {
  3385. CopyParam.FileMask = DelimitFileNameMask(Data->OriginalFileName);
  3386. }
  3387. DebugAssert(Data->Queue != NULL);
  3388. int Params = cpNoConfirmation | cpTemporary;
  3389. TQueueItem * QueueItem = new TUploadQueueItem(Data->Terminal, FileList,
  3390. Data->RemoteDirectory, &CopyParam, Params, true, false);
  3391. QueueItem->CompleteEvent = UploadCompleteEvent;
  3392. AddQueueItem(Data->Queue, QueueItem, Data->Terminal);
  3393. }
  3394. __finally
  3395. {
  3396. delete FileList;
  3397. }
  3398. }
  3399. //---------------------------------------------------------------------------
  3400. void __fastcall TCustomScpExplorerForm::ExecutedFileReload(
  3401. const UnicodeString FileName, const TEditedFileData * Data)
  3402. {
  3403. // Sanity check, we should not be busy otherwise user would not be able to click Reload button.
  3404. DebugAssert(!NonVisualDataModule->Busy);
  3405. if ((Data->Terminal == NULL) || !Data->Terminal->Active)
  3406. {
  3407. throw Exception(FMTLOAD(EDIT_SESSION_CLOSED_RELOAD,
  3408. (ExtractFileName(FileName), Data->SessionName)));
  3409. }
  3410. TTerminal * PrevTerminal = TTerminalManager::Instance()->ActiveTerminal;
  3411. TTerminalManager::Instance()->ActiveTerminal = Data->Terminal;
  3412. NonVisualDataModule->StartBusy();
  3413. try
  3414. {
  3415. std::unique_ptr<TRemoteFile> File;
  3416. UnicodeString RemoteFileName =
  3417. UnixIncludeTrailingBackslash(Data->RemoteDirectory) + Data->OriginalFileName;
  3418. FTerminal->ExceptionOnFail = true;
  3419. try
  3420. {
  3421. TRemoteFile * AFile = NULL;
  3422. FTerminal->ReadFile(RemoteFileName, AFile);
  3423. File.reset(AFile);
  3424. if (!File->HaveFullFileName)
  3425. {
  3426. File->FullFileName = RemoteFileName;
  3427. }
  3428. }
  3429. __finally
  3430. {
  3431. FTerminal->ExceptionOnFail = false;
  3432. }
  3433. std::unique_ptr<TStrings> FileList(new TStringList());
  3434. FileList->AddObject(RemoteFileName, File.get());
  3435. UnicodeString RootTempDir = Data->LocalRootDirectory;
  3436. UnicodeString TempDir = ExtractFilePath(FileName);
  3437. TemporarilyDownloadFiles(FileList.get(), Data->ForceText, RootTempDir,
  3438. TempDir, true, true, true);
  3439. // sanity check, the target file name should be still the same
  3440. DebugAssert(ExtractFileName(FileName) == FileList->Strings[0]);
  3441. }
  3442. __finally
  3443. {
  3444. NonVisualDataModule->EndBusy();
  3445. // it actually may not exist anymore...
  3446. TTerminalManager::Instance()->ActiveTerminal = PrevTerminal;
  3447. }
  3448. }
  3449. //---------------------------------------------------------------------------
  3450. void __fastcall TCustomScpExplorerForm::ExecutedFileEarlyClosed(
  3451. const TEditedFileData * Data, bool & KeepOpen)
  3452. {
  3453. // Command is set for external editors only (not for "shell" open).
  3454. if (!Data->Command.IsEmpty())
  3455. {
  3456. bool AnyFound = false;
  3457. bool AnyMDI = false;
  3458. bool AnyNonMDI = false;
  3459. bool AnyDetect = false;
  3460. TEditorList * EditorList = new TEditorList();
  3461. try
  3462. {
  3463. *EditorList = *WinConfiguration->EditorList;
  3464. for (int i = 0; i < EditorList->Count; i++)
  3465. {
  3466. const TEditorPreferences * Editor = EditorList->Editors[i];
  3467. if ((Editor->Data->Editor == edExternal) &&
  3468. (Editor->Data->ExternalEditor == Data->Command))
  3469. {
  3470. AnyFound = true;
  3471. if (Editor->Data->SDIExternalEditor)
  3472. {
  3473. AnyNonMDI = true;
  3474. if (Editor->Data->DetectMDIExternalEditor)
  3475. {
  3476. AnyDetect = true;
  3477. }
  3478. }
  3479. else
  3480. {
  3481. AnyMDI = true;
  3482. }
  3483. }
  3484. }
  3485. bool EnableMDI = false;
  3486. bool DisableDetect = false;
  3487. if (AnyMDI)
  3488. {
  3489. KeepOpen = true;
  3490. if (AnyNonMDI)
  3491. {
  3492. // there is at least one instance of the editor with MDI support enabled,
  3493. // and one with disabled, enable it for all instances
  3494. EnableMDI = true;
  3495. }
  3496. }
  3497. else if (AnyFound && !AnyDetect)
  3498. {
  3499. // at least once instance found but all have MDI autodetection disabled
  3500. // => close the file (default action)
  3501. }
  3502. else
  3503. {
  3504. // no instance of the editor has MDI support enabled
  3505. TMessageParams Params;
  3506. if (AnyFound)
  3507. {
  3508. // there is at least one instance of the editor
  3509. Params.Params |= mpNeverAskAgainCheck;
  3510. }
  3511. unsigned int Answer = MessageDialog(FMTLOAD(EDITOR_EARLY_CLOSED2, (Data->OriginalFileName)), qtWarning,
  3512. qaYes | qaNo, HELP_EDITOR_EARLY_CLOSED, &Params);
  3513. switch (Answer)
  3514. {
  3515. case qaNeverAskAgain:
  3516. DisableDetect = true;
  3517. break;
  3518. case qaNo:
  3519. EnableMDI = true;
  3520. KeepOpen = true;
  3521. break;
  3522. }
  3523. }
  3524. if (AnyFound && (EnableMDI || DisableDetect))
  3525. {
  3526. bool Changed = false;
  3527. for (int i = 0; i < EditorList->Count; i++)
  3528. {
  3529. const TEditorPreferences * Editor = EditorList->Editors[i];
  3530. if ((Editor->Data->Editor == edExternal) &&
  3531. (Editor->Data->ExternalEditor == Data->Command) &&
  3532. ((EnableMDI && Editor->Data->SDIExternalEditor) ||
  3533. (DisableDetect && Editor->Data->DetectMDIExternalEditor)))
  3534. {
  3535. Changed = true;
  3536. TEditorPreferences * UpdatedEditor = new TEditorPreferences(*Editor);
  3537. if (EnableMDI)
  3538. {
  3539. UpdatedEditor->GetData()->SDIExternalEditor = false;
  3540. }
  3541. if (DisableDetect)
  3542. {
  3543. UpdatedEditor->GetData()->DetectMDIExternalEditor = false;
  3544. }
  3545. EditorList->Change(i, UpdatedEditor);
  3546. }
  3547. }
  3548. if (Changed)
  3549. {
  3550. WinConfiguration->EditorList = EditorList;
  3551. }
  3552. }
  3553. }
  3554. __finally
  3555. {
  3556. delete EditorList;
  3557. }
  3558. }
  3559. else
  3560. {
  3561. // "open" case
  3562. MessageDialog(FMTLOAD(APP_EARLY_CLOSED, (Data->OriginalFileName)), qtWarning,
  3563. qaOK, HELP_APP_EARLY_CLOSED);
  3564. }
  3565. }
  3566. //---------------------------------------------------------------------------
  3567. void __fastcall TCustomScpExplorerForm::ExecutedFileUploadComplete(TObject * Sender)
  3568. {
  3569. EditorFormFileUploadComplete(DebugNotNull(dynamic_cast<TForm *>(Sender)));
  3570. }
  3571. //---------------------------------------------------------------------------
  3572. void __fastcall TCustomScpExplorerForm::RemoteDirViewEnter(TObject * /*Sender*/)
  3573. {
  3574. SideEnter(osRemote);
  3575. }
  3576. //---------------------------------------------------------------------------
  3577. void __fastcall TCustomScpExplorerForm::RemoteDriveViewEnter(TObject * /*Sender*/)
  3578. {
  3579. MakeNextInTabOrder(RemoteDirView, RemoteDriveView);
  3580. SideEnter(osRemote);
  3581. }
  3582. //---------------------------------------------------------------------------
  3583. void __fastcall TCustomScpExplorerForm::SideEnter(TOperationSide Side)
  3584. {
  3585. FCurrentSide = Side;
  3586. if (Visible)
  3587. {
  3588. UpdateControls();
  3589. }
  3590. }
  3591. //---------------------------------------------------------------------------
  3592. void __fastcall TCustomScpExplorerForm::DeleteFiles(TOperationSide Side,
  3593. TStrings * FileList, bool Alternative)
  3594. {
  3595. TCustomDirView * DView = DirView(Side);
  3596. DView->SaveSelection();
  3597. DView->SaveSelectedNames();
  3598. DebugAssert(!FAlternativeDelete);
  3599. FAlternativeDelete = Alternative;
  3600. try
  3601. {
  3602. if (Side == osRemote)
  3603. {
  3604. DebugAssert(Terminal != NULL);
  3605. Terminal->DeleteFiles(FileList, FLAGMASK(Alternative, dfAlternative));
  3606. }
  3607. else
  3608. {
  3609. try
  3610. {
  3611. TTerminalManager::Instance()->LocalTerminal->DeleteLocalFiles(FileList, FLAGMASK(Alternative, dfAlternative));
  3612. }
  3613. __finally
  3614. {
  3615. ReloadLocalDirectory();
  3616. }
  3617. }
  3618. FAlternativeDelete = false;
  3619. }
  3620. catch(...)
  3621. {
  3622. FAlternativeDelete = false;
  3623. DView->DiscardSavedSelection();
  3624. throw;
  3625. }
  3626. DView->RestoreSelection();
  3627. }
  3628. //---------------------------------------------------------------------------
  3629. void __fastcall TCustomScpExplorerForm::LockFiles(TStrings * FileList, bool Lock)
  3630. {
  3631. DebugAssert(Terminal);
  3632. RemoteDirView->SaveSelection();
  3633. RemoteDirView->SaveSelectedNames();
  3634. try
  3635. {
  3636. if (Lock)
  3637. {
  3638. Terminal->LockFiles(FileList);
  3639. }
  3640. else
  3641. {
  3642. Terminal->UnlockFiles(FileList);
  3643. }
  3644. }
  3645. catch(...)
  3646. {
  3647. RemoteDirView->DiscardSavedSelection();
  3648. throw;
  3649. }
  3650. RemoteDirView->RestoreSelection();
  3651. }
  3652. //---------------------------------------------------------------------------
  3653. bool __fastcall TCustomScpExplorerForm::RemoteTransferDialog(TTerminal *& Session,
  3654. TStrings * FileList, UnicodeString & Target, UnicodeString & FileMask, bool & DirectCopy,
  3655. bool NoConfirmation, bool Move)
  3656. {
  3657. DebugAssert(Terminal != NULL);
  3658. // update Terminal->StateData->RemoteDirectory
  3659. UpdateTerminal(Terminal);
  3660. if (Session == NULL)
  3661. {
  3662. Session = TTerminalManager::Instance()->ActiveTerminal;
  3663. }
  3664. if (Session == TTerminalManager::Instance()->ActiveTerminal)
  3665. {
  3666. if (RemoteDriveView->DropTarget != NULL)
  3667. {
  3668. Target = RemoteDriveView->NodePathName(RemoteDriveView->DropTarget);
  3669. }
  3670. else if (RemoteDirView->DropTarget != NULL)
  3671. {
  3672. DebugAssert(RemoteDirView->ItemIsDirectory(RemoteDirView->DropTarget));
  3673. Target = RemoteDirView->ItemFullFileName(RemoteDirView->DropTarget);
  3674. }
  3675. else
  3676. {
  3677. Target = RemoteDirView->Path;
  3678. }
  3679. }
  3680. else
  3681. {
  3682. TManagedTerminal * ManagedTerminal = DebugNotNull(dynamic_cast<TManagedTerminal *>(Session));
  3683. Target = ManagedTerminal->StateData->RemoteDirectory;
  3684. }
  3685. Target = UnixIncludeTrailingBackslash(Target);
  3686. if (FileList->Count == 1)
  3687. {
  3688. FileMask = DelimitFileNameMask(UnixExtractFileName(FileList->Strings[0]));
  3689. }
  3690. else
  3691. {
  3692. FileMask = AnyMask;
  3693. }
  3694. DirectCopy = FTerminal->IsCapable[fcRemoteCopy] || FTerminal->IsCapable[fcSecondaryShell];
  3695. bool Result = true;
  3696. if (!NoConfirmation)
  3697. {
  3698. bool Multi = (FileList->Count > 1);
  3699. if (Move)
  3700. {
  3701. Result = DoRemoteMoveDialog(Multi, Target, FileMask);
  3702. }
  3703. else
  3704. {
  3705. TStrings * Sessions = TTerminalManager::Instance()->TerminalList;
  3706. TStrings * Directories = new TStringList;
  3707. try
  3708. {
  3709. for (int Index = 0; Index < Sessions->Count; Index++)
  3710. {
  3711. TManagedTerminal * Terminal =
  3712. dynamic_cast<TManagedTerminal *>(Sessions->Objects[Index]);
  3713. Directories->Add(Terminal->StateData->RemoteDirectory);
  3714. }
  3715. TDirectRemoteCopy AllowDirectCopy;
  3716. if (FTerminal->IsCapable[fcRemoteCopy] || FTerminal->CommandSessionOpened)
  3717. {
  3718. DebugAssert(DirectCopy);
  3719. AllowDirectCopy = drcAllow;
  3720. }
  3721. else if (FTerminal->IsCapable[fcSecondaryShell])
  3722. {
  3723. DebugAssert(DirectCopy);
  3724. AllowDirectCopy = drcConfirmCommandSession;
  3725. }
  3726. else
  3727. {
  3728. DebugAssert(!DirectCopy);
  3729. AllowDirectCopy = drcDisallow;
  3730. }
  3731. void * ASession = Session;
  3732. Result = DoRemoteCopyDialog(Sessions, Directories, AllowDirectCopy,
  3733. Multi, ASession, Target, FileMask, DirectCopy, TTerminalManager::Instance()->ActiveTerminal);
  3734. Session = static_cast<TTerminal *>(ASession);
  3735. }
  3736. __finally
  3737. {
  3738. delete Directories;
  3739. }
  3740. }
  3741. }
  3742. return Result;
  3743. }
  3744. //---------------------------------------------------------------------------
  3745. bool __fastcall TCustomScpExplorerForm::RemoteTransferFiles(
  3746. TStrings * FileList, bool NoConfirmation, bool Move, TTerminal * Session)
  3747. {
  3748. bool DirectCopy;
  3749. UnicodeString Target, FileMask;
  3750. bool Result = RemoteTransferDialog(Session, FileList, Target, FileMask, DirectCopy, NoConfirmation, Move);
  3751. if (Result)
  3752. {
  3753. if (!Move && !DirectCopy)
  3754. {
  3755. Configuration->Usage->Inc("RemoteCopyTemp");
  3756. UnicodeString RootTempDir;
  3757. UnicodeString TempDir;
  3758. TemporarilyDownloadFiles(FileList, false, RootTempDir, TempDir, false, false, false);
  3759. TStrings * TemporaryFilesList = new TStringList();
  3760. try
  3761. {
  3762. TMakeLocalFileListParams MakeFileListParam;
  3763. MakeFileListParam.FileList = TemporaryFilesList;
  3764. MakeFileListParam.FileTimes = NULL;
  3765. MakeFileListParam.IncludeDirs = true;
  3766. MakeFileListParam.Recursive = false;
  3767. ProcessLocalDirectory(TempDir, FTerminal->MakeLocalFileList, &MakeFileListParam);
  3768. TTerminalManager::Instance()->ActiveTerminal = Session;
  3769. if (TemporaryFilesList->Count > 0)
  3770. {
  3771. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  3772. CopyParam.FileMask = FileMask;
  3773. DebugAssert(!FAutoOperation);
  3774. FAutoOperation = true;
  3775. FTerminal->CopyToRemote(TemporaryFilesList, Target, &CopyParam, cpTemporary, NULL);
  3776. }
  3777. }
  3778. __finally
  3779. {
  3780. delete TemporaryFilesList;
  3781. FAutoOperation = false;
  3782. if (!RootTempDir.IsEmpty())
  3783. {
  3784. RecursiveDeleteFile(ExcludeTrailingBackslash(RootTempDir), false);
  3785. }
  3786. }
  3787. }
  3788. else
  3789. {
  3790. RemoteDirView->SaveSelection();
  3791. RemoteDirView->SaveSelectedNames();
  3792. try
  3793. {
  3794. if (Move)
  3795. {
  3796. Configuration->Usage->Inc("RemoteMove");
  3797. Terminal->MoveFiles(FileList, Target, FileMask);
  3798. }
  3799. else
  3800. {
  3801. Configuration->Usage->Inc("RemoteCopyDirect");
  3802. DebugAssert(DirectCopy);
  3803. DebugAssert(Session == FTerminal);
  3804. if (FTerminal->IsCapable[fcRemoteCopy] ||
  3805. FTerminal->CommandSessionOpened ||
  3806. CommandSessionFallback())
  3807. {
  3808. Terminal->CopyFiles(FileList, Target, FileMask);
  3809. }
  3810. }
  3811. }
  3812. catch(...)
  3813. {
  3814. RemoteDirView->DiscardSavedSelection();
  3815. throw;
  3816. }
  3817. RemoteDirView->RestoreSelection();
  3818. }
  3819. }
  3820. return Result;
  3821. }
  3822. //---------------------------------------------------------------------------
  3823. void __fastcall TCustomScpExplorerForm::CreateRemoteDirectory(
  3824. const UnicodeString & Path, TRemoteProperties & Properties)
  3825. {
  3826. Properties.Valid = Properties.Valid << vpEncrypt;
  3827. Properties.Encrypt = GUIConfiguration->CurrentCopyParam.EncryptNewFiles;
  3828. RemoteDirView->CreateDirectoryEx(Path, &Properties);
  3829. }
  3830. //---------------------------------------------------------------------------
  3831. void __fastcall TCustomScpExplorerForm::CreateDirectory(TOperationSide Side)
  3832. {
  3833. Side = GetSide(Side);
  3834. TRemoteProperties Properties = GUIConfiguration->NewDirectoryProperties;
  3835. TRemoteProperties * AProperties = (Side == osRemote ? &Properties : NULL);
  3836. UnicodeString Name = LoadStr(NEW_FOLDER);
  3837. int AllowedChanges =
  3838. FLAGMASK((Side == osRemote) && Terminal->IsCapable[fcModeChanging], cpMode);
  3839. bool SaveSettings = false;
  3840. if (DoCreateDirectoryDialog(Name, AProperties, AllowedChanges, SaveSettings))
  3841. {
  3842. TWindowLock Lock(this);
  3843. if (Side == osRemote)
  3844. {
  3845. if (SaveSettings)
  3846. {
  3847. GUIConfiguration->NewDirectoryProperties = Properties;
  3848. }
  3849. CreateRemoteDirectory(Name, Properties);
  3850. }
  3851. else
  3852. {
  3853. DirView(Side)->CreateDirectory(Name);
  3854. }
  3855. }
  3856. }
  3857. //---------------------------------------------------------------------------
  3858. void __fastcall TCustomScpExplorerForm::HomeDirectory(TOperationSide Side)
  3859. {
  3860. TWindowLock Lock(this);
  3861. DirView(Side)->ExecuteHomeDirectory();
  3862. }
  3863. //---------------------------------------------------------------------------
  3864. void __fastcall TCustomScpExplorerForm::OpenDirectory(TOperationSide Side)
  3865. {
  3866. DoOpenDirectoryDialog(odBrowse, Side);
  3867. }
  3868. //---------------------------------------------------------------------------
  3869. bool __fastcall TCustomScpExplorerForm::OpenBookmark(TOperationSide Side, TBookmark * Bookmark)
  3870. {
  3871. UnicodeString Path = Bookmark->GetSideDirectory(Side);
  3872. bool Result = !Path.IsEmpty();
  3873. if (Result)
  3874. {
  3875. // While we might get here when the session is closed (from location profiles),
  3876. // it's not a problem as the Path setter is noop then
  3877. DirView(Side)->Path = Path;
  3878. }
  3879. return Result;
  3880. }
  3881. //---------------------------------------------------------------------------
  3882. void __fastcall TCustomScpExplorerForm::CalculateSize(
  3883. TStrings * FileList, __int64 & Size, TCalculateSizeStats & Stats,
  3884. bool & Close)
  3885. {
  3886. // terminal can be already closed (e.g. dropped connection)
  3887. if (Terminal != NULL)
  3888. {
  3889. try
  3890. {
  3891. Terminal->CalculateFilesSize(FileList, Size, 0, NULL, true, Stats);
  3892. }
  3893. catch(...)
  3894. {
  3895. if (!Terminal->Active)
  3896. {
  3897. Close = true;
  3898. }
  3899. throw;
  3900. }
  3901. }
  3902. }
  3903. //---------------------------------------------------------------------------
  3904. void __fastcall TCustomScpExplorerForm::CalculateChecksum(const UnicodeString & Alg,
  3905. TStrings * FileList, TCalculatedChecksumEvent OnCalculatedChecksum,
  3906. bool & Close)
  3907. {
  3908. // terminal can be already closed (e.g. dropped connection)
  3909. if (Terminal != NULL)
  3910. {
  3911. Configuration->Usage->Inc(L"ChecksumCalculated");
  3912. try
  3913. {
  3914. Terminal->CalculateFilesChecksum(Alg, FileList, NULL, OnCalculatedChecksum);
  3915. }
  3916. catch(...)
  3917. {
  3918. if (!Terminal->Active)
  3919. {
  3920. Close = true;
  3921. }
  3922. throw;
  3923. }
  3924. }
  3925. }
  3926. //---------------------------------------------------------------------------
  3927. bool __fastcall TCustomScpExplorerForm::SetProperties(TOperationSide Side, TStrings * FileList)
  3928. {
  3929. bool Result;
  3930. if (Side == osRemote)
  3931. {
  3932. TRemoteTokenList * GroupList = NULL;
  3933. TRemoteTokenList * UserList = NULL;
  3934. try
  3935. {
  3936. TRemoteProperties CurrentProperties;
  3937. if (Terminal->LoadFilesProperties(FileList))
  3938. {
  3939. RemoteDirView->Invalidate();
  3940. }
  3941. bool CapableGroupChanging = Terminal->IsCapable[fcGroupChanging];
  3942. bool CapableOwnerChanging = Terminal->IsCapable[fcOwnerChanging];
  3943. if (CapableGroupChanging || CapableOwnerChanging)
  3944. {
  3945. if (CapableGroupChanging)
  3946. {
  3947. GroupList = Terminal->Groups->Duplicate();
  3948. }
  3949. if (CapableOwnerChanging)
  3950. {
  3951. UserList = Terminal->Users->Duplicate();
  3952. }
  3953. TRemoteDirectory * Files = Terminal->Files;
  3954. int Count = Files->Count;
  3955. if (Count > 100)
  3956. {
  3957. Count = 100;
  3958. }
  3959. for (int Index = 0; Index < Count; Index++)
  3960. {
  3961. TRemoteFile * File = Files->Files[Index];
  3962. if (CapableGroupChanging)
  3963. {
  3964. GroupList->AddUnique(File->Group);
  3965. }
  3966. if (CapableOwnerChanging)
  3967. {
  3968. UserList->AddUnique(File->Owner);
  3969. }
  3970. }
  3971. // if we haven't collected tokens for all files in current directory,
  3972. // make sure we collect them at least for all selected files.
  3973. // (note that so far the files in FileList has to be from current direcotry)
  3974. if (Count < Files->Count)
  3975. {
  3976. for (int Index = 0; Index < FileList->Count; Index++)
  3977. {
  3978. TRemoteFile * File = (TRemoteFile *)(FileList->Objects[Index]);
  3979. if (CapableGroupChanging)
  3980. {
  3981. GroupList->AddUnique(File->Group);
  3982. }
  3983. if (CapableOwnerChanging)
  3984. {
  3985. UserList->AddUnique(File->Owner);
  3986. }
  3987. }
  3988. }
  3989. }
  3990. CurrentProperties = TRemoteProperties::CommonProperties(FileList);
  3991. int Flags = 0;
  3992. if (Terminal->IsCapable[fcModeChanging]) Flags |= cpMode;
  3993. if (CapableOwnerChanging) Flags |= cpOwner;
  3994. if (CapableGroupChanging) Flags |= cpGroup;
  3995. TCalculateChecksumEvent CalculateChecksumEvent = NULL;
  3996. if (Terminal->IsCapable[fcCalculatingChecksum])
  3997. {
  3998. CalculateChecksumEvent = CalculateChecksum;
  3999. }
  4000. std::unique_ptr<TStrings> ChecksumAlgs(new TStringList());
  4001. Terminal->GetSupportedChecksumAlgs(ChecksumAlgs.get());
  4002. TRemoteProperties NewProperties = CurrentProperties;
  4003. Result =
  4004. DoPropertiesDialog(FileList, RemoteDirView->PathName,
  4005. GroupList, UserList, ChecksumAlgs.get(), &NewProperties, Flags,
  4006. Terminal->IsCapable[fcGroupOwnerChangingByID],
  4007. CalculateSize, CalculateChecksumEvent);
  4008. if (Result)
  4009. {
  4010. NewProperties = TRemoteProperties::ChangedProperties(CurrentProperties, NewProperties);
  4011. Terminal->ChangeFilesProperties(FileList, &NewProperties);
  4012. }
  4013. }
  4014. __finally
  4015. {
  4016. delete GroupList;
  4017. delete UserList;
  4018. }
  4019. }
  4020. else
  4021. {
  4022. DirView(Side)->DisplayPropertiesMenu();
  4023. Result = true;
  4024. }
  4025. return Result;
  4026. }
  4027. //---------------------------------------------------------------------------
  4028. void __fastcall TCustomScpExplorerForm::KeyProcessed(Word & Key, TShiftState Shift)
  4029. {
  4030. if (Shift * AllKeyShiftStates() == (TShiftState() << ssAlt))
  4031. {
  4032. FIgnoreNextDialogChar = Key;
  4033. }
  4034. Key = 0;
  4035. }
  4036. //---------------------------------------------------------------------------
  4037. void __fastcall TCustomScpExplorerForm::CheckCustomCommandShortCut(
  4038. TCustomCommandList * List, Word & Key, Classes::TShiftState Shift, TShortCut KeyShortCut)
  4039. {
  4040. const TCustomCommandType * Command = List->Find(KeyShortCut);
  4041. if (Command != NULL)
  4042. {
  4043. KeyProcessed(Key, Shift);
  4044. if (CustomCommandState(*Command, false, ccltAll) > 0)
  4045. {
  4046. TAutoFlag DontCopyCommandToClipboardFlag(DontCopyCommandToClipboard);
  4047. ExecuteFileOperationCommand(foCustomCommand, osRemote,
  4048. false, false, const_cast<TCustomCommandType *>(Command));
  4049. }
  4050. }
  4051. }
  4052. //---------------------------------------------------------------------------
  4053. void __fastcall TCustomScpExplorerForm::KeyDown(Word & Key, Classes::TShiftState Shift)
  4054. {
  4055. if (QueueView3->Focused() && (QueueView3->OnKeyDown != NULL))
  4056. {
  4057. QueueView3->OnKeyDown(QueueView3, Key, Shift);
  4058. }
  4059. if (!DirView(osCurrent)->IsEditing())
  4060. {
  4061. TShortCut KeyShortCut = ShortCut(Key, Shift);
  4062. for (int Index = 0; Index < NonVisualDataModule->ExplorerActions->ActionCount; Index++)
  4063. {
  4064. TAction * Action = (TAction *)NonVisualDataModule->ExplorerActions->Actions[Index];
  4065. if (((Action->ShortCut == KeyShortCut) ||
  4066. (Action->SecondaryShortCuts->IndexOfShortCut(KeyShortCut) >= 0)) &&
  4067. AllowedAction(Action, aaShortCut))
  4068. {
  4069. // Has to be called before the action as the dialog char is already in queue.
  4070. // So when the action consumes message queue, we already need to have the
  4071. // FIgnoreNextDialogChar set
  4072. KeyProcessed(Key, Shift);
  4073. // Reset reference to previous component (when menu/toolbar was clicked).
  4074. // Needed to detect that action was invoked by keyboard shortcut
  4075. // in TNonVisualDataModule::ExplorerActionsExecute
  4076. Action->ActionComponent = NULL;
  4077. Action->Execute();
  4078. return;
  4079. }
  4080. }
  4081. for (int i = 0; i < TTerminalManager::Instance()->Count; i++)
  4082. {
  4083. if (NonVisualDataModule->OpenSessionShortCut(i) == KeyShortCut)
  4084. {
  4085. KeyProcessed(Key, Shift);
  4086. TTerminalManager::Instance()->ActiveTerminalIndex = i;
  4087. return;
  4088. }
  4089. }
  4090. if (Key == VK_TAB && Shift.Contains(ssCtrl))
  4091. {
  4092. KeyProcessed(Key, Shift);
  4093. TTerminalManager::Instance()->CycleTerminals(!Shift.Contains(ssShift));
  4094. }
  4095. TShortCut CustomShortCut = NormalizeCustomShortCut(KeyShortCut);
  4096. if (IsCustomShortCut(CustomShortCut))
  4097. {
  4098. CheckCustomCommandShortCut(WinConfiguration->CustomCommandList, Key, Shift, CustomShortCut);
  4099. CheckCustomCommandShortCut(WinConfiguration->ExtensionList, Key, Shift, CustomShortCut);
  4100. if (WinConfiguration->SharedBookmarks != NULL)
  4101. {
  4102. TBookmark * Bookmark = WinConfiguration->SharedBookmarks->FindByShortCut(CustomShortCut);
  4103. if ((Bookmark != NULL) &&
  4104. OpenBookmark(FCurrentSide, Bookmark))
  4105. {
  4106. KeyProcessed(Key, Shift);
  4107. }
  4108. }
  4109. }
  4110. }
  4111. TForm::KeyDown(Key, Shift);
  4112. }
  4113. //---------------------------------------------------------------------------
  4114. void __fastcall TCustomScpExplorerForm::InitStatusBar()
  4115. {
  4116. DebugAssert(Terminal != NULL);
  4117. const TSessionInfo & SessionInfo = Terminal->GetSessionInfo();
  4118. const TFileSystemInfo & FileSystemInfo = Terminal->GetFileSystemInfo();
  4119. TTBXStatusBar * SessionStatusBar = (TTBXStatusBar *)GetComponent(fcStatusBar);
  4120. int Offset = SessionStatusBar->Panels->Count - SessionPanelCount;
  4121. bool SecurityEnabled = !SessionInfo.SecurityProtocolName.IsEmpty();
  4122. SessionStatusBar->Panels->Items[Offset + 0]->Enabled = SecurityEnabled;
  4123. // expanded from ?: to avoid memory leaks
  4124. if (SecurityEnabled)
  4125. {
  4126. SessionStatusBar->Panels->Items[Offset + 0]->Hint =
  4127. FMTLOAD(STATUS_SECURE, (SessionInfo.SecurityProtocolName));
  4128. }
  4129. else
  4130. {
  4131. SessionStatusBar->Panels->Items[Offset + 0]->Hint = LoadStr(STATUS_INSECURE);
  4132. }
  4133. if (FileSystemInfo.ProtocolName.IsEmpty())
  4134. {
  4135. SessionStatusBar->Panels->Items[Offset + 1]->Caption = SessionInfo.ProtocolName;
  4136. }
  4137. else
  4138. {
  4139. SessionStatusBar->Panels->Items[Offset + 1]->Caption = FileSystemInfo.ProtocolName;
  4140. }
  4141. SessionStatusBar->Panels->Items[Offset + 1]->Hint = LoadStr(STATUS_PROTOCOL_HINT);
  4142. SessionStatusBar->Panels->Items[Offset + 2]->Enabled =
  4143. (!SessionInfo.CSCompression.IsEmpty() || !SessionInfo.SCCompression.IsEmpty());
  4144. UnicodeString CSCompressionStr = DefaultStr(SessionInfo.CSCompression, LoadStr(NO_STR));
  4145. UnicodeString SCCompressionStr = DefaultStr(SessionInfo.SCCompression, LoadStr(NO_STR));
  4146. UnicodeString CompressionStr = CSCompressionStr;
  4147. if (CSCompressionStr != SCCompressionStr)
  4148. {
  4149. CompressionStr += UnicodeString(L"/") + SCCompressionStr;
  4150. }
  4151. SessionStatusBar->Panels->Items[Offset + 2]->Hint = FMTLOAD(STATUS_COMPRESSION_HINT, (CompressionStr));
  4152. SessionStatusBar->Panels->Items[Offset + 3]->Hint = LoadStr(STATUS_DURATION_HINT);
  4153. UpdateStatusBar();
  4154. }
  4155. //---------------------------------------------------------------------------
  4156. void __fastcall TCustomScpExplorerForm::UpdateStatusBar()
  4157. {
  4158. TTBXStatusBar * SessionStatusBar = (TTBXStatusBar *)GetComponent(fcStatusBar);
  4159. DebugAssert(SessionStatusBar != NULL);
  4160. if (!Terminal || !Terminal->Active || Terminal->Status < ssOpened)
  4161. {
  4162. // note: (Terminal->Status < sshReady) currently never happens here,
  4163. // so STATUS_CONNECTING is never used
  4164. SessionStatusBar->SimplePanel = true;
  4165. SessionStatusBar->SimpleText = LoadStr(
  4166. !Terminal || !Terminal->Active ? STATUS_NOT_CONNECTED : STATUS_CONNECTING);
  4167. }
  4168. else
  4169. {
  4170. DebugAssert(Terminal);
  4171. SessionStatusBar->SimplePanel = false;
  4172. const TSessionInfo & SessionInfo = Terminal->GetSessionInfo();
  4173. if (!FNote.IsEmpty())
  4174. {
  4175. SessionStatusBar->Panels->Items[0]->Caption = FNote;
  4176. }
  4177. else
  4178. {
  4179. UpdateStatusPanelText(SessionStatusBar->Panels->Items[0]);
  4180. }
  4181. SessionStatusBar->Panels->Items[0]->Hint = FNoteHints;
  4182. SessionStatusBar->Panels->Items[SessionStatusBar->Panels->Count - 1]->Caption =
  4183. FormatDateTimeSpan(Configuration->TimeFormat, Now() - SessionInfo.LoginTime);
  4184. }
  4185. }
  4186. //---------------------------------------------------------------------------
  4187. void __fastcall TCustomScpExplorerForm::UpdateStatusPanelText(TTBXStatusPanel * Panel)
  4188. {
  4189. Panel->Caption = L"";
  4190. }
  4191. //---------------------------------------------------------------------------
  4192. void __fastcall TCustomScpExplorerForm::Idle()
  4193. {
  4194. if (FShowing || FStandaloneEditing)
  4195. {
  4196. FEditorManager->Check();
  4197. // make sure that Idle is called before update queue, as it may invoke QueueEvent
  4198. // that needs to know if queue view is visible (and it may be closed after queue update)
  4199. TTerminalManager::Instance()->Idle(FDoNotIdleCurrentTerminal > 0);
  4200. }
  4201. if (FShowing)
  4202. {
  4203. if (!NonVisualDataModule->Busy &&
  4204. // Menu is opened or drag&drop is going on
  4205. (Mouse->Capture == NULL))
  4206. {
  4207. if (FRefreshRemoteDirectory)
  4208. {
  4209. if ((Terminal != NULL) && (Terminal->Status == ssOpened))
  4210. {
  4211. Terminal->RefreshDirectory();
  4212. }
  4213. FRefreshRemoteDirectory = false;
  4214. }
  4215. if (FRefreshLocalDirectory)
  4216. {
  4217. ReloadLocalDirectory();
  4218. FRefreshLocalDirectory = false;
  4219. }
  4220. if (WinConfiguration->RefreshRemotePanel)
  4221. {
  4222. TManagedTerminal * ManagedTerminal =
  4223. dynamic_cast<TManagedTerminal *>(Terminal);
  4224. if ((ManagedTerminal != NULL) && (Terminal->Status == ssOpened) &&
  4225. (Now() - ManagedTerminal->DirectoryLoaded >
  4226. WinConfiguration->RefreshRemotePanelInterval))
  4227. {
  4228. RemoteDirView->ReloadDirectory();
  4229. }
  4230. }
  4231. }
  4232. }
  4233. if (FShowing || FStandaloneEditing)
  4234. {
  4235. if (FQueueStatusInvalidated)
  4236. {
  4237. UpdateQueueStatus(false);
  4238. }
  4239. RefreshQueueItems();
  4240. }
  4241. if (FShowing)
  4242. {
  4243. UpdateStatusBar();
  4244. }
  4245. FIgnoreNextDialogChar = 0;
  4246. }
  4247. //---------------------------------------------------------------------------
  4248. void __fastcall TCustomScpExplorerForm::UserActionTimer(TObject * /*Sender*/)
  4249. {
  4250. try
  4251. {
  4252. FUserActionTimer->Enabled = false;
  4253. if (IsQueueAutoPopup() && (FPendingQueueActionItem != NULL))
  4254. {
  4255. if (TQueueItem::IsUserActionStatus(FPendingQueueActionItem->Status))
  4256. {
  4257. FPendingQueueActionItem->ProcessUserAction();
  4258. }
  4259. }
  4260. }
  4261. __finally
  4262. {
  4263. FPendingQueueActionItem = NULL;
  4264. }
  4265. }
  4266. //---------------------------------------------------------------------------
  4267. void __fastcall TCustomScpExplorerForm::ApplicationMinimize(TObject * /*Sender*/)
  4268. {
  4269. if (WinConfiguration->MinimizeToTray)
  4270. {
  4271. UpdateTrayIcon();
  4272. FTrayIcon->Visible = true;
  4273. if (Visible)
  4274. {
  4275. ShowWindow(Handle, SW_HIDE);
  4276. }
  4277. }
  4278. }
  4279. //---------------------------------------------------------------------------
  4280. void __fastcall TCustomScpExplorerForm::ApplicationRestore(TObject * /*Sender*/)
  4281. {
  4282. // WORKAROUND
  4283. // When restoring maximized window from minimization,
  4284. // rarely some controls do not align properly.
  4285. // Two instances seen (both for Commander):
  4286. // - When restoring, window is temporarily narrower (not maximized),
  4287. // causing toolbars on TopDock to wrap and dock to expand horizontally.
  4288. // Once maximized already, top dock shinks back, but the session PageControl,
  4289. // do not align up, leaving space between TopDock and PageControl.
  4290. // - Similar issue seen with LocalDirView not aligning down to status bar.
  4291. for (int Index = 0; Index < ControlCount; Index++)
  4292. {
  4293. RealignControl(Controls[Index]);
  4294. }
  4295. if (FTrayIcon->Visible)
  4296. {
  4297. FTrayIcon->Visible = false;
  4298. if (Visible)
  4299. {
  4300. ShowWindow(Handle, SW_SHOW);
  4301. }
  4302. }
  4303. if (FNeedSession && DebugAlwaysTrue(Terminal == NULL))
  4304. {
  4305. FNeedSession = false;
  4306. NonVisualDataModule->StartBusy();
  4307. try
  4308. {
  4309. NeedSession(false);
  4310. }
  4311. __finally
  4312. {
  4313. NonVisualDataModule->EndBusy();
  4314. }
  4315. }
  4316. }
  4317. //---------------------------------------------------------------------------
  4318. void __fastcall TCustomScpExplorerForm::UpdateTrayIcon()
  4319. {
  4320. FTrayIcon->Hint = GetMainForm()->Caption;
  4321. }
  4322. //---------------------------------------------------------------------------
  4323. void __fastcall TCustomScpExplorerForm::ApplicationTitleChanged()
  4324. {
  4325. UpdateTrayIcon();
  4326. }
  4327. //---------------------------------------------------------------------------
  4328. void __fastcall TCustomScpExplorerForm::RestoreApp()
  4329. {
  4330. // workaround
  4331. // TApplication.WndProc sets TApplication.FAppIconic to false,
  4332. // on WM_ACTIVATEAPP, what renders TApplication.Restore no-op function.
  4333. // But WM_ACTIVATEAPP message can be received even when
  4334. // the main window is minimized to the tray and internal editor window is focused
  4335. // (after another application was previously active)
  4336. if (::IsIconic(Handle))
  4337. {
  4338. if (!IsAppIconic())
  4339. {
  4340. SetAppIconic(true);
  4341. }
  4342. }
  4343. ::ApplicationRestore();
  4344. Application->BringToFront();
  4345. }
  4346. //---------------------------------------------------------------------------
  4347. void __fastcall TCustomScpExplorerForm::TrayIconClick(TObject * /*Sender*/)
  4348. {
  4349. RestoreApp();
  4350. }
  4351. //---------------------------------------------------------------------------
  4352. void __fastcall TCustomScpExplorerForm::NewSession(bool FromSite, const UnicodeString & SessionUrl)
  4353. {
  4354. if (OpenInNewWindow())
  4355. {
  4356. // todo: Pass FromSite
  4357. ExecuteNewInstance(SessionUrl);
  4358. }
  4359. else
  4360. {
  4361. TTerminalManager::Instance()->NewSession(FromSite, SessionUrl);
  4362. }
  4363. }
  4364. //---------------------------------------------------------------------------
  4365. UnicodeString __fastcall TCustomScpExplorerForm::CreateHiddenDuplicateSession()
  4366. {
  4367. UnicodeString SessionName = StoredSessions->HiddenPrefix + Terminal->SessionData->SessionName;
  4368. // current working directories become defaults here, what is not right
  4369. std::unique_ptr<TSessionData> SessionData(CloneCurrentSessionData());
  4370. StoredSessions->NewSession(SessionName, SessionData.get());
  4371. // modified only, explicit
  4372. StoredSessions->Save(false, true);
  4373. // encode session name because of slashes in hierarchical sessions
  4374. return EncodeUrlString(SessionName);
  4375. }
  4376. //---------------------------------------------------------------------------
  4377. void __fastcall TCustomScpExplorerForm::DuplicateSession()
  4378. {
  4379. if (OpenInNewWindow())
  4380. {
  4381. ExecuteNewInstance(CreateHiddenDuplicateSession());
  4382. }
  4383. else
  4384. {
  4385. // current working directories become defaults here, what is not right
  4386. std::unique_ptr<TSessionData> SessionData(CloneCurrentSessionData());
  4387. TTerminalManager * Manager = TTerminalManager::Instance();
  4388. TTerminal * Terminal = Manager->NewTerminal(SessionData.get());
  4389. Manager->ActiveTerminal = Terminal;
  4390. }
  4391. }
  4392. //---------------------------------------------------------------------------
  4393. void __fastcall TCustomScpExplorerForm::RenameSession()
  4394. {
  4395. UnicodeString Name = Terminal->SessionData->Name;
  4396. if (InputDialog(LoadStr(RENAME_SESSION_TITLE), LoadStr(RENAME_SESSION_PROMPT), Name, HELP_SESSION_RENAME))
  4397. {
  4398. // Checks for a slash only, so it's not that big deal that we do the check after submitting the dialog only.
  4399. TSessionData::ValidateName(Name);
  4400. // This is inconsistent with how color (for example) is handled.
  4401. Terminal->SessionData->Name = Name;
  4402. UpdateControls();
  4403. // Add/Remove distinguishing paths from sessions of the same name.
  4404. DoTerminalListChanged();
  4405. }
  4406. }
  4407. //---------------------------------------------------------------------------
  4408. bool __fastcall TCustomScpExplorerForm::CanCloseQueue(TTerminalQueue * Queue)
  4409. {
  4410. DebugAssert(Queue != NULL);
  4411. bool Result = Queue->IsEmpty;
  4412. if (!Result)
  4413. {
  4414. SetFocus();
  4415. Result = (MessageDialog(LoadStr(PENDING_QUEUE_ITEMS2), qtWarning, qaOK | qaCancel, HELP_NONE) == qaOK);
  4416. }
  4417. return Result;
  4418. }
  4419. //---------------------------------------------------------------------------
  4420. bool __fastcall TCustomScpExplorerForm::CanCloseQueue()
  4421. {
  4422. return CanCloseQueue(FQueue);
  4423. }
  4424. //---------------------------------------------------------------------------
  4425. void __fastcall TCustomScpExplorerForm::CloseSession()
  4426. {
  4427. if (CanCloseQueue())
  4428. {
  4429. TTerminalManager::Instance()->FreeActiveTerminal();
  4430. }
  4431. }
  4432. //---------------------------------------------------------------------------
  4433. void __fastcall TCustomScpExplorerForm::OpenStoredSession(TSessionData * Data)
  4434. {
  4435. if (OpenInNewWindow())
  4436. {
  4437. // encode session name because of slashes in hierarchical sessions
  4438. ExecuteNewInstance(EncodeUrlString(Data->Name));
  4439. }
  4440. else
  4441. {
  4442. TTerminalManager * Manager = TTerminalManager::Instance();
  4443. TTerminal * Terminal = Manager->NewTerminal(Data);
  4444. Manager->ActiveTerminal = Terminal;
  4445. }
  4446. }
  4447. //---------------------------------------------------------------------------
  4448. void __fastcall TCustomScpExplorerForm::OpenFolderOrWorkspace(const UnicodeString & Name)
  4449. {
  4450. if (OpenInNewWindow())
  4451. {
  4452. ExecuteNewInstance(Name);
  4453. }
  4454. else
  4455. {
  4456. TTerminalManager * Manager = TTerminalManager::Instance();
  4457. std::unique_ptr<TObjectList> DataList(new TObjectList());
  4458. StoredSessions->GetFolderOrWorkspace(Name, DataList.get());
  4459. Manager->ActiveTerminal = Manager->NewTerminals(DataList.get());
  4460. }
  4461. }
  4462. //---------------------------------------------------------------------------
  4463. void __fastcall TCustomScpExplorerForm::FormCloseQuery(TObject * /*Sender*/,
  4464. bool &CanClose)
  4465. {
  4466. if (Terminal != NULL)
  4467. {
  4468. if (Terminal->Active && WinConfiguration->ConfirmClosingSession)
  4469. {
  4470. unsigned int Result;
  4471. TMessageParams Params(mpNeverAskAgainCheck);
  4472. UnicodeString Message;
  4473. int Answers = qaOK | qaCancel;
  4474. if (TTerminalManager::Instance()->Count > 1)
  4475. {
  4476. if (!WinConfiguration->AutoSaveWorkspace)
  4477. {
  4478. Message = LoadStr(CLOSE_SESSIONS_WORKSPACE2);
  4479. Answers = qaYes | qaNo | qaCancel;
  4480. }
  4481. else
  4482. {
  4483. Message = MainInstructions(LoadStr(CLOSE_SESSIONS));
  4484. }
  4485. }
  4486. else
  4487. {
  4488. Message = MainInstructions(FMTLOAD(CLOSE_SESSION, (Terminal->SessionData->SessionName)));
  4489. }
  4490. if (WinConfiguration->AutoSaveWorkspace)
  4491. {
  4492. Message =
  4493. FORMAT("%s\n\n%s", (Message,
  4494. FMTLOAD(AUTO_WORKSPACE, (WorkspaceName()))));
  4495. }
  4496. SetFocus();
  4497. Result = MessageDialog(Message, qtConfirmation,
  4498. Answers, HELP_NONE, &Params);
  4499. if (Result == qaNeverAskAgain)
  4500. {
  4501. WinConfiguration->ConfirmClosingSession = false;
  4502. }
  4503. if (Result == qaNo)
  4504. {
  4505. CanClose = SaveWorkspace(true);
  4506. // note that the workspace will be saved redundatly again from FormClose
  4507. }
  4508. else
  4509. {
  4510. CanClose =
  4511. (Result == qaOK) ||
  4512. (Result == qaYes) || // CLOSE_SESSIONS_WORKSPACE variant
  4513. (Result == qaNeverAskAgain);
  4514. }
  4515. }
  4516. if (CanClose)
  4517. {
  4518. CanClose = CanCloseQueue();
  4519. }
  4520. }
  4521. if (CanClose)
  4522. {
  4523. CanClose =
  4524. FEditorManager->CloseInternalEditors(CloseInternalEditor) &&
  4525. FEditorManager->CloseExternalFilesWithoutProcess();
  4526. if (CanClose)
  4527. {
  4528. while (CanClose && (FLocalEditors->Count > 0))
  4529. {
  4530. int PrevCount = FLocalEditors->Count;
  4531. static_cast<TForm *>(FLocalEditors->Items[0])->Close();
  4532. CanClose = (FLocalEditors->Count < PrevCount);
  4533. }
  4534. if (CanClose)
  4535. {
  4536. CanClose = FEditorManager->Empty(true);
  4537. if (!CanClose)
  4538. {
  4539. SetFocus();
  4540. CanClose =
  4541. (MessageDialog(
  4542. LoadStr(PENDING_EDITORS), qtWarning, qaIgnore | qaCancel, HELP_NONE) == qaIgnore);
  4543. }
  4544. }
  4545. }
  4546. }
  4547. }
  4548. //---------------------------------------------------------------------------
  4549. void __fastcall TCustomScpExplorerForm::CloseInternalEditor(TObject * Sender)
  4550. {
  4551. TForm * Form = dynamic_cast<TForm *>(Sender);
  4552. DebugAssert(Form != NULL);
  4553. Form->Close();
  4554. }
  4555. //---------------------------------------------------------------------------
  4556. void __fastcall TCustomScpExplorerForm::ForceCloseInternalEditor(TObject * Sender)
  4557. {
  4558. TForm * Form = dynamic_cast<TForm *>(Sender);
  4559. delete Form;
  4560. }
  4561. //---------------------------------------------------------------------------
  4562. void __fastcall TCustomScpExplorerForm::ForceCloseLocalEditors()
  4563. {
  4564. while (FLocalEditors->Count > 0)
  4565. {
  4566. delete static_cast<TForm *>(FLocalEditors->Items[0]);
  4567. }
  4568. }
  4569. //---------------------------------------------------------------------------
  4570. void __fastcall TCustomScpExplorerForm::RemoteDirViewDisplayProperties(
  4571. TObject *Sender)
  4572. {
  4573. TStrings *FileList = ((TUnixDirView*)Sender)->CreateFileList(True, False, NULL);
  4574. try
  4575. {
  4576. SetProperties(osRemote, FileList);
  4577. }
  4578. __finally
  4579. {
  4580. delete FileList;
  4581. }
  4582. }
  4583. //---------------------------------------------------------------------------
  4584. void __fastcall TCustomScpExplorerForm::ComponentShowing(Byte Component, bool value)
  4585. {
  4586. if (value)
  4587. {
  4588. if (Component == fcCustomCommandsBand)
  4589. {
  4590. UpdateCustomCommandsToolbar();
  4591. }
  4592. }
  4593. }
  4594. //---------------------------------------------------------------------------
  4595. void __fastcall TCustomScpExplorerForm::SetComponentVisible(Byte Component, Boolean value)
  4596. {
  4597. TControl * Control = GetComponent(Component);
  4598. DebugAssert(Control);
  4599. bool Changed = (Control->Visible != value);
  4600. if (Changed)
  4601. {
  4602. ComponentShowing(Component, value);
  4603. TWinControl * WinControl = dynamic_cast<TWinControl*>(Control);
  4604. bool WasFocused = (WinControl != NULL) && (ActiveControl != NULL) &&
  4605. ((ActiveControl == WinControl) || (ActiveControl->Parent == WinControl));
  4606. if (value)
  4607. {
  4608. int RemainingHeight = Control->Parent->ClientHeight;
  4609. int RemainingWidth = Control->Parent->ClientWidth;
  4610. for (int i = 0; i < Control->Parent->ControlCount; i++)
  4611. {
  4612. TControl * ChildControl = Control->Parent->Controls[i];
  4613. if (ChildControl->Visible)
  4614. {
  4615. switch (ChildControl->Align)
  4616. {
  4617. case alTop:
  4618. case alBottom:
  4619. RemainingHeight -= ChildControl->Height;
  4620. break;
  4621. case alLeft:
  4622. case alRight:
  4623. RemainingWidth -= ChildControl->Width;
  4624. break;
  4625. }
  4626. }
  4627. }
  4628. int Reserve = ScaleByTextHeight(this, 32);
  4629. // queue in explorer, trees in commander
  4630. if (Control->Height > RemainingHeight - Reserve)
  4631. {
  4632. Control->Height = RemainingHeight / 2;
  4633. }
  4634. if (Control->Width > RemainingWidth - Reserve)
  4635. {
  4636. Control->Width = RemainingWidth / 2;
  4637. }
  4638. }
  4639. Control->Visible = value;
  4640. if (WasFocused && Visible)
  4641. {
  4642. DirView(osCurrent)->SetFocus();
  4643. }
  4644. FixControlsPlacement();
  4645. }
  4646. }
  4647. //---------------------------------------------------------------------------
  4648. bool __fastcall TCustomScpExplorerForm::GetComponentVisible(Byte Component)
  4649. {
  4650. TControl * Control = GetComponent(Component);
  4651. if (Control == NULL)
  4652. {
  4653. return false;
  4654. }
  4655. else
  4656. {
  4657. return Control->Visible;
  4658. }
  4659. }
  4660. //---------------------------------------------------------------------------
  4661. bool __fastcall TCustomScpExplorerForm::IsComponentPossible(Byte Component)
  4662. {
  4663. bool Result;
  4664. if ((Component == fcExplorerUpdatesBand) || (Component == fcCommanderUpdatesBand))
  4665. {
  4666. Result = !IsUWP();
  4667. }
  4668. else
  4669. {
  4670. Result = true;
  4671. }
  4672. return Result;
  4673. }
  4674. //---------------------------------------------------------------------------
  4675. void __fastcall TCustomScpExplorerForm::FixControlsPlacement()
  4676. {
  4677. if (RemoteDirView->ItemFocused != NULL)
  4678. {
  4679. RemoteDirView->ItemFocused->MakeVisible(false);
  4680. }
  4681. QueueSplitter->Visible = QueuePanel->Visible;
  4682. RemotePanelSplitter->Visible = RemoteDriveView->Visible;
  4683. }
  4684. //---------------------------------------------------------------------------
  4685. TControl * __fastcall TCustomScpExplorerForm::GetComponent(Byte Component)
  4686. {
  4687. switch (Component) {
  4688. case fcStatusBar: return RemoteStatusBar;
  4689. case fcRemotePopup: return reinterpret_cast<TControl *>(NonVisualDataModule->RemoteFilePopup);
  4690. case fcQueueView: return QueuePanel;
  4691. case fcQueueToolbar: return QueueDock;
  4692. case fcRemoteTree: return RemoteDriveView;
  4693. case fcSessionsTabs: return SessionsPageControl;
  4694. default: return NULL;
  4695. }
  4696. }
  4697. //---------------------------------------------------------------------------
  4698. void __fastcall TCustomScpExplorerForm::DirViewColumnRightClick(
  4699. TObject *Sender, TListColumn *Column, TPoint &Point)
  4700. {
  4701. DebugAssert(NonVisualDataModule && Column && Sender);
  4702. NonVisualDataModule->ListColumn = Column;
  4703. TPopupMenu * DirViewColumnMenu;
  4704. if (Sender == RemoteDirView)
  4705. {
  4706. DirViewColumnMenu = NonVisualDataModule->RemoteDirViewColumnPopup;
  4707. NonVisualDataModule->RemoteSortByExtColumnPopupItem->Visible =
  4708. (Column->Index == uvName);
  4709. NonVisualDataModule->RemoteFormatSizeBytesPopupItem->Visible =
  4710. (Column->Index == uvSize);
  4711. }
  4712. else
  4713. {
  4714. DirViewColumnMenu = NonVisualDataModule->LocalDirViewColumnPopup;
  4715. NonVisualDataModule->LocalSortByExtColumnPopupItem->Visible =
  4716. (Column->Index == dvName);
  4717. NonVisualDataModule->LocalFormatSizeBytesPopupItem->Visible =
  4718. (Column->Index == dvSize);
  4719. }
  4720. TCustomListView * ListView = DebugNotNull(dynamic_cast<TCustomListView *>(Sender));
  4721. TPoint ScreenPoint = ListView->ClientToScreen(Point);
  4722. ScreenPoint.x -= GetScrollPos(ListView->Handle, SB_HORZ);
  4723. DirViewColumnMenu->Popup(ScreenPoint.x, ScreenPoint.y);
  4724. }
  4725. //---------------------------------------------------------------------------
  4726. void __fastcall TCustomScpExplorerForm::DirViewExecFile(
  4727. TObject *Sender, TListItem *Item, bool &AllowExec)
  4728. {
  4729. DoDirViewExecFile(Sender, Item, AllowExec);
  4730. }
  4731. //---------------------------------------------------------------------------
  4732. void __fastcall TCustomScpExplorerForm::DoDirViewExecFile(TObject * Sender,
  4733. TListItem * Item, bool & AllowExec)
  4734. {
  4735. DebugAssert(Sender && Item && Configuration);
  4736. DebugAssert(AllowExec);
  4737. TCustomDirView * ADirView = (TCustomDirView *)Sender;
  4738. bool Remote = (ADirView == DirView(osRemote));
  4739. bool ResolvedSymlinks = !Remote || Terminal->ResolvingSymlinks;
  4740. TOperationSide Side = (Remote ? osRemote : osLocal);
  4741. // Anything special is done on double click only (not on "open" indicated by FForceExecution),
  4742. // on files only (not directories)
  4743. // and only when symlinks are resolved (apply to remote panel only)
  4744. if (!ADirView->ItemIsDirectory(Item) &&
  4745. (ResolvedSymlinks || FForceExecution || Terminal->IsEncryptingFiles()))
  4746. {
  4747. if ((WinConfiguration->DoubleClickAction != dcaOpen) &&
  4748. !FForceExecution &&
  4749. (ResolvedSymlinks || Terminal->IsEncryptingFiles()))
  4750. {
  4751. if (WinConfiguration->DoubleClickAction == dcaCopy)
  4752. {
  4753. // To counter the lock in DirViewBusy (called from TCustomDirView.DoExecute)
  4754. UnlockWindow();
  4755. try
  4756. {
  4757. ExecuteFileOperation(
  4758. foCopy, Side, true, !WinConfiguration->CopyOnDoubleClickConfirmation);
  4759. }
  4760. __finally
  4761. {
  4762. LockWindow();
  4763. }
  4764. AllowExec = false;
  4765. }
  4766. else if (WinConfiguration->DoubleClickAction == dcaEdit)
  4767. {
  4768. if (!Remote || !WinConfiguration->DisableOpenEdit)
  4769. {
  4770. ExecuteFile(Side, efDefaultEditor);
  4771. AllowExec = false;
  4772. }
  4773. }
  4774. else
  4775. {
  4776. DebugFail();
  4777. }
  4778. }
  4779. // if we have not done anything special, fall back to default behavior
  4780. if (AllowExec)
  4781. {
  4782. if (Remote && !WinConfiguration->DisableOpenEdit)
  4783. {
  4784. ExecuteFile(osRemote, efShell);
  4785. AllowExec = false;
  4786. }
  4787. }
  4788. }
  4789. }
  4790. //---------------------------------------------------------------------------
  4791. bool __fastcall TCustomScpExplorerForm::GetHasDirView(TOperationSide Side)
  4792. {
  4793. return ((Side == osRemote) || (Side == osCurrent));
  4794. }
  4795. //---------------------------------------------------------------------------
  4796. void __fastcall TCustomScpExplorerForm::CompareDirectories()
  4797. {
  4798. DebugFail();
  4799. }
  4800. //---------------------------------------------------------------------------
  4801. void __fastcall TCustomScpExplorerForm::SynchronizeDirectories()
  4802. {
  4803. DebugFail();
  4804. }
  4805. //---------------------------------------------------------------------------
  4806. bool __fastcall TCustomScpExplorerForm::DoSynchronizeDirectories(
  4807. UnicodeString & LocalDirectory, UnicodeString & RemoteDirectory, bool UseDefaults)
  4808. {
  4809. TSynchronizeParamType Params;
  4810. Params.LocalDirectory = LocalDirectory;
  4811. Params.RemoteDirectory = RemoteDirectory;
  4812. int UnusedParams =
  4813. (GUIConfiguration->SynchronizeParams &
  4814. (TTerminal::spPreviewChanges | TTerminal::spTimestamp | TTerminal::spNotByTime | TTerminal::spBySize));
  4815. Params.Params = GUIConfiguration->SynchronizeParams & ~UnusedParams;
  4816. Params.Options = GUIConfiguration->SynchronizeOptions;
  4817. bool SaveSettings = false;
  4818. TSynchronizeController Controller(&DoSynchronize, &DoSynchronizeInvalid,
  4819. &DoSynchronizeTooManyDirectories);
  4820. DebugAssert(FSynchronizeController == NULL);
  4821. FSynchronizeController = &Controller;
  4822. bool Result;
  4823. try
  4824. {
  4825. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  4826. int CopyParamAttrs = Terminal->UsableCopyParamAttrs(0).Upload;
  4827. int Options =
  4828. FLAGMASK(SynchronizeAllowSelectedOnly(), soAllowSelectedOnly);
  4829. DebugAssert(FOnFeedSynchronizeError == NULL);
  4830. Result = DoSynchronizeDialog(Params, &CopyParam, Controller.StartStop,
  4831. SaveSettings, Options, CopyParamAttrs, GetSynchronizeOptions, SynchronizeSessionLog,
  4832. FOnFeedSynchronizeError, SynchronizeInNewWindow, UseDefaults);
  4833. if (Result)
  4834. {
  4835. if (SaveSettings)
  4836. {
  4837. GUIConfiguration->SynchronizeParams = Params.Params | UnusedParams;
  4838. GUIConfiguration->SynchronizeOptions = Params.Options;
  4839. }
  4840. else
  4841. {
  4842. if (FLAGSET(GUIConfiguration->SynchronizeOptions, soSynchronizeAsk) &&
  4843. FLAGCLEAR(Params.Options, soSynchronizeAsk) &&
  4844. FLAGSET(Params.Options, soSynchronize))
  4845. {
  4846. GUIConfiguration->SynchronizeOptions =
  4847. (GUIConfiguration->SynchronizeOptions & ~soSynchronizeAsk) |
  4848. soSynchronize;
  4849. }
  4850. }
  4851. LocalDirectory = Params.LocalDirectory;
  4852. RemoteDirectory = Params.RemoteDirectory;
  4853. }
  4854. }
  4855. __finally
  4856. {
  4857. FSynchronizeController = NULL;
  4858. DebugAssert(FOnFeedSynchronizeError == NULL);
  4859. FOnFeedSynchronizeError = NULL;
  4860. }
  4861. return Result;
  4862. }
  4863. //---------------------------------------------------------------------------
  4864. void __fastcall TCustomScpExplorerForm::DoSynchronize(
  4865. TSynchronizeController * /*Sender*/, const UnicodeString LocalDirectory,
  4866. const UnicodeString RemoteDirectory, const TCopyParamType & CopyParam,
  4867. const TSynchronizeParamType & Params, TSynchronizeChecklist ** Checklist,
  4868. TSynchronizeOptions * Options, bool Full)
  4869. {
  4870. // Keep-up-to-date mode
  4871. if (Terminal->Status == ssOpened)
  4872. {
  4873. try
  4874. {
  4875. int PParams = Params.Params;
  4876. if (!Full)
  4877. {
  4878. PParams |= TTerminal::spNoRecurse | TTerminal::spUseCache | TTerminal::spDelayProgress;
  4879. if (FLAGSET(Params.Options, soRecurse))
  4880. {
  4881. PParams |= TTerminal::spSubDirs;
  4882. }
  4883. }
  4884. else
  4885. {
  4886. // if keepuptodate is non-recursive,
  4887. // full sync before has to be non-recursive as well
  4888. if (FLAGCLEAR(Params.Options, soRecurse))
  4889. {
  4890. PParams |= TTerminal::spNoRecurse;
  4891. }
  4892. }
  4893. Synchronize(LocalDirectory, RemoteDirectory, smRemote, CopyParam,
  4894. PParams, Checklist, Options);
  4895. }
  4896. catch (Exception & E)
  4897. {
  4898. if (FLAGSET(Params.Options, soContinueOnError) && Terminal->Active &&
  4899. DebugAlwaysTrue(FOnFeedSynchronizeError != NULL))
  4900. {
  4901. // noop, already logged in MoreMessageDialog
  4902. }
  4903. else
  4904. {
  4905. ShowExtendedExceptionEx(Terminal, &E);
  4906. throw;
  4907. }
  4908. }
  4909. }
  4910. }
  4911. //---------------------------------------------------------------------------
  4912. void __fastcall TCustomScpExplorerForm::DoSynchronizeInvalid(
  4913. TSynchronizeController * /*Sender*/, const UnicodeString Directory,
  4914. const UnicodeString ErrorStr)
  4915. {
  4916. if (!Directory.IsEmpty())
  4917. {
  4918. SimpleErrorDialog(FMTLOAD(WATCH_ERROR_DIRECTORY, (Directory)), ErrorStr);
  4919. }
  4920. else
  4921. {
  4922. SimpleErrorDialog(LoadStr(WATCH_ERROR_GENERAL), ErrorStr);
  4923. }
  4924. }
  4925. //---------------------------------------------------------------------------
  4926. void __fastcall TCustomScpExplorerForm::DoSynchronizeTooManyDirectories(
  4927. TSynchronizeController * /*Sender*/, int & MaxDirectories)
  4928. {
  4929. if (MaxDirectories < GUIConfiguration->MaxWatchDirectories)
  4930. {
  4931. MaxDirectories = GUIConfiguration->MaxWatchDirectories;
  4932. }
  4933. else
  4934. {
  4935. TMessageParams Params(mpNeverAskAgainCheck);
  4936. unsigned int Result = MessageDialog(
  4937. FMTLOAD(TOO_MANY_WATCH_DIRECTORIES, (MaxDirectories, MaxDirectories)),
  4938. qtConfirmation, qaYes | qaNo, HELP_TOO_MANY_WATCH_DIRECTORIES, &Params);
  4939. if ((Result == qaYes) || (Result == qaNeverAskAgain))
  4940. {
  4941. MaxDirectories *= 2;
  4942. if (Result == qaNeverAskAgain)
  4943. {
  4944. GUIConfiguration->MaxWatchDirectories = MaxDirectories;
  4945. }
  4946. }
  4947. else
  4948. {
  4949. Abort();
  4950. }
  4951. }
  4952. }
  4953. //---------------------------------------------------------------------------
  4954. void __fastcall TCustomScpExplorerForm::Synchronize(const UnicodeString LocalDirectory,
  4955. const UnicodeString RemoteDirectory, TSynchronizeMode Mode,
  4956. const TCopyParamType & CopyParam, int Params, TSynchronizeChecklist ** Checklist,
  4957. TSynchronizeOptions * Options)
  4958. {
  4959. // Keep-up-to-date mode
  4960. DebugAssert(!FAutoOperation);
  4961. void * BatchStorage;
  4962. BatchStart(BatchStorage);
  4963. FAutoOperation = true;
  4964. bool AnyOperation = false;
  4965. TDateTime StartTime = Now();
  4966. TSynchronizeChecklist * AChecklist = NULL;
  4967. try
  4968. {
  4969. FSynchronizeProgressForm = new TSynchronizeProgressForm(Application, true, true);
  4970. if (FLAGCLEAR(Params, TTerminal::spDelayProgress))
  4971. {
  4972. FSynchronizeProgressForm->Start();
  4973. }
  4974. AChecklist = Terminal->SynchronizeCollect(LocalDirectory, RemoteDirectory,
  4975. static_cast<TTerminal::TSynchronizeMode>(Mode),
  4976. &CopyParam, Params | TTerminal::spNoConfirmation, TerminalSynchronizeDirectory,
  4977. Options);
  4978. SAFE_DESTROY(FSynchronizeProgressForm);
  4979. AnyOperation = (AChecklist->CheckedCount > 0);
  4980. if (AnyOperation)
  4981. {
  4982. TSynchronizeProgress SynchronizeProgress(AChecklist);
  4983. CreateProgressForm(&SynchronizeProgress);
  4984. try
  4985. {
  4986. Terminal->SynchronizeApply(
  4987. AChecklist, &CopyParam, Params | TTerminal::spNoConfirmation, TerminalSynchronizeDirectory,
  4988. SynchronizeProcessedItem, NULL, NULL, NULL);
  4989. }
  4990. __finally
  4991. {
  4992. DestroyProgressForm();
  4993. }
  4994. }
  4995. }
  4996. __finally
  4997. {
  4998. if (Checklist == NULL)
  4999. {
  5000. delete AChecklist;
  5001. }
  5002. else
  5003. {
  5004. *Checklist = AChecklist;
  5005. }
  5006. FAutoOperation = false;
  5007. SAFE_DESTROY(FSynchronizeProgressForm);
  5008. BatchEnd(BatchStorage);
  5009. ReloadLocalDirectory();
  5010. if (AnyOperation)
  5011. {
  5012. OperationComplete(StartTime);
  5013. }
  5014. }
  5015. }
  5016. //---------------------------------------------------------------------------
  5017. bool __fastcall TCustomScpExplorerForm::SynchronizeAllowSelectedOnly()
  5018. {
  5019. // can be called from command line
  5020. return Visible &&
  5021. ((DirView(osRemote)->SelCount > 0) ||
  5022. (HasDirView[osLocal] && (DirView(osLocal)->SelCount > 0)));
  5023. }
  5024. //---------------------------------------------------------------------------
  5025. void __fastcall TCustomScpExplorerForm::SynchronizeSessionLog(const UnicodeString & Message)
  5026. {
  5027. LogSynchronizeEvent(Terminal, Message);
  5028. }
  5029. //---------------------------------------------------------------------------
  5030. void __fastcall TCustomScpExplorerForm::GetSynchronizeOptions(
  5031. int Params, TSynchronizeOptions & Options)
  5032. {
  5033. if (FLAGSET(Params, TTerminal::spSelectedOnly) && SynchronizeAllowSelectedOnly())
  5034. {
  5035. Options.Filter = new TStringList();
  5036. Options.Filter->CaseSensitive = false;
  5037. Options.Filter->Duplicates = Types::dupAccept;
  5038. if (DirView(osRemote)->SelCount > 0)
  5039. {
  5040. DirView(osRemote)->CreateFileList(false, false, Options.Filter);
  5041. }
  5042. if (HasDirView[osLocal] && (DirView(osLocal)->SelCount > 0))
  5043. {
  5044. DirView(osLocal)->CreateFileList(false, false, Options.Filter);
  5045. }
  5046. Options.Filter->Sort();
  5047. }
  5048. }
  5049. //---------------------------------------------------------------------------
  5050. void __fastcall TCustomScpExplorerForm::SynchronizeInNewWindow(
  5051. const TSynchronizeParamType & Params, const TCopyParamType * CopyParams)
  5052. {
  5053. UnicodeString SessionName = CreateHiddenDuplicateSession();
  5054. UnicodeString AdditionalParams =
  5055. FORMAT(L"%s \"%s\" \"%s\" %d %d %s", (
  5056. TProgramParams::FormatSwitch(KEEP_UP_TO_DATE_SWITCH),
  5057. Params.LocalDirectory, Params.RemoteDirectory, Params.Params, Params.Options,
  5058. TProgramParams::FormatSwitch(DEFAULTS_SWITCH)));
  5059. TCopyParamType Defaults;
  5060. std::unique_ptr<THierarchicalStorage> ConfigStorage(Configuration->CreateConfigStorage());
  5061. ConfigStorage->AccessMode = smRead;
  5062. if (ConfigStorage->OpenSubKey(Configuration->ConfigurationSubKey, false, false))
  5063. {
  5064. GUIConfiguration->LoadCopyParam(ConfigStorage.get(), &Defaults);
  5065. }
  5066. ConfigStorage.reset(NULL);
  5067. std::unique_ptr<TStringList> Options(new TStringList());
  5068. std::unique_ptr<TOptionsStorage> OptionsStorage(new TOptionsStorage(Options.get(), true));
  5069. GUIConfiguration->SaveCopyParam(OptionsStorage.get(), CopyParams, &Defaults);
  5070. if (Options->Count > 0)
  5071. {
  5072. AdditionalParams +=
  5073. FORMAT(L" %s%s", (TProgramParams::FormatSwitch(RAW_CONFIG_SWITCH), StringsToParams(Options.get())));
  5074. }
  5075. ExecuteNewInstance(SessionName, AdditionalParams);
  5076. }
  5077. //---------------------------------------------------------------------------
  5078. struct TSynchronizeParams
  5079. {
  5080. UnicodeString LocalDirectory;
  5081. UnicodeString RemoteDirectory;
  5082. TSynchronizeMode Mode;
  5083. TSynchronizeChecklist * Checklist;
  5084. int Params;
  5085. TCopyParamType * CopyParam;
  5086. TDateTime CollectElapsed;
  5087. TDateTime * StartTime;
  5088. TProcessedSynchronizationChecklistItem OnProcessedItem;
  5089. };
  5090. //---------------------------------------------------------------------------
  5091. void __fastcall TCustomScpExplorerForm::FullSynchronize(
  5092. TSynchronizeParams & Params, TProcessedSynchronizationChecklistItem OnProcessedItem,
  5093. TUpdatedSynchronizationChecklistItems OnUpdatedSynchronizationChecklistItems)
  5094. {
  5095. DebugAssert(!FAutoOperation);
  5096. void * BatchStorage;
  5097. BatchStart(BatchStorage);
  5098. FAutoOperation = true;
  5099. TFileOperationStatistics Statistics;
  5100. TDateTime Start = Now();
  5101. try
  5102. {
  5103. Params.OnProcessedItem = OnProcessedItem;
  5104. TSynchronizeProgress SynchronizeProgress(Params.Checklist);
  5105. CreateProgressForm(&SynchronizeProgress);
  5106. Terminal->SynchronizeApply(
  5107. Params.Checklist, Params.CopyParam, Params.Params | TTerminal::spNoConfirmation,
  5108. TerminalSynchronizeDirectory, SynchronizeProcessedItem, OnUpdatedSynchronizationChecklistItems, &Params,
  5109. &Statistics);
  5110. }
  5111. __finally
  5112. {
  5113. Params.OnProcessedItem = NULL;
  5114. FAutoOperation = false;
  5115. DestroyProgressForm();
  5116. BatchEnd(BatchStorage);
  5117. ReloadLocalDirectory();
  5118. }
  5119. if (WinConfiguration->SynchronizeSummary)
  5120. {
  5121. UnicodeString Message = MainInstructions(LoadStr(SYNCHRONIZE_COMPLETE)) + L"\n";
  5122. // The statistics should be 0 anyway in this case
  5123. if (FLAGCLEAR(Params.Params, TTerminal::spTimestamp))
  5124. {
  5125. Message += L"\n";
  5126. if (Statistics.FilesUploaded > 0)
  5127. {
  5128. Message += FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 1), (FormatNumber(Statistics.FilesUploaded), FormatBytes(Statistics.TotalUploaded))) + L"\n";
  5129. }
  5130. if (Statistics.FilesDownloaded > 0)
  5131. {
  5132. Message += FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 2), (FormatNumber(Statistics.FilesDownloaded), FormatBytes(Statistics.TotalDownloaded))) + L"\n";
  5133. }
  5134. if (Statistics.FilesDeletedLocal > 0)
  5135. {
  5136. Message += FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 3), (FormatNumber(Statistics.FilesDeletedLocal))) + L"\n";
  5137. }
  5138. if (Statistics.FilesDeletedRemote > 0)
  5139. {
  5140. Message += FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 4), (FormatNumber(Statistics.FilesDeletedRemote))) + L"\n";
  5141. }
  5142. }
  5143. TDateTime Elapsed = (Now() - Start);
  5144. Message +=
  5145. L"\n" +
  5146. FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 5), (FormatDateTimeSpan(Configuration->TimeFormat, Params.CollectElapsed))) + L"\n" +
  5147. FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 6), (FormatDateTimeSpan(Configuration->TimeFormat, Elapsed)));
  5148. TMessageParams Params(mpNeverAskAgainCheck);
  5149. unsigned int Result = MessageDialog(Message, qtInformation, qaOK, HELP_NONE, &Params);
  5150. if (Result == qaNeverAskAgain)
  5151. {
  5152. WinConfiguration->SynchronizeSummary = false;
  5153. }
  5154. }
  5155. }
  5156. //---------------------------------------------------------------------------
  5157. void __fastcall TCustomScpExplorerForm::SynchronizeProcessedItem(void * Token, const TSynchronizeChecklist::TItem * ChecklistItem)
  5158. {
  5159. if (DebugAlwaysTrue(FProgressForm != NULL) && DebugAlwaysTrue(FProgressForm->SynchronizeProgress != NULL))
  5160. {
  5161. FProgressForm->SynchronizeProgress->ItemProcessed(ChecklistItem);
  5162. }
  5163. // Not set in keep-up-to-date mode - Synchronize() method
  5164. if (Token != NULL)
  5165. {
  5166. TSynchronizeParams & Params = *static_cast<TSynchronizeParams *>(Token);
  5167. if (Params.OnProcessedItem != NULL)
  5168. {
  5169. Params.OnProcessedItem(NULL, ChecklistItem);
  5170. }
  5171. }
  5172. }
  5173. //---------------------------------------------------------------------------
  5174. void __fastcall TCustomScpExplorerForm::DoFullSynchronize(
  5175. void * Token, TProcessedSynchronizationChecklistItem OnProcessedItem,
  5176. TUpdatedSynchronizationChecklistItems OnUpdatedSynchronizationChecklistItems)
  5177. {
  5178. TSynchronizeParams & Params = *static_cast<TSynchronizeParams *>(Token);
  5179. *Params.StartTime = Now();
  5180. FullSynchronize(Params, OnProcessedItem, OnUpdatedSynchronizationChecklistItems);
  5181. }
  5182. //---------------------------------------------------------------------------
  5183. void __fastcall TCustomScpExplorerForm::DoSynchronizeChecklistCalculateSize(
  5184. TSynchronizeChecklist * Checklist, const TSynchronizeChecklist::TItemList & Items, void * Token)
  5185. {
  5186. // terminal can be already closed (e.g. dropped connection)
  5187. if (Terminal != NULL)
  5188. {
  5189. TSynchronizeParams & Params = *static_cast<TSynchronizeParams *>(Token);
  5190. Terminal->SynchronizeChecklistCalculateSize(Checklist, Items, Params.CopyParam);
  5191. }
  5192. }
  5193. //---------------------------------------------------------------------------
  5194. void __fastcall TCustomScpExplorerForm::DoSynchronizeMove(
  5195. TOperationSide Side, const UnicodeString & FileName, const UnicodeString & NewFileName, TRemoteFile * RemoteFile)
  5196. {
  5197. TAutoBatch AutoBatch(this);
  5198. TAutoFlag AutoOperationFlag(FAutoOperation);
  5199. if (Side == osRemote)
  5200. {
  5201. std::unique_ptr<TStrings> FileList(new TStringList());
  5202. FileList->AddObject(FileName, RemoteFile);
  5203. UnicodeString Target = UnixExtractFileDir(NewFileName);
  5204. UnicodeString FileMask = DelimitFileNameMask(UnixExtractFileName(NewFileName));
  5205. RemoteDirView->SaveSelection();
  5206. RemoteDirView->SaveSelectedNames();
  5207. try
  5208. {
  5209. Terminal->MoveFiles(FileList.get(), Target, FileMask);
  5210. }
  5211. catch(...)
  5212. {
  5213. RemoteDirView->DiscardSavedSelection();
  5214. throw;
  5215. }
  5216. RemoteDirView->RestoreSelection();
  5217. }
  5218. else if (DebugAlwaysTrue(Side == osLocal))
  5219. {
  5220. if (!MoveFile(FileName.c_str(), NewFileName.c_str()))
  5221. {
  5222. throw EOSExtException(FMTLOAD(RENAME_FILE_ERROR, (FileName, NewFileName)));
  5223. }
  5224. UnicodeString Directory = ExtractFileDir(FileName);
  5225. ReloadLocalDirectory(Directory);
  5226. UnicodeString NewDirectory = ExtractFileDir(NewFileName);
  5227. if (!SamePaths(Directory, NewDirectory))
  5228. {
  5229. ReloadLocalDirectory(NewDirectory);
  5230. }
  5231. }
  5232. }
  5233. //---------------------------------------------------------------------------
  5234. int __fastcall TCustomScpExplorerForm::DoFullSynchronizeDirectories(
  5235. UnicodeString & LocalDirectory, UnicodeString & RemoteDirectory,
  5236. TSynchronizeMode & Mode, bool & SaveMode, bool UseDefaults)
  5237. {
  5238. int Result;
  5239. int Params = GUIConfiguration->SynchronizeParams;
  5240. bool SaveSettings = false;
  5241. int Options =
  5242. FLAGMASK(!Terminal->IsCapable[fcTimestampChanging], fsoDisableTimestamp) |
  5243. FLAGMASK(SynchronizeAllowSelectedOnly(), fsoAllowSelectedOnly);
  5244. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  5245. TUsableCopyParamAttrs CopyParamAttrs = Terminal->UsableCopyParamAttrs(0);
  5246. bool Continue =
  5247. UseDefaults ||
  5248. DoFullSynchronizeDialog(Mode, Params, LocalDirectory, RemoteDirectory,
  5249. &CopyParam, SaveSettings, SaveMode, Options, CopyParamAttrs);
  5250. if (Continue)
  5251. {
  5252. Configuration->Usage->Inc(L"Synchronizations");
  5253. UpdateCopyParamCounters(CopyParam);
  5254. TSynchronizeOptions SynchronizeOptions;
  5255. GetSynchronizeOptions(Params, SynchronizeOptions);
  5256. if (SaveSettings)
  5257. {
  5258. GUIConfiguration->SynchronizeParams = Params;
  5259. }
  5260. else
  5261. {
  5262. SaveMode = false;
  5263. }
  5264. TDateTime StartTime = Now();
  5265. TSynchronizeChecklist * Checklist = NULL;
  5266. try
  5267. {
  5268. DebugAssert(!FAutoOperation);
  5269. FAutoOperation = true;
  5270. try
  5271. {
  5272. FSynchronizeProgressForm = new TSynchronizeProgressForm(Application, true, true);
  5273. FSynchronizeProgressForm->Start();
  5274. Checklist = Terminal->SynchronizeCollect(LocalDirectory, RemoteDirectory,
  5275. static_cast<TTerminal::TSynchronizeMode>(Mode),
  5276. &CopyParam, Params | TTerminal::spNoConfirmation, TerminalSynchronizeDirectory,
  5277. &SynchronizeOptions);
  5278. }
  5279. __finally
  5280. {
  5281. FAutoOperation = false;
  5282. SAFE_DESTROY(FSynchronizeProgressForm);
  5283. }
  5284. TSynchronizeParams SynchronizeParams;
  5285. SynchronizeParams.LocalDirectory = LocalDirectory;
  5286. SynchronizeParams.RemoteDirectory = RemoteDirectory;
  5287. SynchronizeParams.Mode = Mode;
  5288. SynchronizeParams.CopyParam = &CopyParam;
  5289. SynchronizeParams.Params = Params;
  5290. SynchronizeParams.Checklist = Checklist;
  5291. SynchronizeParams.CollectElapsed = (Now() - StartTime);
  5292. SynchronizeParams.StartTime = &StartTime;
  5293. SynchronizeParams.OnProcessedItem = NULL;
  5294. Result = Checklist->Count;
  5295. if (Checklist->Count > 0)
  5296. {
  5297. if (FLAGSET(Params, TTerminal::spPreviewChanges))
  5298. {
  5299. if (!DoSynchronizeChecklistDialog(
  5300. Checklist, Mode, Params, LocalDirectory, RemoteDirectory, CustomCommandMenu, DoFullSynchronize,
  5301. DoSynchronizeChecklistCalculateSize, DoSynchronizeMove, &SynchronizeParams))
  5302. {
  5303. Result = -1;
  5304. }
  5305. }
  5306. else
  5307. {
  5308. FullSynchronize(SynchronizeParams, NULL, NULL);
  5309. }
  5310. }
  5311. else
  5312. {
  5313. UnicodeString Message = MainInstructions(LoadStr(COMPARE_NO_DIFFERENCES));
  5314. MessageDialog(Message, qtInformation, qaOK, HELP_SYNCHRONIZE_NO_DIFFERENCES);
  5315. }
  5316. }
  5317. __finally
  5318. {
  5319. delete Checklist;
  5320. }
  5321. OperationComplete(StartTime);
  5322. }
  5323. else
  5324. {
  5325. Result = -1;
  5326. }
  5327. return Result;
  5328. }
  5329. //---------------------------------------------------------------------------
  5330. void __fastcall TCustomScpExplorerForm::TerminalSynchronizeDirectory(
  5331. const UnicodeString LocalDirectory, const UnicodeString RemoteDirectory,
  5332. bool & Continue, bool Collect)
  5333. {
  5334. if (Collect)
  5335. {
  5336. DebugAssert(FSynchronizeProgressForm != NULL);
  5337. if (!FSynchronizeProgressForm->Started)
  5338. {
  5339. FSynchronizeProgressForm->Start();
  5340. }
  5341. FSynchronizeProgressForm->SetData(LocalDirectory, RemoteDirectory, Continue);
  5342. }
  5343. else
  5344. {
  5345. DebugAssert(FSynchronizeProgressForm == NULL);
  5346. DebugAssert(FProgressForm != NULL);
  5347. }
  5348. }
  5349. //---------------------------------------------------------------------------
  5350. void __fastcall TCustomScpExplorerForm::StandaloneEdit(const UnicodeString & FileName)
  5351. {
  5352. UnicodeString FullFileName = AbsolutePath(FTerminal->CurrentDirectory, FileName);
  5353. TRemoteFile * File = NULL;
  5354. Terminal->ReadFile(FullFileName, File);
  5355. if (File != NULL)
  5356. {
  5357. std::unique_ptr<TRemoteFile> FileOwner(File);
  5358. TAutoFlag Flag(FStandaloneEditing);
  5359. ExecuteRemoteFile(FullFileName, File, efInternalEditor);
  5360. Application->ShowMainForm = false;
  5361. Application->Run();
  5362. }
  5363. }
  5364. //---------------------------------------------------------------------------
  5365. void __fastcall TCustomScpExplorerForm::ExploreLocalDirectory()
  5366. {
  5367. DebugFail();
  5368. }
  5369. //---------------------------------------------------------------------------
  5370. TSessionData * __fastcall TCustomScpExplorerForm::CloneCurrentSessionData()
  5371. {
  5372. std::unique_ptr<TSessionData> SessionData(new TSessionData(L""));
  5373. SessionData->Assign(Terminal->SessionData);
  5374. UpdateSessionData(SessionData.get());
  5375. Terminal->UpdateSessionCredentials(SessionData.get());
  5376. if (!Terminal->SessionData->HasSessionName())
  5377. {
  5378. // Particularly for "Workspace/XXXX" name, we need to reset it, as it would become user-visible
  5379. // once IsWorkspace is cleared
  5380. SessionData->Name = UnicodeString();
  5381. SessionData->IsWorkspace = false;
  5382. }
  5383. return SessionData.release();
  5384. }
  5385. //---------------------------------------------------------------------------
  5386. void __fastcall TCustomScpExplorerForm::SaveCurrentSession()
  5387. {
  5388. TSessionData * SessionData = CloneCurrentSessionData();
  5389. try
  5390. {
  5391. TSessionData * EditingSessionData = StoredSessions->FindSame(SessionData);
  5392. DoSaveSession(SessionData, EditingSessionData, true, NULL);
  5393. }
  5394. __finally
  5395. {
  5396. delete SessionData;
  5397. }
  5398. }
  5399. //---------------------------------------------------------------------------
  5400. TObjectList * __fastcall TCustomScpExplorerForm::DoCollectWorkspace()
  5401. {
  5402. TTerminalManager * Manager = TTerminalManager::Instance();
  5403. std::unique_ptr<TObjectList> DataList(new TObjectList());
  5404. if (DebugAlwaysTrue(Terminal != NULL))
  5405. {
  5406. // Update (Managed)Terminal->StateData
  5407. UpdateTerminal(Terminal);
  5408. }
  5409. Manager->SaveWorkspace(DataList.get());
  5410. return DataList.release();
  5411. }
  5412. //---------------------------------------------------------------------------
  5413. void __fastcall TCustomScpExplorerForm::DoSaveWorkspace(
  5414. const UnicodeString & Name, TObjectList * DataList, bool SavePasswords, bool Explicit)
  5415. {
  5416. WinConfiguration->LastWorkspace = Name;
  5417. if (!SavePasswords)
  5418. {
  5419. for (int Index = 0; Index < DataList->Count; Index++)
  5420. {
  5421. TSessionData * SessionData = dynamic_cast<TSessionData *>(DataList->Items[Index]);
  5422. if (SessionData->Link.IsEmpty())
  5423. {
  5424. SessionData->ClearSessionPasswords();
  5425. }
  5426. }
  5427. }
  5428. StoredSessions->NewWorkspace(Name, DataList);
  5429. // modified only
  5430. StoredSessions->Save(false, Explicit);
  5431. }
  5432. //---------------------------------------------------------------------------
  5433. UnicodeString __fastcall TCustomScpExplorerForm::WorkspaceName()
  5434. {
  5435. return
  5436. DefaultStr(WinConfiguration->LastWorkspace,
  5437. DefaultStr(WinConfiguration->AutoWorkspace, LoadStr(NEW_WORKSPACE)));
  5438. }
  5439. //---------------------------------------------------------------------------
  5440. bool __fastcall TCustomScpExplorerForm::SaveWorkspace(bool EnableAutoSave)
  5441. {
  5442. std::unique_ptr<TObjectList> DataList(DoCollectWorkspace());
  5443. bool AnyNonStoredSessionWithPassword = false;
  5444. bool AnyNonStoredNonWorkspaceSessionWithPassword = false;
  5445. bool AllNonStoredSessionsAnonymous = true;
  5446. TTerminalManager * Manager = TTerminalManager::Instance();
  5447. for (int Index = 0; Index < DataList->Count; Index++)
  5448. {
  5449. TSessionData * SessionData = dynamic_cast<TSessionData *>(DataList->Items[Index]);
  5450. if (SessionData->Link.IsEmpty())
  5451. {
  5452. if (SessionData->HasAnySessionPassword())
  5453. {
  5454. AnyNonStoredSessionWithPassword = true;
  5455. if (!Manager->Terminals[Index]->SessionData->IsWorkspace)
  5456. {
  5457. AnyNonStoredNonWorkspaceSessionWithPassword = true;
  5458. }
  5459. }
  5460. if (!SameText(SessionData->UserName, AnonymousUserName))
  5461. {
  5462. AllNonStoredSessionsAnonymous = false;
  5463. }
  5464. }
  5465. }
  5466. bool SavePasswords;
  5467. bool * PSavePasswords;
  5468. bool NotRecommendedSavingPasswords =
  5469. !CustomWinConfiguration->UseMasterPassword &&
  5470. !AllNonStoredSessionsAnonymous;
  5471. if (Configuration->DisablePasswordStoring ||
  5472. !AnyNonStoredSessionWithPassword)
  5473. {
  5474. PSavePasswords = NULL;
  5475. SavePasswords = false;
  5476. }
  5477. else
  5478. {
  5479. PSavePasswords = &SavePasswords;
  5480. SavePasswords =
  5481. !AnyNonStoredNonWorkspaceSessionWithPassword ||
  5482. !NotRecommendedSavingPasswords;
  5483. }
  5484. UnicodeString Name = WorkspaceName();
  5485. bool CreateShortcut = false;
  5486. bool Result =
  5487. DoSaveWorkspaceDialog(
  5488. Name, PSavePasswords, NotRecommendedSavingPasswords, CreateShortcut,
  5489. EnableAutoSave);
  5490. if (Result)
  5491. {
  5492. DoSaveWorkspace(Name, DataList.get(), SavePasswords, true);
  5493. if (CreateShortcut)
  5494. {
  5495. TOperationVisualizer Visualizer;
  5496. UnicodeString AdditionalParams = TProgramParams::FormatSwitch(DESKTOP_SWITCH);
  5497. CreateDesktopSessionShortCut(Name, L"", AdditionalParams, -1, WORKSPACE_ICON);
  5498. }
  5499. if (EnableAutoSave)
  5500. {
  5501. WinConfiguration->AutoSaveWorkspace = true;
  5502. WinConfiguration->AutoWorkspace = Name;
  5503. if (PSavePasswords != NULL)
  5504. {
  5505. WinConfiguration->AutoSaveWorkspacePasswords = SavePasswords;
  5506. }
  5507. }
  5508. }
  5509. return Result;
  5510. }
  5511. //---------------------------------------------------------------------------
  5512. void __fastcall TCustomScpExplorerForm::UpdateTerminal(TTerminal * Terminal)
  5513. {
  5514. TManagedTerminal * ManagedTerminal = dynamic_cast<TManagedTerminal *>(Terminal);
  5515. DebugAssert(ManagedTerminal != NULL);
  5516. SAFE_DESTROY(ManagedTerminal->RemoteExplorerState);
  5517. if (WinConfiguration->PreservePanelState)
  5518. {
  5519. ManagedTerminal->RemoteExplorerState = RemoteDirView->SaveState();
  5520. }
  5521. UpdateSessionData(ManagedTerminal->StateData);
  5522. }
  5523. //---------------------------------------------------------------------------
  5524. void __fastcall TCustomScpExplorerForm::UpdateSessionData(TSessionData * Data)
  5525. {
  5526. // Keep in sync with TSessionData::CopyStateData
  5527. DebugAssert(Data != NULL);
  5528. // This is inconsistent with how color (for example) is handled.
  5529. Data->Name = Terminal->SessionData->Name;
  5530. // cannot use RemoteDirView->Path, because it is empty if connection
  5531. // was already closed
  5532. // also only peek, we may not be connected at all atm
  5533. // so make sure we do not try retrieving current directory from the server
  5534. // (particularly with FTP)
  5535. UnicodeString ACurrentDirectory = Terminal->PeekCurrentDirectory();
  5536. if (!ACurrentDirectory.IsEmpty())
  5537. {
  5538. Data->RemoteDirectory = ACurrentDirectory;
  5539. }
  5540. Data->Color = SessionColor;
  5541. }
  5542. //---------------------------------------------------------------------------
  5543. void __fastcall TCustomScpExplorerForm::ToolBarResize(TObject *Sender)
  5544. {
  5545. TTBXToolbar * Toolbar = dynamic_cast<TTBXToolbar*>(Sender);
  5546. DebugAssert(Toolbar != NULL);
  5547. for (int i = 0; i < Toolbar->Items->Count; i++)
  5548. {
  5549. TTBXCustomDropDownItem * DropDownItem;
  5550. DropDownItem = dynamic_cast<TTBXCustomDropDownItem *>(Toolbar->Items->Items[i]);
  5551. if (DropDownItem != NULL)
  5552. {
  5553. ToolbarItemResize(DropDownItem,
  5554. Toolbar->Width - (Toolbar->View->BaseSize.x - DropDownItem->EditWidth) -
  5555. Toolbar->NonClientWidth);
  5556. break;
  5557. }
  5558. }
  5559. }
  5560. //---------------------------------------------------------------------------
  5561. void __fastcall TCustomScpExplorerForm::ToolbarItemResize(TTBXCustomDropDownItem * Item, int Width)
  5562. {
  5563. Item->EditWidth = Width;
  5564. }
  5565. //---------------------------------------------------------------------------
  5566. void __fastcall TCustomScpExplorerForm::ToolbarGetBaseSize(
  5567. TTBCustomToolbar * Toolbar, TPoint & ASize)
  5568. {
  5569. for (int i = 0; i < Toolbar->Items->Count; i++)
  5570. {
  5571. TTBXCustomDropDownItem * DropDownItem;
  5572. DropDownItem = dynamic_cast<TTBXCustomDropDownItem *>(Toolbar->Items->Items[i]);
  5573. if (DropDownItem != NULL)
  5574. {
  5575. ASize.x -= DropDownItem->EditWidth;
  5576. ASize.x += ScaleByTextHeight(this, 50) /* minimal combo width */;
  5577. }
  5578. }
  5579. }
  5580. //---------------------------------------------------------------------------
  5581. void __fastcall TCustomScpExplorerForm::DoWarnLackOfTempSpace(
  5582. const UnicodeString Path, __int64 RequiredSpace, bool & Continue)
  5583. {
  5584. if (WinConfiguration->DDWarnLackOfTempSpace)
  5585. {
  5586. UnicodeString ADrive = ExtractFileDrive(ExpandFileName(Path));
  5587. if (!ADrive.IsEmpty())
  5588. {
  5589. __int64 FreeSpace = DiskFree((Byte)(ADrive[1]-'A'+1));
  5590. DebugAssert(RequiredSpace >= 0);
  5591. __int64 RequiredWithReserve;
  5592. RequiredWithReserve = (__int64)(RequiredSpace * WinConfiguration->DDWarnLackOfTempSpaceRatio);
  5593. if (FreeSpace < RequiredWithReserve)
  5594. {
  5595. unsigned int Result;
  5596. TMessageParams Params(mpNeverAskAgainCheck);
  5597. Result = MessageDialog(FMTLOAD(DD_WARN_LACK_OF_TEMP_SPACE, (Path,
  5598. FormatBytes(FreeSpace), FormatBytes(RequiredSpace))),
  5599. qtWarning, qaYes | qaNo, HELP_DD_WARN_LACK_OF_TEMP_SPACE, &Params);
  5600. if (Result == qaNeverAskAgain)
  5601. {
  5602. WinConfiguration->DDWarnLackOfTempSpace = false;
  5603. }
  5604. Continue = (Result == qaYes || Result == qaNeverAskAgain);
  5605. }
  5606. }
  5607. }
  5608. }
  5609. //---------------------------------------------------------------------------
  5610. void __fastcall TCustomScpExplorerForm::AddBookmark(TOperationSide Side)
  5611. {
  5612. DoOpenDirectoryDialog(odAddBookmark, Side);
  5613. }
  5614. //---------------------------------------------------------------------------
  5615. TStrings * __fastcall TCustomScpExplorerForm::CreateVisitedDirectories(TOperationSide Side)
  5616. {
  5617. // we should better use TCustomDirView::FCaseSensitive, but it is private
  5618. TStringList * VisitedDirectories = CreateSortedStringList((Side == osRemote));
  5619. try
  5620. {
  5621. TCustomDirView * DView = DirView(Side);
  5622. for (int Index = -DView->BackCount; Index <= DView->ForwardCount; Index++)
  5623. {
  5624. VisitedDirectories->Add(DView->HistoryPath[Index]);
  5625. }
  5626. }
  5627. catch (...)
  5628. {
  5629. delete VisitedDirectories;
  5630. throw;
  5631. }
  5632. return VisitedDirectories;
  5633. }
  5634. //---------------------------------------------------------------------------
  5635. void __fastcall TCustomScpExplorerForm::DoOpenDirectoryDialog(
  5636. TOpenDirectoryMode Mode, TOperationSide Side)
  5637. {
  5638. bool Continue = true;
  5639. if (Mode == odAddBookmark)
  5640. {
  5641. TMessageParams Params(mpNeverAskAgainCheck);
  5642. Params.NeverAskAgainTitle = LoadStr(ADD_BOOKMARK_SHARED);
  5643. Params.NeverAskAgainCheckedInitially = WinConfiguration->UseSharedBookmarks;
  5644. UnicodeString Message = MainInstructions(FMTLOAD(ADD_BOOKMARK_CONFIRM, (DirView(Side)->PathName)));
  5645. unsigned int Answer =
  5646. MessageDialog(Message,
  5647. qtConfirmation, qaOK | qaCancel, HELP_ADD_BOOKMARK_CONFIRM, &Params);
  5648. if (Answer == qaNeverAskAgain)
  5649. {
  5650. Continue = true;
  5651. WinConfiguration->UseSharedBookmarks = true;
  5652. }
  5653. else if (Answer == qaOK)
  5654. {
  5655. Continue = true;
  5656. WinConfiguration->UseSharedBookmarks = false;
  5657. }
  5658. else
  5659. {
  5660. Continue = false;
  5661. }
  5662. }
  5663. if (Continue)
  5664. {
  5665. TStrings * VisitedDirectories = CreateVisitedDirectories(Side);
  5666. try
  5667. {
  5668. UnicodeString Name = DirView(Side)->PathName;
  5669. bool Repeat;
  5670. do
  5671. {
  5672. Repeat = false;
  5673. if (::DoOpenDirectoryDialog(Mode, Side, Name, VisitedDirectories, Terminal,
  5674. // do not allow switching to location profiles,
  5675. // if we are not connected
  5676. HasDirView[osLocal] && (Terminal != NULL)))
  5677. {
  5678. TWindowLock Lock(this);
  5679. if (!TryOpenDirectory(Side, Name))
  5680. {
  5681. Repeat = true;
  5682. Mode = odBrowse;
  5683. }
  5684. }
  5685. }
  5686. while (Repeat);
  5687. }
  5688. __finally
  5689. {
  5690. delete VisitedDirectories;
  5691. }
  5692. }
  5693. }
  5694. //---------------------------------------------------------------------------
  5695. bool __fastcall TCustomScpExplorerForm::CommandSessionFallback()
  5696. {
  5697. DebugAssert(!FTerminal->CommandSessionOpened);
  5698. return TTerminalManager::Instance()->ConnectTerminal(FTerminal->CommandSession);
  5699. }
  5700. //---------------------------------------------------------------------------
  5701. bool __fastcall TCustomScpExplorerForm::EnsureCommandSessionFallback(TFSCapability Capability)
  5702. {
  5703. bool Result = FTerminal->IsCapable[Capability] ||
  5704. FTerminal->CommandSessionOpened;
  5705. if (!Result)
  5706. {
  5707. DebugAssert(FTerminal->IsCapable[fcSecondaryShell]);
  5708. if (!GUIConfiguration->ConfirmCommandSession)
  5709. {
  5710. Result = true;
  5711. }
  5712. else
  5713. {
  5714. TMessageParams Params(mpNeverAskAgainCheck);
  5715. const TFileSystemInfo & FileSystemInfo = Terminal->GetFileSystemInfo();
  5716. unsigned int Answer = MessageDialog(FMTLOAD(PERFORM_ON_COMMAND_SESSION2,
  5717. (FileSystemInfo.ProtocolName, FileSystemInfo.ProtocolName)), qtConfirmation,
  5718. qaOK | qaCancel, HELP_PERFORM_ON_COMMAND_SESSION, &Params);
  5719. if (Answer == qaNeverAskAgain)
  5720. {
  5721. GUIConfiguration->ConfirmCommandSession = false;
  5722. Result = true;
  5723. }
  5724. else if (Answer == qaOK)
  5725. {
  5726. Result = true;
  5727. }
  5728. }
  5729. if (Result)
  5730. {
  5731. Result = CommandSessionFallback();
  5732. }
  5733. }
  5734. return Result;
  5735. }
  5736. //---------------------------------------------------------------------------
  5737. void __fastcall TCustomScpExplorerForm::OpenConsole(UnicodeString Command)
  5738. {
  5739. if (EnsureCommandSessionFallback(fcAnyCommand))
  5740. {
  5741. DoConsoleDialog(Terminal, Command);
  5742. }
  5743. }
  5744. //---------------------------------------------------------------------------
  5745. void __fastcall TCustomScpExplorerForm::FileControlDDDragEnter(
  5746. TObject *Sender, _di_IDataObject /*DataObj*/, int /*grfKeyState*/,
  5747. const TPoint & /*Point*/, int & /*dwEffect*/, bool & Accept)
  5748. {
  5749. if (IsFileControl(DropSourceControl, osRemote) &&
  5750. (FDDExtMapFile != NULL))
  5751. {
  5752. Accept = true;
  5753. }
  5754. FDDTargetControl = dynamic_cast<TControl*>(Sender);
  5755. DebugAssert(FDDTargetControl != NULL);
  5756. }
  5757. //---------------------------------------------------------------------------
  5758. void __fastcall TCustomScpExplorerForm::SessionsDDDragEnter(
  5759. _di_IDataObject DataObj, int KeyState,
  5760. const TPoint & Point, int & Effect, bool & Accept)
  5761. {
  5762. FileControlDDDragEnter(SessionsPageControl, DataObj, KeyState, Point, Effect, Accept);
  5763. }
  5764. //---------------------------------------------------------------------------
  5765. void __fastcall TCustomScpExplorerForm::QueueDDDragEnter(
  5766. _di_IDataObject DataObj, int KeyState,
  5767. const TPoint & Point, int & Effect, bool & Accept)
  5768. {
  5769. FileControlDDDragEnter(QueueView3, DataObj, KeyState, Point, Effect, Accept);
  5770. }
  5771. //---------------------------------------------------------------------------
  5772. void __fastcall TCustomScpExplorerForm::FileControlDDDragLeave(
  5773. TObject * Sender)
  5774. {
  5775. DebugUsedParam(Sender);
  5776. DebugAssert(FDDTargetControl == Sender);
  5777. FDDTargetControl = NULL;
  5778. }
  5779. //---------------------------------------------------------------------------
  5780. void __fastcall TCustomScpExplorerForm::SessionsDDDragLeave()
  5781. {
  5782. FileControlDDDragLeave(SessionsPageControl);
  5783. }
  5784. //---------------------------------------------------------------------------
  5785. void __fastcall TCustomScpExplorerForm::QueueDDDragLeave()
  5786. {
  5787. FileControlDDDragLeave(QueueView3);
  5788. }
  5789. //---------------------------------------------------------------------------
  5790. void __fastcall TCustomScpExplorerForm::AddEditLink(TOperationSide Side, bool Add)
  5791. {
  5792. DebugAssert(GetSide(Side) == osRemote);
  5793. DebugUsedParam(Side);
  5794. bool Edit = false;
  5795. TRemoteFile * File = NULL;
  5796. UnicodeString FileName;
  5797. UnicodeString PointTo;
  5798. bool SymbolicLink = true;
  5799. if (RemoteDirView->ItemFocused)
  5800. {
  5801. DebugAssert(RemoteDirView->ItemFocused->Data);
  5802. File = (TRemoteFile *)RemoteDirView->ItemFocused->Data;
  5803. Edit = !Add && File->IsSymLink && Terminal->SessionData->ResolveSymlinks;
  5804. if (Edit)
  5805. {
  5806. FileName = File->FileName;
  5807. PointTo = File->LinkTo;
  5808. }
  5809. else
  5810. {
  5811. PointTo = File->FileName;
  5812. }
  5813. }
  5814. if (DoSymlinkDialog(FileName, PointTo, osRemote, SymbolicLink, Edit,
  5815. Terminal->IsCapable[fcHardLink]))
  5816. {
  5817. Configuration->Usage->Inc(L"RemoteLinksCreated");
  5818. if (Edit)
  5819. {
  5820. DebugAssert(File->FileName == FileName);
  5821. int Params = dfNoRecursive;
  5822. Terminal->ExceptionOnFail = true;
  5823. try
  5824. {
  5825. Terminal->DeleteFile(L"", File, &Params);
  5826. }
  5827. __finally
  5828. {
  5829. Terminal->ExceptionOnFail = false;
  5830. }
  5831. }
  5832. Terminal->CreateLink(FileName, PointTo, SymbolicLink);
  5833. }
  5834. }
  5835. //---------------------------------------------------------------------------
  5836. bool __fastcall TCustomScpExplorerForm::CanAddEditLink(TOperationSide Side)
  5837. {
  5838. return
  5839. (Terminal != NULL) &&
  5840. ((GetSide(Side) != osRemote) ||
  5841. (Terminal->ResolvingSymlinks &&
  5842. Terminal->IsCapable[fcSymbolicLink]));
  5843. }
  5844. //---------------------------------------------------------------------------
  5845. bool __fastcall TCustomScpExplorerForm::LinkFocused()
  5846. {
  5847. return
  5848. (FCurrentSide == osRemote) &&
  5849. (RemoteDirView->ItemFocused != NULL) &&
  5850. ((TRemoteFile *)RemoteDirView->ItemFocused->Data)->IsSymLink &&
  5851. Terminal->SessionData->ResolveSymlinks;
  5852. }
  5853. //---------------------------------------------------------------------------
  5854. void __fastcall TCustomScpExplorerForm::ExecuteCurrentFile()
  5855. {
  5856. DebugAssert(!WinConfiguration->DisableOpenEdit);
  5857. FForceExecution = true;
  5858. try
  5859. {
  5860. DirView(osCurrent)->ExecuteCurrentFile();
  5861. }
  5862. __finally
  5863. {
  5864. FForceExecution = false;
  5865. }
  5866. }
  5867. //---------------------------------------------------------------------------
  5868. void __fastcall TCustomScpExplorerForm::ExecuteCurrentFileWith(bool OnFocused)
  5869. {
  5870. TEditorData ExternalEditor;
  5871. ExternalEditor.Editor = edExternal;
  5872. bool Remember = false;
  5873. if (DoEditorPreferencesDialog(&ExternalEditor, Remember, epmAdHoc,
  5874. (GetSide(osCurrent) == osRemote)))
  5875. {
  5876. if (Remember)
  5877. {
  5878. TEditorList * EditorList = new TEditorList;
  5879. try
  5880. {
  5881. *EditorList = *WinConfiguration->EditorList;
  5882. bool Found = false;
  5883. int i = 0;
  5884. while (!Found && (i < EditorList->Count))
  5885. {
  5886. const TEditorPreferences * Editor = EditorList->Editors[i];
  5887. if ((Editor->Data->Editor == edExternal) &&
  5888. (Editor->Data->ExternalEditor == ExternalEditor.ExternalEditor))
  5889. {
  5890. Found = true;
  5891. }
  5892. i++;
  5893. }
  5894. if (!Found)
  5895. {
  5896. EditorList->Add(new TEditorPreferences(ExternalEditor));
  5897. WinConfiguration->EditorList = EditorList;
  5898. }
  5899. }
  5900. __finally
  5901. {
  5902. delete EditorList;
  5903. }
  5904. }
  5905. ExecuteFile(osCurrent, efExternalEditor, &ExternalEditor, true, OnFocused);
  5906. }
  5907. }
  5908. //---------------------------------------------------------------------------
  5909. void __fastcall TCustomScpExplorerForm::TerminalRemoved(TObject * Sender)
  5910. {
  5911. FEditorManager->ProcessFiles(FileTerminalRemoved, Sender);
  5912. if (FFileFindTerminal == Sender)
  5913. {
  5914. FFileFindTerminal = NULL;
  5915. HideFileFindDialog();
  5916. }
  5917. if (FClipboardTerminal == Sender)
  5918. {
  5919. ClipboardClear(); // implies ClipboardStop
  5920. }
  5921. }
  5922. //---------------------------------------------------------------------------
  5923. void __fastcall TCustomScpExplorerForm::FileTerminalRemoved(const UnicodeString FileName,
  5924. TEditedFileData * Data, TObject * /*Token*/, void * Arg)
  5925. {
  5926. TTerminal * Terminal = static_cast<TTerminal *>(Arg);
  5927. DebugAssert(Terminal != NULL);
  5928. if (Data->Terminal == Terminal)
  5929. {
  5930. Data->Terminal = NULL;
  5931. }
  5932. }
  5933. //---------------------------------------------------------------------------
  5934. void __fastcall TCustomScpExplorerForm::LastTerminalClosed(TObject * /*Sender*/)
  5935. {
  5936. UpdateControls();
  5937. SessionColor = TColor(0);
  5938. UpdateRemotePathComboBox(false);
  5939. NeedSession(true);
  5940. }
  5941. //---------------------------------------------------------------------------
  5942. void __fastcall TCustomScpExplorerForm::NeedSession(bool ReloadSessions)
  5943. {
  5944. try
  5945. {
  5946. try
  5947. {
  5948. TTerminalManager::Instance()->NewSession(false, L"", ReloadSessions, this);
  5949. }
  5950. __finally
  5951. {
  5952. if (!WinConfiguration->KeepOpenWhenNoSession &&
  5953. (!Terminal || !Terminal->Active))
  5954. {
  5955. TerminateApplication();
  5956. }
  5957. }
  5958. }
  5959. catch (EAbort &)
  5960. {
  5961. // swallow
  5962. // The TTerminalManager does not expect the OnLastTerminalClose to throw without trying to connect.
  5963. // Also when called from TWinControl.UpdateShowing => CMShowingChanged, the showing is aborted on exception
  5964. }
  5965. }
  5966. //---------------------------------------------------------------------------
  5967. void __fastcall TCustomScpExplorerForm::DoTerminalListChanged()
  5968. {
  5969. TStrings * TerminalList = TTerminalManager::Instance()->TerminalList;
  5970. int ActiveTerminalIndex = TTerminalManager::Instance()->ActiveTerminalIndex;
  5971. Configuration->Usage->SetMax(L"MaxOpenedSessions", TerminalList->Count);
  5972. SendMessage(SessionsPageControl->Handle, WM_SETREDRAW, 0, 0);
  5973. try
  5974. {
  5975. while ((SessionsPageControl->PageCount > TerminalList->Count + 1) ||
  5976. // Clear the design time unthemed tab
  5977. ((SessionsPageControl->PageCount > 0) &&
  5978. (dynamic_cast<TThemeTabSheet *>(SessionsPageControl->Pages[SessionsPageControl->PageCount - 1]) == NULL)))
  5979. {
  5980. delete SessionsPageControl->Pages[SessionsPageControl->PageCount - 1];
  5981. }
  5982. for (int Index = 0; Index <= TerminalList->Count; Index++)
  5983. {
  5984. TThemeTabSheet * TabSheet;
  5985. if (Index >= SessionsPageControl->PageCount)
  5986. {
  5987. TabSheet = new TThemeTabSheet(SessionsPageControl);
  5988. TabSheet->PageControl = SessionsPageControl;
  5989. }
  5990. else
  5991. {
  5992. TabSheet = DebugNotNull(dynamic_cast<TThemeTabSheet *>(SessionsPageControl->Pages[Index]));
  5993. }
  5994. bool IsSessionTab = (Index < TerminalList->Count);
  5995. if (IsSessionTab)
  5996. {
  5997. TTerminal * Terminal = dynamic_cast<TTerminal *>(TerminalList->Objects[Index]);
  5998. TabSheet->Tag = reinterpret_cast<int>(Terminal);
  5999. UpdateSessionTab(TabSheet);
  6000. }
  6001. else
  6002. {
  6003. TabSheet->ImageIndex = FNewSessionTabImageIndex;
  6004. TabSheet->Tag = 0; // not really needed
  6005. // We know that we are at the last page, sotherwise we could not call this (it assumes that new session tab is the last one)
  6006. UpdateNewSessionTab();
  6007. }
  6008. TabSheet->ShowCloseButton = IsSessionTab;
  6009. }
  6010. }
  6011. __finally
  6012. {
  6013. SendMessage(SessionsPageControl->Handle, WM_SETREDRAW, 1, 0);
  6014. }
  6015. SessionsPageControl->ActivePageIndex = ActiveTerminalIndex;
  6016. }
  6017. //---------------------------------------------------------------------------
  6018. void __fastcall TCustomScpExplorerForm::TerminalListChanged(TObject * /*Sender*/)
  6019. {
  6020. DoTerminalListChanged();
  6021. }
  6022. //---------------------------------------------------------------------------
  6023. void __fastcall TCustomScpExplorerForm::UpdateNewSessionTab()
  6024. {
  6025. TTabSheet * TabSheet = SessionsPageControl->Pages[SessionsPageControl->PageCount - 1];
  6026. DebugAssert(TabSheet->ImageIndex == 0);
  6027. TabSheet->Caption =
  6028. WinConfiguration->SelectiveToolbarText ?
  6029. StripHotkey(StripTrailingPunctuation(NonVisualDataModule->NewSessionAction->Caption)) :
  6030. UnicodeString();
  6031. }
  6032. //---------------------------------------------------------------------------
  6033. TTerminal * __fastcall TCustomScpExplorerForm::GetSessionTabTerminal(TTabSheet * TabSheet)
  6034. {
  6035. return reinterpret_cast<TTerminal *>(TabSheet->Tag);
  6036. }
  6037. //---------------------------------------------------------------------------
  6038. void __fastcall TCustomScpExplorerForm::UpdateSessionTab(TTabSheet * TabSheet)
  6039. {
  6040. if (DebugAlwaysTrue(TabSheet != NULL))
  6041. {
  6042. TManagedTerminal * ManagedTerminal =
  6043. dynamic_cast<TManagedTerminal *>(GetSessionTabTerminal(TabSheet));
  6044. if (DebugAlwaysTrue(ManagedTerminal != NULL))
  6045. {
  6046. TColor Color = (ManagedTerminal == FTerminal) ? FSessionColor : ManagedTerminal->StateData->Color;
  6047. TabSheet->ImageIndex = AddSessionColor(Color);
  6048. UnicodeString TabCaption = TTerminalManager::Instance()->GetTerminalTitle(ManagedTerminal, true);
  6049. TabSheet->Caption = SessionsPageControl->FormatCaptionWithCloseButton(TabCaption);
  6050. TThemeTabSheet * ThemeTabSheet = dynamic_cast<TThemeTabSheet *>(TabSheet);
  6051. if (DebugAlwaysTrue(ThemeTabSheet != NULL))
  6052. {
  6053. ThemeTabSheet->Shadowed = !ManagedTerminal->Active;
  6054. }
  6055. }
  6056. }
  6057. }
  6058. //---------------------------------------------------------------------------
  6059. bool __fastcall TCustomScpExplorerForm::SessionTabSwitched()
  6060. {
  6061. DebugAssert(SessionsPageControl->ActivePage != NULL);
  6062. TTerminal * Terminal = GetSessionTabTerminal(SessionsPageControl->ActivePage);
  6063. bool Result = (Terminal != NULL);
  6064. if (Result)
  6065. {
  6066. TTerminalManager::Instance()->ActiveTerminal = Terminal;
  6067. }
  6068. else
  6069. {
  6070. try
  6071. {
  6072. NewSession(false);
  6073. }
  6074. __finally
  6075. {
  6076. DoTerminalListChanged();
  6077. }
  6078. FSessionsPageControlNewSessionTime = Now();
  6079. }
  6080. return Result;
  6081. }
  6082. //---------------------------------------------------------------------------
  6083. void __fastcall TCustomScpExplorerForm::SessionsPageControlChange(TObject * /*Sender*/)
  6084. {
  6085. SessionTabSwitched();
  6086. }
  6087. //---------------------------------------------------------------------------
  6088. void __fastcall TCustomScpExplorerForm::TransferListChange(TObject * Sender)
  6089. {
  6090. TTBXStringList * TransferList = dynamic_cast<TTBXStringList *>(Sender);
  6091. DebugAssert(TransferList != NULL);
  6092. UnicodeString Name;
  6093. if (TransferList->ItemIndex <= 0)
  6094. {
  6095. Name = L"";
  6096. }
  6097. else
  6098. {
  6099. Name = GUIConfiguration->CopyParamList->Names[TransferList->ItemIndex - 1];
  6100. }
  6101. if (FCopyParamAutoSelected.IsEmpty())
  6102. {
  6103. // if previous preset was not autoselected, make new preset the "default"
  6104. FCopyParamDefault = Name;
  6105. }
  6106. GUIConfiguration->CopyParamCurrent = Name;
  6107. }
  6108. //---------------------------------------------------------------------------
  6109. void __fastcall TCustomScpExplorerForm::UpdateTransferLabel()
  6110. {
  6111. // sanity check
  6112. bool ExistingPreset =
  6113. (FTransferListHoverIndex >= 0) &&
  6114. (FTransferListHoverIndex < 1 + GUIConfiguration->CopyParamList->Count);
  6115. DebugAssert(ExistingPreset);
  6116. if (ExistingPreset)
  6117. {
  6118. HDC DC = GetDC(0);
  6119. TCanvas * Canvas = new TCanvas();
  6120. try
  6121. {
  6122. Canvas->Handle = DC;
  6123. Canvas->Font = GetToolbarFont(this);
  6124. UnicodeString Name;
  6125. if (FTransferListHoverIndex == 0)
  6126. {
  6127. Name = L"";
  6128. }
  6129. else
  6130. {
  6131. Name = GUIConfiguration->CopyParamList->Names[FTransferListHoverIndex - 1];
  6132. }
  6133. TTBXLabelItem * TransferLabel = dynamic_cast<TTBXLabelItem*>(
  6134. static_cast<TComponent*>(GetComponent(fcTransferLabel)));
  6135. TTBXStringList * TransferList = dynamic_cast<TTBXStringList*>(
  6136. static_cast<TObject*>(GetComponent(fcTransferList)));
  6137. UnicodeString InfoStr =
  6138. GUIConfiguration->CopyParamPreset[Name].
  6139. GetInfoStr(L"; ",
  6140. FLAGMASK(((Terminal != NULL) && Terminal->Active), Terminal->UsableCopyParamAttrs(0).General));
  6141. int MaxWidth = TransferList->MinWidth - (2 * TransferLabel->Margin) - ScaleByTextHeight(this, 10);
  6142. if (Canvas->TextExtent(InfoStr).cx > MaxWidth)
  6143. {
  6144. while (Canvas->TextExtent(InfoStr + Ellipsis).cx > MaxWidth)
  6145. {
  6146. InfoStr.SetLength(InfoStr.Length() - 1);
  6147. }
  6148. InfoStr += Ellipsis;
  6149. }
  6150. // UpdateCaption does not cause invalidation of whole submenu, while
  6151. // setting Caption property does.
  6152. // also it probably does not resize the label, even if necessary
  6153. // (we do not want that anyway)
  6154. TransferLabel->UpdateCaption(InfoStr);
  6155. }
  6156. __finally
  6157. {
  6158. Canvas->Handle = NULL;
  6159. ReleaseDC(0, DC);
  6160. delete Canvas;
  6161. }
  6162. }
  6163. }
  6164. //---------------------------------------------------------------------------
  6165. void __fastcall TCustomScpExplorerForm::TransferListDrawItem(
  6166. TTBXCustomList * /*Sender*/, TCanvas * /*ACanvas*/, const TRect & /*ARect*/,
  6167. int /*AIndex*/, int AHoverIndex, bool & /*DrawDefault*/)
  6168. {
  6169. if (FTransferListHoverIndex != AHoverIndex)
  6170. {
  6171. FTransferListHoverIndex = AHoverIndex;
  6172. UpdateTransferLabel();
  6173. }
  6174. }
  6175. //---------------------------------------------------------------------------
  6176. void __fastcall TCustomScpExplorerForm::WMAppCommand(TMessage & Message)
  6177. {
  6178. int Command = GET_APPCOMMAND_LPARAM(Message.LParam);
  6179. TShiftState Shift = KeyDataToShiftState(GET_KEYSTATE_LPARAM(Message.LParam));
  6180. if ((Shift * (TShiftState() << ssShift << ssAlt << ssCtrl)).Empty())
  6181. {
  6182. if (Command == APPCOMMAND_BROWSER_FAVORITES)
  6183. {
  6184. OpenDirectory(GetSide(osCurrent));
  6185. Message.Result = 1;
  6186. }
  6187. else
  6188. {
  6189. TForm::Dispatch(&Message);
  6190. }
  6191. }
  6192. else
  6193. {
  6194. TForm::Dispatch(&Message);
  6195. }
  6196. }
  6197. //---------------------------------------------------------------------------
  6198. void __fastcall TCustomScpExplorerForm::CMDialogChar(TMessage & AMessage)
  6199. {
  6200. TCMDialogChar & Message = reinterpret_cast<TCMDialogChar &>(AMessage);
  6201. if ((FIgnoreNextDialogChar != 0) &&
  6202. (toupper(Message.CharCode) == toupper(FIgnoreNextDialogChar)))
  6203. {
  6204. Message.Result = 1;
  6205. }
  6206. else
  6207. {
  6208. TForm::Dispatch(&Message);
  6209. }
  6210. FIgnoreNextDialogChar = 0;
  6211. }
  6212. //---------------------------------------------------------------------------
  6213. void __fastcall TCustomScpExplorerForm::WMSysCommand(TMessage & Message)
  6214. {
  6215. // The four low-order bits are used internally by Windows
  6216. unsigned int Cmd = (Message.WParam & 0xFFF0);
  6217. // SC_RESTORE, SC_MAXIMIZE, SC_MINIMIZE - buttons on windows title
  6218. // SC_DEFAULT - double click on windows title (does not work, at least on WinXP)
  6219. // 61730 - restore thru double click - undocumented
  6220. // 61490 - maximize thru double click - undocumented
  6221. if ((Cmd == SC_RESTORE) || (Cmd == SC_MAXIMIZE) ||
  6222. (Cmd == SC_MINIMIZE) || (Cmd == SC_DEFAULT))
  6223. {
  6224. SysResizing(Cmd);
  6225. }
  6226. TForm::Dispatch(&Message);
  6227. }
  6228. //---------------------------------------------------------------------------
  6229. void __fastcall TCustomScpExplorerForm::WMQueryEndSession(TMessage & Message)
  6230. {
  6231. // We were actually never able to make ENDSESSION_CRITICAL happen.
  6232. // Also there no point returning TRUE as we are not able to
  6233. // handle the abrupt termination caused by subsequent WM_ENDSESSION cleanly.
  6234. // Hence the process termination might be safer :)
  6235. if ((Message.LParam != ENDSESSION_CRITICAL) &&
  6236. (((FQueue != NULL) && !FQueue->IsEmpty) || (FProgressForm != NULL)))
  6237. {
  6238. Message.Result = FALSE;
  6239. }
  6240. else
  6241. {
  6242. Message.Result = TRUE;
  6243. }
  6244. // Do not call default handling as that triggers OnCloseQuery,
  6245. // where our implementation will popup confirmation dialogs, what we do not want,
  6246. // as per Vista guidelines:
  6247. // https://docs.microsoft.com/en-us/windows/desktop/shutdown/shutting-down
  6248. }
  6249. //---------------------------------------------------------------------------
  6250. void __fastcall TCustomScpExplorerForm::WMEndSession(TWMEndSession & Message)
  6251. {
  6252. if (Message.EndSession && IsApplicationMinimized())
  6253. {
  6254. // WORKAROUND
  6255. // TApplication.WndProc() calls Application.Terminate() before Halt(),
  6256. // when it receives WM_ENDSESSION,
  6257. // but that sometimes (particularly when application is minimized) cause crashes.
  6258. // Crash popups message that blocks log off.
  6259. // Obviously application cannot shutdown cleanly after WM_ENDSESSION,
  6260. // so we call ExitProcess() immediately, not even trying to cleanup.
  6261. // It still causes beep, so there's likely some popup, but it does not block
  6262. // log off.
  6263. ExitProcess(0);
  6264. }
  6265. TForm::Dispatch(&Message);
  6266. }
  6267. #ifdef _DEBUG
  6268. //---------------------------------------------------------------------------
  6269. void __fastcall TCustomScpExplorerForm::WMWindowPosChanged(TWMWindowPosMsg & Message)
  6270. {
  6271. TForm::Dispatch(&Message);
  6272. }
  6273. #endif
  6274. //---------------------------------------------------------------------------
  6275. void __fastcall TCustomScpExplorerForm::SysResizing(unsigned int /*Cmd*/)
  6276. {
  6277. }
  6278. //---------------------------------------------------------------------------
  6279. void __fastcall TCustomScpExplorerForm::DoShow()
  6280. {
  6281. // only now are the controls resized finally, so the size constraints
  6282. // will not conflict with possibly very small window size
  6283. RestoreFormParams();
  6284. FixControlsPlacement();
  6285. if (Position == poDefaultPosOnly)
  6286. {
  6287. CutFormToDesktop(this);
  6288. }
  6289. TForm::DoShow();
  6290. FSessionsDragDropFilesEx->DragDropControl = SessionsPageControl;
  6291. FQueueDragDropFilesEx->DragDropControl = QueueView3;
  6292. if (Terminal == NULL)
  6293. {
  6294. StartingDisconnected();
  6295. }
  6296. UpdatePixelsPerInchMainWindowCounter();
  6297. FShowing = true;
  6298. }
  6299. //---------------------------------------------------------------------------
  6300. void __fastcall TCustomScpExplorerForm::UpdatePixelsPerInchMainWindowCounter()
  6301. {
  6302. Configuration->Usage->Set(L"PixelsPerInchMainWindow", PixelsPerInch);
  6303. }
  6304. //---------------------------------------------------------------------------
  6305. void __fastcall TCustomScpExplorerForm::StartingDisconnected()
  6306. {
  6307. DoTerminalListChanged();
  6308. }
  6309. //---------------------------------------------------------------------------
  6310. void __fastcall TCustomScpExplorerForm::PopupTrayBalloon(TTerminal * Terminal,
  6311. const UnicodeString & Str, TQueryType Type, Exception * E, unsigned int Seconds,
  6312. TNotifyEvent OnBalloonClick, TObject * UserData)
  6313. {
  6314. bool Do;
  6315. UnicodeString Message;
  6316. if (E == NULL)
  6317. {
  6318. Message = Str;
  6319. Do = true;
  6320. }
  6321. else
  6322. {
  6323. Do = ExceptionMessage(E, Message);
  6324. }
  6325. if (Do && WinConfiguration->BalloonNotifications)
  6326. {
  6327. UnicodeString Title;
  6328. if ((Terminal == NULL) && (Type == qtInformation) && ExtractMainInstructions(Message, Title))
  6329. {
  6330. Message = TrimLeft(Message);
  6331. }
  6332. else
  6333. {
  6334. Message = UnformatMessage(Message);
  6335. const ResourceString * Captions[] = { &_SMsgDlgConfirm, &_SMsgDlgWarning,
  6336. &_SMsgDlgError, &_SMsgDlgInformation, NULL };
  6337. Title = LoadResourceString(Captions[Type]);
  6338. if (Terminal != NULL)
  6339. {
  6340. Title = FORMAT(L"%s - %s",
  6341. (TTerminalManager::Instance()->GetTerminalTitle(Terminal, true), Title));
  6342. }
  6343. }
  6344. if (Seconds == 0)
  6345. {
  6346. Seconds = WinConfiguration->NotificationsTimeout;
  6347. }
  6348. FTrayIcon->PopupBalloon(Title, Message, Type, Seconds * MSecsPerSec, OnBalloonClick, UserData);
  6349. }
  6350. }
  6351. //---------------------------------------------------------------------------
  6352. unsigned int __fastcall TCustomScpExplorerForm::MoreMessageDialog(const UnicodeString Message,
  6353. TStrings * MoreMessages, TQueryType Type, unsigned int Answers,
  6354. UnicodeString HelpKeyword, const TMessageParams * Params,
  6355. TTerminal * Terminal)
  6356. {
  6357. if (((WinConfiguration->ContinueOnError && (FErrorList != NULL)) ||
  6358. (FOnFeedSynchronizeError != NULL)) &&
  6359. (Params != NULL) && (Params->Params & mpAllowContinueOnError) )
  6360. {
  6361. if (FOnFeedSynchronizeError != NULL)
  6362. {
  6363. FOnFeedSynchronizeError(Message, MoreMessages, Type, HelpKeyword);
  6364. }
  6365. else
  6366. {
  6367. DebugAssert(FErrorList != NULL);
  6368. TStringList * MoreMessagesCopy = NULL;
  6369. if (MoreMessages)
  6370. {
  6371. MoreMessagesCopy = new TStringList();
  6372. MoreMessagesCopy->Assign(MoreMessages);
  6373. }
  6374. FErrorList->AddObject(Message, MoreMessagesCopy);
  6375. }
  6376. PopupTrayBalloon(Terminal, Message, Type);
  6377. return ContinueAnswer(Answers);
  6378. }
  6379. else
  6380. {
  6381. bool UseBalloon = FTrayIcon->Visible;
  6382. if (UseBalloon)
  6383. {
  6384. PopupTrayBalloon(Terminal, Message, Type);
  6385. }
  6386. unsigned int Result;
  6387. try
  6388. {
  6389. Result = ::MoreMessageDialog(Message, MoreMessages, Type, Answers, HelpKeyword, Params);
  6390. }
  6391. __finally
  6392. {
  6393. // cancel only balloon we popped up, otherwise we may cancel notification
  6394. // balloon that was there before the message dialog
  6395. // (such as "dd confirmation opt in balloon)
  6396. if (UseBalloon)
  6397. {
  6398. FTrayIcon->CancelBalloon();
  6399. }
  6400. }
  6401. return Result;
  6402. }
  6403. }
  6404. //---------------------------------------------------------------------------
  6405. void __fastcall TCustomScpExplorerForm::ShowExtendedException(
  6406. TTerminal * Terminal, Exception * E)
  6407. {
  6408. if (FTrayIcon->Visible)
  6409. {
  6410. PopupTrayBalloon(Terminal, L"", qtError, E);
  6411. }
  6412. // particularly prevent opening new session from jump list,
  6413. // while exception is shown
  6414. NonVisualDataModule->StartBusy();
  6415. try
  6416. {
  6417. ShowExtendedExceptionEx(Terminal, E);
  6418. }
  6419. __finally
  6420. {
  6421. NonVisualDataModule->EndBusy();
  6422. FTrayIcon->CancelBalloon();
  6423. }
  6424. }
  6425. //---------------------------------------------------------------------------
  6426. void __fastcall TCustomScpExplorerForm::TerminalReady()
  6427. {
  6428. // cannot rely on active page being page for active terminal,
  6429. // as it can happen that active page is the "new session" page
  6430. // (e.g. when reconnecting active terminal, while login dialog
  6431. // invoked from "new session" page is modal)
  6432. int ActiveTerminalIndex = TTerminalManager::Instance()->ActiveTerminalIndex;
  6433. UpdateSessionTab(SessionsPageControl->Pages[ActiveTerminalIndex]);
  6434. }
  6435. //---------------------------------------------------------------------------
  6436. void __fastcall TCustomScpExplorerForm::InactiveTerminalException(
  6437. TTerminal * Terminal, Exception * E)
  6438. {
  6439. Notify(Terminal, L"", qtError, false, NULL, NULL, E);
  6440. if (!Terminal->Active)
  6441. {
  6442. int Index = TTerminalManager::Instance()->IndexOf(Terminal);
  6443. if (DebugAlwaysTrue((Index >= 0) && (Index < SessionsPageControl->PageCount)))
  6444. {
  6445. UpdateSessionTab(SessionsPageControl->Pages[Index]);
  6446. }
  6447. }
  6448. }
  6449. //---------------------------------------------------------------------------
  6450. void __fastcall TCustomScpExplorerForm::Notify(TTerminal * Terminal,
  6451. UnicodeString Message, TQueryType Type,
  6452. bool Important, TNotifyEvent OnClick, TObject * UserData, Exception * E)
  6453. {
  6454. if ((E == NULL) ||
  6455. ExceptionMessage(E, Message))
  6456. {
  6457. unsigned int Seconds = WinConfiguration->NotificationsTimeout;
  6458. if (Important)
  6459. {
  6460. Seconds *= 5;
  6461. }
  6462. UnicodeString NoteMessage(UnformatMessage(Message));
  6463. if (Terminal != NULL)
  6464. {
  6465. NoteMessage = FORMAT(L"%s: %s",
  6466. (TTerminalManager::Instance()->GetTerminalTitle(Terminal, true), NoteMessage));
  6467. }
  6468. if (WinConfiguration->BalloonNotifications)
  6469. {
  6470. AddNote(NoteMessage);
  6471. PopupTrayBalloon(Terminal, Message, Type, NULL, Seconds, OnClick, UserData);
  6472. }
  6473. else
  6474. {
  6475. FlashOnBackground();
  6476. PostNote(NoteMessage, Seconds, OnClick, UserData);
  6477. }
  6478. }
  6479. }
  6480. //---------------------------------------------------------------------------
  6481. void __fastcall TCustomScpExplorerForm::QueueEmptyNoteClicked(TObject * Sender)
  6482. {
  6483. RestoreApp();
  6484. TTerminalNoteData * TerminalNoteData = dynamic_cast<TTerminalNoteData *>(Sender);
  6485. if (DebugAlwaysTrue(TerminalNoteData != NULL) &&
  6486. !NonVisualDataModule->Busy)
  6487. {
  6488. TTerminal * Terminal = TerminalNoteData->Terminal;
  6489. TTerminalManager::Instance()->ActiveTerminal = Terminal;
  6490. if (!ComponentVisible[fcQueueView])
  6491. {
  6492. ToggleQueueVisibility();
  6493. GoToQueue();
  6494. }
  6495. }
  6496. }
  6497. //---------------------------------------------------------------------------
  6498. void __fastcall TCustomScpExplorerForm::QueueEvent(TTerminal * ATerminal,
  6499. TTerminalQueue * /*Queue*/, TQueueEvent Event)
  6500. {
  6501. TManagedTerminal * ManagedTerminal = DebugNotNull(dynamic_cast<TManagedTerminal *>(ATerminal));
  6502. UnicodeString Message;
  6503. TNotifyEvent OnClick = NULL;
  6504. TObject * UserData = NULL;
  6505. bool QueueInvisible = !ComponentVisible[fcQueueView] || IsApplicationMinimized();
  6506. switch (Event)
  6507. {
  6508. case qeEmptyButMonitored:
  6509. if ((ATerminal != Terminal) || QueueInvisible)
  6510. {
  6511. Message = LoadStr(BALLOON_QUEUE_EMPTY);
  6512. OnClick = QueueEmptyNoteClicked;
  6513. TTerminalNoteData * TerminalNoteData = new TTerminalNoteData();
  6514. TerminalNoteData->Terminal = ATerminal;
  6515. UserData = TerminalNoteData;
  6516. }
  6517. break;
  6518. case qeEmpty:
  6519. OperationComplete(ManagedTerminal->QueueOperationStart);
  6520. break;
  6521. case qePendingUserAction:
  6522. if ((ATerminal != Terminal) ||
  6523. (QueueInvisible && !IsQueueAutoPopup()))
  6524. {
  6525. Message = LoadStr(BALLOON_QUEUE_USER_ACTION);
  6526. }
  6527. break;
  6528. default:
  6529. DebugFail();
  6530. }
  6531. if (!Message.IsEmpty())
  6532. {
  6533. Notify(ATerminal, Message, qtInformation, false, OnClick, UserData);
  6534. }
  6535. }
  6536. //---------------------------------------------------------------------------
  6537. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDCreateDragFileList(
  6538. TObject * /*Sender*/, TFileList * FileList, bool & Created)
  6539. {
  6540. if (FDDExtMapFile != NULL)
  6541. {
  6542. CloseHandle(FDDExtMapFile);
  6543. FDDExtMapFile = NULL;
  6544. }
  6545. if (WinConfiguration->DDFakeFile)
  6546. {
  6547. DDFakeFileInitDrag(FileList, Created);
  6548. }
  6549. }
  6550. //---------------------------------------------------------------------------
  6551. void __fastcall TCustomScpExplorerForm::DDFakeCreated(TObject * /*Sender*/, const UnicodeString FileName)
  6552. {
  6553. if (DebugAlwaysTrue(!FDragFakeDirectory.IsEmpty()) &&
  6554. SameText(ExtractFileName(FileName), ExtractFileName(FDragFakeDirectory)))
  6555. {
  6556. FFakeFileDropTarget = FileName;
  6557. }
  6558. }
  6559. //---------------------------------------------------------------------------
  6560. UnicodeString __fastcall TCustomScpExplorerForm::CreateFakeTransferDirectory()
  6561. {
  6562. UnicodeString Result = ExcludeTrailingBackslash(WinConfiguration->TemporaryDir());
  6563. if (!ForceDirectories(ApiPath(Result)))
  6564. {
  6565. throw Exception(FMTLOAD(CREATE_TEMP_DIR_ERROR, (Result)));
  6566. }
  6567. FileSetAttr(ApiPath(Result), faHidden);
  6568. return Result;
  6569. }
  6570. //---------------------------------------------------------------------------
  6571. void __fastcall TCustomScpExplorerForm::DDFakeFileInitDrag(TFileList * FileList,
  6572. bool & Created)
  6573. {
  6574. FFakeFileDropTarget = UnicodeString();
  6575. FDragFakeDirectory = CreateFakeTransferDirectory();
  6576. FileList->AddItem(NULL, FDragFakeDirectory);
  6577. Created = true;
  6578. if (!WinConfiguration->IsDDExtRunning() || WinConfiguration->IsDDExtBroken())
  6579. {
  6580. FDragFakeMonitors = StartCreationDirectoryMonitorsOnEachDrive(FILE_NOTIFY_CHANGE_DIR_NAME, DDFakeCreated);
  6581. }
  6582. FDDExtMapFile = CreateFileMappingA((HANDLE)0xFFFFFFFF, NULL, PAGE_READWRITE,
  6583. 0, sizeof(TDragExtCommStruct), AnsiString(DRAG_EXT_MAPPING).c_str());
  6584. {
  6585. TMutexGuard Guard(FDDExtMutex, DRAGEXT_MUTEX_RELEASE_TIMEOUT);
  6586. TDragExtCommStruct* CommStruct;
  6587. CommStruct = static_cast<TDragExtCommStruct*>(MapViewOfFile(FDDExtMapFile,
  6588. FILE_MAP_ALL_ACCESS, 0, 0, 0));
  6589. DebugAssert(CommStruct != NULL);
  6590. CommStruct->Version = TDragExtCommStruct::CurrentVersion;
  6591. CommStruct->Dragging = true;
  6592. wcsncpy(CommStruct->DropDest, FDragFakeDirectory.c_str(),
  6593. LENOF(CommStruct->DropDest));
  6594. NULL_TERMINATE(CommStruct->DropDest);
  6595. UnmapViewOfFile(CommStruct);
  6596. }
  6597. }
  6598. //---------------------------------------------------------------------------
  6599. bool __fastcall TCustomScpExplorerForm::RemoteFileControlFileOperation(
  6600. TObject * Sender, TFileOperation Operation, bool NoConfirmation, void * Param)
  6601. {
  6602. bool Result;
  6603. if (Sender == RemoteDirView)
  6604. {
  6605. Result = ExecuteFileOperation(Operation, osRemote, true, NoConfirmation, Param);
  6606. }
  6607. else
  6608. {
  6609. DebugAssert(Sender == RemoteDriveView);
  6610. TStrings * FileList = RemoteDriveView->DragFileList();
  6611. try
  6612. {
  6613. Result = ExecuteFileOperation(Operation, osRemote, FileList, NoConfirmation, Param);
  6614. }
  6615. __finally
  6616. {
  6617. delete FileList;
  6618. }
  6619. }
  6620. if (FDDTargetControl == RemoteDriveView)
  6621. {
  6622. RemoteDriveView->UpdateDropTarget();
  6623. }
  6624. // foRemoteMove happens when file/dir is dragged within the remote tree view
  6625. // or from tree view to dir view.
  6626. // foMove happens when file/dir is dragged from remote tree view outside of
  6627. // application via dragex
  6628. if (((Operation == foRemoteMove) || (Operation == foMove)) &&
  6629. (DropSourceControl == RemoteDriveView))
  6630. {
  6631. RemoteDriveView->UpdateDropSource();
  6632. }
  6633. return Result;
  6634. }
  6635. //---------------------------------------------------------------------------
  6636. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDEnd(TObject * Sender)
  6637. {
  6638. // This also handles drops of remote files to queue.
  6639. // Drops of local files (uploads) are handled in QueueDDProcessDropped.
  6640. SAFE_DESTROY(FDDFileList);
  6641. if (!FFakeFileDropTarget.IsEmpty())
  6642. {
  6643. RemoveDir(ApiPath(FFakeFileDropTarget));
  6644. }
  6645. if ((FDDExtMapFile != NULL) || (FDDTargetControl == QueueView3))
  6646. {
  6647. try
  6648. {
  6649. TDragResult DDResult = (Sender == RemoteDirView) ?
  6650. RemoteDirView->LastDDResult : RemoteDriveView->LastDDResult;
  6651. // Focus is moved to the target application,
  6652. // but as we are going to present the UI, we need to steal the focus back.
  6653. // This most likely won't work though (windows does not allow application
  6654. // to steal focus most of the time)
  6655. Application->BringToFront();
  6656. // On older version of Windows we never got drMove here, see also comment below.
  6657. // On Windows 10, we get the "move".
  6658. if ((DDResult == drCopy) || (DDResult == drMove) || (DDResult == drInvalid))
  6659. {
  6660. UnicodeString TargetDirectory;
  6661. TFileOperation Operation;
  6662. // drInvalid may mean drMove, see comment below
  6663. switch (DDResult)
  6664. {
  6665. case drCopy:
  6666. Operation = foCopy;
  6667. break;
  6668. case drMove:
  6669. Operation = foMove;
  6670. break;
  6671. default:
  6672. DebugFail();
  6673. case drInvalid:
  6674. // prefer "copy" for safety
  6675. Operation = FLAGSET(FLastDropEffect, DROPEFFECT_MOVE) ? foMove : foCopy;
  6676. break;
  6677. }
  6678. TTransferOperationParam Param;
  6679. UnicodeString CounterName;
  6680. bool ForceQueue;
  6681. if (!DDGetTarget(Param.TargetDirectory, ForceQueue, CounterName))
  6682. {
  6683. // we get drInvalid both if d&d was intercepted by ddext,
  6684. // and when users drops on no-drop location.
  6685. // we tell the difference by existence of response from ddext,
  6686. // so we ignore absence of response in this case
  6687. if (DDResult != drInvalid)
  6688. {
  6689. // here we know that the extension is installed,
  6690. // as it is checked as soon as drag&drop starts from
  6691. // RemoteFileControlDDCreateDragFileList
  6692. Configuration->Usage->Inc(L"DownloadsDragDropExternalExtTargetUnknown");
  6693. if (!WinConfiguration->DDExtInstalled || WinConfiguration->IsDDExtBroken())
  6694. {
  6695. throw ExtException(NULL, LoadStr(DD_TARGET_UNKNOWN), HELP_DD_TARGET_UNKNOWN);
  6696. }
  6697. else
  6698. {
  6699. throw ExtException(NULL, LoadStr(DRAGEXT_TARGET_UNKNOWN2), HELP_DRAGEXT_TARGET_UNKNOWN);
  6700. }
  6701. }
  6702. }
  6703. else
  6704. {
  6705. // download using fake file
  6706. Param.Temp = false;
  6707. Param.DragDrop = true;
  6708. if (ForceQueue)
  6709. {
  6710. Param.Queue = asOn;
  6711. }
  6712. if (Sender == RemoteDirView)
  6713. {
  6714. Param.Options = FLAGMASK(SelectedAllFilesInDirView(RemoteDirView), coAllFiles);
  6715. }
  6716. if (RemoteFileControlFileOperation(Sender, Operation,
  6717. (WinConfiguration->DDTransferConfirmation == asOff), &Param))
  6718. {
  6719. Configuration->Usage->Inc(CounterName);
  6720. }
  6721. }
  6722. }
  6723. }
  6724. __finally
  6725. {
  6726. CloseHandle(FDDExtMapFile);
  6727. FDDExtMapFile = NULL;
  6728. delete FDragFakeMonitors;
  6729. FDragFakeMonitors = NULL;
  6730. RemoveDir(ApiPath(FDragFakeDirectory));
  6731. FDragFakeDirectory = L"";
  6732. FFakeFileDropTarget = UnicodeString();
  6733. }
  6734. }
  6735. if (FDragDropSshTerminate.get() != NULL)
  6736. {
  6737. FDragDropSshTerminate->Rethrow();
  6738. }
  6739. }
  6740. //---------------------------------------------------------------------------
  6741. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDGiveFeedback(
  6742. TObject * /*Sender*/, int dwEffect, HRESULT & /*Result*/)
  6743. {
  6744. // Remember drop effect so we know (when user drops files), if we copy or move
  6745. FLastDropEffect = dwEffect;
  6746. }
  6747. //---------------------------------------------------------------------------
  6748. bool __fastcall TCustomScpExplorerForm::DDGetTarget(
  6749. UnicodeString & Directory, bool & ForceQueue, UnicodeString & CounterName)
  6750. {
  6751. bool Result;
  6752. if (FDDTargetControl == QueueView3)
  6753. {
  6754. Directory = DefaultDownloadTargetDirectory();
  6755. Result = true;
  6756. CounterName = L"DownloadsDragDropQueue";
  6757. ForceQueue = true;
  6758. }
  6759. else if (!FFakeFileDropTarget.IsEmpty())
  6760. {
  6761. Directory = ExcludeTrailingBackslash(ExtractFilePath(FFakeFileDropTarget));
  6762. Result = true;
  6763. ForceQueue = false;
  6764. CounterName = L"DownloadsDragDropFakeFile";
  6765. }
  6766. else
  6767. {
  6768. ForceQueue = false;
  6769. Enabled = false;
  6770. try
  6771. {
  6772. int Timer = 0;
  6773. Result = false;
  6774. while (!Result && (Timer < WinConfiguration->DDExtTimeout))
  6775. {
  6776. {
  6777. TMutexGuard Guard(FDDExtMutex, DRAGEXT_MUTEX_RELEASE_TIMEOUT);
  6778. TDragExtCommStruct* CommStruct;
  6779. CommStruct = static_cast<TDragExtCommStruct*>(MapViewOfFile(FDDExtMapFile,
  6780. FILE_MAP_ALL_ACCESS, 0, 0, 0));
  6781. DebugAssert(CommStruct != NULL);
  6782. Result = !CommStruct->Dragging;
  6783. if (Result)
  6784. {
  6785. Directory = ExtractFilePath(CommStruct->DropDest);
  6786. CounterName = L"DownloadsDragDropExternalExt";
  6787. }
  6788. UnmapViewOfFile(CommStruct);
  6789. }
  6790. if (!Result)
  6791. {
  6792. Sleep(50);
  6793. Timer += 50;
  6794. Application->ProcessMessages();
  6795. }
  6796. }
  6797. }
  6798. __finally
  6799. {
  6800. Enabled = true;
  6801. }
  6802. }
  6803. return Result;
  6804. }
  6805. //---------------------------------------------------------------------------
  6806. void __fastcall TCustomScpExplorerForm::AddDelayedDirectoryDeletion(
  6807. const UnicodeString TempDir, int SecDelay)
  6808. {
  6809. TDateTime Alarm = Now() + (double)((double)SecDelay*OneMillisecond);
  6810. FDelayedDeletionList->AddObject(TempDir, reinterpret_cast<TObject*>(Alarm.FileDate()));
  6811. if (FDelayedDeletionTimer == NULL)
  6812. {
  6813. DebugAssert(HandleAllocated());
  6814. FDelayedDeletionTimer = new TTimer(this);
  6815. FDelayedDeletionTimer->Interval = 10000;
  6816. FDelayedDeletionTimer->OnTimer = DoDelayedDeletion;
  6817. }
  6818. else
  6819. {
  6820. FDelayedDeletionTimer->Enabled = true;
  6821. }
  6822. }
  6823. //---------------------------------------------------------------------------
  6824. void __fastcall TCustomScpExplorerForm::DoDelayedDeletion(TObject * Sender)
  6825. {
  6826. DebugAssert(FDelayedDeletionList != NULL);
  6827. TDateTime N = Now();
  6828. TDateTime Alert;
  6829. UnicodeString Directory;
  6830. for (int Index = FDelayedDeletionList->Count-1; Index >= 0; Index--)
  6831. {
  6832. Alert = FileDateToDateTime(reinterpret_cast<int>(FDelayedDeletionList->Objects[Index]));
  6833. if ((N >= Alert) || (Sender == NULL))
  6834. {
  6835. Directory = FDelayedDeletionList->Strings[Index];
  6836. if (DeleteDirectory(ExcludeTrailingBackslash(Directory)))
  6837. {
  6838. FDelayedDeletionList->Delete(Index);
  6839. }
  6840. }
  6841. }
  6842. if (FDelayedDeletionList->Count == 0)
  6843. {
  6844. FDelayedDeletionTimer->Enabled = false;
  6845. }
  6846. }
  6847. //---------------------------------------------------------------------------
  6848. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDTargetDrop()
  6849. {
  6850. if (IsFileControl(FDDTargetControl, osRemote) ||
  6851. (FDDTargetControl == SessionsPageControl))
  6852. {
  6853. TTerminal * TargetTerminal = NULL;
  6854. TFileOperation Operation = foNone;
  6855. if (FDDTargetControl == SessionsPageControl)
  6856. {
  6857. TPoint Point = SessionsPageControl->ScreenToClient(Mouse->CursorPos);
  6858. int Index = SessionsPageControl->IndexOfTabAt(Point.X, Point.Y);
  6859. // do not allow dropping on the "+" tab
  6860. TargetTerminal = GetSessionTabTerminal(SessionsPageControl->Pages[Index]);
  6861. if (TargetTerminal != NULL)
  6862. {
  6863. if ((FLastDropEffect == DROPEFFECT_MOVE) &&
  6864. (TargetTerminal == TTerminalManager::Instance()->ActiveTerminal))
  6865. {
  6866. Operation = foRemoteMove;
  6867. }
  6868. else
  6869. {
  6870. Operation = foRemoteCopy;
  6871. }
  6872. }
  6873. }
  6874. else
  6875. {
  6876. if (FLastDropEffect == DROPEFFECT_MOVE)
  6877. {
  6878. Operation = foRemoteMove;
  6879. }
  6880. else if (FLastDropEffect == DROPEFFECT_COPY)
  6881. {
  6882. Operation = foRemoteCopy;
  6883. }
  6884. }
  6885. if (Operation != foNone)
  6886. {
  6887. RemoteFileControlFileOperation(DropSourceControl,
  6888. Operation, (WinConfiguration->DDTransferConfirmation == asOff), TargetTerminal);
  6889. }
  6890. // abort drag&drop
  6891. Abort();
  6892. }
  6893. else if ((FDDExtMapFile == NULL) && (FLastDropEffect != DROPEFFECT_NONE) &&
  6894. // Drops of remote files to queue are handled in RemoteFileControlDDEnd
  6895. (FDDTargetControl != QueueView3))
  6896. {
  6897. DebugAssert(!FDragTempDir.IsEmpty());
  6898. TTransferType Type;
  6899. UnicodeString TempDir = FDragTempDir;
  6900. // We clear FDragTempDir before calling
  6901. // just in case it fail (raises exception)
  6902. FDragTempDir = L"";
  6903. Type = (FLastDropEffect & DROPEFFECT_MOVE ? ttMove : Type = ttCopy);
  6904. TGUICopyParamType CopyParams = GUIConfiguration->CurrentCopyParam;
  6905. // empty directory parameter means temp directory -> don't display it!
  6906. UnicodeString TargetDir = L"";
  6907. int Options = 0;
  6908. if (!CopyParamDialog(tdToLocal, Type, true, FDDFileList,
  6909. TargetDir, CopyParams, (WinConfiguration->DDTransferConfirmation != asOff), true, Options))
  6910. {
  6911. Abort();
  6912. }
  6913. // TargetDir is set when dropped on local file control
  6914. // (this was workaround for legacy dirview event handling, now it should be
  6915. // made prettier)
  6916. if (TargetDir.IsEmpty())
  6917. {
  6918. TargetDir = TempDir;
  6919. if (ForceDirectories(ApiPath(TargetDir)))
  6920. {
  6921. DebugAssert(Terminal && !TargetDir.IsEmpty());
  6922. FPendingTempSpaceWarn = true;
  6923. try
  6924. {
  6925. FDragDropOperation = true;
  6926. int Params = cpTemporary |
  6927. (Type == ttMove ? cpDelete : 0);
  6928. DDDownload(FDDFileList, TargetDir, &CopyParams, Params);
  6929. Configuration->Usage->Inc(L"DownloadsDragDropExternalTemp");
  6930. }
  6931. __finally
  6932. {
  6933. FDragDropOperation = false;
  6934. FPendingTempSpaceWarn = false;
  6935. AddDelayedDirectoryDeletion(TargetDir, WinConfiguration->DDDeleteDelay);
  6936. }
  6937. }
  6938. else
  6939. {
  6940. throw Exception(FMTLOAD(CREATE_TEMP_DIR_ERROR, (TargetDir)));
  6941. }
  6942. }
  6943. }
  6944. }
  6945. //---------------------------------------------------------------------------
  6946. void __fastcall TCustomScpExplorerForm::DDDownload(
  6947. TStrings * FilesToCopy, const UnicodeString & TargetDir, const TCopyParamType * CopyParam, int Params)
  6948. {
  6949. TAutoBatch AutoBatch(this);
  6950. UpdateCopyParamCounters(*CopyParam);
  6951. Terminal->CopyToLocal(FilesToCopy, TargetDir, CopyParam, Params, NULL);
  6952. if (FLAGSET(Params, cpDelete) && (DropSourceControl == RemoteDriveView))
  6953. {
  6954. RemoteDriveView->UpdateDropSource();
  6955. }
  6956. }
  6957. //---------------------------------------------------------------------------
  6958. class TFakeDataObjectFilesEx : public TDataObjectFilesEx
  6959. {
  6960. public:
  6961. __fastcall TFakeDataObjectFilesEx(TFileList * AFileList, bool RenderPIDL,
  6962. bool RenderFilename) : TDataObjectFilesEx(AFileList, RenderPIDL, RenderFilename, true)
  6963. {
  6964. }
  6965. virtual bool __fastcall AllowData(const tagFORMATETC & FormatEtc)
  6966. {
  6967. return (FormatEtc.cfFormat == CF_HDROP) ? false :
  6968. TDataObjectFilesEx::AllowData(FormatEtc);
  6969. }
  6970. };
  6971. //---------------------------------------------------------------------------
  6972. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDCreateDataObject(
  6973. TObject * Sender, TDataObject *& DataObject)
  6974. {
  6975. if (FDDExtMapFile != NULL)
  6976. {
  6977. TFileList * FileList = DragDropFiles(Sender)->FileList;
  6978. if (!FileList->RenderPIDLs() || !FileList->RenderNames())
  6979. {
  6980. Abort();
  6981. }
  6982. if (FileList->Count > 0)
  6983. {
  6984. TDataObjectFilesEx * FilesObject = new TFakeDataObjectFilesEx(FileList, true, true);
  6985. if (!FilesObject->IsValid(true, true))
  6986. {
  6987. FilesObject->_Release();
  6988. }
  6989. else
  6990. {
  6991. DataObject = FilesObject;
  6992. }
  6993. }
  6994. }
  6995. }
  6996. //---------------------------------------------------------------------------
  6997. void __fastcall TCustomScpExplorerForm::GoToCommandLine()
  6998. {
  6999. DebugFail();
  7000. }
  7001. //---------------------------------------------------------------------------
  7002. void __fastcall TCustomScpExplorerForm::GoToTree()
  7003. {
  7004. ComponentVisible[fcRemoteTree] = true;
  7005. RemoteDriveView->SetFocus();
  7006. }
  7007. //---------------------------------------------------------------------------
  7008. TStrings * __fastcall TCustomScpExplorerForm::PanelExport(TOperationSide Side,
  7009. TPanelExport Export)
  7010. {
  7011. TCustomDirView * DirView = this->DirView(Side);
  7012. std::unique_ptr<TStrings> ExportData(new TStringList());
  7013. switch (Export)
  7014. {
  7015. case pePath:
  7016. ExportData->Add(DirView->PathName);
  7017. break;
  7018. case peFileList:
  7019. case peFullFileList:
  7020. {
  7021. bool FullPath = (Export == peFullFileList);
  7022. DirView->CreateFileList(false, FullPath, ExportData.get());
  7023. for (int Index = 0; Index < ExportData->Count; Index++)
  7024. {
  7025. if (ExportData->Strings[Index].Pos(L" ") > 0)
  7026. {
  7027. ExportData->Strings[Index] = FORMAT(L"\"%s\"", (ExportData->Strings[Index]));
  7028. }
  7029. }
  7030. }
  7031. break;
  7032. default:
  7033. DebugFail();
  7034. }
  7035. return ExportData.release();
  7036. }
  7037. //---------------------------------------------------------------------------
  7038. void __fastcall TCustomScpExplorerForm::PanelExport(TOperationSide Side,
  7039. TPanelExport Export, TPanelExportDestination Destination)
  7040. {
  7041. std::unique_ptr<TStrings> ExportData(PanelExport(Side, Export));
  7042. PanelExportStore(Side, Export, Destination, ExportData.get());
  7043. }
  7044. //---------------------------------------------------------------------------
  7045. void __fastcall TCustomScpExplorerForm::PanelExportStore(TOperationSide /*Side*/,
  7046. TPanelExport /*Export*/, TPanelExportDestination Destination,
  7047. TStrings * ExportData)
  7048. {
  7049. if (Destination == pedClipboard)
  7050. {
  7051. TInstantOperationVisualizer Visualizer;
  7052. CopyToClipboard(ExportData);
  7053. }
  7054. else
  7055. {
  7056. DebugFail();
  7057. }
  7058. }
  7059. //---------------------------------------------------------------------------
  7060. void __fastcall TCustomScpExplorerForm::Filter(TOperationSide Side)
  7061. {
  7062. TCustomDirView * DirView = this->DirView(Side);
  7063. TFileFilter Filter;
  7064. DefaultFileFilter(Filter);
  7065. Filter.Masks = DirView->Mask;
  7066. if (DoFilterMaskDialog(DirView, &Filter))
  7067. {
  7068. DirView->Mask = TFileMasks::NormalizeMask(Filter.Masks);
  7069. Configuration->Usage->Inc(L"Filters");
  7070. }
  7071. }
  7072. //---------------------------------------------------------------------------
  7073. TQueueOperation __fastcall TCustomScpExplorerForm::DefaultQueueOperation()
  7074. {
  7075. return FQueueController->DefaultOperation();
  7076. }
  7077. //---------------------------------------------------------------------------
  7078. bool __fastcall TCustomScpExplorerForm::AllowQueueOperation(
  7079. TQueueOperation Operation, void ** Param)
  7080. {
  7081. switch (Operation)
  7082. {
  7083. case qoPreferences:
  7084. return true;
  7085. case qoGoTo:
  7086. return ComponentVisible[fcQueueView] && QueueView3->Enabled;
  7087. case qoOnceEmpty:
  7088. return !FQueueController->Empty;
  7089. default:
  7090. return FQueueController->AllowOperation(Operation, Param);
  7091. }
  7092. }
  7093. //---------------------------------------------------------------------------
  7094. void __fastcall TCustomScpExplorerForm::GoToQueue()
  7095. {
  7096. if (DebugAlwaysTrue(QueueView3->Visible))
  7097. {
  7098. QueueView3->SetFocus();
  7099. }
  7100. }
  7101. //---------------------------------------------------------------------------
  7102. void __fastcall TCustomScpExplorerForm::ExecuteQueueOperation(
  7103. TQueueOperation Operation, void * Param)
  7104. {
  7105. if (Operation == qoGoTo)
  7106. {
  7107. GoToQueue();
  7108. }
  7109. else if (Operation == qoPreferences)
  7110. {
  7111. PreferencesDialog(pmQueue);
  7112. }
  7113. else
  7114. {
  7115. FQueueController->ExecuteOperation(Operation, Param);
  7116. }
  7117. }
  7118. //---------------------------------------------------------------------------
  7119. bool __fastcall TCustomScpExplorerForm::GetQueueEnabled()
  7120. {
  7121. return (Queue != NULL) && Queue->Enabled;
  7122. }
  7123. //---------------------------------------------------------------------------
  7124. void __fastcall TCustomScpExplorerForm::ToggleQueueEnabled()
  7125. {
  7126. DebugAssert(Queue != NULL);
  7127. if (Queue != NULL)
  7128. {
  7129. Queue->Enabled = !Queue->Enabled;
  7130. }
  7131. }
  7132. //---------------------------------------------------------------------------
  7133. void __fastcall TCustomScpExplorerForm::QueueView3ContextPopup(
  7134. TObject * /*Sender*/, TPoint & /*MousePos*/, bool & /*Handled*/)
  7135. {
  7136. FQueueActedItem = QueueView3->ItemFocused;
  7137. }
  7138. //---------------------------------------------------------------------------
  7139. /*virtual*/ int __fastcall TCustomScpExplorerForm::GetStaticComponentsHeight()
  7140. {
  7141. return TopDock->Height + QueueSplitter->Height;
  7142. }
  7143. //---------------------------------------------------------------------------
  7144. void __fastcall TCustomScpExplorerForm::QueueSplitterCanResize(
  7145. TObject * /*Sender*/, int & NewSize, bool & Accept)
  7146. {
  7147. // when queue is hidden by double-clicking splitter, stray attempt to
  7148. // resize the panel with strange value arrives, make sure it is ignored
  7149. if (ComponentVisible[fcQueueView])
  7150. {
  7151. int HeightLimit = ClientHeight - GetStaticComponentsHeight() -
  7152. RemotePanel->Constraints->MinHeight;
  7153. if (NewSize > HeightLimit)
  7154. {
  7155. NewSize = HeightLimit;
  7156. }
  7157. }
  7158. else
  7159. {
  7160. Accept = false;
  7161. }
  7162. }
  7163. //---------------------------------------------------------------------------
  7164. void __fastcall TCustomScpExplorerForm::QueueView3StartDrag(TObject * /*Sender*/,
  7165. TDragObject *& /*DragObject*/)
  7166. {
  7167. FQueueActedItem = QueueView3->ItemFocused;
  7168. }
  7169. //---------------------------------------------------------------------------
  7170. void __fastcall TCustomScpExplorerForm::QueueView3DragOver(TObject * /*Sender*/,
  7171. TObject * Source, int X, int Y, TDragState /*State*/, bool & Accept)
  7172. {
  7173. Accept = true;
  7174. if (Source == QueueView3)
  7175. {
  7176. TListItem * DropTarget = QueueView3->GetItemAt(X, Y);
  7177. Accept = (DropTarget != NULL) && (FQueueActedItem != NULL);
  7178. if (Accept)
  7179. {
  7180. TQueueItemProxy * QueueItem;
  7181. TQueueItemProxy * DestQueueItem;
  7182. QueueItem = static_cast<TQueueItemProxy *>(FQueueActedItem->Data);
  7183. DestQueueItem = static_cast<TQueueItemProxy *>(DropTarget->Data);
  7184. Accept = (QueueItem != DestQueueItem) &&
  7185. (QueueItem->Status == TQueueItem::qsPending) &&
  7186. (DestQueueItem->Status == TQueueItem::qsPending);
  7187. }
  7188. }
  7189. }
  7190. //---------------------------------------------------------------------------
  7191. void __fastcall TCustomScpExplorerForm::QueueView3DragDrop(TObject * /*Sender*/,
  7192. TObject * /*Source*/, int /*X*/, int /*Y*/)
  7193. {
  7194. if ((FQueueActedItem != NULL) && (QueueView3->DropTarget != NULL))
  7195. {
  7196. TQueueItemProxy * QueueItem;
  7197. TQueueItemProxy * DestQueueItem;
  7198. QueueItem = static_cast<TQueueItemProxy *>(FQueueActedItem->Data);
  7199. DestQueueItem = static_cast<TQueueItemProxy *>(QueueView3->DropTarget->Data);
  7200. QueueItem->Move(DestQueueItem);
  7201. }
  7202. }
  7203. //---------------------------------------------------------------------------
  7204. void __fastcall TCustomScpExplorerForm::QueueView3Enter(TObject * /*Sender*/)
  7205. {
  7206. if ((QueueView3->ItemFocused == NULL) &&
  7207. (QueueView3->Items->Count > 0))
  7208. {
  7209. QueueView3->ItemFocused = QueueView3->Items->Item[0];
  7210. }
  7211. QueueLabelUpdateStatus();
  7212. }
  7213. //---------------------------------------------------------------------------
  7214. void __fastcall TCustomScpExplorerForm::QueueView3Exit(TObject * /*Sender*/)
  7215. {
  7216. QueueLabelUpdateStatus();
  7217. }
  7218. //---------------------------------------------------------------------------
  7219. void __fastcall TCustomScpExplorerForm::QueueLabelUpdateStatus()
  7220. {
  7221. QueueLabel->UpdateStatus();
  7222. }
  7223. //---------------------------------------------------------------------------
  7224. void __fastcall TCustomScpExplorerForm::QueueView3SelectItem(
  7225. TObject * /*Sender*/, TListItem * /*Item*/, bool Selected)
  7226. {
  7227. if (Selected)
  7228. {
  7229. NonVisualDataModule->UpdateNonVisibleActions();
  7230. }
  7231. }
  7232. //---------------------------------------------------------------------------
  7233. TDragDropFilesEx * __fastcall TCustomScpExplorerForm::DragDropFiles(TObject * Sender)
  7234. {
  7235. TDragDropFilesEx * Result = NULL;
  7236. if (Sender == SessionsPageControl)
  7237. {
  7238. Result = FSessionsDragDropFilesEx;
  7239. }
  7240. else if (Sender == QueueView3)
  7241. {
  7242. Result = FQueueDragDropFilesEx;
  7243. }
  7244. else
  7245. {
  7246. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  7247. if (DirView != NULL)
  7248. {
  7249. Result = DirView->DragDropFilesEx;
  7250. }
  7251. else
  7252. {
  7253. TCustomDriveView * DriveView = dynamic_cast<TCustomDriveView *>(Sender);
  7254. if (DriveView != NULL)
  7255. {
  7256. Result = DriveView->DragDropFilesEx;
  7257. }
  7258. }
  7259. }
  7260. DebugAssert(Result != NULL);
  7261. return Result;
  7262. }
  7263. //---------------------------------------------------------------------------
  7264. bool __fastcall TCustomScpExplorerForm::SelectedAllFilesInDirView(TCustomDirView * DView)
  7265. {
  7266. return (DView->SelCount == DView->FilesCount);
  7267. }
  7268. //---------------------------------------------------------------------------
  7269. bool __fastcall TCustomScpExplorerForm::DraggingAllFilesFromDirView(TOperationSide Side, TStrings * FileList)
  7270. {
  7271. return HasDirView[Side] && (DropSourceControl == DirView(Side)) && (FileList->Count == DirView(Side)->FilesCount);
  7272. }
  7273. //---------------------------------------------------------------------------
  7274. void __fastcall TCustomScpExplorerForm::RemoteFileControlDragDropFileOperation(
  7275. TObject * Sender, int Effect, UnicodeString TargetPath, bool ForceQueue, bool DragDrop)
  7276. {
  7277. TFileOperation Operation;
  7278. switch (Effect)
  7279. {
  7280. case DROPEFFECT_MOVE:
  7281. Operation = foMove;
  7282. break;
  7283. case DROPEFFECT_COPY:
  7284. // occures on WinXP (reported by user)
  7285. default:
  7286. Operation = foCopy;
  7287. break;
  7288. };
  7289. TDragDropFilesEx * DragDropFilesEx = DragDropFiles(Sender);
  7290. // see a comment in TUnixDirView::PerformItemDragDropOperation
  7291. if (DragDropFilesEx->FileList->Count > 0)
  7292. {
  7293. TStrings * FileList = new TStringList();
  7294. try
  7295. {
  7296. for (int Index = 0; Index < DragDropFilesEx->FileList->Count; Index++)
  7297. {
  7298. FileList->Add(DragDropFilesEx->FileList->Items[Index]->Name);
  7299. }
  7300. FDragDropOperation = true;
  7301. TTransferOperationParam Param;
  7302. Param.TargetDirectory = TargetPath;
  7303. // upload, no temp dirs
  7304. Param.Temp = false;
  7305. Param.DragDrop = DragDrop;
  7306. Param.Options =
  7307. FLAGMASK(DraggingAllFilesFromDirView(osLocal, FileList), coAllFiles);
  7308. if (ForceQueue)
  7309. {
  7310. Param.Queue = asOn;
  7311. }
  7312. bool NoConfirmation = DragDrop ? (WinConfiguration->DDTransferConfirmation == asOff) : false;
  7313. if (ExecuteFileOperation(Operation, osLocal, FileList, NoConfirmation, &Param))
  7314. {
  7315. if (IsFileControl(DropSourceControl, osLocal))
  7316. {
  7317. Configuration->Usage->Inc(L"UploadsDragDropInternal");
  7318. }
  7319. else
  7320. {
  7321. Configuration->Usage->Inc(L"UploadsDragDropExternal");
  7322. }
  7323. }
  7324. }
  7325. __finally
  7326. {
  7327. FDragDropOperation = false;
  7328. delete FileList;
  7329. }
  7330. }
  7331. }
  7332. //---------------------------------------------------------------------------
  7333. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDFileOperation(
  7334. TObject * Sender, int Effect, UnicodeString /*SourcePath*/,
  7335. UnicodeString TargetPath, bool Paste, bool & /*DoOperation*/)
  7336. {
  7337. RemoteFileControlDragDropFileOperation(Sender, Effect, TargetPath, false, !Paste);
  7338. }
  7339. //---------------------------------------------------------------------------
  7340. void __fastcall TCustomScpExplorerForm::RemoteFileContolDDChooseEffect(
  7341. TObject * Sender, int grfKeyState, int & dwEffect)
  7342. {
  7343. // if any drop effect is allowed at all (e.g. no drop to self and drop to parent)
  7344. if ((dwEffect != DROPEFFECT_None) &&
  7345. IsFileControl(DropSourceControl, osRemote))
  7346. {
  7347. // do not allow drop on remote panel (on free space, still allow drop on directories)
  7348. if ((Sender == RemoteDirView) && (DropSourceControl == RemoteDirView) &&
  7349. (RemoteDirView->DropTarget == NULL))
  7350. {
  7351. dwEffect = DROPEFFECT_None;
  7352. }
  7353. else
  7354. {
  7355. if (dwEffect == DROPEFFECT_Copy)
  7356. {
  7357. bool MoveCapable = FTerminal->IsCapable[fcRemoteMove];
  7358. // currently we support copying always (at least via temporary directory);
  7359. // remove associated checks once this all proves stable and working
  7360. bool CopyCapable = true;
  7361. // if we do not support neither of operations, there's no discussion
  7362. if (!MoveCapable && !CopyCapable)
  7363. {
  7364. dwEffect = DROPEFFECT_None;
  7365. }
  7366. else
  7367. {
  7368. // The default effect inside remote panel is move,
  7369. // unless we do not support it, but support copy
  7370. if (FLAGCLEAR(grfKeyState, MK_CONTROL))
  7371. {
  7372. dwEffect = MoveCapable ? DROPEFFECT_Move : DROPEFFECT_Copy;
  7373. }
  7374. else
  7375. {
  7376. // with ctrl-down, we want copy unless it is not supported
  7377. dwEffect = CopyCapable ? DROPEFFECT_Copy : DROPEFFECT_None;
  7378. }
  7379. }
  7380. }
  7381. }
  7382. }
  7383. }
  7384. //---------------------------------------------------------------------------
  7385. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDDragFileName(
  7386. TObject * Sender, TRemoteFile * File, UnicodeString & FileName)
  7387. {
  7388. if (FDDTotalSize >= 0)
  7389. {
  7390. if (File->IsDirectory)
  7391. {
  7392. FDDTotalSize = -1;
  7393. }
  7394. else
  7395. {
  7396. FDDTotalSize += File->Size;
  7397. }
  7398. }
  7399. DebugAssert(!FDragTempDir.IsEmpty());
  7400. // TODO: this is quite ineffective
  7401. // TODO: what if invalid character replacement is disabled?
  7402. FileName = FDragTempDir + GUIConfiguration->CurrentCopyParam.ValidLocalFileName(File->FileName);
  7403. UnicodeString TransferFileName;
  7404. if (Sender == RemoteDriveView)
  7405. {
  7406. TransferFileName = UnixExcludeTrailingBackslash(File->FullFileName);
  7407. }
  7408. else
  7409. {
  7410. TransferFileName = File->FileName;
  7411. }
  7412. FDDFileList->AddObject(TransferFileName, File);
  7413. }
  7414. //---------------------------------------------------------------------------
  7415. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDDragDetect(
  7416. TObject * /*Sender*/, int /*grfKeyState*/, const TPoint & /*DetectStart*/,
  7417. const TPoint & /*Point*/, TDragDetectStatus /*DragStatus*/)
  7418. {
  7419. // sometimes we do not get DDEnd so the list is not released
  7420. SAFE_DESTROY(FDDFileList);
  7421. FDDFileList = new TStringList();
  7422. FDragTempDir = WinConfiguration->TemporaryDir();
  7423. FDDTotalSize = 0;
  7424. FDragDropSshTerminate.reset(NULL);
  7425. }
  7426. //---------------------------------------------------------------------------
  7427. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDQueryContinueDrag(
  7428. TObject * /*Sender*/, BOOL /*FEscapePressed*/, int /*grfKeyState*/,
  7429. HRESULT & Result)
  7430. {
  7431. if (Result == DRAGDROP_S_DROP)
  7432. {
  7433. try
  7434. {
  7435. GlobalDragImageList->HideDragImage();
  7436. try
  7437. {
  7438. RemoteFileControlDDTargetDrop();
  7439. }
  7440. catch(ESshTerminate & E)
  7441. {
  7442. FDragDropSshTerminate.reset(E.Clone());
  7443. }
  7444. }
  7445. catch (Exception &E)
  7446. {
  7447. // If downloading fails we need to cancel drag&drop, otherwise
  7448. // Explorer shows error
  7449. // But by the way exception probably never reach this point as
  7450. // it's catched on way.
  7451. // Fatal exceptions get here (like when opening a secondary shell extension for file duplication fails).
  7452. Result = DRAGDROP_S_CANCEL;
  7453. ShowExtendedException(Terminal, &E);
  7454. }
  7455. }
  7456. }
  7457. //---------------------------------------------------------------------------
  7458. void __fastcall TCustomScpExplorerForm::DirViewMatchMask(
  7459. TObject * /*Sender*/, UnicodeString FileName, bool Directory, __int64 Size,
  7460. TDateTime Modification, UnicodeString Masks, bool & Matches, bool AllowImplicitMatches)
  7461. {
  7462. TFileMasks::TParams MaskParams;
  7463. MaskParams.Size = Size;
  7464. MaskParams.Modification = Modification;
  7465. // this does not re-parse the mask if it is the same as the last time
  7466. FDirViewMatchMask = Masks;
  7467. bool ImplicitMatch;
  7468. Matches =
  7469. // RecurseInclude parameter has no effect as the Path is empty
  7470. FDirViewMatchMask.Matches(FileName, Directory, UnicodeString(L""), &MaskParams, true, ImplicitMatch) &&
  7471. (AllowImplicitMatches || !ImplicitMatch);
  7472. }
  7473. //---------------------------------------------------------------------------
  7474. void __fastcall TCustomScpExplorerForm::DirViewGetOverlay(
  7475. TObject * Sender, TListItem * Item, WORD & Indexes)
  7476. {
  7477. TCustomDirView * DirView = reinterpret_cast<TCustomDirView *>(Sender);
  7478. UnicodeString Ext;
  7479. if (DirView == RemoteDirView)
  7480. {
  7481. Ext = UnixExtractFileExt(DirView->ItemFileName(Item));
  7482. }
  7483. else
  7484. {
  7485. Ext = ExtractFileExt(DirView->ItemFileName(Item));
  7486. }
  7487. if (SameText(Ext, Configuration->PartialExt))
  7488. {
  7489. Indexes |= oiPartial;
  7490. }
  7491. }
  7492. //---------------------------------------------------------------------------
  7493. bool __fastcall TCustomScpExplorerForm::CanPasteToDirViewFromClipBoard()
  7494. {
  7495. return
  7496. DirViewEnabled(osCurrent) &&
  7497. DirView(osCurrent)->CanPasteFromClipBoard();
  7498. }
  7499. //---------------------------------------------------------------------------
  7500. bool __fastcall TCustomScpExplorerForm::CanPasteFromClipBoard()
  7501. {
  7502. bool Result = false;
  7503. if (CanPasteToDirViewFromClipBoard())
  7504. {
  7505. Result = true;
  7506. }
  7507. else
  7508. {
  7509. UnicodeString ClipboardText;
  7510. if (NonEmptyTextFromClipboard(ClipboardText))
  7511. {
  7512. if (StoredSessions->IsUrl(ClipboardText))
  7513. {
  7514. Result = true;
  7515. }
  7516. else
  7517. {
  7518. Result = DirViewEnabled(osCurrent);
  7519. }
  7520. }
  7521. }
  7522. return Result;
  7523. }
  7524. //---------------------------------------------------------------------------
  7525. void __fastcall TCustomScpExplorerForm::PasteFromClipBoard()
  7526. {
  7527. if (DoesClipboardContainOurFiles())
  7528. {
  7529. if (DebugAlwaysTrue(CanPasteToDirViewFromClipBoard()))
  7530. {
  7531. TTerminalManager * Manager = TTerminalManager::Instance();
  7532. TTerminal * TergetTerminal = Manager->ActiveTerminal;
  7533. Manager->ActiveTerminal = FClipboardTerminal;
  7534. ExecuteFileOperation(foRemoteCopy, osRemote, FClipboardFileList.get(), false, TergetTerminal);
  7535. }
  7536. }
  7537. else if (CanPasteToDirViewFromClipBoard())
  7538. {
  7539. DirView(osCurrent)->PasteFromClipBoard();
  7540. }
  7541. else
  7542. {
  7543. UnicodeString ClipboardText;
  7544. if (NonEmptyTextFromClipboard(ClipboardText))
  7545. {
  7546. if (StoredSessions->IsUrl(ClipboardText))
  7547. {
  7548. NewSession(false, ClipboardText);
  7549. }
  7550. else
  7551. {
  7552. DirView(osCurrent)->Path = ClipboardText;
  7553. }
  7554. }
  7555. }
  7556. }
  7557. //---------------------------------------------------------------------------
  7558. void __fastcall TCustomScpExplorerForm::FileListFromClipboard()
  7559. {
  7560. // TBD
  7561. }
  7562. //---------------------------------------------------------------------------
  7563. void __fastcall TCustomScpExplorerForm::SelectAll(TOperationSide Side, TSelectMode Mode)
  7564. {
  7565. TCustomDirView * ADirView = DirView(Side);
  7566. ADirView->SelectAll(Mode);
  7567. ADirView->SetFocus();
  7568. }
  7569. //---------------------------------------------------------------------------
  7570. void __fastcall TCustomScpExplorerForm::SelectByMask(TOperationSide Side, bool Select)
  7571. {
  7572. TCustomDirView * ADirView = DirView(Side);
  7573. TFileFilter Filter;
  7574. DefaultFileFilter(Filter);
  7575. if (DoSelectMaskDialog(ADirView, Select, &Filter, Configuration))
  7576. {
  7577. Configuration->Usage->Inc(L"MaskSelections");
  7578. ADirView->SelectFiles(Filter, Select);
  7579. ADirView->SetFocus();
  7580. }
  7581. }
  7582. //---------------------------------------------------------------------------
  7583. void __fastcall TCustomScpExplorerForm::RestoreSelectedNames(TOperationSide Side)
  7584. {
  7585. TCustomDirView * ADirView = DirView(Side);
  7586. ADirView->RestoreSelectedNames();
  7587. ADirView->SetFocus();
  7588. }
  7589. //---------------------------------------------------------------------------
  7590. void __fastcall TCustomScpExplorerForm::SelectSameExt(bool Select)
  7591. {
  7592. TCustomDirView * CurrentDirView = DirView(osCurrent);
  7593. if (DebugAlwaysTrue(CurrentDirView->ItemFocused != NULL))
  7594. {
  7595. UnicodeString FileName = CurrentDirView->ItemFileName(CurrentDirView->ItemFocused);
  7596. UnicodeString Ext;
  7597. if (GetSide(osCurrent) == osRemote)
  7598. {
  7599. Ext = UnixExtractFileExt(FileName);
  7600. }
  7601. else
  7602. {
  7603. Ext = ExtractFileExt(FileName);
  7604. }
  7605. if (Ext.IsEmpty())
  7606. {
  7607. Ext = L".";
  7608. }
  7609. TFileFilter Filter;
  7610. Filter.Masks = FORMAT(L"*%s", (Ext));
  7611. Filter.Directories = false;
  7612. CurrentDirView->SelectFiles(Filter, Select);
  7613. }
  7614. }
  7615. //---------------------------------------------------------------------------
  7616. UnicodeString __fastcall TCustomScpExplorerForm::FileStatusBarText(
  7617. const TStatusFileInfo & FileInfo, TOperationSide Side)
  7618. {
  7619. UnicodeString Result;
  7620. if ((Side == osRemote) && (Terminal == NULL))
  7621. {
  7622. // noop
  7623. }
  7624. else
  7625. {
  7626. Result =
  7627. FMTLOAD(FILE_INFO_FORMAT,
  7628. (FormatBytes(FileInfo.SelectedSize),
  7629. FormatBytes(FileInfo.FilesSize),
  7630. FormatNumber(FileInfo.SelectedCount),
  7631. FormatNumber(FileInfo.FilesCount)));
  7632. }
  7633. return Result;
  7634. }
  7635. //---------------------------------------------------------------------------
  7636. void __fastcall TCustomScpExplorerForm::FileStatusBarPanelClick(
  7637. TTBXStatusPanel * Panel, TOperationSide Side)
  7638. {
  7639. if (Panel->Index == 1)
  7640. {
  7641. ToggleShowHiddenFiles();
  7642. }
  7643. else if (Panel->Index == 2)
  7644. {
  7645. Filter(Side);
  7646. }
  7647. }
  7648. //---------------------------------------------------------------------------
  7649. void __fastcall TCustomScpExplorerForm::UpdateFileStatusBar(
  7650. TTBXStatusBar * StatusBar, const TStatusFileInfo & FileInfo, TOperationSide Side)
  7651. {
  7652. DebugAssert(!StatusBar->SimplePanel);
  7653. StatusBar->Panels->Items[0]->Caption = FileStatusBarText(FileInfo, Side);
  7654. UpdateFileStatusExtendedPanels(StatusBar, FileInfo);
  7655. }
  7656. //---------------------------------------------------------------------------
  7657. void __fastcall TCustomScpExplorerForm::UpdateFileStatusExtendedPanels(
  7658. TTBXStatusBar * StatusBar, const TStatusFileInfo & FileInfo)
  7659. {
  7660. DebugAssert(StatusBar->Panels->Count >= 3);
  7661. TTBXStatusPanel * HiddenFilesPanel = StatusBar->Panels->Items[1];
  7662. if (FileInfo.HiddenCount > 0)
  7663. {
  7664. HiddenFilesPanel->Caption = FMTLOAD(FILE_INFO_HIDDEN2, (FormatNumber(FileInfo.HiddenCount)));
  7665. HiddenFilesPanel->ViewPriority = 90; // <100 allows hiding panel when it does not fit
  7666. }
  7667. else
  7668. {
  7669. HiddenFilesPanel->ViewPriority = 0;
  7670. // not really necessary, just to cleanup no-longer-valid data
  7671. HiddenFilesPanel->Caption = L"";
  7672. }
  7673. TTBXStatusPanel * FilteredFilesPanel = StatusBar->Panels->Items[2];
  7674. if (FileInfo.FilteredCount > 0)
  7675. {
  7676. FilteredFilesPanel->Caption = FMTLOAD(FILE_INFO_FILTERED2, (FormatNumber(FileInfo.FilteredCount)));
  7677. FilteredFilesPanel->ViewPriority = 90; // <100 allows hiding panel when it does not fit
  7678. }
  7679. else
  7680. {
  7681. FilteredFilesPanel->ViewPriority = 0;
  7682. // not really necessary, just to cleanup no-longer-valid data
  7683. FilteredFilesPanel->Caption = L"";
  7684. }
  7685. }
  7686. //---------------------------------------------------------------------------
  7687. void __fastcall TCustomScpExplorerForm::RemoteStatusBarClick(
  7688. TObject * /*Sender*/)
  7689. {
  7690. if (RemoteDirView->Enabled)
  7691. {
  7692. RemoteDirView->SetFocus();
  7693. }
  7694. }
  7695. //---------------------------------------------------------------------------
  7696. void __fastcall TCustomScpExplorerForm::ToggleQueueVisibility()
  7697. {
  7698. TQueueViewConfiguration Config = WinConfiguration->QueueView;
  7699. switch (Config.Show)
  7700. {
  7701. case qvShow:
  7702. if ((FQueueStatus != NULL) && (FQueueStatus->Count > 0))
  7703. {
  7704. Config.Show = qvHide;
  7705. }
  7706. else
  7707. {
  7708. Config.Show = Config.LastHideShow;
  7709. }
  7710. break;
  7711. case qvHideWhenEmpty:
  7712. if (ComponentVisible[fcQueueView])
  7713. {
  7714. Config.Show = qvHide;
  7715. }
  7716. else
  7717. {
  7718. Config.LastHideShow = Config.Show;
  7719. Config.Show = qvShow;
  7720. }
  7721. break;
  7722. case qvHide:
  7723. Config.LastHideShow = Config.Show;
  7724. Config.Show = qvShow;
  7725. break;
  7726. }
  7727. WinConfiguration->QueueView = Config;
  7728. }
  7729. //---------------------------------------------------------------------------
  7730. UnicodeString __fastcall TCustomScpExplorerForm::PathForCaption()
  7731. {
  7732. UnicodeString Result;
  7733. if (FTerminal != NULL)
  7734. {
  7735. switch (WinConfiguration->PathInCaption)
  7736. {
  7737. case picShort:
  7738. Result = TTerminalManager::Instance()->GetTerminalShortPath(FTerminal);
  7739. break;
  7740. case picFull:
  7741. Result = FTerminal->CurrentDirectory;
  7742. break;
  7743. }
  7744. }
  7745. return Result;
  7746. }
  7747. //---------------------------------------------------------------------------
  7748. void __fastcall TCustomScpExplorerForm::UpdateControls()
  7749. {
  7750. TTerminalManager::Instance()->UpdateAppTitle();
  7751. // WORAKRDOUND: Disabling list view when it is not showing yet does not set its
  7752. // background to gray on Windows 7 (works on Windows 10).
  7753. // See also EnableControl
  7754. if (Showing)
  7755. {
  7756. bool HasTerminal = (Terminal != NULL) && Terminal->Active;
  7757. if (HasTerminal)
  7758. {
  7759. // Update path when it changes
  7760. if ((SessionsPageControl->ActivePage != NULL) && (GetSessionTabTerminal(SessionsPageControl->ActivePage) == Terminal))
  7761. {
  7762. UpdateSessionTab(SessionsPageControl->ActivePage);
  7763. }
  7764. if (!RemoteDirView->Enabled)
  7765. {
  7766. RemoteDirView->Enabled = true;
  7767. if (FRemoteDirViewWasFocused)
  7768. {
  7769. ActiveControl = RemoteDirView;
  7770. }
  7771. }
  7772. RemoteDriveView->Enabled = true;
  7773. RemoteDirView->Color = (FSessionColor != 0 ? FSessionColor : clWindow);
  7774. RemoteDriveView->Color = RemoteDirView->Color;
  7775. }
  7776. else
  7777. {
  7778. if (RemoteDirView->Enabled)
  7779. {
  7780. // This is first called when the form is being constructed
  7781. // (not anymore due to Showing test above)
  7782. // but the false is overriden in the constructor later.
  7783. // An even later in TScpCommanderForm::DoShow()
  7784. FRemoteDirViewWasFocused = (ActiveControl == RemoteDirView);
  7785. EnableControl(RemoteDirView, false);
  7786. }
  7787. EnableControl(RemoteDriveView, false);
  7788. }
  7789. EnableControl(QueueView3, HasTerminal && Terminal->IsCapable[fsBackgroundTransfers]);
  7790. QueueLabelUpdateStatus();
  7791. reinterpret_cast<TTBCustomItem *>(GetComponent(fcRemotePathComboBox))->Enabled = HasTerminal;
  7792. }
  7793. }
  7794. //---------------------------------------------------------------------------
  7795. void __fastcall TCustomScpExplorerForm::DoDirViewLoaded(TCustomDirView * /*Sender*/)
  7796. {
  7797. UpdateControls();
  7798. }
  7799. //---------------------------------------------------------------------------
  7800. void __fastcall TCustomScpExplorerForm::DirViewLoaded(
  7801. TObject * Sender)
  7802. {
  7803. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  7804. DebugAssert(DirView != NULL);
  7805. DoDirViewLoaded(DirView);
  7806. TransferPresetAutoSelect();
  7807. }
  7808. //---------------------------------------------------------------------------
  7809. void __fastcall TCustomScpExplorerForm::StartUpdates()
  7810. {
  7811. TUpdatesConfiguration Updates = WinConfiguration->Updates;
  7812. // first run after installation
  7813. if (double(Updates.LastCheck) == 0)
  7814. {
  7815. // make sure next time there will be an update (if enabled)
  7816. Updates.LastCheck = TDateTime(1);
  7817. WinConfiguration->Updates = Updates;
  7818. }
  7819. else if ((double(Updates.Period) > 0) &&
  7820. (Now() - Updates.LastCheck >= Updates.Period))
  7821. {
  7822. TThreadMethod OnUpdatesChecked = NULL;
  7823. if (!IsUWP())
  7824. {
  7825. OnUpdatesChecked = UpdatesChecked;
  7826. }
  7827. StartUpdateThread(OnUpdatesChecked);
  7828. }
  7829. }
  7830. //---------------------------------------------------------------------------
  7831. void __fastcall TCustomScpExplorerForm::UpdatesChecked()
  7832. {
  7833. UnicodeString Message;
  7834. bool New;
  7835. TQueryType Type;
  7836. GetUpdatesMessage(Message, New, Type, false);
  7837. if (!Message.IsEmpty())
  7838. {
  7839. if (New)
  7840. {
  7841. Message = FMTLOAD(NEW_VERSION_CLICK, (Message));
  7842. }
  7843. if (!New && (Type != qtWarning))
  7844. {
  7845. PostNote(UnformatMessage(Message), 0, UpdatesNoteClicked, NULL);
  7846. }
  7847. else
  7848. {
  7849. Notify(NULL, Message, Type, true, UpdatesNoteClicked);
  7850. }
  7851. if (New)
  7852. {
  7853. Configuration->Usage->Inc(L"UpdateNotifications");
  7854. }
  7855. }
  7856. }
  7857. //---------------------------------------------------------------------------
  7858. void __fastcall TCustomScpExplorerForm::UpdatesNoteClicked(TObject * /*Sender*/)
  7859. {
  7860. RestoreApp();
  7861. bool CanDisplay = !NonVisualDataModule->Busy;
  7862. if (!CanDisplay && (Screen->ActiveForm != NULL))
  7863. {
  7864. CanDisplay = (Screen->ActiveForm->Perform(WM_CAN_DISPLAY_UPDATES, 0, 0) != 0);
  7865. }
  7866. if (CanDisplay)
  7867. {
  7868. Configuration->Usage->Inc(L"UpdateNotificationsClicked");
  7869. CheckForUpdates(true);
  7870. }
  7871. else
  7872. {
  7873. Configuration->Usage->Inc(L"UpdateNotificationsBusy");
  7874. }
  7875. }
  7876. //---------------------------------------------------------------------------
  7877. void __fastcall TCustomScpExplorerForm::GetTransferPresetAutoSelectData(
  7878. TCopyParamRuleData & Data)
  7879. {
  7880. DebugAssert(Terminal != NULL);
  7881. Data.HostName = Terminal->SessionData->HostNameExpanded;
  7882. Data.UserName = Terminal->SessionData->UserNameExpanded;
  7883. Data.RemoteDirectory = RemoteDirView->PathName;
  7884. }
  7885. //---------------------------------------------------------------------------
  7886. void __fastcall TCustomScpExplorerForm::TransferPresetAutoSelect()
  7887. {
  7888. // Terminal can be null when we are changing local directory implicitly,
  7889. // because it has been deleted, while no session is connected
  7890. // (Login dialog is open)
  7891. if (FAllowTransferPresetAutoSelect && (Terminal != NULL))
  7892. {
  7893. TCopyParamRuleData Data;
  7894. GetTransferPresetAutoSelectData(Data);
  7895. int CopyParamIndex = GUIConfiguration->CopyParamList->Find(Data);
  7896. UnicodeString CopyParamCurrent = GUIConfiguration->CopyParamCurrent;
  7897. if (CopyParamIndex < 0)
  7898. {
  7899. // there is no preset that matches autoselection
  7900. // set preset that we consider "default"
  7901. FCopyParamAutoSelected = L""; // forget last autoselected preset
  7902. GUIConfiguration->CopyParamCurrent = FCopyParamDefault;
  7903. }
  7904. else
  7905. {
  7906. // there is preset matching autoselection
  7907. UnicodeString CopyParamName = GUIConfiguration->CopyParamList->Names[CopyParamIndex];
  7908. if (CopyParamName == FCopyParamAutoSelected)
  7909. {
  7910. // autoselected the same preset as the last time
  7911. // make no change (i.e. preserve custom user preset, if any)
  7912. }
  7913. else
  7914. {
  7915. // autoselected the different preset then the last time (or there
  7916. // was default preset set)
  7917. FCopyParamAutoSelected = CopyParamName; // remember autoselection
  7918. GUIConfiguration->CopyParamCurrent = CopyParamName;
  7919. }
  7920. }
  7921. if (GUIConfiguration->CopyParamCurrent != CopyParamCurrent)
  7922. {
  7923. if (CopyParamIndex >= 0)
  7924. {
  7925. Configuration->Usage->Inc(L"CopyParamAutoSelects");
  7926. }
  7927. TTransferPresetNoteData * Data = new TTransferPresetNoteData;
  7928. try
  7929. {
  7930. int Fmt =
  7931. (CopyParamIndex < 0) ?
  7932. (GUIConfiguration->CopyParamIndex < 0 ? COPY_PARAM_DEFAULT_NORM : COPY_PARAM_DEFAULT_CUSTOM) :
  7933. COPY_PARAM_AUTOSELECTED;
  7934. UnicodeString Message = FMTLOAD(Fmt, (StripHotkey(GUIConfiguration->CopyParamCurrent)));
  7935. Data->Message = MainInstructions(Message);
  7936. int CopyParamAttrs = Terminal->UsableCopyParamAttrs(0).General;
  7937. UnicodeString Info = GUIConfiguration->CurrentCopyParam.GetInfoStr(L"\n",
  7938. CopyParamAttrs);
  7939. if (CopyParamIndex >= 0)
  7940. {
  7941. DebugAssert(GUIConfiguration->CopyParamList->Rules[CopyParamIndex] != NULL);
  7942. Info = FORMAT(L"%s\n \n%s", (Info,
  7943. FMTLOAD(COPY_PARAM_RULE,
  7944. (GUIConfiguration->CopyParamList->Rules[CopyParamIndex]->GetInfoStr(L"\n")))));
  7945. }
  7946. Data->Message += L"\n\n" + Info;
  7947. if (WinConfiguration->CopyParamAutoSelectNotice)
  7948. {
  7949. TransferPresetNoteMessage(Data, true);
  7950. }
  7951. else
  7952. {
  7953. PostNote(Message, 0, TransferPresetNoteClicked, Data);
  7954. Data = NULL; // ownership passed
  7955. }
  7956. }
  7957. __finally
  7958. {
  7959. delete Data;
  7960. }
  7961. }
  7962. }
  7963. }
  7964. //---------------------------------------------------------------------------
  7965. void __fastcall TCustomScpExplorerForm::TransferPresetNoteMessage(
  7966. TTransferPresetNoteData * NoteData, bool AllowNeverAskAgain)
  7967. {
  7968. DebugAssert(NoteData != NULL);
  7969. TMessageParams Params(AllowNeverAskAgain ? mpNeverAskAgainCheck : 0);
  7970. TQueryButtonAlias Aliases[1];
  7971. Aliases[0].Button = qaIgnore; // "ignore" is after "ok"
  7972. Aliases[0].Alias = LoadStr(CONFIGURE_BUTTON);
  7973. Params.Aliases = Aliases;
  7974. Params.AliasesCount = LENOF(Aliases);
  7975. unsigned int Result =
  7976. MoreMessageDialog(NoteData->Message, NULL, qtInformation,
  7977. qaOK | qaIgnore, HELP_COPY_PARAM_AUTOSELECTED, &Params);
  7978. switch (Result)
  7979. {
  7980. case qaNeverAskAgain:
  7981. DebugAssert(AllowNeverAskAgain);
  7982. WinConfiguration->CopyParamAutoSelectNotice = false;
  7983. break;
  7984. case qaIgnore:
  7985. PreferencesDialog(pmPresets);
  7986. break;
  7987. }
  7988. }
  7989. //---------------------------------------------------------------------------
  7990. void __fastcall TCustomScpExplorerForm::TransferPresetNoteClicked(TObject * Sender)
  7991. {
  7992. // as of now useless, as this is used for notes only, never for balloons, ...
  7993. RestoreApp();
  7994. // .. and we should never be busy here
  7995. if (DebugAlwaysTrue(!NonVisualDataModule->Busy))
  7996. {
  7997. TransferPresetNoteMessage(DebugNotNull(dynamic_cast<TTransferPresetNoteData *>(Sender)), false);
  7998. }
  7999. }
  8000. //---------------------------------------------------------------------------
  8001. void __fastcall TCustomScpExplorerForm::PreferencesDialog(
  8002. TPreferencesMode APreferencesMode)
  8003. {
  8004. std::unique_ptr<TPreferencesDialogData> PreferencesData;
  8005. TCopyParamRuleData Data;
  8006. if (Terminal != NULL)
  8007. {
  8008. PreferencesData.reset(new TPreferencesDialogData());
  8009. GetTransferPresetAutoSelectData(Data);
  8010. PreferencesData->CopyParamRuleData = &Data;
  8011. }
  8012. DoPreferencesDialog(APreferencesMode, PreferencesData.get());
  8013. }
  8014. //---------------------------------------------------------------------------
  8015. void __fastcall TCustomScpExplorerForm::AdHocCustomCommandValidate(
  8016. const TCustomCommandType & Command)
  8017. {
  8018. if (CustomCommandState(Command, FEditingFocusedAdHocCommand, ccltAll) <= 0)
  8019. {
  8020. throw Exception(FMTLOAD(CUSTOM_COMMAND_IMPOSSIBLE, (Command.Command)));
  8021. }
  8022. }
  8023. //---------------------------------------------------------------------------
  8024. void __fastcall TCustomScpExplorerForm::AdHocCustomCommand(bool OnFocused)
  8025. {
  8026. bool RemoteAllowed = CustomCommandRemoteAllowed();
  8027. TCustomCommandType Command;
  8028. // make sure we use local custom command when remote are not supported
  8029. if (RemoteAllowed || FLAGSET(FLastCustomCommand.Params, ccLocal))
  8030. {
  8031. Command = FLastCustomCommand;
  8032. }
  8033. else
  8034. {
  8035. Command.Params = Command.Params | ccLocal;
  8036. }
  8037. Command.Name = LoadStr(CUSTOM_COMMAND_AD_HOC_NAME);
  8038. FEditingFocusedAdHocCommand = OnFocused;
  8039. bool LocalSide = (FCurrentSide == osLocal);
  8040. int Options =
  8041. FLAGMASK((!RemoteAllowed || LocalSide), ccoDisableRemote) |
  8042. FLAGMASK(LocalSide, ccoDisableRemoteFiles);
  8043. if (DoCustomCommandDialog(Command, WinConfiguration->CustomCommandList,
  8044. ccmAdHoc, Options, AdHocCustomCommandValidate, NULL))
  8045. {
  8046. FLastCustomCommand = Command;
  8047. UpdateCustomCommandsToolbar();
  8048. ExecuteFileOperation(foCustomCommand, osRemote, OnFocused, false, &Command);
  8049. }
  8050. }
  8051. //---------------------------------------------------------------------------
  8052. void __fastcall TCustomScpExplorerForm::LastCustomCommand(bool OnFocused)
  8053. {
  8054. DebugAssert(!FLastCustomCommand.Command.IsEmpty());
  8055. int State = CustomCommandState(FLastCustomCommand, OnFocused, ccltAll);
  8056. DebugAssert(State > 0);
  8057. if (State <= 0)
  8058. {
  8059. throw Exception(FMTLOAD(CUSTOM_COMMAND_IMPOSSIBLE, (FLastCustomCommand.Command)));
  8060. }
  8061. ExecuteFileOperation(foCustomCommand, osRemote, OnFocused, false, &FLastCustomCommand);
  8062. }
  8063. //---------------------------------------------------------------------------
  8064. bool __fastcall TCustomScpExplorerForm::GetLastCustomCommand(bool OnFocused,
  8065. TCustomCommandType & Command, int & State)
  8066. {
  8067. bool Result = !FLastCustomCommand.Command.IsEmpty();
  8068. if (Result)
  8069. {
  8070. Command = FLastCustomCommand;
  8071. State = CustomCommandState(FLastCustomCommand, OnFocused, ccltAll);
  8072. }
  8073. return Result;
  8074. }
  8075. //---------------------------------------------------------------------------
  8076. void __fastcall TCustomScpExplorerForm::BeforeAction()
  8077. {
  8078. if (RemoteDirView->ItemFocused != NULL)
  8079. {
  8080. RemoteDirView->ItemFocused->CancelEdit();
  8081. }
  8082. }
  8083. //---------------------------------------------------------------------------
  8084. void __fastcall TCustomScpExplorerForm::PostComponentHide(Byte Component)
  8085. {
  8086. DebugAssert(ComponentVisible[Component]);
  8087. PostMessage(Handle, WM_COMPONENT_HIDE, Component, 0);
  8088. }
  8089. //---------------------------------------------------------------------------
  8090. void __fastcall TCustomScpExplorerForm::QueueSplitterDblClick(TObject * /*Sender*/)
  8091. {
  8092. // when queue panel is resized here directly, the status bar is stretched
  8093. // over whole space the panel occupied
  8094. PostComponentHide(fcQueueView);
  8095. }
  8096. //---------------------------------------------------------------------------
  8097. void __fastcall TCustomScpExplorerForm::Dispatch(void * Message)
  8098. {
  8099. TMessage * M = static_cast<TMessage*>(Message);
  8100. switch (M->Msg)
  8101. {
  8102. case CM_DIALOGCHAR:
  8103. CMDialogChar(*M);
  8104. break;
  8105. case WM_APPCOMMAND:
  8106. WMAppCommand(*M);
  8107. break;
  8108. case WM_SYSCOMMAND:
  8109. WMSysCommand(*M);
  8110. break;
  8111. case WM_QUERYENDSESSION:
  8112. WMQueryEndSession(*M);
  8113. break;
  8114. case WM_ENDSESSION:
  8115. WMEndSession(*reinterpret_cast<TWMEndSession *>(M));
  8116. break;
  8117. #ifdef _DEBUG
  8118. case WM_WINDOWPOSCHANGED:
  8119. WMWindowPosChanged(*reinterpret_cast<TWMWindowPosMsg *>(M));
  8120. break;
  8121. case CM_CUSTOMSTYLECHANGED:
  8122. TForm::Dispatch(Message);
  8123. break;
  8124. #endif
  8125. case WM_COMPONENT_HIDE:
  8126. {
  8127. Byte Component = static_cast<Byte>(M->WParam);
  8128. // sanity check
  8129. if (ComponentVisible[Component])
  8130. {
  8131. // special treatment
  8132. if (Component == fcQueueView)
  8133. {
  8134. ToggleQueueVisibility();
  8135. DebugAssert(!ComponentVisible[fcQueueView]);
  8136. }
  8137. else
  8138. {
  8139. ComponentVisible[Component] = false;
  8140. }
  8141. }
  8142. }
  8143. break;
  8144. case WM_COPYDATA:
  8145. WMCopyData(*M);
  8146. break;
  8147. case WM_MANAGES_CAPTION:
  8148. // caption managed in TTerminalManager::UpdateAppTitle()
  8149. M->Result = 1;
  8150. break;
  8151. case WM_WANTS_MOUSEWHEEL:
  8152. M->Result = 1;
  8153. break;
  8154. case CM_SHOWINGCHANGED:
  8155. CMShowingChanged(*M);
  8156. break;
  8157. case WM_CLOSE:
  8158. WMClose(*M);
  8159. break;
  8160. case WM_DPICHANGED:
  8161. WMDpiChanged(*M);
  8162. break;
  8163. case CM_DPICHANGED:
  8164. CMDpiChanged(*M);
  8165. break;
  8166. default:
  8167. TForm::Dispatch(Message);
  8168. break;
  8169. }
  8170. }
  8171. //---------------------------------------------------------------------------
  8172. void __fastcall TCustomScpExplorerForm::UpdateImages()
  8173. {
  8174. // noop
  8175. }
  8176. //---------------------------------------------------------------------------
  8177. void __fastcall TCustomScpExplorerForm::CMDpiChanged(TMessage & Message)
  8178. {
  8179. TForm::Dispatch(&Message);
  8180. FSessionColors->Clear();
  8181. AddFixedSessionImages();
  8182. RegenerateSessionColorsImageList(FSessionColors, FSessionColorMaskImageIndex);
  8183. UpdateImages();
  8184. }
  8185. //---------------------------------------------------------------------------
  8186. void __fastcall TCustomScpExplorerForm::WMDpiChanged(TMessage & Message)
  8187. {
  8188. TForm::Dispatch(&Message);
  8189. Configuration->Usage->Inc(L"PixelsPerInchChanged");
  8190. UpdatePixelsPerInchMainWindowCounter();
  8191. }
  8192. //---------------------------------------------------------------------------
  8193. void __fastcall TCustomScpExplorerForm::WMClose(TMessage & Message)
  8194. {
  8195. // Cannot close window while we are busy.
  8196. // We cannot test this in FormCloseQuery as that is called also from
  8197. // Close(), which is called by CloseApplicationAction. So we can be busy
  8198. // there already even, when it is legitimate to close the application.
  8199. // Possibly a better place to handle this would be WMSysCommand.
  8200. if (NonVisualDataModule->Busy)
  8201. {
  8202. Message.Result = 1;
  8203. }
  8204. else
  8205. {
  8206. TForm::Dispatch(&Message);
  8207. }
  8208. }
  8209. //---------------------------------------------------------------------------
  8210. void __fastcall TCustomScpExplorerForm::CMShowingChanged(TMessage & Message)
  8211. {
  8212. TForm::Dispatch(&Message);
  8213. if (Showing && (Terminal == NULL))
  8214. {
  8215. // When we are starting minimized (i.e. from an installer),
  8216. // postpone showing Login dialog until we get restored.
  8217. // Otherwise the Login dialog (and Authentication window) show restored
  8218. // over invidible (minimized) main window.
  8219. if (WindowState == wsMinimized)
  8220. {
  8221. FNeedSession = true;
  8222. }
  8223. else
  8224. {
  8225. // This happens before application ever goes idle, so the toolbars would
  8226. // stay enabled (initial state) until the Login dialog is dismissed.
  8227. UpdateActions();
  8228. NonVisualDataModule->StartBusy();
  8229. try
  8230. {
  8231. // Need to process WM_ACTIVATEAPP before showing the Login dialog,
  8232. // otherwise the dialog does not receive focus.
  8233. // With Commander interface the ProcessMessages is called already
  8234. // by TDriveView, but with Explorer interface, we need to call it explicily
  8235. Application->ProcessMessages();
  8236. // do not reload sessions, they have been loaded just now (optimization)
  8237. NeedSession(false);
  8238. }
  8239. __finally
  8240. {
  8241. NonVisualDataModule->EndBusy();
  8242. }
  8243. }
  8244. }
  8245. }
  8246. //---------------------------------------------------------------------------
  8247. void __fastcall TCustomScpExplorerForm::FormConstrainedResize(
  8248. TObject * /*Sender*/, int & MinWidth, int & MinHeight, int & MaxWidth,
  8249. int & MaxHeight)
  8250. {
  8251. // workaround for bug in TWinControl.CalcConstraints
  8252. // Check for empty rect (restore from iconic state) is done there only after
  8253. // call to AdjustClientRect, which enlarges the rect (for forms).
  8254. TRect R = GetClientRect();
  8255. // when restoring from iconic state, place no restrictions
  8256. if (IsRectEmpty(R))
  8257. {
  8258. MinWidth = 0;
  8259. MinHeight = 0;
  8260. MaxWidth = 32000;
  8261. MaxHeight = 32000;
  8262. }
  8263. }
  8264. //---------------------------------------------------------------------------
  8265. void __fastcall TCustomScpExplorerForm::GetSpaceAvailable(const UnicodeString Path,
  8266. TSpaceAvailable & ASpaceAvailable, bool & Close)
  8267. {
  8268. // terminal can be already closed (e.g. dropped connection)
  8269. if ((Terminal != NULL) && Terminal->IsCapable[fcCheckingSpaceAvailable])
  8270. {
  8271. Configuration->Usage->Inc(L"SpaceAvailableChecks");
  8272. try
  8273. {
  8274. Terminal->SpaceAvailable(Path, ASpaceAvailable);
  8275. }
  8276. catch(...)
  8277. {
  8278. if (!Terminal->Active)
  8279. {
  8280. Close = true;
  8281. }
  8282. throw;
  8283. }
  8284. }
  8285. }
  8286. //---------------------------------------------------------------------------
  8287. void __fastcall TCustomScpExplorerForm::FileSystemInfo()
  8288. {
  8289. const TSessionInfo & SessionInfo = Terminal->GetSessionInfo();
  8290. const TFileSystemInfo & FileSystemInfo = Terminal->GetFileSystemInfo(true);
  8291. TGetSpaceAvailable OnGetSpaceAvailable = NULL;
  8292. if (Terminal->IsCapable[fcCheckingSpaceAvailable])
  8293. {
  8294. OnGetSpaceAvailable = GetSpaceAvailable;
  8295. }
  8296. DoFileSystemInfoDialog(SessionInfo, FileSystemInfo, Terminal->CurrentDirectory,
  8297. OnGetSpaceAvailable);
  8298. }
  8299. //---------------------------------------------------------------------------
  8300. TSessionData * __fastcall TCustomScpExplorerForm::SessionDataForCode()
  8301. {
  8302. std::unique_ptr<TSessionData> Data(CloneCurrentSessionData());
  8303. Terminal->FillSessionDataForCode(Data.get());
  8304. return Data.release();
  8305. }
  8306. //---------------------------------------------------------------------------
  8307. void __fastcall TCustomScpExplorerForm::GenerateUrl(TStrings * Paths)
  8308. {
  8309. std::unique_ptr<TSessionData> Data(SessionDataForCode());
  8310. DoGenerateUrlDialog(Data.get(), Paths);
  8311. }
  8312. //---------------------------------------------------------------------------
  8313. void __fastcall TCustomScpExplorerForm::SessionGenerateUrl()
  8314. {
  8315. GenerateUrl(NULL);
  8316. }
  8317. //---------------------------------------------------------------------------
  8318. void __fastcall TCustomScpExplorerForm::FileGenerateUrl()
  8319. {
  8320. std::unique_ptr<TStrings> Paths(new TStringList());
  8321. DirView(osCurrent)->CreateFileList(false, true, Paths.get());
  8322. GenerateUrl(Paths.get());
  8323. }
  8324. //---------------------------------------------------------------------------
  8325. void __fastcall TCustomScpExplorerForm::SetSessionColor(TColor value)
  8326. {
  8327. if (value != FSessionColor)
  8328. {
  8329. FSessionColor = value;
  8330. TColor C = (value != 0 ? value : Vcl::Graphics::clNone);
  8331. TTBXColorItem * ColorItem = dynamic_cast<TTBXColorItem *>(
  8332. static_cast<TObject *>(GetComponent(fcColorMenu)));
  8333. DebugAssert(ColorItem != NULL);
  8334. ColorItem->Color = C;
  8335. NonVisualDataModule->ColorMenuItem->Color = C;
  8336. // Is null when called from LastTerminalClosed
  8337. if (Terminal != NULL)
  8338. {
  8339. SessionsPageControl->ActivePage->ImageIndex = AddSessionColor(value);
  8340. }
  8341. UpdateControls();
  8342. }
  8343. }
  8344. //---------------------------------------------------------------------------
  8345. bool __fastcall TCustomScpExplorerForm::CancelNote(bool Force)
  8346. {
  8347. bool Result = FNoteTimer->Enabled;
  8348. if (Result)
  8349. {
  8350. // cannot cancel note too early
  8351. bool NotEarly =
  8352. (Now() - FNoteShown >
  8353. EncodeTimeVerbose(0, 0, (unsigned short)(WinConfiguration->NotificationsStickTime), 0));
  8354. if (Force || NotEarly)
  8355. {
  8356. FNoteTimer->Enabled = false;
  8357. FNote = L"";
  8358. SAFE_DESTROY(FNoteData);
  8359. FOnNoteClick = NULL;
  8360. FNoteHints = FNotes->Text;
  8361. FNoteHints.Delete(FNoteHints.Length() - 1, 2);
  8362. UpdateStatusBar();
  8363. }
  8364. }
  8365. return Result;
  8366. }
  8367. //---------------------------------------------------------------------------
  8368. void __fastcall TCustomScpExplorerForm::NoteTimer(TObject * /*Sender*/)
  8369. {
  8370. DebugAssert(FNoteTimer->Enabled);
  8371. CancelNote(true);
  8372. }
  8373. //---------------------------------------------------------------------------
  8374. void __fastcall TCustomScpExplorerForm::AddNote(UnicodeString Note, bool UpdateNow)
  8375. {
  8376. int P = Note.Pos(L"\n");
  8377. if (P > 0)
  8378. {
  8379. Note.SetLength(P - 1);
  8380. }
  8381. FNotes->Add(FORMAT(L"[%s] %s",
  8382. (FormatDateTime(Configuration->TimeFormat, Now()), Note)));
  8383. while (FNotes->Count > 10)
  8384. {
  8385. FNotes->Delete(0);
  8386. }
  8387. if (UpdateNow)
  8388. {
  8389. FNoteHints = FNotes->Text;
  8390. FNoteHints.Delete(FNoteHints.Length() - 1, 2);
  8391. UpdateStatusBar();
  8392. }
  8393. }
  8394. //---------------------------------------------------------------------------
  8395. void __fastcall TCustomScpExplorerForm::PostNote(UnicodeString Note,
  8396. unsigned int Seconds, TNotifyEvent OnNoteClick, TObject * NoteData)
  8397. {
  8398. int P = Note.Pos(L"\n");
  8399. if (P > 0)
  8400. {
  8401. Note.SetLength(P - 1);
  8402. }
  8403. FNoteHints = FNotes->Text;
  8404. FNoteHints.Delete(FNoteHints.Length() - 1, 2);
  8405. FNote = Note;
  8406. SAFE_DESTROY(FNoteData);
  8407. FNoteData = NoteData;
  8408. FOnNoteClick = OnNoteClick;
  8409. AddNote(Note, false);
  8410. UpdateStatusBar();
  8411. FNoteShown = Now();
  8412. FNoteTimer->Enabled = false;
  8413. if (Seconds == 0)
  8414. {
  8415. Seconds = WinConfiguration->NotificationsTimeout;
  8416. }
  8417. FNoteTimer->Interval = Seconds * MSecsPerSec;
  8418. FNoteTimer->Enabled = true;
  8419. }
  8420. //---------------------------------------------------------------------------
  8421. void __fastcall TCustomScpExplorerForm::ReadDirectoryCancelled()
  8422. {
  8423. PostNote(LoadStr(DIRECTORY_READING_CANCELLED), 0, NULL, NULL);
  8424. }
  8425. //---------------------------------------------------------------------------
  8426. void __fastcall TCustomScpExplorerForm::SynchronizeBrowsingChanged()
  8427. {
  8428. if (NonVisualDataModule->SynchronizeBrowsingAction->Checked)
  8429. {
  8430. Configuration->Usage->Inc(L"SynchronizeBrowsingEnabled");
  8431. }
  8432. PostNote(FORMAT(LoadStrPart(SYNC_DIR_BROWSE_TOGGLE, 1),
  8433. (LoadStrPart(SYNC_DIR_BROWSE_TOGGLE,
  8434. (NonVisualDataModule->SynchronizeBrowsingAction->Checked ? 2 : 3)))),
  8435. 0, NULL, NULL);
  8436. }
  8437. //---------------------------------------------------------------------------
  8438. void __fastcall TCustomScpExplorerForm::ToggleShowHiddenFiles()
  8439. {
  8440. WinConfiguration->ShowHiddenFiles = !WinConfiguration->ShowHiddenFiles;
  8441. PostNote(FORMAT(LoadStrPart(SHOW_HIDDEN_FILES_TOGGLE, 1),
  8442. (LoadStrPart(SHOW_HIDDEN_FILES_TOGGLE,
  8443. (WinConfiguration->ShowHiddenFiles ? 2 : 3)))), 0, NULL, NULL);
  8444. }
  8445. //---------------------------------------------------------------------------
  8446. void __fastcall TCustomScpExplorerForm::SetFormatSizeBytes(TFormatBytesStyle Style)
  8447. {
  8448. WinConfiguration->FormatSizeBytes = Style;
  8449. }
  8450. //---------------------------------------------------------------------------
  8451. void __fastcall TCustomScpExplorerForm::ToggleAutoReadDirectoryAfterOp()
  8452. {
  8453. Configuration->AutoReadDirectoryAfterOp = !Configuration->AutoReadDirectoryAfterOp;
  8454. PostNote(FORMAT(LoadStrPart(AUTO_READ_DIRECTORY_TOGGLE, 1),
  8455. (LoadStrPart(AUTO_READ_DIRECTORY_TOGGLE,
  8456. (Configuration->AutoReadDirectoryAfterOp ? 2 : 3)))), 0, NULL, NULL);
  8457. }
  8458. //---------------------------------------------------------------------------
  8459. void __fastcall TCustomScpExplorerForm::StatusBarPanelDblClick(
  8460. TTBXCustomStatusBar * Sender, TTBXStatusPanel * Panel)
  8461. {
  8462. if (Panel->Index == 0)
  8463. {
  8464. if (FOnNoteClick != NULL)
  8465. {
  8466. // prevent the user data from being freed by possible call
  8467. // to CancelNote or PostNote during call to OnNoteClick
  8468. std::unique_ptr<TObject> NoteData(FNoteData);
  8469. TNotifyEvent OnNoteClick = FOnNoteClick;
  8470. FNoteData = NULL;
  8471. // need to cancel the note as we are going to delete its user data
  8472. CancelNote(true);
  8473. OnNoteClick(NoteData.get());
  8474. }
  8475. }
  8476. if (Panel->Index >= Sender->Panels->Count - SessionPanelCount)
  8477. {
  8478. FileSystemInfo();
  8479. }
  8480. }
  8481. //---------------------------------------------------------------------------
  8482. void __fastcall TCustomScpExplorerForm::LockWindow(bool Force)
  8483. {
  8484. // workaround:
  8485. // 1) for unknown reason, disabling window, while minimized,
  8486. // prevents it from restoring, even if it was enabled again meanwhile
  8487. // 2) when disabling the main window, while another has focus
  8488. // minimize is no longer possible
  8489. // ("keep up to date" dialog - though does not seem to be the case with "find file" -
  8490. // so it has probably something to do with the semi-modalness of the "keep up to date" dialog)
  8491. // Shouldn't we use IsApplicationMinimized() here?
  8492. if ((FLockSuspendLevel == 0) && !IsIconic(Application->Handle) && (Force || (Screen->ActiveForm == this)))
  8493. {
  8494. Enabled = false;
  8495. }
  8496. FLockLevel++;
  8497. }
  8498. //---------------------------------------------------------------------------
  8499. void __fastcall TCustomScpExplorerForm::UnlockWindow()
  8500. {
  8501. DebugAssert(FLockLevel > 0);
  8502. FLockLevel--;
  8503. if (FLockLevel == 0)
  8504. {
  8505. DebugAssert(FLockSuspendLevel == 0);
  8506. Enabled = true;
  8507. }
  8508. }
  8509. //---------------------------------------------------------------------------
  8510. void __fastcall TCustomScpExplorerForm::SuspendWindowLock()
  8511. {
  8512. // We need to make sure that window is enabled when the last modal window closes
  8513. // otherwise focus is not restored correctly.
  8514. // So we re-enable the window when modal window opens and
  8515. // disable it back after is closes
  8516. if (FLockLevel > 0)
  8517. {
  8518. // while we have nesting counter, we know that we never be called
  8519. // recursivelly as Application->OnModalBegin is called only
  8520. // for the top-level modal window
  8521. if (DebugAlwaysTrue(FLockSuspendLevel == 0))
  8522. {
  8523. // won't be disabled when conditions in LockWindow() were not satisfied
  8524. FDisabledOnLockSuspend = !Enabled;
  8525. // When minimized to tray (or actually when set to SW_HIDE),
  8526. // setting Enabled makes the window focusable even when there's
  8527. // modal window over it
  8528. if (!FTrayIcon->Visible)
  8529. {
  8530. Enabled = true;
  8531. }
  8532. }
  8533. FLockSuspendLevel++;
  8534. }
  8535. }
  8536. //---------------------------------------------------------------------------
  8537. void __fastcall TCustomScpExplorerForm::ResumeWindowLock()
  8538. {
  8539. if (FLockSuspendLevel > 0)
  8540. {
  8541. DebugAssert(FLockLevel > 0);
  8542. FLockSuspendLevel--;
  8543. // see comment in SuspendWindowLock
  8544. if (DebugAlwaysTrue(FLockSuspendLevel == 0))
  8545. {
  8546. // Note that window can be enabled here, when we were minized to tray when
  8547. // was SuspendWindowLock() called.
  8548. // We should possibly do the same check as in LockWindow(),
  8549. // if it is ever possible that the consitions change between
  8550. // SuspendWindowLock() and ResumeWindowLock()
  8551. Enabled = !FDisabledOnLockSuspend;
  8552. }
  8553. }
  8554. }
  8555. //---------------------------------------------------------------------------
  8556. void __fastcall TCustomScpExplorerForm::UpdateRemotePathComboBox(bool TextOnly)
  8557. {
  8558. if (!TextOnly)
  8559. {
  8560. TTBXComboBoxItem * RemotePathComboBox =
  8561. reinterpret_cast<TTBXComboBoxItem *>(GetComponent(fcRemotePathComboBox));
  8562. TStrings * Items = RemotePathComboBox->Strings;
  8563. Items->BeginUpdate();
  8564. try
  8565. {
  8566. Items->Clear();
  8567. if (Terminal != NULL)
  8568. {
  8569. UnicodeString APath = UnixExcludeTrailingBackslash(RemoteDirView->Path);
  8570. while (!IsUnixRootPath(APath))
  8571. {
  8572. int P = APath.LastDelimiter(L'/');
  8573. DebugAssert(P >= 0);
  8574. Items->Insert(0, APath.SubString(P + 1, APath.Length() - P));
  8575. APath.SetLength(P - 1);
  8576. }
  8577. Items->Insert(0, Customunixdirview_SUnixDefaultRootName);
  8578. }
  8579. }
  8580. __finally
  8581. {
  8582. RemotePathComboBox->ItemIndex = Items->Count - 1;
  8583. // Setting ItemIndex to -1 does not reset its text
  8584. if (Items->Count == 0)
  8585. {
  8586. RemotePathComboBox->Text = L"";
  8587. }
  8588. Items->EndUpdate();
  8589. }
  8590. }
  8591. }
  8592. //---------------------------------------------------------------------------
  8593. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxAdjustImageIndex(
  8594. TTBXComboBoxItem * Sender, const UnicodeString /*AText*/, int AIndex,
  8595. int & ImageIndex)
  8596. {
  8597. if (AIndex < 0)
  8598. {
  8599. AIndex = Sender->ItemIndex;
  8600. }
  8601. ImageIndex = (AIndex < Sender->Strings->Count - 1 ? StdDirIcon : StdDirSelIcon);
  8602. }
  8603. //---------------------------------------------------------------------------
  8604. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxDrawItem(
  8605. TTBXCustomList * /*Sender*/, TCanvas * /*ACanvas*/, TRect & ARect, int AIndex,
  8606. int /*AHoverIndex*/, bool & /*DrawDefault*/)
  8607. {
  8608. ARect.Left += (10 * AIndex);
  8609. }
  8610. //---------------------------------------------------------------------------
  8611. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxMeasureWidth(
  8612. TTBXCustomList * /*Sender*/, TCanvas * /*ACanvas*/, int AIndex, int &AWidth)
  8613. {
  8614. AWidth += (10 * AIndex);
  8615. }
  8616. //---------------------------------------------------------------------------
  8617. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxItemClick(
  8618. TObject * Sender)
  8619. {
  8620. TTBXComboBoxItem * RemotePathComboBox = dynamic_cast<TTBXComboBoxItem*>(Sender);
  8621. UnicodeString APath = UnixExcludeTrailingBackslash(RemoteDirView->Path);
  8622. int Index = RemotePathComboBox->ItemIndex;
  8623. while (Index < RemotePathComboBox->Strings->Count - 1)
  8624. {
  8625. APath = UnixExtractFileDir(APath);
  8626. Index++;
  8627. }
  8628. // VanDyke style paths
  8629. if (APath.IsEmpty())
  8630. {
  8631. DebugAssert(RemotePathComboBox->ItemIndex == 0);
  8632. APath = ROOTDIRECTORY;
  8633. }
  8634. if (RemoteDirView->Path != APath)
  8635. {
  8636. RemoteDirView->Path = APath;
  8637. }
  8638. }
  8639. //---------------------------------------------------------------------------
  8640. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxCancel(TObject * Sender)
  8641. {
  8642. DebugAssert(Sender == GetComponent(fcRemotePathComboBox));
  8643. DebugUsedParam(Sender);
  8644. UpdateRemotePathComboBox(true);
  8645. }
  8646. //---------------------------------------------------------------------------
  8647. void __fastcall TCustomScpExplorerForm::DirViewEditing(
  8648. TObject * Sender, TListItem * Item, bool & /*AllowEdit*/)
  8649. {
  8650. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  8651. DebugAssert(DirView != NULL);
  8652. if (!WinConfiguration->RenameWholeName && !DirView->ItemIsDirectory(Item))
  8653. {
  8654. HWND Edit = ListView_GetEditControl(DirView->Handle);
  8655. // OnEditing is called also from TCustomListView::CanEdit
  8656. if (Edit != NULL)
  8657. {
  8658. EditSelectBaseName(Edit);
  8659. }
  8660. }
  8661. }
  8662. //---------------------------------------------------------------------------
  8663. //---------------------------------------------------------------------------
  8664. TDragDropFilesEx * __fastcall TCustomScpExplorerForm::CreateDragDropFilesEx()
  8665. {
  8666. TDragDropFilesEx * Result = new TDragDropFilesEx(this);
  8667. Result->AutoDetectDnD = false;
  8668. Result->NeedValid = TFileExMustDnDSet() << nvFilename;
  8669. Result->RenderDataOn = rdoEnterAndDropSync;
  8670. Result->TargetEffects = TDropEffectSet() << deCopy << deMove;
  8671. return Result;
  8672. }
  8673. //---------------------------------------------------------------------------
  8674. void __fastcall TCustomScpExplorerForm::CreateWnd()
  8675. {
  8676. TForm::CreateWnd();
  8677. if (FSessionsDragDropFilesEx == NULL)
  8678. {
  8679. FSessionsDragDropFilesEx = CreateDragDropFilesEx();
  8680. FSessionsDragDropFilesEx->OnDragOver = SessionsDDDragOver;
  8681. FSessionsDragDropFilesEx->OnProcessDropped = SessionsDDProcessDropped;
  8682. FSessionsDragDropFilesEx->OnDragEnter = SessionsDDDragEnter;
  8683. FSessionsDragDropFilesEx->OnDragLeave = SessionsDDDragLeave;
  8684. }
  8685. if (FQueueDragDropFilesEx == NULL)
  8686. {
  8687. FQueueDragDropFilesEx = CreateDragDropFilesEx();
  8688. // No need to set OnDragOver as we do not have any restrictions
  8689. FQueueDragDropFilesEx->OnProcessDropped = QueueDDProcessDropped;
  8690. FQueueDragDropFilesEx->OnDragEnter = QueueDDDragEnter;
  8691. FQueueDragDropFilesEx->OnDragLeave = QueueDDDragLeave;
  8692. }
  8693. }
  8694. //---------------------------------------------------------------------------
  8695. void __fastcall TCustomScpExplorerForm::DestroyWnd()
  8696. {
  8697. TForm::DestroyWnd();
  8698. FSessionsDragDropFilesEx->DragDropControl = NULL;
  8699. FQueueDragDropFilesEx->DragDropControl = NULL;
  8700. }
  8701. //---------------------------------------------------------------------------
  8702. void __fastcall TCustomScpExplorerForm::FormShow(TObject * /*Sender*/)
  8703. {
  8704. SideEnter(FCurrentSide);
  8705. FEverShown = true;
  8706. }
  8707. //---------------------------------------------------------------------------
  8708. void __fastcall TCustomScpExplorerForm::DoFindFiles(
  8709. TTerminal * ATerminal, UnicodeString Directory, const TFileMasks & FileMask,
  8710. TFileFoundEvent OnFileFound, TFindingFileEvent OnFindingFile)
  8711. {
  8712. if (!NonVisualDataModule->Busy)
  8713. {
  8714. TTerminalManager::Instance()->ActiveTerminal = ATerminal;
  8715. Configuration->Usage->Inc(L"FileFinds");
  8716. LockWindow(true);
  8717. NonVisualDataModule->StartBusy();
  8718. WinConfiguration->LockedInterface = true;
  8719. try
  8720. {
  8721. FTerminal->FilesFind(Directory, FileMask, OnFileFound, OnFindingFile);
  8722. }
  8723. __finally
  8724. {
  8725. WinConfiguration->LockedInterface = false;
  8726. NonVisualDataModule->EndBusy();
  8727. UnlockWindow();
  8728. }
  8729. }
  8730. }
  8731. //---------------------------------------------------------------------------
  8732. void __fastcall TCustomScpExplorerForm::DoFocusRemotePath(TTerminal * ATerminal, const UnicodeString & Path)
  8733. {
  8734. if (!NonVisualDataModule->Busy)
  8735. {
  8736. TTerminalManager::Instance()->ActiveTerminal = ATerminal;
  8737. SetFocus();
  8738. RemoteDirView->Path = UnixExtractFilePath(Path);
  8739. TListItem * Item = RemoteDirView->FindFileItem(UnixExtractFileName(Path));
  8740. if (Item != NULL)
  8741. {
  8742. RemoteDirView->ItemFocused = Item;
  8743. RemoteDirView->ItemFocused->MakeVisible(false);
  8744. RemoteDirView->SetFocus();
  8745. }
  8746. }
  8747. }
  8748. //---------------------------------------------------------------------------
  8749. bool __fastcall TCustomScpExplorerForm::CanOperateOnFoundFiles(TTerminal * ATerminal)
  8750. {
  8751. bool Result = !NonVisualDataModule->Busy;
  8752. if (Result)
  8753. {
  8754. TTerminalManager::Instance()->ActiveTerminal = ATerminal;
  8755. }
  8756. return Result;
  8757. }
  8758. //---------------------------------------------------------------------------
  8759. void __fastcall TCustomScpExplorerForm::DoOperationOnFoundFiles(
  8760. TFileOperation Operation, TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  8761. {
  8762. if (CanOperateOnFoundFiles(ATerminal))
  8763. {
  8764. TValueRestorer<TFileOperationFinishedEvent> OnFileOperationFinishedRestorer(FOnFileOperationFinished);
  8765. FOnFileOperationFinished = OnFileOperationFinished;
  8766. ExecuteFileOperation(Operation, osRemote, FileList, false, NULL);
  8767. }
  8768. }
  8769. //---------------------------------------------------------------------------
  8770. void __fastcall TCustomScpExplorerForm::DoDeleteFoundFiles(
  8771. TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  8772. {
  8773. DoOperationOnFoundFiles(foDelete, ATerminal, FileList, OnFileOperationFinished);
  8774. }
  8775. //---------------------------------------------------------------------------
  8776. void __fastcall TCustomScpExplorerForm::DoDownloadFoundFiles(
  8777. TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  8778. {
  8779. DoOperationOnFoundFiles(foCopy, ATerminal, FileList, OnFileOperationFinished);
  8780. }
  8781. //---------------------------------------------------------------------------
  8782. void __fastcall TCustomScpExplorerForm::DoEditFoundFiles(
  8783. TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  8784. {
  8785. if (CanOperateOnFoundFiles(ATerminal))
  8786. {
  8787. for (int Index = 0; Index < FileList->Count; Index++)
  8788. {
  8789. UnicodeString FileName = FileList->Strings[Index];
  8790. TRemoteFile * File = static_cast<TRemoteFile *>(FileList->Objects[Index]);
  8791. ExecuteRemoteFile(FileName, File, efDefaultEditor);
  8792. OnFileOperationFinished(FileName, true);
  8793. }
  8794. }
  8795. }
  8796. //---------------------------------------------------------------------------
  8797. void __fastcall TCustomScpExplorerForm::RemoteFindFiles()
  8798. {
  8799. FFileFindTerminal = Terminal;
  8800. ShowFileFindDialog(
  8801. Terminal, RemoteDirView->Path, DoFindFiles, DoFocusRemotePath, DoDeleteFoundFiles,
  8802. DoDownloadFoundFiles, DoEditFoundFiles);
  8803. }
  8804. //---------------------------------------------------------------------------
  8805. void __fastcall TCustomScpExplorerForm::UpdateTaskbarList(ITaskbarList3 * TaskbarList)
  8806. {
  8807. FTaskbarList = TaskbarList;
  8808. }
  8809. //---------------------------------------------------------------------------
  8810. void __fastcall TCustomScpExplorerForm::SessionsPageControlMouseDown(
  8811. TObject * /*Sender*/, TMouseButton Button, TShiftState /*Shift*/, int X, int Y)
  8812. {
  8813. int Index = SessionsPageControl->IndexOfTabAt(X, Y);
  8814. if (Index >= 0)
  8815. {
  8816. if (Button == mbLeft)
  8817. {
  8818. // "Mouse down" is raised only after tab is switched.
  8819. // If switching tab (switching session) takes long enough for user
  8820. // to actually release the button, "mouse down" is still raised,
  8821. // but we do not get "mouse up" event, so dragging is not cancelled,
  8822. // prevent that by not beginning dragging in the first place.
  8823. if (IsKeyPressed(VK_LBUTTON))
  8824. {
  8825. // when user clicks the "+", we get mouse down only after the session
  8826. // is closed, when new session tab is already on X:Y, so dragging
  8827. // starts, prevent that
  8828. if (MilliSecondsBetween(Now(), FSessionsPageControlNewSessionTime) > 500)
  8829. {
  8830. TTerminal * Terminal = GetSessionTabTerminal(SessionsPageControl->Pages[Index]);
  8831. if (Terminal != NULL)
  8832. {
  8833. SessionsPageControl->BeginDrag(false);
  8834. }
  8835. }
  8836. }
  8837. }
  8838. else if (Button == mbMiddle)
  8839. {
  8840. // ignore middle-click for "New session tab"
  8841. TTerminal * Terminal = GetSessionTabTerminal(SessionsPageControl->Pages[Index]);
  8842. if (Terminal != NULL)
  8843. {
  8844. CloseSessionTab(Index);
  8845. }
  8846. }
  8847. }
  8848. }
  8849. //---------------------------------------------------------------------------
  8850. void __fastcall TCustomScpExplorerForm::SessionsPageControlDragDrop(
  8851. TObject * /*Sender*/, TObject * /*Source*/, int X, int Y)
  8852. {
  8853. int Index = SessionsPageControl->IndexOfTabAt(X, Y);
  8854. // do not allow dropping on the "+" tab
  8855. TTerminal * TargetTerminal = GetSessionTabTerminal(SessionsPageControl->Pages[Index]);
  8856. if ((TargetTerminal != NULL) &&
  8857. (SessionsPageControl->ActivePage->PageIndex != Index))
  8858. {
  8859. Configuration->Usage->Inc(L"SessionTabMoves");
  8860. // this is almost redundant as we would recreate tabs in DoTerminalListChanged,
  8861. // but we want to actually prevent that to avoid flicker
  8862. SessionsPageControl->ActivePage->PageIndex = Index;
  8863. TTerminal * Terminal = GetSessionTabTerminal(SessionsPageControl->ActivePage);
  8864. TTerminalManager::Instance()->Move(Terminal, TargetTerminal);
  8865. }
  8866. }
  8867. //---------------------------------------------------------------------------
  8868. void __fastcall TCustomScpExplorerForm::SessionsPageControlDragOver(
  8869. TObject * Sender, TObject * Source, int X, int Y,
  8870. TDragState /*State*/, bool & Accept)
  8871. {
  8872. Accept = (Sender == Source);
  8873. if (Accept)
  8874. {
  8875. int Index = SessionsPageControl->IndexOfTabAt(X, Y);
  8876. TTerminal * Terminal = GetSessionTabTerminal(SessionsPageControl->Pages[Index]);
  8877. // do not allow dragging to the "+" tab
  8878. Accept = (Terminal != NULL);
  8879. }
  8880. }
  8881. //---------------------------------------------------------------------------
  8882. void __fastcall TCustomScpExplorerForm::SessionsDDDragOver(int /*KeyState*/,
  8883. const TPoint & Point, int & Effect)
  8884. {
  8885. int Index = SessionsPageControl->IndexOfTabAt(Point.X, Point.Y);
  8886. if (Index < 0)
  8887. {
  8888. Effect = DROPEFFECT_None;
  8889. }
  8890. else
  8891. {
  8892. TTerminal * TargetTerminal = GetSessionTabTerminal(SessionsPageControl->Pages[Index]);
  8893. // do not allow dropping on the "+" tab
  8894. if (TargetTerminal == NULL)
  8895. {
  8896. Effect = DROPEFFECT_None;
  8897. }
  8898. else if ((TargetTerminal != Terminal) && (Effect == DROPEFFECT_Move))
  8899. {
  8900. Effect = DROPEFFECT_Copy;
  8901. }
  8902. }
  8903. }
  8904. //---------------------------------------------------------------------------
  8905. void __fastcall TCustomScpExplorerForm::SessionsDDProcessDropped(
  8906. TObject * /*Sender*/, int /*KeyState*/, const TPoint & Point, int Effect)
  8907. {
  8908. int Index = SessionsPageControl->IndexOfTabAt(Point.X, Point.Y);
  8909. // do not allow dropping on the "+" tab
  8910. TTerminal * TargetTerminal = GetSessionTabTerminal(SessionsPageControl->Pages[Index]);
  8911. if (TargetTerminal != NULL)
  8912. {
  8913. DebugAssert(!IsFileControl(DropSourceControl, osRemote));
  8914. if (!IsFileControl(DropSourceControl, osRemote))
  8915. {
  8916. TTerminalManager::Instance()->ActiveTerminal = TargetTerminal;
  8917. RemoteFileControlDragDropFileOperation(SessionsPageControl, Effect,
  8918. // Why don't we use Terminal->CurrentDirectory directly?
  8919. TTerminalManager::Instance()->ActiveTerminal->CurrentDirectory,
  8920. // do not force queue, drag drop
  8921. false, true);
  8922. }
  8923. }
  8924. }
  8925. //---------------------------------------------------------------------------
  8926. void __fastcall TCustomScpExplorerForm::QueueDDProcessDropped(
  8927. TObject * /*Sender*/, int /*KeyState*/, const TPoint & /*Point*/, int Effect)
  8928. {
  8929. // Downloads are handled in RemoteFileControlDDEnd
  8930. if (!IsFileControl(DropSourceControl, osRemote))
  8931. {
  8932. RemoteFileControlDragDropFileOperation(QueueView3, Effect,
  8933. Terminal->CurrentDirectory,
  8934. // force queue, drag drop
  8935. true, true);
  8936. }
  8937. }
  8938. //---------------------------------------------------------------------------
  8939. void __fastcall TCustomScpExplorerForm::FormClose(TObject * /*Sender*/, TCloseAction & /*Action*/)
  8940. {
  8941. FShowing = false;
  8942. // Do not save empty workspace
  8943. if (WinConfiguration->AutoSaveWorkspace && (Terminal != NULL))
  8944. {
  8945. std::unique_ptr<TObjectList> DataList(DoCollectWorkspace());
  8946. UnicodeString Name = WorkspaceName();
  8947. DoSaveWorkspace(
  8948. Name, DataList.get(),
  8949. !Configuration->DisablePasswordStoring &&
  8950. WinConfiguration->AutoSaveWorkspacePasswords, false);
  8951. WinConfiguration->LastStoredSession = Name;
  8952. }
  8953. }
  8954. //---------------------------------------------------------------------------
  8955. void __fastcall TCustomScpExplorerForm::RemoteDirViewRead(TObject * /*Sender*/)
  8956. {
  8957. TManagedTerminal * ManagedTerminal =
  8958. dynamic_cast<TManagedTerminal *>(RemoteDirView->Terminal);
  8959. if (ManagedTerminal != NULL)
  8960. {
  8961. ManagedTerminal->DirectoryLoaded = Now();
  8962. }
  8963. }
  8964. //---------------------------------------------------------------------------
  8965. void __fastcall TCustomScpExplorerForm::DirViewSelectItem(TObject * Sender,
  8966. TListItem * /*Item*/, bool /*Selected*/)
  8967. {
  8968. TCustomDirView * DirView = reinterpret_cast<TCustomDirView *>(Sender);
  8969. switch (DirView->LastSelectMethod)
  8970. {
  8971. case smKeyboard:
  8972. Configuration->Usage->Inc(L"KeyboardSelections");
  8973. break;
  8974. case smMouse:
  8975. Configuration->Usage->Inc(L"MouseSelections");
  8976. break;
  8977. }
  8978. }
  8979. //---------------------------------------------------------------------------
  8980. int __fastcall TCustomScpExplorerForm::AddSessionColor(TColor Color)
  8981. {
  8982. if (Color != 0)
  8983. {
  8984. return GetSessionColorImage(FSessionColors, Color, FSessionColorMaskImageIndex);
  8985. }
  8986. else
  8987. {
  8988. return FSessionTabImageIndex;
  8989. }
  8990. }
  8991. //---------------------------------------------------------------------------
  8992. int __fastcall TCustomScpExplorerForm::AddFixedSessionImage(int GlyphsSourceIndex)
  8993. {
  8994. TPngImageCollectionItem * Item =
  8995. GlyphsModule->ExplorerImages->PngImages->Items[GlyphsSourceIndex];
  8996. FSessionColors->AddPng(Item->PngImage);
  8997. return FSessionColors->Count - 1;
  8998. }
  8999. //---------------------------------------------------------------------------
  9000. void __fastcall TCustomScpExplorerForm::AddFixedSessionImages()
  9001. {
  9002. FSessionColors->SetSize(GlyphsModule->ExplorerImages->Width, GlyphsModule->ExplorerImages->Height);
  9003. FNewSessionTabImageIndex = AddFixedSessionImage(NonVisualDataModule->NewSessionAction->ImageIndex);
  9004. FSessionTabImageIndex = AddFixedSessionImage(SiteImageIndex);
  9005. FSessionColorMaskImageIndex = AddFixedSessionImage(SiteColorMaskImageIndex);
  9006. }
  9007. //---------------------------------------------------------------------------
  9008. void __fastcall TCustomScpExplorerForm::CollectItemsWithTextDisplayMode(TWinControl * Control)
  9009. {
  9010. for (int Index = 0; Index < Control->ControlCount; Index++)
  9011. {
  9012. TControl * AControl = Control->Controls[Index];
  9013. TWinControl * WinControl = dynamic_cast<TWinControl *>(AControl);
  9014. if (WinControl != NULL)
  9015. {
  9016. CollectItemsWithTextDisplayMode(WinControl);
  9017. }
  9018. TTBCustomToolbar * Toolbar = dynamic_cast<TTBCustomToolbar *>(AControl);
  9019. if (Toolbar != NULL)
  9020. {
  9021. // we care for top-level items only
  9022. TTBCustomItem * Items = Toolbar->Items;
  9023. for (int ItemIndex = 0; ItemIndex < Items->Count; ItemIndex++)
  9024. {
  9025. TTBCustomItem * Item = Items->Items[ItemIndex];
  9026. if (((Item->DisplayMode == nbdmImageAndText) ||
  9027. (dynamic_cast<TTBXLabelItem *>(Item) != NULL)) &&
  9028. EligibleForImageDisplayMode(Item))
  9029. {
  9030. FItemsWithTextDisplayMode.insert(Item);
  9031. }
  9032. }
  9033. }
  9034. }
  9035. }
  9036. //---------------------------------------------------------------------------
  9037. bool __fastcall TCustomScpExplorerForm::EligibleForImageDisplayMode(TTBCustomItem * /*Item*/)
  9038. {
  9039. return true;
  9040. }
  9041. //---------------------------------------------------------------------------
  9042. bool __fastcall TCustomScpExplorerForm::UpdateToolbarDisplayMode()
  9043. {
  9044. bool SelectiveToolbarText = WinConfiguration->SelectiveToolbarText;
  9045. TTBItemDisplayMode DisplayMode = (SelectiveToolbarText ? nbdmImageAndText : nbdmDefault);
  9046. typedef std::set<TTBCustomItem *> TItemsWithTextDisplayMode;
  9047. TItemsWithTextDisplayMode::iterator i = FItemsWithTextDisplayMode.begin();
  9048. bool Result = true;
  9049. while (Result && (i != FItemsWithTextDisplayMode.end()))
  9050. {
  9051. TTBCustomItem * Item = *i;
  9052. TTBXLabelItem * Label = dynamic_cast<TTBXLabelItem *>(Item);
  9053. if (Label != NULL)
  9054. {
  9055. // optimization
  9056. if (Label->Visible == SelectiveToolbarText)
  9057. {
  9058. Result = false;
  9059. }
  9060. else
  9061. {
  9062. Label->Visible = SelectiveToolbarText;
  9063. }
  9064. }
  9065. else
  9066. {
  9067. // optimization
  9068. if (Item->DisplayMode == DisplayMode)
  9069. {
  9070. Result = false;
  9071. }
  9072. else
  9073. {
  9074. Item->DisplayMode = DisplayMode;
  9075. }
  9076. }
  9077. ++i;
  9078. }
  9079. if (Result)
  9080. {
  9081. UpdateNewSessionTab();
  9082. }
  9083. return Result;
  9084. }
  9085. //---------------------------------------------------------------------------
  9086. void __fastcall TCustomScpExplorerForm::DisplaySystemContextMenu()
  9087. {
  9088. DebugFail();
  9089. }
  9090. //---------------------------------------------------------------------------
  9091. bool __fastcall TCustomScpExplorerForm::IsBusy()
  9092. {
  9093. // Among other, a lock level is non zero, while directory is being loaded,
  9094. // even when it happens as a result of dirview navigation,
  9095. // i.e. when TNonVisualDataModule is NOT FBusy.
  9096. // That's why the TNonVisualDataModule::GetBusy calls this method.
  9097. // Among other this prevents a panel auto update to occur while
  9098. // directory is changing.
  9099. return (FLockLevel > 0) || DirView(osCurrent)->IsEditing();
  9100. }
  9101. //---------------------------------------------------------------------------
  9102. Boolean __fastcall TCustomScpExplorerForm::AllowedAction(TAction * /*Action*/, TActionAllowed Allowed)
  9103. {
  9104. // While the window is disabled, we still seem to process menu shortcuts at least,
  9105. // so stop it at least here.
  9106. // See also TCustomScpExplorerForm::RemoteDirViewBusy
  9107. return
  9108. (Allowed == aaUpdate) ||
  9109. !NonVisualDataModule->Busy;
  9110. }
  9111. //---------------------------------------------------------------------------
  9112. void __fastcall TCustomScpExplorerForm::EditMenuItemPopup(TTBCustomItem * Sender, bool FromLink)
  9113. {
  9114. NonVisualDataModule->EditMenuItemPopup(Sender, FromLink);
  9115. }
  9116. //---------------------------------------------------------------------------
  9117. void __fastcall TCustomScpExplorerForm::DirViewBusy(TObject * /*Sender*/, int Busy, bool & State)
  9118. {
  9119. // This is somewhat redundant to LockWindow() call from
  9120. // TTerminalManager::TerminalReadDirectoryProgress.
  9121. // But disabling window is known not to block keyboard shorcuts
  9122. // (see TCustomScpExplorerForm::AllowedAction), this hopefully works.
  9123. if (Busy > 0)
  9124. {
  9125. if (NonVisualDataModule->Busy)
  9126. {
  9127. State = false;
  9128. }
  9129. else
  9130. {
  9131. NonVisualDataModule->StartBusy();
  9132. // Will be countered for TCustomDirView.DoExecute in DoDirViewExecFile
  9133. LockWindow();
  9134. }
  9135. }
  9136. else if (Busy < 0)
  9137. {
  9138. UnlockWindow();
  9139. NonVisualDataModule->EndBusy();
  9140. }
  9141. else
  9142. {
  9143. State = NonVisualDataModule->Busy;
  9144. }
  9145. }
  9146. //---------------------------------------------------------------------------
  9147. void __fastcall TCustomScpExplorerForm::SessionsPageControlContextPopup(TObject * /*Sender*/, TPoint & MousePos, bool & Handled)
  9148. {
  9149. int Index = SessionsPageControl->IndexOfTabAt(MousePos.X, MousePos.Y);
  9150. // no context menu for "New session tab"
  9151. if ((Index >= 0) && (GetSessionTabTerminal(SessionsPageControl->Pages[Index]) != NULL))
  9152. {
  9153. SessionsPageControl->ActivePageIndex = Index;
  9154. if (DebugAlwaysTrue(SessionTabSwitched()))
  9155. {
  9156. // copied from TControl.WMContextMenu
  9157. SendCancelMode(SessionsPageControl);
  9158. // explicit popup instead of using PopupMenu property
  9159. // to avoid menu to popup somewhere within SessionTabSwitched above,
  9160. // while connecting yet not-connected session and hence
  9161. // allowing an access to commands over not-completelly connected session
  9162. TPoint Point = SessionsPageControl->ClientToScreen(MousePos);
  9163. TPopupMenu * PopupMenu = NonVisualDataModule->SessionsPopup;
  9164. PopupMenu->PopupComponent = SessionsPageControl;
  9165. PopupMenu->Popup(Point.x, Point.y);
  9166. }
  9167. }
  9168. Handled = true;
  9169. }
  9170. //---------------------------------------------------------------------------
  9171. bool __fastcall TCustomScpExplorerForm::CanChangePassword()
  9172. {
  9173. return (Terminal != NULL) && Terminal->Active && Terminal->IsCapable[fcChangePassword];
  9174. }
  9175. //---------------------------------------------------------------------------
  9176. void __fastcall TCustomScpExplorerForm::ChangePassword()
  9177. {
  9178. Configuration->Usage->Inc(L"PasswordChanges");
  9179. std::unique_ptr<TSessionData> ChangePasswordData(Terminal->SessionData->Clone());
  9180. ChangePasswordData->DisableAuthentationsExceptPassword();
  9181. ChangePasswordData->ChangePassword = true;
  9182. std::unique_ptr<TTerminal> ChangePasswordSession(
  9183. Terminal->CreateSecondarySession(L"ChangePassword", ChangePasswordData.get()));
  9184. ChangePasswordSession->AutoReadDirectory = false;
  9185. if (TTerminalManager::Instance()->ConnectTerminal(ChangePasswordSession.get()))
  9186. {
  9187. MessageDialog(MainInstructions(LoadStr(PASSWORD_CHANGED)), qtInformation, qaOK, HELP_CHANGE_PASSWORD);
  9188. }
  9189. else
  9190. {
  9191. Configuration->Usage->Inc(L"PasswordChangesFailed");
  9192. }
  9193. }
  9194. //---------------------------------------------------------------------------
  9195. bool __fastcall TCustomScpExplorerForm::CanPrivateKeyUpload()
  9196. {
  9197. // No nice way to assert SSH2
  9198. return (Terminal != NULL) && Terminal->Active && (Terminal->FSProtocol == cfsSFTP);
  9199. }
  9200. //---------------------------------------------------------------------------
  9201. void __fastcall TCustomScpExplorerForm::PrivateKeyUpload()
  9202. {
  9203. TOperationVisualizer OperationVisualizer;
  9204. UnicodeString FileName = Terminal->SessionData->PublicKeyFile;
  9205. TTerminalManager::Instance()->UploadPublicKey(Terminal, NULL, FileName);
  9206. }
  9207. //---------------------------------------------------------------------------
  9208. void __fastcall TCustomScpExplorerForm::ChangeScale(int M, int D)
  9209. {
  9210. TForm::ChangeScale(M, D);
  9211. int APixelsPerInch = GetControlPixelsPerInch(this);
  9212. HANDLE ResourceModule = GUIConfiguration->ChangeToDefaultResourceModule();
  9213. try
  9214. {
  9215. GlyphsModule->SetPixelsPerInch(APixelsPerInch);
  9216. }
  9217. __finally
  9218. {
  9219. GUIConfiguration->ChangeResourceModule(ResourceModule);
  9220. }
  9221. }
  9222. //---------------------------------------------------------------------------
  9223. void __fastcall TCustomScpExplorerForm::DockContextPopup(TObject * Sender, TPoint & MousePos, bool & /*Handled*/)
  9224. {
  9225. NonVisualDataModule->ControlContextPopup(Sender, MousePos);
  9226. }
  9227. //---------------------------------------------------------------------
  9228. class TPublicControl : public TControl
  9229. {
  9230. friend class TCustomScpExplorerForm;
  9231. };
  9232. //---------------------------------------------------------------------------
  9233. void __fastcall TCustomScpExplorerForm::CopyPopup(TControl * DestControl, TControl * SourceControl)
  9234. {
  9235. static_cast<TPublicControl *>(DestControl)->PopupMenu = static_cast<TPublicControl *>(SourceControl)->PopupMenu;
  9236. static_cast<TPublicControl *>(DestControl)->OnContextPopup = static_cast<TPublicControl *>(SourceControl)->OnContextPopup;
  9237. }
  9238. //---------------------------------------------------------------------------
  9239. void __fastcall TCustomScpExplorerForm::DoBookmarkClick(TOperationSide Side, TObject * Sender)
  9240. {
  9241. TBookmark * Bookmark = DebugNotNull(reinterpret_cast<TBookmark *>(dynamic_cast<TTBCustomItem *>(Sender)->Tag));
  9242. OpenBookmark(Side, Bookmark);
  9243. }
  9244. //---------------------------------------------------------------------------
  9245. void __fastcall TCustomScpExplorerForm::LocalBookmarkClick(TObject * Sender)
  9246. {
  9247. DoBookmarkClick(osLocal, Sender);
  9248. }
  9249. //---------------------------------------------------------------------------
  9250. void __fastcall TCustomScpExplorerForm::RemoteBookmarkClick(TObject * Sender)
  9251. {
  9252. DoBookmarkClick(osRemote, Sender);
  9253. }
  9254. //---------------------------------------------------------------------------
  9255. void __fastcall TCustomScpExplorerForm::CreateOpenDirMenuList(
  9256. TTBCustomItem * Menu, TOperationSide Side, TBookmarkList * BookmarkList)
  9257. {
  9258. if (BookmarkList != NULL)
  9259. {
  9260. TNotifyEvent OnBookmarkClick = (Side == osLocal) ? &LocalBookmarkClick : &RemoteBookmarkClick;
  9261. if (!WinConfiguration->UseLocationProfiles)
  9262. {
  9263. std::unique_ptr<TStringList> Directories(new TStringList());
  9264. Directories->CaseSensitive = (Side == osRemote);
  9265. for (int Index = 0; Index < BookmarkList->Count; Index++)
  9266. {
  9267. TBookmark * Bookmark = BookmarkList->Bookmarks[Index];
  9268. UnicodeString Directory = Bookmark->GetSideDirectory(Side);
  9269. if (!Directory.IsEmpty() && (Directories->IndexOf(Directory) < 0))
  9270. {
  9271. std::unique_ptr<TTBCustomItem> Item(new TTBXItem(Owner));
  9272. Item->Caption = EscapeHotkey(Directory);
  9273. Item->ShortCut = Bookmark->ShortCut;
  9274. Item->OnClick = OnBookmarkClick;
  9275. Item->Tag = reinterpret_cast<int>(Bookmark);
  9276. Directories->Add(Directory);
  9277. Menu->Add(Item.release());
  9278. }
  9279. }
  9280. }
  9281. else
  9282. {
  9283. std::unique_ptr<TStrings> Folders(CreateSortedStringList());
  9284. for (int Index = 0; Index < BookmarkList->Count; Index++)
  9285. {
  9286. TBookmark * Bookmark = BookmarkList->Bookmarks[Index];
  9287. if (!Bookmark->Node.IsEmpty())
  9288. {
  9289. Folders->Add(Bookmark->Node);
  9290. }
  9291. }
  9292. for (int Index = 0; Index < Folders->Count; Index++)
  9293. {
  9294. std::unique_ptr<TTBCustomItem> Item(new TTBXSubmenuItem(Owner));
  9295. Item->Caption = Folders->Strings[Index];
  9296. Item->ImageIndex = NonVisualDataModule->RemoteChangePathAction->ImageIndex;
  9297. Folders->Objects[Index] = Item.get();
  9298. Menu->Add(Item.release());
  9299. }
  9300. for (int Index = 0; Index < BookmarkList->Count; Index++)
  9301. {
  9302. TBookmark * Bookmark = BookmarkList->Bookmarks[Index];
  9303. TTBCustomItem * Parent;
  9304. if (!Bookmark->Node.IsEmpty())
  9305. {
  9306. DebugAssert(Folders->IndexOf(Bookmark->Node) >= 0);
  9307. Parent = dynamic_cast<TTBCustomItem *>(Folders->Objects[Folders->IndexOf(Bookmark->Node)]);
  9308. }
  9309. else
  9310. {
  9311. Parent = Menu;
  9312. }
  9313. std::unique_ptr<TTBCustomItem> Item(new TTBXItem(Owner));
  9314. Item->Caption = Bookmark->Name;
  9315. Item->ShortCut = Bookmark->ShortCut;
  9316. Item->OnClick = OnBookmarkClick;
  9317. Item->Tag = reinterpret_cast<int>(Bookmark);
  9318. if (((Bookmark->Name == Bookmark->Local) || (Bookmark->Name == Bookmark->Remote)) &&
  9319. (Bookmark->Local.IsEmpty() || Bookmark->Remote.IsEmpty()))
  9320. {
  9321. // No hint for location profiles that are actually mere "bookmarks"
  9322. }
  9323. else
  9324. {
  9325. UnicodeString Hint = FORMAT(LoadStrPart(LOCATION_PROFILE_HINT, 1), (Bookmark->Name));
  9326. UnicodeString LongHint;
  9327. if (!Bookmark->Local.IsEmpty())
  9328. {
  9329. AddToList(LongHint, FORMAT(LoadStrPart(LOCATION_PROFILE_HINT, 2), (Bookmark->Local)), L"\n");
  9330. }
  9331. if (!Bookmark->Remote.IsEmpty())
  9332. {
  9333. AddToList(LongHint, FORMAT(LoadStrPart(LOCATION_PROFILE_HINT, 3), (Bookmark->Remote)), L"\n");
  9334. }
  9335. AddToList(Hint, LongHint, L"|");
  9336. Item->Hint = Hint;
  9337. }
  9338. Parent->Add(Item.release());
  9339. }
  9340. }
  9341. AddMenuSeparator(Menu);
  9342. }
  9343. }
  9344. //---------------------------------------------------------------------------
  9345. void __fastcall TCustomScpExplorerForm::CreateOpenDirMenu(TTBCustomItem * Menu, TOperationSide Side)
  9346. {
  9347. Menu->Clear();
  9348. std::unique_ptr<TTBCustomItem> Item;
  9349. Item.reset(new TTBXItem(Owner));
  9350. Item->Action = (Side == osLocal) ? NonVisualDataModule->LocalOpenDirAction : NonVisualDataModule->RemoteOpenDirAction;
  9351. Item->Options = Item->Options << tboDefault;
  9352. Menu->Add(Item.release());
  9353. AddMenuSeparator(Menu);
  9354. CreateOpenDirMenuList(Menu, Side, WinConfiguration->Bookmarks[Terminal->SessionData->SessionKey]);
  9355. CreateOpenDirMenuList(Menu, Side, WinConfiguration->SharedBookmarks);
  9356. Item.reset(new TTBXItem(Owner));
  9357. Item->Action = (Side == osLocal) ? NonVisualDataModule->LocalAddBookmarkAction : NonVisualDataModule->RemoteAddBookmarkAction;
  9358. Menu->Add(Item.release());
  9359. }
  9360. //---------------------------------------------------------------------------
  9361. bool __fastcall TCustomScpExplorerForm::TryOpenDirectory(TOperationSide Side, const UnicodeString & Path)
  9362. {
  9363. bool Result = true;
  9364. try
  9365. {
  9366. if (Side == osRemote)
  9367. {
  9368. Terminal->ExceptionOnFail = true;
  9369. }
  9370. try
  9371. {
  9372. DirView(Side)->Path = Path;
  9373. }
  9374. __finally
  9375. {
  9376. if (Side == osRemote)
  9377. {
  9378. Terminal->ExceptionOnFail = false;
  9379. }
  9380. }
  9381. }
  9382. catch (Exception & E)
  9383. {
  9384. if ((Side != osRemote) || Terminal->Active)
  9385. {
  9386. ShowExtendedExceptionEx(Terminal, &E);
  9387. Result = false;
  9388. }
  9389. else
  9390. {
  9391. throw;
  9392. }
  9393. }
  9394. return Result;
  9395. }
  9396. //---------------------------------------------------------------------------
  9397. void __fastcall TCustomScpExplorerForm::ReloadDirectory(TOperationSide Side)
  9398. {
  9399. // Make sure there some feedback even when loading tine local folders
  9400. TInstantOperationVisualizer Visualizer;
  9401. DirView(Side)->ReloadDirectory();
  9402. }
  9403. //---------------------------------------------------------------------------
  9404. void __fastcall TCustomScpExplorerForm::CloseSessionTab(int Index)
  9405. {
  9406. if (Index == SessionsPageControl->TabIndex)
  9407. {
  9408. CloseSession();
  9409. }
  9410. else
  9411. {
  9412. TTabSheet * TabSheet = SessionsPageControl->Pages[Index];
  9413. TTerminal * Terminal = GetSessionTabTerminal(TabSheet);
  9414. TTerminalManager * Manager = TTerminalManager::Instance();
  9415. if (!Terminal->Active)
  9416. {
  9417. Manager->FreeTerminal(Terminal);
  9418. }
  9419. else
  9420. {
  9421. TTerminalQueue * Queue = Manager->FindQueueForTerminal(Terminal);
  9422. if (CanCloseQueue(Queue))
  9423. {
  9424. Manager->FreeTerminal(Terminal);
  9425. }
  9426. }
  9427. }
  9428. }
  9429. //---------------------------------------------------------------------------
  9430. void __fastcall TCustomScpExplorerForm::SessionsPageControlCloseButtonClick(TPageControl * /*Sender*/, int Index)
  9431. {
  9432. CloseSessionTab(Index);
  9433. }
  9434. //---------------------------------------------------------------------------
  9435. void __fastcall TCustomScpExplorerForm::CopyFilesToClipboard(TOperationSide Side)
  9436. {
  9437. if (DebugAlwaysTrue(GetSide(Side) == osRemote))
  9438. {
  9439. TInstantOperationVisualizer Visualizer;
  9440. // To trigger ClipboardDataObjectRelease (in case the clipboard already contains our data)
  9441. // before we create a new data object
  9442. ClipboardClear();
  9443. DebugAssert(FClipboardFakeDirectory.IsEmpty());
  9444. DebugAssert(FClipboardFakeMonitors.get() == NULL);
  9445. FClipboardFakeDirectory = CreateFakeTransferDirectory();
  9446. FClipboardFakeMonitors.reset(StartCreationDirectoryMonitorsOnEachDrive(FILE_NOTIFY_CHANGE_DIR_NAME, ClipboardFakeCreated));
  9447. if (FClipboardDragDropFilesEx.get() == NULL)
  9448. {
  9449. FClipboardDragDropFilesEx.reset(new TDragDropFilesEx(this));
  9450. FClipboardDragDropFilesEx->OnDataObjectRelease = ClipboardDataObjectRelease;
  9451. }
  9452. FClipboardDragDropFilesEx->FileList->Clear();
  9453. FClipboardDragDropFilesEx->FileList->AddItem(NULL, FClipboardFakeDirectory);
  9454. FClipboardDragDropFilesEx->CopyToClipboard();
  9455. FClipboardTerminal = FTerminal;
  9456. // Need full paths, as cwd can be different once files are pasted
  9457. FClipboardFileList.reset(TRemoteFileList::CloneStrings(RemoteDirView->CreateFileList(false, true)));
  9458. for (int Index = 0; Index < FClipboardFileList->Count; Index++)
  9459. {
  9460. FClipboardFileList->Strings[Index] = UnixExcludeTrailingBackslash(FClipboardFileList->Strings[Index]);
  9461. }
  9462. }
  9463. }
  9464. //---------------------------------------------------------------------------
  9465. void __fastcall TCustomScpExplorerForm::ClipboardClear()
  9466. {
  9467. if (OpenClipboard(0))
  9468. {
  9469. EmptyClipboard(); // Calls ClipboardDataObjectRelease, if clipboard contains our data
  9470. CloseClipboard();
  9471. }
  9472. }
  9473. //---------------------------------------------------------------------------
  9474. bool __fastcall TCustomScpExplorerForm::DoesClipboardContainOurFiles()
  9475. {
  9476. return !FClipboardFakeDirectory.IsEmpty();
  9477. }
  9478. //---------------------------------------------------------------------------
  9479. void __fastcall TCustomScpExplorerForm::ClipboardStop()
  9480. {
  9481. RemoveDir(ApiPath(FClipboardFakeDirectory));
  9482. FClipboardFakeDirectory = UnicodeString();
  9483. FClipboardTerminal = NULL;
  9484. // Also called in Idle()
  9485. FClipboardFakeMonitors.reset(NULL);
  9486. }
  9487. //---------------------------------------------------------------------------
  9488. void __fastcall TCustomScpExplorerForm::ClipboardDataObjectRelease(TObject * /*Sender*/)
  9489. {
  9490. if (DebugAlwaysTrue(!FClipboardFakeDirectory.IsEmpty()))
  9491. {
  9492. ClipboardStop();
  9493. }
  9494. }
  9495. //---------------------------------------------------------------------------
  9496. void __fastcall TCustomScpExplorerForm::ClipboardDownload(const UnicodeString & TargetDirectory, bool NoConfirmation, bool DragDrop)
  9497. {
  9498. TTerminalManager * Manager = TTerminalManager::Instance();
  9499. Manager->ActiveTerminal = FClipboardTerminal;
  9500. TTransferOperationParam Params;
  9501. Params.TargetDirectory = TargetDirectory;
  9502. Params.DragDrop = DragDrop;
  9503. ExecuteFileOperation(foCopy, osRemote, FClipboardFileList.get(), NoConfirmation, &Params);
  9504. }
  9505. //---------------------------------------------------------------------------
  9506. void __fastcall TCustomScpExplorerForm::ClipboardFakeCreated(TObject * /*Sender*/, const UnicodeString FileName)
  9507. {
  9508. if (DebugAlwaysTrue(!FClipboardFakeDirectory.IsEmpty()) &&
  9509. SameText(ExtractFileName(FileName), ExtractFileName(FClipboardFakeDirectory)))
  9510. {
  9511. RemoveDir(ApiPath(FileName));
  9512. if (!NonVisualDataModule->Busy)
  9513. {
  9514. bool NoConfirmation = (WinConfiguration->DDTransferConfirmation == asOff);
  9515. ClipboardDownload(ExtractFilePath(FileName), NoConfirmation, true);
  9516. }
  9517. }
  9518. }
  9519. //---------------------------------------------------------------------------