CustomScpExplorer.cpp 315 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703
  1. //---------------------------------------------------------------------------
  2. #define NO_WIN32_LEAN_AND_MEAN
  3. #include <vcl.h>
  4. #pragma hdrstop
  5. #include <Common.h>
  6. #include "CustomScpExplorer.h"
  7. #include <Bookmarks.h>
  8. #include <Interface.h>
  9. #include <Exceptions.h>
  10. #include <CoreMain.h>
  11. #include <FileSystems.h>
  12. #include <TextsCore.h>
  13. #include <TextsWin.h>
  14. #include <HelpWin.h>
  15. #include <VCLCommon.h>
  16. #include <Progress.h>
  17. #include <SynchronizeProgress.h>
  18. #include <DragExt.h>
  19. #include <WinApi.h>
  20. #include "GUITools.h"
  21. #include "NonVisual.h"
  22. #include "Glyphs.h"
  23. #include "Tools.h"
  24. #include "WinConfiguration.h"
  25. #include "TerminalManager.h"
  26. #include "EditorManager.h"
  27. #include "ProgParams.h"
  28. #include "Setup.h"
  29. #include <Consts.hpp>
  30. #include <DateUtils.hpp>
  31. #include <TB2Common.hpp>
  32. #include <DirectoryMonitor.hpp>
  33. //---------------------------------------------------------------------------
  34. #pragma package(smart_init)
  35. #pragma link "CustomDirView"
  36. #pragma link "CustomUnixDirView"
  37. #pragma link "IEListView"
  38. #pragma link "NortonLikeListView"
  39. #pragma link "UnixDirView"
  40. #pragma link "CustomDriveView"
  41. #pragma link "UnixDriveView"
  42. #pragma link "CustomDriveView"
  43. #pragma link "TB2Dock"
  44. #pragma link "TBXStatusBars"
  45. #pragma link "TB2Item"
  46. #pragma link "TB2Toolbar"
  47. #pragma link "ThemePageControl"
  48. #pragma link "PathLabel"
  49. #ifndef NO_RESOURCES
  50. #pragma resource "*.dfm"
  51. #endif
  52. //---------------------------------------------------------------------------
  53. #define SAVE_SELECTION(DIRVIEW) \
  54. UnicodeString FocusFile = L""; \
  55. UnicodeString LastFocusedFile = L""; \
  56. if (DIRVIEW->ItemFocused) LastFocusedFile = DIRVIEW->ItemFocused->Caption; \
  57. { TListItem * ClosestUnselected = DIRVIEW->ClosestUnselected(DIRVIEW->ItemFocused); \
  58. if (ClosestUnselected) FocusFile = ClosestUnselected->Caption; }
  59. #define RESTORE_SELECTION(DIRVIEW) \
  60. if (!LastFocusedFile.IsEmpty() && \
  61. (!DIRVIEW->ItemFocused || (DIRVIEW->ItemFocused->Caption != LastFocusedFile))) \
  62. { \
  63. TListItem *ItemToSelect = DIRVIEW->FindFileItem(FocusFile); \
  64. if (ItemToSelect) \
  65. { \
  66. DIRVIEW->ItemFocused = ItemToSelect; \
  67. DIRVIEW->ItemFocused->MakeVisible(False); \
  68. } \
  69. }
  70. //---------------------------------------------------------------------------
  71. #define WM_COMPONENT_HIDE (WM_WINSCP_USER + 4)
  72. static const int SessionPanelCount = 4;
  73. //---------------------------------------------------------------------------
  74. class TMutexGuard
  75. {
  76. public:
  77. TMutexGuard(HANDLE AMutex, int Message = MUTEX_RELEASE_TIMEOUT,
  78. int Timeout = 5000)
  79. {
  80. FMutex = NULL;
  81. unsigned long WaitResult = WaitForSingleObject(AMutex, Timeout);
  82. if (WaitResult == WAIT_TIMEOUT)
  83. {
  84. throw Exception(LoadStr(MUTEX_RELEASE_TIMEOUT));
  85. }
  86. else
  87. {
  88. FMutex = AMutex;
  89. }
  90. }
  91. ~TMutexGuard()
  92. {
  93. if (FMutex != NULL)
  94. {
  95. ReleaseMutex(FMutex);
  96. }
  97. }
  98. private:
  99. HANDLE FMutex;
  100. };
  101. //---------------------------------------------------------------------------
  102. class TWindowLock
  103. {
  104. public:
  105. TWindowLock(TCustomScpExplorerForm * Form) :
  106. FForm(Form)
  107. {
  108. FForm->LockWindow();
  109. }
  110. ~TWindowLock()
  111. {
  112. FForm->UnlockWindow();
  113. }
  114. private:
  115. TCustomScpExplorerForm * FForm;
  116. };
  117. //---------------------------------------------------------------------------
  118. struct TTransferOperationParam
  119. {
  120. TTransferOperationParam();
  121. UnicodeString TargetDirectory;
  122. bool Temp;
  123. bool DragDrop;
  124. int Options;
  125. TAutoSwitch Queue;
  126. };
  127. //---------------------------------------------------------------------------
  128. TTransferOperationParam::TTransferOperationParam()
  129. {
  130. Temp = false;
  131. DragDrop = false;
  132. Options = 0;
  133. Queue = asAuto;
  134. }
  135. //---------------------------------------------------------------------------
  136. class TTransferPresetNoteData : public TObject
  137. {
  138. public:
  139. UnicodeString Message;
  140. };
  141. //---------------------------------------------------------------------------
  142. class TTerminalNoteData : public TObject
  143. {
  144. public:
  145. TTerminal * Terminal;
  146. };
  147. //---------------------------------------------------------------------------
  148. __fastcall TCustomScpExplorerForm::TCustomScpExplorerForm(TComponent* Owner):
  149. FFormRestored(false),
  150. TForm(Owner)
  151. {
  152. FCurrentSide = osRemote;
  153. FEverShown = false;
  154. FDocks = new TList();
  155. RestoreParams();
  156. ConfigurationChanged();
  157. RemoteDirView->Invalidate();
  158. DebugAssert(NonVisualDataModule && !NonVisualDataModule->ScpExplorer);
  159. NonVisualDataModule->ScpExplorer = this;
  160. FAutoOperation = false;
  161. FOnFileOperationFinished = NULL;
  162. FForceExecution = false;
  163. FIgnoreNextDialogChar = 0;
  164. FErrorList = NULL;
  165. FSynchronizeProgressForm = NULL;
  166. FProgressForm = NULL;
  167. FRefreshLocalDirectory = false;
  168. FRefreshRemoteDirectory = false;
  169. FDDMoveSlipped = false;
  170. FDDExtMapFile = NULL;
  171. // CreateMutexW keeps failing with ERROR_NOACCESS
  172. FDDExtMutex = CreateMutexA(NULL, false, AnsiString(DRAG_EXT_MUTEX).c_str());
  173. DebugAssert(FDDExtMutex != NULL);
  174. FDDTargetControl = NULL;
  175. FDelayedDeletionTimer = NULL;
  176. FDelayedDeletionList = new TStringList();
  177. FDDFileList = NULL;
  178. FPendingTempSpaceWarn = false;
  179. FCapturedLog = NULL;
  180. FDragDropOperation = false;
  181. memset(&FHistoryMenu, 0, sizeof(FHistoryMenu));
  182. FAllowTransferPresetAutoSelect = true;
  183. FCopyParamDefault = L"";
  184. FSynchronizeController = NULL;
  185. FPendingQueueActionItem = NULL;
  186. FLockLevel = 0;
  187. FLockSuspendLevel = 0;
  188. FDisabledOnLockSuspend = false;
  189. FAlternativeDelete = false;
  190. FTrayIcon = new ::TTrayIcon(0);
  191. FTrayIcon->OnClick = TrayIconClick;
  192. FMaxQueueLength = 0;
  193. FLastContextPopupScreenPoint = TPoint(-1, -1);
  194. FTransferResumeList = NULL;
  195. FMoveToQueue = false;
  196. FStandaloneEditing = false;
  197. FOnFeedSynchronizeError = NULL;
  198. FNeedSession = false;
  199. FDoNotIdleCurrentTerminal = 0;
  200. FEditorManager = new TEditorManager();
  201. FEditorManager->OnFileChange = ExecutedFileChanged;
  202. FEditorManager->OnFileReload = ExecutedFileReload;
  203. FEditorManager->OnFileEarlyClosed = ExecutedFileEarlyClosed;
  204. FEditorManager->OnFileUploadComplete = ExecutedFileUploadComplete;
  205. FLocalEditors = new TList();
  206. FQueueStatus = NULL;
  207. FQueueStatusSection = new TCriticalSection();
  208. FQueueStatusInvalidated = false;
  209. FQueueItemInvalidated = false;
  210. FQueueActedItem = NULL;
  211. FQueueController = new TQueueController(QueueView3);
  212. FUserActionTimer = new TTimer(this);
  213. FUserActionTimer->Enabled = false;
  214. FUserActionTimer->Interval = 10;
  215. FUserActionTimer->OnTimer = UserActionTimer;
  216. FNotes = new TStringList();
  217. FNoteTimer = new TTimer(this);
  218. FNoteTimer->Enabled = false;
  219. FNoteTimer->OnTimer = NoteTimer;
  220. FOnNoteClick = NULL;
  221. FOle32Library = LoadLibrary(L"Ole32.dll");
  222. FDragMoveCursor = FOle32Library != NULL ?
  223. LoadCursor(FOle32Library, MAKEINTRESOURCE(2)) : NULL;
  224. UseSystemSettings(this);
  225. TTBXStringList * TransferList = dynamic_cast<TTBXStringList*>(
  226. static_cast<TObject*>(GetComponent(fcTransferList)));
  227. DebugAssert(TransferList != NULL);
  228. FTransferListHoverIndex = -1;
  229. TransferList->OnChange = TransferListChange;
  230. TransferList->OnDrawItem = TransferListDrawItem;
  231. SetSubmenu(dynamic_cast<TTBXCustomItem *>(static_cast<TObject *>(GetComponent(fcColorMenu))));
  232. SetSubmenu(NonVisualDataModule->ColorMenuItem);
  233. UseDesktopFont(SessionsPageControl);
  234. UpdateSessionsPageControlHeight();
  235. UseDesktopFont(RemoteDirView);
  236. UseDesktopFont(RemoteDriveView);
  237. UseDesktopFont(QueueView3);
  238. UseDesktopFont(QueueLabel);
  239. UseDesktopFont(RemoteStatusBar);
  240. reinterpret_cast<TLabel*>(QueueSplitter)->OnDblClick = QueueSplitterDblClick;
  241. QueueSplitter->ShowHint = true;
  242. RemotePanelSplitter->ShowHint = true;
  243. UpdateImages();
  244. FCustomCommandMenu = CreateTBXPopupMenu(this);
  245. FCustomCommandLocalFileList = NULL;
  246. FCustomCommandRemoteFileList = NULL;
  247. FSessionColors = new TPngImageList(this);
  248. FSessionColors->ColorDepth = cd32Bit;
  249. AddFixedSessionImages();
  250. SessionsPageControl->Images = FSessionColors;
  251. QueueLabel->FocusControl = QueueView3;
  252. UpdateQueueLabel();
  253. FRemoteDirViewWasFocused = true;
  254. CreateHiddenWindow();
  255. StartUpdates();
  256. }
  257. //---------------------------------------------------------------------------
  258. __fastcall TCustomScpExplorerForm::~TCustomScpExplorerForm()
  259. {
  260. // this has to be one of the very first things to do
  261. StopUpdateThread();
  262. SessionsPageControl->Images = NULL;
  263. SAFE_DESTROY(FSessionColors);
  264. SAFE_DESTROY(FSessionsDragDropFilesEx);
  265. delete FCustomCommandLocalFileList;
  266. delete FCustomCommandRemoteFileList;
  267. delete FCustomCommandMenu;
  268. delete FTrayIcon;
  269. FTrayIcon = NULL;
  270. FEditorManager->CloseInternalEditors(ForceCloseInternalEditor);
  271. delete FEditorManager;
  272. ForceCloseLocalEditors();
  273. delete FLocalEditors;
  274. HideFileFindDialog();
  275. if (FDelayedDeletionTimer)
  276. {
  277. DoDelayedDeletion(NULL);
  278. SAFE_DESTROY(FDelayedDeletionTimer);
  279. }
  280. SAFE_DESTROY(FDelayedDeletionList);
  281. // sometimes we do not get DDEnd so the list is not released
  282. SAFE_DESTROY(FDDFileList);
  283. DebugAssert(FSynchronizeController == NULL);
  284. CloseHandle(FDDExtMutex);
  285. FDDExtMutex = NULL;
  286. FreeLibrary(FOle32Library);
  287. FOle32Library = NULL;
  288. FDragMoveCursor = NULL;
  289. DebugAssert(!FErrorList);
  290. if (FEverShown)
  291. {
  292. // when window is never shown (like when running command-line operation),
  293. // particularly window site is not restored correctly (BoundsRect value set
  294. // in RestoreForm gets lost during handle allocation), so we do not want
  295. // it to be stored
  296. StoreParams();
  297. }
  298. Terminal = NULL;
  299. Queue = NULL;
  300. DebugAssert(NonVisualDataModule && (NonVisualDataModule->ScpExplorer == this));
  301. NonVisualDataModule->ScpExplorer = NULL;
  302. delete FQueueController;
  303. FQueueController = NULL;
  304. delete FQueueStatusSection;
  305. FQueueStatusSection = NULL;
  306. delete FQueueStatus;
  307. FQueueStatus = NULL;
  308. delete FUserActionTimer;
  309. FUserActionTimer = NULL;
  310. delete FNoteTimer;
  311. delete FNotes;
  312. delete FNoteData;
  313. SAFE_DESTROY(FDocks);
  314. SAFE_DESTROY(FHistoryMenu[0][0]);
  315. SAFE_DESTROY(FHistoryMenu[0][1]);
  316. SAFE_DESTROY(FHistoryMenu[1][0]);
  317. SAFE_DESTROY(FHistoryMenu[1][1]);
  318. if (FHiddenWindow != NULL)
  319. {
  320. DestroyWindow(FHiddenWindow);
  321. FHiddenWindow = NULL;
  322. }
  323. }
  324. //---------------------------------------------------------------------------
  325. void __fastcall TCustomScpExplorerForm::RefreshPanel(const UnicodeString & Session, const UnicodeString & Path)
  326. {
  327. std::unique_ptr<TSessionData> Data;
  328. if (!Session.IsEmpty())
  329. {
  330. bool DefaultsOnly;
  331. Data.reset(StoredSessions->ParseUrl(Session, NULL, DefaultsOnly));
  332. }
  333. TTerminalManager * Manager = TTerminalManager::Instance();
  334. for (int Index = 0; Index < Manager->Count; Index++)
  335. {
  336. TTerminal * Terminal = Manager->Terminals[Index];
  337. if (Session.IsEmpty() ||
  338. Terminal->SessionData->IsSameSite(Data.get()))
  339. {
  340. if (Path.IsEmpty())
  341. {
  342. Terminal->ClearCaches();
  343. }
  344. else
  345. {
  346. Terminal->DirectoryModified(Path, true);
  347. }
  348. }
  349. }
  350. // We should flag a pending refresh for the background terminals or busy foreground terminals
  351. if ((Terminal != NULL) && Terminal->Active &&
  352. CanCommandLineFromAnotherInstance() &&
  353. (Session.IsEmpty() || Terminal->SessionData->IsSameSite(Data.get())) &&
  354. (Path.IsEmpty() || UnixIsChildPath(Path, Terminal->CurrentDirectory)))
  355. {
  356. Terminal->ReloadDirectory();
  357. }
  358. }
  359. //---------------------------------------------------------------------------
  360. void __fastcall TCustomScpExplorerForm::WMCopyData(TMessage & Message)
  361. {
  362. PCOPYDATASTRUCT CopyData = reinterpret_cast<PCOPYDATASTRUCT>(Message.LParam);
  363. size_t MessageSize = sizeof(TCopyDataMessage);
  364. bool Result = DebugAlwaysTrue(CopyData->cbData == MessageSize);
  365. if (Result)
  366. {
  367. const TCopyDataMessage & Message = *reinterpret_cast<const TCopyDataMessage *>(CopyData->lpData);
  368. Result = (Message.Version == TCopyDataMessage::Version1);
  369. if (Result)
  370. {
  371. switch (Message.Command)
  372. {
  373. case TCopyDataMessage::CommandCanCommandLine:
  374. Result = CanCommandLineFromAnotherInstance();
  375. break;
  376. case TCopyDataMessage::CommandCommandLine:
  377. {
  378. UnicodeString CommandLine(Message.CommandLine);
  379. Result = CommandLineFromAnotherInstance(CommandLine);
  380. }
  381. break;
  382. case TCopyDataMessage::RefreshPanel:
  383. RefreshPanel(Message.Refresh.Session, Message.Refresh.Path);
  384. break;
  385. case TCopyDataMessage::MainWindowCheck:
  386. Result = true;
  387. break;
  388. default:
  389. Result = false;
  390. break;
  391. }
  392. }
  393. }
  394. Message.Result = Result ? 1 : 0;
  395. }
  396. //---------------------------------------------------------------------------
  397. void __fastcall TCustomScpExplorerForm::CreateHiddenWindow()
  398. {
  399. WNDCLASS WindowClass = {0};
  400. WindowClass.lpfnWndProc = DefWindowProc;
  401. WindowClass.hInstance = HInstance;
  402. WindowClass.lpszClassName = HIDDEN_WINDOW_NAME;
  403. FHiddenWindow = NULL;
  404. if (RegisterClass(&WindowClass))
  405. {
  406. FHiddenWindow = CreateWindow(HIDDEN_WINDOW_NAME, L"",
  407. WS_POPUP, 0, 0, 0, 0, 0, 0, HInstance, NULL);
  408. }
  409. }
  410. //---------------------------------------------------------------------------
  411. bool __fastcall TCustomScpExplorerForm::CanConsole()
  412. {
  413. return (Terminal != NULL) && (Terminal->IsCapable[fcAnyCommand] || Terminal->IsCapable[fcSecondaryShell]);
  414. }
  415. //---------------------------------------------------------------------------
  416. bool __fastcall TCustomScpExplorerForm::CanCommandLineFromAnotherInstance()
  417. {
  418. bool Result = Visible && !NonVisualDataModule->Busy;
  419. return Result;
  420. }
  421. //---------------------------------------------------------------------------
  422. bool __fastcall TCustomScpExplorerForm::CommandLineFromAnotherInstance(
  423. const UnicodeString & CommandLine)
  424. {
  425. TProgramParams Params(CommandLine);
  426. bool Result = CanCommandLineFromAnotherInstance() && DebugAlwaysTrue(Params.ParamCount > 0);
  427. if (Result)
  428. {
  429. NonVisualDataModule->StartBusy();
  430. try
  431. {
  432. // this action is initiated from another process,
  433. // so it's likely that our window is not visible,
  434. // and user won't see what is going on
  435. Application->BringToFront();
  436. // reload sessions as we may be asked to open a session
  437. // just stored by another instance
  438. StoredSessions->Reload();
  439. UnicodeString SessionName = Params.Param[1];
  440. std::unique_ptr<TObjectList> DataList(new TObjectList());
  441. UnicodeString DownloadFile; // unused
  442. try
  443. {
  444. GetLoginData(SessionName, &Params, DataList.get(), DownloadFile, true, this);
  445. }
  446. catch (EAbort &)
  447. {
  448. // Happens when opening session in PuTTY and in other situations,
  449. // in which we do not want to fail WM_COPYDATA, as that would cause master instance
  450. // to process the command-line on its own (or to pass it to yet another instance)
  451. }
  452. if (DataList->Count > 0)
  453. {
  454. TTerminalManager * Manager = TTerminalManager::Instance();
  455. Manager->ActiveTerminal = Manager->NewTerminals(DataList.get());
  456. }
  457. }
  458. __finally
  459. {
  460. NonVisualDataModule->EndBusy();
  461. }
  462. }
  463. return Result;
  464. }
  465. //---------------------------------------------------------------------------
  466. void __fastcall TCustomScpExplorerForm::SetTerminal(TTerminal * value)
  467. {
  468. if (FTerminal != value)
  469. {
  470. TerminalChanging();
  471. FTerminal = value;
  472. bool PrevAllowTransferPresetAutoSelect = FAllowTransferPresetAutoSelect;
  473. FAllowTransferPresetAutoSelect = false;
  474. try
  475. {
  476. TerminalChanged();
  477. }
  478. __finally
  479. {
  480. FAllowTransferPresetAutoSelect = PrevAllowTransferPresetAutoSelect;
  481. }
  482. if (Terminal != NULL)
  483. {
  484. TransferPresetAutoSelect();
  485. }
  486. }
  487. }
  488. //---------------------------------------------------------------------------
  489. void __fastcall TCustomScpExplorerForm::TerminalChanging()
  490. {
  491. if (FTerminal != NULL)
  492. {
  493. UpdateTerminal(Terminal);
  494. }
  495. }
  496. //---------------------------------------------------------------------------
  497. void __fastcall TCustomScpExplorerForm::TerminalChanged()
  498. {
  499. RemoteDirView->Terminal = Terminal;
  500. NonVisualDataModule->ResetQueueOnceEmptyOperation();
  501. TManagedTerminal * ManagedTerminal = NULL;
  502. if (Terminal != NULL)
  503. {
  504. if (Terminal->Active)
  505. {
  506. Terminal->RefreshDirectory();
  507. }
  508. ManagedTerminal = dynamic_cast<TManagedTerminal *>(Terminal);
  509. DebugAssert(ManagedTerminal != NULL);
  510. if (WinConfiguration->PreservePanelState)
  511. {
  512. if (ManagedTerminal->RemoteExplorerState != NULL)
  513. {
  514. RemoteDirView->RestoreState(ManagedTerminal->RemoteExplorerState);
  515. }
  516. else
  517. {
  518. RemoteDirView->ClearState();
  519. }
  520. }
  521. InitStatusBar();
  522. }
  523. DoTerminalListChanged(false);
  524. if (ManagedTerminal != NULL)
  525. {
  526. // this has to be set only after the tab is switched from DoTerminalListChanged,
  527. // otherwise we are changing color of wrong tab
  528. SessionColor = (TColor)ManagedTerminal->StateData->Color;
  529. }
  530. UpdateTransferList();
  531. // Update panels Enable state before refreshing the labels
  532. UpdateControls();
  533. }
  534. //---------------------------------------------------------------------------
  535. void __fastcall TCustomScpExplorerForm::SetQueue(TTerminalQueue * value)
  536. {
  537. if (Queue != value)
  538. {
  539. if (FQueue != NULL)
  540. {
  541. FQueue->OnListUpdate = NULL;
  542. FQueue->OnQueueItemUpdate = NULL;
  543. }
  544. FQueue = value;
  545. if (FQueue != NULL)
  546. {
  547. DebugAssert(FQueue->OnListUpdate == NULL);
  548. FQueue->OnListUpdate = QueueListUpdate;
  549. DebugAssert(FQueue->OnQueueItemUpdate == NULL);
  550. FQueue->OnQueueItemUpdate = QueueItemUpdate;
  551. }
  552. QueueChanged();
  553. }
  554. }
  555. //---------------------------------------------------------------------------
  556. void __fastcall TCustomScpExplorerForm::QueueView3Deletion(TObject * /*Sender*/,
  557. TListItem * Item)
  558. {
  559. if (FQueueActedItem == Item)
  560. {
  561. FQueueActedItem = NULL;
  562. if ((QueueView3->PopupMenu != NULL) &&
  563. (QueueView3->PopupMenu->PopupComponent == QueueView3))
  564. {
  565. // rather "trick", suggested by Jordan on jrsoftware.toolbar2000
  566. ReleaseCapture();
  567. }
  568. }
  569. if (Item->Data == FPendingQueueActionItem)
  570. {
  571. FPendingQueueActionItem = NULL;
  572. }
  573. }
  574. //---------------------------------------------------------------------------
  575. void __fastcall TCustomScpExplorerForm::UpdateQueueStatus(bool QueueChanging)
  576. {
  577. {
  578. TGuard Guard(FQueueStatusSection);
  579. FQueueStatusInvalidated = false;
  580. if (FQueue != NULL)
  581. {
  582. FQueueStatus = FQueue->CreateStatus(FQueueStatus);
  583. }
  584. }
  585. if ((FQueueStatus != NULL) && (FQueueStatus->Count > FMaxQueueLength))
  586. {
  587. FMaxQueueLength = FQueueStatus->Count;
  588. Configuration->Usage->SetMax(L"MaxQueueLength", FMaxQueueLength);
  589. }
  590. FQueueController->UpdateQueueStatus(FQueueStatus);
  591. SetQueueProgress();
  592. UpdateQueueView();
  593. bool IsEmpty = (FQueueStatus == NULL) || (FQueueStatus->Count == 0);
  594. if (IsEmpty && (Terminal != NULL))
  595. {
  596. TOnceDoneOperation OnceDoneOperation =
  597. NonVisualDataModule->CurrentQueueOnceEmptyOperation();
  598. NonVisualDataModule->ResetQueueOnceEmptyOperation();
  599. if ((FQueue != NULL) && !WinConfiguration->EnableQueueByDefault && !QueueChanging)
  600. {
  601. FQueue->Enabled = false;
  602. }
  603. if ((OnceDoneOperation != odoIdle) && !NonVisualDataModule->Busy)
  604. {
  605. Terminal->CloseOnCompletion(OnceDoneOperation, LoadStr(CLOSED_ON_QUEUE_EMPTY));
  606. }
  607. }
  608. }
  609. //---------------------------------------------------------------------------
  610. UnicodeString __fastcall TCustomScpExplorerForm::GetQueueProgressTitle()
  611. {
  612. UnicodeString Result;
  613. if (FQueueStatus != NULL)
  614. {
  615. if (FQueueStatus->IsOnlyOneActiveAndNoPending())
  616. {
  617. TFileOperationProgressType * ProgressData =
  618. FQueueStatus->Items[FQueueStatus->DoneCount]->ProgressData;
  619. if ((ProgressData != NULL) && ProgressData->InProgress)
  620. {
  621. Result = TProgressForm::ProgressStr(ProgressData);
  622. }
  623. }
  624. else if (FQueueStatus->ActiveAndPendingPrimaryCount > 1)
  625. {
  626. Result = FMTLOAD(PROGRESS_IN_QUEUE, (FQueueStatus->ActiveAndPendingPrimaryCount));
  627. }
  628. }
  629. return Result;
  630. }
  631. //---------------------------------------------------------------------------
  632. void __fastcall TCustomScpExplorerForm::UpdateQueueView()
  633. {
  634. ComponentVisible[fcQueueView] =
  635. (WinConfiguration->QueueView.Show == qvShow) ||
  636. ((WinConfiguration->QueueView.Show == qvHideWhenEmpty) &&
  637. (FQueueStatus != NULL) && (FQueueStatus->Count > 0));
  638. }
  639. //---------------------------------------------------------------------------
  640. void __fastcall TCustomScpExplorerForm::QueueChanged()
  641. {
  642. if (FQueueStatus != NULL)
  643. {
  644. delete FQueueStatus;
  645. FQueueStatus = NULL;
  646. }
  647. UpdateQueueStatus(true);
  648. }
  649. //---------------------------------------------------------------------------
  650. void __fastcall TCustomScpExplorerForm::QueueListUpdate(TTerminalQueue * Queue)
  651. {
  652. if (FQueue == Queue)
  653. {
  654. FQueueStatusInvalidated = true;
  655. }
  656. }
  657. //---------------------------------------------------------------------------
  658. void __fastcall TCustomScpExplorerForm::QueueItemUpdate(TTerminalQueue * Queue,
  659. TQueueItem * Item)
  660. {
  661. if (FQueue == Queue)
  662. {
  663. TGuard Guard(FQueueStatusSection);
  664. // this may be running in parallel with QueueChanged
  665. if (FQueueStatus != NULL)
  666. {
  667. TQueueItemProxy * QueueItem = FQueueStatus->FindByQueueItem(Item);
  668. if ((Item->Status == TQueueItem::qsDone) && (Terminal != NULL))
  669. {
  670. FRefreshLocalDirectory = (QueueItem == NULL) ||
  671. (!QueueItem->Info->ModifiedLocal.IsEmpty());
  672. FRefreshRemoteDirectory = (QueueItem == NULL) ||
  673. (!QueueItem->Info->ModifiedRemote.IsEmpty());
  674. }
  675. if (QueueItem != NULL)
  676. {
  677. QueueItem->UserData = (void*)true;
  678. FQueueItemInvalidated = true;
  679. }
  680. }
  681. }
  682. }
  683. //---------------------------------------------------------------------------
  684. bool __fastcall TCustomScpExplorerForm::IsQueueAutoPopup()
  685. {
  686. // during standalone editing, we have no way to see/control queue,
  687. // so we have to always popup prompts automatically
  688. return FStandaloneEditing || GUIConfiguration->QueueAutoPopup;
  689. }
  690. //---------------------------------------------------------------------------
  691. void __fastcall TCustomScpExplorerForm::RefreshQueueItems()
  692. {
  693. if (FQueueStatus != NULL)
  694. {
  695. bool QueueAutoPopup = IsQueueAutoPopup();
  696. bool NeedRefresh = FQueueController->NeedRefresh();
  697. bool Refresh = FQueueItemInvalidated || NeedRefresh;
  698. FQueueItemInvalidated = false;
  699. int Limit = Refresh ? FQueueStatus->Count : FQueueStatus->DoneAndActiveCount;
  700. bool Updated = false;
  701. TQueueItemProxy * QueueItem;
  702. bool WasUserAction;
  703. for (int Index = 0; Index < Limit; Index++)
  704. {
  705. bool Update = false;
  706. QueueItem = FQueueStatus->Items[Index];
  707. WasUserAction = TQueueItem::IsUserActionStatus(QueueItem->Status);
  708. if (!NonVisualDataModule->Busy && QueueAutoPopup && WasUserAction &&
  709. (FPendingQueueActionItem == NULL))
  710. {
  711. FPendingQueueActionItem = QueueItem;
  712. FUserActionTimer->Enabled = true;
  713. }
  714. if ((bool)QueueItem->UserData)
  715. {
  716. QueueItem->UserData = (void*)false;
  717. QueueItem->Update();
  718. Updated = true;
  719. Update = true;
  720. }
  721. else if (FQueueController->QueueItemNeedsFrequentRefresh(QueueItem) || NeedRefresh)
  722. {
  723. Update = true;
  724. }
  725. if (Update)
  726. {
  727. FQueueController->RefreshQueueItem(QueueItem);
  728. }
  729. }
  730. if (Updated)
  731. {
  732. NonVisualDataModule->UpdateNonVisibleActions();
  733. SetQueueProgress();
  734. }
  735. }
  736. }
  737. //---------------------------------------------------------------------------
  738. void __fastcall TCustomScpExplorerForm::SetTaskbarListProgressState(TBPFLAG Flags)
  739. {
  740. // Could now use Application->MainFormHandle, now that we implement Application->OnGetMainFormHandle
  741. FTaskbarList->SetProgressState(GetMainForm()->Handle, Flags);
  742. }
  743. //---------------------------------------------------------------------------
  744. void __fastcall TCustomScpExplorerForm::SetTaskbarListProgressValue(TFileOperationProgressType * ProgressData)
  745. {
  746. if (ProgressData->Operation != foCalculateSize)
  747. {
  748. // implies TBPF_NORMAL
  749. FTaskbarList->SetProgressValue(GetMainForm()->Handle, ProgressData->OverallProgress(), 100);
  750. }
  751. else
  752. {
  753. SetTaskbarListProgressState(TBPF_INDETERMINATE);
  754. }
  755. }
  756. //---------------------------------------------------------------------------
  757. void __fastcall TCustomScpExplorerForm::SetQueueProgress()
  758. {
  759. TTerminalManager::Instance()->QueueStatusUpdated();
  760. if ((FTaskbarList != NULL) && (FProgressForm == NULL))
  761. {
  762. if ((FQueueStatus != NULL) && (FQueueStatus->ActiveAndPendingPrimaryCount > 0))
  763. {
  764. if (FQueueStatus->IsOnlyOneActiveAndNoPending())
  765. {
  766. TFileOperationProgressType * ProgressData = NULL;
  767. if (FQueueStatus->Items[FQueueStatus->DoneCount] != NULL)
  768. {
  769. ProgressData = FQueueStatus->Items[FQueueStatus->DoneCount]->ProgressData;
  770. }
  771. if ((ProgressData != NULL) &&
  772. ProgressData->InProgress)
  773. {
  774. SetTaskbarListProgressValue(ProgressData);
  775. }
  776. else
  777. {
  778. SetTaskbarListProgressState(TBPF_NOPROGRESS);
  779. }
  780. }
  781. else
  782. {
  783. SetTaskbarListProgressState(TBPF_INDETERMINATE);
  784. }
  785. }
  786. else
  787. {
  788. SetTaskbarListProgressState(TBPF_NOPROGRESS);
  789. }
  790. }
  791. UpdateQueueLabel();
  792. }
  793. //---------------------------------------------------------------------------
  794. void __fastcall TCustomScpExplorerForm::UpdateQueueLabel()
  795. {
  796. UnicodeString Caption = LoadStr(QUEUE_CAPTION);
  797. if (FQueueStatus != NULL)
  798. {
  799. int ActiveAndPendingPrimaryCount = FQueueStatus->ActiveAndPendingPrimaryCount;
  800. if (ActiveAndPendingPrimaryCount > 0)
  801. {
  802. Caption = FORMAT("%s (%d)", (Caption, ActiveAndPendingPrimaryCount));
  803. }
  804. }
  805. QueueLabel->Caption = Caption;
  806. }
  807. //---------------------------------------------------------------------------
  808. void __fastcall TCustomScpExplorerForm::UpdateTransferList()
  809. {
  810. TTBXStringList * TransferList = dynamic_cast<TTBXStringList*>(
  811. static_cast<TComponent*>(GetComponent(fcTransferList)));
  812. TTBXDropDownItem * TransferDropDown = dynamic_cast<TTBXDropDownItem*>(
  813. static_cast<TComponent*>(GetComponent(fcTransferDropDown)));
  814. TransferList->Strings->BeginUpdate();
  815. try
  816. {
  817. TransferList->Strings->Assign(GUIConfiguration->CopyParamList->NameList);
  818. TransferList->Strings->Insert(0, StripHotkey(LoadStr(COPY_PARAM_DEFAULT)));
  819. TransferList->ItemIndex = GUIConfiguration->CopyParamIndex + 1;
  820. if (FTransferDropDownHint.IsEmpty())
  821. {
  822. FTransferDropDownHint = TransferDropDown->Hint;
  823. }
  824. // this way we get name for "default" settings (COPY_PARAM_DEFAULT)
  825. UnicodeString Name = TransferList->Strings->Strings[TransferList->ItemIndex];
  826. TransferDropDown->Text = StripHotkey(Name);
  827. TransferDropDown->Hint = FORMAT(L"%s|%s:\n%s",
  828. (FTransferDropDownHint, StripHotkey(Name),
  829. GUIConfiguration->CurrentCopyParam.GetInfoStr(L"; ",
  830. FLAGMASK(Terminal != NULL, Terminal->UsableCopyParamAttrs(0).General))));
  831. // update the label, otherwise when it is updated only on the first draw
  832. // of the list, it is drawn "bold" for some reason
  833. FTransferListHoverIndex = TransferList->ItemIndex;
  834. UpdateTransferLabel();
  835. }
  836. __finally
  837. {
  838. TransferList->Strings->EndUpdate();
  839. }
  840. }
  841. //---------------------------------------------------------------------------
  842. void __fastcall TCustomScpExplorerForm::UpdateCustomCommandsToolbar()
  843. {
  844. TTBXToolbar * Toolbar = dynamic_cast<TTBXToolbar *>(GetComponent(fcCustomCommandsBand));
  845. DebugAssert(Toolbar != NULL);
  846. NonVisualDataModule->UpdateCustomCommandsToolbar(Toolbar);
  847. }
  848. //---------------------------------------------------------------------------
  849. void __fastcall TCustomScpExplorerForm::UpdateActions()
  850. {
  851. TForm::UpdateActions();
  852. if (ComponentVisible[fcCustomCommandsBand])
  853. {
  854. UpdateCustomCommandsToolbar();
  855. }
  856. }
  857. //---------------------------------------------------------------------------
  858. void __fastcall TCustomScpExplorerForm::UpdateSessionsPageControlHeight()
  859. {
  860. SessionsPageControl->Height = SessionsPageControl->GetTabsHeight();
  861. }
  862. //---------------------------------------------------------------------------
  863. void __fastcall TCustomScpExplorerForm::ConfigurationChanged()
  864. {
  865. DebugAssert(Configuration && RemoteDirView);
  866. RemoteDirView->DDAllowMove = WinConfiguration->DDAllowMoveInit;
  867. RemoteDirView->DimmHiddenFiles = WinConfiguration->DimmHiddenFiles;
  868. RemoteDirView->ShowHiddenFiles = WinConfiguration->ShowHiddenFiles;
  869. RemoteDirView->FormatSizeBytes = WinConfiguration->FormatSizeBytes;
  870. RemoteDirView->ShowInaccesibleDirectories = WinConfiguration->ShowInaccesibleDirectories;
  871. RemoteDirView->NaturalOrderNumericalSorting = WinConfiguration->NaturalOrderNumericalSorting;
  872. if (RemoteDirView->RowSelect != WinConfiguration->FullRowSelect)
  873. {
  874. RemoteDirView->RowSelect = WinConfiguration->FullRowSelect;
  875. // selection is not redrawn automatically when RowSelect changes
  876. RemoteDirView->Invalidate();
  877. }
  878. RemoteDriveView->DDAllowMove = WinConfiguration->DDAllowMoveInit;
  879. RemoteDriveView->DimmHiddenDirs = WinConfiguration->DimmHiddenFiles;
  880. RemoteDriveView->ShowHiddenDirs = WinConfiguration->ShowHiddenFiles;
  881. RemoteDriveView->ShowInaccesibleDirectories = WinConfiguration->ShowInaccesibleDirectories;
  882. UpdateSessionsPageControlHeight();
  883. SetDockAllowDrag(!WinConfiguration->LockToolbars);
  884. UpdateToolbarDisplayMode();
  885. UpdateTransferList();
  886. if (Terminal != NULL)
  887. {
  888. TransferPresetAutoSelect();
  889. }
  890. UpdateQueueView();
  891. UpdateControls();
  892. // this can be called even before constuctor finishes.
  893. if (FEditorManager != NULL)
  894. {
  895. FEditorManager->ProcessFiles(FileConfigurationChanged, NULL);
  896. }
  897. // this can be called even before constuctor finishes.
  898. if (FLocalEditors != NULL)
  899. {
  900. for (int Index = 0; Index < FLocalEditors->Count; Index++)
  901. {
  902. ReconfigureEditorForm(static_cast<TForm *>(FLocalEditors->Items[Index]));
  903. }
  904. }
  905. if (ComponentVisible[fcCustomCommandsBand])
  906. {
  907. UpdateCustomCommandsToolbar();
  908. }
  909. // show only when keeping queue items forever,
  910. // otherwise, its enough to have in in the context menu
  911. QueueDeleteAllDoneQueueToolbarItem->Visible =
  912. WinConfiguration->QueueKeepDoneItems && (WinConfiguration->QueueKeepDoneItemsFor < 0);
  913. }
  914. //---------------------------------------------------------------------------
  915. void __fastcall TCustomScpExplorerForm::FileConfigurationChanged(
  916. const UnicodeString FileName, TEditedFileData * /*Data*/, TObject * Token,
  917. void * /*Arg*/)
  918. {
  919. if (Token != NULL)
  920. {
  921. TForm * Editor = dynamic_cast<TForm*>(Token);
  922. DebugAssert(Editor != NULL);
  923. ReconfigureEditorForm(Editor);
  924. }
  925. }
  926. //---------------------------------------------------------------------------
  927. void __fastcall TCustomScpExplorerForm::EnableDDTransferConfirmation(TObject * /*Sender*/)
  928. {
  929. WinConfiguration->DDTransferConfirmation = asOn;
  930. }
  931. //---------------------------------------------------------------------------
  932. void __fastcall TCustomScpExplorerForm::CopyParamDialogAfter(
  933. TTransferDirection /*Direction*/, bool /*Temp*/, const UnicodeString & /*TargetDirectory*/)
  934. {
  935. // noop
  936. }
  937. //---------------------------------------------------------------------------
  938. bool __fastcall TCustomScpExplorerForm::CopyParamDialog(
  939. TTransferDirection Direction, TTransferType Type, bool Temp,
  940. TStrings * FileList, UnicodeString & TargetDirectory, TGUICopyParamType & CopyParam,
  941. bool Confirm, bool DragDrop, int Options)
  942. {
  943. bool Result = true;
  944. DebugAssert(Terminal && Terminal->Active);
  945. // Temp means d&d here so far, may change in future!
  946. if (Temp && (Direction == tdToLocal) && (Type == ttMove) &&
  947. !WinConfiguration->DDAllowMove)
  948. {
  949. TMessageParams Params(mpNeverAskAgainCheck);
  950. unsigned int Answer = MessageDialog(LoadStr(DND_DOWNLOAD_MOVE_WARNING), qtWarning,
  951. qaOK | qaCancel, HELP_DND_DOWNLOAD_MOVE_WARNING, &Params);
  952. if (Answer == qaNeverAskAgain)
  953. {
  954. WinConfiguration->DDAllowMove = true;
  955. }
  956. else if (Answer == qaCancel)
  957. {
  958. Result = false;
  959. }
  960. }
  961. // these parameters are known in advance
  962. int Params =
  963. FLAGMASK(Type == ttMove, cpDelete);
  964. bool ToTemp = (Temp && (Direction == tdToLocal));
  965. if (Result && Confirm && WinConfiguration->ConfirmTransferring)
  966. {
  967. bool DisableNewerOnly =
  968. (!Terminal->IsCapable[fcNewerOnlyUpload] && (Direction == tdToRemote)) ||
  969. ToTemp;
  970. Options |=
  971. FLAGMASK(ToTemp, coTemp) |
  972. FLAGMASK(!Terminal->IsCapable[fsBackgroundTransfers], coDisableQueue) |
  973. coDoNotShowAgain;
  974. TUsableCopyParamAttrs UsableCopyParamAttrs = Terminal->UsableCopyParamAttrs(Params);
  975. int CopyParamAttrs = (Direction == tdToRemote ?
  976. UsableCopyParamAttrs.Upload : UsableCopyParamAttrs.Download) |
  977. FLAGMASK(DisableNewerOnly, cpaNoNewerOnly);
  978. int OutputOptions =
  979. FLAGMASK(DragDrop && (WinConfiguration->DDTransferConfirmation == asAuto),
  980. cooDoNotShowAgain);
  981. std::unique_ptr<TSessionData> SessionData(SessionDataForCode());
  982. Result = DoCopyDialog(Direction == tdToRemote, Type == ttMove,
  983. FileList, TargetDirectory, &CopyParam, Options, CopyParamAttrs, SessionData.get(), &OutputOptions);
  984. if (Result)
  985. {
  986. if (FLAGSET(OutputOptions, cooDoNotShowAgain))
  987. {
  988. if (DragDrop)
  989. {
  990. if (WinConfiguration->DDTransferConfirmation == asAuto)
  991. {
  992. PopupTrayBalloon(NULL, LoadStr(DD_TRANSFER_CONFIRM_OFF2), qtInformation,
  993. NULL, 0, EnableDDTransferConfirmation, NULL);
  994. }
  995. WinConfiguration->DDTransferConfirmation = asOff;
  996. }
  997. else
  998. {
  999. WinConfiguration->ConfirmTransferring = false;
  1000. }
  1001. }
  1002. else
  1003. {
  1004. // User exclicitly unchecked "do not show again",
  1005. // so show him the dialog the next time
  1006. if (DragDrop && (WinConfiguration->DDTransferConfirmation == asAuto))
  1007. {
  1008. WinConfiguration->DDTransferConfirmation = asOn;
  1009. }
  1010. }
  1011. CopyParamDialogAfter(Direction, Temp, TargetDirectory);
  1012. }
  1013. }
  1014. if (Result && CopyParam.Queue && !ToTemp && Terminal->IsCapable[fsBackgroundTransfers])
  1015. {
  1016. Configuration->Usage->Inc(L"TransfersOnBackground");
  1017. // these parameter are known only after transfer dialog
  1018. Params |=
  1019. (CopyParam.QueueNoConfirmation ? cpNoConfirmation : 0);
  1020. AddQueueItem(Queue, Direction, FileList, TargetDirectory, CopyParam, Params);
  1021. Result = false;
  1022. ClearTransferSourceSelection(Direction);
  1023. }
  1024. return Result;
  1025. }
  1026. //---------------------------------------------------------------------------
  1027. void __fastcall TCustomScpExplorerForm::ClearTransferSourceSelection(TTransferDirection Direction)
  1028. {
  1029. if (FOnFileOperationFinished != NULL)
  1030. {
  1031. FOnFileOperationFinished(UnicodeString(), true);
  1032. }
  1033. else
  1034. {
  1035. TOperationSide Side = ((Direction == tdToRemote) ? osLocal : osRemote);
  1036. if (HasDirView[Side])
  1037. {
  1038. DirView(Side)->SelectAll(smNone);
  1039. }
  1040. }
  1041. }
  1042. //---------------------------------------------------------------------------
  1043. void __fastcall TCustomScpExplorerForm::AddQueueItem(
  1044. TTerminalQueue * Queue, TTransferDirection Direction, TStrings * FileList,
  1045. const UnicodeString TargetDirectory, const TGUICopyParamType & CopyParam,
  1046. int Params)
  1047. {
  1048. DebugAssert(Queue != NULL);
  1049. bool SingleFile = false;
  1050. if (FileList->Count == 1)
  1051. {
  1052. if (Direction == tdToRemote)
  1053. {
  1054. UnicodeString FileName = FileList->Strings[0];
  1055. SingleFile = FileExists(ApiPath(FileName));
  1056. }
  1057. else
  1058. {
  1059. TRemoteFile * File = static_cast<TRemoteFile *>(FileList->Objects[0]);
  1060. SingleFile = !File->IsDirectory;
  1061. }
  1062. }
  1063. TQueueItem * QueueItem;
  1064. if (Direction == tdToRemote)
  1065. {
  1066. QueueItem = new TUploadQueueItem(Terminal, FileList, TargetDirectory,
  1067. &CopyParam, Params, SingleFile, CopyParam.QueueParallel);
  1068. }
  1069. else
  1070. {
  1071. QueueItem = new TDownloadQueueItem(Terminal, FileList, TargetDirectory,
  1072. &CopyParam, Params, SingleFile, CopyParam.QueueParallel);
  1073. }
  1074. AddQueueItem(Queue, QueueItem, Terminal);
  1075. }
  1076. //---------------------------------------------------------------------------
  1077. void __fastcall TCustomScpExplorerForm::AddQueueItem(TTerminalQueue * Queue, TQueueItem * QueueItem, TTerminal * Terminal)
  1078. {
  1079. if (Queue->IsEmpty)
  1080. {
  1081. TManagedTerminal * ManagedTerminal = DebugNotNull(dynamic_cast<TManagedTerminal *>(Terminal));
  1082. ManagedTerminal->QueueOperationStart = Now();
  1083. }
  1084. Queue->AddItem(QueueItem);
  1085. }
  1086. //---------------------------------------------------------------------------
  1087. void __fastcall TCustomScpExplorerForm::RestoreFormParams()
  1088. {
  1089. }
  1090. //---------------------------------------------------------------------------
  1091. void __fastcall TCustomScpExplorerForm::RestoreParams()
  1092. {
  1093. DebugAssert(FDocks != NULL);
  1094. for (int Index = 0; Index < ComponentCount; Index++)
  1095. {
  1096. TTBDock * Dock = dynamic_cast<TTBDock *>(Components[Index]);
  1097. if ((Dock != NULL) && (Dock->Tag == 0))
  1098. {
  1099. FDocks->Add(Dock);
  1100. }
  1101. }
  1102. CollectItemsWithTextDisplayMode(this);
  1103. ConfigurationChanged();
  1104. QueuePanel->Height =
  1105. LoadDimension(WinConfiguration->QueueView.Height, WinConfiguration->QueueView.HeightPixelsPerInch, this);
  1106. LoadListViewStr(QueueView3, WinConfiguration->QueueView.Layout);
  1107. QueueDock->Visible = WinConfiguration->QueueView.ToolBar;
  1108. QueueLabel->Visible = WinConfiguration->QueueView.Label;
  1109. }
  1110. //---------------------------------------------------------------------------
  1111. void __fastcall TCustomScpExplorerForm::StoreParams()
  1112. {
  1113. WinConfiguration->QueueView.Height = QueuePanel->Height;
  1114. WinConfiguration->QueueView.HeightPixelsPerInch = GetControlPixelsPerInch(this);
  1115. WinConfiguration->QueueView.Layout = GetListViewStr(QueueView3);
  1116. WinConfiguration->QueueView.ToolBar = QueueDock->Visible;
  1117. WinConfiguration->QueueView.Label = QueueLabel->Visible;
  1118. }
  1119. //---------------------------------------------------------------------------
  1120. void __fastcall TCustomScpExplorerForm::CreateParams(TCreateParams & Params)
  1121. {
  1122. if (!FFormRestored)
  1123. {
  1124. FFormRestored = true;
  1125. RestoreFormParams();
  1126. }
  1127. TForm::CreateParams(Params);
  1128. }
  1129. //---------------------------------------------------------------------------
  1130. void __fastcall TCustomScpExplorerForm::SetDockAllowDrag(bool value)
  1131. {
  1132. DebugAssert(FDocks != NULL);
  1133. for (int Index = 0; Index < FDocks->Count; Index++)
  1134. {
  1135. static_cast<TTBDock*>(FDocks->Items[Index])->AllowDrag = value;
  1136. }
  1137. }
  1138. //---------------------------------------------------------------------------
  1139. void __fastcall TCustomScpExplorerForm::LoadToolbarsLayoutStr(UnicodeString LayoutStr, UnicodeString ButtonsStr)
  1140. {
  1141. SetDockAllowDrag(true);
  1142. while (!ButtonsStr.IsEmpty())
  1143. {
  1144. UnicodeString S = CutToChar(ButtonsStr, L';', true);
  1145. UnicodeString Name = CutToChar(S, L'=', true);
  1146. for (int ToolbarIndex = 0; ToolbarIndex < ComponentCount; ToolbarIndex++)
  1147. {
  1148. TTBCustomToolbar * Toolbar = dynamic_cast<TTBCustomToolbar *>(Components[ToolbarIndex]);
  1149. if ((Toolbar != NULL) && SameText(Name, GetToolbarKey(Toolbar->Name)))
  1150. {
  1151. while (!S.IsEmpty())
  1152. {
  1153. UnicodeString S2 = CutToChar(S, L',', true);
  1154. UnicodeString Name = CutToChar(S2, L':', true);
  1155. for (int ItemIndex = 0; ItemIndex < Toolbar->Items->Count; ItemIndex++)
  1156. {
  1157. TTBCustomItem * Item = Toolbar->Items->Items[ItemIndex];
  1158. if (SameText(GetToolbarItemName(Item), Name))
  1159. {
  1160. // forward compatibility
  1161. UnicodeString S3 = CutToChar(S2, L':', true);
  1162. bool Visible = StrToIntDef(S3, 0);
  1163. Item->Visible = Visible;
  1164. }
  1165. }
  1166. }
  1167. }
  1168. }
  1169. }
  1170. ::LoadToolbarsLayoutStr(this, LayoutStr);
  1171. SetDockAllowDrag(!WinConfiguration->LockToolbars);
  1172. }
  1173. //---------------------------------------------------------------------------
  1174. UnicodeString __fastcall TCustomScpExplorerForm::GetToolbarsLayoutStr()
  1175. {
  1176. UnicodeString Result;
  1177. SetDockAllowDrag(true);
  1178. Result = ::GetToolbarsLayoutStr(this);
  1179. SetDockAllowDrag(!WinConfiguration->LockToolbars);
  1180. return Result;
  1181. }
  1182. //---------------------------------------------------------------------------
  1183. UnicodeString __fastcall TCustomScpExplorerForm::GetToolbarItemName(TTBCustomItem * Item)
  1184. {
  1185. UnicodeString Result;
  1186. if (Item->Action != NULL)
  1187. {
  1188. Result = Item->Action->Name;
  1189. Result = RemoveSuffix(Result, L"Action");
  1190. }
  1191. else
  1192. {
  1193. Result = Item->Name;
  1194. Result = RemoveSuffix(Result, L"SubmenuItem");
  1195. Result = RemoveSuffix(Result, L"Item");
  1196. }
  1197. return Result;
  1198. }
  1199. //---------------------------------------------------------------------------
  1200. UnicodeString __fastcall TCustomScpExplorerForm::GetToolbarsButtonsStr()
  1201. {
  1202. UnicodeString Result;
  1203. for (int ToolbarIndex = 0; ToolbarIndex < ComponentCount; ToolbarIndex++)
  1204. {
  1205. UnicodeString ToolbarStr;
  1206. TTBCustomToolbar * Toolbar = dynamic_cast<TTBCustomToolbar *>(Components[ToolbarIndex]);
  1207. if ((Toolbar != NULL) && (Toolbar != QueueToolbar))
  1208. {
  1209. for (int ItemIndex = 0; ItemIndex < Toolbar->Items->Count; ItemIndex++)
  1210. {
  1211. TTBCustomItem * Item = Toolbar->Items->Items[ItemIndex];
  1212. // Currently all buttons are visible by default, so we can safely remember all hidden buttons.
  1213. // Once we introduce any buttons that aer hidden by default, we would have to remember their initial state
  1214. // and save the changes here only.
  1215. if (!Item->Visible)
  1216. {
  1217. UnicodeString Name = GetToolbarItemName(Item);
  1218. DebugAssert(Name.Pos(L"TBX") == 0);
  1219. AddToList(ToolbarStr, FORMAT(L"%s:0", (Name)), L",");
  1220. }
  1221. }
  1222. }
  1223. if (!ToolbarStr.IsEmpty())
  1224. {
  1225. AddToList(Result, FORMAT(L"%s=%s", (GetToolbarKey(Toolbar->Name), ToolbarStr)), L";");
  1226. }
  1227. }
  1228. return Result;
  1229. }
  1230. //---------------------------------------------------------------------------
  1231. void __fastcall TCustomScpExplorerForm::FileOperationProgress(
  1232. TFileOperationProgressType & ProgressData)
  1233. {
  1234. // Download to temporary local directory
  1235. if (FPendingTempSpaceWarn && ProgressData.InProgress && ProgressData.TotalSizeSet)
  1236. {
  1237. bool Continue = true;
  1238. FPendingTempSpaceWarn = false;
  1239. DoWarnLackOfTempSpace(ProgressData.Directory, ProgressData.TotalSize, Continue);
  1240. if (!Continue)
  1241. {
  1242. Abort();
  1243. }
  1244. }
  1245. // operation is being executed and we still didn't show up progress form
  1246. if (ProgressData.InProgress && !FProgressForm)
  1247. {
  1248. bool AllowSkip =
  1249. ((ProgressData.Operation == foCopy) || (ProgressData.Operation == foMove)) &&
  1250. Terminal->IsCapable[fsSkipTransfer];
  1251. FProgressForm = new TProgressForm(Application, (FTransferResumeList != NULL), AllowSkip);
  1252. // As progress window has delayed show now, we need to lock ourselves,
  1253. // (at least) until then
  1254. LockWindow();
  1255. FProgressForm->DeleteToRecycleBin = (ProgressData.Side == osLocal ?
  1256. (WinConfiguration->DeleteToRecycleBin != FAlternativeDelete) :
  1257. ((Terminal->SessionData->DeleteToRecycleBin != FAlternativeDelete) &&
  1258. !Terminal->SessionData->RecycleBinPath.IsEmpty()));
  1259. // When main window is hidden, synchronisation form does not exist,
  1260. // we suppose "/upload" or URL download mode
  1261. if (!Visible && (ProgressData.Operation != foCalculateSize) &&
  1262. (ProgressData.Operation != foCalculateChecksum) &&
  1263. (FSynchronizeProgressForm == NULL) &&
  1264. !FStandaloneEditing)
  1265. {
  1266. FProgressForm->OnceDoneOperation = odoDisconnect;
  1267. }
  1268. if (FTaskbarList != NULL)
  1269. {
  1270. // Actually, do not know what hides the progress once the operation finishes
  1271. SetTaskbarListProgressState(TBPF_NORMAL);
  1272. }
  1273. }
  1274. // operation is finished (or terminated), so we hide progress form
  1275. else if (!ProgressData.InProgress && FProgressForm)
  1276. {
  1277. UnlockWindow();
  1278. SAFE_DESTROY(FProgressForm);
  1279. SetQueueProgress();
  1280. if (ProgressData.Operation == foCalculateSize)
  1281. {
  1282. // When calculating size before transfer, the abort caused by
  1283. // cancel flag set due to "MoveToQueue" below,
  1284. // is not propagated back to us in ExecuteFileOperation,
  1285. // as it is silently swallowed.
  1286. // So we explicitly (re)throw it here.
  1287. if (FMoveToQueue)
  1288. {
  1289. Abort();
  1290. }
  1291. }
  1292. else
  1293. {
  1294. if ((ProgressData.Cancel == csContinue) &&
  1295. !FAutoOperation)
  1296. {
  1297. OperationComplete(ProgressData.StartTime);
  1298. }
  1299. }
  1300. }
  1301. if (FProgressForm)
  1302. {
  1303. FProgressForm->SetProgressData(ProgressData);
  1304. if (FTaskbarList != NULL)
  1305. {
  1306. DebugAssert(ProgressData.InProgress);
  1307. SetTaskbarListProgressValue(&ProgressData);
  1308. }
  1309. if (FProgressForm->Cancel > csContinue)
  1310. {
  1311. ProgressData.SetCancelAtLeast(FProgressForm->Cancel);
  1312. if (FProgressForm->Cancel == csCancelFile)
  1313. {
  1314. FProgressForm->ClearCancel();
  1315. }
  1316. // cancel cancels even the move
  1317. FMoveToQueue = false;
  1318. }
  1319. else if (FProgressForm->MoveToQueue)
  1320. {
  1321. FMoveToQueue = true;
  1322. ProgressData.SetCancelAtLeast(csCancel);
  1323. }
  1324. if ((FTransferResumeList != NULL) &&
  1325. ProgressData.InProgress &&
  1326. ((ProgressData.Operation == foCopy) || (ProgressData.Operation == foMove)) &&
  1327. !ProgressData.FullFileName.IsEmpty())
  1328. {
  1329. if ((FTransferResumeList->Count == 0) ||
  1330. (FTransferResumeList->Strings[FTransferResumeList->Count - 1] != ProgressData.FullFileName))
  1331. {
  1332. // note that we do not recognize directories from files here
  1333. FTransferResumeList->Add(ProgressData.FullFileName);
  1334. }
  1335. }
  1336. }
  1337. }
  1338. //---------------------------------------------------------------------------
  1339. void __fastcall TCustomScpExplorerForm::OperationComplete(
  1340. const TDateTime & StartTime)
  1341. {
  1342. if (GUIConfiguration->BeepOnFinish &&
  1343. (Now() - StartTime > GUIConfiguration->BeepOnFinishAfter))
  1344. {
  1345. UnicodeString BeepSound = GUIConfiguration->BeepSound;
  1346. DWORD Sound;
  1347. if (!ExtractFileExt(BeepSound).IsEmpty())
  1348. {
  1349. Sound = SND_FILENAME;
  1350. }
  1351. else
  1352. {
  1353. Sound = SND_ALIAS;
  1354. }
  1355. PlaySound(BeepSound.c_str(), NULL, Sound | SND_ASYNC);
  1356. }
  1357. }
  1358. //---------------------------------------------------------------------------
  1359. void __fastcall TCustomScpExplorerForm::OperationProgress(
  1360. TFileOperationProgressType & ProgressData)
  1361. {
  1362. // For example, when calling TFileOperationProgressType::Suspend in TTerminal::QueryReopen,
  1363. // we cannot recurse back to TTerminal::Idle as that may detect another error, calling "suspend" again.
  1364. TAutoNestingCounter Counter(FDoNotIdleCurrentTerminal);
  1365. FileOperationProgress(ProgressData);
  1366. }
  1367. //---------------------------------------------------------------------------
  1368. bool __fastcall TCustomScpExplorerForm::PanelOperation(TOperationSide /*Side*/,
  1369. bool DragDrop)
  1370. {
  1371. return (!DragDrop && (DropSourceControl == NULL)) ||
  1372. (DropSourceControl == RemoteDirView);
  1373. }
  1374. //---------------------------------------------------------------------------
  1375. void __fastcall TCustomScpExplorerForm::DoOperationFinished(
  1376. TFileOperation Operation, TOperationSide Side,
  1377. bool /*Temp*/, const UnicodeString & FileName, bool Success,
  1378. TOnceDoneOperation & OnceDoneOperation)
  1379. {
  1380. if (!FAutoOperation)
  1381. {
  1382. // no selection on "/upload", form servers only as event handler
  1383. // (it is not displayed)
  1384. if (PanelOperation(Side, FDragDropOperation) &&
  1385. Visible && (Operation != foCalculateSize) &&
  1386. (Operation != foGetProperties) && (Operation != foCalculateChecksum))
  1387. {
  1388. if (FOnFileOperationFinished != NULL)
  1389. {
  1390. FOnFileOperationFinished(FileName, Success);
  1391. }
  1392. else
  1393. {
  1394. TCustomDirView * DView = DirView(Side);
  1395. UnicodeString FileNameOnly = ExtractFileName(FileName, (Side == osRemote));
  1396. TListItem *Item = DView->FindFileItem(FileNameOnly);
  1397. // this can happen when local drive is unplugged in the middle of the operation
  1398. if (Item != NULL)
  1399. {
  1400. if (Success) Item->Selected = false;
  1401. DView->MakeProgressVisible(Item);
  1402. }
  1403. }
  1404. }
  1405. if ((Operation == foCopy) || (Operation == foMove))
  1406. {
  1407. if (Side == osLocal)
  1408. {
  1409. Configuration->Usage->Inc(L"UploadedFiles");
  1410. }
  1411. else
  1412. {
  1413. Configuration->Usage->Inc(L"DownloadedFiles");
  1414. }
  1415. }
  1416. }
  1417. if (Success && (FSynchronizeController != NULL))
  1418. {
  1419. if (Operation == foCopy)
  1420. {
  1421. DebugAssert(Side == osLocal);
  1422. FSynchronizeController->LogOperation(soUpload, FileName);
  1423. }
  1424. else if (Operation == foDelete)
  1425. {
  1426. DebugAssert(Side == osRemote);
  1427. FSynchronizeController->LogOperation(soDelete, FileName);
  1428. }
  1429. }
  1430. if (FProgressForm)
  1431. {
  1432. OnceDoneOperation = FProgressForm->OnceDoneOperation;
  1433. }
  1434. }
  1435. //---------------------------------------------------------------------------
  1436. void __fastcall TCustomScpExplorerForm::OperationFinished(
  1437. TFileOperation Operation, TOperationSide Side,
  1438. bool Temp, const UnicodeString & FileName, Boolean Success,
  1439. TOnceDoneOperation & OnceDoneOperation)
  1440. {
  1441. TAutoNestingCounter Counter(FDoNotIdleCurrentTerminal);
  1442. DoOperationFinished(Operation, Side, Temp, FileName, Success,
  1443. OnceDoneOperation);
  1444. }
  1445. //---------------------------------------------------------------------------
  1446. TCustomDirView * __fastcall TCustomScpExplorerForm::DirView(TOperationSide Side)
  1447. {
  1448. DebugAssert(GetSide(Side) == osRemote);
  1449. DebugUsedParam(Side);
  1450. return RemoteDirView;
  1451. }
  1452. //---------------------------------------------------------------------------
  1453. bool __fastcall TCustomScpExplorerForm::DirViewEnabled(TOperationSide Side)
  1454. {
  1455. DebugAssert(GetSide(Side) == osRemote);
  1456. DebugUsedParam(Side);
  1457. return (Terminal != NULL);
  1458. }
  1459. //---------------------------------------------------------------------------
  1460. bool __fastcall TCustomScpExplorerForm::GetEnableFocusedOperation(
  1461. TOperationSide Side, int FilesOnly)
  1462. {
  1463. return DirView(Side)->AnyFileSelected(true, (FilesOnly != 0), true);
  1464. }
  1465. //---------------------------------------------------------------------------
  1466. bool __fastcall TCustomScpExplorerForm::GetEnableSelectedOperation(
  1467. TOperationSide Side, int FilesOnly)
  1468. {
  1469. return DirView(Side)->AnyFileSelected(false, (FilesOnly != 0), true);
  1470. }
  1471. //---------------------------------------------------------------------------
  1472. struct THistoryItemData
  1473. {
  1474. short int Side;
  1475. short int Index;
  1476. };
  1477. //---------------------------------------------------------------------------
  1478. void __fastcall TCustomScpExplorerForm::HistoryGo(TOperationSide Side, int Index)
  1479. {
  1480. DirView(Side)->HistoryGo(Index);
  1481. }
  1482. //---------------------------------------------------------------------------
  1483. void __fastcall TCustomScpExplorerForm::HistoryItemClick(System::TObject* Sender)
  1484. {
  1485. TTBCustomItem * Item = dynamic_cast<TTBCustomItem *>(Sender);
  1486. THistoryItemData Data = *reinterpret_cast<THistoryItemData*>(&(Item->Tag));
  1487. HistoryGo((TOperationSide)Data.Side, Data.Index);
  1488. }
  1489. //---------------------------------------------------------------------------
  1490. void __fastcall TCustomScpExplorerForm::UpdateHistoryMenu(TOperationSide Side,
  1491. bool Back)
  1492. {
  1493. if (FHistoryMenu[Side == osLocal][Back] != NULL)
  1494. {
  1495. TCustomDirView * DView = DirView(Side);
  1496. TTBXPopupMenu * Menu = FHistoryMenu[Side == osLocal][Back];
  1497. int ICount = Back ? DView->BackCount : DView->ForwardCount;
  1498. if (ICount > 10)
  1499. {
  1500. ICount = 10;
  1501. }
  1502. Menu->Items->Clear();
  1503. THistoryItemData Data;
  1504. Data.Side = Side;
  1505. for (short int i = 1; i <= ICount; i++)
  1506. {
  1507. TTBCustomItem * Item = new TTBXItem(Menu);
  1508. Data.Index = static_cast<short int>(i * (Back ? -1 : 1));
  1509. Item->Caption = MinimizeName(DView->HistoryPath[Data.Index], 50, (Side == osRemote));
  1510. Item->Hint = DView->HistoryPath[Data.Index];
  1511. DebugAssert(sizeof(int) == sizeof(THistoryItemData));
  1512. Item->Tag = *reinterpret_cast<int*>(&Data);
  1513. Item->OnClick = HistoryItemClick;
  1514. Menu->Items->Add(Item);
  1515. }
  1516. }
  1517. }
  1518. //---------------------------------------------------------------------------
  1519. TTBXPopupMenu * __fastcall TCustomScpExplorerForm::HistoryMenu(
  1520. TOperationSide Side, bool Back)
  1521. {
  1522. if (FHistoryMenu[Side == osLocal][Back] == NULL)
  1523. {
  1524. // workaround
  1525. // In Pascal the size of TTBXPopupMenu is 132, in C++ 136,
  1526. // operator new allocates memory in Pascal code, but calls inline
  1527. // contructor in C++, leading in problems, the function does
  1528. // both in Pascal code
  1529. FHistoryMenu[Side == osLocal][Back] = CreateTBXPopupMenu(this);
  1530. UpdateHistoryMenu(Side, Back);
  1531. }
  1532. return FHistoryMenu[Side == osLocal][Back];
  1533. }
  1534. //---------------------------------------------------------------------------
  1535. void __fastcall TCustomScpExplorerForm::DirViewHistoryChange(
  1536. TCustomDirView *Sender)
  1537. {
  1538. TOperationSide Side = (Sender == DirView(osRemote) ? osRemote : osLocal);
  1539. UpdateHistoryMenu(Side, true);
  1540. UpdateHistoryMenu(Side, false);
  1541. }
  1542. //---------------------------------------------------------------------------
  1543. bool __fastcall TCustomScpExplorerForm::CustomCommandRemoteAllowed()
  1544. {
  1545. // remote custom commands can be executed only if the server supports shell commands
  1546. // or have secondary shell
  1547. return (FTerminal != NULL) && (FTerminal->IsCapable[fcSecondaryShell] || FTerminal->IsCapable[fcShellAnyCommand]);
  1548. }
  1549. //---------------------------------------------------------------------------
  1550. int __fastcall TCustomScpExplorerForm::CustomCommandState(
  1551. const TCustomCommandType & Command, bool OnFocused, TCustomCommandListType ListType)
  1552. {
  1553. int Result;
  1554. TFileCustomCommand RemoteCustomCommand;
  1555. TLocalCustomCommand LocalCustomCommand;
  1556. TFileCustomCommand * NonInteractiveCustomCommand =
  1557. FLAGCLEAR(Command.Params, ccLocal) ? &RemoteCustomCommand : &LocalCustomCommand;
  1558. TInteractiveCustomCommand InteractiveCustomCommand(NonInteractiveCustomCommand);
  1559. UnicodeString Cmd = InteractiveCustomCommand.Complete(Command.Command, false);
  1560. if (FLAGCLEAR(Command.Params, ccLocal))
  1561. {
  1562. int AllowedState = CustomCommandRemoteAllowed() ? 1 : 0;
  1563. // custom command that does not operate with files can be executed anytime ...
  1564. if (!NonInteractiveCustomCommand->IsFileCommand(Cmd))
  1565. {
  1566. if ((ListType == ccltAll) || (ListType == ccltNonFile))
  1567. {
  1568. // ... but do not show such command in remote file menu (TODO)
  1569. Result = AllowedState;
  1570. }
  1571. else
  1572. {
  1573. Result = -1;
  1574. }
  1575. }
  1576. else
  1577. {
  1578. if ((ListType == ccltAll) || (ListType == ccltFile))
  1579. {
  1580. Result = ((FCurrentSide == osRemote) && DirView(osRemote)->AnyFileSelected(OnFocused, false, true)) ? AllowedState : 0;
  1581. }
  1582. else
  1583. {
  1584. Result = -1;
  1585. }
  1586. }
  1587. }
  1588. else
  1589. {
  1590. // custom command that does not operate with files can be executed anytime
  1591. if (!NonInteractiveCustomCommand->IsFileCommand(Cmd))
  1592. {
  1593. Result = ((ListType == ccltAll) || (ListType == ccltNonFile)) ? 1 : -1;
  1594. }
  1595. else if (LocalCustomCommand.HasLocalFileName(Cmd))
  1596. {
  1597. if ((ListType == ccltAll) || (ListType == ccltFile))
  1598. {
  1599. // special case is "diff"-style command that can be executed over any side,
  1600. // if we have both sides
  1601. Result =
  1602. // Cannot have focus on both panels, so we have to call AnyFileSelected
  1603. // directly (instead of EnableSelectedOperation) to pass
  1604. // false to FocusedFileOnlyWhenFocused when panel is inactive.
  1605. ((HasDirView[osLocal] && DirView(osLocal)->AnyFileSelected(false, false, (FCurrentSide == osLocal))) &&
  1606. DirView(osRemote)->AnyFileSelected(false, false, (FCurrentSide == osRemote))) ? 1 : 0;
  1607. }
  1608. else if (ListType == ccltBoth)
  1609. {
  1610. Result = 1;
  1611. }
  1612. else
  1613. {
  1614. Result = -1;
  1615. }
  1616. }
  1617. else
  1618. {
  1619. if ((ListType == ccltAll) || (ListType == ccltFile))
  1620. {
  1621. Result = DirView(FCurrentSide)->AnyFileSelected(OnFocused, false, true) ? 1 : 0;
  1622. }
  1623. else
  1624. {
  1625. Result = -1;
  1626. }
  1627. }
  1628. }
  1629. return Result;
  1630. }
  1631. //---------------------------------------------------------------------------
  1632. void __fastcall TCustomScpExplorerForm::RemoteCustomCommand(
  1633. TStrings * FileList, const TCustomCommandType & ACommand,
  1634. const TCustomCommandData & Data, const UnicodeString & CommandCommand)
  1635. {
  1636. if (EnsureCommandSessionFallback(fcShellAnyCommand))
  1637. {
  1638. TRemoteCustomCommand RemoteCustomCommand(Data, Terminal->CurrentDirectory);
  1639. TWinInteractiveCustomCommand InteractiveCustomCommand(
  1640. &RemoteCustomCommand, ACommand.Name, ACommand.HomePage);
  1641. UnicodeString Command = InteractiveCustomCommand.Complete(CommandCommand, false);
  1642. Configuration->Usage->Inc(L"RemoteCustomCommandRuns2");
  1643. bool Capture =
  1644. FLAGSET(ACommand.Params, ccShowResults) ||
  1645. FLAGSET(ACommand.Params, ccCopyResults) ||
  1646. FLAGSET(ACommand.Params, ccShowResultsInMsgBox);
  1647. TCaptureOutputEvent OutputEvent = NULL;
  1648. DebugAssert(FCapturedLog == NULL);
  1649. if (Capture)
  1650. {
  1651. FCapturedLog = new TStringList();
  1652. OutputEvent = TerminalCaptureLog;
  1653. }
  1654. try
  1655. {
  1656. if (!RemoteCustomCommand.IsFileCommand(Command))
  1657. {
  1658. Terminal->AnyCommand(RemoteCustomCommand.Complete(Command, true),
  1659. OutputEvent);
  1660. }
  1661. else
  1662. {
  1663. Terminal->CustomCommandOnFiles(Command, ACommand.Params, FileList, OutputEvent);
  1664. }
  1665. if ((FCapturedLog != NULL) && (FCapturedLog->Count > 0))
  1666. {
  1667. if (FLAGSET(ACommand.Params, ccCopyResults))
  1668. {
  1669. CopyToClipboard(FCapturedLog);
  1670. }
  1671. if (FLAGSET(ACommand.Params, ccShowResults))
  1672. {
  1673. DoConsoleDialog(Terminal, L"", FCapturedLog);
  1674. }
  1675. else if (FLAGSET(ACommand.Params, ccShowResultsInMsgBox))
  1676. {
  1677. MessageDialog(FCapturedLog->Text, qtInformation, qaOK);
  1678. }
  1679. }
  1680. }
  1681. __finally
  1682. {
  1683. SAFE_DESTROY(FCapturedLog);
  1684. }
  1685. }
  1686. }
  1687. //---------------------------------------------------------------------------
  1688. void __fastcall TCustomScpExplorerForm::LocalCustomCommandPure(
  1689. TStrings * FileList, const TCustomCommandType & ACommand, const UnicodeString & Command, TStrings * ALocalFileList,
  1690. const TCustomCommandData & Data, bool LocalFileCommand, bool FileListCommand, UnicodeString * POutput)
  1691. {
  1692. TStrings * LocalFileList = NULL;
  1693. TStrings * RemoteFileList = NULL;
  1694. TStrings * RemoteFileListFull = NULL;
  1695. try
  1696. {
  1697. if (LocalFileCommand)
  1698. {
  1699. if (ALocalFileList == NULL)
  1700. {
  1701. DebugAssert(HasDirView[osLocal]);
  1702. // Cannot have focus on both panels, so we have to call AnyFileSelected
  1703. // directly (instead of EnableSelectedOperation) to pass
  1704. // false to FocusedFileOnlyWhenFocused
  1705. DebugAssert(DirView(osLocal)->AnyFileSelected(false, false, false));
  1706. LocalFileList = DirView(osLocal)->CreateFileList(false, true, NULL);
  1707. }
  1708. else
  1709. {
  1710. LocalFileList = ALocalFileList;
  1711. }
  1712. if (FileListCommand)
  1713. {
  1714. if (LocalFileList->Count != 1)
  1715. {
  1716. throw Exception(LoadStr(CUSTOM_COMMAND_SELECTED_UNMATCH1));
  1717. }
  1718. }
  1719. else
  1720. {
  1721. if ((LocalFileList->Count != 1) &&
  1722. (FileList->Count != 1) &&
  1723. (LocalFileList->Count != FileList->Count))
  1724. {
  1725. throw Exception(LoadStr(CUSTOM_COMMAND_SELECTED_UNMATCH));
  1726. }
  1727. }
  1728. }
  1729. UnicodeString RootTempDir;
  1730. UnicodeString TempDir;
  1731. bool RemoteFiles = FLAGSET(ACommand.Params, ccRemoteFiles);
  1732. if (!RemoteFiles)
  1733. {
  1734. TemporarilyDownloadFiles(FileList, false, RootTempDir, TempDir, false, false, true);
  1735. }
  1736. try
  1737. {
  1738. TDateTimes RemoteFileTimes;
  1739. if (RemoteFiles)
  1740. {
  1741. RemoteFileList = FileList;
  1742. }
  1743. else
  1744. {
  1745. RemoteFileList = new TStringList();
  1746. TMakeLocalFileListParams MakeFileListParam;
  1747. MakeFileListParam.FileList = RemoteFileList;
  1748. MakeFileListParam.FileTimes = &RemoteFileTimes;
  1749. MakeFileListParam.IncludeDirs = FLAGSET(ACommand.Params, ccApplyToDirectories);
  1750. MakeFileListParam.Recursive =
  1751. FLAGSET(ACommand.Params, ccRecursive) && !FileListCommand;
  1752. ProcessLocalDirectory(TempDir, Terminal->MakeLocalFileList, &MakeFileListParam);
  1753. if (!MakeFileListParam.Recursive)
  1754. {
  1755. RemoteFileTimes.clear();
  1756. RemoteFileListFull = new TStringList();
  1757. MakeFileListParam.FileList = RemoteFileListFull;
  1758. MakeFileListParam.Recursive = true;
  1759. MakeFileListParam.IncludeDirs = false;
  1760. ProcessLocalDirectory(TempDir, Terminal->MakeLocalFileList, &MakeFileListParam);
  1761. }
  1762. else
  1763. {
  1764. RemoteFileListFull = RemoteFileList;
  1765. }
  1766. }
  1767. bool NonBlocking = FileListCommand && RemoteFiles && !POutput;
  1768. TFileOperationProgressType Progress(&OperationProgress, &OperationFinished);
  1769. if (!NonBlocking)
  1770. {
  1771. Progress.Start(foCustomCommand, osRemote, FileListCommand ? 1 : FileList->Count);
  1772. DebugAssert(FProgressForm != NULL);
  1773. FProgressForm->ReadOnly = true;
  1774. }
  1775. try
  1776. {
  1777. if (FileListCommand)
  1778. {
  1779. UnicodeString LocalFile;
  1780. // MakeFileList does not delimit filenames
  1781. UnicodeString FileList = MakeFileList(RemoteFileList);
  1782. if (LocalFileCommand)
  1783. {
  1784. DebugAssert(LocalFileList->Count == 1);
  1785. LocalFile = LocalFileList->Strings[0];
  1786. }
  1787. TLocalCustomCommand CustomCommand(Data,
  1788. Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(), L"", LocalFile, FileList);
  1789. UnicodeString ShellCommand = CustomCommand.Complete(Command, true);
  1790. if (NonBlocking)
  1791. {
  1792. DebugAssert(!POutput);
  1793. ExecuteShellChecked(ShellCommand);
  1794. }
  1795. else
  1796. {
  1797. ExecuteProcessCheckedAndWait(ShellCommand, HelpKeyword, POutput);
  1798. }
  1799. }
  1800. else if (LocalFileCommand)
  1801. {
  1802. if (LocalFileList->Count == 1)
  1803. {
  1804. UnicodeString LocalFile = LocalFileList->Strings[0];
  1805. for (int Index = 0; Index < RemoteFileList->Count; Index++)
  1806. {
  1807. UnicodeString FileName = RemoteFileList->Strings[Index];
  1808. TLocalCustomCommand CustomCommand(Data,
  1809. Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(), FileName, LocalFile, L"");
  1810. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  1811. }
  1812. }
  1813. else if (RemoteFileList->Count == 1)
  1814. {
  1815. UnicodeString FileName = RemoteFileList->Strings[0];
  1816. for (int Index = 0; Index < LocalFileList->Count; Index++)
  1817. {
  1818. TLocalCustomCommand CustomCommand(
  1819. Data, Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(),
  1820. FileName, LocalFileList->Strings[Index], L"");
  1821. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  1822. }
  1823. }
  1824. else
  1825. {
  1826. if (LocalFileList->Count != RemoteFileList->Count)
  1827. {
  1828. throw Exception(LoadStr(CUSTOM_COMMAND_PAIRS_DOWNLOAD_FAILED));
  1829. }
  1830. for (int Index = 0; Index < LocalFileList->Count; Index++)
  1831. {
  1832. UnicodeString FileName = RemoteFileList->Strings[Index];
  1833. TLocalCustomCommand CustomCommand(
  1834. Data, Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(),
  1835. FileName, LocalFileList->Strings[Index], L"");
  1836. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  1837. }
  1838. }
  1839. }
  1840. else
  1841. {
  1842. for (int Index = 0; Index < RemoteFileList->Count; Index++)
  1843. {
  1844. TLocalCustomCommand CustomCommand(Data,
  1845. Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(),
  1846. RemoteFileList->Strings[Index], L"", L"");
  1847. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  1848. }
  1849. }
  1850. }
  1851. __finally
  1852. {
  1853. if (!NonBlocking)
  1854. {
  1855. Progress.Stop();
  1856. }
  1857. }
  1858. DebugAssert(!FAutoOperation);
  1859. if (!RemoteFiles)
  1860. {
  1861. TempDir = IncludeTrailingBackslash(TempDir);
  1862. for (int Index = 0; Index < RemoteFileListFull->Count; Index++)
  1863. {
  1864. UnicodeString FileName = RemoteFileListFull->Strings[Index];
  1865. if (DebugAlwaysTrue(SameText(TempDir, FileName.SubString(1, TempDir.Length()))) &&
  1866. // Skip directories, process only nested files.
  1867. // The check is redundant as FileAge fails for directories anyway.
  1868. !DirectoryExists(FileName))
  1869. {
  1870. UnicodeString RemoteDir =
  1871. UnixExtractFileDir(
  1872. UnixIncludeTrailingBackslash(FTerminal->CurrentDirectory) +
  1873. ToUnixPath(FileName.SubString(TempDir.Length() + 1, FileName.Length() - TempDir.Length())));
  1874. TDateTime NewTime;
  1875. if (FileAge(FileName, NewTime) &&
  1876. (NewTime != RemoteFileTimes[Index]))
  1877. {
  1878. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  1879. TemporaryFileCopyParam(CopyParam);
  1880. CopyParam.FileMask = L"";
  1881. FAutoOperation = true;
  1882. std::unique_ptr<TStrings> TemporaryFilesList(new TStringList());
  1883. TemporaryFilesList->Add(FileName);
  1884. FTerminal->CopyToRemote(TemporaryFilesList.get(), RemoteDir, &CopyParam, cpTemporary, NULL);
  1885. }
  1886. }
  1887. }
  1888. }
  1889. }
  1890. __finally
  1891. {
  1892. FAutoOperation = false;
  1893. if (!RootTempDir.IsEmpty() && DebugAlwaysTrue(!RemoteFiles))
  1894. {
  1895. RecursiveDeleteFile(ExcludeTrailingBackslash(RootTempDir), false);
  1896. }
  1897. }
  1898. }
  1899. __finally
  1900. {
  1901. if (RemoteFileList != FileList)
  1902. {
  1903. delete RemoteFileList;
  1904. }
  1905. if (LocalFileList != ALocalFileList)
  1906. {
  1907. delete LocalFileList;
  1908. }
  1909. if (RemoteFileListFull != RemoteFileList)
  1910. {
  1911. delete RemoteFileListFull;
  1912. }
  1913. }
  1914. }
  1915. //---------------------------------------------------------------------------
  1916. void __fastcall TCustomScpExplorerForm::LocalCustomCommandWithRemoteFiles(
  1917. const TCustomCommandType & ACommand, const UnicodeString & Command, const TCustomCommandData & Data,
  1918. bool FileListCommand, UnicodeString * POutput)
  1919. {
  1920. std::unique_ptr<TStrings> SelectedFileList(DirView(osLocal)->CreateFileList(false, true, NULL));
  1921. std::unique_ptr<TStrings> LocalFileList(new TStringList());
  1922. for (int Index = 0; Index < SelectedFileList->Count; Index++)
  1923. {
  1924. UnicodeString FileName = SelectedFileList->Strings[Index];
  1925. if (DirectoryExists(FileName))
  1926. {
  1927. if (FLAGSET(ACommand.Params, ccApplyToDirectories))
  1928. {
  1929. LocalFileList->Add(FileName);
  1930. }
  1931. if (FLAGSET(ACommand.Params, ccRecursive))
  1932. {
  1933. TMakeLocalFileListParams MakeFileListParam;
  1934. MakeFileListParam.FileList = LocalFileList.get();
  1935. MakeFileListParam.FileTimes = NULL;
  1936. MakeFileListParam.IncludeDirs = FLAGSET(ACommand.Params, ccApplyToDirectories);
  1937. MakeFileListParam.Recursive = true;
  1938. ProcessLocalDirectory(FileName, Terminal->MakeLocalFileList, &MakeFileListParam);
  1939. }
  1940. }
  1941. else
  1942. {
  1943. LocalFileList->Add(FileName);
  1944. }
  1945. }
  1946. if (FileListCommand)
  1947. {
  1948. UnicodeString FileList = MakeFileList(LocalFileList.get());
  1949. TLocalCustomCommand CustomCommand(
  1950. Data, Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(),
  1951. L"", L"", FileList);
  1952. ExecuteProcessChecked(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  1953. }
  1954. else
  1955. {
  1956. TFileOperationProgressType Progress(&OperationProgress, &OperationFinished);
  1957. Progress.Start(foCustomCommand, osRemote, FileListCommand ? 1 : LocalFileList->Count);
  1958. DebugAssert(FProgressForm != NULL);
  1959. FProgressForm->ReadOnly = true;
  1960. try
  1961. {
  1962. for (int Index = 0; Index < LocalFileList->Count; Index++)
  1963. {
  1964. UnicodeString FileName = LocalFileList->Strings[Index];
  1965. TLocalCustomCommand CustomCommand(
  1966. Data, Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(),
  1967. FileName, L"", L"");
  1968. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  1969. }
  1970. }
  1971. __finally
  1972. {
  1973. Progress.Stop();
  1974. }
  1975. }
  1976. }
  1977. //---------------------------------------------------------------------------
  1978. void __fastcall TCustomScpExplorerForm::LocalCustomCommand(TStrings * FileList,
  1979. const TCustomCommandType & ACommand, TStrings * ALocalFileList,
  1980. const TCustomCommandData & Data, const UnicodeString & CommandCommand)
  1981. {
  1982. TLocalCustomCommand LocalCustomCommand(Data, Terminal->CurrentDirectory, DefaultDownloadTargetDirectory());
  1983. TWinInteractiveCustomCommand InteractiveCustomCommand(
  1984. &LocalCustomCommand, ACommand.Name, ACommand.HomePage);
  1985. UnicodeString Command = InteractiveCustomCommand.Complete(CommandCommand, false);
  1986. bool FileListCommand = LocalCustomCommand.IsFileListCommand(Command);
  1987. bool LocalFileCommand = LocalCustomCommand.HasLocalFileName(Command);
  1988. Configuration->Usage->Inc(L"LocalCustomCommandRuns2");
  1989. std::unique_ptr<UnicodeString> POutput;
  1990. if (FLAGSET(ACommand.Params, ccShowResultsInMsgBox) ||
  1991. FLAGSET(ACommand.Params, ccCopyResults))
  1992. {
  1993. POutput.reset(new UnicodeString());
  1994. }
  1995. if (!LocalCustomCommand.IsFileCommand(Command))
  1996. {
  1997. ExecuteProcessChecked(LocalCustomCommand.Complete(Command, true), HelpKeyword, POutput.get());
  1998. }
  1999. // remote files?
  2000. else if ((FCurrentSide == osRemote) || LocalFileCommand)
  2001. {
  2002. LocalCustomCommandPure(FileList, ACommand, Command, ALocalFileList, Data, LocalFileCommand, FileListCommand, POutput.get());
  2003. }
  2004. // local files
  2005. else
  2006. {
  2007. LocalCustomCommandWithRemoteFiles(ACommand, Command, Data, FileListCommand, POutput.get());
  2008. }
  2009. if (POutput.get() != NULL)
  2010. {
  2011. // If the output is single-line, we do not want the trailing CRLF, as the CopyToClipboard(TStrings *) does
  2012. int P = POutput->Pos(sLineBreak);
  2013. if (P == POutput->Length() - static_cast<int>(strlen(sLineBreak)) + 1)
  2014. {
  2015. POutput->SetLength(P - 1);
  2016. }
  2017. // Copy even empty output to clipboard
  2018. if (FLAGSET(ACommand.Params, ccCopyResults))
  2019. {
  2020. CopyToClipboard(*POutput);
  2021. }
  2022. // But do not show an empty message box.
  2023. // This way the ShowResultsInMsgBox can be used to suppress a console window of commands with no output
  2024. if (FLAGSET(ACommand.Params, ccShowResultsInMsgBox) &&
  2025. !POutput->IsEmpty())
  2026. {
  2027. TClipboardHandler ClipboardHandler;
  2028. ClipboardHandler.Text = *POutput;
  2029. TMessageParams Params;
  2030. TQueryButtonAlias Aliases[1];
  2031. Aliases[0].Button = qaRetry;
  2032. Aliases[0].Alias = LoadStr(EDIT_COPY);
  2033. Aliases[0].OnSubmit = &ClipboardHandler.Copy;
  2034. Params.Aliases = Aliases;
  2035. Params.AliasesCount = LENOF(Aliases);
  2036. MessageDialog(*POutput, qtInformation, qaOK | qaRetry, HelpKeyword, &Params);
  2037. }
  2038. }
  2039. }
  2040. //---------------------------------------------------------------------------
  2041. void __fastcall TCustomScpExplorerForm::CustomCommand(TStrings * FileList,
  2042. const TCustomCommandType & ACommand, TStrings * ALocalFileList)
  2043. {
  2044. TCustomCommandData Data(Terminal);
  2045. UnicodeString Site = Terminal->SessionData->SessionKey;
  2046. UnicodeString HelpKeyword = ACommand.HomePage;
  2047. std::unique_ptr<TStrings> CustomCommandOptions(CloneStrings(WinConfiguration->CustomCommandOptions));
  2048. if (ACommand.AnyOptionWithFlag(TCustomCommandType::ofRun))
  2049. {
  2050. std::unique_ptr<TCustomCommand> CustomCommandForOptions;
  2051. if (FLAGCLEAR(ACommand.Params, ccLocal))
  2052. {
  2053. CustomCommandForOptions.reset(new TRemoteCustomCommand(Data, Terminal->CurrentDirectory));
  2054. }
  2055. else
  2056. {
  2057. CustomCommandForOptions.reset(new TLocalCustomCommand(Data, Terminal->CurrentDirectory, DefaultDownloadTargetDirectory()));
  2058. }
  2059. if (!DoCustomCommandOptionsDialog(
  2060. &ACommand, CustomCommandOptions.get(), NULL, TCustomCommandType::ofRun, CustomCommandForOptions.get(), Site, NULL))
  2061. {
  2062. Abort();
  2063. }
  2064. }
  2065. UnicodeString CommandCommand = ACommand.GetCommandWithExpandedOptions(CustomCommandOptions.get(), Site);
  2066. if (FLAGCLEAR(ACommand.Params, ccLocal))
  2067. {
  2068. RemoteCustomCommand(FileList, ACommand, Data, CommandCommand);
  2069. }
  2070. else
  2071. {
  2072. LocalCustomCommand(FileList, ACommand, ALocalFileList, Data, CommandCommand);
  2073. }
  2074. }
  2075. //---------------------------------------------------------------------------
  2076. void __fastcall TCustomScpExplorerForm::BothCustomCommand(
  2077. const TCustomCommandType & Command)
  2078. {
  2079. DebugAssert(FCustomCommandLocalFileList != NULL);
  2080. DebugAssert(FCustomCommandRemoteFileList != NULL);
  2081. DebugAssert(FCustomCommandLocalFileList->Count == FCustomCommandRemoteFileList->Count);
  2082. TStrings * LocalFileList = new TStringList();
  2083. TStrings * RemoteFileList = new TStringList();
  2084. try
  2085. {
  2086. for (int Index = 0; Index < FCustomCommandLocalFileList->Count; Index++)
  2087. {
  2088. LocalFileList->Clear();
  2089. LocalFileList->AddObject(
  2090. FCustomCommandLocalFileList->Strings[Index],
  2091. FCustomCommandLocalFileList->Objects[Index]);
  2092. RemoteFileList->Clear();
  2093. RemoteFileList->AddObject(
  2094. FCustomCommandRemoteFileList->Strings[Index],
  2095. FCustomCommandRemoteFileList->Objects[Index]);
  2096. CustomCommand(RemoteFileList, Command, LocalFileList);
  2097. }
  2098. }
  2099. __finally
  2100. {
  2101. delete LocalFileList;
  2102. delete RemoteFileList;
  2103. }
  2104. }
  2105. //---------------------------------------------------------------------------
  2106. void __fastcall TCustomScpExplorerForm::CustomCommandMenu(
  2107. TAction * Action, TStrings * LocalFileList, TStrings * RemoteFileList)
  2108. {
  2109. delete FCustomCommandLocalFileList;
  2110. delete FCustomCommandRemoteFileList;
  2111. // takeover ownership,
  2112. // the lists must survive the MenuPopup as OnClick occurs only after it exits
  2113. FCustomCommandLocalFileList = LocalFileList;
  2114. FCustomCommandRemoteFileList = RemoteFileList;
  2115. TButton * Button = dynamic_cast<TButton *>(Action->ActionComponent);
  2116. if (Button != NULL)
  2117. {
  2118. FCustomCommandMenu->Items->Clear();
  2119. NonVisualDataModule->CreateCustomCommandsMenu(FCustomCommandMenu->Items, false, false, ccltBoth);
  2120. MenuPopup(FCustomCommandMenu, Button);
  2121. }
  2122. else
  2123. {
  2124. NonVisualDataModule->CreateCustomCommandsMenu(Action, false, ccltBoth);
  2125. }
  2126. }
  2127. //---------------------------------------------------------------------------
  2128. void __fastcall TCustomScpExplorerForm::TerminalCaptureLog(
  2129. const UnicodeString & AddedLine, TCaptureOutputType OutputType)
  2130. {
  2131. DebugAssert(FCapturedLog != NULL);
  2132. if ((OutputType == cotOutput) || (OutputType == cotError))
  2133. {
  2134. FCapturedLog->Add(AddedLine);
  2135. }
  2136. }
  2137. //---------------------------------------------------------------------------
  2138. bool __fastcall TCustomScpExplorerForm::IsFileControl(TObject * Control,
  2139. TOperationSide Side)
  2140. {
  2141. return (Side == osRemote) &&
  2142. ((Control == RemoteDirView) || (Control == RemoteDriveView));
  2143. }
  2144. //---------------------------------------------------------------------------
  2145. void __fastcall TCustomScpExplorerForm::DirViewContextPopupDefaultItem(
  2146. TOperationSide Side, TTBXCustomItem * Item, TDoubleClickAction DoubleClickAction)
  2147. {
  2148. TTBItemOptions O;
  2149. O = Item->Options;
  2150. TCustomDirView * DView = DirView(Side);
  2151. if ((DView->ItemFocused != NULL) &&
  2152. (WinConfiguration->DoubleClickAction == DoubleClickAction) &&
  2153. // when resolving links is disabled, default action is to enter the directory,
  2154. // no matter what DoubleClickAction is configured to
  2155. ((Side != osRemote) || Terminal->ResolvingSymlinks || Terminal->IsEncryptingFiles()) &&
  2156. // Can only Edit files, but can Open/Copy even directories
  2157. ((DoubleClickAction != dcaEdit) ||
  2158. !DView->ItemIsDirectory(DView->ItemFocused)))
  2159. {
  2160. Item->Options = O << tboDefault;
  2161. }
  2162. else
  2163. {
  2164. Item->Options = O >> tboDefault;
  2165. }
  2166. }
  2167. //---------------------------------------------------------------------------
  2168. void __fastcall TCustomScpExplorerForm::DirViewContextPopup(
  2169. TOperationSide Side, Byte PopupComponent, const TPoint &MousePos)
  2170. {
  2171. TCustomDirView * DView = DirView(Side);
  2172. TListItem * Item = DView->ItemFocused;
  2173. if ((DView->GetItemAt(MousePos.x, MousePos.y) == Item) &&
  2174. EnableFocusedOperation[Side])
  2175. {
  2176. TPoint ClientPoint;
  2177. ClientPoint = ((MousePos.x < 0) && (MousePos.y < 0)) ?
  2178. TPoint(0, 0) : MousePos;
  2179. FLastContextPopupScreenPoint = DView->ClientToScreen(ClientPoint);
  2180. reinterpret_cast<TPopupMenu*>(GetComponent(PopupComponent))->Popup(
  2181. FLastContextPopupScreenPoint.x, FLastContextPopupScreenPoint.y);
  2182. }
  2183. }
  2184. //---------------------------------------------------------------------------
  2185. void __fastcall TCustomScpExplorerForm::RemoteDirViewContextPopup(
  2186. TObject * /*Sender*/, const TPoint &MousePos, bool &Handled)
  2187. {
  2188. DirViewContextPopupDefaultItem(osRemote, NonVisualDataModule->RemoteOpenMenuItem, dcaOpen);
  2189. DirViewContextPopupDefaultItem(osRemote, NonVisualDataModule->RemoteEditMenuItem, dcaEdit);
  2190. DirViewContextPopupDefaultItem(osRemote, NonVisualDataModule->RemoteCopyMenuItem, dcaCopy);
  2191. DirViewContextPopup(osRemote, fcRemotePopup, MousePos);
  2192. Handled = true;
  2193. }
  2194. //---------------------------------------------------------------------------
  2195. void __fastcall TCustomScpExplorerForm::ReloadLocalDirectory(const UnicodeString Directory)
  2196. {
  2197. }
  2198. //---------------------------------------------------------------------------
  2199. void __fastcall TCustomScpExplorerForm::BatchStart(void *& /*Storage*/)
  2200. {
  2201. DebugAssert(FErrorList == NULL);
  2202. if (WinConfiguration->ContinueOnError)
  2203. {
  2204. FErrorList = new TStringList();
  2205. Configuration->Usage->Inc(L"ContinuationsOnError");
  2206. }
  2207. NonVisualDataModule->StartBusy();
  2208. }
  2209. //---------------------------------------------------------------------------
  2210. void __fastcall TCustomScpExplorerForm::BatchEnd(void * /*Storage*/)
  2211. {
  2212. NonVisualDataModule->EndBusy();
  2213. if (FErrorList)
  2214. {
  2215. HandleErrorList(FErrorList);
  2216. }
  2217. }
  2218. //---------------------------------------------------------------------------
  2219. void __fastcall TCustomScpExplorerForm::UpdateCopyParamCounters(
  2220. const TCopyParamType & CopyParam)
  2221. {
  2222. if (!CopyParam.IncludeFileMask.Masks.IsEmpty())
  2223. {
  2224. Configuration->Usage->Inc(L"FileMaskUses");
  2225. }
  2226. if (IsEffectiveFileNameMask(CopyParam.FileMask))
  2227. {
  2228. Configuration->Usage->Inc(L"OperationMaskUses");
  2229. }
  2230. }
  2231. //---------------------------------------------------------------------------
  2232. bool __fastcall TCustomScpExplorerForm::ExecuteFileOperation(TFileOperation Operation,
  2233. TOperationSide Side, TStrings * FileList, bool NoConfirmation, void * Param)
  2234. {
  2235. void * BatchStorage;
  2236. BatchStart(BatchStorage);
  2237. bool Result;
  2238. try
  2239. {
  2240. if ((Operation == foCopy) || (Operation == foMove))
  2241. {
  2242. TTransferDirection Direction = (Side == osLocal ? tdToRemote : tdToLocal);
  2243. TTransferType Type = (Operation == foCopy ? ttCopy : ttMove);
  2244. UnicodeString TargetDirectory;
  2245. bool Temp = false;
  2246. bool DragDrop = false;
  2247. int Options = 0;
  2248. TAutoSwitch UseQueue = asAuto;
  2249. if (Param != NULL)
  2250. {
  2251. TTransferOperationParam& TParam =
  2252. *static_cast<TTransferOperationParam*>(Param);
  2253. TargetDirectory = TParam.TargetDirectory;
  2254. Temp = TParam.Temp;
  2255. DragDrop = TParam.DragDrop;
  2256. Options = TParam.Options;
  2257. UseQueue = TParam.Queue;
  2258. }
  2259. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  2260. switch (UseQueue)
  2261. {
  2262. case asOn:
  2263. CopyParam.Queue = true;
  2264. break;
  2265. case asOff:
  2266. CopyParam.Queue = false;
  2267. break;
  2268. case asAuto:
  2269. default:
  2270. // keep default
  2271. break;
  2272. }
  2273. Result =
  2274. CopyParamDialog(Direction, Type, Temp, FileList, TargetDirectory,
  2275. CopyParam, !NoConfirmation, DragDrop, Options);
  2276. if (Result)
  2277. {
  2278. DebugAssert(Terminal);
  2279. bool SelectionRestored = false;
  2280. TCustomDirView * DView = NULL;
  2281. if (HasDirView[Side])
  2282. {
  2283. DView = DirView(Side);
  2284. DView->SaveSelection();
  2285. DView->SaveSelectedNames();
  2286. }
  2287. UpdateCopyParamCounters(CopyParam);
  2288. std::unique_ptr<TStringList> TransferResumeList(new TStringList());
  2289. DebugAssert(FTransferResumeList == NULL);
  2290. FTransferResumeList =
  2291. Terminal->IsCapable[fcMoveToQueue] ? TransferResumeList.get() : NULL;
  2292. FMoveToQueue = false;
  2293. int Params = FLAGMASK(Operation == foMove, cpDelete);
  2294. try
  2295. {
  2296. TStrings * PermanentFileList;
  2297. std::unique_ptr<TStrings> PermanentFileListOwner;
  2298. try
  2299. {
  2300. if (Side == osLocal)
  2301. {
  2302. PermanentFileList = FileList;
  2303. Params |= FLAGMASK(Temp, cpTemporary);
  2304. Terminal->CopyToRemote(FileList, TargetDirectory, &CopyParam, Params, NULL);
  2305. if (Operation == foMove)
  2306. {
  2307. ReloadLocalDirectory();
  2308. if (DView != NULL)
  2309. {
  2310. DView->RestoreSelection();
  2311. }
  2312. SelectionRestored = true;
  2313. }
  2314. }
  2315. else
  2316. {
  2317. // Clone the file list as it may refer to current directory files,
  2318. // which get destroyed, when the source directory is reloaded after foMove operation.
  2319. // We should actually clone the file list for whole ExecuteFileOperation to protect against reloads.
  2320. // But for a hotfix, we are not going to do such a big change.
  2321. PermanentFileListOwner.reset(TRemoteFileList::CloneStrings(FileList));
  2322. PermanentFileList = PermanentFileListOwner.get();
  2323. try
  2324. {
  2325. Terminal->CopyToLocal(
  2326. FileList, TargetDirectory, &CopyParam, Params, NULL);
  2327. }
  2328. __finally
  2329. {
  2330. if (Operation == foMove)
  2331. {
  2332. if (DView != NULL)
  2333. {
  2334. DView->RestoreSelection();
  2335. }
  2336. SelectionRestored = true;
  2337. }
  2338. ReloadLocalDirectory(TargetDirectory);
  2339. }
  2340. }
  2341. }
  2342. catch (EAbort &)
  2343. {
  2344. if (FMoveToQueue)
  2345. {
  2346. Params |=
  2347. (CopyParam.QueueNoConfirmation ? cpNoConfirmation : 0);
  2348. DebugAssert(CopyParam.TransferSkipList == NULL);
  2349. DebugAssert(CopyParam.TransferResumeFile.IsEmpty());
  2350. if (TransferResumeList->Count > 0)
  2351. {
  2352. CopyParam.TransferResumeFile = TransferResumeList->Strings[TransferResumeList->Count - 1];
  2353. TransferResumeList->Delete(TransferResumeList->Count - 1);
  2354. }
  2355. CopyParam.TransferSkipList = TransferResumeList.release();
  2356. // not really needed, just to keep it consistent with TransferResumeList
  2357. FTransferResumeList = NULL;
  2358. FMoveToQueue = false;
  2359. Configuration->Usage->Inc("MovesToBackground");
  2360. AddQueueItem(Queue, Direction, PermanentFileList, TargetDirectory, CopyParam, Params);
  2361. ClearTransferSourceSelection(Direction);
  2362. }
  2363. throw;
  2364. }
  2365. }
  2366. __finally
  2367. {
  2368. if (!SelectionRestored && (DView != NULL))
  2369. {
  2370. DView->DiscardSavedSelection();
  2371. }
  2372. FTransferResumeList = NULL;
  2373. }
  2374. }
  2375. }
  2376. else if (Operation == foRename)
  2377. {
  2378. DebugAssert(DirView(Side)->ItemFocused);
  2379. DirView(Side)->ItemFocused->EditCaption();
  2380. Result = true;
  2381. }
  2382. else if (Operation == foDelete)
  2383. {
  2384. DebugAssert(FileList->Count);
  2385. // We deliberately do not toggle alternative flag (Param), but use OR,
  2386. // because the Param is set only when command is invoked using Shift-Del/F8 keyboard
  2387. // shortcut of CurrentDeleteAlternativeAction
  2388. bool Alternative =
  2389. bool(Param) || UseAlternativeFunction();
  2390. bool Recycle;
  2391. if (Side == osLocal)
  2392. {
  2393. Recycle = (WinConfiguration->DeleteToRecycleBin != Alternative);
  2394. }
  2395. else
  2396. {
  2397. Recycle = (Terminal->SessionData->DeleteToRecycleBin != Alternative) &&
  2398. !Terminal->SessionData->RecycleBinPath.IsEmpty() &&
  2399. !Terminal->IsRecycledFile(FileList->Strings[0]);
  2400. }
  2401. Result =
  2402. !(Recycle ? WinConfiguration->ConfirmRecycling : WinConfiguration->ConfirmDeleting);
  2403. if (!Result)
  2404. {
  2405. UnicodeString Query;
  2406. if (FileList->Count == 1)
  2407. {
  2408. if (Side == osLocal)
  2409. {
  2410. Query = ExtractFileName(FileList->Strings[0]);
  2411. }
  2412. else
  2413. {
  2414. Query = UnixExtractFileName(FileList->Strings[0]);
  2415. }
  2416. Query = FMTLOAD(
  2417. (Recycle ? CONFIRM_RECYCLE_FILE : CONFIRM_DELETE_FILE), (Query));
  2418. }
  2419. else
  2420. {
  2421. Query = FMTLOAD(
  2422. (Recycle ? CONFIRM_RECYCLE_FILES : CONFIRM_DELETE_FILES), (FileList->Count));
  2423. }
  2424. TMessageParams Params(mpNeverAskAgainCheck);
  2425. Params.ImageName = L"Delete file";
  2426. unsigned int Answer = MessageDialog(MainInstructions(Query), qtConfirmation,
  2427. qaOK | qaCancel, HELP_DELETE_FILE, &Params);
  2428. if (Answer == qaNeverAskAgain)
  2429. {
  2430. Result = true;
  2431. if (Recycle)
  2432. {
  2433. WinConfiguration->ConfirmRecycling = false;
  2434. }
  2435. else
  2436. {
  2437. WinConfiguration->ConfirmDeleting = false;
  2438. }
  2439. }
  2440. else
  2441. {
  2442. Result = (Answer == qaOK);
  2443. }
  2444. }
  2445. if (Result)
  2446. {
  2447. DeleteFiles(Side, FileList, FLAGMASK(Alternative, dfAlternative));
  2448. }
  2449. }
  2450. else if (Operation == foSetProperties)
  2451. {
  2452. RemoteDirView->SaveSelectedNames();
  2453. Result = SetProperties(Side, FileList);
  2454. }
  2455. else if (Operation == foCustomCommand)
  2456. {
  2457. DebugAssert(Param);
  2458. DebugAssert(Side == osRemote);
  2459. RemoteDirView->SaveSelectedNames();
  2460. const TCustomCommandType * Command = static_cast<const TCustomCommandType*>(Param);
  2461. CustomCommand(FileList, *Command, NULL);
  2462. Result = true;
  2463. }
  2464. else if ((Operation == foRemoteMove) || (Operation == foRemoteCopy))
  2465. {
  2466. DebugAssert(Side == osRemote);
  2467. Result = RemoteTransferFiles(FileList, NoConfirmation,
  2468. (Operation == foRemoteMove), reinterpret_cast<TTerminal *>(Param));
  2469. }
  2470. else if (Operation == foLock)
  2471. {
  2472. DebugAssert(Side == osRemote);
  2473. LockFiles(FileList, true);
  2474. Result = true;
  2475. }
  2476. else if (Operation == foUnlock)
  2477. {
  2478. DebugAssert(Side == osRemote);
  2479. LockFiles(FileList, false);
  2480. Result = true;
  2481. }
  2482. else
  2483. {
  2484. DebugFail();
  2485. }
  2486. }
  2487. __finally
  2488. {
  2489. BatchEnd(BatchStorage);
  2490. }
  2491. return Result;
  2492. }
  2493. //---------------------------------------------------------------------------
  2494. TOperationSide __fastcall TCustomScpExplorerForm::GetSide(TOperationSide Side)
  2495. {
  2496. if (Side == osCurrent)
  2497. {
  2498. Side = FCurrentSide;
  2499. }
  2500. return Side;
  2501. }
  2502. //---------------------------------------------------------------------------
  2503. bool __fastcall TCustomScpExplorerForm::ExecuteFileOperation(TFileOperation Operation,
  2504. TOperationSide Side, bool OnFocused, bool NoConfirmation, void * Param)
  2505. {
  2506. Side = GetSide(Side);
  2507. bool Result;
  2508. TStrings * FileList = DirView(Side)->CreateFileList(OnFocused, (Side == osLocal), NULL);
  2509. try
  2510. {
  2511. Result = ExecuteFileOperation(Operation, Side, FileList, NoConfirmation, Param);
  2512. }
  2513. __finally
  2514. {
  2515. delete FileList;
  2516. }
  2517. return Result;
  2518. }
  2519. //---------------------------------------------------------------------------
  2520. void __fastcall TCustomScpExplorerForm::ExecuteFileOperationCommand(
  2521. TFileOperation Operation, TOperationSide Side, bool OnFocused,
  2522. bool NoConfirmation, void * Param)
  2523. {
  2524. if (ExecuteFileOperation(Operation, Side, OnFocused, NoConfirmation, Param))
  2525. {
  2526. if ((Operation == foCopy) || (Operation == foMove))
  2527. {
  2528. if (GetSide(Side) == osLocal)
  2529. {
  2530. Configuration->Usage->Inc(L"UploadsCommand");
  2531. }
  2532. else
  2533. {
  2534. Configuration->Usage->Inc(L"DownloadsCommand");
  2535. }
  2536. }
  2537. }
  2538. }
  2539. //---------------------------------------------------------------------------
  2540. void __fastcall TCustomScpExplorerForm::ExecuteCopyOperationCommand(
  2541. TOperationSide Side, bool OnFocused, unsigned int Flags)
  2542. {
  2543. TTransferOperationParam Param;
  2544. if ((WinConfiguration->Interface != ifCommander) ||
  2545. WinConfiguration->ScpCommander.ExplorerKeyboardShortcuts)
  2546. {
  2547. Flags &= ~cocShortCutHint;
  2548. }
  2549. TCustomDirView * DView = DirView(Side);
  2550. Param.Options =
  2551. FLAGMASK(FLAGSET(Flags, cocShortCutHint), coShortCutHint) |
  2552. FLAGMASK(SelectedAllFilesInDirView(DView), coAllFiles);
  2553. if (FLAGSET(Flags, cocQueue))
  2554. {
  2555. Param.Queue = asOn;
  2556. }
  2557. else if (FLAGSET(Flags, cocNonQueue))
  2558. {
  2559. Param.Queue = asOff;
  2560. }
  2561. ExecuteFileOperationCommand(foCopy, Side, OnFocused, false, &Param);
  2562. }
  2563. //---------------------------------------------------------------------------
  2564. void __fastcall TCustomScpExplorerForm::HandleErrorList(TStringList *& ErrorList)
  2565. {
  2566. try
  2567. {
  2568. if (ErrorList->Count)
  2569. {
  2570. UnicodeString Message = MainInstructions(FMTLOAD(ERROR_LIST_COUNT, (ErrorList->Count)));
  2571. if (MessageDialog(Message, qtError,
  2572. qaOK | qaCancel, HELP_NONE) == qaOK)
  2573. {
  2574. unsigned int Answer;
  2575. int Index = 0;
  2576. do
  2577. {
  2578. DebugAssert(Index >= 0 && Index < ErrorList->Count);
  2579. TQueryButtonAlias Aliases[2];
  2580. Aliases[0].Button = qaYes;
  2581. Aliases[0].Alias = LoadStr(PREV_BUTTON);
  2582. Aliases[1].Button = qaNo;
  2583. Aliases[1].Alias = LoadStr(NEXT_BUTTON);
  2584. TMessageParams Params;
  2585. Params.Aliases = Aliases;
  2586. Params.AliasesCount = LENOF(Aliases);
  2587. Answer = MoreMessageDialog(
  2588. FMTLOAD(ERROR_LIST_NUMBER, (Index+1, ErrorList->Count, ErrorList->Strings[Index])),
  2589. dynamic_cast<TStrings *>(ErrorList->Objects[Index]), qtError,
  2590. (Index ? qaYes : 0) | (Index < ErrorList->Count - 1 ? qaNo : 0) |
  2591. qaOK, HELP_NONE, &Params);
  2592. if (Answer == qaNo)
  2593. {
  2594. Index++;
  2595. }
  2596. if (Answer == qaYes)
  2597. {
  2598. Index--;
  2599. }
  2600. }
  2601. while (Answer != qaOK);
  2602. }
  2603. }
  2604. }
  2605. __finally
  2606. {
  2607. TStrings * List = ErrorList;
  2608. ErrorList = NULL;
  2609. for (int i = 0; i < List->Count; i++)
  2610. {
  2611. delete List->Objects[i];
  2612. }
  2613. delete List;
  2614. }
  2615. }
  2616. //---------------------------------------------------------------------------
  2617. void __fastcall TCustomScpExplorerForm::ExecuteRemoteFile(
  2618. const UnicodeString & FullFileName, TRemoteFile * File, TExecuteFileBy ExecuteFileBy)
  2619. {
  2620. // needed for checking filemasks, as there's no directory object
  2621. // associated with the file object
  2622. File->FullFileName = FullFileName;
  2623. TFileMasks::TParams MaskParams;
  2624. MaskParams.Size = File->Size;
  2625. MaskParams.Modification = File->Modification;
  2626. ExecuteFile(osRemote, ExecuteFileBy, NULL, FullFileName, File, MaskParams);
  2627. }
  2628. //---------------------------------------------------------------------------
  2629. void __fastcall TCustomScpExplorerForm::EditNew(TOperationSide Side)
  2630. {
  2631. DebugAssert(!WinConfiguration->DisableOpenEdit);
  2632. Side = GetSide(Side);
  2633. TCustomDirView * DView = DirView(Side);
  2634. TListItem * FocusedItem = DView->ItemFocused;
  2635. UnicodeString Name;
  2636. if ((FocusedItem != NULL) && !DView->ItemIsParentDirectory(FocusedItem))
  2637. {
  2638. Name = DView->ItemFileName(FocusedItem);
  2639. }
  2640. else
  2641. {
  2642. Name = LoadStr(NEW_FILE);
  2643. }
  2644. UnicodeString Names = Name;
  2645. std::unique_ptr<TStrings> History(CloneStrings(CustomWinConfiguration->History[L"EditFile"]));
  2646. if (InputDialog(LoadStr(EDIT_FILE_CAPTION), LoadStr(EDIT_FILE_PROMPT), Names,
  2647. HELP_EDIT_NEW, History.get(), true))
  2648. {
  2649. while (!Names.IsEmpty())
  2650. {
  2651. Name = CutToChar(Names, FileMasksDelimiters[1], false);
  2652. CustomWinConfiguration->History[L"EditFile"] = History.get();
  2653. UnicodeString TargetFileName;
  2654. UnicodeString LocalFileName;
  2655. UnicodeString RootTempDir;
  2656. UnicodeString TempDir;
  2657. UnicodeString RemoteDirectory;
  2658. bool ExistingFile = false;
  2659. if (Side == osRemote)
  2660. {
  2661. Name = AbsolutePath(FTerminal->CurrentDirectory, Name);
  2662. TRemoteFile * File = NULL;
  2663. if (FTerminal->FileExists(Name, &File))
  2664. {
  2665. try
  2666. {
  2667. ExecuteRemoteFile(Name, File, efDefaultEditor);
  2668. ExistingFile = true;
  2669. }
  2670. __finally
  2671. {
  2672. delete File;
  2673. }
  2674. }
  2675. if (!ExistingFile)
  2676. {
  2677. RemoteDirectory = UnixExtractFilePath(Name);
  2678. TemporaryDirectoryForRemoteFiles(
  2679. RemoteDirectory, GUIConfiguration->CurrentCopyParam, TempDir, RootTempDir);
  2680. TargetFileName = UnixExtractFileName(Name);
  2681. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  2682. LocalFileName = TempDir +
  2683. // We probably do not want to trim the VMS version here
  2684. FTerminal->ChangeFileName(&CopyParam, TargetFileName, osRemote, false);
  2685. }
  2686. }
  2687. else
  2688. {
  2689. if (ExtractFilePath(Name).IsEmpty())
  2690. {
  2691. LocalFileName = IncludeTrailingBackslash(DView->PathName) + Name;
  2692. }
  2693. else
  2694. {
  2695. LocalFileName = ExpandFileName(Name);
  2696. }
  2697. TargetFileName = ExtractFileName(Name);
  2698. }
  2699. if (!ExistingFile)
  2700. {
  2701. if (!FileExists(ApiPath(LocalFileName)))
  2702. {
  2703. int File = FileCreate(ApiPath(LocalFileName));
  2704. if (File < 0)
  2705. {
  2706. if (!RootTempDir.IsEmpty())
  2707. {
  2708. RecursiveDeleteFile(ExcludeTrailingBackslash(RootTempDir), false);
  2709. }
  2710. throw Exception(FMTLOAD(CREATE_FILE_ERROR, (LocalFileName)));
  2711. }
  2712. else
  2713. {
  2714. FileClose(File);
  2715. }
  2716. }
  2717. TExecuteFileBy ExecuteFileBy = efDefaultEditor;
  2718. const TEditorData * ExternalEditor = NULL;
  2719. TFileMasks::TParams MaskParams; // size not known
  2720. ExecuteFileNormalize(ExecuteFileBy, ExternalEditor, TargetFileName,
  2721. false, MaskParams);
  2722. CustomExecuteFile(Side, ExecuteFileBy, LocalFileName, TargetFileName,
  2723. ExternalEditor, RootTempDir, RemoteDirectory);
  2724. }
  2725. }
  2726. }
  2727. }
  2728. //---------------------------------------------------------------------------
  2729. bool __fastcall TCustomScpExplorerForm::RemoteExecuteForceText(
  2730. TExecuteFileBy ExecuteFileBy, const TEditorData * ExternalEditor)
  2731. {
  2732. DebugAssert((ExecuteFileBy == efExternalEditor) ==
  2733. ((ExternalEditor != NULL) && (ExternalEditor->Editor == edExternal)));
  2734. DebugAssert(ExecuteFileBy != efDefaultEditor);
  2735. return
  2736. ((ExecuteFileBy == efInternalEditor)) ||
  2737. ((ExecuteFileBy == efExternalEditor) && ExternalEditor->ExternalEditorText);
  2738. }
  2739. //---------------------------------------------------------------------------
  2740. void __fastcall TCustomScpExplorerForm::CustomExecuteFile(TOperationSide Side,
  2741. TExecuteFileBy ExecuteFileBy, UnicodeString FileName, UnicodeString OriginalFileName,
  2742. const TEditorData * ExternalEditor, UnicodeString LocalRootDirectory,
  2743. UnicodeString RemoteDirectory)
  2744. {
  2745. DebugAssert(!WinConfiguration->DisableOpenEdit);
  2746. DebugAssert((ExecuteFileBy == efExternalEditor) ==
  2747. ((ExternalEditor != NULL) && (ExternalEditor->Editor == edExternal)));
  2748. DebugAssert(ExecuteFileBy != efDefaultEditor);
  2749. Side = GetSide(Side);
  2750. std::unique_ptr<TEditedFileData> Data(new TEditedFileData);
  2751. if (Side == osRemote)
  2752. {
  2753. Data->Terminal = Terminal;
  2754. Data->Queue = Queue;
  2755. Data->SessionData = CloneCurrentSessionData();
  2756. Data->ForceText = RemoteExecuteForceText(ExecuteFileBy, ExternalEditor);
  2757. Data->RemoteDirectory = RemoteDirectory;
  2758. Data->SessionName = Terminal->SessionData->SessionName;
  2759. Data->LocalRootDirectory = LocalRootDirectory;
  2760. Data->OriginalFileName = OriginalFileName;
  2761. Data->Command = L""; // will be changed later for external editor
  2762. }
  2763. if (ExecuteFileBy == efInternalEditor)
  2764. {
  2765. if (Side == osRemote)
  2766. {
  2767. UnicodeString Caption = UnixIncludeTrailingBackslash(RemoteDirectory) + OriginalFileName +
  2768. L" - " + Terminal->SessionData->SessionName;
  2769. TForm * Editor;
  2770. try
  2771. {
  2772. TNotifyEvent OnFileChanged = NULL;
  2773. TNotifyEvent OnSaveAll = NULL;
  2774. TAnyModifiedEvent OnAnyModified = NULL;
  2775. // Edited files are uploaded in background queue, what we do not support with encrypted files
  2776. if (Terminal->IsCapable[fsBackgroundTransfers])
  2777. {
  2778. OnFileChanged = FEditorManager->FileChanged;
  2779. OnSaveAll = SaveAllInternalEditors;
  2780. OnAnyModified = AnyInternalEditorModified;
  2781. }
  2782. Editor = ShowEditorForm(FileName, this, OnFileChanged,
  2783. FEditorManager->FileReload, FEditorManager->FileClosed,
  2784. OnSaveAll, OnAnyModified,
  2785. Caption, FStandaloneEditing, SessionColor, Terminal->SessionData->InternalEditorEncoding);
  2786. }
  2787. catch(...)
  2788. {
  2789. if (!LocalRootDirectory.IsEmpty())
  2790. {
  2791. RecursiveDeleteFile(ExcludeTrailingBackslash(LocalRootDirectory), false);
  2792. }
  2793. throw;
  2794. }
  2795. FEditorManager->AddFileInternal(FileName, Data.release(), Editor);
  2796. }
  2797. else
  2798. {
  2799. DebugAssert(!FStandaloneEditing);
  2800. TForm * Editor =
  2801. ShowEditorForm(FileName, this, NULL, NULL, LocalEditorClosed,
  2802. SaveAllInternalEditors, AnyInternalEditorModified,
  2803. L"", false, SessionColor, -1);
  2804. FLocalEditors->Add(Editor);
  2805. }
  2806. }
  2807. else
  2808. {
  2809. HANDLE Process;
  2810. if (ExecuteFileBy == efExternalEditor)
  2811. {
  2812. UnicodeString Program, Params, Dir;
  2813. Data->Command = ExternalEditor->ExternalEditor;
  2814. ReformatFileNameCommand(Data->Command);
  2815. SplitCommand(Data->Command, Program, Params, Dir);
  2816. Params = ExpandFileNameCommand(Params, FileName);
  2817. Program = ExpandEnvironmentVariables(Program);
  2818. if (!ExecuteShell(Program, Params, Process))
  2819. {
  2820. throw EOSExtException(FMTLOAD(EDITOR_ERROR, (Program)));
  2821. }
  2822. }
  2823. else
  2824. {
  2825. DebugAssert(Side == osRemote);
  2826. if (!ExecuteShell(FileName, L"", Process))
  2827. {
  2828. throw EOSExtException(FMTLOAD(EXECUTE_FILE_ERROR, (FileName)));
  2829. }
  2830. }
  2831. if ((Side == osLocal) ||
  2832. ((ExecuteFileBy == efShell) &&
  2833. !WinConfiguration->Editor.SDIShellEditor) ||
  2834. ((ExecuteFileBy == efExternalEditor) &&
  2835. !ExternalEditor->SDIExternalEditor))
  2836. {
  2837. // no need for handle
  2838. if (Process != NULL)
  2839. {
  2840. DebugCheck(CloseHandle(Process));
  2841. }
  2842. Process = INVALID_HANDLE_VALUE;
  2843. }
  2844. else
  2845. {
  2846. if (Process == NULL)
  2847. {
  2848. throw ExtException(LoadStr(OPEN_FILE_NO_PROCESS2), L"", HELP_OPEN_FILE_NO_PROCESS);
  2849. }
  2850. }
  2851. if (Side == osRemote)
  2852. {
  2853. FEditorManager->AddFileExternal(FileName, Data.release(), Process);
  2854. }
  2855. }
  2856. }
  2857. //---------------------------------------------------------------------------
  2858. void __fastcall TCustomScpExplorerForm::SaveInternalEditor(
  2859. const UnicodeString /*FileName*/, TEditedFileData * /*Data*/, TObject * Token,
  2860. void * /*Arg*/)
  2861. {
  2862. if (Token != NULL)
  2863. {
  2864. EditorFormFileSave(static_cast<TForm *>(Token));
  2865. }
  2866. }
  2867. //---------------------------------------------------------------------------
  2868. void __fastcall TCustomScpExplorerForm::SaveAllInternalEditors(TObject * /*Sender*/)
  2869. {
  2870. for (int Index = 0; Index < FLocalEditors->Count; Index++)
  2871. {
  2872. EditorFormFileSave(static_cast<TForm *>(FLocalEditors->Items[Index]));
  2873. }
  2874. FEditorManager->ProcessFiles(SaveInternalEditor, NULL);
  2875. }
  2876. //---------------------------------------------------------------------------
  2877. void __fastcall TCustomScpExplorerForm::InternalEditorModified(
  2878. const UnicodeString /*FileName*/, TEditedFileData * /*Data*/, TObject * Token,
  2879. void * Arg)
  2880. {
  2881. if ((Token != NULL) &&
  2882. IsEditorFormModified(static_cast<TForm *>(Token)))
  2883. {
  2884. *static_cast<bool *>(Arg) = true;
  2885. }
  2886. }
  2887. //---------------------------------------------------------------------------
  2888. void __fastcall TCustomScpExplorerForm::AnyInternalEditorModified(
  2889. TObject * /*Sender*/, bool & Modified)
  2890. {
  2891. for (int Index = 0; !Modified && (Index < FLocalEditors->Count); Index++)
  2892. {
  2893. if (IsEditorFormModified(static_cast<TForm *>(FLocalEditors->Items[Index])))
  2894. {
  2895. Modified = true;
  2896. }
  2897. }
  2898. if (!Modified)
  2899. {
  2900. FEditorManager->ProcessFiles(InternalEditorModified, &Modified);
  2901. }
  2902. }
  2903. //---------------------------------------------------------------------------
  2904. void __fastcall TCustomScpExplorerForm::LocalEditorClosed(TObject * Sender, bool /*Forced*/)
  2905. {
  2906. DebugCheck(FLocalEditors->Extract(Sender) >= 0);
  2907. }
  2908. //---------------------------------------------------------------------------
  2909. void __fastcall TCustomScpExplorerForm::TemporaryDirectoryForRemoteFiles(
  2910. UnicodeString RemoteDirectory, TCopyParamType CopyParam,
  2911. UnicodeString & Result, UnicodeString & RootDirectory)
  2912. {
  2913. if (!WinConfiguration->TemporaryDirectoryDeterministic)
  2914. {
  2915. RootDirectory = IncludeTrailingBackslash(WinConfiguration->TemporaryDir());
  2916. Result = RootDirectory;
  2917. }
  2918. else
  2919. {
  2920. RootDirectory = L"";
  2921. Result = WinConfiguration->ExpandedTemporaryDirectory();
  2922. Result = IncludeTrailingBackslash(Result);
  2923. }
  2924. if (WinConfiguration->TemporaryDirectoryAppendSession)
  2925. {
  2926. Result = IncludeTrailingBackslash(Result + MakeValidFileName(Terminal->SessionData->SessionName));
  2927. }
  2928. if (WinConfiguration->TemporaryDirectoryAppendPath)
  2929. {
  2930. if (!RemoteDirectory.IsEmpty() && (RemoteDirectory[1] == L'/'))
  2931. {
  2932. RemoteDirectory.Delete(1, 1);
  2933. }
  2934. Result = IncludeTrailingBackslash(Result + CopyParam.ValidLocalPath(FromUnixPath(RemoteDirectory)));
  2935. }
  2936. if (!ForceDirectories(ApiPath(Result)))
  2937. {
  2938. throw EOSExtException(FMTLOAD(CREATE_TEMP_DIR_ERROR, (Result)));
  2939. }
  2940. }
  2941. //---------------------------------------------------------------------------
  2942. void __fastcall TCustomScpExplorerForm::TemporarilyDownloadFiles(
  2943. TStrings * FileList, bool ForceText, UnicodeString & RootTempDir, UnicodeString & TempDir,
  2944. bool AllFiles, bool GetTargetNames, bool AutoOperation)
  2945. {
  2946. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  2947. if (ForceText)
  2948. {
  2949. CopyParam.TransferMode = tmAscii;
  2950. }
  2951. TemporaryFileCopyParam(CopyParam);
  2952. if (AllFiles)
  2953. {
  2954. CopyParam.IncludeFileMask = TFileMasks();
  2955. }
  2956. if (TempDir.IsEmpty())
  2957. {
  2958. TemporaryDirectoryForRemoteFiles(FTerminal->CurrentDirectory, CopyParam, TempDir, RootTempDir);
  2959. }
  2960. DebugAssert(!FAutoOperation);
  2961. FAutoOperation = AutoOperation;
  2962. Terminal->ExceptionOnFail = true;
  2963. try
  2964. {
  2965. try
  2966. {
  2967. // turn off confirmations, as for MDI editors we may possibly download
  2968. // the same file over
  2969. Terminal->CopyToLocal(
  2970. FileList, TempDir, &CopyParam, cpNoConfirmation | cpTemporary, NULL);
  2971. if (GetTargetNames)
  2972. {
  2973. for (int i = 0; i < FileList->Count; i++)
  2974. {
  2975. FileList->Strings[i] =
  2976. Terminal->ChangeFileName(&CopyParam, UnixExtractFileName(FileList->Strings[i]), osRemote, false);
  2977. }
  2978. }
  2979. }
  2980. catch(...)
  2981. {
  2982. if (!RootTempDir.IsEmpty())
  2983. {
  2984. RecursiveDeleteFile(ExcludeTrailingBackslash(RootTempDir), false);
  2985. }
  2986. throw;
  2987. }
  2988. }
  2989. __finally
  2990. {
  2991. FAutoOperation = false;
  2992. Terminal->ExceptionOnFail = false;
  2993. }
  2994. }
  2995. //---------------------------------------------------------------------------
  2996. void __fastcall TCustomScpExplorerForm::EditorAutoConfig()
  2997. {
  2998. // Do not waste time checking for default editor list the next time,
  2999. // or testing if default editor is not notepad.
  3000. bool TryNextTime = false;
  3001. UnicodeString UsageState;
  3002. if (!WinConfiguration->EditorList->IsDefaultList())
  3003. {
  3004. UsageState = "H";
  3005. }
  3006. else
  3007. {
  3008. UnicodeString Executable;
  3009. UnicodeString ExecutableDescription;
  3010. if (DetectSystemExternalEditor(false, Executable, ExecutableDescription, UsageState, TryNextTime))
  3011. {
  3012. UnicodeString Message =
  3013. FMTLOAD(EDITOR_AUTO_CONFIG2, (ExecutableDescription, ExecutableDescription));
  3014. unsigned int Answer =
  3015. MessageDialog(Message, qtConfirmation, qaOK | qaCancel, HELP_EDITOR_AUTO_CONFIG);
  3016. if (Answer != qaOK)
  3017. {
  3018. UsageState = "R";
  3019. }
  3020. else
  3021. {
  3022. UsageState = "A";
  3023. TEditorData EditorData;
  3024. EditorData.Editor = edExternal;
  3025. EditorData.ExternalEditor = FormatCommand(Executable, L"");
  3026. EditorData.DecideExternalEditorText();
  3027. TEditorList EditorList;
  3028. EditorList = *WinConfiguration->EditorList;
  3029. EditorList.Insert(0, new TEditorPreferences(EditorData));
  3030. WinConfiguration->EditorList = &EditorList;
  3031. }
  3032. }
  3033. }
  3034. WinConfiguration->OfferedEditorAutoConfig = !TryNextTime;
  3035. WinConfiguration->Usage->Set(L"EditorAutoConfig", UsageState);
  3036. }
  3037. //---------------------------------------------------------------------------
  3038. void __fastcall TCustomScpExplorerForm::ExecuteFileNormalize(
  3039. TExecuteFileBy & ExecuteFileBy, const TEditorData *& ExternalEditor,
  3040. const UnicodeString & FileName, bool Local, const TFileMasks::TParams & MaskParams)
  3041. {
  3042. if (ExecuteFileBy == efDefaultEditor)
  3043. {
  3044. if (!WinConfiguration->OfferedEditorAutoConfig)
  3045. {
  3046. EditorAutoConfig();
  3047. }
  3048. const TEditorPreferences * Editor =
  3049. WinConfiguration->DefaultEditorForFile(FileName, Local, MaskParams);
  3050. if ((Editor == NULL) || (Editor->Data->Editor == edInternal))
  3051. {
  3052. ExecuteFileBy = efInternalEditor;
  3053. ExternalEditor = NULL;
  3054. }
  3055. else if (Editor->Data->Editor == edOpen)
  3056. {
  3057. ExecuteFileBy = efShell;
  3058. ExternalEditor = NULL;
  3059. }
  3060. else
  3061. {
  3062. ExecuteFileBy = efExternalEditor;
  3063. ExternalEditor = Editor->Data;
  3064. }
  3065. }
  3066. }
  3067. //---------------------------------------------------------------------------
  3068. void __fastcall TCustomScpExplorerForm::ExecuteFile(TOperationSide Side,
  3069. TExecuteFileBy ExecuteFileBy, const TEditorData * ExternalEditor,
  3070. UnicodeString FullFileName, TObject * Object, const TFileMasks::TParams & MaskParams)
  3071. {
  3072. UnicodeString OriginalFileName;
  3073. UnicodeString LocalRootDirectory;
  3074. UnicodeString RemoteDirectory;
  3075. ExecuteFileNormalize(ExecuteFileBy, ExternalEditor, FullFileName,
  3076. (Side == osLocal), MaskParams);
  3077. UnicodeString Counter;
  3078. UnicodeString LocalFileName;
  3079. bool Handled = false;
  3080. if (Side == osRemote)
  3081. {
  3082. // We need to trim VMS version here, so that we use name without version
  3083. // when uploading back to create a new version of the file
  3084. OriginalFileName = FTerminal->GetBaseFileName(UnixExtractFileName(FullFileName));
  3085. RemoteDirectory = UnixExtractFilePath(FullFileName);
  3086. TObject * Token = NULL;
  3087. UnicodeString LocalDirectory;
  3088. if (!FEditorManager->CanAddFile(RemoteDirectory, OriginalFileName,
  3089. Terminal->SessionData->SessionName, Token, LocalRootDirectory,
  3090. LocalDirectory))
  3091. {
  3092. if (Token != NULL)
  3093. {
  3094. TForm * Form = dynamic_cast<TForm *>(Token);
  3095. if (Form->WindowState == wsMinimized)
  3096. {
  3097. ShowWindow(Form->Handle, SW_RESTORE);
  3098. }
  3099. else
  3100. {
  3101. Form->SetFocus();
  3102. }
  3103. Handled = true;
  3104. }
  3105. else
  3106. {
  3107. throw Exception(FMTLOAD(ALREADY_EDITED_EXTERNALLY_OR_UPLOADED, (OriginalFileName)));
  3108. }
  3109. }
  3110. if (!Handled)
  3111. {
  3112. TStringList * FileList1 = new TStringList();
  3113. try
  3114. {
  3115. FileList1->AddObject(FullFileName, Object);
  3116. TemporarilyDownloadFiles(FileList1,
  3117. RemoteExecuteForceText(ExecuteFileBy, ExternalEditor),
  3118. LocalRootDirectory, LocalDirectory, true, true, true);
  3119. LocalFileName = LocalDirectory + FileList1->Strings[0];
  3120. }
  3121. __finally
  3122. {
  3123. delete FileList1;
  3124. }
  3125. switch (ExecuteFileBy)
  3126. {
  3127. case efShell:
  3128. Counter = "RemoteFilesExecuted";
  3129. break;
  3130. case efInternalEditor:
  3131. Counter = "RemoteFilesOpenedInInternalEditor";
  3132. break;
  3133. case efExternalEditor:
  3134. Counter = "RemoteFilesOpenedInExternalEditor";
  3135. break;
  3136. default:
  3137. DebugFail();
  3138. }
  3139. }
  3140. }
  3141. else
  3142. {
  3143. LocalFileName = FullFileName;
  3144. OriginalFileName = ExtractFileName(FullFileName);
  3145. switch (ExecuteFileBy)
  3146. {
  3147. case efShell:
  3148. Counter = "LocalFilesExecuted";
  3149. break;
  3150. case efInternalEditor:
  3151. Counter = "LocalFilesOpenedInInternalEditor";
  3152. break;
  3153. case efExternalEditor:
  3154. Counter = "LocalFilesOpenedInExternalEditor";
  3155. break;
  3156. default:
  3157. DebugFail();
  3158. }
  3159. }
  3160. if (!Handled)
  3161. {
  3162. Configuration->Usage->Inc(Counter);
  3163. CustomExecuteFile(Side, ExecuteFileBy, LocalFileName, OriginalFileName,
  3164. ExternalEditor, LocalRootDirectory, RemoteDirectory);
  3165. }
  3166. }
  3167. //---------------------------------------------------------------------------
  3168. void __fastcall TCustomScpExplorerForm::ExecuteFile(TOperationSide Side,
  3169. TExecuteFileBy ExecuteFileBy, const TEditorData * ExternalEditor,
  3170. bool AllSelected, bool OnFocused)
  3171. {
  3172. DebugAssert(!WinConfiguration->DisableOpenEdit);
  3173. DebugAssert((ExecuteFileBy == efExternalEditor) ==
  3174. ((ExternalEditor != NULL) && (ExternalEditor->Editor == edExternal)));
  3175. Side = GetSide(Side);
  3176. TCustomDirView * DView = DirView(Side);
  3177. TStrings * FileList = AllSelected ?
  3178. DView->CreateFileList(OnFocused, Side == osLocal) :
  3179. DView->CreateFocusedFileList(Side == osLocal);
  3180. try
  3181. {
  3182. DebugAssert(AllSelected || (FileList->Count == 1));
  3183. for (int i = 0; i < FileList->Count; i++)
  3184. {
  3185. UnicodeString ListFileName = FileList->Strings[i];
  3186. UnicodeString FileNameOnly = (Side == osRemote) ?
  3187. UnixExtractFileName(ListFileName) : ExtractFileName(ListFileName);
  3188. TListItem * Item = DView->FindFileItem(FileNameOnly);
  3189. if (!DView->ItemIsDirectory(Item))
  3190. {
  3191. UnicodeString FullFileName;
  3192. if (Side == osRemote)
  3193. {
  3194. FullFileName = RemoteDirView->Path + ListFileName;
  3195. }
  3196. else
  3197. {
  3198. FullFileName = ListFileName;
  3199. }
  3200. TFileMasks::TParams MaskParams;
  3201. MaskParams.Size = DView->ItemFileSize(Item);
  3202. TDateTimePrecision Precision;
  3203. MaskParams.Modification = DView->ItemFileTime(Item, Precision);
  3204. ExecuteFile(Side, ExecuteFileBy, ExternalEditor, FullFileName,
  3205. FileList->Objects[i], MaskParams);
  3206. }
  3207. }
  3208. }
  3209. __finally
  3210. {
  3211. delete FileList;
  3212. }
  3213. }
  3214. //---------------------------------------------------------------------------
  3215. void __fastcall TCustomScpExplorerForm::TemporaryFileCopyParam(TCopyParamType & CopyParam)
  3216. {
  3217. // do not forget to add additional options to TemporarilyDownloadFiles, and AS
  3218. CopyParam.FileNameCase = ncNoChange;
  3219. CopyParam.PreserveRights = false;
  3220. CopyParam.PreserveReadOnly = false;
  3221. CopyParam.ReplaceInvalidChars = true;
  3222. CopyParam.IncludeFileMask = TFileMasks();
  3223. CopyParam.NewerOnly = false;
  3224. CopyParam.FileMask = L"";
  3225. }
  3226. //---------------------------------------------------------------------------
  3227. void __fastcall TCustomScpExplorerForm::ExecutedFileChanged(const UnicodeString FileName,
  3228. TEditedFileData * Data, HANDLE UploadCompleteEvent)
  3229. {
  3230. TTerminalManager * Manager = TTerminalManager::Instance();
  3231. if ((Data->Terminal == NULL) || !Data->Terminal->Active)
  3232. {
  3233. if (!NonVisualDataModule->Busy)
  3234. {
  3235. UnicodeString FileNameOnly = ExtractFileName(FileName);
  3236. UnicodeString EditInactiveSessionReopenAcceptedCounter = L"EditInactiveSessionReopenAccepted";
  3237. UnicodeString EditInactiveSessionReopenRejectedCounter = L"EditInactiveSessionReopenRejected";
  3238. if (Data->Terminal == NULL)
  3239. {
  3240. TTerminal * SameSiteTerminal = Manager->FindActiveTerminalForSite(Data->SessionData);
  3241. if (SameSiteTerminal != NULL)
  3242. {
  3243. UnicodeString Message =
  3244. FMTLOAD(EDIT_SESSION_REATTACH,
  3245. (FileNameOnly, Data->SessionName, FileNameOnly));
  3246. if (MessageDialog(Message, qtConfirmation, qaOK | qaCancel) == qaOK)
  3247. {
  3248. Data->Terminal = Terminal;
  3249. Data->Queue = Manager->FindQueueForTerminal(Terminal);
  3250. Data->SessionName = Terminal->SessionData->SessionName;
  3251. // We might also overwrite session data
  3252. Configuration->Usage->Inc(EditInactiveSessionReopenAcceptedCounter);
  3253. }
  3254. else
  3255. {
  3256. Configuration->Usage->Inc(EditInactiveSessionReopenRejectedCounter);
  3257. Abort();
  3258. }
  3259. }
  3260. }
  3261. // foreground session should reconnect itself
  3262. else if (Terminal != Data->Terminal)
  3263. {
  3264. UnicodeString Message =
  3265. MainInstructions(
  3266. FMTLOAD(EDIT_SESSION_RECONNECT, (Data->SessionName, FileNameOnly)));
  3267. if (MessageDialog(Message, qtConfirmation, qaOK | qaCancel) == qaOK)
  3268. {
  3269. Manager->SetActiveTerminalWithAutoReconnect(Data->Terminal);
  3270. Configuration->Usage->Inc(EditInactiveSessionReopenAcceptedCounter);
  3271. }
  3272. else
  3273. {
  3274. Configuration->Usage->Inc(EditInactiveSessionReopenRejectedCounter);
  3275. Abort();
  3276. }
  3277. }
  3278. }
  3279. if ((Data->Terminal == NULL) || !Data->Terminal->Active)
  3280. {
  3281. Configuration->Usage->Inc(L"EditInactiveSession");
  3282. // Prevent this when not idle (!NonVisualDataModule->Busy)?
  3283. throw Exception(FMTLOAD(EDIT_SESSION_CLOSED2,
  3284. (ExtractFileName(FileName), Data->SessionName)));
  3285. }
  3286. }
  3287. TStrings * FileList = new TStringList();
  3288. try
  3289. {
  3290. FileList->Add(FileName);
  3291. // consider using the same settings (preset) as when the file was downloaded
  3292. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  3293. TemporaryFileCopyParam(CopyParam);
  3294. if (Data->ForceText)
  3295. {
  3296. CopyParam.TransferMode = tmAscii;
  3297. }
  3298. // so i do not need to worry if masking algorithm works in all cases
  3299. // ("" means "copy file name", no masking is actually done)
  3300. if (ExtractFileName(FileName) == Data->OriginalFileName)
  3301. {
  3302. CopyParam.FileMask = L"";
  3303. }
  3304. else
  3305. {
  3306. CopyParam.FileMask = DelimitFileNameMask(Data->OriginalFileName);
  3307. }
  3308. DebugAssert(Data->Queue != NULL);
  3309. int Params = cpNoConfirmation | cpTemporary;
  3310. TQueueItem * QueueItem = new TUploadQueueItem(Data->Terminal, FileList,
  3311. Data->RemoteDirectory, &CopyParam, Params, true, false);
  3312. QueueItem->CompleteEvent = UploadCompleteEvent;
  3313. AddQueueItem(Data->Queue, QueueItem, Data->Terminal);
  3314. }
  3315. __finally
  3316. {
  3317. delete FileList;
  3318. }
  3319. }
  3320. //---------------------------------------------------------------------------
  3321. void __fastcall TCustomScpExplorerForm::ExecutedFileReload(
  3322. const UnicodeString FileName, const TEditedFileData * Data)
  3323. {
  3324. // Sanity check, we should not be busy otherwise user would not be able to click Reload button.
  3325. DebugAssert(!NonVisualDataModule->Busy);
  3326. if ((Data->Terminal == NULL) || !Data->Terminal->Active)
  3327. {
  3328. throw Exception(FMTLOAD(EDIT_SESSION_CLOSED_RELOAD,
  3329. (ExtractFileName(FileName), Data->SessionName)));
  3330. }
  3331. TTerminal * PrevTerminal = TTerminalManager::Instance()->ActiveTerminal;
  3332. TTerminalManager::Instance()->ActiveTerminal = Data->Terminal;
  3333. NonVisualDataModule->StartBusy();
  3334. try
  3335. {
  3336. std::unique_ptr<TRemoteFile> File;
  3337. UnicodeString RemoteFileName =
  3338. UnixIncludeTrailingBackslash(Data->RemoteDirectory) + Data->OriginalFileName;
  3339. FTerminal->ExceptionOnFail = true;
  3340. try
  3341. {
  3342. TRemoteFile * AFile = NULL;
  3343. FTerminal->ReadFile(RemoteFileName, AFile);
  3344. File.reset(AFile);
  3345. if (!File->HaveFullFileName)
  3346. {
  3347. File->FullFileName = RemoteFileName;
  3348. }
  3349. }
  3350. __finally
  3351. {
  3352. FTerminal->ExceptionOnFail = false;
  3353. }
  3354. std::unique_ptr<TStrings> FileList(new TStringList());
  3355. FileList->AddObject(RemoteFileName, File.get());
  3356. UnicodeString RootTempDir = Data->LocalRootDirectory;
  3357. UnicodeString TempDir = ExtractFilePath(FileName);
  3358. TemporarilyDownloadFiles(FileList.get(), Data->ForceText, RootTempDir,
  3359. TempDir, true, true, true);
  3360. // sanity check, the target file name should be still the same
  3361. DebugAssert(ExtractFileName(FileName) == FileList->Strings[0]);
  3362. }
  3363. __finally
  3364. {
  3365. NonVisualDataModule->EndBusy();
  3366. // it actually may not exist anymore...
  3367. TTerminalManager::Instance()->ActiveTerminal = PrevTerminal;
  3368. }
  3369. }
  3370. //---------------------------------------------------------------------------
  3371. void __fastcall TCustomScpExplorerForm::ExecutedFileEarlyClosed(
  3372. const TEditedFileData * Data, bool & KeepOpen)
  3373. {
  3374. // Command is set for external editors only (not for "shell" open).
  3375. if (!Data->Command.IsEmpty())
  3376. {
  3377. bool AnyFound = false;
  3378. bool AnyMDI = false;
  3379. bool AnyNonMDI = false;
  3380. bool AnyDetect = false;
  3381. TEditorList * EditorList = new TEditorList();
  3382. try
  3383. {
  3384. *EditorList = *WinConfiguration->EditorList;
  3385. for (int i = 0; i < EditorList->Count; i++)
  3386. {
  3387. const TEditorPreferences * Editor = EditorList->Editors[i];
  3388. if ((Editor->Data->Editor == edExternal) &&
  3389. (Editor->Data->ExternalEditor == Data->Command))
  3390. {
  3391. AnyFound = true;
  3392. if (Editor->Data->SDIExternalEditor)
  3393. {
  3394. AnyNonMDI = true;
  3395. if (Editor->Data->DetectMDIExternalEditor)
  3396. {
  3397. AnyDetect = true;
  3398. }
  3399. }
  3400. else
  3401. {
  3402. AnyMDI = true;
  3403. }
  3404. }
  3405. }
  3406. bool EnableMDI = false;
  3407. bool DisableDetect = false;
  3408. if (AnyMDI)
  3409. {
  3410. KeepOpen = true;
  3411. if (AnyNonMDI)
  3412. {
  3413. // there is at least one instance of the editor with MDI support enabled,
  3414. // and one with disabled, enable it for all instances
  3415. EnableMDI = true;
  3416. }
  3417. }
  3418. else if (AnyFound && !AnyDetect)
  3419. {
  3420. // at least once instance found but all have MDI autodetection disabled
  3421. // => close the file (default action)
  3422. }
  3423. else
  3424. {
  3425. // no instance of the editor has MDI support enabled
  3426. TMessageParams Params;
  3427. if (AnyFound)
  3428. {
  3429. // there is at least one instance of the editor
  3430. Params.Params |= mpNeverAskAgainCheck;
  3431. }
  3432. unsigned int Answer = MessageDialog(FMTLOAD(EDITOR_EARLY_CLOSED2, (Data->OriginalFileName)), qtWarning,
  3433. qaYes | qaNo, HELP_EDITOR_EARLY_CLOSED, &Params);
  3434. switch (Answer)
  3435. {
  3436. case qaNeverAskAgain:
  3437. DisableDetect = true;
  3438. break;
  3439. case qaNo:
  3440. EnableMDI = true;
  3441. KeepOpen = true;
  3442. break;
  3443. }
  3444. }
  3445. if (AnyFound && (EnableMDI || DisableDetect))
  3446. {
  3447. bool Changed = false;
  3448. for (int i = 0; i < EditorList->Count; i++)
  3449. {
  3450. const TEditorPreferences * Editor = EditorList->Editors[i];
  3451. if ((Editor->Data->Editor == edExternal) &&
  3452. (Editor->Data->ExternalEditor == Data->Command) &&
  3453. ((EnableMDI && Editor->Data->SDIExternalEditor) ||
  3454. (DisableDetect && Editor->Data->DetectMDIExternalEditor)))
  3455. {
  3456. Changed = true;
  3457. TEditorPreferences * UpdatedEditor = new TEditorPreferences(*Editor);
  3458. if (EnableMDI)
  3459. {
  3460. UpdatedEditor->GetData()->SDIExternalEditor = false;
  3461. }
  3462. if (DisableDetect)
  3463. {
  3464. UpdatedEditor->GetData()->DetectMDIExternalEditor = false;
  3465. }
  3466. EditorList->Change(i, UpdatedEditor);
  3467. }
  3468. }
  3469. if (Changed)
  3470. {
  3471. WinConfiguration->EditorList = EditorList;
  3472. }
  3473. }
  3474. }
  3475. __finally
  3476. {
  3477. delete EditorList;
  3478. }
  3479. }
  3480. else
  3481. {
  3482. // "open" case
  3483. MessageDialog(FMTLOAD(APP_EARLY_CLOSED, (Data->OriginalFileName)), qtWarning,
  3484. qaOK, HELP_APP_EARLY_CLOSED);
  3485. }
  3486. }
  3487. //---------------------------------------------------------------------------
  3488. void __fastcall TCustomScpExplorerForm::ExecutedFileUploadComplete(TObject * Sender)
  3489. {
  3490. EditorFormFileUploadComplete(DebugNotNull(dynamic_cast<TForm *>(Sender)));
  3491. }
  3492. //---------------------------------------------------------------------------
  3493. void __fastcall TCustomScpExplorerForm::RemoteDirViewEnter(TObject * /*Sender*/)
  3494. {
  3495. SideEnter(osRemote);
  3496. }
  3497. //---------------------------------------------------------------------------
  3498. void __fastcall TCustomScpExplorerForm::RemoteDriveViewEnter(TObject * /*Sender*/)
  3499. {
  3500. MakeNextInTabOrder(RemoteDirView, RemoteDriveView);
  3501. SideEnter(osRemote);
  3502. }
  3503. //---------------------------------------------------------------------------
  3504. void __fastcall TCustomScpExplorerForm::SideEnter(TOperationSide Side)
  3505. {
  3506. FCurrentSide = Side;
  3507. if (Visible)
  3508. {
  3509. UpdateControls();
  3510. }
  3511. }
  3512. //---------------------------------------------------------------------------
  3513. void __fastcall TCustomScpExplorerForm::DeleteFiles(TOperationSide Side,
  3514. TStrings * FileList, bool Alternative)
  3515. {
  3516. TCustomDirView * DView = DirView(Side);
  3517. DView->SaveSelection();
  3518. DView->SaveSelectedNames();
  3519. DebugAssert(!FAlternativeDelete);
  3520. FAlternativeDelete = Alternative;
  3521. try
  3522. {
  3523. if (Side == osRemote)
  3524. {
  3525. DebugAssert(Terminal != NULL);
  3526. Terminal->DeleteFiles(FileList, FLAGMASK(Alternative, dfAlternative));
  3527. }
  3528. else
  3529. {
  3530. try
  3531. {
  3532. TTerminalManager::Instance()->LocalTerminal->DeleteLocalFiles(FileList, FLAGMASK(Alternative, dfAlternative));
  3533. }
  3534. __finally
  3535. {
  3536. ReloadLocalDirectory();
  3537. }
  3538. }
  3539. FAlternativeDelete = false;
  3540. }
  3541. catch(...)
  3542. {
  3543. FAlternativeDelete = false;
  3544. DView->DiscardSavedSelection();
  3545. throw;
  3546. }
  3547. DView->RestoreSelection();
  3548. }
  3549. //---------------------------------------------------------------------------
  3550. void __fastcall TCustomScpExplorerForm::LockFiles(TStrings * FileList, bool Lock)
  3551. {
  3552. DebugAssert(Terminal);
  3553. RemoteDirView->SaveSelection();
  3554. RemoteDirView->SaveSelectedNames();
  3555. try
  3556. {
  3557. if (Lock)
  3558. {
  3559. Terminal->LockFiles(FileList);
  3560. }
  3561. else
  3562. {
  3563. Terminal->UnlockFiles(FileList);
  3564. }
  3565. }
  3566. catch(...)
  3567. {
  3568. RemoteDirView->DiscardSavedSelection();
  3569. throw;
  3570. }
  3571. RemoteDirView->RestoreSelection();
  3572. }
  3573. //---------------------------------------------------------------------------
  3574. bool __fastcall TCustomScpExplorerForm::RemoteTransferDialog(TTerminal *& Session,
  3575. TStrings * FileList, UnicodeString & Target, UnicodeString & FileMask, bool & DirectCopy,
  3576. bool NoConfirmation, bool Move)
  3577. {
  3578. if (RemoteDriveView->DropTarget != NULL)
  3579. {
  3580. Target = RemoteDriveView->NodePathName(RemoteDriveView->DropTarget);
  3581. }
  3582. else if (RemoteDirView->DropTarget != NULL)
  3583. {
  3584. DebugAssert(RemoteDirView->ItemIsDirectory(RemoteDirView->DropTarget));
  3585. Target = RemoteDirView->ItemFullFileName(RemoteDirView->DropTarget);
  3586. }
  3587. else
  3588. {
  3589. Target = RemoteDirView->Path;
  3590. }
  3591. if (Session == NULL)
  3592. {
  3593. Session = TTerminalManager::Instance()->ActiveTerminal;
  3594. }
  3595. Target = UnixIncludeTrailingBackslash(Target);
  3596. if (FileList->Count == 1)
  3597. {
  3598. FileMask = DelimitFileNameMask(UnixExtractFileName(FileList->Strings[0]));
  3599. }
  3600. else
  3601. {
  3602. FileMask = AnyMask;
  3603. }
  3604. DirectCopy = FTerminal->IsCapable[fcRemoteCopy] || FTerminal->IsCapable[fcSecondaryShell];
  3605. bool Result = true;
  3606. if (!NoConfirmation)
  3607. {
  3608. bool Multi = (FileList->Count > 1);
  3609. if (Move)
  3610. {
  3611. Result = DoRemoteMoveDialog(Multi, Target, FileMask);
  3612. }
  3613. else
  3614. {
  3615. DebugAssert(Terminal != NULL);
  3616. // update Terminal->StateData->RemoteDirectory
  3617. UpdateTerminal(Terminal);
  3618. TStrings * Sessions = TTerminalManager::Instance()->TerminalList;
  3619. TStrings * Directories = new TStringList;
  3620. try
  3621. {
  3622. for (int Index = 0; Index < Sessions->Count; Index++)
  3623. {
  3624. TManagedTerminal * Terminal =
  3625. dynamic_cast<TManagedTerminal *>(Sessions->Objects[Index]);
  3626. Directories->Add(Terminal->StateData->RemoteDirectory);
  3627. }
  3628. TDirectRemoteCopy AllowDirectCopy;
  3629. if (FTerminal->IsCapable[fcRemoteCopy] || FTerminal->CommandSessionOpened)
  3630. {
  3631. DebugAssert(DirectCopy);
  3632. AllowDirectCopy = drcAllow;
  3633. }
  3634. else if (FTerminal->IsCapable[fcSecondaryShell])
  3635. {
  3636. DebugAssert(DirectCopy);
  3637. AllowDirectCopy = drcConfirmCommandSession;
  3638. }
  3639. else
  3640. {
  3641. DebugAssert(!DirectCopy);
  3642. AllowDirectCopy = drcDisallow;
  3643. }
  3644. void * ASession = Session;
  3645. Result = DoRemoteCopyDialog(Sessions, Directories, AllowDirectCopy,
  3646. Multi, ASession, Target, FileMask, DirectCopy, TTerminalManager::Instance()->ActiveTerminal);
  3647. Session = static_cast<TTerminal *>(ASession);
  3648. }
  3649. __finally
  3650. {
  3651. delete Directories;
  3652. }
  3653. }
  3654. }
  3655. return Result;
  3656. }
  3657. //---------------------------------------------------------------------------
  3658. bool __fastcall TCustomScpExplorerForm::RemoteTransferFiles(
  3659. TStrings * FileList, bool NoConfirmation, bool Move, TTerminal * Session)
  3660. {
  3661. bool DirectCopy;
  3662. UnicodeString Target, FileMask;
  3663. bool Result = RemoteTransferDialog(Session, FileList, Target, FileMask, DirectCopy, NoConfirmation, Move);
  3664. if (Result)
  3665. {
  3666. if (!Move && !DirectCopy)
  3667. {
  3668. Configuration->Usage->Inc("RemoteCopyTemp");
  3669. UnicodeString RootTempDir;
  3670. UnicodeString TempDir;
  3671. TemporarilyDownloadFiles(FileList, false, RootTempDir, TempDir, false, false, false);
  3672. TStrings * TemporaryFilesList = new TStringList();
  3673. try
  3674. {
  3675. TMakeLocalFileListParams MakeFileListParam;
  3676. MakeFileListParam.FileList = TemporaryFilesList;
  3677. MakeFileListParam.FileTimes = NULL;
  3678. MakeFileListParam.IncludeDirs = true;
  3679. MakeFileListParam.Recursive = false;
  3680. ProcessLocalDirectory(TempDir, FTerminal->MakeLocalFileList, &MakeFileListParam);
  3681. TTerminalManager::Instance()->ActiveTerminal = Session;
  3682. if (TemporaryFilesList->Count > 0)
  3683. {
  3684. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  3685. CopyParam.FileMask = FileMask;
  3686. DebugAssert(!FAutoOperation);
  3687. FAutoOperation = true;
  3688. FTerminal->CopyToRemote(TemporaryFilesList, Target, &CopyParam, cpTemporary, NULL);
  3689. }
  3690. }
  3691. __finally
  3692. {
  3693. delete TemporaryFilesList;
  3694. FAutoOperation = false;
  3695. if (!RootTempDir.IsEmpty())
  3696. {
  3697. RecursiveDeleteFile(ExcludeTrailingBackslash(RootTempDir), false);
  3698. }
  3699. }
  3700. }
  3701. else
  3702. {
  3703. RemoteDirView->SaveSelection();
  3704. RemoteDirView->SaveSelectedNames();
  3705. try
  3706. {
  3707. if (Move)
  3708. {
  3709. Configuration->Usage->Inc("RemoteMove");
  3710. Terminal->MoveFiles(FileList, Target, FileMask);
  3711. }
  3712. else
  3713. {
  3714. Configuration->Usage->Inc("RemoteCopyDirect");
  3715. DebugAssert(DirectCopy);
  3716. DebugAssert(Session == FTerminal);
  3717. if (FTerminal->IsCapable[fcRemoteCopy] ||
  3718. FTerminal->CommandSessionOpened ||
  3719. CommandSessionFallback())
  3720. {
  3721. Terminal->CopyFiles(FileList, Target, FileMask);
  3722. }
  3723. }
  3724. }
  3725. catch(...)
  3726. {
  3727. RemoteDirView->DiscardSavedSelection();
  3728. throw;
  3729. }
  3730. RemoteDirView->RestoreSelection();
  3731. }
  3732. }
  3733. return Result;
  3734. }
  3735. //---------------------------------------------------------------------------
  3736. void __fastcall TCustomScpExplorerForm::CreateRemoteDirectory(
  3737. const UnicodeString & Path, TRemoteProperties & Properties)
  3738. {
  3739. Properties.Valid = Properties.Valid << vpEncrypt;
  3740. Properties.Encrypt = GUIConfiguration->CurrentCopyParam.EncryptNewFiles;
  3741. RemoteDirView->CreateDirectoryEx(Path, &Properties);
  3742. }
  3743. //---------------------------------------------------------------------------
  3744. void __fastcall TCustomScpExplorerForm::CreateDirectory(TOperationSide Side)
  3745. {
  3746. Side = GetSide(Side);
  3747. TRemoteProperties Properties = GUIConfiguration->NewDirectoryProperties;
  3748. TRemoteProperties * AProperties = (Side == osRemote ? &Properties : NULL);
  3749. UnicodeString Name = LoadStr(NEW_FOLDER);
  3750. int AllowedChanges =
  3751. FLAGMASK((Side == osRemote) && Terminal->IsCapable[fcModeChanging], cpMode);
  3752. bool SaveSettings = false;
  3753. if (DoCreateDirectoryDialog(Name, AProperties, AllowedChanges, SaveSettings))
  3754. {
  3755. TWindowLock Lock(this);
  3756. if (Side == osRemote)
  3757. {
  3758. if (SaveSettings)
  3759. {
  3760. GUIConfiguration->NewDirectoryProperties = Properties;
  3761. }
  3762. CreateRemoteDirectory(Name, Properties);
  3763. }
  3764. else
  3765. {
  3766. DirView(Side)->CreateDirectory(Name);
  3767. }
  3768. }
  3769. }
  3770. //---------------------------------------------------------------------------
  3771. void __fastcall TCustomScpExplorerForm::HomeDirectory(TOperationSide Side)
  3772. {
  3773. TWindowLock Lock(this);
  3774. DirView(Side)->ExecuteHomeDirectory();
  3775. }
  3776. //---------------------------------------------------------------------------
  3777. void __fastcall TCustomScpExplorerForm::OpenDirectory(TOperationSide Side)
  3778. {
  3779. DoOpenDirectoryDialog(odBrowse, Side);
  3780. }
  3781. //---------------------------------------------------------------------------
  3782. bool __fastcall TCustomScpExplorerForm::OpenBookmark(TOperationSide Side, TBookmark * Bookmark)
  3783. {
  3784. UnicodeString Path = Bookmark->GetSideDirectory(Side);
  3785. bool Result = !Path.IsEmpty();
  3786. if (Result)
  3787. {
  3788. // While we might get here when the session is closed (from location profiles),
  3789. // it's not a problem as the Path setter is noop then
  3790. DirView(Side)->Path = Path;
  3791. }
  3792. return Result;
  3793. }
  3794. //---------------------------------------------------------------------------
  3795. void __fastcall TCustomScpExplorerForm::CalculateSize(
  3796. TStrings * FileList, __int64 & Size, TCalculateSizeStats & Stats,
  3797. bool & Close)
  3798. {
  3799. // terminal can be already closed (e.g. dropped connection)
  3800. if (Terminal != NULL)
  3801. {
  3802. try
  3803. {
  3804. Terminal->CalculateFilesSize(FileList, Size, 0, NULL, true, Stats);
  3805. }
  3806. catch(...)
  3807. {
  3808. if (!Terminal->Active)
  3809. {
  3810. Close = true;
  3811. }
  3812. throw;
  3813. }
  3814. }
  3815. }
  3816. //---------------------------------------------------------------------------
  3817. void __fastcall TCustomScpExplorerForm::CalculateChecksum(const UnicodeString & Alg,
  3818. TStrings * FileList, TCalculatedChecksumEvent OnCalculatedChecksum,
  3819. bool & Close)
  3820. {
  3821. // terminal can be already closed (e.g. dropped connection)
  3822. if (Terminal != NULL)
  3823. {
  3824. Configuration->Usage->Inc(L"ChecksumCalculated");
  3825. try
  3826. {
  3827. Terminal->CalculateFilesChecksum(Alg, FileList, NULL, OnCalculatedChecksum);
  3828. }
  3829. catch(...)
  3830. {
  3831. if (!Terminal->Active)
  3832. {
  3833. Close = true;
  3834. }
  3835. throw;
  3836. }
  3837. }
  3838. }
  3839. //---------------------------------------------------------------------------
  3840. bool __fastcall TCustomScpExplorerForm::SetProperties(TOperationSide Side, TStrings * FileList)
  3841. {
  3842. bool Result;
  3843. if (Side == osRemote)
  3844. {
  3845. TRemoteTokenList * GroupList = NULL;
  3846. TRemoteTokenList * UserList = NULL;
  3847. try
  3848. {
  3849. TRemoteProperties CurrentProperties;
  3850. if (Terminal->LoadFilesProperties(FileList))
  3851. {
  3852. RemoteDirView->Invalidate();
  3853. }
  3854. bool CapableGroupChanging = Terminal->IsCapable[fcGroupChanging];
  3855. bool CapableOwnerChanging = Terminal->IsCapable[fcOwnerChanging];
  3856. if (CapableGroupChanging || CapableOwnerChanging)
  3857. {
  3858. if (CapableGroupChanging)
  3859. {
  3860. GroupList = Terminal->Groups->Duplicate();
  3861. }
  3862. if (CapableOwnerChanging)
  3863. {
  3864. UserList = Terminal->Users->Duplicate();
  3865. }
  3866. TRemoteDirectory * Files = Terminal->Files;
  3867. int Count = Files->Count;
  3868. if (Count > 100)
  3869. {
  3870. Count = 100;
  3871. }
  3872. for (int Index = 0; Index < Count; Index++)
  3873. {
  3874. TRemoteFile * File = Files->Files[Index];
  3875. if (CapableGroupChanging)
  3876. {
  3877. GroupList->AddUnique(File->Group);
  3878. }
  3879. if (CapableOwnerChanging)
  3880. {
  3881. UserList->AddUnique(File->Owner);
  3882. }
  3883. }
  3884. // if we haven't collected tokens for all files in current directory,
  3885. // make sure we collect them at least for all selected files.
  3886. // (note that so far the files in FileList has to be from current direcotry)
  3887. if (Count < Files->Count)
  3888. {
  3889. for (int Index = 0; Index < FileList->Count; Index++)
  3890. {
  3891. TRemoteFile * File = (TRemoteFile *)(FileList->Objects[Index]);
  3892. if (CapableGroupChanging)
  3893. {
  3894. GroupList->AddUnique(File->Group);
  3895. }
  3896. if (CapableOwnerChanging)
  3897. {
  3898. UserList->AddUnique(File->Owner);
  3899. }
  3900. }
  3901. }
  3902. }
  3903. CurrentProperties = TRemoteProperties::CommonProperties(FileList);
  3904. int Flags = 0;
  3905. if (Terminal->IsCapable[fcModeChanging]) Flags |= cpMode;
  3906. if (CapableOwnerChanging) Flags |= cpOwner;
  3907. if (CapableGroupChanging) Flags |= cpGroup;
  3908. TCalculateChecksumEvent CalculateChecksumEvent = NULL;
  3909. if (Terminal->IsCapable[fcCalculatingChecksum])
  3910. {
  3911. CalculateChecksumEvent = CalculateChecksum;
  3912. }
  3913. std::unique_ptr<TStrings> ChecksumAlgs(new TStringList());
  3914. Terminal->GetSupportedChecksumAlgs(ChecksumAlgs.get());
  3915. TRemoteProperties NewProperties = CurrentProperties;
  3916. Result =
  3917. DoPropertiesDialog(FileList, RemoteDirView->PathName,
  3918. GroupList, UserList, ChecksumAlgs.get(), &NewProperties, Flags,
  3919. Terminal->IsCapable[fcGroupOwnerChangingByID],
  3920. CalculateSize, CalculateChecksumEvent);
  3921. if (Result)
  3922. {
  3923. NewProperties = TRemoteProperties::ChangedProperties(CurrentProperties, NewProperties);
  3924. Terminal->ChangeFilesProperties(FileList, &NewProperties);
  3925. }
  3926. }
  3927. __finally
  3928. {
  3929. delete GroupList;
  3930. delete UserList;
  3931. }
  3932. }
  3933. else
  3934. {
  3935. DirView(Side)->DisplayPropertiesMenu();
  3936. Result = true;
  3937. }
  3938. return Result;
  3939. }
  3940. //---------------------------------------------------------------------------
  3941. void __fastcall TCustomScpExplorerForm::KeyProcessed(Word & Key, TShiftState Shift)
  3942. {
  3943. if (Shift * AllKeyShiftStates() == (TShiftState() << ssAlt))
  3944. {
  3945. FIgnoreNextDialogChar = Key;
  3946. }
  3947. Key = 0;
  3948. }
  3949. //---------------------------------------------------------------------------
  3950. void __fastcall TCustomScpExplorerForm::CheckCustomCommandShortCut(
  3951. TCustomCommandList * List, Word & Key, Classes::TShiftState Shift, TShortCut KeyShortCut)
  3952. {
  3953. const TCustomCommandType * Command = List->Find(KeyShortCut);
  3954. if (Command != NULL)
  3955. {
  3956. KeyProcessed(Key, Shift);
  3957. if (CustomCommandState(*Command, false, ccltAll) > 0)
  3958. {
  3959. ExecuteFileOperationCommand(foCustomCommand, osRemote,
  3960. false, false, const_cast<TCustomCommandType *>(Command));
  3961. }
  3962. }
  3963. }
  3964. //---------------------------------------------------------------------------
  3965. void __fastcall TCustomScpExplorerForm::KeyDown(Word & Key, Classes::TShiftState Shift)
  3966. {
  3967. if (QueueView3->Focused() && (QueueView3->OnKeyDown != NULL))
  3968. {
  3969. QueueView3->OnKeyDown(QueueView3, Key, Shift);
  3970. }
  3971. if (!DirView(osCurrent)->IsEditing())
  3972. {
  3973. TShortCut KeyShortCut = ShortCut(Key, Shift);
  3974. for (int Index = 0; Index < NonVisualDataModule->ExplorerActions->ActionCount; Index++)
  3975. {
  3976. TAction * Action = (TAction *)NonVisualDataModule->ExplorerActions->Actions[Index];
  3977. if (((Action->ShortCut == KeyShortCut) ||
  3978. (Action->SecondaryShortCuts->IndexOfShortCut(KeyShortCut) >= 0)) &&
  3979. AllowedAction(Action, aaShortCut))
  3980. {
  3981. // Has to be called before the action as the dialog char is already in queue.
  3982. // So when the action consumes message queue, we already need to have the
  3983. // FIgnoreNextDialogChar set
  3984. KeyProcessed(Key, Shift);
  3985. // Reset reference to previous component (when menu/toolbar was clicked).
  3986. // Needed to detect that action was invoked by keyboard shortcut
  3987. // in TNonVisualDataModule::ExplorerActionsExecute
  3988. Action->ActionComponent = NULL;
  3989. Action->Execute();
  3990. return;
  3991. }
  3992. }
  3993. for (int i = 0; i < TTerminalManager::Instance()->Count; i++)
  3994. {
  3995. if (NonVisualDataModule->OpenSessionShortCut(i) == KeyShortCut)
  3996. {
  3997. KeyProcessed(Key, Shift);
  3998. TTerminalManager::Instance()->ActiveTerminalIndex = i;
  3999. return;
  4000. }
  4001. }
  4002. if (Key == VK_TAB && Shift.Contains(ssCtrl))
  4003. {
  4004. KeyProcessed(Key, Shift);
  4005. TTerminalManager::Instance()->CycleTerminals(!Shift.Contains(ssShift));
  4006. }
  4007. TShortCut CustomShortCut = NormalizeCustomShortCut(KeyShortCut);
  4008. if (IsCustomShortCut(CustomShortCut))
  4009. {
  4010. CheckCustomCommandShortCut(WinConfiguration->CustomCommandList, Key, Shift, CustomShortCut);
  4011. CheckCustomCommandShortCut(WinConfiguration->ExtensionList, Key, Shift, CustomShortCut);
  4012. if (WinConfiguration->SharedBookmarks != NULL)
  4013. {
  4014. TBookmark * Bookmark = WinConfiguration->SharedBookmarks->FindByShortCut(CustomShortCut);
  4015. if ((Bookmark != NULL) &&
  4016. OpenBookmark(FCurrentSide, Bookmark))
  4017. {
  4018. KeyProcessed(Key, Shift);
  4019. }
  4020. }
  4021. }
  4022. }
  4023. TForm::KeyDown(Key, Shift);
  4024. }
  4025. //---------------------------------------------------------------------------
  4026. void __fastcall TCustomScpExplorerForm::InitStatusBar()
  4027. {
  4028. const TSessionInfo & SessionInfo = Terminal->GetSessionInfo();
  4029. const TFileSystemInfo & FileSystemInfo = Terminal->GetFileSystemInfo();
  4030. TTBXStatusBar * SessionStatusBar = (TTBXStatusBar *)GetComponent(fcStatusBar);
  4031. DebugAssert(Terminal);
  4032. int Offset = SessionStatusBar->Panels->Count - SessionPanelCount;
  4033. bool SecurityEnabled = !SessionInfo.SecurityProtocolName.IsEmpty();
  4034. SessionStatusBar->Panels->Items[Offset + 0]->Enabled = SecurityEnabled;
  4035. // expanded from ?: to avoid memory leaks
  4036. if (SecurityEnabled)
  4037. {
  4038. SessionStatusBar->Panels->Items[Offset + 0]->Hint =
  4039. FMTLOAD(STATUS_SECURE, (SessionInfo.SecurityProtocolName));
  4040. }
  4041. else
  4042. {
  4043. SessionStatusBar->Panels->Items[Offset + 0]->Hint = LoadStr(STATUS_INSECURE);
  4044. }
  4045. if (FileSystemInfo.ProtocolName.IsEmpty())
  4046. {
  4047. SessionStatusBar->Panels->Items[Offset + 1]->Caption = SessionInfo.ProtocolName;
  4048. }
  4049. else
  4050. {
  4051. SessionStatusBar->Panels->Items[Offset + 1]->Caption = FileSystemInfo.ProtocolName;
  4052. }
  4053. SessionStatusBar->Panels->Items[Offset + 1]->Hint = LoadStr(STATUS_PROTOCOL_HINT);
  4054. SessionStatusBar->Panels->Items[Offset + 2]->Enabled =
  4055. (!SessionInfo.CSCompression.IsEmpty() || !SessionInfo.SCCompression.IsEmpty());
  4056. UnicodeString CSCompressionStr = DefaultStr(SessionInfo.CSCompression, LoadStr(NO_STR));
  4057. UnicodeString SCCompressionStr = DefaultStr(SessionInfo.SCCompression, LoadStr(NO_STR));
  4058. UnicodeString CompressionStr = CSCompressionStr;
  4059. if (CSCompressionStr != SCCompressionStr)
  4060. {
  4061. CompressionStr += UnicodeString(L"/") + SCCompressionStr;
  4062. }
  4063. SessionStatusBar->Panels->Items[Offset + 2]->Hint = FMTLOAD(STATUS_COMPRESSION_HINT, (CompressionStr));
  4064. SessionStatusBar->Panels->Items[Offset + 3]->Hint = LoadStr(STATUS_DURATION_HINT);
  4065. UpdateStatusBar();
  4066. }
  4067. //---------------------------------------------------------------------------
  4068. void __fastcall TCustomScpExplorerForm::UpdateStatusBar()
  4069. {
  4070. TTBXStatusBar * SessionStatusBar = (TTBXStatusBar *)GetComponent(fcStatusBar);
  4071. DebugAssert(SessionStatusBar != NULL);
  4072. if (!Terminal || !Terminal->Active || Terminal->Status < ssOpened)
  4073. {
  4074. // note: (Terminal->Status < sshReady) currently never happens here,
  4075. // so STATUS_CONNECTING is never used
  4076. SessionStatusBar->SimplePanel = true;
  4077. SessionStatusBar->SimpleText = LoadStr(
  4078. !Terminal || !Terminal->Active ? STATUS_NOT_CONNECTED : STATUS_CONNECTING);
  4079. }
  4080. else
  4081. {
  4082. DebugAssert(Terminal);
  4083. SessionStatusBar->SimplePanel = false;
  4084. const TSessionInfo & SessionInfo = Terminal->GetSessionInfo();
  4085. if (!FNote.IsEmpty())
  4086. {
  4087. SessionStatusBar->Panels->Items[0]->Caption = FNote;
  4088. }
  4089. else
  4090. {
  4091. UpdateStatusPanelText(SessionStatusBar->Panels->Items[0]);
  4092. }
  4093. SessionStatusBar->Panels->Items[0]->Hint = FNoteHints;
  4094. SessionStatusBar->Panels->Items[SessionStatusBar->Panels->Count - 1]->Caption =
  4095. FormatDateTimeSpan(Configuration->TimeFormat, Now() - SessionInfo.LoginTime);
  4096. }
  4097. }
  4098. //---------------------------------------------------------------------------
  4099. void __fastcall TCustomScpExplorerForm::UpdateStatusPanelText(TTBXStatusPanel * Panel)
  4100. {
  4101. Panel->Caption = L"";
  4102. }
  4103. //---------------------------------------------------------------------------
  4104. void __fastcall TCustomScpExplorerForm::Idle()
  4105. {
  4106. if (FShowing || FStandaloneEditing)
  4107. {
  4108. FEditorManager->Check();
  4109. // make sure that Idle is called before update queue, as it may invoke QueueEvent
  4110. // that needs to know if queue view is visible (and it may be closed after queue update)
  4111. TTerminalManager::Instance()->Idle(FDoNotIdleCurrentTerminal > 0);
  4112. }
  4113. if (FShowing)
  4114. {
  4115. if (!NonVisualDataModule->Busy &&
  4116. // Menu is opened or drag&drop is going on
  4117. (Mouse->Capture == NULL))
  4118. {
  4119. if (FRefreshRemoteDirectory)
  4120. {
  4121. if ((Terminal != NULL) && (Terminal->Status == ssOpened))
  4122. {
  4123. Terminal->RefreshDirectory();
  4124. }
  4125. FRefreshRemoteDirectory = false;
  4126. }
  4127. if (FRefreshLocalDirectory)
  4128. {
  4129. ReloadLocalDirectory();
  4130. FRefreshLocalDirectory = false;
  4131. }
  4132. if (WinConfiguration->RefreshRemotePanel)
  4133. {
  4134. TManagedTerminal * ManagedTerminal =
  4135. dynamic_cast<TManagedTerminal *>(Terminal);
  4136. if ((ManagedTerminal != NULL) && (Terminal->Status == ssOpened) &&
  4137. (Now() - ManagedTerminal->DirectoryLoaded >
  4138. WinConfiguration->RefreshRemotePanelInterval))
  4139. {
  4140. RemoteDirView->ReloadDirectory();
  4141. }
  4142. }
  4143. }
  4144. }
  4145. if (FShowing || FStandaloneEditing)
  4146. {
  4147. if (FQueueStatusInvalidated)
  4148. {
  4149. UpdateQueueStatus(false);
  4150. }
  4151. RefreshQueueItems();
  4152. }
  4153. if (FShowing)
  4154. {
  4155. UpdateStatusBar();
  4156. }
  4157. FIgnoreNextDialogChar = 0;
  4158. }
  4159. //---------------------------------------------------------------------------
  4160. void __fastcall TCustomScpExplorerForm::UserActionTimer(TObject * /*Sender*/)
  4161. {
  4162. try
  4163. {
  4164. FUserActionTimer->Enabled = false;
  4165. if (IsQueueAutoPopup() && (FPendingQueueActionItem != NULL))
  4166. {
  4167. if (TQueueItem::IsUserActionStatus(FPendingQueueActionItem->Status))
  4168. {
  4169. FPendingQueueActionItem->ProcessUserAction();
  4170. }
  4171. }
  4172. }
  4173. __finally
  4174. {
  4175. FPendingQueueActionItem = NULL;
  4176. }
  4177. }
  4178. //---------------------------------------------------------------------------
  4179. void __fastcall TCustomScpExplorerForm::ApplicationMinimize(TObject * /*Sender*/)
  4180. {
  4181. if (WinConfiguration->MinimizeToTray)
  4182. {
  4183. UpdateTrayIcon();
  4184. FTrayIcon->Visible = true;
  4185. if (Visible)
  4186. {
  4187. ShowWindow(Handle, SW_HIDE);
  4188. }
  4189. }
  4190. }
  4191. //---------------------------------------------------------------------------
  4192. void __fastcall TCustomScpExplorerForm::ApplicationRestore(TObject * /*Sender*/)
  4193. {
  4194. // WORKAROUND
  4195. // When restoring maximized window from minimization,
  4196. // rarely some controls do not align properly.
  4197. // Two instances seen (both for Commander):
  4198. // - When restoring, window is temporarily narrower (not maximized),
  4199. // causing toolbars on TopDock to wrap and dock to expand horizontally.
  4200. // Once maximized already, top dock shinks back, but the session PageControl,
  4201. // do not align up, leaving space between TopDock and PageControl.
  4202. // - Similar issue seen with LocalDirView not aligning down to status bar.
  4203. for (int Index = 0; Index < ControlCount; Index++)
  4204. {
  4205. RealignControl(Controls[Index]);
  4206. }
  4207. if (FTrayIcon->Visible)
  4208. {
  4209. FTrayIcon->Visible = false;
  4210. if (Visible)
  4211. {
  4212. ShowWindow(Handle, SW_SHOW);
  4213. }
  4214. }
  4215. if (FNeedSession && DebugAlwaysTrue(Terminal == NULL))
  4216. {
  4217. FNeedSession = false;
  4218. NonVisualDataModule->StartBusy();
  4219. try
  4220. {
  4221. NeedSession(false);
  4222. }
  4223. __finally
  4224. {
  4225. NonVisualDataModule->EndBusy();
  4226. }
  4227. }
  4228. }
  4229. //---------------------------------------------------------------------------
  4230. void __fastcall TCustomScpExplorerForm::UpdateTrayIcon()
  4231. {
  4232. FTrayIcon->Hint = Caption;
  4233. }
  4234. //---------------------------------------------------------------------------
  4235. void __fastcall TCustomScpExplorerForm::ApplicationTitleChanged()
  4236. {
  4237. UpdateTrayIcon();
  4238. }
  4239. //---------------------------------------------------------------------------
  4240. void __fastcall TCustomScpExplorerForm::RestoreApp()
  4241. {
  4242. // workaround
  4243. // TApplication.WndProc sets TApplication.FAppIconic to false,
  4244. // on WM_ACTIVATEAPP, what renders TApplication.Restore no-op function.
  4245. // But WM_ACTIVATEAPP message can be received even when
  4246. // the main window is minimized to the tray and internal editor window is focused
  4247. // (after another application was previously active)
  4248. if (::IsIconic(Handle))
  4249. {
  4250. if (!IsAppIconic())
  4251. {
  4252. SetAppIconic(true);
  4253. }
  4254. }
  4255. ::ApplicationRestore();
  4256. Application->BringToFront();
  4257. }
  4258. //---------------------------------------------------------------------------
  4259. void __fastcall TCustomScpExplorerForm::TrayIconClick(TObject * /*Sender*/)
  4260. {
  4261. RestoreApp();
  4262. }
  4263. //---------------------------------------------------------------------------
  4264. void __fastcall TCustomScpExplorerForm::NewSession(bool FromSite, const UnicodeString & SessionUrl)
  4265. {
  4266. if (OpenInNewWindow())
  4267. {
  4268. // todo: Pass FromSite
  4269. ExecuteNewInstance(SessionUrl);
  4270. }
  4271. else
  4272. {
  4273. TTerminalManager::Instance()->NewSession(FromSite, SessionUrl);
  4274. }
  4275. }
  4276. //---------------------------------------------------------------------------
  4277. UnicodeString __fastcall TCustomScpExplorerForm::CreateHiddenDuplicateSession()
  4278. {
  4279. UnicodeString SessionName = StoredSessions->HiddenPrefix + Terminal->SessionData->SessionName;
  4280. // current working directories become defaults here, what is not right
  4281. std::unique_ptr<TSessionData> SessionData(CloneCurrentSessionData());
  4282. StoredSessions->NewSession(SessionName, SessionData.get());
  4283. // modified only, explicit
  4284. StoredSessions->Save(false, true);
  4285. // encode session name because of slashes in hierarchical sessions
  4286. return EncodeUrlString(SessionName);
  4287. }
  4288. //---------------------------------------------------------------------------
  4289. void __fastcall TCustomScpExplorerForm::DuplicateSession()
  4290. {
  4291. if (OpenInNewWindow())
  4292. {
  4293. ExecuteNewInstance(CreateHiddenDuplicateSession());
  4294. }
  4295. else
  4296. {
  4297. // current working directories become defaults here, what is not right
  4298. std::unique_ptr<TSessionData> SessionData(CloneCurrentSessionData());
  4299. TTerminalManager * Manager = TTerminalManager::Instance();
  4300. TTerminal * Terminal = Manager->NewTerminal(SessionData.get());
  4301. Manager->ActiveTerminal = Terminal;
  4302. }
  4303. }
  4304. //---------------------------------------------------------------------------
  4305. bool __fastcall TCustomScpExplorerForm::CanCloseQueue()
  4306. {
  4307. DebugAssert(FQueue != NULL);
  4308. bool Result = FQueue->IsEmpty;
  4309. if (!Result)
  4310. {
  4311. SetFocus();
  4312. Result = (MessageDialog(LoadStr(PENDING_QUEUE_ITEMS2), qtWarning, qaOK | qaCancel, HELP_NONE) == qaOK);
  4313. }
  4314. return Result;
  4315. }
  4316. //---------------------------------------------------------------------------
  4317. void __fastcall TCustomScpExplorerForm::CloseSession()
  4318. {
  4319. if (CanCloseQueue())
  4320. {
  4321. TTerminalManager::Instance()->FreeActiveTerminal();
  4322. }
  4323. }
  4324. //---------------------------------------------------------------------------
  4325. void __fastcall TCustomScpExplorerForm::OpenStoredSession(TSessionData * Data)
  4326. {
  4327. if (OpenInNewWindow())
  4328. {
  4329. // encode session name because of slashes in hierarchical sessions
  4330. ExecuteNewInstance(EncodeUrlString(Data->Name));
  4331. }
  4332. else
  4333. {
  4334. TTerminalManager * Manager = TTerminalManager::Instance();
  4335. TTerminal * Terminal = Manager->NewTerminal(Data);
  4336. Manager->ActiveTerminal = Terminal;
  4337. }
  4338. }
  4339. //---------------------------------------------------------------------------
  4340. void __fastcall TCustomScpExplorerForm::OpenFolderOrWorkspace(const UnicodeString & Name)
  4341. {
  4342. if (OpenInNewWindow())
  4343. {
  4344. ExecuteNewInstance(Name);
  4345. }
  4346. else
  4347. {
  4348. TTerminalManager * Manager = TTerminalManager::Instance();
  4349. std::unique_ptr<TObjectList> DataList(new TObjectList());
  4350. StoredSessions->GetFolderOrWorkspace(Name, DataList.get());
  4351. Manager->ActiveTerminal = Manager->NewTerminals(DataList.get());
  4352. }
  4353. }
  4354. //---------------------------------------------------------------------------
  4355. void __fastcall TCustomScpExplorerForm::FormCloseQuery(TObject * /*Sender*/,
  4356. bool &CanClose)
  4357. {
  4358. if (Terminal != NULL)
  4359. {
  4360. if (Terminal->Active && WinConfiguration->ConfirmClosingSession)
  4361. {
  4362. unsigned int Result;
  4363. TMessageParams Params(mpNeverAskAgainCheck);
  4364. UnicodeString Message;
  4365. int Answers = qaOK | qaCancel;
  4366. if (TTerminalManager::Instance()->Count > 1)
  4367. {
  4368. if (!WinConfiguration->AutoSaveWorkspace)
  4369. {
  4370. Message = LoadStr(CLOSE_SESSIONS_WORKSPACE2);
  4371. Answers = qaYes | qaNo | qaCancel;
  4372. }
  4373. else
  4374. {
  4375. Message = MainInstructions(LoadStr(CLOSE_SESSIONS));
  4376. }
  4377. }
  4378. else
  4379. {
  4380. Message = MainInstructions(FMTLOAD(CLOSE_SESSION, (Terminal->SessionData->SessionName)));
  4381. }
  4382. if (WinConfiguration->AutoSaveWorkspace)
  4383. {
  4384. Message =
  4385. FORMAT("%s\n\n%s", (Message,
  4386. FMTLOAD(AUTO_WORKSPACE, (WorkspaceName()))));
  4387. }
  4388. SetFocus();
  4389. Result = MessageDialog(Message, qtConfirmation,
  4390. Answers, HELP_NONE, &Params);
  4391. if (Result == qaNeverAskAgain)
  4392. {
  4393. WinConfiguration->ConfirmClosingSession = false;
  4394. }
  4395. if (Result == qaNo)
  4396. {
  4397. CanClose = SaveWorkspace(true);
  4398. // note that the workspace will be saved redundatly again from FormClose
  4399. }
  4400. else
  4401. {
  4402. CanClose =
  4403. (Result == qaOK) ||
  4404. (Result == qaYes) || // CLOSE_SESSIONS_WORKSPACE variant
  4405. (Result == qaNeverAskAgain);
  4406. }
  4407. }
  4408. if (CanClose)
  4409. {
  4410. CanClose = CanCloseQueue();
  4411. }
  4412. }
  4413. if (CanClose)
  4414. {
  4415. CanClose =
  4416. FEditorManager->CloseInternalEditors(CloseInternalEditor) &&
  4417. FEditorManager->CloseExternalFilesWithoutProcess();
  4418. if (CanClose)
  4419. {
  4420. while (CanClose && (FLocalEditors->Count > 0))
  4421. {
  4422. int PrevCount = FLocalEditors->Count;
  4423. static_cast<TForm *>(FLocalEditors->Items[0])->Close();
  4424. CanClose = (FLocalEditors->Count < PrevCount);
  4425. }
  4426. if (CanClose)
  4427. {
  4428. CanClose = FEditorManager->Empty(true);
  4429. if (!CanClose)
  4430. {
  4431. SetFocus();
  4432. CanClose =
  4433. (MessageDialog(
  4434. LoadStr(PENDING_EDITORS), qtWarning, qaIgnore | qaCancel, HELP_NONE) == qaIgnore);
  4435. }
  4436. }
  4437. }
  4438. }
  4439. }
  4440. //---------------------------------------------------------------------------
  4441. void __fastcall TCustomScpExplorerForm::CloseInternalEditor(TObject * Sender)
  4442. {
  4443. TForm * Form = dynamic_cast<TForm *>(Sender);
  4444. DebugAssert(Form != NULL);
  4445. Form->Close();
  4446. }
  4447. //---------------------------------------------------------------------------
  4448. void __fastcall TCustomScpExplorerForm::ForceCloseInternalEditor(TObject * Sender)
  4449. {
  4450. TForm * Form = dynamic_cast<TForm *>(Sender);
  4451. delete Form;
  4452. }
  4453. //---------------------------------------------------------------------------
  4454. void __fastcall TCustomScpExplorerForm::ForceCloseLocalEditors()
  4455. {
  4456. while (FLocalEditors->Count > 0)
  4457. {
  4458. delete static_cast<TForm *>(FLocalEditors->Items[0]);
  4459. }
  4460. }
  4461. //---------------------------------------------------------------------------
  4462. void __fastcall TCustomScpExplorerForm::RemoteDirViewDisplayProperties(
  4463. TObject *Sender)
  4464. {
  4465. TStrings *FileList = ((TUnixDirView*)Sender)->CreateFileList(True, False, NULL);
  4466. try
  4467. {
  4468. SetProperties(osRemote, FileList);
  4469. }
  4470. __finally
  4471. {
  4472. delete FileList;
  4473. }
  4474. }
  4475. //---------------------------------------------------------------------------
  4476. void __fastcall TCustomScpExplorerForm::ComponentShowing(Byte Component, bool value)
  4477. {
  4478. if (value)
  4479. {
  4480. if (Component == fcCustomCommandsBand)
  4481. {
  4482. UpdateCustomCommandsToolbar();
  4483. }
  4484. }
  4485. }
  4486. //---------------------------------------------------------------------------
  4487. void __fastcall TCustomScpExplorerForm::SetComponentVisible(Byte Component, Boolean value)
  4488. {
  4489. TControl * Control = GetComponent(Component);
  4490. DebugAssert(Control);
  4491. bool Changed = (Control->Visible != value);
  4492. if (Changed)
  4493. {
  4494. ComponentShowing(Component, value);
  4495. TWinControl * WinControl = dynamic_cast<TWinControl*>(Control);
  4496. bool WasFocused = (WinControl != NULL) && (ActiveControl != NULL) &&
  4497. ((ActiveControl == WinControl) || (ActiveControl->Parent == WinControl));
  4498. if (value)
  4499. {
  4500. int RemainingHeight = Control->Parent->ClientHeight;
  4501. int RemainingWidth = Control->Parent->ClientWidth;
  4502. for (int i = 0; i < Control->Parent->ControlCount; i++)
  4503. {
  4504. TControl * ChildControl = Control->Parent->Controls[i];
  4505. if (ChildControl->Visible)
  4506. {
  4507. switch (ChildControl->Align)
  4508. {
  4509. case alTop:
  4510. case alBottom:
  4511. RemainingHeight -= ChildControl->Height;
  4512. break;
  4513. case alLeft:
  4514. case alRight:
  4515. RemainingWidth -= ChildControl->Width;
  4516. break;
  4517. }
  4518. }
  4519. }
  4520. int Reserve = ScaleByTextHeight(this, 32);
  4521. // queue in explorer, trees in commander
  4522. if (Control->Height > RemainingHeight - Reserve)
  4523. {
  4524. Control->Height = RemainingHeight / 2;
  4525. }
  4526. if (Control->Width > RemainingWidth - Reserve)
  4527. {
  4528. Control->Width = RemainingWidth / 2;
  4529. }
  4530. }
  4531. Control->Visible = value;
  4532. if (WasFocused && Visible)
  4533. {
  4534. DirView(osCurrent)->SetFocus();
  4535. }
  4536. FixControlsPlacement();
  4537. }
  4538. }
  4539. //---------------------------------------------------------------------------
  4540. bool __fastcall TCustomScpExplorerForm::GetComponentVisible(Byte Component)
  4541. {
  4542. TControl * Control = GetComponent(Component);
  4543. if (Control == NULL)
  4544. {
  4545. return false;
  4546. }
  4547. else
  4548. {
  4549. return Control->Visible;
  4550. }
  4551. }
  4552. //---------------------------------------------------------------------------
  4553. bool __fastcall TCustomScpExplorerForm::IsComponentPossible(Byte Component)
  4554. {
  4555. bool Result;
  4556. if ((Component == fcExplorerUpdatesBand) || (Component == fcCommanderUpdatesBand))
  4557. {
  4558. Result = !IsUWP();
  4559. }
  4560. else
  4561. {
  4562. Result = true;
  4563. }
  4564. return Result;
  4565. }
  4566. //---------------------------------------------------------------------------
  4567. void __fastcall TCustomScpExplorerForm::FixControlsPlacement()
  4568. {
  4569. if (RemoteDirView->ItemFocused != NULL)
  4570. {
  4571. RemoteDirView->ItemFocused->MakeVisible(false);
  4572. }
  4573. QueueSplitter->Visible = QueuePanel->Visible;
  4574. RemotePanelSplitter->Visible = RemoteDriveView->Visible;
  4575. }
  4576. //---------------------------------------------------------------------------
  4577. TControl * __fastcall TCustomScpExplorerForm::GetComponent(Byte Component)
  4578. {
  4579. switch (Component) {
  4580. case fcStatusBar: return RemoteStatusBar;
  4581. case fcRemotePopup: return reinterpret_cast<TControl *>(NonVisualDataModule->RemoteFilePopup);
  4582. case fcQueueView: return QueuePanel;
  4583. case fcQueueToolbar: return QueueDock;
  4584. case fcRemoteTree: return RemoteDriveView;
  4585. case fcSessionsTabs: return SessionsPageControl;
  4586. default: return NULL;
  4587. }
  4588. }
  4589. //---------------------------------------------------------------------------
  4590. void __fastcall TCustomScpExplorerForm::DirViewColumnRightClick(
  4591. TObject *Sender, TListColumn *Column, TPoint &Point)
  4592. {
  4593. DebugAssert(NonVisualDataModule && Column && Sender);
  4594. NonVisualDataModule->ListColumn = Column;
  4595. TPopupMenu * DirViewColumnMenu;
  4596. if (Sender == RemoteDirView)
  4597. {
  4598. DirViewColumnMenu = NonVisualDataModule->RemoteDirViewColumnPopup;
  4599. NonVisualDataModule->RemoteSortByExtColumnPopupItem->Visible =
  4600. (Column->Index == uvName);
  4601. NonVisualDataModule->RemoteFormatSizeBytesPopupItem->Visible =
  4602. (Column->Index == uvSize);
  4603. }
  4604. else
  4605. {
  4606. DirViewColumnMenu = NonVisualDataModule->LocalDirViewColumnPopup;
  4607. NonVisualDataModule->LocalSortByExtColumnPopupItem->Visible =
  4608. (Column->Index == dvName);
  4609. NonVisualDataModule->LocalFormatSizeBytesPopupItem->Visible =
  4610. (Column->Index == dvSize);
  4611. }
  4612. TCustomListView * ListView = DebugNotNull(dynamic_cast<TCustomListView *>(Sender));
  4613. TPoint ScreenPoint = ListView->ClientToScreen(Point);
  4614. ScreenPoint.x -= GetScrollPos(ListView->Handle, SB_HORZ);
  4615. DirViewColumnMenu->Popup(ScreenPoint.x, ScreenPoint.y);
  4616. }
  4617. //---------------------------------------------------------------------------
  4618. void __fastcall TCustomScpExplorerForm::DirViewExecFile(
  4619. TObject *Sender, TListItem *Item, bool &AllowExec)
  4620. {
  4621. DoDirViewExecFile(Sender, Item, AllowExec);
  4622. }
  4623. //---------------------------------------------------------------------------
  4624. void __fastcall TCustomScpExplorerForm::DoDirViewExecFile(TObject * Sender,
  4625. TListItem * Item, bool & AllowExec)
  4626. {
  4627. DebugAssert(Sender && Item && Configuration);
  4628. DebugAssert(AllowExec);
  4629. TCustomDirView * ADirView = (TCustomDirView *)Sender;
  4630. bool Remote = (ADirView == DirView(osRemote));
  4631. bool ResolvedSymlinks = !Remote || Terminal->ResolvingSymlinks;
  4632. TOperationSide Side = (Remote ? osRemote : osLocal);
  4633. // Anything special is done on double click only (not on "open" indicated by FForceExecution),
  4634. // on files only (not directories)
  4635. // and only when symlinks are resolved (apply to remote panel only)
  4636. if (!ADirView->ItemIsDirectory(Item) &&
  4637. (ResolvedSymlinks || FForceExecution || Terminal->IsEncryptingFiles()))
  4638. {
  4639. if ((WinConfiguration->DoubleClickAction != dcaOpen) &&
  4640. !FForceExecution &&
  4641. (ResolvedSymlinks || Terminal->IsEncryptingFiles()))
  4642. {
  4643. if (WinConfiguration->DoubleClickAction == dcaCopy)
  4644. {
  4645. // To counter the lock in DirViewBusy (called from TCustomDirView.DoExecute)
  4646. UnlockWindow();
  4647. try
  4648. {
  4649. ExecuteFileOperation(
  4650. foCopy, Side, true, !WinConfiguration->CopyOnDoubleClickConfirmation);
  4651. }
  4652. __finally
  4653. {
  4654. LockWindow();
  4655. }
  4656. AllowExec = false;
  4657. }
  4658. else if (WinConfiguration->DoubleClickAction == dcaEdit)
  4659. {
  4660. if (!Remote || !WinConfiguration->DisableOpenEdit)
  4661. {
  4662. ExecuteFile(Side, efDefaultEditor);
  4663. AllowExec = false;
  4664. }
  4665. }
  4666. else
  4667. {
  4668. DebugFail();
  4669. }
  4670. }
  4671. // if we have not done anything special, fall back to default behavior
  4672. if (AllowExec)
  4673. {
  4674. if (Remote && !WinConfiguration->DisableOpenEdit)
  4675. {
  4676. ExecuteFile(osRemote, efShell);
  4677. AllowExec = false;
  4678. }
  4679. }
  4680. }
  4681. }
  4682. //---------------------------------------------------------------------------
  4683. bool __fastcall TCustomScpExplorerForm::GetHasDirView(TOperationSide Side)
  4684. {
  4685. return ((Side == osRemote) || (Side == osCurrent));
  4686. }
  4687. //---------------------------------------------------------------------------
  4688. void __fastcall TCustomScpExplorerForm::CompareDirectories()
  4689. {
  4690. DebugFail();
  4691. }
  4692. //---------------------------------------------------------------------------
  4693. void __fastcall TCustomScpExplorerForm::SynchronizeDirectories()
  4694. {
  4695. DebugFail();
  4696. }
  4697. //---------------------------------------------------------------------------
  4698. bool __fastcall TCustomScpExplorerForm::DoSynchronizeDirectories(
  4699. UnicodeString & LocalDirectory, UnicodeString & RemoteDirectory, bool UseDefaults)
  4700. {
  4701. TSynchronizeParamType Params;
  4702. Params.LocalDirectory = LocalDirectory;
  4703. Params.RemoteDirectory = RemoteDirectory;
  4704. int UnusedParams =
  4705. (GUIConfiguration->SynchronizeParams &
  4706. (TTerminal::spPreviewChanges | TTerminal::spTimestamp | TTerminal::spNotByTime | TTerminal::spBySize));
  4707. Params.Params = GUIConfiguration->SynchronizeParams & ~UnusedParams;
  4708. Params.Options = GUIConfiguration->SynchronizeOptions;
  4709. bool SaveSettings = false;
  4710. TSynchronizeController Controller(&DoSynchronize, &DoSynchronizeInvalid,
  4711. &DoSynchronizeTooManyDirectories);
  4712. DebugAssert(FSynchronizeController == NULL);
  4713. FSynchronizeController = &Controller;
  4714. bool Result;
  4715. try
  4716. {
  4717. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  4718. int CopyParamAttrs = Terminal->UsableCopyParamAttrs(0).Upload;
  4719. int Options =
  4720. FLAGMASK(SynchronizeAllowSelectedOnly(), soAllowSelectedOnly);
  4721. DebugAssert(FOnFeedSynchronizeError == NULL);
  4722. Result = DoSynchronizeDialog(Params, &CopyParam, Controller.StartStop,
  4723. SaveSettings, Options, CopyParamAttrs, GetSynchronizeOptions, SynchronizeSessionLog,
  4724. FOnFeedSynchronizeError, SynchronizeInNewWindow, UseDefaults);
  4725. if (Result)
  4726. {
  4727. if (SaveSettings)
  4728. {
  4729. GUIConfiguration->SynchronizeParams = Params.Params | UnusedParams;
  4730. GUIConfiguration->SynchronizeOptions = Params.Options;
  4731. }
  4732. else
  4733. {
  4734. if (FLAGSET(GUIConfiguration->SynchronizeOptions, soSynchronizeAsk) &&
  4735. FLAGCLEAR(Params.Options, soSynchronizeAsk) &&
  4736. FLAGSET(Params.Options, soSynchronize))
  4737. {
  4738. GUIConfiguration->SynchronizeOptions =
  4739. (GUIConfiguration->SynchronizeOptions & ~soSynchronizeAsk) |
  4740. soSynchronize;
  4741. }
  4742. }
  4743. LocalDirectory = Params.LocalDirectory;
  4744. RemoteDirectory = Params.RemoteDirectory;
  4745. }
  4746. }
  4747. __finally
  4748. {
  4749. FSynchronizeController = NULL;
  4750. DebugAssert(FOnFeedSynchronizeError == NULL);
  4751. FOnFeedSynchronizeError = NULL;
  4752. }
  4753. return Result;
  4754. }
  4755. //---------------------------------------------------------------------------
  4756. void __fastcall TCustomScpExplorerForm::DoSynchronize(
  4757. TSynchronizeController * /*Sender*/, const UnicodeString LocalDirectory,
  4758. const UnicodeString RemoteDirectory, const TCopyParamType & CopyParam,
  4759. const TSynchronizeParamType & Params, TSynchronizeChecklist ** Checklist,
  4760. TSynchronizeOptions * Options, bool Full)
  4761. {
  4762. if (Terminal->Status == ssOpened)
  4763. {
  4764. try
  4765. {
  4766. int PParams = Params.Params;
  4767. if (!Full)
  4768. {
  4769. PParams |= TTerminal::spNoRecurse | TTerminal::spUseCache | TTerminal::spDelayProgress;
  4770. if (FLAGSET(Params.Options, soRecurse))
  4771. {
  4772. PParams |= TTerminal::spSubDirs;
  4773. }
  4774. }
  4775. else
  4776. {
  4777. // if keepuptodate is non-recursive,
  4778. // full sync before has to be non-recursive as well
  4779. if (FLAGCLEAR(Params.Options, soRecurse))
  4780. {
  4781. PParams |= TTerminal::spNoRecurse;
  4782. }
  4783. }
  4784. Synchronize(LocalDirectory, RemoteDirectory, smRemote, CopyParam,
  4785. PParams, Checklist, Options);
  4786. }
  4787. catch (Exception & E)
  4788. {
  4789. if (FLAGSET(Params.Options, soContinueOnError) && Terminal->Active &&
  4790. DebugAlwaysTrue(FOnFeedSynchronizeError != NULL))
  4791. {
  4792. // noop, already logged in MoreMessageDialog
  4793. }
  4794. else
  4795. {
  4796. ShowExtendedExceptionEx(Terminal, &E);
  4797. throw;
  4798. }
  4799. }
  4800. }
  4801. }
  4802. //---------------------------------------------------------------------------
  4803. void __fastcall TCustomScpExplorerForm::DoSynchronizeInvalid(
  4804. TSynchronizeController * /*Sender*/, const UnicodeString Directory,
  4805. const UnicodeString ErrorStr)
  4806. {
  4807. if (!Directory.IsEmpty())
  4808. {
  4809. SimpleErrorDialog(FMTLOAD(WATCH_ERROR_DIRECTORY, (Directory)), ErrorStr);
  4810. }
  4811. else
  4812. {
  4813. SimpleErrorDialog(LoadStr(WATCH_ERROR_GENERAL), ErrorStr);
  4814. }
  4815. }
  4816. //---------------------------------------------------------------------------
  4817. void __fastcall TCustomScpExplorerForm::DoSynchronizeTooManyDirectories(
  4818. TSynchronizeController * /*Sender*/, int & MaxDirectories)
  4819. {
  4820. if (MaxDirectories < GUIConfiguration->MaxWatchDirectories)
  4821. {
  4822. MaxDirectories = GUIConfiguration->MaxWatchDirectories;
  4823. }
  4824. else
  4825. {
  4826. TMessageParams Params(mpNeverAskAgainCheck);
  4827. unsigned int Result = MessageDialog(
  4828. FMTLOAD(TOO_MANY_WATCH_DIRECTORIES, (MaxDirectories, MaxDirectories)),
  4829. qtConfirmation, qaYes | qaNo, HELP_TOO_MANY_WATCH_DIRECTORIES, &Params);
  4830. if ((Result == qaYes) || (Result == qaNeverAskAgain))
  4831. {
  4832. MaxDirectories *= 2;
  4833. if (Result == qaNeverAskAgain)
  4834. {
  4835. GUIConfiguration->MaxWatchDirectories = MaxDirectories;
  4836. }
  4837. }
  4838. else
  4839. {
  4840. Abort();
  4841. }
  4842. }
  4843. }
  4844. //---------------------------------------------------------------------------
  4845. void __fastcall TCustomScpExplorerForm::Synchronize(const UnicodeString LocalDirectory,
  4846. const UnicodeString RemoteDirectory, TSynchronizeMode Mode,
  4847. const TCopyParamType & CopyParam, int Params, TSynchronizeChecklist ** Checklist,
  4848. TSynchronizeOptions * Options)
  4849. {
  4850. DebugAssert(!FAutoOperation);
  4851. void * BatchStorage;
  4852. BatchStart(BatchStorage);
  4853. FAutoOperation = true;
  4854. bool AnyOperation = false;
  4855. TDateTime StartTime = Now();
  4856. TSynchronizeChecklist * AChecklist = NULL;
  4857. try
  4858. {
  4859. FSynchronizeProgressForm = new TSynchronizeProgressForm(Application, true, true);
  4860. if (FLAGCLEAR(Params, TTerminal::spDelayProgress))
  4861. {
  4862. FSynchronizeProgressForm->Start();
  4863. }
  4864. AChecklist = Terminal->SynchronizeCollect(LocalDirectory, RemoteDirectory,
  4865. static_cast<TTerminal::TSynchronizeMode>(Mode),
  4866. &CopyParam, Params | TTerminal::spNoConfirmation, TerminalSynchronizeDirectory,
  4867. Options);
  4868. SAFE_DESTROY(FSynchronizeProgressForm);
  4869. FSynchronizeProgressForm = new TSynchronizeProgressForm(Application, true, false);
  4870. if (FLAGCLEAR(Params, TTerminal::spDelayProgress))
  4871. {
  4872. FSynchronizeProgressForm->Start();
  4873. }
  4874. for (int Index = 0; !AnyOperation && (Index < AChecklist->Count); Index++)
  4875. {
  4876. AnyOperation = AChecklist->Item[Index]->Checked;
  4877. }
  4878. // No need to call if !AnyOperation
  4879. Terminal->SynchronizeApply(AChecklist, LocalDirectory, RemoteDirectory,
  4880. &CopyParam, Params | TTerminal::spNoConfirmation, TerminalSynchronizeDirectory, NULL);
  4881. }
  4882. __finally
  4883. {
  4884. if (Checklist == NULL)
  4885. {
  4886. delete AChecklist;
  4887. }
  4888. else
  4889. {
  4890. *Checklist = AChecklist;
  4891. }
  4892. FAutoOperation = false;
  4893. SAFE_DESTROY(FSynchronizeProgressForm);
  4894. BatchEnd(BatchStorage);
  4895. ReloadLocalDirectory();
  4896. if (AnyOperation)
  4897. {
  4898. OperationComplete(StartTime);
  4899. }
  4900. }
  4901. }
  4902. //---------------------------------------------------------------------------
  4903. bool __fastcall TCustomScpExplorerForm::SynchronizeAllowSelectedOnly()
  4904. {
  4905. // can be called from command line
  4906. return Visible &&
  4907. ((DirView(osRemote)->SelCount > 0) ||
  4908. (HasDirView[osLocal] && (DirView(osLocal)->SelCount > 0)));
  4909. }
  4910. //---------------------------------------------------------------------------
  4911. void __fastcall TCustomScpExplorerForm::SynchronizeSessionLog(const UnicodeString & Message)
  4912. {
  4913. LogSynchronizeEvent(Terminal, Message);
  4914. }
  4915. //---------------------------------------------------------------------------
  4916. void __fastcall TCustomScpExplorerForm::GetSynchronizeOptions(
  4917. int Params, TSynchronizeOptions & Options)
  4918. {
  4919. if (FLAGSET(Params, TTerminal::spSelectedOnly) && SynchronizeAllowSelectedOnly())
  4920. {
  4921. Options.Filter = new TStringList();
  4922. Options.Filter->CaseSensitive = false;
  4923. Options.Filter->Duplicates = Types::dupAccept;
  4924. if (DirView(osRemote)->SelCount > 0)
  4925. {
  4926. DirView(osRemote)->CreateFileList(false, false, Options.Filter);
  4927. }
  4928. if (HasDirView[osLocal] && (DirView(osLocal)->SelCount > 0))
  4929. {
  4930. DirView(osLocal)->CreateFileList(false, false, Options.Filter);
  4931. }
  4932. Options.Filter->Sort();
  4933. }
  4934. }
  4935. //---------------------------------------------------------------------------
  4936. void __fastcall TCustomScpExplorerForm::SynchronizeInNewWindow(
  4937. const TSynchronizeParamType & Params, const TCopyParamType * CopyParams)
  4938. {
  4939. UnicodeString SessionName = CreateHiddenDuplicateSession();
  4940. UnicodeString AdditionalParams =
  4941. FORMAT(L"%s \"%s\" \"%s\" %d %d %s", (
  4942. TProgramParams::FormatSwitch(KEEP_UP_TO_DATE_SWITCH),
  4943. Params.LocalDirectory, Params.RemoteDirectory, Params.Params, Params.Options,
  4944. TProgramParams::FormatSwitch(DEFAULTS_SWITCH)));
  4945. TCopyParamType Defaults;
  4946. std::unique_ptr<THierarchicalStorage> ConfigStorage(Configuration->CreateConfigStorage());
  4947. ConfigStorage->AccessMode = smRead;
  4948. if (ConfigStorage->OpenSubKey(Configuration->ConfigurationSubKey, false, false))
  4949. {
  4950. GUIConfiguration->LoadCopyParam(ConfigStorage.get(), &Defaults);
  4951. }
  4952. ConfigStorage.reset(NULL);
  4953. std::unique_ptr<TStringList> Options(new TStringList());
  4954. std::unique_ptr<TOptionsStorage> OptionsStorage(new TOptionsStorage(Options.get(), true));
  4955. GUIConfiguration->SaveCopyParam(OptionsStorage.get(), CopyParams, &Defaults);
  4956. if (Options->Count > 0)
  4957. {
  4958. AdditionalParams +=
  4959. FORMAT(L" %s%s", (TProgramParams::FormatSwitch(RAW_CONFIG_SWITCH), StringsToParams(Options.get())));
  4960. }
  4961. ExecuteNewInstance(SessionName, AdditionalParams);
  4962. }
  4963. //---------------------------------------------------------------------------
  4964. struct TSynchronizeParams
  4965. {
  4966. UnicodeString LocalDirectory;
  4967. UnicodeString RemoteDirectory;
  4968. TSynchronizeMode Mode;
  4969. TSynchronizeChecklist * Checklist;
  4970. int Params;
  4971. TCopyParamType * CopyParam;
  4972. TDateTime * StartTime;
  4973. };
  4974. //---------------------------------------------------------------------------
  4975. void __fastcall TCustomScpExplorerForm::FullSynchronize(TSynchronizeParams & Params, TProcessedItem OnProcessedItem)
  4976. {
  4977. DebugAssert(!FAutoOperation);
  4978. void * BatchStorage;
  4979. BatchStart(BatchStorage);
  4980. FAutoOperation = true;
  4981. try
  4982. {
  4983. FSynchronizeProgressForm = new TSynchronizeProgressForm(Application, true, false);
  4984. FSynchronizeProgressForm->Start();
  4985. Terminal->SynchronizeApply(
  4986. Params.Checklist, Params.LocalDirectory, Params.RemoteDirectory,
  4987. Params.CopyParam, Params.Params | TTerminal::spNoConfirmation, TerminalSynchronizeDirectory, OnProcessedItem);
  4988. }
  4989. __finally
  4990. {
  4991. FAutoOperation = false;
  4992. SAFE_DESTROY(FSynchronizeProgressForm);
  4993. BatchEnd(BatchStorage);
  4994. ReloadLocalDirectory();
  4995. }
  4996. }
  4997. //---------------------------------------------------------------------------
  4998. void __fastcall TCustomScpExplorerForm::DoFullSynchronize(void * Token, TProcessedItem OnProcessedItem)
  4999. {
  5000. TSynchronizeParams & Params = *static_cast<TSynchronizeParams *>(Token);
  5001. *Params.StartTime = Now();
  5002. FullSynchronize(Params, OnProcessedItem);
  5003. }
  5004. //---------------------------------------------------------------------------
  5005. bool __fastcall TCustomScpExplorerForm::DoFullSynchronizeDirectories(
  5006. UnicodeString & LocalDirectory, UnicodeString & RemoteDirectory,
  5007. TSynchronizeMode & Mode, bool & SaveMode, bool UseDefaults)
  5008. {
  5009. bool Result;
  5010. int Params = GUIConfiguration->SynchronizeParams;
  5011. bool SaveSettings = false;
  5012. int Options =
  5013. FLAGMASK(!Terminal->IsCapable[fcTimestampChanging], fsoDisableTimestamp) |
  5014. FLAGMASK(SynchronizeAllowSelectedOnly(), fsoAllowSelectedOnly);
  5015. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  5016. TUsableCopyParamAttrs CopyParamAttrs = Terminal->UsableCopyParamAttrs(0);
  5017. Result = UseDefaults ||
  5018. DoFullSynchronizeDialog(Mode, Params, LocalDirectory, RemoteDirectory,
  5019. &CopyParam, SaveSettings, SaveMode, Options, CopyParamAttrs);
  5020. if (Result)
  5021. {
  5022. Configuration->Usage->Inc(L"Synchronizations");
  5023. UpdateCopyParamCounters(CopyParam);
  5024. TSynchronizeOptions SynchronizeOptions;
  5025. GetSynchronizeOptions(Params, SynchronizeOptions);
  5026. if (SaveSettings)
  5027. {
  5028. GUIConfiguration->SynchronizeParams = Params;
  5029. }
  5030. else
  5031. {
  5032. SaveMode = false;
  5033. }
  5034. TDateTime StartTime = Now();
  5035. TSynchronizeChecklist * Checklist = NULL;
  5036. try
  5037. {
  5038. DebugAssert(!FAutoOperation);
  5039. FAutoOperation = true;
  5040. try
  5041. {
  5042. FSynchronizeProgressForm = new TSynchronizeProgressForm(Application, true, true);
  5043. FSynchronizeProgressForm->Start();
  5044. Checklist = Terminal->SynchronizeCollect(LocalDirectory, RemoteDirectory,
  5045. static_cast<TTerminal::TSynchronizeMode>(Mode),
  5046. &CopyParam, Params | TTerminal::spNoConfirmation, TerminalSynchronizeDirectory,
  5047. &SynchronizeOptions);
  5048. }
  5049. __finally
  5050. {
  5051. FAutoOperation = false;
  5052. SAFE_DESTROY(FSynchronizeProgressForm);
  5053. }
  5054. if (Checklist->Count == 0)
  5055. {
  5056. UnicodeString Message = MainInstructions(LoadStr(COMPARE_NO_DIFFERENCES));
  5057. MessageDialog(Message, qtInformation, qaOK,
  5058. HELP_SYNCHRONIZE_NO_DIFFERENCES);
  5059. }
  5060. else
  5061. {
  5062. TSynchronizeParams SynchronizeParams;
  5063. SynchronizeParams.LocalDirectory = LocalDirectory;
  5064. SynchronizeParams.RemoteDirectory = RemoteDirectory;
  5065. SynchronizeParams.Mode = Mode;
  5066. SynchronizeParams.CopyParam = &CopyParam;
  5067. SynchronizeParams.Params = Params;
  5068. SynchronizeParams.Checklist = Checklist;
  5069. SynchronizeParams.StartTime = &StartTime;
  5070. if (FLAGSET(Params, TTerminal::spPreviewChanges))
  5071. {
  5072. Result =
  5073. DoSynchronizeChecklistDialog(
  5074. Checklist, Mode, Params, LocalDirectory, RemoteDirectory, CustomCommandMenu, DoFullSynchronize,
  5075. &SynchronizeParams);
  5076. }
  5077. else
  5078. {
  5079. FullSynchronize(SynchronizeParams, NULL);
  5080. }
  5081. }
  5082. }
  5083. __finally
  5084. {
  5085. delete Checklist;
  5086. }
  5087. OperationComplete(StartTime);
  5088. }
  5089. return Result;
  5090. }
  5091. //---------------------------------------------------------------------------
  5092. void __fastcall TCustomScpExplorerForm::TerminalSynchronizeDirectory(
  5093. const UnicodeString LocalDirectory, const UnicodeString RemoteDirectory,
  5094. bool & Continue, bool /*Collect*/)
  5095. {
  5096. DebugAssert(FSynchronizeProgressForm != NULL);
  5097. if (!FSynchronizeProgressForm->Started)
  5098. {
  5099. FSynchronizeProgressForm->Start();
  5100. }
  5101. FSynchronizeProgressForm->SetData(LocalDirectory, RemoteDirectory, Continue);
  5102. }
  5103. //---------------------------------------------------------------------------
  5104. void __fastcall TCustomScpExplorerForm::StandaloneEdit(const UnicodeString & FileName)
  5105. {
  5106. UnicodeString FullFileName = AbsolutePath(FTerminal->CurrentDirectory, FileName);
  5107. TRemoteFile * File = NULL;
  5108. Terminal->ReadFile(FullFileName, File);
  5109. if (File != NULL)
  5110. {
  5111. std::unique_ptr<TRemoteFile> FileOwner(File);
  5112. TAutoFlag Flag(FStandaloneEditing);
  5113. ExecuteRemoteFile(FullFileName, File, efInternalEditor);
  5114. Application->ShowMainForm = false;
  5115. Application->Run();
  5116. }
  5117. }
  5118. //---------------------------------------------------------------------------
  5119. void __fastcall TCustomScpExplorerForm::ExploreLocalDirectory()
  5120. {
  5121. DebugFail();
  5122. }
  5123. //---------------------------------------------------------------------------
  5124. TSessionData * __fastcall TCustomScpExplorerForm::CloneCurrentSessionData()
  5125. {
  5126. std::unique_ptr<TSessionData> SessionData(new TSessionData(L""));
  5127. SessionData->Assign(Terminal->SessionData);
  5128. UpdateSessionData(SessionData.get());
  5129. TTerminalManager::Instance()->UpdateSessionCredentials(SessionData.get());
  5130. if (Terminal->SessionData->IsWorkspace)
  5131. {
  5132. // Have to reset the "Workspace/XXX" name which would become user-visible
  5133. // once IsWorkspace is cleared
  5134. SessionData->Name = UnicodeString();
  5135. SessionData->IsWorkspace = false;
  5136. }
  5137. return SessionData.release();
  5138. }
  5139. //---------------------------------------------------------------------------
  5140. void __fastcall TCustomScpExplorerForm::SaveCurrentSession()
  5141. {
  5142. TSessionData * SessionData = CloneCurrentSessionData();
  5143. try
  5144. {
  5145. TSessionData * EditingSessionData = StoredSessions->FindSame(SessionData);
  5146. DoSaveSession(SessionData, EditingSessionData, true, NULL);
  5147. }
  5148. __finally
  5149. {
  5150. delete SessionData;
  5151. }
  5152. }
  5153. //---------------------------------------------------------------------------
  5154. TObjectList * __fastcall TCustomScpExplorerForm::DoCollectWorkspace()
  5155. {
  5156. TTerminalManager * Manager = TTerminalManager::Instance();
  5157. std::unique_ptr<TObjectList> DataList(new TObjectList());
  5158. if (DebugAlwaysTrue(Terminal != NULL))
  5159. {
  5160. // Update (Managed)Terminal->StateData
  5161. UpdateTerminal(Terminal);
  5162. }
  5163. Manager->SaveWorkspace(DataList.get());
  5164. return DataList.release();
  5165. }
  5166. //---------------------------------------------------------------------------
  5167. void __fastcall TCustomScpExplorerForm::DoSaveWorkspace(
  5168. const UnicodeString & Name, TObjectList * DataList, bool SavePasswords)
  5169. {
  5170. WinConfiguration->LastWorkspace = Name;
  5171. if (!SavePasswords)
  5172. {
  5173. for (int Index = 0; Index < DataList->Count; Index++)
  5174. {
  5175. TSessionData * SessionData = dynamic_cast<TSessionData *>(DataList->Items[Index]);
  5176. if (SessionData->Link.IsEmpty())
  5177. {
  5178. SessionData->ClearSessionPasswords();
  5179. }
  5180. }
  5181. }
  5182. StoredSessions->NewWorkspace(Name, DataList);
  5183. // modified only, explicit
  5184. StoredSessions->Save(false, true);
  5185. }
  5186. //---------------------------------------------------------------------------
  5187. UnicodeString __fastcall TCustomScpExplorerForm::WorkspaceName()
  5188. {
  5189. return
  5190. DefaultStr(WinConfiguration->LastWorkspace,
  5191. DefaultStr(WinConfiguration->AutoWorkspace, LoadStr(NEW_WORKSPACE)));
  5192. }
  5193. //---------------------------------------------------------------------------
  5194. bool __fastcall TCustomScpExplorerForm::SaveWorkspace(bool EnableAutoSave)
  5195. {
  5196. std::unique_ptr<TObjectList> DataList(DoCollectWorkspace());
  5197. bool AnyNonStoredSessionWithPassword = false;
  5198. bool AnyNonStoredNonWorkspaceSessionWithPassword = false;
  5199. bool AllNonStoredSessionsAnonymous = true;
  5200. TTerminalManager * Manager = TTerminalManager::Instance();
  5201. for (int Index = 0; Index < DataList->Count; Index++)
  5202. {
  5203. TSessionData * SessionData = dynamic_cast<TSessionData *>(DataList->Items[Index]);
  5204. if (SessionData->Link.IsEmpty())
  5205. {
  5206. if (SessionData->HasAnySessionPassword())
  5207. {
  5208. AnyNonStoredSessionWithPassword = true;
  5209. if (!Manager->Terminals[Index]->SessionData->IsWorkspace)
  5210. {
  5211. AnyNonStoredNonWorkspaceSessionWithPassword = true;
  5212. }
  5213. }
  5214. if (!SameText(SessionData->UserName, AnonymousUserName))
  5215. {
  5216. AllNonStoredSessionsAnonymous = false;
  5217. }
  5218. }
  5219. }
  5220. bool SavePasswords;
  5221. bool * PSavePasswords;
  5222. bool NotRecommendedSavingPasswords =
  5223. !CustomWinConfiguration->UseMasterPassword &&
  5224. !AllNonStoredSessionsAnonymous;
  5225. if (Configuration->DisablePasswordStoring ||
  5226. !AnyNonStoredSessionWithPassword)
  5227. {
  5228. PSavePasswords = NULL;
  5229. SavePasswords = false;
  5230. }
  5231. else
  5232. {
  5233. PSavePasswords = &SavePasswords;
  5234. SavePasswords =
  5235. !AnyNonStoredNonWorkspaceSessionWithPassword ||
  5236. !NotRecommendedSavingPasswords;
  5237. }
  5238. UnicodeString Name = WorkspaceName();
  5239. bool CreateShortcut = false;
  5240. bool Result =
  5241. DoSaveWorkspaceDialog(
  5242. Name, PSavePasswords, NotRecommendedSavingPasswords, CreateShortcut,
  5243. EnableAutoSave);
  5244. if (Result)
  5245. {
  5246. DoSaveWorkspace(Name, DataList.get(), SavePasswords);
  5247. if (CreateShortcut)
  5248. {
  5249. TOperationVisualizer Visualizer;
  5250. UnicodeString AdditionalParams = TProgramParams::FormatSwitch(DESKTOP_SWITCH);
  5251. CreateDesktopSessionShortCut(Name, L"", AdditionalParams, -1, WORKSPACE_ICON);
  5252. }
  5253. if (EnableAutoSave)
  5254. {
  5255. WinConfiguration->AutoSaveWorkspace = true;
  5256. WinConfiguration->AutoWorkspace = Name;
  5257. if (PSavePasswords != NULL)
  5258. {
  5259. WinConfiguration->AutoSaveWorkspacePasswords = SavePasswords;
  5260. }
  5261. }
  5262. }
  5263. return Result;
  5264. }
  5265. //---------------------------------------------------------------------------
  5266. void __fastcall TCustomScpExplorerForm::UpdateTerminal(TTerminal * Terminal)
  5267. {
  5268. TManagedTerminal * ManagedTerminal = dynamic_cast<TManagedTerminal *>(Terminal);
  5269. DebugAssert(ManagedTerminal != NULL);
  5270. SAFE_DESTROY(ManagedTerminal->RemoteExplorerState);
  5271. if (WinConfiguration->PreservePanelState)
  5272. {
  5273. ManagedTerminal->RemoteExplorerState = RemoteDirView->SaveState();
  5274. }
  5275. UpdateSessionData(ManagedTerminal->StateData);
  5276. }
  5277. //---------------------------------------------------------------------------
  5278. void __fastcall TCustomScpExplorerForm::UpdateSessionData(TSessionData * Data)
  5279. {
  5280. // Keep in sync with TSessionData::CopyStateData
  5281. DebugAssert(Data != NULL);
  5282. // cannot use RemoteDirView->Path, because it is empty if connection
  5283. // was already closed
  5284. // also only peek, we may not be connected at all atm
  5285. // so make sure we do not try retrieving current directory from the server
  5286. // (particularly with FTP)
  5287. UnicodeString ACurrentDirectory = Terminal->PeekCurrentDirectory();
  5288. if (!ACurrentDirectory.IsEmpty())
  5289. {
  5290. Data->RemoteDirectory = ACurrentDirectory;
  5291. }
  5292. Data->Color = SessionColor;
  5293. }
  5294. //---------------------------------------------------------------------------
  5295. void __fastcall TCustomScpExplorerForm::ToolBarResize(TObject *Sender)
  5296. {
  5297. TTBXToolbar * Toolbar = dynamic_cast<TTBXToolbar*>(Sender);
  5298. DebugAssert(Toolbar != NULL);
  5299. for (int i = 0; i < Toolbar->Items->Count; i++)
  5300. {
  5301. TTBXCustomDropDownItem * DropDownItem;
  5302. DropDownItem = dynamic_cast<TTBXCustomDropDownItem *>(Toolbar->Items->Items[i]);
  5303. if (DropDownItem != NULL)
  5304. {
  5305. ToolbarItemResize(DropDownItem,
  5306. Toolbar->Width - (Toolbar->View->BaseSize.x - DropDownItem->EditWidth) -
  5307. Toolbar->NonClientWidth);
  5308. break;
  5309. }
  5310. }
  5311. }
  5312. //---------------------------------------------------------------------------
  5313. void __fastcall TCustomScpExplorerForm::ToolbarItemResize(TTBXCustomDropDownItem * Item, int Width)
  5314. {
  5315. Item->EditWidth = Width;
  5316. }
  5317. //---------------------------------------------------------------------------
  5318. void __fastcall TCustomScpExplorerForm::ToolbarGetBaseSize(
  5319. TTBCustomToolbar * Toolbar, TPoint & ASize)
  5320. {
  5321. for (int i = 0; i < Toolbar->Items->Count; i++)
  5322. {
  5323. TTBXCustomDropDownItem * DropDownItem;
  5324. DropDownItem = dynamic_cast<TTBXCustomDropDownItem *>(Toolbar->Items->Items[i]);
  5325. if (DropDownItem != NULL)
  5326. {
  5327. ASize.x -= DropDownItem->EditWidth;
  5328. ASize.x += ScaleByTextHeight(this, 50) /* minimal combo width */;
  5329. }
  5330. }
  5331. }
  5332. //---------------------------------------------------------------------------
  5333. void __fastcall TCustomScpExplorerForm::DoWarnLackOfTempSpace(
  5334. const UnicodeString Path, __int64 RequiredSpace, bool & Continue)
  5335. {
  5336. if (WinConfiguration->DDWarnLackOfTempSpace)
  5337. {
  5338. UnicodeString ADrive = ExtractFileDrive(ExpandFileName(Path));
  5339. if (!ADrive.IsEmpty())
  5340. {
  5341. __int64 FreeSpace = DiskFree((Byte)(ADrive[1]-'A'+1));
  5342. DebugAssert(RequiredSpace >= 0);
  5343. __int64 RequiredWithReserve;
  5344. RequiredWithReserve = (__int64)(RequiredSpace * WinConfiguration->DDWarnLackOfTempSpaceRatio);
  5345. if (FreeSpace < RequiredWithReserve)
  5346. {
  5347. unsigned int Result;
  5348. TMessageParams Params(mpNeverAskAgainCheck);
  5349. Result = MessageDialog(FMTLOAD(DD_WARN_LACK_OF_TEMP_SPACE, (Path,
  5350. FormatBytes(FreeSpace), FormatBytes(RequiredSpace))),
  5351. qtWarning, qaYes | qaNo, HELP_DD_WARN_LACK_OF_TEMP_SPACE, &Params);
  5352. if (Result == qaNeverAskAgain)
  5353. {
  5354. WinConfiguration->DDWarnLackOfTempSpace = false;
  5355. }
  5356. Continue = (Result == qaYes || Result == qaNeverAskAgain);
  5357. }
  5358. }
  5359. }
  5360. }
  5361. //---------------------------------------------------------------------------
  5362. void __fastcall TCustomScpExplorerForm::AddBookmark(TOperationSide Side)
  5363. {
  5364. DoOpenDirectoryDialog(odAddBookmark, Side);
  5365. }
  5366. //---------------------------------------------------------------------------
  5367. TStrings * __fastcall TCustomScpExplorerForm::CreateVisitedDirectories(TOperationSide Side)
  5368. {
  5369. // we should better use TCustomDirView::FCaseSensitive, but it is private
  5370. TStringList * VisitedDirectories = CreateSortedStringList((Side == osRemote));
  5371. try
  5372. {
  5373. TCustomDirView * DView = DirView(Side);
  5374. for (int Index = -DView->BackCount; Index <= DView->ForwardCount; Index++)
  5375. {
  5376. VisitedDirectories->Add(DView->HistoryPath[Index]);
  5377. }
  5378. }
  5379. catch (...)
  5380. {
  5381. delete VisitedDirectories;
  5382. throw;
  5383. }
  5384. return VisitedDirectories;
  5385. }
  5386. //---------------------------------------------------------------------------
  5387. void __fastcall TCustomScpExplorerForm::DoOpenDirectoryDialog(
  5388. TOpenDirectoryMode Mode, TOperationSide Side)
  5389. {
  5390. bool Continue = true;
  5391. if (Mode == odAddBookmark)
  5392. {
  5393. TMessageParams Params(mpNeverAskAgainCheck);
  5394. Params.NeverAskAgainTitle = LoadStr(ADD_BOOKMARK_SHARED);
  5395. Params.NeverAskAgainCheckedInitially = WinConfiguration->UseSharedBookmarks;
  5396. UnicodeString Message = MainInstructions(FMTLOAD(ADD_BOOKMARK_CONFIRM, (DirView(Side)->PathName)));
  5397. unsigned int Answer =
  5398. MessageDialog(Message,
  5399. qtConfirmation, qaOK | qaCancel, HELP_ADD_BOOKMARK_CONFIRM, &Params);
  5400. if (Answer == qaNeverAskAgain)
  5401. {
  5402. Continue = true;
  5403. WinConfiguration->UseSharedBookmarks = true;
  5404. }
  5405. else if (Answer == qaOK)
  5406. {
  5407. Continue = true;
  5408. WinConfiguration->UseSharedBookmarks = false;
  5409. }
  5410. else
  5411. {
  5412. Continue = false;
  5413. }
  5414. }
  5415. if (Continue)
  5416. {
  5417. TStrings * VisitedDirectories = CreateVisitedDirectories(Side);
  5418. try
  5419. {
  5420. UnicodeString Name = DirView(Side)->PathName;
  5421. if (::DoOpenDirectoryDialog(Mode, Side, Name, VisitedDirectories, Terminal,
  5422. // do not allow switching to location profiles,
  5423. // if we are not connected
  5424. HasDirView[osLocal] && (Terminal != NULL)))
  5425. {
  5426. TWindowLock Lock(this);
  5427. DirView(Side)->Path = Name;
  5428. }
  5429. }
  5430. __finally
  5431. {
  5432. delete VisitedDirectories;
  5433. }
  5434. }
  5435. }
  5436. //---------------------------------------------------------------------------
  5437. bool __fastcall TCustomScpExplorerForm::CommandSessionFallback()
  5438. {
  5439. DebugAssert(!FTerminal->CommandSessionOpened);
  5440. return TTerminalManager::Instance()->ConnectTerminal(FTerminal->CommandSession);
  5441. }
  5442. //---------------------------------------------------------------------------
  5443. bool __fastcall TCustomScpExplorerForm::EnsureCommandSessionFallback(TFSCapability Capability)
  5444. {
  5445. bool Result = FTerminal->IsCapable[Capability] ||
  5446. FTerminal->CommandSessionOpened;
  5447. if (!Result)
  5448. {
  5449. DebugAssert(FTerminal->IsCapable[fcSecondaryShell]);
  5450. if (!GUIConfiguration->ConfirmCommandSession)
  5451. {
  5452. Result = true;
  5453. }
  5454. else
  5455. {
  5456. TMessageParams Params(mpNeverAskAgainCheck);
  5457. const TFileSystemInfo & FileSystemInfo = Terminal->GetFileSystemInfo();
  5458. unsigned int Answer = MessageDialog(FMTLOAD(PERFORM_ON_COMMAND_SESSION2,
  5459. (FileSystemInfo.ProtocolName, FileSystemInfo.ProtocolName)), qtConfirmation,
  5460. qaOK | qaCancel, HELP_PERFORM_ON_COMMAND_SESSION, &Params);
  5461. if (Answer == qaNeverAskAgain)
  5462. {
  5463. GUIConfiguration->ConfirmCommandSession = false;
  5464. Result = true;
  5465. }
  5466. else if (Answer == qaOK)
  5467. {
  5468. Result = true;
  5469. }
  5470. }
  5471. if (Result)
  5472. {
  5473. Result = CommandSessionFallback();
  5474. }
  5475. }
  5476. return Result;
  5477. }
  5478. //---------------------------------------------------------------------------
  5479. void __fastcall TCustomScpExplorerForm::OpenConsole(UnicodeString Command)
  5480. {
  5481. if (EnsureCommandSessionFallback(fcAnyCommand))
  5482. {
  5483. DoConsoleDialog(Terminal, Command);
  5484. }
  5485. }
  5486. //---------------------------------------------------------------------------
  5487. void __fastcall TCustomScpExplorerForm::FileControlDDDragEnter(
  5488. TObject *Sender, _di_IDataObject /*DataObj*/, int /*grfKeyState*/,
  5489. const TPoint & /*Point*/, int & /*dwEffect*/, bool & Accept)
  5490. {
  5491. if (IsFileControl(DropSourceControl, osRemote) &&
  5492. (FDDExtMapFile != NULL))
  5493. {
  5494. Accept = true;
  5495. }
  5496. FDDTargetControl = dynamic_cast<TControl*>(Sender);
  5497. DebugAssert(FDDTargetControl != NULL);
  5498. }
  5499. //---------------------------------------------------------------------------
  5500. void __fastcall TCustomScpExplorerForm::SessionsDDDragEnter(
  5501. _di_IDataObject DataObj, int KeyState,
  5502. const TPoint & Point, int & Effect, bool & Accept)
  5503. {
  5504. FileControlDDDragEnter(SessionsPageControl, DataObj, KeyState, Point, Effect, Accept);
  5505. }
  5506. //---------------------------------------------------------------------------
  5507. void __fastcall TCustomScpExplorerForm::QueueDDDragEnter(
  5508. _di_IDataObject DataObj, int KeyState,
  5509. const TPoint & Point, int & Effect, bool & Accept)
  5510. {
  5511. FileControlDDDragEnter(QueueView3, DataObj, KeyState, Point, Effect, Accept);
  5512. }
  5513. //---------------------------------------------------------------------------
  5514. void __fastcall TCustomScpExplorerForm::FileControlDDDragLeave(
  5515. TObject * Sender)
  5516. {
  5517. DebugUsedParam(Sender);
  5518. DebugAssert(FDDTargetControl == Sender);
  5519. FDDTargetControl = NULL;
  5520. }
  5521. //---------------------------------------------------------------------------
  5522. void __fastcall TCustomScpExplorerForm::SessionsDDDragLeave()
  5523. {
  5524. FileControlDDDragLeave(SessionsPageControl);
  5525. }
  5526. //---------------------------------------------------------------------------
  5527. void __fastcall TCustomScpExplorerForm::QueueDDDragLeave()
  5528. {
  5529. FileControlDDDragLeave(QueueView3);
  5530. }
  5531. //---------------------------------------------------------------------------
  5532. void __fastcall TCustomScpExplorerForm::AddEditLink(TOperationSide Side, bool Add)
  5533. {
  5534. DebugAssert(GetSide(Side) == osRemote);
  5535. DebugUsedParam(Side);
  5536. bool Edit = false;
  5537. TRemoteFile * File = NULL;
  5538. UnicodeString FileName;
  5539. UnicodeString PointTo;
  5540. bool SymbolicLink = true;
  5541. if (RemoteDirView->ItemFocused)
  5542. {
  5543. DebugAssert(RemoteDirView->ItemFocused->Data);
  5544. File = (TRemoteFile *)RemoteDirView->ItemFocused->Data;
  5545. Edit = !Add && File->IsSymLink && Terminal->SessionData->ResolveSymlinks;
  5546. if (Edit)
  5547. {
  5548. FileName = File->FileName;
  5549. PointTo = File->LinkTo;
  5550. }
  5551. else
  5552. {
  5553. PointTo = File->FileName;
  5554. }
  5555. }
  5556. if (DoSymlinkDialog(FileName, PointTo, osRemote, SymbolicLink, Edit,
  5557. Terminal->IsCapable[fcHardLink]))
  5558. {
  5559. Configuration->Usage->Inc(L"RemoteLinksCreated");
  5560. if (Edit)
  5561. {
  5562. DebugAssert(File->FileName == FileName);
  5563. int Params = dfNoRecursive;
  5564. Terminal->ExceptionOnFail = true;
  5565. try
  5566. {
  5567. Terminal->DeleteFile(L"", File, &Params);
  5568. }
  5569. __finally
  5570. {
  5571. Terminal->ExceptionOnFail = false;
  5572. }
  5573. }
  5574. Terminal->CreateLink(FileName, PointTo, SymbolicLink);
  5575. }
  5576. }
  5577. //---------------------------------------------------------------------------
  5578. bool __fastcall TCustomScpExplorerForm::CanAddEditLink(TOperationSide Side)
  5579. {
  5580. return
  5581. (Terminal != NULL) &&
  5582. ((GetSide(Side) != osRemote) ||
  5583. (Terminal->ResolvingSymlinks &&
  5584. Terminal->IsCapable[fcSymbolicLink]));
  5585. }
  5586. //---------------------------------------------------------------------------
  5587. bool __fastcall TCustomScpExplorerForm::LinkFocused()
  5588. {
  5589. return
  5590. (FCurrentSide == osRemote) &&
  5591. (RemoteDirView->ItemFocused != NULL) &&
  5592. ((TRemoteFile *)RemoteDirView->ItemFocused->Data)->IsSymLink &&
  5593. Terminal->SessionData->ResolveSymlinks;
  5594. }
  5595. //---------------------------------------------------------------------------
  5596. void __fastcall TCustomScpExplorerForm::ExecuteCurrentFile()
  5597. {
  5598. DebugAssert(!WinConfiguration->DisableOpenEdit);
  5599. FForceExecution = true;
  5600. try
  5601. {
  5602. DirView(osCurrent)->ExecuteCurrentFile();
  5603. }
  5604. __finally
  5605. {
  5606. FForceExecution = false;
  5607. }
  5608. }
  5609. //---------------------------------------------------------------------------
  5610. void __fastcall TCustomScpExplorerForm::ExecuteCurrentFileWith(bool OnFocused)
  5611. {
  5612. TEditorData ExternalEditor;
  5613. ExternalEditor.Editor = edExternal;
  5614. bool Remember = false;
  5615. if (DoEditorPreferencesDialog(&ExternalEditor, Remember, epmAdHoc,
  5616. (GetSide(osCurrent) == osRemote)))
  5617. {
  5618. if (Remember)
  5619. {
  5620. TEditorList * EditorList = new TEditorList;
  5621. try
  5622. {
  5623. *EditorList = *WinConfiguration->EditorList;
  5624. bool Found = false;
  5625. int i = 0;
  5626. while (!Found && (i < EditorList->Count))
  5627. {
  5628. const TEditorPreferences * Editor = EditorList->Editors[i];
  5629. if ((Editor->Data->Editor == edExternal) &&
  5630. (Editor->Data->ExternalEditor == ExternalEditor.ExternalEditor))
  5631. {
  5632. Found = true;
  5633. }
  5634. i++;
  5635. }
  5636. if (!Found)
  5637. {
  5638. EditorList->Add(new TEditorPreferences(ExternalEditor));
  5639. WinConfiguration->EditorList = EditorList;
  5640. }
  5641. }
  5642. __finally
  5643. {
  5644. delete EditorList;
  5645. }
  5646. }
  5647. ExecuteFile(osCurrent, efExternalEditor, &ExternalEditor, true, OnFocused);
  5648. }
  5649. }
  5650. //---------------------------------------------------------------------------
  5651. void __fastcall TCustomScpExplorerForm::TerminalRemoved(TObject * Sender)
  5652. {
  5653. FEditorManager->ProcessFiles(FileTerminalRemoved, Sender);
  5654. if (FFileFindTerminal == Sender)
  5655. {
  5656. FFileFindTerminal = NULL;
  5657. HideFileFindDialog();
  5658. }
  5659. }
  5660. //---------------------------------------------------------------------------
  5661. void __fastcall TCustomScpExplorerForm::FileTerminalRemoved(const UnicodeString FileName,
  5662. TEditedFileData * Data, TObject * /*Token*/, void * Arg)
  5663. {
  5664. TTerminal * Terminal = static_cast<TTerminal *>(Arg);
  5665. DebugAssert(Terminal != NULL);
  5666. if (Data->Terminal == Terminal)
  5667. {
  5668. Data->Terminal = NULL;
  5669. }
  5670. }
  5671. //---------------------------------------------------------------------------
  5672. void __fastcall TCustomScpExplorerForm::LastTerminalClosed(TObject * /*Sender*/)
  5673. {
  5674. UpdateControls();
  5675. SessionColor = TColor(0);
  5676. UpdateRemotePathComboBox(false);
  5677. NeedSession(true);
  5678. }
  5679. //---------------------------------------------------------------------------
  5680. void __fastcall TCustomScpExplorerForm::NeedSession(bool ReloadSessions)
  5681. {
  5682. try
  5683. {
  5684. try
  5685. {
  5686. TTerminalManager::Instance()->NewSession(false, L"", ReloadSessions, this);
  5687. }
  5688. __finally
  5689. {
  5690. if (!WinConfiguration->KeepOpenWhenNoSession &&
  5691. (!Terminal || !Terminal->Active))
  5692. {
  5693. TerminateApplication();
  5694. }
  5695. }
  5696. }
  5697. catch (EAbort &)
  5698. {
  5699. // swallow
  5700. // The TTerminalManager does not expect the OnLastTerminalClose to throw without trying to connect.
  5701. // Also when called from TWinControl.UpdateShowing => CMShowingChanged, the showing is aborted on exception
  5702. }
  5703. }
  5704. //---------------------------------------------------------------------------
  5705. void __fastcall TCustomScpExplorerForm::DoTerminalListChanged(bool Force)
  5706. {
  5707. TStrings * TerminalList = TTerminalManager::Instance()->TerminalList;
  5708. int ActiveTerminalIndex = TTerminalManager::Instance()->ActiveTerminalIndex;
  5709. Configuration->Usage->SetMax(L"MaxOpenedSessions", TerminalList->Count);
  5710. bool ListChanged = Force || (TerminalList->Count + 1 != SessionsPageControl->PageCount);
  5711. if (!ListChanged)
  5712. {
  5713. int Index = 0;
  5714. while (!ListChanged && (Index < TerminalList->Count))
  5715. {
  5716. ListChanged =
  5717. (GetSessionTabTerminal(SessionsPageControl->Pages[Index]) != TerminalList->Objects[Index]) ||
  5718. (SessionsPageControl->Pages[Index]->Caption != TerminalList->Strings[Index]);
  5719. Index++;
  5720. }
  5721. }
  5722. if (ListChanged)
  5723. {
  5724. SendMessage(SessionsPageControl->Handle, WM_SETREDRAW, 0, 0);
  5725. try
  5726. {
  5727. FSessionColors->Clear();
  5728. AddFixedSessionImages();
  5729. while (SessionsPageControl->PageCount > 0)
  5730. {
  5731. delete SessionsPageControl->Pages[0];
  5732. }
  5733. for (int Index = 0; Index < TerminalList->Count; Index++)
  5734. {
  5735. TThemeTabSheet * TabSheet = new TThemeTabSheet(SessionsPageControl);
  5736. TabSheet->Caption = TerminalList->Strings[Index];
  5737. TTerminal * Terminal = dynamic_cast<TTerminal *>(TerminalList->Objects[Index]);
  5738. TabSheet->Tag = reinterpret_cast<int>(Terminal);
  5739. TabSheet->PageControl = SessionsPageControl;
  5740. UpdateSessionTab(TabSheet);
  5741. }
  5742. TTabSheet * TabSheet = new TTabSheet(SessionsPageControl);
  5743. TabSheet->PageControl = SessionsPageControl;
  5744. TabSheet->ImageIndex = FNewSessionTabImageIndex;
  5745. UpdateNewSessionTab();
  5746. }
  5747. __finally
  5748. {
  5749. SendMessage(SessionsPageControl->Handle, WM_SETREDRAW, 1, 0);
  5750. }
  5751. }
  5752. SessionsPageControl->ActivePageIndex = ActiveTerminalIndex;
  5753. }
  5754. //---------------------------------------------------------------------------
  5755. void __fastcall TCustomScpExplorerForm::TerminalListChanged(TObject * /*Sender*/)
  5756. {
  5757. DoTerminalListChanged(false);
  5758. }
  5759. //---------------------------------------------------------------------------
  5760. void __fastcall TCustomScpExplorerForm::UpdateNewSessionTab()
  5761. {
  5762. TTabSheet * TabSheet = SessionsPageControl->Pages[SessionsPageControl->PageCount - 1];
  5763. DebugAssert(TabSheet->ImageIndex == 0);
  5764. TabSheet->Caption =
  5765. WinConfiguration->SelectiveToolbarText ?
  5766. StripHotkey(StripTrailingPunctuation(NonVisualDataModule->NewSessionAction->Caption)) :
  5767. UnicodeString();
  5768. }
  5769. //---------------------------------------------------------------------------
  5770. TTerminal * __fastcall TCustomScpExplorerForm::GetSessionTabTerminal(TTabSheet * TabSheet)
  5771. {
  5772. return reinterpret_cast<TTerminal *>(TabSheet->Tag);
  5773. }
  5774. //---------------------------------------------------------------------------
  5775. void __fastcall TCustomScpExplorerForm::UpdateSessionTab(TTabSheet * TabSheet)
  5776. {
  5777. if (DebugAlwaysTrue(TabSheet != NULL))
  5778. {
  5779. TManagedTerminal * ManagedTerminal =
  5780. dynamic_cast<TManagedTerminal *>(GetSessionTabTerminal(TabSheet));
  5781. if (DebugAlwaysTrue(ManagedTerminal != NULL))
  5782. {
  5783. TColor Color =
  5784. (ManagedTerminal == FTerminal) ? FSessionColor : ManagedTerminal->StateData->Color;
  5785. TabSheet->ImageIndex = AddSessionColor(Color);
  5786. TabSheet->Caption = TTerminalManager::Instance()->GetTerminalTitle(ManagedTerminal, true);
  5787. TThemeTabSheet * ThemeTabSheet = dynamic_cast<TThemeTabSheet *>(TabSheet);
  5788. if (DebugAlwaysTrue(ThemeTabSheet != NULL))
  5789. {
  5790. ThemeTabSheet->Shadowed = !ManagedTerminal->Active;
  5791. }
  5792. }
  5793. }
  5794. }
  5795. //---------------------------------------------------------------------------
  5796. bool __fastcall TCustomScpExplorerForm::SessionTabSwitched()
  5797. {
  5798. DebugAssert(SessionsPageControl->ActivePage != NULL);
  5799. TTerminal * Terminal = GetSessionTabTerminal(SessionsPageControl->ActivePage);
  5800. bool Result = (Terminal != NULL);
  5801. if (Result)
  5802. {
  5803. TTerminalManager::Instance()->ActiveTerminal = Terminal;
  5804. }
  5805. else
  5806. {
  5807. try
  5808. {
  5809. NewSession(false);
  5810. }
  5811. __finally
  5812. {
  5813. DoTerminalListChanged(false);
  5814. }
  5815. FSessionsPageControlNewSessionTime = Now();
  5816. }
  5817. return Result;
  5818. }
  5819. //---------------------------------------------------------------------------
  5820. void __fastcall TCustomScpExplorerForm::SessionsPageControlChange(TObject * /*Sender*/)
  5821. {
  5822. SessionTabSwitched();
  5823. }
  5824. //---------------------------------------------------------------------------
  5825. void __fastcall TCustomScpExplorerForm::TransferListChange(TObject * Sender)
  5826. {
  5827. TTBXStringList * TransferList = dynamic_cast<TTBXStringList *>(Sender);
  5828. DebugAssert(TransferList != NULL);
  5829. UnicodeString Name;
  5830. if (TransferList->ItemIndex <= 0)
  5831. {
  5832. Name = L"";
  5833. }
  5834. else
  5835. {
  5836. Name = GUIConfiguration->CopyParamList->Names[TransferList->ItemIndex - 1];
  5837. }
  5838. if (FCopyParamAutoSelected.IsEmpty())
  5839. {
  5840. // if previous preset was not autoselected, make new preset the "default"
  5841. FCopyParamDefault = Name;
  5842. }
  5843. GUIConfiguration->CopyParamCurrent = Name;
  5844. }
  5845. //---------------------------------------------------------------------------
  5846. void __fastcall TCustomScpExplorerForm::UpdateTransferLabel()
  5847. {
  5848. // sanity check
  5849. bool ExistingPreset =
  5850. (FTransferListHoverIndex >= 0) &&
  5851. (FTransferListHoverIndex < 1 + GUIConfiguration->CopyParamList->Count);
  5852. DebugAssert(ExistingPreset);
  5853. if (ExistingPreset)
  5854. {
  5855. HDC DC = GetDC(0);
  5856. TCanvas * Canvas = new TCanvas();
  5857. try
  5858. {
  5859. Canvas->Handle = DC;
  5860. Canvas->Font = GetToolbarFont(this);
  5861. UnicodeString Name;
  5862. if (FTransferListHoverIndex == 0)
  5863. {
  5864. Name = L"";
  5865. }
  5866. else
  5867. {
  5868. Name = GUIConfiguration->CopyParamList->Names[FTransferListHoverIndex - 1];
  5869. }
  5870. TTBXLabelItem * TransferLabel = dynamic_cast<TTBXLabelItem*>(
  5871. static_cast<TComponent*>(GetComponent(fcTransferLabel)));
  5872. TTBXStringList * TransferList = dynamic_cast<TTBXStringList*>(
  5873. static_cast<TObject*>(GetComponent(fcTransferList)));
  5874. UnicodeString InfoStr =
  5875. GUIConfiguration->CopyParamPreset[Name].
  5876. GetInfoStr(L"; ",
  5877. FLAGMASK(Terminal != NULL, Terminal->UsableCopyParamAttrs(0).General));
  5878. int MaxWidth = TransferList->MinWidth - (2 * TransferLabel->Margin) - ScaleByTextHeight(this, 10);
  5879. if (Canvas->TextExtent(InfoStr).cx > MaxWidth)
  5880. {
  5881. while (Canvas->TextExtent(InfoStr + Ellipsis).cx > MaxWidth)
  5882. {
  5883. InfoStr.SetLength(InfoStr.Length() - 1);
  5884. }
  5885. InfoStr += Ellipsis;
  5886. }
  5887. // UpdateCaption does not cause invalidation of whole submenu, while
  5888. // setting Caption property does.
  5889. // also it probably does not resize the label, even if necessary
  5890. // (we do not want that anyway)
  5891. TransferLabel->UpdateCaption(InfoStr);
  5892. }
  5893. __finally
  5894. {
  5895. Canvas->Handle = NULL;
  5896. ReleaseDC(0, DC);
  5897. delete Canvas;
  5898. }
  5899. }
  5900. }
  5901. //---------------------------------------------------------------------------
  5902. void __fastcall TCustomScpExplorerForm::TransferListDrawItem(
  5903. TTBXCustomList * /*Sender*/, TCanvas * /*ACanvas*/, const TRect & /*ARect*/,
  5904. int /*AIndex*/, int AHoverIndex, bool & /*DrawDefault*/)
  5905. {
  5906. if (FTransferListHoverIndex != AHoverIndex)
  5907. {
  5908. FTransferListHoverIndex = AHoverIndex;
  5909. UpdateTransferLabel();
  5910. }
  5911. }
  5912. //---------------------------------------------------------------------------
  5913. void __fastcall TCustomScpExplorerForm::WMAppCommand(TMessage & Message)
  5914. {
  5915. int Command = GET_APPCOMMAND_LPARAM(Message.LParam);
  5916. TShiftState Shift = KeyDataToShiftState(GET_KEYSTATE_LPARAM(Message.LParam));
  5917. if ((Shift * (TShiftState() << ssShift << ssAlt << ssCtrl)).Empty())
  5918. {
  5919. if (Command == APPCOMMAND_BROWSER_FAVORITES)
  5920. {
  5921. OpenDirectory(GetSide(osCurrent));
  5922. Message.Result = 1;
  5923. }
  5924. else
  5925. {
  5926. TForm::Dispatch(&Message);
  5927. }
  5928. }
  5929. else
  5930. {
  5931. TForm::Dispatch(&Message);
  5932. }
  5933. }
  5934. //---------------------------------------------------------------------------
  5935. void __fastcall TCustomScpExplorerForm::CMDialogChar(TMessage & AMessage)
  5936. {
  5937. TCMDialogChar & Message = reinterpret_cast<TCMDialogChar &>(AMessage);
  5938. if ((FIgnoreNextDialogChar != 0) &&
  5939. (toupper(Message.CharCode) == toupper(FIgnoreNextDialogChar)))
  5940. {
  5941. Message.Result = 1;
  5942. }
  5943. else
  5944. {
  5945. TForm::Dispatch(&Message);
  5946. }
  5947. FIgnoreNextDialogChar = 0;
  5948. }
  5949. //---------------------------------------------------------------------------
  5950. void __fastcall TCustomScpExplorerForm::WMSysCommand(TMessage & Message)
  5951. {
  5952. // The four low-order bits are used internally by Windows
  5953. unsigned int Cmd = (Message.WParam & 0xFFF0);
  5954. // SC_RESTORE, SC_MAXIMIZE, SC_MINIMIZE - buttons on windows title
  5955. // SC_DEFAULT - double click on windows title (does not work, at least on WinXP)
  5956. // 61730 - restore thru double click - undocumented
  5957. // 61490 - maximize thru double click - undocumented
  5958. if ((Cmd == SC_RESTORE) || (Cmd == SC_MAXIMIZE) ||
  5959. (Cmd == SC_MINIMIZE) || (Cmd == SC_DEFAULT))
  5960. {
  5961. SysResizing(Cmd);
  5962. }
  5963. TForm::Dispatch(&Message);
  5964. }
  5965. //---------------------------------------------------------------------------
  5966. void __fastcall TCustomScpExplorerForm::WMQueryEndSession(TMessage & Message)
  5967. {
  5968. // We were actually never able to make ENDSESSION_CRITICAL happen.
  5969. // Also there no point returning TRUE as we are not able to
  5970. // handle the abrupt termination caused by subsequent WM_ENDSESSION cleanly.
  5971. // Hence the process termination might be safer :)
  5972. if ((Message.LParam != ENDSESSION_CRITICAL) &&
  5973. (((FQueue != NULL) && !FQueue->IsEmpty) || (FProgressForm != NULL)))
  5974. {
  5975. Message.Result = FALSE;
  5976. }
  5977. else
  5978. {
  5979. Message.Result = TRUE;
  5980. }
  5981. // Do not call default handling as that triggers OnCloseQuery,
  5982. // where our implementation will popup confirmation dialogs, what we do not want,
  5983. // as per Vista guidelines:
  5984. // https://docs.microsoft.com/en-us/windows/desktop/shutdown/shutting-down
  5985. }
  5986. //---------------------------------------------------------------------------
  5987. void __fastcall TCustomScpExplorerForm::WMEndSession(TWMEndSession & Message)
  5988. {
  5989. if (Message.EndSession && IsApplicationMinimized())
  5990. {
  5991. // WORKAROUND
  5992. // TApplication.WndProc() calls Application.Terminate() before Halt(),
  5993. // when it receives WM_ENDSESSION,
  5994. // but that sometimes (particularly when application is minimized) cause crashes.
  5995. // Crash popups message that blocks log off.
  5996. // Obviously application cannot shutdown cleanly after WM_ENDSESSION,
  5997. // so we call ExitProcess() immediately, not even trying to cleanup.
  5998. // It still causes beep, so there's likely some popup, but it does not block
  5999. // log off.
  6000. ExitProcess(0);
  6001. }
  6002. TForm::Dispatch(&Message);
  6003. }
  6004. #ifdef _DEBUG
  6005. //---------------------------------------------------------------------------
  6006. void __fastcall TCustomScpExplorerForm::WMWindowPosChanged(TWMWindowPosMsg & Message)
  6007. {
  6008. TForm::Dispatch(&Message);
  6009. }
  6010. #endif
  6011. //---------------------------------------------------------------------------
  6012. void __fastcall TCustomScpExplorerForm::SysResizing(unsigned int /*Cmd*/)
  6013. {
  6014. }
  6015. //---------------------------------------------------------------------------
  6016. void __fastcall TCustomScpExplorerForm::DoShow()
  6017. {
  6018. // only now are the controls resized finally, so the size constraints
  6019. // will not conflict with possibly very small window size
  6020. RestoreFormParams();
  6021. FixControlsPlacement();
  6022. if (Position == poDefaultPosOnly)
  6023. {
  6024. CutFormToDesktop(this);
  6025. }
  6026. TForm::DoShow();
  6027. FSessionsDragDropFilesEx->DragDropControl = SessionsPageControl;
  6028. FQueueDragDropFilesEx->DragDropControl = QueueView3;
  6029. if (Terminal == NULL)
  6030. {
  6031. StartingDisconnected();
  6032. }
  6033. UpdatePixelsPerInchMainWindowCounter();
  6034. FShowing = true;
  6035. }
  6036. //---------------------------------------------------------------------------
  6037. void __fastcall TCustomScpExplorerForm::UpdatePixelsPerInchMainWindowCounter()
  6038. {
  6039. Configuration->Usage->Set(L"PixelsPerInchMainWindow", PixelsPerInch);
  6040. }
  6041. //---------------------------------------------------------------------------
  6042. void __fastcall TCustomScpExplorerForm::StartingDisconnected()
  6043. {
  6044. DoTerminalListChanged(true);
  6045. }
  6046. //---------------------------------------------------------------------------
  6047. void __fastcall TCustomScpExplorerForm::PopupTrayBalloon(TTerminal * Terminal,
  6048. const UnicodeString & Str, TQueryType Type, Exception * E, unsigned int Seconds,
  6049. TNotifyEvent OnBalloonClick, TObject * UserData)
  6050. {
  6051. bool Do;
  6052. UnicodeString Message;
  6053. if (E == NULL)
  6054. {
  6055. Message = Str;
  6056. Do = true;
  6057. }
  6058. else
  6059. {
  6060. Do = ExceptionMessage(E, Message);
  6061. }
  6062. if (Do && WinConfiguration->BalloonNotifications)
  6063. {
  6064. UnicodeString Title;
  6065. if ((Terminal == NULL) && (Type == qtInformation) && ExtractMainInstructions(Message, Title))
  6066. {
  6067. Message = TrimLeft(Message);
  6068. }
  6069. else
  6070. {
  6071. Message = UnformatMessage(Message);
  6072. const ResourceString * Captions[] = { &_SMsgDlgConfirm, &_SMsgDlgWarning,
  6073. &_SMsgDlgError, &_SMsgDlgInformation, NULL };
  6074. Title = LoadResourceString(Captions[Type]);
  6075. if (Terminal != NULL)
  6076. {
  6077. Title = FORMAT(L"%s - %s",
  6078. (TTerminalManager::Instance()->GetTerminalTitle(Terminal, true), Title));
  6079. }
  6080. }
  6081. if (Seconds == 0)
  6082. {
  6083. Seconds = WinConfiguration->NotificationsTimeout;
  6084. }
  6085. FTrayIcon->PopupBalloon(Title, Message, Type, Seconds * MSecsPerSec, OnBalloonClick, UserData);
  6086. }
  6087. }
  6088. //---------------------------------------------------------------------------
  6089. unsigned int __fastcall TCustomScpExplorerForm::MoreMessageDialog(const UnicodeString Message,
  6090. TStrings * MoreMessages, TQueryType Type, unsigned int Answers,
  6091. UnicodeString HelpKeyword, const TMessageParams * Params,
  6092. TTerminal * Terminal)
  6093. {
  6094. if (((WinConfiguration->ContinueOnError && (FErrorList != NULL)) ||
  6095. (FOnFeedSynchronizeError != NULL)) &&
  6096. (Params != NULL) && (Params->Params & mpAllowContinueOnError) )
  6097. {
  6098. if (FOnFeedSynchronizeError != NULL)
  6099. {
  6100. FOnFeedSynchronizeError(Message, MoreMessages, Type, HelpKeyword);
  6101. }
  6102. else
  6103. {
  6104. DebugAssert(FErrorList != NULL);
  6105. TStringList * MoreMessagesCopy = NULL;
  6106. if (MoreMessages)
  6107. {
  6108. MoreMessagesCopy = new TStringList();
  6109. MoreMessagesCopy->Assign(MoreMessages);
  6110. }
  6111. FErrorList->AddObject(Message, MoreMessagesCopy);
  6112. }
  6113. PopupTrayBalloon(Terminal, Message, Type);
  6114. return ContinueAnswer(Answers);
  6115. }
  6116. else
  6117. {
  6118. bool UseBalloon = FTrayIcon->Visible;
  6119. if (UseBalloon)
  6120. {
  6121. PopupTrayBalloon(Terminal, Message, Type);
  6122. }
  6123. unsigned int Result;
  6124. try
  6125. {
  6126. Result = ::MoreMessageDialog(Message, MoreMessages, Type, Answers, HelpKeyword, Params);
  6127. }
  6128. __finally
  6129. {
  6130. // cancel only balloon we popped up, otherwise we may cancel notification
  6131. // balloon that was there before the message dialog
  6132. // (such as "dd confirmation opt in balloon)
  6133. if (UseBalloon)
  6134. {
  6135. FTrayIcon->CancelBalloon();
  6136. }
  6137. }
  6138. return Result;
  6139. }
  6140. }
  6141. //---------------------------------------------------------------------------
  6142. void __fastcall TCustomScpExplorerForm::ShowExtendedException(
  6143. TTerminal * Terminal, Exception * E)
  6144. {
  6145. if (FTrayIcon->Visible)
  6146. {
  6147. PopupTrayBalloon(Terminal, L"", qtError, E);
  6148. }
  6149. // particularly prevent opening new session from jump list,
  6150. // while exception is shown
  6151. NonVisualDataModule->StartBusy();
  6152. try
  6153. {
  6154. ShowExtendedExceptionEx(Terminal, E);
  6155. }
  6156. __finally
  6157. {
  6158. NonVisualDataModule->EndBusy();
  6159. FTrayIcon->CancelBalloon();
  6160. }
  6161. }
  6162. //---------------------------------------------------------------------------
  6163. void __fastcall TCustomScpExplorerForm::TerminalReady()
  6164. {
  6165. // cannot rely on active page being page for active terminal,
  6166. // as it can happen that active page is the "new session" page
  6167. // (e.g. when reconnecting active terminal, while login dialog
  6168. // invoked from "new session" page is modal)
  6169. int ActiveTerminalIndex = TTerminalManager::Instance()->ActiveTerminalIndex;
  6170. UpdateSessionTab(SessionsPageControl->Pages[ActiveTerminalIndex]);
  6171. }
  6172. //---------------------------------------------------------------------------
  6173. void __fastcall TCustomScpExplorerForm::InactiveTerminalException(
  6174. TTerminal * Terminal, Exception * E)
  6175. {
  6176. Notify(Terminal, L"", qtError, false, NULL, NULL, E);
  6177. if (!Terminal->Active)
  6178. {
  6179. int Index = TTerminalManager::Instance()->IndexOf(Terminal);
  6180. if (DebugAlwaysTrue((Index >= 0) && (Index < SessionsPageControl->PageCount)))
  6181. {
  6182. UpdateSessionTab(SessionsPageControl->Pages[Index]);
  6183. }
  6184. }
  6185. }
  6186. //---------------------------------------------------------------------------
  6187. void __fastcall TCustomScpExplorerForm::Notify(TTerminal * Terminal,
  6188. UnicodeString Message, TQueryType Type,
  6189. bool Important, TNotifyEvent OnClick, TObject * UserData, Exception * E)
  6190. {
  6191. if ((E == NULL) ||
  6192. ExceptionMessage(E, Message))
  6193. {
  6194. unsigned int Seconds = WinConfiguration->NotificationsTimeout;
  6195. if (Important)
  6196. {
  6197. Seconds *= 5;
  6198. }
  6199. UnicodeString NoteMessage(UnformatMessage(Message));
  6200. if (Terminal != NULL)
  6201. {
  6202. NoteMessage = FORMAT(L"%s: %s",
  6203. (TTerminalManager::Instance()->GetTerminalTitle(Terminal, true), NoteMessage));
  6204. }
  6205. if (WinConfiguration->BalloonNotifications)
  6206. {
  6207. AddNote(NoteMessage);
  6208. PopupTrayBalloon(Terminal, Message, Type, NULL, Seconds, OnClick, UserData);
  6209. }
  6210. else
  6211. {
  6212. FlashOnBackground();
  6213. PostNote(NoteMessage, Seconds, OnClick, UserData);
  6214. }
  6215. }
  6216. }
  6217. //---------------------------------------------------------------------------
  6218. void __fastcall TCustomScpExplorerForm::QueueEmptyNoteClicked(TObject * Sender)
  6219. {
  6220. RestoreApp();
  6221. TTerminalNoteData * TerminalNoteData = dynamic_cast<TTerminalNoteData *>(Sender);
  6222. if (DebugAlwaysTrue(TerminalNoteData != NULL) &&
  6223. !NonVisualDataModule->Busy)
  6224. {
  6225. TTerminal * Terminal = TerminalNoteData->Terminal;
  6226. TTerminalManager::Instance()->ActiveTerminal = Terminal;
  6227. if (!ComponentVisible[fcQueueView])
  6228. {
  6229. ToggleQueueVisibility();
  6230. GoToQueue();
  6231. }
  6232. }
  6233. }
  6234. //---------------------------------------------------------------------------
  6235. void __fastcall TCustomScpExplorerForm::QueueEvent(TTerminal * ATerminal,
  6236. TTerminalQueue * /*Queue*/, TQueueEvent Event)
  6237. {
  6238. TManagedTerminal * ManagedTerminal = DebugNotNull(dynamic_cast<TManagedTerminal *>(ATerminal));
  6239. UnicodeString Message;
  6240. TNotifyEvent OnClick = NULL;
  6241. TObject * UserData = NULL;
  6242. bool QueueInvisible = !ComponentVisible[fcQueueView] || IsApplicationMinimized();
  6243. switch (Event)
  6244. {
  6245. case qeEmptyButMonitored:
  6246. if ((ATerminal != Terminal) || QueueInvisible)
  6247. {
  6248. Message = LoadStr(BALLOON_QUEUE_EMPTY);
  6249. OnClick = QueueEmptyNoteClicked;
  6250. TTerminalNoteData * TerminalNoteData = new TTerminalNoteData();
  6251. TerminalNoteData->Terminal = ATerminal;
  6252. UserData = TerminalNoteData;
  6253. }
  6254. break;
  6255. case qeEmpty:
  6256. OperationComplete(ManagedTerminal->QueueOperationStart);
  6257. break;
  6258. case qePendingUserAction:
  6259. if ((ATerminal != Terminal) ||
  6260. (QueueInvisible && !IsQueueAutoPopup()))
  6261. {
  6262. Message = LoadStr(BALLOON_QUEUE_USER_ACTION);
  6263. }
  6264. break;
  6265. default:
  6266. DebugFail();
  6267. }
  6268. if (!Message.IsEmpty())
  6269. {
  6270. Notify(ATerminal, Message, qtInformation, false, OnClick, UserData);
  6271. }
  6272. }
  6273. //---------------------------------------------------------------------------
  6274. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDCreateDragFileList(
  6275. TObject * /*Sender*/, TFileList * FileList, bool & Created)
  6276. {
  6277. if (FDDExtMapFile != NULL)
  6278. {
  6279. CloseHandle(FDDExtMapFile);
  6280. FDDExtMapFile = NULL;
  6281. }
  6282. if (WinConfiguration->DDFakeFile)
  6283. {
  6284. DDFakeFileInitDrag(FileList, Created);
  6285. }
  6286. }
  6287. //---------------------------------------------------------------------------
  6288. void __fastcall TCustomScpExplorerForm::DDFakeCreated(TObject * /*Sender*/, const UnicodeString FileName)
  6289. {
  6290. if (DebugAlwaysTrue(!FDragFakeDirectory.IsEmpty()) &&
  6291. SameText(ExtractFileName(FileName), ExtractFileName(FDragFakeDirectory)))
  6292. {
  6293. FFakeFileDropTarget = FileName;
  6294. }
  6295. }
  6296. //---------------------------------------------------------------------------
  6297. void __fastcall TCustomScpExplorerForm::DDFakeFileInitDrag(TFileList * FileList,
  6298. bool & Created)
  6299. {
  6300. FFakeFileDropTarget = UnicodeString();
  6301. FDragFakeDirectory =
  6302. ExcludeTrailingBackslash(WinConfiguration->TemporaryDir());
  6303. if (!ForceDirectories(ApiPath(FDragFakeDirectory)))
  6304. {
  6305. throw Exception(FMTLOAD(CREATE_TEMP_DIR_ERROR, (FDragFakeDirectory)));
  6306. }
  6307. FileSetAttr(ApiPath(FDragFakeDirectory), faHidden);
  6308. FileList->AddItem(NULL, FDragFakeDirectory);
  6309. Created = true;
  6310. if (!WinConfiguration->IsDDExtRunning() || (GetWindowsBuild() >= 17134))
  6311. {
  6312. FDragFakeMonitors = StartCreationDirectoryMonitorsOnEachDrive(FILE_NOTIFY_CHANGE_DIR_NAME, DDFakeCreated);
  6313. }
  6314. FDDExtMapFile = CreateFileMappingA((HANDLE)0xFFFFFFFF, NULL, PAGE_READWRITE,
  6315. 0, sizeof(TDragExtCommStruct), AnsiString(DRAG_EXT_MAPPING).c_str());
  6316. {
  6317. TMutexGuard Guard(FDDExtMutex, DRAGEXT_MUTEX_RELEASE_TIMEOUT);
  6318. TDragExtCommStruct* CommStruct;
  6319. CommStruct = static_cast<TDragExtCommStruct*>(MapViewOfFile(FDDExtMapFile,
  6320. FILE_MAP_ALL_ACCESS, 0, 0, 0));
  6321. DebugAssert(CommStruct != NULL);
  6322. CommStruct->Version = TDragExtCommStruct::CurrentVersion;
  6323. CommStruct->Dragging = true;
  6324. wcsncpy(CommStruct->DropDest, FDragFakeDirectory.c_str(),
  6325. LENOF(CommStruct->DropDest));
  6326. NULL_TERMINATE(CommStruct->DropDest);
  6327. UnmapViewOfFile(CommStruct);
  6328. }
  6329. FDDMoveSlipped = false;
  6330. }
  6331. //---------------------------------------------------------------------------
  6332. bool __fastcall TCustomScpExplorerForm::RemoteFileControlFileOperation(
  6333. TObject * Sender, TFileOperation Operation, bool NoConfirmation, void * Param)
  6334. {
  6335. bool Result;
  6336. if (Sender == RemoteDirView)
  6337. {
  6338. Result = ExecuteFileOperation(Operation, osRemote, true, NoConfirmation, Param);
  6339. }
  6340. else
  6341. {
  6342. DebugAssert(Sender == RemoteDriveView);
  6343. TStrings * FileList = RemoteDriveView->DragFileList();
  6344. try
  6345. {
  6346. Result = ExecuteFileOperation(Operation, osRemote, FileList, NoConfirmation, Param);
  6347. }
  6348. __finally
  6349. {
  6350. delete FileList;
  6351. }
  6352. }
  6353. if (FDDTargetControl == RemoteDriveView)
  6354. {
  6355. RemoteDriveView->UpdateDropTarget();
  6356. }
  6357. // foRemoteMove happens when file/dir is dragged within the remote tree view
  6358. // or from tree view to dir view.
  6359. // foMove happens when file/dir is dragged from remote tree view outside of
  6360. // application via dragex
  6361. if (((Operation == foRemoteMove) || (Operation == foMove)) &&
  6362. (DropSourceControl == RemoteDriveView))
  6363. {
  6364. RemoteDriveView->UpdateDropSource();
  6365. }
  6366. return Result;
  6367. }
  6368. //---------------------------------------------------------------------------
  6369. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDEnd(TObject * Sender)
  6370. {
  6371. // This also handles drops of remote files to queue.
  6372. // Drops of local files (uploads) are handled in QueueDDProcessDropped.
  6373. SAFE_DESTROY(FDDFileList);
  6374. if (!FFakeFileDropTarget.IsEmpty())
  6375. {
  6376. RemoveDir(ApiPath(FFakeFileDropTarget));
  6377. }
  6378. if ((FDDExtMapFile != NULL) || (FDDTargetControl == QueueView3))
  6379. {
  6380. try
  6381. {
  6382. TDragResult DDResult = (Sender == RemoteDirView) ?
  6383. RemoteDirView->LastDDResult : RemoteDriveView->LastDDResult;
  6384. // Focus is moved to the target application,
  6385. // but as we are going to present the UI, we need to steal the focus back.
  6386. // This most likely won't work though (windows does not allow application
  6387. // to steal focus most of the time)
  6388. Application->BringToFront();
  6389. // On older version of Windows we never got drMove here, see also comment below.
  6390. // On Windows 10, we get the "move".
  6391. if ((DDResult == drCopy) || (DDResult == drMove) || (DDResult == drInvalid))
  6392. {
  6393. UnicodeString TargetDirectory;
  6394. TFileOperation Operation;
  6395. // drInvalid may mean drMove, see comment below
  6396. switch (DDResult)
  6397. {
  6398. case drCopy:
  6399. Operation = foCopy;
  6400. break;
  6401. case drMove:
  6402. Operation = foMove;
  6403. break;
  6404. default:
  6405. DebugFail();
  6406. case drInvalid:
  6407. // prefer "copy" for safety
  6408. Operation = FLAGSET(FLastDropEffect, DROPEFFECT_MOVE) ? foMove : foCopy;
  6409. break;
  6410. }
  6411. if (FDDMoveSlipped)
  6412. {
  6413. Operation = foMove;
  6414. }
  6415. TTransferOperationParam Param;
  6416. UnicodeString CounterName;
  6417. bool ForceQueue;
  6418. if (!DDGetTarget(Param.TargetDirectory, ForceQueue, CounterName))
  6419. {
  6420. // we get drInvalid both if d&d was intercepted by ddext,
  6421. // and when users drops on no-drop location.
  6422. // we tell the difference by existence of response from ddext,
  6423. // so we ignore absence of response in this case
  6424. if (DDResult != drInvalid)
  6425. {
  6426. // here we know that the extension is installed,
  6427. // as it is checked as soon as drag&drop starts from
  6428. // RemoteFileControlDDCreateDragFileList
  6429. Configuration->Usage->Inc(L"DownloadsDragDropExternalExtTargetUnknown");
  6430. throw ExtException(NULL, LoadStr(DRAGEXT_TARGET_UNKNOWN2), HELP_DRAGEXT_TARGET_UNKNOWN);
  6431. }
  6432. }
  6433. else
  6434. {
  6435. // download using fake file
  6436. Param.Temp = false;
  6437. Param.DragDrop = true;
  6438. if (ForceQueue)
  6439. {
  6440. Param.Queue = asOn;
  6441. }
  6442. if (Sender == RemoteDirView)
  6443. {
  6444. Param.Options = FLAGMASK(SelectedAllFilesInDirView(RemoteDirView), coAllFiles);
  6445. }
  6446. if (RemoteFileControlFileOperation(Sender, Operation,
  6447. (WinConfiguration->DDTransferConfirmation == asOff), &Param))
  6448. {
  6449. Configuration->Usage->Inc(CounterName);
  6450. }
  6451. }
  6452. }
  6453. }
  6454. __finally
  6455. {
  6456. CloseHandle(FDDExtMapFile);
  6457. FDDExtMapFile = NULL;
  6458. delete FDragFakeMonitors;
  6459. FDragFakeMonitors = NULL;
  6460. RemoveDir(ApiPath(FDragFakeDirectory));
  6461. FDragFakeDirectory = L"";
  6462. FFakeFileDropTarget = UnicodeString();
  6463. }
  6464. }
  6465. if (FDragDropSshTerminate.get() != NULL)
  6466. {
  6467. FDragDropSshTerminate->Rethrow();
  6468. }
  6469. }
  6470. //---------------------------------------------------------------------------
  6471. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDGiveFeedback(
  6472. TObject * Sender, int dwEffect, HRESULT & /*Result*/)
  6473. {
  6474. HCURSOR SlippedCopyCursor;
  6475. FDDMoveSlipped =
  6476. (FDragMoveCursor != NULL) &&
  6477. (!WinConfiguration->DDAllowMoveInit) && (dwEffect == DROPEFFECT_Copy) &&
  6478. ((IsFileControl(FDDTargetControl, osRemote) && (GetKeyState(VK_CONTROL) >= 0) &&
  6479. FTerminal->IsCapable[fcRemoteMove]) ||
  6480. (IsFileControl(FDDTargetControl, osLocal) && (GetKeyState(VK_SHIFT) < 0)));
  6481. SlippedCopyCursor = FDDMoveSlipped ? FDragMoveCursor : Dragdrop::DefaultCursor;
  6482. DragDropFiles(Sender)->CHCopy = SlippedCopyCursor;
  6483. DragDropFiles(Sender)->CHScrollCopy = SlippedCopyCursor;
  6484. // Remember drop effect so we know (when user drops files), if we copy or move
  6485. FLastDropEffect = dwEffect;
  6486. }
  6487. //---------------------------------------------------------------------------
  6488. bool __fastcall TCustomScpExplorerForm::DDGetTarget(
  6489. UnicodeString & Directory, bool & ForceQueue, UnicodeString & CounterName)
  6490. {
  6491. bool Result;
  6492. if (FDDTargetControl == QueueView3)
  6493. {
  6494. Directory = DefaultDownloadTargetDirectory();
  6495. Result = true;
  6496. CounterName = L"DownloadsDragDropQueue";
  6497. ForceQueue = true;
  6498. }
  6499. else if (!FFakeFileDropTarget.IsEmpty())
  6500. {
  6501. Directory = ExcludeTrailingBackslash(ExtractFilePath(FFakeFileDropTarget));
  6502. Result = true;
  6503. ForceQueue = false;
  6504. CounterName = L"DownloadsDragDropFakeFile";
  6505. }
  6506. else
  6507. {
  6508. ForceQueue = false;
  6509. Enabled = false;
  6510. try
  6511. {
  6512. int Timer = 0;
  6513. Result = false;
  6514. while (!Result && (Timer < WinConfiguration->DDExtTimeout))
  6515. {
  6516. {
  6517. TMutexGuard Guard(FDDExtMutex, DRAGEXT_MUTEX_RELEASE_TIMEOUT);
  6518. TDragExtCommStruct* CommStruct;
  6519. CommStruct = static_cast<TDragExtCommStruct*>(MapViewOfFile(FDDExtMapFile,
  6520. FILE_MAP_ALL_ACCESS, 0, 0, 0));
  6521. DebugAssert(CommStruct != NULL);
  6522. Result = !CommStruct->Dragging;
  6523. if (Result)
  6524. {
  6525. Directory = ExtractFilePath(CommStruct->DropDest);
  6526. CounterName = L"DownloadsDragDropExternalExt";
  6527. }
  6528. UnmapViewOfFile(CommStruct);
  6529. }
  6530. if (!Result)
  6531. {
  6532. Sleep(50);
  6533. Timer += 50;
  6534. Application->ProcessMessages();
  6535. }
  6536. }
  6537. }
  6538. __finally
  6539. {
  6540. Enabled = true;
  6541. }
  6542. }
  6543. return Result;
  6544. }
  6545. //---------------------------------------------------------------------------
  6546. void __fastcall TCustomScpExplorerForm::AddDelayedDirectoryDeletion(
  6547. const UnicodeString TempDir, int SecDelay)
  6548. {
  6549. TDateTime Alarm = Now() + (double)((double)SecDelay*OneMillisecond);
  6550. FDelayedDeletionList->AddObject(TempDir, reinterpret_cast<TObject*>(Alarm.FileDate()));
  6551. if (FDelayedDeletionTimer == NULL)
  6552. {
  6553. DebugAssert(HandleAllocated());
  6554. FDelayedDeletionTimer = new TTimer(this);
  6555. FDelayedDeletionTimer->Interval = 10000;
  6556. FDelayedDeletionTimer->OnTimer = DoDelayedDeletion;
  6557. }
  6558. else
  6559. {
  6560. FDelayedDeletionTimer->Enabled = true;
  6561. }
  6562. }
  6563. //---------------------------------------------------------------------------
  6564. void __fastcall TCustomScpExplorerForm::DoDelayedDeletion(TObject * Sender)
  6565. {
  6566. DebugAssert(FDelayedDeletionList != NULL);
  6567. TDateTime N = Now();
  6568. TDateTime Alert;
  6569. UnicodeString Directory;
  6570. for (int Index = FDelayedDeletionList->Count-1; Index >= 0; Index--)
  6571. {
  6572. Alert = FileDateToDateTime(reinterpret_cast<int>(FDelayedDeletionList->Objects[Index]));
  6573. if ((N >= Alert) || (Sender == NULL))
  6574. {
  6575. Directory = FDelayedDeletionList->Strings[Index];
  6576. if (DeleteDirectory(ExcludeTrailingBackslash(Directory)))
  6577. {
  6578. FDelayedDeletionList->Delete(Index);
  6579. }
  6580. }
  6581. }
  6582. if (FDelayedDeletionList->Count == 0)
  6583. {
  6584. FDelayedDeletionTimer->Enabled = false;
  6585. }
  6586. }
  6587. //---------------------------------------------------------------------------
  6588. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDTargetDrop()
  6589. {
  6590. if (IsFileControl(FDDTargetControl, osRemote) ||
  6591. (FDDTargetControl == SessionsPageControl))
  6592. {
  6593. TTerminal * TargetTerminal = NULL;
  6594. TFileOperation Operation = foNone;
  6595. if (FDDTargetControl == SessionsPageControl)
  6596. {
  6597. TPoint Point = SessionsPageControl->ScreenToClient(Mouse->CursorPos);
  6598. int Index = SessionsPageControl->IndexOfTabAt(Point.X, Point.Y);
  6599. // do not allow dropping on the "+" tab
  6600. TargetTerminal = GetSessionTabTerminal(SessionsPageControl->Pages[Index]);
  6601. if (TargetTerminal != NULL)
  6602. {
  6603. if ((FLastDropEffect == DROPEFFECT_MOVE) &&
  6604. (TargetTerminal == TTerminalManager::Instance()->ActiveTerminal))
  6605. {
  6606. Operation = foRemoteMove;
  6607. }
  6608. else
  6609. {
  6610. Operation = foRemoteCopy;
  6611. }
  6612. }
  6613. }
  6614. else
  6615. {
  6616. // when move from remote side is disabled, we allow copying inside the remote
  6617. // panel, but we interpret is as moving (we also slip in the move cursor)
  6618. if ((FLastDropEffect == DROPEFFECT_MOVE) ||
  6619. (!WinConfiguration->DDAllowMoveInit && (FLastDropEffect == DROPEFFECT_COPY) &&
  6620. FDDMoveSlipped))
  6621. {
  6622. Operation = foRemoteMove;
  6623. }
  6624. else if (FLastDropEffect == DROPEFFECT_COPY)
  6625. {
  6626. Operation = foRemoteCopy;
  6627. }
  6628. }
  6629. if (Operation != foNone)
  6630. {
  6631. RemoteFileControlFileOperation(DropSourceControl,
  6632. Operation, (WinConfiguration->DDTransferConfirmation == asOff), TargetTerminal);
  6633. }
  6634. // abort drag&drop
  6635. Abort();
  6636. }
  6637. else if ((FDDExtMapFile == NULL) && (FLastDropEffect != DROPEFFECT_NONE) &&
  6638. // Drops of remote files to queue are handled in RemoteFileControlDDEnd
  6639. (FDDTargetControl != QueueView3))
  6640. {
  6641. DebugAssert(!FDragTempDir.IsEmpty());
  6642. TTransferType Type;
  6643. UnicodeString TempDir = FDragTempDir;
  6644. // We clear FDragTempDir before calling
  6645. // just in case it fail (raises exception)
  6646. FDragTempDir = L"";
  6647. Type = (FLastDropEffect & DROPEFFECT_MOVE ? ttMove : Type = ttCopy);
  6648. TGUICopyParamType CopyParams = GUIConfiguration->CurrentCopyParam;
  6649. // empty directory parameter means temp directory -> don't display it!
  6650. UnicodeString TargetDir = L"";
  6651. int Options = 0;
  6652. if (!CopyParamDialog(tdToLocal, Type, true, FDDFileList,
  6653. TargetDir, CopyParams, (WinConfiguration->DDTransferConfirmation != asOff), true, Options))
  6654. {
  6655. Abort();
  6656. }
  6657. // TargetDir is set when dropped on local file control
  6658. // (this was workaround for legacy dirview event handling, now it should be
  6659. // made prettier)
  6660. if (TargetDir.IsEmpty())
  6661. {
  6662. TargetDir = TempDir;
  6663. if (ForceDirectories(ApiPath(TargetDir)))
  6664. {
  6665. DebugAssert(Terminal && !TargetDir.IsEmpty());
  6666. FPendingTempSpaceWarn = true;
  6667. try
  6668. {
  6669. FDragDropOperation = true;
  6670. int Params = cpTemporary |
  6671. (Type == ttMove ? cpDelete : 0);
  6672. DDDownload(FDDFileList, TargetDir, &CopyParams, Params);
  6673. Configuration->Usage->Inc(L"DownloadsDragDropExternalTemp");
  6674. }
  6675. __finally
  6676. {
  6677. FDragDropOperation = false;
  6678. FPendingTempSpaceWarn = false;
  6679. AddDelayedDirectoryDeletion(TargetDir, WinConfiguration->DDDeleteDelay);
  6680. }
  6681. }
  6682. else
  6683. {
  6684. throw Exception(FMTLOAD(CREATE_TEMP_DIR_ERROR, (TargetDir)));
  6685. }
  6686. }
  6687. }
  6688. }
  6689. //---------------------------------------------------------------------------
  6690. void __fastcall TCustomScpExplorerForm::DDDownload(TStrings * FilesToCopy,
  6691. const UnicodeString TargetDir, const TCopyParamType * CopyParam, int Params)
  6692. {
  6693. void * BatchStorage;
  6694. BatchStart(BatchStorage);
  6695. try
  6696. {
  6697. UpdateCopyParamCounters(*CopyParam);
  6698. Terminal->CopyToLocal(FilesToCopy, TargetDir, CopyParam, Params, NULL);
  6699. if (FLAGSET(Params, cpDelete) && (DropSourceControl == RemoteDriveView))
  6700. {
  6701. RemoteDriveView->UpdateDropSource();
  6702. }
  6703. }
  6704. __finally
  6705. {
  6706. BatchEnd(BatchStorage);
  6707. }
  6708. }
  6709. //---------------------------------------------------------------------------
  6710. class TFakeDataObjectFilesEx : public TDataObjectFilesEx
  6711. {
  6712. public:
  6713. __fastcall TFakeDataObjectFilesEx(TFileList * AFileList, bool RenderPIDL,
  6714. bool RenderFilename) : TDataObjectFilesEx(AFileList, RenderPIDL, RenderFilename)
  6715. {
  6716. }
  6717. virtual bool __fastcall AllowData(const tagFORMATETC & FormatEtc)
  6718. {
  6719. return (FormatEtc.cfFormat == CF_HDROP) ? false :
  6720. TDataObjectFilesEx::AllowData(FormatEtc);
  6721. }
  6722. };
  6723. //---------------------------------------------------------------------------
  6724. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDCreateDataObject(
  6725. TObject * Sender, TDataObject *& DataObject)
  6726. {
  6727. if (FDDExtMapFile != NULL)
  6728. {
  6729. TFileList * FileList = DragDropFiles(Sender)->FileList;
  6730. if (!FileList->RenderPIDLs() || !FileList->RenderNames())
  6731. {
  6732. Abort();
  6733. }
  6734. if (FileList->Count > 0)
  6735. {
  6736. TDataObjectFilesEx * FilesObject = new TFakeDataObjectFilesEx(FileList, true, true);
  6737. if (!FilesObject->IsValid(true, true))
  6738. {
  6739. FilesObject->_Release();
  6740. }
  6741. else
  6742. {
  6743. DataObject = FilesObject;
  6744. }
  6745. }
  6746. }
  6747. }
  6748. //---------------------------------------------------------------------------
  6749. void __fastcall TCustomScpExplorerForm::GoToCommandLine()
  6750. {
  6751. DebugFail();
  6752. }
  6753. //---------------------------------------------------------------------------
  6754. void __fastcall TCustomScpExplorerForm::GoToTree()
  6755. {
  6756. ComponentVisible[fcRemoteTree] = true;
  6757. RemoteDriveView->SetFocus();
  6758. }
  6759. //---------------------------------------------------------------------------
  6760. TStrings * __fastcall TCustomScpExplorerForm::PanelExport(TOperationSide Side,
  6761. TPanelExport Export)
  6762. {
  6763. TCustomDirView * DirView = this->DirView(Side);
  6764. std::unique_ptr<TStrings> ExportData(new TStringList());
  6765. switch (Export)
  6766. {
  6767. case pePath:
  6768. ExportData->Add(DirView->PathName);
  6769. break;
  6770. case peFileList:
  6771. case peFullFileList:
  6772. {
  6773. bool FullPath = (Export == peFullFileList);
  6774. DirView->CreateFileList(false, FullPath, ExportData.get());
  6775. for (int Index = 0; Index < ExportData->Count; Index++)
  6776. {
  6777. if (ExportData->Strings[Index].Pos(L" ") > 0)
  6778. {
  6779. ExportData->Strings[Index] = FORMAT(L"\"%s\"", (ExportData->Strings[Index]));
  6780. }
  6781. }
  6782. }
  6783. break;
  6784. default:
  6785. DebugFail();
  6786. }
  6787. return ExportData.release();
  6788. }
  6789. //---------------------------------------------------------------------------
  6790. void __fastcall TCustomScpExplorerForm::PanelExport(TOperationSide Side,
  6791. TPanelExport Export, TPanelExportDestination Destination)
  6792. {
  6793. std::unique_ptr<TStrings> ExportData(PanelExport(Side, Export));
  6794. PanelExportStore(Side, Export, Destination, ExportData.get());
  6795. }
  6796. //---------------------------------------------------------------------------
  6797. void __fastcall TCustomScpExplorerForm::PanelExportStore(TOperationSide /*Side*/,
  6798. TPanelExport /*Export*/, TPanelExportDestination Destination,
  6799. TStrings * ExportData)
  6800. {
  6801. if (Destination == pedClipboard)
  6802. {
  6803. TInstantOperationVisualizer Visualizer;
  6804. CopyToClipboard(ExportData);
  6805. }
  6806. else
  6807. {
  6808. DebugFail();
  6809. }
  6810. }
  6811. //---------------------------------------------------------------------------
  6812. void __fastcall TCustomScpExplorerForm::Filter(TOperationSide Side)
  6813. {
  6814. TCustomDirView * DirView = this->DirView(Side);
  6815. TFileFilter Filter;
  6816. DefaultFileFilter(Filter);
  6817. Filter.Masks = DirView->Mask;
  6818. if (DoFilterMaskDialog(DirView, &Filter))
  6819. {
  6820. DirView->Mask = TFileMasks::NormalizeMask(Filter.Masks);
  6821. Configuration->Usage->Inc(L"Filters");
  6822. }
  6823. }
  6824. //---------------------------------------------------------------------------
  6825. TQueueOperation __fastcall TCustomScpExplorerForm::DefaultQueueOperation()
  6826. {
  6827. return FQueueController->DefaultOperation();
  6828. }
  6829. //---------------------------------------------------------------------------
  6830. bool __fastcall TCustomScpExplorerForm::AllowQueueOperation(
  6831. TQueueOperation Operation, void ** Param)
  6832. {
  6833. switch (Operation)
  6834. {
  6835. case qoPreferences:
  6836. return true;
  6837. case qoGoTo:
  6838. return ComponentVisible[fcQueueView] && QueueView3->Enabled;
  6839. case qoOnceEmpty:
  6840. return !FQueueController->Empty;
  6841. default:
  6842. return FQueueController->AllowOperation(Operation, Param);
  6843. }
  6844. }
  6845. //---------------------------------------------------------------------------
  6846. void __fastcall TCustomScpExplorerForm::GoToQueue()
  6847. {
  6848. if (DebugAlwaysTrue(QueueView3->Visible))
  6849. {
  6850. QueueView3->SetFocus();
  6851. }
  6852. }
  6853. //---------------------------------------------------------------------------
  6854. void __fastcall TCustomScpExplorerForm::ExecuteQueueOperation(
  6855. TQueueOperation Operation, void * Param)
  6856. {
  6857. if (Operation == qoGoTo)
  6858. {
  6859. GoToQueue();
  6860. }
  6861. else if (Operation == qoPreferences)
  6862. {
  6863. PreferencesDialog(pmQueue);
  6864. }
  6865. else
  6866. {
  6867. FQueueController->ExecuteOperation(Operation, Param);
  6868. }
  6869. }
  6870. //---------------------------------------------------------------------------
  6871. bool __fastcall TCustomScpExplorerForm::GetQueueEnabled()
  6872. {
  6873. return (Queue != NULL) && Queue->Enabled;
  6874. }
  6875. //---------------------------------------------------------------------------
  6876. void __fastcall TCustomScpExplorerForm::ToggleQueueEnabled()
  6877. {
  6878. DebugAssert(Queue != NULL);
  6879. if (Queue != NULL)
  6880. {
  6881. Queue->Enabled = !Queue->Enabled;
  6882. }
  6883. }
  6884. //---------------------------------------------------------------------------
  6885. void __fastcall TCustomScpExplorerForm::QueueView3ContextPopup(
  6886. TObject * /*Sender*/, TPoint & /*MousePos*/, bool & /*Handled*/)
  6887. {
  6888. FQueueActedItem = QueueView3->ItemFocused;
  6889. }
  6890. //---------------------------------------------------------------------------
  6891. /*virtual*/ int __fastcall TCustomScpExplorerForm::GetStaticComponentsHeight()
  6892. {
  6893. return TopDock->Height + QueueSplitter->Height;
  6894. }
  6895. //---------------------------------------------------------------------------
  6896. void __fastcall TCustomScpExplorerForm::QueueSplitterCanResize(
  6897. TObject * /*Sender*/, int & NewSize, bool & Accept)
  6898. {
  6899. // when queue is hidden by double-clicking splitter, stray attempt to
  6900. // resize the panel with strange value arrives, make sure it is ignored
  6901. if (ComponentVisible[fcQueueView])
  6902. {
  6903. int HeightLimit = ClientHeight - GetStaticComponentsHeight() -
  6904. RemotePanel->Constraints->MinHeight;
  6905. if (NewSize > HeightLimit)
  6906. {
  6907. NewSize = HeightLimit;
  6908. }
  6909. }
  6910. else
  6911. {
  6912. Accept = false;
  6913. }
  6914. }
  6915. //---------------------------------------------------------------------------
  6916. void __fastcall TCustomScpExplorerForm::QueueView3StartDrag(TObject * /*Sender*/,
  6917. TDragObject *& /*DragObject*/)
  6918. {
  6919. FQueueActedItem = QueueView3->ItemFocused;
  6920. }
  6921. //---------------------------------------------------------------------------
  6922. void __fastcall TCustomScpExplorerForm::QueueView3DragOver(TObject * /*Sender*/,
  6923. TObject * Source, int X, int Y, TDragState /*State*/, bool & Accept)
  6924. {
  6925. Accept = true;
  6926. if (Source == QueueView3)
  6927. {
  6928. TListItem * DropTarget = QueueView3->GetItemAt(X, Y);
  6929. Accept = (DropTarget != NULL) && (FQueueActedItem != NULL);
  6930. if (Accept)
  6931. {
  6932. TQueueItemProxy * QueueItem;
  6933. TQueueItemProxy * DestQueueItem;
  6934. QueueItem = static_cast<TQueueItemProxy *>(FQueueActedItem->Data);
  6935. DestQueueItem = static_cast<TQueueItemProxy *>(DropTarget->Data);
  6936. Accept = (QueueItem != DestQueueItem) &&
  6937. (QueueItem->Status == TQueueItem::qsPending) &&
  6938. (DestQueueItem->Status == TQueueItem::qsPending);
  6939. }
  6940. }
  6941. }
  6942. //---------------------------------------------------------------------------
  6943. void __fastcall TCustomScpExplorerForm::QueueView3DragDrop(TObject * /*Sender*/,
  6944. TObject * /*Source*/, int /*X*/, int /*Y*/)
  6945. {
  6946. if ((FQueueActedItem != NULL) && (QueueView3->DropTarget != NULL))
  6947. {
  6948. TQueueItemProxy * QueueItem;
  6949. TQueueItemProxy * DestQueueItem;
  6950. QueueItem = static_cast<TQueueItemProxy *>(FQueueActedItem->Data);
  6951. DestQueueItem = static_cast<TQueueItemProxy *>(QueueView3->DropTarget->Data);
  6952. QueueItem->Move(DestQueueItem);
  6953. }
  6954. }
  6955. //---------------------------------------------------------------------------
  6956. void __fastcall TCustomScpExplorerForm::QueueView3Enter(TObject * /*Sender*/)
  6957. {
  6958. if ((QueueView3->ItemFocused == NULL) &&
  6959. (QueueView3->Items->Count > 0))
  6960. {
  6961. QueueView3->ItemFocused = QueueView3->Items->Item[0];
  6962. }
  6963. QueueLabelUpdateStatus();
  6964. }
  6965. //---------------------------------------------------------------------------
  6966. void __fastcall TCustomScpExplorerForm::QueueView3Exit(TObject * /*Sender*/)
  6967. {
  6968. QueueLabelUpdateStatus();
  6969. }
  6970. //---------------------------------------------------------------------------
  6971. void __fastcall TCustomScpExplorerForm::QueueLabelUpdateStatus()
  6972. {
  6973. QueueLabel->UpdateStatus();
  6974. }
  6975. //---------------------------------------------------------------------------
  6976. void __fastcall TCustomScpExplorerForm::QueueView3SelectItem(
  6977. TObject * /*Sender*/, TListItem * /*Item*/, bool Selected)
  6978. {
  6979. if (Selected)
  6980. {
  6981. NonVisualDataModule->UpdateNonVisibleActions();
  6982. }
  6983. }
  6984. //---------------------------------------------------------------------------
  6985. TDragDropFilesEx * __fastcall TCustomScpExplorerForm::DragDropFiles(TObject * Sender)
  6986. {
  6987. TDragDropFilesEx * Result = NULL;
  6988. if (Sender == SessionsPageControl)
  6989. {
  6990. Result = FSessionsDragDropFilesEx;
  6991. }
  6992. else if (Sender == QueueView3)
  6993. {
  6994. Result = FQueueDragDropFilesEx;
  6995. }
  6996. else
  6997. {
  6998. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  6999. if (DirView != NULL)
  7000. {
  7001. Result = DirView->DragDropFilesEx;
  7002. }
  7003. else
  7004. {
  7005. TCustomDriveView * DriveView = dynamic_cast<TCustomDriveView *>(Sender);
  7006. if (DriveView != NULL)
  7007. {
  7008. Result = DriveView->DragDropFilesEx;
  7009. }
  7010. }
  7011. }
  7012. DebugAssert(Result != NULL);
  7013. return Result;
  7014. }
  7015. //---------------------------------------------------------------------------
  7016. bool __fastcall TCustomScpExplorerForm::SelectedAllFilesInDirView(TCustomDirView * DView)
  7017. {
  7018. return (DView->SelCount == DView->FilesCount);
  7019. }
  7020. //---------------------------------------------------------------------------
  7021. bool __fastcall TCustomScpExplorerForm::DraggingAllFilesFromDirView(TOperationSide Side, TStrings * FileList)
  7022. {
  7023. return HasDirView[Side] && (DropSourceControl == DirView(Side)) && (FileList->Count == DirView(Side)->FilesCount);
  7024. }
  7025. //---------------------------------------------------------------------------
  7026. void __fastcall TCustomScpExplorerForm::RemoteFileControlDragDropFileOperation(
  7027. TObject * Sender, int Effect, UnicodeString TargetPath, bool ForceQueue)
  7028. {
  7029. TFileOperation Operation;
  7030. switch (Effect)
  7031. {
  7032. case DROPEFFECT_MOVE:
  7033. Operation = foMove;
  7034. break;
  7035. case DROPEFFECT_COPY:
  7036. // occures on WinXP (reported by user)
  7037. default:
  7038. Operation = foCopy;
  7039. break;
  7040. };
  7041. TDragDropFilesEx * DragDropFilesEx = DragDropFiles(Sender);
  7042. // see a comment in TUnixDirView::PerformItemDragDropOperation
  7043. if (DragDropFilesEx->FileList->Count > 0)
  7044. {
  7045. TStrings * FileList = new TStringList();
  7046. try
  7047. {
  7048. for (int Index = 0; Index < DragDropFilesEx->FileList->Count; Index++)
  7049. {
  7050. FileList->Add(DragDropFilesEx->FileList->Items[Index]->Name);
  7051. }
  7052. FDragDropOperation = true;
  7053. TTransferOperationParam Param;
  7054. Param.TargetDirectory = TargetPath;
  7055. // upload, no temp dirs
  7056. Param.Temp = false;
  7057. Param.DragDrop = true;
  7058. Param.Options =
  7059. FLAGMASK(DraggingAllFilesFromDirView(osLocal, FileList), coAllFiles);
  7060. if (ForceQueue)
  7061. {
  7062. Param.Queue = asOn;
  7063. }
  7064. if (ExecuteFileOperation(Operation, osLocal, FileList,
  7065. (WinConfiguration->DDTransferConfirmation == asOff), &Param))
  7066. {
  7067. if (IsFileControl(DropSourceControl, osLocal))
  7068. {
  7069. Configuration->Usage->Inc(L"UploadsDragDropInternal");
  7070. }
  7071. else
  7072. {
  7073. Configuration->Usage->Inc(L"UploadsDragDropExternal");
  7074. }
  7075. }
  7076. }
  7077. __finally
  7078. {
  7079. FDragDropOperation = false;
  7080. delete FileList;
  7081. }
  7082. }
  7083. }
  7084. //---------------------------------------------------------------------------
  7085. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDFileOperation(
  7086. TObject * Sender, int Effect, UnicodeString /*SourcePath*/,
  7087. UnicodeString TargetPath, bool & /*DoOperation*/)
  7088. {
  7089. RemoteFileControlDragDropFileOperation(Sender, Effect, TargetPath, false);
  7090. }
  7091. //---------------------------------------------------------------------------
  7092. void __fastcall TCustomScpExplorerForm::RemoteFileContolDDChooseEffect(
  7093. TObject * Sender, int grfKeyState, int & dwEffect)
  7094. {
  7095. // if any drop effect is allowed at all (e.g. no drop to self and drop to parent)
  7096. if ((dwEffect != DROPEFFECT_None) &&
  7097. IsFileControl(DropSourceControl, osRemote))
  7098. {
  7099. // do not allow drop on remote panel (on free space, still allow drop on directories)
  7100. if ((Sender == RemoteDirView) && (DropSourceControl == RemoteDirView) &&
  7101. (RemoteDirView->DropTarget == NULL))
  7102. {
  7103. dwEffect = DROPEFFECT_None;
  7104. }
  7105. else
  7106. {
  7107. if (dwEffect == DROPEFFECT_Copy)
  7108. {
  7109. bool MoveCapable = FTerminal->IsCapable[fcRemoteMove];
  7110. // currently we support copying always (at least via temporary directory);
  7111. // remove associated checks once this all proves stable and working
  7112. bool CopyCapable = true;
  7113. // if we do not support neither of operations, there's no discussion
  7114. if (!MoveCapable && !CopyCapable)
  7115. {
  7116. dwEffect = DROPEFFECT_None;
  7117. }
  7118. // when moving is disabled, we need to keep effect to "copy",
  7119. // which will be later interpretted as move (with slipped-in cursor)
  7120. else if (!WinConfiguration->DDAllowMoveInit && FLAGCLEAR(grfKeyState, MK_CONTROL))
  7121. {
  7122. // no-op, keep copy
  7123. }
  7124. else
  7125. {
  7126. // The default effect inside remote panel is move,
  7127. // unless we do not support it, but support copy
  7128. if (FLAGCLEAR(grfKeyState, MK_CONTROL))
  7129. {
  7130. dwEffect = MoveCapable ? DROPEFFECT_Move : DROPEFFECT_Copy;
  7131. }
  7132. else
  7133. {
  7134. // with ctrl-down, we want copy unless it is not supported
  7135. dwEffect = CopyCapable ? DROPEFFECT_Copy : DROPEFFECT_None;
  7136. }
  7137. }
  7138. }
  7139. }
  7140. }
  7141. }
  7142. //---------------------------------------------------------------------------
  7143. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDDragFileName(
  7144. TObject * Sender, TRemoteFile * File, UnicodeString & FileName)
  7145. {
  7146. if (FDDTotalSize >= 0)
  7147. {
  7148. if (File->IsDirectory)
  7149. {
  7150. FDDTotalSize = -1;
  7151. }
  7152. else
  7153. {
  7154. FDDTotalSize += File->Size;
  7155. }
  7156. }
  7157. DebugAssert(!FDragTempDir.IsEmpty());
  7158. // TODO: this is quite ineffective
  7159. // TODO: what if invalid character replacement is disabled?
  7160. FileName = FDragTempDir + GUIConfiguration->CurrentCopyParam.ValidLocalFileName(File->FileName);
  7161. UnicodeString TransferFileName;
  7162. if (Sender == RemoteDriveView)
  7163. {
  7164. TransferFileName = UnixExcludeTrailingBackslash(File->FullFileName);
  7165. }
  7166. else
  7167. {
  7168. TransferFileName = File->FileName;
  7169. }
  7170. FDDFileList->AddObject(TransferFileName, File);
  7171. }
  7172. //---------------------------------------------------------------------------
  7173. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDDragDetect(
  7174. TObject * /*Sender*/, int /*grfKeyState*/, const TPoint & /*DetectStart*/,
  7175. const TPoint & /*Point*/, TDragDetectStatus /*DragStatus*/)
  7176. {
  7177. // sometimes we do not get DDEnd so the list is not released
  7178. SAFE_DESTROY(FDDFileList);
  7179. FDDFileList = new TStringList();
  7180. FDragTempDir = WinConfiguration->TemporaryDir();
  7181. FDDTotalSize = 0;
  7182. FDragDropSshTerminate.reset(NULL);
  7183. }
  7184. //---------------------------------------------------------------------------
  7185. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDQueryContinueDrag(
  7186. TObject * /*Sender*/, BOOL /*FEscapePressed*/, int /*grfKeyState*/,
  7187. HRESULT & Result)
  7188. {
  7189. if (Result == DRAGDROP_S_DROP)
  7190. {
  7191. try
  7192. {
  7193. GlobalDragImageList->HideDragImage();
  7194. try
  7195. {
  7196. RemoteFileControlDDTargetDrop();
  7197. }
  7198. catch(ESshTerminate & E)
  7199. {
  7200. FDragDropSshTerminate.reset(E.Clone());
  7201. }
  7202. }
  7203. catch (Exception &E)
  7204. {
  7205. // If downloading fails we need to cancel drag&drop, otherwise
  7206. // Explorer shows error
  7207. // But by the way exception probably never reach this point as
  7208. // it's catched on way.
  7209. // Fatal exceptions get here (like when opening a secondary shell extension for file duplication fails).
  7210. Result = DRAGDROP_S_CANCEL;
  7211. ShowExtendedException(Terminal, &E);
  7212. }
  7213. }
  7214. }
  7215. //---------------------------------------------------------------------------
  7216. void __fastcall TCustomScpExplorerForm::DirViewMatchMask(
  7217. TObject * /*Sender*/, UnicodeString FileName, bool Directory, __int64 Size,
  7218. TDateTime Modification, UnicodeString Masks, bool & Matches, bool AllowImplicitMatches)
  7219. {
  7220. TFileMasks::TParams MaskParams;
  7221. MaskParams.Size = Size;
  7222. MaskParams.Modification = Modification;
  7223. // this does not re-parse the mask if it is the same as the last time
  7224. FDirViewMatchMask = Masks;
  7225. bool ImplicitMatch;
  7226. Matches =
  7227. // RecurseInclude parameter has no effect as the Path is empty
  7228. FDirViewMatchMask.Matches(FileName, Directory, UnicodeString(L""), &MaskParams, true, ImplicitMatch) &&
  7229. (AllowImplicitMatches || !ImplicitMatch);
  7230. }
  7231. //---------------------------------------------------------------------------
  7232. void __fastcall TCustomScpExplorerForm::DirViewGetOverlay(
  7233. TObject * Sender, TListItem * Item, WORD & Indexes)
  7234. {
  7235. TCustomDirView * DirView = reinterpret_cast<TCustomDirView *>(Sender);
  7236. UnicodeString Ext;
  7237. if (DirView == RemoteDirView)
  7238. {
  7239. Ext = UnixExtractFileExt(DirView->ItemFileName(Item));
  7240. }
  7241. else
  7242. {
  7243. Ext = ExtractFileExt(DirView->ItemFileName(Item));
  7244. }
  7245. if (SameText(Ext, Configuration->PartialExt))
  7246. {
  7247. Indexes |= oiPartial;
  7248. }
  7249. }
  7250. //---------------------------------------------------------------------------
  7251. bool __fastcall TCustomScpExplorerForm::CanPasteToDirViewFromClipBoard()
  7252. {
  7253. return
  7254. DirViewEnabled(osCurrent) &&
  7255. DirView(osCurrent)->CanPasteFromClipBoard();
  7256. }
  7257. //---------------------------------------------------------------------------
  7258. bool __fastcall TCustomScpExplorerForm::CanPasteFromClipBoard()
  7259. {
  7260. bool Result = false;
  7261. if (CanPasteToDirViewFromClipBoard())
  7262. {
  7263. Result = true;
  7264. }
  7265. else
  7266. {
  7267. UnicodeString ClipboardText;
  7268. if (NonEmptyTextFromClipboard(ClipboardText))
  7269. {
  7270. if (StoredSessions->IsUrl(ClipboardText))
  7271. {
  7272. Result = true;
  7273. }
  7274. else
  7275. {
  7276. Result = DirViewEnabled(osCurrent);
  7277. }
  7278. }
  7279. }
  7280. return Result;
  7281. }
  7282. //---------------------------------------------------------------------------
  7283. void __fastcall TCustomScpExplorerForm::PasteFromClipBoard()
  7284. {
  7285. if (CanPasteToDirViewFromClipBoard())
  7286. {
  7287. DirView(osCurrent)->PasteFromClipBoard();
  7288. }
  7289. else
  7290. {
  7291. UnicodeString ClipboardText;
  7292. if (NonEmptyTextFromClipboard(ClipboardText))
  7293. {
  7294. if (StoredSessions->IsUrl(ClipboardText))
  7295. {
  7296. NewSession(false, ClipboardText);
  7297. }
  7298. else
  7299. {
  7300. DirView(osCurrent)->Path = ClipboardText;
  7301. }
  7302. }
  7303. }
  7304. }
  7305. //---------------------------------------------------------------------------
  7306. void __fastcall TCustomScpExplorerForm::FileListFromClipboard()
  7307. {
  7308. // TBD
  7309. }
  7310. //---------------------------------------------------------------------------
  7311. void __fastcall TCustomScpExplorerForm::SelectAll(TOperationSide Side, TSelectMode Mode)
  7312. {
  7313. TCustomDirView * ADirView = DirView(Side);
  7314. ADirView->SelectAll(Mode);
  7315. ADirView->SetFocus();
  7316. }
  7317. //---------------------------------------------------------------------------
  7318. void __fastcall TCustomScpExplorerForm::SelectByMask(TOperationSide Side, bool Select)
  7319. {
  7320. TCustomDirView * ADirView = DirView(Side);
  7321. TFileFilter Filter;
  7322. DefaultFileFilter(Filter);
  7323. if (DoSelectMaskDialog(ADirView, Select, &Filter, Configuration))
  7324. {
  7325. Configuration->Usage->Inc(L"MaskSelections");
  7326. ADirView->SelectFiles(Filter, Select);
  7327. ADirView->SetFocus();
  7328. }
  7329. }
  7330. //---------------------------------------------------------------------------
  7331. void __fastcall TCustomScpExplorerForm::RestoreSelectedNames(TOperationSide Side)
  7332. {
  7333. TCustomDirView * ADirView = DirView(Side);
  7334. ADirView->RestoreSelectedNames();
  7335. ADirView->SetFocus();
  7336. }
  7337. //---------------------------------------------------------------------------
  7338. void __fastcall TCustomScpExplorerForm::SelectSameExt(bool Select)
  7339. {
  7340. TCustomDirView * CurrentDirView = DirView(osCurrent);
  7341. if (DebugAlwaysTrue(CurrentDirView->ItemFocused != NULL))
  7342. {
  7343. UnicodeString FileName = CurrentDirView->ItemFileName(CurrentDirView->ItemFocused);
  7344. UnicodeString Ext;
  7345. if (GetSide(osCurrent) == osRemote)
  7346. {
  7347. Ext = UnixExtractFileExt(FileName);
  7348. }
  7349. else
  7350. {
  7351. Ext = ExtractFileExt(FileName);
  7352. }
  7353. if (Ext.IsEmpty())
  7354. {
  7355. Ext = L".";
  7356. }
  7357. TFileFilter Filter;
  7358. Filter.Masks = FORMAT(L"*%s", (Ext));
  7359. Filter.Directories = false;
  7360. CurrentDirView->SelectFiles(Filter, Select);
  7361. }
  7362. }
  7363. //---------------------------------------------------------------------------
  7364. UnicodeString __fastcall TCustomScpExplorerForm::FileStatusBarText(
  7365. const TStatusFileInfo & FileInfo, TOperationSide Side)
  7366. {
  7367. UnicodeString Result;
  7368. if ((Side == osRemote) && (Terminal == NULL))
  7369. {
  7370. // noop
  7371. }
  7372. else
  7373. {
  7374. Result =
  7375. FMTLOAD(FILE_INFO_FORMAT,
  7376. (FormatBytes(FileInfo.SelectedSize),
  7377. FormatBytes(FileInfo.FilesSize),
  7378. FormatNumber(FileInfo.SelectedCount),
  7379. FormatNumber(FileInfo.FilesCount)));
  7380. }
  7381. return Result;
  7382. }
  7383. //---------------------------------------------------------------------------
  7384. void __fastcall TCustomScpExplorerForm::FileStatusBarPanelClick(
  7385. TTBXStatusPanel * Panel, TOperationSide Side)
  7386. {
  7387. if (Panel->Index == 1)
  7388. {
  7389. ToggleShowHiddenFiles();
  7390. }
  7391. else if (Panel->Index == 2)
  7392. {
  7393. Filter(Side);
  7394. }
  7395. }
  7396. //---------------------------------------------------------------------------
  7397. void __fastcall TCustomScpExplorerForm::UpdateFileStatusBar(
  7398. TTBXStatusBar * StatusBar, const TStatusFileInfo & FileInfo, TOperationSide Side)
  7399. {
  7400. DebugAssert(!StatusBar->SimplePanel);
  7401. StatusBar->Panels->Items[0]->Caption = FileStatusBarText(FileInfo, Side);
  7402. UpdateFileStatusExtendedPanels(StatusBar, FileInfo);
  7403. }
  7404. //---------------------------------------------------------------------------
  7405. void __fastcall TCustomScpExplorerForm::UpdateFileStatusExtendedPanels(
  7406. TTBXStatusBar * StatusBar, const TStatusFileInfo & FileInfo)
  7407. {
  7408. DebugAssert(StatusBar->Panels->Count >= 3);
  7409. TTBXStatusPanel * HiddenFilesPanel = StatusBar->Panels->Items[1];
  7410. if (FileInfo.HiddenCount > 0)
  7411. {
  7412. HiddenFilesPanel->Caption = FMTLOAD(FILE_INFO_HIDDEN2, (FormatNumber(FileInfo.HiddenCount)));
  7413. HiddenFilesPanel->ViewPriority = 90; // <100 allows hiding panel when it does not fit
  7414. }
  7415. else
  7416. {
  7417. HiddenFilesPanel->ViewPriority = 0;
  7418. // not really necessary, just to cleanup no-longer-valid data
  7419. HiddenFilesPanel->Caption = L"";
  7420. }
  7421. TTBXStatusPanel * FilteredFilesPanel = StatusBar->Panels->Items[2];
  7422. if (FileInfo.FilteredCount > 0)
  7423. {
  7424. FilteredFilesPanel->Caption = FMTLOAD(FILE_INFO_FILTERED2, (FormatNumber(FileInfo.FilteredCount)));
  7425. FilteredFilesPanel->ViewPriority = 90; // <100 allows hiding panel when it does not fit
  7426. }
  7427. else
  7428. {
  7429. FilteredFilesPanel->ViewPriority = 0;
  7430. // not really necessary, just to cleanup no-longer-valid data
  7431. FilteredFilesPanel->Caption = L"";
  7432. }
  7433. }
  7434. //---------------------------------------------------------------------------
  7435. void __fastcall TCustomScpExplorerForm::RemoteStatusBarClick(
  7436. TObject * /*Sender*/)
  7437. {
  7438. if (RemoteDirView->Enabled)
  7439. {
  7440. RemoteDirView->SetFocus();
  7441. }
  7442. }
  7443. //---------------------------------------------------------------------------
  7444. void __fastcall TCustomScpExplorerForm::ToggleQueueVisibility()
  7445. {
  7446. TQueueViewConfiguration Config = WinConfiguration->QueueView;
  7447. switch (Config.Show)
  7448. {
  7449. case qvShow:
  7450. if ((FQueueStatus != NULL) && (FQueueStatus->Count > 0))
  7451. {
  7452. Config.Show = qvHide;
  7453. }
  7454. else
  7455. {
  7456. Config.Show = Config.LastHideShow;
  7457. }
  7458. break;
  7459. case qvHideWhenEmpty:
  7460. if (ComponentVisible[fcQueueView])
  7461. {
  7462. Config.Show = qvHide;
  7463. }
  7464. else
  7465. {
  7466. Config.LastHideShow = Config.Show;
  7467. Config.Show = qvShow;
  7468. }
  7469. break;
  7470. case qvHide:
  7471. Config.LastHideShow = Config.Show;
  7472. Config.Show = qvShow;
  7473. break;
  7474. }
  7475. WinConfiguration->QueueView = Config;
  7476. }
  7477. //---------------------------------------------------------------------------
  7478. UnicodeString __fastcall TCustomScpExplorerForm::PathForCaption()
  7479. {
  7480. UnicodeString Result;
  7481. if (FTerminal != NULL)
  7482. {
  7483. switch (WinConfiguration->PathInCaption)
  7484. {
  7485. case picShort:
  7486. Result = TTerminalManager::Instance()->GetTerminalShortPath(FTerminal);
  7487. break;
  7488. case picFull:
  7489. Result = FTerminal->CurrentDirectory;
  7490. break;
  7491. }
  7492. }
  7493. return Result;
  7494. }
  7495. //---------------------------------------------------------------------------
  7496. void __fastcall TCustomScpExplorerForm::UpdateControls()
  7497. {
  7498. TTerminalManager::Instance()->UpdateAppTitle();
  7499. // WORAKRDOUND: Disabling list view when it is not showing yet does not set its
  7500. // background to gray on Windows 7 (works on Windows 10).
  7501. // See also EnableControl
  7502. if (Showing)
  7503. {
  7504. bool HasTerminal = (Terminal != NULL);
  7505. if (HasTerminal)
  7506. {
  7507. // Update path when it changes
  7508. if ((SessionsPageControl->ActivePage != NULL) && (GetSessionTabTerminal(SessionsPageControl->ActivePage) == Terminal))
  7509. {
  7510. UpdateSessionTab(SessionsPageControl->ActivePage);
  7511. }
  7512. if (!RemoteDirView->Enabled)
  7513. {
  7514. RemoteDirView->Enabled = true;
  7515. if (FRemoteDirViewWasFocused)
  7516. {
  7517. ActiveControl = RemoteDirView;
  7518. }
  7519. }
  7520. RemoteDriveView->Enabled = true;
  7521. RemoteDirView->Color = (FSessionColor != 0 ? FSessionColor : clWindow);
  7522. RemoteDriveView->Color = RemoteDirView->Color;
  7523. }
  7524. else
  7525. {
  7526. if (RemoteDirView->Enabled)
  7527. {
  7528. // This is first called when the form is being constructed
  7529. // (not anymore due to Showing test above)
  7530. // but the false is overriden in the constructor later.
  7531. // An even later in TScpCommanderForm::DoShow()
  7532. FRemoteDirViewWasFocused = (ActiveControl == RemoteDirView);
  7533. EnableControl(RemoteDirView, false);
  7534. }
  7535. EnableControl(RemoteDriveView, false);
  7536. }
  7537. EnableControl(QueueView3, HasTerminal && Terminal->IsCapable[fsBackgroundTransfers]);
  7538. QueueLabelUpdateStatus();
  7539. reinterpret_cast<TTBCustomItem *>(GetComponent(fcRemotePathComboBox))->Enabled = HasTerminal;
  7540. }
  7541. }
  7542. //---------------------------------------------------------------------------
  7543. void __fastcall TCustomScpExplorerForm::DoDirViewLoaded(TCustomDirView * /*Sender*/)
  7544. {
  7545. UpdateControls();
  7546. }
  7547. //---------------------------------------------------------------------------
  7548. void __fastcall TCustomScpExplorerForm::DirViewLoaded(
  7549. TObject * Sender)
  7550. {
  7551. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  7552. DebugAssert(DirView != NULL);
  7553. DoDirViewLoaded(DirView);
  7554. TransferPresetAutoSelect();
  7555. }
  7556. //---------------------------------------------------------------------------
  7557. void __fastcall TCustomScpExplorerForm::StartUpdates()
  7558. {
  7559. TUpdatesConfiguration Updates = WinConfiguration->Updates;
  7560. // first run after installation
  7561. if (double(Updates.LastCheck) == 0)
  7562. {
  7563. // make sure next time there will be an update (if enabled)
  7564. Updates.LastCheck = TDateTime(1);
  7565. WinConfiguration->Updates = Updates;
  7566. }
  7567. else if ((double(Updates.Period) > 0) &&
  7568. (Now() - Updates.LastCheck >= Updates.Period))
  7569. {
  7570. TThreadMethod OnUpdatesChecked = NULL;
  7571. if (!IsUWP())
  7572. {
  7573. OnUpdatesChecked = UpdatesChecked;
  7574. }
  7575. StartUpdateThread(OnUpdatesChecked);
  7576. }
  7577. }
  7578. //---------------------------------------------------------------------------
  7579. void __fastcall TCustomScpExplorerForm::UpdatesChecked()
  7580. {
  7581. UnicodeString Message;
  7582. bool New;
  7583. TQueryType Type;
  7584. GetUpdatesMessage(Message, New, Type, false);
  7585. if (!Message.IsEmpty())
  7586. {
  7587. if (New)
  7588. {
  7589. Message = FMTLOAD(NEW_VERSION_CLICK, (Message));
  7590. }
  7591. if (!New && (Type != qtWarning))
  7592. {
  7593. PostNote(UnformatMessage(Message), 0, UpdatesNoteClicked, NULL);
  7594. }
  7595. else
  7596. {
  7597. Notify(NULL, Message, Type, true, UpdatesNoteClicked);
  7598. }
  7599. if (New)
  7600. {
  7601. Configuration->Usage->Inc(L"UpdateNotifications");
  7602. }
  7603. }
  7604. }
  7605. //---------------------------------------------------------------------------
  7606. void __fastcall TCustomScpExplorerForm::UpdatesNoteClicked(TObject * /*Sender*/)
  7607. {
  7608. RestoreApp();
  7609. bool CanDisplay = !NonVisualDataModule->Busy;
  7610. if (!CanDisplay && (Screen->ActiveForm != NULL))
  7611. {
  7612. CanDisplay = (Screen->ActiveForm->Perform(WM_CAN_DISPLAY_UPDATES, 0, 0) != 0);
  7613. }
  7614. if (CanDisplay)
  7615. {
  7616. Configuration->Usage->Inc(L"UpdateNotificationsClicked");
  7617. CheckForUpdates(true);
  7618. }
  7619. else
  7620. {
  7621. Configuration->Usage->Inc(L"UpdateNotificationsBusy");
  7622. }
  7623. }
  7624. //---------------------------------------------------------------------------
  7625. void __fastcall TCustomScpExplorerForm::GetTransferPresetAutoSelectData(
  7626. TCopyParamRuleData & Data)
  7627. {
  7628. DebugAssert(Terminal != NULL);
  7629. Data.HostName = Terminal->SessionData->HostNameExpanded;
  7630. Data.UserName = Terminal->SessionData->UserNameExpanded;
  7631. Data.RemoteDirectory = RemoteDirView->PathName;
  7632. }
  7633. //---------------------------------------------------------------------------
  7634. void __fastcall TCustomScpExplorerForm::TransferPresetAutoSelect()
  7635. {
  7636. // Terminal can be null when we are changing local directory implicitly,
  7637. // because it has been deleted, while no session is connected
  7638. // (Login dialog is open)
  7639. if (FAllowTransferPresetAutoSelect && (Terminal != NULL))
  7640. {
  7641. TCopyParamRuleData Data;
  7642. GetTransferPresetAutoSelectData(Data);
  7643. int CopyParamIndex = GUIConfiguration->CopyParamList->Find(Data);
  7644. UnicodeString CopyParamCurrent = GUIConfiguration->CopyParamCurrent;
  7645. if (CopyParamIndex < 0)
  7646. {
  7647. // there is no preset that matches autoselection
  7648. // set preset that we consider "default"
  7649. FCopyParamAutoSelected = L""; // forget last autoselected preset
  7650. GUIConfiguration->CopyParamCurrent = FCopyParamDefault;
  7651. }
  7652. else
  7653. {
  7654. // there is preset matching autoselection
  7655. UnicodeString CopyParamName = GUIConfiguration->CopyParamList->Names[CopyParamIndex];
  7656. if (CopyParamName == FCopyParamAutoSelected)
  7657. {
  7658. // autoselected the same preset as the last time
  7659. // make no change (i.e. preserve custom user preset, if any)
  7660. }
  7661. else
  7662. {
  7663. // autoselected the different preset then the last time (or there
  7664. // was default preset set)
  7665. FCopyParamAutoSelected = CopyParamName; // remember autoselection
  7666. GUIConfiguration->CopyParamCurrent = CopyParamName;
  7667. }
  7668. }
  7669. if (GUIConfiguration->CopyParamCurrent != CopyParamCurrent)
  7670. {
  7671. if (CopyParamIndex >= 0)
  7672. {
  7673. Configuration->Usage->Inc(L"CopyParamAutoSelects");
  7674. }
  7675. TTransferPresetNoteData * Data = new TTransferPresetNoteData;
  7676. try
  7677. {
  7678. int Fmt =
  7679. (CopyParamIndex < 0) ?
  7680. (GUIConfiguration->CopyParamIndex < 0 ? COPY_PARAM_DEFAULT_NORM : COPY_PARAM_DEFAULT_CUSTOM) :
  7681. COPY_PARAM_AUTOSELECTED;
  7682. UnicodeString Message = FMTLOAD(Fmt, (StripHotkey(GUIConfiguration->CopyParamCurrent)));
  7683. Data->Message = MainInstructions(Message);
  7684. int CopyParamAttrs = Terminal->UsableCopyParamAttrs(0).General;
  7685. UnicodeString Info = GUIConfiguration->CurrentCopyParam.GetInfoStr(L"\n",
  7686. CopyParamAttrs);
  7687. if (CopyParamIndex >= 0)
  7688. {
  7689. DebugAssert(GUIConfiguration->CopyParamList->Rules[CopyParamIndex] != NULL);
  7690. Info = FORMAT(L"%s\n \n%s", (Info,
  7691. FMTLOAD(COPY_PARAM_RULE,
  7692. (GUIConfiguration->CopyParamList->Rules[CopyParamIndex]->GetInfoStr(L"\n")))));
  7693. }
  7694. Data->Message += L"\n\n" + Info;
  7695. if (WinConfiguration->CopyParamAutoSelectNotice)
  7696. {
  7697. TransferPresetNoteMessage(Data, true);
  7698. }
  7699. else
  7700. {
  7701. PostNote(Message, 0, TransferPresetNoteClicked, Data);
  7702. Data = NULL; // ownership passed
  7703. }
  7704. }
  7705. __finally
  7706. {
  7707. delete Data;
  7708. }
  7709. }
  7710. }
  7711. }
  7712. //---------------------------------------------------------------------------
  7713. void __fastcall TCustomScpExplorerForm::TransferPresetNoteMessage(
  7714. TTransferPresetNoteData * NoteData, bool AllowNeverAskAgain)
  7715. {
  7716. DebugAssert(NoteData != NULL);
  7717. TMessageParams Params(AllowNeverAskAgain ? mpNeverAskAgainCheck : 0);
  7718. TQueryButtonAlias Aliases[1];
  7719. Aliases[0].Button = qaIgnore; // "ignore" is after "ok"
  7720. Aliases[0].Alias = LoadStr(CONFIGURE_BUTTON);
  7721. Params.Aliases = Aliases;
  7722. Params.AliasesCount = LENOF(Aliases);
  7723. unsigned int Result =
  7724. MoreMessageDialog(NoteData->Message, NULL, qtInformation,
  7725. qaOK | qaIgnore, HELP_COPY_PARAM_AUTOSELECTED, &Params);
  7726. switch (Result)
  7727. {
  7728. case qaNeverAskAgain:
  7729. DebugAssert(AllowNeverAskAgain);
  7730. WinConfiguration->CopyParamAutoSelectNotice = false;
  7731. break;
  7732. case qaIgnore:
  7733. PreferencesDialog(pmPresets);
  7734. break;
  7735. }
  7736. }
  7737. //---------------------------------------------------------------------------
  7738. void __fastcall TCustomScpExplorerForm::TransferPresetNoteClicked(TObject * Sender)
  7739. {
  7740. // as of now useless, as this is used for notes only, never for balloons, ...
  7741. RestoreApp();
  7742. // .. and we should never be busy here
  7743. if (DebugAlwaysTrue(!NonVisualDataModule->Busy))
  7744. {
  7745. TransferPresetNoteMessage(DebugNotNull(dynamic_cast<TTransferPresetNoteData *>(Sender)), false);
  7746. }
  7747. }
  7748. //---------------------------------------------------------------------------
  7749. void __fastcall TCustomScpExplorerForm::PreferencesDialog(
  7750. TPreferencesMode APreferencesMode)
  7751. {
  7752. std::unique_ptr<TPreferencesDialogData> PreferencesData;
  7753. TCopyParamRuleData Data;
  7754. if (Terminal != NULL)
  7755. {
  7756. PreferencesData.reset(new TPreferencesDialogData());
  7757. GetTransferPresetAutoSelectData(Data);
  7758. PreferencesData->CopyParamRuleData = &Data;
  7759. }
  7760. DoPreferencesDialog(APreferencesMode, PreferencesData.get());
  7761. }
  7762. //---------------------------------------------------------------------------
  7763. void __fastcall TCustomScpExplorerForm::AdHocCustomCommandValidate(
  7764. const TCustomCommandType & Command)
  7765. {
  7766. if (CustomCommandState(Command, FEditingFocusedAdHocCommand, ccltAll) <= 0)
  7767. {
  7768. throw Exception(FMTLOAD(CUSTOM_COMMAND_IMPOSSIBLE, (Command.Command)));
  7769. }
  7770. }
  7771. //---------------------------------------------------------------------------
  7772. void __fastcall TCustomScpExplorerForm::AdHocCustomCommand(bool OnFocused)
  7773. {
  7774. bool RemoteAllowed = CustomCommandRemoteAllowed();
  7775. TCustomCommandType Command;
  7776. // make sure we use local custom command when remote are not supported
  7777. if (RemoteAllowed || FLAGSET(FLastCustomCommand.Params, ccLocal))
  7778. {
  7779. Command = FLastCustomCommand;
  7780. }
  7781. else
  7782. {
  7783. Command.Params = Command.Params | ccLocal;
  7784. }
  7785. Command.Name = LoadStr(CUSTOM_COMMAND_AD_HOC_NAME);
  7786. FEditingFocusedAdHocCommand = OnFocused;
  7787. bool LocalSide = (FCurrentSide == osLocal);
  7788. int Options =
  7789. FLAGMASK((!RemoteAllowed || LocalSide), ccoDisableRemote) |
  7790. FLAGMASK(LocalSide, ccoDisableRemoteFiles);
  7791. if (DoCustomCommandDialog(Command, WinConfiguration->CustomCommandList,
  7792. ccmAdHoc, Options, AdHocCustomCommandValidate, NULL))
  7793. {
  7794. FLastCustomCommand = Command;
  7795. UpdateCustomCommandsToolbar();
  7796. ExecuteFileOperation(foCustomCommand, osRemote, OnFocused, false, &Command);
  7797. }
  7798. }
  7799. //---------------------------------------------------------------------------
  7800. void __fastcall TCustomScpExplorerForm::LastCustomCommand(bool OnFocused)
  7801. {
  7802. DebugAssert(!FLastCustomCommand.Command.IsEmpty());
  7803. int State = CustomCommandState(FLastCustomCommand, OnFocused, ccltAll);
  7804. DebugAssert(State > 0);
  7805. if (State <= 0)
  7806. {
  7807. throw Exception(FMTLOAD(CUSTOM_COMMAND_IMPOSSIBLE, (FLastCustomCommand.Command)));
  7808. }
  7809. ExecuteFileOperation(foCustomCommand, osRemote, OnFocused, false, &FLastCustomCommand);
  7810. }
  7811. //---------------------------------------------------------------------------
  7812. bool __fastcall TCustomScpExplorerForm::GetLastCustomCommand(bool OnFocused,
  7813. TCustomCommandType & Command, int & State)
  7814. {
  7815. bool Result = !FLastCustomCommand.Command.IsEmpty();
  7816. if (Result)
  7817. {
  7818. Command = FLastCustomCommand;
  7819. State = CustomCommandState(FLastCustomCommand, OnFocused, ccltAll);
  7820. }
  7821. return Result;
  7822. }
  7823. //---------------------------------------------------------------------------
  7824. void __fastcall TCustomScpExplorerForm::BeforeAction()
  7825. {
  7826. if (RemoteDirView->ItemFocused != NULL)
  7827. {
  7828. RemoteDirView->ItemFocused->CancelEdit();
  7829. }
  7830. }
  7831. //---------------------------------------------------------------------------
  7832. void __fastcall TCustomScpExplorerForm::PostComponentHide(Byte Component)
  7833. {
  7834. DebugAssert(ComponentVisible[Component]);
  7835. PostMessage(Handle, WM_COMPONENT_HIDE, Component, 0);
  7836. }
  7837. //---------------------------------------------------------------------------
  7838. void __fastcall TCustomScpExplorerForm::QueueSplitterDblClick(TObject * /*Sender*/)
  7839. {
  7840. // when queue panel is resized here directly, the status bar is stretched
  7841. // over whole space the panel occupied
  7842. PostComponentHide(fcQueueView);
  7843. }
  7844. //---------------------------------------------------------------------------
  7845. void __fastcall TCustomScpExplorerForm::Dispatch(void * Message)
  7846. {
  7847. TMessage * M = static_cast<TMessage*>(Message);
  7848. switch (M->Msg)
  7849. {
  7850. case CM_DIALOGCHAR:
  7851. CMDialogChar(*M);
  7852. break;
  7853. case WM_APPCOMMAND:
  7854. WMAppCommand(*M);
  7855. break;
  7856. case WM_SYSCOMMAND:
  7857. WMSysCommand(*M);
  7858. break;
  7859. case WM_QUERYENDSESSION:
  7860. WMQueryEndSession(*M);
  7861. break;
  7862. case WM_ENDSESSION:
  7863. WMEndSession(*reinterpret_cast<TWMEndSession *>(M));
  7864. break;
  7865. #ifdef _DEBUG
  7866. case WM_WINDOWPOSCHANGED:
  7867. WMWindowPosChanged(*reinterpret_cast<TWMWindowPosMsg *>(M));
  7868. break;
  7869. case CM_CUSTOMSTYLECHANGED:
  7870. TForm::Dispatch(Message);
  7871. break;
  7872. #endif
  7873. case WM_COMPONENT_HIDE:
  7874. {
  7875. Byte Component = static_cast<Byte>(M->WParam);
  7876. // sanity check
  7877. if (ComponentVisible[Component])
  7878. {
  7879. // special treatment
  7880. if (Component == fcQueueView)
  7881. {
  7882. ToggleQueueVisibility();
  7883. DebugAssert(!ComponentVisible[fcQueueView]);
  7884. }
  7885. else
  7886. {
  7887. ComponentVisible[Component] = false;
  7888. }
  7889. }
  7890. }
  7891. break;
  7892. case WM_COPYDATA:
  7893. WMCopyData(*M);
  7894. break;
  7895. case WM_MANAGES_CAPTION:
  7896. // caption managed in TTerminalManager::UpdateAppTitle()
  7897. M->Result = 1;
  7898. break;
  7899. case WM_WANTS_MOUSEWHEEL:
  7900. M->Result = 1;
  7901. break;
  7902. case CM_SHOWINGCHANGED:
  7903. CMShowingChanged(*M);
  7904. break;
  7905. case WM_CLOSE:
  7906. WMClose(*M);
  7907. break;
  7908. case WM_DPICHANGED:
  7909. WMDpiChanged(*M);
  7910. break;
  7911. case CM_DPICHANGED:
  7912. CMDpiChanged(*M);
  7913. break;
  7914. default:
  7915. TForm::Dispatch(Message);
  7916. break;
  7917. }
  7918. }
  7919. //---------------------------------------------------------------------------
  7920. void __fastcall TCustomScpExplorerForm::UpdateImages()
  7921. {
  7922. // noop
  7923. }
  7924. //---------------------------------------------------------------------------
  7925. void __fastcall TCustomScpExplorerForm::CMDpiChanged(TMessage & Message)
  7926. {
  7927. TForm::Dispatch(&Message);
  7928. // regenerate session images
  7929. DoTerminalListChanged(true);
  7930. UpdateImages();
  7931. }
  7932. //---------------------------------------------------------------------------
  7933. void __fastcall TCustomScpExplorerForm::WMDpiChanged(TMessage & Message)
  7934. {
  7935. TForm::Dispatch(&Message);
  7936. Configuration->Usage->Inc(L"PixelsPerInchChanged");
  7937. UpdatePixelsPerInchMainWindowCounter();
  7938. }
  7939. //---------------------------------------------------------------------------
  7940. void __fastcall TCustomScpExplorerForm::WMClose(TMessage & Message)
  7941. {
  7942. // Cannot close window while we are busy.
  7943. // We cannot test this in FormCloseQuery as that is called also from
  7944. // Close(), which is called by CloseApplicationAction. So we can be busy
  7945. // there already even, when it is legitimate to close the application.
  7946. // Possibly a better place to handle this would be WMSysCommand.
  7947. if (NonVisualDataModule->Busy)
  7948. {
  7949. Message.Result = 1;
  7950. }
  7951. else
  7952. {
  7953. TForm::Dispatch(&Message);
  7954. }
  7955. }
  7956. //---------------------------------------------------------------------------
  7957. void __fastcall TCustomScpExplorerForm::CMShowingChanged(TMessage & Message)
  7958. {
  7959. TForm::Dispatch(&Message);
  7960. if (Showing && (Terminal == NULL))
  7961. {
  7962. // When we are starting minimized (i.e. from an installer),
  7963. // postpone showing Login dialog until we get restored.
  7964. // Otherwise the Login dialog (and Authentication window) show restored
  7965. // over invidible (minimized) main window.
  7966. if (WindowState == wsMinimized)
  7967. {
  7968. FNeedSession = true;
  7969. }
  7970. else
  7971. {
  7972. // This happens before application ever goes idle, so the toolbars would
  7973. // stay enabled (initial state) until the Login dialog is dismissed.
  7974. UpdateActions();
  7975. NonVisualDataModule->StartBusy();
  7976. try
  7977. {
  7978. // Need to process WM_ACTIVATEAPP before showing the Login dialog,
  7979. // otherwise the dialog does not receive focus.
  7980. // With Commander interface the ProcessMessages is called already
  7981. // by TDriveView, but with Explorer interface, we need to call it explicily
  7982. Application->ProcessMessages();
  7983. // do not reload sessions, they have been loaded just now (optimization)
  7984. NeedSession(false);
  7985. }
  7986. __finally
  7987. {
  7988. NonVisualDataModule->EndBusy();
  7989. }
  7990. }
  7991. }
  7992. }
  7993. //---------------------------------------------------------------------------
  7994. void __fastcall TCustomScpExplorerForm::FormConstrainedResize(
  7995. TObject * /*Sender*/, int & MinWidth, int & MinHeight, int & MaxWidth,
  7996. int & MaxHeight)
  7997. {
  7998. // workaround for bug in TWinControl.CalcConstraints
  7999. // Check for empty rect (restore from iconic state) is done there only after
  8000. // call to AdjustClientRect, which enlarges the rect (for forms).
  8001. TRect R = GetClientRect();
  8002. // when restoring from iconic state, place no restrictions
  8003. if (IsRectEmpty(R))
  8004. {
  8005. MinWidth = 0;
  8006. MinHeight = 0;
  8007. MaxWidth = 32000;
  8008. MaxHeight = 32000;
  8009. }
  8010. }
  8011. //---------------------------------------------------------------------------
  8012. void __fastcall TCustomScpExplorerForm::GetSpaceAvailable(const UnicodeString Path,
  8013. TSpaceAvailable & ASpaceAvailable, bool & Close)
  8014. {
  8015. // terminal can be already closed (e.g. dropped connection)
  8016. if ((Terminal != NULL) && Terminal->IsCapable[fcCheckingSpaceAvailable])
  8017. {
  8018. Configuration->Usage->Inc(L"SpaceAvailableChecks");
  8019. try
  8020. {
  8021. Terminal->SpaceAvailable(Path, ASpaceAvailable);
  8022. }
  8023. catch(...)
  8024. {
  8025. if (!Terminal->Active)
  8026. {
  8027. Close = true;
  8028. }
  8029. throw;
  8030. }
  8031. }
  8032. }
  8033. //---------------------------------------------------------------------------
  8034. void __fastcall TCustomScpExplorerForm::FileSystemInfo()
  8035. {
  8036. const TSessionInfo & SessionInfo = Terminal->GetSessionInfo();
  8037. const TFileSystemInfo & FileSystemInfo = Terminal->GetFileSystemInfo(true);
  8038. TGetSpaceAvailable OnGetSpaceAvailable = NULL;
  8039. if (Terminal->IsCapable[fcCheckingSpaceAvailable])
  8040. {
  8041. OnGetSpaceAvailable = GetSpaceAvailable;
  8042. }
  8043. DoFileSystemInfoDialog(SessionInfo, FileSystemInfo, Terminal->CurrentDirectory,
  8044. OnGetSpaceAvailable);
  8045. }
  8046. //---------------------------------------------------------------------------
  8047. TSessionData * __fastcall TCustomScpExplorerForm::SessionDataForCode()
  8048. {
  8049. std::unique_ptr<TSessionData> Data(CloneCurrentSessionData());
  8050. Terminal->FillSessionDataForCode(Data.get());
  8051. return Data.release();
  8052. }
  8053. //---------------------------------------------------------------------------
  8054. void __fastcall TCustomScpExplorerForm::GenerateUrl(TStrings * Paths)
  8055. {
  8056. std::unique_ptr<TSessionData> Data(SessionDataForCode());
  8057. DoGenerateUrlDialog(Data.get(), Paths);
  8058. }
  8059. //---------------------------------------------------------------------------
  8060. void __fastcall TCustomScpExplorerForm::SessionGenerateUrl()
  8061. {
  8062. GenerateUrl(NULL);
  8063. }
  8064. //---------------------------------------------------------------------------
  8065. void __fastcall TCustomScpExplorerForm::FileGenerateUrl()
  8066. {
  8067. std::unique_ptr<TStrings> Paths(new TStringList());
  8068. DirView(osCurrent)->CreateFileList(false, true, Paths.get());
  8069. GenerateUrl(Paths.get());
  8070. }
  8071. //---------------------------------------------------------------------------
  8072. void __fastcall TCustomScpExplorerForm::SetSessionColor(TColor value)
  8073. {
  8074. if (value != FSessionColor)
  8075. {
  8076. FSessionColor = value;
  8077. TColor C = (value != 0 ? value : Vcl::Graphics::clNone);
  8078. TTBXColorItem * ColorItem = dynamic_cast<TTBXColorItem *>(
  8079. static_cast<TObject *>(GetComponent(fcColorMenu)));
  8080. DebugAssert(ColorItem != NULL);
  8081. ColorItem->Color = C;
  8082. NonVisualDataModule->ColorMenuItem->Color = C;
  8083. // Is null when called from LastTerminalClosed
  8084. if (Terminal != NULL)
  8085. {
  8086. SessionsPageControl->ActivePage->ImageIndex = AddSessionColor(value);
  8087. }
  8088. UpdateControls();
  8089. }
  8090. }
  8091. //---------------------------------------------------------------------------
  8092. bool __fastcall TCustomScpExplorerForm::CancelNote(bool Force)
  8093. {
  8094. bool Result = FNoteTimer->Enabled;
  8095. if (Result)
  8096. {
  8097. // cannot cancel note too early
  8098. bool NotEarly =
  8099. (Now() - FNoteShown >
  8100. EncodeTimeVerbose(0, 0, (unsigned short)(WinConfiguration->NotificationsStickTime), 0));
  8101. if (Force || NotEarly)
  8102. {
  8103. FNoteTimer->Enabled = false;
  8104. FNote = L"";
  8105. SAFE_DESTROY(FNoteData);
  8106. FOnNoteClick = NULL;
  8107. FNoteHints = FNotes->Text;
  8108. FNoteHints.Delete(FNoteHints.Length() - 1, 2);
  8109. UpdateStatusBar();
  8110. }
  8111. }
  8112. return Result;
  8113. }
  8114. //---------------------------------------------------------------------------
  8115. void __fastcall TCustomScpExplorerForm::NoteTimer(TObject * /*Sender*/)
  8116. {
  8117. DebugAssert(FNoteTimer->Enabled);
  8118. CancelNote(true);
  8119. }
  8120. //---------------------------------------------------------------------------
  8121. void __fastcall TCustomScpExplorerForm::AddNote(UnicodeString Note, bool UpdateNow)
  8122. {
  8123. int P = Note.Pos(L"\n");
  8124. if (P > 0)
  8125. {
  8126. Note.SetLength(P - 1);
  8127. }
  8128. FNotes->Add(FORMAT(L"[%s] %s",
  8129. (FormatDateTime(Configuration->TimeFormat, Now()), Note)));
  8130. while (FNotes->Count > 10)
  8131. {
  8132. FNotes->Delete(0);
  8133. }
  8134. if (UpdateNow)
  8135. {
  8136. FNoteHints = FNotes->Text;
  8137. FNoteHints.Delete(FNoteHints.Length() - 1, 2);
  8138. UpdateStatusBar();
  8139. }
  8140. }
  8141. //---------------------------------------------------------------------------
  8142. void __fastcall TCustomScpExplorerForm::PostNote(UnicodeString Note,
  8143. unsigned int Seconds, TNotifyEvent OnNoteClick, TObject * NoteData)
  8144. {
  8145. int P = Note.Pos(L"\n");
  8146. if (P > 0)
  8147. {
  8148. Note.SetLength(P - 1);
  8149. }
  8150. FNoteHints = FNotes->Text;
  8151. FNoteHints.Delete(FNoteHints.Length() - 1, 2);
  8152. FNote = Note;
  8153. SAFE_DESTROY(FNoteData);
  8154. FNoteData = NoteData;
  8155. FOnNoteClick = OnNoteClick;
  8156. AddNote(Note, false);
  8157. UpdateStatusBar();
  8158. FNoteShown = Now();
  8159. FNoteTimer->Enabled = false;
  8160. if (Seconds == 0)
  8161. {
  8162. Seconds = WinConfiguration->NotificationsTimeout;
  8163. }
  8164. FNoteTimer->Interval = Seconds * MSecsPerSec;
  8165. FNoteTimer->Enabled = true;
  8166. }
  8167. //---------------------------------------------------------------------------
  8168. void __fastcall TCustomScpExplorerForm::ReadDirectoryCancelled()
  8169. {
  8170. PostNote(LoadStr(DIRECTORY_READING_CANCELLED), 0, NULL, NULL);
  8171. }
  8172. //---------------------------------------------------------------------------
  8173. void __fastcall TCustomScpExplorerForm::SynchronizeBrowsingChanged()
  8174. {
  8175. if (NonVisualDataModule->SynchronizeBrowsingAction->Checked)
  8176. {
  8177. Configuration->Usage->Inc(L"SynchronizeBrowsingEnabled");
  8178. }
  8179. PostNote(FORMAT(LoadStrPart(SYNC_DIR_BROWSE_TOGGLE, 1),
  8180. (LoadStrPart(SYNC_DIR_BROWSE_TOGGLE,
  8181. (NonVisualDataModule->SynchronizeBrowsingAction->Checked ? 2 : 3)))),
  8182. 0, NULL, NULL);
  8183. }
  8184. //---------------------------------------------------------------------------
  8185. void __fastcall TCustomScpExplorerForm::ToggleShowHiddenFiles()
  8186. {
  8187. WinConfiguration->ShowHiddenFiles = !WinConfiguration->ShowHiddenFiles;
  8188. PostNote(FORMAT(LoadStrPart(SHOW_HIDDEN_FILES_TOGGLE, 1),
  8189. (LoadStrPart(SHOW_HIDDEN_FILES_TOGGLE,
  8190. (WinConfiguration->ShowHiddenFiles ? 2 : 3)))), 0, NULL, NULL);
  8191. }
  8192. //---------------------------------------------------------------------------
  8193. void __fastcall TCustomScpExplorerForm::SetFormatSizeBytes(TFormatBytesStyle Style)
  8194. {
  8195. WinConfiguration->FormatSizeBytes = Style;
  8196. }
  8197. //---------------------------------------------------------------------------
  8198. void __fastcall TCustomScpExplorerForm::ToggleAutoReadDirectoryAfterOp()
  8199. {
  8200. Configuration->AutoReadDirectoryAfterOp = !Configuration->AutoReadDirectoryAfterOp;
  8201. PostNote(FORMAT(LoadStrPart(AUTO_READ_DIRECTORY_TOGGLE, 1),
  8202. (LoadStrPart(AUTO_READ_DIRECTORY_TOGGLE,
  8203. (Configuration->AutoReadDirectoryAfterOp ? 2 : 3)))), 0, NULL, NULL);
  8204. }
  8205. //---------------------------------------------------------------------------
  8206. void __fastcall TCustomScpExplorerForm::StatusBarPanelDblClick(
  8207. TTBXCustomStatusBar * Sender, TTBXStatusPanel * Panel)
  8208. {
  8209. if (Panel->Index == 0)
  8210. {
  8211. if (FOnNoteClick != NULL)
  8212. {
  8213. // prevent the user data from being freed by possible call
  8214. // to CancelNote or PostNote during call to OnNoteClick
  8215. std::unique_ptr<TObject> NoteData(FNoteData);
  8216. TNotifyEvent OnNoteClick = FOnNoteClick;
  8217. FNoteData = NULL;
  8218. // need to cancel the note as we are going to delete its user data
  8219. CancelNote(true);
  8220. OnNoteClick(NoteData.get());
  8221. }
  8222. }
  8223. if (Panel->Index >= Sender->Panels->Count - SessionPanelCount)
  8224. {
  8225. FileSystemInfo();
  8226. }
  8227. }
  8228. //---------------------------------------------------------------------------
  8229. void __fastcall TCustomScpExplorerForm::LockWindow(bool Force)
  8230. {
  8231. // workaround:
  8232. // 1) for unknown reason, disabling window, while minimized,
  8233. // prevents it from restoring, even if it was enabled again meanwhile
  8234. // 2) when disabling the main window, while another has focus
  8235. // minimize is no longer possible
  8236. // ("keep up to date" dialog - though does not seem to be the case with "find file" -
  8237. // so it has probably something to do with the semi-modalness of the "keep up to date" dialog)
  8238. // Shouldn't we use IsApplicationMinimized() here?
  8239. if ((FLockSuspendLevel == 0) && !IsIconic(Application->Handle) && (Force || (Screen->ActiveForm == this)))
  8240. {
  8241. Enabled = false;
  8242. }
  8243. FLockLevel++;
  8244. }
  8245. //---------------------------------------------------------------------------
  8246. void __fastcall TCustomScpExplorerForm::UnlockWindow()
  8247. {
  8248. DebugAssert(FLockLevel > 0);
  8249. FLockLevel--;
  8250. if (FLockLevel == 0)
  8251. {
  8252. DebugAssert(FLockSuspendLevel == 0);
  8253. Enabled = true;
  8254. }
  8255. }
  8256. //---------------------------------------------------------------------------
  8257. void __fastcall TCustomScpExplorerForm::SuspendWindowLock()
  8258. {
  8259. // We need to make sure that window is enabled when the last modal window closes
  8260. // otherwise focus is not restored correctly.
  8261. // So we re-enable the window when modal window opens and
  8262. // disable it back after is closes
  8263. if (FLockLevel > 0)
  8264. {
  8265. // while we have nesting counter, we know that we never be called
  8266. // recursivelly as Application->OnModalBegin is called only
  8267. // for the top-level modal window
  8268. if (DebugAlwaysTrue(FLockSuspendLevel == 0))
  8269. {
  8270. // won't be disabled when conditions in LockWindow() were not satisfied
  8271. FDisabledOnLockSuspend = !Enabled;
  8272. // When minimized to tray (or actually when set to SW_HIDE),
  8273. // setting Enabled makes the window focusable even when there's
  8274. // modal window over it
  8275. if (!FTrayIcon->Visible)
  8276. {
  8277. Enabled = true;
  8278. }
  8279. }
  8280. FLockSuspendLevel++;
  8281. }
  8282. }
  8283. //---------------------------------------------------------------------------
  8284. void __fastcall TCustomScpExplorerForm::ResumeWindowLock()
  8285. {
  8286. if (FLockSuspendLevel > 0)
  8287. {
  8288. DebugAssert(FLockLevel > 0);
  8289. FLockSuspendLevel--;
  8290. // see comment in SuspendWindowLock
  8291. if (DebugAlwaysTrue(FLockSuspendLevel == 0))
  8292. {
  8293. // Note that window can be enabled here, when we were minized to tray when
  8294. // was SuspendWindowLock() called.
  8295. // We should possibly do the same check as in LockWindow(),
  8296. // if it is ever possible that the consitions change between
  8297. // SuspendWindowLock() and ResumeWindowLock()
  8298. Enabled = !FDisabledOnLockSuspend;
  8299. }
  8300. }
  8301. }
  8302. //---------------------------------------------------------------------------
  8303. void __fastcall TCustomScpExplorerForm::UpdateRemotePathComboBox(bool TextOnly)
  8304. {
  8305. if (!TextOnly)
  8306. {
  8307. TTBXComboBoxItem * RemotePathComboBox =
  8308. reinterpret_cast<TTBXComboBoxItem *>(GetComponent(fcRemotePathComboBox));
  8309. TStrings * Items = RemotePathComboBox->Strings;
  8310. Items->BeginUpdate();
  8311. try
  8312. {
  8313. Items->Clear();
  8314. if (Terminal != NULL)
  8315. {
  8316. UnicodeString APath = UnixExcludeTrailingBackslash(RemoteDirView->Path);
  8317. while (!IsUnixRootPath(APath))
  8318. {
  8319. int P = APath.LastDelimiter(L'/');
  8320. DebugAssert(P >= 0);
  8321. Items->Insert(0, APath.SubString(P + 1, APath.Length() - P));
  8322. APath.SetLength(P - 1);
  8323. }
  8324. Items->Insert(0, Customunixdirview_SUnixDefaultRootName);
  8325. }
  8326. }
  8327. __finally
  8328. {
  8329. RemotePathComboBox->ItemIndex = Items->Count - 1;
  8330. // Setting ItemIndex to -1 does not reset its text
  8331. if (Items->Count == 0)
  8332. {
  8333. RemotePathComboBox->Text = L"";
  8334. }
  8335. Items->EndUpdate();
  8336. }
  8337. }
  8338. }
  8339. //---------------------------------------------------------------------------
  8340. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxAdjustImageIndex(
  8341. TTBXComboBoxItem * Sender, const UnicodeString /*AText*/, int AIndex,
  8342. int & ImageIndex)
  8343. {
  8344. if (AIndex < 0)
  8345. {
  8346. AIndex = Sender->ItemIndex;
  8347. }
  8348. ImageIndex = (AIndex < Sender->Strings->Count - 1 ? StdDirIcon : StdDirSelIcon);
  8349. }
  8350. //---------------------------------------------------------------------------
  8351. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxDrawItem(
  8352. TTBXCustomList * /*Sender*/, TCanvas * /*ACanvas*/, TRect & ARect, int AIndex,
  8353. int /*AHoverIndex*/, bool & /*DrawDefault*/)
  8354. {
  8355. ARect.Left += (10 * AIndex);
  8356. }
  8357. //---------------------------------------------------------------------------
  8358. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxMeasureWidth(
  8359. TTBXCustomList * /*Sender*/, TCanvas * /*ACanvas*/, int AIndex, int &AWidth)
  8360. {
  8361. AWidth += (10 * AIndex);
  8362. }
  8363. //---------------------------------------------------------------------------
  8364. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxItemClick(
  8365. TObject * Sender)
  8366. {
  8367. TTBXComboBoxItem * RemotePathComboBox = dynamic_cast<TTBXComboBoxItem*>(Sender);
  8368. UnicodeString APath = UnixExcludeTrailingBackslash(RemoteDirView->Path);
  8369. int Index = RemotePathComboBox->ItemIndex;
  8370. while (Index < RemotePathComboBox->Strings->Count - 1)
  8371. {
  8372. APath = UnixExtractFileDir(APath);
  8373. Index++;
  8374. }
  8375. // VanDyke style paths
  8376. if (APath.IsEmpty())
  8377. {
  8378. DebugAssert(RemotePathComboBox->ItemIndex == 0);
  8379. APath = ROOTDIRECTORY;
  8380. }
  8381. if (RemoteDirView->Path != APath)
  8382. {
  8383. RemoteDirView->Path = APath;
  8384. }
  8385. }
  8386. //---------------------------------------------------------------------------
  8387. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxCancel(TObject * Sender)
  8388. {
  8389. DebugAssert(Sender == GetComponent(fcRemotePathComboBox));
  8390. DebugUsedParam(Sender);
  8391. UpdateRemotePathComboBox(true);
  8392. }
  8393. //---------------------------------------------------------------------------
  8394. void __fastcall TCustomScpExplorerForm::DirViewEditing(
  8395. TObject * Sender, TListItem * Item, bool & /*AllowEdit*/)
  8396. {
  8397. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  8398. DebugAssert(DirView != NULL);
  8399. if (!WinConfiguration->RenameWholeName && !DirView->ItemIsDirectory(Item))
  8400. {
  8401. HWND Edit = ListView_GetEditControl(DirView->Handle);
  8402. // OnEditing is called also from TCustomListView::CanEdit
  8403. if (Edit != NULL)
  8404. {
  8405. EditSelectBaseName(Edit);
  8406. }
  8407. }
  8408. }
  8409. //---------------------------------------------------------------------------
  8410. //---------------------------------------------------------------------------
  8411. TDragDropFilesEx * __fastcall TCustomScpExplorerForm::CreateDragDropFilesEx()
  8412. {
  8413. TDragDropFilesEx * Result = new TDragDropFilesEx(this);
  8414. Result->AutoDetectDnD = false;
  8415. Result->NeedValid = TFileExMustDnDSet() << nvFilename;
  8416. Result->RenderDataOn = rdoEnterAndDropSync;
  8417. Result->TargetEffects = TDropEffectSet() << deCopy << deMove;
  8418. return Result;
  8419. }
  8420. //---------------------------------------------------------------------------
  8421. void __fastcall TCustomScpExplorerForm::CreateWnd()
  8422. {
  8423. TForm::CreateWnd();
  8424. if (FSessionsDragDropFilesEx == NULL)
  8425. {
  8426. FSessionsDragDropFilesEx = CreateDragDropFilesEx();
  8427. FSessionsDragDropFilesEx->OnDragOver = SessionsDDDragOver;
  8428. FSessionsDragDropFilesEx->OnProcessDropped = SessionsDDProcessDropped;
  8429. FSessionsDragDropFilesEx->OnDragEnter = SessionsDDDragEnter;
  8430. FSessionsDragDropFilesEx->OnDragLeave = SessionsDDDragLeave;
  8431. }
  8432. if (FQueueDragDropFilesEx == NULL)
  8433. {
  8434. FQueueDragDropFilesEx = CreateDragDropFilesEx();
  8435. // No need to set OnDragOver as we do not have any restrictions
  8436. FQueueDragDropFilesEx->OnProcessDropped = QueueDDProcessDropped;
  8437. FQueueDragDropFilesEx->OnDragEnter = QueueDDDragEnter;
  8438. FQueueDragDropFilesEx->OnDragLeave = QueueDDDragLeave;
  8439. }
  8440. }
  8441. //---------------------------------------------------------------------------
  8442. void __fastcall TCustomScpExplorerForm::DestroyWnd()
  8443. {
  8444. TForm::DestroyWnd();
  8445. FSessionsDragDropFilesEx->DragDropControl = NULL;
  8446. FQueueDragDropFilesEx->DragDropControl = NULL;
  8447. }
  8448. //---------------------------------------------------------------------------
  8449. void __fastcall TCustomScpExplorerForm::FormShow(TObject * /*Sender*/)
  8450. {
  8451. SideEnter(FCurrentSide);
  8452. FEverShown = true;
  8453. }
  8454. //---------------------------------------------------------------------------
  8455. void __fastcall TCustomScpExplorerForm::DoFindFiles(
  8456. TTerminal * ATerminal, UnicodeString Directory, const TFileMasks & FileMask,
  8457. TFileFoundEvent OnFileFound, TFindingFileEvent OnFindingFile)
  8458. {
  8459. if (!NonVisualDataModule->Busy)
  8460. {
  8461. TTerminalManager::Instance()->ActiveTerminal = ATerminal;
  8462. Configuration->Usage->Inc(L"FileFinds");
  8463. LockWindow(true);
  8464. NonVisualDataModule->StartBusy();
  8465. WinConfiguration->LockedInterface = true;
  8466. try
  8467. {
  8468. FTerminal->FilesFind(Directory, FileMask, OnFileFound, OnFindingFile);
  8469. }
  8470. __finally
  8471. {
  8472. WinConfiguration->LockedInterface = false;
  8473. NonVisualDataModule->EndBusy();
  8474. UnlockWindow();
  8475. }
  8476. }
  8477. }
  8478. //---------------------------------------------------------------------------
  8479. void __fastcall TCustomScpExplorerForm::DoFocusRemotePath(TTerminal * ATerminal, const UnicodeString & Path)
  8480. {
  8481. if (!NonVisualDataModule->Busy)
  8482. {
  8483. TTerminalManager::Instance()->ActiveTerminal = ATerminal;
  8484. SetFocus();
  8485. RemoteDirView->Path = UnixExtractFilePath(Path);
  8486. TListItem * Item = RemoteDirView->FindFileItem(UnixExtractFileName(Path));
  8487. if (Item != NULL)
  8488. {
  8489. RemoteDirView->ItemFocused = Item;
  8490. RemoteDirView->ItemFocused->MakeVisible(false);
  8491. RemoteDirView->SetFocus();
  8492. }
  8493. }
  8494. }
  8495. //---------------------------------------------------------------------------
  8496. bool __fastcall TCustomScpExplorerForm::CanOperateOnFoundFiles(TTerminal * ATerminal)
  8497. {
  8498. bool Result = !NonVisualDataModule->Busy;
  8499. if (Result)
  8500. {
  8501. TTerminalManager::Instance()->ActiveTerminal = ATerminal;
  8502. }
  8503. return Result;
  8504. }
  8505. //---------------------------------------------------------------------------
  8506. void __fastcall TCustomScpExplorerForm::DoOperationOnFoundFiles(
  8507. TFileOperation Operation, TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  8508. {
  8509. if (CanOperateOnFoundFiles(ATerminal))
  8510. {
  8511. TValueRestorer<TFileOperationFinishedEvent> OnFileOperationFinishedRestorer(FOnFileOperationFinished);
  8512. FOnFileOperationFinished = OnFileOperationFinished;
  8513. ExecuteFileOperation(Operation, osRemote, FileList, false, NULL);
  8514. }
  8515. }
  8516. //---------------------------------------------------------------------------
  8517. void __fastcall TCustomScpExplorerForm::DoDeleteFoundFiles(
  8518. TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  8519. {
  8520. DoOperationOnFoundFiles(foDelete, ATerminal, FileList, OnFileOperationFinished);
  8521. }
  8522. //---------------------------------------------------------------------------
  8523. void __fastcall TCustomScpExplorerForm::DoDownloadFoundFiles(
  8524. TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  8525. {
  8526. DoOperationOnFoundFiles(foCopy, ATerminal, FileList, OnFileOperationFinished);
  8527. }
  8528. //---------------------------------------------------------------------------
  8529. void __fastcall TCustomScpExplorerForm::DoEditFoundFiles(
  8530. TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  8531. {
  8532. if (CanOperateOnFoundFiles(ATerminal))
  8533. {
  8534. for (int Index = 0; Index < FileList->Count; Index++)
  8535. {
  8536. UnicodeString FileName = FileList->Strings[Index];
  8537. TRemoteFile * File = static_cast<TRemoteFile *>(FileList->Objects[Index]);
  8538. ExecuteRemoteFile(FileName, File, efDefaultEditor);
  8539. OnFileOperationFinished(FileName, true);
  8540. }
  8541. }
  8542. }
  8543. //---------------------------------------------------------------------------
  8544. void __fastcall TCustomScpExplorerForm::RemoteFindFiles()
  8545. {
  8546. FFileFindTerminal = Terminal;
  8547. ShowFileFindDialog(
  8548. Terminal, RemoteDirView->Path, DoFindFiles, DoFocusRemotePath, DoDeleteFoundFiles,
  8549. DoDownloadFoundFiles, DoEditFoundFiles);
  8550. }
  8551. //---------------------------------------------------------------------------
  8552. void __fastcall TCustomScpExplorerForm::UpdateTaskbarList(ITaskbarList3 * TaskbarList)
  8553. {
  8554. FTaskbarList = TaskbarList;
  8555. }
  8556. //---------------------------------------------------------------------------
  8557. void __fastcall TCustomScpExplorerForm::SessionsPageControlMouseDown(
  8558. TObject * /*Sender*/, TMouseButton Button, TShiftState /*Shift*/, int X, int Y)
  8559. {
  8560. int Index = SessionsPageControl->IndexOfTabAt(X, Y);
  8561. if (Index >= 0)
  8562. {
  8563. if (Button == mbLeft)
  8564. {
  8565. // "Mouse down" is raised only after tab is switched.
  8566. // If switching tab (switching session) takes long enough for user
  8567. // to actually release the button, "mouse down" is still raised,
  8568. // but we do not get "mouse up" event, so dragging is not cancelled,
  8569. // prevent that by not beginning dragging in the first place.
  8570. if (IsKeyPressed(VK_LBUTTON))
  8571. {
  8572. // when user clicks the "+", we get mouse down only after the session
  8573. // is closed, when new session tab is already on X:Y, so dragging
  8574. // starts, prevent that
  8575. if (MilliSecondsBetween(Now(), FSessionsPageControlNewSessionTime) > 500)
  8576. {
  8577. TTerminal * Terminal = GetSessionTabTerminal(SessionsPageControl->Pages[Index]);
  8578. if (Terminal != NULL)
  8579. {
  8580. SessionsPageControl->BeginDrag(false);
  8581. }
  8582. }
  8583. }
  8584. }
  8585. else if (Button == mbMiddle)
  8586. {
  8587. // ignore middle-click for "New session tab"
  8588. TTerminal * Terminal = GetSessionTabTerminal(SessionsPageControl->Pages[Index]);
  8589. if (Terminal != NULL)
  8590. {
  8591. SessionsPageControl->ActivePageIndex = Index;
  8592. // Switch to session tab (has to be session tab, due to previous check)
  8593. if (DebugAlwaysTrue(SessionTabSwitched()))
  8594. {
  8595. CloseSession();
  8596. }
  8597. }
  8598. }
  8599. }
  8600. }
  8601. //---------------------------------------------------------------------------
  8602. void __fastcall TCustomScpExplorerForm::SessionsPageControlDragDrop(
  8603. TObject * /*Sender*/, TObject * /*Source*/, int X, int Y)
  8604. {
  8605. int Index = SessionsPageControl->IndexOfTabAt(X, Y);
  8606. // do not allow dropping on the "+" tab
  8607. TTerminal * TargetTerminal = GetSessionTabTerminal(SessionsPageControl->Pages[Index]);
  8608. if ((TargetTerminal != NULL) &&
  8609. (SessionsPageControl->ActivePage->PageIndex != Index))
  8610. {
  8611. Configuration->Usage->Inc(L"SessionTabMoves");
  8612. // this is almost redundant as we would recreate tabs in DoTerminalListChanged,
  8613. // but we want to actually prevent that to avoid flicker
  8614. SessionsPageControl->ActivePage->PageIndex = Index;
  8615. TTerminal * Terminal = GetSessionTabTerminal(SessionsPageControl->ActivePage);
  8616. TTerminalManager::Instance()->Move(Terminal, TargetTerminal);
  8617. }
  8618. }
  8619. //---------------------------------------------------------------------------
  8620. void __fastcall TCustomScpExplorerForm::SessionsPageControlDragOver(
  8621. TObject * Sender, TObject * Source, int X, int Y,
  8622. TDragState /*State*/, bool & Accept)
  8623. {
  8624. Accept = (Sender == Source);
  8625. if (Accept)
  8626. {
  8627. int Index = SessionsPageControl->IndexOfTabAt(X, Y);
  8628. TTerminal * Terminal = GetSessionTabTerminal(SessionsPageControl->Pages[Index]);
  8629. // do not allow dragging to the "+" tab
  8630. Accept = (Terminal != NULL);
  8631. }
  8632. }
  8633. //---------------------------------------------------------------------------
  8634. void __fastcall TCustomScpExplorerForm::SessionsDDDragOver(int /*KeyState*/,
  8635. const TPoint & Point, int & Effect)
  8636. {
  8637. int Index = SessionsPageControl->IndexOfTabAt(Point.X, Point.Y);
  8638. if (Index < 0)
  8639. {
  8640. Effect = DROPEFFECT_None;
  8641. }
  8642. else
  8643. {
  8644. TTerminal * TargetTerminal = GetSessionTabTerminal(SessionsPageControl->Pages[Index]);
  8645. // do not allow dropping on the "+" tab
  8646. if (TargetTerminal == NULL)
  8647. {
  8648. Effect = DROPEFFECT_None;
  8649. }
  8650. }
  8651. }
  8652. //---------------------------------------------------------------------------
  8653. void __fastcall TCustomScpExplorerForm::SessionsDDProcessDropped(
  8654. TObject * /*Sender*/, int /*KeyState*/, const TPoint & Point, int Effect)
  8655. {
  8656. int Index = SessionsPageControl->IndexOfTabAt(Point.X, Point.Y);
  8657. // do not allow dropping on the "+" tab
  8658. TTerminal * TargetTerminal = GetSessionTabTerminal(SessionsPageControl->Pages[Index]);
  8659. if (TargetTerminal != NULL)
  8660. {
  8661. DebugAssert(!IsFileControl(DropSourceControl, osRemote));
  8662. if (!IsFileControl(DropSourceControl, osRemote))
  8663. {
  8664. TTerminalManager::Instance()->ActiveTerminal = TargetTerminal;
  8665. RemoteFileControlDragDropFileOperation(SessionsPageControl, Effect,
  8666. // Why don't we use Terminal->CurrentDirectory directly?
  8667. TTerminalManager::Instance()->ActiveTerminal->CurrentDirectory, false);
  8668. }
  8669. }
  8670. }
  8671. //---------------------------------------------------------------------------
  8672. void __fastcall TCustomScpExplorerForm::QueueDDProcessDropped(
  8673. TObject * /*Sender*/, int /*KeyState*/, const TPoint & /*Point*/, int Effect)
  8674. {
  8675. // Downloads are handled in RemoteFileControlDDEnd
  8676. if (!IsFileControl(DropSourceControl, osRemote))
  8677. {
  8678. RemoteFileControlDragDropFileOperation(QueueView3, Effect,
  8679. Terminal->CurrentDirectory,
  8680. // force queue
  8681. true);
  8682. }
  8683. }
  8684. //---------------------------------------------------------------------------
  8685. void __fastcall TCustomScpExplorerForm::FormClose(TObject * /*Sender*/, TCloseAction & /*Action*/)
  8686. {
  8687. FShowing = false;
  8688. // Do not save empty workspace
  8689. if (WinConfiguration->AutoSaveWorkspace && (Terminal != NULL))
  8690. {
  8691. std::unique_ptr<TObjectList> DataList(DoCollectWorkspace());
  8692. UnicodeString Name = WorkspaceName();
  8693. DoSaveWorkspace(
  8694. Name, DataList.get(),
  8695. !Configuration->DisablePasswordStoring &&
  8696. WinConfiguration->AutoSaveWorkspacePasswords);
  8697. WinConfiguration->LastStoredSession = Name;
  8698. }
  8699. }
  8700. //---------------------------------------------------------------------------
  8701. void __fastcall TCustomScpExplorerForm::RemoteDirViewRead(TObject * /*Sender*/)
  8702. {
  8703. TManagedTerminal * ManagedTerminal =
  8704. dynamic_cast<TManagedTerminal *>(RemoteDirView->Terminal);
  8705. if (ManagedTerminal != NULL)
  8706. {
  8707. ManagedTerminal->DirectoryLoaded = Now();
  8708. }
  8709. }
  8710. //---------------------------------------------------------------------------
  8711. void __fastcall TCustomScpExplorerForm::DirViewSelectItem(TObject * Sender,
  8712. TListItem * /*Item*/, bool /*Selected*/)
  8713. {
  8714. TCustomDirView * DirView = reinterpret_cast<TCustomDirView *>(Sender);
  8715. switch (DirView->LastSelectMethod)
  8716. {
  8717. case smKeyboard:
  8718. Configuration->Usage->Inc(L"KeyboardSelections");
  8719. break;
  8720. case smMouse:
  8721. Configuration->Usage->Inc(L"MouseSelections");
  8722. break;
  8723. }
  8724. }
  8725. //---------------------------------------------------------------------------
  8726. int __fastcall TCustomScpExplorerForm::AddSessionColor(TColor Color)
  8727. {
  8728. if (Color != 0)
  8729. {
  8730. AddSessionColorImage(FSessionColors, Color, FSessionColorMaskImageIndex);
  8731. return FSessionColors->Count - 1;
  8732. }
  8733. else
  8734. {
  8735. return FSessionTabImageIndex;
  8736. }
  8737. }
  8738. //---------------------------------------------------------------------------
  8739. int __fastcall TCustomScpExplorerForm::AddFixedSessionImage(int GlyphsSourceIndex)
  8740. {
  8741. TPngImageCollectionItem * Item =
  8742. GlyphsModule->ExplorerImages->PngImages->Items[GlyphsSourceIndex];
  8743. FSessionColors->AddPng(Item->PngImage);
  8744. return FSessionColors->Count - 1;
  8745. }
  8746. //---------------------------------------------------------------------------
  8747. void __fastcall TCustomScpExplorerForm::AddFixedSessionImages()
  8748. {
  8749. FSessionColors->SetSize(GlyphsModule->ExplorerImages->Width, GlyphsModule->ExplorerImages->Height);
  8750. FNewSessionTabImageIndex = AddFixedSessionImage(NonVisualDataModule->NewSessionAction->ImageIndex);
  8751. FSessionTabImageIndex = AddFixedSessionImage(SiteImageIndex);
  8752. FSessionColorMaskImageIndex = AddFixedSessionImage(SiteColorMaskImageIndex);
  8753. }
  8754. //---------------------------------------------------------------------------
  8755. void __fastcall TCustomScpExplorerForm::CollectItemsWithTextDisplayMode(TWinControl * Control)
  8756. {
  8757. for (int Index = 0; Index < Control->ControlCount; Index++)
  8758. {
  8759. TControl * AControl = Control->Controls[Index];
  8760. TWinControl * WinControl = dynamic_cast<TWinControl *>(AControl);
  8761. if (WinControl != NULL)
  8762. {
  8763. CollectItemsWithTextDisplayMode(WinControl);
  8764. }
  8765. TTBCustomToolbar * Toolbar = dynamic_cast<TTBCustomToolbar *>(AControl);
  8766. if (Toolbar != NULL)
  8767. {
  8768. // we care for top-level items only
  8769. TTBCustomItem * Items = Toolbar->Items;
  8770. for (int ItemIndex = 0; ItemIndex < Items->Count; ItemIndex++)
  8771. {
  8772. TTBCustomItem * Item = Items->Items[ItemIndex];
  8773. if (((Item->DisplayMode == nbdmImageAndText) ||
  8774. (dynamic_cast<TTBXLabelItem *>(Item) != NULL)) &&
  8775. EligibleForImageDisplayMode(Item))
  8776. {
  8777. FItemsWithTextDisplayMode.insert(Item);
  8778. }
  8779. }
  8780. }
  8781. }
  8782. }
  8783. //---------------------------------------------------------------------------
  8784. bool __fastcall TCustomScpExplorerForm::EligibleForImageDisplayMode(TTBCustomItem * /*Item*/)
  8785. {
  8786. return true;
  8787. }
  8788. //---------------------------------------------------------------------------
  8789. bool __fastcall TCustomScpExplorerForm::UpdateToolbarDisplayMode()
  8790. {
  8791. bool SelectiveToolbarText = WinConfiguration->SelectiveToolbarText;
  8792. TTBItemDisplayMode DisplayMode = (SelectiveToolbarText ? nbdmImageAndText : nbdmDefault);
  8793. typedef std::set<TTBCustomItem *> TItemsWithTextDisplayMode;
  8794. TItemsWithTextDisplayMode::iterator i = FItemsWithTextDisplayMode.begin();
  8795. bool Result = true;
  8796. while (Result && (i != FItemsWithTextDisplayMode.end()))
  8797. {
  8798. TTBCustomItem * Item = *i;
  8799. TTBXLabelItem * Label = dynamic_cast<TTBXLabelItem *>(Item);
  8800. if (Label != NULL)
  8801. {
  8802. // optimization
  8803. if (Label->Visible == SelectiveToolbarText)
  8804. {
  8805. Result = false;
  8806. }
  8807. else
  8808. {
  8809. Label->Visible = SelectiveToolbarText;
  8810. }
  8811. }
  8812. else
  8813. {
  8814. // optimization
  8815. if (Item->DisplayMode == DisplayMode)
  8816. {
  8817. Result = false;
  8818. }
  8819. else
  8820. {
  8821. Item->DisplayMode = DisplayMode;
  8822. }
  8823. }
  8824. ++i;
  8825. }
  8826. if (Result)
  8827. {
  8828. UpdateNewSessionTab();
  8829. }
  8830. return Result;
  8831. }
  8832. //---------------------------------------------------------------------------
  8833. void __fastcall TCustomScpExplorerForm::DisplaySystemContextMenu()
  8834. {
  8835. DebugFail();
  8836. }
  8837. //---------------------------------------------------------------------------
  8838. bool __fastcall TCustomScpExplorerForm::IsBusy()
  8839. {
  8840. // Among other, a lock level is non zero, while directory is being loaded,
  8841. // even when it happens as a result of dirview navigation,
  8842. // i.e. when TNonVisualDataModule is NOT FBusy.
  8843. // That's why the TNonVisualDataModule::GetBusy calls this method.
  8844. // Among other this prevents a panel auto update to occur while
  8845. // directory is changing.
  8846. return (FLockLevel > 0) || DirView(osCurrent)->IsEditing();
  8847. }
  8848. //---------------------------------------------------------------------------
  8849. Boolean __fastcall TCustomScpExplorerForm::AllowedAction(TAction * /*Action*/, TActionAllowed Allowed)
  8850. {
  8851. // While the window is disabled, we still seem to process menu shortcuts at least,
  8852. // so stop it at least here.
  8853. // See also TCustomScpExplorerForm::RemoteDirViewBusy
  8854. return
  8855. (Allowed == aaUpdate) ||
  8856. !NonVisualDataModule->Busy;
  8857. }
  8858. //---------------------------------------------------------------------------
  8859. void __fastcall TCustomScpExplorerForm::EditMenuItemPopup(TTBCustomItem * Sender, bool FromLink)
  8860. {
  8861. NonVisualDataModule->EditMenuItemPopup(Sender, FromLink);
  8862. }
  8863. //---------------------------------------------------------------------------
  8864. void __fastcall TCustomScpExplorerForm::DirViewBusy(TObject * /*Sender*/, int Busy, bool & State)
  8865. {
  8866. // This is somewhat redundant to LockWindow() call from
  8867. // TTerminalManager::TerminalReadDirectoryProgress.
  8868. // But disabling window is known not to block keyboard shorcuts
  8869. // (see TCustomScpExplorerForm::AllowedAction), this hopefully works.
  8870. if (Busy > 0)
  8871. {
  8872. if (NonVisualDataModule->Busy)
  8873. {
  8874. State = false;
  8875. }
  8876. else
  8877. {
  8878. NonVisualDataModule->StartBusy();
  8879. // Will be countered for TCustomDirView.DoExecute in DoDirViewExecFile
  8880. LockWindow();
  8881. }
  8882. }
  8883. else if (Busy < 0)
  8884. {
  8885. UnlockWindow();
  8886. NonVisualDataModule->EndBusy();
  8887. }
  8888. else
  8889. {
  8890. State = NonVisualDataModule->Busy;
  8891. }
  8892. }
  8893. //---------------------------------------------------------------------------
  8894. void __fastcall TCustomScpExplorerForm::SessionsPageControlContextPopup(TObject * /*Sender*/, TPoint & MousePos, bool & Handled)
  8895. {
  8896. int Index = SessionsPageControl->IndexOfTabAt(MousePos.X, MousePos.Y);
  8897. // no context menu for "New session tab"
  8898. if ((Index >= 0) && (GetSessionTabTerminal(SessionsPageControl->Pages[Index]) != NULL))
  8899. {
  8900. SessionsPageControl->ActivePageIndex = Index;
  8901. if (DebugAlwaysTrue(SessionTabSwitched()))
  8902. {
  8903. // copied from TControl.WMContextMenu
  8904. SendCancelMode(SessionsPageControl);
  8905. // explicit popup instead of using PopupMenu property
  8906. // to avoid menu to popup somewhere within SessionTabSwitched above,
  8907. // while connecting yet not-connected session and hence
  8908. // allowing an access to commands over not-completelly connected session
  8909. TPoint Point = SessionsPageControl->ClientToScreen(MousePos);
  8910. TPopupMenu * PopupMenu = NonVisualDataModule->SessionsPopup;
  8911. PopupMenu->PopupComponent = SessionsPageControl;
  8912. PopupMenu->Popup(Point.x, Point.y);
  8913. }
  8914. }
  8915. Handled = true;
  8916. }
  8917. //---------------------------------------------------------------------------
  8918. bool __fastcall TCustomScpExplorerForm::CanChangePassword()
  8919. {
  8920. return (Terminal != NULL) && Terminal->Active && Terminal->IsCapable[fcChangePassword];
  8921. }
  8922. //---------------------------------------------------------------------------
  8923. void __fastcall TCustomScpExplorerForm::ChangePassword()
  8924. {
  8925. Configuration->Usage->Inc(L"PasswordChanges");
  8926. std::unique_ptr<TSessionData> ChangePasswordData(Terminal->SessionData->Clone());
  8927. ChangePasswordData->DisableAuthentationsExceptPassword();
  8928. ChangePasswordData->ChangePassword = true;
  8929. std::unique_ptr<TTerminal> ChangePasswordSession(
  8930. Terminal->CreateSecondarySession(L"ChangePassword", ChangePasswordData.get()));
  8931. ChangePasswordSession->AutoReadDirectory = false;
  8932. if (TTerminalManager::Instance()->ConnectTerminal(ChangePasswordSession.get()))
  8933. {
  8934. MessageDialog(MainInstructions(LoadStr(PASSWORD_CHANGED)), qtInformation, qaOK, HELP_CHANGE_PASSWORD);
  8935. }
  8936. else
  8937. {
  8938. Configuration->Usage->Inc(L"PasswordChangesFailed");
  8939. }
  8940. }
  8941. //---------------------------------------------------------------------------
  8942. bool __fastcall TCustomScpExplorerForm::CanPrivateKeyUpload()
  8943. {
  8944. // No nice way to assert SSH2
  8945. return (Terminal != NULL) && Terminal->Active && (Terminal->FSProtocol == cfsSFTP);
  8946. }
  8947. //---------------------------------------------------------------------------
  8948. void __fastcall TCustomScpExplorerForm::PrivateKeyUpload()
  8949. {
  8950. TOperationVisualizer OperationVisualizer;
  8951. UnicodeString FileName = Terminal->SessionData->PublicKeyFile;
  8952. TTerminalManager::Instance()->UploadPublicKey(Terminal, NULL, FileName);
  8953. }
  8954. //---------------------------------------------------------------------------
  8955. void __fastcall TCustomScpExplorerForm::ChangeScale(int M, int D)
  8956. {
  8957. TForm::ChangeScale(M, D);
  8958. int APixelsPerInch = GetControlPixelsPerInch(this);
  8959. HANDLE ResourceModule = GUIConfiguration->ChangeToDefaultResourceModule();
  8960. try
  8961. {
  8962. GlyphsModule->SetPixelsPerInch(APixelsPerInch);
  8963. }
  8964. __finally
  8965. {
  8966. GUIConfiguration->ChangeResourceModule(ResourceModule);
  8967. }
  8968. }
  8969. //---------------------------------------------------------------------------
  8970. void __fastcall TCustomScpExplorerForm::DockContextPopup(TObject * Sender, TPoint & MousePos, bool & /*Handled*/)
  8971. {
  8972. NonVisualDataModule->ControlContextPopup(Sender, MousePos);
  8973. }
  8974. //---------------------------------------------------------------------
  8975. class TPublicControl : public TControl
  8976. {
  8977. friend class TCustomScpExplorerForm;
  8978. };
  8979. //---------------------------------------------------------------------------
  8980. void __fastcall TCustomScpExplorerForm::CopyPopup(TControl * DestControl, TControl * SourceControl)
  8981. {
  8982. static_cast<TPublicControl *>(DestControl)->PopupMenu = static_cast<TPublicControl *>(SourceControl)->PopupMenu;
  8983. static_cast<TPublicControl *>(DestControl)->OnContextPopup = static_cast<TPublicControl *>(SourceControl)->OnContextPopup;
  8984. }
  8985. //---------------------------------------------------------------------------
  8986. void __fastcall TCustomScpExplorerForm::DoBookmarkClick(TOperationSide Side, TObject * Sender)
  8987. {
  8988. TBookmark * Bookmark = DebugNotNull(reinterpret_cast<TBookmark *>(dynamic_cast<TTBCustomItem *>(Sender)->Tag));
  8989. OpenBookmark(Side, Bookmark);
  8990. }
  8991. //---------------------------------------------------------------------------
  8992. void __fastcall TCustomScpExplorerForm::LocalBookmarkClick(TObject * Sender)
  8993. {
  8994. DoBookmarkClick(osLocal, Sender);
  8995. }
  8996. //---------------------------------------------------------------------------
  8997. void __fastcall TCustomScpExplorerForm::RemoteBookmarkClick(TObject * Sender)
  8998. {
  8999. DoBookmarkClick(osRemote, Sender);
  9000. }
  9001. //---------------------------------------------------------------------------
  9002. void __fastcall TCustomScpExplorerForm::CreateOpenDirMenuList(
  9003. TTBCustomItem * Menu, TOperationSide Side, TBookmarkList * BookmarkList)
  9004. {
  9005. TNotifyEvent OnBookmarkClick = (Side == osLocal) ? &LocalBookmarkClick : &RemoteBookmarkClick;
  9006. if (!WinConfiguration->UseLocationProfiles)
  9007. {
  9008. std::unique_ptr<TStringList> Directories(new TStringList());
  9009. Directories->CaseSensitive = (Side == osRemote);
  9010. for (int Index = 0; Index < BookmarkList->Count; Index++)
  9011. {
  9012. TBookmark * Bookmark = BookmarkList->Bookmarks[Index];
  9013. UnicodeString Directory = Bookmark->GetSideDirectory(Side);
  9014. if (!Directory.IsEmpty() && (Directories->IndexOf(Directory) < 0))
  9015. {
  9016. std::unique_ptr<TTBCustomItem> Item(new TTBXItem(Owner));
  9017. Item->Caption = EscapeHotkey(Directory);
  9018. Item->ShortCut = Bookmark->ShortCut;
  9019. Item->OnClick = OnBookmarkClick;
  9020. Item->Tag = reinterpret_cast<int>(Bookmark);
  9021. Directories->Add(Directory);
  9022. Menu->Add(Item.release());
  9023. }
  9024. }
  9025. }
  9026. else
  9027. {
  9028. std::unique_ptr<TStrings> Folders(CreateSortedStringList());
  9029. for (int Index = 0; Index < BookmarkList->Count; Index++)
  9030. {
  9031. TBookmark * Bookmark = BookmarkList->Bookmarks[Index];
  9032. if (!Bookmark->Node.IsEmpty())
  9033. {
  9034. Folders->Add(Bookmark->Node);
  9035. }
  9036. }
  9037. for (int Index = 0; Index < Folders->Count; Index++)
  9038. {
  9039. std::unique_ptr<TTBCustomItem> Item(new TTBXSubmenuItem(Owner));
  9040. Item->Caption = Folders->Strings[Index];
  9041. Item->ImageIndex = NonVisualDataModule->RemoteChangePathAction->ImageIndex;
  9042. Folders->Objects[Index] = Item.get();
  9043. Menu->Add(Item.release());
  9044. }
  9045. for (int Index = 0; Index < BookmarkList->Count; Index++)
  9046. {
  9047. TBookmark * Bookmark = BookmarkList->Bookmarks[Index];
  9048. TTBCustomItem * Parent;
  9049. if (!Bookmark->Node.IsEmpty())
  9050. {
  9051. DebugAssert(Folders->IndexOf(Bookmark->Node) >= 0);
  9052. Parent = dynamic_cast<TTBCustomItem *>(Folders->Objects[Folders->IndexOf(Bookmark->Node)]);
  9053. }
  9054. else
  9055. {
  9056. Parent = Menu;
  9057. }
  9058. std::unique_ptr<TTBCustomItem> Item(new TTBXItem(Owner));
  9059. Item->Caption = Bookmark->Name;
  9060. Item->ShortCut = Bookmark->ShortCut;
  9061. Item->OnClick = OnBookmarkClick;
  9062. Item->Tag = reinterpret_cast<int>(Bookmark);
  9063. if (((Bookmark->Name == Bookmark->Local) || (Bookmark->Name == Bookmark->Remote)) &&
  9064. (Bookmark->Local.IsEmpty() || Bookmark->Remote.IsEmpty()))
  9065. {
  9066. // No hint for location profiles that are actually mere "bookmarks"
  9067. }
  9068. else
  9069. {
  9070. UnicodeString Hint = FORMAT(LoadStrPart(LOCATION_PROFILE_HINT, 1), (Bookmark->Name));
  9071. UnicodeString LongHint;
  9072. if (!Bookmark->Local.IsEmpty())
  9073. {
  9074. AddToList(LongHint, FORMAT(LoadStrPart(LOCATION_PROFILE_HINT, 2), (Bookmark->Local)), L"\n");
  9075. }
  9076. if (!Bookmark->Remote.IsEmpty())
  9077. {
  9078. AddToList(LongHint, FORMAT(LoadStrPart(LOCATION_PROFILE_HINT, 3), (Bookmark->Remote)), L"\n");
  9079. }
  9080. AddToList(Hint, LongHint, L"|");
  9081. Item->Hint = Hint;
  9082. }
  9083. Parent->Add(Item.release());
  9084. }
  9085. }
  9086. }
  9087. //---------------------------------------------------------------------------
  9088. void __fastcall TCustomScpExplorerForm::CreateOpenDirMenu(TTBCustomItem * Menu, TOperationSide Side)
  9089. {
  9090. Menu->Clear();
  9091. CreateOpenDirMenuList(Menu, Side, WinConfiguration->Bookmarks[Terminal->SessionData->SessionKey]);
  9092. AddMenuSeparator(Menu);
  9093. CreateOpenDirMenuList(Menu, Side, WinConfiguration->SharedBookmarks);
  9094. AddMenuSeparator(Menu);
  9095. std::unique_ptr<TTBCustomItem> Item(new TTBXItem(Owner));
  9096. Item->Action = (Side == osLocal) ? NonVisualDataModule->LocalAddBookmarkAction : NonVisualDataModule->RemoteAddBookmarkAction;
  9097. Menu->Add(Item.release());
  9098. }
  9099. //---------------------------------------------------------------------------