CustomScpExplorer.cpp 286 KB

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