CustomScpExplorer.cpp 303 KB

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