CustomScpExplorer.cpp 303 KB

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