CustomScpExplorer.cpp 286 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925
  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 <Log.h>
  17. #include <Progress.h>
  18. #include <SynchronizeProgress.h>
  19. #include <DragExt.h>
  20. #include <WinApi.h>
  21. #include "GUITools.h"
  22. #include "NonVisual.h"
  23. #include "Glyphs.h"
  24. #include "Tools.h"
  25. #include "WinConfiguration.h"
  26. #include "TerminalManager.h"
  27. #include "EditorManager.h"
  28. #include "ProgParams.h"
  29. #include "Setup.h"
  30. #include <Consts.hpp>
  31. #include <DateUtils.hpp>
  32. #include <TB2Common.hpp>
  33. //---------------------------------------------------------------------------
  34. #pragma package(smart_init)
  35. #pragma link "CustomDirView"
  36. #pragma link "CustomUnixDirView"
  37. #pragma link "IEListView"
  38. #pragma link "NortonLikeListView"
  39. #pragma link "UnixDirView"
  40. #pragma link "CustomDriveView"
  41. #pragma link "UnixDriveView"
  42. #pragma link "CustomDriveView"
  43. #pragma link "TB2Dock"
  44. #pragma link "TBXStatusBars"
  45. #pragma link "TB2Item"
  46. #pragma link "TB2Toolbar"
  47. #pragma link "ThemePageControl"
  48. #pragma link "PathLabel"
  49. #ifndef NO_RESOURCES
  50. #pragma resource "*.dfm"
  51. #endif
  52. //---------------------------------------------------------------------------
  53. #define SAVE_SELECTION(DIRVIEW) \
  54. UnicodeString FocusFile = L""; \
  55. UnicodeString LastFocusedFile = L""; \
  56. if (DIRVIEW->ItemFocused) LastFocusedFile = DIRVIEW->ItemFocused->Caption; \
  57. { TListItem * ClosestUnselected = DIRVIEW->ClosestUnselected(DIRVIEW->ItemFocused); \
  58. if (ClosestUnselected) FocusFile = ClosestUnselected->Caption; }
  59. #define RESTORE_SELECTION(DIRVIEW) \
  60. if (!LastFocusedFile.IsEmpty() && \
  61. (!DIRVIEW->ItemFocused || (DIRVIEW->ItemFocused->Caption != LastFocusedFile))) \
  62. { \
  63. TListItem *ItemToSelect = DIRVIEW->FindFileItem(FocusFile); \
  64. if (ItemToSelect) \
  65. { \
  66. DIRVIEW->ItemFocused = ItemToSelect; \
  67. DIRVIEW->ItemFocused->MakeVisible(False); \
  68. } \
  69. }
  70. //---------------------------------------------------------------------------
  71. #define WM_COMPONENT_HIDE (WM_WINSCP_USER + 4)
  72. static const int SessionPanelCount = 4;
  73. //---------------------------------------------------------------------------
  74. class TMutexGuard
  75. {
  76. public:
  77. TMutexGuard(HANDLE AMutex, int Message = MUTEX_RELEASE_TIMEOUT,
  78. int Timeout = 5000)
  79. {
  80. FMutex = NULL;
  81. unsigned long WaitResult = WaitForSingleObject(AMutex, Timeout);
  82. if (WaitResult == WAIT_TIMEOUT)
  83. {
  84. throw Exception(LoadStr(MUTEX_RELEASE_TIMEOUT));
  85. }
  86. else
  87. {
  88. FMutex = AMutex;
  89. }
  90. }
  91. ~TMutexGuard()
  92. {
  93. if (FMutex != NULL)
  94. {
  95. ReleaseMutex(FMutex);
  96. }
  97. }
  98. private:
  99. HANDLE FMutex;
  100. };
  101. //---------------------------------------------------------------------------
  102. class TWindowLock
  103. {
  104. public:
  105. TWindowLock(TCustomScpExplorerForm * Form) :
  106. FForm(Form)
  107. {
  108. FForm->LockWindow();
  109. }
  110. ~TWindowLock()
  111. {
  112. FForm->UnlockWindow();
  113. }
  114. private:
  115. TCustomScpExplorerForm * FForm;
  116. };
  117. //---------------------------------------------------------------------------
  118. struct TTransferOperationParam
  119. {
  120. TTransferOperationParam();
  121. UnicodeString TargetDirectory;
  122. bool Temp;
  123. bool DragDrop;
  124. int Options;
  125. TAutoSwitch Queue;
  126. };
  127. //---------------------------------------------------------------------------
  128. TTransferOperationParam::TTransferOperationParam()
  129. {
  130. Temp = false;
  131. DragDrop = false;
  132. Options = 0;
  133. Queue = asAuto;
  134. }
  135. //---------------------------------------------------------------------------
  136. class TTransferPresetNoteData : public TObject
  137. {
  138. public:
  139. UnicodeString Message;
  140. };
  141. //---------------------------------------------------------------------------
  142. class TTerminalNoteData : public TObject
  143. {
  144. public:
  145. TTerminal * Terminal;
  146. };
  147. //---------------------------------------------------------------------------
  148. __fastcall TCustomScpExplorerForm::TCustomScpExplorerForm(TComponent* Owner):
  149. FFormRestored(false),
  150. TForm(Owner)
  151. {
  152. FCurrentSide = osRemote;
  153. FEverShown = false;
  154. FDocks = new TList();
  155. RestoreParams();
  156. ConfigurationChanged();
  157. RemoteDirView->Invalidate();
  158. DebugAssert(NonVisualDataModule && !NonVisualDataModule->ScpExplorer);
  159. NonVisualDataModule->ScpExplorer = this;
  160. Application->OnMinimize = ApplicationMinimize;
  161. Application->OnRestore = ApplicationRestore;
  162. FAutoOperation = false;
  163. FForceExecution = false;
  164. FShowStatusBarHint = false;
  165. FIgnoreNextDialogChar = 0;
  166. FErrorList = NULL;
  167. FSynchronizeProgressForm = NULL;
  168. FProgressForm = NULL;
  169. FRefreshLocalDirectory = false;
  170. FRefreshRemoteDirectory = false;
  171. FDDMoveSlipped = false;
  172. FDDExtMapFile = NULL;
  173. // CreateMutexW keeps failing with ERROR_NOACCESS
  174. FDDExtMutex = CreateMutexA(NULL, false, AnsiString(DRAG_EXT_MUTEX).c_str());
  175. DebugAssert(FDDExtMutex != NULL);
  176. FDDTargetControl = NULL;
  177. FDelayedDeletionTimer = NULL;
  178. FDelayedDeletionList = new TStringList();
  179. FDDFileList = NULL;
  180. FPendingTempSpaceWarn = false;
  181. FCapturedLog = NULL;
  182. FDragDropOperation = false;
  183. memset(&FHistoryMenu, 0, sizeof(FHistoryMenu));
  184. FAllowTransferPresetAutoSelect = true;
  185. FCopyParamDefault = L"";
  186. FSynchronizeController = NULL;
  187. FPendingQueueActionItem = NULL;
  188. FLockLevel = 0;
  189. FLockSuspendLevel = 0;
  190. FDisabledOnLockSuspend = false;
  191. FAlternativeDelete = false;
  192. FTrayIcon = new ::TTrayIcon(0);
  193. FTrayIcon->OnClick = TrayIconClick;
  194. FMaxQueueLength = 0;
  195. FLastContextPopupScreenPoint = TPoint(-1, -1);
  196. FTransferResumeList = NULL;
  197. FMoveToQueue = false;
  198. FStandaloneEditing = false;
  199. FOnFeedSynchronizeError = NULL;
  200. FNeedSession = false;
  201. FEditorManager = new TEditorManager();
  202. FEditorManager->OnFileChange = ExecutedFileChanged;
  203. FEditorManager->OnFileReload = ExecutedFileReload;
  204. FEditorManager->OnFileEarlyClosed = ExecutedFileEarlyClosed;
  205. FEditorManager->OnFileUploadComplete = ExecutedFileUploadComplete;
  206. FLocalEditors = new TList();
  207. FQueueStatus = NULL;
  208. FQueueStatusSection = new TCriticalSection();
  209. FQueueStatusInvalidated = false;
  210. FQueueItemInvalidated = false;
  211. FQueueActedItem = NULL;
  212. FQueueController = new TQueueController(QueueView3);
  213. FUserActionTimer = new TTimer(this);
  214. FUserActionTimer->Enabled = false;
  215. FUserActionTimer->Interval = 10;
  216. FUserActionTimer->OnTimer = UserActionTimer;
  217. FNotes = new TStringList();
  218. FNoteTimer = new TTimer(this);
  219. FNoteTimer->Enabled = false;
  220. FNoteTimer->OnTimer = NoteTimer;
  221. FOnNoteClick = NULL;
  222. FOle32Library = LoadLibrary(L"Ole32.dll");
  223. FDragMoveCursor = FOle32Library != NULL ?
  224. LoadCursor(FOle32Library, MAKEINTRESOURCE(2)) : NULL;
  225. UseSystemSettings(this);
  226. TTBXStringList * TransferList = dynamic_cast<TTBXStringList*>(
  227. static_cast<TObject*>(GetComponent(fcTransferList)));
  228. DebugAssert(TransferList != NULL);
  229. FTransferListHoverIndex = -1;
  230. TransferList->OnChange = TransferListChange;
  231. TransferList->OnDrawItem = TransferListDrawItem;
  232. SetSubmenu(dynamic_cast<TTBXCustomItem *>(static_cast<TObject *>(GetComponent(fcColorMenu))));
  233. SetSubmenu(NonVisualDataModule->ColorMenuItem);
  234. UseDesktopFont(SessionsPageControl);
  235. UpdateSessionsPageControlHeight();
  236. UseDesktopFont(RemoteDirView);
  237. UseDesktopFont(RemoteDriveView);
  238. UseDesktopFont(QueueView3);
  239. UseDesktopFont(QueueLabel);
  240. UseDesktopFont(RemoteStatusBar);
  241. reinterpret_cast<TLabel*>(QueueSplitter)->OnDblClick = QueueSplitterDblClick;
  242. FSystemImageList = SharedSystemImageList(false);
  243. FSystemImageList->DrawingStyle = dsTransparent;
  244. FCustomCommandMenu = CreateTBXPopupMenu(this);
  245. FCustomCommandLocalFileList = NULL;
  246. FCustomCommandRemoteFileList = NULL;
  247. FSessionColors = new TPngImageList(this);
  248. FSessionColors->SetSize(GlyphsModule->ExplorerImages->Width, GlyphsModule->ExplorerImages->Height);
  249. FSessionColors->ColorDepth = cd32Bit;
  250. AddFixedSessionImages();
  251. SessionsPageControl->Images = FSessionColors;
  252. QueueLabel->FocusControl = QueueView3;
  253. UpdateQueueLabel();
  254. FRemoteDirViewWasFocused = true;
  255. CreateHiddenWindow();
  256. StartUpdates();
  257. }
  258. //---------------------------------------------------------------------------
  259. __fastcall TCustomScpExplorerForm::~TCustomScpExplorerForm()
  260. {
  261. // this has to be one of the very first things to do
  262. StopUpdateThread();
  263. SessionsPageControl->Images = NULL;
  264. SAFE_DESTROY(FSessionColors);
  265. SAFE_DESTROY(FSessionsDragDropFilesEx);
  266. delete FCustomCommandLocalFileList;
  267. delete FCustomCommandRemoteFileList;
  268. delete FCustomCommandMenu;
  269. delete FSystemImageList;
  270. FSystemImageList = NULL;
  271. if (Application->OnHint == ApplicationHint)
  272. {
  273. Application->OnHint = NULL;
  274. }
  275. DebugAssert(Application->OnMinimize == ApplicationMinimize);
  276. Application->OnMinimize = NULL;
  277. DebugAssert(Application->OnRestore == ApplicationRestore);
  278. Application->OnRestore = NULL;
  279. delete FTrayIcon;
  280. FTrayIcon = NULL;
  281. FEditorManager->CloseInternalEditors(ForceCloseInternalEditor);
  282. delete FEditorManager;
  283. ForceCloseLocalEditors();
  284. delete FLocalEditors;
  285. if (FDelayedDeletionTimer)
  286. {
  287. DoDelayedDeletion(NULL);
  288. SAFE_DESTROY(FDelayedDeletionTimer);
  289. }
  290. SAFE_DESTROY(FDelayedDeletionList);
  291. // sometimes we do not get DDEnd so the list is not released
  292. SAFE_DESTROY(FDDFileList);
  293. DebugAssert(FSynchronizeController == NULL);
  294. CloseHandle(FDDExtMutex);
  295. FDDExtMutex = NULL;
  296. FreeLibrary(FOle32Library);
  297. FOle32Library = NULL;
  298. FDragMoveCursor = NULL;
  299. DebugAssert(!FErrorList);
  300. if (FEverShown)
  301. {
  302. // when window is never shown (like when running command-line operation),
  303. // particularly window site is not restored correctly (BoundsRect value set
  304. // in RestoreForm gets lost during handle allocation), so we do not want
  305. // it to be stored
  306. StoreParams();
  307. }
  308. Terminal = NULL;
  309. Queue = NULL;
  310. DebugAssert(NonVisualDataModule && (NonVisualDataModule->ScpExplorer == this));
  311. NonVisualDataModule->ScpExplorer = NULL;
  312. delete FQueueController;
  313. FQueueController = NULL;
  314. delete FQueueStatusSection;
  315. FQueueStatusSection = NULL;
  316. delete FQueueStatus;
  317. FQueueStatus = NULL;
  318. delete FUserActionTimer;
  319. FUserActionTimer = NULL;
  320. delete FNoteTimer;
  321. delete FNotes;
  322. delete FNoteData;
  323. SAFE_DESTROY(FDocks);
  324. SAFE_DESTROY(FHistoryMenu[0][0]);
  325. SAFE_DESTROY(FHistoryMenu[0][1]);
  326. SAFE_DESTROY(FHistoryMenu[1][0]);
  327. SAFE_DESTROY(FHistoryMenu[1][1]);
  328. if (FHiddenWindow != NULL)
  329. {
  330. DestroyWindow(FHiddenWindow);
  331. FHiddenWindow = NULL;
  332. }
  333. }
  334. //---------------------------------------------------------------------------
  335. LRESULT WINAPI TCustomScpExplorerForm::HiddenWindowProc(
  336. HWND HWnd, UINT Message, WPARAM WParam, LPARAM LParam)
  337. {
  338. LRESULT Result;
  339. if (Message == WM_COPYDATA)
  340. {
  341. LONG_PTR Ptr = GetWindowLongPtr(HWnd, GWLP_USERDATA);
  342. TCustomScpExplorerForm * Form = reinterpret_cast<TCustomScpExplorerForm *>(Ptr);
  343. TMessage AMessage;
  344. AMessage.Msg = Message;
  345. AMessage.WParam = WParam;
  346. AMessage.LParam = LParam;
  347. AMessage.Result = 0;
  348. Form->WMCopyData(AMessage);
  349. Result = AMessage.Result;
  350. }
  351. else
  352. {
  353. Result = DefWindowProc(HWnd, Message, WParam, LParam);
  354. }
  355. return Result;
  356. }
  357. //---------------------------------------------------------------------------
  358. void __fastcall TCustomScpExplorerForm::WMCopyData(TMessage & Message)
  359. {
  360. PCOPYDATASTRUCT CopyData = reinterpret_cast<PCOPYDATASTRUCT>(Message.LParam);
  361. size_t MessageSize = sizeof(TCopyDataMessage);
  362. bool Result = DebugAlwaysTrue(CopyData->cbData == MessageSize);
  363. if (Result)
  364. {
  365. const TCopyDataMessage & Message = *reinterpret_cast<const TCopyDataMessage *>(CopyData->lpData);
  366. Result = (Message.Version == TCopyDataMessage::Version1);
  367. if (Result)
  368. {
  369. switch (Message.Command)
  370. {
  371. case TCopyDataMessage::CommandCanCommandLine:
  372. Result = CanCommandLineFromAnotherInstance();
  373. break;
  374. case TCopyDataMessage::CommandCommandLine:
  375. {
  376. UnicodeString CommandLine(Message.CommandLine);
  377. Result = CommandLineFromAnotherInstance(CommandLine);
  378. }
  379. break;
  380. default:
  381. Result = false;
  382. break;
  383. }
  384. }
  385. }
  386. Message.Result = Result ? 1 : 0;
  387. }
  388. //---------------------------------------------------------------------------
  389. void __fastcall TCustomScpExplorerForm::CreateHiddenWindow()
  390. {
  391. WNDCLASS WindowClass = {0};
  392. WindowClass.lpfnWndProc = HiddenWindowProc;
  393. WindowClass.hInstance = HInstance;
  394. WindowClass.lpszClassName = HIDDEN_WINDOW_NAME;
  395. FHiddenWindow = NULL;
  396. if (RegisterClass(&WindowClass))
  397. {
  398. FHiddenWindow = CreateWindow(HIDDEN_WINDOW_NAME, L"",
  399. WS_POPUP, 0, 0, 0, 0, 0, 0, HInstance, NULL);
  400. if (FHiddenWindow != NULL)
  401. {
  402. SetWindowLongPtr(FHiddenWindow, GWLP_USERDATA, reinterpret_cast<LONG_PTR>(this));
  403. }
  404. }
  405. }
  406. //---------------------------------------------------------------------------
  407. bool __fastcall TCustomScpExplorerForm::CanCommandLineFromAnotherInstance()
  408. {
  409. bool Result = !NonVisualDataModule->Busy;
  410. return Result;
  411. }
  412. //---------------------------------------------------------------------------
  413. bool __fastcall TCustomScpExplorerForm::CommandLineFromAnotherInstance(
  414. const UnicodeString & CommandLine)
  415. {
  416. TProgramParams Params(CommandLine);
  417. bool Result = CanCommandLineFromAnotherInstance() && DebugAlwaysTrue(Params.ParamCount > 0);
  418. if (Result)
  419. {
  420. NonVisualDataModule->StartBusy();
  421. try
  422. {
  423. // this action is initiated from another process,
  424. // so it's likely that our window is not visible,
  425. // and user won't see what is going on
  426. Application->BringToFront();
  427. // reload sessions as we may be asked to open a session
  428. // just stored by another instance
  429. StoredSessions->Load();
  430. UnicodeString SessionName = Params.Param[1];
  431. std::unique_ptr<TObjectList> DataList(new TObjectList());
  432. UnicodeString DownloadFile; // unused
  433. GetLoginData(SessionName, &Params, DataList.get(), DownloadFile, true);
  434. if (DataList->Count > 0)
  435. {
  436. TTerminalManager * Manager = TTerminalManager::Instance();
  437. Manager->ActiveTerminal = Manager->NewTerminals(DataList.get());
  438. }
  439. }
  440. __finally
  441. {
  442. NonVisualDataModule->EndBusy();
  443. }
  444. }
  445. return Result;
  446. }
  447. //---------------------------------------------------------------------------
  448. void __fastcall TCustomScpExplorerForm::SetTerminal(TTerminal * value)
  449. {
  450. if (FTerminal != value)
  451. {
  452. TerminalChanging();
  453. FTerminal = value;
  454. bool PrevAllowTransferPresetAutoSelect = FAllowTransferPresetAutoSelect;
  455. FAllowTransferPresetAutoSelect = false;
  456. try
  457. {
  458. TerminalChanged();
  459. }
  460. __finally
  461. {
  462. FAllowTransferPresetAutoSelect = PrevAllowTransferPresetAutoSelect;
  463. }
  464. if (Terminal != NULL)
  465. {
  466. TransferPresetAutoSelect();
  467. }
  468. }
  469. }
  470. //---------------------------------------------------------------------------
  471. void __fastcall TCustomScpExplorerForm::TerminalChanging()
  472. {
  473. if (FTerminal != NULL)
  474. {
  475. UpdateTerminal(Terminal);
  476. }
  477. }
  478. //---------------------------------------------------------------------------
  479. void __fastcall TCustomScpExplorerForm::TerminalChanged()
  480. {
  481. RemoteDirView->Terminal = Terminal;
  482. NonVisualDataModule->ResetQueueOnceEmptyOperation();
  483. TManagedTerminal * ManagedTerminal = NULL;
  484. if (Terminal != NULL)
  485. {
  486. if (Terminal->Active)
  487. {
  488. Terminal->RefreshDirectory();
  489. }
  490. ManagedTerminal = dynamic_cast<TManagedTerminal *>(Terminal);
  491. DebugAssert(ManagedTerminal != NULL);
  492. if (WinConfiguration->PreservePanelState)
  493. {
  494. if (ManagedTerminal->RemoteExplorerState != NULL)
  495. {
  496. RemoteDirView->RestoreState(ManagedTerminal->RemoteExplorerState);
  497. }
  498. else
  499. {
  500. RemoteDirView->ClearState();
  501. }
  502. }
  503. InitStatusBar();
  504. }
  505. DoTerminalListChanged(false);
  506. if (ManagedTerminal != NULL)
  507. {
  508. // this has to be set only after the tab is switched from DoTerminalListChanged,
  509. // otherwise we are changing color of wrong tab
  510. SessionColor = (TColor)ManagedTerminal->StateData->Color;
  511. }
  512. UpdateTransferList();
  513. // Update panels Enable state before refreshing the labels
  514. UpdateControls();
  515. }
  516. //---------------------------------------------------------------------------
  517. void __fastcall TCustomScpExplorerForm::SetQueue(TTerminalQueue * value)
  518. {
  519. if (Queue != value)
  520. {
  521. if (FQueue != NULL)
  522. {
  523. FQueue->OnListUpdate = NULL;
  524. FQueue->OnQueueItemUpdate = NULL;
  525. }
  526. FQueue = value;
  527. if (FQueue != NULL)
  528. {
  529. DebugAssert(FQueue->OnListUpdate == NULL);
  530. FQueue->OnListUpdate = QueueListUpdate;
  531. DebugAssert(FQueue->OnQueueItemUpdate == NULL);
  532. FQueue->OnQueueItemUpdate = QueueItemUpdate;
  533. }
  534. QueueChanged();
  535. }
  536. }
  537. //---------------------------------------------------------------------------
  538. void __fastcall TCustomScpExplorerForm::QueueView3Deletion(TObject * /*Sender*/,
  539. TListItem * Item)
  540. {
  541. if (FQueueActedItem == Item)
  542. {
  543. FQueueActedItem = NULL;
  544. if ((QueueView3->PopupMenu != NULL) &&
  545. (QueueView3->PopupMenu->PopupComponent == QueueView3))
  546. {
  547. // rather "trick", suggested by Jordan on jrsoftware.toolbar2000
  548. ReleaseCapture();
  549. }
  550. }
  551. if (Item->Data == FPendingQueueActionItem)
  552. {
  553. FPendingQueueActionItem = NULL;
  554. }
  555. }
  556. //---------------------------------------------------------------------------
  557. void __fastcall TCustomScpExplorerForm::UpdateQueueStatus(bool QueueChanging)
  558. {
  559. {
  560. TGuard Guard(FQueueStatusSection);
  561. FQueueStatusInvalidated = false;
  562. if (FQueue != NULL)
  563. {
  564. FQueueStatus = FQueue->CreateStatus(FQueueStatus);
  565. }
  566. }
  567. if ((FQueueStatus != NULL) && (FQueueStatus->Count > FMaxQueueLength))
  568. {
  569. FMaxQueueLength = FQueueStatus->Count;
  570. Configuration->Usage->SetMax(L"MaxQueueLength", FMaxQueueLength);
  571. }
  572. FQueueController->UpdateQueueStatus(FQueueStatus);
  573. SetQueueProgress();
  574. UpdateQueueView();
  575. bool IsEmpty = (FQueueStatus == NULL) || (FQueueStatus->Count == 0);
  576. if (IsEmpty && (Terminal != NULL))
  577. {
  578. TOnceDoneOperation OnceDoneOperation =
  579. NonVisualDataModule->CurrentQueueOnceEmptyOperation();
  580. NonVisualDataModule->ResetQueueOnceEmptyOperation();
  581. if ((FQueue != NULL) && !WinConfiguration->EnableQueueByDefault && !QueueChanging)
  582. {
  583. FQueue->Enabled = false;
  584. }
  585. if ((OnceDoneOperation != odoIdle) && !NonVisualDataModule->Busy)
  586. {
  587. Terminal->CloseOnCompletion(OnceDoneOperation, LoadStr(CLOSED_ON_QUEUE_EMPTY));
  588. }
  589. }
  590. }
  591. //---------------------------------------------------------------------------
  592. UnicodeString __fastcall TCustomScpExplorerForm::GetQueueProgressTitle()
  593. {
  594. UnicodeString Result;
  595. if (FQueueStatus != NULL)
  596. {
  597. int ActiveAndPendingCount = FQueueStatus->ActiveAndPendingCount;
  598. if ((ActiveAndPendingCount == 1) && (FQueueStatus->ActiveCount == 1))
  599. {
  600. TFileOperationProgressType * ProgressData =
  601. FQueueStatus->Items[FQueueStatus->DoneCount]->ProgressData;
  602. if ((ProgressData != NULL) && ProgressData->InProgress)
  603. {
  604. Result = TProgressForm::ProgressStr(ProgressData);
  605. }
  606. }
  607. else if (ActiveAndPendingCount > 1)
  608. {
  609. Result = FMTLOAD(PROGRESS_IN_QUEUE, (ActiveAndPendingCount));
  610. }
  611. }
  612. return Result;
  613. }
  614. //---------------------------------------------------------------------------
  615. void __fastcall TCustomScpExplorerForm::UpdateQueueView()
  616. {
  617. ComponentVisible[fcQueueView] =
  618. (WinConfiguration->QueueView.Show == qvShow) ||
  619. ((WinConfiguration->QueueView.Show == qvHideWhenEmpty) &&
  620. (FQueueStatus != NULL) && (FQueueStatus->Count > 0));
  621. }
  622. //---------------------------------------------------------------------------
  623. void __fastcall TCustomScpExplorerForm::QueueChanged()
  624. {
  625. if (FQueueStatus != NULL)
  626. {
  627. delete FQueueStatus;
  628. FQueueStatus = NULL;
  629. }
  630. UpdateQueueStatus(true);
  631. }
  632. //---------------------------------------------------------------------------
  633. void __fastcall TCustomScpExplorerForm::QueueListUpdate(TTerminalQueue * Queue)
  634. {
  635. if (FQueue == Queue)
  636. {
  637. FQueueStatusInvalidated = true;
  638. }
  639. }
  640. //---------------------------------------------------------------------------
  641. void __fastcall TCustomScpExplorerForm::QueueItemUpdate(TTerminalQueue * Queue,
  642. TQueueItem * Item)
  643. {
  644. if (FQueue == Queue)
  645. {
  646. TGuard Guard(FQueueStatusSection);
  647. // this may be running in parallel with QueueChanged
  648. if (FQueueStatus != NULL)
  649. {
  650. TQueueItemProxy * QueueItem = FQueueStatus->FindByQueueItem(Item);
  651. if ((Item->Status == TQueueItem::qsDone) && (Terminal != NULL))
  652. {
  653. FRefreshLocalDirectory = (QueueItem == NULL) ||
  654. (!QueueItem->Info->ModifiedLocal.IsEmpty());
  655. FRefreshRemoteDirectory = (QueueItem == NULL) ||
  656. (!QueueItem->Info->ModifiedRemote.IsEmpty());
  657. }
  658. if (QueueItem != NULL)
  659. {
  660. QueueItem->UserData = (void*)true;
  661. FQueueItemInvalidated = true;
  662. }
  663. }
  664. }
  665. }
  666. //---------------------------------------------------------------------------
  667. bool __fastcall TCustomScpExplorerForm::IsQueueAutoPopup()
  668. {
  669. // during standalone editing, we have no way to see/control queue,
  670. // so we have to always popup prompts automatically
  671. return FStandaloneEditing || GUIConfiguration->QueueAutoPopup;
  672. }
  673. //---------------------------------------------------------------------------
  674. void __fastcall TCustomScpExplorerForm::RefreshQueueItems()
  675. {
  676. if (FQueueStatus != NULL)
  677. {
  678. bool QueueAutoPopup = IsQueueAutoPopup();
  679. bool NeedRefresh = FQueueController->NeedRefresh();
  680. bool Refresh = FQueueItemInvalidated || NeedRefresh;
  681. FQueueItemInvalidated = false;
  682. int Limit = Refresh ? FQueueStatus->Count : FQueueStatus->DoneAndActiveCount;
  683. bool Updated = false;
  684. TQueueItemProxy * QueueItem;
  685. bool WasUserAction;
  686. for (int Index = 0; Index < Limit; Index++)
  687. {
  688. bool Update = false;
  689. QueueItem = FQueueStatus->Items[Index];
  690. WasUserAction = TQueueItem::IsUserActionStatus(QueueItem->Status);
  691. if (!NonVisualDataModule->Busy && QueueAutoPopup && WasUserAction &&
  692. (FPendingQueueActionItem == NULL))
  693. {
  694. FPendingQueueActionItem = QueueItem;
  695. FUserActionTimer->Enabled = true;
  696. }
  697. if ((bool)QueueItem->UserData)
  698. {
  699. QueueItem->UserData = (void*)false;
  700. QueueItem->Update();
  701. Updated = true;
  702. Update = true;
  703. }
  704. else if (FQueueController->QueueItemNeedsFrequentRefresh(QueueItem) || NeedRefresh)
  705. {
  706. Update = true;
  707. }
  708. if (Update)
  709. {
  710. FQueueController->RefreshQueueItem(QueueItem);
  711. }
  712. }
  713. if (Updated)
  714. {
  715. NonVisualDataModule->UpdateNonVisibleActions();
  716. SetQueueProgress();
  717. }
  718. }
  719. }
  720. //---------------------------------------------------------------------------
  721. void __fastcall TCustomScpExplorerForm::SetTaskbarListProgressState(TBPFLAG Flags)
  722. {
  723. FTaskbarList->SetProgressState(GetMainForm()->Handle, Flags);
  724. }
  725. //---------------------------------------------------------------------------
  726. void __fastcall TCustomScpExplorerForm::SetTaskbarListProgressValue(TFileOperationProgressType * ProgressData)
  727. {
  728. if (ProgressData->Operation != foCalculateSize)
  729. {
  730. // implies TBPF_NORMAL
  731. FTaskbarList->SetProgressValue(GetMainForm()->Handle, ProgressData->OverallProgress(), 100);
  732. }
  733. else
  734. {
  735. SetTaskbarListProgressState(TBPF_INDETERMINATE);
  736. }
  737. }
  738. //---------------------------------------------------------------------------
  739. void __fastcall TCustomScpExplorerForm::SetQueueProgress()
  740. {
  741. TTerminalManager::Instance()->QueueStatusUpdated();
  742. if ((FTaskbarList != NULL) && (FProgressForm == NULL))
  743. {
  744. if ((FQueueStatus != NULL) && (FQueueStatus->ActiveCount > 0))
  745. {
  746. if (FQueueStatus->ActiveCount == 1)
  747. {
  748. TFileOperationProgressType * ProgressData;
  749. if ((FQueueStatus->Items[FQueueStatus->DoneCount] != NULL) &&
  750. ((ProgressData = FQueueStatus->Items[FQueueStatus->DoneCount]->ProgressData) != NULL) &&
  751. ProgressData->InProgress)
  752. {
  753. SetTaskbarListProgressValue(ProgressData);
  754. }
  755. else
  756. {
  757. SetTaskbarListProgressState(TBPF_NOPROGRESS);
  758. }
  759. }
  760. else
  761. {
  762. SetTaskbarListProgressState(TBPF_INDETERMINATE);
  763. }
  764. }
  765. else
  766. {
  767. SetTaskbarListProgressState(TBPF_NOPROGRESS);
  768. }
  769. }
  770. UpdateQueueLabel();
  771. }
  772. //---------------------------------------------------------------------------
  773. void __fastcall TCustomScpExplorerForm::UpdateQueueLabel()
  774. {
  775. UnicodeString Caption = LoadStr(QUEUE_CAPTION);
  776. if (FQueueStatus != NULL)
  777. {
  778. int ActiveAndPendingCount = FQueueStatus->ActiveAndPendingCount;
  779. if (ActiveAndPendingCount > 0)
  780. {
  781. Caption = FORMAT("%s (%d)", (Caption, ActiveAndPendingCount));
  782. }
  783. }
  784. QueueLabel->Caption = Caption;
  785. }
  786. //---------------------------------------------------------------------------
  787. void __fastcall TCustomScpExplorerForm::UpdateTransferList()
  788. {
  789. TTBXStringList * TransferList = dynamic_cast<TTBXStringList*>(
  790. static_cast<TComponent*>(GetComponent(fcTransferList)));
  791. TTBXDropDownItem * TransferDropDown = dynamic_cast<TTBXDropDownItem*>(
  792. static_cast<TComponent*>(GetComponent(fcTransferDropDown)));
  793. TransferList->Strings->BeginUpdate();
  794. try
  795. {
  796. TransferList->Strings->Assign(GUIConfiguration->CopyParamList->NameList);
  797. TransferList->Strings->Insert(0, StripHotkey(LoadStr(COPY_PARAM_DEFAULT)));
  798. TransferList->ItemIndex = GUIConfiguration->CopyParamIndex + 1;
  799. if (FTransferDropDownHint.IsEmpty())
  800. {
  801. FTransferDropDownHint = TransferDropDown->Hint;
  802. }
  803. // this way we get name for "default" settings (COPY_PARAM_DEFAULT)
  804. UnicodeString Name = TransferList->Strings->Strings[TransferList->ItemIndex];
  805. TransferDropDown->Text = StripHotkey(Name);
  806. TransferDropDown->Hint = FORMAT(L"%s\n \n%s:\n%s|%s",
  807. (FTransferDropDownHint, StripHotkey(Name),
  808. GUIConfiguration->CurrentCopyParam.GetInfoStr(L"; ",
  809. FLAGMASK(Terminal != NULL, Terminal->UsableCopyParamAttrs(0).General)),
  810. FTransferDropDownHint));
  811. // update the label, otherwise when it is updated only on the first draw
  812. // of the list, it is drawn "bold" for some reason
  813. FTransferListHoverIndex = TransferList->ItemIndex;
  814. UpdateTransferLabel();
  815. }
  816. __finally
  817. {
  818. TransferList->Strings->EndUpdate();
  819. }
  820. }
  821. //---------------------------------------------------------------------------
  822. void __fastcall TCustomScpExplorerForm::UpdateCustomCommandsToolbar()
  823. {
  824. TTBXToolbar * Toolbar = dynamic_cast<TTBXToolbar *>(GetComponent(fcCustomCommandsBand));
  825. DebugAssert(Toolbar != NULL);
  826. NonVisualDataModule->UpdateCustomCommandsToolbar(Toolbar);
  827. }
  828. //---------------------------------------------------------------------------
  829. void __fastcall TCustomScpExplorerForm::UpdateActions()
  830. {
  831. TForm::UpdateActions();
  832. if (ComponentVisible[fcCustomCommandsBand])
  833. {
  834. UpdateCustomCommandsToolbar();
  835. }
  836. }
  837. //---------------------------------------------------------------------------
  838. void __fastcall TCustomScpExplorerForm::UpdateSessionsPageControlHeight()
  839. {
  840. SessionsPageControl->Height = SessionsPageControl->GetTabsHeight();
  841. }
  842. //---------------------------------------------------------------------------
  843. void __fastcall TCustomScpExplorerForm::ConfigurationChanged()
  844. {
  845. DebugAssert(Configuration && RemoteDirView);
  846. RemoteDirView->DDAllowMove = WinConfiguration->DDAllowMoveInit;
  847. RemoteDirView->DimmHiddenFiles = WinConfiguration->DimmHiddenFiles;
  848. RemoteDirView->ShowHiddenFiles = WinConfiguration->ShowHiddenFiles;
  849. RemoteDirView->FormatSizeBytes = WinConfiguration->FormatSizeBytes;
  850. RemoteDirView->ShowInaccesibleDirectories = WinConfiguration->ShowInaccesibleDirectories;
  851. if (RemoteDirView->RowSelect != WinConfiguration->FullRowSelect)
  852. {
  853. RemoteDirView->RowSelect = WinConfiguration->FullRowSelect;
  854. // selection is not redrawn automatically when RowSelect changes
  855. RemoteDirView->Invalidate();
  856. }
  857. RemoteDriveView->DDAllowMove = WinConfiguration->DDAllowMoveInit;
  858. RemoteDriveView->DimmHiddenDirs = WinConfiguration->DimmHiddenFiles;
  859. RemoteDriveView->ShowHiddenDirs = WinConfiguration->ShowHiddenFiles;
  860. RemoteDriveView->ShowInaccesibleDirectories = WinConfiguration->ShowInaccesibleDirectories;
  861. UpdateSessionsPageControlHeight();
  862. SetDockAllowDrag(!WinConfiguration->LockToolbars);
  863. UpdateToolbarDisplayMode();
  864. UpdateTransferList();
  865. if (Terminal != NULL)
  866. {
  867. TransferPresetAutoSelect();
  868. }
  869. UpdateQueueView();
  870. UpdateControls();
  871. // this can be called even before constuctor finishes.
  872. if (FEditorManager != NULL)
  873. {
  874. FEditorManager->ProcessFiles(FileConfigurationChanged, NULL);
  875. }
  876. // this can be called even before constuctor finishes.
  877. if (FLocalEditors != NULL)
  878. {
  879. for (int Index = 0; Index < FLocalEditors->Count; Index++)
  880. {
  881. ReconfigureEditorForm(static_cast<TForm *>(FLocalEditors->Items[Index]));
  882. }
  883. }
  884. if (ComponentVisible[fcCustomCommandsBand])
  885. {
  886. UpdateCustomCommandsToolbar();
  887. }
  888. // show only when keeping queue items forever,
  889. // otherwise, its enough to have in in the context menu
  890. QueueDeleteAllDoneQueueToolbarItem->Visible =
  891. WinConfiguration->QueueKeepDoneItems && (WinConfiguration->QueueKeepDoneItemsFor < 0);
  892. }
  893. //---------------------------------------------------------------------------
  894. void __fastcall TCustomScpExplorerForm::FileConfigurationChanged(
  895. const UnicodeString FileName, TEditedFileData * /*Data*/, TObject * Token,
  896. void * /*Arg*/)
  897. {
  898. if (Token != NULL)
  899. {
  900. TForm * Editor = dynamic_cast<TForm*>(Token);
  901. DebugAssert(Editor != NULL);
  902. ReconfigureEditorForm(Editor);
  903. }
  904. }
  905. //---------------------------------------------------------------------------
  906. void __fastcall TCustomScpExplorerForm::EnableDDTransferConfirmation(TObject * /*Sender*/)
  907. {
  908. WinConfiguration->DDTransferConfirmation = asOn;
  909. }
  910. //---------------------------------------------------------------------------
  911. bool __fastcall TCustomScpExplorerForm::CopyParamDialog(
  912. TTransferDirection Direction, TTransferType Type, bool Temp,
  913. TStrings * FileList, UnicodeString & TargetDirectory, TGUICopyParamType & CopyParam,
  914. bool Confirm, bool DragDrop, int Options)
  915. {
  916. bool Result = true;
  917. DebugAssert(Terminal && Terminal->Active);
  918. // Temp means d&d here so far, may change in future!
  919. if (Temp && (Direction == tdToLocal) && (Type == ttMove) &&
  920. !WinConfiguration->DDAllowMove)
  921. {
  922. TMessageParams Params(mpNeverAskAgainCheck);
  923. unsigned int Answer = MessageDialog(LoadStr(DND_DOWNLOAD_MOVE_WARNING), qtWarning,
  924. qaOK | qaCancel, HELP_DND_DOWNLOAD_MOVE_WARNING, &Params);
  925. if (Answer == qaNeverAskAgain)
  926. {
  927. WinConfiguration->DDAllowMove = true;
  928. }
  929. else if (Answer == qaCancel)
  930. {
  931. Result = false;
  932. }
  933. }
  934. // these parameters are known in advance
  935. int Params =
  936. FLAGMASK(Type == ttMove, cpDelete);
  937. bool ToTemp = (Temp && (Direction == tdToLocal));
  938. if (Result && Confirm && WinConfiguration->ConfirmTransferring)
  939. {
  940. bool DisableNewerOnly =
  941. (!Terminal->IsCapable[fcNewerOnlyUpload] && (Direction == tdToRemote)) ||
  942. ToTemp;
  943. Options |=
  944. FLAGMASK(ToTemp, coTemp) |
  945. coDoNotShowAgain;
  946. TUsableCopyParamAttrs UsableCopyParamAttrs = Terminal->UsableCopyParamAttrs(Params);
  947. int CopyParamAttrs = (Direction == tdToRemote ?
  948. UsableCopyParamAttrs.Upload : UsableCopyParamAttrs.Download) |
  949. FLAGMASK(DisableNewerOnly, cpaNoNewerOnly);
  950. int OutputOptions =
  951. FLAGMASK(DragDrop && (WinConfiguration->DDTransferConfirmation == asAuto),
  952. cooDoNotShowAgain);
  953. std::unique_ptr<TSessionData> SessionData(SessionDataForCode());
  954. Result = DoCopyDialog(Direction == tdToRemote, Type == ttMove,
  955. FileList, TargetDirectory, &CopyParam, Options, CopyParamAttrs, SessionData.get(), &OutputOptions);
  956. if (Result)
  957. {
  958. if (FLAGSET(OutputOptions, cooDoNotShowAgain))
  959. {
  960. if (DragDrop)
  961. {
  962. if (WinConfiguration->DDTransferConfirmation == asAuto)
  963. {
  964. PopupTrayBalloon(NULL, LoadStr(DD_TRANSFER_CONFIRM_OFF2), qtInformation,
  965. NULL, 0, EnableDDTransferConfirmation, NULL);
  966. }
  967. WinConfiguration->DDTransferConfirmation = asOff;
  968. }
  969. else
  970. {
  971. WinConfiguration->ConfirmTransferring = false;
  972. }
  973. }
  974. else
  975. {
  976. // User exclicitly unchecked "do not show again",
  977. // so show him the dialog the next time
  978. if (DragDrop && (WinConfiguration->DDTransferConfirmation == asAuto))
  979. {
  980. WinConfiguration->DDTransferConfirmation = asOn;
  981. }
  982. }
  983. }
  984. }
  985. if (Result && CopyParam.Queue && !ToTemp)
  986. {
  987. Configuration->Usage->Inc(L"TransfersOnBackground");
  988. // these parameter are known only after transfer dialog
  989. Params |=
  990. (CopyParam.QueueNoConfirmation ? cpNoConfirmation : 0);
  991. if (CopyParam.QueueIndividually)
  992. {
  993. for (int Index = 0; Index < FileList->Count; Index++)
  994. {
  995. TStrings * FileList1 = new TStringList();
  996. try
  997. {
  998. FileList1->AddObject(FileList->Strings[Index], FileList->Objects[Index]);
  999. AddQueueItem(Queue, Direction, FileList1, TargetDirectory, CopyParam, Params);
  1000. }
  1001. __finally
  1002. {
  1003. delete FileList1;
  1004. }
  1005. }
  1006. }
  1007. else
  1008. {
  1009. AddQueueItem(Queue, Direction, FileList, TargetDirectory, CopyParam, Params);
  1010. }
  1011. Result = false;
  1012. ClearTransferSourceSelection(Direction);
  1013. }
  1014. return Result;
  1015. }
  1016. //---------------------------------------------------------------------------
  1017. void __fastcall TCustomScpExplorerForm::ClearTransferSourceSelection(TTransferDirection Direction)
  1018. {
  1019. TOperationSide Side = ((Direction == tdToRemote) ? osLocal : osRemote);
  1020. if (HasDirView[Side])
  1021. {
  1022. DirView(Side)->SelectAll(smNone);
  1023. }
  1024. }
  1025. //---------------------------------------------------------------------------
  1026. void __fastcall TCustomScpExplorerForm::AddQueueItem(
  1027. TTerminalQueue * Queue, TTransferDirection Direction, TStrings * FileList,
  1028. const UnicodeString TargetDirectory, const TCopyParamType & CopyParam,
  1029. int Params)
  1030. {
  1031. DebugAssert(Queue != NULL);
  1032. bool SingleFile = false;
  1033. if (FileList->Count == 1)
  1034. {
  1035. if (Direction == tdToRemote)
  1036. {
  1037. UnicodeString FileName = FileList->Strings[0];
  1038. SingleFile = FileExists(ApiPath(FileName));
  1039. }
  1040. else
  1041. {
  1042. TRemoteFile * File = static_cast<TRemoteFile *>(FileList->Objects[0]);
  1043. SingleFile = !File->IsDirectory;
  1044. }
  1045. }
  1046. TQueueItem * QueueItem;
  1047. if (Direction == tdToRemote)
  1048. {
  1049. QueueItem = new TUploadQueueItem(Terminal, FileList, TargetDirectory,
  1050. &CopyParam, Params, SingleFile);
  1051. }
  1052. else
  1053. {
  1054. QueueItem = new TDownloadQueueItem(Terminal, FileList, TargetDirectory,
  1055. &CopyParam, Params, SingleFile);
  1056. }
  1057. AddQueueItem(Queue, QueueItem, Terminal);
  1058. }
  1059. //---------------------------------------------------------------------------
  1060. void __fastcall TCustomScpExplorerForm::AddQueueItem(TTerminalQueue * Queue, TQueueItem * QueueItem, TTerminal * Terminal)
  1061. {
  1062. if (Queue->IsEmpty)
  1063. {
  1064. TManagedTerminal * ManagedTerminal = DebugNotNull(dynamic_cast<TManagedTerminal *>(Terminal));
  1065. ManagedTerminal->QueueOperationStart = Now();
  1066. }
  1067. Queue->AddItem(QueueItem);
  1068. }
  1069. //---------------------------------------------------------------------------
  1070. void __fastcall TCustomScpExplorerForm::RestoreFormParams()
  1071. {
  1072. }
  1073. //---------------------------------------------------------------------------
  1074. void __fastcall TCustomScpExplorerForm::RestoreParams()
  1075. {
  1076. DebugAssert(FDocks != NULL);
  1077. for (int Index = 0; Index < ComponentCount; Index++)
  1078. {
  1079. TTBDock * Dock = dynamic_cast<TTBDock *>(Components[Index]);
  1080. if ((Dock != NULL) && (Dock->Tag == 0))
  1081. {
  1082. FDocks->Add(Dock);
  1083. }
  1084. }
  1085. CollectItemsWithTextDisplayMode(this);
  1086. ConfigurationChanged();
  1087. QueuePanel->Height = LoadDimension(WinConfiguration->QueueView.Height, WinConfiguration->QueueView.HeightPixelsPerInch);
  1088. LoadListViewStr(QueueView3, WinConfiguration->QueueView.Layout);
  1089. QueueDock->Visible = WinConfiguration->QueueView.ToolBar;
  1090. QueueLabel->Visible = WinConfiguration->QueueView.Label;
  1091. }
  1092. //---------------------------------------------------------------------------
  1093. void __fastcall TCustomScpExplorerForm::StoreParams()
  1094. {
  1095. WinConfiguration->QueueView.Height = QueuePanel->Height;
  1096. WinConfiguration->QueueView.HeightPixelsPerInch = Screen->PixelsPerInch;
  1097. WinConfiguration->QueueView.Layout = GetListViewStr(QueueView3);
  1098. WinConfiguration->QueueView.ToolBar = QueueDock->Visible;
  1099. WinConfiguration->QueueView.Label = QueueLabel->Visible;
  1100. }
  1101. //---------------------------------------------------------------------------
  1102. void __fastcall TCustomScpExplorerForm::CreateParams(TCreateParams & Params)
  1103. {
  1104. if (!FFormRestored)
  1105. {
  1106. FFormRestored = true;
  1107. RestoreFormParams();
  1108. }
  1109. TForm::CreateParams(Params);
  1110. }
  1111. //---------------------------------------------------------------------------
  1112. void __fastcall TCustomScpExplorerForm::SetDockAllowDrag(bool value)
  1113. {
  1114. DebugAssert(FDocks != NULL);
  1115. for (int Index = 0; Index < FDocks->Count; Index++)
  1116. {
  1117. static_cast<TTBDock*>(FDocks->Items[Index])->AllowDrag = value;
  1118. }
  1119. }
  1120. //---------------------------------------------------------------------------
  1121. void __fastcall TCustomScpExplorerForm::LoadToolbarsLayoutStr(UnicodeString LayoutStr)
  1122. {
  1123. SetDockAllowDrag(true);
  1124. ::LoadToolbarsLayoutStr(this, LayoutStr);
  1125. SetDockAllowDrag(!WinConfiguration->LockToolbars);
  1126. }
  1127. //---------------------------------------------------------------------------
  1128. UnicodeString __fastcall TCustomScpExplorerForm::GetToolbarsLayoutStr()
  1129. {
  1130. UnicodeString Result;
  1131. SetDockAllowDrag(true);
  1132. Result = ::GetToolbarsLayoutStr(this);
  1133. SetDockAllowDrag(!WinConfiguration->LockToolbars);
  1134. return Result;
  1135. }
  1136. //---------------------------------------------------------------------------
  1137. void __fastcall TCustomScpExplorerForm::FileOperationProgress(
  1138. TFileOperationProgressType & ProgressData)
  1139. {
  1140. // Download to temporary local directory
  1141. if (FPendingTempSpaceWarn && ProgressData.InProgress && ProgressData.TotalSizeSet)
  1142. {
  1143. bool Continue = true;
  1144. FPendingTempSpaceWarn = false;
  1145. DoWarnLackOfTempSpace(ProgressData.Directory, ProgressData.TotalSize, Continue);
  1146. if (!Continue)
  1147. {
  1148. Abort();
  1149. }
  1150. }
  1151. // operation is being executed and we still didn't show up progress form
  1152. if (ProgressData.InProgress && !FProgressForm)
  1153. {
  1154. FProgressForm = new TProgressForm(Application, (FTransferResumeList != NULL));
  1155. // As progress window has delayed show now, we need to lock ourselves,
  1156. // (at least) until then
  1157. LockWindow();
  1158. FProgressForm->DeleteToRecycleBin = (ProgressData.Side == osLocal ?
  1159. (WinConfiguration->DeleteToRecycleBin != FAlternativeDelete) :
  1160. ((Terminal->SessionData->DeleteToRecycleBin != FAlternativeDelete) &&
  1161. !Terminal->SessionData->RecycleBinPath.IsEmpty()));
  1162. // When main window is hidden, synchronisation form does not exist,
  1163. // we suppose "/upload" or URL download mode
  1164. if (!Visible && (ProgressData.Operation != foCalculateSize) &&
  1165. (ProgressData.Operation != foCalculateChecksum) &&
  1166. (FSynchronizeProgressForm == NULL) &&
  1167. !FStandaloneEditing)
  1168. {
  1169. FProgressForm->OnceDoneOperation = odoDisconnect;
  1170. }
  1171. if (FTaskbarList != NULL)
  1172. {
  1173. // Actually, do not know what hides the progress once the operation finishes
  1174. SetTaskbarListProgressState(TBPF_NORMAL);
  1175. }
  1176. }
  1177. // operation is finished (or terminated), so we hide progress form
  1178. else if (!ProgressData.InProgress && FProgressForm)
  1179. {
  1180. UnlockWindow();
  1181. SAFE_DESTROY(FProgressForm);
  1182. SetQueueProgress();
  1183. if (ProgressData.Operation == foCalculateSize)
  1184. {
  1185. // When calculating size before transfer, the abort caused by
  1186. // cancel flag set due to "MoveToQueue" below,
  1187. // is not propagated back to us in ExecuteFileOperation,
  1188. // as it is silently swallowed.
  1189. // So we explicitly (re)throw it here.
  1190. if (FMoveToQueue)
  1191. {
  1192. Abort();
  1193. }
  1194. }
  1195. else
  1196. {
  1197. if ((ProgressData.Cancel == csContinue) &&
  1198. !FAutoOperation)
  1199. {
  1200. OperationComplete(ProgressData.StartTime);
  1201. }
  1202. }
  1203. }
  1204. if (FProgressForm)
  1205. {
  1206. FProgressForm->SetProgressData(ProgressData);
  1207. if (FTaskbarList != NULL)
  1208. {
  1209. DebugAssert(ProgressData.InProgress);
  1210. SetTaskbarListProgressValue(&ProgressData);
  1211. }
  1212. if (FProgressForm->Cancel > csContinue)
  1213. {
  1214. if (FProgressForm->Cancel > ProgressData.Cancel)
  1215. {
  1216. ProgressData.Cancel = FProgressForm->Cancel;
  1217. }
  1218. // cancel cancels even the move
  1219. FMoveToQueue = false;
  1220. }
  1221. else if (FProgressForm->MoveToQueue)
  1222. {
  1223. FMoveToQueue = true;
  1224. if (ProgressData.Cancel < csCancel)
  1225. {
  1226. ProgressData.Cancel = csCancel;
  1227. }
  1228. }
  1229. if ((FTransferResumeList != NULL) &&
  1230. ProgressData.InProgress &&
  1231. ((ProgressData.Operation == foCopy) || (ProgressData.Operation == foMove)) &&
  1232. !ProgressData.FullFileName.IsEmpty())
  1233. {
  1234. if ((FTransferResumeList->Count == 0) ||
  1235. (FTransferResumeList->Strings[FTransferResumeList->Count - 1] != ProgressData.FullFileName))
  1236. {
  1237. // note that we do not recognize directories from files here
  1238. FTransferResumeList->Add(ProgressData.FullFileName);
  1239. }
  1240. }
  1241. }
  1242. }
  1243. //---------------------------------------------------------------------------
  1244. void __fastcall TCustomScpExplorerForm::OperationComplete(
  1245. const TDateTime & StartTime)
  1246. {
  1247. if (GUIConfiguration->BeepOnFinish &&
  1248. (Now() - StartTime > GUIConfiguration->BeepOnFinishAfter))
  1249. {
  1250. MessageBeep(MB_OK);
  1251. }
  1252. }
  1253. //---------------------------------------------------------------------------
  1254. void __fastcall TCustomScpExplorerForm::OperationProgress(
  1255. TFileOperationProgressType & ProgressData)
  1256. {
  1257. FileOperationProgress(ProgressData);
  1258. }
  1259. //---------------------------------------------------------------------------
  1260. bool __fastcall TCustomScpExplorerForm::PanelOperation(TOperationSide /*Side*/,
  1261. bool DragDrop)
  1262. {
  1263. return (!DragDrop && (DropSourceControl == NULL)) ||
  1264. (DropSourceControl == RemoteDirView);
  1265. }
  1266. //---------------------------------------------------------------------------
  1267. void __fastcall TCustomScpExplorerForm::DoOperationFinished(
  1268. TFileOperation Operation, TOperationSide Side,
  1269. bool /*Temp*/, const UnicodeString & FileName, bool Success,
  1270. TOnceDoneOperation & OnceDoneOperation)
  1271. {
  1272. if (!FAutoOperation)
  1273. {
  1274. // no selection on "/upload", form servers only as event handler
  1275. // (it is not displayed)
  1276. if (PanelOperation(Side, FDragDropOperation) &&
  1277. Visible && (Operation != foCalculateSize) &&
  1278. (Operation != foGetProperties) && (Operation != foCalculateChecksum))
  1279. {
  1280. TCustomDirView * DView = DirView(Side);
  1281. UnicodeString FileNameOnly = ExtractFileName(FileName, (Side == osRemote));
  1282. TListItem *Item = DView->FindFileItem(FileNameOnly);
  1283. // this can happen when local drive is unplugged in the middle of the operation
  1284. if (Item != NULL)
  1285. {
  1286. if (Success) Item->Selected = false;
  1287. if (DView->ViewStyle == vsReport)
  1288. {
  1289. TRect DisplayRect = Item->DisplayRect(drBounds);
  1290. if (DisplayRect.Bottom > DView->ClientHeight)
  1291. {
  1292. DView->Scroll(0, Item->Top - DView->TopItem->Top);
  1293. }
  1294. }
  1295. Item->MakeVisible(false);
  1296. }
  1297. }
  1298. if ((Operation == foCopy) || (Operation == foMove))
  1299. {
  1300. if (Side == osLocal)
  1301. {
  1302. Configuration->Usage->Inc(L"UploadedFiles");
  1303. }
  1304. else
  1305. {
  1306. Configuration->Usage->Inc(L"DownloadedFiles");
  1307. }
  1308. }
  1309. }
  1310. if (Success && (FSynchronizeController != NULL))
  1311. {
  1312. if (Operation == foCopy)
  1313. {
  1314. DebugAssert(Side == osLocal);
  1315. FSynchronizeController->LogOperation(soUpload, FileName);
  1316. }
  1317. else if (Operation == foDelete)
  1318. {
  1319. DebugAssert(Side == osRemote);
  1320. FSynchronizeController->LogOperation(soDelete, FileName);
  1321. }
  1322. }
  1323. if (FProgressForm)
  1324. {
  1325. OnceDoneOperation = FProgressForm->OnceDoneOperation;
  1326. }
  1327. }
  1328. //---------------------------------------------------------------------------
  1329. void __fastcall TCustomScpExplorerForm::OperationFinished(
  1330. TFileOperation Operation, TOperationSide Side,
  1331. bool Temp, const UnicodeString & FileName, Boolean Success,
  1332. TOnceDoneOperation & OnceDoneOperation)
  1333. {
  1334. DoOperationFinished(Operation, Side, Temp, FileName, Success,
  1335. OnceDoneOperation);
  1336. }
  1337. //---------------------------------------------------------------------------
  1338. TCustomDirView * __fastcall TCustomScpExplorerForm::DirView(TOperationSide Side)
  1339. {
  1340. DebugAssert(GetSide(Side) == osRemote);
  1341. DebugUsedParam(Side);
  1342. return RemoteDirView;
  1343. }
  1344. //---------------------------------------------------------------------------
  1345. bool __fastcall TCustomScpExplorerForm::DirViewEnabled(TOperationSide Side)
  1346. {
  1347. DebugAssert(GetSide(Side) == osRemote);
  1348. DebugUsedParam(Side);
  1349. return (Terminal != NULL);
  1350. }
  1351. //---------------------------------------------------------------------------
  1352. bool __fastcall TCustomScpExplorerForm::GetEnableFocusedOperation(
  1353. TOperationSide Side, int FilesOnly)
  1354. {
  1355. return DirView(Side)->AnyFileSelected(true, (FilesOnly != 0), true);
  1356. }
  1357. //---------------------------------------------------------------------------
  1358. bool __fastcall TCustomScpExplorerForm::GetEnableSelectedOperation(
  1359. TOperationSide Side, int FilesOnly)
  1360. {
  1361. return DirView(Side)->AnyFileSelected(false, (FilesOnly != 0), true);
  1362. }
  1363. //---------------------------------------------------------------------------
  1364. struct THistoryItemData
  1365. {
  1366. short int Side;
  1367. short int Index;
  1368. };
  1369. //---------------------------------------------------------------------------
  1370. void __fastcall TCustomScpExplorerForm::HistoryGo(TOperationSide Side, int Index)
  1371. {
  1372. DirView(Side)->HistoryGo(Index);
  1373. }
  1374. //---------------------------------------------------------------------------
  1375. void __fastcall TCustomScpExplorerForm::HistoryItemClick(System::TObject* Sender)
  1376. {
  1377. TTBCustomItem * Item = dynamic_cast<TTBCustomItem *>(Sender);
  1378. THistoryItemData Data = *reinterpret_cast<THistoryItemData*>(&(Item->Tag));
  1379. HistoryGo((TOperationSide)Data.Side, Data.Index);
  1380. }
  1381. //---------------------------------------------------------------------------
  1382. void __fastcall TCustomScpExplorerForm::UpdateHistoryMenu(TOperationSide Side,
  1383. bool Back)
  1384. {
  1385. if (FHistoryMenu[Side == osLocal][Back] != NULL)
  1386. {
  1387. TCustomDirView * DView = DirView(Side);
  1388. TTBXPopupMenu * Menu = FHistoryMenu[Side == osLocal][Back];
  1389. int ICount = Back ? DView->BackCount : DView->ForwardCount;
  1390. if (ICount > 10)
  1391. {
  1392. ICount = 10;
  1393. }
  1394. Menu->Items->Clear();
  1395. THistoryItemData Data;
  1396. Data.Side = Side;
  1397. for (short int i = 1; i <= ICount; i++)
  1398. {
  1399. TTBCustomItem * Item = new TTBXItem(Menu);
  1400. Data.Index = static_cast<short int>(i * (Back ? -1 : 1));
  1401. Item->Caption = MinimizeName(DView->HistoryPath[Data.Index], 50, (Side == osRemote));
  1402. Item->Hint = DView->HistoryPath[Data.Index];
  1403. DebugAssert(sizeof(int) == sizeof(THistoryItemData));
  1404. Item->Tag = *reinterpret_cast<int*>(&Data);
  1405. Item->OnClick = HistoryItemClick;
  1406. Menu->Items->Add(Item);
  1407. }
  1408. }
  1409. }
  1410. //---------------------------------------------------------------------------
  1411. TTBXPopupMenu * __fastcall TCustomScpExplorerForm::HistoryMenu(
  1412. TOperationSide Side, bool Back)
  1413. {
  1414. if (FHistoryMenu[Side == osLocal][Back] == NULL)
  1415. {
  1416. // workaround
  1417. // In Pascal the size of TTBXPopupMenu is 132, in C++ 136,
  1418. // operator new allocates memory in Pascal code, but calls inline
  1419. // contructor in C++, leading in problems, the function does
  1420. // both in Pascal code
  1421. FHistoryMenu[Side == osLocal][Back] = CreateTBXPopupMenu(this);
  1422. UpdateHistoryMenu(Side, Back);
  1423. }
  1424. return FHistoryMenu[Side == osLocal][Back];
  1425. }
  1426. //---------------------------------------------------------------------------
  1427. void __fastcall TCustomScpExplorerForm::DirViewHistoryChange(
  1428. TCustomDirView *Sender)
  1429. {
  1430. TOperationSide Side = (Sender == DirView(osRemote) ? osRemote : osLocal);
  1431. UpdateHistoryMenu(Side, true);
  1432. UpdateHistoryMenu(Side, false);
  1433. }
  1434. //---------------------------------------------------------------------------
  1435. bool __fastcall TCustomScpExplorerForm::CustomCommandRemoteAllowed()
  1436. {
  1437. // remote custom commands can be executed only if the server supports shell commands
  1438. // or have secondary shell
  1439. return (FTerminal != NULL) && (FTerminal->IsCapable[fcSecondaryShell] || FTerminal->IsCapable[fcShellAnyCommand]);
  1440. }
  1441. //---------------------------------------------------------------------------
  1442. int __fastcall TCustomScpExplorerForm::BothCustomCommandState(const TCustomCommandType & Command)
  1443. {
  1444. bool Result = FLAGSET(Command.Params, ccLocal);
  1445. if (Result)
  1446. {
  1447. TLocalCustomCommand LocalCustomCommand;
  1448. TInteractiveCustomCommand InteractiveCustomCommand(&LocalCustomCommand);
  1449. UnicodeString Cmd = InteractiveCustomCommand.Complete(Command.Command, false);
  1450. Result =
  1451. LocalCustomCommand.IsFileCommand(Cmd) &&
  1452. LocalCustomCommand.HasLocalFileName(Cmd);
  1453. }
  1454. return Result ? 1 : -1;
  1455. }
  1456. //---------------------------------------------------------------------------
  1457. int __fastcall TCustomScpExplorerForm::CustomCommandState(
  1458. const TCustomCommandType & Command, bool OnFocused)
  1459. {
  1460. int Result;
  1461. TFileCustomCommand RemoteCustomCommand;
  1462. TLocalCustomCommand LocalCustomCommand;
  1463. TFileCustomCommand * NonInteractiveCustomCommand =
  1464. FLAGCLEAR(Command.Params, ccLocal) ? &RemoteCustomCommand : &LocalCustomCommand;
  1465. TInteractiveCustomCommand InteractiveCustomCommand(NonInteractiveCustomCommand);
  1466. UnicodeString Cmd = InteractiveCustomCommand.Complete(Command.Command, false);
  1467. if (FLAGCLEAR(Command.Params, ccLocal))
  1468. {
  1469. Result = CustomCommandRemoteAllowed();
  1470. if (Result)
  1471. {
  1472. // custom command that does not operate with files can be executed anytime ...
  1473. if (!NonInteractiveCustomCommand->IsFileCommand(Cmd))
  1474. {
  1475. // ... but do not show such command in remote file menu
  1476. // (TODO, currently custom commands are in file menu only, so we cannot hide
  1477. // such command, because it won't be accessible from elsewhere)
  1478. Result = OnFocused ? /*-1*/ true : true;
  1479. }
  1480. else
  1481. {
  1482. Result = (FCurrentSide == osRemote) && EnableSelectedOperation[osRemote];
  1483. }
  1484. }
  1485. }
  1486. else
  1487. {
  1488. // custom command that does not operate with files can be executed anytime
  1489. if (!NonInteractiveCustomCommand->IsFileCommand(Cmd))
  1490. {
  1491. Result = true;
  1492. }
  1493. else if (LocalCustomCommand.HasLocalFileName(Cmd))
  1494. {
  1495. // special case is "diff"-style command that can be executed over any side,
  1496. // if we have both sides
  1497. Result =
  1498. // Cannot have focus on both panels, so we have to call AnyFileSelected
  1499. // directly (instead of EnableSelectedOperation) to pass
  1500. // false to FocusedFileOnlyWhenFocused when panel is inactive.
  1501. (HasDirView[osLocal] && DirView(osLocal)->AnyFileSelected(false, false, (FCurrentSide == osLocal))) &&
  1502. DirView(osRemote)->AnyFileSelected(false, false, (FCurrentSide == osRemote));
  1503. }
  1504. else
  1505. {
  1506. // other local custom commands can be executed only on remote side
  1507. Result = EnableSelectedOperation[FCurrentSide];
  1508. }
  1509. }
  1510. return Result;
  1511. }
  1512. //---------------------------------------------------------------------------
  1513. void __fastcall TCustomScpExplorerForm::CustomCommand(TStrings * FileList,
  1514. const TCustomCommandType & ACommand, TStrings * ALocalFileList)
  1515. {
  1516. if (FLAGCLEAR(ACommand.Params, ccLocal))
  1517. {
  1518. if (EnsureCommandSessionFallback(fcShellAnyCommand))
  1519. {
  1520. TCustomCommandData Data(Terminal);
  1521. TRemoteCustomCommand RemoteCustomCommand(Data, Terminal->CurrentDirectory);
  1522. TWinInteractiveCustomCommand InteractiveCustomCommand(
  1523. &RemoteCustomCommand, ACommand.Name);
  1524. UnicodeString Command = InteractiveCustomCommand.Complete(ACommand.Command, false);
  1525. Configuration->Usage->Inc(L"RemoteCustomCommandRuns2");
  1526. bool Capture = FLAGSET(ACommand.Params, ccShowResults) || FLAGSET(ACommand.Params, ccCopyResults);
  1527. TCaptureOutputEvent OutputEvent = NULL;
  1528. DebugAssert(FCapturedLog == NULL);
  1529. if (Capture)
  1530. {
  1531. FCapturedLog = new TStringList();
  1532. OutputEvent = TerminalCaptureLog;
  1533. }
  1534. try
  1535. {
  1536. if (!RemoteCustomCommand.IsFileCommand(Command))
  1537. {
  1538. Terminal->AnyCommand(RemoteCustomCommand.Complete(Command, true),
  1539. OutputEvent);
  1540. }
  1541. else
  1542. {
  1543. Terminal->CustomCommandOnFiles(Command, ACommand.Params, FileList, OutputEvent);
  1544. }
  1545. if ((FCapturedLog != NULL) && (FCapturedLog->Count > 0))
  1546. {
  1547. if (FLAGSET(ACommand.Params, ccCopyResults))
  1548. {
  1549. CopyToClipboard(FCapturedLog);
  1550. }
  1551. if (FLAGSET(ACommand.Params, ccShowResults))
  1552. {
  1553. DoConsoleDialog(Terminal, L"", FCapturedLog);
  1554. }
  1555. }
  1556. }
  1557. __finally
  1558. {
  1559. SAFE_DESTROY(FCapturedLog);
  1560. }
  1561. }
  1562. }
  1563. else
  1564. {
  1565. TCustomCommandData Data(Terminal);
  1566. TLocalCustomCommand LocalCustomCommand(Data, Terminal->CurrentDirectory);
  1567. TWinInteractiveCustomCommand InteractiveCustomCommand(
  1568. &LocalCustomCommand, ACommand.Name);
  1569. UnicodeString Command = InteractiveCustomCommand.Complete(ACommand.Command, false);
  1570. bool FileListCommand = LocalCustomCommand.IsFileListCommand(Command);
  1571. bool LocalFileCommand = LocalCustomCommand.HasLocalFileName(Command);
  1572. Configuration->Usage->Inc(L"LocalCustomCommandRuns2");
  1573. if (!LocalCustomCommand.IsFileCommand(Command))
  1574. {
  1575. ExecuteShellAndWait(LocalCustomCommand.Complete(Command, true));
  1576. }
  1577. // remote files?
  1578. else if ((FCurrentSide == osRemote) || LocalFileCommand)
  1579. {
  1580. TStrings * LocalFileList = NULL;
  1581. TStrings * RemoteFileList = NULL;
  1582. try
  1583. {
  1584. if (LocalFileCommand)
  1585. {
  1586. if (ALocalFileList == NULL)
  1587. {
  1588. DebugAssert(HasDirView[osLocal]);
  1589. // Cannot have focus on both panels, so we have to call AnyFileSelected
  1590. // directly (instead of EnableSelectedOperation) to pass
  1591. // false to FocusedFileOnlyWhenFocused
  1592. DebugAssert(DirView(osLocal)->AnyFileSelected(false, false, false));
  1593. LocalFileList = DirView(osLocal)->CreateFileList(false, true, NULL);
  1594. }
  1595. else
  1596. {
  1597. LocalFileList = ALocalFileList;
  1598. }
  1599. if (FileListCommand)
  1600. {
  1601. if (LocalFileList->Count != 1)
  1602. {
  1603. throw Exception(LoadStr(CUSTOM_COMMAND_SELECTED_UNMATCH1));
  1604. }
  1605. }
  1606. else
  1607. {
  1608. if ((LocalFileList->Count != 1) &&
  1609. (FileList->Count != 1) &&
  1610. (LocalFileList->Count != FileList->Count))
  1611. {
  1612. throw Exception(LoadStr(CUSTOM_COMMAND_SELECTED_UNMATCH));
  1613. }
  1614. }
  1615. }
  1616. UnicodeString RootTempDir;
  1617. UnicodeString TempDir;
  1618. bool RemoteFiles = FLAGSET(ACommand.Params, ccRemoteFiles);
  1619. if (!RemoteFiles)
  1620. {
  1621. TemporarilyDownloadFiles(FileList, false, RootTempDir, TempDir, false, false, true);
  1622. }
  1623. try
  1624. {
  1625. TDateTimes RemoteFileTimes;
  1626. if (RemoteFiles)
  1627. {
  1628. RemoteFileList = FileList;
  1629. }
  1630. else
  1631. {
  1632. RemoteFileList = new TStringList();
  1633. TMakeLocalFileListParams MakeFileListParam;
  1634. MakeFileListParam.FileList = RemoteFileList;
  1635. MakeFileListParam.FileTimes = &RemoteFileTimes;
  1636. MakeFileListParam.IncludeDirs = FLAGSET(ACommand.Params, ccApplyToDirectories);
  1637. MakeFileListParam.Recursive =
  1638. FLAGSET(ACommand.Params, ccRecursive) && !FileListCommand;
  1639. ProcessLocalDirectory(TempDir, Terminal->MakeLocalFileList, &MakeFileListParam);
  1640. }
  1641. TFileOperationProgressType Progress(&OperationProgress, &OperationFinished);
  1642. Progress.Start(foCustomCommand, osRemote, FileListCommand ? 1 : FileList->Count);
  1643. DebugAssert(FProgressForm != NULL);
  1644. FProgressForm->ReadOnly = true;
  1645. try
  1646. {
  1647. if (FileListCommand)
  1648. {
  1649. UnicodeString LocalFile;
  1650. // MakeFileList does not delimit filenames
  1651. UnicodeString FileList = MakeFileList(RemoteFileList);
  1652. if (LocalFileCommand)
  1653. {
  1654. DebugAssert(LocalFileList->Count == 1);
  1655. LocalFile = LocalFileList->Strings[0];
  1656. }
  1657. TCustomCommandData Data(FTerminal);
  1658. TLocalCustomCommand CustomCommand(Data,
  1659. Terminal->CurrentDirectory, L"", LocalFile, FileList);
  1660. ExecuteShellAndWait(CustomCommand.Complete(Command, true));
  1661. }
  1662. else if (LocalFileCommand)
  1663. {
  1664. if (LocalFileList->Count == 1)
  1665. {
  1666. UnicodeString LocalFile = LocalFileList->Strings[0];
  1667. for (int Index = 0; Index < RemoteFileList->Count; Index++)
  1668. {
  1669. UnicodeString FileName = RemoteFileList->Strings[Index];
  1670. TCustomCommandData Data(FTerminal);
  1671. TLocalCustomCommand CustomCommand(Data,
  1672. Terminal->CurrentDirectory, FileName, LocalFile, L"");
  1673. ExecuteShellAndWait(CustomCommand.Complete(Command, true));
  1674. }
  1675. }
  1676. else if (RemoteFileList->Count == 1)
  1677. {
  1678. UnicodeString FileName = RemoteFileList->Strings[0];
  1679. for (int Index = 0; Index < LocalFileList->Count; Index++)
  1680. {
  1681. TCustomCommandData Data(FTerminal);
  1682. TLocalCustomCommand CustomCommand(
  1683. Data, Terminal->CurrentDirectory,
  1684. FileName, LocalFileList->Strings[Index], L"");
  1685. ExecuteShellAndWait(CustomCommand.Complete(Command, true));
  1686. }
  1687. }
  1688. else
  1689. {
  1690. if (LocalFileList->Count != RemoteFileList->Count)
  1691. {
  1692. throw Exception(LoadStr(CUSTOM_COMMAND_PAIRS_DOWNLOAD_FAILED));
  1693. }
  1694. for (int Index = 0; Index < LocalFileList->Count; Index++)
  1695. {
  1696. UnicodeString FileName = RemoteFileList->Strings[Index];
  1697. TCustomCommandData Data(FTerminal);
  1698. TLocalCustomCommand CustomCommand(
  1699. Data, Terminal->CurrentDirectory,
  1700. FileName, LocalFileList->Strings[Index], L"");
  1701. ExecuteShellAndWait(CustomCommand.Complete(Command, true));
  1702. }
  1703. }
  1704. }
  1705. else
  1706. {
  1707. for (int Index = 0; Index < RemoteFileList->Count; Index++)
  1708. {
  1709. TCustomCommandData Data(FTerminal);
  1710. TLocalCustomCommand CustomCommand(Data,
  1711. Terminal->CurrentDirectory, RemoteFileList->Strings[Index], L"", L"");
  1712. ExecuteShellAndWait(CustomCommand.Complete(Command, true));
  1713. }
  1714. }
  1715. }
  1716. __finally
  1717. {
  1718. Progress.Stop();
  1719. }
  1720. DebugAssert(!FAutoOperation);
  1721. if (!RemoteFiles)
  1722. {
  1723. TempDir = IncludeTrailingBackslash(TempDir);
  1724. for (int Index = 0; Index < RemoteFileList->Count; Index++)
  1725. {
  1726. UnicodeString FileName = RemoteFileList->Strings[Index];
  1727. if (DebugAlwaysTrue(SameText(TempDir, FileName.SubString(1, TempDir.Length()))) &&
  1728. // Skip directories for now, they require recursion,
  1729. // and we do not have original nested files times available here yet.
  1730. // The check is redundant as FileAge fails for directories anyway.
  1731. !DirectoryExists(FileName))
  1732. {
  1733. UnicodeString RemoteDir =
  1734. UnixExtractFileDir(
  1735. UnixIncludeTrailingBackslash(FTerminal->CurrentDirectory) +
  1736. ToUnixPath(FileName.SubString(TempDir.Length() + 1, FileName.Length() - TempDir.Length())));
  1737. TDateTime NewTime;
  1738. if (FileAge(FileName, NewTime) &&
  1739. (NewTime != RemoteFileTimes[Index]))
  1740. {
  1741. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  1742. TemporaryFileCopyParam(CopyParam);
  1743. CopyParam.FileMask = L"";
  1744. FAutoOperation = true;
  1745. std::unique_ptr<TStrings> TemporaryFilesList(new TStringList());
  1746. TemporaryFilesList->Add(FileName);
  1747. FTerminal->CopyToRemote(TemporaryFilesList.get(), RemoteDir, &CopyParam, cpTemporary);
  1748. }
  1749. }
  1750. }
  1751. }
  1752. }
  1753. __finally
  1754. {
  1755. FAutoOperation = false;
  1756. if (!RootTempDir.IsEmpty() && DebugAlwaysTrue(!RemoteFiles))
  1757. {
  1758. RecursiveDeleteFile(ExcludeTrailingBackslash(RootTempDir), false);
  1759. }
  1760. }
  1761. }
  1762. __finally
  1763. {
  1764. if (RemoteFileList != FileList)
  1765. {
  1766. delete RemoteFileList;
  1767. }
  1768. if (LocalFileList != ALocalFileList)
  1769. {
  1770. delete LocalFileList;
  1771. }
  1772. }
  1773. }
  1774. // local files
  1775. else
  1776. {
  1777. std::unique_ptr<TStrings> SelectedFileList(DirView(osLocal)->CreateFileList(false, true, NULL));
  1778. std::unique_ptr<TStrings> LocalFileList(new TStringList());
  1779. for (int Index = 0; Index < SelectedFileList->Count; Index++)
  1780. {
  1781. UnicodeString FileName = SelectedFileList->Strings[Index];
  1782. if (DirectoryExists(FileName))
  1783. {
  1784. if (FLAGSET(ACommand.Params, ccApplyToDirectories))
  1785. {
  1786. LocalFileList->Add(FileName);
  1787. }
  1788. if (FLAGSET(ACommand.Params, ccRecursive))
  1789. {
  1790. TMakeLocalFileListParams MakeFileListParam;
  1791. MakeFileListParam.FileList = LocalFileList.get();
  1792. MakeFileListParam.FileTimes = NULL;
  1793. MakeFileListParam.IncludeDirs = FLAGSET(ACommand.Params, ccApplyToDirectories);
  1794. MakeFileListParam.Recursive = true;
  1795. ProcessLocalDirectory(FileName, Terminal->MakeLocalFileList, &MakeFileListParam);
  1796. }
  1797. }
  1798. else
  1799. {
  1800. LocalFileList->Add(FileName);
  1801. }
  1802. }
  1803. TFileOperationProgressType Progress(&OperationProgress, &OperationFinished);
  1804. Progress.Start(foCustomCommand, osRemote, FileListCommand ? 1 : LocalFileList->Count);
  1805. DebugAssert(FProgressForm != NULL);
  1806. FProgressForm->ReadOnly = true;
  1807. try
  1808. {
  1809. if (FileListCommand)
  1810. {
  1811. UnicodeString FileList = MakeFileList(LocalFileList.get());
  1812. TCustomCommandData Data(FTerminal);
  1813. TLocalCustomCommand CustomCommand(
  1814. Data, Terminal->CurrentDirectory,
  1815. L"", L"", FileList);
  1816. ExecuteShellAndWait(CustomCommand.Complete(Command, true));
  1817. }
  1818. else
  1819. {
  1820. for (int Index = 0; Index < LocalFileList->Count; Index++)
  1821. {
  1822. UnicodeString FileName = LocalFileList->Strings[Index];
  1823. TCustomCommandData Data(FTerminal);
  1824. TLocalCustomCommand CustomCommand(
  1825. Data, Terminal->CurrentDirectory,
  1826. FileName, L"", L"");
  1827. ExecuteShellAndWait(CustomCommand.Complete(Command, true));
  1828. }
  1829. }
  1830. }
  1831. __finally
  1832. {
  1833. Progress.Stop();
  1834. }
  1835. }
  1836. }
  1837. }
  1838. //---------------------------------------------------------------------------
  1839. void __fastcall TCustomScpExplorerForm::BothCustomCommand(
  1840. const TCustomCommandType & Command)
  1841. {
  1842. DebugAssert(FCustomCommandLocalFileList != NULL);
  1843. DebugAssert(FCustomCommandRemoteFileList != NULL);
  1844. DebugAssert(FCustomCommandLocalFileList->Count == FCustomCommandRemoteFileList->Count);
  1845. TStrings * LocalFileList = new TStringList();
  1846. TStrings * RemoteFileList = new TStringList();
  1847. try
  1848. {
  1849. for (int Index = 0; Index < FCustomCommandLocalFileList->Count; Index++)
  1850. {
  1851. LocalFileList->Clear();
  1852. LocalFileList->AddObject(
  1853. FCustomCommandLocalFileList->Strings[Index],
  1854. FCustomCommandLocalFileList->Objects[Index]);
  1855. RemoteFileList->Clear();
  1856. RemoteFileList->AddObject(
  1857. FCustomCommandRemoteFileList->Strings[Index],
  1858. FCustomCommandRemoteFileList->Objects[Index]);
  1859. CustomCommand(RemoteFileList, Command, LocalFileList);
  1860. }
  1861. }
  1862. __finally
  1863. {
  1864. delete LocalFileList;
  1865. delete RemoteFileList;
  1866. }
  1867. }
  1868. //---------------------------------------------------------------------------
  1869. void __fastcall TCustomScpExplorerForm::CustomCommandMenu(
  1870. TAction * Action, TStrings * LocalFileList, TStrings * RemoteFileList)
  1871. {
  1872. delete FCustomCommandLocalFileList;
  1873. delete FCustomCommandRemoteFileList;
  1874. // takeover ownership,
  1875. // the lists must survive the MenuPopup as OnClick occurs only after it exits
  1876. FCustomCommandLocalFileList = LocalFileList;
  1877. FCustomCommandRemoteFileList = RemoteFileList;
  1878. TButton * Button = dynamic_cast<TButton *>(Action->ActionComponent);
  1879. if (Button != NULL)
  1880. {
  1881. FCustomCommandMenu->Items->Clear();
  1882. NonVisualDataModule->CreateCustomCommandsMenu(FCustomCommandMenu->Items, false, false, true);
  1883. MenuPopup(FCustomCommandMenu, Button);
  1884. }
  1885. else
  1886. {
  1887. NonVisualDataModule->CreateCustomCommandsMenu(Action, false, true);
  1888. }
  1889. }
  1890. //---------------------------------------------------------------------------
  1891. void __fastcall TCustomScpExplorerForm::TerminalCaptureLog(
  1892. const UnicodeString & AddedLine, TCaptureOutputType OutputType)
  1893. {
  1894. DebugAssert(FCapturedLog != NULL);
  1895. if ((OutputType == cotOutput) || (OutputType == cotError))
  1896. {
  1897. FCapturedLog->Add(AddedLine);
  1898. }
  1899. }
  1900. //---------------------------------------------------------------------------
  1901. bool __fastcall TCustomScpExplorerForm::IsFileControl(TObject * Control,
  1902. TOperationSide Side)
  1903. {
  1904. return (Side == osRemote) &&
  1905. ((Control == RemoteDirView) || (Control == RemoteDriveView));
  1906. }
  1907. //---------------------------------------------------------------------------
  1908. void __fastcall TCustomScpExplorerForm::DirViewContextPopupDefaultItem(
  1909. TOperationSide Side, TTBXCustomItem * Item, TDoubleClickAction DoubleClickAction)
  1910. {
  1911. TTBItemOptions O;
  1912. O = Item->Options;
  1913. TCustomDirView * DView = DirView(Side);
  1914. if ((DView->ItemFocused != NULL) &&
  1915. (WinConfiguration->DoubleClickAction == DoubleClickAction) &&
  1916. // when resolving links is disabled, default action is to enter the directory,
  1917. // no matter what DoubleClickAction is configured to
  1918. ((Side != osRemote) || Terminal->ResolvingSymlinks) &&
  1919. // Can only Edit files, but can Open/Copy even directories
  1920. ((DoubleClickAction != dcaEdit) ||
  1921. !DView->ItemIsDirectory(DView->ItemFocused)))
  1922. {
  1923. Item->Options = O << tboDefault;
  1924. }
  1925. else
  1926. {
  1927. Item->Options = O >> tboDefault;
  1928. }
  1929. }
  1930. //---------------------------------------------------------------------------
  1931. void __fastcall TCustomScpExplorerForm::DirViewContextPopup(
  1932. TOperationSide Side, Byte PopupComponent, const TPoint &MousePos)
  1933. {
  1934. TCustomDirView * DView = DirView(Side);
  1935. TListItem * Item = DView->ItemFocused;
  1936. if ((DView->GetItemAt(MousePos.x, MousePos.y) == Item) &&
  1937. EnableFocusedOperation[Side])
  1938. {
  1939. TPoint ClientPoint;
  1940. ClientPoint = ((MousePos.x < 0) && (MousePos.y < 0)) ?
  1941. TPoint(0, 0) : MousePos;
  1942. FLastContextPopupScreenPoint = DView->ClientToScreen(ClientPoint);
  1943. reinterpret_cast<TPopupMenu*>(GetComponent(PopupComponent))->Popup(
  1944. FLastContextPopupScreenPoint.x, FLastContextPopupScreenPoint.y);
  1945. }
  1946. }
  1947. //---------------------------------------------------------------------------
  1948. void __fastcall TCustomScpExplorerForm::RemoteDirViewContextPopup(
  1949. TObject * /*Sender*/, const TPoint &MousePos, bool &Handled)
  1950. {
  1951. DirViewContextPopupDefaultItem(osRemote, NonVisualDataModule->RemoteOpenMenuItem, dcaOpen);
  1952. DirViewContextPopupDefaultItem(osRemote, NonVisualDataModule->RemoteEditMenuItem, dcaEdit);
  1953. DirViewContextPopupDefaultItem(osRemote, NonVisualDataModule->RemoteCopyMenuItem, dcaCopy);
  1954. DirViewContextPopup(osRemote, fcRemotePopup, MousePos);
  1955. Handled = true;
  1956. }
  1957. //---------------------------------------------------------------------------
  1958. void __fastcall TCustomScpExplorerForm::ReloadLocalDirectory(const UnicodeString Directory)
  1959. {
  1960. }
  1961. //---------------------------------------------------------------------------
  1962. void __fastcall TCustomScpExplorerForm::BatchStart(void *& /*Storage*/)
  1963. {
  1964. DebugAssert(FErrorList == NULL);
  1965. if (WinConfiguration->ContinueOnError)
  1966. {
  1967. FErrorList = new TStringList();
  1968. Configuration->Usage->Inc(L"ContinuationsOnError");
  1969. }
  1970. NonVisualDataModule->StartBusy();
  1971. }
  1972. //---------------------------------------------------------------------------
  1973. void __fastcall TCustomScpExplorerForm::BatchEnd(void * /*Storage*/)
  1974. {
  1975. NonVisualDataModule->EndBusy();
  1976. if (FErrorList)
  1977. {
  1978. HandleErrorList(FErrorList);
  1979. }
  1980. }
  1981. //---------------------------------------------------------------------------
  1982. void __fastcall TCustomScpExplorerForm::UpdateCopyParamCounters(
  1983. const TCopyParamType & CopyParam)
  1984. {
  1985. if (!CopyParam.IncludeFileMask.Masks.IsEmpty())
  1986. {
  1987. Configuration->Usage->Inc(L"FileMaskUses");
  1988. }
  1989. if (IsEffectiveFileNameMask(CopyParam.FileMask))
  1990. {
  1991. Configuration->Usage->Inc(L"OperationMaskUses");
  1992. }
  1993. }
  1994. //---------------------------------------------------------------------------
  1995. bool __fastcall TCustomScpExplorerForm::ExecuteFileOperation(TFileOperation Operation,
  1996. TOperationSide Side, TStrings * FileList, bool NoConfirmation, void * Param)
  1997. {
  1998. void * BatchStorage;
  1999. BatchStart(BatchStorage);
  2000. bool Result;
  2001. try
  2002. {
  2003. if ((Operation == foCopy) || (Operation == foMove))
  2004. {
  2005. TTransferDirection Direction = (Side == osLocal ? tdToRemote : tdToLocal);
  2006. TTransferType Type = (Operation == foCopy ? ttCopy : ttMove);
  2007. UnicodeString TargetDirectory;
  2008. bool Temp = false;
  2009. bool DragDrop = false;
  2010. int Options = 0;
  2011. TAutoSwitch UseQueue = asAuto;
  2012. if (Param != NULL)
  2013. {
  2014. TTransferOperationParam& TParam =
  2015. *static_cast<TTransferOperationParam*>(Param);
  2016. TargetDirectory = TParam.TargetDirectory;
  2017. Temp = TParam.Temp;
  2018. DragDrop = TParam.DragDrop;
  2019. Options = TParam.Options;
  2020. UseQueue = TParam.Queue;
  2021. }
  2022. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  2023. switch (UseQueue)
  2024. {
  2025. case asOn:
  2026. CopyParam.Queue = true;
  2027. break;
  2028. case asOff:
  2029. CopyParam.Queue = false;
  2030. break;
  2031. case asAuto:
  2032. default:
  2033. // keep default
  2034. break;
  2035. }
  2036. Result =
  2037. CopyParamDialog(Direction, Type, Temp, FileList, TargetDirectory,
  2038. CopyParam, !NoConfirmation, DragDrop, Options);
  2039. if (Result)
  2040. {
  2041. DebugAssert(Terminal);
  2042. bool SelectionRestored = false;
  2043. TCustomDirView * DView = NULL;
  2044. if (HasDirView[Side])
  2045. {
  2046. DView = DirView(Side);
  2047. DView->SaveSelection();
  2048. DView->SaveSelectedNames();
  2049. }
  2050. UpdateCopyParamCounters(CopyParam);
  2051. std::unique_ptr<TStringList> TransferResumeList(new TStringList());
  2052. DebugAssert(FTransferResumeList == NULL);
  2053. FTransferResumeList =
  2054. Terminal->IsCapable[fcMoveToQueue] ? TransferResumeList.get() : NULL;
  2055. FMoveToQueue = false;
  2056. int Params = FLAGMASK(Operation == foMove, cpDelete);
  2057. try
  2058. {
  2059. try
  2060. {
  2061. if (Side == osLocal)
  2062. {
  2063. Params |= FLAGMASK(Temp, cpTemporary);
  2064. Terminal->CopyToRemote(FileList, TargetDirectory, &CopyParam, Params);
  2065. if (Operation == foMove)
  2066. {
  2067. ReloadLocalDirectory();
  2068. if (DView != NULL)
  2069. {
  2070. DView->RestoreSelection();
  2071. }
  2072. SelectionRestored = true;
  2073. }
  2074. }
  2075. else
  2076. {
  2077. try
  2078. {
  2079. Terminal->CopyToLocal(FileList, TargetDirectory, &CopyParam,
  2080. Params);
  2081. }
  2082. __finally
  2083. {
  2084. if (Operation == foMove)
  2085. {
  2086. if (DView != NULL)
  2087. {
  2088. DView->RestoreSelection();
  2089. }
  2090. SelectionRestored = true;
  2091. }
  2092. ReloadLocalDirectory(TargetDirectory);
  2093. }
  2094. }
  2095. }
  2096. catch (EAbort &)
  2097. {
  2098. if (FMoveToQueue)
  2099. {
  2100. Params |=
  2101. (CopyParam.QueueNoConfirmation ? cpNoConfirmation : 0);
  2102. DebugAssert(CopyParam.TransferSkipList == NULL);
  2103. DebugAssert(CopyParam.TransferResumeFile.IsEmpty());
  2104. if (TransferResumeList->Count > 0)
  2105. {
  2106. CopyParam.TransferResumeFile = TransferResumeList->Strings[TransferResumeList->Count - 1];
  2107. TransferResumeList->Delete(TransferResumeList->Count - 1);
  2108. }
  2109. CopyParam.TransferSkipList = TransferResumeList.release();
  2110. // not really needed, just to keep it consistent with TransferResumeList
  2111. FTransferResumeList = NULL;
  2112. FMoveToQueue = false;
  2113. Configuration->Usage->Inc("MovesToBackground");
  2114. AddQueueItem(Queue, Direction, FileList, TargetDirectory, CopyParam, Params);
  2115. ClearTransferSourceSelection(Direction);
  2116. }
  2117. throw;
  2118. }
  2119. }
  2120. __finally
  2121. {
  2122. if (!SelectionRestored && (DView != NULL))
  2123. {
  2124. DView->DiscardSavedSelection();
  2125. }
  2126. FTransferResumeList = NULL;
  2127. }
  2128. }
  2129. }
  2130. else if (Operation == foRename)
  2131. {
  2132. DebugAssert(DirView(Side)->ItemFocused);
  2133. DirView(Side)->ItemFocused->EditCaption();
  2134. Result = true;
  2135. }
  2136. else if (Operation == foDelete)
  2137. {
  2138. DebugAssert(FileList->Count);
  2139. // We deliberately do not toggle alternative flag (Param), but use OR,
  2140. // because the Param is set only when command is invoked using Shift-Del/F8 keyboard
  2141. // shortcut of CurrentDeleteAlternativeAction
  2142. bool Alternative =
  2143. bool(Param) || UseAlternativeFunction();
  2144. bool Recycle;
  2145. if (Side == osLocal)
  2146. {
  2147. Recycle = (WinConfiguration->DeleteToRecycleBin != Alternative);
  2148. }
  2149. else
  2150. {
  2151. Recycle = (Terminal->SessionData->DeleteToRecycleBin != Alternative) &&
  2152. !Terminal->SessionData->RecycleBinPath.IsEmpty() &&
  2153. !Terminal->IsRecycledFile(FileList->Strings[0]);
  2154. }
  2155. Result =
  2156. !(Recycle ? WinConfiguration->ConfirmRecycling : WinConfiguration->ConfirmDeleting);
  2157. if (!Result)
  2158. {
  2159. UnicodeString Query;
  2160. if (FileList->Count == 1)
  2161. {
  2162. if (Side == osLocal)
  2163. {
  2164. Query = ExtractFileName(FileList->Strings[0]);
  2165. }
  2166. else
  2167. {
  2168. Query = UnixExtractFileName(FileList->Strings[0]);
  2169. }
  2170. Query = FMTLOAD(
  2171. (Recycle ? CONFIRM_RECYCLE_FILE : CONFIRM_DELETE_FILE), (Query));
  2172. }
  2173. else
  2174. {
  2175. Query = FMTLOAD(
  2176. (Recycle ? CONFIRM_RECYCLE_FILES : CONFIRM_DELETE_FILES), (FileList->Count));
  2177. }
  2178. TMessageParams Params(mpNeverAskAgainCheck);
  2179. Params.ImageName = L"Delete file";
  2180. unsigned int Answer = MessageDialog(MainInstructions(Query), qtConfirmation,
  2181. qaOK | qaCancel, HELP_DELETE_FILE, &Params);
  2182. if (Answer == qaNeverAskAgain)
  2183. {
  2184. Result = true;
  2185. if (Recycle)
  2186. {
  2187. WinConfiguration->ConfirmRecycling = false;
  2188. }
  2189. else
  2190. {
  2191. WinConfiguration->ConfirmDeleting = false;
  2192. }
  2193. }
  2194. else
  2195. {
  2196. Result = (Answer == qaOK);
  2197. }
  2198. }
  2199. if (Result)
  2200. {
  2201. DeleteFiles(Side, FileList, FLAGMASK(Alternative, dfAlternative));
  2202. }
  2203. }
  2204. else if (Operation == foSetProperties)
  2205. {
  2206. RemoteDirView->SaveSelectedNames();
  2207. Result = SetProperties(Side, FileList);
  2208. }
  2209. else if (Operation == foCustomCommand)
  2210. {
  2211. DebugAssert(Param);
  2212. DebugAssert(Side == osRemote);
  2213. RemoteDirView->SaveSelectedNames();
  2214. const TCustomCommandType * Command = static_cast<const TCustomCommandType*>(Param);
  2215. CustomCommand(FileList, *Command, NULL);
  2216. Result = true;
  2217. }
  2218. else if ((Operation == foRemoteMove) || (Operation == foRemoteCopy))
  2219. {
  2220. DebugAssert(Side == osRemote);
  2221. Result = RemoteTransferFiles(FileList, NoConfirmation,
  2222. (Operation == foRemoteMove), reinterpret_cast<TTerminal *>(Param));
  2223. }
  2224. else if (Operation == foLock)
  2225. {
  2226. DebugAssert(Side == osRemote);
  2227. LockFiles(FileList, true);
  2228. Result = true;
  2229. }
  2230. else if (Operation == foUnlock)
  2231. {
  2232. DebugAssert(Side == osRemote);
  2233. LockFiles(FileList, false);
  2234. Result = true;
  2235. }
  2236. else
  2237. {
  2238. DebugFail();
  2239. }
  2240. }
  2241. __finally
  2242. {
  2243. BatchEnd(BatchStorage);
  2244. }
  2245. return Result;
  2246. }
  2247. //---------------------------------------------------------------------------
  2248. TOperationSide __fastcall TCustomScpExplorerForm::GetSide(TOperationSide Side)
  2249. {
  2250. if (Side == osCurrent)
  2251. {
  2252. Side = FCurrentSide;
  2253. }
  2254. return Side;
  2255. }
  2256. //---------------------------------------------------------------------------
  2257. bool __fastcall TCustomScpExplorerForm::ExecuteFileOperation(TFileOperation Operation,
  2258. TOperationSide Side, bool OnFocused, bool NoConfirmation, void * Param)
  2259. {
  2260. Side = GetSide(Side);
  2261. bool Result;
  2262. TStrings * FileList = DirView(Side)->CreateFileList(OnFocused, (Side == osLocal), NULL);
  2263. try
  2264. {
  2265. Result = ExecuteFileOperation(Operation, Side, FileList, NoConfirmation, Param);
  2266. }
  2267. __finally
  2268. {
  2269. delete FileList;
  2270. }
  2271. return Result;
  2272. }
  2273. //---------------------------------------------------------------------------
  2274. void __fastcall TCustomScpExplorerForm::ExecuteFileOperationCommand(
  2275. TFileOperation Operation, TOperationSide Side, bool OnFocused,
  2276. bool NoConfirmation, void * Param)
  2277. {
  2278. if (ExecuteFileOperation(Operation, Side, OnFocused, NoConfirmation, Param))
  2279. {
  2280. if ((Operation == foCopy) || (Operation == foMove))
  2281. {
  2282. if (GetSide(Side) == osLocal)
  2283. {
  2284. Configuration->Usage->Inc(L"UploadsCommand");
  2285. }
  2286. else
  2287. {
  2288. Configuration->Usage->Inc(L"DownloadsCommand");
  2289. }
  2290. }
  2291. }
  2292. }
  2293. //---------------------------------------------------------------------------
  2294. void __fastcall TCustomScpExplorerForm::ExecuteCopyOperationCommand(
  2295. TOperationSide Side, bool OnFocused, unsigned int Flags)
  2296. {
  2297. TTransferOperationParam Param;
  2298. if ((WinConfiguration->Interface != ifCommander) ||
  2299. WinConfiguration->ScpCommander.ExplorerKeyboardShortcuts)
  2300. {
  2301. Flags &= ~cocShortCutHint;
  2302. }
  2303. TCustomDirView * DView = DirView(Side);
  2304. Param.Options =
  2305. FLAGMASK(FLAGSET(Flags, cocShortCutHint), coShortCutHint) |
  2306. FLAGMASK(SelectedAllFilesInDirView(DView), coAllFiles);
  2307. if (FLAGSET(Flags, cocQueue))
  2308. {
  2309. Param.Queue = asOn;
  2310. }
  2311. else if (FLAGSET(Flags, cocNonQueue))
  2312. {
  2313. Param.Queue = asOff;
  2314. }
  2315. ExecuteFileOperationCommand(foCopy, Side, OnFocused, false, &Param);
  2316. }
  2317. //---------------------------------------------------------------------------
  2318. void __fastcall TCustomScpExplorerForm::HandleErrorList(TStringList *& ErrorList)
  2319. {
  2320. try
  2321. {
  2322. if (ErrorList->Count)
  2323. {
  2324. UnicodeString Message = MainInstructions(FMTLOAD(ERROR_LIST_COUNT, (ErrorList->Count)));
  2325. if (MessageDialog(Message, qtError,
  2326. qaOK | qaCancel, HELP_NONE) == qaOK)
  2327. {
  2328. unsigned int Answer;
  2329. int Index = 0;
  2330. do
  2331. {
  2332. DebugAssert(Index >= 0 && Index < ErrorList->Count);
  2333. TQueryButtonAlias Aliases[2];
  2334. Aliases[0].Button = qaYes;
  2335. Aliases[0].Alias = LoadStr(PREV_BUTTON);
  2336. Aliases[1].Button = qaNo;
  2337. Aliases[1].Alias = LoadStr(NEXT_BUTTON);
  2338. TMessageParams Params;
  2339. Params.Aliases = Aliases;
  2340. Params.AliasesCount = LENOF(Aliases);
  2341. Answer = MoreMessageDialog(
  2342. FMTLOAD(ERROR_LIST_NUMBER, (Index+1, ErrorList->Count, ErrorList->Strings[Index])),
  2343. dynamic_cast<TStrings *>(ErrorList->Objects[Index]), qtError,
  2344. (Index ? qaYes : 0) | (Index < ErrorList->Count - 1 ? qaNo : 0) |
  2345. qaOK, HELP_NONE, &Params);
  2346. if (Answer == qaNo)
  2347. {
  2348. Index++;
  2349. }
  2350. if (Answer == qaYes)
  2351. {
  2352. Index--;
  2353. }
  2354. }
  2355. while (Answer != qaOK);
  2356. }
  2357. }
  2358. }
  2359. __finally
  2360. {
  2361. TStrings * List = ErrorList;
  2362. ErrorList = NULL;
  2363. for (int i = 0; i < List->Count; i++)
  2364. {
  2365. delete List->Objects[i];
  2366. }
  2367. delete List;
  2368. }
  2369. }
  2370. //---------------------------------------------------------------------------
  2371. void __fastcall TCustomScpExplorerForm::ExecuteRemoteFile(
  2372. const UnicodeString & FullFileName, TRemoteFile * File, TExecuteFileBy ExecuteFileBy)
  2373. {
  2374. // needed for checking filemasks, as there's no directory object
  2375. // associated with the file object
  2376. File->FullFileName = FullFileName;
  2377. TFileMasks::TParams MaskParams;
  2378. MaskParams.Size = File->Size;
  2379. MaskParams.Modification = File->Modification;
  2380. ExecuteFile(osRemote, ExecuteFileBy, NULL, FullFileName, File, MaskParams);
  2381. }
  2382. //---------------------------------------------------------------------------
  2383. void __fastcall TCustomScpExplorerForm::EditNew(TOperationSide Side)
  2384. {
  2385. DebugAssert(!WinConfiguration->DisableOpenEdit);
  2386. Side = GetSide(Side);
  2387. TCustomDirView * CurrentDirView = DirView(osCurrent);
  2388. TListItem * FocusedItem = CurrentDirView->ItemFocused;
  2389. UnicodeString Name;
  2390. if ((FocusedItem != NULL) && !CurrentDirView->ItemIsParentDirectory(FocusedItem))
  2391. {
  2392. Name = CurrentDirView->ItemFileName(FocusedItem);
  2393. }
  2394. else
  2395. {
  2396. Name = LoadStr(NEW_FILE);
  2397. }
  2398. std::unique_ptr<TStrings> History(CloneStrings(CustomWinConfiguration->History[L"EditFile"]));
  2399. if (InputDialog(LoadStr(EDIT_FILE_CAPTION), LoadStr(EDIT_FILE_PROMPT), Name,
  2400. HELP_EDIT_NEW, History.get(), true))
  2401. {
  2402. CustomWinConfiguration->History[L"EditFile"] = History.get();
  2403. UnicodeString TargetFileName;
  2404. UnicodeString LocalFileName;
  2405. UnicodeString RootTempDir;
  2406. UnicodeString TempDir;
  2407. UnicodeString RemoteDirectory;
  2408. if (Side == osRemote)
  2409. {
  2410. Name = AbsolutePath(FTerminal->CurrentDirectory, Name);
  2411. TRemoteFile * File = NULL;
  2412. if (FTerminal->FileExists(Name, &File))
  2413. {
  2414. try
  2415. {
  2416. ExecuteRemoteFile(Name, File, efDefaultEditor);
  2417. return;
  2418. }
  2419. __finally
  2420. {
  2421. delete File;
  2422. }
  2423. }
  2424. RemoteDirectory = UnixExtractFilePath(Name);
  2425. TemporaryDirectoryForRemoteFiles(
  2426. RemoteDirectory, GUIConfiguration->CurrentCopyParam, TempDir, RootTempDir);
  2427. TargetFileName = UnixExtractFileName(Name);
  2428. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  2429. LocalFileName = TempDir +
  2430. // We probably do not want to trim the VMS version here
  2431. FTerminal->ChangeFileName(&CopyParam, TargetFileName, osRemote, false);
  2432. }
  2433. else
  2434. {
  2435. if (ExtractFilePath(Name).IsEmpty())
  2436. {
  2437. LocalFileName = IncludeTrailingBackslash(DirView(Side)->PathName) + Name;
  2438. }
  2439. else
  2440. {
  2441. LocalFileName = ExpandFileName(Name);
  2442. }
  2443. TargetFileName = ExtractFileName(Name);
  2444. }
  2445. if (!FileExists(ApiPath(LocalFileName)))
  2446. {
  2447. int File = FileCreate(ApiPath(LocalFileName));
  2448. if (File < 0)
  2449. {
  2450. if (!RootTempDir.IsEmpty())
  2451. {
  2452. RecursiveDeleteFile(ExcludeTrailingBackslash(RootTempDir), false);
  2453. }
  2454. throw Exception(FMTLOAD(CREATE_FILE_ERROR, (LocalFileName)));
  2455. }
  2456. else
  2457. {
  2458. FileClose(File);
  2459. }
  2460. }
  2461. TExecuteFileBy ExecuteFileBy = efDefaultEditor;
  2462. const TEditorData * ExternalEditor = NULL;
  2463. TFileMasks::TParams MaskParams; // size not known
  2464. ExecuteFileNormalize(ExecuteFileBy, ExternalEditor, TargetFileName,
  2465. false, MaskParams);
  2466. CustomExecuteFile(Side, ExecuteFileBy, LocalFileName, TargetFileName,
  2467. ExternalEditor, RootTempDir, RemoteDirectory);
  2468. }
  2469. }
  2470. //---------------------------------------------------------------------------
  2471. bool __fastcall TCustomScpExplorerForm::RemoteExecuteForceText(
  2472. TExecuteFileBy ExecuteFileBy, const TEditorData * ExternalEditor)
  2473. {
  2474. DebugAssert((ExecuteFileBy == efExternalEditor) ==
  2475. ((ExternalEditor != NULL) && (ExternalEditor->Editor == edExternal)));
  2476. DebugAssert(ExecuteFileBy != efDefaultEditor);
  2477. return
  2478. ((ExecuteFileBy == efInternalEditor)) ||
  2479. ((ExecuteFileBy == efExternalEditor) && ExternalEditor->ExternalEditorText);
  2480. }
  2481. //---------------------------------------------------------------------------
  2482. void __fastcall TCustomScpExplorerForm::CustomExecuteFile(TOperationSide Side,
  2483. TExecuteFileBy ExecuteFileBy, UnicodeString FileName, UnicodeString OriginalFileName,
  2484. const TEditorData * ExternalEditor, UnicodeString LocalRootDirectory,
  2485. UnicodeString RemoteDirectory)
  2486. {
  2487. DebugAssert(!WinConfiguration->DisableOpenEdit);
  2488. DebugAssert((ExecuteFileBy == efExternalEditor) ==
  2489. ((ExternalEditor != NULL) && (ExternalEditor->Editor == edExternal)));
  2490. DebugAssert(ExecuteFileBy != efDefaultEditor);
  2491. Side = GetSide(Side);
  2492. std::unique_ptr<TEditedFileData> Data(new TEditedFileData);
  2493. if (Side == osRemote)
  2494. {
  2495. Data->Terminal = Terminal;
  2496. Data->Queue = Queue;
  2497. Data->SessionData = CloneCurrentSessionData();
  2498. Data->ForceText = RemoteExecuteForceText(ExecuteFileBy, ExternalEditor);
  2499. Data->RemoteDirectory = RemoteDirectory;
  2500. Data->SessionName = Terminal->SessionData->SessionName;
  2501. Data->LocalRootDirectory = LocalRootDirectory;
  2502. Data->OriginalFileName = OriginalFileName;
  2503. Data->Command = L""; // will be changed later for external editor
  2504. }
  2505. if (ExecuteFileBy == efInternalEditor)
  2506. {
  2507. if (Side == osRemote)
  2508. {
  2509. UnicodeString Caption = UnixIncludeTrailingBackslash(RemoteDirectory) + OriginalFileName +
  2510. L" - " + Terminal->SessionData->SessionName;
  2511. TForm * Editor;
  2512. try
  2513. {
  2514. Editor = ShowEditorForm(FileName, this, FEditorManager->FileChanged,
  2515. FEditorManager->FileReload, FEditorManager->FileClosed,
  2516. SaveAllInternalEditors, AnyInternalEditorModified,
  2517. Caption, FStandaloneEditing, SessionColor);
  2518. }
  2519. catch(...)
  2520. {
  2521. if (!LocalRootDirectory.IsEmpty())
  2522. {
  2523. RecursiveDeleteFile(ExcludeTrailingBackslash(LocalRootDirectory), false);
  2524. }
  2525. throw;
  2526. }
  2527. FEditorManager->AddFileInternal(FileName, Data.release(), Editor);
  2528. }
  2529. else
  2530. {
  2531. DebugAssert(!FStandaloneEditing);
  2532. TForm * Editor =
  2533. ShowEditorForm(FileName, this, NULL, NULL, LocalEditorClosed,
  2534. SaveAllInternalEditors, AnyInternalEditorModified,
  2535. L"", false, SessionColor);
  2536. FLocalEditors->Add(Editor);
  2537. }
  2538. }
  2539. else
  2540. {
  2541. HANDLE Process;
  2542. if (ExecuteFileBy == efExternalEditor)
  2543. {
  2544. UnicodeString Program, Params, Dir;
  2545. Data->Command = ExternalEditor->ExternalEditor;
  2546. ReformatFileNameCommand(Data->Command);
  2547. SplitCommand(Data->Command, Program, Params, Dir);
  2548. Params = ExpandFileNameCommand(Params, FileName);
  2549. Program = ExpandEnvironmentVariables(Program);
  2550. if (!ExecuteShell(Program, Params, Process))
  2551. {
  2552. throw Exception(FMTLOAD(EDITOR_ERROR, (Program)));
  2553. }
  2554. }
  2555. else
  2556. {
  2557. DebugAssert(Side == osRemote);
  2558. if (!ExecuteShell(FileName, L"", Process))
  2559. {
  2560. throw Exception(FMTLOAD(EXECUTE_FILE_ERROR, (FileName)));
  2561. }
  2562. }
  2563. if ((Side == osLocal) ||
  2564. ((ExecuteFileBy == efShell) &&
  2565. !WinConfiguration->Editor.SDIShellEditor) ||
  2566. ((ExecuteFileBy == efExternalEditor) &&
  2567. !ExternalEditor->SDIExternalEditor))
  2568. {
  2569. // no need for handle
  2570. if (Process != NULL)
  2571. {
  2572. DebugCheck(CloseHandle(Process));
  2573. }
  2574. Process = INVALID_HANDLE_VALUE;
  2575. }
  2576. else
  2577. {
  2578. if (Process == NULL)
  2579. {
  2580. throw ExtException(LoadStr(OPEN_FILE_NO_PROCESS2), L"", HELP_OPEN_FILE_NO_PROCESS);
  2581. }
  2582. }
  2583. if (Side == osRemote)
  2584. {
  2585. FEditorManager->AddFileExternal(FileName, Data.release(), Process);
  2586. }
  2587. }
  2588. }
  2589. //---------------------------------------------------------------------------
  2590. void __fastcall TCustomScpExplorerForm::SaveInternalEditor(
  2591. const UnicodeString /*FileName*/, TEditedFileData * /*Data*/, TObject * Token,
  2592. void * /*Arg*/)
  2593. {
  2594. if (Token != NULL)
  2595. {
  2596. EditorFormFileSave(static_cast<TForm *>(Token));
  2597. }
  2598. }
  2599. //---------------------------------------------------------------------------
  2600. void __fastcall TCustomScpExplorerForm::SaveAllInternalEditors(TObject * /*Sender*/)
  2601. {
  2602. for (int Index = 0; Index < FLocalEditors->Count; Index++)
  2603. {
  2604. EditorFormFileSave(static_cast<TForm *>(FLocalEditors->Items[Index]));
  2605. }
  2606. FEditorManager->ProcessFiles(SaveInternalEditor, NULL);
  2607. }
  2608. //---------------------------------------------------------------------------
  2609. void __fastcall TCustomScpExplorerForm::InternalEditorModified(
  2610. const UnicodeString /*FileName*/, TEditedFileData * /*Data*/, TObject * Token,
  2611. void * Arg)
  2612. {
  2613. if ((Token != NULL) &&
  2614. IsEditorFormModified(static_cast<TForm *>(Token)))
  2615. {
  2616. *static_cast<bool *>(Arg) = true;
  2617. }
  2618. }
  2619. //---------------------------------------------------------------------------
  2620. void __fastcall TCustomScpExplorerForm::AnyInternalEditorModified(
  2621. TObject * /*Sender*/, bool & Modified)
  2622. {
  2623. for (int Index = 0; !Modified && (Index < FLocalEditors->Count); Index++)
  2624. {
  2625. if (IsEditorFormModified(static_cast<TForm *>(FLocalEditors->Items[Index])))
  2626. {
  2627. Modified = true;
  2628. }
  2629. }
  2630. if (!Modified)
  2631. {
  2632. FEditorManager->ProcessFiles(InternalEditorModified, &Modified);
  2633. }
  2634. }
  2635. //---------------------------------------------------------------------------
  2636. void __fastcall TCustomScpExplorerForm::LocalEditorClosed(TObject * Sender, bool /*Forced*/)
  2637. {
  2638. DebugCheck(FLocalEditors->Extract(Sender) >= 0);
  2639. }
  2640. //---------------------------------------------------------------------------
  2641. void __fastcall TCustomScpExplorerForm::TemporaryDirectoryForRemoteFiles(
  2642. UnicodeString RemoteDirectory, TCopyParamType CopyParam,
  2643. UnicodeString & Result, UnicodeString & RootDirectory)
  2644. {
  2645. if (!WinConfiguration->TemporaryDirectoryDeterministic)
  2646. {
  2647. RootDirectory = IncludeTrailingBackslash(WinConfiguration->TemporaryDir());
  2648. Result = RootDirectory;
  2649. }
  2650. else
  2651. {
  2652. RootDirectory = L"";
  2653. Result = WinConfiguration->ExpandedTemporaryDirectory();
  2654. Result = IncludeTrailingBackslash(Result);
  2655. }
  2656. if (WinConfiguration->TemporaryDirectoryAppendSession)
  2657. {
  2658. Result = IncludeTrailingBackslash(Result + CopyParam.ValidLocalPath(Terminal->SessionData->SessionName));
  2659. }
  2660. if (WinConfiguration->TemporaryDirectoryAppendPath)
  2661. {
  2662. if (!RemoteDirectory.IsEmpty() && (RemoteDirectory[1] == L'/'))
  2663. {
  2664. RemoteDirectory.Delete(1, 1);
  2665. }
  2666. Result = IncludeTrailingBackslash(Result + CopyParam.ValidLocalPath(FromUnixPath(RemoteDirectory)));
  2667. }
  2668. if (!ForceDirectories(ApiPath(Result)))
  2669. {
  2670. throw EOSExtException(FMTLOAD(CREATE_TEMP_DIR_ERROR, (Result)));
  2671. }
  2672. }
  2673. //---------------------------------------------------------------------------
  2674. void __fastcall TCustomScpExplorerForm::TemporarilyDownloadFiles(
  2675. TStrings * FileList, bool ForceText, UnicodeString & RootTempDir, UnicodeString & TempDir,
  2676. bool AllFiles, bool GetTargetNames, bool AutoOperation)
  2677. {
  2678. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  2679. if (ForceText)
  2680. {
  2681. CopyParam.TransferMode = tmAscii;
  2682. }
  2683. TemporaryFileCopyParam(CopyParam);
  2684. if (AllFiles)
  2685. {
  2686. CopyParam.IncludeFileMask = TFileMasks();
  2687. }
  2688. if (TempDir.IsEmpty())
  2689. {
  2690. TemporaryDirectoryForRemoteFiles(FTerminal->CurrentDirectory, CopyParam, TempDir, RootTempDir);
  2691. }
  2692. DebugAssert(!FAutoOperation);
  2693. FAutoOperation = AutoOperation;
  2694. Terminal->ExceptionOnFail = true;
  2695. try
  2696. {
  2697. try
  2698. {
  2699. // turn off confirmations, as for MDI editors we may possibly download
  2700. // the same file over
  2701. Terminal->CopyToLocal(FileList, TempDir, &CopyParam,
  2702. cpNoConfirmation | cpTemporary);
  2703. if (GetTargetNames)
  2704. {
  2705. for (int i = 0; i < FileList->Count; i++)
  2706. {
  2707. FileList->Strings[i] =
  2708. Terminal->ChangeFileName(&CopyParam, UnixExtractFileName(FileList->Strings[i]), osRemote, false);
  2709. }
  2710. }
  2711. }
  2712. catch(...)
  2713. {
  2714. if (!RootTempDir.IsEmpty())
  2715. {
  2716. RecursiveDeleteFile(ExcludeTrailingBackslash(RootTempDir), false);
  2717. }
  2718. throw;
  2719. }
  2720. }
  2721. __finally
  2722. {
  2723. FAutoOperation = false;
  2724. Terminal->ExceptionOnFail = false;
  2725. }
  2726. }
  2727. //---------------------------------------------------------------------------
  2728. void __fastcall TCustomScpExplorerForm::EditorAutoConfig()
  2729. {
  2730. // Do not waste time checking for default editor list the next time,
  2731. // or testing if default editor is not notepad.
  2732. bool TryNextTime = false;
  2733. UnicodeString UsageState;
  2734. if (!WinConfiguration->EditorList->IsDefaultList())
  2735. {
  2736. UsageState = "H";
  2737. }
  2738. else
  2739. {
  2740. UnicodeString Executable;
  2741. UnicodeString ExecutableDescription;
  2742. if (DetectSystemExternalEditor(false, Executable, ExecutableDescription, UsageState, TryNextTime))
  2743. {
  2744. UnicodeString Message =
  2745. FMTLOAD(EDITOR_AUTO_CONFIG2, (ExecutableDescription, ExecutableDescription));
  2746. unsigned int Answer =
  2747. MessageDialog(Message, qtConfirmation, qaOK | qaCancel, HELP_EDITOR_AUTO_CONFIG);
  2748. if (Answer != qaOK)
  2749. {
  2750. UsageState = "R";
  2751. }
  2752. else
  2753. {
  2754. UsageState = "A";
  2755. TEditorData EditorData;
  2756. EditorData.Editor = edExternal;
  2757. EditorData.ExternalEditor = FormatCommand(Executable, L"");
  2758. EditorData.DecideExternalEditorText();
  2759. TEditorList EditorList;
  2760. EditorList = *WinConfiguration->EditorList;
  2761. EditorList.Insert(0, new TEditorPreferences(EditorData));
  2762. WinConfiguration->EditorList = &EditorList;
  2763. }
  2764. }
  2765. }
  2766. WinConfiguration->OfferedEditorAutoConfig = !TryNextTime;
  2767. WinConfiguration->Usage->Set(L"EditorAutoConfig", UsageState);
  2768. }
  2769. //---------------------------------------------------------------------------
  2770. void __fastcall TCustomScpExplorerForm::ExecuteFileNormalize(
  2771. TExecuteFileBy & ExecuteFileBy, const TEditorData *& ExternalEditor,
  2772. const UnicodeString & FileName, bool Local, const TFileMasks::TParams & MaskParams)
  2773. {
  2774. if (ExecuteFileBy == efDefaultEditor)
  2775. {
  2776. if (!WinConfiguration->OfferedEditorAutoConfig)
  2777. {
  2778. EditorAutoConfig();
  2779. }
  2780. const TEditorPreferences * Editor =
  2781. WinConfiguration->DefaultEditorForFile(FileName, Local, MaskParams);
  2782. if ((Editor == NULL) || (Editor->Data->Editor == edInternal))
  2783. {
  2784. ExecuteFileBy = efInternalEditor;
  2785. ExternalEditor = NULL;
  2786. }
  2787. else if (Editor->Data->Editor == edOpen)
  2788. {
  2789. ExecuteFileBy = efShell;
  2790. ExternalEditor = NULL;
  2791. }
  2792. else
  2793. {
  2794. ExecuteFileBy = efExternalEditor;
  2795. ExternalEditor = Editor->Data;
  2796. }
  2797. }
  2798. }
  2799. //---------------------------------------------------------------------------
  2800. void __fastcall TCustomScpExplorerForm::ExecuteFile(TOperationSide Side,
  2801. TExecuteFileBy ExecuteFileBy, const TEditorData * ExternalEditor,
  2802. UnicodeString FullFileName, TObject * Object, const TFileMasks::TParams & MaskParams)
  2803. {
  2804. UnicodeString OriginalFileName;
  2805. UnicodeString LocalRootDirectory;
  2806. UnicodeString RemoteDirectory;
  2807. ExecuteFileNormalize(ExecuteFileBy, ExternalEditor, FullFileName,
  2808. (Side == osLocal), MaskParams);
  2809. UnicodeString Counter;
  2810. UnicodeString LocalFileName;
  2811. if (Side == osRemote)
  2812. {
  2813. // We need to trim VMS version here, so that we use name without version
  2814. // when uploading back to create a new version of the file
  2815. OriginalFileName = FTerminal->GetBaseFileName(UnixExtractFileName(FullFileName));
  2816. RemoteDirectory = UnixExtractFilePath(FullFileName);
  2817. TObject * Token = NULL;
  2818. UnicodeString LocalDirectory;
  2819. if (!FEditorManager->CanAddFile(RemoteDirectory, OriginalFileName,
  2820. Terminal->SessionData->SessionName, Token, LocalRootDirectory,
  2821. LocalDirectory))
  2822. {
  2823. if (Token != NULL)
  2824. {
  2825. TForm * Form = dynamic_cast<TForm *>(Token);
  2826. if (Form->WindowState == wsMinimized)
  2827. {
  2828. ShowWindow(Form->Handle, SW_RESTORE);
  2829. }
  2830. else
  2831. {
  2832. Form->SetFocus();
  2833. }
  2834. Abort();
  2835. }
  2836. else
  2837. {
  2838. throw Exception(FMTLOAD(ALREADY_EDITED_EXTERNALLY_OR_UPLOADED, (OriginalFileName)));
  2839. }
  2840. }
  2841. TStringList * FileList1 = new TStringList();
  2842. try
  2843. {
  2844. FileList1->AddObject(FullFileName, Object);
  2845. TemporarilyDownloadFiles(FileList1,
  2846. RemoteExecuteForceText(ExecuteFileBy, ExternalEditor),
  2847. LocalRootDirectory, LocalDirectory, true, true, true);
  2848. LocalFileName = LocalDirectory + FileList1->Strings[0];
  2849. }
  2850. __finally
  2851. {
  2852. delete FileList1;
  2853. }
  2854. switch (ExecuteFileBy)
  2855. {
  2856. case efShell:
  2857. Counter = "RemoteFilesExecuted";
  2858. break;
  2859. case efInternalEditor:
  2860. Counter = "RemoteFilesOpenedInInternalEditor";
  2861. break;
  2862. case efExternalEditor:
  2863. Counter = "RemoteFilesOpenedInExternalEditor";
  2864. break;
  2865. default:
  2866. DebugFail();
  2867. }
  2868. }
  2869. else
  2870. {
  2871. LocalFileName = FullFileName;
  2872. OriginalFileName = ExtractFileName(FullFileName);
  2873. switch (ExecuteFileBy)
  2874. {
  2875. case efShell:
  2876. Counter = "LocalFilesExecuted";
  2877. break;
  2878. case efInternalEditor:
  2879. Counter = "LocalFilesOpenedInInternalEditor";
  2880. break;
  2881. case efExternalEditor:
  2882. Counter = "LocalFilesOpenedInExternalEditor";
  2883. break;
  2884. default:
  2885. DebugFail();
  2886. }
  2887. }
  2888. Configuration->Usage->Inc(Counter);
  2889. CustomExecuteFile(Side, ExecuteFileBy, LocalFileName, OriginalFileName,
  2890. ExternalEditor, LocalRootDirectory, RemoteDirectory);
  2891. }
  2892. //---------------------------------------------------------------------------
  2893. void __fastcall TCustomScpExplorerForm::ExecuteFile(TOperationSide Side,
  2894. TExecuteFileBy ExecuteFileBy, const TEditorData * ExternalEditor,
  2895. bool AllSelected, bool OnFocused)
  2896. {
  2897. DebugAssert(!WinConfiguration->DisableOpenEdit);
  2898. DebugAssert((ExecuteFileBy == efExternalEditor) ==
  2899. ((ExternalEditor != NULL) && (ExternalEditor->Editor == edExternal)));
  2900. Side = GetSide(Side);
  2901. TCustomDirView * DView = DirView(Side);
  2902. TStrings * FileList = AllSelected ?
  2903. DView->CreateFileList(OnFocused, Side == osLocal) :
  2904. DView->CreateFocusedFileList(Side == osLocal);
  2905. try
  2906. {
  2907. DebugAssert(AllSelected || (FileList->Count == 1));
  2908. for (int i = 0; i < FileList->Count; i++)
  2909. {
  2910. UnicodeString ListFileName = FileList->Strings[i];
  2911. UnicodeString FileNameOnly = (Side == osRemote) ?
  2912. UnixExtractFileName(ListFileName) : ExtractFileName(ListFileName);
  2913. TListItem * Item = DView->FindFileItem(FileNameOnly);
  2914. if (!DView->ItemIsDirectory(Item))
  2915. {
  2916. UnicodeString FullFileName;
  2917. if (Side == osRemote)
  2918. {
  2919. FullFileName = RemoteDirView->Path + ListFileName;
  2920. }
  2921. else
  2922. {
  2923. FullFileName = ListFileName;
  2924. }
  2925. TFileMasks::TParams MaskParams;
  2926. MaskParams.Size = DView->ItemFileSize(Item);
  2927. TDateTimePrecision Precision;
  2928. MaskParams.Modification = DView->ItemFileTime(Item, Precision);
  2929. ExecuteFile(Side, ExecuteFileBy, ExternalEditor, FullFileName,
  2930. FileList->Objects[i], MaskParams);
  2931. }
  2932. }
  2933. }
  2934. __finally
  2935. {
  2936. delete FileList;
  2937. }
  2938. }
  2939. //---------------------------------------------------------------------------
  2940. void __fastcall TCustomScpExplorerForm::TemporaryFileCopyParam(TCopyParamType & CopyParam)
  2941. {
  2942. // do not forget to add additional options to TemporarilyDownloadFiles, and AS
  2943. CopyParam.FileNameCase = ncNoChange;
  2944. CopyParam.PreserveRights = false;
  2945. CopyParam.PreserveReadOnly = false;
  2946. CopyParam.ReplaceInvalidChars = true;
  2947. CopyParam.IncludeFileMask = TFileMasks();
  2948. CopyParam.NewerOnly = false;
  2949. CopyParam.FileMask = L"";
  2950. }
  2951. //---------------------------------------------------------------------------
  2952. void __fastcall TCustomScpExplorerForm::ExecutedFileChanged(const UnicodeString FileName,
  2953. TEditedFileData * Data, HANDLE UploadCompleteEvent)
  2954. {
  2955. TTerminalManager * Manager = TTerminalManager::Instance();
  2956. if ((Data->Terminal == NULL) || !Data->Terminal->Active)
  2957. {
  2958. if (!NonVisualDataModule->Busy)
  2959. {
  2960. UnicodeString FileNameOnly = ExtractFileName(FileName);
  2961. UnicodeString EditInactiveSessionReopenAcceptedCounter = L"EditInactiveSessionReopenAccepted";
  2962. UnicodeString EditInactiveSessionReopenRejectedCounter = L"EditInactiveSessionReopenRejected";
  2963. if (Data->Terminal == NULL)
  2964. {
  2965. TTerminal * SameSiteTerminal = Manager->FindActiveTerminalForSite(Data->SessionData);
  2966. if (SameSiteTerminal != NULL)
  2967. {
  2968. UnicodeString Message =
  2969. FMTLOAD(EDIT_SESSION_REATTACH,
  2970. (FileNameOnly, Data->SessionName, FileNameOnly));
  2971. if (MessageDialog(Message, qtConfirmation, qaOK | qaCancel) == qaOK)
  2972. {
  2973. Data->Terminal = Terminal;
  2974. Data->Queue = Manager->FindQueueForTerminal(Terminal);
  2975. Data->SessionName = Terminal->SessionData->SessionName;
  2976. // We might also overwrite session data
  2977. Configuration->Usage->Inc(EditInactiveSessionReopenAcceptedCounter);
  2978. }
  2979. else
  2980. {
  2981. Configuration->Usage->Inc(EditInactiveSessionReopenRejectedCounter);
  2982. Abort();
  2983. }
  2984. }
  2985. }
  2986. // foreground session should reconnect itself
  2987. else if (Terminal != Data->Terminal)
  2988. {
  2989. UnicodeString Message =
  2990. MainInstructions(
  2991. FMTLOAD(EDIT_SESSION_RECONNECT, (Data->SessionName, FileNameOnly)));
  2992. if (MessageDialog(Message, qtConfirmation, qaOK | qaCancel) == qaOK)
  2993. {
  2994. Manager->SetActiveTerminalWithAutoReconnect(Data->Terminal);
  2995. Configuration->Usage->Inc(EditInactiveSessionReopenAcceptedCounter);
  2996. }
  2997. else
  2998. {
  2999. Configuration->Usage->Inc(EditInactiveSessionReopenRejectedCounter);
  3000. Abort();
  3001. }
  3002. }
  3003. }
  3004. if ((Data->Terminal == NULL) || !Data->Terminal->Active)
  3005. {
  3006. Configuration->Usage->Inc(L"EditInactiveSession");
  3007. // Prevent this when not idle (!NonVisualDataModule->Busy)?
  3008. throw Exception(FMTLOAD(EDIT_SESSION_CLOSED2,
  3009. (ExtractFileName(FileName), Data->SessionName)));
  3010. }
  3011. }
  3012. TStrings * FileList = new TStringList();
  3013. try
  3014. {
  3015. FileList->Add(FileName);
  3016. // consider using the same settings (preset) as when the file was downloaded
  3017. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  3018. TemporaryFileCopyParam(CopyParam);
  3019. if (Data->ForceText)
  3020. {
  3021. CopyParam.TransferMode = tmAscii;
  3022. }
  3023. // so i do not need to worry if masking algorithm works in all cases
  3024. // ("" means "copy file name", no masking is actually done)
  3025. if (ExtractFileName(FileName) == Data->OriginalFileName)
  3026. {
  3027. CopyParam.FileMask = L"";
  3028. }
  3029. else
  3030. {
  3031. CopyParam.FileMask = DelimitFileNameMask(Data->OriginalFileName);
  3032. }
  3033. DebugAssert(Data->Queue != NULL);
  3034. int Params = cpNoConfirmation | cpTemporary;
  3035. TQueueItem * QueueItem = new TUploadQueueItem(Data->Terminal, FileList,
  3036. Data->RemoteDirectory, &CopyParam, Params, true);
  3037. QueueItem->CompleteEvent = UploadCompleteEvent;
  3038. AddQueueItem(Data->Queue, QueueItem, Data->Terminal);
  3039. }
  3040. __finally
  3041. {
  3042. delete FileList;
  3043. }
  3044. }
  3045. //---------------------------------------------------------------------------
  3046. void __fastcall TCustomScpExplorerForm::ExecutedFileReload(
  3047. const UnicodeString FileName, const TEditedFileData * Data)
  3048. {
  3049. // Sanity check, we should not be busy otherwise user would not be able to click Reload button.
  3050. DebugAssert(!NonVisualDataModule->Busy);
  3051. if ((Data->Terminal == NULL) || !Data->Terminal->Active)
  3052. {
  3053. throw Exception(FMTLOAD(EDIT_SESSION_CLOSED_RELOAD,
  3054. (ExtractFileName(FileName), Data->SessionName)));
  3055. }
  3056. TTerminal * PrevTerminal = TTerminalManager::Instance()->ActiveTerminal;
  3057. TTerminalManager::Instance()->ActiveTerminal = Data->Terminal;
  3058. try
  3059. {
  3060. std::unique_ptr<TRemoteFile> File;
  3061. UnicodeString RemoteFileName =
  3062. UnixIncludeTrailingBackslash(Data->RemoteDirectory) + Data->OriginalFileName;
  3063. FTerminal->ExceptionOnFail = true;
  3064. try
  3065. {
  3066. TRemoteFile * AFile = NULL;
  3067. FTerminal->ReadFile(RemoteFileName, AFile);
  3068. File.reset(AFile);
  3069. if (!File->HaveFullFileName)
  3070. {
  3071. File->FullFileName = RemoteFileName;
  3072. }
  3073. }
  3074. __finally
  3075. {
  3076. FTerminal->ExceptionOnFail = false;
  3077. }
  3078. std::unique_ptr<TStrings> FileList(new TStringList());
  3079. FileList->AddObject(RemoteFileName, File.get());
  3080. UnicodeString RootTempDir = Data->LocalRootDirectory;
  3081. UnicodeString TempDir = ExtractFilePath(FileName);
  3082. TemporarilyDownloadFiles(FileList.get(), Data->ForceText, RootTempDir,
  3083. TempDir, true, true, true);
  3084. // sanity check, the target file name should be still the same
  3085. DebugAssert(ExtractFileName(FileName) == FileList->Strings[0]);
  3086. }
  3087. __finally
  3088. {
  3089. // it actually may not exist anymore...
  3090. TTerminalManager::Instance()->ActiveTerminal = PrevTerminal;
  3091. }
  3092. }
  3093. //---------------------------------------------------------------------------
  3094. void __fastcall TCustomScpExplorerForm::ExecutedFileEarlyClosed(
  3095. const TEditedFileData * Data, bool & KeepOpen)
  3096. {
  3097. // Command is set for external editors only (not for "shell" open).
  3098. if (!Data->Command.IsEmpty())
  3099. {
  3100. bool AnyFound = false;
  3101. bool AnyMDI = false;
  3102. bool AnyNonMDI = false;
  3103. bool AnyDetect = false;
  3104. TEditorList * EditorList = new TEditorList();
  3105. try
  3106. {
  3107. *EditorList = *WinConfiguration->EditorList;
  3108. for (int i = 0; i < EditorList->Count; i++)
  3109. {
  3110. const TEditorPreferences * Editor = EditorList->Editors[i];
  3111. if ((Editor->Data->Editor == edExternal) &&
  3112. (Editor->Data->ExternalEditor == Data->Command))
  3113. {
  3114. AnyFound = true;
  3115. if (Editor->Data->SDIExternalEditor)
  3116. {
  3117. AnyNonMDI = true;
  3118. if (Editor->Data->DetectMDIExternalEditor)
  3119. {
  3120. AnyDetect = true;
  3121. }
  3122. }
  3123. else
  3124. {
  3125. AnyMDI = true;
  3126. }
  3127. }
  3128. }
  3129. bool EnableMDI = false;
  3130. bool DisableDetect = false;
  3131. if (AnyMDI)
  3132. {
  3133. KeepOpen = true;
  3134. if (AnyNonMDI)
  3135. {
  3136. // there is at least one instance of the editor with MDI support enabled,
  3137. // and one with disabled, enable it for all instances
  3138. EnableMDI = true;
  3139. }
  3140. }
  3141. else if (AnyFound && !AnyDetect)
  3142. {
  3143. // at least once instance found but all have MDI autodetection disabled
  3144. // => close the file (default action)
  3145. }
  3146. else
  3147. {
  3148. // no instance of the editor has MDI support enabled
  3149. TMessageParams Params;
  3150. if (AnyFound)
  3151. {
  3152. // there is at least one instance of the editor
  3153. Params.Params |= mpNeverAskAgainCheck;
  3154. }
  3155. unsigned int Answer = MessageDialog(FMTLOAD(EDITOR_EARLY_CLOSED2, (Data->OriginalFileName)), qtWarning,
  3156. qaYes | qaNo, HELP_EDITOR_EARLY_CLOSED, &Params);
  3157. switch (Answer)
  3158. {
  3159. case qaNeverAskAgain:
  3160. DisableDetect = true;
  3161. break;
  3162. case qaNo:
  3163. EnableMDI = true;
  3164. KeepOpen = true;
  3165. break;
  3166. }
  3167. }
  3168. if (AnyFound && (EnableMDI || DisableDetect))
  3169. {
  3170. bool Changed = false;
  3171. for (int i = 0; i < EditorList->Count; i++)
  3172. {
  3173. const TEditorPreferences * Editor = EditorList->Editors[i];
  3174. if ((Editor->Data->Editor == edExternal) &&
  3175. (Editor->Data->ExternalEditor == Data->Command) &&
  3176. ((EnableMDI && Editor->Data->SDIExternalEditor) ||
  3177. (DisableDetect && Editor->Data->DetectMDIExternalEditor)))
  3178. {
  3179. Changed = true;
  3180. TEditorPreferences * UpdatedEditor = new TEditorPreferences(*Editor);
  3181. if (EnableMDI)
  3182. {
  3183. UpdatedEditor->GetData()->SDIExternalEditor = false;
  3184. }
  3185. if (DisableDetect)
  3186. {
  3187. UpdatedEditor->GetData()->DetectMDIExternalEditor = false;
  3188. }
  3189. EditorList->Change(i, UpdatedEditor);
  3190. }
  3191. }
  3192. if (Changed)
  3193. {
  3194. WinConfiguration->EditorList = EditorList;
  3195. }
  3196. }
  3197. }
  3198. __finally
  3199. {
  3200. delete EditorList;
  3201. }
  3202. }
  3203. else
  3204. {
  3205. // "open" case
  3206. MessageDialog(FMTLOAD(APP_EARLY_CLOSED, (Data->OriginalFileName)), qtWarning,
  3207. qaOK, HELP_APP_EARLY_CLOSED);
  3208. }
  3209. }
  3210. //---------------------------------------------------------------------------
  3211. void __fastcall TCustomScpExplorerForm::ExecutedFileUploadComplete(TObject * Sender)
  3212. {
  3213. EditorFormFileUploadComplete(DebugNotNull(dynamic_cast<TForm *>(Sender)));
  3214. }
  3215. //---------------------------------------------------------------------------
  3216. void __fastcall TCustomScpExplorerForm::RemoteDirViewEnter(TObject * /*Sender*/)
  3217. {
  3218. SideEnter(osRemote);
  3219. }
  3220. //---------------------------------------------------------------------------
  3221. void __fastcall TCustomScpExplorerForm::RemoteDriveViewEnter(TObject * /*Sender*/)
  3222. {
  3223. MakeNextInTabOrder(RemoteDirView, RemoteDriveView);
  3224. SideEnter(osRemote);
  3225. }
  3226. //---------------------------------------------------------------------------
  3227. void __fastcall TCustomScpExplorerForm::SideEnter(TOperationSide Side)
  3228. {
  3229. FCurrentSide = Side;
  3230. if (Visible)
  3231. {
  3232. UpdateControls();
  3233. }
  3234. }
  3235. //---------------------------------------------------------------------------
  3236. void __fastcall TCustomScpExplorerForm::DeleteFiles(TOperationSide Side,
  3237. TStrings * FileList, bool Alternative)
  3238. {
  3239. DebugAssert(Terminal);
  3240. TCustomDirView * DView = DirView(Side);
  3241. DView->SaveSelection();
  3242. DView->SaveSelectedNames();
  3243. DebugAssert(!FAlternativeDelete);
  3244. FAlternativeDelete = Alternative;
  3245. try
  3246. {
  3247. if (Side == osRemote)
  3248. {
  3249. Terminal->DeleteFiles(FileList, FLAGMASK(Alternative, dfAlternative));
  3250. }
  3251. else
  3252. {
  3253. try
  3254. {
  3255. Terminal->DeleteLocalFiles(FileList, FLAGMASK(Alternative, dfAlternative));
  3256. }
  3257. __finally
  3258. {
  3259. ReloadLocalDirectory();
  3260. }
  3261. }
  3262. FAlternativeDelete = false;
  3263. }
  3264. catch(...)
  3265. {
  3266. FAlternativeDelete = false;
  3267. DView->DiscardSavedSelection();
  3268. throw;
  3269. }
  3270. DView->RestoreSelection();
  3271. }
  3272. //---------------------------------------------------------------------------
  3273. void __fastcall TCustomScpExplorerForm::LockFiles(TStrings * FileList, bool Lock)
  3274. {
  3275. DebugAssert(Terminal);
  3276. RemoteDirView->SaveSelection();
  3277. RemoteDirView->SaveSelectedNames();
  3278. try
  3279. {
  3280. if (Lock)
  3281. {
  3282. Terminal->LockFiles(FileList);
  3283. }
  3284. else
  3285. {
  3286. Terminal->UnlockFiles(FileList);
  3287. }
  3288. }
  3289. catch(...)
  3290. {
  3291. RemoteDirView->DiscardSavedSelection();
  3292. throw;
  3293. }
  3294. RemoteDirView->RestoreSelection();
  3295. }
  3296. //---------------------------------------------------------------------------
  3297. bool __fastcall TCustomScpExplorerForm::RemoteTransferDialog(TTerminal *& Session,
  3298. TStrings * FileList, UnicodeString & Target, UnicodeString & FileMask, bool & DirectCopy,
  3299. bool NoConfirmation, bool Move)
  3300. {
  3301. if (RemoteDriveView->DropTarget != NULL)
  3302. {
  3303. Target = RemoteDriveView->NodePathName(RemoteDriveView->DropTarget);
  3304. }
  3305. else if (RemoteDirView->DropTarget != NULL)
  3306. {
  3307. DebugAssert(RemoteDirView->ItemIsDirectory(RemoteDirView->DropTarget));
  3308. Target = RemoteDirView->ItemFullFileName(RemoteDirView->DropTarget);
  3309. }
  3310. else
  3311. {
  3312. Target = RemoteDirView->Path;
  3313. }
  3314. if (Session == NULL)
  3315. {
  3316. Session = TTerminalManager::Instance()->ActiveTerminal;
  3317. }
  3318. Target = UnixIncludeTrailingBackslash(Target);
  3319. if (FileList->Count == 1)
  3320. {
  3321. FileMask = DelimitFileNameMask(UnixExtractFileName(FileList->Strings[0]));
  3322. }
  3323. else
  3324. {
  3325. FileMask = L"*.*";
  3326. }
  3327. DirectCopy = FTerminal->IsCapable[fcRemoteCopy] || FTerminal->IsCapable[fcSecondaryShell];
  3328. bool Result = true;
  3329. if (!NoConfirmation)
  3330. {
  3331. bool Multi = (FileList->Count > 1);
  3332. if (Move)
  3333. {
  3334. Result = DoRemoteMoveDialog(Multi, Target, FileMask);
  3335. }
  3336. else
  3337. {
  3338. DebugAssert(Terminal != NULL);
  3339. // update Terminal->StateData->RemoteDirectory
  3340. UpdateTerminal(Terminal);
  3341. TStrings * Sessions = TTerminalManager::Instance()->TerminalList;
  3342. TStrings * Directories = new TStringList;
  3343. try
  3344. {
  3345. for (int Index = 0; Index < Sessions->Count; Index++)
  3346. {
  3347. TManagedTerminal * Terminal =
  3348. dynamic_cast<TManagedTerminal *>(Sessions->Objects[Index]);
  3349. Directories->Add(Terminal->StateData->RemoteDirectory);
  3350. }
  3351. TDirectRemoteCopy AllowDirectCopy;
  3352. if (FTerminal->IsCapable[fcRemoteCopy] || FTerminal->CommandSessionOpened)
  3353. {
  3354. DebugAssert(DirectCopy);
  3355. AllowDirectCopy = drcAllow;
  3356. }
  3357. else if (FTerminal->IsCapable[fcSecondaryShell])
  3358. {
  3359. DebugAssert(DirectCopy);
  3360. AllowDirectCopy = drcConfirmCommandSession;
  3361. }
  3362. else
  3363. {
  3364. DebugAssert(!DirectCopy);
  3365. AllowDirectCopy = drcDisallow;
  3366. }
  3367. void * ASession = Session;
  3368. Result = DoRemoteCopyDialog(Sessions, Directories, AllowDirectCopy,
  3369. Multi, ASession, Target, FileMask, DirectCopy);
  3370. Session = static_cast<TTerminal *>(ASession);
  3371. }
  3372. __finally
  3373. {
  3374. delete Directories;
  3375. }
  3376. }
  3377. }
  3378. return Result;
  3379. }
  3380. //---------------------------------------------------------------------------
  3381. bool __fastcall TCustomScpExplorerForm::RemoteTransferFiles(
  3382. TStrings * FileList, bool NoConfirmation, bool Move, TTerminal * Session)
  3383. {
  3384. bool DirectCopy;
  3385. UnicodeString Target, FileMask;
  3386. bool Result = RemoteTransferDialog(Session, FileList, Target, FileMask, DirectCopy, NoConfirmation, Move);
  3387. if (Result)
  3388. {
  3389. if (!Move && !DirectCopy)
  3390. {
  3391. Configuration->Usage->Inc("RemoteCopyTemp");
  3392. UnicodeString RootTempDir;
  3393. UnicodeString TempDir;
  3394. TemporarilyDownloadFiles(FileList, false, RootTempDir, TempDir, false, false, false);
  3395. TStrings * TemporaryFilesList = new TStringList();
  3396. try
  3397. {
  3398. TMakeLocalFileListParams MakeFileListParam;
  3399. MakeFileListParam.FileList = TemporaryFilesList;
  3400. MakeFileListParam.FileTimes = NULL;
  3401. MakeFileListParam.IncludeDirs = true;
  3402. MakeFileListParam.Recursive = false;
  3403. ProcessLocalDirectory(TempDir, FTerminal->MakeLocalFileList, &MakeFileListParam);
  3404. TTerminalManager::Instance()->ActiveTerminal = Session;
  3405. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  3406. CopyParam.FileMask = FileMask;
  3407. DebugAssert(!FAutoOperation);
  3408. FAutoOperation = true;
  3409. FTerminal->CopyToRemote(TemporaryFilesList, Target, &CopyParam, cpTemporary);
  3410. }
  3411. __finally
  3412. {
  3413. delete TemporaryFilesList;
  3414. FAutoOperation = false;
  3415. if (!RootTempDir.IsEmpty())
  3416. {
  3417. RecursiveDeleteFile(ExcludeTrailingBackslash(RootTempDir), false);
  3418. }
  3419. }
  3420. }
  3421. else
  3422. {
  3423. RemoteDirView->SaveSelection();
  3424. RemoteDirView->SaveSelectedNames();
  3425. try
  3426. {
  3427. if (Move)
  3428. {
  3429. Configuration->Usage->Inc("RemoteMove");
  3430. Terminal->MoveFiles(FileList, Target, FileMask);
  3431. }
  3432. else
  3433. {
  3434. Configuration->Usage->Inc("RemoteCopyDirect");
  3435. DebugAssert(DirectCopy);
  3436. DebugAssert(Session == FTerminal);
  3437. if (FTerminal->IsCapable[fcRemoteCopy] ||
  3438. FTerminal->CommandSessionOpened ||
  3439. CommandSessionFallback())
  3440. {
  3441. Terminal->CopyFiles(FileList, Target, FileMask);
  3442. }
  3443. }
  3444. }
  3445. catch(...)
  3446. {
  3447. RemoteDirView->DiscardSavedSelection();
  3448. throw;
  3449. }
  3450. RemoteDirView->RestoreSelection();
  3451. }
  3452. }
  3453. return Result;
  3454. }
  3455. //---------------------------------------------------------------------------
  3456. void __fastcall TCustomScpExplorerForm::CreateDirectory(TOperationSide Side)
  3457. {
  3458. Side = GetSide(Side);
  3459. TRemoteProperties Properties = GUIConfiguration->NewDirectoryProperties;
  3460. TRemoteProperties * AProperties = (Side == osRemote ? &Properties : NULL);
  3461. UnicodeString Name = LoadStr(NEW_FOLDER);
  3462. int AllowedChanges =
  3463. FLAGMASK(Terminal->IsCapable[fcModeChanging], cpMode);
  3464. bool SaveSettings = false;
  3465. if (DoCreateDirectoryDialog(Name, AProperties, AllowedChanges, SaveSettings))
  3466. {
  3467. TWindowLock Lock(this);
  3468. if (Side == osRemote)
  3469. {
  3470. if (SaveSettings)
  3471. {
  3472. GUIConfiguration->NewDirectoryProperties = Properties;
  3473. }
  3474. RemoteDirView->CreateDirectoryEx(Name, &Properties);
  3475. }
  3476. else
  3477. {
  3478. DirView(Side)->CreateDirectory(Name);
  3479. }
  3480. }
  3481. }
  3482. //---------------------------------------------------------------------------
  3483. void __fastcall TCustomScpExplorerForm::HomeDirectory(TOperationSide Side)
  3484. {
  3485. TWindowLock Lock(this);
  3486. DirView(Side)->ExecuteHomeDirectory();
  3487. }
  3488. //---------------------------------------------------------------------------
  3489. void __fastcall TCustomScpExplorerForm::OpenDirectory(TOperationSide Side)
  3490. {
  3491. DoOpenDirectoryDialog(odBrowse, Side);
  3492. }
  3493. //---------------------------------------------------------------------------
  3494. bool __fastcall TCustomScpExplorerForm::OpenBookmark(UnicodeString Local, UnicodeString Remote)
  3495. {
  3496. UnicodeString Path;
  3497. if (FCurrentSide == osRemote)
  3498. {
  3499. Path = Remote;
  3500. }
  3501. else
  3502. {
  3503. Path = Local;
  3504. }
  3505. bool Result = !Path.IsEmpty();
  3506. if (Result)
  3507. {
  3508. // While we might get here when the session is closed (from location profiles),
  3509. // it's not a problem as the Path setter is noop then
  3510. DirView(FCurrentSide)->Path = Path;
  3511. }
  3512. return Result;
  3513. }
  3514. //---------------------------------------------------------------------------
  3515. void __fastcall TCustomScpExplorerForm::RemoteDirViewGetSelectFilter(
  3516. TCustomDirView *Sender, bool Select, TFileFilter &Filter)
  3517. {
  3518. DebugAssert(Sender);
  3519. if (!DoSelectMaskDialog(Sender, Select, &Filter, Configuration)) Abort();
  3520. }
  3521. //---------------------------------------------------------------------------
  3522. void __fastcall TCustomScpExplorerForm::CalculateSize(
  3523. TStrings * FileList, __int64 & Size, TCalculateSizeStats & Stats,
  3524. bool & Close)
  3525. {
  3526. // terminal can be already closed (e.g. dropped connection)
  3527. if (Terminal != NULL)
  3528. {
  3529. try
  3530. {
  3531. Terminal->CalculateFilesSize(FileList, Size, 0, NULL, true, &Stats);
  3532. }
  3533. catch(...)
  3534. {
  3535. if (!Terminal->Active)
  3536. {
  3537. Close = true;
  3538. }
  3539. throw;
  3540. }
  3541. }
  3542. }
  3543. //---------------------------------------------------------------------------
  3544. void __fastcall TCustomScpExplorerForm::CalculateChecksum(const UnicodeString & Alg,
  3545. TStrings * FileList, TCalculatedChecksumEvent OnCalculatedChecksum,
  3546. bool & Close)
  3547. {
  3548. // terminal can be already closed (e.g. dropped connection)
  3549. if (Terminal != NULL)
  3550. {
  3551. Configuration->Usage->Inc(L"ChecksumCalculated");
  3552. try
  3553. {
  3554. Terminal->CalculateFilesChecksum(Alg, FileList, NULL, OnCalculatedChecksum);
  3555. }
  3556. catch(...)
  3557. {
  3558. if (!Terminal->Active)
  3559. {
  3560. Close = true;
  3561. }
  3562. throw;
  3563. }
  3564. }
  3565. }
  3566. //---------------------------------------------------------------------------
  3567. bool __fastcall TCustomScpExplorerForm::SetProperties(TOperationSide Side, TStrings * FileList)
  3568. {
  3569. bool Result;
  3570. if (Side == osRemote)
  3571. {
  3572. TRemoteTokenList * GroupList = NULL;
  3573. TRemoteTokenList * UserList = NULL;
  3574. try
  3575. {
  3576. TRemoteProperties CurrentProperties;
  3577. if (Terminal->LoadFilesProperties(FileList))
  3578. {
  3579. RemoteDirView->Invalidate();
  3580. }
  3581. bool CapableGroupChanging = Terminal->IsCapable[fcGroupChanging];
  3582. bool CapableOwnerChanging = Terminal->IsCapable[fcOwnerChanging];
  3583. if (CapableGroupChanging || CapableOwnerChanging)
  3584. {
  3585. if (CapableGroupChanging)
  3586. {
  3587. GroupList = Terminal->Groups->Duplicate();
  3588. }
  3589. if (CapableOwnerChanging)
  3590. {
  3591. UserList = Terminal->Users->Duplicate();
  3592. }
  3593. TRemoteDirectory * Files = Terminal->Files;
  3594. int Count = Files->Count;
  3595. if (Count > 100)
  3596. {
  3597. Count = 100;
  3598. }
  3599. for (int Index = 0; Index < Count; Index++)
  3600. {
  3601. TRemoteFile * File = Files->Files[Index];
  3602. if (CapableGroupChanging)
  3603. {
  3604. GroupList->AddUnique(File->Group);
  3605. }
  3606. if (CapableOwnerChanging)
  3607. {
  3608. UserList->AddUnique(File->Owner);
  3609. }
  3610. }
  3611. // if we haven't collected tokens for all files in current directory,
  3612. // make sure we collect them at least for all selected files.
  3613. // (note that so far the files in FileList has to be from current direcotry)
  3614. if (Count < Files->Count)
  3615. {
  3616. for (int Index = 0; Index < FileList->Count; Index++)
  3617. {
  3618. TRemoteFile * File = (TRemoteFile *)(FileList->Objects[Index]);
  3619. if (CapableGroupChanging)
  3620. {
  3621. GroupList->AddUnique(File->Group);
  3622. }
  3623. if (CapableOwnerChanging)
  3624. {
  3625. UserList->AddUnique(File->Owner);
  3626. }
  3627. }
  3628. }
  3629. }
  3630. CurrentProperties = TRemoteProperties::CommonProperties(FileList);
  3631. int Flags = 0;
  3632. if (Terminal->IsCapable[fcModeChanging]) Flags |= cpMode;
  3633. if (CapableOwnerChanging) Flags |= cpOwner;
  3634. if (CapableGroupChanging) Flags |= cpGroup;
  3635. TCalculateChecksumEvent CalculateChecksumEvent = NULL;
  3636. if (Terminal->IsCapable[fcCalculatingChecksum])
  3637. {
  3638. CalculateChecksumEvent = CalculateChecksum;
  3639. }
  3640. std::unique_ptr<TStrings> ChecksumAlgs(new TStringList());
  3641. Terminal->GetSupportedChecksumAlgs(ChecksumAlgs.get());
  3642. TRemoteProperties NewProperties = CurrentProperties;
  3643. Result =
  3644. DoPropertiesDialog(FileList, RemoteDirView->PathName,
  3645. GroupList, UserList, ChecksumAlgs.get(), &NewProperties, Flags,
  3646. Terminal->IsCapable[fcGroupOwnerChangingByID],
  3647. CalculateSize, CalculateChecksumEvent);
  3648. if (Result)
  3649. {
  3650. NewProperties = TRemoteProperties::ChangedProperties(CurrentProperties, NewProperties);
  3651. Terminal->ChangeFilesProperties(FileList, &NewProperties);
  3652. }
  3653. }
  3654. __finally
  3655. {
  3656. delete GroupList;
  3657. delete UserList;
  3658. }
  3659. }
  3660. else
  3661. {
  3662. DirView(Side)->DisplayPropertiesMenu();
  3663. Result = true;
  3664. }
  3665. return Result;
  3666. }
  3667. //---------------------------------------------------------------------------
  3668. void __fastcall TCustomScpExplorerForm::KeyProcessed(Word & Key, TShiftState Shift)
  3669. {
  3670. if (Shift * AllKeyShiftStates() == (TShiftState() << ssAlt))
  3671. {
  3672. FIgnoreNextDialogChar = Key;
  3673. }
  3674. Key = 0;
  3675. }
  3676. //---------------------------------------------------------------------------
  3677. void __fastcall TCustomScpExplorerForm::CheckCustomCommandShortCut(
  3678. TCustomCommandList * List, Word & Key, Classes::TShiftState Shift, TShortCut KeyShortCut)
  3679. {
  3680. const TCustomCommandType * Command = List->Find(KeyShortCut);
  3681. if (Command != NULL)
  3682. {
  3683. KeyProcessed(Key, Shift);
  3684. if (CustomCommandState(*Command, false) > 0)
  3685. {
  3686. ExecuteFileOperationCommand(foCustomCommand, osRemote,
  3687. false, false, const_cast<TCustomCommandType *>(Command));
  3688. }
  3689. }
  3690. }
  3691. //---------------------------------------------------------------------------
  3692. void __fastcall TCustomScpExplorerForm::KeyDown(Word & Key, Classes::TShiftState Shift)
  3693. {
  3694. if (QueueView3->Focused() && (QueueView3->OnKeyDown != NULL))
  3695. {
  3696. QueueView3->OnKeyDown(QueueView3, Key, Shift);
  3697. }
  3698. if (!DirView(osCurrent)->IsEditing())
  3699. {
  3700. TShortCut KeyShortCut = ShortCut(Key, Shift);
  3701. for (int Index = 0; Index < NonVisualDataModule->ExplorerActions->ActionCount; Index++)
  3702. {
  3703. TAction * Action = (TAction *)NonVisualDataModule->ExplorerActions->Actions[Index];
  3704. if (((Action->ShortCut == KeyShortCut) ||
  3705. (Action->SecondaryShortCuts->IndexOfShortCut(KeyShortCut) >= 0)) &&
  3706. AllowedAction(Action, aaShortCut))
  3707. {
  3708. // Has to be called before the action as the dialog char is already in queue.
  3709. // So when the action consumes message queue, we already need to have the
  3710. // FIgnoreNextDialogChar set
  3711. KeyProcessed(Key, Shift);
  3712. // Reset reference to previous component (when menu/toolbar was clicked).
  3713. // Needed to detect that action was invoked by keyboard shortcut
  3714. // in TNonVisualDataModule::ExplorerActionsExecute
  3715. Action->ActionComponent = NULL;
  3716. Action->Execute();
  3717. return;
  3718. }
  3719. }
  3720. for (int i = 0; i < TTerminalManager::Instance()->Count; i++)
  3721. {
  3722. if (NonVisualDataModule->OpenSessionShortCut(i) == KeyShortCut)
  3723. {
  3724. KeyProcessed(Key, Shift);
  3725. TTerminalManager::Instance()->ActiveTerminalIndex = i;
  3726. return;
  3727. }
  3728. }
  3729. if (Key == VK_TAB && Shift.Contains(ssCtrl))
  3730. {
  3731. KeyProcessed(Key, Shift);
  3732. TTerminalManager::Instance()->CycleTerminals(!Shift.Contains(ssShift));
  3733. }
  3734. if (IsCustomShortCut(KeyShortCut))
  3735. {
  3736. CheckCustomCommandShortCut(WinConfiguration->CustomCommandList, Key, Shift, KeyShortCut);
  3737. CheckCustomCommandShortCut(WinConfiguration->ExtensionList, Key, Shift, KeyShortCut);
  3738. if (WinConfiguration->SharedBookmarks != NULL)
  3739. {
  3740. TBookmark * Bookmark = WinConfiguration->SharedBookmarks->FindByShortCut(KeyShortCut);
  3741. if ((Bookmark != NULL) &&
  3742. OpenBookmark(Bookmark->Local, Bookmark->Remote))
  3743. {
  3744. KeyProcessed(Key, Shift);
  3745. }
  3746. }
  3747. }
  3748. }
  3749. TForm::KeyDown(Key, Shift);
  3750. }
  3751. //---------------------------------------------------------------------------
  3752. void __fastcall TCustomScpExplorerForm::InitStatusBar()
  3753. {
  3754. const TSessionInfo & SessionInfo = Terminal->GetSessionInfo();
  3755. const TFileSystemInfo & FileSystemInfo = Terminal->GetFileSystemInfo();
  3756. TTBXStatusBar * SessionStatusBar = (TTBXStatusBar *)GetComponent(fcStatusBar);
  3757. DebugAssert(Terminal);
  3758. int Offset = SessionStatusBar->Panels->Count - SessionPanelCount;
  3759. bool SecurityEnabled = !SessionInfo.SecurityProtocolName.IsEmpty();
  3760. SessionStatusBar->Panels->Items[Offset + 0]->Enabled = SecurityEnabled;
  3761. // expanded from ?: to avoid memory leaks
  3762. if (SecurityEnabled)
  3763. {
  3764. SessionStatusBar->Panels->Items[Offset + 0]->Hint =
  3765. FMTLOAD(STATUS_SECURE, (SessionInfo.SecurityProtocolName));
  3766. }
  3767. else
  3768. {
  3769. SessionStatusBar->Panels->Items[Offset + 0]->Hint = LoadStr(STATUS_INSECURE);
  3770. }
  3771. if (FileSystemInfo.ProtocolName.IsEmpty())
  3772. {
  3773. SessionStatusBar->Panels->Items[Offset + 1]->Caption = SessionInfo.ProtocolName;
  3774. }
  3775. else
  3776. {
  3777. SessionStatusBar->Panels->Items[Offset + 1]->Caption = FileSystemInfo.ProtocolName;
  3778. }
  3779. SessionStatusBar->Panels->Items[Offset + 1]->Hint = LoadStr(STATUS_PROTOCOL_HINT);
  3780. SessionStatusBar->Panels->Items[Offset + 2]->Enabled =
  3781. (!SessionInfo.CSCompression.IsEmpty() || !SessionInfo.SCCompression.IsEmpty());
  3782. if (SessionInfo.CSCompression == SessionInfo.SCCompression)
  3783. {
  3784. SessionStatusBar->Panels->Items[Offset + 2]->Hint =
  3785. FMTLOAD(STATUS_COMPRESSION_HINT, (DefaultStr(SessionInfo.CSCompression, LoadStr(NO_STR))));
  3786. }
  3787. else
  3788. {
  3789. SessionStatusBar->Panels->Items[Offset + 2]->Hint = FMTLOAD(STATUS_COMPRESSION2_HINT,
  3790. (DefaultStr(SessionInfo.CSCompression, LoadStr(NO_STR)),
  3791. DefaultStr(SessionInfo.SCCompression, LoadStr(NO_STR))));
  3792. }
  3793. SessionStatusBar->Panels->Items[Offset + 3]->Hint = LoadStr(STATUS_DURATION_HINT);
  3794. UpdateStatusBar();
  3795. }
  3796. //---------------------------------------------------------------------------
  3797. void __fastcall TCustomScpExplorerForm::UpdateStatusBar()
  3798. {
  3799. TTBXStatusBar * SessionStatusBar = (TTBXStatusBar *)GetComponent(fcStatusBar);
  3800. DebugAssert(SessionStatusBar != NULL);
  3801. if (!Terminal || !Terminal->Active || Terminal->Status < ssOpened)
  3802. {
  3803. // note: (Terminal->Status < sshReady) currently never happens here,
  3804. // so STATUS_CONNECTING is never used
  3805. SessionStatusBar->SimplePanel = true;
  3806. SessionStatusBar->SimpleText = LoadStr(
  3807. !Terminal || !Terminal->Active ? STATUS_NOT_CONNECTED : STATUS_CONNECTING);
  3808. }
  3809. else
  3810. {
  3811. DebugAssert(Terminal);
  3812. SessionStatusBar->SimplePanel = false;
  3813. const TSessionInfo & SessionInfo = Terminal->GetSessionInfo();
  3814. if (FShowStatusBarHint)
  3815. {
  3816. // escape hotkeys particularly because of the custom commands names
  3817. SessionStatusBar->Panels->Items[0]->Caption = EscapeHotkey(FStatusBarHint);
  3818. }
  3819. else if (!FNote.IsEmpty())
  3820. {
  3821. SessionStatusBar->Panels->Items[0]->Caption = FNote;
  3822. }
  3823. else
  3824. {
  3825. UpdateStatusPanelText(SessionStatusBar->Panels->Items[0]);
  3826. }
  3827. SessionStatusBar->Panels->Items[0]->Hint = FNoteHints;
  3828. SessionStatusBar->Panels->Items[SessionStatusBar->Panels->Count - 1]->Caption =
  3829. FormatDateTimeSpan(Configuration->TimeFormat, Now() - SessionInfo.LoginTime);
  3830. }
  3831. }
  3832. //---------------------------------------------------------------------------
  3833. void __fastcall TCustomScpExplorerForm::UpdateStatusPanelText(TTBXStatusPanel * Panel)
  3834. {
  3835. Panel->Caption = L"";
  3836. }
  3837. //---------------------------------------------------------------------------
  3838. void __fastcall TCustomScpExplorerForm::Idle()
  3839. {
  3840. if (FShowing || FStandaloneEditing)
  3841. {
  3842. FEditorManager->Check();
  3843. // make sure that Idle is called before update queue, as it may invoke QueueEvent
  3844. // that needs to know if queue view is visible (and it may be closed after queue update)
  3845. TTerminalManager::Instance()->Idle();
  3846. }
  3847. if (FShowing)
  3848. {
  3849. if (!NonVisualDataModule->Busy &&
  3850. // Menu is opened or drag&drop is going on
  3851. (Mouse->Capture == NULL))
  3852. {
  3853. if (FRefreshRemoteDirectory)
  3854. {
  3855. if ((Terminal != NULL) && (Terminal->Status == ssOpened))
  3856. {
  3857. Terminal->RefreshDirectory();
  3858. }
  3859. FRefreshRemoteDirectory = false;
  3860. }
  3861. if (FRefreshLocalDirectory)
  3862. {
  3863. ReloadLocalDirectory();
  3864. FRefreshLocalDirectory = false;
  3865. }
  3866. if (WinConfiguration->RefreshRemotePanel)
  3867. {
  3868. TManagedTerminal * ManagedTerminal =
  3869. dynamic_cast<TManagedTerminal *>(Terminal);
  3870. if ((ManagedTerminal != NULL) && (Terminal->Status == ssOpened) &&
  3871. (Now() - ManagedTerminal->DirectoryLoaded >
  3872. WinConfiguration->RefreshRemotePanelInterval))
  3873. {
  3874. RemoteDirView->ReloadDirectory();
  3875. }
  3876. }
  3877. }
  3878. }
  3879. if (FShowing || FStandaloneEditing)
  3880. {
  3881. if (FQueueStatusInvalidated)
  3882. {
  3883. UpdateQueueStatus(false);
  3884. }
  3885. RefreshQueueItems();
  3886. }
  3887. if (FShowing)
  3888. {
  3889. UpdateStatusBar();
  3890. }
  3891. FIgnoreNextDialogChar = 0;
  3892. }
  3893. //---------------------------------------------------------------------------
  3894. void __fastcall TCustomScpExplorerForm::UserActionTimer(TObject * /*Sender*/)
  3895. {
  3896. try
  3897. {
  3898. FUserActionTimer->Enabled = false;
  3899. if (IsQueueAutoPopup() && (FPendingQueueActionItem != NULL))
  3900. {
  3901. if (TQueueItem::IsUserActionStatus(FPendingQueueActionItem->Status))
  3902. {
  3903. FPendingQueueActionItem->ProcessUserAction();
  3904. }
  3905. }
  3906. }
  3907. __finally
  3908. {
  3909. FPendingQueueActionItem = NULL;
  3910. }
  3911. }
  3912. //---------------------------------------------------------------------------
  3913. void __fastcall TCustomScpExplorerForm::ApplicationHint(TObject * /*Sender*/)
  3914. {
  3915. DebugAssert(Application);
  3916. // Application->Hint contains long hint only
  3917. UnicodeString AHint = Application->Hint;
  3918. FShowStatusBarHint = Active && !AHint.IsEmpty();
  3919. if (FShowStatusBarHint)
  3920. {
  3921. FStatusBarHint = AHint;
  3922. }
  3923. else
  3924. {
  3925. FStatusBarHint = L"";
  3926. }
  3927. UpdateStatusBar();
  3928. }
  3929. //---------------------------------------------------------------------------
  3930. void __fastcall TCustomScpExplorerForm::ApplicationMinimize(TObject * /*Sender*/)
  3931. {
  3932. if (WinConfiguration->MinimizeToTray)
  3933. {
  3934. UpdateTrayIcon();
  3935. FTrayIcon->Visible = true;
  3936. if (Visible)
  3937. {
  3938. ShowWindow(Handle, SW_HIDE);
  3939. }
  3940. }
  3941. }
  3942. //---------------------------------------------------------------------------
  3943. void __fastcall TCustomScpExplorerForm::ApplicationRestore(TObject * /*Sender*/)
  3944. {
  3945. // WORKAROUND
  3946. // When restoring maximized window from minimization,
  3947. // rarely some controls do not align properly.
  3948. // Two instances seen (both for Commander):
  3949. // - When restoring, window is temporarily narrower (not maximizer),
  3950. // causing toolbars on TopDock to wrap and dock to expand horizontally.
  3951. // Once maximized already, top dock shinks back, but the session PageControl,
  3952. // do not align up, leaving space between TopDock and PageControl.
  3953. // - Similar issue seem with LocalDirView not aligning down to status bar.
  3954. for (int Index = 0; Index < ControlCount; Index++)
  3955. {
  3956. RealignControl(Controls[Index]);
  3957. }
  3958. if (FTrayIcon->Visible)
  3959. {
  3960. FTrayIcon->Visible = false;
  3961. if (Visible)
  3962. {
  3963. ShowWindow(Handle, SW_SHOW);
  3964. }
  3965. }
  3966. if (FNeedSession && DebugAlwaysTrue(Terminal == NULL))
  3967. {
  3968. FNeedSession = false;
  3969. NonVisualDataModule->StartBusy();
  3970. try
  3971. {
  3972. NeedSession(false);
  3973. }
  3974. __finally
  3975. {
  3976. NonVisualDataModule->EndBusy();
  3977. }
  3978. }
  3979. }
  3980. //---------------------------------------------------------------------------
  3981. void __fastcall TCustomScpExplorerForm::UpdateTrayIcon()
  3982. {
  3983. FTrayIcon->Hint = Caption;
  3984. }
  3985. //---------------------------------------------------------------------------
  3986. void __fastcall TCustomScpExplorerForm::ApplicationTitleChanged()
  3987. {
  3988. UpdateTrayIcon();
  3989. }
  3990. //---------------------------------------------------------------------------
  3991. void __fastcall TCustomScpExplorerForm::RestoreApp()
  3992. {
  3993. // workaround
  3994. // TApplication.WndProc sets TApplication.FAppIconic to false,
  3995. // on WM_ACTIVATEAPP, what renders TApplication.Restore no-op function.
  3996. // But WM_ACTIVATEAPP message can be received even when
  3997. // the main window is minimized to the tray and internal editor window is focused
  3998. // (after another application was previously active)
  3999. if (::IsIconic(Handle))
  4000. {
  4001. if (!IsAppIconic())
  4002. {
  4003. SetAppIconic(true);
  4004. }
  4005. }
  4006. ::ApplicationRestore();
  4007. Application->BringToFront();
  4008. }
  4009. //---------------------------------------------------------------------------
  4010. void __fastcall TCustomScpExplorerForm::TrayIconClick(TObject * /*Sender*/)
  4011. {
  4012. RestoreApp();
  4013. }
  4014. //---------------------------------------------------------------------------
  4015. void __fastcall TCustomScpExplorerForm::NewSession(bool FromSite, const UnicodeString & SessionUrl)
  4016. {
  4017. if (OpenInNewWindow())
  4018. {
  4019. // todo: Pass FromSite
  4020. ExecuteNewInstance(SessionUrl);
  4021. }
  4022. else
  4023. {
  4024. TTerminalManager::Instance()->NewSession(FromSite, SessionUrl);
  4025. }
  4026. }
  4027. //---------------------------------------------------------------------------
  4028. void __fastcall TCustomScpExplorerForm::DuplicateSession()
  4029. {
  4030. // current working directories become defaults here, what is not right
  4031. TSessionData * SessionData = CloneCurrentSessionData();
  4032. try
  4033. {
  4034. if (OpenInNewWindow())
  4035. {
  4036. UnicodeString SessionName = StoredSessions->HiddenPrefix + Terminal->SessionData->Name;
  4037. StoredSessions->NewSession(SessionName, SessionData);
  4038. // modified only, explicit
  4039. StoredSessions->Save(false, true);
  4040. // encode session name because of slashes in hierarchical sessions
  4041. ExecuteNewInstance(EncodeUrlString(SessionName));
  4042. }
  4043. else
  4044. {
  4045. TTerminalManager * Manager = TTerminalManager::Instance();
  4046. TTerminal * Terminal = Manager->NewTerminal(SessionData);
  4047. Manager->ActiveTerminal = Terminal;
  4048. }
  4049. }
  4050. __finally
  4051. {
  4052. delete SessionData;
  4053. }
  4054. }
  4055. //---------------------------------------------------------------------------
  4056. bool __fastcall TCustomScpExplorerForm::CanCloseQueue()
  4057. {
  4058. DebugAssert(FQueue != NULL);
  4059. bool Result = FQueue->IsEmpty;
  4060. if (!Result)
  4061. {
  4062. SetFocus();
  4063. Result = (MessageDialog(LoadStr(PENDING_QUEUE_ITEMS2), qtWarning, qaOK | qaCancel, HELP_NONE) == qaOK);
  4064. }
  4065. return Result;
  4066. }
  4067. //---------------------------------------------------------------------------
  4068. void __fastcall TCustomScpExplorerForm::CloseSession()
  4069. {
  4070. if (CanCloseQueue())
  4071. {
  4072. TTerminalManager::Instance()->FreeActiveTerminal();
  4073. }
  4074. }
  4075. //---------------------------------------------------------------------------
  4076. void __fastcall TCustomScpExplorerForm::OpenStoredSession(TSessionData * Data)
  4077. {
  4078. if (OpenInNewWindow())
  4079. {
  4080. // encode session name because of slashes in hierarchical sessions
  4081. ExecuteNewInstance(EncodeUrlString(Data->Name));
  4082. }
  4083. else
  4084. {
  4085. TTerminalManager * Manager = TTerminalManager::Instance();
  4086. TTerminal * Terminal = Manager->NewTerminal(Data);
  4087. Manager->ActiveTerminal = Terminal;
  4088. }
  4089. }
  4090. //---------------------------------------------------------------------------
  4091. void __fastcall TCustomScpExplorerForm::OpenFolderOrWorkspace(const UnicodeString & Name)
  4092. {
  4093. if (OpenInNewWindow())
  4094. {
  4095. ExecuteNewInstance(Name);
  4096. }
  4097. else
  4098. {
  4099. TTerminalManager * Manager = TTerminalManager::Instance();
  4100. std::unique_ptr<TObjectList> DataList(new TObjectList());
  4101. StoredSessions->GetFolderOrWorkspace(Name, DataList.get());
  4102. Manager->ActiveTerminal = Manager->NewTerminals(DataList.get());
  4103. }
  4104. }
  4105. //---------------------------------------------------------------------------
  4106. void __fastcall TCustomScpExplorerForm::FormCloseQuery(TObject * /*Sender*/,
  4107. bool &CanClose)
  4108. {
  4109. if (Terminal != NULL)
  4110. {
  4111. if (Terminal->Active && WinConfiguration->ConfirmClosingSession)
  4112. {
  4113. unsigned int Result;
  4114. TMessageParams Params(mpNeverAskAgainCheck);
  4115. UnicodeString Message;
  4116. int Answers = qaOK | qaCancel;
  4117. if (TTerminalManager::Instance()->Count > 1)
  4118. {
  4119. if (!WinConfiguration->AutoSaveWorkspace)
  4120. {
  4121. Message = LoadStr(CLOSE_SESSIONS_WORKSPACE2);
  4122. Answers = qaYes | qaNo | qaCancel;
  4123. }
  4124. else
  4125. {
  4126. Message = MainInstructions(LoadStr(CLOSE_SESSIONS));
  4127. }
  4128. }
  4129. else
  4130. {
  4131. Message = MainInstructions(FMTLOAD(CLOSE_SESSION, (Terminal->SessionData->SessionName)));
  4132. }
  4133. if (WinConfiguration->AutoSaveWorkspace)
  4134. {
  4135. Message =
  4136. FORMAT("%s\n\n%s", (Message,
  4137. FMTLOAD(AUTO_WORKSPACE, (WorkspaceName()))));
  4138. }
  4139. SetFocus();
  4140. Result = MessageDialog(Message, qtConfirmation,
  4141. Answers, HELP_NONE, &Params);
  4142. if (Result == qaNeverAskAgain)
  4143. {
  4144. WinConfiguration->ConfirmClosingSession = false;
  4145. }
  4146. if (Result == qaNo)
  4147. {
  4148. CanClose = SaveWorkspace(true);
  4149. // note that the workspace will be saved redundatly again from FormClose
  4150. }
  4151. else
  4152. {
  4153. CanClose =
  4154. (Result == qaOK) ||
  4155. (Result == qaYes) || // CLOSE_SESSIONS_WORKSPACE variant
  4156. (Result == qaNeverAskAgain);
  4157. }
  4158. }
  4159. if (CanClose)
  4160. {
  4161. CanClose = CanCloseQueue();
  4162. }
  4163. }
  4164. if (CanClose)
  4165. {
  4166. CanClose =
  4167. FEditorManager->CloseInternalEditors(CloseInternalEditor) &&
  4168. FEditorManager->CloseExternalFilesWithoutProcess();
  4169. if (CanClose)
  4170. {
  4171. while (CanClose && (FLocalEditors->Count > 0))
  4172. {
  4173. int PrevCount = FLocalEditors->Count;
  4174. static_cast<TForm *>(FLocalEditors->Items[0])->Close();
  4175. CanClose = (FLocalEditors->Count < PrevCount);
  4176. }
  4177. if (CanClose)
  4178. {
  4179. CanClose = FEditorManager->Empty(true);
  4180. if (!CanClose)
  4181. {
  4182. SetFocus();
  4183. CanClose =
  4184. (MessageDialog(
  4185. LoadStr(PENDING_EDITORS), qtWarning, qaIgnore | qaCancel, HELP_NONE) == qaIgnore);
  4186. }
  4187. }
  4188. }
  4189. }
  4190. }
  4191. //---------------------------------------------------------------------------
  4192. void __fastcall TCustomScpExplorerForm::CloseInternalEditor(TObject * Sender)
  4193. {
  4194. TForm * Form = dynamic_cast<TForm *>(Sender);
  4195. DebugAssert(Form != NULL);
  4196. Form->Close();
  4197. }
  4198. //---------------------------------------------------------------------------
  4199. void __fastcall TCustomScpExplorerForm::ForceCloseInternalEditor(TObject * Sender)
  4200. {
  4201. TForm * Form = dynamic_cast<TForm *>(Sender);
  4202. delete Form;
  4203. }
  4204. //---------------------------------------------------------------------------
  4205. void __fastcall TCustomScpExplorerForm::ForceCloseLocalEditors()
  4206. {
  4207. while (FLocalEditors->Count > 0)
  4208. {
  4209. delete static_cast<TForm *>(FLocalEditors->Items[0]);
  4210. }
  4211. }
  4212. //---------------------------------------------------------------------------
  4213. void __fastcall TCustomScpExplorerForm::RemoteDirViewDisplayProperties(
  4214. TObject *Sender)
  4215. {
  4216. TStrings *FileList = ((TUnixDirView*)Sender)->CreateFileList(True, False, NULL);
  4217. try
  4218. {
  4219. SetProperties(osRemote, FileList);
  4220. }
  4221. __finally
  4222. {
  4223. delete FileList;
  4224. }
  4225. }
  4226. //---------------------------------------------------------------------------
  4227. void __fastcall TCustomScpExplorerForm::ComponentShowing(Byte Component, bool value)
  4228. {
  4229. if (value)
  4230. {
  4231. if (Component == fcCustomCommandsBand)
  4232. {
  4233. UpdateCustomCommandsToolbar();
  4234. }
  4235. }
  4236. }
  4237. //---------------------------------------------------------------------------
  4238. void __fastcall TCustomScpExplorerForm::SetComponentVisible(Byte Component, Boolean value)
  4239. {
  4240. TControl * Control = GetComponent(Component);
  4241. DebugAssert(Control);
  4242. bool Changed = (Control->Visible != value);
  4243. if (Changed)
  4244. {
  4245. ComponentShowing(Component, value);
  4246. TWinControl * WinControl = dynamic_cast<TWinControl*>(Control);
  4247. bool WasFocused = (WinControl != NULL) && (ActiveControl != NULL) &&
  4248. ((ActiveControl == WinControl) || (ActiveControl->Parent == WinControl));
  4249. if (value)
  4250. {
  4251. int RemainingHeight = Control->Parent->ClientHeight;
  4252. int RemainingWidth = Control->Parent->ClientWidth;
  4253. for (int i = 0; i < Control->Parent->ControlCount; i++)
  4254. {
  4255. TControl * ChildControl = Control->Parent->Controls[i];
  4256. if (ChildControl->Visible)
  4257. {
  4258. switch (ChildControl->Align)
  4259. {
  4260. case alTop:
  4261. case alBottom:
  4262. RemainingHeight -= ChildControl->Height;
  4263. break;
  4264. case alLeft:
  4265. case alRight:
  4266. RemainingWidth -= ChildControl->Width;
  4267. break;
  4268. }
  4269. }
  4270. }
  4271. int Reserve = ScaleByTextHeight(this, 32);
  4272. // queue in explorer, trees in commander
  4273. if (Control->Height > RemainingHeight - Reserve)
  4274. {
  4275. Control->Height = RemainingHeight / 2;
  4276. }
  4277. if (Control->Width > RemainingWidth - Reserve)
  4278. {
  4279. Control->Width = RemainingWidth / 2;
  4280. }
  4281. }
  4282. Control->Visible = value;
  4283. if (WasFocused && Visible)
  4284. {
  4285. DirView(osCurrent)->SetFocus();
  4286. }
  4287. FixControlsPlacement();
  4288. }
  4289. }
  4290. //---------------------------------------------------------------------------
  4291. bool __fastcall TCustomScpExplorerForm::GetComponentVisible(Byte Component)
  4292. {
  4293. TControl * Control = GetComponent(Component);
  4294. if (Control == NULL)
  4295. {
  4296. return false;
  4297. }
  4298. else
  4299. {
  4300. return Control->Visible;
  4301. }
  4302. }
  4303. //---------------------------------------------------------------------------
  4304. void __fastcall TCustomScpExplorerForm::FixControlsPlacement()
  4305. {
  4306. if (RemoteDirView->ItemFocused != NULL)
  4307. {
  4308. RemoteDirView->ItemFocused->MakeVisible(false);
  4309. }
  4310. QueueSplitter->Visible = QueuePanel->Visible;
  4311. RemotePanelSplitter->Visible = RemoteDriveView->Visible;
  4312. }
  4313. //---------------------------------------------------------------------------
  4314. TControl * __fastcall TCustomScpExplorerForm::GetComponent(Byte Component)
  4315. {
  4316. switch (Component) {
  4317. case fcStatusBar: return RemoteStatusBar;
  4318. case fcRemotePopup: return reinterpret_cast<TControl *>(NonVisualDataModule->RemoteFilePopup);
  4319. case fcQueueView: return QueuePanel;
  4320. case fcQueueToolbar: return QueueDock;
  4321. case fcRemoteTree: return RemoteDriveView;
  4322. case fcSessionsTabs: return SessionsPageControl;
  4323. default: return NULL;
  4324. }
  4325. }
  4326. //---------------------------------------------------------------------------
  4327. void __fastcall TCustomScpExplorerForm::DirViewColumnRightClick(
  4328. TObject *Sender, TListColumn *Column, TPoint &Point)
  4329. {
  4330. DebugAssert(NonVisualDataModule && Column && Sender);
  4331. NonVisualDataModule->ListColumn = Column;
  4332. TPoint ScreenPoint = ((TControl*)Sender)->ClientToScreen(Point);
  4333. TPopupMenu * DirViewColumnMenu;
  4334. if (Sender == RemoteDirView)
  4335. {
  4336. DirViewColumnMenu = NonVisualDataModule->RemoteDirViewColumnPopup;
  4337. NonVisualDataModule->RemoteSortByExtColumnPopupItem->Visible =
  4338. (Column->Index == uvName);
  4339. NonVisualDataModule->RemoteFormatSizeBytesPopupItem->Visible =
  4340. (Column->Index == uvSize);
  4341. }
  4342. else
  4343. {
  4344. DirViewColumnMenu = NonVisualDataModule->LocalDirViewColumnPopup;
  4345. NonVisualDataModule->LocalSortByExtColumnPopupItem->Visible =
  4346. (Column->Index == dvName);
  4347. NonVisualDataModule->LocalFormatSizeBytesPopupItem->Visible =
  4348. (Column->Index == dvSize);
  4349. }
  4350. DirViewColumnMenu->Popup(ScreenPoint.x, ScreenPoint.y);
  4351. }
  4352. //---------------------------------------------------------------------------
  4353. void __fastcall TCustomScpExplorerForm::DirViewExecFile(
  4354. TObject *Sender, TListItem *Item, bool &AllowExec)
  4355. {
  4356. DoDirViewExecFile(Sender, Item, AllowExec);
  4357. }
  4358. //---------------------------------------------------------------------------
  4359. void __fastcall TCustomScpExplorerForm::DoDirViewExecFile(TObject * Sender,
  4360. TListItem * Item, bool & AllowExec)
  4361. {
  4362. DebugAssert(Sender && Item && Configuration);
  4363. DebugAssert(AllowExec);
  4364. TCustomDirView * ADirView = (TCustomDirView *)Sender;
  4365. bool Remote = (ADirView == DirView(osRemote));
  4366. bool ResolvedSymlinks = !Remote || Terminal->ResolvingSymlinks;
  4367. // Anything special is done on double click only (not on "open" indicated by FForceExecution),
  4368. // on files only (not directories)
  4369. // and only when symlinks are resolved (apply to remote panel only)
  4370. if (!ADirView->ItemIsDirectory(Item) &&
  4371. (ResolvedSymlinks || FForceExecution))
  4372. {
  4373. if ((WinConfiguration->DoubleClickAction != dcaOpen) &&
  4374. !FForceExecution &&
  4375. ResolvedSymlinks)
  4376. {
  4377. if (WinConfiguration->DoubleClickAction == dcaCopy)
  4378. {
  4379. ExecuteFileOperation(foCopy,
  4380. (ADirView == DirView(osRemote) ? osRemote : osLocal),
  4381. true, !WinConfiguration->CopyOnDoubleClickConfirmation);
  4382. AllowExec = false;
  4383. }
  4384. else if (WinConfiguration->DoubleClickAction == dcaEdit)
  4385. {
  4386. if (!Remote || !WinConfiguration->DisableOpenEdit)
  4387. {
  4388. ExecuteFile(osCurrent, efDefaultEditor);
  4389. AllowExec = false;
  4390. }
  4391. }
  4392. else
  4393. {
  4394. DebugFail();
  4395. }
  4396. }
  4397. // if we have not done anything special, fall back to default behavior
  4398. if (AllowExec)
  4399. {
  4400. if (Remote && !WinConfiguration->DisableOpenEdit)
  4401. {
  4402. ExecuteFile(osRemote, efShell);
  4403. AllowExec = false;
  4404. }
  4405. }
  4406. }
  4407. }
  4408. //---------------------------------------------------------------------------
  4409. bool __fastcall TCustomScpExplorerForm::GetHasDirView(TOperationSide Side)
  4410. {
  4411. return ((Side == osRemote) || (Side == osCurrent));
  4412. }
  4413. //---------------------------------------------------------------------------
  4414. void __fastcall TCustomScpExplorerForm::CompareDirectories()
  4415. {
  4416. DebugFail();
  4417. }
  4418. //---------------------------------------------------------------------------
  4419. void __fastcall TCustomScpExplorerForm::SynchronizeDirectories()
  4420. {
  4421. DebugFail();
  4422. }
  4423. //---------------------------------------------------------------------------
  4424. bool __fastcall TCustomScpExplorerForm::DoSynchronizeDirectories(
  4425. UnicodeString & LocalDirectory, UnicodeString & RemoteDirectory, bool UseDefaults)
  4426. {
  4427. TSynchronizeParamType Params;
  4428. Params.LocalDirectory = LocalDirectory;
  4429. Params.RemoteDirectory = RemoteDirectory;
  4430. int UnusedParams =
  4431. (GUIConfiguration->SynchronizeParams &
  4432. (spPreviewChanges | spTimestamp | spNotByTime | spBySize));
  4433. Params.Params = GUIConfiguration->SynchronizeParams & ~UnusedParams;
  4434. Params.Options = GUIConfiguration->SynchronizeOptions;
  4435. bool SaveSettings = false;
  4436. TSynchronizeController Controller(&DoSynchronize, &DoSynchronizeInvalid,
  4437. &DoSynchronizeTooManyDirectories);
  4438. DebugAssert(FSynchronizeController == NULL);
  4439. FSynchronizeController = &Controller;
  4440. bool Result;
  4441. try
  4442. {
  4443. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  4444. int CopyParamAttrs = Terminal->UsableCopyParamAttrs(0).Upload;
  4445. int Options =
  4446. FLAGMASK(SynchronizeAllowSelectedOnly(), soAllowSelectedOnly);
  4447. DebugAssert(FOnFeedSynchronizeError == NULL);
  4448. Result = DoSynchronizeDialog(Params, &CopyParam, Controller.StartStop,
  4449. SaveSettings, Options, CopyParamAttrs, GetSynchronizeOptions,
  4450. FOnFeedSynchronizeError, UseDefaults);
  4451. if (Result)
  4452. {
  4453. if (SaveSettings)
  4454. {
  4455. GUIConfiguration->SynchronizeParams = Params.Params | UnusedParams;
  4456. GUIConfiguration->SynchronizeOptions = Params.Options;
  4457. }
  4458. else
  4459. {
  4460. if (FLAGSET(GUIConfiguration->SynchronizeOptions, soSynchronizeAsk) &&
  4461. FLAGCLEAR(Params.Options, soSynchronizeAsk) &&
  4462. FLAGSET(Params.Options, soSynchronize))
  4463. {
  4464. GUIConfiguration->SynchronizeOptions =
  4465. (GUIConfiguration->SynchronizeOptions & ~soSynchronizeAsk) |
  4466. soSynchronize;
  4467. }
  4468. }
  4469. LocalDirectory = Params.LocalDirectory;
  4470. RemoteDirectory = Params.RemoteDirectory;
  4471. }
  4472. }
  4473. __finally
  4474. {
  4475. FSynchronizeController = NULL;
  4476. DebugAssert(FOnFeedSynchronizeError == NULL);
  4477. FOnFeedSynchronizeError = NULL;
  4478. }
  4479. return Result;
  4480. }
  4481. //---------------------------------------------------------------------------
  4482. void __fastcall TCustomScpExplorerForm::DoSynchronize(
  4483. TSynchronizeController * /*Sender*/, const UnicodeString LocalDirectory,
  4484. const UnicodeString RemoteDirectory, const TCopyParamType & CopyParam,
  4485. const TSynchronizeParamType & Params, TSynchronizeChecklist ** Checklist,
  4486. TSynchronizeOptions * Options, bool Full)
  4487. {
  4488. if (Terminal->Status == ssOpened)
  4489. {
  4490. try
  4491. {
  4492. int PParams = Params.Params;
  4493. if (!Full)
  4494. {
  4495. PParams |= TTerminal::spNoRecurse | TTerminal::spUseCache |
  4496. TTerminal::spDelayProgress | TTerminal::spSubDirs;
  4497. }
  4498. else
  4499. {
  4500. // if keepuptodate is non-recursive,
  4501. // full sync before has to be non-recursive as well
  4502. if (FLAGCLEAR(Params.Options, soRecurse))
  4503. {
  4504. PParams |= TTerminal::spNoRecurse;
  4505. }
  4506. }
  4507. Synchronize(LocalDirectory, RemoteDirectory, smRemote, CopyParam,
  4508. PParams, Checklist, Options);
  4509. }
  4510. catch(Exception & E)
  4511. {
  4512. ShowExtendedExceptionEx(Terminal, &E);
  4513. throw;
  4514. }
  4515. }
  4516. }
  4517. //---------------------------------------------------------------------------
  4518. void __fastcall TCustomScpExplorerForm::DoSynchronizeInvalid(
  4519. TSynchronizeController * /*Sender*/, const UnicodeString Directory,
  4520. const UnicodeString ErrorStr)
  4521. {
  4522. if (!Directory.IsEmpty())
  4523. {
  4524. SimpleErrorDialog(FMTLOAD(WATCH_ERROR_DIRECTORY, (Directory)), ErrorStr);
  4525. }
  4526. else
  4527. {
  4528. SimpleErrorDialog(LoadStr(WATCH_ERROR_GENERAL), ErrorStr);
  4529. }
  4530. }
  4531. //---------------------------------------------------------------------------
  4532. void __fastcall TCustomScpExplorerForm::DoSynchronizeTooManyDirectories(
  4533. TSynchronizeController * /*Sender*/, int & MaxDirectories)
  4534. {
  4535. if (MaxDirectories < GUIConfiguration->MaxWatchDirectories)
  4536. {
  4537. MaxDirectories = GUIConfiguration->MaxWatchDirectories;
  4538. }
  4539. else
  4540. {
  4541. TMessageParams Params(mpNeverAskAgainCheck);
  4542. unsigned int Result = MessageDialog(
  4543. FMTLOAD(TOO_MANY_WATCH_DIRECTORIES, (MaxDirectories, MaxDirectories)),
  4544. qtConfirmation, qaYes | qaNo, HELP_TOO_MANY_WATCH_DIRECTORIES, &Params);
  4545. if ((Result == qaYes) || (Result == qaNeverAskAgain))
  4546. {
  4547. MaxDirectories *= 2;
  4548. if (Result == qaNeverAskAgain)
  4549. {
  4550. GUIConfiguration->MaxWatchDirectories = MaxDirectories;
  4551. }
  4552. }
  4553. else
  4554. {
  4555. Abort();
  4556. }
  4557. }
  4558. }
  4559. //---------------------------------------------------------------------------
  4560. void __fastcall TCustomScpExplorerForm::Synchronize(const UnicodeString LocalDirectory,
  4561. const UnicodeString RemoteDirectory, TSynchronizeMode Mode,
  4562. const TCopyParamType & CopyParam, int Params, TSynchronizeChecklist ** Checklist,
  4563. TSynchronizeOptions * Options)
  4564. {
  4565. DebugAssert(!FAutoOperation);
  4566. void * BatchStorage;
  4567. BatchStart(BatchStorage);
  4568. FAutoOperation = true;
  4569. TSynchronizeChecklist * AChecklist = NULL;
  4570. try
  4571. {
  4572. FSynchronizeProgressForm = new TSynchronizeProgressForm(Application, true, true);
  4573. if (FLAGCLEAR(Params, TTerminal::spDelayProgress))
  4574. {
  4575. FSynchronizeProgressForm->Start();
  4576. }
  4577. AChecklist = Terminal->SynchronizeCollect(LocalDirectory, RemoteDirectory,
  4578. static_cast<TTerminal::TSynchronizeMode>(Mode),
  4579. &CopyParam, Params | spNoConfirmation, TerminalSynchronizeDirectory,
  4580. Options);
  4581. SAFE_DESTROY(FSynchronizeProgressForm);
  4582. FSynchronizeProgressForm = new TSynchronizeProgressForm(Application, true, false);
  4583. if (FLAGCLEAR(Params, TTerminal::spDelayProgress))
  4584. {
  4585. FSynchronizeProgressForm->Start();
  4586. }
  4587. Terminal->SynchronizeApply(AChecklist, LocalDirectory, RemoteDirectory,
  4588. &CopyParam, Params | spNoConfirmation, TerminalSynchronizeDirectory);
  4589. }
  4590. __finally
  4591. {
  4592. if (Checklist == NULL)
  4593. {
  4594. delete AChecklist;
  4595. }
  4596. else
  4597. {
  4598. *Checklist = AChecklist;
  4599. }
  4600. FAutoOperation = false;
  4601. SAFE_DESTROY(FSynchronizeProgressForm);
  4602. BatchEnd(BatchStorage);
  4603. ReloadLocalDirectory();
  4604. }
  4605. }
  4606. //---------------------------------------------------------------------------
  4607. bool __fastcall TCustomScpExplorerForm::SynchronizeAllowSelectedOnly()
  4608. {
  4609. // can be called from command line
  4610. return Visible &&
  4611. ((DirView(osRemote)->SelCount > 0) ||
  4612. (HasDirView[osLocal] && (DirView(osLocal)->SelCount > 0)));
  4613. }
  4614. //---------------------------------------------------------------------------
  4615. void __fastcall TCustomScpExplorerForm::GetSynchronizeOptions(
  4616. int Params, TSynchronizeOptions & Options)
  4617. {
  4618. if (FLAGSET(Params, spSelectedOnly) && SynchronizeAllowSelectedOnly())
  4619. {
  4620. Options.Filter = new TStringList();
  4621. Options.Filter->CaseSensitive = false;
  4622. Options.Filter->Duplicates = Types::dupAccept;
  4623. if (DirView(osRemote)->SelCount > 0)
  4624. {
  4625. DirView(osRemote)->CreateFileList(false, false, Options.Filter);
  4626. }
  4627. if (HasDirView[osLocal] && (DirView(osLocal)->SelCount > 0))
  4628. {
  4629. DirView(osLocal)->CreateFileList(false, false, Options.Filter);
  4630. }
  4631. Options.Filter->Sort();
  4632. }
  4633. }
  4634. //---------------------------------------------------------------------------
  4635. bool __fastcall TCustomScpExplorerForm::DoFullSynchronizeDirectories(
  4636. UnicodeString & LocalDirectory, UnicodeString & RemoteDirectory,
  4637. TSynchronizeMode & Mode, bool & SaveMode, bool UseDefaults)
  4638. {
  4639. bool Result;
  4640. int Params = GUIConfiguration->SynchronizeParams;
  4641. bool SaveSettings = false;
  4642. int Options =
  4643. FLAGMASK(!Terminal->IsCapable[fcTimestampChanging], fsoDisableTimestamp) |
  4644. FLAGMASK(SynchronizeAllowSelectedOnly(), fsoAllowSelectedOnly);
  4645. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  4646. TUsableCopyParamAttrs CopyParamAttrs = Terminal->UsableCopyParamAttrs(0);
  4647. Result = UseDefaults ||
  4648. DoFullSynchronizeDialog(Mode, Params, LocalDirectory, RemoteDirectory,
  4649. &CopyParam, SaveSettings, SaveMode, Options, CopyParamAttrs);
  4650. if (Result)
  4651. {
  4652. Configuration->Usage->Inc(L"Synchronizations");
  4653. UpdateCopyParamCounters(CopyParam);
  4654. TSynchronizeOptions SynchronizeOptions;
  4655. GetSynchronizeOptions(Params, SynchronizeOptions);
  4656. if (SaveSettings)
  4657. {
  4658. GUIConfiguration->SynchronizeParams = Params;
  4659. }
  4660. else
  4661. {
  4662. SaveMode = false;
  4663. }
  4664. TDateTime StartTime = Now();
  4665. TSynchronizeChecklist * Checklist = NULL;
  4666. try
  4667. {
  4668. DebugAssert(!FAutoOperation);
  4669. FAutoOperation = true;
  4670. try
  4671. {
  4672. FSynchronizeProgressForm = new TSynchronizeProgressForm(Application, true, true);
  4673. FSynchronizeProgressForm->Start();
  4674. Checklist = Terminal->SynchronizeCollect(LocalDirectory, RemoteDirectory,
  4675. static_cast<TTerminal::TSynchronizeMode>(Mode),
  4676. &CopyParam, Params | spNoConfirmation, TerminalSynchronizeDirectory,
  4677. &SynchronizeOptions);
  4678. }
  4679. __finally
  4680. {
  4681. FAutoOperation = false;
  4682. SAFE_DESTROY(FSynchronizeProgressForm);
  4683. }
  4684. if (Checklist->Count == 0)
  4685. {
  4686. UnicodeString Message = MainInstructions(LoadStr(COMPARE_NO_DIFFERENCES));
  4687. MessageDialog(Message, qtInformation, qaOK,
  4688. HELP_SYNCHRONIZE_NO_DIFFERENCES);
  4689. }
  4690. else if (FLAGCLEAR(Params, spPreviewChanges) ||
  4691. DoSynchronizeChecklistDialog(Checklist, Mode, Params,
  4692. LocalDirectory, RemoteDirectory, CustomCommandMenu))
  4693. {
  4694. DebugAssert(!FAutoOperation);
  4695. void * BatchStorage;
  4696. BatchStart(BatchStorage);
  4697. FAutoOperation = true;
  4698. if (FLAGSET(Params, spPreviewChanges))
  4699. {
  4700. StartTime = Now();
  4701. }
  4702. try
  4703. {
  4704. FSynchronizeProgressForm = new TSynchronizeProgressForm(Application, true, false);
  4705. FSynchronizeProgressForm->Start();
  4706. Terminal->SynchronizeApply(Checklist, LocalDirectory, RemoteDirectory,
  4707. &CopyParam, Params | spNoConfirmation, TerminalSynchronizeDirectory);
  4708. }
  4709. __finally
  4710. {
  4711. FAutoOperation = false;
  4712. SAFE_DESTROY(FSynchronizeProgressForm);
  4713. BatchEnd(BatchStorage);
  4714. ReloadLocalDirectory();
  4715. }
  4716. }
  4717. }
  4718. __finally
  4719. {
  4720. delete Checklist;
  4721. }
  4722. OperationComplete(StartTime);
  4723. }
  4724. return Result;
  4725. }
  4726. //---------------------------------------------------------------------------
  4727. void __fastcall TCustomScpExplorerForm::TerminalSynchronizeDirectory(
  4728. const UnicodeString LocalDirectory, const UnicodeString RemoteDirectory,
  4729. bool & Continue, bool /*Collect*/)
  4730. {
  4731. DebugAssert(FSynchronizeProgressForm != NULL);
  4732. if (!FSynchronizeProgressForm->Started)
  4733. {
  4734. FSynchronizeProgressForm->Start();
  4735. }
  4736. FSynchronizeProgressForm->SetData(LocalDirectory, RemoteDirectory, Continue);
  4737. }
  4738. //---------------------------------------------------------------------------
  4739. void __fastcall TCustomScpExplorerForm::StandaloneEdit(const UnicodeString & FileName)
  4740. {
  4741. UnicodeString FullFileName = AbsolutePath(FTerminal->CurrentDirectory, FileName);
  4742. TRemoteFile * File = NULL;
  4743. Terminal->ReadFile(FullFileName, File);
  4744. if (File != NULL)
  4745. {
  4746. std::unique_ptr<TRemoteFile> FileOwner(File);
  4747. TAutoFlag Flag(FStandaloneEditing);
  4748. ExecuteRemoteFile(FullFileName, File, efInternalEditor);
  4749. Application->ShowMainForm = false;
  4750. Application->Run();
  4751. }
  4752. }
  4753. //---------------------------------------------------------------------------
  4754. void __fastcall TCustomScpExplorerForm::ExploreLocalDirectory()
  4755. {
  4756. DebugFail();
  4757. }
  4758. //---------------------------------------------------------------------------
  4759. TSessionData * __fastcall TCustomScpExplorerForm::CloneCurrentSessionData()
  4760. {
  4761. std::unique_ptr<TSessionData> SessionData(new TSessionData(L""));
  4762. SessionData->Assign(Terminal->SessionData);
  4763. UpdateSessionData(SessionData.get());
  4764. TTerminalManager::Instance()->UpdateSessionCredentials(SessionData.get());
  4765. if (Terminal->SessionData->IsWorkspace)
  4766. {
  4767. // Have to reset the "Workspace/XXX" name which would become user-visible
  4768. // once IsWorkspace is cleared
  4769. SessionData->Name = UnicodeString();
  4770. SessionData->IsWorkspace = false;
  4771. }
  4772. return SessionData.release();
  4773. }
  4774. //---------------------------------------------------------------------------
  4775. void __fastcall TCustomScpExplorerForm::SaveCurrentSession()
  4776. {
  4777. TSessionData * SessionData = CloneCurrentSessionData();
  4778. try
  4779. {
  4780. TSessionData * EditingSessionData = StoredSessions->FindSame(SessionData);
  4781. DoSaveSession(SessionData, EditingSessionData, true, NULL);
  4782. }
  4783. __finally
  4784. {
  4785. delete SessionData;
  4786. }
  4787. }
  4788. //---------------------------------------------------------------------------
  4789. TObjectList * __fastcall TCustomScpExplorerForm::DoCollectWorkspace()
  4790. {
  4791. TTerminalManager * Manager = TTerminalManager::Instance();
  4792. std::unique_ptr<TObjectList> DataList(new TObjectList());
  4793. if (DebugAlwaysTrue(Terminal != NULL))
  4794. {
  4795. // Update (Managed)Terminal->StateData
  4796. UpdateTerminal(Terminal);
  4797. }
  4798. Manager->SaveWorkspace(DataList.get());
  4799. return DataList.release();
  4800. }
  4801. //---------------------------------------------------------------------------
  4802. void __fastcall TCustomScpExplorerForm::DoSaveWorkspace(
  4803. const UnicodeString & Name, TObjectList * DataList, bool SavePasswords)
  4804. {
  4805. WinConfiguration->LastWorkspace = Name;
  4806. if (!SavePasswords)
  4807. {
  4808. for (int Index = 0; Index < DataList->Count; Index++)
  4809. {
  4810. TSessionData * SessionData = dynamic_cast<TSessionData *>(DataList->Items[Index]);
  4811. if (SessionData->Link.IsEmpty())
  4812. {
  4813. SessionData->ClearSessionPasswords();
  4814. }
  4815. }
  4816. }
  4817. StoredSessions->NewWorkspace(Name, DataList);
  4818. // modified only, explicit
  4819. StoredSessions->Save(false, true);
  4820. }
  4821. //---------------------------------------------------------------------------
  4822. UnicodeString __fastcall TCustomScpExplorerForm::WorkspaceName()
  4823. {
  4824. return
  4825. DefaultStr(WinConfiguration->LastWorkspace,
  4826. DefaultStr(WinConfiguration->AutoWorkspace, LoadStr(NEW_WORKSPACE)));
  4827. }
  4828. //---------------------------------------------------------------------------
  4829. bool __fastcall TCustomScpExplorerForm::SaveWorkspace(bool EnableAutoSave)
  4830. {
  4831. std::unique_ptr<TObjectList> DataList(DoCollectWorkspace());
  4832. bool AnyNonStoredSessionWithPassword = false;
  4833. bool AnyNonStoredNonWorkspaceSessionWithPassword = false;
  4834. bool AllNonStoredSessionsAnonymous = true;
  4835. TTerminalManager * Manager = TTerminalManager::Instance();
  4836. for (int Index = 0; Index < DataList->Count; Index++)
  4837. {
  4838. TSessionData * SessionData = dynamic_cast<TSessionData *>(DataList->Items[Index]);
  4839. if (SessionData->Link.IsEmpty())
  4840. {
  4841. if (SessionData->HasAnySessionPassword())
  4842. {
  4843. AnyNonStoredSessionWithPassword = true;
  4844. if (!Manager->Terminals[Index]->SessionData->IsWorkspace)
  4845. {
  4846. AnyNonStoredNonWorkspaceSessionWithPassword = true;
  4847. }
  4848. }
  4849. if (!SameText(SessionData->UserName, AnonymousUserName))
  4850. {
  4851. AllNonStoredSessionsAnonymous = false;
  4852. }
  4853. }
  4854. }
  4855. bool SavePasswords;
  4856. bool * PSavePasswords;
  4857. bool NotRecommendedSavingPasswords =
  4858. !CustomWinConfiguration->UseMasterPassword &&
  4859. !AllNonStoredSessionsAnonymous;
  4860. if (Configuration->DisablePasswordStoring ||
  4861. !AnyNonStoredSessionWithPassword)
  4862. {
  4863. PSavePasswords = NULL;
  4864. SavePasswords = false;
  4865. }
  4866. else
  4867. {
  4868. PSavePasswords = &SavePasswords;
  4869. SavePasswords =
  4870. !AnyNonStoredNonWorkspaceSessionWithPassword ||
  4871. !NotRecommendedSavingPasswords;
  4872. }
  4873. UnicodeString Name = WorkspaceName();
  4874. bool CreateShortcut = false;
  4875. bool Result =
  4876. DoSaveWorkspaceDialog(
  4877. Name, PSavePasswords, NotRecommendedSavingPasswords, CreateShortcut,
  4878. EnableAutoSave);
  4879. if (Result)
  4880. {
  4881. DoSaveWorkspace(Name, DataList.get(), SavePasswords);
  4882. if (CreateShortcut)
  4883. {
  4884. TOperationVisualizer Visualizer;
  4885. UnicodeString AdditionalParams = TProgramParams::FormatSwitch(DESKTOP_SWITCH);
  4886. CreateDesktopSessionShortCut(Name, L"", AdditionalParams, -1, WORKSPACE_ICON);
  4887. }
  4888. if (EnableAutoSave)
  4889. {
  4890. WinConfiguration->AutoSaveWorkspace = true;
  4891. WinConfiguration->AutoWorkspace = Name;
  4892. if (PSavePasswords != NULL)
  4893. {
  4894. WinConfiguration->AutoSaveWorkspacePasswords = SavePasswords;
  4895. }
  4896. }
  4897. }
  4898. return Result;
  4899. }
  4900. //---------------------------------------------------------------------------
  4901. void __fastcall TCustomScpExplorerForm::UpdateTerminal(TTerminal * Terminal)
  4902. {
  4903. TManagedTerminal * ManagedTerminal = dynamic_cast<TManagedTerminal *>(Terminal);
  4904. DebugAssert(ManagedTerminal != NULL);
  4905. SAFE_DESTROY(ManagedTerminal->RemoteExplorerState);
  4906. if (WinConfiguration->PreservePanelState)
  4907. {
  4908. ManagedTerminal->RemoteExplorerState = RemoteDirView->SaveState();
  4909. }
  4910. UpdateSessionData(ManagedTerminal->StateData);
  4911. }
  4912. //---------------------------------------------------------------------------
  4913. void __fastcall TCustomScpExplorerForm::UpdateSessionData(TSessionData * Data)
  4914. {
  4915. // Keep in sync with TSessionData::CopyStateData
  4916. DebugAssert(Data != NULL);
  4917. // cannot use RemoteDirView->Path, because it is empty if connection
  4918. // was already closed
  4919. // also only peek, we may not be connected at all atm
  4920. // so make sure we do not try retrieving current directory from the server
  4921. // (particularly with FTP)
  4922. UnicodeString ACurrentDirectory = Terminal->PeekCurrentDirectory();
  4923. if (!ACurrentDirectory.IsEmpty())
  4924. {
  4925. Data->RemoteDirectory = ACurrentDirectory;
  4926. }
  4927. Data->Color = SessionColor;
  4928. }
  4929. //---------------------------------------------------------------------------
  4930. void __fastcall TCustomScpExplorerForm::ToolBarResize(TObject *Sender)
  4931. {
  4932. TTBXToolbar * Toolbar = dynamic_cast<TTBXToolbar*>(Sender);
  4933. DebugAssert(Toolbar != NULL);
  4934. for (int i = 0; i < Toolbar->Items->Count; i++)
  4935. {
  4936. TTBXCustomDropDownItem * DropDownItem;
  4937. DropDownItem = dynamic_cast<TTBXCustomDropDownItem *>(Toolbar->Items->Items[i]);
  4938. if (DropDownItem != NULL)
  4939. {
  4940. ToolbarItemResize(DropDownItem,
  4941. Toolbar->Width - (Toolbar->View->BaseSize.x - DropDownItem->EditWidth) -
  4942. Toolbar->NonClientWidth);
  4943. break;
  4944. }
  4945. }
  4946. }
  4947. //---------------------------------------------------------------------------
  4948. void __fastcall TCustomScpExplorerForm::ToolbarItemResize(TTBXCustomDropDownItem * Item, int Width)
  4949. {
  4950. Item->EditWidth = Width;
  4951. }
  4952. //---------------------------------------------------------------------------
  4953. void __fastcall TCustomScpExplorerForm::ToolbarGetBaseSize(
  4954. TTBCustomToolbar * Toolbar, TPoint & ASize)
  4955. {
  4956. for (int i = 0; i < Toolbar->Items->Count; i++)
  4957. {
  4958. TTBXCustomDropDownItem * DropDownItem;
  4959. DropDownItem = dynamic_cast<TTBXCustomDropDownItem *>(Toolbar->Items->Items[i]);
  4960. if (DropDownItem != NULL)
  4961. {
  4962. ASize.x -= DropDownItem->EditWidth;
  4963. ASize.x += ScaleByTextHeight(this, 50) /* minimal combo width */;
  4964. }
  4965. }
  4966. }
  4967. //---------------------------------------------------------------------------
  4968. void __fastcall TCustomScpExplorerForm::DoWarnLackOfTempSpace(
  4969. const UnicodeString Path, __int64 RequiredSpace, bool & Continue)
  4970. {
  4971. if (WinConfiguration->DDWarnLackOfTempSpace)
  4972. {
  4973. UnicodeString ADrive = ExtractFileDrive(ExpandFileName(Path));
  4974. if (!ADrive.IsEmpty())
  4975. {
  4976. __int64 FreeSpace = DiskFree((Byte)(ADrive[1]-'A'+1));
  4977. DebugAssert(RequiredSpace >= 0);
  4978. __int64 RequiredWithReserve;
  4979. RequiredWithReserve = (__int64)(RequiredSpace * WinConfiguration->DDWarnLackOfTempSpaceRatio);
  4980. if (FreeSpace < RequiredWithReserve)
  4981. {
  4982. unsigned int Result;
  4983. TMessageParams Params(mpNeverAskAgainCheck);
  4984. Result = MessageDialog(FMTLOAD(DD_WARN_LACK_OF_TEMP_SPACE, (Path,
  4985. FormatBytes(FreeSpace), FormatBytes(RequiredSpace))),
  4986. qtWarning, qaYes | qaNo, HELP_DD_WARN_LACK_OF_TEMP_SPACE, &Params);
  4987. if (Result == qaNeverAskAgain)
  4988. {
  4989. WinConfiguration->DDWarnLackOfTempSpace = false;
  4990. }
  4991. Continue = (Result == qaYes || Result == qaNeverAskAgain);
  4992. }
  4993. }
  4994. }
  4995. }
  4996. //---------------------------------------------------------------------------
  4997. void __fastcall TCustomScpExplorerForm::AddBookmark(TOperationSide Side)
  4998. {
  4999. DoOpenDirectoryDialog(odAddBookmark, Side);
  5000. }
  5001. //---------------------------------------------------------------------------
  5002. TStrings * __fastcall TCustomScpExplorerForm::CreateVisitedDirectories(TOperationSide Side)
  5003. {
  5004. // we should better use TCustomDirView::FCaseSensitive, but it is private
  5005. TStringList * VisitedDirectories = CreateSortedStringList((Side == osRemote));
  5006. try
  5007. {
  5008. TCustomDirView * DView = DirView(Side);
  5009. for (int Index = -DView->BackCount; Index <= DView->ForwardCount; Index++)
  5010. {
  5011. VisitedDirectories->Add(DView->HistoryPath[Index]);
  5012. }
  5013. }
  5014. catch (...)
  5015. {
  5016. delete VisitedDirectories;
  5017. throw;
  5018. }
  5019. return VisitedDirectories;
  5020. }
  5021. //---------------------------------------------------------------------------
  5022. void __fastcall TCustomScpExplorerForm::DoOpenDirectoryDialog(
  5023. TOpenDirectoryMode Mode, TOperationSide Side)
  5024. {
  5025. bool Continue = true;
  5026. if (Mode == odAddBookmark)
  5027. {
  5028. TMessageParams Params(mpNeverAskAgainCheck);
  5029. Params.NeverAskAgainTitle = LoadStr(ADD_BOOKMARK_SHARED);
  5030. Params.NeverAskAgainCheckedInitially = WinConfiguration->UseSharedBookmarks;
  5031. UnicodeString Message = MainInstructions(FMTLOAD(ADD_BOOKMARK_CONFIRM, (DirView(Side)->PathName)));
  5032. unsigned int Answer =
  5033. MessageDialog(Message,
  5034. qtConfirmation, qaOK | qaCancel, HELP_ADD_BOOKMARK_CONFIRM, &Params);
  5035. if (Answer == qaNeverAskAgain)
  5036. {
  5037. Continue = true;
  5038. WinConfiguration->UseSharedBookmarks = true;
  5039. }
  5040. else if (Answer == qaOK)
  5041. {
  5042. Continue = true;
  5043. WinConfiguration->UseSharedBookmarks = false;
  5044. }
  5045. else
  5046. {
  5047. Continue = false;
  5048. }
  5049. }
  5050. if (Continue)
  5051. {
  5052. TStrings * VisitedDirectories = CreateVisitedDirectories(Side);
  5053. try
  5054. {
  5055. UnicodeString Name = DirView(Side)->PathName;
  5056. if (::DoOpenDirectoryDialog(Mode, Side, Name, VisitedDirectories, Terminal,
  5057. // do not allow switching to location profiles,
  5058. // if we are not connected
  5059. HasDirView[osLocal] && (Terminal != NULL)))
  5060. {
  5061. TWindowLock Lock(this);
  5062. DirView(Side)->Path = Name;
  5063. }
  5064. }
  5065. __finally
  5066. {
  5067. delete VisitedDirectories;
  5068. }
  5069. }
  5070. }
  5071. //---------------------------------------------------------------------------
  5072. bool __fastcall TCustomScpExplorerForm::CommandSessionFallback()
  5073. {
  5074. bool Result = true;
  5075. DebugAssert(!FTerminal->CommandSessionOpened);
  5076. try
  5077. {
  5078. TTerminalManager::ConnectTerminal(FTerminal->CommandSession, false);
  5079. }
  5080. catch(Exception & E)
  5081. {
  5082. ShowExtendedExceptionEx(FTerminal->CommandSession, &E);
  5083. Result = false;
  5084. }
  5085. return Result;
  5086. }
  5087. //---------------------------------------------------------------------------
  5088. bool __fastcall TCustomScpExplorerForm::EnsureCommandSessionFallback(TFSCapability Capability)
  5089. {
  5090. bool Result = FTerminal->IsCapable[Capability] ||
  5091. FTerminal->CommandSessionOpened;
  5092. if (!Result)
  5093. {
  5094. DebugAssert(FTerminal->IsCapable[fcSecondaryShell]);
  5095. if (!GUIConfiguration->ConfirmCommandSession)
  5096. {
  5097. Result = true;
  5098. }
  5099. else
  5100. {
  5101. TMessageParams Params(mpNeverAskAgainCheck);
  5102. const TFileSystemInfo & FileSystemInfo = Terminal->GetFileSystemInfo();
  5103. unsigned int Answer = MessageDialog(FMTLOAD(PERFORM_ON_COMMAND_SESSION2,
  5104. (FileSystemInfo.ProtocolName, FileSystemInfo.ProtocolName)), qtConfirmation,
  5105. qaOK | qaCancel, HELP_PERFORM_ON_COMMAND_SESSION, &Params);
  5106. if (Answer == qaNeverAskAgain)
  5107. {
  5108. GUIConfiguration->ConfirmCommandSession = false;
  5109. Result = true;
  5110. }
  5111. else if (Answer == qaOK)
  5112. {
  5113. Result = true;
  5114. }
  5115. }
  5116. if (Result)
  5117. {
  5118. Result = CommandSessionFallback();
  5119. }
  5120. }
  5121. return Result;
  5122. }
  5123. //---------------------------------------------------------------------------
  5124. void __fastcall TCustomScpExplorerForm::OpenConsole(UnicodeString Command)
  5125. {
  5126. if (EnsureCommandSessionFallback(fcAnyCommand))
  5127. {
  5128. DoConsoleDialog(Terminal, Command);
  5129. }
  5130. }
  5131. //---------------------------------------------------------------------------
  5132. void __fastcall TCustomScpExplorerForm::FileControlDDDragEnter(
  5133. TObject *Sender, _di_IDataObject /*DataObj*/, int /*grfKeyState*/,
  5134. const TPoint & /*Point*/, int & /*dwEffect*/, bool & Accept)
  5135. {
  5136. if (IsFileControl(DropSourceControl, osRemote) &&
  5137. (FDDExtMapFile != NULL))
  5138. {
  5139. Accept = true;
  5140. }
  5141. FDDTargetControl = dynamic_cast<TControl*>(Sender);
  5142. DebugAssert(FDDTargetControl != NULL);
  5143. }
  5144. //---------------------------------------------------------------------------
  5145. void __fastcall TCustomScpExplorerForm::SessionsDDDragEnter(
  5146. _di_IDataObject DataObj, int KeyState,
  5147. const TPoint & Point, int & Effect, bool & Accept)
  5148. {
  5149. FileControlDDDragEnter(SessionsPageControl, DataObj, KeyState, Point, Effect, Accept);
  5150. }
  5151. //---------------------------------------------------------------------------
  5152. void __fastcall TCustomScpExplorerForm::QueueDDDragEnter(
  5153. _di_IDataObject DataObj, int KeyState,
  5154. const TPoint & Point, int & Effect, bool & Accept)
  5155. {
  5156. FileControlDDDragEnter(QueueView3, DataObj, KeyState, Point, Effect, Accept);
  5157. }
  5158. //---------------------------------------------------------------------------
  5159. void __fastcall TCustomScpExplorerForm::FileControlDDDragLeave(
  5160. TObject * Sender)
  5161. {
  5162. DebugUsedParam(Sender);
  5163. DebugAssert(FDDTargetControl == Sender);
  5164. FDDTargetControl = NULL;
  5165. }
  5166. //---------------------------------------------------------------------------
  5167. void __fastcall TCustomScpExplorerForm::SessionsDDDragLeave()
  5168. {
  5169. FileControlDDDragLeave(SessionsPageControl);
  5170. }
  5171. //---------------------------------------------------------------------------
  5172. void __fastcall TCustomScpExplorerForm::QueueDDDragLeave()
  5173. {
  5174. FileControlDDDragLeave(QueueView3);
  5175. }
  5176. //---------------------------------------------------------------------------
  5177. void __fastcall TCustomScpExplorerForm::AddEditLink(TOperationSide Side, bool Add)
  5178. {
  5179. DebugAssert(GetSide(Side) == osRemote);
  5180. DebugUsedParam(Side);
  5181. bool Edit = false;
  5182. TRemoteFile * File = NULL;
  5183. UnicodeString FileName;
  5184. UnicodeString PointTo;
  5185. bool SymbolicLink = true;
  5186. if (RemoteDirView->ItemFocused)
  5187. {
  5188. DebugAssert(RemoteDirView->ItemFocused->Data);
  5189. File = (TRemoteFile *)RemoteDirView->ItemFocused->Data;
  5190. Edit = !Add && File->IsSymLink && Terminal->SessionData->ResolveSymlinks;
  5191. if (Edit)
  5192. {
  5193. FileName = File->FileName;
  5194. PointTo = File->LinkTo;
  5195. }
  5196. else
  5197. {
  5198. PointTo = File->FileName;
  5199. }
  5200. }
  5201. if (DoSymlinkDialog(FileName, PointTo, osRemote, SymbolicLink, Edit,
  5202. Terminal->IsCapable[fcHardLink]))
  5203. {
  5204. Configuration->Usage->Inc(L"RemoteLinksCreated");
  5205. if (Edit)
  5206. {
  5207. DebugAssert(File->FileName == FileName);
  5208. int Params = dfNoRecursive;
  5209. Terminal->ExceptionOnFail = true;
  5210. try
  5211. {
  5212. Terminal->DeleteFile(L"", File, &Params);
  5213. }
  5214. __finally
  5215. {
  5216. Terminal->ExceptionOnFail = false;
  5217. }
  5218. }
  5219. Terminal->CreateLink(FileName, PointTo, SymbolicLink);
  5220. }
  5221. }
  5222. //---------------------------------------------------------------------------
  5223. bool __fastcall TCustomScpExplorerForm::CanAddEditLink(TOperationSide Side)
  5224. {
  5225. return
  5226. (Terminal != NULL) &&
  5227. ((GetSide(Side) != osRemote) ||
  5228. (Terminal->ResolvingSymlinks &&
  5229. Terminal->IsCapable[fcSymbolicLink]));
  5230. }
  5231. //---------------------------------------------------------------------------
  5232. bool __fastcall TCustomScpExplorerForm::LinkFocused()
  5233. {
  5234. return
  5235. (FCurrentSide == osRemote) &&
  5236. (RemoteDirView->ItemFocused != NULL) &&
  5237. ((TRemoteFile *)RemoteDirView->ItemFocused->Data)->IsSymLink &&
  5238. Terminal->SessionData->ResolveSymlinks;
  5239. }
  5240. //---------------------------------------------------------------------------
  5241. void __fastcall TCustomScpExplorerForm::ExecuteCurrentFile()
  5242. {
  5243. DebugAssert(!WinConfiguration->DisableOpenEdit);
  5244. FForceExecution = true;
  5245. try
  5246. {
  5247. DirView(osCurrent)->ExecuteCurrentFile();
  5248. }
  5249. __finally
  5250. {
  5251. FForceExecution = false;
  5252. }
  5253. }
  5254. //---------------------------------------------------------------------------
  5255. void __fastcall TCustomScpExplorerForm::ExecuteCurrentFileWith(bool OnFocused)
  5256. {
  5257. TEditorData ExternalEditor;
  5258. ExternalEditor.Editor = edExternal;
  5259. bool Remember = false;
  5260. if (DoEditorPreferencesDialog(&ExternalEditor, Remember, epmAdHoc,
  5261. (GetSide(osCurrent) == osRemote)))
  5262. {
  5263. if (Remember)
  5264. {
  5265. TEditorList * EditorList = new TEditorList;
  5266. try
  5267. {
  5268. *EditorList = *WinConfiguration->EditorList;
  5269. bool Found = false;
  5270. int i = 0;
  5271. while (!Found && (i < EditorList->Count))
  5272. {
  5273. const TEditorPreferences * Editor = EditorList->Editors[i];
  5274. if ((Editor->Data->Editor == edExternal) &&
  5275. (Editor->Data->ExternalEditor == ExternalEditor.ExternalEditor))
  5276. {
  5277. Found = true;
  5278. }
  5279. i++;
  5280. }
  5281. if (!Found)
  5282. {
  5283. EditorList->Add(new TEditorPreferences(ExternalEditor));
  5284. WinConfiguration->EditorList = EditorList;
  5285. }
  5286. }
  5287. __finally
  5288. {
  5289. delete EditorList;
  5290. }
  5291. }
  5292. ExecuteFile(osCurrent, efExternalEditor, &ExternalEditor, true, OnFocused);
  5293. }
  5294. }
  5295. //---------------------------------------------------------------------------
  5296. void __fastcall TCustomScpExplorerForm::TerminalRemoved(TObject * Sender)
  5297. {
  5298. FEditorManager->ProcessFiles(FileTerminalRemoved, Sender);
  5299. }
  5300. //---------------------------------------------------------------------------
  5301. void __fastcall TCustomScpExplorerForm::FileTerminalRemoved(const UnicodeString FileName,
  5302. TEditedFileData * Data, TObject * /*Token*/, void * Arg)
  5303. {
  5304. TTerminal * Terminal = static_cast<TTerminal *>(Arg);
  5305. DebugAssert(Terminal != NULL);
  5306. if (Data->Terminal == Terminal)
  5307. {
  5308. Data->Terminal = NULL;
  5309. }
  5310. }
  5311. //---------------------------------------------------------------------------
  5312. void __fastcall TCustomScpExplorerForm::LastTerminalClosed(TObject * /*Sender*/)
  5313. {
  5314. UpdateControls();
  5315. SessionColor = TColor(0);
  5316. UpdateRemotePathComboBox(false);
  5317. NeedSession(true);
  5318. }
  5319. //---------------------------------------------------------------------------
  5320. void __fastcall TCustomScpExplorerForm::NeedSession(bool ReloadSessions)
  5321. {
  5322. try
  5323. {
  5324. TTerminalManager::Instance()->NewSession(false, L"", ReloadSessions);
  5325. }
  5326. __finally
  5327. {
  5328. if (!WinConfiguration->KeepOpenWhenNoSession &&
  5329. (!Terminal || !Terminal->Active))
  5330. {
  5331. Application->Terminate();
  5332. }
  5333. }
  5334. }
  5335. //---------------------------------------------------------------------------
  5336. void __fastcall TCustomScpExplorerForm::DoTerminalListChanged(bool Force)
  5337. {
  5338. TStrings * TerminalList = TTerminalManager::Instance()->TerminalList;
  5339. int ActiveTerminalIndex = TTerminalManager::Instance()->ActiveTerminalIndex;
  5340. Configuration->Usage->SetMax(L"MaxOpenedSessions", TerminalList->Count);
  5341. bool ListChanged = Force || (TerminalList->Count + 1 != SessionsPageControl->PageCount);
  5342. if (!ListChanged)
  5343. {
  5344. int Index = 0;
  5345. while (!ListChanged && (Index < TerminalList->Count))
  5346. {
  5347. ListChanged =
  5348. (GetSessionTabTerminal(SessionsPageControl->Pages[Index]) != TerminalList->Objects[Index]) ||
  5349. (SessionsPageControl->Pages[Index]->Caption != TerminalList->Strings[Index]);
  5350. Index++;
  5351. }
  5352. }
  5353. if (ListChanged)
  5354. {
  5355. SendMessage(SessionsPageControl->Handle, WM_SETREDRAW, 0, 0);
  5356. try
  5357. {
  5358. FSessionColors->Clear();
  5359. AddFixedSessionImages();
  5360. while (SessionsPageControl->PageCount > 0)
  5361. {
  5362. delete SessionsPageControl->Pages[0];
  5363. }
  5364. for (int Index = 0; Index < TerminalList->Count; Index++)
  5365. {
  5366. TThemeTabSheet * TabSheet = new TThemeTabSheet(SessionsPageControl);
  5367. TabSheet->Caption = TerminalList->Strings[Index];
  5368. TTerminal * Terminal = dynamic_cast<TTerminal *>(TerminalList->Objects[Index]);
  5369. TabSheet->Tag = reinterpret_cast<int>(Terminal);
  5370. TabSheet->PageControl = SessionsPageControl;
  5371. UpdateSessionTab(TabSheet);
  5372. }
  5373. TTabSheet * TabSheet = new TTabSheet(SessionsPageControl);
  5374. TabSheet->PageControl = SessionsPageControl;
  5375. TabSheet->ImageIndex = FNewSessionTabImageIndex;
  5376. UpdateNewSessionTab();
  5377. }
  5378. __finally
  5379. {
  5380. SendMessage(SessionsPageControl->Handle, WM_SETREDRAW, 1, 0);
  5381. }
  5382. }
  5383. SessionsPageControl->ActivePageIndex = ActiveTerminalIndex;
  5384. }
  5385. //---------------------------------------------------------------------------
  5386. void __fastcall TCustomScpExplorerForm::TerminalListChanged(TObject * /*Sender*/)
  5387. {
  5388. DoTerminalListChanged(false);
  5389. }
  5390. //---------------------------------------------------------------------------
  5391. void __fastcall TCustomScpExplorerForm::UpdateNewSessionTab()
  5392. {
  5393. TTabSheet * TabSheet = SessionsPageControl->Pages[SessionsPageControl->PageCount - 1];
  5394. DebugAssert(TabSheet->ImageIndex == 0);
  5395. TabSheet->Caption =
  5396. WinConfiguration->SelectiveToolbarText ?
  5397. StripHotkey(StripTrailingPunctuation(NonVisualDataModule->NewSessionAction->Caption)) :
  5398. UnicodeString();
  5399. }
  5400. //---------------------------------------------------------------------------
  5401. TTerminal * __fastcall TCustomScpExplorerForm::GetSessionTabTerminal(TTabSheet * TabSheet)
  5402. {
  5403. return reinterpret_cast<TTerminal *>(TabSheet->Tag);
  5404. }
  5405. //---------------------------------------------------------------------------
  5406. void __fastcall TCustomScpExplorerForm::UpdateSessionTab(TTabSheet * TabSheet)
  5407. {
  5408. if (DebugAlwaysTrue(TabSheet != NULL))
  5409. {
  5410. TManagedTerminal * ManagedTerminal =
  5411. dynamic_cast<TManagedTerminal *>(GetSessionTabTerminal(TabSheet));
  5412. if (DebugAlwaysTrue(ManagedTerminal != NULL))
  5413. {
  5414. TColor Color =
  5415. (ManagedTerminal == FTerminal) ? FSessionColor : ManagedTerminal->StateData->Color;
  5416. TabSheet->ImageIndex = AddSessionColor(Color);
  5417. TThemeTabSheet * ThemeTabSheet = dynamic_cast<TThemeTabSheet *>(TabSheet);
  5418. if (DebugAlwaysTrue(ThemeTabSheet != NULL))
  5419. {
  5420. ThemeTabSheet->Shadowed = !ManagedTerminal->Active;
  5421. }
  5422. }
  5423. }
  5424. }
  5425. //---------------------------------------------------------------------------
  5426. bool __fastcall TCustomScpExplorerForm::SessionTabSwitched()
  5427. {
  5428. DebugAssert(SessionsPageControl->ActivePage != NULL);
  5429. TTerminal * Terminal = GetSessionTabTerminal(SessionsPageControl->ActivePage);
  5430. bool Result = (Terminal != NULL);
  5431. if (Result)
  5432. {
  5433. TTerminalManager::Instance()->ActiveTerminal = Terminal;
  5434. }
  5435. else
  5436. {
  5437. try
  5438. {
  5439. NewSession(false);
  5440. }
  5441. __finally
  5442. {
  5443. DoTerminalListChanged(false);
  5444. }
  5445. FSessionsPageControlNewSessionTime = Now();
  5446. }
  5447. return Result;
  5448. }
  5449. //---------------------------------------------------------------------------
  5450. void __fastcall TCustomScpExplorerForm::SessionsPageControlChange(TObject * /*Sender*/)
  5451. {
  5452. SessionTabSwitched();
  5453. }
  5454. //---------------------------------------------------------------------------
  5455. void __fastcall TCustomScpExplorerForm::TransferListChange(TObject * Sender)
  5456. {
  5457. TTBXStringList * TransferList = dynamic_cast<TTBXStringList *>(Sender);
  5458. DebugAssert(TransferList != NULL);
  5459. UnicodeString Name;
  5460. if (TransferList->ItemIndex <= 0)
  5461. {
  5462. Name = L"";
  5463. }
  5464. else
  5465. {
  5466. Name = GUIConfiguration->CopyParamList->Names[TransferList->ItemIndex - 1];
  5467. }
  5468. if (FCopyParamAutoSelected.IsEmpty())
  5469. {
  5470. // if previous preset was not autoselected, make new preset the "default"
  5471. FCopyParamDefault = Name;
  5472. }
  5473. GUIConfiguration->CopyParamCurrent = Name;
  5474. }
  5475. //---------------------------------------------------------------------------
  5476. void __fastcall TCustomScpExplorerForm::UpdateTransferLabel()
  5477. {
  5478. // sanity check
  5479. bool ExistingPreset =
  5480. (FTransferListHoverIndex >= 0) &&
  5481. (FTransferListHoverIndex < 1 + GUIConfiguration->CopyParamList->Count);
  5482. DebugAssert(ExistingPreset);
  5483. if (ExistingPreset)
  5484. {
  5485. HDC DC = GetDC(0);
  5486. TCanvas * Canvas = new TCanvas();
  5487. try
  5488. {
  5489. Canvas->Handle = DC;
  5490. Canvas->Font = ToolbarFont;
  5491. UnicodeString Name;
  5492. if (FTransferListHoverIndex == 0)
  5493. {
  5494. Name = L"";
  5495. }
  5496. else
  5497. {
  5498. Name = GUIConfiguration->CopyParamList->Names[FTransferListHoverIndex - 1];
  5499. }
  5500. TTBXLabelItem * TransferLabel = dynamic_cast<TTBXLabelItem*>(
  5501. static_cast<TComponent*>(GetComponent(fcTransferLabel)));
  5502. TTBXStringList * TransferList = dynamic_cast<TTBXStringList*>(
  5503. static_cast<TObject*>(GetComponent(fcTransferList)));
  5504. UnicodeString InfoStr =
  5505. GUIConfiguration->CopyParamPreset[Name].
  5506. GetInfoStr(L"; ",
  5507. FLAGMASK(Terminal != NULL, Terminal->UsableCopyParamAttrs(0).General));
  5508. int MaxWidth = TransferList->MinWidth - (2 * TransferLabel->Margin) - ScaleByTextHeight(this, 10);
  5509. if (Canvas->TextExtent(InfoStr).cx > MaxWidth)
  5510. {
  5511. UnicodeString Ellipsis = L"...";
  5512. while (Canvas->TextExtent(InfoStr + Ellipsis).cx > MaxWidth)
  5513. {
  5514. InfoStr.SetLength(InfoStr.Length() - 1);
  5515. }
  5516. InfoStr += Ellipsis;
  5517. }
  5518. // UpdateCaption does not cause invalidation of whole submenu, while
  5519. // setting Caption property does.
  5520. // also it probably does not resize the label, even if necessary
  5521. // (we do not want that anyway)
  5522. TransferLabel->UpdateCaption(InfoStr);
  5523. }
  5524. __finally
  5525. {
  5526. Canvas->Handle = NULL;
  5527. ReleaseDC(0, DC);
  5528. delete Canvas;
  5529. }
  5530. }
  5531. }
  5532. //---------------------------------------------------------------------------
  5533. void __fastcall TCustomScpExplorerForm::TransferListDrawItem(
  5534. TTBXCustomList * /*Sender*/, TCanvas * /*ACanvas*/, const TRect & /*ARect*/,
  5535. int /*AIndex*/, int AHoverIndex, bool & /*DrawDefault*/)
  5536. {
  5537. if (FTransferListHoverIndex != AHoverIndex)
  5538. {
  5539. FTransferListHoverIndex = AHoverIndex;
  5540. UpdateTransferLabel();
  5541. }
  5542. }
  5543. //---------------------------------------------------------------------------
  5544. void __fastcall TCustomScpExplorerForm::WMAppCommand(TMessage & Message)
  5545. {
  5546. int Command = GET_APPCOMMAND_LPARAM(Message.LParam);
  5547. TShiftState Shift = KeyDataToShiftState(GET_KEYSTATE_LPARAM(Message.LParam));
  5548. if ((Shift * (TShiftState() << ssShift << ssAlt << ssCtrl)).Empty())
  5549. {
  5550. if (Command == _APPCOMMAND_BROWSER_FAVORITES)
  5551. {
  5552. OpenDirectory(GetSide(osCurrent));
  5553. Message.Result = 1;
  5554. }
  5555. else
  5556. {
  5557. TForm::Dispatch(&Message);
  5558. }
  5559. }
  5560. else
  5561. {
  5562. TForm::Dispatch(&Message);
  5563. }
  5564. }
  5565. //---------------------------------------------------------------------------
  5566. void __fastcall TCustomScpExplorerForm::CMDialogChar(TMessage & AMessage)
  5567. {
  5568. TCMDialogChar & Message = reinterpret_cast<TCMDialogChar &>(AMessage);
  5569. if ((FIgnoreNextDialogChar != 0) &&
  5570. (toupper(Message.CharCode) == toupper(FIgnoreNextDialogChar)))
  5571. {
  5572. Message.Result = 1;
  5573. }
  5574. else
  5575. {
  5576. TForm::Dispatch(&Message);
  5577. }
  5578. FIgnoreNextDialogChar = 0;
  5579. }
  5580. //---------------------------------------------------------------------------
  5581. void __fastcall TCustomScpExplorerForm::WMSysCommand(TMessage & Message)
  5582. {
  5583. // The four low-order bits are used internally by Windows
  5584. unsigned int Cmd = (Message.WParam & 0xFFF0);
  5585. // SC_RESTORE, SC_MAXIMIZE, SC_MINIMIZE - buttons on windows title
  5586. // SC_DEFAULT - double click on windows title (does not work, at least on WinXP)
  5587. // 61730 - restore thru double click - undocumented
  5588. // 61490 - maximize thru double click - undocumented
  5589. if ((Cmd == SC_RESTORE) || (Cmd == SC_MAXIMIZE) ||
  5590. (Cmd == SC_MINIMIZE) || (Cmd == SC_DEFAULT))
  5591. {
  5592. SysResizing(Cmd);
  5593. }
  5594. TForm::Dispatch(&Message);
  5595. }
  5596. //---------------------------------------------------------------------------
  5597. void __fastcall TCustomScpExplorerForm::WMQueryEndSession(TMessage & Message)
  5598. {
  5599. // We were actually never able to make ENDSESSION_CRITICAL happen.
  5600. // Also there no point returning TRUE as we are not able to
  5601. // handle the abrupt termination caused by subsequent WM_ENDSESSION cleanly.
  5602. // Hence the process termination might be safer :)
  5603. if ((Message.LParam != ENDSESSION_CRITICAL) &&
  5604. (((FQueue != NULL) && !FQueue->IsEmpty) || (FProgressForm != NULL)))
  5605. {
  5606. Message.Result = FALSE;
  5607. }
  5608. else
  5609. {
  5610. Message.Result = TRUE;
  5611. }
  5612. // Do not call default handling as that triggers OnCloseQuery,
  5613. // where our implementation will popup configuration dialogs, what we do not want,
  5614. // as per Vista guidelines:
  5615. // msdn.microsoft.com/en-us/library/ms700677.aspx
  5616. }
  5617. //---------------------------------------------------------------------------
  5618. void __fastcall TCustomScpExplorerForm::WMEndSession(TWMEndSession & Message)
  5619. {
  5620. if (Message.EndSession && IsApplicationMinimized())
  5621. {
  5622. // WORKAROUND
  5623. // TApplication.WndProc() calls Application.Terminate() before Halt(),
  5624. // when it receives WM_ENDSESSION,
  5625. // but that sometimes (particularly when application is minimized) cause crashes.
  5626. // Crash popups message that blocks log off.
  5627. // Obviously application cannot shutdown cleanly after WM_ENDSESSION,
  5628. // so we call ExitProcess() immediately, not even trying to cleanup.
  5629. // It still causes beep, so there's likely some popup, but it does not block
  5630. // log off.
  5631. ExitProcess(0);
  5632. }
  5633. TForm::Dispatch(&Message);
  5634. }
  5635. //---------------------------------------------------------------------------
  5636. void __fastcall TCustomScpExplorerForm::SysResizing(unsigned int /*Cmd*/)
  5637. {
  5638. }
  5639. //---------------------------------------------------------------------------
  5640. void __fastcall TCustomScpExplorerForm::DoShow()
  5641. {
  5642. // only now are the controls resized finally, so the size constraints
  5643. // will not conflict with possibly very small window size
  5644. RestoreFormParams();
  5645. FixControlsPlacement();
  5646. if (Position == poDefaultPosOnly)
  5647. {
  5648. CutFormToDesktop(this);
  5649. }
  5650. TForm::DoShow();
  5651. FSessionsDragDropFilesEx->DragDropControl = SessionsPageControl;
  5652. FQueueDragDropFilesEx->DragDropControl = QueueView3;
  5653. if (Terminal == NULL)
  5654. {
  5655. StartingDisconnected();
  5656. }
  5657. FShowing = true;
  5658. }
  5659. //---------------------------------------------------------------------------
  5660. void __fastcall TCustomScpExplorerForm::StartingDisconnected()
  5661. {
  5662. DoTerminalListChanged(true);
  5663. }
  5664. //---------------------------------------------------------------------------
  5665. void __fastcall TCustomScpExplorerForm::PopupTrayBalloon(TTerminal * Terminal,
  5666. const UnicodeString & Str, TQueryType Type, Exception * E, unsigned int Seconds,
  5667. TNotifyEvent OnBalloonClick, TObject * UserData)
  5668. {
  5669. bool Do;
  5670. UnicodeString Message;
  5671. if (E == NULL)
  5672. {
  5673. Message = Str;
  5674. Do = true;
  5675. }
  5676. else
  5677. {
  5678. Do = ExceptionMessage(E, Message);
  5679. }
  5680. if (Do && WinConfiguration->BalloonNotifications)
  5681. {
  5682. UnicodeString Title;
  5683. if ((Terminal == NULL) && (Type == qtInformation) && ExtractMainInstructions(Message, Title))
  5684. {
  5685. Message = TrimLeft(Message);
  5686. }
  5687. else
  5688. {
  5689. Message = UnformatMessage(Message);
  5690. const ResourceString * Captions[] = { &_SMsgDlgConfirm, &_SMsgDlgWarning,
  5691. &_SMsgDlgError, &_SMsgDlgInformation, NULL };
  5692. Title = LoadResourceString(Captions[Type]);
  5693. if (Terminal != NULL)
  5694. {
  5695. Title = FORMAT(L"%s - %s",
  5696. (TTerminalManager::Instance()->TerminalTitle(Terminal), Title));
  5697. }
  5698. }
  5699. if (Seconds == 0)
  5700. {
  5701. Seconds = WinConfiguration->NotificationsTimeout;
  5702. }
  5703. FTrayIcon->PopupBalloon(Title, Message, Type, Seconds * MSecsPerSec, OnBalloonClick, UserData);
  5704. }
  5705. }
  5706. //---------------------------------------------------------------------------
  5707. unsigned int __fastcall TCustomScpExplorerForm::MoreMessageDialog(const UnicodeString Message,
  5708. TStrings * MoreMessages, TQueryType Type, unsigned int Answers,
  5709. UnicodeString HelpKeyword, const TMessageParams * Params,
  5710. TTerminal * Terminal)
  5711. {
  5712. if (((WinConfiguration->ContinueOnError && (FErrorList != NULL)) ||
  5713. (FOnFeedSynchronizeError != NULL)) &&
  5714. (Params != NULL) && (Params->Params & mpAllowContinueOnError) )
  5715. {
  5716. if (FOnFeedSynchronizeError != NULL)
  5717. {
  5718. FOnFeedSynchronizeError(Message, MoreMessages, Type, HelpKeyword);
  5719. }
  5720. else
  5721. {
  5722. DebugAssert(FErrorList != NULL);
  5723. TStringList * MoreMessagesCopy = NULL;
  5724. if (MoreMessages)
  5725. {
  5726. MoreMessagesCopy = new TStringList();
  5727. MoreMessagesCopy->Assign(MoreMessages);
  5728. }
  5729. FErrorList->AddObject(Message, MoreMessagesCopy);
  5730. }
  5731. PopupTrayBalloon(Terminal, Message, Type);
  5732. return ContinueAnswer(Answers);
  5733. }
  5734. else
  5735. {
  5736. bool UseBalloon = FTrayIcon->Visible;
  5737. if (UseBalloon)
  5738. {
  5739. PopupTrayBalloon(Terminal, Message, Type);
  5740. }
  5741. unsigned int Result;
  5742. try
  5743. {
  5744. Result = ::MoreMessageDialog(Message, MoreMessages, Type, Answers, HelpKeyword, Params);
  5745. }
  5746. __finally
  5747. {
  5748. // cancel only balloon we popped up, otherwise we may cancel notification
  5749. // balloon that was there before the message dialog
  5750. // (such as "dd confirmation opt in balloon)
  5751. if (UseBalloon)
  5752. {
  5753. FTrayIcon->CancelBalloon();
  5754. }
  5755. }
  5756. return Result;
  5757. }
  5758. }
  5759. //---------------------------------------------------------------------------
  5760. void __fastcall TCustomScpExplorerForm::ShowExtendedException(
  5761. TTerminal * Terminal, Exception * E)
  5762. {
  5763. if (FTrayIcon->Visible)
  5764. {
  5765. PopupTrayBalloon(Terminal, L"", qtError, E);
  5766. }
  5767. // particularly prevent opening new session from jump list,
  5768. // while exception is shown
  5769. NonVisualDataModule->StartBusy();
  5770. try
  5771. {
  5772. ShowExtendedExceptionEx(Terminal, E);
  5773. }
  5774. __finally
  5775. {
  5776. NonVisualDataModule->EndBusy();
  5777. FTrayIcon->CancelBalloon();
  5778. }
  5779. }
  5780. //---------------------------------------------------------------------------
  5781. void __fastcall TCustomScpExplorerForm::TerminalReady()
  5782. {
  5783. // cannot rely on active page being page for active terminal,
  5784. // as it can happen that active page is the "new session" page
  5785. // (e.g. when reconnecting active terminal, while login dialog
  5786. // invoked from "new session" page is modal)
  5787. int ActiveTerminalIndex = TTerminalManager::Instance()->ActiveTerminalIndex;
  5788. UpdateSessionTab(SessionsPageControl->Pages[ActiveTerminalIndex]);
  5789. }
  5790. //---------------------------------------------------------------------------
  5791. void __fastcall TCustomScpExplorerForm::InactiveTerminalException(
  5792. TTerminal * Terminal, Exception * E)
  5793. {
  5794. Notify(Terminal, L"", qtError, false, NULL, NULL, E);
  5795. if (!Terminal->Active)
  5796. {
  5797. int Index = TTerminalManager::Instance()->IndexOf(Terminal);
  5798. if (DebugAlwaysTrue((Index >= 0) && (Index < SessionsPageControl->PageCount)))
  5799. {
  5800. UpdateSessionTab(SessionsPageControl->Pages[Index]);
  5801. }
  5802. }
  5803. }
  5804. //---------------------------------------------------------------------------
  5805. void __fastcall TCustomScpExplorerForm::Notify(TTerminal * Terminal,
  5806. UnicodeString Message, TQueryType Type,
  5807. bool Important, TNotifyEvent OnClick, TObject * UserData, Exception * E)
  5808. {
  5809. if ((E == NULL) ||
  5810. ExceptionMessage(E, Message))
  5811. {
  5812. unsigned int Seconds = WinConfiguration->NotificationsTimeout;
  5813. if (Important)
  5814. {
  5815. Seconds *= 5;
  5816. }
  5817. UnicodeString NoteMessage(UnformatMessage(Message));
  5818. if (Terminal != NULL)
  5819. {
  5820. NoteMessage = FORMAT(L"%s: %s",
  5821. (TTerminalManager::Instance()->TerminalTitle(Terminal), NoteMessage));
  5822. }
  5823. if (WinConfiguration->BalloonNotifications)
  5824. {
  5825. AddNote(NoteMessage);
  5826. PopupTrayBalloon(Terminal, Message, Type, NULL, Seconds, OnClick, UserData);
  5827. }
  5828. else
  5829. {
  5830. FlashOnBackground();
  5831. PostNote(NoteMessage, Seconds, OnClick, UserData);
  5832. }
  5833. }
  5834. }
  5835. //---------------------------------------------------------------------------
  5836. void __fastcall TCustomScpExplorerForm::QueueEmptyNoteClicked(TObject * Sender)
  5837. {
  5838. RestoreApp();
  5839. TTerminalNoteData * TerminalNoteData = dynamic_cast<TTerminalNoteData *>(Sender);
  5840. if (DebugAlwaysTrue(TerminalNoteData != NULL) &&
  5841. !NonVisualDataModule->Busy)
  5842. {
  5843. TTerminal * Terminal = TerminalNoteData->Terminal;
  5844. TTerminalManager::Instance()->ActiveTerminal = Terminal;
  5845. if (!ComponentVisible[fcQueueView])
  5846. {
  5847. ToggleQueueVisibility();
  5848. GoToQueue();
  5849. }
  5850. }
  5851. }
  5852. //---------------------------------------------------------------------------
  5853. void __fastcall TCustomScpExplorerForm::QueueEvent(TTerminal * ATerminal,
  5854. TTerminalQueue * /*Queue*/, TQueueEvent Event)
  5855. {
  5856. TManagedTerminal * ManagedTerminal = DebugNotNull(dynamic_cast<TManagedTerminal *>(ATerminal));
  5857. UnicodeString Message;
  5858. TNotifyEvent OnClick = NULL;
  5859. TObject * UserData = NULL;
  5860. bool QueueInvisible = !ComponentVisible[fcQueueView] || IsApplicationMinimized();
  5861. switch (Event)
  5862. {
  5863. case qeEmptyButMonitored:
  5864. if ((ATerminal != Terminal) || QueueInvisible)
  5865. {
  5866. Message = LoadStr(BALLOON_QUEUE_EMPTY);
  5867. OnClick = QueueEmptyNoteClicked;
  5868. TTerminalNoteData * TerminalNoteData = new TTerminalNoteData();
  5869. TerminalNoteData->Terminal = ATerminal;
  5870. UserData = TerminalNoteData;
  5871. }
  5872. break;
  5873. case qeEmpty:
  5874. OperationComplete(ManagedTerminal->QueueOperationStart);
  5875. break;
  5876. case qePendingUserAction:
  5877. if ((ATerminal != Terminal) ||
  5878. (QueueInvisible && !IsQueueAutoPopup()))
  5879. {
  5880. Message = LoadStr(BALLOON_QUEUE_USER_ACTION);
  5881. }
  5882. break;
  5883. default:
  5884. DebugFail();
  5885. }
  5886. if (!Message.IsEmpty())
  5887. {
  5888. Notify(ATerminal, Message, qtInformation, false, OnClick, UserData);
  5889. }
  5890. }
  5891. //---------------------------------------------------------------------------
  5892. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDCreateDragFileList(
  5893. TObject * /*Sender*/, TFileList * FileList, bool & Created)
  5894. {
  5895. if (FDDExtMapFile != NULL)
  5896. {
  5897. CloseHandle(FDDExtMapFile);
  5898. FDDExtMapFile = NULL;
  5899. }
  5900. if (WinConfiguration->DDExtEnabled)
  5901. {
  5902. if (!WinConfiguration->DDExtInstalled)
  5903. {
  5904. Configuration->Usage->Inc(L"DownloadsDragDropExternalExtNotInstalled");
  5905. throw ExtException(NULL, LoadStr(DRAGEXT_TARGET_NOT_INSTALLED2), HELP_DRAGEXT_TARGET_NOT_INSTALLED);
  5906. }
  5907. DDExtInitDrag(FileList, Created);
  5908. }
  5909. }
  5910. //---------------------------------------------------------------------------
  5911. void __fastcall TCustomScpExplorerForm::DDExtInitDrag(TFileList * FileList,
  5912. bool & Created)
  5913. {
  5914. FDragExtFakeDirectory =
  5915. ExcludeTrailingBackslash(WinConfiguration->TemporaryDir());
  5916. if (!ForceDirectories(ApiPath(FDragExtFakeDirectory)))
  5917. {
  5918. throw Exception(FMTLOAD(CREATE_TEMP_DIR_ERROR, (FDragExtFakeDirectory)));
  5919. }
  5920. FileList->AddItem(NULL, FDragExtFakeDirectory);
  5921. Created = true;
  5922. FDDExtMapFile = CreateFileMappingA((HANDLE)0xFFFFFFFF, NULL, PAGE_READWRITE,
  5923. 0, sizeof(TDragExtCommStruct), AnsiString(DRAG_EXT_MAPPING).c_str());
  5924. {
  5925. TMutexGuard Guard(FDDExtMutex, DRAGEXT_MUTEX_RELEASE_TIMEOUT);
  5926. TDragExtCommStruct* CommStruct;
  5927. CommStruct = static_cast<TDragExtCommStruct*>(MapViewOfFile(FDDExtMapFile,
  5928. FILE_MAP_ALL_ACCESS, 0, 0, 0));
  5929. DebugAssert(CommStruct != NULL);
  5930. CommStruct->Version = TDragExtCommStruct::CurrentVersion;
  5931. CommStruct->Dragging = true;
  5932. wcsncpy(CommStruct->DropDest, FDragExtFakeDirectory.c_str(),
  5933. LENOF(CommStruct->DropDest));
  5934. NULL_TERMINATE(CommStruct->DropDest);
  5935. UnmapViewOfFile(CommStruct);
  5936. }
  5937. FDDMoveSlipped = false;
  5938. }
  5939. //---------------------------------------------------------------------------
  5940. bool __fastcall TCustomScpExplorerForm::RemoteFileControlFileOperation(
  5941. TObject * Sender, TFileOperation Operation, bool NoConfirmation, void * Param)
  5942. {
  5943. bool Result;
  5944. if (Sender == RemoteDirView)
  5945. {
  5946. Result = ExecuteFileOperation(Operation, osRemote, true, NoConfirmation, Param);
  5947. }
  5948. else
  5949. {
  5950. DebugAssert(Sender == RemoteDriveView);
  5951. TStrings * FileList = RemoteDriveView->DragFileList();
  5952. try
  5953. {
  5954. Result = ExecuteFileOperation(Operation, osRemote, FileList, NoConfirmation, Param);
  5955. }
  5956. __finally
  5957. {
  5958. delete FileList;
  5959. }
  5960. }
  5961. if (FDDTargetControl == RemoteDriveView)
  5962. {
  5963. RemoteDriveView->UpdateDropTarget();
  5964. }
  5965. // foRemoteMove happens when file/dir is dragged within the remote tree view
  5966. // or from tree view to dir view.
  5967. // foMove happens when file/dir is dragged from remote tree view outside of
  5968. // application via dragex
  5969. if (((Operation == foRemoteMove) || (Operation == foMove)) &&
  5970. (DropSourceControl == RemoteDriveView))
  5971. {
  5972. RemoteDriveView->UpdateDropSource();
  5973. }
  5974. return Result;
  5975. }
  5976. //---------------------------------------------------------------------------
  5977. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDEnd(TObject * Sender)
  5978. {
  5979. // This also handles drops of remote files to queue.
  5980. // Drops of local files (uploads) are handled in QueueDDProcessDropped.
  5981. SAFE_DESTROY(FDDFileList);
  5982. if ((FDDExtMapFile != NULL) || (FDDTargetControl == QueueView3))
  5983. {
  5984. try
  5985. {
  5986. TDragResult DDResult = (Sender == RemoteDirView) ?
  5987. RemoteDirView->LastDDResult : RemoteDriveView->LastDDResult;
  5988. // Focus is moved to the target application,
  5989. // but as we are going to present the UI, we need to steal the focus back.
  5990. // This most likely won't work though (windows does not allow application
  5991. // to steal focus most of the time)
  5992. Application->BringToFront();
  5993. // note that we seem to never get drMove here, see also comment below
  5994. if ((DDResult == drCopy) || (DDResult == drMove) || (DDResult == drInvalid))
  5995. {
  5996. UnicodeString TargetDirectory;
  5997. TFileOperation Operation;
  5998. // drInvalid may mean drMove, see comment below
  5999. Operation = (DDResult == drCopy) ? foCopy : foMove;
  6000. if (FDDMoveSlipped)
  6001. {
  6002. Operation = foMove;
  6003. }
  6004. TTransferOperationParam Param;
  6005. bool Internal;
  6006. bool ForceQueue;
  6007. if (!DDGetTarget(Param.TargetDirectory, ForceQueue, Internal))
  6008. {
  6009. // we get drInvalid both if move-d&d was intercepted by ddext,
  6010. // and when users drops on no-drop location.
  6011. // we tell the difference by existence of response from ddext,
  6012. // so we ignore absence of response in this case
  6013. if (DDResult != drInvalid)
  6014. {
  6015. // here we know that the extension is installed,
  6016. // as it is checked as soon as drag&drop starts from
  6017. // RemoteFileControlDDCreateDragFileList
  6018. Configuration->Usage->Inc(L"DownloadsDragDropExternalExtTargetUnknown");
  6019. throw ExtException(NULL, LoadStr(DRAGEXT_TARGET_UNKNOWN2), HELP_DRAGEXT_TARGET_UNKNOWN);
  6020. }
  6021. }
  6022. else
  6023. {
  6024. // download using ddext
  6025. Param.Temp = false;
  6026. Param.DragDrop = true;
  6027. if (ForceQueue)
  6028. {
  6029. Param.Queue = asOn;
  6030. }
  6031. if (Sender == RemoteDirView)
  6032. {
  6033. Param.Options = FLAGMASK(SelectedAllFilesInDirView(RemoteDirView), coAllFiles);
  6034. }
  6035. if (RemoteFileControlFileOperation(Sender, Operation,
  6036. (WinConfiguration->DDTransferConfirmation == asOff), &Param))
  6037. {
  6038. Configuration->Usage->Inc(
  6039. Internal ? L"DownloadsDragDropInternal" : L"DownloadsDragDropExternalExt");
  6040. }
  6041. }
  6042. }
  6043. }
  6044. __finally
  6045. {
  6046. CloseHandle(FDDExtMapFile);
  6047. FDDExtMapFile = NULL;
  6048. RemoveDir(ApiPath(FDragExtFakeDirectory));
  6049. FDragExtFakeDirectory = L"";
  6050. }
  6051. }
  6052. if (!FDragDropSshTerminate.IsEmpty())
  6053. {
  6054. throw ESshTerminate(NULL, FDragDropSshTerminate, FDragDropOnceDoneOperation);
  6055. }
  6056. }
  6057. //---------------------------------------------------------------------------
  6058. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDGiveFeedback(
  6059. TObject * Sender, int dwEffect, HRESULT & /*Result*/)
  6060. {
  6061. HCURSOR SlippedCopyCursor;
  6062. FDDMoveSlipped =
  6063. (FDragMoveCursor != NULL) &&
  6064. (!WinConfiguration->DDAllowMoveInit) && (dwEffect == DROPEFFECT_Copy) &&
  6065. ((IsFileControl(FDDTargetControl, osRemote) && (GetKeyState(VK_CONTROL) >= 0) &&
  6066. FTerminal->IsCapable[fcRemoteMove]) ||
  6067. (IsFileControl(FDDTargetControl, osLocal) && (GetKeyState(VK_SHIFT) < 0)));
  6068. SlippedCopyCursor = FDDMoveSlipped ? FDragMoveCursor : Dragdrop::DefaultCursor;
  6069. DragDropFiles(Sender)->CHCopy = SlippedCopyCursor;
  6070. DragDropFiles(Sender)->CHScrollCopy = SlippedCopyCursor;
  6071. // Remember drop effect so we know (when user drops files), if we copy or move
  6072. FLastDropEffect = dwEffect;
  6073. }
  6074. //---------------------------------------------------------------------------
  6075. bool __fastcall TCustomScpExplorerForm::DDGetTarget(
  6076. UnicodeString & Directory, bool & ForceQueue, bool & Internal)
  6077. {
  6078. bool Result;
  6079. if (FDDTargetControl == QueueView3)
  6080. {
  6081. Directory = DefaultDownloadTargetDirectory();
  6082. Result = true;
  6083. Internal = true;
  6084. ForceQueue = true;
  6085. }
  6086. else
  6087. {
  6088. Enabled = false;
  6089. try
  6090. {
  6091. int Timer = 0;
  6092. Result = false;
  6093. while (!Result && (Timer < WinConfiguration->DDExtTimeout))
  6094. {
  6095. {
  6096. TMutexGuard Guard(FDDExtMutex, DRAGEXT_MUTEX_RELEASE_TIMEOUT);
  6097. TDragExtCommStruct* CommStruct;
  6098. CommStruct = static_cast<TDragExtCommStruct*>(MapViewOfFile(FDDExtMapFile,
  6099. FILE_MAP_ALL_ACCESS, 0, 0, 0));
  6100. DebugAssert(CommStruct != NULL);
  6101. Result = !CommStruct->Dragging;
  6102. if (Result)
  6103. {
  6104. Directory = ExtractFilePath(CommStruct->DropDest);
  6105. Internal = false;
  6106. }
  6107. UnmapViewOfFile(CommStruct);
  6108. }
  6109. if (!Result)
  6110. {
  6111. Sleep(50);
  6112. Timer += 50;
  6113. Application->ProcessMessages();
  6114. }
  6115. }
  6116. }
  6117. __finally
  6118. {
  6119. Enabled = true;
  6120. }
  6121. }
  6122. return Result;
  6123. }
  6124. //---------------------------------------------------------------------------
  6125. void __fastcall TCustomScpExplorerForm::AddDelayedDirectoryDeletion(
  6126. const UnicodeString TempDir, int SecDelay)
  6127. {
  6128. TDateTime Alarm = Now() + (double)((double)SecDelay*OneMillisecond);
  6129. FDelayedDeletionList->AddObject(TempDir, reinterpret_cast<TObject*>(Alarm.FileDate()));
  6130. if (FDelayedDeletionTimer == NULL)
  6131. {
  6132. DebugAssert(HandleAllocated());
  6133. FDelayedDeletionTimer = new TTimer(this);
  6134. FDelayedDeletionTimer->Interval = 10000;
  6135. FDelayedDeletionTimer->OnTimer = DoDelayedDeletion;
  6136. }
  6137. else
  6138. {
  6139. FDelayedDeletionTimer->Enabled = true;
  6140. }
  6141. }
  6142. //---------------------------------------------------------------------------
  6143. void __fastcall TCustomScpExplorerForm::DoDelayedDeletion(TObject * Sender)
  6144. {
  6145. DebugAssert(FDelayedDeletionList != NULL);
  6146. TDateTime N = Now();
  6147. TDateTime Alert;
  6148. UnicodeString Directory;
  6149. for (int Index = FDelayedDeletionList->Count-1; Index >= 0; Index--)
  6150. {
  6151. Alert = FileDateToDateTime(reinterpret_cast<int>(FDelayedDeletionList->Objects[Index]));
  6152. if ((N >= Alert) || (Sender == NULL))
  6153. {
  6154. Directory = FDelayedDeletionList->Strings[Index];
  6155. if (DeleteDirectory(ExcludeTrailingBackslash(Directory)))
  6156. {
  6157. FDelayedDeletionList->Delete(Index);
  6158. }
  6159. }
  6160. }
  6161. if (FDelayedDeletionList->Count == 0)
  6162. {
  6163. FDelayedDeletionTimer->Enabled = false;
  6164. }
  6165. }
  6166. //---------------------------------------------------------------------------
  6167. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDTargetDrop()
  6168. {
  6169. if (IsFileControl(FDDTargetControl, osRemote) ||
  6170. (FDDTargetControl == SessionsPageControl))
  6171. {
  6172. TTerminal * TargetTerminal = NULL;
  6173. TFileOperation Operation = foNone;
  6174. if (FDDTargetControl == SessionsPageControl)
  6175. {
  6176. TPoint Point = SessionsPageControl->ScreenToClient(Mouse->CursorPos);
  6177. int Index = SessionsPageControl->IndexOfTabAt(Point.X, Point.Y);
  6178. // do not allow dropping on the "+" tab
  6179. TargetTerminal = GetSessionTabTerminal(SessionsPageControl->Pages[Index]);
  6180. if (TargetTerminal != NULL)
  6181. {
  6182. if ((FLastDropEffect == DROPEFFECT_MOVE) &&
  6183. (TargetTerminal == TTerminalManager::Instance()->ActiveTerminal))
  6184. {
  6185. Operation = foRemoteMove;
  6186. }
  6187. else
  6188. {
  6189. Operation = foRemoteCopy;
  6190. }
  6191. }
  6192. }
  6193. else
  6194. {
  6195. // when move from remote side is disabled, we allow copying inside the remote
  6196. // panel, but we interpret is as moving (we also slip in the move cursor)
  6197. if ((FLastDropEffect == DROPEFFECT_MOVE) ||
  6198. (!WinConfiguration->DDAllowMoveInit && (FLastDropEffect == DROPEFFECT_COPY) &&
  6199. FDDMoveSlipped))
  6200. {
  6201. Operation = foRemoteMove;
  6202. }
  6203. else if (FLastDropEffect == DROPEFFECT_COPY)
  6204. {
  6205. Operation = foRemoteCopy;
  6206. }
  6207. }
  6208. if (Operation != foNone)
  6209. {
  6210. RemoteFileControlFileOperation(DropSourceControl,
  6211. Operation, (WinConfiguration->DDTransferConfirmation == asOff), TargetTerminal);
  6212. }
  6213. // abort drag&drop
  6214. Abort();
  6215. }
  6216. else if ((FDDExtMapFile == NULL) && (FLastDropEffect != DROPEFFECT_NONE) &&
  6217. // Drops of remote files to queue are handled in RemoteFileControlDDEnd
  6218. (FDDTargetControl != QueueView3))
  6219. {
  6220. DebugAssert(!FDragTempDir.IsEmpty());
  6221. TTransferType Type;
  6222. UnicodeString TempDir = FDragTempDir;
  6223. // We clear FDragTempDir before calling
  6224. // just in case it fail (raises exception)
  6225. FDragTempDir = L"";
  6226. Type = (FLastDropEffect & DROPEFFECT_MOVE ? ttMove : Type = ttCopy);
  6227. TGUICopyParamType CopyParams = GUIConfiguration->CurrentCopyParam;
  6228. // empty directory parameter means temp directory -> don't display it!
  6229. UnicodeString TargetDir = L"";
  6230. int Options = 0;
  6231. if (!CopyParamDialog(tdToLocal, Type, true, FDDFileList,
  6232. TargetDir, CopyParams, (WinConfiguration->DDTransferConfirmation != asOff), true, Options))
  6233. {
  6234. Abort();
  6235. }
  6236. // TargetDir is set when dropped on local file control
  6237. // (this was workaround for legacy dirview event handling, now it should be
  6238. // made prettier)
  6239. if (TargetDir.IsEmpty())
  6240. {
  6241. TargetDir = TempDir;
  6242. if (ForceDirectories(ApiPath(TargetDir)))
  6243. {
  6244. DebugAssert(Terminal && !TargetDir.IsEmpty());
  6245. FPendingTempSpaceWarn = true;
  6246. try
  6247. {
  6248. FDragDropOperation = true;
  6249. int Params = cpTemporary |
  6250. (Type == ttMove ? cpDelete : 0);
  6251. DDDownload(FDDFileList, TargetDir, &CopyParams, Params);
  6252. Configuration->Usage->Inc(L"DownloadsDragDropExternalTemp");
  6253. }
  6254. __finally
  6255. {
  6256. FDragDropOperation = false;
  6257. FPendingTempSpaceWarn = false;
  6258. AddDelayedDirectoryDeletion(TargetDir, WinConfiguration->DDDeleteDelay);
  6259. }
  6260. }
  6261. else
  6262. {
  6263. throw Exception(FMTLOAD(CREATE_TEMP_DIR_ERROR, (TargetDir)));
  6264. }
  6265. }
  6266. }
  6267. }
  6268. //---------------------------------------------------------------------------
  6269. void __fastcall TCustomScpExplorerForm::DDDownload(TStrings * FilesToCopy,
  6270. const UnicodeString TargetDir, const TCopyParamType * CopyParam, int Params)
  6271. {
  6272. void * BatchStorage;
  6273. BatchStart(BatchStorage);
  6274. try
  6275. {
  6276. UpdateCopyParamCounters(*CopyParam);
  6277. Terminal->CopyToLocal(FilesToCopy, TargetDir, CopyParam, Params);
  6278. if (FLAGSET(Params, cpDelete) && (DropSourceControl == RemoteDriveView))
  6279. {
  6280. RemoteDriveView->UpdateDropSource();
  6281. }
  6282. }
  6283. __finally
  6284. {
  6285. BatchEnd(BatchStorage);
  6286. }
  6287. }
  6288. //---------------------------------------------------------------------------
  6289. class TFakeDataObjectFilesEx : public TDataObjectFilesEx
  6290. {
  6291. public:
  6292. __fastcall TFakeDataObjectFilesEx(TFileList * AFileList, bool RenderPIDL,
  6293. bool RenderFilename) : TDataObjectFilesEx(AFileList, RenderPIDL, RenderFilename)
  6294. {
  6295. }
  6296. virtual bool __fastcall AllowData(const tagFORMATETC & FormatEtc)
  6297. {
  6298. return (FormatEtc.cfFormat == CF_HDROP) ? false :
  6299. TDataObjectFilesEx::AllowData(FormatEtc);
  6300. }
  6301. };
  6302. //---------------------------------------------------------------------------
  6303. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDCreateDataObject(
  6304. TObject * Sender, TDataObject *& DataObject)
  6305. {
  6306. if (FDDExtMapFile != NULL)
  6307. {
  6308. TFileList * FileList = DragDropFiles(Sender)->FileList;
  6309. if (!FileList->RenderPIDLs() || !FileList->RenderNames())
  6310. {
  6311. Abort();
  6312. }
  6313. if (FileList->Count > 0)
  6314. {
  6315. TDataObjectFilesEx * FilesObject = new TFakeDataObjectFilesEx(FileList, true, true);
  6316. if (!FilesObject->IsValid(true, true))
  6317. {
  6318. FilesObject->_Release();
  6319. }
  6320. else
  6321. {
  6322. DataObject = FilesObject;
  6323. }
  6324. }
  6325. }
  6326. }
  6327. //---------------------------------------------------------------------------
  6328. void __fastcall TCustomScpExplorerForm::GoToCommandLine()
  6329. {
  6330. DebugFail();
  6331. }
  6332. //---------------------------------------------------------------------------
  6333. void __fastcall TCustomScpExplorerForm::GoToTree()
  6334. {
  6335. ComponentVisible[fcRemoteTree] = true;
  6336. RemoteDriveView->SetFocus();
  6337. }
  6338. //---------------------------------------------------------------------------
  6339. TStrings * __fastcall TCustomScpExplorerForm::PanelExport(TOperationSide Side,
  6340. TPanelExport Export)
  6341. {
  6342. TCustomDirView * DirView = this->DirView(Side);
  6343. std::unique_ptr<TStrings> ExportData(new TStringList());
  6344. switch (Export)
  6345. {
  6346. case pePath:
  6347. ExportData->Add(DirView->PathName);
  6348. break;
  6349. case peFileList:
  6350. case peFullFileList:
  6351. {
  6352. bool FullPath = (Export == peFullFileList);
  6353. DirView->CreateFileList(false, FullPath, ExportData.get());
  6354. for (int Index = 0; Index < ExportData->Count; Index++)
  6355. {
  6356. if (ExportData->Strings[Index].Pos(L" ") > 0)
  6357. {
  6358. ExportData->Strings[Index] = FORMAT(L"\"%s\"", (ExportData->Strings[Index]));
  6359. }
  6360. }
  6361. }
  6362. break;
  6363. default:
  6364. DebugFail();
  6365. }
  6366. return ExportData.release();
  6367. }
  6368. //---------------------------------------------------------------------------
  6369. void __fastcall TCustomScpExplorerForm::PanelExport(TOperationSide Side,
  6370. TPanelExport Export, TPanelExportDestination Destination)
  6371. {
  6372. std::unique_ptr<TStrings> ExportData(PanelExport(Side, Export));
  6373. PanelExportStore(Side, Export, Destination, ExportData.get());
  6374. }
  6375. //---------------------------------------------------------------------------
  6376. void __fastcall TCustomScpExplorerForm::PanelExportStore(TOperationSide /*Side*/,
  6377. TPanelExport /*Export*/, TPanelExportDestination Destination,
  6378. TStrings * ExportData)
  6379. {
  6380. if (Destination == pedClipboard)
  6381. {
  6382. TInstantOperationVisualizer Visualizer;
  6383. CopyToClipboard(ExportData);
  6384. }
  6385. else
  6386. {
  6387. DebugFail();
  6388. }
  6389. }
  6390. //---------------------------------------------------------------------------
  6391. void __fastcall TCustomScpExplorerForm::Filter(TOperationSide Side)
  6392. {
  6393. TCustomDirView * DirView = this->DirView(Side);
  6394. TFileFilter Filter;
  6395. DefaultFileFilter(Filter);
  6396. Filter.Masks = DirView->Mask;
  6397. if (DoFilterMaskDialog(DirView, &Filter))
  6398. {
  6399. DirView->Mask = TFileMasks::NormalizeMask(Filter.Masks);
  6400. }
  6401. }
  6402. //---------------------------------------------------------------------------
  6403. TQueueOperation __fastcall TCustomScpExplorerForm::DefaultQueueOperation()
  6404. {
  6405. return FQueueController->DefaultOperation();
  6406. }
  6407. //---------------------------------------------------------------------------
  6408. bool __fastcall TCustomScpExplorerForm::AllowQueueOperation(
  6409. TQueueOperation Operation, void ** Param)
  6410. {
  6411. switch (Operation)
  6412. {
  6413. case qoPreferences:
  6414. return true;
  6415. case qoGoTo:
  6416. return ComponentVisible[fcQueueView] && QueueView3->Enabled;
  6417. case qoOnceEmpty:
  6418. return !FQueueController->Empty;
  6419. default:
  6420. return FQueueController->AllowOperation(Operation, Param);
  6421. }
  6422. }
  6423. //---------------------------------------------------------------------------
  6424. void __fastcall TCustomScpExplorerForm::GoToQueue()
  6425. {
  6426. if (DebugAlwaysTrue(QueueView3->Visible))
  6427. {
  6428. QueueView3->SetFocus();
  6429. }
  6430. }
  6431. //---------------------------------------------------------------------------
  6432. void __fastcall TCustomScpExplorerForm::ExecuteQueueOperation(
  6433. TQueueOperation Operation, void * Param)
  6434. {
  6435. if (Operation == qoGoTo)
  6436. {
  6437. GoToQueue();
  6438. }
  6439. else if (Operation == qoPreferences)
  6440. {
  6441. PreferencesDialog(pmQueue);
  6442. }
  6443. else
  6444. {
  6445. FQueueController->ExecuteOperation(Operation, Param);
  6446. }
  6447. }
  6448. //---------------------------------------------------------------------------
  6449. bool __fastcall TCustomScpExplorerForm::GetQueueEnabled()
  6450. {
  6451. return (Queue != NULL) && Queue->Enabled;
  6452. }
  6453. //---------------------------------------------------------------------------
  6454. void __fastcall TCustomScpExplorerForm::ToggleQueueEnabled()
  6455. {
  6456. DebugAssert(Queue != NULL);
  6457. if (Queue != NULL)
  6458. {
  6459. Queue->Enabled = !Queue->Enabled;
  6460. }
  6461. }
  6462. //---------------------------------------------------------------------------
  6463. void __fastcall TCustomScpExplorerForm::QueueView3ContextPopup(
  6464. TObject * /*Sender*/, TPoint & /*MousePos*/, bool & /*Handled*/)
  6465. {
  6466. FQueueActedItem = QueueView3->ItemFocused;
  6467. }
  6468. //---------------------------------------------------------------------------
  6469. /*virtual*/ int __fastcall TCustomScpExplorerForm::GetStaticComponentsHeight()
  6470. {
  6471. return TopDock->Height + QueueSplitter->Height;
  6472. }
  6473. //---------------------------------------------------------------------------
  6474. void __fastcall TCustomScpExplorerForm::QueueSplitterCanResize(
  6475. TObject * /*Sender*/, int & NewSize, bool & Accept)
  6476. {
  6477. // when queue is hidden by double-clicking splitter, stray attempt to
  6478. // resize the panel with strange value arrives, make sure it is ignored
  6479. if (ComponentVisible[fcQueueView])
  6480. {
  6481. int HeightLimit = ClientHeight - GetStaticComponentsHeight() -
  6482. RemotePanel->Constraints->MinHeight;
  6483. if (NewSize > HeightLimit)
  6484. {
  6485. NewSize = HeightLimit;
  6486. }
  6487. }
  6488. else
  6489. {
  6490. Accept = false;
  6491. }
  6492. }
  6493. //---------------------------------------------------------------------------
  6494. void __fastcall TCustomScpExplorerForm::QueueView3StartDrag(TObject * /*Sender*/,
  6495. TDragObject *& /*DragObject*/)
  6496. {
  6497. FQueueActedItem = QueueView3->ItemFocused;
  6498. }
  6499. //---------------------------------------------------------------------------
  6500. void __fastcall TCustomScpExplorerForm::QueueView3DragOver(TObject * /*Sender*/,
  6501. TObject * Source, int X, int Y, TDragState /*State*/, bool & Accept)
  6502. {
  6503. Accept = true;
  6504. if (Source == QueueView3)
  6505. {
  6506. TListItem * DropTarget = QueueView3->GetItemAt(X, Y);
  6507. Accept = (DropTarget != NULL) && (FQueueActedItem != NULL);
  6508. if (Accept)
  6509. {
  6510. TQueueItemProxy * QueueItem;
  6511. TQueueItemProxy * DestQueueItem;
  6512. QueueItem = static_cast<TQueueItemProxy *>(FQueueActedItem->Data);
  6513. DestQueueItem = static_cast<TQueueItemProxy *>(DropTarget->Data);
  6514. Accept = (QueueItem != DestQueueItem) &&
  6515. (QueueItem->Status == TQueueItem::qsPending) &&
  6516. (DestQueueItem->Status == TQueueItem::qsPending);
  6517. }
  6518. }
  6519. }
  6520. //---------------------------------------------------------------------------
  6521. void __fastcall TCustomScpExplorerForm::QueueView3DragDrop(TObject * /*Sender*/,
  6522. TObject * /*Source*/, int /*X*/, int /*Y*/)
  6523. {
  6524. if ((FQueueActedItem != NULL) && (QueueView3->DropTarget != NULL))
  6525. {
  6526. TQueueItemProxy * QueueItem;
  6527. TQueueItemProxy * DestQueueItem;
  6528. QueueItem = static_cast<TQueueItemProxy *>(FQueueActedItem->Data);
  6529. DestQueueItem = static_cast<TQueueItemProxy *>(QueueView3->DropTarget->Data);
  6530. QueueItem->Move(DestQueueItem);
  6531. }
  6532. }
  6533. //---------------------------------------------------------------------------
  6534. void __fastcall TCustomScpExplorerForm::QueueView3Enter(TObject * /*Sender*/)
  6535. {
  6536. if ((QueueView3->ItemFocused == NULL) &&
  6537. (QueueView3->Items->Count > 0))
  6538. {
  6539. QueueView3->ItemFocused = QueueView3->Items->Item[0];
  6540. }
  6541. QueueLabelUpdateStatus();
  6542. }
  6543. //---------------------------------------------------------------------------
  6544. void __fastcall TCustomScpExplorerForm::QueueView3Exit(TObject * /*Sender*/)
  6545. {
  6546. QueueLabelUpdateStatus();
  6547. }
  6548. //---------------------------------------------------------------------------
  6549. void __fastcall TCustomScpExplorerForm::QueueLabelUpdateStatus()
  6550. {
  6551. QueueLabel->UpdateStatus();
  6552. }
  6553. //---------------------------------------------------------------------------
  6554. void __fastcall TCustomScpExplorerForm::QueueView3SelectItem(
  6555. TObject * /*Sender*/, TListItem * /*Item*/, bool Selected)
  6556. {
  6557. if (Selected)
  6558. {
  6559. NonVisualDataModule->UpdateNonVisibleActions();
  6560. }
  6561. }
  6562. //---------------------------------------------------------------------------
  6563. TDragDropFilesEx * __fastcall TCustomScpExplorerForm::DragDropFiles(TObject * Sender)
  6564. {
  6565. TDragDropFilesEx * Result = NULL;
  6566. if (Sender == SessionsPageControl)
  6567. {
  6568. Result = FSessionsDragDropFilesEx;
  6569. }
  6570. else if (Sender == QueueView3)
  6571. {
  6572. Result = FQueueDragDropFilesEx;
  6573. }
  6574. else
  6575. {
  6576. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  6577. if (DirView != NULL)
  6578. {
  6579. Result = DirView->DragDropFilesEx;
  6580. }
  6581. else
  6582. {
  6583. TCustomDriveView * DriveView = dynamic_cast<TCustomDriveView *>(Sender);
  6584. if (DriveView != NULL)
  6585. {
  6586. Result = DriveView->DragDropFilesEx;
  6587. }
  6588. }
  6589. }
  6590. DebugAssert(Result != NULL);
  6591. return Result;
  6592. }
  6593. //---------------------------------------------------------------------------
  6594. bool __fastcall TCustomScpExplorerForm::SelectedAllFilesInDirView(TCustomDirView * DView)
  6595. {
  6596. return (DView->SelCount == DView->FilesCount);
  6597. }
  6598. //---------------------------------------------------------------------------
  6599. bool __fastcall TCustomScpExplorerForm::DraggingAllFilesFromDirView(TOperationSide Side, TStrings * FileList)
  6600. {
  6601. return HasDirView[Side] && (DropSourceControl == DirView(Side)) && (FileList->Count == DirView(Side)->FilesCount);
  6602. }
  6603. //---------------------------------------------------------------------------
  6604. void __fastcall TCustomScpExplorerForm::RemoteFileControlDragDropFileOperation(
  6605. TObject * Sender, int Effect, UnicodeString TargetPath, bool ForceQueue)
  6606. {
  6607. TFileOperation Operation;
  6608. switch (Effect)
  6609. {
  6610. case DROPEFFECT_MOVE:
  6611. Operation = foMove;
  6612. break;
  6613. case DROPEFFECT_COPY:
  6614. // occures on WinXP (reported by user)
  6615. default:
  6616. Operation = foCopy;
  6617. break;
  6618. };
  6619. TDragDropFilesEx * DragDropFilesEx = DragDropFiles(Sender);
  6620. // see a comment in TUnixDirView::PerformItemDragDropOperation
  6621. if (DragDropFilesEx->FileList->Count > 0)
  6622. {
  6623. TStrings * FileList = new TStringList();
  6624. try
  6625. {
  6626. for (int Index = 0; Index < DragDropFilesEx->FileList->Count; Index++)
  6627. {
  6628. FileList->Add(DragDropFilesEx->FileList->Items[Index]->Name);
  6629. }
  6630. FDragDropOperation = true;
  6631. TTransferOperationParam Param;
  6632. Param.TargetDirectory = TargetPath;
  6633. // upload, no temp dirs
  6634. Param.Temp = false;
  6635. Param.DragDrop = true;
  6636. Param.Options =
  6637. FLAGMASK(DraggingAllFilesFromDirView(osLocal, FileList), coAllFiles);
  6638. if (ForceQueue)
  6639. {
  6640. Param.Queue = asOn;
  6641. }
  6642. if (ExecuteFileOperation(Operation, osLocal, FileList,
  6643. (WinConfiguration->DDTransferConfirmation == asOff), &Param))
  6644. {
  6645. if (IsFileControl(DropSourceControl, osLocal))
  6646. {
  6647. Configuration->Usage->Inc(L"UploadsDragDropInternal");
  6648. }
  6649. else
  6650. {
  6651. Configuration->Usage->Inc(L"UploadsDragDropExternal");
  6652. }
  6653. }
  6654. }
  6655. __finally
  6656. {
  6657. FDragDropOperation = false;
  6658. delete FileList;
  6659. }
  6660. }
  6661. }
  6662. //---------------------------------------------------------------------------
  6663. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDFileOperation(
  6664. TObject * Sender, int Effect, UnicodeString /*SourcePath*/,
  6665. UnicodeString TargetPath, bool & /*DoOperation*/)
  6666. {
  6667. RemoteFileControlDragDropFileOperation(Sender, Effect, TargetPath, false);
  6668. }
  6669. //---------------------------------------------------------------------------
  6670. void __fastcall TCustomScpExplorerForm::RemoteFileContolDDChooseEffect(
  6671. TObject * Sender, int grfKeyState, int & dwEffect)
  6672. {
  6673. // if any drop effect is allowed at all (e.g. no drop to self and drop to parent)
  6674. if ((dwEffect != DROPEFFECT_None) &&
  6675. IsFileControl(DropSourceControl, osRemote))
  6676. {
  6677. // do not allow drop on remote panel (on free space, still allow drop on directories)
  6678. if ((Sender == RemoteDirView) && (DropSourceControl == RemoteDirView) &&
  6679. (RemoteDirView->DropTarget == NULL))
  6680. {
  6681. dwEffect = DROPEFFECT_None;
  6682. }
  6683. else
  6684. {
  6685. if (dwEffect == DROPEFFECT_Copy)
  6686. {
  6687. bool MoveCapable = FTerminal->IsCapable[fcRemoteMove];
  6688. // currently we support copying always (at least via temporary directory);
  6689. // remove associated checks once this all proves stable and working
  6690. bool CopyCapable = true;
  6691. // if we do not support neither of operations, there's no discussion
  6692. if (!MoveCapable && !CopyCapable)
  6693. {
  6694. dwEffect = DROPEFFECT_None;
  6695. }
  6696. // when moving is disabled, we need to keep effect to "copy",
  6697. // which will be later interpretted as move (with slipped-in cursor)
  6698. else if (!WinConfiguration->DDAllowMoveInit && FLAGCLEAR(grfKeyState, MK_CONTROL))
  6699. {
  6700. // no-op, keep copy
  6701. }
  6702. else
  6703. {
  6704. // The default effect inside remote panel is move,
  6705. // unless we do not support it, but support copy
  6706. if (FLAGCLEAR(grfKeyState, MK_CONTROL))
  6707. {
  6708. dwEffect = MoveCapable ? DROPEFFECT_Move : DROPEFFECT_Copy;
  6709. }
  6710. else
  6711. {
  6712. // with ctrl-down, we want copy unless it is not supported
  6713. dwEffect = CopyCapable ? DROPEFFECT_Copy : DROPEFFECT_None;
  6714. }
  6715. }
  6716. }
  6717. }
  6718. }
  6719. }
  6720. //---------------------------------------------------------------------------
  6721. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDDragFileName(
  6722. TObject * Sender, TRemoteFile * File, UnicodeString & FileName)
  6723. {
  6724. if (FDDTotalSize >= 0)
  6725. {
  6726. if (File->IsDirectory)
  6727. {
  6728. FDDTotalSize = -1;
  6729. }
  6730. else
  6731. {
  6732. FDDTotalSize += File->Size;
  6733. }
  6734. }
  6735. DebugAssert(!FDragTempDir.IsEmpty());
  6736. // TODO: this is quite ineffective
  6737. // TODO: what if invalid character replacement is disabled?
  6738. FileName = FDragTempDir + GUIConfiguration->CurrentCopyParam.ValidLocalFileName(File->FileName);
  6739. UnicodeString TransferFileName;
  6740. if (Sender == RemoteDriveView)
  6741. {
  6742. TransferFileName = UnixExcludeTrailingBackslash(File->FullFileName);
  6743. }
  6744. else
  6745. {
  6746. TransferFileName = File->FileName;
  6747. }
  6748. FDDFileList->AddObject(TransferFileName, File);
  6749. }
  6750. //---------------------------------------------------------------------------
  6751. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDDragDetect(
  6752. TObject * /*Sender*/, int /*grfKeyState*/, const TPoint & /*DetectStart*/,
  6753. const TPoint & /*Point*/, TDragDetectStatus /*DragStatus*/)
  6754. {
  6755. // sometimes we do not get DDEnd so the list is not released
  6756. SAFE_DESTROY(FDDFileList);
  6757. FDDFileList = new TStringList();
  6758. FDragTempDir = WinConfiguration->TemporaryDir();
  6759. FDDTotalSize = 0;
  6760. FDragDropSshTerminate = L"";
  6761. FDragDropOnceDoneOperation = odoIdle;
  6762. }
  6763. //---------------------------------------------------------------------------
  6764. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDQueryContinueDrag(
  6765. TObject * /*Sender*/, BOOL /*FEscapePressed*/, int /*grfKeyState*/,
  6766. HRESULT & Result)
  6767. {
  6768. if (Result == DRAGDROP_S_DROP)
  6769. {
  6770. try
  6771. {
  6772. GlobalDragImageList->HideDragImage();
  6773. try
  6774. {
  6775. RemoteFileControlDDTargetDrop();
  6776. }
  6777. catch(ESshTerminate & E)
  6778. {
  6779. DebugAssert(E.MoreMessages == NULL); // not supported
  6780. DebugAssert(!E.Message.IsEmpty());
  6781. FDragDropSshTerminate = E.Message;
  6782. FDragDropOnceDoneOperation = E.Operation;
  6783. }
  6784. }
  6785. catch (Exception &E)
  6786. {
  6787. // If downloading fails we need to cancel drag&drop, otherwise
  6788. // Explorer shows error
  6789. // But by the way exception probably never reach this point as
  6790. // it's catched on way
  6791. Result = DRAGDROP_S_CANCEL;
  6792. DebugAssert(Terminal != NULL);
  6793. Terminal->ShowExtendedException(&E);
  6794. }
  6795. }
  6796. }
  6797. //---------------------------------------------------------------------------
  6798. void __fastcall TCustomScpExplorerForm::DirViewMatchMask(
  6799. TObject * /*Sender*/, UnicodeString FileName, bool Directory, __int64 Size,
  6800. TDateTime Modification, UnicodeString Masks, bool & Matches, bool AllowImplicitMatches)
  6801. {
  6802. TFileMasks::TParams MaskParams;
  6803. MaskParams.Size = Size;
  6804. MaskParams.Modification = Modification;
  6805. // this does not re-parse the mask if it is the same as the last time
  6806. FDirViewMatchMask = Masks;
  6807. bool ImplicitMatch;
  6808. Matches =
  6809. // RecurseInclude parameter has no effect as the Path is empty
  6810. FDirViewMatchMask.Matches(FileName, Directory, UnicodeString(L""), &MaskParams, true, ImplicitMatch) &&
  6811. (AllowImplicitMatches || !ImplicitMatch);
  6812. }
  6813. //---------------------------------------------------------------------------
  6814. void __fastcall TCustomScpExplorerForm::DirViewGetOverlay(
  6815. TObject * Sender, TListItem * Item, WORD & Indexes)
  6816. {
  6817. TCustomDirView * DirView = reinterpret_cast<TCustomDirView *>(Sender);
  6818. UnicodeString Ext;
  6819. if (DirView == RemoteDirView)
  6820. {
  6821. Ext = UnixExtractFileExt(DirView->ItemFileName(Item));
  6822. }
  6823. else
  6824. {
  6825. Ext = ExtractFileExt(DirView->ItemFileName(Item));
  6826. }
  6827. if (AnsiSameText(Ext, Configuration->PartialExt))
  6828. {
  6829. Indexes |= oiPartial;
  6830. }
  6831. }
  6832. //---------------------------------------------------------------------------
  6833. bool __fastcall TCustomScpExplorerForm::CanPasteToDirViewFromClipBoard()
  6834. {
  6835. return
  6836. DirViewEnabled(osCurrent) &&
  6837. DirView(osCurrent)->CanPasteFromClipBoard();
  6838. }
  6839. //---------------------------------------------------------------------------
  6840. bool __fastcall TCustomScpExplorerForm::CanPasteFromClipBoard()
  6841. {
  6842. bool Result = false;
  6843. if (CanPasteToDirViewFromClipBoard())
  6844. {
  6845. Result = true;
  6846. }
  6847. else
  6848. {
  6849. UnicodeString ClipboardText;
  6850. if (NonEmptyTextFromClipboard(ClipboardText))
  6851. {
  6852. if (StoredSessions->IsUrl(ClipboardText))
  6853. {
  6854. Result = true;
  6855. }
  6856. else
  6857. {
  6858. Result = DirViewEnabled(osCurrent);
  6859. }
  6860. }
  6861. }
  6862. return Result;
  6863. }
  6864. //---------------------------------------------------------------------------
  6865. void __fastcall TCustomScpExplorerForm::PasteFromClipBoard()
  6866. {
  6867. if (CanPasteToDirViewFromClipBoard())
  6868. {
  6869. DirView(osCurrent)->PasteFromClipBoard();
  6870. }
  6871. else
  6872. {
  6873. UnicodeString ClipboardText;
  6874. if (NonEmptyTextFromClipboard(ClipboardText))
  6875. {
  6876. if (StoredSessions->IsUrl(ClipboardText))
  6877. {
  6878. NewSession(false, ClipboardText);
  6879. }
  6880. else
  6881. {
  6882. DirView(osCurrent)->Path = ClipboardText;
  6883. }
  6884. }
  6885. }
  6886. }
  6887. //---------------------------------------------------------------------------
  6888. void __fastcall TCustomScpExplorerForm::FileListFromClipboard()
  6889. {
  6890. // TBD
  6891. }
  6892. //---------------------------------------------------------------------------
  6893. void __fastcall TCustomScpExplorerForm::SelectSameExt(bool Select)
  6894. {
  6895. TCustomDirView * CurrentDirView = DirView(osCurrent);
  6896. if (DebugAlwaysTrue(CurrentDirView->ItemFocused != NULL))
  6897. {
  6898. UnicodeString FileName = CurrentDirView->ItemFileName(CurrentDirView->ItemFocused);
  6899. UnicodeString Ext;
  6900. if (GetSide(osCurrent) == osRemote)
  6901. {
  6902. Ext = UnixExtractFileExt(FileName);
  6903. }
  6904. else
  6905. {
  6906. Ext = ExtractFileExt(FileName);
  6907. }
  6908. if (Ext.IsEmpty())
  6909. {
  6910. Ext = L".";
  6911. }
  6912. TFileFilter Filter;
  6913. Filter.Masks = FORMAT(L"*%s", (Ext));
  6914. Filter.Directories = false;
  6915. CurrentDirView->SelectFiles(Filter, Select);
  6916. }
  6917. }
  6918. //---------------------------------------------------------------------------
  6919. UnicodeString __fastcall TCustomScpExplorerForm::FileStatusBarText(
  6920. const TStatusFileInfo & FileInfo, TOperationSide Side)
  6921. {
  6922. UnicodeString Result;
  6923. if ((Side == osRemote) && (Terminal == NULL))
  6924. {
  6925. // noop
  6926. }
  6927. else
  6928. {
  6929. Result =
  6930. FMTLOAD(FILE_INFO_FORMAT,
  6931. (FormatBytes(FileInfo.SelectedSize),
  6932. FormatBytes(FileInfo.FilesSize),
  6933. FormatNumber(FileInfo.SelectedCount),
  6934. FormatNumber(FileInfo.FilesCount)));
  6935. }
  6936. return Result;
  6937. }
  6938. //---------------------------------------------------------------------------
  6939. void __fastcall TCustomScpExplorerForm::FileStatusBarPanelClick(
  6940. TTBXStatusPanel * Panel, TOperationSide Side)
  6941. {
  6942. if (Panel->Index == 1)
  6943. {
  6944. ToggleShowHiddenFiles();
  6945. }
  6946. else if (Panel->Index == 2)
  6947. {
  6948. Filter(Side);
  6949. }
  6950. }
  6951. //---------------------------------------------------------------------------
  6952. void __fastcall TCustomScpExplorerForm::UpdateFileStatusBar(
  6953. TTBXStatusBar * StatusBar, const TStatusFileInfo & FileInfo, TOperationSide Side)
  6954. {
  6955. DebugAssert(!StatusBar->SimplePanel);
  6956. StatusBar->Panels->Items[0]->Caption = FileStatusBarText(FileInfo, Side);
  6957. UpdateFileStatusExtendedPanels(StatusBar, FileInfo);
  6958. }
  6959. //---------------------------------------------------------------------------
  6960. void __fastcall TCustomScpExplorerForm::UpdateFileStatusExtendedPanels(
  6961. TTBXStatusBar * StatusBar, const TStatusFileInfo & FileInfo)
  6962. {
  6963. DebugAssert(StatusBar->Panels->Count >= 3);
  6964. TTBXStatusPanel * HiddenFilesPanel = StatusBar->Panels->Items[1];
  6965. if (FileInfo.HiddenCount > 0)
  6966. {
  6967. HiddenFilesPanel->Caption = FMTLOAD(FILE_INFO_HIDDEN2, (FormatNumber(FileInfo.HiddenCount)));
  6968. HiddenFilesPanel->ViewPriority = 90; // <100 allows hiding panel when it does not fit
  6969. }
  6970. else
  6971. {
  6972. HiddenFilesPanel->ViewPriority = 0;
  6973. // not really necessary, just to cleanup no-longer-valid data
  6974. HiddenFilesPanel->Caption = L"";
  6975. }
  6976. TTBXStatusPanel * FilteredFilesPanel = StatusBar->Panels->Items[2];
  6977. if (FileInfo.FilteredCount > 0)
  6978. {
  6979. FilteredFilesPanel->Caption = FMTLOAD(FILE_INFO_FILTERED2, (FormatNumber(FileInfo.FilteredCount)));
  6980. FilteredFilesPanel->ViewPriority = 90; // <100 allows hiding panel when it does not fit
  6981. }
  6982. else
  6983. {
  6984. FilteredFilesPanel->ViewPriority = 0;
  6985. // not really necessary, just to cleanup no-longer-valid data
  6986. FilteredFilesPanel->Caption = L"";
  6987. }
  6988. }
  6989. //---------------------------------------------------------------------------
  6990. void __fastcall TCustomScpExplorerForm::RemoteStatusBarClick(
  6991. TObject * /*Sender*/)
  6992. {
  6993. if (RemoteDirView->Enabled)
  6994. {
  6995. RemoteDirView->SetFocus();
  6996. }
  6997. }
  6998. //---------------------------------------------------------------------------
  6999. void __fastcall TCustomScpExplorerForm::ToggleQueueVisibility()
  7000. {
  7001. TQueueViewConfiguration Config = WinConfiguration->QueueView;
  7002. switch (Config.Show)
  7003. {
  7004. case qvShow:
  7005. if ((FQueueStatus != NULL) && (FQueueStatus->Count > 0))
  7006. {
  7007. Config.Show = qvHide;
  7008. }
  7009. else
  7010. {
  7011. Config.Show = Config.LastHideShow;
  7012. }
  7013. break;
  7014. case qvHideWhenEmpty:
  7015. if (ComponentVisible[fcQueueView])
  7016. {
  7017. Config.Show = qvHide;
  7018. }
  7019. else
  7020. {
  7021. Config.LastHideShow = Config.Show;
  7022. Config.Show = qvShow;
  7023. }
  7024. break;
  7025. case qvHide:
  7026. Config.LastHideShow = Config.Show;
  7027. Config.Show = qvShow;
  7028. break;
  7029. }
  7030. WinConfiguration->QueueView = Config;
  7031. }
  7032. //---------------------------------------------------------------------------
  7033. UnicodeString __fastcall TCustomScpExplorerForm::PathForCaption()
  7034. {
  7035. UnicodeString Result;
  7036. if (FTerminal != NULL)
  7037. {
  7038. switch (WinConfiguration->PathInCaption)
  7039. {
  7040. case picShort:
  7041. {
  7042. Result = UnixExtractFileName(FTerminal->CurrentDirectory);
  7043. if (Result.IsEmpty())
  7044. {
  7045. Result = FTerminal->CurrentDirectory;
  7046. }
  7047. }
  7048. break;
  7049. case picFull:
  7050. Result = FTerminal->CurrentDirectory;
  7051. break;
  7052. }
  7053. }
  7054. return Result;
  7055. }
  7056. //---------------------------------------------------------------------------
  7057. void __fastcall TCustomScpExplorerForm::UpdateControls()
  7058. {
  7059. TTerminalManager::Instance()->UpdateAppTitle();
  7060. // WORAKRDOUND: Disabling list view when it is not showing yet does not set its
  7061. // background to gray on Windows 7 (works on Windows 10).
  7062. // See also EnableControl
  7063. if (Showing)
  7064. {
  7065. bool HasTerminal = (Terminal != NULL);
  7066. if (HasTerminal)
  7067. {
  7068. if (!RemoteDirView->Enabled)
  7069. {
  7070. RemoteDirView->Enabled = true;
  7071. if (FRemoteDirViewWasFocused)
  7072. {
  7073. ActiveControl = RemoteDirView;
  7074. }
  7075. }
  7076. RemoteDriveView->Enabled = true;
  7077. RemoteDirView->Color = (FSessionColor != 0 ? FSessionColor : clWindow);
  7078. RemoteDriveView->Color = RemoteDirView->Color;
  7079. }
  7080. else
  7081. {
  7082. if (RemoteDirView->Enabled)
  7083. {
  7084. // This is first called when the form is being constructed
  7085. // (not anymore due to Showing test above)
  7086. // but the false is overriden in the constructor later.
  7087. // An even later in TScpCommanderForm::DoShow()
  7088. FRemoteDirViewWasFocused = (ActiveControl == RemoteDirView);
  7089. EnableControl(RemoteDirView, false);
  7090. }
  7091. EnableControl(RemoteDriveView, false);
  7092. }
  7093. EnableControl(QueueView3, HasTerminal);
  7094. QueueLabelUpdateStatus();
  7095. reinterpret_cast<TTBCustomItem *>(GetComponent(fcRemotePathComboBox))->Enabled = HasTerminal;
  7096. }
  7097. }
  7098. //---------------------------------------------------------------------------
  7099. void __fastcall TCustomScpExplorerForm::DoDirViewLoaded(TCustomDirView * /*Sender*/)
  7100. {
  7101. UpdateControls();
  7102. }
  7103. //---------------------------------------------------------------------------
  7104. void __fastcall TCustomScpExplorerForm::DirViewLoaded(
  7105. TObject * Sender)
  7106. {
  7107. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  7108. DebugAssert(DirView != NULL);
  7109. DoDirViewLoaded(DirView);
  7110. TransferPresetAutoSelect();
  7111. }
  7112. //---------------------------------------------------------------------------
  7113. void __fastcall TCustomScpExplorerForm::StartUpdates()
  7114. {
  7115. TUpdatesConfiguration Updates = WinConfiguration->Updates;
  7116. // first run after installation
  7117. if (double(Updates.LastCheck) == 0)
  7118. {
  7119. // make sure next time there will be an update (if enabled)
  7120. Updates.LastCheck = TDateTime(1);
  7121. WinConfiguration->Updates = Updates;
  7122. }
  7123. else if ((double(Updates.Period) > 0) &&
  7124. (Now() - Updates.LastCheck >= Updates.Period))
  7125. {
  7126. StartUpdateThread(UpdatesChecked);
  7127. }
  7128. }
  7129. //---------------------------------------------------------------------------
  7130. void __fastcall TCustomScpExplorerForm::UpdatesChecked()
  7131. {
  7132. UnicodeString Message;
  7133. bool New;
  7134. TQueryType Type;
  7135. GetUpdatesMessage(Message, New, Type, false);
  7136. if (!Message.IsEmpty())
  7137. {
  7138. if (New)
  7139. {
  7140. Message = FMTLOAD(NEW_VERSION_CLICK, (Message));
  7141. }
  7142. if (!New && (Type != qtWarning))
  7143. {
  7144. PostNote(UnformatMessage(Message), 0, UpdatesNoteClicked, NULL);
  7145. }
  7146. else
  7147. {
  7148. Notify(NULL, Message, Type, true, UpdatesNoteClicked);
  7149. }
  7150. if (New)
  7151. {
  7152. Configuration->Usage->Inc(L"UpdateNotifications");
  7153. }
  7154. }
  7155. }
  7156. //---------------------------------------------------------------------------
  7157. void __fastcall TCustomScpExplorerForm::UpdatesNoteClicked(TObject * /*Sender*/)
  7158. {
  7159. RestoreApp();
  7160. if (!NonVisualDataModule->Busy)
  7161. {
  7162. Configuration->Usage->Inc(L"UpdateNotificationsClicked");
  7163. CheckForUpdates(true);
  7164. }
  7165. }
  7166. //---------------------------------------------------------------------------
  7167. void __fastcall TCustomScpExplorerForm::GetTransferPresetAutoSelectData(
  7168. TCopyParamRuleData & Data)
  7169. {
  7170. DebugAssert(Terminal != NULL);
  7171. Data.HostName = Terminal->SessionData->HostNameExpanded;
  7172. Data.UserName = Terminal->SessionData->UserNameExpanded;
  7173. Data.RemoteDirectory = RemoteDirView->PathName;
  7174. }
  7175. //---------------------------------------------------------------------------
  7176. void __fastcall TCustomScpExplorerForm::TransferPresetAutoSelect()
  7177. {
  7178. // Terminal can be null when we are changing local directory implicitly,
  7179. // because it has been deleted, while no session is connected
  7180. // (Login dialog is open)
  7181. if (FAllowTransferPresetAutoSelect && (Terminal != NULL))
  7182. {
  7183. TCopyParamRuleData Data;
  7184. GetTransferPresetAutoSelectData(Data);
  7185. int CopyParamIndex = GUIConfiguration->CopyParamList->Find(Data);
  7186. UnicodeString CopyParamCurrent = GUIConfiguration->CopyParamCurrent;
  7187. if (CopyParamIndex < 0)
  7188. {
  7189. // there is no preset that matches autoselection
  7190. // set preset that we consider "default"
  7191. FCopyParamAutoSelected = L""; // forget last autoselected preset
  7192. GUIConfiguration->CopyParamCurrent = FCopyParamDefault;
  7193. }
  7194. else
  7195. {
  7196. // there is preset matching autoselection
  7197. UnicodeString CopyParamName = GUIConfiguration->CopyParamList->Names[CopyParamIndex];
  7198. if (CopyParamName == FCopyParamAutoSelected)
  7199. {
  7200. // autoselected the same preset as the last time
  7201. // make no change (i.e. preserve custom user preset, if any)
  7202. }
  7203. else
  7204. {
  7205. // autoselected the different preset then the last time (or there
  7206. // was default preset set)
  7207. FCopyParamAutoSelected = CopyParamName; // remember autoselection
  7208. GUIConfiguration->CopyParamCurrent = CopyParamName;
  7209. }
  7210. }
  7211. if (GUIConfiguration->CopyParamCurrent != CopyParamCurrent)
  7212. {
  7213. if (CopyParamIndex >= 0)
  7214. {
  7215. Configuration->Usage->Inc(L"CopyParamAutoSelects");
  7216. }
  7217. TTransferPresetNoteData * Data = new TTransferPresetNoteData;
  7218. try
  7219. {
  7220. int Fmt =
  7221. (CopyParamIndex < 0) ?
  7222. (GUIConfiguration->CopyParamIndex < 0 ? COPY_PARAM_DEFAULT_NORM : COPY_PARAM_DEFAULT_CUSTOM) :
  7223. COPY_PARAM_AUTOSELECTED;
  7224. UnicodeString Message = FMTLOAD(Fmt, (StripHotkey(GUIConfiguration->CopyParamCurrent)));
  7225. Data->Message = MainInstructions(Message);
  7226. int CopyParamAttrs = Terminal->UsableCopyParamAttrs(0).General;
  7227. UnicodeString Info = GUIConfiguration->CurrentCopyParam.GetInfoStr(L"\n",
  7228. CopyParamAttrs);
  7229. if (CopyParamIndex >= 0)
  7230. {
  7231. DebugAssert(GUIConfiguration->CopyParamList->Rules[CopyParamIndex] != NULL);
  7232. Info = FORMAT(L"%s\n \n%s", (Info,
  7233. FMTLOAD(COPY_PARAM_RULE,
  7234. (GUIConfiguration->CopyParamList->Rules[CopyParamIndex]->GetInfoStr(L"\n")))));
  7235. }
  7236. Data->Message += L"\n\n" + Info;
  7237. if (WinConfiguration->CopyParamAutoSelectNotice)
  7238. {
  7239. TransferPresetNoteMessage(Data, true);
  7240. }
  7241. else
  7242. {
  7243. PostNote(Message, 0, TransferPresetNoteClicked, Data);
  7244. Data = NULL; // ownership passed
  7245. }
  7246. }
  7247. __finally
  7248. {
  7249. delete Data;
  7250. }
  7251. }
  7252. }
  7253. }
  7254. //---------------------------------------------------------------------------
  7255. void __fastcall TCustomScpExplorerForm::TransferPresetNoteMessage(
  7256. TTransferPresetNoteData * NoteData, bool AllowNeverAskAgain)
  7257. {
  7258. DebugAssert(NoteData != NULL);
  7259. TMessageParams Params(AllowNeverAskAgain ? mpNeverAskAgainCheck : 0);
  7260. TQueryButtonAlias Aliases[1];
  7261. Aliases[0].Button = qaIgnore; // "ignore" is after "ok"
  7262. Aliases[0].Alias = LoadStr(CONFIGURE_BUTTON);
  7263. Params.Aliases = Aliases;
  7264. Params.AliasesCount = LENOF(Aliases);
  7265. unsigned int Result =
  7266. MoreMessageDialog(NoteData->Message, NULL, qtInformation,
  7267. qaOK | qaIgnore, HELP_COPY_PARAM_AUTOSELECTED, &Params);
  7268. switch (Result)
  7269. {
  7270. case qaNeverAskAgain:
  7271. DebugAssert(AllowNeverAskAgain);
  7272. WinConfiguration->CopyParamAutoSelectNotice = false;
  7273. break;
  7274. case qaIgnore:
  7275. PreferencesDialog(pmPresets);
  7276. break;
  7277. }
  7278. }
  7279. //---------------------------------------------------------------------------
  7280. void __fastcall TCustomScpExplorerForm::TransferPresetNoteClicked(TObject * Sender)
  7281. {
  7282. // as of now useless, as this is used for notes only, never for balloons, ...
  7283. RestoreApp();
  7284. // .. and we should never be busy here
  7285. if (DebugAlwaysTrue(!NonVisualDataModule->Busy))
  7286. {
  7287. TransferPresetNoteMessage(DebugNotNull(dynamic_cast<TTransferPresetNoteData *>(Sender)), false);
  7288. }
  7289. }
  7290. //---------------------------------------------------------------------------
  7291. void __fastcall TCustomScpExplorerForm::PreferencesDialog(
  7292. TPreferencesMode APreferencesMode)
  7293. {
  7294. std::unique_ptr<TPreferencesDialogData> PreferencesData;
  7295. TCopyParamRuleData Data;
  7296. if (Terminal != NULL)
  7297. {
  7298. PreferencesData.reset(new TPreferencesDialogData());
  7299. GetTransferPresetAutoSelectData(Data);
  7300. PreferencesData->CopyParamRuleData = &Data;
  7301. }
  7302. DoPreferencesDialog(APreferencesMode, PreferencesData.get());
  7303. }
  7304. //---------------------------------------------------------------------------
  7305. void __fastcall TCustomScpExplorerForm::AdHocCustomCommandValidate(
  7306. const TCustomCommandType & Command)
  7307. {
  7308. if (CustomCommandState(Command, FEditingFocusedAdHocCommand) <= 0)
  7309. {
  7310. throw Exception(FMTLOAD(CUSTOM_COMMAND_IMPOSSIBLE, (Command.Command)));
  7311. }
  7312. }
  7313. //---------------------------------------------------------------------------
  7314. void __fastcall TCustomScpExplorerForm::AdHocCustomCommand(bool OnFocused)
  7315. {
  7316. bool RemoteAllowed = CustomCommandRemoteAllowed();
  7317. TCustomCommandType Command;
  7318. // make sure we use local custom command when remote are not supported
  7319. if (RemoteAllowed || FLAGSET(FLastCustomCommand.Params, ccLocal))
  7320. {
  7321. Command = FLastCustomCommand;
  7322. }
  7323. else
  7324. {
  7325. Command.Params = Command.Params | ccLocal;
  7326. }
  7327. Command.Name = LoadStr(CUSTOM_COMMAND_AD_HOC_NAME);
  7328. FEditingFocusedAdHocCommand = OnFocused;
  7329. bool LocalSide = (FCurrentSide == osLocal);
  7330. int Options =
  7331. FLAGMASK((!RemoteAllowed || LocalSide), ccoDisableRemote) |
  7332. FLAGMASK(LocalSide, ccoDisableRemoteFiles);
  7333. if (DoCustomCommandDialog(Command, WinConfiguration->CustomCommandList,
  7334. ccmAdHoc, Options, AdHocCustomCommandValidate, NULL))
  7335. {
  7336. FLastCustomCommand = Command;
  7337. UpdateCustomCommandsToolbar();
  7338. ExecuteFileOperation(foCustomCommand, osRemote, OnFocused, false, &Command);
  7339. }
  7340. }
  7341. //---------------------------------------------------------------------------
  7342. void __fastcall TCustomScpExplorerForm::LastCustomCommand(bool OnFocused)
  7343. {
  7344. DebugAssert(!FLastCustomCommand.Command.IsEmpty());
  7345. int State = CustomCommandState(FLastCustomCommand, OnFocused);
  7346. DebugAssert(State > 0);
  7347. if (State <= 0)
  7348. {
  7349. throw Exception(FMTLOAD(CUSTOM_COMMAND_IMPOSSIBLE, (FLastCustomCommand.Command)));
  7350. }
  7351. ExecuteFileOperation(foCustomCommand, osRemote, OnFocused, false, &FLastCustomCommand);
  7352. }
  7353. //---------------------------------------------------------------------------
  7354. bool __fastcall TCustomScpExplorerForm::GetLastCustomCommand(bool OnFocused,
  7355. TCustomCommandType & Command, int & State)
  7356. {
  7357. bool Result = !FLastCustomCommand.Command.IsEmpty();
  7358. if (Result)
  7359. {
  7360. Command = FLastCustomCommand;
  7361. State = CustomCommandState(FLastCustomCommand, OnFocused);
  7362. }
  7363. return Result;
  7364. }
  7365. //---------------------------------------------------------------------------
  7366. void __fastcall TCustomScpExplorerForm::WhatsThis()
  7367. {
  7368. SendMessage(Handle, WM_SYSCOMMAND, SC_CONTEXTHELP, 0);
  7369. }
  7370. //---------------------------------------------------------------------------
  7371. void __fastcall TCustomScpExplorerForm::BeforeAction()
  7372. {
  7373. if (RemoteDirView->ItemFocused != NULL)
  7374. {
  7375. RemoteDirView->ItemFocused->CancelEdit();
  7376. }
  7377. }
  7378. //---------------------------------------------------------------------------
  7379. void __fastcall TCustomScpExplorerForm::PostComponentHide(Byte Component)
  7380. {
  7381. DebugAssert(ComponentVisible[Component]);
  7382. PostMessage(Handle, WM_COMPONENT_HIDE, Component, 0);
  7383. }
  7384. //---------------------------------------------------------------------------
  7385. void __fastcall TCustomScpExplorerForm::QueueSplitterDblClick(TObject * /*Sender*/)
  7386. {
  7387. // when queue panel is resized here directly, the status bar is stretched
  7388. // over whole space the panel occupied
  7389. PostComponentHide(fcQueueView);
  7390. }
  7391. //---------------------------------------------------------------------------
  7392. void __fastcall TCustomScpExplorerForm::Dispatch(void * Message)
  7393. {
  7394. TMessage * M = static_cast<TMessage*>(Message);
  7395. switch (M->Msg)
  7396. {
  7397. case CM_DIALOGCHAR:
  7398. CMDialogChar(*M);
  7399. break;
  7400. case _WM_APPCOMMAND:
  7401. WMAppCommand(*M);
  7402. break;
  7403. case WM_SYSCOMMAND:
  7404. WMSysCommand(*M);
  7405. break;
  7406. case WM_QUERYENDSESSION:
  7407. WMQueryEndSession(*M);
  7408. break;
  7409. case WM_ENDSESSION:
  7410. WMEndSession(*reinterpret_cast<TWMEndSession *>(M));
  7411. break;
  7412. case WM_COMPONENT_HIDE:
  7413. {
  7414. Byte Component = static_cast<Byte>(M->WParam);
  7415. // sanity check
  7416. if (ComponentVisible[Component])
  7417. {
  7418. // special treatment
  7419. if (Component == fcQueueView)
  7420. {
  7421. ToggleQueueVisibility();
  7422. DebugAssert(!ComponentVisible[fcQueueView]);
  7423. }
  7424. else
  7425. {
  7426. ComponentVisible[Component] = false;
  7427. }
  7428. }
  7429. }
  7430. break;
  7431. case WM_COPYDATA:
  7432. WMCopyData(*M);
  7433. break;
  7434. case WM_MANAGES_CAPTION:
  7435. // caption managed in TTerminalManager::UpdateAppTitle()
  7436. M->Result = 1;
  7437. break;
  7438. case WM_WANTS_MOUSEWHEEL:
  7439. M->Result = 1;
  7440. break;
  7441. case CM_SHOWINGCHANGED:
  7442. CMShowingChanged(*M);
  7443. break;
  7444. case WM_CLOSE:
  7445. WMClose(*M);
  7446. break;
  7447. default:
  7448. TForm::Dispatch(Message);
  7449. break;
  7450. }
  7451. }
  7452. //---------------------------------------------------------------------------
  7453. void __fastcall TCustomScpExplorerForm::WMClose(TMessage & Message)
  7454. {
  7455. // Cannot close window while we are busy.
  7456. // We cannot test this in FormCloseQuery as that is called also from
  7457. // Close(), which is called by CloseApplicationAction. So we can be busy
  7458. // there already even, when it is legitimate to close the application.
  7459. // Possibly a better place to handle this would be WMSysCommand.
  7460. if (NonVisualDataModule->Busy)
  7461. {
  7462. Message.Result = 1;
  7463. }
  7464. else
  7465. {
  7466. TForm::Dispatch(&Message);
  7467. }
  7468. }
  7469. //---------------------------------------------------------------------------
  7470. void __fastcall TCustomScpExplorerForm::CMShowingChanged(TMessage & Message)
  7471. {
  7472. TForm::Dispatch(&Message);
  7473. if (Showing && (Terminal == NULL))
  7474. {
  7475. // When we are starting minimized (i.e. from an installer),
  7476. // postpone showing Login dialog until we get restored.
  7477. // Otherwise the Login dialog (and Authentication window) show restored
  7478. // over invidible (minimized) main window.
  7479. if (WindowState == wsMinimized)
  7480. {
  7481. FNeedSession = true;
  7482. }
  7483. else
  7484. {
  7485. // This happens before application ever goes idle, so the toolbars would
  7486. // stay enabled (initial state) until the Login dialog is dismissed.
  7487. UpdateActions();
  7488. NonVisualDataModule->StartBusy();
  7489. try
  7490. {
  7491. // Need to process WM_ACTIVATEAPP before showing the Login dialog,
  7492. // otherwise the dialog does not receive focus.
  7493. // With Commander interface the ProcessMessages is called already
  7494. // by TDriveView, but with Explorer interface, we need to call it explicily
  7495. Application->ProcessMessages();
  7496. // do not reload sessions, they have been loaded just now (optimization)
  7497. NeedSession(false);
  7498. }
  7499. __finally
  7500. {
  7501. NonVisualDataModule->EndBusy();
  7502. }
  7503. }
  7504. }
  7505. }
  7506. //---------------------------------------------------------------------------
  7507. void __fastcall TCustomScpExplorerForm::FormConstrainedResize(
  7508. TObject * /*Sender*/, int & MinWidth, int & MinHeight, int & MaxWidth,
  7509. int & MaxHeight)
  7510. {
  7511. // workaround for bug in TWinControl.CalcConstraints
  7512. // Check for empty rect (restore from iconic state) is done there only after
  7513. // call to AdjustClientRect, which enlarges the rect (for forms).
  7514. TRect R = GetClientRect();
  7515. // when restoring from iconic state, place no restrictions
  7516. if (IsRectEmpty(R))
  7517. {
  7518. MinWidth = 0;
  7519. MinHeight = 0;
  7520. MaxWidth = 32000;
  7521. MaxHeight = 32000;
  7522. }
  7523. }
  7524. //---------------------------------------------------------------------------
  7525. void __fastcall TCustomScpExplorerForm::GetSpaceAvailable(const UnicodeString Path,
  7526. TSpaceAvailable & ASpaceAvailable, bool & Close)
  7527. {
  7528. // terminal can be already closed (e.g. dropped connection)
  7529. if ((Terminal != NULL) && Terminal->IsCapable[fcCheckingSpaceAvailable])
  7530. {
  7531. Configuration->Usage->Inc(L"SpaceAvailableChecks");
  7532. try
  7533. {
  7534. Terminal->SpaceAvailable(Path, ASpaceAvailable);
  7535. }
  7536. catch(...)
  7537. {
  7538. if (!Terminal->Active)
  7539. {
  7540. Close = true;
  7541. }
  7542. throw;
  7543. }
  7544. }
  7545. }
  7546. //---------------------------------------------------------------------------
  7547. void __fastcall TCustomScpExplorerForm::FileSystemInfo()
  7548. {
  7549. const TSessionInfo & SessionInfo = Terminal->GetSessionInfo();
  7550. const TFileSystemInfo & FileSystemInfo = Terminal->GetFileSystemInfo(true);
  7551. TGetSpaceAvailable OnGetSpaceAvailable = NULL;
  7552. if (Terminal->IsCapable[fcCheckingSpaceAvailable])
  7553. {
  7554. OnGetSpaceAvailable = GetSpaceAvailable;
  7555. }
  7556. DoFileSystemInfoDialog(SessionInfo, FileSystemInfo, Terminal->CurrentDirectory,
  7557. OnGetSpaceAvailable);
  7558. }
  7559. //---------------------------------------------------------------------------
  7560. TSessionData * __fastcall TCustomScpExplorerForm::SessionDataForCode()
  7561. {
  7562. std::unique_ptr<TSessionData> Data(CloneCurrentSessionData());
  7563. const TSessionInfo & SessionInfo = Terminal->GetSessionInfo();
  7564. Data->HostKey = SessionInfo.HostKeyFingerprint;
  7565. return Data.release();
  7566. }
  7567. //---------------------------------------------------------------------------
  7568. void __fastcall TCustomScpExplorerForm::GenerateUrl(TStrings * Paths)
  7569. {
  7570. std::unique_ptr<TSessionData> Data(SessionDataForCode());
  7571. DoGenerateUrlDialog(Data.get(), Paths);
  7572. }
  7573. //---------------------------------------------------------------------------
  7574. void __fastcall TCustomScpExplorerForm::SessionGenerateUrl()
  7575. {
  7576. GenerateUrl(NULL);
  7577. }
  7578. //---------------------------------------------------------------------------
  7579. void __fastcall TCustomScpExplorerForm::FileGenerateUrl()
  7580. {
  7581. std::unique_ptr<TStrings> Paths(new TStringList());
  7582. DirView(osCurrent)->CreateFileList(false, true, Paths.get());
  7583. GenerateUrl(Paths.get());
  7584. }
  7585. //---------------------------------------------------------------------------
  7586. void __fastcall TCustomScpExplorerForm::SetSessionColor(TColor value)
  7587. {
  7588. if (value != FSessionColor)
  7589. {
  7590. FSessionColor = value;
  7591. TColor C = (value != 0 ? value : Vcl::Graphics::clNone);
  7592. TTBXColorItem * ColorItem = dynamic_cast<TTBXColorItem *>(
  7593. static_cast<TObject *>(GetComponent(fcColorMenu)));
  7594. DebugAssert(ColorItem != NULL);
  7595. ColorItem->Color = C;
  7596. NonVisualDataModule->ColorMenuItem->Color = C;
  7597. // Is null when called from LastTerminalClosed
  7598. if (Terminal != NULL)
  7599. {
  7600. SessionsPageControl->ActivePage->ImageIndex = AddSessionColor(value);
  7601. }
  7602. UpdateControls();
  7603. }
  7604. }
  7605. //---------------------------------------------------------------------------
  7606. bool __fastcall TCustomScpExplorerForm::CancelNote(bool Force)
  7607. {
  7608. bool Result = FNoteTimer->Enabled;
  7609. if (Result)
  7610. {
  7611. // cannot cancel note too early
  7612. bool NotEarly =
  7613. (Now() - FNoteShown >
  7614. EncodeTimeVerbose(0, 0, (unsigned short)(WinConfiguration->NotificationsStickTime), 0));
  7615. if (Force || NotEarly)
  7616. {
  7617. FNoteTimer->Enabled = false;
  7618. FNote = L"";
  7619. SAFE_DESTROY(FNoteData);
  7620. FOnNoteClick = NULL;
  7621. FNoteHints = FNotes->Text;
  7622. FNoteHints.Delete(FNoteHints.Length() - 1, 2);
  7623. UpdateStatusBar();
  7624. }
  7625. }
  7626. return Result;
  7627. }
  7628. //---------------------------------------------------------------------------
  7629. void __fastcall TCustomScpExplorerForm::NoteTimer(TObject * /*Sender*/)
  7630. {
  7631. DebugAssert(FNoteTimer->Enabled);
  7632. CancelNote(true);
  7633. }
  7634. //---------------------------------------------------------------------------
  7635. void __fastcall TCustomScpExplorerForm::AddNote(UnicodeString Note, bool UpdateNow)
  7636. {
  7637. int P = Note.Pos(L"\n");
  7638. if (P > 0)
  7639. {
  7640. Note.SetLength(P - 1);
  7641. }
  7642. FNotes->Add(FORMAT(L"[%s] %s",
  7643. (FormatDateTime(Configuration->TimeFormat, Now()), Note)));
  7644. while (FNotes->Count > 10)
  7645. {
  7646. FNotes->Delete(0);
  7647. }
  7648. if (UpdateNow)
  7649. {
  7650. FNoteHints = FNotes->Text;
  7651. FNoteHints.Delete(FNoteHints.Length() - 1, 2);
  7652. UpdateStatusBar();
  7653. }
  7654. }
  7655. //---------------------------------------------------------------------------
  7656. void __fastcall TCustomScpExplorerForm::PostNote(UnicodeString Note,
  7657. unsigned int Seconds, TNotifyEvent OnNoteClick, TObject * NoteData)
  7658. {
  7659. int P = Note.Pos(L"\n");
  7660. if (P > 0)
  7661. {
  7662. Note.SetLength(P - 1);
  7663. }
  7664. FNoteHints = FNotes->Text;
  7665. FNoteHints.Delete(FNoteHints.Length() - 1, 2);
  7666. FNote = Note;
  7667. SAFE_DESTROY(FNoteData);
  7668. FNoteData = NoteData;
  7669. FOnNoteClick = OnNoteClick;
  7670. AddNote(Note, false);
  7671. UpdateStatusBar();
  7672. FNoteShown = Now();
  7673. FNoteTimer->Enabled = false;
  7674. if (Seconds == 0)
  7675. {
  7676. Seconds = WinConfiguration->NotificationsTimeout;
  7677. }
  7678. FNoteTimer->Interval = Seconds * MSecsPerSec;
  7679. FNoteTimer->Enabled = true;
  7680. }
  7681. //---------------------------------------------------------------------------
  7682. void __fastcall TCustomScpExplorerForm::ReadDirectoryCancelled()
  7683. {
  7684. PostNote(LoadStr(DIRECTORY_READING_CANCELLED), 0, NULL, NULL);
  7685. }
  7686. //---------------------------------------------------------------------------
  7687. void __fastcall TCustomScpExplorerForm::SynchronizeBrowsingChanged()
  7688. {
  7689. if (NonVisualDataModule->SynchronizeBrowsingAction->Checked)
  7690. {
  7691. Configuration->Usage->Inc(L"SynchronizeBrowsingEnabled");
  7692. }
  7693. PostNote(FORMAT(LoadStrPart(SYNC_DIR_BROWSE_TOGGLE, 1),
  7694. (LoadStrPart(SYNC_DIR_BROWSE_TOGGLE,
  7695. (NonVisualDataModule->SynchronizeBrowsingAction->Checked ? 2 : 3)))),
  7696. 0, NULL, NULL);
  7697. }
  7698. //---------------------------------------------------------------------------
  7699. void __fastcall TCustomScpExplorerForm::ToggleShowHiddenFiles()
  7700. {
  7701. WinConfiguration->ShowHiddenFiles = !WinConfiguration->ShowHiddenFiles;
  7702. PostNote(FORMAT(LoadStrPart(SHOW_HIDDEN_FILES_TOGGLE, 1),
  7703. (LoadStrPart(SHOW_HIDDEN_FILES_TOGGLE,
  7704. (WinConfiguration->ShowHiddenFiles ? 2 : 3)))), 0, NULL, NULL);
  7705. }
  7706. //---------------------------------------------------------------------------
  7707. void __fastcall TCustomScpExplorerForm::SetFormatSizeBytes(TFormatBytesStyle Style)
  7708. {
  7709. WinConfiguration->FormatSizeBytes = Style;
  7710. }
  7711. //---------------------------------------------------------------------------
  7712. void __fastcall TCustomScpExplorerForm::ToggleAutoReadDirectoryAfterOp()
  7713. {
  7714. Configuration->AutoReadDirectoryAfterOp = !Configuration->AutoReadDirectoryAfterOp;
  7715. PostNote(FORMAT(LoadStrPart(AUTO_READ_DIRECTORY_TOGGLE, 1),
  7716. (LoadStrPart(AUTO_READ_DIRECTORY_TOGGLE,
  7717. (Configuration->AutoReadDirectoryAfterOp ? 2 : 3)))), 0, NULL, NULL);
  7718. }
  7719. //---------------------------------------------------------------------------
  7720. void __fastcall TCustomScpExplorerForm::StatusBarPanelDblClick(
  7721. TTBXCustomStatusBar * Sender, TTBXStatusPanel * Panel)
  7722. {
  7723. if (Panel->Index == 0)
  7724. {
  7725. if (FOnNoteClick != NULL)
  7726. {
  7727. // prevent the user data from being freed by possible call
  7728. // to CancelNote or PostNote during call to OnNoteClick
  7729. std::unique_ptr<TObject> NoteData(FNoteData);
  7730. TNotifyEvent OnNoteClick = FOnNoteClick;
  7731. FNoteData = NULL;
  7732. // need to cancel the note as we are going to delete its user data
  7733. CancelNote(true);
  7734. OnNoteClick(NoteData.get());
  7735. }
  7736. }
  7737. if (Panel->Index >= Sender->Panels->Count - SessionPanelCount)
  7738. {
  7739. FileSystemInfo();
  7740. }
  7741. }
  7742. //---------------------------------------------------------------------------
  7743. void __fastcall TCustomScpExplorerForm::LockWindow()
  7744. {
  7745. // workaround:
  7746. // 1) for unknown reason, disabling window, while minimized,
  7747. // prevents it from restoring, even if it was enabled again meanwhile
  7748. // 2) when disabling the main window, while another has focus
  7749. // minimize is no longer possible ("keep up to date" dialog)
  7750. // Whouldn't we use IsApplicationMinimized() here?
  7751. if ((FLockSuspendLevel == 0) && !IsIconic(Application->Handle) && (Screen->ActiveForm == this))
  7752. {
  7753. Enabled = false;
  7754. }
  7755. FLockLevel++;
  7756. }
  7757. //---------------------------------------------------------------------------
  7758. void __fastcall TCustomScpExplorerForm::UnlockWindow()
  7759. {
  7760. DebugAssert(FLockLevel > 0);
  7761. FLockLevel--;
  7762. if (FLockLevel == 0)
  7763. {
  7764. DebugAssert(FLockSuspendLevel == 0);
  7765. Enabled = true;
  7766. }
  7767. }
  7768. //---------------------------------------------------------------------------
  7769. void __fastcall TCustomScpExplorerForm::SuspendWindowLock()
  7770. {
  7771. // We need to make sure that window is enabled when the last modal window closes
  7772. // otherwise focus is not restored correctly.
  7773. // So we re-enable the window when modal window opens and
  7774. // disable it back after is closes
  7775. if (FLockLevel > 0)
  7776. {
  7777. // while we have nesting counter, we know that we never be called
  7778. // recursivelly as Application->OnModalBegin is called only
  7779. // for the top-level modal window
  7780. if (DebugAlwaysTrue(FLockSuspendLevel == 0))
  7781. {
  7782. // won't be disabled when conditions in LockWindow() were not satisfied
  7783. FDisabledOnLockSuspend = !Enabled;
  7784. // When minimized to tray (or actually when set to SW_HIDE),
  7785. // setting Enabled makes the window focusable even when there's
  7786. // modal window over it
  7787. if (!FTrayIcon->Visible)
  7788. {
  7789. Enabled = true;
  7790. }
  7791. }
  7792. FLockSuspendLevel++;
  7793. }
  7794. }
  7795. //---------------------------------------------------------------------------
  7796. void __fastcall TCustomScpExplorerForm::ResumeWindowLock()
  7797. {
  7798. if (FLockSuspendLevel > 0)
  7799. {
  7800. DebugAssert(FLockLevel > 0);
  7801. FLockSuspendLevel--;
  7802. // see comment in SuspendWindowLock
  7803. if (DebugAlwaysTrue(FLockSuspendLevel == 0))
  7804. {
  7805. // Note that window can be enabled here, when we were minized to tray when
  7806. // was SuspendWindowLock() called.
  7807. // We should possibly do the same check as in LockWindow(),
  7808. // if it is ever possible that the consitions change between
  7809. // SuspendWindowLock() and ResumeWindowLock()
  7810. Enabled = !FDisabledOnLockSuspend;
  7811. }
  7812. }
  7813. }
  7814. //---------------------------------------------------------------------------
  7815. void __fastcall TCustomScpExplorerForm::UpdateRemotePathComboBox(bool TextOnly)
  7816. {
  7817. if (!TextOnly)
  7818. {
  7819. TTBXComboBoxItem * RemotePathComboBox =
  7820. reinterpret_cast<TTBXComboBoxItem *>(GetComponent(fcRemotePathComboBox));
  7821. TStrings * Items = RemotePathComboBox->Strings;
  7822. Items->BeginUpdate();
  7823. try
  7824. {
  7825. Items->Clear();
  7826. if (Terminal != NULL)
  7827. {
  7828. UnicodeString APath = UnixExcludeTrailingBackslash(RemoteDirView->Path);
  7829. while (!IsUnixRootPath(APath))
  7830. {
  7831. int P = APath.LastDelimiter(L'/');
  7832. DebugAssert(P >= 0);
  7833. Items->Insert(0, APath.SubString(P + 1, APath.Length() - P));
  7834. APath.SetLength(P - 1);
  7835. }
  7836. Items->Insert(0, Customunixdirview_SUnixDefaultRootName);
  7837. }
  7838. }
  7839. __finally
  7840. {
  7841. RemotePathComboBox->ItemIndex = Items->Count - 1;
  7842. // Setting ItemIndex to -1 does not reset its text
  7843. if (Items->Count == 0)
  7844. {
  7845. RemotePathComboBox->Text = L"";
  7846. }
  7847. Items->EndUpdate();
  7848. }
  7849. }
  7850. }
  7851. //---------------------------------------------------------------------------
  7852. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxAdjustImageIndex(
  7853. TTBXComboBoxItem * Sender, const UnicodeString /*AText*/, int AIndex,
  7854. int & ImageIndex)
  7855. {
  7856. if (AIndex < 0)
  7857. {
  7858. AIndex = Sender->ItemIndex;
  7859. }
  7860. ImageIndex = (AIndex < Sender->Strings->Count - 1 ? StdDirIcon : StdDirSelIcon);
  7861. }
  7862. //---------------------------------------------------------------------------
  7863. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxDrawItem(
  7864. TTBXCustomList * /*Sender*/, TCanvas * /*ACanvas*/, TRect & ARect, int AIndex,
  7865. int /*AHoverIndex*/, bool & /*DrawDefault*/)
  7866. {
  7867. ARect.Left += (10 * AIndex);
  7868. }
  7869. //---------------------------------------------------------------------------
  7870. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxMeasureWidth(
  7871. TTBXCustomList * /*Sender*/, TCanvas * /*ACanvas*/, int AIndex, int &AWidth)
  7872. {
  7873. AWidth += (10 * AIndex);
  7874. }
  7875. //---------------------------------------------------------------------------
  7876. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxItemClick(
  7877. TObject * Sender)
  7878. {
  7879. TTBXComboBoxItem * RemotePathComboBox = dynamic_cast<TTBXComboBoxItem*>(Sender);
  7880. UnicodeString APath = UnixExcludeTrailingBackslash(RemoteDirView->Path);
  7881. int Index = RemotePathComboBox->ItemIndex;
  7882. while (Index < RemotePathComboBox->Strings->Count - 1)
  7883. {
  7884. APath = UnixExtractFileDir(APath);
  7885. Index++;
  7886. }
  7887. // VanDyke style paths
  7888. if (APath.IsEmpty())
  7889. {
  7890. DebugAssert(RemotePathComboBox->ItemIndex == 0);
  7891. APath = ROOTDIRECTORY;
  7892. }
  7893. if (RemoteDirView->Path != APath)
  7894. {
  7895. RemoteDirView->Path = APath;
  7896. }
  7897. }
  7898. //---------------------------------------------------------------------------
  7899. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxCancel(TObject * Sender)
  7900. {
  7901. DebugAssert(Sender == GetComponent(fcRemotePathComboBox));
  7902. DebugUsedParam(Sender);
  7903. UpdateRemotePathComboBox(true);
  7904. }
  7905. //---------------------------------------------------------------------------
  7906. void __fastcall TCustomScpExplorerForm::ClickToolbarItem(TTBCustomItem * Item,
  7907. bool PositionCursor)
  7908. {
  7909. TTBCustomItem * TopItem = Item;
  7910. while (TopItem->Parent != NULL)
  7911. {
  7912. TopItem = TopItem->Parent;
  7913. }
  7914. TTBCustomToolbar * Toolbar = dynamic_cast<TTBCustomToolbar *>(TopItem->ParentComponent);
  7915. DebugAssert(Toolbar != NULL);
  7916. TTBItemViewer * Viewer = Toolbar->View->Find(Item);
  7917. DebugAssert(Viewer != NULL);
  7918. int X = Viewer->BoundsRect.Left + (Viewer->BoundsRect.Width() / 2);
  7919. int Y = Viewer->BoundsRect.Top + (Viewer->BoundsRect.Height() / 2);
  7920. if (PositionCursor)
  7921. {
  7922. Mouse->CursorPos = Toolbar->ClientToScreen(TPoint(X, Y));
  7923. }
  7924. PostMessage(Toolbar->Handle, WM_LBUTTONDOWN, MK_LBUTTON, MAKELPARAM(X, Y));
  7925. }
  7926. //---------------------------------------------------------------------------
  7927. void __fastcall TCustomScpExplorerForm::DirViewEditing(
  7928. TObject * Sender, TListItem * Item, bool & /*AllowEdit*/)
  7929. {
  7930. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  7931. DebugAssert(DirView != NULL);
  7932. if (!WinConfiguration->RenameWholeName && !DirView->ItemIsDirectory(Item))
  7933. {
  7934. HWND Edit = ListView_GetEditControl(DirView->Handle);
  7935. // OnEditing is called also from TCustomListView::CanEdit
  7936. if (Edit != NULL)
  7937. {
  7938. EditSelectBaseName(Edit);
  7939. }
  7940. }
  7941. }
  7942. //---------------------------------------------------------------------------
  7943. void __fastcall TCustomScpExplorerForm::FormActivate(TObject * /*Sender*/)
  7944. {
  7945. Application->OnHint = ApplicationHint;
  7946. }
  7947. //---------------------------------------------------------------------------
  7948. TDragDropFilesEx * __fastcall TCustomScpExplorerForm::CreateDragDropFilesEx()
  7949. {
  7950. TDragDropFilesEx * Result = new TDragDropFilesEx(this);
  7951. Result->AutoDetectDnD = false;
  7952. Result->NeedValid = TFileExMustDnDSet() << nvFilename;
  7953. Result->RenderDataOn = rdoEnterAndDropSync;
  7954. Result->TargetEffects = TDropEffectSet() << deCopy << deMove;
  7955. return Result;
  7956. }
  7957. //---------------------------------------------------------------------------
  7958. void __fastcall TCustomScpExplorerForm::CreateWnd()
  7959. {
  7960. TForm::CreateWnd();
  7961. if (FSessionsDragDropFilesEx == NULL)
  7962. {
  7963. FSessionsDragDropFilesEx = CreateDragDropFilesEx();
  7964. FSessionsDragDropFilesEx->OnDragOver = SessionsDDDragOver;
  7965. FSessionsDragDropFilesEx->OnProcessDropped = SessionsDDProcessDropped;
  7966. FSessionsDragDropFilesEx->OnDragEnter = SessionsDDDragEnter;
  7967. FSessionsDragDropFilesEx->OnDragLeave = SessionsDDDragLeave;
  7968. }
  7969. if (FQueueDragDropFilesEx == NULL)
  7970. {
  7971. FQueueDragDropFilesEx = CreateDragDropFilesEx();
  7972. // No need to set OnDragOver as we do not have any restrictions
  7973. FQueueDragDropFilesEx->OnProcessDropped = QueueDDProcessDropped;
  7974. FQueueDragDropFilesEx->OnDragEnter = QueueDDDragEnter;
  7975. FQueueDragDropFilesEx->OnDragLeave = QueueDDDragLeave;
  7976. }
  7977. }
  7978. //---------------------------------------------------------------------------
  7979. void __fastcall TCustomScpExplorerForm::DestroyWnd()
  7980. {
  7981. TForm::DestroyWnd();
  7982. FSessionsDragDropFilesEx->DragDropControl = NULL;
  7983. FQueueDragDropFilesEx->DragDropControl = NULL;
  7984. }
  7985. //---------------------------------------------------------------------------
  7986. void __fastcall TCustomScpExplorerForm::FormShow(TObject * /*Sender*/)
  7987. {
  7988. SideEnter(FCurrentSide);
  7989. FEverShown = true;
  7990. }
  7991. //---------------------------------------------------------------------------
  7992. void __fastcall TCustomScpExplorerForm::DoFindFiles(
  7993. UnicodeString Directory, const TFileMasks & FileMask,
  7994. TFileFoundEvent OnFileFound, TFindingFileEvent OnFindingFile)
  7995. {
  7996. Configuration->Usage->Inc(L"FileFinds");
  7997. FTerminal->FilesFind(Directory, FileMask, OnFileFound, OnFindingFile);
  7998. }
  7999. //---------------------------------------------------------------------------
  8000. void __fastcall TCustomScpExplorerForm::DoFocusRemotePath(UnicodeString Path)
  8001. {
  8002. RemoteDirView->Path = UnixExtractFilePath(Path);
  8003. TListItem * Item = RemoteDirView->FindFileItem(UnixExtractFileName(Path));
  8004. if (Item != NULL)
  8005. {
  8006. RemoteDirView->ItemFocused = Item;
  8007. RemoteDirView->ItemFocused->MakeVisible(false);
  8008. RemoteDirView->SetFocus();
  8009. }
  8010. }
  8011. //---------------------------------------------------------------------------
  8012. void __fastcall TCustomScpExplorerForm::RemoteFindFiles()
  8013. {
  8014. UnicodeString Path;
  8015. if (DoFileFindDialog(RemoteDirView->Path, DoFindFiles, Path))
  8016. {
  8017. DoFocusRemotePath(Path);
  8018. }
  8019. }
  8020. //---------------------------------------------------------------------------
  8021. void __fastcall TCustomScpExplorerForm::UpdateTaskbarList(ITaskbarList3 * TaskbarList)
  8022. {
  8023. FTaskbarList = TaskbarList;
  8024. }
  8025. //---------------------------------------------------------------------------
  8026. void __fastcall TCustomScpExplorerForm::SessionsPageControlMouseDown(
  8027. TObject * /*Sender*/, TMouseButton Button, TShiftState /*Shift*/, int X, int Y)
  8028. {
  8029. int Index = SessionsPageControl->IndexOfTabAt(X, Y);
  8030. if (Index >= 0)
  8031. {
  8032. if (Button == mbRight)
  8033. {
  8034. SessionsPageControl->ActivePageIndex = Index;
  8035. // do not popup menu when we click "New session" tab
  8036. // (it would popup only after login dialog > auth dialog, what is strange)
  8037. if (SessionTabSwitched())
  8038. {
  8039. // copied from TControl.WMContextMenu
  8040. SendCancelMode(SessionsPageControl);
  8041. // explicit popup instead of using PopupMenu property
  8042. // to avoid menu to popup somewhere within SessionTabSwitched above,
  8043. // while connecting yet not-connected session and hence
  8044. // allowing an access to commands over not-completelly connected session
  8045. TPoint Point = SessionsPageControl->ClientToScreen(TPoint(X, Y));
  8046. TPopupMenu * PopupMenu = NonVisualDataModule->SessionsPopup;
  8047. PopupMenu->PopupComponent = SessionsPageControl;
  8048. PopupMenu->Popup(Point.x, Point.y);
  8049. }
  8050. }
  8051. else if (Button == mbLeft)
  8052. {
  8053. // "Mouse down" is raised only after tab is switched.
  8054. // If switching tab (switching session) takes long enough for user
  8055. // to actually release the button, "mouse down" is still raised,
  8056. // but we do not get "mouse up" event, so dragging is not cancelled,
  8057. // prevent that by not beginning dragging in the first place.
  8058. if (FLAGSET(GetAsyncKeyState(VK_LBUTTON), 0x8000))
  8059. {
  8060. // when user clicks the "+", we get mouse down only after the session
  8061. // is closed, when new session tab is already on X:Y, so dragging
  8062. // starts, prevent that
  8063. if (MilliSecondsBetween(Now(), FSessionsPageControlNewSessionTime) > 500)
  8064. {
  8065. TTerminal * Terminal = GetSessionTabTerminal(SessionsPageControl->Pages[Index]);
  8066. if (Terminal != NULL)
  8067. {
  8068. SessionsPageControl->BeginDrag(false);
  8069. }
  8070. }
  8071. }
  8072. }
  8073. else if (Button == mbMiddle)
  8074. {
  8075. // ignore middle-click for "New session tab"
  8076. TTerminal * Terminal = GetSessionTabTerminal(SessionsPageControl->Pages[Index]);
  8077. if (Terminal != NULL)
  8078. {
  8079. SessionsPageControl->ActivePageIndex = Index;
  8080. // Switch to session tab (has to be session tab, due to previous check)
  8081. if (DebugAlwaysTrue(SessionTabSwitched()))
  8082. {
  8083. CloseSession();
  8084. }
  8085. }
  8086. }
  8087. }
  8088. }
  8089. //---------------------------------------------------------------------------
  8090. void __fastcall TCustomScpExplorerForm::SessionsPageControlDragDrop(
  8091. TObject * /*Sender*/, TObject * /*Source*/, int X, int Y)
  8092. {
  8093. int Index = SessionsPageControl->IndexOfTabAt(X, Y);
  8094. // do not allow dropping on the "+" tab
  8095. TTerminal * TargetTerminal = GetSessionTabTerminal(SessionsPageControl->Pages[Index]);
  8096. if ((TargetTerminal != NULL) &&
  8097. (SessionsPageControl->ActivePage->PageIndex != Index))
  8098. {
  8099. Configuration->Usage->Inc(L"SessionTabMoves");
  8100. // this is almost redundant as we would recreate tabs in DoTerminalListChanged,
  8101. // but we want to actually prevent that to avoid flicker
  8102. SessionsPageControl->ActivePage->PageIndex = Index;
  8103. TTerminal * Terminal = GetSessionTabTerminal(SessionsPageControl->ActivePage);
  8104. TTerminalManager::Instance()->Move(Terminal, TargetTerminal);
  8105. }
  8106. }
  8107. //---------------------------------------------------------------------------
  8108. void __fastcall TCustomScpExplorerForm::SessionsPageControlDragOver(
  8109. TObject * Sender, TObject * Source, int X, int Y,
  8110. TDragState /*State*/, bool & Accept)
  8111. {
  8112. Accept = (Sender == Source);
  8113. if (Accept)
  8114. {
  8115. int Index = SessionsPageControl->IndexOfTabAt(X, Y);
  8116. TTerminal * Terminal = GetSessionTabTerminal(SessionsPageControl->Pages[Index]);
  8117. // do not allow dragging to the "+" tab
  8118. Accept = (Terminal != NULL);
  8119. }
  8120. }
  8121. //---------------------------------------------------------------------------
  8122. void __fastcall TCustomScpExplorerForm::SessionsDDDragOver(int /*KeyState*/,
  8123. const TPoint & Point, int & Effect)
  8124. {
  8125. int Index = SessionsPageControl->IndexOfTabAt(Point.X, Point.Y);
  8126. if (Index < 0)
  8127. {
  8128. Effect = DROPEFFECT_None;
  8129. }
  8130. else
  8131. {
  8132. TTerminal * TargetTerminal = GetSessionTabTerminal(SessionsPageControl->Pages[Index]);
  8133. // do not allow dropping on the "+" tab
  8134. if (TargetTerminal == NULL)
  8135. {
  8136. Effect = DROPEFFECT_None;
  8137. }
  8138. }
  8139. }
  8140. //---------------------------------------------------------------------------
  8141. void __fastcall TCustomScpExplorerForm::SessionsDDProcessDropped(
  8142. TObject * /*Sender*/, int /*KeyState*/, const TPoint & Point, int Effect)
  8143. {
  8144. int Index = SessionsPageControl->IndexOfTabAt(Point.X, Point.Y);
  8145. // do not allow dropping on the "+" tab
  8146. TTerminal * TargetTerminal = GetSessionTabTerminal(SessionsPageControl->Pages[Index]);
  8147. if (TargetTerminal != NULL)
  8148. {
  8149. DebugAssert(!IsFileControl(DropSourceControl, osRemote));
  8150. if (!IsFileControl(DropSourceControl, osRemote))
  8151. {
  8152. TTerminalManager::Instance()->ActiveTerminal = TargetTerminal;
  8153. RemoteFileControlDragDropFileOperation(SessionsPageControl, Effect,
  8154. // Why don't we use Terminal->CurrentDirectory directly?
  8155. TTerminalManager::Instance()->ActiveTerminal->CurrentDirectory, false);
  8156. }
  8157. }
  8158. }
  8159. //---------------------------------------------------------------------------
  8160. void __fastcall TCustomScpExplorerForm::QueueDDProcessDropped(
  8161. TObject * /*Sender*/, int /*KeyState*/, const TPoint & /*Point*/, int Effect)
  8162. {
  8163. // Downloads are handled in RemoteFileControlDDEnd
  8164. if (!IsFileControl(DropSourceControl, osRemote))
  8165. {
  8166. RemoteFileControlDragDropFileOperation(QueueView3, Effect,
  8167. Terminal->CurrentDirectory,
  8168. // force queue
  8169. true);
  8170. }
  8171. }
  8172. //---------------------------------------------------------------------------
  8173. void __fastcall TCustomScpExplorerForm::FormClose(TObject * /*Sender*/, TCloseAction & /*Action*/)
  8174. {
  8175. FShowing = false;
  8176. // Do not save empty workspace
  8177. if (WinConfiguration->AutoSaveWorkspace && (Terminal != NULL))
  8178. {
  8179. std::unique_ptr<TObjectList> DataList(DoCollectWorkspace());
  8180. UnicodeString Name = WorkspaceName();
  8181. DoSaveWorkspace(
  8182. Name, DataList.get(),
  8183. !Configuration->DisablePasswordStoring &&
  8184. WinConfiguration->AutoSaveWorkspacePasswords);
  8185. WinConfiguration->LastStoredSession = Name;
  8186. }
  8187. }
  8188. //---------------------------------------------------------------------------
  8189. void __fastcall TCustomScpExplorerForm::RemoteDirViewRead(TObject * /*Sender*/)
  8190. {
  8191. TManagedTerminal * ManagedTerminal =
  8192. dynamic_cast<TManagedTerminal *>(RemoteDirView->Terminal);
  8193. if (ManagedTerminal != NULL)
  8194. {
  8195. ManagedTerminal->DirectoryLoaded = Now();
  8196. }
  8197. }
  8198. //---------------------------------------------------------------------------
  8199. void __fastcall TCustomScpExplorerForm::DirViewSelectItem(TObject * Sender,
  8200. TListItem * /*Item*/, bool /*Selected*/)
  8201. {
  8202. TCustomDirView * DirView = reinterpret_cast<TCustomDirView *>(Sender);
  8203. switch (DirView->LastSelectMethod)
  8204. {
  8205. case smKeyboard:
  8206. Configuration->Usage->Inc(L"KeyboardSelections");
  8207. break;
  8208. case smMouse:
  8209. Configuration->Usage->Inc(L"MouseSelections");
  8210. break;
  8211. }
  8212. }
  8213. //---------------------------------------------------------------------------
  8214. int __fastcall TCustomScpExplorerForm::AddSessionColor(TColor Color)
  8215. {
  8216. if (Color != 0)
  8217. {
  8218. AddSessionColorImage(FSessionColors, Color, FSessionColorMaskImageIndex);
  8219. return FSessionColors->Count - 1;
  8220. }
  8221. else
  8222. {
  8223. return FSessionTabImageIndex;
  8224. }
  8225. }
  8226. //---------------------------------------------------------------------------
  8227. int __fastcall TCustomScpExplorerForm::AddFixedSessionImage(int GlyphsSourceIndex)
  8228. {
  8229. TPngImageCollectionItem * Item =
  8230. GlyphsModule->ExplorerImages->PngImages->Items[GlyphsSourceIndex];
  8231. FSessionColors->AddPng(Item->PngImage);
  8232. return FSessionColors->Count - 1;
  8233. }
  8234. //---------------------------------------------------------------------------
  8235. void __fastcall TCustomScpExplorerForm::AddFixedSessionImages()
  8236. {
  8237. FNewSessionTabImageIndex = AddFixedSessionImage(NonVisualDataModule->NewSessionAction->ImageIndex);
  8238. FSessionTabImageIndex = AddFixedSessionImage(SiteImageIndex);
  8239. FSessionColorMaskImageIndex = AddFixedSessionImage(SiteColorMaskImageIndex);
  8240. }
  8241. //---------------------------------------------------------------------------
  8242. void __fastcall TCustomScpExplorerForm::CollectItemsWithTextDisplayMode(TWinControl * Control)
  8243. {
  8244. for (int Index = 0; Index < Control->ControlCount; Index++)
  8245. {
  8246. TControl * AControl = Control->Controls[Index];
  8247. TWinControl * WinControl = dynamic_cast<TWinControl *>(AControl);
  8248. if (WinControl != NULL)
  8249. {
  8250. CollectItemsWithTextDisplayMode(WinControl);
  8251. }
  8252. TTBCustomToolbar * Toolbar = dynamic_cast<TTBCustomToolbar *>(AControl);
  8253. if (Toolbar != NULL)
  8254. {
  8255. // we care for top-level items only
  8256. TTBCustomItem * Items = Toolbar->Items;
  8257. for (int ItemIndex = 0; ItemIndex < Items->Count; ItemIndex++)
  8258. {
  8259. TTBCustomItem * Item = Items->Items[ItemIndex];
  8260. if (((Item->DisplayMode == nbdmImageAndText) ||
  8261. (dynamic_cast<TTBXLabelItem *>(Item) != NULL)) &&
  8262. EligibleForImageDisplayMode(Item))
  8263. {
  8264. FItemsWithTextDisplayMode.insert(Item);
  8265. }
  8266. }
  8267. }
  8268. }
  8269. }
  8270. //---------------------------------------------------------------------------
  8271. bool __fastcall TCustomScpExplorerForm::EligibleForImageDisplayMode(TTBCustomItem * /*Item*/)
  8272. {
  8273. return true;
  8274. }
  8275. //---------------------------------------------------------------------------
  8276. bool __fastcall TCustomScpExplorerForm::UpdateToolbarDisplayMode()
  8277. {
  8278. bool SelectiveToolbarText = WinConfiguration->SelectiveToolbarText;
  8279. TTBItemDisplayMode DisplayMode = (SelectiveToolbarText ? nbdmImageAndText : nbdmDefault);
  8280. typedef std::set<TTBCustomItem *> TItemsWithTextDisplayMode;
  8281. TItemsWithTextDisplayMode::iterator i = FItemsWithTextDisplayMode.begin();
  8282. bool Result = true;
  8283. while (Result && (i != FItemsWithTextDisplayMode.end()))
  8284. {
  8285. TTBCustomItem * Item = *i;
  8286. TTBXLabelItem * Label = dynamic_cast<TTBXLabelItem *>(Item);
  8287. if (Label != NULL)
  8288. {
  8289. // optimization
  8290. if (Label->Visible == SelectiveToolbarText)
  8291. {
  8292. Result = false;
  8293. }
  8294. else
  8295. {
  8296. Label->Visible = SelectiveToolbarText;
  8297. }
  8298. }
  8299. else
  8300. {
  8301. // optimization
  8302. if (Item->DisplayMode == DisplayMode)
  8303. {
  8304. Result = false;
  8305. }
  8306. else
  8307. {
  8308. Item->DisplayMode = DisplayMode;
  8309. }
  8310. }
  8311. ++i;
  8312. }
  8313. if (Result)
  8314. {
  8315. UpdateNewSessionTab();
  8316. }
  8317. return Result;
  8318. }
  8319. //---------------------------------------------------------------------------
  8320. void __fastcall TCustomScpExplorerForm::DisplaySystemContextMenu()
  8321. {
  8322. DebugFail();
  8323. }
  8324. //---------------------------------------------------------------------------
  8325. bool __fastcall TCustomScpExplorerForm::IsBusy()
  8326. {
  8327. // Among other, a lock level is non zero, while directory is being loaded,
  8328. // even when it happens as a result of dirview navigation,
  8329. // i.e. when TNonVisualDataModule is NOT FBusy.
  8330. // That's why the TNonVisualDataModule::GetBusy calls this method.
  8331. // Among other this prevents a panel auto update to occur while
  8332. // directory is changing.
  8333. return (FLockLevel > 0) || DirView(osCurrent)->IsEditing();
  8334. }
  8335. //---------------------------------------------------------------------------
  8336. Boolean __fastcall TCustomScpExplorerForm::AllowedAction(TAction * /*Action*/, TActionAllowed Allowed)
  8337. {
  8338. // While the window is disabled, we still seem to process menu shortcuts at least,
  8339. // so stop it at least here.
  8340. // See also TCustomScpExplorerForm::RemoteDirViewBusy
  8341. return
  8342. (Allowed == aaUpdate) ||
  8343. !NonVisualDataModule->Busy;
  8344. }
  8345. //---------------------------------------------------------------------------
  8346. void __fastcall TCustomScpExplorerForm::EditMenuItemPopup(TTBCustomItem * Sender, bool FromLink)
  8347. {
  8348. NonVisualDataModule->EditMenuItemPopup(Sender, FromLink);
  8349. }
  8350. //---------------------------------------------------------------------------
  8351. void __fastcall TCustomScpExplorerForm::DirViewBusy(TObject * /*Sender*/, int Busy, bool & State)
  8352. {
  8353. // This is somewhat redundant to LockWindow() call from
  8354. // TTerminalManager::TerminalReadDirectoryProgress.
  8355. // But disabling window is known not to block keyboard shorcuts
  8356. // (see TCustomScpExplorerForm::AllowedAction), this hopefully works.
  8357. if (Busy > 0)
  8358. {
  8359. if (NonVisualDataModule->Busy)
  8360. {
  8361. State = false;
  8362. }
  8363. else
  8364. {
  8365. NonVisualDataModule->StartBusy();
  8366. LockWindow();
  8367. }
  8368. }
  8369. else if (Busy < 0)
  8370. {
  8371. UnlockWindow();
  8372. NonVisualDataModule->EndBusy();
  8373. }
  8374. else
  8375. {
  8376. State = NonVisualDataModule->Busy;
  8377. }
  8378. }
  8379. //---------------------------------------------------------------------------