CustomScpExplorer.cpp 303 KB

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