CustomScpExplorer.cpp 315 KB

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