CustomScpExplorer.cpp 302 KB

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