CustomScpExplorer.cpp 303 KB

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