CustomScpExplorer.cpp 298 KB

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