CustomScpExplorer.cpp 384 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612
  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 <algorithm>
  8. #include <Bookmarks.h>
  9. #include <Interface.h>
  10. #include <Exceptions.h>
  11. #include <CoreMain.h>
  12. #include <FileSystems.h>
  13. #include <TextsCore.h>
  14. #include <TextsWin.h>
  15. #include <HelpWin.h>
  16. #include <VCLCommon.h>
  17. #include <Progress.h>
  18. #include <SynchronizeProgress.h>
  19. #include <DragExt.h>
  20. #include <WinApi.h>
  21. #include "GUITools.h"
  22. #include "NonVisual.h"
  23. #include "Glyphs.h"
  24. #include "Tools.h"
  25. #include "WinConfiguration.h"
  26. #include "TerminalManager.h"
  27. #include "EditorManager.h"
  28. #include "ProgParams.h"
  29. #include "Setup.h"
  30. #include <Consts.hpp>
  31. #include <DateUtils.hpp>
  32. #include <TB2Common.hpp>
  33. #include <DirectoryMonitor.hpp>
  34. #include <System.IOUtils.hpp>
  35. //---------------------------------------------------------------------------
  36. #pragma package(smart_init)
  37. #pragma link "CustomDirView"
  38. #pragma link "CustomUnixDirView"
  39. #pragma link "IEListView"
  40. #pragma link "NortonLikeListView"
  41. #pragma link "UnixDirView"
  42. #pragma link "CustomDriveView"
  43. #pragma link "UnixDriveView"
  44. #pragma link "CustomDriveView"
  45. #pragma link "TB2Dock"
  46. #pragma link "TBXStatusBars"
  47. #pragma link "TB2Item"
  48. #pragma link "TB2Toolbar"
  49. #pragma link "ThemePageControl"
  50. #pragma link "PathLabel"
  51. #pragma resource "*.dfm"
  52. //---------------------------------------------------------------------------
  53. #define WM_COMPONENT_HIDE (WM_WINSCP_USER + 4)
  54. static const int SessionPanelCount = 3;
  55. //---------------------------------------------------------------------------
  56. class TMutexGuard
  57. {
  58. public:
  59. TMutexGuard(HANDLE AMutex, int Message = MUTEX_RELEASE_TIMEOUT,
  60. int Timeout = 5000)
  61. {
  62. FMutex = NULL;
  63. unsigned long WaitResult = WaitForSingleObject(AMutex, Timeout);
  64. if (WaitResult == WAIT_TIMEOUT)
  65. {
  66. throw Exception(LoadStr(MUTEX_RELEASE_TIMEOUT));
  67. }
  68. else
  69. {
  70. FMutex = AMutex;
  71. }
  72. }
  73. ~TMutexGuard()
  74. {
  75. if (FMutex != NULL)
  76. {
  77. ReleaseMutex(FMutex);
  78. }
  79. }
  80. private:
  81. HANDLE FMutex;
  82. };
  83. //---------------------------------------------------------------------------
  84. class TWindowLock
  85. {
  86. public:
  87. TWindowLock(TCustomScpExplorerForm * Form) :
  88. FForm(Form)
  89. {
  90. FForm->LockWindow();
  91. }
  92. ~TWindowLock()
  93. {
  94. FForm->UnlockWindow();
  95. }
  96. private:
  97. TCustomScpExplorerForm * FForm;
  98. };
  99. //---------------------------------------------------------------------------
  100. TAutoBatch::TAutoBatch(TCustomScpExplorerForm * Form) :
  101. FForm(Form)
  102. {
  103. FForm->BatchStart(FBatchStorage);
  104. }
  105. //---------------------------------------------------------------------------
  106. TAutoBatch::~TAutoBatch()
  107. {
  108. FForm->BatchEnd(FBatchStorage);
  109. }
  110. //---------------------------------------------------------------------------
  111. struct TTransferOperationParam
  112. {
  113. TTransferOperationParam();
  114. UnicodeString TargetDirectory;
  115. bool Temp;
  116. bool DragDrop;
  117. int Options;
  118. TAutoSwitch Queue;
  119. };
  120. //---------------------------------------------------------------------------
  121. TTransferOperationParam::TTransferOperationParam()
  122. {
  123. Temp = false;
  124. DragDrop = false;
  125. Options = 0;
  126. Queue = asAuto;
  127. }
  128. //---------------------------------------------------------------------------
  129. class TTransferPresetNoteData : public TObject
  130. {
  131. public:
  132. UnicodeString Message;
  133. };
  134. //---------------------------------------------------------------------------
  135. class TTerminalNoteData : public TObject
  136. {
  137. public:
  138. TManagedTerminal * Terminal;
  139. };
  140. //---------------------------------------------------------------------------
  141. __fastcall TCustomScpExplorerForm::TCustomScpExplorerForm(TComponent* Owner):
  142. FFormRestored(false),
  143. TForm(Owner)
  144. {
  145. AddStartupSequence(L"F");
  146. FInvalid = false;
  147. FCurrentSide = osRemote;
  148. FEverShown = false;
  149. FDocks = new TList();
  150. InitControls();
  151. DebugAssert(NonVisualDataModule && !NonVisualDataModule->ScpExplorer);
  152. // So that UpdateCustomCommandsToolbar called from RestoreParams works
  153. NonVisualDataModule->ScpExplorer = this;
  154. RestoreParams();
  155. ConfigurationChanged();
  156. RemoteDirView->Invalidate();
  157. FAutoOperation = false;
  158. FOnFileOperationFinished = NULL;
  159. FPrimaryOperation = foNone;
  160. FForceExecution = false;
  161. FIgnoreNextDialogChar = 0;
  162. FErrorList = NULL;
  163. FSynchronizeProgressForm = NULL;
  164. FProgressForm = NULL;
  165. FRefreshLocalDirectory = false;
  166. FRefreshRemoteDirectory = false;
  167. FDDExtMapFile = NULL;
  168. // CreateMutexW keeps failing with ERROR_NOACCESS
  169. FDDExtMutex = CreateMutexA(NULL, false, AnsiString(DRAG_EXT_MUTEX).c_str());
  170. DebugAssert(FDDExtMutex != NULL);
  171. FDDTargetControl = NULL;
  172. FDelayedDeletionTimer = NULL;
  173. FDelayedDeletionList = new TStringList();
  174. FDDFileList = NULL;
  175. FPendingTempSpaceWarn = false;
  176. FCapturedLog = NULL;
  177. FDragDropOperation = false;
  178. memset(&FHistoryMenu, 0, sizeof(FHistoryMenu));
  179. FAllowTransferPresetAutoSelect = true;
  180. FSessionChanging = false;
  181. FCopyParamDefault = L"";
  182. FSynchronizeController = NULL;
  183. FPendingQueueActionItem = NULL;
  184. FLockLevel = 0;
  185. FLockSuspendLevel = 0;
  186. FDisabledOnLockSuspend = false;
  187. FAlternativeDelete = false;
  188. FTrayIcon = new ::TTrayIcon(0);
  189. FTrayIcon->OnClick = TrayIconClick;
  190. FMaxQueueLength = 0;
  191. FLastContextPopupScreenPoint = TPoint(-1, -1);
  192. FTransferResumeList = NULL;
  193. FDeletedFiles = NULL;
  194. FMoveToQueue = false;
  195. StandaloneOperation = false;
  196. FOnFeedSynchronizeError = NULL;
  197. FOnSynchronizeAbort = NULL;
  198. FSynchronizeTerminal = NULL;
  199. FNeedSession = false;
  200. FDoNotIdleCurrentTerminal = 0;
  201. FIncrementalSearching = 0;
  202. FIncrementalSearchHaveNext = false;
  203. FQueueFileList.reset(new TQueueFileList());
  204. FProgressSide = osCurrent;
  205. FCalculateSizeOperation = NULL;
  206. FDownloadingFromClipboard = false;
  207. FClipboardFakeMonitorsPendingReset = false;
  208. FEditorManager = new TEditorManager();
  209. FEditorManager->OnFileChange = ExecutedFileChanged;
  210. FEditorManager->OnFileReload = ExecutedFileReload;
  211. FEditorManager->OnFileEarlyClosed = ExecutedFileEarlyClosed;
  212. FEditorManager->OnFileUploadComplete = ExecutedFileUploadComplete;
  213. FLocalEditors = new TList();
  214. FQueueStatus = NULL;
  215. FQueueStatusSection = new TCriticalSection();
  216. FQueueStatusInvalidated = false;
  217. FQueueItemInvalidated = false;
  218. FQueueStatusUpdating = false;
  219. FQueueActedItem = NULL;
  220. FQueueController = new TQueueController(QueueView3);
  221. FUserActionTimer = new TTimer(this);
  222. FUserActionTimer->Enabled = false;
  223. FUserActionTimer->Interval = 10;
  224. FUserActionTimer->OnTimer = UserActionTimer;
  225. FNotes = new TStringList();
  226. FNoteTimer = new TTimer(this);
  227. FNoteTimer->Enabled = false;
  228. FNoteTimer->OnTimer = NoteTimer;
  229. FOnNoteClick = NULL;
  230. FOle32Library = LoadLibrary(L"Ole32.dll");
  231. FDragMoveCursor = FOle32Library != NULL ?
  232. LoadCursor(FOle32Library, MAKEINTRESOURCE(2)) : NULL;
  233. UseSystemSettings(this);
  234. TTBXStringList * TransferList = dynamic_cast<TTBXStringList*>(
  235. static_cast<TObject*>(GetComponent(fcTransferList)));
  236. DebugAssert(TransferList != NULL);
  237. FTransferListHoverIndex = -1;
  238. TransferList->OnChange = TransferListChange;
  239. TransferList->OnDrawItem = TransferListDrawItem;
  240. SetSubmenu(dynamic_cast<TTBXCustomItem *>(static_cast<TObject *>(GetComponent(fcColorMenu))), true);
  241. SetSubmenu(NonVisualDataModule->ColorMenuItem, true);
  242. UseDesktopFont(SessionsPageControl);
  243. UpdateSessionsPageControlHeight();
  244. UseDesktopFont(RemoteDirView);
  245. UseDesktopFont(RemoteDriveView);
  246. UseDesktopFont(QueueView3);
  247. UseDesktopFont(QueueFileList);
  248. UseDesktopFont(QueueLabel);
  249. UseDesktopFont(RemoteStatusBar);
  250. reinterpret_cast<TLabel*>(QueueSplitter)->OnDblClick = QueueSplitterDblClick;
  251. QueueSplitter->ShowHint = true;
  252. reinterpret_cast<TLabel*>(QueueFileListSplitter)->OnDblClick = QueueFileListSplitterDblClick;
  253. QueueFileListSplitter->ShowHint = true;
  254. RemotePanelSplitter->ShowHint = true;
  255. UpdateImages();
  256. FCustomCommandMenu = CreateTBXPopupMenu(this);
  257. FCustomCommandLocalFileList = NULL;
  258. FCustomCommandRemoteFileList = NULL;
  259. FSessionColors = new TPngImageList(this);
  260. FSessionColors->ColorDepth = cd32Bit;
  261. AddFixedSessionImages();
  262. SessionsPageControl->Images = FSessionColors;
  263. UpdateQueueLabel();
  264. CreateHiddenWindow();
  265. StartUpdates();
  266. }
  267. //---------------------------------------------------------------------------
  268. __fastcall TCustomScpExplorerForm::~TCustomScpExplorerForm()
  269. {
  270. FInvalid = true;
  271. // this has to be one of the very first things to do
  272. StopUpdateThread();
  273. // This is needed when shuting down Windows only. Otherwise it's already set NULL from Execute()
  274. TTerminalManager::Instance()->ScpExplorer = NULL;
  275. delete FCustomCommandLocalFileList;
  276. delete FCustomCommandRemoteFileList;
  277. delete FCustomCommandMenu;
  278. delete FTrayIcon;
  279. FTrayIcon = NULL;
  280. FEditorManager->CloseInternalEditors(ForceCloseInternalEditor);
  281. delete FEditorManager;
  282. ForceCloseLocalEditors();
  283. delete FLocalEditors;
  284. HideFileFindDialog();
  285. if (FDelayedDeletionTimer)
  286. {
  287. DoDelayedDeletion(NULL);
  288. SAFE_DESTROY(FDelayedDeletionTimer);
  289. }
  290. SAFE_DESTROY(FDelayedDeletionList);
  291. // sometimes we do not get DDEnd so the list is not released
  292. SAFE_DESTROY(FDDFileList);
  293. DebugAssert(FSynchronizeController == NULL);
  294. CloseHandle(FDDExtMutex);
  295. FDDExtMutex = NULL;
  296. FreeLibrary(FOle32Library);
  297. FOle32Library = NULL;
  298. FDragMoveCursor = NULL;
  299. DebugAssert(!FErrorList);
  300. if (FEverShown)
  301. {
  302. // when window is never shown (like when running command-line operation),
  303. // particularly window site is not restored correctly (BoundsRect value set
  304. // in RestoreForm gets lost during handle allocation), so we do not want
  305. // it to be stored
  306. StoreParams();
  307. }
  308. ManagedSession = NULL;
  309. Queue = NULL;
  310. DebugAssert(NonVisualDataModule && (NonVisualDataModule->ScpExplorer == this));
  311. NonVisualDataModule->ScpExplorer = NULL;
  312. delete FQueueController;
  313. FQueueController = NULL;
  314. delete FQueueStatusSection;
  315. FQueueStatusSection = NULL;
  316. delete FQueueStatus;
  317. FQueueStatus = NULL;
  318. delete FUserActionTimer;
  319. FUserActionTimer = NULL;
  320. delete FNoteTimer;
  321. delete FNotes;
  322. delete FNoteData;
  323. SAFE_DESTROY(FDocks);
  324. SessionsPageControl->Images = NULL;
  325. // only after clearing Terminal (after SessionListChanged => ... => AddSessionColor is called)
  326. SAFE_DESTROY(FSessionColors);
  327. SAFE_DESTROY(FSessionsDragDropFilesEx);
  328. SAFE_DESTROY(FHistoryMenu[0][0]);
  329. SAFE_DESTROY(FHistoryMenu[0][1]);
  330. SAFE_DESTROY(FHistoryMenu[1][0]);
  331. SAFE_DESTROY(FHistoryMenu[1][1]);
  332. if (FHiddenWindow != NULL)
  333. {
  334. DestroyWindow(FHiddenWindow);
  335. FHiddenWindow = NULL;
  336. }
  337. }
  338. //---------------------------------------------------------------------------
  339. void __fastcall TCustomScpExplorerForm::RefreshPanel(const UnicodeString & Session, const UnicodeString & Path)
  340. {
  341. std::unique_ptr<TSessionData> Data;
  342. if (!Session.IsEmpty())
  343. {
  344. bool DefaultsOnly;
  345. Data.reset(StoredSessions->ParseUrl(Session, NULL, DefaultsOnly));
  346. }
  347. TTerminalManager * Manager = TTerminalManager::Instance();
  348. for (int Index = 0; Index < Manager->Count; Index++)
  349. {
  350. TTerminal * Terminal = Manager->Sessions[Index];
  351. if (Session.IsEmpty() ||
  352. Manager->IsActiveTerminalForSite(Terminal, Data.get()))
  353. {
  354. if (Path.IsEmpty())
  355. {
  356. Terminal->ClearCaches();
  357. }
  358. else
  359. {
  360. Terminal->DirectoryModified(Path, true);
  361. }
  362. }
  363. }
  364. // We should flag a pending refresh for the background terminals or busy foreground terminals
  365. if (HasActiveTerminal() &&
  366. CanCommandLineFromAnotherInstance() &&
  367. (Session.IsEmpty() || Manager->IsActiveTerminalForSite(Terminal, Data.get())) &&
  368. (Path.IsEmpty() || UnixIsChildPath(Path, Terminal->CurrentDirectory)))
  369. {
  370. Terminal->ReloadDirectory();
  371. }
  372. }
  373. //---------------------------------------------------------------------------
  374. void __fastcall TCustomScpExplorerForm::WMCopyData(TMessage & Message)
  375. {
  376. PCOPYDATASTRUCT CopyData = reinterpret_cast<PCOPYDATASTRUCT>(Message.LParam);
  377. size_t MessageSize = sizeof(TCopyDataMessage);
  378. bool Result = DebugAlwaysTrue(CopyData->cbData == MessageSize);
  379. if (Result)
  380. {
  381. const TCopyDataMessage & Message = *reinterpret_cast<const TCopyDataMessage *>(CopyData->lpData);
  382. Result = (Message.Version == TCopyDataMessage::Version1);
  383. if (Result)
  384. {
  385. switch (Message.Command)
  386. {
  387. case TCopyDataMessage::CommandCanCommandLine:
  388. Result = CanCommandLineFromAnotherInstance();
  389. break;
  390. case TCopyDataMessage::CommandCommandLine:
  391. {
  392. UnicodeString CommandLine(Message.CommandLine);
  393. Result = CommandLineFromAnotherInstance(CommandLine);
  394. }
  395. break;
  396. case TCopyDataMessage::RefreshPanel:
  397. RefreshPanel(Message.Refresh.Session, Message.Refresh.Path);
  398. break;
  399. case TCopyDataMessage::MainWindowCheck:
  400. Result = true;
  401. break;
  402. default:
  403. Result = false;
  404. break;
  405. }
  406. }
  407. }
  408. Message.Result = Result ? 1 : 0;
  409. }
  410. //---------------------------------------------------------------------------
  411. void __fastcall TCustomScpExplorerForm::CreateHiddenWindow()
  412. {
  413. WNDCLASS WindowClass = {0};
  414. WindowClass.lpfnWndProc = DefWindowProc;
  415. WindowClass.hInstance = HInstance;
  416. WindowClass.lpszClassName = HIDDEN_WINDOW_NAME;
  417. FHiddenWindow = NULL;
  418. if (RegisterClass(&WindowClass))
  419. {
  420. FHiddenWindow = CreateWindow(HIDDEN_WINDOW_NAME, L"",
  421. WS_POPUP, 0, 0, 0, 0, 0, 0, HInstance, NULL);
  422. }
  423. }
  424. //---------------------------------------------------------------------------
  425. bool __fastcall TCustomScpExplorerForm::CanConsole()
  426. {
  427. return
  428. HasActiveTerminal() &&
  429. (Terminal->IsCapable[fcAnyCommand] || Terminal->IsCapable[fcSecondaryShell]);
  430. }
  431. //---------------------------------------------------------------------------
  432. bool __fastcall TCustomScpExplorerForm::CanCommandLineFromAnotherInstance()
  433. {
  434. bool Result = Visible && !NonVisualDataModule->Busy;
  435. return Result;
  436. }
  437. //---------------------------------------------------------------------------
  438. bool __fastcall TCustomScpExplorerForm::CommandLineFromAnotherInstance(
  439. const UnicodeString & CommandLine)
  440. {
  441. TProgramParams Params(CommandLine);
  442. bool Result = CanCommandLineFromAnotherInstance() && DebugAlwaysTrue(Params.ParamCount > 0);
  443. if (Result)
  444. {
  445. NonVisualDataModule->StartBusy();
  446. try
  447. {
  448. // this action is initiated from another process,
  449. // so it's likely that our window is not visible,
  450. // and user won't see what is going on
  451. Application->BringToFront();
  452. // reload sessions as we may be asked to open a session
  453. // just stored by another instance
  454. StoredSessions->Reload();
  455. UnicodeString SessionName = Params.Param[1];
  456. std::unique_ptr<TObjectList> DataList(new TObjectList());
  457. try
  458. {
  459. UnicodeString DownloadFile; // unused
  460. int Flags = GetCommandLineParseUrlFlags(&Params);
  461. GetLoginData(SessionName, &Params, DataList.get(), DownloadFile, true, this, Flags);
  462. }
  463. catch (EAbort &)
  464. {
  465. // Happens when opening session in PuTTY and in other situations,
  466. // in which we do not want to fail WM_COPYDATA, as that would cause master instance
  467. // to process the command-line on its own (or to pass it to yet another instance)
  468. }
  469. if (DataList->Count > 0)
  470. {
  471. TTerminalManager * Manager = TTerminalManager::Instance();
  472. Manager->ActiveSession = Manager->NewSessions(DataList.get());
  473. }
  474. }
  475. __finally
  476. {
  477. NonVisualDataModule->EndBusy();
  478. }
  479. }
  480. return Result;
  481. }
  482. //---------------------------------------------------------------------------
  483. TManagedTerminal * TCustomScpExplorerForm::GetTerminal()
  484. {
  485. TManagedTerminal * Result;
  486. if ((FManagedSession != NULL) && !FManagedSession->LocalBrowser)
  487. {
  488. Result = FManagedSession;
  489. }
  490. else
  491. {
  492. Result = NULL;
  493. }
  494. return Result;
  495. }
  496. //---------------------------------------------------------------------------
  497. void __fastcall TCustomScpExplorerForm::SetManagedSession(TManagedTerminal * value)
  498. {
  499. if (FManagedSession != value)
  500. {
  501. SessionChanging();
  502. DoSetManagedSession(value, false);
  503. }
  504. }
  505. //---------------------------------------------------------------------------
  506. void __fastcall TCustomScpExplorerForm::DoSetManagedSession(TManagedTerminal * value, bool Replace)
  507. {
  508. DebugAssert(!Replace || ((value != NULL) && !value->LocalBrowser));
  509. FManagedSession = value;
  510. {
  511. TValueRestorer<bool> AllowTransferPresetAutoSelectRestorer(FAllowTransferPresetAutoSelect);
  512. FAllowTransferPresetAutoSelect = false;
  513. TAutoFlag SessionChangingFlag(FSessionChanging);
  514. SessionChanged(Replace);
  515. }
  516. if (Terminal != NULL)
  517. {
  518. TransferPresetAutoSelect();
  519. }
  520. // Update app and tab titles, prevented above by FSessionChanging.
  521. UpdateControls();
  522. }
  523. //---------------------------------------------------------------------------
  524. void __fastcall TCustomScpExplorerForm::ReplaceTerminal(TManagedTerminal * value)
  525. {
  526. DoSetManagedSession(value, true);
  527. }
  528. //---------------------------------------------------------------------------
  529. void __fastcall TCustomScpExplorerForm::SessionChanging()
  530. {
  531. if (ManagedSession != NULL)
  532. {
  533. UpdateSession(ManagedSession);
  534. }
  535. }
  536. //---------------------------------------------------------------------------
  537. void __fastcall TCustomScpExplorerForm::SessionChanged(bool Replaced)
  538. {
  539. if (Terminal != NULL)
  540. {
  541. UpdateSessionColor((TColor)Terminal->StateData->Color);
  542. }
  543. if (!TTerminalManager::Instance()->Updating)
  544. {
  545. SessionListChanged();
  546. }
  547. if (Terminal != NULL)
  548. {
  549. TObject * State = NULL;
  550. if (WinConfiguration->PreservePanelState)
  551. {
  552. State = Terminal->RemoteExplorerState;
  553. DirView(osRemote)->AnnounceState(State);
  554. }
  555. try
  556. {
  557. if (Replaced)
  558. {
  559. RemoteDirView->ReplaceTerminal(Terminal);
  560. }
  561. else
  562. {
  563. RemoteDirView->Terminal = Terminal;
  564. }
  565. if (Terminal->Active)
  566. {
  567. Terminal->RefreshDirectory();
  568. }
  569. if (State != NULL)
  570. {
  571. DirView(osRemote)->RestoreState(State);
  572. }
  573. }
  574. __finally
  575. {
  576. DirView(osRemote)->AnnounceState(NULL);
  577. }
  578. }
  579. else
  580. {
  581. RemoteDirView->Terminal = NULL;
  582. }
  583. InitStatusBar();
  584. UpdateTransferList();
  585. // Update panels Enable state before refreshing the labels
  586. UpdateControls();
  587. }
  588. //---------------------------------------------------------------------------
  589. void __fastcall TCustomScpExplorerForm::SetQueue(TTerminalQueue * value)
  590. {
  591. if (Queue != value)
  592. {
  593. if (FQueue != NULL)
  594. {
  595. FQueue->OnListUpdate = NULL;
  596. FQueue->OnQueueItemUpdate = NULL;
  597. }
  598. FQueue = value;
  599. if (FQueue != NULL)
  600. {
  601. DebugAssert(FQueue->OnListUpdate == NULL);
  602. FQueue->OnListUpdate = QueueListUpdate;
  603. DebugAssert(FQueue->OnQueueItemUpdate == NULL);
  604. FQueue->OnQueueItemUpdate = QueueItemUpdate;
  605. }
  606. QueueChanged();
  607. }
  608. }
  609. //---------------------------------------------------------------------------
  610. void __fastcall TCustomScpExplorerForm::QueueView3Deletion(TObject * /*Sender*/,
  611. TListItem * Item)
  612. {
  613. if (FQueueActedItem == Item)
  614. {
  615. FQueueActedItem = NULL;
  616. if ((QueueView3->PopupMenu != NULL) &&
  617. (QueueView3->PopupMenu->PopupComponent == QueueView3))
  618. {
  619. // rather "trick", suggested by Jordan on jrsoftware.toolbar2000
  620. ReleaseCapture();
  621. }
  622. }
  623. if (Item->Data == FPendingQueueActionItem)
  624. {
  625. FPendingQueueActionItem = NULL;
  626. }
  627. }
  628. //---------------------------------------------------------------------------
  629. bool TCustomScpExplorerForm::IsAnythingQueued()
  630. {
  631. return (FQueueStatus != NULL) && (FQueueStatus->ActiveAndPendingPrimaryCount > 0);
  632. }
  633. //---------------------------------------------------------------------------
  634. void __fastcall TCustomScpExplorerForm::UpdateQueueStatus(bool QueueChanging)
  635. {
  636. {
  637. TGuard Guard(FQueueStatusSection);
  638. FQueueStatusInvalidated = false;
  639. if (FQueue != NULL)
  640. {
  641. FQueueStatus = FQueue->CreateStatus(FQueueStatus);
  642. }
  643. }
  644. if ((FQueueStatus != NULL) && (FQueueStatus->Count > FMaxQueueLength))
  645. {
  646. FMaxQueueLength = FQueueStatus->Count;
  647. Configuration->Usage->SetMax(L"MaxQueueLength", FMaxQueueLength);
  648. }
  649. {
  650. TAutoFlag Flag(FQueueStatusUpdating);
  651. FQueueController->UpdateQueueStatus(FQueueStatus);
  652. }
  653. UpdateQueueFileList();
  654. SetQueueProgress();
  655. UpdateQueueView();
  656. bool IsEmpty = !IsAnythingQueued();
  657. if (IsEmpty && (Terminal != NULL))
  658. {
  659. TOnceDoneOperation OnceDoneOperation =
  660. NonVisualDataModule->CurrentQueueOnceEmptyOperation();
  661. NonVisualDataModule->ResetQueueOnceEmptyOperation();
  662. if ((FQueue != NULL) && !WinConfiguration->EnableQueueByDefault && !QueueChanging)
  663. {
  664. FQueue->Enabled = false;
  665. }
  666. if ((OnceDoneOperation != odoIdle) && !NonVisualDataModule->Busy)
  667. {
  668. Terminal->CloseOnCompletion(OnceDoneOperation, LoadStr(CLOSED_ON_QUEUE_EMPTY));
  669. }
  670. }
  671. }
  672. //---------------------------------------------------------------------------
  673. UnicodeString __fastcall TCustomScpExplorerForm::GetQueueProgressTitle()
  674. {
  675. UnicodeString Result;
  676. if (FQueueStatus != NULL)
  677. {
  678. if (FQueueStatus->IsOnlyOneActiveAndNoPending())
  679. {
  680. TFileOperationProgressType * ProgressData =
  681. FQueueStatus->Items[FQueueStatus->DoneCount]->ProgressData;
  682. if ((ProgressData != NULL) && ProgressData->InProgress)
  683. {
  684. Result = TProgressForm::ProgressStr(NULL, ProgressData);
  685. }
  686. }
  687. else if (FQueueStatus->ActiveAndPendingPrimaryCount > 1)
  688. {
  689. Result = FMTLOAD(PROGRESS_IN_QUEUE, (FQueueStatus->ActiveAndPendingPrimaryCount));
  690. }
  691. }
  692. return Result;
  693. }
  694. //---------------------------------------------------------------------------
  695. void __fastcall TCustomScpExplorerForm::UpdateQueueView()
  696. {
  697. ComponentVisible[fcQueueView] =
  698. !IsLocalBrowserMode() &&
  699. ((WinConfiguration->QueueView.Show == qvShow) ||
  700. ((WinConfiguration->QueueView.Show == qvHideWhenEmpty) &&
  701. (FQueueStatus != NULL) && (FQueueStatus->Count > 0)));
  702. }
  703. //---------------------------------------------------------------------------
  704. void __fastcall TCustomScpExplorerForm::QueueChanged()
  705. {
  706. if (FQueueStatus != NULL)
  707. {
  708. // UpdateFileList implementation relies on the status being recreated when session changes
  709. delete FQueueStatus;
  710. FQueueStatus = NULL;
  711. }
  712. UpdateQueueStatus(true);
  713. }
  714. //---------------------------------------------------------------------------
  715. void __fastcall TCustomScpExplorerForm::QueueListUpdate(TTerminalQueue * Queue)
  716. {
  717. if (FQueue == Queue)
  718. {
  719. FQueueStatusInvalidated = true;
  720. }
  721. }
  722. //---------------------------------------------------------------------------
  723. void __fastcall TCustomScpExplorerForm::QueueItemUpdate(TTerminalQueue * Queue,
  724. TQueueItem * Item)
  725. {
  726. if (FQueue == Queue)
  727. {
  728. TGuard Guard(FQueueStatusSection);
  729. // this may be running in parallel with QueueChanged
  730. if (FQueueStatus != NULL)
  731. {
  732. TQueueItemProxy * QueueItem = FQueueStatus->FindByQueueItem(Item);
  733. if ((Item->Status == TQueueItem::qsDone) && (Terminal != NULL))
  734. {
  735. FRefreshLocalDirectory = (QueueItem == NULL) ||
  736. (!QueueItem->Info->ModifiedLocal.IsEmpty());
  737. FRefreshRemoteDirectory = (QueueItem == NULL) ||
  738. (!QueueItem->Info->ModifiedRemote.IsEmpty());
  739. }
  740. if (QueueItem != NULL)
  741. {
  742. QueueItem->UserData = (void*)true;
  743. FQueueItemInvalidated = true;
  744. }
  745. }
  746. }
  747. }
  748. //---------------------------------------------------------------------------
  749. bool __fastcall TCustomScpExplorerForm::IsQueueAutoPopup()
  750. {
  751. // during standalone editing, we have no way to see/control queue,
  752. // so we have to always popup prompts automatically
  753. return StandaloneOperation || GUIConfiguration->QueueAutoPopup;
  754. }
  755. //---------------------------------------------------------------------------
  756. void __fastcall TCustomScpExplorerForm::RefreshQueueItems()
  757. {
  758. if (FQueueStatus != NULL)
  759. {
  760. bool QueueAutoPopup = IsQueueAutoPopup();
  761. bool NeedRefresh = FQueueController->NeedRefresh();
  762. bool Refresh = FQueueItemInvalidated || NeedRefresh;
  763. FQueueItemInvalidated = false;
  764. int Limit = Refresh ? FQueueStatus->Count : FQueueStatus->DoneAndActiveCount;
  765. bool Updated = false;
  766. TQueueItemProxy * QueueItem;
  767. bool WasUserAction;
  768. for (int Index = 0; Index < Limit; Index++)
  769. {
  770. bool Update = false;
  771. QueueItem = FQueueStatus->Items[Index];
  772. WasUserAction = TQueueItem::IsUserActionStatus(QueueItem->Status);
  773. if (!NonVisualDataModule->Busy && QueueAutoPopup && WasUserAction &&
  774. (FPendingQueueActionItem == NULL))
  775. {
  776. FPendingQueueActionItem = QueueItem;
  777. FUserActionTimer->Enabled = true;
  778. }
  779. if ((bool)QueueItem->UserData)
  780. {
  781. QueueItem->UserData = (void*)false;
  782. QueueItem->Update();
  783. Updated = true;
  784. Update = true;
  785. }
  786. else if (FQueueController->QueueItemNeedsFrequentRefresh(QueueItem) || NeedRefresh)
  787. {
  788. Update = true;
  789. }
  790. if (Update)
  791. {
  792. FQueueController->RefreshQueueItem(QueueItem);
  793. }
  794. }
  795. if (Updated)
  796. {
  797. NonVisualDataModule->UpdateNonVisibleActions();
  798. SetQueueProgress();
  799. UpdateQueueFileList();
  800. }
  801. }
  802. }
  803. //---------------------------------------------------------------------------
  804. void __fastcall TCustomScpExplorerForm::SetTaskbarListProgressState(TBPFLAG Flags)
  805. {
  806. if (FTaskbarList != NULL)
  807. {
  808. // Could now use Application->MainFormHandle, now that we implement Application->OnGetMainFormHandle
  809. FTaskbarList->SetProgressState(GetMainForm()->Handle, Flags);
  810. }
  811. }
  812. //---------------------------------------------------------------------------
  813. void __fastcall TCustomScpExplorerForm::SetTaskbarListProgressValue(int Progress)
  814. {
  815. if (Progress >= 0)
  816. {
  817. if (FTaskbarList != NULL)
  818. {
  819. FTaskbarList->SetProgressValue(GetMainForm()->Handle, Progress, 100);
  820. }
  821. }
  822. else
  823. {
  824. SetTaskbarListProgressState(TBPF_INDETERMINATE);
  825. }
  826. }
  827. //---------------------------------------------------------------------------
  828. void __fastcall TCustomScpExplorerForm::SetTaskbarListProgressValue(TFileOperationProgressType * ProgressData)
  829. {
  830. int OverallProgress;
  831. // FProgressForm is null when this is called from SetQueueProgress
  832. if ((FProgressForm != NULL) && (FProgressForm->SynchronizeProgress != NULL))
  833. {
  834. OverallProgress = FProgressForm->SynchronizeProgress->Progress(ProgressData);
  835. }
  836. else if (!ProgressData->IsIndeterminate())
  837. {
  838. OverallProgress = ProgressData->OverallProgress();
  839. }
  840. else
  841. {
  842. OverallProgress = -1;
  843. }
  844. SetTaskbarListProgressValue(OverallProgress);
  845. }
  846. //---------------------------------------------------------------------------
  847. void __fastcall TCustomScpExplorerForm::SetQueueProgress()
  848. {
  849. TTerminalManager::Instance()->QueueStatusUpdated();
  850. if ((FTaskbarList != NULL) && (FProgressForm == NULL))
  851. {
  852. if ((FQueueStatus != NULL) && (FQueueStatus->ActiveAndPendingPrimaryCount > 0))
  853. {
  854. if (FQueueStatus->IsOnlyOneActiveAndNoPending())
  855. {
  856. TFileOperationProgressType * ProgressData = NULL;
  857. if (FQueueStatus->Items[FQueueStatus->DoneCount] != NULL)
  858. {
  859. ProgressData = FQueueStatus->Items[FQueueStatus->DoneCount]->ProgressData;
  860. }
  861. if ((ProgressData != NULL) &&
  862. ProgressData->InProgress)
  863. {
  864. SetTaskbarListProgressValue(ProgressData);
  865. }
  866. else
  867. {
  868. SetTaskbarListProgressState(TBPF_NOPROGRESS);
  869. }
  870. }
  871. else
  872. {
  873. SetTaskbarListProgressState(TBPF_INDETERMINATE);
  874. }
  875. }
  876. else
  877. {
  878. SetTaskbarListProgressState(TBPF_NOPROGRESS);
  879. }
  880. }
  881. UpdateQueueLabel();
  882. }
  883. //---------------------------------------------------------------------------
  884. void __fastcall TCustomScpExplorerForm::UpdateQueueLabel()
  885. {
  886. UnicodeString Caption = LoadStr(QUEUE_CAPTION);
  887. if (FQueueStatus != NULL)
  888. {
  889. int ActiveAndPendingPrimaryCount = FQueueStatus->ActiveAndPendingPrimaryCount;
  890. if (ActiveAndPendingPrimaryCount > 0)
  891. {
  892. Caption = FORMAT("%s (%d)", (Caption, ActiveAndPendingPrimaryCount));
  893. }
  894. }
  895. QueueLabel->Caption = Caption;
  896. }
  897. //---------------------------------------------------------------------------
  898. void __fastcall TCustomScpExplorerForm::UpdateTransferList()
  899. {
  900. TTBXStringList * TransferList = dynamic_cast<TTBXStringList*>(
  901. static_cast<TComponent*>(GetComponent(fcTransferList)));
  902. TTBXDropDownItem * TransferDropDown = dynamic_cast<TTBXDropDownItem*>(
  903. static_cast<TComponent*>(GetComponent(fcTransferDropDown)));
  904. TransferList->Strings->BeginUpdate();
  905. try
  906. {
  907. TransferList->Strings->Assign(GUIConfiguration->CopyParamList->NameList);
  908. TransferList->Strings->Insert(0, StripHotkey(LoadStr(COPY_PARAM_DEFAULT)));
  909. TransferList->ItemIndex = GUIConfiguration->CopyParamIndex + 1;
  910. if (FTransferDropDownHint.IsEmpty())
  911. {
  912. FTransferDropDownHint = TransferDropDown->Hint;
  913. }
  914. // this way we get name for "default" settings (COPY_PARAM_DEFAULT)
  915. UnicodeString Name = TransferList->Strings->Strings[TransferList->ItemIndex];
  916. TransferDropDown->Text = StripHotkey(Name);
  917. TransferDropDown->Hint = FORMAT(L"%s|%s:\n%s",
  918. (FTransferDropDownHint, StripHotkey(Name),
  919. GUIConfiguration->CurrentCopyParam.GetInfoStr(L"; ",
  920. FLAGMASK(HasActiveTerminal(), Terminal->UsableCopyParamAttrs(0).General))));
  921. // update the label, otherwise when it is updated only on the first draw
  922. // of the list, it is drawn "bold" for some reason
  923. FTransferListHoverIndex = TransferList->ItemIndex;
  924. UpdateTransferLabel();
  925. }
  926. __finally
  927. {
  928. TransferList->Strings->EndUpdate();
  929. }
  930. }
  931. //---------------------------------------------------------------------------
  932. void __fastcall TCustomScpExplorerForm::UpdateCustomCommandsToolbar()
  933. {
  934. TTBXToolbar * Toolbar = dynamic_cast<TTBXToolbar *>(GetComponent(fcCustomCommandsBand));
  935. DebugAssert(Toolbar != NULL);
  936. NonVisualDataModule->UpdateCustomCommandsToolbar(Toolbar);
  937. }
  938. //---------------------------------------------------------------------------
  939. void __fastcall TCustomScpExplorerForm::UpdateActions()
  940. {
  941. TForm::UpdateActions();
  942. if (ComponentVisible[fcCustomCommandsBand])
  943. {
  944. UpdateCustomCommandsToolbar();
  945. }
  946. }
  947. //---------------------------------------------------------------------------
  948. void __fastcall TCustomScpExplorerForm::UpdateSessionsPageControlHeight()
  949. {
  950. SessionsPageControl->Height = SessionsPageControl->GetTabsHeight();
  951. }
  952. //---------------------------------------------------------------------------
  953. void __fastcall TCustomScpExplorerForm::UpdateRowSelect(TCustomDirView * DirView)
  954. {
  955. if (DirView->RowSelect != WinConfiguration->FullRowSelect)
  956. {
  957. DirView->RowSelect = WinConfiguration->FullRowSelect;
  958. // Without this, the panel turns gray and unused part of header turns black
  959. DirView->Perform(CM_COLORCHANGED, 0, 0);
  960. }
  961. }
  962. //---------------------------------------------------------------------------
  963. void __fastcall TCustomScpExplorerForm::ConfigurationChanged()
  964. {
  965. Color = GetBtnFaceColor();
  966. DebugAssert(Configuration && RemoteDirView);
  967. RemoteDirView->DDAllowMove = !WinConfiguration->DDDisableMove;
  968. RemoteDirView->DimmHiddenFiles = WinConfiguration->DimmHiddenFiles;
  969. RemoteDirView->ShowHiddenFiles = WinConfiguration->ShowHiddenFiles;
  970. RemoteDirView->FormatSizeBytes = WinConfiguration->FormatSizeBytes;
  971. RemoteDirView->ShowInaccesibleDirectories = WinConfiguration->ShowInaccesibleDirectories;
  972. RemoteDirView->NaturalOrderNumericalSorting = WinConfiguration->NaturalOrderNumericalSorting;
  973. UpdateRowSelect(RemoteDirView);
  974. RemoteDriveView->DDAllowMove = !WinConfiguration->DDDisableMove;
  975. RemoteDriveView->DimmHiddenDirs = WinConfiguration->DimmHiddenFiles;
  976. RemoteDriveView->ShowHiddenDirs = WinConfiguration->ShowHiddenFiles;
  977. RemoteDriveView->ShowInaccesibleDirectories = WinConfiguration->ShowInaccesibleDirectories;
  978. RemoteDriveView->NaturalOrderNumericalSorting = WinConfiguration->NaturalOrderNumericalSorting;
  979. UpdateSessionsPageControlHeight();
  980. SetDockAllowDrag(!WinConfiguration->LockToolbars);
  981. UpdateToolbarDisplayMode();
  982. UpdateTransferList();
  983. if (Terminal != NULL)
  984. {
  985. TransferPresetAutoSelect();
  986. }
  987. UpdateQueueView();
  988. UpdateControls();
  989. // this can be called even before constuctor finishes.
  990. if (FEditorManager != NULL)
  991. {
  992. FEditorManager->ProcessFiles(FileConfigurationChanged, NULL);
  993. }
  994. // this can be called even before constuctor finishes.
  995. if (FLocalEditors != NULL)
  996. {
  997. for (int Index = 0; Index < FLocalEditors->Count; Index++)
  998. {
  999. ReconfigureEditorForm(static_cast<TForm *>(FLocalEditors->Items[Index]));
  1000. }
  1001. }
  1002. if (ComponentVisible[fcCustomCommandsBand])
  1003. {
  1004. UpdateCustomCommandsToolbar();
  1005. }
  1006. // show only when keeping queue items forever,
  1007. // otherwise, it's enough to have it in the context menu
  1008. QueueDeleteAllDoneQueueToolbarItem->Visible =
  1009. WinConfiguration->QueueKeepDoneItems && (WinConfiguration->QueueKeepDoneItemsFor < 0);
  1010. if (FFileColorsCurrent != WinConfiguration->FileColors)
  1011. {
  1012. TFileColorData::LoadList(WinConfiguration->FileColors, FFileColors);
  1013. FFileColorsCurrent = WinConfiguration->FileColors;
  1014. FileColorsChanged();
  1015. }
  1016. }
  1017. //---------------------------------------------------------------------------
  1018. void __fastcall TCustomScpExplorerForm::DoFileColorsChanged(TCustomDirView * DirView)
  1019. {
  1020. DirView->OnGetItemColor = FFileColors.empty() ? TDirViewGetItemColorEvent(NULL) : TDirViewGetItemColorEvent(&DirViewGetItemColor);
  1021. DirView->Invalidate();
  1022. }
  1023. //---------------------------------------------------------------------------
  1024. void __fastcall TCustomScpExplorerForm::FileColorsChanged()
  1025. {
  1026. DoFileColorsChanged(RemoteDirView);
  1027. }
  1028. //---------------------------------------------------------------------------
  1029. void __fastcall TCustomScpExplorerForm::FileConfigurationChanged(
  1030. const UnicodeString FileName, TEditedFileData * /*Data*/, TObject * Token,
  1031. void * /*Arg*/)
  1032. {
  1033. if (Token != NULL)
  1034. {
  1035. TForm * Editor = dynamic_cast<TForm*>(Token);
  1036. DebugAssert(Editor != NULL);
  1037. ReconfigureEditorForm(Editor);
  1038. }
  1039. }
  1040. //---------------------------------------------------------------------------
  1041. void __fastcall TCustomScpExplorerForm::EnableDDTransferConfirmation(TObject * /*Sender*/)
  1042. {
  1043. WinConfiguration->DDTransferConfirmation = asOn;
  1044. }
  1045. //---------------------------------------------------------------------------
  1046. void __fastcall TCustomScpExplorerForm::CopyParamDialogAfter(
  1047. TTransferDirection /*Direction*/, bool /*Temp*/, const UnicodeString & /*TargetDirectory*/)
  1048. {
  1049. // noop
  1050. }
  1051. //---------------------------------------------------------------------------
  1052. bool TCustomScpExplorerForm::GetDoNotShowCopyDialogDefault(bool DragDrop)
  1053. {
  1054. return DragDrop && (WinConfiguration->DDTransferConfirmation == asAuto);
  1055. }
  1056. //---------------------------------------------------------------------------
  1057. void TCustomScpExplorerForm::HandleDoNotShowCopyDialogAgain(bool DragDrop, bool DoNotShowAgain)
  1058. {
  1059. if (DoNotShowAgain)
  1060. {
  1061. if (DragDrop)
  1062. {
  1063. if (WinConfiguration->DDTransferConfirmation == asAuto)
  1064. {
  1065. PopupTrayBalloon(
  1066. NULL, LoadStr(DD_TRANSFER_CONFIRM_OFF2), qtInformation, NULL, 0, EnableDDTransferConfirmation, NULL);
  1067. }
  1068. WinConfiguration->DDTransferConfirmation = asOff;
  1069. }
  1070. else
  1071. {
  1072. WinConfiguration->ConfirmTransferring = false;
  1073. }
  1074. }
  1075. else
  1076. {
  1077. // User explicitly unchecked "do not show again",
  1078. // so show him the dialog the next time
  1079. if (DragDrop && (WinConfiguration->DDTransferConfirmation == asAuto))
  1080. {
  1081. WinConfiguration->DDTransferConfirmation = asOn;
  1082. }
  1083. }
  1084. }
  1085. //---------------------------------------------------------------------------
  1086. bool __fastcall TCustomScpExplorerForm::CopyParamDialog(
  1087. TTransferDirection Direction, TTransferType Type, bool Temp,
  1088. TStrings * FileList, UnicodeString & TargetDirectory, TGUICopyParamType & CopyParam,
  1089. bool Confirm, bool DragDrop, int Options)
  1090. {
  1091. bool Result = true;
  1092. DebugAssert(HasActiveTerminal());
  1093. // these parameters are known in advance
  1094. int Params =
  1095. FLAGMASK(Type == ttMove, cpDelete);
  1096. bool ToTemp = (Temp && (Direction == tdToLocal));
  1097. if (Result && Confirm && WinConfiguration->ConfirmTransferring)
  1098. {
  1099. bool DisableNewerOnly =
  1100. (!Terminal->IsCapable[fcNewerOnlyUpload] && (Direction == tdToRemote)) ||
  1101. ToTemp;
  1102. Options |=
  1103. FLAGMASK(ToTemp, coTemp) |
  1104. FLAGMASK(!Terminal->IsCapable[fcBackgroundTransfers], coDisableQueue) |
  1105. coDoNotShowAgain;
  1106. TUsableCopyParamAttrs UsableCopyParamAttrs = Terminal->UsableCopyParamAttrs(Params);
  1107. int CopyParamAttrs = (Direction == tdToRemote ?
  1108. UsableCopyParamAttrs.Upload : UsableCopyParamAttrs.Download) |
  1109. FLAGMASK(DisableNewerOnly, cpaNoNewerOnly);
  1110. int OutputOptions = FLAGMASK(GetDoNotShowCopyDialogDefault(DragDrop), cooDoNotShowAgain);
  1111. std::unique_ptr<TSessionData> SessionData(SessionDataForCode());
  1112. FlashOnBackground(); // Particularly when called from ClipboardFakeCreated
  1113. Result = DoCopyDialog(Direction == tdToRemote, Type == ttMove,
  1114. FileList, TargetDirectory, &CopyParam, Options, CopyParamAttrs, SessionData.get(), &OutputOptions, -1);
  1115. if (Result)
  1116. {
  1117. HandleDoNotShowCopyDialogAgain(DragDrop, FLAGSET(OutputOptions, cooDoNotShowAgain));
  1118. CopyParamDialogAfter(Direction, Temp, TargetDirectory);
  1119. }
  1120. }
  1121. if (Result && CopyParam.Queue && !ToTemp && Terminal->IsCapable[fcBackgroundTransfers])
  1122. {
  1123. Configuration->Usage->Inc(L"TransfersOnBackground");
  1124. // these parameter are known only after transfer dialog
  1125. Params |=
  1126. (CopyParam.QueueNoConfirmation ? cpNoConfirmation : 0);
  1127. AddQueueItem(Queue, Direction, FileList, TargetDirectory, CopyParam, Params);
  1128. Result = false;
  1129. ClearTransferSourceSelection(Direction);
  1130. }
  1131. return Result;
  1132. }
  1133. //---------------------------------------------------------------------------
  1134. void TCustomScpExplorerForm::ClearOperationSelection(TOperationSide Side)
  1135. {
  1136. if (FOnFileOperationFinished != NULL)
  1137. {
  1138. FOnFileOperationFinished(Side, UnicodeString(), true);
  1139. }
  1140. else
  1141. {
  1142. if (HasDirView[Side])
  1143. {
  1144. DirView(Side)->SelectAll(smNone);
  1145. }
  1146. }
  1147. }
  1148. //---------------------------------------------------------------------------
  1149. void __fastcall TCustomScpExplorerForm::ClearTransferSourceSelection(TTransferDirection Direction)
  1150. {
  1151. TOperationSide Side = ((Direction == tdToRemote) ? osLocal : osRemote);
  1152. ClearOperationSelection(Side);
  1153. }
  1154. //---------------------------------------------------------------------------
  1155. void __fastcall TCustomScpExplorerForm::AddQueueItem(
  1156. TTerminalQueue * Queue, TTransferDirection Direction, TStrings * FileList,
  1157. const UnicodeString TargetDirectory, TGUICopyParamType & CopyParam,
  1158. int Params)
  1159. {
  1160. DebugAssert(Queue != NULL);
  1161. bool SingleFile = false;
  1162. if (FileList->Count == 1)
  1163. {
  1164. if (Direction == tdToRemote)
  1165. {
  1166. UnicodeString FileName = FileList->Strings[0];
  1167. SingleFile = FileExists(ApiPath(FileName));
  1168. }
  1169. else
  1170. {
  1171. TRemoteFile * File = static_cast<TRemoteFile *>(FileList->Objects[0]);
  1172. SingleFile = !File->IsDirectory;
  1173. }
  1174. }
  1175. TQueueItem * QueueItem;
  1176. if (Direction == tdToRemote)
  1177. {
  1178. CopyParam.IncludeFileMask.SetRoots(FileList, TargetDirectory);
  1179. QueueItem = new TUploadQueueItem(Terminal, FileList, TargetDirectory,
  1180. &CopyParam, Params, SingleFile, CopyParam.QueueParallel);
  1181. }
  1182. else
  1183. {
  1184. CopyParam.IncludeFileMask.SetRoots(TargetDirectory, FileList);
  1185. QueueItem = new TDownloadQueueItem(Terminal, FileList, TargetDirectory,
  1186. &CopyParam, Params, SingleFile, CopyParam.QueueParallel);
  1187. }
  1188. AddQueueItem(Queue, QueueItem, Terminal);
  1189. }
  1190. //---------------------------------------------------------------------------
  1191. void __fastcall TCustomScpExplorerForm::AddQueueItem(TTerminalQueue * Queue, TQueueItem * QueueItem, TManagedTerminal * Terminal)
  1192. {
  1193. if (Queue->IsEmpty)
  1194. {
  1195. Terminal->QueueOperationStart = Now();
  1196. }
  1197. Queue->AddItem(QueueItem);
  1198. }
  1199. //---------------------------------------------------------------------------
  1200. void __fastcall TCustomScpExplorerForm::RestoreFormParams()
  1201. {
  1202. }
  1203. //---------------------------------------------------------------------------
  1204. void __fastcall TCustomScpExplorerForm::InitControls()
  1205. {
  1206. DebugAssert(FDocks != NULL);
  1207. for (int Index = 0; Index < ComponentCount; Index++)
  1208. {
  1209. TTBDock * Dock = dynamic_cast<TTBDock *>(Components[Index]);
  1210. if ((Dock != NULL) && (Dock->Tag == 0))
  1211. {
  1212. FDocks->Add(Dock);
  1213. }
  1214. }
  1215. CollectItemsWithTextDisplayMode(this);
  1216. }
  1217. //---------------------------------------------------------------------------
  1218. void __fastcall TCustomScpExplorerForm::RestoreParams()
  1219. {
  1220. ConfigurationChanged();
  1221. QueuePanel->Height =
  1222. LoadDimension(WinConfiguration->QueueView.Height, WinConfiguration->QueueView.HeightPixelsPerInch, this);
  1223. LoadListViewStr(QueueView3, WinConfiguration->QueueView.Layout);
  1224. QueueDock->Visible = WinConfiguration->QueueView.ToolBar;
  1225. QueueLabel->Visible = WinConfiguration->QueueView.Label;
  1226. QueueFileList->Visible = WinConfiguration->QueueView.FileList;
  1227. QueueFileList->Height =
  1228. LoadDimension(WinConfiguration->QueueView.FileListHeight, WinConfiguration->QueueView.FileListHeightPixelsPerInch, this);
  1229. if (QueueFileList->Visible)
  1230. {
  1231. AdjustQueueLayout();
  1232. }
  1233. UpdateCustomCommandsToolbar();
  1234. }
  1235. //---------------------------------------------------------------------------
  1236. void __fastcall TCustomScpExplorerForm::StoreParams()
  1237. {
  1238. int APixelsPerInch = GetControlPixelsPerInch(this);
  1239. WinConfiguration->QueueView.Height = QueuePanel->Height;
  1240. WinConfiguration->QueueView.HeightPixelsPerInch = APixelsPerInch;
  1241. WinConfiguration->QueueView.Layout = GetListViewStr(QueueView3);
  1242. WinConfiguration->QueueView.ToolBar = QueueDock->Visible;
  1243. WinConfiguration->QueueView.Label = QueueLabel->Visible;
  1244. WinConfiguration->QueueView.FileList = QueueFileList->Visible;
  1245. WinConfiguration->QueueView.FileListHeight = QueueFileList->Height;
  1246. WinConfiguration->QueueView.FileListHeightPixelsPerInch = APixelsPerInch;
  1247. }
  1248. //---------------------------------------------------------------------------
  1249. void __fastcall TCustomScpExplorerForm::CreateParams(TCreateParams & Params)
  1250. {
  1251. if (!FFormRestored)
  1252. {
  1253. FFormRestored = true;
  1254. RestoreFormParams();
  1255. }
  1256. TForm::CreateParams(Params);
  1257. }
  1258. //---------------------------------------------------------------------------
  1259. void __fastcall TCustomScpExplorerForm::SetDockAllowDrag(bool value)
  1260. {
  1261. DebugAssert(FDocks != NULL);
  1262. for (int Index = 0; Index < FDocks->Count; Index++)
  1263. {
  1264. static_cast<TTBDock*>(FDocks->Items[Index])->AllowDrag = value;
  1265. }
  1266. }
  1267. //---------------------------------------------------------------------------
  1268. void __fastcall TCustomScpExplorerForm::LoadToolbarsLayoutStr(UnicodeString LayoutStr, UnicodeString ButtonsStr)
  1269. {
  1270. SetDockAllowDrag(true);
  1271. while (!ButtonsStr.IsEmpty())
  1272. {
  1273. UnicodeString S = CutToChar(ButtonsStr, L';', true);
  1274. UnicodeString Name = CutToChar(S, L'=', true);
  1275. for (int ToolbarIndex = 0; ToolbarIndex < ComponentCount; ToolbarIndex++)
  1276. {
  1277. TTBCustomToolbar * Toolbar = dynamic_cast<TTBCustomToolbar *>(Components[ToolbarIndex]);
  1278. if ((Toolbar != NULL) && SameText(Name, GetToolbarKey(Toolbar->Name)))
  1279. {
  1280. while (!S.IsEmpty())
  1281. {
  1282. UnicodeString S2 = CutToChar(S, L',', true);
  1283. UnicodeString Name = CutToChar(S2, L':', true);
  1284. for (int ItemIndex = 0; ItemIndex < Toolbar->Items->Count; ItemIndex++)
  1285. {
  1286. TTBCustomItem * Item = Toolbar->Items->Items[ItemIndex];
  1287. if (SameText(GetToolbarItemName(Item), Name))
  1288. {
  1289. // forward compatibility
  1290. UnicodeString S3 = CutToChar(S2, L':', true);
  1291. bool Visible = StrToIntDef(S3, 0);
  1292. Item->Visible = Visible;
  1293. }
  1294. }
  1295. }
  1296. }
  1297. }
  1298. }
  1299. ::LoadToolbarsLayoutStr(this, LayoutStr);
  1300. SetDockAllowDrag(!WinConfiguration->LockToolbars);
  1301. }
  1302. //---------------------------------------------------------------------------
  1303. UnicodeString __fastcall TCustomScpExplorerForm::GetToolbarsLayoutStr()
  1304. {
  1305. UnicodeString Result;
  1306. SetDockAllowDrag(true);
  1307. Result = ::GetToolbarsLayoutStr(this);
  1308. SetDockAllowDrag(!WinConfiguration->LockToolbars);
  1309. return Result;
  1310. }
  1311. //---------------------------------------------------------------------------
  1312. UnicodeString __fastcall TCustomScpExplorerForm::GetToolbarItemName(TTBCustomItem * Item)
  1313. {
  1314. UnicodeString Result;
  1315. if (Item->Action != NULL)
  1316. {
  1317. Result = Item->Action->Name;
  1318. Result = RemoveSuffix(Result, L"Action", true);
  1319. }
  1320. else
  1321. {
  1322. Result = Item->Name;
  1323. Result = RemoveSuffix(Result, L"SubmenuItem", true);
  1324. Result = RemoveSuffix(Result, L"Item", true);
  1325. }
  1326. return Result;
  1327. }
  1328. //---------------------------------------------------------------------------
  1329. UnicodeString __fastcall TCustomScpExplorerForm::GetToolbarsButtonsStr()
  1330. {
  1331. UnicodeString Result;
  1332. for (int ToolbarIndex = 0; ToolbarIndex < ComponentCount; ToolbarIndex++)
  1333. {
  1334. UnicodeString ToolbarStr;
  1335. TTBCustomToolbar * Toolbar = dynamic_cast<TTBCustomToolbar *>(Components[ToolbarIndex]);
  1336. if ((Toolbar != NULL) && (Toolbar != QueueToolbar))
  1337. {
  1338. for (int ItemIndex = 0; ItemIndex < Toolbar->Items->Count; ItemIndex++)
  1339. {
  1340. TTBCustomItem * Item = Toolbar->Items->Items[ItemIndex];
  1341. // Currently all buttons are visible by default, so we can safely remember all hidden buttons.
  1342. // Once we introduce any buttons that are hidden by default, we would have to remember their initial state
  1343. // and save the changes here only.
  1344. if (NonVisualDataModule->IsCustomizableToolbarItem(Item) && !Item->Visible)
  1345. {
  1346. UnicodeString Name = GetToolbarItemName(Item);
  1347. DebugAssert(Name.Pos(L"TBX") == 0);
  1348. AddToList(ToolbarStr, FORMAT(L"%s:0", (Name)), L",");
  1349. }
  1350. }
  1351. }
  1352. if (!ToolbarStr.IsEmpty())
  1353. {
  1354. AddToList(Result, FORMAT(L"%s=%s", (GetToolbarKey(Toolbar->Name), ToolbarStr)), L";");
  1355. }
  1356. }
  1357. return Result;
  1358. }
  1359. //---------------------------------------------------------------------------
  1360. void __fastcall TCustomScpExplorerForm::CreateProgressForm(TSynchronizeProgress * SynchronizeProgress)
  1361. {
  1362. DebugAssert(FProgressForm == NULL);
  1363. bool AllowSkip = HasActiveTerminal() ? Terminal->IsCapable[fcSkipTransfer] : false;
  1364. bool AllowMoveToQueue = (FTransferResumeList != NULL) || (FDeletedFiles != NULL);
  1365. FProgressForm = new TProgressForm(Application, AllowMoveToQueue, AllowSkip, SynchronizeProgress);
  1366. FProgressForm->DeleteLocalToRecycleBin =
  1367. (WinConfiguration->DeleteToRecycleBin != FAlternativeDelete);
  1368. FProgressForm->DeleteRemoteToRecycleBin =
  1369. HasActiveTerminal() &&
  1370. (Terminal->SessionData->DeleteToRecycleBin != FAlternativeDelete) &&
  1371. !Terminal->SessionData->RecycleBinPath.IsEmpty();
  1372. // As progress window has delayed show now, we need to lock ourselves, (at least) until then
  1373. LockWindow();
  1374. // Actually, do not know what hides the progress once the operation finishes
  1375. // (it is possibly the SetQueueProgress - and we should not rely on that)
  1376. SetTaskbarListProgressState(TBPF_NORMAL);
  1377. }
  1378. //---------------------------------------------------------------------------
  1379. void __fastcall TCustomScpExplorerForm::DestroyProgressForm()
  1380. {
  1381. UnlockWindow();
  1382. SAFE_DESTROY(FProgressForm);
  1383. SetQueueProgress();
  1384. }
  1385. //---------------------------------------------------------------------------
  1386. void __fastcall TCustomScpExplorerForm::FileOperationProgress(
  1387. TFileOperationProgressType & ProgressData)
  1388. {
  1389. // Download to temporary local directory
  1390. if (FPendingTempSpaceWarn && ProgressData.InProgress && ProgressData.TotalSizeSet)
  1391. {
  1392. bool Continue = true;
  1393. FPendingTempSpaceWarn = false;
  1394. DoWarnLackOfTempSpace(ProgressData.Directory, ProgressData.TotalSize, Continue);
  1395. if (!Continue)
  1396. {
  1397. Abort();
  1398. }
  1399. }
  1400. // operation is being executed and we still didn't show up progress form
  1401. if (ProgressData.InProgress && !FProgressForm)
  1402. {
  1403. CreateProgressForm(NULL);
  1404. DebugAssert(FProgressForm->OnceDoneOperation == odoIdle);
  1405. FProgressForm->OnceDoneOperation = ProgressData.InitialOnceDoneOperation;
  1406. }
  1407. // operation is finished (or terminated), so we hide progress form
  1408. else if (!ProgressData.InProgress && (FProgressForm != NULL) && (FProgressForm->SynchronizeProgress == NULL))
  1409. {
  1410. DestroyProgressForm();
  1411. if (ProgressData.Operation == foCalculateSize)
  1412. {
  1413. // When calculating size before transfer, the abort caused by
  1414. // cancel flag set due to "MoveToQueue" below,
  1415. // is not propagated back to us in ExecuteFileOperation,
  1416. // as it is silently swallowed.
  1417. // So we explicitly (re)throw it here.
  1418. if (FMoveToQueue)
  1419. {
  1420. Abort();
  1421. }
  1422. }
  1423. else
  1424. {
  1425. if ((ProgressData.Cancel == csContinue) &&
  1426. !FAutoOperation)
  1427. {
  1428. OperationComplete(ProgressData.StartTime);
  1429. }
  1430. }
  1431. }
  1432. if (FProgressForm &&
  1433. // Can happen only during synchronization, when ending one of the operations
  1434. ProgressData.InProgress)
  1435. {
  1436. FProgressForm->SetProgressData(ProgressData);
  1437. DebugAssert(ProgressData.InProgress);
  1438. SetTaskbarListProgressValue(&ProgressData);
  1439. if (FProgressForm->Cancel > csContinue)
  1440. {
  1441. ProgressData.SetCancelAtLeast(FProgressForm->Cancel);
  1442. if (FProgressForm->Cancel == csCancelFile)
  1443. {
  1444. FProgressForm->ClearCancel();
  1445. }
  1446. // cancel cancels even the move
  1447. FMoveToQueue = false;
  1448. }
  1449. else if (FProgressForm->MoveToQueue)
  1450. {
  1451. FMoveToQueue = true;
  1452. ProgressData.SetCancelAtLeast(csCancel);
  1453. }
  1454. if ((FTransferResumeList != NULL) &&
  1455. ProgressData.InProgress &&
  1456. ProgressData.IsTransfer() &&
  1457. !ProgressData.FullFileName.IsEmpty())
  1458. {
  1459. if ((FTransferResumeList->Count == 0) ||
  1460. (FTransferResumeList->Strings[FTransferResumeList->Count - 1] != ProgressData.FullFileName))
  1461. {
  1462. // note that we do not recognize directories from files here
  1463. FTransferResumeList->Add(ProgressData.FullFileName);
  1464. }
  1465. }
  1466. }
  1467. }
  1468. //---------------------------------------------------------------------------
  1469. void __fastcall TCustomScpExplorerForm::OperationComplete(
  1470. const TDateTime & StartTime)
  1471. {
  1472. if (GUIConfiguration->BeepOnFinish &&
  1473. (Now() - StartTime > GUIConfiguration->BeepOnFinishAfter))
  1474. {
  1475. UnicodeString BeepSound = GUIConfiguration->BeepSound;
  1476. DWORD Sound;
  1477. if (!ExtractFileExt(BeepSound).IsEmpty())
  1478. {
  1479. Sound = SND_FILENAME;
  1480. }
  1481. else
  1482. {
  1483. Sound = SND_ALIAS;
  1484. }
  1485. PlaySound(BeepSound.c_str(), NULL, Sound | SND_ASYNC);
  1486. }
  1487. }
  1488. //---------------------------------------------------------------------------
  1489. void __fastcall TCustomScpExplorerForm::OperationProgress(
  1490. TFileOperationProgressType & ProgressData)
  1491. {
  1492. // For example, when calling TFileOperationProgressType::Suspend in TTerminal::QueryReopen,
  1493. // we cannot recurse back to TTerminal::Idle as that may detect another error, calling "suspend" again.
  1494. TAutoNestingCounter Counter(FDoNotIdleCurrentTerminal);
  1495. FileOperationProgress(ProgressData);
  1496. }
  1497. //---------------------------------------------------------------------------
  1498. UnicodeString __fastcall TCustomScpExplorerForm::GetProgressTitle()
  1499. {
  1500. UnicodeString Result;
  1501. if (FProgressForm != NULL)
  1502. {
  1503. Result = FProgressForm->ProgressStr();
  1504. }
  1505. return Result;
  1506. }
  1507. //---------------------------------------------------------------------------
  1508. bool __fastcall TCustomScpExplorerForm::PanelOperation(TOperationSide /*Side*/,
  1509. bool DragDrop)
  1510. {
  1511. return (!DragDrop && (DropSourceControl == NULL)) ||
  1512. (DropSourceControl == DirView(osOther));
  1513. }
  1514. //---------------------------------------------------------------------------
  1515. TListItem * TCustomScpExplorerForm::VisualiseOperationFinished(TOperationSide Side, const UnicodeString & FileName, bool Unselect)
  1516. {
  1517. TCustomDirView * ADirView = DirView(Side);
  1518. UnicodeString FileNameOnly = ExtractFileName(FileName, !IsSideLocalBrowser(Side));
  1519. TListItem * Item = ADirView->FindFileItem(FileNameOnly);
  1520. // this can happen when local drive is unplugged in the middle of the operation
  1521. if (Item != NULL)
  1522. {
  1523. if (Unselect)
  1524. {
  1525. Item->Selected = false;
  1526. }
  1527. ADirView->MakeProgressVisible(Item);
  1528. }
  1529. return Item;
  1530. }
  1531. //---------------------------------------------------------------------------
  1532. void __fastcall TCustomScpExplorerForm::DoOperationFinished(
  1533. TFileOperation Operation, TOperationSide Side,
  1534. bool /*Temp*/, const UnicodeString & FileName, bool Success,
  1535. TOnceDoneOperation & OnceDoneOperation)
  1536. {
  1537. if (!FAutoOperation)
  1538. {
  1539. // no selection on "/upload", form serves only as event handler
  1540. // (it is not displayed)
  1541. if (PanelOperation(Side, FDragDropOperation) && Visible &&
  1542. ((Operation == FPrimaryOperation) ||
  1543. ((Operation != foCalculateSize) && (Operation != foGetProperties) && (Operation != foCalculateChecksum))))
  1544. {
  1545. TOperationSide DViewSide;
  1546. if ((FProgressSide != osCurrent) &&
  1547. IsLocalBrowserMode() && (Side == osLocal)) // Only to limit the impact
  1548. {
  1549. // assume the operation is over the focused panel
  1550. DViewSide = FProgressSide;
  1551. }
  1552. else
  1553. {
  1554. DViewSide = Side;
  1555. }
  1556. if (FOnFileOperationFinished != NULL)
  1557. {
  1558. FOnFileOperationFinished(DViewSide, FileName, Success);
  1559. }
  1560. else
  1561. {
  1562. VisualiseOperationFinished(DViewSide, FileName, Success);
  1563. }
  1564. }
  1565. if (TFileOperationProgressType::IsTransferOperation(Operation))
  1566. {
  1567. if (DebugAlwaysTrue(!IsLocalBrowserMode()))
  1568. {
  1569. if (Side == osLocal)
  1570. {
  1571. Configuration->Usage->Inc(L"UploadedFiles");
  1572. }
  1573. else
  1574. {
  1575. Configuration->Usage->Inc(L"DownloadedFiles");
  1576. }
  1577. }
  1578. }
  1579. }
  1580. if (Success && (FSynchronizeController != NULL))
  1581. {
  1582. DebugAssert(!IsSideLocalBrowser(osRemote));
  1583. if (Operation == foCopy)
  1584. {
  1585. DebugAssert(Side == osLocal);
  1586. FSynchronizeController->LogOperation(soUpload, FileName);
  1587. }
  1588. else if (Operation == foDelete)
  1589. {
  1590. DebugAssert(Side == osRemote);
  1591. FSynchronizeController->LogOperation(soDelete, FileName);
  1592. }
  1593. }
  1594. if (FProgressForm)
  1595. {
  1596. OnceDoneOperation = FProgressForm->OnceDoneOperation;
  1597. }
  1598. }
  1599. //---------------------------------------------------------------------------
  1600. void __fastcall TCustomScpExplorerForm::OperationFinished(
  1601. TFileOperation Operation, TOperationSide Side,
  1602. bool Temp, const UnicodeString & FileName, Boolean Success,
  1603. TOnceDoneOperation & OnceDoneOperation)
  1604. {
  1605. TAutoNestingCounter Counter(FDoNotIdleCurrentTerminal);
  1606. DoOperationFinished(Operation, Side, Temp, FileName, Success,
  1607. OnceDoneOperation);
  1608. }
  1609. //---------------------------------------------------------------------------
  1610. bool TCustomScpExplorerForm::IsLocalBrowserMode()
  1611. {
  1612. return false;
  1613. }
  1614. //---------------------------------------------------------------------------
  1615. bool TCustomScpExplorerForm::SupportsLocalBrowser()
  1616. {
  1617. return false;
  1618. }
  1619. //---------------------------------------------------------------------------
  1620. bool TCustomScpExplorerForm::IsSideLocalBrowser(TOperationSide)
  1621. {
  1622. return false;
  1623. }
  1624. //---------------------------------------------------------------------------
  1625. TCustomDirView * TCustomScpExplorerForm::GetCurrentLocalBrowser()
  1626. {
  1627. return NULL;
  1628. }
  1629. //---------------------------------------------------------------------------
  1630. TCustomDirView * __fastcall TCustomScpExplorerForm::DirView(TOperationSide Side)
  1631. {
  1632. DebugAssert(GetSide(Side) == osRemote);
  1633. DebugUsedParam(Side);
  1634. return RemoteDirView;
  1635. }
  1636. //---------------------------------------------------------------------------
  1637. TCustomDriveView * __fastcall TCustomScpExplorerForm::DriveView(TOperationSide Side)
  1638. {
  1639. DebugAssert(GetSide(Side) == osRemote);
  1640. DebugUsedParam(Side);
  1641. return RemoteDriveView;
  1642. }
  1643. //---------------------------------------------------------------------------
  1644. bool __fastcall TCustomScpExplorerForm::DirViewEnabled(TOperationSide Side)
  1645. {
  1646. DebugAssert(GetSide(Side) == osRemote);
  1647. DebugUsedParam(Side);
  1648. return HasActiveTerminal();
  1649. }
  1650. //---------------------------------------------------------------------------
  1651. bool __fastcall TCustomScpExplorerForm::GetEnableFocusedOperation(
  1652. TOperationSide Side, int FilesOnly)
  1653. {
  1654. return DirView(Side)->AnyFileSelected(true, (FilesOnly != 0), true);
  1655. }
  1656. //---------------------------------------------------------------------------
  1657. bool __fastcall TCustomScpExplorerForm::GetEnableSelectedOperation(
  1658. TOperationSide Side, int FilesOnly)
  1659. {
  1660. return DirView(Side)->AnyFileSelected(false, (FilesOnly != 0), true);
  1661. }
  1662. //---------------------------------------------------------------------------
  1663. struct THistoryItemData
  1664. {
  1665. short int Side;
  1666. short int Index;
  1667. };
  1668. //---------------------------------------------------------------------------
  1669. void __fastcall TCustomScpExplorerForm::HistoryGo(TOperationSide Side, int Index)
  1670. {
  1671. DirView(Side)->HistoryGo(Index);
  1672. }
  1673. //---------------------------------------------------------------------------
  1674. void __fastcall TCustomScpExplorerForm::HistoryItemClick(System::TObject* Sender)
  1675. {
  1676. TTBCustomItem * Item = dynamic_cast<TTBCustomItem *>(Sender);
  1677. THistoryItemData Data = *reinterpret_cast<THistoryItemData*>(&(Item->Tag));
  1678. HistoryGo((TOperationSide)Data.Side, Data.Index);
  1679. }
  1680. //---------------------------------------------------------------------------
  1681. void __fastcall TCustomScpExplorerForm::UpdateHistoryMenu(TOperationSide Side,
  1682. bool Back)
  1683. {
  1684. if (FHistoryMenu[Side == osLocal][Back] != NULL)
  1685. {
  1686. TCustomDirView * DView = DirView(Side);
  1687. TTBXPopupMenu * Menu = FHistoryMenu[Side == osLocal][Back];
  1688. int ICount = Back ? DView->BackCount : DView->ForwardCount;
  1689. if (ICount > 10)
  1690. {
  1691. ICount = 10;
  1692. }
  1693. Menu->Items->Clear();
  1694. THistoryItemData Data;
  1695. Data.Side = Side;
  1696. for (short int i = 1; i <= ICount; i++)
  1697. {
  1698. TTBCustomItem * Item = new TTBXItem(Menu);
  1699. Data.Index = static_cast<short int>(i * (Back ? -1 : 1));
  1700. Item->Caption = MinimizeName(DView->HistoryPath[Data.Index], 50, !IsSideLocalBrowser(Side));
  1701. Item->Hint = DView->HistoryPath[Data.Index];
  1702. DebugAssert(sizeof(int) == sizeof(THistoryItemData));
  1703. Item->Tag = *reinterpret_cast<int*>(&Data);
  1704. Item->OnClick = HistoryItemClick;
  1705. Menu->Items->Add(Item);
  1706. }
  1707. }
  1708. }
  1709. //---------------------------------------------------------------------------
  1710. TTBXPopupMenu * __fastcall TCustomScpExplorerForm::HistoryMenu(
  1711. TOperationSide Side, bool Back)
  1712. {
  1713. if (FHistoryMenu[Side == osLocal][Back] == NULL)
  1714. {
  1715. // workaround
  1716. // In Pascal the size of TTBXPopupMenu is 132, in C++ 136,
  1717. // operator new allocates memory in Pascal code, but calls inline
  1718. // contructor in C++, leading in problems, the function does
  1719. // both in Pascal code
  1720. FHistoryMenu[Side == osLocal][Back] = CreateTBXPopupMenu(this);
  1721. UpdateHistoryMenu(Side, Back);
  1722. }
  1723. return FHistoryMenu[Side == osLocal][Back];
  1724. }
  1725. //---------------------------------------------------------------------------
  1726. void __fastcall TCustomScpExplorerForm::DirViewHistoryChange(
  1727. TCustomDirView *Sender)
  1728. {
  1729. TOperationSide Side = (Sender == DirView(osOther) ? osOther : osLocal);
  1730. UpdateHistoryMenu(Side, true);
  1731. UpdateHistoryMenu(Side, false);
  1732. }
  1733. //---------------------------------------------------------------------------
  1734. bool __fastcall TCustomScpExplorerForm::CustomCommandRemoteAllowed()
  1735. {
  1736. // remote custom commands can be executed only if the server supports shell commands
  1737. // or have secondary shell
  1738. return HasActiveTerminal() && (Terminal->IsCapable[fcSecondaryShell] || Terminal->IsCapable[fcShellAnyCommand]);
  1739. }
  1740. //---------------------------------------------------------------------------
  1741. int __fastcall TCustomScpExplorerForm::CustomCommandState(
  1742. const TCustomCommandType & Command, bool OnFocused, TCustomCommandListType ListType)
  1743. {
  1744. // -1 = hidden, 0 = disabled, 1 = enabled
  1745. int Result;
  1746. TFileCustomCommand RemoteCustomCommand;
  1747. TLocalCustomCommand LocalCustomCommand;
  1748. TFileCustomCommand * NonInteractiveCustomCommand =
  1749. FLAGCLEAR(Command.Params, ccLocal) ? &RemoteCustomCommand : &LocalCustomCommand;
  1750. TInteractiveCustomCommand InteractiveCustomCommand(NonInteractiveCustomCommand);
  1751. UnicodeString Cmd = InteractiveCustomCommand.Complete(Command.Command, false);
  1752. if (FLAGCLEAR(Command.Params, ccLocal))
  1753. {
  1754. int AllowedState = CustomCommandRemoteAllowed() ? 1 : 0;
  1755. // custom command that does not operate with files can be executed anytime ...
  1756. if (!NonInteractiveCustomCommand->IsFileCommand(Cmd))
  1757. {
  1758. // ... but do not show such command in remote file menu
  1759. if ((ListType == ccltAll) || (ListType == ccltNonFile))
  1760. {
  1761. Result = AllowedState;
  1762. }
  1763. else
  1764. {
  1765. Result = -1;
  1766. }
  1767. }
  1768. else
  1769. {
  1770. if ((ListType == ccltAll) || ((ListType == ccltFile) && !IsSideLocalBrowser(FCurrentSide)))
  1771. {
  1772. Result = (DirView(FCurrentSide)->AnyFileSelected(OnFocused, false, true)) ? AllowedState : 0;
  1773. }
  1774. else
  1775. {
  1776. Result = -1;
  1777. }
  1778. }
  1779. }
  1780. else
  1781. {
  1782. // custom command that does not operate with files can be executed anytime
  1783. if (!NonInteractiveCustomCommand->IsFileCommand(Cmd))
  1784. {
  1785. Result = ((ListType == ccltAll) || (ListType == ccltNonFile)) ? 1 : -1;
  1786. }
  1787. else if (LocalCustomCommand.HasLocalFileName(Cmd))
  1788. {
  1789. if ((ListType == ccltAll) || (ListType == ccltFile))
  1790. {
  1791. // special case is "diff"-style command that can be executed over any side,
  1792. // if we have both sides
  1793. Result =
  1794. // Cannot have focus on both panels, so we have to call AnyFileSelected
  1795. // directly (instead of EnableSelectedOperation) to pass
  1796. // false to FocusedFileOnlyWhenFocused when panel is inactive.
  1797. ((HasDirView[osLocal] && !IsSideLocalBrowser(osRemote) &&
  1798. DirView(osLocal)->AnyFileSelected(false, false, (FCurrentSide == osLocal))) &&
  1799. DirView(osRemote)->AnyFileSelected(false, false, (FCurrentSide == osRemote))) ? 1 : 0;
  1800. }
  1801. else if (ListType == ccltBoth)
  1802. {
  1803. DebugAssert(!IsLocalBrowserMode());
  1804. Result = 1;
  1805. }
  1806. else
  1807. {
  1808. Result = -1;
  1809. }
  1810. }
  1811. else
  1812. {
  1813. if ((ListType == ccltAll) ||
  1814. ((ListType == ccltFile) && (!IsSideLocalBrowser(FCurrentSide) || FLAGCLEAR(Command.Params, ccRemoteFiles))))
  1815. {
  1816. Result = DirView(FCurrentSide)->AnyFileSelected(OnFocused, false, true) ? 1 : 0;
  1817. }
  1818. else
  1819. {
  1820. Result = -1;
  1821. }
  1822. }
  1823. if ((Result > 0) &&
  1824. LocalCustomCommand.IsSessionCommand(Cmd) &&
  1825. !HasActiveTerminal())
  1826. {
  1827. Result = 0;
  1828. }
  1829. }
  1830. return Result;
  1831. }
  1832. //---------------------------------------------------------------------------
  1833. void __fastcall TCustomScpExplorerForm::RemoteCustomCommand(
  1834. TStrings * FileList, const TCustomCommandType & ACommand,
  1835. const TCustomCommandData & Data, const UnicodeString & CommandCommand)
  1836. {
  1837. if (EnsureCommandSessionFallback(fcShellAnyCommand))
  1838. {
  1839. TRemoteCustomCommand RemoteCustomCommand(Data, Terminal->CurrentDirectory);
  1840. TWinInteractiveCustomCommand InteractiveCustomCommand(
  1841. &RemoteCustomCommand, ACommand.Name, ACommand.HomePage);
  1842. UnicodeString Command = InteractiveCustomCommand.Complete(CommandCommand, false);
  1843. Configuration->Usage->Inc(L"RemoteCustomCommandRuns2");
  1844. bool Capture =
  1845. FLAGSET(ACommand.Params, ccShowResults) ||
  1846. FLAGSET(ACommand.Params, ccCopyResults) ||
  1847. FLAGSET(ACommand.Params, ccShowResultsInMsgBox);
  1848. TCaptureOutputEvent OutputEvent = NULL;
  1849. DebugAssert(FCapturedLog == NULL);
  1850. if (Capture)
  1851. {
  1852. FCapturedLog = new TStringList();
  1853. OutputEvent = TerminalCaptureLog;
  1854. }
  1855. try
  1856. {
  1857. if (!RemoteCustomCommand.IsFileCommand(Command))
  1858. {
  1859. Terminal->AnyCommand(RemoteCustomCommand.Complete(Command, true),
  1860. OutputEvent);
  1861. }
  1862. else
  1863. {
  1864. Terminal->CustomCommandOnFiles(Command, ACommand.Params, FileList, OutputEvent);
  1865. }
  1866. if ((FCapturedLog != NULL) && (FCapturedLog->Count > 0))
  1867. {
  1868. if (FLAGSET(ACommand.Params, ccCopyResults))
  1869. {
  1870. CopyToClipboard(FCapturedLog);
  1871. }
  1872. if (FLAGSET(ACommand.Params, ccShowResults))
  1873. {
  1874. DoConsoleDialog(Terminal, L"", FCapturedLog);
  1875. }
  1876. else if (FLAGSET(ACommand.Params, ccShowResultsInMsgBox))
  1877. {
  1878. MessageDialog(FCapturedLog->Text, qtInformation, qaOK);
  1879. }
  1880. }
  1881. }
  1882. __finally
  1883. {
  1884. SAFE_DESTROY(FCapturedLog);
  1885. }
  1886. }
  1887. }
  1888. //---------------------------------------------------------------------------
  1889. void __fastcall TCustomScpExplorerForm::LocalCustomCommandPure(
  1890. TStrings * FileList, const TCustomCommandType & ACommand, const UnicodeString & Command, TStrings * ALocalFileList,
  1891. const TCustomCommandData & Data, bool LocalFileCommand, bool FileListCommand, UnicodeString * POutput)
  1892. {
  1893. DebugAssert(!IsLocalBrowserMode());
  1894. TStrings * LocalFileList = NULL;
  1895. TStrings * RemoteFileList = NULL;
  1896. TStrings * RemoteFileListFull = NULL;
  1897. try
  1898. {
  1899. if (LocalFileCommand)
  1900. {
  1901. if (ALocalFileList == NULL)
  1902. {
  1903. DebugAssert(HasDirView[osLocal]);
  1904. // Cannot have focus on both panels, so we have to call AnyFileSelected
  1905. // directly (instead of EnableSelectedOperation) to pass
  1906. // false to FocusedFileOnlyWhenFocused
  1907. DebugAssert(DirView(osLocal)->AnyFileSelected(false, false, false));
  1908. LocalFileList = DirView(osLocal)->CreateFileList(false, true, NULL);
  1909. }
  1910. else
  1911. {
  1912. LocalFileList = ALocalFileList;
  1913. }
  1914. if (FileListCommand)
  1915. {
  1916. if (LocalFileList->Count != 1)
  1917. {
  1918. throw Exception(LoadStr(CUSTOM_COMMAND_SELECTED_UNMATCH1));
  1919. }
  1920. }
  1921. else
  1922. {
  1923. if ((LocalFileList->Count != 1) &&
  1924. (FileList->Count != 1) &&
  1925. (LocalFileList->Count != FileList->Count))
  1926. {
  1927. throw Exception(LoadStr(CUSTOM_COMMAND_SELECTED_UNMATCH));
  1928. }
  1929. }
  1930. }
  1931. UnicodeString RootTempDir;
  1932. UnicodeString TempDir;
  1933. bool RemoteFiles = FLAGSET(ACommand.Params, ccRemoteFiles);
  1934. if (!RemoteFiles)
  1935. {
  1936. TemporarilyDownloadFiles(FileList, false, RootTempDir, TempDir, false, true);
  1937. }
  1938. try
  1939. {
  1940. TDateTimes RemoteFileTimes;
  1941. if (RemoteFiles)
  1942. {
  1943. RemoteFileList = FileList;
  1944. }
  1945. else
  1946. {
  1947. RemoteFileList = new TStringList();
  1948. TMakeLocalFileListParams MakeFileListParam;
  1949. MakeFileListParam.FileList = RemoteFileList;
  1950. MakeFileListParam.FileTimes = &RemoteFileTimes;
  1951. MakeFileListParam.IncludeDirs = FLAGSET(ACommand.Params, ccApplyToDirectories);
  1952. MakeFileListParam.Recursive =
  1953. FLAGSET(ACommand.Params, ccRecursive) && !FileListCommand;
  1954. ProcessLocalDirectory(TempDir, Terminal->MakeLocalFileList, &MakeFileListParam);
  1955. if (!MakeFileListParam.Recursive)
  1956. {
  1957. RemoteFileTimes.clear();
  1958. RemoteFileListFull = new TStringList();
  1959. MakeFileListParam.FileList = RemoteFileListFull;
  1960. MakeFileListParam.Recursive = true;
  1961. MakeFileListParam.IncludeDirs = false;
  1962. ProcessLocalDirectory(TempDir, Terminal->MakeLocalFileList, &MakeFileListParam);
  1963. }
  1964. else
  1965. {
  1966. RemoteFileListFull = RemoteFileList;
  1967. }
  1968. }
  1969. bool NonBlocking = FileListCommand && RemoteFiles && !POutput;
  1970. TFileOperationProgressType Progress(&OperationProgress, &OperationFinished);
  1971. if (!NonBlocking)
  1972. {
  1973. int Count;
  1974. if (FileListCommand)
  1975. {
  1976. Count = 1;
  1977. }
  1978. else if (LocalFileCommand)
  1979. {
  1980. Count = std::max(LocalFileList->Count, RemoteFileList->Count);
  1981. }
  1982. else
  1983. {
  1984. Count = RemoteFileList->Count;
  1985. }
  1986. Progress.Start(foCustomCommand, osRemote, Count);
  1987. DebugAssert(FProgressForm != NULL);
  1988. FProgressForm->ReadOnly = true;
  1989. }
  1990. try
  1991. {
  1992. if (FileListCommand)
  1993. {
  1994. UnicodeString LocalFile;
  1995. // MakeFileList does not delimit filenames
  1996. UnicodeString FileList = MakeFileList(RemoteFileList);
  1997. if (LocalFileCommand)
  1998. {
  1999. DebugAssert(LocalFileList->Count == 1);
  2000. LocalFile = LocalFileList->Strings[0];
  2001. }
  2002. TLocalCustomCommand CustomCommand(Data,
  2003. Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(), L"", LocalFile, FileList);
  2004. UnicodeString ShellCommand = CustomCommand.Complete(Command, true);
  2005. if (NonBlocking)
  2006. {
  2007. DebugAssert(!POutput);
  2008. ExecuteShellChecked(ShellCommand);
  2009. }
  2010. else
  2011. {
  2012. ExecuteProcessCheckedAndWait(ShellCommand, HelpKeyword, POutput);
  2013. }
  2014. }
  2015. else if (LocalFileCommand)
  2016. {
  2017. if (LocalFileList->Count == 1)
  2018. {
  2019. UnicodeString LocalFile = LocalFileList->Strings[0];
  2020. for (int Index = 0; Index < RemoteFileList->Count; Index++)
  2021. {
  2022. UnicodeString FileName = RemoteFileList->Strings[Index];
  2023. UnicodeString FileNameForProgress = RemoteFiles ? FileName : ExtractFileName(FileName);
  2024. Progress.SetFile(FileNameForProgress);
  2025. TLocalCustomCommand CustomCommand(Data,
  2026. Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(), FileName, LocalFile, L"");
  2027. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  2028. TOnceDoneOperation OnceDoneOperation;
  2029. // Do not unselect anything, as it won't work with two panels anyway
  2030. Progress.Finish(UnicodeString(), true, OnceDoneOperation);
  2031. }
  2032. }
  2033. else if (RemoteFileList->Count == 1)
  2034. {
  2035. UnicodeString FileName = RemoteFileList->Strings[0];
  2036. for (int Index = 0; Index < LocalFileList->Count; Index++)
  2037. {
  2038. UnicodeString LocalFileName = LocalFileList->Strings[Index];
  2039. UnicodeString LocalFileNameOnly = ExtractFileName(LocalFileName);
  2040. Progress.SetFile(LocalFileNameOnly);
  2041. TLocalCustomCommand CustomCommand(
  2042. Data, Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(),
  2043. FileName, LocalFileName, L"");
  2044. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  2045. TOnceDoneOperation OnceDoneOperation;
  2046. // Do not unselect anything, as it won't work with two panels anyway
  2047. Progress.Finish(UnicodeString(), true, OnceDoneOperation);
  2048. }
  2049. }
  2050. else
  2051. {
  2052. if (LocalFileList->Count != RemoteFileList->Count)
  2053. {
  2054. throw Exception(LoadStr(CUSTOM_COMMAND_PAIRS_DOWNLOAD_FAILED));
  2055. }
  2056. for (int Index = 0; Index < LocalFileList->Count; Index++)
  2057. {
  2058. UnicodeString FileName = RemoteFileList->Strings[Index];
  2059. UnicodeString LocalFileName = LocalFileList->Strings[Index];
  2060. UnicodeString LocalFileNameOnly = ExtractFileName(LocalFileName);
  2061. Progress.SetFile(LocalFileNameOnly); // sic
  2062. TLocalCustomCommand CustomCommand(
  2063. Data, Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(),
  2064. FileName, LocalFileName, L"");
  2065. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  2066. TOnceDoneOperation OnceDoneOperation;
  2067. // Do not unselect anything, as it won't work with two panels anyway
  2068. Progress.Finish(UnicodeString(), true, OnceDoneOperation);
  2069. }
  2070. }
  2071. }
  2072. else
  2073. {
  2074. for (int Index = 0; Index < RemoteFileList->Count; Index++)
  2075. {
  2076. UnicodeString FileName = RemoteFileList->Strings[Index];
  2077. UnicodeString FileNameForProgress = RemoteFiles ? FileName : ExtractFileName(FileName);
  2078. Progress.SetFile(FileNameForProgress);
  2079. TLocalCustomCommand CustomCommand(Data,
  2080. Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(),
  2081. FileName, L"", L"");
  2082. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  2083. TOnceDoneOperation OnceDoneOperation;
  2084. Progress.Finish(FileNameForProgress, true, OnceDoneOperation);
  2085. }
  2086. }
  2087. }
  2088. __finally
  2089. {
  2090. if (!NonBlocking)
  2091. {
  2092. Progress.Stop();
  2093. }
  2094. }
  2095. DebugAssert(!FAutoOperation);
  2096. if (!RemoteFiles)
  2097. {
  2098. TempDir = IncludeTrailingBackslash(TempDir);
  2099. for (int Index = 0; Index < RemoteFileListFull->Count; Index++)
  2100. {
  2101. UnicodeString FileName = RemoteFileListFull->Strings[Index];
  2102. if (DebugAlwaysTrue(SameText(TempDir, FileName.SubString(1, TempDir.Length()))) &&
  2103. // Skip directories, process only nested files.
  2104. // The check is redundant as FileAge fails for directories anyway.
  2105. !DirectoryExists(FileName))
  2106. {
  2107. UnicodeString RemoteDir =
  2108. UnixExtractFileDir(
  2109. UnixIncludeTrailingBackslash(Terminal->CurrentDirectory) +
  2110. ToUnixPath(FileName.SubString(TempDir.Length() + 1, FileName.Length() - TempDir.Length())));
  2111. TDateTime NewTime;
  2112. if (FileAge(FileName, NewTime) &&
  2113. (NewTime != RemoteFileTimes[Index]))
  2114. {
  2115. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  2116. TemporaryFileCopyParam(CopyParam);
  2117. CopyParam.FileMask = L"";
  2118. FAutoOperation = true;
  2119. std::unique_ptr<TStrings> TemporaryFilesList(new TStringList());
  2120. TemporaryFilesList->Add(FileName);
  2121. Terminal->CopyToRemote(TemporaryFilesList.get(), RemoteDir, &CopyParam, cpTemporary, NULL);
  2122. }
  2123. }
  2124. }
  2125. }
  2126. }
  2127. __finally
  2128. {
  2129. FAutoOperation = false;
  2130. if (!RootTempDir.IsEmpty() && DebugAlwaysTrue(!RemoteFiles))
  2131. {
  2132. RecursiveDeleteFile(ExcludeTrailingBackslash(RootTempDir), false);
  2133. }
  2134. }
  2135. }
  2136. __finally
  2137. {
  2138. if (RemoteFileList != FileList)
  2139. {
  2140. delete RemoteFileList;
  2141. }
  2142. if (LocalFileList != ALocalFileList)
  2143. {
  2144. delete LocalFileList;
  2145. }
  2146. if (RemoteFileListFull != RemoteFileList)
  2147. {
  2148. delete RemoteFileListFull;
  2149. }
  2150. }
  2151. }
  2152. //---------------------------------------------------------------------------
  2153. void __fastcall TCustomScpExplorerForm::LocalCustomCommandWithLocalFiles(
  2154. const TCustomCommandType & ACommand, const UnicodeString & Command, const TCustomCommandData & Data,
  2155. bool FileListCommand, UnicodeString * POutput)
  2156. {
  2157. std::unique_ptr<TStrings> SelectedFileList(DirView(osCurrent)->CreateFileList(false, true, NULL));
  2158. std::unique_ptr<TStrings> LocalFileList(new TStringList());
  2159. for (int Index = 0; Index < SelectedFileList->Count; Index++)
  2160. {
  2161. UnicodeString FileName = SelectedFileList->Strings[Index];
  2162. if (DirectoryExists(FileName))
  2163. {
  2164. if (FLAGSET(ACommand.Params, ccApplyToDirectories))
  2165. {
  2166. LocalFileList->Add(FileName);
  2167. }
  2168. if (FLAGSET(ACommand.Params, ccRecursive))
  2169. {
  2170. TMakeLocalFileListParams MakeFileListParam;
  2171. MakeFileListParam.FileList = LocalFileList.get();
  2172. MakeFileListParam.FileTimes = NULL;
  2173. MakeFileListParam.IncludeDirs = FLAGSET(ACommand.Params, ccApplyToDirectories);
  2174. MakeFileListParam.Recursive = true;
  2175. ProcessLocalDirectory(FileName, Terminal->MakeLocalFileList, &MakeFileListParam);
  2176. }
  2177. }
  2178. else
  2179. {
  2180. LocalFileList->Add(FileName);
  2181. }
  2182. }
  2183. UnicodeString RemotePath;
  2184. if (Terminal != NULL)
  2185. {
  2186. RemotePath = Terminal->CurrentDirectory;
  2187. }
  2188. if (FileListCommand)
  2189. {
  2190. UnicodeString FileList = MakeFileList(LocalFileList.get());
  2191. TLocalCustomCommand CustomCommand(
  2192. Data, RemotePath, DefaultDownloadTargetDirectory(),
  2193. L"", L"", FileList);
  2194. ExecuteProcessChecked(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  2195. }
  2196. else
  2197. {
  2198. TFileOperationProgressType Progress(&OperationProgress, &OperationFinished);
  2199. Progress.Start(foCustomCommand, osLocal, LocalFileList->Count);
  2200. DebugAssert(FProgressForm != NULL);
  2201. FProgressForm->ReadOnly = true;
  2202. try
  2203. {
  2204. for (int Index = 0; Index < LocalFileList->Count; Index++)
  2205. {
  2206. UnicodeString FileName = LocalFileList->Strings[Index];
  2207. Progress.SetFile(FileName);
  2208. TLocalCustomCommand CustomCommand(
  2209. Data, RemotePath, DefaultDownloadTargetDirectory(),
  2210. FileName, L"", L"");
  2211. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  2212. TOnceDoneOperation OnceDoneOperation;
  2213. Progress.Finish(FileName, true, OnceDoneOperation);
  2214. }
  2215. }
  2216. __finally
  2217. {
  2218. Progress.Stop();
  2219. }
  2220. }
  2221. }
  2222. //---------------------------------------------------------------------------
  2223. void __fastcall TCustomScpExplorerForm::LocalCustomCommand(TStrings * FileList,
  2224. const TCustomCommandType & ACommand, TStrings * ALocalFileList,
  2225. const TCustomCommandData & Data, const UnicodeString & CommandCommand)
  2226. {
  2227. UnicodeString RemotePath = (Terminal != NULL) ? Terminal->CurrentDirectory : UnicodeString();
  2228. TLocalCustomCommand LocalCustomCommand(Data, RemotePath, DefaultDownloadTargetDirectory());
  2229. TWinInteractiveCustomCommand InteractiveCustomCommand(
  2230. &LocalCustomCommand, ACommand.Name, ACommand.HomePage);
  2231. UnicodeString Command = InteractiveCustomCommand.Complete(CommandCommand, false);
  2232. bool FileListCommand = LocalCustomCommand.IsFileListCommand(Command);
  2233. bool LocalFileCommand = LocalCustomCommand.HasLocalFileName(Command);
  2234. Configuration->Usage->Inc(L"LocalCustomCommandRuns2");
  2235. std::unique_ptr<UnicodeString> POutput;
  2236. if (FLAGSET(ACommand.Params, ccShowResultsInMsgBox) ||
  2237. FLAGSET(ACommand.Params, ccCopyResults))
  2238. {
  2239. POutput.reset(new UnicodeString());
  2240. }
  2241. TValueRestorer<TOperationSide> ProgressSideRestorer(FProgressSide);
  2242. FProgressSide = FCurrentSide;
  2243. if (!LocalCustomCommand.IsFileCommand(Command))
  2244. {
  2245. ExecuteProcessChecked(LocalCustomCommand.Complete(Command, true), HelpKeyword, POutput.get());
  2246. }
  2247. // remote files?
  2248. else if (!IsSideLocalBrowser(FCurrentSide) || LocalFileCommand)
  2249. {
  2250. LocalCustomCommandPure(FileList, ACommand, Command, ALocalFileList, Data, LocalFileCommand, FileListCommand, POutput.get());
  2251. }
  2252. // local files
  2253. else
  2254. {
  2255. LocalCustomCommandWithLocalFiles(ACommand, Command, Data, FileListCommand, POutput.get());
  2256. }
  2257. if (POutput.get() != NULL)
  2258. {
  2259. // If the output is single-line, we do not want the trailing CRLF, as the CopyToClipboard(TStrings *) does
  2260. int P = POutput->Pos(sLineBreak);
  2261. if (P == POutput->Length() - static_cast<int>(strlen(sLineBreak)) + 1)
  2262. {
  2263. POutput->SetLength(P - 1);
  2264. }
  2265. // Copy even empty output to clipboard
  2266. if (FLAGSET(ACommand.Params, ccCopyResults))
  2267. {
  2268. CopyToClipboard(*POutput);
  2269. }
  2270. // But do not show an empty message box.
  2271. // This way the ShowResultsInMsgBox can be used to suppress a console window of commands with no output
  2272. if (FLAGSET(ACommand.Params, ccShowResultsInMsgBox) &&
  2273. !POutput->IsEmpty())
  2274. {
  2275. TClipboardHandler ClipboardHandler;
  2276. ClipboardHandler.Text = *POutput;
  2277. TMessageParams Params;
  2278. TQueryButtonAlias Aliases[1];
  2279. Aliases[0].Button = qaRetry;
  2280. Aliases[0].Alias = LoadStr(EDIT_COPY);
  2281. Aliases[0].OnSubmit = &ClipboardHandler.Copy;
  2282. Params.Aliases = Aliases;
  2283. Params.AliasesCount = LENOF(Aliases);
  2284. MessageDialog(*POutput, qtInformation, qaOK | qaRetry, HelpKeyword, &Params);
  2285. }
  2286. }
  2287. }
  2288. //---------------------------------------------------------------------------
  2289. void __fastcall TCustomScpExplorerForm::CustomCommand(TStrings * FileList,
  2290. const TCustomCommandType & ACommand, TStrings * ALocalFileList)
  2291. {
  2292. TCustomCommandData Data;
  2293. UnicodeString Site;
  2294. UnicodeString RemotePath;
  2295. if (HasActiveTerminal())
  2296. {
  2297. std::unique_ptr<TSessionData> SessionData(SessionDataForCode());
  2298. Data = TCustomCommandData(SessionData.get());
  2299. Site = Terminal->SessionData->SessionKey;
  2300. RemotePath = Terminal->CurrentDirectory;
  2301. }
  2302. UnicodeString HelpKeyword = ACommand.HomePage;
  2303. std::unique_ptr<TStrings> CustomCommandOptions(CloneStrings(WinConfiguration->CustomCommandOptions));
  2304. if (ACommand.AnyOptionWithFlag(TCustomCommandType::ofRun))
  2305. {
  2306. std::unique_ptr<TCustomCommand> CustomCommandForOptions;
  2307. if (FLAGCLEAR(ACommand.Params, ccLocal))
  2308. {
  2309. CustomCommandForOptions.reset(new TRemoteCustomCommand(Data, RemotePath));
  2310. }
  2311. else
  2312. {
  2313. CustomCommandForOptions.reset(new TLocalCustomCommand(Data, RemotePath, DefaultDownloadTargetDirectory()));
  2314. }
  2315. if (!DoCustomCommandOptionsDialog(
  2316. &ACommand, CustomCommandOptions.get(), NULL, TCustomCommandType::ofRun, CustomCommandForOptions.get(), Site, NULL))
  2317. {
  2318. Abort();
  2319. }
  2320. }
  2321. UnicodeString CommandCommand = ACommand.GetCommandWithExpandedOptions(CustomCommandOptions.get(), Site);
  2322. if (FLAGCLEAR(ACommand.Params, ccLocal))
  2323. {
  2324. RemoteCustomCommand(FileList, ACommand, Data, CommandCommand);
  2325. }
  2326. else
  2327. {
  2328. LocalCustomCommand(FileList, ACommand, ALocalFileList, Data, CommandCommand);
  2329. }
  2330. }
  2331. //---------------------------------------------------------------------------
  2332. void __fastcall TCustomScpExplorerForm::BothCustomCommand(
  2333. const TCustomCommandType & Command)
  2334. {
  2335. DebugAssert(FCustomCommandLocalFileList != NULL);
  2336. DebugAssert(FCustomCommandRemoteFileList != NULL);
  2337. DebugAssert(FCustomCommandLocalFileList->Count == FCustomCommandRemoteFileList->Count);
  2338. TStrings * LocalFileList = new TStringList();
  2339. TStrings * RemoteFileList = new TStringList();
  2340. try
  2341. {
  2342. for (int Index = 0; Index < FCustomCommandLocalFileList->Count; Index++)
  2343. {
  2344. LocalFileList->Clear();
  2345. LocalFileList->AddObject(
  2346. FCustomCommandLocalFileList->Strings[Index],
  2347. FCustomCommandLocalFileList->Objects[Index]);
  2348. RemoteFileList->Clear();
  2349. RemoteFileList->AddObject(
  2350. FCustomCommandRemoteFileList->Strings[Index],
  2351. FCustomCommandRemoteFileList->Objects[Index]);
  2352. CustomCommand(RemoteFileList, Command, LocalFileList);
  2353. }
  2354. }
  2355. __finally
  2356. {
  2357. delete LocalFileList;
  2358. delete RemoteFileList;
  2359. }
  2360. }
  2361. //---------------------------------------------------------------------------
  2362. void __fastcall TCustomScpExplorerForm::CustomCommandMenu(
  2363. TAction * Action, TStrings * LocalFileList, TStrings * RemoteFileList)
  2364. {
  2365. delete FCustomCommandLocalFileList;
  2366. delete FCustomCommandRemoteFileList;
  2367. // takeover ownership,
  2368. // the lists must survive the MenuPopup as OnClick occurs only after it exits
  2369. FCustomCommandLocalFileList = LocalFileList;
  2370. FCustomCommandRemoteFileList = RemoteFileList;
  2371. TButton * Button = dynamic_cast<TButton *>(Action->ActionComponent);
  2372. if (Button != NULL)
  2373. {
  2374. FCustomCommandMenu->Items->Clear();
  2375. NonVisualDataModule->CreateCustomCommandsMenu(FCustomCommandMenu->Items, false, false, ccltBoth, NULL);
  2376. MenuPopup(FCustomCommandMenu, Button);
  2377. }
  2378. else
  2379. {
  2380. NonVisualDataModule->CreateCustomCommandsMenu(Action, false, ccltBoth);
  2381. }
  2382. }
  2383. //---------------------------------------------------------------------------
  2384. void __fastcall TCustomScpExplorerForm::TerminalCaptureLog(
  2385. const UnicodeString & AddedLine, TCaptureOutputType OutputType)
  2386. {
  2387. DebugAssert(FCapturedLog != NULL);
  2388. if ((OutputType == cotOutput) || (OutputType == cotError))
  2389. {
  2390. FCapturedLog->Add(AddedLine);
  2391. }
  2392. }
  2393. //---------------------------------------------------------------------------
  2394. bool __fastcall TCustomScpExplorerForm::IsFileControl(TObject * Control,
  2395. TOperationSide Side)
  2396. {
  2397. return
  2398. (Side == osOther) &&
  2399. ((Control == DirView(osOther)) || (Control == DriveView(osOther)));
  2400. }
  2401. //---------------------------------------------------------------------------
  2402. void __fastcall TCustomScpExplorerForm::DirViewContextPopupDefaultItem(
  2403. TOperationSide Side, TTBXCustomItem * Item,
  2404. TResolvedDoubleClickAction DoubleClickAction1, TResolvedDoubleClickAction DoubleClickAction2)
  2405. {
  2406. TCustomDirView * DView = DirView(Side);
  2407. bool IsDefault = false;
  2408. if (DView->ItemFocused != NULL)
  2409. {
  2410. TTerminal * ATerminal = !IsSideLocalBrowser(Side) ? Terminal : NULL;
  2411. TResolvedDoubleClickAction DefaultAction =
  2412. WinConfiguration->ResolveDoubleClickAction(DView->ItemIsDirectory(DView->ItemFocused), ATerminal);
  2413. IsDefault = (DefaultAction == DoubleClickAction1) || (DefaultAction == DoubleClickAction2);
  2414. }
  2415. TTBItemOptions O = Item->Options;
  2416. if (IsDefault)
  2417. {
  2418. Item->Options = O << tboDefault;
  2419. }
  2420. else
  2421. {
  2422. Item->Options = O >> tboDefault;
  2423. }
  2424. }
  2425. //---------------------------------------------------------------------------
  2426. void __fastcall TCustomScpExplorerForm::DirViewContextPopup(
  2427. TOperationSide Side, Byte PopupComponent, const TPoint &MousePos)
  2428. {
  2429. TCustomDirView * DView = DirView(Side);
  2430. TListItem * Item = DView->ItemFocused;
  2431. if ((DView->GetItemAt(MousePos.x, MousePos.y) == Item) &&
  2432. EnableFocusedOperation[Side])
  2433. {
  2434. TPoint ClientPoint;
  2435. ClientPoint = ((MousePos.x < 0) && (MousePos.y < 0)) ?
  2436. TPoint(0, 0) : MousePos;
  2437. FLastContextPopupScreenPoint = DView->ClientToScreen(ClientPoint);
  2438. reinterpret_cast<TPopupMenu*>(GetComponent(PopupComponent))->Popup(
  2439. FLastContextPopupScreenPoint.x, FLastContextPopupScreenPoint.y);
  2440. }
  2441. }
  2442. //---------------------------------------------------------------------------
  2443. void __fastcall TCustomScpExplorerForm::RemoteDirViewContextPopup(
  2444. TObject * /*Sender*/, const TPoint &MousePos, bool &Handled)
  2445. {
  2446. DirViewContextPopupDefaultItem(osRemote, NonVisualDataModule->RemoteOpenMenuItem, rdcaChangeDir, rdcaOpen);
  2447. DirViewContextPopupDefaultItem(osRemote, NonVisualDataModule->RemoteEditMenuItem, rdcaEdit, rdcaNone);
  2448. DirViewContextPopupDefaultItem(osRemote, NonVisualDataModule->RemoteCopyMenuItem, rdcaCopy, rdcaNone);
  2449. DirViewContextPopup(osRemote, fcRemotePopup, MousePos);
  2450. Handled = true;
  2451. }
  2452. //---------------------------------------------------------------------------
  2453. void __fastcall TCustomScpExplorerForm::ReloadLocalDirectory(const UnicodeString Directory)
  2454. {
  2455. }
  2456. //---------------------------------------------------------------------------
  2457. void __fastcall TCustomScpExplorerForm::BatchStart(void *& /*Storage*/)
  2458. {
  2459. DebugAssert(FErrorList == NULL);
  2460. if (WinConfiguration->ContinueOnError)
  2461. {
  2462. FErrorList = new TStringList();
  2463. Configuration->Usage->Inc(L"ContinuationsOnError");
  2464. }
  2465. NonVisualDataModule->StartBusy();
  2466. }
  2467. //---------------------------------------------------------------------------
  2468. void __fastcall TCustomScpExplorerForm::BatchEnd(void * /*Storage*/)
  2469. {
  2470. NonVisualDataModule->EndBusy();
  2471. if (FErrorList)
  2472. {
  2473. HandleErrorList(FErrorList);
  2474. }
  2475. }
  2476. //---------------------------------------------------------------------------
  2477. void __fastcall TCustomScpExplorerForm::UpdateCopyParamCounters(
  2478. const TCopyParamType & CopyParam)
  2479. {
  2480. if (!CopyParam.IncludeFileMask.Masks.IsEmpty())
  2481. {
  2482. Configuration->Usage->Inc(L"FileMaskUses");
  2483. }
  2484. if (IsEffectiveFileNameMask(CopyParam.FileMask))
  2485. {
  2486. Configuration->Usage->Inc(L"OperationMaskUses");
  2487. }
  2488. }
  2489. //---------------------------------------------------------------------------
  2490. bool __fastcall TCustomScpExplorerForm::ExecuteCopyMoveFileOperation(
  2491. TFileOperation Operation, TOperationSide Side, TStrings * FileList, bool NoConfirmation, void * AParam)
  2492. {
  2493. DebugAssert(!IsLocalBrowserMode());
  2494. TTransferDirection Direction = (Side == osLocal ? tdToRemote : tdToLocal);
  2495. TTransferType Type = (Operation == foCopy ? ttCopy : ttMove);
  2496. TTransferOperationParam DefaultParam;
  2497. TTransferOperationParam & Param = (AParam != NULL) ? *static_cast<TTransferOperationParam *>(AParam) : DefaultParam;
  2498. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  2499. switch (Param.Queue)
  2500. {
  2501. case asOn:
  2502. CopyParam.Queue = true;
  2503. break;
  2504. case asOff:
  2505. CopyParam.Queue = false;
  2506. break;
  2507. case asAuto:
  2508. default:
  2509. // keep default
  2510. break;
  2511. }
  2512. bool Result =
  2513. CopyParamDialog(Direction, Type, Param.Temp, FileList, Param.TargetDirectory, CopyParam, !NoConfirmation, Param.DragDrop, Param.Options);
  2514. if (Result)
  2515. {
  2516. DebugAssert(Terminal);
  2517. bool SelectionRestored = false;
  2518. TCustomDirView * DView = NULL;
  2519. if (HasDirView[Side])
  2520. {
  2521. DView = DirView(Side);
  2522. DView->SaveSelection();
  2523. DView->SaveSelectedNames();
  2524. }
  2525. UpdateCopyParamCounters(CopyParam);
  2526. std::unique_ptr<TStringList> TransferResumeList(new TStringList());
  2527. DebugAssert(FTransferResumeList == NULL);
  2528. FTransferResumeList = Terminal->IsCapable[fcMoveToQueue] ? TransferResumeList.get() : NULL;
  2529. FMoveToQueue = false;
  2530. int Params = FLAGMASK(Operation == foMove, cpDelete);
  2531. try
  2532. {
  2533. TStrings * PermanentFileList;
  2534. std::unique_ptr<TStrings> PermanentFileListOwner;
  2535. try
  2536. {
  2537. if (Side == osLocal)
  2538. {
  2539. PermanentFileList = FileList;
  2540. Params |= FLAGMASK(Param.Temp, cpTemporary);
  2541. CopyParam.IncludeFileMask.SetRoots(FileList, Param.TargetDirectory);
  2542. Terminal->CopyToRemote(FileList, Param.TargetDirectory, &CopyParam, Params, NULL);
  2543. if (Operation == foMove)
  2544. {
  2545. ReloadLocalDirectory();
  2546. if (DView != NULL)
  2547. {
  2548. DView->RestoreSelection();
  2549. }
  2550. SelectionRestored = true;
  2551. }
  2552. }
  2553. else
  2554. {
  2555. // Clone the file list as it may refer to current directory files,
  2556. // which get destroyed, when the source directory is reloaded after foMove operation.
  2557. // We should actually clone the file list for whole ExecuteFileOperation to protect against reloads.
  2558. // But for a hotfix, we are not going to do such a big change.
  2559. PermanentFileListOwner.reset(TRemoteFileList::CloneStrings(FileList));
  2560. PermanentFileList = PermanentFileListOwner.get();
  2561. try
  2562. {
  2563. CopyParam.IncludeFileMask.SetRoots(Param.TargetDirectory, FileList);
  2564. Terminal->CopyToLocal(FileList, Param.TargetDirectory, &CopyParam, Params, NULL);
  2565. }
  2566. __finally
  2567. {
  2568. if (Operation == foMove)
  2569. {
  2570. if (DView != NULL)
  2571. {
  2572. DView->RestoreSelection();
  2573. }
  2574. SelectionRestored = true;
  2575. }
  2576. ReloadLocalDirectory(Param.TargetDirectory);
  2577. }
  2578. }
  2579. // When the transfer batch is aborted between individual file transfers, the Abort is not called.
  2580. if (FMoveToQueue)
  2581. {
  2582. Abort();
  2583. }
  2584. }
  2585. catch (EAbort &)
  2586. {
  2587. if (FMoveToQueue)
  2588. {
  2589. Params |= (CopyParam.QueueNoConfirmation ? cpNoConfirmation : 0);
  2590. DebugAssert(CopyParam.TransferSkipList == NULL);
  2591. DebugAssert(CopyParam.TransferResumeFile.IsEmpty());
  2592. if (TransferResumeList->Count > 0)
  2593. {
  2594. CopyParam.TransferResumeFile = TransferResumeList->Strings[TransferResumeList->Count - 1];
  2595. TransferResumeList->Delete(TransferResumeList->Count - 1);
  2596. }
  2597. CopyParam.TransferSkipList = TransferResumeList.release();
  2598. // not really needed, just to keep it consistent with TransferResumeList
  2599. FTransferResumeList = NULL;
  2600. FMoveToQueue = false;
  2601. Configuration->Usage->Inc("MovesToBackground");
  2602. AddQueueItem(Queue, Direction, PermanentFileList, Param.TargetDirectory, CopyParam, Params);
  2603. ClearTransferSourceSelection(Direction);
  2604. }
  2605. throw;
  2606. }
  2607. }
  2608. __finally
  2609. {
  2610. if (!SelectionRestored && (DView != NULL))
  2611. {
  2612. DView->DiscardSavedSelection();
  2613. }
  2614. FTransferResumeList = NULL;
  2615. }
  2616. }
  2617. return Result;
  2618. }
  2619. //---------------------------------------------------------------------------
  2620. bool __fastcall TCustomScpExplorerForm::ExecuteDeleteFileOperation(
  2621. TOperationSide Side, TStrings * FileList, void * Param)
  2622. {
  2623. DebugAssert(FileList->Count);
  2624. // We deliberately do not toggle alternative flag (Param), but use OR,
  2625. // because the Param is set only when command is invoked using Shift-Del/F8 keyboard
  2626. // shortcut of CurrentDeleteAlternativeAction
  2627. bool Alternative =
  2628. bool(Param) || UseAlternativeFunction();
  2629. bool Recycle;
  2630. if (IsSideLocalBrowser(Side))
  2631. {
  2632. Recycle = (WinConfiguration->DeleteToRecycleBin != Alternative);
  2633. }
  2634. else
  2635. {
  2636. Recycle =
  2637. (Terminal->SessionData->DeleteToRecycleBin != Alternative) &&
  2638. !Terminal->SessionData->RecycleBinPath.IsEmpty() &&
  2639. !Terminal->IsRecycledFile(FileList->Strings[0]);
  2640. }
  2641. bool Result = !(Recycle ? WinConfiguration->ConfirmRecycling : WinConfiguration->ConfirmDeleting);
  2642. if (!Result)
  2643. {
  2644. UnicodeString Query;
  2645. if (FileList->Count == 1)
  2646. {
  2647. if (IsSideLocalBrowser(Side))
  2648. {
  2649. Query = ExtractFileName(FileList->Strings[0]);
  2650. }
  2651. else
  2652. {
  2653. Query = UnixExtractFileName(FileList->Strings[0]);
  2654. }
  2655. Query = FMTLOAD((Recycle ? CONFIRM_RECYCLE_FILE : CONFIRM_DELETE_FILE), (Query));
  2656. }
  2657. else
  2658. {
  2659. Query = FMTLOAD((Recycle ? CONFIRM_RECYCLE_FILES : CONFIRM_DELETE_FILES), (FileList->Count));
  2660. }
  2661. TMessageParams Params(mpNeverAskAgainCheck);
  2662. Params.ImageName = L"Delete file";
  2663. unsigned int Answer =
  2664. MessageDialog(MainInstructions(Query), qtConfirmation, qaOK | qaCancel, HELP_DELETE_FILE, &Params);
  2665. if (Answer == qaNeverAskAgain)
  2666. {
  2667. Result = true;
  2668. if (Recycle)
  2669. {
  2670. WinConfiguration->ConfirmRecycling = false;
  2671. }
  2672. else
  2673. {
  2674. WinConfiguration->ConfirmDeleting = false;
  2675. }
  2676. }
  2677. else
  2678. {
  2679. Result = (Answer == qaOK);
  2680. }
  2681. }
  2682. if (Result)
  2683. {
  2684. DeleteFiles(Side, FileList, FLAGMASK(Alternative, dfAlternative));
  2685. }
  2686. return Result;
  2687. }
  2688. //---------------------------------------------------------------------------
  2689. bool __fastcall TCustomScpExplorerForm::ExecuteFileOperation(TFileOperation Operation,
  2690. TOperationSide Side, TStrings * FileList, bool NoConfirmation, void * Param)
  2691. {
  2692. TAutoBatch AutoBatch(this);
  2693. bool Result;
  2694. if (TFileOperationProgressType::IsTransferOperation(Operation))
  2695. {
  2696. Result = ExecuteCopyMoveFileOperation(Operation, Side, FileList, NoConfirmation, Param);
  2697. }
  2698. else if (Operation == foRename)
  2699. {
  2700. DebugAssert(DirView(Side)->ItemFocused);
  2701. DirView(Side)->ItemFocused->EditCaption();
  2702. Result = true;
  2703. }
  2704. else if (Operation == foDelete)
  2705. {
  2706. Result = ExecuteDeleteFileOperation(Side, FileList, Param);
  2707. }
  2708. else if (Operation == foSetProperties)
  2709. {
  2710. DirView(osRemote)->SaveSelectedNames();
  2711. Result = SetProperties(Side, FileList);
  2712. }
  2713. else if (Operation == foCustomCommand)
  2714. {
  2715. DebugAssert(Param);
  2716. DebugAssert(Side == osRemote); // Side carries no meaning for foCustomCommand
  2717. DirView(osRemote)->SaveSelectedNames();
  2718. const TCustomCommandType * Command = static_cast<const TCustomCommandType*>(Param);
  2719. CustomCommand(FileList, *Command, NULL);
  2720. Result = true;
  2721. }
  2722. else if ((Operation == foRemoteMove) || (Operation == foRemoteCopy))
  2723. {
  2724. DebugAssert(!IsSideLocalBrowser(Side));
  2725. Result = RemoteTransferFiles(FileList, NoConfirmation,
  2726. (Operation == foRemoteMove), reinterpret_cast<TManagedTerminal *>(Param));
  2727. }
  2728. else if (Operation == foLock)
  2729. {
  2730. DebugAssert(!IsSideLocalBrowser(Side));
  2731. LockFiles(FileList, true);
  2732. Result = true;
  2733. }
  2734. else if (Operation == foUnlock)
  2735. {
  2736. DebugAssert(!IsSideLocalBrowser(Side));
  2737. LockFiles(FileList, false);
  2738. Result = true;
  2739. }
  2740. else
  2741. {
  2742. DebugFail();
  2743. }
  2744. return Result;
  2745. }
  2746. //---------------------------------------------------------------------------
  2747. TOperationSide __fastcall TCustomScpExplorerForm::GetSide(TOperationSide Side)
  2748. {
  2749. if (Side == osCurrent)
  2750. {
  2751. Side = FCurrentSide;
  2752. }
  2753. return Side;
  2754. }
  2755. //---------------------------------------------------------------------------
  2756. bool __fastcall TCustomScpExplorerForm::ExecuteFileOperation(TFileOperation Operation,
  2757. TOperationSide Side, bool OnFocused, bool NoConfirmation, void * Param)
  2758. {
  2759. Side = GetSide(Side);
  2760. bool FullPath = IsSideLocalBrowser(Side);
  2761. TCustomDirView * ADirView = DirView(Side);
  2762. std::unique_ptr<TStringList> FileList(new TStringList());
  2763. ADirView->CreateFileList(OnFocused, FullPath, FileList.get());
  2764. bool ReloadProperties =
  2765. (Side == osRemote) && (Operation == foSetProperties) &&
  2766. DebugAlwaysTrue(HasActiveTerminal()) && Terminal->IsCapable[fcLoadingAdditionalProperties];
  2767. bool Result = ExecuteFileOperation(Operation, Side, FileList.get(), NoConfirmation, Param);
  2768. if (Result && ReloadProperties)
  2769. {
  2770. std::unique_ptr<TStringList> FileList2(new TStringList());
  2771. FileList->CaseSensitive = true;
  2772. FileList->Sort();
  2773. for (int Index = 0; Index < ADirView->Items->Count; Index++)
  2774. {
  2775. TListItem * Item = ADirView->Items->Item[Index];
  2776. UnicodeString FileName = ADirView->ItemFileName(Item);
  2777. int Index2;
  2778. if (FileList->Find(FileName, Index2))
  2779. {
  2780. FileList2->AddObject(FileName, ADirView->ItemData(Item));
  2781. }
  2782. }
  2783. LoadFilesProperties(FileList2.get());
  2784. }
  2785. return Result;
  2786. }
  2787. //---------------------------------------------------------------------------
  2788. void __fastcall TCustomScpExplorerForm::ExecuteFileOperationCommand(
  2789. TFileOperation Operation, TOperationSide Side, bool OnFocused,
  2790. bool NoConfirmation, void * Param)
  2791. {
  2792. if (ExecuteFileOperation(Operation, Side, OnFocused, NoConfirmation, Param))
  2793. {
  2794. if (TFileOperationProgressType::IsTransferOperation(Operation))
  2795. {
  2796. DebugAssert(!IsLocalBrowserMode());
  2797. if (GetSide(Side) == osLocal)
  2798. {
  2799. Configuration->Usage->Inc(L"UploadsCommand");
  2800. }
  2801. else
  2802. {
  2803. Configuration->Usage->Inc(L"DownloadsCommand");
  2804. }
  2805. }
  2806. }
  2807. }
  2808. //---------------------------------------------------------------------------
  2809. void __fastcall TCustomScpExplorerForm::ExecuteCopyOperationCommand(
  2810. TOperationSide Side, bool OnFocused, unsigned int Flags)
  2811. {
  2812. TTransferOperationParam Param;
  2813. if ((WinConfiguration->Interface != ifCommander) ||
  2814. WinConfiguration->ScpCommander.ExplorerKeyboardShortcuts)
  2815. {
  2816. Flags &= ~cocShortCutHint;
  2817. }
  2818. TCustomDirView * DView = DirView(Side);
  2819. Param.Options =
  2820. FLAGMASK(FLAGSET(Flags, cocShortCutHint), coShortCutHint) |
  2821. FLAGMASK(SelectedAllFilesInDirView(DView), coAllFiles);
  2822. if (FLAGSET(Flags, cocQueue))
  2823. {
  2824. Param.Queue = asOn;
  2825. }
  2826. else if (FLAGSET(Flags, cocNonQueue))
  2827. {
  2828. Param.Queue = asOff;
  2829. }
  2830. ExecuteFileOperationCommand(foCopy, Side, OnFocused, false, &Param);
  2831. }
  2832. //---------------------------------------------------------------------------
  2833. void __fastcall TCustomScpExplorerForm::HandleErrorList(TStringList *& ErrorList)
  2834. {
  2835. int Count = ErrorList->Count;
  2836. try
  2837. {
  2838. if (ErrorList->Count)
  2839. {
  2840. UnicodeString Message = MainInstructions(FMTLOAD(ERROR_LIST_COUNT, (ErrorList->Count)));
  2841. if (MessageDialog(Message, qtError,
  2842. qaOK | qaCancel, HELP_NONE) == qaOK)
  2843. {
  2844. unsigned int Answer;
  2845. int Index = 0;
  2846. do
  2847. {
  2848. DebugAssert(Index >= 0 && Index < Count);
  2849. TQueryButtonAlias Aliases[2];
  2850. Aliases[0].Button = qaYes;
  2851. Aliases[0].Alias = LoadStr(PREV_BUTTON);
  2852. Aliases[1].Button = qaNo;
  2853. Aliases[1].Alias = LoadStr(NEXT_BUTTON);
  2854. TMessageParams Params;
  2855. Params.Aliases = Aliases;
  2856. Params.AliasesCount = LENOF(Aliases);
  2857. int No = Index + 1;
  2858. UnicodeString Message = ErrorList->Strings[Index];
  2859. UnicodeString AMainInstructions;
  2860. UnicodeString Details;
  2861. if (ExtractMainInstructions(Message, AMainInstructions))
  2862. {
  2863. Details = Message;
  2864. Message = AMainInstructions;
  2865. }
  2866. Message = MainInstructions(FMTLOAD(ERROR_LIST_NUMBER, (No, Count, Message))) + Details;
  2867. Answer = MoreMessageDialog(
  2868. Message,
  2869. dynamic_cast<TStrings *>(ErrorList->Objects[Index]), qtError,
  2870. (Index ? qaYes : 0) | (Index < Count - 1 ? qaNo : 0) |
  2871. qaOK, HELP_NONE, &Params);
  2872. if (Answer == qaNo)
  2873. {
  2874. Index++;
  2875. }
  2876. if (Answer == qaYes)
  2877. {
  2878. Index--;
  2879. }
  2880. }
  2881. while (Answer != qaOK);
  2882. }
  2883. }
  2884. }
  2885. __finally
  2886. {
  2887. TStrings * List = ErrorList;
  2888. ErrorList = NULL;
  2889. for (int i = 0; i < Count; i++)
  2890. {
  2891. delete List->Objects[i];
  2892. }
  2893. delete List;
  2894. }
  2895. }
  2896. //---------------------------------------------------------------------------
  2897. void __fastcall TCustomScpExplorerForm::ExecuteRemoteFile(
  2898. const UnicodeString & FullFileName, TRemoteFile * File, TExecuteFileBy ExecuteFileBy)
  2899. {
  2900. // needed for checking filemasks, as there's no directory object
  2901. // associated with the file object
  2902. File->FullFileName = FullFileName;
  2903. TFileMasks::TParams MaskParams;
  2904. MaskParams.Size = File->Size;
  2905. MaskParams.Modification = File->Modification;
  2906. ExecuteFile(osRemote, ExecuteFileBy, NULL, FullFileName, File, MaskParams);
  2907. }
  2908. //---------------------------------------------------------------------------
  2909. void __fastcall TCustomScpExplorerForm::EditNew(TOperationSide Side)
  2910. {
  2911. DebugAssert(!WinConfiguration->DisableOpenEdit);
  2912. Side = GetSide(Side);
  2913. TCustomDirView * DView = DirView(Side);
  2914. TListItem * FocusedItem = DView->ItemFocused;
  2915. UnicodeString Name;
  2916. if ((FocusedItem != NULL) && !DView->ItemIsDirectory(FocusedItem))
  2917. {
  2918. Name = DView->ItemFileName(FocusedItem);
  2919. }
  2920. else
  2921. {
  2922. Name = LoadStr(NEW_FILE);
  2923. }
  2924. UnicodeString Names = Name;
  2925. std::unique_ptr<TStrings> History(CloneStrings(CustomWinConfiguration->History[L"EditFile"]));
  2926. if (InputDialog(LoadStr(EDIT_FILE_CAPTION), LoadStr(EDIT_FILE_PROMPT), Names,
  2927. HELP_EDIT_NEW, History.get(), true, NULL, true, 400))
  2928. {
  2929. while (!Names.IsEmpty())
  2930. {
  2931. Name = CutToChar(Names, FileMasksDelimiters[1], false);
  2932. CustomWinConfiguration->History[L"EditFile"] = History.get();
  2933. UnicodeString TargetFileName;
  2934. UnicodeString LocalFileName;
  2935. UnicodeString RootTempDir;
  2936. UnicodeString TempDir;
  2937. UnicodeString RemoteDirectory;
  2938. bool ExistingFile = false;
  2939. if (!IsSideLocalBrowser(Side))
  2940. {
  2941. Name = AbsolutePath(Terminal->CurrentDirectory, Name);
  2942. TRemoteFile * File = NULL;
  2943. if (Terminal->FileExists(Name, &File) &&
  2944. !File->IsDirectory)
  2945. {
  2946. try
  2947. {
  2948. ExecuteRemoteFile(Name, File, efDefaultEditor);
  2949. ExistingFile = true;
  2950. }
  2951. __finally
  2952. {
  2953. delete File;
  2954. }
  2955. }
  2956. if (!ExistingFile)
  2957. {
  2958. RemoteDirectory = UnixExtractFilePath(Name);
  2959. TemporaryDirectoryForRemoteFiles(
  2960. RemoteDirectory, GUIConfiguration->CurrentCopyParam, TempDir, RootTempDir);
  2961. TargetFileName = UnixExtractFileName(Name);
  2962. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  2963. LocalFileName = TempDir +
  2964. // We probably do not want to trim the VMS version here
  2965. Terminal->ChangeFileName(&CopyParam, TargetFileName, osRemote, false);
  2966. }
  2967. }
  2968. else
  2969. {
  2970. if (ExtractFilePath(Name).IsEmpty())
  2971. {
  2972. LocalFileName = IncludeTrailingBackslash(DView->PathName) + Name;
  2973. }
  2974. else
  2975. {
  2976. LocalFileName = ExpandFileName(Name);
  2977. }
  2978. TargetFileName = ExtractFileName(Name);
  2979. }
  2980. if (!ExistingFile)
  2981. {
  2982. bool NewFile = !FileExists(ApiPath(LocalFileName));
  2983. if (NewFile)
  2984. {
  2985. int File = FileCreate(ApiPath(LocalFileName));
  2986. if (File < 0)
  2987. {
  2988. if (!RootTempDir.IsEmpty())
  2989. {
  2990. RecursiveDeleteFile(ExcludeTrailingBackslash(RootTempDir), false);
  2991. }
  2992. throw Exception(FMTLOAD(CREATE_FILE_ERROR, (LocalFileName)));
  2993. }
  2994. else
  2995. {
  2996. FileClose(File);
  2997. }
  2998. }
  2999. TExecuteFileBy ExecuteFileBy = efDefaultEditor;
  3000. const TEditorData * ExternalEditor = NULL;
  3001. TFileMasks::TParams MaskParams; // size not known
  3002. ExecuteFileNormalize(ExecuteFileBy, ExternalEditor, TargetFileName,
  3003. false, MaskParams);
  3004. CustomExecuteFile(Side, ExecuteFileBy, LocalFileName, TargetFileName,
  3005. ExternalEditor, RootTempDir, RemoteDirectory, NewFile);
  3006. }
  3007. }
  3008. }
  3009. }
  3010. //---------------------------------------------------------------------------
  3011. bool __fastcall TCustomScpExplorerForm::RemoteExecuteForceText(
  3012. TExecuteFileBy ExecuteFileBy, const TEditorData * ExternalEditor)
  3013. {
  3014. DebugAssert((ExecuteFileBy == efExternalEditor) ==
  3015. ((ExternalEditor != NULL) && (ExternalEditor->Editor == edExternal)));
  3016. DebugAssert(ExecuteFileBy != efDefaultEditor);
  3017. return
  3018. ((ExecuteFileBy == efInternalEditor)) ||
  3019. ((ExecuteFileBy == efExternalEditor) && ExternalEditor->ExternalEditorText);
  3020. }
  3021. //---------------------------------------------------------------------------
  3022. void __fastcall TCustomScpExplorerForm::CustomExecuteFile(TOperationSide Side,
  3023. TExecuteFileBy ExecuteFileBy, UnicodeString FileName, UnicodeString OriginalFileName,
  3024. const TEditorData * ExternalEditor, UnicodeString LocalRootDirectory,
  3025. UnicodeString RemoteDirectory, bool NewFile)
  3026. {
  3027. DebugAssert(!WinConfiguration->DisableOpenEdit);
  3028. DebugAssert((ExecuteFileBy == efExternalEditor) ==
  3029. ((ExternalEditor != NULL) && (ExternalEditor->Editor == edExternal)));
  3030. DebugAssert(ExecuteFileBy != efDefaultEditor);
  3031. Side = GetSide(Side);
  3032. std::unique_ptr<TEditedFileData> Data(new TEditedFileData);
  3033. if (!IsSideLocalBrowser(Side))
  3034. {
  3035. Data->Terminal = Terminal;
  3036. Data->Queue = Queue;
  3037. Data->SessionData = CloneCurrentSessionData();
  3038. Data->ForceText = RemoteExecuteForceText(ExecuteFileBy, ExternalEditor);
  3039. Data->RemoteDirectory = RemoteDirectory;
  3040. Data->SessionName = Terminal->SessionData->SessionName;
  3041. Data->LocalRootDirectory = LocalRootDirectory;
  3042. Data->OriginalFileName = OriginalFileName;
  3043. Data->Command = L""; // will be changed later for external editor
  3044. }
  3045. if (ExecuteFileBy == efInternalEditor)
  3046. {
  3047. if (!IsSideLocalBrowser(Side))
  3048. {
  3049. UnicodeString RemotePath = UnixCombinePaths(RemoteDirectory, OriginalFileName);
  3050. UnicodeString Caption = RemotePath + TitleSeparator + Terminal->SessionData->SessionName;
  3051. TForm * Editor;
  3052. try
  3053. {
  3054. Editor =
  3055. ShowEditorForm(
  3056. FileName, this,
  3057. FEditorManager->FileChanged, FEditorManager->FileReload, FEditorManager->FileClosed,
  3058. SaveAllInternalEditors, AnyInternalEditorModified,
  3059. Caption, StandaloneOperation, SessionColor, Terminal->SessionData->InternalEditorEncoding, NewFile);
  3060. }
  3061. catch(...)
  3062. {
  3063. if (!LocalRootDirectory.IsEmpty())
  3064. {
  3065. RecursiveDeleteFile(ExcludeTrailingBackslash(LocalRootDirectory), false);
  3066. }
  3067. throw;
  3068. }
  3069. FEditorManager->AddFileInternal(FileName, Data.release(), Editor);
  3070. }
  3071. else
  3072. {
  3073. DebugAssert(!StandaloneOperation);
  3074. TForm * Editor =
  3075. ShowEditorForm(FileName, this, NULL, NULL, LocalEditorClosed,
  3076. SaveAllInternalEditors, AnyInternalEditorModified,
  3077. L"", false, SessionColor, -1, NewFile);
  3078. FLocalEditors->Add(Editor);
  3079. }
  3080. }
  3081. else
  3082. {
  3083. HANDLE Process;
  3084. if (ExecuteFileBy == efExternalEditor)
  3085. {
  3086. UnicodeString Program, Params, Dir;
  3087. Data->Command = ExternalEditor->ExternalEditor;
  3088. ReformatFileNameCommand(Data->Command);
  3089. SplitCommand(Data->Command, Program, Params, Dir);
  3090. Params = ExpandFileNameCommand(Params, FileName);
  3091. Program = ExpandEnvironmentVariables(Program);
  3092. if (!ExecuteShell(Program, Params, Process))
  3093. {
  3094. throw EOSExtException(FMTLOAD(EDITOR_ERROR, (Program)));
  3095. }
  3096. }
  3097. else
  3098. {
  3099. DebugAssert(!IsSideLocalBrowser(Side));
  3100. if (!ExecuteShell(FileName, L"", Process))
  3101. {
  3102. throw EOSExtException(FMTLOAD(EXECUTE_FILE_ERROR, (FileName)));
  3103. }
  3104. }
  3105. if (IsSideLocalBrowser(Side) ||
  3106. ((ExecuteFileBy == efShell) &&
  3107. !WinConfiguration->Editor.SDIShellEditor) ||
  3108. ((ExecuteFileBy == efExternalEditor) &&
  3109. !ExternalEditor->SDIExternalEditor))
  3110. {
  3111. // no need for handle
  3112. if (Process != NULL)
  3113. {
  3114. DebugCheck(CloseHandle(Process));
  3115. }
  3116. Process = INVALID_HANDLE_VALUE;
  3117. }
  3118. else
  3119. {
  3120. if (Process == NULL)
  3121. {
  3122. throw ExtException(LoadStr(OPEN_FILE_NO_PROCESS2), L"", HELP_OPEN_FILE_NO_PROCESS);
  3123. }
  3124. }
  3125. if (!IsSideLocalBrowser(Side))
  3126. {
  3127. FEditorManager->AddFileExternal(FileName, Data.release(), Process);
  3128. }
  3129. }
  3130. }
  3131. //---------------------------------------------------------------------------
  3132. void __fastcall TCustomScpExplorerForm::SaveInternalEditor(
  3133. const UnicodeString /*FileName*/, TEditedFileData * /*Data*/, TObject * Token,
  3134. void * /*Arg*/)
  3135. {
  3136. if (Token != NULL)
  3137. {
  3138. EditorFormFileSave(static_cast<TForm *>(Token));
  3139. }
  3140. }
  3141. //---------------------------------------------------------------------------
  3142. void __fastcall TCustomScpExplorerForm::SaveAllInternalEditors(TObject * /*Sender*/)
  3143. {
  3144. for (int Index = 0; Index < FLocalEditors->Count; Index++)
  3145. {
  3146. EditorFormFileSave(static_cast<TForm *>(FLocalEditors->Items[Index]));
  3147. }
  3148. FEditorManager->ProcessFiles(SaveInternalEditor, NULL);
  3149. }
  3150. //---------------------------------------------------------------------------
  3151. void __fastcall TCustomScpExplorerForm::InternalEditorModified(
  3152. const UnicodeString /*FileName*/, TEditedFileData * /*Data*/, TObject * Token,
  3153. void * Arg)
  3154. {
  3155. if ((Token != NULL) &&
  3156. IsEditorFormModified(static_cast<TForm *>(Token)))
  3157. {
  3158. *static_cast<bool *>(Arg) = true;
  3159. }
  3160. }
  3161. //---------------------------------------------------------------------------
  3162. void __fastcall TCustomScpExplorerForm::AnyInternalEditorModified(
  3163. TObject * /*Sender*/, bool & Modified)
  3164. {
  3165. for (int Index = 0; !Modified && (Index < FLocalEditors->Count); Index++)
  3166. {
  3167. if (IsEditorFormModified(static_cast<TForm *>(FLocalEditors->Items[Index])))
  3168. {
  3169. Modified = true;
  3170. }
  3171. }
  3172. if (!Modified)
  3173. {
  3174. FEditorManager->ProcessFiles(InternalEditorModified, &Modified);
  3175. }
  3176. }
  3177. //---------------------------------------------------------------------------
  3178. void __fastcall TCustomScpExplorerForm::LocalEditorClosed(TObject * Sender, bool /*Forced*/)
  3179. {
  3180. DebugCheck(FLocalEditors->Extract(Sender) >= 0);
  3181. }
  3182. //---------------------------------------------------------------------------
  3183. void __fastcall TCustomScpExplorerForm::TemporaryDirectoryForRemoteFiles(
  3184. UnicodeString RemoteDirectory, TCopyParamType CopyParam,
  3185. UnicodeString & Result, UnicodeString & RootDirectory)
  3186. {
  3187. if (!WinConfiguration->TemporaryDirectoryDeterministic)
  3188. {
  3189. RootDirectory = IncludeTrailingBackslash(WinConfiguration->TemporaryDir());
  3190. Result = RootDirectory;
  3191. }
  3192. else
  3193. {
  3194. RootDirectory = L"";
  3195. Result = WinConfiguration->ExpandedTemporaryDirectory();
  3196. Result = IncludeTrailingBackslash(Result);
  3197. }
  3198. if (WinConfiguration->TemporaryDirectoryAppendSession)
  3199. {
  3200. Result = IncludeTrailingBackslash(Result + MakeValidFileName(Terminal->SessionData->SessionName));
  3201. }
  3202. if (WinConfiguration->TemporaryDirectoryAppendPath)
  3203. {
  3204. if (!RemoteDirectory.IsEmpty() && (RemoteDirectory[1] == L'/'))
  3205. {
  3206. RemoteDirectory.Delete(1, 1);
  3207. }
  3208. Result = IncludeTrailingBackslash(Result + CopyParam.ValidLocalPath(FromUnixPath(RemoteDirectory)));
  3209. }
  3210. if (!ForceDirectories(ApiPath(Result)))
  3211. {
  3212. throw EOSExtException(FMTLOAD(CREATE_TEMP_DIR_ERROR, (Result)));
  3213. }
  3214. }
  3215. //---------------------------------------------------------------------------
  3216. void __fastcall TCustomScpExplorerForm::TemporarilyDownloadFiles(
  3217. TStrings * FileList, bool ForceText, UnicodeString & RootTempDir, UnicodeString & TempDir,
  3218. bool GetTargetNames, bool AutoOperation)
  3219. {
  3220. DebugAssert(!IsLocalBrowserMode());
  3221. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  3222. if (ForceText)
  3223. {
  3224. CopyParam.TransferMode = tmAscii;
  3225. }
  3226. TemporaryFileCopyParam(CopyParam);
  3227. if (TempDir.IsEmpty())
  3228. {
  3229. TemporaryDirectoryForRemoteFiles(Terminal->CurrentDirectory, CopyParam, TempDir, RootTempDir);
  3230. }
  3231. DebugAssert(!FAutoOperation);
  3232. FAutoOperation = AutoOperation;
  3233. Terminal->ExceptionOnFail = true;
  3234. try
  3235. {
  3236. try
  3237. {
  3238. // turn off confirmations, as for MDI editors we may possibly download
  3239. // the same file over
  3240. Terminal->CopyToLocal(
  3241. FileList, TempDir, &CopyParam, cpNoConfirmation | cpTemporary, NULL);
  3242. if (GetTargetNames)
  3243. {
  3244. for (int i = 0; i < FileList->Count; i++)
  3245. {
  3246. FileList->Strings[i] =
  3247. Terminal->ChangeFileName(&CopyParam, UnixExtractFileName(FileList->Strings[i]), osRemote, false);
  3248. }
  3249. }
  3250. }
  3251. catch(...)
  3252. {
  3253. if (!RootTempDir.IsEmpty())
  3254. {
  3255. RecursiveDeleteFile(ExcludeTrailingBackslash(RootTempDir), false);
  3256. }
  3257. throw;
  3258. }
  3259. }
  3260. __finally
  3261. {
  3262. FAutoOperation = false;
  3263. Terminal->ExceptionOnFail = false;
  3264. }
  3265. }
  3266. //---------------------------------------------------------------------------
  3267. void __fastcall TCustomScpExplorerForm::EditorAutoConfig()
  3268. {
  3269. // Do not waste time checking for default editor list the next time,
  3270. // or testing if default editor is not notepad.
  3271. bool TryNextTime = false;
  3272. UnicodeString UsageState;
  3273. if (!WinConfiguration->EditorList->IsDefaultList())
  3274. {
  3275. UsageState = "H";
  3276. }
  3277. else
  3278. {
  3279. UnicodeString Executable;
  3280. UnicodeString ExecutableDescription;
  3281. if (DetectSystemExternalEditor(false, Executable, ExecutableDescription, UsageState, TryNextTime))
  3282. {
  3283. UnicodeString Message =
  3284. FMTLOAD(EDITOR_AUTO_CONFIG2, (ExecutableDescription, ExecutableDescription));
  3285. unsigned int Answer =
  3286. MessageDialog(Message, qtConfirmation, qaOK | qaCancel, HELP_EDITOR_AUTO_CONFIG);
  3287. if (Answer != qaOK)
  3288. {
  3289. UsageState = "R";
  3290. }
  3291. else
  3292. {
  3293. UsageState = "A";
  3294. TEditorData EditorData;
  3295. EditorData.Editor = edExternal;
  3296. EditorData.ExternalEditor = FormatCommand(Executable, L"");
  3297. EditorData.ExternalEditorOptionsAutodetect();
  3298. TEditorList EditorList;
  3299. EditorList = *WinConfiguration->EditorList;
  3300. EditorList.Insert(0, new TEditorPreferences(EditorData));
  3301. WinConfiguration->EditorList = &EditorList;
  3302. }
  3303. }
  3304. }
  3305. WinConfiguration->OfferedEditorAutoConfig = !TryNextTime;
  3306. WinConfiguration->Usage->Set(L"EditorAutoConfig", UsageState);
  3307. }
  3308. //---------------------------------------------------------------------------
  3309. void __fastcall TCustomScpExplorerForm::ExecuteFileNormalize(
  3310. TExecuteFileBy & ExecuteFileBy, const TEditorData *& ExternalEditor,
  3311. const UnicodeString & FileName, bool Local, const TFileMasks::TParams & MaskParams)
  3312. {
  3313. if (ExecuteFileBy == efDefaultEditor)
  3314. {
  3315. if (!WinConfiguration->OfferedEditorAutoConfig)
  3316. {
  3317. EditorAutoConfig();
  3318. }
  3319. const TEditorPreferences * Editor =
  3320. WinConfiguration->DefaultEditorForFile(FileName, Local, MaskParams);
  3321. if ((Editor == NULL) || (Editor->Data->Editor == edInternal))
  3322. {
  3323. ExecuteFileBy = efInternalEditor;
  3324. ExternalEditor = NULL;
  3325. }
  3326. else if (Editor->Data->Editor == edOpen)
  3327. {
  3328. ExecuteFileBy = efShell;
  3329. ExternalEditor = NULL;
  3330. }
  3331. else
  3332. {
  3333. ExecuteFileBy = efExternalEditor;
  3334. ExternalEditor = Editor->Data;
  3335. }
  3336. }
  3337. }
  3338. //---------------------------------------------------------------------------
  3339. void __fastcall TCustomScpExplorerForm::ExecuteFile(TOperationSide Side,
  3340. TExecuteFileBy ExecuteFileBy, const TEditorData * ExternalEditor,
  3341. UnicodeString FullFileName, TObject * Object, const TFileMasks::TParams & MaskParams)
  3342. {
  3343. UnicodeString OriginalFileName;
  3344. UnicodeString LocalRootDirectory;
  3345. UnicodeString RemoteDirectory;
  3346. ExecuteFileNormalize(ExecuteFileBy, ExternalEditor, FullFileName,
  3347. IsSideLocalBrowser(Side), MaskParams);
  3348. UnicodeString Counter;
  3349. UnicodeString LocalFileName;
  3350. bool Handled = false;
  3351. if (!IsSideLocalBrowser(Side))
  3352. {
  3353. // We need to trim VMS version here, so that we use name without version
  3354. // when uploading back to create a new version of the file
  3355. OriginalFileName = Terminal->GetBaseFileName(UnixExtractFileName(FullFileName));
  3356. RemoteDirectory = UnixExtractFilePath(FullFileName);
  3357. TObject * Token = NULL;
  3358. UnicodeString LocalDirectory;
  3359. if (!FEditorManager->CanAddFile(RemoteDirectory, OriginalFileName,
  3360. Terminal->SessionData->SessionName, Token, LocalRootDirectory,
  3361. LocalDirectory))
  3362. {
  3363. if (Token != NULL)
  3364. {
  3365. TForm * Form = dynamic_cast<TForm *>(Token);
  3366. if (Form->WindowState == wsMinimized)
  3367. {
  3368. ShowWindow(Form->Handle, SW_RESTORE);
  3369. }
  3370. else
  3371. {
  3372. Form->SetFocus();
  3373. }
  3374. Handled = true;
  3375. }
  3376. else
  3377. {
  3378. throw Exception(FMTLOAD(ALREADY_EDITED_EXTERNALLY_OR_UPLOADED, (OriginalFileName)));
  3379. }
  3380. }
  3381. if (!Handled)
  3382. {
  3383. TStringList * FileList1 = new TStringList();
  3384. try
  3385. {
  3386. FileList1->AddObject(FullFileName, Object);
  3387. TemporarilyDownloadFiles(FileList1,
  3388. RemoteExecuteForceText(ExecuteFileBy, ExternalEditor),
  3389. LocalRootDirectory, LocalDirectory, true, true);
  3390. LocalFileName = LocalDirectory + FileList1->Strings[0];
  3391. }
  3392. __finally
  3393. {
  3394. delete FileList1;
  3395. }
  3396. switch (ExecuteFileBy)
  3397. {
  3398. case efShell:
  3399. Counter = "RemoteFilesExecuted";
  3400. break;
  3401. case efInternalEditor:
  3402. Counter = "RemoteFilesOpenedInInternalEditor";
  3403. break;
  3404. case efExternalEditor:
  3405. Counter = "RemoteFilesOpenedInExternalEditor";
  3406. break;
  3407. default:
  3408. DebugFail();
  3409. }
  3410. }
  3411. }
  3412. else
  3413. {
  3414. LocalFileName = FullFileName;
  3415. OriginalFileName = ExtractFileName(FullFileName);
  3416. switch (ExecuteFileBy)
  3417. {
  3418. case efShell:
  3419. Counter = "LocalFilesExecuted";
  3420. break;
  3421. case efInternalEditor:
  3422. Counter = "LocalFilesOpenedInInternalEditor";
  3423. break;
  3424. case efExternalEditor:
  3425. Counter = "LocalFilesOpenedInExternalEditor";
  3426. break;
  3427. default:
  3428. DebugFail();
  3429. }
  3430. }
  3431. if (!Handled)
  3432. {
  3433. Configuration->Usage->Inc(Counter);
  3434. CustomExecuteFile(Side, ExecuteFileBy, LocalFileName, OriginalFileName,
  3435. ExternalEditor, LocalRootDirectory, RemoteDirectory, false);
  3436. }
  3437. }
  3438. //---------------------------------------------------------------------------
  3439. void __fastcall TCustomScpExplorerForm::ExecuteFile(TOperationSide Side,
  3440. TExecuteFileBy ExecuteFileBy, const TEditorData * ExternalEditor,
  3441. bool AllSelected, bool OnFocused)
  3442. {
  3443. DebugAssert(!WinConfiguration->DisableOpenEdit);
  3444. DebugAssert((ExecuteFileBy == efExternalEditor) ==
  3445. ((ExternalEditor != NULL) && (ExternalEditor->Editor == edExternal)));
  3446. Side = GetSide(Side);
  3447. TCustomDirView * DView = DirView(Side);
  3448. TStrings * FileList =
  3449. AllSelected ?
  3450. DView->CreateFileList(OnFocused, IsSideLocalBrowser(Side)) :
  3451. DView->CreateFocusedFileList(IsSideLocalBrowser(Side));
  3452. try
  3453. {
  3454. DebugAssert(AllSelected || (FileList->Count == 1));
  3455. for (int i = 0; i < FileList->Count; i++)
  3456. {
  3457. UnicodeString ListFileName = FileList->Strings[i];
  3458. UnicodeString FileNameOnly =
  3459. !IsSideLocalBrowser(Side) ? UnixExtractFileName(ListFileName) : ExtractFileName(ListFileName);
  3460. TListItem * Item = DView->FindFileItem(FileNameOnly);
  3461. if (!DView->ItemIsDirectory(Item))
  3462. {
  3463. UnicodeString FullFileName;
  3464. if (!IsSideLocalBrowser(Side))
  3465. {
  3466. FullFileName = DirView(Side)->Path + ListFileName;
  3467. }
  3468. else
  3469. {
  3470. FullFileName = ListFileName;
  3471. }
  3472. TFileMasks::TParams MaskParams;
  3473. MaskParams.Size = DView->ItemFileSize(Item);
  3474. TDateTimePrecision Precision;
  3475. MaskParams.Modification = DView->ItemFileTime(Item, Precision);
  3476. ExecuteFile(Side, ExecuteFileBy, ExternalEditor, FullFileName,
  3477. FileList->Objects[i], MaskParams);
  3478. }
  3479. }
  3480. }
  3481. __finally
  3482. {
  3483. delete FileList;
  3484. }
  3485. }
  3486. //---------------------------------------------------------------------------
  3487. void __fastcall TCustomScpExplorerForm::TemporaryFileCopyParam(TCopyParamType & CopyParam)
  3488. {
  3489. CopyParam.FileNameCase = ncNoChange;
  3490. CopyParam.PreserveRights = false;
  3491. CopyParam.PreserveReadOnly = false;
  3492. CopyParam.ReplaceInvalidChars = true;
  3493. CopyParam.IncludeFileMask = TFileMasks();
  3494. CopyParam.NewerOnly = false;
  3495. CopyParam.FileMask = L"";
  3496. }
  3497. //---------------------------------------------------------------------------
  3498. void __fastcall TCustomScpExplorerForm::ExecutedFileChanged(
  3499. const UnicodeString & FileName, TEditedFileData * Data, HANDLE UploadCompleteEvent, bool & Retry)
  3500. {
  3501. TTerminalManager * Manager = TTerminalManager::Instance();
  3502. if (!IsActiveTerminal(Data->Terminal))
  3503. {
  3504. if (!NonVisualDataModule->Busy)
  3505. {
  3506. UnicodeString FileNameOnly = ExtractFileName(FileName);
  3507. UnicodeString EditInactiveSessionReopenAcceptedCounter = L"EditInactiveSessionReopenAccepted";
  3508. UnicodeString EditInactiveSessionReopenRejectedCounter = L"EditInactiveSessionReopenRejected";
  3509. if (Data->Terminal == NULL)
  3510. {
  3511. TTerminal * SameSiteTerminal = Manager->FindActiveTerminalForSite(Data->SessionData);
  3512. if (SameSiteTerminal != NULL)
  3513. {
  3514. UnicodeString Message =
  3515. FMTLOAD(EDIT_SESSION_REATTACH,
  3516. (FileNameOnly, Data->SessionName, FileNameOnly));
  3517. if (MessageDialog(Message, qtConfirmation, qaOK | qaCancel) == qaOK)
  3518. {
  3519. Data->Terminal = Terminal;
  3520. Data->Queue = Manager->FindQueueForTerminal(Terminal);
  3521. Data->SessionName = Terminal->SessionData->SessionName;
  3522. // We might also overwrite session data
  3523. Configuration->Usage->Inc(EditInactiveSessionReopenAcceptedCounter);
  3524. }
  3525. else
  3526. {
  3527. Configuration->Usage->Inc(EditInactiveSessionReopenRejectedCounter);
  3528. Abort();
  3529. }
  3530. }
  3531. }
  3532. // foreground session should reconnect itself
  3533. else if (Terminal != Data->Terminal)
  3534. {
  3535. UnicodeString Message =
  3536. MainInstructions(
  3537. FMTLOAD(EDIT_SESSION_RECONNECT, (Data->SessionName, FileNameOnly)));
  3538. if (MessageDialog(Message, qtConfirmation, qaOK | qaCancel) == qaOK)
  3539. {
  3540. Manager->SetActiveTerminalWithAutoReconnect(Data->Terminal);
  3541. Configuration->Usage->Inc(EditInactiveSessionReopenAcceptedCounter);
  3542. }
  3543. else
  3544. {
  3545. Configuration->Usage->Inc(EditInactiveSessionReopenRejectedCounter);
  3546. Abort();
  3547. }
  3548. }
  3549. }
  3550. if (!IsActiveTerminal(Data->Terminal))
  3551. {
  3552. Configuration->Usage->Inc(L"EditInactiveSession");
  3553. // Prevent this when not idle (!NonVisualDataModule->Busy)?
  3554. throw Exception(FMTLOAD(EDIT_SESSION_CLOSED2,
  3555. (ExtractFileName(FileName), Data->SessionName)));
  3556. }
  3557. }
  3558. TStrings * FileList = new TStringList();
  3559. try
  3560. {
  3561. FileList->Add(FileName);
  3562. // consider using the same settings (preset) as when the file was downloaded
  3563. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  3564. TemporaryFileCopyParam(CopyParam);
  3565. if (Data->ForceText)
  3566. {
  3567. CopyParam.TransferMode = tmAscii;
  3568. }
  3569. // so i do not need to worry if masking algorithm works in all cases
  3570. // ("" means "copy file name", no masking is actually done)
  3571. if (ExtractFileName(FileName) == Data->OriginalFileName)
  3572. {
  3573. CopyParam.FileMask = L"";
  3574. }
  3575. else
  3576. {
  3577. CopyParam.FileMask = DelimitFileNameMask(Data->OriginalFileName);
  3578. }
  3579. int Params = cpNoConfirmation | cpTemporary;
  3580. if (Terminal->IsCapable[fcBackgroundTransfers])
  3581. {
  3582. DebugAssert(Data->Queue != NULL);
  3583. TQueueItem * QueueItem = new TUploadQueueItem(Data->Terminal, FileList,
  3584. Data->RemoteDirectory, &CopyParam, Params, true, false);
  3585. QueueItem->CompleteEvent = UploadCompleteEvent;
  3586. AddQueueItem(Data->Queue, QueueItem, Data->Terminal);
  3587. }
  3588. else
  3589. {
  3590. if (NonVisualDataModule->Busy)
  3591. {
  3592. Retry = true;
  3593. }
  3594. else
  3595. {
  3596. Terminal->CopyToRemote(FileList, Data->RemoteDirectory, &CopyParam, Params, NULL);
  3597. SetEvent(UploadCompleteEvent);
  3598. }
  3599. }
  3600. }
  3601. __finally
  3602. {
  3603. delete FileList;
  3604. }
  3605. }
  3606. //---------------------------------------------------------------------------
  3607. void __fastcall TCustomScpExplorerForm::ExecutedFileReload(
  3608. const UnicodeString FileName, const TEditedFileData * Data)
  3609. {
  3610. // Sanity check, we should not be busy otherwise user would not be able to click Reload button.
  3611. DebugAssert(!NonVisualDataModule->Busy);
  3612. if (!IsActiveTerminal(Data->Terminal))
  3613. {
  3614. throw Exception(FMTLOAD(EDIT_SESSION_CLOSED_RELOAD,
  3615. (ExtractFileName(FileName), Data->SessionName)));
  3616. }
  3617. TManagedTerminal * PrevSession = TTerminalManager::Instance()->ActiveSession;
  3618. TTerminalManager::Instance()->ActiveSession = Data->Terminal;
  3619. NonVisualDataModule->StartBusy();
  3620. try
  3621. {
  3622. std::unique_ptr<TRemoteFile> File;
  3623. UnicodeString RemoteFileName =
  3624. UnixIncludeTrailingBackslash(Data->RemoteDirectory) + Data->OriginalFileName;
  3625. Terminal->ExceptionOnFail = true;
  3626. try
  3627. {
  3628. TRemoteFile * AFile = NULL;
  3629. Terminal->ReadFile(RemoteFileName, AFile);
  3630. File.reset(AFile);
  3631. if (!File->HaveFullFileName)
  3632. {
  3633. File->FullFileName = RemoteFileName;
  3634. }
  3635. }
  3636. __finally
  3637. {
  3638. Terminal->ExceptionOnFail = false;
  3639. }
  3640. std::unique_ptr<TStrings> FileList(new TStringList());
  3641. FileList->AddObject(RemoteFileName, File.get());
  3642. UnicodeString RootTempDir = Data->LocalRootDirectory;
  3643. UnicodeString TempDir = ExtractFilePath(FileName);
  3644. TemporarilyDownloadFiles(FileList.get(), Data->ForceText, RootTempDir, TempDir, true, true);
  3645. // sanity check, the target file name should be still the same
  3646. DebugAssert(ExtractFileName(FileName) == FileList->Strings[0]);
  3647. }
  3648. __finally
  3649. {
  3650. NonVisualDataModule->EndBusy();
  3651. // it actually may not exist anymore...
  3652. TTerminalManager::Instance()->ActiveSession = PrevSession;
  3653. }
  3654. }
  3655. //---------------------------------------------------------------------------
  3656. void __fastcall TCustomScpExplorerForm::ExecutedFileEarlyClosed(
  3657. const TEditedFileData * Data, bool & KeepOpen)
  3658. {
  3659. // Command is set for external editors only (not for "shell" open).
  3660. if (!Data->Command.IsEmpty())
  3661. {
  3662. bool AnyFound = false;
  3663. bool AnyMDI = false;
  3664. bool AnyNonMDI = false;
  3665. bool AnyDetect = false;
  3666. TEditorList * EditorList = new TEditorList();
  3667. try
  3668. {
  3669. *EditorList = *WinConfiguration->EditorList;
  3670. for (int i = 0; i < EditorList->Count; i++)
  3671. {
  3672. const TEditorPreferences * Editor = EditorList->Editors[i];
  3673. if ((Editor->Data->Editor == edExternal) &&
  3674. (Editor->Data->ExternalEditor == Data->Command))
  3675. {
  3676. AnyFound = true;
  3677. if (Editor->Data->SDIExternalEditor)
  3678. {
  3679. AnyNonMDI = true;
  3680. if (Editor->Data->DetectMDIExternalEditor)
  3681. {
  3682. AnyDetect = true;
  3683. }
  3684. }
  3685. else
  3686. {
  3687. AnyMDI = true;
  3688. }
  3689. }
  3690. }
  3691. bool EnableMDI = false;
  3692. bool DisableDetect = false;
  3693. if (AnyMDI)
  3694. {
  3695. KeepOpen = true;
  3696. if (AnyNonMDI)
  3697. {
  3698. // there is at least one instance of the editor with MDI support enabled,
  3699. // and one with disabled, enable it for all instances
  3700. EnableMDI = true;
  3701. }
  3702. }
  3703. else if (AnyFound && !AnyDetect)
  3704. {
  3705. // at least once instance found but all have MDI autodetection disabled
  3706. // => close the file (default action)
  3707. }
  3708. else
  3709. {
  3710. // no instance of the editor has MDI support enabled
  3711. TMessageParams Params;
  3712. if (AnyFound)
  3713. {
  3714. // there is at least one instance of the editor
  3715. Params.Params |= mpNeverAskAgainCheck;
  3716. }
  3717. unsigned int Answer = MessageDialog(FMTLOAD(EDITOR_EARLY_CLOSED2, (Data->OriginalFileName)), qtWarning,
  3718. qaYes | qaNo, HELP_EDITOR_EARLY_CLOSED, &Params);
  3719. switch (Answer)
  3720. {
  3721. case qaNeverAskAgain:
  3722. DisableDetect = true;
  3723. break;
  3724. case qaNo:
  3725. EnableMDI = true;
  3726. KeepOpen = true;
  3727. break;
  3728. }
  3729. }
  3730. if (AnyFound && (EnableMDI || DisableDetect))
  3731. {
  3732. bool Changed = false;
  3733. for (int i = 0; i < EditorList->Count; i++)
  3734. {
  3735. const TEditorPreferences * Editor = EditorList->Editors[i];
  3736. if ((Editor->Data->Editor == edExternal) &&
  3737. (Editor->Data->ExternalEditor == Data->Command) &&
  3738. ((EnableMDI && Editor->Data->SDIExternalEditor) ||
  3739. (DisableDetect && Editor->Data->DetectMDIExternalEditor)))
  3740. {
  3741. Changed = true;
  3742. TEditorPreferences * UpdatedEditor = new TEditorPreferences(*Editor);
  3743. if (EnableMDI)
  3744. {
  3745. UpdatedEditor->GetData()->SDIExternalEditor = false;
  3746. }
  3747. if (DisableDetect)
  3748. {
  3749. UpdatedEditor->GetData()->DetectMDIExternalEditor = false;
  3750. }
  3751. EditorList->Change(i, UpdatedEditor);
  3752. }
  3753. }
  3754. if (Changed)
  3755. {
  3756. WinConfiguration->EditorList = EditorList;
  3757. }
  3758. }
  3759. }
  3760. __finally
  3761. {
  3762. delete EditorList;
  3763. }
  3764. }
  3765. else
  3766. {
  3767. // "open" case
  3768. MessageDialog(FMTLOAD(APP_EARLY_CLOSED, (Data->OriginalFileName)), qtWarning,
  3769. qaOK, HELP_APP_EARLY_CLOSED);
  3770. }
  3771. }
  3772. //---------------------------------------------------------------------------
  3773. void __fastcall TCustomScpExplorerForm::ExecutedFileUploadComplete(TObject * Sender)
  3774. {
  3775. EditorFormFileUploadComplete(DebugNotNull(dynamic_cast<TForm *>(Sender)));
  3776. }
  3777. //---------------------------------------------------------------------------
  3778. void __fastcall TCustomScpExplorerForm::RemoteDirViewEnter(TObject * /*Sender*/)
  3779. {
  3780. SideEnter(osRemote);
  3781. }
  3782. //---------------------------------------------------------------------------
  3783. void __fastcall TCustomScpExplorerForm::RemoteDriveViewEnter(TObject * /*Sender*/)
  3784. {
  3785. MakeNextInTabOrder(RemoteDirPanel, RemoteDrivePanel);
  3786. SideEnter(osRemote);
  3787. }
  3788. //---------------------------------------------------------------------------
  3789. void __fastcall TCustomScpExplorerForm::SideEnter(TOperationSide Side)
  3790. {
  3791. FCurrentSide = Side;
  3792. if (Visible)
  3793. {
  3794. UpdateControls();
  3795. }
  3796. }
  3797. //---------------------------------------------------------------------------
  3798. void __fastcall TCustomScpExplorerForm::FileDeleted(TOperationSide Side, const UnicodeString & FileName, bool Success)
  3799. {
  3800. VisualiseOperationFinished(Side, FileName, Success);
  3801. if (DebugAlwaysTrue(FDeletedFiles != NULL) && Success)
  3802. {
  3803. FDeletedFiles->Add(FileName);
  3804. }
  3805. }
  3806. //---------------------------------------------------------------------------
  3807. void __fastcall TCustomScpExplorerForm::DeleteFiles(TOperationSide Side,
  3808. TStrings * FileList, bool Alternative)
  3809. {
  3810. TCustomDirView * DView = DirView(Side);
  3811. DView->SaveSelection();
  3812. DView->SaveSelectedNames();
  3813. DebugAssert(!FAlternativeDelete);
  3814. FAlternativeDelete = Alternative;
  3815. try
  3816. {
  3817. if (!IsSideLocalBrowser(Side))
  3818. {
  3819. DebugAssert(Terminal != NULL);
  3820. // Clone the file list as it may refer to current directory files, which get destroyed, when the directory is reloaded after the operation
  3821. std::unique_ptr<TStrings> PermanentFileList(TRemoteFileList::CloneStrings(FileList));
  3822. std::unique_ptr<TStringList> DeletedFiles(new TStringList());
  3823. DeletedFiles->CaseSensitive = DebugAlwaysTrue(!IsSideLocalBrowser(Side));
  3824. int Params = FLAGMASK(Alternative, dfAlternative);
  3825. try
  3826. {
  3827. TValueRestorer<TStrings *> DeletedFilesRestorer(FDeletedFiles);
  3828. DebugAssert(FDeletedFiles == NULL);
  3829. FDeletedFiles = DeletedFiles.get();
  3830. TValueRestorer<TFileOperationFinishedEvent> OnFileOperationFinishedRestorer(FOnFileOperationFinished);
  3831. FOnFileOperationFinished = FileDeleted;
  3832. FMoveToQueue = false;
  3833. Terminal->DeleteFiles(FileList, Params);
  3834. // Probably not needed for deleting, just for consistency with transfer code
  3835. if (FMoveToQueue)
  3836. {
  3837. Abort();
  3838. }
  3839. }
  3840. catch (EAbort &)
  3841. {
  3842. if (FMoveToQueue)
  3843. {
  3844. DeletedFiles->Sorted = true;
  3845. for (int Index = 0; Index < PermanentFileList->Count; Index++)
  3846. {
  3847. if (DeletedFiles->IndexOf(PermanentFileList->Strings[Index]) >= 0)
  3848. {
  3849. // We should always be deleting the first item => what can be used to optimize this code, if needed
  3850. DebugAssert(Index == 0);
  3851. PermanentFileList->Delete(Index);
  3852. Index--;
  3853. }
  3854. }
  3855. Configuration->Usage->Inc("MovesToBackgroundDelete");
  3856. TQueueItem * QueueItem = new TDeleteQueueItem(Terminal, PermanentFileList.get(), Params);
  3857. AddQueueItem(Queue, QueueItem, Terminal);
  3858. ClearOperationSelection(osRemote);
  3859. }
  3860. }
  3861. }
  3862. else
  3863. {
  3864. if (IsLocalBrowserMode())
  3865. {
  3866. Configuration->Usage->Inc(L"LocalLocalDeletes");
  3867. }
  3868. TValueRestorer<TOperationSide> ProgressSideRestorer(FProgressSide);
  3869. FProgressSide = FCurrentSide;
  3870. try
  3871. {
  3872. TTerminalManager::Instance()->LocalTerminal->DeleteLocalFiles(FileList, FLAGMASK(Alternative, dfAlternative));
  3873. }
  3874. __finally
  3875. {
  3876. ReloadLocalDirectory();
  3877. }
  3878. }
  3879. FAlternativeDelete = false;
  3880. }
  3881. catch(...)
  3882. {
  3883. FAlternativeDelete = false;
  3884. DView->DiscardSavedSelection();
  3885. throw;
  3886. }
  3887. DView->RestoreSelection();
  3888. }
  3889. //---------------------------------------------------------------------------
  3890. void __fastcall TCustomScpExplorerForm::LockFiles(TStrings * FileList, bool Lock)
  3891. {
  3892. DebugAssert(Terminal);
  3893. DebugAssert(!IsLocalBrowserMode());
  3894. RemoteDirView->SaveSelection();
  3895. RemoteDirView->SaveSelectedNames();
  3896. try
  3897. {
  3898. if (Lock)
  3899. {
  3900. Terminal->LockFiles(FileList);
  3901. }
  3902. else
  3903. {
  3904. Terminal->UnlockFiles(FileList);
  3905. }
  3906. }
  3907. catch(...)
  3908. {
  3909. RemoteDirView->DiscardSavedSelection();
  3910. throw;
  3911. }
  3912. RemoteDirView->RestoreSelection();
  3913. }
  3914. //---------------------------------------------------------------------------
  3915. bool __fastcall TCustomScpExplorerForm::RemoteTransferDialog(TManagedTerminal *& Session,
  3916. TStrings * FileList, UnicodeString & Target, UnicodeString & FileMask, bool & DirectCopy,
  3917. bool NoConfirmation, bool Move)
  3918. {
  3919. DebugAssert(Terminal != NULL);
  3920. DebugAssert(Terminal == TTerminalManager::Instance()->ActiveTerminal);
  3921. DebugAssert(!IsLocalBrowserMode());
  3922. if (Session == NULL)
  3923. {
  3924. Session = Terminal;
  3925. }
  3926. if (Session == Terminal)
  3927. {
  3928. if (RemoteDriveView->DropTarget != NULL)
  3929. {
  3930. Target = RemoteDriveView->NodePathName(RemoteDriveView->DropTarget);
  3931. }
  3932. else if (RemoteDirView->DropTarget != NULL)
  3933. {
  3934. DebugAssert(RemoteDirView->ItemIsDirectory(RemoteDirView->DropTarget));
  3935. Target = RemoteDirView->ItemFullFileName(RemoteDirView->DropTarget);
  3936. }
  3937. else
  3938. {
  3939. Target = RemoteDirView->Path;
  3940. }
  3941. }
  3942. else
  3943. {
  3944. Target = GetSessionPath(Session, osRemote);
  3945. }
  3946. Target = UnixIncludeTrailingBackslash(Target);
  3947. if (FileList->Count == 1)
  3948. {
  3949. FileMask = DelimitFileNameMask(UnixExtractFileName(FileList->Strings[0]));
  3950. }
  3951. else
  3952. {
  3953. FileMask = AnyMask;
  3954. }
  3955. DirectCopy = Terminal->IsCapable[fcRemoteCopy] || Terminal->IsCapable[fcSecondaryShell];
  3956. bool Result = true;
  3957. if (!NoConfirmation)
  3958. {
  3959. bool Multi = (FileList->Count > 1);
  3960. if (Move)
  3961. {
  3962. Result = DoRemoteMoveDialog(Multi, Target, FileMask);
  3963. }
  3964. else
  3965. {
  3966. std::unique_ptr<TStrings> Sessions(new TStringList);
  3967. std::unique_ptr<TStrings> Directories(new TStringList);
  3968. TStrings * SessionList = TTerminalManager::Instance()->SessionList;
  3969. for (int Index = 0; Index < SessionList->Count; Index++)
  3970. {
  3971. TManagedTerminal * ASession = DebugNotNull(dynamic_cast<TManagedTerminal *>(SessionList->Objects[Index]));
  3972. if (IsActiveTerminal(ASession) && DebugAlwaysTrue(!ASession->LocalBrowser))
  3973. {
  3974. Sessions->AddObject(SessionList->Strings[Index], ASession);
  3975. Directories->Add(GetSessionPath(ASession, osRemote));
  3976. }
  3977. }
  3978. TDirectRemoteCopy AllowDirectCopy;
  3979. if (Terminal->IsCapable[fcRemoteCopy] || Terminal->CommandSessionOpened)
  3980. {
  3981. DebugAssert(DirectCopy);
  3982. AllowDirectCopy = drcAllow;
  3983. }
  3984. else if (Terminal->IsCapable[fcSecondaryShell])
  3985. {
  3986. DebugAssert(DirectCopy);
  3987. AllowDirectCopy = drcConfirmCommandSession;
  3988. }
  3989. else
  3990. {
  3991. DebugAssert(!DirectCopy);
  3992. AllowDirectCopy = drcDisallow;
  3993. }
  3994. void * ASession = Session;
  3995. Result = DoRemoteCopyDialog(
  3996. Sessions.get(), Directories.get(), AllowDirectCopy, Multi, ASession, Target, FileMask, DirectCopy, Terminal);
  3997. Session = static_cast<TManagedTerminal *>(ASession);
  3998. }
  3999. }
  4000. return Result;
  4001. }
  4002. //---------------------------------------------------------------------------
  4003. bool __fastcall TCustomScpExplorerForm::RemoteTransferFiles(
  4004. TStrings * FileList, bool NoConfirmation, bool Move, TManagedTerminal * Session)
  4005. {
  4006. DebugAssert(!IsLocalBrowserMode());
  4007. bool DirectCopy;
  4008. UnicodeString Target, FileMask;
  4009. bool Result = RemoteTransferDialog(Session, FileList, Target, FileMask, DirectCopy, NoConfirmation, Move);
  4010. if (Result)
  4011. {
  4012. if (!Move && !DirectCopy)
  4013. {
  4014. Configuration->Usage->Inc("RemoteCopyTemp");
  4015. UnicodeString RootTempDir;
  4016. UnicodeString TempDir;
  4017. TemporarilyDownloadFiles(FileList, false, RootTempDir, TempDir, false, false);
  4018. TStrings * TemporaryFilesList = new TStringList();
  4019. try
  4020. {
  4021. TMakeLocalFileListParams MakeFileListParam;
  4022. MakeFileListParam.FileList = TemporaryFilesList;
  4023. MakeFileListParam.FileTimes = NULL;
  4024. MakeFileListParam.IncludeDirs = true;
  4025. MakeFileListParam.Recursive = false;
  4026. ProcessLocalDirectory(TempDir, Terminal->MakeLocalFileList, &MakeFileListParam);
  4027. TTerminalManager::Instance()->ActiveSession = Session;
  4028. if (TemporaryFilesList->Count > 0)
  4029. {
  4030. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  4031. CopyParam.FileMask = FileMask;
  4032. DebugAssert(!FAutoOperation);
  4033. FAutoOperation = true;
  4034. Terminal->CopyToRemote(TemporaryFilesList, Target, &CopyParam, cpTemporary, NULL);
  4035. }
  4036. }
  4037. __finally
  4038. {
  4039. delete TemporaryFilesList;
  4040. FAutoOperation = false;
  4041. if (!RootTempDir.IsEmpty())
  4042. {
  4043. RecursiveDeleteFile(ExcludeTrailingBackslash(RootTempDir), false);
  4044. }
  4045. }
  4046. }
  4047. else
  4048. {
  4049. RemoteDirView->SaveSelection();
  4050. RemoteDirView->SaveSelectedNames();
  4051. try
  4052. {
  4053. if (Move)
  4054. {
  4055. Configuration->Usage->Inc("RemoteMove");
  4056. Terminal->MoveFiles(FileList, Target, FileMask);
  4057. }
  4058. else
  4059. {
  4060. Configuration->Usage->Inc("RemoteCopyDirect");
  4061. DebugAssert(DirectCopy);
  4062. DebugAssert(Session == Terminal);
  4063. if (Terminal->IsCapable[fcRemoteCopy] ||
  4064. Terminal->CommandSessionOpened ||
  4065. CommandSessionFallback())
  4066. {
  4067. Terminal->CopyFiles(FileList, Target, FileMask);
  4068. }
  4069. }
  4070. }
  4071. catch(...)
  4072. {
  4073. RemoteDirView->DiscardSavedSelection();
  4074. throw;
  4075. }
  4076. RemoteDirView->RestoreSelection();
  4077. }
  4078. }
  4079. return Result;
  4080. }
  4081. //---------------------------------------------------------------------------
  4082. void __fastcall TCustomScpExplorerForm::CreateRemoteDirectory(
  4083. const UnicodeString & Path, TRemoteProperties & Properties)
  4084. {
  4085. DebugAssert(!IsLocalBrowserMode());
  4086. Properties.Valid = Properties.Valid << vpEncrypt;
  4087. Properties.Encrypt = GUIConfiguration->CurrentCopyParam.EncryptNewFiles;
  4088. RemoteDirView->CreateDirectoryEx(Path, &Properties);
  4089. }
  4090. //---------------------------------------------------------------------------
  4091. void __fastcall TCustomScpExplorerForm::CreateDirectory(TOperationSide Side)
  4092. {
  4093. Side = GetSide(Side);
  4094. TRemoteProperties Properties = GUIConfiguration->NewDirectoryProperties;
  4095. TRemoteProperties * AProperties = (!IsSideLocalBrowser(Side) ? &Properties : NULL);
  4096. UnicodeString Name = LoadStr(NEW_FOLDER);
  4097. int AllowedChanges =
  4098. FLAGMASK(!IsSideLocalBrowser(Side) && Terminal->IsCapable[fcModeChanging], cpMode);
  4099. bool SaveSettings = false;
  4100. if (DoCreateDirectoryDialog(Name, AProperties, AllowedChanges, SaveSettings))
  4101. {
  4102. TWindowLock Lock(this);
  4103. if (!IsSideLocalBrowser(Side))
  4104. {
  4105. if (SaveSettings)
  4106. {
  4107. GUIConfiguration->NewDirectoryProperties = Properties;
  4108. }
  4109. CreateRemoteDirectory(Name, Properties);
  4110. }
  4111. else
  4112. {
  4113. DirView(Side)->CreateDirectory(Name);
  4114. }
  4115. }
  4116. }
  4117. //---------------------------------------------------------------------------
  4118. void __fastcall TCustomScpExplorerForm::HomeDirectory(TOperationSide Side)
  4119. {
  4120. TWindowLock Lock(this);
  4121. DirView(Side)->ExecuteHomeDirectory();
  4122. }
  4123. //---------------------------------------------------------------------------
  4124. void __fastcall TCustomScpExplorerForm::OpenDirectory(TOperationSide Side)
  4125. {
  4126. DoOpenDirectoryDialog(odBrowse, Side);
  4127. }
  4128. //---------------------------------------------------------------------------
  4129. bool __fastcall TCustomScpExplorerForm::OpenBookmark(TOperationSide Side, TBookmark * Bookmark)
  4130. {
  4131. TOperationSide BookmarkSide = IsLocalBrowserMode() ? osLocal : Side;
  4132. UnicodeString Path = Bookmark->GetSideDirectory(BookmarkSide);
  4133. bool Result = !Path.IsEmpty();
  4134. if (Result)
  4135. {
  4136. // While we might get here when the session is closed (from location profiles),
  4137. // it's not a problem as the Path setter is noop then
  4138. DirView(Side)->Path = Path;
  4139. }
  4140. return Result;
  4141. }
  4142. //---------------------------------------------------------------------------
  4143. void __fastcall TCustomScpExplorerForm::CalculateSize(
  4144. TStrings * FileList, __int64 & Size, TCalculateSizeStats & Stats,
  4145. bool & Close)
  4146. {
  4147. // terminal can be already closed (e.g. dropped connection)
  4148. if (Terminal != NULL)
  4149. {
  4150. try
  4151. {
  4152. TCalculateSizeParams Params;
  4153. Params.Stats = &Stats;
  4154. Terminal->CalculateFilesSize(FileList, Size, Params);
  4155. }
  4156. catch(...)
  4157. {
  4158. if (!Terminal->Active)
  4159. {
  4160. Close = true;
  4161. }
  4162. throw;
  4163. }
  4164. }
  4165. }
  4166. //---------------------------------------------------------------------------
  4167. void __fastcall TCustomScpExplorerForm::CalculateChecksum(const UnicodeString & Alg,
  4168. TStrings * FileList, TCalculatedChecksumEvent OnCalculatedChecksum,
  4169. bool & Close)
  4170. {
  4171. // terminal can be already closed (e.g. dropped connection)
  4172. if (Terminal != NULL)
  4173. {
  4174. Configuration->Usage->Inc(L"ChecksumCalculated");
  4175. try
  4176. {
  4177. Terminal->CalculateFilesChecksum(Alg, FileList, NULL, OnCalculatedChecksum);
  4178. }
  4179. catch(...)
  4180. {
  4181. if (!Terminal->Active)
  4182. {
  4183. Close = true;
  4184. }
  4185. throw;
  4186. }
  4187. }
  4188. }
  4189. //---------------------------------------------------------------------------
  4190. void TCustomScpExplorerForm::LoadFilesProperties(TStrings * FileList)
  4191. {
  4192. if (Terminal->LoadFilesProperties(FileList))
  4193. {
  4194. RemoteDirView->Invalidate();
  4195. }
  4196. }
  4197. //---------------------------------------------------------------------------
  4198. bool __fastcall TCustomScpExplorerForm::SetProperties(TOperationSide Side, TStrings * FileList)
  4199. {
  4200. bool Result;
  4201. if (!IsSideLocalBrowser(Side))
  4202. {
  4203. TRemoteTokenList * GroupList = NULL;
  4204. TRemoteTokenList * UserList = NULL;
  4205. try
  4206. {
  4207. TRemoteProperties CurrentProperties;
  4208. LoadFilesProperties(FileList);
  4209. bool CapableGroupChanging = Terminal->IsCapable[fcGroupChanging];
  4210. bool CapableOwnerChanging = Terminal->IsCapable[fcOwnerChanging];
  4211. if (CapableGroupChanging || CapableOwnerChanging)
  4212. {
  4213. if (CapableGroupChanging)
  4214. {
  4215. GroupList = Terminal->Groups->Duplicate();
  4216. }
  4217. if (CapableOwnerChanging)
  4218. {
  4219. UserList = Terminal->Users->Duplicate();
  4220. }
  4221. TRemoteDirectory * Files = Terminal->Files;
  4222. int Count = Files->Count;
  4223. if (Count > 100)
  4224. {
  4225. Count = 100;
  4226. }
  4227. for (int Index = 0; Index < Count; Index++)
  4228. {
  4229. TRemoteFile * File = Files->Files[Index];
  4230. if (CapableGroupChanging)
  4231. {
  4232. GroupList->AddUnique(File->Group);
  4233. }
  4234. if (CapableOwnerChanging)
  4235. {
  4236. UserList->AddUnique(File->Owner);
  4237. }
  4238. }
  4239. // if we haven't collected tokens for all files in current directory,
  4240. // make sure we collect them at least for all selected files.
  4241. // (note that so far the files in FileList has to be from current direcotry)
  4242. if (Count < Files->Count)
  4243. {
  4244. for (int Index = 0; Index < FileList->Count; Index++)
  4245. {
  4246. TRemoteFile * File = (TRemoteFile *)(FileList->Objects[Index]);
  4247. if (CapableGroupChanging)
  4248. {
  4249. GroupList->AddUnique(File->Group);
  4250. }
  4251. if (CapableOwnerChanging)
  4252. {
  4253. UserList->AddUnique(File->Owner);
  4254. }
  4255. }
  4256. }
  4257. }
  4258. CurrentProperties = TRemoteProperties::CommonProperties(FileList);
  4259. bool CapableAclChanging = Terminal->IsCapable[fcAclChangingFiles];
  4260. for (int Index = 0; (Index < FileList->Count) && CapableAclChanging; Index++)
  4261. {
  4262. if (dynamic_cast<TRemoteFile *>(FileList->Objects[Index])->IsDirectory)
  4263. {
  4264. CapableAclChanging = false;
  4265. CurrentProperties.Valid = CurrentProperties.Valid >> vpRights;
  4266. }
  4267. }
  4268. int Flags = 0;
  4269. if (Terminal->IsCapable[fcModeChanging]) Flags |= cpMode;
  4270. if (CapableAclChanging) Flags |= cpAcl;
  4271. if (CapableOwnerChanging) Flags |= cpOwner;
  4272. if (CapableGroupChanging) Flags |= cpGroup;
  4273. TCalculateChecksumEvent CalculateChecksumEvent = NULL;
  4274. if (Terminal->IsCapable[fcCalculatingChecksum])
  4275. {
  4276. CalculateChecksumEvent = CalculateChecksum;
  4277. }
  4278. std::unique_ptr<TStrings> ChecksumAlgs(new TStringList());
  4279. Terminal->GetSupportedChecksumAlgs(ChecksumAlgs.get());
  4280. TRemoteProperties NewProperties = CurrentProperties;
  4281. Result =
  4282. DoPropertiesDialog(FileList, RemoteDirView->PathName,
  4283. GroupList, UserList, ChecksumAlgs.get(), &NewProperties, Flags,
  4284. Terminal->IsCapable[fcGroupOwnerChangingByID],
  4285. CalculateSize, CalculateChecksumEvent);
  4286. if (Result)
  4287. {
  4288. NewProperties = TRemoteProperties::ChangedProperties(CurrentProperties, NewProperties);
  4289. Terminal->ChangeFilesProperties(FileList, &NewProperties);
  4290. }
  4291. }
  4292. __finally
  4293. {
  4294. delete GroupList;
  4295. delete UserList;
  4296. }
  4297. }
  4298. else
  4299. {
  4300. DirView(Side)->DisplayPropertiesMenu();
  4301. Result = true;
  4302. }
  4303. return Result;
  4304. }
  4305. //---------------------------------------------------------------------------
  4306. void __fastcall TCustomScpExplorerForm::KeyProcessed(Word & Key, TShiftState Shift)
  4307. {
  4308. if (Shift * AllKeyShiftStates() == (TShiftState() << ssAlt))
  4309. {
  4310. FIgnoreNextDialogChar = Key;
  4311. }
  4312. Key = 0;
  4313. }
  4314. //---------------------------------------------------------------------------
  4315. void __fastcall TCustomScpExplorerForm::CheckCustomCommandShortCut(
  4316. TCustomCommandList * List, Word & Key, Classes::TShiftState Shift, TShortCut KeyShortCut)
  4317. {
  4318. const TCustomCommandType * Command = List->Find(KeyShortCut);
  4319. if (Command != NULL)
  4320. {
  4321. KeyProcessed(Key, Shift);
  4322. if (CustomCommandState(*Command, false, ccltAll) > 0)
  4323. {
  4324. TAutoFlag DontCopyCommandToClipboardFlag(DontCopyCommandToClipboard);
  4325. ExecuteFileOperationCommand(foCustomCommand, osRemote,
  4326. false, false, const_cast<TCustomCommandType *>(Command));
  4327. }
  4328. }
  4329. }
  4330. //---------------------------------------------------------------------------
  4331. void __fastcall TCustomScpExplorerForm::KeyDown(Word & Key, Classes::TShiftState Shift)
  4332. {
  4333. if (!NonVisualDataModule->Busy)
  4334. {
  4335. if (QueueView3->Focused() && (QueueView3->OnKeyDown != NULL))
  4336. {
  4337. QueueView3->OnKeyDown(QueueView3, Key, Shift);
  4338. }
  4339. if ((Key != 0) && !DirView(osCurrent)->IsEditing())
  4340. {
  4341. TShortCut KeyShortCut = ShortCut(Key, Shift);
  4342. for (int Index = 0; Index < NonVisualDataModule->ExplorerActions->ActionCount; Index++)
  4343. {
  4344. // Note that handling shortcuts on panel (current control) popup menu has precendence over this.
  4345. // But so far it's not a problem, as it does what we want.
  4346. TAction * Action = (TAction *)NonVisualDataModule->ExplorerActions->Actions[Index];
  4347. if (((Action->ShortCut == KeyShortCut) ||
  4348. (Action->SecondaryShortCuts->IndexOfShortCut(KeyShortCut) >= 0)) &&
  4349. AllowedAction(Action, aaShortCut))
  4350. {
  4351. // Has to be called before the action as the dialog char is already in queue.
  4352. // So when the action consumes message queue, we already need to have the
  4353. // FIgnoreNextDialogChar set
  4354. KeyProcessed(Key, Shift);
  4355. // Reset reference to previous component (when menu/toolbar was clicked).
  4356. // Needed to detect that action was invoked by keyboard shortcut
  4357. // in TNonVisualDataModule::ExplorerActionsExecute
  4358. Action->ActionComponent = NULL;
  4359. Action->Execute();
  4360. return;
  4361. }
  4362. }
  4363. for (int i = 0; i < TTerminalManager::Instance()->Count; i++)
  4364. {
  4365. if (NonVisualDataModule->OpenSessionShortCut(i) == KeyShortCut)
  4366. {
  4367. KeyProcessed(Key, Shift);
  4368. TTerminalManager::Instance()->ActiveSessionIndex = i;
  4369. return;
  4370. }
  4371. }
  4372. if (Key == VK_TAB && Shift.Contains(ssCtrl))
  4373. {
  4374. KeyProcessed(Key, Shift);
  4375. TTerminalManager::Instance()->CycleTerminals(!Shift.Contains(ssShift));
  4376. }
  4377. TShortCut CustomShortCut = NormalizeCustomShortCut(KeyShortCut);
  4378. if (IsCustomShortCut(CustomShortCut))
  4379. {
  4380. CheckCustomCommandShortCut(WinConfiguration->CustomCommandList, Key, Shift, CustomShortCut);
  4381. CheckCustomCommandShortCut(WinConfiguration->ExtensionList, Key, Shift, CustomShortCut);
  4382. if (WinConfiguration->SharedBookmarks != NULL)
  4383. {
  4384. TBookmark * Bookmark = WinConfiguration->SharedBookmarks->FindByShortCut(CustomShortCut);
  4385. if ((Bookmark != NULL) &&
  4386. OpenBookmark(FCurrentSide, Bookmark))
  4387. {
  4388. KeyProcessed(Key, Shift);
  4389. }
  4390. }
  4391. }
  4392. }
  4393. }
  4394. TForm::KeyDown(Key, Shift);
  4395. }
  4396. //---------------------------------------------------------------------------
  4397. void __fastcall TCustomScpExplorerForm::InitStatusBar()
  4398. {
  4399. bool OpenedTerminal = false;
  4400. UnicodeString ProtocolName;
  4401. UnicodeString ProtocolNameHint;
  4402. UnicodeString SecurityProtocolName;
  4403. UnicodeString DurationHint;
  4404. // See the comment in TScpCommanderForm::UpdateControls()
  4405. TTerminal * ActiveTerminal = TTerminalManager::Instance()->ActiveTerminal;
  4406. if (ActiveTerminal != NULL)
  4407. {
  4408. // Active is not enough here
  4409. if (ActiveTerminal->Status == ssOpened)
  4410. {
  4411. const TSessionInfo & SessionInfo = ActiveTerminal->GetSessionInfo();
  4412. const TFileSystemInfo & FileSystemInfo = ActiveTerminal->GetFileSystemInfo();
  4413. if (FileSystemInfo.ProtocolName.IsEmpty())
  4414. {
  4415. ProtocolName = SessionInfo.ProtocolName;
  4416. }
  4417. else
  4418. {
  4419. ProtocolName = FileSystemInfo.ProtocolName;
  4420. }
  4421. ProtocolNameHint = LoadStr(STATUS_PROTOCOL_HINT);
  4422. SecurityProtocolName = SessionInfo.SecurityProtocolName;
  4423. DurationHint = LoadStr(STATUS_DURATION_HINT);
  4424. OpenedTerminal = true;
  4425. }
  4426. else if (ActiveTerminal->Status > ssClosed)
  4427. {
  4428. ProtocolName = LoadStr(STATUS_CONNECTING);
  4429. }
  4430. else
  4431. {
  4432. ProtocolName = LoadStr(STATUS_NOT_CONNECTED2);
  4433. }
  4434. }
  4435. TTBXStatusBar * SessionStatusBar = (TTBXStatusBar *)GetComponent(fcStatusBar);
  4436. int Offset = SessionStatusBar->Panels->Count - SessionPanelCount;
  4437. TTBXStatusPanel * ProtocolStatusPanel = SessionStatusBar->Panels->Items[Offset + 0];
  4438. ProtocolStatusPanel->ViewPriority = (ActiveTerminal != NULL) ? 100 : 0;
  4439. if (ProtocolStatusPanel->Caption != ProtocolName)
  4440. {
  4441. ProtocolStatusPanel->Caption = ProtocolName;
  4442. std::unique_ptr<TCanvas> Canvas(CreateControlCanvas(SessionStatusBar));
  4443. ProtocolStatusPanel->Size = Canvas->TextWidth(ProtocolName) + ScaleByTextHeight(SessionStatusBar, 32);
  4444. }
  4445. ProtocolStatusPanel->Hint = ProtocolNameHint;
  4446. TTBXStatusPanel * SecurityStatusPanel = SessionStatusBar->Panels->Items[Offset + 1];
  4447. SecurityStatusPanel->ViewPriority = OpenedTerminal ? 98 : 0;
  4448. bool SecurityEnabled = !SecurityProtocolName.IsEmpty();
  4449. SecurityStatusPanel->Enabled = SecurityEnabled;
  4450. UnicodeString SecurityProtocolHint;
  4451. if (SecurityEnabled)
  4452. {
  4453. SecurityProtocolHint = FMTLOAD(STATUS_SECURE, (SecurityProtocolName));
  4454. }
  4455. else
  4456. {
  4457. SecurityProtocolHint = LoadStr(STATUS_INSECURE);
  4458. }
  4459. SecurityStatusPanel->Hint = SecurityProtocolHint;
  4460. TTBXStatusPanel * DurationStatusPanel = SessionStatusBar->Panels->Items[Offset + 2];
  4461. DurationStatusPanel->ViewPriority = OpenedTerminal ? 99 : 0;
  4462. DurationStatusPanel->Hint = DurationHint;
  4463. UpdateStatusBar();
  4464. }
  4465. //---------------------------------------------------------------------------
  4466. void __fastcall TCustomScpExplorerForm::UpdateStatusBar()
  4467. {
  4468. TTBXStatusBar * SessionStatusBar = (TTBXStatusBar *)GetComponent(fcStatusBar);
  4469. DebugAssert(SessionStatusBar != NULL);
  4470. TTBXStatusPanel * NoteStatusPanel = SessionStatusBar->Panels->Items[0];
  4471. if (!FNote.IsEmpty())
  4472. {
  4473. NoteStatusPanel->Caption = FNote;
  4474. }
  4475. else
  4476. {
  4477. UpdateStatusPanelText(NoteStatusPanel);
  4478. }
  4479. NoteStatusPanel->Hint = FNoteHints;
  4480. UnicodeString LoginTime;
  4481. // See the comment in TScpCommanderForm::UpdateControls()
  4482. TTerminal * ActiveTerminal = TTerminalManager::Instance()->ActiveTerminal;
  4483. // Active is not enough here
  4484. if ((ActiveTerminal != NULL) && (ActiveTerminal->Status == ssOpened))
  4485. {
  4486. const TSessionInfo & SessionInfo = ActiveTerminal->GetSessionInfo();
  4487. LoginTime = FormatDateTimeSpan(Configuration->TimeFormat, Now() - SessionInfo.LoginTime);
  4488. }
  4489. TTBXStatusPanel * DurationStatusPanel = SessionStatusBar->Panels->Items[SessionStatusBar->Panels->Count - 1];
  4490. DurationStatusPanel->Caption = LoginTime;
  4491. }
  4492. //---------------------------------------------------------------------------
  4493. void __fastcall TCustomScpExplorerForm::UpdateStatusPanelText(TTBXStatusPanel * Panel)
  4494. {
  4495. Panel->Caption = L"";
  4496. }
  4497. //---------------------------------------------------------------------------
  4498. void __fastcall TCustomScpExplorerForm::Idle()
  4499. {
  4500. if (FShowing ||
  4501. // Particularly for
  4502. // - Standalone editing
  4503. // - Detecting closed connection and automatically reconnect it while waiting for changes
  4504. // while "keeping remote directory up to date".
  4505. StandaloneOperation)
  4506. {
  4507. FEditorManager->Check();
  4508. // make sure that Idle is called before update queue, as it may invoke QueueEvent
  4509. // that needs to know if queue view is visible (and it may be closed after queue update)
  4510. TTerminalManager::Instance()->Idle(FDoNotIdleCurrentTerminal > 0);
  4511. }
  4512. if (FShowing)
  4513. {
  4514. if (!NonVisualDataModule->Busy &&
  4515. // Menu is opened or drag&drop is going on
  4516. (Mouse->Capture == NULL))
  4517. {
  4518. if (FRefreshRemoteDirectory)
  4519. {
  4520. if ((Terminal != NULL) && (Terminal->Status == ssOpened))
  4521. {
  4522. Terminal->RefreshDirectory();
  4523. }
  4524. FRefreshRemoteDirectory = false;
  4525. }
  4526. if (FRefreshLocalDirectory)
  4527. {
  4528. ReloadLocalDirectory();
  4529. FRefreshLocalDirectory = false;
  4530. }
  4531. if (WinConfiguration->RefreshRemotePanel)
  4532. {
  4533. if ((Terminal != NULL) && (Terminal->Status == ssOpened) &&
  4534. (Now() - Terminal->DirectoryLoaded > WinConfiguration->RefreshRemotePanelInterval))
  4535. {
  4536. RemoteDirView->ReloadDirectory();
  4537. }
  4538. }
  4539. }
  4540. }
  4541. if (FShowing || StandaloneOperation)
  4542. {
  4543. if (FQueueStatusInvalidated)
  4544. {
  4545. UpdateQueueStatus(false);
  4546. }
  4547. RefreshQueueItems();
  4548. }
  4549. if (FShowing)
  4550. {
  4551. UpdateStatusBar();
  4552. }
  4553. if (FClipboardFakeMonitorsPendingReset && !FDownloadingFromClipboard)
  4554. {
  4555. FClipboardFakeMonitorsPendingReset = false;
  4556. FClipboardFakeMonitors.reset(NULL);
  4557. }
  4558. FIgnoreNextDialogChar = 0;
  4559. }
  4560. //---------------------------------------------------------------------------
  4561. void __fastcall TCustomScpExplorerForm::UserActionTimer(TObject * /*Sender*/)
  4562. {
  4563. try
  4564. {
  4565. FUserActionTimer->Enabled = false;
  4566. if (IsQueueAutoPopup() && (FPendingQueueActionItem != NULL))
  4567. {
  4568. if (TQueueItem::IsUserActionStatus(FPendingQueueActionItem->Status))
  4569. {
  4570. FPendingQueueActionItem->ProcessUserAction();
  4571. }
  4572. }
  4573. }
  4574. __finally
  4575. {
  4576. FPendingQueueActionItem = NULL;
  4577. }
  4578. }
  4579. //---------------------------------------------------------------------------
  4580. void __fastcall TCustomScpExplorerForm::ApplicationMinimize(TObject * /*Sender*/)
  4581. {
  4582. if (WinConfiguration->MinimizeToTray)
  4583. {
  4584. UpdateTrayIcon();
  4585. FTrayIcon->Visible = true;
  4586. if (Visible)
  4587. {
  4588. ShowWindow(Handle, SW_HIDE);
  4589. }
  4590. }
  4591. }
  4592. //---------------------------------------------------------------------------
  4593. void __fastcall TCustomScpExplorerForm::ApplicationRestore(TObject * /*Sender*/)
  4594. {
  4595. // WORKAROUND
  4596. // When restoring maximized window from minimization,
  4597. // rarely some controls do not align properly.
  4598. // Two instances seen (both for Commander):
  4599. // - When restoring, window is temporarily narrower (not maximized),
  4600. // causing toolbars on TopDock to wrap and dock to expand horizontally.
  4601. // Once maximized already, top dock shinks back, but the session PageControl,
  4602. // do not align up, leaving space between TopDock and PageControl.
  4603. // - Similar issue seen with LocalDirView not aligning down to status bar.
  4604. for (int Index = 0; Index < ControlCount; Index++)
  4605. {
  4606. RealignControl(Controls[Index]);
  4607. }
  4608. if (FTrayIcon->Visible)
  4609. {
  4610. FTrayIcon->Visible = false;
  4611. if (Visible)
  4612. {
  4613. ShowWindow(Handle, SW_SHOW);
  4614. }
  4615. }
  4616. if (FNeedSession && DebugAlwaysTrue(Terminal == NULL))
  4617. {
  4618. FNeedSession = false;
  4619. NonVisualDataModule->StartBusy();
  4620. try
  4621. {
  4622. NeedSession(true);
  4623. }
  4624. __finally
  4625. {
  4626. NonVisualDataModule->EndBusy();
  4627. }
  4628. }
  4629. }
  4630. //---------------------------------------------------------------------------
  4631. void __fastcall TCustomScpExplorerForm::UpdateTrayIcon()
  4632. {
  4633. FTrayIcon->Hint = GetMainForm()->Caption;
  4634. }
  4635. //---------------------------------------------------------------------------
  4636. void __fastcall TCustomScpExplorerForm::ApplicationTitleChanged()
  4637. {
  4638. UpdateTrayIcon();
  4639. }
  4640. //---------------------------------------------------------------------------
  4641. void __fastcall TCustomScpExplorerForm::RestoreApp()
  4642. {
  4643. // workaround
  4644. // TApplication.WndProc sets TApplication.FAppIconic to false,
  4645. // on WM_ACTIVATEAPP, what renders TApplication.Restore no-op function.
  4646. // But WM_ACTIVATEAPP message can be received even when
  4647. // the main window is minimized to the tray and internal editor window is focused
  4648. // (after another application was previously active)
  4649. if (::IsIconic(Handle))
  4650. {
  4651. if (!IsAppIconic())
  4652. {
  4653. SetAppIconic(true);
  4654. }
  4655. }
  4656. ::ApplicationRestore();
  4657. Application->BringToFront();
  4658. }
  4659. //---------------------------------------------------------------------------
  4660. void __fastcall TCustomScpExplorerForm::TrayIconClick(TObject * /*Sender*/)
  4661. {
  4662. RestoreApp();
  4663. }
  4664. //---------------------------------------------------------------------------
  4665. void __fastcall TCustomScpExplorerForm::NewSession(const UnicodeString & SessionUrl)
  4666. {
  4667. if (OpenInNewWindow())
  4668. {
  4669. ExecuteNewInstance(SessionUrl);
  4670. }
  4671. else
  4672. {
  4673. TTerminalManager::Instance()->NewSession(SessionUrl);
  4674. }
  4675. }
  4676. //---------------------------------------------------------------------------
  4677. void TCustomScpExplorerForm::NewTab(TOperationSide)
  4678. {
  4679. NewSession();
  4680. }
  4681. //---------------------------------------------------------------------------
  4682. UnicodeString __fastcall TCustomScpExplorerForm::SaveHiddenDuplicateSession(TSessionData * SessionData)
  4683. {
  4684. UnicodeString SessionName = StoredSessions->HiddenPrefix + ManagedSession->SessionData->SessionName;
  4685. StoredSessions->NewSession(SessionName, SessionData);
  4686. // modified only, explicit
  4687. StoredSessions->Save(false, true);
  4688. // encode session name because of slashes in hierarchical sessions
  4689. return EncodeUrlString(SessionName);
  4690. }
  4691. //---------------------------------------------------------------------------
  4692. UnicodeString __fastcall TCustomScpExplorerForm::CreateHiddenDuplicateSession()
  4693. {
  4694. // current working directories become defaults here, what is not right
  4695. std::unique_ptr<TSessionData> SessionData(CloneCurrentSessionData());
  4696. return SaveHiddenDuplicateSession(SessionData.get());
  4697. }
  4698. //---------------------------------------------------------------------------
  4699. void __fastcall TCustomScpExplorerForm::DuplicateSession()
  4700. {
  4701. if (OpenInNewWindow())
  4702. {
  4703. ExecuteNewInstance(CreateHiddenDuplicateSession());
  4704. }
  4705. else
  4706. {
  4707. // current working directories become defaults here, what is not right
  4708. std::unique_ptr<TSessionData> SessionData(CloneCurrentSessionData());
  4709. TTerminalManager * Manager = TTerminalManager::Instance();
  4710. TManagedTerminal * ASession = Manager->NewManagedTerminal(SessionData.get());
  4711. DebugAssert(ASession->LocalBrowser == ManagedSession->LocalBrowser);
  4712. // We definitelly want these
  4713. ASession->Disconnected = ManagedSession->Disconnected;
  4714. // Not sure about these two
  4715. ASession->Permanent = ManagedSession->Permanent;
  4716. ASession->DisconnectedTemporarily = ManagedSession->DisconnectedTemporarily;
  4717. Manager->ActiveSession = ASession;
  4718. }
  4719. }
  4720. //---------------------------------------------------------------------------
  4721. void __fastcall TCustomScpExplorerForm::RenameSession()
  4722. {
  4723. UnicodeString Name = ManagedSession->SessionData->SessionName;
  4724. if (InputDialog(LoadStr(RENAME_SESSION_TITLE), LoadStr(RENAME_SESSION_PROMPT), Name, HELP_SESSION_RENAME) &&
  4725. // When submitting the default name, do not fix it
  4726. (Name != ManagedSession->SessionData->SessionName))
  4727. {
  4728. // Checks for a slash only, so it's not that big deal that we do the check after submitting the dialog only.
  4729. TSessionData::ValidateName(Name);
  4730. // This is inconsistent with how color (for example) is handled.
  4731. ManagedSession->SessionData->Name = Name;
  4732. UpdateControls();
  4733. // Add/Remove distinguishing paths from sessions of the same name.
  4734. SessionListChanged();
  4735. }
  4736. }
  4737. //---------------------------------------------------------------------------
  4738. bool __fastcall TCustomScpExplorerForm::CanCloseQueue(TTerminalQueue * Queue)
  4739. {
  4740. DebugAssert(Queue != NULL);
  4741. bool Result = Queue->IsEmpty;
  4742. if (!Result)
  4743. {
  4744. SetFocus();
  4745. Result = (MessageDialog(LoadStr(PENDING_QUEUE_ITEMS2), qtWarning, qaOK | qaCancel, HELP_NONE) == qaOK);
  4746. }
  4747. return Result;
  4748. }
  4749. //---------------------------------------------------------------------------
  4750. bool __fastcall TCustomScpExplorerForm::CanCloseQueue()
  4751. {
  4752. return CanCloseQueue(FQueue);
  4753. }
  4754. //---------------------------------------------------------------------------
  4755. void __fastcall TCustomScpExplorerForm::CloseSession()
  4756. {
  4757. if (CanCloseQueue())
  4758. {
  4759. TTerminalManager::Instance()->FreeActiveTerminal();
  4760. }
  4761. }
  4762. //---------------------------------------------------------------------------
  4763. void __fastcall TCustomScpExplorerForm::DisconnectSession()
  4764. {
  4765. if (CanCloseQueue())
  4766. {
  4767. TTerminalManager::Instance()->DisconnectActiveTerminal();
  4768. }
  4769. }
  4770. //---------------------------------------------------------------------------
  4771. void __fastcall TCustomScpExplorerForm::TerminalDisconnected()
  4772. {
  4773. DetachTerminal(Terminal);
  4774. RemoteDirView->Terminal = Terminal;
  4775. UpdateRemotePathComboBox(false);
  4776. InitStatusBar();
  4777. UpdateControls();
  4778. }
  4779. //---------------------------------------------------------------------------
  4780. void __fastcall TCustomScpExplorerForm::TerminalConnecting()
  4781. {
  4782. InitStatusBar();
  4783. }
  4784. //---------------------------------------------------------------------------
  4785. void __fastcall TCustomScpExplorerForm::ReconnectSession()
  4786. {
  4787. if (DebugAlwaysTrue(!Terminal->Active))
  4788. {
  4789. TTerminalManager::Instance()->ReconnectActiveTerminal();
  4790. }
  4791. }
  4792. //---------------------------------------------------------------------------
  4793. void __fastcall TCustomScpExplorerForm::OpenStoredSession(TSessionData * Data)
  4794. {
  4795. if (OpenInNewWindow())
  4796. {
  4797. // encode session name because of slashes in hierarchical sessions
  4798. ExecuteNewInstance(EncodeUrlString(Data->Name));
  4799. }
  4800. else
  4801. {
  4802. TTerminalManager * Manager = TTerminalManager::Instance();
  4803. TManagedTerminal * Terminal = Manager->NewManagedTerminal(Data);
  4804. Manager->ActiveSession = Terminal;
  4805. }
  4806. }
  4807. //---------------------------------------------------------------------------
  4808. void TCustomScpExplorerForm::DoOpenFolderOrWorkspace(
  4809. const UnicodeString & Name, bool ConnectFirstTerminal, bool CheckMaxSessions)
  4810. {
  4811. TTerminalManager * Manager = TTerminalManager::Instance();
  4812. std::unique_ptr<TObjectList> DataList(new TObjectList());
  4813. StoredSessions->GetFolderOrWorkspace(Name, DataList.get());
  4814. if (!CheckMaxSessions || (DataList->Count <= WinConfiguration->MaxSessions))
  4815. {
  4816. TManagedTerminal * FirstSession = Manager->NewSessions(DataList.get());
  4817. // FirstSession can be null, if some of the
  4818. if (!ConnectFirstTerminal && (FirstSession != NULL))
  4819. {
  4820. FirstSession->Disconnected = true;
  4821. FirstSession->DisconnectedTemporarily = true;
  4822. }
  4823. Manager->ActiveSession = FirstSession;
  4824. }
  4825. }
  4826. //---------------------------------------------------------------------------
  4827. void __fastcall TCustomScpExplorerForm::OpenFolderOrWorkspace(const UnicodeString & Name)
  4828. {
  4829. if (OpenInNewWindow())
  4830. {
  4831. ExecuteNewInstance(Name);
  4832. }
  4833. else
  4834. {
  4835. DoOpenFolderOrWorkspace(Name, true, false);
  4836. }
  4837. }
  4838. //---------------------------------------------------------------------------
  4839. void __fastcall TCustomScpExplorerForm::FormCloseQuery(TObject * /*Sender*/,
  4840. bool &CanClose)
  4841. {
  4842. // See the comment in CloseApp()
  4843. if (NonVisualDataModule->Busy)
  4844. {
  4845. CanClose = false;
  4846. }
  4847. else
  4848. {
  4849. TTerminalManager * Manager = TTerminalManager::Instance();
  4850. int ActiveSessions = 0;
  4851. for (int Index = 0; Index < Manager->Count; Index++)
  4852. {
  4853. if (Manager->Sessions[Index]->Active)
  4854. {
  4855. ActiveSessions++;
  4856. }
  4857. }
  4858. // Confirm, when there's at least one active remote session or
  4859. // when there are multiple (inactive or local) sessions and auto workspace saving is not enabled.
  4860. if (((ActiveSessions > 0) || ((Manager->Count > 1) && !WinConfiguration->AutoSaveWorkspace)) &&
  4861. WinConfiguration->ConfirmClosingSession)
  4862. {
  4863. unsigned int Result;
  4864. TMessageParams Params(mpNeverAskAgainCheck);
  4865. UnicodeString Message;
  4866. int Answers = qaOK | qaCancel;
  4867. // The current session is the only active session
  4868. if ((ActiveSessions == 1) && (Terminal != NULL) && Terminal->Active)
  4869. {
  4870. if (!WinConfiguration->AutoSaveWorkspace)
  4871. {
  4872. Message = LoadStr(CLOSE_SESSION_WORKSPACE);
  4873. }
  4874. else
  4875. {
  4876. Message = LoadStr(CLOSE_SESSION);
  4877. }
  4878. Message = FORMAT(Message, (Terminal->SessionData->SessionName));
  4879. }
  4880. // Multiple active sessions or one active session, but it's not the current one
  4881. else if (ActiveSessions > 0)
  4882. {
  4883. if (!WinConfiguration->AutoSaveWorkspace)
  4884. {
  4885. Message = LoadStr(CLOSE_SESSIONS_WORKSPACE3);
  4886. }
  4887. else
  4888. {
  4889. Message = LoadStr(CLOSE_SESSIONS);
  4890. }
  4891. }
  4892. // Multiple tabs, but all are inactive or local, and saving of worspace is not enabled
  4893. else
  4894. {
  4895. DebugAlwaysTrue(Manager->Count > 1);
  4896. DebugAlwaysTrue(!WinConfiguration->AutoSaveWorkspace);
  4897. Message = LoadStr(CLOSE_WORKSPACE);
  4898. }
  4899. UnicodeString Note;
  4900. if (WinConfiguration->AutoSaveWorkspace)
  4901. {
  4902. Note = FMTLOAD(AUTO_WORKSPACE, (WorkspaceName()));
  4903. }
  4904. else
  4905. {
  4906. Note = LoadStr(AUTO_WORKSPACE_ENABLE);
  4907. Answers = qaYes | qaNo | qaCancel;
  4908. }
  4909. Message = FORMAT("%s\n\n%s", (MainInstructions(Message), Note));
  4910. SetFocus();
  4911. Result = MessageDialog(Message, qtConfirmation,
  4912. Answers, HELP_CLOSE_SESSION_WORKSPACE, &Params);
  4913. if (Result == qaNeverAskAgain)
  4914. {
  4915. WinConfiguration->ConfirmClosingSession = false;
  4916. }
  4917. if (Result == qaNo)
  4918. {
  4919. CanClose = SaveWorkspace(true);
  4920. // note that the workspace will be saved redundatly again from FormClose
  4921. }
  4922. else
  4923. {
  4924. CanClose =
  4925. (Result == qaOK) ||
  4926. (Result == qaYes) || // CLOSE_SESSIONS_WORKSPACE variant
  4927. (Result == qaNeverAskAgain);
  4928. }
  4929. }
  4930. if (CanClose && HasActiveTerminal())
  4931. {
  4932. CanClose = CanCloseQueue();
  4933. }
  4934. }
  4935. if (CanClose)
  4936. {
  4937. CanClose =
  4938. FEditorManager->CloseInternalEditors(CloseInternalEditor) &&
  4939. FEditorManager->CloseExternalFilesWithoutProcess();
  4940. if (CanClose)
  4941. {
  4942. while (CanClose && (FLocalEditors->Count > 0))
  4943. {
  4944. int PrevCount = FLocalEditors->Count;
  4945. static_cast<TForm *>(FLocalEditors->Items[0])->Close();
  4946. CanClose = (FLocalEditors->Count < PrevCount);
  4947. }
  4948. if (CanClose)
  4949. {
  4950. CanClose = FEditorManager->Empty(true);
  4951. if (!CanClose)
  4952. {
  4953. SetFocus();
  4954. CanClose =
  4955. (MessageDialog(
  4956. LoadStr(PENDING_EDITORS), qtWarning, qaIgnore | qaCancel, HELP_NONE) == qaIgnore);
  4957. }
  4958. }
  4959. }
  4960. }
  4961. }
  4962. //---------------------------------------------------------------------------
  4963. void __fastcall TCustomScpExplorerForm::CloseInternalEditor(TObject * Sender)
  4964. {
  4965. TForm * Form = dynamic_cast<TForm *>(Sender);
  4966. DebugAssert(Form != NULL);
  4967. Form->Close();
  4968. }
  4969. //---------------------------------------------------------------------------
  4970. void __fastcall TCustomScpExplorerForm::ForceCloseInternalEditor(TObject * Sender)
  4971. {
  4972. TForm * Form = dynamic_cast<TForm *>(Sender);
  4973. delete Form;
  4974. }
  4975. //---------------------------------------------------------------------------
  4976. void __fastcall TCustomScpExplorerForm::ForceCloseLocalEditors()
  4977. {
  4978. while (FLocalEditors->Count > 0)
  4979. {
  4980. delete static_cast<TForm *>(FLocalEditors->Items[0]);
  4981. }
  4982. }
  4983. //---------------------------------------------------------------------------
  4984. void __fastcall TCustomScpExplorerForm::RemoteDirViewDisplayProperties(
  4985. TObject *Sender)
  4986. {
  4987. TStrings *FileList = ((TUnixDirView*)Sender)->CreateFileList(True, False, NULL);
  4988. try
  4989. {
  4990. SetProperties(osRemote, FileList);
  4991. }
  4992. __finally
  4993. {
  4994. delete FileList;
  4995. }
  4996. }
  4997. //---------------------------------------------------------------------------
  4998. void __fastcall TCustomScpExplorerForm::ComponentShowing(Byte Component, bool value)
  4999. {
  5000. if (value)
  5001. {
  5002. if (Component == fcCustomCommandsBand)
  5003. {
  5004. UpdateCustomCommandsToolbar();
  5005. }
  5006. if (Component == fcQueueFileList)
  5007. {
  5008. DebugAssert(!QueueFileListSplitter->Visible);
  5009. AdjustQueueLayout();
  5010. UpdateQueueFileList();
  5011. }
  5012. }
  5013. }
  5014. //---------------------------------------------------------------------------
  5015. void __fastcall TCustomScpExplorerForm::AdjustQueueLayout()
  5016. {
  5017. int MinHeightLimit =
  5018. GetStaticQueuePanelComponentsHeight() +
  5019. QueueFileListSplitter->Height +
  5020. QueueFileList->Height +
  5021. GetMinQueueViewHeight();
  5022. if (QueuePanel->ClientHeight < MinHeightLimit)
  5023. {
  5024. QueuePanel->ClientHeight = MinHeightLimit;
  5025. }
  5026. }
  5027. //---------------------------------------------------------------------------
  5028. void __fastcall TCustomScpExplorerForm::SetComponentVisible(Byte Component, Boolean value)
  5029. {
  5030. TControl * Control = GetComponent(Component);
  5031. DebugAssert(Control);
  5032. bool Changed = (Control->Visible != value);
  5033. if (Changed)
  5034. {
  5035. ComponentShowing(Component, value);
  5036. TWinControl * WinControl = dynamic_cast<TWinControl*>(Control);
  5037. bool WasFocused = (WinControl != NULL) && (ActiveControl != NULL) &&
  5038. ((ActiveControl == WinControl) || (ActiveControl->Parent == WinControl));
  5039. if (value)
  5040. {
  5041. int RemainingHeight = Control->Parent->ClientHeight;
  5042. int RemainingWidth = Control->Parent->ClientWidth;
  5043. for (int i = 0; i < Control->Parent->ControlCount; i++)
  5044. {
  5045. TControl * ChildControl = Control->Parent->Controls[i];
  5046. if (ChildControl->Visible)
  5047. {
  5048. switch (ChildControl->Align)
  5049. {
  5050. case alTop:
  5051. case alBottom:
  5052. RemainingHeight -= ChildControl->Height;
  5053. break;
  5054. case alLeft:
  5055. case alRight:
  5056. RemainingWidth -= ChildControl->Width;
  5057. break;
  5058. }
  5059. }
  5060. }
  5061. int Reserve = ScaleByTextHeight(this, 32);
  5062. // queue in explorer, trees in commander
  5063. if (Control->Height > RemainingHeight - Reserve)
  5064. {
  5065. Control->Height = RemainingHeight / 2;
  5066. }
  5067. if (Control->Width > RemainingWidth - Reserve)
  5068. {
  5069. Control->Width = RemainingWidth / 2;
  5070. }
  5071. }
  5072. Control->Visible = value;
  5073. if (WasFocused && Visible)
  5074. {
  5075. DirView(osCurrent)->SetFocus();
  5076. }
  5077. FixControlsPlacement();
  5078. }
  5079. }
  5080. //---------------------------------------------------------------------------
  5081. bool __fastcall TCustomScpExplorerForm::GetComponentVisible(Byte Component)
  5082. {
  5083. TControl * Control = GetComponent(Component);
  5084. if (Control == NULL)
  5085. {
  5086. return false;
  5087. }
  5088. else
  5089. {
  5090. return Control->Visible;
  5091. }
  5092. }
  5093. //---------------------------------------------------------------------------
  5094. bool __fastcall TCustomScpExplorerForm::IsComponentPossible(Byte Component)
  5095. {
  5096. bool Result;
  5097. if ((Component == fcExplorerUpdatesBand) || (Component == fcCommanderUpdatesBand))
  5098. {
  5099. Result = !IsUWP();
  5100. }
  5101. else
  5102. {
  5103. Result = true;
  5104. }
  5105. return Result;
  5106. }
  5107. //---------------------------------------------------------------------------
  5108. void __fastcall TCustomScpExplorerForm::MakeFocusedItemVisible(TCustomDirView * DirView)
  5109. {
  5110. if (DirView->ItemFocused != NULL)
  5111. {
  5112. DirView->ItemFocused->MakeVisible(false);
  5113. }
  5114. }
  5115. //---------------------------------------------------------------------------
  5116. void __fastcall TCustomScpExplorerForm::FixControlsPlacement()
  5117. {
  5118. MakeFocusedItemVisible(RemoteDirView);
  5119. QueueSplitter->Visible = QueuePanel->Visible;
  5120. QueueFileListSplitter->Visible = QueueFileList->Visible;
  5121. RemotePanelSplitter->Visible = RemoteDrivePanel->Visible;
  5122. TControl * QueueControlsOrder[] =
  5123. { QueueDock, QueueView3, QueueFileListSplitter, QueueFileList };
  5124. SetVerticalControlsOrder(QueueControlsOrder, LENOF(QueueControlsOrder));
  5125. }
  5126. //---------------------------------------------------------------------------
  5127. TControl * __fastcall TCustomScpExplorerForm::GetComponent(Byte Component)
  5128. {
  5129. switch (Component) {
  5130. case fcStatusBar: return RemoteStatusBar;
  5131. case fcRemotePopup: return reinterpret_cast<TControl *>(NonVisualDataModule->RemoteFilePopup);
  5132. case fcQueueView: return QueuePanel;
  5133. case fcQueueToolbar: return QueueDock;
  5134. case fcRemoteTree: return RemoteDrivePanel;
  5135. case fcSessionsTabs: return SessionsPageControl;
  5136. case fcQueueFileList: return QueueFileList;
  5137. default: return NULL;
  5138. }
  5139. }
  5140. //---------------------------------------------------------------------------
  5141. void __fastcall TCustomScpExplorerForm::DirViewColumnRightClick(
  5142. TObject *Sender, TListColumn *Column, TPoint &Point)
  5143. {
  5144. DebugAssert(NonVisualDataModule && Column && Sender);
  5145. NonVisualDataModule->ListColumn = Column;
  5146. TPopupMenu * DirViewColumnMenu;
  5147. if (Sender == DirView(osOther))
  5148. {
  5149. DirViewColumnMenu = NonVisualDataModule->RemoteDirViewColumnPopup;
  5150. NonVisualDataModule->RemoteSortByExtColumnPopupItem->Visible =
  5151. (Column->Index == uvName);
  5152. NonVisualDataModule->RemoteFormatSizeBytesPopupItem->Visible =
  5153. (Column->Index == uvSize);
  5154. }
  5155. else
  5156. {
  5157. DirViewColumnMenu = NonVisualDataModule->LocalDirViewColumnPopup;
  5158. NonVisualDataModule->LocalSortByExtColumnPopupItem->Visible =
  5159. (Column->Index == dvName);
  5160. NonVisualDataModule->LocalFormatSizeBytesPopupItem->Visible =
  5161. (Column->Index == dvSize);
  5162. }
  5163. TCustomListView * ListView = DebugNotNull(dynamic_cast<TCustomListView *>(Sender));
  5164. TPoint ScreenPoint = ListView->ClientToScreen(Point);
  5165. ScreenPoint.x -= GetScrollPos(ListView->Handle, SB_HORZ);
  5166. DirViewColumnMenu->Popup(ScreenPoint.x, ScreenPoint.y);
  5167. }
  5168. //---------------------------------------------------------------------------
  5169. void __fastcall TCustomScpExplorerForm::DirViewExecFile(
  5170. TObject *Sender, TListItem *Item, bool &AllowExec)
  5171. {
  5172. DoDirViewExecFile(Sender, Item, AllowExec);
  5173. }
  5174. //---------------------------------------------------------------------------
  5175. void __fastcall TCustomScpExplorerForm::DoDirViewExecFile(TObject * Sender,
  5176. TListItem * Item, bool & AllowExec)
  5177. {
  5178. DebugAssert(Sender && Item && Configuration);
  5179. DebugAssert(AllowExec);
  5180. TCustomDirView * ADirView = (TCustomDirView *)Sender;
  5181. TOperationSide Side = ((ADirView == DirView(osRemote)) ? osRemote : osLocal);
  5182. bool Remote = !IsSideLocalBrowser(Side);
  5183. DebugAssert(Item == ADirView->ItemFocused);
  5184. bool IsDirectory = ADirView->ItemIsDirectory(Item);
  5185. TResolvedDoubleClickAction Action;
  5186. if (FForceExecution)
  5187. {
  5188. Action = IsDirectory ? rdcaChangeDir : rdcaOpen;
  5189. }
  5190. else
  5191. {
  5192. TTerminal * ATerminal = !IsSideLocalBrowser(Side) ? Terminal : NULL;
  5193. Action = WinConfiguration->ResolveDoubleClickAction(IsDirectory, ATerminal);
  5194. }
  5195. if (Action == rdcaCopy)
  5196. {
  5197. // To counter the lock in DirViewBusy (called from TCustomDirView.DoExecute)
  5198. UnlockWindow();
  5199. try
  5200. {
  5201. if (IsLocalBrowserMode())
  5202. {
  5203. LocalLocalCopy(foCopy, Side, true, !WinConfiguration->CopyOnDoubleClickConfirmation, false, 0);
  5204. }
  5205. else
  5206. {
  5207. ExecuteFileOperation(foCopy, Side, true, !WinConfiguration->CopyOnDoubleClickConfirmation);
  5208. }
  5209. }
  5210. __finally
  5211. {
  5212. LockWindow();
  5213. }
  5214. AllowExec = false;
  5215. }
  5216. else if (Action == rdcaEdit)
  5217. {
  5218. if (!Remote || !WinConfiguration->DisableOpenEdit)
  5219. {
  5220. ExecuteFile(Side, efDefaultEditor);
  5221. AllowExec = false;
  5222. }
  5223. }
  5224. else if (Action == rdcaOpen)
  5225. {
  5226. if (DebugAlwaysTrue(!IsDirectory) && Remote && !WinConfiguration->DisableOpenEdit)
  5227. {
  5228. ExecuteFile(osRemote, efShell);
  5229. AllowExec = false;
  5230. }
  5231. }
  5232. }
  5233. //---------------------------------------------------------------------------
  5234. bool __fastcall TCustomScpExplorerForm::GetHasDirView(TOperationSide Side)
  5235. {
  5236. return ((Side == osRemote) || (Side == osCurrent));
  5237. }
  5238. //---------------------------------------------------------------------------
  5239. void __fastcall TCustomScpExplorerForm::CompareDirectories()
  5240. {
  5241. DebugFail();
  5242. }
  5243. //---------------------------------------------------------------------------
  5244. void __fastcall TCustomScpExplorerForm::SynchronizeDirectories()
  5245. {
  5246. DebugFail();
  5247. }
  5248. //---------------------------------------------------------------------------
  5249. bool __fastcall TCustomScpExplorerForm::DoSynchronizeDirectories(
  5250. UnicodeString & LocalDirectory, UnicodeString & RemoteDirectory, int UseDefaults)
  5251. {
  5252. TSynchronizeParamType Params;
  5253. Params.LocalDirectory = LocalDirectory;
  5254. Params.RemoteDirectory = RemoteDirectory;
  5255. int UnusedParams =
  5256. (GUIConfiguration->SynchronizeParams &
  5257. (TTerminal::spPreviewChanges | TTerminal::spTimestamp | TTerminal::spNotByTime | TTerminal::spBySize |
  5258. TTerminal::spCaseSensitive));
  5259. Params.Params = GUIConfiguration->SynchronizeParams & ~UnusedParams;
  5260. Params.Options = GUIConfiguration->SynchronizeOptions;
  5261. bool SaveSettings = false;
  5262. TSynchronizeController Controller(&DoSynchronize, &DoSynchronizeInvalid,
  5263. &DoSynchronizeTooManyDirectories);
  5264. DebugAssert(FSynchronizeController == NULL);
  5265. FSynchronizeController = &Controller;
  5266. bool Result;
  5267. try
  5268. {
  5269. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  5270. int CopyParamAttrs = Terminal->UsableCopyParamAttrs(0).Upload;
  5271. int Options =
  5272. FLAGMASK(SynchronizeAllowSelectedOnly(), soAllowSelectedOnly);
  5273. DebugAssert(FOnFeedSynchronizeError == NULL);
  5274. DebugAssert(FOnSynchronizeAbort == NULL);
  5275. DebugAssert(FSynchronizeTerminal == NULL);
  5276. FSynchronizeTerminal = Terminal;
  5277. Result = DoSynchronizeDialog(Params, &CopyParam, Controller.StartStop,
  5278. SaveSettings, Options, CopyParamAttrs, GetSynchronizeOptions, SynchronizeSessionLog,
  5279. FOnFeedSynchronizeError, FOnSynchronizeAbort, SynchronizeInNewWindow, UseDefaults);
  5280. if (Result)
  5281. {
  5282. if (SaveSettings)
  5283. {
  5284. GUIConfiguration->SynchronizeParams = Params.Params | UnusedParams;
  5285. GUIConfiguration->SynchronizeOptions = Params.Options;
  5286. }
  5287. else
  5288. {
  5289. if (FLAGSET(GUIConfiguration->SynchronizeOptions, soSynchronizeAsk) &&
  5290. FLAGCLEAR(Params.Options, soSynchronizeAsk) &&
  5291. FLAGSET(Params.Options, soSynchronize))
  5292. {
  5293. GUIConfiguration->SynchronizeOptions =
  5294. (GUIConfiguration->SynchronizeOptions & ~soSynchronizeAsk) |
  5295. soSynchronize;
  5296. }
  5297. }
  5298. LocalDirectory = Params.LocalDirectory;
  5299. RemoteDirectory = Params.RemoteDirectory;
  5300. }
  5301. }
  5302. __finally
  5303. {
  5304. FSynchronizeController = NULL;
  5305. DebugAssert(FOnFeedSynchronizeError == NULL);
  5306. FOnFeedSynchronizeError = NULL;
  5307. DebugAssert(FOnSynchronizeAbort == NULL);
  5308. FOnSynchronizeAbort = NULL;
  5309. FSynchronizeTerminal = NULL;
  5310. }
  5311. return Result;
  5312. }
  5313. //---------------------------------------------------------------------------
  5314. void __fastcall TCustomScpExplorerForm::DoSynchronize(
  5315. TSynchronizeController * /*Sender*/, const UnicodeString LocalDirectory,
  5316. const UnicodeString RemoteDirectory, const TCopyParamType & CopyParam,
  5317. const TSynchronizeParamType & Params, TSynchronizeChecklist ** Checklist,
  5318. TSynchronizeOptions * Options, bool Full)
  5319. {
  5320. // Keep-up-to-date mode
  5321. if (Terminal->Status == ssOpened)
  5322. {
  5323. try
  5324. {
  5325. int PParams = Params.Params;
  5326. if (!Full)
  5327. {
  5328. PParams |= TTerminal::spNoRecurse | TTerminal::spUseCache | TTerminal::spDelayProgress;
  5329. if (FLAGSET(Params.Options, soRecurse))
  5330. {
  5331. PParams |= TTerminal::spSubDirs;
  5332. }
  5333. }
  5334. else
  5335. {
  5336. // if keepuptodate is non-recursive,
  5337. // full sync before has to be non-recursive as well
  5338. if (FLAGCLEAR(Params.Options, soRecurse))
  5339. {
  5340. PParams |= TTerminal::spNoRecurse;
  5341. }
  5342. }
  5343. Synchronize(LocalDirectory, RemoteDirectory, smRemote, CopyParam,
  5344. PParams, Checklist, Options);
  5345. }
  5346. catch (Exception & E)
  5347. {
  5348. if (!Terminal->Active)
  5349. {
  5350. ShowExtendedExceptionEx(Terminal, &E);
  5351. // Do not abort the "keep up to date" when the session was succesfully reconnected.
  5352. if (!Terminal->Active)
  5353. {
  5354. throw;
  5355. }
  5356. }
  5357. else
  5358. {
  5359. if (FLAGSET(Params.Options, soContinueOnError) &&
  5360. DebugAlwaysTrue(FOnFeedSynchronizeError != NULL))
  5361. {
  5362. // noop, already logged in MoreMessageDialog
  5363. }
  5364. else
  5365. {
  5366. // We get mostly EAbort here, so this is noop.
  5367. // Exception is an error when listing directories while looking for differences.
  5368. ShowExtendedExceptionEx(Terminal, &E);
  5369. throw;
  5370. }
  5371. }
  5372. }
  5373. }
  5374. }
  5375. //---------------------------------------------------------------------------
  5376. void __fastcall TCustomScpExplorerForm::DoSynchronizeInvalid(
  5377. TSynchronizeController * /*Sender*/, const UnicodeString Directory,
  5378. const UnicodeString ErrorStr)
  5379. {
  5380. if (!Directory.IsEmpty())
  5381. {
  5382. SimpleErrorDialog(FMTLOAD(WATCH_ERROR_DIRECTORY, (Directory)), ErrorStr);
  5383. }
  5384. else
  5385. {
  5386. SimpleErrorDialog(LoadStr(WATCH_ERROR_GENERAL), ErrorStr);
  5387. }
  5388. }
  5389. //---------------------------------------------------------------------------
  5390. void __fastcall TCustomScpExplorerForm::DoSynchronizeTooManyDirectories(
  5391. TSynchronizeController * /*Sender*/, int & MaxDirectories)
  5392. {
  5393. if (MaxDirectories < GUIConfiguration->MaxWatchDirectories)
  5394. {
  5395. MaxDirectories = GUIConfiguration->MaxWatchDirectories;
  5396. }
  5397. else
  5398. {
  5399. TMessageParams Params(mpNeverAskAgainCheck);
  5400. unsigned int Result = MessageDialog(
  5401. FMTLOAD(TOO_MANY_WATCH_DIRECTORIES, (MaxDirectories, MaxDirectories)),
  5402. qtConfirmation, qaYes | qaNo, HELP_TOO_MANY_WATCH_DIRECTORIES, &Params);
  5403. if ((Result == qaYes) || (Result == qaNeverAskAgain))
  5404. {
  5405. MaxDirectories *= 2;
  5406. if (Result == qaNeverAskAgain)
  5407. {
  5408. GUIConfiguration->MaxWatchDirectories = MaxDirectories;
  5409. }
  5410. }
  5411. else
  5412. {
  5413. Abort();
  5414. }
  5415. }
  5416. }
  5417. //---------------------------------------------------------------------------
  5418. void __fastcall TCustomScpExplorerForm::Synchronize(const UnicodeString LocalDirectory,
  5419. const UnicodeString RemoteDirectory, TSynchronizeMode Mode,
  5420. const TCopyParamType & CopyParam, int Params, TSynchronizeChecklist ** Checklist,
  5421. TSynchronizeOptions * Options)
  5422. {
  5423. // Keep-up-to-date mode
  5424. DebugAssert(!FAutoOperation);
  5425. void * BatchStorage;
  5426. BatchStart(BatchStorage);
  5427. FAutoOperation = true;
  5428. bool AnyOperation = false;
  5429. TDateTime StartTime = Now();
  5430. TSynchronizeChecklist * AChecklist = NULL;
  5431. try
  5432. {
  5433. FSynchronizeProgressForm = new TSynchronizeProgressForm(Application, true, -1);
  5434. if (FLAGCLEAR(Params, TTerminal::spDelayProgress))
  5435. {
  5436. FSynchronizeProgressForm->Start();
  5437. }
  5438. AChecklist = Terminal->SynchronizeCollect(LocalDirectory, RemoteDirectory,
  5439. static_cast<TTerminal::TSynchronizeMode>(Mode),
  5440. &CopyParam, Params | TTerminal::spNoConfirmation, TerminalSynchronizeDirectory,
  5441. Options);
  5442. SetTaskbarListProgressState(TBPF_NOPROGRESS);
  5443. SAFE_DESTROY(FSynchronizeProgressForm);
  5444. AnyOperation = (AChecklist->CheckedCount > 0);
  5445. if (AnyOperation)
  5446. {
  5447. TSynchronizeProgress SynchronizeProgress(AChecklist);
  5448. CreateProgressForm(&SynchronizeProgress);
  5449. try
  5450. {
  5451. Terminal->SynchronizeApply(
  5452. AChecklist, &CopyParam, Params | TTerminal::spNoConfirmation, TerminalSynchronizeDirectory,
  5453. SynchronizeProcessedItem, NULL, NULL, NULL);
  5454. }
  5455. __finally
  5456. {
  5457. DestroyProgressForm();
  5458. }
  5459. }
  5460. }
  5461. __finally
  5462. {
  5463. if (Checklist == NULL)
  5464. {
  5465. delete AChecklist;
  5466. }
  5467. else
  5468. {
  5469. *Checklist = AChecklist;
  5470. }
  5471. FAutoOperation = false;
  5472. SAFE_DESTROY(FSynchronizeProgressForm);
  5473. BatchEnd(BatchStorage);
  5474. ReloadLocalDirectory();
  5475. if (AnyOperation)
  5476. {
  5477. OperationComplete(StartTime);
  5478. }
  5479. }
  5480. }
  5481. //---------------------------------------------------------------------------
  5482. bool __fastcall TCustomScpExplorerForm::SynchronizeAllowSelectedOnly()
  5483. {
  5484. // can be called from command line
  5485. return Visible &&
  5486. ((DirView(osRemote)->SelCount > 0) ||
  5487. (HasDirView[osLocal] && (DirView(osLocal)->SelCount > 0)));
  5488. }
  5489. //---------------------------------------------------------------------------
  5490. void __fastcall TCustomScpExplorerForm::SynchronizeSessionLog(const UnicodeString & Message)
  5491. {
  5492. LogSynchronizeEvent(Terminal, Message);
  5493. }
  5494. //---------------------------------------------------------------------------
  5495. void __fastcall TCustomScpExplorerForm::GetSynchronizeOptions(
  5496. int Params, TSynchronizeOptions & Options)
  5497. {
  5498. DebugAssert(!IsLocalBrowserMode());
  5499. if (FLAGSET(Params, TTerminal::spSelectedOnly) && SynchronizeAllowSelectedOnly())
  5500. {
  5501. Options.Filter = new TStringList();
  5502. Options.Filter->CaseSensitive = false;
  5503. Options.Filter->Duplicates = Types::dupAccept;
  5504. if (DirView(osRemote)->SelCount > 0)
  5505. {
  5506. DirView(osRemote)->CreateFileList(false, false, Options.Filter);
  5507. }
  5508. if (HasDirView[osLocal] && (DirView(osLocal)->SelCount > 0))
  5509. {
  5510. DirView(osLocal)->CreateFileList(false, false, Options.Filter);
  5511. }
  5512. Options.Filter->Sort();
  5513. }
  5514. }
  5515. //---------------------------------------------------------------------------
  5516. UnicodeString __fastcall TCustomScpExplorerForm::SerializeCopyParamForCommandLine(const TCopyParamType * CopyParams)
  5517. {
  5518. TCopyParamType Defaults;
  5519. std::unique_ptr<THierarchicalStorage> ConfigStorage(Configuration->CreateConfigStorage());
  5520. ConfigStorage->AccessMode = smRead;
  5521. if (ConfigStorage->OpenSubKey(Configuration->ConfigurationSubKey, false))
  5522. {
  5523. GUIConfiguration->LoadCopyParam(ConfigStorage.get(), &Defaults);
  5524. }
  5525. ConfigStorage.reset(NULL);
  5526. std::unique_ptr<TStringList> Options(new TStringList());
  5527. std::unique_ptr<TOptionsStorage> OptionsStorage(new TOptionsStorage(Options.get(), true));
  5528. CopyParams->Save(OptionsStorage.get(), &Defaults);
  5529. UnicodeString Result;
  5530. if (Options->Count > 0)
  5531. {
  5532. Result =
  5533. FORMAT(L" %s%s", (TProgramParams::FormatSwitch(RAWTRANSFERSETTINGS_SWITCH), StringsToParams(Options.get())));
  5534. }
  5535. return Result;
  5536. }
  5537. //---------------------------------------------------------------------------
  5538. void __fastcall TCustomScpExplorerForm::SynchronizeInNewWindow(
  5539. const TSynchronizeParamType & Params, const TCopyParamType * CopyParams)
  5540. {
  5541. UnicodeString SessionName = CreateHiddenDuplicateSession();
  5542. UnicodeString AdditionalParams =
  5543. FORMAT(L"%s%s %s // \"%s\" \"%s\" %d %d", (
  5544. TProgramParams::FormatSwitch(DEFAULTS_SWITCH),
  5545. SerializeCopyParamForCommandLine(CopyParams),
  5546. TProgramParams::FormatSwitch(KEEP_UP_TO_DATE_SWITCH),
  5547. Params.LocalDirectory, Params.RemoteDirectory, Params.Params, Params.Options));
  5548. ExecuteNewInstance(SessionName, AdditionalParams);
  5549. }
  5550. //---------------------------------------------------------------------------
  5551. struct TSynchronizeParams
  5552. {
  5553. UnicodeString LocalDirectory;
  5554. UnicodeString RemoteDirectory;
  5555. TSynchronizeMode Mode;
  5556. TSynchronizeChecklist * Checklist;
  5557. int Params;
  5558. TCopyParamType * CopyParam;
  5559. TDateTime CollectElapsed;
  5560. TDateTime * StartTime;
  5561. TProcessedSynchronizationChecklistItem OnProcessedItem;
  5562. };
  5563. //---------------------------------------------------------------------------
  5564. void __fastcall TCustomScpExplorerForm::FullSynchronize(
  5565. TSynchronizeParams & Params, TProcessedSynchronizationChecklistItem OnProcessedItem,
  5566. TUpdatedSynchronizationChecklistItems OnUpdatedSynchronizationChecklistItems)
  5567. {
  5568. DebugAssert(!FAutoOperation);
  5569. void * BatchStorage;
  5570. BatchStart(BatchStorage);
  5571. FAutoOperation = true;
  5572. TFileOperationStatistics Statistics;
  5573. TDateTime Start = Now();
  5574. try
  5575. {
  5576. Params.OnProcessedItem = OnProcessedItem;
  5577. TSynchronizeProgress SynchronizeProgress(Params.Checklist);
  5578. CreateProgressForm(&SynchronizeProgress);
  5579. Terminal->SynchronizeApply(
  5580. Params.Checklist, Params.CopyParam, Params.Params | TTerminal::spNoConfirmation,
  5581. TerminalSynchronizeDirectory, SynchronizeProcessedItem, OnUpdatedSynchronizationChecklistItems, &Params,
  5582. &Statistics);
  5583. }
  5584. __finally
  5585. {
  5586. Params.OnProcessedItem = NULL;
  5587. FAutoOperation = false;
  5588. DestroyProgressForm();
  5589. BatchEnd(BatchStorage);
  5590. ReloadLocalDirectory();
  5591. }
  5592. if (WinConfiguration->SynchronizeSummary)
  5593. {
  5594. UnicodeString Message = MainInstructions(LoadStr(SYNCHRONIZE_COMPLETE)) + L"\n";
  5595. // The statistics should be 0 anyway in this case
  5596. if (FLAGCLEAR(Params.Params, TTerminal::spTimestamp))
  5597. {
  5598. Message += L"\n";
  5599. if (Statistics.FilesUploaded > 0)
  5600. {
  5601. Message += FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 1), (FormatNumber(Statistics.FilesUploaded), FormatBytes(Statistics.TotalUploaded))) + L"\n";
  5602. }
  5603. if (Statistics.FilesDownloaded > 0)
  5604. {
  5605. Message += FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 2), (FormatNumber(Statistics.FilesDownloaded), FormatBytes(Statistics.TotalDownloaded))) + L"\n";
  5606. }
  5607. if (Statistics.FilesDeletedLocal > 0)
  5608. {
  5609. Message += FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 3), (FormatNumber(Statistics.FilesDeletedLocal))) + L"\n";
  5610. }
  5611. if (Statistics.FilesDeletedRemote > 0)
  5612. {
  5613. Message += FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 4), (FormatNumber(Statistics.FilesDeletedRemote))) + L"\n";
  5614. }
  5615. }
  5616. TDateTime Elapsed = (Now() - Start);
  5617. Message +=
  5618. L"\n" +
  5619. FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 5), (FormatDateTimeSpan(Configuration->TimeFormat, Params.CollectElapsed))) + L"\n" +
  5620. FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 6), (FormatDateTimeSpan(Configuration->TimeFormat, Elapsed)));
  5621. TMessageParams Params(mpNeverAskAgainCheck);
  5622. unsigned int Result = MessageDialog(Message, qtInformation, qaOK, HELP_NONE, &Params);
  5623. if (Result == qaNeverAskAgain)
  5624. {
  5625. WinConfiguration->SynchronizeSummary = false;
  5626. }
  5627. }
  5628. }
  5629. //---------------------------------------------------------------------------
  5630. void __fastcall TCustomScpExplorerForm::SynchronizeProcessedItem(void * Token, const TSynchronizeChecklist::TItem * ChecklistItem)
  5631. {
  5632. if (DebugAlwaysTrue(FProgressForm != NULL) && DebugAlwaysTrue(FProgressForm->SynchronizeProgress != NULL))
  5633. {
  5634. FProgressForm->SynchronizeProgress->ItemProcessed(ChecklistItem);
  5635. }
  5636. // Not set in keep-up-to-date mode - Synchronize() method
  5637. if (Token != NULL)
  5638. {
  5639. TSynchronizeParams & Params = *static_cast<TSynchronizeParams *>(Token);
  5640. if (Params.OnProcessedItem != NULL)
  5641. {
  5642. Params.OnProcessedItem(NULL, ChecklistItem);
  5643. }
  5644. }
  5645. }
  5646. //---------------------------------------------------------------------------
  5647. void __fastcall TCustomScpExplorerForm::DoFullSynchronize(
  5648. void * Token, TProcessedSynchronizationChecklistItem OnProcessedItem,
  5649. TUpdatedSynchronizationChecklistItems OnUpdatedSynchronizationChecklistItems)
  5650. {
  5651. TSynchronizeParams & Params = *static_cast<TSynchronizeParams *>(Token);
  5652. *Params.StartTime = Now();
  5653. FullSynchronize(Params, OnProcessedItem, OnUpdatedSynchronizationChecklistItems);
  5654. }
  5655. //---------------------------------------------------------------------------
  5656. void __fastcall TCustomScpExplorerForm::DoSynchronizeChecklistCalculateSize(
  5657. TSynchronizeChecklist * Checklist, const TSynchronizeChecklist::TItemList & Items, void * Token)
  5658. {
  5659. // terminal can be already closed (e.g. dropped connection)
  5660. if (Terminal != NULL)
  5661. {
  5662. TSynchronizeParams & Params = *static_cast<TSynchronizeParams *>(Token);
  5663. Terminal->SynchronizeChecklistCalculateSize(Checklist, Items, Params.CopyParam);
  5664. }
  5665. }
  5666. //---------------------------------------------------------------------------
  5667. void __fastcall TCustomScpExplorerForm::DoSynchronizeMove(
  5668. TOperationSide Side, const UnicodeString & FileName, const UnicodeString & NewFileName, TRemoteFile * RemoteFile)
  5669. {
  5670. DebugAssert(!IsLocalBrowserMode());
  5671. TAutoBatch AutoBatch(this);
  5672. TAutoFlag AutoOperationFlag(FAutoOperation);
  5673. if (Side == osRemote)
  5674. {
  5675. std::unique_ptr<TStrings> FileList(new TStringList());
  5676. FileList->AddObject(FileName, RemoteFile);
  5677. UnicodeString Target = UnixExtractFileDir(NewFileName);
  5678. UnicodeString FileMask = DelimitFileNameMask(UnixExtractFileName(NewFileName));
  5679. RemoteDirView->SaveSelection();
  5680. RemoteDirView->SaveSelectedNames();
  5681. try
  5682. {
  5683. Terminal->MoveFiles(FileList.get(), Target, FileMask);
  5684. }
  5685. catch(...)
  5686. {
  5687. RemoteDirView->DiscardSavedSelection();
  5688. throw;
  5689. }
  5690. RemoteDirView->RestoreSelection();
  5691. }
  5692. else if (DebugAlwaysTrue(Side == osLocal))
  5693. {
  5694. if (!MoveFile(FileName.c_str(), NewFileName.c_str()))
  5695. {
  5696. throw EOSExtException(FMTLOAD(RENAME_FILE_ERROR, (FileName, NewFileName)));
  5697. }
  5698. UnicodeString Directory = ExtractFileDir(FileName);
  5699. ReloadLocalDirectory(Directory);
  5700. UnicodeString NewDirectory = ExtractFileDir(NewFileName);
  5701. if (!SamePaths(Directory, NewDirectory))
  5702. {
  5703. ReloadLocalDirectory(NewDirectory);
  5704. }
  5705. }
  5706. }
  5707. //---------------------------------------------------------------------------
  5708. void __fastcall TCustomScpExplorerForm::DoSynchronizeBrowse(TOperationSide Side, TSynchronizeChecklist::TAction Action, const TSynchronizeChecklist::TItem * Item)
  5709. {
  5710. UnicodeString LocalPath = ExcludeTrailingBackslash(Item->Local.Directory);
  5711. if (Side == osLocal)
  5712. {
  5713. if (Action == TSynchronizeChecklist::saDownloadNew)
  5714. {
  5715. OpenFolderInExplorer(LocalPath);
  5716. }
  5717. else
  5718. {
  5719. OpenFileInExplorer(TPath::Combine(LocalPath, Item->GetFileName()));
  5720. }
  5721. }
  5722. else if (DebugAlwaysTrue(Side == osRemote))
  5723. {
  5724. // Similar to CreateHiddenDuplicateSession, except that it modifies the initial directories
  5725. std::unique_ptr<TSessionData> SessionData(CloneCurrentSessionData());
  5726. SessionData->RemoteDirectory = UnixExcludeTrailingBackslash(Item->Remote.Directory);
  5727. if (!LocalPath.IsEmpty())
  5728. {
  5729. SessionData->LocalDirectory = LocalPath;
  5730. }
  5731. UnicodeString SessionName = SaveHiddenDuplicateSession(SessionData.get());
  5732. ExecuteNewInstance(SessionName, FORMAT(L"%s=\"%s\"", (TProgramParams::FormatSwitch(BROWSE_SWITCH), Item->GetFileName())));
  5733. }
  5734. }
  5735. //---------------------------------------------------------------------------
  5736. void __fastcall TCustomScpExplorerForm::FullSynchronizeInNewWindow(
  5737. TSynchronizeMode Mode, int Params, const UnicodeString & LocalDirectory, const UnicodeString & RemoteDirectory,
  5738. const TCopyParamType * CopyParams)
  5739. {
  5740. UnicodeString SessionName = CreateHiddenDuplicateSession();
  5741. UnicodeString AdditionalParams =
  5742. FORMAT(L"%s%s %s // \"%s\" \"%s\" %d %d", (
  5743. TProgramParams::FormatSwitch(DEFAULTS_SWITCH),
  5744. SerializeCopyParamForCommandLine(CopyParams),
  5745. TProgramParams::FormatSwitch(SYNCHRONIZE_SWITCH),
  5746. LocalDirectory, RemoteDirectory, Mode, Params));
  5747. ExecuteNewInstance(SessionName, AdditionalParams);
  5748. }
  5749. //---------------------------------------------------------------------------
  5750. int __fastcall TCustomScpExplorerForm::DoFullSynchronizeDirectories(
  5751. UnicodeString & LocalDirectory, UnicodeString & RemoteDirectory,
  5752. TSynchronizeMode & Mode, int Params, bool & SaveMode, int UseDefaults)
  5753. {
  5754. int Result;
  5755. bool SaveSettings = false;
  5756. int Options =
  5757. FLAGMASK(!Terminal->IsCapable[fcTimestampChanging], fsoDisableTimestamp) |
  5758. FLAGMASK(SynchronizeAllowSelectedOnly(), fsoAllowSelectedOnly);
  5759. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  5760. TUsableCopyParamAttrs CopyParamAttrs = Terminal->UsableCopyParamAttrs(0);
  5761. bool Continue =
  5762. (UseDefaults == 0) ||
  5763. DoFullSynchronizeDialog(Mode, Params, LocalDirectory, RemoteDirectory,
  5764. &CopyParam, SaveSettings, SaveMode, Options, CopyParamAttrs, FullSynchronizeInNewWindow, UseDefaults);
  5765. if (Continue)
  5766. {
  5767. Configuration->Usage->Inc(L"Synchronizations");
  5768. CopyParam.IncludeFileMask.SetRoots(LocalDirectory, RemoteDirectory);
  5769. UpdateCopyParamCounters(CopyParam);
  5770. TSynchronizeOptions SynchronizeOptions;
  5771. GetSynchronizeOptions(Params, SynchronizeOptions);
  5772. if (SaveSettings)
  5773. {
  5774. GUIConfiguration->SynchronizeParams = Params;
  5775. }
  5776. else
  5777. {
  5778. SaveMode = false;
  5779. }
  5780. TDateTime StartTime = Now();
  5781. TSynchronizeChecklist * Checklist = NULL;
  5782. try
  5783. {
  5784. DebugAssert(!FAutoOperation);
  5785. FAutoOperation = true;
  5786. try
  5787. {
  5788. UnicodeString SessionKey = Terminal->SessionData->SessionKey;
  5789. std::unique_ptr<TStrings> DataList(Configuration->LoadDirectoryStatisticsCache(SessionKey, RemoteDirectory, CopyParam));
  5790. int Files = -1;
  5791. if (DataList->Count >= 1)
  5792. {
  5793. Files = StrToIntDef(DataList->Strings[0], Files);
  5794. }
  5795. else
  5796. {
  5797. DataList->Add(UnicodeString());
  5798. }
  5799. FSynchronizeProgressForm = new TSynchronizeProgressForm(Application, true, Files);
  5800. FSynchronizeProgressForm->Start();
  5801. Checklist = Terminal->SynchronizeCollect(LocalDirectory, RemoteDirectory,
  5802. static_cast<TTerminal::TSynchronizeMode>(Mode),
  5803. &CopyParam, Params | TTerminal::spNoConfirmation, TerminalSynchronizeDirectory,
  5804. &SynchronizeOptions);
  5805. if (Terminal->SessionData->CacheDirectories)
  5806. {
  5807. DataList->Strings[0] = IntToStr(SynchronizeOptions.Files);
  5808. Configuration->SaveDirectoryStatisticsCache(SessionKey, RemoteDirectory, CopyParam, DataList.get());
  5809. }
  5810. }
  5811. __finally
  5812. {
  5813. FAutoOperation = false;
  5814. SetTaskbarListProgressState(TBPF_NOPROGRESS);
  5815. SAFE_DESTROY(FSynchronizeProgressForm);
  5816. }
  5817. TSynchronizeParams SynchronizeParams;
  5818. SynchronizeParams.LocalDirectory = LocalDirectory;
  5819. SynchronizeParams.RemoteDirectory = RemoteDirectory;
  5820. SynchronizeParams.Mode = Mode;
  5821. SynchronizeParams.CopyParam = &CopyParam;
  5822. SynchronizeParams.Params = Params;
  5823. SynchronizeParams.Checklist = Checklist;
  5824. SynchronizeParams.CollectElapsed = (Now() - StartTime);
  5825. SynchronizeParams.StartTime = &StartTime;
  5826. SynchronizeParams.OnProcessedItem = NULL;
  5827. Result = Checklist->Count;
  5828. if (Checklist->Count > 0)
  5829. {
  5830. if (FLAGSET(Params, TTerminal::spPreviewChanges))
  5831. {
  5832. if (!DoSynchronizeChecklistDialog(
  5833. Checklist, Mode, Params, LocalDirectory, RemoteDirectory, CustomCommandMenu, DoFullSynchronize,
  5834. DoSynchronizeChecklistCalculateSize, DoSynchronizeMove, DoSynchronizeBrowse, &SynchronizeParams))
  5835. {
  5836. Result = -1;
  5837. }
  5838. }
  5839. else
  5840. {
  5841. FullSynchronize(SynchronizeParams, NULL, NULL);
  5842. }
  5843. }
  5844. else
  5845. {
  5846. UnicodeString Message = MainInstructions(LoadStr(COMPARE_NO_DIFFERENCES));
  5847. MessageDialog(Message, qtInformation, qaOK, HELP_SYNCHRONIZE_NO_DIFFERENCES);
  5848. }
  5849. }
  5850. __finally
  5851. {
  5852. delete Checklist;
  5853. }
  5854. OperationComplete(StartTime);
  5855. }
  5856. else
  5857. {
  5858. Result = -1;
  5859. }
  5860. return Result;
  5861. }
  5862. //---------------------------------------------------------------------------
  5863. void __fastcall TCustomScpExplorerForm::TerminalSynchronizeDirectory(
  5864. const UnicodeString & LocalDirectory, const UnicodeString & RemoteDirectory,
  5865. bool & Continue, bool Collect, const TSynchronizeOptions * Options)
  5866. {
  5867. if (Collect)
  5868. {
  5869. DebugAssert(FSynchronizeProgressForm != NULL);
  5870. if (!FSynchronizeProgressForm->Started)
  5871. {
  5872. FSynchronizeProgressForm->Start();
  5873. }
  5874. int CompareProgress = FSynchronizeProgressForm->SetData(LocalDirectory, RemoteDirectory, Options->Files, Continue);
  5875. SetTaskbarListProgressValue(CompareProgress);
  5876. }
  5877. else
  5878. {
  5879. DebugAssert(FSynchronizeProgressForm == NULL);
  5880. DebugAssert(FProgressForm != NULL);
  5881. }
  5882. }
  5883. //---------------------------------------------------------------------------
  5884. void __fastcall TCustomScpExplorerForm::StandaloneEdit(const UnicodeString & FileName)
  5885. {
  5886. UnicodeString FullFileName = AbsolutePath(Terminal->CurrentDirectory, FileName);
  5887. TRemoteFile * File = NULL;
  5888. Terminal->ReadFile(FullFileName, File);
  5889. if (File != NULL)
  5890. {
  5891. std::unique_ptr<TRemoteFile> FileOwner(File);
  5892. ExecuteRemoteFile(FullFileName, File, efInternalEditor);
  5893. Application->ShowMainForm = false;
  5894. Application->Run();
  5895. }
  5896. }
  5897. //---------------------------------------------------------------------------
  5898. void __fastcall TCustomScpExplorerForm::ExploreLocalDirectory(TOperationSide)
  5899. {
  5900. DebugFail();
  5901. }
  5902. //---------------------------------------------------------------------------
  5903. TSessionData * __fastcall TCustomScpExplorerForm::CloneCurrentSessionData()
  5904. {
  5905. std::unique_ptr<TSessionData> SessionData(new TSessionData(L""));
  5906. SessionData->Assign(ManagedSession->SessionData);
  5907. UpdateSessionData(SessionData.get());
  5908. DebugAssert(SessionData->IsLocalBrowser == IsLocalBrowserMode());
  5909. if (IsActiveTerminal(ManagedSession))
  5910. {
  5911. ManagedSession->UpdateSessionCredentials(SessionData.get());
  5912. }
  5913. if (!ManagedSession->SessionData->HasSessionName())
  5914. {
  5915. // Particularly for "Workspace/XXXX" name, we need to reset it, as it would become user-visible
  5916. // once IsWorkspace is cleared
  5917. SessionData->Name = UnicodeString();
  5918. }
  5919. // Most uses of this method ends with saving the session, and we do not want to store the IsWorkspace flag.
  5920. // Particularly, with SaveHiddenDuplicateSession, it won't work as TSessionData::ParseUrl explicitly ignored workspace sessions.
  5921. SessionData->IsWorkspace = false;
  5922. return SessionData.release();
  5923. }
  5924. //---------------------------------------------------------------------------
  5925. void __fastcall TCustomScpExplorerForm::SaveCurrentSession()
  5926. {
  5927. TSessionData * SessionData = CloneCurrentSessionData();
  5928. try
  5929. {
  5930. TSessionData * EditingSessionData = StoredSessions->FindSame(SessionData);
  5931. DoSaveSession(SessionData, EditingSessionData, true, NULL);
  5932. }
  5933. __finally
  5934. {
  5935. delete SessionData;
  5936. }
  5937. }
  5938. //---------------------------------------------------------------------------
  5939. TObjectList * __fastcall TCustomScpExplorerForm::DoCollectWorkspace()
  5940. {
  5941. TTerminalManager * Manager = TTerminalManager::Instance();
  5942. std::unique_ptr<TObjectList> DataList(new TObjectList());
  5943. if (DebugAlwaysTrue(ManagedSession != NULL))
  5944. {
  5945. // Update ManagedSession->StateData
  5946. UpdateSession(ManagedSession);
  5947. }
  5948. Manager->SaveWorkspace(DataList.get());
  5949. return DataList.release();
  5950. }
  5951. //---------------------------------------------------------------------------
  5952. void __fastcall TCustomScpExplorerForm::DoSaveWorkspace(
  5953. const UnicodeString & Name, TObjectList * DataList, bool SavePasswords, bool Explicit)
  5954. {
  5955. WinConfiguration->LastWorkspace = Name;
  5956. if (!SavePasswords)
  5957. {
  5958. for (int Index = 0; Index < DataList->Count; Index++)
  5959. {
  5960. TSessionData * SessionData = dynamic_cast<TSessionData *>(DataList->Items[Index]);
  5961. if (SessionData->Link.IsEmpty())
  5962. {
  5963. SessionData->ClearSessionPasswords();
  5964. }
  5965. }
  5966. }
  5967. StoredSessions->NewWorkspace(Name, DataList);
  5968. // modified only
  5969. StoredSessions->Save(false, Explicit);
  5970. }
  5971. //---------------------------------------------------------------------------
  5972. UnicodeString __fastcall TCustomScpExplorerForm::WorkspaceName()
  5973. {
  5974. return
  5975. DefaultStr(WinConfiguration->LastWorkspace,
  5976. DefaultStr(WinConfiguration->AutoWorkspace, LoadStr(NEW_WORKSPACE)));
  5977. }
  5978. //---------------------------------------------------------------------------
  5979. bool __fastcall TCustomScpExplorerForm::SaveWorkspace(bool EnableAutoSave)
  5980. {
  5981. std::unique_ptr<TObjectList> DataList(DoCollectWorkspace());
  5982. bool AnyNonStoredSessionWithPassword = false;
  5983. bool AnyNonStoredNonWorkspaceSessionWithPassword = false;
  5984. bool AllNonStoredSessionsAnonymous = true;
  5985. TTerminalManager * Manager = TTerminalManager::Instance();
  5986. for (int Index = 0; Index < DataList->Count; Index++)
  5987. {
  5988. TSessionData * SessionData = dynamic_cast<TSessionData *>(DataList->Items[Index]);
  5989. if (SessionData->Link.IsEmpty())
  5990. {
  5991. if (SessionData->HasAnySessionPassword())
  5992. {
  5993. AnyNonStoredSessionWithPassword = true;
  5994. if (!Manager->Sessions[Index]->SessionData->IsWorkspace)
  5995. {
  5996. AnyNonStoredNonWorkspaceSessionWithPassword = true;
  5997. }
  5998. }
  5999. if (!SameText(SessionData->UserName, AnonymousUserName))
  6000. {
  6001. AllNonStoredSessionsAnonymous = false;
  6002. }
  6003. }
  6004. }
  6005. bool SavePasswords;
  6006. bool * PSavePasswords;
  6007. bool NotRecommendedSavingPasswords =
  6008. !CustomWinConfiguration->UseMasterPassword &&
  6009. !AllNonStoredSessionsAnonymous;
  6010. if (Configuration->DisablePasswordStoring ||
  6011. !AnyNonStoredSessionWithPassword)
  6012. {
  6013. PSavePasswords = NULL;
  6014. SavePasswords = false;
  6015. }
  6016. else
  6017. {
  6018. PSavePasswords = &SavePasswords;
  6019. SavePasswords =
  6020. !AnyNonStoredNonWorkspaceSessionWithPassword ||
  6021. !NotRecommendedSavingPasswords;
  6022. }
  6023. UnicodeString Name = WorkspaceName();
  6024. bool CreateShortcut = false;
  6025. bool Result =
  6026. DoSaveWorkspaceDialog(
  6027. Name, PSavePasswords, NotRecommendedSavingPasswords, CreateShortcut,
  6028. EnableAutoSave);
  6029. if (Result)
  6030. {
  6031. DoSaveWorkspace(Name, DataList.get(), SavePasswords, true);
  6032. if (CreateShortcut)
  6033. {
  6034. TOperationVisualizer Visualizer;
  6035. UnicodeString AdditionalParams = TProgramParams::FormatSwitch(DESKTOP_SWITCH);
  6036. CreateDesktopSessionShortCut(Name, L"", AdditionalParams, -1, WORKSPACE_ICON);
  6037. }
  6038. if (EnableAutoSave)
  6039. {
  6040. WinConfiguration->AutoSaveWorkspace = true;
  6041. WinConfiguration->AutoWorkspace = Name;
  6042. if (PSavePasswords != NULL)
  6043. {
  6044. WinConfiguration->AutoSaveWorkspacePasswords = SavePasswords;
  6045. }
  6046. }
  6047. }
  6048. return Result;
  6049. }
  6050. //---------------------------------------------------------------------------
  6051. void __fastcall TCustomScpExplorerForm::UpdateSession(TManagedTerminal * Session)
  6052. {
  6053. DebugAssert(IsLocalBrowserMode() == Session->LocalBrowser);
  6054. DebugAssert(Session == ManagedSession);
  6055. if (!IsLocalBrowserMode())
  6056. {
  6057. SAFE_DESTROY(Session->RemoteExplorerState);
  6058. if (WinConfiguration->PreservePanelState)
  6059. {
  6060. Session->RemoteExplorerState = RemoteDirView->SaveState();
  6061. }
  6062. }
  6063. UpdateSessionData(Session->StateData);
  6064. }
  6065. //---------------------------------------------------------------------------
  6066. void __fastcall TCustomScpExplorerForm::UpdateSessionData(TSessionData * Data)
  6067. {
  6068. // Keep in sync with TSessionData::CopyStateData
  6069. DebugAssert(Data != NULL);
  6070. // This is inconsistent with how color (for example) is handled.
  6071. Data->Name = ManagedSession->SessionData->Name;
  6072. if (!IsLocalBrowserMode())
  6073. {
  6074. // cannot use RemoteDirView->Path, because it is empty if connection
  6075. // was already closed
  6076. // also only peek, we may not be connected at all atm
  6077. // so make sure we do not try retrieving current directory from the server
  6078. // (particularly with FTP)
  6079. UnicodeString ACurrentDirectory = Terminal->PeekCurrentDirectory();
  6080. if (!ACurrentDirectory.IsEmpty())
  6081. {
  6082. Data->RemoteDirectory = ACurrentDirectory;
  6083. }
  6084. }
  6085. Data->Color = SessionColor;
  6086. }
  6087. //---------------------------------------------------------------------------
  6088. void __fastcall TCustomScpExplorerForm::ToolBarResize(TObject *Sender)
  6089. {
  6090. TTBXToolbar * Toolbar = dynamic_cast<TTBXToolbar*>(Sender);
  6091. DebugAssert(Toolbar != NULL);
  6092. for (int i = 0; i < Toolbar->Items->Count; i++)
  6093. {
  6094. TTBXCustomDropDownItem * DropDownItem;
  6095. DropDownItem = dynamic_cast<TTBXCustomDropDownItem *>(Toolbar->Items->Items[i]);
  6096. if (DropDownItem != NULL)
  6097. {
  6098. ToolbarItemResize(DropDownItem,
  6099. Toolbar->Width - (Toolbar->View->BaseSize.x - DropDownItem->EditWidth) -
  6100. Toolbar->NonClientWidth);
  6101. break;
  6102. }
  6103. }
  6104. }
  6105. //---------------------------------------------------------------------------
  6106. void __fastcall TCustomScpExplorerForm::ToolbarItemResize(TTBXCustomDropDownItem * Item, int Width)
  6107. {
  6108. Item->EditWidth = Width;
  6109. }
  6110. //---------------------------------------------------------------------------
  6111. void __fastcall TCustomScpExplorerForm::ToolbarGetBaseSize(
  6112. TTBCustomToolbar * Toolbar, TPoint & ASize)
  6113. {
  6114. for (int i = 0; i < Toolbar->Items->Count; i++)
  6115. {
  6116. TTBXCustomDropDownItem * DropDownItem;
  6117. DropDownItem = dynamic_cast<TTBXCustomDropDownItem *>(Toolbar->Items->Items[i]);
  6118. if (DropDownItem != NULL)
  6119. {
  6120. ASize.x -= DropDownItem->EditWidth;
  6121. ASize.x += ScaleByTextHeight(this, 50) /* minimal combo width */;
  6122. }
  6123. }
  6124. }
  6125. //---------------------------------------------------------------------------
  6126. void __fastcall TCustomScpExplorerForm::DoWarnLackOfTempSpace(
  6127. const UnicodeString Path, __int64 RequiredSpace, bool & Continue)
  6128. {
  6129. if (WinConfiguration->DDWarnLackOfTempSpace)
  6130. {
  6131. UnicodeString ADrive = ExtractFileDrive(ExpandFileName(Path));
  6132. if (!ADrive.IsEmpty())
  6133. {
  6134. __int64 FreeSpace = DiskFree((Byte)(ADrive[1]-'A'+1));
  6135. DebugAssert(RequiredSpace >= 0);
  6136. __int64 RequiredWithReserve;
  6137. RequiredWithReserve = (__int64)(RequiredSpace * WinConfiguration->DDWarnLackOfTempSpaceRatio);
  6138. if (FreeSpace < RequiredWithReserve)
  6139. {
  6140. unsigned int Result;
  6141. TMessageParams Params(mpNeverAskAgainCheck);
  6142. Result = MessageDialog(FMTLOAD(DD_WARN_LACK_OF_TEMP_SPACE, (Path,
  6143. FormatBytes(FreeSpace), FormatBytes(RequiredSpace))),
  6144. qtWarning, qaYes | qaNo, HELP_DD_WARN_LACK_OF_TEMP_SPACE, &Params);
  6145. if (Result == qaNeverAskAgain)
  6146. {
  6147. WinConfiguration->DDWarnLackOfTempSpace = false;
  6148. }
  6149. Continue = (Result == qaYes || Result == qaNeverAskAgain);
  6150. }
  6151. }
  6152. }
  6153. }
  6154. //---------------------------------------------------------------------------
  6155. void __fastcall TCustomScpExplorerForm::AddBookmark(TOperationSide Side)
  6156. {
  6157. DoOpenDirectoryDialog(odAddBookmark, Side);
  6158. }
  6159. //---------------------------------------------------------------------------
  6160. TStrings * __fastcall TCustomScpExplorerForm::CreateVisitedDirectories(TOperationSide Side)
  6161. {
  6162. // we should better use TCustomDirView::FCaseSensitive, but it is private
  6163. TStringList * VisitedDirectories = CreateSortedStringList(!IsSideLocalBrowser(Side));
  6164. try
  6165. {
  6166. TCustomDirView * DView = DirView(Side);
  6167. for (int Index = -DView->BackCount; Index <= DView->ForwardCount; Index++)
  6168. {
  6169. VisitedDirectories->Add(DView->HistoryPath[Index]);
  6170. }
  6171. }
  6172. catch (...)
  6173. {
  6174. delete VisitedDirectories;
  6175. throw;
  6176. }
  6177. return VisitedDirectories;
  6178. }
  6179. //---------------------------------------------------------------------------
  6180. void __fastcall TCustomScpExplorerForm::DoOpenDirectoryDialog(
  6181. TOpenDirectoryMode Mode, TOperationSide Side)
  6182. {
  6183. bool Continue = true;
  6184. if (Mode == odAddBookmark)
  6185. {
  6186. TMessageParams Params(mpNeverAskAgainCheck);
  6187. Params.NeverAskAgainTitle = LoadStr(ADD_BOOKMARK_SHARED);
  6188. Params.NeverAskAgainCheckedInitially = WinConfiguration->UseSharedBookmarks;
  6189. UnicodeString Message = MainInstructions(FMTLOAD(ADD_BOOKMARK_CONFIRM, (DirView(Side)->PathName)));
  6190. unsigned int Answer =
  6191. MessageDialog(Message,
  6192. qtConfirmation, qaOK | qaCancel, HELP_ADD_BOOKMARK_CONFIRM, &Params);
  6193. if (Answer == qaNeverAskAgain)
  6194. {
  6195. Continue = true;
  6196. WinConfiguration->UseSharedBookmarks = true;
  6197. }
  6198. else if (Answer == qaOK)
  6199. {
  6200. Continue = true;
  6201. WinConfiguration->UseSharedBookmarks = false;
  6202. }
  6203. else
  6204. {
  6205. Continue = false;
  6206. }
  6207. }
  6208. if (Continue)
  6209. {
  6210. TStrings * VisitedDirectories = CreateVisitedDirectories(Side);
  6211. try
  6212. {
  6213. UnicodeString Name = DirView(Side)->PathName;
  6214. bool Repeat;
  6215. do
  6216. {
  6217. Repeat = false;
  6218. TOperationSide BookmarkSide = IsSideLocalBrowser(Side) ? osLocal : osRemote;
  6219. if (::DoOpenDirectoryDialog(Mode, BookmarkSide, Name, VisitedDirectories, Terminal,
  6220. // do not allow switching to location profiles,
  6221. // if we are not connected
  6222. HasDirView[osLocal] && (Terminal != NULL)))
  6223. {
  6224. TWindowLock Lock(this);
  6225. if (!TryOpenDirectory(Side, Name))
  6226. {
  6227. Repeat = true;
  6228. Mode = odBrowse;
  6229. }
  6230. }
  6231. }
  6232. while (Repeat);
  6233. }
  6234. __finally
  6235. {
  6236. delete VisitedDirectories;
  6237. }
  6238. }
  6239. }
  6240. //---------------------------------------------------------------------------
  6241. bool __fastcall TCustomScpExplorerForm::CommandSessionFallback()
  6242. {
  6243. DebugAssert(!Terminal->CommandSessionOpened);
  6244. return TTerminalManager::Instance()->ConnectTerminal(Terminal->CommandSession);
  6245. }
  6246. //---------------------------------------------------------------------------
  6247. bool __fastcall TCustomScpExplorerForm::EnsureCommandSessionFallback(TFSCapability Capability)
  6248. {
  6249. bool Result = Terminal->IsCapable[Capability] ||
  6250. Terminal->CommandSessionOpened;
  6251. if (!Result)
  6252. {
  6253. DebugAssert(Terminal->IsCapable[fcSecondaryShell]);
  6254. if (!GUIConfiguration->ConfirmCommandSession)
  6255. {
  6256. Result = true;
  6257. }
  6258. else
  6259. {
  6260. TMessageParams Params(mpNeverAskAgainCheck);
  6261. const TFileSystemInfo & FileSystemInfo = Terminal->GetFileSystemInfo();
  6262. unsigned int Answer = MessageDialog(FMTLOAD(PERFORM_ON_COMMAND_SESSION2,
  6263. (FileSystemInfo.ProtocolName, FileSystemInfo.ProtocolName)), qtConfirmation,
  6264. qaOK | qaCancel, HELP_PERFORM_ON_COMMAND_SESSION, &Params);
  6265. if (Answer == qaNeverAskAgain)
  6266. {
  6267. GUIConfiguration->ConfirmCommandSession = false;
  6268. Result = true;
  6269. }
  6270. else if (Answer == qaOK)
  6271. {
  6272. Result = true;
  6273. }
  6274. }
  6275. if (Result)
  6276. {
  6277. Result = CommandSessionFallback();
  6278. }
  6279. }
  6280. return Result;
  6281. }
  6282. //---------------------------------------------------------------------------
  6283. void __fastcall TCustomScpExplorerForm::OpenConsole(UnicodeString Command)
  6284. {
  6285. if (EnsureCommandSessionFallback(fcAnyCommand))
  6286. {
  6287. DoConsoleDialog(Terminal, Command);
  6288. }
  6289. }
  6290. //---------------------------------------------------------------------------
  6291. void __fastcall TCustomScpExplorerForm::FileControlDDDragEnter(
  6292. TObject *Sender, _di_IDataObject /*DataObj*/, int /*grfKeyState*/,
  6293. const TPoint & /*Point*/, int & /*dwEffect*/, bool & Accept)
  6294. {
  6295. if (!IsSideLocalBrowser(osRemote) &&
  6296. IsFileControl(DropSourceControl, osRemote) &&
  6297. (FDDExtMapFile != NULL))
  6298. {
  6299. Accept = true;
  6300. }
  6301. FDDTargetControl = dynamic_cast<TControl*>(Sender);
  6302. DebugAssert(FDDTargetControl != NULL);
  6303. }
  6304. //---------------------------------------------------------------------------
  6305. void __fastcall TCustomScpExplorerForm::SessionsDDDragEnter(
  6306. _di_IDataObject DataObj, int KeyState,
  6307. const TPoint & Point, int & Effect, bool & Accept)
  6308. {
  6309. FileControlDDDragEnter(SessionsPageControl, DataObj, KeyState, Point, Effect, Accept);
  6310. }
  6311. //---------------------------------------------------------------------------
  6312. void __fastcall TCustomScpExplorerForm::QueueDDDragEnter(
  6313. _di_IDataObject DataObj, int KeyState,
  6314. const TPoint & Point, int & Effect, bool & Accept)
  6315. {
  6316. FileControlDDDragEnter(QueueView3, DataObj, KeyState, Point, Effect, Accept);
  6317. }
  6318. //---------------------------------------------------------------------------
  6319. void __fastcall TCustomScpExplorerForm::FileControlDDDragLeave(
  6320. TObject * Sender)
  6321. {
  6322. DebugUsedParam(Sender);
  6323. DebugAssert(FDDTargetControl == Sender);
  6324. FDDTargetControl = NULL;
  6325. }
  6326. //---------------------------------------------------------------------------
  6327. void __fastcall TCustomScpExplorerForm::SessionsDDDragLeave()
  6328. {
  6329. FileControlDDDragLeave(SessionsPageControl);
  6330. }
  6331. //---------------------------------------------------------------------------
  6332. void __fastcall TCustomScpExplorerForm::QueueDDDragLeave()
  6333. {
  6334. FileControlDDDragLeave(QueueView3);
  6335. }
  6336. //---------------------------------------------------------------------------
  6337. void __fastcall TCustomScpExplorerForm::AddEditLink(TOperationSide Side, bool Add)
  6338. {
  6339. DebugAssert(!IsSideLocalBrowser(Side));
  6340. DebugUsedParam(Side);
  6341. bool Edit = false;
  6342. TRemoteFile * File = NULL;
  6343. UnicodeString FileName;
  6344. UnicodeString PointTo;
  6345. bool SymbolicLink = true;
  6346. if (RemoteDirView->ItemFocused)
  6347. {
  6348. DebugAssert(RemoteDirView->ItemFocused->Data);
  6349. File = (TRemoteFile *)RemoteDirView->ItemFocused->Data;
  6350. Edit = !Add && File->IsSymLink && Terminal->SessionData->ResolveSymlinks;
  6351. if (Edit)
  6352. {
  6353. FileName = File->FileName;
  6354. PointTo = File->LinkTo;
  6355. }
  6356. else
  6357. {
  6358. PointTo = File->FileName;
  6359. }
  6360. }
  6361. if (DoSymlinkDialog(FileName, PointTo, osRemote, SymbolicLink, Edit,
  6362. Terminal->IsCapable[fcHardLink]))
  6363. {
  6364. Configuration->Usage->Inc(L"RemoteLinksCreated");
  6365. if (Edit)
  6366. {
  6367. DebugAssert(File->FileName == FileName);
  6368. int Params = dfNoRecursive;
  6369. Terminal->ExceptionOnFail = true;
  6370. try
  6371. {
  6372. Terminal->DeleteFile(L"", File, &Params);
  6373. }
  6374. __finally
  6375. {
  6376. Terminal->ExceptionOnFail = false;
  6377. }
  6378. }
  6379. Terminal->CreateLink(FileName, PointTo, SymbolicLink);
  6380. }
  6381. }
  6382. //---------------------------------------------------------------------------
  6383. bool __fastcall TCustomScpExplorerForm::CanAddEditLink(TOperationSide Side)
  6384. {
  6385. return
  6386. (IsSideLocalBrowser(Side) ||
  6387. (HasActiveTerminal() &&
  6388. Terminal->ResolvingSymlinks &&
  6389. Terminal->IsCapable[fcSymbolicLink]));
  6390. }
  6391. //---------------------------------------------------------------------------
  6392. bool __fastcall TCustomScpExplorerForm::LinkFocused()
  6393. {
  6394. return
  6395. !IsSideLocalBrowser(FCurrentSide) &&
  6396. (RemoteDirView->ItemFocused != NULL) &&
  6397. ((TRemoteFile *)RemoteDirView->ItemFocused->Data)->IsSymLink &&
  6398. Terminal->SessionData->ResolveSymlinks;
  6399. }
  6400. //---------------------------------------------------------------------------
  6401. void __fastcall TCustomScpExplorerForm::ExecuteCurrentFile()
  6402. {
  6403. DebugAssert(!WinConfiguration->DisableOpenEdit);
  6404. FForceExecution = true;
  6405. try
  6406. {
  6407. DirView(osCurrent)->ExecuteCurrentFile();
  6408. }
  6409. __finally
  6410. {
  6411. FForceExecution = false;
  6412. }
  6413. }
  6414. //---------------------------------------------------------------------------
  6415. void __fastcall TCustomScpExplorerForm::ExecuteCurrentFileWith(bool OnFocused)
  6416. {
  6417. TEditorData ExternalEditor;
  6418. ExternalEditor.Editor = edExternal;
  6419. bool Remember = false;
  6420. if (DoEditorPreferencesDialog(&ExternalEditor, Remember, epmAdHoc, !IsSideLocalBrowser(osCurrent)))
  6421. {
  6422. if (Remember)
  6423. {
  6424. TEditorList * EditorList = new TEditorList;
  6425. try
  6426. {
  6427. *EditorList = *WinConfiguration->EditorList;
  6428. bool Found = false;
  6429. int i = 0;
  6430. while (!Found && (i < EditorList->Count))
  6431. {
  6432. const TEditorPreferences * Editor = EditorList->Editors[i];
  6433. if ((Editor->Data->Editor == edExternal) &&
  6434. (Editor->Data->ExternalEditor == ExternalEditor.ExternalEditor))
  6435. {
  6436. Found = true;
  6437. }
  6438. i++;
  6439. }
  6440. if (!Found)
  6441. {
  6442. EditorList->Add(new TEditorPreferences(ExternalEditor));
  6443. WinConfiguration->EditorList = EditorList;
  6444. }
  6445. }
  6446. __finally
  6447. {
  6448. delete EditorList;
  6449. }
  6450. }
  6451. ExecuteFile(osCurrent, efExternalEditor, &ExternalEditor, true, OnFocused);
  6452. }
  6453. }
  6454. //---------------------------------------------------------------------------
  6455. void __fastcall TCustomScpExplorerForm::DetachTerminal(TObject * ATerminal)
  6456. {
  6457. FEditorManager->ProcessFiles(FileTerminalRemoved, ATerminal);
  6458. if (FFileFindTerminal == ATerminal)
  6459. {
  6460. FFileFindTerminal = NULL;
  6461. HideFileFindDialog();
  6462. }
  6463. if (FClipboardTerminal == ATerminal)
  6464. {
  6465. ClipboardClear(); // implies ClipboardStop
  6466. }
  6467. if ((FOnSynchronizeAbort != NULL) &&
  6468. (ATerminal == FManagedSession))
  6469. {
  6470. FOnSynchronizeAbort(NULL);
  6471. }
  6472. }
  6473. //---------------------------------------------------------------------------
  6474. void __fastcall TCustomScpExplorerForm::TerminalRemoved(TObject * Sender)
  6475. {
  6476. DetachTerminal(Sender);
  6477. }
  6478. //---------------------------------------------------------------------------
  6479. void __fastcall TCustomScpExplorerForm::FileTerminalRemoved(const UnicodeString FileName,
  6480. TEditedFileData * Data, TObject * /*Token*/, void * Arg)
  6481. {
  6482. TTerminal * Terminal = static_cast<TTerminal *>(Arg);
  6483. DebugAssert(Terminal != NULL);
  6484. if (Data->Terminal == Terminal)
  6485. {
  6486. Data->Terminal = NULL;
  6487. }
  6488. }
  6489. //---------------------------------------------------------------------------
  6490. TManagedTerminal * TCustomScpExplorerForm::GetReplacementForLastSession()
  6491. {
  6492. return NULL;
  6493. }
  6494. //---------------------------------------------------------------------------
  6495. void __fastcall TCustomScpExplorerForm::LastTerminalClosed()
  6496. {
  6497. UpdateControls();
  6498. SessionColor = TColor(0);
  6499. UpdateRemotePathComboBox(false);
  6500. NeedSession(false);
  6501. }
  6502. //---------------------------------------------------------------------------
  6503. void __fastcall TCustomScpExplorerForm::NeedSession(bool Startup)
  6504. {
  6505. try
  6506. {
  6507. // Cache, as the login dialog can change its value.
  6508. // See also DoShow. Also see TTerminalManager::NewSession.
  6509. bool ShowLogin = WinConfiguration->ShowLoginWhenNoSession;
  6510. try
  6511. {
  6512. if (ShowLogin)
  6513. {
  6514. bool ReloadSessions = !Startup;
  6515. TTerminalManager::Instance()->NewSession(L"", ReloadSessions, this, true);
  6516. }
  6517. }
  6518. __finally
  6519. {
  6520. // Do not terminate, if we are only starting up and we are not showing Login dialog
  6521. // (so there was no chance for the user to open any session yet)
  6522. if ((ShowLogin || !Startup) &&
  6523. !WinConfiguration->KeepOpenWhenNoSession &&
  6524. // this rules out the implicit Commander's local-local browser
  6525. ((Terminal == NULL) || (!Terminal->Active && !Terminal->Permanent)))
  6526. {
  6527. TerminateApplication();
  6528. }
  6529. }
  6530. }
  6531. catch (EAbort &)
  6532. {
  6533. // swallow
  6534. // The TTerminalManager does not expect the OnLastTerminalClose to throw without trying to connect.
  6535. // Also when called from TWinControl.UpdateShowing => CMShowingChanged, the showing is aborted on exception
  6536. }
  6537. }
  6538. //---------------------------------------------------------------------------
  6539. void __fastcall TCustomScpExplorerForm::SessionListChanged(bool ForceTruncationUpdate)
  6540. {
  6541. TTerminalManager * Manager = TTerminalManager::Instance();
  6542. DebugAssert(!Manager->Updating);
  6543. TAutoFlag UpdatingSessionTabsFlag(FUpdatingSessionTabs);
  6544. int ActiveSessionIndex = Manager->ActiveSessionIndex;
  6545. Configuration->Usage->SetMax(L"MaxOpenedSessions", Manager->Count);
  6546. SendMessage(SessionsPageControl->Handle, WM_SETREDRAW, 0, 0);
  6547. try
  6548. {
  6549. int PrevTabsWidth = SessionsPageControl->TotalTabsWidth();
  6550. while (SessionsPageControl->PageCount > Manager->Count + 1)
  6551. {
  6552. delete SessionsPageControl->Pages[SessionsPageControl->PageCount - 1];
  6553. }
  6554. for (int Index = 0; Index <= Manager->Count; Index++)
  6555. {
  6556. TThemeTabSheet * TabSheet;
  6557. if (Index >= SessionsPageControl->PageCount)
  6558. {
  6559. TabSheet = new TThemeTabSheet(SessionsPageControl);
  6560. TabSheet->PageControl = SessionsPageControl;
  6561. }
  6562. else
  6563. {
  6564. TabSheet = SessionsPageControl->Pages[Index];
  6565. }
  6566. bool IsSessionTab = (Index < Manager->Count);
  6567. if (IsSessionTab)
  6568. {
  6569. TTerminal * Terminal = Manager->Sessions[Index];
  6570. TabSheet->Tag = reinterpret_cast<int>(Terminal);
  6571. TabSheet->CaptionTruncation = tttEllipsis;
  6572. UpdateSessionTab(TabSheet);
  6573. }
  6574. else
  6575. {
  6576. TabSheet->Tag = 0; // not really needed
  6577. TabSheet->Shadowed = false;
  6578. TabSheet->Button = SupportsLocalBrowser() ? ttbDropDown : ttbNone;
  6579. TabSheet->CaptionTruncation = tttNoText;
  6580. // We know that we are at the last page, otherwise we could not call this (it assumes that new session tab is the last one)
  6581. UpdateNewTabTab();
  6582. }
  6583. }
  6584. // Prevent flicker on the right side where scrolling buttons would be, unless really needed
  6585. if (ForceTruncationUpdate || (SessionsPageControl->TotalTabsWidth() != PrevTabsWidth))
  6586. {
  6587. SessionsPageControl->UpdateTabsCaptionTruncation();
  6588. }
  6589. }
  6590. __finally
  6591. {
  6592. SendMessage(SessionsPageControl->Handle, WM_SETREDRAW, 1, 0);
  6593. }
  6594. SessionsPageControl->ActivePageIndex = ActiveSessionIndex;
  6595. }
  6596. //---------------------------------------------------------------------------
  6597. UnicodeString TCustomScpExplorerForm::GetNewTabTabCaption()
  6598. {
  6599. return StripHotkey(StripTrailingPunctuation(NonVisualDataModule->NewTabAction->Caption));
  6600. }
  6601. //---------------------------------------------------------------------------
  6602. void __fastcall TCustomScpExplorerForm::UpdateNewTabTab()
  6603. {
  6604. TThemeTabSheet * TabSheet = SessionsPageControl->Pages[SessionsPageControl->PageCount - 1];
  6605. UnicodeString TabCaption;
  6606. if (WinConfiguration->SelectiveToolbarText)
  6607. {
  6608. TabCaption = GetNewTabTabCaption();
  6609. }
  6610. TabSheet->BaseCaption = TabCaption;
  6611. TabSheet->ImageIndex = GetNewTabTabImageIndex(osCurrent);
  6612. }
  6613. //---------------------------------------------------------------------------
  6614. TManagedTerminal * __fastcall TCustomScpExplorerForm::GetSessionTabSession(TTabSheet * TabSheet)
  6615. {
  6616. return reinterpret_cast<TManagedTerminal *>(TabSheet->Tag);
  6617. }
  6618. //---------------------------------------------------------------------------
  6619. UnicodeString TCustomScpExplorerForm::GetLocalBrowserSessionTitle(TManagedTerminal *)
  6620. {
  6621. DebugFail();
  6622. return UnicodeString();
  6623. }
  6624. //---------------------------------------------------------------------------
  6625. void TCustomScpExplorerForm::UpdateSessionTab(TThemeTabSheet * TabSheet)
  6626. {
  6627. if (DebugAlwaysTrue(TabSheet != NULL))
  6628. {
  6629. TManagedTerminal * ASession = GetSessionTabSession(TabSheet);
  6630. if (DebugAlwaysTrue(ASession != NULL))
  6631. {
  6632. TColor Color = (ASession == Terminal) ? FSessionColor : ASession->StateData->Color;
  6633. if (ASession->LocalBrowser)
  6634. {
  6635. TabSheet->ImageIndex = FLocalBrowserTabImageIndex;
  6636. }
  6637. else
  6638. {
  6639. TabSheet->ImageIndex = AddSessionColor(Color);
  6640. }
  6641. TTerminalManager * Manager = TTerminalManager::Instance();
  6642. TabSheet->Shadowed = !ASession->Active && !ASession->LocalBrowser;
  6643. TabSheet->Button = CanCloseSession(ASession) ? ttbClose : ttbNone;
  6644. TabSheet->BaseCaption = Manager->GetSessionTitle(ASession, true);
  6645. }
  6646. }
  6647. }
  6648. //---------------------------------------------------------------------------
  6649. bool TCustomScpExplorerForm::CanCloseSession(TManagedTerminal * Session)
  6650. {
  6651. return !Session->LocalBrowser || (TTerminalManager::Instance()->Count > 1);
  6652. }
  6653. //---------------------------------------------------------------------------
  6654. bool __fastcall TCustomScpExplorerForm::SessionTabSwitched()
  6655. {
  6656. DebugAssert(SessionsPageControl->ActivePage != NULL);
  6657. TManagedTerminal * Session = GetSessionTabSession(SessionsPageControl->ActivePage);
  6658. bool Result = (Session != NULL);
  6659. if (Result)
  6660. {
  6661. TTerminalManager::Instance()->ActiveSession = Session;
  6662. }
  6663. else
  6664. {
  6665. try
  6666. {
  6667. NewTab();
  6668. }
  6669. __finally
  6670. {
  6671. SessionListChanged();
  6672. }
  6673. FSessionsPageControlNewTabTime = Now();
  6674. }
  6675. return Result;
  6676. }
  6677. //---------------------------------------------------------------------------
  6678. void __fastcall TCustomScpExplorerForm::SessionsPageControlChange(TObject * /*Sender*/)
  6679. {
  6680. SessionTabSwitched();
  6681. }
  6682. //---------------------------------------------------------------------------
  6683. void __fastcall TCustomScpExplorerForm::TransferListChange(TObject * Sender)
  6684. {
  6685. TTBXStringList * TransferList = dynamic_cast<TTBXStringList *>(Sender);
  6686. DebugAssert(TransferList != NULL);
  6687. UnicodeString Name;
  6688. if (TransferList->ItemIndex <= 0)
  6689. {
  6690. Name = L"";
  6691. }
  6692. else
  6693. {
  6694. Name = GUIConfiguration->CopyParamList->Names[TransferList->ItemIndex - 1];
  6695. }
  6696. if (FCopyParamAutoSelected.IsEmpty())
  6697. {
  6698. // if previous preset was not autoselected, make new preset the "default"
  6699. FCopyParamDefault = Name;
  6700. }
  6701. GUIConfiguration->CopyParamCurrent = Name;
  6702. }
  6703. //---------------------------------------------------------------------------
  6704. void __fastcall TCustomScpExplorerForm::UpdateTransferLabel()
  6705. {
  6706. // sanity check
  6707. bool ExistingPreset =
  6708. (FTransferListHoverIndex >= 0) &&
  6709. (FTransferListHoverIndex < 1 + GUIConfiguration->CopyParamList->Count);
  6710. DebugAssert(ExistingPreset);
  6711. if (ExistingPreset)
  6712. {
  6713. HDC DC = GetDC(0);
  6714. TCanvas * Canvas = new TCanvas();
  6715. try
  6716. {
  6717. Canvas->Handle = DC;
  6718. Canvas->Font = GetToolbarFont(this);
  6719. UnicodeString Name;
  6720. if (FTransferListHoverIndex == 0)
  6721. {
  6722. Name = L"";
  6723. }
  6724. else
  6725. {
  6726. Name = GUIConfiguration->CopyParamList->Names[FTransferListHoverIndex - 1];
  6727. }
  6728. TTBXLabelItem * TransferLabel = dynamic_cast<TTBXLabelItem*>(
  6729. static_cast<TComponent*>(GetComponent(fcTransferLabel)));
  6730. TTBXStringList * TransferList = dynamic_cast<TTBXStringList*>(
  6731. static_cast<TObject*>(GetComponent(fcTransferList)));
  6732. UnicodeString InfoStr =
  6733. GUIConfiguration->CopyParamPreset[Name].
  6734. GetInfoStr(L"; ",
  6735. FLAGMASK(HasActiveTerminal(), Terminal->UsableCopyParamAttrs(0).General));
  6736. int MaxWidth = TransferList->MinWidth - (2 * TransferLabel->Margin) - ScaleByTextHeight(this, 10);
  6737. if (Canvas->TextExtent(InfoStr).cx > MaxWidth)
  6738. {
  6739. while (Canvas->TextExtent(InfoStr + Ellipsis).cx > MaxWidth)
  6740. {
  6741. InfoStr.SetLength(InfoStr.Length() - 1);
  6742. }
  6743. InfoStr += Ellipsis;
  6744. }
  6745. // UpdateCaption does not cause invalidation of whole submenu, while
  6746. // setting Caption property does.
  6747. // also it probably does not resize the label, even if necessary
  6748. // (we do not want that anyway)
  6749. TransferLabel->UpdateCaption(InfoStr);
  6750. }
  6751. __finally
  6752. {
  6753. Canvas->Handle = NULL;
  6754. ReleaseDC(0, DC);
  6755. delete Canvas;
  6756. }
  6757. }
  6758. }
  6759. //---------------------------------------------------------------------------
  6760. void __fastcall TCustomScpExplorerForm::TransferListDrawItem(
  6761. TTBXCustomList * /*Sender*/, TCanvas * /*ACanvas*/, const TRect & /*ARect*/,
  6762. int /*AIndex*/, int AHoverIndex, bool & /*DrawDefault*/)
  6763. {
  6764. if (FTransferListHoverIndex != AHoverIndex)
  6765. {
  6766. FTransferListHoverIndex = AHoverIndex;
  6767. UpdateTransferLabel();
  6768. }
  6769. }
  6770. //---------------------------------------------------------------------------
  6771. void __fastcall TCustomScpExplorerForm::WMAppCommand(TMessage & Message)
  6772. {
  6773. int Command = GET_APPCOMMAND_LPARAM(Message.LParam);
  6774. TShiftState Shift = KeyDataToShiftState(GET_KEYSTATE_LPARAM(Message.LParam));
  6775. if ((Shift * (TShiftState() << ssShift << ssAlt << ssCtrl)).Empty())
  6776. {
  6777. if (Command == APPCOMMAND_BROWSER_FAVORITES)
  6778. {
  6779. if (!NonVisualDataModule->Busy)
  6780. {
  6781. OpenDirectory(GetSide(osCurrent));
  6782. }
  6783. Message.Result = 1;
  6784. }
  6785. else
  6786. {
  6787. TForm::Dispatch(&Message);
  6788. }
  6789. }
  6790. else
  6791. {
  6792. TForm::Dispatch(&Message);
  6793. }
  6794. }
  6795. //---------------------------------------------------------------------------
  6796. void __fastcall TCustomScpExplorerForm::CMDialogChar(TMessage & AMessage)
  6797. {
  6798. TCMDialogChar & Message = reinterpret_cast<TCMDialogChar &>(AMessage);
  6799. if ((FIgnoreNextDialogChar != 0) &&
  6800. (toupper(Message.CharCode) == toupper(FIgnoreNextDialogChar)))
  6801. {
  6802. Message.Result = 1;
  6803. }
  6804. else
  6805. {
  6806. TForm::Dispatch(&Message);
  6807. }
  6808. FIgnoreNextDialogChar = 0;
  6809. }
  6810. //---------------------------------------------------------------------------
  6811. void __fastcall TCustomScpExplorerForm::WMSysCommand(TMessage & Message)
  6812. {
  6813. // The four low-order bits are used internally by Windows
  6814. unsigned int Cmd = (Message.WParam & 0xFFF0);
  6815. // SC_RESTORE, SC_MAXIMIZE, SC_MINIMIZE - buttons on windows title
  6816. // SC_DEFAULT - double click on windows title (does not work, at least on WinXP)
  6817. // 61730 - restore thru double click - undocumented
  6818. // 61490 - maximize thru double click - undocumented
  6819. if ((Cmd == SC_RESTORE) || (Cmd == SC_MAXIMIZE) ||
  6820. (Cmd == SC_MINIMIZE) || (Cmd == SC_DEFAULT))
  6821. {
  6822. SysResizing(Cmd);
  6823. }
  6824. TForm::Dispatch(&Message);
  6825. }
  6826. //---------------------------------------------------------------------------
  6827. void __fastcall TCustomScpExplorerForm::WMQueryEndSession(TMessage & Message)
  6828. {
  6829. // We were actually never able to make ENDSESSION_CRITICAL happen.
  6830. // Also there no point returning TRUE as we are not able to
  6831. // handle the abrupt termination caused by subsequent WM_ENDSESSION cleanly.
  6832. // Hence the process termination might be safer :)
  6833. if ((Message.LParam != ENDSESSION_CRITICAL) &&
  6834. (((FQueue != NULL) && !FQueue->IsEmpty) || (FProgressForm != NULL)))
  6835. {
  6836. Message.Result = FALSE;
  6837. }
  6838. else
  6839. {
  6840. Message.Result = TRUE;
  6841. }
  6842. // Do not call default handling as that triggers OnCloseQuery,
  6843. // where our implementation will popup confirmation dialogs, what we do not want,
  6844. // as per Vista guidelines:
  6845. // https://docs.microsoft.com/en-us/windows/win32/shutdown/shutting-down
  6846. }
  6847. //---------------------------------------------------------------------------
  6848. void __fastcall TCustomScpExplorerForm::WMEndSession(TWMEndSession & Message)
  6849. {
  6850. if (Message.EndSession && IsApplicationMinimized())
  6851. {
  6852. // WORKAROUND
  6853. // TApplication.WndProc() calls Application.Terminate() before Halt(),
  6854. // when it receives WM_ENDSESSION,
  6855. // but that sometimes (particularly when application is minimized) cause crashes.
  6856. // Crash popups message that blocks log off.
  6857. // Obviously application cannot shutdown cleanly after WM_ENDSESSION,
  6858. // so we call ExitProcess() immediately, not even trying to cleanup.
  6859. // It still causes beep, so there's likely some popup, but it does not block
  6860. // log off.
  6861. ExitProcess(0);
  6862. }
  6863. TForm::Dispatch(&Message);
  6864. }
  6865. #ifdef _DEBUG
  6866. //---------------------------------------------------------------------------
  6867. void __fastcall TCustomScpExplorerForm::WMWindowPosChanged(TWMWindowPosMsg & Message)
  6868. {
  6869. TForm::Dispatch(&Message);
  6870. }
  6871. #endif
  6872. //---------------------------------------------------------------------------
  6873. void __fastcall TCustomScpExplorerForm::SysResizing(unsigned int /*Cmd*/)
  6874. {
  6875. }
  6876. //---------------------------------------------------------------------------
  6877. void __fastcall TCustomScpExplorerForm::DoShow()
  6878. {
  6879. // Called from TCustomScpExplorerForm::CMShowingChanged
  6880. // only now are the controls resized finally, so the size constraints
  6881. // will not conflict with possibly very small window size
  6882. RestoreFormParams();
  6883. // This indicates that the form was rescaled after parameters were loaded and
  6884. // as most probably the parameters were actually saved with the current form scaling
  6885. // (windows was the last time likely closed on the same monitor, where it is starting now),
  6886. // rescaling happened twice already. Reload the parameters, as now they likely won't need any scaling
  6887. // and no rounding problems will occur.
  6888. // See also RestoreForm.
  6889. // (we should be safe to call RestoreParams unconditionally)
  6890. if (PixelsPerInch != Screen->PixelsPerInch)
  6891. {
  6892. RestoreParams();
  6893. }
  6894. FixControlsPlacement();
  6895. if (Position == poDefaultPosOnly)
  6896. {
  6897. CutFormToDesktop(this);
  6898. }
  6899. TForm::DoShow();
  6900. FSessionsDragDropFilesEx->DragDropControl = SessionsPageControl;
  6901. FQueueDragDropFilesEx->DragDropControl = QueueView3;
  6902. // This was previously in NeedSession, where other related code is.
  6903. // But that is called from CMShowingChanged, when the window is already visible and the local-local window state
  6904. // unpleasantly shows briefly.
  6905. if ((ManagedSession == NULL) &&
  6906. !WinConfiguration->ShowLoginWhenNoSession &&
  6907. WinConfiguration->AutoSaveWorkspace && !WinConfiguration->AutoWorkspace.IsEmpty() &&
  6908. // This detects if workspace was saved the last time the main widow was closed
  6909. SameText(WinConfiguration->LastStoredSession, WinConfiguration->AutoWorkspace))
  6910. {
  6911. DoOpenFolderOrWorkspace(WinConfiguration->AutoWorkspace, false, true);
  6912. Configuration->Usage->Inc(L"OpenedWorkspacesAuto");
  6913. }
  6914. // Before main window displays
  6915. if (ManagedSession == NULL)
  6916. {
  6917. StartingWithoutSession();
  6918. }
  6919. UpdatePixelsPerInchMainWindowCounter();
  6920. FShowing = true;
  6921. }
  6922. //---------------------------------------------------------------------------
  6923. void __fastcall TCustomScpExplorerForm::UpdatePixelsPerInchMainWindowCounter()
  6924. {
  6925. Configuration->Usage->Set(L"PixelsPerInchMainWindow", PixelsPerInch);
  6926. }
  6927. //---------------------------------------------------------------------------
  6928. void __fastcall TCustomScpExplorerForm::StartingWithoutSession()
  6929. {
  6930. SessionListChanged();
  6931. InitStatusBar();
  6932. UpdateControls();
  6933. }
  6934. //---------------------------------------------------------------------------
  6935. void __fastcall TCustomScpExplorerForm::PopupTrayBalloon(TTerminal * Terminal,
  6936. const UnicodeString & Str, TQueryType Type, Exception * E, unsigned int Seconds,
  6937. TNotifyEvent OnBalloonClick, TObject * UserData)
  6938. {
  6939. bool Do;
  6940. UnicodeString Message;
  6941. if (E == NULL)
  6942. {
  6943. Message = Str;
  6944. Do = true;
  6945. }
  6946. else
  6947. {
  6948. Do = ExceptionMessage(E, Message);
  6949. }
  6950. if (Do && WinConfiguration->BalloonNotifications)
  6951. {
  6952. UnicodeString Title;
  6953. if ((Terminal == NULL) && (Type == qtInformation) && ExtractMainInstructions(Message, Title))
  6954. {
  6955. Message = TrimLeft(Message);
  6956. }
  6957. else
  6958. {
  6959. Message = UnformatMessage(Message);
  6960. const ResourceString * Captions[] = { &_SMsgDlgConfirm, &_SMsgDlgWarning,
  6961. &_SMsgDlgError, &_SMsgDlgInformation, NULL };
  6962. Title = LoadResourceString(Captions[Type]);
  6963. if (Terminal != NULL)
  6964. {
  6965. TManagedTerminal * Session = DebugNotNull(dynamic_cast<TManagedTerminal *>(Terminal));
  6966. Title = TTerminalManager::Instance()->GetSessionTitle(Session, true) + TitleSeparator + Title;
  6967. }
  6968. }
  6969. if (Seconds == 0)
  6970. {
  6971. Seconds = WinConfiguration->NotificationsTimeout;
  6972. }
  6973. FTrayIcon->PopupBalloon(Title, Message, Type, Seconds * MSecsPerSec, OnBalloonClick, UserData);
  6974. }
  6975. }
  6976. //---------------------------------------------------------------------------
  6977. unsigned int __fastcall TCustomScpExplorerForm::MoreMessageDialog(const UnicodeString Message,
  6978. TStrings * MoreMessages, TQueryType Type, unsigned int Answers,
  6979. UnicodeString HelpKeyword, const TMessageParams * Params,
  6980. TTerminal * Terminal)
  6981. {
  6982. if (((WinConfiguration->ContinueOnError && (FErrorList != NULL)) ||
  6983. (FOnFeedSynchronizeError != NULL)) &&
  6984. (Params != NULL) && (Params->Params & mpAllowContinueOnError) )
  6985. {
  6986. if (FOnFeedSynchronizeError != NULL)
  6987. {
  6988. FOnFeedSynchronizeError(Message, MoreMessages, Type, HelpKeyword);
  6989. }
  6990. else
  6991. {
  6992. DebugAssert(FErrorList != NULL);
  6993. TStringList * MoreMessagesCopy = NULL;
  6994. if (MoreMessages)
  6995. {
  6996. MoreMessagesCopy = new TStringList();
  6997. MoreMessagesCopy->Assign(MoreMessages);
  6998. }
  6999. FErrorList->AddObject(Message, MoreMessagesCopy);
  7000. }
  7001. PopupTrayBalloon(Terminal, Message, Type);
  7002. return ContinueAnswer(Answers);
  7003. }
  7004. else
  7005. {
  7006. bool UseBalloon = FTrayIcon->Visible;
  7007. if (UseBalloon)
  7008. {
  7009. PopupTrayBalloon(Terminal, Message, Type);
  7010. }
  7011. unsigned int Result;
  7012. try
  7013. {
  7014. Result = ::MoreMessageDialog(Message, MoreMessages, Type, Answers, HelpKeyword, Params);
  7015. }
  7016. __finally
  7017. {
  7018. // cancel only balloon we popped up, otherwise we may cancel notification
  7019. // balloon that was there before the message dialog
  7020. // (such as "dd confirmation opt in balloon)
  7021. if (UseBalloon)
  7022. {
  7023. FTrayIcon->CancelBalloon();
  7024. }
  7025. }
  7026. return Result;
  7027. }
  7028. }
  7029. //---------------------------------------------------------------------------
  7030. void __fastcall TCustomScpExplorerForm::ShowExtendedException(
  7031. TTerminal * Terminal, Exception * E)
  7032. {
  7033. if (FTrayIcon->Visible)
  7034. {
  7035. PopupTrayBalloon(Terminal, L"", qtError, E);
  7036. }
  7037. // particularly prevent opening new session from jump list,
  7038. // while exception is shown
  7039. NonVisualDataModule->StartBusy();
  7040. try
  7041. {
  7042. ShowExtendedExceptionEx(Terminal, E);
  7043. }
  7044. __finally
  7045. {
  7046. NonVisualDataModule->EndBusy();
  7047. FTrayIcon->CancelBalloon();
  7048. }
  7049. }
  7050. //---------------------------------------------------------------------------
  7051. void __fastcall TCustomScpExplorerForm::SessionReady()
  7052. {
  7053. InitStatusBar();
  7054. // cannot rely on active page being page for active terminal,
  7055. // as it can happen that active page is the "new session" page
  7056. // (e.g. when reconnecting active terminal, while login dialog
  7057. // invoked from "new session" page is modal)
  7058. int ActiveSessionIndex = TTerminalManager::Instance()->ActiveSessionIndex;
  7059. UpdateSessionTab(SessionsPageControl->Pages[ActiveSessionIndex]);
  7060. }
  7061. //---------------------------------------------------------------------------
  7062. void __fastcall TCustomScpExplorerForm::InactiveTerminalException(
  7063. TTerminal * Terminal, Exception * E)
  7064. {
  7065. Notify(Terminal, L"", qtError, false, NULL, NULL, E);
  7066. if (!Terminal->Active)
  7067. {
  7068. int Index = TTerminalManager::Instance()->IndexOf(Terminal);
  7069. if (DebugAlwaysTrue((Index >= 0) && (Index < SessionsPageControl->PageCount)))
  7070. {
  7071. UpdateSessionTab(SessionsPageControl->Pages[Index]);
  7072. }
  7073. }
  7074. }
  7075. //---------------------------------------------------------------------------
  7076. void __fastcall TCustomScpExplorerForm::Notify(TTerminal * Terminal,
  7077. UnicodeString Message, TQueryType Type,
  7078. bool Important, TNotifyEvent OnClick, TObject * UserData, Exception * E)
  7079. {
  7080. if ((E == NULL) ||
  7081. ExceptionMessage(E, Message))
  7082. {
  7083. unsigned int Seconds = WinConfiguration->NotificationsTimeout;
  7084. if (Important)
  7085. {
  7086. Seconds *= 5;
  7087. }
  7088. UnicodeString NoteMessage(UnformatMessage(Message));
  7089. if (Terminal != NULL)
  7090. {
  7091. TManagedTerminal * Session = DebugNotNull(dynamic_cast<TManagedTerminal *>(Terminal));
  7092. NoteMessage = FORMAT(L"%s: %s",
  7093. (TTerminalManager::Instance()->GetSessionTitle(Session, true), NoteMessage));
  7094. }
  7095. if (WinConfiguration->BalloonNotifications)
  7096. {
  7097. AddNote(NoteMessage);
  7098. PopupTrayBalloon(Terminal, Message, Type, NULL, Seconds, OnClick, UserData);
  7099. }
  7100. else
  7101. {
  7102. FlashOnBackground();
  7103. PostNote(NoteMessage, Seconds, OnClick, UserData);
  7104. }
  7105. }
  7106. }
  7107. //---------------------------------------------------------------------------
  7108. void __fastcall TCustomScpExplorerForm::QueueEmptyNoteClicked(TObject * Sender)
  7109. {
  7110. RestoreApp();
  7111. TTerminalNoteData * TerminalNoteData = dynamic_cast<TTerminalNoteData *>(Sender);
  7112. if (DebugAlwaysTrue(TerminalNoteData != NULL) &&
  7113. !NonVisualDataModule->Busy)
  7114. {
  7115. TManagedTerminal * Terminal = TerminalNoteData->Terminal;
  7116. TTerminalManager::Instance()->ActiveSession = Terminal;
  7117. if (!ComponentVisible[fcQueueView])
  7118. {
  7119. ToggleQueueVisibility();
  7120. GoToQueue();
  7121. }
  7122. }
  7123. }
  7124. //---------------------------------------------------------------------------
  7125. void __fastcall TCustomScpExplorerForm::QueueEvent(TManagedTerminal * ATerminal,
  7126. TTerminalQueue * /*Queue*/, TQueueEvent Event)
  7127. {
  7128. UnicodeString Message;
  7129. TNotifyEvent OnClick = NULL;
  7130. TObject * UserData = NULL;
  7131. bool QueueInvisible = !ComponentVisible[fcQueueView] || IsApplicationMinimized();
  7132. switch (Event)
  7133. {
  7134. case qeEmptyButMonitored:
  7135. if ((ATerminal != Terminal) || QueueInvisible)
  7136. {
  7137. Message = LoadStr(BALLOON_QUEUE_EMPTY);
  7138. OnClick = QueueEmptyNoteClicked;
  7139. TTerminalNoteData * TerminalNoteData = new TTerminalNoteData();
  7140. TerminalNoteData->Terminal = ATerminal;
  7141. UserData = TerminalNoteData;
  7142. }
  7143. break;
  7144. case qeEmpty:
  7145. OperationComplete(ATerminal->QueueOperationStart);
  7146. break;
  7147. case qePendingUserAction:
  7148. if ((ATerminal != Terminal) ||
  7149. (QueueInvisible && !IsQueueAutoPopup()))
  7150. {
  7151. Message = LoadStr(BALLOON_QUEUE_USER_ACTION);
  7152. }
  7153. break;
  7154. default:
  7155. DebugFail();
  7156. }
  7157. if (!Message.IsEmpty())
  7158. {
  7159. Notify(ATerminal, Message, qtInformation, false, OnClick, UserData);
  7160. }
  7161. }
  7162. //---------------------------------------------------------------------------
  7163. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDCreateDragFileList(
  7164. TObject * /*Sender*/, TFileList * FileList, bool & Created)
  7165. {
  7166. if (FDDExtMapFile != NULL)
  7167. {
  7168. CloseHandle(FDDExtMapFile);
  7169. FDDExtMapFile = NULL;
  7170. }
  7171. if (WinConfiguration->DDFakeFile)
  7172. {
  7173. DDFakeFileInitDrag(FileList, Created);
  7174. }
  7175. }
  7176. //---------------------------------------------------------------------------
  7177. void __fastcall TCustomScpExplorerForm::DDFakeCreated(TObject * /*Sender*/, const UnicodeString FileName)
  7178. {
  7179. if (DebugAlwaysTrue(!FDragFakeDirectory.IsEmpty()) &&
  7180. SameText(ExtractFileName(FileName), ExtractFileName(FDragFakeDirectory)))
  7181. {
  7182. FFakeFileDropTarget = FileName;
  7183. }
  7184. }
  7185. //---------------------------------------------------------------------------
  7186. UnicodeString __fastcall TCustomScpExplorerForm::CreateFakeTransferDirectory()
  7187. {
  7188. UnicodeString Result = ExcludeTrailingBackslash(WinConfiguration->TemporaryDir());
  7189. if (!ForceDirectories(ApiPath(Result)))
  7190. {
  7191. throw Exception(FMTLOAD(CREATE_TEMP_DIR_ERROR, (Result)));
  7192. }
  7193. FileSetAttr(ApiPath(Result), faHidden);
  7194. return Result;
  7195. }
  7196. //---------------------------------------------------------------------------
  7197. void __fastcall TCustomScpExplorerForm::DDFakeFileInitDrag(TFileList * FileList,
  7198. bool & Created)
  7199. {
  7200. FFakeFileDropTarget = UnicodeString();
  7201. FDragFakeDirectory = CreateFakeTransferDirectory();
  7202. FileList->AddItem(NULL, FDragFakeDirectory);
  7203. Created = true;
  7204. if (!WinConfiguration->IsDDExtRunning() || WinConfiguration->IsDDExtBroken())
  7205. {
  7206. FDragFakeMonitors = StartCreationDirectoryMonitorsOnEachDrive(FILE_NOTIFY_CHANGE_DIR_NAME, DDFakeCreated);
  7207. }
  7208. FDDExtMapFile = CreateFileMappingA((HANDLE)0xFFFFFFFF, NULL, PAGE_READWRITE,
  7209. 0, sizeof(TDragExtCommStruct), AnsiString(DRAG_EXT_MAPPING).c_str());
  7210. {
  7211. TMutexGuard Guard(FDDExtMutex, DRAGEXT_MUTEX_RELEASE_TIMEOUT);
  7212. TDragExtCommStruct* CommStruct;
  7213. CommStruct = static_cast<TDragExtCommStruct*>(MapViewOfFile(FDDExtMapFile,
  7214. FILE_MAP_ALL_ACCESS, 0, 0, 0));
  7215. DebugAssert(CommStruct != NULL);
  7216. CommStruct->Version = TDragExtCommStruct::CurrentVersion;
  7217. CommStruct->Dragging = true;
  7218. wcsncpy(CommStruct->DropDest, FDragFakeDirectory.c_str(),
  7219. LENOF(CommStruct->DropDest));
  7220. NULL_TERMINATE(CommStruct->DropDest);
  7221. UnmapViewOfFile(CommStruct);
  7222. }
  7223. }
  7224. //---------------------------------------------------------------------------
  7225. bool __fastcall TCustomScpExplorerForm::RemoteFileControlFileOperation(
  7226. TObject * Sender, TFileOperation Operation, bool NoConfirmation, void * Param)
  7227. {
  7228. bool Result;
  7229. DebugAssert(!IsLocalBrowserMode());
  7230. if (Sender == RemoteDirView)
  7231. {
  7232. Result = ExecuteFileOperation(Operation, osRemote, true, NoConfirmation, Param);
  7233. }
  7234. else
  7235. {
  7236. DebugAssert(Sender == RemoteDriveView);
  7237. TStrings * FileList = RemoteDriveView->DragFileList();
  7238. try
  7239. {
  7240. Result = ExecuteFileOperation(Operation, osRemote, FileList, NoConfirmation, Param);
  7241. }
  7242. __finally
  7243. {
  7244. delete FileList;
  7245. }
  7246. }
  7247. if (FDDTargetControl == RemoteDriveView)
  7248. {
  7249. RemoteDriveView->UpdateDropTarget();
  7250. }
  7251. // foRemoteMove happens when file/dir is dragged within the remote tree view
  7252. // or from tree view to dir view.
  7253. // foMove happens when file/dir is dragged from remote tree view outside of
  7254. // application via dragex
  7255. if (((Operation == foRemoteMove) || (Operation == foMove)) &&
  7256. (DropSourceControl == RemoteDriveView))
  7257. {
  7258. RemoteDriveView->UpdateDropSource();
  7259. }
  7260. return Result;
  7261. }
  7262. //---------------------------------------------------------------------------
  7263. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDEnd(TObject * Sender)
  7264. {
  7265. DebugAssert(!IsLocalBrowserMode());
  7266. // This also handles drops of remote files to queue.
  7267. // Drops of local files (uploads) are handled in QueueDDProcessDropped.
  7268. SAFE_DESTROY(FDDFileList);
  7269. if (!FFakeFileDropTarget.IsEmpty())
  7270. {
  7271. RemoveDir(ApiPath(FFakeFileDropTarget));
  7272. }
  7273. if ((FDDExtMapFile != NULL) || (FDDTargetControl == QueueView3))
  7274. {
  7275. try
  7276. {
  7277. TDragResult DDResult = (Sender == RemoteDirView) ?
  7278. RemoteDirView->LastDDResult : RemoteDriveView->LastDDResult;
  7279. // Focus is moved to the target application,
  7280. // but as we are going to present the UI, we need to steal the focus back.
  7281. // This most likely won't work though (windows does not allow application
  7282. // to steal focus most of the time)
  7283. Application->BringToFront();
  7284. // On older version of Windows we never got drMove here, see also comment below.
  7285. // On Windows 10, we get the "move".
  7286. if ((DDResult == drCopy) || (DDResult == drMove) || (DDResult == drInvalid))
  7287. {
  7288. UnicodeString TargetDirectory;
  7289. TFileOperation Operation;
  7290. // drInvalid may mean drMove, see comment below
  7291. switch (DDResult)
  7292. {
  7293. case drCopy:
  7294. Operation = foCopy;
  7295. break;
  7296. case drMove:
  7297. Operation = foMove;
  7298. break;
  7299. default:
  7300. DebugFail();
  7301. case drInvalid:
  7302. // prefer "copy" for safety
  7303. Operation = FLAGSET(FLastDropEffect, DROPEFFECT_MOVE) ? foMove : foCopy;
  7304. break;
  7305. }
  7306. TTransferOperationParam Param;
  7307. UnicodeString CounterName;
  7308. bool ForceQueue;
  7309. if (!DDGetTarget(Param.TargetDirectory, ForceQueue, CounterName))
  7310. {
  7311. // we get drInvalid both if d&d was intercepted by ddext,
  7312. // and when users drops on no-drop location.
  7313. // we tell the difference by existence of response from ddext,
  7314. // so we ignore absence of response in this case
  7315. if (DDResult != drInvalid)
  7316. {
  7317. // here we know that the extension is installed,
  7318. // as it is checked as soon as drag&drop starts from
  7319. // RemoteFileControlDDCreateDragFileList
  7320. Configuration->Usage->Inc(L"DownloadsDragDropExternalExtTargetUnknown");
  7321. if (!WinConfiguration->DDExtInstalled || WinConfiguration->IsDDExtBroken())
  7322. {
  7323. throw ExtException(NULL, LoadStr(DD_TARGET_UNKNOWN), HELP_DD_TARGET_UNKNOWN);
  7324. }
  7325. else
  7326. {
  7327. throw ExtException(NULL, LoadStr(DRAGEXT_TARGET_UNKNOWN2), HELP_DRAGEXT_TARGET_UNKNOWN);
  7328. }
  7329. }
  7330. }
  7331. else
  7332. {
  7333. // download using fake file
  7334. Param.Temp = false;
  7335. Param.DragDrop = true;
  7336. if (ForceQueue)
  7337. {
  7338. Param.Queue = asOn;
  7339. }
  7340. if (Sender == RemoteDirView)
  7341. {
  7342. Param.Options = FLAGMASK(SelectedAllFilesInDirView(RemoteDirView), coAllFiles);
  7343. }
  7344. if (RemoteFileControlFileOperation(Sender, Operation,
  7345. (WinConfiguration->DDTransferConfirmation == asOff), &Param))
  7346. {
  7347. Configuration->Usage->Inc(CounterName);
  7348. }
  7349. }
  7350. }
  7351. }
  7352. __finally
  7353. {
  7354. CloseHandle(FDDExtMapFile);
  7355. FDDExtMapFile = NULL;
  7356. delete FDragFakeMonitors;
  7357. FDragFakeMonitors = NULL;
  7358. RemoveDir(ApiPath(FDragFakeDirectory));
  7359. FDragFakeDirectory = L"";
  7360. FFakeFileDropTarget = UnicodeString();
  7361. }
  7362. }
  7363. if (FDragDropSshTerminate.get() != NULL)
  7364. {
  7365. FDragDropSshTerminate->Rethrow();
  7366. }
  7367. }
  7368. //---------------------------------------------------------------------------
  7369. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDGiveFeedback(
  7370. TObject * /*Sender*/, int dwEffect, HRESULT & /*Result*/)
  7371. {
  7372. // Remember drop effect so we know (when user drops files), if we copy or move
  7373. FLastDropEffect = dwEffect;
  7374. }
  7375. //---------------------------------------------------------------------------
  7376. bool __fastcall TCustomScpExplorerForm::DDGetTarget(
  7377. UnicodeString & Directory, bool & ForceQueue, UnicodeString & CounterName)
  7378. {
  7379. bool Result;
  7380. if (FDDTargetControl == QueueView3)
  7381. {
  7382. Directory = DefaultDownloadTargetDirectory();
  7383. Result = true;
  7384. CounterName = L"DownloadsDragDropQueue";
  7385. ForceQueue = true;
  7386. }
  7387. else if (!FFakeFileDropTarget.IsEmpty())
  7388. {
  7389. Directory = ExcludeTrailingBackslash(ExtractFilePath(FFakeFileDropTarget));
  7390. Result = true;
  7391. ForceQueue = false;
  7392. CounterName = L"DownloadsDragDropFakeFile";
  7393. }
  7394. else
  7395. {
  7396. ForceQueue = false;
  7397. Enabled = false;
  7398. try
  7399. {
  7400. int Timer = 0;
  7401. Result = false;
  7402. while (!Result && (Timer < WinConfiguration->DDExtTimeout))
  7403. {
  7404. {
  7405. TMutexGuard Guard(FDDExtMutex, DRAGEXT_MUTEX_RELEASE_TIMEOUT);
  7406. TDragExtCommStruct* CommStruct;
  7407. CommStruct = static_cast<TDragExtCommStruct*>(MapViewOfFile(FDDExtMapFile,
  7408. FILE_MAP_ALL_ACCESS, 0, 0, 0));
  7409. DebugAssert(CommStruct != NULL);
  7410. Result = !CommStruct->Dragging;
  7411. if (Result)
  7412. {
  7413. Directory = ExtractFilePath(CommStruct->DropDest);
  7414. CounterName = L"DownloadsDragDropExternalExt";
  7415. }
  7416. UnmapViewOfFile(CommStruct);
  7417. }
  7418. if (!Result)
  7419. {
  7420. Sleep(50);
  7421. Timer += 50;
  7422. Application->ProcessMessages();
  7423. }
  7424. }
  7425. }
  7426. __finally
  7427. {
  7428. Enabled = true;
  7429. }
  7430. }
  7431. return Result;
  7432. }
  7433. //---------------------------------------------------------------------------
  7434. void __fastcall TCustomScpExplorerForm::AddDelayedDirectoryDeletion(
  7435. const UnicodeString TempDir, int SecDelay)
  7436. {
  7437. TDateTime Alarm = IncSecond(Now(), SecDelay);
  7438. FDelayedDeletionList->AddObject(TempDir, reinterpret_cast<TObject*>(Alarm.FileDate()));
  7439. if (FDelayedDeletionTimer == NULL)
  7440. {
  7441. DebugAssert(HandleAllocated());
  7442. FDelayedDeletionTimer = new TTimer(this);
  7443. FDelayedDeletionTimer->Interval = 10000;
  7444. FDelayedDeletionTimer->OnTimer = DoDelayedDeletion;
  7445. }
  7446. else
  7447. {
  7448. FDelayedDeletionTimer->Enabled = true;
  7449. }
  7450. }
  7451. //---------------------------------------------------------------------------
  7452. void __fastcall TCustomScpExplorerForm::DoDelayedDeletion(TObject * Sender)
  7453. {
  7454. DebugAssert(FDelayedDeletionList != NULL);
  7455. TDateTime N = Now();
  7456. TDateTime Alert;
  7457. UnicodeString Directory;
  7458. for (int Index = FDelayedDeletionList->Count-1; Index >= 0; Index--)
  7459. {
  7460. Alert = FileDateToDateTime(reinterpret_cast<int>(FDelayedDeletionList->Objects[Index]));
  7461. if ((N >= Alert) || (Sender == NULL))
  7462. {
  7463. Directory = FDelayedDeletionList->Strings[Index];
  7464. if (DeleteDirectory(ExcludeTrailingBackslash(Directory)))
  7465. {
  7466. FDelayedDeletionList->Delete(Index);
  7467. }
  7468. }
  7469. }
  7470. if (FDelayedDeletionList->Count == 0)
  7471. {
  7472. FDelayedDeletionTimer->Enabled = false;
  7473. }
  7474. }
  7475. //---------------------------------------------------------------------------
  7476. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDTargetDrop()
  7477. {
  7478. DebugAssert(!IsLocalBrowserMode());
  7479. if (IsFileControl(FDDTargetControl, osRemote) ||
  7480. (FDDTargetControl == SessionsPageControl))
  7481. {
  7482. TManagedTerminal * TargetTerminal = NULL;
  7483. TFileOperation Operation = foNone;
  7484. if (FDDTargetControl == SessionsPageControl)
  7485. {
  7486. TPoint Point = SessionsPageControl->ScreenToClient(Mouse->CursorPos);
  7487. int Index = SessionsPageControl->IndexOfTabAt(Point.X, Point.Y);
  7488. // do not allow dropping on local-local, disconnected and the "+" tab (see also SessionsDDProcessDropped)
  7489. TargetTerminal = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  7490. if (IsActiveTerminal(TargetTerminal) && DebugAlwaysTrue(!TargetTerminal->LocalBrowser))
  7491. {
  7492. if ((FLastDropEffect == DROPEFFECT_MOVE) &&
  7493. (TargetTerminal == TTerminalManager::Instance()->ActiveTerminal))
  7494. {
  7495. Operation = foRemoteMove;
  7496. }
  7497. else
  7498. {
  7499. Operation = foRemoteCopy;
  7500. }
  7501. }
  7502. }
  7503. else
  7504. {
  7505. if (FLastDropEffect == DROPEFFECT_MOVE)
  7506. {
  7507. Operation = foRemoteMove;
  7508. }
  7509. else if (FLastDropEffect == DROPEFFECT_COPY)
  7510. {
  7511. Operation = foRemoteCopy;
  7512. }
  7513. }
  7514. if (Operation != foNone)
  7515. {
  7516. RemoteFileControlFileOperation(DropSourceControl,
  7517. Operation, (WinConfiguration->DDTransferConfirmation == asOff), TargetTerminal);
  7518. }
  7519. // abort drag&drop
  7520. Abort();
  7521. }
  7522. else if ((FDDExtMapFile == NULL) && (FLastDropEffect != DROPEFFECT_NONE) &&
  7523. // Drops of remote files to queue are handled in RemoteFileControlDDEnd
  7524. (FDDTargetControl != QueueView3))
  7525. {
  7526. DebugAssert(!FDragTempDir.IsEmpty());
  7527. TTransferType Type;
  7528. UnicodeString TempDir = FDragTempDir;
  7529. // We clear FDragTempDir before calling
  7530. // just in case it fail (raises exception)
  7531. FDragTempDir = L"";
  7532. Type = (FLastDropEffect & DROPEFFECT_MOVE ? ttMove : Type = ttCopy);
  7533. TGUICopyParamType CopyParams = GUIConfiguration->CurrentCopyParam;
  7534. // empty directory parameter means temp directory -> don't display it!
  7535. UnicodeString TargetDir = L"";
  7536. int Options = 0;
  7537. if (!CopyParamDialog(tdToLocal, Type, true, FDDFileList,
  7538. TargetDir, CopyParams, (WinConfiguration->DDTransferConfirmation != asOff), true, Options))
  7539. {
  7540. Abort();
  7541. }
  7542. // TargetDir is set when dropped on local file control
  7543. // (this was workaround for legacy dirview event handling, now it should be
  7544. // made prettier)
  7545. if (TargetDir.IsEmpty())
  7546. {
  7547. TargetDir = TempDir;
  7548. if (ForceDirectories(ApiPath(TargetDir)))
  7549. {
  7550. DebugAssert(Terminal && !TargetDir.IsEmpty());
  7551. FPendingTempSpaceWarn = true;
  7552. try
  7553. {
  7554. FDragDropOperation = true;
  7555. int Params = cpTemporary |
  7556. (Type == ttMove ? cpDelete : 0);
  7557. DDDownload(FDDFileList, TargetDir, &CopyParams, Params);
  7558. Configuration->Usage->Inc(L"DownloadsDragDropExternalTemp");
  7559. }
  7560. __finally
  7561. {
  7562. FDragDropOperation = false;
  7563. FPendingTempSpaceWarn = false;
  7564. AddDelayedDirectoryDeletion(TargetDir, WinConfiguration->DDDeleteDelay);
  7565. }
  7566. }
  7567. else
  7568. {
  7569. throw Exception(FMTLOAD(CREATE_TEMP_DIR_ERROR, (TargetDir)));
  7570. }
  7571. }
  7572. }
  7573. }
  7574. //---------------------------------------------------------------------------
  7575. void __fastcall TCustomScpExplorerForm::DDDownload(
  7576. TStrings * FilesToCopy, const UnicodeString & TargetDir, TCopyParamType * CopyParam, int Params)
  7577. {
  7578. DebugAssert(!IsLocalBrowserMode());
  7579. TAutoBatch AutoBatch(this);
  7580. CopyParam->IncludeFileMask.SetRoots(TargetDir, FilesToCopy);
  7581. UpdateCopyParamCounters(*CopyParam);
  7582. Terminal->CopyToLocal(FilesToCopy, TargetDir, CopyParam, Params, NULL);
  7583. if (FLAGSET(Params, cpDelete) && (DropSourceControl == RemoteDriveView))
  7584. {
  7585. RemoteDriveView->UpdateDropSource();
  7586. }
  7587. }
  7588. //---------------------------------------------------------------------------
  7589. class TFakeDataObjectFilesEx : public TDataObjectFilesEx
  7590. {
  7591. public:
  7592. __fastcall TFakeDataObjectFilesEx(TFileList * AFileList, bool RenderPIDL,
  7593. bool RenderFilename) : TDataObjectFilesEx(AFileList, RenderPIDL, RenderFilename, true)
  7594. {
  7595. }
  7596. virtual bool __fastcall AllowData(const tagFORMATETC & FormatEtc)
  7597. {
  7598. return (FormatEtc.cfFormat == CF_HDROP) ? false :
  7599. TDataObjectFilesEx::AllowData(FormatEtc);
  7600. }
  7601. };
  7602. //---------------------------------------------------------------------------
  7603. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDCreateDataObject(
  7604. TObject * Sender, TDataObject *& DataObject)
  7605. {
  7606. if (FDDExtMapFile != NULL)
  7607. {
  7608. TFileList * FileList = DragDropFiles(Sender)->FileList;
  7609. if (!FileList->RenderPIDLs() || !FileList->RenderNames())
  7610. {
  7611. Abort();
  7612. }
  7613. if (FileList->Count > 0)
  7614. {
  7615. TDataObjectFilesEx * FilesObject = new TFakeDataObjectFilesEx(FileList, true, true);
  7616. if (!FilesObject->IsValid(true, true))
  7617. {
  7618. FilesObject->_Release();
  7619. }
  7620. else
  7621. {
  7622. DataObject = FilesObject;
  7623. }
  7624. }
  7625. }
  7626. }
  7627. //---------------------------------------------------------------------------
  7628. void __fastcall TCustomScpExplorerForm::GoToCommandLine()
  7629. {
  7630. DebugFail();
  7631. }
  7632. //---------------------------------------------------------------------------
  7633. void __fastcall TCustomScpExplorerForm::GoToTree()
  7634. {
  7635. ComponentVisible[fcRemoteTree] = true;
  7636. DriveView(osRemote)->SetFocus();
  7637. }
  7638. //---------------------------------------------------------------------------
  7639. TStrings * __fastcall TCustomScpExplorerForm::PanelExport(TOperationSide Side,
  7640. TPanelExport Export)
  7641. {
  7642. TCustomDirView * DirView = this->DirView(Side);
  7643. std::unique_ptr<TStrings> ExportData(new TStringList());
  7644. switch (Export)
  7645. {
  7646. case pePath:
  7647. ExportData->Add(DirView->PathName);
  7648. break;
  7649. case peFileList:
  7650. case peFullFileList:
  7651. {
  7652. bool FullPath = (Export == peFullFileList);
  7653. DirView->CreateFileList(false, FullPath, ExportData.get());
  7654. for (int Index = 0; Index < ExportData->Count; Index++)
  7655. {
  7656. if (ExportData->Strings[Index].Pos(L" ") > 0)
  7657. {
  7658. ExportData->Strings[Index] = FORMAT(L"\"%s\"", (ExportData->Strings[Index]));
  7659. }
  7660. }
  7661. }
  7662. break;
  7663. default:
  7664. DebugFail();
  7665. }
  7666. return ExportData.release();
  7667. }
  7668. //---------------------------------------------------------------------------
  7669. void __fastcall TCustomScpExplorerForm::PanelExport(TOperationSide Side,
  7670. TPanelExport Export, TPanelExportDestination Destination)
  7671. {
  7672. std::unique_ptr<TStrings> ExportData(PanelExport(Side, Export));
  7673. PanelExportStore(Side, Export, Destination, ExportData.get());
  7674. }
  7675. //---------------------------------------------------------------------------
  7676. void __fastcall TCustomScpExplorerForm::PanelExportStore(TOperationSide /*Side*/,
  7677. TPanelExport /*Export*/, TPanelExportDestination Destination,
  7678. TStrings * ExportData)
  7679. {
  7680. if (Destination == pedClipboard)
  7681. {
  7682. TInstantOperationVisualizer Visualizer;
  7683. CopyToClipboard(ExportData);
  7684. }
  7685. else
  7686. {
  7687. DebugFail();
  7688. }
  7689. }
  7690. //---------------------------------------------------------------------------
  7691. void __fastcall TCustomScpExplorerForm::Filter(TOperationSide Side)
  7692. {
  7693. TCustomDirView * DirView = this->DirView(Side);
  7694. UnicodeString Mask = DirView->Mask;
  7695. if (DoFilterMaskDialog(DirView, Mask))
  7696. {
  7697. DirView->Mask = TFileMasks::NormalizeMask(Mask);
  7698. Configuration->Usage->Inc(L"Filters");
  7699. }
  7700. }
  7701. //---------------------------------------------------------------------------
  7702. TQueueOperation __fastcall TCustomScpExplorerForm::DefaultQueueOperation()
  7703. {
  7704. return FQueueController->DefaultOperation();
  7705. }
  7706. //---------------------------------------------------------------------------
  7707. bool __fastcall TCustomScpExplorerForm::AllowQueueOperation(
  7708. TQueueOperation Operation, void ** Param)
  7709. {
  7710. switch (Operation)
  7711. {
  7712. case qoPreferences:
  7713. return true;
  7714. case qoGoTo:
  7715. return ComponentVisible[fcQueueView] && QueueView3->Enabled;
  7716. case qoOnceEmpty:
  7717. return IsAnythingQueued();
  7718. default:
  7719. return FQueueController->AllowOperation(Operation, Param);
  7720. }
  7721. }
  7722. //---------------------------------------------------------------------------
  7723. void __fastcall TCustomScpExplorerForm::GoToQueue()
  7724. {
  7725. if (DebugAlwaysTrue(QueueView3->Visible))
  7726. {
  7727. QueueView3->SetFocus();
  7728. }
  7729. }
  7730. //---------------------------------------------------------------------------
  7731. void __fastcall TCustomScpExplorerForm::ExecuteQueueOperation(
  7732. TQueueOperation Operation, void * Param)
  7733. {
  7734. if (Operation == qoGoTo)
  7735. {
  7736. GoToQueue();
  7737. }
  7738. else if (Operation == qoPreferences)
  7739. {
  7740. PreferencesDialog(pmQueue);
  7741. }
  7742. else
  7743. {
  7744. FQueueController->ExecuteOperation(Operation, Param);
  7745. }
  7746. }
  7747. //---------------------------------------------------------------------------
  7748. bool __fastcall TCustomScpExplorerForm::GetQueueEnabled()
  7749. {
  7750. return (Queue != NULL) && Queue->Enabled;
  7751. }
  7752. //---------------------------------------------------------------------------
  7753. void __fastcall TCustomScpExplorerForm::ToggleQueueEnabled()
  7754. {
  7755. DebugAssert(Queue != NULL);
  7756. if (Queue != NULL)
  7757. {
  7758. Queue->Enabled = !Queue->Enabled;
  7759. }
  7760. }
  7761. //---------------------------------------------------------------------------
  7762. void __fastcall TCustomScpExplorerForm::QueueView3ContextPopup(
  7763. TObject * /*Sender*/, TPoint & /*MousePos*/, bool & /*Handled*/)
  7764. {
  7765. FQueueActedItem = QueueView3->ItemFocused;
  7766. }
  7767. //---------------------------------------------------------------------------
  7768. /*virtual*/ int __fastcall TCustomScpExplorerForm::GetStaticComponentsHeight()
  7769. {
  7770. return TopDock->Height + QueueSplitter->Height;
  7771. }
  7772. //---------------------------------------------------------------------------
  7773. int __fastcall TCustomScpExplorerForm::GetStaticQueuePanelComponentsHeight()
  7774. {
  7775. return
  7776. (QueueFileListSplitter->Visible ? QueueFileListSplitter->Height : 0) +
  7777. (QueueDock->Visible ? QueueDock->Height : 0) +
  7778. (QueueLabel->Visible ? QueueLabel->Height : 0);
  7779. }
  7780. //---------------------------------------------------------------------------
  7781. int __fastcall TCustomScpExplorerForm::GetMinQueueViewHeight()
  7782. {
  7783. return ScaleByTextHeight(this, 48);
  7784. }
  7785. //---------------------------------------------------------------------------
  7786. void __fastcall TCustomScpExplorerForm::QueueSplitterCanResize(
  7787. TObject * /*Sender*/, int & NewSize, bool & Accept)
  7788. {
  7789. // when queue is hidden by double-clicking splitter, stray attempt to
  7790. // resize the panel with strange value arrives, make sure it is ignored
  7791. if (ComponentVisible[fcQueueView])
  7792. {
  7793. int MaxHeightLimit = ClientHeight - GetStaticComponentsHeight() - RemotePanel->Constraints->MinHeight;
  7794. if (NewSize > MaxHeightLimit)
  7795. {
  7796. NewSize = MaxHeightLimit;
  7797. }
  7798. int MinHeightLimit =
  7799. GetStaticQueuePanelComponentsHeight() +
  7800. (QueueFileList->Visible ? QueueFileList->Height : 0) +
  7801. GetMinQueueViewHeight();
  7802. if (NewSize < MinHeightLimit)
  7803. {
  7804. NewSize = MinHeightLimit;
  7805. }
  7806. }
  7807. else
  7808. {
  7809. Accept = false;
  7810. }
  7811. }
  7812. //---------------------------------------------------------------------------
  7813. void __fastcall TCustomScpExplorerForm::QueueFileListSplitterCanResize(TObject *, int & NewSize, bool &)
  7814. {
  7815. int TotalHeight = GetStaticQueuePanelComponentsHeight() + NewSize;
  7816. int QueueViewHeight = QueuePanel->ClientHeight - TotalHeight;
  7817. if (QueueViewHeight < GetMinQueueViewHeight())
  7818. {
  7819. NewSize -= (GetMinQueueViewHeight() - QueueViewHeight);
  7820. }
  7821. }
  7822. //---------------------------------------------------------------------------
  7823. void __fastcall TCustomScpExplorerForm::QueueView3StartDrag(TObject * /*Sender*/,
  7824. TDragObject *& /*DragObject*/)
  7825. {
  7826. FQueueActedItem = QueueView3->ItemFocused;
  7827. }
  7828. //---------------------------------------------------------------------------
  7829. void __fastcall TCustomScpExplorerForm::QueueView3DragOver(TObject * /*Sender*/,
  7830. TObject * Source, int X, int Y, TDragState /*State*/, bool & Accept)
  7831. {
  7832. Accept = true;
  7833. if (Source == QueueView3)
  7834. {
  7835. TListItem * DropTarget = QueueView3->GetItemAt(X, Y);
  7836. Accept = (DropTarget != NULL) && (FQueueActedItem != NULL);
  7837. if (Accept)
  7838. {
  7839. TQueueItemProxy * QueueItem;
  7840. TQueueItemProxy * DestQueueItem;
  7841. QueueItem = static_cast<TQueueItemProxy *>(FQueueActedItem->Data);
  7842. DestQueueItem = static_cast<TQueueItemProxy *>(DropTarget->Data);
  7843. Accept = (QueueItem != DestQueueItem) &&
  7844. (QueueItem->Status == TQueueItem::qsPending) &&
  7845. (DestQueueItem->Status == TQueueItem::qsPending);
  7846. }
  7847. }
  7848. }
  7849. //---------------------------------------------------------------------------
  7850. void __fastcall TCustomScpExplorerForm::QueueView3DragDrop(TObject * /*Sender*/,
  7851. TObject * /*Source*/, int /*X*/, int /*Y*/)
  7852. {
  7853. if ((FQueueActedItem != NULL) && (QueueView3->DropTarget != NULL))
  7854. {
  7855. TQueueItemProxy * QueueItem;
  7856. TQueueItemProxy * DestQueueItem;
  7857. QueueItem = static_cast<TQueueItemProxy *>(FQueueActedItem->Data);
  7858. DestQueueItem = static_cast<TQueueItemProxy *>(QueueView3->DropTarget->Data);
  7859. QueueItem->Move(DestQueueItem);
  7860. }
  7861. }
  7862. //---------------------------------------------------------------------------
  7863. void __fastcall TCustomScpExplorerForm::QueueView3Enter(TObject * /*Sender*/)
  7864. {
  7865. if ((QueueView3->ItemFocused == NULL) &&
  7866. (QueueView3->Items->Count > 0))
  7867. {
  7868. QueueView3->ItemFocused = QueueView3->Items->Item[0];
  7869. }
  7870. QueueLabelUpdateStatus();
  7871. }
  7872. //---------------------------------------------------------------------------
  7873. void __fastcall TCustomScpExplorerForm::QueueView3Exit(TObject * /*Sender*/)
  7874. {
  7875. QueueLabelUpdateStatus();
  7876. }
  7877. //---------------------------------------------------------------------------
  7878. void __fastcall TCustomScpExplorerForm::QueueLabelUpdateStatus()
  7879. {
  7880. QueueLabel->UpdateStatus();
  7881. }
  7882. //---------------------------------------------------------------------------
  7883. void __fastcall TCustomScpExplorerForm::QueueView3SelectItem(
  7884. TObject * /*Sender*/, TListItem * /*Item*/, bool Selected)
  7885. {
  7886. if (Selected)
  7887. {
  7888. NonVisualDataModule->UpdateNonVisibleActions();
  7889. }
  7890. }
  7891. //---------------------------------------------------------------------------
  7892. TDragDropFilesEx * __fastcall TCustomScpExplorerForm::DragDropFiles(TObject * Sender)
  7893. {
  7894. TDragDropFilesEx * Result = NULL;
  7895. if (Sender == SessionsPageControl)
  7896. {
  7897. Result = FSessionsDragDropFilesEx;
  7898. }
  7899. else if (Sender == QueueView3)
  7900. {
  7901. Result = FQueueDragDropFilesEx;
  7902. }
  7903. else
  7904. {
  7905. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  7906. if (DirView != NULL)
  7907. {
  7908. Result = DirView->DragDropFilesEx;
  7909. }
  7910. else
  7911. {
  7912. TCustomDriveView * DriveView = dynamic_cast<TCustomDriveView *>(Sender);
  7913. if (DriveView != NULL)
  7914. {
  7915. Result = DriveView->DragDropFilesEx;
  7916. }
  7917. }
  7918. }
  7919. DebugAssert(Result != NULL);
  7920. return Result;
  7921. }
  7922. //---------------------------------------------------------------------------
  7923. bool __fastcall TCustomScpExplorerForm::SelectedAllFilesInDirView(TCustomDirView * DView)
  7924. {
  7925. return (DView->SelCount == DView->FilesCount);
  7926. }
  7927. //---------------------------------------------------------------------------
  7928. bool __fastcall TCustomScpExplorerForm::DraggingAllFilesFromDirView(TOperationSide Side, TStrings * FileList)
  7929. {
  7930. return HasDirView[Side] && (DropSourceControl == DirView(Side)) && (FileList->Count == DirView(Side)->FilesCount);
  7931. }
  7932. //---------------------------------------------------------------------------
  7933. void __fastcall TCustomScpExplorerForm::RemoteFileControlDragDropFileOperation(
  7934. TObject * Sender, int Effect, UnicodeString TargetPath, bool ForceQueue, bool DragDrop)
  7935. {
  7936. DebugAssert(!IsLocalBrowserMode());
  7937. TFileOperation Operation;
  7938. switch (Effect)
  7939. {
  7940. case DROPEFFECT_MOVE:
  7941. Operation = foMove;
  7942. break;
  7943. case DROPEFFECT_COPY:
  7944. // occures on WinXP (reported by user)
  7945. default:
  7946. Operation = foCopy;
  7947. break;
  7948. };
  7949. TDragDropFilesEx * DragDropFilesEx = DragDropFiles(Sender);
  7950. // see a comment in TUnixDirView::PerformItemDragDropOperation
  7951. if (DragDropFilesEx->FileList->Count > 0)
  7952. {
  7953. TStrings * FileList = new TStringList();
  7954. try
  7955. {
  7956. for (int Index = 0; Index < DragDropFilesEx->FileList->Count; Index++)
  7957. {
  7958. FileList->Add(DragDropFilesEx->FileList->Items[Index]->Name);
  7959. }
  7960. FDragDropOperation = true;
  7961. TTransferOperationParam Param;
  7962. Param.TargetDirectory = TargetPath;
  7963. // upload, no temp dirs
  7964. Param.Temp = false;
  7965. Param.DragDrop = DragDrop;
  7966. Param.Options =
  7967. FLAGMASK(DraggingAllFilesFromDirView(osLocal, FileList), coAllFiles);
  7968. if (ForceQueue)
  7969. {
  7970. Param.Queue = asOn;
  7971. }
  7972. bool NoConfirmation = DragDrop ? (WinConfiguration->DDTransferConfirmation == asOff) : false;
  7973. if (ExecuteFileOperation(Operation, osLocal, FileList, NoConfirmation, &Param))
  7974. {
  7975. if (IsFileControl(DropSourceControl, osLocal))
  7976. {
  7977. Configuration->Usage->Inc(L"UploadsDragDropInternal");
  7978. }
  7979. else
  7980. {
  7981. Configuration->Usage->Inc(L"UploadsDragDropExternal");
  7982. }
  7983. }
  7984. }
  7985. __finally
  7986. {
  7987. FDragDropOperation = false;
  7988. delete FileList;
  7989. }
  7990. }
  7991. }
  7992. //---------------------------------------------------------------------------
  7993. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDFileOperation(
  7994. TObject * Sender, int Effect, UnicodeString /*SourcePath*/,
  7995. UnicodeString TargetPath, bool Paste, bool & /*DoOperation*/)
  7996. {
  7997. RemoteFileControlDragDropFileOperation(Sender, Effect, TargetPath, false, !Paste);
  7998. }
  7999. //---------------------------------------------------------------------------
  8000. void __fastcall TCustomScpExplorerForm::RemoteFileContolDDChooseEffect(
  8001. TObject * Sender, int grfKeyState, int & dwEffect)
  8002. {
  8003. DebugAssert(!IsLocalBrowserMode());
  8004. // if any drop effect is allowed at all (e.g. no drop to self and drop to parent)
  8005. if ((dwEffect != DROPEFFECT_NONE) &&
  8006. IsFileControl(DropSourceControl, osRemote))
  8007. {
  8008. // do not allow drop on remote panel (on free space, still allow drop on directories)
  8009. if ((Sender == RemoteDirView) && (DropSourceControl == RemoteDirView) &&
  8010. (RemoteDirView->DropTarget == NULL))
  8011. {
  8012. dwEffect = DROPEFFECT_NONE;
  8013. }
  8014. else
  8015. {
  8016. if (dwEffect == DROPEFFECT_COPY)
  8017. {
  8018. bool MoveCapable = Terminal->IsCapable[fcRemoteMove];
  8019. // currently we support copying always (at least via temporary directory);
  8020. // remove associated checks once this all proves stable and working
  8021. bool CopyCapable = true;
  8022. // if we do not support neither of operations, there's no discussion
  8023. if (!MoveCapable && !CopyCapable)
  8024. {
  8025. dwEffect = DROPEFFECT_NONE;
  8026. }
  8027. else
  8028. {
  8029. // The default effect inside remote panel is move,
  8030. // unless we do not support it, but support copy
  8031. if (FLAGCLEAR(grfKeyState, MK_CONTROL))
  8032. {
  8033. dwEffect = MoveCapable ? DROPEFFECT_MOVE : DROPEFFECT_COPY;
  8034. }
  8035. else
  8036. {
  8037. // with ctrl-down, we want copy unless it is not supported
  8038. dwEffect = CopyCapable ? DROPEFFECT_COPY : DROPEFFECT_NONE;
  8039. }
  8040. }
  8041. }
  8042. }
  8043. }
  8044. }
  8045. //---------------------------------------------------------------------------
  8046. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDDragFileName(
  8047. TObject * Sender, TRemoteFile * File, UnicodeString & FileName)
  8048. {
  8049. DebugAssert(!IsLocalBrowserMode());
  8050. if (FDDTotalSize >= 0)
  8051. {
  8052. if (File->IsDirectory)
  8053. {
  8054. FDDTotalSize = -1;
  8055. }
  8056. else
  8057. {
  8058. FDDTotalSize += File->Size;
  8059. }
  8060. }
  8061. DebugAssert(!FDragTempDir.IsEmpty());
  8062. // TODO: this is quite ineffective
  8063. // TODO: what if invalid character replacement is disabled?
  8064. FileName = FDragTempDir + GUIConfiguration->CurrentCopyParam.ValidLocalFileName(File->FileName);
  8065. UnicodeString TransferFileName;
  8066. if (Sender == RemoteDriveView)
  8067. {
  8068. TransferFileName = UnixExcludeTrailingBackslash(File->FullFileName);
  8069. }
  8070. else
  8071. {
  8072. TransferFileName = File->FileName;
  8073. }
  8074. FDDFileList->AddObject(TransferFileName, File);
  8075. }
  8076. //---------------------------------------------------------------------------
  8077. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDDragDetect(
  8078. TObject * /*Sender*/, int /*grfKeyState*/, const TPoint & /*DetectStart*/,
  8079. const TPoint & /*Point*/, TDragDetectStatus /*DragStatus*/)
  8080. {
  8081. // sometimes we do not get DDEnd so the list is not released
  8082. SAFE_DESTROY(FDDFileList);
  8083. FDDFileList = new TStringList();
  8084. FDragTempDir = WinConfiguration->TemporaryDir();
  8085. FDDTotalSize = 0;
  8086. FDragDropSshTerminate.reset(NULL);
  8087. }
  8088. //---------------------------------------------------------------------------
  8089. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDQueryContinueDrag(
  8090. TObject * /*Sender*/, BOOL /*FEscapePressed*/, int /*grfKeyState*/,
  8091. HRESULT & Result)
  8092. {
  8093. if (Result == DRAGDROP_S_DROP)
  8094. {
  8095. try
  8096. {
  8097. GlobalDragImageList->HideDragImage();
  8098. try
  8099. {
  8100. RemoteFileControlDDTargetDrop();
  8101. }
  8102. catch(ESshTerminate & E)
  8103. {
  8104. FDragDropSshTerminate.reset(E.Clone());
  8105. }
  8106. }
  8107. catch (Exception &E)
  8108. {
  8109. // If downloading fails we need to cancel drag&drop, otherwise
  8110. // Explorer shows error
  8111. // But by the way exception probably never reach this point as
  8112. // it's catched on way.
  8113. // Fatal exceptions get here (like when opening a secondary shell extension for file duplication fails).
  8114. Result = DRAGDROP_S_CANCEL;
  8115. ShowExtendedException(Terminal, &E);
  8116. }
  8117. }
  8118. }
  8119. //---------------------------------------------------------------------------
  8120. void __fastcall TCustomScpExplorerForm::DirViewMatchMask(
  8121. TObject * /*Sender*/, UnicodeString FileName, bool Directory, __int64 Size,
  8122. TDateTime Modification, UnicodeString Masks, bool & Matches, bool AllowImplicitMatches)
  8123. {
  8124. TFileMasks::TParams MaskParams;
  8125. MaskParams.Size = Size;
  8126. MaskParams.Modification = Modification;
  8127. // this does not re-parse the mask if it is the same as the last time
  8128. FDirViewMatchMask = Masks;
  8129. bool ImplicitMatch;
  8130. Matches =
  8131. // Local and RecurseInclude parameters have no effect as there's no path.
  8132. // This should use potential MatchesFileName overload with ImplicitMatch argument.
  8133. FDirViewMatchMask.Matches(FileName, false, Directory, &MaskParams, true, ImplicitMatch) &&
  8134. (AllowImplicitMatches || !ImplicitMatch);
  8135. }
  8136. //---------------------------------------------------------------------------
  8137. void __fastcall TCustomScpExplorerForm::DirViewGetOverlay(
  8138. TObject * Sender, TListItem * Item, WORD & Indexes)
  8139. {
  8140. TCustomDirView * DirView = reinterpret_cast<TCustomDirView *>(Sender);
  8141. UnicodeString Ext;
  8142. if (dynamic_cast<TUnixDirView *>(DirView) != NULL)
  8143. {
  8144. Ext = UnixExtractFileExt(DirView->ItemFileName(Item));
  8145. }
  8146. else
  8147. {
  8148. Ext = ExtractFileExt(DirView->ItemFileName(Item));
  8149. }
  8150. if (SameText(Ext, Configuration->PartialExt))
  8151. {
  8152. Indexes |= oiPartial;
  8153. }
  8154. }
  8155. //---------------------------------------------------------------------------
  8156. bool __fastcall TCustomScpExplorerForm::CanPasteToDirViewFromClipBoard()
  8157. {
  8158. return
  8159. DirViewEnabled(osCurrent) &&
  8160. DirView(osCurrent)->CanPasteFromClipBoard();
  8161. }
  8162. //---------------------------------------------------------------------------
  8163. bool __fastcall TCustomScpExplorerForm::CanPasteFromClipBoard()
  8164. {
  8165. bool Result = false;
  8166. if (CanPasteToDirViewFromClipBoard())
  8167. {
  8168. Result = true;
  8169. }
  8170. else
  8171. {
  8172. UnicodeString ClipboardText;
  8173. if (NonEmptyTextFromClipboard(ClipboardText) &&
  8174. (ClipboardText.Pos(L"\n") == 0)) // it's already trimmed
  8175. {
  8176. if (StoredSessions->IsUrl(ClipboardText))
  8177. {
  8178. Result = true;
  8179. }
  8180. else
  8181. {
  8182. Result = DirViewEnabled(osCurrent);
  8183. }
  8184. }
  8185. }
  8186. return Result;
  8187. }
  8188. //---------------------------------------------------------------------------
  8189. void __fastcall TCustomScpExplorerForm::PasteFromClipBoard()
  8190. {
  8191. // Pasting to any local panel is handled in TScpCommanderForm::PasteFromClipBoard()
  8192. if (DoesClipboardContainOurFiles())
  8193. {
  8194. if (DebugAlwaysTrue(CanPasteToDirViewFromClipBoard()))
  8195. {
  8196. DebugAssert(!IsLocalBrowserMode());
  8197. TTerminalManager * Manager = TTerminalManager::Instance();
  8198. TTerminal * TargetTerminal = Manager->ActiveTerminal;
  8199. Manager->ActiveSession = FClipboardTerminal;
  8200. ExecuteFileOperation(foRemoteCopy, osRemote, FClipboardFileList.get(), false, TargetTerminal);
  8201. }
  8202. }
  8203. else if (CanPasteToDirViewFromClipBoard())
  8204. {
  8205. DirView(osCurrent)->PasteFromClipBoard();
  8206. }
  8207. else
  8208. {
  8209. UnicodeString ClipboardText;
  8210. if (NonEmptyTextFromClipboard(ClipboardText))
  8211. {
  8212. if (StoredSessions->IsUrl(ClipboardText))
  8213. {
  8214. NewSession(ClipboardText);
  8215. }
  8216. else
  8217. {
  8218. DirView(osCurrent)->Path = ClipboardText;
  8219. }
  8220. }
  8221. }
  8222. }
  8223. //---------------------------------------------------------------------------
  8224. void __fastcall TCustomScpExplorerForm::FileListFromClipboard()
  8225. {
  8226. // TBD
  8227. }
  8228. //---------------------------------------------------------------------------
  8229. void __fastcall TCustomScpExplorerForm::SelectAll(TOperationSide Side, TSelectMode Mode)
  8230. {
  8231. TCustomDirView * ADirView = DirView(Side);
  8232. ADirView->SelectAll(Mode);
  8233. ADirView->SetFocus();
  8234. }
  8235. //---------------------------------------------------------------------------
  8236. void __fastcall TCustomScpExplorerForm::SelectByMask(TOperationSide Side, bool Select)
  8237. {
  8238. TCustomDirView * ADirView = DirView(Side);
  8239. TFileFilter Filter;
  8240. DefaultFileFilter(Filter);
  8241. if (DoSelectMaskDialog(ADirView, Select, Filter))
  8242. {
  8243. Configuration->Usage->Inc(L"MaskSelections");
  8244. ADirView->SelectFiles(Filter, Select);
  8245. ADirView->SetFocus();
  8246. }
  8247. }
  8248. //---------------------------------------------------------------------------
  8249. void __fastcall TCustomScpExplorerForm::RestoreSelectedNames(TOperationSide Side)
  8250. {
  8251. TCustomDirView * ADirView = DirView(Side);
  8252. ADirView->RestoreSelectedNames();
  8253. ADirView->SetFocus();
  8254. }
  8255. //---------------------------------------------------------------------------
  8256. void __fastcall TCustomScpExplorerForm::SelectSameExt(bool Select)
  8257. {
  8258. TCustomDirView * CurrentDirView = DirView(osCurrent);
  8259. if (DebugAlwaysTrue(CurrentDirView->ItemFocused != NULL))
  8260. {
  8261. UnicodeString FileName = CurrentDirView->ItemFileName(CurrentDirView->ItemFocused);
  8262. UnicodeString Ext;
  8263. if (!IsSideLocalBrowser(osCurrent))
  8264. {
  8265. Ext = UnixExtractFileExt(FileName);
  8266. }
  8267. else
  8268. {
  8269. Ext = ExtractFileExt(FileName);
  8270. }
  8271. if (Ext.IsEmpty())
  8272. {
  8273. Ext = L".";
  8274. }
  8275. TFileFilter Filter;
  8276. Filter.Masks = FORMAT(L"*%s", (Ext));
  8277. Filter.Directories = false;
  8278. CurrentDirView->SelectFiles(Filter, Select);
  8279. }
  8280. }
  8281. //---------------------------------------------------------------------------
  8282. UnicodeString __fastcall TCustomScpExplorerForm::FileStatusBarText(
  8283. const TStatusFileInfo & FileInfo, TOperationSide Side)
  8284. {
  8285. UnicodeString Result;
  8286. if (!FIncrementalSearch.IsEmpty() && (Side == GetSide(osCurrent)))
  8287. {
  8288. Result = FormatIncrementalSearchStatus(FIncrementalSearch, FIncrementalSearchHaveNext);
  8289. }
  8290. else if (!IsSideLocalBrowser(Side) && ((Terminal == NULL) || Terminal->Disconnected))
  8291. {
  8292. // noop
  8293. }
  8294. else
  8295. {
  8296. Result =
  8297. FMTLOAD(FILE_INFO_FORMAT,
  8298. (FormatBytes(FileInfo.SelectedSize),
  8299. FormatBytes(FileInfo.FilesSize),
  8300. FormatNumber(FileInfo.SelectedCount),
  8301. FormatNumber(FileInfo.FilesCount)));
  8302. }
  8303. return Result;
  8304. }
  8305. //---------------------------------------------------------------------------
  8306. void __fastcall TCustomScpExplorerForm::FileStatusBarPanelClick(
  8307. TTBXStatusPanel * Panel, TOperationSide Side)
  8308. {
  8309. if (Panel->Index == 1)
  8310. {
  8311. ToggleShowHiddenFiles();
  8312. }
  8313. else if (Panel->Index == 2)
  8314. {
  8315. Filter(Side);
  8316. }
  8317. }
  8318. //---------------------------------------------------------------------------
  8319. void __fastcall TCustomScpExplorerForm::UpdateFileStatusBar(
  8320. TTBXStatusBar * StatusBar, const TStatusFileInfo & FileInfo, TOperationSide Side)
  8321. {
  8322. DebugAssert(!StatusBar->SimplePanel);
  8323. StatusBar->Panels->Items[0]->Caption = FileStatusBarText(FileInfo, Side);
  8324. UpdateFileStatusExtendedPanels(StatusBar, FileInfo);
  8325. }
  8326. //---------------------------------------------------------------------------
  8327. void __fastcall TCustomScpExplorerForm::UpdateFileStatusExtendedPanels(
  8328. TTBXStatusBar * StatusBar, const TStatusFileInfo & FileInfo)
  8329. {
  8330. DebugAssert(StatusBar->Panels->Count >= 3);
  8331. TTBXStatusPanel * HiddenFilesPanel = StatusBar->Panels->Items[1];
  8332. if (FileInfo.HiddenCount > 0)
  8333. {
  8334. HiddenFilesPanel->Caption = FMTLOAD(FILE_INFO_HIDDEN2, (FormatNumber(FileInfo.HiddenCount)));
  8335. HiddenFilesPanel->ViewPriority = 90; // <100 allows hiding panel when it does not fit
  8336. }
  8337. else
  8338. {
  8339. HiddenFilesPanel->ViewPriority = 0;
  8340. // not really necessary, just to cleanup no-longer-valid data
  8341. HiddenFilesPanel->Caption = L"";
  8342. }
  8343. TTBXStatusPanel * FilteredFilesPanel = StatusBar->Panels->Items[2];
  8344. if (FileInfo.FilteredCount > 0)
  8345. {
  8346. FilteredFilesPanel->Caption = FMTLOAD(FILE_INFO_FILTERED2, (FormatNumber(FileInfo.FilteredCount)));
  8347. FilteredFilesPanel->ViewPriority = 90; // <100 allows hiding panel when it does not fit
  8348. }
  8349. else
  8350. {
  8351. FilteredFilesPanel->ViewPriority = 0;
  8352. // not really necessary, just to cleanup no-longer-valid data
  8353. FilteredFilesPanel->Caption = L"";
  8354. }
  8355. }
  8356. //---------------------------------------------------------------------------
  8357. void __fastcall TCustomScpExplorerForm::RemoteStatusBarClick(
  8358. TObject * /*Sender*/)
  8359. {
  8360. if (DirView(osOther)->Enabled)
  8361. {
  8362. DirView(osOther)->SetFocus();
  8363. }
  8364. }
  8365. //---------------------------------------------------------------------------
  8366. void __fastcall TCustomScpExplorerForm::ToggleQueueVisibility()
  8367. {
  8368. TQueueViewConfiguration Config = WinConfiguration->QueueView;
  8369. switch (Config.Show)
  8370. {
  8371. case qvShow:
  8372. if ((FQueueStatus != NULL) && (FQueueStatus->Count > 0))
  8373. {
  8374. Config.Show = qvHide;
  8375. }
  8376. else
  8377. {
  8378. Config.Show = Config.LastHideShow;
  8379. }
  8380. break;
  8381. case qvHideWhenEmpty:
  8382. if (ComponentVisible[fcQueueView])
  8383. {
  8384. Config.Show = qvHide;
  8385. }
  8386. else
  8387. {
  8388. Config.LastHideShow = Config.Show;
  8389. Config.Show = qvShow;
  8390. }
  8391. break;
  8392. case qvHide:
  8393. Config.LastHideShow = Config.Show;
  8394. Config.Show = qvShow;
  8395. break;
  8396. }
  8397. WinConfiguration->QueueView = Config;
  8398. }
  8399. //---------------------------------------------------------------------------
  8400. UnicodeString __fastcall TCustomScpExplorerForm::PathForCaption()
  8401. {
  8402. UnicodeString Result;
  8403. if (Terminal != NULL)
  8404. {
  8405. switch (WinConfiguration->PathInCaption)
  8406. {
  8407. case picShort:
  8408. Result = ExtractShortName(Terminal->CurrentDirectory, true);
  8409. break;
  8410. case picFull:
  8411. Result = Terminal->CurrentDirectory;
  8412. break;
  8413. }
  8414. }
  8415. return Result;
  8416. }
  8417. //---------------------------------------------------------------------------
  8418. TColor __fastcall TCustomScpExplorerForm::PanelColor()
  8419. {
  8420. TColor Result = GetWindowColor(FSessionColor);
  8421. return Result;
  8422. }
  8423. //---------------------------------------------------------------------------
  8424. TColor __fastcall TCustomScpExplorerForm::DisabledPanelColor()
  8425. {
  8426. TColor Result = (WinConfiguration->UseDarkTheme() ? static_cast<TColor>(RGB(0x40, 0x40, 0x40)) : clBtnFace);
  8427. return Result;
  8428. }
  8429. //---------------------------------------------------------------------------
  8430. void __fastcall TCustomScpExplorerForm::UpdateControls()
  8431. {
  8432. if (!FSessionChanging)
  8433. {
  8434. TTerminalManager::Instance()->UpdateAppTitle();
  8435. }
  8436. // WORKAROUND: Disabling list view when it is not showing yet does not set its
  8437. // background to gray on Windows 7 (works on Windows 10).
  8438. // See also EnableControl
  8439. if (Showing)
  8440. {
  8441. if (ManagedSession != NULL)
  8442. {
  8443. // Update path when it changes
  8444. if ((SessionsPageControl->ActivePage != NULL) &&
  8445. (GetSessionTabSession(SessionsPageControl->ActivePage) == ManagedSession))
  8446. {
  8447. UpdateSessionTab(SessionsPageControl->ActivePage);
  8448. }
  8449. }
  8450. NonVisualDataModule->ReconnectSessionAction->Update();
  8451. ReconnectToolbar->Visible = NonVisualDataModule->ReconnectSessionAction->Visible;
  8452. // ReconnectSessionAction is hidden when disabled, so enabling it actualy resizes the toolbar
  8453. CenterReconnectToolbar();
  8454. bool HasTerminal = HasActiveTerminal();
  8455. RemoteDirView->Enabled = HasTerminal;
  8456. RemoteDirView->Color = HasTerminal ? PanelColor() : DisabledPanelColor();
  8457. RemoteDirView->Font->Color = GetWindowTextColor(RemoteDirView->Color);
  8458. RemoteDriveView->Enabled = RemoteDirView->Enabled;
  8459. RemoteDriveView->Color = RemoteDirView->Color;
  8460. RemoteDriveView->Font->Color = RemoteDirView->Font->Color;
  8461. QueueView3->Enabled = HasTerminal && Terminal->IsCapable[fcBackgroundTransfers];
  8462. QueueView3->Color = QueueView3->Enabled ? GetWindowColor() : DisabledPanelColor();
  8463. QueueView3->Font->Color = GetWindowTextColor(QueueView3->Color);
  8464. QueueFileList->Enabled = QueueView3->Enabled;
  8465. QueueFileList->Color = QueueView3->Color;
  8466. QueueFileList->Font->Color = QueueView3->Font->Color;
  8467. QueueLabelUpdateStatus();
  8468. RemoteDirView->DarkMode = WinConfiguration->UseDarkTheme();
  8469. RemoteDriveView->DarkMode = RemoteDirView->DarkMode;
  8470. if (FImmersiveDarkMode != WinConfiguration->UseDarkTheme())
  8471. {
  8472. UpdateDarkMode();
  8473. }
  8474. reinterpret_cast<TTBCustomItem *>(GetComponent(fcRemotePathComboBox))->Enabled = HasTerminal || IsLocalBrowserMode();
  8475. }
  8476. }
  8477. //---------------------------------------------------------------------------
  8478. void __fastcall TCustomScpExplorerForm::DoDirViewLoaded(TCustomDirView * /*Sender*/)
  8479. {
  8480. UpdateControls();
  8481. }
  8482. //---------------------------------------------------------------------------
  8483. void __fastcall TCustomScpExplorerForm::DirViewLoaded(
  8484. TObject * Sender)
  8485. {
  8486. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  8487. DebugAssert(DirView != NULL);
  8488. DoDirViewLoaded(DirView);
  8489. TransferPresetAutoSelect();
  8490. }
  8491. //---------------------------------------------------------------------------
  8492. void __fastcall TCustomScpExplorerForm::StartUpdates()
  8493. {
  8494. TUpdatesConfiguration Updates = WinConfiguration->Updates;
  8495. // first run after installation
  8496. if (double(Updates.LastCheck) == 0)
  8497. {
  8498. // make sure next time there will be an update (if enabled)
  8499. Updates.LastCheck = TDateTime(1);
  8500. WinConfiguration->Updates = Updates;
  8501. }
  8502. else if ((double(Updates.Period) > 0) &&
  8503. (Now() - Updates.LastCheck >= Updates.Period))
  8504. {
  8505. TThreadMethod OnUpdatesChecked = NULL;
  8506. if (!IsUWP())
  8507. {
  8508. OnUpdatesChecked = UpdatesChecked;
  8509. }
  8510. StartUpdateThread(OnUpdatesChecked);
  8511. }
  8512. }
  8513. //---------------------------------------------------------------------------
  8514. void __fastcall TCustomScpExplorerForm::UpdatesChecked()
  8515. {
  8516. UnicodeString Message;
  8517. bool New;
  8518. TQueryType Type;
  8519. GetUpdatesMessage(Message, New, Type, false);
  8520. if (!Message.IsEmpty())
  8521. {
  8522. if (New)
  8523. {
  8524. Message = FMTLOAD(NEW_VERSION_CLICK, (Message));
  8525. }
  8526. if (!New && (Type != qtWarning))
  8527. {
  8528. PostNote(UnformatMessage(Message), 0, UpdatesNoteClicked, NULL);
  8529. }
  8530. else
  8531. {
  8532. Notify(NULL, Message, Type, true, UpdatesNoteClicked);
  8533. }
  8534. if (New)
  8535. {
  8536. Configuration->Usage->Inc(L"UpdateNotifications");
  8537. }
  8538. }
  8539. }
  8540. //---------------------------------------------------------------------------
  8541. void __fastcall TCustomScpExplorerForm::UpdatesNoteClicked(TObject * /*Sender*/)
  8542. {
  8543. RestoreApp();
  8544. bool CanDisplay = !NonVisualDataModule->Busy;
  8545. if (!CanDisplay && (Screen->ActiveForm != NULL))
  8546. {
  8547. CanDisplay = (Screen->ActiveForm->Perform(WM_CAN_DISPLAY_UPDATES, 0, 0) != 0);
  8548. }
  8549. if (CanDisplay)
  8550. {
  8551. Configuration->Usage->Inc(L"UpdateNotificationsClicked");
  8552. CheckForUpdates(true);
  8553. }
  8554. else
  8555. {
  8556. Configuration->Usage->Inc(L"UpdateNotificationsBusy");
  8557. }
  8558. }
  8559. //---------------------------------------------------------------------------
  8560. void __fastcall TCustomScpExplorerForm::GetTransferPresetAutoSelectData(
  8561. TCopyParamRuleData & Data)
  8562. {
  8563. DebugAssert(Terminal != NULL);
  8564. Data.HostName = Terminal->SessionData->HostNameExpanded;
  8565. Data.UserName = Terminal->SessionData->UserNameExpanded;
  8566. Data.RemoteDirectory = RemoteDirView->PathName;
  8567. }
  8568. //---------------------------------------------------------------------------
  8569. void __fastcall TCustomScpExplorerForm::TransferPresetAutoSelect()
  8570. {
  8571. // Terminal can be null when we are changing local directory implicitly,
  8572. // because it has been deleted, while no session is connected
  8573. // (Login dialog is open)
  8574. if (FAllowTransferPresetAutoSelect && (Terminal != NULL))
  8575. {
  8576. DebugAssert(!IsLocalBrowserMode());
  8577. TCopyParamRuleData Data;
  8578. GetTransferPresetAutoSelectData(Data);
  8579. int CopyParamIndex = GUIConfiguration->CopyParamList->Find(Data);
  8580. UnicodeString CopyParamCurrent = GUIConfiguration->CopyParamCurrent;
  8581. if (CopyParamIndex < 0)
  8582. {
  8583. // there is no preset that matches autoselection
  8584. // set preset that we consider "default"
  8585. FCopyParamAutoSelected = L""; // forget last autoselected preset
  8586. GUIConfiguration->CopyParamCurrent = FCopyParamDefault;
  8587. }
  8588. else
  8589. {
  8590. // there is preset matching autoselection
  8591. UnicodeString CopyParamName = GUIConfiguration->CopyParamList->Names[CopyParamIndex];
  8592. if (CopyParamName == FCopyParamAutoSelected)
  8593. {
  8594. // autoselected the same preset as the last time
  8595. // make no change (i.e. preserve custom user preset, if any)
  8596. }
  8597. else
  8598. {
  8599. // autoselected the different preset then the last time (or there
  8600. // was default preset set)
  8601. FCopyParamAutoSelected = CopyParamName; // remember autoselection
  8602. GUIConfiguration->CopyParamCurrent = CopyParamName;
  8603. }
  8604. }
  8605. if (GUIConfiguration->CopyParamCurrent != CopyParamCurrent)
  8606. {
  8607. if (CopyParamIndex >= 0)
  8608. {
  8609. Configuration->Usage->Inc(L"CopyParamAutoSelects");
  8610. }
  8611. TTransferPresetNoteData * Data = new TTransferPresetNoteData;
  8612. try
  8613. {
  8614. int Fmt =
  8615. (CopyParamIndex < 0) ?
  8616. (GUIConfiguration->CopyParamIndex < 0 ? COPY_PARAM_DEFAULT_NORM : COPY_PARAM_DEFAULT_CUSTOM) :
  8617. COPY_PARAM_AUTOSELECTED;
  8618. UnicodeString Message = FMTLOAD(Fmt, (StripHotkey(GUIConfiguration->CopyParamCurrent)));
  8619. Data->Message = MainInstructions(Message);
  8620. int CopyParamAttrs = Terminal->UsableCopyParamAttrs(0).General;
  8621. UnicodeString Info = GUIConfiguration->CurrentCopyParam.GetInfoStr(L"\n",
  8622. CopyParamAttrs);
  8623. if (CopyParamIndex >= 0)
  8624. {
  8625. DebugAssert(GUIConfiguration->CopyParamList->Rules[CopyParamIndex] != NULL);
  8626. Info = FORMAT(L"%s\n \n%s", (Info,
  8627. FMTLOAD(COPY_PARAM_RULE,
  8628. (GUIConfiguration->CopyParamList->Rules[CopyParamIndex]->GetInfoStr(L"\n")))));
  8629. }
  8630. Data->Message += L"\n\n" + Info;
  8631. if (WinConfiguration->CopyParamAutoSelectNotice)
  8632. {
  8633. TransferPresetNoteMessage(Data, true);
  8634. }
  8635. else
  8636. {
  8637. PostNote(Message, 0, TransferPresetNoteClicked, Data);
  8638. Data = NULL; // ownership passed
  8639. }
  8640. }
  8641. __finally
  8642. {
  8643. delete Data;
  8644. }
  8645. }
  8646. }
  8647. }
  8648. //---------------------------------------------------------------------------
  8649. void __fastcall TCustomScpExplorerForm::TransferPresetNoteMessage(
  8650. TTransferPresetNoteData * NoteData, bool AllowNeverAskAgain)
  8651. {
  8652. DebugAssert(NoteData != NULL);
  8653. DebugAssert(!IsLocalBrowserMode());
  8654. TMessageParams Params(AllowNeverAskAgain ? mpNeverAskAgainCheck : 0);
  8655. TQueryButtonAlias Aliases[1];
  8656. Aliases[0].Button = qaIgnore; // "ignore" is after "ok"
  8657. Aliases[0].Alias = LoadStr(CONFIGURE_BUTTON);
  8658. Params.Aliases = Aliases;
  8659. Params.AliasesCount = LENOF(Aliases);
  8660. unsigned int Result =
  8661. MoreMessageDialog(NoteData->Message, NULL, qtInformation,
  8662. qaOK | qaIgnore, HELP_COPY_PARAM_AUTOSELECTED, &Params);
  8663. switch (Result)
  8664. {
  8665. case qaNeverAskAgain:
  8666. DebugAssert(AllowNeverAskAgain);
  8667. WinConfiguration->CopyParamAutoSelectNotice = false;
  8668. break;
  8669. case qaIgnore:
  8670. PreferencesDialog(pmPresets);
  8671. break;
  8672. }
  8673. }
  8674. //---------------------------------------------------------------------------
  8675. void __fastcall TCustomScpExplorerForm::TransferPresetNoteClicked(TObject * Sender)
  8676. {
  8677. // as of now useless, as this is used for notes only, never for balloons, ...
  8678. RestoreApp();
  8679. // .. and we should never be busy here
  8680. if (DebugAlwaysTrue(!NonVisualDataModule->Busy))
  8681. {
  8682. TransferPresetNoteMessage(DebugNotNull(dynamic_cast<TTransferPresetNoteData *>(Sender)), false);
  8683. }
  8684. }
  8685. //---------------------------------------------------------------------------
  8686. void __fastcall TCustomScpExplorerForm::PreferencesDialog(
  8687. TPreferencesMode APreferencesMode)
  8688. {
  8689. std::unique_ptr<TPreferencesDialogData> PreferencesData;
  8690. TCopyParamRuleData Data;
  8691. if (Terminal != NULL)
  8692. {
  8693. PreferencesData.reset(new TPreferencesDialogData());
  8694. GetTransferPresetAutoSelectData(Data);
  8695. PreferencesData->CopyParamRuleData = &Data;
  8696. }
  8697. DoPreferencesDialog(APreferencesMode, PreferencesData.get());
  8698. }
  8699. //---------------------------------------------------------------------------
  8700. void __fastcall TCustomScpExplorerForm::AdHocCustomCommandValidate(
  8701. const TCustomCommandType & Command)
  8702. {
  8703. if (CustomCommandState(Command, FEditingFocusedAdHocCommand, ccltAll) <= 0)
  8704. {
  8705. throw Exception(FMTLOAD(CUSTOM_COMMAND_IMPOSSIBLE2, (Command.Command)));
  8706. }
  8707. }
  8708. //---------------------------------------------------------------------------
  8709. void __fastcall TCustomScpExplorerForm::AdHocCustomCommand(bool OnFocused)
  8710. {
  8711. bool RemoteAllowed = CustomCommandRemoteAllowed();
  8712. TCustomCommandType Command;
  8713. // make sure we use local custom command when remote are not supported
  8714. if (RemoteAllowed || FLAGSET(FLastCustomCommand.Params, ccLocal))
  8715. {
  8716. Command = FLastCustomCommand;
  8717. }
  8718. else
  8719. {
  8720. Command.Params = Command.Params | ccLocal;
  8721. }
  8722. Command.Name = LoadStr(CUSTOM_COMMAND_AD_HOC_NAME);
  8723. FEditingFocusedAdHocCommand = OnFocused;
  8724. bool LocalSide = IsSideLocalBrowser(FCurrentSide);
  8725. int Options =
  8726. FLAGMASK((!RemoteAllowed || LocalSide), ccoDisableRemote) |
  8727. FLAGMASK(LocalSide, ccoDisableRemoteFiles);
  8728. if (DoCustomCommandDialog(Command, WinConfiguration->CustomCommandList,
  8729. ccmAdHoc, Options, AdHocCustomCommandValidate, NULL))
  8730. {
  8731. FLastCustomCommand = Command;
  8732. UpdateCustomCommandsToolbar();
  8733. ExecuteFileOperation(foCustomCommand, osRemote, OnFocused, false, &Command);
  8734. }
  8735. }
  8736. //---------------------------------------------------------------------------
  8737. void __fastcall TCustomScpExplorerForm::LastCustomCommand(bool OnFocused)
  8738. {
  8739. DebugAssert(!FLastCustomCommand.Command.IsEmpty());
  8740. int State = CustomCommandState(FLastCustomCommand, OnFocused, ccltAll);
  8741. DebugAssert(State > 0);
  8742. if (State <= 0)
  8743. {
  8744. throw Exception(FMTLOAD(CUSTOM_COMMAND_IMPOSSIBLE2, (FLastCustomCommand.Command)));
  8745. }
  8746. ExecuteFileOperation(foCustomCommand, osRemote, OnFocused, false, &FLastCustomCommand);
  8747. }
  8748. //---------------------------------------------------------------------------
  8749. bool __fastcall TCustomScpExplorerForm::GetLastCustomCommand(bool OnFocused,
  8750. TCustomCommandType & Command, int & State)
  8751. {
  8752. bool Result = !FLastCustomCommand.Command.IsEmpty();
  8753. if (Result)
  8754. {
  8755. Command = FLastCustomCommand;
  8756. State = CustomCommandState(FLastCustomCommand, OnFocused, ccltAll);
  8757. }
  8758. return Result;
  8759. }
  8760. //---------------------------------------------------------------------------
  8761. void __fastcall TCustomScpExplorerForm::BeforeAction()
  8762. {
  8763. ResetIncrementalSearch();
  8764. }
  8765. //---------------------------------------------------------------------------
  8766. void __fastcall TCustomScpExplorerForm::PostComponentHide(Byte Component)
  8767. {
  8768. DebugAssert(ComponentVisible[Component]);
  8769. PostMessage(Handle, WM_COMPONENT_HIDE, Component, 0);
  8770. }
  8771. //---------------------------------------------------------------------------
  8772. void __fastcall TCustomScpExplorerForm::QueueSplitterDblClick(TObject * /*Sender*/)
  8773. {
  8774. // when queue panel is resized here directly, the status bar is stretched
  8775. // over whole space the panel occupied
  8776. PostComponentHide(fcQueueView);
  8777. }
  8778. //---------------------------------------------------------------------------
  8779. void __fastcall TCustomScpExplorerForm::QueueFileListSplitterDblClick(TObject *)
  8780. {
  8781. ComponentVisible[fcQueueFileList] = false;
  8782. }
  8783. //---------------------------------------------------------------------------
  8784. void __fastcall TCustomScpExplorerForm::ThemeChanged()
  8785. {
  8786. // We hoped this will refresh scrollbar colors, but it does not have any effect here.
  8787. RefreshColorMode();
  8788. ResetSysDarkTheme();
  8789. ConfigurationChanged();
  8790. ConfigureInterface();
  8791. // Should be called for all controls
  8792. RemoteDirView->Perform(WM_THEMECHANGED, 0, 0);
  8793. }
  8794. //---------------------------------------------------------------------------
  8795. void __fastcall TCustomScpExplorerForm::WMSettingChange(TMessage & Message)
  8796. {
  8797. // Do not handle, when shutting down anyway (maybe also when not setup completelly yet?)
  8798. if (!FInvalid &&
  8799. (Message.LParam != 0) &&
  8800. (wcscmp(reinterpret_cast<LPCWCH>(Message.LParam), L"ImmersiveColorSet") == 0))
  8801. {
  8802. ThemeChanged();
  8803. }
  8804. TForm::Dispatch(&Message);
  8805. }
  8806. //---------------------------------------------------------------------------
  8807. void __fastcall TCustomScpExplorerForm::Dispatch(void * Message)
  8808. {
  8809. TMessage * M = static_cast<TMessage*>(Message);
  8810. switch (M->Msg)
  8811. {
  8812. case CM_DIALOGCHAR:
  8813. CMDialogChar(*M);
  8814. break;
  8815. case WM_APPCOMMAND:
  8816. WMAppCommand(*M);
  8817. break;
  8818. case WM_SYSCOMMAND:
  8819. WMSysCommand(*M);
  8820. break;
  8821. case WM_QUERYENDSESSION:
  8822. WMQueryEndSession(*M);
  8823. break;
  8824. case WM_ENDSESSION:
  8825. WMEndSession(*reinterpret_cast<TWMEndSession *>(M));
  8826. break;
  8827. #ifdef _DEBUG
  8828. case WM_WINDOWPOSCHANGED:
  8829. WMWindowPosChanged(*reinterpret_cast<TWMWindowPosMsg *>(M));
  8830. break;
  8831. case CM_CUSTOMSTYLECHANGED:
  8832. TForm::Dispatch(Message);
  8833. break;
  8834. #endif
  8835. case WM_COMPONENT_HIDE:
  8836. {
  8837. Byte Component = static_cast<Byte>(M->WParam);
  8838. // sanity check
  8839. if (ComponentVisible[Component])
  8840. {
  8841. // special treatment
  8842. if (Component == fcQueueView)
  8843. {
  8844. ToggleQueueVisibility();
  8845. DebugAssert(!ComponentVisible[fcQueueView]);
  8846. }
  8847. else
  8848. {
  8849. ComponentVisible[Component] = false;
  8850. }
  8851. }
  8852. }
  8853. break;
  8854. case WM_COPYDATA:
  8855. WMCopyData(*M);
  8856. break;
  8857. case WM_MANAGES_CAPTION:
  8858. // caption managed in TTerminalManager::UpdateAppTitle()
  8859. M->Result = 1;
  8860. break;
  8861. case WM_WANTS_MOUSEWHEEL:
  8862. M->Result = 1;
  8863. break;
  8864. case CM_SHOWINGCHANGED:
  8865. CMShowingChanged(*M);
  8866. break;
  8867. case WM_CLOSE:
  8868. WMClose(*M);
  8869. break;
  8870. case WM_DPICHANGED:
  8871. WMDpiChanged(*M);
  8872. break;
  8873. case CM_DPICHANGED:
  8874. CMDpiChanged(*M);
  8875. break;
  8876. case WM_SETTINGCHANGE:
  8877. WMSettingChange(*M);
  8878. break;
  8879. case CM_DIALOGKEY:
  8880. CMDialogKey(*reinterpret_cast<TWMKeyDown *>(M));
  8881. break;
  8882. default:
  8883. TForm::Dispatch(Message);
  8884. break;
  8885. }
  8886. }
  8887. //---------------------------------------------------------------------------
  8888. void __fastcall TCustomScpExplorerForm::UpdateImages()
  8889. {
  8890. // noop
  8891. }
  8892. //---------------------------------------------------------------------------
  8893. void __fastcall TCustomScpExplorerForm::CMDpiChanged(TMessage & Message)
  8894. {
  8895. TForm::Dispatch(&Message);
  8896. FSessionColors->Clear();
  8897. AddFixedSessionImages();
  8898. RegenerateSessionColorsImageList(FSessionColors, FSessionColorMaskImageIndex);
  8899. UpdateImages();
  8900. }
  8901. //---------------------------------------------------------------------------
  8902. void __fastcall TCustomScpExplorerForm::WMDpiChanged(TMessage & Message)
  8903. {
  8904. TForm::Dispatch(&Message);
  8905. Configuration->Usage->Inc(L"PixelsPerInchChanged");
  8906. UpdatePixelsPerInchMainWindowCounter();
  8907. }
  8908. //---------------------------------------------------------------------------
  8909. void __fastcall TCustomScpExplorerForm::WMClose(TMessage & Message)
  8910. {
  8911. // Cannot close window while we are busy.
  8912. // We cannot test this in FormCloseQuery as that is called also from
  8913. // Close(), which is called by CloseApplicationAction. So we can be busy
  8914. // there already even, when it is legitimate to close the application.
  8915. // Possibly a better place to handle this would be WMSysCommand.
  8916. if (NonVisualDataModule->Busy)
  8917. {
  8918. Message.Result = 1;
  8919. }
  8920. else
  8921. {
  8922. TForm::Dispatch(&Message);
  8923. }
  8924. }
  8925. //---------------------------------------------------------------------------
  8926. void __fastcall TCustomScpExplorerForm::CMShowingChanged(TMessage & Message)
  8927. {
  8928. if (!FShowingChanged)
  8929. {
  8930. AddStartupSequence(L"D");
  8931. FShowingChanged = true;
  8932. }
  8933. // Ignoring implicit local-local browser of Commander, opened from DoShow
  8934. bool NoSession = (ManagedSession == NULL);
  8935. // This calls TCustomScpExplorerForm::DoShow
  8936. TForm::Dispatch(&Message);
  8937. // Now the window is visible (TForm::Dispatch is what usually takes long, like when loading a "local" network directory)
  8938. if (Showing && !FStarted)
  8939. {
  8940. FStarted = true;
  8941. InterfaceStarted();
  8942. }
  8943. if (Showing && NoSession)
  8944. {
  8945. // When we are starting minimized (i.e. from an installer),
  8946. // postpone showing Login dialog until we get restored.
  8947. // Otherwise the Login dialog (and Authentication window) show restored
  8948. // over invisible (minimized) main window.
  8949. if (WindowState == wsMinimized)
  8950. {
  8951. FNeedSession = true;
  8952. }
  8953. else
  8954. {
  8955. // This happens before application ever goes idle, so the toolbars would
  8956. // stay enabled (initial state) until the Login dialog is dismissed.
  8957. UpdateActions();
  8958. NonVisualDataModule->StartBusy();
  8959. try
  8960. {
  8961. // Need to process WM_ACTIVATEAPP before showing the Login dialog,
  8962. // otherwise the dialog does not receive focus.
  8963. // With Commander interface the ProcessMessages is called already
  8964. // by TDriveView, but with Explorer interface, we need to call it explicily
  8965. Application->ProcessMessages();
  8966. // do not reload sessions, they have been loaded just now (optimization)
  8967. NeedSession(true);
  8968. }
  8969. __finally
  8970. {
  8971. NonVisualDataModule->EndBusy();
  8972. }
  8973. }
  8974. }
  8975. }
  8976. //---------------------------------------------------------------------------
  8977. void __fastcall TCustomScpExplorerForm::CenterReconnectToolbar()
  8978. {
  8979. ReconnectToolbar->Left = (ReconnectToolbar->Parent->ClientWidth - ReconnectToolbar->Width) / 2;
  8980. ReconnectToolbar->Top = (ReconnectToolbar->Parent->ClientHeight - ReconnectToolbar->Height) / 2;
  8981. }
  8982. //---------------------------------------------------------------------------
  8983. void __fastcall TCustomScpExplorerForm::FormConstrainedResize(
  8984. TObject * /*Sender*/, int & MinWidth, int & MinHeight, int & MaxWidth,
  8985. int & MaxHeight)
  8986. {
  8987. // workaround for bug in TWinControl.CalcConstraints
  8988. // Check for empty rect (restore from iconic state) is done there only after
  8989. // call to AdjustClientRect, which enlarges the rect (for forms).
  8990. TRect R = GetClientRect();
  8991. // when restoring from iconic state, place no restrictions
  8992. if (IsRectEmpty(R))
  8993. {
  8994. MinWidth = 0;
  8995. MinHeight = 0;
  8996. MaxWidth = 32000;
  8997. MaxHeight = 32000;
  8998. }
  8999. }
  9000. //---------------------------------------------------------------------------
  9001. void __fastcall TCustomScpExplorerForm::GetSpaceAvailable(const UnicodeString Path,
  9002. TSpaceAvailable & ASpaceAvailable, bool & Close)
  9003. {
  9004. // terminal can be already closed (e.g. dropped connection)
  9005. if ((Terminal != NULL) && Terminal->IsCapable[fcCheckingSpaceAvailable])
  9006. {
  9007. Configuration->Usage->Inc(L"SpaceAvailableChecks");
  9008. try
  9009. {
  9010. Terminal->SpaceAvailable(Path, ASpaceAvailable);
  9011. }
  9012. catch(...)
  9013. {
  9014. if (!Terminal->Active)
  9015. {
  9016. Close = true;
  9017. }
  9018. throw;
  9019. }
  9020. }
  9021. }
  9022. //---------------------------------------------------------------------------
  9023. void __fastcall TCustomScpExplorerForm::FileSystemInfo()
  9024. {
  9025. const TSessionInfo & SessionInfo = Terminal->GetSessionInfo();
  9026. const TFileSystemInfo & FileSystemInfo = Terminal->GetFileSystemInfo(true);
  9027. TGetSpaceAvailable OnGetSpaceAvailable = NULL;
  9028. if (Terminal->IsCapable[fcCheckingSpaceAvailable])
  9029. {
  9030. OnGetSpaceAvailable = GetSpaceAvailable;
  9031. }
  9032. DoFileSystemInfoDialog(SessionInfo, FileSystemInfo, Terminal->CurrentDirectory,
  9033. OnGetSpaceAvailable);
  9034. }
  9035. //---------------------------------------------------------------------------
  9036. TSessionData * __fastcall TCustomScpExplorerForm::SessionDataForCode()
  9037. {
  9038. std::unique_ptr<TSessionData> Data(CloneCurrentSessionData());
  9039. Terminal->FillSessionDataForCode(Data.get());
  9040. return Data.release();
  9041. }
  9042. //---------------------------------------------------------------------------
  9043. void __fastcall TCustomScpExplorerForm::GenerateUrl(TStrings * Paths)
  9044. {
  9045. std::unique_ptr<TSessionData> Data(SessionDataForCode());
  9046. DoGenerateUrlDialog(Data.get(), Paths);
  9047. }
  9048. //---------------------------------------------------------------------------
  9049. void __fastcall TCustomScpExplorerForm::SessionGenerateUrl()
  9050. {
  9051. GenerateUrl(NULL);
  9052. }
  9053. //---------------------------------------------------------------------------
  9054. void __fastcall TCustomScpExplorerForm::FileGenerateUrl()
  9055. {
  9056. std::unique_ptr<TStrings> Paths(new TStringList());
  9057. DirView(osCurrent)->CreateFileList(false, true, Paths.get());
  9058. GenerateUrl(Paths.get());
  9059. }
  9060. //---------------------------------------------------------------------------
  9061. void __fastcall TCustomScpExplorerForm::UpdateSessionColor(TColor value)
  9062. {
  9063. FSessionColor = value;
  9064. TColor C = (value != 0 ? value : Vcl::Graphics::clNone);
  9065. TTBXColorItem * ColorItem = dynamic_cast<TTBXColorItem *>(
  9066. static_cast<TObject *>(GetComponent(fcColorMenu)));
  9067. DebugAssert(ColorItem != NULL);
  9068. ColorItem->Color = C;
  9069. NonVisualDataModule->ColorMenuItem->Color = C;
  9070. UpdateControls();
  9071. }
  9072. //---------------------------------------------------------------------------
  9073. void __fastcall TCustomScpExplorerForm::SetSessionColor(TColor value)
  9074. {
  9075. if (value != FSessionColor)
  9076. {
  9077. UpdateSessionColor(value);
  9078. // Is null when called from LastTerminalClosed
  9079. if (Terminal != NULL)
  9080. {
  9081. SessionsPageControl->ActivePage->ImageIndex = AddSessionColor(value);
  9082. }
  9083. }
  9084. }
  9085. //---------------------------------------------------------------------------
  9086. bool __fastcall TCustomScpExplorerForm::CancelNote(bool Force)
  9087. {
  9088. bool Result = FNoteTimer->Enabled;
  9089. if (Result)
  9090. {
  9091. // cannot cancel note too early
  9092. bool NotEarly =
  9093. (Now() - FNoteShown >
  9094. EncodeTimeVerbose(0, 0, (unsigned short)(WinConfiguration->NotificationsStickTime), 0));
  9095. if (Force || NotEarly)
  9096. {
  9097. FNoteTimer->Enabled = false;
  9098. FNote = L"";
  9099. SAFE_DESTROY(FNoteData);
  9100. FOnNoteClick = NULL;
  9101. FNoteHints = FNotes->Text;
  9102. FNoteHints.Delete(FNoteHints.Length() - 1, 2);
  9103. UpdateStatusBar();
  9104. }
  9105. }
  9106. return Result;
  9107. }
  9108. //---------------------------------------------------------------------------
  9109. void __fastcall TCustomScpExplorerForm::NoteTimer(TObject * /*Sender*/)
  9110. {
  9111. DebugAssert(FNoteTimer->Enabled);
  9112. CancelNote(true);
  9113. }
  9114. //---------------------------------------------------------------------------
  9115. void __fastcall TCustomScpExplorerForm::UpdateNoteHints()
  9116. {
  9117. FNoteHints = FNotes->Text.TrimRight();
  9118. }
  9119. //---------------------------------------------------------------------------
  9120. void __fastcall TCustomScpExplorerForm::AddNote(UnicodeString Note, bool UpdateNow)
  9121. {
  9122. int P = Note.Pos(L"\n");
  9123. if (P > 0)
  9124. {
  9125. Note.SetLength(P - 1);
  9126. }
  9127. FNotes->Add(FORMAT(L"[%s] %s",
  9128. (FormatDateTime(Configuration->TimeFormat, Now()), Note)));
  9129. while (FNotes->Count > 10)
  9130. {
  9131. FNotes->Delete(0);
  9132. }
  9133. if (UpdateNow)
  9134. {
  9135. UpdateNoteHints();
  9136. UpdateStatusBar();
  9137. }
  9138. }
  9139. //---------------------------------------------------------------------------
  9140. void __fastcall TCustomScpExplorerForm::PostNote(UnicodeString Note,
  9141. unsigned int Seconds, TNotifyEvent OnNoteClick, TObject * NoteData)
  9142. {
  9143. int P = Note.Pos(L"\n");
  9144. if (P > 0)
  9145. {
  9146. Note.SetLength(P - 1);
  9147. }
  9148. UpdateNoteHints();
  9149. FNote = Note;
  9150. SAFE_DESTROY(FNoteData);
  9151. FNoteData = NoteData;
  9152. FOnNoteClick = OnNoteClick;
  9153. AddNote(Note, false);
  9154. UpdateStatusBar();
  9155. FNoteShown = Now();
  9156. FNoteTimer->Enabled = false;
  9157. if (Seconds == 0)
  9158. {
  9159. Seconds = WinConfiguration->NotificationsTimeout;
  9160. }
  9161. FNoteTimer->Interval = Seconds * MSecsPerSec;
  9162. FNoteTimer->Enabled = true;
  9163. }
  9164. //---------------------------------------------------------------------------
  9165. void __fastcall TCustomScpExplorerForm::ReadDirectoryCancelled()
  9166. {
  9167. PostNote(LoadStr(DIRECTORY_READING_CANCELLED), 0, NULL, NULL);
  9168. }
  9169. //---------------------------------------------------------------------------
  9170. void __fastcall TCustomScpExplorerForm::SynchronizeBrowsingChanged()
  9171. {
  9172. if (NonVisualDataModule->SynchronizeBrowsingAction2->Checked)
  9173. {
  9174. Configuration->Usage->Inc(L"SynchronizeBrowsingEnabled");
  9175. }
  9176. PostNote(FORMAT(LoadStrPart(SYNC_DIR_BROWSE_TOGGLE, 1),
  9177. (LoadStrPart(SYNC_DIR_BROWSE_TOGGLE,
  9178. (NonVisualDataModule->SynchronizeBrowsingAction2->Checked ? 2 : 3)))),
  9179. 0, NULL, NULL);
  9180. }
  9181. //---------------------------------------------------------------------------
  9182. void __fastcall TCustomScpExplorerForm::ToggleShowHiddenFiles()
  9183. {
  9184. PostNote(FORMAT(LoadStrPart(SHOW_HIDDEN_FILES_TOGGLE, 1),
  9185. (LoadStrPart(SHOW_HIDDEN_FILES_TOGGLE,
  9186. (WinConfiguration->ShowHiddenFiles ? 2 : 3)))), 0, NULL, NULL);
  9187. GetComponent(fcStatusBar)->Repaint(); // toggling ShowHiddenFiles takes time, force repaint beforehand
  9188. WinConfiguration->ShowHiddenFiles = !WinConfiguration->ShowHiddenFiles;
  9189. }
  9190. //---------------------------------------------------------------------------
  9191. void __fastcall TCustomScpExplorerForm::SetFormatSizeBytes(TFormatBytesStyle Style)
  9192. {
  9193. WinConfiguration->FormatSizeBytes = Style;
  9194. }
  9195. //---------------------------------------------------------------------------
  9196. void __fastcall TCustomScpExplorerForm::ToggleAutoReadDirectoryAfterOp()
  9197. {
  9198. Configuration->AutoReadDirectoryAfterOp = !Configuration->AutoReadDirectoryAfterOp;
  9199. PostNote(FORMAT(LoadStrPart(AUTO_READ_DIRECTORY_TOGGLE, 1),
  9200. (LoadStrPart(AUTO_READ_DIRECTORY_TOGGLE,
  9201. (Configuration->AutoReadDirectoryAfterOp ? 2 : 3)))), 0, NULL, NULL);
  9202. }
  9203. //---------------------------------------------------------------------------
  9204. void __fastcall TCustomScpExplorerForm::StatusBarPanelDblClick(
  9205. TTBXCustomStatusBar * Sender, TTBXStatusPanel * Panel)
  9206. {
  9207. if (Panel->Index == 0)
  9208. {
  9209. if (FOnNoteClick != NULL)
  9210. {
  9211. // prevent the user data from being freed by possible call
  9212. // to CancelNote or PostNote during call to OnNoteClick
  9213. std::unique_ptr<TObject> NoteData(FNoteData);
  9214. TNotifyEvent OnNoteClick = FOnNoteClick;
  9215. FNoteData = NULL;
  9216. // need to cancel the note as we are going to delete its user data
  9217. CancelNote(true);
  9218. OnNoteClick(NoteData.get());
  9219. }
  9220. }
  9221. if (HasActiveTerminal() &&
  9222. (Panel->Index >= Sender->Panels->Count - SessionPanelCount))
  9223. {
  9224. FileSystemInfo();
  9225. }
  9226. }
  9227. //---------------------------------------------------------------------------
  9228. void __fastcall TCustomScpExplorerForm::LockWindow(bool Force)
  9229. {
  9230. // workaround:
  9231. // 1) for unknown reason, disabling window, while minimized,
  9232. // prevents it from restoring, even if it was enabled again meanwhile
  9233. // 2) when disabling the main window, while another has focus
  9234. // minimize is no longer possible
  9235. // ("keep up to date" dialog - though does not seem to be the case with "find file" -
  9236. // so it has probably something to do with the semi-modalness of the "keep up to date" dialog)
  9237. // Shouldn't we use IsApplicationMinimized() here?
  9238. if ((FLockSuspendLevel == 0) && !IsIconic(Application->Handle) && (Force || (Screen->ActiveForm == this)))
  9239. {
  9240. Enabled = false;
  9241. }
  9242. FLockLevel++;
  9243. }
  9244. //---------------------------------------------------------------------------
  9245. void __fastcall TCustomScpExplorerForm::UnlockWindow()
  9246. {
  9247. DebugAssert(FLockLevel > 0);
  9248. FLockLevel--;
  9249. if (FLockLevel == 0)
  9250. {
  9251. DebugAssert(FLockSuspendLevel == 0);
  9252. Enabled = true;
  9253. }
  9254. }
  9255. //---------------------------------------------------------------------------
  9256. void __fastcall TCustomScpExplorerForm::SuspendWindowLock()
  9257. {
  9258. // We need to make sure that window is enabled when the last modal window closes
  9259. // otherwise focus is not restored correctly.
  9260. // So we re-enable the window when modal window opens and
  9261. // disable it back after is closes
  9262. if (FLockLevel > 0)
  9263. {
  9264. // while we have nesting counter, we know that we never be called
  9265. // recursivelly as Application->OnModalBegin is called only
  9266. // for the top-level modal window
  9267. if (DebugAlwaysTrue(FLockSuspendLevel == 0))
  9268. {
  9269. // won't be disabled when conditions in LockWindow() were not satisfied
  9270. FDisabledOnLockSuspend = !Enabled;
  9271. // When minimized to tray (or actually when set to SW_HIDE),
  9272. // setting Enabled makes the window focusable even when there's
  9273. // modal window over it
  9274. if (!FTrayIcon->Visible)
  9275. {
  9276. Enabled = true;
  9277. }
  9278. }
  9279. FLockSuspendLevel++;
  9280. }
  9281. }
  9282. //---------------------------------------------------------------------------
  9283. void __fastcall TCustomScpExplorerForm::ResumeWindowLock()
  9284. {
  9285. if (FLockSuspendLevel > 0)
  9286. {
  9287. DebugAssert(FLockLevel > 0);
  9288. FLockSuspendLevel--;
  9289. // see comment in SuspendWindowLock
  9290. if (DebugAlwaysTrue(FLockSuspendLevel == 0))
  9291. {
  9292. // Note that window can be enabled here, when we were minized to tray when
  9293. // was SuspendWindowLock() called.
  9294. // We should possibly do the same check as in LockWindow(),
  9295. // if it is ever possible that the consitions change between
  9296. // SuspendWindowLock() and ResumeWindowLock()
  9297. Enabled = !FDisabledOnLockSuspend;
  9298. }
  9299. }
  9300. }
  9301. //---------------------------------------------------------------------------
  9302. void __fastcall TCustomScpExplorerForm::UpdateRemotePathComboBox(bool TextOnly)
  9303. {
  9304. TTBXComboBoxItem * RemotePathComboBox =
  9305. reinterpret_cast<TTBXComboBoxItem *>(GetComponent(fcRemotePathComboBox));
  9306. if (!TextOnly)
  9307. {
  9308. TStrings * Items = RemotePathComboBox->Strings;
  9309. Items->BeginUpdate();
  9310. try
  9311. {
  9312. Items->Clear();
  9313. if ((Terminal != NULL) && !RemoteDirView->Path.IsEmpty())
  9314. {
  9315. UnicodeString APath = UnixExcludeTrailingBackslash(RemoteDirView->Path);
  9316. while (!IsUnixRootPath(APath))
  9317. {
  9318. int P = APath.LastDelimiter(L'/');
  9319. DebugAssert(P >= 0);
  9320. Items->Insert(0, APath.SubString(P + 1, APath.Length() - P));
  9321. APath.SetLength(P - 1);
  9322. }
  9323. Items->Insert(0, Customunixdirview_SUnixDefaultRootName);
  9324. }
  9325. }
  9326. __finally
  9327. {
  9328. Items->EndUpdate();
  9329. }
  9330. }
  9331. RemotePathComboBox->ItemIndex = RemotePathComboBox->Strings->Count - 1;
  9332. // Setting ItemIndex to -1 does not reset its text
  9333. if (RemotePathComboBox->Strings->Count == 0)
  9334. {
  9335. RemotePathComboBox->Text = L"";
  9336. }
  9337. }
  9338. //---------------------------------------------------------------------------
  9339. void __fastcall TCustomScpExplorerForm::DoRemotePathComboBoxAdjustImageIndex(
  9340. TTBXComboBoxItem * Sender, const UnicodeString DebugUsedArg(AText), int AIndex, int & ImageIndex)
  9341. {
  9342. if (AIndex < 0)
  9343. {
  9344. AIndex = Sender->ItemIndex;
  9345. }
  9346. ImageIndex = (AIndex < Sender->Strings->Count - 1 ? StdDirIcon : StdDirSelIcon);
  9347. }
  9348. //---------------------------------------------------------------------------
  9349. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxAdjustImageIndex(
  9350. TTBXComboBoxItem * Sender, const UnicodeString AText, int AIndex,
  9351. int & ImageIndex)
  9352. {
  9353. DoRemotePathComboBoxAdjustImageIndex(Sender, AText, AIndex, ImageIndex);
  9354. }
  9355. //---------------------------------------------------------------------------
  9356. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxDrawItem(
  9357. TTBXCustomList * /*Sender*/, TCanvas * /*ACanvas*/, TRect & ARect, int AIndex,
  9358. int /*AHoverIndex*/, bool & /*DrawDefault*/)
  9359. {
  9360. if (!IsLocalBrowserMode())
  9361. {
  9362. ARect.Left += (10 * AIndex);
  9363. }
  9364. }
  9365. //---------------------------------------------------------------------------
  9366. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxMeasureWidth(
  9367. TTBXCustomList * /*Sender*/, TCanvas * /*ACanvas*/, int AIndex, int &AWidth)
  9368. {
  9369. AWidth += (10 * AIndex);
  9370. }
  9371. //---------------------------------------------------------------------------
  9372. void __fastcall TCustomScpExplorerForm::DoRemotePathComboBoxItemClick(TObject * Sender)
  9373. {
  9374. TTBXComboBoxItem * RemotePathComboBox = dynamic_cast<TTBXComboBoxItem*>(Sender);
  9375. UnicodeString APath = UnixExcludeTrailingBackslash(RemoteDirView->Path);
  9376. int Index = RemotePathComboBox->ItemIndex;
  9377. while (Index < RemotePathComboBox->Strings->Count - 1)
  9378. {
  9379. APath = UnixExtractFileDir(APath);
  9380. Index++;
  9381. }
  9382. // VanDyke style paths
  9383. if (APath.IsEmpty())
  9384. {
  9385. DebugAssert(RemotePathComboBox->ItemIndex == 0);
  9386. APath = ROOTDIRECTORY;
  9387. }
  9388. if (RemoteDirView->Path != APath)
  9389. {
  9390. RemoteDirView->Path = APath;
  9391. }
  9392. }
  9393. //---------------------------------------------------------------------------
  9394. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxItemClick(TObject * Sender)
  9395. {
  9396. DoRemotePathComboBoxItemClick(Sender);
  9397. }
  9398. //---------------------------------------------------------------------------
  9399. void __fastcall TCustomScpExplorerForm::DoRemotePathComboBoxCancel(TObject * Sender)
  9400. {
  9401. DebugAssert(Sender == GetComponent(fcRemotePathComboBox));
  9402. DebugUsedParam(Sender);
  9403. UpdateRemotePathComboBox(true);
  9404. }
  9405. //---------------------------------------------------------------------------
  9406. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxCancel(TObject * Sender)
  9407. {
  9408. DoRemotePathComboBoxCancel(Sender);
  9409. }
  9410. //---------------------------------------------------------------------------
  9411. void __fastcall TCustomScpExplorerForm::DirViewEditing(
  9412. TObject * Sender, TListItem * Item, bool & /*AllowEdit*/)
  9413. {
  9414. ResetIncrementalSearch();
  9415. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  9416. DebugAssert(DirView != NULL);
  9417. if (!WinConfiguration->RenameWholeName && !DirView->ItemIsDirectory(Item))
  9418. {
  9419. HWND Edit = ListView_GetEditControl(DirView->Handle);
  9420. // OnEditing is called also from TCustomListView::CanEdit
  9421. if (Edit != NULL)
  9422. {
  9423. EditSelectBaseName(Edit);
  9424. }
  9425. }
  9426. }
  9427. //---------------------------------------------------------------------------
  9428. //---------------------------------------------------------------------------
  9429. TDragDropFilesEx * __fastcall TCustomScpExplorerForm::CreateDragDropFilesEx()
  9430. {
  9431. TDragDropFilesEx * Result = new TDragDropFilesEx(this);
  9432. Result->AutoDetectDnD = false;
  9433. Result->NeedValid = TFileExMustDnDSet() << nvFilename;
  9434. Result->RenderDataOn = rdoEnterAndDropSync;
  9435. Result->TargetEffects = TDropEffectSet() << deCopy << deMove;
  9436. return Result;
  9437. }
  9438. //---------------------------------------------------------------------------
  9439. void __fastcall TCustomScpExplorerForm::UpdateDarkMode()
  9440. {
  9441. if (IsWin10Build(2004))
  9442. {
  9443. FImmersiveDarkMode = WinConfiguration->UseDarkTheme();
  9444. BOOL DarkMode = FImmersiveDarkMode ? TRUE : FALSE;
  9445. const int DWMWA_USE_IMMERSIVE_DARK_MODE = 20;
  9446. DwmSetWindowAttribute(Handle, DWMWA_USE_IMMERSIVE_DARK_MODE, &DarkMode, sizeof(DarkMode));
  9447. }
  9448. }
  9449. //---------------------------------------------------------------------------
  9450. void __fastcall TCustomScpExplorerForm::CreateWnd()
  9451. {
  9452. TForm::CreateWnd();
  9453. // win32-darkmode calls AllowDarkModeForWindow(this, true) here, but it does not seem to have any effect
  9454. UpdateDarkMode();
  9455. if (FSessionsDragDropFilesEx == NULL)
  9456. {
  9457. FSessionsDragDropFilesEx = CreateDragDropFilesEx();
  9458. FSessionsDragDropFilesEx->OnDragOver = SessionsDDDragOver;
  9459. FSessionsDragDropFilesEx->OnProcessDropped = SessionsDDProcessDropped;
  9460. FSessionsDragDropFilesEx->OnDragEnter = SessionsDDDragEnter;
  9461. FSessionsDragDropFilesEx->OnDragLeave = SessionsDDDragLeave;
  9462. }
  9463. if (FQueueDragDropFilesEx == NULL)
  9464. {
  9465. FQueueDragDropFilesEx = CreateDragDropFilesEx();
  9466. // No need to set OnDragOver as we do not have any restrictions
  9467. FQueueDragDropFilesEx->OnProcessDropped = QueueDDProcessDropped;
  9468. FQueueDragDropFilesEx->OnDragEnter = QueueDDDragEnter;
  9469. FQueueDragDropFilesEx->OnDragLeave = QueueDDDragLeave;
  9470. }
  9471. }
  9472. //---------------------------------------------------------------------------
  9473. void __fastcall TCustomScpExplorerForm::DestroyWnd()
  9474. {
  9475. TForm::DestroyWnd();
  9476. FSessionsDragDropFilesEx->DragDropControl = NULL;
  9477. FQueueDragDropFilesEx->DragDropControl = NULL;
  9478. }
  9479. //---------------------------------------------------------------------------
  9480. void __fastcall TCustomScpExplorerForm::FormShow(TObject * /*Sender*/)
  9481. {
  9482. SideEnter(FCurrentSide);
  9483. FEverShown = true;
  9484. }
  9485. //---------------------------------------------------------------------------
  9486. void __fastcall TCustomScpExplorerForm::DoFindFiles(
  9487. TTerminal * ATerminal, UnicodeString Directory, const TFileMasks & FileMask,
  9488. TFileFoundEvent OnFileFound, TFindingFileEvent OnFindingFile)
  9489. {
  9490. if (!NonVisualDataModule->Busy)
  9491. {
  9492. TTerminalManager::Instance()->ActiveSession = DebugNotNull(dynamic_cast<TManagedTerminal *>(ATerminal));
  9493. Configuration->Usage->Inc(L"FileFinds");
  9494. LockWindow(true);
  9495. NonVisualDataModule->StartBusy();
  9496. WinConfiguration->LockedInterface = true;
  9497. try
  9498. {
  9499. Terminal->FilesFind(Directory, FileMask, OnFileFound, OnFindingFile);
  9500. }
  9501. __finally
  9502. {
  9503. WinConfiguration->LockedInterface = false;
  9504. NonVisualDataModule->EndBusy();
  9505. UnlockWindow();
  9506. }
  9507. }
  9508. }
  9509. //---------------------------------------------------------------------------
  9510. void __fastcall TCustomScpExplorerForm::DoFocusRemotePath(TTerminal * ATerminal, const UnicodeString & Path)
  9511. {
  9512. if (!NonVisualDataModule->Busy)
  9513. {
  9514. TTerminalManager::Instance()->ActiveSession = DebugNotNull(dynamic_cast<TManagedTerminal *>(ATerminal));
  9515. SetFocus();
  9516. RemoteDirView->Path = UnixExtractFilePath(Path);
  9517. UnicodeString FileName = UnixExtractFileName(Path);
  9518. TListItem * Item = RemoteDirView->FindFileItem(FileName);
  9519. // If not found, it can because we have loaded directory from cache and the file is new, try again after reload.
  9520. // (tbd: we should not even try this, if we have not loaded the directory from the cache)
  9521. if (Item == NULL)
  9522. {
  9523. ReloadDirectory(osRemote);
  9524. Item = RemoteDirView->FindFileItem(FileName);
  9525. }
  9526. if (Item != NULL)
  9527. {
  9528. RemoteDirView->ItemFocused = Item;
  9529. RemoteDirView->ItemFocused->MakeVisible(false);
  9530. RemoteDirView->SetFocus();
  9531. }
  9532. }
  9533. }
  9534. //---------------------------------------------------------------------------
  9535. bool __fastcall TCustomScpExplorerForm::CanOperateOnFoundFiles(TTerminal * ATerminal)
  9536. {
  9537. bool Result = !NonVisualDataModule->Busy;
  9538. if (Result)
  9539. {
  9540. TTerminalManager::Instance()->ActiveSession = DebugNotNull(dynamic_cast<TManagedTerminal *>(ATerminal));
  9541. }
  9542. return Result;
  9543. }
  9544. //---------------------------------------------------------------------------
  9545. void __fastcall TCustomScpExplorerForm::DoOperationOnFoundFiles(
  9546. TFileOperation Operation, TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  9547. {
  9548. if (CanOperateOnFoundFiles(ATerminal))
  9549. {
  9550. TValueRestorer<TFileOperationFinishedEvent> OnFileOperationFinishedRestorer(FOnFileOperationFinished);
  9551. FOnFileOperationFinished = OnFileOperationFinished;
  9552. ExecuteFileOperation(Operation, osRemote, FileList, false, NULL);
  9553. }
  9554. }
  9555. //---------------------------------------------------------------------------
  9556. void __fastcall TCustomScpExplorerForm::DoDeleteFoundFiles(
  9557. TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  9558. {
  9559. DoOperationOnFoundFiles(foDelete, ATerminal, FileList, OnFileOperationFinished);
  9560. }
  9561. //---------------------------------------------------------------------------
  9562. void __fastcall TCustomScpExplorerForm::DoDownloadFoundFiles(
  9563. TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  9564. {
  9565. DoOperationOnFoundFiles(foCopy, ATerminal, FileList, OnFileOperationFinished);
  9566. }
  9567. //---------------------------------------------------------------------------
  9568. void __fastcall TCustomScpExplorerForm::DoEditFoundFiles(
  9569. TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  9570. {
  9571. if (CanOperateOnFoundFiles(ATerminal))
  9572. {
  9573. for (int Index = 0; Index < FileList->Count; Index++)
  9574. {
  9575. UnicodeString FileName = FileList->Strings[Index];
  9576. TRemoteFile * File = static_cast<TRemoteFile *>(FileList->Objects[Index]);
  9577. ExecuteRemoteFile(FileName, File, efDefaultEditor);
  9578. OnFileOperationFinished(osRemote, FileName, true);
  9579. }
  9580. }
  9581. }
  9582. //---------------------------------------------------------------------------
  9583. void __fastcall TCustomScpExplorerForm::RemoteFindFiles()
  9584. {
  9585. FFileFindTerminal = Terminal;
  9586. ShowFileFindDialog(
  9587. Terminal, RemoteDirView->Path, DoFindFiles, DoFocusRemotePath, DoDeleteFoundFiles,
  9588. DoDownloadFoundFiles, DoEditFoundFiles);
  9589. }
  9590. //---------------------------------------------------------------------------
  9591. void __fastcall TCustomScpExplorerForm::UpdateTaskbarList(ITaskbarList3 * TaskbarList)
  9592. {
  9593. FTaskbarList = TaskbarList;
  9594. }
  9595. //---------------------------------------------------------------------------
  9596. void __fastcall TCustomScpExplorerForm::SessionsPageControlMouseDown(
  9597. TObject * /*Sender*/, TMouseButton Button, TShiftState /*Shift*/, int X, int Y)
  9598. {
  9599. int Index = SessionsPageControl->IndexOfTabAt(X, Y);
  9600. if (Index >= 0)
  9601. {
  9602. if (Button == mbLeft)
  9603. {
  9604. // WORKAROUND: "Mouse down" is raised only after tab is switched.
  9605. // If switching tab (switching session) takes long enough for user
  9606. // to actually release the button, "mouse down" is still raised,
  9607. // but we do not get "mouse up" event, so dragging is not cancelled,
  9608. // prevent that by not beginning dragging in the first place.
  9609. if (IsKeyPressed(VK_LBUTTON))
  9610. {
  9611. // when user clicks the "+", we get mouse down only after the session
  9612. // is closed, when new session tab is already on X:Y, so dragging
  9613. // starts, prevent that
  9614. if (MilliSecondsBetween(Now(), FSessionsPageControlNewTabTime) > 500)
  9615. {
  9616. TTerminal * Terminal = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  9617. if (Terminal != NULL)
  9618. {
  9619. SessionsPageControl->BeginDrag(false);
  9620. }
  9621. }
  9622. }
  9623. }
  9624. else if (Button == mbMiddle)
  9625. {
  9626. // ignore middle-click for "New session tab" and the last local-local tab
  9627. TManagedTerminal * Session = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  9628. if ((Session != NULL) && CanCloseSession(Session))
  9629. {
  9630. CloseSessionTab(Index);
  9631. }
  9632. }
  9633. }
  9634. }
  9635. //---------------------------------------------------------------------------
  9636. void __fastcall TCustomScpExplorerForm::SessionsPageControlDragDrop(
  9637. TObject * /*Sender*/, TObject * /*Source*/, int X, int Y)
  9638. {
  9639. int Index = SessionsPageControl->IndexOfTabAt(X, Y);
  9640. // do not allow dropping on the "+" tab
  9641. TTerminal * TargetTerminal = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  9642. if ((TargetTerminal != NULL) &&
  9643. (SessionsPageControl->ActivePage->PageIndex != Index))
  9644. {
  9645. Configuration->Usage->Inc(L"SessionTabMoves");
  9646. // this is almost redundant as we would recreate tabs in SessionListChanged,
  9647. // but we want to actually prevent that to avoid flicker
  9648. SessionsPageControl->ActivePage->PageIndex = Index;
  9649. TTerminal * Terminal = GetSessionTabSession(SessionsPageControl->ActivePage);
  9650. TTerminalManager::Instance()->Move(Terminal, TargetTerminal);
  9651. }
  9652. }
  9653. //---------------------------------------------------------------------------
  9654. void __fastcall TCustomScpExplorerForm::SessionsPageControlDragOver(
  9655. TObject * Sender, TObject * Source, int X, int Y,
  9656. TDragState /*State*/, bool & Accept)
  9657. {
  9658. Accept = (Sender == Source);
  9659. if (Accept)
  9660. {
  9661. int Index = SessionsPageControl->IndexOfTabAt(X, Y);
  9662. TTerminal * Terminal = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  9663. // do not allow dragging to the "+" tab
  9664. Accept = (Terminal != NULL);
  9665. }
  9666. }
  9667. //---------------------------------------------------------------------------
  9668. void __fastcall TCustomScpExplorerForm::SessionsDDDragOver(
  9669. int DebugUsedArg(KeyState), const TPoint & Point, int & Effect, int DebugUsedArg(PreferredEffect))
  9670. {
  9671. int Index = SessionsPageControl->IndexOfTabAt(Point.X, Point.Y);
  9672. if (Index < 0)
  9673. {
  9674. Effect = DROPEFFECT_NONE;
  9675. }
  9676. else
  9677. {
  9678. TManagedTerminal * TargetSession = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  9679. // do not allow dropping on the "+" tab or on disconnected or local-local tabs
  9680. if (!IsActiveTerminal(TargetSession) || DebugAlwaysFalse(TargetSession->LocalBrowser))
  9681. {
  9682. Effect = DROPEFFECT_NONE;
  9683. }
  9684. else if ((TargetSession != Terminal) && (Effect == DROPEFFECT_MOVE))
  9685. {
  9686. Effect = DROPEFFECT_COPY;
  9687. }
  9688. }
  9689. }
  9690. //---------------------------------------------------------------------------
  9691. void __fastcall TCustomScpExplorerForm::SessionsDDProcessDropped(
  9692. TObject * /*Sender*/, int /*KeyState*/, const TPoint & Point, int Effect)
  9693. {
  9694. int Index = SessionsPageControl->IndexOfTabAt(Point.X, Point.Y);
  9695. // do not allow dropping on local-local, disconnected and the "+" tab
  9696. // (though we do not seem to get here in that case anyway, contrary to RemoteFileControlDDTargetDrop)
  9697. TManagedTerminal * TargetTerminal = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  9698. if (DebugAlwaysTrue(IsActiveTerminal(TargetTerminal) && !TargetTerminal->LocalBrowser))
  9699. {
  9700. DebugAssert(!IsFileControl(DropSourceControl, osRemote));
  9701. if (!IsFileControl(DropSourceControl, osRemote))
  9702. {
  9703. TTerminalManager::Instance()->ActiveSession = TargetTerminal;
  9704. RemoteFileControlDragDropFileOperation(SessionsPageControl, Effect,
  9705. // Why don't we use Terminal->CurrentDirectory directly?
  9706. TTerminalManager::Instance()->ActiveTerminal->CurrentDirectory,
  9707. // do not force queue, drag drop
  9708. false, true);
  9709. }
  9710. }
  9711. }
  9712. //---------------------------------------------------------------------------
  9713. void __fastcall TCustomScpExplorerForm::QueueDDProcessDropped(
  9714. TObject * /*Sender*/, int /*KeyState*/, const TPoint & /*Point*/, int Effect)
  9715. {
  9716. // Downloads are handled in RemoteFileControlDDEnd
  9717. if (!IsFileControl(DropSourceControl, osRemote))
  9718. {
  9719. RemoteFileControlDragDropFileOperation(QueueView3, Effect,
  9720. Terminal->CurrentDirectory,
  9721. // force queue, drag drop
  9722. true, true);
  9723. }
  9724. }
  9725. //---------------------------------------------------------------------------
  9726. void __fastcall TCustomScpExplorerForm::FormClose(TObject * /*Sender*/, TCloseAction & /*Action*/)
  9727. {
  9728. FShowing = false;
  9729. // Do not save empty workspace
  9730. if (WinConfiguration->AutoSaveWorkspace && (ManagedSession != NULL))
  9731. {
  9732. std::unique_ptr<TObjectList> DataList(DoCollectWorkspace());
  9733. UnicodeString Name = WorkspaceName();
  9734. DoSaveWorkspace(
  9735. Name, DataList.get(),
  9736. !Configuration->DisablePasswordStoring &&
  9737. WinConfiguration->AutoSaveWorkspacePasswords, false);
  9738. WinConfiguration->LastStoredSession = Name;
  9739. }
  9740. }
  9741. //---------------------------------------------------------------------------
  9742. void __fastcall TCustomScpExplorerForm::RemoteDirViewRead(TObject * /*Sender*/)
  9743. {
  9744. TManagedTerminal * ManagedTerminal =
  9745. dynamic_cast<TManagedTerminal *>(RemoteDirView->Terminal);
  9746. if (ManagedTerminal != NULL)
  9747. {
  9748. ManagedTerminal->DirectoryLoaded = Now();
  9749. }
  9750. }
  9751. //---------------------------------------------------------------------------
  9752. void __fastcall TCustomScpExplorerForm::DirViewSelectItem(TObject * Sender,
  9753. TListItem * /*Item*/, bool /*Selected*/)
  9754. {
  9755. TCustomDirView * DirView = reinterpret_cast<TCustomDirView *>(Sender);
  9756. switch (DirView->LastSelectMethod)
  9757. {
  9758. case smKeyboard:
  9759. Configuration->Usage->Inc(L"KeyboardSelections");
  9760. break;
  9761. case smMouse:
  9762. Configuration->Usage->Inc(L"MouseSelections");
  9763. break;
  9764. }
  9765. }
  9766. //---------------------------------------------------------------------------
  9767. int __fastcall TCustomScpExplorerForm::AddSessionColor(TColor Color)
  9768. {
  9769. if (Color != 0)
  9770. {
  9771. return GetSessionColorImage(FSessionColors, Color, FSessionColorMaskImageIndex);
  9772. }
  9773. else
  9774. {
  9775. return FSessionTabImageIndex;
  9776. }
  9777. }
  9778. //---------------------------------------------------------------------------
  9779. int __fastcall TCustomScpExplorerForm::AddFixedSessionImage(int GlyphsSourceIndex)
  9780. {
  9781. TPngImageCollectionItem * Item =
  9782. GlyphsModule->ExplorerImages->PngImages->Items[GlyphsSourceIndex];
  9783. FSessionColors->AddPng(Item->PngImage);
  9784. return FSessionColors->Count - 1;
  9785. }
  9786. //---------------------------------------------------------------------------
  9787. void __fastcall TCustomScpExplorerForm::AddFixedSessionImages()
  9788. {
  9789. FSessionColors->SetSize(GlyphsModule->ExplorerImages->Width, GlyphsModule->ExplorerImages->Height);
  9790. FNewRemoteTabTabImageIndex = AddFixedSessionImage(NonVisualDataModule->NewRemoteTabAction->ImageIndex);
  9791. FNewLocalTabTabImageIndex = AddFixedSessionImage(NonVisualDataModule->NewLocalTabAction->ImageIndex);
  9792. FSessionTabImageIndex = AddFixedSessionImage(SiteImageIndex);
  9793. FSessionColorMaskImageIndex = AddFixedSessionImage(SiteColorMaskImageIndex);
  9794. FLocalBrowserTabImageIndex = AddFixedSessionImage(LocalBrowserImageIndex);
  9795. }
  9796. //---------------------------------------------------------------------------
  9797. int TCustomScpExplorerForm::GetNewTabActionImageIndex()
  9798. {
  9799. return NonVisualDataModule->NewRemoteTabAction->ImageIndex;
  9800. }
  9801. //---------------------------------------------------------------------------
  9802. int TCustomScpExplorerForm::GetNewTabTabImageIndex(TOperationSide Side)
  9803. {
  9804. switch (Side)
  9805. {
  9806. case osLocal:
  9807. return FNewLocalTabTabImageIndex;
  9808. case osRemote:
  9809. case osCurrent:
  9810. return FNewRemoteTabTabImageIndex;
  9811. default:
  9812. DebugFail();
  9813. return -1;
  9814. }
  9815. }
  9816. //---------------------------------------------------------------------------
  9817. void __fastcall TCustomScpExplorerForm::CollectItemsWithTextDisplayMode(TWinControl * Control)
  9818. {
  9819. for (int Index = 0; Index < Control->ControlCount; Index++)
  9820. {
  9821. TControl * AControl = Control->Controls[Index];
  9822. TWinControl * WinControl = dynamic_cast<TWinControl *>(AControl);
  9823. if (WinControl != NULL)
  9824. {
  9825. CollectItemsWithTextDisplayMode(WinControl);
  9826. }
  9827. TTBCustomToolbar * Toolbar = dynamic_cast<TTBCustomToolbar *>(AControl);
  9828. if (Toolbar != NULL)
  9829. {
  9830. // we care for top-level items only
  9831. TTBCustomItem * Items = Toolbar->Items;
  9832. for (int ItemIndex = 0; ItemIndex < Items->Count; ItemIndex++)
  9833. {
  9834. TTBCustomItem * Item = Items->Items[ItemIndex];
  9835. if (((Item->DisplayMode == nbdmImageAndText) ||
  9836. (dynamic_cast<TTBXLabelItem *>(Item) != NULL)) &&
  9837. EligibleForImageDisplayMode(Item))
  9838. {
  9839. FItemsWithTextDisplayMode.insert(Item);
  9840. }
  9841. }
  9842. }
  9843. }
  9844. }
  9845. //---------------------------------------------------------------------------
  9846. bool __fastcall TCustomScpExplorerForm::EligibleForImageDisplayMode(TTBCustomItem * /*Item*/)
  9847. {
  9848. return true;
  9849. }
  9850. //---------------------------------------------------------------------------
  9851. bool __fastcall TCustomScpExplorerForm::UpdateToolbarDisplayMode()
  9852. {
  9853. bool SelectiveToolbarText = WinConfiguration->SelectiveToolbarText;
  9854. TTBItemDisplayMode DisplayMode = (SelectiveToolbarText ? nbdmImageAndText : nbdmDefault);
  9855. typedef std::set<TTBCustomItem *> TItemsWithTextDisplayMode;
  9856. TItemsWithTextDisplayMode::iterator i = FItemsWithTextDisplayMode.begin();
  9857. bool Result = true;
  9858. while (Result && (i != FItemsWithTextDisplayMode.end()))
  9859. {
  9860. TTBCustomItem * Item = *i;
  9861. TTBXLabelItem * Label = dynamic_cast<TTBXLabelItem *>(Item);
  9862. if (Label != NULL)
  9863. {
  9864. // optimization
  9865. if (Label->Visible == SelectiveToolbarText)
  9866. {
  9867. Result = false;
  9868. }
  9869. else
  9870. {
  9871. Label->Visible = SelectiveToolbarText;
  9872. }
  9873. }
  9874. else
  9875. {
  9876. // optimization
  9877. if (Item->DisplayMode == DisplayMode)
  9878. {
  9879. Result = false;
  9880. }
  9881. else
  9882. {
  9883. Item->DisplayMode = DisplayMode;
  9884. }
  9885. }
  9886. ++i;
  9887. }
  9888. UpdateNewTabTab();
  9889. return Result;
  9890. }
  9891. //---------------------------------------------------------------------------
  9892. void __fastcall TCustomScpExplorerForm::DisplaySystemContextMenu()
  9893. {
  9894. DebugFail();
  9895. }
  9896. //---------------------------------------------------------------------------
  9897. bool __fastcall TCustomScpExplorerForm::IsBusy()
  9898. {
  9899. // Among other, a lock level is non zero, while directory is being loaded,
  9900. // even when it happens as a result of dirview navigation,
  9901. // i.e. when TNonVisualDataModule is NOT FBusy.
  9902. // That's why the TNonVisualDataModule::GetBusy calls this method.
  9903. // Among other this prevents a panel auto update to occur while
  9904. // directory is changing.
  9905. return (FLockLevel > 0) || DirView(osCurrent)->IsEditing();
  9906. }
  9907. //---------------------------------------------------------------------------
  9908. Boolean __fastcall TCustomScpExplorerForm::AllowedAction(TAction * Action, TActionAllowed Allowed)
  9909. {
  9910. // While the window is disabled, we still seem to process menu shortcuts at least,
  9911. // so stop it at least here.
  9912. // See also TCustomScpExplorerForm::RemoteDirViewBusy
  9913. bool Result;
  9914. if (Allowed == aaUpdate)
  9915. {
  9916. Result = true;
  9917. }
  9918. else
  9919. {
  9920. Result = !NonVisualDataModule->Busy;
  9921. if (Result && (Allowed == aaShortCut))
  9922. {
  9923. // Otherwise F5 for local-local will be swallowed by disabled local<>remote actions in TCustomScpExplorerForm::KeyDown
  9924. Action->Update();
  9925. Result = Action->Enabled;
  9926. }
  9927. }
  9928. return Result;
  9929. }
  9930. //---------------------------------------------------------------------------
  9931. void __fastcall TCustomScpExplorerForm::EditMenuItemPopup(TTBCustomItem * Sender, bool FromLink)
  9932. {
  9933. NonVisualDataModule->EditMenuItemPopup(Sender, FromLink);
  9934. }
  9935. //---------------------------------------------------------------------------
  9936. void __fastcall TCustomScpExplorerForm::DirViewBusy(TObject * /*Sender*/, int Busy, bool & State)
  9937. {
  9938. // This is somewhat redundant to LockWindow() call from
  9939. // TTerminalManager::TerminalReadDirectoryProgress.
  9940. // But disabling window is known not to block keyboard shorcuts
  9941. // (see TCustomScpExplorerForm::AllowedAction), this hopefully works.
  9942. if (Busy > 0)
  9943. {
  9944. if (NonVisualDataModule->Busy)
  9945. {
  9946. State = false;
  9947. }
  9948. else
  9949. {
  9950. NonVisualDataModule->StartBusy();
  9951. // Will be countered for TCustomDirView.DoExecute in DoDirViewExecFile
  9952. LockWindow();
  9953. }
  9954. }
  9955. else if (Busy < 0)
  9956. {
  9957. UnlockWindow();
  9958. NonVisualDataModule->EndBusy();
  9959. }
  9960. else
  9961. {
  9962. State = NonVisualDataModule->Busy;
  9963. }
  9964. }
  9965. //---------------------------------------------------------------------------
  9966. void __fastcall TCustomScpExplorerForm::SessionsPageControlContextPopup(TObject * /*Sender*/, TPoint & MousePos, bool & Handled)
  9967. {
  9968. int Index = SessionsPageControl->IndexOfTabAt(MousePos.X, MousePos.Y);
  9969. if (Index >= 0)
  9970. {
  9971. TPopupMenu * PopupMenu = NULL;
  9972. TManagedTerminal * Session = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  9973. if (Session != NULL)
  9974. {
  9975. SessionsPageControl->ActivePageIndex = Index;
  9976. if (DebugAlwaysTrue(SessionTabSwitched()))
  9977. {
  9978. // copied from TControl.WMContextMenu
  9979. SendCancelMode(SessionsPageControl);
  9980. // explicit popup instead of using PopupMenu property
  9981. // to avoid menu to popup somewhere within SessionTabSwitched above,
  9982. // while connecting yet not-connected session and hence
  9983. // allowing an access to commands over not-completelly connected session
  9984. PopupMenu = Session->LocalBrowser ? NonVisualDataModule->LocalBrowserPopup : NonVisualDataModule->SessionsPopup;
  9985. }
  9986. }
  9987. else
  9988. {
  9989. if (SupportsLocalBrowser())
  9990. {
  9991. PopupMenu = NonVisualDataModule->NewTabPopup;
  9992. }
  9993. }
  9994. if (PopupMenu != NULL)
  9995. {
  9996. TPoint Point = SessionsPageControl->ClientToScreen(MousePos);
  9997. PopupMenu->PopupComponent = SessionsPageControl;
  9998. PopupMenu->Popup(Point.x, Point.y);
  9999. }
  10000. }
  10001. Handled = true;
  10002. }
  10003. //---------------------------------------------------------------------------
  10004. bool __fastcall TCustomScpExplorerForm::CanChangePassword()
  10005. {
  10006. return HasActiveTerminal() && Terminal->IsCapable[fcChangePassword];
  10007. }
  10008. //---------------------------------------------------------------------------
  10009. void __fastcall TCustomScpExplorerForm::ChangePassword()
  10010. {
  10011. Configuration->Usage->Inc(L"PasswordChanges");
  10012. std::unique_ptr<TSessionData> ChangePasswordData(Terminal->SessionData->Clone());
  10013. ChangePasswordData->DisableAuthentationsExceptPassword();
  10014. ChangePasswordData->ChangePassword = true;
  10015. std::unique_ptr<TTerminal> ChangePasswordSession(
  10016. Terminal->CreateSecondarySession(L"ChangePassword", ChangePasswordData.get()));
  10017. ChangePasswordSession->AutoReadDirectory = false;
  10018. if (TTerminalManager::Instance()->ConnectTerminal(ChangePasswordSession.get()))
  10019. {
  10020. MessageDialog(MainInstructions(LoadStr(PASSWORD_CHANGED)), qtInformation, qaOK, HELP_CHANGE_PASSWORD);
  10021. }
  10022. else
  10023. {
  10024. Configuration->Usage->Inc(L"PasswordChangesFailed");
  10025. }
  10026. }
  10027. //---------------------------------------------------------------------------
  10028. bool __fastcall TCustomScpExplorerForm::CanPrivateKeyUpload()
  10029. {
  10030. // No nice way to assert SSH2
  10031. return HasActiveTerminal() && (Terminal->FSProtocol == cfsSFTP);
  10032. }
  10033. //---------------------------------------------------------------------------
  10034. void __fastcall TCustomScpExplorerForm::PrivateKeyUpload()
  10035. {
  10036. TOperationVisualizer OperationVisualizer;
  10037. UnicodeString FileName = Terminal->SessionData->PublicKeyFile;
  10038. TTerminalManager::Instance()->UploadPublicKey(Terminal, NULL, FileName);
  10039. }
  10040. //---------------------------------------------------------------------------
  10041. void __fastcall TCustomScpExplorerForm::ChangeScale(int M, int D)
  10042. {
  10043. TForm::ChangeScale(M, D);
  10044. int APixelsPerInch = GetControlPixelsPerInch(this);
  10045. HANDLE ResourceModule = GUIConfiguration->ChangeToDefaultResourceModule();
  10046. try
  10047. {
  10048. GlyphsModule->SetPixelsPerInch(APixelsPerInch);
  10049. }
  10050. __finally
  10051. {
  10052. GUIConfiguration->ChangeResourceModule(ResourceModule);
  10053. }
  10054. }
  10055. //---------------------------------------------------------------------------
  10056. void __fastcall TCustomScpExplorerForm::DockContextPopup(TObject * Sender, TPoint & MousePos, bool & /*Handled*/)
  10057. {
  10058. NonVisualDataModule->ControlContextPopup(Sender, MousePos);
  10059. }
  10060. //---------------------------------------------------------------------
  10061. class TPublicControl : public TControl
  10062. {
  10063. friend class TCustomScpExplorerForm;
  10064. };
  10065. //---------------------------------------------------------------------------
  10066. void __fastcall TCustomScpExplorerForm::CopyPopup(TControl * DestControl, TControl * SourceControl)
  10067. {
  10068. static_cast<TPublicControl *>(DestControl)->PopupMenu = static_cast<TPublicControl *>(SourceControl)->PopupMenu;
  10069. static_cast<TPublicControl *>(DestControl)->OnContextPopup = static_cast<TPublicControl *>(SourceControl)->OnContextPopup;
  10070. }
  10071. //---------------------------------------------------------------------------
  10072. void __fastcall TCustomScpExplorerForm::DoBookmarkClick(TOperationSide Side, TObject * Sender)
  10073. {
  10074. TBookmark * Bookmark = DebugNotNull(reinterpret_cast<TBookmark *>(dynamic_cast<TTBCustomItem *>(Sender)->Tag));
  10075. OpenBookmark(Side, Bookmark);
  10076. }
  10077. //---------------------------------------------------------------------------
  10078. void __fastcall TCustomScpExplorerForm::LocalBookmarkClick(TObject * Sender)
  10079. {
  10080. DoBookmarkClick(osLocal, Sender);
  10081. }
  10082. //---------------------------------------------------------------------------
  10083. void __fastcall TCustomScpExplorerForm::RemoteBookmarkClick(TObject * Sender)
  10084. {
  10085. DoBookmarkClick(osRemote, Sender);
  10086. }
  10087. //---------------------------------------------------------------------------
  10088. void __fastcall TCustomScpExplorerForm::CreateOpenDirMenuList(
  10089. TTBCustomItem * Menu, TOperationSide Side, TBookmarkList * BookmarkList)
  10090. {
  10091. if (BookmarkList != NULL)
  10092. {
  10093. TNotifyEvent OnBookmarkClick = (Side == osLocal) ? &LocalBookmarkClick : &RemoteBookmarkClick;
  10094. if (IsLocalBrowserMode())
  10095. {
  10096. Side = osLocal;
  10097. }
  10098. if (!WinConfiguration->UseLocationProfiles)
  10099. {
  10100. std::unique_ptr<TStringList> Directories(new TStringList());
  10101. Directories->CaseSensitive = (Side == osRemote);
  10102. for (int Index = 0; Index < BookmarkList->Count; Index++)
  10103. {
  10104. TBookmark * Bookmark = BookmarkList->Bookmarks[Index];
  10105. UnicodeString Directory = Bookmark->GetSideDirectory(Side);
  10106. if (!Directory.IsEmpty() && (Directories->IndexOf(Directory) < 0))
  10107. {
  10108. std::unique_ptr<TTBCustomItem> Item(new TTBXItem(Owner));
  10109. Item->Caption = EscapeHotkey(Directory);
  10110. Item->ShortCut = Bookmark->ShortCut;
  10111. Item->OnClick = OnBookmarkClick;
  10112. Item->Tag = reinterpret_cast<int>(Bookmark);
  10113. Directories->Add(Directory);
  10114. Menu->Add(Item.release());
  10115. }
  10116. }
  10117. }
  10118. else
  10119. {
  10120. std::unique_ptr<TStrings> Folders(CreateSortedStringList());
  10121. for (int Index = 0; Index < BookmarkList->Count; Index++)
  10122. {
  10123. TBookmark * Bookmark = BookmarkList->Bookmarks[Index];
  10124. if (!Bookmark->Node.IsEmpty())
  10125. {
  10126. Folders->Add(Bookmark->Node);
  10127. }
  10128. }
  10129. for (int Index = 0; Index < Folders->Count; Index++)
  10130. {
  10131. std::unique_ptr<TTBCustomItem> Item(new TTBXSubmenuItem(Owner));
  10132. Item->Caption = Folders->Strings[Index];
  10133. Item->ImageIndex = NonVisualDataModule->RemoteChangePathAction2->ImageIndex;
  10134. Folders->Objects[Index] = Item.get();
  10135. Menu->Add(Item.release());
  10136. }
  10137. for (int Index = 0; Index < BookmarkList->Count; Index++)
  10138. {
  10139. TBookmark * Bookmark = BookmarkList->Bookmarks[Index];
  10140. TTBCustomItem * Parent;
  10141. if (!Bookmark->Node.IsEmpty())
  10142. {
  10143. DebugAssert(Folders->IndexOf(Bookmark->Node) >= 0);
  10144. Parent = dynamic_cast<TTBCustomItem *>(Folders->Objects[Folders->IndexOf(Bookmark->Node)]);
  10145. }
  10146. else
  10147. {
  10148. Parent = Menu;
  10149. }
  10150. std::unique_ptr<TTBCustomItem> Item(new TTBXItem(Owner));
  10151. Item->Caption = Bookmark->Name;
  10152. Item->ShortCut = Bookmark->ShortCut;
  10153. Item->OnClick = OnBookmarkClick;
  10154. Item->Tag = reinterpret_cast<int>(Bookmark);
  10155. if (((Bookmark->Name == Bookmark->Local) || (Bookmark->Name == Bookmark->Remote)) &&
  10156. (Bookmark->Local.IsEmpty() || Bookmark->Remote.IsEmpty()))
  10157. {
  10158. // No hint for location profiles that are actually mere "bookmarks"
  10159. }
  10160. else
  10161. {
  10162. UnicodeString Hint = FORMAT(LoadStrPart(LOCATION_PROFILE_HINT, 1), (Bookmark->Name));
  10163. UnicodeString LongHint;
  10164. if (!Bookmark->Local.IsEmpty())
  10165. {
  10166. AddToList(LongHint, FORMAT(LoadStrPart(LOCATION_PROFILE_HINT, 2), (Bookmark->Local)), L"\n");
  10167. }
  10168. if (!Bookmark->Remote.IsEmpty())
  10169. {
  10170. AddToList(LongHint, FORMAT(LoadStrPart(LOCATION_PROFILE_HINT, 3), (Bookmark->Remote)), L"\n");
  10171. }
  10172. AddToList(Hint, LongHint, L"|");
  10173. Item->Hint = Hint;
  10174. }
  10175. Parent->Add(Item.release());
  10176. }
  10177. }
  10178. AddMenuSeparator(Menu);
  10179. }
  10180. }
  10181. //---------------------------------------------------------------------------
  10182. void __fastcall TCustomScpExplorerForm::CreateOpenDirMenu(TTBCustomItem * Menu, TOperationSide Side)
  10183. {
  10184. Menu->Clear();
  10185. std::unique_ptr<TTBCustomItem> Item;
  10186. Item.reset(new TTBXItem(Owner));
  10187. Item->Action = (Side == osLocal) ? NonVisualDataModule->LocalOpenDirAction : NonVisualDataModule->RemoteOpenDirAction;
  10188. Item->Options = Item->Options << tboDefault;
  10189. Menu->Add(Item.release());
  10190. AddMenuSeparator(Menu);
  10191. if (Terminal != NULL)
  10192. {
  10193. CreateOpenDirMenuList(Menu, Side, WinConfiguration->Bookmarks[Terminal->SessionData->SessionKey]);
  10194. }
  10195. CreateOpenDirMenuList(Menu, Side, WinConfiguration->SharedBookmarks);
  10196. Item.reset(new TTBXItem(Owner));
  10197. Item->Action = (Side == osLocal) ? NonVisualDataModule->LocalAddBookmarkAction2 : NonVisualDataModule->RemoteAddBookmarkAction2;
  10198. Menu->Add(Item.release());
  10199. }
  10200. //---------------------------------------------------------------------------
  10201. bool __fastcall TCustomScpExplorerForm::TryOpenDirectory(TOperationSide Side, const UnicodeString & Path)
  10202. {
  10203. bool Result = true;
  10204. bool Remote = !IsSideLocalBrowser(Side);
  10205. try
  10206. {
  10207. if (Remote)
  10208. {
  10209. Terminal->ExceptionOnFail = true;
  10210. }
  10211. try
  10212. {
  10213. DirView(Side)->Path = Path;
  10214. }
  10215. __finally
  10216. {
  10217. if (Remote)
  10218. {
  10219. Terminal->ExceptionOnFail = false;
  10220. }
  10221. }
  10222. }
  10223. catch (Exception & E)
  10224. {
  10225. if (!Remote || Terminal->Active)
  10226. {
  10227. ShowExtendedExceptionEx(Terminal, &E);
  10228. Result = false;
  10229. }
  10230. else
  10231. {
  10232. throw;
  10233. }
  10234. }
  10235. return Result;
  10236. }
  10237. //---------------------------------------------------------------------------
  10238. void __fastcall TCustomScpExplorerForm::ReloadDirectory(TOperationSide Side)
  10239. {
  10240. // Make sure there some feedback even when loading tiny local folders
  10241. TInstantOperationVisualizer Visualizer;
  10242. DirView(Side)->ReloadDirectory();
  10243. }
  10244. //---------------------------------------------------------------------------
  10245. void __fastcall TCustomScpExplorerForm::CloseSessionTab(int Index)
  10246. {
  10247. if (Index == SessionsPageControl->TabIndex)
  10248. {
  10249. CloseSession();
  10250. }
  10251. else
  10252. {
  10253. TTabSheet * TabSheet = SessionsPageControl->Pages[Index];
  10254. TTerminal * Terminal = GetSessionTabSession(TabSheet);
  10255. TTerminalManager * Manager = TTerminalManager::Instance();
  10256. if (!Terminal->Active)
  10257. {
  10258. Manager->FreeTerminal(Terminal);
  10259. }
  10260. else
  10261. {
  10262. TTerminalQueue * Queue = Manager->FindQueueForTerminal(Terminal);
  10263. if (CanCloseQueue(Queue))
  10264. {
  10265. Manager->FreeTerminal(Terminal);
  10266. }
  10267. }
  10268. }
  10269. }
  10270. //---------------------------------------------------------------------------
  10271. void __fastcall TCustomScpExplorerForm::SessionsPageControlTabButtonClick(TPageControl *, int Index)
  10272. {
  10273. TManagedTerminal * Session = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  10274. if (Session == NULL)
  10275. {
  10276. TRect ButtonRect = SessionsPageControl->TabButtonRect(Index);
  10277. ButtonRect = TRect(SessionsPageControl->ClientToScreen(ButtonRect.TopLeft()), SessionsPageControl->ClientToScreen(ButtonRect.BottomRight()));
  10278. //TPoint P = TPoint(ButtonRect.Left, ButtonRect.Bottom);
  10279. //P = SessionsPageControl->ClientToScreen(P);
  10280. MenuPopup(NonVisualDataModule->NewTabPopup, ButtonRect, SessionsPageControl);
  10281. }
  10282. else
  10283. {
  10284. CloseSessionTab(Index);
  10285. }
  10286. }
  10287. //---------------------------------------------------------------------------
  10288. void __fastcall TCustomScpExplorerForm::CopyFilesToClipboard(TOperationSide Side, bool OnFocused)
  10289. {
  10290. if (DebugAlwaysTrue(!IsSideLocalBrowser(Side)))
  10291. {
  10292. TInstantOperationVisualizer Visualizer;
  10293. // To trigger ClipboardDataObjectRelease (in case the clipboard already contains our data)
  10294. // before we create a new data object
  10295. ClipboardClear();
  10296. DebugAssert(FClipboardFakeDirectory.IsEmpty());
  10297. DebugAssert(FClipboardFakeMonitors.get() == NULL);
  10298. FClipboardFakeDirectory = CreateFakeTransferDirectory();
  10299. // Intermediate variable to WORKAROUND a crash in compiler
  10300. TObjectList * ClipboardFakeMonitors =
  10301. StartCreationDirectoryMonitorsOnEachDrive(FILE_NOTIFY_CHANGE_DIR_NAME, ClipboardFakeCreated);
  10302. FClipboardFakeMonitors.reset(ClipboardFakeMonitors);
  10303. if (FClipboardDragDropFilesEx.get() == NULL)
  10304. {
  10305. FClipboardDragDropFilesEx.reset(new TDragDropFilesEx(this));
  10306. FClipboardDragDropFilesEx->OnDataObjectRelease = ClipboardDataObjectRelease;
  10307. }
  10308. FClipboardDragDropFilesEx->FileList->Clear();
  10309. FClipboardDragDropFilesEx->FileList->AddItem(NULL, FClipboardFakeDirectory);
  10310. FClipboardDragDropFilesEx->CopyToClipboard();
  10311. FClipboardTerminal = Terminal;
  10312. // Need full paths, as cwd can be different once files are pasted
  10313. FClipboardFileList.reset(TRemoteFileList::CloneStrings(RemoteDirView->CreateFileList(OnFocused, true)));
  10314. for (int Index = 0; Index < FClipboardFileList->Count; Index++)
  10315. {
  10316. FClipboardFileList->Strings[Index] = UnixExcludeTrailingBackslash(FClipboardFileList->Strings[Index]);
  10317. }
  10318. }
  10319. }
  10320. //---------------------------------------------------------------------------
  10321. void __fastcall TCustomScpExplorerForm::ClipboardClear()
  10322. {
  10323. if (OpenClipboard(0))
  10324. {
  10325. EmptyClipboard(); // Calls ClipboardDataObjectRelease, if clipboard contains our data
  10326. CloseClipboard();
  10327. }
  10328. }
  10329. //---------------------------------------------------------------------------
  10330. bool __fastcall TCustomScpExplorerForm::DoesClipboardContainOurFiles()
  10331. {
  10332. return !FClipboardFakeDirectory.IsEmpty();
  10333. }
  10334. //---------------------------------------------------------------------------
  10335. void __fastcall TCustomScpExplorerForm::ClipboardStop()
  10336. {
  10337. RemoveDir(ApiPath(FClipboardFakeDirectory));
  10338. FClipboardFakeDirectory = UnicodeString();
  10339. FClipboardTerminal = NULL;
  10340. if (FDownloadingFromClipboard)
  10341. {
  10342. // We are called by the monitor, so attempt to release it would deadlock
  10343. FClipboardFakeMonitorsPendingReset = true;
  10344. }
  10345. else
  10346. {
  10347. FClipboardFakeMonitors.reset(NULL);
  10348. }
  10349. }
  10350. //---------------------------------------------------------------------------
  10351. void __fastcall TCustomScpExplorerForm::ClipboardDataObjectRelease(TObject * /*Sender*/)
  10352. {
  10353. if (DebugAlwaysTrue(!FClipboardFakeDirectory.IsEmpty()))
  10354. {
  10355. ClipboardStop();
  10356. }
  10357. }
  10358. //---------------------------------------------------------------------------
  10359. void __fastcall TCustomScpExplorerForm::ClipboardDownload(const UnicodeString & TargetDirectory, bool NoConfirmation, bool DragDrop)
  10360. {
  10361. TTerminalManager * Manager = TTerminalManager::Instance();
  10362. Manager->ActiveSession = FClipboardTerminal;
  10363. TTransferOperationParam Params;
  10364. Params.TargetDirectory = TargetDirectory;
  10365. Params.DragDrop = DragDrop;
  10366. ExecuteFileOperation(foCopy, osRemote, FClipboardFileList.get(), NoConfirmation, &Params);
  10367. }
  10368. //---------------------------------------------------------------------------
  10369. void __fastcall TCustomScpExplorerForm::ClipboardFakeCreated(TObject * /*Sender*/, const UnicodeString FileName)
  10370. {
  10371. // It can actually rarelly happen that some random file is created, while we are shutting down the monitor
  10372. // (as it pumps a Windows message queue while being shutted down)
  10373. if (DebugAlwaysTrue(!FClipboardFakeDirectory.IsEmpty()) &&
  10374. SameText(ExtractFileName(FileName), ExtractFileName(FClipboardFakeDirectory)))
  10375. {
  10376. // Can fail as it can be e.g. locked by AV, so we retry that later
  10377. bool Removed = RemoveDir(ApiPath(FileName));
  10378. try
  10379. {
  10380. if (!NonVisualDataModule->Busy)
  10381. {
  10382. DebugAssert(!FClipboardFakeMonitorsPendingReset);
  10383. FClipboardFakeMonitorsPendingReset = false;
  10384. bool NoConfirmation = (WinConfiguration->DDTransferConfirmation == asOff);
  10385. TAutoFlag Flag(FDownloadingFromClipboard);
  10386. ClipboardDownload(ExtractFilePath(FileName), NoConfirmation, true);
  10387. }
  10388. }
  10389. __finally
  10390. {
  10391. if (!Removed)
  10392. {
  10393. RemoveDir(ApiPath(FileName));
  10394. }
  10395. }
  10396. }
  10397. }
  10398. //---------------------------------------------------------------------------
  10399. void __fastcall TCustomScpExplorerForm::DirViewGetItemColor(
  10400. TObject * Sender, UnicodeString FileName, bool Directory, __int64 Size, TDateTime Modification, TColor & Color)
  10401. {
  10402. TFileMasks::TParams MaskParams;
  10403. MaskParams.Size = Size;
  10404. MaskParams.Modification = Modification;
  10405. TCustomDirView * DirView = DebugNotNull(dynamic_cast<TCustomDirView *>(Sender));
  10406. bool Local = (dynamic_cast<TUnixDirView *>(DirView) == NULL);
  10407. for (TFileColorData::TList::const_iterator Iter = FFileColors.begin(); Iter != FFileColors.end(); Iter++)
  10408. {
  10409. bool ImplicitMatch;
  10410. if (Iter->FileMask.Matches(FileName, Local, Directory, &MaskParams, false, ImplicitMatch) &&
  10411. !ImplicitMatch)
  10412. {
  10413. Color = Iter->Color;
  10414. break;
  10415. }
  10416. }
  10417. }
  10418. //---------------------------------------------------------------------------
  10419. void __fastcall TCustomScpExplorerForm::DirViewKeyDown(TObject * Sender, WORD & Key, TShiftState)
  10420. {
  10421. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  10422. if (!DirView->IsEditing())
  10423. {
  10424. // Handled here, instead of RemoteDirViewKeyPress (as on Login dialog),
  10425. // as VK_BACK is handled by TCustomDirView.KeyDown and we can swallow it here,
  10426. // while searching
  10427. if (Key == VK_BACK)
  10428. {
  10429. if (!FIncrementalSearch.IsEmpty())
  10430. {
  10431. if (FIncrementalSearch.Length() == 1)
  10432. {
  10433. ResetIncrementalSearch();
  10434. }
  10435. else
  10436. {
  10437. UnicodeString NewText = FIncrementalSearch.SubString(1, FIncrementalSearch.Length() - 1);
  10438. IncrementalSearch(NewText, false, false);
  10439. }
  10440. Key = 0;
  10441. }
  10442. }
  10443. }
  10444. }
  10445. //---------------------------------------------------------------------------
  10446. void __fastcall TCustomScpExplorerForm::DirViewKeyPress(TObject * Sender, wchar_t & Key)
  10447. {
  10448. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  10449. if (!DirView->IsEditing() && (WinConfiguration->PanelSearch != isOff))
  10450. {
  10451. // Filter control sequences.
  10452. // When not searching yet, prefer use of the space for toggling file selection
  10453. // (so we cannot incrementally search for a string starting with a space).
  10454. if ((Key > VK_SPACE) ||
  10455. ((Key == VK_SPACE) && (GetKeyState(VK_CONTROL) >= 0) && !FIncrementalSearch.IsEmpty()))
  10456. {
  10457. IncrementalSearch(FIncrementalSearch + Key, false, false);
  10458. Key = 0;
  10459. }
  10460. }
  10461. }
  10462. //---------------------------------------------------------------------------
  10463. void __fastcall TCustomScpExplorerForm::ResetIncrementalSearch()
  10464. {
  10465. if (!FIncrementalSearch.IsEmpty())
  10466. {
  10467. FIncrementalSearch = L"";
  10468. DirView(osCurrent)->UpdateStatusBar();
  10469. }
  10470. }
  10471. //---------------------------------------------------------------------------
  10472. void __fastcall TCustomScpExplorerForm::IncrementalSearch(const UnicodeString & Text, bool SkipCurrent, bool Reverse)
  10473. {
  10474. TListItem * Item = SearchFile(Text, SkipCurrent, Reverse);
  10475. if (Item == NULL)
  10476. {
  10477. MessageBeep(MB_ICONHAND);
  10478. }
  10479. else
  10480. {
  10481. TCustomDirView * ADirView = DirView(osCurrent);
  10482. {
  10483. TAutoNestingCounter Guard(FIncrementalSearching);
  10484. ADirView->FocusItem(Item);
  10485. if (ADirView->NortonLike == nlOff)
  10486. {
  10487. if ((ADirView->Selected != Item) ||
  10488. (ADirView->SelCount > 1))
  10489. {
  10490. ADirView->ClearSelection();
  10491. ADirView->Selected = Item;
  10492. }
  10493. }
  10494. }
  10495. FIncrementalSearch = Text;
  10496. Item->MakeVisible(false);
  10497. TListItem * NextItem = SearchFile(Text, true, Reverse);
  10498. FIncrementalSearchHaveNext = (NextItem != NULL) && (NextItem != Item);
  10499. ADirView->UpdateStatusBar();
  10500. }
  10501. }
  10502. //---------------------------------------------------------------------------
  10503. TListItem * __fastcall TCustomScpExplorerForm::GetNextFile(TListItem * Item, bool Reverse)
  10504. {
  10505. int Index = Item->Index;
  10506. if (!Reverse)
  10507. {
  10508. Index++;
  10509. if (Index >= Item->Owner->Count)
  10510. {
  10511. Index = 0;
  10512. }
  10513. }
  10514. else
  10515. {
  10516. Index--;
  10517. if (Index < 0)
  10518. {
  10519. Index = Item->Owner->Count - 1;
  10520. }
  10521. }
  10522. return Item->Owner->Item[Index];
  10523. }
  10524. //---------------------------------------------------------------------------
  10525. TListItem * __fastcall TCustomScpExplorerForm::SearchFile(const UnicodeString & Text, bool SkipCurrent, bool Reverse)
  10526. {
  10527. TCustomDirView * ADirView = DirView(osCurrent);
  10528. if (ADirView->Items->Count == 0)
  10529. {
  10530. return NULL;
  10531. }
  10532. else
  10533. {
  10534. TListItem * CurrentItem = (ADirView->ItemFocused != NULL) ? ADirView->ItemFocused : ADirView->Items->Item[0];
  10535. TListItem * Item = CurrentItem;
  10536. if (SkipCurrent)
  10537. {
  10538. Item = DebugNotNull(GetNextFile(Item, Reverse));
  10539. }
  10540. while (true)
  10541. {
  10542. bool Matches = false;
  10543. switch (WinConfiguration->PanelSearch)
  10544. {
  10545. case isNameStartOnly:
  10546. Matches = ContainsTextSemiCaseSensitive(ADirView->ItemFileName(Item).SubString(1, Text.Length()), Text);
  10547. break;
  10548. case isName:
  10549. Matches = ContainsTextSemiCaseSensitive(ADirView->ItemFileName(Item), Text);
  10550. break;
  10551. case isAll:
  10552. int ColCount = (ADirView->ViewStyle == vsReport) ? ADirView->ColProperties->Count : 1;
  10553. int Index = 0;
  10554. while ((Index < ColCount) && !Matches)
  10555. {
  10556. bool Visible = (ADirView->ViewStyle == vsReport) ? ADirView->ColProperties->Visible[Index] : true;
  10557. if (Visible)
  10558. {
  10559. Matches = ContainsTextSemiCaseSensitive(ADirView->GetColumnText(Item, Index), Text);
  10560. }
  10561. Index++;
  10562. }
  10563. break;
  10564. }
  10565. if (Matches)
  10566. {
  10567. return Item;
  10568. }
  10569. Item = GetNextFile(Item, Reverse);
  10570. if (Item == CurrentItem)
  10571. {
  10572. return NULL;
  10573. }
  10574. }
  10575. }
  10576. }
  10577. //---------------------------------------------------------------------------
  10578. void __fastcall TCustomScpExplorerForm::DirViewExit(TObject *)
  10579. {
  10580. ResetIncrementalSearch();
  10581. }
  10582. //---------------------------------------------------------------------------
  10583. void __fastcall TCustomScpExplorerForm::CMDialogKey(TWMKeyDown & Message)
  10584. {
  10585. if (Message.CharCode == VK_TAB)
  10586. {
  10587. if (!FIncrementalSearch.IsEmpty())
  10588. {
  10589. TShiftState Shift = KeyDataToShiftState(Message.KeyData);
  10590. bool Reverse = Shift.Contains(ssShift);
  10591. IncrementalSearch(FIncrementalSearch, true, Reverse);
  10592. Message.Result = 1;
  10593. return;
  10594. }
  10595. }
  10596. else if (Message.CharCode == VK_ESCAPE)
  10597. {
  10598. if (!FIncrementalSearch.IsEmpty())
  10599. {
  10600. ResetIncrementalSearch();
  10601. Message.Result = 1;
  10602. return;
  10603. }
  10604. }
  10605. TForm::Dispatch(&Message);
  10606. }
  10607. //---------------------------------------------------------------------------
  10608. void __fastcall TCustomScpExplorerForm::Deactivate()
  10609. {
  10610. TForm::Deactivate();
  10611. // E.g. when switching to an internal editor window
  10612. ResetIncrementalSearch();
  10613. }
  10614. //---------------------------------------------------------------------------
  10615. void __fastcall TCustomScpExplorerForm::ApplicationEventsDeactivate(TObject *)
  10616. {
  10617. // When switching to another application
  10618. ResetIncrementalSearch();
  10619. }
  10620. //---------------------------------------------------------------------------
  10621. void __fastcall TCustomScpExplorerForm::ApplicationEventsModalBegin(TObject *)
  10622. {
  10623. ResetIncrementalSearch();
  10624. }
  10625. //---------------------------------------------------------------------------
  10626. void __fastcall TCustomScpExplorerForm::DirViewChangeFocus(TObject *, TListItem *)
  10627. {
  10628. if (FIncrementalSearching <= 0)
  10629. {
  10630. ResetIncrementalSearch();
  10631. }
  10632. }
  10633. //---------------------------------------------------------------------------
  10634. void __fastcall TCustomScpExplorerForm::RemoteStatusBarMouseDown(TObject *, TMouseButton, TShiftState, int, int)
  10635. {
  10636. CountClicksForWindowPrint(this);
  10637. }
  10638. //---------------------------------------------------------------------------
  10639. void __fastcall TCustomScpExplorerForm::RemoteDirViewResize(TObject *)
  10640. {
  10641. CenterReconnectToolbar();
  10642. }
  10643. //---------------------------------------------------------------------------
  10644. void __fastcall TCustomScpExplorerForm::BrowseFile()
  10645. {
  10646. if (RemoteDirView->ItemFocused != NULL)
  10647. {
  10648. RemoteDirView->ItemFocused->Selected = true;
  10649. }
  10650. }
  10651. //---------------------------------------------------------------------------
  10652. void __fastcall TCustomScpExplorerForm::UpdateQueueFileList()
  10653. {
  10654. if (!FQueueStatusUpdating)
  10655. {
  10656. bool Refresh;
  10657. if ((FQueueStatus != NULL) && QueueFileList->Visible)
  10658. {
  10659. Refresh = FQueueStatus->UpdateFileList(FQueueController->GetFocusedPrimaryItem(), FQueueFileList.get());
  10660. }
  10661. else
  10662. {
  10663. if (FQueueFileList->GetCount() > 0)
  10664. {
  10665. FQueueFileList->Clear();
  10666. Refresh = true;
  10667. }
  10668. }
  10669. int Count = FQueueFileList->GetCount();
  10670. if (QueueFileList->Items->Count != Count)
  10671. {
  10672. DebugAssert(Refresh);
  10673. QueueFileList->Items->Count = Count;
  10674. }
  10675. if (Refresh)
  10676. {
  10677. QueueFileList->Invalidate();
  10678. }
  10679. DebugAssert(QueueFileList->Items->Count == FQueueFileList->GetCount());
  10680. }
  10681. }
  10682. //---------------------------------------------------------------------------
  10683. void __fastcall TCustomScpExplorerForm::QueueFileListData(TObject *, TListItem * Item)
  10684. {
  10685. Item->Caption = FQueueFileList->GetFileName(Item->Index);
  10686. }
  10687. //---------------------------------------------------------------------------
  10688. void __fastcall TCustomScpExplorerForm::QueueView3Change(TObject *, TListItem *, TItemChange)
  10689. {
  10690. // Should be improved, once we do not refresh the list when switching between details of the same batch.
  10691. // See TSynchronizeChecklistDialog::UpdateTimer
  10692. if (QueueFileList->Items->Count > 0)
  10693. {
  10694. QueueFileList->Items->Item[0]->MakeVisible(false);
  10695. }
  10696. UpdateQueueFileList();
  10697. }
  10698. //---------------------------------------------------------------------------
  10699. void __fastcall TCustomScpExplorerForm::QueueLabelGetStatus(TCustomPathLabel *, bool & Active)
  10700. {
  10701. Active = QueueView3->Focused() || QueueFileList->Focused();
  10702. }
  10703. //---------------------------------------------------------------------------
  10704. void __fastcall TCustomScpExplorerForm::QueueFileListEnterExit(TObject *)
  10705. {
  10706. QueueLabelUpdateStatus();
  10707. }
  10708. //---------------------------------------------------------------------------
  10709. void __fastcall TCustomScpExplorerForm::QueueFileListCustomDrawItem(
  10710. TCustomListView * Sender, TListItem * Item, TCustomDrawState, bool & DebugUsedArg(DefaultDraw))
  10711. {
  10712. int State = FQueueFileList->GetState(Item->Index);
  10713. if (State == qfsProcessed)
  10714. {
  10715. Sender->Canvas->Font->Color = clGrayText;
  10716. }
  10717. }
  10718. //---------------------------------------------------------------------------
  10719. void __fastcall TCustomScpExplorerForm::QueueFileListColumnAutoSize()
  10720. {
  10721. // We do not really need whole width, so we always preemptivelly keep free space for the vertical scrollbar
  10722. QueueFileList->Column[0]->Width =
  10723. QueueFileList->ClientWidth - GetSystemMetricsForControl(QueueFileList, SM_CXVSCROLL) - ScaleByTextHeight(QueueFileList, 8);
  10724. }
  10725. //---------------------------------------------------------------------------
  10726. void __fastcall TCustomScpExplorerForm::QueueFileListResize(TObject *)
  10727. {
  10728. QueueFileListColumnAutoSize();
  10729. }
  10730. //---------------------------------------------------------------------------
  10731. void __fastcall TCustomScpExplorerForm::CloseApp()
  10732. {
  10733. // Called from TNonVisualDataModule::ExplorerActionsExecute, which sets busy flag.
  10734. // FormCloseQuery must check busy state, so that the application cannot be closed while busy by Alt+F4 and X button.
  10735. // So we clear the flag here.
  10736. DebugAssert(NonVisualDataModule->Busy);
  10737. NonVisualDataModule->EndBusy();
  10738. try
  10739. {
  10740. Close();
  10741. }
  10742. __finally
  10743. {
  10744. NonVisualDataModule->StartBusy();
  10745. }
  10746. }
  10747. //---------------------------------------------------------------------------
  10748. bool __fastcall TCustomScpExplorerForm::IsActiveTerminal(TTerminal * Terminal)
  10749. {
  10750. return (Terminal != NULL) && Terminal->Active;
  10751. }
  10752. //---------------------------------------------------------------------------
  10753. bool __fastcall TCustomScpExplorerForm::HasManagedSession()
  10754. {
  10755. return (ManagedSession != NULL);
  10756. }
  10757. //---------------------------------------------------------------------------
  10758. bool __fastcall TCustomScpExplorerForm::HasActiveTerminal()
  10759. {
  10760. return IsActiveTerminal(Terminal);
  10761. }
  10762. //---------------------------------------------------------------------------
  10763. void TCustomScpExplorerForm::LocalLocalCopy(
  10764. TFileOperation, TOperationSide, bool DebugUsedArg(OnFocused), bool DebugUsedArg(NoConfirmation),
  10765. bool DebugUsedArg(DragDrop), unsigned int DebugUsedArg(Flags))
  10766. {
  10767. DebugFail();
  10768. }
  10769. //---------------------------------------------------------------------------
  10770. void TCustomScpExplorerForm::LocalLocalCopyCommand(TFileOperation Operation, TOperationSide Side, bool OnFocused, unsigned int Flags)
  10771. {
  10772. LocalLocalCopy(Operation, Side, OnFocused, !WinConfiguration->ConfirmTransferring, false, Flags);
  10773. }
  10774. //---------------------------------------------------------------------------
  10775. void __fastcall TCustomScpExplorerForm::SessionsPageControlResize(TObject *)
  10776. {
  10777. // Changing tab list triggers the OnResize.
  10778. if (!FUpdatingSessionTabs)
  10779. {
  10780. // UpdateTabsCaptionTruncation should be enough
  10781. SessionListChanged(true);
  10782. }
  10783. }
  10784. //---------------------------------------------------------------------------
  10785. UnicodeString TCustomScpExplorerForm::GetSessionPath(TManagedTerminal * ASession, TOperationSide Side)
  10786. {
  10787. if (ASession == ManagedSession)
  10788. {
  10789. UpdateSession(ASession);
  10790. }
  10791. TSessionData * Data = ASession->StateData;
  10792. UnicodeString Result;
  10793. if (Side == osLocal)
  10794. {
  10795. Result = Data->LocalDirectory;
  10796. }
  10797. else if (DebugAlwaysTrue(Side == osOther))
  10798. {
  10799. Result = ASession->LocalBrowser ? Data->OtherLocalDirectory : Data->RemoteDirectory;
  10800. }
  10801. return Result;
  10802. }
  10803. //---------------------------------------------------------------------------
  10804. UnicodeString TCustomScpExplorerForm::GetTabHintDetails(TManagedTerminal * ASession)
  10805. {
  10806. UnicodeString Result;
  10807. if (!ASession->Active)
  10808. {
  10809. Result = LoadStr(STATUS_NOT_CONNECTED2);
  10810. }
  10811. else
  10812. {
  10813. Result = GetSessionPath(ASession, osRemote);
  10814. }
  10815. return Result;
  10816. }
  10817. //---------------------------------------------------------------------------
  10818. UnicodeString TCustomScpExplorerForm::GetNewTabHintDetails()
  10819. {
  10820. return LoadStr(NEW_REMOTE_TAB_HINT);
  10821. }
  10822. //---------------------------------------------------------------------------
  10823. void __fastcall TCustomScpExplorerForm::SessionsPageControlTabHint(TPageControl *, int Index, UnicodeString & Hint)
  10824. {
  10825. if (Index >= 0)
  10826. {
  10827. TThemeTabSheet * TabSheet = SessionsPageControl->Pages[Index];
  10828. TManagedTerminal * TabSession = GetSessionTabSession(TabSheet);
  10829. if (TabSession == NULL)
  10830. {
  10831. UnicodeString NewTabTabCaption = GetNewTabTabCaption();
  10832. if (NewTabTabCaption != TabSheet->Caption)
  10833. {
  10834. Hint = NewTabTabCaption + L"|" + GetNewTabHintDetails();
  10835. }
  10836. }
  10837. else
  10838. {
  10839. TTerminalManager * Manager = TTerminalManager::Instance();
  10840. UnicodeString TabCaption = Manager->GetSessionTitle(TabSession, true);
  10841. Hint = TabCaption + L"|" + GetTabHintDetails(TabSession);
  10842. }
  10843. }
  10844. }
  10845. //---------------------------------------------------------------------------
  10846. typedef std::vector<TListItem *> TListItemsVector;
  10847. struct TCalculateSizeOperation
  10848. {
  10849. TCalculateSizeStats Stats;
  10850. TListItemsVector ListItems;
  10851. size_t Index;
  10852. };
  10853. //---------------------------------------------------------------------------
  10854. void TCustomScpExplorerForm::CalculateDirectorySizes()
  10855. {
  10856. TCalculateSizeOperation CalculateSizeOperation;
  10857. TOperationSide Side = GetSide(osCurrent);
  10858. TCustomDirView * ADirView = DirView(Side);
  10859. bool FullPath = IsSideLocalBrowser(Side);
  10860. std::unique_ptr<TStrings> AllFileList(ADirView->CreateFileList(false, FullPath, NULL, true));
  10861. std::unique_ptr<TStrings> FileList(new TStringList());
  10862. for (int Index = 0; Index < AllFileList->Count; Index++)
  10863. {
  10864. TListItem * Item = DebugNotNull(dynamic_cast<TListItem *>(AllFileList->Objects[Index]));
  10865. if (ADirView->ItemIsDirectory(Item))
  10866. {
  10867. FileList->AddObject(AllFileList->Strings[Index], ADirView->ItemData(Item));
  10868. CalculateSizeOperation.ListItems.push_back(Item);
  10869. }
  10870. }
  10871. CalculateSizeOperation.Index = 0;
  10872. __int64 Size = 0;
  10873. TValueRestorer<TCalculateSizeOperation *> DirectorySizeOperationRestorer(FCalculateSizeOperation);
  10874. FCalculateSizeOperation = &CalculateSizeOperation;
  10875. TValueRestorer<TFileOperationFinishedEvent> OnFileOperationFinishedRestorer(FOnFileOperationFinished);
  10876. FOnFileOperationFinished = DirectorySizeCalculated;
  10877. TValueRestorer<TFileOperation> PrimaryOperationRestorer(FPrimaryOperation);
  10878. FPrimaryOperation = foCalculateSize;
  10879. TCalculatedSizes CalculatedSizes;
  10880. CalculateSizeOperation.Stats.CalculatedSizes = &CalculatedSizes;
  10881. if (Side == osLocal)
  10882. {
  10883. Terminal->CalculateLocalFilesSize(FileList.get(), Size, NULL, true, NULL, &CalculatedSizes);
  10884. }
  10885. else
  10886. {
  10887. TCalculateSizeParams Params;
  10888. Params.Stats = &CalculateSizeOperation.Stats;
  10889. Params.UseCache = true;
  10890. Terminal->CalculateFilesSize(FileList.get(), Size, Params);
  10891. }
  10892. }
  10893. //---------------------------------------------------------------------------
  10894. void __fastcall TCustomScpExplorerForm::DirectorySizeCalculated(TOperationSide Side, const UnicodeString & FileName, bool)
  10895. {
  10896. DebugAlwaysTrue(FCalculateSizeOperation != NULL);
  10897. TListItem * Item = VisualiseOperationFinished(Side, FileName, false);
  10898. TCustomDirView * ADirView = dynamic_cast<TCustomDirView *>(Item->Owner->Owner);
  10899. // optimization
  10900. bool GotExpectedItem =
  10901. (FCalculateSizeOperation->Index < FCalculateSizeOperation->ListItems.size()) &&
  10902. (FCalculateSizeOperation->ListItems[FCalculateSizeOperation->Index] == Item);
  10903. int Index = -1;
  10904. if (DebugAlwaysTrue(GotExpectedItem))
  10905. {
  10906. Index = FCalculateSizeOperation->Index;
  10907. FCalculateSizeOperation->Index++;
  10908. }
  10909. else
  10910. {
  10911. TListItemsVector::iterator I =
  10912. std::find(FCalculateSizeOperation->ListItems.begin(), FCalculateSizeOperation->ListItems.end(), Item);
  10913. if (DebugAlwaysTrue(I != FCalculateSizeOperation->ListItems.end()))
  10914. {
  10915. Index = I - FCalculateSizeOperation->ListItems.begin();
  10916. }
  10917. }
  10918. if (DebugAlwaysTrue(Index >= 0))
  10919. {
  10920. __int64 Size = (*FCalculateSizeOperation->Stats.CalculatedSizes)[Index];
  10921. ADirView->SetItemCalculatedSize(Item, Size);
  10922. }
  10923. }